gitlab.com/CoiaPrant/sqlite3@v1.19.1/vfs/c/vfs.c (about)

     1  // https://www.sqlite.org/src/doc/trunk/src/test_demovfs.c
     2  
     3  /*
     4  ** 2010 April 7
     5  **
     6  ** The author disclaims copyright to this source code.  In place of
     7  ** a legal notice, here is a blessing:
     8  **
     9  **    May you do good and not evil.
    10  **    May you find forgiveness for yourself and forgive others.
    11  **    May you share freely, never taking more than you give.
    12  **
    13  *************************************************************************
    14  **
    15  ** This file implements an example of a simple VFS implementation that 
    16  ** omits complex features often not required or not possible on embedded
    17  ** platforms.  Code is included to buffer writes to the journal file, 
    18  ** which can be a significant performance improvement on some embedded
    19  ** platforms.
    20  **
    21  ** OVERVIEW
    22  **
    23  **   The code in this file implements a minimal SQLite VFS that can be 
    24  **   used on Linux and other posix-like operating systems. The following 
    25  **   system calls are used:
    26  **
    27  **    File-system: access(), unlink(), getcwd()
    28  **    File IO:     open(), read(), write(), fsync(), close(), fstat()
    29  **    Other:       sleep(), usleep(), time()
    30  **
    31  **   The following VFS features are omitted:
    32  **
    33  **     1. File locking. The user must ensure that there is at most one
    34  **        connection to each database when using this VFS. Multiple
    35  **        connections to a single shared-cache count as a single connection
    36  **        for the purposes of the previous statement.
    37  **
    38  **     2. The loading of dynamic extensions (shared libraries).
    39  **
    40  **     3. Temporary files. The user must configure SQLite to use in-memory
    41  **        temp files when using this VFS. The easiest way to do this is to
    42  **        compile with:
    43  **
    44  **          -DSQLITE_TEMP_STORE=3
    45  **
    46  **     4. File truncation. As of version 3.6.24, SQLite may run without
    47  **        a working xTruncate() call, providing the user does not configure
    48  **        SQLite to use "journal_mode=truncate", or use both
    49  **        "journal_mode=persist" and ATTACHed databases.
    50  **
    51  **   It is assumed that the system uses UNIX-like path-names. Specifically,
    52  **   that '/' characters are used to separate path components and that
    53  **   a path-name is a relative path unless it begins with a '/'. And that
    54  **   no UTF-8 encoded paths are greater than 512 bytes in length.
    55  **
    56  ** JOURNAL WRITE-BUFFERING
    57  **
    58  **   To commit a transaction to the database, SQLite first writes rollback
    59  **   information into the journal file. This usually consists of 4 steps:
    60  **
    61  **     1. The rollback information is sequentially written into the journal
    62  **        file, starting at the start of the file.
    63  **     2. The journal file is synced to disk.
    64  **     3. A modification is made to the first few bytes of the journal file.
    65  **     4. The journal file is synced to disk again.
    66  **
    67  **   Most of the data is written in step 1 using a series of calls to the
    68  **   VFS xWrite() method. The buffers passed to the xWrite() calls are of
    69  **   various sizes. For example, as of version 3.6.24, when committing a 
    70  **   transaction that modifies 3 pages of a database file that uses 4096 
    71  **   byte pages residing on a media with 512 byte sectors, SQLite makes 
    72  **   eleven calls to the xWrite() method to create the rollback journal, 
    73  **   as follows:
    74  **
    75  **             Write offset | Bytes written
    76  **             ----------------------------
    77  **                        0            512
    78  **                      512              4
    79  **                      516           4096
    80  **                     4612              4
    81  **                     4616              4
    82  **                     4620           4096
    83  **                     8716              4
    84  **                     8720              4
    85  **                     8724           4096
    86  **                    12820              4
    87  **             ++++++++++++SYNC+++++++++++
    88  **                        0             12
    89  **             ++++++++++++SYNC+++++++++++
    90  **
    91  **   On many operating systems, this is an efficient way to write to a file.
    92  **   However, on some embedded systems that do not cache writes in OS 
    93  **   buffers it is much more efficient to write data in blocks that are
    94  **   an integer multiple of the sector-size in size and aligned at the
    95  **   start of a sector.
    96  **
    97  **   To work around this, the code in this file allocates a fixed size
    98  **   buffer of SQLITE_VFS_BUFFERSZ using sqlite3_malloc() whenever a 
    99  **   journal file is opened. It uses the buffer to coalesce sequential
   100  **   writes into aligned SQLITE_VFS_BUFFERSZ blocks. When SQLite
   101  **   invokes the xSync() method to sync the contents of the file to disk,
   102  **   all accumulated data is written out, even if it does not constitute
   103  **   a complete block. This means the actual IO to create the rollback 
   104  **   journal for the example transaction above is this:
   105  **
   106  **             Write offset | Bytes written
   107  **             ----------------------------
   108  **                        0           8192
   109  **                     8192           4632
   110  **             ++++++++++++SYNC+++++++++++
   111  **                        0             12
   112  **             ++++++++++++SYNC+++++++++++
   113  **
   114  **   Much more efficient if the underlying OS is not caching write 
   115  **   operations.
   116  */
   117  
   118  // This file is modified to support Go's embed.FS
   119  
   120  #if !defined(SQLITE_TEST) || SQLITE_OS_UNIX
   121  
   122  #include "sqlite3.h"
   123  
   124  #include <assert.h>
   125  #include <string.h>
   126  #include <sys/types.h>
   127  #include <sys/stat.h>
   128  #include <sys/file.h>
   129  #include <sys/param.h>
   130  #include <unistd.h>
   131  #include <time.h>
   132  #include <errno.h>
   133  #include <fcntl.h>
   134  
   135  #define hook __builtin_printf("TODO %s:%i:\n", __func__, __LINE__); abort();
   136  // #define hook __builtin_printf("TRC %s:%i:\n", __func__, __LINE__);
   137  
   138  /*
   139  ** Size of the write buffer used by journal files in bytes.
   140  */
   141  #ifndef SQLITE_VFS_BUFFERSZ
   142  # define SQLITE_VFS_BUFFERSZ 8192
   143  #endif
   144  
   145  /*
   146  ** The maximum pathname length supported by this VFS.
   147  */
   148  #define MAXPATHNAME 4096
   149  
   150  /*
   151  ** When using this VFS, the sqlite3_file* handles that SQLite uses are
   152  ** actually pointers to instances of type VFSFile.
   153  */
   154  typedef struct VFSFile VFSFile;
   155  struct VFSFile {
   156    sqlite3_file base;              /* Base class. Must be first. */
   157    void *fsFile; // handle of Go fs.File
   158    int fd;                         /* File descriptor */
   159  
   160    char *aBuffer;                  /* Pointer to malloc'd buffer */
   161    int nBuffer;                    /* Valid bytes of data in zBuffer */
   162    sqlite3_int64 iBufferOfst;      /* Offset in file of zBuffer[0] */
   163  };
   164  
   165  /*
   166  ** Write directly to the file passed as the first argument. Even if the
   167  ** file has a write-buffer (VFSFile.aBuffer), ignore it.
   168  */
   169  static int vfsDirectWrite(
   170    VFSFile *p,                    /* File handle */
   171    const void *zBuf,               /* Buffer containing data to write */
   172    int iAmt,                       /* Size of data to write in bytes */
   173    sqlite_int64 iOfst              /* File offset to write to */
   174  ){
   175    off_t ofst;                     /* Return value from lseek() */
   176    size_t nWrite;                  /* Return value from write() */
   177  
   178    hook
   179    ofst = lseek(p->fd, iOfst, SEEK_SET);
   180    if( ofst!=iOfst ){
   181      return SQLITE_IOERR_WRITE;
   182    }
   183  
   184    nWrite = write(p->fd, zBuf, iAmt);
   185    if( nWrite!=iAmt ){
   186      return SQLITE_IOERR_WRITE;
   187    }
   188  
   189    return SQLITE_OK;
   190  }
   191  
   192  /*
   193  ** Flush the contents of the VFSFile.aBuffer buffer to disk. This is a
   194  ** no-op if this particular file does not have a buffer (i.e. it is not
   195  ** a journal file) or if the buffer is currently empty.
   196  */
   197  static int vfsFlushBuffer(VFSFile *p){
   198    hook
   199    int rc = SQLITE_OK;
   200    if( p->nBuffer ){
   201      rc = vfsDirectWrite(p, p->aBuffer, p->nBuffer, p->iBufferOfst);
   202      p->nBuffer = 0;
   203    }
   204    return rc;
   205  }
   206  
   207  /*
   208  ** Close a file.
   209  */
   210  static int vfsClose(sqlite3_file *pFile){
   211    hook
   212    int rc;
   213    VFSFile *p = (VFSFile*)pFile;
   214    rc = vfsFlushBuffer(p);
   215    sqlite3_free(p->aBuffer);
   216    close(p->fd);
   217    return rc;
   218  }
   219  
   220  /*
   221  ** Read data from a file.
   222  */
   223  static int vfsRead(
   224    sqlite3_file *pFile, 
   225    void *zBuf, 
   226    int iAmt, 
   227    sqlite_int64 iOfst
   228  ){
   229    hook
   230    VFSFile *p = (VFSFile*)pFile;
   231    off_t ofst;                     /* Return value from lseek() */
   232    int nRead;                      /* Return value from read() */
   233    int rc;                         /* Return code from vfsFlushBuffer() */
   234  
   235    /* Flush any data in the write buffer to disk in case this operation
   236    ** is trying to read data the file-region currently cached in the buffer.
   237    ** It would be possible to detect this case and possibly save an 
   238    ** unnecessary write here, but in practice SQLite will rarely read from
   239    ** a journal file when there is data cached in the write-buffer.
   240    */
   241    rc = vfsFlushBuffer(p);
   242    if( rc!=SQLITE_OK ){
   243      return rc;
   244    }
   245  
   246    ofst = lseek(p->fd, iOfst, SEEK_SET);
   247    if( ofst!=iOfst ){
   248      return SQLITE_IOERR_READ;
   249    }
   250    nRead = read(p->fd, zBuf, iAmt);
   251  
   252    if( nRead==iAmt ){
   253      return SQLITE_OK;
   254    }else if( nRead>=0 ){
   255      if( nRead<iAmt ){
   256        memset(&((char*)zBuf)[nRead], 0, iAmt-nRead);
   257      }
   258      return SQLITE_IOERR_SHORT_READ;
   259    }
   260  
   261    return SQLITE_IOERR_READ;
   262  }
   263  
   264  /*
   265  ** Write data to a crash-file.
   266  */
   267  static int vfsWrite(
   268    sqlite3_file *pFile, 
   269    const void *zBuf, 
   270    int iAmt, 
   271    sqlite_int64 iOfst
   272  ){
   273    hook
   274    VFSFile *p = (VFSFile*)pFile;
   275    
   276    if( p->aBuffer ){
   277      char *z = (char *)zBuf;       /* Pointer to remaining data to write */
   278      int n = iAmt;                 /* Number of bytes at z */
   279      sqlite3_int64 i = iOfst;      /* File offset to write to */
   280  
   281      while( n>0 ){
   282        int nCopy;                  /* Number of bytes to copy into buffer */
   283  
   284        /* If the buffer is full, or if this data is not being written directly
   285        ** following the data already buffered, flush the buffer. Flushing
   286        ** the buffer is a no-op if it is empty.  
   287        */
   288        if( p->nBuffer==SQLITE_VFS_BUFFERSZ || p->iBufferOfst+p->nBuffer!=i ){
   289          int rc = vfsFlushBuffer(p);
   290          if( rc!=SQLITE_OK ){
   291            return rc;
   292          }
   293        }
   294        assert( p->nBuffer==0 || p->iBufferOfst+p->nBuffer==i );
   295        p->iBufferOfst = i - p->nBuffer;
   296  
   297        /* Copy as much data as possible into the buffer. */
   298        nCopy = SQLITE_VFS_BUFFERSZ - p->nBuffer;
   299        if( nCopy>n ){
   300          nCopy = n;
   301        }
   302        memcpy(&p->aBuffer[p->nBuffer], z, nCopy);
   303        p->nBuffer += nCopy;
   304  
   305        n -= nCopy;
   306        i += nCopy;
   307        z += nCopy;
   308      }
   309    }else{
   310      return vfsDirectWrite(p, zBuf, iAmt, iOfst);
   311    }
   312  
   313    return SQLITE_OK;
   314  }
   315  
   316  /*
   317  ** Truncate a file. This is a no-op for this VFS (see header comments at
   318  ** the top of the file).
   319  */
   320  static int vfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
   321  #if 0
   322    if( ftruncate(((VFSFile *)pFile)->fd, size) ) return SQLITE_IOERR_TRUNCATE;
   323  #endif
   324    return SQLITE_OK;
   325  }
   326  
   327  /*
   328  ** Sync the contents of the file to the persistent media.
   329  */
   330  static int vfsSync(sqlite3_file *pFile, int flags){
   331    hook
   332    VFSFile *p = (VFSFile*)pFile;
   333    int rc;
   334  
   335    rc = vfsFlushBuffer(p);
   336    if( rc!=SQLITE_OK ){
   337      return rc;
   338    }
   339  
   340    rc = fsync(p->fd);
   341    return (rc==0 ? SQLITE_OK : SQLITE_IOERR_FSYNC);
   342  }
   343  
   344  /*
   345  ** Write the size of the file in bytes to *pSize.
   346  */
   347  static int vfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
   348    hook
   349    VFSFile *p = (VFSFile*)pFile;
   350    int rc;                         /* Return code from fstat() call */
   351    struct stat sStat;              /* Output of fstat() call */
   352  
   353    /* Flush the contents of the buffer to disk. As with the flush in the
   354    ** vfsRead() method, it would be possible to avoid this and save a write
   355    ** here and there. But in practice this comes up so infrequently it is
   356    ** not worth the trouble.
   357    */
   358    rc = vfsFlushBuffer(p);
   359    if( rc!=SQLITE_OK ){
   360      return rc;
   361    }
   362  
   363    rc = fstat(p->fd, &sStat);
   364    if( rc!=0 ) return SQLITE_IOERR_FSTAT;
   365    *pSize = sStat.st_size;
   366    return SQLITE_OK;
   367  }
   368  
   369  /*
   370  ** Locking functions. The xLock() and xUnlock() methods are both no-ops.
   371  ** The xCheckReservedLock() always indicates that no other process holds
   372  ** a reserved lock on the database file. This ensures that if a hot-journal
   373  ** file is found in the file-system it is rolled back.
   374  */
   375  static int vfsLock(sqlite3_file *pFile, int eLock){
   376    return SQLITE_OK;
   377  }
   378  static int vfsUnlock(sqlite3_file *pFile, int eLock){
   379    return SQLITE_OK;
   380  }
   381  static int vfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
   382    *pResOut = 0;
   383    return SQLITE_OK;
   384  }
   385  
   386  /*
   387  ** No xFileControl() verbs are implemented by this VFS.
   388  */
   389  static int vfsFileControl(sqlite3_file *pFile, int op, void *pArg){
   390    return SQLITE_NOTFOUND;
   391  }
   392  
   393  /*
   394  ** The xSectorSize() and xDeviceCharacteristics() methods. These two
   395  ** may return special values allowing SQLite to optimize file-system 
   396  ** access to some extent. But it is also safe to simply return 0.
   397  */
   398  static int vfsSectorSize(sqlite3_file *pFile){
   399    return 0;
   400  }
   401  static int vfsDeviceCharacteristics(sqlite3_file *pFile){
   402    return 0;
   403  }
   404  
   405  /*
   406  ** Open a file handle.
   407  */
   408  static int vfsOpen(
   409    sqlite3_vfs *pVfs,              /* VFS */
   410    const char *zName,              /* File to open, or 0 for a temp file */
   411    sqlite3_file *pFile,            /* Pointer to VFSFile struct to populate */
   412    int flags,                      /* Input SQLITE_OPEN_XXX flags */
   413    int *pOutFlags                  /* Output SQLITE_OPEN_XXX flags (or NULL) */
   414  ){
   415    static const sqlite3_io_methods vfsio = {
   416      1,                            /* iVersion */
   417      vfsClose,                    /* xClose */
   418      vfsRead,                     /* xRead */
   419      vfsWrite,                    /* xWrite */
   420      vfsTruncate,                 /* xTruncate */
   421      vfsSync,                     /* xSync */
   422      vfsFileSize,                 /* xFileSize */
   423      vfsLock,                     /* xLock */
   424      vfsUnlock,                   /* xUnlock */
   425      vfsCheckReservedLock,        /* xCheckReservedLock */
   426      vfsFileControl,              /* xFileControl */
   427      vfsSectorSize,               /* xSectorSize */
   428      vfsDeviceCharacteristics     /* xDeviceCharacteristics */
   429    };
   430  
   431    hook
   432    VFSFile *p = (VFSFile*)pFile; /* Populate this structure */
   433    int oflags = 0;                 /* flags to pass to open() call */
   434    char *aBuf = 0;
   435  
   436    if( zName==0 ){
   437      return SQLITE_IOERR;
   438    }
   439  
   440    if( flags&SQLITE_OPEN_MAIN_JOURNAL ){
   441      aBuf = (char *)sqlite3_malloc(SQLITE_VFS_BUFFERSZ);
   442      if( !aBuf ){
   443        return SQLITE_NOMEM;
   444      }
   445    }
   446  
   447    if( flags&SQLITE_OPEN_EXCLUSIVE ) oflags |= O_EXCL;
   448    if( flags&SQLITE_OPEN_CREATE )    oflags |= O_CREAT;
   449    if( flags&SQLITE_OPEN_READONLY )  oflags |= O_RDONLY;
   450    if( flags&SQLITE_OPEN_READWRITE ) oflags |= O_RDWR;
   451  
   452    memset(p, 0, sizeof(VFSFile));
   453    p->fd = open(zName, oflags, 0600);
   454    if( p->fd<0 ){
   455      sqlite3_free(aBuf);
   456      return SQLITE_CANTOPEN;
   457    }
   458    p->aBuffer = aBuf;
   459  
   460    if( pOutFlags ){
   461      *pOutFlags = flags;
   462    }
   463    p->base.pMethods = &vfsio;
   464    return SQLITE_OK;
   465  }
   466  
   467  /*
   468  ** Delete the file identified by argument zPath. If the dirSync parameter
   469  ** is non-zero, then ensure the file-system modification to delete the
   470  ** file has been synced to disk before returning.
   471  */
   472  static int vfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
   473    hook
   474    int rc;                         /* Return code */
   475  
   476    rc = unlink(zPath);
   477    if( rc!=0 && errno==ENOENT ) return SQLITE_OK;
   478  
   479    if( rc==0 && dirSync ){
   480      int dfd;                      /* File descriptor open on directory */
   481      int i;                        /* Iterator variable */
   482      char zDir[MAXPATHNAME+1];     /* Name of directory containing file zPath */
   483  
   484      /* Figure out the directory name from the path of the file deleted. */
   485      sqlite3_snprintf(MAXPATHNAME, zDir, "%s", zPath);
   486      zDir[MAXPATHNAME] = '\0';
   487      for(i=strlen(zDir); i>1 && zDir[i]!='/'; i++);
   488      zDir[i] = '\0';
   489  
   490      /* Open a file-descriptor on the directory. Sync. Close. */
   491      dfd = open(zDir, O_RDONLY, 0);
   492      if( dfd<0 ){
   493        rc = -1;
   494      }else{
   495        rc = fsync(dfd);
   496        close(dfd);
   497      }
   498    }
   499    return (rc==0 ? SQLITE_OK : SQLITE_IOERR_DELETE);
   500  }
   501  
   502  #ifndef F_OK
   503  # define F_OK 0
   504  #endif
   505  #ifndef R_OK
   506  # define R_OK 4
   507  #endif
   508  #ifndef W_OK
   509  # define W_OK 2
   510  #endif
   511  
   512  /*
   513  ** Query the file-system to see if the named file exists, is readable or
   514  ** is both readable and writable.
   515  */
   516  static int vfsAccess(
   517    sqlite3_vfs *pVfs, 
   518    const char *zPath, 
   519    int flags, 
   520    int *pResOut
   521  ){
   522    hook
   523    int rc;                         /* access() return code */
   524    int eAccess = F_OK;             /* Second argument to access() */
   525  
   526    assert( flags==SQLITE_ACCESS_EXISTS       /* access(zPath, F_OK) */
   527         || flags==SQLITE_ACCESS_READ         /* access(zPath, R_OK) */
   528         || flags==SQLITE_ACCESS_READWRITE    /* access(zPath, R_OK|W_OK) */
   529    );
   530  
   531    if( flags==SQLITE_ACCESS_READWRITE ) eAccess = R_OK|W_OK;
   532    if( flags==SQLITE_ACCESS_READ )      eAccess = R_OK;
   533  
   534    rc = access(zPath, eAccess);
   535    *pResOut = (rc==0);
   536    return SQLITE_OK;
   537  }
   538  
   539  /*
   540  ** Argument zPath points to a nul-terminated string containing a file path.
   541  ** If zPath is an absolute path, then it is copied as is into the output 
   542  ** buffer. Otherwise, if it is a relative path, then the equivalent full
   543  ** path is written to the output buffer.
   544  **
   545  ** This function assumes that paths are UNIX style. Specifically, that:
   546  **
   547  **   1. Path components are separated by a '/'. and 
   548  **   2. Full paths begin with a '/' character.
   549  */
   550  static int vfsFullPathname(
   551    sqlite3_vfs *pVfs,              /* VFS */
   552    const char *zPath,              /* Input path (possibly a relative path) */
   553    int nPathOut,                   /* Size of output buffer in bytes */
   554    char *zPathOut                  /* Pointer to output buffer */
   555  ){
   556    hook
   557    char zDir[MAXPATHNAME+1];
   558    if( zPath[0]=='/' ){
   559      zDir[0] = '\0';
   560    }else{
   561      if( getcwd(zDir, sizeof(zDir))==0 ) return SQLITE_IOERR;
   562    }
   563    zDir[MAXPATHNAME] = '\0';
   564  
   565    sqlite3_snprintf(nPathOut, zPathOut, "%s/%s", zDir, zPath);
   566    zPathOut[nPathOut-1] = '\0';
   567  
   568    return SQLITE_OK;
   569  }
   570  
   571  /*
   572  ** The following four VFS methods:
   573  **
   574  **   xDlOpen
   575  **   xDlError
   576  **   xDlSym
   577  **   xDlClose
   578  **
   579  ** are supposed to implement the functionality needed by SQLite to load
   580  ** extensions compiled as shared objects. This simple VFS does not support
   581  ** this functionality, so the following functions are no-ops.
   582  */
   583  static void *vfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
   584    return 0;
   585  }
   586  static void vfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
   587    sqlite3_snprintf(nByte, zErrMsg, "Loadable extensions are not supported");
   588    zErrMsg[nByte-1] = '\0';
   589  }
   590  static void (*vfsDlSym(sqlite3_vfs *pVfs, void *pH, const char *z))(void){
   591    return 0;
   592  }
   593  static void vfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
   594    return;
   595  }
   596  
   597  /*
   598  ** Parameter zByte points to a buffer nByte bytes in size. Populate this
   599  ** buffer with pseudo-random data.
   600  */
   601  static int vfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zByte){
   602    return SQLITE_OK;
   603  }
   604  
   605  /*
   606  ** Sleep for at least nMicro microseconds. Return the (approximate) number 
   607  ** of microseconds slept for.
   608  */
   609  static int vfsSleep(sqlite3_vfs *pVfs, int nMicro){
   610    sleep(nMicro / 1000000);
   611    usleep(nMicro % 1000000);
   612    return nMicro;
   613  }
   614  
   615  /*
   616  ** Set *pTime to the current UTC time expressed as a Julian day. Return
   617  ** SQLITE_OK if successful, or an error code otherwise.
   618  **
   619  **   http://en.wikipedia.org/wiki/Julian_day
   620  **
   621  ** This implementation is not very good. The current time is rounded to
   622  ** an integer number of seconds. Also, assuming time_t is a signed 32-bit 
   623  ** value, it will stop working some time in the year 2038 AD (the so-called
   624  ** "year 2038" problem that afflicts systems that store time this way). 
   625  */
   626  static int vfsCurrentTime(sqlite3_vfs *pVfs, double *pTime){
   627    time_t t = time(0);
   628    *pTime = t/86400.0 + 2440587.5; 
   629    return SQLITE_OK;
   630  }
   631  
   632  /*
   633  ** This function returns a pointer to the VFS implemented in this file.
   634  ** To make the VFS available to SQLite:
   635  **
   636  **   sqlite3_vfs_register(sqlite3_fsFS(), 0);
   637  */
   638  sqlite3_vfs *sqlite3_fsFS(char *zName, void *pAppData){
   639    sqlite3_vfs *p = sqlite3_malloc(sizeof(sqlite3_vfs));
   640    if (!p) {
   641      return NULL;
   642    }
   643  
   644    *p = (sqlite3_vfs){
   645      1,                           /* iVersion */
   646      sizeof(VFSFile),             /* szOsFile */
   647      MAXPATHNAME,                 /* mxPathname */
   648      0,                           /* pNext */
   649      zName,                       /* zName */
   650      pAppData,                    /* pAppData */
   651      vfsOpen,                     /* xOpen */
   652      vfsDelete,                   /* xDelete */
   653      vfsAccess,                   /* xAccess */
   654      vfsFullPathname,             /* xFullPathname */
   655      vfsDlOpen,                   /* xDlOpen */
   656      vfsDlError,                  /* xDlError */
   657      vfsDlSym,                    /* xDlSym */
   658      vfsDlClose,                  /* xDlClose */
   659      vfsRandomness,               /* xRandomness */
   660      vfsSleep,                    /* xSleep */
   661      vfsCurrentTime,              /* xCurrentTime */
   662    };
   663    return p;
   664  }
   665  
   666  #endif /* !defined(SQLITE_TEST) || SQLITE_OS_UNIX */
   667  
   668  
   669  #ifdef SQLITE_TEST
   670  
   671  #if defined(INCLUDE_SQLITE_TCL_H)
   672  #  include "sqlite_tcl.h"
   673  #else
   674  #  include "tcl.h"
   675  #  ifndef SQLITE_TCLAPI
   676  #    define SQLITE_TCLAPI
   677  #  endif
   678  #endif
   679  
   680  #if SQLITE_OS_UNIX
   681  static int SQLITE_TCLAPI register_fsFS(
   682    ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
   683    Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   684    int objc,              /* Number of arguments */
   685    Tcl_Obj *CONST objv[]  /* Command arguments */
   686  ){
   687    sqlite3_vfs_register(sqlite3_fsFS("fsFS", 0), 1);
   688    return TCL_OK;
   689  }
   690  static int SQLITE_TCLAPI unregister_fsFS(
   691    ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
   692    Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   693    int objc,              /* Number of arguments */
   694    Tcl_Obj *CONST objv[]  /* Command arguments */
   695  ){
   696    sqlite3_vfs_unregister(sqlite3_fsFS("fsFS", 0));
   697    return TCL_OK;
   698  }
   699  
   700  /*
   701  ** Register commands with the TCL interpreter.
   702  */
   703  int Sqlitetest_fsFS_Init(Tcl_Interp *interp){
   704    Tcl_CreateObjCommand(interp, "register_fsFS", register_fsFS, 0, 0);
   705    Tcl_CreateObjCommand(interp, "unregister_fsFS", unregister_fsFS, 0, 0);
   706    return TCL_OK;
   707  }
   708  
   709  #else
   710  int Sqlitetest_fsFS_Init(Tcl_Interp *interp){ return TCL_OK; }
   711  #endif
   712  
   713  #endif /* SQLITE_TEST */