blob: 57f7f447ead88b88960eca2b82f3be3daffce53d [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");
drhe98c9042009-06-02 21:31:38 +000091 rc = SQLITE_NOMEM;
92 }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/*
danielk197704103022009-02-03 16:51:24 +0000126** Create an sqlite3_backup process to copy the contents of zSrcDb from
127** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
128** a pointer to the new sqlite3_backup object.
129**
130** If an error occurs, NULL is returned and an error code and error message
131** stored in database handle pDestDb.
132*/
133sqlite3_backup *sqlite3_backup_init(
134 sqlite3* pDestDb, /* Database to write to */
135 const char *zDestDb, /* Name of database within pDestDb */
136 sqlite3* pSrcDb, /* Database connection to read from */
137 const char *zSrcDb /* Name of database within pSrcDb */
138){
139 sqlite3_backup *p; /* Value to return */
140
drh9ca95732014-10-24 00:35:58 +0000141#ifdef SQLITE_ENABLE_API_ARMOR
142 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
143 (void)SQLITE_MISUSE_BKPT;
144 return 0;
145 }
146#endif
147
danielk197704103022009-02-03 16:51:24 +0000148 /* Lock the source database handle. The destination database
drh662c58c2009-02-03 21:13:07 +0000149 ** handle is not locked in this routine, but it is locked in
150 ** sqlite3_backup_step(). The user is required to ensure that no
danielk197704103022009-02-03 16:51:24 +0000151 ** other thread accesses the destination handle for the duration
drh662c58c2009-02-03 21:13:07 +0000152 ** of the backup operation. Any attempt to use the destination
153 ** database connection while a backup is in progress may cause
154 ** a malfunction or a deadlock.
danielk197704103022009-02-03 16:51:24 +0000155 */
156 sqlite3_mutex_enter(pSrcDb->mutex);
drheef1eb02009-02-04 16:56:19 +0000157 sqlite3_mutex_enter(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000158
159 if( pSrcDb==pDestDb ){
drh13f40da2014-08-22 18:00:11 +0000160 sqlite3ErrorWithMsg(
drhb309bec2009-02-04 17:40:57 +0000161 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
danielk197704103022009-02-03 16:51:24 +0000162 );
163 p = 0;
164 }else {
drh9f129f42010-08-31 15:27:32 +0000165 /* Allocate space for a new sqlite3_backup object...
166 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
167 ** call to sqlite3_backup_init() and is destroyed by a call to
168 ** sqlite3_backup_finish(). */
dan6809c962012-07-30 14:53:54 +0000169 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
danielk197704103022009-02-03 16:51:24 +0000170 if( !p ){
drh13f40da2014-08-22 18:00:11 +0000171 sqlite3Error(pDestDb, SQLITE_NOMEM);
danielk197704103022009-02-03 16:51:24 +0000172 }
173 }
174
175 /* If the allocation succeeded, populate the new object. */
176 if( p ){
danielk197704103022009-02-03 16:51:24 +0000177 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
178 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
179 p->pDestDb = pDestDb;
180 p->pSrcDb = pSrcDb;
181 p->iNext = 1;
danielk1977e70f4f62009-05-13 07:52:06 +0000182 p->isAttached = 0;
danielk197704103022009-02-03 16:51:24 +0000183
drhca94d8b2011-01-11 17:38:03 +0000184 if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
185 /* One (or both) of the named databases did not exist or an OOM
186 ** error was hit. The error has already been written into the
187 ** pDestDb handle. All that is left to do here is free the
188 ** sqlite3_backup structure.
danielk197704103022009-02-03 16:51:24 +0000189 */
190 sqlite3_free(p);
191 p = 0;
192 }
193 }
danielk197704103022009-02-03 16:51:24 +0000194 if( p ){
danielk197704103022009-02-03 16:51:24 +0000195 p->pSrc->nBackup++;
196 }
197
drheef1eb02009-02-04 16:56:19 +0000198 sqlite3_mutex_leave(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000199 sqlite3_mutex_leave(pSrcDb->mutex);
200 return p;
201}
202
203/*
danielk197703ab0352009-02-06 05:59:44 +0000204** Argument rc is an SQLite error code. Return true if this error is
205** considered fatal if encountered during a backup operation. All errors
206** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
207*/
208static int isFatalError(int rc){
drhdcd7db52009-05-14 19:26:51 +0000209 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
danielk197703ab0352009-02-06 05:59:44 +0000210}
211
212/*
danielk197704103022009-02-03 16:51:24 +0000213** Parameter zSrcData points to a buffer containing the data for
214** page iSrcPg from the source database. Copy this data into the
215** destination database.
216*/
dan5cc3bea2012-12-21 16:15:35 +0000217static int backupOnePage(
218 sqlite3_backup *p, /* Backup handle */
219 Pgno iSrcPg, /* Source database page to backup */
220 const u8 *zSrcData, /* Source database page data */
221 int bUpdate /* True for an update, false otherwise */
222){
danielk197704103022009-02-03 16:51:24 +0000223 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
224 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
225 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
226 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
227 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
drh2b89fbc2011-04-09 02:09:44 +0000228#ifdef SQLITE_HAS_CODEC
dan0094f372012-09-28 20:23:42 +0000229 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
230 ** guaranteed that the shared-mutex is held by this thread, handle
231 ** p->pSrc may not actually be the owner. */
232 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
drh2b89fbc2011-04-09 02:09:44 +0000233 int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
234#endif
danielk197704103022009-02-03 16:51:24 +0000235 int rc = SQLITE_OK;
236 i64 iOff;
237
dan0094f372012-09-28 20:23:42 +0000238 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
danielk197704103022009-02-03 16:51:24 +0000239 assert( p->bDestLocked );
danielk197703ab0352009-02-06 05:59:44 +0000240 assert( !isFatalError(p->rc) );
danielk197704103022009-02-03 16:51:24 +0000241 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
242 assert( zSrcData );
243
244 /* Catch the case where the destination is an in-memory database and the
245 ** page sizes of the source and destination differ.
246 */
drh3289c5e2010-05-05 16:23:26 +0000247 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
danielk197704103022009-02-03 16:51:24 +0000248 rc = SQLITE_READONLY;
249 }
250
drh871919b2010-08-20 15:32:21 +0000251#ifdef SQLITE_HAS_CODEC
252 /* Backup is not possible if the page size of the destination is changing
drh2b89fbc2011-04-09 02:09:44 +0000253 ** and a codec is in use.
drh871919b2010-08-20 15:32:21 +0000254 */
255 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
256 rc = SQLITE_READONLY;
257 }
drh2b89fbc2011-04-09 02:09:44 +0000258
259 /* Backup is not possible if the number of bytes of reserve space differ
260 ** between source and destination. If there is a difference, try to
261 ** fix the destination to agree with the source. If that is not possible,
262 ** then the backup cannot proceed.
263 */
264 if( nSrcReserve!=nDestReserve ){
265 u32 newPgsz = nSrcPgsz;
266 rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
267 if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
268 }
drh871919b2010-08-20 15:32:21 +0000269#endif
270
danielk197704103022009-02-03 16:51:24 +0000271 /* This loop runs once for each destination page spanned by the source
272 ** page. For each iteration, variable iOff is set to the byte offset
273 ** of the destination page.
274 */
275 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
276 DbPage *pDestPg = 0;
277 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
278 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
279 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
280 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
281 ){
282 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
283 u8 *zDestData = sqlite3PagerGetData(pDestPg);
284 u8 *zOut = &zDestData[iOff%nDestPgsz];
285
286 /* Copy the data from the source page into the destination page.
287 ** Then clear the Btree layer MemPage.isInit flag. Both this module
288 ** and the pager code use this trick (clearing the first byte
289 ** of the page 'extra' space to invalidate the Btree layers
290 ** cached parse of the page). MemPage.isInit is marked
291 ** "MUST BE FIRST" for this purpose.
292 */
293 memcpy(zOut, zIn, nCopy);
294 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
dan5cc3bea2012-12-21 16:15:35 +0000295 if( iOff==0 && bUpdate==0 ){
296 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
297 }
danielk197704103022009-02-03 16:51:24 +0000298 }
299 sqlite3PagerUnref(pDestPg);
300 }
301
302 return rc;
303}
304
305/*
danielk19773d0cbc32009-02-09 18:55:45 +0000306** If pFile is currently larger than iSize bytes, then truncate it to
307** exactly iSize bytes. If pFile is not larger than iSize bytes, then
308** this function is a no-op.
309**
310** Return SQLITE_OK if everything is successful, or an SQLite error
311** code if an error occurs.
312*/
313static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
314 i64 iCurrent;
315 int rc = sqlite3OsFileSize(pFile, &iCurrent);
316 if( rc==SQLITE_OK && iCurrent>iSize ){
317 rc = sqlite3OsTruncate(pFile, iSize);
318 }
319 return rc;
320}
321
322/*
danielk1977e70f4f62009-05-13 07:52:06 +0000323** Register this backup object with the associated source pager for
324** callbacks when pages are changed or the cache invalidated.
325*/
326static void attachBackupObject(sqlite3_backup *p){
327 sqlite3_backup **pp;
328 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
329 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
330 p->pNext = *pp;
331 *pp = p;
332 p->isAttached = 1;
333}
334
335/*
danielk197704103022009-02-03 16:51:24 +0000336** Copy nPage pages from the source b-tree to the destination.
337*/
338int sqlite3_backup_step(sqlite3_backup *p, int nPage){
339 int rc;
drh3289c5e2010-05-05 16:23:26 +0000340 int destMode; /* Destination journal mode */
drh5c10f772010-05-05 18:46:44 +0000341 int pgszSrc = 0; /* Source page size */
342 int pgszDest = 0; /* Destination page size */
danielk197704103022009-02-03 16:51:24 +0000343
drh9ca95732014-10-24 00:35:58 +0000344#ifdef SQLITE_ENABLE_API_ARMOR
345 if( p==0 ) return SQLITE_MISUSE_BKPT;
346#endif
danielk197704103022009-02-03 16:51:24 +0000347 sqlite3_mutex_enter(p->pSrcDb->mutex);
348 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000349 if( p->pDestDb ){
350 sqlite3_mutex_enter(p->pDestDb->mutex);
351 }
danielk197704103022009-02-03 16:51:24 +0000352
drh5c10f772010-05-05 18:46:44 +0000353 rc = p->rc;
danielk197703ab0352009-02-06 05:59:44 +0000354 if( !isFatalError(rc) ){
danielk197704103022009-02-03 16:51:24 +0000355 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
356 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
357 int ii; /* Iterator variable */
shane63207ab2009-02-04 01:49:30 +0000358 int nSrcPage = -1; /* Size of source db in pages */
danielk197704103022009-02-03 16:51:24 +0000359 int bCloseTrans = 0; /* True if src db requires unlocking */
360
361 /* If the source pager is currently in a write-transaction, return
danielk1977404ca072009-03-16 13:19:36 +0000362 ** SQLITE_BUSY immediately.
danielk197704103022009-02-03 16:51:24 +0000363 */
364 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
danielk1977404ca072009-03-16 13:19:36 +0000365 rc = SQLITE_BUSY;
danielk197703ab0352009-02-06 05:59:44 +0000366 }else{
367 rc = SQLITE_OK;
danielk197704103022009-02-03 16:51:24 +0000368 }
369
370 /* Lock the destination database, if it is not locked already. */
371 if( SQLITE_OK==rc && p->bDestLocked==0
372 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
373 ){
374 p->bDestLocked = 1;
danielk1977602b4662009-07-02 07:47:33 +0000375 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
danielk197704103022009-02-03 16:51:24 +0000376 }
377
378 /* If there is no open read-transaction on the source database, open
379 ** one now. If a transaction is opened here, then it will be closed
380 ** before this function exits.
381 */
382 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
383 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
384 bCloseTrans = 1;
385 }
drh5c10f772010-05-05 18:46:44 +0000386
387 /* Do not allow backup if the destination database is in WAL mode
388 ** and the page sizes are different between source and destination */
389 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
390 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
drh0b9b4302010-06-11 17:01:24 +0000391 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
drh5c10f772010-05-05 18:46:44 +0000392 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
393 rc = SQLITE_READONLY;
394 }
danielk197704103022009-02-03 16:51:24 +0000395
396 /* Now that there is a read-lock on the source database, query the
397 ** source pager for the number of pages in the database.
398 */
drhb1299152010-03-30 22:58:33 +0000399 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
400 assert( nSrcPage>=0 );
danielk197703ab0352009-02-06 05:59:44 +0000401 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
danielk197704103022009-02-03 16:51:24 +0000402 const Pgno iSrcPg = p->iNext; /* Source page number */
403 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
404 DbPage *pSrcPg; /* Source page object */
drh7e369622013-04-03 03:53:15 +0000405 rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
drhb00fc3b2013-08-21 23:42:32 +0000406 PAGER_GET_READONLY);
danielk197704103022009-02-03 16:51:24 +0000407 if( rc==SQLITE_OK ){
dan5cc3bea2012-12-21 16:15:35 +0000408 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
danielk197704103022009-02-03 16:51:24 +0000409 sqlite3PagerUnref(pSrcPg);
410 }
411 }
412 p->iNext++;
413 }
414 if( rc==SQLITE_OK ){
415 p->nPagecount = nSrcPage;
416 p->nRemaining = nSrcPage+1-p->iNext;
shane63207ab2009-02-04 01:49:30 +0000417 if( p->iNext>(Pgno)nSrcPage ){
danielk197704103022009-02-03 16:51:24 +0000418 rc = SQLITE_DONE;
danielk1977e70f4f62009-05-13 07:52:06 +0000419 }else if( !p->isAttached ){
420 attachBackupObject(p);
danielk197704103022009-02-03 16:51:24 +0000421 }
422 }
423
drhf25cd712009-07-06 19:03:12 +0000424 /* Update the schema version field in the destination database. This
425 ** is to make sure that the schema-version really does change in
426 ** the case where the source and destination databases have the
427 ** same schema version.
428 */
drhc5dbffe2011-08-25 20:18:47 +0000429 if( rc==SQLITE_DONE ){
danb483eba2012-10-13 19:58:11 +0000430 if( nSrcPage==0 ){
431 rc = sqlite3BtreeNewDb(p->pDest);
432 nSrcPage = 1;
433 }
434 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
435 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
436 }
drhc5dbffe2011-08-25 20:18:47 +0000437 if( rc==SQLITE_OK ){
438 if( p->pDestDb ){
drh81028a42012-05-15 18:28:27 +0000439 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
drhc5dbffe2011-08-25 20:18:47 +0000440 }
441 if( destMode==PAGER_JOURNALMODE_WAL ){
442 rc = sqlite3BtreeSetVersion(p->pDest, 2);
443 }
drhd3a5c502009-02-03 22:51:06 +0000444 }
dan4b270402011-08-25 19:28:47 +0000445 if( rc==SQLITE_OK ){
446 int nDestTruncate;
447 /* Set nDestTruncate to the final number of pages in the destination
448 ** database. The complication here is that the destination page
449 ** size may be different to the source page size.
danielk197704103022009-02-03 16:51:24 +0000450 **
dan4b270402011-08-25 19:28:47 +0000451 ** If the source page size is smaller than the destination page size,
452 ** round up. In this case the call to sqlite3OsTruncate() below will
453 ** fix the size of the file. However it is important to call
454 ** sqlite3PagerTruncateImage() here so that any pages in the
455 ** destination file that lie beyond the nDestTruncate page mark are
456 ** journalled by PagerCommitPhaseOne() before they are destroyed
457 ** by the file truncation.
danielk197704103022009-02-03 16:51:24 +0000458 */
dan4b270402011-08-25 19:28:47 +0000459 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
460 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
461 if( pgszSrc<pgszDest ){
462 int ratio = pgszDest/pgszSrc;
463 nDestTruncate = (nSrcPage+ratio-1)/ratio;
464 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
465 nDestTruncate--;
drhc6aed542011-01-16 22:37:09 +0000466 }
dan4b270402011-08-25 19:28:47 +0000467 }else{
468 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
drhc6aed542011-01-16 22:37:09 +0000469 }
danb483eba2012-10-13 19:58:11 +0000470 assert( nDestTruncate>0 );
dan4d26d582011-01-25 18:19:24 +0000471
dan4b270402011-08-25 19:28:47 +0000472 if( pgszSrc<pgszDest ){
473 /* If the source page-size is smaller than the destination page-size,
474 ** two extra things may need to happen:
475 **
476 ** * The destination may need to be truncated, and
477 **
478 ** * Data stored on the pages immediately following the
479 ** pending-byte page in the source database may need to be
480 ** copied into the destination database.
481 */
482 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
483 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
danbc1a3c62013-02-23 16:40:46 +0000484 Pgno iPg;
485 int nDstPage;
dan4b270402011-08-25 19:28:47 +0000486 i64 iOff;
487 i64 iEnd;
488
489 assert( pFile );
danb483eba2012-10-13 19:58:11 +0000490 assert( nDestTruncate==0
491 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
dan4b270402011-08-25 19:28:47 +0000492 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
493 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
494 ));
495
danbc1a3c62013-02-23 16:40:46 +0000496 /* This block ensures that all data required to recreate the original
dan4b270402011-08-25 19:28:47 +0000497 ** database has been stored in the journal for pDestPager and the
498 ** journal synced to disk. So at this point we may safely modify
499 ** the database file in any way, knowing that if a power failure
500 ** occurs, the original database will be reconstructed from the
501 ** journal file. */
danbc1a3c62013-02-23 16:40:46 +0000502 sqlite3PagerPagecount(pDestPager, &nDstPage);
503 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
504 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
505 DbPage *pPg;
506 rc = sqlite3PagerGet(pDestPager, iPg, &pPg);
507 if( rc==SQLITE_OK ){
508 rc = sqlite3PagerWrite(pPg);
509 sqlite3PagerUnref(pPg);
510 }
511 }
512 }
dan896f99e2013-02-25 07:12:40 +0000513 if( rc==SQLITE_OK ){
514 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
515 }
dan4b270402011-08-25 19:28:47 +0000516
517 /* Write the extra pages and truncate the database file as required */
518 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
519 for(
520 iOff=PENDING_BYTE+pgszSrc;
521 rc==SQLITE_OK && iOff<iEnd;
522 iOff+=pgszSrc
523 ){
524 PgHdr *pSrcPg = 0;
525 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
526 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
527 if( rc==SQLITE_OK ){
528 u8 *zData = sqlite3PagerGetData(pSrcPg);
danf23da962013-03-23 21:00:41 +0000529 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
dan4b270402011-08-25 19:28:47 +0000530 }
531 sqlite3PagerUnref(pSrcPg);
532 }
533 if( rc==SQLITE_OK ){
534 rc = backupTruncateFile(pFile, iSize);
535 }
536
537 /* Sync the database file to disk. */
538 if( rc==SQLITE_OK ){
dan999cd082013-12-09 20:42:03 +0000539 rc = sqlite3PagerSync(pDestPager, 0);
dan4b270402011-08-25 19:28:47 +0000540 }
541 }else{
danbc1a3c62013-02-23 16:40:46 +0000542 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
dan4b270402011-08-25 19:28:47 +0000543 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
danielk197704103022009-02-03 16:51:24 +0000544 }
dan4b270402011-08-25 19:28:47 +0000545
546 /* Finish committing the transaction to the destination database. */
547 if( SQLITE_OK==rc
548 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
549 ){
550 rc = SQLITE_DONE;
551 }
danielk197704103022009-02-03 16:51:24 +0000552 }
553 }
554
555 /* If bCloseTrans is true, then this function opened a read transaction
556 ** on the source database. Close the read transaction here. There is
557 ** no need to check the return values of the btree methods here, as
558 ** "committing" a read-only transaction cannot fail.
559 */
560 if( bCloseTrans ){
561 TESTONLY( int rc2 );
562 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
dan60939d02011-03-29 15:40:55 +0000563 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
danielk197704103022009-02-03 16:51:24 +0000564 assert( rc2==SQLITE_OK );
565 }
566
danba3cbf32010-06-30 04:29:03 +0000567 if( rc==SQLITE_IOERR_NOMEM ){
568 rc = SQLITE_NOMEM;
569 }
danielk197703ab0352009-02-06 05:59:44 +0000570 p->rc = rc;
danielk197704103022009-02-03 16:51:24 +0000571 }
drhd3a5c502009-02-03 22:51:06 +0000572 if( p->pDestDb ){
573 sqlite3_mutex_leave(p->pDestDb->mutex);
574 }
danielk197704103022009-02-03 16:51:24 +0000575 sqlite3BtreeLeave(p->pSrc);
576 sqlite3_mutex_leave(p->pSrcDb->mutex);
577 return rc;
578}
579
580/*
581** Release all resources associated with an sqlite3_backup* handle.
582*/
583int sqlite3_backup_finish(sqlite3_backup *p){
584 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
drhed688012012-06-21 15:51:42 +0000585 sqlite3 *pSrcDb; /* Source database connection */
danielk197704103022009-02-03 16:51:24 +0000586 int rc; /* Value to return */
587
588 /* Enter the mutexes */
drhdcd7db52009-05-14 19:26:51 +0000589 if( p==0 ) return SQLITE_OK;
drhed688012012-06-21 15:51:42 +0000590 pSrcDb = p->pSrcDb;
591 sqlite3_mutex_enter(pSrcDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000592 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000593 if( p->pDestDb ){
594 sqlite3_mutex_enter(p->pDestDb->mutex);
595 }
danielk197704103022009-02-03 16:51:24 +0000596
597 /* Detach this backup from the source pager. */
598 if( p->pDestDb ){
danielk1977e70f4f62009-05-13 07:52:06 +0000599 p->pSrc->nBackup--;
600 }
601 if( p->isAttached ){
danielk197704103022009-02-03 16:51:24 +0000602 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
603 while( *pp!=p ){
604 pp = &(*pp)->pNext;
605 }
606 *pp = p->pNext;
danielk197704103022009-02-03 16:51:24 +0000607 }
608
609 /* If a transaction is still open on the Btree, roll it back. */
drh47b7fc72014-11-11 01:33:57 +0000610 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
danielk197704103022009-02-03 16:51:24 +0000611
612 /* Set the error code of the destination database handle. */
613 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
drhd3a5c502009-02-03 22:51:06 +0000614 if( p->pDestDb ){
drh13f40da2014-08-22 18:00:11 +0000615 sqlite3Error(p->pDestDb, rc);
drha3cc0072013-12-13 16:23:55 +0000616
617 /* Exit the mutexes and free the backup context structure. */
drh4245c402012-06-02 14:32:21 +0000618 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
drhd3a5c502009-02-03 22:51:06 +0000619 }
danielk197704103022009-02-03 16:51:24 +0000620 sqlite3BtreeLeave(p->pSrc);
621 if( p->pDestDb ){
drh9f129f42010-08-31 15:27:32 +0000622 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
623 ** call to sqlite3_backup_init() and is destroyed by a call to
624 ** sqlite3_backup_finish(). */
danielk197704103022009-02-03 16:51:24 +0000625 sqlite3_free(p);
626 }
drh4245c402012-06-02 14:32:21 +0000627 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
danielk197704103022009-02-03 16:51:24 +0000628 return rc;
629}
630
631/*
632** Return the number of pages still to be backed up as of the most recent
633** call to sqlite3_backup_step().
634*/
635int sqlite3_backup_remaining(sqlite3_backup *p){
drh9ca95732014-10-24 00:35:58 +0000636#ifdef SQLITE_ENABLE_API_ARMOR
637 if( p==0 ){
638 (void)SQLITE_MISUSE_BKPT;
639 return 0;
640 }
641#endif
danielk197704103022009-02-03 16:51:24 +0000642 return p->nRemaining;
643}
644
645/*
646** Return the total number of pages in the source database as of the most
647** recent call to sqlite3_backup_step().
648*/
649int sqlite3_backup_pagecount(sqlite3_backup *p){
drh9ca95732014-10-24 00:35:58 +0000650#ifdef SQLITE_ENABLE_API_ARMOR
651 if( p==0 ){
652 (void)SQLITE_MISUSE_BKPT;
653 return 0;
654 }
655#endif
danielk197704103022009-02-03 16:51:24 +0000656 return p->nPagecount;
657}
658
659/*
660** This function is called after the contents of page iPage of the
661** source database have been modified. If page iPage has already been
662** copied into the destination database, then the data written to the
663** destination is now invalidated. The destination copy of iPage needs
664** to be updated with the new data before the backup operation is
665** complete.
666**
667** It is assumed that the mutex associated with the BtShared object
668** corresponding to the source database is held when this function is
669** called.
670*/
671void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
672 sqlite3_backup *p; /* Iterator variable */
673 for(p=pBackup; p; p=p->pNext){
674 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
danielk197703ab0352009-02-06 05:59:44 +0000675 if( !isFatalError(p->rc) && iPage<p->iNext ){
danielk197704103022009-02-03 16:51:24 +0000676 /* The backup process p has already copied page iPage. But now it
677 ** has been modified by a transaction on the source pager. Copy
678 ** the new data into the backup.
679 */
drh2b89fbc2011-04-09 02:09:44 +0000680 int rc;
drh806ebcb2011-04-09 17:53:30 +0000681 assert( p->pDestDb );
682 sqlite3_mutex_enter(p->pDestDb->mutex);
dan5cc3bea2012-12-21 16:15:35 +0000683 rc = backupOnePage(p, iPage, aData, 1);
drh806ebcb2011-04-09 17:53:30 +0000684 sqlite3_mutex_leave(p->pDestDb->mutex);
danielk197703ab0352009-02-06 05:59:44 +0000685 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
danielk197704103022009-02-03 16:51:24 +0000686 if( rc!=SQLITE_OK ){
687 p->rc = rc;
688 }
689 }
690 }
691}
692
693/*
694** Restart the backup process. This is called when the pager layer
695** detects that the database has been modified by an external database
696** connection. In this case there is no way of knowing which of the
697** pages that have been copied into the destination database are still
698** valid and which are not, so the entire process needs to be restarted.
699**
700** It is assumed that the mutex associated with the BtShared object
701** corresponding to the source database is held when this function is
702** called.
703*/
704void sqlite3BackupRestart(sqlite3_backup *pBackup){
705 sqlite3_backup *p; /* Iterator variable */
706 for(p=pBackup; p; p=p->pNext){
707 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
708 p->iNext = 1;
709 }
710}
711
712#ifndef SQLITE_OMIT_VACUUM
713/*
714** Copy the complete content of pBtFrom into pBtTo. A transaction
715** must be active for both files.
716**
717** The size of file pTo may be reduced by this operation. If anything
718** goes wrong, the transaction on pTo is rolled back. If successful, the
719** transaction is committed before returning.
720*/
721int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
722 int rc;
danc5f20a02011-10-07 16:57:59 +0000723 sqlite3_file *pFd; /* File descriptor for database pTo */
danielk197704103022009-02-03 16:51:24 +0000724 sqlite3_backup b;
725 sqlite3BtreeEnter(pTo);
726 sqlite3BtreeEnter(pFrom);
727
danc5f20a02011-10-07 16:57:59 +0000728 assert( sqlite3BtreeIsInTrans(pTo) );
729 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
730 if( pFd->pMethods ){
731 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
drh2bfe5b32012-01-10 16:40:50 +0000732 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
733 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
734 if( rc ) goto copy_finished;
danc5f20a02011-10-07 16:57:59 +0000735 }
736
danielk197704103022009-02-03 16:51:24 +0000737 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
738 ** to 0. This is used by the implementations of sqlite3_backup_step()
739 ** and sqlite3_backup_finish() to detect that they are being called
740 ** from this function, not directly by the user.
741 */
742 memset(&b, 0, sizeof(b));
743 b.pSrcDb = pFrom->db;
744 b.pSrc = pFrom;
745 b.pDest = pTo;
746 b.iNext = 1;
747
748 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
749 ** file. By passing this as the number of pages to copy to
750 ** sqlite3_backup_step(), we can guarantee that the copy finishes
751 ** within a single call (unless an error occurs). The assert() statement
752 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
753 ** or an error code.
754 */
755 sqlite3_backup_step(&b, 0x7FFFFFFF);
756 assert( b.rc!=SQLITE_OK );
757 rc = sqlite3_backup_finish(&b);
758 if( rc==SQLITE_OK ){
drhc9166342012-01-05 23:32:06 +0000759 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
dan1a83bc52011-10-21 14:27:32 +0000760 }else{
761 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
danielk197704103022009-02-03 16:51:24 +0000762 }
763
danc5f20a02011-10-07 16:57:59 +0000764 assert( sqlite3BtreeIsInTrans(pTo)==0 );
drh2bfe5b32012-01-10 16:40:50 +0000765copy_finished:
danielk197704103022009-02-03 16:51:24 +0000766 sqlite3BtreeLeave(pFrom);
767 sqlite3BtreeLeave(pTo);
768 return rc;
769}
770#endif /* SQLITE_OMIT_VACUUM */