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