blob: afbaeeb10971dbaefe277e6f9c7ae9f62cb9cb73 [file] [log] [blame]
danielk197704103022009-02-03 16:51:24 +00001/*
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.
danielk197704103022009-02-03 16:51:24 +000014*/
15#include "sqliteInt.h"
16#include "btreeInt.h"
17
danielk197704103022009-02-03 16:51:24 +000018/*
19** Structure allocated for each backup operation.
20*/
21struct 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
danielk1977e70f4f62009-05-13 07:52:06 +000039 int isAttached; /* True once backup has been registered with pager */
danielk197704103022009-02-03 16:51:24 +000040 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*/
82static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
83 int i = sqlite3FindDbName(pDb, zDb);
84
85 if( i==1 ){
drhe98c9042009-06-02 21:31:38 +000086 Parse *pParse;
87 int rc = 0;
88 pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
89 if( pParse==0 ){
90 sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
91 rc = SQLITE_NOMEM;
92 }else{
93 pParse->db = pDb;
94 if( sqlite3OpenTempDatabase(pParse) ){
drhe98c9042009-06-02 21:31:38 +000095 sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
96 rc = SQLITE_ERROR;
97 }
drha7564662010-02-22 19:32:31 +000098 sqlite3DbFree(pErrorDb, pParse->zErrMsg);
drhe98c9042009-06-02 21:31:38 +000099 sqlite3StackFree(pErrorDb, pParse);
100 }
101 if( rc ){
danielk197704103022009-02-03 16:51:24 +0000102 return 0;
103 }
danielk197704103022009-02-03 16:51:24 +0000104 }
105
106 if( i<0 ){
107 sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
108 return 0;
109 }
110
111 return pDb->aDb[i].pBt;
112}
113
114/*
drhca94d8b2011-01-11 17:38:03 +0000115** Attempt to set the page size of the destination to match the page size
116** of the source.
117*/
118static int setDestPgsz(sqlite3_backup *p){
119 int rc;
120 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
121 return rc;
122}
123
124/*
danielk197704103022009-02-03 16:51:24 +0000125** Create an sqlite3_backup process to copy the contents of zSrcDb from
126** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
127** a pointer to the new sqlite3_backup object.
128**
129** If an error occurs, NULL is returned and an error code and error message
130** stored in database handle pDestDb.
131*/
132sqlite3_backup *sqlite3_backup_init(
133 sqlite3* pDestDb, /* Database to write to */
134 const char *zDestDb, /* Name of database within pDestDb */
135 sqlite3* pSrcDb, /* Database connection to read from */
136 const char *zSrcDb /* Name of database within pSrcDb */
137){
138 sqlite3_backup *p; /* Value to return */
139
140 /* Lock the source database handle. The destination database
drh662c58c2009-02-03 21:13:07 +0000141 ** handle is not locked in this routine, but it is locked in
142 ** sqlite3_backup_step(). The user is required to ensure that no
danielk197704103022009-02-03 16:51:24 +0000143 ** other thread accesses the destination handle for the duration
drh662c58c2009-02-03 21:13:07 +0000144 ** of the backup operation. Any attempt to use the destination
145 ** database connection while a backup is in progress may cause
146 ** a malfunction or a deadlock.
danielk197704103022009-02-03 16:51:24 +0000147 */
148 sqlite3_mutex_enter(pSrcDb->mutex);
drheef1eb02009-02-04 16:56:19 +0000149 sqlite3_mutex_enter(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000150
151 if( pSrcDb==pDestDb ){
152 sqlite3Error(
drhb309bec2009-02-04 17:40:57 +0000153 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
danielk197704103022009-02-03 16:51:24 +0000154 );
155 p = 0;
156 }else {
drh9f129f42010-08-31 15:27:32 +0000157 /* Allocate space for a new sqlite3_backup object...
158 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
159 ** call to sqlite3_backup_init() and is destroyed by a call to
160 ** sqlite3_backup_finish(). */
dan6809c962012-07-30 14:53:54 +0000161 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
danielk197704103022009-02-03 16:51:24 +0000162 if( !p ){
163 sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
164 }
165 }
166
167 /* If the allocation succeeded, populate the new object. */
168 if( p ){
danielk197704103022009-02-03 16:51:24 +0000169 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
170 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
171 p->pDestDb = pDestDb;
172 p->pSrcDb = pSrcDb;
173 p->iNext = 1;
danielk1977e70f4f62009-05-13 07:52:06 +0000174 p->isAttached = 0;
danielk197704103022009-02-03 16:51:24 +0000175
drhca94d8b2011-01-11 17:38:03 +0000176 if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
177 /* One (or both) of the named databases did not exist or an OOM
178 ** error was hit. The error has already been written into the
179 ** pDestDb handle. All that is left to do here is free the
180 ** sqlite3_backup structure.
danielk197704103022009-02-03 16:51:24 +0000181 */
182 sqlite3_free(p);
183 p = 0;
184 }
185 }
danielk197704103022009-02-03 16:51:24 +0000186 if( p ){
danielk197704103022009-02-03 16:51:24 +0000187 p->pSrc->nBackup++;
188 }
189
drheef1eb02009-02-04 16:56:19 +0000190 sqlite3_mutex_leave(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000191 sqlite3_mutex_leave(pSrcDb->mutex);
192 return p;
193}
194
195/*
danielk197703ab0352009-02-06 05:59:44 +0000196** Argument rc is an SQLite error code. Return true if this error is
197** considered fatal if encountered during a backup operation. All errors
198** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
199*/
200static int isFatalError(int rc){
drhdcd7db52009-05-14 19:26:51 +0000201 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
danielk197703ab0352009-02-06 05:59:44 +0000202}
203
204/*
danielk197704103022009-02-03 16:51:24 +0000205** Parameter zSrcData points to a buffer containing the data for
206** page iSrcPg from the source database. Copy this data into the
207** destination database.
208*/
dan5cc3bea2012-12-21 16:15:35 +0000209static int backupOnePage(
210 sqlite3_backup *p, /* Backup handle */
211 Pgno iSrcPg, /* Source database page to backup */
212 const u8 *zSrcData, /* Source database page data */
213 int bUpdate /* True for an update, false otherwise */
214){
danielk197704103022009-02-03 16:51:24 +0000215 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
216 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
217 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
218 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
219 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
drh2b89fbc2011-04-09 02:09:44 +0000220#ifdef SQLITE_HAS_CODEC
dan0094f372012-09-28 20:23:42 +0000221 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
222 ** guaranteed that the shared-mutex is held by this thread, handle
223 ** p->pSrc may not actually be the owner. */
224 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
drh2b89fbc2011-04-09 02:09:44 +0000225 int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
226#endif
danielk197704103022009-02-03 16:51:24 +0000227 int rc = SQLITE_OK;
228 i64 iOff;
229
dan0094f372012-09-28 20:23:42 +0000230 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
danielk197704103022009-02-03 16:51:24 +0000231 assert( p->bDestLocked );
danielk197703ab0352009-02-06 05:59:44 +0000232 assert( !isFatalError(p->rc) );
danielk197704103022009-02-03 16:51:24 +0000233 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
234 assert( zSrcData );
235
236 /* Catch the case where the destination is an in-memory database and the
237 ** page sizes of the source and destination differ.
238 */
drh3289c5e2010-05-05 16:23:26 +0000239 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
danielk197704103022009-02-03 16:51:24 +0000240 rc = SQLITE_READONLY;
241 }
242
drh871919b2010-08-20 15:32:21 +0000243#ifdef SQLITE_HAS_CODEC
244 /* Backup is not possible if the page size of the destination is changing
drh2b89fbc2011-04-09 02:09:44 +0000245 ** and a codec is in use.
drh871919b2010-08-20 15:32:21 +0000246 */
247 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
248 rc = SQLITE_READONLY;
249 }
drh2b89fbc2011-04-09 02:09:44 +0000250
251 /* Backup is not possible if the number of bytes of reserve space differ
252 ** between source and destination. If there is a difference, try to
253 ** fix the destination to agree with the source. If that is not possible,
254 ** then the backup cannot proceed.
255 */
256 if( nSrcReserve!=nDestReserve ){
257 u32 newPgsz = nSrcPgsz;
258 rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
259 if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
260 }
drh871919b2010-08-20 15:32:21 +0000261#endif
262
danielk197704103022009-02-03 16:51:24 +0000263 /* This loop runs once for each destination page spanned by the source
264 ** page. For each iteration, variable iOff is set to the byte offset
265 ** of the destination page.
266 */
267 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
268 DbPage *pDestPg = 0;
269 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
270 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
271 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
272 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
273 ){
274 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
275 u8 *zDestData = sqlite3PagerGetData(pDestPg);
276 u8 *zOut = &zDestData[iOff%nDestPgsz];
277
278 /* Copy the data from the source page into the destination page.
279 ** Then clear the Btree layer MemPage.isInit flag. Both this module
280 ** and the pager code use this trick (clearing the first byte
281 ** of the page 'extra' space to invalidate the Btree layers
282 ** cached parse of the page). MemPage.isInit is marked
283 ** "MUST BE FIRST" for this purpose.
284 */
285 memcpy(zOut, zIn, nCopy);
286 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
dan5cc3bea2012-12-21 16:15:35 +0000287 if( iOff==0 && bUpdate==0 ){
288 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
289 }
danielk197704103022009-02-03 16:51:24 +0000290 }
291 sqlite3PagerUnref(pDestPg);
292 }
293
294 return rc;
295}
296
297/*
danielk19773d0cbc32009-02-09 18:55:45 +0000298** If pFile is currently larger than iSize bytes, then truncate it to
299** exactly iSize bytes. If pFile is not larger than iSize bytes, then
300** this function is a no-op.
301**
302** Return SQLITE_OK if everything is successful, or an SQLite error
303** code if an error occurs.
304*/
305static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
306 i64 iCurrent;
307 int rc = sqlite3OsFileSize(pFile, &iCurrent);
308 if( rc==SQLITE_OK && iCurrent>iSize ){
309 rc = sqlite3OsTruncate(pFile, iSize);
310 }
311 return rc;
312}
313
314/*
danielk1977e70f4f62009-05-13 07:52:06 +0000315** Register this backup object with the associated source pager for
316** callbacks when pages are changed or the cache invalidated.
317*/
318static void attachBackupObject(sqlite3_backup *p){
319 sqlite3_backup **pp;
320 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
321 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
322 p->pNext = *pp;
323 *pp = p;
324 p->isAttached = 1;
325}
326
327/*
danielk197704103022009-02-03 16:51:24 +0000328** Copy nPage pages from the source b-tree to the destination.
329*/
330int sqlite3_backup_step(sqlite3_backup *p, int nPage){
331 int rc;
drh3289c5e2010-05-05 16:23:26 +0000332 int destMode; /* Destination journal mode */
drh5c10f772010-05-05 18:46:44 +0000333 int pgszSrc = 0; /* Source page size */
334 int pgszDest = 0; /* Destination page size */
danielk197704103022009-02-03 16:51:24 +0000335
336 sqlite3_mutex_enter(p->pSrcDb->mutex);
337 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000338 if( p->pDestDb ){
339 sqlite3_mutex_enter(p->pDestDb->mutex);
340 }
danielk197704103022009-02-03 16:51:24 +0000341
drh5c10f772010-05-05 18:46:44 +0000342 rc = p->rc;
danielk197703ab0352009-02-06 05:59:44 +0000343 if( !isFatalError(rc) ){
danielk197704103022009-02-03 16:51:24 +0000344 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
345 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
346 int ii; /* Iterator variable */
shane63207ab2009-02-04 01:49:30 +0000347 int nSrcPage = -1; /* Size of source db in pages */
danielk197704103022009-02-03 16:51:24 +0000348 int bCloseTrans = 0; /* True if src db requires unlocking */
349
350 /* If the source pager is currently in a write-transaction, return
danielk1977404ca072009-03-16 13:19:36 +0000351 ** SQLITE_BUSY immediately.
danielk197704103022009-02-03 16:51:24 +0000352 */
353 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
danielk1977404ca072009-03-16 13:19:36 +0000354 rc = SQLITE_BUSY;
danielk197703ab0352009-02-06 05:59:44 +0000355 }else{
356 rc = SQLITE_OK;
danielk197704103022009-02-03 16:51:24 +0000357 }
358
359 /* Lock the destination database, if it is not locked already. */
360 if( SQLITE_OK==rc && p->bDestLocked==0
361 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
362 ){
363 p->bDestLocked = 1;
danielk1977602b4662009-07-02 07:47:33 +0000364 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
danielk197704103022009-02-03 16:51:24 +0000365 }
366
367 /* If there is no open read-transaction on the source database, open
368 ** one now. If a transaction is opened here, then it will be closed
369 ** before this function exits.
370 */
371 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
372 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
373 bCloseTrans = 1;
374 }
drh5c10f772010-05-05 18:46:44 +0000375
376 /* Do not allow backup if the destination database is in WAL mode
377 ** and the page sizes are different between source and destination */
378 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
379 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
drh0b9b4302010-06-11 17:01:24 +0000380 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
drh5c10f772010-05-05 18:46:44 +0000381 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
382 rc = SQLITE_READONLY;
383 }
danielk197704103022009-02-03 16:51:24 +0000384
385 /* Now that there is a read-lock on the source database, query the
386 ** source pager for the number of pages in the database.
387 */
drhb1299152010-03-30 22:58:33 +0000388 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
389 assert( nSrcPage>=0 );
danielk197703ab0352009-02-06 05:59:44 +0000390 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
danielk197704103022009-02-03 16:51:24 +0000391 const Pgno iSrcPg = p->iNext; /* Source page number */
392 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
393 DbPage *pSrcPg; /* Source page object */
drh7e369622013-04-03 03:53:15 +0000394 rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
drhb00fc3b2013-08-21 23:42:32 +0000395 PAGER_GET_READONLY);
danielk197704103022009-02-03 16:51:24 +0000396 if( rc==SQLITE_OK ){
dan5cc3bea2012-12-21 16:15:35 +0000397 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
danielk197704103022009-02-03 16:51:24 +0000398 sqlite3PagerUnref(pSrcPg);
399 }
400 }
401 p->iNext++;
402 }
403 if( rc==SQLITE_OK ){
404 p->nPagecount = nSrcPage;
405 p->nRemaining = nSrcPage+1-p->iNext;
shane63207ab2009-02-04 01:49:30 +0000406 if( p->iNext>(Pgno)nSrcPage ){
danielk197704103022009-02-03 16:51:24 +0000407 rc = SQLITE_DONE;
danielk1977e70f4f62009-05-13 07:52:06 +0000408 }else if( !p->isAttached ){
409 attachBackupObject(p);
danielk197704103022009-02-03 16:51:24 +0000410 }
411 }
412
drhf25cd712009-07-06 19:03:12 +0000413 /* Update the schema version field in the destination database. This
414 ** is to make sure that the schema-version really does change in
415 ** the case where the source and destination databases have the
416 ** same schema version.
417 */
drhc5dbffe2011-08-25 20:18:47 +0000418 if( rc==SQLITE_DONE ){
danb483eba2012-10-13 19:58:11 +0000419 if( nSrcPage==0 ){
420 rc = sqlite3BtreeNewDb(p->pDest);
421 nSrcPage = 1;
422 }
423 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
424 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
425 }
drhc5dbffe2011-08-25 20:18:47 +0000426 if( rc==SQLITE_OK ){
427 if( p->pDestDb ){
drh81028a42012-05-15 18:28:27 +0000428 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
drhc5dbffe2011-08-25 20:18:47 +0000429 }
430 if( destMode==PAGER_JOURNALMODE_WAL ){
431 rc = sqlite3BtreeSetVersion(p->pDest, 2);
432 }
drhd3a5c502009-02-03 22:51:06 +0000433 }
dan4b270402011-08-25 19:28:47 +0000434 if( rc==SQLITE_OK ){
435 int nDestTruncate;
436 /* Set nDestTruncate to the final number of pages in the destination
437 ** database. The complication here is that the destination page
438 ** size may be different to the source page size.
danielk197704103022009-02-03 16:51:24 +0000439 **
dan4b270402011-08-25 19:28:47 +0000440 ** If the source page size is smaller than the destination page size,
441 ** round up. In this case the call to sqlite3OsTruncate() below will
442 ** fix the size of the file. However it is important to call
443 ** sqlite3PagerTruncateImage() here so that any pages in the
444 ** destination file that lie beyond the nDestTruncate page mark are
445 ** journalled by PagerCommitPhaseOne() before they are destroyed
446 ** by the file truncation.
danielk197704103022009-02-03 16:51:24 +0000447 */
dan4b270402011-08-25 19:28:47 +0000448 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
449 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
450 if( pgszSrc<pgszDest ){
451 int ratio = pgszDest/pgszSrc;
452 nDestTruncate = (nSrcPage+ratio-1)/ratio;
453 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
454 nDestTruncate--;
drhc6aed542011-01-16 22:37:09 +0000455 }
dan4b270402011-08-25 19:28:47 +0000456 }else{
457 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
drhc6aed542011-01-16 22:37:09 +0000458 }
danb483eba2012-10-13 19:58:11 +0000459 assert( nDestTruncate>0 );
dan4d26d582011-01-25 18:19:24 +0000460
dan4b270402011-08-25 19:28:47 +0000461 if( pgszSrc<pgszDest ){
462 /* If the source page-size is smaller than the destination page-size,
463 ** two extra things may need to happen:
464 **
465 ** * The destination may need to be truncated, and
466 **
467 ** * Data stored on the pages immediately following the
468 ** pending-byte page in the source database may need to be
469 ** copied into the destination database.
470 */
471 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
472 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
danbc1a3c62013-02-23 16:40:46 +0000473 Pgno iPg;
474 int nDstPage;
dan4b270402011-08-25 19:28:47 +0000475 i64 iOff;
476 i64 iEnd;
477
478 assert( pFile );
danb483eba2012-10-13 19:58:11 +0000479 assert( nDestTruncate==0
480 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
dan4b270402011-08-25 19:28:47 +0000481 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
482 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
483 ));
484
danbc1a3c62013-02-23 16:40:46 +0000485 /* This block ensures that all data required to recreate the original
dan4b270402011-08-25 19:28:47 +0000486 ** database has been stored in the journal for pDestPager and the
487 ** journal synced to disk. So at this point we may safely modify
488 ** the database file in any way, knowing that if a power failure
489 ** occurs, the original database will be reconstructed from the
490 ** journal file. */
danbc1a3c62013-02-23 16:40:46 +0000491 sqlite3PagerPagecount(pDestPager, &nDstPage);
492 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
493 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
494 DbPage *pPg;
495 rc = sqlite3PagerGet(pDestPager, iPg, &pPg);
496 if( rc==SQLITE_OK ){
497 rc = sqlite3PagerWrite(pPg);
498 sqlite3PagerUnref(pPg);
499 }
500 }
501 }
dan896f99e2013-02-25 07:12:40 +0000502 if( rc==SQLITE_OK ){
503 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
504 }
dan4b270402011-08-25 19:28:47 +0000505
506 /* Write the extra pages and truncate the database file as required */
507 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
508 for(
509 iOff=PENDING_BYTE+pgszSrc;
510 rc==SQLITE_OK && iOff<iEnd;
511 iOff+=pgszSrc
512 ){
513 PgHdr *pSrcPg = 0;
514 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
515 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
516 if( rc==SQLITE_OK ){
517 u8 *zData = sqlite3PagerGetData(pSrcPg);
danf23da962013-03-23 21:00:41 +0000518 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
dan4b270402011-08-25 19:28:47 +0000519 }
520 sqlite3PagerUnref(pSrcPg);
521 }
522 if( rc==SQLITE_OK ){
523 rc = backupTruncateFile(pFile, iSize);
524 }
525
526 /* Sync the database file to disk. */
527 if( rc==SQLITE_OK ){
528 rc = sqlite3PagerSync(pDestPager);
529 }
530 }else{
danbc1a3c62013-02-23 16:40:46 +0000531 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
dan4b270402011-08-25 19:28:47 +0000532 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
danielk197704103022009-02-03 16:51:24 +0000533 }
dan4b270402011-08-25 19:28:47 +0000534
535 /* Finish committing the transaction to the destination database. */
536 if( SQLITE_OK==rc
537 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
538 ){
539 rc = SQLITE_DONE;
540 }
danielk197704103022009-02-03 16:51:24 +0000541 }
542 }
543
544 /* If bCloseTrans is true, then this function opened a read transaction
545 ** on the source database. Close the read transaction here. There is
546 ** no need to check the return values of the btree methods here, as
547 ** "committing" a read-only transaction cannot fail.
548 */
549 if( bCloseTrans ){
550 TESTONLY( int rc2 );
551 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
dan60939d02011-03-29 15:40:55 +0000552 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
danielk197704103022009-02-03 16:51:24 +0000553 assert( rc2==SQLITE_OK );
554 }
555
danba3cbf32010-06-30 04:29:03 +0000556 if( rc==SQLITE_IOERR_NOMEM ){
557 rc = SQLITE_NOMEM;
558 }
danielk197703ab0352009-02-06 05:59:44 +0000559 p->rc = rc;
danielk197704103022009-02-03 16:51:24 +0000560 }
drhd3a5c502009-02-03 22:51:06 +0000561 if( p->pDestDb ){
562 sqlite3_mutex_leave(p->pDestDb->mutex);
563 }
danielk197704103022009-02-03 16:51:24 +0000564 sqlite3BtreeLeave(p->pSrc);
565 sqlite3_mutex_leave(p->pSrcDb->mutex);
566 return rc;
567}
568
569/*
570** Release all resources associated with an sqlite3_backup* handle.
571*/
572int sqlite3_backup_finish(sqlite3_backup *p){
573 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
drhed688012012-06-21 15:51:42 +0000574 sqlite3 *pSrcDb; /* Source database connection */
danielk197704103022009-02-03 16:51:24 +0000575 int rc; /* Value to return */
576
577 /* Enter the mutexes */
drhdcd7db52009-05-14 19:26:51 +0000578 if( p==0 ) return SQLITE_OK;
drhed688012012-06-21 15:51:42 +0000579 pSrcDb = p->pSrcDb;
580 sqlite3_mutex_enter(pSrcDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000581 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000582 if( p->pDestDb ){
583 sqlite3_mutex_enter(p->pDestDb->mutex);
584 }
danielk197704103022009-02-03 16:51:24 +0000585
586 /* Detach this backup from the source pager. */
587 if( p->pDestDb ){
danielk1977e70f4f62009-05-13 07:52:06 +0000588 p->pSrc->nBackup--;
589 }
590 if( p->isAttached ){
danielk197704103022009-02-03 16:51:24 +0000591 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
592 while( *pp!=p ){
593 pp = &(*pp)->pNext;
594 }
595 *pp = p->pNext;
danielk197704103022009-02-03 16:51:24 +0000596 }
597
598 /* If a transaction is still open on the Btree, roll it back. */
drh0f198a72012-02-13 16:43:16 +0000599 sqlite3BtreeRollback(p->pDest, SQLITE_OK);
danielk197704103022009-02-03 16:51:24 +0000600
601 /* Set the error code of the destination database handle. */
602 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
603 sqlite3Error(p->pDestDb, rc, 0);
604
605 /* Exit the mutexes and free the backup context structure. */
drhd3a5c502009-02-03 22:51:06 +0000606 if( p->pDestDb ){
drh4245c402012-06-02 14:32:21 +0000607 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
drhd3a5c502009-02-03 22:51:06 +0000608 }
danielk197704103022009-02-03 16:51:24 +0000609 sqlite3BtreeLeave(p->pSrc);
610 if( p->pDestDb ){
drh9f129f42010-08-31 15:27:32 +0000611 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
612 ** call to sqlite3_backup_init() and is destroyed by a call to
613 ** sqlite3_backup_finish(). */
danielk197704103022009-02-03 16:51:24 +0000614 sqlite3_free(p);
615 }
drh4245c402012-06-02 14:32:21 +0000616 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
danielk197704103022009-02-03 16:51:24 +0000617 return rc;
618}
619
620/*
621** Return the number of pages still to be backed up as of the most recent
622** call to sqlite3_backup_step().
623*/
624int sqlite3_backup_remaining(sqlite3_backup *p){
625 return p->nRemaining;
626}
627
628/*
629** Return the total number of pages in the source database as of the most
630** recent call to sqlite3_backup_step().
631*/
632int sqlite3_backup_pagecount(sqlite3_backup *p){
633 return p->nPagecount;
634}
635
636/*
637** This function is called after the contents of page iPage of the
638** source database have been modified. If page iPage has already been
639** copied into the destination database, then the data written to the
640** destination is now invalidated. The destination copy of iPage needs
641** to be updated with the new data before the backup operation is
642** complete.
643**
644** It is assumed that the mutex associated with the BtShared object
645** corresponding to the source database is held when this function is
646** called.
647*/
648void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
649 sqlite3_backup *p; /* Iterator variable */
650 for(p=pBackup; p; p=p->pNext){
651 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
danielk197703ab0352009-02-06 05:59:44 +0000652 if( !isFatalError(p->rc) && iPage<p->iNext ){
danielk197704103022009-02-03 16:51:24 +0000653 /* The backup process p has already copied page iPage. But now it
654 ** has been modified by a transaction on the source pager. Copy
655 ** the new data into the backup.
656 */
drh2b89fbc2011-04-09 02:09:44 +0000657 int rc;
drh806ebcb2011-04-09 17:53:30 +0000658 assert( p->pDestDb );
659 sqlite3_mutex_enter(p->pDestDb->mutex);
dan5cc3bea2012-12-21 16:15:35 +0000660 rc = backupOnePage(p, iPage, aData, 1);
drh806ebcb2011-04-09 17:53:30 +0000661 sqlite3_mutex_leave(p->pDestDb->mutex);
danielk197703ab0352009-02-06 05:59:44 +0000662 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
danielk197704103022009-02-03 16:51:24 +0000663 if( rc!=SQLITE_OK ){
664 p->rc = rc;
665 }
666 }
667 }
668}
669
670/*
671** Restart the backup process. This is called when the pager layer
672** detects that the database has been modified by an external database
673** connection. In this case there is no way of knowing which of the
674** pages that have been copied into the destination database are still
675** valid and which are not, so the entire process needs to be restarted.
676**
677** It is assumed that the mutex associated with the BtShared object
678** corresponding to the source database is held when this function is
679** called.
680*/
681void sqlite3BackupRestart(sqlite3_backup *pBackup){
682 sqlite3_backup *p; /* Iterator variable */
683 for(p=pBackup; p; p=p->pNext){
684 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
685 p->iNext = 1;
686 }
687}
688
689#ifndef SQLITE_OMIT_VACUUM
690/*
691** Copy the complete content of pBtFrom into pBtTo. A transaction
692** must be active for both files.
693**
694** The size of file pTo may be reduced by this operation. If anything
695** goes wrong, the transaction on pTo is rolled back. If successful, the
696** transaction is committed before returning.
697*/
698int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
699 int rc;
danc5f20a02011-10-07 16:57:59 +0000700 sqlite3_file *pFd; /* File descriptor for database pTo */
danielk197704103022009-02-03 16:51:24 +0000701 sqlite3_backup b;
702 sqlite3BtreeEnter(pTo);
703 sqlite3BtreeEnter(pFrom);
704
danc5f20a02011-10-07 16:57:59 +0000705 assert( sqlite3BtreeIsInTrans(pTo) );
706 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
707 if( pFd->pMethods ){
708 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
drh2bfe5b32012-01-10 16:40:50 +0000709 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
710 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
711 if( rc ) goto copy_finished;
danc5f20a02011-10-07 16:57:59 +0000712 }
713
danielk197704103022009-02-03 16:51:24 +0000714 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
715 ** to 0. This is used by the implementations of sqlite3_backup_step()
716 ** and sqlite3_backup_finish() to detect that they are being called
717 ** from this function, not directly by the user.
718 */
719 memset(&b, 0, sizeof(b));
720 b.pSrcDb = pFrom->db;
721 b.pSrc = pFrom;
722 b.pDest = pTo;
723 b.iNext = 1;
724
725 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
726 ** file. By passing this as the number of pages to copy to
727 ** sqlite3_backup_step(), we can guarantee that the copy finishes
728 ** within a single call (unless an error occurs). The assert() statement
729 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
730 ** or an error code.
731 */
732 sqlite3_backup_step(&b, 0x7FFFFFFF);
733 assert( b.rc!=SQLITE_OK );
734 rc = sqlite3_backup_finish(&b);
735 if( rc==SQLITE_OK ){
drhc9166342012-01-05 23:32:06 +0000736 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
dan1a83bc52011-10-21 14:27:32 +0000737 }else{
738 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
danielk197704103022009-02-03 16:51:24 +0000739 }
740
danc5f20a02011-10-07 16:57:59 +0000741 assert( sqlite3BtreeIsInTrans(pTo)==0 );
drh2bfe5b32012-01-10 16:40:50 +0000742copy_finished:
danielk197704103022009-02-03 16:51:24 +0000743 sqlite3BtreeLeave(pFrom);
744 sqlite3BtreeLeave(pTo);
745 return rc;
746}
747#endif /* SQLITE_OMIT_VACUUM */