blob: eb8f15cc7e0a26fa80e49d682bf8984c85deca81 [file] [log] [blame]
danielk197704103022009-02-03 16:51:24 +00001/*
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.
danielk197704103022009-02-03 16:51:24 +000014*/
15#include "sqliteInt.h"
16#include "btreeInt.h"
17
danielk197704103022009-02-03 16:51:24 +000018/*
19** Structure allocated for each backup operation.
20*/
21struct 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
danielk1977e70f4f62009-05-13 07:52:06 +000039 int isAttached; /* True once backup has been registered with pager */
danielk197704103022009-02-03 16:51:24 +000040 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*/
82static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
83 int i = sqlite3FindDbName(pDb, zDb);
84
85 if( i==1 ){
drhe98c9042009-06-02 21:31:38 +000086 Parse *pParse;
87 int rc = 0;
88 pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
89 if( pParse==0 ){
drh13f40da2014-08-22 18:00:11 +000090 sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +000091 rc = SQLITE_NOMEM_BKPT;
drhe98c9042009-06-02 21:31:38 +000092 }else{
93 pParse->db = pDb;
94 if( sqlite3OpenTempDatabase(pParse) ){
drh13f40da2014-08-22 18:00:11 +000095 sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
drhe98c9042009-06-02 21:31:38 +000096 rc = SQLITE_ERROR;
97 }
drha7564662010-02-22 19:32:31 +000098 sqlite3DbFree(pErrorDb, pParse->zErrMsg);
drhf30a9692013-11-15 01:10:18 +000099 sqlite3ParserReset(pParse);
drhe98c9042009-06-02 21:31:38 +0000100 sqlite3StackFree(pErrorDb, pParse);
101 }
102 if( rc ){
danielk197704103022009-02-03 16:51:24 +0000103 return 0;
104 }
danielk197704103022009-02-03 16:51:24 +0000105 }
106
107 if( i<0 ){
drh13f40da2014-08-22 18:00:11 +0000108 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
danielk197704103022009-02-03 16:51:24 +0000109 return 0;
110 }
111
112 return pDb->aDb[i].pBt;
113}
114
115/*
drhca94d8b2011-01-11 17:38:03 +0000116** Attempt to set the page size of the destination to match the page size
117** of the source.
118*/
119static int setDestPgsz(sqlite3_backup *p){
120 int rc;
121 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
122 return rc;
123}
124
125/*
danfad01992014-11-13 14:18:25 +0000126** Check that there is no open read-transaction on the b-tree passed as the
127** second argument. If there is not, return SQLITE_OK. Otherwise, if there
128** is an open read-transaction, return SQLITE_ERROR and leave an error
129** message in database handle db.
130*/
131static int checkReadTransaction(sqlite3 *db, Btree *p){
132 if( sqlite3BtreeIsInReadTrans(p) ){
133 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
134 return SQLITE_ERROR;
135 }
136 return SQLITE_OK;
137}
138
139/*
danielk197704103022009-02-03 16:51:24 +0000140** Create an sqlite3_backup process to copy the contents of zSrcDb from
141** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
142** a pointer to the new sqlite3_backup object.
143**
144** If an error occurs, NULL is returned and an error code and error message
145** stored in database handle pDestDb.
146*/
147sqlite3_backup *sqlite3_backup_init(
148 sqlite3* pDestDb, /* Database to write to */
149 const char *zDestDb, /* Name of database within pDestDb */
150 sqlite3* pSrcDb, /* Database connection to read from */
151 const char *zSrcDb /* Name of database within pSrcDb */
152){
153 sqlite3_backup *p; /* Value to return */
154
drh9ca95732014-10-24 00:35:58 +0000155#ifdef SQLITE_ENABLE_API_ARMOR
156 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
157 (void)SQLITE_MISUSE_BKPT;
158 return 0;
159 }
160#endif
161
danielk197704103022009-02-03 16:51:24 +0000162 /* Lock the source database handle. The destination database
drh662c58c2009-02-03 21:13:07 +0000163 ** handle is not locked in this routine, but it is locked in
164 ** sqlite3_backup_step(). The user is required to ensure that no
danielk197704103022009-02-03 16:51:24 +0000165 ** other thread accesses the destination handle for the duration
drh662c58c2009-02-03 21:13:07 +0000166 ** of the backup operation. Any attempt to use the destination
167 ** database connection while a backup is in progress may cause
168 ** a malfunction or a deadlock.
danielk197704103022009-02-03 16:51:24 +0000169 */
170 sqlite3_mutex_enter(pSrcDb->mutex);
drheef1eb02009-02-04 16:56:19 +0000171 sqlite3_mutex_enter(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000172
173 if( pSrcDb==pDestDb ){
drh13f40da2014-08-22 18:00:11 +0000174 sqlite3ErrorWithMsg(
drhb309bec2009-02-04 17:40:57 +0000175 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
danielk197704103022009-02-03 16:51:24 +0000176 );
177 p = 0;
178 }else {
drh9f129f42010-08-31 15:27:32 +0000179 /* Allocate space for a new sqlite3_backup object...
180 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
181 ** call to sqlite3_backup_init() and is destroyed by a call to
182 ** sqlite3_backup_finish(). */
dan6809c962012-07-30 14:53:54 +0000183 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
danielk197704103022009-02-03 16:51:24 +0000184 if( !p ){
mistachkinfad30392016-02-13 23:43:46 +0000185 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
danielk197704103022009-02-03 16:51:24 +0000186 }
187 }
188
189 /* If the allocation succeeded, populate the new object. */
190 if( p ){
danielk197704103022009-02-03 16:51:24 +0000191 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
192 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
193 p->pDestDb = pDestDb;
194 p->pSrcDb = pSrcDb;
195 p->iNext = 1;
danielk1977e70f4f62009-05-13 07:52:06 +0000196 p->isAttached = 0;
danielk197704103022009-02-03 16:51:24 +0000197
danfad01992014-11-13 14:18:25 +0000198 if( 0==p->pSrc || 0==p->pDest
danfad01992014-11-13 14:18:25 +0000199 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
200 ){
drhca94d8b2011-01-11 17:38:03 +0000201 /* One (or both) of the named databases did not exist or an OOM
danfad01992014-11-13 14:18:25 +0000202 ** error was hit. Or there is a transaction open on the destination
203 ** database. The error has already been written into the pDestDb
204 ** handle. All that is left to do here is free the sqlite3_backup
205 ** structure. */
danielk197704103022009-02-03 16:51:24 +0000206 sqlite3_free(p);
207 p = 0;
208 }
209 }
danielk197704103022009-02-03 16:51:24 +0000210 if( p ){
danielk197704103022009-02-03 16:51:24 +0000211 p->pSrc->nBackup++;
212 }
213
drheef1eb02009-02-04 16:56:19 +0000214 sqlite3_mutex_leave(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000215 sqlite3_mutex_leave(pSrcDb->mutex);
216 return p;
217}
218
219/*
danielk197703ab0352009-02-06 05:59:44 +0000220** Argument rc is an SQLite error code. Return true if this error is
221** considered fatal if encountered during a backup operation. All errors
222** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
223*/
224static int isFatalError(int rc){
drhdcd7db52009-05-14 19:26:51 +0000225 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
danielk197703ab0352009-02-06 05:59:44 +0000226}
227
228/*
danielk197704103022009-02-03 16:51:24 +0000229** Parameter zSrcData points to a buffer containing the data for
230** page iSrcPg from the source database. Copy this data into the
231** destination database.
232*/
dan5cc3bea2012-12-21 16:15:35 +0000233static int backupOnePage(
234 sqlite3_backup *p, /* Backup handle */
235 Pgno iSrcPg, /* Source database page to backup */
236 const u8 *zSrcData, /* Source database page data */
237 int bUpdate /* True for an update, false otherwise */
238){
danielk197704103022009-02-03 16:51:24 +0000239 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
240 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
241 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
242 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
243 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
drh2b89fbc2011-04-09 02:09:44 +0000244#ifdef SQLITE_HAS_CODEC
dan0094f372012-09-28 20:23:42 +0000245 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
246 ** guaranteed that the shared-mutex is held by this thread, handle
247 ** p->pSrc may not actually be the owner. */
248 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
drhad0961b2015-02-21 00:19:25 +0000249 int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest);
drh2b89fbc2011-04-09 02:09:44 +0000250#endif
danielk197704103022009-02-03 16:51:24 +0000251 int rc = SQLITE_OK;
252 i64 iOff;
253
dan0094f372012-09-28 20:23:42 +0000254 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
danielk197704103022009-02-03 16:51:24 +0000255 assert( p->bDestLocked );
danielk197703ab0352009-02-06 05:59:44 +0000256 assert( !isFatalError(p->rc) );
danielk197704103022009-02-03 16:51:24 +0000257 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
258 assert( zSrcData );
259
260 /* Catch the case where the destination is an in-memory database and the
261 ** page sizes of the source and destination differ.
262 */
drh3289c5e2010-05-05 16:23:26 +0000263 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
danielk197704103022009-02-03 16:51:24 +0000264 rc = SQLITE_READONLY;
265 }
266
drh871919b2010-08-20 15:32:21 +0000267#ifdef SQLITE_HAS_CODEC
268 /* Backup is not possible if the page size of the destination is changing
drh2b89fbc2011-04-09 02:09:44 +0000269 ** and a codec is in use.
drh871919b2010-08-20 15:32:21 +0000270 */
271 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
272 rc = SQLITE_READONLY;
273 }
drh2b89fbc2011-04-09 02:09:44 +0000274
275 /* Backup is not possible if the number of bytes of reserve space differ
276 ** between source and destination. If there is a difference, try to
277 ** fix the destination to agree with the source. If that is not possible,
278 ** then the backup cannot proceed.
279 */
280 if( nSrcReserve!=nDestReserve ){
281 u32 newPgsz = nSrcPgsz;
282 rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
283 if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
284 }
drh871919b2010-08-20 15:32:21 +0000285#endif
286
danielk197704103022009-02-03 16:51:24 +0000287 /* This loop runs once for each destination page spanned by the source
288 ** page. For each iteration, variable iOff is set to the byte offset
289 ** of the destination page.
290 */
291 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
292 DbPage *pDestPg = 0;
293 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
294 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
drh9584f582015-11-04 20:22:37 +0000295 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
danielk197704103022009-02-03 16:51:24 +0000296 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
297 ){
298 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
299 u8 *zDestData = sqlite3PagerGetData(pDestPg);
300 u8 *zOut = &zDestData[iOff%nDestPgsz];
301
302 /* Copy the data from the source page into the destination page.
303 ** Then clear the Btree layer MemPage.isInit flag. Both this module
304 ** and the pager code use this trick (clearing the first byte
305 ** of the page 'extra' space to invalidate the Btree layers
306 ** cached parse of the page). MemPage.isInit is marked
307 ** "MUST BE FIRST" for this purpose.
308 */
309 memcpy(zOut, zIn, nCopy);
310 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
dan5cc3bea2012-12-21 16:15:35 +0000311 if( iOff==0 && bUpdate==0 ){
312 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
313 }
danielk197704103022009-02-03 16:51:24 +0000314 }
315 sqlite3PagerUnref(pDestPg);
316 }
317
318 return rc;
319}
320
321/*
danielk19773d0cbc32009-02-09 18:55:45 +0000322** If pFile is currently larger than iSize bytes, then truncate it to
323** exactly iSize bytes. If pFile is not larger than iSize bytes, then
324** this function is a no-op.
325**
326** Return SQLITE_OK if everything is successful, or an SQLite error
327** code if an error occurs.
328*/
329static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
330 i64 iCurrent;
331 int rc = sqlite3OsFileSize(pFile, &iCurrent);
332 if( rc==SQLITE_OK && iCurrent>iSize ){
333 rc = sqlite3OsTruncate(pFile, iSize);
334 }
335 return rc;
336}
337
338/*
danielk1977e70f4f62009-05-13 07:52:06 +0000339** Register this backup object with the associated source pager for
340** callbacks when pages are changed or the cache invalidated.
341*/
342static void attachBackupObject(sqlite3_backup *p){
343 sqlite3_backup **pp;
344 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
345 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
346 p->pNext = *pp;
347 *pp = p;
348 p->isAttached = 1;
349}
350
351/*
danielk197704103022009-02-03 16:51:24 +0000352** Copy nPage pages from the source b-tree to the destination.
353*/
354int sqlite3_backup_step(sqlite3_backup *p, int nPage){
355 int rc;
drh3289c5e2010-05-05 16:23:26 +0000356 int destMode; /* Destination journal mode */
drh5c10f772010-05-05 18:46:44 +0000357 int pgszSrc = 0; /* Source page size */
358 int pgszDest = 0; /* Destination page size */
danielk197704103022009-02-03 16:51:24 +0000359
drh9ca95732014-10-24 00:35:58 +0000360#ifdef SQLITE_ENABLE_API_ARMOR
361 if( p==0 ) return SQLITE_MISUSE_BKPT;
362#endif
danielk197704103022009-02-03 16:51:24 +0000363 sqlite3_mutex_enter(p->pSrcDb->mutex);
364 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000365 if( p->pDestDb ){
366 sqlite3_mutex_enter(p->pDestDb->mutex);
367 }
danielk197704103022009-02-03 16:51:24 +0000368
drh5c10f772010-05-05 18:46:44 +0000369 rc = p->rc;
danielk197703ab0352009-02-06 05:59:44 +0000370 if( !isFatalError(rc) ){
danielk197704103022009-02-03 16:51:24 +0000371 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
372 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
373 int ii; /* Iterator variable */
shane63207ab2009-02-04 01:49:30 +0000374 int nSrcPage = -1; /* Size of source db in pages */
danielk197704103022009-02-03 16:51:24 +0000375 int bCloseTrans = 0; /* True if src db requires unlocking */
376
377 /* If the source pager is currently in a write-transaction, return
danielk1977404ca072009-03-16 13:19:36 +0000378 ** SQLITE_BUSY immediately.
danielk197704103022009-02-03 16:51:24 +0000379 */
380 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
danielk1977404ca072009-03-16 13:19:36 +0000381 rc = SQLITE_BUSY;
danielk197703ab0352009-02-06 05:59:44 +0000382 }else{
383 rc = SQLITE_OK;
danielk197704103022009-02-03 16:51:24 +0000384 }
385
danielk197704103022009-02-03 16:51:24 +0000386 /* If there is no open read-transaction on the source database, open
387 ** one now. If a transaction is opened here, then it will be closed
388 ** before this function exits.
389 */
390 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
391 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
392 bCloseTrans = 1;
393 }
drh5c10f772010-05-05 18:46:44 +0000394
dan033564c2016-09-02 17:18:20 +0000395 /* If the destination database has not yet been locked (i.e. if this
396 ** is the first call to backup_step() for the current backup operation),
397 ** try to set its page size to the same as the source database. This
398 ** is especially important on ZipVFS systems, as in that case it is
399 ** not possible to create a database file that uses one page size by
400 ** writing to it with another. */
drh76729ed2016-09-02 21:17:51 +0000401 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
402 rc = SQLITE_NOMEM;
403 }
dan033564c2016-09-02 17:18:20 +0000404
405 /* Lock the destination database, if it is not locked already. */
406 if( SQLITE_OK==rc && p->bDestLocked==0
407 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
408 ){
409 p->bDestLocked = 1;
410 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
411 }
412
drh5c10f772010-05-05 18:46:44 +0000413 /* Do not allow backup if the destination database is in WAL mode
414 ** and the page sizes are different between source and destination */
415 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
416 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
drh0b9b4302010-06-11 17:01:24 +0000417 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
drh5c10f772010-05-05 18:46:44 +0000418 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
419 rc = SQLITE_READONLY;
420 }
danielk197704103022009-02-03 16:51:24 +0000421
422 /* Now that there is a read-lock on the source database, query the
423 ** source pager for the number of pages in the database.
424 */
drhb1299152010-03-30 22:58:33 +0000425 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
426 assert( nSrcPage>=0 );
danielk197703ab0352009-02-06 05:59:44 +0000427 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
danielk197704103022009-02-03 16:51:24 +0000428 const Pgno iSrcPg = p->iNext; /* Source page number */
429 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
430 DbPage *pSrcPg; /* Source page object */
drh9584f582015-11-04 20:22:37 +0000431 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
danielk197704103022009-02-03 16:51:24 +0000432 if( rc==SQLITE_OK ){
dan5cc3bea2012-12-21 16:15:35 +0000433 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
danielk197704103022009-02-03 16:51:24 +0000434 sqlite3PagerUnref(pSrcPg);
435 }
436 }
437 p->iNext++;
438 }
439 if( rc==SQLITE_OK ){
440 p->nPagecount = nSrcPage;
441 p->nRemaining = nSrcPage+1-p->iNext;
shane63207ab2009-02-04 01:49:30 +0000442 if( p->iNext>(Pgno)nSrcPage ){
danielk197704103022009-02-03 16:51:24 +0000443 rc = SQLITE_DONE;
danielk1977e70f4f62009-05-13 07:52:06 +0000444 }else if( !p->isAttached ){
445 attachBackupObject(p);
danielk197704103022009-02-03 16:51:24 +0000446 }
447 }
448
drhf25cd712009-07-06 19:03:12 +0000449 /* Update the schema version field in the destination database. This
450 ** is to make sure that the schema-version really does change in
451 ** the case where the source and destination databases have the
452 ** same schema version.
453 */
drhc5dbffe2011-08-25 20:18:47 +0000454 if( rc==SQLITE_DONE ){
danb483eba2012-10-13 19:58:11 +0000455 if( nSrcPage==0 ){
456 rc = sqlite3BtreeNewDb(p->pDest);
457 nSrcPage = 1;
458 }
459 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
460 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
461 }
drhc5dbffe2011-08-25 20:18:47 +0000462 if( rc==SQLITE_OK ){
463 if( p->pDestDb ){
drh81028a42012-05-15 18:28:27 +0000464 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
drhc5dbffe2011-08-25 20:18:47 +0000465 }
466 if( destMode==PAGER_JOURNALMODE_WAL ){
467 rc = sqlite3BtreeSetVersion(p->pDest, 2);
468 }
drhd3a5c502009-02-03 22:51:06 +0000469 }
dan4b270402011-08-25 19:28:47 +0000470 if( rc==SQLITE_OK ){
471 int nDestTruncate;
472 /* Set nDestTruncate to the final number of pages in the destination
473 ** database. The complication here is that the destination page
474 ** size may be different to the source page size.
danielk197704103022009-02-03 16:51:24 +0000475 **
dan4b270402011-08-25 19:28:47 +0000476 ** If the source page size is smaller than the destination page size,
477 ** round up. In this case the call to sqlite3OsTruncate() below will
478 ** fix the size of the file. However it is important to call
479 ** sqlite3PagerTruncateImage() here so that any pages in the
480 ** destination file that lie beyond the nDestTruncate page mark are
481 ** journalled by PagerCommitPhaseOne() before they are destroyed
482 ** by the file truncation.
danielk197704103022009-02-03 16:51:24 +0000483 */
dan4b270402011-08-25 19:28:47 +0000484 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
485 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
486 if( pgszSrc<pgszDest ){
487 int ratio = pgszDest/pgszSrc;
488 nDestTruncate = (nSrcPage+ratio-1)/ratio;
489 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
490 nDestTruncate--;
drhc6aed542011-01-16 22:37:09 +0000491 }
dan4b270402011-08-25 19:28:47 +0000492 }else{
493 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
drhc6aed542011-01-16 22:37:09 +0000494 }
danb483eba2012-10-13 19:58:11 +0000495 assert( nDestTruncate>0 );
dan4d26d582011-01-25 18:19:24 +0000496
dan4b270402011-08-25 19:28:47 +0000497 if( pgszSrc<pgszDest ){
498 /* If the source page-size is smaller than the destination page-size,
499 ** two extra things may need to happen:
500 **
501 ** * The destination may need to be truncated, and
502 **
503 ** * Data stored on the pages immediately following the
504 ** pending-byte page in the source database may need to be
505 ** copied into the destination database.
506 */
507 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
508 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
danbc1a3c62013-02-23 16:40:46 +0000509 Pgno iPg;
510 int nDstPage;
dan4b270402011-08-25 19:28:47 +0000511 i64 iOff;
512 i64 iEnd;
513
514 assert( pFile );
danb483eba2012-10-13 19:58:11 +0000515 assert( nDestTruncate==0
516 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
dan4b270402011-08-25 19:28:47 +0000517 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
518 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
519 ));
520
danbc1a3c62013-02-23 16:40:46 +0000521 /* This block ensures that all data required to recreate the original
dan4b270402011-08-25 19:28:47 +0000522 ** database has been stored in the journal for pDestPager and the
523 ** journal synced to disk. So at this point we may safely modify
524 ** the database file in any way, knowing that if a power failure
525 ** occurs, the original database will be reconstructed from the
526 ** journal file. */
danbc1a3c62013-02-23 16:40:46 +0000527 sqlite3PagerPagecount(pDestPager, &nDstPage);
528 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
529 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
530 DbPage *pPg;
drh9584f582015-11-04 20:22:37 +0000531 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
danbc1a3c62013-02-23 16:40:46 +0000532 if( rc==SQLITE_OK ){
533 rc = sqlite3PagerWrite(pPg);
534 sqlite3PagerUnref(pPg);
535 }
536 }
537 }
dan896f99e2013-02-25 07:12:40 +0000538 if( rc==SQLITE_OK ){
539 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
540 }
dan4b270402011-08-25 19:28:47 +0000541
542 /* Write the extra pages and truncate the database file as required */
543 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
544 for(
545 iOff=PENDING_BYTE+pgszSrc;
546 rc==SQLITE_OK && iOff<iEnd;
547 iOff+=pgszSrc
548 ){
549 PgHdr *pSrcPg = 0;
550 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
drh9584f582015-11-04 20:22:37 +0000551 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
dan4b270402011-08-25 19:28:47 +0000552 if( rc==SQLITE_OK ){
553 u8 *zData = sqlite3PagerGetData(pSrcPg);
danf23da962013-03-23 21:00:41 +0000554 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
dan4b270402011-08-25 19:28:47 +0000555 }
556 sqlite3PagerUnref(pSrcPg);
557 }
558 if( rc==SQLITE_OK ){
559 rc = backupTruncateFile(pFile, iSize);
560 }
561
562 /* Sync the database file to disk. */
563 if( rc==SQLITE_OK ){
dan999cd082013-12-09 20:42:03 +0000564 rc = sqlite3PagerSync(pDestPager, 0);
dan4b270402011-08-25 19:28:47 +0000565 }
566 }else{
danbc1a3c62013-02-23 16:40:46 +0000567 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
dan4b270402011-08-25 19:28:47 +0000568 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
danielk197704103022009-02-03 16:51:24 +0000569 }
dan4b270402011-08-25 19:28:47 +0000570
571 /* Finish committing the transaction to the destination database. */
572 if( SQLITE_OK==rc
573 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
574 ){
575 rc = SQLITE_DONE;
576 }
danielk197704103022009-02-03 16:51:24 +0000577 }
578 }
579
580 /* If bCloseTrans is true, then this function opened a read transaction
581 ** on the source database. Close the read transaction here. There is
582 ** no need to check the return values of the btree methods here, as
583 ** "committing" a read-only transaction cannot fail.
584 */
585 if( bCloseTrans ){
586 TESTONLY( int rc2 );
587 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
dan60939d02011-03-29 15:40:55 +0000588 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
danielk197704103022009-02-03 16:51:24 +0000589 assert( rc2==SQLITE_OK );
590 }
591
danba3cbf32010-06-30 04:29:03 +0000592 if( rc==SQLITE_IOERR_NOMEM ){
mistachkinfad30392016-02-13 23:43:46 +0000593 rc = SQLITE_NOMEM_BKPT;
danba3cbf32010-06-30 04:29:03 +0000594 }
danielk197703ab0352009-02-06 05:59:44 +0000595 p->rc = rc;
danielk197704103022009-02-03 16:51:24 +0000596 }
drhd3a5c502009-02-03 22:51:06 +0000597 if( p->pDestDb ){
598 sqlite3_mutex_leave(p->pDestDb->mutex);
599 }
danielk197704103022009-02-03 16:51:24 +0000600 sqlite3BtreeLeave(p->pSrc);
601 sqlite3_mutex_leave(p->pSrcDb->mutex);
602 return rc;
603}
604
605/*
606** Release all resources associated with an sqlite3_backup* handle.
607*/
608int sqlite3_backup_finish(sqlite3_backup *p){
609 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
drhed688012012-06-21 15:51:42 +0000610 sqlite3 *pSrcDb; /* Source database connection */
danielk197704103022009-02-03 16:51:24 +0000611 int rc; /* Value to return */
612
613 /* Enter the mutexes */
drhdcd7db52009-05-14 19:26:51 +0000614 if( p==0 ) return SQLITE_OK;
drhed688012012-06-21 15:51:42 +0000615 pSrcDb = p->pSrcDb;
616 sqlite3_mutex_enter(pSrcDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000617 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000618 if( p->pDestDb ){
619 sqlite3_mutex_enter(p->pDestDb->mutex);
620 }
danielk197704103022009-02-03 16:51:24 +0000621
622 /* Detach this backup from the source pager. */
623 if( p->pDestDb ){
danielk1977e70f4f62009-05-13 07:52:06 +0000624 p->pSrc->nBackup--;
625 }
626 if( p->isAttached ){
danielk197704103022009-02-03 16:51:24 +0000627 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
628 while( *pp!=p ){
629 pp = &(*pp)->pNext;
630 }
631 *pp = p->pNext;
danielk197704103022009-02-03 16:51:24 +0000632 }
633
634 /* If a transaction is still open on the Btree, roll it back. */
drh47b7fc72014-11-11 01:33:57 +0000635 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
danielk197704103022009-02-03 16:51:24 +0000636
637 /* Set the error code of the destination database handle. */
638 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
drhd3a5c502009-02-03 22:51:06 +0000639 if( p->pDestDb ){
drh13f40da2014-08-22 18:00:11 +0000640 sqlite3Error(p->pDestDb, rc);
drha3cc0072013-12-13 16:23:55 +0000641
642 /* Exit the mutexes and free the backup context structure. */
drh4245c402012-06-02 14:32:21 +0000643 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
drhd3a5c502009-02-03 22:51:06 +0000644 }
danielk197704103022009-02-03 16:51:24 +0000645 sqlite3BtreeLeave(p->pSrc);
646 if( p->pDestDb ){
drh9f129f42010-08-31 15:27:32 +0000647 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
648 ** call to sqlite3_backup_init() and is destroyed by a call to
649 ** sqlite3_backup_finish(). */
danielk197704103022009-02-03 16:51:24 +0000650 sqlite3_free(p);
651 }
drh4245c402012-06-02 14:32:21 +0000652 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
danielk197704103022009-02-03 16:51:24 +0000653 return rc;
654}
655
656/*
657** Return the number of pages still to be backed up as of the most recent
658** call to sqlite3_backup_step().
659*/
660int sqlite3_backup_remaining(sqlite3_backup *p){
drh9ca95732014-10-24 00:35:58 +0000661#ifdef SQLITE_ENABLE_API_ARMOR
662 if( p==0 ){
663 (void)SQLITE_MISUSE_BKPT;
664 return 0;
665 }
666#endif
danielk197704103022009-02-03 16:51:24 +0000667 return p->nRemaining;
668}
669
670/*
671** Return the total number of pages in the source database as of the most
672** recent call to sqlite3_backup_step().
673*/
674int sqlite3_backup_pagecount(sqlite3_backup *p){
drh9ca95732014-10-24 00:35:58 +0000675#ifdef SQLITE_ENABLE_API_ARMOR
676 if( p==0 ){
677 (void)SQLITE_MISUSE_BKPT;
678 return 0;
679 }
680#endif
danielk197704103022009-02-03 16:51:24 +0000681 return p->nPagecount;
682}
683
684/*
685** This function is called after the contents of page iPage of the
686** source database have been modified. If page iPage has already been
687** copied into the destination database, then the data written to the
688** destination is now invalidated. The destination copy of iPage needs
689** to be updated with the new data before the backup operation is
690** complete.
691**
692** It is assumed that the mutex associated with the BtShared object
693** corresponding to the source database is held when this function is
694** called.
695*/
drh14681b72015-07-01 19:59:36 +0000696static SQLITE_NOINLINE void backupUpdate(
697 sqlite3_backup *p,
698 Pgno iPage,
699 const u8 *aData
700){
701 assert( p!=0 );
702 do{
danielk197704103022009-02-03 16:51:24 +0000703 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
danielk197703ab0352009-02-06 05:59:44 +0000704 if( !isFatalError(p->rc) && iPage<p->iNext ){
danielk197704103022009-02-03 16:51:24 +0000705 /* The backup process p has already copied page iPage. But now it
706 ** has been modified by a transaction on the source pager. Copy
707 ** the new data into the backup.
708 */
drh2b89fbc2011-04-09 02:09:44 +0000709 int rc;
drh806ebcb2011-04-09 17:53:30 +0000710 assert( p->pDestDb );
711 sqlite3_mutex_enter(p->pDestDb->mutex);
dan5cc3bea2012-12-21 16:15:35 +0000712 rc = backupOnePage(p, iPage, aData, 1);
drh806ebcb2011-04-09 17:53:30 +0000713 sqlite3_mutex_leave(p->pDestDb->mutex);
danielk197703ab0352009-02-06 05:59:44 +0000714 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
danielk197704103022009-02-03 16:51:24 +0000715 if( rc!=SQLITE_OK ){
716 p->rc = rc;
717 }
718 }
drh14681b72015-07-01 19:59:36 +0000719 }while( (p = p->pNext)!=0 );
720}
721void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
722 if( pBackup ) backupUpdate(pBackup, iPage, aData);
danielk197704103022009-02-03 16:51:24 +0000723}
724
725/*
726** Restart the backup process. This is called when the pager layer
727** detects that the database has been modified by an external database
728** connection. In this case there is no way of knowing which of the
729** pages that have been copied into the destination database are still
730** valid and which are not, so the entire process needs to be restarted.
731**
732** It is assumed that the mutex associated with the BtShared object
733** corresponding to the source database is held when this function is
734** called.
735*/
736void sqlite3BackupRestart(sqlite3_backup *pBackup){
737 sqlite3_backup *p; /* Iterator variable */
738 for(p=pBackup; p; p=p->pNext){
739 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
740 p->iNext = 1;
741 }
742}
743
744#ifndef SQLITE_OMIT_VACUUM
745/*
746** Copy the complete content of pBtFrom into pBtTo. A transaction
747** must be active for both files.
748**
749** The size of file pTo may be reduced by this operation. If anything
750** goes wrong, the transaction on pTo is rolled back. If successful, the
751** transaction is committed before returning.
752*/
753int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
754 int rc;
danc5f20a02011-10-07 16:57:59 +0000755 sqlite3_file *pFd; /* File descriptor for database pTo */
danielk197704103022009-02-03 16:51:24 +0000756 sqlite3_backup b;
757 sqlite3BtreeEnter(pTo);
758 sqlite3BtreeEnter(pFrom);
759
danc5f20a02011-10-07 16:57:59 +0000760 assert( sqlite3BtreeIsInTrans(pTo) );
761 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
762 if( pFd->pMethods ){
763 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
drh2bfe5b32012-01-10 16:40:50 +0000764 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
765 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
766 if( rc ) goto copy_finished;
danc5f20a02011-10-07 16:57:59 +0000767 }
768
danielk197704103022009-02-03 16:51:24 +0000769 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
770 ** to 0. This is used by the implementations of sqlite3_backup_step()
771 ** and sqlite3_backup_finish() to detect that they are being called
772 ** from this function, not directly by the user.
773 */
774 memset(&b, 0, sizeof(b));
775 b.pSrcDb = pFrom->db;
776 b.pSrc = pFrom;
777 b.pDest = pTo;
778 b.iNext = 1;
779
drh58cb6db2015-09-23 19:17:23 +0000780#ifdef SQLITE_HAS_CODEC
781 sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom));
782#endif
783
danielk197704103022009-02-03 16:51:24 +0000784 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
785 ** file. By passing this as the number of pages to copy to
786 ** sqlite3_backup_step(), we can guarantee that the copy finishes
787 ** within a single call (unless an error occurs). The assert() statement
788 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
dan43c1ce32016-08-05 16:16:26 +0000789 ** or an error code. */
danielk197704103022009-02-03 16:51:24 +0000790 sqlite3_backup_step(&b, 0x7FFFFFFF);
791 assert( b.rc!=SQLITE_OK );
dan43c1ce32016-08-05 16:16:26 +0000792
danielk197704103022009-02-03 16:51:24 +0000793 rc = sqlite3_backup_finish(&b);
794 if( rc==SQLITE_OK ){
drhc9166342012-01-05 23:32:06 +0000795 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
dan43c1ce32016-08-05 16:16:26 +0000796 }else{
797 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
danielk197704103022009-02-03 16:51:24 +0000798 }
799
danc5f20a02011-10-07 16:57:59 +0000800 assert( sqlite3BtreeIsInTrans(pTo)==0 );
drh2bfe5b32012-01-10 16:40:50 +0000801copy_finished:
danielk197704103022009-02-03 16:51:24 +0000802 sqlite3BtreeLeave(pFrom);
803 sqlite3BtreeLeave(pTo);
804 return rc;
805}
806#endif /* SQLITE_OMIT_VACUUM */