1 /* 2 ** 2008 Jan 22 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 code that modified the OS layer in order to simulate 14 ** different device types (by overriding the return values of the 15 ** xDeviceCharacteristics() and xSectorSize() methods). 16 */ 17 #if SQLITE_TEST /* This file is used for testing only */ 18 19 #include "sqlite3.h" 20 #include "sqliteInt.h" 21 22 /* 23 ** Maximum pathname length supported by the devsym backend. 24 */ 25 #define DEVSYM_MAX_PATHNAME 512 26 27 /* 28 ** Name used to identify this VFS. 29 */ 30 #define DEVSYM_VFS_NAME "devsym" 31 32 typedef struct devsym_file devsym_file; 33 struct devsym_file { 34 sqlite3_file base; 35 sqlite3_file *pReal; 36 }; 37 38 /* 39 ** Method declarations for devsym_file. 40 */ 41 static int devsymClose(sqlite3_file*); 42 static int devsymRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); 43 static int devsymWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); 44 static int devsymTruncate(sqlite3_file*, sqlite3_int64 size); 45 static int devsymSync(sqlite3_file*, int flags); 46 static int devsymFileSize(sqlite3_file*, sqlite3_int64 *pSize); 47 static int devsymLock(sqlite3_file*, int); 48 static int devsymUnlock(sqlite3_file*, int); 49 static int devsymCheckReservedLock(sqlite3_file*, int *); 50 static int devsymFileControl(sqlite3_file*, int op, void *pArg); 51 static int devsymSectorSize(sqlite3_file*); 52 static int devsymDeviceCharacteristics(sqlite3_file*); 53 54 /* 55 ** Method declarations for devsym_vfs. 56 */ 57 static int devsymOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); 58 static int devsymDelete(sqlite3_vfs*, const char *zName, int syncDir); 59 static int devsymAccess(sqlite3_vfs*, const char *zName, int flags, int *); 60 static int devsymFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); 61 #ifndef SQLITE_OMIT_LOAD_EXTENSION 62 static void *devsymDlOpen(sqlite3_vfs*, const char *zFilename); 63 static void devsymDlError(sqlite3_vfs*, int nByte, char *zErrMsg); 64 static void (*devsymDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void); 65 static void devsymDlClose(sqlite3_vfs*, void*); 66 #endif /* SQLITE_OMIT_LOAD_EXTENSION */ 67 static int devsymRandomness(sqlite3_vfs*, int nByte, char *zOut); 68 static int devsymSleep(sqlite3_vfs*, int microseconds); 69 static int devsymCurrentTime(sqlite3_vfs*, double*); 70 71 static sqlite3_vfs devsym_vfs = { 72 1, /* iVersion */ 73 sizeof(devsym_file), /* szOsFile */ 74 DEVSYM_MAX_PATHNAME, /* mxPathname */ 75 0, /* pNext */ 76 DEVSYM_VFS_NAME, /* zName */ 77 0, /* pAppData */ 78 devsymOpen, /* xOpen */ 79 devsymDelete, /* xDelete */ 80 devsymAccess, /* xAccess */ 81 devsymFullPathname, /* xFullPathname */ 82 #ifndef SQLITE_OMIT_LOAD_EXTENSION 83 devsymDlOpen, /* xDlOpen */ 84 devsymDlError, /* xDlError */ 85 devsymDlSym, /* xDlSym */ 86 devsymDlClose, /* xDlClose */ 87 #else 88 0, /* xDlOpen */ 89 0, /* xDlError */ 90 0, /* xDlSym */ 91 0, /* xDlClose */ 92 #endif /* SQLITE_OMIT_LOAD_EXTENSION */ 93 devsymRandomness, /* xRandomness */ 94 devsymSleep, /* xSleep */ 95 devsymCurrentTime /* xCurrentTime */ 96 }; 97 98 static sqlite3_io_methods devsym_io_methods = { 99 1, /* iVersion */ 100 devsymClose, /* xClose */ 101 devsymRead, /* xRead */ 102 devsymWrite, /* xWrite */ 103 devsymTruncate, /* xTruncate */ 104 devsymSync, /* xSync */ 105 devsymFileSize, /* xFileSize */ 106 devsymLock, /* xLock */ 107 devsymUnlock, /* xUnlock */ 108 devsymCheckReservedLock, /* xCheckReservedLock */ 109 devsymFileControl, /* xFileControl */ 110 devsymSectorSize, /* xSectorSize */ 111 devsymDeviceCharacteristics /* xDeviceCharacteristics */ 112 }; 113 114 struct DevsymGlobal { 115 sqlite3_vfs *pVfs; 116 int iDeviceChar; 117 int iSectorSize; 118 }; 119 struct DevsymGlobal g = {0, 0, 512}; 120 121 /* 122 ** Close an devsym-file. 123 */ 124 static int devsymClose(sqlite3_file *pFile){ 125 devsym_file *p = (devsym_file *)pFile; 126 return sqlite3OsClose(p->pReal); 127 } 128 129 /* 130 ** Read data from an devsym-file. 131 */ 132 static int devsymRead( 133 sqlite3_file *pFile, 134 void *zBuf, 135 int iAmt, 136 sqlite_int64 iOfst 137 ){ 138 devsym_file *p = (devsym_file *)pFile; 139 return sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst); 140 } 141 142 /* 143 ** Write data to an devsym-file. 144 */ 145 static int devsymWrite( 146 sqlite3_file *pFile, 147 const void *zBuf, 148 int iAmt, 149 sqlite_int64 iOfst 150 ){ 151 devsym_file *p = (devsym_file *)pFile; 152 return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst); 153 } 154 155 /* 156 ** Truncate an devsym-file. 157 */ 158 static int devsymTruncate(sqlite3_file *pFile, sqlite_int64 size){ 159 devsym_file *p = (devsym_file *)pFile; 160 return sqlite3OsTruncate(p->pReal, size); 161 } 162 163 /* 164 ** Sync an devsym-file. 165 */ 166 static int devsymSync(sqlite3_file *pFile, int flags){ 167 devsym_file *p = (devsym_file *)pFile; 168 return sqlite3OsSync(p->pReal, flags); 169 } 170 171 /* 172 ** Return the current file-size of an devsym-file. 173 */ 174 static int devsymFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 175 devsym_file *p = (devsym_file *)pFile; 176 return sqlite3OsFileSize(p->pReal, pSize); 177 } 178 179 /* 180 ** Lock an devsym-file. 181 */ 182 static int devsymLock(sqlite3_file *pFile, int eLock){ 183 devsym_file *p = (devsym_file *)pFile; 184 return sqlite3OsLock(p->pReal, eLock); 185 } 186 187 /* 188 ** Unlock an devsym-file. 189 */ 190 static int devsymUnlock(sqlite3_file *pFile, int eLock){ 191 devsym_file *p = (devsym_file *)pFile; 192 return sqlite3OsUnlock(p->pReal, eLock); 193 } 194 195 /* 196 ** Check if another file-handle holds a RESERVED lock on an devsym-file. 197 */ 198 static int devsymCheckReservedLock(sqlite3_file *pFile, int *pResOut){ 199 devsym_file *p = (devsym_file *)pFile; 200 return sqlite3OsCheckReservedLock(p->pReal, pResOut); 201 } 202 203 /* 204 ** File control method. For custom operations on an devsym-file. 205 */ 206 static int devsymFileControl(sqlite3_file *pFile, int op, void *pArg){ 207 devsym_file *p = (devsym_file *)pFile; 208 return sqlite3OsFileControl(p->pReal, op, pArg); 209 } 210 211 /* 212 ** Return the sector-size in bytes for an devsym-file. 213 */ 214 static int devsymSectorSize(sqlite3_file *pFile){ 215 return g.iSectorSize; 216 } 217 218 /* 219 ** Return the device characteristic flags supported by an devsym-file. 220 */ 221 static int devsymDeviceCharacteristics(sqlite3_file *pFile){ 222 return g.iDeviceChar; 223 } 224 225 /* 226 ** Open an devsym file handle. 227 */ 228 static int devsymOpen( 229 sqlite3_vfs *pVfs, 230 const char *zName, 231 sqlite3_file *pFile, 232 int flags, 233 int *pOutFlags 234 ){ 235 int rc; 236 devsym_file *p = (devsym_file *)pFile; 237 p->pReal = (sqlite3_file *)&p[1]; 238 rc = sqlite3OsOpen(g.pVfs, zName, p->pReal, flags, pOutFlags); 239 if( p->pReal->pMethods ){ 240 pFile->pMethods = &devsym_io_methods; 241 } 242 return rc; 243 } 244 245 /* 246 ** Delete the file located at zPath. If the dirSync argument is true, 247 ** ensure the file-system modifications are synced to disk before 248 ** returning. 249 */ 250 static int devsymDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ 251 return sqlite3OsDelete(g.pVfs, zPath, dirSync); 252 } 253 254 /* 255 ** Test for access permissions. Return true if the requested permission 256 ** is available, or false otherwise. 257 */ 258 static int devsymAccess( 259 sqlite3_vfs *pVfs, 260 const char *zPath, 261 int flags, 262 int *pResOut 263 ){ 264 return sqlite3OsAccess(g.pVfs, zPath, flags, pResOut); 265 } 266 267 /* 268 ** Populate buffer zOut with the full canonical pathname corresponding 269 ** to the pathname in zPath. zOut is guaranteed to point to a buffer 270 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes. 271 */ 272 static int devsymFullPathname( 273 sqlite3_vfs *pVfs, 274 const char *zPath, 275 int nOut, 276 char *zOut 277 ){ 278 return sqlite3OsFullPathname(g.pVfs, zPath, nOut, zOut); 279 } 280 281 #ifndef SQLITE_OMIT_LOAD_EXTENSION 282 /* 283 ** Open the dynamic library located at zPath and return a handle. 284 */ 285 static void *devsymDlOpen(sqlite3_vfs *pVfs, const char *zPath){ 286 return sqlite3OsDlOpen(g.pVfs, zPath); 287 } 288 289 /* 290 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable 291 ** utf-8 string describing the most recent error encountered associated 292 ** with dynamic libraries. 293 */ 294 static void devsymDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ 295 sqlite3OsDlError(g.pVfs, nByte, zErrMsg); 296 } 297 298 /* 299 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. 300 */ 301 static void (*devsymDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ 302 return sqlite3OsDlSym(g.pVfs, p, zSym); 303 } 304 305 /* 306 ** Close the dynamic library handle pHandle. 307 */ 308 static void devsymDlClose(sqlite3_vfs *pVfs, void *pHandle){ 309 sqlite3OsDlClose(g.pVfs, pHandle); 310 } 311 #endif /* SQLITE_OMIT_LOAD_EXTENSION */ 312 313 /* 314 ** Populate the buffer pointed to by zBufOut with nByte bytes of 315 ** random data. 316 */ 317 static int devsymRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ 318 return sqlite3OsRandomness(g.pVfs, nByte, zBufOut); 319 } 320 321 /* 322 ** Sleep for nMicro microseconds. Return the number of microseconds 323 ** actually slept. 324 */ 325 static int devsymSleep(sqlite3_vfs *pVfs, int nMicro){ 326 return sqlite3OsSleep(g.pVfs, nMicro); 327 } 328 329 /* 330 ** Return the current time as a Julian Day number in *pTimeOut. 331 */ 332 static int devsymCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ 333 return sqlite3OsCurrentTime(g.pVfs, pTimeOut); 334 } 335 336 /* 337 ** This procedure registers the devsym vfs with SQLite. If the argument is 338 ** true, the devsym vfs becomes the new default vfs. It is the only publicly 339 ** available function in this file. 340 */ 341 void devsym_register(int iDeviceChar, int iSectorSize){ 342 if( g.pVfs==0 ){ 343 g.pVfs = sqlite3_vfs_find(0); 344 devsym_vfs.szOsFile += g.pVfs->szOsFile; 345 sqlite3_vfs_register(&devsym_vfs, 0); 346 } 347 if( iDeviceChar>=0 ){ 348 g.iDeviceChar = iDeviceChar; 349 }else{ 350 g.iDeviceChar = 0; 351 } 352 if( iSectorSize>=0 ){ 353 g.iSectorSize = iSectorSize; 354 }else{ 355 g.iSectorSize = 512; 356 } 357 } 358 359 #endif 360