blob: fcfe778d1b1c79e07f712c23a114073c3fec9ed5 [file] [log] [blame]
danielk1977a0fc7292008-12-20 18:33:59 +00001/*
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
danielk1977e86a5b72009-01-08 12:05:56 +000014** 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.
danielk1977a0fc7292008-12-20 18:33:59 +000017*/
18#if SQLITE_TEST /* This file is used for testing only */
19
20#include "sqlite3.h"
21#include "sqliteInt.h"
22
23/*
danielk1977e86a5b72009-01-08 12:05:56 +000024** 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**
shanebe217792009-03-05 04:20:31 +000051** The start of a write-transaction is deemed to have occurred when a
danielk1977e86a5b72009-01-08 12:05:56 +000052** 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/*
danielk1977a0fc7292008-12-20 18:33:59 +0000104** 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
113typedef struct jt_file jt_file;
114struct 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 */
danielk1977cd1cbff2009-01-06 17:52:43 +0000124 u32 *aCksum; /* Checksum for first nPage pages */
danielk1977e86a5b72009-01-08 12:05:56 +0000125 int nSync; /* Number of times journal file has been synced */
danielk1977cd1cbff2009-01-06 17:52:43 +0000126
127 /* Only used by journal file-handles */
128 sqlite3_int64 iMaxOff; /* Maximum offset written to this transaction */
danielk1977a0fc7292008-12-20 18:33:59 +0000129
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*/
137static int jtClose(sqlite3_file*);
138static int jtRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
139static int jtWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
140static int jtTruncate(sqlite3_file*, sqlite3_int64 size);
141static int jtSync(sqlite3_file*, int flags);
142static int jtFileSize(sqlite3_file*, sqlite3_int64 *pSize);
143static int jtLock(sqlite3_file*, int);
144static int jtUnlock(sqlite3_file*, int);
145static int jtCheckReservedLock(sqlite3_file*, int *);
146static int jtFileControl(sqlite3_file*, int op, void *pArg);
147static int jtSectorSize(sqlite3_file*);
148static int jtDeviceCharacteristics(sqlite3_file*);
149
150/*
151** Method declarations for jt_vfs.
152*/
153static int jtOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
154static int jtDelete(sqlite3_vfs*, const char *zName, int syncDir);
155static int jtAccess(sqlite3_vfs*, const char *zName, int flags, int *);
156static int jtFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
danielk1977a0fc7292008-12-20 18:33:59 +0000157static void *jtDlOpen(sqlite3_vfs*, const char *zFilename);
158static void jtDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
159static void (*jtDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
160static void jtDlClose(sqlite3_vfs*, void*);
danielk1977a0fc7292008-12-20 18:33:59 +0000161static int jtRandomness(sqlite3_vfs*, int nByte, char *zOut);
162static int jtSleep(sqlite3_vfs*, int microseconds);
163static int jtCurrentTime(sqlite3_vfs*, double*);
164
165static 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 */
danielk1977a0fc7292008-12-20 18:33:59 +0000176 jtDlOpen, /* xDlOpen */
177 jtDlError, /* xDlError */
178 jtDlSym, /* xDlSym */
179 jtDlClose, /* xDlClose */
danielk1977a0fc7292008-12-20 18:33:59 +0000180 jtRandomness, /* xRandomness */
181 jtSleep, /* xSleep */
drhf2424c52010-04-26 00:04:55 +0000182 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 */
danielk1977a0fc7292008-12-20 18:33:59 +0000192};
193
194static 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
210struct JtGlobal {
danielk1977e86a5b72009-01-08 12:05:56 +0000211 sqlite3_vfs *pVfs; /* Parent VFS */
212 jt_file *pList; /* List of all open files */
danielk1977a0fc7292008-12-20 18:33:59 +0000213};
214static struct JtGlobal g = {0, 0};
215
danielk19771a321c32009-03-28 17:21:52 +0000216/*
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*/
220static void enterJtMutex(void){
221 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG));
222}
223static void leaveJtMutex(void){
224 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG));
225}
226
danielk1977f7f33fb2009-02-12 09:11:56 +0000227extern int sqlite3_io_error_pending;
danielk19778ca82552009-06-26 10:39:36 +0000228extern int sqlite3_io_error_hit;
229static void stop_ioerr_simulation(int *piSave, int *piSave2){
danielk1977f7f33fb2009-02-12 09:11:56 +0000230 *piSave = sqlite3_io_error_pending;
danielk19778ca82552009-06-26 10:39:36 +0000231 *piSave2 = sqlite3_io_error_hit;
danielk1977f7f33fb2009-02-12 09:11:56 +0000232 sqlite3_io_error_pending = -1;
danielk19778ca82552009-06-26 10:39:36 +0000233 sqlite3_io_error_hit = 0;
danielk1977f7f33fb2009-02-12 09:11:56 +0000234}
danielk19778ca82552009-06-26 10:39:36 +0000235static void start_ioerr_simulation(int iSave, int iSave2){
danielk1977f7f33fb2009-02-12 09:11:56 +0000236 sqlite3_io_error_pending = iSave;
danielk19778ca82552009-06-26 10:39:36 +0000237 sqlite3_io_error_hit = iSave2;
danielk1977f7f33fb2009-02-12 09:11:56 +0000238}
239
danielk1977e86a5b72009-01-08 12:05:56 +0000240/*
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*/
danielk1977f3107512008-12-22 10:58:46 +0000245static void closeTransaction(jt_file *p){
246 sqlite3BitvecDestroy(p->pWritable);
danielk1977cd1cbff2009-01-06 17:52:43 +0000247 sqlite3_free(p->aCksum);
danielk1977f3107512008-12-22 10:58:46 +0000248 p->pWritable = 0;
danielk1977cd1cbff2009-01-06 17:52:43 +0000249 p->aCksum = 0;
danielk1977e86a5b72009-01-08 12:05:56 +0000250 p->nSync = 0;
danielk1977f3107512008-12-22 10:58:46 +0000251}
252
danielk1977a0fc7292008-12-20 18:33:59 +0000253/*
254** Close an jt-file.
255*/
256static int jtClose(sqlite3_file *pFile){
257 jt_file **pp;
258 jt_file *p = (jt_file *)pFile;
259
danielk1977401b65e2009-01-06 14:34:34 +0000260 closeTransaction(p);
danielk19771a321c32009-03-28 17:21:52 +0000261 enterJtMutex();
danielk1977a0fc7292008-12-20 18:33:59 +0000262 if( p->zName ){
danielk1977f3107512008-12-22 10:58:46 +0000263 for(pp=&g.pList; *pp!=p; pp=&(*pp)->pNext);
danielk1977a0fc7292008-12-20 18:33:59 +0000264 *pp = p->pNext;
265 }
danielk19771a321c32009-03-28 17:21:52 +0000266 leaveJtMutex();
danielk1977a0fc7292008-12-20 18:33:59 +0000267 return sqlite3OsClose(p->pReal);
268}
269
270/*
271** Read data from an jt-file.
272*/
273static 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
danielk1977e86a5b72009-01-08 12:05:56 +0000283/*
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**/
danielk1977a0fc7292008-12-20 18:33:59 +0000297static jt_file *locateDatabaseHandle(const char *zJournal){
danielk1977e86a5b72009-01-08 12:05:56 +0000298 jt_file *pMain = 0;
danielk19771a321c32009-03-28 17:21:52 +0000299 enterJtMutex();
danielk1977a0fc7292008-12-20 18:33:59 +0000300 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 }
danielk19771a321c32009-03-28 17:21:52 +0000310 leaveJtMutex();
danielk1977a0fc7292008-12-20 18:33:59 +0000311 return pMain;
312}
313
danielk1977e86a5b72009-01-08 12:05:56 +0000314/*
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*/
danielk1977a0fc7292008-12-20 18:33:59 +0000319static u32 decodeUint32(const unsigned char *z){
320 return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
321}
322
danielk1977e86a5b72009-01-08 12:05:56 +0000323/*
324** Calculate a checksum from the buffer of length n bytes pointed to
325** by parameter z.
326*/
327static 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);
danielk1977cd1cbff2009-01-06 17:52:43 +0000332 }
danielk1977e86a5b72009-01-08 12:05:56 +0000333 return cksum;
danielk1977a0fc7292008-12-20 18:33:59 +0000334}
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.
danielk1977e86a5b72009-01-08 12:05:56 +0000341**
342** SQLITE_OK is returned if the journal-header is successfully
343** decoded. Otherwise, SQLITE_ERROR.
danielk1977a0fc7292008-12-20 18:33:59 +0000344*/
345static 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 };
danielk1977e86a5b72009-01-08 12:05:56 +0000353 if( memcmp(aMagic, zBuf, 8) ) return SQLITE_ERROR;
danielk1977a0fc7292008-12-20 18:33:59 +0000354 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]);
danielk1977e86a5b72009-01-08 12:05:56 +0000358 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*/
365static 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;
danielk1977f7f33fb2009-02-12 09:11:56 +0000379 int iSave;
danielk19778ca82552009-06-26 10:39:36 +0000380 int iSave2;
danielk1977f7f33fb2009-02-12 09:11:56 +0000381
danielk19778ca82552009-06-26 10:39:36 +0000382 stop_ioerr_simulation(&iSave, &iSave2);
danielk1977e86a5b72009-01-08 12:05:56 +0000383
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;
danielk19774faa5f42009-02-11 07:38:11 +0000407 if( iOff==PENDING_BYTE ) continue;
danielk1977e86a5b72009-01-08 12:05:56 +0000408 rc = sqlite3OsRead(pMain->pReal, aData, pMain->nPagesize, iOff);
409 pMain->aCksum[ii] = genCksum(aData, pMain->nPagesize);
410 }
411 }
danielk1977f7f33fb2009-02-12 09:11:56 +0000412
danielk19778ca82552009-06-26 10:39:36 +0000413 start_ioerr_simulation(iSave, iSave2);
danielk1977e86a5b72009-01-08 12:05:56 +0000414 }
415
416 sqlite3_free(aData);
417 return rc;
danielk1977a0fc7292008-12-20 18:33:59 +0000418}
419
420/*
danielk1977a0fc7292008-12-20 18:33:59 +0000421** 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*/
danielk1977f3107512008-12-22 10:58:46 +0000425static int readJournalFile(jt_file *p, jt_file *pMain){
danielk1977cd1cbff2009-01-06 17:52:43 +0000426 int rc = SQLITE_OK;
danielk1977a0fc7292008-12-20 18:33:59 +0000427 unsigned char zBuf[28];
428 sqlite3_file *pReal = p->pReal;
429 sqlite3_int64 iOff = 0;
danielk1977e86a5b72009-01-08 12:05:56 +0000430 sqlite3_int64 iSize = p->iMaxOff;
danielk1977cd1cbff2009-01-06 17:52:43 +0000431 unsigned char *aPage;
danielk1977f7f33fb2009-02-12 09:11:56 +0000432 int iSave;
danielk19778ca82552009-06-26 10:39:36 +0000433 int iSave2;
danielk1977a0fc7292008-12-20 18:33:59 +0000434
danielk1977cd1cbff2009-01-06 17:52:43 +0000435 aPage = sqlite3_malloc(pMain->nPagesize);
436 if( !aPage ){
437 return SQLITE_IOERR_NOMEM;
438 }
danielk1977e86a5b72009-01-08 12:05:56 +0000439
danielk19778ca82552009-06-26 10:39:36 +0000440 stop_ioerr_simulation(&iSave, &iSave2);
danielk1977f7f33fb2009-02-12 09:11:56 +0000441
danielk1977f3107512008-12-22 10:58:46 +0000442 while( rc==SQLITE_OK && iOff<iSize ){
danielk1977a0fc7292008-12-20 18:33:59 +0000443 u32 nRec, nPage, nSector, nPagesize;
444 u32 ii;
danielk1977e86a5b72009-01-08 12:05:56 +0000445
446 /* Read and decode the next journal-header from the journal file. */
danielk1977f3107512008-12-22 10:58:46 +0000447 rc = sqlite3OsRead(pReal, zBuf, 28, iOff);
448 if( rc!=SQLITE_OK
449 || decodeJournalHdr(zBuf, &nRec, &nPage, &nSector, &nPagesize)
450 ){
danielk19770d519ca2009-01-05 17:15:00 +0000451 goto finish_rjf;
danielk1977a0fc7292008-12-20 18:33:59 +0000452 }
453 iOff += nSector;
danielk1977e86a5b72009-01-08 12:05:56 +0000454
danielk1977f3107512008-12-22 10:58:46 +0000455 if( nRec==0 ){
danielk19770d519ca2009-01-05 17:15:00 +0000456 /* 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 */
danielk1977cd1cbff2009-01-06 17:52:43 +0000460 if( iSize>=(iOff+nSector) ){
danielk19770d519ca2009-01-05 17:15:00 +0000461 rc = sqlite3OsRead(pReal, zBuf, 28, iOff);
462 if( rc!=SQLITE_OK || 0==decodeJournalHdr(zBuf, 0, 0, 0, 0) ){
463 continue;
464 }
465 }
danielk1977cd1cbff2009-01-06 17:52:43 +0000466 nRec = (iSize-iOff) / (pMain->nPagesize+8);
danielk1977f3107512008-12-22 10:58:46 +0000467 }
danielk1977e86a5b72009-01-08 12:05:56 +0000468
469 /* Read all the records that follow the journal-header just read. */
danielk1977f3107512008-12-22 10:58:46 +0000470 for(ii=0; rc==SQLITE_OK && ii<nRec && iOff<iSize; ii++){
danielk1977a0fc7292008-12-20 18:33:59 +0000471 u32 pgno;
danielk1977f3107512008-12-22 10:58:46 +0000472 rc = sqlite3OsRead(pReal, zBuf, 4, iOff);
473 if( rc==SQLITE_OK ){
474 pgno = decodeUint32(zBuf);
danielk1977a6417482008-12-24 09:30:22 +0000475 if( pgno>0 && pgno<=pMain->nPage ){
danielk1977cd1cbff2009-01-06 17:52:43 +0000476 if( 0==sqlite3BitvecTest(pMain->pWritable, pgno) ){
477 rc = sqlite3OsRead(pReal, aPage, pMain->nPagesize, iOff+4);
478 if( rc==SQLITE_OK ){
danielk1977e86a5b72009-01-08 12:05:56 +0000479 u32 cksum = genCksum(aPage, pMain->nPagesize);
danielk1977cd1cbff2009-01-06 17:52:43 +0000480 assert( cksum==pMain->aCksum[pgno-1] );
481 }
482 }
danielk1977a6417482008-12-24 09:30:22 +0000483 sqlite3BitvecSet(pMain->pWritable, pgno);
484 }
danielk1977cd1cbff2009-01-06 17:52:43 +0000485 iOff += (8 + pMain->nPagesize);
danielk1977f3107512008-12-22 10:58:46 +0000486 }
danielk1977a0fc7292008-12-20 18:33:59 +0000487 }
488
489 iOff = ((iOff + (nSector-1)) / nSector) * nSector;
490 }
danielk1977f3107512008-12-22 10:58:46 +0000491
danielk19770d519ca2009-01-05 17:15:00 +0000492finish_rjf:
danielk19778ca82552009-06-26 10:39:36 +0000493 start_ioerr_simulation(iSave, iSave2);
danielk1977cd1cbff2009-01-06 17:52:43 +0000494 sqlite3_free(aPage);
danielk19770d519ca2009-01-05 17:15:00 +0000495 if( rc==SQLITE_IOERR_SHORT_READ ){
496 rc = SQLITE_OK;
497 }
danielk1977f3107512008-12-22 10:58:46 +0000498 return rc;
danielk1977a0fc7292008-12-20 18:33:59 +0000499}
500
danielk19778ca82552009-06-26 10:39:36 +0000501
502/*
503** Write data to an jt-file.
504*/
505static 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*/
569static 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
danielk1977a0fc7292008-12-20 18:33:59 +0000586/*
587** Sync an jt-file.
588*/
589static int jtSync(sqlite3_file *pFile, int flags){
590 jt_file *p = (jt_file *)pFile;
591
592 if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){
danielk1977f3107512008-12-22 10:58:46 +0000593 int rc;
danielk1977a0fc7292008-12-20 18:33:59 +0000594 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);
danielk1977a0fc7292008-12-20 18:33:59 +0000603
604 /* Set the bitvec values */
danielk1977f3107512008-12-22 10:58:46 +0000605 if( pMain->pWritable ){
danielk1977e86a5b72009-01-08 12:05:56 +0000606 pMain->nSync++;
danielk1977f3107512008-12-22 10:58:46 +0000607 rc = readJournalFile(p, pMain);
608 if( rc!=SQLITE_OK ){
609 return rc;
610 }
611 }
danielk1977a0fc7292008-12-20 18:33:59 +0000612 }
613
614 return sqlite3OsSync(p->pReal, flags);
615}
616
617/*
618** Return the current file-size of an jt-file.
619*/
620static 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*/
628static 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*/
641static 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*/
654static 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*/
662static 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*/
670static 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*/
678static 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*/
686static 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;
danielk19775a2cc662009-03-26 11:49:11 +0000695 pFile->pMethods = 0;
danielk1977a0fc7292008-12-20 18:33:59 +0000696 p->pReal = (sqlite3_file *)&p[1];
danielk1977a6417482008-12-24 09:30:22 +0000697 p->pReal->pMethods = 0;
danielk1977a0fc7292008-12-20 18:33:59 +0000698 rc = sqlite3OsOpen(g.pVfs, zName, p->pReal, flags, pOutFlags);
danielk1977a6417482008-12-24 09:30:22 +0000699 assert( rc==SQLITE_OK || p->pReal->pMethods==0 );
700 if( rc==SQLITE_OK ){
danielk1977a0fc7292008-12-20 18:33:59 +0000701 pFile->pMethods = &jt_io_methods;
702 p->eLock = 0;
703 p->zName = zName;
704 p->flags = flags;
705 p->pNext = 0;
danielk1977f3107512008-12-22 10:58:46 +0000706 p->pWritable = 0;
danielk1977cd1cbff2009-01-06 17:52:43 +0000707 p->aCksum = 0;
danielk19771a321c32009-03-28 17:21:52 +0000708 enterJtMutex();
danielk1977a0fc7292008-12-20 18:33:59 +0000709 if( zName ){
710 p->pNext = g.pList;
711 g.pList = p;
712 }
danielk19771a321c32009-03-28 17:21:52 +0000713 leaveJtMutex();
danielk1977a0fc7292008-12-20 18:33:59 +0000714 }
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*/
723static 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);
danielk1977f3107512008-12-22 10:58:46 +0000728 if( pMain ){
729 closeTransaction(pMain);
730 }
danielk1977a0fc7292008-12-20 18:33:59 +0000731 }
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*/
740static 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*/
754static 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
danielk1977a0fc7292008-12-20 18:33:59 +0000763/*
764** Open the dynamic library located at zPath and return a handle.
765*/
766static void *jtDlOpen(sqlite3_vfs *pVfs, const char *zPath){
danielk1977e86a5b72009-01-08 12:05:56 +0000767 return g.pVfs->xDlOpen(g.pVfs, zPath);
danielk1977a0fc7292008-12-20 18:33:59 +0000768}
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*/
775static void jtDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
danielk1977e86a5b72009-01-08 12:05:56 +0000776 g.pVfs->xDlError(g.pVfs, nByte, zErrMsg);
danielk1977a0fc7292008-12-20 18:33:59 +0000777}
778
779/*
780** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
781*/
782static void (*jtDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
danielk1977e86a5b72009-01-08 12:05:56 +0000783 return g.pVfs->xDlSym(g.pVfs, p, zSym);
danielk1977a0fc7292008-12-20 18:33:59 +0000784}
785
786/*
787** Close the dynamic library handle pHandle.
788*/
789static void jtDlClose(sqlite3_vfs *pVfs, void *pHandle){
danielk1977e86a5b72009-01-08 12:05:56 +0000790 g.pVfs->xDlClose(g.pVfs, pHandle);
danielk1977a0fc7292008-12-20 18:33:59 +0000791}
danielk1977a0fc7292008-12-20 18:33:59 +0000792
793/*
794** Populate the buffer pointed to by zBufOut with nByte bytes of
795** random data.
796*/
797static 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*/
805static 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*/
812static int jtCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
813 return sqlite3OsCurrentTime(g.pVfs, pTimeOut);
814}
815
danielk1977e86a5b72009-01-08 12:05:56 +0000816/**************************************************************************
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*/
danielk1977a0fc7292008-12-20 18:33:59 +0000827int jt_register(char *zWrap, int isDefault){
828 g.pVfs = sqlite3_vfs_find(zWrap);
829 if( g.pVfs==0 ){
830 return SQLITE_ERROR;
831 }
danielk1977e86a5b72009-01-08 12:05:56 +0000832 jt_vfs.szOsFile = sizeof(jt_file) + g.pVfs->szOsFile;
danielk1977a0fc7292008-12-20 18:33:59 +0000833 sqlite3_vfs_register(&jt_vfs, isDefault);
834 return SQLITE_OK;
835}
836
danielk1977e86a5b72009-01-08 12:05:56 +0000837/*
838** Uninstall the jt VFS, if it is installed.
839*/
danielk197764aca192009-04-07 11:21:28 +0000840void jt_unregister(void){
danielk1977a0fc7292008-12-20 18:33:59 +0000841 sqlite3_vfs_unregister(&jt_vfs);
842}
843
844#endif