OLD | NEW |
| (Empty) |
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 /* | |
19 ** Structure allocated for each backup operation. | |
20 */ | |
21 struct sqlite3_backup { | |
22 sqlite3* pDestDb; /* Destination database handle */ | |
23 Btree *pDest; /* Destination b-tree file */ | |
24 u32 iDestSchema; /* Original schema cookie in destination */ | |
25 int bDestLocked; /* True once a write-transaction is open on pDest */ | |
26 | |
27 Pgno iNext; /* Page number of the next source page to copy */ | |
28 sqlite3* pSrcDb; /* Source database handle */ | |
29 Btree *pSrc; /* Source b-tree file */ | |
30 | |
31 int rc; /* Backup process error code */ | |
32 | |
33 /* These two variables are set by every call to backup_step(). They are | |
34 ** read by calls to backup_remaining() and backup_pagecount(). | |
35 */ | |
36 Pgno nRemaining; /* Number of pages left to copy */ | |
37 Pgno nPagecount; /* Total number of pages to copy */ | |
38 | |
39 int isAttached; /* True once backup has been registered with pager */ | |
40 sqlite3_backup *pNext; /* Next backup associated with source pager */ | |
41 }; | |
42 | |
43 /* | |
44 ** THREAD SAFETY NOTES: | |
45 ** | |
46 ** Once it has been created using backup_init(), a single sqlite3_backup | |
47 ** structure may be accessed via two groups of thread-safe entry points: | |
48 ** | |
49 ** * Via the sqlite3_backup_XXX() API function backup_step() and | |
50 ** backup_finish(). Both these functions obtain the source database | |
51 ** handle mutex and the mutex associated with the source BtShared | |
52 ** structure, in that order. | |
53 ** | |
54 ** * Via the BackupUpdate() and BackupRestart() functions, which are | |
55 ** invoked by the pager layer to report various state changes in | |
56 ** the page cache associated with the source database. The mutex | |
57 ** associated with the source database BtShared structure will always | |
58 ** be held when either of these functions are invoked. | |
59 ** | |
60 ** The other sqlite3_backup_XXX() API functions, backup_remaining() and | |
61 ** backup_pagecount() are not thread-safe functions. If they are called | |
62 ** while some other thread is calling backup_step() or backup_finish(), | |
63 ** the values returned may be invalid. There is no way for a call to | |
64 ** BackupUpdate() or BackupRestart() to interfere with backup_remaining() | |
65 ** or backup_pagecount(). | |
66 ** | |
67 ** Depending on the SQLite configuration, the database handles and/or | |
68 ** the Btree objects may have their own mutexes that require locking. | |
69 ** Non-sharable Btrees (in-memory databases for example), do not have | |
70 ** associated mutexes. | |
71 */ | |
72 | |
73 /* | |
74 ** Return a pointer corresponding to database zDb (i.e. "main", "temp") | |
75 ** in connection handle pDb. If such a database cannot be found, return | |
76 ** a NULL pointer and write an error message to pErrorDb. | |
77 ** | |
78 ** If the "temp" database is requested, it may need to be opened by this | |
79 ** function. If an error occurs while doing so, return 0 and write an | |
80 ** error message to pErrorDb. | |
81 */ | |
82 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){ | |
83 int i = sqlite3FindDbName(pDb, zDb); | |
84 | |
85 if( i==1 ){ | |
86 Parse *pParse; | |
87 int rc = 0; | |
88 pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse)); | |
89 if( pParse==0 ){ | |
90 sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory"); | |
91 rc = SQLITE_NOMEM; | |
92 }else{ | |
93 pParse->db = pDb; | |
94 if( sqlite3OpenTempDatabase(pParse) ){ | |
95 sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg); | |
96 rc = SQLITE_ERROR; | |
97 } | |
98 sqlite3DbFree(pErrorDb, pParse->zErrMsg); | |
99 sqlite3ParserReset(pParse); | |
100 sqlite3StackFree(pErrorDb, pParse); | |
101 } | |
102 if( rc ){ | |
103 return 0; | |
104 } | |
105 } | |
106 | |
107 if( i<0 ){ | |
108 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb); | |
109 return 0; | |
110 } | |
111 | |
112 return pDb->aDb[i].pBt; | |
113 } | |
114 | |
115 /* | |
116 ** Attempt to set the page size of the destination to match the page size | |
117 ** of the source. | |
118 */ | |
119 static int setDestPgsz(sqlite3_backup *p){ | |
120 int rc; | |
121 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); | |
122 return rc; | |
123 } | |
124 | |
125 /* | |
126 ** Check that there is no open read-transaction on the b-tree passed as the | |
127 ** second argument. If there is not, return SQLITE_OK. Otherwise, if there | |
128 ** is an open read-transaction, return SQLITE_ERROR and leave an error | |
129 ** message in database handle db. | |
130 */ | |
131 static int checkReadTransaction(sqlite3 *db, Btree *p){ | |
132 if( sqlite3BtreeIsInReadTrans(p) ){ | |
133 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use"); | |
134 return SQLITE_ERROR; | |
135 } | |
136 return SQLITE_OK; | |
137 } | |
138 | |
139 /* | |
140 ** Create an sqlite3_backup process to copy the contents of zSrcDb from | |
141 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return | |
142 ** a pointer to the new sqlite3_backup object. | |
143 ** | |
144 ** If an error occurs, NULL is returned and an error code and error message | |
145 ** stored in database handle pDestDb. | |
146 */ | |
147 sqlite3_backup *sqlite3_backup_init( | |
148 sqlite3* pDestDb, /* Database to write to */ | |
149 const char *zDestDb, /* Name of database within pDestDb */ | |
150 sqlite3* pSrcDb, /* Database connection to read from */ | |
151 const char *zSrcDb /* Name of database within pSrcDb */ | |
152 ){ | |
153 sqlite3_backup *p; /* Value to return */ | |
154 | |
155 #ifdef SQLITE_ENABLE_API_ARMOR | |
156 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){ | |
157 (void)SQLITE_MISUSE_BKPT; | |
158 return 0; | |
159 } | |
160 #endif | |
161 | |
162 /* Lock the source database handle. The destination database | |
163 ** handle is not locked in this routine, but it is locked in | |
164 ** sqlite3_backup_step(). The user is required to ensure that no | |
165 ** other thread accesses the destination handle for the duration | |
166 ** of the backup operation. Any attempt to use the destination | |
167 ** database connection while a backup is in progress may cause | |
168 ** a malfunction or a deadlock. | |
169 */ | |
170 sqlite3_mutex_enter(pSrcDb->mutex); | |
171 sqlite3_mutex_enter(pDestDb->mutex); | |
172 | |
173 if( pSrcDb==pDestDb ){ | |
174 sqlite3ErrorWithMsg( | |
175 pDestDb, SQLITE_ERROR, "source and destination must be distinct" | |
176 ); | |
177 p = 0; | |
178 }else { | |
179 /* Allocate space for a new sqlite3_backup object... | |
180 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a | |
181 ** call to sqlite3_backup_init() and is destroyed by a call to | |
182 ** sqlite3_backup_finish(). */ | |
183 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup)); | |
184 if( !p ){ | |
185 sqlite3Error(pDestDb, SQLITE_NOMEM); | |
186 } | |
187 } | |
188 | |
189 /* If the allocation succeeded, populate the new object. */ | |
190 if( p ){ | |
191 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb); | |
192 p->pDest = findBtree(pDestDb, pDestDb, zDestDb); | |
193 p->pDestDb = pDestDb; | |
194 p->pSrcDb = pSrcDb; | |
195 p->iNext = 1; | |
196 p->isAttached = 0; | |
197 | |
198 if( 0==p->pSrc || 0==p->pDest | |
199 || setDestPgsz(p)==SQLITE_NOMEM | |
200 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK | |
201 ){ | |
202 /* One (or both) of the named databases did not exist or an OOM | |
203 ** error was hit. Or there is a transaction open on the destination | |
204 ** database. The error has already been written into the pDestDb | |
205 ** handle. All that is left to do here is free the sqlite3_backup | |
206 ** structure. */ | |
207 sqlite3_free(p); | |
208 p = 0; | |
209 } | |
210 } | |
211 if( p ){ | |
212 p->pSrc->nBackup++; | |
213 } | |
214 | |
215 sqlite3_mutex_leave(pDestDb->mutex); | |
216 sqlite3_mutex_leave(pSrcDb->mutex); | |
217 return p; | |
218 } | |
219 | |
220 /* | |
221 ** Argument rc is an SQLite error code. Return true if this error is | |
222 ** considered fatal if encountered during a backup operation. All errors | |
223 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED. | |
224 */ | |
225 static int isFatalError(int rc){ | |
226 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED)); | |
227 } | |
228 | |
229 /* | |
230 ** Parameter zSrcData points to a buffer containing the data for | |
231 ** page iSrcPg from the source database. Copy this data into the | |
232 ** destination database. | |
233 */ | |
234 static int backupOnePage( | |
235 sqlite3_backup *p, /* Backup handle */ | |
236 Pgno iSrcPg, /* Source database page to backup */ | |
237 const u8 *zSrcData, /* Source database page data */ | |
238 int bUpdate /* True for an update, false otherwise */ | |
239 ){ | |
240 Pager * const pDestPager = sqlite3BtreePager(p->pDest); | |
241 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc); | |
242 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest); | |
243 const int nCopy = MIN(nSrcPgsz, nDestPgsz); | |
244 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz; | |
245 #ifdef SQLITE_HAS_CODEC | |
246 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is | |
247 ** guaranteed that the shared-mutex is held by this thread, handle | |
248 ** p->pSrc may not actually be the owner. */ | |
249 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc); | |
250 int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest); | |
251 #endif | |
252 int rc = SQLITE_OK; | |
253 i64 iOff; | |
254 | |
255 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 ); | |
256 assert( p->bDestLocked ); | |
257 assert( !isFatalError(p->rc) ); | |
258 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ); | |
259 assert( zSrcData ); | |
260 | |
261 /* Catch the case where the destination is an in-memory database and the | |
262 ** page sizes of the source and destination differ. | |
263 */ | |
264 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){ | |
265 rc = SQLITE_READONLY; | |
266 } | |
267 | |
268 #ifdef SQLITE_HAS_CODEC | |
269 /* Backup is not possible if the page size of the destination is changing | |
270 ** and a codec is in use. | |
271 */ | |
272 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){ | |
273 rc = SQLITE_READONLY; | |
274 } | |
275 | |
276 /* Backup is not possible if the number of bytes of reserve space differ | |
277 ** between source and destination. If there is a difference, try to | |
278 ** fix the destination to agree with the source. If that is not possible, | |
279 ** then the backup cannot proceed. | |
280 */ | |
281 if( nSrcReserve!=nDestReserve ){ | |
282 u32 newPgsz = nSrcPgsz; | |
283 rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve); | |
284 if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY; | |
285 } | |
286 #endif | |
287 | |
288 /* This loop runs once for each destination page spanned by the source | |
289 ** page. For each iteration, variable iOff is set to the byte offset | |
290 ** of the destination page. | |
291 */ | |
292 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){ | |
293 DbPage *pDestPg = 0; | |
294 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1; | |
295 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue; | |
296 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0)) | |
297 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg)) | |
298 ){ | |
299 const u8 *zIn = &zSrcData[iOff%nSrcPgsz]; | |
300 u8 *zDestData = sqlite3PagerGetData(pDestPg); | |
301 u8 *zOut = &zDestData[iOff%nDestPgsz]; | |
302 | |
303 /* Copy the data from the source page into the destination page. | |
304 ** Then clear the Btree layer MemPage.isInit flag. Both this module | |
305 ** and the pager code use this trick (clearing the first byte | |
306 ** of the page 'extra' space to invalidate the Btree layers | |
307 ** cached parse of the page). MemPage.isInit is marked | |
308 ** "MUST BE FIRST" for this purpose. | |
309 */ | |
310 memcpy(zOut, zIn, nCopy); | |
311 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0; | |
312 if( iOff==0 && bUpdate==0 ){ | |
313 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc)); | |
314 } | |
315 } | |
316 sqlite3PagerUnref(pDestPg); | |
317 } | |
318 | |
319 return rc; | |
320 } | |
321 | |
322 /* | |
323 ** If pFile is currently larger than iSize bytes, then truncate it to | |
324 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then | |
325 ** this function is a no-op. | |
326 ** | |
327 ** Return SQLITE_OK if everything is successful, or an SQLite error | |
328 ** code if an error occurs. | |
329 */ | |
330 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){ | |
331 i64 iCurrent; | |
332 int rc = sqlite3OsFileSize(pFile, &iCurrent); | |
333 if( rc==SQLITE_OK && iCurrent>iSize ){ | |
334 rc = sqlite3OsTruncate(pFile, iSize); | |
335 } | |
336 return rc; | |
337 } | |
338 | |
339 /* | |
340 ** Register this backup object with the associated source pager for | |
341 ** callbacks when pages are changed or the cache invalidated. | |
342 */ | |
343 static void attachBackupObject(sqlite3_backup *p){ | |
344 sqlite3_backup **pp; | |
345 assert( sqlite3BtreeHoldsMutex(p->pSrc) ); | |
346 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); | |
347 p->pNext = *pp; | |
348 *pp = p; | |
349 p->isAttached = 1; | |
350 } | |
351 | |
352 /* | |
353 ** Copy nPage pages from the source b-tree to the destination. | |
354 */ | |
355 int sqlite3_backup_step(sqlite3_backup *p, int nPage){ | |
356 int rc; | |
357 int destMode; /* Destination journal mode */ | |
358 int pgszSrc = 0; /* Source page size */ | |
359 int pgszDest = 0; /* Destination page size */ | |
360 | |
361 #ifdef SQLITE_ENABLE_API_ARMOR | |
362 if( p==0 ) return SQLITE_MISUSE_BKPT; | |
363 #endif | |
364 sqlite3_mutex_enter(p->pSrcDb->mutex); | |
365 sqlite3BtreeEnter(p->pSrc); | |
366 if( p->pDestDb ){ | |
367 sqlite3_mutex_enter(p->pDestDb->mutex); | |
368 } | |
369 | |
370 rc = p->rc; | |
371 if( !isFatalError(rc) ){ | |
372 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */ | |
373 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */ | |
374 int ii; /* Iterator variable */ | |
375 int nSrcPage = -1; /* Size of source db in pages */ | |
376 int bCloseTrans = 0; /* True if src db requires unlocking */ | |
377 | |
378 /* If the source pager is currently in a write-transaction, return | |
379 ** SQLITE_BUSY immediately. | |
380 */ | |
381 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){ | |
382 rc = SQLITE_BUSY; | |
383 }else{ | |
384 rc = SQLITE_OK; | |
385 } | |
386 | |
387 /* Lock the destination database, if it is not locked already. */ | |
388 if( SQLITE_OK==rc && p->bDestLocked==0 | |
389 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) | |
390 ){ | |
391 p->bDestLocked = 1; | |
392 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema); | |
393 } | |
394 | |
395 /* If there is no open read-transaction on the source database, open | |
396 ** one now. If a transaction is opened here, then it will be closed | |
397 ** before this function exits. | |
398 */ | |
399 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){ | |
400 rc = sqlite3BtreeBeginTrans(p->pSrc, 0); | |
401 bCloseTrans = 1; | |
402 } | |
403 | |
404 /* Do not allow backup if the destination database is in WAL mode | |
405 ** and the page sizes are different between source and destination */ | |
406 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc); | |
407 pgszDest = sqlite3BtreeGetPageSize(p->pDest); | |
408 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest)); | |
409 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){ | |
410 rc = SQLITE_READONLY; | |
411 } | |
412 | |
413 /* Now that there is a read-lock on the source database, query the | |
414 ** source pager for the number of pages in the database. | |
415 */ | |
416 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc); | |
417 assert( nSrcPage>=0 ); | |
418 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){ | |
419 const Pgno iSrcPg = p->iNext; /* Source page number */ | |
420 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){ | |
421 DbPage *pSrcPg; /* Source page object */ | |
422 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY); | |
423 if( rc==SQLITE_OK ){ | |
424 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0); | |
425 sqlite3PagerUnref(pSrcPg); | |
426 } | |
427 } | |
428 p->iNext++; | |
429 } | |
430 if( rc==SQLITE_OK ){ | |
431 p->nPagecount = nSrcPage; | |
432 p->nRemaining = nSrcPage+1-p->iNext; | |
433 if( p->iNext>(Pgno)nSrcPage ){ | |
434 rc = SQLITE_DONE; | |
435 }else if( !p->isAttached ){ | |
436 attachBackupObject(p); | |
437 } | |
438 } | |
439 | |
440 /* Update the schema version field in the destination database. This | |
441 ** is to make sure that the schema-version really does change in | |
442 ** the case where the source and destination databases have the | |
443 ** same schema version. | |
444 */ | |
445 if( rc==SQLITE_DONE ){ | |
446 if( nSrcPage==0 ){ | |
447 rc = sqlite3BtreeNewDb(p->pDest); | |
448 nSrcPage = 1; | |
449 } | |
450 if( rc==SQLITE_OK || rc==SQLITE_DONE ){ | |
451 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1); | |
452 } | |
453 if( rc==SQLITE_OK ){ | |
454 if( p->pDestDb ){ | |
455 sqlite3ResetAllSchemasOfConnection(p->pDestDb); | |
456 } | |
457 if( destMode==PAGER_JOURNALMODE_WAL ){ | |
458 rc = sqlite3BtreeSetVersion(p->pDest, 2); | |
459 } | |
460 } | |
461 if( rc==SQLITE_OK ){ | |
462 int nDestTruncate; | |
463 /* Set nDestTruncate to the final number of pages in the destination | |
464 ** database. The complication here is that the destination page | |
465 ** size may be different to the source page size. | |
466 ** | |
467 ** If the source page size is smaller than the destination page size, | |
468 ** round up. In this case the call to sqlite3OsTruncate() below will | |
469 ** fix the size of the file. However it is important to call | |
470 ** sqlite3PagerTruncateImage() here so that any pages in the | |
471 ** destination file that lie beyond the nDestTruncate page mark are | |
472 ** journalled by PagerCommitPhaseOne() before they are destroyed | |
473 ** by the file truncation. | |
474 */ | |
475 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) ); | |
476 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) ); | |
477 if( pgszSrc<pgszDest ){ | |
478 int ratio = pgszDest/pgszSrc; | |
479 nDestTruncate = (nSrcPage+ratio-1)/ratio; | |
480 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){ | |
481 nDestTruncate--; | |
482 } | |
483 }else{ | |
484 nDestTruncate = nSrcPage * (pgszSrc/pgszDest); | |
485 } | |
486 assert( nDestTruncate>0 ); | |
487 | |
488 if( pgszSrc<pgszDest ){ | |
489 /* If the source page-size is smaller than the destination page-size, | |
490 ** two extra things may need to happen: | |
491 ** | |
492 ** * The destination may need to be truncated, and | |
493 ** | |
494 ** * Data stored on the pages immediately following the | |
495 ** pending-byte page in the source database may need to be | |
496 ** copied into the destination database. | |
497 */ | |
498 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage; | |
499 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager); | |
500 Pgno iPg; | |
501 int nDstPage; | |
502 i64 iOff; | |
503 i64 iEnd; | |
504 | |
505 assert( pFile ); | |
506 assert( nDestTruncate==0 | |
507 || (i64)nDestTruncate*(i64)pgszDest >= iSize || ( | |
508 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) | |
509 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest | |
510 )); | |
511 | |
512 /* This block ensures that all data required to recreate the original | |
513 ** database has been stored in the journal for pDestPager and the | |
514 ** journal synced to disk. So at this point we may safely modify | |
515 ** the database file in any way, knowing that if a power failure | |
516 ** occurs, the original database will be reconstructed from the | |
517 ** journal file. */ | |
518 sqlite3PagerPagecount(pDestPager, &nDstPage); | |
519 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){ | |
520 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){ | |
521 DbPage *pPg; | |
522 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0); | |
523 if( rc==SQLITE_OK ){ | |
524 rc = sqlite3PagerWrite(pPg); | |
525 sqlite3PagerUnref(pPg); | |
526 } | |
527 } | |
528 } | |
529 if( rc==SQLITE_OK ){ | |
530 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1); | |
531 } | |
532 | |
533 /* Write the extra pages and truncate the database file as required */ | |
534 iEnd = MIN(PENDING_BYTE + pgszDest, iSize); | |
535 for( | |
536 iOff=PENDING_BYTE+pgszSrc; | |
537 rc==SQLITE_OK && iOff<iEnd; | |
538 iOff+=pgszSrc | |
539 ){ | |
540 PgHdr *pSrcPg = 0; | |
541 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1); | |
542 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0); | |
543 if( rc==SQLITE_OK ){ | |
544 u8 *zData = sqlite3PagerGetData(pSrcPg); | |
545 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff); | |
546 } | |
547 sqlite3PagerUnref(pSrcPg); | |
548 } | |
549 if( rc==SQLITE_OK ){ | |
550 rc = backupTruncateFile(pFile, iSize); | |
551 } | |
552 | |
553 /* Sync the database file to disk. */ | |
554 if( rc==SQLITE_OK ){ | |
555 rc = sqlite3PagerSync(pDestPager, 0); | |
556 } | |
557 }else{ | |
558 sqlite3PagerTruncateImage(pDestPager, nDestTruncate); | |
559 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); | |
560 } | |
561 | |
562 /* Finish committing the transaction to the destination database. */ | |
563 if( SQLITE_OK==rc | |
564 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0)) | |
565 ){ | |
566 rc = SQLITE_DONE; | |
567 } | |
568 } | |
569 } | |
570 | |
571 /* If bCloseTrans is true, then this function opened a read transaction | |
572 ** on the source database. Close the read transaction here. There is | |
573 ** no need to check the return values of the btree methods here, as | |
574 ** "committing" a read-only transaction cannot fail. | |
575 */ | |
576 if( bCloseTrans ){ | |
577 TESTONLY( int rc2 ); | |
578 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0); | |
579 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0); | |
580 assert( rc2==SQLITE_OK ); | |
581 } | |
582 | |
583 if( rc==SQLITE_IOERR_NOMEM ){ | |
584 rc = SQLITE_NOMEM; | |
585 } | |
586 p->rc = rc; | |
587 } | |
588 if( p->pDestDb ){ | |
589 sqlite3_mutex_leave(p->pDestDb->mutex); | |
590 } | |
591 sqlite3BtreeLeave(p->pSrc); | |
592 sqlite3_mutex_leave(p->pSrcDb->mutex); | |
593 return rc; | |
594 } | |
595 | |
596 /* | |
597 ** Release all resources associated with an sqlite3_backup* handle. | |
598 */ | |
599 int sqlite3_backup_finish(sqlite3_backup *p){ | |
600 sqlite3_backup **pp; /* Ptr to head of pagers backup list */ | |
601 sqlite3 *pSrcDb; /* Source database connection */ | |
602 int rc; /* Value to return */ | |
603 | |
604 /* Enter the mutexes */ | |
605 if( p==0 ) return SQLITE_OK; | |
606 pSrcDb = p->pSrcDb; | |
607 sqlite3_mutex_enter(pSrcDb->mutex); | |
608 sqlite3BtreeEnter(p->pSrc); | |
609 if( p->pDestDb ){ | |
610 sqlite3_mutex_enter(p->pDestDb->mutex); | |
611 } | |
612 | |
613 /* Detach this backup from the source pager. */ | |
614 if( p->pDestDb ){ | |
615 p->pSrc->nBackup--; | |
616 } | |
617 if( p->isAttached ){ | |
618 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); | |
619 while( *pp!=p ){ | |
620 pp = &(*pp)->pNext; | |
621 } | |
622 *pp = p->pNext; | |
623 } | |
624 | |
625 /* If a transaction is still open on the Btree, roll it back. */ | |
626 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0); | |
627 | |
628 /* Set the error code of the destination database handle. */ | |
629 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc; | |
630 if( p->pDestDb ){ | |
631 sqlite3Error(p->pDestDb, rc); | |
632 | |
633 /* Exit the mutexes and free the backup context structure. */ | |
634 sqlite3LeaveMutexAndCloseZombie(p->pDestDb); | |
635 } | |
636 sqlite3BtreeLeave(p->pSrc); | |
637 if( p->pDestDb ){ | |
638 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a | |
639 ** call to sqlite3_backup_init() and is destroyed by a call to | |
640 ** sqlite3_backup_finish(). */ | |
641 sqlite3_free(p); | |
642 } | |
643 sqlite3LeaveMutexAndCloseZombie(pSrcDb); | |
644 return rc; | |
645 } | |
646 | |
647 /* | |
648 ** Return the number of pages still to be backed up as of the most recent | |
649 ** call to sqlite3_backup_step(). | |
650 */ | |
651 int sqlite3_backup_remaining(sqlite3_backup *p){ | |
652 #ifdef SQLITE_ENABLE_API_ARMOR | |
653 if( p==0 ){ | |
654 (void)SQLITE_MISUSE_BKPT; | |
655 return 0; | |
656 } | |
657 #endif | |
658 return p->nRemaining; | |
659 } | |
660 | |
661 /* | |
662 ** Return the total number of pages in the source database as of the most | |
663 ** recent call to sqlite3_backup_step(). | |
664 */ | |
665 int sqlite3_backup_pagecount(sqlite3_backup *p){ | |
666 #ifdef SQLITE_ENABLE_API_ARMOR | |
667 if( p==0 ){ | |
668 (void)SQLITE_MISUSE_BKPT; | |
669 return 0; | |
670 } | |
671 #endif | |
672 return p->nPagecount; | |
673 } | |
674 | |
675 /* | |
676 ** This function is called after the contents of page iPage of the | |
677 ** source database have been modified. If page iPage has already been | |
678 ** copied into the destination database, then the data written to the | |
679 ** destination is now invalidated. The destination copy of iPage needs | |
680 ** to be updated with the new data before the backup operation is | |
681 ** complete. | |
682 ** | |
683 ** It is assumed that the mutex associated with the BtShared object | |
684 ** corresponding to the source database is held when this function is | |
685 ** called. | |
686 */ | |
687 static SQLITE_NOINLINE void backupUpdate( | |
688 sqlite3_backup *p, | |
689 Pgno iPage, | |
690 const u8 *aData | |
691 ){ | |
692 assert( p!=0 ); | |
693 do{ | |
694 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); | |
695 if( !isFatalError(p->rc) && iPage<p->iNext ){ | |
696 /* The backup process p has already copied page iPage. But now it | |
697 ** has been modified by a transaction on the source pager. Copy | |
698 ** the new data into the backup. | |
699 */ | |
700 int rc; | |
701 assert( p->pDestDb ); | |
702 sqlite3_mutex_enter(p->pDestDb->mutex); | |
703 rc = backupOnePage(p, iPage, aData, 1); | |
704 sqlite3_mutex_leave(p->pDestDb->mutex); | |
705 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED ); | |
706 if( rc!=SQLITE_OK ){ | |
707 p->rc = rc; | |
708 } | |
709 } | |
710 }while( (p = p->pNext)!=0 ); | |
711 } | |
712 void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){ | |
713 if( pBackup ) backupUpdate(pBackup, iPage, aData); | |
714 } | |
715 | |
716 /* | |
717 ** Restart the backup process. This is called when the pager layer | |
718 ** detects that the database has been modified by an external database | |
719 ** connection. In this case there is no way of knowing which of the | |
720 ** pages that have been copied into the destination database are still | |
721 ** valid and which are not, so the entire process needs to be restarted. | |
722 ** | |
723 ** It is assumed that the mutex associated with the BtShared object | |
724 ** corresponding to the source database is held when this function is | |
725 ** called. | |
726 */ | |
727 void sqlite3BackupRestart(sqlite3_backup *pBackup){ | |
728 sqlite3_backup *p; /* Iterator variable */ | |
729 for(p=pBackup; p; p=p->pNext){ | |
730 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); | |
731 p->iNext = 1; | |
732 } | |
733 } | |
734 | |
735 #ifndef SQLITE_OMIT_VACUUM | |
736 /* | |
737 ** Copy the complete content of pBtFrom into pBtTo. A transaction | |
738 ** must be active for both files. | |
739 ** | |
740 ** The size of file pTo may be reduced by this operation. If anything | |
741 ** goes wrong, the transaction on pTo is rolled back. If successful, the | |
742 ** transaction is committed before returning. | |
743 */ | |
744 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ | |
745 int rc; | |
746 sqlite3_file *pFd; /* File descriptor for database pTo */ | |
747 sqlite3_backup b; | |
748 sqlite3BtreeEnter(pTo); | |
749 sqlite3BtreeEnter(pFrom); | |
750 | |
751 assert( sqlite3BtreeIsInTrans(pTo) ); | |
752 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo)); | |
753 if( pFd->pMethods ){ | |
754 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom); | |
755 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte); | |
756 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; | |
757 if( rc ) goto copy_finished; | |
758 } | |
759 | |
760 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set | |
761 ** to 0. This is used by the implementations of sqlite3_backup_step() | |
762 ** and sqlite3_backup_finish() to detect that they are being called | |
763 ** from this function, not directly by the user. | |
764 */ | |
765 memset(&b, 0, sizeof(b)); | |
766 b.pSrcDb = pFrom->db; | |
767 b.pSrc = pFrom; | |
768 b.pDest = pTo; | |
769 b.iNext = 1; | |
770 | |
771 #ifdef SQLITE_HAS_CODEC | |
772 sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom)); | |
773 #endif | |
774 | |
775 /* 0x7FFFFFFF is the hard limit for the number of pages in a database | |
776 ** file. By passing this as the number of pages to copy to | |
777 ** sqlite3_backup_step(), we can guarantee that the copy finishes | |
778 ** within a single call (unless an error occurs). The assert() statement | |
779 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE | |
780 ** or an error code. | |
781 */ | |
782 sqlite3_backup_step(&b, 0x7FFFFFFF); | |
783 assert( b.rc!=SQLITE_OK ); | |
784 rc = sqlite3_backup_finish(&b); | |
785 if( rc==SQLITE_OK ){ | |
786 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED; | |
787 }else{ | |
788 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest)); | |
789 } | |
790 | |
791 assert( sqlite3BtreeIsInTrans(pTo)==0 ); | |
792 copy_finished: | |
793 sqlite3BtreeLeave(pFrom); | |
794 sqlite3BtreeLeave(pTo); | |
795 return rc; | |
796 } | |
797 #endif /* SQLITE_OMIT_VACUUM */ | |
OLD | NEW |