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