blob: d3614b88c388e301eac188344a498c847d527486 [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
18/* Macro to find the minimum of two numeric values.
19*/
20#ifndef MIN
21# define MIN(x,y) ((x)<(y)?(x):(y))
22#endif
23
24/*
25** Structure allocated for each backup operation.
26*/
27struct sqlite3_backup {
28 sqlite3* pDestDb; /* Destination database handle */
29 Btree *pDest; /* Destination b-tree file */
30 u32 iDestSchema; /* Original schema cookie in destination */
31 int bDestLocked; /* True once a write-transaction is open on pDest */
32
33 Pgno iNext; /* Page number of the next source page to copy */
34 sqlite3* pSrcDb; /* Source database handle */
35 Btree *pSrc; /* Source b-tree file */
36
37 int rc; /* Backup process error code */
38
39 /* These two variables are set by every call to backup_step(). They are
40 ** read by calls to backup_remaining() and backup_pagecount().
41 */
42 Pgno nRemaining; /* Number of pages left to copy */
43 Pgno nPagecount; /* Total number of pages to copy */
44
danielk1977e70f4f62009-05-13 07:52:06 +000045 int isAttached; /* True once backup has been registered with pager */
danielk197704103022009-02-03 16:51:24 +000046 sqlite3_backup *pNext; /* Next backup associated with source pager */
47};
48
49/*
50** THREAD SAFETY NOTES:
51**
52** Once it has been created using backup_init(), a single sqlite3_backup
53** structure may be accessed via two groups of thread-safe entry points:
54**
55** * Via the sqlite3_backup_XXX() API function backup_step() and
56** backup_finish(). Both these functions obtain the source database
57** handle mutex and the mutex associated with the source BtShared
58** structure, in that order.
59**
60** * Via the BackupUpdate() and BackupRestart() functions, which are
61** invoked by the pager layer to report various state changes in
62** the page cache associated with the source database. The mutex
63** associated with the source database BtShared structure will always
64** be held when either of these functions are invoked.
65**
66** The other sqlite3_backup_XXX() API functions, backup_remaining() and
67** backup_pagecount() are not thread-safe functions. If they are called
68** while some other thread is calling backup_step() or backup_finish(),
69** the values returned may be invalid. There is no way for a call to
70** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
71** or backup_pagecount().
72**
73** Depending on the SQLite configuration, the database handles and/or
74** the Btree objects may have their own mutexes that require locking.
75** Non-sharable Btrees (in-memory databases for example), do not have
76** associated mutexes.
77*/
78
79/*
80** Return a pointer corresponding to database zDb (i.e. "main", "temp")
81** in connection handle pDb. If such a database cannot be found, return
82** a NULL pointer and write an error message to pErrorDb.
83**
84** If the "temp" database is requested, it may need to be opened by this
85** function. If an error occurs while doing so, return 0 and write an
86** error message to pErrorDb.
87*/
88static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
89 int i = sqlite3FindDbName(pDb, zDb);
90
91 if( i==1 ){
drhe98c9042009-06-02 21:31:38 +000092 Parse *pParse;
93 int rc = 0;
94 pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
95 if( pParse==0 ){
96 sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
97 rc = SQLITE_NOMEM;
98 }else{
99 pParse->db = pDb;
100 if( sqlite3OpenTempDatabase(pParse) ){
drhe98c9042009-06-02 21:31:38 +0000101 sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
102 rc = SQLITE_ERROR;
103 }
drha7564662010-02-22 19:32:31 +0000104 sqlite3DbFree(pErrorDb, pParse->zErrMsg);
drhe98c9042009-06-02 21:31:38 +0000105 sqlite3StackFree(pErrorDb, pParse);
106 }
107 if( rc ){
danielk197704103022009-02-03 16:51:24 +0000108 return 0;
109 }
danielk197704103022009-02-03 16:51:24 +0000110 }
111
112 if( i<0 ){
113 sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
114 return 0;
115 }
116
117 return pDb->aDb[i].pBt;
118}
119
120/*
drhca94d8b2011-01-11 17:38:03 +0000121** Attempt to set the page size of the destination to match the page size
122** of the source.
123*/
124static int setDestPgsz(sqlite3_backup *p){
125 int rc;
126 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
127 return rc;
128}
129
130/*
danielk197704103022009-02-03 16:51:24 +0000131** Create an sqlite3_backup process to copy the contents of zSrcDb from
132** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
133** a pointer to the new sqlite3_backup object.
134**
135** If an error occurs, NULL is returned and an error code and error message
136** stored in database handle pDestDb.
137*/
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
drh662c58c2009-02-03 21:13:07 +0000147 ** handle is not locked in this routine, but it is locked in
148 ** sqlite3_backup_step(). The user is required to ensure that no
danielk197704103022009-02-03 16:51:24 +0000149 ** other thread accesses the destination handle for the duration
drh662c58c2009-02-03 21:13:07 +0000150 ** 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.
danielk197704103022009-02-03 16:51:24 +0000153 */
154 sqlite3_mutex_enter(pSrcDb->mutex);
drheef1eb02009-02-04 16:56:19 +0000155 sqlite3_mutex_enter(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000156
157 if( pSrcDb==pDestDb ){
158 sqlite3Error(
drhb309bec2009-02-04 17:40:57 +0000159 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
danielk197704103022009-02-03 16:51:24 +0000160 );
161 p = 0;
162 }else {
drh9f129f42010-08-31 15:27:32 +0000163 /* 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(). */
dan6809c962012-07-30 14:53:54 +0000167 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
danielk197704103022009-02-03 16:51:24 +0000168 if( !p ){
169 sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
170 }
171 }
172
173 /* If the allocation succeeded, populate the new object. */
174 if( p ){
danielk197704103022009-02-03 16:51:24 +0000175 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
176 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
177 p->pDestDb = pDestDb;
178 p->pSrcDb = pSrcDb;
179 p->iNext = 1;
danielk1977e70f4f62009-05-13 07:52:06 +0000180 p->isAttached = 0;
danielk197704103022009-02-03 16:51:24 +0000181
drhca94d8b2011-01-11 17:38:03 +0000182 if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
183 /* One (or both) of the named databases did not exist or an OOM
184 ** error was hit. The error has already been written into the
185 ** pDestDb handle. All that is left to do here is free the
186 ** sqlite3_backup structure.
danielk197704103022009-02-03 16:51:24 +0000187 */
188 sqlite3_free(p);
189 p = 0;
190 }
191 }
danielk197704103022009-02-03 16:51:24 +0000192 if( p ){
danielk197704103022009-02-03 16:51:24 +0000193 p->pSrc->nBackup++;
194 }
195
drheef1eb02009-02-04 16:56:19 +0000196 sqlite3_mutex_leave(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000197 sqlite3_mutex_leave(pSrcDb->mutex);
198 return p;
199}
200
201/*
danielk197703ab0352009-02-06 05:59:44 +0000202** Argument rc is an SQLite error code. Return true if this error is
203** considered fatal if encountered during a backup operation. All errors
204** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
205*/
206static int isFatalError(int rc){
drhdcd7db52009-05-14 19:26:51 +0000207 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
danielk197703ab0352009-02-06 05:59:44 +0000208}
209
210/*
danielk197704103022009-02-03 16:51:24 +0000211** Parameter zSrcData points to a buffer containing the data for
212** page iSrcPg from the source database. Copy this data into the
213** destination database.
214*/
dan5cc3bea2012-12-21 16:15:35 +0000215static int backupOnePage(
216 sqlite3_backup *p, /* Backup handle */
217 Pgno iSrcPg, /* Source database page to backup */
218 const u8 *zSrcData, /* Source database page data */
219 int bUpdate /* True for an update, false otherwise */
220){
danielk197704103022009-02-03 16:51:24 +0000221 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
222 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
223 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
224 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
225 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
drh2b89fbc2011-04-09 02:09:44 +0000226#ifdef SQLITE_HAS_CODEC
dan0094f372012-09-28 20:23:42 +0000227 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
228 ** guaranteed that the shared-mutex is held by this thread, handle
229 ** p->pSrc may not actually be the owner. */
230 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
drh2b89fbc2011-04-09 02:09:44 +0000231 int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
232#endif
danielk197704103022009-02-03 16:51:24 +0000233 int rc = SQLITE_OK;
234 i64 iOff;
235
dan0094f372012-09-28 20:23:42 +0000236 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
danielk197704103022009-02-03 16:51:24 +0000237 assert( p->bDestLocked );
danielk197703ab0352009-02-06 05:59:44 +0000238 assert( !isFatalError(p->rc) );
danielk197704103022009-02-03 16:51:24 +0000239 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
240 assert( zSrcData );
241
242 /* Catch the case where the destination is an in-memory database and the
243 ** page sizes of the source and destination differ.
244 */
drh3289c5e2010-05-05 16:23:26 +0000245 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
danielk197704103022009-02-03 16:51:24 +0000246 rc = SQLITE_READONLY;
247 }
248
drh871919b2010-08-20 15:32:21 +0000249#ifdef SQLITE_HAS_CODEC
250 /* Backup is not possible if the page size of the destination is changing
drh2b89fbc2011-04-09 02:09:44 +0000251 ** and a codec is in use.
drh871919b2010-08-20 15:32:21 +0000252 */
253 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
254 rc = SQLITE_READONLY;
255 }
drh2b89fbc2011-04-09 02:09:44 +0000256
257 /* Backup is not possible if the number of bytes of reserve space differ
258 ** between source and destination. If there is a difference, try to
259 ** fix the destination to agree with the source. If that is not possible,
260 ** then the backup cannot proceed.
261 */
262 if( nSrcReserve!=nDestReserve ){
263 u32 newPgsz = nSrcPgsz;
264 rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
265 if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
266 }
drh871919b2010-08-20 15:32:21 +0000267#endif
268
danielk197704103022009-02-03 16:51:24 +0000269 /* This loop runs once for each destination page spanned by the source
270 ** page. For each iteration, variable iOff is set to the byte offset
271 ** of the destination page.
272 */
273 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
274 DbPage *pDestPg = 0;
275 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
276 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
277 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
278 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
279 ){
280 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
281 u8 *zDestData = sqlite3PagerGetData(pDestPg);
282 u8 *zOut = &zDestData[iOff%nDestPgsz];
283
284 /* Copy the data from the source page into the destination page.
285 ** Then clear the Btree layer MemPage.isInit flag. Both this module
286 ** and the pager code use this trick (clearing the first byte
287 ** of the page 'extra' space to invalidate the Btree layers
288 ** cached parse of the page). MemPage.isInit is marked
289 ** "MUST BE FIRST" for this purpose.
290 */
291 memcpy(zOut, zIn, nCopy);
292 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
dan5cc3bea2012-12-21 16:15:35 +0000293 if( iOff==0 && bUpdate==0 ){
294 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
295 }
danielk197704103022009-02-03 16:51:24 +0000296 }
297 sqlite3PagerUnref(pDestPg);
298 }
299
300 return rc;
301}
302
303/*
danielk19773d0cbc32009-02-09 18:55:45 +0000304** If pFile is currently larger than iSize bytes, then truncate it to
305** exactly iSize bytes. If pFile is not larger than iSize bytes, then
306** this function is a no-op.
307**
308** Return SQLITE_OK if everything is successful, or an SQLite error
309** code if an error occurs.
310*/
311static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
312 i64 iCurrent;
313 int rc = sqlite3OsFileSize(pFile, &iCurrent);
314 if( rc==SQLITE_OK && iCurrent>iSize ){
315 rc = sqlite3OsTruncate(pFile, iSize);
316 }
317 return rc;
318}
319
320/*
danielk1977e70f4f62009-05-13 07:52:06 +0000321** Register this backup object with the associated source pager for
322** callbacks when pages are changed or the cache invalidated.
323*/
324static void attachBackupObject(sqlite3_backup *p){
325 sqlite3_backup **pp;
326 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
327 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
328 p->pNext = *pp;
329 *pp = p;
330 p->isAttached = 1;
331}
332
333/*
danielk197704103022009-02-03 16:51:24 +0000334** Copy nPage pages from the source b-tree to the destination.
335*/
336int sqlite3_backup_step(sqlite3_backup *p, int nPage){
337 int rc;
drh3289c5e2010-05-05 16:23:26 +0000338 int destMode; /* Destination journal mode */
drh5c10f772010-05-05 18:46:44 +0000339 int pgszSrc = 0; /* Source page size */
340 int pgszDest = 0; /* Destination page size */
danielk197704103022009-02-03 16:51:24 +0000341
342 sqlite3_mutex_enter(p->pSrcDb->mutex);
343 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000344 if( p->pDestDb ){
345 sqlite3_mutex_enter(p->pDestDb->mutex);
346 }
danielk197704103022009-02-03 16:51:24 +0000347
drh5c10f772010-05-05 18:46:44 +0000348 rc = p->rc;
danielk197703ab0352009-02-06 05:59:44 +0000349 if( !isFatalError(rc) ){
danielk197704103022009-02-03 16:51:24 +0000350 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
351 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
352 int ii; /* Iterator variable */
shane63207ab2009-02-04 01:49:30 +0000353 int nSrcPage = -1; /* Size of source db in pages */
danielk197704103022009-02-03 16:51:24 +0000354 int bCloseTrans = 0; /* True if src db requires unlocking */
355
356 /* If the source pager is currently in a write-transaction, return
danielk1977404ca072009-03-16 13:19:36 +0000357 ** SQLITE_BUSY immediately.
danielk197704103022009-02-03 16:51:24 +0000358 */
359 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
danielk1977404ca072009-03-16 13:19:36 +0000360 rc = SQLITE_BUSY;
danielk197703ab0352009-02-06 05:59:44 +0000361 }else{
362 rc = SQLITE_OK;
danielk197704103022009-02-03 16:51:24 +0000363 }
364
365 /* Lock the destination database, if it is not locked already. */
366 if( SQLITE_OK==rc && p->bDestLocked==0
367 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
368 ){
369 p->bDestLocked = 1;
danielk1977602b4662009-07-02 07:47:33 +0000370 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
danielk197704103022009-02-03 16:51:24 +0000371 }
372
373 /* If there is no open read-transaction on the source database, open
374 ** one now. If a transaction is opened here, then it will be closed
375 ** before this function exits.
376 */
377 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
378 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
379 bCloseTrans = 1;
380 }
drh5c10f772010-05-05 18:46:44 +0000381
382 /* Do not allow backup if the destination database is in WAL mode
383 ** and the page sizes are different between source and destination */
384 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
385 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
drh0b9b4302010-06-11 17:01:24 +0000386 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
drh5c10f772010-05-05 18:46:44 +0000387 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
388 rc = SQLITE_READONLY;
389 }
danielk197704103022009-02-03 16:51:24 +0000390
391 /* Now that there is a read-lock on the source database, query the
392 ** source pager for the number of pages in the database.
393 */
drhb1299152010-03-30 22:58:33 +0000394 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
395 assert( nSrcPage>=0 );
danielk197703ab0352009-02-06 05:59:44 +0000396 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
danielk197704103022009-02-03 16:51:24 +0000397 const Pgno iSrcPg = p->iNext; /* Source page number */
398 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
399 DbPage *pSrcPg; /* Source page object */
400 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
401 if( rc==SQLITE_OK ){
dan5cc3bea2012-12-21 16:15:35 +0000402 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
danielk197704103022009-02-03 16:51:24 +0000403 sqlite3PagerUnref(pSrcPg);
404 }
405 }
406 p->iNext++;
407 }
408 if( rc==SQLITE_OK ){
409 p->nPagecount = nSrcPage;
410 p->nRemaining = nSrcPage+1-p->iNext;
shane63207ab2009-02-04 01:49:30 +0000411 if( p->iNext>(Pgno)nSrcPage ){
danielk197704103022009-02-03 16:51:24 +0000412 rc = SQLITE_DONE;
danielk1977e70f4f62009-05-13 07:52:06 +0000413 }else if( !p->isAttached ){
414 attachBackupObject(p);
danielk197704103022009-02-03 16:51:24 +0000415 }
416 }
417
drhf25cd712009-07-06 19:03:12 +0000418 /* Update the schema version field in the destination database. This
419 ** is to make sure that the schema-version really does change in
420 ** the case where the source and destination databases have the
421 ** same schema version.
422 */
drhc5dbffe2011-08-25 20:18:47 +0000423 if( rc==SQLITE_DONE ){
danb483eba2012-10-13 19:58:11 +0000424 if( nSrcPage==0 ){
425 rc = sqlite3BtreeNewDb(p->pDest);
426 nSrcPage = 1;
427 }
428 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
429 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
430 }
drhc5dbffe2011-08-25 20:18:47 +0000431 if( rc==SQLITE_OK ){
432 if( p->pDestDb ){
drh81028a42012-05-15 18:28:27 +0000433 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
drhc5dbffe2011-08-25 20:18:47 +0000434 }
435 if( destMode==PAGER_JOURNALMODE_WAL ){
436 rc = sqlite3BtreeSetVersion(p->pDest, 2);
437 }
drhd3a5c502009-02-03 22:51:06 +0000438 }
dan4b270402011-08-25 19:28:47 +0000439 if( rc==SQLITE_OK ){
440 int nDestTruncate;
441 /* Set nDestTruncate to the final number of pages in the destination
442 ** database. The complication here is that the destination page
443 ** size may be different to the source page size.
danielk197704103022009-02-03 16:51:24 +0000444 **
dan4b270402011-08-25 19:28:47 +0000445 ** If the source page size is smaller than the destination page size,
446 ** round up. In this case the call to sqlite3OsTruncate() below will
447 ** fix the size of the file. However it is important to call
448 ** sqlite3PagerTruncateImage() here so that any pages in the
449 ** destination file that lie beyond the nDestTruncate page mark are
450 ** journalled by PagerCommitPhaseOne() before they are destroyed
451 ** by the file truncation.
danielk197704103022009-02-03 16:51:24 +0000452 */
dan4b270402011-08-25 19:28:47 +0000453 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
454 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
455 if( pgszSrc<pgszDest ){
456 int ratio = pgszDest/pgszSrc;
457 nDestTruncate = (nSrcPage+ratio-1)/ratio;
458 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
459 nDestTruncate--;
drhc6aed542011-01-16 22:37:09 +0000460 }
dan4b270402011-08-25 19:28:47 +0000461 }else{
462 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
drhc6aed542011-01-16 22:37:09 +0000463 }
danb483eba2012-10-13 19:58:11 +0000464 assert( nDestTruncate>0 );
dan4b270402011-08-25 19:28:47 +0000465 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
dan4d26d582011-01-25 18:19:24 +0000466
dan4b270402011-08-25 19:28:47 +0000467 if( pgszSrc<pgszDest ){
468 /* If the source page-size is smaller than the destination page-size,
469 ** two extra things may need to happen:
470 **
471 ** * The destination may need to be truncated, and
472 **
473 ** * Data stored on the pages immediately following the
474 ** pending-byte page in the source database may need to be
475 ** copied into the destination database.
476 */
477 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
478 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
479 i64 iOff;
480 i64 iEnd;
481
482 assert( pFile );
danb483eba2012-10-13 19:58:11 +0000483 assert( nDestTruncate==0
484 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
dan4b270402011-08-25 19:28:47 +0000485 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
486 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
487 ));
488
489 /* This call ensures that all data required to recreate the original
490 ** database has been stored in the journal for pDestPager and the
491 ** journal synced to disk. So at this point we may safely modify
492 ** the database file in any way, knowing that if a power failure
493 ** occurs, the original database will be reconstructed from the
494 ** journal file. */
495 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
496
497 /* Write the extra pages and truncate the database file as required */
498 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
499 for(
500 iOff=PENDING_BYTE+pgszSrc;
501 rc==SQLITE_OK && iOff<iEnd;
502 iOff+=pgszSrc
503 ){
504 PgHdr *pSrcPg = 0;
505 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
506 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
507 if( rc==SQLITE_OK ){
508 u8 *zData = sqlite3PagerGetData(pSrcPg);
509 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
510 }
511 sqlite3PagerUnref(pSrcPg);
512 }
513 if( rc==SQLITE_OK ){
514 rc = backupTruncateFile(pFile, iSize);
515 }
516
517 /* Sync the database file to disk. */
518 if( rc==SQLITE_OK ){
519 rc = sqlite3PagerSync(pDestPager);
520 }
521 }else{
522 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
danielk197704103022009-02-03 16:51:24 +0000523 }
dan4b270402011-08-25 19:28:47 +0000524
525 /* Finish committing the transaction to the destination database. */
526 if( SQLITE_OK==rc
527 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
528 ){
529 rc = SQLITE_DONE;
530 }
danielk197704103022009-02-03 16:51:24 +0000531 }
532 }
533
534 /* If bCloseTrans is true, then this function opened a read transaction
535 ** on the source database. Close the read transaction here. There is
536 ** no need to check the return values of the btree methods here, as
537 ** "committing" a read-only transaction cannot fail.
538 */
539 if( bCloseTrans ){
540 TESTONLY( int rc2 );
541 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
dan60939d02011-03-29 15:40:55 +0000542 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
danielk197704103022009-02-03 16:51:24 +0000543 assert( rc2==SQLITE_OK );
544 }
545
danba3cbf32010-06-30 04:29:03 +0000546 if( rc==SQLITE_IOERR_NOMEM ){
547 rc = SQLITE_NOMEM;
548 }
danielk197703ab0352009-02-06 05:59:44 +0000549 p->rc = rc;
danielk197704103022009-02-03 16:51:24 +0000550 }
drhd3a5c502009-02-03 22:51:06 +0000551 if( p->pDestDb ){
552 sqlite3_mutex_leave(p->pDestDb->mutex);
553 }
danielk197704103022009-02-03 16:51:24 +0000554 sqlite3BtreeLeave(p->pSrc);
555 sqlite3_mutex_leave(p->pSrcDb->mutex);
556 return rc;
557}
558
559/*
560** Release all resources associated with an sqlite3_backup* handle.
561*/
562int sqlite3_backup_finish(sqlite3_backup *p){
563 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
drhed688012012-06-21 15:51:42 +0000564 sqlite3 *pSrcDb; /* Source database connection */
danielk197704103022009-02-03 16:51:24 +0000565 int rc; /* Value to return */
566
567 /* Enter the mutexes */
drhdcd7db52009-05-14 19:26:51 +0000568 if( p==0 ) return SQLITE_OK;
drhed688012012-06-21 15:51:42 +0000569 pSrcDb = p->pSrcDb;
570 sqlite3_mutex_enter(pSrcDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000571 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000572 if( p->pDestDb ){
573 sqlite3_mutex_enter(p->pDestDb->mutex);
574 }
danielk197704103022009-02-03 16:51:24 +0000575
576 /* Detach this backup from the source pager. */
577 if( p->pDestDb ){
danielk1977e70f4f62009-05-13 07:52:06 +0000578 p->pSrc->nBackup--;
579 }
580 if( p->isAttached ){
danielk197704103022009-02-03 16:51:24 +0000581 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
582 while( *pp!=p ){
583 pp = &(*pp)->pNext;
584 }
585 *pp = p->pNext;
danielk197704103022009-02-03 16:51:24 +0000586 }
587
588 /* If a transaction is still open on the Btree, roll it back. */
drh0f198a72012-02-13 16:43:16 +0000589 sqlite3BtreeRollback(p->pDest, SQLITE_OK);
danielk197704103022009-02-03 16:51:24 +0000590
591 /* Set the error code of the destination database handle. */
592 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
593 sqlite3Error(p->pDestDb, rc, 0);
594
595 /* Exit the mutexes and free the backup context structure. */
drhd3a5c502009-02-03 22:51:06 +0000596 if( p->pDestDb ){
drh4245c402012-06-02 14:32:21 +0000597 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
drhd3a5c502009-02-03 22:51:06 +0000598 }
danielk197704103022009-02-03 16:51:24 +0000599 sqlite3BtreeLeave(p->pSrc);
600 if( p->pDestDb ){
drh9f129f42010-08-31 15:27:32 +0000601 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
602 ** call to sqlite3_backup_init() and is destroyed by a call to
603 ** sqlite3_backup_finish(). */
danielk197704103022009-02-03 16:51:24 +0000604 sqlite3_free(p);
605 }
drh4245c402012-06-02 14:32:21 +0000606 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
danielk197704103022009-02-03 16:51:24 +0000607 return rc;
608}
609
610/*
611** Return the number of pages still to be backed up as of the most recent
612** call to sqlite3_backup_step().
613*/
614int sqlite3_backup_remaining(sqlite3_backup *p){
615 return p->nRemaining;
616}
617
618/*
619** Return the total number of pages in the source database as of the most
620** recent call to sqlite3_backup_step().
621*/
622int sqlite3_backup_pagecount(sqlite3_backup *p){
623 return p->nPagecount;
624}
625
626/*
627** This function is called after the contents of page iPage of the
628** source database have been modified. If page iPage has already been
629** copied into the destination database, then the data written to the
630** destination is now invalidated. The destination copy of iPage needs
631** to be updated with the new data before the backup operation is
632** complete.
633**
634** It is assumed that the mutex associated with the BtShared object
635** corresponding to the source database is held when this function is
636** called.
637*/
638void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
639 sqlite3_backup *p; /* Iterator variable */
640 for(p=pBackup; p; p=p->pNext){
641 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
danielk197703ab0352009-02-06 05:59:44 +0000642 if( !isFatalError(p->rc) && iPage<p->iNext ){
danielk197704103022009-02-03 16:51:24 +0000643 /* The backup process p has already copied page iPage. But now it
644 ** has been modified by a transaction on the source pager. Copy
645 ** the new data into the backup.
646 */
drh2b89fbc2011-04-09 02:09:44 +0000647 int rc;
drh806ebcb2011-04-09 17:53:30 +0000648 assert( p->pDestDb );
649 sqlite3_mutex_enter(p->pDestDb->mutex);
dan5cc3bea2012-12-21 16:15:35 +0000650 rc = backupOnePage(p, iPage, aData, 1);
drh806ebcb2011-04-09 17:53:30 +0000651 sqlite3_mutex_leave(p->pDestDb->mutex);
danielk197703ab0352009-02-06 05:59:44 +0000652 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
danielk197704103022009-02-03 16:51:24 +0000653 if( rc!=SQLITE_OK ){
654 p->rc = rc;
655 }
656 }
657 }
658}
659
660/*
661** Restart the backup process. This is called when the pager layer
662** detects that the database has been modified by an external database
663** connection. In this case there is no way of knowing which of the
664** pages that have been copied into the destination database are still
665** valid and which are not, so the entire process needs to be restarted.
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 sqlite3BackupRestart(sqlite3_backup *pBackup){
672 sqlite3_backup *p; /* Iterator variable */
673 for(p=pBackup; p; p=p->pNext){
674 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
675 p->iNext = 1;
676 }
677}
678
679#ifndef SQLITE_OMIT_VACUUM
680/*
681** Copy the complete content of pBtFrom into pBtTo. A transaction
682** must be active for both files.
683**
684** The size of file pTo may be reduced by this operation. If anything
685** goes wrong, the transaction on pTo is rolled back. If successful, the
686** transaction is committed before returning.
687*/
688int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
689 int rc;
danc5f20a02011-10-07 16:57:59 +0000690 sqlite3_file *pFd; /* File descriptor for database pTo */
danielk197704103022009-02-03 16:51:24 +0000691 sqlite3_backup b;
692 sqlite3BtreeEnter(pTo);
693 sqlite3BtreeEnter(pFrom);
694
danc5f20a02011-10-07 16:57:59 +0000695 assert( sqlite3BtreeIsInTrans(pTo) );
696 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
697 if( pFd->pMethods ){
698 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
drh2bfe5b32012-01-10 16:40:50 +0000699 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
700 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
701 if( rc ) goto copy_finished;
danc5f20a02011-10-07 16:57:59 +0000702 }
703
danielk197704103022009-02-03 16:51:24 +0000704 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
705 ** to 0. This is used by the implementations of sqlite3_backup_step()
706 ** and sqlite3_backup_finish() to detect that they are being called
707 ** from this function, not directly by the user.
708 */
709 memset(&b, 0, sizeof(b));
710 b.pSrcDb = pFrom->db;
711 b.pSrc = pFrom;
712 b.pDest = pTo;
713 b.iNext = 1;
714
715 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
716 ** file. By passing this as the number of pages to copy to
717 ** sqlite3_backup_step(), we can guarantee that the copy finishes
718 ** within a single call (unless an error occurs). The assert() statement
719 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
720 ** or an error code.
721 */
722 sqlite3_backup_step(&b, 0x7FFFFFFF);
723 assert( b.rc!=SQLITE_OK );
724 rc = sqlite3_backup_finish(&b);
725 if( rc==SQLITE_OK ){
drhc9166342012-01-05 23:32:06 +0000726 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
dan1a83bc52011-10-21 14:27:32 +0000727 }else{
728 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
danielk197704103022009-02-03 16:51:24 +0000729 }
730
danc5f20a02011-10-07 16:57:59 +0000731 assert( sqlite3BtreeIsInTrans(pTo)==0 );
drh2bfe5b32012-01-10 16:40:50 +0000732copy_finished:
danielk197704103022009-02-03 16:51:24 +0000733 sqlite3BtreeLeave(pFrom);
734 sqlite3BtreeLeave(pTo);
735 return rc;
736}
737#endif /* SQLITE_OMIT_VACUUM */