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