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