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