4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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 */
30 #include "sqliteInt.h"
31 #if defined(INCLUDE_SQLITE_TCL_H)
32 # include "sqlite_tcl.h"
37 typedef struct Testvfs Testvfs
;
38 typedef struct TestvfsShm TestvfsShm
;
39 typedef struct TestvfsBuffer TestvfsBuffer
;
40 typedef struct TestvfsFile TestvfsFile
;
41 typedef struct TestvfsFd TestvfsFd
;
44 ** An open file handle.
47 sqlite3_file base
; /* Base class. Must be first */
48 TestvfsFd
*pFd
; /* File data */
50 #define tvfsGetFd(pFile) (((TestvfsFile *)pFile)->pFd)
53 sqlite3_vfs
*pVfs
; /* The VFS */
54 const char *zFilename
; /* Filename as passed to xOpen() */
55 sqlite3_file
*pReal
; /* The real, underlying file descriptor */
56 Tcl_Obj
*pShmId
; /* Shared memory id for Tcl callbacks */
58 TestvfsBuffer
*pShm
; /* Shared memory buffer */
59 u32 excllock
; /* Mask of exclusive locks */
60 u32 sharedlock
; /* Mask of shared locks */
61 TestvfsFd
*pNext
; /* Next handle opened on the same file */
65 #define FAULT_INJECT_NONE 0
66 #define FAULT_INJECT_TRANSIENT 1
67 #define FAULT_INJECT_PERSISTENT 2
69 typedef struct TestFaultInject TestFaultInject
;
70 struct TestFaultInject
{
71 int iCnt
; /* Remaining calls before fault injection */
72 int eFault
; /* A FAULT_INJECT_* value */
73 int nFail
; /* Number of faults injected */
77 ** An instance of this structure is allocated for each VFS created. The
78 ** sqlite3_vfs.pAppData field of the VFS structure registered with SQLite
79 ** is set to point to it.
82 char *zName
; /* Name of this VFS */
83 sqlite3_vfs
*pParent
; /* The VFS to use for file IO */
84 sqlite3_vfs
*pVfs
; /* The testvfs registered with SQLite */
85 Tcl_Interp
*interp
; /* Interpreter to run script in */
86 Tcl_Obj
*pScript
; /* Script to execute */
87 TestvfsBuffer
*pBuffer
; /* List of shared buffers */
91 int mask
; /* Mask controlling [script] and [ioerr] */
93 TestFaultInject ioerr_err
;
94 TestFaultInject full_err
;
95 TestFaultInject cantopen_err
;
111 ** The Testvfs.mask variable is set to a combination of the following.
112 ** If a bit is clear in Testvfs.mask, then calls made by SQLite to the
113 ** corresponding VFS method is ignored for purposes of:
115 ** + Simulating IO errors, and
116 ** + Invoking the Tcl callback script.
118 #define TESTVFS_SHMOPEN_MASK 0x00000001
119 #define TESTVFS_SHMLOCK_MASK 0x00000010
120 #define TESTVFS_SHMMAP_MASK 0x00000020
121 #define TESTVFS_SHMBARRIER_MASK 0x00000040
122 #define TESTVFS_SHMCLOSE_MASK 0x00000080
124 #define TESTVFS_OPEN_MASK 0x00000100
125 #define TESTVFS_SYNC_MASK 0x00000200
126 #define TESTVFS_DELETE_MASK 0x00000400
127 #define TESTVFS_CLOSE_MASK 0x00000800
128 #define TESTVFS_WRITE_MASK 0x00001000
129 #define TESTVFS_TRUNCATE_MASK 0x00002000
130 #define TESTVFS_ACCESS_MASK 0x00004000
131 #define TESTVFS_FULLPATHNAME_MASK 0x00008000
132 #define TESTVFS_READ_MASK 0x00010000
133 #define TESTVFS_UNLOCK_MASK 0x00020000
134 #define TESTVFS_LOCK_MASK 0x00040000
135 #define TESTVFS_CKLOCK_MASK 0x00080000
137 #define TESTVFS_ALL_MASK 0x000FFFFF
140 #define TESTVFS_MAX_PAGES 1024
143 ** A shared-memory buffer. There is one of these objects for each shared
144 ** memory region opened by clients. If two clients open the same file,
145 ** there are two TestvfsFile structures but only one TestvfsBuffer structure.
147 struct TestvfsBuffer
{
148 char *zFile
; /* Associated file name */
149 int pgsz
; /* Page size */
150 u8
*aPage
[TESTVFS_MAX_PAGES
]; /* Array of ckalloc'd pages */
151 TestvfsFd
*pFile
; /* List of open handles */
152 TestvfsBuffer
*pNext
; /* Next in linked list of all buffers */
156 #define PARENTVFS(x) (((Testvfs *)((x)->pAppData))->pParent)
158 #define TESTVFS_MAX_ARGS 12
162 ** Method declarations for TestvfsFile.
164 static int tvfsClose(sqlite3_file
*);
165 static int tvfsRead(sqlite3_file
*, void*, int iAmt
, sqlite3_int64 iOfst
);
166 static int tvfsWrite(sqlite3_file
*,const void*,int iAmt
, sqlite3_int64 iOfst
);
167 static int tvfsTruncate(sqlite3_file
*, sqlite3_int64 size
);
168 static int tvfsSync(sqlite3_file
*, int flags
);
169 static int tvfsFileSize(sqlite3_file
*, sqlite3_int64
*pSize
);
170 static int tvfsLock(sqlite3_file
*, int);
171 static int tvfsUnlock(sqlite3_file
*, int);
172 static int tvfsCheckReservedLock(sqlite3_file
*, int *);
173 static int tvfsFileControl(sqlite3_file
*, int op
, void *pArg
);
174 static int tvfsSectorSize(sqlite3_file
*);
175 static int tvfsDeviceCharacteristics(sqlite3_file
*);
178 ** Method declarations for tvfs_vfs.
180 static int tvfsOpen(sqlite3_vfs
*, const char *, sqlite3_file
*, int , int *);
181 static int tvfsDelete(sqlite3_vfs
*, const char *zName
, int syncDir
);
182 static int tvfsAccess(sqlite3_vfs
*, const char *zName
, int flags
, int *);
183 static int tvfsFullPathname(sqlite3_vfs
*, const char *zName
, int, char *zOut
);
184 #ifndef SQLITE_OMIT_LOAD_EXTENSION
185 static void *tvfsDlOpen(sqlite3_vfs
*, const char *zFilename
);
186 static void tvfsDlError(sqlite3_vfs
*, int nByte
, char *zErrMsg
);
187 static void (*tvfsDlSym(sqlite3_vfs
*,void*, const char *zSymbol
))(void);
188 static void tvfsDlClose(sqlite3_vfs
*, void*);
189 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
190 static int tvfsRandomness(sqlite3_vfs
*, int nByte
, char *zOut
);
191 static int tvfsSleep(sqlite3_vfs
*, int microseconds
);
192 static int tvfsCurrentTime(sqlite3_vfs
*, double*);
194 static int tvfsShmOpen(sqlite3_file
*);
195 static int tvfsShmLock(sqlite3_file
*, int , int, int);
196 static int tvfsShmMap(sqlite3_file
*,int,int,int, void volatile **);
197 static void tvfsShmBarrier(sqlite3_file
*);
198 static int tvfsShmUnmap(sqlite3_file
*, int);
200 static int tvfsFetch(sqlite3_file
*, sqlite3_int64
, int, void**);
201 static int tvfsUnfetch(sqlite3_file
*, sqlite3_int64
, void*);
203 static sqlite3_io_methods tvfs_io_methods
= {
205 tvfsClose
, /* xClose */
206 tvfsRead
, /* xRead */
207 tvfsWrite
, /* xWrite */
208 tvfsTruncate
, /* xTruncate */
209 tvfsSync
, /* xSync */
210 tvfsFileSize
, /* xFileSize */
211 tvfsLock
, /* xLock */
212 tvfsUnlock
, /* xUnlock */
213 tvfsCheckReservedLock
, /* xCheckReservedLock */
214 tvfsFileControl
, /* xFileControl */
215 tvfsSectorSize
, /* xSectorSize */
216 tvfsDeviceCharacteristics
, /* xDeviceCharacteristics */
217 tvfsShmMap
, /* xShmMap */
218 tvfsShmLock
, /* xShmLock */
219 tvfsShmBarrier
, /* xShmBarrier */
220 tvfsShmUnmap
, /* xShmUnmap */
225 static int tvfsResultCode(Testvfs
*p
, int *pRc
){
230 { SQLITE_OK
, "SQLITE_OK" },
231 { SQLITE_ERROR
, "SQLITE_ERROR" },
232 { SQLITE_IOERR
, "SQLITE_IOERR" },
233 { SQLITE_LOCKED
, "SQLITE_LOCKED" },
234 { SQLITE_BUSY
, "SQLITE_BUSY" },
240 z
= Tcl_GetStringResult(p
->interp
);
241 for(i
=0; i
<ArraySize(aCode
); i
++){
242 if( 0==strcmp(z
, aCode
[i
].zCode
) ){
243 *pRc
= aCode
[i
].eCode
;
251 static int tvfsInjectFault(TestFaultInject
*p
){
255 if( p
->iCnt
==0 || (p
->iCnt
<0 && p
->eFault
==FAULT_INJECT_PERSISTENT
) ){
264 static int tvfsInjectIoerr(Testvfs
*p
){
265 return tvfsInjectFault(&p
->ioerr_err
);
268 static int tvfsInjectFullerr(Testvfs
*p
){
269 return tvfsInjectFault(&p
->full_err
);
271 static int tvfsInjectCantopenerr(Testvfs
*p
){
272 return tvfsInjectFault(&p
->cantopen_err
);
276 static void tvfsExecTcl(
284 int rc
; /* Return code from Tcl_EvalObj() */
286 assert( p
->pScript
);
290 assert( arg2
==0 || arg1
!=0 );
291 assert( arg3
==0 || arg2
!=0 );
293 pEval
= Tcl_DuplicateObj(p
->pScript
);
294 Tcl_IncrRefCount(p
->pScript
);
295 Tcl_ListObjAppendElement(p
->interp
, pEval
, Tcl_NewStringObj(zMethod
, -1));
296 if( arg1
) Tcl_ListObjAppendElement(p
->interp
, pEval
, arg1
);
297 if( arg2
) Tcl_ListObjAppendElement(p
->interp
, pEval
, arg2
);
298 if( arg3
) Tcl_ListObjAppendElement(p
->interp
, pEval
, arg3
);
299 if( arg4
) Tcl_ListObjAppendElement(p
->interp
, pEval
, arg4
);
301 rc
= Tcl_EvalObjEx(p
->interp
, pEval
, TCL_EVAL_GLOBAL
);
303 Tcl_BackgroundError(p
->interp
);
304 Tcl_ResetResult(p
->interp
);
310 ** Close an tvfs-file.
312 static int tvfsClose(sqlite3_file
*pFile
){
313 TestvfsFile
*pTestfile
= (TestvfsFile
*)pFile
;
314 TestvfsFd
*pFd
= pTestfile
->pFd
;
315 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
317 if( p
->pScript
&& p
->mask
&TESTVFS_CLOSE_MASK
){
318 tvfsExecTcl(p
, "xClose",
319 Tcl_NewStringObj(pFd
->zFilename
, -1), pFd
->pShmId
, 0, 0
324 Tcl_DecrRefCount(pFd
->pShmId
);
327 if( pFile
->pMethods
){
328 ckfree((char *)pFile
->pMethods
);
330 sqlite3OsClose(pFd
->pReal
);
337 ** Read data from an tvfs-file.
346 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
347 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
348 if( p
->pScript
&& p
->mask
&TESTVFS_READ_MASK
){
349 tvfsExecTcl(p
, "xRead",
350 Tcl_NewStringObj(pFd
->zFilename
, -1), pFd
->pShmId
, 0, 0
352 tvfsResultCode(p
, &rc
);
354 if( rc
==SQLITE_OK
&& p
->mask
&TESTVFS_READ_MASK
&& tvfsInjectIoerr(p
) ){
358 rc
= sqlite3OsRead(pFd
->pReal
, zBuf
, iAmt
, iOfst
);
364 ** Write data to an tvfs-file.
366 static int tvfsWrite(
373 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
374 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
376 if( p
->pScript
&& p
->mask
&TESTVFS_WRITE_MASK
){
377 tvfsExecTcl(p
, "xWrite",
378 Tcl_NewStringObj(pFd
->zFilename
, -1), pFd
->pShmId
,
379 Tcl_NewWideIntObj(iOfst
), Tcl_NewIntObj(iAmt
)
381 tvfsResultCode(p
, &rc
);
384 if( rc
==SQLITE_OK
&& tvfsInjectFullerr(p
) ){
387 if( rc
==SQLITE_OK
&& p
->mask
&TESTVFS_WRITE_MASK
&& tvfsInjectIoerr(p
) ){
392 rc
= sqlite3OsWrite(pFd
->pReal
, zBuf
, iAmt
, iOfst
);
398 ** Truncate an tvfs-file.
400 static int tvfsTruncate(sqlite3_file
*pFile
, sqlite_int64 size
){
402 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
403 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
405 if( p
->pScript
&& p
->mask
&TESTVFS_TRUNCATE_MASK
){
406 tvfsExecTcl(p
, "xTruncate",
407 Tcl_NewStringObj(pFd
->zFilename
, -1), pFd
->pShmId
, 0, 0
409 tvfsResultCode(p
, &rc
);
413 rc
= sqlite3OsTruncate(pFd
->pReal
, size
);
419 ** Sync an tvfs-file.
421 static int tvfsSync(sqlite3_file
*pFile
, int flags
){
423 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
424 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
426 if( p
->pScript
&& p
->mask
&TESTVFS_SYNC_MASK
){
430 case SQLITE_SYNC_NORMAL
:
433 case SQLITE_SYNC_FULL
:
436 case SQLITE_SYNC_NORMAL
|SQLITE_SYNC_DATAONLY
:
437 zFlags
= "normal|dataonly";
439 case SQLITE_SYNC_FULL
|SQLITE_SYNC_DATAONLY
:
440 zFlags
= "full|dataonly";
446 tvfsExecTcl(p
, "xSync",
447 Tcl_NewStringObj(pFd
->zFilename
, -1), pFd
->pShmId
,
448 Tcl_NewStringObj(zFlags
, -1), 0
450 tvfsResultCode(p
, &rc
);
453 if( rc
==SQLITE_OK
&& tvfsInjectFullerr(p
) ) rc
= SQLITE_FULL
;
456 rc
= sqlite3OsSync(pFd
->pReal
, flags
);
463 ** Return the current file-size of an tvfs-file.
465 static int tvfsFileSize(sqlite3_file
*pFile
, sqlite_int64
*pSize
){
466 TestvfsFd
*p
= tvfsGetFd(pFile
);
467 return sqlite3OsFileSize(p
->pReal
, pSize
);
471 ** Lock an tvfs-file.
473 static int tvfsLock(sqlite3_file
*pFile
, int eLock
){
474 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
475 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
476 if( p
->pScript
&& p
->mask
&TESTVFS_LOCK_MASK
){
478 sqlite3_snprintf(sizeof(zLock
),zLock
,"%d",eLock
);
479 tvfsExecTcl(p
, "xLock", Tcl_NewStringObj(pFd
->zFilename
, -1),
480 Tcl_NewStringObj(zLock
, -1), 0, 0);
482 return sqlite3OsLock(pFd
->pReal
, eLock
);
486 ** Unlock an tvfs-file.
488 static int tvfsUnlock(sqlite3_file
*pFile
, int eLock
){
489 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
490 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
491 if( p
->pScript
&& p
->mask
&TESTVFS_UNLOCK_MASK
){
493 sqlite3_snprintf(sizeof(zLock
),zLock
,"%d",eLock
);
494 tvfsExecTcl(p
, "xUnlock", Tcl_NewStringObj(pFd
->zFilename
, -1),
495 Tcl_NewStringObj(zLock
, -1), 0, 0);
497 if( p
->mask
&TESTVFS_WRITE_MASK
&& tvfsInjectIoerr(p
) ){
498 return SQLITE_IOERR_UNLOCK
;
500 return sqlite3OsUnlock(pFd
->pReal
, eLock
);
504 ** Check if another file-handle holds a RESERVED lock on an tvfs-file.
506 static int tvfsCheckReservedLock(sqlite3_file
*pFile
, int *pResOut
){
507 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
508 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
509 if( p
->pScript
&& p
->mask
&TESTVFS_CKLOCK_MASK
){
510 tvfsExecTcl(p
, "xCheckReservedLock", Tcl_NewStringObj(pFd
->zFilename
, -1),
513 return sqlite3OsCheckReservedLock(pFd
->pReal
, pResOut
);
517 ** File control method. For custom operations on an tvfs-file.
519 static int tvfsFileControl(sqlite3_file
*pFile
, int op
, void *pArg
){
520 TestvfsFd
*p
= tvfsGetFd(pFile
);
521 if( op
==SQLITE_FCNTL_PRAGMA
){
522 char **argv
= (char**)pArg
;
523 if( sqlite3_stricmp(argv
[1],"error")==0 ){
524 int rc
= SQLITE_ERROR
;
526 const char *z
= argv
[2];
530 while( sqlite3Isdigit(z
[0]) ){ z
++; }
531 while( sqlite3Isspace(z
[0]) ){ z
++; }
533 if( z
[0] ) argv
[0] = sqlite3_mprintf("%s", z
);
537 if( sqlite3_stricmp(argv
[1], "filename")==0 ){
538 argv
[0] = sqlite3_mprintf("%s", p
->zFilename
);
542 return sqlite3OsFileControl(p
->pReal
, op
, pArg
);
546 ** Return the sector-size in bytes for an tvfs-file.
548 static int tvfsSectorSize(sqlite3_file
*pFile
){
549 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
550 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
551 if( p
->iSectorsize
>=0 ){
552 return p
->iSectorsize
;
554 return sqlite3OsSectorSize(pFd
->pReal
);
558 ** Return the device characteristic flags supported by an tvfs-file.
560 static int tvfsDeviceCharacteristics(sqlite3_file
*pFile
){
561 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
562 Testvfs
*p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
563 if( p
->iDevchar
>=0 ){
566 return sqlite3OsDeviceCharacteristics(pFd
->pReal
);
570 ** Open an tvfs file handle.
580 TestvfsFile
*pTestfile
= (TestvfsFile
*)pFile
;
583 Testvfs
*p
= (Testvfs
*)pVfs
->pAppData
;
585 pFd
= (TestvfsFd
*)ckalloc(sizeof(TestvfsFd
) + PARENTVFS(pVfs
)->szOsFile
);
586 memset(pFd
, 0, sizeof(TestvfsFd
) + PARENTVFS(pVfs
)->szOsFile
);
589 pFd
->zFilename
= zName
;
591 pFd
->pReal
= (sqlite3_file
*)&pFd
[1];
592 memset(pTestfile
, 0, sizeof(TestvfsFile
));
593 pTestfile
->pFd
= pFd
;
595 /* Evaluate the Tcl script:
597 ** SCRIPT xOpen FILENAME KEY-VALUE-ARGS
599 ** If the script returns an SQLite error code other than SQLITE_OK, an
600 ** error is returned to the caller. If it returns SQLITE_OK, the new
601 ** connection is named "anon". Otherwise, the value returned by the
602 ** script is used as the connection name.
604 Tcl_ResetResult(p
->interp
);
605 if( p
->pScript
&& p
->mask
&TESTVFS_OPEN_MASK
){
606 Tcl_Obj
*pArg
= Tcl_NewObj();
607 Tcl_IncrRefCount(pArg
);
608 if( flags
&SQLITE_OPEN_MAIN_DB
){
609 const char *z
= &zName
[strlen(zName
)+1];
611 Tcl_ListObjAppendElement(0, pArg
, Tcl_NewStringObj(z
, -1));
613 Tcl_ListObjAppendElement(0, pArg
, Tcl_NewStringObj(z
, -1));
617 tvfsExecTcl(p
, "xOpen", Tcl_NewStringObj(pFd
->zFilename
, -1), pArg
, 0, 0);
618 Tcl_DecrRefCount(pArg
);
619 if( tvfsResultCode(p
, &rc
) ){
620 if( rc
!=SQLITE_OK
) return rc
;
622 pId
= Tcl_GetObjResult(p
->interp
);
626 if( (p
->mask
&TESTVFS_OPEN_MASK
) && tvfsInjectIoerr(p
) ) return SQLITE_IOERR
;
627 if( tvfsInjectCantopenerr(p
) ) return SQLITE_CANTOPEN
;
628 if( tvfsInjectFullerr(p
) ) return SQLITE_FULL
;
631 pId
= Tcl_NewStringObj("anon", -1);
633 Tcl_IncrRefCount(pId
);
635 Tcl_ResetResult(p
->interp
);
637 rc
= sqlite3OsOpen(PARENTVFS(pVfs
), zName
, pFd
->pReal
, flags
, pOutFlags
);
638 if( pFd
->pReal
->pMethods
){
639 sqlite3_io_methods
*pMethods
;
642 if( pVfs
->iVersion
>1 ){
643 nByte
= sizeof(sqlite3_io_methods
);
645 nByte
= offsetof(sqlite3_io_methods
, xShmMap
);
648 pMethods
= (sqlite3_io_methods
*)ckalloc(nByte
);
649 memcpy(pMethods
, &tvfs_io_methods
, nByte
);
650 pMethods
->iVersion
= pFd
->pReal
->pMethods
->iVersion
;
651 if( pMethods
->iVersion
>pVfs
->iVersion
){
652 pMethods
->iVersion
= pVfs
->iVersion
;
654 if( pVfs
->iVersion
>1 && ((Testvfs
*)pVfs
->pAppData
)->isNoshm
){
655 pMethods
->xShmUnmap
= 0;
656 pMethods
->xShmLock
= 0;
657 pMethods
->xShmBarrier
= 0;
658 pMethods
->xShmMap
= 0;
660 pFile
->pMethods
= pMethods
;
667 ** Delete the file located at zPath. If the dirSync argument is true,
668 ** ensure the file-system modifications are synced to disk before
671 static int tvfsDelete(sqlite3_vfs
*pVfs
, const char *zPath
, int dirSync
){
673 Testvfs
*p
= (Testvfs
*)pVfs
->pAppData
;
675 if( p
->pScript
&& p
->mask
&TESTVFS_DELETE_MASK
){
676 tvfsExecTcl(p
, "xDelete",
677 Tcl_NewStringObj(zPath
, -1), Tcl_NewIntObj(dirSync
), 0, 0
679 tvfsResultCode(p
, &rc
);
682 rc
= sqlite3OsDelete(PARENTVFS(pVfs
), zPath
, dirSync
);
688 ** Test for access permissions. Return true if the requested permission
689 ** is available, or false otherwise.
691 static int tvfsAccess(
697 Testvfs
*p
= (Testvfs
*)pVfs
->pAppData
;
698 if( p
->pScript
&& p
->mask
&TESTVFS_ACCESS_MASK
){
701 if( flags
==SQLITE_ACCESS_EXISTS
) zArg
= "SQLITE_ACCESS_EXISTS";
702 if( flags
==SQLITE_ACCESS_READWRITE
) zArg
= "SQLITE_ACCESS_READWRITE";
703 if( flags
==SQLITE_ACCESS_READ
) zArg
= "SQLITE_ACCESS_READ";
704 tvfsExecTcl(p
, "xAccess",
705 Tcl_NewStringObj(zPath
, -1), Tcl_NewStringObj(zArg
, -1), 0, 0
707 if( tvfsResultCode(p
, &rc
) ){
708 if( rc
!=SQLITE_OK
) return rc
;
710 Tcl_Interp
*interp
= p
->interp
;
711 if( TCL_OK
==Tcl_GetBooleanFromObj(0, Tcl_GetObjResult(interp
), pResOut
) ){
716 return sqlite3OsAccess(PARENTVFS(pVfs
), zPath
, flags
, pResOut
);
720 ** Populate buffer zOut with the full canonical pathname corresponding
721 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
722 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
724 static int tvfsFullPathname(
730 Testvfs
*p
= (Testvfs
*)pVfs
->pAppData
;
731 if( p
->pScript
&& p
->mask
&TESTVFS_FULLPATHNAME_MASK
){
733 tvfsExecTcl(p
, "xFullPathname", Tcl_NewStringObj(zPath
, -1), 0, 0, 0);
734 if( tvfsResultCode(p
, &rc
) ){
735 if( rc
!=SQLITE_OK
) return rc
;
738 return sqlite3OsFullPathname(PARENTVFS(pVfs
), zPath
, nOut
, zOut
);
741 #ifndef SQLITE_OMIT_LOAD_EXTENSION
743 ** Open the dynamic library located at zPath and return a handle.
745 static void *tvfsDlOpen(sqlite3_vfs
*pVfs
, const char *zPath
){
746 return sqlite3OsDlOpen(PARENTVFS(pVfs
), zPath
);
750 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
751 ** utf-8 string describing the most recent error encountered associated
752 ** with dynamic libraries.
754 static void tvfsDlError(sqlite3_vfs
*pVfs
, int nByte
, char *zErrMsg
){
755 sqlite3OsDlError(PARENTVFS(pVfs
), nByte
, zErrMsg
);
759 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
761 static void (*tvfsDlSym(sqlite3_vfs
*pVfs
, void *p
, const char *zSym
))(void){
762 return sqlite3OsDlSym(PARENTVFS(pVfs
), p
, zSym
);
766 ** Close the dynamic library handle pHandle.
768 static void tvfsDlClose(sqlite3_vfs
*pVfs
, void *pHandle
){
769 sqlite3OsDlClose(PARENTVFS(pVfs
), pHandle
);
771 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
774 ** Populate the buffer pointed to by zBufOut with nByte bytes of
777 static int tvfsRandomness(sqlite3_vfs
*pVfs
, int nByte
, char *zBufOut
){
778 return sqlite3OsRandomness(PARENTVFS(pVfs
), nByte
, zBufOut
);
782 ** Sleep for nMicro microseconds. Return the number of microseconds
785 static int tvfsSleep(sqlite3_vfs
*pVfs
, int nMicro
){
786 return sqlite3OsSleep(PARENTVFS(pVfs
), nMicro
);
790 ** Return the current time as a Julian Day number in *pTimeOut.
792 static int tvfsCurrentTime(sqlite3_vfs
*pVfs
, double *pTimeOut
){
793 return PARENTVFS(pVfs
)->xCurrentTime(PARENTVFS(pVfs
), pTimeOut
);
796 static int tvfsShmOpen(sqlite3_file
*pFile
){
798 int rc
= SQLITE_OK
; /* Return code */
799 TestvfsBuffer
*pBuffer
; /* Buffer to open connection to */
800 TestvfsFd
*pFd
; /* The testvfs file structure */
802 pFd
= tvfsGetFd(pFile
);
803 p
= (Testvfs
*)pFd
->pVfs
->pAppData
;
804 assert( 0==p
->isFullshm
);
805 assert( pFd
->pShmId
&& pFd
->pShm
==0 && pFd
->pNext
==0 );
807 /* Evaluate the Tcl script:
809 ** SCRIPT xShmOpen FILENAME
811 Tcl_ResetResult(p
->interp
);
812 if( p
->pScript
&& p
->mask
&TESTVFS_SHMOPEN_MASK
){
813 tvfsExecTcl(p
, "xShmOpen", Tcl_NewStringObj(pFd
->zFilename
, -1), 0, 0, 0);
814 if( tvfsResultCode(p
, &rc
) ){
815 if( rc
!=SQLITE_OK
) return rc
;
819 assert( rc
==SQLITE_OK
);
820 if( p
->mask
&TESTVFS_SHMOPEN_MASK
&& tvfsInjectIoerr(p
) ){
824 /* Search for a TestvfsBuffer. Create a new one if required. */
825 for(pBuffer
=p
->pBuffer
; pBuffer
; pBuffer
=pBuffer
->pNext
){
826 if( 0==strcmp(pFd
->zFilename
, pBuffer
->zFile
) ) break;
829 int szName
= (int)strlen(pFd
->zFilename
);
830 int nByte
= sizeof(TestvfsBuffer
) + szName
+ 1;
831 pBuffer
= (TestvfsBuffer
*)ckalloc(nByte
);
832 memset(pBuffer
, 0, nByte
);
833 pBuffer
->zFile
= (char *)&pBuffer
[1];
834 memcpy(pBuffer
->zFile
, pFd
->zFilename
, szName
+1);
835 pBuffer
->pNext
= p
->pBuffer
;
836 p
->pBuffer
= pBuffer
;
839 /* Connect the TestvfsBuffer to the new TestvfsShm handle and return. */
840 pFd
->pNext
= pBuffer
->pFile
;
841 pBuffer
->pFile
= pFd
;
846 static void tvfsAllocPage(TestvfsBuffer
*p
, int iPage
, int pgsz
){
847 assert( iPage
<TESTVFS_MAX_PAGES
);
848 if( p
->aPage
[iPage
]==0 ){
849 p
->aPage
[iPage
] = (u8
*)ckalloc(pgsz
);
850 memset(p
->aPage
[iPage
], 0, pgsz
);
855 static int tvfsShmMap(
856 sqlite3_file
*pFile
, /* Handle open on database file */
857 int iPage
, /* Page to retrieve */
858 int pgsz
, /* Size of pages */
859 int isWrite
, /* True to extend file if necessary */
860 void volatile **pp
/* OUT: Mapped memory */
863 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
864 Testvfs
*p
= (Testvfs
*)(pFd
->pVfs
->pAppData
);
867 return sqlite3OsShmMap(pFd
->pReal
, iPage
, pgsz
, isWrite
, pp
);
871 rc
= tvfsShmOpen(pFile
);
877 if( p
->pScript
&& p
->mask
&TESTVFS_SHMMAP_MASK
){
878 Tcl_Obj
*pArg
= Tcl_NewObj();
879 Tcl_IncrRefCount(pArg
);
880 Tcl_ListObjAppendElement(p
->interp
, pArg
, Tcl_NewIntObj(iPage
));
881 Tcl_ListObjAppendElement(p
->interp
, pArg
, Tcl_NewIntObj(pgsz
));
882 Tcl_ListObjAppendElement(p
->interp
, pArg
, Tcl_NewIntObj(isWrite
));
883 tvfsExecTcl(p
, "xShmMap",
884 Tcl_NewStringObj(pFd
->pShm
->zFile
, -1), pFd
->pShmId
, pArg
, 0
886 tvfsResultCode(p
, &rc
);
887 Tcl_DecrRefCount(pArg
);
889 if( rc
==SQLITE_OK
&& p
->mask
&TESTVFS_SHMMAP_MASK
&& tvfsInjectIoerr(p
) ){
893 if( rc
==SQLITE_OK
&& isWrite
&& !pFd
->pShm
->aPage
[iPage
] ){
894 tvfsAllocPage(pFd
->pShm
, iPage
, pgsz
);
896 *pp
= (void volatile *)pFd
->pShm
->aPage
[iPage
];
902 static int tvfsShmLock(
909 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
910 Testvfs
*p
= (Testvfs
*)(pFd
->pVfs
->pAppData
);
915 return sqlite3OsShmLock(pFd
->pReal
, ofst
, n
, flags
);
918 if( p
->pScript
&& p
->mask
&TESTVFS_SHMLOCK_MASK
){
919 sqlite3_snprintf(sizeof(zLock
), zLock
, "%d %d", ofst
, n
);
920 nLock
= (int)strlen(zLock
);
921 if( flags
& SQLITE_SHM_LOCK
){
922 strcpy(&zLock
[nLock
], " lock");
924 strcpy(&zLock
[nLock
], " unlock");
926 nLock
+= (int)strlen(&zLock
[nLock
]);
927 if( flags
& SQLITE_SHM_SHARED
){
928 strcpy(&zLock
[nLock
], " shared");
930 strcpy(&zLock
[nLock
], " exclusive");
932 tvfsExecTcl(p
, "xShmLock",
933 Tcl_NewStringObj(pFd
->pShm
->zFile
, -1), pFd
->pShmId
,
934 Tcl_NewStringObj(zLock
, -1), 0
936 tvfsResultCode(p
, &rc
);
939 if( rc
==SQLITE_OK
&& p
->mask
&TESTVFS_SHMLOCK_MASK
&& tvfsInjectIoerr(p
) ){
944 int isLock
= (flags
& SQLITE_SHM_LOCK
);
945 int isExcl
= (flags
& SQLITE_SHM_EXCLUSIVE
);
946 u32 mask
= (((1<<n
)-1) << ofst
);
949 for(p2
=pFd
->pShm
->pFile
; p2
; p2
=p2
->pNext
){
950 if( p2
==pFd
) continue;
951 if( (p2
->excllock
&mask
) || (isExcl
&& p2
->sharedlock
&mask
) ){
957 if( isExcl
) pFd
->excllock
|= mask
;
958 if( !isExcl
) pFd
->sharedlock
|= mask
;
961 if( isExcl
) pFd
->excllock
&= (~mask
);
962 if( !isExcl
) pFd
->sharedlock
&= (~mask
);
969 static void tvfsShmBarrier(sqlite3_file
*pFile
){
970 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
971 Testvfs
*p
= (Testvfs
*)(pFd
->pVfs
->pAppData
);
973 if( p
->pScript
&& p
->mask
&TESTVFS_SHMBARRIER_MASK
){
974 const char *z
= pFd
->pShm
? pFd
->pShm
->zFile
: "";
975 tvfsExecTcl(p
, "xShmBarrier", Tcl_NewStringObj(z
, -1), pFd
->pShmId
, 0, 0);
979 sqlite3OsShmBarrier(pFd
->pReal
);
984 static int tvfsShmUnmap(
989 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
990 Testvfs
*p
= (Testvfs
*)(pFd
->pVfs
->pAppData
);
991 TestvfsBuffer
*pBuffer
= pFd
->pShm
;
995 return sqlite3OsShmUnmap(pFd
->pReal
, deleteFlag
);
998 if( !pBuffer
) return SQLITE_OK
;
999 assert( pFd
->pShmId
&& pFd
->pShm
);
1001 if( p
->pScript
&& p
->mask
&TESTVFS_SHMCLOSE_MASK
){
1002 tvfsExecTcl(p
, "xShmUnmap",
1003 Tcl_NewStringObj(pFd
->pShm
->zFile
, -1), pFd
->pShmId
, 0, 0
1005 tvfsResultCode(p
, &rc
);
1008 for(ppFd
=&pBuffer
->pFile
; *ppFd
!=pFd
; ppFd
=&((*ppFd
)->pNext
));
1009 assert( (*ppFd
)==pFd
);
1013 if( pBuffer
->pFile
==0 ){
1016 for(pp
=&p
->pBuffer
; *pp
!=pBuffer
; pp
=&((*pp
)->pNext
));
1018 for(i
=0; pBuffer
->aPage
[i
]; i
++){
1019 ckfree((char *)pBuffer
->aPage
[i
]);
1021 ckfree((char *)pBuffer
);
1028 static int tvfsFetch(
1029 sqlite3_file
*pFile
,
1030 sqlite3_int64 iOfst
,
1034 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
1035 return sqlite3OsFetch(pFd
->pReal
, iOfst
, iAmt
, pp
);
1038 static int tvfsUnfetch(sqlite3_file
*pFile
, sqlite3_int64 iOfst
, void *p
){
1039 TestvfsFd
*pFd
= tvfsGetFd(pFile
);
1040 return sqlite3OsUnfetch(pFd
->pReal
, iOfst
, p
);
1043 static int SQLITE_TCLAPI
testvfs_obj_cmd(
1047 Tcl_Obj
*CONST objv
[]
1049 Testvfs
*p
= (Testvfs
*)cd
;
1052 CMD_SHM
, CMD_DELETE
, CMD_FILTER
, CMD_IOERR
, CMD_SCRIPT
,
1053 CMD_DEVCHAR
, CMD_SECTORSIZE
, CMD_FULLERR
, CMD_CANTOPENERR
1055 struct TestvfsSubcmd
{
1060 { "delete", CMD_DELETE
},
1061 { "filter", CMD_FILTER
},
1062 { "ioerr", CMD_IOERR
},
1063 { "fullerr", CMD_FULLERR
},
1064 { "cantopenerr", CMD_CANTOPENERR
},
1065 { "script", CMD_SCRIPT
},
1066 { "devchar", CMD_DEVCHAR
},
1067 { "sectorsize", CMD_SECTORSIZE
},
1073 Tcl_WrongNumArgs(interp
, 1, objv
, "SUBCOMMAND ...");
1076 if( Tcl_GetIndexFromObjStruct(
1077 interp
, objv
[1], aSubcmd
, sizeof(aSubcmd
[0]), "subcommand", 0, &i
)
1081 Tcl_ResetResult(interp
);
1083 switch( aSubcmd
[i
].eCmd
){
1087 TestvfsBuffer
*pBuffer
;
1089 if( objc
!=3 && objc
!=4 ){
1090 Tcl_WrongNumArgs(interp
, 2, objv
, "FILE ?VALUE?");
1093 zName
= ckalloc(p
->pParent
->mxPathname
);
1094 rc
= p
->pParent
->xFullPathname(
1095 p
->pParent
, Tcl_GetString(objv
[2]),
1096 p
->pParent
->mxPathname
, zName
1098 if( rc
!=SQLITE_OK
){
1099 Tcl_AppendResult(interp
, "failed to get full path: ",
1100 Tcl_GetString(objv
[2]), 0);
1104 for(pBuffer
=p
->pBuffer
; pBuffer
; pBuffer
=pBuffer
->pNext
){
1105 if( 0==strcmp(pBuffer
->zFile
, zName
) ) break;
1109 Tcl_AppendResult(interp
, "no such file: ", Tcl_GetString(objv
[2]), 0);
1114 u8
*a
= Tcl_GetByteArrayFromObj(objv
[3], &n
);
1115 int pgsz
= pBuffer
->pgsz
;
1116 if( pgsz
==0 ) pgsz
= 65536;
1117 for(i
=0; i
*pgsz
<n
; i
++){
1119 tvfsAllocPage(pBuffer
, i
, pgsz
);
1120 if( n
-i
*pgsz
<pgsz
){
1123 memcpy(pBuffer
->aPage
[i
], &a
[i
*pgsz
], nByte
);
1127 pObj
= Tcl_NewObj();
1128 for(i
=0; pBuffer
->aPage
[i
]; i
++){
1129 int pgsz
= pBuffer
->pgsz
;
1130 if( pgsz
==0 ) pgsz
= 65536;
1131 Tcl_AppendObjToObj(pObj
, Tcl_NewByteArrayObj(pBuffer
->aPage
[i
], pgsz
));
1133 Tcl_SetObjResult(interp
, pObj
);
1137 /* TESTVFS filter METHOD-LIST
1139 ** Activate special processing for those methods contained in the list
1142 static struct VfsMethod
{
1146 { "xShmOpen", TESTVFS_SHMOPEN_MASK
},
1147 { "xShmLock", TESTVFS_SHMLOCK_MASK
},
1148 { "xShmBarrier", TESTVFS_SHMBARRIER_MASK
},
1149 { "xShmUnmap", TESTVFS_SHMCLOSE_MASK
},
1150 { "xShmMap", TESTVFS_SHMMAP_MASK
},
1151 { "xSync", TESTVFS_SYNC_MASK
},
1152 { "xDelete", TESTVFS_DELETE_MASK
},
1153 { "xWrite", TESTVFS_WRITE_MASK
},
1154 { "xRead", TESTVFS_READ_MASK
},
1155 { "xTruncate", TESTVFS_TRUNCATE_MASK
},
1156 { "xOpen", TESTVFS_OPEN_MASK
},
1157 { "xClose", TESTVFS_CLOSE_MASK
},
1158 { "xAccess", TESTVFS_ACCESS_MASK
},
1159 { "xFullPathname", TESTVFS_FULLPATHNAME_MASK
},
1160 { "xUnlock", TESTVFS_UNLOCK_MASK
},
1161 { "xLock", TESTVFS_LOCK_MASK
},
1162 { "xCheckReservedLock", TESTVFS_CKLOCK_MASK
},
1164 Tcl_Obj
**apElem
= 0;
1168 Tcl_WrongNumArgs(interp
, 2, objv
, "LIST");
1171 if( Tcl_ListObjGetElements(interp
, objv
[2], &nElem
, &apElem
) ){
1174 Tcl_ResetResult(interp
);
1175 for(i
=0; i
<nElem
; i
++){
1177 char *zElem
= Tcl_GetString(apElem
[i
]);
1178 for(iMethod
=0; iMethod
<ArraySize(vfsmethod
); iMethod
++){
1179 if( strcmp(zElem
, vfsmethod
[iMethod
].zName
)==0 ){
1180 mask
|= vfsmethod
[iMethod
].mask
;
1184 if( iMethod
==ArraySize(vfsmethod
) ){
1185 Tcl_AppendResult(interp
, "unknown method: ", zElem
, 0);
1194 ** TESTVFS script ?SCRIPT?
1196 ** Query or set the script to be run when filtered VFS events
1203 Tcl_DecrRefCount(p
->pScript
);
1206 Tcl_GetStringFromObj(objv
[2], &nByte
);
1208 p
->pScript
= Tcl_DuplicateObj(objv
[2]);
1209 Tcl_IncrRefCount(p
->pScript
);
1211 }else if( objc
!=2 ){
1212 Tcl_WrongNumArgs(interp
, 2, objv
, "?SCRIPT?");
1216 Tcl_ResetResult(interp
);
1217 if( p
->pScript
) Tcl_SetObjResult(interp
, p
->pScript
);
1223 ** TESTVFS ioerr ?IFAIL PERSIST?
1225 ** Where IFAIL is an integer and PERSIST is boolean.
1227 case CMD_CANTOPENERR
:
1230 TestFaultInject
*pTest
= 0;
1233 switch( aSubcmd
[i
].eCmd
){
1234 case CMD_IOERR
: pTest
= &p
->ioerr_err
; break;
1235 case CMD_FULLERR
: pTest
= &p
->full_err
; break;
1236 case CMD_CANTOPENERR
: pTest
= &p
->cantopen_err
; break;
1239 iRet
= pTest
->nFail
;
1246 if( TCL_OK
!=Tcl_GetIntFromObj(interp
, objv
[2], &iCnt
)
1247 || TCL_OK
!=Tcl_GetBooleanFromObj(interp
, objv
[3], &iPersist
)
1251 pTest
->eFault
= iPersist
?FAULT_INJECT_PERSISTENT
:FAULT_INJECT_TRANSIENT
;
1253 }else if( objc
!=2 ){
1254 Tcl_WrongNumArgs(interp
, 2, objv
, "?CNT PERSIST?");
1257 Tcl_SetObjResult(interp
, Tcl_NewIntObj(iRet
));
1262 Tcl_DeleteCommand(interp
, Tcl_GetString(objv
[0]));
1272 { "atomic", SQLITE_IOCAP_ATOMIC
},
1273 { "atomic512", SQLITE_IOCAP_ATOMIC512
},
1274 { "atomic1k", SQLITE_IOCAP_ATOMIC1K
},
1275 { "atomic2k", SQLITE_IOCAP_ATOMIC2K
},
1276 { "atomic4k", SQLITE_IOCAP_ATOMIC4K
},
1277 { "atomic8k", SQLITE_IOCAP_ATOMIC8K
},
1278 { "atomic16k", SQLITE_IOCAP_ATOMIC16K
},
1279 { "atomic32k", SQLITE_IOCAP_ATOMIC32K
},
1280 { "atomic64k", SQLITE_IOCAP_ATOMIC64K
},
1281 { "sequential", SQLITE_IOCAP_SEQUENTIAL
},
1282 { "safe_append", SQLITE_IOCAP_SAFE_APPEND
},
1283 { "undeletable_when_open", SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
},
1284 { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE
},
1285 { "immutable", SQLITE_IOCAP_IMMUTABLE
},
1292 Tcl_WrongNumArgs(interp
, 2, objv
, "?ATTR-LIST?");
1298 Tcl_Obj
**flags
= 0;
1301 if( Tcl_ListObjGetElements(interp
, objv
[2], &nFlags
, &flags
) ){
1305 for(j
=0; j
<nFlags
; j
++){
1307 if( Tcl_GetIndexFromObjStruct(interp
, flags
[j
], aFlag
,
1308 sizeof(aFlag
[0]), "flag", 0, &idx
)
1312 if( aFlag
[idx
].iValue
<0 && nFlags
>1 ){
1313 Tcl_AppendResult(interp
, "bad flags: ", Tcl_GetString(objv
[2]), 0);
1316 iNew
|= aFlag
[idx
].iValue
;
1319 p
->iDevchar
= iNew
| 0x10000000;
1322 pRet
= Tcl_NewObj();
1323 for(iFlag
=0; iFlag
<sizeof(aFlag
)/sizeof(aFlag
[0]); iFlag
++){
1324 if( p
->iDevchar
& aFlag
[iFlag
].iValue
){
1325 Tcl_ListObjAppendElement(
1326 interp
, pRet
, Tcl_NewStringObj(aFlag
[iFlag
].zName
, -1)
1330 Tcl_SetObjResult(interp
, pRet
);
1335 case CMD_SECTORSIZE
: {
1337 Tcl_WrongNumArgs(interp
, 2, objv
, "?VALUE?");
1342 if( Tcl_GetIntFromObj(interp
, objv
[2], &iNew
) ){
1345 p
->iSectorsize
= iNew
;
1347 Tcl_SetObjResult(interp
, Tcl_NewIntObj(p
->iSectorsize
));
1355 static void SQLITE_TCLAPI
testvfs_obj_del(ClientData cd
){
1356 Testvfs
*p
= (Testvfs
*)cd
;
1357 if( p
->pScript
) Tcl_DecrRefCount(p
->pScript
);
1358 sqlite3_vfs_unregister(p
->pVfs
);
1359 ckfree((char *)p
->pVfs
);
1364 ** Usage: testvfs VFSNAME ?SWITCHES?
1368 ** -noshm BOOLEAN (True to omit shm methods. Default false)
1369 ** -default BOOLEAN (True to make the vfs default. Default false)
1371 ** This command creates two things when it is invoked: an SQLite VFS, and
1372 ** a Tcl command. Both are named VFSNAME. The VFS is installed. It is not
1373 ** installed as the default VFS.
1375 ** The VFS passes all file I/O calls through to the underlying VFS.
1377 ** Whenever the xShmMap method of the VFS
1378 ** is invoked, the SCRIPT is executed as follows:
1380 ** SCRIPT xShmMap FILENAME ID
1382 ** The value returned by the invocation of SCRIPT above is interpreted as
1383 ** an SQLite error code and returned to SQLite. Either a symbolic
1384 ** "SQLITE_OK" or numeric "0" value may be returned.
1386 ** The contents of the shared-memory buffer associated with a given file
1387 ** may be read and set using the following command:
1389 ** VFSNAME shm FILENAME ?NEWVALUE?
1391 ** When the xShmLock method is invoked by SQLite, the following script is
1394 ** SCRIPT xShmLock FILENAME ID LOCK
1396 ** where LOCK is of the form "OFFSET NBYTE lock/unlock shared/exclusive"
1398 static int SQLITE_TCLAPI
testvfs_cmd(
1402 Tcl_Obj
*CONST objv
[]
1404 static sqlite3_vfs tvfs_vfs
= {
1411 tvfsOpen
, /* xOpen */
1412 tvfsDelete
, /* xDelete */
1413 tvfsAccess
, /* xAccess */
1414 tvfsFullPathname
, /* xFullPathname */
1415 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1416 tvfsDlOpen
, /* xDlOpen */
1417 tvfsDlError
, /* xDlError */
1418 tvfsDlSym
, /* xDlSym */
1419 tvfsDlClose
, /* xDlClose */
1425 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
1426 tvfsRandomness
, /* xRandomness */
1427 tvfsSleep
, /* xSleep */
1428 tvfsCurrentTime
, /* xCurrentTime */
1429 0, /* xGetLastError */
1430 0, /* xCurrentTimeInt64 */
1431 0, /* xSetSystemCall */
1432 0, /* xGetSystemCall */
1433 0, /* xNextSystemCall */
1436 Testvfs
*p
; /* New object */
1437 sqlite3_vfs
*pVfs
; /* New VFS */
1439 int nByte
; /* Bytes of space to allocate at p */
1442 int isNoshm
= 0; /* True if -noshm is passed */
1443 int isFullshm
= 0; /* True if -fullshm is passed */
1444 int isDefault
= 0; /* True if -default is passed */
1445 int szOsFile
= 0; /* Value passed to -szosfile */
1446 int mxPathname
= -1; /* Value passed to -mxpathname */
1447 int iVersion
= 3; /* Value passed to -iversion */
1449 if( objc
<2 || 0!=(objc
%2) ) goto bad_args
;
1450 for(i
=2; i
<objc
; i
+= 2){
1453 zSwitch
= Tcl_GetStringFromObj(objv
[i
], &nSwitch
);
1455 if( nSwitch
>2 && 0==strncmp("-noshm", zSwitch
, nSwitch
) ){
1456 if( Tcl_GetBooleanFromObj(interp
, objv
[i
+1], &isNoshm
) ){
1459 if( isNoshm
) isFullshm
= 0;
1461 else if( nSwitch
>2 && 0==strncmp("-default", zSwitch
, nSwitch
) ){
1462 if( Tcl_GetBooleanFromObj(interp
, objv
[i
+1], &isDefault
) ){
1466 else if( nSwitch
>2 && 0==strncmp("-szosfile", zSwitch
, nSwitch
) ){
1467 if( Tcl_GetIntFromObj(interp
, objv
[i
+1], &szOsFile
) ){
1471 else if( nSwitch
>2 && 0==strncmp("-mxpathname", zSwitch
, nSwitch
) ){
1472 if( Tcl_GetIntFromObj(interp
, objv
[i
+1], &mxPathname
) ){
1476 else if( nSwitch
>2 && 0==strncmp("-iversion", zSwitch
, nSwitch
) ){
1477 if( Tcl_GetIntFromObj(interp
, objv
[i
+1], &iVersion
) ){
1481 else if( nSwitch
>2 && 0==strncmp("-fullshm", zSwitch
, nSwitch
) ){
1482 if( Tcl_GetBooleanFromObj(interp
, objv
[i
+1], &isFullshm
) ){
1485 if( isFullshm
) isNoshm
= 0;
1492 if( szOsFile
<sizeof(TestvfsFile
) ){
1493 szOsFile
= sizeof(TestvfsFile
);
1496 zVfs
= Tcl_GetString(objv
[1]);
1497 nByte
= sizeof(Testvfs
) + (int)strlen(zVfs
)+1;
1498 p
= (Testvfs
*)ckalloc(nByte
);
1499 memset(p
, 0, nByte
);
1501 p
->iSectorsize
= -1;
1503 /* Create the new object command before querying SQLite for a default VFS
1504 ** to use for 'real' IO operations. This is because creating the new VFS
1505 ** may delete an existing [testvfs] VFS of the same name. If such a VFS
1506 ** is currently the default, the new [testvfs] may end up calling the
1507 ** methods of a deleted object.
1509 Tcl_CreateObjCommand(interp
, zVfs
, testvfs_obj_cmd
, p
, testvfs_obj_del
);
1510 p
->pParent
= sqlite3_vfs_find(0);
1513 p
->zName
= (char *)&p
[1];
1514 memcpy(p
->zName
, zVfs
, strlen(zVfs
)+1);
1516 pVfs
= (sqlite3_vfs
*)ckalloc(sizeof(sqlite3_vfs
));
1517 memcpy(pVfs
, &tvfs_vfs
, sizeof(sqlite3_vfs
));
1518 pVfs
->pAppData
= (void *)p
;
1519 pVfs
->iVersion
= iVersion
;
1520 pVfs
->zName
= p
->zName
;
1521 pVfs
->mxPathname
= p
->pParent
->mxPathname
;
1522 if( mxPathname
>=0 && mxPathname
<pVfs
->mxPathname
){
1523 pVfs
->mxPathname
= mxPathname
;
1525 pVfs
->szOsFile
= szOsFile
;
1527 p
->isNoshm
= isNoshm
;
1528 p
->isFullshm
= isFullshm
;
1529 p
->mask
= TESTVFS_ALL_MASK
;
1531 sqlite3_vfs_register(pVfs
, isDefault
);
1536 Tcl_WrongNumArgs(interp
, 1, objv
, "VFSNAME ?-noshm BOOL? ?-fullshm BOOL? ?-default BOOL? ?-mxpathname INT? ?-szosfile INT? ?-iversion INT?");
1540 int Sqlitetestvfs_Init(Tcl_Interp
*interp
){
1541 Tcl_CreateObjCommand(interp
, "testvfs", testvfs_cmd
, 0, 0);