xref: /sqlite-3.40.0/src/test_journal.c (revision b39187ae)
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
14 ** an existing VFS. The code in this file attempts to detect a specific
15 ** bug in SQLite - writing data to a database file page when:
16 **
17 **   a) the original page data is not stored in a synced portion of the
18 **      journal file, and
19 **   b) the page was not a free-list leaf page when the transaction was
20 **      first opened.
21 **
22 ** $Id: test_journal.c,v 1.3 2008/12/24 09:30:22 danielk1977 Exp $
23 */
24 #if SQLITE_TEST          /* This file is used for testing only */
25 
26 #include "sqlite3.h"
27 #include "sqliteInt.h"
28 
29 /*
30 ** Maximum pathname length supported by the jt backend.
31 */
32 #define JT_MAX_PATHNAME 512
33 
34 /*
35 ** Name used to identify this VFS.
36 */
37 #define JT_VFS_NAME "jt"
38 
39 typedef struct jt_file jt_file;
40 struct jt_file {
41   sqlite3_file base;
42   const char *zName;       /* Name of open file */
43   int flags;               /* Flags the file was opened with */
44 
45   /* The following are only used by database file file handles */
46   int eLock;               /* Current lock held on the file */
47   u32 nPage;               /* Size of file in pages when transaction started */
48   u32 nPagesize;           /* Page size when transaction started */
49   Bitvec *pWritable;       /* Bitvec of pages that may be written to the file */
50 
51   jt_file *pNext;          /* All files are stored in a linked list */
52   sqlite3_file *pReal;     /* The file handle for the underlying vfs */
53 };
54 
55 /*
56 ** Method declarations for jt_file.
57 */
58 static int jtClose(sqlite3_file*);
59 static int jtRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
60 static int jtWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
61 static int jtTruncate(sqlite3_file*, sqlite3_int64 size);
62 static int jtSync(sqlite3_file*, int flags);
63 static int jtFileSize(sqlite3_file*, sqlite3_int64 *pSize);
64 static int jtLock(sqlite3_file*, int);
65 static int jtUnlock(sqlite3_file*, int);
66 static int jtCheckReservedLock(sqlite3_file*, int *);
67 static int jtFileControl(sqlite3_file*, int op, void *pArg);
68 static int jtSectorSize(sqlite3_file*);
69 static int jtDeviceCharacteristics(sqlite3_file*);
70 
71 /*
72 ** Method declarations for jt_vfs.
73 */
74 static int jtOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
75 static int jtDelete(sqlite3_vfs*, const char *zName, int syncDir);
76 static int jtAccess(sqlite3_vfs*, const char *zName, int flags, int *);
77 static int jtFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
78 #ifndef SQLITE_OMIT_LOAD_EXTENSION
79 static void *jtDlOpen(sqlite3_vfs*, const char *zFilename);
80 static void jtDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
81 static void (*jtDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
82 static void jtDlClose(sqlite3_vfs*, void*);
83 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
84 static int jtRandomness(sqlite3_vfs*, int nByte, char *zOut);
85 static int jtSleep(sqlite3_vfs*, int microseconds);
86 static int jtCurrentTime(sqlite3_vfs*, double*);
87 
88 static sqlite3_vfs jt_vfs = {
89   1,                             /* iVersion */
90   sizeof(jt_file),               /* szOsFile */
91   JT_MAX_PATHNAME,               /* mxPathname */
92   0,                             /* pNext */
93   JT_VFS_NAME,                   /* zName */
94   0,                             /* pAppData */
95   jtOpen,                        /* xOpen */
96   jtDelete,                      /* xDelete */
97   jtAccess,                      /* xAccess */
98   jtFullPathname,                /* xFullPathname */
99 #ifndef SQLITE_OMIT_LOAD_EXTENSION
100   jtDlOpen,                      /* xDlOpen */
101   jtDlError,                     /* xDlError */
102   jtDlSym,                       /* xDlSym */
103   jtDlClose,                     /* xDlClose */
104 #else
105   0,                             /* xDlOpen */
106   0,                             /* xDlError */
107   0,                             /* xDlSym */
108   0,                             /* xDlClose */
109 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
110   jtRandomness,                  /* xRandomness */
111   jtSleep,                       /* xSleep */
112   jtCurrentTime                  /* xCurrentTime */
113 };
114 
115 static sqlite3_io_methods jt_io_methods = {
116   1,                             /* iVersion */
117   jtClose,                       /* xClose */
118   jtRead,                        /* xRead */
119   jtWrite,                       /* xWrite */
120   jtTruncate,                    /* xTruncate */
121   jtSync,                        /* xSync */
122   jtFileSize,                    /* xFileSize */
123   jtLock,                        /* xLock */
124   jtUnlock,                      /* xUnlock */
125   jtCheckReservedLock,           /* xCheckReservedLock */
126   jtFileControl,                 /* xFileControl */
127   jtSectorSize,                  /* xSectorSize */
128   jtDeviceCharacteristics        /* xDeviceCharacteristics */
129 };
130 
131 struct JtGlobal {
132   sqlite3_vfs *pVfs;
133   jt_file *pList;
134 };
135 static struct JtGlobal g = {0, 0};
136 
137 static void closeTransaction(jt_file *p){
138   sqlite3BitvecDestroy(p->pWritable);
139   p->pWritable = 0;
140 }
141 
142 /*
143 ** Close an jt-file.
144 */
145 static int jtClose(sqlite3_file *pFile){
146   jt_file **pp;
147   jt_file *p = (jt_file *)pFile;
148 
149   if( p->zName ){
150     for(pp=&g.pList; *pp!=p; pp=&(*pp)->pNext);
151     *pp = p->pNext;
152   }
153 
154   return sqlite3OsClose(p->pReal);
155 }
156 
157 /*
158 ** Read data from an jt-file.
159 */
160 static int jtRead(
161   sqlite3_file *pFile,
162   void *zBuf,
163   int iAmt,
164   sqlite_int64 iOfst
165 ){
166   jt_file *p = (jt_file *)pFile;
167   return sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
168 }
169 
170 
171 static jt_file *locateDatabaseHandle(const char *zJournal){
172   jt_file *pMain;
173   for(pMain=g.pList; pMain; pMain=pMain->pNext){
174     int nName = strlen(zJournal) - strlen("-journal");
175     if( (pMain->flags&SQLITE_OPEN_MAIN_DB)
176      && (strlen(pMain->zName)==nName)
177      && 0==memcmp(pMain->zName, zJournal, nName)
178      && (pMain->eLock>=SQLITE_LOCK_RESERVED)
179     ){
180       break;
181     }
182   }
183   return pMain;
184 }
185 
186 
187 static u32 decodeUint32(const unsigned char *z){
188   return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
189 }
190 
191 static int readFreelist(jt_file *pMain){
192   int rc;
193   sqlite3_file *p = pMain->pReal;
194   sqlite3_int64 iSize;
195 
196   rc = sqlite3OsFileSize(p, &iSize);
197   if( rc==SQLITE_OK && iSize>=pMain->nPagesize ){
198     unsigned char *zBuf = (unsigned char *)malloc(pMain->nPagesize);
199     u32 iTrunk;
200 
201     rc = sqlite3OsRead(p, zBuf, pMain->nPagesize, 0);
202     iTrunk = decodeUint32(&zBuf[32]);
203     while( rc==SQLITE_OK && iTrunk>0 ){
204       u32 nLeaf;
205       u32 iLeaf;
206       sqlite3_int64 iOff = (iTrunk-1)*pMain->nPagesize;
207       rc = sqlite3OsRead(p, zBuf, pMain->nPagesize, iOff);
208       nLeaf = decodeUint32(&zBuf[4]);
209       for(iLeaf=0; rc==SQLITE_OK && iLeaf<nLeaf; iLeaf++){
210         u32 pgno = decodeUint32(&zBuf[8+4*iLeaf]);
211         sqlite3BitvecSet(pMain->pWritable, pgno);
212       }
213       iTrunk = decodeUint32(zBuf);
214     }
215 
216     free(zBuf);
217   }
218 
219   return rc;
220 }
221 
222 /*
223 ** The first argument, zBuf, points to a buffer containing a 28 byte
224 ** serialized journal header. This function deserializes four of the
225 ** integer fields contained in the journal header and writes their
226 ** values to the output variables.
227 */
228 static int decodeJournalHdr(
229   const unsigned char *zBuf,         /* Input: 28 byte journal header */
230   u32 *pnRec,                        /* Out: Number of journalled records */
231   u32 *pnPage,                       /* Out: Original database page count */
232   u32 *pnSector,                     /* Out: Sector size in bytes */
233   u32 *pnPagesize                    /* Out: Page size in bytes */
234 ){
235   unsigned char aMagic[] = { 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7 };
236   if( memcmp(aMagic, zBuf, 8) ) return 1;
237   if( pnRec ) *pnRec = decodeUint32(&zBuf[8]);
238   if( pnPage ) *pnPage = decodeUint32(&zBuf[16]);
239   if( pnSector ) *pnSector = decodeUint32(&zBuf[20]);
240   if( pnPagesize ) *pnPagesize = decodeUint32(&zBuf[24]);
241   return 0;
242 }
243 
244 /*
245 ** Write data to an jt-file.
246 */
247 static int jtWrite(
248   sqlite3_file *pFile,
249   const void *zBuf,
250   int iAmt,
251   sqlite_int64 iOfst
252 ){
253   jt_file *p = (jt_file *)pFile;
254   if( p->flags&SQLITE_OPEN_MAIN_JOURNAL && iOfst==0 ){
255     jt_file *pMain = locateDatabaseHandle(p->zName);
256     assert( pMain );
257 
258     if( decodeJournalHdr(zBuf, 0, &pMain->nPage, 0, &pMain->nPagesize) ){
259       /* Zeroing the first journal-file header. This is the end of a
260       ** transaction. */
261       closeTransaction(pMain);
262     }else{
263       /* Writing the first journal header to a journal file. This happens
264       ** when a transaction is first started.  */
265       int rc;
266       pMain->pWritable = sqlite3BitvecCreate(pMain->nPage);
267       if( !pMain->pWritable ){
268 	return SQLITE_IOERR_NOMEM;
269       }
270       rc = readFreelist(pMain);
271       if( rc!=SQLITE_OK ){
272         return rc;
273       }
274     }
275   }
276 
277   if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable && iAmt==p->nPagesize ){
278     u32 pgno = iOfst/p->nPagesize + 1;
279     assert( pgno>p->nPage || sqlite3BitvecTest(p->pWritable, pgno) );
280   }
281 
282   return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
283 }
284 
285 /*
286 ** Truncate an jt-file.
287 */
288 static int jtTruncate(sqlite3_file *pFile, sqlite_int64 size){
289   jt_file *p = (jt_file *)pFile;
290   if( p->flags&SQLITE_OPEN_MAIN_JOURNAL && size==0 ){
291     /* Truncating a journal file. This is the end of a transaction. */
292     jt_file *pMain = locateDatabaseHandle(p->zName);
293     closeTransaction(pMain);
294   }
295   return sqlite3OsTruncate(p->pReal, size);
296 }
297 
298 /*
299 ** The first argument to this function is a handle open on a journal file.
300 ** This function reads the journal file and adds the page number for each
301 ** page in the journal to the Bitvec object passed as the second argument.
302 */
303 static int readJournalFile(jt_file *p, jt_file *pMain){
304   int rc;
305   unsigned char zBuf[28];
306   sqlite3_file *pReal = p->pReal;
307   sqlite3_int64 iOff = 0;
308   sqlite3_int64 iSize = 0;
309 
310   rc = sqlite3OsFileSize(p->pReal, &iSize);
311   while( rc==SQLITE_OK && iOff<iSize ){
312     u32 nRec, nPage, nSector, nPagesize;
313     u32 ii;
314     rc = sqlite3OsRead(pReal, zBuf, 28, iOff);
315     if( rc!=SQLITE_OK
316      || decodeJournalHdr(zBuf, &nRec, &nPage, &nSector, &nPagesize)
317     ){
318       return rc;
319     }
320     iOff += nSector;
321     if( nRec==0 ){
322       nRec = (iSize - iOff)/(pMain->nPagesize + 8);
323     }
324     for(ii=0; rc==SQLITE_OK && ii<nRec && iOff<iSize; ii++){
325       u32 pgno;
326       rc = sqlite3OsRead(pReal, zBuf, 4, iOff);
327       if( rc==SQLITE_OK ){
328         pgno = decodeUint32(zBuf);
329         iOff += (8 + pMain->nPagesize);
330         if( pgno>0 && pgno<=pMain->nPage ){
331           sqlite3BitvecSet(pMain->pWritable, pgno);
332         }
333       }
334     }
335 
336     iOff = ((iOff + (nSector-1)) / nSector) * nSector;
337   }
338 
339   return rc;
340 }
341 
342 /*
343 ** Sync an jt-file.
344 */
345 static int jtSync(sqlite3_file *pFile, int flags){
346   jt_file *p = (jt_file *)pFile;
347 
348   if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){
349     int rc;
350     jt_file *pMain;                   /* The associated database file */
351 
352     /* The journal file is being synced. At this point, we inspect the
353     ** contents of the file up to this point and set each bit in the
354     ** jt_file.pWritable bitvec of the main database file associated with
355     ** this journal file.
356     */
357     pMain = locateDatabaseHandle(p->zName);
358     assert(pMain);
359 
360     /* Set the bitvec values */
361     if( pMain->pWritable ){
362       rc = readJournalFile(p, pMain);
363       if( rc!=SQLITE_OK ){
364         return rc;
365       }
366     }
367   }
368 
369   return sqlite3OsSync(p->pReal, flags);
370 }
371 
372 /*
373 ** Return the current file-size of an jt-file.
374 */
375 static int jtFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
376   jt_file *p = (jt_file *)pFile;
377   return sqlite3OsFileSize(p->pReal, pSize);
378 }
379 
380 /*
381 ** Lock an jt-file.
382 */
383 static int jtLock(sqlite3_file *pFile, int eLock){
384   int rc;
385   jt_file *p = (jt_file *)pFile;
386   rc = sqlite3OsLock(p->pReal, eLock);
387   if( rc==SQLITE_OK && eLock>p->eLock ){
388     p->eLock = eLock;
389   }
390   return rc;
391 }
392 
393 /*
394 ** Unlock an jt-file.
395 */
396 static int jtUnlock(sqlite3_file *pFile, int eLock){
397   int rc;
398   jt_file *p = (jt_file *)pFile;
399   rc = sqlite3OsUnlock(p->pReal, eLock);
400   if( rc==SQLITE_OK && eLock<p->eLock ){
401     p->eLock = eLock;
402   }
403   return rc;
404 }
405 
406 /*
407 ** Check if another file-handle holds a RESERVED lock on an jt-file.
408 */
409 static int jtCheckReservedLock(sqlite3_file *pFile, int *pResOut){
410   jt_file *p = (jt_file *)pFile;
411   return sqlite3OsCheckReservedLock(p->pReal, pResOut);
412 }
413 
414 /*
415 ** File control method. For custom operations on an jt-file.
416 */
417 static int jtFileControl(sqlite3_file *pFile, int op, void *pArg){
418   jt_file *p = (jt_file *)pFile;
419   return sqlite3OsFileControl(p->pReal, op, pArg);
420 }
421 
422 /*
423 ** Return the sector-size in bytes for an jt-file.
424 */
425 static int jtSectorSize(sqlite3_file *pFile){
426   jt_file *p = (jt_file *)pFile;
427   return sqlite3OsSectorSize(p->pReal);
428 }
429 
430 /*
431 ** Return the device characteristic flags supported by an jt-file.
432 */
433 static int jtDeviceCharacteristics(sqlite3_file *pFile){
434   jt_file *p = (jt_file *)pFile;
435   return sqlite3OsDeviceCharacteristics(p->pReal);
436 }
437 
438 /*
439 ** Open an jt file handle.
440 */
441 static int jtOpen(
442   sqlite3_vfs *pVfs,
443   const char *zName,
444   sqlite3_file *pFile,
445   int flags,
446   int *pOutFlags
447 ){
448   int rc;
449   jt_file *p = (jt_file *)pFile;
450   p->pReal = (sqlite3_file *)&p[1];
451   p->pReal->pMethods = 0;
452   rc = sqlite3OsOpen(g.pVfs, zName, p->pReal, flags, pOutFlags);
453   assert( rc==SQLITE_OK || p->pReal->pMethods==0 );
454   if( rc==SQLITE_OK ){
455     pFile->pMethods = &jt_io_methods;
456     p->eLock = 0;
457     p->zName = zName;
458     p->flags = flags;
459     p->pNext = 0;
460     p->pWritable = 0;
461     if( zName ){
462       p->pNext = g.pList;
463       g.pList = p;
464     }
465   }
466   return rc;
467 }
468 
469 /*
470 ** Delete the file located at zPath. If the dirSync argument is true,
471 ** ensure the file-system modifications are synced to disk before
472 ** returning.
473 */
474 static int jtDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
475   int nPath = strlen(zPath);
476   if( nPath>8 && 0==strcmp("-journal", &zPath[nPath-8]) ){
477     /* Deleting a journal file. The end of a transaction. */
478     jt_file *pMain = locateDatabaseHandle(zPath);
479     if( pMain ){
480       closeTransaction(pMain);
481     }
482   }
483 
484   return sqlite3OsDelete(g.pVfs, zPath, dirSync);
485 }
486 
487 /*
488 ** Test for access permissions. Return true if the requested permission
489 ** is available, or false otherwise.
490 */
491 static int jtAccess(
492   sqlite3_vfs *pVfs,
493   const char *zPath,
494   int flags,
495   int *pResOut
496 ){
497   return sqlite3OsAccess(g.pVfs, zPath, flags, pResOut);
498 }
499 
500 /*
501 ** Populate buffer zOut with the full canonical pathname corresponding
502 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
503 ** of at least (JT_MAX_PATHNAME+1) bytes.
504 */
505 static int jtFullPathname(
506   sqlite3_vfs *pVfs,
507   const char *zPath,
508   int nOut,
509   char *zOut
510 ){
511   return sqlite3OsFullPathname(g.pVfs, zPath, nOut, zOut);
512 }
513 
514 #ifndef SQLITE_OMIT_LOAD_EXTENSION
515 /*
516 ** Open the dynamic library located at zPath and return a handle.
517 */
518 static void *jtDlOpen(sqlite3_vfs *pVfs, const char *zPath){
519   return sqlite3OsDlOpen(g.pVfs, zPath);
520 }
521 
522 /*
523 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
524 ** utf-8 string describing the most recent error encountered associated
525 ** with dynamic libraries.
526 */
527 static void jtDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
528   sqlite3OsDlError(g.pVfs, nByte, zErrMsg);
529 }
530 
531 /*
532 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
533 */
534 static void (*jtDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
535   return sqlite3OsDlSym(g.pVfs, p, zSym);
536 }
537 
538 /*
539 ** Close the dynamic library handle pHandle.
540 */
541 static void jtDlClose(sqlite3_vfs *pVfs, void *pHandle){
542   sqlite3OsDlClose(g.pVfs, pHandle);
543 }
544 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
545 
546 /*
547 ** Populate the buffer pointed to by zBufOut with nByte bytes of
548 ** random data.
549 */
550 static int jtRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
551   return sqlite3OsRandomness(g.pVfs, nByte, zBufOut);
552 }
553 
554 /*
555 ** Sleep for nMicro microseconds. Return the number of microseconds
556 ** actually slept.
557 */
558 static int jtSleep(sqlite3_vfs *pVfs, int nMicro){
559   return sqlite3OsSleep(g.pVfs, nMicro);
560 }
561 
562 /*
563 ** Return the current time as a Julian Day number in *pTimeOut.
564 */
565 static int jtCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
566   return sqlite3OsCurrentTime(g.pVfs, pTimeOut);
567 }
568 
569 int jt_register(char *zWrap, int isDefault){
570   g.pVfs = sqlite3_vfs_find(zWrap);
571   if( g.pVfs==0 ){
572     return SQLITE_ERROR;
573   }
574   jt_vfs.szOsFile += g.pVfs->szOsFile;
575   sqlite3_vfs_register(&jt_vfs, isDefault);
576   return SQLITE_OK;
577 }
578 
579 void jt_unregister(){
580   sqlite3_vfs_unregister(&jt_vfs);
581 }
582 
583 #endif
584