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