Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: third_party/sqlite/src/backup.c

Issue 3108030: Move bundled copy of sqlite one level deeper to better separate it... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/src/auth.c ('k') | third_party/sqlite/src/bitvec.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 ** $Id: backup.c,v 1.19 2009/07/06 19:03:13 drh 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 int isAttached; /* True once backup has been registered with pager */
48 sqlite3_backup *pNext; /* Next backup associated with source pager */
49 };
50
51 /*
52 ** THREAD SAFETY NOTES:
53 **
54 ** Once it has been created using backup_init(), a single sqlite3_backup
55 ** structure may be accessed via two groups of thread-safe entry points:
56 **
57 ** * Via the sqlite3_backup_XXX() API function backup_step() and
58 ** backup_finish(). Both these functions obtain the source database
59 ** handle mutex and the mutex associated with the source BtShared
60 ** structure, in that order.
61 **
62 ** * Via the BackupUpdate() and BackupRestart() functions, which are
63 ** invoked by the pager layer to report various state changes in
64 ** the page cache associated with the source database. The mutex
65 ** associated with the source database BtShared structure will always
66 ** be held when either of these functions are invoked.
67 **
68 ** The other sqlite3_backup_XXX() API functions, backup_remaining() and
69 ** backup_pagecount() are not thread-safe functions. If they are called
70 ** while some other thread is calling backup_step() or backup_finish(),
71 ** the values returned may be invalid. There is no way for a call to
72 ** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
73 ** or backup_pagecount().
74 **
75 ** Depending on the SQLite configuration, the database handles and/or
76 ** the Btree objects may have their own mutexes that require locking.
77 ** Non-sharable Btrees (in-memory databases for example), do not have
78 ** associated mutexes.
79 */
80
81 /*
82 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
83 ** in connection handle pDb. If such a database cannot be found, return
84 ** a NULL pointer and write an error message to pErrorDb.
85 **
86 ** If the "temp" database is requested, it may need to be opened by this
87 ** function. If an error occurs while doing so, return 0 and write an
88 ** error message to pErrorDb.
89 */
90 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
91 int i = sqlite3FindDbName(pDb, zDb);
92
93 if( i==1 ){
94 Parse *pParse;
95 int rc = 0;
96 pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
97 if( pParse==0 ){
98 sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
99 rc = SQLITE_NOMEM;
100 }else{
101 pParse->db = pDb;
102 if( sqlite3OpenTempDatabase(pParse) ){
103 sqlite3ErrorClear(pParse);
104 sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
105 rc = SQLITE_ERROR;
106 }
107 sqlite3StackFree(pErrorDb, pParse);
108 }
109 if( rc ){
110 return 0;
111 }
112 }
113
114 if( i<0 ){
115 sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
116 return 0;
117 }
118
119 return pDb->aDb[i].pBt;
120 }
121
122 /*
123 ** Create an sqlite3_backup process to copy the contents of zSrcDb from
124 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
125 ** a pointer to the new sqlite3_backup object.
126 **
127 ** If an error occurs, NULL is returned and an error code and error message
128 ** stored in database handle pDestDb.
129 */
130 sqlite3_backup *sqlite3_backup_init(
131 sqlite3* pDestDb, /* Database to write to */
132 const char *zDestDb, /* Name of database within pDestDb */
133 sqlite3* pSrcDb, /* Database connection to read from */
134 const char *zSrcDb /* Name of database within pSrcDb */
135 ){
136 sqlite3_backup *p; /* Value to return */
137
138 /* Lock the source database handle. The destination database
139 ** handle is not locked in this routine, but it is locked in
140 ** sqlite3_backup_step(). The user is required to ensure that no
141 ** other thread accesses the destination handle for the duration
142 ** of the backup operation. Any attempt to use the destination
143 ** database connection while a backup is in progress may cause
144 ** a malfunction or a deadlock.
145 */
146 sqlite3_mutex_enter(pSrcDb->mutex);
147 sqlite3_mutex_enter(pDestDb->mutex);
148
149 if( pSrcDb==pDestDb ){
150 sqlite3Error(
151 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
152 );
153 p = 0;
154 }else {
155 /* Allocate space for a new sqlite3_backup object */
156 p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
157 if( !p ){
158 sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
159 }
160 }
161
162 /* If the allocation succeeded, populate the new object. */
163 if( p ){
164 memset(p, 0, sizeof(sqlite3_backup));
165 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
166 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
167 p->pDestDb = pDestDb;
168 p->pSrcDb = pSrcDb;
169 p->iNext = 1;
170 p->isAttached = 0;
171
172 if( 0==p->pSrc || 0==p->pDest ){
173 /* One (or both) of the named databases did not exist. An error has
174 ** already been written into the pDestDb handle. All that is left
175 ** to do here is free the sqlite3_backup structure.
176 */
177 sqlite3_free(p);
178 p = 0;
179 }
180 }
181 if( p ){
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 && ALWAYS(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 ** Register this backup object with the associated source pager for
276 ** callbacks when pages are changed or the cache invalidated.
277 */
278 static void attachBackupObject(sqlite3_backup *p){
279 sqlite3_backup **pp;
280 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
281 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
282 p->pNext = *pp;
283 *pp = p;
284 p->isAttached = 1;
285 }
286
287 /*
288 ** Copy nPage pages from the source b-tree to the destination.
289 */
290 int sqlite3_backup_step(sqlite3_backup *p, int nPage){
291 int rc;
292
293 sqlite3_mutex_enter(p->pSrcDb->mutex);
294 sqlite3BtreeEnter(p->pSrc);
295 if( p->pDestDb ){
296 sqlite3_mutex_enter(p->pDestDb->mutex);
297 }
298
299 rc = p->rc;
300 if( !isFatalError(rc) ){
301 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
302 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
303 int ii; /* Iterator variable */
304 int nSrcPage = -1; /* Size of source db in pages */
305 int bCloseTrans = 0; /* True if src db requires unlocking */
306
307 /* If the source pager is currently in a write-transaction, return
308 ** SQLITE_BUSY immediately.
309 */
310 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
311 rc = SQLITE_BUSY;
312 }else{
313 rc = SQLITE_OK;
314 }
315
316 /* Lock the destination database, if it is not locked already. */
317 if( SQLITE_OK==rc && p->bDestLocked==0
318 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
319 ){
320 p->bDestLocked = 1;
321 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
322 }
323
324 /* If there is no open read-transaction on the source database, open
325 ** one now. If a transaction is opened here, then it will be closed
326 ** before this function exits.
327 */
328 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
329 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
330 bCloseTrans = 1;
331 }
332
333 /* Now that there is a read-lock on the source database, query the
334 ** source pager for the number of pages in the database.
335 */
336 if( rc==SQLITE_OK ){
337 rc = sqlite3PagerPagecount(pSrcPager, &nSrcPage);
338 }
339 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
340 const Pgno iSrcPg = p->iNext; /* Source page number */
341 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
342 DbPage *pSrcPg; /* Source page object */
343 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
344 if( rc==SQLITE_OK ){
345 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
346 sqlite3PagerUnref(pSrcPg);
347 }
348 }
349 p->iNext++;
350 }
351 if( rc==SQLITE_OK ){
352 p->nPagecount = nSrcPage;
353 p->nRemaining = nSrcPage+1-p->iNext;
354 if( p->iNext>(Pgno)nSrcPage ){
355 rc = SQLITE_DONE;
356 }else if( !p->isAttached ){
357 attachBackupObject(p);
358 }
359 }
360
361 /* Update the schema version field in the destination database. This
362 ** is to make sure that the schema-version really does change in
363 ** the case where the source and destination databases have the
364 ** same schema version.
365 */
366 if( rc==SQLITE_DONE
367 && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
368 ){
369 const int nSrcPagesize = sqlite3BtreeGetPageSize(p->pSrc);
370 const int nDestPagesize = sqlite3BtreeGetPageSize(p->pDest);
371 int nDestTruncate;
372
373 if( p->pDestDb ){
374 sqlite3ResetInternalSchema(p->pDestDb, 0);
375 }
376
377 /* Set nDestTruncate to the final number of pages in the destination
378 ** database. The complication here is that the destination page
379 ** size may be different to the source page size.
380 **
381 ** If the source page size is smaller than the destination page size,
382 ** round up. In this case the call to sqlite3OsTruncate() below will
383 ** fix the size of the file. However it is important to call
384 ** sqlite3PagerTruncateImage() here so that any pages in the
385 ** destination file that lie beyond the nDestTruncate page mark are
386 ** journalled by PagerCommitPhaseOne() before they are destroyed
387 ** by the file truncation.
388 */
389 if( nSrcPagesize<nDestPagesize ){
390 int ratio = nDestPagesize/nSrcPagesize;
391 nDestTruncate = (nSrcPage+ratio-1)/ratio;
392 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
393 nDestTruncate--;
394 }
395 }else{
396 nDestTruncate = nSrcPage * (nSrcPagesize/nDestPagesize);
397 }
398 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
399
400 if( nSrcPagesize<nDestPagesize ){
401 /* If the source page-size is smaller than the destination page-size,
402 ** two extra things may need to happen:
403 **
404 ** * The destination may need to be truncated, and
405 **
406 ** * Data stored on the pages immediately following the
407 ** pending-byte page in the source database may need to be
408 ** copied into the destination database.
409 */
410 const i64 iSize = (i64)nSrcPagesize * (i64)nSrcPage;
411 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
412
413 assert( pFile );
414 assert( (i64)nDestTruncate*(i64)nDestPagesize >= iSize || (
415 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
416 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+nDestPagesize
417 ));
418 if( SQLITE_OK==(rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1))
419 && SQLITE_OK==(rc = backupTruncateFile(pFile, iSize))
420 && SQLITE_OK==(rc = sqlite3PagerSync(pDestPager))
421 ){
422 i64 iOff;
423 i64 iEnd = MIN(PENDING_BYTE + nDestPagesize, iSize);
424 for(
425 iOff=PENDING_BYTE+nSrcPagesize;
426 rc==SQLITE_OK && iOff<iEnd;
427 iOff+=nSrcPagesize
428 ){
429 PgHdr *pSrcPg = 0;
430 const Pgno iSrcPg = (Pgno)((iOff/nSrcPagesize)+1);
431 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
432 if( rc==SQLITE_OK ){
433 u8 *zData = sqlite3PagerGetData(pSrcPg);
434 rc = sqlite3OsWrite(pFile, zData, nSrcPagesize, iOff);
435 }
436 sqlite3PagerUnref(pSrcPg);
437 }
438 }
439 }else{
440 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
441 }
442
443 /* Finish committing the transaction to the destination database. */
444 if( SQLITE_OK==rc
445 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest))
446 ){
447 rc = SQLITE_DONE;
448 }
449 }
450
451 /* If bCloseTrans is true, then this function opened a read transaction
452 ** on the source database. Close the read transaction here. There is
453 ** no need to check the return values of the btree methods here, as
454 ** "committing" a read-only transaction cannot fail.
455 */
456 if( bCloseTrans ){
457 TESTONLY( int rc2 );
458 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
459 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc);
460 assert( rc2==SQLITE_OK );
461 }
462
463 p->rc = rc;
464 }
465 if( p->pDestDb ){
466 sqlite3_mutex_leave(p->pDestDb->mutex);
467 }
468 sqlite3BtreeLeave(p->pSrc);
469 sqlite3_mutex_leave(p->pSrcDb->mutex);
470 return rc;
471 }
472
473 /*
474 ** Release all resources associated with an sqlite3_backup* handle.
475 */
476 int sqlite3_backup_finish(sqlite3_backup *p){
477 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
478 sqlite3_mutex *mutex; /* Mutex to protect source database */
479 int rc; /* Value to return */
480
481 /* Enter the mutexes */
482 if( p==0 ) return SQLITE_OK;
483 sqlite3_mutex_enter(p->pSrcDb->mutex);
484 sqlite3BtreeEnter(p->pSrc);
485 mutex = p->pSrcDb->mutex;
486 if( p->pDestDb ){
487 sqlite3_mutex_enter(p->pDestDb->mutex);
488 }
489
490 /* Detach this backup from the source pager. */
491 if( p->pDestDb ){
492 p->pSrc->nBackup--;
493 }
494 if( p->isAttached ){
495 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
496 while( *pp!=p ){
497 pp = &(*pp)->pNext;
498 }
499 *pp = p->pNext;
500 }
501
502 /* If a transaction is still open on the Btree, roll it back. */
503 sqlite3BtreeRollback(p->pDest);
504
505 /* Set the error code of the destination database handle. */
506 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
507 sqlite3Error(p->pDestDb, rc, 0);
508
509 /* Exit the mutexes and free the backup context structure. */
510 if( p->pDestDb ){
511 sqlite3_mutex_leave(p->pDestDb->mutex);
512 }
513 sqlite3BtreeLeave(p->pSrc);
514 if( p->pDestDb ){
515 sqlite3_free(p);
516 }
517 sqlite3_mutex_leave(mutex);
518 return rc;
519 }
520
521 /*
522 ** Return the number of pages still to be backed up as of the most recent
523 ** call to sqlite3_backup_step().
524 */
525 int sqlite3_backup_remaining(sqlite3_backup *p){
526 return p->nRemaining;
527 }
528
529 /*
530 ** Return the total number of pages in the source database as of the most
531 ** recent call to sqlite3_backup_step().
532 */
533 int sqlite3_backup_pagecount(sqlite3_backup *p){
534 return p->nPagecount;
535 }
536
537 /*
538 ** This function is called after the contents of page iPage of the
539 ** source database have been modified. If page iPage has already been
540 ** copied into the destination database, then the data written to the
541 ** destination is now invalidated. The destination copy of iPage needs
542 ** to be updated with the new data before the backup operation is
543 ** complete.
544 **
545 ** It is assumed that the mutex associated with the BtShared object
546 ** corresponding to the source database is held when this function is
547 ** called.
548 */
549 void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
550 sqlite3_backup *p; /* Iterator variable */
551 for(p=pBackup; p; p=p->pNext){
552 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
553 if( !isFatalError(p->rc) && iPage<p->iNext ){
554 /* The backup process p has already copied page iPage. But now it
555 ** has been modified by a transaction on the source pager. Copy
556 ** the new data into the backup.
557 */
558 int rc = backupOnePage(p, iPage, aData);
559 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
560 if( rc!=SQLITE_OK ){
561 p->rc = rc;
562 }
563 }
564 }
565 }
566
567 /*
568 ** Restart the backup process. This is called when the pager layer
569 ** detects that the database has been modified by an external database
570 ** connection. In this case there is no way of knowing which of the
571 ** pages that have been copied into the destination database are still
572 ** valid and which are not, so the entire process needs to be restarted.
573 **
574 ** It is assumed that the mutex associated with the BtShared object
575 ** corresponding to the source database is held when this function is
576 ** called.
577 */
578 void sqlite3BackupRestart(sqlite3_backup *pBackup){
579 sqlite3_backup *p; /* Iterator variable */
580 for(p=pBackup; p; p=p->pNext){
581 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
582 p->iNext = 1;
583 }
584 }
585
586 #ifndef SQLITE_OMIT_VACUUM
587 /*
588 ** Copy the complete content of pBtFrom into pBtTo. A transaction
589 ** must be active for both files.
590 **
591 ** The size of file pTo may be reduced by this operation. If anything
592 ** goes wrong, the transaction on pTo is rolled back. If successful, the
593 ** transaction is committed before returning.
594 */
595 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
596 int rc;
597 sqlite3_backup b;
598 sqlite3BtreeEnter(pTo);
599 sqlite3BtreeEnter(pFrom);
600
601 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
602 ** to 0. This is used by the implementations of sqlite3_backup_step()
603 ** and sqlite3_backup_finish() to detect that they are being called
604 ** from this function, not directly by the user.
605 */
606 memset(&b, 0, sizeof(b));
607 b.pSrcDb = pFrom->db;
608 b.pSrc = pFrom;
609 b.pDest = pTo;
610 b.iNext = 1;
611
612 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
613 ** file. By passing this as the number of pages to copy to
614 ** sqlite3_backup_step(), we can guarantee that the copy finishes
615 ** within a single call (unless an error occurs). The assert() statement
616 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
617 ** or an error code.
618 */
619 sqlite3_backup_step(&b, 0x7FFFFFFF);
620 assert( b.rc!=SQLITE_OK );
621 rc = sqlite3_backup_finish(&b);
622 if( rc==SQLITE_OK ){
623 pTo->pBt->pageSizeFixed = 0;
624 }
625
626 sqlite3BtreeLeave(pFrom);
627 sqlite3BtreeLeave(pTo);
628 return rc;
629 }
630 #endif /* SQLITE_OMIT_VACUUM */
OLDNEW
« no previous file with comments | « third_party/sqlite/src/auth.c ('k') | third_party/sqlite/src/bitvec.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698