xref: /sqlite-3.40.0/src/test_devsym.c (revision b7e8ea20)
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 int devsymShmOpen(sqlite3_vfs *, const char *, sqlite3_shm **);
72 static int devsymShmSize(sqlite3_shm *, int , int *);
73 static int devsymShmGet(sqlite3_shm *, int , int *, void **);
74 static int devsymShmRelease(sqlite3_shm *);
75 static int devsymShmLock(sqlite3_shm *, int , int *);
76 static int devsymShmClose(sqlite3_shm *, int);
77 
78 static sqlite3_vfs devsym_vfs = {
79   2,                     /* iVersion */
80   sizeof(devsym_file),      /* szOsFile */
81   DEVSYM_MAX_PATHNAME,      /* mxPathname */
82   0,                     /* pNext */
83   DEVSYM_VFS_NAME,          /* zName */
84   0,                     /* pAppData */
85   devsymOpen,               /* xOpen */
86   devsymDelete,             /* xDelete */
87   devsymAccess,             /* xAccess */
88   devsymFullPathname,       /* xFullPathname */
89 #ifndef SQLITE_OMIT_LOAD_EXTENSION
90   devsymDlOpen,             /* xDlOpen */
91   devsymDlError,            /* xDlError */
92   devsymDlSym,              /* xDlSym */
93   devsymDlClose,            /* xDlClose */
94 #else
95   0,                        /* xDlOpen */
96   0,                        /* xDlError */
97   0,                        /* xDlSym */
98   0,                        /* xDlClose */
99 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
100   devsymRandomness,         /* xRandomness */
101   devsymSleep,              /* xSleep */
102   devsymCurrentTime,        /* xCurrentTime */
103   0,                        /* xGetLastError */
104   devsymShmOpen,
105   devsymShmSize,
106   devsymShmGet,
107   devsymShmRelease,
108   devsymShmLock,
109   devsymShmClose,
110   0,
111   0,
112 };
113 
114 static sqlite3_io_methods devsym_io_methods = {
115   1,                            /* iVersion */
116   devsymClose,                      /* xClose */
117   devsymRead,                       /* xRead */
118   devsymWrite,                      /* xWrite */
119   devsymTruncate,                   /* xTruncate */
120   devsymSync,                       /* xSync */
121   devsymFileSize,                   /* xFileSize */
122   devsymLock,                       /* xLock */
123   devsymUnlock,                     /* xUnlock */
124   devsymCheckReservedLock,          /* xCheckReservedLock */
125   devsymFileControl,                /* xFileControl */
126   devsymSectorSize,                 /* xSectorSize */
127   devsymDeviceCharacteristics       /* xDeviceCharacteristics */
128 };
129 
130 struct DevsymGlobal {
131   sqlite3_vfs *pVfs;
132   int iDeviceChar;
133   int iSectorSize;
134 };
135 struct DevsymGlobal g = {0, 0, 512};
136 
137 /*
138 ** Close an devsym-file.
139 */
140 static int devsymClose(sqlite3_file *pFile){
141   devsym_file *p = (devsym_file *)pFile;
142   return sqlite3OsClose(p->pReal);
143 }
144 
145 /*
146 ** Read data from an devsym-file.
147 */
148 static int devsymRead(
149   sqlite3_file *pFile,
150   void *zBuf,
151   int iAmt,
152   sqlite_int64 iOfst
153 ){
154   devsym_file *p = (devsym_file *)pFile;
155   return sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
156 }
157 
158 /*
159 ** Write data to an devsym-file.
160 */
161 static int devsymWrite(
162   sqlite3_file *pFile,
163   const void *zBuf,
164   int iAmt,
165   sqlite_int64 iOfst
166 ){
167   devsym_file *p = (devsym_file *)pFile;
168   return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
169 }
170 
171 /*
172 ** Truncate an devsym-file.
173 */
174 static int devsymTruncate(sqlite3_file *pFile, sqlite_int64 size){
175   devsym_file *p = (devsym_file *)pFile;
176   return sqlite3OsTruncate(p->pReal, size);
177 }
178 
179 /*
180 ** Sync an devsym-file.
181 */
182 static int devsymSync(sqlite3_file *pFile, int flags){
183   devsym_file *p = (devsym_file *)pFile;
184   return sqlite3OsSync(p->pReal, flags);
185 }
186 
187 /*
188 ** Return the current file-size of an devsym-file.
189 */
190 static int devsymFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
191   devsym_file *p = (devsym_file *)pFile;
192   return sqlite3OsFileSize(p->pReal, pSize);
193 }
194 
195 /*
196 ** Lock an devsym-file.
197 */
198 static int devsymLock(sqlite3_file *pFile, int eLock){
199   devsym_file *p = (devsym_file *)pFile;
200   return sqlite3OsLock(p->pReal, eLock);
201 }
202 
203 /*
204 ** Unlock an devsym-file.
205 */
206 static int devsymUnlock(sqlite3_file *pFile, int eLock){
207   devsym_file *p = (devsym_file *)pFile;
208   return sqlite3OsUnlock(p->pReal, eLock);
209 }
210 
211 /*
212 ** Check if another file-handle holds a RESERVED lock on an devsym-file.
213 */
214 static int devsymCheckReservedLock(sqlite3_file *pFile, int *pResOut){
215   devsym_file *p = (devsym_file *)pFile;
216   return sqlite3OsCheckReservedLock(p->pReal, pResOut);
217 }
218 
219 /*
220 ** File control method. For custom operations on an devsym-file.
221 */
222 static int devsymFileControl(sqlite3_file *pFile, int op, void *pArg){
223   devsym_file *p = (devsym_file *)pFile;
224   return sqlite3OsFileControl(p->pReal, op, pArg);
225 }
226 
227 /*
228 ** Return the sector-size in bytes for an devsym-file.
229 */
230 static int devsymSectorSize(sqlite3_file *pFile){
231   return g.iSectorSize;
232 }
233 
234 /*
235 ** Return the device characteristic flags supported by an devsym-file.
236 */
237 static int devsymDeviceCharacteristics(sqlite3_file *pFile){
238   return g.iDeviceChar;
239 }
240 
241 /*
242 ** Open an devsym file handle.
243 */
244 static int devsymOpen(
245   sqlite3_vfs *pVfs,
246   const char *zName,
247   sqlite3_file *pFile,
248   int flags,
249   int *pOutFlags
250 ){
251   int rc;
252   devsym_file *p = (devsym_file *)pFile;
253   p->pReal = (sqlite3_file *)&p[1];
254   rc = sqlite3OsOpen(g.pVfs, zName, p->pReal, flags, pOutFlags);
255   if( p->pReal->pMethods ){
256     pFile->pMethods = &devsym_io_methods;
257   }
258   return rc;
259 }
260 
261 /*
262 ** Delete the file located at zPath. If the dirSync argument is true,
263 ** ensure the file-system modifications are synced to disk before
264 ** returning.
265 */
266 static int devsymDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
267   return sqlite3OsDelete(g.pVfs, zPath, dirSync);
268 }
269 
270 /*
271 ** Test for access permissions. Return true if the requested permission
272 ** is available, or false otherwise.
273 */
274 static int devsymAccess(
275   sqlite3_vfs *pVfs,
276   const char *zPath,
277   int flags,
278   int *pResOut
279 ){
280   return sqlite3OsAccess(g.pVfs, zPath, flags, pResOut);
281 }
282 
283 /*
284 ** Populate buffer zOut with the full canonical pathname corresponding
285 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
286 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
287 */
288 static int devsymFullPathname(
289   sqlite3_vfs *pVfs,
290   const char *zPath,
291   int nOut,
292   char *zOut
293 ){
294   return sqlite3OsFullPathname(g.pVfs, zPath, nOut, zOut);
295 }
296 
297 #ifndef SQLITE_OMIT_LOAD_EXTENSION
298 /*
299 ** Open the dynamic library located at zPath and return a handle.
300 */
301 static void *devsymDlOpen(sqlite3_vfs *pVfs, const char *zPath){
302   return sqlite3OsDlOpen(g.pVfs, zPath);
303 }
304 
305 /*
306 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
307 ** utf-8 string describing the most recent error encountered associated
308 ** with dynamic libraries.
309 */
310 static void devsymDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
311   sqlite3OsDlError(g.pVfs, nByte, zErrMsg);
312 }
313 
314 /*
315 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
316 */
317 static void (*devsymDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
318   return sqlite3OsDlSym(g.pVfs, p, zSym);
319 }
320 
321 /*
322 ** Close the dynamic library handle pHandle.
323 */
324 static void devsymDlClose(sqlite3_vfs *pVfs, void *pHandle){
325   sqlite3OsDlClose(g.pVfs, pHandle);
326 }
327 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
328 
329 /*
330 ** Populate the buffer pointed to by zBufOut with nByte bytes of
331 ** random data.
332 */
333 static int devsymRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
334   return sqlite3OsRandomness(g.pVfs, nByte, zBufOut);
335 }
336 
337 /*
338 ** Sleep for nMicro microseconds. Return the number of microseconds
339 ** actually slept.
340 */
341 static int devsymSleep(sqlite3_vfs *pVfs, int nMicro){
342   return sqlite3OsSleep(g.pVfs, nMicro);
343 }
344 
345 /*
346 ** Return the current time as a Julian Day number in *pTimeOut.
347 */
348 static int devsymCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
349   return g.pVfs->xCurrentTime(g.pVfs, pTimeOut);
350 }
351 
352 
353 static int devsymShmOpen(
354   sqlite3_vfs *pVfs,
355   const char *zName,
356   sqlite3_shm **pp
357 ){
358   return g.pVfs->xShmOpen(g.pVfs, zName, pp);
359 }
360 static int devsymShmSize(sqlite3_shm *p, int reqSize, int *pNewSize){
361   return g.pVfs->xShmSize(p, reqSize, pNewSize);
362 }
363 static int devsymShmGet(
364   sqlite3_shm *p,
365   int reqMapSize,
366   int *pMapSize,
367   void **pp
368 ){
369   return g.pVfs->xShmGet(p, reqMapSize, pMapSize, pp);
370 }
371 static int devsymShmRelease(sqlite3_shm *p){
372   return g.pVfs->xShmRelease(p);
373 }
374 static int devsymShmLock(sqlite3_shm *p, int desiredLock, int *gotLock){
375   return g.pVfs->xShmLock(p, desiredLock, gotLock);
376 }
377 static int devsymShmClose(sqlite3_shm *p, int deleteFlag){
378   return g.pVfs->xShmClose(p, deleteFlag);
379 }
380 
381 /*
382 ** This procedure registers the devsym vfs with SQLite. If the argument is
383 ** true, the devsym vfs becomes the new default vfs. It is the only publicly
384 ** available function in this file.
385 */
386 void devsym_register(int iDeviceChar, int iSectorSize){
387   if( g.pVfs==0 ){
388     g.pVfs = sqlite3_vfs_find(0);
389     devsym_vfs.szOsFile += g.pVfs->szOsFile;
390     devsym_vfs.xShmOpen = (g.pVfs->xShmOpen ? devsymShmOpen : 0);
391     devsym_vfs.xShmSize = (g.pVfs->xShmSize ? devsymShmSize : 0);
392     devsym_vfs.xShmGet = (g.pVfs->xShmGet ? devsymShmGet : 0);
393     devsym_vfs.xShmRelease = (g.pVfs->xShmRelease ? devsymShmRelease : 0);
394     devsym_vfs.xShmLock = (g.pVfs->xShmLock ? devsymShmLock : 0);
395     devsym_vfs.xShmClose = (g.pVfs->xShmClose ? devsymShmClose : 0);
396     sqlite3_vfs_register(&devsym_vfs, 0);
397   }
398   if( iDeviceChar>=0 ){
399     g.iDeviceChar = iDeviceChar;
400   }else{
401     g.iDeviceChar = 0;
402   }
403   if( iSectorSize>=0 ){
404     g.iSectorSize = iSectorSize;
405   }else{
406     g.iSectorSize = 512;
407   }
408 }
409 
410 #endif
411