blob: d9671750f40d9e011f16545f24173d064dec84c9 [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 ){
drhcb43a932016-10-03 01:21:51 +000086 Parse sParse;
drhe98c9042009-06-02 21:31:38 +000087 int rc = 0;
drh92ecf302022-01-31 12:29:14 +000088 sqlite3ParseObjectInit(&sParse,pDb);
drhcb43a932016-10-03 01:21:51 +000089 if( sqlite3OpenTempDatabase(&sParse) ){
90 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
91 rc = SQLITE_ERROR;
drhe98c9042009-06-02 21:31:38 +000092 }
drhcb43a932016-10-03 01:21:51 +000093 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
drhc692df22022-01-24 15:34:55 +000094 sqlite3ParseObjectReset(&sParse);
drhe98c9042009-06-02 21:31:38 +000095 if( rc ){
danielk197704103022009-02-03 16:51:24 +000096 return 0;
97 }
danielk197704103022009-02-03 16:51:24 +000098 }
99
100 if( i<0 ){
drh13f40da2014-08-22 18:00:11 +0000101 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
danielk197704103022009-02-03 16:51:24 +0000102 return 0;
103 }
104
105 return pDb->aDb[i].pBt;
106}
107
108/*
drhca94d8b2011-01-11 17:38:03 +0000109** Attempt to set the page size of the destination to match the page size
110** of the source.
111*/
112static int setDestPgsz(sqlite3_backup *p){
113 int rc;
drhe937df82020-05-07 01:56:57 +0000114 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
drhca94d8b2011-01-11 17:38:03 +0000115 return rc;
116}
117
118/*
danfad01992014-11-13 14:18:25 +0000119** Check that there is no open read-transaction on the b-tree passed as the
120** second argument. If there is not, return SQLITE_OK. Otherwise, if there
121** is an open read-transaction, return SQLITE_ERROR and leave an error
122** message in database handle db.
123*/
124static int checkReadTransaction(sqlite3 *db, Btree *p){
drh99744fa2020-08-25 19:09:07 +0000125 if( sqlite3BtreeTxnState(p)!=SQLITE_TXN_NONE ){
danfad01992014-11-13 14:18:25 +0000126 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
127 return SQLITE_ERROR;
128 }
129 return SQLITE_OK;
130}
131
132/*
danielk197704103022009-02-03 16:51:24 +0000133** Create an sqlite3_backup process to copy the contents of zSrcDb from
134** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
135** a pointer to the new sqlite3_backup object.
136**
137** If an error occurs, NULL is returned and an error code and error message
138** stored in database handle pDestDb.
139*/
140sqlite3_backup *sqlite3_backup_init(
141 sqlite3* pDestDb, /* Database to write to */
142 const char *zDestDb, /* Name of database within pDestDb */
143 sqlite3* pSrcDb, /* Database connection to read from */
144 const char *zSrcDb /* Name of database within pSrcDb */
145){
146 sqlite3_backup *p; /* Value to return */
147
drh9ca95732014-10-24 00:35:58 +0000148#ifdef SQLITE_ENABLE_API_ARMOR
149 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
150 (void)SQLITE_MISUSE_BKPT;
151 return 0;
152 }
153#endif
154
danielk197704103022009-02-03 16:51:24 +0000155 /* Lock the source database handle. The destination database
drh662c58c2009-02-03 21:13:07 +0000156 ** handle is not locked in this routine, but it is locked in
157 ** sqlite3_backup_step(). The user is required to ensure that no
danielk197704103022009-02-03 16:51:24 +0000158 ** other thread accesses the destination handle for the duration
drh662c58c2009-02-03 21:13:07 +0000159 ** of the backup operation. Any attempt to use the destination
160 ** database connection while a backup is in progress may cause
161 ** a malfunction or a deadlock.
danielk197704103022009-02-03 16:51:24 +0000162 */
163 sqlite3_mutex_enter(pSrcDb->mutex);
drheef1eb02009-02-04 16:56:19 +0000164 sqlite3_mutex_enter(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000165
166 if( pSrcDb==pDestDb ){
drh13f40da2014-08-22 18:00:11 +0000167 sqlite3ErrorWithMsg(
drhb309bec2009-02-04 17:40:57 +0000168 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
danielk197704103022009-02-03 16:51:24 +0000169 );
170 p = 0;
171 }else {
drh9f129f42010-08-31 15:27:32 +0000172 /* Allocate space for a new sqlite3_backup object...
173 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
174 ** call to sqlite3_backup_init() and is destroyed by a call to
175 ** sqlite3_backup_finish(). */
dan6809c962012-07-30 14:53:54 +0000176 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
danielk197704103022009-02-03 16:51:24 +0000177 if( !p ){
mistachkinfad30392016-02-13 23:43:46 +0000178 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
danielk197704103022009-02-03 16:51:24 +0000179 }
180 }
181
182 /* If the allocation succeeded, populate the new object. */
183 if( p ){
danielk197704103022009-02-03 16:51:24 +0000184 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
185 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
186 p->pDestDb = pDestDb;
187 p->pSrcDb = pSrcDb;
188 p->iNext = 1;
danielk1977e70f4f62009-05-13 07:52:06 +0000189 p->isAttached = 0;
danielk197704103022009-02-03 16:51:24 +0000190
danfad01992014-11-13 14:18:25 +0000191 if( 0==p->pSrc || 0==p->pDest
danfad01992014-11-13 14:18:25 +0000192 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
193 ){
drhca94d8b2011-01-11 17:38:03 +0000194 /* One (or both) of the named databases did not exist or an OOM
danfad01992014-11-13 14:18:25 +0000195 ** error was hit. Or there is a transaction open on the destination
196 ** database. The error has already been written into the pDestDb
197 ** handle. All that is left to do here is free the sqlite3_backup
198 ** structure. */
danielk197704103022009-02-03 16:51:24 +0000199 sqlite3_free(p);
200 p = 0;
201 }
202 }
danielk197704103022009-02-03 16:51:24 +0000203 if( p ){
danielk197704103022009-02-03 16:51:24 +0000204 p->pSrc->nBackup++;
205 }
206
drheef1eb02009-02-04 16:56:19 +0000207 sqlite3_mutex_leave(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000208 sqlite3_mutex_leave(pSrcDb->mutex);
209 return p;
210}
211
212/*
danielk197703ab0352009-02-06 05:59:44 +0000213** Argument rc is an SQLite error code. Return true if this error is
214** considered fatal if encountered during a backup operation. All errors
215** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
216*/
217static int isFatalError(int rc){
drhdcd7db52009-05-14 19:26:51 +0000218 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
danielk197703ab0352009-02-06 05:59:44 +0000219}
220
221/*
danielk197704103022009-02-03 16:51:24 +0000222** Parameter zSrcData points to a buffer containing the data for
223** page iSrcPg from the source database. Copy this data into the
224** destination database.
225*/
dan5cc3bea2012-12-21 16:15:35 +0000226static int backupOnePage(
227 sqlite3_backup *p, /* Backup handle */
228 Pgno iSrcPg, /* Source database page to backup */
229 const u8 *zSrcData, /* Source database page data */
230 int bUpdate /* True for an update, false otherwise */
231){
danielk197704103022009-02-03 16:51:24 +0000232 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
233 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
234 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
235 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
236 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
danielk197704103022009-02-03 16:51:24 +0000237 int rc = SQLITE_OK;
238 i64 iOff;
239
dan0094f372012-09-28 20:23:42 +0000240 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
danielk197704103022009-02-03 16:51:24 +0000241 assert( p->bDestLocked );
danielk197703ab0352009-02-06 05:59:44 +0000242 assert( !isFatalError(p->rc) );
danielk197704103022009-02-03 16:51:24 +0000243 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
244 assert( zSrcData );
245
246 /* Catch the case where the destination is an in-memory database and the
247 ** page sizes of the source and destination differ.
248 */
drh3289c5e2010-05-05 16:23:26 +0000249 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
danielk197704103022009-02-03 16:51:24 +0000250 rc = SQLITE_READONLY;
251 }
252
253 /* This loop runs once for each destination page spanned by the source
254 ** page. For each iteration, variable iOff is set to the byte offset
255 ** of the destination page.
256 */
257 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
258 DbPage *pDestPg = 0;
259 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
260 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
drh9584f582015-11-04 20:22:37 +0000261 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
danielk197704103022009-02-03 16:51:24 +0000262 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
263 ){
264 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
265 u8 *zDestData = sqlite3PagerGetData(pDestPg);
266 u8 *zOut = &zDestData[iOff%nDestPgsz];
267
268 /* Copy the data from the source page into the destination page.
269 ** Then clear the Btree layer MemPage.isInit flag. Both this module
270 ** and the pager code use this trick (clearing the first byte
271 ** of the page 'extra' space to invalidate the Btree layers
272 ** cached parse of the page). MemPage.isInit is marked
273 ** "MUST BE FIRST" for this purpose.
274 */
275 memcpy(zOut, zIn, nCopy);
276 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
dan5cc3bea2012-12-21 16:15:35 +0000277 if( iOff==0 && bUpdate==0 ){
278 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
279 }
danielk197704103022009-02-03 16:51:24 +0000280 }
281 sqlite3PagerUnref(pDestPg);
282 }
283
284 return rc;
285}
286
287/*
danielk19773d0cbc32009-02-09 18:55:45 +0000288** If pFile is currently larger than iSize bytes, then truncate it to
289** exactly iSize bytes. If pFile is not larger than iSize bytes, then
290** this function is a no-op.
291**
292** Return SQLITE_OK if everything is successful, or an SQLite error
293** code if an error occurs.
294*/
295static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
296 i64 iCurrent;
297 int rc = sqlite3OsFileSize(pFile, &iCurrent);
298 if( rc==SQLITE_OK && iCurrent>iSize ){
299 rc = sqlite3OsTruncate(pFile, iSize);
300 }
301 return rc;
302}
303
304/*
danielk1977e70f4f62009-05-13 07:52:06 +0000305** Register this backup object with the associated source pager for
306** callbacks when pages are changed or the cache invalidated.
307*/
308static void attachBackupObject(sqlite3_backup *p){
309 sqlite3_backup **pp;
310 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
311 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
312 p->pNext = *pp;
313 *pp = p;
314 p->isAttached = 1;
315}
316
317/*
danielk197704103022009-02-03 16:51:24 +0000318** Copy nPage pages from the source b-tree to the destination.
319*/
320int sqlite3_backup_step(sqlite3_backup *p, int nPage){
321 int rc;
drh3289c5e2010-05-05 16:23:26 +0000322 int destMode; /* Destination journal mode */
drh5c10f772010-05-05 18:46:44 +0000323 int pgszSrc = 0; /* Source page size */
324 int pgszDest = 0; /* Destination page size */
danielk197704103022009-02-03 16:51:24 +0000325
drh9ca95732014-10-24 00:35:58 +0000326#ifdef SQLITE_ENABLE_API_ARMOR
327 if( p==0 ) return SQLITE_MISUSE_BKPT;
328#endif
danielk197704103022009-02-03 16:51:24 +0000329 sqlite3_mutex_enter(p->pSrcDb->mutex);
330 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000331 if( p->pDestDb ){
332 sqlite3_mutex_enter(p->pDestDb->mutex);
333 }
danielk197704103022009-02-03 16:51:24 +0000334
drh5c10f772010-05-05 18:46:44 +0000335 rc = p->rc;
danielk197703ab0352009-02-06 05:59:44 +0000336 if( !isFatalError(rc) ){
danielk197704103022009-02-03 16:51:24 +0000337 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
338 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
339 int ii; /* Iterator variable */
shane63207ab2009-02-04 01:49:30 +0000340 int nSrcPage = -1; /* Size of source db in pages */
danielk197704103022009-02-03 16:51:24 +0000341 int bCloseTrans = 0; /* True if src db requires unlocking */
342
343 /* If the source pager is currently in a write-transaction, return
danielk1977404ca072009-03-16 13:19:36 +0000344 ** SQLITE_BUSY immediately.
danielk197704103022009-02-03 16:51:24 +0000345 */
346 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
danielk1977404ca072009-03-16 13:19:36 +0000347 rc = SQLITE_BUSY;
danielk197703ab0352009-02-06 05:59:44 +0000348 }else{
349 rc = SQLITE_OK;
danielk197704103022009-02-03 16:51:24 +0000350 }
351
danielk197704103022009-02-03 16:51:24 +0000352 /* If there is no open read-transaction on the source database, open
353 ** one now. If a transaction is opened here, then it will be closed
354 ** before this function exits.
355 */
drh99744fa2020-08-25 19:09:07 +0000356 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
drhbb2d9b12018-06-06 16:28:40 +0000357 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
danielk197704103022009-02-03 16:51:24 +0000358 bCloseTrans = 1;
359 }
drh5c10f772010-05-05 18:46:44 +0000360
dan033564c2016-09-02 17:18:20 +0000361 /* If the destination database has not yet been locked (i.e. if this
362 ** is the first call to backup_step() for the current backup operation),
363 ** try to set its page size to the same as the source database. This
364 ** is especially important on ZipVFS systems, as in that case it is
365 ** not possible to create a database file that uses one page size by
366 ** writing to it with another. */
drh76729ed2016-09-02 21:17:51 +0000367 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
368 rc = SQLITE_NOMEM;
369 }
dan033564c2016-09-02 17:18:20 +0000370
371 /* Lock the destination database, if it is not locked already. */
372 if( SQLITE_OK==rc && p->bDestLocked==0
drhbb2d9b12018-06-06 16:28:40 +0000373 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
374 (int*)&p->iDestSchema))
dan033564c2016-09-02 17:18:20 +0000375 ){
376 p->bDestLocked = 1;
dan033564c2016-09-02 17:18:20 +0000377 }
378
drh5c10f772010-05-05 18:46:44 +0000379 /* Do not allow backup if the destination database is in WAL mode
380 ** and the page sizes are different between source and destination */
381 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
382 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
drh0b9b4302010-06-11 17:01:24 +0000383 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
drh5c10f772010-05-05 18:46:44 +0000384 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
385 rc = SQLITE_READONLY;
386 }
danielk197704103022009-02-03 16:51:24 +0000387
388 /* Now that there is a read-lock on the source database, query the
389 ** source pager for the number of pages in the database.
390 */
drhb1299152010-03-30 22:58:33 +0000391 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
392 assert( nSrcPage>=0 );
danielk197703ab0352009-02-06 05:59:44 +0000393 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
danielk197704103022009-02-03 16:51:24 +0000394 const Pgno iSrcPg = p->iNext; /* Source page number */
395 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
396 DbPage *pSrcPg; /* Source page object */
drh9584f582015-11-04 20:22:37 +0000397 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
danielk197704103022009-02-03 16:51:24 +0000398 if( rc==SQLITE_OK ){
dan5cc3bea2012-12-21 16:15:35 +0000399 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
danielk197704103022009-02-03 16:51:24 +0000400 sqlite3PagerUnref(pSrcPg);
401 }
402 }
403 p->iNext++;
404 }
405 if( rc==SQLITE_OK ){
406 p->nPagecount = nSrcPage;
407 p->nRemaining = nSrcPage+1-p->iNext;
shane63207ab2009-02-04 01:49:30 +0000408 if( p->iNext>(Pgno)nSrcPage ){
danielk197704103022009-02-03 16:51:24 +0000409 rc = SQLITE_DONE;
danielk1977e70f4f62009-05-13 07:52:06 +0000410 }else if( !p->isAttached ){
411 attachBackupObject(p);
danielk197704103022009-02-03 16:51:24 +0000412 }
413 }
414
drhf25cd712009-07-06 19:03:12 +0000415 /* Update the schema version field in the destination database. This
416 ** is to make sure that the schema-version really does change in
417 ** the case where the source and destination databases have the
418 ** same schema version.
419 */
drhc5dbffe2011-08-25 20:18:47 +0000420 if( rc==SQLITE_DONE ){
danb483eba2012-10-13 19:58:11 +0000421 if( nSrcPage==0 ){
422 rc = sqlite3BtreeNewDb(p->pDest);
423 nSrcPage = 1;
424 }
425 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
426 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
427 }
drhc5dbffe2011-08-25 20:18:47 +0000428 if( rc==SQLITE_OK ){
429 if( p->pDestDb ){
drh81028a42012-05-15 18:28:27 +0000430 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
drhc5dbffe2011-08-25 20:18:47 +0000431 }
432 if( destMode==PAGER_JOURNALMODE_WAL ){
433 rc = sqlite3BtreeSetVersion(p->pDest, 2);
434 }
drhd3a5c502009-02-03 22:51:06 +0000435 }
dan4b270402011-08-25 19:28:47 +0000436 if( rc==SQLITE_OK ){
437 int nDestTruncate;
438 /* Set nDestTruncate to the final number of pages in the destination
439 ** database. The complication here is that the destination page
440 ** size may be different to the source page size.
danielk197704103022009-02-03 16:51:24 +0000441 **
dan4b270402011-08-25 19:28:47 +0000442 ** If the source page size is smaller than the destination page size,
443 ** round up. In this case the call to sqlite3OsTruncate() below will
444 ** fix the size of the file. However it is important to call
445 ** sqlite3PagerTruncateImage() here so that any pages in the
446 ** destination file that lie beyond the nDestTruncate page mark are
447 ** journalled by PagerCommitPhaseOne() before they are destroyed
448 ** by the file truncation.
danielk197704103022009-02-03 16:51:24 +0000449 */
dan4b270402011-08-25 19:28:47 +0000450 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
451 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
452 if( pgszSrc<pgszDest ){
453 int ratio = pgszDest/pgszSrc;
454 nDestTruncate = (nSrcPage+ratio-1)/ratio;
455 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
456 nDestTruncate--;
drhc6aed542011-01-16 22:37:09 +0000457 }
dan4b270402011-08-25 19:28:47 +0000458 }else{
459 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
drhc6aed542011-01-16 22:37:09 +0000460 }
danb483eba2012-10-13 19:58:11 +0000461 assert( nDestTruncate>0 );
dan4d26d582011-01-25 18:19:24 +0000462
dan4b270402011-08-25 19:28:47 +0000463 if( pgszSrc<pgszDest ){
464 /* If the source page-size is smaller than the destination page-size,
465 ** two extra things may need to happen:
466 **
467 ** * The destination may need to be truncated, and
468 **
469 ** * Data stored on the pages immediately following the
470 ** pending-byte page in the source database may need to be
471 ** copied into the destination database.
472 */
473 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
474 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
danbc1a3c62013-02-23 16:40:46 +0000475 Pgno iPg;
476 int nDstPage;
dan4b270402011-08-25 19:28:47 +0000477 i64 iOff;
478 i64 iEnd;
479
480 assert( pFile );
danb483eba2012-10-13 19:58:11 +0000481 assert( nDestTruncate==0
482 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
dan4b270402011-08-25 19:28:47 +0000483 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
484 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
485 ));
486
danbc1a3c62013-02-23 16:40:46 +0000487 /* This block ensures that all data required to recreate the original
dan4b270402011-08-25 19:28:47 +0000488 ** database has been stored in the journal for pDestPager and the
489 ** journal synced to disk. So at this point we may safely modify
490 ** the database file in any way, knowing that if a power failure
491 ** occurs, the original database will be reconstructed from the
492 ** journal file. */
danbc1a3c62013-02-23 16:40:46 +0000493 sqlite3PagerPagecount(pDestPager, &nDstPage);
494 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
495 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
496 DbPage *pPg;
drh9584f582015-11-04 20:22:37 +0000497 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
danbc1a3c62013-02-23 16:40:46 +0000498 if( rc==SQLITE_OK ){
499 rc = sqlite3PagerWrite(pPg);
500 sqlite3PagerUnref(pPg);
501 }
502 }
503 }
dan896f99e2013-02-25 07:12:40 +0000504 if( rc==SQLITE_OK ){
505 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
506 }
dan4b270402011-08-25 19:28:47 +0000507
508 /* Write the extra pages and truncate the database file as required */
509 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
510 for(
511 iOff=PENDING_BYTE+pgszSrc;
512 rc==SQLITE_OK && iOff<iEnd;
513 iOff+=pgszSrc
514 ){
515 PgHdr *pSrcPg = 0;
516 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
drh9584f582015-11-04 20:22:37 +0000517 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
dan4b270402011-08-25 19:28:47 +0000518 if( rc==SQLITE_OK ){
519 u8 *zData = sqlite3PagerGetData(pSrcPg);
danf23da962013-03-23 21:00:41 +0000520 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
dan4b270402011-08-25 19:28:47 +0000521 }
522 sqlite3PagerUnref(pSrcPg);
523 }
524 if( rc==SQLITE_OK ){
525 rc = backupTruncateFile(pFile, iSize);
526 }
527
528 /* Sync the database file to disk. */
529 if( rc==SQLITE_OK ){
dan999cd082013-12-09 20:42:03 +0000530 rc = sqlite3PagerSync(pDestPager, 0);
dan4b270402011-08-25 19:28:47 +0000531 }
532 }else{
danbc1a3c62013-02-23 16:40:46 +0000533 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
dan4b270402011-08-25 19:28:47 +0000534 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
danielk197704103022009-02-03 16:51:24 +0000535 }
dan4b270402011-08-25 19:28:47 +0000536
537 /* Finish committing the transaction to the destination database. */
538 if( SQLITE_OK==rc
539 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
540 ){
541 rc = SQLITE_DONE;
542 }
danielk197704103022009-02-03 16:51:24 +0000543 }
544 }
545
546 /* If bCloseTrans is true, then this function opened a read transaction
547 ** on the source database. Close the read transaction here. There is
548 ** no need to check the return values of the btree methods here, as
549 ** "committing" a read-only transaction cannot fail.
550 */
551 if( bCloseTrans ){
552 TESTONLY( int rc2 );
553 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
dan60939d02011-03-29 15:40:55 +0000554 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
danielk197704103022009-02-03 16:51:24 +0000555 assert( rc2==SQLITE_OK );
556 }
557
danba3cbf32010-06-30 04:29:03 +0000558 if( rc==SQLITE_IOERR_NOMEM ){
mistachkinfad30392016-02-13 23:43:46 +0000559 rc = SQLITE_NOMEM_BKPT;
danba3cbf32010-06-30 04:29:03 +0000560 }
danielk197703ab0352009-02-06 05:59:44 +0000561 p->rc = rc;
danielk197704103022009-02-03 16:51:24 +0000562 }
drhd3a5c502009-02-03 22:51:06 +0000563 if( p->pDestDb ){
564 sqlite3_mutex_leave(p->pDestDb->mutex);
565 }
danielk197704103022009-02-03 16:51:24 +0000566 sqlite3BtreeLeave(p->pSrc);
567 sqlite3_mutex_leave(p->pSrcDb->mutex);
568 return rc;
569}
570
571/*
572** Release all resources associated with an sqlite3_backup* handle.
573*/
574int sqlite3_backup_finish(sqlite3_backup *p){
575 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
drhed688012012-06-21 15:51:42 +0000576 sqlite3 *pSrcDb; /* Source database connection */
danielk197704103022009-02-03 16:51:24 +0000577 int rc; /* Value to return */
578
579 /* Enter the mutexes */
drhdcd7db52009-05-14 19:26:51 +0000580 if( p==0 ) return SQLITE_OK;
drhed688012012-06-21 15:51:42 +0000581 pSrcDb = p->pSrcDb;
582 sqlite3_mutex_enter(pSrcDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000583 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000584 if( p->pDestDb ){
585 sqlite3_mutex_enter(p->pDestDb->mutex);
586 }
danielk197704103022009-02-03 16:51:24 +0000587
588 /* Detach this backup from the source pager. */
589 if( p->pDestDb ){
danielk1977e70f4f62009-05-13 07:52:06 +0000590 p->pSrc->nBackup--;
591 }
592 if( p->isAttached ){
danielk197704103022009-02-03 16:51:24 +0000593 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
drh55f66b32019-07-16 19:44:32 +0000594 assert( pp!=0 );
danielk197704103022009-02-03 16:51:24 +0000595 while( *pp!=p ){
596 pp = &(*pp)->pNext;
drh55f66b32019-07-16 19:44:32 +0000597 assert( pp!=0 );
danielk197704103022009-02-03 16:51:24 +0000598 }
599 *pp = p->pNext;
danielk197704103022009-02-03 16:51:24 +0000600 }
601
602 /* If a transaction is still open on the Btree, roll it back. */
drh47b7fc72014-11-11 01:33:57 +0000603 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
danielk197704103022009-02-03 16:51:24 +0000604
605 /* Set the error code of the destination database handle. */
606 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
drhd3a5c502009-02-03 22:51:06 +0000607 if( p->pDestDb ){
drh13f40da2014-08-22 18:00:11 +0000608 sqlite3Error(p->pDestDb, rc);
drha3cc0072013-12-13 16:23:55 +0000609
610 /* Exit the mutexes and free the backup context structure. */
drh4245c402012-06-02 14:32:21 +0000611 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
drhd3a5c502009-02-03 22:51:06 +0000612 }
danielk197704103022009-02-03 16:51:24 +0000613 sqlite3BtreeLeave(p->pSrc);
614 if( p->pDestDb ){
drh9f129f42010-08-31 15:27:32 +0000615 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
616 ** call to sqlite3_backup_init() and is destroyed by a call to
617 ** sqlite3_backup_finish(). */
danielk197704103022009-02-03 16:51:24 +0000618 sqlite3_free(p);
619 }
drh4245c402012-06-02 14:32:21 +0000620 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
danielk197704103022009-02-03 16:51:24 +0000621 return rc;
622}
623
624/*
625** Return the number of pages still to be backed up as of the most recent
626** call to sqlite3_backup_step().
627*/
628int sqlite3_backup_remaining(sqlite3_backup *p){
drh9ca95732014-10-24 00:35:58 +0000629#ifdef SQLITE_ENABLE_API_ARMOR
630 if( p==0 ){
631 (void)SQLITE_MISUSE_BKPT;
632 return 0;
633 }
634#endif
danielk197704103022009-02-03 16:51:24 +0000635 return p->nRemaining;
636}
637
638/*
639** Return the total number of pages in the source database as of the most
640** recent call to sqlite3_backup_step().
641*/
642int sqlite3_backup_pagecount(sqlite3_backup *p){
drh9ca95732014-10-24 00:35:58 +0000643#ifdef SQLITE_ENABLE_API_ARMOR
644 if( p==0 ){
645 (void)SQLITE_MISUSE_BKPT;
646 return 0;
647 }
648#endif
danielk197704103022009-02-03 16:51:24 +0000649 return p->nPagecount;
650}
651
652/*
653** This function is called after the contents of page iPage of the
654** source database have been modified. If page iPage has already been
655** copied into the destination database, then the data written to the
656** destination is now invalidated. The destination copy of iPage needs
657** to be updated with the new data before the backup operation is
658** complete.
659**
660** It is assumed that the mutex associated with the BtShared object
661** corresponding to the source database is held when this function is
662** called.
663*/
drh14681b72015-07-01 19:59:36 +0000664static SQLITE_NOINLINE void backupUpdate(
665 sqlite3_backup *p,
666 Pgno iPage,
667 const u8 *aData
668){
669 assert( p!=0 );
670 do{
danielk197704103022009-02-03 16:51:24 +0000671 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
danielk197703ab0352009-02-06 05:59:44 +0000672 if( !isFatalError(p->rc) && iPage<p->iNext ){
danielk197704103022009-02-03 16:51:24 +0000673 /* The backup process p has already copied page iPage. But now it
674 ** has been modified by a transaction on the source pager. Copy
675 ** the new data into the backup.
676 */
drh2b89fbc2011-04-09 02:09:44 +0000677 int rc;
drh806ebcb2011-04-09 17:53:30 +0000678 assert( p->pDestDb );
679 sqlite3_mutex_enter(p->pDestDb->mutex);
dan5cc3bea2012-12-21 16:15:35 +0000680 rc = backupOnePage(p, iPage, aData, 1);
drh806ebcb2011-04-09 17:53:30 +0000681 sqlite3_mutex_leave(p->pDestDb->mutex);
danielk197703ab0352009-02-06 05:59:44 +0000682 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
danielk197704103022009-02-03 16:51:24 +0000683 if( rc!=SQLITE_OK ){
684 p->rc = rc;
685 }
686 }
drh14681b72015-07-01 19:59:36 +0000687 }while( (p = p->pNext)!=0 );
688}
689void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
690 if( pBackup ) backupUpdate(pBackup, iPage, aData);
danielk197704103022009-02-03 16:51:24 +0000691}
692
693/*
694** Restart the backup process. This is called when the pager layer
695** detects that the database has been modified by an external database
696** connection. In this case there is no way of knowing which of the
697** pages that have been copied into the destination database are still
698** valid and which are not, so the entire process needs to be restarted.
699**
700** It is assumed that the mutex associated with the BtShared object
701** corresponding to the source database is held when this function is
702** called.
703*/
704void sqlite3BackupRestart(sqlite3_backup *pBackup){
705 sqlite3_backup *p; /* Iterator variable */
706 for(p=pBackup; p; p=p->pNext){
707 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
708 p->iNext = 1;
709 }
710}
711
712#ifndef SQLITE_OMIT_VACUUM
713/*
714** Copy the complete content of pBtFrom into pBtTo. A transaction
715** must be active for both files.
716**
717** The size of file pTo may be reduced by this operation. If anything
718** goes wrong, the transaction on pTo is rolled back. If successful, the
719** transaction is committed before returning.
720*/
721int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
722 int rc;
danc5f20a02011-10-07 16:57:59 +0000723 sqlite3_file *pFd; /* File descriptor for database pTo */
danielk197704103022009-02-03 16:51:24 +0000724 sqlite3_backup b;
725 sqlite3BtreeEnter(pTo);
726 sqlite3BtreeEnter(pFrom);
727
drh99744fa2020-08-25 19:09:07 +0000728 assert( sqlite3BtreeTxnState(pTo)==SQLITE_TXN_WRITE );
danc5f20a02011-10-07 16:57:59 +0000729 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
730 if( pFd->pMethods ){
731 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
drh2bfe5b32012-01-10 16:40:50 +0000732 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
733 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
734 if( rc ) goto copy_finished;
danc5f20a02011-10-07 16:57:59 +0000735 }
736
danielk197704103022009-02-03 16:51:24 +0000737 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
738 ** to 0. This is used by the implementations of sqlite3_backup_step()
739 ** and sqlite3_backup_finish() to detect that they are being called
740 ** from this function, not directly by the user.
741 */
742 memset(&b, 0, sizeof(b));
743 b.pSrcDb = pFrom->db;
744 b.pSrc = pFrom;
745 b.pDest = pTo;
746 b.iNext = 1;
747
748 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
749 ** file. By passing this as the number of pages to copy to
750 ** sqlite3_backup_step(), we can guarantee that the copy finishes
751 ** within a single call (unless an error occurs). The assert() statement
752 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
dan43c1ce32016-08-05 16:16:26 +0000753 ** or an error code. */
danielk197704103022009-02-03 16:51:24 +0000754 sqlite3_backup_step(&b, 0x7FFFFFFF);
755 assert( b.rc!=SQLITE_OK );
dan43c1ce32016-08-05 16:16:26 +0000756
danielk197704103022009-02-03 16:51:24 +0000757 rc = sqlite3_backup_finish(&b);
758 if( rc==SQLITE_OK ){
drhc9166342012-01-05 23:32:06 +0000759 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
dan43c1ce32016-08-05 16:16:26 +0000760 }else{
761 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
danielk197704103022009-02-03 16:51:24 +0000762 }
763
drh99744fa2020-08-25 19:09:07 +0000764 assert( sqlite3BtreeTxnState(pTo)!=SQLITE_TXN_WRITE );
drh2bfe5b32012-01-10 16:40:50 +0000765copy_finished:
danielk197704103022009-02-03 16:51:24 +0000766 sqlite3BtreeLeave(pFrom);
767 sqlite3BtreeLeave(pTo);
768 return rc;
769}
770#endif /* SQLITE_OMIT_VACUUM */