/ Check-in [6c927dd3]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix a bug in the legacy journal format writing logic. (CVS 862)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6c927dd36c19ebb8bb8222b4d18ed67f4fe733e8
User & Date: drh 2003-02-12 02:10:15
Context
2003-02-12
14:09
Added the new FULL option to the SYNCHRONOUS pragma. Still need to test it. (CVS 863) check-in: 792a9e15 user: drh tags: trunk
02:10
Fix a bug in the legacy journal format writing logic. (CVS 862) check-in: 6c927dd3 user: drh tags: trunk
2003-02-11
14:55
Modify the journal format to be more robust against garbage that might appear in the file after a power failure. The changes are mostly working but more testing is still required. This check-in is to checkpoint the changes so far. (CVS 861) check-in: 8ec56325 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.73 2003/02/11 14:55:41 drh Exp $
           21  +** @(#) $Id: pager.c,v 1.74 2003/02/12 02:10:15 drh Exp $
    22     22   */
    23     23   #include "os.h"         /* Must be first to enable large file support */
    24     24   #include "sqliteInt.h"
    25     25   #include "pager.h"
    26     26   #include <assert.h>
    27     27   #include <string.h>
    28     28   
................................................................................
   608    608       if( rc ) goto end_playback;
   609    609       rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
   610    610       if( rc ) goto end_playback;
   611    611       if( nRec==0xffffffff ){
   612    612         nRec = (szJ - JOURNAL_HDR_SZ(3))/JOURNAL_PG_SZ(3);
   613    613       }
   614    614     }else{
   615         -    nRec = (szJ - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord);
          615  +    nRec = (szJ - JOURNAL_HDR_SZ(2))/JOURNAL_PG_SZ(2);
          616  +    assert( nRec*JOURNAL_PG_SZ(2)+JOURNAL_HDR_SZ(2)==szJ );
   616    617     }
   617    618     rc = read32bits(format, &pPager->jfd, &mxPg);
   618    619     if( rc!=SQLITE_OK ){
   619    620       goto end_playback;
   620    621     }
          622  +  assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
   621    623     rc = sqliteOsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
   622    624     if( rc!=SQLITE_OK ){
   623    625       goto end_playback;
   624    626     }
   625    627     pPager->dbSize = mxPg;
   626    628     
   627    629     /* Copy original pages out of the journal and back into the database file.
   628    630     */
   629    631     for(i=0; i<nRec; i++){
   630    632       rc = pager_playback_one_page(pPager, &pPager->jfd, format);
   631    633       if( rc!=SQLITE_OK ){
   632    634         if( rc==SQLITE_DONE ){
   633         -fprintf(stderr,"Playback complete after %d of %d records\n", i, nRec);
   634    635           rc = SQLITE_OK;
   635    636         }
   636    637         break;
   637    638       }
   638    639     }
   639    640   
   640    641   
................................................................................
  1022   1023     int rc = SQLITE_OK;
  1023   1024   
  1024   1025     /* Sync the journal before modifying the main database
  1025   1026     ** (assuming there is a journal and it needs to be synced.)
  1026   1027     */
  1027   1028     if( pPager->needSync ){
  1028   1029       if( !pPager->tempFile ){
  1029         -      off_t szJ;
  1030   1030         assert( pPager->journalOpen );
  1031   1031         assert( !pPager->noSync );
  1032   1032   #ifndef NDEBUG
  1033   1033         {
  1034   1034           off_t hdrSz, pgSz;
  1035   1035           hdrSz = JOURNAL_HDR_SZ(journal_format);
  1036   1036           pgSz = JOURNAL_PG_SZ(journal_format);
  1037   1037           rc = sqliteOsFileSize(&pPager->jfd, &pPager->syncJSize);
  1038   1038           if( rc!=0 ) return rc;
  1039   1039           assert( pPager->nRec*pgSz+hdrSz==pPager->syncJSize );
  1040   1040         }
  1041   1041   #endif
  1042         -      if( pPager->fullSync ){
  1043         -        TRACE1("SYNC\n");
  1044         -        rc = sqliteOsSync(&pPager->jfd);
  1045         -        if( rc!=0 ) return rc;
         1042  +      if( journal_format>=3 ){
         1043  +        off_t szJ;
         1044  +        if( pPager->fullSync ){
         1045  +          TRACE1("SYNC\n");
         1046  +          rc = sqliteOsSync(&pPager->jfd);
         1047  +          if( rc!=0 ) return rc;
         1048  +        }
         1049  +        sqliteOsSeek(&pPager->jfd, sizeof(aJournalMagic1));
         1050  +        write32bits(&pPager->jfd, pPager->nRec);
         1051  +        szJ = JOURNAL_HDR_SZ(journal_format) +
         1052  +                 pPager->nRec*JOURNAL_PG_SZ(journal_format);
         1053  +        sqliteOsSeek(&pPager->jfd, szJ);
  1046   1054         }
  1047         -      sqliteOsSeek(&pPager->jfd, sizeof(aJournalMagic1));
  1048         -      write32bits(&pPager->jfd, pPager->nRec);
  1049         -      szJ = JOURNAL_HDR_SZ(journal_format) +
  1050         -               pPager->nRec*JOURNAL_PG_SZ(journal_format);
  1051         -      sqliteOsSeek(&pPager->jfd, szJ);
  1052   1055         TRACE1("SYNC\n");
  1053   1056         rc = sqliteOsSync(&pPager->jfd);
  1054   1057         if( rc!=0 ) return rc;
  1055   1058         pPager->journalStarted = 1;
  1056   1059       }
  1057   1060       pPager->needSync = 0;
  1058   1061