sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 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. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 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. |
| 16 | ** |
| 17 | ** testvfs VFSNAME ?OPTIONS? |
| 18 | ** |
| 19 | ** Available options are: |
| 20 | ** |
| 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) |
| 26 | */ |
| 27 | #if SQLITE_TEST /* This file is used for testing only */ |
| 28 | |
| 29 | #include "sqlite3.h" |
| 30 | #include "sqliteInt.h" |
| 31 | #include "tclsqlite.h" |
| 32 | |
| 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; |
| 38 | |
| 39 | /* |
| 40 | ** An open file handle. |
| 41 | */ |
| 42 | struct TestvfsFile { |
| 43 | sqlite3_file base; /* Base class. Must be first */ |
| 44 | TestvfsFd *pFd; /* File data */ |
| 45 | }; |
| 46 | #define tvfsGetFd(pFile) (((TestvfsFile *)pFile)->pFd) |
| 47 | |
| 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 */ |
| 53 | |
| 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 */ |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | #define FAULT_INJECT_NONE 0 |
| 62 | #define FAULT_INJECT_TRANSIENT 1 |
| 63 | #define FAULT_INJECT_PERSISTENT 2 |
| 64 | |
| 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 */ |
| 70 | }; |
| 71 | |
| 72 | /* |
| 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. |
| 76 | */ |
| 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; |
| 86 | |
| 87 | int mask; /* Mask controlling [script] and [ioerr] */ |
| 88 | |
| 89 | TestFaultInject ioerr_err; |
| 90 | TestFaultInject full_err; |
| 91 | TestFaultInject cantopen_err; |
| 92 | |
| 93 | #if 0 |
| 94 | int iIoerrCnt; |
| 95 | int ioerr; |
| 96 | int nIoerrFail; |
| 97 | int iFullCnt; |
| 98 | int fullerr; |
| 99 | int nFullFail; |
| 100 | #endif |
| 101 | |
| 102 | int iDevchar; |
| 103 | int iSectorsize; |
| 104 | }; |
| 105 | |
| 106 | /* |
| 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: |
| 110 | ** |
| 111 | ** + Simulating IO errors, and |
| 112 | ** + Invoking the Tcl callback script. |
| 113 | */ |
| 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 |
| 119 | |
| 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 |
| 132 | #define TESTVFS_FCNTL_MASK 0x00100000 |
| 133 | #define TESTVFS_SLEEP_MASK 0x00200000 |
| 134 | |
| 135 | #define TESTVFS_ALL_MASK 0x003FFFFF |
| 136 | |
| 137 | |
| 138 | #define TESTVFS_MAX_PAGES 1024 |
| 139 | |
| 140 | /* |
| 141 | ** A shared-memory buffer. There is one of these objects for each shared |
| 142 | ** memory region opened by clients. If two clients open the same file, |
| 143 | ** there are two TestvfsFile structures but only one TestvfsBuffer structure. |
| 144 | */ |
| 145 | struct TestvfsBuffer { |
| 146 | char *zFile; /* Associated file name */ |
| 147 | int pgsz; /* Page size */ |
| 148 | u8 *aPage[TESTVFS_MAX_PAGES]; /* Array of ckalloc'd pages */ |
| 149 | TestvfsFd *pFile; /* List of open handles */ |
| 150 | TestvfsBuffer *pNext; /* Next in linked list of all buffers */ |
| 151 | }; |
| 152 | |
| 153 | |
| 154 | #define PARENTVFS(x) (((Testvfs *)((x)->pAppData))->pParent) |
| 155 | |
| 156 | #define TESTVFS_MAX_ARGS 12 |
| 157 | |
| 158 | |
| 159 | /* |
| 160 | ** Method declarations for TestvfsFile. |
| 161 | */ |
| 162 | static int tvfsClose(sqlite3_file*); |
| 163 | static int tvfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 164 | static int tvfsWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); |
| 165 | static int tvfsTruncate(sqlite3_file*, sqlite3_int64 size); |
| 166 | static int tvfsSync(sqlite3_file*, int flags); |
| 167 | static int tvfsFileSize(sqlite3_file*, sqlite3_int64 *pSize); |
| 168 | static int tvfsLock(sqlite3_file*, int); |
| 169 | static int tvfsUnlock(sqlite3_file*, int); |
| 170 | static int tvfsCheckReservedLock(sqlite3_file*, int *); |
| 171 | static int tvfsFileControl(sqlite3_file*, int op, void *pArg); |
| 172 | static int tvfsSectorSize(sqlite3_file*); |
| 173 | static int tvfsDeviceCharacteristics(sqlite3_file*); |
| 174 | |
| 175 | /* |
| 176 | ** Method declarations for tvfs_vfs. |
| 177 | */ |
| 178 | static int tvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
| 179 | static int tvfsDelete(sqlite3_vfs*, const char *zName, int syncDir); |
| 180 | static int tvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
| 181 | static int tvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); |
| 182 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 183 | static void *tvfsDlOpen(sqlite3_vfs*, const char *zFilename); |
| 184 | static void tvfsDlError(sqlite3_vfs*, int nByte, char *zErrMsg); |
| 185 | static void (*tvfsDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void); |
| 186 | static void tvfsDlClose(sqlite3_vfs*, void*); |
| 187 | #endif /* SQLITE_OMIT_LOAD_EXTENSION */ |
| 188 | static int tvfsRandomness(sqlite3_vfs*, int nByte, char *zOut); |
| 189 | static int tvfsSleep(sqlite3_vfs*, int microseconds); |
| 190 | static int tvfsCurrentTime(sqlite3_vfs*, double*); |
| 191 | |
| 192 | static int tvfsShmOpen(sqlite3_file*); |
| 193 | static int tvfsShmLock(sqlite3_file*, int , int, int); |
| 194 | static int tvfsShmMap(sqlite3_file*,int,int,int, void volatile **); |
| 195 | static void tvfsShmBarrier(sqlite3_file*); |
| 196 | static int tvfsShmUnmap(sqlite3_file*, int); |
| 197 | |
| 198 | static int tvfsFetch(sqlite3_file*, sqlite3_int64, int, void**); |
| 199 | static int tvfsUnfetch(sqlite3_file*, sqlite3_int64, void*); |
| 200 | |
| 201 | static sqlite3_io_methods tvfs_io_methods = { |
| 202 | 3, /* iVersion */ |
| 203 | tvfsClose, /* xClose */ |
| 204 | tvfsRead, /* xRead */ |
| 205 | tvfsWrite, /* xWrite */ |
| 206 | tvfsTruncate, /* xTruncate */ |
| 207 | tvfsSync, /* xSync */ |
| 208 | tvfsFileSize, /* xFileSize */ |
| 209 | tvfsLock, /* xLock */ |
| 210 | tvfsUnlock, /* xUnlock */ |
| 211 | tvfsCheckReservedLock, /* xCheckReservedLock */ |
| 212 | tvfsFileControl, /* xFileControl */ |
| 213 | tvfsSectorSize, /* xSectorSize */ |
| 214 | tvfsDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 215 | tvfsShmMap, /* xShmMap */ |
| 216 | tvfsShmLock, /* xShmLock */ |
| 217 | tvfsShmBarrier, /* xShmBarrier */ |
| 218 | tvfsShmUnmap, /* xShmUnmap */ |
| 219 | tvfsFetch, |
| 220 | tvfsUnfetch |
| 221 | }; |
| 222 | |
| 223 | static int tvfsResultCode(Testvfs *p, int *pRc){ |
| 224 | struct errcode { |
| 225 | int eCode; |
| 226 | const char *zCode; |
| 227 | } aCode[] = { |
| 228 | { SQLITE_OK, "SQLITE_OK" }, |
| 229 | { SQLITE_ERROR, "SQLITE_ERROR" }, |
| 230 | { SQLITE_IOERR, "SQLITE_IOERR" }, |
| 231 | { SQLITE_LOCKED, "SQLITE_LOCKED" }, |
| 232 | { SQLITE_BUSY, "SQLITE_BUSY" }, |
| 233 | { SQLITE_READONLY, "SQLITE_READONLY" }, |
| 234 | { SQLITE_READONLY_CANTINIT, "SQLITE_READONLY_CANTINIT" }, |
| 235 | { SQLITE_NOTFOUND, "SQLITE_NOTFOUND" }, |
| 236 | { -1, "SQLITE_OMIT" }, |
| 237 | }; |
| 238 | |
| 239 | const char *z; |
| 240 | int i; |
| 241 | |
| 242 | z = Tcl_GetStringResult(p->interp); |
| 243 | for(i=0; i<ArraySize(aCode); i++){ |
| 244 | if( 0==strcmp(z, aCode[i].zCode) ){ |
| 245 | *pRc = aCode[i].eCode; |
| 246 | return 1; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int tvfsInjectFault(TestFaultInject *p){ |
| 254 | int ret = 0; |
| 255 | if( p->eFault ){ |
| 256 | p->iCnt--; |
| 257 | if( p->iCnt==0 || (p->iCnt<0 && p->eFault==FAULT_INJECT_PERSISTENT ) ){ |
| 258 | ret = 1; |
| 259 | p->nFail++; |
| 260 | } |
| 261 | } |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | static int tvfsInjectIoerr(Testvfs *p){ |
| 267 | return tvfsInjectFault(&p->ioerr_err); |
| 268 | } |
| 269 | |
| 270 | static int tvfsInjectFullerr(Testvfs *p){ |
| 271 | return tvfsInjectFault(&p->full_err); |
| 272 | } |
| 273 | static int tvfsInjectCantopenerr(Testvfs *p){ |
| 274 | return tvfsInjectFault(&p->cantopen_err); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | static void tvfsExecTcl( |
| 279 | Testvfs *p, |
| 280 | const char *zMethod, |
| 281 | Tcl_Obj *arg1, |
| 282 | Tcl_Obj *arg2, |
| 283 | Tcl_Obj *arg3, |
| 284 | Tcl_Obj *arg4 |
| 285 | ){ |
| 286 | int rc; /* Return code from Tcl_EvalObj() */ |
| 287 | Tcl_Obj *pEval; |
| 288 | assert( p->pScript ); |
| 289 | |
| 290 | assert( zMethod ); |
| 291 | assert( p ); |
| 292 | assert( arg2==0 || arg1!=0 ); |
| 293 | assert( arg3==0 || arg2!=0 ); |
| 294 | |
| 295 | pEval = Tcl_DuplicateObj(p->pScript); |
| 296 | Tcl_IncrRefCount(p->pScript); |
| 297 | Tcl_ListObjAppendElement(p->interp, pEval, Tcl_NewStringObj(zMethod, -1)); |
| 298 | if( arg1 ) Tcl_ListObjAppendElement(p->interp, pEval, arg1); |
| 299 | if( arg2 ) Tcl_ListObjAppendElement(p->interp, pEval, arg2); |
| 300 | if( arg3 ) Tcl_ListObjAppendElement(p->interp, pEval, arg3); |
| 301 | if( arg4 ) Tcl_ListObjAppendElement(p->interp, pEval, arg4); |
| 302 | |
| 303 | rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); |
| 304 | if( rc!=TCL_OK ){ |
| 305 | Tcl_BackgroundError(p->interp); |
| 306 | Tcl_ResetResult(p->interp); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | |
| 311 | /* |
| 312 | ** Close an tvfs-file. |
| 313 | */ |
| 314 | static int tvfsClose(sqlite3_file *pFile){ |
| 315 | TestvfsFile *pTestfile = (TestvfsFile *)pFile; |
| 316 | TestvfsFd *pFd = pTestfile->pFd; |
| 317 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 318 | |
| 319 | if( p->pScript && p->mask&TESTVFS_CLOSE_MASK ){ |
| 320 | tvfsExecTcl(p, "xClose", |
| 321 | Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0, 0 |
| 322 | ); |
| 323 | } |
| 324 | |
| 325 | if( pFd->pShmId ){ |
| 326 | Tcl_DecrRefCount(pFd->pShmId); |
| 327 | pFd->pShmId = 0; |
| 328 | } |
| 329 | if( pFile->pMethods ){ |
| 330 | ckfree((char *)pFile->pMethods); |
| 331 | } |
| 332 | sqlite3OsClose(pFd->pReal); |
| 333 | ckfree((char *)pFd); |
| 334 | pTestfile->pFd = 0; |
| 335 | return SQLITE_OK; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | ** Read data from an tvfs-file. |
| 340 | */ |
| 341 | static int tvfsRead( |
| 342 | sqlite3_file *pFile, |
| 343 | void *zBuf, |
| 344 | int iAmt, |
| 345 | sqlite_int64 iOfst |
| 346 | ){ |
| 347 | int rc = SQLITE_OK; |
| 348 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 349 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 350 | if( p->pScript && p->mask&TESTVFS_READ_MASK ){ |
| 351 | tvfsExecTcl(p, "xRead", |
| 352 | Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0, 0 |
| 353 | ); |
| 354 | tvfsResultCode(p, &rc); |
| 355 | } |
| 356 | if( rc==SQLITE_OK && p->mask&TESTVFS_READ_MASK && tvfsInjectIoerr(p) ){ |
| 357 | rc = SQLITE_IOERR; |
| 358 | } |
| 359 | if( rc==SQLITE_OK ){ |
| 360 | rc = sqlite3OsRead(pFd->pReal, zBuf, iAmt, iOfst); |
| 361 | } |
| 362 | return rc; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | ** Write data to an tvfs-file. |
| 367 | */ |
| 368 | static int tvfsWrite( |
| 369 | sqlite3_file *pFile, |
| 370 | const void *zBuf, |
| 371 | int iAmt, |
| 372 | sqlite_int64 iOfst |
| 373 | ){ |
| 374 | int rc = SQLITE_OK; |
| 375 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 376 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 377 | |
| 378 | if( p->pScript && p->mask&TESTVFS_WRITE_MASK ){ |
| 379 | tvfsExecTcl(p, "xWrite", |
| 380 | Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, |
| 381 | Tcl_NewWideIntObj(iOfst), Tcl_NewIntObj(iAmt) |
| 382 | ); |
| 383 | tvfsResultCode(p, &rc); |
| 384 | if( rc<0 ) return SQLITE_OK; |
| 385 | } |
| 386 | |
| 387 | if( rc==SQLITE_OK && tvfsInjectFullerr(p) ){ |
| 388 | rc = SQLITE_FULL; |
| 389 | } |
| 390 | if( rc==SQLITE_OK && p->mask&TESTVFS_WRITE_MASK && tvfsInjectIoerr(p) ){ |
| 391 | rc = SQLITE_IOERR; |
| 392 | } |
| 393 | |
| 394 | if( rc==SQLITE_OK ){ |
| 395 | rc = sqlite3OsWrite(pFd->pReal, zBuf, iAmt, iOfst); |
| 396 | } |
| 397 | return rc; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | ** Truncate an tvfs-file. |
| 402 | */ |
| 403 | static int tvfsTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
| 404 | int rc = SQLITE_OK; |
| 405 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 406 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 407 | |
| 408 | if( p->pScript && p->mask&TESTVFS_TRUNCATE_MASK ){ |
| 409 | tvfsExecTcl(p, "xTruncate", |
| 410 | Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0, 0 |
| 411 | ); |
| 412 | tvfsResultCode(p, &rc); |
| 413 | } |
| 414 | |
| 415 | if( rc==SQLITE_OK ){ |
| 416 | rc = sqlite3OsTruncate(pFd->pReal, size); |
| 417 | } |
| 418 | return rc; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | ** Sync an tvfs-file. |
| 423 | */ |
| 424 | static int tvfsSync(sqlite3_file *pFile, int flags){ |
| 425 | int rc = SQLITE_OK; |
| 426 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 427 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 428 | |
| 429 | if( p->pScript && p->mask&TESTVFS_SYNC_MASK ){ |
| 430 | char *zFlags = 0; |
| 431 | |
| 432 | switch( flags ){ |
| 433 | case SQLITE_SYNC_NORMAL: |
| 434 | zFlags = "normal"; |
| 435 | break; |
| 436 | case SQLITE_SYNC_FULL: |
| 437 | zFlags = "full"; |
| 438 | break; |
| 439 | case SQLITE_SYNC_NORMAL|SQLITE_SYNC_DATAONLY: |
| 440 | zFlags = "normal|dataonly"; |
| 441 | break; |
| 442 | case SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY: |
| 443 | zFlags = "full|dataonly"; |
| 444 | break; |
| 445 | default: |
| 446 | assert(0); |
| 447 | } |
| 448 | |
| 449 | tvfsExecTcl(p, "xSync", |
| 450 | Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, |
| 451 | Tcl_NewStringObj(zFlags, -1), 0 |
| 452 | ); |
| 453 | tvfsResultCode(p, &rc); |
| 454 | } |
| 455 | |
| 456 | if( rc==SQLITE_OK && tvfsInjectFullerr(p) ) rc = SQLITE_FULL; |
| 457 | |
| 458 | if( rc==SQLITE_OK ){ |
| 459 | rc = sqlite3OsSync(pFd->pReal, flags); |
| 460 | } |
| 461 | |
| 462 | return rc; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | ** Return the current file-size of an tvfs-file. |
| 467 | */ |
| 468 | static int tvfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
| 469 | TestvfsFd *p = tvfsGetFd(pFile); |
| 470 | return sqlite3OsFileSize(p->pReal, pSize); |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | ** Lock an tvfs-file. |
| 475 | */ |
| 476 | static int tvfsLock(sqlite3_file *pFile, int eLock){ |
| 477 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 478 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 479 | if( p->pScript && p->mask&TESTVFS_LOCK_MASK ){ |
| 480 | char zLock[30]; |
| 481 | sqlite3_snprintf(sizeof(zLock),zLock,"%d",eLock); |
| 482 | tvfsExecTcl(p, "xLock", Tcl_NewStringObj(pFd->zFilename, -1), |
| 483 | Tcl_NewStringObj(zLock, -1), 0, 0); |
| 484 | } |
| 485 | if( p->mask&TESTVFS_LOCK_MASK && tvfsInjectIoerr(p) ){ |
| 486 | return SQLITE_IOERR_LOCK; |
| 487 | } |
| 488 | return sqlite3OsLock(pFd->pReal, eLock); |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | ** Unlock an tvfs-file. |
| 493 | */ |
| 494 | static int tvfsUnlock(sqlite3_file *pFile, int eLock){ |
| 495 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 496 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 497 | if( p->pScript && p->mask&TESTVFS_UNLOCK_MASK ){ |
| 498 | char zLock[30]; |
| 499 | sqlite3_snprintf(sizeof(zLock),zLock,"%d",eLock); |
| 500 | tvfsExecTcl(p, "xUnlock", Tcl_NewStringObj(pFd->zFilename, -1), |
| 501 | Tcl_NewStringObj(zLock, -1), 0, 0); |
| 502 | } |
| 503 | if( p->mask&TESTVFS_UNLOCK_MASK && tvfsInjectIoerr(p) ){ |
| 504 | return SQLITE_IOERR_UNLOCK; |
| 505 | } |
| 506 | return sqlite3OsUnlock(pFd->pReal, eLock); |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | ** Check if another file-handle holds a RESERVED lock on an tvfs-file. |
| 511 | */ |
| 512 | static int tvfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 513 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 514 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 515 | if( p->pScript && p->mask&TESTVFS_CKLOCK_MASK ){ |
| 516 | tvfsExecTcl(p, "xCheckReservedLock", Tcl_NewStringObj(pFd->zFilename, -1), |
| 517 | 0, 0, 0); |
| 518 | } |
| 519 | return sqlite3OsCheckReservedLock(pFd->pReal, pResOut); |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | ** File control method. For custom operations on an tvfs-file. |
| 524 | */ |
| 525 | static int tvfsFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 526 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 527 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 528 | if( op==SQLITE_FCNTL_PRAGMA ){ |
| 529 | char **argv = (char**)pArg; |
| 530 | if( sqlite3_stricmp(argv[1],"error")==0 ){ |
| 531 | int rc = SQLITE_ERROR; |
| 532 | if( argv[2] ){ |
| 533 | const char *z = argv[2]; |
| 534 | int x = atoi(z); |
| 535 | if( x ){ |
| 536 | rc = x; |
| 537 | while( sqlite3Isdigit(z[0]) ){ z++; } |
| 538 | while( sqlite3Isspace(z[0]) ){ z++; } |
| 539 | } |
| 540 | if( z[0] ) argv[0] = sqlite3_mprintf("%s", z); |
| 541 | } |
| 542 | return rc; |
| 543 | } |
| 544 | if( sqlite3_stricmp(argv[1], "filename")==0 ){ |
| 545 | argv[0] = sqlite3_mprintf("%s", pFd->zFilename); |
| 546 | return SQLITE_OK; |
| 547 | } |
| 548 | } |
| 549 | if( p->pScript && (p->mask&TESTVFS_FCNTL_MASK) ){ |
| 550 | struct Fcntl { |
| 551 | int iFnctl; |
| 552 | const char *zFnctl; |
| 553 | } aF[] = { |
| 554 | { SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, "BEGIN_ATOMIC_WRITE" }, |
| 555 | { SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, "COMMIT_ATOMIC_WRITE" }, |
| 556 | { SQLITE_FCNTL_ZIPVFS, "ZIPVFS" }, |
| 557 | { SQLITE_FCNTL_LOCK_TIMEOUT, "LOCK_TIMEOUT" }, |
| 558 | }; |
| 559 | int i; |
| 560 | for(i=0; i<sizeof(aF)/sizeof(aF[0]); i++){ |
| 561 | if( op==aF[i].iFnctl ) break; |
| 562 | } |
| 563 | if( i<sizeof(aF)/sizeof(aF[0]) ){ |
| 564 | int rc = 0; |
| 565 | tvfsExecTcl(p, "xFileControl", |
| 566 | Tcl_NewStringObj(pFd->zFilename, -1), |
| 567 | Tcl_NewStringObj(aF[i].zFnctl, -1), |
| 568 | 0, 0 |
| 569 | ); |
| 570 | tvfsResultCode(p, &rc); |
| 571 | if( rc ) return (rc<0 ? SQLITE_OK : rc); |
| 572 | } |
| 573 | } |
| 574 | return sqlite3OsFileControl(pFd->pReal, op, pArg); |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | ** Return the sector-size in bytes for an tvfs-file. |
| 579 | */ |
| 580 | static int tvfsSectorSize(sqlite3_file *pFile){ |
| 581 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 582 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 583 | if( p->iSectorsize>=0 ){ |
| 584 | return p->iSectorsize; |
| 585 | } |
| 586 | return sqlite3OsSectorSize(pFd->pReal); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | ** Return the device characteristic flags supported by an tvfs-file. |
| 591 | */ |
| 592 | static int tvfsDeviceCharacteristics(sqlite3_file *pFile){ |
| 593 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 594 | Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; |
| 595 | if( p->iDevchar>=0 ){ |
| 596 | return p->iDevchar; |
| 597 | } |
| 598 | return sqlite3OsDeviceCharacteristics(pFd->pReal); |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | ** Open an tvfs file handle. |
| 603 | */ |
| 604 | static int tvfsOpen( |
| 605 | sqlite3_vfs *pVfs, |
| 606 | const char *zName, |
| 607 | sqlite3_file *pFile, |
| 608 | int flags, |
| 609 | int *pOutFlags |
| 610 | ){ |
| 611 | int rc; |
| 612 | TestvfsFile *pTestfile = (TestvfsFile *)pFile; |
| 613 | TestvfsFd *pFd; |
| 614 | Tcl_Obj *pId = 0; |
| 615 | Testvfs *p = (Testvfs *)pVfs->pAppData; |
| 616 | |
| 617 | pFd = (TestvfsFd *)ckalloc(sizeof(TestvfsFd) + PARENTVFS(pVfs)->szOsFile); |
| 618 | memset(pFd, 0, sizeof(TestvfsFd) + PARENTVFS(pVfs)->szOsFile); |
| 619 | pFd->pShm = 0; |
| 620 | pFd->pShmId = 0; |
| 621 | pFd->zFilename = zName; |
| 622 | pFd->pVfs = pVfs; |
| 623 | pFd->pReal = (sqlite3_file *)&pFd[1]; |
| 624 | memset(pTestfile, 0, sizeof(TestvfsFile)); |
| 625 | pTestfile->pFd = pFd; |
| 626 | |
| 627 | /* Evaluate the Tcl script: |
| 628 | ** |
| 629 | ** SCRIPT xOpen FILENAME KEY-VALUE-ARGS |
| 630 | ** |
| 631 | ** If the script returns an SQLite error code other than SQLITE_OK, an |
| 632 | ** error is returned to the caller. If it returns SQLITE_OK, the new |
| 633 | ** connection is named "anon". Otherwise, the value returned by the |
| 634 | ** script is used as the connection name. |
| 635 | */ |
| 636 | Tcl_ResetResult(p->interp); |
| 637 | if( p->pScript && p->mask&TESTVFS_OPEN_MASK ){ |
| 638 | Tcl_Obj *pArg = Tcl_NewObj(); |
| 639 | Tcl_IncrRefCount(pArg); |
| 640 | if( flags&SQLITE_OPEN_MAIN_DB ){ |
| 641 | const char *z = &zName[strlen(zName)+1]; |
| 642 | while( *z ){ |
| 643 | Tcl_ListObjAppendElement(0, pArg, Tcl_NewStringObj(z, -1)); |
| 644 | z += strlen(z) + 1; |
| 645 | Tcl_ListObjAppendElement(0, pArg, Tcl_NewStringObj(z, -1)); |
| 646 | z += strlen(z) + 1; |
| 647 | } |
| 648 | } |
| 649 | tvfsExecTcl(p, "xOpen", Tcl_NewStringObj(pFd->zFilename, -1), pArg, 0, 0); |
| 650 | Tcl_DecrRefCount(pArg); |
| 651 | if( tvfsResultCode(p, &rc) ){ |
| 652 | if( rc!=SQLITE_OK ) return rc; |
| 653 | }else{ |
| 654 | pId = Tcl_GetObjResult(p->interp); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | if( (p->mask&TESTVFS_OPEN_MASK) && tvfsInjectIoerr(p) ) return SQLITE_IOERR; |
| 659 | if( tvfsInjectCantopenerr(p) ) return SQLITE_CANTOPEN; |
| 660 | if( tvfsInjectFullerr(p) ) return SQLITE_FULL; |
| 661 | |
| 662 | if( !pId ){ |
| 663 | pId = Tcl_NewStringObj("anon", -1); |
| 664 | } |
| 665 | Tcl_IncrRefCount(pId); |
| 666 | pFd->pShmId = pId; |
| 667 | Tcl_ResetResult(p->interp); |
| 668 | |
| 669 | rc = sqlite3OsOpen(PARENTVFS(pVfs), zName, pFd->pReal, flags, pOutFlags); |
| 670 | if( pFd->pReal->pMethods ){ |
| 671 | sqlite3_io_methods *pMethods; |
| 672 | int nByte; |
| 673 | |
| 674 | if( pVfs->iVersion>1 ){ |
| 675 | nByte = sizeof(sqlite3_io_methods); |
| 676 | }else{ |
| 677 | nByte = offsetof(sqlite3_io_methods, xShmMap); |
| 678 | } |
| 679 | |
| 680 | pMethods = (sqlite3_io_methods *)ckalloc(nByte); |
| 681 | memcpy(pMethods, &tvfs_io_methods, nByte); |
| 682 | pMethods->iVersion = pFd->pReal->pMethods->iVersion; |
| 683 | if( pMethods->iVersion>pVfs->iVersion ){ |
| 684 | pMethods->iVersion = pVfs->iVersion; |
| 685 | } |
| 686 | if( pVfs->iVersion>1 && ((Testvfs *)pVfs->pAppData)->isNoshm ){ |
| 687 | pMethods->xShmUnmap = 0; |
| 688 | pMethods->xShmLock = 0; |
| 689 | pMethods->xShmBarrier = 0; |
| 690 | pMethods->xShmMap = 0; |
| 691 | } |
| 692 | pFile->pMethods = pMethods; |
| 693 | } |
| 694 | |
| 695 | return rc; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | ** Delete the file located at zPath. If the dirSync argument is true, |
| 700 | ** ensure the file-system modifications are synced to disk before |
| 701 | ** returning. |
| 702 | */ |
| 703 | static int tvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 704 | int rc = SQLITE_OK; |
| 705 | Testvfs *p = (Testvfs *)pVfs->pAppData; |
| 706 | |
| 707 | if( p->pScript && p->mask&TESTVFS_DELETE_MASK ){ |
| 708 | tvfsExecTcl(p, "xDelete", |
| 709 | Tcl_NewStringObj(zPath, -1), Tcl_NewIntObj(dirSync), 0, 0 |
| 710 | ); |
| 711 | tvfsResultCode(p, &rc); |
| 712 | } |
| 713 | if( rc==SQLITE_OK ){ |
| 714 | rc = sqlite3OsDelete(PARENTVFS(pVfs), zPath, dirSync); |
| 715 | } |
| 716 | return rc; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | ** Test for access permissions. Return true if the requested permission |
| 721 | ** is available, or false otherwise. |
| 722 | */ |
| 723 | static int tvfsAccess( |
| 724 | sqlite3_vfs *pVfs, |
| 725 | const char *zPath, |
| 726 | int flags, |
| 727 | int *pResOut |
| 728 | ){ |
| 729 | Testvfs *p = (Testvfs *)pVfs->pAppData; |
| 730 | if( p->pScript && p->mask&TESTVFS_ACCESS_MASK ){ |
| 731 | int rc; |
| 732 | char *zArg = 0; |
| 733 | if( flags==SQLITE_ACCESS_EXISTS ) zArg = "SQLITE_ACCESS_EXISTS"; |
| 734 | if( flags==SQLITE_ACCESS_READWRITE ) zArg = "SQLITE_ACCESS_READWRITE"; |
| 735 | if( flags==SQLITE_ACCESS_READ ) zArg = "SQLITE_ACCESS_READ"; |
| 736 | tvfsExecTcl(p, "xAccess", |
| 737 | Tcl_NewStringObj(zPath, -1), Tcl_NewStringObj(zArg, -1), 0, 0 |
| 738 | ); |
| 739 | if( tvfsResultCode(p, &rc) ){ |
| 740 | if( rc!=SQLITE_OK ) return rc; |
| 741 | }else{ |
| 742 | Tcl_Interp *interp = p->interp; |
| 743 | if( TCL_OK==Tcl_GetBooleanFromObj(0, Tcl_GetObjResult(interp), pResOut) ){ |
| 744 | return SQLITE_OK; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | return sqlite3OsAccess(PARENTVFS(pVfs), zPath, flags, pResOut); |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | ** Populate buffer zOut with the full canonical pathname corresponding |
| 753 | ** to the pathname in zPath. zOut is guaranteed to point to a buffer |
| 754 | ** of at least (DEVSYM_MAX_PATHNAME+1) bytes. |
| 755 | */ |
| 756 | static int tvfsFullPathname( |
| 757 | sqlite3_vfs *pVfs, |
| 758 | const char *zPath, |
| 759 | int nOut, |
| 760 | char *zOut |
| 761 | ){ |
| 762 | Testvfs *p = (Testvfs *)pVfs->pAppData; |
| 763 | if( p->pScript && p->mask&TESTVFS_FULLPATHNAME_MASK ){ |
| 764 | int rc; |
| 765 | tvfsExecTcl(p, "xFullPathname", Tcl_NewStringObj(zPath, -1), 0, 0, 0); |
| 766 | if( tvfsResultCode(p, &rc) ){ |
| 767 | if( rc!=SQLITE_OK ) return rc; |
| 768 | } |
| 769 | } |
| 770 | return sqlite3OsFullPathname(PARENTVFS(pVfs), zPath, nOut, zOut); |
| 771 | } |
| 772 | |
| 773 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 774 | /* |
| 775 | ** Open the dynamic library located at zPath and return a handle. |
| 776 | */ |
| 777 | static void *tvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 778 | return sqlite3OsDlOpen(PARENTVFS(pVfs), zPath); |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | ** Populate the buffer zErrMsg (size nByte bytes) with a human readable |
| 783 | ** utf-8 string describing the most recent error encountered associated |
| 784 | ** with dynamic libraries. |
| 785 | */ |
| 786 | static void tvfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ |
| 787 | sqlite3OsDlError(PARENTVFS(pVfs), nByte, zErrMsg); |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. |
| 792 | */ |
| 793 | static void (*tvfsDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ |
| 794 | return sqlite3OsDlSym(PARENTVFS(pVfs), p, zSym); |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | ** Close the dynamic library handle pHandle. |
| 799 | */ |
| 800 | static void tvfsDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 801 | sqlite3OsDlClose(PARENTVFS(pVfs), pHandle); |
| 802 | } |
| 803 | #endif /* SQLITE_OMIT_LOAD_EXTENSION */ |
| 804 | |
| 805 | /* |
| 806 | ** Populate the buffer pointed to by zBufOut with nByte bytes of |
| 807 | ** random data. |
| 808 | */ |
| 809 | static int tvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 810 | return sqlite3OsRandomness(PARENTVFS(pVfs), nByte, zBufOut); |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | ** Sleep for nMicro microseconds. Return the number of microseconds |
| 815 | ** actually slept. |
| 816 | */ |
| 817 | static int tvfsSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 818 | Testvfs *p = (Testvfs *)pVfs->pAppData; |
| 819 | if( p->pScript && (p->mask&TESTVFS_SLEEP_MASK) ){ |
| 820 | tvfsExecTcl(p, "xSleep", Tcl_NewIntObj(nMicro), 0, 0, 0); |
| 821 | } |
| 822 | return sqlite3OsSleep(PARENTVFS(pVfs), nMicro); |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | ** Return the current time as a Julian Day number in *pTimeOut. |
| 827 | */ |
| 828 | static int tvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 829 | return PARENTVFS(pVfs)->xCurrentTime(PARENTVFS(pVfs), pTimeOut); |
| 830 | } |
| 831 | |
| 832 | static int tvfsShmOpen(sqlite3_file *pFile){ |
| 833 | Testvfs *p; |
| 834 | int rc = SQLITE_OK; /* Return code */ |
| 835 | TestvfsBuffer *pBuffer; /* Buffer to open connection to */ |
| 836 | TestvfsFd *pFd; /* The testvfs file structure */ |
| 837 | |
| 838 | pFd = tvfsGetFd(pFile); |
| 839 | p = (Testvfs *)pFd->pVfs->pAppData; |
| 840 | assert( 0==p->isFullshm ); |
| 841 | assert( pFd->pShmId && pFd->pShm==0 && pFd->pNext==0 ); |
| 842 | |
| 843 | /* Evaluate the Tcl script: |
| 844 | ** |
| 845 | ** SCRIPT xShmOpen FILENAME |
| 846 | */ |
| 847 | Tcl_ResetResult(p->interp); |
| 848 | if( p->pScript && p->mask&TESTVFS_SHMOPEN_MASK ){ |
| 849 | tvfsExecTcl(p, "xShmOpen", Tcl_NewStringObj(pFd->zFilename, -1), 0, 0, 0); |
| 850 | if( tvfsResultCode(p, &rc) ){ |
| 851 | if( rc!=SQLITE_OK ) return rc; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | assert( rc==SQLITE_OK ); |
| 856 | if( p->mask&TESTVFS_SHMOPEN_MASK && tvfsInjectIoerr(p) ){ |
| 857 | return SQLITE_IOERR; |
| 858 | } |
| 859 | |
| 860 | /* Search for a TestvfsBuffer. Create a new one if required. */ |
| 861 | for(pBuffer=p->pBuffer; pBuffer; pBuffer=pBuffer->pNext){ |
| 862 | if( 0==strcmp(pFd->zFilename, pBuffer->zFile) ) break; |
| 863 | } |
| 864 | if( !pBuffer ){ |
| 865 | int szName = (int)strlen(pFd->zFilename); |
| 866 | int nByte = sizeof(TestvfsBuffer) + szName + 1; |
| 867 | pBuffer = (TestvfsBuffer *)ckalloc(nByte); |
| 868 | memset(pBuffer, 0, nByte); |
| 869 | pBuffer->zFile = (char *)&pBuffer[1]; |
| 870 | memcpy(pBuffer->zFile, pFd->zFilename, szName+1); |
| 871 | pBuffer->pNext = p->pBuffer; |
| 872 | p->pBuffer = pBuffer; |
| 873 | } |
| 874 | |
| 875 | /* Connect the TestvfsBuffer to the new TestvfsShm handle and return. */ |
| 876 | pFd->pNext = pBuffer->pFile; |
| 877 | pBuffer->pFile = pFd; |
| 878 | pFd->pShm = pBuffer; |
| 879 | return rc; |
| 880 | } |
| 881 | |
| 882 | static void tvfsAllocPage(TestvfsBuffer *p, int iPage, int pgsz){ |
| 883 | assert( iPage<TESTVFS_MAX_PAGES ); |
| 884 | if( p->aPage[iPage]==0 ){ |
| 885 | p->aPage[iPage] = (u8 *)ckalloc(pgsz); |
| 886 | memset(p->aPage[iPage], 0, pgsz); |
| 887 | p->pgsz = pgsz; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | static int tvfsShmMap( |
| 892 | sqlite3_file *pFile, /* Handle open on database file */ |
| 893 | int iPage, /* Page to retrieve */ |
| 894 | int pgsz, /* Size of pages */ |
| 895 | int isWrite, /* True to extend file if necessary */ |
| 896 | void volatile **pp /* OUT: Mapped memory */ |
| 897 | ){ |
| 898 | int rc = SQLITE_OK; |
| 899 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 900 | Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData); |
| 901 | |
| 902 | if( p->isFullshm ){ |
| 903 | sqlite3_file *pReal = pFd->pReal; |
| 904 | return pReal->pMethods->xShmMap(pReal, iPage, pgsz, isWrite, pp); |
| 905 | } |
| 906 | |
| 907 | if( 0==pFd->pShm ){ |
| 908 | rc = tvfsShmOpen(pFile); |
| 909 | if( rc!=SQLITE_OK ){ |
| 910 | return rc; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | if( p->pScript && p->mask&TESTVFS_SHMMAP_MASK ){ |
| 915 | Tcl_Obj *pArg = Tcl_NewObj(); |
| 916 | Tcl_IncrRefCount(pArg); |
| 917 | Tcl_ListObjAppendElement(p->interp, pArg, Tcl_NewIntObj(iPage)); |
| 918 | Tcl_ListObjAppendElement(p->interp, pArg, Tcl_NewIntObj(pgsz)); |
| 919 | Tcl_ListObjAppendElement(p->interp, pArg, Tcl_NewIntObj(isWrite)); |
| 920 | tvfsExecTcl(p, "xShmMap", |
| 921 | Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, pArg, 0 |
| 922 | ); |
| 923 | tvfsResultCode(p, &rc); |
| 924 | Tcl_DecrRefCount(pArg); |
| 925 | } |
| 926 | if( rc==SQLITE_OK && p->mask&TESTVFS_SHMMAP_MASK && tvfsInjectIoerr(p) ){ |
| 927 | rc = SQLITE_IOERR; |
| 928 | } |
| 929 | |
| 930 | if( rc==SQLITE_OK && isWrite && !pFd->pShm->aPage[iPage] ){ |
| 931 | tvfsAllocPage(pFd->pShm, iPage, pgsz); |
| 932 | } |
| 933 | if( rc==SQLITE_OK || rc==SQLITE_READONLY ){ |
| 934 | *pp = (void volatile *)pFd->pShm->aPage[iPage]; |
| 935 | } |
| 936 | |
| 937 | return rc; |
| 938 | } |
| 939 | |
| 940 | |
| 941 | static int tvfsShmLock( |
| 942 | sqlite3_file *pFile, |
| 943 | int ofst, |
| 944 | int n, |
| 945 | int flags |
| 946 | ){ |
| 947 | int rc = SQLITE_OK; |
| 948 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 949 | Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData); |
| 950 | int nLock; |
| 951 | char zLock[80]; |
| 952 | |
| 953 | if( p->isFullshm ){ |
| 954 | sqlite3_file *pReal = pFd->pReal; |
| 955 | return pReal->pMethods->xShmLock(pReal, ofst, n, flags); |
| 956 | } |
| 957 | |
| 958 | if( p->pScript && p->mask&TESTVFS_SHMLOCK_MASK ){ |
| 959 | sqlite3_snprintf(sizeof(zLock), zLock, "%d %d", ofst, n); |
| 960 | nLock = (int)strlen(zLock); |
| 961 | if( flags & SQLITE_SHM_LOCK ){ |
| 962 | strcpy(&zLock[nLock], " lock"); |
| 963 | }else{ |
| 964 | strcpy(&zLock[nLock], " unlock"); |
| 965 | } |
| 966 | nLock += (int)strlen(&zLock[nLock]); |
| 967 | if( flags & SQLITE_SHM_SHARED ){ |
| 968 | strcpy(&zLock[nLock], " shared"); |
| 969 | }else{ |
| 970 | strcpy(&zLock[nLock], " exclusive"); |
| 971 | } |
| 972 | tvfsExecTcl(p, "xShmLock", |
| 973 | Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, |
| 974 | Tcl_NewStringObj(zLock, -1), 0 |
| 975 | ); |
| 976 | tvfsResultCode(p, &rc); |
| 977 | } |
| 978 | |
| 979 | if( rc==SQLITE_OK && p->mask&TESTVFS_SHMLOCK_MASK && tvfsInjectIoerr(p) ){ |
| 980 | rc = SQLITE_IOERR; |
| 981 | } |
| 982 | |
| 983 | if( rc==SQLITE_OK ){ |
| 984 | int isLock = (flags & SQLITE_SHM_LOCK); |
| 985 | int isExcl = (flags & SQLITE_SHM_EXCLUSIVE); |
| 986 | u32 mask = (((1<<n)-1) << ofst); |
| 987 | if( isLock ){ |
| 988 | TestvfsFd *p2; |
| 989 | for(p2=pFd->pShm->pFile; p2; p2=p2->pNext){ |
| 990 | if( p2==pFd ) continue; |
| 991 | if( (p2->excllock&mask) || (isExcl && p2->sharedlock&mask) ){ |
| 992 | rc = SQLITE_BUSY; |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | if( rc==SQLITE_OK ){ |
| 997 | if( isExcl ) pFd->excllock |= mask; |
| 998 | if( !isExcl ) pFd->sharedlock |= mask; |
| 999 | } |
| 1000 | }else{ |
| 1001 | if( isExcl ) pFd->excllock &= (~mask); |
| 1002 | if( !isExcl ) pFd->sharedlock &= (~mask); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | return rc; |
| 1007 | } |
| 1008 | |
| 1009 | static void tvfsShmBarrier(sqlite3_file *pFile){ |
| 1010 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 1011 | Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData); |
| 1012 | |
| 1013 | if( p->pScript && p->mask&TESTVFS_SHMBARRIER_MASK ){ |
| 1014 | const char *z = pFd->pShm ? pFd->pShm->zFile : ""; |
| 1015 | tvfsExecTcl(p, "xShmBarrier", Tcl_NewStringObj(z, -1), pFd->pShmId, 0, 0); |
| 1016 | } |
| 1017 | |
| 1018 | if( p->isFullshm ){ |
| 1019 | sqlite3_file *pReal = pFd->pReal; |
| 1020 | pReal->pMethods->xShmBarrier(pReal); |
| 1021 | return; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | static int tvfsShmUnmap( |
| 1026 | sqlite3_file *pFile, |
| 1027 | int deleteFlag |
| 1028 | ){ |
| 1029 | int rc = SQLITE_OK; |
| 1030 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 1031 | Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData); |
| 1032 | TestvfsBuffer *pBuffer = pFd->pShm; |
| 1033 | TestvfsFd **ppFd; |
| 1034 | |
| 1035 | if( p->isFullshm ){ |
| 1036 | sqlite3_file *pReal = pFd->pReal; |
| 1037 | return pReal->pMethods->xShmUnmap(pReal, deleteFlag); |
| 1038 | } |
| 1039 | |
| 1040 | if( !pBuffer ) return SQLITE_OK; |
| 1041 | assert( pFd->pShmId && pFd->pShm ); |
| 1042 | |
| 1043 | if( p->pScript && p->mask&TESTVFS_SHMCLOSE_MASK ){ |
| 1044 | tvfsExecTcl(p, "xShmUnmap", |
| 1045 | Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0, 0 |
| 1046 | ); |
| 1047 | tvfsResultCode(p, &rc); |
| 1048 | } |
| 1049 | |
| 1050 | for(ppFd=&pBuffer->pFile; *ppFd!=pFd; ppFd=&((*ppFd)->pNext)); |
| 1051 | assert( (*ppFd)==pFd ); |
| 1052 | *ppFd = pFd->pNext; |
| 1053 | pFd->pNext = 0; |
| 1054 | |
| 1055 | if( pBuffer->pFile==0 ){ |
| 1056 | int i; |
| 1057 | TestvfsBuffer **pp; |
| 1058 | for(pp=&p->pBuffer; *pp!=pBuffer; pp=&((*pp)->pNext)); |
| 1059 | *pp = (*pp)->pNext; |
| 1060 | for(i=0; pBuffer->aPage[i]; i++){ |
| 1061 | ckfree((char *)pBuffer->aPage[i]); |
| 1062 | } |
| 1063 | ckfree((char *)pBuffer); |
| 1064 | } |
| 1065 | pFd->pShm = 0; |
| 1066 | |
| 1067 | return rc; |
| 1068 | } |
| 1069 | |
| 1070 | static int tvfsFetch( |
| 1071 | sqlite3_file *pFile, |
| 1072 | sqlite3_int64 iOfst, |
| 1073 | int iAmt, |
| 1074 | void **pp |
| 1075 | ){ |
| 1076 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 1077 | return sqlite3OsFetch(pFd->pReal, iOfst, iAmt, pp); |
| 1078 | } |
| 1079 | |
| 1080 | static int tvfsUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *p){ |
| 1081 | TestvfsFd *pFd = tvfsGetFd(pFile); |
| 1082 | return sqlite3OsUnfetch(pFd->pReal, iOfst, p); |
| 1083 | } |
| 1084 | |
| 1085 | static int SQLITE_TCLAPI testvfs_obj_cmd( |
| 1086 | ClientData cd, |
| 1087 | Tcl_Interp *interp, |
| 1088 | int objc, |
| 1089 | Tcl_Obj *CONST objv[] |
| 1090 | ){ |
| 1091 | Testvfs *p = (Testvfs *)cd; |
| 1092 | |
| 1093 | enum DB_enum { |
| 1094 | CMD_SHM, CMD_DELETE, CMD_FILTER, CMD_IOERR, CMD_SCRIPT, |
| 1095 | CMD_DEVCHAR, CMD_SECTORSIZE, CMD_FULLERR, CMD_CANTOPENERR |
| 1096 | }; |
| 1097 | struct TestvfsSubcmd { |
| 1098 | char *zName; |
| 1099 | enum DB_enum eCmd; |
| 1100 | } aSubcmd[] = { |
| 1101 | { "shm", CMD_SHM }, |
| 1102 | { "delete", CMD_DELETE }, |
| 1103 | { "filter", CMD_FILTER }, |
| 1104 | { "ioerr", CMD_IOERR }, |
| 1105 | { "fullerr", CMD_FULLERR }, |
| 1106 | { "cantopenerr", CMD_CANTOPENERR }, |
| 1107 | { "script", CMD_SCRIPT }, |
| 1108 | { "devchar", CMD_DEVCHAR }, |
| 1109 | { "sectorsize", CMD_SECTORSIZE }, |
| 1110 | { 0, 0 } |
| 1111 | }; |
| 1112 | int i; |
| 1113 | |
| 1114 | if( objc<2 ){ |
| 1115 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
| 1116 | return TCL_ERROR; |
| 1117 | } |
| 1118 | if( Tcl_GetIndexFromObjStruct( |
| 1119 | interp, objv[1], aSubcmd, sizeof(aSubcmd[0]), "subcommand", 0, &i) |
| 1120 | ){ |
| 1121 | return TCL_ERROR; |
| 1122 | } |
| 1123 | Tcl_ResetResult(interp); |
| 1124 | |
| 1125 | switch( aSubcmd[i].eCmd ){ |
| 1126 | case CMD_SHM: { |
| 1127 | Tcl_Obj *pObj; |
| 1128 | int rc; |
| 1129 | TestvfsBuffer *pBuffer; |
| 1130 | char *zName; |
| 1131 | if( objc!=3 && objc!=4 ){ |
| 1132 | Tcl_WrongNumArgs(interp, 2, objv, "FILE ?VALUE?"); |
| 1133 | return TCL_ERROR; |
| 1134 | } |
| 1135 | zName = ckalloc(p->pParent->mxPathname); |
| 1136 | rc = p->pParent->xFullPathname( |
| 1137 | p->pParent, Tcl_GetString(objv[2]), |
| 1138 | p->pParent->mxPathname, zName |
| 1139 | ); |
| 1140 | if( rc!=SQLITE_OK ){ |
| 1141 | Tcl_AppendResult(interp, "failed to get full path: ", |
| 1142 | Tcl_GetString(objv[2]), NULL); |
| 1143 | ckfree(zName); |
| 1144 | return TCL_ERROR; |
| 1145 | } |
| 1146 | for(pBuffer=p->pBuffer; pBuffer; pBuffer=pBuffer->pNext){ |
| 1147 | if( 0==strcmp(pBuffer->zFile, zName) ) break; |
| 1148 | } |
| 1149 | ckfree(zName); |
| 1150 | if( !pBuffer ){ |
| 1151 | Tcl_AppendResult(interp, "no such file: ", Tcl_GetString(objv[2]), NULL); |
| 1152 | return TCL_ERROR; |
| 1153 | } |
| 1154 | if( objc==4 ){ |
| 1155 | Tcl_Size n; |
| 1156 | u8 *a = Tcl_GetByteArrayFromObj(objv[3], &n); |
| 1157 | int pgsz = pBuffer->pgsz; |
| 1158 | if( pgsz==0 ) pgsz = 65536; |
| 1159 | for(i=0; i*pgsz<(int)n; i++){ |
| 1160 | int nByte = pgsz; |
| 1161 | tvfsAllocPage(pBuffer, i, pgsz); |
| 1162 | if( n-i*pgsz<pgsz ){ |
| 1163 | nByte = (int)n; |
| 1164 | } |
| 1165 | memcpy(pBuffer->aPage[i], &a[i*pgsz], nByte); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | pObj = Tcl_NewObj(); |
| 1170 | for(i=0; pBuffer->aPage[i]; i++){ |
| 1171 | int pgsz = pBuffer->pgsz; |
| 1172 | if( pgsz==0 ) pgsz = 65536; |
| 1173 | Tcl_AppendObjToObj(pObj, Tcl_NewByteArrayObj(pBuffer->aPage[i], pgsz)); |
| 1174 | } |
| 1175 | Tcl_SetObjResult(interp, pObj); |
| 1176 | break; |
| 1177 | } |
| 1178 | |
| 1179 | /* TESTVFS filter METHOD-LIST |
| 1180 | ** |
| 1181 | ** Activate special processing for those methods contained in the list |
| 1182 | */ |
| 1183 | case CMD_FILTER: { |
| 1184 | static struct VfsMethod { |
| 1185 | char *zName; |
| 1186 | int mask; |
| 1187 | } vfsmethod [] = { |
| 1188 | { "xShmOpen", TESTVFS_SHMOPEN_MASK }, |
| 1189 | { "xShmLock", TESTVFS_SHMLOCK_MASK }, |
| 1190 | { "xShmBarrier", TESTVFS_SHMBARRIER_MASK }, |
| 1191 | { "xShmUnmap", TESTVFS_SHMCLOSE_MASK }, |
| 1192 | { "xShmMap", TESTVFS_SHMMAP_MASK }, |
| 1193 | { "xSync", TESTVFS_SYNC_MASK }, |
| 1194 | { "xDelete", TESTVFS_DELETE_MASK }, |
| 1195 | { "xWrite", TESTVFS_WRITE_MASK }, |
| 1196 | { "xRead", TESTVFS_READ_MASK }, |
| 1197 | { "xTruncate", TESTVFS_TRUNCATE_MASK }, |
| 1198 | { "xOpen", TESTVFS_OPEN_MASK }, |
| 1199 | { "xClose", TESTVFS_CLOSE_MASK }, |
| 1200 | { "xAccess", TESTVFS_ACCESS_MASK }, |
| 1201 | { "xFullPathname", TESTVFS_FULLPATHNAME_MASK }, |
| 1202 | { "xUnlock", TESTVFS_UNLOCK_MASK }, |
| 1203 | { "xLock", TESTVFS_LOCK_MASK }, |
| 1204 | { "xCheckReservedLock", TESTVFS_CKLOCK_MASK }, |
| 1205 | { "xFileControl", TESTVFS_FCNTL_MASK }, |
| 1206 | { "xSleep", TESTVFS_SLEEP_MASK }, |
| 1207 | }; |
| 1208 | Tcl_Obj **apElem = 0; |
| 1209 | Tcl_Size nElem = 0; |
| 1210 | int mask = 0; |
| 1211 | if( objc!=3 ){ |
| 1212 | Tcl_WrongNumArgs(interp, 2, objv, "LIST"); |
| 1213 | return TCL_ERROR; |
| 1214 | } |
| 1215 | if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){ |
| 1216 | return TCL_ERROR; |
| 1217 | } |
| 1218 | Tcl_ResetResult(interp); |
| 1219 | for(i=0; i<(int)nElem; i++){ |
| 1220 | int iMethod; |
| 1221 | char *zElem = Tcl_GetString(apElem[i]); |
| 1222 | for(iMethod=0; iMethod<ArraySize(vfsmethod); iMethod++){ |
| 1223 | if( strcmp(zElem, vfsmethod[iMethod].zName)==0 ){ |
| 1224 | mask |= vfsmethod[iMethod].mask; |
| 1225 | break; |
| 1226 | } |
| 1227 | } |
| 1228 | if( iMethod==ArraySize(vfsmethod) ){ |
| 1229 | Tcl_AppendResult(interp, "unknown method: ", zElem, NULL); |
| 1230 | return TCL_ERROR; |
| 1231 | } |
| 1232 | } |
| 1233 | p->mask = mask; |
| 1234 | break; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | ** TESTVFS script ?SCRIPT? |
| 1239 | ** |
| 1240 | ** Query or set the script to be run when filtered VFS events |
| 1241 | ** occur. |
| 1242 | */ |
| 1243 | case CMD_SCRIPT: { |
| 1244 | if( objc==3 ){ |
| 1245 | Tcl_Size nByte; |
| 1246 | if( p->pScript ){ |
| 1247 | Tcl_DecrRefCount(p->pScript); |
| 1248 | p->pScript = 0; |
| 1249 | } |
| 1250 | Tcl_GetStringFromObj(objv[2], &nByte); |
| 1251 | if( nByte>0 ){ |
| 1252 | p->pScript = Tcl_DuplicateObj(objv[2]); |
| 1253 | Tcl_IncrRefCount(p->pScript); |
| 1254 | } |
| 1255 | }else if( objc!=2 ){ |
| 1256 | Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); |
| 1257 | return TCL_ERROR; |
| 1258 | } |
| 1259 | |
| 1260 | Tcl_ResetResult(interp); |
| 1261 | if( p->pScript ) Tcl_SetObjResult(interp, p->pScript); |
| 1262 | |
| 1263 | break; |
| 1264 | } |
| 1265 | |
| 1266 | /* |
| 1267 | ** TESTVFS ioerr ?IFAIL PERSIST? |
| 1268 | ** |
| 1269 | ** Where IFAIL is an integer and PERSIST is boolean. |
| 1270 | */ |
| 1271 | case CMD_CANTOPENERR: |
| 1272 | case CMD_IOERR: |
| 1273 | case CMD_FULLERR: { |
| 1274 | TestFaultInject *pTest = 0; |
| 1275 | int iRet; |
| 1276 | |
| 1277 | switch( aSubcmd[i].eCmd ){ |
| 1278 | case CMD_IOERR: pTest = &p->ioerr_err; break; |
| 1279 | case CMD_FULLERR: pTest = &p->full_err; break; |
| 1280 | case CMD_CANTOPENERR: pTest = &p->cantopen_err; break; |
| 1281 | default: assert(0); |
| 1282 | } |
| 1283 | iRet = pTest->nFail; |
| 1284 | pTest->nFail = 0; |
| 1285 | pTest->eFault = 0; |
| 1286 | pTest->iCnt = 0; |
| 1287 | |
| 1288 | if( objc==4 ){ |
| 1289 | int iCnt, iPersist; |
| 1290 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iCnt) |
| 1291 | || TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &iPersist) |
| 1292 | ){ |
| 1293 | return TCL_ERROR; |
| 1294 | } |
| 1295 | pTest->eFault = iPersist?FAULT_INJECT_PERSISTENT:FAULT_INJECT_TRANSIENT; |
| 1296 | pTest->iCnt = iCnt; |
| 1297 | }else if( objc!=2 ){ |
| 1298 | Tcl_WrongNumArgs(interp, 2, objv, "?CNT PERSIST?"); |
| 1299 | return TCL_ERROR; |
| 1300 | } |
| 1301 | Tcl_SetObjResult(interp, Tcl_NewIntObj(iRet)); |
| 1302 | break; |
| 1303 | } |
| 1304 | |
| 1305 | case CMD_DELETE: { |
| 1306 | Tcl_DeleteCommand(interp, Tcl_GetString(objv[0])); |
| 1307 | break; |
| 1308 | } |
| 1309 | |
| 1310 | case CMD_DEVCHAR: { |
| 1311 | struct DeviceFlag { |
| 1312 | char *zName; |
| 1313 | int iValue; |
| 1314 | } aFlag[] = { |
| 1315 | { "default", -1 }, |
| 1316 | { "atomic", SQLITE_IOCAP_ATOMIC }, |
| 1317 | { "atomic512", SQLITE_IOCAP_ATOMIC512 }, |
| 1318 | { "atomic1k", SQLITE_IOCAP_ATOMIC1K }, |
| 1319 | { "atomic2k", SQLITE_IOCAP_ATOMIC2K }, |
| 1320 | { "atomic4k", SQLITE_IOCAP_ATOMIC4K }, |
| 1321 | { "atomic8k", SQLITE_IOCAP_ATOMIC8K }, |
| 1322 | { "atomic16k", SQLITE_IOCAP_ATOMIC16K }, |
| 1323 | { "atomic32k", SQLITE_IOCAP_ATOMIC32K }, |
| 1324 | { "atomic64k", SQLITE_IOCAP_ATOMIC64K }, |
| 1325 | { "sequential", SQLITE_IOCAP_SEQUENTIAL }, |
| 1326 | { "safe_append", SQLITE_IOCAP_SAFE_APPEND }, |
| 1327 | { "undeletable_when_open", SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN }, |
| 1328 | { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE }, |
| 1329 | { "immutable", SQLITE_IOCAP_IMMUTABLE }, |
| 1330 | { "batch_atomic", SQLITE_IOCAP_BATCH_ATOMIC }, |
| 1331 | { 0, 0 } |
| 1332 | }; |
| 1333 | Tcl_Obj *pRet; |
| 1334 | int iFlag; |
| 1335 | |
| 1336 | if( objc>3 ){ |
| 1337 | Tcl_WrongNumArgs(interp, 2, objv, "?ATTR-LIST?"); |
| 1338 | return TCL_ERROR; |
| 1339 | } |
| 1340 | if( objc==3 ){ |
| 1341 | int j; |
| 1342 | int iNew = 0; |
| 1343 | Tcl_Obj **flags = 0; |
| 1344 | Tcl_Size nFlags = 0; |
| 1345 | |
| 1346 | if( Tcl_ListObjGetElements(interp, objv[2], &nFlags, &flags) ){ |
| 1347 | return TCL_ERROR; |
| 1348 | } |
| 1349 | |
| 1350 | for(j=0; j<(int)nFlags; j++){ |
| 1351 | int idx = 0; |
| 1352 | if( Tcl_GetIndexFromObjStruct(interp, flags[j], aFlag, |
| 1353 | sizeof(aFlag[0]), "flag", 0, &idx) |
| 1354 | ){ |
| 1355 | return TCL_ERROR; |
| 1356 | } |
| 1357 | if( aFlag[idx].iValue<0 && nFlags>1 ){ |
| 1358 | Tcl_AppendResult(interp, "bad flags: ", Tcl_GetString(objv[2]), NULL); |
| 1359 | return TCL_ERROR; |
| 1360 | } |
| 1361 | iNew |= aFlag[idx].iValue; |
| 1362 | } |
| 1363 | |
| 1364 | p->iDevchar = iNew| 0x10000000; |
| 1365 | } |
| 1366 | |
| 1367 | pRet = Tcl_NewObj(); |
| 1368 | for(iFlag=0; iFlag<sizeof(aFlag)/sizeof(aFlag[0]); iFlag++){ |
| 1369 | if( p->iDevchar & aFlag[iFlag].iValue ){ |
| 1370 | Tcl_ListObjAppendElement( |
| 1371 | interp, pRet, Tcl_NewStringObj(aFlag[iFlag].zName, -1) |
| 1372 | ); |
| 1373 | } |
| 1374 | } |
| 1375 | Tcl_SetObjResult(interp, pRet); |
| 1376 | |
| 1377 | break; |
| 1378 | } |
| 1379 | |
| 1380 | case CMD_SECTORSIZE: { |
| 1381 | if( objc>3 ){ |
| 1382 | Tcl_WrongNumArgs(interp, 2, objv, "?VALUE?"); |
| 1383 | return TCL_ERROR; |
| 1384 | } |
| 1385 | if( objc==3 ){ |
| 1386 | int iNew = 0; |
| 1387 | if( Tcl_GetIntFromObj(interp, objv[2], &iNew) ){ |
| 1388 | return TCL_ERROR; |
| 1389 | } |
| 1390 | p->iSectorsize = iNew; |
| 1391 | } |
| 1392 | Tcl_SetObjResult(interp, Tcl_NewIntObj(p->iSectorsize)); |
| 1393 | break; |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | return TCL_OK; |
| 1398 | } |
| 1399 | |
| 1400 | static void SQLITE_TCLAPI testvfs_obj_del(ClientData cd){ |
| 1401 | Testvfs *p = (Testvfs *)cd; |
| 1402 | if( p->pScript ) Tcl_DecrRefCount(p->pScript); |
| 1403 | sqlite3_vfs_unregister(p->pVfs); |
| 1404 | memset(p->pVfs, 0, sizeof(sqlite3_vfs)); |
| 1405 | ckfree((char *)p->pVfs); |
| 1406 | memset(p, 0, sizeof(Testvfs)); |
| 1407 | ckfree((char *)p); |
| 1408 | } |
| 1409 | |
| 1410 | /* |
| 1411 | ** Usage: testvfs VFSNAME ?SWITCHES? |
| 1412 | ** |
| 1413 | ** Switches are: |
| 1414 | ** |
| 1415 | ** -noshm BOOLEAN (True to omit shm methods. Default false) |
| 1416 | ** -default BOOLEAN (True to make the vfs default. Default false) |
| 1417 | ** |
| 1418 | ** This command creates two things when it is invoked: an SQLite VFS, and |
| 1419 | ** a Tcl command. Both are named VFSNAME. The VFS is installed. It is not |
| 1420 | ** installed as the default VFS. |
| 1421 | ** |
| 1422 | ** The VFS passes all file I/O calls through to the underlying VFS. |
| 1423 | ** |
| 1424 | ** Whenever the xShmMap method of the VFS |
| 1425 | ** is invoked, the SCRIPT is executed as follows: |
| 1426 | ** |
| 1427 | ** SCRIPT xShmMap FILENAME ID |
| 1428 | ** |
| 1429 | ** The value returned by the invocation of SCRIPT above is interpreted as |
| 1430 | ** an SQLite error code and returned to SQLite. Either a symbolic |
| 1431 | ** "SQLITE_OK" or numeric "0" value may be returned. |
| 1432 | ** |
| 1433 | ** The contents of the shared-memory buffer associated with a given file |
| 1434 | ** may be read and set using the following command: |
| 1435 | ** |
| 1436 | ** VFSNAME shm FILENAME ?NEWVALUE? |
| 1437 | ** |
| 1438 | ** When the xShmLock method is invoked by SQLite, the following script is |
| 1439 | ** run: |
| 1440 | ** |
| 1441 | ** SCRIPT xShmLock FILENAME ID LOCK |
| 1442 | ** |
| 1443 | ** where LOCK is of the form "OFFSET NBYTE lock/unlock shared/exclusive" |
| 1444 | */ |
| 1445 | static int SQLITE_TCLAPI testvfs_cmd( |
| 1446 | ClientData cd, |
| 1447 | Tcl_Interp *interp, |
| 1448 | int objc, |
| 1449 | Tcl_Obj *CONST objv[] |
| 1450 | ){ |
| 1451 | static sqlite3_vfs tvfs_vfs = { |
| 1452 | 3, /* iVersion */ |
| 1453 | 0, /* szOsFile */ |
| 1454 | 0, /* mxPathname */ |
| 1455 | 0, /* pNext */ |
| 1456 | 0, /* zName */ |
| 1457 | 0, /* pAppData */ |
| 1458 | tvfsOpen, /* xOpen */ |
| 1459 | tvfsDelete, /* xDelete */ |
| 1460 | tvfsAccess, /* xAccess */ |
| 1461 | tvfsFullPathname, /* xFullPathname */ |
| 1462 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 1463 | tvfsDlOpen, /* xDlOpen */ |
| 1464 | tvfsDlError, /* xDlError */ |
| 1465 | tvfsDlSym, /* xDlSym */ |
| 1466 | tvfsDlClose, /* xDlClose */ |
| 1467 | #else |
| 1468 | 0, /* xDlOpen */ |
| 1469 | 0, /* xDlError */ |
| 1470 | 0, /* xDlSym */ |
| 1471 | 0, /* xDlClose */ |
| 1472 | #endif /* SQLITE_OMIT_LOAD_EXTENSION */ |
| 1473 | tvfsRandomness, /* xRandomness */ |
| 1474 | tvfsSleep, /* xSleep */ |
| 1475 | tvfsCurrentTime, /* xCurrentTime */ |
| 1476 | 0, /* xGetLastError */ |
| 1477 | 0, /* xCurrentTimeInt64 */ |
| 1478 | 0, /* xSetSystemCall */ |
| 1479 | 0, /* xGetSystemCall */ |
| 1480 | 0, /* xNextSystemCall */ |
| 1481 | }; |
| 1482 | |
| 1483 | Testvfs *p; /* New object */ |
| 1484 | sqlite3_vfs *pVfs; /* New VFS */ |
| 1485 | char *zVfs; |
| 1486 | int nByte; /* Bytes of space to allocate at p */ |
| 1487 | |
| 1488 | int i; |
| 1489 | int isNoshm = 0; /* True if -noshm is passed */ |
| 1490 | int isFullshm = 0; /* True if -fullshm is passed */ |
| 1491 | int isDefault = 0; /* True if -default is passed */ |
| 1492 | int szOsFile = 0; /* Value passed to -szosfile */ |
| 1493 | int mxPathname = -1; /* Value passed to -mxpathname */ |
| 1494 | int iVersion = 3; /* Value passed to -iversion */ |
| 1495 | |
| 1496 | if( objc<2 || 0!=(objc%2) ) goto bad_args; |
| 1497 | for(i=2; i<objc; i += 2){ |
| 1498 | Tcl_Size nSwitch; |
| 1499 | char *zSwitch; |
| 1500 | zSwitch = Tcl_GetStringFromObj(objv[i], &nSwitch); |
| 1501 | |
| 1502 | if( nSwitch>2 && 0==strncmp("-noshm", zSwitch, nSwitch) ){ |
| 1503 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &isNoshm) ){ |
| 1504 | return TCL_ERROR; |
| 1505 | } |
| 1506 | if( isNoshm ) isFullshm = 0; |
| 1507 | } |
| 1508 | else if( nSwitch>2 && 0==strncmp("-default", zSwitch, nSwitch) ){ |
| 1509 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &isDefault) ){ |
| 1510 | return TCL_ERROR; |
| 1511 | } |
| 1512 | } |
| 1513 | else if( nSwitch>2 && 0==strncmp("-szosfile", zSwitch, nSwitch) ){ |
| 1514 | if( Tcl_GetIntFromObj(interp, objv[i+1], &szOsFile) ){ |
| 1515 | return TCL_ERROR; |
| 1516 | } |
| 1517 | } |
| 1518 | else if( nSwitch>2 && 0==strncmp("-mxpathname", zSwitch, nSwitch) ){ |
| 1519 | if( Tcl_GetIntFromObj(interp, objv[i+1], &mxPathname) ){ |
| 1520 | return TCL_ERROR; |
| 1521 | } |
| 1522 | } |
| 1523 | else if( nSwitch>2 && 0==strncmp("-iversion", zSwitch, nSwitch) ){ |
| 1524 | if( Tcl_GetIntFromObj(interp, objv[i+1], &iVersion) ){ |
| 1525 | return TCL_ERROR; |
| 1526 | } |
| 1527 | } |
| 1528 | else if( nSwitch>2 && 0==strncmp("-fullshm", zSwitch, nSwitch) ){ |
| 1529 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &isFullshm) ){ |
| 1530 | return TCL_ERROR; |
| 1531 | } |
| 1532 | if( isFullshm ) isNoshm = 0; |
| 1533 | } |
| 1534 | else{ |
| 1535 | goto bad_args; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | if( szOsFile<sizeof(TestvfsFile) ){ |
| 1540 | szOsFile = sizeof(TestvfsFile); |
| 1541 | } |
| 1542 | |
| 1543 | zVfs = Tcl_GetString(objv[1]); |
| 1544 | nByte = sizeof(Testvfs) + (int)strlen(zVfs)+1; |
| 1545 | p = (Testvfs *)ckalloc(nByte); |
| 1546 | memset(p, 0, nByte); |
| 1547 | p->iDevchar = -1; |
| 1548 | p->iSectorsize = -1; |
| 1549 | |
| 1550 | /* Create the new object command before querying SQLite for a default VFS |
| 1551 | ** to use for 'real' IO operations. This is because creating the new VFS |
| 1552 | ** may delete an existing [testvfs] VFS of the same name. If such a VFS |
| 1553 | ** is currently the default, the new [testvfs] may end up calling the |
| 1554 | ** methods of a deleted object. |
| 1555 | */ |
| 1556 | Tcl_CreateObjCommand(interp, zVfs, testvfs_obj_cmd, p, testvfs_obj_del); |
| 1557 | p->pParent = sqlite3_vfs_find(0); |
| 1558 | p->interp = interp; |
| 1559 | |
| 1560 | p->zName = (char *)&p[1]; |
| 1561 | memcpy(p->zName, zVfs, strlen(zVfs)+1); |
| 1562 | |
| 1563 | pVfs = (sqlite3_vfs *)ckalloc(sizeof(sqlite3_vfs)); |
| 1564 | memcpy(pVfs, &tvfs_vfs, sizeof(sqlite3_vfs)); |
| 1565 | pVfs->pAppData = (void *)p; |
| 1566 | pVfs->iVersion = iVersion; |
| 1567 | pVfs->zName = p->zName; |
| 1568 | pVfs->mxPathname = p->pParent->mxPathname; |
| 1569 | if( mxPathname>=0 && mxPathname<pVfs->mxPathname ){ |
| 1570 | pVfs->mxPathname = mxPathname; |
| 1571 | } |
| 1572 | pVfs->szOsFile = szOsFile; |
| 1573 | p->pVfs = pVfs; |
| 1574 | p->isNoshm = isNoshm; |
| 1575 | p->isFullshm = isFullshm; |
| 1576 | p->mask = TESTVFS_ALL_MASK; |
| 1577 | |
| 1578 | sqlite3_vfs_register(pVfs, isDefault); |
| 1579 | |
| 1580 | return TCL_OK; |
| 1581 | |
| 1582 | bad_args: |
| 1583 | Tcl_WrongNumArgs(interp, 1, objv, "VFSNAME ?-noshm BOOL? ?-fullshm BOOL? ?-default BOOL? ?-mxpathname INT? ?-szosfile INT? ?-iversion INT?"); |
| 1584 | return TCL_ERROR; |
| 1585 | } |
| 1586 | |
| 1587 | extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); |
| 1588 | extern const char *sqlite3ErrName(int); |
| 1589 | |
| 1590 | /* |
| 1591 | ** tclcmd: vfs_shmlock DB DBNAME (shared|exclusive) (lock|unlock) OFFSET N |
| 1592 | */ |
| 1593 | static int SQLITE_TCLAPI test_vfs_shmlock( |
| 1594 | void * clientData, |
| 1595 | Tcl_Interp *interp, |
| 1596 | int objc, |
| 1597 | Tcl_Obj *CONST objv[] |
| 1598 | ){ |
| 1599 | const char *azArg1[] = {"shared", "exclusive", 0}; |
| 1600 | const char *azArg2[] = {"lock", "unlock", 0}; |
| 1601 | sqlite3 *db = 0; |
| 1602 | int rc = SQLITE_OK; |
| 1603 | const char *zDbname = 0; |
| 1604 | int iArg1 = 0; |
| 1605 | int iArg2 = 0; |
| 1606 | int iOffset = 0; |
| 1607 | int n = 0; |
| 1608 | sqlite3_file *pFd; |
| 1609 | |
| 1610 | if( objc!=7 ){ |
| 1611 | Tcl_WrongNumArgs(interp, 1, objv, |
| 1612 | "DB DBNAME (shared|exclusive) (lock|unlock) OFFSET N" |
| 1613 | ); |
| 1614 | return TCL_ERROR; |
| 1615 | } |
| 1616 | |
| 1617 | zDbname = Tcl_GetString(objv[2]); |
| 1618 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 1619 | || Tcl_GetIndexFromObj(interp, objv[3], azArg1, "ARG", 0, &iArg1) |
| 1620 | || Tcl_GetIndexFromObj(interp, objv[4], azArg2, "ARG", 0, &iArg2) |
| 1621 | || Tcl_GetIntFromObj(interp, objv[5], &iOffset) |
| 1622 | || Tcl_GetIntFromObj(interp, objv[6], &n) |
| 1623 | ){ |
| 1624 | return TCL_ERROR; |
| 1625 | } |
| 1626 | |
| 1627 | sqlite3_file_control(db, zDbname, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd); |
| 1628 | if( pFd==0 ){ |
| 1629 | return TCL_ERROR; |
| 1630 | } |
| 1631 | rc = pFd->pMethods->xShmLock(pFd, iOffset, n, |
| 1632 | (iArg1==0 ? SQLITE_SHM_SHARED : SQLITE_SHM_EXCLUSIVE) |
| 1633 | | (iArg2==0 ? SQLITE_SHM_LOCK : SQLITE_SHM_UNLOCK) |
| 1634 | ); |
| 1635 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 1636 | return TCL_OK; |
| 1637 | } |
| 1638 | |
| 1639 | static int SQLITE_TCLAPI test_vfs_set_readmark( |
| 1640 | void * clientData, |
| 1641 | Tcl_Interp *interp, |
| 1642 | int objc, |
| 1643 | Tcl_Obj *CONST objv[] |
| 1644 | ){ |
| 1645 | sqlite3 *db = 0; |
| 1646 | int rc = SQLITE_OK; |
| 1647 | const char *zDbname = 0; |
| 1648 | int iSlot = 0; |
| 1649 | int iVal = -1; |
| 1650 | sqlite3_file *pFd; |
| 1651 | void volatile *pShm = 0; |
| 1652 | u32 *aShm; |
| 1653 | int iOff; |
| 1654 | |
| 1655 | if( objc!=4 && objc!=5 ){ |
| 1656 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SLOT ?VALUE?"); |
| 1657 | return TCL_ERROR; |
| 1658 | } |
| 1659 | |
| 1660 | zDbname = Tcl_GetString(objv[2]); |
| 1661 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 1662 | || Tcl_GetIntFromObj(interp, objv[3], &iSlot) |
| 1663 | || (objc==5 && Tcl_GetIntFromObj(interp, objv[4], &iVal)) |
| 1664 | ){ |
| 1665 | return TCL_ERROR; |
| 1666 | } |
| 1667 | |
| 1668 | sqlite3_file_control(db, zDbname, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd); |
| 1669 | if( pFd==0 ){ |
| 1670 | return TCL_ERROR; |
| 1671 | } |
| 1672 | rc = pFd->pMethods->xShmMap(pFd, 0, 32*1024, 0, &pShm); |
| 1673 | if( rc!=SQLITE_OK ){ |
| 1674 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 1675 | return TCL_ERROR; |
| 1676 | } |
| 1677 | if( pShm==0 ){ |
| 1678 | Tcl_AppendResult(interp, "*-shm is not yet mapped", NULL); |
| 1679 | return TCL_ERROR; |
| 1680 | } |
| 1681 | aShm = (u32*)pShm; |
| 1682 | iOff = 12*2+1+iSlot; |
| 1683 | |
| 1684 | if( objc==5 ){ |
| 1685 | aShm[iOff] = iVal; |
| 1686 | } |
| 1687 | Tcl_SetObjResult(interp, Tcl_NewIntObj(aShm[iOff])); |
| 1688 | |
| 1689 | return TCL_OK; |
| 1690 | } |
| 1691 | |
| 1692 | int Sqlitetestvfs_Init(Tcl_Interp *interp){ |
| 1693 | Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0); |
| 1694 | Tcl_CreateObjCommand(interp, "vfs_shmlock", test_vfs_shmlock, 0, 0); |
| 1695 | Tcl_CreateObjCommand(interp, "vfs_set_readmark", test_vfs_set_readmark, 0, 0); |
| 1696 | return TCL_OK; |
| 1697 | } |
| 1698 | |
| 1699 | #endif |