OLD | NEW |
1 /* | 1 /* |
2 ** 2009 January 28 | 2 ** 2009 January 28 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** This file contains the implementation of the sqlite3_backup_XXX() | 12 ** This file contains the implementation of the sqlite3_backup_XXX() |
13 ** API functions and the related features. | 13 ** API functions and the related features. |
14 */ | 14 */ |
15 #include "sqliteInt.h" | 15 #include "sqliteInt.h" |
16 #include "btreeInt.h" | 16 #include "btreeInt.h" |
17 | 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 /* | 18 /* |
25 ** Structure allocated for each backup operation. | 19 ** Structure allocated for each backup operation. |
26 */ | 20 */ |
27 struct sqlite3_backup { | 21 struct sqlite3_backup { |
28 sqlite3* pDestDb; /* Destination database handle */ | 22 sqlite3* pDestDb; /* Destination database handle */ |
29 Btree *pDest; /* Destination b-tree file */ | 23 Btree *pDest; /* Destination b-tree file */ |
30 u32 iDestSchema; /* Original schema cookie in destination */ | 24 u32 iDestSchema; /* Original schema cookie in destination */ |
31 int bDestLocked; /* True once a write-transaction is open on pDest */ | 25 int bDestLocked; /* True once a write-transaction is open on pDest */ |
32 | 26 |
33 Pgno iNext; /* Page number of the next source page to copy */ | 27 Pgno iNext; /* Page number of the next source page to copy */ |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 ** error message to pErrorDb. | 80 ** error message to pErrorDb. |
87 */ | 81 */ |
88 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){ | 82 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){ |
89 int i = sqlite3FindDbName(pDb, zDb); | 83 int i = sqlite3FindDbName(pDb, zDb); |
90 | 84 |
91 if( i==1 ){ | 85 if( i==1 ){ |
92 Parse *pParse; | 86 Parse *pParse; |
93 int rc = 0; | 87 int rc = 0; |
94 pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse)); | 88 pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse)); |
95 if( pParse==0 ){ | 89 if( pParse==0 ){ |
96 sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory"); | 90 sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory"); |
97 rc = SQLITE_NOMEM; | 91 rc = SQLITE_NOMEM; |
98 }else{ | 92 }else{ |
99 pParse->db = pDb; | 93 pParse->db = pDb; |
100 if( sqlite3OpenTempDatabase(pParse) ){ | 94 if( sqlite3OpenTempDatabase(pParse) ){ |
101 sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg); | 95 sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg); |
102 rc = SQLITE_ERROR; | 96 rc = SQLITE_ERROR; |
103 } | 97 } |
104 sqlite3DbFree(pErrorDb, pParse->zErrMsg); | 98 sqlite3DbFree(pErrorDb, pParse->zErrMsg); |
| 99 sqlite3ParserReset(pParse); |
105 sqlite3StackFree(pErrorDb, pParse); | 100 sqlite3StackFree(pErrorDb, pParse); |
106 } | 101 } |
107 if( rc ){ | 102 if( rc ){ |
108 return 0; | 103 return 0; |
109 } | 104 } |
110 } | 105 } |
111 | 106 |
112 if( i<0 ){ | 107 if( i<0 ){ |
113 sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb); | 108 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb); |
114 return 0; | 109 return 0; |
115 } | 110 } |
116 | 111 |
117 return pDb->aDb[i].pBt; | 112 return pDb->aDb[i].pBt; |
118 } | 113 } |
119 | 114 |
120 /* | 115 /* |
121 ** Attempt to set the page size of the destination to match the page size | 116 ** Attempt to set the page size of the destination to match the page size |
122 ** of the source. | 117 ** of the source. |
123 */ | 118 */ |
(...skipping 24 matching lines...) Expand all Loading... |
148 ** sqlite3_backup_step(). The user is required to ensure that no | 143 ** sqlite3_backup_step(). The user is required to ensure that no |
149 ** other thread accesses the destination handle for the duration | 144 ** other thread accesses the destination handle for the duration |
150 ** of the backup operation. Any attempt to use the destination | 145 ** of the backup operation. Any attempt to use the destination |
151 ** database connection while a backup is in progress may cause | 146 ** database connection while a backup is in progress may cause |
152 ** a malfunction or a deadlock. | 147 ** a malfunction or a deadlock. |
153 */ | 148 */ |
154 sqlite3_mutex_enter(pSrcDb->mutex); | 149 sqlite3_mutex_enter(pSrcDb->mutex); |
155 sqlite3_mutex_enter(pDestDb->mutex); | 150 sqlite3_mutex_enter(pDestDb->mutex); |
156 | 151 |
157 if( pSrcDb==pDestDb ){ | 152 if( pSrcDb==pDestDb ){ |
158 sqlite3Error( | 153 sqlite3ErrorWithMsg( |
159 pDestDb, SQLITE_ERROR, "source and destination must be distinct" | 154 pDestDb, SQLITE_ERROR, "source and destination must be distinct" |
160 ); | 155 ); |
161 p = 0; | 156 p = 0; |
162 }else { | 157 }else { |
163 /* Allocate space for a new sqlite3_backup object... | 158 /* Allocate space for a new sqlite3_backup object... |
164 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a | 159 ** 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 | 160 ** call to sqlite3_backup_init() and is destroyed by a call to |
166 ** sqlite3_backup_finish(). */ | 161 ** sqlite3_backup_finish(). */ |
167 p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup)); | 162 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup)); |
168 if( !p ){ | 163 if( !p ){ |
169 sqlite3Error(pDestDb, SQLITE_NOMEM, 0); | 164 sqlite3Error(pDestDb, SQLITE_NOMEM); |
170 } | 165 } |
171 } | 166 } |
172 | 167 |
173 /* If the allocation succeeded, populate the new object. */ | 168 /* If the allocation succeeded, populate the new object. */ |
174 if( p ){ | 169 if( p ){ |
175 memset(p, 0, sizeof(sqlite3_backup)); | |
176 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb); | 170 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb); |
177 p->pDest = findBtree(pDestDb, pDestDb, zDestDb); | 171 p->pDest = findBtree(pDestDb, pDestDb, zDestDb); |
178 p->pDestDb = pDestDb; | 172 p->pDestDb = pDestDb; |
179 p->pSrcDb = pSrcDb; | 173 p->pSrcDb = pSrcDb; |
180 p->iNext = 1; | 174 p->iNext = 1; |
181 p->isAttached = 0; | 175 p->isAttached = 0; |
182 | 176 |
183 if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){ | 177 if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){ |
184 /* One (or both) of the named databases did not exist or an OOM | 178 /* One (or both) of the named databases did not exist or an OOM |
185 ** error was hit. The error has already been written into the | 179 ** error was hit. The error has already been written into the |
(...skipping 20 matching lines...) Expand all Loading... |
206 */ | 200 */ |
207 static int isFatalError(int rc){ | 201 static int isFatalError(int rc){ |
208 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED)); | 202 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED)); |
209 } | 203 } |
210 | 204 |
211 /* | 205 /* |
212 ** Parameter zSrcData points to a buffer containing the data for | 206 ** Parameter zSrcData points to a buffer containing the data for |
213 ** page iSrcPg from the source database. Copy this data into the | 207 ** page iSrcPg from the source database. Copy this data into the |
214 ** destination database. | 208 ** destination database. |
215 */ | 209 */ |
216 static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){ | 210 static int backupOnePage( |
| 211 sqlite3_backup *p, /* Backup handle */ |
| 212 Pgno iSrcPg, /* Source database page to backup */ |
| 213 const u8 *zSrcData, /* Source database page data */ |
| 214 int bUpdate /* True for an update, false otherwise */ |
| 215 ){ |
217 Pager * const pDestPager = sqlite3BtreePager(p->pDest); | 216 Pager * const pDestPager = sqlite3BtreePager(p->pDest); |
218 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc); | 217 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc); |
219 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest); | 218 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest); |
220 const int nCopy = MIN(nSrcPgsz, nDestPgsz); | 219 const int nCopy = MIN(nSrcPgsz, nDestPgsz); |
221 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz; | 220 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz; |
222 #ifdef SQLITE_HAS_CODEC | 221 #ifdef SQLITE_HAS_CODEC |
223 int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc); | 222 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is |
| 223 ** guaranteed that the shared-mutex is held by this thread, handle |
| 224 ** p->pSrc may not actually be the owner. */ |
| 225 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc); |
224 int nDestReserve = sqlite3BtreeGetReserve(p->pDest); | 226 int nDestReserve = sqlite3BtreeGetReserve(p->pDest); |
225 #endif | 227 #endif |
226 | |
227 int rc = SQLITE_OK; | 228 int rc = SQLITE_OK; |
228 i64 iOff; | 229 i64 iOff; |
229 | 230 |
| 231 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 ); |
230 assert( p->bDestLocked ); | 232 assert( p->bDestLocked ); |
231 assert( !isFatalError(p->rc) ); | 233 assert( !isFatalError(p->rc) ); |
232 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ); | 234 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ); |
233 assert( zSrcData ); | 235 assert( zSrcData ); |
234 | 236 |
235 /* Catch the case where the destination is an in-memory database and the | 237 /* Catch the case where the destination is an in-memory database and the |
236 ** page sizes of the source and destination differ. | 238 ** page sizes of the source and destination differ. |
237 */ | 239 */ |
238 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){ | 240 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){ |
239 rc = SQLITE_READONLY; | 241 rc = SQLITE_READONLY; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 | 278 |
277 /* Copy the data from the source page into the destination page. | 279 /* Copy the data from the source page into the destination page. |
278 ** Then clear the Btree layer MemPage.isInit flag. Both this module | 280 ** Then clear the Btree layer MemPage.isInit flag. Both this module |
279 ** and the pager code use this trick (clearing the first byte | 281 ** and the pager code use this trick (clearing the first byte |
280 ** of the page 'extra' space to invalidate the Btree layers | 282 ** of the page 'extra' space to invalidate the Btree layers |
281 ** cached parse of the page). MemPage.isInit is marked | 283 ** cached parse of the page). MemPage.isInit is marked |
282 ** "MUST BE FIRST" for this purpose. | 284 ** "MUST BE FIRST" for this purpose. |
283 */ | 285 */ |
284 memcpy(zOut, zIn, nCopy); | 286 memcpy(zOut, zIn, nCopy); |
285 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0; | 287 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0; |
| 288 if( iOff==0 && bUpdate==0 ){ |
| 289 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc)); |
| 290 } |
286 } | 291 } |
287 sqlite3PagerUnref(pDestPg); | 292 sqlite3PagerUnref(pDestPg); |
288 } | 293 } |
289 | 294 |
290 return rc; | 295 return rc; |
291 } | 296 } |
292 | 297 |
293 /* | 298 /* |
294 ** If pFile is currently larger than iSize bytes, then truncate it to | 299 ** If pFile is currently larger than iSize bytes, then truncate it to |
295 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then | 300 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 | 385 |
381 /* Now that there is a read-lock on the source database, query the | 386 /* Now that there is a read-lock on the source database, query the |
382 ** source pager for the number of pages in the database. | 387 ** source pager for the number of pages in the database. |
383 */ | 388 */ |
384 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc); | 389 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc); |
385 assert( nSrcPage>=0 ); | 390 assert( nSrcPage>=0 ); |
386 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){ | 391 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){ |
387 const Pgno iSrcPg = p->iNext; /* Source page number */ | 392 const Pgno iSrcPg = p->iNext; /* Source page number */ |
388 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){ | 393 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){ |
389 DbPage *pSrcPg; /* Source page object */ | 394 DbPage *pSrcPg; /* Source page object */ |
390 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); | 395 rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg, |
| 396 PAGER_GET_READONLY); |
391 if( rc==SQLITE_OK ){ | 397 if( rc==SQLITE_OK ){ |
392 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg)); | 398 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0); |
393 sqlite3PagerUnref(pSrcPg); | 399 sqlite3PagerUnref(pSrcPg); |
394 } | 400 } |
395 } | 401 } |
396 p->iNext++; | 402 p->iNext++; |
397 } | 403 } |
398 if( rc==SQLITE_OK ){ | 404 if( rc==SQLITE_OK ){ |
399 p->nPagecount = nSrcPage; | 405 p->nPagecount = nSrcPage; |
400 p->nRemaining = nSrcPage+1-p->iNext; | 406 p->nRemaining = nSrcPage+1-p->iNext; |
401 if( p->iNext>(Pgno)nSrcPage ){ | 407 if( p->iNext>(Pgno)nSrcPage ){ |
402 rc = SQLITE_DONE; | 408 rc = SQLITE_DONE; |
403 }else if( !p->isAttached ){ | 409 }else if( !p->isAttached ){ |
404 attachBackupObject(p); | 410 attachBackupObject(p); |
405 } | 411 } |
406 } | 412 } |
407 | 413 |
408 /* Update the schema version field in the destination database. This | 414 /* Update the schema version field in the destination database. This |
409 ** is to make sure that the schema-version really does change in | 415 ** is to make sure that the schema-version really does change in |
410 ** the case where the source and destination databases have the | 416 ** the case where the source and destination databases have the |
411 ** same schema version. | 417 ** same schema version. |
412 */ | 418 */ |
413 if( rc==SQLITE_DONE | 419 if( rc==SQLITE_DONE ){ |
414 && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK | 420 if( nSrcPage==0 ){ |
415 ){ | 421 rc = sqlite3BtreeNewDb(p->pDest); |
416 int nDestTruncate; | 422 nSrcPage = 1; |
417 | |
418 if( p->pDestDb ){ | |
419 sqlite3ResetInternalSchema(p->pDestDb, -1); | |
420 } | 423 } |
| 424 if( rc==SQLITE_OK || rc==SQLITE_DONE ){ |
| 425 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1); |
| 426 } |
| 427 if( rc==SQLITE_OK ){ |
| 428 if( p->pDestDb ){ |
| 429 sqlite3ResetAllSchemasOfConnection(p->pDestDb); |
| 430 } |
| 431 if( destMode==PAGER_JOURNALMODE_WAL ){ |
| 432 rc = sqlite3BtreeSetVersion(p->pDest, 2); |
| 433 } |
| 434 } |
| 435 if( rc==SQLITE_OK ){ |
| 436 int nDestTruncate; |
| 437 /* Set nDestTruncate to the final number of pages in the destination |
| 438 ** database. The complication here is that the destination page |
| 439 ** size may be different to the source page size. |
| 440 ** |
| 441 ** If the source page size is smaller than the destination page size, |
| 442 ** round up. In this case the call to sqlite3OsTruncate() below will |
| 443 ** fix the size of the file. However it is important to call |
| 444 ** sqlite3PagerTruncateImage() here so that any pages in the |
| 445 ** destination file that lie beyond the nDestTruncate page mark are |
| 446 ** journalled by PagerCommitPhaseOne() before they are destroyed |
| 447 ** by the file truncation. |
| 448 */ |
| 449 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) ); |
| 450 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) ); |
| 451 if( pgszSrc<pgszDest ){ |
| 452 int ratio = pgszDest/pgszSrc; |
| 453 nDestTruncate = (nSrcPage+ratio-1)/ratio; |
| 454 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){ |
| 455 nDestTruncate--; |
| 456 } |
| 457 }else{ |
| 458 nDestTruncate = nSrcPage * (pgszSrc/pgszDest); |
| 459 } |
| 460 assert( nDestTruncate>0 ); |
421 | 461 |
422 /* Set nDestTruncate to the final number of pages in the destination | 462 if( pgszSrc<pgszDest ){ |
423 ** database. The complication here is that the destination page | 463 /* If the source page-size is smaller than the destination page-size, |
424 ** size may be different to the source page size. | 464 ** two extra things may need to happen: |
425 ** | 465 ** |
426 ** If the source page size is smaller than the destination page size, | 466 ** * The destination may need to be truncated, and |
427 ** round up. In this case the call to sqlite3OsTruncate() below will | 467 ** |
428 ** fix the size of the file. However it is important to call | 468 ** * Data stored on the pages immediately following the |
429 ** sqlite3PagerTruncateImage() here so that any pages in the | 469 ** pending-byte page in the source database may need to be |
430 ** destination file that lie beyond the nDestTruncate page mark are | 470 ** copied into the destination database. |
431 ** journalled by PagerCommitPhaseOne() before they are destroyed | 471 */ |
432 ** by the file truncation. | 472 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage; |
433 */ | 473 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager); |
434 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) ); | 474 Pgno iPg; |
435 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) ); | 475 int nDstPage; |
436 if( pgszSrc<pgszDest ){ | 476 i64 iOff; |
437 int ratio = pgszDest/pgszSrc; | 477 i64 iEnd; |
438 nDestTruncate = (nSrcPage+ratio-1)/ratio; | 478 |
439 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){ | 479 assert( pFile ); |
440 nDestTruncate--; | 480 assert( nDestTruncate==0 |
| 481 || (i64)nDestTruncate*(i64)pgszDest >= iSize || ( |
| 482 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) |
| 483 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest |
| 484 )); |
| 485 |
| 486 /* This block ensures that all data required to recreate the original |
| 487 ** database has been stored in the journal for pDestPager and the |
| 488 ** journal synced to disk. So at this point we may safely modify |
| 489 ** the database file in any way, knowing that if a power failure |
| 490 ** occurs, the original database will be reconstructed from the |
| 491 ** journal file. */ |
| 492 sqlite3PagerPagecount(pDestPager, &nDstPage); |
| 493 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){ |
| 494 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){ |
| 495 DbPage *pPg; |
| 496 rc = sqlite3PagerGet(pDestPager, iPg, &pPg); |
| 497 if( rc==SQLITE_OK ){ |
| 498 rc = sqlite3PagerWrite(pPg); |
| 499 sqlite3PagerUnref(pPg); |
| 500 } |
| 501 } |
| 502 } |
| 503 if( rc==SQLITE_OK ){ |
| 504 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1); |
| 505 } |
| 506 |
| 507 /* Write the extra pages and truncate the database file as required */ |
| 508 iEnd = MIN(PENDING_BYTE + pgszDest, iSize); |
| 509 for( |
| 510 iOff=PENDING_BYTE+pgszSrc; |
| 511 rc==SQLITE_OK && iOff<iEnd; |
| 512 iOff+=pgszSrc |
| 513 ){ |
| 514 PgHdr *pSrcPg = 0; |
| 515 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1); |
| 516 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); |
| 517 if( rc==SQLITE_OK ){ |
| 518 u8 *zData = sqlite3PagerGetData(pSrcPg); |
| 519 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff); |
| 520 } |
| 521 sqlite3PagerUnref(pSrcPg); |
| 522 } |
| 523 if( rc==SQLITE_OK ){ |
| 524 rc = backupTruncateFile(pFile, iSize); |
| 525 } |
| 526 |
| 527 /* Sync the database file to disk. */ |
| 528 if( rc==SQLITE_OK ){ |
| 529 rc = sqlite3PagerSync(pDestPager, 0); |
| 530 } |
| 531 }else{ |
| 532 sqlite3PagerTruncateImage(pDestPager, nDestTruncate); |
| 533 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); |
441 } | 534 } |
442 }else{ | 535 |
443 nDestTruncate = nSrcPage * (pgszSrc/pgszDest); | 536 /* Finish committing the transaction to the destination database. */ |
444 } | 537 if( SQLITE_OK==rc |
445 sqlite3PagerTruncateImage(pDestPager, nDestTruncate); | 538 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0)) |
446 | |
447 if( pgszSrc<pgszDest ){ | |
448 /* If the source page-size is smaller than the destination page-size, | |
449 ** two extra things may need to happen: | |
450 ** | |
451 ** * The destination may need to be truncated, and | |
452 ** | |
453 ** * Data stored on the pages immediately following the | |
454 ** pending-byte page in the source database may need to be | |
455 ** copied into the destination database. | |
456 */ | |
457 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage; | |
458 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager); | |
459 i64 iOff; | |
460 i64 iEnd; | |
461 | |
462 assert( pFile ); | |
463 assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || ( | |
464 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) | |
465 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest | |
466 )); | |
467 | |
468 /* This call ensures that all data required to recreate the original | |
469 ** database has been stored in the journal for pDestPager and the | |
470 ** journal synced to disk. So at this point we may safely modify | |
471 ** the database file in any way, knowing that if a power failure | |
472 ** occurs, the original database will be reconstructed from the | |
473 ** journal file. */ | |
474 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1); | |
475 | |
476 /* Write the extra pages and truncate the database file as required. */ | |
477 iEnd = MIN(PENDING_BYTE + pgszDest, iSize); | |
478 for( | |
479 iOff=PENDING_BYTE+pgszSrc; | |
480 rc==SQLITE_OK && iOff<iEnd; | |
481 iOff+=pgszSrc | |
482 ){ | 539 ){ |
483 PgHdr *pSrcPg = 0; | 540 rc = SQLITE_DONE; |
484 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1); | |
485 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); | |
486 if( rc==SQLITE_OK ){ | |
487 u8 *zData = sqlite3PagerGetData(pSrcPg); | |
488 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff); | |
489 } | |
490 sqlite3PagerUnref(pSrcPg); | |
491 } | 541 } |
492 if( rc==SQLITE_OK ){ | |
493 rc = backupTruncateFile(pFile, iSize); | |
494 } | |
495 | |
496 /* Sync the database file to disk. */ | |
497 if( rc==SQLITE_OK ){ | |
498 rc = sqlite3PagerSync(pDestPager); | |
499 } | |
500 }else{ | |
501 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); | |
502 } | |
503 | |
504 /* Finish committing the transaction to the destination database. */ | |
505 if( SQLITE_OK==rc | |
506 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0)) | |
507 ){ | |
508 rc = SQLITE_DONE; | |
509 } | 542 } |
510 } | 543 } |
511 | 544 |
512 /* If bCloseTrans is true, then this function opened a read transaction | 545 /* If bCloseTrans is true, then this function opened a read transaction |
513 ** on the source database. Close the read transaction here. There is | 546 ** on the source database. Close the read transaction here. There is |
514 ** no need to check the return values of the btree methods here, as | 547 ** no need to check the return values of the btree methods here, as |
515 ** "committing" a read-only transaction cannot fail. | 548 ** "committing" a read-only transaction cannot fail. |
516 */ | 549 */ |
517 if( bCloseTrans ){ | 550 if( bCloseTrans ){ |
518 TESTONLY( int rc2 ); | 551 TESTONLY( int rc2 ); |
(...skipping 13 matching lines...) Expand all Loading... |
532 sqlite3BtreeLeave(p->pSrc); | 565 sqlite3BtreeLeave(p->pSrc); |
533 sqlite3_mutex_leave(p->pSrcDb->mutex); | 566 sqlite3_mutex_leave(p->pSrcDb->mutex); |
534 return rc; | 567 return rc; |
535 } | 568 } |
536 | 569 |
537 /* | 570 /* |
538 ** Release all resources associated with an sqlite3_backup* handle. | 571 ** Release all resources associated with an sqlite3_backup* handle. |
539 */ | 572 */ |
540 int sqlite3_backup_finish(sqlite3_backup *p){ | 573 int sqlite3_backup_finish(sqlite3_backup *p){ |
541 sqlite3_backup **pp; /* Ptr to head of pagers backup list */ | 574 sqlite3_backup **pp; /* Ptr to head of pagers backup list */ |
542 sqlite3_mutex *mutex; /* Mutex to protect source database */ | 575 sqlite3 *pSrcDb; /* Source database connection */ |
543 int rc; /* Value to return */ | 576 int rc; /* Value to return */ |
544 | 577 |
545 /* Enter the mutexes */ | 578 /* Enter the mutexes */ |
546 if( p==0 ) return SQLITE_OK; | 579 if( p==0 ) return SQLITE_OK; |
547 sqlite3_mutex_enter(p->pSrcDb->mutex); | 580 pSrcDb = p->pSrcDb; |
| 581 sqlite3_mutex_enter(pSrcDb->mutex); |
548 sqlite3BtreeEnter(p->pSrc); | 582 sqlite3BtreeEnter(p->pSrc); |
549 mutex = p->pSrcDb->mutex; | |
550 if( p->pDestDb ){ | 583 if( p->pDestDb ){ |
551 sqlite3_mutex_enter(p->pDestDb->mutex); | 584 sqlite3_mutex_enter(p->pDestDb->mutex); |
552 } | 585 } |
553 | 586 |
554 /* Detach this backup from the source pager. */ | 587 /* Detach this backup from the source pager. */ |
555 if( p->pDestDb ){ | 588 if( p->pDestDb ){ |
556 p->pSrc->nBackup--; | 589 p->pSrc->nBackup--; |
557 } | 590 } |
558 if( p->isAttached ){ | 591 if( p->isAttached ){ |
559 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); | 592 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); |
560 while( *pp!=p ){ | 593 while( *pp!=p ){ |
561 pp = &(*pp)->pNext; | 594 pp = &(*pp)->pNext; |
562 } | 595 } |
563 *pp = p->pNext; | 596 *pp = p->pNext; |
564 } | 597 } |
565 | 598 |
566 /* If a transaction is still open on the Btree, roll it back. */ | 599 /* If a transaction is still open on the Btree, roll it back. */ |
567 sqlite3BtreeRollback(p->pDest); | 600 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0); |
568 | 601 |
569 /* Set the error code of the destination database handle. */ | 602 /* Set the error code of the destination database handle. */ |
570 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc; | 603 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc; |
571 sqlite3Error(p->pDestDb, rc, 0); | 604 if( p->pDestDb ){ |
| 605 sqlite3Error(p->pDestDb, rc); |
572 | 606 |
573 /* Exit the mutexes and free the backup context structure. */ | 607 /* Exit the mutexes and free the backup context structure. */ |
574 if( p->pDestDb ){ | 608 sqlite3LeaveMutexAndCloseZombie(p->pDestDb); |
575 sqlite3_mutex_leave(p->pDestDb->mutex); | |
576 } | 609 } |
577 sqlite3BtreeLeave(p->pSrc); | 610 sqlite3BtreeLeave(p->pSrc); |
578 if( p->pDestDb ){ | 611 if( p->pDestDb ){ |
579 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a | 612 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a |
580 ** call to sqlite3_backup_init() and is destroyed by a call to | 613 ** call to sqlite3_backup_init() and is destroyed by a call to |
581 ** sqlite3_backup_finish(). */ | 614 ** sqlite3_backup_finish(). */ |
582 sqlite3_free(p); | 615 sqlite3_free(p); |
583 } | 616 } |
584 sqlite3_mutex_leave(mutex); | 617 sqlite3LeaveMutexAndCloseZombie(pSrcDb); |
585 return rc; | 618 return rc; |
586 } | 619 } |
587 | 620 |
588 /* | 621 /* |
589 ** Return the number of pages still to be backed up as of the most recent | 622 ** Return the number of pages still to be backed up as of the most recent |
590 ** call to sqlite3_backup_step(). | 623 ** call to sqlite3_backup_step(). |
591 */ | 624 */ |
592 int sqlite3_backup_remaining(sqlite3_backup *p){ | 625 int sqlite3_backup_remaining(sqlite3_backup *p){ |
593 return p->nRemaining; | 626 return p->nRemaining; |
594 } | 627 } |
(...skipping 23 matching lines...) Expand all Loading... |
618 for(p=pBackup; p; p=p->pNext){ | 651 for(p=pBackup; p; p=p->pNext){ |
619 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); | 652 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); |
620 if( !isFatalError(p->rc) && iPage<p->iNext ){ | 653 if( !isFatalError(p->rc) && iPage<p->iNext ){ |
621 /* The backup process p has already copied page iPage. But now it | 654 /* The backup process p has already copied page iPage. But now it |
622 ** has been modified by a transaction on the source pager. Copy | 655 ** has been modified by a transaction on the source pager. Copy |
623 ** the new data into the backup. | 656 ** the new data into the backup. |
624 */ | 657 */ |
625 int rc; | 658 int rc; |
626 assert( p->pDestDb ); | 659 assert( p->pDestDb ); |
627 sqlite3_mutex_enter(p->pDestDb->mutex); | 660 sqlite3_mutex_enter(p->pDestDb->mutex); |
628 rc = backupOnePage(p, iPage, aData); | 661 rc = backupOnePage(p, iPage, aData, 1); |
629 sqlite3_mutex_leave(p->pDestDb->mutex); | 662 sqlite3_mutex_leave(p->pDestDb->mutex); |
630 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED ); | 663 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED ); |
631 if( rc!=SQLITE_OK ){ | 664 if( rc!=SQLITE_OK ){ |
632 p->rc = rc; | 665 p->rc = rc; |
633 } | 666 } |
634 } | 667 } |
635 } | 668 } |
636 } | 669 } |
637 | 670 |
638 /* | 671 /* |
(...skipping 19 matching lines...) Expand all Loading... |
658 /* | 691 /* |
659 ** Copy the complete content of pBtFrom into pBtTo. A transaction | 692 ** Copy the complete content of pBtFrom into pBtTo. A transaction |
660 ** must be active for both files. | 693 ** must be active for both files. |
661 ** | 694 ** |
662 ** The size of file pTo may be reduced by this operation. If anything | 695 ** The size of file pTo may be reduced by this operation. If anything |
663 ** goes wrong, the transaction on pTo is rolled back. If successful, the | 696 ** goes wrong, the transaction on pTo is rolled back. If successful, the |
664 ** transaction is committed before returning. | 697 ** transaction is committed before returning. |
665 */ | 698 */ |
666 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ | 699 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
667 int rc; | 700 int rc; |
| 701 sqlite3_file *pFd; /* File descriptor for database pTo */ |
668 sqlite3_backup b; | 702 sqlite3_backup b; |
669 sqlite3BtreeEnter(pTo); | 703 sqlite3BtreeEnter(pTo); |
670 sqlite3BtreeEnter(pFrom); | 704 sqlite3BtreeEnter(pFrom); |
671 | 705 |
| 706 assert( sqlite3BtreeIsInTrans(pTo) ); |
| 707 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo)); |
| 708 if( pFd->pMethods ){ |
| 709 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom); |
| 710 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte); |
| 711 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; |
| 712 if( rc ) goto copy_finished; |
| 713 } |
| 714 |
672 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set | 715 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set |
673 ** to 0. This is used by the implementations of sqlite3_backup_step() | 716 ** to 0. This is used by the implementations of sqlite3_backup_step() |
674 ** and sqlite3_backup_finish() to detect that they are being called | 717 ** and sqlite3_backup_finish() to detect that they are being called |
675 ** from this function, not directly by the user. | 718 ** from this function, not directly by the user. |
676 */ | 719 */ |
677 memset(&b, 0, sizeof(b)); | 720 memset(&b, 0, sizeof(b)); |
678 b.pSrcDb = pFrom->db; | 721 b.pSrcDb = pFrom->db; |
679 b.pSrc = pFrom; | 722 b.pSrc = pFrom; |
680 b.pDest = pTo; | 723 b.pDest = pTo; |
681 b.iNext = 1; | 724 b.iNext = 1; |
682 | 725 |
683 /* 0x7FFFFFFF is the hard limit for the number of pages in a database | 726 /* 0x7FFFFFFF is the hard limit for the number of pages in a database |
684 ** file. By passing this as the number of pages to copy to | 727 ** file. By passing this as the number of pages to copy to |
685 ** sqlite3_backup_step(), we can guarantee that the copy finishes | 728 ** sqlite3_backup_step(), we can guarantee that the copy finishes |
686 ** within a single call (unless an error occurs). The assert() statement | 729 ** within a single call (unless an error occurs). The assert() statement |
687 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE | 730 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE |
688 ** or an error code. | 731 ** or an error code. |
689 */ | 732 */ |
690 sqlite3_backup_step(&b, 0x7FFFFFFF); | 733 sqlite3_backup_step(&b, 0x7FFFFFFF); |
691 assert( b.rc!=SQLITE_OK ); | 734 assert( b.rc!=SQLITE_OK ); |
692 rc = sqlite3_backup_finish(&b); | 735 rc = sqlite3_backup_finish(&b); |
693 if( rc==SQLITE_OK ){ | 736 if( rc==SQLITE_OK ){ |
694 pTo->pBt->pageSizeFixed = 0; | 737 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED; |
| 738 }else{ |
| 739 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest)); |
695 } | 740 } |
696 | 741 |
| 742 assert( sqlite3BtreeIsInTrans(pTo)==0 ); |
| 743 copy_finished: |
697 sqlite3BtreeLeave(pFrom); | 744 sqlite3BtreeLeave(pFrom); |
698 sqlite3BtreeLeave(pTo); | 745 sqlite3BtreeLeave(pTo); |
699 return rc; | 746 return rc; |
700 } | 747 } |
701 #endif /* SQLITE_OMIT_VACUUM */ | 748 #endif /* SQLITE_OMIT_VACUUM */ |
OLD | NEW |