| OLD | NEW | 
 | (Empty) | 
|     1 /* |  | 
|     2 ** 2004 May 22 |  | 
|     3 ** |  | 
|     4 ** The author disclaims copyright to this source code.  In place of |  | 
|     5 ** a legal notice, here is a blessing: |  | 
|     6 ** |  | 
|     7 **    May you do good and not evil. |  | 
|     8 **    May you find forgiveness for yourself and forgive others. |  | 
|     9 **    May you share freely, never taking more than you give. |  | 
|    10 ** |  | 
|    11 ****************************************************************************** |  | 
|    12 ** |  | 
|    13 ** This file contains the VFS implementation for unix-like operating systems |  | 
|    14 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |  | 
|    15 ** |  | 
|    16 ** There are actually several different VFS implementations in this file. |  | 
|    17 ** The differences are in the way that file locking is done.  The default |  | 
|    18 ** implementation uses Posix Advisory Locks.  Alternative implementations |  | 
|    19 ** use flock(), dot-files, various proprietary locking schemas, or simply |  | 
|    20 ** skip locking all together. |  | 
|    21 ** |  | 
|    22 ** This source file is organized into divisions where the logic for various |  | 
|    23 ** subfunctions is contained within the appropriate division.  PLEASE |  | 
|    24 ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed |  | 
|    25 ** in the correct division and should be clearly labeled. |  | 
|    26 ** |  | 
|    27 ** The layout of divisions is as follows: |  | 
|    28 ** |  | 
|    29 **   *  General-purpose declarations and utility functions. |  | 
|    30 **   *  Unique file ID logic used by VxWorks. |  | 
|    31 **   *  Various locking primitive implementations (all except proxy locking): |  | 
|    32 **      + for Posix Advisory Locks |  | 
|    33 **      + for no-op locks |  | 
|    34 **      + for dot-file locks |  | 
|    35 **      + for flock() locking |  | 
|    36 **      + for named semaphore locks (VxWorks only) |  | 
|    37 **      + for AFP filesystem locks (MacOSX only) |  | 
|    38 **   *  sqlite3_file methods not associated with locking. |  | 
|    39 **   *  Definitions of sqlite3_io_methods objects for all locking |  | 
|    40 **      methods plus "finder" functions for each locking method. |  | 
|    41 **   *  sqlite3_vfs method implementations. |  | 
|    42 **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only) |  | 
|    43 **   *  Definitions of sqlite3_vfs objects for all locking methods |  | 
|    44 **      plus implementations of sqlite3_os_init() and sqlite3_os_end(). |  | 
|    45 */ |  | 
|    46 #include "sqliteInt.h" |  | 
|    47 #if SQLITE_OS_UNIX              /* This file is used on unix only */ |  | 
|    48  |  | 
|    49 /* |  | 
|    50 ** There are various methods for file locking used for concurrency |  | 
|    51 ** control: |  | 
|    52 ** |  | 
|    53 **   1. POSIX locking (the default), |  | 
|    54 **   2. No locking, |  | 
|    55 **   3. Dot-file locking, |  | 
|    56 **   4. flock() locking, |  | 
|    57 **   5. AFP locking (OSX only), |  | 
|    58 **   6. Named POSIX semaphores (VXWorks only), |  | 
|    59 **   7. proxy locking. (OSX only) |  | 
|    60 ** |  | 
|    61 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |  | 
|    62 ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |  | 
|    63 ** selection of the appropriate locking style based on the filesystem |  | 
|    64 ** where the database is located.   |  | 
|    65 */ |  | 
|    66 #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |  | 
|    67 #  if defined(__APPLE__) |  | 
|    68 #    define SQLITE_ENABLE_LOCKING_STYLE 1 |  | 
|    69 #  else |  | 
|    70 #    define SQLITE_ENABLE_LOCKING_STYLE 0 |  | 
|    71 #  endif |  | 
|    72 #endif |  | 
|    73  |  | 
|    74 /* |  | 
|    75 ** Define the OS_VXWORKS pre-processor macro to 1 if building on  |  | 
|    76 ** vxworks, or 0 otherwise. |  | 
|    77 */ |  | 
|    78 #ifndef OS_VXWORKS |  | 
|    79 #  if defined(__RTP__) || defined(_WRS_KERNEL) |  | 
|    80 #    define OS_VXWORKS 1 |  | 
|    81 #  else |  | 
|    82 #    define OS_VXWORKS 0 |  | 
|    83 #  endif |  | 
|    84 #endif |  | 
|    85  |  | 
|    86 /* |  | 
|    87 ** These #defines should enable >2GB file support on Posix if the |  | 
|    88 ** underlying operating system supports it.  If the OS lacks |  | 
|    89 ** large file support, these should be no-ops. |  | 
|    90 ** |  | 
|    91 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |  | 
|    92 ** on the compiler command line.  This is necessary if you are compiling |  | 
|    93 ** on a recent machine (ex: RedHat 7.2) but you want your code to work |  | 
|    94 ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2 |  | 
|    95 ** without this option, LFS is enable.  But LFS does not exist in the kernel |  | 
|    96 ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary |  | 
|    97 ** portability you should omit LFS. |  | 
|    98 ** |  | 
|    99 ** The previous paragraph was written in 2005.  (This paragraph is written |  | 
|   100 ** on 2008-11-28.) These days, all Linux kernels support large files, so |  | 
|   101 ** you should probably leave LFS enabled.  But some embedded platforms might |  | 
|   102 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful. |  | 
|   103 */ |  | 
|   104 #ifndef SQLITE_DISABLE_LFS |  | 
|   105 # define _LARGE_FILE       1 |  | 
|   106 # ifndef _FILE_OFFSET_BITS |  | 
|   107 #   define _FILE_OFFSET_BITS 64 |  | 
|   108 # endif |  | 
|   109 # define _LARGEFILE_SOURCE 1 |  | 
|   110 #endif |  | 
|   111  |  | 
|   112 /* |  | 
|   113 ** standard include files. |  | 
|   114 */ |  | 
|   115 #include <sys/types.h> |  | 
|   116 #include <sys/stat.h> |  | 
|   117 #include <fcntl.h> |  | 
|   118 #include <unistd.h> |  | 
|   119 #include <time.h> |  | 
|   120 #include <sys/time.h> |  | 
|   121 #include <errno.h> |  | 
|   122  |  | 
|   123 #if SQLITE_ENABLE_LOCKING_STYLE |  | 
|   124 # include <sys/ioctl.h> |  | 
|   125 # if OS_VXWORKS |  | 
|   126 #  include <semaphore.h> |  | 
|   127 #  include <limits.h> |  | 
|   128 # else |  | 
|   129 #  include <sys/file.h> |  | 
|   130 #  include <sys/param.h> |  | 
|   131 #  include <sys/mount.h> |  | 
|   132 # endif |  | 
|   133 #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |  | 
|   134  |  | 
|   135 /* |  | 
|   136 ** If we are to be thread-safe, include the pthreads header and define |  | 
|   137 ** the SQLITE_UNIX_THREADS macro. |  | 
|   138 */ |  | 
|   139 #if SQLITE_THREADSAFE |  | 
|   140 # include <pthread.h> |  | 
|   141 # define SQLITE_UNIX_THREADS 1 |  | 
|   142 #endif |  | 
|   143  |  | 
|   144 /* |  | 
|   145 ** Default permissions when creating a new file |  | 
|   146 */ |  | 
|   147 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |  | 
|   148 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |  | 
|   149 #endif |  | 
|   150  |  | 
|   151 /* |  | 
|   152  ** Default permissions when creating auto proxy dir |  | 
|   153  */ |  | 
|   154 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |  | 
|   155 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |  | 
|   156 #endif |  | 
|   157  |  | 
|   158 /* |  | 
|   159 ** Maximum supported path-length. |  | 
|   160 */ |  | 
|   161 #define MAX_PATHNAME 512 |  | 
|   162  |  | 
|   163 /* |  | 
|   164 ** Only set the lastErrno if the error code is a real error and not  |  | 
|   165 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |  | 
|   166 */ |  | 
|   167 #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |  | 
|   168  |  | 
|   169  |  | 
|   170 /* |  | 
|   171 ** Sometimes, after a file handle is closed by SQLite, the file descriptor |  | 
|   172 ** cannot be closed immediately. In these cases, instances of the following |  | 
|   173 ** structure are used to store the file descriptor while waiting for an |  | 
|   174 ** opportunity to either close or reuse it. |  | 
|   175 */ |  | 
|   176 typedef struct UnixUnusedFd UnixUnusedFd; |  | 
|   177 struct UnixUnusedFd { |  | 
|   178   int fd;                   /* File descriptor to close */ |  | 
|   179   int flags;                /* Flags this file descriptor was opened with */ |  | 
|   180   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */ |  | 
|   181 }; |  | 
|   182  |  | 
|   183 /* |  | 
|   184 ** The unixFile structure is subclass of sqlite3_file specific to the unix |  | 
|   185 ** VFS implementations. |  | 
|   186 */ |  | 
|   187 typedef struct unixFile unixFile; |  | 
|   188 struct unixFile { |  | 
|   189   sqlite3_io_methods const *pMethod;  /* Always the first entry */ |  | 
|   190   struct unixOpenCnt *pOpen;       /* Info about all open fd's on this inode */ |  | 
|   191   struct unixLockInfo *pLock;      /* Info about locks on this inode */ |  | 
|   192   int h;                           /* The file descriptor */ |  | 
|   193   int dirfd;                       /* File descriptor for the directory */ |  | 
|   194   unsigned char locktype;          /* The type of lock held on this fd */ |  | 
|   195   int lastErrno;                   /* The unix errno from the last I/O error */ |  | 
|   196   void *lockingContext;            /* Locking style specific state */ |  | 
|   197   UnixUnusedFd *pUnused;           /* Pre-allocated UnixUnusedFd */ |  | 
|   198   int fileFlags;                   /* Miscellanous flags */ |  | 
|   199 #if SQLITE_ENABLE_LOCKING_STYLE |  | 
|   200   int openFlags;                   /* The flags specified at open() */ |  | 
|   201 #endif |  | 
|   202 #if SQLITE_THREADSAFE && defined(__linux__) |  | 
|   203   pthread_t tid;                   /* The thread that "owns" this unixFile */ |  | 
|   204 #endif |  | 
|   205 #if OS_VXWORKS |  | 
|   206   int isDelete;                    /* Delete on close if true */ |  | 
|   207   struct vxworksFileId *pId;       /* Unique file ID */ |  | 
|   208 #endif |  | 
|   209 #ifndef NDEBUG |  | 
|   210   /* The next group of variables are used to track whether or not the |  | 
|   211   ** transaction counter in bytes 24-27 of database files are updated |  | 
|   212   ** whenever any part of the database changes.  An assertion fault will |  | 
|   213   ** occur if a file is updated without also updating the transaction |  | 
|   214   ** counter.  This test is made to avoid new problems similar to the |  | 
|   215   ** one described by ticket #3584.  |  | 
|   216   */ |  | 
|   217   unsigned char transCntrChng;   /* True if the transaction counter changed */ |  | 
|   218   unsigned char dbUpdate;        /* True if any part of database file changed */ |  | 
|   219   unsigned char inNormalWrite;   /* True if in a normal write operation */ |  | 
|   220 #endif |  | 
|   221 #ifdef SQLITE_TEST |  | 
|   222   /* In test mode, increase the size of this structure a bit so that  |  | 
|   223   ** it is larger than the struct CrashFile defined in test6.c. |  | 
|   224   */ |  | 
|   225   char aPadding[32]; |  | 
|   226 #endif |  | 
|   227 }; |  | 
|   228  |  | 
|   229 /* |  | 
|   230 ** The following macros define bits in unixFile.fileFlags |  | 
|   231 */ |  | 
|   232 #define SQLITE_WHOLE_FILE_LOCKING  0x0001   /* Use whole-file locking */ |  | 
|   233  |  | 
|   234 /* |  | 
|   235 ** Include code that is common to all os_*.c files |  | 
|   236 */ |  | 
|   237 #include "os_common.h" |  | 
|   238  |  | 
|   239 /* |  | 
|   240 ** Define various macros that are missing from some systems. |  | 
|   241 */ |  | 
|   242 #ifndef O_LARGEFILE |  | 
|   243 # define O_LARGEFILE 0 |  | 
|   244 #endif |  | 
|   245 #ifdef SQLITE_DISABLE_LFS |  | 
|   246 # undef O_LARGEFILE |  | 
|   247 # define O_LARGEFILE 0 |  | 
|   248 #endif |  | 
|   249 #ifndef O_NOFOLLOW |  | 
|   250 # define O_NOFOLLOW 0 |  | 
|   251 #endif |  | 
|   252 #ifndef O_BINARY |  | 
|   253 # define O_BINARY 0 |  | 
|   254 #endif |  | 
|   255  |  | 
|   256 /* |  | 
|   257 ** The DJGPP compiler environment looks mostly like Unix, but it |  | 
|   258 ** lacks the fcntl() system call.  So redefine fcntl() to be something |  | 
|   259 ** that always succeeds.  This means that locking does not occur under |  | 
|   260 ** DJGPP.  But it is DOS - what did you expect? |  | 
|   261 */ |  | 
|   262 #ifdef __DJGPP__ |  | 
|   263 # define fcntl(A,B,C) 0 |  | 
|   264 #endif |  | 
|   265  |  | 
|   266 /* |  | 
|   267 ** The threadid macro resolves to the thread-id or to 0.  Used for |  | 
|   268 ** testing and debugging only. |  | 
|   269 */ |  | 
|   270 #if SQLITE_THREADSAFE |  | 
|   271 #define threadid pthread_self() |  | 
|   272 #else |  | 
|   273 #define threadid 0 |  | 
|   274 #endif |  | 
|   275  |  | 
|   276  |  | 
|   277 /* |  | 
|   278 ** Helper functions to obtain and relinquish the global mutex. The |  | 
|   279 ** global mutex is used to protect the unixOpenCnt, unixLockInfo and |  | 
|   280 ** vxworksFileId objects used by this file, all of which may be  |  | 
|   281 ** shared by multiple threads. |  | 
|   282 ** |  | 
|   283 ** Function unixMutexHeld() is used to assert() that the global mutex  |  | 
|   284 ** is held when required. This function is only used as part of assert()  |  | 
|   285 ** statements. e.g. |  | 
|   286 ** |  | 
|   287 **   unixEnterMutex() |  | 
|   288 **     assert( unixMutexHeld() ); |  | 
|   289 **   unixEnterLeave() |  | 
|   290 */ |  | 
|   291 static void unixEnterMutex(void){ |  | 
|   292   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |  | 
|   293 } |  | 
|   294 static void unixLeaveMutex(void){ |  | 
|   295   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |  | 
|   296 } |  | 
|   297 #ifdef SQLITE_DEBUG |  | 
|   298 static int unixMutexHeld(void) { |  | 
|   299   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |  | 
|   300 } |  | 
|   301 #endif |  | 
|   302  |  | 
|   303  |  | 
|   304 #ifdef SQLITE_DEBUG |  | 
|   305 /* |  | 
|   306 ** Helper function for printing out trace information from debugging |  | 
|   307 ** binaries. This returns the string represetation of the supplied |  | 
|   308 ** integer lock-type. |  | 
|   309 */ |  | 
|   310 static const char *locktypeName(int locktype){ |  | 
|   311   switch( locktype ){ |  | 
|   312     case NO_LOCK: return "NONE"; |  | 
|   313     case SHARED_LOCK: return "SHARED"; |  | 
|   314     case RESERVED_LOCK: return "RESERVED"; |  | 
|   315     case PENDING_LOCK: return "PENDING"; |  | 
|   316     case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |  | 
|   317   } |  | 
|   318   return "ERROR"; |  | 
|   319 } |  | 
|   320 #endif |  | 
|   321  |  | 
|   322 #ifdef SQLITE_LOCK_TRACE |  | 
|   323 /* |  | 
|   324 ** Print out information about all locking operations. |  | 
|   325 ** |  | 
|   326 ** This routine is used for troubleshooting locks on multithreaded |  | 
|   327 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE |  | 
|   328 ** command-line option on the compiler.  This code is normally |  | 
|   329 ** turned off. |  | 
|   330 */ |  | 
|   331 static int lockTrace(int fd, int op, struct flock *p){ |  | 
|   332   char *zOpName, *zType; |  | 
|   333   int s; |  | 
|   334   int savedErrno; |  | 
|   335   if( op==F_GETLK ){ |  | 
|   336     zOpName = "GETLK"; |  | 
|   337   }else if( op==F_SETLK ){ |  | 
|   338     zOpName = "SETLK"; |  | 
|   339   }else{ |  | 
|   340     s = fcntl(fd, op, p); |  | 
|   341     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |  | 
|   342     return s; |  | 
|   343   } |  | 
|   344   if( p->l_type==F_RDLCK ){ |  | 
|   345     zType = "RDLCK"; |  | 
|   346   }else if( p->l_type==F_WRLCK ){ |  | 
|   347     zType = "WRLCK"; |  | 
|   348   }else if( p->l_type==F_UNLCK ){ |  | 
|   349     zType = "UNLCK"; |  | 
|   350   }else{ |  | 
|   351     assert( 0 ); |  | 
|   352   } |  | 
|   353   assert( p->l_whence==SEEK_SET ); |  | 
|   354   s = fcntl(fd, op, p); |  | 
|   355   savedErrno = errno; |  | 
|   356   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |  | 
|   357      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |  | 
|   358      (int)p->l_pid, s); |  | 
|   359   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |  | 
|   360     struct flock l2; |  | 
|   361     l2 = *p; |  | 
|   362     fcntl(fd, F_GETLK, &l2); |  | 
|   363     if( l2.l_type==F_RDLCK ){ |  | 
|   364       zType = "RDLCK"; |  | 
|   365     }else if( l2.l_type==F_WRLCK ){ |  | 
|   366       zType = "WRLCK"; |  | 
|   367     }else if( l2.l_type==F_UNLCK ){ |  | 
|   368       zType = "UNLCK"; |  | 
|   369     }else{ |  | 
|   370       assert( 0 ); |  | 
|   371     } |  | 
|   372     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |  | 
|   373        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |  | 
|   374   } |  | 
|   375   errno = savedErrno; |  | 
|   376   return s; |  | 
|   377 } |  | 
|   378 #define fcntl lockTrace |  | 
|   379 #endif /* SQLITE_LOCK_TRACE */ |  | 
|   380  |  | 
|   381  |  | 
|   382  |  | 
|   383 /* |  | 
|   384 ** This routine translates a standard POSIX errno code into something |  | 
|   385 ** useful to the clients of the sqlite3 functions.  Specifically, it is |  | 
|   386 ** intended to translate a variety of "try again" errors into SQLITE_BUSY |  | 
|   387 ** and a variety of "please close the file descriptor NOW" errors into  |  | 
|   388 ** SQLITE_IOERR |  | 
|   389 **  |  | 
|   390 ** Errors during initialization of locks, or file system support for locks, |  | 
|   391 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |  | 
|   392 */ |  | 
|   393 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |  | 
|   394   switch (posixError) { |  | 
|   395   case 0:  |  | 
|   396     return SQLITE_OK; |  | 
|   397      |  | 
|   398   case EAGAIN: |  | 
|   399   case ETIMEDOUT: |  | 
|   400   case EBUSY: |  | 
|   401   case EINTR: |  | 
|   402   case ENOLCK:   |  | 
|   403     /* random NFS retry error, unless during file system support  |  | 
|   404      * introspection, in which it actually means what it says */ |  | 
|   405     return SQLITE_BUSY; |  | 
|   406      |  | 
|   407   case EACCES:  |  | 
|   408     /* EACCES is like EAGAIN during locking operations, but not any other time*/ |  | 
|   409     if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||  |  | 
|   410         (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||  |  | 
|   411         (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |  | 
|   412         (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |  | 
|   413       return SQLITE_BUSY; |  | 
|   414     } |  | 
|   415     /* else fall through */ |  | 
|   416   case EPERM:  |  | 
|   417     return SQLITE_PERM; |  | 
|   418      |  | 
|   419   case EDEADLK: |  | 
|   420     return SQLITE_IOERR_BLOCKED; |  | 
|   421      |  | 
|   422 #if EOPNOTSUPP!=ENOTSUP |  | 
|   423   case EOPNOTSUPP:  |  | 
|   424     /* something went terribly awry, unless during file system support  |  | 
|   425      * introspection, in which it actually means what it says */ |  | 
|   426 #endif |  | 
|   427 #ifdef ENOTSUP |  | 
|   428   case ENOTSUP:  |  | 
|   429     /* invalid fd, unless during file system support introspection, in which  |  | 
|   430      * it actually means what it says */ |  | 
|   431 #endif |  | 
|   432   case EIO: |  | 
|   433   case EBADF: |  | 
|   434   case EINVAL: |  | 
|   435   case ENOTCONN: |  | 
|   436   case ENODEV: |  | 
|   437   case ENXIO: |  | 
|   438   case ENOENT: |  | 
|   439   case ESTALE: |  | 
|   440   case ENOSYS: |  | 
|   441     /* these should force the client to close the file and reconnect */ |  | 
|   442      |  | 
|   443   default:  |  | 
|   444     return sqliteIOErr; |  | 
|   445   } |  | 
|   446 } |  | 
|   447  |  | 
|   448  |  | 
|   449  |  | 
|   450 /****************************************************************************** |  | 
|   451 ****************** Begin Unique File ID Utility Used By VxWorks *************** |  | 
|   452 ** |  | 
|   453 ** On most versions of unix, we can get a unique ID for a file by concatenating |  | 
|   454 ** the device number and the inode number.  But this does not work on VxWorks. |  | 
|   455 ** On VxWorks, a unique file id must be based on the canonical filename. |  | 
|   456 ** |  | 
|   457 ** A pointer to an instance of the following structure can be used as a |  | 
|   458 ** unique file ID in VxWorks.  Each instance of this structure contains |  | 
|   459 ** a copy of the canonical filename.  There is also a reference count.   |  | 
|   460 ** The structure is reclaimed when the number of pointers to it drops to |  | 
|   461 ** zero. |  | 
|   462 ** |  | 
|   463 ** There are never very many files open at one time and lookups are not |  | 
|   464 ** a performance-critical path, so it is sufficient to put these |  | 
|   465 ** structures on a linked list. |  | 
|   466 */ |  | 
|   467 struct vxworksFileId { |  | 
|   468   struct vxworksFileId *pNext;  /* Next in a list of them all */ |  | 
|   469   int nRef;                     /* Number of references to this one */ |  | 
|   470   int nName;                    /* Length of the zCanonicalName[] string */ |  | 
|   471   char *zCanonicalName;         /* Canonical filename */ |  | 
|   472 }; |  | 
|   473  |  | 
|   474 #if OS_VXWORKS |  | 
|   475 /*  |  | 
|   476 ** All unique filenames are held on a linked list headed by this |  | 
|   477 ** variable: |  | 
|   478 */ |  | 
|   479 static struct vxworksFileId *vxworksFileList = 0; |  | 
|   480  |  | 
|   481 /* |  | 
|   482 ** Simplify a filename into its canonical form |  | 
|   483 ** by making the following changes: |  | 
|   484 ** |  | 
|   485 **  * removing any trailing and duplicate / |  | 
|   486 **  * convert /./ into just / |  | 
|   487 **  * convert /A/../ where A is any simple name into just / |  | 
|   488 ** |  | 
|   489 ** Changes are made in-place.  Return the new name length. |  | 
|   490 ** |  | 
|   491 ** The original filename is in z[0..n-1].  Return the number of |  | 
|   492 ** characters in the simplified name. |  | 
|   493 */ |  | 
|   494 static int vxworksSimplifyName(char *z, int n){ |  | 
|   495   int i, j; |  | 
|   496   while( n>1 && z[n-1]=='/' ){ n--; } |  | 
|   497   for(i=j=0; i<n; i++){ |  | 
|   498     if( z[i]=='/' ){ |  | 
|   499       if( z[i+1]=='/' ) continue; |  | 
|   500       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |  | 
|   501         i += 1; |  | 
|   502         continue; |  | 
|   503       } |  | 
|   504       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |  | 
|   505         while( j>0 && z[j-1]!='/' ){ j--; } |  | 
|   506         if( j>0 ){ j--; } |  | 
|   507         i += 2; |  | 
|   508         continue; |  | 
|   509       } |  | 
|   510     } |  | 
|   511     z[j++] = z[i]; |  | 
|   512   } |  | 
|   513   z[j] = 0; |  | 
|   514   return j; |  | 
|   515 } |  | 
|   516  |  | 
|   517 /* |  | 
|   518 ** Find a unique file ID for the given absolute pathname.  Return |  | 
|   519 ** a pointer to the vxworksFileId object.  This pointer is the unique |  | 
|   520 ** file ID. |  | 
|   521 ** |  | 
|   522 ** The nRef field of the vxworksFileId object is incremented before |  | 
|   523 ** the object is returned.  A new vxworksFileId object is created |  | 
|   524 ** and added to the global list if necessary. |  | 
|   525 ** |  | 
|   526 ** If a memory allocation error occurs, return NULL. |  | 
|   527 */ |  | 
|   528 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |  | 
|   529   struct vxworksFileId *pNew;         /* search key and new file ID */ |  | 
|   530   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */ |  | 
|   531   int n;                              /* Length of zAbsoluteName string */ |  | 
|   532  |  | 
|   533   assert( zAbsoluteName[0]=='/' ); |  | 
|   534   n = (int)strlen(zAbsoluteName); |  | 
|   535   pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |  | 
|   536   if( pNew==0 ) return 0; |  | 
|   537   pNew->zCanonicalName = (char*)&pNew[1]; |  | 
|   538   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |  | 
|   539   n = vxworksSimplifyName(pNew->zCanonicalName, n); |  | 
|   540  |  | 
|   541   /* Search for an existing entry that matching the canonical name. |  | 
|   542   ** If found, increment the reference count and return a pointer to |  | 
|   543   ** the existing file ID. |  | 
|   544   */ |  | 
|   545   unixEnterMutex(); |  | 
|   546   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |  | 
|   547     if( pCandidate->nName==n  |  | 
|   548      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |  | 
|   549     ){ |  | 
|   550        sqlite3_free(pNew); |  | 
|   551        pCandidate->nRef++; |  | 
|   552        unixLeaveMutex(); |  | 
|   553        return pCandidate; |  | 
|   554     } |  | 
|   555   } |  | 
|   556  |  | 
|   557   /* No match was found.  We will make a new file ID */ |  | 
|   558   pNew->nRef = 1; |  | 
|   559   pNew->nName = n; |  | 
|   560   pNew->pNext = vxworksFileList; |  | 
|   561   vxworksFileList = pNew; |  | 
|   562   unixLeaveMutex(); |  | 
|   563   return pNew; |  | 
|   564 } |  | 
|   565  |  | 
|   566 /* |  | 
|   567 ** Decrement the reference count on a vxworksFileId object.  Free |  | 
|   568 ** the object when the reference count reaches zero. |  | 
|   569 */ |  | 
|   570 static void vxworksReleaseFileId(struct vxworksFileId *pId){ |  | 
|   571   unixEnterMutex(); |  | 
|   572   assert( pId->nRef>0 ); |  | 
|   573   pId->nRef--; |  | 
|   574   if( pId->nRef==0 ){ |  | 
|   575     struct vxworksFileId **pp; |  | 
|   576     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |  | 
|   577     assert( *pp==pId ); |  | 
|   578     *pp = pId->pNext; |  | 
|   579     sqlite3_free(pId); |  | 
|   580   } |  | 
|   581   unixLeaveMutex(); |  | 
|   582 } |  | 
|   583 #endif /* OS_VXWORKS */ |  | 
|   584 /*************** End of Unique File ID Utility Used By VxWorks **************** |  | 
|   585 ******************************************************************************/ |  | 
|   586  |  | 
|   587  |  | 
|   588 /****************************************************************************** |  | 
|   589 *************************** Posix Advisory Locking **************************** |  | 
|   590 ** |  | 
|   591 ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996) |  | 
|   592 ** section 6.5.2.2 lines 483 through 490 specify that when a process |  | 
|   593 ** sets or clears a lock, that operation overrides any prior locks set |  | 
|   594 ** by the same process.  It does not explicitly say so, but this implies |  | 
|   595 ** that it overrides locks set by the same process using a different |  | 
|   596 ** file descriptor.  Consider this test case: |  | 
|   597 ** |  | 
|   598 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |  | 
|   599 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |  | 
|   600 ** |  | 
|   601 ** Suppose ./file1 and ./file2 are really the same file (because |  | 
|   602 ** one is a hard or symbolic link to the other) then if you set |  | 
|   603 ** an exclusive lock on fd1, then try to get an exclusive lock |  | 
|   604 ** on fd2, it works.  I would have expected the second lock to |  | 
|   605 ** fail since there was already a lock on the file due to fd1. |  | 
|   606 ** But not so.  Since both locks came from the same process, the |  | 
|   607 ** second overrides the first, even though they were on different |  | 
|   608 ** file descriptors opened on different file names. |  | 
|   609 ** |  | 
|   610 ** This means that we cannot use POSIX locks to synchronize file access |  | 
|   611 ** among competing threads of the same process.  POSIX locks will work fine |  | 
|   612 ** to synchronize access for threads in separate processes, but not |  | 
|   613 ** threads within the same process. |  | 
|   614 ** |  | 
|   615 ** To work around the problem, SQLite has to manage file locks internally |  | 
|   616 ** on its own.  Whenever a new database is opened, we have to find the |  | 
|   617 ** specific inode of the database file (the inode is determined by the |  | 
|   618 ** st_dev and st_ino fields of the stat structure that fstat() fills in) |  | 
|   619 ** and check for locks already existing on that inode.  When locks are |  | 
|   620 ** created or removed, we have to look at our own internal record of the |  | 
|   621 ** locks to see if another thread has previously set a lock on that same |  | 
|   622 ** inode. |  | 
|   623 ** |  | 
|   624 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |  | 
|   625 ** For VxWorks, we have to use the alternative unique ID system based on |  | 
|   626 ** canonical filename and implemented in the previous division.) |  | 
|   627 ** |  | 
|   628 ** The sqlite3_file structure for POSIX is no longer just an integer file |  | 
|   629 ** descriptor.  It is now a structure that holds the integer file |  | 
|   630 ** descriptor and a pointer to a structure that describes the internal |  | 
|   631 ** locks on the corresponding inode.  There is one locking structure |  | 
|   632 ** per inode, so if the same inode is opened twice, both unixFile structures |  | 
|   633 ** point to the same locking structure.  The locking structure keeps |  | 
|   634 ** a reference count (so we will know when to delete it) and a "cnt" |  | 
|   635 ** field that tells us its internal lock status.  cnt==0 means the |  | 
|   636 ** file is unlocked.  cnt==-1 means the file has an exclusive lock. |  | 
|   637 ** cnt>0 means there are cnt shared locks on the file. |  | 
|   638 ** |  | 
|   639 ** Any attempt to lock or unlock a file first checks the locking |  | 
|   640 ** structure.  The fcntl() system call is only invoked to set a  |  | 
|   641 ** POSIX lock if the internal lock structure transitions between |  | 
|   642 ** a locked and an unlocked state. |  | 
|   643 ** |  | 
|   644 ** But wait:  there are yet more problems with POSIX advisory locks. |  | 
|   645 ** |  | 
|   646 ** If you close a file descriptor that points to a file that has locks, |  | 
|   647 ** all locks on that file that are owned by the current process are |  | 
|   648 ** released.  To work around this problem, each unixFile structure contains |  | 
|   649 ** a pointer to an unixOpenCnt structure.  There is one unixOpenCnt structure |  | 
|   650 ** per open inode, which means that multiple unixFile can point to a single |  | 
|   651 ** unixOpenCnt.  When an attempt is made to close an unixFile, if there are |  | 
|   652 ** other unixFile open on the same inode that are holding locks, the call |  | 
|   653 ** to close() the file descriptor is deferred until all of the locks clear. |  | 
|   654 ** The unixOpenCnt structure keeps a list of file descriptors that need to |  | 
|   655 ** be closed and that list is walked (and cleared) when the last lock |  | 
|   656 ** clears. |  | 
|   657 ** |  | 
|   658 ** Yet another problem:  LinuxThreads do not play well with posix locks. |  | 
|   659 ** |  | 
|   660 ** Many older versions of linux use the LinuxThreads library which is |  | 
|   661 ** not posix compliant.  Under LinuxThreads, a lock created by thread |  | 
|   662 ** A cannot be modified or overridden by a different thread B. |  | 
|   663 ** Only thread A can modify the lock.  Locking behavior is correct |  | 
|   664 ** if the appliation uses the newer Native Posix Thread Library (NPTL) |  | 
|   665 ** on linux - with NPTL a lock created by thread A can override locks |  | 
|   666 ** in thread B.  But there is no way to know at compile-time which |  | 
|   667 ** threading library is being used.  So there is no way to know at |  | 
|   668 ** compile-time whether or not thread A can override locks on thread B. |  | 
|   669 ** We have to do a run-time check to discover the behavior of the |  | 
|   670 ** current process. |  | 
|   671 ** |  | 
|   672 ** On systems where thread A is unable to modify locks created by |  | 
|   673 ** thread B, we have to keep track of which thread created each |  | 
|   674 ** lock.  Hence there is an extra field in the key to the unixLockInfo |  | 
|   675 ** structure to record this information.  And on those systems it |  | 
|   676 ** is illegal to begin a transaction in one thread and finish it |  | 
|   677 ** in another.  For this latter restriction, there is no work-around. |  | 
|   678 ** It is a limitation of LinuxThreads. |  | 
|   679 */ |  | 
|   680  |  | 
|   681 /* |  | 
|   682 ** Set or check the unixFile.tid field.  This field is set when an unixFile |  | 
|   683 ** is first opened.  All subsequent uses of the unixFile verify that the |  | 
|   684 ** same thread is operating on the unixFile.  Some operating systems do |  | 
|   685 ** not allow locks to be overridden by other threads and that restriction |  | 
|   686 ** means that sqlite3* database handles cannot be moved from one thread |  | 
|   687 ** to another while locks are held. |  | 
|   688 ** |  | 
|   689 ** Version 3.3.1 (2006-01-15):  unixFile can be moved from one thread to |  | 
|   690 ** another as long as we are running on a system that supports threads |  | 
|   691 ** overriding each others locks (which is now the most common behavior) |  | 
|   692 ** or if no locks are held.  But the unixFile.pLock field needs to be |  | 
|   693 ** recomputed because its key includes the thread-id.  See the  |  | 
|   694 ** transferOwnership() function below for additional information |  | 
|   695 */ |  | 
|   696 #if SQLITE_THREADSAFE && defined(__linux__) |  | 
|   697 # define SET_THREADID(X)   (X)->tid = pthread_self() |  | 
|   698 # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \ |  | 
|   699                             !pthread_equal((X)->tid, pthread_self())) |  | 
|   700 #else |  | 
|   701 # define SET_THREADID(X) |  | 
|   702 # define CHECK_THREADID(X) 0 |  | 
|   703 #endif |  | 
|   704  |  | 
|   705 /* |  | 
|   706 ** An instance of the following structure serves as the key used |  | 
|   707 ** to locate a particular unixOpenCnt structure given its inode.  This |  | 
|   708 ** is the same as the unixLockKey except that the thread ID is omitted. |  | 
|   709 */ |  | 
|   710 struct unixFileId { |  | 
|   711   dev_t dev;                  /* Device number */ |  | 
|   712 #if OS_VXWORKS |  | 
|   713   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */ |  | 
|   714 #else |  | 
|   715   ino_t ino;                  /* Inode number */ |  | 
|   716 #endif |  | 
|   717 }; |  | 
|   718  |  | 
|   719 /* |  | 
|   720 ** An instance of the following structure serves as the key used |  | 
|   721 ** to locate a particular unixLockInfo structure given its inode. |  | 
|   722 ** |  | 
|   723 ** If threads cannot override each others locks (LinuxThreads), then we |  | 
|   724 ** set the unixLockKey.tid field to the thread ID.  If threads can override |  | 
|   725 ** each others locks (Posix and NPTL) then tid is always set to zero. |  | 
|   726 ** tid is omitted if we compile without threading support or on an OS |  | 
|   727 ** other than linux. |  | 
|   728 */ |  | 
|   729 struct unixLockKey { |  | 
|   730   struct unixFileId fid;  /* Unique identifier for the file */ |  | 
|   731 #if SQLITE_THREADSAFE && defined(__linux__) |  | 
|   732   pthread_t tid;  /* Thread ID of lock owner. Zero if not using LinuxThreads */ |  | 
|   733 #endif |  | 
|   734 }; |  | 
|   735  |  | 
|   736 /* |  | 
|   737 ** An instance of the following structure is allocated for each open |  | 
|   738 ** inode.  Or, on LinuxThreads, there is one of these structures for |  | 
|   739 ** each inode opened by each thread. |  | 
|   740 ** |  | 
|   741 ** A single inode can have multiple file descriptors, so each unixFile |  | 
|   742 ** structure contains a pointer to an instance of this object and this |  | 
|   743 ** object keeps a count of the number of unixFile pointing to it. |  | 
|   744 */ |  | 
|   745 struct unixLockInfo { |  | 
|   746   struct unixLockKey lockKey;     /* The lookup key */ |  | 
|   747   int cnt;                        /* Number of SHARED locks held */ |  | 
|   748   int locktype;                   /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |  | 
|   749   int nRef;                       /* Number of pointers to this structure */ |  | 
|   750   struct unixLockInfo *pNext;     /* List of all unixLockInfo objects */ |  | 
|   751   struct unixLockInfo *pPrev;     /*    .... doubly linked */ |  | 
|   752 }; |  | 
|   753  |  | 
|   754 /* |  | 
|   755 ** An instance of the following structure is allocated for each open |  | 
|   756 ** inode.  This structure keeps track of the number of locks on that |  | 
|   757 ** inode.  If a close is attempted against an inode that is holding |  | 
|   758 ** locks, the close is deferred until all locks clear by adding the |  | 
|   759 ** file descriptor to be closed to the pending list. |  | 
|   760 ** |  | 
|   761 ** TODO:  Consider changing this so that there is only a single file |  | 
|   762 ** descriptor for each open file, even when it is opened multiple times. |  | 
|   763 ** The close() system call would only occur when the last database |  | 
|   764 ** using the file closes. |  | 
|   765 */ |  | 
|   766 struct unixOpenCnt { |  | 
|   767   struct unixFileId fileId;   /* The lookup key */ |  | 
|   768   int nRef;                   /* Number of pointers to this structure */ |  | 
|   769   int nLock;                  /* Number of outstanding locks */ |  | 
|   770   UnixUnusedFd *pUnused;      /* Unused file descriptors to close */ |  | 
|   771 #if OS_VXWORKS |  | 
|   772   sem_t *pSem;                     /* Named POSIX semaphore */ |  | 
|   773   char aSemName[MAX_PATHNAME+2];   /* Name of that semaphore */ |  | 
|   774 #endif |  | 
|   775   struct unixOpenCnt *pNext, *pPrev;   /* List of all unixOpenCnt objects */ |  | 
|   776 }; |  | 
|   777  |  | 
|   778 /* |  | 
|   779 ** Lists of all unixLockInfo and unixOpenCnt objects.  These used to be hash |  | 
|   780 ** tables.  But the number of objects is rarely more than a dozen and |  | 
|   781 ** never exceeds a few thousand.  And lookup is not on a critical |  | 
|   782 ** path so a simple linked list will suffice. |  | 
|   783 */ |  | 
|   784 static struct unixLockInfo *lockList = 0; |  | 
|   785 static struct unixOpenCnt *openList = 0; |  | 
|   786  |  | 
|   787 /* |  | 
|   788 ** This variable remembers whether or not threads can override each others |  | 
|   789 ** locks. |  | 
|   790 ** |  | 
|   791 **    0:  No.  Threads cannot override each others locks.  (LinuxThreads) |  | 
|   792 **    1:  Yes.  Threads can override each others locks.  (Posix & NLPT) |  | 
|   793 **   -1:  We don't know yet. |  | 
|   794 ** |  | 
|   795 ** On some systems, we know at compile-time if threads can override each |  | 
|   796 ** others locks.  On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro |  | 
|   797 ** will be set appropriately.  On other systems, we have to check at |  | 
|   798 ** runtime.  On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is |  | 
|   799 ** undefined. |  | 
|   800 ** |  | 
|   801 ** This variable normally has file scope only.  But during testing, we make |  | 
|   802 ** it a global so that the test code can change its value in order to verify |  | 
|   803 ** that the right stuff happens in either case. |  | 
|   804 */ |  | 
|   805 #if SQLITE_THREADSAFE && defined(__linux__) |  | 
|   806 #  ifndef SQLITE_THREAD_OVERRIDE_LOCK |  | 
|   807 #    define SQLITE_THREAD_OVERRIDE_LOCK -1 |  | 
|   808 #  endif |  | 
|   809 #  ifdef SQLITE_TEST |  | 
|   810 int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |  | 
|   811 #  else |  | 
|   812 static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |  | 
|   813 #  endif |  | 
|   814 #endif |  | 
|   815  |  | 
|   816 /* |  | 
|   817 ** This structure holds information passed into individual test |  | 
|   818 ** threads by the testThreadLockingBehavior() routine. |  | 
|   819 */ |  | 
|   820 struct threadTestData { |  | 
|   821   int fd;                /* File to be locked */ |  | 
|   822   struct flock lock;     /* The locking operation */ |  | 
|   823   int result;            /* Result of the locking operation */ |  | 
|   824 }; |  | 
|   825  |  | 
|   826 #if SQLITE_THREADSAFE && defined(__linux__) |  | 
|   827 /* |  | 
|   828 ** This function is used as the main routine for a thread launched by |  | 
|   829 ** testThreadLockingBehavior(). It tests whether the shared-lock obtained |  | 
|   830 ** by the main thread in testThreadLockingBehavior() conflicts with a |  | 
|   831 ** hypothetical write-lock obtained by this thread on the same file. |  | 
|   832 ** |  | 
|   833 ** The write-lock is not actually acquired, as this is not possible if  |  | 
|   834 ** the file is open in read-only mode (see ticket #3472). |  | 
|   835 */  |  | 
|   836 static void *threadLockingTest(void *pArg){ |  | 
|   837   struct threadTestData *pData = (struct threadTestData*)pArg; |  | 
|   838   pData->result = fcntl(pData->fd, F_GETLK, &pData->lock); |  | 
|   839   return pArg; |  | 
|   840 } |  | 
|   841 #endif /* SQLITE_THREADSAFE && defined(__linux__) */ |  | 
|   842  |  | 
|   843  |  | 
|   844 #if SQLITE_THREADSAFE && defined(__linux__) |  | 
|   845 /* |  | 
|   846 ** This procedure attempts to determine whether or not threads |  | 
|   847 ** can override each others locks then sets the  |  | 
|   848 ** threadsOverrideEachOthersLocks variable appropriately. |  | 
|   849 */ |  | 
|   850 static void testThreadLockingBehavior(int fd_orig){ |  | 
|   851   int fd; |  | 
|   852   int rc; |  | 
|   853   struct threadTestData d; |  | 
|   854   struct flock l; |  | 
|   855   pthread_t t; |  | 
|   856  |  | 
|   857   fd = dup(fd_orig); |  | 
|   858   if( fd<0 ) return; |  | 
|   859   memset(&l, 0, sizeof(l)); |  | 
|   860   l.l_type = F_RDLCK; |  | 
|   861   l.l_len = 1; |  | 
|   862   l.l_start = 0; |  | 
|   863   l.l_whence = SEEK_SET; |  | 
|   864   rc = fcntl(fd_orig, F_SETLK, &l); |  | 
|   865   if( rc!=0 ) return; |  | 
|   866   memset(&d, 0, sizeof(d)); |  | 
|   867   d.fd = fd; |  | 
|   868   d.lock = l; |  | 
|   869   d.lock.l_type = F_WRLCK; |  | 
|   870   if( pthread_create(&t, 0, threadLockingTest, &d)==0 ){ |  | 
|   871     pthread_join(t, 0); |  | 
|   872   } |  | 
|   873   close(fd); |  | 
|   874   if( d.result!=0 ) return; |  | 
|   875   threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK); |  | 
|   876 } |  | 
|   877 #endif /* SQLITE_THREADSAFE && defined(__linux__) */ |  | 
|   878  |  | 
|   879 /* |  | 
|   880 ** Release a unixLockInfo structure previously allocated by findLockInfo(). |  | 
|   881 ** |  | 
|   882 ** The mutex entered using the unixEnterMutex() function must be held |  | 
|   883 ** when this function is called. |  | 
|   884 */ |  | 
|   885 static void releaseLockInfo(struct unixLockInfo *pLock){ |  | 
|   886   assert( unixMutexHeld() ); |  | 
|   887   if( pLock ){ |  | 
|   888     pLock->nRef--; |  | 
|   889     if( pLock->nRef==0 ){ |  | 
|   890       if( pLock->pPrev ){ |  | 
|   891         assert( pLock->pPrev->pNext==pLock ); |  | 
|   892         pLock->pPrev->pNext = pLock->pNext; |  | 
|   893       }else{ |  | 
|   894         assert( lockList==pLock ); |  | 
|   895         lockList = pLock->pNext; |  | 
|   896       } |  | 
|   897       if( pLock->pNext ){ |  | 
|   898         assert( pLock->pNext->pPrev==pLock ); |  | 
|   899         pLock->pNext->pPrev = pLock->pPrev; |  | 
|   900       } |  | 
|   901       sqlite3_free(pLock); |  | 
|   902     } |  | 
|   903   } |  | 
|   904 } |  | 
|   905  |  | 
|   906 /* |  | 
|   907 ** Release a unixOpenCnt structure previously allocated by findLockInfo(). |  | 
|   908 ** |  | 
|   909 ** The mutex entered using the unixEnterMutex() function must be held |  | 
|   910 ** when this function is called. |  | 
|   911 */ |  | 
|   912 static void releaseOpenCnt(struct unixOpenCnt *pOpen){ |  | 
|   913   assert( unixMutexHeld() ); |  | 
|   914   if( pOpen ){ |  | 
|   915     pOpen->nRef--; |  | 
|   916     if( pOpen->nRef==0 ){ |  | 
|   917       if( pOpen->pPrev ){ |  | 
|   918         assert( pOpen->pPrev->pNext==pOpen ); |  | 
|   919         pOpen->pPrev->pNext = pOpen->pNext; |  | 
|   920       }else{ |  | 
|   921         assert( openList==pOpen ); |  | 
|   922         openList = pOpen->pNext; |  | 
|   923       } |  | 
|   924       if( pOpen->pNext ){ |  | 
|   925         assert( pOpen->pNext->pPrev==pOpen ); |  | 
|   926         pOpen->pNext->pPrev = pOpen->pPrev; |  | 
|   927       } |  | 
|   928 #if SQLITE_THREADSAFE && defined(__linux__) |  | 
|   929       assert( !pOpen->pUnused || threadsOverrideEachOthersLocks==0 ); |  | 
|   930 #endif |  | 
|   931  |  | 
|   932       /* If pOpen->pUnused is not null, then memory and file-descriptors |  | 
|   933       ** are leaked. |  | 
|   934       ** |  | 
|   935       ** This will only happen if, under Linuxthreads, the user has opened |  | 
|   936       ** a transaction in one thread, then attempts to close the database |  | 
|   937       ** handle from another thread (without first unlocking the db file). |  | 
|   938       ** This is a misuse.  */ |  | 
|   939       sqlite3_free(pOpen); |  | 
|   940     } |  | 
|   941   } |  | 
|   942 } |  | 
|   943  |  | 
|   944 /* |  | 
|   945 ** Given a file descriptor, locate unixLockInfo and unixOpenCnt structures that |  | 
|   946 ** describes that file descriptor.  Create new ones if necessary.  The |  | 
|   947 ** return values might be uninitialized if an error occurs. |  | 
|   948 ** |  | 
|   949 ** The mutex entered using the unixEnterMutex() function must be held |  | 
|   950 ** when this function is called. |  | 
|   951 ** |  | 
|   952 ** Return an appropriate error code. |  | 
|   953 */ |  | 
|   954 static int findLockInfo( |  | 
|   955   unixFile *pFile,               /* Unix file with file desc used in the key */ |  | 
|   956   struct unixLockInfo **ppLock,  /* Return the unixLockInfo structure here */ |  | 
|   957   struct unixOpenCnt **ppOpen    /* Return the unixOpenCnt structure here */ |  | 
|   958 ){ |  | 
|   959   int rc;                        /* System call return code */ |  | 
|   960   int fd;                        /* The file descriptor for pFile */ |  | 
|   961   struct unixLockKey lockKey;    /* Lookup key for the unixLockInfo structure */ |  | 
|   962   struct unixFileId fileId;      /* Lookup key for the unixOpenCnt struct */ |  | 
|   963   struct stat statbuf;           /* Low-level file information */ |  | 
|   964   struct unixLockInfo *pLock = 0;/* Candidate unixLockInfo object */ |  | 
|   965   struct unixOpenCnt *pOpen;     /* Candidate unixOpenCnt object */ |  | 
|   966  |  | 
|   967   assert( unixMutexHeld() ); |  | 
|   968  |  | 
|   969   /* Get low-level information about the file that we can used to |  | 
|   970   ** create a unique name for the file. |  | 
|   971   */ |  | 
|   972   fd = pFile->h; |  | 
|   973   rc = fstat(fd, &statbuf); |  | 
|   974   if( rc!=0 ){ |  | 
|   975     pFile->lastErrno = errno; |  | 
|   976 #ifdef EOVERFLOW |  | 
|   977     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |  | 
|   978 #endif |  | 
|   979     return SQLITE_IOERR; |  | 
|   980   } |  | 
|   981  |  | 
|   982 #ifdef __APPLE__ |  | 
|   983   /* On OS X on an msdos filesystem, the inode number is reported |  | 
|   984   ** incorrectly for zero-size files.  See ticket #3260.  To work |  | 
|   985   ** around this problem (we consider it a bug in OS X, not SQLite) |  | 
|   986   ** we always increase the file size to 1 by writing a single byte |  | 
|   987   ** prior to accessing the inode number.  The one byte written is |  | 
|   988   ** an ASCII 'S' character which also happens to be the first byte |  | 
|   989   ** in the header of every SQLite database.  In this way, if there |  | 
|   990   ** is a race condition such that another thread has already populated |  | 
|   991   ** the first page of the database, no damage is done. |  | 
|   992   */ |  | 
|   993   if( statbuf.st_size==0 ){ |  | 
|   994     rc = write(fd, "S", 1); |  | 
|   995     if( rc!=1 ){ |  | 
|   996       return SQLITE_IOERR; |  | 
|   997     } |  | 
|   998     rc = fstat(fd, &statbuf); |  | 
|   999     if( rc!=0 ){ |  | 
|  1000       pFile->lastErrno = errno; |  | 
|  1001       return SQLITE_IOERR; |  | 
|  1002     } |  | 
|  1003   } |  | 
|  1004 #endif |  | 
|  1005  |  | 
|  1006   memset(&lockKey, 0, sizeof(lockKey)); |  | 
|  1007   lockKey.fid.dev = statbuf.st_dev; |  | 
|  1008 #if OS_VXWORKS |  | 
|  1009   lockKey.fid.pId = pFile->pId; |  | 
|  1010 #else |  | 
|  1011   lockKey.fid.ino = statbuf.st_ino; |  | 
|  1012 #endif |  | 
|  1013 #if SQLITE_THREADSAFE && defined(__linux__) |  | 
|  1014   if( threadsOverrideEachOthersLocks<0 ){ |  | 
|  1015     testThreadLockingBehavior(fd); |  | 
|  1016   } |  | 
|  1017   lockKey.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self(); |  | 
|  1018 #endif |  | 
|  1019   fileId = lockKey.fid; |  | 
|  1020   if( ppLock!=0 ){ |  | 
|  1021     pLock = lockList; |  | 
|  1022     while( pLock && memcmp(&lockKey, &pLock->lockKey, sizeof(lockKey)) ){ |  | 
|  1023       pLock = pLock->pNext; |  | 
|  1024     } |  | 
|  1025     if( pLock==0 ){ |  | 
|  1026       pLock = sqlite3_malloc( sizeof(*pLock) ); |  | 
|  1027       if( pLock==0 ){ |  | 
|  1028         rc = SQLITE_NOMEM; |  | 
|  1029         goto exit_findlockinfo; |  | 
|  1030       } |  | 
|  1031       pLock->lockKey = lockKey; |  | 
|  1032       pLock->nRef = 1; |  | 
|  1033       pLock->cnt = 0; |  | 
|  1034       pLock->locktype = 0; |  | 
|  1035       pLock->pNext = lockList; |  | 
|  1036       pLock->pPrev = 0; |  | 
|  1037       if( lockList ) lockList->pPrev = pLock; |  | 
|  1038       lockList = pLock; |  | 
|  1039     }else{ |  | 
|  1040       pLock->nRef++; |  | 
|  1041     } |  | 
|  1042     *ppLock = pLock; |  | 
|  1043   } |  | 
|  1044   if( ppOpen!=0 ){ |  | 
|  1045     pOpen = openList; |  | 
|  1046     while( pOpen && memcmp(&fileId, &pOpen->fileId, sizeof(fileId)) ){ |  | 
|  1047       pOpen = pOpen->pNext; |  | 
|  1048     } |  | 
|  1049     if( pOpen==0 ){ |  | 
|  1050       pOpen = sqlite3_malloc( sizeof(*pOpen) ); |  | 
|  1051       if( pOpen==0 ){ |  | 
|  1052         releaseLockInfo(pLock); |  | 
|  1053         rc = SQLITE_NOMEM; |  | 
|  1054         goto exit_findlockinfo; |  | 
|  1055       } |  | 
|  1056       memset(pOpen, 0, sizeof(*pOpen)); |  | 
|  1057       pOpen->fileId = fileId; |  | 
|  1058       pOpen->nRef = 1; |  | 
|  1059       pOpen->pNext = openList; |  | 
|  1060       if( openList ) openList->pPrev = pOpen; |  | 
|  1061       openList = pOpen; |  | 
|  1062     }else{ |  | 
|  1063       pOpen->nRef++; |  | 
|  1064     } |  | 
|  1065     *ppOpen = pOpen; |  | 
|  1066   } |  | 
|  1067  |  | 
|  1068 exit_findlockinfo: |  | 
|  1069   return rc; |  | 
|  1070 } |  | 
|  1071  |  | 
|  1072 /* |  | 
|  1073 ** If we are currently in a different thread than the thread that the |  | 
|  1074 ** unixFile argument belongs to, then transfer ownership of the unixFile |  | 
|  1075 ** over to the current thread. |  | 
|  1076 ** |  | 
|  1077 ** A unixFile is only owned by a thread on systems that use LinuxThreads. |  | 
|  1078 ** |  | 
|  1079 ** Ownership transfer is only allowed if the unixFile is currently unlocked. |  | 
|  1080 ** If the unixFile is locked and an ownership is wrong, then return |  | 
|  1081 ** SQLITE_MISUSE.  SQLITE_OK is returned if everything works. |  | 
|  1082 */ |  | 
|  1083 #if SQLITE_THREADSAFE && defined(__linux__) |  | 
|  1084 static int transferOwnership(unixFile *pFile){ |  | 
|  1085   int rc; |  | 
|  1086   pthread_t hSelf; |  | 
|  1087   if( threadsOverrideEachOthersLocks ){ |  | 
|  1088     /* Ownership transfers not needed on this system */ |  | 
|  1089     return SQLITE_OK; |  | 
|  1090   } |  | 
|  1091   hSelf = pthread_self(); |  | 
|  1092   if( pthread_equal(pFile->tid, hSelf) ){ |  | 
|  1093     /* We are still in the same thread */ |  | 
|  1094     OSTRACE1("No-transfer, same thread\n"); |  | 
|  1095     return SQLITE_OK; |  | 
|  1096   } |  | 
|  1097   if( pFile->locktype!=NO_LOCK ){ |  | 
|  1098     /* We cannot change ownership while we are holding a lock! */ |  | 
|  1099     return SQLITE_MISUSE; |  | 
|  1100   } |  | 
|  1101   OSTRACE4("Transfer ownership of %d from %d to %d\n", |  | 
|  1102             pFile->h, pFile->tid, hSelf); |  | 
|  1103   pFile->tid = hSelf; |  | 
|  1104   if (pFile->pLock != NULL) { |  | 
|  1105     releaseLockInfo(pFile->pLock); |  | 
|  1106     rc = findLockInfo(pFile, &pFile->pLock, 0); |  | 
|  1107     OSTRACE5("LOCK    %d is now %s(%s,%d)\n", pFile->h, |  | 
|  1108            locktypeName(pFile->locktype), |  | 
|  1109            locktypeName(pFile->pLock->locktype), pFile->pLock->cnt); |  | 
|  1110     return rc; |  | 
|  1111   } else { |  | 
|  1112     return SQLITE_OK; |  | 
|  1113   } |  | 
|  1114 } |  | 
|  1115 #else  /* if not SQLITE_THREADSAFE */ |  | 
|  1116   /* On single-threaded builds, ownership transfer is a no-op */ |  | 
|  1117 # define transferOwnership(X) SQLITE_OK |  | 
|  1118 #endif /* SQLITE_THREADSAFE */ |  | 
|  1119  |  | 
|  1120  |  | 
|  1121 /* |  | 
|  1122 ** This routine checks if there is a RESERVED lock held on the specified |  | 
|  1123 ** file by this or any other process. If such a lock is held, set *pResOut |  | 
|  1124 ** to a non-zero value otherwise *pResOut is set to zero.  The return value |  | 
|  1125 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |  | 
|  1126 */ |  | 
|  1127 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |  | 
|  1128   int rc = SQLITE_OK; |  | 
|  1129   int reserved = 0; |  | 
|  1130   unixFile *pFile = (unixFile*)id; |  | 
|  1131  |  | 
|  1132   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |  | 
|  1133  |  | 
|  1134   assert( pFile ); |  | 
|  1135   unixEnterMutex(); /* Because pFile->pLock is shared across threads */ |  | 
|  1136  |  | 
|  1137   /* Check if a thread in this process holds such a lock */ |  | 
|  1138   if( pFile->pLock->locktype>SHARED_LOCK ){ |  | 
|  1139     reserved = 1; |  | 
|  1140   } |  | 
|  1141  |  | 
|  1142   /* Otherwise see if some other process holds it. |  | 
|  1143   */ |  | 
|  1144 #ifndef __DJGPP__ |  | 
|  1145   if( !reserved ){ |  | 
|  1146     struct flock lock; |  | 
|  1147     lock.l_whence = SEEK_SET; |  | 
|  1148     lock.l_start = RESERVED_BYTE; |  | 
|  1149     lock.l_len = 1; |  | 
|  1150     lock.l_type = F_WRLCK; |  | 
|  1151     if (-1 == fcntl(pFile->h, F_GETLK, &lock)) { |  | 
|  1152       int tErrno = errno; |  | 
|  1153       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |  | 
|  1154       pFile->lastErrno = tErrno; |  | 
|  1155     } else if( lock.l_type!=F_UNLCK ){ |  | 
|  1156       reserved = 1; |  | 
|  1157     } |  | 
|  1158   } |  | 
|  1159 #endif |  | 
|  1160    |  | 
|  1161   unixLeaveMutex(); |  | 
|  1162   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |  | 
|  1163  |  | 
|  1164   *pResOut = reserved; |  | 
|  1165   return rc; |  | 
|  1166 } |  | 
|  1167  |  | 
|  1168 /* |  | 
|  1169 ** Perform a file locking operation on a range of bytes in a file. |  | 
|  1170 ** The "op" parameter should be one of F_RDLCK, F_WRLCK, or F_UNLCK. |  | 
|  1171 ** Return 0 on success or -1 for failure.  On failure, write the error |  | 
|  1172 ** code into *pErrcode. |  | 
|  1173 ** |  | 
|  1174 ** If the SQLITE_WHOLE_FILE_LOCKING bit is clear, then only lock |  | 
|  1175 ** the range of bytes on the locking page between SHARED_FIRST and |  | 
|  1176 ** SHARED_SIZE.  If SQLITE_WHOLE_FILE_LOCKING is set, then lock all |  | 
|  1177 ** bytes from 0 up to but not including PENDING_BYTE, and all bytes |  | 
|  1178 ** that follow SHARED_FIRST. |  | 
|  1179 ** |  | 
|  1180 ** In other words, of SQLITE_WHOLE_FILE_LOCKING if false (the historical |  | 
|  1181 ** default case) then only lock a small range of bytes from SHARED_FIRST |  | 
|  1182 ** through SHARED_FIRST+SHARED_SIZE-1.  But if SQLITE_WHOLE_FILE_LOCKING is |  | 
|  1183 ** true then lock every byte in the file except for PENDING_BYTE and |  | 
|  1184 ** RESERVED_BYTE. |  | 
|  1185 ** |  | 
|  1186 ** SQLITE_WHOLE_FILE_LOCKING=true overlaps SQLITE_WHOLE_FILE_LOCKING=false |  | 
|  1187 ** and so the locking schemes are compatible.  One type of lock will |  | 
|  1188 ** effectively exclude the other type.  The reason for using the |  | 
|  1189 ** SQLITE_WHOLE_FILE_LOCKING=true is that by indicating the full range |  | 
|  1190 ** of bytes to be read or written, we give hints to NFS to help it |  | 
|  1191 ** maintain cache coherency.  On the other hand, whole file locking |  | 
|  1192 ** is slower, so we don't want to use it except for NFS. |  | 
|  1193 */ |  | 
|  1194 static int rangeLock(unixFile *pFile, int op, int *pErrcode){ |  | 
|  1195   struct flock lock; |  | 
|  1196   int rc; |  | 
|  1197   lock.l_type = op; |  | 
|  1198   lock.l_start = SHARED_FIRST; |  | 
|  1199   lock.l_whence = SEEK_SET; |  | 
|  1200   if( (pFile->fileFlags & SQLITE_WHOLE_FILE_LOCKING)==0 ){ |  | 
|  1201     lock.l_len = SHARED_SIZE; |  | 
|  1202     rc = fcntl(pFile->h, F_SETLK, &lock); |  | 
|  1203     *pErrcode = errno; |  | 
|  1204   }else{ |  | 
|  1205     lock.l_len = 0; |  | 
|  1206     rc = fcntl(pFile->h, F_SETLK, &lock); |  | 
|  1207     *pErrcode = errno; |  | 
|  1208     if( NEVER(op==F_UNLCK) || rc!=(-1) ){ |  | 
|  1209       lock.l_start = 0; |  | 
|  1210       lock.l_len = PENDING_BYTE; |  | 
|  1211       rc = fcntl(pFile->h, F_SETLK, &lock); |  | 
|  1212       if( ALWAYS(op!=F_UNLCK) && rc==(-1) ){ |  | 
|  1213         *pErrcode = errno; |  | 
|  1214         lock.l_type = F_UNLCK; |  | 
|  1215         lock.l_start = SHARED_FIRST; |  | 
|  1216         lock.l_len = 0; |  | 
|  1217         fcntl(pFile->h, F_SETLK, &lock); |  | 
|  1218       } |  | 
|  1219     } |  | 
|  1220   } |  | 
|  1221   return rc; |  | 
|  1222 } |  | 
|  1223  |  | 
|  1224 /* |  | 
|  1225 ** Lock the file with the lock specified by parameter locktype - one |  | 
|  1226 ** of the following: |  | 
|  1227 ** |  | 
|  1228 **     (1) SHARED_LOCK |  | 
|  1229 **     (2) RESERVED_LOCK |  | 
|  1230 **     (3) PENDING_LOCK |  | 
|  1231 **     (4) EXCLUSIVE_LOCK |  | 
|  1232 ** |  | 
|  1233 ** Sometimes when requesting one lock state, additional lock states |  | 
|  1234 ** are inserted in between.  The locking might fail on one of the later |  | 
|  1235 ** transitions leaving the lock state different from what it started but |  | 
|  1236 ** still short of its goal.  The following chart shows the allowed |  | 
|  1237 ** transitions and the inserted intermediate states: |  | 
|  1238 ** |  | 
|  1239 **    UNLOCKED -> SHARED |  | 
|  1240 **    SHARED -> RESERVED |  | 
|  1241 **    SHARED -> (PENDING) -> EXCLUSIVE |  | 
|  1242 **    RESERVED -> (PENDING) -> EXCLUSIVE |  | 
|  1243 **    PENDING -> EXCLUSIVE |  | 
|  1244 ** |  | 
|  1245 ** This routine will only increase a lock.  Use the sqlite3OsUnlock() |  | 
|  1246 ** routine to lower a locking level. |  | 
|  1247 */ |  | 
|  1248 static int unixLock(sqlite3_file *id, int locktype){ |  | 
|  1249   /* The following describes the implementation of the various locks and |  | 
|  1250   ** lock transitions in terms of the POSIX advisory shared and exclusive |  | 
|  1251   ** lock primitives (called read-locks and write-locks below, to avoid |  | 
|  1252   ** confusion with SQLite lock names). The algorithms are complicated |  | 
|  1253   ** slightly in order to be compatible with windows systems simultaneously |  | 
|  1254   ** accessing the same database file, in case that is ever required. |  | 
|  1255   ** |  | 
|  1256   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |  | 
|  1257   ** byte', each single bytes at well known offsets, and the 'shared byte |  | 
|  1258   ** range', a range of 510 bytes at a well known offset. |  | 
|  1259   ** |  | 
|  1260   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |  | 
|  1261   ** byte'.  If this is successful, a random byte from the 'shared byte |  | 
|  1262   ** range' is read-locked and the lock on the 'pending byte' released. |  | 
|  1263   ** |  | 
|  1264   ** A process may only obtain a RESERVED lock after it has a SHARED lock. |  | 
|  1265   ** A RESERVED lock is implemented by grabbing a write-lock on the |  | 
|  1266   ** 'reserved byte'.  |  | 
|  1267   ** |  | 
|  1268   ** A process may only obtain a PENDING lock after it has obtained a |  | 
|  1269   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |  | 
|  1270   ** on the 'pending byte'. This ensures that no new SHARED locks can be |  | 
|  1271   ** obtained, but existing SHARED locks are allowed to persist. A process |  | 
|  1272   ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |  | 
|  1273   ** This property is used by the algorithm for rolling back a journal file |  | 
|  1274   ** after a crash. |  | 
|  1275   ** |  | 
|  1276   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |  | 
|  1277   ** implemented by obtaining a write-lock on the entire 'shared byte |  | 
|  1278   ** range'. Since all other locks require a read-lock on one of the bytes |  | 
|  1279   ** within this range, this ensures that no other locks are held on the |  | 
|  1280   ** database.  |  | 
|  1281   ** |  | 
|  1282   ** The reason a single byte cannot be used instead of the 'shared byte |  | 
|  1283   ** range' is that some versions of windows do not support read-locks. By |  | 
|  1284   ** locking a random byte from a range, concurrent SHARED locks may exist |  | 
|  1285   ** even if the locking primitive used is always a write-lock. |  | 
|  1286   */ |  | 
|  1287   int rc = SQLITE_OK; |  | 
|  1288   unixFile *pFile = (unixFile*)id; |  | 
|  1289   struct unixLockInfo *pLock = pFile->pLock; |  | 
|  1290   struct flock lock; |  | 
|  1291   int s = 0; |  | 
|  1292   int tErrno; |  | 
|  1293  |  | 
|  1294   assert( pFile ); |  | 
|  1295   OSTRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h, |  | 
|  1296       locktypeName(locktype), locktypeName(pFile->locktype), |  | 
|  1297       locktypeName(pLock->locktype), pLock->cnt , getpid()); |  | 
|  1298  |  | 
|  1299   /* If there is already a lock of this type or more restrictive on the |  | 
|  1300   ** unixFile, do nothing. Don't use the end_lock: exit path, as |  | 
|  1301   ** unixEnterMutex() hasn't been called yet. |  | 
|  1302   */ |  | 
|  1303   if( pFile->locktype>=locktype ){ |  | 
|  1304     OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h, |  | 
|  1305             locktypeName(locktype)); |  | 
|  1306     return SQLITE_OK; |  | 
|  1307   } |  | 
|  1308  |  | 
|  1309   /* Make sure the locking sequence is correct. |  | 
|  1310   **  (1) We never move from unlocked to anything higher than shared lock. |  | 
|  1311   **  (2) SQLite never explicitly requests a pendig lock. |  | 
|  1312   **  (3) A shared lock is always held when a reserve lock is requested. |  | 
|  1313   */ |  | 
|  1314   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |  | 
|  1315   assert( locktype!=PENDING_LOCK ); |  | 
|  1316   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |  | 
|  1317  |  | 
|  1318   /* This mutex is needed because pFile->pLock is shared across threads |  | 
|  1319   */ |  | 
|  1320   unixEnterMutex(); |  | 
|  1321  |  | 
|  1322   /* Make sure the current thread owns the pFile. |  | 
|  1323   */ |  | 
|  1324   rc = transferOwnership(pFile); |  | 
|  1325   if( rc!=SQLITE_OK ){ |  | 
|  1326     unixLeaveMutex(); |  | 
|  1327     return rc; |  | 
|  1328   } |  | 
|  1329   pLock = pFile->pLock; |  | 
|  1330  |  | 
|  1331   /* If some thread using this PID has a lock via a different unixFile* |  | 
|  1332   ** handle that precludes the requested lock, return BUSY. |  | 
|  1333   */ |  | 
|  1334   if( (pFile->locktype!=pLock->locktype &&  |  | 
|  1335           (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK)) |  | 
|  1336   ){ |  | 
|  1337     rc = SQLITE_BUSY; |  | 
|  1338     goto end_lock; |  | 
|  1339   } |  | 
|  1340  |  | 
|  1341   /* If a SHARED lock is requested, and some thread using this PID already |  | 
|  1342   ** has a SHARED or RESERVED lock, then increment reference counts and |  | 
|  1343   ** return SQLITE_OK. |  | 
|  1344   */ |  | 
|  1345   if( locktype==SHARED_LOCK &&  |  | 
|  1346       (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){ |  | 
|  1347     assert( locktype==SHARED_LOCK ); |  | 
|  1348     assert( pFile->locktype==0 ); |  | 
|  1349     assert( pLock->cnt>0 ); |  | 
|  1350     pFile->locktype = SHARED_LOCK; |  | 
|  1351     pLock->cnt++; |  | 
|  1352     pFile->pOpen->nLock++; |  | 
|  1353     goto end_lock; |  | 
|  1354   } |  | 
|  1355  |  | 
|  1356  |  | 
|  1357   /* A PENDING lock is needed before acquiring a SHARED lock and before |  | 
|  1358   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will |  | 
|  1359   ** be released. |  | 
|  1360   */ |  | 
|  1361   lock.l_len = 1L; |  | 
|  1362   lock.l_whence = SEEK_SET; |  | 
|  1363   if( locktype==SHARED_LOCK  |  | 
|  1364       || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |  | 
|  1365   ){ |  | 
|  1366     lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK); |  | 
|  1367     lock.l_start = PENDING_BYTE; |  | 
|  1368     s = fcntl(pFile->h, F_SETLK, &lock); |  | 
|  1369     if( s==(-1) ){ |  | 
|  1370       tErrno = errno; |  | 
|  1371       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |  | 
|  1372       if( IS_LOCK_ERROR(rc) ){ |  | 
|  1373         pFile->lastErrno = tErrno; |  | 
|  1374       } |  | 
|  1375       goto end_lock; |  | 
|  1376     } |  | 
|  1377   } |  | 
|  1378  |  | 
|  1379  |  | 
|  1380   /* If control gets to this point, then actually go ahead and make |  | 
|  1381   ** operating system calls for the specified lock. |  | 
|  1382   */ |  | 
|  1383   if( locktype==SHARED_LOCK ){ |  | 
|  1384     assert( pLock->cnt==0 ); |  | 
|  1385     assert( pLock->locktype==0 ); |  | 
|  1386  |  | 
|  1387     /* Now get the read-lock */ |  | 
|  1388     s = rangeLock(pFile, F_RDLCK, &tErrno); |  | 
|  1389  |  | 
|  1390     /* Drop the temporary PENDING lock */ |  | 
|  1391     lock.l_start = PENDING_BYTE; |  | 
|  1392     lock.l_len = 1L; |  | 
|  1393     lock.l_type = F_UNLCK; |  | 
|  1394     if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |  | 
|  1395       if( s != -1 ){ |  | 
|  1396         /* This could happen with a network mount */ |  | 
|  1397         tErrno = errno;  |  | 
|  1398         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);  |  | 
|  1399         if( IS_LOCK_ERROR(rc) ){ |  | 
|  1400           pFile->lastErrno = tErrno; |  | 
|  1401         } |  | 
|  1402         goto end_lock; |  | 
|  1403       } |  | 
|  1404     } |  | 
|  1405     if( s==(-1) ){ |  | 
|  1406       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |  | 
|  1407       if( IS_LOCK_ERROR(rc) ){ |  | 
|  1408         pFile->lastErrno = tErrno; |  | 
|  1409       } |  | 
|  1410     }else{ |  | 
|  1411       pFile->locktype = SHARED_LOCK; |  | 
|  1412       pFile->pOpen->nLock++; |  | 
|  1413       pLock->cnt = 1; |  | 
|  1414     } |  | 
|  1415   }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){ |  | 
|  1416     /* We are trying for an exclusive lock but another thread in this |  | 
|  1417     ** same process is still holding a shared lock. */ |  | 
|  1418     rc = SQLITE_BUSY; |  | 
|  1419   }else{ |  | 
|  1420     /* The request was for a RESERVED or EXCLUSIVE lock.  It is |  | 
|  1421     ** assumed that there is a SHARED or greater lock on the file |  | 
|  1422     ** already. |  | 
|  1423     */ |  | 
|  1424     assert( 0!=pFile->locktype ); |  | 
|  1425     lock.l_type = F_WRLCK; |  | 
|  1426     switch( locktype ){ |  | 
|  1427       case RESERVED_LOCK: |  | 
|  1428         lock.l_start = RESERVED_BYTE; |  | 
|  1429         s = fcntl(pFile->h, F_SETLK, &lock); |  | 
|  1430         tErrno = errno; |  | 
|  1431         break; |  | 
|  1432       case EXCLUSIVE_LOCK: |  | 
|  1433         s = rangeLock(pFile, F_WRLCK, &tErrno); |  | 
|  1434         break; |  | 
|  1435       default: |  | 
|  1436         assert(0); |  | 
|  1437     } |  | 
|  1438     if( s==(-1) ){ |  | 
|  1439       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |  | 
|  1440       if( IS_LOCK_ERROR(rc) ){ |  | 
|  1441         pFile->lastErrno = tErrno; |  | 
|  1442       } |  | 
|  1443     } |  | 
|  1444   } |  | 
|  1445    |  | 
|  1446  |  | 
|  1447 #ifndef NDEBUG |  | 
|  1448   /* Set up the transaction-counter change checking flags when |  | 
|  1449   ** transitioning from a SHARED to a RESERVED lock.  The change |  | 
|  1450   ** from SHARED to RESERVED marks the beginning of a normal |  | 
|  1451   ** write operation (not a hot journal rollback). |  | 
|  1452   */ |  | 
|  1453   if( rc==SQLITE_OK |  | 
|  1454    && pFile->locktype<=SHARED_LOCK |  | 
|  1455    && locktype==RESERVED_LOCK |  | 
|  1456   ){ |  | 
|  1457     pFile->transCntrChng = 0; |  | 
|  1458     pFile->dbUpdate = 0; |  | 
|  1459     pFile->inNormalWrite = 1; |  | 
|  1460   } |  | 
|  1461 #endif |  | 
|  1462  |  | 
|  1463  |  | 
|  1464   if( rc==SQLITE_OK ){ |  | 
|  1465     pFile->locktype = locktype; |  | 
|  1466     pLock->locktype = locktype; |  | 
|  1467   }else if( locktype==EXCLUSIVE_LOCK ){ |  | 
|  1468     pFile->locktype = PENDING_LOCK; |  | 
|  1469     pLock->locktype = PENDING_LOCK; |  | 
|  1470   } |  | 
|  1471  |  | 
|  1472 end_lock: |  | 
|  1473   unixLeaveMutex(); |  | 
|  1474   OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype),  |  | 
|  1475       rc==SQLITE_OK ? "ok" : "failed"); |  | 
|  1476   return rc; |  | 
|  1477 } |  | 
|  1478  |  | 
|  1479 /* |  | 
|  1480 ** Close all file descriptors accumuated in the unixOpenCnt->pUnused list. |  | 
|  1481 ** If all such file descriptors are closed without error, the list is |  | 
|  1482 ** cleared and SQLITE_OK returned. |  | 
|  1483 ** |  | 
|  1484 ** Otherwise, if an error occurs, then successfully closed file descriptor |  | 
|  1485 ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned.  |  | 
|  1486 ** not deleted and SQLITE_IOERR_CLOSE returned. |  | 
|  1487 */  |  | 
|  1488 static int closePendingFds(unixFile *pFile){ |  | 
|  1489   int rc = SQLITE_OK; |  | 
|  1490   struct unixOpenCnt *pOpen = pFile->pOpen; |  | 
|  1491   UnixUnusedFd *pError = 0; |  | 
|  1492   UnixUnusedFd *p; |  | 
|  1493   UnixUnusedFd *pNext; |  | 
|  1494   for(p=pOpen->pUnused; p; p=pNext){ |  | 
|  1495     pNext = p->pNext; |  | 
|  1496     if( close(p->fd) ){ |  | 
|  1497       pFile->lastErrno = errno; |  | 
|  1498       rc = SQLITE_IOERR_CLOSE; |  | 
|  1499       p->pNext = pError; |  | 
|  1500       pError = p; |  | 
|  1501     }else{ |  | 
|  1502       sqlite3_free(p); |  | 
|  1503     } |  | 
|  1504   } |  | 
|  1505   pOpen->pUnused = pError; |  | 
|  1506   return rc; |  | 
|  1507 } |  | 
|  1508  |  | 
|  1509 /* |  | 
|  1510 ** Add the file descriptor used by file handle pFile to the corresponding |  | 
|  1511 ** pUnused list. |  | 
|  1512 */ |  | 
|  1513 static void setPendingFd(unixFile *pFile){ |  | 
|  1514   struct unixOpenCnt *pOpen = pFile->pOpen; |  | 
|  1515   UnixUnusedFd *p = pFile->pUnused; |  | 
|  1516   p->pNext = pOpen->pUnused; |  | 
|  1517   pOpen->pUnused = p; |  | 
|  1518   pFile->h = -1; |  | 
|  1519   pFile->pUnused = 0; |  | 
|  1520 } |  | 
|  1521  |  | 
|  1522 /* |  | 
|  1523 ** Lower the locking level on file descriptor pFile to locktype.  locktype |  | 
|  1524 ** must be either NO_LOCK or SHARED_LOCK. |  | 
|  1525 ** |  | 
|  1526 ** If the locking level of the file descriptor is already at or below |  | 
|  1527 ** the requested locking level, this routine is a no-op. |  | 
|  1528 */ |  | 
|  1529 static int unixUnlock(sqlite3_file *id, int locktype){ |  | 
|  1530   unixFile *pFile = (unixFile*)id; /* The open file */ |  | 
|  1531   struct unixLockInfo *pLock;      /* Structure describing current lock state */ |  | 
|  1532   struct flock lock;               /* Information passed into fcntl() */ |  | 
|  1533   int rc = SQLITE_OK;              /* Return code from this interface */ |  | 
|  1534   int h;                           /* The underlying file descriptor */ |  | 
|  1535   int tErrno;                      /* Error code from system call errors */ |  | 
|  1536  |  | 
|  1537   assert( pFile ); |  | 
|  1538   OSTRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype, |  | 
|  1539       pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid()); |  | 
|  1540  |  | 
|  1541   assert( locktype<=SHARED_LOCK ); |  | 
|  1542   if( pFile->locktype<=locktype ){ |  | 
|  1543     return SQLITE_OK; |  | 
|  1544   } |  | 
|  1545   if( CHECK_THREADID(pFile) ){ |  | 
|  1546     return SQLITE_MISUSE; |  | 
|  1547   } |  | 
|  1548   unixEnterMutex(); |  | 
|  1549   h = pFile->h; |  | 
|  1550   pLock = pFile->pLock; |  | 
|  1551   assert( pLock->cnt!=0 ); |  | 
|  1552   if( pFile->locktype>SHARED_LOCK ){ |  | 
|  1553     assert( pLock->locktype==pFile->locktype ); |  | 
|  1554     SimulateIOErrorBenign(1); |  | 
|  1555     SimulateIOError( h=(-1) ) |  | 
|  1556     SimulateIOErrorBenign(0); |  | 
|  1557  |  | 
|  1558 #ifndef NDEBUG |  | 
|  1559     /* When reducing a lock such that other processes can start |  | 
|  1560     ** reading the database file again, make sure that the |  | 
|  1561     ** transaction counter was updated if any part of the database |  | 
|  1562     ** file changed.  If the transaction counter is not updated, |  | 
|  1563     ** other connections to the same file might not realize that |  | 
|  1564     ** the file has changed and hence might not know to flush their |  | 
|  1565     ** cache.  The use of a stale cache can lead to database corruption. |  | 
|  1566     */ |  | 
|  1567     assert( pFile->inNormalWrite==0 |  | 
|  1568          || pFile->dbUpdate==0 |  | 
|  1569          || pFile->transCntrChng==1 ); |  | 
|  1570     pFile->inNormalWrite = 0; |  | 
|  1571 #endif |  | 
|  1572  |  | 
|  1573  |  | 
|  1574     if( locktype==SHARED_LOCK ){ |  | 
|  1575       if( rangeLock(pFile, F_RDLCK, &tErrno)==(-1) ){ |  | 
|  1576         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |  | 
|  1577         if( IS_LOCK_ERROR(rc) ){ |  | 
|  1578           pFile->lastErrno = tErrno; |  | 
|  1579         } |  | 
|  1580         goto end_unlock; |  | 
|  1581       } |  | 
|  1582     } |  | 
|  1583     lock.l_type = F_UNLCK; |  | 
|  1584     lock.l_whence = SEEK_SET; |  | 
|  1585     lock.l_start = PENDING_BYTE; |  | 
|  1586     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE ); |  | 
|  1587     if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |  | 
|  1588       pLock->locktype = SHARED_LOCK; |  | 
|  1589     }else{ |  | 
|  1590       tErrno = errno; |  | 
|  1591       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |  | 
|  1592       if( IS_LOCK_ERROR(rc) ){ |  | 
|  1593         pFile->lastErrno = tErrno; |  | 
|  1594       } |  | 
|  1595       goto end_unlock; |  | 
|  1596     } |  | 
|  1597   } |  | 
|  1598   if( locktype==NO_LOCK ){ |  | 
|  1599     struct unixOpenCnt *pOpen; |  | 
|  1600  |  | 
|  1601     /* Decrement the shared lock counter.  Release the lock using an |  | 
|  1602     ** OS call only when all threads in this same process have released |  | 
|  1603     ** the lock. |  | 
|  1604     */ |  | 
|  1605     pLock->cnt--; |  | 
|  1606     if( pLock->cnt==0 ){ |  | 
|  1607       lock.l_type = F_UNLCK; |  | 
|  1608       lock.l_whence = SEEK_SET; |  | 
|  1609       lock.l_start = lock.l_len = 0L; |  | 
|  1610       SimulateIOErrorBenign(1); |  | 
|  1611       SimulateIOError( h=(-1) ) |  | 
|  1612       SimulateIOErrorBenign(0); |  | 
|  1613       if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |  | 
|  1614         pLock->locktype = NO_LOCK; |  | 
|  1615       }else{ |  | 
|  1616         tErrno = errno; |  | 
|  1617         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |  | 
|  1618         if( IS_LOCK_ERROR(rc) ){ |  | 
|  1619           pFile->lastErrno = tErrno; |  | 
|  1620         } |  | 
|  1621         pLock->locktype = NO_LOCK; |  | 
|  1622         pFile->locktype = NO_LOCK; |  | 
|  1623       } |  | 
|  1624     } |  | 
|  1625  |  | 
|  1626     /* Decrement the count of locks against this same file.  When the |  | 
|  1627     ** count reaches zero, close any other file descriptors whose close |  | 
|  1628     ** was deferred because of outstanding locks. |  | 
|  1629     */ |  | 
|  1630     pOpen = pFile->pOpen; |  | 
|  1631     pOpen->nLock--; |  | 
|  1632     assert( pOpen->nLock>=0 ); |  | 
|  1633     if( pOpen->nLock==0 ){ |  | 
|  1634       int rc2 = closePendingFds(pFile); |  | 
|  1635       if( rc==SQLITE_OK ){ |  | 
|  1636         rc = rc2; |  | 
|  1637       } |  | 
|  1638     } |  | 
|  1639   } |  | 
|  1640          |  | 
|  1641 end_unlock: |  | 
|  1642   unixLeaveMutex(); |  | 
|  1643   if( rc==SQLITE_OK ) pFile->locktype = locktype; |  | 
|  1644   return rc; |  | 
|  1645 } |  | 
|  1646  |  | 
|  1647 /* |  | 
|  1648 ** This function performs the parts of the "close file" operation  |  | 
|  1649 ** common to all locking schemes. It closes the directory and file |  | 
|  1650 ** handles, if they are valid, and sets all fields of the unixFile |  | 
|  1651 ** structure to 0. |  | 
|  1652 ** |  | 
|  1653 ** It is *not* necessary to hold the mutex when this routine is called, |  | 
|  1654 ** even on VxWorks.  A mutex will be acquired on VxWorks by the |  | 
|  1655 ** vxworksReleaseFileId() routine. |  | 
|  1656 */ |  | 
|  1657 static int closeUnixFile(sqlite3_file *id){ |  | 
|  1658   unixFile *pFile = (unixFile*)id; |  | 
|  1659   if( pFile ){ |  | 
|  1660     if( pFile->dirfd>=0 ){ |  | 
|  1661       int err = close(pFile->dirfd); |  | 
|  1662       if( err ){ |  | 
|  1663         pFile->lastErrno = errno; |  | 
|  1664         return SQLITE_IOERR_DIR_CLOSE; |  | 
|  1665       }else{ |  | 
|  1666         pFile->dirfd=-1; |  | 
|  1667       } |  | 
|  1668     } |  | 
|  1669     if( pFile->h>=0 ){ |  | 
|  1670       int err = close(pFile->h); |  | 
|  1671       if( err ){ |  | 
|  1672         pFile->lastErrno = errno; |  | 
|  1673         return SQLITE_IOERR_CLOSE; |  | 
|  1674       } |  | 
|  1675     } |  | 
|  1676 #if OS_VXWORKS |  | 
|  1677     if( pFile->pId ){ |  | 
|  1678       if( pFile->isDelete ){ |  | 
|  1679         unlink(pFile->pId->zCanonicalName); |  | 
|  1680       } |  | 
|  1681       vxworksReleaseFileId(pFile->pId); |  | 
|  1682       pFile->pId = 0; |  | 
|  1683     } |  | 
|  1684 #endif |  | 
|  1685     OSTRACE2("CLOSE   %-3d\n", pFile->h); |  | 
|  1686     OpenCounter(-1); |  | 
|  1687     sqlite3_free(pFile->pUnused); |  | 
|  1688     memset(pFile, 0, sizeof(unixFile)); |  | 
|  1689   } |  | 
|  1690   return SQLITE_OK; |  | 
|  1691 } |  | 
|  1692  |  | 
|  1693 /* |  | 
|  1694 ** Close a file. |  | 
|  1695 */ |  | 
|  1696 static int unixClose(sqlite3_file *id){ |  | 
|  1697   int rc = SQLITE_OK; |  | 
|  1698   if( id ){ |  | 
|  1699     unixFile *pFile = (unixFile *)id; |  | 
|  1700     unixUnlock(id, NO_LOCK); |  | 
|  1701     unixEnterMutex(); |  | 
|  1702     if( pFile->pOpen && pFile->pOpen->nLock ){ |  | 
|  1703       /* If there are outstanding locks, do not actually close the file just |  | 
|  1704       ** yet because that would clear those locks.  Instead, add the file |  | 
|  1705       ** descriptor to pOpen->pUnused list.  It will be automatically closed  |  | 
|  1706       ** when the last lock is cleared. |  | 
|  1707       */ |  | 
|  1708       setPendingFd(pFile); |  | 
|  1709     } |  | 
|  1710     releaseLockInfo(pFile->pLock); |  | 
|  1711     releaseOpenCnt(pFile->pOpen); |  | 
|  1712     rc = closeUnixFile(id); |  | 
|  1713     unixLeaveMutex(); |  | 
|  1714   } |  | 
|  1715   return rc; |  | 
|  1716 } |  | 
|  1717  |  | 
|  1718 /************** End of the posix advisory lock implementation ***************** |  | 
|  1719 ******************************************************************************/ |  | 
|  1720  |  | 
|  1721 /****************************************************************************** |  | 
|  1722 ****************************** No-op Locking ********************************** |  | 
|  1723 ** |  | 
|  1724 ** Of the various locking implementations available, this is by far the |  | 
|  1725 ** simplest:  locking is ignored.  No attempt is made to lock the database |  | 
|  1726 ** file for reading or writing. |  | 
|  1727 ** |  | 
|  1728 ** This locking mode is appropriate for use on read-only databases |  | 
|  1729 ** (ex: databases that are burned into CD-ROM, for example.)  It can |  | 
|  1730 ** also be used if the application employs some external mechanism to |  | 
|  1731 ** prevent simultaneous access of the same database by two or more |  | 
|  1732 ** database connections.  But there is a serious risk of database |  | 
|  1733 ** corruption if this locking mode is used in situations where multiple |  | 
|  1734 ** database connections are accessing the same database file at the same |  | 
|  1735 ** time and one or more of those connections are writing. |  | 
|  1736 */ |  | 
|  1737  |  | 
|  1738 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |  | 
|  1739   UNUSED_PARAMETER(NotUsed); |  | 
|  1740   *pResOut = 0; |  | 
|  1741   return SQLITE_OK; |  | 
|  1742 } |  | 
|  1743 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |  | 
|  1744   UNUSED_PARAMETER2(NotUsed, NotUsed2); |  | 
|  1745   return SQLITE_OK; |  | 
|  1746 } |  | 
|  1747 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |  | 
|  1748   UNUSED_PARAMETER2(NotUsed, NotUsed2); |  | 
|  1749   return SQLITE_OK; |  | 
|  1750 } |  | 
|  1751  |  | 
|  1752 /* |  | 
|  1753 ** Close the file. |  | 
|  1754 */ |  | 
|  1755 static int nolockClose(sqlite3_file *id) { |  | 
|  1756   return closeUnixFile(id); |  | 
|  1757 } |  | 
|  1758  |  | 
|  1759 /******************* End of the no-op lock implementation ********************* |  | 
|  1760 ******************************************************************************/ |  | 
|  1761  |  | 
|  1762 /****************************************************************************** |  | 
|  1763 ************************* Begin dot-file Locking ****************************** |  | 
|  1764 ** |  | 
|  1765 ** The dotfile locking implementation uses the existance of separate lock |  | 
|  1766 ** files in order to control access to the database.  This works on just |  | 
|  1767 ** about every filesystem imaginable.  But there are serious downsides: |  | 
|  1768 ** |  | 
|  1769 **    (1)  There is zero concurrency.  A single reader blocks all other |  | 
|  1770 **         connections from reading or writing the database. |  | 
|  1771 ** |  | 
|  1772 **    (2)  An application crash or power loss can leave stale lock files |  | 
|  1773 **         sitting around that need to be cleared manually. |  | 
|  1774 ** |  | 
|  1775 ** Nevertheless, a dotlock is an appropriate locking mode for use if no |  | 
|  1776 ** other locking strategy is available. |  | 
|  1777 ** |  | 
|  1778 ** Dotfile locking works by creating a file in the same directory as the |  | 
|  1779 ** database and with the same name but with a ".lock" extension added. |  | 
|  1780 ** The existance of a lock file implies an EXCLUSIVE lock.  All other lock |  | 
|  1781 ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |  | 
|  1782 */ |  | 
|  1783  |  | 
|  1784 /* |  | 
|  1785 ** The file suffix added to the data base filename in order to create the |  | 
|  1786 ** lock file. |  | 
|  1787 */ |  | 
|  1788 #define DOTLOCK_SUFFIX ".lock" |  | 
|  1789  |  | 
|  1790 /* |  | 
|  1791 ** This routine checks if there is a RESERVED lock held on the specified |  | 
|  1792 ** file by this or any other process. If such a lock is held, set *pResOut |  | 
|  1793 ** to a non-zero value otherwise *pResOut is set to zero.  The return value |  | 
|  1794 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |  | 
|  1795 ** |  | 
|  1796 ** In dotfile locking, either a lock exists or it does not.  So in this |  | 
|  1797 ** variation of CheckReservedLock(), *pResOut is set to true if any lock |  | 
|  1798 ** is held on the file and false if the file is unlocked. |  | 
|  1799 */ |  | 
|  1800 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |  | 
|  1801   int rc = SQLITE_OK; |  | 
|  1802   int reserved = 0; |  | 
|  1803   unixFile *pFile = (unixFile*)id; |  | 
|  1804  |  | 
|  1805   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |  | 
|  1806    |  | 
|  1807   assert( pFile ); |  | 
|  1808  |  | 
|  1809   /* Check if a thread in this process holds such a lock */ |  | 
|  1810   if( pFile->locktype>SHARED_LOCK ){ |  | 
|  1811     /* Either this connection or some other connection in the same process |  | 
|  1812     ** holds a lock on the file.  No need to check further. */ |  | 
|  1813     reserved = 1; |  | 
|  1814   }else{ |  | 
|  1815     /* The lock is held if and only if the lockfile exists */ |  | 
|  1816     const char *zLockFile = (const char*)pFile->lockingContext; |  | 
|  1817     reserved = access(zLockFile, 0)==0; |  | 
|  1818   } |  | 
|  1819   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |  | 
|  1820   *pResOut = reserved; |  | 
|  1821   return rc; |  | 
|  1822 } |  | 
|  1823  |  | 
|  1824 /* |  | 
|  1825 ** Lock the file with the lock specified by parameter locktype - one |  | 
|  1826 ** of the following: |  | 
|  1827 ** |  | 
|  1828 **     (1) SHARED_LOCK |  | 
|  1829 **     (2) RESERVED_LOCK |  | 
|  1830 **     (3) PENDING_LOCK |  | 
|  1831 **     (4) EXCLUSIVE_LOCK |  | 
|  1832 ** |  | 
|  1833 ** Sometimes when requesting one lock state, additional lock states |  | 
|  1834 ** are inserted in between.  The locking might fail on one of the later |  | 
|  1835 ** transitions leaving the lock state different from what it started but |  | 
|  1836 ** still short of its goal.  The following chart shows the allowed |  | 
|  1837 ** transitions and the inserted intermediate states: |  | 
|  1838 ** |  | 
|  1839 **    UNLOCKED -> SHARED |  | 
|  1840 **    SHARED -> RESERVED |  | 
|  1841 **    SHARED -> (PENDING) -> EXCLUSIVE |  | 
|  1842 **    RESERVED -> (PENDING) -> EXCLUSIVE |  | 
|  1843 **    PENDING -> EXCLUSIVE |  | 
|  1844 ** |  | 
|  1845 ** This routine will only increase a lock.  Use the sqlite3OsUnlock() |  | 
|  1846 ** routine to lower a locking level. |  | 
|  1847 ** |  | 
|  1848 ** With dotfile locking, we really only support state (4): EXCLUSIVE. |  | 
|  1849 ** But we track the other locking levels internally. |  | 
|  1850 */ |  | 
|  1851 static int dotlockLock(sqlite3_file *id, int locktype) { |  | 
|  1852   unixFile *pFile = (unixFile*)id; |  | 
|  1853   int fd; |  | 
|  1854   char *zLockFile = (char *)pFile->lockingContext; |  | 
|  1855   int rc = SQLITE_OK; |  | 
|  1856  |  | 
|  1857  |  | 
|  1858   /* If we have any lock, then the lock file already exists.  All we have |  | 
|  1859   ** to do is adjust our internal record of the lock level. |  | 
|  1860   */ |  | 
|  1861   if( pFile->locktype > NO_LOCK ){ |  | 
|  1862     pFile->locktype = locktype; |  | 
|  1863 #if !OS_VXWORKS |  | 
|  1864     /* Always update the timestamp on the old file */ |  | 
|  1865     utimes(zLockFile, NULL); |  | 
|  1866 #endif |  | 
|  1867     return SQLITE_OK; |  | 
|  1868   } |  | 
|  1869    |  | 
|  1870   /* grab an exclusive lock */ |  | 
|  1871   fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |  | 
|  1872   if( fd<0 ){ |  | 
|  1873     /* failed to open/create the file, someone else may have stolen the lock */ |  | 
|  1874     int tErrno = errno; |  | 
|  1875     if( EEXIST == tErrno ){ |  | 
|  1876       rc = SQLITE_BUSY; |  | 
|  1877     } else { |  | 
|  1878       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |  | 
|  1879       if( IS_LOCK_ERROR(rc) ){ |  | 
|  1880         pFile->lastErrno = tErrno; |  | 
|  1881       } |  | 
|  1882     } |  | 
|  1883     return rc; |  | 
|  1884   }  |  | 
|  1885   if( close(fd) ){ |  | 
|  1886     pFile->lastErrno = errno; |  | 
|  1887     rc = SQLITE_IOERR_CLOSE; |  | 
|  1888   } |  | 
|  1889    |  | 
|  1890   /* got it, set the type and return ok */ |  | 
|  1891   pFile->locktype = locktype; |  | 
|  1892   return rc; |  | 
|  1893 } |  | 
|  1894  |  | 
|  1895 /* |  | 
|  1896 ** Lower the locking level on file descriptor pFile to locktype.  locktype |  | 
|  1897 ** must be either NO_LOCK or SHARED_LOCK. |  | 
|  1898 ** |  | 
|  1899 ** If the locking level of the file descriptor is already at or below |  | 
|  1900 ** the requested locking level, this routine is a no-op. |  | 
|  1901 ** |  | 
|  1902 ** When the locking level reaches NO_LOCK, delete the lock file. |  | 
|  1903 */ |  | 
|  1904 static int dotlockUnlock(sqlite3_file *id, int locktype) { |  | 
|  1905   unixFile *pFile = (unixFile*)id; |  | 
|  1906   char *zLockFile = (char *)pFile->lockingContext; |  | 
|  1907  |  | 
|  1908   assert( pFile ); |  | 
|  1909   OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype, |  | 
|  1910            pFile->locktype, getpid()); |  | 
|  1911   assert( locktype<=SHARED_LOCK ); |  | 
|  1912    |  | 
|  1913   /* no-op if possible */ |  | 
|  1914   if( pFile->locktype==locktype ){ |  | 
|  1915     return SQLITE_OK; |  | 
|  1916   } |  | 
|  1917  |  | 
|  1918   /* To downgrade to shared, simply update our internal notion of the |  | 
|  1919   ** lock state.  No need to mess with the file on disk. |  | 
|  1920   */ |  | 
|  1921   if( locktype==SHARED_LOCK ){ |  | 
|  1922     pFile->locktype = SHARED_LOCK; |  | 
|  1923     return SQLITE_OK; |  | 
|  1924   } |  | 
|  1925    |  | 
|  1926   /* To fully unlock the database, delete the lock file */ |  | 
|  1927   assert( locktype==NO_LOCK ); |  | 
|  1928   if( unlink(zLockFile) ){ |  | 
|  1929     int rc = 0; |  | 
|  1930     int tErrno = errno; |  | 
|  1931     if( ENOENT != tErrno ){ |  | 
|  1932       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |  | 
|  1933     } |  | 
|  1934     if( IS_LOCK_ERROR(rc) ){ |  | 
|  1935       pFile->lastErrno = tErrno; |  | 
|  1936     } |  | 
|  1937     return rc;  |  | 
|  1938   } |  | 
|  1939   pFile->locktype = NO_LOCK; |  | 
|  1940   return SQLITE_OK; |  | 
|  1941 } |  | 
|  1942  |  | 
|  1943 /* |  | 
|  1944 ** Close a file.  Make sure the lock has been released before closing. |  | 
|  1945 */ |  | 
|  1946 static int dotlockClose(sqlite3_file *id) { |  | 
|  1947   int rc; |  | 
|  1948   if( id ){ |  | 
|  1949     unixFile *pFile = (unixFile*)id; |  | 
|  1950     dotlockUnlock(id, NO_LOCK); |  | 
|  1951     sqlite3_free(pFile->lockingContext); |  | 
|  1952   } |  | 
|  1953   rc = closeUnixFile(id); |  | 
|  1954   return rc; |  | 
|  1955 } |  | 
|  1956 /****************** End of the dot-file lock implementation ******************* |  | 
|  1957 ******************************************************************************/ |  | 
|  1958  |  | 
|  1959 /****************************************************************************** |  | 
|  1960 ************************** Begin flock Locking ******************************** |  | 
|  1961 ** |  | 
|  1962 ** Use the flock() system call to do file locking. |  | 
|  1963 ** |  | 
|  1964 ** flock() locking is like dot-file locking in that the various |  | 
|  1965 ** fine-grain locking levels supported by SQLite are collapsed into |  | 
|  1966 ** a single exclusive lock.  In other words, SHARED, RESERVED, and |  | 
|  1967 ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite |  | 
|  1968 ** still works when you do this, but concurrency is reduced since |  | 
|  1969 ** only a single process can be reading the database at a time. |  | 
|  1970 ** |  | 
|  1971 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |  | 
|  1972 ** compiling for VXWORKS. |  | 
|  1973 */ |  | 
|  1974 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |  | 
|  1975  |  | 
|  1976 /* |  | 
|  1977 ** This routine checks if there is a RESERVED lock held on the specified |  | 
|  1978 ** file by this or any other process. If such a lock is held, set *pResOut |  | 
|  1979 ** to a non-zero value otherwise *pResOut is set to zero.  The return value |  | 
|  1980 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |  | 
|  1981 */ |  | 
|  1982 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |  | 
|  1983   int rc = SQLITE_OK; |  | 
|  1984   int reserved = 0; |  | 
|  1985   unixFile *pFile = (unixFile*)id; |  | 
|  1986    |  | 
|  1987   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |  | 
|  1988    |  | 
|  1989   assert( pFile ); |  | 
|  1990    |  | 
|  1991   /* Check if a thread in this process holds such a lock */ |  | 
|  1992   if( pFile->locktype>SHARED_LOCK ){ |  | 
|  1993     reserved = 1; |  | 
|  1994   } |  | 
|  1995    |  | 
|  1996   /* Otherwise see if some other process holds it. */ |  | 
|  1997   if( !reserved ){ |  | 
|  1998     /* attempt to get the lock */ |  | 
|  1999     int lrc = flock(pFile->h, LOCK_EX | LOCK_NB); |  | 
|  2000     if( !lrc ){ |  | 
|  2001       /* got the lock, unlock it */ |  | 
|  2002       lrc = flock(pFile->h, LOCK_UN); |  | 
|  2003       if ( lrc ) { |  | 
|  2004         int tErrno = errno; |  | 
|  2005         /* unlock failed with an error */ |  | 
|  2006         lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);  |  | 
|  2007         if( IS_LOCK_ERROR(lrc) ){ |  | 
|  2008           pFile->lastErrno = tErrno; |  | 
|  2009           rc = lrc; |  | 
|  2010         } |  | 
|  2011       } |  | 
|  2012     } else { |  | 
|  2013       int tErrno = errno; |  | 
|  2014       reserved = 1; |  | 
|  2015       /* someone else might have it reserved */ |  | 
|  2016       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);  |  | 
|  2017       if( IS_LOCK_ERROR(lrc) ){ |  | 
|  2018         pFile->lastErrno = tErrno; |  | 
|  2019         rc = lrc; |  | 
|  2020       } |  | 
|  2021     } |  | 
|  2022   } |  | 
|  2023   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |  | 
|  2024  |  | 
|  2025 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |  | 
|  2026   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |  | 
|  2027     rc = SQLITE_OK; |  | 
|  2028     reserved=1; |  | 
|  2029   } |  | 
|  2030 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |  | 
|  2031   *pResOut = reserved; |  | 
|  2032   return rc; |  | 
|  2033 } |  | 
|  2034  |  | 
|  2035 /* |  | 
|  2036 ** Lock the file with the lock specified by parameter locktype - one |  | 
|  2037 ** of the following: |  | 
|  2038 ** |  | 
|  2039 **     (1) SHARED_LOCK |  | 
|  2040 **     (2) RESERVED_LOCK |  | 
|  2041 **     (3) PENDING_LOCK |  | 
|  2042 **     (4) EXCLUSIVE_LOCK |  | 
|  2043 ** |  | 
|  2044 ** Sometimes when requesting one lock state, additional lock states |  | 
|  2045 ** are inserted in between.  The locking might fail on one of the later |  | 
|  2046 ** transitions leaving the lock state different from what it started but |  | 
|  2047 ** still short of its goal.  The following chart shows the allowed |  | 
|  2048 ** transitions and the inserted intermediate states: |  | 
|  2049 ** |  | 
|  2050 **    UNLOCKED -> SHARED |  | 
|  2051 **    SHARED -> RESERVED |  | 
|  2052 **    SHARED -> (PENDING) -> EXCLUSIVE |  | 
|  2053 **    RESERVED -> (PENDING) -> EXCLUSIVE |  | 
|  2054 **    PENDING -> EXCLUSIVE |  | 
|  2055 ** |  | 
|  2056 ** flock() only really support EXCLUSIVE locks.  We track intermediate |  | 
|  2057 ** lock states in the sqlite3_file structure, but all locks SHARED or |  | 
|  2058 ** above are really EXCLUSIVE locks and exclude all other processes from |  | 
|  2059 ** access the file. |  | 
|  2060 ** |  | 
|  2061 ** This routine will only increase a lock.  Use the sqlite3OsUnlock() |  | 
|  2062 ** routine to lower a locking level. |  | 
|  2063 */ |  | 
|  2064 static int flockLock(sqlite3_file *id, int locktype) { |  | 
|  2065   int rc = SQLITE_OK; |  | 
|  2066   unixFile *pFile = (unixFile*)id; |  | 
|  2067  |  | 
|  2068   assert( pFile ); |  | 
|  2069  |  | 
|  2070   /* if we already have a lock, it is exclusive.   |  | 
|  2071   ** Just adjust level and punt on outta here. */ |  | 
|  2072   if (pFile->locktype > NO_LOCK) { |  | 
|  2073     pFile->locktype = locktype; |  | 
|  2074     return SQLITE_OK; |  | 
|  2075   } |  | 
|  2076    |  | 
|  2077   /* grab an exclusive lock */ |  | 
|  2078    |  | 
|  2079   if (flock(pFile->h, LOCK_EX | LOCK_NB)) { |  | 
|  2080     int tErrno = errno; |  | 
|  2081     /* didn't get, must be busy */ |  | 
|  2082     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |  | 
|  2083     if( IS_LOCK_ERROR(rc) ){ |  | 
|  2084       pFile->lastErrno = tErrno; |  | 
|  2085     } |  | 
|  2086   } else { |  | 
|  2087     /* got it, set the type and return ok */ |  | 
|  2088     pFile->locktype = locktype; |  | 
|  2089   } |  | 
|  2090   OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype),  |  | 
|  2091            rc==SQLITE_OK ? "ok" : "failed"); |  | 
|  2092 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |  | 
|  2093   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |  | 
|  2094     rc = SQLITE_BUSY; |  | 
|  2095   } |  | 
|  2096 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |  | 
|  2097   return rc; |  | 
|  2098 } |  | 
|  2099  |  | 
|  2100  |  | 
|  2101 /* |  | 
|  2102 ** Lower the locking level on file descriptor pFile to locktype.  locktype |  | 
|  2103 ** must be either NO_LOCK or SHARED_LOCK. |  | 
|  2104 ** |  | 
|  2105 ** If the locking level of the file descriptor is already at or below |  | 
|  2106 ** the requested locking level, this routine is a no-op. |  | 
|  2107 */ |  | 
|  2108 static int flockUnlock(sqlite3_file *id, int locktype) { |  | 
|  2109   unixFile *pFile = (unixFile*)id; |  | 
|  2110    |  | 
|  2111   assert( pFile ); |  | 
|  2112   OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype, |  | 
|  2113            pFile->locktype, getpid()); |  | 
|  2114   assert( locktype<=SHARED_LOCK ); |  | 
|  2115    |  | 
|  2116   /* no-op if possible */ |  | 
|  2117   if( pFile->locktype==locktype ){ |  | 
|  2118     return SQLITE_OK; |  | 
|  2119   } |  | 
|  2120    |  | 
|  2121   /* shared can just be set because we always have an exclusive */ |  | 
|  2122   if (locktype==SHARED_LOCK) { |  | 
|  2123     pFile->locktype = locktype; |  | 
|  2124     return SQLITE_OK; |  | 
|  2125   } |  | 
|  2126    |  | 
|  2127   /* no, really, unlock. */ |  | 
|  2128   int rc = flock(pFile->h, LOCK_UN); |  | 
|  2129   if (rc) { |  | 
|  2130     int r, tErrno = errno; |  | 
|  2131     r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |  | 
|  2132     if( IS_LOCK_ERROR(r) ){ |  | 
|  2133       pFile->lastErrno = tErrno; |  | 
|  2134     } |  | 
|  2135 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |  | 
|  2136     if( (r & SQLITE_IOERR) == SQLITE_IOERR ){ |  | 
|  2137       r = SQLITE_BUSY; |  | 
|  2138     } |  | 
|  2139 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |  | 
|  2140      |  | 
|  2141     return r; |  | 
|  2142   } else { |  | 
|  2143     pFile->locktype = NO_LOCK; |  | 
|  2144     return SQLITE_OK; |  | 
|  2145   } |  | 
|  2146 } |  | 
|  2147  |  | 
|  2148 /* |  | 
|  2149 ** Close a file. |  | 
|  2150 */ |  | 
|  2151 static int flockClose(sqlite3_file *id) { |  | 
|  2152   if( id ){ |  | 
|  2153     flockUnlock(id, NO_LOCK); |  | 
|  2154   } |  | 
|  2155   return closeUnixFile(id); |  | 
|  2156 } |  | 
|  2157  |  | 
|  2158 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |  | 
|  2159  |  | 
|  2160 /******************* End of the flock lock implementation ********************* |  | 
|  2161 ******************************************************************************/ |  | 
|  2162  |  | 
|  2163 /****************************************************************************** |  | 
|  2164 ************************ Begin Named Semaphore Locking ************************ |  | 
|  2165 ** |  | 
|  2166 ** Named semaphore locking is only supported on VxWorks. |  | 
|  2167 ** |  | 
|  2168 ** Semaphore locking is like dot-lock and flock in that it really only |  | 
|  2169 ** supports EXCLUSIVE locking.  Only a single process can read or write |  | 
|  2170 ** the database file at a time.  This reduces potential concurrency, but |  | 
|  2171 ** makes the lock implementation much easier. |  | 
|  2172 */ |  | 
|  2173 #if OS_VXWORKS |  | 
|  2174  |  | 
|  2175 /* |  | 
|  2176 ** This routine checks if there is a RESERVED lock held on the specified |  | 
|  2177 ** file by this or any other process. If such a lock is held, set *pResOut |  | 
|  2178 ** to a non-zero value otherwise *pResOut is set to zero.  The return value |  | 
|  2179 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |  | 
|  2180 */ |  | 
|  2181 static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |  | 
|  2182   int rc = SQLITE_OK; |  | 
|  2183   int reserved = 0; |  | 
|  2184   unixFile *pFile = (unixFile*)id; |  | 
|  2185  |  | 
|  2186   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |  | 
|  2187    |  | 
|  2188   assert( pFile ); |  | 
|  2189  |  | 
|  2190   /* Check if a thread in this process holds such a lock */ |  | 
|  2191   if( pFile->locktype>SHARED_LOCK ){ |  | 
|  2192     reserved = 1; |  | 
|  2193   } |  | 
|  2194    |  | 
|  2195   /* Otherwise see if some other process holds it. */ |  | 
|  2196   if( !reserved ){ |  | 
|  2197     sem_t *pSem = pFile->pOpen->pSem; |  | 
|  2198     struct stat statBuf; |  | 
|  2199  |  | 
|  2200     if( sem_trywait(pSem)==-1 ){ |  | 
|  2201       int tErrno = errno; |  | 
|  2202       if( EAGAIN != tErrno ){ |  | 
|  2203         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |  | 
|  2204         pFile->lastErrno = tErrno; |  | 
|  2205       } else { |  | 
|  2206         /* someone else has the lock when we are in NO_LOCK */ |  | 
|  2207         reserved = (pFile->locktype < SHARED_LOCK); |  | 
|  2208       } |  | 
|  2209     }else{ |  | 
|  2210       /* we could have it if we want it */ |  | 
|  2211       sem_post(pSem); |  | 
|  2212     } |  | 
|  2213   } |  | 
|  2214   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |  | 
|  2215  |  | 
|  2216   *pResOut = reserved; |  | 
|  2217   return rc; |  | 
|  2218 } |  | 
|  2219  |  | 
|  2220 /* |  | 
|  2221 ** Lock the file with the lock specified by parameter locktype - one |  | 
|  2222 ** of the following: |  | 
|  2223 ** |  | 
|  2224 **     (1) SHARED_LOCK |  | 
|  2225 **     (2) RESERVED_LOCK |  | 
|  2226 **     (3) PENDING_LOCK |  | 
|  2227 **     (4) EXCLUSIVE_LOCK |  | 
|  2228 ** |  | 
|  2229 ** Sometimes when requesting one lock state, additional lock states |  | 
|  2230 ** are inserted in between.  The locking might fail on one of the later |  | 
|  2231 ** transitions leaving the lock state different from what it started but |  | 
|  2232 ** still short of its goal.  The following chart shows the allowed |  | 
|  2233 ** transitions and the inserted intermediate states: |  | 
|  2234 ** |  | 
|  2235 **    UNLOCKED -> SHARED |  | 
|  2236 **    SHARED -> RESERVED |  | 
|  2237 **    SHARED -> (PENDING) -> EXCLUSIVE |  | 
|  2238 **    RESERVED -> (PENDING) -> EXCLUSIVE |  | 
|  2239 **    PENDING -> EXCLUSIVE |  | 
|  2240 ** |  | 
|  2241 ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate |  | 
|  2242 ** lock states in the sqlite3_file structure, but all locks SHARED or |  | 
|  2243 ** above are really EXCLUSIVE locks and exclude all other processes from |  | 
|  2244 ** access the file. |  | 
|  2245 ** |  | 
|  2246 ** This routine will only increase a lock.  Use the sqlite3OsUnlock() |  | 
|  2247 ** routine to lower a locking level. |  | 
|  2248 */ |  | 
|  2249 static int semLock(sqlite3_file *id, int locktype) { |  | 
|  2250   unixFile *pFile = (unixFile*)id; |  | 
|  2251   int fd; |  | 
|  2252   sem_t *pSem = pFile->pOpen->pSem; |  | 
|  2253   int rc = SQLITE_OK; |  | 
|  2254  |  | 
|  2255   /* if we already have a lock, it is exclusive.   |  | 
|  2256   ** Just adjust level and punt on outta here. */ |  | 
|  2257   if (pFile->locktype > NO_LOCK) { |  | 
|  2258     pFile->locktype = locktype; |  | 
|  2259     rc = SQLITE_OK; |  | 
|  2260     goto sem_end_lock; |  | 
|  2261   } |  | 
|  2262    |  | 
|  2263   /* lock semaphore now but bail out when already locked. */ |  | 
|  2264   if( sem_trywait(pSem)==-1 ){ |  | 
|  2265     rc = SQLITE_BUSY; |  | 
|  2266     goto sem_end_lock; |  | 
|  2267   } |  | 
|  2268  |  | 
|  2269   /* got it, set the type and return ok */ |  | 
|  2270   pFile->locktype = locktype; |  | 
|  2271  |  | 
|  2272  sem_end_lock: |  | 
|  2273   return rc; |  | 
|  2274 } |  | 
|  2275  |  | 
|  2276 /* |  | 
|  2277 ** Lower the locking level on file descriptor pFile to locktype.  locktype |  | 
|  2278 ** must be either NO_LOCK or SHARED_LOCK. |  | 
|  2279 ** |  | 
|  2280 ** If the locking level of the file descriptor is already at or below |  | 
|  2281 ** the requested locking level, this routine is a no-op. |  | 
|  2282 */ |  | 
|  2283 static int semUnlock(sqlite3_file *id, int locktype) { |  | 
|  2284   unixFile *pFile = (unixFile*)id; |  | 
|  2285   sem_t *pSem = pFile->pOpen->pSem; |  | 
|  2286  |  | 
|  2287   assert( pFile ); |  | 
|  2288   assert( pSem ); |  | 
|  2289   OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype, |  | 
|  2290            pFile->locktype, getpid()); |  | 
|  2291   assert( locktype<=SHARED_LOCK ); |  | 
|  2292    |  | 
|  2293   /* no-op if possible */ |  | 
|  2294   if( pFile->locktype==locktype ){ |  | 
|  2295     return SQLITE_OK; |  | 
|  2296   } |  | 
|  2297    |  | 
|  2298   /* shared can just be set because we always have an exclusive */ |  | 
|  2299   if (locktype==SHARED_LOCK) { |  | 
|  2300     pFile->locktype = locktype; |  | 
|  2301     return SQLITE_OK; |  | 
|  2302   } |  | 
|  2303    |  | 
|  2304   /* no, really unlock. */ |  | 
|  2305   if ( sem_post(pSem)==-1 ) { |  | 
|  2306     int rc, tErrno = errno; |  | 
|  2307     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |  | 
|  2308     if( IS_LOCK_ERROR(rc) ){ |  | 
|  2309       pFile->lastErrno = tErrno; |  | 
|  2310     } |  | 
|  2311     return rc;  |  | 
|  2312   } |  | 
|  2313   pFile->locktype = NO_LOCK; |  | 
|  2314   return SQLITE_OK; |  | 
|  2315 } |  | 
|  2316  |  | 
|  2317 /* |  | 
|  2318  ** Close a file. |  | 
|  2319  */ |  | 
|  2320 static int semClose(sqlite3_file *id) { |  | 
|  2321   if( id ){ |  | 
|  2322     unixFile *pFile = (unixFile*)id; |  | 
|  2323     semUnlock(id, NO_LOCK); |  | 
|  2324     assert( pFile ); |  | 
|  2325     unixEnterMutex(); |  | 
|  2326     releaseLockInfo(pFile->pLock); |  | 
|  2327     releaseOpenCnt(pFile->pOpen); |  | 
|  2328     unixLeaveMutex(); |  | 
|  2329     closeUnixFile(id); |  | 
|  2330   } |  | 
|  2331   return SQLITE_OK; |  | 
|  2332 } |  | 
|  2333  |  | 
|  2334 #endif /* OS_VXWORKS */ |  | 
|  2335 /* |  | 
|  2336 ** Named semaphore locking is only available on VxWorks. |  | 
|  2337 ** |  | 
|  2338 *************** End of the named semaphore lock implementation **************** |  | 
|  2339 ******************************************************************************/ |  | 
|  2340  |  | 
|  2341  |  | 
|  2342 /****************************************************************************** |  | 
|  2343 *************************** Begin AFP Locking ********************************* |  | 
|  2344 ** |  | 
|  2345 ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found |  | 
|  2346 ** on Apple Macintosh computers - both OS9 and OSX. |  | 
|  2347 ** |  | 
|  2348 ** Third-party implementations of AFP are available.  But this code here |  | 
|  2349 ** only works on OSX. |  | 
|  2350 */ |  | 
|  2351  |  | 
|  2352 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |  | 
|  2353 /* |  | 
|  2354 ** The afpLockingContext structure contains all afp lock specific state |  | 
|  2355 */ |  | 
|  2356 typedef struct afpLockingContext afpLockingContext; |  | 
|  2357 struct afpLockingContext { |  | 
|  2358   unsigned long long sharedByte; |  | 
|  2359   const char *dbPath;             /* Name of the open file */ |  | 
|  2360 }; |  | 
|  2361  |  | 
|  2362 struct ByteRangeLockPB2 |  | 
|  2363 { |  | 
|  2364   unsigned long long offset;        /* offset to first byte to lock */ |  | 
|  2365   unsigned long long length;        /* nbr of bytes to lock */ |  | 
|  2366   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |  | 
|  2367   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */ |  | 
|  2368   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */ |  | 
|  2369   int fd;                           /* file desc to assoc this lock with */ |  | 
|  2370 }; |  | 
|  2371  |  | 
|  2372 #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2) |  | 
|  2373  |  | 
|  2374 /* |  | 
|  2375 ** This is a utility for setting or clearing a bit-range lock on an |  | 
|  2376 ** AFP filesystem. |  | 
|  2377 **  |  | 
|  2378 ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |  | 
|  2379 */ |  | 
|  2380 static int afpSetLock( |  | 
|  2381   const char *path,              /* Name of the file to be locked or unlocked */ |  | 
|  2382   unixFile *pFile,               /* Open file descriptor on path */ |  | 
|  2383   unsigned long long offset,     /* First byte to be locked */ |  | 
|  2384   unsigned long long length,     /* Number of bytes to lock */ |  | 
|  2385   int setLockFlag                /* True to set lock.  False to clear lock */ |  | 
|  2386 ){ |  | 
|  2387   struct ByteRangeLockPB2 pb; |  | 
|  2388   int err; |  | 
|  2389    |  | 
|  2390   pb.unLockFlag = setLockFlag ? 0 : 1; |  | 
|  2391   pb.startEndFlag = 0; |  | 
|  2392   pb.offset = offset; |  | 
|  2393   pb.length = length;  |  | 
|  2394   pb.fd = pFile->h; |  | 
|  2395    |  | 
|  2396   OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",  |  | 
|  2397     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |  | 
|  2398     offset, length); |  | 
|  2399   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |  | 
|  2400   if ( err==-1 ) { |  | 
|  2401     int rc; |  | 
|  2402     int tErrno = errno; |  | 
|  2403     OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |  | 
|  2404              path, tErrno, strerror(tErrno)); |  | 
|  2405 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |  | 
|  2406     rc = SQLITE_BUSY; |  | 
|  2407 #else |  | 
|  2408     rc = sqliteErrorFromPosixError(tErrno, |  | 
|  2409                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |  | 
|  2410 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |  | 
|  2411     if( IS_LOCK_ERROR(rc) ){ |  | 
|  2412       pFile->lastErrno = tErrno; |  | 
|  2413     } |  | 
|  2414     return rc; |  | 
|  2415   } else { |  | 
|  2416     return SQLITE_OK; |  | 
|  2417   } |  | 
|  2418 } |  | 
|  2419  |  | 
|  2420 /* |  | 
|  2421 ** This routine checks if there is a RESERVED lock held on the specified |  | 
|  2422 ** file by this or any other process. If such a lock is held, set *pResOut |  | 
|  2423 ** to a non-zero value otherwise *pResOut is set to zero.  The return value |  | 
|  2424 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |  | 
|  2425 */ |  | 
|  2426 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |  | 
|  2427   int rc = SQLITE_OK; |  | 
|  2428   int reserved = 0; |  | 
|  2429   unixFile *pFile = (unixFile*)id; |  | 
|  2430    |  | 
|  2431   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |  | 
|  2432    |  | 
|  2433   assert( pFile ); |  | 
|  2434   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |  | 
|  2435    |  | 
|  2436   /* Check if a thread in this process holds such a lock */ |  | 
|  2437   if( pFile->locktype>SHARED_LOCK ){ |  | 
|  2438     reserved = 1; |  | 
|  2439   } |  | 
|  2440    |  | 
|  2441   /* Otherwise see if some other process holds it. |  | 
|  2442    */ |  | 
|  2443   if( !reserved ){ |  | 
|  2444     /* lock the RESERVED byte */ |  | 
|  2445     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);   |  | 
|  2446     if( SQLITE_OK==lrc ){ |  | 
|  2447       /* if we succeeded in taking the reserved lock, unlock it to restore |  | 
|  2448       ** the original state */ |  | 
|  2449       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |  | 
|  2450     } else { |  | 
|  2451       /* if we failed to get the lock then someone else must have it */ |  | 
|  2452       reserved = 1; |  | 
|  2453     } |  | 
|  2454     if( IS_LOCK_ERROR(lrc) ){ |  | 
|  2455       rc=lrc; |  | 
|  2456     } |  | 
|  2457   } |  | 
|  2458    |  | 
|  2459   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |  | 
|  2460    |  | 
|  2461   *pResOut = reserved; |  | 
|  2462   return rc; |  | 
|  2463 } |  | 
|  2464  |  | 
|  2465 /* |  | 
|  2466 ** Lock the file with the lock specified by parameter locktype - one |  | 
|  2467 ** of the following: |  | 
|  2468 ** |  | 
|  2469 **     (1) SHARED_LOCK |  | 
|  2470 **     (2) RESERVED_LOCK |  | 
|  2471 **     (3) PENDING_LOCK |  | 
|  2472 **     (4) EXCLUSIVE_LOCK |  | 
|  2473 ** |  | 
|  2474 ** Sometimes when requesting one lock state, additional lock states |  | 
|  2475 ** are inserted in between.  The locking might fail on one of the later |  | 
|  2476 ** transitions leaving the lock state different from what it started but |  | 
|  2477 ** still short of its goal.  The following chart shows the allowed |  | 
|  2478 ** transitions and the inserted intermediate states: |  | 
|  2479 ** |  | 
|  2480 **    UNLOCKED -> SHARED |  | 
|  2481 **    SHARED -> RESERVED |  | 
|  2482 **    SHARED -> (PENDING) -> EXCLUSIVE |  | 
|  2483 **    RESERVED -> (PENDING) -> EXCLUSIVE |  | 
|  2484 **    PENDING -> EXCLUSIVE |  | 
|  2485 ** |  | 
|  2486 ** This routine will only increase a lock.  Use the sqlite3OsUnlock() |  | 
|  2487 ** routine to lower a locking level. |  | 
|  2488 */ |  | 
|  2489 static int afpLock(sqlite3_file *id, int locktype){ |  | 
|  2490   int rc = SQLITE_OK; |  | 
|  2491   unixFile *pFile = (unixFile*)id; |  | 
|  2492   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |  | 
|  2493    |  | 
|  2494   assert( pFile ); |  | 
|  2495   OSTRACE5("LOCK    %d %s was %s pid=%d\n", pFile->h, |  | 
|  2496          locktypeName(locktype), locktypeName(pFile->locktype), getpid()); |  | 
|  2497  |  | 
|  2498   /* If there is already a lock of this type or more restrictive on the |  | 
|  2499   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |  | 
|  2500   ** unixEnterMutex() hasn't been called yet. |  | 
|  2501   */ |  | 
|  2502   if( pFile->locktype>=locktype ){ |  | 
|  2503     OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h, |  | 
|  2504            locktypeName(locktype)); |  | 
|  2505     return SQLITE_OK; |  | 
|  2506   } |  | 
|  2507  |  | 
|  2508   /* Make sure the locking sequence is correct |  | 
|  2509   */ |  | 
|  2510   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |  | 
|  2511   assert( locktype!=PENDING_LOCK ); |  | 
|  2512   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |  | 
|  2513    |  | 
|  2514   /* This mutex is needed because pFile->pLock is shared across threads |  | 
|  2515   */ |  | 
|  2516   unixEnterMutex(); |  | 
|  2517  |  | 
|  2518   /* Make sure the current thread owns the pFile. |  | 
|  2519   */ |  | 
|  2520   rc = transferOwnership(pFile); |  | 
|  2521   if( rc!=SQLITE_OK ){ |  | 
|  2522     unixLeaveMutex(); |  | 
|  2523     return rc; |  | 
|  2524   } |  | 
|  2525      |  | 
|  2526   /* A PENDING lock is needed before acquiring a SHARED lock and before |  | 
|  2527   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will |  | 
|  2528   ** be released. |  | 
|  2529   */ |  | 
|  2530   if( locktype==SHARED_LOCK  |  | 
|  2531       || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |  | 
|  2532   ){ |  | 
|  2533     int failed; |  | 
|  2534     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |  | 
|  2535     if (failed) { |  | 
|  2536       rc = failed; |  | 
|  2537       goto afp_end_lock; |  | 
|  2538     } |  | 
|  2539   } |  | 
|  2540    |  | 
|  2541   /* If control gets to this point, then actually go ahead and make |  | 
|  2542   ** operating system calls for the specified lock. |  | 
|  2543   */ |  | 
|  2544   if( locktype==SHARED_LOCK ){ |  | 
|  2545     int lk, lrc1, lrc2, lrc1Errno; |  | 
|  2546      |  | 
|  2547     /* Now get the read-lock SHARED_LOCK */ |  | 
|  2548     /* note that the quality of the randomness doesn't matter that much */ |  | 
|  2549     lk = random();  |  | 
|  2550     context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); |  | 
|  2551     lrc1 = afpSetLock(context->dbPath, pFile,  |  | 
|  2552           SHARED_FIRST+context->sharedByte, 1, 1); |  | 
|  2553     if( IS_LOCK_ERROR(lrc1) ){ |  | 
|  2554       lrc1Errno = pFile->lastErrno; |  | 
|  2555     } |  | 
|  2556     /* Drop the temporary PENDING lock */ |  | 
|  2557     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |  | 
|  2558      |  | 
|  2559     if( IS_LOCK_ERROR(lrc1) ) { |  | 
|  2560       pFile->lastErrno = lrc1Errno; |  | 
|  2561       rc = lrc1; |  | 
|  2562       goto afp_end_lock; |  | 
|  2563     } else if( IS_LOCK_ERROR(lrc2) ){ |  | 
|  2564       rc = lrc2; |  | 
|  2565       goto afp_end_lock; |  | 
|  2566     } else if( lrc1 != SQLITE_OK ) { |  | 
|  2567       rc = lrc1; |  | 
|  2568     } else { |  | 
|  2569       pFile->locktype = SHARED_LOCK; |  | 
|  2570       pFile->pOpen->nLock++; |  | 
|  2571     } |  | 
|  2572   }else{ |  | 
|  2573     /* The request was for a RESERVED or EXCLUSIVE lock.  It is |  | 
|  2574     ** assumed that there is a SHARED or greater lock on the file |  | 
|  2575     ** already. |  | 
|  2576     */ |  | 
|  2577     int failed = 0; |  | 
|  2578     assert( 0!=pFile->locktype ); |  | 
|  2579     if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) { |  | 
|  2580         /* Acquire a RESERVED lock */ |  | 
|  2581         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |  | 
|  2582     } |  | 
|  2583     if (!failed && locktype == EXCLUSIVE_LOCK) { |  | 
|  2584       /* Acquire an EXCLUSIVE lock */ |  | 
|  2585          |  | 
|  2586       /* Remove the shared lock before trying the range.  we'll need to  |  | 
|  2587       ** reestablish the shared lock if we can't get the  afpUnlock |  | 
|  2588       */ |  | 
|  2589       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |  | 
|  2590                          context->sharedByte, 1, 0)) ){ |  | 
|  2591         int failed2 = SQLITE_OK; |  | 
|  2592         /* now attemmpt to get the exclusive lock range */ |  | 
|  2593         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,  |  | 
|  2594                                SHARED_SIZE, 1); |  | 
|  2595         if( failed && (failed2 = afpSetLock(context->dbPath, pFile,  |  | 
|  2596                        SHARED_FIRST + context->sharedByte, 1, 1)) ){ |  | 
|  2597           /* Can't reestablish the shared lock.  Sqlite can't deal, this is |  | 
|  2598           ** a critical I/O error |  | 
|  2599           */ |  | 
|  2600           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :  |  | 
|  2601                SQLITE_IOERR_LOCK; |  | 
|  2602           goto afp_end_lock; |  | 
|  2603         }  |  | 
|  2604       }else{ |  | 
|  2605         rc = failed;  |  | 
|  2606       } |  | 
|  2607     } |  | 
|  2608     if( failed ){ |  | 
|  2609       rc = failed; |  | 
|  2610     } |  | 
|  2611   } |  | 
|  2612    |  | 
|  2613   if( rc==SQLITE_OK ){ |  | 
|  2614     pFile->locktype = locktype; |  | 
|  2615   }else if( locktype==EXCLUSIVE_LOCK ){ |  | 
|  2616     pFile->locktype = PENDING_LOCK; |  | 
|  2617   } |  | 
|  2618    |  | 
|  2619 afp_end_lock: |  | 
|  2620   unixLeaveMutex(); |  | 
|  2621   OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype),  |  | 
|  2622          rc==SQLITE_OK ? "ok" : "failed"); |  | 
|  2623   return rc; |  | 
|  2624 } |  | 
|  2625  |  | 
|  2626 /* |  | 
|  2627 ** Lower the locking level on file descriptor pFile to locktype.  locktype |  | 
|  2628 ** must be either NO_LOCK or SHARED_LOCK. |  | 
|  2629 ** |  | 
|  2630 ** If the locking level of the file descriptor is already at or below |  | 
|  2631 ** the requested locking level, this routine is a no-op. |  | 
|  2632 */ |  | 
|  2633 static int afpUnlock(sqlite3_file *id, int locktype) { |  | 
|  2634   int rc = SQLITE_OK; |  | 
|  2635   unixFile *pFile = (unixFile*)id; |  | 
|  2636   afpLockingContext *pCtx = (afpLockingContext *) pFile->lockingContext; |  | 
|  2637  |  | 
|  2638   assert( pFile ); |  | 
|  2639   OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype, |  | 
|  2640          pFile->locktype, getpid()); |  | 
|  2641  |  | 
|  2642   assert( locktype<=SHARED_LOCK ); |  | 
|  2643   if( pFile->locktype<=locktype ){ |  | 
|  2644     return SQLITE_OK; |  | 
|  2645   } |  | 
|  2646   if( CHECK_THREADID(pFile) ){ |  | 
|  2647     return SQLITE_MISUSE; |  | 
|  2648   } |  | 
|  2649   unixEnterMutex(); |  | 
|  2650   if( pFile->locktype>SHARED_LOCK ){ |  | 
|  2651      |  | 
|  2652     if( pFile->locktype==EXCLUSIVE_LOCK ){ |  | 
|  2653       rc = afpSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |  | 
|  2654       if( rc==SQLITE_OK && locktype==SHARED_LOCK ){ |  | 
|  2655         /* only re-establish the shared lock if necessary */ |  | 
|  2656         int sharedLockByte = SHARED_FIRST+pCtx->sharedByte; |  | 
|  2657         rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1); |  | 
|  2658       } |  | 
|  2659     } |  | 
|  2660     if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){ |  | 
|  2661       rc = afpSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0); |  | 
|  2662     }  |  | 
|  2663     if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){ |  | 
|  2664       rc = afpSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0); |  | 
|  2665     } |  | 
|  2666   }else if( locktype==NO_LOCK ){ |  | 
|  2667     /* clear the shared lock */ |  | 
|  2668     int sharedLockByte = SHARED_FIRST+pCtx->sharedByte; |  | 
|  2669     rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0); |  | 
|  2670   } |  | 
|  2671  |  | 
|  2672   if( rc==SQLITE_OK ){ |  | 
|  2673     if( locktype==NO_LOCK ){ |  | 
|  2674       struct unixOpenCnt *pOpen = pFile->pOpen; |  | 
|  2675       pOpen->nLock--; |  | 
|  2676       assert( pOpen->nLock>=0 ); |  | 
|  2677       if( pOpen->nLock==0 ){ |  | 
|  2678         rc = closePendingFds(pFile); |  | 
|  2679       } |  | 
|  2680     } |  | 
|  2681   } |  | 
|  2682   unixLeaveMutex(); |  | 
|  2683   if( rc==SQLITE_OK ){ |  | 
|  2684     pFile->locktype = locktype; |  | 
|  2685   } |  | 
|  2686   return rc; |  | 
|  2687 } |  | 
|  2688  |  | 
|  2689 /* |  | 
|  2690 ** Close a file & cleanup AFP specific locking context  |  | 
|  2691 */ |  | 
|  2692 static int afpClose(sqlite3_file *id) { |  | 
|  2693   if( id ){ |  | 
|  2694     unixFile *pFile = (unixFile*)id; |  | 
|  2695     afpUnlock(id, NO_LOCK); |  | 
|  2696     unixEnterMutex(); |  | 
|  2697     if( pFile->pOpen && pFile->pOpen->nLock ){ |  | 
|  2698       /* If there are outstanding locks, do not actually close the file just |  | 
|  2699       ** yet because that would clear those locks.  Instead, add the file |  | 
|  2700       ** descriptor to pOpen->aPending.  It will be automatically closed when |  | 
|  2701       ** the last lock is cleared. |  | 
|  2702       */ |  | 
|  2703       setPendingFd(pFile); |  | 
|  2704     } |  | 
|  2705     releaseOpenCnt(pFile->pOpen); |  | 
|  2706     sqlite3_free(pFile->lockingContext); |  | 
|  2707     closeUnixFile(id); |  | 
|  2708     unixLeaveMutex(); |  | 
|  2709   } |  | 
|  2710   return SQLITE_OK; |  | 
|  2711 } |  | 
|  2712  |  | 
|  2713 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |  | 
|  2714 /* |  | 
|  2715 ** The code above is the AFP lock implementation.  The code is specific |  | 
|  2716 ** to MacOSX and does not work on other unix platforms.  No alternative |  | 
|  2717 ** is available.  If you don't compile for a mac, then the "unix-afp" |  | 
|  2718 ** VFS is not available. |  | 
|  2719 ** |  | 
|  2720 ********************* End of the AFP lock implementation ********************** |  | 
|  2721 ******************************************************************************/ |  | 
|  2722  |  | 
|  2723  |  | 
|  2724 /****************************************************************************** |  | 
|  2725 **************** Non-locking sqlite3_file methods ***************************** |  | 
|  2726 ** |  | 
|  2727 ** The next division contains implementations for all methods of the  |  | 
|  2728 ** sqlite3_file object other than the locking methods.  The locking |  | 
|  2729 ** methods were defined in divisions above (one locking method per |  | 
|  2730 ** division).  Those methods that are common to all locking modes |  | 
|  2731 ** are gather together into this division. |  | 
|  2732 */ |  | 
|  2733  |  | 
|  2734 /* |  | 
|  2735 ** Seek to the offset passed as the second argument, then read cnt  |  | 
|  2736 ** bytes into pBuf. Return the number of bytes actually read. |  | 
|  2737 ** |  | 
|  2738 ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also |  | 
|  2739 ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from |  | 
|  2740 ** one system to another.  Since SQLite does not define USE_PREAD |  | 
|  2741 ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |  | 
|  2742 ** See tickets #2741 and #2681. |  | 
|  2743 ** |  | 
|  2744 ** To avoid stomping the errno value on a failed read the lastErrno value |  | 
|  2745 ** is set before returning. |  | 
|  2746 */ |  | 
|  2747 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |  | 
|  2748   int got; |  | 
|  2749   i64 newOffset; |  | 
|  2750   TIMER_START; |  | 
|  2751 #if defined(USE_PREAD) |  | 
|  2752   got = pread(id->h, pBuf, cnt, offset); |  | 
|  2753   SimulateIOError( got = -1 ); |  | 
|  2754 #elif defined(USE_PREAD64) |  | 
|  2755   got = pread64(id->h, pBuf, cnt, offset); |  | 
|  2756   SimulateIOError( got = -1 ); |  | 
|  2757 #else |  | 
|  2758   newOffset = lseek(id->h, offset, SEEK_SET); |  | 
|  2759   SimulateIOError( newOffset-- ); |  | 
|  2760   if( newOffset!=offset ){ |  | 
|  2761     if( newOffset == -1 ){ |  | 
|  2762       ((unixFile*)id)->lastErrno = errno; |  | 
|  2763     }else{ |  | 
|  2764       ((unixFile*)id)->lastErrno = 0;                    |  | 
|  2765     } |  | 
|  2766     return -1; |  | 
|  2767   } |  | 
|  2768   got = read(id->h, pBuf, cnt); |  | 
|  2769 #endif |  | 
|  2770   TIMER_END; |  | 
|  2771   if( got<0 ){ |  | 
|  2772     ((unixFile*)id)->lastErrno = errno; |  | 
|  2773   } |  | 
|  2774   OSTRACE5("READ    %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); |  | 
|  2775   return got; |  | 
|  2776 } |  | 
|  2777  |  | 
|  2778 /* |  | 
|  2779 ** Read data from a file into a buffer.  Return SQLITE_OK if all |  | 
|  2780 ** bytes were read successfully and SQLITE_IOERR if anything goes |  | 
|  2781 ** wrong. |  | 
|  2782 */ |  | 
|  2783 static int unixRead( |  | 
|  2784   sqlite3_file *id,  |  | 
|  2785   void *pBuf,  |  | 
|  2786   int amt, |  | 
|  2787   sqlite3_int64 offset |  | 
|  2788 ){ |  | 
|  2789   unixFile *pFile = (unixFile *)id; |  | 
|  2790   int got; |  | 
|  2791   assert( id ); |  | 
|  2792  |  | 
|  2793   /* If this is a database file (not a journal, master-journal or temp |  | 
|  2794   ** file), the bytes in the locking range should never be read or written. */ |  | 
|  2795   assert( pFile->pUnused==0 |  | 
|  2796        || offset>=PENDING_BYTE+512 |  | 
|  2797        || offset+amt<=PENDING_BYTE  |  | 
|  2798   ); |  | 
|  2799  |  | 
|  2800   got = seekAndRead(pFile, offset, pBuf, amt); |  | 
|  2801   if( got==amt ){ |  | 
|  2802     return SQLITE_OK; |  | 
|  2803   }else if( got<0 ){ |  | 
|  2804     /* lastErrno set by seekAndRead */ |  | 
|  2805     return SQLITE_IOERR_READ; |  | 
|  2806   }else{ |  | 
|  2807     pFile->lastErrno = 0; /* not a system error */ |  | 
|  2808     /* Unread parts of the buffer must be zero-filled */ |  | 
|  2809     memset(&((char*)pBuf)[got], 0, amt-got); |  | 
|  2810     return SQLITE_IOERR_SHORT_READ; |  | 
|  2811   } |  | 
|  2812 } |  | 
|  2813  |  | 
|  2814 /* |  | 
|  2815 ** Seek to the offset in id->offset then read cnt bytes into pBuf. |  | 
|  2816 ** Return the number of bytes actually read.  Update the offset. |  | 
|  2817 ** |  | 
|  2818 ** To avoid stomping the errno value on a failed write the lastErrno value |  | 
|  2819 ** is set before returning. |  | 
|  2820 */ |  | 
|  2821 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |  | 
|  2822   int got; |  | 
|  2823   i64 newOffset; |  | 
|  2824   TIMER_START; |  | 
|  2825 #if defined(USE_PREAD) |  | 
|  2826   got = pwrite(id->h, pBuf, cnt, offset); |  | 
|  2827 #elif defined(USE_PREAD64) |  | 
|  2828   got = pwrite64(id->h, pBuf, cnt, offset); |  | 
|  2829 #else |  | 
|  2830   newOffset = lseek(id->h, offset, SEEK_SET); |  | 
|  2831   if( newOffset!=offset ){ |  | 
|  2832     if( newOffset == -1 ){ |  | 
|  2833       ((unixFile*)id)->lastErrno = errno; |  | 
|  2834     }else{ |  | 
|  2835       ((unixFile*)id)->lastErrno = 0;                    |  | 
|  2836     } |  | 
|  2837     return -1; |  | 
|  2838   } |  | 
|  2839   got = write(id->h, pBuf, cnt); |  | 
|  2840 #endif |  | 
|  2841   TIMER_END; |  | 
|  2842   if( got<0 ){ |  | 
|  2843     ((unixFile*)id)->lastErrno = errno; |  | 
|  2844   } |  | 
|  2845  |  | 
|  2846   OSTRACE5("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); |  | 
|  2847   return got; |  | 
|  2848 } |  | 
|  2849  |  | 
|  2850  |  | 
|  2851 /* |  | 
|  2852 ** Write data from a buffer into a file.  Return SQLITE_OK on success |  | 
|  2853 ** or some other error code on failure. |  | 
|  2854 */ |  | 
|  2855 static int unixWrite( |  | 
|  2856   sqlite3_file *id,  |  | 
|  2857   const void *pBuf,  |  | 
|  2858   int amt, |  | 
|  2859   sqlite3_int64 offset  |  | 
|  2860 ){ |  | 
|  2861   unixFile *pFile = (unixFile*)id; |  | 
|  2862   int wrote = 0; |  | 
|  2863   assert( id ); |  | 
|  2864   assert( amt>0 ); |  | 
|  2865  |  | 
|  2866   /* If this is a database file (not a journal, master-journal or temp |  | 
|  2867   ** file), the bytes in the locking range should never be read or written. */ |  | 
|  2868   assert( pFile->pUnused==0 |  | 
|  2869        || offset>=PENDING_BYTE+512 |  | 
|  2870        || offset+amt<=PENDING_BYTE  |  | 
|  2871   ); |  | 
|  2872  |  | 
|  2873 #ifndef NDEBUG |  | 
|  2874   /* If we are doing a normal write to a database file (as opposed to |  | 
|  2875   ** doing a hot-journal rollback or a write to some file other than a |  | 
|  2876   ** normal database file) then record the fact that the database |  | 
|  2877   ** has changed.  If the transaction counter is modified, record that |  | 
|  2878   ** fact too. |  | 
|  2879   */ |  | 
|  2880   if( pFile->inNormalWrite ){ |  | 
|  2881     pFile->dbUpdate = 1;  /* The database has been modified */ |  | 
|  2882     if( offset<=24 && offset+amt>=27 ){ |  | 
|  2883       int rc; |  | 
|  2884       char oldCntr[4]; |  | 
|  2885       SimulateIOErrorBenign(1); |  | 
|  2886       rc = seekAndRead(pFile, 24, oldCntr, 4); |  | 
|  2887       SimulateIOErrorBenign(0); |  | 
|  2888       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |  | 
|  2889         pFile->transCntrChng = 1;  /* The transaction counter has changed */ |  | 
|  2890       } |  | 
|  2891     } |  | 
|  2892   } |  | 
|  2893 #endif |  | 
|  2894  |  | 
|  2895   while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |  | 
|  2896     amt -= wrote; |  | 
|  2897     offset += wrote; |  | 
|  2898     pBuf = &((char*)pBuf)[wrote]; |  | 
|  2899   } |  | 
|  2900   SimulateIOError(( wrote=(-1), amt=1 )); |  | 
|  2901   SimulateDiskfullError(( wrote=0, amt=1 )); |  | 
|  2902   if( amt>0 ){ |  | 
|  2903     if( wrote<0 ){ |  | 
|  2904       /* lastErrno set by seekAndWrite */ |  | 
|  2905       return SQLITE_IOERR_WRITE; |  | 
|  2906     }else{ |  | 
|  2907       pFile->lastErrno = 0; /* not a system error */ |  | 
|  2908       return SQLITE_FULL; |  | 
|  2909     } |  | 
|  2910   } |  | 
|  2911   return SQLITE_OK; |  | 
|  2912 } |  | 
|  2913  |  | 
|  2914 #ifdef SQLITE_TEST |  | 
|  2915 /* |  | 
|  2916 ** Count the number of fullsyncs and normal syncs.  This is used to test |  | 
|  2917 ** that syncs and fullsyncs are occurring at the right times. |  | 
|  2918 */ |  | 
|  2919 int sqlite3_sync_count = 0; |  | 
|  2920 int sqlite3_fullsync_count = 0; |  | 
|  2921 #endif |  | 
|  2922  |  | 
|  2923 /* |  | 
|  2924 ** We do not trust systems to provide a working fdatasync().  Some do. |  | 
|  2925 ** Others do no.  To be safe, we will stick with the (slower) fsync(). |  | 
|  2926 ** If you know that your system does support fdatasync() correctly, |  | 
|  2927 ** then simply compile with -Dfdatasync=fdatasync |  | 
|  2928 */ |  | 
|  2929 #if !defined(fdatasync) && !defined(__linux__) |  | 
|  2930 # define fdatasync fsync |  | 
|  2931 #endif |  | 
|  2932  |  | 
|  2933 /* |  | 
|  2934 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |  | 
|  2935 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently |  | 
|  2936 ** only available on Mac OS X.  But that could change. |  | 
|  2937 */ |  | 
|  2938 #ifdef F_FULLFSYNC |  | 
|  2939 # define HAVE_FULLFSYNC 1 |  | 
|  2940 #else |  | 
|  2941 # define HAVE_FULLFSYNC 0 |  | 
|  2942 #endif |  | 
|  2943  |  | 
|  2944  |  | 
|  2945 /* |  | 
|  2946 ** The fsync() system call does not work as advertised on many |  | 
|  2947 ** unix systems.  The following procedure is an attempt to make |  | 
|  2948 ** it work better. |  | 
|  2949 ** |  | 
|  2950 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful |  | 
|  2951 ** for testing when we want to run through the test suite quickly. |  | 
|  2952 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |  | 
|  2953 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |  | 
|  2954 ** or power failure will likely corrupt the database file. |  | 
|  2955 ** |  | 
|  2956 ** SQLite sets the dataOnly flag if the size of the file is unchanged. |  | 
|  2957 ** The idea behind dataOnly is that it should only write the file content |  | 
|  2958 ** to disk, not the inode.  We only set dataOnly if the file size is  |  | 
|  2959 ** unchanged since the file size is part of the inode.  However,  |  | 
|  2960 ** Ted Ts'o tells us that fdatasync() will also write the inode if the |  | 
|  2961 ** file size has changed.  The only real difference between fdatasync() |  | 
|  2962 ** and fsync(), Ted tells us, is that fdatasync() will not flush the |  | 
|  2963 ** inode if the mtime or owner or other inode attributes have changed. |  | 
|  2964 ** We only care about the file size, not the other file attributes, so |  | 
|  2965 ** as far as SQLite is concerned, an fdatasync() is always adequate. |  | 
|  2966 ** So, we always use fdatasync() if it is available, regardless of |  | 
|  2967 ** the value of the dataOnly flag. |  | 
|  2968 */ |  | 
|  2969 static int full_fsync(int fd, int fullSync, int dataOnly){ |  | 
|  2970   int rc; |  | 
|  2971  |  | 
|  2972   /* The following "ifdef/elif/else/" block has the same structure as |  | 
|  2973   ** the one below. It is replicated here solely to avoid cluttering  |  | 
|  2974   ** up the real code with the UNUSED_PARAMETER() macros. |  | 
|  2975   */ |  | 
|  2976 #ifdef SQLITE_NO_SYNC |  | 
|  2977   UNUSED_PARAMETER(fd); |  | 
|  2978   UNUSED_PARAMETER(fullSync); |  | 
|  2979   UNUSED_PARAMETER(dataOnly); |  | 
|  2980 #elif HAVE_FULLFSYNC |  | 
|  2981   UNUSED_PARAMETER(dataOnly); |  | 
|  2982 #else |  | 
|  2983   UNUSED_PARAMETER(fullSync); |  | 
|  2984   UNUSED_PARAMETER(dataOnly); |  | 
|  2985 #endif |  | 
|  2986  |  | 
|  2987   /* Record the number of times that we do a normal fsync() and  |  | 
|  2988   ** FULLSYNC.  This is used during testing to verify that this procedure |  | 
|  2989   ** gets called with the correct arguments. |  | 
|  2990   */ |  | 
|  2991 #ifdef SQLITE_TEST |  | 
|  2992   if( fullSync ) sqlite3_fullsync_count++; |  | 
|  2993   sqlite3_sync_count++; |  | 
|  2994 #endif |  | 
|  2995  |  | 
|  2996   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |  | 
|  2997   ** no-op |  | 
|  2998   */ |  | 
|  2999 #ifdef SQLITE_NO_SYNC |  | 
|  3000   rc = SQLITE_OK; |  | 
|  3001 #elif HAVE_FULLFSYNC |  | 
|  3002   if( fullSync ){ |  | 
|  3003     rc = fcntl(fd, F_FULLFSYNC, 0); |  | 
|  3004   }else{ |  | 
|  3005     rc = 1; |  | 
|  3006   } |  | 
|  3007   /* If the FULLFSYNC failed, fall back to attempting an fsync(). |  | 
|  3008   ** It shouldn't be possible for fullfsync to fail on the local  |  | 
|  3009   ** file system (on OSX), so failure indicates that FULLFSYNC |  | 
|  3010   ** isn't supported for this file system. So, attempt an fsync  |  | 
|  3011   ** and (for now) ignore the overhead of a superfluous fcntl call.   |  | 
|  3012   ** It'd be better to detect fullfsync support once and avoid  |  | 
|  3013   ** the fcntl call every time sync is called. |  | 
|  3014   */ |  | 
|  3015   if( rc ) rc = fsync(fd); |  | 
|  3016  |  | 
|  3017 #else  |  | 
|  3018   rc = fdatasync(fd); |  | 
|  3019 #if OS_VXWORKS |  | 
|  3020   if( rc==-1 && errno==ENOTSUP ){ |  | 
|  3021     rc = fsync(fd); |  | 
|  3022   } |  | 
|  3023 #endif /* OS_VXWORKS */ |  | 
|  3024 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |  | 
|  3025  |  | 
|  3026   if( OS_VXWORKS && rc!= -1 ){ |  | 
|  3027     rc = 0; |  | 
|  3028   } |  | 
|  3029   return rc; |  | 
|  3030 } |  | 
|  3031  |  | 
|  3032 /* |  | 
|  3033 ** Make sure all writes to a particular file are committed to disk. |  | 
|  3034 ** |  | 
|  3035 ** If dataOnly==0 then both the file itself and its metadata (file |  | 
|  3036 ** size, access time, etc) are synced.  If dataOnly!=0 then only the |  | 
|  3037 ** file data is synced. |  | 
|  3038 ** |  | 
|  3039 ** Under Unix, also make sure that the directory entry for the file |  | 
|  3040 ** has been created by fsync-ing the directory that contains the file. |  | 
|  3041 ** If we do not do this and we encounter a power failure, the directory |  | 
|  3042 ** entry for the journal might not exist after we reboot.  The next |  | 
|  3043 ** SQLite to access the file will not know that the journal exists (because |  | 
|  3044 ** the directory entry for the journal was never created) and the transaction |  | 
|  3045 ** will not roll back - possibly leading to database corruption. |  | 
|  3046 */ |  | 
|  3047 static int unixSync(sqlite3_file *id, int flags){ |  | 
|  3048   int rc; |  | 
|  3049   unixFile *pFile = (unixFile*)id; |  | 
|  3050  |  | 
|  3051   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |  | 
|  3052   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |  | 
|  3053  |  | 
|  3054   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |  | 
|  3055   assert((flags&0x0F)==SQLITE_SYNC_NORMAL |  | 
|  3056       || (flags&0x0F)==SQLITE_SYNC_FULL |  | 
|  3057   ); |  | 
|  3058  |  | 
|  3059   /* Unix cannot, but some systems may return SQLITE_FULL from here. This |  | 
|  3060   ** line is to test that doing so does not cause any problems. |  | 
|  3061   */ |  | 
|  3062   SimulateDiskfullError( return SQLITE_FULL ); |  | 
|  3063  |  | 
|  3064   assert( pFile ); |  | 
|  3065   OSTRACE2("SYNC    %-3d\n", pFile->h); |  | 
|  3066   rc = full_fsync(pFile->h, isFullsync, isDataOnly); |  | 
|  3067   SimulateIOError( rc=1 ); |  | 
|  3068   if( rc ){ |  | 
|  3069     pFile->lastErrno = errno; |  | 
|  3070     return SQLITE_IOERR_FSYNC; |  | 
|  3071   } |  | 
|  3072   if( pFile->dirfd>=0 ){ |  | 
|  3073     int err; |  | 
|  3074     OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |  | 
|  3075             HAVE_FULLFSYNC, isFullsync); |  | 
|  3076 #ifndef SQLITE_DISABLE_DIRSYNC |  | 
|  3077     /* The directory sync is only attempted if full_fsync is |  | 
|  3078     ** turned off or unavailable.  If a full_fsync occurred above, |  | 
|  3079     ** then the directory sync is superfluous. |  | 
|  3080     */ |  | 
|  3081     if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |  | 
|  3082        /* |  | 
|  3083        ** We have received multiple reports of fsync() returning |  | 
|  3084        ** errors when applied to directories on certain file systems. |  | 
|  3085        ** A failed directory sync is not a big deal.  So it seems |  | 
|  3086        ** better to ignore the error.  Ticket #1657 |  | 
|  3087        */ |  | 
|  3088        /* pFile->lastErrno = errno; */ |  | 
|  3089        /* return SQLITE_IOERR; */ |  | 
|  3090     } |  | 
|  3091 #endif |  | 
|  3092     err = close(pFile->dirfd); /* Only need to sync once, so close the */ |  | 
|  3093     if( err==0 ){              /* directory when we are done */ |  | 
|  3094       pFile->dirfd = -1; |  | 
|  3095     }else{ |  | 
|  3096       pFile->lastErrno = errno; |  | 
|  3097       rc = SQLITE_IOERR_DIR_CLOSE; |  | 
|  3098     } |  | 
|  3099   } |  | 
|  3100   return rc; |  | 
|  3101 } |  | 
|  3102  |  | 
|  3103 /* |  | 
|  3104 ** Truncate an open file to a specified size |  | 
|  3105 */ |  | 
|  3106 static int unixTruncate(sqlite3_file *id, i64 nByte){ |  | 
|  3107   int rc; |  | 
|  3108   assert( id ); |  | 
|  3109   SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |  | 
|  3110   rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); |  | 
|  3111   if( rc ){ |  | 
|  3112     ((unixFile*)id)->lastErrno = errno; |  | 
|  3113     return SQLITE_IOERR_TRUNCATE; |  | 
|  3114   }else{ |  | 
|  3115     return SQLITE_OK; |  | 
|  3116   } |  | 
|  3117 } |  | 
|  3118  |  | 
|  3119 /* |  | 
|  3120 ** Determine the current size of a file in bytes |  | 
|  3121 */ |  | 
|  3122 static int unixFileSize(sqlite3_file *id, i64 *pSize){ |  | 
|  3123   int rc; |  | 
|  3124   struct stat buf; |  | 
|  3125   assert( id ); |  | 
|  3126   rc = fstat(((unixFile*)id)->h, &buf); |  | 
|  3127   SimulateIOError( rc=1 ); |  | 
|  3128   if( rc!=0 ){ |  | 
|  3129     ((unixFile*)id)->lastErrno = errno; |  | 
|  3130     return SQLITE_IOERR_FSTAT; |  | 
|  3131   } |  | 
|  3132   *pSize = buf.st_size; |  | 
|  3133  |  | 
|  3134   /* When opening a zero-size database, the findLockInfo() procedure |  | 
|  3135   ** writes a single byte into that file in order to work around a bug |  | 
|  3136   ** in the OS-X msdos filesystem.  In order to avoid problems with upper |  | 
|  3137   ** layers, we need to report this file size as zero even though it is |  | 
|  3138   ** really 1.   Ticket #3260. |  | 
|  3139   */ |  | 
|  3140   if( *pSize==1 ) *pSize = 0; |  | 
|  3141  |  | 
|  3142  |  | 
|  3143   return SQLITE_OK; |  | 
|  3144 } |  | 
|  3145  |  | 
|  3146 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |  | 
|  3147 /* |  | 
|  3148 ** Handler for proxy-locking file-control verbs.  Defined below in the |  | 
|  3149 ** proxying locking division. |  | 
|  3150 */ |  | 
|  3151 static int proxyFileControl(sqlite3_file*,int,void*); |  | 
|  3152 #endif |  | 
|  3153  |  | 
|  3154  |  | 
|  3155 /* |  | 
|  3156 ** Information and control of an open file handle. |  | 
|  3157 */ |  | 
|  3158 static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |  | 
|  3159   switch( op ){ |  | 
|  3160     case SQLITE_FCNTL_LOCKSTATE: { |  | 
|  3161       *(int*)pArg = ((unixFile*)id)->locktype; |  | 
|  3162       return SQLITE_OK; |  | 
|  3163     } |  | 
|  3164     case SQLITE_LAST_ERRNO: { |  | 
|  3165       *(int*)pArg = ((unixFile*)id)->lastErrno; |  | 
|  3166       return SQLITE_OK; |  | 
|  3167     } |  | 
|  3168 #ifndef NDEBUG |  | 
|  3169     /* The pager calls this method to signal that it has done |  | 
|  3170     ** a rollback and that the database is therefore unchanged and |  | 
|  3171     ** it hence it is OK for the transaction change counter to be |  | 
|  3172     ** unchanged. |  | 
|  3173     */ |  | 
|  3174     case SQLITE_FCNTL_DB_UNCHANGED: { |  | 
|  3175       ((unixFile*)id)->dbUpdate = 0; |  | 
|  3176       return SQLITE_OK; |  | 
|  3177     } |  | 
|  3178 #endif |  | 
|  3179 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |  | 
|  3180     case SQLITE_SET_LOCKPROXYFILE: |  | 
|  3181     case SQLITE_GET_LOCKPROXYFILE: { |  | 
|  3182       return proxyFileControl(id,op,pArg); |  | 
|  3183     } |  | 
|  3184 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |  | 
|  3185   } |  | 
|  3186   return SQLITE_ERROR; |  | 
|  3187 } |  | 
|  3188  |  | 
|  3189 /* |  | 
|  3190 ** Return the sector size in bytes of the underlying block device for |  | 
|  3191 ** the specified file. This is almost always 512 bytes, but may be |  | 
|  3192 ** larger for some devices. |  | 
|  3193 ** |  | 
|  3194 ** SQLite code assumes this function cannot fail. It also assumes that |  | 
|  3195 ** if two files are created in the same file-system directory (i.e. |  | 
|  3196 ** a database and its journal file) that the sector size will be the |  | 
|  3197 ** same for both. |  | 
|  3198 */ |  | 
|  3199 static int unixSectorSize(sqlite3_file *NotUsed){ |  | 
|  3200   UNUSED_PARAMETER(NotUsed); |  | 
|  3201   return SQLITE_DEFAULT_SECTOR_SIZE; |  | 
|  3202 } |  | 
|  3203  |  | 
|  3204 /* |  | 
|  3205 ** Return the device characteristics for the file. This is always 0 for unix. |  | 
|  3206 */ |  | 
|  3207 static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ |  | 
|  3208   UNUSED_PARAMETER(NotUsed); |  | 
|  3209   return 0; |  | 
|  3210 } |  | 
|  3211  |  | 
|  3212 /* |  | 
|  3213 ** Here ends the implementation of all sqlite3_file methods. |  | 
|  3214 ** |  | 
|  3215 ********************** End sqlite3_file Methods ******************************* |  | 
|  3216 ******************************************************************************/ |  | 
|  3217  |  | 
|  3218  |  | 
|  3219 /* |  | 
|  3220 ** This division contains definitions of sqlite3_io_methods objects that |  | 
|  3221 ** implement various file locking strategies.  It also contains definitions |  | 
|  3222 ** of "finder" functions.  A finder-function is used to locate the appropriate |  | 
|  3223 ** sqlite3_io_methods object for a particular database file.  The pAppData |  | 
|  3224 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |  | 
|  3225 ** the correct finder-function for that VFS. |  | 
|  3226 ** |  | 
|  3227 ** Most finder functions return a pointer to a fixed sqlite3_io_methods |  | 
|  3228 ** object.  The only interesting finder-function is autolockIoFinder, which |  | 
|  3229 ** looks at the filesystem type and tries to guess the best locking |  | 
|  3230 ** strategy from that. |  | 
|  3231 ** |  | 
|  3232 ** For finder-funtion F, two objects are created: |  | 
|  3233 ** |  | 
|  3234 **    (1) The real finder-function named "FImpt()". |  | 
|  3235 ** |  | 
|  3236 **    (2) A constant pointer to this function named just "F". |  | 
|  3237 ** |  | 
|  3238 ** |  | 
|  3239 ** A pointer to the F pointer is used as the pAppData value for VFS |  | 
|  3240 ** objects.  We have to do this instead of letting pAppData point |  | 
|  3241 ** directly at the finder-function since C90 rules prevent a void* |  | 
|  3242 ** from be cast into a function pointer. |  | 
|  3243 ** |  | 
|  3244 ** |  | 
|  3245 ** Each instance of this macro generates two objects: |  | 
|  3246 ** |  | 
|  3247 **   *  A constant sqlite3_io_methods object call METHOD that has locking |  | 
|  3248 **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |  | 
|  3249 ** |  | 
|  3250 **   *  An I/O method finder function called FINDER that returns a pointer |  | 
|  3251 **      to the METHOD object in the previous bullet. |  | 
|  3252 */ |  | 
|  3253 #define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK)               \ |  | 
|  3254 static const sqlite3_io_methods METHOD = {                                   \ |  | 
|  3255    1,                          /* iVersion */                                \ |  | 
|  3256    CLOSE,                      /* xClose */                                  \ |  | 
|  3257    unixRead,                   /* xRead */                                   \ |  | 
|  3258    unixWrite,                  /* xWrite */                                  \ |  | 
|  3259    unixTruncate,               /* xTruncate */                               \ |  | 
|  3260    unixSync,                   /* xSync */                                   \ |  | 
|  3261    unixFileSize,               /* xFileSize */                               \ |  | 
|  3262    LOCK,                       /* xLock */                                   \ |  | 
|  3263    UNLOCK,                     /* xUnlock */                                 \ |  | 
|  3264    CKLOCK,                     /* xCheckReservedLock */                      \ |  | 
|  3265    unixFileControl,            /* xFileControl */                            \ |  | 
|  3266    unixSectorSize,             /* xSectorSize */                             \ |  | 
|  3267    unixDeviceCharacteristics   /* xDeviceCapabilities */                     \ |  | 
|  3268 };                                                                           \ |  | 
|  3269 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \ |  | 
|  3270   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \ |  | 
|  3271   return &METHOD;                                                            \ |  | 
|  3272 }                                                                            \ |  | 
|  3273 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \ |  | 
|  3274     = FINDER##Impl; |  | 
|  3275  |  | 
|  3276 /* |  | 
|  3277 ** Here are all of the sqlite3_io_methods objects for each of the |  | 
|  3278 ** locking strategies.  Functions that return pointers to these methods |  | 
|  3279 ** are also created. |  | 
|  3280 */ |  | 
|  3281 IOMETHODS( |  | 
|  3282   posixIoFinder,            /* Finder function name */ |  | 
|  3283   posixIoMethods,           /* sqlite3_io_methods object name */ |  | 
|  3284   unixClose,                /* xClose method */ |  | 
|  3285   unixLock,                 /* xLock method */ |  | 
|  3286   unixUnlock,               /* xUnlock method */ |  | 
|  3287   unixCheckReservedLock     /* xCheckReservedLock method */ |  | 
|  3288 ) |  | 
|  3289 IOMETHODS( |  | 
|  3290   nolockIoFinder,           /* Finder function name */ |  | 
|  3291   nolockIoMethods,          /* sqlite3_io_methods object name */ |  | 
|  3292   nolockClose,              /* xClose method */ |  | 
|  3293   nolockLock,               /* xLock method */ |  | 
|  3294   nolockUnlock,             /* xUnlock method */ |  | 
|  3295   nolockCheckReservedLock   /* xCheckReservedLock method */ |  | 
|  3296 ) |  | 
|  3297 IOMETHODS( |  | 
|  3298   dotlockIoFinder,          /* Finder function name */ |  | 
|  3299   dotlockIoMethods,         /* sqlite3_io_methods object name */ |  | 
|  3300   dotlockClose,             /* xClose method */ |  | 
|  3301   dotlockLock,              /* xLock method */ |  | 
|  3302   dotlockUnlock,            /* xUnlock method */ |  | 
|  3303   dotlockCheckReservedLock  /* xCheckReservedLock method */ |  | 
|  3304 ) |  | 
|  3305  |  | 
|  3306 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |  | 
|  3307 IOMETHODS( |  | 
|  3308   flockIoFinder,            /* Finder function name */ |  | 
|  3309   flockIoMethods,           /* sqlite3_io_methods object name */ |  | 
|  3310   flockClose,               /* xClose method */ |  | 
|  3311   flockLock,                /* xLock method */ |  | 
|  3312   flockUnlock,              /* xUnlock method */ |  | 
|  3313   flockCheckReservedLock    /* xCheckReservedLock method */ |  | 
|  3314 ) |  | 
|  3315 #endif |  | 
|  3316  |  | 
|  3317 #if OS_VXWORKS |  | 
|  3318 IOMETHODS( |  | 
|  3319   semIoFinder,              /* Finder function name */ |  | 
|  3320   semIoMethods,             /* sqlite3_io_methods object name */ |  | 
|  3321   semClose,                 /* xClose method */ |  | 
|  3322   semLock,                  /* xLock method */ |  | 
|  3323   semUnlock,                /* xUnlock method */ |  | 
|  3324   semCheckReservedLock      /* xCheckReservedLock method */ |  | 
|  3325 ) |  | 
|  3326 #endif |  | 
|  3327  |  | 
|  3328 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |  | 
|  3329 IOMETHODS( |  | 
|  3330   afpIoFinder,              /* Finder function name */ |  | 
|  3331   afpIoMethods,             /* sqlite3_io_methods object name */ |  | 
|  3332   afpClose,                 /* xClose method */ |  | 
|  3333   afpLock,                  /* xLock method */ |  | 
|  3334   afpUnlock,                /* xUnlock method */ |  | 
|  3335   afpCheckReservedLock      /* xCheckReservedLock method */ |  | 
|  3336 ) |  | 
|  3337 #endif |  | 
|  3338  |  | 
|  3339 /* |  | 
|  3340 ** The "Whole File Locking" finder returns the same set of methods as |  | 
|  3341 ** the posix locking finder.  But it also sets the SQLITE_WHOLE_FILE_LOCKING |  | 
|  3342 ** flag to force the posix advisory locks to cover the whole file instead |  | 
|  3343 ** of just a small span of bytes near the 1GiB boundary.  Whole File Locking |  | 
|  3344 ** is useful on NFS-mounted files since it helps NFS to maintain cache |  | 
|  3345 ** coherency.  But it is a detriment to other filesystems since it runs |  | 
|  3346 ** slower. |  | 
|  3347 */ |  | 
|  3348 static const sqlite3_io_methods *posixWflIoFinderImpl(const char*z, unixFile*p){ |  | 
|  3349   UNUSED_PARAMETER(z); |  | 
|  3350   p->fileFlags = SQLITE_WHOLE_FILE_LOCKING; |  | 
|  3351   return &posixIoMethods; |  | 
|  3352 } |  | 
|  3353 static const sqlite3_io_methods  |  | 
|  3354   *(*const posixWflIoFinder)(const char*,unixFile *p) = posixWflIoFinderImpl; |  | 
|  3355  |  | 
|  3356 /* |  | 
|  3357 ** The proxy locking method is a "super-method" in the sense that it |  | 
|  3358 ** opens secondary file descriptors for the conch and lock files and |  | 
|  3359 ** it uses proxy, dot-file, AFP, and flock() locking methods on those |  | 
|  3360 ** secondary files.  For this reason, the division that implements |  | 
|  3361 ** proxy locking is located much further down in the file.  But we need |  | 
|  3362 ** to go ahead and define the sqlite3_io_methods and finder function |  | 
|  3363 ** for proxy locking here.  So we forward declare the I/O methods. |  | 
|  3364 */ |  | 
|  3365 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |  | 
|  3366 static int proxyClose(sqlite3_file*); |  | 
|  3367 static int proxyLock(sqlite3_file*, int); |  | 
|  3368 static int proxyUnlock(sqlite3_file*, int); |  | 
|  3369 static int proxyCheckReservedLock(sqlite3_file*, int*); |  | 
|  3370 IOMETHODS( |  | 
|  3371   proxyIoFinder,            /* Finder function name */ |  | 
|  3372   proxyIoMethods,           /* sqlite3_io_methods object name */ |  | 
|  3373   proxyClose,               /* xClose method */ |  | 
|  3374   proxyLock,                /* xLock method */ |  | 
|  3375   proxyUnlock,              /* xUnlock method */ |  | 
|  3376   proxyCheckReservedLock    /* xCheckReservedLock method */ |  | 
|  3377 ) |  | 
|  3378 #endif |  | 
|  3379  |  | 
|  3380  |  | 
|  3381 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |  | 
|  3382 /*  |  | 
|  3383 ** This "finder" function attempts to determine the best locking strategy  |  | 
|  3384 ** for the database file "filePath".  It then returns the sqlite3_io_methods |  | 
|  3385 ** object that implements that strategy. |  | 
|  3386 ** |  | 
|  3387 ** This is for MacOSX only. |  | 
|  3388 */ |  | 
|  3389 static const sqlite3_io_methods *autolockIoFinderImpl( |  | 
|  3390   const char *filePath,    /* name of the database file */ |  | 
|  3391   unixFile *pNew           /* open file object for the database file */ |  | 
|  3392 ){ |  | 
|  3393   static const struct Mapping { |  | 
|  3394     const char *zFilesystem;              /* Filesystem type name */ |  | 
|  3395     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */ |  | 
|  3396   } aMap[] = { |  | 
|  3397     { "hfs",    &posixIoMethods }, |  | 
|  3398     { "ufs",    &posixIoMethods }, |  | 
|  3399     { "afpfs",  &afpIoMethods }, |  | 
|  3400 #ifdef SQLITE_ENABLE_AFP_LOCKING_SMB |  | 
|  3401     { "smbfs",  &afpIoMethods }, |  | 
|  3402 #else |  | 
|  3403     { "smbfs",  &flockIoMethods }, |  | 
|  3404 #endif |  | 
|  3405     { "webdav", &nolockIoMethods }, |  | 
|  3406     { 0, 0 } |  | 
|  3407   }; |  | 
|  3408   int i; |  | 
|  3409   struct statfs fsInfo; |  | 
|  3410   struct flock lockInfo; |  | 
|  3411  |  | 
|  3412   if( !filePath ){ |  | 
|  3413     /* If filePath==NULL that means we are dealing with a transient file |  | 
|  3414     ** that does not need to be locked. */ |  | 
|  3415     return &nolockIoMethods; |  | 
|  3416   } |  | 
|  3417   if( statfs(filePath, &fsInfo) != -1 ){ |  | 
|  3418     if( fsInfo.f_flags & MNT_RDONLY ){ |  | 
|  3419       return &nolockIoMethods; |  | 
|  3420     } |  | 
|  3421     for(i=0; aMap[i].zFilesystem; i++){ |  | 
|  3422       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |  | 
|  3423         return aMap[i].pMethods; |  | 
|  3424       } |  | 
|  3425     } |  | 
|  3426   } |  | 
|  3427  |  | 
|  3428   /* Default case. Handles, amongst others, "nfs". |  | 
|  3429   ** Test byte-range lock using fcntl(). If the call succeeds,  |  | 
|  3430   ** assume that the file-system supports POSIX style locks.  |  | 
|  3431   */ |  | 
|  3432   lockInfo.l_len = 1; |  | 
|  3433   lockInfo.l_start = 0; |  | 
|  3434   lockInfo.l_whence = SEEK_SET; |  | 
|  3435   lockInfo.l_type = F_RDLCK; |  | 
|  3436   if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |  | 
|  3437     pNew->fileFlags = SQLITE_WHOLE_FILE_LOCKING; |  | 
|  3438     return &posixIoMethods; |  | 
|  3439   }else{ |  | 
|  3440     return &dotlockIoMethods; |  | 
|  3441   } |  | 
|  3442 } |  | 
|  3443 static const sqlite3_io_methods  |  | 
|  3444   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |  | 
|  3445  |  | 
|  3446 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |  | 
|  3447  |  | 
|  3448 #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |  | 
|  3449 /*  |  | 
|  3450 ** This "finder" function attempts to determine the best locking strategy  |  | 
|  3451 ** for the database file "filePath".  It then returns the sqlite3_io_methods |  | 
|  3452 ** object that implements that strategy. |  | 
|  3453 ** |  | 
|  3454 ** This is for VXWorks only. |  | 
|  3455 */ |  | 
|  3456 static const sqlite3_io_methods *autolockIoFinderImpl( |  | 
|  3457   const char *filePath,    /* name of the database file */ |  | 
|  3458   unixFile *pNew           /* the open file object */ |  | 
|  3459 ){ |  | 
|  3460   struct flock lockInfo; |  | 
|  3461  |  | 
|  3462   if( !filePath ){ |  | 
|  3463     /* If filePath==NULL that means we are dealing with a transient file |  | 
|  3464     ** that does not need to be locked. */ |  | 
|  3465     return &nolockIoMethods; |  | 
|  3466   } |  | 
|  3467  |  | 
|  3468   /* Test if fcntl() is supported and use POSIX style locks. |  | 
|  3469   ** Otherwise fall back to the named semaphore method. |  | 
|  3470   */ |  | 
|  3471   lockInfo.l_len = 1; |  | 
|  3472   lockInfo.l_start = 0; |  | 
|  3473   lockInfo.l_whence = SEEK_SET; |  | 
|  3474   lockInfo.l_type = F_RDLCK; |  | 
|  3475   if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |  | 
|  3476     return &posixIoMethods; |  | 
|  3477   }else{ |  | 
|  3478     return &semIoMethods; |  | 
|  3479   } |  | 
|  3480 } |  | 
|  3481 static const sqlite3_io_methods  |  | 
|  3482   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |  | 
|  3483  |  | 
|  3484 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |  | 
|  3485  |  | 
|  3486 /* |  | 
|  3487 ** An abstract type for a pointer to a IO method finder function: |  | 
|  3488 */ |  | 
|  3489 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |  | 
|  3490  |  | 
|  3491  |  | 
|  3492 /**************************************************************************** |  | 
|  3493 **************************** sqlite3_vfs methods **************************** |  | 
|  3494 ** |  | 
|  3495 ** This division contains the implementation of methods on the |  | 
|  3496 ** sqlite3_vfs object. |  | 
|  3497 */ |  | 
|  3498  |  | 
|  3499 /* |  | 
|  3500 ** Initialize the contents of the unixFile structure pointed to by pId. |  | 
|  3501 */ |  | 
|  3502 static int fillInUnixFile( |  | 
|  3503   sqlite3_vfs *pVfs,      /* Pointer to vfs object */ |  | 
|  3504   int h,                  /* Open file descriptor of file being opened */ |  | 
|  3505   int dirfd,              /* Directory file descriptor */ |  | 
|  3506   sqlite3_file *pId,      /* Write to the unixFile structure here */ |  | 
|  3507   const char *zFilename,  /* Name of the file being opened */ |  | 
|  3508   int noLock,             /* Omit locking if true */ |  | 
|  3509   int isDelete            /* Delete on close if true */ |  | 
|  3510 ){ |  | 
|  3511   const sqlite3_io_methods *pLockingStyle; |  | 
|  3512   unixFile *pNew = (unixFile *)pId; |  | 
|  3513   int rc = SQLITE_OK; |  | 
|  3514  |  | 
|  3515   assert( pNew->pLock==NULL ); |  | 
|  3516   assert( pNew->pOpen==NULL ); |  | 
|  3517  |  | 
|  3518   /* Parameter isDelete is only used on vxworks. Express this explicitly  |  | 
|  3519   ** here to prevent compiler warnings about unused parameters. |  | 
|  3520   */ |  | 
|  3521   UNUSED_PARAMETER(isDelete); |  | 
|  3522  |  | 
|  3523   OSTRACE3("OPEN    %-3d %s\n", h, zFilename);     |  | 
|  3524   pNew->h = h; |  | 
|  3525   pNew->dirfd = dirfd; |  | 
|  3526   SET_THREADID(pNew); |  | 
|  3527   pNew->fileFlags = 0; |  | 
|  3528  |  | 
|  3529 #if OS_VXWORKS |  | 
|  3530   pNew->pId = vxworksFindFileId(zFilename); |  | 
|  3531   if( pNew->pId==0 ){ |  | 
|  3532     noLock = 1; |  | 
|  3533     rc = SQLITE_NOMEM; |  | 
|  3534   } |  | 
|  3535 #endif |  | 
|  3536  |  | 
|  3537   if( noLock ){ |  | 
|  3538     pLockingStyle = &nolockIoMethods; |  | 
|  3539   }else{ |  | 
|  3540     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |  | 
|  3541 #if SQLITE_ENABLE_LOCKING_STYLE |  | 
|  3542     /* Cache zFilename in the locking context (AFP and dotlock override) for |  | 
|  3543     ** proxyLock activation is possible (remote proxy is based on db name) |  | 
|  3544     ** zFilename remains valid until file is closed, to support */ |  | 
|  3545     pNew->lockingContext = (void*)zFilename; |  | 
|  3546 #endif |  | 
|  3547   } |  | 
|  3548  |  | 
|  3549   if( pLockingStyle == &posixIoMethods ){ |  | 
|  3550     unixEnterMutex(); |  | 
|  3551     rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen); |  | 
|  3552     if( rc!=SQLITE_OK ){ |  | 
|  3553       /* If an error occured in findLockInfo(), close the file descriptor |  | 
|  3554       ** immediately, before releasing the mutex. findLockInfo() may fail |  | 
|  3555       ** in two scenarios: |  | 
|  3556       ** |  | 
|  3557       **   (a) A call to fstat() failed. |  | 
|  3558       **   (b) A malloc failed. |  | 
|  3559       ** |  | 
|  3560       ** Scenario (b) may only occur if the process is holding no other |  | 
|  3561       ** file descriptors open on the same file. If there were other file |  | 
|  3562       ** descriptors on this file, then no malloc would be required by |  | 
|  3563       ** findLockInfo(). If this is the case, it is quite safe to close |  | 
|  3564       ** handle h - as it is guaranteed that no posix locks will be released |  | 
|  3565       ** by doing so. |  | 
|  3566       ** |  | 
|  3567       ** If scenario (a) caused the error then things are not so safe. The |  | 
|  3568       ** implicit assumption here is that if fstat() fails, things are in |  | 
|  3569       ** such bad shape that dropping a lock or two doesn't matter much. |  | 
|  3570       */ |  | 
|  3571       close(h); |  | 
|  3572       h = -1; |  | 
|  3573     } |  | 
|  3574     unixLeaveMutex(); |  | 
|  3575   } |  | 
|  3576  |  | 
|  3577 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |  | 
|  3578   else if( pLockingStyle == &afpIoMethods ){ |  | 
|  3579     /* AFP locking uses the file path so it needs to be included in |  | 
|  3580     ** the afpLockingContext. |  | 
|  3581     */ |  | 
|  3582     afpLockingContext *pCtx; |  | 
|  3583     pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |  | 
|  3584     if( pCtx==0 ){ |  | 
|  3585       rc = SQLITE_NOMEM; |  | 
|  3586     }else{ |  | 
|  3587       /* NB: zFilename exists and remains valid until the file is closed |  | 
|  3588       ** according to requirement F11141.  So we do not need to make a |  | 
|  3589       ** copy of the filename. */ |  | 
|  3590       pCtx->dbPath = zFilename; |  | 
|  3591       srandomdev(); |  | 
|  3592       unixEnterMutex(); |  | 
|  3593       rc = findLockInfo(pNew, NULL, &pNew->pOpen); |  | 
|  3594       unixLeaveMutex();         |  | 
|  3595     } |  | 
|  3596   } |  | 
|  3597 #endif |  | 
|  3598  |  | 
|  3599   else if( pLockingStyle == &dotlockIoMethods ){ |  | 
|  3600     /* Dotfile locking uses the file path so it needs to be included in |  | 
|  3601     ** the dotlockLockingContext  |  | 
|  3602     */ |  | 
|  3603     char *zLockFile; |  | 
|  3604     int nFilename; |  | 
|  3605     nFilename = (int)strlen(zFilename) + 6; |  | 
|  3606     zLockFile = (char *)sqlite3_malloc(nFilename); |  | 
|  3607     if( zLockFile==0 ){ |  | 
|  3608       rc = SQLITE_NOMEM; |  | 
|  3609     }else{ |  | 
|  3610       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |  | 
|  3611     } |  | 
|  3612     pNew->lockingContext = zLockFile; |  | 
|  3613   } |  | 
|  3614  |  | 
|  3615 #if OS_VXWORKS |  | 
|  3616   else if( pLockingStyle == &semIoMethods ){ |  | 
|  3617     /* Named semaphore locking uses the file path so it needs to be |  | 
|  3618     ** included in the semLockingContext |  | 
|  3619     */ |  | 
|  3620     unixEnterMutex(); |  | 
|  3621     rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen); |  | 
|  3622     if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){ |  | 
|  3623       char *zSemName = pNew->pOpen->aSemName; |  | 
|  3624       int n; |  | 
|  3625       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |  | 
|  3626                        pNew->pId->zCanonicalName); |  | 
|  3627       for( n=1; zSemName[n]; n++ ) |  | 
|  3628         if( zSemName[n]=='/' ) zSemName[n] = '_'; |  | 
|  3629       pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |  | 
|  3630       if( pNew->pOpen->pSem == SEM_FAILED ){ |  | 
|  3631         rc = SQLITE_NOMEM; |  | 
|  3632         pNew->pOpen->aSemName[0] = '\0'; |  | 
|  3633       } |  | 
|  3634     } |  | 
|  3635     unixLeaveMutex(); |  | 
|  3636   } |  | 
|  3637 #endif |  | 
|  3638    |  | 
|  3639   pNew->lastErrno = 0; |  | 
|  3640 #if OS_VXWORKS |  | 
|  3641   if( rc!=SQLITE_OK ){ |  | 
|  3642     unlink(zFilename); |  | 
|  3643     isDelete = 0; |  | 
|  3644   } |  | 
|  3645   pNew->isDelete = isDelete; |  | 
|  3646 #endif |  | 
|  3647   if( rc!=SQLITE_OK ){ |  | 
|  3648     if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */ |  | 
|  3649     if( h>=0 ) close(h); |  | 
|  3650   }else{ |  | 
|  3651     pNew->pMethod = pLockingStyle; |  | 
|  3652     OpenCounter(+1); |  | 
|  3653   } |  | 
|  3654   return rc; |  | 
|  3655 } |  | 
|  3656  |  | 
|  3657 /* |  | 
|  3658 ** Open a file descriptor to the directory containing file zFilename. |  | 
|  3659 ** If successful, *pFd is set to the opened file descriptor and |  | 
|  3660 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |  | 
|  3661 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |  | 
|  3662 ** value. |  | 
|  3663 ** |  | 
|  3664 ** If SQLITE_OK is returned, the caller is responsible for closing |  | 
|  3665 ** the file descriptor *pFd using close(). |  | 
|  3666 */ |  | 
|  3667 static int openDirectory(const char *zFilename, int *pFd){ |  | 
|  3668   int ii; |  | 
|  3669   int fd = -1; |  | 
|  3670   char zDirname[MAX_PATHNAME+1]; |  | 
|  3671  |  | 
|  3672   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |  | 
|  3673   for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |  | 
|  3674   if( ii>0 ){ |  | 
|  3675     zDirname[ii] = '\0'; |  | 
|  3676     fd = open(zDirname, O_RDONLY|O_BINARY, 0); |  | 
|  3677     if( fd>=0 ){ |  | 
|  3678 #ifdef FD_CLOEXEC |  | 
|  3679       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |  | 
|  3680 #endif |  | 
|  3681       OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname); |  | 
|  3682     } |  | 
|  3683   } |  | 
|  3684   *pFd = fd; |  | 
|  3685   return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN); |  | 
|  3686 } |  | 
|  3687  |  | 
|  3688 /* |  | 
|  3689 ** Create a temporary file name in zBuf.  zBuf must be allocated |  | 
|  3690 ** by the calling process and must be big enough to hold at least |  | 
|  3691 ** pVfs->mxPathname bytes. |  | 
|  3692 */ |  | 
|  3693 static int getTempname(int nBuf, char *zBuf){ |  | 
|  3694   static const char *azDirs[] = { |  | 
|  3695      0, |  | 
|  3696      0, |  | 
|  3697      "/var/tmp", |  | 
|  3698      "/usr/tmp", |  | 
|  3699      "/tmp", |  | 
|  3700      ".", |  | 
|  3701   }; |  | 
|  3702   static const unsigned char zChars[] = |  | 
|  3703     "abcdefghijklmnopqrstuvwxyz" |  | 
|  3704     "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |  | 
|  3705     "0123456789"; |  | 
|  3706   unsigned int i, j; |  | 
|  3707   struct stat buf; |  | 
|  3708   const char *zDir = "."; |  | 
|  3709  |  | 
|  3710   /* It's odd to simulate an io-error here, but really this is just |  | 
|  3711   ** using the io-error infrastructure to test that SQLite handles this |  | 
|  3712   ** function failing.  |  | 
|  3713   */ |  | 
|  3714   SimulateIOError( return SQLITE_IOERR ); |  | 
|  3715  |  | 
|  3716   azDirs[0] = sqlite3_temp_directory; |  | 
|  3717   if (NULL == azDirs[1]) { |  | 
|  3718     azDirs[1] = getenv("TMPDIR"); |  | 
|  3719   } |  | 
|  3720    |  | 
|  3721   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |  | 
|  3722     if( azDirs[i]==0 ) continue; |  | 
|  3723     if( stat(azDirs[i], &buf) ) continue; |  | 
|  3724     if( !S_ISDIR(buf.st_mode) ) continue; |  | 
|  3725     if( access(azDirs[i], 07) ) continue; |  | 
|  3726     zDir = azDirs[i]; |  | 
|  3727     break; |  | 
|  3728   } |  | 
|  3729  |  | 
|  3730   /* Check that the output buffer is large enough for the temporary file  |  | 
|  3731   ** name. If it is not, return SQLITE_ERROR. |  | 
|  3732   */ |  | 
|  3733   if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |  | 
|  3734     return SQLITE_ERROR; |  | 
|  3735   } |  | 
|  3736  |  | 
|  3737   do{ |  | 
|  3738     sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |  | 
|  3739     j = (int)strlen(zBuf); |  | 
|  3740     sqlite3_randomness(15, &zBuf[j]); |  | 
|  3741     for(i=0; i<15; i++, j++){ |  | 
|  3742       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |  | 
|  3743     } |  | 
|  3744     zBuf[j] = 0; |  | 
|  3745   }while( access(zBuf,0)==0 ); |  | 
|  3746   return SQLITE_OK; |  | 
|  3747 } |  | 
|  3748  |  | 
|  3749 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |  | 
|  3750 /* |  | 
|  3751 ** Routine to transform a unixFile into a proxy-locking unixFile. |  | 
|  3752 ** Implementation in the proxy-lock division, but used by unixOpen() |  | 
|  3753 ** if SQLITE_PREFER_PROXY_LOCKING is defined. |  | 
|  3754 */ |  | 
|  3755 static int proxyTransformUnixFile(unixFile*, const char*); |  | 
|  3756 #endif |  | 
|  3757  |  | 
|  3758 /* |  | 
|  3759 ** Search for an unused file descriptor that was opened on the database  |  | 
|  3760 ** file (not a journal or master-journal file) identified by pathname |  | 
|  3761 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |  | 
|  3762 ** argument to this function. |  | 
|  3763 ** |  | 
|  3764 ** Such a file descriptor may exist if a database connection was closed |  | 
|  3765 ** but the associated file descriptor could not be closed because some |  | 
|  3766 ** other file descriptor open on the same file is holding a file-lock. |  | 
|  3767 ** Refer to comments in the unixClose() function and the lengthy comment |  | 
|  3768 ** describing "Posix Advisory Locking" at the start of this file for  |  | 
|  3769 ** further details. Also, ticket #4018. |  | 
|  3770 ** |  | 
|  3771 ** If a suitable file descriptor is found, then it is returned. If no |  | 
|  3772 ** such file descriptor is located, -1 is returned. |  | 
|  3773 */ |  | 
|  3774 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |  | 
|  3775   UnixUnusedFd *pUnused = 0; |  | 
|  3776  |  | 
|  3777   /* Do not search for an unused file descriptor on vxworks. Not because |  | 
|  3778   ** vxworks would not benefit from the change (it might, we're not sure), |  | 
|  3779   ** but because no way to test it is currently available. It is better  |  | 
|  3780   ** not to risk breaking vxworks support for the sake of such an obscure  |  | 
|  3781   ** feature.  */ |  | 
|  3782 #if !OS_VXWORKS |  | 
|  3783   struct stat sStat;                   /* Results of stat() call */ |  | 
|  3784  |  | 
|  3785   /* A stat() call may fail for various reasons. If this happens, it is |  | 
|  3786   ** almost certain that an open() call on the same path will also fail. |  | 
|  3787   ** For this reason, if an error occurs in the stat() call here, it is |  | 
|  3788   ** ignored and -1 is returned. The caller will try to open a new file |  | 
|  3789   ** descriptor on the same path, fail, and return an error to SQLite. |  | 
|  3790   ** |  | 
|  3791   ** Even if a subsequent open() call does succeed, the consequences of |  | 
|  3792   ** not searching for a resusable file descriptor are not dire.  */ |  | 
|  3793   if( 0==stat(zPath, &sStat) ){ |  | 
|  3794     struct unixOpenCnt *pO; |  | 
|  3795     struct unixFileId id; |  | 
|  3796     id.dev = sStat.st_dev; |  | 
|  3797     id.ino = sStat.st_ino; |  | 
|  3798  |  | 
|  3799     unixEnterMutex(); |  | 
|  3800     for(pO=openList; pO && memcmp(&id, &pO->fileId, sizeof(id)); pO=pO->pNext); |  | 
|  3801     if( pO ){ |  | 
|  3802       UnixUnusedFd **pp; |  | 
|  3803       for(pp=&pO->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |  | 
|  3804       pUnused = *pp; |  | 
|  3805       if( pUnused ){ |  | 
|  3806         *pp = pUnused->pNext; |  | 
|  3807       } |  | 
|  3808     } |  | 
|  3809     unixLeaveMutex(); |  | 
|  3810   } |  | 
|  3811 #endif    /* if !OS_VXWORKS */ |  | 
|  3812   return pUnused; |  | 
|  3813 } |  | 
|  3814  |  | 
|  3815 /* |  | 
|  3816 ** Initializes a unixFile structure with zeros. |  | 
|  3817 */ |  | 
|  3818 void chromium_sqlite3_initialize_unix_sqlite3_file(sqlite3_file* file) { |  | 
|  3819   memset(file, 0, sizeof(unixFile)); |  | 
|  3820 } |  | 
|  3821  |  | 
|  3822 int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* vfs, |  | 
|  3823                                                int fd, |  | 
|  3824                                                int dirfd, |  | 
|  3825                                                sqlite3_file* file, |  | 
|  3826                                                const char* fileName, |  | 
|  3827                                                int noLock, |  | 
|  3828                                                int isDelete) { |  | 
|  3829   return fillInUnixFile(vfs, fd, dirfd, file, fileName, noLock, isDelete); |  | 
|  3830 } |  | 
|  3831  |  | 
|  3832 /* |  | 
|  3833 ** Search for an unused file descriptor that was opened on the database file. |  | 
|  3834 ** If a suitable file descriptor if found, then it is stored in *fd; otherwise, |  | 
|  3835 ** *fd is not modified. |  | 
|  3836 ** |  | 
|  3837 ** If a reusable file descriptor is not found, and a new UnixUnusedFd cannot |  | 
|  3838 ** be allocated, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK is returned. |  | 
|  3839 */ |  | 
|  3840 int chromium_sqlite3_get_reusable_file_handle(sqlite3_file* file, |  | 
|  3841                                               const char* fileName, |  | 
|  3842                                               int flags, |  | 
|  3843                                               int* fd) { |  | 
|  3844   unixFile* unixSQLite3File = (unixFile*)file; |  | 
|  3845   int fileType = flags & 0xFFFFFF00; |  | 
|  3846   if (fileType == SQLITE_OPEN_MAIN_DB) { |  | 
|  3847     UnixUnusedFd *unusedFd = findReusableFd(fileName, flags); |  | 
|  3848     if (unusedFd) { |  | 
|  3849       *fd = unusedFd->fd; |  | 
|  3850     } else { |  | 
|  3851       unusedFd = sqlite3_malloc(sizeof(*unusedFd)); |  | 
|  3852       if (!unusedFd) { |  | 
|  3853         return SQLITE_NOMEM; |  | 
|  3854       } |  | 
|  3855     } |  | 
|  3856     unixSQLite3File->pUnused = unusedFd; |  | 
|  3857   } |  | 
|  3858   return SQLITE_OK; |  | 
|  3859 } |  | 
|  3860  |  | 
|  3861 /* |  | 
|  3862 ** Marks 'fd' as the unused file descriptor for 'pFile'. |  | 
|  3863 */ |  | 
|  3864 void chromium_sqlite3_update_reusable_file_handle(sqlite3_file* file, |  | 
|  3865                                                   int fd, |  | 
|  3866                                                   int flags) { |  | 
|  3867   unixFile* unixSQLite3File = (unixFile*)file; |  | 
|  3868   if (unixSQLite3File->pUnused) { |  | 
|  3869     unixSQLite3File->pUnused->fd = fd; |  | 
|  3870     unixSQLite3File->pUnused->flags = flags; |  | 
|  3871   } |  | 
|  3872 } |  | 
|  3873  |  | 
|  3874 /* |  | 
|  3875 ** Destroys pFile's field that keeps track of the unused file descriptor. |  | 
|  3876 */ |  | 
|  3877 void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file) { |  | 
|  3878   unixFile* unixSQLite3File = (unixFile*)file; |  | 
|  3879   sqlite3_free(unixSQLite3File->pUnused); |  | 
|  3880 } |  | 
|  3881  |  | 
|  3882 /* |  | 
|  3883 ** Open the file zPath. |  | 
|  3884 **  |  | 
|  3885 ** Previously, the SQLite OS layer used three functions in place of this |  | 
|  3886 ** one: |  | 
|  3887 ** |  | 
|  3888 **     sqlite3OsOpenReadWrite(); |  | 
|  3889 **     sqlite3OsOpenReadOnly(); |  | 
|  3890 **     sqlite3OsOpenExclusive(); |  | 
|  3891 ** |  | 
|  3892 ** These calls correspond to the following combinations of flags: |  | 
|  3893 ** |  | 
|  3894 **     ReadWrite() ->     (READWRITE | CREATE) |  | 
|  3895 **     ReadOnly()  ->     (READONLY)  |  | 
|  3896 **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |  | 
|  3897 ** |  | 
|  3898 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |  | 
|  3899 ** true, the file was configured to be automatically deleted when the |  | 
|  3900 ** file handle closed. To achieve the same effect using this new  |  | 
|  3901 ** interface, add the DELETEONCLOSE flag to those specified above for  |  | 
|  3902 ** OpenExclusive(). |  | 
|  3903 */ |  | 
|  3904 static int unixOpen( |  | 
|  3905   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */ |  | 
|  3906   const char *zPath,           /* Pathname of file to be opened */ |  | 
|  3907   sqlite3_file *pFile,         /* The file descriptor to be filled in */ |  | 
|  3908   int flags,                   /* Input flags to control the opening */ |  | 
|  3909   int *pOutFlags               /* Output flags returned to SQLite core */ |  | 
|  3910 ){ |  | 
|  3911   unixFile *p = (unixFile *)pFile; |  | 
|  3912   int fd = -1;                   /* File descriptor returned by open() */ |  | 
|  3913   int dirfd = -1;                /* Directory file descriptor */ |  | 
|  3914   int openFlags = 0;             /* Flags to pass to open() */ |  | 
|  3915   int eType = flags&0xFFFFFF00;  /* Type of file to open */ |  | 
|  3916   int noLock;                    /* True to omit locking primitives */ |  | 
|  3917   int rc = SQLITE_OK;            /* Function Return Code */ |  | 
|  3918  |  | 
|  3919   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE); |  | 
|  3920   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE); |  | 
|  3921   int isCreate     = (flags & SQLITE_OPEN_CREATE); |  | 
|  3922   int isReadonly   = (flags & SQLITE_OPEN_READONLY); |  | 
|  3923   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE); |  | 
|  3924  |  | 
|  3925   /* If creating a master or main-file journal, this function will open |  | 
|  3926   ** a file-descriptor on the directory too. The first time unixSync() |  | 
|  3927   ** is called the directory file descriptor will be fsync()ed and close()d. |  | 
|  3928   */ |  | 
|  3929   int isOpenDirectory = (isCreate &&  |  | 
|  3930       (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL) |  | 
|  3931   ); |  | 
|  3932  |  | 
|  3933   /* If argument zPath is a NULL pointer, this function is required to open |  | 
|  3934   ** a temporary file. Use this buffer to store the file name in. |  | 
|  3935   */ |  | 
|  3936   char zTmpname[MAX_PATHNAME+1]; |  | 
|  3937   const char *zName = zPath; |  | 
|  3938  |  | 
|  3939   /* Check the following statements are true:  |  | 
|  3940   ** |  | 
|  3941   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and  |  | 
|  3942   **   (b) if CREATE is set, then READWRITE must also be set, and |  | 
|  3943   **   (c) if EXCLUSIVE is set, then CREATE must also be set. |  | 
|  3944   **   (d) if DELETEONCLOSE is set, then CREATE must also be set. |  | 
|  3945   */ |  | 
|  3946   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |  | 
|  3947   assert(isCreate==0 || isReadWrite); |  | 
|  3948   assert(isExclusive==0 || isCreate); |  | 
|  3949   assert(isDelete==0 || isCreate); |  | 
|  3950  |  | 
|  3951   /* The main DB, main journal, and master journal are never automatically |  | 
|  3952   ** deleted. Nor are they ever temporary files.  */ |  | 
|  3953   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |  | 
|  3954   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |  | 
|  3955   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |  | 
|  3956  |  | 
|  3957   /* Assert that the upper layer has set one of the "file-type" flags. */ |  | 
|  3958   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB  |  | 
|  3959        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL  |  | 
|  3960        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL  |  | 
|  3961        || eType==SQLITE_OPEN_TRANSIENT_DB |  | 
|  3962   ); |  | 
|  3963  |  | 
|  3964   chromium_sqlite3_initialize_unix_sqlite3_file(pFile); |  | 
|  3965  |  | 
|  3966   if( eType==SQLITE_OPEN_MAIN_DB ){ |  | 
|  3967     rc = chromium_sqlite3_get_reusable_file_handle(pFile, zName, flags, &fd); |  | 
|  3968     if( rc!=SQLITE_OK ){ |  | 
|  3969       return rc; |  | 
|  3970     } |  | 
|  3971   }else if( !zName ){ |  | 
|  3972     /* If zName is NULL, the upper layer is requesting a temp file. */ |  | 
|  3973     assert(isDelete && !isOpenDirectory); |  | 
|  3974     rc = getTempname(MAX_PATHNAME+1, zTmpname); |  | 
|  3975     if( rc!=SQLITE_OK ){ |  | 
|  3976       return rc; |  | 
|  3977     } |  | 
|  3978     zName = zTmpname; |  | 
|  3979   } |  | 
|  3980  |  | 
|  3981   /* Determine the value of the flags parameter passed to POSIX function |  | 
|  3982   ** open(). These must be calculated even if open() is not called, as |  | 
|  3983   ** they may be stored as part of the file handle and used by the  |  | 
|  3984   ** 'conch file' locking functions later on.  */ |  | 
|  3985   if( isReadonly )  openFlags |= O_RDONLY; |  | 
|  3986   if( isReadWrite ) openFlags |= O_RDWR; |  | 
|  3987   if( isCreate )    openFlags |= O_CREAT; |  | 
|  3988   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |  | 
|  3989   openFlags |= (O_LARGEFILE|O_BINARY); |  | 
|  3990  |  | 
|  3991   if( fd<0 ){ |  | 
|  3992     mode_t openMode = (isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS); |  | 
|  3993     fd = open(zName, openFlags, openMode); |  | 
|  3994     OSTRACE4("OPENX   %-3d %s 0%o\n", fd, zName, openFlags); |  | 
|  3995     if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |  | 
|  3996       /* Failed to open the file for read/write access. Try read-only. */ |  | 
|  3997       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |  | 
|  3998       openFlags &= ~(O_RDWR|O_CREAT); |  | 
|  3999       flags |= SQLITE_OPEN_READONLY; |  | 
|  4000       openFlags |= O_RDONLY; |  | 
|  4001       fd = open(zName, openFlags, openMode); |  | 
|  4002     } |  | 
|  4003     if( fd<0 ){ |  | 
|  4004       rc = SQLITE_CANTOPEN; |  | 
|  4005       goto open_finished; |  | 
|  4006     } |  | 
|  4007   } |  | 
|  4008   assert( fd>=0 ); |  | 
|  4009   if( pOutFlags ){ |  | 
|  4010     *pOutFlags = flags; |  | 
|  4011   } |  | 
|  4012  |  | 
|  4013   chromium_sqlite3_update_reusable_file_handle(pFile, fd, flags); |  | 
|  4014  |  | 
|  4015   if( isDelete ){ |  | 
|  4016 #if OS_VXWORKS |  | 
|  4017     zPath = zName; |  | 
|  4018 #else |  | 
|  4019     unlink(zName); |  | 
|  4020 #endif |  | 
|  4021   } |  | 
|  4022 #if SQLITE_ENABLE_LOCKING_STYLE |  | 
|  4023   else{ |  | 
|  4024     p->openFlags = openFlags; |  | 
|  4025   } |  | 
|  4026 #endif |  | 
|  4027  |  | 
|  4028   if( isOpenDirectory ){ |  | 
|  4029     rc = openDirectory(zPath, &dirfd); |  | 
|  4030     if( rc!=SQLITE_OK ){ |  | 
|  4031       /* It is safe to close fd at this point, because it is guaranteed not |  | 
|  4032       ** to be open on a database file. If it were open on a database file, |  | 
|  4033       ** it would not be safe to close as this would release any locks held |  | 
|  4034       ** on the file by this process.  */ |  | 
|  4035       assert( eType!=SQLITE_OPEN_MAIN_DB ); |  | 
|  4036       close(fd);             /* silently leak if fail, already in error */ |  | 
|  4037       goto open_finished; |  | 
|  4038     } |  | 
|  4039   } |  | 
|  4040  |  | 
|  4041 #ifdef FD_CLOEXEC |  | 
|  4042   fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |  | 
|  4043 #endif |  | 
|  4044  |  | 
|  4045   noLock = eType!=SQLITE_OPEN_MAIN_DB; |  | 
|  4046  |  | 
|  4047 #if SQLITE_PREFER_PROXY_LOCKING |  | 
|  4048   if( zPath!=NULL && !noLock && pVfs->xOpen ){ |  | 
|  4049     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |  | 
|  4050     int useProxy = 0; |  | 
|  4051  |  | 
|  4052     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means  |  | 
|  4053     ** never use proxy, NULL means use proxy for non-local files only.  */ |  | 
|  4054     if( envforce!=NULL ){ |  | 
|  4055       useProxy = atoi(envforce)>0; |  | 
|  4056     }else{ |  | 
|  4057       struct statfs fsInfo; |  | 
|  4058       if( statfs(zPath, &fsInfo) == -1 ){ |  | 
|  4059         /* In theory, the close(fd) call is sub-optimal. If the file opened |  | 
|  4060         ** with fd is a database file, and there are other connections open |  | 
|  4061         ** on that file that are currently holding advisory locks on it, |  | 
|  4062         ** then the call to close() will cancel those locks. In practice, |  | 
|  4063         ** we're assuming that statfs() doesn't fail very often. At least |  | 
|  4064         ** not while other file descriptors opened by the same process on |  | 
|  4065         ** the same file are working.  */ |  | 
|  4066         p->lastErrno = errno; |  | 
|  4067         if( dirfd>=0 ){ |  | 
|  4068           close(dirfd); /* silently leak if fail, in error */ |  | 
|  4069         } |  | 
|  4070         close(fd); /* silently leak if fail, in error */ |  | 
|  4071         rc = SQLITE_IOERR_ACCESS; |  | 
|  4072         goto open_finished; |  | 
|  4073       } |  | 
|  4074       useProxy = !(fsInfo.f_flags&MNT_LOCAL); |  | 
|  4075     } |  | 
|  4076     if( useProxy ){ |  | 
|  4077       rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |  | 
|  4078       if( rc==SQLITE_OK ){ |  | 
|  4079         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |  | 
|  4080       } |  | 
|  4081       goto open_finished; |  | 
|  4082     } |  | 
|  4083   } |  | 
|  4084 #endif |  | 
|  4085  |  | 
|  4086   rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |  | 
|  4087 open_finished: |  | 
|  4088   if( rc!=SQLITE_OK ){ |  | 
|  4089     chromium_sqlite3_destroy_reusable_file_handle(pFile); |  | 
|  4090   } |  | 
|  4091   return rc; |  | 
|  4092 } |  | 
|  4093  |  | 
|  4094  |  | 
|  4095 /* |  | 
|  4096 ** Delete the file at zPath. If the dirSync argument is true, fsync() |  | 
|  4097 ** the directory after deleting the file. |  | 
|  4098 */ |  | 
|  4099 static int unixDelete( |  | 
|  4100   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */ |  | 
|  4101   const char *zPath,        /* Name of file to be deleted */ |  | 
|  4102   int dirSync               /* If true, fsync() directory after deleting file */ |  | 
|  4103 ){ |  | 
|  4104   int rc = SQLITE_OK; |  | 
|  4105   UNUSED_PARAMETER(NotUsed); |  | 
|  4106   SimulateIOError(return SQLITE_IOERR_DELETE); |  | 
|  4107   unlink(zPath); |  | 
|  4108 #ifndef SQLITE_DISABLE_DIRSYNC |  | 
|  4109   if( dirSync ){ |  | 
|  4110     int fd; |  | 
|  4111     rc = openDirectory(zPath, &fd); |  | 
|  4112     if( rc==SQLITE_OK ){ |  | 
|  4113 #if OS_VXWORKS |  | 
|  4114       if( fsync(fd)==-1 ) |  | 
|  4115 #else |  | 
|  4116       if( fsync(fd) ) |  | 
|  4117 #endif |  | 
|  4118       { |  | 
|  4119         rc = SQLITE_IOERR_DIR_FSYNC; |  | 
|  4120       } |  | 
|  4121       if( close(fd)&&!rc ){ |  | 
|  4122         rc = SQLITE_IOERR_DIR_CLOSE; |  | 
|  4123       } |  | 
|  4124     } |  | 
|  4125   } |  | 
|  4126 #endif |  | 
|  4127   return rc; |  | 
|  4128 } |  | 
|  4129  |  | 
|  4130 /* |  | 
|  4131 ** Test the existance of or access permissions of file zPath. The |  | 
|  4132 ** test performed depends on the value of flags: |  | 
|  4133 ** |  | 
|  4134 **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists |  | 
|  4135 **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |  | 
|  4136 **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |  | 
|  4137 ** |  | 
|  4138 ** Otherwise return 0. |  | 
|  4139 */ |  | 
|  4140 static int unixAccess( |  | 
|  4141   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */ |  | 
|  4142   const char *zPath,      /* Path of the file to examine */ |  | 
|  4143   int flags,              /* What do we want to learn about the zPath file? */ |  | 
|  4144   int *pResOut            /* Write result boolean here */ |  | 
|  4145 ){ |  | 
|  4146   int amode = 0; |  | 
|  4147   UNUSED_PARAMETER(NotUsed); |  | 
|  4148   SimulateIOError( return SQLITE_IOERR_ACCESS; ); |  | 
|  4149   switch( flags ){ |  | 
|  4150     case SQLITE_ACCESS_EXISTS: |  | 
|  4151       amode = F_OK; |  | 
|  4152       break; |  | 
|  4153     case SQLITE_ACCESS_READWRITE: |  | 
|  4154       amode = W_OK|R_OK; |  | 
|  4155       break; |  | 
|  4156     case SQLITE_ACCESS_READ: |  | 
|  4157       amode = R_OK; |  | 
|  4158       break; |  | 
|  4159  |  | 
|  4160     default: |  | 
|  4161       assert(!"Invalid flags argument"); |  | 
|  4162   } |  | 
|  4163   *pResOut = (access(zPath, amode)==0); |  | 
|  4164   return SQLITE_OK; |  | 
|  4165 } |  | 
|  4166  |  | 
|  4167  |  | 
|  4168 /* |  | 
|  4169 ** Turn a relative pathname into a full pathname. The relative path |  | 
|  4170 ** is stored as a nul-terminated string in the buffer pointed to by |  | 
|  4171 ** zPath.  |  | 
|  4172 ** |  | 
|  4173 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes  |  | 
|  4174 ** (in this case, MAX_PATHNAME bytes). The full-path is written to |  | 
|  4175 ** this buffer before returning. |  | 
|  4176 */ |  | 
|  4177 static int unixFullPathname( |  | 
|  4178   sqlite3_vfs *pVfs,            /* Pointer to vfs object */ |  | 
|  4179   const char *zPath,            /* Possibly relative input path */ |  | 
|  4180   int nOut,                     /* Size of output buffer in bytes */ |  | 
|  4181   char *zOut                    /* Output buffer */ |  | 
|  4182 ){ |  | 
|  4183  |  | 
|  4184   /* It's odd to simulate an io-error here, but really this is just |  | 
|  4185   ** using the io-error infrastructure to test that SQLite handles this |  | 
|  4186   ** function failing. This function could fail if, for example, the |  | 
|  4187   ** current working directory has been unlinked. |  | 
|  4188   */ |  | 
|  4189   SimulateIOError( return SQLITE_ERROR ); |  | 
|  4190  |  | 
|  4191   assert( pVfs->mxPathname==MAX_PATHNAME ); |  | 
|  4192   UNUSED_PARAMETER(pVfs); |  | 
|  4193  |  | 
|  4194   zOut[nOut-1] = '\0'; |  | 
|  4195   if( zPath[0]=='/' ){ |  | 
|  4196     sqlite3_snprintf(nOut, zOut, "%s", zPath); |  | 
|  4197   }else{ |  | 
|  4198     int nCwd; |  | 
|  4199     if( getcwd(zOut, nOut-1)==0 ){ |  | 
|  4200       return SQLITE_CANTOPEN; |  | 
|  4201     } |  | 
|  4202     nCwd = (int)strlen(zOut); |  | 
|  4203     sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |  | 
|  4204   } |  | 
|  4205   return SQLITE_OK; |  | 
|  4206 } |  | 
|  4207  |  | 
|  4208  |  | 
|  4209 #ifndef SQLITE_OMIT_LOAD_EXTENSION |  | 
|  4210 /* |  | 
|  4211 ** Interfaces for opening a shared library, finding entry points |  | 
|  4212 ** within the shared library, and closing the shared library. |  | 
|  4213 */ |  | 
|  4214 #include <dlfcn.h> |  | 
|  4215 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |  | 
|  4216   UNUSED_PARAMETER(NotUsed); |  | 
|  4217   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |  | 
|  4218 } |  | 
|  4219  |  | 
|  4220 /* |  | 
|  4221 ** SQLite calls this function immediately after a call to unixDlSym() or |  | 
|  4222 ** unixDlOpen() fails (returns a null pointer). If a more detailed error |  | 
|  4223 ** message is available, it is written to zBufOut. If no error message |  | 
|  4224 ** is available, zBufOut is left unmodified and SQLite uses a default |  | 
|  4225 ** error message. |  | 
|  4226 */ |  | 
|  4227 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |  | 
|  4228   char *zErr; |  | 
|  4229   UNUSED_PARAMETER(NotUsed); |  | 
|  4230   unixEnterMutex(); |  | 
|  4231   zErr = dlerror(); |  | 
|  4232   if( zErr ){ |  | 
|  4233     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |  | 
|  4234   } |  | 
|  4235   unixLeaveMutex(); |  | 
|  4236 } |  | 
|  4237 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |  | 
|  4238   /*  |  | 
|  4239   ** GCC with -pedantic-errors says that C90 does not allow a void* to be |  | 
|  4240   ** cast into a pointer to a function.  And yet the library dlsym() routine |  | 
|  4241   ** returns a void* which is really a pointer to a function.  So how do we |  | 
|  4242   ** use dlsym() with -pedantic-errors? |  | 
|  4243   ** |  | 
|  4244   ** Variable x below is defined to be a pointer to a function taking |  | 
|  4245   ** parameters void* and const char* and returning a pointer to a function. |  | 
|  4246   ** We initialize x by assigning it a pointer to the dlsym() function. |  | 
|  4247   ** (That assignment requires a cast.)  Then we call the function that |  | 
|  4248   ** x points to.   |  | 
|  4249   ** |  | 
|  4250   ** This work-around is unlikely to work correctly on any system where |  | 
|  4251   ** you really cannot cast a function pointer into void*.  But then, on the |  | 
|  4252   ** other hand, dlsym() will not work on such a system either, so we have |  | 
|  4253   ** not really lost anything. |  | 
|  4254   */ |  | 
|  4255   void (*(*x)(void*,const char*))(void); |  | 
|  4256   UNUSED_PARAMETER(NotUsed); |  | 
|  4257   x = (void(*(*)(void*,const char*))(void))dlsym; |  | 
|  4258   return (*x)(p, zSym); |  | 
|  4259 } |  | 
|  4260 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |  | 
|  4261   UNUSED_PARAMETER(NotUsed); |  | 
|  4262   dlclose(pHandle); |  | 
|  4263 } |  | 
|  4264 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |  | 
|  4265   #define unixDlOpen  0 |  | 
|  4266   #define unixDlError 0 |  | 
|  4267   #define unixDlSym   0 |  | 
|  4268   #define unixDlClose 0 |  | 
|  4269 #endif |  | 
|  4270  |  | 
|  4271 /* |  | 
|  4272 ** Write nBuf bytes of random data to the supplied buffer zBuf. |  | 
|  4273 */ |  | 
|  4274 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |  | 
|  4275   UNUSED_PARAMETER(NotUsed); |  | 
|  4276   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |  | 
|  4277  |  | 
|  4278   /* We have to initialize zBuf to prevent valgrind from reporting |  | 
|  4279   ** errors.  The reports issued by valgrind are incorrect - we would |  | 
|  4280   ** prefer that the randomness be increased by making use of the |  | 
|  4281   ** uninitialized space in zBuf - but valgrind errors tend to worry |  | 
|  4282   ** some users.  Rather than argue, it seems easier just to initialize |  | 
|  4283   ** the whole array and silence valgrind, even if that means less randomness |  | 
|  4284   ** in the random seed. |  | 
|  4285   ** |  | 
|  4286   ** When testing, initializing zBuf[] to zero is all we do.  That means |  | 
|  4287   ** that we always use the same random number sequence.  This makes the |  | 
|  4288   ** tests repeatable. |  | 
|  4289   */ |  | 
|  4290   memset(zBuf, 0, nBuf); |  | 
|  4291 #if !defined(SQLITE_TEST) |  | 
|  4292   { |  | 
|  4293     int pid, fd; |  | 
|  4294     fd = open("/dev/urandom", O_RDONLY); |  | 
|  4295     if( fd<0 ){ |  | 
|  4296       time_t t; |  | 
|  4297       time(&t); |  | 
|  4298       memcpy(zBuf, &t, sizeof(t)); |  | 
|  4299       pid = getpid(); |  | 
|  4300       memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |  | 
|  4301       assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |  | 
|  4302       nBuf = sizeof(t) + sizeof(pid); |  | 
|  4303     }else{ |  | 
|  4304       nBuf = read(fd, zBuf, nBuf); |  | 
|  4305       close(fd); |  | 
|  4306     } |  | 
|  4307   } |  | 
|  4308 #endif |  | 
|  4309   return nBuf; |  | 
|  4310 } |  | 
|  4311  |  | 
|  4312  |  | 
|  4313 /* |  | 
|  4314 ** Sleep for a little while.  Return the amount of time slept. |  | 
|  4315 ** The argument is the number of microseconds we want to sleep. |  | 
|  4316 ** The return value is the number of microseconds of sleep actually |  | 
|  4317 ** requested from the underlying operating system, a number which |  | 
|  4318 ** might be greater than or equal to the argument, but not less |  | 
|  4319 ** than the argument. |  | 
|  4320 */ |  | 
|  4321 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |  | 
|  4322 #if OS_VXWORKS |  | 
|  4323   struct timespec sp; |  | 
|  4324  |  | 
|  4325   sp.tv_sec = microseconds / 1000000; |  | 
|  4326   sp.tv_nsec = (microseconds % 1000000) * 1000; |  | 
|  4327   nanosleep(&sp, NULL); |  | 
|  4328   UNUSED_PARAMETER(NotUsed); |  | 
|  4329   return microseconds; |  | 
|  4330 #elif defined(HAVE_USLEEP) && HAVE_USLEEP |  | 
|  4331   usleep(microseconds); |  | 
|  4332   UNUSED_PARAMETER(NotUsed); |  | 
|  4333   return microseconds; |  | 
|  4334 #else |  | 
|  4335   int seconds = (microseconds+999999)/1000000; |  | 
|  4336   sleep(seconds); |  | 
|  4337   UNUSED_PARAMETER(NotUsed); |  | 
|  4338   return seconds*1000000; |  | 
|  4339 #endif |  | 
|  4340 } |  | 
|  4341  |  | 
|  4342 /* |  | 
|  4343 ** The following variable, if set to a non-zero value, is interpreted as |  | 
|  4344 ** the number of seconds since 1970 and is used to set the result of |  | 
|  4345 ** sqlite3OsCurrentTime() during testing. |  | 
|  4346 */ |  | 
|  4347 #ifdef SQLITE_TEST |  | 
|  4348 int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */ |  | 
|  4349 #endif |  | 
|  4350  |  | 
|  4351 /* |  | 
|  4352 ** Find the current time (in Universal Coordinated Time).  Write the |  | 
|  4353 ** current time and date as a Julian Day number into *prNow and |  | 
|  4354 ** return 0.  Return 1 if the time and date cannot be found. |  | 
|  4355 */ |  | 
|  4356 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |  | 
|  4357 #if defined(SQLITE_OMIT_FLOATING_POINT) |  | 
|  4358   time_t t; |  | 
|  4359   time(&t); |  | 
|  4360   *prNow = (((sqlite3_int64)t)/8640 + 24405875)/10; |  | 
|  4361 #elif defined(NO_GETTOD) |  | 
|  4362   time_t t; |  | 
|  4363   time(&t); |  | 
|  4364   *prNow = t/86400.0 + 2440587.5; |  | 
|  4365 #elif OS_VXWORKS |  | 
|  4366   struct timespec sNow; |  | 
|  4367   clock_gettime(CLOCK_REALTIME, &sNow); |  | 
|  4368   *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0; |  | 
|  4369 #else |  | 
|  4370   struct timeval sNow; |  | 
|  4371   gettimeofday(&sNow, 0); |  | 
|  4372   *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0; |  | 
|  4373 #endif |  | 
|  4374  |  | 
|  4375 #ifdef SQLITE_TEST |  | 
|  4376   if( sqlite3_current_time ){ |  | 
|  4377     *prNow = sqlite3_current_time/86400.0 + 2440587.5; |  | 
|  4378   } |  | 
|  4379 #endif |  | 
|  4380   UNUSED_PARAMETER(NotUsed); |  | 
|  4381   return 0; |  | 
|  4382 } |  | 
|  4383  |  | 
|  4384 /* |  | 
|  4385 ** We added the xGetLastError() method with the intention of providing |  | 
|  4386 ** better low-level error messages when operating-system problems come up |  | 
|  4387 ** during SQLite operation.  But so far, none of that has been implemented |  | 
|  4388 ** in the core.  So this routine is never called.  For now, it is merely |  | 
|  4389 ** a place-holder. |  | 
|  4390 */ |  | 
|  4391 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |  | 
|  4392   UNUSED_PARAMETER(NotUsed); |  | 
|  4393   UNUSED_PARAMETER(NotUsed2); |  | 
|  4394   UNUSED_PARAMETER(NotUsed3); |  | 
|  4395   return 0; |  | 
|  4396 } |  | 
|  4397  |  | 
|  4398 /* |  | 
|  4399 ************************ End of sqlite3_vfs methods *************************** |  | 
|  4400 ******************************************************************************/ |  | 
|  4401  |  | 
|  4402 /****************************************************************************** |  | 
|  4403 ************************** Begin Proxy Locking ******************************** |  | 
|  4404 ** |  | 
|  4405 ** Proxy locking is a "uber-locking-method" in this sense:  It uses the |  | 
|  4406 ** other locking methods on secondary lock files.  Proxy locking is a |  | 
|  4407 ** meta-layer over top of the primitive locking implemented above.  For |  | 
|  4408 ** this reason, the division that implements of proxy locking is deferred |  | 
|  4409 ** until late in the file (here) after all of the other I/O methods have |  | 
|  4410 ** been defined - so that the primitive locking methods are available |  | 
|  4411 ** as services to help with the implementation of proxy locking. |  | 
|  4412 ** |  | 
|  4413 **** |  | 
|  4414 ** |  | 
|  4415 ** The default locking schemes in SQLite use byte-range locks on the |  | 
|  4416 ** database file to coordinate safe, concurrent access by multiple readers |  | 
|  4417 ** and writers [http://sqlite.org/lockingv3.html].  The five file locking |  | 
|  4418 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |  | 
|  4419 ** as POSIX read & write locks over fixed set of locations (via fsctl), |  | 
|  4420 ** on AFP and SMB only exclusive byte-range locks are available via fsctl |  | 
|  4421 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |  | 
|  4422 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |  | 
|  4423 ** address in the shared range is taken for a SHARED lock, the entire |  | 
|  4424 ** shared range is taken for an EXCLUSIVE lock): |  | 
|  4425 ** |  | 
|  4426 **      PENDING_BYTE        0x40000000                   |  | 
|  4427 **      RESERVED_BYTE       0x40000001 |  | 
|  4428 **      SHARED_RANGE        0x40000002 -> 0x40000200 |  | 
|  4429 ** |  | 
|  4430 ** This works well on the local file system, but shows a nearly 100x |  | 
|  4431 ** slowdown in read performance on AFP because the AFP client disables |  | 
|  4432 ** the read cache when byte-range locks are present.  Enabling the read |  | 
|  4433 ** cache exposes a cache coherency problem that is present on all OS X |  | 
|  4434 ** supported network file systems.  NFS and AFP both observe the |  | 
|  4435 ** close-to-open semantics for ensuring cache coherency |  | 
|  4436 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |  | 
|  4437 ** address the requirements for concurrent database access by multiple |  | 
|  4438 ** readers and writers |  | 
|  4439 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |  | 
|  4440 ** |  | 
|  4441 ** To address the performance and cache coherency issues, proxy file locking |  | 
|  4442 ** changes the way database access is controlled by limiting access to a |  | 
|  4443 ** single host at a time and moving file locks off of the database file |  | 
|  4444 ** and onto a proxy file on the local file system.   |  | 
|  4445 ** |  | 
|  4446 ** |  | 
|  4447 ** Using proxy locks |  | 
|  4448 ** ----------------- |  | 
|  4449 ** |  | 
|  4450 ** C APIs |  | 
|  4451 ** |  | 
|  4452 **  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |  | 
|  4453 **                       <proxy_path> | ":auto:"); |  | 
|  4454 **  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |  | 
|  4455 ** |  | 
|  4456 ** |  | 
|  4457 ** SQL pragmas |  | 
|  4458 ** |  | 
|  4459 **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |  | 
|  4460 **  PRAGMA [database.]lock_proxy_file |  | 
|  4461 ** |  | 
|  4462 ** Specifying ":auto:" means that if there is a conch file with a matching |  | 
|  4463 ** host ID in it, the proxy path in the conch file will be used, otherwise |  | 
|  4464 ** a proxy path based on the user's temp dir |  | 
|  4465 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |  | 
|  4466 ** actual proxy file name is generated from the name and path of the |  | 
|  4467 ** database file.  For example: |  | 
|  4468 ** |  | 
|  4469 **       For database path "/Users/me/foo.db"  |  | 
|  4470 **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |  | 
|  4471 ** |  | 
|  4472 ** Once a lock proxy is configured for a database connection, it can not |  | 
|  4473 ** be removed, however it may be switched to a different proxy path via |  | 
|  4474 ** the above APIs (assuming the conch file is not being held by another |  | 
|  4475 ** connection or process).  |  | 
|  4476 ** |  | 
|  4477 ** |  | 
|  4478 ** How proxy locking works |  | 
|  4479 ** ----------------------- |  | 
|  4480 ** |  | 
|  4481 ** Proxy file locking relies primarily on two new supporting files:  |  | 
|  4482 ** |  | 
|  4483 **   *  conch file to limit access to the database file to a single host |  | 
|  4484 **      at a time |  | 
|  4485 ** |  | 
|  4486 **   *  proxy file to act as a proxy for the advisory locks normally |  | 
|  4487 **      taken on the database |  | 
|  4488 ** |  | 
|  4489 ** The conch file - to use a proxy file, sqlite must first "hold the conch" |  | 
|  4490 ** by taking an sqlite-style shared lock on the conch file, reading the |  | 
|  4491 ** contents and comparing the host's unique host ID (see below) and lock |  | 
|  4492 ** proxy path against the values stored in the conch.  The conch file is |  | 
|  4493 ** stored in the same directory as the database file and the file name |  | 
|  4494 ** is patterned after the database file name as ".<databasename>-conch". |  | 
|  4495 ** If the conch file does not exist, or it's contents do not match the |  | 
|  4496 ** host ID and/or proxy path, then the lock is escalated to an exclusive |  | 
|  4497 ** lock and the conch file contents is updated with the host ID and proxy |  | 
|  4498 ** path and the lock is downgraded to a shared lock again.  If the conch |  | 
|  4499 ** is held by another process (with a shared lock), the exclusive lock |  | 
|  4500 ** will fail and SQLITE_BUSY is returned. |  | 
|  4501 ** |  | 
|  4502 ** The proxy file - a single-byte file used for all advisory file locks |  | 
|  4503 ** normally taken on the database file.   This allows for safe sharing |  | 
|  4504 ** of the database file for multiple readers and writers on the same |  | 
|  4505 ** host (the conch ensures that they all use the same local lock file). |  | 
|  4506 ** |  | 
|  4507 ** There is a third file - the host ID file - used as a persistent record |  | 
|  4508 ** of a unique identifier for the host, a 128-byte unique host id file |  | 
|  4509 ** in the path defined by the HOSTIDPATH macro (default value is |  | 
|  4510 ** /Library/Caches/.com.apple.sqliteConchHostId). |  | 
|  4511 ** |  | 
|  4512 ** Requesting the lock proxy does not immediately take the conch, it is |  | 
|  4513 ** only taken when the first request to lock database file is made.   |  | 
|  4514 ** This matches the semantics of the traditional locking behavior, where |  | 
|  4515 ** opening a connection to a database file does not take a lock on it. |  | 
|  4516 ** The shared lock and an open file descriptor are maintained until  |  | 
|  4517 ** the connection to the database is closed.  |  | 
|  4518 ** |  | 
|  4519 ** The proxy file and the lock file are never deleted so they only need |  | 
|  4520 ** to be created the first time they are used. |  | 
|  4521 ** |  | 
|  4522 ** Configuration options |  | 
|  4523 ** --------------------- |  | 
|  4524 ** |  | 
|  4525 **  SQLITE_PREFER_PROXY_LOCKING |  | 
|  4526 ** |  | 
|  4527 **       Database files accessed on non-local file systems are |  | 
|  4528 **       automatically configured for proxy locking, lock files are |  | 
|  4529 **       named automatically using the same logic as |  | 
|  4530 **       PRAGMA lock_proxy_file=":auto:" |  | 
|  4531 **     |  | 
|  4532 **  SQLITE_PROXY_DEBUG |  | 
|  4533 ** |  | 
|  4534 **       Enables the logging of error messages during host id file |  | 
|  4535 **       retrieval and creation |  | 
|  4536 ** |  | 
|  4537 **  HOSTIDPATH |  | 
|  4538 ** |  | 
|  4539 **       Overrides the default host ID file path location |  | 
|  4540 ** |  | 
|  4541 **  LOCKPROXYDIR |  | 
|  4542 ** |  | 
|  4543 **       Overrides the default directory used for lock proxy files that |  | 
|  4544 **       are named automatically via the ":auto:" setting |  | 
|  4545 ** |  | 
|  4546 **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |  | 
|  4547 ** |  | 
|  4548 **       Permissions to use when creating a directory for storing the |  | 
|  4549 **       lock proxy files, only used when LOCKPROXYDIR is not set. |  | 
|  4550 **     |  | 
|  4551 **     |  | 
|  4552 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |  | 
|  4553 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |  | 
|  4554 ** force proxy locking to be used for every database file opened, and 0 |  | 
|  4555 ** will force automatic proxy locking to be disabled for all database |  | 
|  4556 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |  | 
|  4557 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |  | 
|  4558 */ |  | 
|  4559  |  | 
|  4560 /* |  | 
|  4561 ** Proxy locking is only available on MacOSX  |  | 
|  4562 */ |  | 
|  4563 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |  | 
|  4564  |  | 
|  4565 #ifdef SQLITE_TEST |  | 
|  4566 /* simulate multiple hosts by creating unique hostid file paths */ |  | 
|  4567 int sqlite3_hostid_num = 0; |  | 
|  4568 #endif |  | 
|  4569  |  | 
|  4570 /* |  | 
|  4571 ** The proxyLockingContext has the path and file structures for the remote  |  | 
|  4572 ** and local proxy files in it |  | 
|  4573 */ |  | 
|  4574 typedef struct proxyLockingContext proxyLockingContext; |  | 
|  4575 struct proxyLockingContext { |  | 
|  4576   unixFile *conchFile;         /* Open conch file */ |  | 
|  4577   char *conchFilePath;         /* Name of the conch file */ |  | 
|  4578   unixFile *lockProxy;         /* Open proxy lock file */ |  | 
|  4579   char *lockProxyPath;         /* Name of the proxy lock file */ |  | 
|  4580   char *dbPath;                /* Name of the open file */ |  | 
|  4581   int conchHeld;               /* True if the conch is currently held */ |  | 
|  4582   void *oldLockingContext;     /* Original lockingcontext to restore on close */ |  | 
|  4583   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */ |  | 
|  4584 }; |  | 
|  4585  |  | 
|  4586 /* HOSTIDLEN and CONCHLEN both include space for the string  |  | 
|  4587 ** terminating nul  |  | 
|  4588 */ |  | 
|  4589 #define HOSTIDLEN         128 |  | 
|  4590 #define CONCHLEN          (MAXPATHLEN+HOSTIDLEN+1) |  | 
|  4591 #ifndef HOSTIDPATH |  | 
|  4592 # define HOSTIDPATH       "/Library/Caches/.com.apple.sqliteConchHostId" |  | 
|  4593 #endif |  | 
|  4594  |  | 
|  4595 /* basically a copy of unixRandomness with different |  | 
|  4596 ** test behavior built in */ |  | 
|  4597 static int proxyGenerateHostID(char *pHostID){ |  | 
|  4598   int pid, fd, len; |  | 
|  4599   unsigned char *key = (unsigned char *)pHostID; |  | 
|  4600    |  | 
|  4601   memset(key, 0, HOSTIDLEN); |  | 
|  4602   len = 0; |  | 
|  4603   fd = open("/dev/urandom", O_RDONLY); |  | 
|  4604   if( fd>=0 ){ |  | 
|  4605     len = read(fd, key, HOSTIDLEN); |  | 
|  4606     close(fd); /* silently leak the fd if it fails */ |  | 
|  4607   } |  | 
|  4608   if( len < HOSTIDLEN ){ |  | 
|  4609     time_t t; |  | 
|  4610     time(&t); |  | 
|  4611     memcpy(key, &t, sizeof(t)); |  | 
|  4612     pid = getpid(); |  | 
|  4613     memcpy(&key[sizeof(t)], &pid, sizeof(pid)); |  | 
|  4614   } |  | 
|  4615    |  | 
|  4616 #ifdef MAKE_PRETTY_HOSTID |  | 
|  4617   { |  | 
|  4618     int i; |  | 
|  4619     /* filter the bytes into printable ascii characters and NUL terminate */ |  | 
|  4620     key[(HOSTIDLEN-1)] = 0x00; |  | 
|  4621     for( i=0; i<(HOSTIDLEN-1); i++ ){ |  | 
|  4622       unsigned char pa = key[i]&0x7F; |  | 
|  4623       if( pa<0x20 ){ |  | 
|  4624         key[i] = (key[i]&0x80 == 0x80) ? pa+0x40 : pa+0x20; |  | 
|  4625       }else if( pa==0x7F ){ |  | 
|  4626         key[i] = (key[i]&0x80 == 0x80) ? pa=0x20 : pa+0x7E; |  | 
|  4627       } |  | 
|  4628     } |  | 
|  4629   } |  | 
|  4630 #endif |  | 
|  4631   return SQLITE_OK; |  | 
|  4632 } |  | 
|  4633  |  | 
|  4634 /* writes the host id path to path, path should be an pre-allocated buffer |  | 
|  4635 ** with enough space for a path  |  | 
|  4636 */ |  | 
|  4637 static void proxyGetHostIDPath(char *path, size_t len){ |  | 
|  4638   strlcpy(path, HOSTIDPATH, len); |  | 
|  4639 #ifdef SQLITE_TEST |  | 
|  4640   if( sqlite3_hostid_num>0 ){ |  | 
|  4641     char suffix[2] = "1"; |  | 
|  4642     suffix[0] = suffix[0] + sqlite3_hostid_num; |  | 
|  4643     strlcat(path, suffix, len); |  | 
|  4644   } |  | 
|  4645 #endif |  | 
|  4646   OSTRACE3("GETHOSTIDPATH  %s pid=%d\n", path, getpid()); |  | 
|  4647 } |  | 
|  4648  |  | 
|  4649 /* get the host ID from a sqlite hostid file stored in the  |  | 
|  4650 ** user-specific tmp directory, create the ID if it's not there already  |  | 
|  4651 */ |  | 
|  4652 static int proxyGetHostID(char *pHostID, int *pError){ |  | 
|  4653   int fd; |  | 
|  4654   char path[MAXPATHLEN];  |  | 
|  4655   size_t len; |  | 
|  4656   int rc=SQLITE_OK; |  | 
|  4657  |  | 
|  4658   proxyGetHostIDPath(path, MAXPATHLEN); |  | 
|  4659   /* try to create the host ID file, if it already exists read the contents */ |  | 
|  4660   fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644); |  | 
|  4661   if( fd<0 ){ |  | 
|  4662     int err=errno; |  | 
|  4663                  |  | 
|  4664     if( err!=EEXIST ){ |  | 
|  4665 #ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */ |  | 
|  4666       fprintf(stderr, "sqlite error creating host ID file %s: %s\n", |  | 
|  4667               path, strerror(err)); |  | 
|  4668 #endif |  | 
|  4669       return SQLITE_PERM; |  | 
|  4670     } |  | 
|  4671     /* couldn't create the file, read it instead */ |  | 
|  4672     fd = open(path, O_RDONLY|O_EXCL); |  | 
|  4673     if( fd<0 ){ |  | 
|  4674 #ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */ |  | 
|  4675       int err = errno; |  | 
|  4676       fprintf(stderr, "sqlite error opening host ID file %s: %s\n", |  | 
|  4677               path, strerror(err)); |  | 
|  4678 #endif |  | 
|  4679       return SQLITE_PERM; |  | 
|  4680     } |  | 
|  4681     len = pread(fd, pHostID, HOSTIDLEN, 0); |  | 
|  4682     if( len<0 ){ |  | 
|  4683       *pError = errno; |  | 
|  4684       rc = SQLITE_IOERR_READ; |  | 
|  4685     }else if( len<HOSTIDLEN ){ |  | 
|  4686       *pError = 0; |  | 
|  4687       rc = SQLITE_IOERR_SHORT_READ; |  | 
|  4688     } |  | 
|  4689     close(fd); /* silently leak the fd if it fails */ |  | 
|  4690     OSTRACE3("GETHOSTID  read %s pid=%d\n", pHostID, getpid()); |  | 
|  4691     return rc; |  | 
|  4692   }else{ |  | 
|  4693     /* we're creating the host ID file (use a random string of bytes) */ |  | 
|  4694     proxyGenerateHostID(pHostID); |  | 
|  4695     len = pwrite(fd, pHostID, HOSTIDLEN, 0); |  | 
|  4696     if( len<0 ){ |  | 
|  4697       *pError = errno; |  | 
|  4698       rc = SQLITE_IOERR_WRITE; |  | 
|  4699     }else if( len<HOSTIDLEN ){ |  | 
|  4700       *pError = 0; |  | 
|  4701       rc = SQLITE_IOERR_WRITE; |  | 
|  4702     } |  | 
|  4703     close(fd); /* silently leak the fd if it fails */ |  | 
|  4704     OSTRACE3("GETHOSTID  wrote %s pid=%d\n", pHostID, getpid()); |  | 
|  4705     return rc; |  | 
|  4706   } |  | 
|  4707 } |  | 
|  4708  |  | 
|  4709 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |  | 
|  4710   int len; |  | 
|  4711   int dbLen; |  | 
|  4712   int i; |  | 
|  4713  |  | 
|  4714 #ifdef LOCKPROXYDIR |  | 
|  4715   len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |  | 
|  4716 #else |  | 
|  4717 # ifdef _CS_DARWIN_USER_TEMP_DIR |  | 
|  4718   { |  | 
|  4719     confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen); |  | 
|  4720     len = strlcat(lPath, "sqliteplocks", maxLen); |  | 
|  4721     if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |  | 
|  4722       /* if mkdir fails, handle as lock file creation failure */ |  | 
|  4723 #  ifdef SQLITE_DEBUG |  | 
|  4724       int err = errno; |  | 
|  4725       if( err!=EEXIST ){ |  | 
|  4726         fprintf(stderr, "proxyGetLockPath: mkdir(%s,0%o) error %d %s\n", lPath, |  | 
|  4727                 SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err)); |  | 
|  4728       } |  | 
|  4729 #  endif |  | 
|  4730     }else{ |  | 
|  4731       OSTRACE3("GETLOCKPATH  mkdir %s pid=%d\n", lPath, getpid()); |  | 
|  4732     } |  | 
|  4733      |  | 
|  4734   } |  | 
|  4735 # else |  | 
|  4736   len = strlcpy(lPath, "/tmp/", maxLen); |  | 
|  4737 # endif |  | 
|  4738 #endif |  | 
|  4739  |  | 
|  4740   if( lPath[len-1]!='/' ){ |  | 
|  4741     len = strlcat(lPath, "/", maxLen); |  | 
|  4742   } |  | 
|  4743    |  | 
|  4744   /* transform the db path to a unique cache name */ |  | 
|  4745   dbLen = (int)strlen(dbPath); |  | 
|  4746   for( i=0; i<dbLen && (i+len+7)<maxLen; i++){ |  | 
|  4747     char c = dbPath[i]; |  | 
|  4748     lPath[i+len] = (c=='/')?'_':c; |  | 
|  4749   } |  | 
|  4750   lPath[i+len]='\0'; |  | 
|  4751   strlcat(lPath, ":auto:", maxLen); |  | 
|  4752   return SQLITE_OK; |  | 
|  4753 } |  | 
|  4754  |  | 
|  4755 /* |  | 
|  4756 ** Create a new VFS file descriptor (stored in memory obtained from |  | 
|  4757 ** sqlite3_malloc) and open the file named "path" in the file descriptor. |  | 
|  4758 ** |  | 
|  4759 ** The caller is responsible not only for closing the file descriptor |  | 
|  4760 ** but also for freeing the memory associated with the file descriptor. |  | 
|  4761 */ |  | 
|  4762 static int proxyCreateUnixFile(const char *path, unixFile **ppFile) { |  | 
|  4763   unixFile *pNew; |  | 
|  4764   int flags = SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE; |  | 
|  4765   int rc = SQLITE_OK; |  | 
|  4766   sqlite3_vfs dummyVfs; |  | 
|  4767  |  | 
|  4768   pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile)); |  | 
|  4769   if( !pNew ){ |  | 
|  4770     return SQLITE_NOMEM; |  | 
|  4771   } |  | 
|  4772   memset(pNew, 0, sizeof(unixFile)); |  | 
|  4773  |  | 
|  4774   /* Call unixOpen() to open the proxy file. The flags passed to unixOpen() |  | 
|  4775   ** suggest that the file being opened is a "main database". This is |  | 
|  4776   ** necessary as other file types do not necessarily support locking. It |  | 
|  4777   ** is better to use unixOpen() instead of opening the file directly with |  | 
|  4778   ** open(), as unixOpen() sets up the various mechanisms required to |  | 
|  4779   ** make sure a call to close() does not cause the system to discard |  | 
|  4780   ** POSIX locks prematurely. |  | 
|  4781   ** |  | 
|  4782   ** It is important that the xOpen member of the VFS object passed to  |  | 
|  4783   ** unixOpen() is NULL. This tells unixOpen() may try to open a proxy-file  |  | 
|  4784   ** for the proxy-file (creating a potential infinite loop). |  | 
|  4785   */ |  | 
|  4786   dummyVfs.pAppData = (void*)&autolockIoFinder; |  | 
|  4787   dummyVfs.xOpen = 0; |  | 
|  4788   rc = unixOpen(&dummyVfs, path, (sqlite3_file *)pNew, flags, &flags); |  | 
|  4789   if( rc==SQLITE_OK && (flags&SQLITE_OPEN_READONLY) ){ |  | 
|  4790     pNew->pMethod->xClose((sqlite3_file *)pNew); |  | 
|  4791     rc = SQLITE_CANTOPEN; |  | 
|  4792   } |  | 
|  4793  |  | 
|  4794   if( rc!=SQLITE_OK ){ |  | 
|  4795     sqlite3_free(pNew); |  | 
|  4796     pNew = 0; |  | 
|  4797   } |  | 
|  4798  |  | 
|  4799   *ppFile = pNew; |  | 
|  4800   return rc; |  | 
|  4801 } |  | 
|  4802  |  | 
|  4803 /* takes the conch by taking a shared lock and read the contents conch, if  |  | 
|  4804 ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL  |  | 
|  4805 ** lockPath means that the lockPath in the conch file will be used if the  |  | 
|  4806 ** host IDs match, or a new lock path will be generated automatically  |  | 
|  4807 ** and written to the conch file. |  | 
|  4808 */ |  | 
|  4809 static int proxyTakeConch(unixFile *pFile){ |  | 
|  4810   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;  |  | 
|  4811    |  | 
|  4812   if( pCtx->conchHeld>0 ){ |  | 
|  4813     return SQLITE_OK; |  | 
|  4814   }else{ |  | 
|  4815     unixFile *conchFile = pCtx->conchFile; |  | 
|  4816     char testValue[CONCHLEN]; |  | 
|  4817     char conchValue[CONCHLEN]; |  | 
|  4818     char lockPath[MAXPATHLEN]; |  | 
|  4819     char *tLockPath = NULL; |  | 
|  4820     int rc = SQLITE_OK; |  | 
|  4821     int readRc = SQLITE_OK; |  | 
|  4822     int syncPerms = 0; |  | 
|  4823  |  | 
|  4824     OSTRACE4("TAKECONCH  %d for %s pid=%d\n", conchFile->h, |  | 
|  4825              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()); |  | 
|  4826  |  | 
|  4827     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |  | 
|  4828     if( rc==SQLITE_OK ){ |  | 
|  4829       int pError = 0; |  | 
|  4830       memset(testValue, 0, CONCHLEN); /* conch is fixed size */ |  | 
|  4831       rc = proxyGetHostID(testValue, &pError); |  | 
|  4832       if( (rc&0xff)==SQLITE_IOERR ){ |  | 
|  4833         pFile->lastErrno = pError; |  | 
|  4834       } |  | 
|  4835       if( pCtx->lockProxyPath ){ |  | 
|  4836         strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN); |  | 
|  4837       } |  | 
|  4838     } |  | 
|  4839     if( rc!=SQLITE_OK ){ |  | 
|  4840       goto end_takeconch; |  | 
|  4841     } |  | 
|  4842      |  | 
|  4843     readRc = unixRead((sqlite3_file *)conchFile, conchValue, CONCHLEN, 0); |  | 
|  4844     if( readRc!=SQLITE_IOERR_SHORT_READ ){ |  | 
|  4845       if( readRc!=SQLITE_OK ){ |  | 
|  4846         if( (rc&0xff)==SQLITE_IOERR ){ |  | 
|  4847           pFile->lastErrno = conchFile->lastErrno; |  | 
|  4848         } |  | 
|  4849         rc = readRc; |  | 
|  4850         goto end_takeconch; |  | 
|  4851       } |  | 
|  4852       /* if the conch has data compare the contents */ |  | 
|  4853       if( !pCtx->lockProxyPath ){ |  | 
|  4854         /* for auto-named local lock file, just check the host ID and we'll |  | 
|  4855          ** use the local lock file path that's already in there */ |  | 
|  4856         if( !memcmp(testValue, conchValue, HOSTIDLEN) ){ |  | 
|  4857           tLockPath = (char *)&conchValue[HOSTIDLEN]; |  | 
|  4858           goto end_takeconch; |  | 
|  4859         } |  | 
|  4860       }else{ |  | 
|  4861         /* we've got the conch if conchValue matches our path and host ID */ |  | 
|  4862         if( !memcmp(testValue, conchValue, CONCHLEN) ){ |  | 
|  4863           goto end_takeconch; |  | 
|  4864         } |  | 
|  4865       } |  | 
|  4866     }else{ |  | 
|  4867       /* a short read means we're "creating" the conch (even though it could  |  | 
|  4868       ** have been user-intervention), if we acquire the exclusive lock, |  | 
|  4869       ** we'll try to match the current on-disk permissions of the database |  | 
|  4870       */ |  | 
|  4871       syncPerms = 1; |  | 
|  4872     } |  | 
|  4873      |  | 
|  4874     /* either conch was emtpy or didn't match */ |  | 
|  4875     if( !pCtx->lockProxyPath ){ |  | 
|  4876       proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |  | 
|  4877       tLockPath = lockPath; |  | 
|  4878       strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN); |  | 
|  4879     } |  | 
|  4880      |  | 
|  4881     /* update conch with host and path (this will fail if other process |  | 
|  4882      ** has a shared lock already) */ |  | 
|  4883     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |  | 
|  4884     if( rc==SQLITE_OK ){ |  | 
|  4885       rc = unixWrite((sqlite3_file *)conchFile, testValue, CONCHLEN, 0); |  | 
|  4886       if( rc==SQLITE_OK && syncPerms ){ |  | 
|  4887         struct stat buf; |  | 
|  4888         int err = fstat(pFile->h, &buf); |  | 
|  4889         if( err==0 ){ |  | 
|  4890           /* try to match the database file permissions, ignore failure */ |  | 
|  4891 #ifndef SQLITE_PROXY_DEBUG |  | 
|  4892           fchmod(conchFile->h, buf.st_mode); |  | 
|  4893 #else |  | 
|  4894           if( fchmod(conchFile->h, buf.st_mode)!=0 ){ |  | 
|  4895             int code = errno; |  | 
|  4896             fprintf(stderr, "fchmod %o FAILED with %d %s\n", |  | 
|  4897                              buf.st_mode, code, strerror(code)); |  | 
|  4898           } else { |  | 
|  4899             fprintf(stderr, "fchmod %o SUCCEDED\n",buf.st_mode); |  | 
|  4900           } |  | 
|  4901         }else{ |  | 
|  4902           int code = errno; |  | 
|  4903           fprintf(stderr, "STAT FAILED[%d] with %d %s\n",  |  | 
|  4904                           err, code, strerror(code)); |  | 
|  4905 #endif |  | 
|  4906         } |  | 
|  4907       } |  | 
|  4908     } |  | 
|  4909     conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |  | 
|  4910    |  | 
|  4911 end_takeconch: |  | 
|  4912     OSTRACE2("TRANSPROXY: CLOSE  %d\n", pFile->h); |  | 
|  4913     if( rc==SQLITE_OK && pFile->openFlags ){ |  | 
|  4914       if( pFile->h>=0 ){ |  | 
|  4915 #ifdef STRICT_CLOSE_ERROR |  | 
|  4916         if( close(pFile->h) ){ |  | 
|  4917           pFile->lastErrno = errno; |  | 
|  4918           return SQLITE_IOERR_CLOSE; |  | 
|  4919         } |  | 
|  4920 #else |  | 
|  4921         close(pFile->h); /* silently leak fd if fail */ |  | 
|  4922 #endif |  | 
|  4923       } |  | 
|  4924       pFile->h = -1; |  | 
|  4925       int fd = open(pCtx->dbPath, pFile->openFlags, |  | 
|  4926                     SQLITE_DEFAULT_FILE_PERMISSIONS); |  | 
|  4927       OSTRACE2("TRANSPROXY: OPEN  %d\n", fd); |  | 
|  4928       if( fd>=0 ){ |  | 
|  4929         pFile->h = fd; |  | 
|  4930       }else{ |  | 
|  4931         rc=SQLITE_CANTOPEN; /* SQLITE_BUSY? proxyTakeConch called |  | 
|  4932                                during locking */ |  | 
|  4933       } |  | 
|  4934     } |  | 
|  4935     if( rc==SQLITE_OK && !pCtx->lockProxy ){ |  | 
|  4936       char *path = tLockPath ? tLockPath : pCtx->lockProxyPath; |  | 
|  4937       /* ACS: Need to make a copy of path sometimes */ |  | 
|  4938       rc = proxyCreateUnixFile(path, &pCtx->lockProxy); |  | 
|  4939     } |  | 
|  4940     if( rc==SQLITE_OK ){ |  | 
|  4941       pCtx->conchHeld = 1; |  | 
|  4942  |  | 
|  4943       if( tLockPath ){ |  | 
|  4944         pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath); |  | 
|  4945         if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |  | 
|  4946           ((afpLockingContext *)pCtx->lockProxy->lockingContext)->dbPath = |  | 
|  4947                      pCtx->lockProxyPath; |  | 
|  4948         } |  | 
|  4949       } |  | 
|  4950     } else { |  | 
|  4951       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |  | 
|  4952     } |  | 
|  4953     OSTRACE3("TAKECONCH  %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed"); |  | 
|  4954     return rc; |  | 
|  4955   } |  | 
|  4956 } |  | 
|  4957  |  | 
|  4958 /* |  | 
|  4959 ** If pFile holds a lock on a conch file, then release that lock. |  | 
|  4960 */ |  | 
|  4961 static int proxyReleaseConch(unixFile *pFile){ |  | 
|  4962   int rc;                     /* Subroutine return code */ |  | 
|  4963   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */ |  | 
|  4964   unixFile *conchFile;        /* Name of the conch file */ |  | 
|  4965  |  | 
|  4966   pCtx = (proxyLockingContext *)pFile->lockingContext; |  | 
|  4967   conchFile = pCtx->conchFile; |  | 
|  4968   OSTRACE4("RELEASECONCH  %d for %s pid=%d\n", conchFile->h, |  | 
|  4969            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),  |  | 
|  4970            getpid()); |  | 
|  4971   pCtx->conchHeld = 0; |  | 
|  4972   rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |  | 
|  4973   OSTRACE3("RELEASECONCH  %d %s\n", conchFile->h, |  | 
|  4974            (rc==SQLITE_OK ? "ok" : "failed")); |  | 
|  4975   return rc; |  | 
|  4976 } |  | 
|  4977  |  | 
|  4978 /* |  | 
|  4979 ** Given the name of a database file, compute the name of its conch file. |  | 
|  4980 ** Store the conch filename in memory obtained from sqlite3_malloc(). |  | 
|  4981 ** Make *pConchPath point to the new name.  Return SQLITE_OK on success |  | 
|  4982 ** or SQLITE_NOMEM if unable to obtain memory. |  | 
|  4983 ** |  | 
|  4984 ** The caller is responsible for ensuring that the allocated memory |  | 
|  4985 ** space is eventually freed. |  | 
|  4986 ** |  | 
|  4987 ** *pConchPath is set to NULL if a memory allocation error occurs. |  | 
|  4988 */ |  | 
|  4989 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |  | 
|  4990   int i;                        /* Loop counter */ |  | 
|  4991   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |  | 
|  4992   char *conchPath;              /* buffer in which to construct conch name */ |  | 
|  4993  |  | 
|  4994   /* Allocate space for the conch filename and initialize the name to |  | 
|  4995   ** the name of the original database file. */   |  | 
|  4996   *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |  | 
|  4997   if( conchPath==0 ){ |  | 
|  4998     return SQLITE_NOMEM; |  | 
|  4999   } |  | 
|  5000   memcpy(conchPath, dbPath, len+1); |  | 
|  5001    |  | 
|  5002   /* now insert a "." before the last / character */ |  | 
|  5003   for( i=(len-1); i>=0; i-- ){ |  | 
|  5004     if( conchPath[i]=='/' ){ |  | 
|  5005       i++; |  | 
|  5006       break; |  | 
|  5007     } |  | 
|  5008   } |  | 
|  5009   conchPath[i]='.'; |  | 
|  5010   while ( i<len ){ |  | 
|  5011     conchPath[i+1]=dbPath[i]; |  | 
|  5012     i++; |  | 
|  5013   } |  | 
|  5014  |  | 
|  5015   /* append the "-conch" suffix to the file */ |  | 
|  5016   memcpy(&conchPath[i+1], "-conch", 7); |  | 
|  5017   assert( (int)strlen(conchPath) == len+7 ); |  | 
|  5018  |  | 
|  5019   return SQLITE_OK; |  | 
|  5020 } |  | 
|  5021  |  | 
|  5022  |  | 
|  5023 /* Takes a fully configured proxy locking-style unix file and switches |  | 
|  5024 ** the local lock file path  |  | 
|  5025 */ |  | 
|  5026 static int switchLockProxyPath(unixFile *pFile, const char *path) { |  | 
|  5027   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |  | 
|  5028   char *oldPath = pCtx->lockProxyPath; |  | 
|  5029   int rc = SQLITE_OK; |  | 
|  5030  |  | 
|  5031   if( pFile->locktype!=NO_LOCK ){ |  | 
|  5032     return SQLITE_BUSY; |  | 
|  5033   }   |  | 
|  5034  |  | 
|  5035   /* nothing to do if the path is NULL, :auto: or matches the existing path */ |  | 
|  5036   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |  | 
|  5037     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |  | 
|  5038     return SQLITE_OK; |  | 
|  5039   }else{ |  | 
|  5040     unixFile *lockProxy = pCtx->lockProxy; |  | 
|  5041     pCtx->lockProxy=NULL; |  | 
|  5042     pCtx->conchHeld = 0; |  | 
|  5043     if( lockProxy!=NULL ){ |  | 
|  5044       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |  | 
|  5045       if( rc ) return rc; |  | 
|  5046       sqlite3_free(lockProxy); |  | 
|  5047     } |  | 
|  5048     sqlite3_free(oldPath); |  | 
|  5049     pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |  | 
|  5050   } |  | 
|  5051    |  | 
|  5052   return rc; |  | 
|  5053 } |  | 
|  5054  |  | 
|  5055 /* |  | 
|  5056 ** pFile is a file that has been opened by a prior xOpen call.  dbPath |  | 
|  5057 ** is a string buffer at least MAXPATHLEN+1 characters in size. |  | 
|  5058 ** |  | 
|  5059 ** This routine find the filename associated with pFile and writes it |  | 
|  5060 ** int dbPath. |  | 
|  5061 */ |  | 
|  5062 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |  | 
|  5063 #if defined(__APPLE__) |  | 
|  5064   if( pFile->pMethod == &afpIoMethods ){ |  | 
|  5065     /* afp style keeps a reference to the db path in the filePath field  |  | 
|  5066     ** of the struct */ |  | 
|  5067     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |  | 
|  5068     strcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath); |  | 
|  5069   }else |  | 
|  5070 #endif |  | 
|  5071   if( pFile->pMethod == &dotlockIoMethods ){ |  | 
|  5072     /* dot lock style uses the locking context to store the dot lock |  | 
|  5073     ** file path */ |  | 
|  5074     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |  | 
|  5075     memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |  | 
|  5076   }else{ |  | 
|  5077     /* all other styles use the locking context to store the db file path */ |  | 
|  5078     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |  | 
|  5079     strcpy(dbPath, (char *)pFile->lockingContext); |  | 
|  5080   } |  | 
|  5081   return SQLITE_OK; |  | 
|  5082 } |  | 
|  5083  |  | 
|  5084 /* |  | 
|  5085 ** Takes an already filled in unix file and alters it so all file locking  |  | 
|  5086 ** will be performed on the local proxy lock file.  The following fields |  | 
|  5087 ** are preserved in the locking context so that they can be restored and  |  | 
|  5088 ** the unix structure properly cleaned up at close time: |  | 
|  5089 **  ->lockingContext |  | 
|  5090 **  ->pMethod |  | 
|  5091 */ |  | 
|  5092 static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |  | 
|  5093   proxyLockingContext *pCtx; |  | 
|  5094   char dbPath[MAXPATHLEN+1];       /* Name of the database file */ |  | 
|  5095   char *lockPath=NULL; |  | 
|  5096   int rc = SQLITE_OK; |  | 
|  5097    |  | 
|  5098   if( pFile->locktype!=NO_LOCK ){ |  | 
|  5099     return SQLITE_BUSY; |  | 
|  5100   } |  | 
|  5101   proxyGetDbPathForUnixFile(pFile, dbPath); |  | 
|  5102   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |  | 
|  5103     lockPath=NULL; |  | 
|  5104   }else{ |  | 
|  5105     lockPath=(char *)path; |  | 
|  5106   } |  | 
|  5107    |  | 
|  5108   OSTRACE4("TRANSPROXY  %d for %s pid=%d\n", pFile->h, |  | 
|  5109            (lockPath ? lockPath : ":auto:"), getpid()); |  | 
|  5110  |  | 
|  5111   pCtx = sqlite3_malloc( sizeof(*pCtx) ); |  | 
|  5112   if( pCtx==0 ){ |  | 
|  5113     return SQLITE_NOMEM; |  | 
|  5114   } |  | 
|  5115   memset(pCtx, 0, sizeof(*pCtx)); |  | 
|  5116  |  | 
|  5117   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |  | 
|  5118   if( rc==SQLITE_OK ){ |  | 
|  5119     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile); |  | 
|  5120   }   |  | 
|  5121   if( rc==SQLITE_OK && lockPath ){ |  | 
|  5122     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |  | 
|  5123   } |  | 
|  5124  |  | 
|  5125   if( rc==SQLITE_OK ){ |  | 
|  5126     /* all memory is allocated, proxys are created and assigned,  |  | 
|  5127     ** switch the locking context and pMethod then return. |  | 
|  5128     */ |  | 
|  5129     pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |  | 
|  5130     pCtx->oldLockingContext = pFile->lockingContext; |  | 
|  5131     pFile->lockingContext = pCtx; |  | 
|  5132     pCtx->pOldMethod = pFile->pMethod; |  | 
|  5133     pFile->pMethod = &proxyIoMethods; |  | 
|  5134   }else{ |  | 
|  5135     if( pCtx->conchFile ){  |  | 
|  5136       rc = pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |  | 
|  5137       if( rc ) return rc; |  | 
|  5138       sqlite3_free(pCtx->conchFile); |  | 
|  5139     } |  | 
|  5140     sqlite3_free(pCtx->conchFilePath);  |  | 
|  5141     sqlite3_free(pCtx); |  | 
|  5142   } |  | 
|  5143   OSTRACE3("TRANSPROXY  %d %s\n", pFile->h, |  | 
|  5144            (rc==SQLITE_OK ? "ok" : "failed")); |  | 
|  5145   return rc; |  | 
|  5146 } |  | 
|  5147  |  | 
|  5148  |  | 
|  5149 /* |  | 
|  5150 ** This routine handles sqlite3_file_control() calls that are specific |  | 
|  5151 ** to proxy locking. |  | 
|  5152 */ |  | 
|  5153 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |  | 
|  5154   switch( op ){ |  | 
|  5155     case SQLITE_GET_LOCKPROXYFILE: { |  | 
|  5156       unixFile *pFile = (unixFile*)id; |  | 
|  5157       if( pFile->pMethod == &proxyIoMethods ){ |  | 
|  5158         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |  | 
|  5159         proxyTakeConch(pFile); |  | 
|  5160         if( pCtx->lockProxyPath ){ |  | 
|  5161           *(const char **)pArg = pCtx->lockProxyPath; |  | 
|  5162         }else{ |  | 
|  5163           *(const char **)pArg = ":auto: (not held)"; |  | 
|  5164         } |  | 
|  5165       } else { |  | 
|  5166         *(const char **)pArg = NULL; |  | 
|  5167       } |  | 
|  5168       return SQLITE_OK; |  | 
|  5169     } |  | 
|  5170     case SQLITE_SET_LOCKPROXYFILE: { |  | 
|  5171       unixFile *pFile = (unixFile*)id; |  | 
|  5172       int rc = SQLITE_OK; |  | 
|  5173       int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |  | 
|  5174       if( pArg==NULL || (const char *)pArg==0 ){ |  | 
|  5175         if( isProxyStyle ){ |  | 
|  5176           /* turn off proxy locking - not supported */ |  | 
|  5177           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |  | 
|  5178         }else{ |  | 
|  5179           /* turn off proxy locking - already off - NOOP */ |  | 
|  5180           rc = SQLITE_OK; |  | 
|  5181         } |  | 
|  5182       }else{ |  | 
|  5183         const char *proxyPath = (const char *)pArg; |  | 
|  5184         if( isProxyStyle ){ |  | 
|  5185           proxyLockingContext *pCtx =  |  | 
|  5186             (proxyLockingContext*)pFile->lockingContext; |  | 
|  5187           if( !strcmp(pArg, ":auto:")  |  | 
|  5188            || (pCtx->lockProxyPath && |  | 
|  5189                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |  | 
|  5190           ){ |  | 
|  5191             rc = SQLITE_OK; |  | 
|  5192           }else{ |  | 
|  5193             rc = switchLockProxyPath(pFile, proxyPath); |  | 
|  5194           } |  | 
|  5195         }else{ |  | 
|  5196           /* turn on proxy file locking */ |  | 
|  5197           rc = proxyTransformUnixFile(pFile, proxyPath); |  | 
|  5198         } |  | 
|  5199       } |  | 
|  5200       return rc; |  | 
|  5201     } |  | 
|  5202     default: { |  | 
|  5203       assert( 0 );  /* The call assures that only valid opcodes are sent */ |  | 
|  5204     } |  | 
|  5205   } |  | 
|  5206   /*NOTREACHED*/ |  | 
|  5207   return SQLITE_ERROR; |  | 
|  5208 } |  | 
|  5209  |  | 
|  5210 /* |  | 
|  5211 ** Within this division (the proxying locking implementation) the procedures |  | 
|  5212 ** above this point are all utilities.  The lock-related methods of the |  | 
|  5213 ** proxy-locking sqlite3_io_method object follow. |  | 
|  5214 */ |  | 
|  5215  |  | 
|  5216  |  | 
|  5217 /* |  | 
|  5218 ** This routine checks if there is a RESERVED lock held on the specified |  | 
|  5219 ** file by this or any other process. If such a lock is held, set *pResOut |  | 
|  5220 ** to a non-zero value otherwise *pResOut is set to zero.  The return value |  | 
|  5221 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |  | 
|  5222 */ |  | 
|  5223 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |  | 
|  5224   unixFile *pFile = (unixFile*)id; |  | 
|  5225   int rc = proxyTakeConch(pFile); |  | 
|  5226   if( rc==SQLITE_OK ){ |  | 
|  5227     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |  | 
|  5228     unixFile *proxy = pCtx->lockProxy; |  | 
|  5229     return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |  | 
|  5230   } |  | 
|  5231   return rc; |  | 
|  5232 } |  | 
|  5233  |  | 
|  5234 /* |  | 
|  5235 ** Lock the file with the lock specified by parameter locktype - one |  | 
|  5236 ** of the following: |  | 
|  5237 ** |  | 
|  5238 **     (1) SHARED_LOCK |  | 
|  5239 **     (2) RESERVED_LOCK |  | 
|  5240 **     (3) PENDING_LOCK |  | 
|  5241 **     (4) EXCLUSIVE_LOCK |  | 
|  5242 ** |  | 
|  5243 ** Sometimes when requesting one lock state, additional lock states |  | 
|  5244 ** are inserted in between.  The locking might fail on one of the later |  | 
|  5245 ** transitions leaving the lock state different from what it started but |  | 
|  5246 ** still short of its goal.  The following chart shows the allowed |  | 
|  5247 ** transitions and the inserted intermediate states: |  | 
|  5248 ** |  | 
|  5249 **    UNLOCKED -> SHARED |  | 
|  5250 **    SHARED -> RESERVED |  | 
|  5251 **    SHARED -> (PENDING) -> EXCLUSIVE |  | 
|  5252 **    RESERVED -> (PENDING) -> EXCLUSIVE |  | 
|  5253 **    PENDING -> EXCLUSIVE |  | 
|  5254 ** |  | 
|  5255 ** This routine will only increase a lock.  Use the sqlite3OsUnlock() |  | 
|  5256 ** routine to lower a locking level. |  | 
|  5257 */ |  | 
|  5258 static int proxyLock(sqlite3_file *id, int locktype) { |  | 
|  5259   unixFile *pFile = (unixFile*)id; |  | 
|  5260   int rc = proxyTakeConch(pFile); |  | 
|  5261   if( rc==SQLITE_OK ){ |  | 
|  5262     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |  | 
|  5263     unixFile *proxy = pCtx->lockProxy; |  | 
|  5264     rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype); |  | 
|  5265     pFile->locktype = proxy->locktype; |  | 
|  5266   } |  | 
|  5267   return rc; |  | 
|  5268 } |  | 
|  5269  |  | 
|  5270  |  | 
|  5271 /* |  | 
|  5272 ** Lower the locking level on file descriptor pFile to locktype.  locktype |  | 
|  5273 ** must be either NO_LOCK or SHARED_LOCK. |  | 
|  5274 ** |  | 
|  5275 ** If the locking level of the file descriptor is already at or below |  | 
|  5276 ** the requested locking level, this routine is a no-op. |  | 
|  5277 */ |  | 
|  5278 static int proxyUnlock(sqlite3_file *id, int locktype) { |  | 
|  5279   unixFile *pFile = (unixFile*)id; |  | 
|  5280   int rc = proxyTakeConch(pFile); |  | 
|  5281   if( rc==SQLITE_OK ){ |  | 
|  5282     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |  | 
|  5283     unixFile *proxy = pCtx->lockProxy; |  | 
|  5284     rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype); |  | 
|  5285     pFile->locktype = proxy->locktype; |  | 
|  5286   } |  | 
|  5287   return rc; |  | 
|  5288 } |  | 
|  5289  |  | 
|  5290 /* |  | 
|  5291 ** Close a file that uses proxy locks. |  | 
|  5292 */ |  | 
|  5293 static int proxyClose(sqlite3_file *id) { |  | 
|  5294   if( id ){ |  | 
|  5295     unixFile *pFile = (unixFile*)id; |  | 
|  5296     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |  | 
|  5297     unixFile *lockProxy = pCtx->lockProxy; |  | 
|  5298     unixFile *conchFile = pCtx->conchFile; |  | 
|  5299     int rc = SQLITE_OK; |  | 
|  5300      |  | 
|  5301     if( lockProxy ){ |  | 
|  5302       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |  | 
|  5303       if( rc ) return rc; |  | 
|  5304       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |  | 
|  5305       if( rc ) return rc; |  | 
|  5306       sqlite3_free(lockProxy); |  | 
|  5307       pCtx->lockProxy = 0; |  | 
|  5308     } |  | 
|  5309     if( conchFile ){ |  | 
|  5310       if( pCtx->conchHeld ){ |  | 
|  5311         rc = proxyReleaseConch(pFile); |  | 
|  5312         if( rc ) return rc; |  | 
|  5313       } |  | 
|  5314       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |  | 
|  5315       if( rc ) return rc; |  | 
|  5316       sqlite3_free(conchFile); |  | 
|  5317     } |  | 
|  5318     sqlite3_free(pCtx->lockProxyPath); |  | 
|  5319     sqlite3_free(pCtx->conchFilePath); |  | 
|  5320     sqlite3_free(pCtx->dbPath); |  | 
|  5321     /* restore the original locking context and pMethod then close it */ |  | 
|  5322     pFile->lockingContext = pCtx->oldLockingContext; |  | 
|  5323     pFile->pMethod = pCtx->pOldMethod; |  | 
|  5324     sqlite3_free(pCtx); |  | 
|  5325     return pFile->pMethod->xClose(id); |  | 
|  5326   } |  | 
|  5327   return SQLITE_OK; |  | 
|  5328 } |  | 
|  5329  |  | 
|  5330  |  | 
|  5331  |  | 
|  5332 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |  | 
|  5333 /* |  | 
|  5334 ** The proxy locking style is intended for use with AFP filesystems. |  | 
|  5335 ** And since AFP is only supported on MacOSX, the proxy locking is also |  | 
|  5336 ** restricted to MacOSX. |  | 
|  5337 **  |  | 
|  5338 ** |  | 
|  5339 ******************* End of the proxy lock implementation ********************** |  | 
|  5340 ******************************************************************************/ |  | 
|  5341  |  | 
|  5342 /* |  | 
|  5343 ** Initialize the operating system interface. |  | 
|  5344 ** |  | 
|  5345 ** This routine registers all VFS implementations for unix-like operating |  | 
|  5346 ** systems.  This routine, and the sqlite3_os_end() routine that follows, |  | 
|  5347 ** should be the only routines in this file that are visible from other |  | 
|  5348 ** files. |  | 
|  5349 ** |  | 
|  5350 ** This routine is called once during SQLite initialization and by a |  | 
|  5351 ** single thread.  The memory allocation and mutex subsystems have not |  | 
|  5352 ** necessarily been initialized when this routine is called, and so they |  | 
|  5353 ** should not be used. |  | 
|  5354 */ |  | 
|  5355 int sqlite3_os_init(void){  |  | 
|  5356   /*  |  | 
|  5357   ** The following macro defines an initializer for an sqlite3_vfs object. |  | 
|  5358   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer |  | 
|  5359   ** to the "finder" function.  (pAppData is a pointer to a pointer because |  | 
|  5360   ** silly C90 rules prohibit a void* from being cast to a function pointer |  | 
|  5361   ** and so we have to go through the intermediate pointer to avoid problems |  | 
|  5362   ** when compiling with -pedantic-errors on GCC.) |  | 
|  5363   ** |  | 
|  5364   ** The FINDER parameter to this macro is the name of the pointer to the |  | 
|  5365   ** finder-function.  The finder-function returns a pointer to the |  | 
|  5366   ** sqlite_io_methods object that implements the desired locking |  | 
|  5367   ** behaviors.  See the division above that contains the IOMETHODS |  | 
|  5368   ** macro for addition information on finder-functions. |  | 
|  5369   ** |  | 
|  5370   ** Most finders simply return a pointer to a fixed sqlite3_io_methods |  | 
|  5371   ** object.  But the "autolockIoFinder" available on MacOSX does a little |  | 
|  5372   ** more than that; it looks at the filesystem type that hosts the  |  | 
|  5373   ** database file and tries to choose an locking method appropriate for |  | 
|  5374   ** that filesystem time. |  | 
|  5375   */ |  | 
|  5376   #define UNIXVFS(VFSNAME, FINDER) {                        \ |  | 
|  5377     1,                    /* iVersion */                    \ |  | 
|  5378     sizeof(unixFile),     /* szOsFile */                    \ |  | 
|  5379     MAX_PATHNAME,         /* mxPathname */                  \ |  | 
|  5380     0,                    /* pNext */                       \ |  | 
|  5381     VFSNAME,              /* zName */                       \ |  | 
|  5382     (void*)&FINDER,       /* pAppData */                    \ |  | 
|  5383     unixOpen,             /* xOpen */                       \ |  | 
|  5384     unixDelete,           /* xDelete */                     \ |  | 
|  5385     unixAccess,           /* xAccess */                     \ |  | 
|  5386     unixFullPathname,     /* xFullPathname */               \ |  | 
|  5387     unixDlOpen,           /* xDlOpen */                     \ |  | 
|  5388     unixDlError,          /* xDlError */                    \ |  | 
|  5389     unixDlSym,            /* xDlSym */                      \ |  | 
|  5390     unixDlClose,          /* xDlClose */                    \ |  | 
|  5391     unixRandomness,       /* xRandomness */                 \ |  | 
|  5392     unixSleep,            /* xSleep */                      \ |  | 
|  5393     unixCurrentTime,      /* xCurrentTime */                \ |  | 
|  5394     unixGetLastError      /* xGetLastError */               \ |  | 
|  5395   } |  | 
|  5396  |  | 
|  5397   /* |  | 
|  5398   ** All default VFSes for unix are contained in the following array. |  | 
|  5399   ** |  | 
|  5400   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |  | 
|  5401   ** by the SQLite core when the VFS is registered.  So the following |  | 
|  5402   ** array cannot be const. |  | 
|  5403   */ |  | 
|  5404   static sqlite3_vfs aVfs[] = { |  | 
|  5405 #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |  | 
|  5406     UNIXVFS("unix",          autolockIoFinder ), |  | 
|  5407 #else |  | 
|  5408     UNIXVFS("unix",          posixIoFinder ), |  | 
|  5409 #endif |  | 
|  5410     UNIXVFS("unix-none",     nolockIoFinder ), |  | 
|  5411     UNIXVFS("unix-dotfile",  dotlockIoFinder ), |  | 
|  5412     UNIXVFS("unix-wfl",      posixWflIoFinder ), |  | 
|  5413 #if OS_VXWORKS |  | 
|  5414     UNIXVFS("unix-namedsem", semIoFinder ), |  | 
|  5415 #endif |  | 
|  5416 #if SQLITE_ENABLE_LOCKING_STYLE |  | 
|  5417     UNIXVFS("unix-posix",    posixIoFinder ), |  | 
|  5418 #if !OS_VXWORKS |  | 
|  5419     UNIXVFS("unix-flock",    flockIoFinder ), |  | 
|  5420 #endif |  | 
|  5421 #endif |  | 
|  5422 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |  | 
|  5423     UNIXVFS("unix-afp",      afpIoFinder ), |  | 
|  5424     UNIXVFS("unix-proxy",    proxyIoFinder ), |  | 
|  5425 #endif |  | 
|  5426   }; |  | 
|  5427   unsigned int i;          /* Loop counter */ |  | 
|  5428  |  | 
|  5429   /* Register all VFSes defined in the aVfs[] array */ |  | 
|  5430   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |  | 
|  5431     sqlite3_vfs_register(&aVfs[i], i==0); |  | 
|  5432   } |  | 
|  5433   return SQLITE_OK;  |  | 
|  5434 } |  | 
|  5435  |  | 
|  5436 /* |  | 
|  5437 ** Shutdown the operating system interface. |  | 
|  5438 ** |  | 
|  5439 ** Some operating systems might need to do some cleanup in this routine, |  | 
|  5440 ** to release dynamically allocated objects.  But not on unix. |  | 
|  5441 ** This routine is a no-op for unix. |  | 
|  5442 */ |  | 
|  5443 int sqlite3_os_end(void){  |  | 
|  5444   return SQLITE_OK;  |  | 
|  5445 } |  | 
|  5446   |  | 
|  5447 #endif /* SQLITE_OS_UNIX */ |  | 
| OLD | NEW |