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