xref: /sqlite-3.40.0/src/test_onefile.c (revision f72a8751)
1 /*
2 ** 2007 September 14
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 ** OVERVIEW:
14 **
15 **   This file contains some example code demonstrating how the SQLite
16 **   vfs feature can be used to have SQLite operate directly on an
17 **   embedded media, without using an intermediate file system.
18 **
19 **   Because this is only a demo designed to run on a workstation, the
20 **   underlying media is simulated using a regular file-system file. The
21 **   size of the file is fixed when it is first created (default size 10 MB).
22 **   From SQLite's point of view, this space is used to store a single
23 **   database file and the journal file.
24 **
25 **   Any statement journal created is stored in volatile memory obtained
26 **   from sqlite3_malloc(). Any attempt to create a temporary database file
27 **   will fail (SQLITE_IOERR). To prevent SQLite from attempting this,
28 **   it should be configured to store all temporary database files in
29 **   main memory (see pragma "temp_store" or the SQLITE_TEMP_STORE compile
30 **   time option).
31 **
32 ** ASSUMPTIONS:
33 **
34 **   After it has been created, the blob file is accessed using the
35 **   following three functions only:
36 **
37 **       mediaRead();            - Read a 512 byte block from the file.
38 **       mediaWrite();           - Write a 512 byte block to the file.
39 **       mediaSync();            - Tell the media hardware to sync.
40 **
41 **   It is assumed that these can be easily implemented by any "real"
42 **   media vfs driver adapting this code.
43 **
44 ** FILE FORMAT:
45 **
46 **   The basic principle is that the "database file" is stored at the
47 **   beginning of the 10 MB blob and grows in a forward direction. The
48 **   "journal file" is stored at the end of the 10MB blob and grows
49 **   in the reverse direction. If, during a transaction, insufficient
50 **   space is available to expand either the journal or database file,
51 **   an SQLITE_FULL error is returned. The database file is never allowed
52 **   to consume more than 90% of the blob space. If SQLite tries to
53 **   create a file larger than this, SQLITE_FULL is returned.
54 **
55 **   No allowance is made for "wear-leveling", as is required by.
56 **   embedded devices in the absence of equivalent hardware features.
57 **
58 **   The first 512 block byte of the file is reserved for storing the
59 **   size of the "database file". It is updated as part of the sync()
60 **   operation. On startup, it can only be trusted if no journal file
61 **   exists. If a journal-file does exist, then it stores the real size
62 **   of the database region. The second and subsequent blocks store the
63 **   actual database content.
64 **
65 **   The size of the "journal file" is not stored persistently in the
66 **   file. When the system is running, the size of the journal file is
67 **   stored in volatile memory. When recovering from a crash, this vfs
68 **   reports a very large size for the journal file. The normal journal
69 **   header and checksum mechanisms serve to prevent SQLite from
70 **   processing any data that lies past the logical end of the journal.
71 **
72 **   When SQLite calls OsDelete() to delete the journal file, the final
73 **   512 bytes of the blob (the area containing the first journal header)
74 **   are zeroed.
75 **
76 ** LOCKING:
77 **
78 **   File locking is a no-op. Only one connection may be open at any one
79 **   time using this demo vfs.
80 */
81 
82 #include "sqlite3.h"
83 #include <assert.h>
84 #include <string.h>
85 
86 /*
87 ** Maximum pathname length supported by the fs backend.
88 */
89 #define BLOCKSIZE 512
90 #define BLOBSIZE 10485760
91 
92 /*
93 ** Name used to identify this VFS.
94 */
95 #define FS_VFS_NAME "fs"
96 
97 typedef struct fs_real_file fs_real_file;
98 struct fs_real_file {
99   sqlite3_file *pFile;
100   const char *zName;
101   int nDatabase;              /* Current size of database region */
102   int nJournal;               /* Current size of journal region */
103   int nBlob;                  /* Total size of allocated blob */
104   int nRef;                   /* Number of pointers to this structure */
105   fs_real_file *pNext;
106   fs_real_file **ppThis;
107 };
108 
109 typedef struct fs_file fs_file;
110 struct fs_file {
111   sqlite3_file base;
112   int eType;
113   fs_real_file *pReal;
114 };
115 
116 typedef struct tmp_file tmp_file;
117 struct tmp_file {
118   sqlite3_file base;
119   int nSize;
120   int nAlloc;
121   char *zAlloc;
122 };
123 
124 /* Values for fs_file.eType. */
125 #define DATABASE_FILE   1
126 #define JOURNAL_FILE    2
127 
128 /*
129 ** Method declarations for fs_file.
130 */
131 static int fsClose(sqlite3_file*);
132 static int fsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
133 static int fsWrite(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
134 static int fsTruncate(sqlite3_file*, sqlite3_int64 size);
135 static int fsSync(sqlite3_file*, int flags);
136 static int fsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
137 static int fsLock(sqlite3_file*, int);
138 static int fsUnlock(sqlite3_file*, int);
139 static int fsCheckReservedLock(sqlite3_file*, int *pResOut);
140 static int fsFileControl(sqlite3_file*, int op, void *pArg);
141 static int fsSectorSize(sqlite3_file*);
142 static int fsDeviceCharacteristics(sqlite3_file*);
143 
144 /*
145 ** Method declarations for tmp_file.
146 */
147 static int tmpClose(sqlite3_file*);
148 static int tmpRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
149 static int tmpWrite(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
150 static int tmpTruncate(sqlite3_file*, sqlite3_int64 size);
151 static int tmpSync(sqlite3_file*, int flags);
152 static int tmpFileSize(sqlite3_file*, sqlite3_int64 *pSize);
153 static int tmpLock(sqlite3_file*, int);
154 static int tmpUnlock(sqlite3_file*, int);
155 static int tmpCheckReservedLock(sqlite3_file*, int *pResOut);
156 static int tmpFileControl(sqlite3_file*, int op, void *pArg);
157 static int tmpSectorSize(sqlite3_file*);
158 static int tmpDeviceCharacteristics(sqlite3_file*);
159 
160 /*
161 ** Method declarations for fs_vfs.
162 */
163 static int fsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
164 static int fsDelete(sqlite3_vfs*, const char *zName, int syncDir);
165 static int fsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
166 static int fsFullPathname(sqlite3_vfs*, const char *zName, int nOut,char *zOut);
167 static void *fsDlOpen(sqlite3_vfs*, const char *zFilename);
168 static void fsDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
169 static void (*fsDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
170 static void fsDlClose(sqlite3_vfs*, void*);
171 static int fsRandomness(sqlite3_vfs*, int nByte, char *zOut);
172 static int fsSleep(sqlite3_vfs*, int microseconds);
173 static int fsCurrentTime(sqlite3_vfs*, double*);
174 
175 
176 typedef struct fs_vfs_t fs_vfs_t;
177 struct fs_vfs_t {
178   sqlite3_vfs base;
179   fs_real_file *pFileList;
180   sqlite3_vfs *pParent;
181 };
182 
183 static fs_vfs_t fs_vfs = {
184   {
185     1,                                          /* iVersion */
186     0,                                          /* szOsFile */
187     0,                                          /* mxPathname */
188     0,                                          /* pNext */
189     FS_VFS_NAME,                                /* zName */
190     0,                                          /* pAppData */
191     fsOpen,                                     /* xOpen */
192     fsDelete,                                   /* xDelete */
193     fsAccess,                                   /* xAccess */
194     fsFullPathname,                             /* xFullPathname */
195     fsDlOpen,                                   /* xDlOpen */
196     fsDlError,                                  /* xDlError */
197     fsDlSym,                                    /* xDlSym */
198     fsDlClose,                                  /* xDlClose */
199     fsRandomness,                               /* xRandomness */
200     fsSleep,                                    /* xSleep */
201     fsCurrentTime,                              /* xCurrentTime */
202     0,                                          /* xShmOpen */
203     0,                                          /* xShmSize */
204     0,                                          /* xShmLock */
205     0,                                          /* xShmClose */
206     0,                                          /* xShmDelete */
207     0,                                          /* xRename */
208     0                                           /* xCurrentTimeInt64 */
209   },
210   0,                                            /* pFileList */
211   0                                             /* pParent */
212 };
213 
214 static sqlite3_io_methods fs_io_methods = {
215   1,                            /* iVersion */
216   fsClose,                      /* xClose */
217   fsRead,                       /* xRead */
218   fsWrite,                      /* xWrite */
219   fsTruncate,                   /* xTruncate */
220   fsSync,                       /* xSync */
221   fsFileSize,                   /* xFileSize */
222   fsLock,                       /* xLock */
223   fsUnlock,                     /* xUnlock */
224   fsCheckReservedLock,          /* xCheckReservedLock */
225   fsFileControl,                /* xFileControl */
226   fsSectorSize,                 /* xSectorSize */
227   fsDeviceCharacteristics       /* xDeviceCharacteristics */
228 };
229 
230 
231 static sqlite3_io_methods tmp_io_methods = {
232   1,                            /* iVersion */
233   tmpClose,                     /* xClose */
234   tmpRead,                      /* xRead */
235   tmpWrite,                     /* xWrite */
236   tmpTruncate,                  /* xTruncate */
237   tmpSync,                      /* xSync */
238   tmpFileSize,                  /* xFileSize */
239   tmpLock,                      /* xLock */
240   tmpUnlock,                    /* xUnlock */
241   tmpCheckReservedLock,         /* xCheckReservedLock */
242   tmpFileControl,               /* xFileControl */
243   tmpSectorSize,                /* xSectorSize */
244   tmpDeviceCharacteristics      /* xDeviceCharacteristics */
245 };
246 
247 /* Useful macros used in several places */
248 #define MIN(x,y) ((x)<(y)?(x):(y))
249 #define MAX(x,y) ((x)>(y)?(x):(y))
250 
251 
252 /*
253 ** Close a tmp-file.
254 */
255 static int tmpClose(sqlite3_file *pFile){
256   tmp_file *pTmp = (tmp_file *)pFile;
257   sqlite3_free(pTmp->zAlloc);
258   return SQLITE_OK;
259 }
260 
261 /*
262 ** Read data from a tmp-file.
263 */
264 static int tmpRead(
265   sqlite3_file *pFile,
266   void *zBuf,
267   int iAmt,
268   sqlite_int64 iOfst
269 ){
270   tmp_file *pTmp = (tmp_file *)pFile;
271   if( (iAmt+iOfst)>pTmp->nSize ){
272     return SQLITE_IOERR_SHORT_READ;
273   }
274   memcpy(zBuf, &pTmp->zAlloc[iOfst], iAmt);
275   return SQLITE_OK;
276 }
277 
278 /*
279 ** Write data to a tmp-file.
280 */
281 static int tmpWrite(
282   sqlite3_file *pFile,
283   const void *zBuf,
284   int iAmt,
285   sqlite_int64 iOfst
286 ){
287   tmp_file *pTmp = (tmp_file *)pFile;
288   if( (iAmt+iOfst)>pTmp->nAlloc ){
289     int nNew = 2*(iAmt+iOfst+pTmp->nAlloc);
290     char *zNew = sqlite3_realloc(pTmp->zAlloc, nNew);
291     if( !zNew ){
292       return SQLITE_NOMEM;
293     }
294     pTmp->zAlloc = zNew;
295     pTmp->nAlloc = nNew;
296   }
297   memcpy(&pTmp->zAlloc[iOfst], zBuf, iAmt);
298   pTmp->nSize = MAX(pTmp->nSize, iOfst+iAmt);
299   return SQLITE_OK;
300 }
301 
302 /*
303 ** Truncate a tmp-file.
304 */
305 static int tmpTruncate(sqlite3_file *pFile, sqlite_int64 size){
306   tmp_file *pTmp = (tmp_file *)pFile;
307   pTmp->nSize = MIN(pTmp->nSize, size);
308   return SQLITE_OK;
309 }
310 
311 /*
312 ** Sync a tmp-file.
313 */
314 static int tmpSync(sqlite3_file *pFile, int flags){
315   return SQLITE_OK;
316 }
317 
318 /*
319 ** Return the current file-size of a tmp-file.
320 */
321 static int tmpFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
322   tmp_file *pTmp = (tmp_file *)pFile;
323   *pSize = pTmp->nSize;
324   return SQLITE_OK;
325 }
326 
327 /*
328 ** Lock a tmp-file.
329 */
330 static int tmpLock(sqlite3_file *pFile, int eLock){
331   return SQLITE_OK;
332 }
333 
334 /*
335 ** Unlock a tmp-file.
336 */
337 static int tmpUnlock(sqlite3_file *pFile, int eLock){
338   return SQLITE_OK;
339 }
340 
341 /*
342 ** Check if another file-handle holds a RESERVED lock on a tmp-file.
343 */
344 static int tmpCheckReservedLock(sqlite3_file *pFile, int *pResOut){
345   *pResOut = 0;
346   return SQLITE_OK;
347 }
348 
349 /*
350 ** File control method. For custom operations on a tmp-file.
351 */
352 static int tmpFileControl(sqlite3_file *pFile, int op, void *pArg){
353   return SQLITE_OK;
354 }
355 
356 /*
357 ** Return the sector-size in bytes for a tmp-file.
358 */
359 static int tmpSectorSize(sqlite3_file *pFile){
360   return 0;
361 }
362 
363 /*
364 ** Return the device characteristic flags supported by a tmp-file.
365 */
366 static int tmpDeviceCharacteristics(sqlite3_file *pFile){
367   return 0;
368 }
369 
370 /*
371 ** Close an fs-file.
372 */
373 static int fsClose(sqlite3_file *pFile){
374   int rc = SQLITE_OK;
375   fs_file *p = (fs_file *)pFile;
376   fs_real_file *pReal = p->pReal;
377 
378   /* Decrement the real_file ref-count. */
379   pReal->nRef--;
380   assert(pReal->nRef>=0);
381 
382   /* When the ref-count reaches 0, destroy the structure */
383   if( pReal->nRef==0 ){
384     *pReal->ppThis = pReal->pNext;
385     if( pReal->pNext ){
386       pReal->pNext->ppThis = pReal->ppThis;
387     }
388     rc = pReal->pFile->pMethods->xClose(pReal->pFile);
389     sqlite3_free(pReal);
390   }
391 
392   return rc;
393 }
394 
395 /*
396 ** Read data from an fs-file.
397 */
398 static int fsRead(
399   sqlite3_file *pFile,
400   void *zBuf,
401   int iAmt,
402   sqlite_int64 iOfst
403 ){
404   int rc = SQLITE_OK;
405   fs_file *p = (fs_file *)pFile;
406   fs_real_file *pReal = p->pReal;
407   sqlite3_file *pF = pReal->pFile;
408 
409   if( (p->eType==DATABASE_FILE && (iAmt+iOfst)>pReal->nDatabase)
410    || (p->eType==JOURNAL_FILE && (iAmt+iOfst)>pReal->nJournal)
411   ){
412     rc = SQLITE_IOERR_SHORT_READ;
413   }else if( p->eType==DATABASE_FILE ){
414     rc = pF->pMethods->xRead(pF, zBuf, iAmt, iOfst+BLOCKSIZE);
415   }else{
416     /* Journal file. */
417     int iRem = iAmt;
418     int iBuf = 0;
419     int ii = iOfst;
420     while( iRem>0 && rc==SQLITE_OK ){
421       int iRealOff = pReal->nBlob - BLOCKSIZE*((ii/BLOCKSIZE)+1) + ii%BLOCKSIZE;
422       int iRealAmt = MIN(iRem, BLOCKSIZE - (iRealOff%BLOCKSIZE));
423 
424       rc = pF->pMethods->xRead(pF, &((char *)zBuf)[iBuf], iRealAmt, iRealOff);
425       ii += iRealAmt;
426       iBuf += iRealAmt;
427       iRem -= iRealAmt;
428     }
429   }
430 
431   return rc;
432 }
433 
434 /*
435 ** Write data to an fs-file.
436 */
437 static int fsWrite(
438   sqlite3_file *pFile,
439   const void *zBuf,
440   int iAmt,
441   sqlite_int64 iOfst
442 ){
443   int rc = SQLITE_OK;
444   fs_file *p = (fs_file *)pFile;
445   fs_real_file *pReal = p->pReal;
446   sqlite3_file *pF = pReal->pFile;
447 
448   if( p->eType==DATABASE_FILE ){
449     if( (iAmt+iOfst+BLOCKSIZE)>(pReal->nBlob-pReal->nJournal) ){
450       rc = SQLITE_FULL;
451     }else{
452       rc = pF->pMethods->xWrite(pF, zBuf, iAmt, iOfst+BLOCKSIZE);
453       if( rc==SQLITE_OK ){
454         pReal->nDatabase = MAX(pReal->nDatabase, iAmt+iOfst);
455       }
456     }
457   }else{
458     /* Journal file. */
459     int iRem = iAmt;
460     int iBuf = 0;
461     int ii = iOfst;
462     while( iRem>0 && rc==SQLITE_OK ){
463       int iRealOff = pReal->nBlob - BLOCKSIZE*((ii/BLOCKSIZE)+1) + ii%BLOCKSIZE;
464       int iRealAmt = MIN(iRem, BLOCKSIZE - (iRealOff%BLOCKSIZE));
465 
466       if( iRealOff<(pReal->nDatabase+BLOCKSIZE) ){
467         rc = SQLITE_FULL;
468       }else{
469         rc = pF->pMethods->xWrite(pF, &((char *)zBuf)[iBuf], iRealAmt,iRealOff);
470         ii += iRealAmt;
471         iBuf += iRealAmt;
472         iRem -= iRealAmt;
473       }
474     }
475     if( rc==SQLITE_OK ){
476       pReal->nJournal = MAX(pReal->nJournal, iAmt+iOfst);
477     }
478   }
479 
480   return rc;
481 }
482 
483 /*
484 ** Truncate an fs-file.
485 */
486 static int fsTruncate(sqlite3_file *pFile, sqlite_int64 size){
487   fs_file *p = (fs_file *)pFile;
488   fs_real_file *pReal = p->pReal;
489   if( p->eType==DATABASE_FILE ){
490     pReal->nDatabase = MIN(pReal->nDatabase, size);
491   }else{
492     pReal->nJournal = MIN(pReal->nJournal, size);
493   }
494   return SQLITE_OK;
495 }
496 
497 /*
498 ** Sync an fs-file.
499 */
500 static int fsSync(sqlite3_file *pFile, int flags){
501   fs_file *p = (fs_file *)pFile;
502   fs_real_file *pReal = p->pReal;
503   sqlite3_file *pRealFile = pReal->pFile;
504   int rc = SQLITE_OK;
505 
506   if( p->eType==DATABASE_FILE ){
507     unsigned char zSize[4];
508     zSize[0] = (pReal->nDatabase&0xFF000000)>>24;
509     zSize[1] = (pReal->nDatabase&0x00FF0000)>>16;
510     zSize[2] = (pReal->nDatabase&0x0000FF00)>>8;
511     zSize[3] = (pReal->nDatabase&0x000000FF);
512     rc = pRealFile->pMethods->xWrite(pRealFile, zSize, 4, 0);
513   }
514   if( rc==SQLITE_OK ){
515     rc = pRealFile->pMethods->xSync(pRealFile, flags&(~SQLITE_SYNC_DATAONLY));
516   }
517 
518   return rc;
519 }
520 
521 /*
522 ** Return the current file-size of an fs-file.
523 */
524 static int fsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
525   fs_file *p = (fs_file *)pFile;
526   fs_real_file *pReal = p->pReal;
527   if( p->eType==DATABASE_FILE ){
528     *pSize = pReal->nDatabase;
529   }else{
530     *pSize = pReal->nJournal;
531   }
532   return SQLITE_OK;
533 }
534 
535 /*
536 ** Lock an fs-file.
537 */
538 static int fsLock(sqlite3_file *pFile, int eLock){
539   return SQLITE_OK;
540 }
541 
542 /*
543 ** Unlock an fs-file.
544 */
545 static int fsUnlock(sqlite3_file *pFile, int eLock){
546   return SQLITE_OK;
547 }
548 
549 /*
550 ** Check if another file-handle holds a RESERVED lock on an fs-file.
551 */
552 static int fsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
553   *pResOut = 0;
554   return SQLITE_OK;
555 }
556 
557 /*
558 ** File control method. For custom operations on an fs-file.
559 */
560 static int fsFileControl(sqlite3_file *pFile, int op, void *pArg){
561   return SQLITE_OK;
562 }
563 
564 /*
565 ** Return the sector-size in bytes for an fs-file.
566 */
567 static int fsSectorSize(sqlite3_file *pFile){
568   return BLOCKSIZE;
569 }
570 
571 /*
572 ** Return the device characteristic flags supported by an fs-file.
573 */
574 static int fsDeviceCharacteristics(sqlite3_file *pFile){
575   return 0;
576 }
577 
578 /*
579 ** Open an fs file handle.
580 */
581 static int fsOpen(
582   sqlite3_vfs *pVfs,
583   const char *zName,
584   sqlite3_file *pFile,
585   int flags,
586   int *pOutFlags
587 ){
588   fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs;
589   fs_file *p = (fs_file *)pFile;
590   fs_real_file *pReal = 0;
591   int eType;
592   int nName;
593   int rc = SQLITE_OK;
594 
595   if( 0==(flags&(SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_MAIN_JOURNAL)) ){
596     tmp_file *p = (tmp_file *)pFile;
597     memset(p, 0, sizeof(*p));
598     p->base.pMethods = &tmp_io_methods;
599     return SQLITE_OK;
600   }
601 
602   eType = ((flags&(SQLITE_OPEN_MAIN_DB))?DATABASE_FILE:JOURNAL_FILE);
603   p->base.pMethods = &fs_io_methods;
604   p->eType = eType;
605 
606   assert(strlen("-journal")==8);
607   nName = strlen(zName)-((eType==JOURNAL_FILE)?8:0);
608   pReal=pFsVfs->pFileList;
609   for(; pReal && strncmp(pReal->zName, zName, nName); pReal=pReal->pNext);
610 
611   if( !pReal ){
612     int real_flags = (flags&~(SQLITE_OPEN_MAIN_DB))|SQLITE_OPEN_TEMP_DB;
613     sqlite3_int64 size;
614     sqlite3_file *pRealFile;
615     sqlite3_vfs *pParent = pFsVfs->pParent;
616     assert(eType==DATABASE_FILE);
617 
618     pReal = (fs_real_file *)sqlite3_malloc(sizeof(*pReal)+pParent->szOsFile);
619     if( !pReal ){
620       rc = SQLITE_NOMEM;
621       goto open_out;
622     }
623     memset(pReal, 0, sizeof(*pReal)+pParent->szOsFile);
624     pReal->zName = zName;
625     pReal->pFile = (sqlite3_file *)(&pReal[1]);
626 
627     rc = pParent->xOpen(pParent, zName, pReal->pFile, real_flags, pOutFlags);
628     if( rc!=SQLITE_OK ){
629       goto open_out;
630     }
631     pRealFile = pReal->pFile;
632 
633     rc = pRealFile->pMethods->xFileSize(pRealFile, &size);
634     if( rc!=SQLITE_OK ){
635       goto open_out;
636     }
637     if( size==0 ){
638       rc = pRealFile->pMethods->xWrite(pRealFile, "\0", 1, BLOBSIZE-1);
639       pReal->nBlob = BLOBSIZE;
640     }else{
641       unsigned char zS[4];
642       pReal->nBlob = size;
643       rc = pRealFile->pMethods->xRead(pRealFile, zS, 4, 0);
644       pReal->nDatabase = (zS[0]<<24)+(zS[1]<<16)+(zS[2]<<8)+zS[3];
645       if( rc==SQLITE_OK ){
646         rc = pRealFile->pMethods->xRead(pRealFile, zS, 4, pReal->nBlob-4);
647         if( zS[0] || zS[1] || zS[2] || zS[3] ){
648           pReal->nJournal = pReal->nBlob;
649         }
650       }
651     }
652 
653     if( rc==SQLITE_OK ){
654       pReal->pNext = pFsVfs->pFileList;
655       if( pReal->pNext ){
656         pReal->pNext->ppThis = &pReal->pNext;
657       }
658       pReal->ppThis = &pFsVfs->pFileList;
659       pFsVfs->pFileList = pReal;
660     }
661   }
662 
663 open_out:
664   if( pReal ){
665     if( rc==SQLITE_OK ){
666       p->pReal = pReal;
667       pReal->nRef++;
668     }else{
669       if( pReal->pFile->pMethods ){
670         pReal->pFile->pMethods->xClose(pReal->pFile);
671       }
672       sqlite3_free(pReal);
673     }
674   }
675   return rc;
676 }
677 
678 /*
679 ** Delete the file located at zPath. If the dirSync argument is true,
680 ** ensure the file-system modifications are synced to disk before
681 ** returning.
682 */
683 static int fsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
684   int rc = SQLITE_OK;
685   fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs;
686   fs_real_file *pReal;
687   sqlite3_file *pF;
688   int nName = strlen(zPath) - 8;
689 
690   assert(strlen("-journal")==8);
691   assert(strcmp("-journal", &zPath[nName])==0);
692 
693   pReal = pFsVfs->pFileList;
694   for(; pReal && strncmp(pReal->zName, zPath, nName); pReal=pReal->pNext);
695   if( pReal ){
696     pF = pReal->pFile;
697     rc = pF->pMethods->xWrite(pF, "\0\0\0\0", 4, pReal->nBlob-BLOCKSIZE);
698     if( rc==SQLITE_OK ){
699       pReal->nJournal = 0;
700     }
701   }
702   return rc;
703 }
704 
705 /*
706 ** Test for access permissions. Return true if the requested permission
707 ** is available, or false otherwise.
708 */
709 static int fsAccess(
710   sqlite3_vfs *pVfs,
711   const char *zPath,
712   int flags,
713   int *pResOut
714 ){
715   fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs;
716   fs_real_file *pReal;
717   int isJournal = 0;
718   int nName = strlen(zPath);
719 
720   if( flags!=SQLITE_ACCESS_EXISTS ){
721     sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
722     return pParent->xAccess(pParent, zPath, flags, pResOut);
723   }
724 
725   assert(strlen("-journal")==8);
726   if( nName>8 && strcmp("-journal", &zPath[nName-8])==0 ){
727     nName -= 8;
728     isJournal = 1;
729   }
730 
731   pReal = pFsVfs->pFileList;
732   for(; pReal && strncmp(pReal->zName, zPath, nName); pReal=pReal->pNext);
733 
734   *pResOut = (pReal && (!isJournal || pReal->nJournal>0));
735   return SQLITE_OK;
736 }
737 
738 /*
739 ** Populate buffer zOut with the full canonical pathname corresponding
740 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
741 ** of at least (FS_MAX_PATHNAME+1) bytes.
742 */
743 static int fsFullPathname(
744   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
745   const char *zPath,            /* Possibly relative input path */
746   int nOut,                     /* Size of output buffer in bytes */
747   char *zOut                    /* Output buffer */
748 ){
749   sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
750   return pParent->xFullPathname(pParent, zPath, nOut, zOut);
751 }
752 
753 /*
754 ** Open the dynamic library located at zPath and return a handle.
755 */
756 static void *fsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
757   sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
758   return pParent->xDlOpen(pParent, zPath);
759 }
760 
761 /*
762 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
763 ** utf-8 string describing the most recent error encountered associated
764 ** with dynamic libraries.
765 */
766 static void fsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
767   sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
768   pParent->xDlError(pParent, nByte, zErrMsg);
769 }
770 
771 /*
772 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
773 */
774 static void (*fsDlSym(sqlite3_vfs *pVfs, void *pH, const char *zSym))(void){
775   sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
776   return pParent->xDlSym(pParent, pH, zSym);
777 }
778 
779 /*
780 ** Close the dynamic library handle pHandle.
781 */
782 static void fsDlClose(sqlite3_vfs *pVfs, void *pHandle){
783   sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
784   pParent->xDlClose(pParent, pHandle);
785 }
786 
787 /*
788 ** Populate the buffer pointed to by zBufOut with nByte bytes of
789 ** random data.
790 */
791 static int fsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
792   sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
793   return pParent->xRandomness(pParent, nByte, zBufOut);
794 }
795 
796 /*
797 ** Sleep for nMicro microseconds. Return the number of microseconds
798 ** actually slept.
799 */
800 static int fsSleep(sqlite3_vfs *pVfs, int nMicro){
801   sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
802   return pParent->xSleep(pParent, nMicro);
803 }
804 
805 /*
806 ** Return the current time as a Julian Day number in *pTimeOut.
807 */
808 static int fsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
809   sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
810   return pParent->xCurrentTime(pParent, pTimeOut);
811 }
812 
813 /*
814 ** This procedure registers the fs vfs with SQLite. If the argument is
815 ** true, the fs vfs becomes the new default vfs. It is the only publicly
816 ** available function in this file.
817 */
818 int fs_register(void){
819   if( fs_vfs.pParent ) return SQLITE_OK;
820   fs_vfs.pParent = sqlite3_vfs_find(0);
821   fs_vfs.base.mxPathname = fs_vfs.pParent->mxPathname;
822   fs_vfs.base.szOsFile = MAX(sizeof(tmp_file), sizeof(fs_file));
823   return sqlite3_vfs_register(&fs_vfs.base, 0);
824 }
825 
826 #ifdef SQLITE_TEST
827   int SqlitetestOnefile_Init() {return fs_register();}
828 #endif
829