4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 ******************************************************************************
13 ** This file implements an in-memory VFS. A database is held as a contiguous
16 ** This file also implements interface sqlite3_serialize() and
17 ** sqlite3_deserialize().
19 #include "sqliteInt.h"
20 #ifdef SQLITE_ENABLE_DESERIALIZE
23 ** Forward declaration of objects used by this utility
25 typedef struct sqlite3_vfs MemVfs
;
26 typedef struct MemFile MemFile
;
28 /* Access to a lower-level VFS that (might) implement dynamic loading,
29 ** access to randomness, etc.
31 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
35 sqlite3_file base
; /* IO methods */
36 sqlite3_int64 sz
; /* Size of the file */
37 sqlite3_int64 szAlloc
; /* Space allocated to aData */
38 sqlite3_int64 szMax
; /* Maximum allowed size of the file */
39 unsigned char *aData
; /* content of the file */
40 int nMmap
; /* Number of memory mapped pages */
41 unsigned mFlags
; /* Flags */
42 int eLock
; /* Most recent lock against this file */
46 ** Methods for MemFile
48 static int memdbClose(sqlite3_file
*);
49 static int memdbRead(sqlite3_file
*, void*, int iAmt
, sqlite3_int64 iOfst
);
50 static int memdbWrite(sqlite3_file
*,const void*,int iAmt
, sqlite3_int64 iOfst
);
51 static int memdbTruncate(sqlite3_file
*, sqlite3_int64 size
);
52 static int memdbSync(sqlite3_file
*, int flags
);
53 static int memdbFileSize(sqlite3_file
*, sqlite3_int64
*pSize
);
54 static int memdbLock(sqlite3_file
*, int);
55 /* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
56 static int memdbFileControl(sqlite3_file
*, int op
, void *pArg
);
57 /* static int memdbSectorSize(sqlite3_file*); // not used */
58 static int memdbDeviceCharacteristics(sqlite3_file
*);
59 static int memdbFetch(sqlite3_file
*, sqlite3_int64 iOfst
, int iAmt
, void **pp
);
60 static int memdbUnfetch(sqlite3_file
*, sqlite3_int64 iOfst
, void *p
);
65 static int memdbOpen(sqlite3_vfs
*, const char *, sqlite3_file
*, int , int *);
66 /* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
67 static int memdbAccess(sqlite3_vfs
*, const char *zName
, int flags
, int *);
68 static int memdbFullPathname(sqlite3_vfs
*, const char *zName
, int, char *zOut
);
69 static void *memdbDlOpen(sqlite3_vfs
*, const char *zFilename
);
70 static void memdbDlError(sqlite3_vfs
*, int nByte
, char *zErrMsg
);
71 static void (*memdbDlSym(sqlite3_vfs
*pVfs
, void *p
, const char*zSym
))(void);
72 static void memdbDlClose(sqlite3_vfs
*, void*);
73 static int memdbRandomness(sqlite3_vfs
*, int nByte
, char *zOut
);
74 static int memdbSleep(sqlite3_vfs
*, int microseconds
);
75 /* static int memdbCurrentTime(sqlite3_vfs*, double*); */
76 static int memdbGetLastError(sqlite3_vfs
*, int, char *);
77 static int memdbCurrentTimeInt64(sqlite3_vfs
*, sqlite3_int64
*);
79 static sqlite3_vfs memdb_vfs
= {
81 0, /* szOsFile (set when registered) */
82 1024, /* mxPathname */
85 0, /* pAppData (set when registered) */
86 memdbOpen
, /* xOpen */
87 0, /* memdbDelete, */ /* xDelete */
88 memdbAccess
, /* xAccess */
89 memdbFullPathname
, /* xFullPathname */
90 memdbDlOpen
, /* xDlOpen */
91 memdbDlError
, /* xDlError */
92 memdbDlSym
, /* xDlSym */
93 memdbDlClose
, /* xDlClose */
94 memdbRandomness
, /* xRandomness */
95 memdbSleep
, /* xSleep */
96 0, /* memdbCurrentTime, */ /* xCurrentTime */
97 memdbGetLastError
, /* xGetLastError */
98 memdbCurrentTimeInt64
/* xCurrentTimeInt64 */
101 static const sqlite3_io_methods memdb_io_methods
= {
103 memdbClose
, /* xClose */
104 memdbRead
, /* xRead */
105 memdbWrite
, /* xWrite */
106 memdbTruncate
, /* xTruncate */
107 memdbSync
, /* xSync */
108 memdbFileSize
, /* xFileSize */
109 memdbLock
, /* xLock */
110 memdbLock
, /* xUnlock - same as xLock in this case */
111 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
112 memdbFileControl
, /* xFileControl */
113 0, /* memdbSectorSize,*/ /* xSectorSize */
114 memdbDeviceCharacteristics
, /* xDeviceCharacteristics */
119 memdbFetch
, /* xFetch */
120 memdbUnfetch
/* xUnfetch */
126 ** Close an memdb-file.
128 ** The pData pointer is owned by the application, so there is nothing
131 static int memdbClose(sqlite3_file
*pFile
){
132 MemFile
*p
= (MemFile
*)pFile
;
133 if( p
->mFlags
& SQLITE_DESERIALIZE_FREEONCLOSE
) sqlite3_free(p
->aData
);
138 ** Read data from an memdb-file.
140 static int memdbRead(
146 MemFile
*p
= (MemFile
*)pFile
;
147 if( iOfst
+iAmt
>p
->sz
){
148 memset(zBuf
, 0, iAmt
);
149 if( iOfst
<p
->sz
) memcpy(zBuf
, p
->aData
+iOfst
, p
->sz
- iOfst
);
150 return SQLITE_IOERR_SHORT_READ
;
152 memcpy(zBuf
, p
->aData
+iOfst
, iAmt
);
157 ** Try to enlarge the memory allocation to hold at least sz bytes
159 static int memdbEnlarge(MemFile
*p
, sqlite3_int64 newSz
){
161 if( (p
->mFlags
& SQLITE_DESERIALIZE_RESIZEABLE
)==0 || p
->nMmap
>0 ){
164 if( newSz
>p
->szMax
){
168 if( newSz
>p
->szMax
) newSz
= p
->szMax
;
169 pNew
= sqlite3_realloc64(p
->aData
, newSz
);
170 if( pNew
==0 ) return SQLITE_NOMEM
;
177 ** Write data to an memdb-file.
179 static int memdbWrite(
185 MemFile
*p
= (MemFile
*)pFile
;
186 if( NEVER(p
->mFlags
& SQLITE_DESERIALIZE_READONLY
) ) return SQLITE_READONLY
;
187 if( iOfst
+iAmt
>p
->sz
){
189 if( iOfst
+iAmt
>p
->szAlloc
190 && (rc
= memdbEnlarge(p
, iOfst
+iAmt
))!=SQLITE_OK
194 if( iOfst
>p
->sz
) memset(p
->aData
+p
->sz
, 0, iOfst
-p
->sz
);
197 memcpy(p
->aData
+iOfst
, z
, iAmt
);
202 ** Truncate an memdb-file.
204 ** In rollback mode (which is always the case for memdb, as it does not
205 ** support WAL mode) the truncate() method is only used to reduce
206 ** the size of a file, never to increase the size.
208 static int memdbTruncate(sqlite3_file
*pFile
, sqlite_int64 size
){
209 MemFile
*p
= (MemFile
*)pFile
;
210 if( NEVER(size
>p
->sz
) ) return SQLITE_FULL
;
216 ** Sync an memdb-file.
218 static int memdbSync(sqlite3_file
*pFile
, int flags
){
223 ** Return the current file-size of an memdb-file.
225 static int memdbFileSize(sqlite3_file
*pFile
, sqlite_int64
*pSize
){
226 MemFile
*p
= (MemFile
*)pFile
;
232 ** Lock an memdb-file.
234 static int memdbLock(sqlite3_file
*pFile
, int eLock
){
235 MemFile
*p
= (MemFile
*)pFile
;
236 if( eLock
>SQLITE_LOCK_SHARED
237 && (p
->mFlags
& SQLITE_DESERIALIZE_READONLY
)!=0
239 return SQLITE_READONLY
;
245 #if 0 /* Never used because memdbAccess() always returns false */
247 ** Check if another file-handle holds a RESERVED lock on an memdb-file.
249 static int memdbCheckReservedLock(sqlite3_file
*pFile
, int *pResOut
){
256 ** File control method. For custom operations on an memdb-file.
258 static int memdbFileControl(sqlite3_file
*pFile
, int op
, void *pArg
){
259 MemFile
*p
= (MemFile
*)pFile
;
260 int rc
= SQLITE_NOTFOUND
;
261 if( op
==SQLITE_FCNTL_VFSNAME
){
262 *(char**)pArg
= sqlite3_mprintf("memdb(%p,%lld)", p
->aData
, p
->sz
);
265 if( op
==SQLITE_FCNTL_SIZE_LIMIT
){
266 sqlite3_int64 iLimit
= *(sqlite3_int64
*)pArg
;
275 *(sqlite3_int64
*)pArg
= iLimit
;
281 #if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
283 ** Return the sector-size in bytes for an memdb-file.
285 static int memdbSectorSize(sqlite3_file
*pFile
){
291 ** Return the device characteristic flags supported by an memdb-file.
293 static int memdbDeviceCharacteristics(sqlite3_file
*pFile
){
294 return SQLITE_IOCAP_ATOMIC
|
295 SQLITE_IOCAP_POWERSAFE_OVERWRITE
|
296 SQLITE_IOCAP_SAFE_APPEND
|
297 SQLITE_IOCAP_SEQUENTIAL
;
300 /* Fetch a page of a memory-mapped file */
301 static int memdbFetch(
307 MemFile
*p
= (MemFile
*)pFile
;
308 if( iOfst
+iAmt
>p
->sz
){
312 *pp
= (void*)(p
->aData
+ iOfst
);
317 /* Release a memory-mapped page */
318 static int memdbUnfetch(sqlite3_file
*pFile
, sqlite3_int64 iOfst
, void *pPage
){
319 MemFile
*p
= (MemFile
*)pFile
;
325 ** Open an mem file handle.
327 static int memdbOpen(
334 MemFile
*p
= (MemFile
*)pFile
;
335 if( (flags
& SQLITE_OPEN_MAIN_DB
)==0 ){
336 return ORIGVFS(pVfs
)->xOpen(ORIGVFS(pVfs
), zName
, pFile
, flags
, pOutFlags
);
338 memset(p
, 0, sizeof(*p
));
339 p
->mFlags
= SQLITE_DESERIALIZE_RESIZEABLE
| SQLITE_DESERIALIZE_FREEONCLOSE
;
340 assert( pOutFlags
!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */
341 *pOutFlags
= flags
| SQLITE_OPEN_MEMORY
;
342 p
->base
.pMethods
= &memdb_io_methods
;
343 p
->szMax
= sqlite3GlobalConfig
.mxMemdbSize
;
347 #if 0 /* Only used to delete rollback journals, master journals, and WAL
348 ** files, none of which exist in memdb. So this routine is never used */
350 ** Delete the file located at zPath. If the dirSync argument is true,
351 ** ensure the file-system modifications are synced to disk before
354 static int memdbDelete(sqlite3_vfs
*pVfs
, const char *zPath
, int dirSync
){
355 return SQLITE_IOERR_DELETE
;
360 ** Test for access permissions. Return true if the requested permission
361 ** is available, or false otherwise.
363 ** With memdb, no files ever exist on disk. So always return false.
365 static int memdbAccess(
376 ** Populate buffer zOut with the full canonical pathname corresponding
377 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
378 ** of at least (INST_MAX_PATHNAME+1) bytes.
380 static int memdbFullPathname(
386 sqlite3_snprintf(nOut
, zOut
, "%s", zPath
);
391 ** Open the dynamic library located at zPath and return a handle.
393 static void *memdbDlOpen(sqlite3_vfs
*pVfs
, const char *zPath
){
394 return ORIGVFS(pVfs
)->xDlOpen(ORIGVFS(pVfs
), zPath
);
398 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
399 ** utf-8 string describing the most recent error encountered associated
400 ** with dynamic libraries.
402 static void memdbDlError(sqlite3_vfs
*pVfs
, int nByte
, char *zErrMsg
){
403 ORIGVFS(pVfs
)->xDlError(ORIGVFS(pVfs
), nByte
, zErrMsg
);
407 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
409 static void (*memdbDlSym(sqlite3_vfs
*pVfs
, void *p
, const char *zSym
))(void){
410 return ORIGVFS(pVfs
)->xDlSym(ORIGVFS(pVfs
), p
, zSym
);
414 ** Close the dynamic library handle pHandle.
416 static void memdbDlClose(sqlite3_vfs
*pVfs
, void *pHandle
){
417 ORIGVFS(pVfs
)->xDlClose(ORIGVFS(pVfs
), pHandle
);
421 ** Populate the buffer pointed to by zBufOut with nByte bytes of
424 static int memdbRandomness(sqlite3_vfs
*pVfs
, int nByte
, char *zBufOut
){
425 return ORIGVFS(pVfs
)->xRandomness(ORIGVFS(pVfs
), nByte
, zBufOut
);
429 ** Sleep for nMicro microseconds. Return the number of microseconds
432 static int memdbSleep(sqlite3_vfs
*pVfs
, int nMicro
){
433 return ORIGVFS(pVfs
)->xSleep(ORIGVFS(pVfs
), nMicro
);
436 #if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
438 ** Return the current time as a Julian Day number in *pTimeOut.
440 static int memdbCurrentTime(sqlite3_vfs
*pVfs
, double *pTimeOut
){
441 return ORIGVFS(pVfs
)->xCurrentTime(ORIGVFS(pVfs
), pTimeOut
);
445 static int memdbGetLastError(sqlite3_vfs
*pVfs
, int a
, char *b
){
446 return ORIGVFS(pVfs
)->xGetLastError(ORIGVFS(pVfs
), a
, b
);
448 static int memdbCurrentTimeInt64(sqlite3_vfs
*pVfs
, sqlite3_int64
*p
){
449 return ORIGVFS(pVfs
)->xCurrentTimeInt64(ORIGVFS(pVfs
), p
);
453 ** Translate a database connection pointer and schema name into a
456 static MemFile
*memdbFromDbSchema(sqlite3
*db
, const char *zSchema
){
458 int rc
= sqlite3_file_control(db
, zSchema
, SQLITE_FCNTL_FILE_POINTER
, &p
);
460 if( p
->base
.pMethods
!=&memdb_io_methods
) return 0;
465 ** Return the serialization of a database
467 unsigned char *sqlite3_serialize(
468 sqlite3
*db
, /* The database connection */
469 const char *zSchema
, /* Which database within the connection */
470 sqlite3_int64
*piSize
, /* Write size here, if not NULL */
471 unsigned int mFlags
/* Maybe SQLITE_SERIALIZE_NOCOPY */
478 sqlite3_stmt
*pStmt
= 0;
483 #ifdef SQLITE_ENABLE_API_ARMOR
484 if( !sqlite3SafetyCheckOk(db
) ){
485 (void)SQLITE_MISUSE_BKPT
;
490 if( zSchema
==0 ) zSchema
= db
->aDb
[0].zDbSName
;
491 p
= memdbFromDbSchema(db
, zSchema
);
492 iDb
= sqlite3FindDbName(db
, zSchema
);
493 if( piSize
) *piSize
= -1;
494 if( iDb
<0 ) return 0;
496 if( piSize
) *piSize
= p
->sz
;
497 if( mFlags
& SQLITE_SERIALIZE_NOCOPY
){
500 pOut
= sqlite3_malloc64( p
->sz
);
501 if( pOut
) memcpy(pOut
, p
->aData
, p
->sz
);
505 pBt
= db
->aDb
[iDb
].pBt
;
506 if( pBt
==0 ) return 0;
507 szPage
= sqlite3BtreeGetPageSize(pBt
);
508 zSql
= sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema
);
509 rc
= zSql
? sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0) : SQLITE_NOMEM
;
512 rc
= sqlite3_step(pStmt
);
513 if( rc
!=SQLITE_ROW
){
516 sz
= sqlite3_column_int64(pStmt
, 0)*szPage
;
517 if( piSize
) *piSize
= sz
;
518 if( mFlags
& SQLITE_SERIALIZE_NOCOPY
){
521 pOut
= sqlite3_malloc64( sz
);
523 int nPage
= sqlite3_column_int(pStmt
, 0);
524 Pager
*pPager
= sqlite3BtreePager(pBt
);
526 for(pgno
=1; pgno
<=nPage
; pgno
++){
528 unsigned char *pTo
= pOut
+ szPage
*(sqlite3_int64
)(pgno
-1);
529 rc
= sqlite3PagerGet(pPager
, pgno
, (DbPage
**)&pPage
, 0);
531 memcpy(pTo
, sqlite3PagerGetData(pPage
), szPage
);
533 memset(pTo
, 0, szPage
);
535 sqlite3PagerUnref(pPage
);
540 sqlite3_finalize(pStmt
);
544 /* Convert zSchema to a MemDB and initialize its content.
546 int sqlite3_deserialize(
547 sqlite3
*db
, /* The database connection */
548 const char *zSchema
, /* Which DB to reopen with the deserialization */
549 unsigned char *pData
, /* The serialized database content */
550 sqlite3_int64 szDb
, /* Number bytes in the deserialization */
551 sqlite3_int64 szBuf
, /* Total size of buffer pData[] */
552 unsigned mFlags
/* Zero or more SQLITE_DESERIALIZE_* flags */
556 sqlite3_stmt
*pStmt
= 0;
560 #ifdef SQLITE_ENABLE_API_ARMOR
561 if( !sqlite3SafetyCheckOk(db
) ){
562 return SQLITE_MISUSE_BKPT
;
564 if( szDb
<0 ) return SQLITE_MISUSE_BKPT
;
565 if( szBuf
<0 ) return SQLITE_MISUSE_BKPT
;
568 sqlite3_mutex_enter(db
->mutex
);
569 if( zSchema
==0 ) zSchema
= db
->aDb
[0].zDbSName
;
570 iDb
= sqlite3FindDbName(db
, zSchema
);
573 goto end_deserialize
;
575 zSql
= sqlite3_mprintf("ATTACH x AS %Q", zSchema
);
576 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
578 if( rc
) goto end_deserialize
;
579 db
->init
.iDb
= (u8
)iDb
;
580 db
->init
.reopenMemdb
= 1;
581 rc
= sqlite3_step(pStmt
);
582 db
->init
.reopenMemdb
= 0;
583 if( rc
!=SQLITE_DONE
){
585 goto end_deserialize
;
587 p
= memdbFromDbSchema(db
, zSchema
);
595 if( p
->szMax
<sqlite3GlobalConfig
.mxMemdbSize
){
596 p
->szMax
= sqlite3GlobalConfig
.mxMemdbSize
;
603 sqlite3_finalize(pStmt
);
604 sqlite3_mutex_leave(db
->mutex
);
609 ** This routine is called when the extension is loaded.
610 ** Register the new VFS.
612 int sqlite3MemdbInit(void){
613 sqlite3_vfs
*pLower
= sqlite3_vfs_find(0);
614 int sz
= pLower
->szOsFile
;
615 memdb_vfs
.pAppData
= pLower
;
616 /* In all known configurations of SQLite, the size of a default
617 ** sqlite3_file is greater than the size of a memdb sqlite3_file.
618 ** Should that ever change, remove the following NEVER() */
619 if( NEVER(sz
<sizeof(MemFile
)) ) sz
= sizeof(MemFile
);
620 memdb_vfs
.szOsFile
= sz
;
621 return sqlite3_vfs_register(&memdb_vfs
, 0);
623 #endif /* SQLITE_ENABLE_DESERIALIZE */