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