diff --git a/VERSION b/VERSION
index cd57a8b95d..399088bf46 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.1.5
+2.1.6
diff --git a/manifest b/manifest
index 0e62737094..ff97e2b19f 100644
--- a/manifest
+++ b/manifest
@@ -1,9 +1,9 @@
-C Version\s2.1.5\s(CVS\s456)
-D 2001-12-06T13:30:00
+C Fix\sa\srace\scondition\sin\sthe\slocking\scode\sthat\swould\ssometimes\scause\nSQLITE_PROTOCOL\sor\sSQLITE_CORRUPT\sto\sbe\sreturned\swhen\sSQLITE_BUSY\sshould\nhave\sbeen\sreturned.\s(CVS\s326)
+D 2001-12-14T15:09:56
F Makefile.in 352fed589f09dd94347e0bb391d047118ebd6105
F Makefile.template 0fbf0ee1fe38183d760170a13e91fffec64e73f5
F README a4c0ba11354ef6ba0776b400d057c59da47a4cc0
-F VERSION 592b381154fbd36565cdbefe0075aa43623f2f5a
+F VERSION 1943794ff83649775b2cc2e31bf3e3390e952fbf
F aclocal.m4 11faa843caa38fd451bc6aeb43e248d1723a269d
F config.guess f38b1e93d1e0fa6f5a6913e9e7b12774b9232588
F config.log 6a73d03433669b10a3f0c221198c3f26b9413914
@@ -19,7 +19,7 @@ F libtool c56e618713c9510a103bda6b95f3ea3900dcacd6
F ltmain.sh e9ed72eb1d690f447c13945eaf69e28af531eda1
F publish.sh 33cbe6798969f637698044023c139080e5d772a6
F src/TODO af7f3cab0228e34149cf98e073aa83d45878e7e6
-F src/btree.c 5fbfeb383465ca311ac202cbf6ba8f18196426de
+F src/btree.c e4ea48618c136694de21e96fea0ed9a3d79c7996
F src/btree.h 0250a0a577a98cc64ddf1582d50c08b8d2451650
F src/build.c 83733f96255db003363e786d1b28a5b85611acca
F src/delete.c 5d93a21c1388cfb1359bda01c072f25583a2f4f2
@@ -29,9 +29,9 @@ F src/hash.h a5f5b3ce2d086a172c5879b0b06a27a82eac9fac
F src/insert.c 3526be771a01035198bef28d8f370cbcab94f46d
F src/main.c e5fa4773e6684b81fc0bcd9d9ae4578d56660c0c
F src/md5.c 52f677bfc590e09f71d07d7e327bd59da738d07c
-F src/os.c f4d19ca9202bfd496a07d2d55e1c4b57b6e5b48c
-F src/os.h c5c12f5f25b0a1baf9a62937b456fabcad2c1f49
-F src/pager.c 16173a7b7aff0c7bd7da94070e46d38778ab0f76
+F src/os.c 07882cde5c61f26751b8ee76fd84726c1f7e453c
+F src/os.h 00a18e0ae1139a64f1d3ead465ae2b9ff43f3db2
+F src/pager.c f39d99e9339e5fff3fd9852f48d2fc8308933d3b
F src/pager.h df1fb8a759ab69112ea88b9f14601a7633d0ccc0
F src/parse.y 5295f393f41ea89958287e5738e6c12c7cd67482
F src/printf.c 300a90554345751f26e1fc0c0333b90a66110a1d
@@ -104,7 +104,7 @@ F www/arch.fig d5f9752a4dbf242e9cfffffd3f5762b6c63b3bcf
F www/arch.png 82ef36db1143828a7abc88b1e308a5f55d4336f4
F www/arch.tcl 72a0c80e9054cc7025a50928d28d9c75c02c2b8b
F www/c_interface.tcl 58922228e8fdb0f6af3561a051ee8ccec6dbfd17
-F www/changes.tcl 40bf58e136891ce1e5a4bda00d1c59f161560c59
+F www/changes.tcl e0be983df11a904e14def78526509080a57fca98
F www/crosscompile.tcl 3622ebbe518927a3854a12de51344673eb2dd060
F www/download.tcl 1ea61f9d89a2a5a9b2cee36b0d5cf97321bdefe0
F www/dynload.tcl 02eb8273aa78cfa9070dd4501dca937fb22b466c
@@ -117,7 +117,7 @@ F www/speed.tcl 83457b2bf6bb430900bd48ca3dd98264d9a916a5
F www/sqlite.tcl 8b5884354cb615049aed83039f8dfe1552a44279
F www/tclsqlite.tcl 880ef67cb4f2797b95bf1368fc4e0d8ca0fda956
F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218
-P 669454060867593290c1ce8c45bd87d011976289
-R 283460d2e743d411ca396b1d81679e15
+P 8e90ad552fad83568297202848fe7eb2b63db33d
+R 11fe1b88a51d4db88bdca916a2550755
U drh
-Z 8fa34c42ea83abd5884352fa7c87327e
+Z fba78b88b4bbf12a6828c6912be362f4
diff --git a/manifest.uuid b/manifest.uuid
index 5c23c37b24..0f1a3a5806 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-8e90ad552fad83568297202848fe7eb2b63db33d
\ No newline at end of file
+b0d218876442187af08161d989e6887b1cb4130c
\ No newline at end of file
diff --git a/src/btree.c b/src/btree.c
index 00f898202f..2028c896ec 100644
--- a/src/btree.c
+++ b/src/btree.c
@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
-** $Id: btree.c,v 1.42 2001/12/05 00:21:20 drh Exp $
+** $Id: btree.c,v 1.43 2001/12/14 15:09:57 drh Exp $
**
** This file implements a external (disk-based) database using BTrees.
** For a detailed discussion of BTrees, refer to
@@ -990,7 +990,10 @@ static int getPayload(BtCursor *pCur, int offset, int amt, char *zBuf){
}
sqlitepager_unref(pOvfl);
}
- return amt==0 ? SQLITE_OK : SQLITE_CORRUPT;
+ if( amt>0 ){
+ return SQLITE_CORRUPT;
+ }
+ return SQLITE_OK;
}
/*
diff --git a/src/os.c b/src/os.c
index 50d443267f..c192d377de 100644
--- a/src/os.c
+++ b/src/os.c
@@ -108,7 +108,7 @@ struct inodeKey {
*/
struct lockInfo {
struct inodeKey key; /* The lookup key */
- int cnt; /* 0: unlocked. -1: write lock. >=1: read lock */
+ int cnt; /* 0: unlocked. -1: write lock. 1...: read lock. */
int nRef; /* Number of pointers to this structure */
};
@@ -227,7 +227,7 @@ int sqliteOsFileExists(const char *zFilename){
** fails, try opening it read-only. If the file does not exist,
** try to create it.
**
-** On success, a handle for the open file is written to *pResult
+** On success, a handle for the open file is written to *id
** and *pReadonly is set to 0 if the file was opened for reading and
** writing or 1 if the file was opened read-only. The function returns
** SQLITE_OK.
@@ -237,15 +237,14 @@ int sqliteOsFileExists(const char *zFilename){
*/
int sqliteOsOpenReadWrite(
const char *zFilename,
- OsFile *pResult,
+ OsFile *id,
int *pReadonly
){
#if OS_UNIX
- OsFile s;
- s.fd = open(zFilename, O_RDWR|O_CREAT, 0644);
- if( s.fd<0 ){
- s.fd = open(zFilename, O_RDONLY);
- if( s.fd<0 ){
+ id->fd = open(zFilename, O_RDWR|O_CREAT, 0644);
+ if( id->fd<0 ){
+ id->fd = open(zFilename, O_RDONLY);
+ if( id->fd<0 ){
return SQLITE_CANTOPEN;
}
*pReadonly = 1;
@@ -253,13 +252,13 @@ int sqliteOsOpenReadWrite(
*pReadonly = 0;
}
sqliteOsEnterMutex();
- s.pLock = findLockInfo(s.fd);
+ id->pLock = findLockInfo(id->fd);
sqliteOsLeaveMutex();
- if( s.pLock==0 ){
- close(s.fd);
+ if( id->pLock==0 ){
+ close(id->fd);
return SQLITE_NOMEM;
}
- *pResult = s;
+ id->locked = 0;
return SQLITE_OK;
#endif
#if OS_WIN
@@ -287,7 +286,8 @@ int sqliteOsOpenReadWrite(
}else{
*pReadonly = 0;
}
- *pResult = h;
+ id->h = h;
+ id->locked = 0;
return SQLITE_OK;
#endif
}
@@ -300,32 +300,31 @@ int sqliteOsOpenReadWrite(
** previously existed. Nor do we allow the file to be a symbolic
** link.
**
-** On success, write the file handle into *pResult and return SQLITE_OK.
+** On success, write the file handle into *id and return SQLITE_OK.
**
** On failure, return SQLITE_CANTOPEN.
*/
-int sqliteOsOpenExclusive(const char *zFilename, OsFile *pResult){
+int sqliteOsOpenExclusive(const char *zFilename, OsFile *id){
#if OS_UNIX
- OsFile s;
if( access(zFilename, 0)==0 ){
return SQLITE_CANTOPEN;
}
#ifndef O_NOFOLLOW
# define O_NOFOLLOW 0
#endif
- s.fd = open(zFilename, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600);
- if( s.fd<0 ){
+ id->fd = open(zFilename, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600);
+ if( id->fd<0 ){
return SQLITE_CANTOPEN;
}
sqliteOsEnterMutex();
- s.pLock = findLockInfo(s.fd);
+ id->pLock = findLockInfo(id->fd);
sqliteOsLeaveMutex();
- if( s.pLock==0 ){
- close(s.fd);
+ if( id->pLock==0 ){
+ close(id->fd);
unlink(zFilename);
return SQLITE_NOMEM;
}
- *pResult = s;
+ id->locked = 0;
return SQLITE_OK;
#endif
#if OS_WIN
@@ -340,7 +339,8 @@ int sqliteOsOpenExclusive(const char *zFilename, OsFile *pResult){
if( h==INVALID_HANDLE_VALUE ){
return SQLITE_CANTOPEN;
}
- *pResult = h;
+ id->h = h;
+ id->locked = 0;
return SQLITE_OK;
#endif
}
@@ -348,25 +348,24 @@ int sqliteOsOpenExclusive(const char *zFilename, OsFile *pResult){
/*
** Attempt to open a new file for read-only access.
**
-** On success, write the file handle into *pResult and return SQLITE_OK.
+** On success, write the file handle into *id and return SQLITE_OK.
**
** On failure, return SQLITE_CANTOPEN.
*/
-int sqliteOsOpenReadOnly(const char *zFilename, OsFile *pResult){
+int sqliteOsOpenReadOnly(const char *zFilename, OsFile *id){
#if OS_UNIX
- OsFile s;
- s.fd = open(zFilename, O_RDONLY);
- if( s.fd<0 ){
+ id->fd = open(zFilename, O_RDONLY);
+ if( id->fd<0 ){
return SQLITE_CANTOPEN;
}
sqliteOsEnterMutex();
- s.pLock = findLockInfo(s.fd);
+ id->pLock = findLockInfo(id->fd);
sqliteOsLeaveMutex();
- if( s.pLock==0 ){
- close(s.fd);
+ if( id->pLock==0 ){
+ close(id->fd);
return SQLITE_NOMEM;
}
- *pResult = s;
+ id->locked = 0;
return SQLITE_OK;
#endif
#if OS_WIN
@@ -381,7 +380,8 @@ int sqliteOsOpenReadOnly(const char *zFilename, OsFile *pResult){
if( h==INVALID_HANDLE_VALUE ){
return SQLITE_CANTOPEN;
}
- *pResult = h;
+ id->h = h;
+ id->locked = 0;
return SQLITE_OK;
#endif
}
@@ -447,16 +447,16 @@ int sqliteOsTempFileName(char *zBuf){
/*
** Close a file
*/
-int sqliteOsClose(OsFile id){
+int sqliteOsClose(OsFile *id){
#if OS_UNIX
- close(id.fd);
+ close(id->fd);
sqliteOsEnterMutex();
- releaseLockInfo(id.pLock);
+ releaseLockInfo(id->pLock);
sqliteOsLeaveMutex();
return SQLITE_OK;
#endif
#if OS_WIN
- CloseHandle(id);
+ CloseHandle(id->h);
return SQLITE_OK;
#endif
}
@@ -466,18 +466,18 @@ int sqliteOsClose(OsFile id){
** bytes were read successfully and SQLITE_IOERR if anything goes
** wrong.
*/
-int sqliteOsRead(OsFile id, void *pBuf, int amt){
+int sqliteOsRead(OsFile *id, void *pBuf, int amt){
#if OS_UNIX
int got;
SimulateIOError(SQLITE_IOERR);
- got = read(id.fd, pBuf, amt);
+ got = read(id->fd, pBuf, amt);
if( got<0 ) got = 0;
return got==amt ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
DWORD got;
SimulateIOError(SQLITE_IOERR);
- if( !ReadFile(id, pBuf, amt, &got, 0) ){
+ if( !ReadFile(id->h, pBuf, amt, &got, 0) ){
got = 0;
}
return got==amt ? SQLITE_OK : SQLITE_IOERR;
@@ -488,18 +488,18 @@ int sqliteOsRead(OsFile id, void *pBuf, int amt){
** Write data from a buffer into a file. Return SQLITE_OK on success
** or some other error code on failure.
*/
-int sqliteOsWrite(OsFile id, const void *pBuf, int amt){
+int sqliteOsWrite(OsFile *id, const void *pBuf, int amt){
#if OS_UNIX
int wrote;
SimulateIOError(SQLITE_IOERR);
- wrote = write(id.fd, pBuf, amt);
+ wrote = write(id->fd, pBuf, amt);
if( wroteh, pBuf, amt, &wrote, 0) || wrotefd, offset, SEEK_SET);
return SQLITE_OK;
#endif
#if OS_WIN
- SetFilePointer(id, offset, 0, FILE_BEGIN);
+ SetFilePointer(id->h, offset, 0, FILE_BEGIN);
return SQLITE_OK;
#endif
}
@@ -523,27 +523,27 @@ int sqliteOsSeek(OsFile id, int offset){
/*
** Make sure all writes to a particular file are committed to disk.
*/
-int sqliteOsSync(OsFile id){
+int sqliteOsSync(OsFile *id){
SimulateIOError(SQLITE_IOERR);
#if OS_UNIX
- return fsync(id.fd)==0 ? SQLITE_OK : SQLITE_IOERR;
+ return fsync(id->fd)==0 ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
- return FlushFileBuffers(id) ? SQLITE_OK : SQLITE_IOERR;
+ return FlushFileBuffers(id->h) ? SQLITE_OK : SQLITE_IOERR;
#endif
}
/*
** Truncate an open file to a specified size
*/
-int sqliteOsTruncate(OsFile id, int nByte){
+int sqliteOsTruncate(OsFile *id, int nByte){
SimulateIOError(SQLITE_IOERR);
#if OS_UNIX
- return ftruncate(id.fd, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
+ return ftruncate(id->fd, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
- SetFilePointer(id, nByte, 0, FILE_BEGIN);
- SetEndOfFile(id);
+ SetFilePointer(id->h, nByte, 0, FILE_BEGIN);
+ SetEndOfFile(id->h);
return SQLITE_OK;
#endif
}
@@ -551,11 +551,11 @@ int sqliteOsTruncate(OsFile id, int nByte){
/*
** Determine the current size of a file in bytes
*/
-int sqliteOsFileSize(OsFile id, int *pSize){
+int sqliteOsFileSize(OsFile *id, int *pSize){
#if OS_UNIX
struct stat buf;
SimulateIOError(SQLITE_IOERR);
- if( fstat(id.fd, &buf)!=0 ){
+ if( fstat(id->fd, &buf)!=0 ){
return SQLITE_IOERR;
}
*pSize = buf.st_size;
@@ -563,89 +563,141 @@ int sqliteOsFileSize(OsFile id, int *pSize){
#endif
#if OS_WIN
SimulateIOError(SQLITE_IOERR);
- *pSize = GetFileSize(id, 0);
+ *pSize = GetFileSize(id->h, 0);
return SQLITE_OK;
#endif
}
/*
-** Get a read or write lock on a file.
+** Change the status of the lock on the file "id" to be a readlock.
+** If the file was write locked, then this reduces the lock to a read.
+** If the file was read locked, then this acquires a new read lock.
+**
+** Return SQLITE_OK on success and SQLITE_BUSY on failure.
*/
-int sqliteOsLock(OsFile id, int wrlock){
+int sqliteOsReadLock(OsFile *id){
#if OS_UNIX
int rc;
- int needSysLock;
sqliteOsEnterMutex();
- if( wrlock ){
- if( id.pLock->cnt!=0 ){
- rc = SQLITE_BUSY;
- }else{
- rc = SQLITE_OK;
- id.pLock->cnt = -1;
- needSysLock = 1;
+ if( id->pLock->cnt>0 ){
+ if( !id->locked ){
+ id->pLock->cnt++;
+ id->locked = 1;
}
- }else{
- if( id.pLock->cnt<0 ){
- rc = SQLITE_BUSY;
- }else{
- rc = SQLITE_OK;
- needSysLock = id.pLock->cnt==0;
- id.pLock->cnt++;
- }
- }
- if( rc==SQLITE_OK && needSysLock ){
- struct flock lock;
- lock.l_type = wrlock ? F_WRLCK : F_RDLCK;
- lock.l_whence = SEEK_SET;
- lock.l_start = lock.l_len = 0L;
- rc = fcntl(id.fd, F_SETLK, &lock)==0 ? SQLITE_OK : SQLITE_BUSY;
- if( rc ){
- id.pLock->cnt = 0;
- }
- }
- sqliteOsLeaveMutex();
- return rc;
-#endif
-#if OS_WIN
- if( !LockFile(id, 0, 0, 1024, 0) ){
- return SQLITE_BUSY;
- }
- return SQLITE_OK;
-#endif
-}
-
-/*
-** Release the read or write lock from a file.
-*/
-int sqliteOsUnlock(OsFile id){
-#if OS_UNIX
- int rc;
- int needSysUnlock;
-
- sqliteOsEnterMutex();
- if( id.pLock->cnt<0 ){
- needSysUnlock = 1;
- id.pLock->cnt = 0;
- }else if( id.pLock->cnt>0 ){
- id.pLock->cnt--;
- needSysUnlock = id.pLock->cnt==0;
- }else{
rc = SQLITE_OK;
- needSysUnlock = 0;
- }
- if( needSysUnlock ){
+ }else if( id->locked || id->pLock->cnt==0 ){
struct flock lock;
- lock.l_type = F_UNLCK;
+ lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
- rc = fcntl(id.fd, F_SETLK, &lock)==0 ? SQLITE_OK : SQLITE_IOERR;
+ if( fcntl(id->fd, F_SETLK, &lock)!=0 ){
+ rc = SQLITE_BUSY;
+ }else{
+ rc = SQLITE_OK;
+ id->pLock->cnt = 1;
+ id->locked = 1;
+ }
+ }else{
+ rc = SQLITE_BUSY;
}
sqliteOsLeaveMutex();
return rc;
#endif
#if OS_WIN
- return UnlockFile(id, 0, 0, 1024, 0) ? SQLITE_OK : SQLITE_IOERR;
+ int rc;
+ if( id->locked ){
+ rc = SQLITE_OK;
+ }else if( LockFile(id->h, 0, 0, 1024, 0) ){
+ rc = SQLITE_OK;
+ id->locked = 1;
+ }else{
+ rc = SQLITE_BUSY;
+ }
+ return rc;
+#endif
+}
+
+/*
+** Change the lock status to be an exclusive or write lock. Return
+** SQLITE_OK on success and SQLITE_BUSY on a failure.
+*/
+int sqliteOsWriteLock(OsFile *id){
+#if OS_UNIX
+ int rc;
+ sqliteOsEnterMutex();
+ if( id->pLock->cnt==0 || (id->pLock->cnt==1 && id->locked==1) ){
+ struct flock lock;
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = lock.l_len = 0L;
+ if( fcntl(id->fd, F_SETLK, &lock)!=0 ){
+ rc = SQLITE_BUSY;
+ }else{
+ rc = SQLITE_OK;
+ id->pLock->cnt = -1;
+ id->locked = 1;
+ }
+ }else{
+ rc = SQLITE_BUSY;
+ }
+ sqliteOsLeaveMutex();
+ return rc;
+#endif
+#if OS_WIN
+ int rc;
+ if( id->locked ){
+ rc = SQLITE_OK;
+ }else if( LockFile(id->h, 0, 0, 1024, 0) ){
+ rc = SQLITE_OK;
+ id->locked = 1;
+ }else{
+ rc = SQLITE_BUSY;
+ }
+ return rc;
+#endif
+}
+
+/*
+** Unlock the given file descriptor. If the file descriptor was
+** not previously locked, then this routine is a no-op.
+*/
+int sqliteOsUnlock(OsFile *id){
+#if OS_UNIX
+ int rc;
+ if( !id->locked ) return SQLITE_OK;
+ sqliteOsEnterMutex();
+ assert( id->pLock->cnt!=0 );
+ if( id->pLock->cnt>1 ){
+ id->pLock->cnt--;
+ rc = SQLITE_OK;
+ }else{
+ struct flock lock;
+ lock.l_type = F_UNLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = lock.l_len = 0L;
+ if( fcntl(id->fd, F_SETLK, &lock)!=0 ){
+ rc = SQLITE_BUSY;
+ }else{
+ rc = SQLITE_OK;
+ id->pLock->cnt = 0;
+ }
+ }
+ sqliteOsLeaveMutex();
+ id->locked = 0;
+ return rc;
+#endif
+#if OS_WIN
+ int rc;
+ if( !id->locked ){
+ rc = SQLITE_OK;
+ }else if( UnlockFile(id->h, 0, 0, 1024, 0) ){
+ rc = SQLITE_OK;
+ id->locked = 0;
+ }else{
+ rc = SQLITE_BUSY;
+ }
+ return rc;
#endif
}
diff --git a/src/os.h b/src/os.h
index f90b525890..d145475941 100644
--- a/src/os.h
+++ b/src/os.h
@@ -25,6 +25,7 @@
struct OsFile {
struct lockInfo *pLock; /* Information about locks on this inode */
int fd; /* The file descriptor */
+ int locked; /* True if this user holds the lock */
};
# define SQLITE_TEMPNAME_SIZE 200
# if defined(HAVE_USLEEP) && HAVE_USLEEP
@@ -37,7 +38,11 @@
#if OS_WIN
#include
#include
- typedef HANDLE OsFile;
+ typedef struct OsFile OsFile;
+ struct OsFile {
+ HANDLE h;
+ int locked;
+ };
# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
# define SQLITE_MIN_SLEEP_MS 1
#endif
@@ -48,15 +53,16 @@ int sqliteOsOpenReadWrite(const char*, OsFile*, int*);
int sqliteOsOpenExclusive(const char*, OsFile*);
int sqliteOsOpenReadOnly(const char*, OsFile*);
int sqliteOsTempFileName(char*);
-int sqliteOsClose(OsFile);
-int sqliteOsRead(OsFile, void*, int amt);
-int sqliteOsWrite(OsFile, const void*, int amt);
-int sqliteOsSeek(OsFile, int offset);
-int sqliteOsSync(OsFile);
-int sqliteOsTruncate(OsFile, int size);
-int sqliteOsFileSize(OsFile, int *pSize);
-int sqliteOsLock(OsFile, int wrlock);
-int sqliteOsUnlock(OsFile);
+int sqliteOsClose(OsFile*);
+int sqliteOsRead(OsFile*, void*, int amt);
+int sqliteOsWrite(OsFile*, const void*, int amt);
+int sqliteOsSeek(OsFile*, int offset);
+int sqliteOsSync(OsFile*);
+int sqliteOsTruncate(OsFile*, int size);
+int sqliteOsFileSize(OsFile*, int *pSize);
+int sqliteOsReadLock(OsFile*);
+int sqliteOsWriteLock(OsFile*);
+int sqliteOsUnlock(OsFile*);
int sqliteOsRandomSeed(char*);
int sqliteOsSleep(int ms);
void sqliteOsEnterMutex(void);
diff --git a/src/pager.c b/src/pager.c
index 378a9e41dc..1cb2bef945 100644
--- a/src/pager.c
+++ b/src/pager.c
@@ -18,7 +18,7 @@
** file simultaneously, or one process from reading the database while
** another is writing.
**
-** @(#) $Id: pager.c,v 1.32 2001/12/05 00:21:20 drh Exp $
+** @(#) $Id: pager.c,v 1.33 2001/12/14 15:09:57 drh Exp $
*/
#include "sqliteInt.h"
#include "pager.h"
@@ -218,7 +218,7 @@ static void pager_reset(Pager *pPager){
if( pPager->state==SQLITE_WRITELOCK ){
sqlitepager_rollback(pPager);
}
- sqliteOsUnlock(pPager->fd);
+ sqliteOsUnlock(&pPager->fd);
pPager->state = SQLITE_UNLOCK;
pPager->dbSize = -1;
pPager->nRef = 0;
@@ -246,25 +246,18 @@ static int pager_unwritelock(Pager *pPager){
int rc;
PgHdr *pPg;
if( pPager->state!=SQLITE_WRITELOCK ) return SQLITE_OK;
- sqliteOsUnlock(pPager->fd);
- rc = sqliteOsLock(pPager->fd, 0);
- sqliteOsClose(pPager->jfd);
+ sqliteOsClose(&pPager->jfd);
pPager->journalOpen = 0;
sqliteOsDelete(pPager->zJournal);
+ rc = sqliteOsReadLock(&pPager->fd);
+ assert( rc==SQLITE_OK );
sqliteFree( pPager->aInJournal );
pPager->aInJournal = 0;
for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
pPg->inJournal = 0;
pPg->dirty = 0;
}
- if( rc!=SQLITE_OK ){
- pPager->state = SQLITE_UNLOCK;
- rc = SQLITE_PROTOCOL;
- pPager->errMask |= PAGER_ERR_LOCK;
- }else{
- rc = SQLITE_OK;
- pPager->state = SQLITE_READLOCK;
- }
+ pPager->state = SQLITE_READLOCK;
return rc;
}
@@ -304,8 +297,8 @@ static int pager_playback(Pager *pPager){
** the journal is empty.
*/
assert( pPager->journalOpen );
- sqliteOsSeek(pPager->jfd, 0);
- rc = sqliteOsFileSize(pPager->jfd, &nRec);
+ sqliteOsSeek(&pPager->jfd, 0);
+ rc = sqliteOsFileSize(&pPager->jfd, &nRec);
if( rc!=SQLITE_OK ){
goto end_playback;
}
@@ -317,16 +310,16 @@ static int pager_playback(Pager *pPager){
/* Read the beginning of the journal and truncate the
** database file back to its original size.
*/
- rc = sqliteOsRead(pPager->jfd, aMagic, sizeof(aMagic));
+ rc = sqliteOsRead(&pPager->jfd, aMagic, sizeof(aMagic));
if( rc!=SQLITE_OK || memcmp(aMagic,aJournalMagic,sizeof(aMagic))!=0 ){
rc = SQLITE_PROTOCOL;
goto end_playback;
}
- rc = sqliteOsRead(pPager->jfd, &mxPg, sizeof(mxPg));
+ rc = sqliteOsRead(&pPager->jfd, &mxPg, sizeof(mxPg));
if( rc!=SQLITE_OK ){
goto end_playback;
}
- rc = sqliteOsTruncate(pPager->fd, mxPg*SQLITE_PAGE_SIZE);
+ rc = sqliteOsTruncate(&pPager->fd, mxPg*SQLITE_PAGE_SIZE);
if( rc!=SQLITE_OK ){
goto end_playback;
}
@@ -339,9 +332,9 @@ static int pager_playback(Pager *pPager){
/* Seek to the beginning of the segment */
int ofst;
ofst = i*sizeof(PageRecord) + sizeof(aMagic) + sizeof(Pgno);
- rc = sqliteOsSeek(pPager->jfd, ofst);
+ rc = sqliteOsSeek(&pPager->jfd, ofst);
if( rc!=SQLITE_OK ) break;
- rc = sqliteOsRead(pPager->jfd, &pgRec, sizeof(pgRec));
+ rc = sqliteOsRead(&pPager->jfd, &pgRec, sizeof(pgRec));
if( rc!=SQLITE_OK ) break;
/* Sanity checking on the page */
@@ -358,9 +351,9 @@ static int pager_playback(Pager *pPager){
memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
}
- rc = sqliteOsSeek(pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE);
+ rc = sqliteOsSeek(&pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE);
if( rc!=SQLITE_OK ) break;
- rc = sqliteOsWrite(pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
+ rc = sqliteOsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
if( rc!=SQLITE_OK ) break;
}
@@ -432,7 +425,7 @@ int sqlitepager_open(
nameLen = strlen(zFilename);
pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 );
if( pPager==0 ){
- sqliteOsClose(fd);
+ sqliteOsClose(&fd);
return SQLITE_NOMEM;
}
pPager->zFilename = (char*)&pPager[1];
@@ -481,7 +474,7 @@ int sqlitepager_pagecount(Pager *pPager){
if( pPager->dbSize>=0 ){
return pPager->dbSize;
}
- if( sqliteOsFileSize(pPager->fd, &n)!=SQLITE_OK ){
+ if( sqliteOsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
pPager->errMask |= PAGER_ERR_DISK;
return 0;
}
@@ -506,12 +499,12 @@ int sqlitepager_close(Pager *pPager){
switch( pPager->state ){
case SQLITE_WRITELOCK: {
sqlitepager_rollback(pPager);
- sqliteOsUnlock(pPager->fd);
+ sqliteOsUnlock(&pPager->fd);
assert( pPager->journalOpen==0 );
break;
}
case SQLITE_READLOCK: {
- sqliteOsUnlock(pPager->fd);
+ sqliteOsUnlock(&pPager->fd);
break;
}
default: {
@@ -523,7 +516,7 @@ int sqlitepager_close(Pager *pPager){
pNext = pPg->pNextAll;
sqliteFree(pPg);
}
- sqliteOsClose(pPager->fd);
+ sqliteOsClose(&pPager->fd);
assert( pPager->journalOpen==0 );
if( pPager->tempFile ){
sqliteOsDelete(pPager->zFilename);
@@ -590,14 +583,14 @@ static int syncAllPages(Pager *pPager){
PgHdr *pPg;
int rc = SQLITE_OK;
if( pPager->needSync ){
- rc = sqliteOsSync(pPager->jfd);
+ rc = sqliteOsSync(&pPager->jfd);
if( rc!=0 ) return rc;
pPager->needSync = 0;
}
for(pPg=pPager->pFirst; pPg; pPg=pPg->pNextFree){
if( pPg->dirty ){
- sqliteOsSeek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
- rc = sqliteOsWrite(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
+ sqliteOsSeek(&pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
+ rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
if( rc!=SQLITE_OK ) break;
pPg->dirty = 0;
}
@@ -644,7 +637,7 @@ int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
** on the database file.
*/
if( pPager->nRef==0 ){
- if( sqliteOsLock(pPager->fd, 0)!=SQLITE_OK ){
+ if( sqliteOsReadLock(&pPager->fd)!=SQLITE_OK ){
*ppPage = 0;
return SQLITE_BUSY;
}
@@ -655,6 +648,17 @@ int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
if( sqliteOsFileExists(pPager->zJournal) ){
int rc, dummy;
+ /* Get a write lock on the database
+ */
+ rc = sqliteOsWriteLock(&pPager->fd);
+ if( rc!=SQLITE_OK ){
+ rc = sqliteOsReadLock(&pPager->fd);
+ assert( rc==SQLITE_OK );
+ *ppPage = 0;
+ return SQLITE_BUSY;
+ }
+ pPager->state = SQLITE_WRITELOCK;
+
/* Open the journal for exclusive access. Return SQLITE_BUSY if
** we cannot get exclusive access to the journal file.
**
@@ -663,28 +667,13 @@ int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
** exclusive access lock.
*/
rc = sqliteOsOpenReadWrite(pPager->zJournal, &pPager->jfd, &dummy);
- if( rc==SQLITE_OK ){
- pPager->journalOpen = 1;
- }
- if( rc!=SQLITE_OK || sqliteOsLock(pPager->jfd, 1)!=SQLITE_OK ){
- if( pPager->journalOpen ){
- sqliteOsClose(pPager->jfd);
- pPager->journalOpen = 0;
- }
- sqliteOsUnlock(pPager->fd);
+ if( rc!=SQLITE_OK ){
+ rc = sqliteOsUnlock(&pPager->fd);
+ assert( rc==SQLITE_OK );
*ppPage = 0;
return SQLITE_BUSY;
}
-
- /* Get a write lock on the database */
- sqliteOsUnlock(pPager->fd);
- if( sqliteOsLock(pPager->fd, 1)!=SQLITE_OK ){
- sqliteOsClose(pPager->jfd);
- pPager->journalOpen = 0;
- *ppPage = 0;
- return SQLITE_PROTOCOL;
- }
- pPager->state = SQLITE_WRITELOCK;
+ pPager->journalOpen = 1;
/* Playback and delete the journal. Drop the database write
** lock and reacquire the read lock.
@@ -804,8 +793,8 @@ int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
}else{
int rc;
- sqliteOsSeek(pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE);
- rc = sqliteOsRead(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
+ sqliteOsSeek(&pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE);
+ rc = sqliteOsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
if( rc!=SQLITE_OK ){
return rc;
}
@@ -934,48 +923,30 @@ int sqlitepager_write(void *pData){
assert( pPager->state!=SQLITE_UNLOCK );
if( pPager->state==SQLITE_READLOCK ){
assert( pPager->aInJournal==0 );
+ rc = sqliteOsWriteLock(&pPager->fd);
+ if( rc!=SQLITE_OK ){
+ return rc;
+ }
pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
if( pPager->aInJournal==0 ){
- sqliteFree(pPager->aInJournal);
+ sqliteOsReadLock(&pPager->fd);
return SQLITE_NOMEM;
}
rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd);
if( rc!=SQLITE_OK ){
sqliteFree(pPager->aInJournal);
+ pPager->aInJournal = 0;
+ sqliteOsReadLock(&pPager->fd);
return SQLITE_CANTOPEN;
}
pPager->journalOpen = 1;
pPager->needSync = 0;
- if( sqliteOsLock(pPager->jfd, 1)!=SQLITE_OK ){
- sqliteFree(pPager->aInJournal);
- sqliteOsClose(pPager->jfd);
- sqliteOsDelete(pPager->zJournal);
- pPager->journalOpen = 0;
- return SQLITE_BUSY;
- }
- sqliteOsUnlock(pPager->fd);
- if( sqliteOsLock(pPager->fd, 1)!=SQLITE_OK ){
- sqliteOsUnlock(pPager->fd);
- rc = sqliteOsLock(pPager->fd, 0);
- sqliteFree(pPager->aInJournal);
- sqliteOsClose(pPager->jfd);
- sqliteOsDelete(pPager->zJournal);
- pPager->journalOpen = 0;
- if( rc ){
- pPager->state = SQLITE_UNLOCK;
- pPager->errMask |= PAGER_ERR_LOCK;
- return SQLITE_PROTOCOL;
- }else{
- pPager->state = SQLITE_READLOCK;
- return SQLITE_BUSY;
- }
- }
pPager->state = SQLITE_WRITELOCK;
sqlitepager_pagecount(pPager);
pPager->origDbSize = pPager->dbSize;
- rc = sqliteOsWrite(pPager->jfd, aJournalMagic, sizeof(aJournalMagic));
+ rc = sqliteOsWrite(&pPager->jfd, aJournalMagic, sizeof(aJournalMagic));
if( rc==SQLITE_OK ){
- rc = sqliteOsWrite(pPager->jfd, &pPager->dbSize, sizeof(Pgno));
+ rc = sqliteOsWrite(&pPager->jfd, &pPager->dbSize, sizeof(Pgno));
}
if( rc!=SQLITE_OK ){
rc = pager_unwritelock(pPager);
@@ -986,9 +957,9 @@ int sqlitepager_write(void *pData){
assert( pPager->state==SQLITE_WRITELOCK );
assert( pPager->journalOpen );
if( pPg->pgno <= pPager->origDbSize ){
- rc = sqliteOsWrite(pPager->jfd, &pPg->pgno, sizeof(Pgno));
+ rc = sqliteOsWrite(&pPager->jfd, &pPg->pgno, sizeof(Pgno));
if( rc==SQLITE_OK ){
- rc = sqliteOsWrite(pPager->jfd, pData, SQLITE_PAGE_SIZE);
+ rc = sqliteOsWrite(&pPager->jfd, pData, SQLITE_PAGE_SIZE);
}
if( rc!=SQLITE_OK ){
sqlitepager_rollback(pPager);
@@ -1040,17 +1011,17 @@ int sqlitepager_commit(Pager *pPager){
return SQLITE_ERROR;
}
assert( pPager->journalOpen );
- if( pPager->needSync && sqliteOsSync(pPager->jfd)!=SQLITE_OK ){
+ if( pPager->needSync && sqliteOsSync(&pPager->jfd)!=SQLITE_OK ){
goto commit_abort;
}
for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
if( pPg->dirty==0 ) continue;
- rc = sqliteOsSeek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
+ rc = sqliteOsSeek(&pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
if( rc!=SQLITE_OK ) goto commit_abort;
- rc = sqliteOsWrite(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
+ rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
if( rc!=SQLITE_OK ) goto commit_abort;
}
- if( sqliteOsSync(pPager->fd)!=SQLITE_OK ) goto commit_abort;
+ if( sqliteOsSync(&pPager->fd)!=SQLITE_OK ) goto commit_abort;
rc = pager_unwritelock(pPager);
pPager->dbSize = -1;
return rc;
diff --git a/www/changes.tcl b/www/changes.tcl
index 0de6dd3391..d50c5016e3 100644
--- a/www/changes.tcl
+++ b/www/changes.tcl
@@ -17,6 +17,13 @@ proc chng {date desc} {
puts "
"
}
+chng {2001 Dec 14 (2.1.6)} {
+Fix the locking mechanism yet again to prevent
+ sqlite_exec() from returning SQLITE_PROTOCOL
+ unnecessarily. This time the bug was a race condition in
+ the locking code. This change effects both POSIX and Windows users.
+}
+
chng {2001 Dec 6 (2.1.5)} {
Fix for another problem (unrelated to the one fixed in 2.1.4)
that sometimes causes sqlite_exec() to return SQLITE_PROTOCOL