1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Fix a race condition in the locking code that would sometimes cause

SQLITE_PROTOCOL or SQLITE_CORRUPT to be returned when SQLITE_BUSY should
have been returned. (CVS 326)

FossilOrigin-Name: b0d218876442187af08161d989e6887b1cb4130c
This commit is contained in:
drh
2001-12-14 15:09:55 +00:00
parent 6a6cfb9b31
commit a7fcb05988
8 changed files with 267 additions and 228 deletions

View File

@ -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;
}
/*

288
src/os.c
View File

@ -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( wrote<amt ) return SQLITE_FULL;
return SQLITE_OK;
#endif
#if OS_WIN
DWORD wrote;
SimulateIOError(SQLITE_IOERR);
if( !WriteFile(id, pBuf, amt, &wrote, 0) || wrote<amt ){
if( !WriteFile(id->h, pBuf, amt, &wrote, 0) || wrote<amt ){
return SQLITE_FULL;
}
return SQLITE_OK;
@ -509,13 +509,13 @@ int sqliteOsWrite(OsFile id, const void *pBuf, int amt){
/*
** Move the read/write pointer in a file.
*/
int sqliteOsSeek(OsFile id, int offset){
int sqliteOsSeek(OsFile *id, int offset){
#if OS_UNIX
lseek(id.fd, offset, SEEK_SET);
lseek(id->fd, 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
}

View File

@ -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 <windows.h>
#include <winbase.h>
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);

View File

@ -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;