Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / sqlite / sqlite-src-3080704 / src / test_vfs.c
blob7ee2a93453f56913b7911e706c325f56e17026fc
1 /*
2 ** 2010 May 05
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.
11 ******************************************************************************
13 ** This file contains the implementation of the Tcl [testvfs] command,
14 ** used to create SQLite VFS implementations with various properties and
15 ** instrumentation to support testing SQLite.
17 ** testvfs VFSNAME ?OPTIONS?
19 ** Available options are:
21 ** -noshm BOOLEAN (True to omit shm methods. Default false)
22 ** -default BOOLEAN (True to make the vfs default. Default false)
23 ** -szosfile INTEGER (Value for sqlite3_vfs.szOsFile)
24 ** -mxpathname INTEGER (Value for sqlite3_vfs.mxPathname)
25 ** -iversion INTEGER (Value for sqlite3_vfs.iVersion)
27 #if SQLITE_TEST /* This file is used for testing only */
29 #include "sqlite3.h"
30 #include "sqliteInt.h"
31 #include <tcl.h>
33 typedef struct Testvfs Testvfs;
34 typedef struct TestvfsShm TestvfsShm;
35 typedef struct TestvfsBuffer TestvfsBuffer;
36 typedef struct TestvfsFile TestvfsFile;
37 typedef struct TestvfsFd TestvfsFd;
40 ** An open file handle.
42 struct TestvfsFile {
43 sqlite3_file base; /* Base class. Must be first */
44 TestvfsFd *pFd; /* File data */
46 #define tvfsGetFd(pFile) (((TestvfsFile *)pFile)->pFd)
48 struct TestvfsFd {
49 sqlite3_vfs *pVfs; /* The VFS */
50 const char *zFilename; /* Filename as passed to xOpen() */
51 sqlite3_file *pReal; /* The real, underlying file descriptor */
52 Tcl_Obj *pShmId; /* Shared memory id for Tcl callbacks */
54 TestvfsBuffer *pShm; /* Shared memory buffer */
55 u32 excllock; /* Mask of exclusive locks */
56 u32 sharedlock; /* Mask of shared locks */
57 TestvfsFd *pNext; /* Next handle opened on the same file */
61 #define FAULT_INJECT_NONE 0
62 #define FAULT_INJECT_TRANSIENT 1
63 #define FAULT_INJECT_PERSISTENT 2
65 typedef struct TestFaultInject TestFaultInject;
66 struct TestFaultInject {
67 int iCnt; /* Remaining calls before fault injection */
68 int eFault; /* A FAULT_INJECT_* value */
69 int nFail; /* Number of faults injected */
73 ** An instance of this structure is allocated for each VFS created. The
74 ** sqlite3_vfs.pAppData field of the VFS structure registered with SQLite
75 ** is set to point to it.
77 struct Testvfs {
78 char *zName; /* Name of this VFS */
79 sqlite3_vfs *pParent; /* The VFS to use for file IO */
80 sqlite3_vfs *pVfs; /* The testvfs registered with SQLite */
81 Tcl_Interp *interp; /* Interpreter to run script in */
82 Tcl_Obj *pScript; /* Script to execute */
83 TestvfsBuffer *pBuffer; /* List of shared buffers */
84 int isNoshm;
85 int isFullshm;
87 int mask; /* Mask controlling [script] and [ioerr] */
89 TestFaultInject ioerr_err;
90 TestFaultInject full_err;
91 TestFaultInject cantopen_err;
93 #if 0
94 int iIoerrCnt;
95 int ioerr;
96 int nIoerrFail;
97 int iFullCnt;
98 int fullerr;
99 int nFullFail;
100 #endif
102 int iDevchar;
103 int iSectorsize;
107 ** The Testvfs.mask variable is set to a combination of the following.
108 ** If a bit is clear in Testvfs.mask, then calls made by SQLite to the
109 ** corresponding VFS method is ignored for purposes of:
111 ** + Simulating IO errors, and
112 ** + Invoking the Tcl callback script.
114 #define TESTVFS_SHMOPEN_MASK 0x00000001
115 #define TESTVFS_SHMLOCK_MASK 0x00000010
116 #define TESTVFS_SHMMAP_MASK 0x00000020
117 #define TESTVFS_SHMBARRIER_MASK 0x00000040
118 #define TESTVFS_SHMCLOSE_MASK 0x00000080
120 #define TESTVFS_OPEN_MASK 0x00000100
121 #define TESTVFS_SYNC_MASK 0x00000200
122 #define TESTVFS_DELETE_MASK 0x00000400
123 #define TESTVFS_CLOSE_MASK 0x00000800
124 #define TESTVFS_WRITE_MASK 0x00001000
125 #define TESTVFS_TRUNCATE_MASK 0x00002000
126 #define TESTVFS_ACCESS_MASK 0x00004000
127 #define TESTVFS_FULLPATHNAME_MASK 0x00008000
128 #define TESTVFS_READ_MASK 0x00010000
129 #define TESTVFS_UNLOCK_MASK 0x00020000
130 #define TESTVFS_LOCK_MASK 0x00040000
131 #define TESTVFS_CKLOCK_MASK 0x00080000
133 #define TESTVFS_ALL_MASK 0x000FFFFF
136 #define TESTVFS_MAX_PAGES 1024
139 ** A shared-memory buffer. There is one of these objects for each shared
140 ** memory region opened by clients. If two clients open the same file,
141 ** there are two TestvfsFile structures but only one TestvfsBuffer structure.
143 struct TestvfsBuffer {
144 char *zFile; /* Associated file name */
145 int pgsz; /* Page size */
146 u8 *aPage[TESTVFS_MAX_PAGES]; /* Array of ckalloc'd pages */
147 TestvfsFd *pFile; /* List of open handles */
148 TestvfsBuffer *pNext; /* Next in linked list of all buffers */
152 #define PARENTVFS(x) (((Testvfs *)((x)->pAppData))->pParent)
154 #define TESTVFS_MAX_ARGS 12
158 ** Method declarations for TestvfsFile.
160 static int tvfsClose(sqlite3_file*);
161 static int tvfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
162 static int tvfsWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
163 static int tvfsTruncate(sqlite3_file*, sqlite3_int64 size);
164 static int tvfsSync(sqlite3_file*, int flags);
165 static int tvfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
166 static int tvfsLock(sqlite3_file*, int);
167 static int tvfsUnlock(sqlite3_file*, int);
168 static int tvfsCheckReservedLock(sqlite3_file*, int *);
169 static int tvfsFileControl(sqlite3_file*, int op, void *pArg);
170 static int tvfsSectorSize(sqlite3_file*);
171 static int tvfsDeviceCharacteristics(sqlite3_file*);
174 ** Method declarations for tvfs_vfs.
176 static int tvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
177 static int tvfsDelete(sqlite3_vfs*, const char *zName, int syncDir);
178 static int tvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
179 static int tvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
180 #ifndef SQLITE_OMIT_LOAD_EXTENSION
181 static void *tvfsDlOpen(sqlite3_vfs*, const char *zFilename);
182 static void tvfsDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
183 static void (*tvfsDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
184 static void tvfsDlClose(sqlite3_vfs*, void*);
185 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
186 static int tvfsRandomness(sqlite3_vfs*, int nByte, char *zOut);
187 static int tvfsSleep(sqlite3_vfs*, int microseconds);
188 static int tvfsCurrentTime(sqlite3_vfs*, double*);
190 static int tvfsShmOpen(sqlite3_file*);
191 static int tvfsShmLock(sqlite3_file*, int , int, int);
192 static int tvfsShmMap(sqlite3_file*,int,int,int, void volatile **);
193 static void tvfsShmBarrier(sqlite3_file*);
194 static int tvfsShmUnmap(sqlite3_file*, int);
196 static int tvfsFetch(sqlite3_file*, sqlite3_int64, int, void**);
197 static int tvfsUnfetch(sqlite3_file*, sqlite3_int64, void*);
199 static sqlite3_io_methods tvfs_io_methods = {
200 3, /* iVersion */
201 tvfsClose, /* xClose */
202 tvfsRead, /* xRead */
203 tvfsWrite, /* xWrite */
204 tvfsTruncate, /* xTruncate */
205 tvfsSync, /* xSync */
206 tvfsFileSize, /* xFileSize */
207 tvfsLock, /* xLock */
208 tvfsUnlock, /* xUnlock */
209 tvfsCheckReservedLock, /* xCheckReservedLock */
210 tvfsFileControl, /* xFileControl */
211 tvfsSectorSize, /* xSectorSize */
212 tvfsDeviceCharacteristics, /* xDeviceCharacteristics */
213 tvfsShmMap, /* xShmMap */
214 tvfsShmLock, /* xShmLock */
215 tvfsShmBarrier, /* xShmBarrier */
216 tvfsShmUnmap, /* xShmUnmap */
217 tvfsFetch,
218 tvfsUnfetch
221 static int tvfsResultCode(Testvfs *p, int *pRc){
222 struct errcode {
223 int eCode;
224 const char *zCode;
225 } aCode[] = {
226 { SQLITE_OK, "SQLITE_OK" },
227 { SQLITE_ERROR, "SQLITE_ERROR" },
228 { SQLITE_IOERR, "SQLITE_IOERR" },
229 { SQLITE_LOCKED, "SQLITE_LOCKED" },
230 { SQLITE_BUSY, "SQLITE_BUSY" },
233 const char *z;
234 int i;
236 z = Tcl_GetStringResult(p->interp);
237 for(i=0; i<ArraySize(aCode); i++){
238 if( 0==strcmp(z, aCode[i].zCode) ){
239 *pRc = aCode[i].eCode;
240 return 1;
244 return 0;
247 static int tvfsInjectFault(TestFaultInject *p){
248 int ret = 0;
249 if( p->eFault ){
250 p->iCnt--;
251 if( p->iCnt==0 || (p->iCnt<0 && p->eFault==FAULT_INJECT_PERSISTENT ) ){
252 ret = 1;
253 p->nFail++;
256 return ret;
260 static int tvfsInjectIoerr(Testvfs *p){
261 return tvfsInjectFault(&p->ioerr_err);
264 static int tvfsInjectFullerr(Testvfs *p){
265 return tvfsInjectFault(&p->full_err);
267 static int tvfsInjectCantopenerr(Testvfs *p){
268 return tvfsInjectFault(&p->cantopen_err);
272 static void tvfsExecTcl(
273 Testvfs *p,
274 const char *zMethod,
275 Tcl_Obj *arg1,
276 Tcl_Obj *arg2,
277 Tcl_Obj *arg3,
278 Tcl_Obj *arg4
280 int rc; /* Return code from Tcl_EvalObj() */
281 Tcl_Obj *pEval;
282 assert( p->pScript );
284 assert( zMethod );
285 assert( p );
286 assert( arg2==0 || arg1!=0 );
287 assert( arg3==0 || arg2!=0 );
289 pEval = Tcl_DuplicateObj(p->pScript);
290 Tcl_IncrRefCount(p->pScript);
291 Tcl_ListObjAppendElement(p->interp, pEval, Tcl_NewStringObj(zMethod, -1));
292 if( arg1 ) Tcl_ListObjAppendElement(p->interp, pEval, arg1);
293 if( arg2 ) Tcl_ListObjAppendElement(p->interp, pEval, arg2);
294 if( arg3 ) Tcl_ListObjAppendElement(p->interp, pEval, arg3);
295 if( arg4 ) Tcl_ListObjAppendElement(p->interp, pEval, arg4);
297 rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
298 if( rc!=TCL_OK ){
299 Tcl_BackgroundError(p->interp);
300 Tcl_ResetResult(p->interp);
306 ** Close an tvfs-file.
308 static int tvfsClose(sqlite3_file *pFile){
309 int rc;
310 TestvfsFile *pTestfile = (TestvfsFile *)pFile;
311 TestvfsFd *pFd = pTestfile->pFd;
312 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
314 if( p->pScript && p->mask&TESTVFS_CLOSE_MASK ){
315 tvfsExecTcl(p, "xClose",
316 Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0, 0
320 if( pFd->pShmId ){
321 Tcl_DecrRefCount(pFd->pShmId);
322 pFd->pShmId = 0;
324 if( pFile->pMethods ){
325 ckfree((char *)pFile->pMethods);
327 rc = sqlite3OsClose(pFd->pReal);
328 ckfree((char *)pFd);
329 pTestfile->pFd = 0;
330 return rc;
334 ** Read data from an tvfs-file.
336 static int tvfsRead(
337 sqlite3_file *pFile,
338 void *zBuf,
339 int iAmt,
340 sqlite_int64 iOfst
342 int rc = SQLITE_OK;
343 TestvfsFd *pFd = tvfsGetFd(pFile);
344 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
345 if( p->pScript && p->mask&TESTVFS_READ_MASK ){
346 tvfsExecTcl(p, "xRead",
347 Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0, 0
349 tvfsResultCode(p, &rc);
351 if( rc==SQLITE_OK && p->mask&TESTVFS_READ_MASK && tvfsInjectIoerr(p) ){
352 rc = SQLITE_IOERR;
354 if( rc==SQLITE_OK ){
355 rc = sqlite3OsRead(pFd->pReal, zBuf, iAmt, iOfst);
357 return rc;
361 ** Write data to an tvfs-file.
363 static int tvfsWrite(
364 sqlite3_file *pFile,
365 const void *zBuf,
366 int iAmt,
367 sqlite_int64 iOfst
369 int rc = SQLITE_OK;
370 TestvfsFd *pFd = tvfsGetFd(pFile);
371 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
373 if( p->pScript && p->mask&TESTVFS_WRITE_MASK ){
374 tvfsExecTcl(p, "xWrite",
375 Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId,
376 Tcl_NewWideIntObj(iOfst), Tcl_NewIntObj(iAmt)
378 tvfsResultCode(p, &rc);
381 if( rc==SQLITE_OK && tvfsInjectFullerr(p) ){
382 rc = SQLITE_FULL;
384 if( rc==SQLITE_OK && p->mask&TESTVFS_WRITE_MASK && tvfsInjectIoerr(p) ){
385 rc = SQLITE_IOERR;
388 if( rc==SQLITE_OK ){
389 rc = sqlite3OsWrite(pFd->pReal, zBuf, iAmt, iOfst);
391 return rc;
395 ** Truncate an tvfs-file.
397 static int tvfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
398 int rc = SQLITE_OK;
399 TestvfsFd *pFd = tvfsGetFd(pFile);
400 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
402 if( p->pScript && p->mask&TESTVFS_TRUNCATE_MASK ){
403 tvfsExecTcl(p, "xTruncate",
404 Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0, 0
406 tvfsResultCode(p, &rc);
409 if( rc==SQLITE_OK ){
410 rc = sqlite3OsTruncate(pFd->pReal, size);
412 return rc;
416 ** Sync an tvfs-file.
418 static int tvfsSync(sqlite3_file *pFile, int flags){
419 int rc = SQLITE_OK;
420 TestvfsFd *pFd = tvfsGetFd(pFile);
421 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
423 if( p->pScript && p->mask&TESTVFS_SYNC_MASK ){
424 char *zFlags;
426 switch( flags ){
427 case SQLITE_SYNC_NORMAL:
428 zFlags = "normal";
429 break;
430 case SQLITE_SYNC_FULL:
431 zFlags = "full";
432 break;
433 case SQLITE_SYNC_NORMAL|SQLITE_SYNC_DATAONLY:
434 zFlags = "normal|dataonly";
435 break;
436 case SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY:
437 zFlags = "full|dataonly";
438 break;
439 default:
440 assert(0);
443 tvfsExecTcl(p, "xSync",
444 Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId,
445 Tcl_NewStringObj(zFlags, -1), 0
447 tvfsResultCode(p, &rc);
450 if( rc==SQLITE_OK && tvfsInjectFullerr(p) ) rc = SQLITE_FULL;
452 if( rc==SQLITE_OK ){
453 rc = sqlite3OsSync(pFd->pReal, flags);
456 return rc;
460 ** Return the current file-size of an tvfs-file.
462 static int tvfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
463 TestvfsFd *p = tvfsGetFd(pFile);
464 return sqlite3OsFileSize(p->pReal, pSize);
468 ** Lock an tvfs-file.
470 static int tvfsLock(sqlite3_file *pFile, int eLock){
471 TestvfsFd *pFd = tvfsGetFd(pFile);
472 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
473 if( p->pScript && p->mask&TESTVFS_LOCK_MASK ){
474 char zLock[30];
475 sqlite3_snprintf(sizeof(zLock),zLock,"%d",eLock);
476 tvfsExecTcl(p, "xLock", Tcl_NewStringObj(pFd->zFilename, -1),
477 Tcl_NewStringObj(zLock, -1), 0, 0);
479 return sqlite3OsLock(pFd->pReal, eLock);
483 ** Unlock an tvfs-file.
485 static int tvfsUnlock(sqlite3_file *pFile, int eLock){
486 TestvfsFd *pFd = tvfsGetFd(pFile);
487 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
488 if( p->pScript && p->mask&TESTVFS_UNLOCK_MASK ){
489 char zLock[30];
490 sqlite3_snprintf(sizeof(zLock),zLock,"%d",eLock);
491 tvfsExecTcl(p, "xUnlock", Tcl_NewStringObj(pFd->zFilename, -1),
492 Tcl_NewStringObj(zLock, -1), 0, 0);
494 if( p->mask&TESTVFS_WRITE_MASK && tvfsInjectIoerr(p) ){
495 return SQLITE_IOERR_UNLOCK;
497 return sqlite3OsUnlock(pFd->pReal, eLock);
501 ** Check if another file-handle holds a RESERVED lock on an tvfs-file.
503 static int tvfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
504 TestvfsFd *pFd = tvfsGetFd(pFile);
505 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
506 if( p->pScript && p->mask&TESTVFS_CKLOCK_MASK ){
507 tvfsExecTcl(p, "xCheckReservedLock", Tcl_NewStringObj(pFd->zFilename, -1),
508 0, 0, 0);
510 return sqlite3OsCheckReservedLock(pFd->pReal, pResOut);
514 ** File control method. For custom operations on an tvfs-file.
516 static int tvfsFileControl(sqlite3_file *pFile, int op, void *pArg){
517 TestvfsFd *p = tvfsGetFd(pFile);
518 if( op==SQLITE_FCNTL_PRAGMA ){
519 char **argv = (char**)pArg;
520 if( sqlite3_stricmp(argv[1],"error")==0 ){
521 int rc = SQLITE_ERROR;
522 if( argv[2] ){
523 const char *z = argv[2];
524 int x = atoi(z);
525 if( x ){
526 rc = x;
527 while( sqlite3Isdigit(z[0]) ){ z++; }
528 while( sqlite3Isspace(z[0]) ){ z++; }
530 if( z[0] ) argv[0] = sqlite3_mprintf("%s", z);
532 return rc;
534 if( sqlite3_stricmp(argv[1], "filename")==0 ){
535 argv[0] = sqlite3_mprintf("%s", p->zFilename);
536 return SQLITE_OK;
539 return sqlite3OsFileControl(p->pReal, op, pArg);
543 ** Return the sector-size in bytes for an tvfs-file.
545 static int tvfsSectorSize(sqlite3_file *pFile){
546 TestvfsFd *pFd = tvfsGetFd(pFile);
547 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
548 if( p->iSectorsize>=0 ){
549 return p->iSectorsize;
551 return sqlite3OsSectorSize(pFd->pReal);
555 ** Return the device characteristic flags supported by an tvfs-file.
557 static int tvfsDeviceCharacteristics(sqlite3_file *pFile){
558 TestvfsFd *pFd = tvfsGetFd(pFile);
559 Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
560 if( p->iDevchar>=0 ){
561 return p->iDevchar;
563 return sqlite3OsDeviceCharacteristics(pFd->pReal);
567 ** Open an tvfs file handle.
569 static int tvfsOpen(
570 sqlite3_vfs *pVfs,
571 const char *zName,
572 sqlite3_file *pFile,
573 int flags,
574 int *pOutFlags
576 int rc;
577 TestvfsFile *pTestfile = (TestvfsFile *)pFile;
578 TestvfsFd *pFd;
579 Tcl_Obj *pId = 0;
580 Testvfs *p = (Testvfs *)pVfs->pAppData;
582 pFd = (TestvfsFd *)ckalloc(sizeof(TestvfsFd) + PARENTVFS(pVfs)->szOsFile);
583 memset(pFd, 0, sizeof(TestvfsFd) + PARENTVFS(pVfs)->szOsFile);
584 pFd->pShm = 0;
585 pFd->pShmId = 0;
586 pFd->zFilename = zName;
587 pFd->pVfs = pVfs;
588 pFd->pReal = (sqlite3_file *)&pFd[1];
589 memset(pTestfile, 0, sizeof(TestvfsFile));
590 pTestfile->pFd = pFd;
592 /* Evaluate the Tcl script:
594 ** SCRIPT xOpen FILENAME KEY-VALUE-ARGS
596 ** If the script returns an SQLite error code other than SQLITE_OK, an
597 ** error is returned to the caller. If it returns SQLITE_OK, the new
598 ** connection is named "anon". Otherwise, the value returned by the
599 ** script is used as the connection name.
601 Tcl_ResetResult(p->interp);
602 if( p->pScript && p->mask&TESTVFS_OPEN_MASK ){
603 Tcl_Obj *pArg = Tcl_NewObj();
604 Tcl_IncrRefCount(pArg);
605 if( flags&SQLITE_OPEN_MAIN_DB ){
606 const char *z = &zName[strlen(zName)+1];
607 while( *z ){
608 Tcl_ListObjAppendElement(0, pArg, Tcl_NewStringObj(z, -1));
609 z += strlen(z) + 1;
610 Tcl_ListObjAppendElement(0, pArg, Tcl_NewStringObj(z, -1));
611 z += strlen(z) + 1;
614 tvfsExecTcl(p, "xOpen", Tcl_NewStringObj(pFd->zFilename, -1), pArg, 0, 0);
615 Tcl_DecrRefCount(pArg);
616 if( tvfsResultCode(p, &rc) ){
617 if( rc!=SQLITE_OK ) return rc;
618 }else{
619 pId = Tcl_GetObjResult(p->interp);
623 if( (p->mask&TESTVFS_OPEN_MASK) && tvfsInjectIoerr(p) ) return SQLITE_IOERR;
624 if( tvfsInjectCantopenerr(p) ) return SQLITE_CANTOPEN;
625 if( tvfsInjectFullerr(p) ) return SQLITE_FULL;
627 if( !pId ){
628 pId = Tcl_NewStringObj("anon", -1);
630 Tcl_IncrRefCount(pId);
631 pFd->pShmId = pId;
632 Tcl_ResetResult(p->interp);
634 rc = sqlite3OsOpen(PARENTVFS(pVfs), zName, pFd->pReal, flags, pOutFlags);
635 if( pFd->pReal->pMethods ){
636 sqlite3_io_methods *pMethods;
637 int nByte;
639 if( pVfs->iVersion>1 ){
640 nByte = sizeof(sqlite3_io_methods);
641 }else{
642 nByte = offsetof(sqlite3_io_methods, xShmMap);
645 pMethods = (sqlite3_io_methods *)ckalloc(nByte);
646 memcpy(pMethods, &tvfs_io_methods, nByte);
647 pMethods->iVersion = pFd->pReal->pMethods->iVersion;
648 if( pMethods->iVersion>pVfs->iVersion ){
649 pMethods->iVersion = pVfs->iVersion;
651 if( pVfs->iVersion>1 && ((Testvfs *)pVfs->pAppData)->isNoshm ){
652 pMethods->xShmUnmap = 0;
653 pMethods->xShmLock = 0;
654 pMethods->xShmBarrier = 0;
655 pMethods->xShmMap = 0;
657 pFile->pMethods = pMethods;
660 return rc;
664 ** Delete the file located at zPath. If the dirSync argument is true,
665 ** ensure the file-system modifications are synced to disk before
666 ** returning.
668 static int tvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
669 int rc = SQLITE_OK;
670 Testvfs *p = (Testvfs *)pVfs->pAppData;
672 if( p->pScript && p->mask&TESTVFS_DELETE_MASK ){
673 tvfsExecTcl(p, "xDelete",
674 Tcl_NewStringObj(zPath, -1), Tcl_NewIntObj(dirSync), 0, 0
676 tvfsResultCode(p, &rc);
678 if( rc==SQLITE_OK ){
679 rc = sqlite3OsDelete(PARENTVFS(pVfs), zPath, dirSync);
681 return rc;
685 ** Test for access permissions. Return true if the requested permission
686 ** is available, or false otherwise.
688 static int tvfsAccess(
689 sqlite3_vfs *pVfs,
690 const char *zPath,
691 int flags,
692 int *pResOut
694 Testvfs *p = (Testvfs *)pVfs->pAppData;
695 if( p->pScript && p->mask&TESTVFS_ACCESS_MASK ){
696 int rc;
697 char *zArg = 0;
698 if( flags==SQLITE_ACCESS_EXISTS ) zArg = "SQLITE_ACCESS_EXISTS";
699 if( flags==SQLITE_ACCESS_READWRITE ) zArg = "SQLITE_ACCESS_READWRITE";
700 if( flags==SQLITE_ACCESS_READ ) zArg = "SQLITE_ACCESS_READ";
701 tvfsExecTcl(p, "xAccess",
702 Tcl_NewStringObj(zPath, -1), Tcl_NewStringObj(zArg, -1), 0, 0
704 if( tvfsResultCode(p, &rc) ){
705 if( rc!=SQLITE_OK ) return rc;
706 }else{
707 Tcl_Interp *interp = p->interp;
708 if( TCL_OK==Tcl_GetBooleanFromObj(0, Tcl_GetObjResult(interp), pResOut) ){
709 return SQLITE_OK;
713 return sqlite3OsAccess(PARENTVFS(pVfs), zPath, flags, pResOut);
717 ** Populate buffer zOut with the full canonical pathname corresponding
718 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
719 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
721 static int tvfsFullPathname(
722 sqlite3_vfs *pVfs,
723 const char *zPath,
724 int nOut,
725 char *zOut
727 Testvfs *p = (Testvfs *)pVfs->pAppData;
728 if( p->pScript && p->mask&TESTVFS_FULLPATHNAME_MASK ){
729 int rc;
730 tvfsExecTcl(p, "xFullPathname", Tcl_NewStringObj(zPath, -1), 0, 0, 0);
731 if( tvfsResultCode(p, &rc) ){
732 if( rc!=SQLITE_OK ) return rc;
735 return sqlite3OsFullPathname(PARENTVFS(pVfs), zPath, nOut, zOut);
738 #ifndef SQLITE_OMIT_LOAD_EXTENSION
740 ** Open the dynamic library located at zPath and return a handle.
742 static void *tvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
743 return sqlite3OsDlOpen(PARENTVFS(pVfs), zPath);
747 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
748 ** utf-8 string describing the most recent error encountered associated
749 ** with dynamic libraries.
751 static void tvfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
752 sqlite3OsDlError(PARENTVFS(pVfs), nByte, zErrMsg);
756 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
758 static void (*tvfsDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
759 return sqlite3OsDlSym(PARENTVFS(pVfs), p, zSym);
763 ** Close the dynamic library handle pHandle.
765 static void tvfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
766 sqlite3OsDlClose(PARENTVFS(pVfs), pHandle);
768 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
771 ** Populate the buffer pointed to by zBufOut with nByte bytes of
772 ** random data.
774 static int tvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
775 return sqlite3OsRandomness(PARENTVFS(pVfs), nByte, zBufOut);
779 ** Sleep for nMicro microseconds. Return the number of microseconds
780 ** actually slept.
782 static int tvfsSleep(sqlite3_vfs *pVfs, int nMicro){
783 return sqlite3OsSleep(PARENTVFS(pVfs), nMicro);
787 ** Return the current time as a Julian Day number in *pTimeOut.
789 static int tvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
790 return PARENTVFS(pVfs)->xCurrentTime(PARENTVFS(pVfs), pTimeOut);
793 static int tvfsShmOpen(sqlite3_file *pFile){
794 Testvfs *p;
795 int rc = SQLITE_OK; /* Return code */
796 TestvfsBuffer *pBuffer; /* Buffer to open connection to */
797 TestvfsFd *pFd; /* The testvfs file structure */
799 pFd = tvfsGetFd(pFile);
800 p = (Testvfs *)pFd->pVfs->pAppData;
801 assert( 0==p->isFullshm );
802 assert( pFd->pShmId && pFd->pShm==0 && pFd->pNext==0 );
804 /* Evaluate the Tcl script:
806 ** SCRIPT xShmOpen FILENAME
808 Tcl_ResetResult(p->interp);
809 if( p->pScript && p->mask&TESTVFS_SHMOPEN_MASK ){
810 tvfsExecTcl(p, "xShmOpen", Tcl_NewStringObj(pFd->zFilename, -1), 0, 0, 0);
811 if( tvfsResultCode(p, &rc) ){
812 if( rc!=SQLITE_OK ) return rc;
816 assert( rc==SQLITE_OK );
817 if( p->mask&TESTVFS_SHMOPEN_MASK && tvfsInjectIoerr(p) ){
818 return SQLITE_IOERR;
821 /* Search for a TestvfsBuffer. Create a new one if required. */
822 for(pBuffer=p->pBuffer; pBuffer; pBuffer=pBuffer->pNext){
823 if( 0==strcmp(pFd->zFilename, pBuffer->zFile) ) break;
825 if( !pBuffer ){
826 int nByte = sizeof(TestvfsBuffer) + (int)strlen(pFd->zFilename) + 1;
827 pBuffer = (TestvfsBuffer *)ckalloc(nByte);
828 memset(pBuffer, 0, nByte);
829 pBuffer->zFile = (char *)&pBuffer[1];
830 strcpy(pBuffer->zFile, pFd->zFilename);
831 pBuffer->pNext = p->pBuffer;
832 p->pBuffer = pBuffer;
835 /* Connect the TestvfsBuffer to the new TestvfsShm handle and return. */
836 pFd->pNext = pBuffer->pFile;
837 pBuffer->pFile = pFd;
838 pFd->pShm = pBuffer;
839 return SQLITE_OK;
842 static void tvfsAllocPage(TestvfsBuffer *p, int iPage, int pgsz){
843 assert( iPage<TESTVFS_MAX_PAGES );
844 if( p->aPage[iPage]==0 ){
845 p->aPage[iPage] = (u8 *)ckalloc(pgsz);
846 memset(p->aPage[iPage], 0, pgsz);
847 p->pgsz = pgsz;
851 static int tvfsShmMap(
852 sqlite3_file *pFile, /* Handle open on database file */
853 int iPage, /* Page to retrieve */
854 int pgsz, /* Size of pages */
855 int isWrite, /* True to extend file if necessary */
856 void volatile **pp /* OUT: Mapped memory */
858 int rc = SQLITE_OK;
859 TestvfsFd *pFd = tvfsGetFd(pFile);
860 Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
862 if( p->isFullshm ){
863 return sqlite3OsShmMap(pFd->pReal, iPage, pgsz, isWrite, pp);
866 if( 0==pFd->pShm ){
867 rc = tvfsShmOpen(pFile);
868 if( rc!=SQLITE_OK ){
869 return rc;
873 if( p->pScript && p->mask&TESTVFS_SHMMAP_MASK ){
874 Tcl_Obj *pArg = Tcl_NewObj();
875 Tcl_IncrRefCount(pArg);
876 Tcl_ListObjAppendElement(p->interp, pArg, Tcl_NewIntObj(iPage));
877 Tcl_ListObjAppendElement(p->interp, pArg, Tcl_NewIntObj(pgsz));
878 Tcl_ListObjAppendElement(p->interp, pArg, Tcl_NewIntObj(isWrite));
879 tvfsExecTcl(p, "xShmMap",
880 Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, pArg, 0
882 tvfsResultCode(p, &rc);
883 Tcl_DecrRefCount(pArg);
885 if( rc==SQLITE_OK && p->mask&TESTVFS_SHMMAP_MASK && tvfsInjectIoerr(p) ){
886 rc = SQLITE_IOERR;
889 if( rc==SQLITE_OK && isWrite && !pFd->pShm->aPage[iPage] ){
890 tvfsAllocPage(pFd->pShm, iPage, pgsz);
892 *pp = (void volatile *)pFd->pShm->aPage[iPage];
894 return rc;
898 static int tvfsShmLock(
899 sqlite3_file *pFile,
900 int ofst,
901 int n,
902 int flags
904 int rc = SQLITE_OK;
905 TestvfsFd *pFd = tvfsGetFd(pFile);
906 Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
907 int nLock;
908 char zLock[80];
910 if( p->isFullshm ){
911 return sqlite3OsShmLock(pFd->pReal, ofst, n, flags);
914 if( p->pScript && p->mask&TESTVFS_SHMLOCK_MASK ){
915 sqlite3_snprintf(sizeof(zLock), zLock, "%d %d", ofst, n);
916 nLock = (int)strlen(zLock);
917 if( flags & SQLITE_SHM_LOCK ){
918 strcpy(&zLock[nLock], " lock");
919 }else{
920 strcpy(&zLock[nLock], " unlock");
922 nLock += (int)strlen(&zLock[nLock]);
923 if( flags & SQLITE_SHM_SHARED ){
924 strcpy(&zLock[nLock], " shared");
925 }else{
926 strcpy(&zLock[nLock], " exclusive");
928 tvfsExecTcl(p, "xShmLock",
929 Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId,
930 Tcl_NewStringObj(zLock, -1), 0
932 tvfsResultCode(p, &rc);
935 if( rc==SQLITE_OK && p->mask&TESTVFS_SHMLOCK_MASK && tvfsInjectIoerr(p) ){
936 rc = SQLITE_IOERR;
939 if( rc==SQLITE_OK ){
940 int isLock = (flags & SQLITE_SHM_LOCK);
941 int isExcl = (flags & SQLITE_SHM_EXCLUSIVE);
942 u32 mask = (((1<<n)-1) << ofst);
943 if( isLock ){
944 TestvfsFd *p2;
945 for(p2=pFd->pShm->pFile; p2; p2=p2->pNext){
946 if( p2==pFd ) continue;
947 if( (p2->excllock&mask) || (isExcl && p2->sharedlock&mask) ){
948 rc = SQLITE_BUSY;
949 break;
952 if( rc==SQLITE_OK ){
953 if( isExcl ) pFd->excllock |= mask;
954 if( !isExcl ) pFd->sharedlock |= mask;
956 }else{
957 if( isExcl ) pFd->excllock &= (~mask);
958 if( !isExcl ) pFd->sharedlock &= (~mask);
962 return rc;
965 static void tvfsShmBarrier(sqlite3_file *pFile){
966 TestvfsFd *pFd = tvfsGetFd(pFile);
967 Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
969 if( p->isFullshm ){
970 sqlite3OsShmBarrier(pFd->pReal);
971 return;
974 if( p->pScript && p->mask&TESTVFS_SHMBARRIER_MASK ){
975 tvfsExecTcl(p, "xShmBarrier",
976 Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0, 0
981 static int tvfsShmUnmap(
982 sqlite3_file *pFile,
983 int deleteFlag
985 int rc = SQLITE_OK;
986 TestvfsFd *pFd = tvfsGetFd(pFile);
987 Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
988 TestvfsBuffer *pBuffer = pFd->pShm;
989 TestvfsFd **ppFd;
991 if( p->isFullshm ){
992 return sqlite3OsShmUnmap(pFd->pReal, deleteFlag);
995 if( !pBuffer ) return SQLITE_OK;
996 assert( pFd->pShmId && pFd->pShm );
998 if( p->pScript && p->mask&TESTVFS_SHMCLOSE_MASK ){
999 tvfsExecTcl(p, "xShmUnmap",
1000 Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0, 0
1002 tvfsResultCode(p, &rc);
1005 for(ppFd=&pBuffer->pFile; *ppFd!=pFd; ppFd=&((*ppFd)->pNext));
1006 assert( (*ppFd)==pFd );
1007 *ppFd = pFd->pNext;
1008 pFd->pNext = 0;
1010 if( pBuffer->pFile==0 ){
1011 int i;
1012 TestvfsBuffer **pp;
1013 for(pp=&p->pBuffer; *pp!=pBuffer; pp=&((*pp)->pNext));
1014 *pp = (*pp)->pNext;
1015 for(i=0; pBuffer->aPage[i]; i++){
1016 ckfree((char *)pBuffer->aPage[i]);
1018 ckfree((char *)pBuffer);
1020 pFd->pShm = 0;
1022 return rc;
1025 static int tvfsFetch(
1026 sqlite3_file *pFile,
1027 sqlite3_int64 iOfst,
1028 int iAmt,
1029 void **pp
1031 TestvfsFd *pFd = tvfsGetFd(pFile);
1032 return sqlite3OsFetch(pFd->pReal, iOfst, iAmt, pp);
1035 static int tvfsUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *p){
1036 TestvfsFd *pFd = tvfsGetFd(pFile);
1037 return sqlite3OsUnfetch(pFd->pReal, iOfst, p);
1040 static int testvfs_obj_cmd(
1041 ClientData cd,
1042 Tcl_Interp *interp,
1043 int objc,
1044 Tcl_Obj *CONST objv[]
1046 Testvfs *p = (Testvfs *)cd;
1048 enum DB_enum {
1049 CMD_SHM, CMD_DELETE, CMD_FILTER, CMD_IOERR, CMD_SCRIPT,
1050 CMD_DEVCHAR, CMD_SECTORSIZE, CMD_FULLERR, CMD_CANTOPENERR
1052 struct TestvfsSubcmd {
1053 char *zName;
1054 enum DB_enum eCmd;
1055 } aSubcmd[] = {
1056 { "shm", CMD_SHM },
1057 { "delete", CMD_DELETE },
1058 { "filter", CMD_FILTER },
1059 { "ioerr", CMD_IOERR },
1060 { "fullerr", CMD_FULLERR },
1061 { "cantopenerr", CMD_CANTOPENERR },
1062 { "script", CMD_SCRIPT },
1063 { "devchar", CMD_DEVCHAR },
1064 { "sectorsize", CMD_SECTORSIZE },
1065 { 0, 0 }
1067 int i;
1069 if( objc<2 ){
1070 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
1071 return TCL_ERROR;
1073 if( Tcl_GetIndexFromObjStruct(
1074 interp, objv[1], aSubcmd, sizeof(aSubcmd[0]), "subcommand", 0, &i)
1076 return TCL_ERROR;
1078 Tcl_ResetResult(interp);
1080 switch( aSubcmd[i].eCmd ){
1081 case CMD_SHM: {
1082 Tcl_Obj *pObj;
1083 int i, rc;
1084 TestvfsBuffer *pBuffer;
1085 char *zName;
1086 if( objc!=3 && objc!=4 ){
1087 Tcl_WrongNumArgs(interp, 2, objv, "FILE ?VALUE?");
1088 return TCL_ERROR;
1090 zName = ckalloc(p->pParent->mxPathname);
1091 rc = p->pParent->xFullPathname(
1092 p->pParent, Tcl_GetString(objv[2]),
1093 p->pParent->mxPathname, zName
1095 if( rc!=SQLITE_OK ){
1096 Tcl_AppendResult(interp, "failed to get full path: ",
1097 Tcl_GetString(objv[2]), 0);
1098 ckfree(zName);
1099 return TCL_ERROR;
1101 for(pBuffer=p->pBuffer; pBuffer; pBuffer=pBuffer->pNext){
1102 if( 0==strcmp(pBuffer->zFile, zName) ) break;
1104 ckfree(zName);
1105 if( !pBuffer ){
1106 Tcl_AppendResult(interp, "no such file: ", Tcl_GetString(objv[2]), 0);
1107 return TCL_ERROR;
1109 if( objc==4 ){
1110 int n;
1111 u8 *a = Tcl_GetByteArrayFromObj(objv[3], &n);
1112 int pgsz = pBuffer->pgsz;
1113 if( pgsz==0 ) pgsz = 65536;
1114 for(i=0; i*pgsz<n; i++){
1115 int nByte = pgsz;
1116 tvfsAllocPage(pBuffer, i, pgsz);
1117 if( n-i*pgsz<pgsz ){
1118 nByte = n;
1120 memcpy(pBuffer->aPage[i], &a[i*pgsz], nByte);
1124 pObj = Tcl_NewObj();
1125 for(i=0; pBuffer->aPage[i]; i++){
1126 int pgsz = pBuffer->pgsz;
1127 if( pgsz==0 ) pgsz = 65536;
1128 Tcl_AppendObjToObj(pObj, Tcl_NewByteArrayObj(pBuffer->aPage[i], pgsz));
1130 Tcl_SetObjResult(interp, pObj);
1131 break;
1134 /* TESTVFS filter METHOD-LIST
1136 ** Activate special processing for those methods contained in the list
1138 case CMD_FILTER: {
1139 static struct VfsMethod {
1140 char *zName;
1141 int mask;
1142 } vfsmethod [] = {
1143 { "xShmOpen", TESTVFS_SHMOPEN_MASK },
1144 { "xShmLock", TESTVFS_SHMLOCK_MASK },
1145 { "xShmBarrier", TESTVFS_SHMBARRIER_MASK },
1146 { "xShmUnmap", TESTVFS_SHMCLOSE_MASK },
1147 { "xShmMap", TESTVFS_SHMMAP_MASK },
1148 { "xSync", TESTVFS_SYNC_MASK },
1149 { "xDelete", TESTVFS_DELETE_MASK },
1150 { "xWrite", TESTVFS_WRITE_MASK },
1151 { "xRead", TESTVFS_READ_MASK },
1152 { "xTruncate", TESTVFS_TRUNCATE_MASK },
1153 { "xOpen", TESTVFS_OPEN_MASK },
1154 { "xClose", TESTVFS_CLOSE_MASK },
1155 { "xAccess", TESTVFS_ACCESS_MASK },
1156 { "xFullPathname", TESTVFS_FULLPATHNAME_MASK },
1157 { "xUnlock", TESTVFS_UNLOCK_MASK },
1158 { "xLock", TESTVFS_LOCK_MASK },
1159 { "xCheckReservedLock", TESTVFS_CKLOCK_MASK },
1161 Tcl_Obj **apElem = 0;
1162 int nElem = 0;
1163 int i;
1164 int mask = 0;
1165 if( objc!=3 ){
1166 Tcl_WrongNumArgs(interp, 2, objv, "LIST");
1167 return TCL_ERROR;
1169 if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){
1170 return TCL_ERROR;
1172 Tcl_ResetResult(interp);
1173 for(i=0; i<nElem; i++){
1174 int iMethod;
1175 char *zElem = Tcl_GetString(apElem[i]);
1176 for(iMethod=0; iMethod<ArraySize(vfsmethod); iMethod++){
1177 if( strcmp(zElem, vfsmethod[iMethod].zName)==0 ){
1178 mask |= vfsmethod[iMethod].mask;
1179 break;
1182 if( iMethod==ArraySize(vfsmethod) ){
1183 Tcl_AppendResult(interp, "unknown method: ", zElem, 0);
1184 return TCL_ERROR;
1187 p->mask = mask;
1188 break;
1192 ** TESTVFS script ?SCRIPT?
1194 ** Query or set the script to be run when filtered VFS events
1195 ** occur.
1197 case CMD_SCRIPT: {
1198 if( objc==3 ){
1199 int nByte;
1200 if( p->pScript ){
1201 Tcl_DecrRefCount(p->pScript);
1202 p->pScript = 0;
1204 Tcl_GetStringFromObj(objv[2], &nByte);
1205 if( nByte>0 ){
1206 p->pScript = Tcl_DuplicateObj(objv[2]);
1207 Tcl_IncrRefCount(p->pScript);
1209 }else if( objc!=2 ){
1210 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?");
1211 return TCL_ERROR;
1214 Tcl_ResetResult(interp);
1215 if( p->pScript ) Tcl_SetObjResult(interp, p->pScript);
1217 break;
1221 ** TESTVFS ioerr ?IFAIL PERSIST?
1223 ** Where IFAIL is an integer and PERSIST is boolean.
1225 case CMD_CANTOPENERR:
1226 case CMD_IOERR:
1227 case CMD_FULLERR: {
1228 TestFaultInject *pTest;
1229 int iRet;
1231 switch( aSubcmd[i].eCmd ){
1232 case CMD_IOERR: pTest = &p->ioerr_err; break;
1233 case CMD_FULLERR: pTest = &p->full_err; break;
1234 case CMD_CANTOPENERR: pTest = &p->cantopen_err; break;
1235 default: assert(0);
1237 iRet = pTest->nFail;
1238 pTest->nFail = 0;
1239 pTest->eFault = 0;
1240 pTest->iCnt = 0;
1242 if( objc==4 ){
1243 int iCnt, iPersist;
1244 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iCnt)
1245 || TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &iPersist)
1247 return TCL_ERROR;
1249 pTest->eFault = iPersist?FAULT_INJECT_PERSISTENT:FAULT_INJECT_TRANSIENT;
1250 pTest->iCnt = iCnt;
1251 }else if( objc!=2 ){
1252 Tcl_WrongNumArgs(interp, 2, objv, "?CNT PERSIST?");
1253 return TCL_ERROR;
1255 Tcl_SetObjResult(interp, Tcl_NewIntObj(iRet));
1256 break;
1259 case CMD_DELETE: {
1260 Tcl_DeleteCommand(interp, Tcl_GetString(objv[0]));
1261 break;
1264 case CMD_DEVCHAR: {
1265 struct DeviceFlag {
1266 char *zName;
1267 int iValue;
1268 } aFlag[] = {
1269 { "default", -1 },
1270 { "atomic", SQLITE_IOCAP_ATOMIC },
1271 { "atomic512", SQLITE_IOCAP_ATOMIC512 },
1272 { "atomic1k", SQLITE_IOCAP_ATOMIC1K },
1273 { "atomic2k", SQLITE_IOCAP_ATOMIC2K },
1274 { "atomic4k", SQLITE_IOCAP_ATOMIC4K },
1275 { "atomic8k", SQLITE_IOCAP_ATOMIC8K },
1276 { "atomic16k", SQLITE_IOCAP_ATOMIC16K },
1277 { "atomic32k", SQLITE_IOCAP_ATOMIC32K },
1278 { "atomic64k", SQLITE_IOCAP_ATOMIC64K },
1279 { "sequential", SQLITE_IOCAP_SEQUENTIAL },
1280 { "safe_append", SQLITE_IOCAP_SAFE_APPEND },
1281 { "undeletable_when_open", SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN },
1282 { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE },
1283 { "immutable", SQLITE_IOCAP_IMMUTABLE },
1284 { 0, 0 }
1286 Tcl_Obj *pRet;
1287 int iFlag;
1289 if( objc>3 ){
1290 Tcl_WrongNumArgs(interp, 2, objv, "?ATTR-LIST?");
1291 return TCL_ERROR;
1293 if( objc==3 ){
1294 int j;
1295 int iNew = 0;
1296 Tcl_Obj **flags = 0;
1297 int nFlags = 0;
1299 if( Tcl_ListObjGetElements(interp, objv[2], &nFlags, &flags) ){
1300 return TCL_ERROR;
1303 for(j=0; j<nFlags; j++){
1304 int idx = 0;
1305 if( Tcl_GetIndexFromObjStruct(interp, flags[j], aFlag,
1306 sizeof(aFlag[0]), "flag", 0, &idx)
1308 return TCL_ERROR;
1310 if( aFlag[idx].iValue<0 && nFlags>1 ){
1311 Tcl_AppendResult(interp, "bad flags: ", Tcl_GetString(objv[2]), 0);
1312 return TCL_ERROR;
1314 iNew |= aFlag[idx].iValue;
1317 p->iDevchar = iNew| 0x10000000;
1320 pRet = Tcl_NewObj();
1321 for(iFlag=0; iFlag<sizeof(aFlag)/sizeof(aFlag[0]); iFlag++){
1322 if( p->iDevchar & aFlag[iFlag].iValue ){
1323 Tcl_ListObjAppendElement(
1324 interp, pRet, Tcl_NewStringObj(aFlag[iFlag].zName, -1)
1328 Tcl_SetObjResult(interp, pRet);
1330 break;
1333 case CMD_SECTORSIZE: {
1334 if( objc>3 ){
1335 Tcl_WrongNumArgs(interp, 2, objv, "?VALUE?");
1336 return TCL_ERROR;
1338 if( objc==3 ){
1339 int iNew = 0;
1340 if( Tcl_GetIntFromObj(interp, objv[2], &iNew) ){
1341 return TCL_ERROR;
1343 p->iSectorsize = iNew;
1345 Tcl_SetObjResult(interp, Tcl_NewIntObj(p->iSectorsize));
1346 break;
1350 return TCL_OK;
1353 static void testvfs_obj_del(ClientData cd){
1354 Testvfs *p = (Testvfs *)cd;
1355 if( p->pScript ) Tcl_DecrRefCount(p->pScript);
1356 sqlite3_vfs_unregister(p->pVfs);
1357 ckfree((char *)p->pVfs);
1358 ckfree((char *)p);
1362 ** Usage: testvfs VFSNAME ?SWITCHES?
1364 ** Switches are:
1366 ** -noshm BOOLEAN (True to omit shm methods. Default false)
1367 ** -default BOOLEAN (True to make the vfs default. Default false)
1369 ** This command creates two things when it is invoked: an SQLite VFS, and
1370 ** a Tcl command. Both are named VFSNAME. The VFS is installed. It is not
1371 ** installed as the default VFS.
1373 ** The VFS passes all file I/O calls through to the underlying VFS.
1375 ** Whenever the xShmMap method of the VFS
1376 ** is invoked, the SCRIPT is executed as follows:
1378 ** SCRIPT xShmMap FILENAME ID
1380 ** The value returned by the invocation of SCRIPT above is interpreted as
1381 ** an SQLite error code and returned to SQLite. Either a symbolic
1382 ** "SQLITE_OK" or numeric "0" value may be returned.
1384 ** The contents of the shared-memory buffer associated with a given file
1385 ** may be read and set using the following command:
1387 ** VFSNAME shm FILENAME ?NEWVALUE?
1389 ** When the xShmLock method is invoked by SQLite, the following script is
1390 ** run:
1392 ** SCRIPT xShmLock FILENAME ID LOCK
1394 ** where LOCK is of the form "OFFSET NBYTE lock/unlock shared/exclusive"
1396 static int testvfs_cmd(
1397 ClientData cd,
1398 Tcl_Interp *interp,
1399 int objc,
1400 Tcl_Obj *CONST objv[]
1402 static sqlite3_vfs tvfs_vfs = {
1403 3, /* iVersion */
1404 0, /* szOsFile */
1405 0, /* mxPathname */
1406 0, /* pNext */
1407 0, /* zName */
1408 0, /* pAppData */
1409 tvfsOpen, /* xOpen */
1410 tvfsDelete, /* xDelete */
1411 tvfsAccess, /* xAccess */
1412 tvfsFullPathname, /* xFullPathname */
1413 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1414 tvfsDlOpen, /* xDlOpen */
1415 tvfsDlError, /* xDlError */
1416 tvfsDlSym, /* xDlSym */
1417 tvfsDlClose, /* xDlClose */
1418 #else
1419 0, /* xDlOpen */
1420 0, /* xDlError */
1421 0, /* xDlSym */
1422 0, /* xDlClose */
1423 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
1424 tvfsRandomness, /* xRandomness */
1425 tvfsSleep, /* xSleep */
1426 tvfsCurrentTime, /* xCurrentTime */
1427 0, /* xGetLastError */
1428 0, /* xCurrentTimeInt64 */
1429 0, /* xSetSystemCall */
1430 0, /* xGetSystemCall */
1431 0, /* xNextSystemCall */
1434 Testvfs *p; /* New object */
1435 sqlite3_vfs *pVfs; /* New VFS */
1436 char *zVfs;
1437 int nByte; /* Bytes of space to allocate at p */
1439 int i;
1440 int isNoshm = 0; /* True if -noshm is passed */
1441 int isFullshm = 0; /* True if -fullshm is passed */
1442 int isDefault = 0; /* True if -default is passed */
1443 int szOsFile = 0; /* Value passed to -szosfile */
1444 int mxPathname = -1; /* Value passed to -mxpathname */
1445 int iVersion = 3; /* Value passed to -iversion */
1447 if( objc<2 || 0!=(objc%2) ) goto bad_args;
1448 for(i=2; i<objc; i += 2){
1449 int nSwitch;
1450 char *zSwitch;
1451 zSwitch = Tcl_GetStringFromObj(objv[i], &nSwitch);
1453 if( nSwitch>2 && 0==strncmp("-noshm", zSwitch, nSwitch) ){
1454 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &isNoshm) ){
1455 return TCL_ERROR;
1457 if( isNoshm ) isFullshm = 0;
1459 else if( nSwitch>2 && 0==strncmp("-default", zSwitch, nSwitch) ){
1460 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &isDefault) ){
1461 return TCL_ERROR;
1464 else if( nSwitch>2 && 0==strncmp("-szosfile", zSwitch, nSwitch) ){
1465 if( Tcl_GetIntFromObj(interp, objv[i+1], &szOsFile) ){
1466 return TCL_ERROR;
1469 else if( nSwitch>2 && 0==strncmp("-mxpathname", zSwitch, nSwitch) ){
1470 if( Tcl_GetIntFromObj(interp, objv[i+1], &mxPathname) ){
1471 return TCL_ERROR;
1474 else if( nSwitch>2 && 0==strncmp("-iversion", zSwitch, nSwitch) ){
1475 if( Tcl_GetIntFromObj(interp, objv[i+1], &iVersion) ){
1476 return TCL_ERROR;
1479 else if( nSwitch>2 && 0==strncmp("-fullshm", zSwitch, nSwitch) ){
1480 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &isFullshm) ){
1481 return TCL_ERROR;
1483 if( isFullshm ) isNoshm = 0;
1485 else{
1486 goto bad_args;
1490 if( szOsFile<sizeof(TestvfsFile) ){
1491 szOsFile = sizeof(TestvfsFile);
1494 zVfs = Tcl_GetString(objv[1]);
1495 nByte = sizeof(Testvfs) + (int)strlen(zVfs)+1;
1496 p = (Testvfs *)ckalloc(nByte);
1497 memset(p, 0, nByte);
1498 p->iDevchar = -1;
1499 p->iSectorsize = -1;
1501 /* Create the new object command before querying SQLite for a default VFS
1502 ** to use for 'real' IO operations. This is because creating the new VFS
1503 ** may delete an existing [testvfs] VFS of the same name. If such a VFS
1504 ** is currently the default, the new [testvfs] may end up calling the
1505 ** methods of a deleted object.
1507 Tcl_CreateObjCommand(interp, zVfs, testvfs_obj_cmd, p, testvfs_obj_del);
1508 p->pParent = sqlite3_vfs_find(0);
1509 p->interp = interp;
1511 p->zName = (char *)&p[1];
1512 memcpy(p->zName, zVfs, strlen(zVfs)+1);
1514 pVfs = (sqlite3_vfs *)ckalloc(sizeof(sqlite3_vfs));
1515 memcpy(pVfs, &tvfs_vfs, sizeof(sqlite3_vfs));
1516 pVfs->pAppData = (void *)p;
1517 pVfs->iVersion = iVersion;
1518 pVfs->zName = p->zName;
1519 pVfs->mxPathname = p->pParent->mxPathname;
1520 if( mxPathname>=0 && mxPathname<pVfs->mxPathname ){
1521 pVfs->mxPathname = mxPathname;
1523 pVfs->szOsFile = szOsFile;
1524 p->pVfs = pVfs;
1525 p->isNoshm = isNoshm;
1526 p->isFullshm = isFullshm;
1527 p->mask = TESTVFS_ALL_MASK;
1529 sqlite3_vfs_register(pVfs, isDefault);
1531 return TCL_OK;
1533 bad_args:
1534 Tcl_WrongNumArgs(interp, 1, objv, "VFSNAME ?-noshm BOOL? ?-default BOOL? ?-mxpathname INT? ?-szosfile INT? ?-iversion INT?");
1535 return TCL_ERROR;
1538 int Sqlitetestvfs_Init(Tcl_Interp *interp){
1539 Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0);
1540 return TCL_OK;
1543 #endif