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