From 731bf5bcf87cbbd34a11f73a6d345255f90e7547 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 17 Jun 2010 16:44:21 +0000 Subject: [PATCH] A different fix to [fc62af4523]. When changing from journal_mode=PERSIST or TRINCATE to some other rollback mode, delete the journal file only if a RESERVED lock can be obtained on the database file first. FossilOrigin-Name: b9b11855e8a9522309dd30e5256bb67d67e1353a --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/pager.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/vdbe.c | 7 ++----- test/journal2.test | 9 ++------- test/jrnlmode.test | 4 ++-- 6 files changed, 58 insertions(+), 24 deletions(-) diff --git a/manifest b/manifest index b5533dd304..d5453b91cc 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sbug\sin\sjournal2.test. -D 2010-06-17T11:36:28 +C A\sdifferent\sfix\sto\s[fc62af4523].\sWhen\schanging\sfrom\sjournal_mode=PERSIST\sor\sTRINCATE\sto\ssome\sother\srollback\smode,\sdelete\sthe\sjournal\sfile\sonly\sif\sa\sRESERVED\slock\scan\sbe\sobtained\son\sthe\sdatabase\sfile\sfirst. +D 2010-06-17T16:44:22 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in a5cad1f8f3e021356bfcc6c77dc16f6f1952bbc3 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -156,7 +156,7 @@ F src/os_common.h a8f95b81eca8a1ab8593d23e94f8a35f35d4078f F src/os_os2.c 665876d5eec7585226b0a1cf5e18098de2b2da19 F src/os_unix.c ae173c9f6afaa58b2833a1c95c6cd32021755c42 F src/os_win.c dfde7d33c446e89dd9a277c036f2c4cc564b3138 -F src/pager.c 658e5063d2d1661336a1e269e1cc324dfa8a683a +F src/pager.c 5968e0d73febd2e1c9e4e785660bdf49ff2e2cab F src/pager.h ca1f23c0cf137ac26f8908df2427c8b308361efd F src/parse.y ace5c7a125d9f2a410e431ee3209034105045f7e F src/pcache.c 1e9aa2dbc0845b52e1b51cc39753b6d1e041cb07 @@ -217,7 +217,7 @@ F src/update.c 9859f2056c7739a1db0d9774ccb6c2f0cee6d1de F src/utf.c 1baeeac91707a4df97ccc6141ec0f808278af685 F src/util.c 32aebf04c10e51ad3977a928b7416bed671b620b F src/vacuum.c 241a8386727c1497eba4955933356dfba6ff8c9f -F src/vdbe.c 8a910a19981b8f670f4d67b5b0673459de50ccba +F src/vdbe.c e115585b14d2cc4128cb53a7e42f207750e80f55 F src/vdbe.h 471f6a3dcec4817ca33596fe7f6654d56c0e75f3 F src/vdbeInt.h 19ebc8c2a2e938340051ee65af3f377fb99102d1 F src/vdbeapi.c dc3138f10afbc95ed3c21dd25abb154504b1db9d @@ -466,8 +466,8 @@ F test/join4.test 1a352e4e267114444c29266ce79e941af5885916 F test/join5.test 86675fc2919269aa923c84dd00ee4249b97990fe F test/join6.test bf82cf3f979e9eade83ad0d056a66c5ed71d1901 F test/journal1.test 36f2d1bb9bf03f790f43fbdb439e44c0657fab19 -F test/journal2.test 61c60f287d2d5f4d3d62af0a0d97100c90e055a6 -F test/jrnlmode.test cd0c9c5b2555f3d5bb72ed8aa8d0ad16ecf77e7c +F test/journal2.test 3e98dc6b7486aa7e1ed67ff542e891a978654093 +F test/jrnlmode.test 76f94d61528c5ff32102a12f8cf34f4cc36f7849 F test/jrnlmode2.test fe79ea1f0375c926b8de0362ddf94f34a64135fd F test/jrnlmode3.test cfcdb12b90e640a23b92785a002d96c0624c8710 F test/keyword1.test a2400977a2e4fde43bf33754c2929fda34dbca05 @@ -824,7 +824,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 9f6ea1de5abab0ca28688e257ddf03c66413cf6e -R 07d7ad85696711a45c0b4b5690681a45 +P c1e04f1d4e8575ebc41b5b4403dbfe3f60d578dc +R c1f8308c51de4402b52d191c23c2aef4 U dan -Z b6a1325b7da2976f7858122f03070f50 +Z 10be27f6ff328697757cba4d76703f36 diff --git a/manifest.uuid b/manifest.uuid index f33d818d2d..aab680823a 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -c1e04f1d4e8575ebc41b5b4403dbfe3f60d578dc \ No newline at end of file +b9b11855e8a9522309dd30e5256bb67d67e1353a \ No newline at end of file diff --git a/src/pager.c b/src/pager.c index 8869802c54..a902d70efd 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( statestate==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 14785d35d5..6803beafd3 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); diff --git a/test/journal2.test b/test/journal2.test index 18c898a28c..20331b8bd1 100644 --- a/test/journal2.test +++ b/test/journal2.test @@ -215,16 +215,11 @@ ifcapable wal { do_test journal2-2.3 { expr {[file size test.db-journal] > 512} } {1} - do_test journal2-2.3 { + do_test journal2-2.4 { set ::oplog [list] execsql { PRAGMA journal_mode = WAL } set ::oplog - } [list \ - xClose test.db-journal \ - xOpen test.db-journal xClose test.db-journal \ - xOpen test.db-journal xClose test.db-journal \ - xDelete test.db-journal \ - ] + } {xClose test.db-journal xDelete test.db-journal} db close } diff --git a/test/jrnlmode.test b/test/jrnlmode.test index 2a6338fc9e..fd3e59f12d 100644 --- a/test/jrnlmode.test +++ b/test/jrnlmode.test @@ -484,7 +484,7 @@ ifcapable pragma { INSERT INTO t4 VALUES(3, 4); } file exists test.db-journal - } {1} + } {0} do_test jrnlmode-6.7 { execsql { COMMIT; @@ -493,7 +493,7 @@ ifcapable pragma { } {1 2 3 4} do_test jrnlmode-6.8 { file exists test.db-journal - } {1} + } {0} do_test jrnlmode-6.9 { execsql { PRAGMA journal_mode = DELETE;