drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2016-09-07 |
| 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 | ** |
drh | 840fda4 | 2018-03-28 15:06:39 +0000 | [diff] [blame] | 13 | ** This file implements an in-memory VFS. A database is held as a contiguous |
drh | 99abe5c | 2018-01-03 22:48:38 +0000 | [diff] [blame] | 14 | ** block of memory. |
| 15 | ** |
| 16 | ** This file also implements interface sqlite3_serialize() and |
| 17 | ** sqlite3_deserialize(). |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 18 | */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 19 | #include "sqliteInt.h" |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 20 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | ** Forward declaration of objects used by this utility |
| 24 | */ |
| 25 | typedef struct sqlite3_vfs MemVfs; |
| 26 | typedef struct MemFile MemFile; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 27 | typedef struct MemStore MemStore; |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 28 | |
| 29 | /* Access to a lower-level VFS that (might) implement dynamic loading, |
| 30 | ** access to randomness, etc. |
| 31 | */ |
| 32 | #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) |
| 33 | |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 34 | /* Storage for a memdb file. |
| 35 | ** |
| 36 | ** An memdb object can be shared or separate. Shared memdb objects can be |
| 37 | ** used by more than one database connection. Mutexes are used by shared |
| 38 | ** memdb objects to coordinate access. Separate memdb objects are only |
| 39 | ** connected to a single database connection and do not require additional |
| 40 | ** mutexes. |
| 41 | ** |
| 42 | ** Shared memdb objects have .zFName!=0 and .pMutex!=0. They are created |
| 43 | ** using "file:/name?vfs=memdb". The first character of the name must be |
| 44 | ** "/" or else the object will be a separate memdb object. All shared |
| 45 | ** memdb objects are stored in memdb_g.apMemStore[] in an arbitrary order. |
| 46 | ** |
| 47 | ** Separate memdb objects are created using a name that does not begin |
| 48 | ** with "/" or using sqlite3_deserialize(). |
| 49 | ** |
| 50 | ** Access rules for shared MemStore objects: |
| 51 | ** |
| 52 | ** * .zFName is initialized when the object is created and afterwards |
| 53 | ** is unchanged until the object is destroyed. So it can be accessed |
| 54 | ** at any time as long as we know the object is not being destroyed, |
| 55 | ** which means while either the SQLITE_MUTEX_STATIC_VFS1 or |
| 56 | ** .pMutex is held or the object is not part of memdb_g.apMemStore[]. |
| 57 | ** |
| 58 | ** * Can .pMutex can only be changed while holding the |
| 59 | ** SQLITE_MUTEX_STATIC_VFS1 mutex or while the object is not part |
| 60 | ** of memdb_g.apMemStore[]. |
| 61 | ** |
| 62 | ** * Other fields can only be changed while holding the .pMutex mutex |
| 63 | ** or when the .nRef is less than zero and the object is not part of |
| 64 | ** memdb_g.apMemStore[]. |
| 65 | ** |
| 66 | ** * The .aData pointer has the added requirement that it can can only |
| 67 | ** be changed (for resizing) when nMmap is zero. |
| 68 | ** |
| 69 | */ |
| 70 | struct MemStore { |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 71 | sqlite3_int64 sz; /* Size of the file */ |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 72 | sqlite3_int64 szAlloc; /* Space allocated to aData */ |
| 73 | sqlite3_int64 szMax; /* Maximum allowed size of the file */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 74 | unsigned char *aData; /* content of the file */ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 75 | sqlite3_mutex *pMutex; /* Used by shared stores only */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 76 | int nMmap; /* Number of memory mapped pages */ |
| 77 | unsigned mFlags; /* Flags */ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 78 | int nRdLock; /* Number of readers */ |
| 79 | int nWrLock; /* Number of writers. (Always 0 or 1) */ |
| 80 | int nRef; /* Number of users of this MemStore */ |
| 81 | char *zFName; /* The filename for shared stores */ |
| 82 | }; |
| 83 | |
| 84 | /* An open file */ |
| 85 | struct MemFile { |
| 86 | sqlite3_file base; /* IO methods */ |
| 87 | MemStore *pStore; /* The storage */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 88 | int eLock; /* Most recent lock against this file */ |
| 89 | }; |
| 90 | |
| 91 | /* |
larrybr | 99bd552 | 2021-05-19 02:33:42 +0000 | [diff] [blame] | 92 | ** File-scope variables for holding the memdb files that are accessible |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 93 | ** to multiple database connections in separate threads. |
| 94 | ** |
| 95 | ** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object. |
| 96 | */ |
larrybr | 99bd552 | 2021-05-19 02:33:42 +0000 | [diff] [blame] | 97 | static struct MemFS { |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 98 | int nMemStore; /* Number of shared MemStore objects */ |
| 99 | MemStore **apMemStore; /* Array of all shared MemStore objects */ |
| 100 | } memdb_g; |
| 101 | |
| 102 | /* |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 103 | ** Methods for MemFile |
| 104 | */ |
| 105 | static int memdbClose(sqlite3_file*); |
| 106 | static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 107 | static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); |
| 108 | static int memdbTruncate(sqlite3_file*, sqlite3_int64 size); |
| 109 | static int memdbSync(sqlite3_file*, int flags); |
| 110 | static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize); |
| 111 | static int memdbLock(sqlite3_file*, int); |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 112 | static int memdbUnlock(sqlite3_file*, int); |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 113 | /* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 114 | static int memdbFileControl(sqlite3_file*, int op, void *pArg); |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 115 | /* static int memdbSectorSize(sqlite3_file*); // not used */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 116 | static int memdbDeviceCharacteristics(sqlite3_file*); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 117 | static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); |
| 118 | static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); |
| 119 | |
| 120 | /* |
| 121 | ** Methods for MemVfs |
| 122 | */ |
| 123 | static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 124 | /* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 125 | static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
| 126 | static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); |
| 127 | static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename); |
| 128 | static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg); |
| 129 | static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); |
| 130 | static void memdbDlClose(sqlite3_vfs*, void*); |
| 131 | static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut); |
| 132 | static int memdbSleep(sqlite3_vfs*, int microseconds); |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 133 | /* static int memdbCurrentTime(sqlite3_vfs*, double*); */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 134 | static int memdbGetLastError(sqlite3_vfs*, int, char *); |
| 135 | static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); |
| 136 | |
| 137 | static sqlite3_vfs memdb_vfs = { |
| 138 | 2, /* iVersion */ |
| 139 | 0, /* szOsFile (set when registered) */ |
| 140 | 1024, /* mxPathname */ |
| 141 | 0, /* pNext */ |
| 142 | "memdb", /* zName */ |
| 143 | 0, /* pAppData (set when registered) */ |
| 144 | memdbOpen, /* xOpen */ |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 145 | 0, /* memdbDelete, */ /* xDelete */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 146 | memdbAccess, /* xAccess */ |
| 147 | memdbFullPathname, /* xFullPathname */ |
| 148 | memdbDlOpen, /* xDlOpen */ |
| 149 | memdbDlError, /* xDlError */ |
| 150 | memdbDlSym, /* xDlSym */ |
| 151 | memdbDlClose, /* xDlClose */ |
| 152 | memdbRandomness, /* xRandomness */ |
| 153 | memdbSleep, /* xSleep */ |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 154 | 0, /* memdbCurrentTime, */ /* xCurrentTime */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 155 | memdbGetLastError, /* xGetLastError */ |
drh | 904e48e | 2021-05-17 17:14:38 +0000 | [diff] [blame] | 156 | memdbCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 157 | 0, /* xSetSystemCall */ |
| 158 | 0, /* xGetSystemCall */ |
| 159 | 0, /* xNextSystemCall */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | static const sqlite3_io_methods memdb_io_methods = { |
| 163 | 3, /* iVersion */ |
| 164 | memdbClose, /* xClose */ |
| 165 | memdbRead, /* xRead */ |
| 166 | memdbWrite, /* xWrite */ |
| 167 | memdbTruncate, /* xTruncate */ |
| 168 | memdbSync, /* xSync */ |
| 169 | memdbFileSize, /* xFileSize */ |
| 170 | memdbLock, /* xLock */ |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 171 | memdbUnlock, /* xUnlock */ |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 172 | 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 173 | memdbFileControl, /* xFileControl */ |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 174 | 0, /* memdbSectorSize,*/ /* xSectorSize */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 175 | memdbDeviceCharacteristics, /* xDeviceCharacteristics */ |
drh | 99abe5c | 2018-01-03 22:48:38 +0000 | [diff] [blame] | 176 | 0, /* xShmMap */ |
| 177 | 0, /* xShmLock */ |
| 178 | 0, /* xShmBarrier */ |
| 179 | 0, /* xShmUnmap */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 180 | memdbFetch, /* xFetch */ |
| 181 | memdbUnfetch /* xUnfetch */ |
| 182 | }; |
| 183 | |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 184 | /* |
| 185 | ** Enter/leave the mutex on a MemStore |
| 186 | */ |
drh | 904e48e | 2021-05-17 17:14:38 +0000 | [diff] [blame] | 187 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0 |
| 188 | static void memdbEnter(MemStore *p){ |
| 189 | UNUSED_PARAMETER(p); |
| 190 | } |
| 191 | static void memdbLeave(MemStore *p){ |
| 192 | UNUSED_PARAMETER(p); |
| 193 | } |
| 194 | #else |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 195 | static void memdbEnter(MemStore *p){ |
| 196 | sqlite3_mutex_enter(p->pMutex); |
| 197 | } |
| 198 | static void memdbLeave(MemStore *p){ |
| 199 | sqlite3_mutex_leave(p->pMutex); |
| 200 | } |
drh | 904e48e | 2021-05-17 17:14:38 +0000 | [diff] [blame] | 201 | #endif |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 202 | |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 203 | |
| 204 | |
| 205 | /* |
| 206 | ** Close an memdb-file. |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 207 | ** Free the underlying MemStore object when its refcount drops to zero |
| 208 | ** or less. |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 209 | */ |
| 210 | static int memdbClose(sqlite3_file *pFile){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 211 | MemStore *p = ((MemFile*)pFile)->pStore; |
drh | 52d1407 | 2021-05-12 15:39:02 +0000 | [diff] [blame] | 212 | if( p->zFName ){ |
| 213 | int i; |
drh | 2d344f9 | 2021-05-12 02:52:20 +0000 | [diff] [blame] | 214 | #ifndef SQLITE_MUTEX_OMIT |
drh | 52d1407 | 2021-05-12 15:39:02 +0000 | [diff] [blame] | 215 | sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
drh | 2d344f9 | 2021-05-12 02:52:20 +0000 | [diff] [blame] | 216 | #endif |
drh | 52d1407 | 2021-05-12 15:39:02 +0000 | [diff] [blame] | 217 | sqlite3_mutex_enter(pVfsMutex); |
| 218 | for(i=0; ALWAYS(i<memdb_g.nMemStore); i++){ |
| 219 | if( memdb_g.apMemStore[i]==p ){ |
| 220 | memdbEnter(p); |
| 221 | if( p->nRef==1 ){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 222 | memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore]; |
| 223 | if( memdb_g.nMemStore==0 ){ |
| 224 | sqlite3_free(memdb_g.apMemStore); |
| 225 | memdb_g.apMemStore = 0; |
| 226 | } |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 227 | } |
drh | 52d1407 | 2021-05-12 15:39:02 +0000 | [diff] [blame] | 228 | break; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 229 | } |
drh | 52d1407 | 2021-05-12 15:39:02 +0000 | [diff] [blame] | 230 | } |
| 231 | sqlite3_mutex_leave(pVfsMutex); |
| 232 | }else{ |
| 233 | memdbEnter(p); |
| 234 | } |
| 235 | p->nRef--; |
| 236 | if( p->nRef<=0 ){ |
| 237 | if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){ |
| 238 | sqlite3_free(p->aData); |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 239 | } |
| 240 | memdbLeave(p); |
| 241 | sqlite3_mutex_free(p->pMutex); |
| 242 | sqlite3_free(p); |
| 243 | }else{ |
| 244 | memdbLeave(p); |
drh | ff01ee3 | 2020-10-17 22:13:16 +0000 | [diff] [blame] | 245 | } |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 246 | return SQLITE_OK; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | ** Read data from an memdb-file. |
| 251 | */ |
| 252 | static int memdbRead( |
| 253 | sqlite3_file *pFile, |
| 254 | void *zBuf, |
| 255 | int iAmt, |
| 256 | sqlite_int64 iOfst |
| 257 | ){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 258 | MemStore *p = ((MemFile*)pFile)->pStore; |
| 259 | memdbEnter(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 260 | if( iOfst+iAmt>p->sz ){ |
| 261 | memset(zBuf, 0, iAmt); |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 262 | if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst); |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 263 | memdbLeave(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 264 | return SQLITE_IOERR_SHORT_READ; |
| 265 | } |
| 266 | memcpy(zBuf, p->aData+iOfst, iAmt); |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 267 | memdbLeave(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 268 | return SQLITE_OK; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | ** Try to enlarge the memory allocation to hold at least sz bytes |
| 273 | */ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 274 | static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 275 | unsigned char *pNew; |
drh | 88944e6 | 2021-10-23 17:46:00 +0000 | [diff] [blame] | 276 | if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){ |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 277 | return SQLITE_FULL; |
| 278 | } |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 279 | if( newSz>p->szMax ){ |
| 280 | return SQLITE_FULL; |
| 281 | } |
| 282 | newSz *= 2; |
| 283 | if( newSz>p->szMax ) newSz = p->szMax; |
drh | d924e7b | 2020-05-17 00:26:44 +0000 | [diff] [blame] | 284 | pNew = sqlite3Realloc(p->aData, newSz); |
dan | a3a91dd | 2021-04-09 20:50:40 +0000 | [diff] [blame] | 285 | if( pNew==0 ) return SQLITE_IOERR_NOMEM; |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 286 | p->aData = pNew; |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 287 | p->szAlloc = newSz; |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 288 | return SQLITE_OK; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | ** Write data to an memdb-file. |
| 293 | */ |
| 294 | static int memdbWrite( |
| 295 | sqlite3_file *pFile, |
| 296 | const void *z, |
| 297 | int iAmt, |
| 298 | sqlite_int64 iOfst |
| 299 | ){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 300 | MemStore *p = ((MemFile*)pFile)->pStore; |
| 301 | memdbEnter(p); |
drh | 483051c | 2021-05-12 02:09:01 +0000 | [diff] [blame] | 302 | if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){ |
| 303 | /* Can't happen: memdbLock() will return SQLITE_READONLY before |
| 304 | ** reaching this point */ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 305 | memdbLeave(p); |
| 306 | return SQLITE_IOERR_WRITE; |
| 307 | } |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 308 | if( iOfst+iAmt>p->sz ){ |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 309 | int rc; |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 310 | if( iOfst+iAmt>p->szAlloc |
| 311 | && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 312 | ){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 313 | memdbLeave(p); |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 314 | return rc; |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 315 | } |
| 316 | if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz); |
| 317 | p->sz = iOfst+iAmt; |
| 318 | } |
| 319 | memcpy(p->aData+iOfst, z, iAmt); |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 320 | memdbLeave(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 321 | return SQLITE_OK; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | ** Truncate an memdb-file. |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 326 | ** |
| 327 | ** In rollback mode (which is always the case for memdb, as it does not |
| 328 | ** support WAL mode) the truncate() method is only used to reduce |
| 329 | ** the size of a file, never to increase the size. |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 330 | */ |
| 331 | static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 332 | MemStore *p = ((MemFile*)pFile)->pStore; |
| 333 | int rc = SQLITE_OK; |
| 334 | memdbEnter(p); |
dan | 1a39e45 | 2021-11-08 15:46:08 +0000 | [diff] [blame] | 335 | if( size>p->sz ){ |
| 336 | /* This can only happen with a corrupt wal mode db */ |
| 337 | rc = SQLITE_CORRUPT; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 338 | }else{ |
| 339 | p->sz = size; |
| 340 | } |
| 341 | memdbLeave(p); |
| 342 | return rc; |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* |
| 346 | ** Sync an memdb-file. |
| 347 | */ |
| 348 | static int memdbSync(sqlite3_file *pFile, int flags){ |
drh | 904e48e | 2021-05-17 17:14:38 +0000 | [diff] [blame] | 349 | UNUSED_PARAMETER(pFile); |
| 350 | UNUSED_PARAMETER(flags); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 351 | return SQLITE_OK; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | ** Return the current file-size of an memdb-file. |
| 356 | */ |
| 357 | static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 358 | MemStore *p = ((MemFile*)pFile)->pStore; |
| 359 | memdbEnter(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 360 | *pSize = p->sz; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 361 | memdbLeave(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 362 | return SQLITE_OK; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | ** Lock an memdb-file. |
| 367 | */ |
| 368 | static int memdbLock(sqlite3_file *pFile, int eLock){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 369 | MemFile *pThis = (MemFile*)pFile; |
| 370 | MemStore *p = pThis->pStore; |
| 371 | int rc = SQLITE_OK; |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 372 | if( eLock<=pThis->eLock ) return SQLITE_OK; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 373 | memdbEnter(p); |
drh | 7c66faa | 2022-12-07 16:58:04 +0000 | [diff] [blame] | 374 | |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 375 | assert( p->nWrLock==0 || p->nWrLock==1 ); |
| 376 | assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 ); |
| 377 | assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 ); |
drh | 7c66faa | 2022-12-07 16:58:04 +0000 | [diff] [blame] | 378 | |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 379 | if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){ |
| 380 | rc = SQLITE_READONLY; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 381 | }else{ |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 382 | switch( eLock ){ |
| 383 | case SQLITE_LOCK_SHARED: { |
| 384 | assert( pThis->eLock==SQLITE_LOCK_NONE ); |
| 385 | if( p->nWrLock>0 ){ |
| 386 | rc = SQLITE_BUSY; |
| 387 | }else{ |
| 388 | p->nRdLock++; |
| 389 | } |
| 390 | break; |
| 391 | }; |
| 392 | |
| 393 | case SQLITE_LOCK_RESERVED: |
| 394 | case SQLITE_LOCK_PENDING: { |
| 395 | assert( pThis->eLock>=SQLITE_LOCK_SHARED ); |
drh | 2656b1b | 2022-12-20 15:02:44 +0000 | [diff] [blame] | 396 | if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){ |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 397 | if( p->nWrLock>0 ){ |
| 398 | rc = SQLITE_BUSY; |
| 399 | }else{ |
| 400 | p->nWrLock = 1; |
| 401 | } |
| 402 | } |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | default: { |
| 407 | assert( eLock==SQLITE_LOCK_EXCLUSIVE ); |
| 408 | assert( pThis->eLock>=SQLITE_LOCK_SHARED ); |
| 409 | if( p->nRdLock>1 ){ |
| 410 | rc = SQLITE_BUSY; |
| 411 | }else if( pThis->eLock==SQLITE_LOCK_SHARED ){ |
| 412 | p->nWrLock = 1; |
| 413 | } |
| 414 | break; |
| 415 | } |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 416 | } |
drh | f186f0b | 2019-01-22 16:43:47 +0000 | [diff] [blame] | 417 | } |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 418 | if( rc==SQLITE_OK ) pThis->eLock = eLock; |
| 419 | memdbLeave(p); |
| 420 | return rc; |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 421 | } |
| 422 | |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 423 | /* |
| 424 | ** Unlock an memdb-file. |
| 425 | */ |
| 426 | static int memdbUnlock(sqlite3_file *pFile, int eLock){ |
| 427 | MemFile *pThis = (MemFile*)pFile; |
| 428 | MemStore *p = pThis->pStore; |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 429 | if( eLock>=pThis->eLock ) return SQLITE_OK; |
| 430 | memdbEnter(p); |
| 431 | |
| 432 | assert( eLock==SQLITE_LOCK_SHARED || eLock==SQLITE_LOCK_NONE ); |
| 433 | if( eLock==SQLITE_LOCK_SHARED ){ |
drh | 2656b1b | 2022-12-20 15:02:44 +0000 | [diff] [blame] | 434 | if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){ |
dan | 1532f9d | 2022-12-19 14:06:36 +0000 | [diff] [blame] | 435 | p->nWrLock--; |
| 436 | } |
| 437 | }else{ |
| 438 | if( pThis->eLock>SQLITE_LOCK_SHARED ){ |
| 439 | p->nWrLock--; |
| 440 | } |
| 441 | p->nRdLock--; |
| 442 | } |
| 443 | |
| 444 | pThis->eLock = eLock; |
| 445 | memdbLeave(p); |
| 446 | return SQLITE_OK; |
| 447 | } |
| 448 | |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 449 | #if 0 |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 450 | /* |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 451 | ** This interface is only used for crash recovery, which does not |
| 452 | ** occur on an in-memory database. |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 453 | */ |
| 454 | static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 455 | *pResOut = 0; |
| 456 | return SQLITE_OK; |
| 457 | } |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 458 | #endif |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 459 | |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 460 | |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 461 | /* |
| 462 | ** File control method. For custom operations on an memdb-file. |
| 463 | */ |
| 464 | static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 465 | MemStore *p = ((MemFile*)pFile)->pStore; |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 466 | int rc = SQLITE_NOTFOUND; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 467 | memdbEnter(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 468 | if( op==SQLITE_FCNTL_VFSNAME ){ |
| 469 | *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz); |
| 470 | rc = SQLITE_OK; |
| 471 | } |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 472 | if( op==SQLITE_FCNTL_SIZE_LIMIT ){ |
| 473 | sqlite3_int64 iLimit = *(sqlite3_int64*)pArg; |
| 474 | if( iLimit<p->sz ){ |
| 475 | if( iLimit<0 ){ |
| 476 | iLimit = p->szMax; |
| 477 | }else{ |
| 478 | iLimit = p->sz; |
| 479 | } |
| 480 | } |
| 481 | p->szMax = iLimit; |
| 482 | *(sqlite3_int64*)pArg = iLimit; |
| 483 | rc = SQLITE_OK; |
| 484 | } |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 485 | memdbLeave(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 486 | return rc; |
| 487 | } |
| 488 | |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 489 | #if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 490 | /* |
| 491 | ** Return the sector-size in bytes for an memdb-file. |
| 492 | */ |
| 493 | static int memdbSectorSize(sqlite3_file *pFile){ |
| 494 | return 1024; |
| 495 | } |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 496 | #endif |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 497 | |
| 498 | /* |
| 499 | ** Return the device characteristic flags supported by an memdb-file. |
| 500 | */ |
| 501 | static int memdbDeviceCharacteristics(sqlite3_file *pFile){ |
drh | 904e48e | 2021-05-17 17:14:38 +0000 | [diff] [blame] | 502 | UNUSED_PARAMETER(pFile); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 503 | return SQLITE_IOCAP_ATOMIC | |
| 504 | SQLITE_IOCAP_POWERSAFE_OVERWRITE | |
| 505 | SQLITE_IOCAP_SAFE_APPEND | |
| 506 | SQLITE_IOCAP_SEQUENTIAL; |
| 507 | } |
| 508 | |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 509 | /* Fetch a page of a memory-mapped file */ |
| 510 | static int memdbFetch( |
| 511 | sqlite3_file *pFile, |
| 512 | sqlite3_int64 iOfst, |
| 513 | int iAmt, |
| 514 | void **pp |
| 515 | ){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 516 | MemStore *p = ((MemFile*)pFile)->pStore; |
| 517 | memdbEnter(p); |
drh | 88944e6 | 2021-10-23 17:46:00 +0000 | [diff] [blame] | 518 | if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){ |
drh | 94f0a83 | 2019-01-25 14:16:01 +0000 | [diff] [blame] | 519 | *pp = 0; |
| 520 | }else{ |
| 521 | p->nMmap++; |
| 522 | *pp = (void*)(p->aData + iOfst); |
| 523 | } |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 524 | memdbLeave(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 525 | return SQLITE_OK; |
| 526 | } |
| 527 | |
| 528 | /* Release a memory-mapped page */ |
| 529 | static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 530 | MemStore *p = ((MemFile*)pFile)->pStore; |
drh | 904e48e | 2021-05-17 17:14:38 +0000 | [diff] [blame] | 531 | UNUSED_PARAMETER(iOfst); |
| 532 | UNUSED_PARAMETER(pPage); |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 533 | memdbEnter(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 534 | p->nMmap--; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 535 | memdbLeave(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 536 | return SQLITE_OK; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | ** Open an mem file handle. |
| 541 | */ |
| 542 | static int memdbOpen( |
| 543 | sqlite3_vfs *pVfs, |
| 544 | const char *zName, |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 545 | sqlite3_file *pFd, |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 546 | int flags, |
| 547 | int *pOutFlags |
| 548 | ){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 549 | MemFile *pFile = (MemFile*)pFd; |
| 550 | MemStore *p = 0; |
| 551 | int szName; |
drh | 90385dd | 2021-10-27 15:19:01 +0000 | [diff] [blame] | 552 | UNUSED_PARAMETER(pVfs); |
drh | 88944e6 | 2021-10-23 17:46:00 +0000 | [diff] [blame] | 553 | |
larrybr | 3a9793e | 2021-09-10 18:35:59 +0000 | [diff] [blame] | 554 | memset(pFile, 0, sizeof(*pFile)); |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 555 | szName = sqlite3Strlen30(zName); |
dan | 118b53b | 2022-12-15 18:56:12 +0000 | [diff] [blame] | 556 | if( szName>1 && (zName[0]=='/' || zName[0]=='\\') ){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 557 | int i; |
drh | 2d344f9 | 2021-05-12 02:52:20 +0000 | [diff] [blame] | 558 | #ifndef SQLITE_MUTEX_OMIT |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 559 | sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
drh | 2d344f9 | 2021-05-12 02:52:20 +0000 | [diff] [blame] | 560 | #endif |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 561 | sqlite3_mutex_enter(pVfsMutex); |
| 562 | for(i=0; i<memdb_g.nMemStore; i++){ |
| 563 | if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){ |
| 564 | p = memdb_g.apMemStore[i]; |
| 565 | break; |
| 566 | } |
| 567 | } |
| 568 | if( p==0 ){ |
| 569 | MemStore **apNew; |
| 570 | p = sqlite3Malloc( sizeof(*p) + szName + 3 ); |
| 571 | if( p==0 ){ |
| 572 | sqlite3_mutex_leave(pVfsMutex); |
| 573 | return SQLITE_NOMEM; |
| 574 | } |
| 575 | apNew = sqlite3Realloc(memdb_g.apMemStore, |
| 576 | sizeof(apNew[0])*(memdb_g.nMemStore+1) ); |
| 577 | if( apNew==0 ){ |
| 578 | sqlite3_free(p); |
| 579 | sqlite3_mutex_leave(pVfsMutex); |
| 580 | return SQLITE_NOMEM; |
| 581 | } |
| 582 | apNew[memdb_g.nMemStore++] = p; |
| 583 | memdb_g.apMemStore = apNew; |
| 584 | memset(p, 0, sizeof(*p)); |
| 585 | p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE; |
| 586 | p->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 587 | p->zFName = (char*)&p[1]; |
| 588 | memcpy(p->zFName, zName, szName+1); |
| 589 | p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
drh | 2d344f9 | 2021-05-12 02:52:20 +0000 | [diff] [blame] | 590 | if( p->pMutex==0 ){ |
| 591 | memdb_g.nMemStore--; |
| 592 | sqlite3_free(p); |
| 593 | sqlite3_mutex_leave(pVfsMutex); |
| 594 | return SQLITE_NOMEM; |
| 595 | } |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 596 | p->nRef = 1; |
| 597 | memdbEnter(p); |
| 598 | }else{ |
| 599 | memdbEnter(p); |
| 600 | p->nRef++; |
| 601 | } |
| 602 | sqlite3_mutex_leave(pVfsMutex); |
| 603 | }else{ |
| 604 | p = sqlite3Malloc( sizeof(*p) ); |
| 605 | if( p==0 ){ |
| 606 | return SQLITE_NOMEM; |
| 607 | } |
| 608 | memset(p, 0, sizeof(*p)); |
| 609 | p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE; |
| 610 | p->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 611 | } |
| 612 | pFile->pStore = p; |
drh | 88944e6 | 2021-10-23 17:46:00 +0000 | [diff] [blame] | 613 | if( pOutFlags!=0 ){ |
| 614 | *pOutFlags = flags | SQLITE_OPEN_MEMORY; |
| 615 | } |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 616 | pFd->pMethods = &memdb_io_methods; |
| 617 | memdbLeave(p); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 618 | return SQLITE_OK; |
| 619 | } |
| 620 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 621 | #if 0 /* Only used to delete rollback journals, super-journals, and WAL |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 622 | ** files, none of which exist in memdb. So this routine is never used */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 623 | /* |
| 624 | ** Delete the file located at zPath. If the dirSync argument is true, |
| 625 | ** ensure the file-system modifications are synced to disk before |
| 626 | ** returning. |
| 627 | */ |
| 628 | static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 629 | return SQLITE_IOERR_DELETE; |
| 630 | } |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 631 | #endif |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 632 | |
| 633 | /* |
| 634 | ** Test for access permissions. Return true if the requested permission |
| 635 | ** is available, or false otherwise. |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 636 | ** |
| 637 | ** With memdb, no files ever exist on disk. So always return false. |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 638 | */ |
| 639 | static int memdbAccess( |
| 640 | sqlite3_vfs *pVfs, |
| 641 | const char *zPath, |
| 642 | int flags, |
| 643 | int *pResOut |
| 644 | ){ |
drh | 904e48e | 2021-05-17 17:14:38 +0000 | [diff] [blame] | 645 | UNUSED_PARAMETER(pVfs); |
| 646 | UNUSED_PARAMETER(zPath); |
| 647 | UNUSED_PARAMETER(flags); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 648 | *pResOut = 0; |
| 649 | return SQLITE_OK; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | ** Populate buffer zOut with the full canonical pathname corresponding |
| 654 | ** to the pathname in zPath. zOut is guaranteed to point to a buffer |
| 655 | ** of at least (INST_MAX_PATHNAME+1) bytes. |
| 656 | */ |
| 657 | static int memdbFullPathname( |
| 658 | sqlite3_vfs *pVfs, |
| 659 | const char *zPath, |
| 660 | int nOut, |
| 661 | char *zOut |
| 662 | ){ |
drh | 904e48e | 2021-05-17 17:14:38 +0000 | [diff] [blame] | 663 | UNUSED_PARAMETER(pVfs); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 664 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
| 665 | return SQLITE_OK; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | ** Open the dynamic library located at zPath and return a handle. |
| 670 | */ |
| 671 | static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 672 | return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | ** Populate the buffer zErrMsg (size nByte bytes) with a human readable |
| 677 | ** utf-8 string describing the most recent error encountered associated |
| 678 | ** with dynamic libraries. |
| 679 | */ |
| 680 | static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ |
| 681 | ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. |
| 686 | */ |
| 687 | static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ |
| 688 | return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | ** Close the dynamic library handle pHandle. |
| 693 | */ |
| 694 | static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 695 | ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | ** Populate the buffer pointed to by zBufOut with nByte bytes of |
| 700 | ** random data. |
| 701 | */ |
| 702 | static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 703 | return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | ** Sleep for nMicro microseconds. Return the number of microseconds |
| 708 | ** actually slept. |
| 709 | */ |
| 710 | static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 711 | return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); |
| 712 | } |
| 713 | |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 714 | #if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */ |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 715 | /* |
| 716 | ** Return the current time as a Julian Day number in *pTimeOut. |
| 717 | */ |
| 718 | static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 719 | return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); |
| 720 | } |
drh | 1471416 | 2018-03-06 19:14:32 +0000 | [diff] [blame] | 721 | #endif |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 722 | |
| 723 | static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){ |
| 724 | return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); |
| 725 | } |
| 726 | static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ |
| 727 | return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | ** Translate a database connection pointer and schema name into a |
| 732 | ** MemFile pointer. |
| 733 | */ |
| 734 | static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){ |
| 735 | MemFile *p = 0; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 736 | MemStore *pStore; |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 737 | int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p); |
| 738 | if( rc ) return 0; |
| 739 | if( p->base.pMethods!=&memdb_io_methods ) return 0; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 740 | pStore = p->pStore; |
| 741 | memdbEnter(pStore); |
| 742 | if( pStore->zFName!=0 ) p = 0; |
| 743 | memdbLeave(pStore); |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 744 | return p; |
| 745 | } |
| 746 | |
| 747 | /* |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 748 | ** Return the serialization of a database |
| 749 | */ |
| 750 | unsigned char *sqlite3_serialize( |
| 751 | sqlite3 *db, /* The database connection */ |
| 752 | const char *zSchema, /* Which database within the connection */ |
| 753 | sqlite3_int64 *piSize, /* Write size here, if not NULL */ |
| 754 | unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */ |
| 755 | ){ |
drh | b2194ce | 2018-03-01 22:18:26 +0000 | [diff] [blame] | 756 | MemFile *p; |
| 757 | int iDb; |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 758 | Btree *pBt; |
| 759 | sqlite3_int64 sz; |
| 760 | int szPage = 0; |
| 761 | sqlite3_stmt *pStmt = 0; |
| 762 | unsigned char *pOut; |
| 763 | char *zSql; |
| 764 | int rc; |
| 765 | |
mistachkin | 6630f94 | 2018-03-08 19:56:52 +0000 | [diff] [blame] | 766 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 767 | if( !sqlite3SafetyCheckOk(db) ){ |
| 768 | (void)SQLITE_MISUSE_BKPT; |
| 769 | return 0; |
| 770 | } |
| 771 | #endif |
| 772 | |
drh | b2194ce | 2018-03-01 22:18:26 +0000 | [diff] [blame] | 773 | if( zSchema==0 ) zSchema = db->aDb[0].zDbSName; |
| 774 | p = memdbFromDbSchema(db, zSchema); |
| 775 | iDb = sqlite3FindDbName(db, zSchema); |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 776 | if( piSize ) *piSize = -1; |
| 777 | if( iDb<0 ) return 0; |
| 778 | if( p ){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 779 | MemStore *pStore = p->pStore; |
| 780 | assert( pStore->pMutex==0 ); |
| 781 | if( piSize ) *piSize = pStore->sz; |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 782 | if( mFlags & SQLITE_SERIALIZE_NOCOPY ){ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 783 | pOut = pStore->aData; |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 784 | }else{ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 785 | pOut = sqlite3_malloc64( pStore->sz ); |
| 786 | if( pOut ) memcpy(pOut, pStore->aData, pStore->sz); |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 787 | } |
| 788 | return pOut; |
| 789 | } |
| 790 | pBt = db->aDb[iDb].pBt; |
| 791 | if( pBt==0 ) return 0; |
| 792 | szPage = sqlite3BtreeGetPageSize(pBt); |
| 793 | zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema); |
| 794 | rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM; |
| 795 | sqlite3_free(zSql); |
| 796 | if( rc ) return 0; |
drh | 8784efa | 2018-03-06 20:54:27 +0000 | [diff] [blame] | 797 | rc = sqlite3_step(pStmt); |
| 798 | if( rc!=SQLITE_ROW ){ |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 799 | pOut = 0; |
| 800 | }else{ |
drh | 8784efa | 2018-03-06 20:54:27 +0000 | [diff] [blame] | 801 | sz = sqlite3_column_int64(pStmt, 0)*szPage; |
| 802 | if( piSize ) *piSize = sz; |
| 803 | if( mFlags & SQLITE_SERIALIZE_NOCOPY ){ |
| 804 | pOut = 0; |
| 805 | }else{ |
| 806 | pOut = sqlite3_malloc64( sz ); |
| 807 | if( pOut ){ |
| 808 | int nPage = sqlite3_column_int(pStmt, 0); |
| 809 | Pager *pPager = sqlite3BtreePager(pBt); |
| 810 | int pgno; |
| 811 | for(pgno=1; pgno<=nPage; pgno++){ |
| 812 | DbPage *pPage = 0; |
| 813 | unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1); |
| 814 | rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0); |
| 815 | if( rc==SQLITE_OK ){ |
| 816 | memcpy(pTo, sqlite3PagerGetData(pPage), szPage); |
| 817 | }else{ |
| 818 | memset(pTo, 0, szPage); |
| 819 | } |
| 820 | sqlite3PagerUnref(pPage); |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 821 | } |
drh | cb7d541 | 2018-01-03 16:49:52 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | } |
| 825 | sqlite3_finalize(pStmt); |
| 826 | return pOut; |
| 827 | } |
| 828 | |
drh | 3ec8665 | 2018-01-03 19:03:31 +0000 | [diff] [blame] | 829 | /* Convert zSchema to a MemDB and initialize its content. |
| 830 | */ |
| 831 | int sqlite3_deserialize( |
| 832 | sqlite3 *db, /* The database connection */ |
| 833 | const char *zSchema, /* Which DB to reopen with the deserialization */ |
| 834 | unsigned char *pData, /* The serialized database content */ |
| 835 | sqlite3_int64 szDb, /* Number bytes in the deserialization */ |
| 836 | sqlite3_int64 szBuf, /* Total size of buffer pData[] */ |
| 837 | unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */ |
| 838 | ){ |
| 839 | MemFile *p; |
| 840 | char *zSql; |
| 841 | sqlite3_stmt *pStmt = 0; |
| 842 | int rc; |
| 843 | int iDb; |
| 844 | |
mistachkin | 6630f94 | 2018-03-08 19:56:52 +0000 | [diff] [blame] | 845 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 846 | if( !sqlite3SafetyCheckOk(db) ){ |
| 847 | return SQLITE_MISUSE_BKPT; |
| 848 | } |
| 849 | if( szDb<0 ) return SQLITE_MISUSE_BKPT; |
| 850 | if( szBuf<0 ) return SQLITE_MISUSE_BKPT; |
| 851 | #endif |
| 852 | |
drh | 3ec8665 | 2018-01-03 19:03:31 +0000 | [diff] [blame] | 853 | sqlite3_mutex_enter(db->mutex); |
| 854 | if( zSchema==0 ) zSchema = db->aDb[0].zDbSName; |
| 855 | iDb = sqlite3FindDbName(db, zSchema); |
drh | 53fa025 | 2021-07-20 02:02:24 +0000 | [diff] [blame] | 856 | testcase( iDb==1 ); |
| 857 | if( iDb<2 && iDb!=0 ){ |
drh | 3ec8665 | 2018-01-03 19:03:31 +0000 | [diff] [blame] | 858 | rc = SQLITE_ERROR; |
| 859 | goto end_deserialize; |
drh | 53fa025 | 2021-07-20 02:02:24 +0000 | [diff] [blame] | 860 | } |
drh | 3ec8665 | 2018-01-03 19:03:31 +0000 | [diff] [blame] | 861 | zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema); |
drh | 672f07c | 2020-10-20 14:40:53 +0000 | [diff] [blame] | 862 | if( zSql==0 ){ |
| 863 | rc = SQLITE_NOMEM; |
| 864 | }else{ |
| 865 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 866 | sqlite3_free(zSql); |
| 867 | } |
drh | 3ec8665 | 2018-01-03 19:03:31 +0000 | [diff] [blame] | 868 | if( rc ) goto end_deserialize; |
| 869 | db->init.iDb = (u8)iDb; |
| 870 | db->init.reopenMemdb = 1; |
| 871 | rc = sqlite3_step(pStmt); |
| 872 | db->init.reopenMemdb = 0; |
| 873 | if( rc!=SQLITE_DONE ){ |
| 874 | rc = SQLITE_ERROR; |
| 875 | goto end_deserialize; |
| 876 | } |
| 877 | p = memdbFromDbSchema(db, zSchema); |
drh | 8784efa | 2018-03-06 20:54:27 +0000 | [diff] [blame] | 878 | if( p==0 ){ |
| 879 | rc = SQLITE_ERROR; |
| 880 | }else{ |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 881 | MemStore *pStore = p->pStore; |
| 882 | pStore->aData = pData; |
drh | ff01ee3 | 2020-10-17 22:13:16 +0000 | [diff] [blame] | 883 | pData = 0; |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 884 | pStore->sz = szDb; |
| 885 | pStore->szAlloc = szBuf; |
| 886 | pStore->szMax = szBuf; |
| 887 | if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){ |
| 888 | pStore->szMax = sqlite3GlobalConfig.mxMemdbSize; |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 889 | } |
drh | 2f4d0ec | 2021-05-10 23:48:46 +0000 | [diff] [blame] | 890 | pStore->mFlags = mFlags; |
drh | 8784efa | 2018-03-06 20:54:27 +0000 | [diff] [blame] | 891 | rc = SQLITE_OK; |
| 892 | } |
| 893 | |
drh | 3ec8665 | 2018-01-03 19:03:31 +0000 | [diff] [blame] | 894 | end_deserialize: |
| 895 | sqlite3_finalize(pStmt); |
drh | ff01ee3 | 2020-10-17 22:13:16 +0000 | [diff] [blame] | 896 | if( pData && (mFlags & SQLITE_DESERIALIZE_FREEONCLOSE)!=0 ){ |
| 897 | sqlite3_free(pData); |
| 898 | } |
drh | 3ec8665 | 2018-01-03 19:03:31 +0000 | [diff] [blame] | 899 | sqlite3_mutex_leave(db->mutex); |
| 900 | return rc; |
| 901 | } |
| 902 | |
drh | cf3107c | 2022-11-19 00:08:35 +0000 | [diff] [blame] | 903 | /* |
| 904 | ** Return true if the VFS is the memvfs. |
| 905 | */ |
| 906 | int sqlite3IsMemdb(const sqlite3_vfs *pVfs){ |
| 907 | return pVfs==&memdb_vfs; |
| 908 | } |
| 909 | |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 910 | /* |
| 911 | ** This routine is called when the extension is loaded. |
| 912 | ** Register the new VFS. |
| 913 | */ |
| 914 | int sqlite3MemdbInit(void){ |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 915 | sqlite3_vfs *pLower = sqlite3_vfs_find(0); |
drh | a959bf5 | 2021-06-15 15:15:40 +0000 | [diff] [blame] | 916 | unsigned int sz; |
| 917 | if( NEVER(pLower==0) ) return SQLITE_ERROR; |
| 918 | sz = pLower->szOsFile; |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 919 | memdb_vfs.pAppData = pLower; |
drh | f25f8d5 | 2020-05-21 20:38:39 +0000 | [diff] [blame] | 920 | /* The following conditional can only be true when compiled for |
| 921 | ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave |
| 922 | ** it in, to be safe, but it is marked as NO_TEST since there |
| 923 | ** is no way to reach it under most builds. */ |
| 924 | if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/ |
drh | 5f9d192 | 2018-03-06 04:01:08 +0000 | [diff] [blame] | 925 | memdb_vfs.szOsFile = sz; |
drh | ac442f4 | 2018-01-03 01:28:46 +0000 | [diff] [blame] | 926 | return sqlite3_vfs_register(&memdb_vfs, 0); |
| 927 | } |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 928 | #endif /* SQLITE_OMIT_DESERIALIZE */ |