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