/ Check-in [4d384761]
Login

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

Overview
Comment:Set the Pager.eState variable to PAGER_ERROR whenever the pager enters the error state.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | experimental
Files: files | file ages | folders
SHA1: 4d384761d27425b71555f4f3262778277a955835
User & Date: dan 2010-08-03 18:18:42
Context
2010-08-03
18:29
Merge trunk changes into experimental branch. check-in: 15368a9f user: dan tags: experimental
18:18
Set the Pager.eState variable to PAGER_ERROR whenever the pager enters the error state. check-in: 4d384761 user: dan tags: experimental
15:57
Add comments to clarify the purpose of the pager "error state". check-in: fde4c597 user: dan tags: experimental
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/pager.c.

   287    287   */
   288    288   #define PAGER_NONE                  0
   289    289   #define PAGER_READER                1
   290    290   #define PAGER_WRITER_INITIAL        2
   291    291   #define PAGER_WRITER_CACHEMOD       3
   292    292   #define PAGER_WRITER_DBMOD          4
   293    293   #define PAGER_WRITER_FINISHED       5
          294  +#define PAGER_ERROR                 6
   294    295   
   295    296   
   296    297   /*
   297    298   ** The page cache as a whole is always in one of the following
   298    299   ** states:
   299    300   **
   300    301   **   PAGER_UNLOCK        The page cache is not currently reading or 
................................................................................
   692    693     /* State must be valid. */
   693    694     assert( p->eState==PAGER_NONE
   694    695          || p->eState==PAGER_READER
   695    696          || p->eState==PAGER_WRITER_INITIAL
   696    697          || p->eState==PAGER_WRITER_CACHEMOD
   697    698          || p->eState==PAGER_WRITER_DBMOD
   698    699          || p->eState==PAGER_WRITER_FINISHED
          700  +       || p->eState==PAGER_ERROR
   699    701     );
   700    702   
   701    703     /* Regardless of the current state, a temp-file connection always behaves
   702    704     ** as if it has an exclusive lock on the database file. It never updates
   703    705     ** the change-counter field, so the changeCountDone flag is always set.
   704    706     */
   705    707     assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
................................................................................
   714    716     /* Check that MEMDB implies noSync. */
   715    717     assert( !MEMDB || p->noSync );
   716    718   
   717    719     switch( p->eState ){
   718    720       case PAGER_NONE:
   719    721         assert( !MEMDB );
   720    722         assert( !p->tempFile );
          723  +      assert( pPager->errCode==SQLITE_OK );
          724  +      assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
   721    725         break;
   722    726   
   723    727       case PAGER_READER:
          728  +      assert( pPager->errCode==SQLITE_OK );
   724    729         assert( p->eLock>=SHARED_LOCK || p->noReadlock );
   725    730         break;
   726    731   
   727    732       case PAGER_WRITER_INITIAL:
          733  +      assert( pPager->errCode==SQLITE_OK );
   728    734         if( !pagerUseWal(pPager) ){
   729    735           assert( p->eLock>=RESERVED_LOCK );
   730    736         }
   731    737         assert( pPager->dbSize==pPager->dbOrigSize );
   732    738         assert( pPager->dbOrigSize==pPager->dbFileSize );
          739  +      assert( pPager->setMaster==0 );
   733    740         break;
   734    741   
   735    742       case PAGER_WRITER_CACHEMOD:
          743  +      assert( pPager->errCode==SQLITE_OK );
   736    744         if( !pagerUseWal(pPager) ){
   737    745           /* It is possible that if journal_mode=wal here that neither the
   738    746           ** journal file nor the WAL file are open. This happens during
   739    747           ** a rollback transaction that switches from journal_mode=off
   740    748           ** to journal_mode=wal.
   741    749           */
   742    750           assert( p->eLock>=RESERVED_LOCK );
................................................................................
   745    753                || p->journalMode==PAGER_JOURNALMODE_WAL 
   746    754           );
   747    755         }
   748    756         assert( pPager->dbOrigSize==pPager->dbFileSize );
   749    757         break;
   750    758   
   751    759       case PAGER_WRITER_DBMOD:
          760  +      assert( pPager->errCode==SQLITE_OK );
   752    761         assert( !pagerUseWal(pPager) );
   753    762         assert( p->eLock>=EXCLUSIVE_LOCK || pagerUseWal(pPager) );
   754    763         assert( isOpen(p->jfd) 
   755    764              || p->journalMode==PAGER_JOURNALMODE_OFF 
   756    765              || p->journalMode==PAGER_JOURNALMODE_WAL 
   757    766         );
   758    767         break;
   759    768   
   760    769       case PAGER_WRITER_FINISHED:
          770  +      assert( pPager->errCode==SQLITE_OK );
   761    771         assert( !pagerUseWal(pPager) );
   762    772         assert( p->eLock>=EXCLUSIVE_LOCK || pagerUseWal(pPager) );
   763    773         assert( isOpen(p->jfd) 
   764    774              || p->journalMode==PAGER_JOURNALMODE_OFF 
   765    775              || p->journalMode==PAGER_JOURNALMODE_WAL 
   766    776         );
   767    777         break;
          778  +
          779  +    case PAGER_ERROR:
          780  +      /* There must be at least one outstanding reference to the pager if
          781  +      ** in ERROR state. Otherwise the pager should have already dropped
          782  +      ** back to NONE state.
          783  +      */
          784  +      assert( pPager->errCode!=SQLITE_OK );
          785  +      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
          786  +      break;
   768    787     }
   769    788   
   770    789     return 1;
   771    790   }
   772    791   
   773    792   /*
   774    793   ** (gdb) printf "%s", print_pager_state(pPager)
................................................................................
   783    802         "Journal mode:  journal_mode=%s\n"
   784    803         "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
   785    804         , p->eState==PAGER_NONE            ? "NONE" :
   786    805           p->eState==PAGER_READER          ? "READER" :
   787    806           p->eState==PAGER_WRITER_INITIAL  ? "WRITER_INITIAL" :
   788    807           p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
   789    808           p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
   790         -        p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" : "?error?"
          809  +        p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
          810  +        p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
   791    811         , p->eLock==NO_LOCK         ? "NONE" :
   792    812           p->eLock==RESERVED_LOCK   ? "RESERVED" :
   793    813           p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
   794    814           p->eLock==SHARED_LOCK     ? "SHARED" : "?error?"
   795    815         , p->exclusiveMode ? "exclusive" : "normal"
   796    816         , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
   797    817           p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
................................................................................
  1439   1459     ** fail, since no attempt to allocate dynamic memory will be made.
  1440   1460     */
  1441   1461     (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
  1442   1462     return p;
  1443   1463   }
  1444   1464   
  1445   1465   /*
  1446         -** Unless the pager is in error-state, discard all in-memory pages. If
  1447         -** the pager is in error-state, then this call is a no-op.
  1448         -**
  1449         -** TODO: Why can we not reset the pager while in error state?
         1466  +** Discard the entire contents of the in-memory page-cache.
  1450   1467   */
  1451   1468   static void pager_reset(Pager *pPager){
  1452         -  if( SQLITE_OK==pPager->errCode ){
  1453         -    sqlite3BackupRestart(pPager->pBackup);
  1454         -    sqlite3PcacheClear(pPager->pPCache);
  1455         -  }
         1469  +  sqlite3BackupRestart(pPager->pBackup);
         1470  +  sqlite3PcacheClear(pPager->pPCache);
  1456   1471   }
  1457   1472   
  1458   1473   /*
  1459   1474   ** Free all structures in the Pager.aSavepoint[] array and set both
  1460   1475   ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
  1461   1476   ** if it is open and the pager is not in exclusive mode.
  1462   1477   */
................................................................................
  1501   1516   ** If the pager is currently in error state, discard the contents of 
  1502   1517   ** the cache and reset the Pager structure internal state. If there is
  1503   1518   ** an open journal-file, then the next time a shared-lock is obtained
  1504   1519   ** on the pager file (by this or any other process), it will be
  1505   1520   ** treated as a hot-journal and rolled back.
  1506   1521   */
  1507   1522   static void pager_unlock(Pager *pPager){
  1508         -  if( !pPager->exclusiveMode ){
  1509         -    int rc = SQLITE_OK;          /* Return code */
         1523  +
         1524  +  sqlite3BitvecDestroy(pPager->pInJournal);
         1525  +  pPager->pInJournal = 0;
         1526  +  releaseAllSavepoints(pPager);
         1527  +
         1528  +  if( pagerUseWal(pPager) ){
         1529  +    assert( !isOpen(pPager->jfd) );
         1530  +    sqlite3WalEndReadTransaction(pPager->pWal);
         1531  +    pPager->eState = PAGER_NONE;
         1532  +  }else if( !pPager->exclusiveMode ){
  1510   1533       int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
  1511   1534   
  1512   1535       /* If the operating system support deletion of open files, then
  1513   1536       ** close the journal file when dropping the database lock.  Otherwise
  1514   1537       ** another connection with journal_mode=delete might delete the file
  1515   1538       ** out from under us.
  1516   1539       */
  1517   1540       assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
  1518   1541       assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
  1519   1542       assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
  1520   1543       assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
  1521   1544       assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
  1522   1545       assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
  1523         -
  1524   1546       if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
  1525   1547        || 1!=(pPager->journalMode & 5)
  1526   1548       ){
  1527   1549         sqlite3OsClose(pPager->jfd);
  1528   1550       }
         1551  +    osUnlock(pPager, NO_LOCK);
  1529   1552   
  1530         -    sqlite3BitvecDestroy(pPager->pInJournal);
  1531         -    pPager->pInJournal = 0;
  1532         -    releaseAllSavepoints(pPager);
  1533         -
  1534         -    if( pagerUseWal(pPager) ){
  1535         -      sqlite3WalEndReadTransaction(pPager->pWal);
  1536         -    }else{
  1537         -      rc = osUnlock(pPager, NO_LOCK);
  1538         -    }
  1539         -    if( rc ){
  1540         -      pPager->errCode = rc;
  1541         -    }
         1553  +    /* The pager state may be changed from PAGER_ERROR to PAGER_NONE here
         1554  +    ** without clearing the error code. This is intentional - the error
         1555  +    ** code is cleared and the cache reset in the block below.
         1556  +    */
         1557  +    assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
         1558  +    pPager->changeCountDone = 0;
         1559  +    pPager->eState = PAGER_NONE;
  1542   1560       IOTRACE(("UNLOCK %p\n", pPager))
         1561  +  }
  1543   1562   
  1544         -    /* If Pager.errCode is set, the contents of the pager cache cannot be
  1545         -    ** trusted. Now that the pager file is unlocked, the contents of the
  1546         -    ** cache can be discarded and the error code safely cleared.
  1547         -    */
  1548         -    if( pPager->errCode ){
  1549         -      if( rc==SQLITE_OK ){
  1550         -        pPager->errCode = SQLITE_OK;
  1551         -      }
  1552         -      pager_reset(pPager);
  1553         -    }
  1554         -
         1563  +  /* If Pager.errCode is set, the contents of the pager cache cannot be
         1564  +  ** trusted. Now that there are no outstanding references to the pager,
         1565  +  ** it can safely move back to PAGER_NONE state. This happens in both
         1566  +  ** normal and exclusive-locking mode.
         1567  +  */
         1568  +  if( pPager->errCode ){
         1569  +    pager_reset(pPager);
  1555   1570       pPager->changeCountDone = 0;
  1556   1571       pPager->eState = PAGER_NONE;
         1572  +    pPager->errCode = SQLITE_OK;
  1557   1573     }
  1558   1574   }
  1559   1575   
  1560   1576   /*
  1561   1577   ** This function should be called when an IOERR, CORRUPT or FULL error
  1562   1578   ** may have occurred. The first argument is a pointer to the pager 
  1563   1579   ** structure, the second the error-code about to be returned by a pager 
................................................................................
  1582   1598     assert(
  1583   1599          pPager->errCode==SQLITE_FULL ||
  1584   1600          pPager->errCode==SQLITE_OK ||
  1585   1601          (pPager->errCode & 0xff)==SQLITE_IOERR
  1586   1602     );
  1587   1603     if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
  1588   1604       pPager->errCode = rc;
         1605  +    pPager->eState = PAGER_ERROR;
  1589   1606     }
  1590   1607     return rc;
  1591   1608   }
  1592   1609   
  1593   1610   /*
  1594   1611   ** This routine ends a transaction. A transaction is usually ended by 
  1595   1612   ** either a COMMIT or a ROLLBACK operation. This routine may be called 
................................................................................
  1648   1665   */
  1649   1666   static int pager_end_transaction(Pager *pPager, int hasMaster){
  1650   1667     int rc = SQLITE_OK;      /* Error code from journal finalization operation */
  1651   1668     int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
  1652   1669   
  1653   1670     /* Do nothing if the pager does not have an open write transaction. */
  1654   1671     assert( assert_pager_state(pPager) );
         1672  +  assert( pPager->eState!=PAGER_ERROR );
  1655   1673     if( pPager->eState<PAGER_WRITER_INITIAL && pPager->eLock<RESERVED_LOCK ){
  1656   1674       return SQLITE_OK;
  1657   1675     }
  1658   1676   
  1659   1677     releaseAllSavepoints(pPager);
  1660   1678     assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
  1661   1679     if( isOpen(pPager->jfd) ){
................................................................................
  1737   1755   **
  1738   1756   ** If the pager has not already entered the error state, but an IO or
  1739   1757   ** malloc error occurs during a rollback, then this will itself cause 
  1740   1758   ** the pager to enter the error state. Which will be cleared by the
  1741   1759   ** call to pager_unlock(), as described above.
  1742   1760   */
  1743   1761   static void pagerUnlockAndRollback(Pager *pPager){
  1744         -  if( pPager->errCode==SQLITE_OK ){
         1762  +  if( pPager->eState!=PAGER_ERROR ){
         1763  +    assert( assert_pager_state(pPager) );
  1745   1764       if( pPager->eState>=PAGER_WRITER_INITIAL ){
  1746   1765         sqlite3BeginBenignMalloc();
  1747   1766         sqlite3PagerRollback(pPager);
  1748   1767         sqlite3EndBenignMalloc();
  1749   1768       }else if( pPager->eLock>=RESERVED_LOCK && !pPager->exclusiveMode ){
  1750   1769         pager_end_transaction(pPager, 0);
  1751   1770       }
................................................................................
  2195   2214   ** the end of the new file instead.
  2196   2215   **
  2197   2216   ** If successful, return SQLITE_OK. If an IO error occurs while modifying
  2198   2217   ** the database file, return the error code to the caller.
  2199   2218   */
  2200   2219   static int pager_truncate(Pager *pPager, Pgno nPage){
  2201   2220     int rc = SQLITE_OK;
         2221  +  assert( pPager->eState!=PAGER_ERROR );
  2202   2222     if( pPager->eState>=PAGER_WRITER_DBMOD && isOpen(pPager->fd) ){
  2203   2223       i64 currentSize, newSize;
  2204   2224       /* TODO: Is it safe to use Pager.dbFileSize here? */
  2205   2225       rc = sqlite3OsFileSize(pPager->fd, &currentSize);
  2206   2226       newSize = pPager->pageSize*(i64)nPage;
  2207   2227       if( rc==SQLITE_OK && currentSize!=newSize ){
  2208   2228         if( currentSize>newSize ){
................................................................................
  2829   2849   */
  2830   2850   static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
  2831   2851     i64 szJ;                 /* Effective size of the main journal */
  2832   2852     i64 iHdrOff;             /* End of first segment of main-journal records */
  2833   2853     int rc = SQLITE_OK;      /* Return code */
  2834   2854     Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
  2835   2855   
         2856  +  assert( pPager->eState!=PAGER_ERROR );
  2836   2857     assert( pPager->eState>=PAGER_WRITER_INITIAL );
  2837   2858   
  2838   2859     /* Allocate a bitvec to use to store the set of pages rolled back */
  2839   2860     if( pSavepoint ){
  2840   2861       pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
  2841   2862       if( !pDone ){
  2842   2863         return SQLITE_NOMEM;
................................................................................
  3075   3096   ** If the page size is not changed, either because one of the enumerated
  3076   3097   ** conditions above is not true, the pager was in error state when this
  3077   3098   ** function was called, or because the memory allocation attempt failed, 
  3078   3099   ** then *pPageSize is set to the old, retained page size before returning.
  3079   3100   */
  3080   3101   int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize, int nReserve){
  3081   3102     int rc = pPager->errCode;
         3103  +
         3104  +  /* It is not possible to do a full assert_pager_state() here, as this
         3105  +  ** function may be called from within PagerOpen(), before the state
         3106  +  ** of the Pager object is internally consistent.
         3107  +  */
         3108  +  assert( rc==SQLITE_OK || pPager->eState==PAGER_ERROR );
  3082   3109   
  3083   3110     if( rc==SQLITE_OK ){
  3084   3111       u16 pageSize = *pPageSize;
  3085   3112       assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
  3086   3113       if( (pPager->memDb==0 || pPager->dbSize==0)
  3087   3114        && sqlite3PcacheRefCount(pPager->pPCache)==0 
  3088   3115        && pageSize && pageSize!=pPager->pageSize 
................................................................................
  3366   3393   ** to the caller.
  3367   3394   */
  3368   3395   int sqlite3PagerClose(Pager *pPager){
  3369   3396     u8 *pTmp = (u8 *)pPager->pTmpSpace;
  3370   3397   
  3371   3398     disable_simulated_io_errors();
  3372   3399     sqlite3BeginBenignMalloc();
  3373         -  pPager->errCode = 0;
         3400  +  /* pPager->errCode = 0; */
  3374   3401     pPager->exclusiveMode = 0;
  3375   3402   #ifndef SQLITE_OMIT_WAL
  3376   3403     sqlite3WalClose(pPager->pWal,
  3377   3404       (pPager->noSync ? 0 : pPager->sync_flags), 
  3378   3405       pPager->pageSize, pTmp
  3379   3406     );
  3380   3407     pPager->pWal = 0;
  3381   3408   #endif
  3382   3409     pager_reset(pPager);
  3383   3410     if( MEMDB ){
  3384   3411       pager_unlock(pPager);
  3385   3412     }else{
  3386         -    /* Set Pager.journalHdr to -1 for the benefit of the pager_playback() 
  3387         -    ** call which may be made from within pagerUnlockAndRollback(). If it
  3388         -    ** is not -1, then the unsynced portion of an open journal file may
  3389         -    ** be played back into the database. If a power failure occurs while
  3390         -    ** this is happening, the database may become corrupt.
         3413  +    /* If it is open, sync the journal file before calling UnlockAndRollback.
         3414  +    ** If this is not done, then an unsynced portion of the open journal 
         3415  +    ** file may be played back into the database. If a power failure occurs 
         3416  +    ** while this is happening, the database could become corrupt.
         3417  +    **
         3418  +    ** If an error occurs while trying to sync the journal, shift the pager
         3419  +    ** into the ERROR state. This causes UnlockAndRollback to unlock the
         3420  +    ** database and close the journal file without attempting to roll it
         3421  +    ** back or finalize it. The next database user will have to do hot-journal
         3422  +    ** rollback before accessing the database file.
  3391   3423       */
  3392   3424       if( isOpen(pPager->jfd) ){
  3393         -      pPager->errCode = pagerSyncHotJournal(pPager);
         3425  +      pager_error(pPager, pagerSyncHotJournal(pPager));
  3394   3426       }
  3395   3427       pagerUnlockAndRollback(pPager);
  3396   3428     }
  3397   3429     sqlite3EndBenignMalloc();
  3398   3430     enable_simulated_io_errors();
  3399   3431     PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
  3400   3432     IOTRACE(("CLOSE %p\n", pPager))
................................................................................
  4355   4387     ** exclusive access mode.
  4356   4388     */
  4357   4389     assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
  4358   4390     assert( pPager->eState==PAGER_NONE || pPager->eState==PAGER_READER );
  4359   4391     assert( assert_pager_state(pPager) );
  4360   4392     if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
  4361   4393   
  4362         -  /* If this database is in an error-state, now is a chance to clear
  4363         -  ** the error. Discard the contents of the pager-cache and rollback
  4364         -  ** any hot journal in the file-system.
  4365         -  */
  4366         -  if( pPager->errCode ){
  4367         -    if( isOpen(pPager->jfd) || pPager->zJournal ){
  4368         -      isErrorReset = 1;
  4369         -    }
  4370         -    pPager->errCode = SQLITE_OK;
  4371         -    pPager->eState = PAGER_NONE;
  4372         -    pager_reset(pPager);
  4373         -  }
  4374         -
  4375   4394     if( !pagerUseWal(pPager) && pPager->eState==PAGER_NONE ){
  4376   4395       sqlite3_vfs * const pVfs = pPager->pVfs;
  4377   4396   
  4378   4397       assert( !MEMDB && !pPager->tempFile );
  4379         -    assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
  4380   4398       assert( pPager->noReadlock==0 || pPager->readOnly );
  4381   4399   
  4382   4400       if( pPager->noReadlock==0 ){
  4383   4401         rc = pager_wait_on_lock(pPager, SHARED_LOCK);
  4384   4402         if( rc!=SQLITE_OK ){
  4385   4403           assert( pPager->eLock==PAGER_UNLOCK );
  4386   4404           goto failed;
................................................................................
  4470   4488         ** Even if an error occurs while syncing or rolling back the 
  4471   4489         ** hot-journal, there is no need to enter the ERROR state here, as
  4472   4490         ** the pager never left state NONE anyhow.
  4473   4491         */
  4474   4492         if( isOpen(pPager->jfd) ){
  4475   4493           rc = pagerSyncHotJournal(pPager);
  4476   4494           if( rc==SQLITE_OK ){
         4495  +          assert( pPager->eState==PAGER_NONE );
  4477   4496             pPager->eState = PAGER_WRITER_FINISHED;
  4478   4497             rc = pager_playback(pPager, 1);
  4479   4498             pPager->eState = PAGER_NONE;
  4480   4499           }
  4481   4500           if( rc!=SQLITE_OK ){
  4482   4501             goto failed;
  4483   4502           }
................................................................................
  4536   4555     }
  4537   4556   
  4538   4557     if( pagerUseWal(pPager) && rc==SQLITE_OK ){
  4539   4558       rc = pagerBeginReadTransaction(pPager);
  4540   4559     }
  4541   4560   
  4542   4561     if( pPager->eState==PAGER_NONE && rc==SQLITE_OK ){
  4543         -    pPager->eState = PAGER_NONE;
  4544   4562       rc = pagerPagecount(pPager, &pPager->dbSize);
  4545   4563     }
  4546   4564   
  4547   4565    failed:
  4548   4566     if( rc!=SQLITE_OK ){
  4549   4567       pager_unlock(pPager);
  4550         -    assert( (pPager->eState==PAGER_NONE)
  4551         -        ||  (pPager->exclusiveMode && pagerUseWal(pPager))
  4552         -    );
         4568  +    assert( pPager->eState==PAGER_NONE );
  4553   4569     }else{
  4554   4570       pPager->eState = PAGER_READER;
  4555   4571     }
  4556   4572     return rc;
  4557   4573   }
  4558   4574   
  4559   4575   /*
................................................................................
  4958   4974   static int pager_write(PgHdr *pPg){
  4959   4975     void *pData = pPg->pData;
  4960   4976     Pager *pPager = pPg->pPager;
  4961   4977     int rc = SQLITE_OK;
  4962   4978   
  4963   4979     /* This routine is not called unless a write-transaction has already 
  4964   4980     ** been started. The journal file may or may not be open at this point.
         4981  +  ** It is never called in the ERROR state.
  4965   4982     */
  4966         -  assert( pPager->eState>=PAGER_WRITER_INITIAL );
  4967         -  assert( pPager->eState!=PAGER_WRITER_FINISHED );
         4983  +  assert( pPager->eState==PAGER_WRITER_INITIAL
         4984  +       || pPager->eState==PAGER_WRITER_CACHEMOD
         4985  +       || pPager->eState==PAGER_WRITER_DBMOD
         4986  +  );
  4968   4987     assert( assert_pager_state(pPager) );
  4969   4988   
  4970   4989     /* If an error has been previously detected, report the same error
  4971         -  ** again.
  4972         -  */
         4990  +  ** again. This should not happen, but the check provides robustness. */
  4973   4991     if( NEVER(pPager->errCode) )  return pPager->errCode;
  4974   4992   
  4975   4993     /* Higher-level routines never call this function if database is not
  4976   4994     ** writable.  But check anyway, just for robustness. */
  4977   4995     if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
  4978   4996   
  4979   4997     CHECK_PAGE(pPg);
................................................................................
  5109   5127     int rc = SQLITE_OK;
  5110   5128   
  5111   5129     PgHdr *pPg = pDbPage;
  5112   5130     Pager *pPager = pPg->pPager;
  5113   5131     Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
  5114   5132   
  5115   5133     assert( pPager->eState>=PAGER_WRITER_INITIAL );
         5134  +  assert( pPager->eState!=PAGER_ERROR );
  5116   5135     assert( assert_pager_state(pPager) );
  5117   5136   
  5118   5137     if( nPagePerSector>1 ){
  5119   5138       Pgno nPageCount;          /* Total number of pages in database file */
  5120   5139       Pgno pg1;                 /* First page of the sector pPg is located on. */
  5121   5140       int nPage = 0;            /* Number of pages starting at pg1 to journal */
  5122   5141       int ii;                   /* Loop counter */
................................................................................
  5404   5423   
  5405   5424     assert( pPager->eState==PAGER_WRITER_INITIAL
  5406   5425          || pPager->eState==PAGER_WRITER_CACHEMOD
  5407   5426          || pPager->eState==PAGER_WRITER_DBMOD
  5408   5427     );
  5409   5428     assert( assert_pager_state(pPager) );
  5410   5429   
  5411         -#if 0
  5412         -  /* The dbOrigSize is never set if journal_mode=OFF */
  5413         -  assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
  5414         -#endif
  5415         -
  5416   5430     /* If a prior error occurred, report that error again. */
  5417   5431     if( pPager->errCode ) return pPager->errCode;
  5418   5432   
  5419   5433     PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
  5420   5434         pPager->zFilename, zMaster, pPager->dbSize));
  5421   5435   
  5422   5436     /* If no database changes have been made, return early. */
................................................................................
  5678   5692   **   some other process can use it to restore the database state (by
  5679   5693   **   hot-journal rollback).
  5680   5694   */
  5681   5695   int sqlite3PagerRollback(Pager *pPager){
  5682   5696     int rc = SQLITE_OK;                  /* Return code */
  5683   5697     PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
  5684   5698   
  5685         -  /* PagerRollback() is a no-op if called in READER or NONE state. */
         5699  +  /* PagerRollback() is a no-op if called in READER or NONE state. If
         5700  +  ** the pager is already in the ERROR state, the rollback is not 
         5701  +  ** attempted here. Instead, the error code is returned to the caller.
         5702  +  */
  5686   5703     assert( assert_pager_state(pPager) );
         5704  +  if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
  5687   5705     if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
  5688   5706   
  5689   5707     if( pagerUseWal(pPager) ){
  5690   5708       int rc2;
  5691   5709       rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
  5692   5710       rc2 = pager_end_transaction(pPager, pPager->setMaster);
  5693   5711       if( rc==SQLITE_OK ) rc = rc2;
  5694         -  }else if( pPager->eState==PAGER_WRITER_INITIAL ){
         5712  +  }else if( !isOpen(pPager->jfd) ){
  5695   5713       rc = pager_end_transaction(pPager, 0);
  5696         -  }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
  5697         -    if( pPager->eState>=PAGER_WRITER_DBMOD ){
  5698         -      pager_playback(pPager, 0);
  5699         -    }
  5700         -    rc = pPager->errCode;
  5701   5714     }else{
  5702         -    if( pPager->eState==PAGER_WRITER_CACHEMOD ){
  5703         -      int rc2;
  5704         -      rc = pager_playback(pPager, 0);
  5705         -      rc2 = pager_end_transaction(pPager, pPager->setMaster);
  5706         -      if( rc==SQLITE_OK ){
  5707         -        rc = rc2;
  5708         -      }
  5709         -    }else{
  5710         -      rc = pager_playback(pPager, 0);
  5711         -    }
         5715  +    rc = pager_playback(pPager, 0);
  5712   5716     }
         5717  +  assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
         5718  +  assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
  5713   5719   
  5714   5720     /* If an error occurs during a ROLLBACK, we can no longer trust the pager
  5715   5721     ** cache. So call pager_error() on the way out to make any error persistent.
  5716   5722     */
  5717   5723     return pager_error(pPager, rc);
  5718   5724   }
  5719   5725