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 ** $Id: backup.c,v 1.13 2009/03/16 13:19:36 danielk1977 Exp $ 16 */ 17 #include "sqliteInt.h" 18 #include "btreeInt.h" 19 20 /* Macro to find the minimum of two numeric values. 21 */ 22 #ifndef MIN 23 # define MIN(x,y) ((x)<(y)?(x):(y)) 24 #endif 25 26 /* 27 ** Structure allocated for each backup operation. 28 */ 29 struct sqlite3_backup { 30 sqlite3* pDestDb; /* Destination database handle */ 31 Btree *pDest; /* Destination b-tree file */ 32 u32 iDestSchema; /* Original schema cookie in destination */ 33 int bDestLocked; /* True once a write-transaction is open on pDest */ 34 35 Pgno iNext; /* Page number of the next source page to copy */ 36 sqlite3* pSrcDb; /* Source database handle */ 37 Btree *pSrc; /* Source b-tree file */ 38 39 int rc; /* Backup process error code */ 40 41 /* These two variables are set by every call to backup_step(). They are 42 ** read by calls to backup_remaining() and backup_pagecount(). 43 */ 44 Pgno nRemaining; /* Number of pages left to copy */ 45 Pgno nPagecount; /* Total number of pages to copy */ 46 47 sqlite3_backup *pNext; /* Next backup associated with source pager */ 48 }; 49 50 /* 51 ** THREAD SAFETY NOTES: 52 ** 53 ** Once it has been created using backup_init(), a single sqlite3_backup 54 ** structure may be accessed via two groups of thread-safe entry points: 55 ** 56 ** * Via the sqlite3_backup_XXX() API function backup_step() and 57 ** backup_finish(). Both these functions obtain the source database 58 ** handle mutex and the mutex associated with the source BtShared 59 ** structure, in that order. 60 ** 61 ** * Via the BackupUpdate() and BackupRestart() functions, which are 62 ** invoked by the pager layer to report various state changes in 63 ** the page cache associated with the source database. The mutex 64 ** associated with the source database BtShared structure will always 65 ** be held when either of these functions are invoked. 66 ** 67 ** The other sqlite3_backup_XXX() API functions, backup_remaining() and 68 ** backup_pagecount() are not thread-safe functions. If they are called 69 ** while some other thread is calling backup_step() or backup_finish(), 70 ** the values returned may be invalid. There is no way for a call to 71 ** BackupUpdate() or BackupRestart() to interfere with backup_remaining() 72 ** or backup_pagecount(). 73 ** 74 ** Depending on the SQLite configuration, the database handles and/or 75 ** the Btree objects may have their own mutexes that require locking. 76 ** Non-sharable Btrees (in-memory databases for example), do not have 77 ** associated mutexes. 78 */ 79 80 /* 81 ** Return a pointer corresponding to database zDb (i.e. "main", "temp") 82 ** in connection handle pDb. If such a database cannot be found, return 83 ** a NULL pointer and write an error message to pErrorDb. 84 ** 85 ** If the "temp" database is requested, it may need to be opened by this 86 ** function. If an error occurs while doing so, return 0 and write an 87 ** error message to pErrorDb. 88 */ 89 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){ 90 int i = sqlite3FindDbName(pDb, zDb); 91 92 if( i==1 ){ 93 Parse sParse; 94 memset(&sParse, 0, sizeof(sParse)); 95 sParse.db = pDb; 96 if( sqlite3OpenTempDatabase(&sParse) ){ 97 sqlite3ErrorClear(&sParse); 98 sqlite3Error(pErrorDb, sParse.rc, "%s", sParse.zErrMsg); 99 return 0; 100 } 101 assert( sParse.zErrMsg==0 ); 102 } 103 104 if( i<0 ){ 105 sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb); 106 return 0; 107 } 108 109 return pDb->aDb[i].pBt; 110 } 111 112 /* 113 ** Create an sqlite3_backup process to copy the contents of zSrcDb from 114 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return 115 ** a pointer to the new sqlite3_backup object. 116 ** 117 ** If an error occurs, NULL is returned and an error code and error message 118 ** stored in database handle pDestDb. 119 */ 120 sqlite3_backup *sqlite3_backup_init( 121 sqlite3* pDestDb, /* Database to write to */ 122 const char *zDestDb, /* Name of database within pDestDb */ 123 sqlite3* pSrcDb, /* Database connection to read from */ 124 const char *zSrcDb /* Name of database within pSrcDb */ 125 ){ 126 sqlite3_backup *p; /* Value to return */ 127 128 /* Lock the source database handle. The destination database 129 ** handle is not locked in this routine, but it is locked in 130 ** sqlite3_backup_step(). The user is required to ensure that no 131 ** other thread accesses the destination handle for the duration 132 ** of the backup operation. Any attempt to use the destination 133 ** database connection while a backup is in progress may cause 134 ** a malfunction or a deadlock. 135 */ 136 sqlite3_mutex_enter(pSrcDb->mutex); 137 sqlite3_mutex_enter(pDestDb->mutex); 138 139 if( pSrcDb==pDestDb ){ 140 sqlite3Error( 141 pDestDb, SQLITE_ERROR, "source and destination must be distinct" 142 ); 143 p = 0; 144 }else { 145 /* Allocate space for a new sqlite3_backup object */ 146 p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup)); 147 if( !p ){ 148 sqlite3Error(pDestDb, SQLITE_NOMEM, 0); 149 } 150 } 151 152 /* If the allocation succeeded, populate the new object. */ 153 if( p ){ 154 memset(p, 0, sizeof(sqlite3_backup)); 155 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb); 156 p->pDest = findBtree(pDestDb, pDestDb, zDestDb); 157 p->pDestDb = pDestDb; 158 p->pSrcDb = pSrcDb; 159 p->iNext = 1; 160 161 if( 0==p->pSrc || 0==p->pDest ){ 162 /* One (or both) of the named databases did not exist. An error has 163 ** already been written into the pDestDb handle. All that is left 164 ** to do here is free the sqlite3_backup structure. 165 */ 166 sqlite3_free(p); 167 p = 0; 168 } 169 } 170 171 /* If everything has gone as planned, attach the backup object to the 172 ** source pager. The source pager calls BackupUpdate() and BackupRestart() 173 ** to notify this module if the source file is modified mid-backup. 174 */ 175 if( p ){ 176 sqlite3_backup **pp; /* Pointer to head of pagers backup list */ 177 sqlite3BtreeEnter(p->pSrc); 178 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); 179 p->pNext = *pp; 180 *pp = p; 181 sqlite3BtreeLeave(p->pSrc); 182 p->pSrc->nBackup++; 183 } 184 185 sqlite3_mutex_leave(pDestDb->mutex); 186 sqlite3_mutex_leave(pSrcDb->mutex); 187 return p; 188 } 189 190 /* 191 ** Argument rc is an SQLite error code. Return true if this error is 192 ** considered fatal if encountered during a backup operation. All errors 193 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED. 194 */ 195 static int isFatalError(int rc){ 196 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED); 197 } 198 199 /* 200 ** Parameter zSrcData points to a buffer containing the data for 201 ** page iSrcPg from the source database. Copy this data into the 202 ** destination database. 203 */ 204 static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){ 205 Pager * const pDestPager = sqlite3BtreePager(p->pDest); 206 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc); 207 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest); 208 const int nCopy = MIN(nSrcPgsz, nDestPgsz); 209 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz; 210 211 int rc = SQLITE_OK; 212 i64 iOff; 213 214 assert( p->bDestLocked ); 215 assert( !isFatalError(p->rc) ); 216 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ); 217 assert( zSrcData ); 218 219 /* Catch the case where the destination is an in-memory database and the 220 ** page sizes of the source and destination differ. 221 */ 222 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(sqlite3BtreePager(p->pDest)) ){ 223 rc = SQLITE_READONLY; 224 } 225 226 /* This loop runs once for each destination page spanned by the source 227 ** page. For each iteration, variable iOff is set to the byte offset 228 ** of the destination page. 229 */ 230 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){ 231 DbPage *pDestPg = 0; 232 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1; 233 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue; 234 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg)) 235 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg)) 236 ){ 237 const u8 *zIn = &zSrcData[iOff%nSrcPgsz]; 238 u8 *zDestData = sqlite3PagerGetData(pDestPg); 239 u8 *zOut = &zDestData[iOff%nDestPgsz]; 240 241 /* Copy the data from the source page into the destination page. 242 ** Then clear the Btree layer MemPage.isInit flag. Both this module 243 ** and the pager code use this trick (clearing the first byte 244 ** of the page 'extra' space to invalidate the Btree layers 245 ** cached parse of the page). MemPage.isInit is marked 246 ** "MUST BE FIRST" for this purpose. 247 */ 248 memcpy(zOut, zIn, nCopy); 249 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0; 250 } 251 sqlite3PagerUnref(pDestPg); 252 } 253 254 return rc; 255 } 256 257 /* 258 ** If pFile is currently larger than iSize bytes, then truncate it to 259 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then 260 ** this function is a no-op. 261 ** 262 ** Return SQLITE_OK if everything is successful, or an SQLite error 263 ** code if an error occurs. 264 */ 265 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){ 266 i64 iCurrent; 267 int rc = sqlite3OsFileSize(pFile, &iCurrent); 268 if( rc==SQLITE_OK && iCurrent>iSize ){ 269 rc = sqlite3OsTruncate(pFile, iSize); 270 } 271 return rc; 272 } 273 274 /* 275 ** Copy nPage pages from the source b-tree to the destination. 276 */ 277 int sqlite3_backup_step(sqlite3_backup *p, int nPage){ 278 int rc; 279 280 sqlite3_mutex_enter(p->pSrcDb->mutex); 281 sqlite3BtreeEnter(p->pSrc); 282 if( p->pDestDb ){ 283 sqlite3_mutex_enter(p->pDestDb->mutex); 284 } 285 286 rc = p->rc; 287 if( !isFatalError(rc) ){ 288 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */ 289 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */ 290 int ii; /* Iterator variable */ 291 int nSrcPage = -1; /* Size of source db in pages */ 292 int bCloseTrans = 0; /* True if src db requires unlocking */ 293 294 /* If the source pager is currently in a write-transaction, return 295 ** SQLITE_BUSY immediately. 296 */ 297 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){ 298 rc = SQLITE_BUSY; 299 }else{ 300 rc = SQLITE_OK; 301 } 302 303 /* Lock the destination database, if it is not locked already. */ 304 if( SQLITE_OK==rc && p->bDestLocked==0 305 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 306 ){ 307 p->bDestLocked = 1; 308 rc = sqlite3BtreeGetMeta(p->pDest, 1, &p->iDestSchema); 309 } 310 311 /* If there is no open read-transaction on the source database, open 312 ** one now. If a transaction is opened here, then it will be closed 313 ** before this function exits. 314 */ 315 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){ 316 rc = sqlite3BtreeBeginTrans(p->pSrc, 0); 317 bCloseTrans = 1; 318 } 319 320 /* Now that there is a read-lock on the source database, query the 321 ** source pager for the number of pages in the database. 322 */ 323 if( rc==SQLITE_OK ){ 324 rc = sqlite3PagerPagecount(pSrcPager, &nSrcPage); 325 } 326 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){ 327 const Pgno iSrcPg = p->iNext; /* Source page number */ 328 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){ 329 DbPage *pSrcPg; /* Source page object */ 330 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); 331 if( rc==SQLITE_OK ){ 332 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg)); 333 sqlite3PagerUnref(pSrcPg); 334 } 335 } 336 p->iNext++; 337 } 338 if( rc==SQLITE_OK ){ 339 p->nPagecount = nSrcPage; 340 p->nRemaining = nSrcPage+1-p->iNext; 341 if( p->iNext>(Pgno)nSrcPage ){ 342 rc = SQLITE_DONE; 343 } 344 } 345 346 if( rc==SQLITE_DONE ){ 347 const int nSrcPagesize = sqlite3BtreeGetPageSize(p->pSrc); 348 const int nDestPagesize = sqlite3BtreeGetPageSize(p->pDest); 349 int nDestTruncate; 350 351 /* Update the schema version field in the destination database. This 352 ** is to make sure that the schema-version really does change in 353 ** the case where the source and destination databases have the 354 ** same schema version. 355 */ 356 sqlite3BtreeUpdateMeta(p->pDest, 1, p->iDestSchema+1); 357 if( p->pDestDb ){ 358 sqlite3ResetInternalSchema(p->pDestDb, 0); 359 } 360 361 /* Set nDestTruncate to the final number of pages in the destination 362 ** database. The complication here is that the destination page 363 ** size may be different to the source page size. 364 ** 365 ** If the source page size is smaller than the destination page size, 366 ** round up. In this case the call to sqlite3OsTruncate() below will 367 ** fix the size of the file. However it is important to call 368 ** sqlite3PagerTruncateImage() here so that any pages in the 369 ** destination file that lie beyond the nDestTruncate page mark are 370 ** journalled by PagerCommitPhaseOne() before they are destroyed 371 ** by the file truncation. 372 */ 373 if( nSrcPagesize<nDestPagesize ){ 374 int ratio = nDestPagesize/nSrcPagesize; 375 nDestTruncate = (nSrcPage+ratio-1)/ratio; 376 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){ 377 nDestTruncate--; 378 } 379 }else{ 380 nDestTruncate = nSrcPage * (nSrcPagesize/nDestPagesize); 381 } 382 sqlite3PagerTruncateImage(pDestPager, nDestTruncate); 383 384 if( nSrcPagesize<nDestPagesize ){ 385 /* If the source page-size is smaller than the destination page-size, 386 ** two extra things may need to happen: 387 ** 388 ** * The destination may need to be truncated, and 389 ** 390 ** * Data stored on the pages immediately following the 391 ** pending-byte page in the source database may need to be 392 ** copied into the destination database. 393 */ 394 const i64 iSize = (i64)nSrcPagesize * (i64)nSrcPage; 395 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager); 396 397 assert( pFile ); 398 assert( (i64)nDestTruncate*(i64)nDestPagesize >= iSize || ( 399 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) 400 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+nDestPagesize 401 )); 402 if( SQLITE_OK==(rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1)) 403 && SQLITE_OK==(rc = backupTruncateFile(pFile, iSize)) 404 && SQLITE_OK==(rc = sqlite3PagerSync(pDestPager)) 405 ){ 406 i64 iOff; 407 i64 iEnd = MIN(PENDING_BYTE + nDestPagesize, iSize); 408 for( 409 iOff=PENDING_BYTE+nSrcPagesize; 410 rc==SQLITE_OK && iOff<iEnd; 411 iOff+=nSrcPagesize 412 ){ 413 PgHdr *pSrcPg = 0; 414 const Pgno iSrcPg = (Pgno)((iOff/nSrcPagesize)+1); 415 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); 416 if( rc==SQLITE_OK ){ 417 u8 *zData = sqlite3PagerGetData(pSrcPg); 418 rc = sqlite3OsWrite(pFile, zData, nSrcPagesize, iOff); 419 } 420 sqlite3PagerUnref(pSrcPg); 421 } 422 } 423 }else{ 424 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); 425 } 426 427 /* Finish committing the transaction to the destination database. */ 428 if( SQLITE_OK==rc 429 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest)) 430 ){ 431 rc = SQLITE_DONE; 432 } 433 } 434 435 /* If bCloseTrans is true, then this function opened a read transaction 436 ** on the source database. Close the read transaction here. There is 437 ** no need to check the return values of the btree methods here, as 438 ** "committing" a read-only transaction cannot fail. 439 */ 440 if( bCloseTrans ){ 441 TESTONLY( int rc2 ); 442 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0); 443 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc); 444 assert( rc2==SQLITE_OK ); 445 } 446 447 p->rc = rc; 448 } 449 if( p->pDestDb ){ 450 sqlite3_mutex_leave(p->pDestDb->mutex); 451 } 452 sqlite3BtreeLeave(p->pSrc); 453 sqlite3_mutex_leave(p->pSrcDb->mutex); 454 return rc; 455 } 456 457 /* 458 ** Release all resources associated with an sqlite3_backup* handle. 459 */ 460 int sqlite3_backup_finish(sqlite3_backup *p){ 461 sqlite3_backup **pp; /* Ptr to head of pagers backup list */ 462 sqlite3_mutex *mutex; /* Mutex to protect source database */ 463 int rc; /* Value to return */ 464 465 /* Enter the mutexes */ 466 sqlite3_mutex_enter(p->pSrcDb->mutex); 467 sqlite3BtreeEnter(p->pSrc); 468 mutex = p->pSrcDb->mutex; 469 if( p->pDestDb ){ 470 sqlite3_mutex_enter(p->pDestDb->mutex); 471 } 472 473 /* Detach this backup from the source pager. */ 474 if( p->pDestDb ){ 475 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); 476 while( *pp!=p ){ 477 pp = &(*pp)->pNext; 478 } 479 *pp = p->pNext; 480 p->pSrc->nBackup--; 481 } 482 483 /* If a transaction is still open on the Btree, roll it back. */ 484 sqlite3BtreeRollback(p->pDest); 485 486 /* Set the error code of the destination database handle. */ 487 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc; 488 sqlite3Error(p->pDestDb, rc, 0); 489 490 /* Exit the mutexes and free the backup context structure. */ 491 if( p->pDestDb ){ 492 sqlite3_mutex_leave(p->pDestDb->mutex); 493 } 494 sqlite3BtreeLeave(p->pSrc); 495 if( p->pDestDb ){ 496 sqlite3_free(p); 497 } 498 sqlite3_mutex_leave(mutex); 499 return rc; 500 } 501 502 /* 503 ** Return the number of pages still to be backed up as of the most recent 504 ** call to sqlite3_backup_step(). 505 */ 506 int sqlite3_backup_remaining(sqlite3_backup *p){ 507 return p->nRemaining; 508 } 509 510 /* 511 ** Return the total number of pages in the source database as of the most 512 ** recent call to sqlite3_backup_step(). 513 */ 514 int sqlite3_backup_pagecount(sqlite3_backup *p){ 515 return p->nPagecount; 516 } 517 518 /* 519 ** This function is called after the contents of page iPage of the 520 ** source database have been modified. If page iPage has already been 521 ** copied into the destination database, then the data written to the 522 ** destination is now invalidated. The destination copy of iPage needs 523 ** to be updated with the new data before the backup operation is 524 ** complete. 525 ** 526 ** It is assumed that the mutex associated with the BtShared object 527 ** corresponding to the source database is held when this function is 528 ** called. 529 */ 530 void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){ 531 sqlite3_backup *p; /* Iterator variable */ 532 for(p=pBackup; p; p=p->pNext){ 533 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); 534 if( !isFatalError(p->rc) && iPage<p->iNext ){ 535 /* The backup process p has already copied page iPage. But now it 536 ** has been modified by a transaction on the source pager. Copy 537 ** the new data into the backup. 538 */ 539 int rc = backupOnePage(p, iPage, aData); 540 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED ); 541 if( rc!=SQLITE_OK ){ 542 p->rc = rc; 543 } 544 } 545 } 546 } 547 548 /* 549 ** Restart the backup process. This is called when the pager layer 550 ** detects that the database has been modified by an external database 551 ** connection. In this case there is no way of knowing which of the 552 ** pages that have been copied into the destination database are still 553 ** valid and which are not, so the entire process needs to be restarted. 554 ** 555 ** It is assumed that the mutex associated with the BtShared object 556 ** corresponding to the source database is held when this function is 557 ** called. 558 */ 559 void sqlite3BackupRestart(sqlite3_backup *pBackup){ 560 sqlite3_backup *p; /* Iterator variable */ 561 for(p=pBackup; p; p=p->pNext){ 562 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); 563 p->iNext = 1; 564 } 565 } 566 567 #ifndef SQLITE_OMIT_VACUUM 568 /* 569 ** Copy the complete content of pBtFrom into pBtTo. A transaction 570 ** must be active for both files. 571 ** 572 ** The size of file pTo may be reduced by this operation. If anything 573 ** goes wrong, the transaction on pTo is rolled back. If successful, the 574 ** transaction is committed before returning. 575 */ 576 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ 577 int rc; 578 sqlite3_backup b; 579 sqlite3BtreeEnter(pTo); 580 sqlite3BtreeEnter(pFrom); 581 582 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set 583 ** to 0. This is used by the implementations of sqlite3_backup_step() 584 ** and sqlite3_backup_finish() to detect that they are being called 585 ** from this function, not directly by the user. 586 */ 587 memset(&b, 0, sizeof(b)); 588 b.pSrcDb = pFrom->db; 589 b.pSrc = pFrom; 590 b.pDest = pTo; 591 b.iNext = 1; 592 593 /* 0x7FFFFFFF is the hard limit for the number of pages in a database 594 ** file. By passing this as the number of pages to copy to 595 ** sqlite3_backup_step(), we can guarantee that the copy finishes 596 ** within a single call (unless an error occurs). The assert() statement 597 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 598 ** or an error code. 599 */ 600 sqlite3_backup_step(&b, 0x7FFFFFFF); 601 assert( b.rc!=SQLITE_OK ); 602 rc = sqlite3_backup_finish(&b); 603 if( rc==SQLITE_OK ){ 604 pTo->pBt->pageSizeFixed = 0; 605 } 606 607 sqlite3BtreeLeave(pFrom); 608 sqlite3BtreeLeave(pTo); 609 return rc; 610 } 611 #endif /* SQLITE_OMIT_VACUUM */ 612