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