xref: /sqlite-3.40.0/src/backup.c (revision 8749c183)
1 /*
2 ** 2009 January 28
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains the implementation of the sqlite3_backup_XXX()
13 ** API functions and the related features.
14 */
15 #include "sqliteInt.h"
16 #include "btreeInt.h"
17 
18 /* Macro to find the minimum of two numeric values.
19 */
20 #ifndef MIN
21 # define MIN(x,y) ((x)<(y)?(x):(y))
22 #endif
23 
24 /*
25 ** Structure allocated for each backup operation.
26 */
27 struct sqlite3_backup {
28   sqlite3* pDestDb;        /* Destination database handle */
29   Btree *pDest;            /* Destination b-tree file */
30   u32 iDestSchema;         /* Original schema cookie in destination */
31   int bDestLocked;         /* True once a write-transaction is open on pDest */
32 
33   Pgno iNext;              /* Page number of the next source page to copy */
34   sqlite3* pSrcDb;         /* Source database handle */
35   Btree *pSrc;             /* Source b-tree file */
36 
37   int rc;                  /* Backup process error code */
38 
39   /* These two variables are set by every call to backup_step(). They are
40   ** read by calls to backup_remaining() and backup_pagecount().
41   */
42   Pgno nRemaining;         /* Number of pages left to copy */
43   Pgno nPagecount;         /* Total number of pages to copy */
44 
45   int isAttached;          /* True once backup has been registered with pager */
46   sqlite3_backup *pNext;   /* Next backup associated with source pager */
47 };
48 
49 /*
50 ** THREAD SAFETY NOTES:
51 **
52 **   Once it has been created using backup_init(), a single sqlite3_backup
53 **   structure may be accessed via two groups of thread-safe entry points:
54 **
55 **     * Via the sqlite3_backup_XXX() API function backup_step() and
56 **       backup_finish(). Both these functions obtain the source database
57 **       handle mutex and the mutex associated with the source BtShared
58 **       structure, in that order.
59 **
60 **     * Via the BackupUpdate() and BackupRestart() functions, which are
61 **       invoked by the pager layer to report various state changes in
62 **       the page cache associated with the source database. The mutex
63 **       associated with the source database BtShared structure will always
64 **       be held when either of these functions are invoked.
65 **
66 **   The other sqlite3_backup_XXX() API functions, backup_remaining() and
67 **   backup_pagecount() are not thread-safe functions. If they are called
68 **   while some other thread is calling backup_step() or backup_finish(),
69 **   the values returned may be invalid. There is no way for a call to
70 **   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
71 **   or backup_pagecount().
72 **
73 **   Depending on the SQLite configuration, the database handles and/or
74 **   the Btree objects may have their own mutexes that require locking.
75 **   Non-sharable Btrees (in-memory databases for example), do not have
76 **   associated mutexes.
77 */
78 
79 /*
80 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
81 ** in connection handle pDb. If such a database cannot be found, return
82 ** a NULL pointer and write an error message to pErrorDb.
83 **
84 ** If the "temp" database is requested, it may need to be opened by this
85 ** function. If an error occurs while doing so, return 0 and write an
86 ** error message to pErrorDb.
87 */
88 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
89   int i = sqlite3FindDbName(pDb, zDb);
90 
91   if( i==1 ){
92     Parse *pParse;
93     int rc = 0;
94     pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
95     if( pParse==0 ){
96       sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
97       rc = SQLITE_NOMEM;
98     }else{
99       pParse->db = pDb;
100       if( sqlite3OpenTempDatabase(pParse) ){
101         sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
102         rc = SQLITE_ERROR;
103       }
104       sqlite3DbFree(pErrorDb, pParse->zErrMsg);
105       sqlite3StackFree(pErrorDb, pParse);
106     }
107     if( rc ){
108       return 0;
109     }
110   }
111 
112   if( i<0 ){
113     sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
114     return 0;
115   }
116 
117   return pDb->aDb[i].pBt;
118 }
119 
120 /*
121 ** Attempt to set the page size of the destination to match the page size
122 ** of the source.
123 */
124 static int setDestPgsz(sqlite3_backup *p){
125   int rc;
126   rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
127   return rc;
128 }
129 
130 /*
131 ** Create an sqlite3_backup process to copy the contents of zSrcDb from
132 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
133 ** a pointer to the new sqlite3_backup object.
134 **
135 ** If an error occurs, NULL is returned and an error code and error message
136 ** stored in database handle pDestDb.
137 */
138 sqlite3_backup *sqlite3_backup_init(
139   sqlite3* pDestDb,                     /* Database to write to */
140   const char *zDestDb,                  /* Name of database within pDestDb */
141   sqlite3* pSrcDb,                      /* Database connection to read from */
142   const char *zSrcDb                    /* Name of database within pSrcDb */
143 ){
144   sqlite3_backup *p;                    /* Value to return */
145 
146   /* Lock the source database handle. The destination database
147   ** handle is not locked in this routine, but it is locked in
148   ** sqlite3_backup_step(). The user is required to ensure that no
149   ** other thread accesses the destination handle for the duration
150   ** of the backup operation.  Any attempt to use the destination
151   ** database connection while a backup is in progress may cause
152   ** a malfunction or a deadlock.
153   */
154   sqlite3_mutex_enter(pSrcDb->mutex);
155   sqlite3_mutex_enter(pDestDb->mutex);
156 
157   if( pSrcDb==pDestDb ){
158     sqlite3Error(
159         pDestDb, SQLITE_ERROR, "source and destination must be distinct"
160     );
161     p = 0;
162   }else {
163     /* Allocate space for a new sqlite3_backup object...
164     ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
165     ** call to sqlite3_backup_init() and is destroyed by a call to
166     ** sqlite3_backup_finish(). */
167     p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
168     if( !p ){
169       sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
170     }
171   }
172 
173   /* If the allocation succeeded, populate the new object. */
174   if( p ){
175     p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
176     p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
177     p->pDestDb = pDestDb;
178     p->pSrcDb = pSrcDb;
179     p->iNext = 1;
180     p->isAttached = 0;
181 
182     if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
183       /* One (or both) of the named databases did not exist or an OOM
184       ** error was hit.  The error has already been written into the
185       ** pDestDb handle.  All that is left to do here is free the
186       ** sqlite3_backup structure.
187       */
188       sqlite3_free(p);
189       p = 0;
190     }
191   }
192   if( p ){
193     p->pSrc->nBackup++;
194   }
195 
196   sqlite3_mutex_leave(pDestDb->mutex);
197   sqlite3_mutex_leave(pSrcDb->mutex);
198   return p;
199 }
200 
201 /*
202 ** Argument rc is an SQLite error code. Return true if this error is
203 ** considered fatal if encountered during a backup operation. All errors
204 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
205 */
206 static int isFatalError(int rc){
207   return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
208 }
209 
210 /*
211 ** Parameter zSrcData points to a buffer containing the data for
212 ** page iSrcPg from the source database. Copy this data into the
213 ** destination database.
214 */
215 static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
216   Pager * const pDestPager = sqlite3BtreePager(p->pDest);
217   const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
218   int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
219   const int nCopy = MIN(nSrcPgsz, nDestPgsz);
220   const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
221 #ifdef SQLITE_HAS_CODEC
222   /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
223   ** guaranteed that the shared-mutex is held by this thread, handle
224   ** p->pSrc may not actually be the owner.  */
225   int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
226   int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
227 #endif
228   int rc = SQLITE_OK;
229   i64 iOff;
230 
231   assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
232   assert( p->bDestLocked );
233   assert( !isFatalError(p->rc) );
234   assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
235   assert( zSrcData );
236 
237   /* Catch the case where the destination is an in-memory database and the
238   ** page sizes of the source and destination differ.
239   */
240   if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
241     rc = SQLITE_READONLY;
242   }
243 
244 #ifdef SQLITE_HAS_CODEC
245   /* Backup is not possible if the page size of the destination is changing
246   ** and a codec is in use.
247   */
248   if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
249     rc = SQLITE_READONLY;
250   }
251 
252   /* Backup is not possible if the number of bytes of reserve space differ
253   ** between source and destination.  If there is a difference, try to
254   ** fix the destination to agree with the source.  If that is not possible,
255   ** then the backup cannot proceed.
256   */
257   if( nSrcReserve!=nDestReserve ){
258     u32 newPgsz = nSrcPgsz;
259     rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
260     if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
261   }
262 #endif
263 
264   /* This loop runs once for each destination page spanned by the source
265   ** page. For each iteration, variable iOff is set to the byte offset
266   ** of the destination page.
267   */
268   for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
269     DbPage *pDestPg = 0;
270     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
271     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
272     if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
273      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
274     ){
275       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
276       u8 *zDestData = sqlite3PagerGetData(pDestPg);
277       u8 *zOut = &zDestData[iOff%nDestPgsz];
278 
279       /* Copy the data from the source page into the destination page.
280       ** Then clear the Btree layer MemPage.isInit flag. Both this module
281       ** and the pager code use this trick (clearing the first byte
282       ** of the page 'extra' space to invalidate the Btree layers
283       ** cached parse of the page). MemPage.isInit is marked
284       ** "MUST BE FIRST" for this purpose.
285       */
286       memcpy(zOut, zIn, nCopy);
287       ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
288     }
289     sqlite3PagerUnref(pDestPg);
290   }
291 
292   return rc;
293 }
294 
295 /*
296 ** If pFile is currently larger than iSize bytes, then truncate it to
297 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
298 ** this function is a no-op.
299 **
300 ** Return SQLITE_OK if everything is successful, or an SQLite error
301 ** code if an error occurs.
302 */
303 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
304   i64 iCurrent;
305   int rc = sqlite3OsFileSize(pFile, &iCurrent);
306   if( rc==SQLITE_OK && iCurrent>iSize ){
307     rc = sqlite3OsTruncate(pFile, iSize);
308   }
309   return rc;
310 }
311 
312 /*
313 ** Register this backup object with the associated source pager for
314 ** callbacks when pages are changed or the cache invalidated.
315 */
316 static void attachBackupObject(sqlite3_backup *p){
317   sqlite3_backup **pp;
318   assert( sqlite3BtreeHoldsMutex(p->pSrc) );
319   pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
320   p->pNext = *pp;
321   *pp = p;
322   p->isAttached = 1;
323 }
324 
325 /*
326 ** Copy nPage pages from the source b-tree to the destination.
327 */
328 int sqlite3_backup_step(sqlite3_backup *p, int nPage){
329   int rc;
330   int destMode;       /* Destination journal mode */
331   int pgszSrc = 0;    /* Source page size */
332   int pgszDest = 0;   /* Destination page size */
333 
334   sqlite3_mutex_enter(p->pSrcDb->mutex);
335   sqlite3BtreeEnter(p->pSrc);
336   if( p->pDestDb ){
337     sqlite3_mutex_enter(p->pDestDb->mutex);
338   }
339 
340   rc = p->rc;
341   if( !isFatalError(rc) ){
342     Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
343     Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
344     int ii;                            /* Iterator variable */
345     int nSrcPage = -1;                 /* Size of source db in pages */
346     int bCloseTrans = 0;               /* True if src db requires unlocking */
347 
348     /* If the source pager is currently in a write-transaction, return
349     ** SQLITE_BUSY immediately.
350     */
351     if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
352       rc = SQLITE_BUSY;
353     }else{
354       rc = SQLITE_OK;
355     }
356 
357     /* Lock the destination database, if it is not locked already. */
358     if( SQLITE_OK==rc && p->bDestLocked==0
359      && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
360     ){
361       p->bDestLocked = 1;
362       sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
363     }
364 
365     /* If there is no open read-transaction on the source database, open
366     ** one now. If a transaction is opened here, then it will be closed
367     ** before this function exits.
368     */
369     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
370       rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
371       bCloseTrans = 1;
372     }
373 
374     /* Do not allow backup if the destination database is in WAL mode
375     ** and the page sizes are different between source and destination */
376     pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
377     pgszDest = sqlite3BtreeGetPageSize(p->pDest);
378     destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
379     if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
380       rc = SQLITE_READONLY;
381     }
382 
383     /* Now that there is a read-lock on the source database, query the
384     ** source pager for the number of pages in the database.
385     */
386     nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
387     assert( nSrcPage>=0 );
388     for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
389       const Pgno iSrcPg = p->iNext;                 /* Source page number */
390       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
391         DbPage *pSrcPg;                             /* Source page object */
392         rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
393         if( rc==SQLITE_OK ){
394           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
395           sqlite3PagerUnref(pSrcPg);
396         }
397       }
398       p->iNext++;
399     }
400     if( rc==SQLITE_OK ){
401       p->nPagecount = nSrcPage;
402       p->nRemaining = nSrcPage+1-p->iNext;
403       if( p->iNext>(Pgno)nSrcPage ){
404         rc = SQLITE_DONE;
405       }else if( !p->isAttached ){
406         attachBackupObject(p);
407       }
408     }
409 
410     /* Update the schema version field in the destination database. This
411     ** is to make sure that the schema-version really does change in
412     ** the case where the source and destination databases have the
413     ** same schema version.
414     */
415     if( rc==SQLITE_DONE ){
416       if( nSrcPage==0 ){
417         rc = sqlite3BtreeNewDb(p->pDest);
418         nSrcPage = 1;
419       }
420       if( rc==SQLITE_OK || rc==SQLITE_DONE ){
421         rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
422       }
423       if( rc==SQLITE_OK ){
424         if( p->pDestDb ){
425           sqlite3ResetAllSchemasOfConnection(p->pDestDb);
426         }
427         if( destMode==PAGER_JOURNALMODE_WAL ){
428           rc = sqlite3BtreeSetVersion(p->pDest, 2);
429         }
430       }
431       if( rc==SQLITE_OK ){
432         int nDestTruncate;
433         /* Set nDestTruncate to the final number of pages in the destination
434         ** database. The complication here is that the destination page
435         ** size may be different to the source page size.
436         **
437         ** If the source page size is smaller than the destination page size,
438         ** round up. In this case the call to sqlite3OsTruncate() below will
439         ** fix the size of the file. However it is important to call
440         ** sqlite3PagerTruncateImage() here so that any pages in the
441         ** destination file that lie beyond the nDestTruncate page mark are
442         ** journalled by PagerCommitPhaseOne() before they are destroyed
443         ** by the file truncation.
444         */
445         assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
446         assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
447         if( pgszSrc<pgszDest ){
448           int ratio = pgszDest/pgszSrc;
449           nDestTruncate = (nSrcPage+ratio-1)/ratio;
450           if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
451             nDestTruncate--;
452           }
453         }else{
454           nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
455         }
456         assert( nDestTruncate>0 );
457         sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
458 
459         if( pgszSrc<pgszDest ){
460           /* If the source page-size is smaller than the destination page-size,
461           ** two extra things may need to happen:
462           **
463           **   * The destination may need to be truncated, and
464           **
465           **   * Data stored on the pages immediately following the
466           **     pending-byte page in the source database may need to be
467           **     copied into the destination database.
468           */
469           const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
470           sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
471           i64 iOff;
472           i64 iEnd;
473 
474           assert( pFile );
475           assert( nDestTruncate==0
476               || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
477                 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
478              && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
479           ));
480 
481           /* This call ensures that all data required to recreate the original
482           ** database has been stored in the journal for pDestPager and the
483           ** journal synced to disk. So at this point we may safely modify
484           ** the database file in any way, knowing that if a power failure
485           ** occurs, the original database will be reconstructed from the
486           ** journal file.  */
487           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
488 
489           /* Write the extra pages and truncate the database file as required */
490           iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
491           for(
492             iOff=PENDING_BYTE+pgszSrc;
493             rc==SQLITE_OK && iOff<iEnd;
494             iOff+=pgszSrc
495           ){
496             PgHdr *pSrcPg = 0;
497             const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
498             rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
499             if( rc==SQLITE_OK ){
500               u8 *zData = sqlite3PagerGetData(pSrcPg);
501               rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
502             }
503             sqlite3PagerUnref(pSrcPg);
504           }
505           if( rc==SQLITE_OK ){
506             rc = backupTruncateFile(pFile, iSize);
507           }
508 
509           /* Sync the database file to disk. */
510           if( rc==SQLITE_OK ){
511             rc = sqlite3PagerSync(pDestPager);
512           }
513         }else{
514           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
515         }
516 
517         /* Finish committing the transaction to the destination database. */
518         if( SQLITE_OK==rc
519          && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
520         ){
521           rc = SQLITE_DONE;
522         }
523       }
524     }
525 
526     /* If bCloseTrans is true, then this function opened a read transaction
527     ** on the source database. Close the read transaction here. There is
528     ** no need to check the return values of the btree methods here, as
529     ** "committing" a read-only transaction cannot fail.
530     */
531     if( bCloseTrans ){
532       TESTONLY( int rc2 );
533       TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
534       TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
535       assert( rc2==SQLITE_OK );
536     }
537 
538     if( rc==SQLITE_IOERR_NOMEM ){
539       rc = SQLITE_NOMEM;
540     }
541     p->rc = rc;
542   }
543   if( p->pDestDb ){
544     sqlite3_mutex_leave(p->pDestDb->mutex);
545   }
546   sqlite3BtreeLeave(p->pSrc);
547   sqlite3_mutex_leave(p->pSrcDb->mutex);
548   return rc;
549 }
550 
551 /*
552 ** Release all resources associated with an sqlite3_backup* handle.
553 */
554 int sqlite3_backup_finish(sqlite3_backup *p){
555   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
556   sqlite3 *pSrcDb;                     /* Source database connection */
557   int rc;                              /* Value to return */
558 
559   /* Enter the mutexes */
560   if( p==0 ) return SQLITE_OK;
561   pSrcDb = p->pSrcDb;
562   sqlite3_mutex_enter(pSrcDb->mutex);
563   sqlite3BtreeEnter(p->pSrc);
564   if( p->pDestDb ){
565     sqlite3_mutex_enter(p->pDestDb->mutex);
566   }
567 
568   /* Detach this backup from the source pager. */
569   if( p->pDestDb ){
570     p->pSrc->nBackup--;
571   }
572   if( p->isAttached ){
573     pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
574     while( *pp!=p ){
575       pp = &(*pp)->pNext;
576     }
577     *pp = p->pNext;
578   }
579 
580   /* If a transaction is still open on the Btree, roll it back. */
581   sqlite3BtreeRollback(p->pDest, SQLITE_OK);
582 
583   /* Set the error code of the destination database handle. */
584   rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
585   sqlite3Error(p->pDestDb, rc, 0);
586 
587   /* Exit the mutexes and free the backup context structure. */
588   if( p->pDestDb ){
589     sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
590   }
591   sqlite3BtreeLeave(p->pSrc);
592   if( p->pDestDb ){
593     /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
594     ** call to sqlite3_backup_init() and is destroyed by a call to
595     ** sqlite3_backup_finish(). */
596     sqlite3_free(p);
597   }
598   sqlite3LeaveMutexAndCloseZombie(pSrcDb);
599   return rc;
600 }
601 
602 /*
603 ** Return the number of pages still to be backed up as of the most recent
604 ** call to sqlite3_backup_step().
605 */
606 int sqlite3_backup_remaining(sqlite3_backup *p){
607   return p->nRemaining;
608 }
609 
610 /*
611 ** Return the total number of pages in the source database as of the most
612 ** recent call to sqlite3_backup_step().
613 */
614 int sqlite3_backup_pagecount(sqlite3_backup *p){
615   return p->nPagecount;
616 }
617 
618 /*
619 ** This function is called after the contents of page iPage of the
620 ** source database have been modified. If page iPage has already been
621 ** copied into the destination database, then the data written to the
622 ** destination is now invalidated. The destination copy of iPage needs
623 ** to be updated with the new data before the backup operation is
624 ** complete.
625 **
626 ** It is assumed that the mutex associated with the BtShared object
627 ** corresponding to the source database is held when this function is
628 ** called.
629 */
630 void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
631   sqlite3_backup *p;                   /* Iterator variable */
632   for(p=pBackup; p; p=p->pNext){
633     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
634     if( !isFatalError(p->rc) && iPage<p->iNext ){
635       /* The backup process p has already copied page iPage. But now it
636       ** has been modified by a transaction on the source pager. Copy
637       ** the new data into the backup.
638       */
639       int rc;
640       assert( p->pDestDb );
641       sqlite3_mutex_enter(p->pDestDb->mutex);
642       rc = backupOnePage(p, iPage, aData);
643       sqlite3_mutex_leave(p->pDestDb->mutex);
644       assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
645       if( rc!=SQLITE_OK ){
646         p->rc = rc;
647       }
648     }
649   }
650 }
651 
652 /*
653 ** Restart the backup process. This is called when the pager layer
654 ** detects that the database has been modified by an external database
655 ** connection. In this case there is no way of knowing which of the
656 ** pages that have been copied into the destination database are still
657 ** valid and which are not, so the entire process needs to be restarted.
658 **
659 ** It is assumed that the mutex associated with the BtShared object
660 ** corresponding to the source database is held when this function is
661 ** called.
662 */
663 void sqlite3BackupRestart(sqlite3_backup *pBackup){
664   sqlite3_backup *p;                   /* Iterator variable */
665   for(p=pBackup; p; p=p->pNext){
666     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
667     p->iNext = 1;
668   }
669 }
670 
671 #ifndef SQLITE_OMIT_VACUUM
672 /*
673 ** Copy the complete content of pBtFrom into pBtTo.  A transaction
674 ** must be active for both files.
675 **
676 ** The size of file pTo may be reduced by this operation. If anything
677 ** goes wrong, the transaction on pTo is rolled back. If successful, the
678 ** transaction is committed before returning.
679 */
680 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
681   int rc;
682   sqlite3_file *pFd;              /* File descriptor for database pTo */
683   sqlite3_backup b;
684   sqlite3BtreeEnter(pTo);
685   sqlite3BtreeEnter(pFrom);
686 
687   assert( sqlite3BtreeIsInTrans(pTo) );
688   pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
689   if( pFd->pMethods ){
690     i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
691     rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
692     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
693     if( rc ) goto copy_finished;
694   }
695 
696   /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
697   ** to 0. This is used by the implementations of sqlite3_backup_step()
698   ** and sqlite3_backup_finish() to detect that they are being called
699   ** from this function, not directly by the user.
700   */
701   memset(&b, 0, sizeof(b));
702   b.pSrcDb = pFrom->db;
703   b.pSrc = pFrom;
704   b.pDest = pTo;
705   b.iNext = 1;
706 
707   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
708   ** file. By passing this as the number of pages to copy to
709   ** sqlite3_backup_step(), we can guarantee that the copy finishes
710   ** within a single call (unless an error occurs). The assert() statement
711   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
712   ** or an error code.
713   */
714   sqlite3_backup_step(&b, 0x7FFFFFFF);
715   assert( b.rc!=SQLITE_OK );
716   rc = sqlite3_backup_finish(&b);
717   if( rc==SQLITE_OK ){
718     pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
719   }else{
720     sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
721   }
722 
723   assert( sqlite3BtreeIsInTrans(pTo)==0 );
724 copy_finished:
725   sqlite3BtreeLeave(pFrom);
726   sqlite3BtreeLeave(pTo);
727   return rc;
728 }
729 #endif /* SQLITE_OMIT_VACUUM */
730