/ Check-in [215d36ea]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Continue with the cleanup of os_unix.c. (CVS 5969)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 215d36ea89078036fb66b5154f054613b735dab3
User & Date: drh 2008-12-03 19:34:47
Context
2008-12-03
21:26
Make os_unix compile with SQLITE_ENABLE_PROXY_LOCKING (CVS 5970) check-in: 3efedac6 user: aswift tags: trunk
19:34
Continue with the cleanup of os_unix.c. (CVS 5969) check-in: 215d36ea user: drh tags: trunk
2008-11-29
22:49
Fully initialize the unused bytes of the buffer that will become the journal file header, in order to silence a complaint from valgrind. (CVS 5968) check-in: 2822cbb9 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/os_unix.c.

    20     20   ** skip locking all together.
    21     21   **
    22     22   ** This source file is organized into divisions where the logic for various
    23     23   ** subfunctions is contained within the appropriate division.  PLEASE
    24     24   ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
    25     25   ** in the correct division and should be clearly labeled.
    26     26   **
    27         -** The current set of divisions is as follows:
           27  +** The layout of divisions is as follows:
    28     28   **
    29     29   **   *  General-purpose declarations and utility functions.
    30     30   **   *  Unique file ID logic used by VxWorks.
    31     31   **   *  Various locking primitive implementations:
    32     32   **      + for Posix Advisory Locks
    33     33   **      + for no-op locks
    34     34   **      + for dot-file locks
................................................................................
    35     35   **      + for flock() locking
    36     36   **      + for named semaphore locks (VxWorks only)
    37     37   **      + for AFP filesystem locks (MacOSX only)
    38     38   **      + for proxy locks (MacOSX only)
    39     39   **   *  sqlite3_file methods not associated with locking.
    40     40   **   *  Definitions of sqlite3_io_methods objects for all locking
    41     41   **      methods plus "finder" functions for each locking method.
    42         -**   *  VFS method implementations.
           42  +**   *  sqlite3_vfs method implementations.
    43     43   **   *  Definitions of sqlite3_vfs objects for all locking methods
    44     44   **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
    45     45   **
    46         -** $Id: os_unix.c,v 1.224 2008/11/29 02:20:27 drh Exp $
           46  +** $Id: os_unix.c,v 1.225 2008/12/03 19:34:47 drh Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #if SQLITE_OS_UNIX              /* This file is used on unix only */
    50     50   
    51     51   /*
    52         -** This module implements the following locking styles:
           52  +** There are various methods for file locking used for concurrency
           53  +** control:
    53     54   **
    54     55   **   1. POSIX locking (the default),
    55     56   **   2. No locking,
    56     57   **   3. Dot-file locking,
    57     58   **   4. flock() locking,
    58     59   **   5. AFP locking (OSX only),
    59     60   **   6. Named POSIX semaphores (VXWorks only),
................................................................................
  1786   1787   /****************** End of the dot-file lock implementation *******************
  1787   1788   ******************************************************************************/
  1788   1789   
  1789   1790   /******************************************************************************
  1790   1791   ************************** Begin flock Locking ********************************
  1791   1792   **
  1792   1793   ** Use the flock() system call to do file locking.
         1794  +**
         1795  +** flock() locking is like dot-file locking in that the various
         1796  +** fine-grain locking levels supported by SQLite are collapsed into
         1797  +** a single exclusive lock.  In other words, SHARED, RESERVED, and
         1798  +** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
         1799  +** still works when you do this, but concurrency is reduced since
         1800  +** only a single process can be reading the database at a time.
  1793   1801   **
  1794   1802   ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
  1795   1803   ** compiling for VXWORKS.
  1796   1804   */
  1797   1805   #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
  1798   1806   
  1799         -/* flock-style reserved lock checking following the behavior of 
  1800         - ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
         1807  +/*
         1808  +** This routine checks if there is a RESERVED lock held on the specified
         1809  +** file by this or any other process. If such a lock is held, set *pResOut
         1810  +** to a non-zero value otherwise *pResOut is set to zero.  The return value
         1811  +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
         1812  +*/
  1801   1813   static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
  1802   1814     int rc = SQLITE_OK;
  1803   1815     int reserved = 0;
  1804   1816     unixFile *pFile = (unixFile*)id;
  1805   1817     
  1806   1818     SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  1807   1819     
................................................................................
  1847   1859       reserved=1;
  1848   1860     }
  1849   1861   #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
  1850   1862     *pResOut = reserved;
  1851   1863     return rc;
  1852   1864   }
  1853   1865   
         1866  +/*
         1867  +** Lock the file with the lock specified by parameter locktype - one
         1868  +** of the following:
         1869  +**
         1870  +**     (1) SHARED_LOCK
         1871  +**     (2) RESERVED_LOCK
         1872  +**     (3) PENDING_LOCK
         1873  +**     (4) EXCLUSIVE_LOCK
         1874  +**
         1875  +** Sometimes when requesting one lock state, additional lock states
         1876  +** are inserted in between.  The locking might fail on one of the later
         1877  +** transitions leaving the lock state different from what it started but
         1878  +** still short of its goal.  The following chart shows the allowed
         1879  +** transitions and the inserted intermediate states:
         1880  +**
         1881  +**    UNLOCKED -> SHARED
         1882  +**    SHARED -> RESERVED
         1883  +**    SHARED -> (PENDING) -> EXCLUSIVE
         1884  +**    RESERVED -> (PENDING) -> EXCLUSIVE
         1885  +**    PENDING -> EXCLUSIVE
         1886  +**
         1887  +** flock() only really support EXCLUSIVE locks.  We track intermediate
         1888  +** lock states in the sqlite3_file structure, but all locks SHARED or
         1889  +** above are really EXCLUSIVE locks and exclude all other processes from
         1890  +** access the file.
         1891  +**
         1892  +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
         1893  +** routine to lower a locking level.
         1894  +*/
  1854   1895   static int flockLock(sqlite3_file *id, int locktype) {
  1855   1896     int rc = SQLITE_OK;
  1856   1897     unixFile *pFile = (unixFile*)id;
  1857   1898   
  1858   1899     assert( pFile );
  1859   1900   
  1860   1901     /* if we already have a lock, it is exclusive.  
................................................................................
  1883   1924     if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
  1884   1925       rc = SQLITE_BUSY;
  1885   1926     }
  1886   1927   #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
  1887   1928     return rc;
  1888   1929   }
  1889   1930   
         1931  +
         1932  +/*
         1933  +** Lower the locking level on file descriptor pFile to locktype.  locktype
         1934  +** must be either NO_LOCK or SHARED_LOCK.
         1935  +**
         1936  +** If the locking level of the file descriptor is already at or below
         1937  +** the requested locking level, this routine is a no-op.
         1938  +*/
  1890   1939   static int flockUnlock(sqlite3_file *id, int locktype) {
  1891   1940     unixFile *pFile = (unixFile*)id;
  1892   1941     
  1893   1942     assert( pFile );
  1894   1943     OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
  1895   1944              pFile->locktype, getpid());
  1896   1945     assert( locktype<=SHARED_LOCK );
................................................................................
  1942   1991   /******************* End of the flock lock implementation *********************
  1943   1992   ******************************************************************************/
  1944   1993   
  1945   1994   /******************************************************************************
  1946   1995   ************************ Begin Named Semaphore Locking ************************
  1947   1996   **
  1948   1997   ** Named semaphore locking is only supported on VxWorks.
         1998  +**
         1999  +** Semaphore locking is like dot-lock and flock in that it really only
         2000  +** supports EXCLUSIVE locking.  Only a single process can read or write
         2001  +** the database file at a time.  This reduces potential concurrency, but
         2002  +** makes the lock implementation much easier.
  1949   2003   */
  1950   2004   #if OS_VXWORKS
  1951   2005   
  1952         -/* Namedsem-style reserved lock checking following the behavior of 
  1953         -** unixCheckReservedLock, see the unixCheckReservedLock function comments */
         2006  +/*
         2007  +** This routine checks if there is a RESERVED lock held on the specified
         2008  +** file by this or any other process. If such a lock is held, set *pResOut
         2009  +** to a non-zero value otherwise *pResOut is set to zero.  The return value
         2010  +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
         2011  +*/
  1954   2012   static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
  1955   2013     int rc = SQLITE_OK;
  1956   2014     int reserved = 0;
  1957   2015     unixFile *pFile = (unixFile*)id;
  1958   2016   
  1959   2017     SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  1960   2018     
................................................................................
  1986   2044     }
  1987   2045     OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
  1988   2046   
  1989   2047     *pResOut = reserved;
  1990   2048     return rc;
  1991   2049   }
  1992   2050   
         2051  +/*
         2052  +** Lock the file with the lock specified by parameter locktype - one
         2053  +** of the following:
         2054  +**
         2055  +**     (1) SHARED_LOCK
         2056  +**     (2) RESERVED_LOCK
         2057  +**     (3) PENDING_LOCK
         2058  +**     (4) EXCLUSIVE_LOCK
         2059  +**
         2060  +** Sometimes when requesting one lock state, additional lock states
         2061  +** are inserted in between.  The locking might fail on one of the later
         2062  +** transitions leaving the lock state different from what it started but
         2063  +** still short of its goal.  The following chart shows the allowed
         2064  +** transitions and the inserted intermediate states:
         2065  +**
         2066  +**    UNLOCKED -> SHARED
         2067  +**    SHARED -> RESERVED
         2068  +**    SHARED -> (PENDING) -> EXCLUSIVE
         2069  +**    RESERVED -> (PENDING) -> EXCLUSIVE
         2070  +**    PENDING -> EXCLUSIVE
         2071  +**
         2072  +** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
         2073  +** lock states in the sqlite3_file structure, but all locks SHARED or
         2074  +** above are really EXCLUSIVE locks and exclude all other processes from
         2075  +** access the file.
         2076  +**
         2077  +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
         2078  +** routine to lower a locking level.
         2079  +*/
  1993   2080   static int semLock(sqlite3_file *id, int locktype) {
  1994   2081     unixFile *pFile = (unixFile*)id;
  1995   2082     int fd;
  1996   2083     sem_t *pSem = pFile->pOpen->pSem;
  1997   2084     int rc = SQLITE_OK;
  1998   2085   
  1999   2086     /* if we already have a lock, it is exclusive.  
................................................................................
  2013   2100     /* got it, set the type and return ok */
  2014   2101     pFile->locktype = locktype;
  2015   2102   
  2016   2103    sem_end_lock:
  2017   2104     return rc;
  2018   2105   }
  2019   2106   
         2107  +/*
         2108  +** Lower the locking level on file descriptor pFile to locktype.  locktype
         2109  +** must be either NO_LOCK or SHARED_LOCK.
         2110  +**
         2111  +** If the locking level of the file descriptor is already at or below
         2112  +** the requested locking level, this routine is a no-op.
         2113  +*/
  2020   2114   static int semUnlock(sqlite3_file *id, int locktype) {
  2021   2115     unixFile *pFile = (unixFile*)id;
  2022   2116     sem_t *pSem = pFile->pOpen->pSem;
  2023   2117   
  2024   2118     assert( pFile );
  2025   2119     assert( pSem );
  2026   2120     OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
................................................................................
  2089   2183   #if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
  2090   2184   /*
  2091   2185   ** The afpLockingContext structure contains all afp lock specific state
  2092   2186   */
  2093   2187   typedef struct afpLockingContext afpLockingContext;
  2094   2188   struct afpLockingContext {
  2095   2189     unsigned long long sharedByte;
  2096         -  const char *dbPath;
         2190  +  const char *dbPath;             /* Name of the open file */
  2097   2191   };
  2098   2192   
  2099   2193   struct ByteRangeLockPB2
  2100   2194   {
  2101   2195     unsigned long long offset;        /* offset to first byte to lock */
  2102   2196     unsigned long long length;        /* nbr of bytes to lock */
  2103   2197     unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
................................................................................
  2104   2198     unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
  2105   2199     unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
  2106   2200     int fd;                           /* file desc to assoc this lock with */
  2107   2201   };
  2108   2202   
  2109   2203   #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
  2110   2204   
  2111         -/* 
  2112         - ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
  2113         - */
  2114         -static int _AFPFSSetLock(
  2115         -  const char *path, 
  2116         -  unixFile *pFile, 
  2117         -  unsigned long long offset, 
  2118         -  unsigned long long length, 
  2119         -  int setLockFlag
         2205  +/*
         2206  +** This is a utility for setting or clearing a bit-range lock on an
         2207  +** AFP filesystem.
         2208  +** 
         2209  +** Return SQLITE_OK on success, SQLITE_BUSY on failure.
         2210  +*/
         2211  +static int afpSetLock(
         2212  +  const char *path,              /* Name of the file to be locked or unlocked */
         2213  +  unixFile *pFile,               /* Open file descriptor on path */
         2214  +  unsigned long long offset,     /* First byte to be locked */
         2215  +  unsigned long long length,     /* Number of bytes to lock */
         2216  +  int setLockFlag                /* True to set lock.  False to clear lock */
  2120   2217   ){
  2121         -  struct ByteRangeLockPB2       pb;
  2122         -  int                     err;
         2218  +  struct ByteRangeLockPB2 pb;
         2219  +  int err;
  2123   2220     
  2124   2221     pb.unLockFlag = setLockFlag ? 0 : 1;
  2125   2222     pb.startEndFlag = 0;
  2126   2223     pb.offset = offset;
  2127   2224     pb.length = length; 
  2128   2225     pb.fd = pFile->h;
  2129   2226     //SimulateIOErrorBenign(1);
................................................................................
  2150   2247       }
  2151   2248       return rc;
  2152   2249     } else {
  2153   2250       return SQLITE_OK;
  2154   2251     }
  2155   2252   }
  2156   2253   
  2157         -/* AFP-style reserved lock checking following the behavior of 
  2158         -** unixCheckReservedLock, see the unixCheckReservedLock function comments */
         2254  +/*
         2255  +** This routine checks if there is a RESERVED lock held on the specified
         2256  +** file by this or any other process. If such a lock is held, set *pResOut
         2257  +** to a non-zero value otherwise *pResOut is set to zero.  The return value
         2258  +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
         2259  +*/
  2159   2260   static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
  2160   2261     int rc = SQLITE_OK;
  2161   2262     int reserved = 0;
  2162   2263     unixFile *pFile = (unixFile*)id;
  2163   2264     
  2164   2265     SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  2165   2266     
................................................................................
  2171   2272       reserved = 1;
  2172   2273     }
  2173   2274     
  2174   2275     /* Otherwise see if some other process holds it.
  2175   2276      */
  2176   2277     if( !reserved ){
  2177   2278       /* lock the RESERVED byte */
  2178         -    int lrc = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);  
         2279  +    int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);  
  2179   2280       if( SQLITE_OK==lrc ){
  2180   2281         /* if we succeeded in taking the reserved lock, unlock it to restore
  2181   2282         ** the original state */
  2182         -      lrc = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
         2283  +      lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
  2183   2284       } else {
  2184   2285         /* if we failed to get the lock then someone else must have it */
  2185   2286         reserved = 1;
  2186   2287       }
  2187   2288       if( IS_LOCK_ERROR(lrc) ){
  2188   2289         rc=lrc;
  2189   2290       }
................................................................................
  2191   2292     
  2192   2293     OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
  2193   2294     
  2194   2295     *pResOut = reserved;
  2195   2296     return rc;
  2196   2297   }
  2197   2298   
  2198         -/* AFP-style locking following the behavior of unixLock, see the unixLock 
  2199         -** function comments for details of lock management. */
         2299  +/*
         2300  +** Lock the file with the lock specified by parameter locktype - one
         2301  +** of the following:
         2302  +**
         2303  +**     (1) SHARED_LOCK
         2304  +**     (2) RESERVED_LOCK
         2305  +**     (3) PENDING_LOCK
         2306  +**     (4) EXCLUSIVE_LOCK
         2307  +**
         2308  +** Sometimes when requesting one lock state, additional lock states
         2309  +** are inserted in between.  The locking might fail on one of the later
         2310  +** transitions leaving the lock state different from what it started but
         2311  +** still short of its goal.  The following chart shows the allowed
         2312  +** transitions and the inserted intermediate states:
         2313  +**
         2314  +**    UNLOCKED -> SHARED
         2315  +**    SHARED -> RESERVED
         2316  +**    SHARED -> (PENDING) -> EXCLUSIVE
         2317  +**    RESERVED -> (PENDING) -> EXCLUSIVE
         2318  +**    PENDING -> EXCLUSIVE
         2319  +**
         2320  +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
         2321  +** routine to lower a locking level.
         2322  +*/
  2200   2323   static int afpLock(sqlite3_file *id, int locktype){
  2201   2324     int rc = SQLITE_OK;
  2202   2325     unixFile *pFile = (unixFile*)id;
  2203   2326     afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  2204   2327     
  2205   2328     assert( pFile );
  2206   2329     OSTRACE5("LOCK    %d %s was %s pid=%d\n", pFile->h,
................................................................................
  2238   2361     ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
  2239   2362     ** be released.
  2240   2363     */
  2241   2364     if( locktype==SHARED_LOCK 
  2242   2365         || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
  2243   2366     ){
  2244   2367       int failed;
  2245         -    failed = _AFPFSSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
         2368  +    failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
  2246   2369       if (failed) {
  2247   2370         rc = failed;
  2248   2371         goto afp_end_lock;
  2249   2372       }
  2250   2373     }
  2251   2374     
  2252   2375     /* If control gets to this point, then actually go ahead and make
................................................................................
  2255   2378     if( locktype==SHARED_LOCK ){
  2256   2379       int lk, lrc1, lrc2, lrc1Errno;
  2257   2380       
  2258   2381       /* Now get the read-lock SHARED_LOCK */
  2259   2382       /* note that the quality of the randomness doesn't matter that much */
  2260   2383       lk = random(); 
  2261   2384       context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
  2262         -    lrc1 = _AFPFSSetLock(context->dbPath, pFile, 
         2385  +    lrc1 = afpSetLock(context->dbPath, pFile, 
  2263   2386             SHARED_FIRST+context->sharedByte, 1, 1);
  2264   2387       if( IS_LOCK_ERROR(lrc1) ){
  2265   2388         lrc1Errno = pFile->lastErrno;
  2266   2389       }
  2267   2390       /* Drop the temporary PENDING lock */
  2268         -    lrc2 = _AFPFSSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
         2391  +    lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
  2269   2392       
  2270   2393       if( IS_LOCK_ERROR(lrc1) ) {
  2271   2394         pFile->lastErrno = lrc1Errno;
  2272   2395         rc = lrc1;
  2273   2396         goto afp_end_lock;
  2274   2397       } else if( IS_LOCK_ERROR(lrc2) ){
  2275   2398         rc = lrc2;
................................................................................
  2285   2408       ** assumed that there is a SHARED or greater lock on the file
  2286   2409       ** already.
  2287   2410       */
  2288   2411       int failed = 0;
  2289   2412       assert( 0!=pFile->locktype );
  2290   2413       if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
  2291   2414           /* Acquire a RESERVED lock */
  2292         -        failed = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
         2415  +        failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
  2293   2416       }
  2294   2417       if (!failed && locktype == EXCLUSIVE_LOCK) {
  2295   2418         /* Acquire an EXCLUSIVE lock */
  2296   2419           
  2297   2420         /* Remove the shared lock before trying the range.  we'll need to 
  2298   2421         ** reestablish the shared lock if we can't get the  afpUnlock
  2299   2422         */
  2300         -      if( !(failed = _AFPFSSetLock(context->dbPath, pFile, SHARED_FIRST +
         2423  +      if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
  2301   2424                            context->sharedByte, 1, 0)) ){
  2302   2425           int failed2 = SQLITE_OK;
  2303   2426           /* now attemmpt to get the exclusive lock range */
  2304         -        failed = _AFPFSSetLock(context->dbPath, pFile, SHARED_FIRST, 
         2427  +        failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, 
  2305   2428                                  SHARED_SIZE, 1);
  2306         -        if( failed && (failed2 = _AFPFSSetLock(context->dbPath, pFile, 
         2429  +        if( failed && (failed2 = afpSetLock(context->dbPath, pFile, 
  2307   2430                          SHARED_FIRST + context->sharedByte, 1, 1)) ){
  2308   2431             /* Can't reestablish the shared lock.  Sqlite can't deal, this is
  2309   2432             ** a critical I/O error
  2310   2433             */
  2311   2434             rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : 
  2312   2435                  SQLITE_IOERR_LOCK;
  2313   2436             goto afp_end_lock;
................................................................................
  2357   2480     if( CHECK_THREADID(pFile) ){
  2358   2481       return SQLITE_MISUSE;
  2359   2482     }
  2360   2483     unixEnterMutex();
  2361   2484     if( pFile->locktype>SHARED_LOCK ){
  2362   2485       
  2363   2486       if( pFile->locktype==EXCLUSIVE_LOCK ){
  2364         -      rc = _AFPFSSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
         2487  +      rc = afpSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
  2365   2488         if( rc==SQLITE_OK && locktype==SHARED_LOCK ){
  2366   2489           /* only re-establish the shared lock if necessary */
  2367   2490           int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
  2368         -        rc = _AFPFSSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1);
         2491  +        rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1);
  2369   2492         }
  2370   2493       }
  2371   2494       if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
  2372         -      rc = _AFPFSSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0);
         2495  +      rc = afpSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0);
  2373   2496       } 
  2374   2497       if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){
  2375         -      rc = _AFPFSSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0);
         2498  +      rc = afpSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0);
  2376   2499       }
  2377   2500     }else if( locktype==NO_LOCK ){
  2378   2501       /* clear the shared lock */
  2379   2502       int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
  2380         -    rc = _AFPFSSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0);
         2503  +    rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0);
  2381   2504     }
  2382   2505   
  2383   2506     if( rc==SQLITE_OK ){
  2384   2507       if( locktype==NO_LOCK ){
  2385   2508         struct unixOpenCnt *pOpen = pFile->pOpen;
  2386   2509         pOpen->nLock--;
  2387   2510         assert( pOpen->nLock>=0 );
................................................................................
  2604   2727   */
  2605   2728   
  2606   2729   /*
  2607   2730   ** Proxy locking is only available on MacOSX 
  2608   2731   */
  2609   2732   #if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
  2610   2733   
  2611         -
  2612         -static int getDbPathForUnixFile(unixFile *pFile, char *dbPath);
  2613         -static int getLockPath(const char *dbPath, char *lPath, size_t maxLen);
  2614         -static int createProxyUnixFile(const char *path, unixFile **ppFile);
  2615         -static int fillInUnixFile(sqlite3_vfs *pVfs, int h, int dirfd, sqlite3_file *pId, const char *zFilename, int noLock, int isDelete);
  2616         -static int takeConch(unixFile *pFile);
  2617         -static int releaseConch(unixFile *pFile);
  2618         -static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf);
         2734  +/*
         2735  +** Forward reference
         2736  +*/
         2737  +static int fillInUnixFile(
         2738  +  sqlite3_vfs *pVfs,
         2739  +  int h,
         2740  +  int dirfd,
         2741  +  sqlite3_file *pId,
         2742  +  const char *zFilename,
         2743  +  int noLock,
         2744  +  int isDelete
         2745  +);
  2619   2746   
  2620   2747   
  2621   2748   #ifdef SQLITE_TEST
  2622   2749   /* simulate multiple hosts by creating unique hostid file paths */
  2623   2750   int sqlite3_hostid_num = 0;
  2624   2751   #endif
  2625   2752   
  2626   2753   /*
  2627   2754   ** The proxyLockingContext has the path and file structures for the remote 
  2628   2755   ** and local proxy files in it
  2629   2756   */
  2630   2757   typedef struct proxyLockingContext proxyLockingContext;
  2631   2758   struct proxyLockingContext {
  2632         -  unixFile *conchFile;
  2633         -  char *conchFilePath;
  2634         -  unixFile *lockProxy;
  2635         -  char *lockProxyPath;
  2636         -  char *dbPath;
  2637         -  int conchHeld;
  2638         -  void *oldLockingContext; /* preserve the original locking context for close */
  2639         -  sqlite3_io_methods const *pOldMethod; /* ditto pMethod */
  2640         -};
  2641         -
  2642         -
  2643         -static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
  2644         -  unixFile *pFile = (unixFile*)id;
  2645         -  int rc = takeConch(pFile);
  2646         -  if( rc==SQLITE_OK ){
  2647         -    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
  2648         -    unixFile *proxy = pCtx->lockProxy;
  2649         -    return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
  2650         -  }
  2651         -  return rc;
  2652         -}
  2653         -
  2654         -static int proxyLock(sqlite3_file *id, int locktype) {
  2655         -  unixFile *pFile = (unixFile*)id;
  2656         -  int rc = takeConch(pFile);
  2657         -  if( rc==SQLITE_OK ){
  2658         -    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
  2659         -    unixFile *proxy = pCtx->lockProxy;
  2660         -    rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
  2661         -    pFile->locktype = proxy->locktype;
  2662         -  }
  2663         -  return rc;
  2664         -}
  2665         -
  2666         -static int proxyUnlock(sqlite3_file *id, int locktype) {
  2667         -  unixFile *pFile = (unixFile*)id;
  2668         -  int rc = takeConch(pFile);
  2669         -  if( rc==SQLITE_OK ){
  2670         -    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
  2671         -    unixFile *proxy = pCtx->lockProxy;
  2672         -    rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
  2673         -    pFile->locktype = proxy->locktype;
  2674         -  }
  2675         -  return rc;
  2676         -}
  2677         -
  2678         -/*
  2679         - ** Close a file.
  2680         - */
  2681         -static int proxyClose(sqlite3_file *id) {
  2682         -  if( id ){
  2683         -    unixFile *pFile = (unixFile*)id;
  2684         -    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
  2685         -    unixFile *lockProxy = pCtx->lockProxy;
  2686         -    unixFile *conchFile = pCtx->conchFile;
  2687         -    int rc = SQLITE_OK;
  2688         -    
  2689         -    if( lockProxy ){
  2690         -      rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
  2691         -      if( rc ) return rc;
  2692         -      rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
  2693         -      if( rc ) return rc;
  2694         -      sqlite3_free(lockProxy);
  2695         -    }
  2696         -    if( conchFile ){
  2697         -      if( pCtx->conchHeld ){
  2698         -        rc = releaseConch(pFile);
  2699         -        if( rc ) return rc;
  2700         -      }
  2701         -      rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
  2702         -      if( rc ) return rc;
  2703         -      sqlite3_free(conchFile);
  2704         -    }
  2705         -    sqlite3_free(pCtx->lockProxyPath);
  2706         -    sqlite3_free(pCtx->conchFilePath);
  2707         -    sqlite3_free(pCtx->dbPath);
  2708         -    /* restore the original locking context and pMethod then close it */
  2709         -    pFile->lockingContext = pCtx->oldLockingContext;
  2710         -    pFile->pMethod = pCtx->pOldMethod;
  2711         -    sqlite3_free(pCtx);
  2712         -    return pFile->pMethod->xClose(id);
  2713         -  }
  2714         -  return SQLITE_OK;
  2715         -}
         2759  +  unixFile *conchFile;         /* Open conch file */
         2760  +  char *conchFilePath;         /* Name of the conch file */
         2761  +  unixFile *lockProxy;         /* Open proxy lock file */
         2762  +  char *lockProxyPath;         /* Name of the proxy lock file */
         2763  +  char *dbPath;                /* Name of the open file */
         2764  +  int conchHeld;               /* True if the conch is currently held */
         2765  +  void *oldLockingContext;     /* Original lockingcontext to restore on close */
         2766  +  sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
         2767  +};
  2716   2768   
  2717   2769   /* HOSTIDLEN and CONCHLEN both include space for the string 
  2718   2770   ** terminating nul 
  2719   2771   */
  2720   2772   #define HOSTIDLEN         128
  2721   2773   #define CONCHLEN          (MAXPATHLEN+HOSTIDLEN+1)
  2722   2774   #ifndef HOSTIDPATH
  2723   2775   # define HOSTIDPATH       "/Library/Caches/.com.apple.sqliteConchHostId"
  2724   2776   #endif
  2725   2777   
  2726   2778   /* basically a copy of unixRandomness with different
  2727   2779   ** test behavior built in */
  2728         -static int genHostID(char *pHostID){
         2780  +static int proxyGenerateHostID(char *pHostID){
  2729   2781     int pid, fd, i, len;
  2730   2782     unsigned char *key = (unsigned char *)pHostID;
  2731   2783     
  2732   2784     memset(key, 0, HOSTIDLEN);
  2733   2785     len = 0;
  2734   2786     fd = open("/dev/urandom", O_RDONLY);
  2735   2787     if( fd>=0 ){
................................................................................
  2756   2808       }
  2757   2809     }
  2758   2810   #endif
  2759   2811     return SQLITE_OK;
  2760   2812   }
  2761   2813   
  2762   2814   /* writes the host id path to path, path should be an pre-allocated buffer
  2763         -** with enough space for a path */
  2764         -static int getHostIDPath(char *path, size_t len){
         2815  +** with enough space for a path 
         2816  +*/
         2817  +static int proxyGetHostIDPath(char *path, size_t len){
  2765   2818     strlcpy(path, HOSTIDPATH, len);
  2766   2819   #ifdef SQLITE_TEST
  2767   2820     if( sqlite3_hostid_num>0 ){
  2768   2821       char suffix[2] = "1";
  2769   2822       suffix[0] = suffix[0] + sqlite3_hostid_num;
  2770   2823       strlcat(path, suffix, len);
  2771   2824     }
................................................................................
  2772   2825   #endif
  2773   2826     OSTRACE3("GETHOSTIDPATH  %s pid=%d\n", path, getpid());
  2774   2827   }
  2775   2828   
  2776   2829   /* get the host ID from a sqlite hostid file stored in the 
  2777   2830   ** user-specific tmp directory, create the ID if it's not there already 
  2778   2831   */
  2779         -static int getHostID(char *pHostID, int *pError){
         2832  +static int proxyGetHostID(char *pHostID, int *pError){
  2780   2833     int fd;
  2781   2834     char path[MAXPATHLEN]; 
  2782   2835     size_t len;
  2783         -	int rc=SQLITE_OK;
         2836  +  int rc=SQLITE_OK;
  2784   2837   
  2785         -  getHostIDPath(path, MAXPATHLEN);
         2838  +  proxyGetHostIDPath(path, MAXPATHLEN);
  2786   2839     /* try to create the host ID file, if it already exists read the contents */
  2787   2840     fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644);
  2788   2841     if( fd<0 ){
  2789   2842       int err=errno;
  2790   2843   		
  2791   2844       if( err!=EEXIST ){
  2792   2845   #ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
................................................................................
  2815   2868       }
  2816   2869       close(fd); /* silently leak the fd if it fails */
  2817   2870       OSTRACE3("GETHOSTID  read %s pid=%d\n", pHostID, getpid());
  2818   2871       return rc;
  2819   2872     }else{
  2820   2873       int i;
  2821   2874       /* we're creating the host ID file (use a random string of bytes) */
  2822         -    genHostID(pHostID);
         2875  +    proxyGenerateHostID(pHostID);
  2823   2876       len = pwrite(fd, pHostID, HOSTIDLEN, 0);
  2824   2877       if( len<0 ){
  2825   2878         *pError = errno;
  2826   2879         rc = SQLITE_IOERR_WRITE;
  2827   2880       }else if( len<HOSTIDLEN ){
  2828   2881         *pError = 0;
  2829   2882         rc = SQLITE_IOERR_WRITE;
  2830   2883       }
  2831   2884       close(fd); /* silently leak the fd if it fails */
  2832   2885       OSTRACE3("GETHOSTID  wrote %s pid=%d\n", pHostID, getpid());
  2833   2886       return rc;
  2834   2887     }
  2835   2888   }
         2889  +
         2890  +static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
         2891  +  int len;
         2892  +  int dbLen;
         2893  +  int i;
         2894  +
         2895  +#ifdef LOCKPROXYDIR
         2896  +  len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
         2897  +#else
         2898  +# ifdef _CS_DARWIN_USER_TEMP_DIR
         2899  +  {
         2900  +    char utdir[MAXPATHLEN];
         2901  +    
         2902  +    confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen);
         2903  +    len = strlcat(lPath, "sqliteplocks", maxLen);
         2904  +    if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
         2905  +      /* if mkdir fails, handle as lock file creation failure */
         2906  +      int err = errno;
         2907  +#  ifdef SQLITE_DEBUG
         2908  +      if( err!=EEXIST ){
         2909  +        fprintf(stderr, "proxyGetLockPath: mkdir(%s,0%o) error %d %s\n", lPath,
         2910  +                SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err));
         2911  +      }
         2912  +#  endif
         2913  +    }else{
         2914  +      OSTRACE3("GETLOCKPATH  mkdir %s pid=%d\n", lPath, getpid());
         2915  +    }
         2916  +    
         2917  +  }
         2918  +# else
         2919  +  len = strlcpy(lPath, "/tmp/", maxLen);
         2920  +# endif
         2921  +#endif
         2922  +
         2923  +  if( lPath[len-1]!='/' ){
         2924  +    len = strlcat(lPath, "/", maxLen);
         2925  +  }
         2926  +  
         2927  +  /* transform the db path to a unique cache name */
         2928  +  dbLen = strlen(dbPath);
         2929  +  for( i=0; i<dbLen && (i+len+7)<maxLen; i++){
         2930  +    char c = dbPath[i];
         2931  +    lPath[i+len] = (c=='/')?'_':c;
         2932  +  }
         2933  +  lPath[i+len]='\0';
         2934  +  strlcat(lPath, ":auto:", maxLen);
         2935  +  return SQLITE_OK;
         2936  +}
         2937  +
         2938  +/*
         2939  +** Create a new VFS file descriptor (stored in memory obtained from
         2940  +** sqlite3_malloc) and open the file named "path" in the file descriptor.
         2941  +**
         2942  +** The caller is responsible not only for closing the file descriptor
         2943  +** but also for freeing the memory associated with the file descriptor.
         2944  +*/
         2945  +static int proxyCreateUnixFile(const char *path, unixFile **ppFile) {
         2946  +  int fd;
         2947  +  int dirfd = -1;
         2948  +  unixFile *pNew;
         2949  +  int rc = SQLITE_OK;
         2950  +
         2951  +  fd = open(path, O_RDWR | O_CREAT, SQLITE_DEFAULT_FILE_PERMISSIONS);
         2952  +  if( fd<0 ){
         2953  +    return SQLITE_CANTOPEN;
         2954  +  }
         2955  +  
         2956  +  pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile));
         2957  +  if( pNew==NULL ){
         2958  +    rc = SQLITE_NOMEM;
         2959  +    goto end_create_proxy;
         2960  +  }
         2961  +  memset(pNew, 0, sizeof(unixFile));
         2962  +  
         2963  +  rc = fillInUnixFile(NULL, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0);
         2964  +  if( rc==SQLITE_OK ){
         2965  +    *ppFile = pNew;
         2966  +    return SQLITE_OK;
         2967  +  }
         2968  +end_create_proxy:    
         2969  +  close(fd); /* silently leak fd if error, we're already in error */
         2970  +  sqlite3_free(pNew);
         2971  +  return rc;
         2972  +}
  2836   2973   
  2837   2974   /* takes the conch by taking a shared lock and read the contents conch, if 
  2838   2975   ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL 
  2839   2976   ** lockPath means that the lockPath in the conch file will be used if the 
  2840   2977   ** host IDs match, or a new lock path will be generated automatically 
  2841   2978   ** and written to the conch file.
  2842   2979   */
  2843         -static int takeConch(unixFile *pFile){
         2980  +static int proxyTakeConch(unixFile *pFile){
  2844   2981     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
  2845   2982     
  2846   2983     if( pCtx->conchHeld>0 ){
  2847   2984       return SQLITE_OK;
  2848   2985     }else{
  2849   2986       unixFile *conchFile = pCtx->conchFile;
  2850   2987       char testValue[CONCHLEN];
................................................................................
  2856   2993       int syncPerms = 0;
  2857   2994   
  2858   2995       OSTRACE4("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
  2859   2996                (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
  2860   2997   
  2861   2998       rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
  2862   2999       if( rc==SQLITE_OK ){
  2863         -			int pError = 0;
         3000  +      int pError = 0;
  2864   3001         memset(testValue, 0, CONCHLEN); // conch is fixed size
  2865         -      rc = getHostID(testValue, &pError);
  2866         -			if( rc&SQLITE_IOERR==SQLITE_IOERR ){
  2867         -				pFile->lastErrno = pError;
  2868         -			}
         3002  +      rc = proxyGetHostID(testValue, &pError);
         3003  +      if( rc&SQLITE_IOERR==SQLITE_IOERR ){
         3004  +        pFile->lastErrno = pError;
         3005  +      }
  2869   3006         if( pCtx->lockProxyPath ){
  2870   3007           strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN);
  2871   3008         }
  2872   3009       }
  2873   3010       if( rc!=SQLITE_OK ){
  2874   3011         goto end_takeconch;
  2875   3012       }
................................................................................
  2904   3041         ** we'll try to match the current on-disk permissions of the database
  2905   3042         */
  2906   3043         syncPerms = 1;
  2907   3044       }
  2908   3045       
  2909   3046       /* either conch was emtpy or didn't match */
  2910   3047       if( !pCtx->lockProxyPath ){
  2911         -      getLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
         3048  +      proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
  2912   3049         tLockPath = lockPath;
  2913   3050         strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN);
  2914   3051       }
  2915   3052       
  2916   3053       /* update conch with host and path (this will fail if other process
  2917   3054        ** has a shared lock already) */
  2918   3055       rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
................................................................................
  2960   3097         pFile->h = -1;
  2961   3098         int fd = open(pCtx->dbPath, pFile->openFlags,
  2962   3099                       SQLITE_DEFAULT_FILE_PERMISSIONS);
  2963   3100         OSTRACE2("TRANSPROXY: OPEN  %d\n", fd);
  2964   3101         if( fd>=0 ){
  2965   3102           pFile->h = fd;
  2966   3103         }else{
  2967         -        rc=SQLITE_CANTOPEN; // SQLITE_BUSY? takeConch called during locking
         3104  +        rc=SQLITE_CANTOPEN; // SQLITE_BUSY? proxyTakeConch called during locking
  2968   3105         }
  2969   3106       }
  2970   3107       if( rc==SQLITE_OK && !pCtx->lockProxy ){
  2971   3108         char *path = tLockPath ? tLockPath : pCtx->lockProxyPath;
  2972   3109         // ACS: Need to make a copy of path sometimes
  2973         -      rc = createProxyUnixFile(path, &pCtx->lockProxy);
         3110  +      rc = proxyCreateUnixFile(path, &pCtx->lockProxy);
  2974   3111       }
  2975   3112       if( rc==SQLITE_OK ){
  2976   3113         pCtx->conchHeld = 1;
  2977   3114   
  2978   3115         if( tLockPath ){
  2979   3116           pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath);
  2980   3117           if( pCtx->lockProxy->pMethod == &afpIoMethods ){
................................................................................
  2985   3122       } else {
  2986   3123         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
  2987   3124       }
  2988   3125       OSTRACE3("TAKECONCH  %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed");
  2989   3126       return rc;
  2990   3127     }
  2991   3128   }
  2992         -  
  2993         -static int releaseConch(unixFile *pFile){
  2994         -  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
  2995         -  int rc;
  2996         -  unixFile *conchFile = pCtx->conchFile;
  2997   3129   
         3130  +/*
         3131  +** If pFile holds a lock on a conch file, then release that lock.
         3132  +*/
         3133  +static int proxyReleaseConch(unixFile *pFile){
         3134  +  int rc;                     /* Subroutine return code */
         3135  +  proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
         3136  +  unixFile *conchFile;        /* Name of the conch file */
         3137  +
         3138  +  pCtx = (proxyLockingContext *)pFile->lockingContext;
         3139  +  conchFile = pCtx->conchFile;
  2998   3140     OSTRACE4("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
  2999   3141              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), 
  3000   3142              getpid());
  3001   3143     pCtx->conchHeld = 0;
  3002   3144     rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
  3003   3145     OSTRACE3("RELEASECONCH  %d %s\n", conchFile->h,
  3004   3146              (rc==SQLITE_OK ? "ok" : "failed"));
  3005   3147     return rc;
  3006   3148   }
  3007   3149   
  3008         -static int getConchPathFromDBPath(char *dbPath, char **pConchPath){
  3009         -  int i;
  3010         -  int len = strlen(dbPath);
  3011         -  char *conchPath;
  3012         -  
  3013         -  conchPath = (char *)sqlite3_malloc(len + 8);
         3150  +/*
         3151  +** Given the name of a database file, compute the name of its conch file.
         3152  +** Store the conch filename in memory obtained from sqlite3_malloc().
         3153  +** Make *pConchPath point to the new name.  Return SQLITE_OK on success
         3154  +** or SQLITE_NOMEM if unable to obtain memory.
         3155  +**
         3156  +** The caller is responsible for ensuring that the allocated memory
         3157  +** space is eventually freed.
         3158  +**
         3159  +** *pConchPath is set to NULL if a memory allocation error occurs.
         3160  +*/
         3161  +static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
         3162  +  int i;                        /* Loop counter */
         3163  +  int len = strlen(dbPath);     /* Length of database filename - dbPath */
         3164  +  char *conchPath;              /* buffer in which to construct conch name */
         3165  +
         3166  +  /* Allocate space for the conch filename and initialize the name to
         3167  +  ** the name of the original database file. */  
         3168  +  *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
  3014   3169     if( conchPath==0 ){
  3015   3170       return SQLITE_NOMEM;
  3016   3171     }
  3017         -  strlcpy(conchPath, dbPath, len+1);
         3172  +  memcpy(conchPath, dbPath, len+1);
  3018   3173     
  3019   3174     /* now insert a "." before the last / character */
  3020   3175     for( i=(len-1); i>=0; i-- ){
  3021   3176       if( conchPath[i]=='/' ){
  3022   3177         i++;
  3023   3178         break;
  3024   3179       }
  3025   3180     }
  3026   3181     conchPath[i]='.';
  3027   3182     while ( i<len ){
  3028   3183       conchPath[i+1]=dbPath[i];
  3029   3184       i++;
  3030   3185     }
  3031         -  conchPath[i+1]='\0';
  3032         -  strlcat(conchPath, "-conch", len + 8);
  3033         -  *pConchPath = conchPath;
         3186  +
         3187  +  /* append the "-conch" suffix to the file */
         3188  +  memcpy(&conchPath[i+1], "-conch", 7);
         3189  +  assert( strlen(conchPath) == len+7 );
         3190  +
  3034   3191     return SQLITE_OK;
  3035   3192   }
  3036   3193   
  3037   3194   
  3038         -static int getLockPath(const char *dbPath, char *lPath, size_t maxLen){
  3039         -  int len;
  3040         -  int dbLen;
  3041         -  int i;
  3042         -
  3043         -#ifdef LOCKPROXYDIR
  3044         -  len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
  3045         -#else
  3046         -# ifdef _CS_DARWIN_USER_TEMP_DIR
  3047         -  {
  3048         -    char utdir[MAXPATHLEN];
  3049         -    
  3050         -    confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen);
  3051         -    len = strlcat(lPath, "sqliteplocks", maxLen);
  3052         -    if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
  3053         -      /* if mkdir fails, handle as lock file creation failure */
  3054         -      int err = errno;
  3055         -#  ifdef SQLITE_DEBUG
  3056         -      if( err!=EEXIST ){
  3057         -        fprintf(stderr, "getLockPath: mkdir(%s,0%o) error %d %s\n", lPath,
  3058         -                SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err));
  3059         -      }
  3060         -#  endif
  3061         -    }else{
  3062         -      OSTRACE3("GETLOCKPATH  mkdir %s pid=%d\n", lPath, getpid());
  3063         -    }
  3064         -    
  3065         -  }
  3066         -# else
  3067         -  len = strlcpy(lPath, "/tmp/", maxLen);
  3068         -# endif
  3069         -#endif
  3070         -
  3071         -  if( lPath[len-1]!='/' ){
  3072         -    len = strlcat(lPath, "/", maxLen);
  3073         -  }
  3074         -  
  3075         -  /* transform the db path to a unique cache name */
  3076         -  dbLen = strlen(dbPath);
  3077         -  for( i=0; i<dbLen && (i+len+7)<maxLen; i++){
  3078         -    char c = dbPath[i];
  3079         -    lPath[i+len] = (c=='/')?'_':c;
  3080         -  }
  3081         -  lPath[i+len]='\0';
  3082         -  strlcat(lPath, ":auto:", maxLen);
  3083         -  return SQLITE_OK;
  3084         -}
  3085         -
  3086   3195   /* Takes a fully configured proxy locking-style unix file and switches
  3087   3196   ** the local lock file path 
  3088   3197   */
  3089   3198   static int switchLockProxyPath(unixFile *pFile, const char *path) {
  3090   3199     proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
  3091   3200     char *oldPath = pCtx->lockProxyPath;
  3092   3201     int taken = 0;
................................................................................
  3111   3220       }
  3112   3221       sqlite3_free(oldPath);
  3113   3222       pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
  3114   3223     }
  3115   3224     
  3116   3225     return rc;
  3117   3226   }
         3227  +
         3228  +/*
         3229  +** pFile is a file that has been opened by a prior xOpen call.  dbPath
         3230  +** is a string buffer at least MAXPATHLEN+1 characters in size.
         3231  +**
         3232  +** This routine find the filename associated with pFile and writes it
         3233  +** int dbPath.
         3234  +*/
         3235  +static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
         3236  +#if defined(__DARWIN__)
         3237  +  if( pFile->pMethod == &afpIoMethods ){
         3238  +    /* afp style keeps a reference to the db path in the filePath field 
         3239  +    ** of the struct */
         3240  +    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
         3241  +    strcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath)
         3242  +  }else
         3243  +#endif
         3244  +  if( pFile->pMethod == &dotlockIoMethods ){
         3245  +    /* dot lock style uses the locking context to store the dot lock
         3246  +    ** file path */
         3247  +    int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
         3248  +    memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
         3249  +  }else{
         3250  +    /* all other styles use the locking context to store the db file path */
         3251  +    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
         3252  +    strcpy(dbPath, (char *)pFile->lockingContext);
         3253  +  }
         3254  +  return SQLITE_OK;
         3255  +}
  3118   3256   
  3119   3257   /*
  3120   3258   ** Takes an already filled in unix file and alters it so all file locking 
  3121   3259   ** will be performed on the local proxy lock file.  The following fields
  3122   3260   ** are preserved in the locking context so that they can be restored and 
  3123   3261   ** the unix structure properly cleaned up at close time:
  3124   3262   **  ->lockingContext
  3125   3263   **  ->pMethod
  3126   3264   */
  3127   3265   static int transformUnixFileForLockProxy(unixFile *pFile, const char *path) {
  3128   3266     proxyLockingContext *pCtx;
  3129         -  char dbPath[MAXPATHLEN];
         3267  +  char dbPath[MAXPATHLEN+1];       /* Name of the database file */
  3130   3268     char *lockPath=NULL;
  3131   3269     int rc = SQLITE_OK;
  3132   3270     
  3133   3271     if( pFile->locktype!=NO_LOCK ){
  3134   3272       return SQLITE_BUSY;
  3135   3273     }
  3136         -  getDbPathForUnixFile(pFile, dbPath);
         3274  +  proxyGetDbPathForUnixFile(pFile, dbPath);
  3137   3275     if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
  3138   3276       lockPath=NULL;
  3139   3277     }else{
  3140   3278       lockPath=(char *)path;
  3141   3279     }
  3142   3280     
  3143   3281     OSTRACE4("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
................................................................................
  3145   3283   
  3146   3284     pCtx = sqlite3_malloc( sizeof(*pCtx) );
  3147   3285     if( pCtx==0 ){
  3148   3286       return SQLITE_NOMEM;
  3149   3287     }
  3150   3288     memset(pCtx, 0, sizeof(*pCtx));
  3151   3289   
  3152         -  rc = getConchPathFromDBPath(dbPath, &pCtx->conchFilePath);
         3290  +  rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
  3153   3291     if( rc==SQLITE_OK ){
  3154         -    rc = createProxyUnixFile(pCtx->conchFilePath, &pCtx->conchFile);
         3292  +    rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile);
  3155   3293     }  
  3156   3294     if( rc==SQLITE_OK && lockPath ){
  3157   3295       pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
  3158   3296     }
  3159   3297   
  3160   3298   end_transform_file:
  3161   3299     if( rc==SQLITE_OK ){
................................................................................
  3175   3313       }
  3176   3314       sqlite3_free(pCtx->conchFilePath); 
  3177   3315       sqlite3_free(pCtx);
  3178   3316     }
  3179   3317     OSTRACE3("TRANSPROXY  %d %s\n", pFile->h,
  3180   3318              (rc==SQLITE_OK ? "ok" : "failed"));
  3181   3319     return rc;
  3182         -} 
  3183         -
  3184         -static int createProxyUnixFile(const char *path, unixFile **ppFile) {
  3185         -  int fd;
  3186         -  int dirfd = -1;
  3187         -  unixFile *pNew;
  3188         -  int rc = SQLITE_OK;
  3189         -
  3190         -  fd = open(path, O_RDWR | O_CREAT, SQLITE_DEFAULT_FILE_PERMISSIONS);
  3191         -  if( fd<0 ){
  3192         -    return SQLITE_CANTOPEN;
  3193         -  }
  3194         -  
  3195         -  pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile));
  3196         -  if( pNew==NULL ){
  3197         -    rc = SQLITE_NOMEM;
  3198         -    goto end_create_proxy;
  3199         -  }
  3200         -  memset(pNew, 0, sizeof(unixFile));
  3201         -  
  3202         -  rc = fillInUnixFile(NULL, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0);
         3320  +}
         3321  +
         3322  +/*
         3323  +** Within this division (the proxying locking implementation) the procedures
         3324  +** above this point are all utilities.  The lock-related methods of the
         3325  +** proxy-locking sqlite3_io_method object follow.
         3326  +*/
         3327  +
         3328  +
         3329  +/*
         3330  +** This routine checks if there is a RESERVED lock held on the specified
         3331  +** file by this or any other process. If such a lock is held, set *pResOut
         3332  +** to a non-zero value otherwise *pResOut is set to zero.  The return value
         3333  +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
         3334  +*/
         3335  +static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
         3336  +  unixFile *pFile = (unixFile*)id;
         3337  +  int rc = proxyTakeConch(pFile);
         3338  +  if( rc==SQLITE_OK ){
         3339  +    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
         3340  +    unixFile *proxy = pCtx->lockProxy;
         3341  +    return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
         3342  +  }
         3343  +  return rc;
         3344  +}
         3345  +
         3346  +/*
         3347  +** Lock the file with the lock specified by parameter locktype - one
         3348  +** of the following:
         3349  +**
         3350  +**     (1) SHARED_LOCK
         3351  +**     (2) RESERVED_LOCK
         3352  +**     (3) PENDING_LOCK
         3353  +**     (4) EXCLUSIVE_LOCK
         3354  +**
         3355  +** Sometimes when requesting one lock state, additional lock states
         3356  +** are inserted in between.  The locking might fail on one of the later
         3357  +** transitions leaving the lock state different from what it started but
         3358  +** still short of its goal.  The following chart shows the allowed
         3359  +** transitions and the inserted intermediate states:
         3360  +**
         3361  +**    UNLOCKED -> SHARED
         3362  +**    SHARED -> RESERVED
         3363  +**    SHARED -> (PENDING) -> EXCLUSIVE
         3364  +**    RESERVED -> (PENDING) -> EXCLUSIVE
         3365  +**    PENDING -> EXCLUSIVE
         3366  +**
         3367  +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
         3368  +** routine to lower a locking level.
         3369  +*/
         3370  +static int proxyLock(sqlite3_file *id, int locktype) {
         3371  +  unixFile *pFile = (unixFile*)id;
         3372  +  int rc = proxyTakeConch(pFile);
         3373  +  if( rc==SQLITE_OK ){
         3374  +    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
         3375  +    unixFile *proxy = pCtx->lockProxy;
         3376  +    rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
         3377  +    pFile->locktype = proxy->locktype;
         3378  +  }
         3379  +  return rc;
         3380  +}
         3381  +
         3382  +
         3383  +/*
         3384  +** Lower the locking level on file descriptor pFile to locktype.  locktype
         3385  +** must be either NO_LOCK or SHARED_LOCK.
         3386  +**
         3387  +** If the locking level of the file descriptor is already at or below
         3388  +** the requested locking level, this routine is a no-op.
         3389  +*/
         3390  +static int proxyUnlock(sqlite3_file *id, int locktype) {
         3391  +  unixFile *pFile = (unixFile*)id;
         3392  +  int rc = proxyTakeConch(pFile);
  3203   3393     if( rc==SQLITE_OK ){
  3204         -    *ppFile = pNew;
  3205         -    return SQLITE_OK;
         3394  +    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
         3395  +    unixFile *proxy = pCtx->lockProxy;
         3396  +    rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
         3397  +    pFile->locktype = proxy->locktype;
  3206   3398     }
  3207         -end_create_proxy:    
  3208         -  close(fd); /* silently leak fd if error, we're already in error */
  3209         -  sqlite3_free(pNew);
  3210   3399     return rc;
  3211   3400   }
         3401  +
         3402  +/*
         3403  +** Close a file that uses proxy locks.
         3404  +*/
         3405  +static int proxyClose(sqlite3_file *id) {
         3406  +  if( id ){
         3407  +    unixFile *pFile = (unixFile*)id;
         3408  +    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
         3409  +    unixFile *lockProxy = pCtx->lockProxy;
         3410  +    unixFile *conchFile = pCtx->conchFile;
         3411  +    int rc = SQLITE_OK;
         3412  +    
         3413  +    if( lockProxy ){
         3414  +      rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
         3415  +      if( rc ) return rc;
         3416  +      rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
         3417  +      if( rc ) return rc;
         3418  +      sqlite3_free(lockProxy);
         3419  +      pCtx->lockProxy = 0;
         3420  +    }
         3421  +    if( conchFile ){
         3422  +      if( pCtx->conchHeld ){
         3423  +        rc = proxyReleaseConch(pFile);
         3424  +        if( rc ) return rc;
         3425  +      }
         3426  +      rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
         3427  +      if( rc ) return rc;
         3428  +      sqlite3_free(conchFile);
         3429  +    }
         3430  +    sqlite3_free(pCtx->lockProxyPath);
         3431  +    sqlite3_free(pCtx->conchFilePath);
         3432  +    sqlite3_free(pCtx->dbPath);
         3433  +    /* restore the original locking context and pMethod then close it */
         3434  +    pFile->lockingContext = pCtx->oldLockingContext;
         3435  +    pFile->pMethod = pCtx->pOldMethod;
         3436  +    sqlite3_free(pCtx);
         3437  +    return pFile->pMethod->xClose(id);
         3438  +  }
         3439  +  return SQLITE_OK;
         3440  +}
         3441  +
  3212   3442   
  3213   3443   
  3214   3444   #endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
  3215   3445   /*
  3216   3446   ** The proxy locking style is intended for use with AFP filesystems.
  3217   3447   ** And since AFP is only supported on MacOSX, the proxy locking is also
  3218   3448   ** restricted to MacOSX.
................................................................................
  3371   3601     }
  3372   3602     return SQLITE_OK;
  3373   3603   }
  3374   3604   
  3375   3605   #ifdef SQLITE_TEST
  3376   3606   /*
  3377   3607   ** Count the number of fullsyncs and normal syncs.  This is used to test
  3378         -** that syncs and fullsyncs are occuring at the right times.
         3608  +** that syncs and fullsyncs are occurring at the right times.
  3379   3609   */
  3380   3610   int sqlite3_sync_count = 0;
  3381   3611   int sqlite3_fullsync_count = 0;
  3382   3612   #endif
  3383   3613   
  3384   3614   /*
  3385   3615   ** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
................................................................................
  3446   3676   #elif HAVE_FULLFSYNC
  3447   3677     if( fullSync ){
  3448   3678       rc = fcntl(fd, F_FULLFSYNC, 0);
  3449   3679     }else{
  3450   3680       rc = 1;
  3451   3681     }
  3452   3682     /* If the FULLFSYNC failed, fall back to attempting an fsync().
  3453         -   * It shouldn't be possible for fullfsync to fail on the local 
  3454         -   * file system (on OSX), so failure indicates that FULLFSYNC
  3455         -   * isn't supported for this file system. So, attempt an fsync 
  3456         -   * and (for now) ignore the overhead of a superfluous fcntl call.  
  3457         -   * It'd be better to detect fullfsync support once and avoid 
  3458         -   * the fcntl call every time sync is called.
  3459         -   */
         3683  +  ** It shouldn't be possible for fullfsync to fail on the local 
         3684  +  ** file system (on OSX), so failure indicates that FULLFSYNC
         3685  +  ** isn't supported for this file system. So, attempt an fsync 
         3686  +  ** and (for now) ignore the overhead of a superfluous fcntl call.  
         3687  +  ** It'd be better to detect fullfsync support once and avoid 
         3688  +  ** the fcntl call every time sync is called.
         3689  +  */
  3460   3690     if( rc ) rc = fsync(fd);
  3461   3691   
  3462   3692   #else 
  3463   3693     if( dataOnly ){
  3464   3694       rc = fdatasync(fd);
  3465   3695       if( OS_VXWORKS && rc==-1 && errno==ENOTSUP ){
  3466   3696         rc = fsync(fd);
................................................................................
  3605   3835         return SQLITE_OK;
  3606   3836       }
  3607   3837   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
  3608   3838       case SQLITE_GET_LOCKPROXYFILE: {
  3609   3839         unixFile *pFile = (unixFile*)id;
  3610   3840         if( pFile->pMethod == &proxyIoMethods ){
  3611   3841           proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
  3612         -        takeConch(pFile);
         3842  +        proxyTakeConch(pFile);
  3613   3843           if( pCtx->lockProxyPath ){
  3614   3844             *(const char **)pArg = pCtx->lockProxyPath;
  3615   3845           }else{
  3616   3846             *(const char **)pArg = ":auto: (not held)";
  3617   3847           }
  3618   3848         } else {
  3619   3849           *(const char **)pArg = NULL;
................................................................................
  3648   3878           }else{
  3649   3879             /* turn on proxy file locking */
  3650   3880             rc = transformUnixFileForLockProxy(pFile, proxyPath);
  3651   3881           }
  3652   3882         }
  3653   3883         return rc;
  3654   3884       }
  3655         -#endif
         3885  +#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__) */
  3656   3886     }
  3657   3887     return SQLITE_ERROR;
  3658   3888   }
  3659   3889   
  3660   3890   /*
  3661   3891   ** Return the sector size in bytes of the underlying block device for
  3662   3892   ** the specified file. This is almost always 512 bytes, but may be
................................................................................
  3683   3913   /*
  3684   3914   ** Here ends the implementation of all sqlite3_file methods.
  3685   3915   **
  3686   3916   ********************** End sqlite3_file Methods *******************************
  3687   3917   ******************************************************************************/
  3688   3918   
  3689   3919   /*
         3920  +** This division contains definitions of sqlite3_io_methods objects that
         3921  +** implement various file locking strategies.  It also contains definitions
         3922  +** of "finder" functions.  A finder-function is used to locate the appropriate
         3923  +** sqlite3_io_methods object for a particular database file.  The pAppData
         3924  +** field of the sqlite3_vfs VFS objects are initialized to be pointers to
         3925  +** the correct finder-function for that VFS.
         3926  +**
         3927  +** Most finder functions return a pointer to a fixed sqlite3_io_methods
         3928  +** object.  The only interesting finder-function is autolockIoFinder, which
         3929  +** looks at the filesystem type and tries to guess the best locking
         3930  +** strategy from that.
         3931  +**
         3932  +**
  3690   3933   ** Each instance of this macro generates two objects:
  3691   3934   **
  3692   3935   **   *  A constant sqlite3_io_methods object call METHOD that has locking
  3693   3936   **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
  3694   3937   **
  3695   3938   **   *  An I/O method finder function called FINDER that returns a pointer
  3696   3939   **      to the METHOD object in the previous bullet.
................................................................................
  3786   4029     proxyCheckReservedLock    /* xCheckReservedLock method */
  3787   4030   );
  3788   4031   #endif
  3789   4032   
  3790   4033   
  3791   4034   #if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
  3792   4035   /* 
  3793         -** This procedure attempts to determine the best locking strategy for
  3794         -** the given database file.  It then returns the sqlite3_io_methods
         4036  +** This "finder" function attempts to determine the best locking strategy 
         4037  +** for the database file "filePath".  It then returns the sqlite3_io_methods
  3795   4038   ** object that implements that strategy.
  3796   4039   **
  3797   4040   ** This is for MacOSX only.
  3798   4041   */
  3799   4042   static const sqlite3_io_methods *autolockIoFinder(
  3800   4043     const char *filePath,    /* name of the database file */
  3801   4044     int fd                   /* file descriptor open on the database file */
  3802   4045   ){
  3803   4046     static const struct Mapping {
  3804         -    const char *zFilesystem;
  3805         -    const sqlite3_io_methods *pMethods;
         4047  +    const char *zFilesystem;              /* Filesystem type name */
         4048  +    const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
  3806   4049     } aMap[] = {
  3807   4050       { "hfs",    &posixIoMethods },
  3808   4051       { "ufs",    &posixIoMethods },
  3809   4052       { "afpfs",  &afpIoMethods },
  3810   4053   #ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
  3811   4054       { "smbfs",  &afpIoMethods },
  3812   4055   #else
................................................................................
  3816   4059       { 0, 0 }
  3817   4060     };
  3818   4061     int i;
  3819   4062     struct statfs fsInfo;
  3820   4063     struct flock lockInfo;
  3821   4064   
  3822   4065     if( !filePath ){
         4066  +    /* If filePath==NULL that means we are dealing with a transient file
         4067  +    ** that does not need to be locked. */
  3823   4068       return &nolockIoMethods;
  3824   4069     }
  3825   4070     if( statfs(filePath, &fsInfo) != -1 ){
  3826   4071       if( fsInfo.f_flags & MNT_RDONLY ){
  3827   4072         return &nolockIoMethods;
  3828   4073       }
  3829   4074       for(i=0; aMap[i].zFilesystem; i++){
................................................................................
  4004   4249     }else{
  4005   4250       pNew->pMethod = pLockingStyle;
  4006   4251       OpenCounter(+1);
  4007   4252     }
  4008   4253     return rc;
  4009   4254   }
  4010   4255   
  4011         -#if SQLITE_ENABLE_LOCKING_STYLE
  4012         -static int getDbPathForUnixFile(unixFile *pFile, char *dbPath){
  4013         -#if defined(__DARWIN__)
  4014         -  if( pFile->pMethod == &afpIoMethods ){
  4015         -    /* afp style keeps a reference to the db path in the filePath field 
  4016         -    ** of the struct */
  4017         -    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
  4018         -    strcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath)
  4019         -  }else
  4020         -#endif
  4021         -  if( pFile->pMethod == &dotlockIoMethods ){
  4022         -    /* dot lock style uses the locking context to store the dot lock
  4023         -    ** file path */
  4024         -    int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
  4025         -    memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
  4026         -  }else{
  4027         -    /* all other styles use the locking context to store the db file path */
  4028         -    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
  4029         -    strcpy(dbPath, (char *)pFile->lockingContext);
  4030         -  }
  4031         -  return SQLITE_OK;
  4032         -}
  4033         -#endif
  4034         -
  4035   4256   /*
  4036   4257   ** Open a file descriptor to the directory containing file zFilename.
  4037   4258   ** If successful, *pFd is set to the opened file descriptor and
  4038   4259   ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
  4039   4260   ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
  4040   4261   ** value.
  4041   4262   **
................................................................................
  4144   4365   ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
  4145   4366   ** true, the file was configured to be automatically deleted when the
  4146   4367   ** file handle closed. To achieve the same effect using this new 
  4147   4368   ** interface, add the DELETEONCLOSE flag to those specified above for 
  4148   4369   ** OpenExclusive().
  4149   4370   */
  4150   4371   static int unixOpen(
  4151         -  sqlite3_vfs *pVfs, 
  4152         -  const char *zPath, 
  4153         -  sqlite3_file *pFile,
  4154         -  int flags,
  4155         -  int *pOutFlags
         4372  +  sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
         4373  +  const char *zPath,           /* Pathname of file to be opened */
         4374  +  sqlite3_file *pFile,         /* The file descriptor to be filled in */
         4375  +  int flags,                   /* Input flags to control the opening */
         4376  +  int *pOutFlags               /* Output flags returned to SQLite core */
  4156   4377   ){
  4157   4378     int fd = 0;                    /* File descriptor returned by open() */
  4158   4379     int dirfd = -1;                /* Directory file descriptor */
  4159         -  int openFlags = 0;                /* Flags to pass to open() */
         4380  +  int openFlags = 0;             /* Flags to pass to open() */
  4160   4381     int eType = flags&0xFFFFFF00;  /* Type of file to open */
  4161   4382     int noLock;                    /* True to omit locking primitives */
  4162   4383     int rc = SQLITE_OK;
  4163   4384   
  4164   4385     int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
  4165   4386     int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  4166   4387     int isCreate     = (flags & SQLITE_OPEN_CREATE);
................................................................................
  4300   4521     return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
  4301   4522   }
  4302   4523   
  4303   4524   /*
  4304   4525   ** Delete the file at zPath. If the dirSync argument is true, fsync()
  4305   4526   ** the directory after deleting the file.
  4306   4527   */
  4307         -static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync){
         4528  +static int unixDelete(
         4529  +  sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
         4530  +  const char *zPath,        /* Name of file to be deleted */
         4531  +  int dirSync               /* If true, fsync() directory after deleting file */
         4532  +){
  4308   4533     int rc = SQLITE_OK;
  4309   4534     UNUSED_PARAMETER(NotUsed);
  4310   4535     SimulateIOError(return SQLITE_IOERR_DELETE);
  4311   4536     unlink(zPath);
  4312   4537   #ifndef SQLITE_DISABLE_DIRSYNC
  4313   4538     if( dirSync ){
  4314   4539       int fd;
................................................................................
  4338   4563   **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
  4339   4564   **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
  4340   4565   **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
  4341   4566   **
  4342   4567   ** Otherwise return 0.
  4343   4568   */
  4344   4569   static int unixAccess(
  4345         -  sqlite3_vfs *NotUsed, 
  4346         -  const char *zPath, 
  4347         -  int flags, 
  4348         -  int *pResOut
         4570  +  sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
         4571  +  const char *zPath,      /* Path of the file to examine */
         4572  +  int flags,              /* What do we want to learn about the zPath file? */
         4573  +  int *pResOut            /* Write result boolean here */
  4349   4574   ){
  4350   4575     int amode = 0;
  4351   4576     UNUSED_PARAMETER(NotUsed);
  4352   4577     SimulateIOError( return SQLITE_IOERR_ACCESS; );
  4353   4578     switch( flags ){
  4354   4579       case SQLITE_ACCESS_EXISTS:
  4355   4580         amode = F_OK;
................................................................................
  4384   4609     int nOut,                     /* Size of output buffer in bytes */
  4385   4610     char *zOut                    /* Output buffer */
  4386   4611   ){
  4387   4612   
  4388   4613     /* It's odd to simulate an io-error here, but really this is just
  4389   4614     ** using the io-error infrastructure to test that SQLite handles this
  4390   4615     ** function failing. This function could fail if, for example, the
  4391         -  ** current working directly has been unlinked.
         4616  +  ** current working directory has been unlinked.
  4392   4617     */
  4393   4618     SimulateIOError( return SQLITE_ERROR );
  4394   4619   
  4395   4620     assert( pVfs->mxPathname==MAX_PATHNAME );
  4396   4621     UNUSED_PARAMETER(pVfs);
  4397   4622   
  4398   4623     zOut[nOut-1] = '\0';
................................................................................
  4519   4744     sleep(seconds);
  4520   4745     return seconds*1000000;
  4521   4746   #endif
  4522   4747     UNUSED_PARAMETER(NotUsed);
  4523   4748   }
  4524   4749   
  4525   4750   /*
  4526         -** The following variable, if set to a non-zero value, becomes the result
  4527         -** returned from sqlite3OsCurrentTime().  This is used for testing.
         4751  +** The following variable, if set to a non-zero value, is interpreted as
         4752  +** the number of seconds since 1970 and is used to set the result of
         4753  +** sqlite3OsCurrentTime() during testing.
  4528   4754   */
  4529   4755   #ifdef SQLITE_TEST
  4530         -int sqlite3_current_time = 0;
         4756  +int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
  4531   4757   #endif
  4532   4758   
  4533   4759   /*
  4534   4760   ** Find the current time (in Universal Coordinated Time).  Write the
  4535   4761   ** current time and date as a Julian Day number into *prNow and
  4536   4762   ** return 0.  Return 1 if the time and date cannot be found.
  4537   4763   */
................................................................................
  4555   4781       *prNow = sqlite3_current_time/86400.0 + 2440587.5;
  4556   4782     }
  4557   4783   #endif
  4558   4784     UNUSED_PARAMETER(NotUsed);
  4559   4785     return 0;
  4560   4786   }
  4561   4787   
         4788  +/*
         4789  +** We added the xGetLastError() method with the intention of providing
         4790  +** better low-level error messages when operating-system problems come up
         4791  +** during SQLite operation.  But so far, none of that has been implemented
         4792  +** in the core.  So this routine is never called.  For now, it is merely
         4793  +** a place-holder.
         4794  +*/
  4562   4795   static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
  4563   4796     UNUSED_PARAMETER(NotUsed);
  4564   4797     UNUSED_PARAMETER(NotUsed2);
  4565   4798     UNUSED_PARAMETER(NotUsed3);
  4566   4799     return 0;
  4567   4800   }
  4568   4801   
................................................................................
  4573   4806   /*
  4574   4807   ** Initialize the operating system interface.
  4575   4808   **
  4576   4809   ** This routine registers all VFS implementations for unix-like operating
  4577   4810   ** systems.  This routine, and the sqlite3_os_end() routine that follows,
  4578   4811   ** should be the only routines in this file that are visible from other
  4579   4812   ** files.
         4813  +**
         4814  +** This routine is called once during SQLite initialization and by a
         4815  +** single thread.  The memory allocation and mutex subsystems have not
         4816  +** necessarily been initialized when this routine is called, and so they
         4817  +** should not be used.
  4580   4818   */
  4581   4819   int sqlite3_os_init(void){ 
  4582         -  /* Macro to define the static contents of an sqlite3_vfs structure for
  4583         -  ** the unix backend. The two parameters are the values to use for
  4584         -  ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
  4585         -  ** 
         4820  +  /* 
         4821  +  ** The following macro defines an initializer for an sqlite3_vfs object.
         4822  +  ** The name of the VFS is NAME.  The pAppData is a pointer to a "finder"
         4823  +  ** function.  The FINDER parameter to this macro is the name of the
         4824  +  ** finder-function.  The finder-function returns a pointer to the
         4825  +  ** sqlite_io_methods object that implements the desired locking
         4826  +  ** behaviors.  See the division above that contains the IOMETHODS
         4827  +  ** macro for addition information on finder-functions.
         4828  +  **
         4829  +  ** Most finders simply return a pointer to a fixed sqlite3_io_methods
         4830  +  ** object.  But the "autolockIoFinder" available on MacOSX does a little
         4831  +  ** more than that; it looks at the filesystem type that hosts the 
         4832  +  ** database file and tries to choose an locking method appropriate for
         4833  +  ** that filesystem time.
  4586   4834     */
  4587   4835     #define UNIXVFS(VFSNAME, FINDER) {                        \
  4588   4836       1,                    /* iVersion */                    \
  4589   4837       sizeof(unixFile),     /* szOsFile */                    \
  4590   4838       MAX_PATHNAME,         /* mxPathname */                  \
  4591   4839       0,                    /* pNext */                       \
  4592   4840       VFSNAME,              /* zName */                       \
................................................................................
  4601   4849       unixDlClose,          /* xDlClose */                    \
  4602   4850       unixRandomness,       /* xRandomness */                 \
  4603   4851       unixSleep,            /* xSleep */                      \
  4604   4852       unixCurrentTime,      /* xCurrentTime */                \
  4605   4853       unixGetLastError      /* xGetLastError */               \
  4606   4854     }
  4607   4855   
  4608         -  unsigned int i;
         4856  +  /*
         4857  +  ** All default VFSes for unix are contained in the following array.
         4858  +  **
         4859  +  ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
         4860  +  ** by the SQLite core when the VFS is registered.  So the following
         4861  +  ** array cannot be const.
         4862  +  */
  4609   4863     static sqlite3_vfs aVfs[] = {
  4610   4864   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
  4611   4865       UNIXVFS("unix",          autolockIoFinder ),
  4612   4866   #else
  4613   4867       UNIXVFS("unix",          posixIoFinder ),
  4614   4868   #endif
  4615   4869       UNIXVFS("unix-none",     nolockIoFinder ),
................................................................................
  4622   4876       UNIXVFS("unix-flock",    flockIoFinder ),
  4623   4877   #endif
  4624   4878   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
  4625   4879       UNIXVFS("unix-afp",      afpIoFinder ),
  4626   4880       UNIXVFS("unix-proxy",    proxyIoFinder ),
  4627   4881   #endif
  4628   4882     };
         4883  +  unsigned int i;          /* Loop counter */
         4884  +
         4885  +  /* Register all VFSes defined in the aVfs[] array */
  4629   4886     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
  4630   4887       sqlite3_vfs_register(&aVfs[i], i==0);
  4631   4888     }
  4632   4889     return SQLITE_OK; 
  4633   4890   }
  4634   4891   
  4635   4892   /*
  4636         -** Shutdown the operating system interface. This is a no-op for unix.
         4893  +** Shutdown the operating system interface.
         4894  +**
         4895  +** Some operating systems might need to do some cleanup in this routine,
         4896  +** to release dynamically allocated objects.  But not on unix.
         4897  +** This routine is a no-op for unix.
  4637   4898   */
  4638   4899   int sqlite3_os_end(void){ 
  4639   4900     return SQLITE_OK; 
  4640   4901   }
  4641   4902    
  4642   4903   #endif /* SQLITE_OS_UNIX */