1 /* 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. 14 */ 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 */ 27 struct 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 45 int isAttached; /* True once backup has been registered with pager */ 46 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 */ 88 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){ 89 int i = sqlite3FindDbName(pDb, zDb); 90 91 if( i==1 ){ 92 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) ){ 101 sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg); 102 rc = SQLITE_ERROR; 103 } 104 sqlite3DbFree(pErrorDb, pParse->zErrMsg); 105 sqlite3StackFree(pErrorDb, pParse); 106 } 107 if( rc ){ 108 return 0; 109 } 110 } 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 */ 128 sqlite3_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 137 ** handle is not locked in this routine, but it is locked in 138 ** sqlite3_backup_step(). The user is required to ensure that no 139 ** other thread accesses the destination handle for the duration 140 ** 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. 143 */ 144 sqlite3_mutex_enter(pSrcDb->mutex); 145 sqlite3_mutex_enter(pDestDb->mutex); 146 147 if( pSrcDb==pDestDb ){ 148 sqlite3Error( 149 pDestDb, SQLITE_ERROR, "source and destination must be distinct" 150 ); 151 p = 0; 152 }else { 153 /* Allocate space for a new sqlite3_backup object... 154 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a 155 ** call to sqlite3_backup_init() and is destroyed by a call to 156 ** sqlite3_backup_finish(). */ 157 p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup)); 158 if( !p ){ 159 sqlite3Error(pDestDb, SQLITE_NOMEM, 0); 160 } 161 } 162 163 /* If the allocation succeeded, populate the new object. */ 164 if( p ){ 165 memset(p, 0, sizeof(sqlite3_backup)); 166 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb); 167 p->pDest = findBtree(pDestDb, pDestDb, zDestDb); 168 p->pDestDb = pDestDb; 169 p->pSrcDb = pSrcDb; 170 p->iNext = 1; 171 p->isAttached = 0; 172 173 if( 0==p->pSrc || 0==p->pDest ){ 174 /* One (or both) of the named databases did not exist. An error has 175 ** already been written into the pDestDb handle. All that is left 176 ** to do here is free the sqlite3_backup structure. 177 */ 178 sqlite3_free(p); 179 p = 0; 180 } 181 } 182 if( p ){ 183 p->pSrc->nBackup++; 184 } 185 186 sqlite3_mutex_leave(pDestDb->mutex); 187 sqlite3_mutex_leave(pSrcDb->mutex); 188 return p; 189 } 190 191 /* 192 ** Argument rc is an SQLite error code. Return true if this error is 193 ** considered fatal if encountered during a backup operation. All errors 194 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED. 195 */ 196 static int isFatalError(int rc){ 197 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED)); 198 } 199 200 /* 201 ** Parameter zSrcData points to a buffer containing the data for 202 ** page iSrcPg from the source database. Copy this data into the 203 ** destination database. 204 */ 205 static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){ 206 Pager * const pDestPager = sqlite3BtreePager(p->pDest); 207 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc); 208 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest); 209 const int nCopy = MIN(nSrcPgsz, nDestPgsz); 210 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz; 211 212 int rc = SQLITE_OK; 213 i64 iOff; 214 215 assert( p->bDestLocked ); 216 assert( !isFatalError(p->rc) ); 217 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ); 218 assert( zSrcData ); 219 220 /* Catch the case where the destination is an in-memory database and the 221 ** page sizes of the source and destination differ. 222 */ 223 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){ 224 rc = SQLITE_READONLY; 225 } 226 227 #ifdef SQLITE_HAS_CODEC 228 /* Backup is not possible if the page size of the destination is changing 229 ** a a codec is in use. 230 */ 231 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){ 232 rc = SQLITE_READONLY; 233 } 234 #endif 235 236 /* This loop runs once for each destination page spanned by the source 237 ** page. For each iteration, variable iOff is set to the byte offset 238 ** of the destination page. 239 */ 240 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){ 241 DbPage *pDestPg = 0; 242 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1; 243 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue; 244 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg)) 245 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg)) 246 ){ 247 const u8 *zIn = &zSrcData[iOff%nSrcPgsz]; 248 u8 *zDestData = sqlite3PagerGetData(pDestPg); 249 u8 *zOut = &zDestData[iOff%nDestPgsz]; 250 251 /* Copy the data from the source page into the destination page. 252 ** Then clear the Btree layer MemPage.isInit flag. Both this module 253 ** and the pager code use this trick (clearing the first byte 254 ** of the page 'extra' space to invalidate the Btree layers 255 ** cached parse of the page). MemPage.isInit is marked 256 ** "MUST BE FIRST" for this purpose. 257 */ 258 memcpy(zOut, zIn, nCopy); 259 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0; 260 } 261 sqlite3PagerUnref(pDestPg); 262 } 263 264 return rc; 265 } 266 267 /* 268 ** If pFile is currently larger than iSize bytes, then truncate it to 269 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then 270 ** this function is a no-op. 271 ** 272 ** Return SQLITE_OK if everything is successful, or an SQLite error 273 ** code if an error occurs. 274 */ 275 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){ 276 i64 iCurrent; 277 int rc = sqlite3OsFileSize(pFile, &iCurrent); 278 if( rc==SQLITE_OK && iCurrent>iSize ){ 279 rc = sqlite3OsTruncate(pFile, iSize); 280 } 281 return rc; 282 } 283 284 /* 285 ** Register this backup object with the associated source pager for 286 ** callbacks when pages are changed or the cache invalidated. 287 */ 288 static void attachBackupObject(sqlite3_backup *p){ 289 sqlite3_backup **pp; 290 assert( sqlite3BtreeHoldsMutex(p->pSrc) ); 291 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); 292 p->pNext = *pp; 293 *pp = p; 294 p->isAttached = 1; 295 } 296 297 /* 298 ** Copy nPage pages from the source b-tree to the destination. 299 */ 300 int sqlite3_backup_step(sqlite3_backup *p, int nPage){ 301 int rc; 302 int destMode; /* Destination journal mode */ 303 int pgszSrc = 0; /* Source page size */ 304 int pgszDest = 0; /* Destination page size */ 305 306 sqlite3_mutex_enter(p->pSrcDb->mutex); 307 sqlite3BtreeEnter(p->pSrc); 308 if( p->pDestDb ){ 309 sqlite3_mutex_enter(p->pDestDb->mutex); 310 } 311 312 rc = p->rc; 313 if( !isFatalError(rc) ){ 314 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */ 315 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */ 316 int ii; /* Iterator variable */ 317 int nSrcPage = -1; /* Size of source db in pages */ 318 int bCloseTrans = 0; /* True if src db requires unlocking */ 319 320 /* If the source pager is currently in a write-transaction, return 321 ** SQLITE_BUSY immediately. 322 */ 323 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){ 324 rc = SQLITE_BUSY; 325 }else{ 326 rc = SQLITE_OK; 327 } 328 329 /* Lock the destination database, if it is not locked already. */ 330 if( SQLITE_OK==rc && p->bDestLocked==0 331 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 332 ){ 333 p->bDestLocked = 1; 334 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema); 335 } 336 337 /* If there is no open read-transaction on the source database, open 338 ** one now. If a transaction is opened here, then it will be closed 339 ** before this function exits. 340 */ 341 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){ 342 rc = sqlite3BtreeBeginTrans(p->pSrc, 0); 343 bCloseTrans = 1; 344 } 345 346 /* Do not allow backup if the destination database is in WAL mode 347 ** and the page sizes are different between source and destination */ 348 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc); 349 pgszDest = sqlite3BtreeGetPageSize(p->pDest); 350 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest)); 351 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){ 352 rc = SQLITE_READONLY; 353 } 354 355 /* Now that there is a read-lock on the source database, query the 356 ** source pager for the number of pages in the database. 357 */ 358 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc); 359 assert( nSrcPage>=0 ); 360 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){ 361 const Pgno iSrcPg = p->iNext; /* Source page number */ 362 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){ 363 DbPage *pSrcPg; /* Source page object */ 364 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); 365 if( rc==SQLITE_OK ){ 366 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg)); 367 sqlite3PagerUnref(pSrcPg); 368 } 369 } 370 p->iNext++; 371 } 372 if( rc==SQLITE_OK ){ 373 p->nPagecount = nSrcPage; 374 p->nRemaining = nSrcPage+1-p->iNext; 375 if( p->iNext>(Pgno)nSrcPage ){ 376 rc = SQLITE_DONE; 377 }else if( !p->isAttached ){ 378 attachBackupObject(p); 379 } 380 } 381 382 /* Update the schema version field in the destination database. This 383 ** is to make sure that the schema-version really does change in 384 ** the case where the source and destination databases have the 385 ** same schema version. 386 */ 387 if( rc==SQLITE_DONE 388 && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK 389 ){ 390 int nDestTruncate; 391 392 if( p->pDestDb ){ 393 sqlite3ResetInternalSchema(p->pDestDb, 0); 394 } 395 396 /* Set nDestTruncate to the final number of pages in the destination 397 ** database. The complication here is that the destination page 398 ** size may be different to the source page size. 399 ** 400 ** If the source page size is smaller than the destination page size, 401 ** round up. In this case the call to sqlite3OsTruncate() below will 402 ** fix the size of the file. However it is important to call 403 ** sqlite3PagerTruncateImage() here so that any pages in the 404 ** destination file that lie beyond the nDestTruncate page mark are 405 ** journalled by PagerCommitPhaseOne() before they are destroyed 406 ** by the file truncation. 407 */ 408 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) ); 409 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) ); 410 if( pgszSrc<pgszDest ){ 411 int ratio = pgszDest/pgszSrc; 412 nDestTruncate = (nSrcPage+ratio-1)/ratio; 413 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){ 414 nDestTruncate--; 415 } 416 }else{ 417 nDestTruncate = nSrcPage * (pgszSrc/pgszDest); 418 } 419 sqlite3PagerTruncateImage(pDestPager, nDestTruncate); 420 421 if( pgszSrc<pgszDest ){ 422 /* If the source page-size is smaller than the destination page-size, 423 ** two extra things may need to happen: 424 ** 425 ** * The destination may need to be truncated, and 426 ** 427 ** * Data stored on the pages immediately following the 428 ** pending-byte page in the source database may need to be 429 ** copied into the destination database. 430 */ 431 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage; 432 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager); 433 434 assert( pFile ); 435 assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || ( 436 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) 437 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest 438 )); 439 if( SQLITE_OK==(rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1)) 440 && SQLITE_OK==(rc = backupTruncateFile(pFile, iSize)) 441 && SQLITE_OK==(rc = sqlite3PagerSync(pDestPager)) 442 ){ 443 i64 iOff; 444 i64 iEnd = MIN(PENDING_BYTE + pgszDest, iSize); 445 for( 446 iOff=PENDING_BYTE+pgszSrc; 447 rc==SQLITE_OK && iOff<iEnd; 448 iOff+=pgszSrc 449 ){ 450 PgHdr *pSrcPg = 0; 451 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1); 452 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); 453 if( rc==SQLITE_OK ){ 454 u8 *zData = sqlite3PagerGetData(pSrcPg); 455 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff); 456 } 457 sqlite3PagerUnref(pSrcPg); 458 } 459 } 460 }else{ 461 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); 462 } 463 464 /* Finish committing the transaction to the destination database. */ 465 if( SQLITE_OK==rc 466 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest)) 467 ){ 468 rc = SQLITE_DONE; 469 } 470 } 471 472 /* If bCloseTrans is true, then this function opened a read transaction 473 ** on the source database. Close the read transaction here. There is 474 ** no need to check the return values of the btree methods here, as 475 ** "committing" a read-only transaction cannot fail. 476 */ 477 if( bCloseTrans ){ 478 TESTONLY( int rc2 ); 479 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0); 480 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc); 481 assert( rc2==SQLITE_OK ); 482 } 483 484 if( rc==SQLITE_IOERR_NOMEM ){ 485 rc = SQLITE_NOMEM; 486 } 487 p->rc = rc; 488 } 489 if( p->pDestDb ){ 490 sqlite3_mutex_leave(p->pDestDb->mutex); 491 } 492 sqlite3BtreeLeave(p->pSrc); 493 sqlite3_mutex_leave(p->pSrcDb->mutex); 494 return rc; 495 } 496 497 /* 498 ** Release all resources associated with an sqlite3_backup* handle. 499 */ 500 int sqlite3_backup_finish(sqlite3_backup *p){ 501 sqlite3_backup **pp; /* Ptr to head of pagers backup list */ 502 sqlite3_mutex *mutex; /* Mutex to protect source database */ 503 int rc; /* Value to return */ 504 505 /* Enter the mutexes */ 506 if( p==0 ) return SQLITE_OK; 507 sqlite3_mutex_enter(p->pSrcDb->mutex); 508 sqlite3BtreeEnter(p->pSrc); 509 mutex = p->pSrcDb->mutex; 510 if( p->pDestDb ){ 511 sqlite3_mutex_enter(p->pDestDb->mutex); 512 } 513 514 /* Detach this backup from the source pager. */ 515 if( p->pDestDb ){ 516 p->pSrc->nBackup--; 517 } 518 if( p->isAttached ){ 519 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); 520 while( *pp!=p ){ 521 pp = &(*pp)->pNext; 522 } 523 *pp = p->pNext; 524 } 525 526 /* If a transaction is still open on the Btree, roll it back. */ 527 sqlite3BtreeRollback(p->pDest); 528 529 /* Set the error code of the destination database handle. */ 530 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc; 531 sqlite3Error(p->pDestDb, rc, 0); 532 533 /* Exit the mutexes and free the backup context structure. */ 534 if( p->pDestDb ){ 535 sqlite3_mutex_leave(p->pDestDb->mutex); 536 } 537 sqlite3BtreeLeave(p->pSrc); 538 if( p->pDestDb ){ 539 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a 540 ** call to sqlite3_backup_init() and is destroyed by a call to 541 ** sqlite3_backup_finish(). */ 542 sqlite3_free(p); 543 } 544 sqlite3_mutex_leave(mutex); 545 return rc; 546 } 547 548 /* 549 ** Return the number of pages still to be backed up as of the most recent 550 ** call to sqlite3_backup_step(). 551 */ 552 int sqlite3_backup_remaining(sqlite3_backup *p){ 553 return p->nRemaining; 554 } 555 556 /* 557 ** Return the total number of pages in the source database as of the most 558 ** recent call to sqlite3_backup_step(). 559 */ 560 int sqlite3_backup_pagecount(sqlite3_backup *p){ 561 return p->nPagecount; 562 } 563 564 /* 565 ** This function is called after the contents of page iPage of the 566 ** source database have been modified. If page iPage has already been 567 ** copied into the destination database, then the data written to the 568 ** destination is now invalidated. The destination copy of iPage needs 569 ** to be updated with the new data before the backup operation is 570 ** complete. 571 ** 572 ** It is assumed that the mutex associated with the BtShared object 573 ** corresponding to the source database is held when this function is 574 ** called. 575 */ 576 void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){ 577 sqlite3_backup *p; /* Iterator variable */ 578 for(p=pBackup; p; p=p->pNext){ 579 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); 580 if( !isFatalError(p->rc) && iPage<p->iNext ){ 581 /* The backup process p has already copied page iPage. But now it 582 ** has been modified by a transaction on the source pager. Copy 583 ** the new data into the backup. 584 */ 585 int rc = backupOnePage(p, iPage, aData); 586 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED ); 587 if( rc!=SQLITE_OK ){ 588 p->rc = rc; 589 } 590 } 591 } 592 } 593 594 /* 595 ** Restart the backup process. This is called when the pager layer 596 ** detects that the database has been modified by an external database 597 ** connection. In this case there is no way of knowing which of the 598 ** pages that have been copied into the destination database are still 599 ** valid and which are not, so the entire process needs to be restarted. 600 ** 601 ** It is assumed that the mutex associated with the BtShared object 602 ** corresponding to the source database is held when this function is 603 ** called. 604 */ 605 void sqlite3BackupRestart(sqlite3_backup *pBackup){ 606 sqlite3_backup *p; /* Iterator variable */ 607 for(p=pBackup; p; p=p->pNext){ 608 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); 609 p->iNext = 1; 610 } 611 } 612 613 #ifndef SQLITE_OMIT_VACUUM 614 /* 615 ** Copy the complete content of pBtFrom into pBtTo. A transaction 616 ** must be active for both files. 617 ** 618 ** The size of file pTo may be reduced by this operation. If anything 619 ** goes wrong, the transaction on pTo is rolled back. If successful, the 620 ** transaction is committed before returning. 621 */ 622 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ 623 int rc; 624 sqlite3_backup b; 625 sqlite3BtreeEnter(pTo); 626 sqlite3BtreeEnter(pFrom); 627 628 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set 629 ** to 0. This is used by the implementations of sqlite3_backup_step() 630 ** and sqlite3_backup_finish() to detect that they are being called 631 ** from this function, not directly by the user. 632 */ 633 memset(&b, 0, sizeof(b)); 634 b.pSrcDb = pFrom->db; 635 b.pSrc = pFrom; 636 b.pDest = pTo; 637 b.iNext = 1; 638 639 /* 0x7FFFFFFF is the hard limit for the number of pages in a database 640 ** file. By passing this as the number of pages to copy to 641 ** sqlite3_backup_step(), we can guarantee that the copy finishes 642 ** within a single call (unless an error occurs). The assert() statement 643 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 644 ** or an error code. 645 */ 646 sqlite3_backup_step(&b, 0x7FFFFFFF); 647 assert( b.rc!=SQLITE_OK ); 648 rc = sqlite3_backup_finish(&b); 649 if( rc==SQLITE_OK ){ 650 pTo->pBt->pageSizeFixed = 0; 651 } 652 653 sqlite3BtreeLeave(pFrom); 654 sqlite3BtreeLeave(pTo); 655 return rc; 656 } 657 #endif /* SQLITE_OMIT_VACUUM */ 658