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