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