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