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 | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | #include <time.h> |
| 21 | #include <errno.h> |
| 22 | #include <unistd.h> |
| 23 | #ifndef O_LARGEFILE |
| 24 | # define O_LARGEFILE 0 |
| 25 | #endif |
| 26 | #ifdef SQLITE_DISABLE_LFS |
| 27 | # undef O_LARGEFILE |
| 28 | # define O_LARGEFILE 0 |
| 29 | #endif |
| 30 | #ifndef O_NOFOLLOW |
| 31 | # define O_NOFOLLOW 0 |
| 32 | #endif |
| 33 | #ifndef O_BINARY |
| 34 | # define O_BINARY 0 |
| 35 | #endif |
| 36 | |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 37 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 38 | /* |
| 39 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 40 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 41 | ** that always succeeds. This means that locking does not occur under |
| 42 | ** DJGPP. But its DOS - what did you expect? |
| 43 | */ |
| 44 | #ifdef __DJGPP__ |
| 45 | # define fcntl(A,B,C) 0 |
| 46 | #endif |
| 47 | |
| 48 | /* |
| 49 | ** Macros used to determine whether or not to use threads. The |
| 50 | ** SQLITE_UNIX_THREADS macro is defined if we are synchronizing for |
| 51 | ** Posix threads and SQLITE_W32_THREADS is defined if we are |
| 52 | ** synchronizing using Win32 threads. |
| 53 | */ |
| 54 | #if defined(THREADSAFE) && THREADSAFE |
| 55 | # include <pthread.h> |
| 56 | # define SQLITE_UNIX_THREADS 1 |
| 57 | #endif |
| 58 | |
| 59 | |
| 60 | /* |
| 61 | ** Include code that is common to all os_*.c files |
| 62 | */ |
| 63 | #include "os_common.h" |
| 64 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 65 | #if defined(THREADSAFE) && THREADSAFE && defined(__linux__) |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 66 | #define getpid pthread_self |
| 67 | #endif |
| 68 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 69 | /* |
| 70 | ** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996) |
| 71 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 72 | ** sets or clears a lock, that operation overrides any prior locks set |
| 73 | ** by the same process. It does not explicitly say so, but this implies |
| 74 | ** that it overrides locks set by the same process using a different |
| 75 | ** file descriptor. Consider this test case: |
| 76 | ** |
| 77 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
| 78 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 79 | ** |
| 80 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 81 | ** one is a hard or symbolic link to the other) then if you set |
| 82 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 83 | ** on fd2, it works. I would have expected the second lock to |
| 84 | ** fail since there was already a lock on the file due to fd1. |
| 85 | ** But not so. Since both locks came from the same process, the |
| 86 | ** second overrides the first, even though they were on different |
| 87 | ** file descriptors opened on different file names. |
| 88 | ** |
| 89 | ** Bummer. If you ask me, this is broken. Badly broken. It means |
| 90 | ** that we cannot use POSIX locks to synchronize file access among |
| 91 | ** competing threads of the same process. POSIX locks will work fine |
| 92 | ** to synchronize access for threads in separate processes, but not |
| 93 | ** threads within the same process. |
| 94 | ** |
| 95 | ** To work around the problem, SQLite has to manage file locks internally |
| 96 | ** on its own. Whenever a new database is opened, we have to find the |
| 97 | ** specific inode of the database file (the inode is determined by the |
| 98 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 99 | ** and check for locks already existing on that inode. When locks are |
| 100 | ** created or removed, we have to look at our own internal record of the |
| 101 | ** locks to see if another thread has previously set a lock on that same |
| 102 | ** inode. |
| 103 | ** |
| 104 | ** The OsFile structure for POSIX is no longer just an integer file |
| 105 | ** descriptor. It is now a structure that holds the integer file |
| 106 | ** descriptor and a pointer to a structure that describes the internal |
| 107 | ** locks on the corresponding inode. There is one locking structure |
| 108 | ** per inode, so if the same inode is opened twice, both OsFile structures |
| 109 | ** point to the same locking structure. The locking structure keeps |
| 110 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 111 | ** field that tells us its internal lock status. cnt==0 means the |
| 112 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 113 | ** cnt>0 means there are cnt shared locks on the file. |
| 114 | ** |
| 115 | ** Any attempt to lock or unlock a file first checks the locking |
| 116 | ** structure. The fcntl() system call is only invoked to set a |
| 117 | ** POSIX lock if the internal lock structure transitions between |
| 118 | ** a locked and an unlocked state. |
| 119 | ** |
| 120 | ** 2004-Jan-11: |
| 121 | ** More recent discoveries about POSIX advisory locks. (The more |
| 122 | ** I discover, the more I realize the a POSIX advisory locks are |
| 123 | ** an abomination.) |
| 124 | ** |
| 125 | ** If you close a file descriptor that points to a file that has locks, |
| 126 | ** all locks on that file that are owned by the current process are |
| 127 | ** released. To work around this problem, each OsFile structure contains |
| 128 | ** a pointer to an openCnt structure. There is one openCnt structure |
| 129 | ** per open inode, which means that multiple OsFiles can point to a single |
| 130 | ** openCnt. When an attempt is made to close an OsFile, if there are |
| 131 | ** other OsFiles open on the same inode that are holding locks, the call |
| 132 | ** to close() the file descriptor is deferred until all of the locks clear. |
| 133 | ** The openCnt structure keeps a list of file descriptors that need to |
| 134 | ** be closed and that list is walked (and cleared) when the last lock |
| 135 | ** clears. |
| 136 | ** |
| 137 | ** First, under Linux threads, because each thread has a separate |
| 138 | ** process ID, lock operations in one thread do not override locks |
| 139 | ** to the same file in other threads. Linux threads behave like |
| 140 | ** separate processes in this respect. But, if you close a file |
| 141 | ** descriptor in linux threads, all locks are cleared, even locks |
| 142 | ** on other threads and even though the other threads have different |
| 143 | ** process IDs. Linux threads is inconsistent in this respect. |
| 144 | ** (I'm beginning to think that linux threads is an abomination too.) |
| 145 | ** The consequence of this all is that the hash table for the lockInfo |
| 146 | ** structure has to include the process id as part of its key because |
| 147 | ** locks in different threads are treated as distinct. But the |
| 148 | ** openCnt structure should not include the process id in its |
| 149 | ** key because close() clears lock on all threads, not just the current |
| 150 | ** thread. Were it not for this goofiness in linux threads, we could |
| 151 | ** combine the lockInfo and openCnt structures into a single structure. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 152 | ** |
| 153 | ** 2004-Jun-28: |
| 154 | ** On some versions of linux, threads can override each others locks. |
| 155 | ** On others not. Sometimes you can change the behavior on the same |
| 156 | ** system by setting the LD_ASSUME_KERNEL environment variable. The |
| 157 | ** POSIX standard is silent as to which behavior is correct, as far |
| 158 | ** as I can tell, so other versions of unix might show the same |
| 159 | ** inconsistency. There is no little doubt in my mind that posix |
| 160 | ** advisory locks and linux threads are profoundly broken. |
| 161 | ** |
| 162 | ** To work around the inconsistencies, we have to test at runtime |
| 163 | ** whether or not threads can override each others locks. This test |
| 164 | ** is run once, the first time any lock is attempted. A static |
| 165 | ** variable is set to record the results of this test for future |
| 166 | ** use. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 167 | */ |
| 168 | |
| 169 | /* |
| 170 | ** An instance of the following structure serves as the key used |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 171 | ** to locate a particular lockInfo structure given its inode. |
| 172 | ** |
| 173 | ** If threads cannot override each others locks, then we set the |
| 174 | ** lockKey.tid field to the thread ID. If threads can override |
| 175 | ** each others locks then tid is always set to zero. tid is also |
| 176 | ** set to zero if we compile without threading support. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 177 | */ |
| 178 | struct lockKey { |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 179 | dev_t dev; /* Device number */ |
| 180 | ino_t ino; /* Inode number */ |
| 181 | #ifdef SQLITE_UNIX_THREADS |
| 182 | pthread_t tid; /* Thread ID or zero if threads cannot override each other */ |
| 183 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | /* |
| 187 | ** An instance of the following structure is allocated for each open |
| 188 | ** inode on each thread with a different process ID. (Threads have |
| 189 | ** different process IDs on linux, but not on most other unixes.) |
| 190 | ** |
| 191 | ** A single inode can have multiple file descriptors, so each OsFile |
| 192 | ** structure contains a pointer to an instance of this object and this |
| 193 | ** object keeps a count of the number of OsFiles pointing to it. |
| 194 | */ |
| 195 | struct lockInfo { |
| 196 | struct lockKey key; /* The lookup key */ |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 197 | int cnt; /* Number of SHARED locks held */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 198 | int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 199 | int nRef; /* Number of pointers to this structure */ |
| 200 | }; |
| 201 | |
| 202 | /* |
| 203 | ** An instance of the following structure serves as the key used |
| 204 | ** to locate a particular openCnt structure given its inode. This |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 205 | ** is the same as the lockKey except that the thread ID is omitted. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 206 | */ |
| 207 | struct openKey { |
| 208 | dev_t dev; /* Device number */ |
| 209 | ino_t ino; /* Inode number */ |
| 210 | }; |
| 211 | |
| 212 | /* |
| 213 | ** An instance of the following structure is allocated for each open |
| 214 | ** inode. This structure keeps track of the number of locks on that |
| 215 | ** inode. If a close is attempted against an inode that is holding |
| 216 | ** locks, the close is deferred until all locks clear by adding the |
| 217 | ** file descriptor to be closed to the pending list. |
| 218 | */ |
| 219 | struct openCnt { |
| 220 | struct openKey key; /* The lookup key */ |
| 221 | int nRef; /* Number of pointers to this structure */ |
| 222 | int nLock; /* Number of outstanding locks */ |
| 223 | int nPending; /* Number of pending close() operations */ |
| 224 | int *aPending; /* Malloced space holding fd's awaiting a close() */ |
| 225 | }; |
| 226 | |
| 227 | /* |
| 228 | ** These hash table maps inodes and process IDs into lockInfo and openCnt |
| 229 | ** structures. Access to these hash tables must be protected by a mutex. |
| 230 | */ |
| 231 | static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 }; |
| 232 | static Hash openHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 }; |
| 233 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 234 | |
| 235 | #ifdef SQLITE_UNIX_THREADS |
| 236 | /* |
| 237 | ** This variable records whether or not threads can override each others |
| 238 | ** locks. |
| 239 | ** |
| 240 | ** 0: No. Threads cannot override each others locks. |
| 241 | ** 1: Yes. Threads can override each others locks. |
| 242 | ** -1: We don't know yet. |
| 243 | */ |
| 244 | static int threadsOverrideEachOthersLocks = -1; |
| 245 | |
| 246 | /* |
| 247 | ** This structure holds information passed into individual test |
| 248 | ** threads by the testThreadLockingBehavior() routine. |
| 249 | */ |
| 250 | struct threadTestData { |
| 251 | int fd; /* File to be locked */ |
| 252 | struct flock lock; /* The locking operation */ |
| 253 | int result; /* Result of the locking operation */ |
| 254 | }; |
| 255 | |
| 256 | /* |
| 257 | ** The testThreadLockingBehavior() routine launches two separate |
| 258 | ** threads on this routine. This routine attempts to lock a file |
| 259 | ** descriptor then returns. The success or failure of that attempt |
| 260 | ** allows the testThreadLockingBehavior() procedure to determine |
| 261 | ** whether or not threads can override each others locks. |
| 262 | */ |
| 263 | static void *threadLockingTest(void *pArg){ |
| 264 | struct threadTestData *pData = (struct threadTestData*)pArg; |
| 265 | pData->result = fcntl(pData->fd, F_SETLK, &pData->lock); |
| 266 | return pArg; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | ** This procedure attempts to determine whether or not threads |
| 271 | ** can override each others locks then sets the |
| 272 | ** threadsOverrideEachOthersLocks variable appropriately. |
| 273 | */ |
| 274 | static void testThreadLockingBehavior(fd_orig){ |
| 275 | int fd; |
| 276 | struct threadTestData d[2]; |
| 277 | pthread_t t[2]; |
| 278 | |
| 279 | fd = dup(fd_orig); |
| 280 | if( fd<0 ) return; |
| 281 | memset(d, 0, sizeof(d)); |
| 282 | d[0].fd = fd; |
| 283 | d[0].lock.l_type = F_RDLCK; |
| 284 | d[0].lock.l_len = 1; |
| 285 | d[0].lock.l_start = 0; |
| 286 | d[0].lock.l_whence = SEEK_SET; |
| 287 | d[1] = d[0]; |
| 288 | d[1].lock.l_type = F_WRLCK; |
| 289 | pthread_create(&t[0], 0, threadLockingTest, &d[0]); |
| 290 | pthread_create(&t[1], 0, threadLockingTest, &d[1]); |
| 291 | pthread_join(t[0], 0); |
| 292 | pthread_join(t[1], 0); |
| 293 | close(fd); |
| 294 | threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0; |
| 295 | } |
| 296 | #endif /* SQLITE_UNIX_THREADS */ |
| 297 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 298 | /* |
| 299 | ** Release a lockInfo structure previously allocated by findLockInfo(). |
| 300 | */ |
| 301 | static void releaseLockInfo(struct lockInfo *pLock){ |
| 302 | pLock->nRef--; |
| 303 | if( pLock->nRef==0 ){ |
| 304 | sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0); |
| 305 | sqliteFree(pLock); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | ** Release a openCnt structure previously allocated by findLockInfo(). |
| 311 | */ |
| 312 | static void releaseOpenCnt(struct openCnt *pOpen){ |
| 313 | pOpen->nRef--; |
| 314 | if( pOpen->nRef==0 ){ |
| 315 | sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0); |
| 316 | sqliteFree(pOpen->aPending); |
| 317 | sqliteFree(pOpen); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | ** Given a file descriptor, locate lockInfo and openCnt structures that |
| 323 | ** describes that file descriptor. Create a new ones if necessary. The |
| 324 | ** return values might be unset if an error occurs. |
| 325 | ** |
| 326 | ** Return the number of errors. |
| 327 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 328 | static int findLockInfo( |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 329 | int fd, /* The file descriptor used in the key */ |
| 330 | struct lockInfo **ppLock, /* Return the lockInfo structure here */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 331 | struct openCnt **ppOpen /* Return the openCnt structure here */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 332 | ){ |
| 333 | int rc; |
| 334 | struct lockKey key1; |
| 335 | struct openKey key2; |
| 336 | struct stat statbuf; |
| 337 | struct lockInfo *pLock; |
| 338 | struct openCnt *pOpen; |
| 339 | rc = fstat(fd, &statbuf); |
| 340 | if( rc!=0 ) return 1; |
| 341 | memset(&key1, 0, sizeof(key1)); |
| 342 | key1.dev = statbuf.st_dev; |
| 343 | key1.ino = statbuf.st_ino; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 344 | #ifdef SQLITE_UNIX_THREADS |
| 345 | if( threadsOverrideEachOthersLocks<0 ){ |
| 346 | testThreadLockingBehavior(fd); |
| 347 | } |
| 348 | key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self(); |
| 349 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 350 | memset(&key2, 0, sizeof(key2)); |
| 351 | key2.dev = statbuf.st_dev; |
| 352 | key2.ino = statbuf.st_ino; |
| 353 | pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1)); |
| 354 | if( pLock==0 ){ |
| 355 | struct lockInfo *pOld; |
| 356 | pLock = sqliteMallocRaw( sizeof(*pLock) ); |
| 357 | if( pLock==0 ) return 1; |
| 358 | pLock->key = key1; |
| 359 | pLock->nRef = 1; |
| 360 | pLock->cnt = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 361 | pLock->locktype = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 362 | pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock); |
| 363 | if( pOld!=0 ){ |
| 364 | assert( pOld==pLock ); |
| 365 | sqliteFree(pLock); |
| 366 | return 1; |
| 367 | } |
| 368 | }else{ |
| 369 | pLock->nRef++; |
| 370 | } |
| 371 | *ppLock = pLock; |
| 372 | pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2)); |
| 373 | if( pOpen==0 ){ |
| 374 | struct openCnt *pOld; |
| 375 | pOpen = sqliteMallocRaw( sizeof(*pOpen) ); |
| 376 | if( pOpen==0 ){ |
| 377 | releaseLockInfo(pLock); |
| 378 | return 1; |
| 379 | } |
| 380 | pOpen->key = key2; |
| 381 | pOpen->nRef = 1; |
| 382 | pOpen->nLock = 0; |
| 383 | pOpen->nPending = 0; |
| 384 | pOpen->aPending = 0; |
| 385 | pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen); |
| 386 | if( pOld!=0 ){ |
| 387 | assert( pOld==pOpen ); |
| 388 | sqliteFree(pOpen); |
| 389 | releaseLockInfo(pLock); |
| 390 | return 1; |
| 391 | } |
| 392 | }else{ |
| 393 | pOpen->nRef++; |
| 394 | } |
| 395 | *ppOpen = pOpen; |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | ** Delete the named file |
| 401 | */ |
| 402 | int sqlite3OsDelete(const char *zFilename){ |
| 403 | unlink(zFilename); |
| 404 | return SQLITE_OK; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | ** Return TRUE if the named file exists. |
| 409 | */ |
| 410 | int sqlite3OsFileExists(const char *zFilename){ |
| 411 | return access(zFilename, 0)==0; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | ** Attempt to open a file for both reading and writing. If that |
| 416 | ** fails, try opening it read-only. If the file does not exist, |
| 417 | ** try to create it. |
| 418 | ** |
| 419 | ** On success, a handle for the open file is written to *id |
| 420 | ** and *pReadonly is set to 0 if the file was opened for reading and |
| 421 | ** writing or 1 if the file was opened read-only. The function returns |
| 422 | ** SQLITE_OK. |
| 423 | ** |
| 424 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 425 | ** *id and *pReadonly unchanged. |
| 426 | */ |
| 427 | int sqlite3OsOpenReadWrite( |
| 428 | const char *zFilename, |
| 429 | OsFile *id, |
| 430 | int *pReadonly |
| 431 | ){ |
| 432 | int rc; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 433 | assert( !id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 434 | id->dirfd = -1; |
drh | 8e85577 | 2005-05-17 11:25:31 +0000 | [diff] [blame] | 435 | id->h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY, |
| 436 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 437 | if( id->h<0 ){ |
drh | 6458e39 | 2004-07-20 01:14:13 +0000 | [diff] [blame] | 438 | #ifdef EISDIR |
| 439 | if( errno==EISDIR ){ |
| 440 | return SQLITE_CANTOPEN; |
| 441 | } |
| 442 | #endif |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 443 | id->h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY); |
| 444 | if( id->h<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 445 | return SQLITE_CANTOPEN; |
| 446 | } |
| 447 | *pReadonly = 1; |
| 448 | }else{ |
| 449 | *pReadonly = 0; |
| 450 | } |
| 451 | sqlite3OsEnterMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 452 | rc = findLockInfo(id->h, &id->pLock, &id->pOpen); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 453 | sqlite3OsLeaveMutex(); |
| 454 | if( rc ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 455 | close(id->h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 456 | return SQLITE_NOMEM; |
| 457 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 458 | id->locktype = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 459 | id->isOpen = 1; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 460 | TRACE3("OPEN %-3d %s\n", id->h, zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 461 | OpenCounter(+1); |
| 462 | return SQLITE_OK; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | /* |
| 467 | ** Attempt to open a new file for exclusive access by this process. |
| 468 | ** The file will be opened for both reading and writing. To avoid |
| 469 | ** a potential security problem, we do not allow the file to have |
| 470 | ** previously existed. Nor do we allow the file to be a symbolic |
| 471 | ** link. |
| 472 | ** |
| 473 | ** If delFlag is true, then make arrangements to automatically delete |
| 474 | ** the file when it is closed. |
| 475 | ** |
| 476 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 477 | ** |
| 478 | ** On failure, return SQLITE_CANTOPEN. |
| 479 | */ |
| 480 | int sqlite3OsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){ |
| 481 | int rc; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 482 | assert( !id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 483 | if( access(zFilename, 0)==0 ){ |
| 484 | return SQLITE_CANTOPEN; |
| 485 | } |
| 486 | id->dirfd = -1; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 487 | id->h = open(zFilename, |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 488 | O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY, 0600); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 489 | if( id->h<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 490 | return SQLITE_CANTOPEN; |
| 491 | } |
| 492 | sqlite3OsEnterMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 493 | rc = findLockInfo(id->h, &id->pLock, &id->pOpen); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 494 | sqlite3OsLeaveMutex(); |
| 495 | if( rc ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 496 | close(id->h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 497 | unlink(zFilename); |
| 498 | return SQLITE_NOMEM; |
| 499 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 500 | id->locktype = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 501 | id->isOpen = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 502 | if( delFlag ){ |
| 503 | unlink(zFilename); |
| 504 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 505 | TRACE3("OPEN-EX %-3d %s\n", id->h, zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 506 | OpenCounter(+1); |
| 507 | return SQLITE_OK; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | ** Attempt to open a new file for read-only access. |
| 512 | ** |
| 513 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 514 | ** |
| 515 | ** On failure, return SQLITE_CANTOPEN. |
| 516 | */ |
| 517 | int sqlite3OsOpenReadOnly(const char *zFilename, OsFile *id){ |
| 518 | int rc; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 519 | assert( !id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 520 | id->dirfd = -1; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 521 | id->h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY); |
| 522 | if( id->h<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 523 | return SQLITE_CANTOPEN; |
| 524 | } |
| 525 | sqlite3OsEnterMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 526 | rc = findLockInfo(id->h, &id->pLock, &id->pOpen); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 527 | sqlite3OsLeaveMutex(); |
| 528 | if( rc ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 529 | close(id->h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 530 | return SQLITE_NOMEM; |
| 531 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 532 | id->locktype = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 533 | id->isOpen = 1; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 534 | TRACE3("OPEN-RO %-3d %s\n", id->h, zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 535 | OpenCounter(+1); |
| 536 | return SQLITE_OK; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | ** Attempt to open a file descriptor for the directory that contains a |
| 541 | ** file. This file descriptor can be used to fsync() the directory |
| 542 | ** in order to make sure the creation of a new file is actually written |
| 543 | ** to disk. |
| 544 | ** |
| 545 | ** This routine is only meaningful for Unix. It is a no-op under |
| 546 | ** windows since windows does not support hard links. |
| 547 | ** |
| 548 | ** On success, a handle for a previously open file is at *id is |
| 549 | ** updated with the new directory file descriptor and SQLITE_OK is |
| 550 | ** returned. |
| 551 | ** |
| 552 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 553 | ** *id unchanged. |
| 554 | */ |
| 555 | int sqlite3OsOpenDirectory( |
| 556 | const char *zDirname, |
| 557 | OsFile *id |
| 558 | ){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 559 | if( !id->isOpen ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 560 | /* Do not open the directory if the corresponding file is not already |
| 561 | ** open. */ |
| 562 | return SQLITE_CANTOPEN; |
| 563 | } |
| 564 | assert( id->dirfd<0 ); |
drh | 8e85577 | 2005-05-17 11:25:31 +0000 | [diff] [blame] | 565 | id->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 566 | if( id->dirfd<0 ){ |
| 567 | return SQLITE_CANTOPEN; |
| 568 | } |
| 569 | TRACE3("OPENDIR %-3d %s\n", id->dirfd, zDirname); |
| 570 | return SQLITE_OK; |
| 571 | } |
| 572 | |
| 573 | /* |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 574 | ** If the following global variable points to a string which is the |
| 575 | ** name of a directory, then that directory will be used to store |
| 576 | ** temporary files. |
| 577 | */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 578 | char *sqlite3_temp_directory = 0; |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 579 | |
| 580 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 581 | ** Create a temporary file name in zBuf. zBuf must be big enough to |
| 582 | ** hold at least SQLITE_TEMPNAME_SIZE characters. |
| 583 | */ |
| 584 | int sqlite3OsTempFileName(char *zBuf){ |
| 585 | static const char *azDirs[] = { |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 586 | 0, |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 587 | "/var/tmp", |
| 588 | "/usr/tmp", |
| 589 | "/tmp", |
| 590 | ".", |
| 591 | }; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 592 | static const unsigned char zChars[] = |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 593 | "abcdefghijklmnopqrstuvwxyz" |
| 594 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 595 | "0123456789"; |
| 596 | int i, j; |
| 597 | struct stat buf; |
| 598 | const char *zDir = "."; |
drh | effd02b | 2004-08-29 23:42:13 +0000 | [diff] [blame] | 599 | azDirs[0] = sqlite3_temp_directory; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 600 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 601 | if( azDirs[i]==0 ) continue; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 602 | if( stat(azDirs[i], &buf) ) continue; |
| 603 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 604 | if( access(azDirs[i], 07) ) continue; |
| 605 | zDir = azDirs[i]; |
| 606 | break; |
| 607 | } |
| 608 | do{ |
| 609 | sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir); |
| 610 | j = strlen(zBuf); |
| 611 | sqlite3Randomness(15, &zBuf[j]); |
| 612 | for(i=0; i<15; i++, j++){ |
| 613 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 614 | } |
| 615 | zBuf[j] = 0; |
| 616 | }while( access(zBuf,0)==0 ); |
| 617 | return SQLITE_OK; |
| 618 | } |
| 619 | |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 620 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 621 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 622 | ** Check that a given pathname is a directory and is writable |
| 623 | ** |
| 624 | */ |
| 625 | int sqlite3OsIsDirWritable(char *zBuf){ |
| 626 | struct stat buf; |
| 627 | if( zBuf==0 ) return 0; |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 628 | if( zBuf[0]==0 ) return 0; |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 629 | if( stat(zBuf, &buf) ) return 0; |
| 630 | if( !S_ISDIR(buf.st_mode) ) return 0; |
| 631 | if( access(zBuf, 07) ) return 0; |
| 632 | return 1; |
| 633 | } |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 634 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 635 | |
| 636 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 637 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 638 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 639 | ** wrong. |
| 640 | */ |
| 641 | int sqlite3OsRead(OsFile *id, void *pBuf, int amt){ |
| 642 | int got; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 643 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 644 | SimulateIOError(SQLITE_IOERR); |
| 645 | TIMER_START; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 646 | got = read(id->h, pBuf, amt); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 647 | TIMER_END; |
drh | e29b915 | 2005-03-18 14:03:15 +0000 | [diff] [blame] | 648 | TRACE5("READ %-3d %5d %7d %d\n", id->h, got, last_page, TIMER_ELAPSED); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 649 | SEEK(0); |
| 650 | /* if( got<0 ) got = 0; */ |
| 651 | if( got==amt ){ |
| 652 | return SQLITE_OK; |
| 653 | }else{ |
| 654 | return SQLITE_IOERR; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 660 | ** or some other error code on failure. |
| 661 | */ |
| 662 | int sqlite3OsWrite(OsFile *id, const void *pBuf, int amt){ |
| 663 | int wrote = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 664 | assert( id->isOpen ); |
drh | 4c7f941 | 2005-02-03 00:29:47 +0000 | [diff] [blame] | 665 | assert( amt>0 ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 666 | SimulateIOError(SQLITE_IOERR); |
drh | 047d483 | 2004-10-01 14:38:02 +0000 | [diff] [blame] | 667 | SimulateDiskfullError; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 668 | TIMER_START; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 669 | while( amt>0 && (wrote = write(id->h, pBuf, amt))>0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 670 | amt -= wrote; |
| 671 | pBuf = &((char*)pBuf)[wrote]; |
| 672 | } |
| 673 | TIMER_END; |
drh | e29b915 | 2005-03-18 14:03:15 +0000 | [diff] [blame] | 674 | TRACE5("WRITE %-3d %5d %7d %d\n", id->h, wrote, last_page, TIMER_ELAPSED); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 675 | SEEK(0); |
| 676 | if( amt>0 ){ |
| 677 | return SQLITE_FULL; |
| 678 | } |
| 679 | return SQLITE_OK; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | ** Move the read/write pointer in a file. |
| 684 | */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 685 | int sqlite3OsSeek(OsFile *id, i64 offset){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 686 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 687 | SEEK(offset/1024 + 1); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 688 | lseek(id->h, offset, SEEK_SET); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 689 | return SQLITE_OK; |
| 690 | } |
| 691 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 692 | #ifdef SQLITE_TEST |
| 693 | /* |
| 694 | ** Count the number of fullsyncs and normal syncs. This is used to test |
| 695 | ** that syncs and fullsyncs are occuring at the right times. |
| 696 | */ |
| 697 | int sqlite3_sync_count = 0; |
| 698 | int sqlite3_fullsync_count = 0; |
| 699 | #endif |
| 700 | |
| 701 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 702 | /* |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 703 | ** The fsync() system call does not work as advertised on many |
| 704 | ** unix systems. The following procedure is an attempt to make |
| 705 | ** it work better. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 706 | ** |
| 707 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 708 | ** for testing when we want to run through the test suite quickly. |
| 709 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 710 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 711 | ** or power failure will likely corrupt the database file. |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 712 | */ |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 713 | static int full_fsync(int fd, int fullSync){ |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 714 | int rc; |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 715 | |
| 716 | /* Record the number of times that we do a normal fsync() and |
| 717 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 718 | ** gets called with the correct arguments. |
| 719 | */ |
| 720 | #ifdef SQLITE_TEST |
| 721 | if( fullSync ) sqlite3_fullsync_count++; |
| 722 | sqlite3_sync_count++; |
| 723 | #endif |
| 724 | |
| 725 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 726 | ** no-op |
| 727 | */ |
| 728 | #ifdef SQLITE_NO_SYNC |
| 729 | rc = SQLITE_OK; |
| 730 | #else |
| 731 | |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 732 | #ifdef F_FULLFSYNC |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 733 | if( fullSync ){ |
drh | f30cc94 | 2005-03-11 17:52:34 +0000 | [diff] [blame] | 734 | rc = fcntl(fd, F_FULLFSYNC, 0); |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 735 | }else{ |
| 736 | rc = 1; |
| 737 | } |
| 738 | /* If the FULLSYNC failed, try to do a normal fsync() */ |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 739 | if( rc ) rc = fsync(fd); |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 740 | |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 741 | #else |
| 742 | rc = fsync(fd); |
drh | f30cc94 | 2005-03-11 17:52:34 +0000 | [diff] [blame] | 743 | #endif /* defined(F_FULLFSYNC) */ |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 744 | #endif /* defined(SQLITE_NO_SYNC) */ |
| 745 | |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 746 | return rc; |
| 747 | } |
| 748 | |
| 749 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 750 | ** Make sure all writes to a particular file are committed to disk. |
| 751 | ** |
| 752 | ** Under Unix, also make sure that the directory entry for the file |
| 753 | ** has been created by fsync-ing the directory that contains the file. |
| 754 | ** If we do not do this and we encounter a power failure, the directory |
| 755 | ** entry for the journal might not exist after we reboot. The next |
| 756 | ** SQLite to access the file will not know that the journal exists (because |
| 757 | ** the directory entry for the journal was never created) and the transaction |
| 758 | ** will not roll back - possibly leading to database corruption. |
| 759 | */ |
| 760 | int sqlite3OsSync(OsFile *id){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 761 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 762 | SimulateIOError(SQLITE_IOERR); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 763 | TRACE2("SYNC %-3d\n", id->h); |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 764 | if( full_fsync(id->h, id->fullSync) ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 765 | return SQLITE_IOERR; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 766 | } |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 767 | if( id->dirfd>=0 ){ |
| 768 | TRACE2("DIRSYNC %-3d\n", id->dirfd); |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 769 | full_fsync(id->dirfd, id->fullSync); |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 770 | close(id->dirfd); /* Only need to sync once, so close the directory */ |
| 771 | id->dirfd = -1; /* when we are done. */ |
| 772 | } |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 773 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 777 | ** Sync the directory zDirname. This is a no-op on operating systems other |
| 778 | ** than UNIX. |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 779 | ** |
| 780 | ** This is used to make sure the master journal file has truely been deleted |
| 781 | ** before making changes to individual journals on a multi-database commit. |
drh | f30cc94 | 2005-03-11 17:52:34 +0000 | [diff] [blame] | 782 | ** The F_FULLFSYNC option is not needed here. |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 783 | */ |
| 784 | int sqlite3OsSyncDirectory(const char *zDirname){ |
| 785 | int fd; |
| 786 | int r; |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 787 | SimulateIOError(SQLITE_IOERR); |
drh | 8e85577 | 2005-05-17 11:25:31 +0000 | [diff] [blame] | 788 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 789 | TRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname); |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 790 | if( fd<0 ){ |
| 791 | return SQLITE_CANTOPEN; |
| 792 | } |
| 793 | r = fsync(fd); |
| 794 | close(fd); |
| 795 | return ((r==0)?SQLITE_OK:SQLITE_IOERR); |
| 796 | } |
| 797 | |
| 798 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 799 | ** Truncate an open file to a specified size |
| 800 | */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 801 | int sqlite3OsTruncate(OsFile *id, i64 nByte){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 802 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 803 | SimulateIOError(SQLITE_IOERR); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 804 | return ftruncate(id->h, nByte)==0 ? SQLITE_OK : SQLITE_IOERR; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /* |
| 808 | ** Determine the current size of a file in bytes |
| 809 | */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 810 | int sqlite3OsFileSize(OsFile *id, i64 *pSize){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 811 | struct stat buf; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 812 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 813 | SimulateIOError(SQLITE_IOERR); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 814 | if( fstat(id->h, &buf)!=0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 815 | return SQLITE_IOERR; |
| 816 | } |
| 817 | *pSize = buf.st_size; |
| 818 | return SQLITE_OK; |
| 819 | } |
| 820 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 821 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 822 | ** This routine checks if there is a RESERVED lock held on the specified |
| 823 | ** 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] | 824 | ** non-zero. If the file is unlocked or holds only SHARED locks, then |
| 825 | ** return zero. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 826 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 827 | int sqlite3OsCheckReservedLock(OsFile *id){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 828 | int r = 0; |
| 829 | |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 830 | assert( id->isOpen ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 831 | sqlite3OsEnterMutex(); /* Needed because id->pLock is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 832 | |
| 833 | /* Check if a thread in this process holds such a lock */ |
| 834 | if( id->pLock->locktype>SHARED_LOCK ){ |
| 835 | r = 1; |
| 836 | } |
| 837 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 838 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 839 | */ |
| 840 | if( !r ){ |
| 841 | struct flock lock; |
| 842 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 843 | lock.l_start = RESERVED_BYTE; |
| 844 | lock.l_len = 1; |
| 845 | lock.l_type = F_WRLCK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 846 | fcntl(id->h, F_GETLK, &lock); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 847 | if( lock.l_type!=F_UNLCK ){ |
| 848 | r = 1; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | sqlite3OsLeaveMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 853 | TRACE3("TEST WR-LOCK %d %d\n", id->h, r); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 854 | |
| 855 | return r; |
| 856 | } |
| 857 | |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 858 | #ifdef SQLITE_DEBUG |
| 859 | /* |
| 860 | ** Helper function for printing out trace information from debugging |
| 861 | ** binaries. This returns the string represetation of the supplied |
| 862 | ** integer lock-type. |
| 863 | */ |
| 864 | static const char * locktypeName(int locktype){ |
| 865 | switch( locktype ){ |
| 866 | case NO_LOCK: return "NONE"; |
| 867 | case SHARED_LOCK: return "SHARED"; |
| 868 | case RESERVED_LOCK: return "RESERVED"; |
| 869 | case PENDING_LOCK: return "PENDING"; |
| 870 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
| 871 | } |
| 872 | return "ERROR"; |
| 873 | } |
| 874 | #endif |
| 875 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 876 | /* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 877 | ** Lock the file with the lock specified by parameter locktype - one |
| 878 | ** of the following: |
| 879 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 880 | ** (1) SHARED_LOCK |
| 881 | ** (2) RESERVED_LOCK |
| 882 | ** (3) PENDING_LOCK |
| 883 | ** (4) EXCLUSIVE_LOCK |
| 884 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 885 | ** Sometimes when requesting one lock state, additional lock states |
| 886 | ** are inserted in between. The locking might fail on one of the later |
| 887 | ** transitions leaving the lock state different from what it started but |
| 888 | ** still short of its goal. The following chart shows the allowed |
| 889 | ** transitions and the inserted intermediate states: |
| 890 | ** |
| 891 | ** UNLOCKED -> SHARED |
| 892 | ** SHARED -> RESERVED |
| 893 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 894 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 895 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 896 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 897 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 898 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 899 | */ |
| 900 | int sqlite3OsLock(OsFile *id, int locktype){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 901 | /* The following describes the implementation of the various locks and |
| 902 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 903 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 904 | ** confusion with SQLite lock names). The algorithms are complicated |
| 905 | ** slightly in order to be compatible with windows systems simultaneously |
| 906 | ** accessing the same database file, in case that is ever required. |
| 907 | ** |
| 908 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 909 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 910 | ** range', a range of 510 bytes at a well known offset. |
| 911 | ** |
| 912 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 913 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 914 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 915 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 916 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 917 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 918 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 919 | ** |
| 920 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 921 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 922 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 923 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 924 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 925 | ** This property is used by the algorithm for rolling back a journal file |
| 926 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 927 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 928 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 929 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 930 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 931 | ** within this range, this ensures that no other locks are held on the |
| 932 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 933 | ** |
| 934 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 935 | ** range' is that some versions of windows do not support read-locks. By |
| 936 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 937 | ** even if the locking primitive used is always a write-lock. |
| 938 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 939 | int rc = SQLITE_OK; |
| 940 | struct lockInfo *pLock = id->pLock; |
| 941 | struct flock lock; |
| 942 | int s; |
| 943 | |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 944 | assert( id->isOpen ); |
drh | e29b915 | 2005-03-18 14:03:15 +0000 | [diff] [blame] | 945 | TRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", id->h, locktypeName(locktype), |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 946 | locktypeName(id->locktype), locktypeName(pLock->locktype), pLock->cnt |
| 947 | ,getpid() ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 948 | |
| 949 | /* If there is already a lock of this type or more restrictive on the |
| 950 | ** OsFile, do nothing. Don't use the end_lock: exit path, as |
| 951 | ** sqlite3OsEnterMutex() hasn't been called yet. |
| 952 | */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 953 | if( id->locktype>=locktype ){ |
drh | e29b915 | 2005-03-18 14:03:15 +0000 | [diff] [blame] | 954 | TRACE3("LOCK %d %s ok (already held)\n", id->h, locktypeName(locktype)); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 955 | return SQLITE_OK; |
| 956 | } |
| 957 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 958 | /* Make sure the locking sequence is correct |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 959 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 960 | assert( id->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 961 | assert( locktype!=PENDING_LOCK ); |
| 962 | assert( locktype!=RESERVED_LOCK || id->locktype==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 963 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 964 | /* This mutex is needed because id->pLock is shared across threads |
| 965 | */ |
| 966 | sqlite3OsEnterMutex(); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 967 | |
| 968 | /* If some thread using this PID has a lock via a different OsFile* |
| 969 | ** handle that precludes the requested lock, return BUSY. |
| 970 | */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 971 | if( (id->locktype!=pLock->locktype && |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 972 | (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 973 | ){ |
| 974 | rc = SQLITE_BUSY; |
| 975 | goto end_lock; |
| 976 | } |
| 977 | |
| 978 | /* If a SHARED lock is requested, and some thread using this PID already |
| 979 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 980 | ** return SQLITE_OK. |
| 981 | */ |
| 982 | if( locktype==SHARED_LOCK && |
| 983 | (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){ |
| 984 | assert( locktype==SHARED_LOCK ); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 985 | assert( id->locktype==0 ); |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 986 | assert( pLock->cnt>0 ); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 987 | id->locktype = SHARED_LOCK; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 988 | pLock->cnt++; |
| 989 | id->pOpen->nLock++; |
| 990 | goto end_lock; |
| 991 | } |
| 992 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 993 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 994 | lock.l_whence = SEEK_SET; |
| 995 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 996 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 997 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 998 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 999 | */ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1000 | if( locktype==SHARED_LOCK |
| 1001 | || (locktype==EXCLUSIVE_LOCK && id->locktype<PENDING_LOCK) |
| 1002 | ){ |
danielk1977 | 489468c | 2004-06-28 08:25:47 +0000 | [diff] [blame] | 1003 | lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1004 | lock.l_start = PENDING_BYTE; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1005 | s = fcntl(id->h, F_SETLK, &lock); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1006 | if( s ){ |
| 1007 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 1008 | goto end_lock; |
| 1009 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | /* If control gets to this point, then actually go ahead and make |
| 1014 | ** operating system calls for the specified lock. |
| 1015 | */ |
| 1016 | if( locktype==SHARED_LOCK ){ |
| 1017 | assert( pLock->cnt==0 ); |
| 1018 | assert( pLock->locktype==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1019 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1020 | /* Now get the read-lock */ |
| 1021 | lock.l_start = SHARED_FIRST; |
| 1022 | lock.l_len = SHARED_SIZE; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1023 | s = fcntl(id->h, F_SETLK, &lock); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1024 | |
| 1025 | /* Drop the temporary PENDING lock */ |
| 1026 | lock.l_start = PENDING_BYTE; |
| 1027 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1028 | lock.l_type = F_UNLCK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1029 | fcntl(id->h, F_SETLK, &lock); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1030 | if( s ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1031 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 1032 | }else{ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1033 | id->locktype = SHARED_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1034 | id->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1035 | pLock->cnt = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1036 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1037 | }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){ |
| 1038 | /* We are trying for an exclusive lock but another thread in this |
| 1039 | ** same process is still holding a shared lock. */ |
| 1040 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1041 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1042 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1043 | ** assumed that there is a SHARED or greater lock on the file |
| 1044 | ** already. |
| 1045 | */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1046 | assert( 0!=id->locktype ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1047 | lock.l_type = F_WRLCK; |
| 1048 | switch( locktype ){ |
| 1049 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1050 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1051 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1052 | case EXCLUSIVE_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1053 | lock.l_start = SHARED_FIRST; |
| 1054 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | default: |
| 1057 | assert(0); |
| 1058 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1059 | s = fcntl(id->h, F_SETLK, &lock); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1060 | if( s ){ |
| 1061 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 1062 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1063 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1064 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1065 | if( rc==SQLITE_OK ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1066 | id->locktype = locktype; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1067 | pLock->locktype = locktype; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1068 | }else if( locktype==EXCLUSIVE_LOCK ){ |
| 1069 | id->locktype = PENDING_LOCK; |
| 1070 | pLock->locktype = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1071 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1072 | |
| 1073 | end_lock: |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1074 | sqlite3OsLeaveMutex(); |
drh | e29b915 | 2005-03-18 14:03:15 +0000 | [diff] [blame] | 1075 | TRACE4("LOCK %d %s %s\n", id->h, locktypeName(locktype), |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 1076 | rc==SQLITE_OK ? "ok" : "failed"); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1077 | return rc; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1081 | ** Lower the locking level on file descriptor id to locktype. locktype |
| 1082 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1083 | ** |
| 1084 | ** If the locking level of the file descriptor is already at or below |
| 1085 | ** the requested locking level, this routine is a no-op. |
| 1086 | ** |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1087 | ** It is not possible for this routine to fail if the second argument |
| 1088 | ** is NO_LOCK. If the second argument is SHARED_LOCK, this routine |
| 1089 | ** might return SQLITE_IOERR instead of SQLITE_OK. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1090 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1091 | int sqlite3OsUnlock(OsFile *id, int locktype){ |
| 1092 | struct lockInfo *pLock; |
| 1093 | struct flock lock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1094 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1095 | |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 1096 | assert( id->isOpen ); |
drh | e29b915 | 2005-03-18 14:03:15 +0000 | [diff] [blame] | 1097 | TRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", id->h, locktype, id->locktype, |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 1098 | id->pLock->locktype, id->pLock->cnt, getpid()); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1099 | |
| 1100 | assert( locktype<=SHARED_LOCK ); |
| 1101 | if( id->locktype<=locktype ){ |
| 1102 | return SQLITE_OK; |
| 1103 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1104 | sqlite3OsEnterMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1105 | pLock = id->pLock; |
| 1106 | assert( pLock->cnt!=0 ); |
| 1107 | if( id->locktype>SHARED_LOCK ){ |
| 1108 | assert( pLock->locktype==id->locktype ); |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1109 | if( locktype==SHARED_LOCK ){ |
| 1110 | lock.l_type = F_RDLCK; |
| 1111 | lock.l_whence = SEEK_SET; |
| 1112 | lock.l_start = SHARED_FIRST; |
| 1113 | lock.l_len = SHARED_SIZE; |
| 1114 | if( fcntl(id->h, F_SETLK, &lock)!=0 ){ |
| 1115 | /* This should never happen */ |
| 1116 | rc = SQLITE_IOERR; |
| 1117 | } |
| 1118 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1119 | lock.l_type = F_UNLCK; |
| 1120 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1121 | lock.l_start = PENDING_BYTE; |
| 1122 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
| 1123 | fcntl(id->h, F_SETLK, &lock); |
| 1124 | pLock->locktype = SHARED_LOCK; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1125 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1126 | if( locktype==NO_LOCK ){ |
| 1127 | struct openCnt *pOpen; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1128 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1129 | /* Decrement the shared lock counter. Release the lock using an |
| 1130 | ** OS call only when all threads in this same process have released |
| 1131 | ** the lock. |
| 1132 | */ |
| 1133 | pLock->cnt--; |
| 1134 | if( pLock->cnt==0 ){ |
| 1135 | lock.l_type = F_UNLCK; |
| 1136 | lock.l_whence = SEEK_SET; |
| 1137 | lock.l_start = lock.l_len = 0L; |
| 1138 | fcntl(id->h, F_SETLK, &lock); |
| 1139 | pLock->locktype = NO_LOCK; |
| 1140 | } |
| 1141 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1142 | /* Decrement the count of locks against this same file. When the |
| 1143 | ** count reaches zero, close any other file descriptors whose close |
| 1144 | ** was deferred because of outstanding locks. |
| 1145 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1146 | pOpen = id->pOpen; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1147 | pOpen->nLock--; |
| 1148 | assert( pOpen->nLock>=0 ); |
| 1149 | if( pOpen->nLock==0 && pOpen->nPending>0 ){ |
| 1150 | int i; |
| 1151 | for(i=0; i<pOpen->nPending; i++){ |
| 1152 | close(pOpen->aPending[i]); |
| 1153 | } |
| 1154 | sqliteFree(pOpen->aPending); |
| 1155 | pOpen->nPending = 0; |
| 1156 | pOpen->aPending = 0; |
| 1157 | } |
| 1158 | } |
| 1159 | sqlite3OsLeaveMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1160 | id->locktype = locktype; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1161 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1165 | ** Close a file. |
| 1166 | */ |
| 1167 | int sqlite3OsClose(OsFile *id){ |
| 1168 | if( !id->isOpen ) return SQLITE_OK; |
| 1169 | sqlite3OsUnlock(id, NO_LOCK); |
| 1170 | if( id->dirfd>=0 ) close(id->dirfd); |
| 1171 | id->dirfd = -1; |
| 1172 | sqlite3OsEnterMutex(); |
| 1173 | if( id->pOpen->nLock ){ |
| 1174 | /* If there are outstanding locks, do not actually close the file just |
| 1175 | ** yet because that would clear those locks. Instead, add the file |
| 1176 | ** descriptor to pOpen->aPending. It will be automatically closed when |
| 1177 | ** the last lock is cleared. |
| 1178 | */ |
| 1179 | int *aNew; |
| 1180 | struct openCnt *pOpen = id->pOpen; |
| 1181 | pOpen->nPending++; |
| 1182 | aNew = sqliteRealloc( pOpen->aPending, pOpen->nPending*sizeof(int) ); |
| 1183 | if( aNew==0 ){ |
| 1184 | /* If a malloc fails, just leak the file descriptor */ |
| 1185 | }else{ |
| 1186 | pOpen->aPending = aNew; |
| 1187 | pOpen->aPending[pOpen->nPending-1] = id->h; |
| 1188 | } |
| 1189 | }else{ |
| 1190 | /* There are no outstanding locks so we can close the file immediately */ |
| 1191 | close(id->h); |
| 1192 | } |
| 1193 | releaseLockInfo(id->pLock); |
| 1194 | releaseOpenCnt(id->pOpen); |
| 1195 | sqlite3OsLeaveMutex(); |
| 1196 | id->isOpen = 0; |
| 1197 | TRACE2("CLOSE %-3d\n", id->h); |
| 1198 | OpenCounter(-1); |
| 1199 | return SQLITE_OK; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1203 | ** Get information to seed the random number generator. The seed |
| 1204 | ** is written into the buffer zBuf[256]. The calling function must |
| 1205 | ** supply a sufficiently large buffer. |
| 1206 | */ |
| 1207 | int sqlite3OsRandomSeed(char *zBuf){ |
| 1208 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 1209 | ** errors. The reports issued by valgrind are incorrect - we would |
| 1210 | ** prefer that the randomness be increased by making use of the |
| 1211 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 1212 | ** some users. Rather than argue, it seems easier just to initialize |
| 1213 | ** the whole array and silence valgrind, even if that means less randomness |
| 1214 | ** in the random seed. |
| 1215 | ** |
| 1216 | ** When testing, initializing zBuf[] to zero is all we do. That means |
| 1217 | ** that we always use the same random number sequence.* This makes the |
| 1218 | ** tests repeatable. |
| 1219 | */ |
| 1220 | memset(zBuf, 0, 256); |
| 1221 | #if !defined(SQLITE_TEST) |
| 1222 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 1223 | int pid, fd; |
| 1224 | fd = open("/dev/urandom", O_RDONLY); |
| 1225 | if( fd<0 ){ |
| 1226 | time((time_t*)zBuf); |
| 1227 | pid = getpid(); |
| 1228 | memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid)); |
| 1229 | }else{ |
| 1230 | read(fd, zBuf, 256); |
| 1231 | close(fd); |
| 1232 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1233 | } |
| 1234 | #endif |
| 1235 | return SQLITE_OK; |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | ** Sleep for a little while. Return the amount of time slept. |
| 1240 | */ |
| 1241 | int sqlite3OsSleep(int ms){ |
| 1242 | #if defined(HAVE_USLEEP) && HAVE_USLEEP |
| 1243 | usleep(ms*1000); |
| 1244 | return ms; |
| 1245 | #else |
| 1246 | sleep((ms+999)/1000); |
| 1247 | return 1000*((ms+999)/1000); |
| 1248 | #endif |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | ** Static variables used for thread synchronization |
| 1253 | */ |
| 1254 | static int inMutex = 0; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1255 | #ifdef SQLITE_UNIX_THREADS |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1256 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1257 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1258 | |
| 1259 | /* |
| 1260 | ** The following pair of routine implement mutual exclusion for |
| 1261 | ** multi-threaded processes. Only a single thread is allowed to |
| 1262 | ** executed code that is surrounded by EnterMutex() and LeaveMutex(). |
| 1263 | ** |
| 1264 | ** SQLite uses only a single Mutex. There is not much critical |
| 1265 | ** code and what little there is executes quickly and without blocking. |
| 1266 | */ |
| 1267 | void sqlite3OsEnterMutex(){ |
| 1268 | #ifdef SQLITE_UNIX_THREADS |
| 1269 | pthread_mutex_lock(&mutex); |
| 1270 | #endif |
| 1271 | assert( !inMutex ); |
| 1272 | inMutex = 1; |
| 1273 | } |
| 1274 | void sqlite3OsLeaveMutex(){ |
| 1275 | assert( inMutex ); |
| 1276 | inMutex = 0; |
| 1277 | #ifdef SQLITE_UNIX_THREADS |
| 1278 | pthread_mutex_unlock(&mutex); |
| 1279 | #endif |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | ** Turn a relative pathname into a full pathname. Return a pointer |
| 1284 | ** to the full pathname stored in space obtained from sqliteMalloc(). |
| 1285 | ** The calling function is responsible for freeing this space once it |
| 1286 | ** is no longer needed. |
| 1287 | */ |
| 1288 | char *sqlite3OsFullPathname(const char *zRelative){ |
| 1289 | char *zFull = 0; |
| 1290 | if( zRelative[0]=='/' ){ |
| 1291 | sqlite3SetString(&zFull, zRelative, (char*)0); |
| 1292 | }else{ |
| 1293 | char zBuf[5000]; |
drh | 41f5852 | 2005-06-06 15:06:39 +0000 | [diff] [blame] | 1294 | zBuf[0] = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1295 | sqlite3SetString(&zFull, getcwd(zBuf, sizeof(zBuf)), "/", zRelative, |
| 1296 | (char*)0); |
| 1297 | } |
| 1298 | return zFull; |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | ** The following variable, if set to a non-zero value, becomes the result |
| 1303 | ** returned from sqlite3OsCurrentTime(). This is used for testing. |
| 1304 | */ |
| 1305 | #ifdef SQLITE_TEST |
| 1306 | int sqlite3_current_time = 0; |
| 1307 | #endif |
| 1308 | |
| 1309 | /* |
| 1310 | ** Find the current time (in Universal Coordinated Time). Write the |
| 1311 | ** current time and date as a Julian Day number into *prNow and |
| 1312 | ** return 0. Return 1 if the time and date cannot be found. |
| 1313 | */ |
| 1314 | int sqlite3OsCurrentTime(double *prNow){ |
| 1315 | time_t t; |
| 1316 | time(&t); |
| 1317 | *prNow = t/86400.0 + 2440587.5; |
| 1318 | #ifdef SQLITE_TEST |
| 1319 | if( sqlite3_current_time ){ |
| 1320 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 1321 | } |
| 1322 | #endif |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
drh | fd69dd6 | 2004-06-29 11:08:19 +0000 | [diff] [blame] | 1326 | #if 0 /* NOT USED */ |
drh | bf9a7e4 | 2004-06-15 00:29:03 +0000 | [diff] [blame] | 1327 | /* |
| 1328 | ** Find the time that the file was last modified. Write the |
| 1329 | ** modification time and date as a Julian Day number into *prNow and |
| 1330 | ** return SQLITE_OK. Return SQLITE_ERROR if the modification |
| 1331 | ** time cannot be found. |
| 1332 | */ |
| 1333 | int sqlite3OsFileModTime(OsFile *id, double *prNow){ |
| 1334 | int rc; |
| 1335 | struct stat statbuf; |
| 1336 | if( fstat(id->h, &statbuf)==0 ){ |
| 1337 | *prNow = statbuf.st_mtime/86400.0 + 2440587.5; |
| 1338 | rc = SQLITE_OK; |
| 1339 | }else{ |
| 1340 | rc = SQLITE_ERROR; |
| 1341 | } |
| 1342 | return rc; |
| 1343 | } |
drh | fd69dd6 | 2004-06-29 11:08:19 +0000 | [diff] [blame] | 1344 | #endif /* NOT USED */ |
drh | bf9a7e4 | 2004-06-15 00:29:03 +0000 | [diff] [blame] | 1345 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1346 | #endif /* OS_UNIX */ |