xref: /sqlite-3.40.0/src/backup.c (revision d5578433)
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   int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
223   int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
224 #endif
225 
226   int rc = SQLITE_OK;
227   i64 iOff;
228 
229   assert( p->bDestLocked );
230   assert( !isFatalError(p->rc) );
231   assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
232   assert( zSrcData );
233 
234   /* Catch the case where the destination is an in-memory database and the
235   ** page sizes of the source and destination differ.
236   */
237   if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
238     rc = SQLITE_READONLY;
239   }
240 
241 #ifdef SQLITE_HAS_CODEC
242   /* Backup is not possible if the page size of the destination is changing
243   ** and a codec is in use.
244   */
245   if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
246     rc = SQLITE_READONLY;
247   }
248 
249   /* Backup is not possible if the number of bytes of reserve space differ
250   ** between source and destination.  If there is a difference, try to
251   ** fix the destination to agree with the source.  If that is not possible,
252   ** then the backup cannot proceed.
253   */
254   if( nSrcReserve!=nDestReserve ){
255     u32 newPgsz = nSrcPgsz;
256     rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
257     if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
258   }
259 #endif
260 
261   /* This loop runs once for each destination page spanned by the source
262   ** page. For each iteration, variable iOff is set to the byte offset
263   ** of the destination page.
264   */
265   for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
266     DbPage *pDestPg = 0;
267     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
268     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
269     if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
270      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
271     ){
272       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
273       u8 *zDestData = sqlite3PagerGetData(pDestPg);
274       u8 *zOut = &zDestData[iOff%nDestPgsz];
275 
276       /* Copy the data from the source page into the destination page.
277       ** Then clear the Btree layer MemPage.isInit flag. Both this module
278       ** and the pager code use this trick (clearing the first byte
279       ** of the page 'extra' space to invalidate the Btree layers
280       ** cached parse of the page). MemPage.isInit is marked
281       ** "MUST BE FIRST" for this purpose.
282       */
283       memcpy(zOut, zIn, nCopy);
284       ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
285     }
286     sqlite3PagerUnref(pDestPg);
287   }
288 
289   return rc;
290 }
291 
292 /*
293 ** If pFile is currently larger than iSize bytes, then truncate it to
294 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
295 ** this function is a no-op.
296 **
297 ** Return SQLITE_OK if everything is successful, or an SQLite error
298 ** code if an error occurs.
299 */
300 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
301   i64 iCurrent;
302   int rc = sqlite3OsFileSize(pFile, &iCurrent);
303   if( rc==SQLITE_OK && iCurrent>iSize ){
304     rc = sqlite3OsTruncate(pFile, iSize);
305   }
306   return rc;
307 }
308 
309 /*
310 ** Register this backup object with the associated source pager for
311 ** callbacks when pages are changed or the cache invalidated.
312 */
313 static void attachBackupObject(sqlite3_backup *p){
314   sqlite3_backup **pp;
315   assert( sqlite3BtreeHoldsMutex(p->pSrc) );
316   pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
317   p->pNext = *pp;
318   *pp = p;
319   p->isAttached = 1;
320 }
321 
322 /*
323 ** Copy nPage pages from the source b-tree to the destination.
324 */
325 int sqlite3_backup_step(sqlite3_backup *p, int nPage){
326   int rc;
327   int destMode;       /* Destination journal mode */
328   int pgszSrc = 0;    /* Source page size */
329   int pgszDest = 0;   /* Destination page size */
330 
331   sqlite3_mutex_enter(p->pSrcDb->mutex);
332   sqlite3BtreeEnter(p->pSrc);
333   if( p->pDestDb ){
334     sqlite3_mutex_enter(p->pDestDb->mutex);
335   }
336 
337   rc = p->rc;
338   if( !isFatalError(rc) ){
339     Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
340     Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
341     int ii;                            /* Iterator variable */
342     int nSrcPage = -1;                 /* Size of source db in pages */
343     int bCloseTrans = 0;               /* True if src db requires unlocking */
344 
345     /* If the source pager is currently in a write-transaction, return
346     ** SQLITE_BUSY immediately.
347     */
348     if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
349       rc = SQLITE_BUSY;
350     }else{
351       rc = SQLITE_OK;
352     }
353 
354     /* Lock the destination database, if it is not locked already. */
355     if( SQLITE_OK==rc && p->bDestLocked==0
356      && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
357     ){
358       p->bDestLocked = 1;
359       sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
360     }
361 
362     /* If there is no open read-transaction on the source database, open
363     ** one now. If a transaction is opened here, then it will be closed
364     ** before this function exits.
365     */
366     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
367       rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
368       bCloseTrans = 1;
369     }
370 
371     /* Do not allow backup if the destination database is in WAL mode
372     ** and the page sizes are different between source and destination */
373     pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
374     pgszDest = sqlite3BtreeGetPageSize(p->pDest);
375     destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
376     if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
377       rc = SQLITE_READONLY;
378     }
379 
380     /* Now that there is a read-lock on the source database, query the
381     ** source pager for the number of pages in the database.
382     */
383     nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
384     assert( nSrcPage>=0 );
385     for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
386       const Pgno iSrcPg = p->iNext;                 /* Source page number */
387       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
388         DbPage *pSrcPg;                             /* Source page object */
389         rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
390         if( rc==SQLITE_OK ){
391           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
392           sqlite3PagerUnref(pSrcPg);
393         }
394       }
395       p->iNext++;
396     }
397     if( rc==SQLITE_OK ){
398       p->nPagecount = nSrcPage;
399       p->nRemaining = nSrcPage+1-p->iNext;
400       if( p->iNext>(Pgno)nSrcPage ){
401         rc = SQLITE_DONE;
402       }else if( !p->isAttached ){
403         attachBackupObject(p);
404       }
405     }
406 
407     /* Update the schema version field in the destination database. This
408     ** is to make sure that the schema-version really does change in
409     ** the case where the source and destination databases have the
410     ** same schema version.
411     */
412     if( rc==SQLITE_DONE ){
413       rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
414       if( rc==SQLITE_OK ){
415         if( p->pDestDb ){
416           sqlite3ResetAllSchemasOfConnection(p->pDestDb);
417         }
418         if( destMode==PAGER_JOURNALMODE_WAL ){
419           rc = sqlite3BtreeSetVersion(p->pDest, 2);
420         }
421       }
422       if( rc==SQLITE_OK ){
423         int nDestTruncate;
424         /* Set nDestTruncate to the final number of pages in the destination
425         ** database. The complication here is that the destination page
426         ** size may be different to the source page size.
427         **
428         ** If the source page size is smaller than the destination page size,
429         ** round up. In this case the call to sqlite3OsTruncate() below will
430         ** fix the size of the file. However it is important to call
431         ** sqlite3PagerTruncateImage() here so that any pages in the
432         ** destination file that lie beyond the nDestTruncate page mark are
433         ** journalled by PagerCommitPhaseOne() before they are destroyed
434         ** by the file truncation.
435         */
436         assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
437         assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
438         if( pgszSrc<pgszDest ){
439           int ratio = pgszDest/pgszSrc;
440           nDestTruncate = (nSrcPage+ratio-1)/ratio;
441           if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
442             nDestTruncate--;
443           }
444         }else{
445           nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
446         }
447         sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
448 
449         if( pgszSrc<pgszDest ){
450           /* If the source page-size is smaller than the destination page-size,
451           ** two extra things may need to happen:
452           **
453           **   * The destination may need to be truncated, and
454           **
455           **   * Data stored on the pages immediately following the
456           **     pending-byte page in the source database may need to be
457           **     copied into the destination database.
458           */
459           const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
460           sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
461           i64 iOff;
462           i64 iEnd;
463 
464           assert( pFile );
465           assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
466                 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
467              && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
468           ));
469 
470           /* This call ensures that all data required to recreate the original
471           ** database has been stored in the journal for pDestPager and the
472           ** journal synced to disk. So at this point we may safely modify
473           ** the database file in any way, knowing that if a power failure
474           ** occurs, the original database will be reconstructed from the
475           ** journal file.  */
476           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
477 
478           /* Write the extra pages and truncate the database file as required */
479           iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
480           for(
481             iOff=PENDING_BYTE+pgszSrc;
482             rc==SQLITE_OK && iOff<iEnd;
483             iOff+=pgszSrc
484           ){
485             PgHdr *pSrcPg = 0;
486             const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
487             rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
488             if( rc==SQLITE_OK ){
489               u8 *zData = sqlite3PagerGetData(pSrcPg);
490               rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
491             }
492             sqlite3PagerUnref(pSrcPg);
493           }
494           if( rc==SQLITE_OK ){
495             rc = backupTruncateFile(pFile, iSize);
496           }
497 
498           /* Sync the database file to disk. */
499           if( rc==SQLITE_OK ){
500             rc = sqlite3PagerSync(pDestPager);
501           }
502         }else{
503           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
504         }
505 
506         /* Finish committing the transaction to the destination database. */
507         if( SQLITE_OK==rc
508          && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
509         ){
510           rc = SQLITE_DONE;
511         }
512       }
513     }
514 
515     /* If bCloseTrans is true, then this function opened a read transaction
516     ** on the source database. Close the read transaction here. There is
517     ** no need to check the return values of the btree methods here, as
518     ** "committing" a read-only transaction cannot fail.
519     */
520     if( bCloseTrans ){
521       TESTONLY( int rc2 );
522       TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
523       TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
524       assert( rc2==SQLITE_OK );
525     }
526 
527     if( rc==SQLITE_IOERR_NOMEM ){
528       rc = SQLITE_NOMEM;
529     }
530     p->rc = rc;
531   }
532   if( p->pDestDb ){
533     sqlite3_mutex_leave(p->pDestDb->mutex);
534   }
535   sqlite3BtreeLeave(p->pSrc);
536   sqlite3_mutex_leave(p->pSrcDb->mutex);
537   return rc;
538 }
539 
540 /*
541 ** Release all resources associated with an sqlite3_backup* handle.
542 */
543 int sqlite3_backup_finish(sqlite3_backup *p){
544   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
545   sqlite3 *pSrcDb;                     /* Source database connection */
546   int rc;                              /* Value to return */
547 
548   /* Enter the mutexes */
549   if( p==0 ) return SQLITE_OK;
550   pSrcDb = p->pSrcDb;
551   sqlite3_mutex_enter(pSrcDb->mutex);
552   sqlite3BtreeEnter(p->pSrc);
553   if( p->pDestDb ){
554     sqlite3_mutex_enter(p->pDestDb->mutex);
555   }
556 
557   /* Detach this backup from the source pager. */
558   if( p->pDestDb ){
559     p->pSrc->nBackup--;
560   }
561   if( p->isAttached ){
562     pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
563     while( *pp!=p ){
564       pp = &(*pp)->pNext;
565     }
566     *pp = p->pNext;
567   }
568 
569   /* If a transaction is still open on the Btree, roll it back. */
570   sqlite3BtreeRollback(p->pDest, SQLITE_OK);
571 
572   /* Set the error code of the destination database handle. */
573   rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
574   sqlite3Error(p->pDestDb, rc, 0);
575 
576   /* Exit the mutexes and free the backup context structure. */
577   if( p->pDestDb ){
578     sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
579   }
580   sqlite3BtreeLeave(p->pSrc);
581   if( p->pDestDb ){
582     /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
583     ** call to sqlite3_backup_init() and is destroyed by a call to
584     ** sqlite3_backup_finish(). */
585     sqlite3_free(p);
586   }
587   sqlite3LeaveMutexAndCloseZombie(pSrcDb);
588   return rc;
589 }
590 
591 /*
592 ** Return the number of pages still to be backed up as of the most recent
593 ** call to sqlite3_backup_step().
594 */
595 int sqlite3_backup_remaining(sqlite3_backup *p){
596   return p->nRemaining;
597 }
598 
599 /*
600 ** Return the total number of pages in the source database as of the most
601 ** recent call to sqlite3_backup_step().
602 */
603 int sqlite3_backup_pagecount(sqlite3_backup *p){
604   return p->nPagecount;
605 }
606 
607 /*
608 ** This function is called after the contents of page iPage of the
609 ** source database have been modified. If page iPage has already been
610 ** copied into the destination database, then the data written to the
611 ** destination is now invalidated. The destination copy of iPage needs
612 ** to be updated with the new data before the backup operation is
613 ** complete.
614 **
615 ** It is assumed that the mutex associated with the BtShared object
616 ** corresponding to the source database is held when this function is
617 ** called.
618 */
619 void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
620   sqlite3_backup *p;                   /* Iterator variable */
621   for(p=pBackup; p; p=p->pNext){
622     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
623     if( !isFatalError(p->rc) && iPage<p->iNext ){
624       /* The backup process p has already copied page iPage. But now it
625       ** has been modified by a transaction on the source pager. Copy
626       ** the new data into the backup.
627       */
628       int rc;
629       assert( p->pDestDb );
630       sqlite3_mutex_enter(p->pDestDb->mutex);
631       rc = backupOnePage(p, iPage, aData);
632       sqlite3_mutex_leave(p->pDestDb->mutex);
633       assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
634       if( rc!=SQLITE_OK ){
635         p->rc = rc;
636       }
637     }
638   }
639 }
640 
641 /*
642 ** Restart the backup process. This is called when the pager layer
643 ** detects that the database has been modified by an external database
644 ** connection. In this case there is no way of knowing which of the
645 ** pages that have been copied into the destination database are still
646 ** valid and which are not, so the entire process needs to be restarted.
647 **
648 ** It is assumed that the mutex associated with the BtShared object
649 ** corresponding to the source database is held when this function is
650 ** called.
651 */
652 void sqlite3BackupRestart(sqlite3_backup *pBackup){
653   sqlite3_backup *p;                   /* Iterator variable */
654   for(p=pBackup; p; p=p->pNext){
655     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
656     p->iNext = 1;
657   }
658 }
659 
660 #ifndef SQLITE_OMIT_VACUUM
661 /*
662 ** Copy the complete content of pBtFrom into pBtTo.  A transaction
663 ** must be active for both files.
664 **
665 ** The size of file pTo may be reduced by this operation. If anything
666 ** goes wrong, the transaction on pTo is rolled back. If successful, the
667 ** transaction is committed before returning.
668 */
669 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
670   int rc;
671   sqlite3_file *pFd;              /* File descriptor for database pTo */
672   sqlite3_backup b;
673   sqlite3BtreeEnter(pTo);
674   sqlite3BtreeEnter(pFrom);
675 
676   assert( sqlite3BtreeIsInTrans(pTo) );
677   pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
678   if( pFd->pMethods ){
679     i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
680     rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
681     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
682     if( rc ) goto copy_finished;
683   }
684 
685   /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
686   ** to 0. This is used by the implementations of sqlite3_backup_step()
687   ** and sqlite3_backup_finish() to detect that they are being called
688   ** from this function, not directly by the user.
689   */
690   memset(&b, 0, sizeof(b));
691   b.pSrcDb = pFrom->db;
692   b.pSrc = pFrom;
693   b.pDest = pTo;
694   b.iNext = 1;
695 
696   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
697   ** file. By passing this as the number of pages to copy to
698   ** sqlite3_backup_step(), we can guarantee that the copy finishes
699   ** within a single call (unless an error occurs). The assert() statement
700   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
701   ** or an error code.
702   */
703   sqlite3_backup_step(&b, 0x7FFFFFFF);
704   assert( b.rc!=SQLITE_OK );
705   rc = sqlite3_backup_finish(&b);
706   if( rc==SQLITE_OK ){
707     pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
708   }else{
709     sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
710   }
711 
712   assert( sqlite3BtreeIsInTrans(pTo)==0 );
713 copy_finished:
714   sqlite3BtreeLeave(pFrom);
715   sqlite3BtreeLeave(pTo);
716   return rc;
717 }
718 #endif /* SQLITE_OMIT_VACUUM */
719