blob: 2b286de6bda0d2ef213d97b957d7ac62d80f5733 [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;
drhcb43a932016-10-03 01:21:51 +000088 memset(&sParse, 0, sizeof(sParse));
89 sParse.db = pDb;
90 if( sqlite3OpenTempDatabase(&sParse) ){
91 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
92 rc = SQLITE_ERROR;
drhe98c9042009-06-02 21:31:38 +000093 }
drhcb43a932016-10-03 01:21:51 +000094 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
95 sqlite3ParserReset(&sParse);
drhe98c9042009-06-02 21:31:38 +000096 if( rc ){
danielk197704103022009-02-03 16:51:24 +000097 return 0;
98 }
danielk197704103022009-02-03 16:51:24 +000099 }
100
101 if( i<0 ){
drh13f40da2014-08-22 18:00:11 +0000102 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
danielk197704103022009-02-03 16:51:24 +0000103 return 0;
104 }
105
106 return pDb->aDb[i].pBt;
107}
108
109/*
drhca94d8b2011-01-11 17:38:03 +0000110** Attempt to set the page size of the destination to match the page size
111** of the source.
112*/
113static int setDestPgsz(sqlite3_backup *p){
114 int rc;
drhe937df82020-05-07 01:56:57 +0000115 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
drhca94d8b2011-01-11 17:38:03 +0000116 return rc;
117}
118
119/*
danfad01992014-11-13 14:18:25 +0000120** Check that there is no open read-transaction on the b-tree passed as the
121** second argument. If there is not, return SQLITE_OK. Otherwise, if there
122** is an open read-transaction, return SQLITE_ERROR and leave an error
123** message in database handle db.
124*/
125static int checkReadTransaction(sqlite3 *db, Btree *p){
drh99744fa2020-08-25 19:09:07 +0000126 if( sqlite3BtreeTxnState(p)!=SQLITE_TXN_NONE ){
danfad01992014-11-13 14:18:25 +0000127 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
128 return SQLITE_ERROR;
129 }
130 return SQLITE_OK;
131}
132
133/*
danielk197704103022009-02-03 16:51:24 +0000134** Create an sqlite3_backup process to copy the contents of zSrcDb from
135** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
136** a pointer to the new sqlite3_backup object.
137**
138** If an error occurs, NULL is returned and an error code and error message
139** stored in database handle pDestDb.
140*/
141sqlite3_backup *sqlite3_backup_init(
142 sqlite3* pDestDb, /* Database to write to */
143 const char *zDestDb, /* Name of database within pDestDb */
144 sqlite3* pSrcDb, /* Database connection to read from */
145 const char *zSrcDb /* Name of database within pSrcDb */
146){
147 sqlite3_backup *p; /* Value to return */
148
drh9ca95732014-10-24 00:35:58 +0000149#ifdef SQLITE_ENABLE_API_ARMOR
150 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
151 (void)SQLITE_MISUSE_BKPT;
152 return 0;
153 }
154#endif
155
danielk197704103022009-02-03 16:51:24 +0000156 /* Lock the source database handle. The destination database
drh662c58c2009-02-03 21:13:07 +0000157 ** handle is not locked in this routine, but it is locked in
158 ** sqlite3_backup_step(). The user is required to ensure that no
danielk197704103022009-02-03 16:51:24 +0000159 ** other thread accesses the destination handle for the duration
drh662c58c2009-02-03 21:13:07 +0000160 ** of the backup operation. Any attempt to use the destination
161 ** database connection while a backup is in progress may cause
162 ** a malfunction or a deadlock.
danielk197704103022009-02-03 16:51:24 +0000163 */
164 sqlite3_mutex_enter(pSrcDb->mutex);
drheef1eb02009-02-04 16:56:19 +0000165 sqlite3_mutex_enter(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000166
167 if( pSrcDb==pDestDb ){
drh13f40da2014-08-22 18:00:11 +0000168 sqlite3ErrorWithMsg(
drhb309bec2009-02-04 17:40:57 +0000169 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
danielk197704103022009-02-03 16:51:24 +0000170 );
171 p = 0;
172 }else {
drh9f129f42010-08-31 15:27:32 +0000173 /* Allocate space for a new sqlite3_backup object...
174 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
175 ** call to sqlite3_backup_init() and is destroyed by a call to
176 ** sqlite3_backup_finish(). */
dan6809c962012-07-30 14:53:54 +0000177 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
danielk197704103022009-02-03 16:51:24 +0000178 if( !p ){
mistachkinfad30392016-02-13 23:43:46 +0000179 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
danielk197704103022009-02-03 16:51:24 +0000180 }
181 }
182
183 /* If the allocation succeeded, populate the new object. */
184 if( p ){
danielk197704103022009-02-03 16:51:24 +0000185 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
186 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
187 p->pDestDb = pDestDb;
188 p->pSrcDb = pSrcDb;
189 p->iNext = 1;
danielk1977e70f4f62009-05-13 07:52:06 +0000190 p->isAttached = 0;
danielk197704103022009-02-03 16:51:24 +0000191
danfad01992014-11-13 14:18:25 +0000192 if( 0==p->pSrc || 0==p->pDest
danfad01992014-11-13 14:18:25 +0000193 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
194 ){
drhca94d8b2011-01-11 17:38:03 +0000195 /* One (or both) of the named databases did not exist or an OOM
danfad01992014-11-13 14:18:25 +0000196 ** error was hit. Or there is a transaction open on the destination
197 ** database. The error has already been written into the pDestDb
198 ** handle. All that is left to do here is free the sqlite3_backup
199 ** structure. */
danielk197704103022009-02-03 16:51:24 +0000200 sqlite3_free(p);
201 p = 0;
202 }
203 }
danielk197704103022009-02-03 16:51:24 +0000204 if( p ){
danielk197704103022009-02-03 16:51:24 +0000205 p->pSrc->nBackup++;
206 }
207
drheef1eb02009-02-04 16:56:19 +0000208 sqlite3_mutex_leave(pDestDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000209 sqlite3_mutex_leave(pSrcDb->mutex);
210 return p;
211}
212
213/*
danielk197703ab0352009-02-06 05:59:44 +0000214** Argument rc is an SQLite error code. Return true if this error is
215** considered fatal if encountered during a backup operation. All errors
216** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
217*/
218static int isFatalError(int rc){
drhdcd7db52009-05-14 19:26:51 +0000219 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
danielk197703ab0352009-02-06 05:59:44 +0000220}
221
222/*
danielk197704103022009-02-03 16:51:24 +0000223** Parameter zSrcData points to a buffer containing the data for
224** page iSrcPg from the source database. Copy this data into the
225** destination database.
226*/
dan5cc3bea2012-12-21 16:15:35 +0000227static int backupOnePage(
228 sqlite3_backup *p, /* Backup handle */
229 Pgno iSrcPg, /* Source database page to backup */
230 const u8 *zSrcData, /* Source database page data */
231 int bUpdate /* True for an update, false otherwise */
232){
danielk197704103022009-02-03 16:51:24 +0000233 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
234 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
235 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
236 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
237 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
danielk197704103022009-02-03 16:51:24 +0000238 int rc = SQLITE_OK;
239 i64 iOff;
240
dan0094f372012-09-28 20:23:42 +0000241 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
danielk197704103022009-02-03 16:51:24 +0000242 assert( p->bDestLocked );
danielk197703ab0352009-02-06 05:59:44 +0000243 assert( !isFatalError(p->rc) );
danielk197704103022009-02-03 16:51:24 +0000244 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
245 assert( zSrcData );
246
247 /* Catch the case where the destination is an in-memory database and the
248 ** page sizes of the source and destination differ.
249 */
drh3289c5e2010-05-05 16:23:26 +0000250 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
danielk197704103022009-02-03 16:51:24 +0000251 rc = SQLITE_READONLY;
252 }
253
254 /* This loop runs once for each destination page spanned by the source
255 ** page. For each iteration, variable iOff is set to the byte offset
256 ** of the destination page.
257 */
258 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
259 DbPage *pDestPg = 0;
260 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
261 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
drh9584f582015-11-04 20:22:37 +0000262 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
danielk197704103022009-02-03 16:51:24 +0000263 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
264 ){
265 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
266 u8 *zDestData = sqlite3PagerGetData(pDestPg);
267 u8 *zOut = &zDestData[iOff%nDestPgsz];
268
269 /* Copy the data from the source page into the destination page.
270 ** Then clear the Btree layer MemPage.isInit flag. Both this module
271 ** and the pager code use this trick (clearing the first byte
272 ** of the page 'extra' space to invalidate the Btree layers
273 ** cached parse of the page). MemPage.isInit is marked
274 ** "MUST BE FIRST" for this purpose.
275 */
276 memcpy(zOut, zIn, nCopy);
277 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
dan5cc3bea2012-12-21 16:15:35 +0000278 if( iOff==0 && bUpdate==0 ){
279 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
280 }
danielk197704103022009-02-03 16:51:24 +0000281 }
282 sqlite3PagerUnref(pDestPg);
283 }
284
285 return rc;
286}
287
288/*
danielk19773d0cbc32009-02-09 18:55:45 +0000289** If pFile is currently larger than iSize bytes, then truncate it to
290** exactly iSize bytes. If pFile is not larger than iSize bytes, then
291** this function is a no-op.
292**
293** Return SQLITE_OK if everything is successful, or an SQLite error
294** code if an error occurs.
295*/
296static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
297 i64 iCurrent;
298 int rc = sqlite3OsFileSize(pFile, &iCurrent);
299 if( rc==SQLITE_OK && iCurrent>iSize ){
300 rc = sqlite3OsTruncate(pFile, iSize);
301 }
302 return rc;
303}
304
305/*
danielk1977e70f4f62009-05-13 07:52:06 +0000306** Register this backup object with the associated source pager for
307** callbacks when pages are changed or the cache invalidated.
308*/
309static void attachBackupObject(sqlite3_backup *p){
310 sqlite3_backup **pp;
311 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
312 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
313 p->pNext = *pp;
314 *pp = p;
315 p->isAttached = 1;
316}
317
318/*
danielk197704103022009-02-03 16:51:24 +0000319** Copy nPage pages from the source b-tree to the destination.
320*/
321int sqlite3_backup_step(sqlite3_backup *p, int nPage){
322 int rc;
drh3289c5e2010-05-05 16:23:26 +0000323 int destMode; /* Destination journal mode */
drh5c10f772010-05-05 18:46:44 +0000324 int pgszSrc = 0; /* Source page size */
325 int pgszDest = 0; /* Destination page size */
danielk197704103022009-02-03 16:51:24 +0000326
drh9ca95732014-10-24 00:35:58 +0000327#ifdef SQLITE_ENABLE_API_ARMOR
328 if( p==0 ) return SQLITE_MISUSE_BKPT;
329#endif
danielk197704103022009-02-03 16:51:24 +0000330 sqlite3_mutex_enter(p->pSrcDb->mutex);
331 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000332 if( p->pDestDb ){
333 sqlite3_mutex_enter(p->pDestDb->mutex);
334 }
danielk197704103022009-02-03 16:51:24 +0000335
drh5c10f772010-05-05 18:46:44 +0000336 rc = p->rc;
danielk197703ab0352009-02-06 05:59:44 +0000337 if( !isFatalError(rc) ){
danielk197704103022009-02-03 16:51:24 +0000338 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
339 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
340 int ii; /* Iterator variable */
shane63207ab2009-02-04 01:49:30 +0000341 int nSrcPage = -1; /* Size of source db in pages */
danielk197704103022009-02-03 16:51:24 +0000342 int bCloseTrans = 0; /* True if src db requires unlocking */
343
344 /* If the source pager is currently in a write-transaction, return
danielk1977404ca072009-03-16 13:19:36 +0000345 ** SQLITE_BUSY immediately.
danielk197704103022009-02-03 16:51:24 +0000346 */
347 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
danielk1977404ca072009-03-16 13:19:36 +0000348 rc = SQLITE_BUSY;
danielk197703ab0352009-02-06 05:59:44 +0000349 }else{
350 rc = SQLITE_OK;
danielk197704103022009-02-03 16:51:24 +0000351 }
352
danielk197704103022009-02-03 16:51:24 +0000353 /* If there is no open read-transaction on the source database, open
354 ** one now. If a transaction is opened here, then it will be closed
355 ** before this function exits.
356 */
drh99744fa2020-08-25 19:09:07 +0000357 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
drhbb2d9b12018-06-06 16:28:40 +0000358 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
danielk197704103022009-02-03 16:51:24 +0000359 bCloseTrans = 1;
360 }
drh5c10f772010-05-05 18:46:44 +0000361
dan033564c2016-09-02 17:18:20 +0000362 /* If the destination database has not yet been locked (i.e. if this
363 ** is the first call to backup_step() for the current backup operation),
364 ** try to set its page size to the same as the source database. This
365 ** is especially important on ZipVFS systems, as in that case it is
366 ** not possible to create a database file that uses one page size by
367 ** writing to it with another. */
drh76729ed2016-09-02 21:17:51 +0000368 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
369 rc = SQLITE_NOMEM;
370 }
dan033564c2016-09-02 17:18:20 +0000371
372 /* Lock the destination database, if it is not locked already. */
373 if( SQLITE_OK==rc && p->bDestLocked==0
drhbb2d9b12018-06-06 16:28:40 +0000374 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
375 (int*)&p->iDestSchema))
dan033564c2016-09-02 17:18:20 +0000376 ){
377 p->bDestLocked = 1;
dan033564c2016-09-02 17:18:20 +0000378 }
379
drh5c10f772010-05-05 18:46:44 +0000380 /* Do not allow backup if the destination database is in WAL mode
381 ** and the page sizes are different between source and destination */
382 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
383 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
drh0b9b4302010-06-11 17:01:24 +0000384 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
drh5c10f772010-05-05 18:46:44 +0000385 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
386 rc = SQLITE_READONLY;
387 }
danielk197704103022009-02-03 16:51:24 +0000388
389 /* Now that there is a read-lock on the source database, query the
390 ** source pager for the number of pages in the database.
391 */
drhb1299152010-03-30 22:58:33 +0000392 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
393 assert( nSrcPage>=0 );
danielk197703ab0352009-02-06 05:59:44 +0000394 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
danielk197704103022009-02-03 16:51:24 +0000395 const Pgno iSrcPg = p->iNext; /* Source page number */
396 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
397 DbPage *pSrcPg; /* Source page object */
drh9584f582015-11-04 20:22:37 +0000398 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
danielk197704103022009-02-03 16:51:24 +0000399 if( rc==SQLITE_OK ){
dan5cc3bea2012-12-21 16:15:35 +0000400 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
danielk197704103022009-02-03 16:51:24 +0000401 sqlite3PagerUnref(pSrcPg);
402 }
403 }
404 p->iNext++;
405 }
406 if( rc==SQLITE_OK ){
407 p->nPagecount = nSrcPage;
408 p->nRemaining = nSrcPage+1-p->iNext;
shane63207ab2009-02-04 01:49:30 +0000409 if( p->iNext>(Pgno)nSrcPage ){
danielk197704103022009-02-03 16:51:24 +0000410 rc = SQLITE_DONE;
danielk1977e70f4f62009-05-13 07:52:06 +0000411 }else if( !p->isAttached ){
412 attachBackupObject(p);
danielk197704103022009-02-03 16:51:24 +0000413 }
414 }
415
drhf25cd712009-07-06 19:03:12 +0000416 /* Update the schema version field in the destination database. This
417 ** is to make sure that the schema-version really does change in
418 ** the case where the source and destination databases have the
419 ** same schema version.
420 */
drhc5dbffe2011-08-25 20:18:47 +0000421 if( rc==SQLITE_DONE ){
danb483eba2012-10-13 19:58:11 +0000422 if( nSrcPage==0 ){
423 rc = sqlite3BtreeNewDb(p->pDest);
424 nSrcPage = 1;
425 }
426 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
427 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
428 }
drhc5dbffe2011-08-25 20:18:47 +0000429 if( rc==SQLITE_OK ){
430 if( p->pDestDb ){
drh81028a42012-05-15 18:28:27 +0000431 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
drhc5dbffe2011-08-25 20:18:47 +0000432 }
433 if( destMode==PAGER_JOURNALMODE_WAL ){
434 rc = sqlite3BtreeSetVersion(p->pDest, 2);
435 }
drhd3a5c502009-02-03 22:51:06 +0000436 }
dan4b270402011-08-25 19:28:47 +0000437 if( rc==SQLITE_OK ){
438 int nDestTruncate;
439 /* Set nDestTruncate to the final number of pages in the destination
440 ** database. The complication here is that the destination page
441 ** size may be different to the source page size.
danielk197704103022009-02-03 16:51:24 +0000442 **
dan4b270402011-08-25 19:28:47 +0000443 ** If the source page size is smaller than the destination page size,
444 ** round up. In this case the call to sqlite3OsTruncate() below will
445 ** fix the size of the file. However it is important to call
446 ** sqlite3PagerTruncateImage() here so that any pages in the
447 ** destination file that lie beyond the nDestTruncate page mark are
448 ** journalled by PagerCommitPhaseOne() before they are destroyed
449 ** by the file truncation.
danielk197704103022009-02-03 16:51:24 +0000450 */
dan4b270402011-08-25 19:28:47 +0000451 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
452 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
453 if( pgszSrc<pgszDest ){
454 int ratio = pgszDest/pgszSrc;
455 nDestTruncate = (nSrcPage+ratio-1)/ratio;
456 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
457 nDestTruncate--;
drhc6aed542011-01-16 22:37:09 +0000458 }
dan4b270402011-08-25 19:28:47 +0000459 }else{
460 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
drhc6aed542011-01-16 22:37:09 +0000461 }
danb483eba2012-10-13 19:58:11 +0000462 assert( nDestTruncate>0 );
dan4d26d582011-01-25 18:19:24 +0000463
dan4b270402011-08-25 19:28:47 +0000464 if( pgszSrc<pgszDest ){
465 /* If the source page-size is smaller than the destination page-size,
466 ** two extra things may need to happen:
467 **
468 ** * The destination may need to be truncated, and
469 **
470 ** * Data stored on the pages immediately following the
471 ** pending-byte page in the source database may need to be
472 ** copied into the destination database.
473 */
474 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
475 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
danbc1a3c62013-02-23 16:40:46 +0000476 Pgno iPg;
477 int nDstPage;
dan4b270402011-08-25 19:28:47 +0000478 i64 iOff;
479 i64 iEnd;
480
481 assert( pFile );
danb483eba2012-10-13 19:58:11 +0000482 assert( nDestTruncate==0
483 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
dan4b270402011-08-25 19:28:47 +0000484 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
485 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
486 ));
487
danbc1a3c62013-02-23 16:40:46 +0000488 /* This block ensures that all data required to recreate the original
dan4b270402011-08-25 19:28:47 +0000489 ** database has been stored in the journal for pDestPager and the
490 ** journal synced to disk. So at this point we may safely modify
491 ** the database file in any way, knowing that if a power failure
492 ** occurs, the original database will be reconstructed from the
493 ** journal file. */
danbc1a3c62013-02-23 16:40:46 +0000494 sqlite3PagerPagecount(pDestPager, &nDstPage);
495 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
496 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
497 DbPage *pPg;
drh9584f582015-11-04 20:22:37 +0000498 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
danbc1a3c62013-02-23 16:40:46 +0000499 if( rc==SQLITE_OK ){
500 rc = sqlite3PagerWrite(pPg);
501 sqlite3PagerUnref(pPg);
502 }
503 }
504 }
dan896f99e2013-02-25 07:12:40 +0000505 if( rc==SQLITE_OK ){
506 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
507 }
dan4b270402011-08-25 19:28:47 +0000508
509 /* Write the extra pages and truncate the database file as required */
510 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
511 for(
512 iOff=PENDING_BYTE+pgszSrc;
513 rc==SQLITE_OK && iOff<iEnd;
514 iOff+=pgszSrc
515 ){
516 PgHdr *pSrcPg = 0;
517 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
drh9584f582015-11-04 20:22:37 +0000518 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
dan4b270402011-08-25 19:28:47 +0000519 if( rc==SQLITE_OK ){
520 u8 *zData = sqlite3PagerGetData(pSrcPg);
danf23da962013-03-23 21:00:41 +0000521 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
dan4b270402011-08-25 19:28:47 +0000522 }
523 sqlite3PagerUnref(pSrcPg);
524 }
525 if( rc==SQLITE_OK ){
526 rc = backupTruncateFile(pFile, iSize);
527 }
528
529 /* Sync the database file to disk. */
530 if( rc==SQLITE_OK ){
dan999cd082013-12-09 20:42:03 +0000531 rc = sqlite3PagerSync(pDestPager, 0);
dan4b270402011-08-25 19:28:47 +0000532 }
533 }else{
danbc1a3c62013-02-23 16:40:46 +0000534 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
dan4b270402011-08-25 19:28:47 +0000535 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
danielk197704103022009-02-03 16:51:24 +0000536 }
dan4b270402011-08-25 19:28:47 +0000537
538 /* Finish committing the transaction to the destination database. */
539 if( SQLITE_OK==rc
540 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
541 ){
542 rc = SQLITE_DONE;
543 }
danielk197704103022009-02-03 16:51:24 +0000544 }
545 }
546
547 /* If bCloseTrans is true, then this function opened a read transaction
548 ** on the source database. Close the read transaction here. There is
549 ** no need to check the return values of the btree methods here, as
550 ** "committing" a read-only transaction cannot fail.
551 */
552 if( bCloseTrans ){
553 TESTONLY( int rc2 );
554 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
dan60939d02011-03-29 15:40:55 +0000555 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
danielk197704103022009-02-03 16:51:24 +0000556 assert( rc2==SQLITE_OK );
557 }
558
danba3cbf32010-06-30 04:29:03 +0000559 if( rc==SQLITE_IOERR_NOMEM ){
mistachkinfad30392016-02-13 23:43:46 +0000560 rc = SQLITE_NOMEM_BKPT;
danba3cbf32010-06-30 04:29:03 +0000561 }
danielk197703ab0352009-02-06 05:59:44 +0000562 p->rc = rc;
danielk197704103022009-02-03 16:51:24 +0000563 }
drhd3a5c502009-02-03 22:51:06 +0000564 if( p->pDestDb ){
565 sqlite3_mutex_leave(p->pDestDb->mutex);
566 }
danielk197704103022009-02-03 16:51:24 +0000567 sqlite3BtreeLeave(p->pSrc);
568 sqlite3_mutex_leave(p->pSrcDb->mutex);
569 return rc;
570}
571
572/*
573** Release all resources associated with an sqlite3_backup* handle.
574*/
575int sqlite3_backup_finish(sqlite3_backup *p){
576 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
drhed688012012-06-21 15:51:42 +0000577 sqlite3 *pSrcDb; /* Source database connection */
danielk197704103022009-02-03 16:51:24 +0000578 int rc; /* Value to return */
579
580 /* Enter the mutexes */
drhdcd7db52009-05-14 19:26:51 +0000581 if( p==0 ) return SQLITE_OK;
drhed688012012-06-21 15:51:42 +0000582 pSrcDb = p->pSrcDb;
583 sqlite3_mutex_enter(pSrcDb->mutex);
danielk197704103022009-02-03 16:51:24 +0000584 sqlite3BtreeEnter(p->pSrc);
drhd3a5c502009-02-03 22:51:06 +0000585 if( p->pDestDb ){
586 sqlite3_mutex_enter(p->pDestDb->mutex);
587 }
danielk197704103022009-02-03 16:51:24 +0000588
589 /* Detach this backup from the source pager. */
590 if( p->pDestDb ){
danielk1977e70f4f62009-05-13 07:52:06 +0000591 p->pSrc->nBackup--;
592 }
593 if( p->isAttached ){
danielk197704103022009-02-03 16:51:24 +0000594 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
drh55f66b32019-07-16 19:44:32 +0000595 assert( pp!=0 );
danielk197704103022009-02-03 16:51:24 +0000596 while( *pp!=p ){
597 pp = &(*pp)->pNext;
drh55f66b32019-07-16 19:44:32 +0000598 assert( pp!=0 );
danielk197704103022009-02-03 16:51:24 +0000599 }
600 *pp = p->pNext;
danielk197704103022009-02-03 16:51:24 +0000601 }
602
603 /* If a transaction is still open on the Btree, roll it back. */
drh47b7fc72014-11-11 01:33:57 +0000604 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
danielk197704103022009-02-03 16:51:24 +0000605
606 /* Set the error code of the destination database handle. */
607 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
drhd3a5c502009-02-03 22:51:06 +0000608 if( p->pDestDb ){
drh13f40da2014-08-22 18:00:11 +0000609 sqlite3Error(p->pDestDb, rc);
drha3cc0072013-12-13 16:23:55 +0000610
611 /* Exit the mutexes and free the backup context structure. */
drh4245c402012-06-02 14:32:21 +0000612 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
drhd3a5c502009-02-03 22:51:06 +0000613 }
danielk197704103022009-02-03 16:51:24 +0000614 sqlite3BtreeLeave(p->pSrc);
615 if( p->pDestDb ){
drh9f129f42010-08-31 15:27:32 +0000616 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
617 ** call to sqlite3_backup_init() and is destroyed by a call to
618 ** sqlite3_backup_finish(). */
danielk197704103022009-02-03 16:51:24 +0000619 sqlite3_free(p);
620 }
drh4245c402012-06-02 14:32:21 +0000621 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
danielk197704103022009-02-03 16:51:24 +0000622 return rc;
623}
624
625/*
626** Return the number of pages still to be backed up as of the most recent
627** call to sqlite3_backup_step().
628*/
629int sqlite3_backup_remaining(sqlite3_backup *p){
drh9ca95732014-10-24 00:35:58 +0000630#ifdef SQLITE_ENABLE_API_ARMOR
631 if( p==0 ){
632 (void)SQLITE_MISUSE_BKPT;
633 return 0;
634 }
635#endif
danielk197704103022009-02-03 16:51:24 +0000636 return p->nRemaining;
637}
638
639/*
640** Return the total number of pages in the source database as of the most
641** recent call to sqlite3_backup_step().
642*/
643int sqlite3_backup_pagecount(sqlite3_backup *p){
drh9ca95732014-10-24 00:35:58 +0000644#ifdef SQLITE_ENABLE_API_ARMOR
645 if( p==0 ){
646 (void)SQLITE_MISUSE_BKPT;
647 return 0;
648 }
649#endif
danielk197704103022009-02-03 16:51:24 +0000650 return p->nPagecount;
651}
652
653/*
654** This function is called after the contents of page iPage of the
655** source database have been modified. If page iPage has already been
656** copied into the destination database, then the data written to the
657** destination is now invalidated. The destination copy of iPage needs
658** to be updated with the new data before the backup operation is
659** complete.
660**
661** It is assumed that the mutex associated with the BtShared object
662** corresponding to the source database is held when this function is
663** called.
664*/
drh14681b72015-07-01 19:59:36 +0000665static SQLITE_NOINLINE void backupUpdate(
666 sqlite3_backup *p,
667 Pgno iPage,
668 const u8 *aData
669){
670 assert( p!=0 );
671 do{
danielk197704103022009-02-03 16:51:24 +0000672 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
danielk197703ab0352009-02-06 05:59:44 +0000673 if( !isFatalError(p->rc) && iPage<p->iNext ){
danielk197704103022009-02-03 16:51:24 +0000674 /* The backup process p has already copied page iPage. But now it
675 ** has been modified by a transaction on the source pager. Copy
676 ** the new data into the backup.
677 */
drh2b89fbc2011-04-09 02:09:44 +0000678 int rc;
drh806ebcb2011-04-09 17:53:30 +0000679 assert( p->pDestDb );
680 sqlite3_mutex_enter(p->pDestDb->mutex);
dan5cc3bea2012-12-21 16:15:35 +0000681 rc = backupOnePage(p, iPage, aData, 1);
drh806ebcb2011-04-09 17:53:30 +0000682 sqlite3_mutex_leave(p->pDestDb->mutex);
danielk197703ab0352009-02-06 05:59:44 +0000683 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
danielk197704103022009-02-03 16:51:24 +0000684 if( rc!=SQLITE_OK ){
685 p->rc = rc;
686 }
687 }
drh14681b72015-07-01 19:59:36 +0000688 }while( (p = p->pNext)!=0 );
689}
690void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
691 if( pBackup ) backupUpdate(pBackup, iPage, aData);
danielk197704103022009-02-03 16:51:24 +0000692}
693
694/*
695** Restart the backup process. This is called when the pager layer
696** detects that the database has been modified by an external database
697** connection. In this case there is no way of knowing which of the
698** pages that have been copied into the destination database are still
699** valid and which are not, so the entire process needs to be restarted.
700**
701** It is assumed that the mutex associated with the BtShared object
702** corresponding to the source database is held when this function is
703** called.
704*/
705void sqlite3BackupRestart(sqlite3_backup *pBackup){
706 sqlite3_backup *p; /* Iterator variable */
707 for(p=pBackup; p; p=p->pNext){
708 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
709 p->iNext = 1;
710 }
711}
712
713#ifndef SQLITE_OMIT_VACUUM
714/*
715** Copy the complete content of pBtFrom into pBtTo. A transaction
716** must be active for both files.
717**
718** The size of file pTo may be reduced by this operation. If anything
719** goes wrong, the transaction on pTo is rolled back. If successful, the
720** transaction is committed before returning.
721*/
722int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
723 int rc;
danc5f20a02011-10-07 16:57:59 +0000724 sqlite3_file *pFd; /* File descriptor for database pTo */
danielk197704103022009-02-03 16:51:24 +0000725 sqlite3_backup b;
726 sqlite3BtreeEnter(pTo);
727 sqlite3BtreeEnter(pFrom);
728
drh99744fa2020-08-25 19:09:07 +0000729 assert( sqlite3BtreeTxnState(pTo)==SQLITE_TXN_WRITE );
danc5f20a02011-10-07 16:57:59 +0000730 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
731 if( pFd->pMethods ){
732 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
drh2bfe5b32012-01-10 16:40:50 +0000733 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
734 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
735 if( rc ) goto copy_finished;
danc5f20a02011-10-07 16:57:59 +0000736 }
737
danielk197704103022009-02-03 16:51:24 +0000738 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
739 ** to 0. This is used by the implementations of sqlite3_backup_step()
740 ** and sqlite3_backup_finish() to detect that they are being called
741 ** from this function, not directly by the user.
742 */
743 memset(&b, 0, sizeof(b));
744 b.pSrcDb = pFrom->db;
745 b.pSrc = pFrom;
746 b.pDest = pTo;
747 b.iNext = 1;
748
749 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
750 ** file. By passing this as the number of pages to copy to
751 ** sqlite3_backup_step(), we can guarantee that the copy finishes
752 ** within a single call (unless an error occurs). The assert() statement
753 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
dan43c1ce32016-08-05 16:16:26 +0000754 ** or an error code. */
danielk197704103022009-02-03 16:51:24 +0000755 sqlite3_backup_step(&b, 0x7FFFFFFF);
756 assert( b.rc!=SQLITE_OK );
dan43c1ce32016-08-05 16:16:26 +0000757
danielk197704103022009-02-03 16:51:24 +0000758 rc = sqlite3_backup_finish(&b);
759 if( rc==SQLITE_OK ){
drhc9166342012-01-05 23:32:06 +0000760 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
dan43c1ce32016-08-05 16:16:26 +0000761 }else{
762 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
danielk197704103022009-02-03 16:51:24 +0000763 }
764
drh99744fa2020-08-25 19:09:07 +0000765 assert( sqlite3BtreeTxnState(pTo)!=SQLITE_TXN_WRITE );
drh2bfe5b32012-01-10 16:40:50 +0000766copy_finished:
danielk197704103022009-02-03 16:51:24 +0000767 sqlite3BtreeLeave(pFrom);
768 sqlite3BtreeLeave(pTo);
769 return rc;
770}
771#endif /* SQLITE_OMIT_VACUUM */