/ Check-in [57d70240]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Move the os_unix.c file closer to trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 57d7024007c2a71bf6cb875b1f1daf26924d188e
User & Date: drh 2015-02-19 02:43:19
Context
2015-02-19
15:56
Incremental merge to prevent a huge pileup of merge conflicts resulting from editing the same code in two different branches. check-in: 82c4c4e2 user: drh tags: apple-osx
02:43
Move the os_unix.c file closer to trunk. check-in: 57d70240 user: drh tags: apple-osx
2015-02-13
16:42
Enhancements to SQLITE_ENABLE_API_ARMOR merged from trunk. check-in: 5147b94a user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/os_unix.c.

    41     41   **   *  sqlite3_vfs method implementations.
    42     42   **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
    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     46   #include "sqliteInt.h"
    47     47   #if SQLITE_OS_UNIX              /* This file is used on unix only */
    48         -
    49         -#include "btreeInt.h"           /* Use by Apple extensions only */
    50         -
    51     48   
    52     49   /*
    53     50   ** There are various methods for file locking used for concurrency
    54     51   ** control:
    55     52   **
    56     53   **   1. POSIX locking (the default),
    57     54   **   2. No locking,
................................................................................
    99     96   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
   100     97   # include <sys/mman.h>
   101     98   #endif
   102     99   
   103    100   #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
   104    101   # include <sys/ioctl.h>
   105    102   # include <uuid/uuid.h>
   106         -# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
   107         -                            (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
   108         -#  define HAVE_GETHOSTUUID 1
   109         -# endif
   110    103   # if OS_VXWORKS
   111    104   #  include <semaphore.h>
   112    105   #  include <limits.h>
   113    106   # else
   114    107   #  include <sys/file.h>
   115    108   #  include <sys/param.h>
   116    109   # endif
................................................................................
  1469   1462   static void robust_close(unixFile *pFile, int h, int lineno){
  1470   1463     if( osClose(h) ){
  1471   1464       unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
  1472   1465                          pFile ? pFile->zPath : 0, lineno);
  1473   1466     }
  1474   1467   }
  1475   1468   
         1469  +/*
         1470  +** Set the pFile->lastErrno.  Do this in a subroutine as that provides a convenient
         1471  +** place to set a breakpoint.
         1472  +*/
  1476   1473   static void storeLastErrno(unixFile *pFile, int error){
  1477   1474     pFile->lastErrno = error;
  1478   1475   }
  1479   1476   
  1480   1477   /*
  1481   1478   ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
  1482   1479   */ 
................................................................................
  1667   1664     }
  1668   1665     if( buf.st_nlink>1 ){
  1669   1666       sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
  1670   1667       pFile->ctrlFlags |= UNIXFILE_WARNED;
  1671   1668       return;
  1672   1669     }
  1673   1670     if( fileHasMoved(pFile) ){
  1674         -    sqlite3_log(SQLITE_WARNING, "file renamed while open: [%s]", pFile->zPath);
         1671  +    sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
  1675   1672       pFile->ctrlFlags |= UNIXFILE_WARNED;
  1676   1673       return;
  1677   1674     }
  1678   1675   }
  1679   1676   
  1680   1677   
  1681   1678   /*
................................................................................
  2380   2377   */
  2381   2378   static int nolockClose(sqlite3_file *id) {
  2382   2379     int rc = SQLITE_OK;
  2383   2380     unixFile *pFile = (unixFile *)id;
  2384   2381     unixEnterMutex();
  2385   2382     
  2386   2383     /* unixFile.pInode is always valid here. Otherwise, a different close
  2387         -   ** routine (e.g. nolockClose()) would be called instead.
  2388         -   */
         2384  +  ** routine (e.g. nolockClose()) would be called instead.
         2385  +  */
  2389   2386     assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
  2390   2387     if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
  2391   2388       /* If there are outstanding locks, do not actually close the file just
  2392         -     ** yet because that would clear those locks.  Instead, add the file
  2393         -     ** descriptor to pInode->pUnused list.  It will be automatically closed 
  2394         -     ** when the last lock is cleared.
  2395         -     */
         2389  +    ** yet because that would clear those locks.  Instead, add the file
         2390  +    ** descriptor to pInode->pUnused list.  It will be automatically closed 
         2391  +    ** when the last lock is cleared.
         2392  +    */
  2396   2393       setPendingFd(pFile);
  2397   2394     }
  2398   2395     releaseInodeInfo(pFile);
  2399   2396     rc = closeUnixFile(id);
  2400   2397     unixLeaveMutex();
  2401   2398     return rc;
  2402   2399   }
................................................................................
  3609   3606     got = seekAndRead(pFile, offset, pBuf, amt);
  3610   3607     if( got==amt ){
  3611   3608       return SQLITE_OK;
  3612   3609     }else if( got<0 ){
  3613   3610       /* lastErrno set by seekAndRead */
  3614   3611       return SQLITE_IOERR_READ;
  3615   3612     }else{
         3613  +    storeLastErrno(pFile, 0);   /* not a system error */
  3616   3614       /* Unread parts of the buffer must be zero-filled */
  3617   3615       memset(&((char*)pBuf)[got], 0, amt-got);
  3618   3616       return SQLITE_IOERR_SHORT_READ;
  3619   3617     }
  3620   3618   }
  3621   3619   
  3622   3620   /*
................................................................................
  3749   3747     SimulateDiskfullError(( wrote=0, amt=1 ));
  3750   3748   
  3751   3749     if( amt>0 ){
  3752   3750       if( wrote<0 && pFile->lastErrno!=ENOSPC ){
  3753   3751         /* lastErrno set by seekAndWrite */
  3754   3752         return SQLITE_IOERR_WRITE;
  3755   3753       }else{
         3754  +      storeLastErrno(pFile, 0); /* not a system error */
  3756   3755         return SQLITE_FULL;
  3757   3756       }
  3758   3757     }
  3759   3758   
  3760   3759     return SQLITE_OK;
  3761   3760   }
  3762   3761   
................................................................................
  4154   4153     }
  4155   4154   
  4156   4155   #if SQLITE_MAX_MMAP_SIZE>0
  4157   4156     if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
  4158   4157       int rc;
  4159   4158       if( pFile->szChunk<=0 ){
  4160   4159         if( robust_ftruncate(pFile->h, nByte) ){
  4161         -        pFile->lastErrno = errno;
         4160  +        storeLastErrno(pFile, errno);
  4162   4161           return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
  4163   4162         }
  4164   4163       }
  4165   4164   
  4166   4165       rc = unixMapfile(pFile, nByte);
  4167   4166       return rc;
  4168   4167     }
................................................................................
  4171   4170     return SQLITE_OK;
  4172   4171   }
  4173   4172   
  4174   4173   
  4175   4174   #if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
  4176   4175   #include "sqlite3_private.h"
  4177   4176   #include <copyfile.h>
  4178         -static int getDbPathForUnixFile(unixFile *pFile, char *dbPath);
         4177  +static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath);
  4179   4178   #endif
  4180   4179   
  4181   4180   #if SQLITE_ENABLE_LOCKING_STYLE
  4182   4181   static int isProxyLockingMode(unixFile *);
  4183   4182   #endif
  4184   4183   
  4185   4184   #if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
................................................................................
  4997   4996     ** one if present. Create a new one if necessary.
  4998   4997     */
  4999   4998     unixEnterMutex();
  5000   4999     pInode = pDbFd->pInode;
  5001   5000     pShmNode = pInode->pShmNode;
  5002   5001     if( pShmNode==0 ){
  5003   5002       struct stat sStat;                 /* fstat() info for database file */
         5003  +#ifndef SQLITE_SHM_DIRECTORY
  5004   5004       const char *zBasePath = pDbFd->zPath;
         5005  +#endif
  5005   5006   
  5006   5007       /* Call fstat() to figure out the permissions on the database file. If
  5007   5008       ** a new *-shm file is created, an attempt will be made to create it
  5008   5009       ** with the same permissions.
  5009   5010       */
  5010   5011       if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
  5011   5012         storeLastErrno(pDbFd, errno);
  5012   5013         rc = SQLITE_IOERR_FSTAT;
  5013   5014         goto shm_open_err;
  5014   5015       }
  5015   5016   
  5016         -#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
         5017  +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE \
         5018  +      && !defined(SQLITE_SHM_DIRECTORY)
  5017   5019       /* If pDbFd is configured with proxy locking mode, use the local 
  5018         -     ** lock file path to determine the -shm file path
  5019         -     */
         5020  +    ** lock file path to determine the -shm file path
         5021  +    */
  5020   5022       if( isProxyLockingMode(pDbFd) ){
  5021   5023         zBasePath = proxySharedMemoryBasePath(pDbFd);
  5022   5024         if( !zBasePath ){
  5023   5025           rc = SQLITE_CANTOPEN_BKPT;
  5024   5026           goto shm_open_err;
  5025   5027         }
  5026   5028       }
................................................................................
  5442   5444     ** shared-memory file, too */
  5443   5445     unixEnterMutex();
  5444   5446     assert( pShmNode->nRef>0 );
  5445   5447     pShmNode->nRef--;
  5446   5448     if( pShmNode->nRef==0 ){
  5447   5449       if( deleteFlag && pShmNode->h>=0 ) {
  5448   5450         if (deleteFlag == 1) { 
  5449         -        unlink(pShmNode->zFilename);
         5451  +        osUnlink(pShmNode->zFilename);
  5450   5452         } else if (deleteFlag == 2) {
  5451   5453           /* ftruncate(pShmNode->h, 32 * 1024); */
  5452   5454         }
  5453   5455       }
  5454   5456       
  5455   5457       unixShmPurge(pDbFd);
  5456   5458     }
................................................................................
  6562   6564           pNew->pInode->aSemName[0] = '\0';
  6563   6565         }
  6564   6566       }
  6565   6567       unixLeaveMutex();
  6566   6568     }
  6567   6569   #endif
  6568   6570     
  6569         -  pNew->lastErrno = 0;
         6571  +  storeLastErrno(pNew, 0);
  6570   6572   #if OS_VXWORKS
  6571   6573     if( rc!=SQLITE_OK ){
  6572   6574       if( h>=0 ) robust_close(pNew, h, __LINE__);
  6573   6575       h = -1;
  6574   6576       osUnlink(zFilename);
  6575   6577       pNew->ctrlFlags |= UNIXFILE_DELETE;
  6576   6578     }
................................................................................
  7496   7498   **
  7497   7499   **
  7498   7500   ** Using proxy locks
  7499   7501   ** -----------------
  7500   7502   **
  7501   7503   ** C APIs
  7502   7504   **
  7503         -**  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
         7505  +**  sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
  7504   7506   **                       <proxy_path> | ":auto:");
  7505         -**  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
         7507  +**  sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
         7508  +**                       &<proxy_path>);
  7506   7509   **
  7507   7510   **
  7508   7511   ** SQL pragmas
  7509   7512   **
  7510   7513   **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
  7511   7514   **  PRAGMA [database.]lock_proxy_file
  7512   7515   **
................................................................................
  7591   7594   **       lock proxy files, only used when LOCKPROXYDIR is not set.
  7592   7595   **    
  7593   7596   **    
  7594   7597   ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
  7595   7598   ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
  7596   7599   ** force proxy locking to be used for every database file opened, and 0
  7597   7600   ** will force automatic proxy locking to be disabled for all database
  7598         -** files (explicitly calling the SQLITE_SET_LOCKPROXYFILE pragma or
         7601  +** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
  7599   7602   ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
  7600   7603   */
  7601   7604   
  7602   7605   /*
  7603   7606   ** Proxy locking is only available on MacOSX 
  7604   7607   */
  7605   7608   #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
................................................................................
  7828   7831   
  7829   7832   /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
  7830   7833   ** bytes of writable memory.
  7831   7834   */
  7832   7835   static int proxyGetHostID(unsigned char *pHostID, int *pError){
  7833   7836     assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
  7834   7837     memset(pHostID, 0, PROXY_HOSTIDLEN);
  7835         -#if HAVE_GETHOSTUUID
         7838  +# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
         7839  +                            (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
  7836   7840     {
  7837   7841       struct timespec timeout = {1, 0}; /* 1 sec timeout */
  7838         -    
  7839   7842       if( gethostuuid(pHostID, &timeout) ){
  7840   7843         int err = errno;
  7841   7844         if( pError ){
  7842   7845           *pError = err;
  7843   7846         }
  7844   7847         return SQLITE_IOERR;
  7845   7848       }
................................................................................
  8350   8353   /*
  8351   8354   ** pFile is a file that has been opened by a prior xOpen call.  dbPath
  8352   8355   ** is a string buffer at least MAXPATHLEN+1 characters in size.
  8353   8356   **
  8354   8357   ** This routine find the filename associated with pFile and writes it
  8355   8358   ** int dbPath.
  8356   8359   */
  8357         -static int getDbPathForUnixFile(unixFile *pFile, char *dbPath){
         8360  +static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
  8358   8361   #if defined(__APPLE__)
  8359   8362     if( pFile->pMethod == &afpIoMethods ){
  8360   8363       /* afp style keeps a reference to the db path in the filePath field 
  8361   8364       ** of the struct */
  8362   8365       assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
  8363   8366       strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
  8364   8367               MAXPATHLEN);
................................................................................
  8390   8393     char dbPath[MAXPATHLEN+1];       /* Name of the database file */
  8391   8394     char *lockPath=NULL;
  8392   8395     int rc = SQLITE_OK;
  8393   8396     
  8394   8397     if( pFile->eFileLock!=NO_LOCK ){
  8395   8398       return SQLITE_BUSY;
  8396   8399     }
  8397         -  getDbPathForUnixFile(pFile, dbPath);
         8400  +  proxyGetDbPathForUnixFile(pFile, dbPath);
  8398   8401     if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
  8399   8402       lockPath=NULL;
  8400   8403     }else{
  8401   8404       lockPath=(char *)path;
  8402   8405     }
  8403   8406     
  8404   8407     OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
................................................................................
  8473   8476   
  8474   8477   /*
  8475   8478   ** This routine handles sqlite3_file_control() calls that are specific
  8476   8479   ** to proxy locking.
  8477   8480   */
  8478   8481   static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
  8479   8482     switch( op ){
  8480         -    case SQLITE_GET_LOCKPROXYFILE: {
         8483  +    case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
  8481   8484         unixFile *pFile = (unixFile*)id;
  8482   8485         if( isProxyLockingMode(pFile) ){
  8483   8486           proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
  8484   8487           proxyTakeConch(pFile);
  8485   8488           if( pCtx->lockProxyPath ){
  8486   8489             *(const char **)pArg = pCtx->lockProxyPath;
  8487   8490           }else{
................................................................................
  8488   8491             *(const char **)pArg = ":auto: (not held)";
  8489   8492           }
  8490   8493         } else {
  8491   8494           *(const char **)pArg = NULL;
  8492   8495         }
  8493   8496         return SQLITE_OK;
  8494   8497       }
  8495         -    case SQLITE_SET_LOCKPROXYFILE: {
         8498  +    case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
  8496   8499         unixFile *pFile = (unixFile*)id;
  8497   8500         int rc = SQLITE_OK;
  8498   8501         int isProxyStyle = isProxyLockingMode(pFile);
  8499   8502         if( pArg==NULL || (const char *)pArg==0 ){
  8500   8503           if( isProxyStyle ){
  8501   8504             /* turn off proxy locking - not supported.  If support is added for
  8502   8505             ** switching proxy locking mode off then it will need to fail if