/ Check-in [6ebce3b7]
Login

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

Overview
Comment:Updates to the locking-style code in os_unix.c. Not yet working. (CVS 4834)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6ebce3b798c60050fb3c583d805570bb06837108
User & Date: drh 2008-03-07 00:27:10
Context
2008-03-07
02:20
Add missing prefix & exec_prefix to Makefile - fixes #2979. (CVS 4835) check-in: 4cda019b user: mlcreech tags: trunk
00:27
Updates to the locking-style code in os_unix.c. Not yet working. (CVS 4834) check-in: 6ebce3b7 user: drh tags: trunk
2008-03-06
16:28
Clean up recent autoconf-related additions, add config.h to the amalgamation (CVS 4833) check-in: 80446230 user: mlcreech tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/os_unix.c.

1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760

1761

1762
1763

1764
1765
1766
1767
1768
1769
1770
....
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852

1853
1854
1855
1856

1857
1858
1859
1860
1861
1862
1863
....
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972

1973
1974
1975
1976
1977
1978
1979
....
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011

2012
2013
2014
2015
2016
2017
2018
....
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
....
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177



2178
2179

2180
2181
2182
2183

2184
2185
2186
2187
2188
2189
2190
2191
2192


2193
2194

2195
2196
2197
2198
2199
2200
2201
2202
2203
2204

2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
  return rc;
}

/*
 ** Close a file & cleanup AFP specific locking context 
 */
static int afpUnixClose(sqlite3_file *id) {
  unixFile *pFile = (unixFile*)pId;

  if( !pFile ) return SQLITE_OK;
  afpUnixUnlock(*pId, NO_LOCK);
  /* free the AFP locking structure */
  if (pFile->lockingContext != NULL) {
    if (((afpLockingContext *)pFile->lockingContext)->filePath != NULL)
      sqlite3_free(((afpLockingContext*)pFile->lockingContext)->filePath);
    sqlite3_free(pFile->lockingContext);
  }

  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;

  close(pFile->h);

  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);

  return SQLITE_OK;
}


#pragma mark flock() style locking

/*
................................................................................
    return SQLITE_OK;
  }
}

/*
 ** Close a file.
 */
static int flockUnixClose(sqlite3_file *pId) {
  unixFile *pFile = (unixFile*)*pId;
  
  if( !pFile ) return SQLITE_OK;
  flockUnixUnlock(*pId, NO_LOCK);
  
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();
  

  close(pFile->h);  
  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);

  return SQLITE_OK;
}

#pragma mark Old-School .lock file based locking

/*
 ** The dotlockLockingContext structure contains all dotlock (.lock) lock
................................................................................
/*
 ** Close a file.
 */
static int dotlockUnixClose(sqlite3_file *id) {
  unixFile *pFile = (unixFile*)id;
  
  if( !pFile ) return SQLITE_OK;
  dotlockUnixUnlock(*pId, NO_LOCK);
  /* free the dotlock locking structure */
  if (pFile->lockingContext != NULL) {
    if (((dotlockLockingContext *)pFile->lockingContext)->lockPath != NULL)
      sqlite3_free( ( (dotlockLockingContext *)
        pFile->lockingContext)->lockPath);
    sqlite3_free(pFile->lockingContext);
  }
  
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();
  
  close(pFile->h);
  
  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);

  return SQLITE_OK;
}


#pragma mark No locking

/*
................................................................................
static int nolockUnixClose(sqlite3_file *id) {
  unixFile *pFile = (unixFile*)id;
  
  if( !pFile ) return SQLITE_OK;
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();
  
  close(pFile->h);
  
  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);

  return SQLITE_OK;
}

#endif /* SQLITE_ENABLE_LOCKING_STYLE */


/*
................................................................................
#ifdef SQLITE_ENABLE_LOCKING_STYLE
/*
** This vector defines all the methods that can operate on an sqlite3_file
** for unix with AFP style file locking.
*/
static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = {
  1,                        /* iVersion */
  unixClose,
  unixRead,
  unixWrite,
  unixTruncate,
  unixSync,
  unixFileSize,
  afpUnixLock,
  afpUnixUnlock,
................................................................................
** are needed to determine the unixFile pMethod to use for locking operations.
** The locking-style specific lockingContext data structure is created 
** and assigned here also.
*/
static int fillInUnixFile(
  int h,                  /* Open file descriptor of file being opened */
  int dirfd,              /* Directory file descriptor */
  sqlite3_file *pId,      /* Write completed initialization here */
  const char *zFilename,  /* Name of the file being opened */
){
  sqlite3LockingStyle lockingStyle;
  unixFile *pNew = (unixFile *)pId;
  int rc;

  memset(pNew, 0, sizeof(unixFile));



  lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
  if ( lockingStyle == posixLockingStyle ) {

    enterMutex();
    rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
    leaveMutex();
    if( rc ){

      close(h);
      unlink(zFilename);
      return SQLITE_NOMEM;
    }
  } else {
    /*  pLock and pOpen are only used for posix advisory locking */
    pNew->pLock = NULL;
    pNew->pOpen = NULL;
  }


  pNew->dirfd = -1;
  pNew->h = h;

  SET_THREADID(pNew);
  pNew = sqlite3_malloc( sizeof(unixFile) );
  if( pNew==0 ){
    close(h);
    enterMutex();
    releaseLockInfo(pNew->pLock);
    releaseOpenCnt(pNew->pOpen);
    leaveMutex();
    return SQLITE_NOMEM;
  }else{

    switch(lockingStyle) {
      case afpLockingStyle: {
        /* afp locking uses the file path so it needs to be included in
        ** the afpLockingContext */
        int nFilename;
        pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
        pNew->lockingContext = 
          sqlite3_malloc(sizeof(afpLockingContext));
        nFilename = strlen(zFilename)+1;
        ((afpLockingContext *)pNew->lockingContext)->filePath = 
          sqlite3_malloc(nFilename);
        memcpy(((afpLockingContext *)pNew->lockingContext)->filePath, 
               zFilename, nFilename);
        srandomdev();
        break;
      }
      case flockLockingStyle:
        /* flock locking doesn't need additional lockingContext information */
        pNew->pMethod = &sqlite3FlockLockingUnixIoMethod;
        break;
      case dotlockLockingStyle: {
        /* dotlock locking uses the file path so it needs to be included in
         ** the dotlockLockingContext */
        int nFilename;
        pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
        pNew->lockingContext = sqlite3_malloc(
          sizeof(dotlockLockingContext));
        nFilename = strlen(zFilename) + 6;
        ((dotlockLockingContext *)pNew->lockingContext)->lockPath = 
            sqlite3_malloc( nFilename );
        sqlite3_snprintf(nFilename, 
                ((dotlockLockingContext *)pNew->lockingContext)->lockPath, 
                "%s.lock", zFilename);
        break;
      }
      case posixLockingStyle:
        /* posix locking doesn't need additional lockingContext information */
        pNew->pMethod = &sqlite3UnixIoMethod;
        break;
      case noLockingStyle:
      case unsupportedLockingStyle:
      default: 
        pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
    }
    OpenCounter(+1);
    return SQLITE_OK;
  }
}
#else /* SQLITE_ENABLE_LOCKING_STYLE */
static int fillInUnixFile(
  int h,                 /* Open file descriptor on file being opened */
  int dirfd,
  sqlite3_file *pId,     /* Write to the unixFile structure here */
  const char *zFilename  /* Name of the file being opened */







|


|









>

>


>







 







|
|


|



<
|
>




>







 







|










|
<

<



>







 







<

<



>







 







|







 







|
|





|
>
>
>


>




>

<







>
>


>

<
<
<
<
<
<
<
<
<
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<







1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
....
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853

1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
....
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970

1971

1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
....
2002
2003
2004
2005
2006
2007
2008

2009

2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
....
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
....
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191

2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204









2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251

2252
2253
2254
2255
2256
2257
2258
  return rc;
}

/*
 ** Close a file & cleanup AFP specific locking context 
 */
static int afpUnixClose(sqlite3_file *id) {
  unixFile *pFile = (unixFile*)id;

  if( !pFile ) return SQLITE_OK;
  afpUnixUnlock(id, NO_LOCK);
  /* free the AFP locking structure */
  if (pFile->lockingContext != NULL) {
    if (((afpLockingContext *)pFile->lockingContext)->filePath != NULL)
      sqlite3_free(((afpLockingContext*)pFile->lockingContext)->filePath);
    sqlite3_free(pFile->lockingContext);
  }

  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();
  close(pFile->h);
  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);
  memset(pFile, 0, sizeof(unixFile));
  return SQLITE_OK;
}


#pragma mark flock() style locking

/*
................................................................................
    return SQLITE_OK;
  }
}

/*
 ** Close a file.
 */
static int flockUnixClose(sqlite3_file *id) {
  unixFile *pFile = (unixFile*)id;
  
  if( !pFile ) return SQLITE_OK;
  flockUnixUnlock(id, NO_LOCK);
  
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;


  enterMutex();
  close(pFile->h);  
  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);
  memset(pFile, 0, sizeof(unixFile));
  return SQLITE_OK;
}

#pragma mark Old-School .lock file based locking

/*
 ** The dotlockLockingContext structure contains all dotlock (.lock) lock
................................................................................
/*
 ** Close a file.
 */
static int dotlockUnixClose(sqlite3_file *id) {
  unixFile *pFile = (unixFile*)id;
  
  if( !pFile ) return SQLITE_OK;
  dotlockUnixUnlock(id, NO_LOCK);
  /* free the dotlock locking structure */
  if (pFile->lockingContext != NULL) {
    if (((dotlockLockingContext *)pFile->lockingContext)->lockPath != NULL)
      sqlite3_free( ( (dotlockLockingContext *)
        pFile->lockingContext)->lockPath);
    sqlite3_free(pFile->lockingContext);
  }
  
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();  

  close(pFile->h);

  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);
  memset(pFile, 0, sizeof(unixFile));
  return SQLITE_OK;
}


#pragma mark No locking

/*
................................................................................
static int nolockUnixClose(sqlite3_file *id) {
  unixFile *pFile = (unixFile*)id;
  
  if( !pFile ) return SQLITE_OK;
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();

  close(pFile->h);

  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);
  memset(pFile, 0, sizeof(unixFile));
  return SQLITE_OK;
}

#endif /* SQLITE_ENABLE_LOCKING_STYLE */


/*
................................................................................
#ifdef SQLITE_ENABLE_LOCKING_STYLE
/*
** This vector defines all the methods that can operate on an sqlite3_file
** for unix with AFP style file locking.
*/
static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = {
  1,                        /* iVersion */
  afpUnixClose,
  unixRead,
  unixWrite,
  unixTruncate,
  unixSync,
  unixFileSize,
  afpUnixLock,
  afpUnixUnlock,
................................................................................
** are needed to determine the unixFile pMethod to use for locking operations.
** The locking-style specific lockingContext data structure is created 
** and assigned here also.
*/
static int fillInUnixFile(
  int h,                  /* Open file descriptor of file being opened */
  int dirfd,              /* Directory file descriptor */
  sqlite3_file *pId,      /* Write to the unixFile structure here */
  const char *zFilename   /* Name of the file being opened */
){
  sqlite3LockingStyle lockingStyle;
  unixFile *pNew = (unixFile *)pId;
  int rc;

#ifdef FD_CLOEXEC
  fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);
#endif

  lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
  if ( lockingStyle == posixLockingStyle ) {
      
    enterMutex();
    rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
    leaveMutex();
    if( rc ){
      if( dirfd>=0 ) close(dirfd);
      close(h);

      return SQLITE_NOMEM;
    }
  } else {
    /*  pLock and pOpen are only used for posix advisory locking */
    pNew->pLock = NULL;
    pNew->pOpen = NULL;
  }

  OSTRACE3("OPEN    %-3d %s\n", h, zFilename);    
  pNew->dirfd = -1;
  pNew->h = h;
  pNew->dirfd = dirfd;
  SET_THREADID(pNew);









    
  switch(lockingStyle) {
    case afpLockingStyle: {
      /* afp locking uses the file path so it needs to be included in
      ** the afpLockingContext */
      int nFilename;
      pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
      pNew->lockingContext = 
        sqlite3_malloc(sizeof(afpLockingContext));
      nFilename = strlen(zFilename)+1;
      ((afpLockingContext *)pNew->lockingContext)->filePath = 
        sqlite3_malloc(nFilename);
      memcpy(((afpLockingContext *)pNew->lockingContext)->filePath, 
             zFilename, nFilename);
      srandomdev();
      break;
    }
    case flockLockingStyle:
      /* flock locking doesn't need additional lockingContext information */
      pNew->pMethod = &sqlite3FlockLockingUnixIoMethod;
      break;
    case dotlockLockingStyle: {
      /* dotlock locking uses the file path so it needs to be included in
       ** the dotlockLockingContext */
      int nFilename;
      pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
      pNew->lockingContext = sqlite3_malloc(
        sizeof(dotlockLockingContext));
      nFilename = strlen(zFilename) + 6;
      ((dotlockLockingContext *)pNew->lockingContext)->lockPath = 
          sqlite3_malloc( nFilename );
      sqlite3_snprintf(nFilename, 
              ((dotlockLockingContext *)pNew->lockingContext)->lockPath, 
              "%s.lock", zFilename);
      break;
    }
    case posixLockingStyle:
      /* posix locking doesn't need additional lockingContext information */
      pNew->pMethod = &sqlite3UnixIoMethod;
      break;
    case noLockingStyle:
    case unsupportedLockingStyle:
    default: 
      pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
  }
  OpenCounter(+1);
  return SQLITE_OK;

}
#else /* SQLITE_ENABLE_LOCKING_STYLE */
static int fillInUnixFile(
  int h,                 /* Open file descriptor on file being opened */
  int dirfd,
  sqlite3_file *pId,     /* Write to the unixFile structure here */
  const char *zFilename  /* Name of the file being opened */