000001  /*
000002  ** 2009 January 28
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** This file contains the implementation of the sqlite3_backup_XXX() 
000013  ** API functions and the related features.
000014  */
000015  #include "sqliteInt.h"
000016  #include "btreeInt.h"
000017  
000018  /*
000019  ** Structure allocated for each backup operation.
000020  */
000021  struct sqlite3_backup {
000022    sqlite3* pDestDb;        /* Destination database handle */
000023    Btree *pDest;            /* Destination b-tree file */
000024    u32 iDestSchema;         /* Original schema cookie in destination */
000025    int bDestLocked;         /* True once a write-transaction is open on pDest */
000026  
000027    Pgno iNext;              /* Page number of the next source page to copy */
000028    sqlite3* pSrcDb;         /* Source database handle */
000029    Btree *pSrc;             /* Source b-tree file */
000030  
000031    int rc;                  /* Backup process error code */
000032  
000033    /* These two variables are set by every call to backup_step(). They are
000034    ** read by calls to backup_remaining() and backup_pagecount().
000035    */
000036    Pgno nRemaining;         /* Number of pages left to copy */
000037    Pgno nPagecount;         /* Total number of pages to copy */
000038  
000039    int isAttached;          /* True once backup has been registered with pager */
000040    sqlite3_backup *pNext;   /* Next backup associated with source pager */
000041  };
000042  
000043  /*
000044  ** THREAD SAFETY NOTES:
000045  **
000046  **   Once it has been created using backup_init(), a single sqlite3_backup
000047  **   structure may be accessed via two groups of thread-safe entry points:
000048  **
000049  **     * Via the sqlite3_backup_XXX() API function backup_step() and 
000050  **       backup_finish(). Both these functions obtain the source database
000051  **       handle mutex and the mutex associated with the source BtShared 
000052  **       structure, in that order.
000053  **
000054  **     * Via the BackupUpdate() and BackupRestart() functions, which are
000055  **       invoked by the pager layer to report various state changes in
000056  **       the page cache associated with the source database. The mutex
000057  **       associated with the source database BtShared structure will always 
000058  **       be held when either of these functions are invoked.
000059  **
000060  **   The other sqlite3_backup_XXX() API functions, backup_remaining() and
000061  **   backup_pagecount() are not thread-safe functions. If they are called
000062  **   while some other thread is calling backup_step() or backup_finish(),
000063  **   the values returned may be invalid. There is no way for a call to
000064  **   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
000065  **   or backup_pagecount().
000066  **
000067  **   Depending on the SQLite configuration, the database handles and/or
000068  **   the Btree objects may have their own mutexes that require locking.
000069  **   Non-sharable Btrees (in-memory databases for example), do not have
000070  **   associated mutexes.
000071  */
000072  
000073  /*
000074  ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
000075  ** in connection handle pDb. If such a database cannot be found, return
000076  ** a NULL pointer and write an error message to pErrorDb.
000077  **
000078  ** If the "temp" database is requested, it may need to be opened by this 
000079  ** function. If an error occurs while doing so, return 0 and write an 
000080  ** error message to pErrorDb.
000081  */
000082  static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
000083    int i = sqlite3FindDbName(pDb, zDb);
000084  
000085    if( i==1 ){
000086      Parse sParse;
000087      int rc = 0;
000088      sqlite3ParseObjectInit(&sParse,pDb);
000089      if( sqlite3OpenTempDatabase(&sParse) ){
000090        sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
000091        rc = SQLITE_ERROR;
000092      }
000093      sqlite3DbFree(pErrorDb, sParse.zErrMsg);
000094      sqlite3ParseObjectReset(&sParse);
000095      if( rc ){
000096        return 0;
000097      }
000098    }
000099  
000100    if( i<0 ){
000101      sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
000102      return 0;
000103    }
000104  
000105    return pDb->aDb[i].pBt;
000106  }
000107  
000108  /*
000109  ** Attempt to set the page size of the destination to match the page size
000110  ** of the source.
000111  */
000112  static int setDestPgsz(sqlite3_backup *p){
000113    int rc;
000114    rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
000115    return rc;
000116  }
000117  
000118  /*
000119  ** Check that there is no open read-transaction on the b-tree passed as the
000120  ** second argument. If there is not, return SQLITE_OK. Otherwise, if there
000121  ** is an open read-transaction, return SQLITE_ERROR and leave an error 
000122  ** message in database handle db.
000123  */
000124  static int checkReadTransaction(sqlite3 *db, Btree *p){
000125    if( sqlite3BtreeTxnState(p)!=SQLITE_TXN_NONE ){
000126      sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
000127      return SQLITE_ERROR;
000128    }
000129    return SQLITE_OK;
000130  }
000131  
000132  /*
000133  ** Create an sqlite3_backup process to copy the contents of zSrcDb from
000134  ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
000135  ** a pointer to the new sqlite3_backup object.
000136  **
000137  ** If an error occurs, NULL is returned and an error code and error message
000138  ** stored in database handle pDestDb.
000139  */
000140  sqlite3_backup *sqlite3_backup_init(
000141    sqlite3* pDestDb,                     /* Database to write to */
000142    const char *zDestDb,                  /* Name of database within pDestDb */
000143    sqlite3* pSrcDb,                      /* Database connection to read from */
000144    const char *zSrcDb                    /* Name of database within pSrcDb */
000145  ){
000146    sqlite3_backup *p;                    /* Value to return */
000147  
000148  #ifdef SQLITE_ENABLE_API_ARMOR
000149    if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
000150      (void)SQLITE_MISUSE_BKPT;
000151      return 0;
000152    }
000153  #endif
000154  
000155    /* Lock the source database handle. The destination database
000156    ** handle is not locked in this routine, but it is locked in
000157    ** sqlite3_backup_step(). The user is required to ensure that no
000158    ** other thread accesses the destination handle for the duration
000159    ** of the backup operation.  Any attempt to use the destination
000160    ** database connection while a backup is in progress may cause
000161    ** a malfunction or a deadlock.
000162    */
000163    sqlite3_mutex_enter(pSrcDb->mutex);
000164    sqlite3_mutex_enter(pDestDb->mutex);
000165  
000166    if( pSrcDb==pDestDb ){
000167      sqlite3ErrorWithMsg(
000168          pDestDb, SQLITE_ERROR, "source and destination must be distinct"
000169      );
000170      p = 0;
000171    }else {
000172      /* Allocate space for a new sqlite3_backup object...
000173      ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
000174      ** call to sqlite3_backup_init() and is destroyed by a call to
000175      ** sqlite3_backup_finish(). */
000176      p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
000177      if( !p ){
000178        sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
000179      }
000180    }
000181  
000182    /* If the allocation succeeded, populate the new object. */
000183    if( p ){
000184      p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
000185      p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
000186      p->pDestDb = pDestDb;
000187      p->pSrcDb = pSrcDb;
000188      p->iNext = 1;
000189      p->isAttached = 0;
000190  
000191      if( 0==p->pSrc || 0==p->pDest 
000192       || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK 
000193       ){
000194        /* One (or both) of the named databases did not exist or an OOM
000195        ** error was hit. Or there is a transaction open on the destination
000196        ** database. The error has already been written into the pDestDb 
000197        ** handle. All that is left to do here is free the sqlite3_backup 
000198        ** structure.  */
000199        sqlite3_free(p);
000200        p = 0;
000201      }
000202    }
000203    if( p ){
000204      p->pSrc->nBackup++;
000205    }
000206  
000207    sqlite3_mutex_leave(pDestDb->mutex);
000208    sqlite3_mutex_leave(pSrcDb->mutex);
000209    return p;
000210  }
000211  
000212  /*
000213  ** Argument rc is an SQLite error code. Return true if this error is 
000214  ** considered fatal if encountered during a backup operation. All errors
000215  ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
000216  */
000217  static int isFatalError(int rc){
000218    return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
000219  }
000220  
000221  /*
000222  ** Parameter zSrcData points to a buffer containing the data for 
000223  ** page iSrcPg from the source database. Copy this data into the 
000224  ** destination database.
000225  */
000226  static int backupOnePage(
000227    sqlite3_backup *p,              /* Backup handle */
000228    Pgno iSrcPg,                    /* Source database page to backup */
000229    const u8 *zSrcData,             /* Source database page data */
000230    int bUpdate                     /* True for an update, false otherwise */
000231  ){
000232    Pager * const pDestPager = sqlite3BtreePager(p->pDest);
000233    const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
000234    int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
000235    const int nCopy = MIN(nSrcPgsz, nDestPgsz);
000236    const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
000237    int rc = SQLITE_OK;
000238    i64 iOff;
000239  
000240    assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
000241    assert( p->bDestLocked );
000242    assert( !isFatalError(p->rc) );
000243    assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
000244    assert( zSrcData );
000245    assert( nSrcPgsz==nDestPgsz || sqlite3PagerIsMemdb(pDestPager)==0 );
000246  
000247    /* This loop runs once for each destination page spanned by the source 
000248    ** page. For each iteration, variable iOff is set to the byte offset
000249    ** of the destination page.
000250    */
000251    for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
000252      DbPage *pDestPg = 0;
000253      Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
000254      if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
000255      if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
000256       && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
000257      ){
000258        const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
000259        u8 *zDestData = sqlite3PagerGetData(pDestPg);
000260        u8 *zOut = &zDestData[iOff%nDestPgsz];
000261  
000262        /* Copy the data from the source page into the destination page.
000263        ** Then clear the Btree layer MemPage.isInit flag. Both this module
000264        ** and the pager code use this trick (clearing the first byte
000265        ** of the page 'extra' space to invalidate the Btree layers
000266        ** cached parse of the page). MemPage.isInit is marked 
000267        ** "MUST BE FIRST" for this purpose.
000268        */
000269        memcpy(zOut, zIn, nCopy);
000270        ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
000271        if( iOff==0 && bUpdate==0 ){
000272          sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
000273        }
000274      }
000275      sqlite3PagerUnref(pDestPg);
000276    }
000277  
000278    return rc;
000279  }
000280  
000281  /*
000282  ** If pFile is currently larger than iSize bytes, then truncate it to
000283  ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
000284  ** this function is a no-op.
000285  **
000286  ** Return SQLITE_OK if everything is successful, or an SQLite error 
000287  ** code if an error occurs.
000288  */
000289  static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
000290    i64 iCurrent;
000291    int rc = sqlite3OsFileSize(pFile, &iCurrent);
000292    if( rc==SQLITE_OK && iCurrent>iSize ){
000293      rc = sqlite3OsTruncate(pFile, iSize);
000294    }
000295    return rc;
000296  }
000297  
000298  /*
000299  ** Register this backup object with the associated source pager for
000300  ** callbacks when pages are changed or the cache invalidated.
000301  */
000302  static void attachBackupObject(sqlite3_backup *p){
000303    sqlite3_backup **pp;
000304    assert( sqlite3BtreeHoldsMutex(p->pSrc) );
000305    pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
000306    p->pNext = *pp;
000307    *pp = p;
000308    p->isAttached = 1;
000309  }
000310  
000311  /*
000312  ** Copy nPage pages from the source b-tree to the destination.
000313  */
000314  int sqlite3_backup_step(sqlite3_backup *p, int nPage){
000315    int rc;
000316    int destMode;       /* Destination journal mode */
000317    int pgszSrc = 0;    /* Source page size */
000318    int pgszDest = 0;   /* Destination page size */
000319  
000320  #ifdef SQLITE_ENABLE_API_ARMOR
000321    if( p==0 ) return SQLITE_MISUSE_BKPT;
000322  #endif
000323    sqlite3_mutex_enter(p->pSrcDb->mutex);
000324    sqlite3BtreeEnter(p->pSrc);
000325    if( p->pDestDb ){
000326      sqlite3_mutex_enter(p->pDestDb->mutex);
000327    }
000328  
000329    rc = p->rc;
000330    if( !isFatalError(rc) ){
000331      Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
000332      Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
000333      int ii;                            /* Iterator variable */
000334      int nSrcPage = -1;                 /* Size of source db in pages */
000335      int bCloseTrans = 0;               /* True if src db requires unlocking */
000336  
000337      /* If the source pager is currently in a write-transaction, return
000338      ** SQLITE_BUSY immediately.
000339      */
000340      if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
000341        rc = SQLITE_BUSY;
000342      }else{
000343        rc = SQLITE_OK;
000344      }
000345  
000346      /* If there is no open read-transaction on the source database, open
000347      ** one now. If a transaction is opened here, then it will be closed
000348      ** before this function exits.
000349      */
000350      if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
000351        rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
000352        bCloseTrans = 1;
000353      }
000354  
000355      /* If the destination database has not yet been locked (i.e. if this
000356      ** is the first call to backup_step() for the current backup operation),
000357      ** try to set its page size to the same as the source database. This
000358      ** is especially important on ZipVFS systems, as in that case it is
000359      ** not possible to create a database file that uses one page size by
000360      ** writing to it with another.  */
000361      if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
000362        rc = SQLITE_NOMEM;
000363      }
000364  
000365      /* Lock the destination database, if it is not locked already. */
000366      if( SQLITE_OK==rc && p->bDestLocked==0
000367       && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
000368                                                  (int*)&p->iDestSchema)) 
000369      ){
000370        p->bDestLocked = 1;
000371      }
000372  
000373      /* Do not allow backup if the destination database is in WAL mode
000374      ** and the page sizes are different between source and destination */
000375      pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
000376      pgszDest = sqlite3BtreeGetPageSize(p->pDest);
000377      destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
000378      if( SQLITE_OK==rc 
000379       && (destMode==PAGER_JOURNALMODE_WAL || sqlite3PagerIsMemdb(pDestPager))
000380       && pgszSrc!=pgszDest 
000381      ){
000382        rc = SQLITE_READONLY;
000383      }
000384    
000385      /* Now that there is a read-lock on the source database, query the
000386      ** source pager for the number of pages in the database.
000387      */
000388      nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
000389      assert( nSrcPage>=0 );
000390      for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
000391        const Pgno iSrcPg = p->iNext;                 /* Source page number */
000392        if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
000393          DbPage *pSrcPg;                             /* Source page object */
000394          rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
000395          if( rc==SQLITE_OK ){
000396            rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
000397            sqlite3PagerUnref(pSrcPg);
000398          }
000399        }
000400        p->iNext++;
000401      }
000402      if( rc==SQLITE_OK ){
000403        p->nPagecount = nSrcPage;
000404        p->nRemaining = nSrcPage+1-p->iNext;
000405        if( p->iNext>(Pgno)nSrcPage ){
000406          rc = SQLITE_DONE;
000407        }else if( !p->isAttached ){
000408          attachBackupObject(p);
000409        }
000410      }
000411    
000412      /* Update the schema version field in the destination database. This
000413      ** is to make sure that the schema-version really does change in
000414      ** the case where the source and destination databases have the
000415      ** same schema version.
000416      */
000417      if( rc==SQLITE_DONE ){
000418        if( nSrcPage==0 ){
000419          rc = sqlite3BtreeNewDb(p->pDest);
000420          nSrcPage = 1;
000421        }
000422        if( rc==SQLITE_OK || rc==SQLITE_DONE ){
000423          rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
000424        }
000425        if( rc==SQLITE_OK ){
000426          if( p->pDestDb ){
000427            sqlite3ResetAllSchemasOfConnection(p->pDestDb);
000428          }
000429          if( destMode==PAGER_JOURNALMODE_WAL ){
000430            rc = sqlite3BtreeSetVersion(p->pDest, 2);
000431          }
000432        }
000433        if( rc==SQLITE_OK ){
000434          int nDestTruncate;
000435          /* Set nDestTruncate to the final number of pages in the destination
000436          ** database. The complication here is that the destination page
000437          ** size may be different to the source page size. 
000438          **
000439          ** If the source page size is smaller than the destination page size, 
000440          ** round up. In this case the call to sqlite3OsTruncate() below will
000441          ** fix the size of the file. However it is important to call
000442          ** sqlite3PagerTruncateImage() here so that any pages in the 
000443          ** destination file that lie beyond the nDestTruncate page mark are
000444          ** journalled by PagerCommitPhaseOne() before they are destroyed
000445          ** by the file truncation.
000446          */
000447          assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
000448          assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
000449          if( pgszSrc<pgszDest ){
000450            int ratio = pgszDest/pgszSrc;
000451            nDestTruncate = (nSrcPage+ratio-1)/ratio;
000452            if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
000453              nDestTruncate--;
000454            }
000455          }else{
000456            nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
000457          }
000458          assert( nDestTruncate>0 );
000459  
000460          if( pgszSrc<pgszDest ){
000461            /* If the source page-size is smaller than the destination page-size,
000462            ** two extra things may need to happen:
000463            **
000464            **   * The destination may need to be truncated, and
000465            **
000466            **   * Data stored on the pages immediately following the 
000467            **     pending-byte page in the source database may need to be
000468            **     copied into the destination database.
000469            */
000470            const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
000471            sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
000472            Pgno iPg;
000473            int nDstPage;
000474            i64 iOff;
000475            i64 iEnd;
000476  
000477            assert( pFile );
000478            assert( nDestTruncate==0 
000479                || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
000480                  nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
000481               && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
000482            ));
000483  
000484            /* This block ensures that all data required to recreate the original
000485            ** database has been stored in the journal for pDestPager and the
000486            ** journal synced to disk. So at this point we may safely modify
000487            ** the database file in any way, knowing that if a power failure
000488            ** occurs, the original database will be reconstructed from the 
000489            ** journal file.  */
000490            sqlite3PagerPagecount(pDestPager, &nDstPage);
000491            for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
000492              if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
000493                DbPage *pPg;
000494                rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
000495                if( rc==SQLITE_OK ){
000496                  rc = sqlite3PagerWrite(pPg);
000497                  sqlite3PagerUnref(pPg);
000498                }
000499              }
000500            }
000501            if( rc==SQLITE_OK ){
000502              rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
000503            }
000504  
000505            /* Write the extra pages and truncate the database file as required */
000506            iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
000507            for(
000508              iOff=PENDING_BYTE+pgszSrc; 
000509              rc==SQLITE_OK && iOff<iEnd; 
000510              iOff+=pgszSrc
000511            ){
000512              PgHdr *pSrcPg = 0;
000513              const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
000514              rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
000515              if( rc==SQLITE_OK ){
000516                u8 *zData = sqlite3PagerGetData(pSrcPg);
000517                rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
000518              }
000519              sqlite3PagerUnref(pSrcPg);
000520            }
000521            if( rc==SQLITE_OK ){
000522              rc = backupTruncateFile(pFile, iSize);
000523            }
000524  
000525            /* Sync the database file to disk. */
000526            if( rc==SQLITE_OK ){
000527              rc = sqlite3PagerSync(pDestPager, 0);
000528            }
000529          }else{
000530            sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
000531            rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
000532          }
000533      
000534          /* Finish committing the transaction to the destination database. */
000535          if( SQLITE_OK==rc
000536           && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
000537          ){
000538            rc = SQLITE_DONE;
000539          }
000540        }
000541      }
000542    
000543      /* If bCloseTrans is true, then this function opened a read transaction
000544      ** on the source database. Close the read transaction here. There is
000545      ** no need to check the return values of the btree methods here, as
000546      ** "committing" a read-only transaction cannot fail.
000547      */
000548      if( bCloseTrans ){
000549        TESTONLY( int rc2 );
000550        TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
000551        TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
000552        assert( rc2==SQLITE_OK );
000553      }
000554    
000555      if( rc==SQLITE_IOERR_NOMEM ){
000556        rc = SQLITE_NOMEM_BKPT;
000557      }
000558      p->rc = rc;
000559    }
000560    if( p->pDestDb ){
000561      sqlite3_mutex_leave(p->pDestDb->mutex);
000562    }
000563    sqlite3BtreeLeave(p->pSrc);
000564    sqlite3_mutex_leave(p->pSrcDb->mutex);
000565    return rc;
000566  }
000567  
000568  /*
000569  ** Release all resources associated with an sqlite3_backup* handle.
000570  */
000571  int sqlite3_backup_finish(sqlite3_backup *p){
000572    sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
000573    sqlite3 *pSrcDb;                     /* Source database connection */
000574    int rc;                              /* Value to return */
000575  
000576    /* Enter the mutexes */
000577    if( p==0 ) return SQLITE_OK;
000578    pSrcDb = p->pSrcDb;
000579    sqlite3_mutex_enter(pSrcDb->mutex);
000580    sqlite3BtreeEnter(p->pSrc);
000581    if( p->pDestDb ){
000582      sqlite3_mutex_enter(p->pDestDb->mutex);
000583    }
000584  
000585    /* Detach this backup from the source pager. */
000586    if( p->pDestDb ){
000587      p->pSrc->nBackup--;
000588    }
000589    if( p->isAttached ){
000590      pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
000591      assert( pp!=0 );
000592      while( *pp!=p ){
000593        pp = &(*pp)->pNext;
000594        assert( pp!=0 );
000595      }
000596      *pp = p->pNext;
000597    }
000598  
000599    /* If a transaction is still open on the Btree, roll it back. */
000600    sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
000601  
000602    /* Set the error code of the destination database handle. */
000603    rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
000604    if( p->pDestDb ){
000605      sqlite3Error(p->pDestDb, rc);
000606  
000607      /* Exit the mutexes and free the backup context structure. */
000608      sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
000609    }
000610    sqlite3BtreeLeave(p->pSrc);
000611    if( p->pDestDb ){
000612      /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
000613      ** call to sqlite3_backup_init() and is destroyed by a call to
000614      ** sqlite3_backup_finish(). */
000615      sqlite3_free(p);
000616    }
000617    sqlite3LeaveMutexAndCloseZombie(pSrcDb);
000618    return rc;
000619  }
000620  
000621  /*
000622  ** Return the number of pages still to be backed up as of the most recent
000623  ** call to sqlite3_backup_step().
000624  */
000625  int sqlite3_backup_remaining(sqlite3_backup *p){
000626  #ifdef SQLITE_ENABLE_API_ARMOR
000627    if( p==0 ){
000628      (void)SQLITE_MISUSE_BKPT;
000629      return 0;
000630    }
000631  #endif
000632    return p->nRemaining;
000633  }
000634  
000635  /*
000636  ** Return the total number of pages in the source database as of the most 
000637  ** recent call to sqlite3_backup_step().
000638  */
000639  int sqlite3_backup_pagecount(sqlite3_backup *p){
000640  #ifdef SQLITE_ENABLE_API_ARMOR
000641    if( p==0 ){
000642      (void)SQLITE_MISUSE_BKPT;
000643      return 0;
000644    }
000645  #endif
000646    return p->nPagecount;
000647  }
000648  
000649  /*
000650  ** This function is called after the contents of page iPage of the
000651  ** source database have been modified. If page iPage has already been 
000652  ** copied into the destination database, then the data written to the
000653  ** destination is now invalidated. The destination copy of iPage needs
000654  ** to be updated with the new data before the backup operation is
000655  ** complete.
000656  **
000657  ** It is assumed that the mutex associated with the BtShared object
000658  ** corresponding to the source database is held when this function is
000659  ** called.
000660  */
000661  static SQLITE_NOINLINE void backupUpdate(
000662    sqlite3_backup *p,
000663    Pgno iPage,
000664    const u8 *aData
000665  ){
000666    assert( p!=0 );
000667    do{
000668      assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
000669      if( !isFatalError(p->rc) && iPage<p->iNext ){
000670        /* The backup process p has already copied page iPage. But now it
000671        ** has been modified by a transaction on the source pager. Copy
000672        ** the new data into the backup.
000673        */
000674        int rc;
000675        assert( p->pDestDb );
000676        sqlite3_mutex_enter(p->pDestDb->mutex);
000677        rc = backupOnePage(p, iPage, aData, 1);
000678        sqlite3_mutex_leave(p->pDestDb->mutex);
000679        assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
000680        if( rc!=SQLITE_OK ){
000681          p->rc = rc;
000682        }
000683      }
000684    }while( (p = p->pNext)!=0 );
000685  }
000686  void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
000687    if( pBackup ) backupUpdate(pBackup, iPage, aData);
000688  }
000689  
000690  /*
000691  ** Restart the backup process. This is called when the pager layer
000692  ** detects that the database has been modified by an external database
000693  ** connection. In this case there is no way of knowing which of the
000694  ** pages that have been copied into the destination database are still 
000695  ** valid and which are not, so the entire process needs to be restarted.
000696  **
000697  ** It is assumed that the mutex associated with the BtShared object
000698  ** corresponding to the source database is held when this function is
000699  ** called.
000700  */
000701  void sqlite3BackupRestart(sqlite3_backup *pBackup){
000702    sqlite3_backup *p;                   /* Iterator variable */
000703    for(p=pBackup; p; p=p->pNext){
000704      assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
000705      p->iNext = 1;
000706    }
000707  }
000708  
000709  #ifndef SQLITE_OMIT_VACUUM
000710  /*
000711  ** Copy the complete content of pBtFrom into pBtTo.  A transaction
000712  ** must be active for both files.
000713  **
000714  ** The size of file pTo may be reduced by this operation. If anything 
000715  ** goes wrong, the transaction on pTo is rolled back. If successful, the 
000716  ** transaction is committed before returning.
000717  */
000718  int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
000719    int rc;
000720    sqlite3_file *pFd;              /* File descriptor for database pTo */
000721    sqlite3_backup b;
000722    sqlite3BtreeEnter(pTo);
000723    sqlite3BtreeEnter(pFrom);
000724  
000725    assert( sqlite3BtreeTxnState(pTo)==SQLITE_TXN_WRITE );
000726    pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
000727    if( pFd->pMethods ){
000728      i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
000729      rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
000730      if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
000731      if( rc ) goto copy_finished;
000732    }
000733  
000734    /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
000735    ** to 0. This is used by the implementations of sqlite3_backup_step()
000736    ** and sqlite3_backup_finish() to detect that they are being called
000737    ** from this function, not directly by the user.
000738    */
000739    memset(&b, 0, sizeof(b));
000740    b.pSrcDb = pFrom->db;
000741    b.pSrc = pFrom;
000742    b.pDest = pTo;
000743    b.iNext = 1;
000744  
000745    /* 0x7FFFFFFF is the hard limit for the number of pages in a database
000746    ** file. By passing this as the number of pages to copy to
000747    ** sqlite3_backup_step(), we can guarantee that the copy finishes 
000748    ** within a single call (unless an error occurs). The assert() statement
000749    ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
000750    ** or an error code.  */
000751    sqlite3_backup_step(&b, 0x7FFFFFFF);
000752    assert( b.rc!=SQLITE_OK );
000753  
000754    rc = sqlite3_backup_finish(&b);
000755    if( rc==SQLITE_OK ){
000756      pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
000757    }else{
000758      sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
000759    }
000760  
000761    assert( sqlite3BtreeTxnState(pTo)!=SQLITE_TXN_WRITE );
000762  copy_finished:
000763    sqlite3BtreeLeave(pFrom);
000764    sqlite3BtreeLeave(pTo);
000765    return rc;
000766  }
000767  #endif /* SQLITE_OMIT_VACUUM */