| OLD | NEW |
| (Empty) |
| 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->pScript && p->mask&TESTVFS_SHMBARRIER_MASK ){ | |
| 971 const char *z = pFd->pShm ? pFd->pShm->zFile : ""; | |
| 972 tvfsExecTcl(p, "xShmBarrier", Tcl_NewStringObj(z, -1), pFd->pShmId, 0, 0); | |
| 973 } | |
| 974 | |
| 975 if( p->isFullshm ){ | |
| 976 sqlite3OsShmBarrier(pFd->pReal); | |
| 977 return; | |
| 978 } | |
| 979 } | |
| 980 | |
| 981 static int tvfsShmUnmap( | |
| 982 sqlite3_file *pFile, | |
| 983 int deleteFlag | |
| 984 ){ | |
| 985 int rc = SQLITE_OK; | |
| 986 TestvfsFd *pFd = tvfsGetFd(pFile); | |
| 987 Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData); | |
| 988 TestvfsBuffer *pBuffer = pFd->pShm; | |
| 989 TestvfsFd **ppFd; | |
| 990 | |
| 991 if( p->isFullshm ){ | |
| 992 return sqlite3OsShmUnmap(pFd->pReal, deleteFlag); | |
| 993 } | |
| 994 | |
| 995 if( !pBuffer ) return SQLITE_OK; | |
| 996 assert( pFd->pShmId && pFd->pShm ); | |
| 997 | |
| 998 if( p->pScript && p->mask&TESTVFS_SHMCLOSE_MASK ){ | |
| 999 tvfsExecTcl(p, "xShmUnmap", | |
| 1000 Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0, 0 | |
| 1001 ); | |
| 1002 tvfsResultCode(p, &rc); | |
| 1003 } | |
| 1004 | |
| 1005 for(ppFd=&pBuffer->pFile; *ppFd!=pFd; ppFd=&((*ppFd)->pNext)); | |
| 1006 assert( (*ppFd)==pFd ); | |
| 1007 *ppFd = pFd->pNext; | |
| 1008 pFd->pNext = 0; | |
| 1009 | |
| 1010 if( pBuffer->pFile==0 ){ | |
| 1011 int i; | |
| 1012 TestvfsBuffer **pp; | |
| 1013 for(pp=&p->pBuffer; *pp!=pBuffer; pp=&((*pp)->pNext)); | |
| 1014 *pp = (*pp)->pNext; | |
| 1015 for(i=0; pBuffer->aPage[i]; i++){ | |
| 1016 ckfree((char *)pBuffer->aPage[i]); | |
| 1017 } | |
| 1018 ckfree((char *)pBuffer); | |
| 1019 } | |
| 1020 pFd->pShm = 0; | |
| 1021 | |
| 1022 return rc; | |
| 1023 } | |
| 1024 | |
| 1025 static int tvfsFetch( | |
| 1026 sqlite3_file *pFile, | |
| 1027 sqlite3_int64 iOfst, | |
| 1028 int iAmt, | |
| 1029 void **pp | |
| 1030 ){ | |
| 1031 TestvfsFd *pFd = tvfsGetFd(pFile); | |
| 1032 return sqlite3OsFetch(pFd->pReal, iOfst, iAmt, pp); | |
| 1033 } | |
| 1034 | |
| 1035 static int tvfsUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *p){ | |
| 1036 TestvfsFd *pFd = tvfsGetFd(pFile); | |
| 1037 return sqlite3OsUnfetch(pFd->pReal, iOfst, p); | |
| 1038 } | |
| 1039 | |
| 1040 static int testvfs_obj_cmd( | |
| 1041 ClientData cd, | |
| 1042 Tcl_Interp *interp, | |
| 1043 int objc, | |
| 1044 Tcl_Obj *CONST objv[] | |
| 1045 ){ | |
| 1046 Testvfs *p = (Testvfs *)cd; | |
| 1047 | |
| 1048 enum DB_enum { | |
| 1049 CMD_SHM, CMD_DELETE, CMD_FILTER, CMD_IOERR, CMD_SCRIPT, | |
| 1050 CMD_DEVCHAR, CMD_SECTORSIZE, CMD_FULLERR, CMD_CANTOPENERR | |
| 1051 }; | |
| 1052 struct TestvfsSubcmd { | |
| 1053 char *zName; | |
| 1054 enum DB_enum eCmd; | |
| 1055 } aSubcmd[] = { | |
| 1056 { "shm", CMD_SHM }, | |
| 1057 { "delete", CMD_DELETE }, | |
| 1058 { "filter", CMD_FILTER }, | |
| 1059 { "ioerr", CMD_IOERR }, | |
| 1060 { "fullerr", CMD_FULLERR }, | |
| 1061 { "cantopenerr", CMD_CANTOPENERR }, | |
| 1062 { "script", CMD_SCRIPT }, | |
| 1063 { "devchar", CMD_DEVCHAR }, | |
| 1064 { "sectorsize", CMD_SECTORSIZE }, | |
| 1065 { 0, 0 } | |
| 1066 }; | |
| 1067 int i; | |
| 1068 | |
| 1069 if( objc<2 ){ | |
| 1070 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); | |
| 1071 return TCL_ERROR; | |
| 1072 } | |
| 1073 if( Tcl_GetIndexFromObjStruct( | |
| 1074 interp, objv[1], aSubcmd, sizeof(aSubcmd[0]), "subcommand", 0, &i) | |
| 1075 ){ | |
| 1076 return TCL_ERROR; | |
| 1077 } | |
| 1078 Tcl_ResetResult(interp); | |
| 1079 | |
| 1080 switch( aSubcmd[i].eCmd ){ | |
| 1081 case CMD_SHM: { | |
| 1082 Tcl_Obj *pObj; | |
| 1083 int rc; | |
| 1084 TestvfsBuffer *pBuffer; | |
| 1085 char *zName; | |
| 1086 if( objc!=3 && objc!=4 ){ | |
| 1087 Tcl_WrongNumArgs(interp, 2, objv, "FILE ?VALUE?"); | |
| 1088 return TCL_ERROR; | |
| 1089 } | |
| 1090 zName = ckalloc(p->pParent->mxPathname); | |
| 1091 rc = p->pParent->xFullPathname( | |
| 1092 p->pParent, Tcl_GetString(objv[2]), | |
| 1093 p->pParent->mxPathname, zName | |
| 1094 ); | |
| 1095 if( rc!=SQLITE_OK ){ | |
| 1096 Tcl_AppendResult(interp, "failed to get full path: ", | |
| 1097 Tcl_GetString(objv[2]), 0); | |
| 1098 ckfree(zName); | |
| 1099 return TCL_ERROR; | |
| 1100 } | |
| 1101 for(pBuffer=p->pBuffer; pBuffer; pBuffer=pBuffer->pNext){ | |
| 1102 if( 0==strcmp(pBuffer->zFile, zName) ) break; | |
| 1103 } | |
| 1104 ckfree(zName); | |
| 1105 if( !pBuffer ){ | |
| 1106 Tcl_AppendResult(interp, "no such file: ", Tcl_GetString(objv[2]), 0); | |
| 1107 return TCL_ERROR; | |
| 1108 } | |
| 1109 if( objc==4 ){ | |
| 1110 int n; | |
| 1111 u8 *a = Tcl_GetByteArrayFromObj(objv[3], &n); | |
| 1112 int pgsz = pBuffer->pgsz; | |
| 1113 if( pgsz==0 ) pgsz = 65536; | |
| 1114 for(i=0; i*pgsz<n; i++){ | |
| 1115 int nByte = pgsz; | |
| 1116 tvfsAllocPage(pBuffer, i, pgsz); | |
| 1117 if( n-i*pgsz<pgsz ){ | |
| 1118 nByte = n; | |
| 1119 } | |
| 1120 memcpy(pBuffer->aPage[i], &a[i*pgsz], nByte); | |
| 1121 } | |
| 1122 } | |
| 1123 | |
| 1124 pObj = Tcl_NewObj(); | |
| 1125 for(i=0; pBuffer->aPage[i]; i++){ | |
| 1126 int pgsz = pBuffer->pgsz; | |
| 1127 if( pgsz==0 ) pgsz = 65536; | |
| 1128 Tcl_AppendObjToObj(pObj, Tcl_NewByteArrayObj(pBuffer->aPage[i], pgsz)); | |
| 1129 } | |
| 1130 Tcl_SetObjResult(interp, pObj); | |
| 1131 break; | |
| 1132 } | |
| 1133 | |
| 1134 /* TESTVFS filter METHOD-LIST | |
| 1135 ** | |
| 1136 ** Activate special processing for those methods contained in the list | |
| 1137 */ | |
| 1138 case CMD_FILTER: { | |
| 1139 static struct VfsMethod { | |
| 1140 char *zName; | |
| 1141 int mask; | |
| 1142 } vfsmethod [] = { | |
| 1143 { "xShmOpen", TESTVFS_SHMOPEN_MASK }, | |
| 1144 { "xShmLock", TESTVFS_SHMLOCK_MASK }, | |
| 1145 { "xShmBarrier", TESTVFS_SHMBARRIER_MASK }, | |
| 1146 { "xShmUnmap", TESTVFS_SHMCLOSE_MASK }, | |
| 1147 { "xShmMap", TESTVFS_SHMMAP_MASK }, | |
| 1148 { "xSync", TESTVFS_SYNC_MASK }, | |
| 1149 { "xDelete", TESTVFS_DELETE_MASK }, | |
| 1150 { "xWrite", TESTVFS_WRITE_MASK }, | |
| 1151 { "xRead", TESTVFS_READ_MASK }, | |
| 1152 { "xTruncate", TESTVFS_TRUNCATE_MASK }, | |
| 1153 { "xOpen", TESTVFS_OPEN_MASK }, | |
| 1154 { "xClose", TESTVFS_CLOSE_MASK }, | |
| 1155 { "xAccess", TESTVFS_ACCESS_MASK }, | |
| 1156 { "xFullPathname", TESTVFS_FULLPATHNAME_MASK }, | |
| 1157 { "xUnlock", TESTVFS_UNLOCK_MASK }, | |
| 1158 { "xLock", TESTVFS_LOCK_MASK }, | |
| 1159 { "xCheckReservedLock", TESTVFS_CKLOCK_MASK }, | |
| 1160 }; | |
| 1161 Tcl_Obj **apElem = 0; | |
| 1162 int nElem = 0; | |
| 1163 int mask = 0; | |
| 1164 if( objc!=3 ){ | |
| 1165 Tcl_WrongNumArgs(interp, 2, objv, "LIST"); | |
| 1166 return TCL_ERROR; | |
| 1167 } | |
| 1168 if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){ | |
| 1169 return TCL_ERROR; | |
| 1170 } | |
| 1171 Tcl_ResetResult(interp); | |
| 1172 for(i=0; i<nElem; i++){ | |
| 1173 int iMethod; | |
| 1174 char *zElem = Tcl_GetString(apElem[i]); | |
| 1175 for(iMethod=0; iMethod<ArraySize(vfsmethod); iMethod++){ | |
| 1176 if( strcmp(zElem, vfsmethod[iMethod].zName)==0 ){ | |
| 1177 mask |= vfsmethod[iMethod].mask; | |
| 1178 break; | |
| 1179 } | |
| 1180 } | |
| 1181 if( iMethod==ArraySize(vfsmethod) ){ | |
| 1182 Tcl_AppendResult(interp, "unknown method: ", zElem, 0); | |
| 1183 return TCL_ERROR; | |
| 1184 } | |
| 1185 } | |
| 1186 p->mask = mask; | |
| 1187 break; | |
| 1188 } | |
| 1189 | |
| 1190 /* | |
| 1191 ** TESTVFS script ?SCRIPT? | |
| 1192 ** | |
| 1193 ** Query or set the script to be run when filtered VFS events | |
| 1194 ** occur. | |
| 1195 */ | |
| 1196 case CMD_SCRIPT: { | |
| 1197 if( objc==3 ){ | |
| 1198 int nByte; | |
| 1199 if( p->pScript ){ | |
| 1200 Tcl_DecrRefCount(p->pScript); | |
| 1201 p->pScript = 0; | |
| 1202 } | |
| 1203 Tcl_GetStringFromObj(objv[2], &nByte); | |
| 1204 if( nByte>0 ){ | |
| 1205 p->pScript = Tcl_DuplicateObj(objv[2]); | |
| 1206 Tcl_IncrRefCount(p->pScript); | |
| 1207 } | |
| 1208 }else if( objc!=2 ){ | |
| 1209 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); | |
| 1210 return TCL_ERROR; | |
| 1211 } | |
| 1212 | |
| 1213 Tcl_ResetResult(interp); | |
| 1214 if( p->pScript ) Tcl_SetObjResult(interp, p->pScript); | |
| 1215 | |
| 1216 break; | |
| 1217 } | |
| 1218 | |
| 1219 /* | |
| 1220 ** TESTVFS ioerr ?IFAIL PERSIST? | |
| 1221 ** | |
| 1222 ** Where IFAIL is an integer and PERSIST is boolean. | |
| 1223 */ | |
| 1224 case CMD_CANTOPENERR: | |
| 1225 case CMD_IOERR: | |
| 1226 case CMD_FULLERR: { | |
| 1227 TestFaultInject *pTest = 0; | |
| 1228 int iRet; | |
| 1229 | |
| 1230 switch( aSubcmd[i].eCmd ){ | |
| 1231 case CMD_IOERR: pTest = &p->ioerr_err; break; | |
| 1232 case CMD_FULLERR: pTest = &p->full_err; break; | |
| 1233 case CMD_CANTOPENERR: pTest = &p->cantopen_err; break; | |
| 1234 default: assert(0); | |
| 1235 } | |
| 1236 iRet = pTest->nFail; | |
| 1237 pTest->nFail = 0; | |
| 1238 pTest->eFault = 0; | |
| 1239 pTest->iCnt = 0; | |
| 1240 | |
| 1241 if( objc==4 ){ | |
| 1242 int iCnt, iPersist; | |
| 1243 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iCnt) | |
| 1244 || TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &iPersist) | |
| 1245 ){ | |
| 1246 return TCL_ERROR; | |
| 1247 } | |
| 1248 pTest->eFault = iPersist?FAULT_INJECT_PERSISTENT:FAULT_INJECT_TRANSIENT; | |
| 1249 pTest->iCnt = iCnt; | |
| 1250 }else if( objc!=2 ){ | |
| 1251 Tcl_WrongNumArgs(interp, 2, objv, "?CNT PERSIST?"); | |
| 1252 return TCL_ERROR; | |
| 1253 } | |
| 1254 Tcl_SetObjResult(interp, Tcl_NewIntObj(iRet)); | |
| 1255 break; | |
| 1256 } | |
| 1257 | |
| 1258 case CMD_DELETE: { | |
| 1259 Tcl_DeleteCommand(interp, Tcl_GetString(objv[0])); | |
| 1260 break; | |
| 1261 } | |
| 1262 | |
| 1263 case CMD_DEVCHAR: { | |
| 1264 struct DeviceFlag { | |
| 1265 char *zName; | |
| 1266 int iValue; | |
| 1267 } aFlag[] = { | |
| 1268 { "default", -1 }, | |
| 1269 { "atomic", SQLITE_IOCAP_ATOMIC }, | |
| 1270 { "atomic512", SQLITE_IOCAP_ATOMIC512 }, | |
| 1271 { "atomic1k", SQLITE_IOCAP_ATOMIC1K }, | |
| 1272 { "atomic2k", SQLITE_IOCAP_ATOMIC2K }, | |
| 1273 { "atomic4k", SQLITE_IOCAP_ATOMIC4K }, | |
| 1274 { "atomic8k", SQLITE_IOCAP_ATOMIC8K }, | |
| 1275 { "atomic16k", SQLITE_IOCAP_ATOMIC16K }, | |
| 1276 { "atomic32k", SQLITE_IOCAP_ATOMIC32K }, | |
| 1277 { "atomic64k", SQLITE_IOCAP_ATOMIC64K }, | |
| 1278 { "sequential", SQLITE_IOCAP_SEQUENTIAL }, | |
| 1279 { "safe_append", SQLITE_IOCAP_SAFE_APPEND }, | |
| 1280 { "undeletable_when_open", SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN }, | |
| 1281 { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE }, | |
| 1282 { "immutable", SQLITE_IOCAP_IMMUTABLE }, | |
| 1283 { 0, 0 } | |
| 1284 }; | |
| 1285 Tcl_Obj *pRet; | |
| 1286 int iFlag; | |
| 1287 | |
| 1288 if( objc>3 ){ | |
| 1289 Tcl_WrongNumArgs(interp, 2, objv, "?ATTR-LIST?"); | |
| 1290 return TCL_ERROR; | |
| 1291 } | |
| 1292 if( objc==3 ){ | |
| 1293 int j; | |
| 1294 int iNew = 0; | |
| 1295 Tcl_Obj **flags = 0; | |
| 1296 int nFlags = 0; | |
| 1297 | |
| 1298 if( Tcl_ListObjGetElements(interp, objv[2], &nFlags, &flags) ){ | |
| 1299 return TCL_ERROR; | |
| 1300 } | |
| 1301 | |
| 1302 for(j=0; j<nFlags; j++){ | |
| 1303 int idx = 0; | |
| 1304 if( Tcl_GetIndexFromObjStruct(interp, flags[j], aFlag, | |
| 1305 sizeof(aFlag[0]), "flag", 0, &idx) | |
| 1306 ){ | |
| 1307 return TCL_ERROR; | |
| 1308 } | |
| 1309 if( aFlag[idx].iValue<0 && nFlags>1 ){ | |
| 1310 Tcl_AppendResult(interp, "bad flags: ", Tcl_GetString(objv[2]), 0); | |
| 1311 return TCL_ERROR; | |
| 1312 } | |
| 1313 iNew |= aFlag[idx].iValue; | |
| 1314 } | |
| 1315 | |
| 1316 p->iDevchar = iNew| 0x10000000; | |
| 1317 } | |
| 1318 | |
| 1319 pRet = Tcl_NewObj(); | |
| 1320 for(iFlag=0; iFlag<sizeof(aFlag)/sizeof(aFlag[0]); iFlag++){ | |
| 1321 if( p->iDevchar & aFlag[iFlag].iValue ){ | |
| 1322 Tcl_ListObjAppendElement( | |
| 1323 interp, pRet, Tcl_NewStringObj(aFlag[iFlag].zName, -1) | |
| 1324 ); | |
| 1325 } | |
| 1326 } | |
| 1327 Tcl_SetObjResult(interp, pRet); | |
| 1328 | |
| 1329 break; | |
| 1330 } | |
| 1331 | |
| 1332 case CMD_SECTORSIZE: { | |
| 1333 if( objc>3 ){ | |
| 1334 Tcl_WrongNumArgs(interp, 2, objv, "?VALUE?"); | |
| 1335 return TCL_ERROR; | |
| 1336 } | |
| 1337 if( objc==3 ){ | |
| 1338 int iNew = 0; | |
| 1339 if( Tcl_GetIntFromObj(interp, objv[2], &iNew) ){ | |
| 1340 return TCL_ERROR; | |
| 1341 } | |
| 1342 p->iSectorsize = iNew; | |
| 1343 } | |
| 1344 Tcl_SetObjResult(interp, Tcl_NewIntObj(p->iSectorsize)); | |
| 1345 break; | |
| 1346 } | |
| 1347 } | |
| 1348 | |
| 1349 return TCL_OK; | |
| 1350 } | |
| 1351 | |
| 1352 static void testvfs_obj_del(ClientData cd){ | |
| 1353 Testvfs *p = (Testvfs *)cd; | |
| 1354 if( p->pScript ) Tcl_DecrRefCount(p->pScript); | |
| 1355 sqlite3_vfs_unregister(p->pVfs); | |
| 1356 ckfree((char *)p->pVfs); | |
| 1357 ckfree((char *)p); | |
| 1358 } | |
| 1359 | |
| 1360 /* | |
| 1361 ** Usage: testvfs VFSNAME ?SWITCHES? | |
| 1362 ** | |
| 1363 ** Switches are: | |
| 1364 ** | |
| 1365 ** -noshm BOOLEAN (True to omit shm methods. Default false) | |
| 1366 ** -default BOOLEAN (True to make the vfs default. Default false) | |
| 1367 ** | |
| 1368 ** This command creates two things when it is invoked: an SQLite VFS, and | |
| 1369 ** a Tcl command. Both are named VFSNAME. The VFS is installed. It is not | |
| 1370 ** installed as the default VFS. | |
| 1371 ** | |
| 1372 ** The VFS passes all file I/O calls through to the underlying VFS. | |
| 1373 ** | |
| 1374 ** Whenever the xShmMap method of the VFS | |
| 1375 ** is invoked, the SCRIPT is executed as follows: | |
| 1376 ** | |
| 1377 ** SCRIPT xShmMap FILENAME ID | |
| 1378 ** | |
| 1379 ** The value returned by the invocation of SCRIPT above is interpreted as | |
| 1380 ** an SQLite error code and returned to SQLite. Either a symbolic | |
| 1381 ** "SQLITE_OK" or numeric "0" value may be returned. | |
| 1382 ** | |
| 1383 ** The contents of the shared-memory buffer associated with a given file | |
| 1384 ** may be read and set using the following command: | |
| 1385 ** | |
| 1386 ** VFSNAME shm FILENAME ?NEWVALUE? | |
| 1387 ** | |
| 1388 ** When the xShmLock method is invoked by SQLite, the following script is | |
| 1389 ** run: | |
| 1390 ** | |
| 1391 ** SCRIPT xShmLock FILENAME ID LOCK | |
| 1392 ** | |
| 1393 ** where LOCK is of the form "OFFSET NBYTE lock/unlock shared/exclusive" | |
| 1394 */ | |
| 1395 static int testvfs_cmd( | |
| 1396 ClientData cd, | |
| 1397 Tcl_Interp *interp, | |
| 1398 int objc, | |
| 1399 Tcl_Obj *CONST objv[] | |
| 1400 ){ | |
| 1401 static sqlite3_vfs tvfs_vfs = { | |
| 1402 3, /* iVersion */ | |
| 1403 0, /* szOsFile */ | |
| 1404 0, /* mxPathname */ | |
| 1405 0, /* pNext */ | |
| 1406 0, /* zName */ | |
| 1407 0, /* pAppData */ | |
| 1408 tvfsOpen, /* xOpen */ | |
| 1409 tvfsDelete, /* xDelete */ | |
| 1410 tvfsAccess, /* xAccess */ | |
| 1411 tvfsFullPathname, /* xFullPathname */ | |
| 1412 #ifndef SQLITE_OMIT_LOAD_EXTENSION | |
| 1413 tvfsDlOpen, /* xDlOpen */ | |
| 1414 tvfsDlError, /* xDlError */ | |
| 1415 tvfsDlSym, /* xDlSym */ | |
| 1416 tvfsDlClose, /* xDlClose */ | |
| 1417 #else | |
| 1418 0, /* xDlOpen */ | |
| 1419 0, /* xDlError */ | |
| 1420 0, /* xDlSym */ | |
| 1421 0, /* xDlClose */ | |
| 1422 #endif /* SQLITE_OMIT_LOAD_EXTENSION */ | |
| 1423 tvfsRandomness, /* xRandomness */ | |
| 1424 tvfsSleep, /* xSleep */ | |
| 1425 tvfsCurrentTime, /* xCurrentTime */ | |
| 1426 0, /* xGetLastError */ | |
| 1427 0, /* xCurrentTimeInt64 */ | |
| 1428 0, /* xSetSystemCall */ | |
| 1429 0, /* xGetSystemCall */ | |
| 1430 0, /* xNextSystemCall */ | |
| 1431 }; | |
| 1432 | |
| 1433 Testvfs *p; /* New object */ | |
| 1434 sqlite3_vfs *pVfs; /* New VFS */ | |
| 1435 char *zVfs; | |
| 1436 int nByte; /* Bytes of space to allocate at p */ | |
| 1437 | |
| 1438 int i; | |
| 1439 int isNoshm = 0; /* True if -noshm is passed */ | |
| 1440 int isFullshm = 0; /* True if -fullshm is passed */ | |
| 1441 int isDefault = 0; /* True if -default is passed */ | |
| 1442 int szOsFile = 0; /* Value passed to -szosfile */ | |
| 1443 int mxPathname = -1; /* Value passed to -mxpathname */ | |
| 1444 int iVersion = 3; /* Value passed to -iversion */ | |
| 1445 | |
| 1446 if( objc<2 || 0!=(objc%2) ) goto bad_args; | |
| 1447 for(i=2; i<objc; i += 2){ | |
| 1448 int nSwitch; | |
| 1449 char *zSwitch; | |
| 1450 zSwitch = Tcl_GetStringFromObj(objv[i], &nSwitch); | |
| 1451 | |
| 1452 if( nSwitch>2 && 0==strncmp("-noshm", zSwitch, nSwitch) ){ | |
| 1453 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &isNoshm) ){ | |
| 1454 return TCL_ERROR; | |
| 1455 } | |
| 1456 if( isNoshm ) isFullshm = 0; | |
| 1457 } | |
| 1458 else if( nSwitch>2 && 0==strncmp("-default", zSwitch, nSwitch) ){ | |
| 1459 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &isDefault) ){ | |
| 1460 return TCL_ERROR; | |
| 1461 } | |
| 1462 } | |
| 1463 else if( nSwitch>2 && 0==strncmp("-szosfile", zSwitch, nSwitch) ){ | |
| 1464 if( Tcl_GetIntFromObj(interp, objv[i+1], &szOsFile) ){ | |
| 1465 return TCL_ERROR; | |
| 1466 } | |
| 1467 } | |
| 1468 else if( nSwitch>2 && 0==strncmp("-mxpathname", zSwitch, nSwitch) ){ | |
| 1469 if( Tcl_GetIntFromObj(interp, objv[i+1], &mxPathname) ){ | |
| 1470 return TCL_ERROR; | |
| 1471 } | |
| 1472 } | |
| 1473 else if( nSwitch>2 && 0==strncmp("-iversion", zSwitch, nSwitch) ){ | |
| 1474 if( Tcl_GetIntFromObj(interp, objv[i+1], &iVersion) ){ | |
| 1475 return TCL_ERROR; | |
| 1476 } | |
| 1477 } | |
| 1478 else if( nSwitch>2 && 0==strncmp("-fullshm", zSwitch, nSwitch) ){ | |
| 1479 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &isFullshm) ){ | |
| 1480 return TCL_ERROR; | |
| 1481 } | |
| 1482 if( isFullshm ) isNoshm = 0; | |
| 1483 } | |
| 1484 else{ | |
| 1485 goto bad_args; | |
| 1486 } | |
| 1487 } | |
| 1488 | |
| 1489 if( szOsFile<sizeof(TestvfsFile) ){ | |
| 1490 szOsFile = sizeof(TestvfsFile); | |
| 1491 } | |
| 1492 | |
| 1493 zVfs = Tcl_GetString(objv[1]); | |
| 1494 nByte = sizeof(Testvfs) + (int)strlen(zVfs)+1; | |
| 1495 p = (Testvfs *)ckalloc(nByte); | |
| 1496 memset(p, 0, nByte); | |
| 1497 p->iDevchar = -1; | |
| 1498 p->iSectorsize = -1; | |
| 1499 | |
| 1500 /* Create the new object command before querying SQLite for a default VFS | |
| 1501 ** to use for 'real' IO operations. This is because creating the new VFS | |
| 1502 ** may delete an existing [testvfs] VFS of the same name. If such a VFS | |
| 1503 ** is currently the default, the new [testvfs] may end up calling the | |
| 1504 ** methods of a deleted object. | |
| 1505 */ | |
| 1506 Tcl_CreateObjCommand(interp, zVfs, testvfs_obj_cmd, p, testvfs_obj_del); | |
| 1507 p->pParent = sqlite3_vfs_find(0); | |
| 1508 p->interp = interp; | |
| 1509 | |
| 1510 p->zName = (char *)&p[1]; | |
| 1511 memcpy(p->zName, zVfs, strlen(zVfs)+1); | |
| 1512 | |
| 1513 pVfs = (sqlite3_vfs *)ckalloc(sizeof(sqlite3_vfs)); | |
| 1514 memcpy(pVfs, &tvfs_vfs, sizeof(sqlite3_vfs)); | |
| 1515 pVfs->pAppData = (void *)p; | |
| 1516 pVfs->iVersion = iVersion; | |
| 1517 pVfs->zName = p->zName; | |
| 1518 pVfs->mxPathname = p->pParent->mxPathname; | |
| 1519 if( mxPathname>=0 && mxPathname<pVfs->mxPathname ){ | |
| 1520 pVfs->mxPathname = mxPathname; | |
| 1521 } | |
| 1522 pVfs->szOsFile = szOsFile; | |
| 1523 p->pVfs = pVfs; | |
| 1524 p->isNoshm = isNoshm; | |
| 1525 p->isFullshm = isFullshm; | |
| 1526 p->mask = TESTVFS_ALL_MASK; | |
| 1527 | |
| 1528 sqlite3_vfs_register(pVfs, isDefault); | |
| 1529 | |
| 1530 return TCL_OK; | |
| 1531 | |
| 1532 bad_args: | |
| 1533 Tcl_WrongNumArgs(interp, 1, objv, "VFSNAME ?-noshm BOOL? ?-fullshm BOOL? ?-def
ault BOOL? ?-mxpathname INT? ?-szosfile INT? ?-iversion INT?"); | |
| 1534 return TCL_ERROR; | |
| 1535 } | |
| 1536 | |
| 1537 int Sqlitetestvfs_Init(Tcl_Interp *interp){ | |
| 1538 Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0); | |
| 1539 return TCL_OK; | |
| 1540 } | |
| 1541 | |
| 1542 #endif | |
| OLD | NEW |