1
0

test_demovfs.c 21 KB

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