danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2008 Jan 22 |
| 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 | ** |
| 13 | ** This file contains code for a VFS layer that acts as a wrapper around |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 14 | ** an existing VFS. The code in this file attempts to verify that SQLite |
| 15 | ** correctly populates and syncs a journal file before writing to a |
| 16 | ** corresponding database file. |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 17 | */ |
| 18 | #if SQLITE_TEST /* This file is used for testing only */ |
| 19 | |
| 20 | #include "sqlite3.h" |
| 21 | #include "sqliteInt.h" |
| 22 | |
| 23 | /* |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 24 | ** INTERFACE |
| 25 | ** |
| 26 | ** The public interface to this wrapper VFS is two functions: |
| 27 | ** |
| 28 | ** jt_register() |
| 29 | ** jt_unregister() |
| 30 | ** |
| 31 | ** See header comments associated with those two functions below for |
| 32 | ** details. |
| 33 | ** |
| 34 | ** LIMITATIONS |
| 35 | ** |
| 36 | ** This wrapper will not work if "PRAGMA synchronous = off" is used. |
| 37 | ** |
| 38 | ** OPERATION |
| 39 | ** |
| 40 | ** Starting a Transaction: |
| 41 | ** |
| 42 | ** When a write-transaction is started, the contents of the database is |
| 43 | ** inspected and the following data stored as part of the database file |
| 44 | ** handle (type struct jt_file): |
| 45 | ** |
| 46 | ** a) The page-size of the database file. |
| 47 | ** b) The number of pages that are in the database file. |
| 48 | ** c) The set of page numbers corresponding to free-list leaf pages. |
| 49 | ** d) A check-sum for every page in the database file. |
| 50 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 51 | ** The start of a write-transaction is deemed to have occurred when a |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 52 | ** 28-byte journal header is written to byte offset 0 of the journal |
| 53 | ** file. |
| 54 | ** |
| 55 | ** Syncing the Journal File: |
| 56 | ** |
| 57 | ** Whenever the xSync method is invoked to sync a journal-file, the |
| 58 | ** contents of the journal file are read. For each page written to |
| 59 | ** the journal file, a check-sum is calculated and compared to the |
| 60 | ** check-sum calculated for the corresponding database page when the |
| 61 | ** write-transaction was initialized. The success of the comparison |
| 62 | ** is assert()ed. So if SQLite has written something other than the |
| 63 | ** original content to the database file, an assert() will fail. |
| 64 | ** |
| 65 | ** Additionally, the set of page numbers for which records exist in |
| 66 | ** the journal file is added to (unioned with) the set of page numbers |
| 67 | ** corresponding to free-list leaf pages collected when the |
| 68 | ** write-transaction was initialized. This set comprises the page-numbers |
| 69 | ** corresponding to those pages that SQLite may now safely modify. |
| 70 | ** |
| 71 | ** Writing to the Database File: |
| 72 | ** |
| 73 | ** When a block of data is written to a database file, the following |
| 74 | ** invariants are asserted: |
| 75 | ** |
| 76 | ** a) That the block of data is an aligned block of page-size bytes. |
| 77 | ** |
| 78 | ** b) That if the page being written did not exist when the |
| 79 | ** transaction was started (i.e. the database file is growing), then |
| 80 | ** the journal-file must have been synced at least once since |
| 81 | ** the start of the transaction. |
| 82 | ** |
| 83 | ** c) That if the page being written did exist when the transaction |
| 84 | ** was started, then the page must have either been a free-list |
| 85 | ** leaf page at the start of the transaction, or else must have |
| 86 | ** been stored in the journal file prior to the most recent sync. |
| 87 | ** |
| 88 | ** Closing a Transaction: |
| 89 | ** |
| 90 | ** When a transaction is closed, all data collected at the start of |
| 91 | ** the transaction, or following an xSync of a journal-file, is |
| 92 | ** discarded. The end of a transaction is recognized when any one |
| 93 | ** of the following occur: |
| 94 | ** |
| 95 | ** a) A block of zeroes (or anything else that is not a valid |
| 96 | ** journal-header) is written to the start of the journal file. |
| 97 | ** |
| 98 | ** b) A journal file is truncated to zero bytes in size using xTruncate. |
| 99 | ** |
| 100 | ** c) The journal file is deleted using xDelete. |
| 101 | */ |
| 102 | |
| 103 | /* |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 104 | ** Maximum pathname length supported by the jt backend. |
| 105 | */ |
| 106 | #define JT_MAX_PATHNAME 512 |
| 107 | |
| 108 | /* |
| 109 | ** Name used to identify this VFS. |
| 110 | */ |
| 111 | #define JT_VFS_NAME "jt" |
| 112 | |
| 113 | typedef struct jt_file jt_file; |
| 114 | struct jt_file { |
| 115 | sqlite3_file base; |
| 116 | const char *zName; /* Name of open file */ |
| 117 | int flags; /* Flags the file was opened with */ |
| 118 | |
| 119 | /* The following are only used by database file file handles */ |
| 120 | int eLock; /* Current lock held on the file */ |
| 121 | u32 nPage; /* Size of file in pages when transaction started */ |
| 122 | u32 nPagesize; /* Page size when transaction started */ |
| 123 | Bitvec *pWritable; /* Bitvec of pages that may be written to the file */ |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 124 | u32 *aCksum; /* Checksum for first nPage pages */ |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 125 | int nSync; /* Number of times journal file has been synced */ |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 126 | |
| 127 | /* Only used by journal file-handles */ |
| 128 | sqlite3_int64 iMaxOff; /* Maximum offset written to this transaction */ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 129 | |
| 130 | jt_file *pNext; /* All files are stored in a linked list */ |
| 131 | sqlite3_file *pReal; /* The file handle for the underlying vfs */ |
| 132 | }; |
| 133 | |
| 134 | /* |
| 135 | ** Method declarations for jt_file. |
| 136 | */ |
| 137 | static int jtClose(sqlite3_file*); |
| 138 | static int jtRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 139 | static int jtWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); |
| 140 | static int jtTruncate(sqlite3_file*, sqlite3_int64 size); |
| 141 | static int jtSync(sqlite3_file*, int flags); |
| 142 | static int jtFileSize(sqlite3_file*, sqlite3_int64 *pSize); |
| 143 | static int jtLock(sqlite3_file*, int); |
| 144 | static int jtUnlock(sqlite3_file*, int); |
| 145 | static int jtCheckReservedLock(sqlite3_file*, int *); |
| 146 | static int jtFileControl(sqlite3_file*, int op, void *pArg); |
| 147 | static int jtSectorSize(sqlite3_file*); |
| 148 | static int jtDeviceCharacteristics(sqlite3_file*); |
| 149 | |
| 150 | /* |
| 151 | ** Method declarations for jt_vfs. |
| 152 | */ |
| 153 | static int jtOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
| 154 | static int jtDelete(sqlite3_vfs*, const char *zName, int syncDir); |
| 155 | static int jtAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
| 156 | static int jtFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 157 | static void *jtDlOpen(sqlite3_vfs*, const char *zFilename); |
| 158 | static void jtDlError(sqlite3_vfs*, int nByte, char *zErrMsg); |
| 159 | static void (*jtDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void); |
| 160 | static void jtDlClose(sqlite3_vfs*, void*); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 161 | static int jtRandomness(sqlite3_vfs*, int nByte, char *zOut); |
| 162 | static int jtSleep(sqlite3_vfs*, int microseconds); |
| 163 | static int jtCurrentTime(sqlite3_vfs*, double*); |
| 164 | |
| 165 | static sqlite3_vfs jt_vfs = { |
| 166 | 1, /* iVersion */ |
| 167 | sizeof(jt_file), /* szOsFile */ |
| 168 | JT_MAX_PATHNAME, /* mxPathname */ |
| 169 | 0, /* pNext */ |
| 170 | JT_VFS_NAME, /* zName */ |
| 171 | 0, /* pAppData */ |
| 172 | jtOpen, /* xOpen */ |
| 173 | jtDelete, /* xDelete */ |
| 174 | jtAccess, /* xAccess */ |
| 175 | jtFullPathname, /* xFullPathname */ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 176 | jtDlOpen, /* xDlOpen */ |
| 177 | jtDlError, /* xDlError */ |
| 178 | jtDlSym, /* xDlSym */ |
| 179 | jtDlClose, /* xDlClose */ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 180 | jtRandomness, /* xRandomness */ |
| 181 | jtSleep, /* xSleep */ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 182 | jtCurrentTime, /* xCurrentTime */ |
| 183 | 0, /* xShmOpen */ |
| 184 | 0, /* xShmSize */ |
| 185 | 0, /* xShmPush */ |
| 186 | 0, /* xShmPull */ |
| 187 | 0, /* xShmLock */ |
| 188 | 0, /* xShmClose */ |
| 189 | 0, /* xShmDelete */ |
| 190 | 0, /* xRename */ |
| 191 | 0 /* xCurrentTimeInt64 */ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | static sqlite3_io_methods jt_io_methods = { |
| 195 | 1, /* iVersion */ |
| 196 | jtClose, /* xClose */ |
| 197 | jtRead, /* xRead */ |
| 198 | jtWrite, /* xWrite */ |
| 199 | jtTruncate, /* xTruncate */ |
| 200 | jtSync, /* xSync */ |
| 201 | jtFileSize, /* xFileSize */ |
| 202 | jtLock, /* xLock */ |
| 203 | jtUnlock, /* xUnlock */ |
| 204 | jtCheckReservedLock, /* xCheckReservedLock */ |
| 205 | jtFileControl, /* xFileControl */ |
| 206 | jtSectorSize, /* xSectorSize */ |
| 207 | jtDeviceCharacteristics /* xDeviceCharacteristics */ |
| 208 | }; |
| 209 | |
| 210 | struct JtGlobal { |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 211 | sqlite3_vfs *pVfs; /* Parent VFS */ |
| 212 | jt_file *pList; /* List of all open files */ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 213 | }; |
| 214 | static struct JtGlobal g = {0, 0}; |
| 215 | |
danielk1977 | 1a321c3 | 2009-03-28 17:21:52 +0000 | [diff] [blame] | 216 | /* |
| 217 | ** Functions to obtain and relinquish a mutex to protect g.pList. The |
| 218 | ** STATIC_PRNG mutex is reused, purely for the sake of convenience. |
| 219 | */ |
| 220 | static void enterJtMutex(void){ |
| 221 | sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG)); |
| 222 | } |
| 223 | static void leaveJtMutex(void){ |
| 224 | sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG)); |
| 225 | } |
| 226 | |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 227 | extern int sqlite3_io_error_pending; |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 228 | extern int sqlite3_io_error_hit; |
| 229 | static void stop_ioerr_simulation(int *piSave, int *piSave2){ |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 230 | *piSave = sqlite3_io_error_pending; |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 231 | *piSave2 = sqlite3_io_error_hit; |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 232 | sqlite3_io_error_pending = -1; |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 233 | sqlite3_io_error_hit = 0; |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 234 | } |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 235 | static void start_ioerr_simulation(int iSave, int iSave2){ |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 236 | sqlite3_io_error_pending = iSave; |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 237 | sqlite3_io_error_hit = iSave2; |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 238 | } |
| 239 | |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 240 | /* |
| 241 | ** The jt_file pointed to by the argument may or may not be a file-handle |
| 242 | ** open on a main database file. If it is, and a transaction is currently |
| 243 | ** opened on the file, then discard all transaction related data. |
| 244 | */ |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 245 | static void closeTransaction(jt_file *p){ |
| 246 | sqlite3BitvecDestroy(p->pWritable); |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 247 | sqlite3_free(p->aCksum); |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 248 | p->pWritable = 0; |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 249 | p->aCksum = 0; |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 250 | p->nSync = 0; |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 253 | /* |
| 254 | ** Close an jt-file. |
| 255 | */ |
| 256 | static int jtClose(sqlite3_file *pFile){ |
| 257 | jt_file **pp; |
| 258 | jt_file *p = (jt_file *)pFile; |
| 259 | |
danielk1977 | 401b65e | 2009-01-06 14:34:34 +0000 | [diff] [blame] | 260 | closeTransaction(p); |
danielk1977 | 1a321c3 | 2009-03-28 17:21:52 +0000 | [diff] [blame] | 261 | enterJtMutex(); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 262 | if( p->zName ){ |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 263 | for(pp=&g.pList; *pp!=p; pp=&(*pp)->pNext); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 264 | *pp = p->pNext; |
| 265 | } |
danielk1977 | 1a321c3 | 2009-03-28 17:21:52 +0000 | [diff] [blame] | 266 | leaveJtMutex(); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 267 | return sqlite3OsClose(p->pReal); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | ** Read data from an jt-file. |
| 272 | */ |
| 273 | static int jtRead( |
| 274 | sqlite3_file *pFile, |
| 275 | void *zBuf, |
| 276 | int iAmt, |
| 277 | sqlite_int64 iOfst |
| 278 | ){ |
| 279 | jt_file *p = (jt_file *)pFile; |
| 280 | return sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst); |
| 281 | } |
| 282 | |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 283 | /* |
| 284 | ** Parameter zJournal is the name of a journal file that is currently |
| 285 | ** open. This function locates and returns the handle opened on the |
| 286 | ** corresponding database file by the pager that currently has the |
| 287 | ** journal file opened. This file-handle is identified by the |
| 288 | ** following properties: |
| 289 | ** |
| 290 | ** a) SQLITE_OPEN_MAIN_DB was specified when the file was opened. |
| 291 | ** |
| 292 | ** b) The file-name specified when the file was opened matches |
| 293 | ** all but the final 8 characters of the journal file name. |
| 294 | ** |
| 295 | ** c) There is currently a reserved lock on the file. |
| 296 | **/ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 297 | static jt_file *locateDatabaseHandle(const char *zJournal){ |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 298 | jt_file *pMain = 0; |
danielk1977 | 1a321c3 | 2009-03-28 17:21:52 +0000 | [diff] [blame] | 299 | enterJtMutex(); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 300 | for(pMain=g.pList; pMain; pMain=pMain->pNext){ |
| 301 | int nName = strlen(zJournal) - strlen("-journal"); |
| 302 | if( (pMain->flags&SQLITE_OPEN_MAIN_DB) |
| 303 | && (strlen(pMain->zName)==nName) |
| 304 | && 0==memcmp(pMain->zName, zJournal, nName) |
| 305 | && (pMain->eLock>=SQLITE_LOCK_RESERVED) |
| 306 | ){ |
| 307 | break; |
| 308 | } |
| 309 | } |
danielk1977 | 1a321c3 | 2009-03-28 17:21:52 +0000 | [diff] [blame] | 310 | leaveJtMutex(); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 311 | return pMain; |
| 312 | } |
| 313 | |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 314 | /* |
| 315 | ** Parameter z points to a buffer of 4 bytes in size containing a |
| 316 | ** unsigned 32-bit integer stored in big-endian format. Decode the |
| 317 | ** integer and return its value. |
| 318 | */ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 319 | static u32 decodeUint32(const unsigned char *z){ |
| 320 | return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3]; |
| 321 | } |
| 322 | |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 323 | /* |
| 324 | ** Calculate a checksum from the buffer of length n bytes pointed to |
| 325 | ** by parameter z. |
| 326 | */ |
| 327 | static u32 genCksum(const unsigned char *z, int n){ |
| 328 | int i; |
| 329 | u32 cksum = 0; |
| 330 | for(i=0; i<n; i++){ |
| 331 | cksum = cksum + z[i] + (cksum<<3); |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 332 | } |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 333 | return cksum; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* |
| 337 | ** The first argument, zBuf, points to a buffer containing a 28 byte |
| 338 | ** serialized journal header. This function deserializes four of the |
| 339 | ** integer fields contained in the journal header and writes their |
| 340 | ** values to the output variables. |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 341 | ** |
| 342 | ** SQLITE_OK is returned if the journal-header is successfully |
| 343 | ** decoded. Otherwise, SQLITE_ERROR. |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 344 | */ |
| 345 | static int decodeJournalHdr( |
| 346 | const unsigned char *zBuf, /* Input: 28 byte journal header */ |
| 347 | u32 *pnRec, /* Out: Number of journalled records */ |
| 348 | u32 *pnPage, /* Out: Original database page count */ |
| 349 | u32 *pnSector, /* Out: Sector size in bytes */ |
| 350 | u32 *pnPagesize /* Out: Page size in bytes */ |
| 351 | ){ |
| 352 | unsigned char aMagic[] = { 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7 }; |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 353 | if( memcmp(aMagic, zBuf, 8) ) return SQLITE_ERROR; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 354 | if( pnRec ) *pnRec = decodeUint32(&zBuf[8]); |
| 355 | if( pnPage ) *pnPage = decodeUint32(&zBuf[16]); |
| 356 | if( pnSector ) *pnSector = decodeUint32(&zBuf[20]); |
| 357 | if( pnPagesize ) *pnPagesize = decodeUint32(&zBuf[24]); |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 358 | return SQLITE_OK; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | ** This function is called when a new transaction is opened, just after |
| 363 | ** the first journal-header is written to the journal file. |
| 364 | */ |
| 365 | static int openTransaction(jt_file *pMain, jt_file *pJournal){ |
| 366 | unsigned char *aData; |
| 367 | sqlite3_file *p = pMain->pReal; |
| 368 | int rc = SQLITE_OK; |
| 369 | |
| 370 | aData = sqlite3_malloc(pMain->nPagesize); |
| 371 | pMain->pWritable = sqlite3BitvecCreate(pMain->nPage); |
| 372 | pMain->aCksum = sqlite3_malloc(sizeof(u32) * (pMain->nPage + 1)); |
| 373 | pJournal->iMaxOff = 0; |
| 374 | |
| 375 | if( !pMain->pWritable || !pMain->aCksum || !aData ){ |
| 376 | rc = SQLITE_IOERR_NOMEM; |
| 377 | }else if( pMain->nPage>0 ){ |
| 378 | u32 iTrunk; |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 379 | int iSave; |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 380 | int iSave2; |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 381 | |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 382 | stop_ioerr_simulation(&iSave, &iSave2); |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 383 | |
| 384 | /* Read the database free-list. Add the page-number for each free-list |
| 385 | ** leaf to the jt_file.pWritable bitvec. |
| 386 | */ |
| 387 | rc = sqlite3OsRead(p, aData, pMain->nPagesize, 0); |
| 388 | iTrunk = decodeUint32(&aData[32]); |
| 389 | while( rc==SQLITE_OK && iTrunk>0 ){ |
| 390 | u32 nLeaf; |
| 391 | u32 iLeaf; |
| 392 | sqlite3_int64 iOff = (iTrunk-1)*pMain->nPagesize; |
| 393 | rc = sqlite3OsRead(p, aData, pMain->nPagesize, iOff); |
| 394 | nLeaf = decodeUint32(&aData[4]); |
| 395 | for(iLeaf=0; rc==SQLITE_OK && iLeaf<nLeaf; iLeaf++){ |
| 396 | u32 pgno = decodeUint32(&aData[8+4*iLeaf]); |
| 397 | sqlite3BitvecSet(pMain->pWritable, pgno); |
| 398 | } |
| 399 | iTrunk = decodeUint32(aData); |
| 400 | } |
| 401 | |
| 402 | /* Calculate and store a checksum for each page in the database file. */ |
| 403 | if( rc==SQLITE_OK ){ |
| 404 | int ii; |
| 405 | for(ii=0; rc==SQLITE_OK && ii<pMain->nPage; ii++){ |
| 406 | i64 iOff = (i64)(pMain->nPagesize) * (i64)ii; |
danielk1977 | 4faa5f4 | 2009-02-11 07:38:11 +0000 | [diff] [blame] | 407 | if( iOff==PENDING_BYTE ) continue; |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 408 | rc = sqlite3OsRead(pMain->pReal, aData, pMain->nPagesize, iOff); |
| 409 | pMain->aCksum[ii] = genCksum(aData, pMain->nPagesize); |
| 410 | } |
| 411 | } |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 412 | |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 413 | start_ioerr_simulation(iSave, iSave2); |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | sqlite3_free(aData); |
| 417 | return rc; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 421 | ** The first argument to this function is a handle open on a journal file. |
| 422 | ** This function reads the journal file and adds the page number for each |
| 423 | ** page in the journal to the Bitvec object passed as the second argument. |
| 424 | */ |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 425 | static int readJournalFile(jt_file *p, jt_file *pMain){ |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 426 | int rc = SQLITE_OK; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 427 | unsigned char zBuf[28]; |
| 428 | sqlite3_file *pReal = p->pReal; |
| 429 | sqlite3_int64 iOff = 0; |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 430 | sqlite3_int64 iSize = p->iMaxOff; |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 431 | unsigned char *aPage; |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 432 | int iSave; |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 433 | int iSave2; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 434 | |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 435 | aPage = sqlite3_malloc(pMain->nPagesize); |
| 436 | if( !aPage ){ |
| 437 | return SQLITE_IOERR_NOMEM; |
| 438 | } |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 439 | |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 440 | stop_ioerr_simulation(&iSave, &iSave2); |
danielk1977 | f7f33fb | 2009-02-12 09:11:56 +0000 | [diff] [blame] | 441 | |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 442 | while( rc==SQLITE_OK && iOff<iSize ){ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 443 | u32 nRec, nPage, nSector, nPagesize; |
| 444 | u32 ii; |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 445 | |
| 446 | /* Read and decode the next journal-header from the journal file. */ |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 447 | rc = sqlite3OsRead(pReal, zBuf, 28, iOff); |
| 448 | if( rc!=SQLITE_OK |
| 449 | || decodeJournalHdr(zBuf, &nRec, &nPage, &nSector, &nPagesize) |
| 450 | ){ |
danielk1977 | 0d519ca | 2009-01-05 17:15:00 +0000 | [diff] [blame] | 451 | goto finish_rjf; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 452 | } |
| 453 | iOff += nSector; |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 454 | |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 455 | if( nRec==0 ){ |
danielk1977 | 0d519ca | 2009-01-05 17:15:00 +0000 | [diff] [blame] | 456 | /* A trick. There might be another journal-header immediately |
| 457 | ** following this one. In this case, 0 records means 0 records, |
| 458 | ** not "read until the end of the file". See also ticket #2565. |
| 459 | */ |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 460 | if( iSize>=(iOff+nSector) ){ |
danielk1977 | 0d519ca | 2009-01-05 17:15:00 +0000 | [diff] [blame] | 461 | rc = sqlite3OsRead(pReal, zBuf, 28, iOff); |
| 462 | if( rc!=SQLITE_OK || 0==decodeJournalHdr(zBuf, 0, 0, 0, 0) ){ |
| 463 | continue; |
| 464 | } |
| 465 | } |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 466 | nRec = (iSize-iOff) / (pMain->nPagesize+8); |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 467 | } |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 468 | |
| 469 | /* Read all the records that follow the journal-header just read. */ |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 470 | for(ii=0; rc==SQLITE_OK && ii<nRec && iOff<iSize; ii++){ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 471 | u32 pgno; |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 472 | rc = sqlite3OsRead(pReal, zBuf, 4, iOff); |
| 473 | if( rc==SQLITE_OK ){ |
| 474 | pgno = decodeUint32(zBuf); |
danielk1977 | a641748 | 2008-12-24 09:30:22 +0000 | [diff] [blame] | 475 | if( pgno>0 && pgno<=pMain->nPage ){ |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 476 | if( 0==sqlite3BitvecTest(pMain->pWritable, pgno) ){ |
| 477 | rc = sqlite3OsRead(pReal, aPage, pMain->nPagesize, iOff+4); |
| 478 | if( rc==SQLITE_OK ){ |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 479 | u32 cksum = genCksum(aPage, pMain->nPagesize); |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 480 | assert( cksum==pMain->aCksum[pgno-1] ); |
| 481 | } |
| 482 | } |
danielk1977 | a641748 | 2008-12-24 09:30:22 +0000 | [diff] [blame] | 483 | sqlite3BitvecSet(pMain->pWritable, pgno); |
| 484 | } |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 485 | iOff += (8 + pMain->nPagesize); |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 486 | } |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | iOff = ((iOff + (nSector-1)) / nSector) * nSector; |
| 490 | } |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 491 | |
danielk1977 | 0d519ca | 2009-01-05 17:15:00 +0000 | [diff] [blame] | 492 | finish_rjf: |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 493 | start_ioerr_simulation(iSave, iSave2); |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 494 | sqlite3_free(aPage); |
danielk1977 | 0d519ca | 2009-01-05 17:15:00 +0000 | [diff] [blame] | 495 | if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 496 | rc = SQLITE_OK; |
| 497 | } |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 498 | return rc; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 499 | } |
| 500 | |
danielk1977 | 8ca8255 | 2009-06-26 10:39:36 +0000 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | ** Write data to an jt-file. |
| 504 | */ |
| 505 | static int jtWrite( |
| 506 | sqlite3_file *pFile, |
| 507 | const void *zBuf, |
| 508 | int iAmt, |
| 509 | sqlite_int64 iOfst |
| 510 | ){ |
| 511 | int rc; |
| 512 | jt_file *p = (jt_file *)pFile; |
| 513 | if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){ |
| 514 | if( iOfst==0 ){ |
| 515 | jt_file *pMain = locateDatabaseHandle(p->zName); |
| 516 | assert( pMain ); |
| 517 | |
| 518 | if( iAmt==28 ){ |
| 519 | /* Zeroing the first journal-file header. This is the end of a |
| 520 | ** transaction. */ |
| 521 | closeTransaction(pMain); |
| 522 | }else if( iAmt!=12 ){ |
| 523 | /* Writing the first journal header to a journal file. This happens |
| 524 | ** when a transaction is first started. */ |
| 525 | u8 *z = (u8 *)zBuf; |
| 526 | pMain->nPage = decodeUint32(&z[16]); |
| 527 | pMain->nPagesize = decodeUint32(&z[24]); |
| 528 | if( SQLITE_OK!=(rc=openTransaction(pMain, p)) ){ |
| 529 | return rc; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | if( p->iMaxOff<(iOfst + iAmt) ){ |
| 534 | p->iMaxOff = iOfst + iAmt; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable ){ |
| 539 | if( iAmt<p->nPagesize |
| 540 | && p->nPagesize%iAmt==0 |
| 541 | && iOfst>=(PENDING_BYTE+512) |
| 542 | && iOfst+iAmt<=PENDING_BYTE+p->nPagesize |
| 543 | ){ |
| 544 | /* No-op. This special case is hit when the backup code is copying a |
| 545 | ** to a database with a larger page-size than the source database and |
| 546 | ** it needs to fill in the non-locking-region part of the original |
| 547 | ** pending-byte page. |
| 548 | */ |
| 549 | }else{ |
| 550 | u32 pgno = iOfst/p->nPagesize + 1; |
| 551 | assert( (iAmt==1||iAmt==p->nPagesize) && ((iOfst+iAmt)%p->nPagesize)==0 ); |
| 552 | assert( pgno<=p->nPage || p->nSync>0 ); |
| 553 | assert( pgno>p->nPage || sqlite3BitvecTest(p->pWritable, pgno) ); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst); |
| 558 | if( (p->flags&SQLITE_OPEN_MAIN_JOURNAL) && iAmt==12 ){ |
| 559 | jt_file *pMain = locateDatabaseHandle(p->zName); |
| 560 | int rc2 = readJournalFile(p, pMain); |
| 561 | if( rc==SQLITE_OK ) rc = rc2; |
| 562 | } |
| 563 | return rc; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | ** Truncate an jt-file. |
| 568 | */ |
| 569 | static int jtTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
| 570 | jt_file *p = (jt_file *)pFile; |
| 571 | if( p->flags&SQLITE_OPEN_MAIN_JOURNAL && size==0 ){ |
| 572 | /* Truncating a journal file. This is the end of a transaction. */ |
| 573 | jt_file *pMain = locateDatabaseHandle(p->zName); |
| 574 | closeTransaction(pMain); |
| 575 | } |
| 576 | if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable ){ |
| 577 | u32 pgno; |
| 578 | u32 locking_page = (u32)(PENDING_BYTE/p->nPagesize+1); |
| 579 | for(pgno=size/p->nPagesize+1; pgno<=p->nPage; pgno++){ |
| 580 | assert( pgno==locking_page || sqlite3BitvecTest(p->pWritable, pgno) ); |
| 581 | } |
| 582 | } |
| 583 | return sqlite3OsTruncate(p->pReal, size); |
| 584 | } |
| 585 | |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 586 | /* |
| 587 | ** Sync an jt-file. |
| 588 | */ |
| 589 | static int jtSync(sqlite3_file *pFile, int flags){ |
| 590 | jt_file *p = (jt_file *)pFile; |
| 591 | |
| 592 | if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){ |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 593 | int rc; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 594 | jt_file *pMain; /* The associated database file */ |
| 595 | |
| 596 | /* The journal file is being synced. At this point, we inspect the |
| 597 | ** contents of the file up to this point and set each bit in the |
| 598 | ** jt_file.pWritable bitvec of the main database file associated with |
| 599 | ** this journal file. |
| 600 | */ |
| 601 | pMain = locateDatabaseHandle(p->zName); |
| 602 | assert(pMain); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 603 | |
| 604 | /* Set the bitvec values */ |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 605 | if( pMain->pWritable ){ |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 606 | pMain->nSync++; |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 607 | rc = readJournalFile(p, pMain); |
| 608 | if( rc!=SQLITE_OK ){ |
| 609 | return rc; |
| 610 | } |
| 611 | } |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | return sqlite3OsSync(p->pReal, flags); |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | ** Return the current file-size of an jt-file. |
| 619 | */ |
| 620 | static int jtFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
| 621 | jt_file *p = (jt_file *)pFile; |
| 622 | return sqlite3OsFileSize(p->pReal, pSize); |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | ** Lock an jt-file. |
| 627 | */ |
| 628 | static int jtLock(sqlite3_file *pFile, int eLock){ |
| 629 | int rc; |
| 630 | jt_file *p = (jt_file *)pFile; |
| 631 | rc = sqlite3OsLock(p->pReal, eLock); |
| 632 | if( rc==SQLITE_OK && eLock>p->eLock ){ |
| 633 | p->eLock = eLock; |
| 634 | } |
| 635 | return rc; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | ** Unlock an jt-file. |
| 640 | */ |
| 641 | static int jtUnlock(sqlite3_file *pFile, int eLock){ |
| 642 | int rc; |
| 643 | jt_file *p = (jt_file *)pFile; |
| 644 | rc = sqlite3OsUnlock(p->pReal, eLock); |
| 645 | if( rc==SQLITE_OK && eLock<p->eLock ){ |
| 646 | p->eLock = eLock; |
| 647 | } |
| 648 | return rc; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | ** Check if another file-handle holds a RESERVED lock on an jt-file. |
| 653 | */ |
| 654 | static int jtCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 655 | jt_file *p = (jt_file *)pFile; |
| 656 | return sqlite3OsCheckReservedLock(p->pReal, pResOut); |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | ** File control method. For custom operations on an jt-file. |
| 661 | */ |
| 662 | static int jtFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 663 | jt_file *p = (jt_file *)pFile; |
| 664 | return sqlite3OsFileControl(p->pReal, op, pArg); |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | ** Return the sector-size in bytes for an jt-file. |
| 669 | */ |
| 670 | static int jtSectorSize(sqlite3_file *pFile){ |
| 671 | jt_file *p = (jt_file *)pFile; |
| 672 | return sqlite3OsSectorSize(p->pReal); |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | ** Return the device characteristic flags supported by an jt-file. |
| 677 | */ |
| 678 | static int jtDeviceCharacteristics(sqlite3_file *pFile){ |
| 679 | jt_file *p = (jt_file *)pFile; |
| 680 | return sqlite3OsDeviceCharacteristics(p->pReal); |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | ** Open an jt file handle. |
| 685 | */ |
| 686 | static int jtOpen( |
| 687 | sqlite3_vfs *pVfs, |
| 688 | const char *zName, |
| 689 | sqlite3_file *pFile, |
| 690 | int flags, |
| 691 | int *pOutFlags |
| 692 | ){ |
| 693 | int rc; |
| 694 | jt_file *p = (jt_file *)pFile; |
danielk1977 | 5a2cc66 | 2009-03-26 11:49:11 +0000 | [diff] [blame] | 695 | pFile->pMethods = 0; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 696 | p->pReal = (sqlite3_file *)&p[1]; |
danielk1977 | a641748 | 2008-12-24 09:30:22 +0000 | [diff] [blame] | 697 | p->pReal->pMethods = 0; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 698 | rc = sqlite3OsOpen(g.pVfs, zName, p->pReal, flags, pOutFlags); |
danielk1977 | a641748 | 2008-12-24 09:30:22 +0000 | [diff] [blame] | 699 | assert( rc==SQLITE_OK || p->pReal->pMethods==0 ); |
| 700 | if( rc==SQLITE_OK ){ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 701 | pFile->pMethods = &jt_io_methods; |
| 702 | p->eLock = 0; |
| 703 | p->zName = zName; |
| 704 | p->flags = flags; |
| 705 | p->pNext = 0; |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 706 | p->pWritable = 0; |
danielk1977 | cd1cbff | 2009-01-06 17:52:43 +0000 | [diff] [blame] | 707 | p->aCksum = 0; |
danielk1977 | 1a321c3 | 2009-03-28 17:21:52 +0000 | [diff] [blame] | 708 | enterJtMutex(); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 709 | if( zName ){ |
| 710 | p->pNext = g.pList; |
| 711 | g.pList = p; |
| 712 | } |
danielk1977 | 1a321c3 | 2009-03-28 17:21:52 +0000 | [diff] [blame] | 713 | leaveJtMutex(); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 714 | } |
| 715 | return rc; |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | ** Delete the file located at zPath. If the dirSync argument is true, |
| 720 | ** ensure the file-system modifications are synced to disk before |
| 721 | ** returning. |
| 722 | */ |
| 723 | static int jtDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 724 | int nPath = strlen(zPath); |
| 725 | if( nPath>8 && 0==strcmp("-journal", &zPath[nPath-8]) ){ |
| 726 | /* Deleting a journal file. The end of a transaction. */ |
| 727 | jt_file *pMain = locateDatabaseHandle(zPath); |
danielk1977 | f310751 | 2008-12-22 10:58:46 +0000 | [diff] [blame] | 728 | if( pMain ){ |
| 729 | closeTransaction(pMain); |
| 730 | } |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | return sqlite3OsDelete(g.pVfs, zPath, dirSync); |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | ** Test for access permissions. Return true if the requested permission |
| 738 | ** is available, or false otherwise. |
| 739 | */ |
| 740 | static int jtAccess( |
| 741 | sqlite3_vfs *pVfs, |
| 742 | const char *zPath, |
| 743 | int flags, |
| 744 | int *pResOut |
| 745 | ){ |
| 746 | return sqlite3OsAccess(g.pVfs, zPath, flags, pResOut); |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | ** Populate buffer zOut with the full canonical pathname corresponding |
| 751 | ** to the pathname in zPath. zOut is guaranteed to point to a buffer |
| 752 | ** of at least (JT_MAX_PATHNAME+1) bytes. |
| 753 | */ |
| 754 | static int jtFullPathname( |
| 755 | sqlite3_vfs *pVfs, |
| 756 | const char *zPath, |
| 757 | int nOut, |
| 758 | char *zOut |
| 759 | ){ |
| 760 | return sqlite3OsFullPathname(g.pVfs, zPath, nOut, zOut); |
| 761 | } |
| 762 | |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 763 | /* |
| 764 | ** Open the dynamic library located at zPath and return a handle. |
| 765 | */ |
| 766 | static void *jtDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 767 | return g.pVfs->xDlOpen(g.pVfs, zPath); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* |
| 771 | ** Populate the buffer zErrMsg (size nByte bytes) with a human readable |
| 772 | ** utf-8 string describing the most recent error encountered associated |
| 773 | ** with dynamic libraries. |
| 774 | */ |
| 775 | static void jtDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 776 | g.pVfs->xDlError(g.pVfs, nByte, zErrMsg); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* |
| 780 | ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. |
| 781 | */ |
| 782 | static void (*jtDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 783 | return g.pVfs->xDlSym(g.pVfs, p, zSym); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /* |
| 787 | ** Close the dynamic library handle pHandle. |
| 788 | */ |
| 789 | static void jtDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 790 | g.pVfs->xDlClose(g.pVfs, pHandle); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 791 | } |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 792 | |
| 793 | /* |
| 794 | ** Populate the buffer pointed to by zBufOut with nByte bytes of |
| 795 | ** random data. |
| 796 | */ |
| 797 | static int jtRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 798 | return sqlite3OsRandomness(g.pVfs, nByte, zBufOut); |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | ** Sleep for nMicro microseconds. Return the number of microseconds |
| 803 | ** actually slept. |
| 804 | */ |
| 805 | static int jtSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 806 | return sqlite3OsSleep(g.pVfs, nMicro); |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | ** Return the current time as a Julian Day number in *pTimeOut. |
| 811 | */ |
| 812 | static int jtCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 813 | return sqlite3OsCurrentTime(g.pVfs, pTimeOut); |
| 814 | } |
| 815 | |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 816 | /************************************************************************** |
| 817 | ** Start of public API. |
| 818 | */ |
| 819 | |
| 820 | /* |
| 821 | ** Configure the jt VFS as a wrapper around the VFS named by parameter |
| 822 | ** zWrap. If the isDefault parameter is true, then the jt VFS is installed |
| 823 | ** as the new default VFS for SQLite connections. If isDefault is not |
| 824 | ** true, then the jt VFS is installed as non-default. In this case it |
| 825 | ** is available via its name, "jt". |
| 826 | */ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 827 | int jt_register(char *zWrap, int isDefault){ |
| 828 | g.pVfs = sqlite3_vfs_find(zWrap); |
| 829 | if( g.pVfs==0 ){ |
| 830 | return SQLITE_ERROR; |
| 831 | } |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 832 | jt_vfs.szOsFile = sizeof(jt_file) + g.pVfs->szOsFile; |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 833 | sqlite3_vfs_register(&jt_vfs, isDefault); |
| 834 | return SQLITE_OK; |
| 835 | } |
| 836 | |
danielk1977 | e86a5b7 | 2009-01-08 12:05:56 +0000 | [diff] [blame] | 837 | /* |
| 838 | ** Uninstall the jt VFS, if it is installed. |
| 839 | */ |
danielk1977 | 64aca19 | 2009-04-07 11:21:28 +0000 | [diff] [blame] | 840 | void jt_unregister(void){ |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 841 | sqlite3_vfs_unregister(&jt_vfs); |
| 842 | } |
| 843 | |
| 844 | #endif |