test_devsym.c 11 KB

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