1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-12 13:01:09 +03:00

Cleanup the locking-style code in os_unix.c. (CVS 4837)

FossilOrigin-Name: 40f55c09dbd31f861b9f9c7641cce92553d94e35
This commit is contained in:
drh
2008-03-07 15:34:11 +00:00
parent b279941f73
commit 339eb0b8af
3 changed files with 116 additions and 127 deletions

View File

@@ -1,5 +1,5 @@
C Trivial\sdocumentation\sfixes\s(CVS\s4836) C Cleanup\sthe\slocking-style\scode\sin\sos_unix.c.\s(CVS\s4837)
D 2008-03-07T03:20:32 D 2008-03-07T15:34:11
F Makefile.arm-wince-mingw32ce-gcc ac5f7b2cef0cd850d6f755ba6ee4ab961b1fadf7 F Makefile.arm-wince-mingw32ce-gcc ac5f7b2cef0cd850d6f755ba6ee4ab961b1fadf7
F Makefile.in d521464011d6965bbda1b699f1850c6e33141c73 F Makefile.in d521464011d6965bbda1b699f1850c6e33141c73
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -125,7 +125,7 @@ F src/os_os2.c 10b23539e0050bdfc9f136242086a5c18c70c6f8
F src/os_os2.h c3f7d0af7e3453d1d7aa81b06c0a56f5a226530b F src/os_os2.h c3f7d0af7e3453d1d7aa81b06c0a56f5a226530b
F src/os_test.c 49833426101f99aee4bb5f6a44b7c4b2029fda1c F src/os_test.c 49833426101f99aee4bb5f6a44b7c4b2029fda1c
F src/os_test.h 903c93554c23d88f34f667f1979e4a1cee792af3 F src/os_test.h 903c93554c23d88f34f667f1979e4a1cee792af3
F src/os_unix.c b21695b8cc529f9aad7507bdc0d31cfa72623159 F src/os_unix.c bf339cd689054c8edc603c0209d91f62444c1477
F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e
F src/os_win.c aa3f4bbee3b8c182d25a33fbc319f486857c12c1 F src/os_win.c aa3f4bbee3b8c182d25a33fbc319f486857c12c1
F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b
@@ -623,7 +623,7 @@ F www/tclsqlite.tcl 8be95ee6dba05eabcd27a9d91331c803f2ce2130
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5 F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5
P 4cda019b63e8ba9f1101fb296a7d745c69e84e00 P 9819cefbd7032fe6884c6c891e8e399000e0821f
R c15bb445f4066eaf2e9cf0566419d3fb R 94e0b03717c8cb302d6990c16331816e
U mlcreech U drh
Z 485b426fb4816fce857c16a85c39d37f Z c7afd0fbb52cbc7669e22683bc9d24e9

View File

@@ -1 +1 @@
9819cefbd7032fe6884c6c891e8e399000e0821f 40f55c09dbd31f861b9f9c7641cce92553d94e35

View File

@@ -581,31 +581,31 @@ static sqlite3LockingStyle sqlite3DetectLockingStyle(
#else #else
struct statfs fsInfo; struct statfs fsInfo;
if (statfs(filePath, &fsInfo) == -1) if( statfs(filePath, &fsInfo) == -1 ){
return sqlite3TestLockingStyle(filePath, fd); return sqlite3TestLockingStyle(filePath, fd);
}
if (fsInfo.f_flags & MNT_RDONLY) if( fsInfo.f_flags & MNT_RDONLY ){
return noLockingStyle; return noLockingStyle;
}
if( (!strcmp(fsInfo.f_fstypename, "hfs")) || if( strcmp(fsInfo.f_fstypename, "hfs")==0 ||
(!strcmp(fsInfo.f_fstypename, "ufs")) ) strcmp(fsInfo.f_fstypename, "ufs")==0 ){
return posixLockingStyle; return posixLockingStyle;
}
if(!strcmp(fsInfo.f_fstypename, "afpfs")) if( strcmp(fsInfo.f_fstypename, "afpfs")==0 ){
return afpLockingStyle; return afpLockingStyle;
}
if(!strcmp(fsInfo.f_fstypename, "nfs")) if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
return sqlite3TestLockingStyle(filePath, fd); return sqlite3TestLockingStyle(filePath, fd);
}
if(!strcmp(fsInfo.f_fstypename, "smbfs")) if( strcmp(fsInfo.f_fstypename, "smbfs")==0 ){
return flockLockingStyle; return flockLockingStyle;
}
if(!strcmp(fsInfo.f_fstypename, "msdos")) if( strcmp(fsInfo.f_fstypename, "msdos")==0 ){
return dotlockLockingStyle; return dotlockLockingStyle;
}
if(!strcmp(fsInfo.f_fstypename, "webdav")) if( strcmp(fsInfo.f_fstypename, "webdav")==0 ){
return unsupportedLockingStyle; return unsupportedLockingStyle;
}
return sqlite3TestLockingStyle(filePath, fd); return sqlite3TestLockingStyle(filePath, fd);
#endif /* SQLITE_FIXED_LOCKING_STYLE */ #endif /* SQLITE_FIXED_LOCKING_STYLE */
} }
@@ -1454,8 +1454,8 @@ static int unixClose(sqlite3_file *id){
*/ */
typedef struct afpLockingContext afpLockingContext; typedef struct afpLockingContext afpLockingContext;
struct afpLockingContext { struct afpLockingContext {
unsigned long long sharedLockByte; unsigned long long sharedLockByte; /* Byte offset of shared lock byte */
char *filePath; const char *filePath; /* Name of the file */
}; };
struct ByteRangeLockPB2 struct ByteRangeLockPB2
@@ -1542,8 +1542,7 @@ static int afpUnixCheckReservedLock(sqlite3_file *id){
/* AFP-style locking following the behavior of unixLock, see the unixLock /* AFP-style locking following the behavior of unixLock, see the unixLock
** function comments for details of lock management. */ ** function comments for details of lock management. */
static int afpUnixLock(sqlite3_file *id, int locktype) static int afpUnixLock(sqlite3_file *id, int locktype){
{
int rc = SQLITE_OK; int rc = SQLITE_OK;
unixFile *pFile = (unixFile*)id; unixFile *pFile = (unixFile*)id;
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
@@ -1552,6 +1551,7 @@ static int afpUnixLock(sqlite3_file *id, int locktype)
assert( pFile ); assert( pFile );
OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h, OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
locktypeName(locktype), locktypeName(pFile->locktype), getpid()); locktypeName(locktype), locktypeName(pFile->locktype), getpid());
/* If there is already a lock of this type or more restrictive on the /* If there is already a lock of this type or more restrictive on the
** unixFile, do nothing. Don't use the afp_end_lock: exit path, as ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
** enterMutex() hasn't been called yet. ** enterMutex() hasn't been called yet.
@@ -1587,8 +1587,8 @@ static int afpUnixLock(sqlite3_file *id, int locktype)
if( locktype==SHARED_LOCK if( locktype==SHARED_LOCK
|| (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
){ ){
int failed = _AFPFSSetLock(context->filePath, pFile->h, int failed;
PENDING_BYTE, 1, 1); failed = _AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 1);
if (failed) { if (failed) {
rc = SQLITE_BUSY; rc = SQLITE_BUSY;
goto afp_end_lock; goto afp_end_lock;
@@ -1749,18 +1749,10 @@ static int afpUnixClose(sqlite3_file *id) {
if( !pFile ) return SQLITE_OK; if( !pFile ) return SQLITE_OK;
afpUnixUnlock(id, NO_LOCK); 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); sqlite3_free(pFile->lockingContext);
}
if( pFile->dirfd>=0 ) close(pFile->dirfd); if( pFile->dirfd>=0 ) close(pFile->dirfd);
pFile->dirfd = -1; pFile->dirfd = -1;
enterMutex();
close(pFile->h); close(pFile->h);
leaveMutex();
OSTRACE2("CLOSE %-3d\n", pFile->h); OSTRACE2("CLOSE %-3d\n", pFile->h);
OpenCounter(-1); OpenCounter(-1);
memset(pFile, 0, sizeof(unixFile)); memset(pFile, 0, sizeof(unixFile));
@@ -1851,10 +1843,7 @@ static int flockUnixClose(sqlite3_file *id) {
if( pFile->dirfd>=0 ) close(pFile->dirfd); if( pFile->dirfd>=0 ) close(pFile->dirfd);
pFile->dirfd = -1; pFile->dirfd = -1;
enterMutex();
close(pFile->h); close(pFile->h);
leaveMutex();
OSTRACE2("CLOSE %-3d\n", pFile->h); OSTRACE2("CLOSE %-3d\n", pFile->h);
OpenCounter(-1); OpenCounter(-1);
memset(pFile, 0, sizeof(unixFile)); memset(pFile, 0, sizeof(unixFile));
@@ -1875,26 +1864,29 @@ struct dotlockLockingContext {
static int dotlockUnixCheckReservedLock(sqlite3_file *id) { static int dotlockUnixCheckReservedLock(sqlite3_file *id) {
unixFile *pFile = (unixFile*)id; unixFile *pFile = (unixFile*)id;
dotlockLockingContext *context = dotlockLockingContext *context;
(dotlockLockingContext *) pFile->lockingContext;
context = (dotlockLockingContext*)pFile->lockingContext;
if (pFile->locktype == RESERVED_LOCK) { if (pFile->locktype == RESERVED_LOCK) {
return 1; /* already have a reserved lock */ return 1; /* already have a reserved lock */
} else { } else {
struct stat statBuf; struct stat statBuf;
if (lstat(context->lockPath,&statBuf) == 0) if (lstat(context->lockPath,&statBuf) == 0){
/* file exists, someone else has the lock */ /* file exists, someone else has the lock */
return 1; return 1;
else }else{
/* file does not exist, we could have it if we want it */ /* file does not exist, we could have it if we want it */
return 0; return 0;
} }
} }
}
static int dotlockUnixLock(sqlite3_file *id, int locktype) { static int dotlockUnixLock(sqlite3_file *id, int locktype) {
unixFile *pFile = (unixFile*)id; unixFile *pFile = (unixFile*)id;
dotlockLockingContext *context = dotlockLockingContext *context;
(dotlockLockingContext *) pFile->lockingContext; int fd;
context = (dotlockLockingContext*)pFile->lockingContext;
/* if we already have a lock, it is exclusive. /* if we already have a lock, it is exclusive.
** Just adjust level and punt on outta here. */ ** Just adjust level and punt on outta here. */
@@ -1913,7 +1905,7 @@ static int dotlockUnixLock(sqlite3_file *id, int locktype) {
} }
/* grab an exclusive lock */ /* grab an exclusive lock */
int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600); fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600);
if( fd<0 ){ if( fd<0 ){
/* failed to open/create the file, someone else may have stolen the lock */ /* failed to open/create the file, someone else may have stolen the lock */
return SQLITE_BUSY; return SQLITE_BUSY;
@@ -1927,8 +1919,9 @@ static int dotlockUnixLock(sqlite3_file *id, int locktype) {
static int dotlockUnixUnlock(sqlite3_file *id, int locktype) { static int dotlockUnixUnlock(sqlite3_file *id, int locktype) {
unixFile *pFile = (unixFile*)id; unixFile *pFile = (unixFile*)id;
dotlockLockingContext *context = dotlockLockingContext *context;
(dotlockLockingContext *) pFile->lockingContext;
context = (dotlockLockingContext*)pFile->lockingContext;
assert( locktype<=SHARED_LOCK ); assert( locktype<=SHARED_LOCK );
@@ -1957,19 +1950,10 @@ static int dotlockUnixClose(sqlite3_file *id) {
if( !pFile ) return SQLITE_OK; if( !pFile ) return SQLITE_OK;
dotlockUnixUnlock(id, NO_LOCK); 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); sqlite3_free(pFile->lockingContext);
}
if( pFile->dirfd>=0 ) close(pFile->dirfd); if( pFile->dirfd>=0 ) close(pFile->dirfd);
pFile->dirfd = -1; pFile->dirfd = -1;
enterMutex();
close(pFile->h); close(pFile->h);
leaveMutex();
OSTRACE2("CLOSE %-3d\n", pFile->h); OSTRACE2("CLOSE %-3d\n", pFile->h);
OpenCounter(-1); OpenCounter(-1);
memset(pFile, 0, sizeof(unixFile)); memset(pFile, 0, sizeof(unixFile));
@@ -2005,9 +1989,7 @@ static int nolockUnixClose(sqlite3_file *id) {
if( !pFile ) return SQLITE_OK; if( !pFile ) return SQLITE_OK;
if( pFile->dirfd>=0 ) close(pFile->dirfd); if( pFile->dirfd>=0 ) close(pFile->dirfd);
pFile->dirfd = -1; pFile->dirfd = -1;
enterMutex();
close(pFile->h); close(pFile->h);
leaveMutex();
OSTRACE2("CLOSE %-3d\n", pFile->h); OSTRACE2("CLOSE %-3d\n", pFile->h);
OpenCounter(-1); OpenCounter(-1);
memset(pFile, 0, sizeof(unixFile)); memset(pFile, 0, sizeof(unixFile));
@@ -2134,7 +2116,7 @@ static const sqlite3_io_methods sqlite3DotlockLockingUnixIoMethod = {
/* /*
** This vector defines all the methods that can operate on an sqlite3_file ** This vector defines all the methods that can operate on an sqlite3_file
** for unix with dotlock style file locking. ** for unix with nolock style file locking.
*/ */
static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = { static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = {
1, /* iVersion */ 1, /* iVersion */
@@ -2182,7 +2164,6 @@ static int fillInUnixFile(
lockingStyle = sqlite3DetectLockingStyle(zFilename, h); lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
if ( lockingStyle==posixLockingStyle ){ if ( lockingStyle==posixLockingStyle ){
enterMutex(); enterMutex();
rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen); rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
leaveMutex(); leaveMutex();
@@ -2207,15 +2188,19 @@ static int fillInUnixFile(
case afpLockingStyle: { case afpLockingStyle: {
/* afp locking uses the file path so it needs to be included in /* afp locking uses the file path so it needs to be included in
** the afpLockingContext */ ** the afpLockingContext */
int nFilename; afpLockingContext *context;
pNew->pMethod = &sqlite3AFPLockingUnixIoMethod; pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
pNew->lockingContext = pNew->lockingContext = context = sqlite3_malloc( sizeof(*context) );
sqlite3_malloc(sizeof(afpLockingContext)); if( context==0 ){
nFilename = strlen(zFilename)+1; close(h);
((afpLockingContext *)pNew->lockingContext)->filePath = if( dirfd>=0 ) close(dirfd);
sqlite3_malloc(nFilename); return SQLITE_NOMEM;
memcpy(((afpLockingContext *)pNew->lockingContext)->filePath, }
zFilename, nFilename);
/* NB: zFilename exists and remains valid until the file is closed
** according to requirement F11141. So we do not need to make a
** copy of the filename. */
context->filePath = zFilename;
srandomdev(); srandomdev();
break; break;
} }
@@ -2226,15 +2211,19 @@ static int fillInUnixFile(
case dotlockLockingStyle: { case dotlockLockingStyle: {
/* dotlock locking uses the file path so it needs to be included in /* dotlock locking uses the file path so it needs to be included in
** the dotlockLockingContext */ ** the dotlockLockingContext */
dotlockLockingContext *context;
int nFilename; int nFilename;
nFilename = strlen(zFilename);
pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod; pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
pNew->lockingContext = sqlite3_malloc( pNew->lockingContext = context =
sizeof(dotlockLockingContext)); sqlite3_malloc( sizeof(*context) + nFilename + 6 );
nFilename = strlen(zFilename) + 6; if( context==0 ){
((dotlockLockingContext *)pNew->lockingContext)->lockPath = close(h);
sqlite3_malloc( nFilename ); if( dirfd>=0 ) close(dirfd);
sqlite3_snprintf(nFilename, return SQLITE_NOMEM;
((dotlockLockingContext *)pNew->lockingContext)->lockPath, }
context->lockPath = (char*)&context[1];
sqlite3_snprintf(nFilename, context->lockPath,
"%s.lock", zFilename); "%s.lock", zFilename);
break; break;
} }