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