blob: c2e00d635bd5eff8e2e0602d82c718642e008b12 [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/*
121** Create an sqlite3_backup process to copy the contents of zSrcDb from
122** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
123** a pointer to the new sqlite3_backup object.
124**
125** If an error occurs, NULL is returned and an error code and error message
126** stored in database handle pDestDb.
127*/
128sqlite3_backup *sqlite3_backup_init(
129 sqlite3* pDestDb, /* Database to write to */
130 const char *zDestDb, /* Name of database within pDestDb */
131 sqlite3* pSrcDb, /* Database connection to read from */
132 const char *zSrcDb /* Name of database within pSrcDb */
133){
134 sqlite3_backup *p; /* Value to return */
135
136 /* Lock the source database handle. The destination database
drh662c58c2009-02-03 21:13:07 +0000137 ** handle is not locked in this routine, but it is locked in
138 ** sqlite3_backup_step(). The user is required to ensure that no
danielk197704103022009-02-03 16:51:24 +0000139 ** other thread accesses the destination handle for the duration
drh662c58c2009-02-03 21:13:07 +0000140 ** of the backup operation. Any attempt to use the destination
141 ** database connection while a backup is in progress may cause
142 ** a malfunction or a deadlock.
danielk197704103022009-02-03 16:51:24 +0000143 */
144 sqlite3_mutex_enter(pSrcDb->mutex);
drheef1eb02009-02-04 16:56:19 +0000145 sqlite3_mutex_enter(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000146
147 if( pSrcDb==pDestDb ){
148 sqlite3Error(
drhb309bec2009-02-04 17:40:57 +0000149 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
danielk197704103022009-02-03 16:51:24 +0000150 );
151 p = 0;
152 }else {
153 /* Allocate space for a new sqlite3_backup object */
154 p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
155 if( !p ){
156 sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
157 }
158 }
159
160 /* If the allocation succeeded, populate the new object. */
161 if( p ){
162 memset(p, 0, sizeof(sqlite3_backup));
163 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
164 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
165 p->pDestDb = pDestDb;
166 p->pSrcDb = pSrcDb;
167 p->iNext = 1;
danielk1977e70f4f62009-05-13 07:52:06 +0000168 p->isAttached = 0;
danielk197704103022009-02-03 16:51:24 +0000169
170 if( 0==p->pSrc || 0==p->pDest ){
171 /* One (or both) of the named databases did not exist. An error has
172 ** already been written into the pDestDb handle. All that is left
173 ** to do here is free the sqlite3_backup structure.
174 */
175 sqlite3_free(p);
176 p = 0;
177 }
178 }
danielk197704103022009-02-03 16:51:24 +0000179 if( p ){
danielk197704103022009-02-03 16:51:24 +0000180 p->pSrc->nBackup++;
181 }
182
drheef1eb02009-02-04 16:56:19 +0000183 sqlite3_mutex_leave(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000184 sqlite3_mutex_leave(pSrcDb->mutex);
185 return p;
186}
187
188/*
danielk197703ab0352009-02-06 05:59:44 +0000189** Argument rc is an SQLite error code. Return true if this error is
190** considered fatal if encountered during a backup operation. All errors
191** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
192*/
193static int isFatalError(int rc){
drhdcd7db52009-05-14 19:26:51 +0000194 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
danielk197703ab0352009-02-06 05:59:44 +0000195}
196
197/*
danielk197704103022009-02-03 16:51:24 +0000198** Parameter zSrcData points to a buffer containing the data for
199** page iSrcPg from the source database. Copy this data into the
200** destination database.
201*/
202static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
203 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
204 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
205 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
206 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
207 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
208
209 int rc = SQLITE_OK;
210 i64 iOff;
211
212 assert( p->bDestLocked );
danielk197703ab0352009-02-06 05:59:44 +0000213 assert( !isFatalError(p->rc) );
danielk197704103022009-02-03 16:51:24 +0000214 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
215 assert( zSrcData );
216
217 /* Catch the case where the destination is an in-memory database and the
218 ** page sizes of the source and destination differ.
219 */
220 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(sqlite3BtreePager(p->pDest)) ){
221 rc = SQLITE_READONLY;
222 }
223
224 /* This loop runs once for each destination page spanned by the source
225 ** page. For each iteration, variable iOff is set to the byte offset
226 ** of the destination page.
227 */
228 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
229 DbPage *pDestPg = 0;
230 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
231 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
232 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
233 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
234 ){
235 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
236 u8 *zDestData = sqlite3PagerGetData(pDestPg);
237 u8 *zOut = &zDestData[iOff%nDestPgsz];
238
239 /* Copy the data from the source page into the destination page.
240 ** Then clear the Btree layer MemPage.isInit flag. Both this module
241 ** and the pager code use this trick (clearing the first byte
242 ** of the page 'extra' space to invalidate the Btree layers
243 ** cached parse of the page). MemPage.isInit is marked
244 ** "MUST BE FIRST" for this purpose.
245 */
246 memcpy(zOut, zIn, nCopy);
247 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
248 }
249 sqlite3PagerUnref(pDestPg);
250 }
251
252 return rc;
253}
254
255/*
danielk19773d0cbc32009-02-09 18:55:45 +0000256** If pFile is currently larger than iSize bytes, then truncate it to
257** exactly iSize bytes. If pFile is not larger than iSize bytes, then
258** this function is a no-op.
259**
260** Return SQLITE_OK if everything is successful, or an SQLite error
261** code if an error occurs.
262*/
263static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
264 i64 iCurrent;
265 int rc = sqlite3OsFileSize(pFile, &iCurrent);
266 if( rc==SQLITE_OK && iCurrent>iSize ){
267 rc = sqlite3OsTruncate(pFile, iSize);
268 }
269 return rc;
270}
271
272/*
danielk1977e70f4f62009-05-13 07:52:06 +0000273** Register this backup object with the associated source pager for
274** callbacks when pages are changed or the cache invalidated.
275*/
276static void attachBackupObject(sqlite3_backup *p){
277 sqlite3_backup **pp;
278 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
279 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
280 p->pNext = *pp;
281 *pp = p;
282 p->isAttached = 1;
283}
284
285/*
danielk197704103022009-02-03 16:51:24 +0000286** Copy nPage pages from the source b-tree to the destination.
287*/
288int sqlite3_backup_step(sqlite3_backup *p, int nPage){
289 int rc;
290
291 sqlite3_mutex_enter(p->pSrcDb->mutex);
292 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000293 if( p->pDestDb ){
294 sqlite3_mutex_enter(p->pDestDb->mutex);
295 }
danielk197704103022009-02-03 16:51:24 +0000296
297 rc = p->rc;
danielk197703ab0352009-02-06 05:59:44 +0000298 if( !isFatalError(rc) ){
danielk197704103022009-02-03 16:51:24 +0000299 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
300 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
301 int ii; /* Iterator variable */
shane63207ab2009-02-04 01:49:30 +0000302 int nSrcPage = -1; /* Size of source db in pages */
danielk197704103022009-02-03 16:51:24 +0000303 int bCloseTrans = 0; /* True if src db requires unlocking */
304
305 /* If the source pager is currently in a write-transaction, return
danielk1977404ca072009-03-16 13:19:36 +0000306 ** SQLITE_BUSY immediately.
danielk197704103022009-02-03 16:51:24 +0000307 */
308 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
danielk1977404ca072009-03-16 13:19:36 +0000309 rc = SQLITE_BUSY;
danielk197703ab0352009-02-06 05:59:44 +0000310 }else{
311 rc = SQLITE_OK;
danielk197704103022009-02-03 16:51:24 +0000312 }
313
314 /* Lock the destination database, if it is not locked already. */
315 if( SQLITE_OK==rc && p->bDestLocked==0
316 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
317 ){
318 p->bDestLocked = 1;
danielk1977602b4662009-07-02 07:47:33 +0000319 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
danielk197704103022009-02-03 16:51:24 +0000320 }
321
322 /* If there is no open read-transaction on the source database, open
323 ** one now. If a transaction is opened here, then it will be closed
324 ** before this function exits.
325 */
326 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
327 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
328 bCloseTrans = 1;
329 }
330
331 /* Now that there is a read-lock on the source database, query the
332 ** source pager for the number of pages in the database.
333 */
drhb1299152010-03-30 22:58:33 +0000334 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
335 assert( nSrcPage>=0 );
danielk197703ab0352009-02-06 05:59:44 +0000336 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
danielk197704103022009-02-03 16:51:24 +0000337 const Pgno iSrcPg = p->iNext; /* Source page number */
338 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
339 DbPage *pSrcPg; /* Source page object */
340 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
341 if( rc==SQLITE_OK ){
342 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
343 sqlite3PagerUnref(pSrcPg);
344 }
345 }
346 p->iNext++;
347 }
348 if( rc==SQLITE_OK ){
349 p->nPagecount = nSrcPage;
350 p->nRemaining = nSrcPage+1-p->iNext;
shane63207ab2009-02-04 01:49:30 +0000351 if( p->iNext>(Pgno)nSrcPage ){
danielk197704103022009-02-03 16:51:24 +0000352 rc = SQLITE_DONE;
danielk1977e70f4f62009-05-13 07:52:06 +0000353 }else if( !p->isAttached ){
354 attachBackupObject(p);
danielk197704103022009-02-03 16:51:24 +0000355 }
356 }
357
drhf25cd712009-07-06 19:03:12 +0000358 /* Update the schema version field in the destination database. This
359 ** is to make sure that the schema-version really does change in
360 ** the case where the source and destination databases have the
361 ** same schema version.
362 */
363 if( rc==SQLITE_DONE
364 && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
365 ){
danielk197704103022009-02-03 16:51:24 +0000366 const int nSrcPagesize = sqlite3BtreeGetPageSize(p->pSrc);
367 const int nDestPagesize = sqlite3BtreeGetPageSize(p->pDest);
368 int nDestTruncate;
danielk197704103022009-02-03 16:51:24 +0000369
drhd3a5c502009-02-03 22:51:06 +0000370 if( p->pDestDb ){
371 sqlite3ResetInternalSchema(p->pDestDb, 0);
372 }
danielk197704103022009-02-03 16:51:24 +0000373
374 /* Set nDestTruncate to the final number of pages in the destination
375 ** database. The complication here is that the destination page
376 ** size may be different to the source page size.
377 **
378 ** If the source page size is smaller than the destination page size,
379 ** round up. In this case the call to sqlite3OsTruncate() below will
380 ** fix the size of the file. However it is important to call
381 ** sqlite3PagerTruncateImage() here so that any pages in the
382 ** destination file that lie beyond the nDestTruncate page mark are
383 ** journalled by PagerCommitPhaseOne() before they are destroyed
384 ** by the file truncation.
385 */
386 if( nSrcPagesize<nDestPagesize ){
387 int ratio = nDestPagesize/nSrcPagesize;
388 nDestTruncate = (nSrcPage+ratio-1)/ratio;
drh38aec8d2009-02-16 16:23:09 +0000389 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
danielk1977f2a79f22009-02-12 17:01:49 +0000390 nDestTruncate--;
391 }
danielk197704103022009-02-03 16:51:24 +0000392 }else{
393 nDestTruncate = nSrcPage * (nSrcPagesize/nDestPagesize);
394 }
395 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
396
397 if( nSrcPagesize<nDestPagesize ){
398 /* If the source page-size is smaller than the destination page-size,
399 ** two extra things may need to happen:
400 **
401 ** * The destination may need to be truncated, and
402 **
403 ** * Data stored on the pages immediately following the
404 ** pending-byte page in the source database may need to be
405 ** copied into the destination database.
406 */
danielk19773d0cbc32009-02-09 18:55:45 +0000407 const i64 iSize = (i64)nSrcPagesize * (i64)nSrcPage;
408 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
409
danielk197704103022009-02-03 16:51:24 +0000410 assert( pFile );
danielk1977f2a79f22009-02-12 17:01:49 +0000411 assert( (i64)nDestTruncate*(i64)nDestPagesize >= iSize || (
shane65ad7d22009-02-16 17:55:47 +0000412 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
danielk1977f2a79f22009-02-12 17:01:49 +0000413 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+nDestPagesize
414 ));
danielk197704103022009-02-03 16:51:24 +0000415 if( SQLITE_OK==(rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1))
danielk19773d0cbc32009-02-09 18:55:45 +0000416 && SQLITE_OK==(rc = backupTruncateFile(pFile, iSize))
danielk197704103022009-02-03 16:51:24 +0000417 && SQLITE_OK==(rc = sqlite3PagerSync(pDestPager))
418 ){
419 i64 iOff;
420 i64 iEnd = MIN(PENDING_BYTE + nDestPagesize, iSize);
421 for(
422 iOff=PENDING_BYTE+nSrcPagesize;
423 rc==SQLITE_OK && iOff<iEnd;
424 iOff+=nSrcPagesize
425 ){
426 PgHdr *pSrcPg = 0;
shane63207ab2009-02-04 01:49:30 +0000427 const Pgno iSrcPg = (Pgno)((iOff/nSrcPagesize)+1);
danielk197704103022009-02-03 16:51:24 +0000428 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
429 if( rc==SQLITE_OK ){
430 u8 *zData = sqlite3PagerGetData(pSrcPg);
431 rc = sqlite3OsWrite(pFile, zData, nSrcPagesize, iOff);
432 }
433 sqlite3PagerUnref(pSrcPg);
434 }
435 }
436 }else{
437 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
438 }
439
440 /* Finish committing the transaction to the destination database. */
441 if( SQLITE_OK==rc
442 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest))
443 ){
444 rc = SQLITE_DONE;
445 }
446 }
447
448 /* If bCloseTrans is true, then this function opened a read transaction
449 ** on the source database. Close the read transaction here. There is
450 ** no need to check the return values of the btree methods here, as
451 ** "committing" a read-only transaction cannot fail.
452 */
453 if( bCloseTrans ){
454 TESTONLY( int rc2 );
455 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
456 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc);
457 assert( rc2==SQLITE_OK );
458 }
459
danielk197703ab0352009-02-06 05:59:44 +0000460 p->rc = rc;
danielk197704103022009-02-03 16:51:24 +0000461 }
drhd3a5c502009-02-03 22:51:06 +0000462 if( p->pDestDb ){
463 sqlite3_mutex_leave(p->pDestDb->mutex);
464 }
danielk197704103022009-02-03 16:51:24 +0000465 sqlite3BtreeLeave(p->pSrc);
466 sqlite3_mutex_leave(p->pSrcDb->mutex);
467 return rc;
468}
469
470/*
471** Release all resources associated with an sqlite3_backup* handle.
472*/
473int sqlite3_backup_finish(sqlite3_backup *p){
474 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
475 sqlite3_mutex *mutex; /* Mutex to protect source database */
476 int rc; /* Value to return */
477
478 /* Enter the mutexes */
drhdcd7db52009-05-14 19:26:51 +0000479 if( p==0 ) return SQLITE_OK;
danielk197704103022009-02-03 16:51:24 +0000480 sqlite3_mutex_enter(p->pSrcDb->mutex);
481 sqlite3BtreeEnter(p->pSrc);
482 mutex = p->pSrcDb->mutex;
drhd3a5c502009-02-03 22:51:06 +0000483 if( p->pDestDb ){
484 sqlite3_mutex_enter(p->pDestDb->mutex);
485 }
danielk197704103022009-02-03 16:51:24 +0000486
487 /* Detach this backup from the source pager. */
488 if( p->pDestDb ){
danielk1977e70f4f62009-05-13 07:52:06 +0000489 p->pSrc->nBackup--;
490 }
491 if( p->isAttached ){
danielk197704103022009-02-03 16:51:24 +0000492 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
493 while( *pp!=p ){
494 pp = &(*pp)->pNext;
495 }
496 *pp = p->pNext;
danielk197704103022009-02-03 16:51:24 +0000497 }
498
499 /* If a transaction is still open on the Btree, roll it back. */
500 sqlite3BtreeRollback(p->pDest);
501
502 /* Set the error code of the destination database handle. */
503 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
504 sqlite3Error(p->pDestDb, rc, 0);
505
506 /* Exit the mutexes and free the backup context structure. */
drhd3a5c502009-02-03 22:51:06 +0000507 if( p->pDestDb ){
508 sqlite3_mutex_leave(p->pDestDb->mutex);
509 }
danielk197704103022009-02-03 16:51:24 +0000510 sqlite3BtreeLeave(p->pSrc);
511 if( p->pDestDb ){
512 sqlite3_free(p);
513 }
514 sqlite3_mutex_leave(mutex);
515 return rc;
516}
517
518/*
519** Return the number of pages still to be backed up as of the most recent
520** call to sqlite3_backup_step().
521*/
522int sqlite3_backup_remaining(sqlite3_backup *p){
523 return p->nRemaining;
524}
525
526/*
527** Return the total number of pages in the source database as of the most
528** recent call to sqlite3_backup_step().
529*/
530int sqlite3_backup_pagecount(sqlite3_backup *p){
531 return p->nPagecount;
532}
533
534/*
535** This function is called after the contents of page iPage of the
536** source database have been modified. If page iPage has already been
537** copied into the destination database, then the data written to the
538** destination is now invalidated. The destination copy of iPage needs
539** to be updated with the new data before the backup operation is
540** complete.
541**
542** It is assumed that the mutex associated with the BtShared object
543** corresponding to the source database is held when this function is
544** called.
545*/
546void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
547 sqlite3_backup *p; /* Iterator variable */
548 for(p=pBackup; p; p=p->pNext){
549 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
danielk197703ab0352009-02-06 05:59:44 +0000550 if( !isFatalError(p->rc) && iPage<p->iNext ){
danielk197704103022009-02-03 16:51:24 +0000551 /* The backup process p has already copied page iPage. But now it
552 ** has been modified by a transaction on the source pager. Copy
553 ** the new data into the backup.
554 */
555 int rc = backupOnePage(p, iPage, aData);
danielk197703ab0352009-02-06 05:59:44 +0000556 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
danielk197704103022009-02-03 16:51:24 +0000557 if( rc!=SQLITE_OK ){
558 p->rc = rc;
559 }
560 }
561 }
562}
563
564/*
565** Restart the backup process. This is called when the pager layer
566** detects that the database has been modified by an external database
567** connection. In this case there is no way of knowing which of the
568** pages that have been copied into the destination database are still
569** valid and which are not, so the entire process needs to be restarted.
570**
571** It is assumed that the mutex associated with the BtShared object
572** corresponding to the source database is held when this function is
573** called.
574*/
575void sqlite3BackupRestart(sqlite3_backup *pBackup){
576 sqlite3_backup *p; /* Iterator variable */
577 for(p=pBackup; p; p=p->pNext){
578 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
579 p->iNext = 1;
580 }
581}
582
583#ifndef SQLITE_OMIT_VACUUM
584/*
585** Copy the complete content of pBtFrom into pBtTo. A transaction
586** must be active for both files.
587**
588** The size of file pTo may be reduced by this operation. If anything
589** goes wrong, the transaction on pTo is rolled back. If successful, the
590** transaction is committed before returning.
591*/
592int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
593 int rc;
594 sqlite3_backup b;
595 sqlite3BtreeEnter(pTo);
596 sqlite3BtreeEnter(pFrom);
597
598 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
599 ** to 0. This is used by the implementations of sqlite3_backup_step()
600 ** and sqlite3_backup_finish() to detect that they are being called
601 ** from this function, not directly by the user.
602 */
603 memset(&b, 0, sizeof(b));
604 b.pSrcDb = pFrom->db;
605 b.pSrc = pFrom;
606 b.pDest = pTo;
607 b.iNext = 1;
608
609 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
610 ** file. By passing this as the number of pages to copy to
611 ** sqlite3_backup_step(), we can guarantee that the copy finishes
612 ** within a single call (unless an error occurs). The assert() statement
613 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
614 ** or an error code.
615 */
616 sqlite3_backup_step(&b, 0x7FFFFFFF);
617 assert( b.rc!=SQLITE_OK );
618 rc = sqlite3_backup_finish(&b);
619 if( rc==SQLITE_OK ){
620 pTo->pBt->pageSizeFixed = 0;
621 }
622
623 sqlite3BtreeLeave(pFrom);
624 sqlite3BtreeLeave(pTo);
625 return rc;
626}
627#endif /* SQLITE_OMIT_VACUUM */