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