backup.c 26 KB

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