diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/pager.c | 42 | ||||
-rw-r--r-- | src/vdbe.c | 7 |
2 files changed, 44 insertions, 5 deletions
diff --git a/src/pager.c b/src/pager.c index 8869802c5..a902d70ef 100644 --- a/src/pager.c +++ b/src/pager.c @@ -5898,6 +5898,48 @@ int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ /* Change the journal mode. */ pPager->journalMode = (u8)eMode; + + /* When transistioning from TRUNCATE or PERSIST to any other journal + ** mode except WAL (and we are not in locking_mode=EXCLUSIVE) then + ** delete the journal file. + */ + assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 ); + assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 ); + assert( (PAGER_JOURNALMODE_DELETE & 5)==0 ); + assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 ); + assert( (PAGER_JOURNALMODE_OFF & 5)==0 ); + assert( (PAGER_JOURNALMODE_WAL & 5)==5 ); + + assert( isOpen(pPager->fd) || pPager->exclusiveMode ); + if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){ + + /* In this case we would like to delete the journal file. If it is + ** not possible, then that is not a problem. Deleting the journal file + ** here is an optimization only. + ** + ** Before deleting the journal file, obtain a RESERVED lock on the + ** database file. This ensures that the journal file is not deleted + ** while it is in use by some other client. + */ + int rc = SQLITE_OK; + int state = pPager->state; + if( state<PAGER_SHARED ){ + rc = sqlite3PagerSharedLock(pPager); + } + if( pPager->state==PAGER_SHARED ){ + assert( rc==SQLITE_OK ); + rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK); + } + if( rc==SQLITE_OK ){ + sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); + } + if( rc==SQLITE_OK && state==PAGER_SHARED ){ + sqlite3OsUnlock(pPager->fd, SHARED_LOCK); + }else if( state==PAGER_UNLOCK ){ + pager_unlock(pPager); + } + assert( state==pPager->state ); + } } /* Return the new journal mode */ diff --git a/src/vdbe.c b/src/vdbe.c index 14785d35d..6803beafd 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -5251,9 +5251,6 @@ case OP_JournalMode: { /* out2-prerelease */ }else if( rc==SQLITE_BUSY && pOp->p5==0 ){ goto abort_due_to_error; } - }else{ - sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_DELETE); - rc = SQLITE_OK; } /* Open a transaction on the database file. Regardless of the journal @@ -5261,8 +5258,7 @@ case OP_JournalMode: { /* out2-prerelease */ */ assert( sqlite3BtreeIsInTrans(pBt)==0 ); if( rc==SQLITE_OK ){ - rc = sqlite3BtreeSetVersion(pBt, - (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); + rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); if( rc==SQLITE_BUSY && pOp->p5==0 ) goto abort_due_to_error; } if( rc==SQLITE_BUSY ){ @@ -5274,6 +5270,7 @@ case OP_JournalMode: { /* out2-prerelease */ #endif /* ifndef SQLITE_OMIT_WAL */ eNew = sqlite3PagerSetJournalMode(pPager, eNew); + pOut = &aMem[pOp->p2]; pOut->flags = MEM_Str|MEM_Static|MEM_Term; pOut->z = (char *)sqlite3JournalModename(eNew); |