drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** This file contains code that is specific to Unix systems. |
| 14 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 15 | #include "sqliteInt.h" |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 16 | #if OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 17 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 18 | /* #define SQLITE_ENABLE_LOCKING_STYLE 0 */ |
| 19 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 20 | /* |
| 21 | ** These #defines should enable >2GB file support on Posix if the |
| 22 | ** underlying operating system supports it. If the OS lacks |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 23 | ** large file support, these should be no-ops. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 24 | ** |
| 25 | ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |
| 26 | ** on the compiler command line. This is necessary if you are compiling |
| 27 | ** on a recent machine (ex: RedHat 7.2) but you want your code to work |
| 28 | ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 |
| 29 | ** without this option, LFS is enable. But LFS does not exist in the kernel |
| 30 | ** in RedHat 6.0, so the code won't work. Hence, for maximum binary |
| 31 | ** portability you should omit LFS. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 32 | */ |
| 33 | #ifndef SQLITE_DISABLE_LFS |
| 34 | # define _LARGE_FILE 1 |
| 35 | # ifndef _FILE_OFFSET_BITS |
| 36 | # define _FILE_OFFSET_BITS 64 |
| 37 | # endif |
| 38 | # define _LARGEFILE_SOURCE 1 |
| 39 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 40 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 41 | /* |
| 42 | ** standard include files. |
| 43 | */ |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 48 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 49 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 50 | #include <errno.h> |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 51 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 52 | #include <sys/ioctl.h> |
| 53 | #include <sys/param.h> |
| 54 | #include <sys/mount.h> |
| 55 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 58 | ** If we are to be thread-safe, include the pthreads header and define |
| 59 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 60 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 61 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 62 | # include <pthread.h> |
| 63 | # define SQLITE_UNIX_THREADS 1 |
| 64 | #endif |
| 65 | |
| 66 | /* |
| 67 | ** Default permissions when creating a new file |
| 68 | */ |
| 69 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 70 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 71 | #endif |
| 72 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 73 | /* |
| 74 | ** Maximum supported path-length. |
| 75 | */ |
| 76 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 80 | ** The unixFile structure is subclass of sqlite3_file specific for the unix |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 81 | ** protability layer. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 82 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 83 | typedef struct unixFile unixFile; |
| 84 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 85 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 86 | #ifdef SQLITE_TEST |
| 87 | /* In test mode, increase the size of this structure a bit so that |
| 88 | ** it is larger than the struct CrashFile defined in test6.c. |
| 89 | */ |
| 90 | char aPadding[32]; |
| 91 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 92 | struct openCnt *pOpen; /* Info about all open fd's on this inode */ |
| 93 | struct lockInfo *pLock; /* Info about locks on this inode */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 94 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 95 | void *lockingContext; /* Locking style specific state */ |
| 96 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 97 | int h; /* The file descriptor */ |
| 98 | unsigned char locktype; /* The type of lock held on this fd */ |
| 99 | unsigned char isOpen; /* True if needs to be closed */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 100 | int dirfd; /* File descriptor for the directory */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 101 | #if SQLITE_THREADSAFE |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 102 | pthread_t tid; /* The thread that "owns" this unixFile */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 103 | #endif |
| 104 | }; |
| 105 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 106 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 107 | ** Include code that is common to all os_*.c files |
| 108 | */ |
| 109 | #include "os_common.h" |
| 110 | |
| 111 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 112 | ** Define various macros that are missing from some systems. |
| 113 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 114 | #ifndef O_LARGEFILE |
| 115 | # define O_LARGEFILE 0 |
| 116 | #endif |
| 117 | #ifdef SQLITE_DISABLE_LFS |
| 118 | # undef O_LARGEFILE |
| 119 | # define O_LARGEFILE 0 |
| 120 | #endif |
| 121 | #ifndef O_NOFOLLOW |
| 122 | # define O_NOFOLLOW 0 |
| 123 | #endif |
| 124 | #ifndef O_BINARY |
| 125 | # define O_BINARY 0 |
| 126 | #endif |
| 127 | |
| 128 | /* |
| 129 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 130 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 131 | ** that always succeeds. This means that locking does not occur under |
danielk1977 | 26c5d79 | 2005-11-25 09:01:23 +0000 | [diff] [blame] | 132 | ** DJGPP. But it's DOS - what did you expect? |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 133 | */ |
| 134 | #ifdef __DJGPP__ |
| 135 | # define fcntl(A,B,C) 0 |
| 136 | #endif |
| 137 | |
| 138 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 139 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 140 | ** testing and debugging only. |
| 141 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 142 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 143 | #define threadid pthread_self() |
| 144 | #else |
| 145 | #define threadid 0 |
| 146 | #endif |
| 147 | |
| 148 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 149 | ** Set or check the unixFile.tid field. This field is set when an unixFile |
| 150 | ** is first opened. All subsequent uses of the unixFile verify that the |
| 151 | ** same thread is operating on the unixFile. Some operating systems do |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 152 | ** not allow locks to be overridden by other threads and that restriction |
| 153 | ** means that sqlite3* database handles cannot be moved from one thread |
| 154 | ** to another. This logic makes sure a user does not try to do that |
| 155 | ** by mistake. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 156 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 157 | ** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 158 | ** another as long as we are running on a system that supports threads |
| 159 | ** overriding each others locks (which now the most common behavior) |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 160 | ** or if no locks are held. But the unixFile.pLock field needs to be |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 161 | ** recomputed because its key includes the thread-id. See the |
| 162 | ** transferOwnership() function below for additional information |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 163 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 164 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 165 | # define SET_THREADID(X) (X)->tid = pthread_self() |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 166 | # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \ |
| 167 | !pthread_equal((X)->tid, pthread_self())) |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 168 | #else |
| 169 | # define SET_THREADID(X) |
| 170 | # define CHECK_THREADID(X) 0 |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 171 | #endif |
| 172 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 173 | /* |
| 174 | ** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996) |
| 175 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 176 | ** sets or clears a lock, that operation overrides any prior locks set |
| 177 | ** by the same process. It does not explicitly say so, but this implies |
| 178 | ** that it overrides locks set by the same process using a different |
| 179 | ** file descriptor. Consider this test case: |
| 180 | ** |
| 181 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
| 182 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 183 | ** |
| 184 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 185 | ** one is a hard or symbolic link to the other) then if you set |
| 186 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 187 | ** on fd2, it works. I would have expected the second lock to |
| 188 | ** fail since there was already a lock on the file due to fd1. |
| 189 | ** But not so. Since both locks came from the same process, the |
| 190 | ** second overrides the first, even though they were on different |
| 191 | ** file descriptors opened on different file names. |
| 192 | ** |
| 193 | ** Bummer. If you ask me, this is broken. Badly broken. It means |
| 194 | ** that we cannot use POSIX locks to synchronize file access among |
| 195 | ** competing threads of the same process. POSIX locks will work fine |
| 196 | ** to synchronize access for threads in separate processes, but not |
| 197 | ** threads within the same process. |
| 198 | ** |
| 199 | ** To work around the problem, SQLite has to manage file locks internally |
| 200 | ** on its own. Whenever a new database is opened, we have to find the |
| 201 | ** specific inode of the database file (the inode is determined by the |
| 202 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 203 | ** and check for locks already existing on that inode. When locks are |
| 204 | ** created or removed, we have to look at our own internal record of the |
| 205 | ** locks to see if another thread has previously set a lock on that same |
| 206 | ** inode. |
| 207 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 208 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 209 | ** descriptor. It is now a structure that holds the integer file |
| 210 | ** descriptor and a pointer to a structure that describes the internal |
| 211 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 212 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 213 | ** point to the same locking structure. The locking structure keeps |
| 214 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 215 | ** field that tells us its internal lock status. cnt==0 means the |
| 216 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 217 | ** cnt>0 means there are cnt shared locks on the file. |
| 218 | ** |
| 219 | ** Any attempt to lock or unlock a file first checks the locking |
| 220 | ** structure. The fcntl() system call is only invoked to set a |
| 221 | ** POSIX lock if the internal lock structure transitions between |
| 222 | ** a locked and an unlocked state. |
| 223 | ** |
| 224 | ** 2004-Jan-11: |
| 225 | ** More recent discoveries about POSIX advisory locks. (The more |
| 226 | ** I discover, the more I realize the a POSIX advisory locks are |
| 227 | ** an abomination.) |
| 228 | ** |
| 229 | ** If you close a file descriptor that points to a file that has locks, |
| 230 | ** all locks on that file that are owned by the current process are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 231 | ** released. To work around this problem, each unixFile structure contains |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 232 | ** a pointer to an openCnt structure. There is one openCnt structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 233 | ** per open inode, which means that multiple unixFile can point to a single |
| 234 | ** openCnt. When an attempt is made to close an unixFile, if there are |
| 235 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 236 | ** to close() the file descriptor is deferred until all of the locks clear. |
| 237 | ** The openCnt structure keeps a list of file descriptors that need to |
| 238 | ** be closed and that list is walked (and cleared) when the last lock |
| 239 | ** clears. |
| 240 | ** |
| 241 | ** First, under Linux threads, because each thread has a separate |
| 242 | ** process ID, lock operations in one thread do not override locks |
| 243 | ** to the same file in other threads. Linux threads behave like |
| 244 | ** separate processes in this respect. But, if you close a file |
| 245 | ** descriptor in linux threads, all locks are cleared, even locks |
| 246 | ** on other threads and even though the other threads have different |
| 247 | ** process IDs. Linux threads is inconsistent in this respect. |
| 248 | ** (I'm beginning to think that linux threads is an abomination too.) |
| 249 | ** The consequence of this all is that the hash table for the lockInfo |
| 250 | ** structure has to include the process id as part of its key because |
| 251 | ** locks in different threads are treated as distinct. But the |
| 252 | ** openCnt structure should not include the process id in its |
| 253 | ** key because close() clears lock on all threads, not just the current |
| 254 | ** thread. Were it not for this goofiness in linux threads, we could |
| 255 | ** combine the lockInfo and openCnt structures into a single structure. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 256 | ** |
| 257 | ** 2004-Jun-28: |
| 258 | ** On some versions of linux, threads can override each others locks. |
| 259 | ** On others not. Sometimes you can change the behavior on the same |
| 260 | ** system by setting the LD_ASSUME_KERNEL environment variable. The |
| 261 | ** POSIX standard is silent as to which behavior is correct, as far |
| 262 | ** as I can tell, so other versions of unix might show the same |
| 263 | ** inconsistency. There is no little doubt in my mind that posix |
| 264 | ** advisory locks and linux threads are profoundly broken. |
| 265 | ** |
| 266 | ** To work around the inconsistencies, we have to test at runtime |
| 267 | ** whether or not threads can override each others locks. This test |
| 268 | ** is run once, the first time any lock is attempted. A static |
| 269 | ** variable is set to record the results of this test for future |
| 270 | ** use. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 271 | */ |
| 272 | |
| 273 | /* |
| 274 | ** An instance of the following structure serves as the key used |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 275 | ** to locate a particular lockInfo structure given its inode. |
| 276 | ** |
| 277 | ** If threads cannot override each others locks, then we set the |
| 278 | ** lockKey.tid field to the thread ID. If threads can override |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 279 | ** each others locks then tid is always set to zero. tid is omitted |
| 280 | ** if we compile without threading support. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 281 | */ |
| 282 | struct lockKey { |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 283 | dev_t dev; /* Device number */ |
| 284 | ino_t ino; /* Inode number */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 285 | #if SQLITE_THREADSAFE |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 286 | pthread_t tid; /* Thread ID or zero if threads can override each other */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 287 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | /* |
| 291 | ** An instance of the following structure is allocated for each open |
| 292 | ** inode on each thread with a different process ID. (Threads have |
| 293 | ** different process IDs on linux, but not on most other unixes.) |
| 294 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 295 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 296 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 297 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 298 | */ |
| 299 | struct lockInfo { |
| 300 | struct lockKey key; /* The lookup key */ |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 301 | int cnt; /* Number of SHARED locks held */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 302 | int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 303 | int nRef; /* Number of pointers to this structure */ |
| 304 | }; |
| 305 | |
| 306 | /* |
| 307 | ** An instance of the following structure serves as the key used |
| 308 | ** to locate a particular openCnt structure given its inode. This |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 309 | ** is the same as the lockKey except that the thread ID is omitted. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 310 | */ |
| 311 | struct openKey { |
| 312 | dev_t dev; /* Device number */ |
| 313 | ino_t ino; /* Inode number */ |
| 314 | }; |
| 315 | |
| 316 | /* |
| 317 | ** An instance of the following structure is allocated for each open |
| 318 | ** inode. This structure keeps track of the number of locks on that |
| 319 | ** inode. If a close is attempted against an inode that is holding |
| 320 | ** locks, the close is deferred until all locks clear by adding the |
| 321 | ** file descriptor to be closed to the pending list. |
| 322 | */ |
| 323 | struct openCnt { |
| 324 | struct openKey key; /* The lookup key */ |
| 325 | int nRef; /* Number of pointers to this structure */ |
| 326 | int nLock; /* Number of outstanding locks */ |
| 327 | int nPending; /* Number of pending close() operations */ |
| 328 | int *aPending; /* Malloced space holding fd's awaiting a close() */ |
| 329 | }; |
| 330 | |
| 331 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 332 | ** These hash tables map inodes and file descriptors (really, lockKey and |
| 333 | ** openKey structures) into lockInfo and openCnt structures. Access to |
| 334 | ** these hash tables must be protected by a mutex. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 335 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 336 | static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0}; |
| 337 | static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0}; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 338 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 339 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 340 | /* |
| 341 | ** The locking styles are associated with the different file locking |
| 342 | ** capabilities supported by different file systems. |
| 343 | ** |
| 344 | ** POSIX locking style fully supports shared and exclusive byte-range locks |
| 345 | ** ADP locking only supports exclusive byte-range locks |
| 346 | ** FLOCK only supports a single file-global exclusive lock |
| 347 | ** DOTLOCK isn't a true locking style, it refers to the use of a special |
| 348 | ** file named the same as the database file with a '.lock' extension, this |
| 349 | ** can be used on file systems that do not offer any reliable file locking |
| 350 | ** NO locking means that no locking will be attempted, this is only used for |
| 351 | ** read-only file systems currently |
| 352 | ** UNSUPPORTED means that no locking will be attempted, this is only used for |
| 353 | ** file systems that are known to be unsupported |
| 354 | */ |
| 355 | typedef enum { |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 356 | posixLockingStyle = 0, /* standard posix-advisory locks */ |
| 357 | afpLockingStyle, /* use afp locks */ |
| 358 | flockLockingStyle, /* use flock() */ |
| 359 | dotlockLockingStyle, /* use <file>.lock files */ |
| 360 | noLockingStyle, /* useful for read-only file system */ |
| 361 | unsupportedLockingStyle /* indicates unsupported file system */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 362 | } sqlite3LockingStyle; |
| 363 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 364 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 365 | /* |
| 366 | ** Helper functions to obtain and relinquish the global mutex. |
| 367 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 368 | static void enterMutex(){ |
drh | 51fc347 | 2007-08-21 13:51:23 +0000 | [diff] [blame] | 369 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 370 | } |
| 371 | static void leaveMutex(){ |
drh | 51fc347 | 2007-08-21 13:51:23 +0000 | [diff] [blame] | 372 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 373 | } |
| 374 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 375 | #if SQLITE_THREADSAFE |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 376 | /* |
| 377 | ** This variable records whether or not threads can override each others |
| 378 | ** locks. |
| 379 | ** |
| 380 | ** 0: No. Threads cannot override each others locks. |
| 381 | ** 1: Yes. Threads can override each others locks. |
| 382 | ** -1: We don't know yet. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 383 | ** |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 384 | ** On some systems, we know at compile-time if threads can override each |
| 385 | ** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro |
| 386 | ** will be set appropriately. On other systems, we have to check at |
| 387 | ** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is |
| 388 | ** undefined. |
| 389 | ** |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 390 | ** This variable normally has file scope only. But during testing, we make |
| 391 | ** it a global so that the test code can change its value in order to verify |
| 392 | ** that the right stuff happens in either case. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 393 | */ |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 394 | #ifndef SQLITE_THREAD_OVERRIDE_LOCK |
| 395 | # define SQLITE_THREAD_OVERRIDE_LOCK -1 |
| 396 | #endif |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 397 | #ifdef SQLITE_TEST |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 398 | int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 399 | #else |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 400 | static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 401 | #endif |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | ** This structure holds information passed into individual test |
| 405 | ** threads by the testThreadLockingBehavior() routine. |
| 406 | */ |
| 407 | struct threadTestData { |
| 408 | int fd; /* File to be locked */ |
| 409 | struct flock lock; /* The locking operation */ |
| 410 | int result; /* Result of the locking operation */ |
| 411 | }; |
| 412 | |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 413 | #ifdef SQLITE_LOCK_TRACE |
| 414 | /* |
| 415 | ** Print out information about all locking operations. |
| 416 | ** |
| 417 | ** This routine is used for troubleshooting locks on multithreaded |
| 418 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 419 | ** command-line option on the compiler. This code is normally |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 420 | ** turned off. |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 421 | */ |
| 422 | static int lockTrace(int fd, int op, struct flock *p){ |
| 423 | char *zOpName, *zType; |
| 424 | int s; |
| 425 | int savedErrno; |
| 426 | if( op==F_GETLK ){ |
| 427 | zOpName = "GETLK"; |
| 428 | }else if( op==F_SETLK ){ |
| 429 | zOpName = "SETLK"; |
| 430 | }else{ |
| 431 | s = fcntl(fd, op, p); |
| 432 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 433 | return s; |
| 434 | } |
| 435 | if( p->l_type==F_RDLCK ){ |
| 436 | zType = "RDLCK"; |
| 437 | }else if( p->l_type==F_WRLCK ){ |
| 438 | zType = "WRLCK"; |
| 439 | }else if( p->l_type==F_UNLCK ){ |
| 440 | zType = "UNLCK"; |
| 441 | }else{ |
| 442 | assert( 0 ); |
| 443 | } |
| 444 | assert( p->l_whence==SEEK_SET ); |
| 445 | s = fcntl(fd, op, p); |
| 446 | savedErrno = errno; |
| 447 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 448 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 449 | (int)p->l_pid, s); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 450 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 451 | struct flock l2; |
| 452 | l2 = *p; |
| 453 | fcntl(fd, F_GETLK, &l2); |
| 454 | if( l2.l_type==F_RDLCK ){ |
| 455 | zType = "RDLCK"; |
| 456 | }else if( l2.l_type==F_WRLCK ){ |
| 457 | zType = "WRLCK"; |
| 458 | }else if( l2.l_type==F_UNLCK ){ |
| 459 | zType = "UNLCK"; |
| 460 | }else{ |
| 461 | assert( 0 ); |
| 462 | } |
| 463 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 464 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 465 | } |
| 466 | errno = savedErrno; |
| 467 | return s; |
| 468 | } |
| 469 | #define fcntl lockTrace |
| 470 | #endif /* SQLITE_LOCK_TRACE */ |
| 471 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 472 | /* |
| 473 | ** The testThreadLockingBehavior() routine launches two separate |
| 474 | ** threads on this routine. This routine attempts to lock a file |
| 475 | ** descriptor then returns. The success or failure of that attempt |
| 476 | ** allows the testThreadLockingBehavior() procedure to determine |
| 477 | ** whether or not threads can override each others locks. |
| 478 | */ |
| 479 | static void *threadLockingTest(void *pArg){ |
| 480 | struct threadTestData *pData = (struct threadTestData*)pArg; |
| 481 | pData->result = fcntl(pData->fd, F_SETLK, &pData->lock); |
| 482 | return pArg; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | ** This procedure attempts to determine whether or not threads |
| 487 | ** can override each others locks then sets the |
| 488 | ** threadsOverrideEachOthersLocks variable appropriately. |
| 489 | */ |
danielk1977 | 4d5238f | 2006-01-27 06:32:00 +0000 | [diff] [blame] | 490 | static void testThreadLockingBehavior(int fd_orig){ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 491 | int fd; |
| 492 | struct threadTestData d[2]; |
| 493 | pthread_t t[2]; |
| 494 | |
| 495 | fd = dup(fd_orig); |
| 496 | if( fd<0 ) return; |
| 497 | memset(d, 0, sizeof(d)); |
| 498 | d[0].fd = fd; |
| 499 | d[0].lock.l_type = F_RDLCK; |
| 500 | d[0].lock.l_len = 1; |
| 501 | d[0].lock.l_start = 0; |
| 502 | d[0].lock.l_whence = SEEK_SET; |
| 503 | d[1] = d[0]; |
| 504 | d[1].lock.l_type = F_WRLCK; |
| 505 | pthread_create(&t[0], 0, threadLockingTest, &d[0]); |
| 506 | pthread_create(&t[1], 0, threadLockingTest, &d[1]); |
| 507 | pthread_join(t[0], 0); |
| 508 | pthread_join(t[1], 0); |
| 509 | close(fd); |
| 510 | threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0; |
| 511 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 512 | #endif /* SQLITE_THREADSAFE */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 513 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 514 | /* |
| 515 | ** Release a lockInfo structure previously allocated by findLockInfo(). |
| 516 | */ |
| 517 | static void releaseLockInfo(struct lockInfo *pLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 518 | if (pLock == NULL) |
| 519 | return; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 520 | pLock->nRef--; |
| 521 | if( pLock->nRef==0 ){ |
| 522 | sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 523 | sqlite3_free(pLock); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | ** Release a openCnt structure previously allocated by findLockInfo(). |
| 529 | */ |
| 530 | static void releaseOpenCnt(struct openCnt *pOpen){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 531 | if (pOpen == NULL) |
| 532 | return; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 533 | pOpen->nRef--; |
| 534 | if( pOpen->nRef==0 ){ |
| 535 | sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0); |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 536 | free(pOpen->aPending); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 537 | sqlite3_free(pOpen); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 541 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 542 | /* |
| 543 | ** Tests a byte-range locking query to see if byte range locks are |
| 544 | ** supported, if not we fall back to dotlockLockingStyle. |
| 545 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 546 | static sqlite3LockingStyle sqlite3TestLockingStyle( |
| 547 | const char *filePath, |
| 548 | int fd |
| 549 | ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 550 | /* test byte-range lock using fcntl */ |
| 551 | struct flock lockInfo; |
| 552 | |
| 553 | lockInfo.l_len = 1; |
| 554 | lockInfo.l_start = 0; |
| 555 | lockInfo.l_whence = SEEK_SET; |
| 556 | lockInfo.l_type = F_RDLCK; |
| 557 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 558 | if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 559 | return posixLockingStyle; |
| 560 | } |
| 561 | |
| 562 | /* testing for flock can give false positives. So if if the above test |
| 563 | ** fails, then we fall back to using dot-lock style locking. |
| 564 | */ |
| 565 | return dotlockLockingStyle; |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | ** Examines the f_fstypename entry in the statfs structure as returned by |
| 570 | ** stat() for the file system hosting the database file, assigns the |
| 571 | ** appropriate locking style based on it's value. These values and |
| 572 | ** assignments are based on Darwin/OSX behavior and have not been tested on |
| 573 | ** other systems. |
| 574 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 575 | static sqlite3LockingStyle sqlite3DetectLockingStyle( |
| 576 | const char *filePath, |
| 577 | int fd |
| 578 | ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 579 | |
| 580 | #ifdef SQLITE_FIXED_LOCKING_STYLE |
| 581 | return (sqlite3LockingStyle)SQLITE_FIXED_LOCKING_STYLE; |
| 582 | #else |
| 583 | struct statfs fsInfo; |
| 584 | |
| 585 | if (statfs(filePath, &fsInfo) == -1) |
| 586 | return sqlite3TestLockingStyle(filePath, fd); |
| 587 | |
| 588 | if (fsInfo.f_flags & MNT_RDONLY) |
| 589 | return noLockingStyle; |
| 590 | |
| 591 | if( (!strcmp(fsInfo.f_fstypename, "hfs")) || |
| 592 | (!strcmp(fsInfo.f_fstypename, "ufs")) ) |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 593 | return posixLockingStyle; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 594 | |
| 595 | if(!strcmp(fsInfo.f_fstypename, "afpfs")) |
| 596 | return afpLockingStyle; |
| 597 | |
| 598 | if(!strcmp(fsInfo.f_fstypename, "nfs")) |
| 599 | return sqlite3TestLockingStyle(filePath, fd); |
| 600 | |
| 601 | if(!strcmp(fsInfo.f_fstypename, "smbfs")) |
| 602 | return flockLockingStyle; |
| 603 | |
| 604 | if(!strcmp(fsInfo.f_fstypename, "msdos")) |
| 605 | return dotlockLockingStyle; |
| 606 | |
| 607 | if(!strcmp(fsInfo.f_fstypename, "webdav")) |
| 608 | return unsupportedLockingStyle; |
| 609 | |
| 610 | return sqlite3TestLockingStyle(filePath, fd); |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 611 | #endif /* SQLITE_FIXED_LOCKING_STYLE */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 615 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 616 | /* |
| 617 | ** Given a file descriptor, locate lockInfo and openCnt structures that |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 618 | ** describes that file descriptor. Create new ones if necessary. The |
| 619 | ** return values might be uninitialized if an error occurs. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 620 | ** |
| 621 | ** Return the number of errors. |
| 622 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 623 | static int findLockInfo( |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 624 | int fd, /* The file descriptor used in the key */ |
| 625 | struct lockInfo **ppLock, /* Return the lockInfo structure here */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 626 | struct openCnt **ppOpen /* Return the openCnt structure here */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 627 | ){ |
| 628 | int rc; |
| 629 | struct lockKey key1; |
| 630 | struct openKey key2; |
| 631 | struct stat statbuf; |
| 632 | struct lockInfo *pLock; |
| 633 | struct openCnt *pOpen; |
| 634 | rc = fstat(fd, &statbuf); |
| 635 | if( rc!=0 ) return 1; |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 636 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 637 | memset(&key1, 0, sizeof(key1)); |
| 638 | key1.dev = statbuf.st_dev; |
| 639 | key1.ino = statbuf.st_ino; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 640 | #if SQLITE_THREADSAFE |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 641 | if( threadsOverrideEachOthersLocks<0 ){ |
| 642 | testThreadLockingBehavior(fd); |
| 643 | } |
| 644 | key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self(); |
| 645 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 646 | memset(&key2, 0, sizeof(key2)); |
| 647 | key2.dev = statbuf.st_dev; |
| 648 | key2.ino = statbuf.st_ino; |
| 649 | pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1)); |
| 650 | if( pLock==0 ){ |
| 651 | struct lockInfo *pOld; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 652 | pLock = sqlite3_malloc( sizeof(*pLock) ); |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 653 | if( pLock==0 ){ |
| 654 | rc = 1; |
| 655 | goto exit_findlockinfo; |
| 656 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 657 | pLock->key = key1; |
| 658 | pLock->nRef = 1; |
| 659 | pLock->cnt = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 660 | pLock->locktype = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 661 | pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock); |
| 662 | if( pOld!=0 ){ |
| 663 | assert( pOld==pLock ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 664 | sqlite3_free(pLock); |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 665 | rc = 1; |
| 666 | goto exit_findlockinfo; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 667 | } |
| 668 | }else{ |
| 669 | pLock->nRef++; |
| 670 | } |
| 671 | *ppLock = pLock; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 672 | if( ppOpen!=0 ){ |
| 673 | pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2)); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 674 | if( pOpen==0 ){ |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 675 | struct openCnt *pOld; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 676 | pOpen = sqlite3_malloc( sizeof(*pOpen) ); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 677 | if( pOpen==0 ){ |
| 678 | releaseLockInfo(pLock); |
| 679 | rc = 1; |
| 680 | goto exit_findlockinfo; |
| 681 | } |
| 682 | pOpen->key = key2; |
| 683 | pOpen->nRef = 1; |
| 684 | pOpen->nLock = 0; |
| 685 | pOpen->nPending = 0; |
| 686 | pOpen->aPending = 0; |
| 687 | pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen); |
| 688 | if( pOld!=0 ){ |
| 689 | assert( pOld==pOpen ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 690 | sqlite3_free(pOpen); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 691 | releaseLockInfo(pLock); |
| 692 | rc = 1; |
| 693 | goto exit_findlockinfo; |
| 694 | } |
| 695 | }else{ |
| 696 | pOpen->nRef++; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 697 | } |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 698 | *ppOpen = pOpen; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 699 | } |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 700 | |
| 701 | exit_findlockinfo: |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 702 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 703 | } |
| 704 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 705 | #ifdef SQLITE_DEBUG |
| 706 | /* |
| 707 | ** Helper function for printing out trace information from debugging |
| 708 | ** binaries. This returns the string represetation of the supplied |
| 709 | ** integer lock-type. |
| 710 | */ |
| 711 | static const char *locktypeName(int locktype){ |
| 712 | switch( locktype ){ |
| 713 | case NO_LOCK: return "NONE"; |
| 714 | case SHARED_LOCK: return "SHARED"; |
| 715 | case RESERVED_LOCK: return "RESERVED"; |
| 716 | case PENDING_LOCK: return "PENDING"; |
| 717 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
| 718 | } |
| 719 | return "ERROR"; |
| 720 | } |
| 721 | #endif |
| 722 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 723 | /* |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 724 | ** If we are currently in a different thread than the thread that the |
| 725 | ** unixFile argument belongs to, then transfer ownership of the unixFile |
| 726 | ** over to the current thread. |
| 727 | ** |
| 728 | ** A unixFile is only owned by a thread on systems where one thread is |
| 729 | ** unable to override locks created by a different thread. RedHat9 is |
| 730 | ** an example of such a system. |
| 731 | ** |
| 732 | ** Ownership transfer is only allowed if the unixFile is currently unlocked. |
| 733 | ** If the unixFile is locked and an ownership is wrong, then return |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 734 | ** SQLITE_MISUSE. SQLITE_OK is returned if everything works. |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 735 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 736 | #if SQLITE_THREADSAFE |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 737 | static int transferOwnership(unixFile *pFile){ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 738 | int rc; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 739 | pthread_t hSelf; |
| 740 | if( threadsOverrideEachOthersLocks ){ |
| 741 | /* Ownership transfers not needed on this system */ |
| 742 | return SQLITE_OK; |
| 743 | } |
| 744 | hSelf = pthread_self(); |
| 745 | if( pthread_equal(pFile->tid, hSelf) ){ |
| 746 | /* We are still in the same thread */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 747 | OSTRACE1("No-transfer, same thread\n"); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 748 | return SQLITE_OK; |
| 749 | } |
| 750 | if( pFile->locktype!=NO_LOCK ){ |
| 751 | /* We cannot change ownership while we are holding a lock! */ |
| 752 | return SQLITE_MISUSE; |
| 753 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 754 | OSTRACE4("Transfer ownership of %d from %d to %d\n", |
| 755 | pFile->h, pFile->tid, hSelf); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 756 | pFile->tid = hSelf; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 757 | if (pFile->pLock != NULL) { |
| 758 | releaseLockInfo(pFile->pLock); |
| 759 | rc = findLockInfo(pFile->h, &pFile->pLock, 0); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 760 | OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 761 | locktypeName(pFile->locktype), |
| 762 | locktypeName(pFile->pLock->locktype), pFile->pLock->cnt); |
| 763 | return rc; |
| 764 | } else { |
| 765 | return SQLITE_OK; |
| 766 | } |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 767 | } |
| 768 | #else |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 769 | /* On single-threaded builds, ownership transfer is a no-op */ |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 770 | # define transferOwnership(X) SQLITE_OK |
| 771 | #endif |
| 772 | |
| 773 | /* |
danielk1977 | 2a6bdf6 | 2007-08-20 16:07:00 +0000 | [diff] [blame] | 774 | ** Seek to the offset passed as the second argument, then read cnt |
| 775 | ** bytes into pBuf. Return the number of bytes actually read. |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 776 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 777 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 778 | int got; |
drh | 8ebf670 | 2007-02-06 11:11:08 +0000 | [diff] [blame] | 779 | i64 newOffset; |
drh | 15d00c4 | 2007-02-27 02:01:14 +0000 | [diff] [blame] | 780 | TIMER_START; |
drh | 8350a21 | 2007-03-22 15:22:06 +0000 | [diff] [blame] | 781 | #if defined(USE_PREAD) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 782 | got = pread(id->h, pBuf, cnt, offset); |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 783 | SimulateIOError( got = -1 ); |
drh | 8350a21 | 2007-03-22 15:22:06 +0000 | [diff] [blame] | 784 | #elif defined(USE_PREAD64) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 785 | got = pread64(id->h, pBuf, cnt, offset); |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 786 | SimulateIOError( got = -1 ); |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 787 | #else |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 788 | newOffset = lseek(id->h, offset, SEEK_SET); |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 789 | SimulateIOError( newOffset-- ); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 790 | if( newOffset!=offset ){ |
drh | 8ebf670 | 2007-02-06 11:11:08 +0000 | [diff] [blame] | 791 | return -1; |
| 792 | } |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 793 | got = read(id->h, pBuf, cnt); |
| 794 | #endif |
drh | 15d00c4 | 2007-02-27 02:01:14 +0000 | [diff] [blame] | 795 | TIMER_END; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 796 | OSTRACE5("READ %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED); |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 797 | return got; |
| 798 | } |
| 799 | |
| 800 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 801 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 802 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 803 | ** wrong. |
| 804 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 805 | static int unixRead( |
| 806 | sqlite3_file *id, |
| 807 | void *pBuf, |
| 808 | int amt, |
| 809 | sqlite3_int64 offset |
| 810 | ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 811 | int got; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 812 | assert( id ); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 813 | got = seekAndRead((unixFile*)id, offset, pBuf, amt); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 814 | if( got==amt ){ |
| 815 | return SQLITE_OK; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 816 | }else if( got<0 ){ |
| 817 | return SQLITE_IOERR_READ; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 818 | }else{ |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 819 | memset(&((char*)pBuf)[got], 0, amt-got); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 820 | return SQLITE_IOERR_SHORT_READ; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
| 824 | /* |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 825 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 826 | ** Return the number of bytes actually read. Update the offset. |
| 827 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 828 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 829 | int got; |
drh | 8ebf670 | 2007-02-06 11:11:08 +0000 | [diff] [blame] | 830 | i64 newOffset; |
drh | 15d00c4 | 2007-02-27 02:01:14 +0000 | [diff] [blame] | 831 | TIMER_START; |
drh | 8350a21 | 2007-03-22 15:22:06 +0000 | [diff] [blame] | 832 | #if defined(USE_PREAD) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 833 | got = pwrite(id->h, pBuf, cnt, offset); |
drh | 8350a21 | 2007-03-22 15:22:06 +0000 | [diff] [blame] | 834 | #elif defined(USE_PREAD64) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 835 | got = pwrite64(id->h, pBuf, cnt, offset); |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 836 | #else |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 837 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 838 | if( newOffset!=offset ){ |
drh | 8ebf670 | 2007-02-06 11:11:08 +0000 | [diff] [blame] | 839 | return -1; |
| 840 | } |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 841 | got = write(id->h, pBuf, cnt); |
| 842 | #endif |
drh | 15d00c4 | 2007-02-27 02:01:14 +0000 | [diff] [blame] | 843 | TIMER_END; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 844 | OSTRACE5("WRITE %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED); |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 845 | return got; |
| 846 | } |
| 847 | |
| 848 | |
| 849 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 850 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 851 | ** or some other error code on failure. |
| 852 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 853 | static int unixWrite( |
| 854 | sqlite3_file *id, |
| 855 | const void *pBuf, |
| 856 | int amt, |
| 857 | sqlite3_int64 offset |
| 858 | ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 859 | int wrote = 0; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 860 | assert( id ); |
drh | 4c7f941 | 2005-02-03 00:29:47 +0000 | [diff] [blame] | 861 | assert( amt>0 ); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 862 | while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 863 | amt -= wrote; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 864 | offset += wrote; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 865 | pBuf = &((char*)pBuf)[wrote]; |
| 866 | } |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 867 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 868 | SimulateDiskfullError(( wrote=0, amt=1 )); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 869 | if( amt>0 ){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 870 | if( wrote<0 ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 871 | return SQLITE_IOERR_WRITE; |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 872 | }else{ |
| 873 | return SQLITE_FULL; |
| 874 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 875 | } |
| 876 | return SQLITE_OK; |
| 877 | } |
| 878 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 879 | #ifdef SQLITE_TEST |
| 880 | /* |
| 881 | ** Count the number of fullsyncs and normal syncs. This is used to test |
| 882 | ** that syncs and fullsyncs are occuring at the right times. |
| 883 | */ |
| 884 | int sqlite3_sync_count = 0; |
| 885 | int sqlite3_fullsync_count = 0; |
| 886 | #endif |
| 887 | |
drh | f2f2391 | 2005-10-05 10:29:36 +0000 | [diff] [blame] | 888 | /* |
| 889 | ** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined. |
| 890 | ** Otherwise use fsync() in its place. |
| 891 | */ |
| 892 | #ifndef HAVE_FDATASYNC |
| 893 | # define fdatasync fsync |
| 894 | #endif |
| 895 | |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 896 | /* |
| 897 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 898 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 899 | ** only available on Mac OS X. But that could change. |
| 900 | */ |
| 901 | #ifdef F_FULLFSYNC |
| 902 | # define HAVE_FULLFSYNC 1 |
| 903 | #else |
| 904 | # define HAVE_FULLFSYNC 0 |
| 905 | #endif |
| 906 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 907 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 908 | /* |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 909 | ** The fsync() system call does not work as advertised on many |
| 910 | ** unix systems. The following procedure is an attempt to make |
| 911 | ** it work better. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 912 | ** |
| 913 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 914 | ** for testing when we want to run through the test suite quickly. |
| 915 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 916 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 917 | ** or power failure will likely corrupt the database file. |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 918 | */ |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 919 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 920 | int rc; |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 921 | |
| 922 | /* Record the number of times that we do a normal fsync() and |
| 923 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 924 | ** gets called with the correct arguments. |
| 925 | */ |
| 926 | #ifdef SQLITE_TEST |
| 927 | if( fullSync ) sqlite3_fullsync_count++; |
| 928 | sqlite3_sync_count++; |
| 929 | #endif |
| 930 | |
| 931 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 932 | ** no-op |
| 933 | */ |
| 934 | #ifdef SQLITE_NO_SYNC |
| 935 | rc = SQLITE_OK; |
| 936 | #else |
| 937 | |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 938 | #if HAVE_FULLFSYNC |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 939 | if( fullSync ){ |
drh | f30cc94 | 2005-03-11 17:52:34 +0000 | [diff] [blame] | 940 | rc = fcntl(fd, F_FULLFSYNC, 0); |
aswift | ae0943b | 2007-01-31 23:37:07 +0000 | [diff] [blame] | 941 | }else{ |
| 942 | rc = 1; |
| 943 | } |
| 944 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
| 945 | * It shouldn't be possible for fullfsync to fail on the local |
| 946 | * file system (on OSX), so failure indicates that FULLFSYNC |
| 947 | * isn't supported for this file system. So, attempt an fsync |
| 948 | * and (for now) ignore the overhead of a superfluous fcntl call. |
| 949 | * It'd be better to detect fullfsync support once and avoid |
| 950 | * the fcntl call every time sync is called. |
| 951 | */ |
| 952 | if( rc ) rc = fsync(fd); |
| 953 | |
| 954 | #else |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 955 | if( dataOnly ){ |
| 956 | rc = fdatasync(fd); |
drh | f2f2391 | 2005-10-05 10:29:36 +0000 | [diff] [blame] | 957 | }else{ |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 958 | rc = fsync(fd); |
| 959 | } |
aswift | ae0943b | 2007-01-31 23:37:07 +0000 | [diff] [blame] | 960 | #endif /* HAVE_FULLFSYNC */ |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 961 | #endif /* defined(SQLITE_NO_SYNC) */ |
| 962 | |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 963 | return rc; |
| 964 | } |
| 965 | |
| 966 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 967 | ** Make sure all writes to a particular file are committed to disk. |
| 968 | ** |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 969 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 970 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 971 | ** file data is synced. |
| 972 | ** |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 973 | ** Under Unix, also make sure that the directory entry for the file |
| 974 | ** has been created by fsync-ing the directory that contains the file. |
| 975 | ** If we do not do this and we encounter a power failure, the directory |
| 976 | ** entry for the journal might not exist after we reboot. The next |
| 977 | ** SQLite to access the file will not know that the journal exists (because |
| 978 | ** the directory entry for the journal was never created) and the transaction |
| 979 | ** will not roll back - possibly leading to database corruption. |
| 980 | */ |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 981 | static int unixSync(sqlite3_file *id, int flags){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 982 | int rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 983 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 984 | |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 985 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 986 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 987 | |
| 988 | /* Check that one of SQLITE_SYNC_NORMAL, FULL or BARRIER was passed */ |
| 989 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 990 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 991 | || (flags&0x0F)==SQLITE_SYNC_BARRIER |
| 992 | ); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 993 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 994 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 995 | OSTRACE2("SYNC %-3d\n", pFile->h); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 996 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 997 | SimulateIOError( rc=1 ); |
| 998 | if( rc ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 999 | return SQLITE_IOERR_FSYNC; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1000 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1001 | if( pFile->dirfd>=0 ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1002 | OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1003 | HAVE_FULLFSYNC, isFullsync); |
danielk1977 | d7c03f7 | 2005-11-25 10:38:22 +0000 | [diff] [blame] | 1004 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1005 | /* The directory sync is only attempted if full_fsync is |
| 1006 | ** turned off or unavailable. If a full_fsync occurred above, |
| 1007 | ** then the directory sync is superfluous. |
| 1008 | */ |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1009 | if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1010 | /* |
| 1011 | ** We have received multiple reports of fsync() returning |
drh | 86631a5 | 2006-02-09 23:05:51 +0000 | [diff] [blame] | 1012 | ** errors when applied to directories on certain file systems. |
| 1013 | ** A failed directory sync is not a big deal. So it seems |
| 1014 | ** better to ignore the error. Ticket #1657 |
| 1015 | */ |
| 1016 | /* return SQLITE_IOERR; */ |
danielk1977 | 0964b23 | 2005-11-25 08:47:57 +0000 | [diff] [blame] | 1017 | } |
danielk1977 | d7c03f7 | 2005-11-25 10:38:22 +0000 | [diff] [blame] | 1018 | #endif |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1019 | close(pFile->dirfd); /* Only need to sync once, so close the directory */ |
| 1020 | pFile->dirfd = -1; /* when we are done. */ |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 1021 | } |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 1022 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | ** Truncate an open file to a specified size |
| 1027 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1028 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1029 | int rc; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1030 | assert( id ); |
drh | 63fff5f | 2007-06-19 10:50:38 +0000 | [diff] [blame] | 1031 | rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1032 | SimulateIOError( rc=1 ); |
| 1033 | if( rc ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1034 | return SQLITE_IOERR_TRUNCATE; |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1035 | }else{ |
| 1036 | return SQLITE_OK; |
| 1037 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | ** Determine the current size of a file in bytes |
| 1042 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1043 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1044 | int rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1045 | struct stat buf; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1046 | assert( id ); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1047 | rc = fstat(((unixFile*)id)->h, &buf); |
| 1048 | SimulateIOError( rc=1 ); |
| 1049 | if( rc!=0 ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1050 | return SQLITE_IOERR_FSTAT; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1051 | } |
| 1052 | *pSize = buf.st_size; |
| 1053 | return SQLITE_OK; |
| 1054 | } |
| 1055 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1056 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1057 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1058 | ** file by this or any other process. If such a lock is held, return |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1059 | ** non-zero. If the file is unlocked or holds only SHARED locks, then |
| 1060 | ** return zero. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1061 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1062 | static int unixCheckReservedLock(sqlite3_file *id){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1063 | int r = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1064 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1065 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1066 | assert( pFile ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1067 | enterMutex(); /* Because pFile->pLock is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1068 | |
| 1069 | /* Check if a thread in this process holds such a lock */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1070 | if( pFile->pLock->locktype>SHARED_LOCK ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1071 | r = 1; |
| 1072 | } |
| 1073 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1074 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1075 | */ |
| 1076 | if( !r ){ |
| 1077 | struct flock lock; |
| 1078 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1079 | lock.l_start = RESERVED_BYTE; |
| 1080 | lock.l_len = 1; |
| 1081 | lock.l_type = F_WRLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1082 | fcntl(pFile->h, F_GETLK, &lock); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1083 | if( lock.l_type!=F_UNLCK ){ |
| 1084 | r = 1; |
| 1085 | } |
| 1086 | } |
| 1087 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1088 | leaveMutex(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1089 | OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1090 | |
| 1091 | return r; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1095 | ** Lock the file with the lock specified by parameter locktype - one |
| 1096 | ** of the following: |
| 1097 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1098 | ** (1) SHARED_LOCK |
| 1099 | ** (2) RESERVED_LOCK |
| 1100 | ** (3) PENDING_LOCK |
| 1101 | ** (4) EXCLUSIVE_LOCK |
| 1102 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1103 | ** Sometimes when requesting one lock state, additional lock states |
| 1104 | ** are inserted in between. The locking might fail on one of the later |
| 1105 | ** transitions leaving the lock state different from what it started but |
| 1106 | ** still short of its goal. The following chart shows the allowed |
| 1107 | ** transitions and the inserted intermediate states: |
| 1108 | ** |
| 1109 | ** UNLOCKED -> SHARED |
| 1110 | ** SHARED -> RESERVED |
| 1111 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1112 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1113 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1114 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1115 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1116 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1117 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1118 | static int unixLock(sqlite3_file *id, int locktype){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1119 | /* The following describes the implementation of the various locks and |
| 1120 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1121 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1122 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1123 | ** slightly in order to be compatible with windows systems simultaneously |
| 1124 | ** accessing the same database file, in case that is ever required. |
| 1125 | ** |
| 1126 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1127 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1128 | ** range', a range of 510 bytes at a well known offset. |
| 1129 | ** |
| 1130 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1131 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1132 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1133 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1134 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1135 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1136 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1137 | ** |
| 1138 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1139 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1140 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1141 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1142 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1143 | ** This property is used by the algorithm for rolling back a journal file |
| 1144 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1145 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1146 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1147 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1148 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1149 | ** within this range, this ensures that no other locks are held on the |
| 1150 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1151 | ** |
| 1152 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1153 | ** range' is that some versions of windows do not support read-locks. By |
| 1154 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1155 | ** even if the locking primitive used is always a write-lock. |
| 1156 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1157 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1158 | unixFile *pFile = (unixFile*)id; |
| 1159 | struct lockInfo *pLock = pFile->pLock; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1160 | struct flock lock; |
| 1161 | int s; |
| 1162 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1163 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1164 | OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1165 | locktypeName(locktype), locktypeName(pFile->locktype), |
| 1166 | locktypeName(pLock->locktype), pLock->cnt , getpid()); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* If there is already a lock of this type or more restrictive on the |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1169 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1170 | ** enterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1171 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1172 | if( pFile->locktype>=locktype ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1173 | OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1174 | locktypeName(locktype)); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1175 | return SQLITE_OK; |
| 1176 | } |
| 1177 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1178 | /* Make sure the locking sequence is correct |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1179 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1180 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1181 | assert( locktype!=PENDING_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1182 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1183 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1184 | /* This mutex is needed because pFile->pLock is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1185 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1186 | enterMutex(); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1187 | |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1188 | /* Make sure the current thread owns the pFile. |
| 1189 | */ |
| 1190 | rc = transferOwnership(pFile); |
| 1191 | if( rc!=SQLITE_OK ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1192 | leaveMutex(); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1193 | return rc; |
| 1194 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1195 | pLock = pFile->pLock; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1196 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1197 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1198 | ** handle that precludes the requested lock, return BUSY. |
| 1199 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1200 | if( (pFile->locktype!=pLock->locktype && |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1201 | (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1202 | ){ |
| 1203 | rc = SQLITE_BUSY; |
| 1204 | goto end_lock; |
| 1205 | } |
| 1206 | |
| 1207 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1208 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1209 | ** return SQLITE_OK. |
| 1210 | */ |
| 1211 | if( locktype==SHARED_LOCK && |
| 1212 | (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){ |
| 1213 | assert( locktype==SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1214 | assert( pFile->locktype==0 ); |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1215 | assert( pLock->cnt>0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1216 | pFile->locktype = SHARED_LOCK; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1217 | pLock->cnt++; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1218 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1219 | goto end_lock; |
| 1220 | } |
| 1221 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1222 | lock.l_len = 1L; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1223 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1224 | lock.l_whence = SEEK_SET; |
| 1225 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1226 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1227 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1228 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1229 | */ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1230 | if( locktype==SHARED_LOCK |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1231 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1232 | ){ |
danielk1977 | 489468c | 2004-06-28 08:25:47 +0000 | [diff] [blame] | 1233 | lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1234 | lock.l_start = PENDING_BYTE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1235 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1236 | if( s==(-1) ){ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1237 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 1238 | goto end_lock; |
| 1239 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | |
| 1243 | /* If control gets to this point, then actually go ahead and make |
| 1244 | ** operating system calls for the specified lock. |
| 1245 | */ |
| 1246 | if( locktype==SHARED_LOCK ){ |
| 1247 | assert( pLock->cnt==0 ); |
| 1248 | assert( pLock->locktype==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1249 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1250 | /* Now get the read-lock */ |
| 1251 | lock.l_start = SHARED_FIRST; |
| 1252 | lock.l_len = SHARED_SIZE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1253 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1254 | |
| 1255 | /* Drop the temporary PENDING lock */ |
| 1256 | lock.l_start = PENDING_BYTE; |
| 1257 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1258 | lock.l_type = F_UNLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1259 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1260 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1261 | goto end_lock; |
| 1262 | } |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1263 | if( s==(-1) ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1264 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 1265 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1266 | pFile->locktype = SHARED_LOCK; |
| 1267 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1268 | pLock->cnt = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1269 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1270 | }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){ |
| 1271 | /* We are trying for an exclusive lock but another thread in this |
| 1272 | ** same process is still holding a shared lock. */ |
| 1273 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1274 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1275 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1276 | ** assumed that there is a SHARED or greater lock on the file |
| 1277 | ** already. |
| 1278 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1279 | assert( 0!=pFile->locktype ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1280 | lock.l_type = F_WRLCK; |
| 1281 | switch( locktype ){ |
| 1282 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1283 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1284 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1285 | case EXCLUSIVE_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1286 | lock.l_start = SHARED_FIRST; |
| 1287 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1288 | break; |
| 1289 | default: |
| 1290 | assert(0); |
| 1291 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1292 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1293 | if( s==(-1) ){ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1294 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 1295 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1296 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1297 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1298 | if( rc==SQLITE_OK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1299 | pFile->locktype = locktype; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1300 | pLock->locktype = locktype; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1301 | }else if( locktype==EXCLUSIVE_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1302 | pFile->locktype = PENDING_LOCK; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1303 | pLock->locktype = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1304 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1305 | |
| 1306 | end_lock: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1307 | leaveMutex(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1308 | OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 1309 | rc==SQLITE_OK ? "ok" : "failed"); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1310 | return rc; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1314 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1315 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1316 | ** |
| 1317 | ** If the locking level of the file descriptor is already at or below |
| 1318 | ** the requested locking level, this routine is a no-op. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1319 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1320 | static int unixUnlock(sqlite3_file *id, int locktype){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1321 | struct lockInfo *pLock; |
| 1322 | struct flock lock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1323 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1324 | unixFile *pFile = (unixFile*)id; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1325 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1326 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1327 | OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1328 | pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid()); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1329 | |
| 1330 | assert( locktype<=SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1331 | if( pFile->locktype<=locktype ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1332 | return SQLITE_OK; |
| 1333 | } |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1334 | if( CHECK_THREADID(pFile) ){ |
| 1335 | return SQLITE_MISUSE; |
| 1336 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1337 | enterMutex(); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1338 | pLock = pFile->pLock; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1339 | assert( pLock->cnt!=0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1340 | if( pFile->locktype>SHARED_LOCK ){ |
| 1341 | assert( pLock->locktype==pFile->locktype ); |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1342 | if( locktype==SHARED_LOCK ){ |
| 1343 | lock.l_type = F_RDLCK; |
| 1344 | lock.l_whence = SEEK_SET; |
| 1345 | lock.l_start = SHARED_FIRST; |
| 1346 | lock.l_len = SHARED_SIZE; |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1347 | if( fcntl(pFile->h, F_SETLK, &lock)==(-1) ){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1348 | /* This should never happen */ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1349 | rc = SQLITE_IOERR_RDLOCK; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1350 | } |
| 1351 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1352 | lock.l_type = F_UNLCK; |
| 1353 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1354 | lock.l_start = PENDING_BYTE; |
| 1355 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1356 | if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1357 | pLock->locktype = SHARED_LOCK; |
| 1358 | }else{ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1359 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1360 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1361 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1362 | if( locktype==NO_LOCK ){ |
| 1363 | struct openCnt *pOpen; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1364 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1365 | /* Decrement the shared lock counter. Release the lock using an |
| 1366 | ** OS call only when all threads in this same process have released |
| 1367 | ** the lock. |
| 1368 | */ |
| 1369 | pLock->cnt--; |
| 1370 | if( pLock->cnt==0 ){ |
| 1371 | lock.l_type = F_UNLCK; |
| 1372 | lock.l_whence = SEEK_SET; |
| 1373 | lock.l_start = lock.l_len = 0L; |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1374 | if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1375 | pLock->locktype = NO_LOCK; |
| 1376 | }else{ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1377 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1378 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1381 | /* Decrement the count of locks against this same file. When the |
| 1382 | ** count reaches zero, close any other file descriptors whose close |
| 1383 | ** was deferred because of outstanding locks. |
| 1384 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1385 | pOpen = pFile->pOpen; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1386 | pOpen->nLock--; |
| 1387 | assert( pOpen->nLock>=0 ); |
| 1388 | if( pOpen->nLock==0 && pOpen->nPending>0 ){ |
| 1389 | int i; |
| 1390 | for(i=0; i<pOpen->nPending; i++){ |
| 1391 | close(pOpen->aPending[i]); |
| 1392 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1393 | free(pOpen->aPending); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1394 | pOpen->nPending = 0; |
| 1395 | pOpen->aPending = 0; |
| 1396 | } |
| 1397 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1398 | leaveMutex(); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1399 | pFile->locktype = locktype; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1400 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1404 | ** Close a file. |
| 1405 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1406 | static int unixClose(sqlite3_file *id){ |
| 1407 | unixFile *pFile = (unixFile *)id; |
| 1408 | if( !pFile ) return SQLITE_OK; |
| 1409 | unixUnlock(id, NO_LOCK); |
| 1410 | if( pFile->dirfd>=0 ) close(pFile->dirfd); |
| 1411 | pFile->dirfd = -1; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1412 | enterMutex(); |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 1413 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1414 | if( pFile->pOpen->nLock ){ |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1415 | /* If there are outstanding locks, do not actually close the file just |
| 1416 | ** yet because that would clear those locks. Instead, add the file |
| 1417 | ** descriptor to pOpen->aPending. It will be automatically closed when |
| 1418 | ** the last lock is cleared. |
| 1419 | */ |
| 1420 | int *aNew; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1421 | struct openCnt *pOpen = pFile->pOpen; |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1422 | aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) ); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1423 | if( aNew==0 ){ |
| 1424 | /* If a malloc fails, just leak the file descriptor */ |
| 1425 | }else{ |
| 1426 | pOpen->aPending = aNew; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1427 | pOpen->aPending[pOpen->nPending] = pFile->h; |
drh | ad81e87 | 2005-08-21 21:45:01 +0000 | [diff] [blame] | 1428 | pOpen->nPending++; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1429 | } |
| 1430 | }else{ |
| 1431 | /* There are no outstanding locks so we can close the file immediately */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1432 | close(pFile->h); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1433 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1434 | releaseLockInfo(pFile->pLock); |
| 1435 | releaseOpenCnt(pFile->pOpen); |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 1436 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1437 | leaveMutex(); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1438 | pFile->isOpen = 0; |
| 1439 | OSTRACE2("CLOSE %-3d\n", pFile->h); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1440 | OpenCounter(-1); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1441 | memset(pFile, 0, sizeof(unixFile)); |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 1442 | return SQLITE_OK; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1445 | |
| 1446 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 1447 | #pragma mark AFP Support |
| 1448 | |
| 1449 | /* |
| 1450 | ** The afpLockingContext structure contains all afp lock specific state |
| 1451 | */ |
| 1452 | typedef struct afpLockingContext afpLockingContext; |
| 1453 | struct afpLockingContext { |
| 1454 | unsigned long long sharedLockByte; |
| 1455 | char *filePath; |
| 1456 | }; |
| 1457 | |
| 1458 | struct ByteRangeLockPB2 |
| 1459 | { |
| 1460 | unsigned long long offset; /* offset to first byte to lock */ |
| 1461 | unsigned long long length; /* nbr of bytes to lock */ |
| 1462 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 1463 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 1464 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 1465 | int fd; /* file desc to assoc this lock with */ |
| 1466 | }; |
| 1467 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 1468 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1469 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1470 | /* |
| 1471 | ** Return 0 on success, 1 on failure. To match the behavior of the |
| 1472 | ** normal posix file locking (used in unixLock for example), we should |
| 1473 | ** provide 'richer' return codes - specifically to differentiate between |
| 1474 | ** 'file busy' and 'file system error' results. |
| 1475 | */ |
| 1476 | static int _AFPFSSetLock( |
| 1477 | const char *path, |
| 1478 | int fd, |
| 1479 | unsigned long long offset, |
| 1480 | unsigned long long length, |
| 1481 | int setLockFlag |
| 1482 | ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 1483 | struct ByteRangeLockPB2 pb; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1484 | int err; |
| 1485 | |
| 1486 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 1487 | pb.startEndFlag = 0; |
| 1488 | pb.offset = offset; |
| 1489 | pb.length = length; |
| 1490 | pb.fd = fd; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1491 | OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n", |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1492 | (setLockFlag?"ON":"OFF"), fd, offset, length); |
| 1493 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 1494 | if ( err==-1 ) { |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1495 | OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1496 | strerror(errno)); |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1497 | return 1; /* error */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1498 | } else { |
| 1499 | return 0; |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1505 | ** file by this or any other process. If such a lock is held, return |
| 1506 | ** non-zero. If the file is unlocked or holds only SHARED locks, then |
| 1507 | ** return zero. |
| 1508 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1509 | static int afpUnixCheckReservedLock(sqlite3_file *id){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1510 | int r = 0; |
| 1511 | unixFile *pFile = (unixFile*)id; |
| 1512 | |
| 1513 | assert( pFile ); |
| 1514 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 1515 | |
| 1516 | /* Check if a thread in this process holds such a lock */ |
| 1517 | if( pFile->locktype>SHARED_LOCK ){ |
| 1518 | r = 1; |
| 1519 | } |
| 1520 | |
| 1521 | /* Otherwise see if some other process holds it. |
| 1522 | */ |
| 1523 | if ( !r ) { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1524 | /* lock the byte */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1525 | int failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1); |
| 1526 | if (failed) { |
| 1527 | /* if we failed to get the lock then someone else must have it */ |
| 1528 | r = 1; |
| 1529 | } else { |
| 1530 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 1531 | ** the original state */ |
| 1532 | _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0); |
| 1533 | } |
| 1534 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1535 | OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1536 | |
| 1537 | return r; |
| 1538 | } |
| 1539 | |
| 1540 | /* AFP-style locking following the behavior of unixLock, see the unixLock |
| 1541 | ** function comments for details of lock management. */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1542 | static int afpUnixLock(sqlite3_file *id, int locktype) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1543 | { |
| 1544 | int rc = SQLITE_OK; |
| 1545 | unixFile *pFile = (unixFile*)id; |
| 1546 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 1547 | int gotPendingLock = 0; |
| 1548 | |
| 1549 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1550 | OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1551 | locktypeName(locktype), locktypeName(pFile->locktype), getpid()); |
| 1552 | /* If there is already a lock of this type or more restrictive on the |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1553 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1554 | ** enterMutex() hasn't been called yet. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1555 | */ |
| 1556 | if( pFile->locktype>=locktype ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1557 | OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1558 | locktypeName(locktype)); |
| 1559 | return SQLITE_OK; |
| 1560 | } |
| 1561 | |
| 1562 | /* Make sure the locking sequence is correct |
| 1563 | */ |
| 1564 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 1565 | assert( locktype!=PENDING_LOCK ); |
| 1566 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
| 1567 | |
| 1568 | /* This mutex is needed because pFile->pLock is shared across threads |
| 1569 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1570 | enterMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* Make sure the current thread owns the pFile. |
| 1573 | */ |
| 1574 | rc = transferOwnership(pFile); |
| 1575 | if( rc!=SQLITE_OK ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1576 | leaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1577 | return rc; |
| 1578 | } |
| 1579 | |
| 1580 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1581 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1582 | ** be released. |
| 1583 | */ |
| 1584 | if( locktype==SHARED_LOCK |
| 1585 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |
| 1586 | ){ |
| 1587 | int failed = _AFPFSSetLock(context->filePath, pFile->h, |
| 1588 | PENDING_BYTE, 1, 1); |
| 1589 | if (failed) { |
| 1590 | rc = SQLITE_BUSY; |
| 1591 | goto afp_end_lock; |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | /* If control gets to this point, then actually go ahead and make |
| 1596 | ** operating system calls for the specified lock. |
| 1597 | */ |
| 1598 | if( locktype==SHARED_LOCK ){ |
| 1599 | int lk, failed; |
| 1600 | int tries = 0; |
| 1601 | |
| 1602 | /* Now get the read-lock */ |
| 1603 | /* note that the quality of the randomness doesn't matter that much */ |
| 1604 | lk = random(); |
| 1605 | context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); |
| 1606 | failed = _AFPFSSetLock(context->filePath, pFile->h, |
| 1607 | SHARED_FIRST+context->sharedLockByte, 1, 1); |
| 1608 | |
| 1609 | /* Drop the temporary PENDING lock */ |
| 1610 | if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)) { |
| 1611 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
| 1612 | goto afp_end_lock; |
| 1613 | } |
| 1614 | |
| 1615 | if( failed ){ |
| 1616 | rc = SQLITE_BUSY; |
| 1617 | } else { |
| 1618 | pFile->locktype = SHARED_LOCK; |
| 1619 | } |
| 1620 | }else{ |
| 1621 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 1622 | ** assumed that there is a SHARED or greater lock on the file |
| 1623 | ** already. |
| 1624 | */ |
| 1625 | int failed = 0; |
| 1626 | assert( 0!=pFile->locktype ); |
| 1627 | if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) { |
| 1628 | /* Acquire a RESERVED lock */ |
| 1629 | failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1); |
| 1630 | } |
| 1631 | if (!failed && locktype == EXCLUSIVE_LOCK) { |
| 1632 | /* Acquire an EXCLUSIVE lock */ |
| 1633 | |
| 1634 | /* Remove the shared lock before trying the range. we'll need to |
| 1635 | ** reestablish the shared lock if we can't get the afpUnixUnlock |
| 1636 | */ |
| 1637 | if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST + |
| 1638 | context->sharedLockByte, 1, 0)) { |
| 1639 | /* now attemmpt to get the exclusive lock range */ |
| 1640 | failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST, |
| 1641 | SHARED_SIZE, 1); |
| 1642 | if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST + |
| 1643 | context->sharedLockByte, 1, 1)) { |
| 1644 | rc = SQLITE_IOERR_RDLOCK; /* this should never happen */ |
| 1645 | } |
| 1646 | } else { |
| 1647 | /* */ |
| 1648 | rc = SQLITE_IOERR_UNLOCK; /* this should never happen */ |
| 1649 | } |
| 1650 | } |
| 1651 | if( failed && rc == SQLITE_OK){ |
| 1652 | rc = SQLITE_BUSY; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | if( rc==SQLITE_OK ){ |
| 1657 | pFile->locktype = locktype; |
| 1658 | }else if( locktype==EXCLUSIVE_LOCK ){ |
| 1659 | pFile->locktype = PENDING_LOCK; |
| 1660 | } |
| 1661 | |
| 1662 | afp_end_lock: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1663 | leaveMutex(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1664 | OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1665 | rc==SQLITE_OK ? "ok" : "failed"); |
| 1666 | return rc; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 1671 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1672 | ** |
| 1673 | ** If the locking level of the file descriptor is already at or below |
| 1674 | ** the requested locking level, this routine is a no-op. |
| 1675 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1676 | static int afpUnixUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1677 | struct flock lock; |
| 1678 | int rc = SQLITE_OK; |
| 1679 | unixFile *pFile = (unixFile*)id; |
| 1680 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 1681 | |
| 1682 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1683 | OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1684 | pFile->locktype, getpid()); |
| 1685 | |
| 1686 | assert( locktype<=SHARED_LOCK ); |
| 1687 | if( pFile->locktype<=locktype ){ |
| 1688 | return SQLITE_OK; |
| 1689 | } |
| 1690 | if( CHECK_THREADID(pFile) ){ |
| 1691 | return SQLITE_MISUSE; |
| 1692 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1693 | enterMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1694 | if( pFile->locktype>SHARED_LOCK ){ |
| 1695 | if( locktype==SHARED_LOCK ){ |
| 1696 | int failed = 0; |
| 1697 | |
| 1698 | /* unlock the exclusive range - then re-establish the shared lock */ |
| 1699 | if (pFile->locktype==EXCLUSIVE_LOCK) { |
| 1700 | failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST, |
| 1701 | SHARED_SIZE, 0); |
| 1702 | if (!failed) { |
| 1703 | /* successfully removed the exclusive lock */ |
| 1704 | if (_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST+ |
| 1705 | context->sharedLockByte, 1, 1)) { |
| 1706 | /* failed to re-establish our shared lock */ |
| 1707 | rc = SQLITE_IOERR_RDLOCK; /* This should never happen */ |
| 1708 | } |
| 1709 | } else { |
| 1710 | /* This should never happen - failed to unlock the exclusive range */ |
| 1711 | rc = SQLITE_IOERR_UNLOCK; |
| 1712 | } |
| 1713 | } |
| 1714 | } |
| 1715 | if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) { |
| 1716 | if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)){ |
| 1717 | /* failed to release the pending lock */ |
| 1718 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
| 1719 | } |
| 1720 | } |
| 1721 | if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) { |
| 1722 | if (_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0)) { |
| 1723 | /* failed to release the reserved lock */ |
| 1724 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
| 1725 | } |
| 1726 | } |
| 1727 | } |
| 1728 | if( locktype==NO_LOCK ){ |
| 1729 | int failed = _AFPFSSetLock(context->filePath, pFile->h, |
| 1730 | SHARED_FIRST + context->sharedLockByte, 1, 0); |
| 1731 | if (failed) { |
| 1732 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
| 1733 | } |
| 1734 | } |
| 1735 | if (rc == SQLITE_OK) |
| 1736 | pFile->locktype = locktype; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1737 | leaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1738 | return rc; |
| 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | ** Close a file & cleanup AFP specific locking context |
| 1743 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1744 | static int afpUnixClose(sqlite3_file *id) { |
| 1745 | unixFile *pFile = (unixFile*)pId; |
| 1746 | |
| 1747 | if( !pFile ) return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1748 | afpUnixUnlock(*pId, NO_LOCK); |
| 1749 | /* free the AFP locking structure */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1750 | if (pFile->lockingContext != NULL) { |
| 1751 | if (((afpLockingContext *)pFile->lockingContext)->filePath != NULL) |
| 1752 | sqlite3_free(((afpLockingContext*)pFile->lockingContext)->filePath); |
| 1753 | sqlite3_free(pFile->lockingContext); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1754 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1755 | |
| 1756 | if( pFile->dirfd>=0 ) close(pFile->dirfd); |
| 1757 | pFile->dirfd = -1; |
| 1758 | close(pFile->h); |
| 1759 | pFile->isOpen = 0; |
| 1760 | OSTRACE2("CLOSE %-3d\n", pFile->h); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1761 | OpenCounter(-1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1762 | return SQLITE_OK; |
| 1763 | } |
| 1764 | |
| 1765 | |
| 1766 | #pragma mark flock() style locking |
| 1767 | |
| 1768 | /* |
| 1769 | ** The flockLockingContext is not used |
| 1770 | */ |
| 1771 | typedef void flockLockingContext; |
| 1772 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1773 | static int flockUnixCheckReservedLock(sqlite3_file *id) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1774 | unixFile *pFile = (unixFile*)id; |
| 1775 | |
| 1776 | if (pFile->locktype == RESERVED_LOCK) { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1777 | return 1; /* already have a reserved lock */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1778 | } else { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1779 | /* attempt to get the lock */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1780 | int rc = flock(pFile->h, LOCK_EX | LOCK_NB); |
| 1781 | if (!rc) { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1782 | /* got the lock, unlock it */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1783 | flock(pFile->h, LOCK_UN); |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1784 | return 0; /* no one has it reserved */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1785 | } |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1786 | return 1; /* someone else might have it reserved */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1790 | static int flockUnixLock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1791 | unixFile *pFile = (unixFile*)id; |
| 1792 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1793 | /* if we already have a lock, it is exclusive. |
| 1794 | ** Just adjust level and punt on outta here. */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1795 | if (pFile->locktype > NO_LOCK) { |
| 1796 | pFile->locktype = locktype; |
| 1797 | return SQLITE_OK; |
| 1798 | } |
| 1799 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1800 | /* grab an exclusive lock */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1801 | int rc = flock(pFile->h, LOCK_EX | LOCK_NB); |
| 1802 | if (rc) { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1803 | /* didn't get, must be busy */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1804 | return SQLITE_BUSY; |
| 1805 | } else { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1806 | /* got it, set the type and return ok */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1807 | pFile->locktype = locktype; |
| 1808 | return SQLITE_OK; |
| 1809 | } |
| 1810 | } |
| 1811 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1812 | static int flockUnixUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1813 | unixFile *pFile = (unixFile*)id; |
| 1814 | |
| 1815 | assert( locktype<=SHARED_LOCK ); |
| 1816 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1817 | /* no-op if possible */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1818 | if( pFile->locktype==locktype ){ |
| 1819 | return SQLITE_OK; |
| 1820 | } |
| 1821 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1822 | /* shared can just be set because we always have an exclusive */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1823 | if (locktype==SHARED_LOCK) { |
| 1824 | pFile->locktype = locktype; |
| 1825 | return SQLITE_OK; |
| 1826 | } |
| 1827 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1828 | /* no, really, unlock. */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1829 | int rc = flock(pFile->h, LOCK_UN); |
| 1830 | if (rc) |
| 1831 | return SQLITE_IOERR_UNLOCK; |
| 1832 | else { |
| 1833 | pFile->locktype = NO_LOCK; |
| 1834 | return SQLITE_OK; |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | /* |
| 1839 | ** Close a file. |
| 1840 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1841 | static int flockUnixClose(sqlite3_file *pId) { |
| 1842 | unixFile *pFile = (unixFile*)*pId; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1843 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1844 | if( !pFile ) return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1845 | flockUnixUnlock(*pId, NO_LOCK); |
| 1846 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1847 | if( pFile->dirfd>=0 ) close(pFile->dirfd); |
| 1848 | pFile->dirfd = -1; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1849 | enterMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1850 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1851 | close(pFile->h); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1852 | leaveMutex(); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1853 | pFile->isOpen = 0; |
| 1854 | OSTRACE2("CLOSE %-3d\n", pFile->h); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1855 | OpenCounter(-1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1856 | return SQLITE_OK; |
| 1857 | } |
| 1858 | |
| 1859 | #pragma mark Old-School .lock file based locking |
| 1860 | |
| 1861 | /* |
| 1862 | ** The dotlockLockingContext structure contains all dotlock (.lock) lock |
| 1863 | ** specific state |
| 1864 | */ |
| 1865 | typedef struct dotlockLockingContext dotlockLockingContext; |
| 1866 | struct dotlockLockingContext { |
| 1867 | char *lockPath; |
| 1868 | }; |
| 1869 | |
| 1870 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1871 | static int dotlockUnixCheckReservedLock(sqlite3_file *id) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1872 | unixFile *pFile = (unixFile*)id; |
| 1873 | dotlockLockingContext *context = |
| 1874 | (dotlockLockingContext *) pFile->lockingContext; |
| 1875 | |
| 1876 | if (pFile->locktype == RESERVED_LOCK) { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1877 | return 1; /* already have a reserved lock */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1878 | } else { |
| 1879 | struct stat statBuf; |
| 1880 | if (lstat(context->lockPath,&statBuf) == 0) |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1881 | /* file exists, someone else has the lock */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1882 | return 1; |
| 1883 | else |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1884 | /* file does not exist, we could have it if we want it */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1885 | return 0; |
| 1886 | } |
| 1887 | } |
| 1888 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1889 | static int dotlockUnixLock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1890 | unixFile *pFile = (unixFile*)id; |
| 1891 | dotlockLockingContext *context = |
| 1892 | (dotlockLockingContext *) pFile->lockingContext; |
| 1893 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1894 | /* if we already have a lock, it is exclusive. |
| 1895 | ** Just adjust level and punt on outta here. */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1896 | if (pFile->locktype > NO_LOCK) { |
| 1897 | pFile->locktype = locktype; |
| 1898 | |
| 1899 | /* Always update the timestamp on the old file */ |
| 1900 | utimes(context->lockPath,NULL); |
| 1901 | return SQLITE_OK; |
| 1902 | } |
| 1903 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1904 | /* check to see if lock file already exists */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1905 | struct stat statBuf; |
| 1906 | if (lstat(context->lockPath,&statBuf) == 0){ |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1907 | return SQLITE_BUSY; /* it does, busy */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1910 | /* grab an exclusive lock */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1911 | int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600); |
| 1912 | if (fd < 0) { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1913 | /* failed to open/create the file, someone else may have stolen the lock */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1914 | return SQLITE_BUSY; |
| 1915 | } |
| 1916 | close(fd); |
| 1917 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1918 | /* got it, set the type and return ok */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1919 | pFile->locktype = locktype; |
| 1920 | return SQLITE_OK; |
| 1921 | } |
| 1922 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1923 | static int dotlockUnixUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1924 | unixFile *pFile = (unixFile*)id; |
| 1925 | dotlockLockingContext *context = |
| 1926 | (dotlockLockingContext *) pFile->lockingContext; |
| 1927 | |
| 1928 | assert( locktype<=SHARED_LOCK ); |
| 1929 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1930 | /* no-op if possible */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1931 | if( pFile->locktype==locktype ){ |
| 1932 | return SQLITE_OK; |
| 1933 | } |
| 1934 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1935 | /* shared can just be set because we always have an exclusive */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1936 | if (locktype==SHARED_LOCK) { |
| 1937 | pFile->locktype = locktype; |
| 1938 | return SQLITE_OK; |
| 1939 | } |
| 1940 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 1941 | /* no, really, unlock. */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1942 | unlink(context->lockPath); |
| 1943 | pFile->locktype = NO_LOCK; |
| 1944 | return SQLITE_OK; |
| 1945 | } |
| 1946 | |
| 1947 | /* |
| 1948 | ** Close a file. |
| 1949 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1950 | static int dotlockUnixClose(sqlite3_file *id) { |
| 1951 | unixFile *pFile = (unixFile*)id; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1952 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1953 | if( !pFile ) return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1954 | dotlockUnixUnlock(*pId, NO_LOCK); |
| 1955 | /* free the dotlock locking structure */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1956 | if (pFile->lockingContext != NULL) { |
| 1957 | if (((dotlockLockingContext *)pFile->lockingContext)->lockPath != NULL) |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1958 | sqlite3_free( ( (dotlockLockingContext *) |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1959 | pFile->lockingContext)->lockPath); |
| 1960 | sqlite3_free(pFile->lockingContext); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1963 | if( pFile->dirfd>=0 ) close(pFile->dirfd); |
| 1964 | pFile->dirfd = -1; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1965 | enterMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1966 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1967 | close(pFile->h); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1968 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1969 | leaveMutex(); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1970 | pFile->isOpen = 0; |
| 1971 | OSTRACE2("CLOSE %-3d\n", pFile->h); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1972 | OpenCounter(-1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1973 | return SQLITE_OK; |
| 1974 | } |
| 1975 | |
| 1976 | |
| 1977 | #pragma mark No locking |
| 1978 | |
| 1979 | /* |
| 1980 | ** The nolockLockingContext is void |
| 1981 | */ |
| 1982 | typedef void nolockLockingContext; |
| 1983 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1984 | static int nolockUnixCheckReservedLock(sqlite3_file *id) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1985 | return 0; |
| 1986 | } |
| 1987 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1988 | static int nolockUnixLock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1989 | return SQLITE_OK; |
| 1990 | } |
| 1991 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1992 | static int nolockUnixUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1993 | return SQLITE_OK; |
| 1994 | } |
| 1995 | |
| 1996 | /* |
| 1997 | ** Close a file. |
| 1998 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1999 | static int nolockUnixClose(sqlite3_file *id) { |
| 2000 | unixFile *pFile = (unixFile*)id; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2001 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2002 | if( !pFile ) return SQLITE_OK; |
| 2003 | if( pFile->dirfd>=0 ) close(pFile->dirfd); |
| 2004 | pFile->dirfd = -1; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2005 | enterMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2006 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2007 | close(pFile->h); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2008 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2009 | leaveMutex(); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2010 | pFile->isOpen = 0; |
| 2011 | OSTRACE2("CLOSE %-3d\n", pFile->h); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2012 | OpenCounter(-1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2013 | return SQLITE_OK; |
| 2014 | } |
| 2015 | |
| 2016 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 2017 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2018 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2019 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2020 | ** TODO: xBreakLock() for this vfs. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 2021 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2022 | static int unixBreakLock(sqlite3_file *id){ |
| 2023 | assert(!"TODO: unixBreakLock()"); |
| 2024 | return 0; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2028 | ** Return an integer that indices the type of lock currently held |
| 2029 | ** by this handle. (Used for testing and analysis only.) |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 2030 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2031 | static int unixLockState(sqlite3_file *id){ |
| 2032 | return ((unixFile*)id)->locktype; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2035 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 2036 | ** Return the sector size in bytes of the underlying block device for |
| 2037 | ** the specified file. This is almost always 512 bytes, but may be |
| 2038 | ** larger for some devices. |
| 2039 | ** |
| 2040 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 2041 | ** if two files are created in the same file-system directory (i.e. |
| 2042 | ** a database and it's journal file) that the sector size will be the |
| 2043 | ** same for both. |
| 2044 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2045 | static int unixSectorSize(sqlite3_file *id){ |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 2046 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2049 | /* |
| 2050 | ** Return the device characteristics for the file. This is always 0. |
| 2051 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2052 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 2056 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2057 | ** This vector defines all the methods that can operate on an sqlite3_file |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2058 | ** for unix. |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2059 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2060 | static const sqlite3_io_methods sqlite3UnixIoMethod = { |
| 2061 | 1, /* iVersion */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2062 | unixClose, |
| 2063 | unixRead, |
| 2064 | unixWrite, |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2065 | unixTruncate, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2066 | unixSync, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2067 | unixFileSize, |
| 2068 | unixLock, |
| 2069 | unixUnlock, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2070 | unixCheckReservedLock, |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2071 | unixBreakLock, |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2072 | unixLockState, |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 2073 | unixSectorSize, |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2074 | unixDeviceCharacteristics |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2075 | }; |
| 2076 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2077 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2078 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2079 | ** This vector defines all the methods that can operate on an sqlite3_file |
| 2080 | ** for unix with AFP style file locking. |
| 2081 | */ |
| 2082 | static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = { |
| 2083 | 1, /* iVersion */ |
| 2084 | unixClose, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2085 | unixRead, |
| 2086 | unixWrite, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2087 | unixTruncate, |
| 2088 | unixSync, |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2089 | unixFileSize, |
| 2090 | afpUnixLock, |
| 2091 | afpUnixUnlock, |
| 2092 | afpUnixCheckReservedLock, |
| 2093 | unixBreakLock, |
| 2094 | unixLockState, |
| 2095 | unixSectorSize, |
| 2096 | unixDeviceCharacteristics |
| 2097 | }; |
| 2098 | |
| 2099 | /* |
| 2100 | ** This vector defines all the methods that can operate on an sqlite3_file |
| 2101 | ** for unix with flock() style file locking. |
| 2102 | */ |
| 2103 | static const sqlite3_io_methods sqlite3FlockLockingUnixIoMethod = { |
| 2104 | 1, /* iVersion */ |
| 2105 | flockUnixClose, |
| 2106 | unixRead, |
| 2107 | unixWrite, |
| 2108 | unixTruncate, |
| 2109 | unixSync, |
| 2110 | unixFileSize, |
| 2111 | flockUnixLock, |
| 2112 | flockUnixUnlock, |
| 2113 | flockUnixCheckReservedLock, |
| 2114 | unixBreakLock, |
| 2115 | unixLockState, |
| 2116 | unixSectorSize, |
| 2117 | unixDeviceCharacteristics |
| 2118 | }; |
| 2119 | |
| 2120 | /* |
| 2121 | ** This vector defines all the methods that can operate on an sqlite3_file |
| 2122 | ** for unix with dotlock style file locking. |
| 2123 | */ |
| 2124 | static const sqlite3_io_methods sqlite3DotlockLockingUnixIoMethod = { |
| 2125 | 1, /* iVersion */ |
| 2126 | dotlockUnixClose, |
| 2127 | unixRead, |
| 2128 | unixWrite, |
| 2129 | unixTruncate, |
| 2130 | unixSync, |
| 2131 | unixFileSize, |
| 2132 | dotlockUnixLock, |
| 2133 | dotlockUnixUnlock, |
| 2134 | dotlockUnixCheckReservedLock, |
| 2135 | unixBreakLock, |
| 2136 | unixLockState, |
| 2137 | unixSectorSize, |
| 2138 | unixDeviceCharacteristics |
| 2139 | }; |
| 2140 | |
| 2141 | /* |
| 2142 | ** This vector defines all the methods that can operate on an sqlite3_file |
| 2143 | ** for unix with dotlock style file locking. |
| 2144 | */ |
| 2145 | static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = { |
| 2146 | 1, /* iVersion */ |
| 2147 | nolockUnixClose, |
| 2148 | unixRead, |
| 2149 | unixWrite, |
| 2150 | unixTruncate, |
| 2151 | unixSync, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2152 | unixFileSize, |
| 2153 | nolockUnixLock, |
| 2154 | nolockUnixUnlock, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2155 | nolockUnixCheckReservedLock, |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2156 | unixBreakLock, |
| 2157 | unixLockState, |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 2158 | unixSectorSize, |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2159 | unixDeviceCharacteristics |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2160 | }; |
| 2161 | |
| 2162 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 2163 | |
| 2164 | /* |
| 2165 | ** Allocate memory for a new unixFile and initialize that unixFile. |
| 2166 | ** Write a pointer to the new unixFile into *pId. |
| 2167 | ** If we run out of memory, close the file and return an error. |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2168 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2169 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 2170 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2171 | ** When locking extensions are enabled, the filepath and locking style |
| 2172 | ** are needed to determine the unixFile pMethod to use for locking operations. |
| 2173 | ** The locking-style specific lockingContext data structure is created |
| 2174 | ** and assigned here also. |
| 2175 | */ |
| 2176 | static int fillInUnixFile( |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2177 | int h, /* Open file descriptor of file being opened */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2178 | int dirfd, /* Directory file descriptor */ |
| 2179 | sqlite3_file *pId, /* Write completed initialization here */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2180 | const char *zFilename, /* Name of the file being opened */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2181 | ){ |
aswift | 108bc32 | 2006-10-11 17:19:46 +0000 | [diff] [blame] | 2182 | sqlite3LockingStyle lockingStyle; |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2183 | unixFile *pNew = (unixFile *)pId; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2184 | int rc; |
| 2185 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2186 | memset(pNew, 0, sizeof(unixFile)); |
aswift | 448aa6f | 2006-11-11 01:31:58 +0000 | [diff] [blame] | 2187 | lockingStyle = sqlite3DetectLockingStyle(zFilename, h); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2188 | if ( lockingStyle == posixLockingStyle ) { |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2189 | enterMutex(); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2190 | rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2191 | leaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2192 | if( rc ){ |
| 2193 | close(h); |
| 2194 | unlink(zFilename); |
| 2195 | return SQLITE_NOMEM; |
| 2196 | } |
| 2197 | } else { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2198 | /* pLock and pOpen are only used for posix advisory locking */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2199 | pNew->pLock = NULL; |
| 2200 | pNew->pOpen = NULL; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2201 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2202 | pNew->dirfd = -1; |
| 2203 | pNew->h = h; |
| 2204 | SET_THREADID(pNew); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2205 | pNew = sqlite3_malloc( sizeof(unixFile) ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2206 | if( pNew==0 ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2207 | close(h); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2208 | enterMutex(); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2209 | releaseLockInfo(pNew->pLock); |
| 2210 | releaseOpenCnt(pNew->pOpen); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2211 | leaveMutex(); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2212 | return SQLITE_NOMEM; |
| 2213 | }else{ |
aswift | 108bc32 | 2006-10-11 17:19:46 +0000 | [diff] [blame] | 2214 | switch(lockingStyle) { |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2215 | case afpLockingStyle: { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2216 | /* afp locking uses the file path so it needs to be included in |
| 2217 | ** the afpLockingContext */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2218 | int nFilename; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2219 | pNew->pMethod = &sqlite3AFPLockingUnixIoMethod; |
| 2220 | pNew->lockingContext = |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2221 | sqlite3_malloc(sizeof(afpLockingContext)); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2222 | nFilename = strlen(zFilename)+1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2223 | ((afpLockingContext *)pNew->lockingContext)->filePath = |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2224 | sqlite3_malloc(nFilename); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2225 | memcpy(((afpLockingContext *)pNew->lockingContext)->filePath, |
| 2226 | zFilename, nFilename); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2227 | srandomdev(); |
| 2228 | break; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2229 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2230 | case flockLockingStyle: |
| 2231 | /* flock locking doesn't need additional lockingContext information */ |
| 2232 | pNew->pMethod = &sqlite3FlockLockingUnixIoMethod; |
| 2233 | break; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2234 | case dotlockLockingStyle: { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2235 | /* dotlock locking uses the file path so it needs to be included in |
| 2236 | ** the dotlockLockingContext */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2237 | int nFilename; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2238 | pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2239 | pNew->lockingContext = sqlite3_malloc( |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2240 | sizeof(dotlockLockingContext)); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2241 | nFilename = strlen(zFilename) + 6; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2242 | ((dotlockLockingContext *)pNew->lockingContext)->lockPath = |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2243 | sqlite3_malloc( nFilename ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2244 | sqlite3_snprintf(nFilename, |
| 2245 | ((dotlockLockingContext *)pNew->lockingContext)->lockPath, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2246 | "%s.lock", zFilename); |
| 2247 | break; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2248 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2249 | case posixLockingStyle: |
| 2250 | /* posix locking doesn't need additional lockingContext information */ |
| 2251 | pNew->pMethod = &sqlite3UnixIoMethod; |
| 2252 | break; |
| 2253 | case noLockingStyle: |
| 2254 | case unsupportedLockingStyle: |
| 2255 | default: |
| 2256 | pNew->pMethod = &sqlite3NolockLockingUnixIoMethod; |
| 2257 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2258 | OpenCounter(+1); |
| 2259 | return SQLITE_OK; |
| 2260 | } |
| 2261 | } |
| 2262 | #else /* SQLITE_ENABLE_LOCKING_STYLE */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2263 | static int fillInUnixFile( |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2264 | int h, /* Open file descriptor on file being opened */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2265 | int dirfd, |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2266 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
| 2267 | const char *zFilename /* Name of the file being opened */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2268 | ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2269 | unixFile *pNew = (unixFile *)pId; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2270 | int rc; |
| 2271 | |
drh | e78669b | 2007-06-29 12:04:26 +0000 | [diff] [blame] | 2272 | #ifdef FD_CLOEXEC |
| 2273 | fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC); |
| 2274 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2275 | |
| 2276 | enterMutex(); |
| 2277 | rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen); |
| 2278 | leaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2279 | if( rc ){ |
| 2280 | close(h); |
| 2281 | return SQLITE_NOMEM; |
| 2282 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2283 | |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2284 | OSTRACE3("OPEN %-3d %s\n", h, zFilename); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2285 | pNew->dirfd = -1; |
| 2286 | pNew->h = h; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2287 | pNew->dirfd = dirfd; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2288 | SET_THREADID(pNew); |
| 2289 | |
| 2290 | pNew->pMethod = &sqlite3UnixIoMethod; |
| 2291 | OpenCounter(+1); |
| 2292 | return SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2293 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2294 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2295 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2296 | /* |
| 2297 | ** Open a file descriptor to the directory containing file zFilename. |
| 2298 | ** If successful, *pFd is set to the opened file descriptor and |
| 2299 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 2300 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 2301 | ** value. |
| 2302 | ** |
| 2303 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 2304 | ** the file descriptor *pFd using close(). |
| 2305 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2306 | static int openDirectory(const char *zFilename, int *pFd){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2307 | int ii; |
| 2308 | int fd; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2309 | char zDirname[MAX_PATHNAME+1]; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2310 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2311 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2312 | for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--); |
| 2313 | if( ii>0 ){ |
| 2314 | zDirname[ii] = '\0'; |
| 2315 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
| 2316 | if( fd>0 ){ |
| 2317 | #ifdef FD_CLOEXEC |
| 2318 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 2319 | #endif |
| 2320 | OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname); |
| 2321 | } |
| 2322 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2323 | *pFd = fd; |
| 2324 | return (fd>0?SQLITE_OK:SQLITE_CANTOPEN); |
| 2325 | } |
| 2326 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2327 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2328 | ** Open the file zPath. |
| 2329 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2330 | ** Previously, the SQLite OS layer used three functions in place of this |
| 2331 | ** one: |
| 2332 | ** |
| 2333 | ** sqlite3OsOpenReadWrite(); |
| 2334 | ** sqlite3OsOpenReadOnly(); |
| 2335 | ** sqlite3OsOpenExclusive(); |
| 2336 | ** |
| 2337 | ** These calls correspond to the following combinations of flags: |
| 2338 | ** |
| 2339 | ** ReadWrite() -> (READWRITE | CREATE) |
| 2340 | ** ReadOnly() -> (READONLY) |
| 2341 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 2342 | ** |
| 2343 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 2344 | ** true, the file was configured to be automatically deleted when the |
| 2345 | ** file handle closed. To achieve the same effect using this new |
| 2346 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 2347 | ** OpenExclusive(). |
| 2348 | */ |
| 2349 | static int unixOpen( |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2350 | sqlite3_vfs *pVfs, |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2351 | const char *zPath, |
| 2352 | sqlite3_file *pFile, |
| 2353 | int flags, |
| 2354 | int *pOutFlags |
| 2355 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2356 | int fd = 0; /* File descriptor returned by open() */ |
| 2357 | int dirfd = -1; /* Directory file descriptor */ |
| 2358 | int oflags = 0; /* Flags to pass to open() */ |
| 2359 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2360 | |
| 2361 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 2362 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 2363 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 2364 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 2365 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
| 2366 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2367 | /* If creating a master or main-file journal, this function will open |
| 2368 | ** a file-descriptor on the directory too. The first time unixSync() |
| 2369 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 2370 | */ |
| 2371 | int isOpenDirectory = (isCreate && |
| 2372 | (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL) |
| 2373 | ); |
| 2374 | |
| 2375 | /* Check the following statements are true: |
| 2376 | ** |
| 2377 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 2378 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 2379 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
| 2380 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2381 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2382 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2383 | assert(isExclusive==0 || isCreate); |
| 2384 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2385 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 2386 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 2387 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 2388 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
| 2389 | ); |
| 2390 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2391 | if( isReadonly ) oflags |= O_RDONLY; |
| 2392 | if( isReadWrite ) oflags |= O_RDWR; |
| 2393 | if( isCreate ) oflags |= O_CREAT; |
| 2394 | if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW); |
| 2395 | oflags |= (O_LARGEFILE|O_BINARY); |
| 2396 | |
| 2397 | memset(pFile, 0, sizeof(unixFile)); |
| 2398 | fd = open(zPath, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 2399 | if( fd<0 && isReadWrite && !isExclusive ){ |
| 2400 | /* Failed to open the file for read/write access. Try read-only. */ |
| 2401 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
| 2402 | flags |= SQLITE_OPEN_READONLY; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2403 | return unixOpen(pVfs, zPath, pFile, flags, pOutFlags); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2404 | } |
| 2405 | if( fd<0 ){ |
| 2406 | return SQLITE_CANTOPEN; |
| 2407 | } |
| 2408 | if( isDelete ){ |
| 2409 | unlink(zPath); |
| 2410 | } |
| 2411 | if( pOutFlags ){ |
| 2412 | *pOutFlags = flags; |
| 2413 | } |
| 2414 | |
| 2415 | assert(fd!=0); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2416 | if( isOpenDirectory ){ |
| 2417 | int rc = openDirectory(zPath, &dirfd); |
| 2418 | if( rc!=SQLITE_OK ){ |
| 2419 | close(fd); |
| 2420 | return rc; |
| 2421 | } |
| 2422 | } |
| 2423 | return fillInUnixFile(fd, dirfd, pFile, zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2427 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 2428 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2429 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2430 | static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2431 | int rc = SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2432 | SimulateIOError(return SQLITE_IOERR_DELETE); |
| 2433 | unlink(zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2434 | if( dirSync ){ |
| 2435 | int fd; |
| 2436 | rc = openDirectory(zPath, &fd); |
| 2437 | if( rc==SQLITE_OK ){ |
| 2438 | if( fsync(fd) ){ |
| 2439 | rc = SQLITE_IOERR_DIR_FSYNC; |
| 2440 | } |
| 2441 | close(fd); |
| 2442 | } |
| 2443 | } |
| 2444 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2447 | /* |
| 2448 | ** Test the existance of or access permissions of file zPath. The |
| 2449 | ** test performed depends on the value of flags: |
| 2450 | ** |
| 2451 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 2452 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 2453 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 2454 | ** |
| 2455 | ** Otherwise return 0. |
| 2456 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2457 | static int unixAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2458 | int amode; |
| 2459 | switch( flags ){ |
| 2460 | case SQLITE_ACCESS_EXISTS: |
| 2461 | amode = F_OK; |
| 2462 | break; |
| 2463 | case SQLITE_ACCESS_READWRITE: |
| 2464 | amode = W_OK|R_OK; |
| 2465 | break; |
| 2466 | case SQLITE_ACCESS_READONLY: |
| 2467 | amode = R_OK; |
| 2468 | break; |
| 2469 | |
| 2470 | default: |
| 2471 | assert(!"Invalid flags argument"); |
| 2472 | } |
| 2473 | return (access(zPath, amode)==0); |
| 2474 | } |
| 2475 | |
| 2476 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2477 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 2478 | ** by the calling process and must be big enough to hold at least |
| 2479 | ** pVfs->mxPathname bytes. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2480 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2481 | static int unixGetTempName(sqlite3_vfs *pVfs, char *zBuf){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2482 | static const char *azDirs[] = { |
| 2483 | 0, |
| 2484 | "/var/tmp", |
| 2485 | "/usr/tmp", |
| 2486 | "/tmp", |
| 2487 | ".", |
| 2488 | }; |
| 2489 | static const unsigned char zChars[] = |
| 2490 | "abcdefghijklmnopqrstuvwxyz" |
| 2491 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 2492 | "0123456789"; |
| 2493 | int i, j; |
| 2494 | struct stat buf; |
| 2495 | const char *zDir = "."; |
| 2496 | azDirs[0] = sqlite3_temp_directory; |
| 2497 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
| 2498 | if( azDirs[i]==0 ) continue; |
| 2499 | if( stat(azDirs[i], &buf) ) continue; |
| 2500 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 2501 | if( access(azDirs[i], 07) ) continue; |
| 2502 | zDir = azDirs[i]; |
| 2503 | break; |
| 2504 | } |
| 2505 | do{ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2506 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
| 2507 | sqlite3_snprintf(MAX_PATHNAME-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2508 | j = strlen(zBuf); |
| 2509 | sqlite3Randomness(15, &zBuf[j]); |
| 2510 | for(i=0; i<15; i++, j++){ |
| 2511 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 2512 | } |
| 2513 | zBuf[j] = 0; |
| 2514 | }while( access(zBuf,0)==0 ); |
| 2515 | return SQLITE_OK; |
| 2516 | } |
| 2517 | |
| 2518 | |
| 2519 | /* |
| 2520 | ** Turn a relative pathname into a full pathname. The relative path |
| 2521 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 2522 | ** zPath. |
| 2523 | ** |
| 2524 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 2525 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 2526 | ** this buffer before returning. |
| 2527 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2528 | static int unixFullPathname(sqlite3_vfs *pVfs, const char *zPath, char *zOut){ |
| 2529 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2530 | zOut[MAX_PATHNAME-1] = '\0'; |
| 2531 | if( zPath[0]=='/' ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2532 | sqlite3_snprintf(MAX_PATHNAME, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2533 | }else{ |
| 2534 | int nCwd; |
| 2535 | if( getcwd(zOut, MAX_PATHNAME-1)==0 ){ |
| 2536 | return SQLITE_ERROR; |
| 2537 | } |
| 2538 | nCwd = strlen(zOut); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2539 | sqlite3_snprintf(MAX_PATHNAME-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2540 | } |
| 2541 | return SQLITE_OK; |
| 2542 | |
| 2543 | #if 0 |
| 2544 | /* |
| 2545 | ** Remove "/./" path elements and convert "/A/./" path elements |
| 2546 | ** to just "/". |
| 2547 | */ |
| 2548 | if( zFull ){ |
| 2549 | int i, j; |
| 2550 | for(i=j=0; zFull[i]; i++){ |
| 2551 | if( zFull[i]=='/' ){ |
| 2552 | if( zFull[i+1]=='/' ) continue; |
| 2553 | if( zFull[i+1]=='.' && zFull[i+2]=='/' ){ |
| 2554 | i += 1; |
| 2555 | continue; |
| 2556 | } |
| 2557 | if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){ |
| 2558 | while( j>0 && zFull[j-1]!='/' ){ j--; } |
| 2559 | i += 3; |
| 2560 | continue; |
| 2561 | } |
| 2562 | } |
| 2563 | zFull[j++] = zFull[i]; |
| 2564 | } |
| 2565 | zFull[j] = 0; |
| 2566 | } |
| 2567 | #endif |
| 2568 | } |
| 2569 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 2570 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 2571 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 2572 | /* |
| 2573 | ** Interfaces for opening a shared library, finding entry points |
| 2574 | ** within the shared library, and closing the shared library. |
| 2575 | */ |
| 2576 | #include <dlfcn.h> |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2577 | static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){ |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 2578 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 2579 | } |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2580 | static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2581 | char *zErr; |
| 2582 | enterMutex(); |
| 2583 | zErr = dlerror(); |
| 2584 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2585 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2586 | }else if(nBuf>0) { |
| 2587 | zBufOut[0] = '\0'; |
| 2588 | } |
| 2589 | leaveMutex(); |
| 2590 | } |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2591 | void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){ |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 2592 | return dlsym(pHandle, zSymbol); |
| 2593 | } |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2594 | void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2595 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 2596 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2597 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 2598 | #define unixDlOpen 0 |
| 2599 | #define unixDlError 0 |
| 2600 | #define unixDlSym 0 |
| 2601 | #define unixDlClose 0 |
| 2602 | #endif |
| 2603 | |
| 2604 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2605 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2606 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2607 | static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2608 | |
| 2609 | assert(nBuf>=(sizeof(time_t)+sizeof(int))); |
| 2610 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2611 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 2612 | ** errors. The reports issued by valgrind are incorrect - we would |
| 2613 | ** prefer that the randomness be increased by making use of the |
| 2614 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 2615 | ** some users. Rather than argue, it seems easier just to initialize |
| 2616 | ** the whole array and silence valgrind, even if that means less randomness |
| 2617 | ** in the random seed. |
| 2618 | ** |
| 2619 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 2620 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2621 | ** tests repeatable. |
| 2622 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2623 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2624 | #if !defined(SQLITE_TEST) |
| 2625 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 2626 | int pid, fd; |
| 2627 | fd = open("/dev/urandom", O_RDONLY); |
| 2628 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 2629 | time_t t; |
| 2630 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2631 | memcpy(zBuf, &t, sizeof(t)); |
| 2632 | pid = getpid(); |
| 2633 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 2634 | }else{ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2635 | read(fd, zBuf, nBuf); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 2636 | close(fd); |
| 2637 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2638 | } |
| 2639 | #endif |
| 2640 | return SQLITE_OK; |
| 2641 | } |
| 2642 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2643 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2644 | /* |
| 2645 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2646 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 2647 | ** The return value is the number of microseconds of sleep actually |
| 2648 | ** requested from the underlying operating system, a number which |
| 2649 | ** might be greater than or equal to the argument, but not less |
| 2650 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2651 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2652 | static int unixSleep(sqlite3_vfs *pVfs, int microseconds){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2653 | #if defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2654 | usleep(microseconds); |
| 2655 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2656 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2657 | int seconds = (microseconds+999999)/1000000; |
| 2658 | sleep(seconds); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 2659 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 2660 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2664 | ** The following variable, if set to a non-zero value, becomes the result |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 2665 | ** returned from sqlite3OsCurrentTime(). This is used for testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2666 | */ |
| 2667 | #ifdef SQLITE_TEST |
| 2668 | int sqlite3_current_time = 0; |
| 2669 | #endif |
| 2670 | |
| 2671 | /* |
| 2672 | ** Find the current time (in Universal Coordinated Time). Write the |
| 2673 | ** current time and date as a Julian Day number into *prNow and |
| 2674 | ** return 0. Return 1 if the time and date cannot be found. |
| 2675 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2676 | static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2677 | #ifdef NO_GETTOD |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2678 | time_t t; |
| 2679 | time(&t); |
| 2680 | *prNow = t/86400.0 + 2440587.5; |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2681 | #else |
| 2682 | struct timeval sNow; |
drh | bdcc276 | 2007-04-02 18:06:57 +0000 | [diff] [blame] | 2683 | gettimeofday(&sNow, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2684 | *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0; |
| 2685 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2686 | #ifdef SQLITE_TEST |
| 2687 | if( sqlite3_current_time ){ |
| 2688 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 2689 | } |
| 2690 | #endif |
| 2691 | return 0; |
| 2692 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2693 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2694 | /* |
| 2695 | ** Return a pointer to the sqlite3DefaultVfs structure. We use |
| 2696 | ** a function rather than give the structure global scope because |
| 2697 | ** some compilers (MSVC) do not allow forward declarations of |
| 2698 | ** initialized structures. |
| 2699 | */ |
| 2700 | sqlite3_vfs *sqlite3OsDefaultVfs(void){ |
| 2701 | static sqlite3_vfs unixVfs = { |
| 2702 | 1, /* iVersion */ |
| 2703 | sizeof(unixFile), /* szOsFile */ |
| 2704 | MAX_PATHNAME, /* mxPathname */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2705 | 0, /* pNext */ |
| 2706 | "unix", /* zName */ |
| 2707 | 0, /* pAppData */ |
| 2708 | |
| 2709 | unixOpen, /* xOpen */ |
| 2710 | unixDelete, /* xDelete */ |
| 2711 | unixAccess, /* xAccess */ |
| 2712 | unixGetTempName, /* xGetTempName */ |
| 2713 | unixFullPathname, /* xFullPathname */ |
| 2714 | unixDlOpen, /* xDlOpen */ |
| 2715 | unixDlError, /* xDlError */ |
| 2716 | unixDlSym, /* xDlSym */ |
| 2717 | unixDlClose, /* xDlClose */ |
| 2718 | unixRandomness, /* xRandomness */ |
| 2719 | unixSleep, /* xSleep */ |
| 2720 | unixCurrentTime /* xCurrentTime */ |
| 2721 | }; |
| 2722 | |
| 2723 | return &unixVfs; |
| 2724 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 2725 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2726 | #endif /* OS_UNIX */ |