os/persistentdata/persistentstorage/sqlite3api/SQLite/os_unix.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/SQLite/os_unix.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,2987 @@
     1.4 +/*
     1.5 +** 2004 May 22
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +******************************************************************************
    1.15 +**
    1.16 +** This file contains code that is specific to Unix systems.
    1.17 +**
    1.18 +** $Id: os_unix.c,v 1.204 2008/09/24 09:12:47 danielk1977 Exp $
    1.19 +*/
    1.20 +#include "sqliteInt.h"
    1.21 +#if SQLITE_OS_UNIX              /* This file is used on unix only */
    1.22 +
    1.23 +/*
    1.24 +** If SQLITE_ENABLE_LOCKING_STYLE is defined and is non-zero, then several
    1.25 +** alternative locking implementations are provided:
    1.26 +**
    1.27 +**   * POSIX locking (the default),
    1.28 +**   * No locking,
    1.29 +**   * Dot-file locking,
    1.30 +**   * flock() locking,
    1.31 +**   * AFP locking (OSX only).
    1.32 +**
    1.33 +** SQLITE_ENABLE_LOCKING_STYLE only works on a Mac. It is turned on by
    1.34 +** default on a Mac and disabled on all other posix platforms.
    1.35 +*/
    1.36 +#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
    1.37 +#  if defined(__DARWIN__)
    1.38 +#    define SQLITE_ENABLE_LOCKING_STYLE 1
    1.39 +#  else
    1.40 +#    define SQLITE_ENABLE_LOCKING_STYLE 0
    1.41 +#  endif
    1.42 +#endif
    1.43 +
    1.44 +/*
    1.45 +** These #defines should enable >2GB file support on Posix if the
    1.46 +** underlying operating system supports it.  If the OS lacks
    1.47 +** large file support, these should be no-ops.
    1.48 +**
    1.49 +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
    1.50 +** on the compiler command line.  This is necessary if you are compiling
    1.51 +** on a recent machine (ex: RedHat 7.2) but you want your code to work
    1.52 +** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
    1.53 +** without this option, LFS is enable.  But LFS does not exist in the kernel
    1.54 +** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
    1.55 +** portability you should omit LFS.
    1.56 +*/
    1.57 +#ifndef SQLITE_DISABLE_LFS
    1.58 +# define _LARGE_FILE       1
    1.59 +# ifndef _FILE_OFFSET_BITS
    1.60 +#   define _FILE_OFFSET_BITS 64
    1.61 +# endif
    1.62 +# define _LARGEFILE_SOURCE 1
    1.63 +#endif
    1.64 +
    1.65 +/*
    1.66 +** standard include files.
    1.67 +*/
    1.68 +#include <sys/types.h>
    1.69 +#include <sys/stat.h>
    1.70 +#include <fcntl.h>
    1.71 +#include <unistd.h>
    1.72 +#include <time.h>
    1.73 +#include <sys/time.h>
    1.74 +#include <errno.h>
    1.75 +
    1.76 +#if SQLITE_ENABLE_LOCKING_STYLE
    1.77 +#include <sys/ioctl.h>
    1.78 +#include <sys/param.h>
    1.79 +#include <sys/mount.h>
    1.80 +#endif /* SQLITE_ENABLE_LOCKING_STYLE */
    1.81 +
    1.82 +/*
    1.83 +** If we are to be thread-safe, include the pthreads header and define
    1.84 +** the SQLITE_UNIX_THREADS macro.
    1.85 +*/
    1.86 +#if SQLITE_THREADSAFE
    1.87 +# include <pthread.h>
    1.88 +# define SQLITE_UNIX_THREADS 1
    1.89 +#endif
    1.90 +
    1.91 +/*
    1.92 +** Default permissions when creating a new file
    1.93 +*/
    1.94 +#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
    1.95 +# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
    1.96 +#endif
    1.97 +
    1.98 +/*
    1.99 +** Maximum supported path-length.
   1.100 +*/
   1.101 +#define MAX_PATHNAME 512
   1.102 +
   1.103 +
   1.104 +/*
   1.105 +** The unixFile structure is subclass of sqlite3_file specific for the unix
   1.106 +** protability layer.
   1.107 +*/
   1.108 +typedef struct unixFile unixFile;
   1.109 +struct unixFile {
   1.110 +  sqlite3_io_methods const *pMethod;  /* Always the first entry */
   1.111 +#ifdef SQLITE_TEST
   1.112 +  /* In test mode, increase the size of this structure a bit so that 
   1.113 +  ** it is larger than the struct CrashFile defined in test6.c.
   1.114 +  */
   1.115 +  char aPadding[32];
   1.116 +#endif
   1.117 +  struct openCnt *pOpen;    /* Info about all open fd's on this inode */
   1.118 +  struct lockInfo *pLock;   /* Info about locks on this inode */
   1.119 +#if SQLITE_ENABLE_LOCKING_STYLE
   1.120 +  void *lockingContext;     /* Locking style specific state */
   1.121 +#endif
   1.122 +  int h;                    /* The file descriptor */
   1.123 +  unsigned char locktype;   /* The type of lock held on this fd */
   1.124 +  int dirfd;                /* File descriptor for the directory */
   1.125 +#if SQLITE_THREADSAFE
   1.126 +  pthread_t tid;            /* The thread that "owns" this unixFile */
   1.127 +#endif
   1.128 +  int lastErrno;            /* The unix errno from the last I/O error */
   1.129 +};
   1.130 +
   1.131 +/*
   1.132 +** Include code that is common to all os_*.c files
   1.133 +*/
   1.134 +#include "os_common.h"
   1.135 +
   1.136 +/*
   1.137 +** Define various macros that are missing from some systems.
   1.138 +*/
   1.139 +#ifndef O_LARGEFILE
   1.140 +# define O_LARGEFILE 0
   1.141 +#endif
   1.142 +#ifdef SQLITE_DISABLE_LFS
   1.143 +# undef O_LARGEFILE
   1.144 +# define O_LARGEFILE 0
   1.145 +#endif
   1.146 +#ifndef O_NOFOLLOW
   1.147 +# define O_NOFOLLOW 0
   1.148 +#endif
   1.149 +#ifndef O_BINARY
   1.150 +# define O_BINARY 0
   1.151 +#endif
   1.152 +
   1.153 +/*
   1.154 +** The DJGPP compiler environment looks mostly like Unix, but it
   1.155 +** lacks the fcntl() system call.  So redefine fcntl() to be something
   1.156 +** that always succeeds.  This means that locking does not occur under
   1.157 +** DJGPP.  But it is DOS - what did you expect?
   1.158 +*/
   1.159 +#ifdef __DJGPP__
   1.160 +# define fcntl(A,B,C) 0
   1.161 +#endif
   1.162 +
   1.163 +/*
   1.164 +** The threadid macro resolves to the thread-id or to 0.  Used for
   1.165 +** testing and debugging only.
   1.166 +*/
   1.167 +#if SQLITE_THREADSAFE
   1.168 +#define threadid pthread_self()
   1.169 +#else
   1.170 +#define threadid 0
   1.171 +#endif
   1.172 +
   1.173 +/*
   1.174 +** Set or check the unixFile.tid field.  This field is set when an unixFile
   1.175 +** is first opened.  All subsequent uses of the unixFile verify that the
   1.176 +** same thread is operating on the unixFile.  Some operating systems do
   1.177 +** not allow locks to be overridden by other threads and that restriction
   1.178 +** means that sqlite3* database handles cannot be moved from one thread
   1.179 +** to another.  This logic makes sure a user does not try to do that
   1.180 +** by mistake.
   1.181 +**
   1.182 +** Version 3.3.1 (2006-01-15):  unixFile can be moved from one thread to
   1.183 +** another as long as we are running on a system that supports threads
   1.184 +** overriding each others locks (which now the most common behavior)
   1.185 +** or if no locks are held.  But the unixFile.pLock field needs to be
   1.186 +** recomputed because its key includes the thread-id.  See the 
   1.187 +** transferOwnership() function below for additional information
   1.188 +*/
   1.189 +#if SQLITE_THREADSAFE
   1.190 +# define SET_THREADID(X)   (X)->tid = pthread_self()
   1.191 +# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
   1.192 +                            !pthread_equal((X)->tid, pthread_self()))
   1.193 +#else
   1.194 +# define SET_THREADID(X)
   1.195 +# define CHECK_THREADID(X) 0
   1.196 +#endif
   1.197 +
   1.198 +/*
   1.199 +** Here is the dirt on POSIX advisory locks:  ANSI STD 1003.1 (1996)
   1.200 +** section 6.5.2.2 lines 483 through 490 specify that when a process
   1.201 +** sets or clears a lock, that operation overrides any prior locks set
   1.202 +** by the same process.  It does not explicitly say so, but this implies
   1.203 +** that it overrides locks set by the same process using a different
   1.204 +** file descriptor.  Consider this test case:
   1.205 +**       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
   1.206 +**
   1.207 +** Suppose ./file1 and ./file2 are really the same file (because
   1.208 +** one is a hard or symbolic link to the other) then if you set
   1.209 +** an exclusive lock on fd1, then try to get an exclusive lock
   1.210 +** on fd2, it works.  I would have expected the second lock to
   1.211 +** fail since there was already a lock on the file due to fd1.
   1.212 +** But not so.  Since both locks came from the same process, the
   1.213 +** second overrides the first, even though they were on different
   1.214 +** file descriptors opened on different file names.
   1.215 +**
   1.216 +** Bummer.  If you ask me, this is broken.  Badly broken.  It means
   1.217 +** that we cannot use POSIX locks to synchronize file access among
   1.218 +** competing threads of the same process.  POSIX locks will work fine
   1.219 +** to synchronize access for threads in separate processes, but not
   1.220 +** threads within the same process.
   1.221 +**
   1.222 +** To work around the problem, SQLite has to manage file locks internally
   1.223 +** on its own.  Whenever a new database is opened, we have to find the
   1.224 +** specific inode of the database file (the inode is determined by the
   1.225 +** st_dev and st_ino fields of the stat structure that fstat() fills in)
   1.226 +** and check for locks already existing on that inode.  When locks are
   1.227 +** created or removed, we have to look at our own internal record of the
   1.228 +** locks to see if another thread has previously set a lock on that same
   1.229 +** inode.
   1.230 +**
   1.231 +** The sqlite3_file structure for POSIX is no longer just an integer file
   1.232 +** descriptor.  It is now a structure that holds the integer file
   1.233 +** descriptor and a pointer to a structure that describes the internal
   1.234 +** locks on the corresponding inode.  There is one locking structure
   1.235 +** per inode, so if the same inode is opened twice, both unixFile structures
   1.236 +** point to the same locking structure.  The locking structure keeps
   1.237 +** a reference count (so we will know when to delete it) and a "cnt"
   1.238 +** field that tells us its internal lock status.  cnt==0 means the
   1.239 +** file is unlocked.  cnt==-1 means the file has an exclusive lock.
   1.240 +** cnt>0 means there are cnt shared locks on the file.
   1.241 +**
   1.242 +** Any attempt to lock or unlock a file first checks the locking
   1.243 +** structure.  The fcntl() system call is only invoked to set a 
   1.244 +** POSIX lock if the internal lock structure transitions between
   1.245 +** a locked and an unlocked state.
   1.246 +**
   1.247 +** 2004-Jan-11:
   1.248 +** More recent discoveries about POSIX advisory locks.  (The more
   1.249 +** I discover, the more I realize the a POSIX advisory locks are
   1.250 +** an abomination.)
   1.251 +**
   1.252 +** If you close a file descriptor that points to a file that has locks,
   1.253 +** all locks on that file that are owned by the current process are
   1.254 +** released.  To work around this problem, each unixFile structure contains
   1.255 +** a pointer to an openCnt structure.  There is one openCnt structure
   1.256 +** per open inode, which means that multiple unixFile can point to a single
   1.257 +** openCnt.  When an attempt is made to close an unixFile, if there are
   1.258 +** other unixFile open on the same inode that are holding locks, the call
   1.259 +** to close() the file descriptor is deferred until all of the locks clear.
   1.260 +** The openCnt structure keeps a list of file descriptors that need to
   1.261 +** be closed and that list is walked (and cleared) when the last lock
   1.262 +** clears.
   1.263 +**
   1.264 +** First, under Linux threads, because each thread has a separate
   1.265 +** process ID, lock operations in one thread do not override locks
   1.266 +** to the same file in other threads.  Linux threads behave like
   1.267 +** separate processes in this respect.  But, if you close a file
   1.268 +** descriptor in linux threads, all locks are cleared, even locks
   1.269 +** on other threads and even though the other threads have different
   1.270 +** process IDs.  Linux threads is inconsistent in this respect.
   1.271 +** (I'm beginning to think that linux threads is an abomination too.)
   1.272 +** The consequence of this all is that the hash table for the lockInfo
   1.273 +** structure has to include the process id as part of its key because
   1.274 +** locks in different threads are treated as distinct.  But the 
   1.275 +** openCnt structure should not include the process id in its
   1.276 +** key because close() clears lock on all threads, not just the current
   1.277 +** thread.  Were it not for this goofiness in linux threads, we could
   1.278 +** combine the lockInfo and openCnt structures into a single structure.
   1.279 +**
   1.280 +** 2004-Jun-28:
   1.281 +** On some versions of linux, threads can override each others locks.
   1.282 +** On others not.  Sometimes you can change the behavior on the same
   1.283 +** system by setting the LD_ASSUME_KERNEL environment variable.  The
   1.284 +** POSIX standard is silent as to which behavior is correct, as far
   1.285 +** as I can tell, so other versions of unix might show the same
   1.286 +** inconsistency.  There is no little doubt in my mind that posix
   1.287 +** advisory locks and linux threads are profoundly broken.
   1.288 +**
   1.289 +** To work around the inconsistencies, we have to test at runtime 
   1.290 +** whether or not threads can override each others locks.  This test
   1.291 +** is run once, the first time any lock is attempted.  A static 
   1.292 +** variable is set to record the results of this test for future
   1.293 +** use.
   1.294 +*/
   1.295 +
   1.296 +/*
   1.297 +** An instance of the following structure serves as the key used
   1.298 +** to locate a particular lockInfo structure given its inode.
   1.299 +**
   1.300 +** If threads cannot override each others locks, then we set the
   1.301 +** lockKey.tid field to the thread ID.  If threads can override
   1.302 +** each others locks then tid is always set to zero.  tid is omitted
   1.303 +** if we compile without threading support.
   1.304 +*/
   1.305 +struct lockKey {
   1.306 +  dev_t dev;       /* Device number */
   1.307 +  ino_t ino;       /* Inode number */
   1.308 +#if SQLITE_THREADSAFE
   1.309 +  pthread_t tid;   /* Thread ID or zero if threads can override each other */
   1.310 +#endif
   1.311 +};
   1.312 +
   1.313 +/*
   1.314 +** An instance of the following structure is allocated for each open
   1.315 +** inode on each thread with a different process ID.  (Threads have
   1.316 +** different process IDs on linux, but not on most other unixes.)
   1.317 +**
   1.318 +** A single inode can have multiple file descriptors, so each unixFile
   1.319 +** structure contains a pointer to an instance of this object and this
   1.320 +** object keeps a count of the number of unixFile pointing to it.
   1.321 +*/
   1.322 +struct lockInfo {
   1.323 +  struct lockKey key;  /* The lookup key */
   1.324 +  int cnt;             /* Number of SHARED locks held */
   1.325 +  int locktype;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
   1.326 +  int nRef;            /* Number of pointers to this structure */
   1.327 +  struct lockInfo *pNext, *pPrev;   /* List of all lockInfo objects */
   1.328 +};
   1.329 +
   1.330 +/*
   1.331 +** An instance of the following structure serves as the key used
   1.332 +** to locate a particular openCnt structure given its inode.  This
   1.333 +** is the same as the lockKey except that the thread ID is omitted.
   1.334 +*/
   1.335 +struct openKey {
   1.336 +  dev_t dev;   /* Device number */
   1.337 +  ino_t ino;   /* Inode number */
   1.338 +};
   1.339 +
   1.340 +/*
   1.341 +** An instance of the following structure is allocated for each open
   1.342 +** inode.  This structure keeps track of the number of locks on that
   1.343 +** inode.  If a close is attempted against an inode that is holding
   1.344 +** locks, the close is deferred until all locks clear by adding the
   1.345 +** file descriptor to be closed to the pending list.
   1.346 +*/
   1.347 +struct openCnt {
   1.348 +  struct openKey key;   /* The lookup key */
   1.349 +  int nRef;             /* Number of pointers to this structure */
   1.350 +  int nLock;            /* Number of outstanding locks */
   1.351 +  int nPending;         /* Number of pending close() operations */
   1.352 +  int *aPending;        /* Malloced space holding fd's awaiting a close() */
   1.353 +  struct openCnt *pNext, *pPrev;   /* List of all openCnt objects */
   1.354 +};
   1.355 +
   1.356 +/*
   1.357 +** List of all lockInfo and openCnt objects.  This used to be a hash
   1.358 +** table.  But the number of objects is rarely more than a dozen and
   1.359 +** never exceeds a few thousand.  And lookup is not on a critical
   1.360 +** path oo a simple linked list will suffice.
   1.361 +*/
   1.362 +static struct lockInfo *lockList = 0;
   1.363 +static struct openCnt *openList = 0;
   1.364 +
   1.365 +/*
   1.366 +** The locking styles are associated with the different file locking
   1.367 +** capabilities supported by different file systems.  
   1.368 +**
   1.369 +** POSIX locking style fully supports shared and exclusive byte-range locks 
   1.370 +** AFP locking only supports exclusive byte-range locks
   1.371 +** FLOCK only supports a single file-global exclusive lock
   1.372 +** DOTLOCK isn't a true locking style, it refers to the use of a special
   1.373 +**   file named the same as the database file with a '.lock' extension, this
   1.374 +**   can be used on file systems that do not offer any reliable file locking
   1.375 +** NO locking means that no locking will be attempted, this is only used for
   1.376 +**   read-only file systems currently
   1.377 +** UNSUPPORTED means that no locking will be attempted, this is only used for
   1.378 +**   file systems that are known to be unsupported
   1.379 +*/
   1.380 +#define LOCKING_STYLE_POSIX        1
   1.381 +#define LOCKING_STYLE_NONE         2
   1.382 +#define LOCKING_STYLE_DOTFILE      3
   1.383 +#define LOCKING_STYLE_FLOCK        4
   1.384 +#define LOCKING_STYLE_AFP          5
   1.385 +
   1.386 +/*
   1.387 +** Only set the lastErrno if the error code is a real error and not 
   1.388 +** a normal expected return code of SQLITE_BUSY or SQLITE_OK
   1.389 +*/
   1.390 +#define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
   1.391 +
   1.392 +/*
   1.393 +** Helper functions to obtain and relinquish the global mutex.
   1.394 +*/
   1.395 +static void enterMutex(void){
   1.396 +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   1.397 +}
   1.398 +static void leaveMutex(void){
   1.399 +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   1.400 +}
   1.401 +
   1.402 +#if SQLITE_THREADSAFE
   1.403 +/*
   1.404 +** This variable records whether or not threads can override each others
   1.405 +** locks.
   1.406 +**
   1.407 +**    0:  No.  Threads cannot override each others locks.
   1.408 +**    1:  Yes.  Threads can override each others locks.
   1.409 +**   -1:  We don't know yet.
   1.410 +**
   1.411 +** On some systems, we know at compile-time if threads can override each
   1.412 +** others locks.  On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
   1.413 +** will be set appropriately.  On other systems, we have to check at
   1.414 +** runtime.  On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
   1.415 +** undefined.
   1.416 +**
   1.417 +** This variable normally has file scope only.  But during testing, we make
   1.418 +** it a global so that the test code can change its value in order to verify
   1.419 +** that the right stuff happens in either case.
   1.420 +*/
   1.421 +#ifndef SQLITE_THREAD_OVERRIDE_LOCK
   1.422 +# define SQLITE_THREAD_OVERRIDE_LOCK -1
   1.423 +#endif
   1.424 +#ifdef SQLITE_TEST
   1.425 +int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
   1.426 +#else
   1.427 +static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
   1.428 +#endif
   1.429 +
   1.430 +/*
   1.431 +** This structure holds information passed into individual test
   1.432 +** threads by the testThreadLockingBehavior() routine.
   1.433 +*/
   1.434 +struct threadTestData {
   1.435 +  int fd;                /* File to be locked */
   1.436 +  struct flock lock;     /* The locking operation */
   1.437 +  int result;            /* Result of the locking operation */
   1.438 +};
   1.439 +
   1.440 +#ifdef SQLITE_LOCK_TRACE
   1.441 +/*
   1.442 +** Print out information about all locking operations.
   1.443 +**
   1.444 +** This routine is used for troubleshooting locks on multithreaded
   1.445 +** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
   1.446 +** command-line option on the compiler.  This code is normally
   1.447 +** turned off.
   1.448 +*/
   1.449 +static int lockTrace(int fd, int op, struct flock *p){
   1.450 +  char *zOpName, *zType;
   1.451 +  int s;
   1.452 +  int savedErrno;
   1.453 +  if( op==F_GETLK ){
   1.454 +    zOpName = "GETLK";
   1.455 +  }else if( op==F_SETLK ){
   1.456 +    zOpName = "SETLK";
   1.457 +  }else{
   1.458 +    s = fcntl(fd, op, p);
   1.459 +    sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
   1.460 +    return s;
   1.461 +  }
   1.462 +  if( p->l_type==F_RDLCK ){
   1.463 +    zType = "RDLCK";
   1.464 +  }else if( p->l_type==F_WRLCK ){
   1.465 +    zType = "WRLCK";
   1.466 +  }else if( p->l_type==F_UNLCK ){
   1.467 +    zType = "UNLCK";
   1.468 +  }else{
   1.469 +    assert( 0 );
   1.470 +  }
   1.471 +  assert( p->l_whence==SEEK_SET );
   1.472 +  s = fcntl(fd, op, p);
   1.473 +  savedErrno = errno;
   1.474 +  sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
   1.475 +     threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
   1.476 +     (int)p->l_pid, s);
   1.477 +  if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
   1.478 +    struct flock l2;
   1.479 +    l2 = *p;
   1.480 +    fcntl(fd, F_GETLK, &l2);
   1.481 +    if( l2.l_type==F_RDLCK ){
   1.482 +      zType = "RDLCK";
   1.483 +    }else if( l2.l_type==F_WRLCK ){
   1.484 +      zType = "WRLCK";
   1.485 +    }else if( l2.l_type==F_UNLCK ){
   1.486 +      zType = "UNLCK";
   1.487 +    }else{
   1.488 +      assert( 0 );
   1.489 +    }
   1.490 +    sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
   1.491 +       zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
   1.492 +  }
   1.493 +  errno = savedErrno;
   1.494 +  return s;
   1.495 +}
   1.496 +#define fcntl lockTrace
   1.497 +#endif /* SQLITE_LOCK_TRACE */
   1.498 +
   1.499 +/*
   1.500 +** The testThreadLockingBehavior() routine launches two separate
   1.501 +** threads on this routine.  This routine attempts to lock a file
   1.502 +** descriptor then returns.  The success or failure of that attempt
   1.503 +** allows the testThreadLockingBehavior() procedure to determine
   1.504 +** whether or not threads can override each others locks.
   1.505 +*/
   1.506 +static void *threadLockingTest(void *pArg){
   1.507 +  struct threadTestData *pData = (struct threadTestData*)pArg;
   1.508 +  pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
   1.509 +  return pArg;
   1.510 +}
   1.511 +
   1.512 +/*
   1.513 +** This procedure attempts to determine whether or not threads
   1.514 +** can override each others locks then sets the 
   1.515 +** threadsOverrideEachOthersLocks variable appropriately.
   1.516 +*/
   1.517 +static void testThreadLockingBehavior(int fd_orig){
   1.518 +  int fd;
   1.519 +  struct threadTestData d[2];
   1.520 +  pthread_t t[2];
   1.521 +
   1.522 +  fd = dup(fd_orig);
   1.523 +  if( fd<0 ) return;
   1.524 +  memset(d, 0, sizeof(d));
   1.525 +  d[0].fd = fd;
   1.526 +  d[0].lock.l_type = F_RDLCK;
   1.527 +  d[0].lock.l_len = 1;
   1.528 +  d[0].lock.l_start = 0;
   1.529 +  d[0].lock.l_whence = SEEK_SET;
   1.530 +  d[1] = d[0];
   1.531 +  d[1].lock.l_type = F_WRLCK;
   1.532 +  pthread_create(&t[0], 0, threadLockingTest, &d[0]);
   1.533 +  pthread_create(&t[1], 0, threadLockingTest, &d[1]);
   1.534 +  pthread_join(t[0], 0);
   1.535 +  pthread_join(t[1], 0);
   1.536 +  close(fd);
   1.537 +  threadsOverrideEachOthersLocks =  d[0].result==0 && d[1].result==0;
   1.538 +}
   1.539 +#endif /* SQLITE_THREADSAFE */
   1.540 +
   1.541 +/*
   1.542 +** Release a lockInfo structure previously allocated by findLockInfo().
   1.543 +*/
   1.544 +static void releaseLockInfo(struct lockInfo *pLock){
   1.545 +  if( pLock ){
   1.546 +    pLock->nRef--;
   1.547 +    if( pLock->nRef==0 ){
   1.548 +      if( pLock->pPrev ){
   1.549 +        assert( pLock->pPrev->pNext==pLock );
   1.550 +        pLock->pPrev->pNext = pLock->pNext;
   1.551 +      }else{
   1.552 +        assert( lockList==pLock );
   1.553 +        lockList = pLock->pNext;
   1.554 +      }
   1.555 +      if( pLock->pNext ){
   1.556 +        assert( pLock->pNext->pPrev==pLock );
   1.557 +        pLock->pNext->pPrev = pLock->pPrev;
   1.558 +      }
   1.559 +      sqlite3_free(pLock);
   1.560 +    }
   1.561 +  }
   1.562 +}
   1.563 +
   1.564 +/*
   1.565 +** Release a openCnt structure previously allocated by findLockInfo().
   1.566 +*/
   1.567 +static void releaseOpenCnt(struct openCnt *pOpen){
   1.568 +  if( pOpen ){
   1.569 +    pOpen->nRef--;
   1.570 +    if( pOpen->nRef==0 ){
   1.571 +      if( pOpen->pPrev ){
   1.572 +        assert( pOpen->pPrev->pNext==pOpen );
   1.573 +        pOpen->pPrev->pNext = pOpen->pNext;
   1.574 +      }else{
   1.575 +        assert( openList==pOpen );
   1.576 +        openList = pOpen->pNext;
   1.577 +      }
   1.578 +      if( pOpen->pNext ){
   1.579 +        assert( pOpen->pNext->pPrev==pOpen );
   1.580 +        pOpen->pNext->pPrev = pOpen->pPrev;
   1.581 +      }
   1.582 +      sqlite3_free(pOpen->aPending);
   1.583 +      sqlite3_free(pOpen);
   1.584 +    }
   1.585 +  }
   1.586 +}
   1.587 +
   1.588 +#if SQLITE_ENABLE_LOCKING_STYLE
   1.589 +/*
   1.590 +** Tests a byte-range locking query to see if byte range locks are 
   1.591 +** supported, if not we fall back to dotlockLockingStyle.
   1.592 +*/
   1.593 +static int testLockingStyle(int fd){
   1.594 +  struct flock lockInfo;
   1.595 +
   1.596 +  /* Test byte-range lock using fcntl(). If the call succeeds, 
   1.597 +  ** assume that the file-system supports POSIX style locks. 
   1.598 +  */
   1.599 +  lockInfo.l_len = 1;
   1.600 +  lockInfo.l_start = 0;
   1.601 +  lockInfo.l_whence = SEEK_SET;
   1.602 +  lockInfo.l_type = F_RDLCK;
   1.603 +  if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
   1.604 +    return LOCKING_STYLE_POSIX;
   1.605 +  }
   1.606 +  
   1.607 +  /* Testing for flock() can give false positives.  So if if the above 
   1.608 +  ** test fails, then we fall back to using dot-file style locking.
   1.609 +  */  
   1.610 +  return LOCKING_STYLE_DOTFILE;
   1.611 +}
   1.612 +#endif
   1.613 +
   1.614 +/* 
   1.615 +** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the 
   1.616 +** f_fstypename entry in the statfs structure as returned by stat() for 
   1.617 +** the file system hosting the database file and selects  the appropriate
   1.618 +** locking style based on its value.  These values and assignments are 
   1.619 +** based on Darwin/OSX behavior and have not been thoroughly tested on 
   1.620 +** other systems.
   1.621 +**
   1.622 +** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
   1.623 +** returns LOCKING_STYLE_POSIX.
   1.624 +*/
   1.625 +static int detectLockingStyle(
   1.626 +  sqlite3_vfs *pVfs,
   1.627 +  const char *filePath, 
   1.628 +  int fd
   1.629 +){
   1.630 +#if SQLITE_ENABLE_LOCKING_STYLE
   1.631 +  struct Mapping {
   1.632 +    const char *zFilesystem;
   1.633 +    int eLockingStyle;
   1.634 +  } aMap[] = {
   1.635 +    { "hfs",    LOCKING_STYLE_POSIX },
   1.636 +    { "ufs",    LOCKING_STYLE_POSIX },
   1.637 +    { "afpfs",  LOCKING_STYLE_AFP },
   1.638 +#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
   1.639 +    { "smbfs",  LOCKING_STYLE_AFP },
   1.640 +#else
   1.641 +    { "smbfs",  LOCKING_STYLE_FLOCK },
   1.642 +#endif
   1.643 +    { "msdos",  LOCKING_STYLE_DOTFILE },
   1.644 +    { "webdav", LOCKING_STYLE_NONE },
   1.645 +    { 0, 0 }
   1.646 +  };
   1.647 +  int i;
   1.648 +  struct statfs fsInfo;
   1.649 +
   1.650 +  if( !filePath ){
   1.651 +    return LOCKING_STYLE_NONE;
   1.652 +  }
   1.653 +  if( pVfs->pAppData ){
   1.654 +    return SQLITE_PTR_TO_INT(pVfs->pAppData);
   1.655 +  }
   1.656 +
   1.657 +  if( statfs(filePath, &fsInfo) != -1 ){
   1.658 +    if( fsInfo.f_flags & MNT_RDONLY ){
   1.659 +      return LOCKING_STYLE_NONE;
   1.660 +    }
   1.661 +    for(i=0; aMap[i].zFilesystem; i++){
   1.662 +      if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
   1.663 +        return aMap[i].eLockingStyle;
   1.664 +      }
   1.665 +    }
   1.666 +  }
   1.667 +
   1.668 +  /* Default case. Handles, amongst others, "nfs". */
   1.669 +  return testLockingStyle(fd);  
   1.670 +#endif
   1.671 +  return LOCKING_STYLE_POSIX;
   1.672 +}
   1.673 +
   1.674 +/*
   1.675 +** Given a file descriptor, locate lockInfo and openCnt structures that
   1.676 +** describes that file descriptor.  Create new ones if necessary.  The
   1.677 +** return values might be uninitialized if an error occurs.
   1.678 +**
   1.679 +** Return an appropriate error code.
   1.680 +*/
   1.681 +static int findLockInfo(
   1.682 +  int fd,                      /* The file descriptor used in the key */
   1.683 +  struct lockInfo **ppLock,    /* Return the lockInfo structure here */
   1.684 +  struct openCnt **ppOpen      /* Return the openCnt structure here */
   1.685 +){
   1.686 +  int rc;
   1.687 +  struct lockKey key1;
   1.688 +  struct openKey key2;
   1.689 +  struct stat statbuf;
   1.690 +  struct lockInfo *pLock;
   1.691 +  struct openCnt *pOpen;
   1.692 +  rc = fstat(fd, &statbuf);
   1.693 +  if( rc!=0 ){
   1.694 +#ifdef EOVERFLOW
   1.695 +    if( errno==EOVERFLOW ) return SQLITE_NOLFS;
   1.696 +#endif
   1.697 +    return SQLITE_IOERR;
   1.698 +  }
   1.699 +
   1.700 +  /* On OS X on an msdos filesystem, the inode number is reported
   1.701 +  ** incorrectly for zero-size files.  See ticket #3260.  To work
   1.702 +  ** around this problem (we consider it a bug in OS X, not SQLite)
   1.703 +  ** we always increase the file size to 1 by writing a single byte
   1.704 +  ** prior to accessing the inode number.  The one byte written is
   1.705 +  ** an ASCII 'S' character which also happens to be the first byte
   1.706 +  ** in the header of every SQLite database.  In this way, if there
   1.707 +  ** is a race condition such that another thread has already populated
   1.708 +  ** the first page of the database, no damage is done.
   1.709 +  */
   1.710 +  if( statbuf.st_size==0 ){
   1.711 +    write(fd, "S", 1);
   1.712 +    rc = fstat(fd, &statbuf);
   1.713 +    if( rc!=0 ){
   1.714 +      return SQLITE_IOERR;
   1.715 +    }
   1.716 +  }
   1.717 +
   1.718 +  memset(&key1, 0, sizeof(key1));
   1.719 +  key1.dev = statbuf.st_dev;
   1.720 +  key1.ino = statbuf.st_ino;
   1.721 +#if SQLITE_THREADSAFE
   1.722 +  if( threadsOverrideEachOthersLocks<0 ){
   1.723 +    testThreadLockingBehavior(fd);
   1.724 +  }
   1.725 +  key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
   1.726 +#endif
   1.727 +  memset(&key2, 0, sizeof(key2));
   1.728 +  key2.dev = statbuf.st_dev;
   1.729 +  key2.ino = statbuf.st_ino;
   1.730 +  pLock = lockList;
   1.731 +  while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
   1.732 +    pLock = pLock->pNext;
   1.733 +  }
   1.734 +  if( pLock==0 ){
   1.735 +    pLock = sqlite3_malloc( sizeof(*pLock) );
   1.736 +    if( pLock==0 ){
   1.737 +      rc = SQLITE_NOMEM;
   1.738 +      goto exit_findlockinfo;
   1.739 +    }
   1.740 +    pLock->key = key1;
   1.741 +    pLock->nRef = 1;
   1.742 +    pLock->cnt = 0;
   1.743 +    pLock->locktype = 0;
   1.744 +    pLock->pNext = lockList;
   1.745 +    pLock->pPrev = 0;
   1.746 +    if( lockList ) lockList->pPrev = pLock;
   1.747 +    lockList = pLock;
   1.748 +  }else{
   1.749 +    pLock->nRef++;
   1.750 +  }
   1.751 +  *ppLock = pLock;
   1.752 +  if( ppOpen!=0 ){
   1.753 +    pOpen = openList;
   1.754 +    while( pOpen && memcmp(&key2, &pOpen->key, sizeof(key2)) ){
   1.755 +      pOpen = pOpen->pNext;
   1.756 +    }
   1.757 +    if( pOpen==0 ){
   1.758 +      pOpen = sqlite3_malloc( sizeof(*pOpen) );
   1.759 +      if( pOpen==0 ){
   1.760 +        releaseLockInfo(pLock);
   1.761 +        rc = SQLITE_NOMEM;
   1.762 +        goto exit_findlockinfo;
   1.763 +      }
   1.764 +      pOpen->key = key2;
   1.765 +      pOpen->nRef = 1;
   1.766 +      pOpen->nLock = 0;
   1.767 +      pOpen->nPending = 0;
   1.768 +      pOpen->aPending = 0;
   1.769 +      pOpen->pNext = openList;
   1.770 +      pOpen->pPrev = 0;
   1.771 +      if( openList ) openList->pPrev = pOpen;
   1.772 +      openList = pOpen;
   1.773 +    }else{
   1.774 +      pOpen->nRef++;
   1.775 +    }
   1.776 +    *ppOpen = pOpen;
   1.777 +  }
   1.778 +
   1.779 +exit_findlockinfo:
   1.780 +  return rc;
   1.781 +}
   1.782 +
   1.783 +#ifdef SQLITE_DEBUG
   1.784 +/*
   1.785 +** Helper function for printing out trace information from debugging
   1.786 +** binaries. This returns the string represetation of the supplied
   1.787 +** integer lock-type.
   1.788 +*/
   1.789 +static const char *locktypeName(int locktype){
   1.790 +  switch( locktype ){
   1.791 +  case NO_LOCK: return "NONE";
   1.792 +  case SHARED_LOCK: return "SHARED";
   1.793 +  case RESERVED_LOCK: return "RESERVED";
   1.794 +  case PENDING_LOCK: return "PENDING";
   1.795 +  case EXCLUSIVE_LOCK: return "EXCLUSIVE";
   1.796 +  }
   1.797 +  return "ERROR";
   1.798 +}
   1.799 +#endif
   1.800 +
   1.801 +/*
   1.802 +** If we are currently in a different thread than the thread that the
   1.803 +** unixFile argument belongs to, then transfer ownership of the unixFile
   1.804 +** over to the current thread.
   1.805 +**
   1.806 +** A unixFile is only owned by a thread on systems where one thread is
   1.807 +** unable to override locks created by a different thread.  RedHat9 is
   1.808 +** an example of such a system.
   1.809 +**
   1.810 +** Ownership transfer is only allowed if the unixFile is currently unlocked.
   1.811 +** If the unixFile is locked and an ownership is wrong, then return
   1.812 +** SQLITE_MISUSE.  SQLITE_OK is returned if everything works.
   1.813 +*/
   1.814 +#if SQLITE_THREADSAFE
   1.815 +static int transferOwnership(unixFile *pFile){
   1.816 +  int rc;
   1.817 +  pthread_t hSelf;
   1.818 +  if( threadsOverrideEachOthersLocks ){
   1.819 +    /* Ownership transfers not needed on this system */
   1.820 +    return SQLITE_OK;
   1.821 +  }
   1.822 +  hSelf = pthread_self();
   1.823 +  if( pthread_equal(pFile->tid, hSelf) ){
   1.824 +    /* We are still in the same thread */
   1.825 +    OSTRACE1("No-transfer, same thread\n");
   1.826 +    return SQLITE_OK;
   1.827 +  }
   1.828 +  if( pFile->locktype!=NO_LOCK ){
   1.829 +    /* We cannot change ownership while we are holding a lock! */
   1.830 +    return SQLITE_MISUSE;
   1.831 +  }
   1.832 +  OSTRACE4("Transfer ownership of %d from %d to %d\n",
   1.833 +            pFile->h, pFile->tid, hSelf);
   1.834 +  pFile->tid = hSelf;
   1.835 +  if (pFile->pLock != NULL) {
   1.836 +    releaseLockInfo(pFile->pLock);
   1.837 +    rc = findLockInfo(pFile->h, &pFile->pLock, 0);
   1.838 +    OSTRACE5("LOCK    %d is now %s(%s,%d)\n", pFile->h,
   1.839 +           locktypeName(pFile->locktype),
   1.840 +           locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
   1.841 +    return rc;
   1.842 +  } else {
   1.843 +    return SQLITE_OK;
   1.844 +  }
   1.845 +}
   1.846 +#else
   1.847 +  /* On single-threaded builds, ownership transfer is a no-op */
   1.848 +# define transferOwnership(X) SQLITE_OK
   1.849 +#endif
   1.850 +
   1.851 +/*
   1.852 +** Seek to the offset passed as the second argument, then read cnt 
   1.853 +** bytes into pBuf. Return the number of bytes actually read.
   1.854 +**
   1.855 +** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
   1.856 +** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
   1.857 +** one system to another.  Since SQLite does not define USE_PREAD
   1.858 +** any any form by default, we will not attempt to define _XOPEN_SOURCE.
   1.859 +** See tickets #2741 and #2681.
   1.860 +*/
   1.861 +static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
   1.862 +  int got;
   1.863 +  i64 newOffset;
   1.864 +  TIMER_START;
   1.865 +#if defined(USE_PREAD)
   1.866 +  got = pread(id->h, pBuf, cnt, offset);
   1.867 +  SimulateIOError( got = -1 );
   1.868 +#elif defined(USE_PREAD64)
   1.869 +  got = pread64(id->h, pBuf, cnt, offset);
   1.870 +  SimulateIOError( got = -1 );
   1.871 +#else
   1.872 +  newOffset = lseek(id->h, offset, SEEK_SET);
   1.873 +  SimulateIOError( newOffset-- );
   1.874 +  if( newOffset!=offset ){
   1.875 +    return -1;
   1.876 +  }
   1.877 +  got = read(id->h, pBuf, cnt);
   1.878 +#endif
   1.879 +  TIMER_END;
   1.880 +  OSTRACE5("READ    %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
   1.881 +  return got;
   1.882 +}
   1.883 +
   1.884 +/*
   1.885 +** Read data from a file into a buffer.  Return SQLITE_OK if all
   1.886 +** bytes were read successfully and SQLITE_IOERR if anything goes
   1.887 +** wrong.
   1.888 +*/
   1.889 +static int unixRead(
   1.890 +  sqlite3_file *id, 
   1.891 +  void *pBuf, 
   1.892 +  int amt,
   1.893 +  sqlite3_int64 offset
   1.894 +){
   1.895 +  int got;
   1.896 +  assert( id );
   1.897 +  got = seekAndRead((unixFile*)id, offset, pBuf, amt);
   1.898 +  if( got==amt ){
   1.899 +    return SQLITE_OK;
   1.900 +  }else if( got<0 ){
   1.901 +    return SQLITE_IOERR_READ;
   1.902 +  }else{
   1.903 +    memset(&((char*)pBuf)[got], 0, amt-got);
   1.904 +    return SQLITE_IOERR_SHORT_READ;
   1.905 +  }
   1.906 +}
   1.907 +
   1.908 +/*
   1.909 +** Seek to the offset in id->offset then read cnt bytes into pBuf.
   1.910 +** Return the number of bytes actually read.  Update the offset.
   1.911 +*/
   1.912 +static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
   1.913 +  int got;
   1.914 +  i64 newOffset;
   1.915 +  TIMER_START;
   1.916 +#if defined(USE_PREAD)
   1.917 +  got = pwrite(id->h, pBuf, cnt, offset);
   1.918 +#elif defined(USE_PREAD64)
   1.919 +  got = pwrite64(id->h, pBuf, cnt, offset);
   1.920 +#else
   1.921 +  newOffset = lseek(id->h, offset, SEEK_SET);
   1.922 +  if( newOffset!=offset ){
   1.923 +    return -1;
   1.924 +  }
   1.925 +  got = write(id->h, pBuf, cnt);
   1.926 +#endif
   1.927 +  TIMER_END;
   1.928 +  OSTRACE5("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
   1.929 +  return got;
   1.930 +}
   1.931 +
   1.932 +
   1.933 +/*
   1.934 +** Write data from a buffer into a file.  Return SQLITE_OK on success
   1.935 +** or some other error code on failure.
   1.936 +*/
   1.937 +static int unixWrite(
   1.938 +  sqlite3_file *id, 
   1.939 +  const void *pBuf, 
   1.940 +  int amt,
   1.941 +  sqlite3_int64 offset 
   1.942 +){
   1.943 +  int wrote = 0;
   1.944 +  assert( id );
   1.945 +  assert( amt>0 );
   1.946 +  while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
   1.947 +    amt -= wrote;
   1.948 +    offset += wrote;
   1.949 +    pBuf = &((char*)pBuf)[wrote];
   1.950 +  }
   1.951 +  SimulateIOError(( wrote=(-1), amt=1 ));
   1.952 +  SimulateDiskfullError(( wrote=0, amt=1 ));
   1.953 +  if( amt>0 ){
   1.954 +    if( wrote<0 ){
   1.955 +      return SQLITE_IOERR_WRITE;
   1.956 +    }else{
   1.957 +      return SQLITE_FULL;
   1.958 +    }
   1.959 +  }
   1.960 +  return SQLITE_OK;
   1.961 +}
   1.962 +
   1.963 +#ifdef SQLITE_TEST
   1.964 +/*
   1.965 +** Count the number of fullsyncs and normal syncs.  This is used to test
   1.966 +** that syncs and fullsyncs are occuring at the right times.
   1.967 +*/
   1.968 +int sqlite3_sync_count = 0;
   1.969 +int sqlite3_fullsync_count = 0;
   1.970 +#endif
   1.971 +
   1.972 +/*
   1.973 +** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
   1.974 +** Otherwise use fsync() in its place.
   1.975 +*/
   1.976 +#ifndef HAVE_FDATASYNC
   1.977 +# define fdatasync fsync
   1.978 +#endif
   1.979 +
   1.980 +/*
   1.981 +** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
   1.982 +** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
   1.983 +** only available on Mac OS X.  But that could change.
   1.984 +*/
   1.985 +#ifdef F_FULLFSYNC
   1.986 +# define HAVE_FULLFSYNC 1
   1.987 +#else
   1.988 +# define HAVE_FULLFSYNC 0
   1.989 +#endif
   1.990 +
   1.991 +
   1.992 +/*
   1.993 +** The fsync() system call does not work as advertised on many
   1.994 +** unix systems.  The following procedure is an attempt to make
   1.995 +** it work better.
   1.996 +**
   1.997 +** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
   1.998 +** for testing when we want to run through the test suite quickly.
   1.999 +** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
  1.1000 +** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
  1.1001 +** or power failure will likely corrupt the database file.
  1.1002 +*/
  1.1003 +static int full_fsync(int fd, int fullSync, int dataOnly){
  1.1004 +  int rc;
  1.1005 +
  1.1006 +  /* Record the number of times that we do a normal fsync() and 
  1.1007 +  ** FULLSYNC.  This is used during testing to verify that this procedure
  1.1008 +  ** gets called with the correct arguments.
  1.1009 +  */
  1.1010 +#ifdef SQLITE_TEST
  1.1011 +  if( fullSync ) sqlite3_fullsync_count++;
  1.1012 +  sqlite3_sync_count++;
  1.1013 +#endif
  1.1014 +
  1.1015 +  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
  1.1016 +  ** no-op
  1.1017 +  */
  1.1018 +#ifdef SQLITE_NO_SYNC
  1.1019 +  rc = SQLITE_OK;
  1.1020 +#else
  1.1021 +
  1.1022 +#if HAVE_FULLFSYNC
  1.1023 +  if( fullSync ){
  1.1024 +    rc = fcntl(fd, F_FULLFSYNC, 0);
  1.1025 +  }else{
  1.1026 +    rc = 1;
  1.1027 +  }
  1.1028 +  /* If the FULLFSYNC failed, fall back to attempting an fsync().
  1.1029 +   * It shouldn't be possible for fullfsync to fail on the local 
  1.1030 +   * file system (on OSX), so failure indicates that FULLFSYNC
  1.1031 +   * isn't supported for this file system. So, attempt an fsync 
  1.1032 +   * and (for now) ignore the overhead of a superfluous fcntl call.  
  1.1033 +   * It'd be better to detect fullfsync support once and avoid 
  1.1034 +   * the fcntl call every time sync is called.
  1.1035 +   */
  1.1036 +  if( rc ) rc = fsync(fd);
  1.1037 +
  1.1038 +#else 
  1.1039 +  if( dataOnly ){
  1.1040 +    rc = fdatasync(fd);
  1.1041 +  }else{
  1.1042 +    rc = fsync(fd);
  1.1043 +  }
  1.1044 +#endif /* HAVE_FULLFSYNC */
  1.1045 +#endif /* defined(SQLITE_NO_SYNC) */
  1.1046 +
  1.1047 +  return rc;
  1.1048 +}
  1.1049 +
  1.1050 +/*
  1.1051 +** Make sure all writes to a particular file are committed to disk.
  1.1052 +**
  1.1053 +** If dataOnly==0 then both the file itself and its metadata (file
  1.1054 +** size, access time, etc) are synced.  If dataOnly!=0 then only the
  1.1055 +** file data is synced.
  1.1056 +**
  1.1057 +** Under Unix, also make sure that the directory entry for the file
  1.1058 +** has been created by fsync-ing the directory that contains the file.
  1.1059 +** If we do not do this and we encounter a power failure, the directory
  1.1060 +** entry for the journal might not exist after we reboot.  The next
  1.1061 +** SQLite to access the file will not know that the journal exists (because
  1.1062 +** the directory entry for the journal was never created) and the transaction
  1.1063 +** will not roll back - possibly leading to database corruption.
  1.1064 +*/
  1.1065 +static int unixSync(sqlite3_file *id, int flags){
  1.1066 +  int rc;
  1.1067 +  unixFile *pFile = (unixFile*)id;
  1.1068 +
  1.1069 +  int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
  1.1070 +  int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
  1.1071 +
  1.1072 +  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
  1.1073 +  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
  1.1074 +      || (flags&0x0F)==SQLITE_SYNC_FULL
  1.1075 +  );
  1.1076 +
  1.1077 +  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
  1.1078 +  ** line is to test that doing so does not cause any problems.
  1.1079 +  */
  1.1080 +  SimulateDiskfullError( return SQLITE_FULL );
  1.1081 +
  1.1082 +  assert( pFile );
  1.1083 +  OSTRACE2("SYNC    %-3d\n", pFile->h);
  1.1084 +  rc = full_fsync(pFile->h, isFullsync, isDataOnly);
  1.1085 +  SimulateIOError( rc=1 );
  1.1086 +  if( rc ){
  1.1087 +    return SQLITE_IOERR_FSYNC;
  1.1088 +  }
  1.1089 +  if( pFile->dirfd>=0 ){
  1.1090 +    OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
  1.1091 +            HAVE_FULLFSYNC, isFullsync);
  1.1092 +#ifndef SQLITE_DISABLE_DIRSYNC
  1.1093 +    /* The directory sync is only attempted if full_fsync is
  1.1094 +    ** turned off or unavailable.  If a full_fsync occurred above,
  1.1095 +    ** then the directory sync is superfluous.
  1.1096 +    */
  1.1097 +    if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
  1.1098 +       /*
  1.1099 +       ** We have received multiple reports of fsync() returning
  1.1100 +       ** errors when applied to directories on certain file systems.
  1.1101 +       ** A failed directory sync is not a big deal.  So it seems
  1.1102 +       ** better to ignore the error.  Ticket #1657
  1.1103 +       */
  1.1104 +       /* return SQLITE_IOERR; */
  1.1105 +    }
  1.1106 +#endif
  1.1107 +    close(pFile->dirfd);  /* Only need to sync once, so close the directory */
  1.1108 +    pFile->dirfd = -1;    /* when we are done. */
  1.1109 +  }
  1.1110 +  return SQLITE_OK;
  1.1111 +}
  1.1112 +
  1.1113 +/*
  1.1114 +** Truncate an open file to a specified size
  1.1115 +*/
  1.1116 +static int unixTruncate(sqlite3_file *id, i64 nByte){
  1.1117 +  int rc;
  1.1118 +  assert( id );
  1.1119 +  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
  1.1120 +  rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
  1.1121 +  if( rc ){
  1.1122 +    return SQLITE_IOERR_TRUNCATE;
  1.1123 +  }else{
  1.1124 +    return SQLITE_OK;
  1.1125 +  }
  1.1126 +}
  1.1127 +
  1.1128 +/*
  1.1129 +** Determine the current size of a file in bytes
  1.1130 +*/
  1.1131 +static int unixFileSize(sqlite3_file *id, i64 *pSize){
  1.1132 +  int rc;
  1.1133 +  struct stat buf;
  1.1134 +  assert( id );
  1.1135 +  rc = fstat(((unixFile*)id)->h, &buf);
  1.1136 +  SimulateIOError( rc=1 );
  1.1137 +  if( rc!=0 ){
  1.1138 +    return SQLITE_IOERR_FSTAT;
  1.1139 +  }
  1.1140 +  *pSize = buf.st_size;
  1.1141 +
  1.1142 +  /* When opening a zero-size database, the findLockInfo() procedure
  1.1143 +  ** writes a single byte into that file in order to work around a bug
  1.1144 +  ** in the OS-X msdos filesystem.  In order to avoid problems with upper
  1.1145 +  ** layers, we need to report this file size as zero even though it is
  1.1146 +  ** really 1.   Ticket #3260.
  1.1147 +  */
  1.1148 +  if( *pSize==1 ) *pSize = 0;
  1.1149 +
  1.1150 +
  1.1151 +  return SQLITE_OK;
  1.1152 +}
  1.1153 +
  1.1154 +/*
  1.1155 +** This routine translates a standard POSIX errno code into something
  1.1156 +** useful to the clients of the sqlite3 functions.  Specifically, it is
  1.1157 +** intended to translate a variety of "try again" errors into SQLITE_BUSY
  1.1158 +** and a variety of "please close the file descriptor NOW" errors into 
  1.1159 +** SQLITE_IOERR
  1.1160 +** 
  1.1161 +** Errors during initialization of locks, or file system support for locks,
  1.1162 +** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
  1.1163 +*/
  1.1164 +static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
  1.1165 +  switch (posixError) {
  1.1166 +  case 0: 
  1.1167 +    return SQLITE_OK;
  1.1168 +    
  1.1169 +  case EAGAIN:
  1.1170 +  case ETIMEDOUT:
  1.1171 +  case EBUSY:
  1.1172 +  case EINTR:
  1.1173 +  case ENOLCK:  
  1.1174 +    /* random NFS retry error, unless during file system support 
  1.1175 +     * introspection, in which it actually means what it says */
  1.1176 +    return SQLITE_BUSY;
  1.1177 +    
  1.1178 +  case EACCES: 
  1.1179 +    /* EACCES is like EAGAIN during locking operations, but not any other time*/
  1.1180 +    if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
  1.1181 +	(sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
  1.1182 +	(sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
  1.1183 +	(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
  1.1184 +      return SQLITE_BUSY;
  1.1185 +    }
  1.1186 +    /* else fall through */
  1.1187 +  case EPERM: 
  1.1188 +    return SQLITE_PERM;
  1.1189 +    
  1.1190 +  case EDEADLK:
  1.1191 +    return SQLITE_IOERR_BLOCKED;
  1.1192 +    
  1.1193 +#if EOPNOTSUPP!=ENOTSUP
  1.1194 +  case EOPNOTSUPP: 
  1.1195 +    /* something went terribly awry, unless during file system support 
  1.1196 +     * introspection, in which it actually means what it says */
  1.1197 +#endif
  1.1198 +#ifdef ENOTSUP
  1.1199 +  case ENOTSUP: 
  1.1200 +    /* invalid fd, unless during file system support introspection, in which 
  1.1201 +     * it actually means what it says */
  1.1202 +#endif
  1.1203 +  case EIO:
  1.1204 +  case EBADF:
  1.1205 +  case EINVAL:
  1.1206 +  case ENOTCONN:
  1.1207 +  case ENODEV:
  1.1208 +  case ENXIO:
  1.1209 +  case ENOENT:
  1.1210 +  case ESTALE:
  1.1211 +  case ENOSYS:
  1.1212 +    /* these should force the client to close the file and reconnect */
  1.1213 +    
  1.1214 +  default: 
  1.1215 +    return sqliteIOErr;
  1.1216 +  }
  1.1217 +}
  1.1218 +
  1.1219 +/*
  1.1220 +** This routine checks if there is a RESERVED lock held on the specified
  1.1221 +** file by this or any other process. If such a lock is held, set *pResOut
  1.1222 +** to a non-zero value otherwise *pResOut is set to zero.  The return value
  1.1223 +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
  1.1224 +*/
  1.1225 +static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
  1.1226 +  int rc = SQLITE_OK;
  1.1227 +  int reserved = 0;
  1.1228 +  unixFile *pFile = (unixFile*)id;
  1.1229 +
  1.1230 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  1.1231 +
  1.1232 +  assert( pFile );
  1.1233 +  enterMutex(); /* Because pFile->pLock is shared across threads */
  1.1234 +
  1.1235 +  /* Check if a thread in this process holds such a lock */
  1.1236 +  if( pFile->pLock->locktype>SHARED_LOCK ){
  1.1237 +    reserved = 1;
  1.1238 +  }
  1.1239 +
  1.1240 +  /* Otherwise see if some other process holds it.
  1.1241 +  */
  1.1242 +  if( !reserved ){
  1.1243 +    struct flock lock;
  1.1244 +    lock.l_whence = SEEK_SET;
  1.1245 +    lock.l_start = RESERVED_BYTE;
  1.1246 +    lock.l_len = 1;
  1.1247 +    lock.l_type = F_WRLCK;
  1.1248 +    if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
  1.1249 +      int tErrno = errno;
  1.1250 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
  1.1251 +      pFile->lastErrno = tErrno;
  1.1252 +    } else if( lock.l_type!=F_UNLCK ){
  1.1253 +      reserved = 1;
  1.1254 +    }
  1.1255 +  }
  1.1256 +  
  1.1257 +  leaveMutex();
  1.1258 +  OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
  1.1259 +
  1.1260 +  *pResOut = reserved;
  1.1261 +  return rc;
  1.1262 +}
  1.1263 +
  1.1264 +/*
  1.1265 +** Lock the file with the lock specified by parameter locktype - one
  1.1266 +** of the following:
  1.1267 +**
  1.1268 +**     (1) SHARED_LOCK
  1.1269 +**     (2) RESERVED_LOCK
  1.1270 +**     (3) PENDING_LOCK
  1.1271 +**     (4) EXCLUSIVE_LOCK
  1.1272 +**
  1.1273 +** Sometimes when requesting one lock state, additional lock states
  1.1274 +** are inserted in between.  The locking might fail on one of the later
  1.1275 +** transitions leaving the lock state different from what it started but
  1.1276 +** still short of its goal.  The following chart shows the allowed
  1.1277 +** transitions and the inserted intermediate states:
  1.1278 +**
  1.1279 +**    UNLOCKED -> SHARED
  1.1280 +**    SHARED -> RESERVED
  1.1281 +**    SHARED -> (PENDING) -> EXCLUSIVE
  1.1282 +**    RESERVED -> (PENDING) -> EXCLUSIVE
  1.1283 +**    PENDING -> EXCLUSIVE
  1.1284 +**
  1.1285 +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
  1.1286 +** routine to lower a locking level.
  1.1287 +*/
  1.1288 +static int unixLock(sqlite3_file *id, int locktype){
  1.1289 +  /* The following describes the implementation of the various locks and
  1.1290 +  ** lock transitions in terms of the POSIX advisory shared and exclusive
  1.1291 +  ** lock primitives (called read-locks and write-locks below, to avoid
  1.1292 +  ** confusion with SQLite lock names). The algorithms are complicated
  1.1293 +  ** slightly in order to be compatible with windows systems simultaneously
  1.1294 +  ** accessing the same database file, in case that is ever required.
  1.1295 +  **
  1.1296 +  ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
  1.1297 +  ** byte', each single bytes at well known offsets, and the 'shared byte
  1.1298 +  ** range', a range of 510 bytes at a well known offset.
  1.1299 +  **
  1.1300 +  ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
  1.1301 +  ** byte'.  If this is successful, a random byte from the 'shared byte
  1.1302 +  ** range' is read-locked and the lock on the 'pending byte' released.
  1.1303 +  **
  1.1304 +  ** A process may only obtain a RESERVED lock after it has a SHARED lock.
  1.1305 +  ** A RESERVED lock is implemented by grabbing a write-lock on the
  1.1306 +  ** 'reserved byte'. 
  1.1307 +  **
  1.1308 +  ** A process may only obtain a PENDING lock after it has obtained a
  1.1309 +  ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
  1.1310 +  ** on the 'pending byte'. This ensures that no new SHARED locks can be
  1.1311 +  ** obtained, but existing SHARED locks are allowed to persist. A process
  1.1312 +  ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
  1.1313 +  ** This property is used by the algorithm for rolling back a journal file
  1.1314 +  ** after a crash.
  1.1315 +  **
  1.1316 +  ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
  1.1317 +  ** implemented by obtaining a write-lock on the entire 'shared byte
  1.1318 +  ** range'. Since all other locks require a read-lock on one of the bytes
  1.1319 +  ** within this range, this ensures that no other locks are held on the
  1.1320 +  ** database. 
  1.1321 +  **
  1.1322 +  ** The reason a single byte cannot be used instead of the 'shared byte
  1.1323 +  ** range' is that some versions of windows do not support read-locks. By
  1.1324 +  ** locking a random byte from a range, concurrent SHARED locks may exist
  1.1325 +  ** even if the locking primitive used is always a write-lock.
  1.1326 +  */
  1.1327 +  int rc = SQLITE_OK;
  1.1328 +  unixFile *pFile = (unixFile*)id;
  1.1329 +  struct lockInfo *pLock = pFile->pLock;
  1.1330 +  struct flock lock;
  1.1331 +  int s;
  1.1332 +
  1.1333 +  assert( pFile );
  1.1334 +  OSTRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h,
  1.1335 +      locktypeName(locktype), locktypeName(pFile->locktype),
  1.1336 +      locktypeName(pLock->locktype), pLock->cnt , getpid());
  1.1337 +
  1.1338 +  /* If there is already a lock of this type or more restrictive on the
  1.1339 +  ** unixFile, do nothing. Don't use the end_lock: exit path, as
  1.1340 +  ** enterMutex() hasn't been called yet.
  1.1341 +  */
  1.1342 +  if( pFile->locktype>=locktype ){
  1.1343 +    OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
  1.1344 +            locktypeName(locktype));
  1.1345 +    return SQLITE_OK;
  1.1346 +  }
  1.1347 +
  1.1348 +  /* Make sure the locking sequence is correct
  1.1349 +  */
  1.1350 +  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
  1.1351 +  assert( locktype!=PENDING_LOCK );
  1.1352 +  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
  1.1353 +
  1.1354 +  /* This mutex is needed because pFile->pLock is shared across threads
  1.1355 +  */
  1.1356 +  enterMutex();
  1.1357 +
  1.1358 +  /* Make sure the current thread owns the pFile.
  1.1359 +  */
  1.1360 +  rc = transferOwnership(pFile);
  1.1361 +  if( rc!=SQLITE_OK ){
  1.1362 +    leaveMutex();
  1.1363 +    return rc;
  1.1364 +  }
  1.1365 +  pLock = pFile->pLock;
  1.1366 +
  1.1367 +  /* If some thread using this PID has a lock via a different unixFile*
  1.1368 +  ** handle that precludes the requested lock, return BUSY.
  1.1369 +  */
  1.1370 +  if( (pFile->locktype!=pLock->locktype && 
  1.1371 +          (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
  1.1372 +  ){
  1.1373 +    rc = SQLITE_BUSY;
  1.1374 +    goto end_lock;
  1.1375 +  }
  1.1376 +
  1.1377 +  /* If a SHARED lock is requested, and some thread using this PID already
  1.1378 +  ** has a SHARED or RESERVED lock, then increment reference counts and
  1.1379 +  ** return SQLITE_OK.
  1.1380 +  */
  1.1381 +  if( locktype==SHARED_LOCK && 
  1.1382 +      (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
  1.1383 +    assert( locktype==SHARED_LOCK );
  1.1384 +    assert( pFile->locktype==0 );
  1.1385 +    assert( pLock->cnt>0 );
  1.1386 +    pFile->locktype = SHARED_LOCK;
  1.1387 +    pLock->cnt++;
  1.1388 +    pFile->pOpen->nLock++;
  1.1389 +    goto end_lock;
  1.1390 +  }
  1.1391 +
  1.1392 +  lock.l_len = 1L;
  1.1393 +
  1.1394 +  lock.l_whence = SEEK_SET;
  1.1395 +
  1.1396 +  /* A PENDING lock is needed before acquiring a SHARED lock and before
  1.1397 +  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
  1.1398 +  ** be released.
  1.1399 +  */
  1.1400 +  if( locktype==SHARED_LOCK 
  1.1401 +      || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
  1.1402 +  ){
  1.1403 +    lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
  1.1404 +    lock.l_start = PENDING_BYTE;
  1.1405 +    s = fcntl(pFile->h, F_SETLK, &lock);
  1.1406 +    if( s==(-1) ){
  1.1407 +      int tErrno = errno;
  1.1408 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  1.1409 +      if( IS_LOCK_ERROR(rc) ){
  1.1410 +        pFile->lastErrno = tErrno;
  1.1411 +      }
  1.1412 +      goto end_lock;
  1.1413 +    }
  1.1414 +  }
  1.1415 +
  1.1416 +
  1.1417 +  /* If control gets to this point, then actually go ahead and make
  1.1418 +  ** operating system calls for the specified lock.
  1.1419 +  */
  1.1420 +  if( locktype==SHARED_LOCK ){
  1.1421 +    int tErrno = 0;
  1.1422 +    assert( pLock->cnt==0 );
  1.1423 +    assert( pLock->locktype==0 );
  1.1424 +
  1.1425 +    /* Now get the read-lock */
  1.1426 +    lock.l_start = SHARED_FIRST;
  1.1427 +    lock.l_len = SHARED_SIZE;
  1.1428 +    if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
  1.1429 +      tErrno = errno;
  1.1430 +    }
  1.1431 +    /* Drop the temporary PENDING lock */
  1.1432 +    lock.l_start = PENDING_BYTE;
  1.1433 +    lock.l_len = 1L;
  1.1434 +    lock.l_type = F_UNLCK;
  1.1435 +    if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
  1.1436 +      if( s != -1 ){
  1.1437 +        /* This could happen with a network mount */
  1.1438 +        tErrno = errno; 
  1.1439 +        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); 
  1.1440 +        if( IS_LOCK_ERROR(rc) ){
  1.1441 +          pFile->lastErrno = tErrno;
  1.1442 +        }
  1.1443 +        goto end_lock;
  1.1444 +      }
  1.1445 +    }
  1.1446 +    if( s==(-1) ){
  1.1447 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  1.1448 +      if( IS_LOCK_ERROR(rc) ){
  1.1449 +        pFile->lastErrno = tErrno;
  1.1450 +      }
  1.1451 +    }else{
  1.1452 +      pFile->locktype = SHARED_LOCK;
  1.1453 +      pFile->pOpen->nLock++;
  1.1454 +      pLock->cnt = 1;
  1.1455 +    }
  1.1456 +  }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
  1.1457 +    /* We are trying for an exclusive lock but another thread in this
  1.1458 +    ** same process is still holding a shared lock. */
  1.1459 +    rc = SQLITE_BUSY;
  1.1460 +  }else{
  1.1461 +    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
  1.1462 +    ** assumed that there is a SHARED or greater lock on the file
  1.1463 +    ** already.
  1.1464 +    */
  1.1465 +    assert( 0!=pFile->locktype );
  1.1466 +    lock.l_type = F_WRLCK;
  1.1467 +    switch( locktype ){
  1.1468 +      case RESERVED_LOCK:
  1.1469 +        lock.l_start = RESERVED_BYTE;
  1.1470 +        break;
  1.1471 +      case EXCLUSIVE_LOCK:
  1.1472 +        lock.l_start = SHARED_FIRST;
  1.1473 +        lock.l_len = SHARED_SIZE;
  1.1474 +        break;
  1.1475 +      default:
  1.1476 +        assert(0);
  1.1477 +    }
  1.1478 +    s = fcntl(pFile->h, F_SETLK, &lock);
  1.1479 +    if( s==(-1) ){
  1.1480 +      int tErrno = errno;
  1.1481 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  1.1482 +      if( IS_LOCK_ERROR(rc) ){
  1.1483 +        pFile->lastErrno = tErrno;
  1.1484 +      }
  1.1485 +    }
  1.1486 +  }
  1.1487 +  
  1.1488 +  if( rc==SQLITE_OK ){
  1.1489 +    pFile->locktype = locktype;
  1.1490 +    pLock->locktype = locktype;
  1.1491 +  }else if( locktype==EXCLUSIVE_LOCK ){
  1.1492 +    pFile->locktype = PENDING_LOCK;
  1.1493 +    pLock->locktype = PENDING_LOCK;
  1.1494 +  }
  1.1495 +
  1.1496 +end_lock:
  1.1497 +  leaveMutex();
  1.1498 +  OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
  1.1499 +      rc==SQLITE_OK ? "ok" : "failed");
  1.1500 +  return rc;
  1.1501 +}
  1.1502 +
  1.1503 +/*
  1.1504 +** Lower the locking level on file descriptor pFile to locktype.  locktype
  1.1505 +** must be either NO_LOCK or SHARED_LOCK.
  1.1506 +**
  1.1507 +** If the locking level of the file descriptor is already at or below
  1.1508 +** the requested locking level, this routine is a no-op.
  1.1509 +*/
  1.1510 +static int unixUnlock(sqlite3_file *id, int locktype){
  1.1511 +  struct lockInfo *pLock;
  1.1512 +  struct flock lock;
  1.1513 +  int rc = SQLITE_OK;
  1.1514 +  unixFile *pFile = (unixFile*)id;
  1.1515 +  int h;
  1.1516 +
  1.1517 +  assert( pFile );
  1.1518 +  OSTRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
  1.1519 +      pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
  1.1520 +
  1.1521 +  assert( locktype<=SHARED_LOCK );
  1.1522 +  if( pFile->locktype<=locktype ){
  1.1523 +    return SQLITE_OK;
  1.1524 +  }
  1.1525 +  if( CHECK_THREADID(pFile) ){
  1.1526 +    return SQLITE_MISUSE;
  1.1527 +  }
  1.1528 +  enterMutex();
  1.1529 +  h = pFile->h;
  1.1530 +  pLock = pFile->pLock;
  1.1531 +  assert( pLock->cnt!=0 );
  1.1532 +  if( pFile->locktype>SHARED_LOCK ){
  1.1533 +    assert( pLock->locktype==pFile->locktype );
  1.1534 +    SimulateIOErrorBenign(1);
  1.1535 +    SimulateIOError( h=(-1) )
  1.1536 +    SimulateIOErrorBenign(0);
  1.1537 +    if( locktype==SHARED_LOCK ){
  1.1538 +      lock.l_type = F_RDLCK;
  1.1539 +      lock.l_whence = SEEK_SET;
  1.1540 +      lock.l_start = SHARED_FIRST;
  1.1541 +      lock.l_len = SHARED_SIZE;
  1.1542 +      if( fcntl(h, F_SETLK, &lock)==(-1) ){
  1.1543 +        int tErrno = errno;
  1.1544 +        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
  1.1545 +        if( IS_LOCK_ERROR(rc) ){
  1.1546 +          pFile->lastErrno = tErrno;
  1.1547 +        }
  1.1548 +				goto end_unlock;
  1.1549 +      }
  1.1550 +    }
  1.1551 +    lock.l_type = F_UNLCK;
  1.1552 +    lock.l_whence = SEEK_SET;
  1.1553 +    lock.l_start = PENDING_BYTE;
  1.1554 +    lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
  1.1555 +    if( fcntl(h, F_SETLK, &lock)!=(-1) ){
  1.1556 +      pLock->locktype = SHARED_LOCK;
  1.1557 +    }else{
  1.1558 +      int tErrno = errno;
  1.1559 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
  1.1560 +      if( IS_LOCK_ERROR(rc) ){
  1.1561 +        pFile->lastErrno = tErrno;
  1.1562 +      }
  1.1563 +			goto end_unlock;
  1.1564 +    }
  1.1565 +  }
  1.1566 +  if( locktype==NO_LOCK ){
  1.1567 +    struct openCnt *pOpen;
  1.1568 +
  1.1569 +    /* Decrement the shared lock counter.  Release the lock using an
  1.1570 +    ** OS call only when all threads in this same process have released
  1.1571 +    ** the lock.
  1.1572 +    */
  1.1573 +    pLock->cnt--;
  1.1574 +    if( pLock->cnt==0 ){
  1.1575 +      lock.l_type = F_UNLCK;
  1.1576 +      lock.l_whence = SEEK_SET;
  1.1577 +      lock.l_start = lock.l_len = 0L;
  1.1578 +      SimulateIOErrorBenign(1);
  1.1579 +      SimulateIOError( h=(-1) )
  1.1580 +      SimulateIOErrorBenign(0);
  1.1581 +      if( fcntl(h, F_SETLK, &lock)!=(-1) ){
  1.1582 +        pLock->locktype = NO_LOCK;
  1.1583 +      }else{
  1.1584 +        int tErrno = errno;
  1.1585 +        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
  1.1586 +        if( IS_LOCK_ERROR(rc) ){
  1.1587 +          pFile->lastErrno = tErrno;
  1.1588 +        }
  1.1589 +        pLock->cnt = 1;
  1.1590 +				goto end_unlock;
  1.1591 +      }
  1.1592 +    }
  1.1593 +
  1.1594 +    /* Decrement the count of locks against this same file.  When the
  1.1595 +    ** count reaches zero, close any other file descriptors whose close
  1.1596 +    ** was deferred because of outstanding locks.
  1.1597 +    */
  1.1598 +    if( rc==SQLITE_OK ){
  1.1599 +      pOpen = pFile->pOpen;
  1.1600 +      pOpen->nLock--;
  1.1601 +      assert( pOpen->nLock>=0 );
  1.1602 +      if( pOpen->nLock==0 && pOpen->nPending>0 ){
  1.1603 +        int i;
  1.1604 +        for(i=0; i<pOpen->nPending; i++){
  1.1605 +          close(pOpen->aPending[i]);
  1.1606 +        }
  1.1607 +        sqlite3_free(pOpen->aPending);
  1.1608 +        pOpen->nPending = 0;
  1.1609 +        pOpen->aPending = 0;
  1.1610 +      }
  1.1611 +    }
  1.1612 +  }
  1.1613 +	
  1.1614 +end_unlock:
  1.1615 +  leaveMutex();
  1.1616 +  if( rc==SQLITE_OK ) pFile->locktype = locktype;
  1.1617 +  return rc;
  1.1618 +}
  1.1619 +
  1.1620 +/*
  1.1621 +** This function performs the parts of the "close file" operation 
  1.1622 +** common to all locking schemes. It closes the directory and file
  1.1623 +** handles, if they are valid, and sets all fields of the unixFile
  1.1624 +** structure to 0.
  1.1625 +*/
  1.1626 +static int closeUnixFile(sqlite3_file *id){
  1.1627 +  unixFile *pFile = (unixFile*)id;
  1.1628 +  if( pFile ){
  1.1629 +    if( pFile->dirfd>=0 ){
  1.1630 +      close(pFile->dirfd);
  1.1631 +    }
  1.1632 +    if( pFile->h>=0 ){
  1.1633 +      close(pFile->h);
  1.1634 +    }
  1.1635 +    OSTRACE2("CLOSE   %-3d\n", pFile->h);
  1.1636 +    OpenCounter(-1);
  1.1637 +    memset(pFile, 0, sizeof(unixFile));
  1.1638 +  }
  1.1639 +  return SQLITE_OK;
  1.1640 +}
  1.1641 +
  1.1642 +/*
  1.1643 +** Close a file.
  1.1644 +*/
  1.1645 +static int unixClose(sqlite3_file *id){
  1.1646 +  if( id ){
  1.1647 +    unixFile *pFile = (unixFile *)id;
  1.1648 +    unixUnlock(id, NO_LOCK);
  1.1649 +    enterMutex();
  1.1650 +    if( pFile->pOpen && pFile->pOpen->nLock ){
  1.1651 +      /* If there are outstanding locks, do not actually close the file just
  1.1652 +      ** yet because that would clear those locks.  Instead, add the file
  1.1653 +      ** descriptor to pOpen->aPending.  It will be automatically closed when
  1.1654 +      ** the last lock is cleared.
  1.1655 +      */
  1.1656 +      int *aNew;
  1.1657 +      struct openCnt *pOpen = pFile->pOpen;
  1.1658 +      aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
  1.1659 +      if( aNew==0 ){
  1.1660 +        /* If a malloc fails, just leak the file descriptor */
  1.1661 +      }else{
  1.1662 +        pOpen->aPending = aNew;
  1.1663 +        pOpen->aPending[pOpen->nPending] = pFile->h;
  1.1664 +        pOpen->nPending++;
  1.1665 +        pFile->h = -1;
  1.1666 +      }
  1.1667 +    }
  1.1668 +    releaseLockInfo(pFile->pLock);
  1.1669 +    releaseOpenCnt(pFile->pOpen);
  1.1670 +    closeUnixFile(id);
  1.1671 +    leaveMutex();
  1.1672 +  }
  1.1673 +  return SQLITE_OK;
  1.1674 +}
  1.1675 +
  1.1676 +
  1.1677 +#if SQLITE_ENABLE_LOCKING_STYLE
  1.1678 +#pragma mark AFP Support
  1.1679 +
  1.1680 +/*
  1.1681 + ** The afpLockingContext structure contains all afp lock specific state
  1.1682 + */
  1.1683 +typedef struct afpLockingContext afpLockingContext;
  1.1684 +struct afpLockingContext {
  1.1685 +  unsigned long long sharedLockByte;
  1.1686 +  const char *filePath;
  1.1687 +};
  1.1688 +
  1.1689 +struct ByteRangeLockPB2
  1.1690 +{
  1.1691 +  unsigned long long offset;        /* offset to first byte to lock */
  1.1692 +  unsigned long long length;        /* nbr of bytes to lock */
  1.1693 +  unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
  1.1694 +  unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
  1.1695 +  unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
  1.1696 +  int fd;                           /* file desc to assoc this lock with */
  1.1697 +};
  1.1698 +
  1.1699 +#define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
  1.1700 +
  1.1701 +/* 
  1.1702 + ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
  1.1703 + */
  1.1704 +static int _AFPFSSetLock(
  1.1705 +  const char *path, 
  1.1706 +  unixFile *pFile, 
  1.1707 +  unsigned long long offset, 
  1.1708 +  unsigned long long length, 
  1.1709 +  int setLockFlag
  1.1710 +){
  1.1711 +  struct ByteRangeLockPB2       pb;
  1.1712 +  int                     err;
  1.1713 +  
  1.1714 +  pb.unLockFlag = setLockFlag ? 0 : 1;
  1.1715 +  pb.startEndFlag = 0;
  1.1716 +  pb.offset = offset;
  1.1717 +  pb.length = length; 
  1.1718 +  pb.fd = pFile->h;
  1.1719 +  OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n", 
  1.1720 +    (setLockFlag?"ON":"OFF"), pFile->h, offset, length);
  1.1721 +  err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
  1.1722 +  if ( err==-1 ) {
  1.1723 +    int rc;
  1.1724 +    int tErrno = errno;
  1.1725 +    OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, tErrno, strerror(tErrno));
  1.1726 +    rc = sqliteErrorFromPosixError(tErrno, setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); /* error */
  1.1727 +    if( IS_LOCK_ERROR(rc) ){
  1.1728 +      pFile->lastErrno = tErrno;
  1.1729 +    }
  1.1730 +    return rc;
  1.1731 +  } else {
  1.1732 +    return SQLITE_OK;
  1.1733 +  }
  1.1734 +}
  1.1735 +
  1.1736 +/* AFP-style reserved lock checking following the behavior of 
  1.1737 +** unixCheckReservedLock, see the unixCheckReservedLock function comments */
  1.1738 +static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
  1.1739 +  int rc = SQLITE_OK;
  1.1740 +  int reserved = 0;
  1.1741 +  unixFile *pFile = (unixFile*)id;
  1.1742 +  
  1.1743 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  1.1744 +  
  1.1745 +  assert( pFile );
  1.1746 +  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  1.1747 +  
  1.1748 +  /* Check if a thread in this process holds such a lock */
  1.1749 +  if( pFile->locktype>SHARED_LOCK ){
  1.1750 +    reserved = 1;
  1.1751 +  }
  1.1752 +  
  1.1753 +  /* Otherwise see if some other process holds it.
  1.1754 +   */
  1.1755 +  if( !reserved ){
  1.1756 +    /* lock the RESERVED byte */
  1.1757 +    int lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);  
  1.1758 +    if( SQLITE_OK==lrc ){
  1.1759 +      /* if we succeeded in taking the reserved lock, unlock it to restore
  1.1760 +      ** the original state */
  1.1761 +      lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1, 0);
  1.1762 +    } else {
  1.1763 +      /* if we failed to get the lock then someone else must have it */
  1.1764 +      reserved = 1;
  1.1765 +    }
  1.1766 +    if( IS_LOCK_ERROR(lrc) ){
  1.1767 +      rc=lrc;
  1.1768 +    }
  1.1769 +  }
  1.1770 +  
  1.1771 +  OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
  1.1772 +  
  1.1773 +  *pResOut = reserved;
  1.1774 +  return rc;
  1.1775 +}
  1.1776 +
  1.1777 +/* AFP-style locking following the behavior of unixLock, see the unixLock 
  1.1778 +** function comments for details of lock management. */
  1.1779 +static int afpLock(sqlite3_file *id, int locktype){
  1.1780 +  int rc = SQLITE_OK;
  1.1781 +  unixFile *pFile = (unixFile*)id;
  1.1782 +  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  1.1783 +  
  1.1784 +  assert( pFile );
  1.1785 +  OSTRACE5("LOCK    %d %s was %s pid=%d\n", pFile->h,
  1.1786 +         locktypeName(locktype), locktypeName(pFile->locktype), getpid());
  1.1787 +
  1.1788 +  /* If there is already a lock of this type or more restrictive on the
  1.1789 +  ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
  1.1790 +  ** enterMutex() hasn't been called yet.
  1.1791 +  */
  1.1792 +  if( pFile->locktype>=locktype ){
  1.1793 +    OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
  1.1794 +           locktypeName(locktype));
  1.1795 +    return SQLITE_OK;
  1.1796 +  }
  1.1797 +
  1.1798 +  /* Make sure the locking sequence is correct
  1.1799 +  */
  1.1800 +  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
  1.1801 +  assert( locktype!=PENDING_LOCK );
  1.1802 +  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
  1.1803 +  
  1.1804 +  /* This mutex is needed because pFile->pLock is shared across threads
  1.1805 +  */
  1.1806 +  enterMutex();
  1.1807 +
  1.1808 +  /* Make sure the current thread owns the pFile.
  1.1809 +  */
  1.1810 +  rc = transferOwnership(pFile);
  1.1811 +  if( rc!=SQLITE_OK ){
  1.1812 +    leaveMutex();
  1.1813 +    return rc;
  1.1814 +  }
  1.1815 +    
  1.1816 +  /* A PENDING lock is needed before acquiring a SHARED lock and before
  1.1817 +  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
  1.1818 +  ** be released.
  1.1819 +  */
  1.1820 +  if( locktype==SHARED_LOCK 
  1.1821 +      || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
  1.1822 +  ){
  1.1823 +    int failed;
  1.1824 +    failed = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 1);
  1.1825 +    if (failed) {
  1.1826 +      rc = failed;
  1.1827 +      goto afp_end_lock;
  1.1828 +    }
  1.1829 +  }
  1.1830 +  
  1.1831 +  /* If control gets to this point, then actually go ahead and make
  1.1832 +  ** operating system calls for the specified lock.
  1.1833 +  */
  1.1834 +  if( locktype==SHARED_LOCK ){
  1.1835 +    int lk, lrc1, lrc2, lrc1Errno;
  1.1836 +    
  1.1837 +    /* Now get the read-lock SHARED_LOCK */
  1.1838 +    /* note that the quality of the randomness doesn't matter that much */
  1.1839 +    lk = random(); 
  1.1840 +    context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
  1.1841 +    lrc1 = _AFPFSSetLock(context->filePath, pFile, 
  1.1842 +          SHARED_FIRST+context->sharedLockByte, 1, 1);
  1.1843 +    if( IS_LOCK_ERROR(lrc1) ){
  1.1844 +      lrc1Errno = pFile->lastErrno;
  1.1845 +    }
  1.1846 +    /* Drop the temporary PENDING lock */
  1.1847 +    lrc2 = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 0);
  1.1848 +    
  1.1849 +    if( IS_LOCK_ERROR(lrc1) ) {
  1.1850 +      pFile->lastErrno = lrc1Errno;
  1.1851 +      rc = lrc1;
  1.1852 +      goto afp_end_lock;
  1.1853 +    } else if( IS_LOCK_ERROR(lrc2) ){
  1.1854 +      rc = lrc2;
  1.1855 +      goto afp_end_lock;
  1.1856 +    } else if( lrc1 != SQLITE_OK ) {
  1.1857 +      rc = lrc1;
  1.1858 +    } else {
  1.1859 +      pFile->locktype = SHARED_LOCK;
  1.1860 +    }
  1.1861 +  }else{
  1.1862 +    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
  1.1863 +    ** assumed that there is a SHARED or greater lock on the file
  1.1864 +    ** already.
  1.1865 +    */
  1.1866 +    int failed = 0;
  1.1867 +    assert( 0!=pFile->locktype );
  1.1868 +    if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
  1.1869 +        /* Acquire a RESERVED lock */
  1.1870 +        failed = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
  1.1871 +    }
  1.1872 +    if (!failed && locktype == EXCLUSIVE_LOCK) {
  1.1873 +      /* Acquire an EXCLUSIVE lock */
  1.1874 +        
  1.1875 +      /* Remove the shared lock before trying the range.  we'll need to 
  1.1876 +      ** reestablish the shared lock if we can't get the  afpUnlock
  1.1877 +      */
  1.1878 +      if (!(failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST +
  1.1879 +                         context->sharedLockByte, 1, 0))) {
  1.1880 +        /* now attemmpt to get the exclusive lock range */
  1.1881 +        failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST, 
  1.1882 +                               SHARED_SIZE, 1);
  1.1883 +        if (failed && (failed = _AFPFSSetLock(context->filePath, pFile, 
  1.1884 +                       SHARED_FIRST + context->sharedLockByte, 1, 1))) {
  1.1885 +          rc = failed;
  1.1886 +        }
  1.1887 +      } else {
  1.1888 +        rc = failed; 
  1.1889 +      }
  1.1890 +    }
  1.1891 +    if( failed ){
  1.1892 +      rc = failed;
  1.1893 +    }
  1.1894 +  }
  1.1895 +  
  1.1896 +  if( rc==SQLITE_OK ){
  1.1897 +    pFile->locktype = locktype;
  1.1898 +  }else if( locktype==EXCLUSIVE_LOCK ){
  1.1899 +    pFile->locktype = PENDING_LOCK;
  1.1900 +  }
  1.1901 +  
  1.1902 +afp_end_lock:
  1.1903 +  leaveMutex();
  1.1904 +  OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
  1.1905 +         rc==SQLITE_OK ? "ok" : "failed");
  1.1906 +  return rc;
  1.1907 +}
  1.1908 +
  1.1909 +/*
  1.1910 +** Lower the locking level on file descriptor pFile to locktype.  locktype
  1.1911 +** must be either NO_LOCK or SHARED_LOCK.
  1.1912 +**
  1.1913 +** If the locking level of the file descriptor is already at or below
  1.1914 +** the requested locking level, this routine is a no-op.
  1.1915 +*/
  1.1916 +static int afpUnlock(sqlite3_file *id, int locktype) {
  1.1917 +  int rc = SQLITE_OK;
  1.1918 +  unixFile *pFile = (unixFile*)id;
  1.1919 +  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  1.1920 +
  1.1921 +  assert( pFile );
  1.1922 +  OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
  1.1923 +         pFile->locktype, getpid());
  1.1924 +
  1.1925 +  assert( locktype<=SHARED_LOCK );
  1.1926 +  if( pFile->locktype<=locktype ){
  1.1927 +    return SQLITE_OK;
  1.1928 +  }
  1.1929 +  if( CHECK_THREADID(pFile) ){
  1.1930 +    return SQLITE_MISUSE;
  1.1931 +  }
  1.1932 +  enterMutex();
  1.1933 +  int failed = SQLITE_OK;
  1.1934 +  if( pFile->locktype>SHARED_LOCK ){
  1.1935 +    if( locktype==SHARED_LOCK ){
  1.1936 +
  1.1937 +      /* unlock the exclusive range - then re-establish the shared lock */
  1.1938 +      if (pFile->locktype==EXCLUSIVE_LOCK) {
  1.1939 +        failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST, 
  1.1940 +                                 SHARED_SIZE, 0);
  1.1941 +        if (!failed) {
  1.1942 +          /* successfully removed the exclusive lock */
  1.1943 +          if ((failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST+
  1.1944 +                            context->sharedLockByte, 1, 1))) {
  1.1945 +            /* failed to re-establish our shared lock */
  1.1946 +            rc = failed;
  1.1947 +          }
  1.1948 +        } else {
  1.1949 +          rc = failed;
  1.1950 +        } 
  1.1951 +      }
  1.1952 +    }
  1.1953 +    if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
  1.1954 +      if ((failed = _AFPFSSetLock(context->filePath, pFile, 
  1.1955 +                                  PENDING_BYTE, 1, 0))){
  1.1956 +        /* failed to release the pending lock */
  1.1957 +        rc = failed; 
  1.1958 +      }
  1.1959 +    } 
  1.1960 +    if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
  1.1961 +      if ((failed = _AFPFSSetLock(context->filePath, pFile, 
  1.1962 +                                  RESERVED_BYTE, 1, 0))) {
  1.1963 +        /* failed to release the reserved lock */
  1.1964 +        rc = failed;  
  1.1965 +      }
  1.1966 +    } 
  1.1967 +  }
  1.1968 +  if( locktype==NO_LOCK ){
  1.1969 +    int failed = _AFPFSSetLock(context->filePath, pFile, 
  1.1970 +                               SHARED_FIRST + context->sharedLockByte, 1, 0);
  1.1971 +    if (failed) {
  1.1972 +      rc = failed;  
  1.1973 +    }
  1.1974 +  }
  1.1975 +  if (rc == SQLITE_OK)
  1.1976 +    pFile->locktype = locktype;
  1.1977 +  leaveMutex();
  1.1978 +  return rc;
  1.1979 +}
  1.1980 +
  1.1981 +/*
  1.1982 +** Close a file & cleanup AFP specific locking context 
  1.1983 +*/
  1.1984 +static int afpClose(sqlite3_file *id) {
  1.1985 +  if( id ){
  1.1986 +    unixFile *pFile = (unixFile*)id;
  1.1987 +    afpUnlock(id, NO_LOCK);
  1.1988 +    sqlite3_free(pFile->lockingContext);
  1.1989 +  }
  1.1990 +  return closeUnixFile(id);
  1.1991 +}
  1.1992 +
  1.1993 +
  1.1994 +#pragma mark flock() style locking
  1.1995 +
  1.1996 +/*
  1.1997 +** The flockLockingContext is not used
  1.1998 +*/
  1.1999 +typedef void flockLockingContext;
  1.2000 +
  1.2001 +/* flock-style reserved lock checking following the behavior of 
  1.2002 + ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
  1.2003 +static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
  1.2004 +  int rc = SQLITE_OK;
  1.2005 +  int reserved = 0;
  1.2006 +  unixFile *pFile = (unixFile*)id;
  1.2007 +  
  1.2008 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  1.2009 +  
  1.2010 +  assert( pFile );
  1.2011 +  
  1.2012 +  /* Check if a thread in this process holds such a lock */
  1.2013 +  if( pFile->locktype>SHARED_LOCK ){
  1.2014 +    reserved = 1;
  1.2015 +  }
  1.2016 +  
  1.2017 +  /* Otherwise see if some other process holds it. */
  1.2018 +  if( !reserved ){
  1.2019 +    /* attempt to get the lock */
  1.2020 +    int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
  1.2021 +    if( !lrc ){
  1.2022 +      /* got the lock, unlock it */
  1.2023 +      lrc = flock(pFile->h, LOCK_UN);
  1.2024 +      if ( lrc ) {
  1.2025 +        int tErrno = errno;
  1.2026 +        /* unlock failed with an error */
  1.2027 +        lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); 
  1.2028 +        if( IS_LOCK_ERROR(lrc) ){
  1.2029 +          pFile->lastErrno = tErrno;
  1.2030 +          rc = lrc;
  1.2031 +        }
  1.2032 +      }
  1.2033 +    } else {
  1.2034 +      int tErrno = errno;
  1.2035 +      reserved = 1;
  1.2036 +      /* someone else might have it reserved */
  1.2037 +      lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); 
  1.2038 +      if( IS_LOCK_ERROR(lrc) ){
  1.2039 +        pFile->lastErrno = tErrno;
  1.2040 +        rc = lrc;
  1.2041 +      }
  1.2042 +    }
  1.2043 +  }
  1.2044 +  OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
  1.2045 +
  1.2046 +  *pResOut = reserved;
  1.2047 +  return rc;
  1.2048 +}
  1.2049 +
  1.2050 +static int flockLock(sqlite3_file *id, int locktype) {
  1.2051 +  int rc = SQLITE_OK;
  1.2052 +  unixFile *pFile = (unixFile*)id;
  1.2053 +
  1.2054 +  assert( pFile );
  1.2055 +
  1.2056 +  /* if we already have a lock, it is exclusive.  
  1.2057 +  ** Just adjust level and punt on outta here. */
  1.2058 +  if (pFile->locktype > NO_LOCK) {
  1.2059 +    pFile->locktype = locktype;
  1.2060 +    return SQLITE_OK;
  1.2061 +  }
  1.2062 +  
  1.2063 +  /* grab an exclusive lock */
  1.2064 +  
  1.2065 +  if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
  1.2066 +    int tErrno = errno;
  1.2067 +    /* didn't get, must be busy */
  1.2068 +    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  1.2069 +    if( IS_LOCK_ERROR(rc) ){
  1.2070 +      pFile->lastErrno = tErrno;
  1.2071 +    }
  1.2072 +  } else {
  1.2073 +    /* got it, set the type and return ok */
  1.2074 +    pFile->locktype = locktype;
  1.2075 +  }
  1.2076 +  OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
  1.2077 +           rc==SQLITE_OK ? "ok" : "failed");
  1.2078 +  return rc;
  1.2079 +}
  1.2080 +
  1.2081 +static int flockUnlock(sqlite3_file *id, int locktype) {
  1.2082 +  unixFile *pFile = (unixFile*)id;
  1.2083 +  
  1.2084 +  assert( pFile );
  1.2085 +  OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
  1.2086 +           pFile->locktype, getpid());
  1.2087 +  assert( locktype<=SHARED_LOCK );
  1.2088 +  
  1.2089 +  /* no-op if possible */
  1.2090 +  if( pFile->locktype==locktype ){
  1.2091 +    return SQLITE_OK;
  1.2092 +  }
  1.2093 +  
  1.2094 +  /* shared can just be set because we always have an exclusive */
  1.2095 +  if (locktype==SHARED_LOCK) {
  1.2096 +    pFile->locktype = locktype;
  1.2097 +    return SQLITE_OK;
  1.2098 +  }
  1.2099 +  
  1.2100 +  /* no, really, unlock. */
  1.2101 +  int rc = flock(pFile->h, LOCK_UN);
  1.2102 +  if (rc) {
  1.2103 +    int r, tErrno = errno;
  1.2104 +    r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
  1.2105 +    if( IS_LOCK_ERROR(r) ){
  1.2106 +      pFile->lastErrno = tErrno;
  1.2107 +    }
  1.2108 +    return r;
  1.2109 +  } else {
  1.2110 +    pFile->locktype = NO_LOCK;
  1.2111 +    return SQLITE_OK;
  1.2112 +  }
  1.2113 +}
  1.2114 +
  1.2115 +/*
  1.2116 +** Close a file.
  1.2117 +*/
  1.2118 +static int flockClose(sqlite3_file *id) {
  1.2119 +  if( id ){
  1.2120 +    flockUnlock(id, NO_LOCK);
  1.2121 +  }
  1.2122 +  return closeUnixFile(id);
  1.2123 +}
  1.2124 +
  1.2125 +#pragma mark Old-School .lock file based locking
  1.2126 +
  1.2127 +/* Dotlock-style reserved lock checking following the behavior of 
  1.2128 +** unixCheckReservedLock, see the unixCheckReservedLock function comments */
  1.2129 +static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
  1.2130 +  int rc = SQLITE_OK;
  1.2131 +  int reserved = 0;
  1.2132 +  unixFile *pFile = (unixFile*)id;
  1.2133 +
  1.2134 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  1.2135 +  
  1.2136 +  assert( pFile );
  1.2137 +
  1.2138 +  /* Check if a thread in this process holds such a lock */
  1.2139 +  if( pFile->locktype>SHARED_LOCK ){
  1.2140 +    reserved = 1;
  1.2141 +  }
  1.2142 +  
  1.2143 +  /* Otherwise see if some other process holds it. */
  1.2144 +  if( !reserved ){
  1.2145 +    char *zLockFile = (char *)pFile->lockingContext;
  1.2146 +    struct stat statBuf;
  1.2147 +    
  1.2148 +    if( lstat(zLockFile, &statBuf)==0 ){
  1.2149 +      /* file exists, someone else has the lock */
  1.2150 +      reserved = 1;
  1.2151 +    }else{
  1.2152 +      /* file does not exist, we could have it if we want it */
  1.2153 +			int tErrno = errno;
  1.2154 +      if( ENOENT != tErrno ){
  1.2155 +        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
  1.2156 +        pFile->lastErrno = tErrno;
  1.2157 +      }
  1.2158 +    }
  1.2159 +  }
  1.2160 +  OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
  1.2161 +
  1.2162 +  *pResOut = reserved;
  1.2163 +  return rc;
  1.2164 +}
  1.2165 +
  1.2166 +static int dotlockLock(sqlite3_file *id, int locktype) {
  1.2167 +  unixFile *pFile = (unixFile*)id;
  1.2168 +  int fd;
  1.2169 +  char *zLockFile = (char *)pFile->lockingContext;
  1.2170 +  int rc=SQLITE_OK;
  1.2171 +
  1.2172 +  /* if we already have a lock, it is exclusive.  
  1.2173 +  ** Just adjust level and punt on outta here. */
  1.2174 +  if (pFile->locktype > NO_LOCK) {
  1.2175 +    pFile->locktype = locktype;
  1.2176 +    
  1.2177 +    /* Always update the timestamp on the old file */
  1.2178 +    utimes(zLockFile, NULL);
  1.2179 +    rc = SQLITE_OK;
  1.2180 +    goto dotlock_end_lock;
  1.2181 +  }
  1.2182 +  
  1.2183 +  /* check to see if lock file already exists */
  1.2184 +  struct stat statBuf;
  1.2185 +  if (lstat(zLockFile,&statBuf) == 0){
  1.2186 +    rc = SQLITE_BUSY; /* it does, busy */
  1.2187 +    goto dotlock_end_lock;
  1.2188 +  }
  1.2189 +  
  1.2190 +  /* grab an exclusive lock */
  1.2191 +  fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
  1.2192 +  if( fd<0 ){
  1.2193 +    /* failed to open/create the file, someone else may have stolen the lock */
  1.2194 +    int tErrno = errno;
  1.2195 +    if( EEXIST == tErrno ){
  1.2196 +      rc = SQLITE_BUSY;
  1.2197 +    } else {
  1.2198 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  1.2199 +      if( IS_LOCK_ERROR(rc) ){
  1.2200 +	pFile->lastErrno = tErrno;
  1.2201 +      }
  1.2202 +    }
  1.2203 +    goto dotlock_end_lock;
  1.2204 +  } 
  1.2205 +  close(fd);
  1.2206 +  
  1.2207 +  /* got it, set the type and return ok */
  1.2208 +  pFile->locktype = locktype;
  1.2209 +
  1.2210 + dotlock_end_lock:
  1.2211 +  return rc;
  1.2212 +}
  1.2213 +
  1.2214 +static int dotlockUnlock(sqlite3_file *id, int locktype) {
  1.2215 +  unixFile *pFile = (unixFile*)id;
  1.2216 +  char *zLockFile = (char *)pFile->lockingContext;
  1.2217 +
  1.2218 +  assert( pFile );
  1.2219 +  OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
  1.2220 +	   pFile->locktype, getpid());
  1.2221 +  assert( locktype<=SHARED_LOCK );
  1.2222 +  
  1.2223 +  /* no-op if possible */
  1.2224 +  if( pFile->locktype==locktype ){
  1.2225 +    return SQLITE_OK;
  1.2226 +  }
  1.2227 +  
  1.2228 +  /* shared can just be set because we always have an exclusive */
  1.2229 +  if (locktype==SHARED_LOCK) {
  1.2230 +    pFile->locktype = locktype;
  1.2231 +    return SQLITE_OK;
  1.2232 +  }
  1.2233 +  
  1.2234 +  /* no, really, unlock. */
  1.2235 +  if (unlink(zLockFile) ) {
  1.2236 +    int rc, tErrno = errno;
  1.2237 +    if( ENOENT != tErrno ){
  1.2238 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
  1.2239 +    }
  1.2240 +    if( IS_LOCK_ERROR(rc) ){
  1.2241 +      pFile->lastErrno = tErrno;
  1.2242 +    }
  1.2243 +    return rc; 
  1.2244 +  }
  1.2245 +  pFile->locktype = NO_LOCK;
  1.2246 +  return SQLITE_OK;
  1.2247 +}
  1.2248 +
  1.2249 +/*
  1.2250 + ** Close a file.
  1.2251 + */
  1.2252 +static int dotlockClose(sqlite3_file *id) {
  1.2253 +  if( id ){
  1.2254 +    unixFile *pFile = (unixFile*)id;
  1.2255 +    dotlockUnlock(id, NO_LOCK);
  1.2256 +    sqlite3_free(pFile->lockingContext);
  1.2257 +  }
  1.2258 +  return closeUnixFile(id);
  1.2259 +}
  1.2260 +
  1.2261 +
  1.2262 +#endif /* SQLITE_ENABLE_LOCKING_STYLE */
  1.2263 +
  1.2264 +/*
  1.2265 +** The nolockLockingContext is void
  1.2266 +*/
  1.2267 +typedef void nolockLockingContext;
  1.2268 +
  1.2269 +static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) {
  1.2270 +  *pResOut = 0;
  1.2271 +  return SQLITE_OK;
  1.2272 +}
  1.2273 +
  1.2274 +static int nolockLock(sqlite3_file *id, int locktype) {
  1.2275 +  return SQLITE_OK;
  1.2276 +}
  1.2277 +
  1.2278 +static int nolockUnlock(sqlite3_file *id, int locktype) {
  1.2279 +  return SQLITE_OK;
  1.2280 +}
  1.2281 +
  1.2282 +/*
  1.2283 +** Close a file.
  1.2284 +*/
  1.2285 +static int nolockClose(sqlite3_file *id) {
  1.2286 +  return closeUnixFile(id);
  1.2287 +}
  1.2288 +
  1.2289 +
  1.2290 +/*
  1.2291 +** Information and control of an open file handle.
  1.2292 +*/
  1.2293 +static int unixFileControl(sqlite3_file *id, int op, void *pArg){
  1.2294 +  switch( op ){
  1.2295 +    case SQLITE_FCNTL_LOCKSTATE: {
  1.2296 +      *(int*)pArg = ((unixFile*)id)->locktype;
  1.2297 +      return SQLITE_OK;
  1.2298 +    }
  1.2299 +  }
  1.2300 +  return SQLITE_ERROR;
  1.2301 +}
  1.2302 +
  1.2303 +/*
  1.2304 +** Return the sector size in bytes of the underlying block device for
  1.2305 +** the specified file. This is almost always 512 bytes, but may be
  1.2306 +** larger for some devices.
  1.2307 +**
  1.2308 +** SQLite code assumes this function cannot fail. It also assumes that
  1.2309 +** if two files are created in the same file-system directory (i.e.
  1.2310 +** a database and its journal file) that the sector size will be the
  1.2311 +** same for both.
  1.2312 +*/
  1.2313 +static int unixSectorSize(sqlite3_file *id){
  1.2314 +  return SQLITE_DEFAULT_SECTOR_SIZE;
  1.2315 +}
  1.2316 +
  1.2317 +/*
  1.2318 +** Return the device characteristics for the file. This is always 0.
  1.2319 +*/
  1.2320 +static int unixDeviceCharacteristics(sqlite3_file *id){
  1.2321 +  return 0;
  1.2322 +}
  1.2323 +
  1.2324 +/*
  1.2325 +** Initialize the contents of the unixFile structure pointed to by pId.
  1.2326 +**
  1.2327 +** When locking extensions are enabled, the filepath and locking style 
  1.2328 +** are needed to determine the unixFile pMethod to use for locking operations.
  1.2329 +** The locking-style specific lockingContext data structure is created 
  1.2330 +** and assigned here also.
  1.2331 +*/
  1.2332 +static int fillInUnixFile(
  1.2333 +  sqlite3_vfs *pVfs,      /* Pointer to vfs object */
  1.2334 +  int h,                  /* Open file descriptor of file being opened */
  1.2335 +  int dirfd,              /* Directory file descriptor */
  1.2336 +  sqlite3_file *pId,      /* Write to the unixFile structure here */
  1.2337 +  const char *zFilename,  /* Name of the file being opened */
  1.2338 +  int noLock              /* Omit locking if true */
  1.2339 +){
  1.2340 +  int eLockingStyle;
  1.2341 +  unixFile *pNew = (unixFile *)pId;
  1.2342 +  int rc = SQLITE_OK;
  1.2343 +
  1.2344 +  /* Macro to define the static contents of an sqlite3_io_methods 
  1.2345 +  ** structure for a unix backend file. Different locking methods
  1.2346 +  ** require different functions for the xClose, xLock, xUnlock and
  1.2347 +  ** xCheckReservedLock methods.
  1.2348 +  */
  1.2349 +  #define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) {    \
  1.2350 +    1,                          /* iVersion */                           \
  1.2351 +    xClose,                     /* xClose */                             \
  1.2352 +    unixRead,                   /* xRead */                              \
  1.2353 +    unixWrite,                  /* xWrite */                             \
  1.2354 +    unixTruncate,               /* xTruncate */                          \
  1.2355 +    unixSync,                   /* xSync */                              \
  1.2356 +    unixFileSize,               /* xFileSize */                          \
  1.2357 +    xLock,                      /* xLock */                              \
  1.2358 +    xUnlock,                    /* xUnlock */                            \
  1.2359 +    xCheckReservedLock,         /* xCheckReservedLock */                 \
  1.2360 +    unixFileControl,            /* xFileControl */                       \
  1.2361 +    unixSectorSize,             /* xSectorSize */                        \
  1.2362 +    unixDeviceCharacteristics   /* xDeviceCapabilities */                \
  1.2363 +  }
  1.2364 +  static sqlite3_io_methods aIoMethod[] = {
  1.2365 +    IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock) 
  1.2366 +   ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
  1.2367 +#if SQLITE_ENABLE_LOCKING_STYLE
  1.2368 +   ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
  1.2369 +   ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
  1.2370 +   ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
  1.2371 +#endif
  1.2372 +  };
  1.2373 +  /* The order of the IOMETHODS macros above is important.  It must be the
  1.2374 +  ** same order as the LOCKING_STYLE numbers
  1.2375 +  */
  1.2376 +  assert(LOCKING_STYLE_POSIX==1);
  1.2377 +  assert(LOCKING_STYLE_NONE==2);
  1.2378 +  assert(LOCKING_STYLE_DOTFILE==3);
  1.2379 +  assert(LOCKING_STYLE_FLOCK==4);
  1.2380 +  assert(LOCKING_STYLE_AFP==5);
  1.2381 +
  1.2382 +  assert( pNew->pLock==NULL );
  1.2383 +  assert( pNew->pOpen==NULL );
  1.2384 +
  1.2385 +  OSTRACE3("OPEN    %-3d %s\n", h, zFilename);    
  1.2386 +  pNew->h = h;
  1.2387 +  pNew->dirfd = dirfd;
  1.2388 +  SET_THREADID(pNew);
  1.2389 +
  1.2390 +  if( noLock ){
  1.2391 +    eLockingStyle = LOCKING_STYLE_NONE;
  1.2392 +  }else{
  1.2393 +    eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
  1.2394 +  }
  1.2395 +
  1.2396 +  switch( eLockingStyle ){
  1.2397 +
  1.2398 +    case LOCKING_STYLE_POSIX: {
  1.2399 +      enterMutex();
  1.2400 +      rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
  1.2401 +      leaveMutex();
  1.2402 +      break;
  1.2403 +    }
  1.2404 +
  1.2405 +#if SQLITE_ENABLE_LOCKING_STYLE
  1.2406 +    case LOCKING_STYLE_AFP: {
  1.2407 +      /* AFP locking uses the file path so it needs to be included in
  1.2408 +      ** the afpLockingContext.
  1.2409 +      */
  1.2410 +      afpLockingContext *pCtx;
  1.2411 +      pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
  1.2412 +      if( pCtx==0 ){
  1.2413 +        rc = SQLITE_NOMEM;
  1.2414 +      }else{
  1.2415 +        /* NB: zFilename exists and remains valid until the file is closed
  1.2416 +        ** according to requirement F11141.  So we do not need to make a
  1.2417 +        ** copy of the filename. */
  1.2418 +        pCtx->filePath = zFilename;
  1.2419 +        srandomdev();
  1.2420 +      }
  1.2421 +      break;
  1.2422 +    }
  1.2423 +
  1.2424 +    case LOCKING_STYLE_DOTFILE: {
  1.2425 +      /* Dotfile locking uses the file path so it needs to be included in
  1.2426 +      ** the dotlockLockingContext 
  1.2427 +      */
  1.2428 +      char *zLockFile;
  1.2429 +      int nFilename;
  1.2430 +      nFilename = strlen(zFilename) + 6;
  1.2431 +      zLockFile = (char *)sqlite3_malloc(nFilename);
  1.2432 +      if( zLockFile==0 ){
  1.2433 +        rc = SQLITE_NOMEM;
  1.2434 +      }else{
  1.2435 +        sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
  1.2436 +      }
  1.2437 +      pNew->lockingContext = zLockFile;
  1.2438 +      break;
  1.2439 +    }
  1.2440 +
  1.2441 +    case LOCKING_STYLE_FLOCK: 
  1.2442 +    case LOCKING_STYLE_NONE: 
  1.2443 +      break;
  1.2444 +#endif
  1.2445 +  }
  1.2446 +  
  1.2447 +  pNew->lastErrno = 0;
  1.2448 +  if( rc!=SQLITE_OK ){
  1.2449 +    if( dirfd>=0 ) close(dirfd);
  1.2450 +    close(h);
  1.2451 +  }else{
  1.2452 +    pNew->pMethod = &aIoMethod[eLockingStyle-1];
  1.2453 +    OpenCounter(+1);
  1.2454 +  }
  1.2455 +  return rc;
  1.2456 +}
  1.2457 +
  1.2458 +/*
  1.2459 +** Open a file descriptor to the directory containing file zFilename.
  1.2460 +** If successful, *pFd is set to the opened file descriptor and
  1.2461 +** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
  1.2462 +** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
  1.2463 +** value.
  1.2464 +**
  1.2465 +** If SQLITE_OK is returned, the caller is responsible for closing
  1.2466 +** the file descriptor *pFd using close().
  1.2467 +*/
  1.2468 +static int openDirectory(const char *zFilename, int *pFd){
  1.2469 +  int ii;
  1.2470 +  int fd = -1;
  1.2471 +  char zDirname[MAX_PATHNAME+1];
  1.2472 +
  1.2473 +  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
  1.2474 +  for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
  1.2475 +  if( ii>0 ){
  1.2476 +    zDirname[ii] = '\0';
  1.2477 +    fd = open(zDirname, O_RDONLY|O_BINARY, 0);
  1.2478 +    if( fd>=0 ){
  1.2479 +#ifdef FD_CLOEXEC
  1.2480 +      fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
  1.2481 +#endif
  1.2482 +      OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
  1.2483 +    }
  1.2484 +  }
  1.2485 +  *pFd = fd;
  1.2486 +  return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
  1.2487 +}
  1.2488 +
  1.2489 +/*
  1.2490 +** Create a temporary file name in zBuf.  zBuf must be allocated
  1.2491 +** by the calling process and must be big enough to hold at least
  1.2492 +** pVfs->mxPathname bytes.
  1.2493 +*/
  1.2494 +static int getTempname(int nBuf, char *zBuf){
  1.2495 +  static const char *azDirs[] = {
  1.2496 +     0,
  1.2497 +     "/var/tmp",
  1.2498 +     "/usr/tmp",
  1.2499 +     "/tmp",
  1.2500 +     ".",
  1.2501 +  };
  1.2502 +  static const unsigned char zChars[] =
  1.2503 +    "abcdefghijklmnopqrstuvwxyz"
  1.2504 +    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1.2505 +    "0123456789";
  1.2506 +  int i, j;
  1.2507 +  struct stat buf;
  1.2508 +  const char *zDir = ".";
  1.2509 +
  1.2510 +  /* It's odd to simulate an io-error here, but really this is just
  1.2511 +  ** using the io-error infrastructure to test that SQLite handles this
  1.2512 +  ** function failing. 
  1.2513 +  */
  1.2514 +  SimulateIOError( return SQLITE_IOERR );
  1.2515 +
  1.2516 +  azDirs[0] = sqlite3_temp_directory;
  1.2517 +  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
  1.2518 +    if( azDirs[i]==0 ) continue;
  1.2519 +    if( stat(azDirs[i], &buf) ) continue;
  1.2520 +    if( !S_ISDIR(buf.st_mode) ) continue;
  1.2521 +    if( access(azDirs[i], 07) ) continue;
  1.2522 +    zDir = azDirs[i];
  1.2523 +    break;
  1.2524 +  }
  1.2525 +
  1.2526 +  /* Check that the output buffer is large enough for the temporary file 
  1.2527 +  ** name. If it is not, return SQLITE_ERROR.
  1.2528 +  */
  1.2529 +  if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
  1.2530 +    return SQLITE_ERROR;
  1.2531 +  }
  1.2532 +
  1.2533 +  do{
  1.2534 +    sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
  1.2535 +    j = strlen(zBuf);
  1.2536 +    sqlite3_randomness(15, &zBuf[j]);
  1.2537 +    for(i=0; i<15; i++, j++){
  1.2538 +      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
  1.2539 +    }
  1.2540 +    zBuf[j] = 0;
  1.2541 +  }while( access(zBuf,0)==0 );
  1.2542 +  return SQLITE_OK;
  1.2543 +}
  1.2544 +
  1.2545 +
  1.2546 +/*
  1.2547 +** Open the file zPath.
  1.2548 +** 
  1.2549 +** Previously, the SQLite OS layer used three functions in place of this
  1.2550 +** one:
  1.2551 +**
  1.2552 +**     sqlite3OsOpenReadWrite();
  1.2553 +**     sqlite3OsOpenReadOnly();
  1.2554 +**     sqlite3OsOpenExclusive();
  1.2555 +**
  1.2556 +** These calls correspond to the following combinations of flags:
  1.2557 +**
  1.2558 +**     ReadWrite() ->     (READWRITE | CREATE)
  1.2559 +**     ReadOnly()  ->     (READONLY) 
  1.2560 +**     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
  1.2561 +**
  1.2562 +** The old OpenExclusive() accepted a boolean argument - "delFlag". If
  1.2563 +** true, the file was configured to be automatically deleted when the
  1.2564 +** file handle closed. To achieve the same effect using this new 
  1.2565 +** interface, add the DELETEONCLOSE flag to those specified above for 
  1.2566 +** OpenExclusive().
  1.2567 +*/
  1.2568 +static int unixOpen(
  1.2569 +  sqlite3_vfs *pVfs, 
  1.2570 +  const char *zPath, 
  1.2571 +  sqlite3_file *pFile,
  1.2572 +  int flags,
  1.2573 +  int *pOutFlags
  1.2574 +){
  1.2575 +  int fd = 0;                    /* File descriptor returned by open() */
  1.2576 +  int dirfd = -1;                /* Directory file descriptor */
  1.2577 +  int oflags = 0;                /* Flags to pass to open() */
  1.2578 +  int eType = flags&0xFFFFFF00;  /* Type of file to open */
  1.2579 +  int noLock;                    /* True to omit locking primitives */
  1.2580 +
  1.2581 +  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
  1.2582 +  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  1.2583 +  int isCreate     = (flags & SQLITE_OPEN_CREATE);
  1.2584 +  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  1.2585 +  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
  1.2586 +
  1.2587 +  /* If creating a master or main-file journal, this function will open
  1.2588 +  ** a file-descriptor on the directory too. The first time unixSync()
  1.2589 +  ** is called the directory file descriptor will be fsync()ed and close()d.
  1.2590 +  */
  1.2591 +  int isOpenDirectory = (isCreate && 
  1.2592 +      (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
  1.2593 +  );
  1.2594 +
  1.2595 +  /* If argument zPath is a NULL pointer, this function is required to open
  1.2596 +  ** a temporary file. Use this buffer to store the file name in.
  1.2597 +  */
  1.2598 +  char zTmpname[MAX_PATHNAME+1];
  1.2599 +  const char *zName = zPath;
  1.2600 +
  1.2601 +  /* Check the following statements are true: 
  1.2602 +  **
  1.2603 +  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
  1.2604 +  **   (b) if CREATE is set, then READWRITE must also be set, and
  1.2605 +  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
  1.2606 +  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
  1.2607 +  */
  1.2608 +  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
  1.2609 +  assert(isCreate==0 || isReadWrite);
  1.2610 +  assert(isExclusive==0 || isCreate);
  1.2611 +  assert(isDelete==0 || isCreate);
  1.2612 +
  1.2613 +  /* The main DB, main journal, and master journal are never automatically
  1.2614 +  ** deleted
  1.2615 +  */
  1.2616 +  assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
  1.2617 +  assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
  1.2618 +  assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
  1.2619 +
  1.2620 +  /* Assert that the upper layer has set one of the "file-type" flags. */
  1.2621 +  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
  1.2622 +       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
  1.2623 +       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
  1.2624 +       || eType==SQLITE_OPEN_TRANSIENT_DB
  1.2625 +  );
  1.2626 +
  1.2627 +  memset(pFile, 0, sizeof(unixFile));
  1.2628 +
  1.2629 +  if( !zName ){
  1.2630 +    int rc;
  1.2631 +    assert(isDelete && !isOpenDirectory);
  1.2632 +    rc = getTempname(MAX_PATHNAME+1, zTmpname);
  1.2633 +    if( rc!=SQLITE_OK ){
  1.2634 +      return rc;
  1.2635 +    }
  1.2636 +    zName = zTmpname;
  1.2637 +  }
  1.2638 +
  1.2639 +  if( isReadonly )  oflags |= O_RDONLY;
  1.2640 +  if( isReadWrite ) oflags |= O_RDWR;
  1.2641 +  if( isCreate )    oflags |= O_CREAT;
  1.2642 +  if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
  1.2643 +  oflags |= (O_LARGEFILE|O_BINARY);
  1.2644 +
  1.2645 +  fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
  1.2646 +  if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
  1.2647 +    /* Failed to open the file for read/write access. Try read-only. */
  1.2648 +    flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
  1.2649 +    flags |= SQLITE_OPEN_READONLY;
  1.2650 +    return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
  1.2651 +  }
  1.2652 +  if( fd<0 ){
  1.2653 +    return SQLITE_CANTOPEN;
  1.2654 +  }
  1.2655 +  if( isDelete ){
  1.2656 +    unlink(zName);
  1.2657 +  }
  1.2658 +  if( pOutFlags ){
  1.2659 +    *pOutFlags = flags;
  1.2660 +  }
  1.2661 +
  1.2662 +  assert(fd!=0);
  1.2663 +  if( isOpenDirectory ){
  1.2664 +    int rc = openDirectory(zPath, &dirfd);
  1.2665 +    if( rc!=SQLITE_OK ){
  1.2666 +      close(fd);
  1.2667 +      return rc;
  1.2668 +    }
  1.2669 +  }
  1.2670 +
  1.2671 +#ifdef FD_CLOEXEC
  1.2672 +  fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
  1.2673 +#endif
  1.2674 +
  1.2675 +  noLock = eType!=SQLITE_OPEN_MAIN_DB;
  1.2676 +  return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock);
  1.2677 +}
  1.2678 +
  1.2679 +/*
  1.2680 +** Delete the file at zPath. If the dirSync argument is true, fsync()
  1.2681 +** the directory after deleting the file.
  1.2682 +*/
  1.2683 +static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
  1.2684 +  int rc = SQLITE_OK;
  1.2685 +  SimulateIOError(return SQLITE_IOERR_DELETE);
  1.2686 +  unlink(zPath);
  1.2687 +  if( dirSync ){
  1.2688 +    int fd;
  1.2689 +    rc = openDirectory(zPath, &fd);
  1.2690 +    if( rc==SQLITE_OK ){
  1.2691 +      if( fsync(fd) ){
  1.2692 +        rc = SQLITE_IOERR_DIR_FSYNC;
  1.2693 +      }
  1.2694 +      close(fd);
  1.2695 +    }
  1.2696 +  }
  1.2697 +  return rc;
  1.2698 +}
  1.2699 +
  1.2700 +/*
  1.2701 +** Test the existance of or access permissions of file zPath. The
  1.2702 +** test performed depends on the value of flags:
  1.2703 +**
  1.2704 +**     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
  1.2705 +**     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
  1.2706 +**     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
  1.2707 +**
  1.2708 +** Otherwise return 0.
  1.2709 +*/
  1.2710 +static int unixAccess(
  1.2711 +  sqlite3_vfs *pVfs, 
  1.2712 +  const char *zPath, 
  1.2713 +  int flags, 
  1.2714 +  int *pResOut
  1.2715 +){
  1.2716 +  int amode = 0;
  1.2717 +  SimulateIOError( return SQLITE_IOERR_ACCESS; );
  1.2718 +  switch( flags ){
  1.2719 +    case SQLITE_ACCESS_EXISTS:
  1.2720 +      amode = F_OK;
  1.2721 +      break;
  1.2722 +    case SQLITE_ACCESS_READWRITE:
  1.2723 +      amode = W_OK|R_OK;
  1.2724 +      break;
  1.2725 +    case SQLITE_ACCESS_READ:
  1.2726 +      amode = R_OK;
  1.2727 +      break;
  1.2728 +
  1.2729 +    default:
  1.2730 +      assert(!"Invalid flags argument");
  1.2731 +  }
  1.2732 +  *pResOut = (access(zPath, amode)==0);
  1.2733 +  return SQLITE_OK;
  1.2734 +}
  1.2735 +
  1.2736 +
  1.2737 +/*
  1.2738 +** Turn a relative pathname into a full pathname. The relative path
  1.2739 +** is stored as a nul-terminated string in the buffer pointed to by
  1.2740 +** zPath. 
  1.2741 +**
  1.2742 +** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes 
  1.2743 +** (in this case, MAX_PATHNAME bytes). The full-path is written to
  1.2744 +** this buffer before returning.
  1.2745 +*/
  1.2746 +static int unixFullPathname(
  1.2747 +  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
  1.2748 +  const char *zPath,            /* Possibly relative input path */
  1.2749 +  int nOut,                     /* Size of output buffer in bytes */
  1.2750 +  char *zOut                    /* Output buffer */
  1.2751 +){
  1.2752 +
  1.2753 +  /* It's odd to simulate an io-error here, but really this is just
  1.2754 +  ** using the io-error infrastructure to test that SQLite handles this
  1.2755 +  ** function failing. This function could fail if, for example, the
  1.2756 +  ** current working directly has been unlinked.
  1.2757 +  */
  1.2758 +  SimulateIOError( return SQLITE_ERROR );
  1.2759 +
  1.2760 +  assert( pVfs->mxPathname==MAX_PATHNAME );
  1.2761 +  zOut[nOut-1] = '\0';
  1.2762 +  if( zPath[0]=='/' ){
  1.2763 +    sqlite3_snprintf(nOut, zOut, "%s", zPath);
  1.2764 +  }else{
  1.2765 +    int nCwd;
  1.2766 +    if( getcwd(zOut, nOut-1)==0 ){
  1.2767 +      return SQLITE_CANTOPEN;
  1.2768 +    }
  1.2769 +    nCwd = strlen(zOut);
  1.2770 +    sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
  1.2771 +  }
  1.2772 +  return SQLITE_OK;
  1.2773 +
  1.2774 +#if 0
  1.2775 +  /*
  1.2776 +  ** Remove "/./" path elements and convert "/A/./" path elements
  1.2777 +  ** to just "/".
  1.2778 +  */
  1.2779 +  if( zFull ){
  1.2780 +    int i, j;
  1.2781 +    for(i=j=0; zFull[i]; i++){
  1.2782 +      if( zFull[i]=='/' ){
  1.2783 +        if( zFull[i+1]=='/' ) continue;
  1.2784 +        if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
  1.2785 +          i += 1;
  1.2786 +          continue;
  1.2787 +        }
  1.2788 +        if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
  1.2789 +          while( j>0 && zFull[j-1]!='/' ){ j--; }
  1.2790 +          i += 3;
  1.2791 +          continue;
  1.2792 +        }
  1.2793 +      }
  1.2794 +      zFull[j++] = zFull[i];
  1.2795 +    }
  1.2796 +    zFull[j] = 0;
  1.2797 +  }
  1.2798 +#endif
  1.2799 +}
  1.2800 +
  1.2801 +
  1.2802 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
  1.2803 +/*
  1.2804 +** Interfaces for opening a shared library, finding entry points
  1.2805 +** within the shared library, and closing the shared library.
  1.2806 +*/
  1.2807 +#include <dlfcn.h>
  1.2808 +static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
  1.2809 +  return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
  1.2810 +}
  1.2811 +
  1.2812 +/*
  1.2813 +** SQLite calls this function immediately after a call to unixDlSym() or
  1.2814 +** unixDlOpen() fails (returns a null pointer). If a more detailed error
  1.2815 +** message is available, it is written to zBufOut. If no error message
  1.2816 +** is available, zBufOut is left unmodified and SQLite uses a default
  1.2817 +** error message.
  1.2818 +*/
  1.2819 +static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
  1.2820 +  char *zErr;
  1.2821 +  enterMutex();
  1.2822 +  zErr = dlerror();
  1.2823 +  if( zErr ){
  1.2824 +    sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
  1.2825 +  }
  1.2826 +  leaveMutex();
  1.2827 +}
  1.2828 +static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
  1.2829 +  return dlsym(pHandle, zSymbol);
  1.2830 +}
  1.2831 +static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){
  1.2832 +  dlclose(pHandle);
  1.2833 +}
  1.2834 +#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
  1.2835 +  #define unixDlOpen  0
  1.2836 +  #define unixDlError 0
  1.2837 +  #define unixDlSym   0
  1.2838 +  #define unixDlClose 0
  1.2839 +#endif
  1.2840 +
  1.2841 +/*
  1.2842 +** Write nBuf bytes of random data to the supplied buffer zBuf.
  1.2843 +*/
  1.2844 +static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  1.2845 +
  1.2846 +  assert(nBuf>=(sizeof(time_t)+sizeof(int)));
  1.2847 +
  1.2848 +  /* We have to initialize zBuf to prevent valgrind from reporting
  1.2849 +  ** errors.  The reports issued by valgrind are incorrect - we would
  1.2850 +  ** prefer that the randomness be increased by making use of the
  1.2851 +  ** uninitialized space in zBuf - but valgrind errors tend to worry
  1.2852 +  ** some users.  Rather than argue, it seems easier just to initialize
  1.2853 +  ** the whole array and silence valgrind, even if that means less randomness
  1.2854 +  ** in the random seed.
  1.2855 +  **
  1.2856 +  ** When testing, initializing zBuf[] to zero is all we do.  That means
  1.2857 +  ** that we always use the same random number sequence.  This makes the
  1.2858 +  ** tests repeatable.
  1.2859 +  */
  1.2860 +  memset(zBuf, 0, nBuf);
  1.2861 +#if !defined(SQLITE_TEST)
  1.2862 +  {
  1.2863 +    int pid, fd;
  1.2864 +    fd = open("/dev/urandom", O_RDONLY);
  1.2865 +    if( fd<0 ){
  1.2866 +      time_t t;
  1.2867 +      time(&t);
  1.2868 +      memcpy(zBuf, &t, sizeof(t));
  1.2869 +      pid = getpid();
  1.2870 +      memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
  1.2871 +    }else{
  1.2872 +      read(fd, zBuf, nBuf);
  1.2873 +      close(fd);
  1.2874 +    }
  1.2875 +  }
  1.2876 +#endif
  1.2877 +  return SQLITE_OK;
  1.2878 +}
  1.2879 +
  1.2880 +
  1.2881 +/*
  1.2882 +** Sleep for a little while.  Return the amount of time slept.
  1.2883 +** The argument is the number of microseconds we want to sleep.
  1.2884 +** The return value is the number of microseconds of sleep actually
  1.2885 +** requested from the underlying operating system, a number which
  1.2886 +** might be greater than or equal to the argument, but not less
  1.2887 +** than the argument.
  1.2888 +*/
  1.2889 +static int unixSleep(sqlite3_vfs *pVfs, int microseconds){
  1.2890 +#if defined(HAVE_USLEEP) && HAVE_USLEEP
  1.2891 +  usleep(microseconds);
  1.2892 +  return microseconds;
  1.2893 +#else
  1.2894 +  int seconds = (microseconds+999999)/1000000;
  1.2895 +  sleep(seconds);
  1.2896 +  return seconds*1000000;
  1.2897 +#endif
  1.2898 +}
  1.2899 +
  1.2900 +/*
  1.2901 +** The following variable, if set to a non-zero value, becomes the result
  1.2902 +** returned from sqlite3OsCurrentTime().  This is used for testing.
  1.2903 +*/
  1.2904 +#ifdef SQLITE_TEST
  1.2905 +int sqlite3_current_time = 0;
  1.2906 +#endif
  1.2907 +
  1.2908 +/*
  1.2909 +** Find the current time (in Universal Coordinated Time).  Write the
  1.2910 +** current time and date as a Julian Day number into *prNow and
  1.2911 +** return 0.  Return 1 if the time and date cannot be found.
  1.2912 +*/
  1.2913 +static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){
  1.2914 +#ifdef NO_GETTOD
  1.2915 +  time_t t;
  1.2916 +  time(&t);
  1.2917 +  *prNow = t/86400.0 + 2440587.5;
  1.2918 +#else
  1.2919 +  struct timeval sNow;
  1.2920 +  gettimeofday(&sNow, 0);
  1.2921 +  *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
  1.2922 +#endif
  1.2923 +#ifdef SQLITE_TEST
  1.2924 +  if( sqlite3_current_time ){
  1.2925 +    *prNow = sqlite3_current_time/86400.0 + 2440587.5;
  1.2926 +  }
  1.2927 +#endif
  1.2928 +  return 0;
  1.2929 +}
  1.2930 +
  1.2931 +static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  1.2932 +  return 0;
  1.2933 +}
  1.2934 +
  1.2935 +/*
  1.2936 +** Initialize the operating system interface.
  1.2937 +*/
  1.2938 +int sqlite3_os_init(void){ 
  1.2939 +  /* Macro to define the static contents of an sqlite3_vfs structure for
  1.2940 +  ** the unix backend. The two parameters are the values to use for
  1.2941 +  ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
  1.2942 +  ** 
  1.2943 +  */
  1.2944 +  #define UNIXVFS(zVfsName, pVfsAppData) {                  \
  1.2945 +    1,                    /* iVersion */                    \
  1.2946 +    sizeof(unixFile),     /* szOsFile */                    \
  1.2947 +    MAX_PATHNAME,         /* mxPathname */                  \
  1.2948 +    0,                    /* pNext */                       \
  1.2949 +    zVfsName,             /* zName */                       \
  1.2950 +    (void *)pVfsAppData,  /* pAppData */                    \
  1.2951 +    unixOpen,             /* xOpen */                       \
  1.2952 +    unixDelete,           /* xDelete */                     \
  1.2953 +    unixAccess,           /* xAccess */                     \
  1.2954 +    unixFullPathname,     /* xFullPathname */               \
  1.2955 +    unixDlOpen,           /* xDlOpen */                     \
  1.2956 +    unixDlError,          /* xDlError */                    \
  1.2957 +    unixDlSym,            /* xDlSym */                      \
  1.2958 +    unixDlClose,          /* xDlClose */                    \
  1.2959 +    unixRandomness,       /* xRandomness */                 \
  1.2960 +    unixSleep,            /* xSleep */                      \
  1.2961 +    unixCurrentTime,      /* xCurrentTime */                \
  1.2962 +    unixGetLastError      /* xGetLastError */               \
  1.2963 +  }
  1.2964 +
  1.2965 +  static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
  1.2966 +#if SQLITE_ENABLE_LOCKING_STYLE
  1.2967 +  int i;
  1.2968 +  static sqlite3_vfs aVfs[] = {
  1.2969 +    UNIXVFS("unix-posix",   LOCKING_STYLE_POSIX), 
  1.2970 +    UNIXVFS("unix-afp",     LOCKING_STYLE_AFP), 
  1.2971 +    UNIXVFS("unix-flock",   LOCKING_STYLE_FLOCK), 
  1.2972 +    UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE), 
  1.2973 +    UNIXVFS("unix-none",    LOCKING_STYLE_NONE)
  1.2974 +  };
  1.2975 +  for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
  1.2976 +    sqlite3_vfs_register(&aVfs[i], 0);
  1.2977 +  }
  1.2978 +#endif
  1.2979 +  sqlite3_vfs_register(&unixVfs, 1);
  1.2980 +  return SQLITE_OK; 
  1.2981 +}
  1.2982 +
  1.2983 +/*
  1.2984 +** Shutdown the operating system interface. This is a no-op for unix.
  1.2985 +*/
  1.2986 +int sqlite3_os_end(void){ 
  1.2987 +  return SQLITE_OK; 
  1.2988 +}
  1.2989 + 
  1.2990 +#endif /* SQLITE_OS_UNIX */