SQLite4
Check-in [cba26fd7cc]
Not logged in

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

Overview
Comment:Add a checksum to the database header.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: cba26fd7ccc5d66908713926f8eae03e0dc11a9b
User & Date: dan 2013-11-15 09:44:58
Context
2013-11-15
18:47
Various fixes so that the "bt" permutation test passes. check-in: 0866df185c user: dan tags: trunk
09:44
Add a checksum to the database header. check-in: cba26fd7cc user: dan tags: trunk
2013-11-14
19:29
Fix various savepoint-related problems in bt. check-in: c633d85c33 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btInt.h.

    43     43   ** Database header fields.
    44     44   */
    45     45   typedef struct BtDbhdr BtDbhdr;
    46     46   struct BtDbhdr {
    47     47     u32 pgsz;                       /* Page size in bytes */
    48     48     u32 nPg;                        /* Number of pages in database */
    49     49     u32 cookie;                     /* User cookie value (SQL schema cookie) */
           50  +  u32 padding;                    /* Unused */
           51  +  u32 aCksum[2];                  /* Checksum over other fields */
    50     52   };
    51     53   
    52     54   /*************************************************************************
    53     55   ** Interface to bt_pager.c functionality.
    54     56   */
    55     57   typedef struct BtPage BtPage;
    56     58   typedef struct BtPager BtPager;

Changes to src/bt_log.c.

   802    802   
   803    803     aLog[5] = iLast;
   804    804     return btLogHashRollback(pLog, btLogFrameHash(pLog, iLast), iLast);
   805    805   }
   806    806   
   807    807   static int btLogReadDbhdr(BtLog *pLog, BtDbhdr *pHdr){
   808    808     BtLock *p = pLog->pLock;
   809         -  int rc;
   810         -  i64 nByte;
          809  +  int rc;                         /* Return code */
          810  +  i64 nByte;                      /* Size of database file in byte */
   811    811   
   812    812     rc = p->pVfs->xSize(p->pFd, &nByte);
   813    813     if( rc==SQLITE4_OK && nByte>0 ){
   814    814       rc = p->pVfs->xRead(p->pFd, 0, pHdr, sizeof(BtDbhdr));
   815         -    if( BTLOG_LITTLE_ENDIAN ){
   816         -      pHdr->cookie = BYTESWAP32(pHdr->cookie);
   817         -      pHdr->nPg = BYTESWAP32(pHdr->nPg);
   818         -      pHdr->pgsz = BYTESWAP32(pHdr->pgsz);
   819         -    }
   820         -    assert( pHdr->pgsz>0 );
   821         -  }else{
   822         -    memset(pHdr, 0, sizeof(BtDbhdr));
   823         -    pHdr->pgsz = BT_DEFAULT_PGSZ;
   824         -    pHdr->nPg = 1;
   825         -  }
   826         -
   827         -  return rc;
   828         -}
          815  +    if( rc==SQLITE4_OK ){
          816  +      u32 aCksum[2];
          817  +      btLogChecksum(1, pHdr, offsetof(BtDbhdr, aCksum), 0, aCksum);
          818  +      if( aCksum[0]==pHdr->aCksum[0] && aCksum[1]==pHdr->aCksum[1] ){
          819  +        return SQLITE4_OK;
          820  +      }
          821  +    }
          822  +  }
          823  +
          824  +  memset(pHdr, 0, sizeof(BtDbhdr));
          825  +  pHdr->pgsz = BT_DEFAULT_PGSZ;
          826  +  pHdr->nPg = 1;
          827  +  return rc;
          828  +}
          829  +
          830  +static int btLogUpdateDbhdr(BtLog *pLog, u8 *aData){
          831  +  BtDbhdr dbhdr;
          832  +
          833  +  dbhdr.cookie = pLog->snapshot.iCookie;
          834  +  dbhdr.nPg = pLog->snapshot.nPg;
          835  +  dbhdr.pgsz = pLog->snapshot.pgsz;
          836  +  dbhdr.padding = 0;
          837  +  btLogChecksum(1, &dbhdr, offsetof(BtDbhdr, aCksum), 0, dbhdr.aCksum);
          838  +
          839  +  assert( dbhdr.pgsz>0 );
          840  +  memcpy(aData, &dbhdr, sizeof(BtDbhdr));
          841  +
          842  +  return SQLITE4_OK;
          843  +}
          844  +
   829    845   
   830    846   /*
   831    847   ** Run log recovery. In other words, read the log file from disk and 
   832    848   ** initialize the shared-memory accordingly.
   833    849   */
   834    850   static int btLogRecover(BtLog *pLog){
   835    851     bt_env *pVfs = pLog->pLock->pVfs;
................................................................................
  1696   1712       + (int)pLog->snapshot.aLog[3] - (int)pLog->snapshot.aLog[2]
  1697   1713       + (pLog->snapshot.aLog[3]!=0)
  1698   1714       + (int)pLog->snapshot.aLog[5] - (int)pLog->snapshot.aLog[4]
  1699   1715       + (pLog->snapshot.aLog[5]!=0)
  1700   1716     ;
  1701   1717   }
  1702   1718   
  1703         -static int btLogUpdateDbhdr(BtLog *pLog, u8 *aData){
  1704         -  BtDbhdr dbhdr;
  1705         -
  1706         -  dbhdr.cookie = pLog->snapshot.iCookie;
  1707         -  dbhdr.nPg = pLog->snapshot.nPg;
  1708         -  dbhdr.pgsz = pLog->snapshot.pgsz;
  1709         -
  1710         -  if( BTLOG_LITTLE_ENDIAN ){
  1711         -    dbhdr.cookie = BYTESWAP32(dbhdr.cookie);
  1712         -    dbhdr.nPg = BYTESWAP32(dbhdr.nPg);
  1713         -    dbhdr.pgsz = BYTESWAP32(dbhdr.pgsz);
  1714         -    assert( dbhdr.pgsz>0 );
  1715         -  }
  1716         -  memcpy(aData, &dbhdr, sizeof(BtDbhdr));
  1717         -
  1718         -  return SQLITE4_OK;
  1719         -}
  1720         -
  1721   1719   int sqlite4BtLogCheckpoint(BtLog *pLog, int nFrameBuffer){
  1722   1720     BtLock *pLock = pLog->pLock;
  1723   1721     int rc;
  1724   1722   
  1725   1723     /* Take the CHECKPOINTER lock. */
  1726   1724     rc = sqlite4BtLockCkpt(pLock);
  1727   1725     if( rc==SQLITE4_OK ){

Changes to test/permutations.test.

   128    128   #   bt
   129    129   #   veryquick
   130    130   #   quick
   131    131   #   full
   132    132   #
   133    133   lappend ::testsuitelist xxx
   134    134   
   135         -  # attach.test attach3.test 
   136    135     # fkey2.test 
   137    136   test_suite "bt" -prefix "" -description {
   138    137   } -files {
   139    138     simple3.test
   140    139   
   141    140     alter.test alter3.test alter4.test
   142    141     analyze.test analyze3.test analyze4.test analyze5.test 
   143    142     analyze6.test analyze7.test analyze8.test
   144    143     auth.test auth2.test auth3.test auth4.test
   145    144     aggerror.test
   146         -  attach4.test
          145  +  attach.test attach3.test attach4.test
   147    146     autoindex1.test
   148    147     badutf.test badutf2.test
   149    148     between.test
   150    149     bigrow.test
   151    150     bind.test
   152    151     blob.test
   153    152     boundary1.test boundary2.test boundary3.test boundary4.test