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