sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** 2022-09-06 |
| 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 an experimental VFS layer that operates on a |
| 14 | ** Key/Value storage engine where both keys and values must be pure |
| 15 | ** text. |
| 16 | ** |
| 17 | ** DEBUG AND TEST |
| 18 | ** |
| 19 | ** For testing on Unix, compile using: |
| 20 | ** |
| 21 | ** make clean sqlite3d CFLAGS='-DSQLITE_OS_KV_OPTIONAL' |
| 22 | ** |
| 23 | ** Then start up a shell using something like: |
| 24 | ** |
| 25 | ** ./sqlite3d 'file:dbname?vfs=kvvfs' |
| 26 | ** |
| 27 | ** Each K/V entry is stored in a separate file in the working |
| 28 | ** directory that has a name like "kvvfs-dbname-*". Due to limitations |
| 29 | ** on the key size, the name of the database must be very short - just |
| 30 | ** a few characters. If the database name is too long, the VFS will |
| 31 | ** malfunction and you will get SQLITE_CORRUPT errors. |
| 32 | */ |
| 33 | #include <sqliteInt.h> |
| 34 | #if SQLITE_OS_KV || (SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)) |
| 35 | |
| 36 | /***************************************************************************** |
| 37 | ** Debugging logic |
| 38 | */ |
| 39 | |
| 40 | /* SQLITE_KV_TRACE() is used for tracing calls to kvrecord routines. */ |
| 41 | #if 0 |
| 42 | #define SQLITE_KV_TRACE(X) printf X |
| 43 | #else |
| 44 | #define SQLITE_KV_TRACE(X) |
| 45 | #endif |
| 46 | |
| 47 | /* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */ |
| 48 | #if 0 |
| 49 | #define SQLITE_KV_LOG(X) printf X |
| 50 | #else |
| 51 | #define SQLITE_KV_LOG(X) |
| 52 | #endif |
| 53 | |
| 54 | /* |
| 55 | ** Forward declaration of objects used by this VFS implementation |
| 56 | */ |
| 57 | typedef struct KVVfsFile KVVfsFile; |
| 58 | |
| 59 | /* A single open file. There are only two files represented by this |
| 60 | ** VFS - the database and the rollback journal. |
| 61 | ** |
| 62 | ** Maintenance reminder: if this struct changes in any way, the JSON |
| 63 | ** rendering of its structure must be updated in |
| 64 | ** sqlite3-wasm.c:sqlite3__wasm_enum_json(). There are no binary |
| 65 | ** compatibility concerns, so it does not need an iVersion member. |
| 66 | */ |
| 67 | struct KVVfsFile { |
| 68 | sqlite3_file base; /* IO methods */ |
| 69 | const char *zClass; /* Storage class */ |
| 70 | int isJournal; /* True if this is a journal file */ |
| 71 | unsigned int nJrnl; /* Space allocated for aJrnl[] */ |
| 72 | char *aJrnl; /* Journal content */ |
| 73 | int szPage; /* Last known page size */ |
| 74 | sqlite3_int64 szDb; /* Database file size. -1 means unknown */ |
| 75 | char *aData; /* Buffer to hold page data */ |
| 76 | }; |
| 77 | #define SQLITE_KVOS_SZ 133073 |
| 78 | |
| 79 | /* |
| 80 | ** Methods for KVVfsFile |
| 81 | */ |
| 82 | static int kvvfsClose(sqlite3_file*); |
| 83 | static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 84 | static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 85 | static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64); |
| 86 | static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64); |
| 87 | static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size); |
| 88 | static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size); |
| 89 | static int kvvfsSyncDb(sqlite3_file*, int flags); |
| 90 | static int kvvfsSyncJrnl(sqlite3_file*, int flags); |
| 91 | static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize); |
| 92 | static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize); |
| 93 | static int kvvfsLock(sqlite3_file*, int); |
| 94 | static int kvvfsUnlock(sqlite3_file*, int); |
| 95 | static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut); |
| 96 | static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg); |
| 97 | static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg); |
| 98 | static int kvvfsSectorSize(sqlite3_file*); |
| 99 | static int kvvfsDeviceCharacteristics(sqlite3_file*); |
| 100 | |
| 101 | /* |
| 102 | ** Methods for sqlite3_vfs |
| 103 | */ |
| 104 | static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
| 105 | static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir); |
| 106 | static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
| 107 | static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); |
| 108 | static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename); |
| 109 | static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut); |
| 110 | static int kvvfsSleep(sqlite3_vfs*, int microseconds); |
| 111 | static int kvvfsCurrentTime(sqlite3_vfs*, double*); |
| 112 | static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); |
| 113 | |
| 114 | static sqlite3_vfs sqlite3OsKvvfsObject = { |
| 115 | 2, /* iVersion */ |
| 116 | sizeof(KVVfsFile), /* szOsFile */ |
| 117 | 1024, /* mxPathname */ |
| 118 | 0, /* pNext */ |
| 119 | "kvvfs", /* zName */ |
| 120 | 0, /* pAppData */ |
| 121 | kvvfsOpen, /* xOpen */ |
| 122 | kvvfsDelete, /* xDelete */ |
| 123 | kvvfsAccess, /* xAccess */ |
| 124 | kvvfsFullPathname, /* xFullPathname */ |
| 125 | kvvfsDlOpen, /* xDlOpen */ |
| 126 | 0, /* xDlError */ |
| 127 | 0, /* xDlSym */ |
| 128 | 0, /* xDlClose */ |
| 129 | kvvfsRandomness, /* xRandomness */ |
| 130 | kvvfsSleep, /* xSleep */ |
| 131 | kvvfsCurrentTime, /* xCurrentTime */ |
| 132 | 0, /* xGetLastError */ |
| 133 | kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */ |
| 134 | }; |
| 135 | |
| 136 | /* Methods for sqlite3_file objects referencing a database file |
| 137 | */ |
| 138 | static sqlite3_io_methods kvvfs_db_io_methods = { |
| 139 | 1, /* iVersion */ |
| 140 | kvvfsClose, /* xClose */ |
| 141 | kvvfsReadDb, /* xRead */ |
| 142 | kvvfsWriteDb, /* xWrite */ |
| 143 | kvvfsTruncateDb, /* xTruncate */ |
| 144 | kvvfsSyncDb, /* xSync */ |
| 145 | kvvfsFileSizeDb, /* xFileSize */ |
| 146 | kvvfsLock, /* xLock */ |
| 147 | kvvfsUnlock, /* xUnlock */ |
| 148 | kvvfsCheckReservedLock, /* xCheckReservedLock */ |
| 149 | kvvfsFileControlDb, /* xFileControl */ |
| 150 | kvvfsSectorSize, /* xSectorSize */ |
| 151 | kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 152 | 0, /* xShmMap */ |
| 153 | 0, /* xShmLock */ |
| 154 | 0, /* xShmBarrier */ |
| 155 | 0, /* xShmUnmap */ |
| 156 | 0, /* xFetch */ |
| 157 | 0 /* xUnfetch */ |
| 158 | }; |
| 159 | |
| 160 | /* Methods for sqlite3_file objects referencing a rollback journal |
| 161 | */ |
| 162 | static sqlite3_io_methods kvvfs_jrnl_io_methods = { |
| 163 | 1, /* iVersion */ |
| 164 | kvvfsClose, /* xClose */ |
| 165 | kvvfsReadJrnl, /* xRead */ |
| 166 | kvvfsWriteJrnl, /* xWrite */ |
| 167 | kvvfsTruncateJrnl, /* xTruncate */ |
| 168 | kvvfsSyncJrnl, /* xSync */ |
| 169 | kvvfsFileSizeJrnl, /* xFileSize */ |
| 170 | kvvfsLock, /* xLock */ |
| 171 | kvvfsUnlock, /* xUnlock */ |
| 172 | kvvfsCheckReservedLock, /* xCheckReservedLock */ |
| 173 | kvvfsFileControlJrnl, /* xFileControl */ |
| 174 | kvvfsSectorSize, /* xSectorSize */ |
| 175 | kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 176 | 0, /* xShmMap */ |
| 177 | 0, /* xShmLock */ |
| 178 | 0, /* xShmBarrier */ |
| 179 | 0, /* xShmUnmap */ |
| 180 | 0, /* xFetch */ |
| 181 | 0 /* xUnfetch */ |
| 182 | }; |
| 183 | |
| 184 | /****** Storage subsystem **************************************************/ |
| 185 | #include <sys/types.h> |
| 186 | #include <sys/stat.h> |
| 187 | #include <unistd.h> |
| 188 | |
| 189 | /* Forward declarations for the low-level storage engine |
| 190 | */ |
| 191 | #ifndef SQLITE_WASM |
| 192 | /* In WASM builds these are implemented in JS. */ |
| 193 | static int kvrecordWrite(const char*, const char *zKey, const char *zData); |
| 194 | static int kvrecordDelete(const char*, const char *zKey); |
| 195 | static int kvrecordRead(const char*, const char *zKey, char *zBuf, int nBuf); |
| 196 | #endif |
| 197 | #ifndef KVRECORD_KEY_SZ |
| 198 | #define KVRECORD_KEY_SZ 32 |
| 199 | #endif |
| 200 | |
| 201 | /* Expand the key name with an appropriate prefix and put the result |
| 202 | ** in zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least |
| 203 | ** KVRECORD_KEY_SZ bytes. |
| 204 | */ |
| 205 | static void kvrecordMakeKey( |
| 206 | const char *zClass, |
| 207 | const char *zKeyIn, |
| 208 | char *zKeyOut |
| 209 | ){ |
| 210 | assert( zKeyIn ); |
| 211 | assert( zKeyOut ); |
| 212 | assert( zClass ); |
| 213 | sqlite3_snprintf(KVRECORD_KEY_SZ, zKeyOut, "kvvfs-%s-%s", |
| 214 | zClass, zKeyIn); |
| 215 | } |
| 216 | |
| 217 | #ifndef SQLITE_WASM |
| 218 | /* In WASM builds do not define APIs which use fopen(), fwrite(), |
| 219 | ** and the like because those APIs are a portability issue for |
| 220 | ** WASM. |
| 221 | */ |
| 222 | /* Write content into a key. zClass is the particular namespace of the |
| 223 | ** underlying key/value store to use - either "local" or "session". |
| 224 | ** |
| 225 | ** Both zKey and zData are zero-terminated pure text strings. |
| 226 | ** |
| 227 | ** Return the number of errors. |
| 228 | */ |
| 229 | static int kvrecordWrite( |
| 230 | const char *zClass, |
| 231 | const char *zKey, |
| 232 | const char *zData |
| 233 | ){ |
| 234 | FILE *fd; |
| 235 | char zXKey[KVRECORD_KEY_SZ]; |
| 236 | kvrecordMakeKey(zClass, zKey, zXKey); |
| 237 | fd = fopen(zXKey, "wb"); |
| 238 | if( fd ){ |
| 239 | SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey, |
| 240 | (int)strlen(zData), zData, |
| 241 | strlen(zData)>50 ? "..." : "")); |
| 242 | fputs(zData, fd); |
| 243 | fclose(fd); |
| 244 | return 0; |
| 245 | }else{ |
| 246 | return 1; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* Delete a key (with its corresponding data) from the key/value |
| 251 | ** namespace given by zClass. If the key does not previously exist, |
| 252 | ** this routine is a no-op. |
| 253 | */ |
| 254 | static int kvrecordDelete(const char *zClass, const char *zKey){ |
| 255 | char zXKey[KVRECORD_KEY_SZ]; |
| 256 | kvrecordMakeKey(zClass, zKey, zXKey); |
| 257 | unlink(zXKey); |
| 258 | SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey)); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* Read the value associated with a zKey from the key/value namespace given |
| 263 | ** by zClass and put the text data associated with that key in the first |
| 264 | ** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large |
| 265 | ** enough to hold it all. The value put into zBuf must always be zero |
| 266 | ** terminated, even if it gets truncated because nBuf is not large enough. |
| 267 | ** |
| 268 | ** Return the total number of bytes in the data, without truncation, and |
| 269 | ** not counting the final zero terminator. Return -1 if the key does |
| 270 | ** not exist or its key cannot be read. |
| 271 | ** |
| 272 | ** If nBuf<=0 then this routine simply returns the size of the data |
| 273 | ** without actually reading it. Similarly, if nBuf==1 then it |
| 274 | ** zero-terminates zBuf at zBuf[0] and returns the size of the data |
| 275 | ** without reading it. |
| 276 | */ |
| 277 | static int kvrecordRead( |
| 278 | const char *zClass, |
| 279 | const char *zKey, |
| 280 | char *zBuf, |
| 281 | int nBuf |
| 282 | ){ |
| 283 | FILE *fd; |
| 284 | struct stat buf; |
| 285 | char zXKey[KVRECORD_KEY_SZ]; |
| 286 | kvrecordMakeKey(zClass, zKey, zXKey); |
| 287 | if( access(zXKey, R_OK)!=0 |
| 288 | || stat(zXKey, &buf)!=0 |
| 289 | || !S_ISREG(buf.st_mode) |
| 290 | ){ |
| 291 | SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey)); |
| 292 | return -1; |
| 293 | } |
| 294 | if( nBuf<=0 ){ |
| 295 | return (int)buf.st_size; |
| 296 | }else if( nBuf==1 ){ |
| 297 | zBuf[0] = 0; |
| 298 | SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey, |
| 299 | (int)buf.st_size)); |
| 300 | return (int)buf.st_size; |
| 301 | } |
| 302 | if( nBuf > buf.st_size + 1 ){ |
| 303 | nBuf = buf.st_size + 1; |
| 304 | } |
| 305 | fd = fopen(zXKey, "rb"); |
| 306 | if( fd==0 ){ |
| 307 | SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey)); |
| 308 | return -1; |
| 309 | }else{ |
| 310 | sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd); |
| 311 | fclose(fd); |
| 312 | zBuf[n] = 0; |
| 313 | SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey, |
| 314 | n, zBuf, n>50 ? "..." : "")); |
| 315 | return (int)n; |
| 316 | } |
| 317 | } |
| 318 | #endif /* #ifndef SQLITE_WASM */ |
| 319 | |
| 320 | |
| 321 | /* |
| 322 | ** An internal level of indirection which enables us to replace the |
| 323 | ** kvvfs i/o methods with JavaScript implementations in WASM builds. |
| 324 | ** Maintenance reminder: if this struct changes in any way, the JSON |
| 325 | ** rendering of its structure must be updated in |
| 326 | ** sqlite3-wasm.c:sqlite3__wasm_enum_json(). There are no binary |
| 327 | ** compatibility concerns, so it does not need an iVersion member. |
| 328 | */ |
| 329 | typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods; |
| 330 | struct sqlite3_kvvfs_methods { |
| 331 | int (*xRcrdRead)(const char*, const char *zKey, char *zBuf, int nBuf); |
| 332 | int (*xRcrdWrite)(const char*, const char *zKey, const char *zData); |
| 333 | int (*xRcrdDelete)(const char*, const char *zKey); |
| 334 | const int nKeySize; |
| 335 | const int nBufferSize; |
| 336 | #ifndef SQLITE_WASM |
| 337 | # define MAYBE_CONST const |
| 338 | #else |
| 339 | # define MAYBE_CONST |
| 340 | #endif |
| 341 | MAYBE_CONST sqlite3_vfs * pVfs; |
| 342 | MAYBE_CONST sqlite3_io_methods *pIoDb; |
| 343 | MAYBE_CONST sqlite3_io_methods *pIoJrnl; |
| 344 | #undef MAYBE_CONST |
| 345 | }; |
| 346 | |
| 347 | |
| 348 | /* |
| 349 | ** This object holds the kvvfs I/O methods which may be swapped out |
| 350 | ** for JavaScript-side implementations in WASM builds. In such builds |
| 351 | ** it cannot be const, but in native builds it should be so that |
| 352 | ** the compiler can hopefully optimize this level of indirection out. |
| 353 | ** That said, kvvfs is intended primarily for use in WASM builds. |
| 354 | ** |
| 355 | ** This is not explicitly flagged as static because the amalgamation |
| 356 | ** build will tag it with SQLITE_PRIVATE. |
| 357 | */ |
| 358 | #ifndef SQLITE_WASM |
| 359 | const |
| 360 | #endif |
| 361 | sqlite3_kvvfs_methods sqlite3KvvfsMethods = { |
| 362 | #ifndef SQLITE_WASM |
| 363 | .xRcrdRead = kvrecordRead, |
| 364 | .xRcrdWrite = kvrecordWrite, |
| 365 | .xRcrdDelete = kvrecordDelete, |
| 366 | #else |
| 367 | .xRcrdRead = 0, |
| 368 | .xRcrdWrite = 0, |
| 369 | .xRcrdDelete = 0, |
| 370 | #endif |
| 371 | .nKeySize = KVRECORD_KEY_SZ, |
| 372 | .nBufferSize = SQLITE_KVOS_SZ, |
| 373 | .pVfs = &sqlite3OsKvvfsObject, |
| 374 | .pIoDb = &kvvfs_db_io_methods, |
| 375 | .pIoJrnl = &kvvfs_jrnl_io_methods |
| 376 | }; |
| 377 | |
| 378 | /****** Utility subroutines ************************************************/ |
| 379 | |
| 380 | /* |
| 381 | ** Encode binary into the text encoded used to persist on disk. |
| 382 | ** The output text is stored in aOut[], which must be at least |
| 383 | ** nData+1 bytes in length. |
| 384 | ** |
| 385 | ** Return the actual length of the encoded text, not counting the |
| 386 | ** zero terminator at the end. |
| 387 | ** |
| 388 | ** Encoding format |
| 389 | ** --------------- |
| 390 | ** |
| 391 | ** * Non-zero bytes are encoded as upper-case hexadecimal |
| 392 | ** |
| 393 | ** * A sequence of one or more zero-bytes that are not at the |
| 394 | ** beginning of the buffer are encoded as a little-endian |
| 395 | ** base-26 number using a..z. "a" means 0. "b" means 1, |
| 396 | ** "z" means 25. "ab" means 26. "ac" means 52. And so forth. |
| 397 | ** |
| 398 | ** * Because there is no overlap between the encoding characters |
| 399 | ** of hexadecimal and base-26 numbers, it is always clear where |
| 400 | ** one stops and the next begins. |
| 401 | */ |
| 402 | #ifndef SQLITE_WASM |
| 403 | static |
| 404 | #endif |
| 405 | int kvvfsEncode(const char *aData, int nData, char *aOut){ |
| 406 | int i, j; |
| 407 | const unsigned char *a = (const unsigned char*)aData; |
| 408 | for(i=j=0; i<nData; i++){ |
| 409 | unsigned char c = a[i]; |
| 410 | if( c!=0 ){ |
| 411 | aOut[j++] = "0123456789ABCDEF"[c>>4]; |
| 412 | aOut[j++] = "0123456789ABCDEF"[c&0xf]; |
| 413 | }else{ |
| 414 | /* A sequence of 1 or more zeros is stored as a little-endian |
| 415 | ** base-26 number using a..z as the digits. So one zero is "b". |
| 416 | ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb", |
| 417 | ** and so forth. |
| 418 | */ |
| 419 | int k; |
| 420 | for(k=1; i+k<nData && a[i+k]==0; k++){} |
| 421 | i += k-1; |
| 422 | while( k>0 ){ |
| 423 | aOut[j++] = 'a'+(k%26); |
| 424 | k /= 26; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | aOut[j] = 0; |
| 429 | return j; |
| 430 | } |
| 431 | |
| 432 | static const signed char kvvfsHexValue[256] = { |
| 433 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 434 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 435 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 436 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 437 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 438 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 439 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 440 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 441 | |
| 442 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 443 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 444 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 445 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 446 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 447 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 448 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 449 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 450 | }; |
| 451 | |
| 452 | /* |
| 453 | ** Decode the text encoding back to binary. The binary content is |
| 454 | ** written into pOut, which must be at least nOut bytes in length. |
| 455 | ** |
| 456 | ** The return value is the number of bytes actually written into aOut[], or |
| 457 | ** -1 for malformed inputs. |
| 458 | */ |
| 459 | #ifndef SQLITE_WASM |
| 460 | static |
| 461 | #endif |
| 462 | int kvvfsDecode(const char *a, char *aOut, int nOut){ |
| 463 | int i, j; |
| 464 | int c; |
| 465 | const unsigned char *aIn = (const unsigned char*)a; |
| 466 | i = 0; |
| 467 | j = 0; |
| 468 | while( 1 ){ |
| 469 | c = kvvfsHexValue[aIn[i]]; |
| 470 | if( c<0 ){ |
| 471 | sqlite3_int64 n = 0; |
| 472 | sqlite3_int64 mult = 1; |
| 473 | c = aIn[i]; |
| 474 | if( c==0 ) break; |
| 475 | while( c>='a' && c<='z' ){ |
| 476 | n += (c - 'a')*mult; |
| 477 | if( n>nOut ) return -1 /* oversized/malformed input */; |
| 478 | mult *= 26; |
| 479 | c = aIn[++i]; |
| 480 | } |
| 481 | if( j+n>nOut ) return -1 /* oversized/malformed input */; |
| 482 | memset(&aOut[j], 0, n); |
| 483 | j += n; |
| 484 | if( c==0 || mult==1 ) break; /* progress stalled if mult==1 */ |
| 485 | }else if( j<nOut ){ |
| 486 | aOut[j] = c<<4; |
| 487 | c = kvvfsHexValue[aIn[++i]]; |
| 488 | if( c<0 ) return -1 /* hex bytes are always in pairs */; |
| 489 | aOut[j++] += c; |
| 490 | i++; |
| 491 | }else{ |
| 492 | return -1; |
| 493 | } |
| 494 | } |
| 495 | return j; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | ** Decode a complete journal file. Allocate space in pFile->aJrnl |
| 500 | ** and store the decoding there. Or leave pFile->aJrnl set to NULL |
| 501 | ** if an error is encountered. |
| 502 | ** |
| 503 | ** The first few characters of the text encoding will be a little-endian |
| 504 | ** base-26 number (digits a..z) that is the total number of bytes |
| 505 | ** in the decoded journal file image. This base-26 number is followed |
| 506 | ** by a single space, then the encoding of the journal. The space |
| 507 | ** separator is required to act as a terminator for the base-26 number. |
| 508 | */ |
| 509 | static void kvvfsDecodeJournal( |
| 510 | KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */ |
| 511 | const char *zTxt, /* Text encoding. Zero-terminated */ |
| 512 | int nTxt /* Bytes in zTxt, excluding zero terminator */ |
| 513 | ){ |
| 514 | unsigned int n = 0, mult; |
| 515 | int c, i; |
| 516 | i = 0; |
| 517 | mult = 1; |
| 518 | sqlite3_free(pFile->aJrnl); |
| 519 | pFile->aJrnl = 0; |
| 520 | pFile->nJrnl = 0; |
| 521 | while( (c = zTxt[i])>='a' && c<='z' ){ |
| 522 | n += (c - 'a')*mult; |
| 523 | mult *= 26; |
| 524 | ++i; |
| 525 | } |
| 526 | if( ' '!=zTxt[i++] ){ |
| 527 | /* Malformed input */ |
| 528 | return; |
| 529 | } |
| 530 | pFile->aJrnl = sqlite3_malloc64( n ); |
| 531 | if( pFile->aJrnl==0 ){ |
| 532 | return; |
| 533 | } |
| 534 | pFile->nJrnl = n; |
| 535 | n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl); |
| 536 | if( n<pFile->nJrnl ){ |
| 537 | sqlite3_free(pFile->aJrnl); |
| 538 | pFile->aJrnl = 0; |
| 539 | pFile->nJrnl = 0; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | ** Read or write the "sz" element, containing the database file size. |
| 545 | */ |
| 546 | static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){ |
| 547 | char zData[50]; |
| 548 | zData[0] = 0; |
| 549 | sqlite3KvvfsMethods.xRcrdRead(pFile->zClass, "sz", zData, |
| 550 | sizeof(zData)-1); |
| 551 | return strtoll(zData, 0, 0); |
| 552 | } |
| 553 | static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){ |
| 554 | char zData[50]; |
| 555 | sqlite3_snprintf(sizeof(zData), zData, "%lld", sz); |
| 556 | return sqlite3KvvfsMethods.xRcrdWrite(pFile->zClass, "sz", zData); |
| 557 | } |
| 558 | |
| 559 | /****** sqlite3_io_methods methods ******************************************/ |
| 560 | |
| 561 | /* |
| 562 | ** Close an kvvfs-file. |
| 563 | */ |
| 564 | static int kvvfsClose(sqlite3_file *pProtoFile){ |
| 565 | KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 566 | |
| 567 | SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass, |
| 568 | pFile->isJournal ? "journal" : "db")); |
| 569 | sqlite3_free(pFile->aJrnl); |
| 570 | sqlite3_free(pFile->aData); |
| 571 | memset(pFile, 0, sizeof(*pFile)); |
| 572 | return SQLITE_OK; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | ** Read from the -journal file. |
| 577 | */ |
| 578 | static int kvvfsReadJrnl( |
| 579 | sqlite3_file *pProtoFile, |
| 580 | void *zBuf, |
| 581 | int iAmt, |
| 582 | sqlite_int64 iOfst |
| 583 | ){ |
| 584 | KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 585 | assert( pFile->isJournal ); |
| 586 | SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); |
| 587 | if( pFile->aJrnl==0 ){ |
| 588 | int rc; |
| 589 | int szTxt = sqlite3KvvfsMethods.xRcrdRead(pFile->zClass, "jrnl", |
| 590 | 0, 0); |
| 591 | char *aTxt; |
| 592 | if( szTxt<=4 ){ |
| 593 | return SQLITE_IOERR; |
| 594 | } |
| 595 | aTxt = sqlite3_malloc64( szTxt+1 ); |
| 596 | if( aTxt==0 ) return SQLITE_NOMEM; |
| 597 | rc = sqlite3KvvfsMethods.xRcrdRead(pFile->zClass, "jrnl", |
| 598 | aTxt, szTxt+1); |
| 599 | if( rc>=0 ){ |
| 600 | kvvfsDecodeJournal(pFile, aTxt, szTxt); |
| 601 | rc = 0; |
| 602 | } |
| 603 | sqlite3_free(aTxt); |
| 604 | if( rc ) return rc; |
| 605 | if( pFile->aJrnl==0 ) return SQLITE_IOERR; |
| 606 | } |
| 607 | if( iOfst+iAmt>pFile->nJrnl ){ |
| 608 | return SQLITE_IOERR_SHORT_READ; |
| 609 | } |
| 610 | memcpy(zBuf, pFile->aJrnl+iOfst, iAmt); |
| 611 | return SQLITE_OK; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | ** Read from the database file. |
| 616 | */ |
| 617 | static int kvvfsReadDb( |
| 618 | sqlite3_file *pProtoFile, |
| 619 | void *zBuf, |
| 620 | int iAmt, |
| 621 | sqlite_int64 iOfst |
| 622 | ){ |
| 623 | KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 624 | unsigned int pgno; |
| 625 | int got, n; |
| 626 | char zKey[30]; |
| 627 | char *aData = pFile->aData; |
| 628 | assert( iOfst>=0 ); |
| 629 | assert( iAmt>=0 ); |
| 630 | SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); |
| 631 | if( iOfst+iAmt>=512 ){ |
| 632 | if( (iOfst % iAmt)!=0 ){ |
| 633 | return SQLITE_IOERR_READ; |
| 634 | } |
| 635 | if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){ |
| 636 | return SQLITE_IOERR_READ; |
| 637 | } |
| 638 | pFile->szPage = iAmt; |
| 639 | pgno = 1 + iOfst/iAmt; |
| 640 | }else{ |
| 641 | pgno = 1; |
| 642 | } |
| 643 | sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno); |
| 644 | got = sqlite3KvvfsMethods.xRcrdRead(pFile->zClass, zKey, |
| 645 | aData, SQLITE_KVOS_SZ-1); |
| 646 | if( got<0 ){ |
| 647 | n = 0; |
| 648 | }else{ |
| 649 | aData[got] = 0; |
| 650 | if( iOfst+iAmt<512 ){ |
| 651 | int k = iOfst+iAmt; |
| 652 | aData[k*2] = 0; |
| 653 | n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000); |
| 654 | if( n>=iOfst+iAmt ){ |
| 655 | memcpy(zBuf, &aData[2000+iOfst], iAmt); |
| 656 | n = iAmt; |
| 657 | }else{ |
| 658 | n = 0; |
| 659 | } |
| 660 | }else{ |
| 661 | n = kvvfsDecode(aData, zBuf, iAmt); |
| 662 | } |
| 663 | } |
| 664 | if( n<iAmt ){ |
| 665 | memset(zBuf+n, 0, iAmt-n); |
| 666 | return SQLITE_IOERR_SHORT_READ; |
| 667 | } |
| 668 | return SQLITE_OK; |
| 669 | } |
| 670 | |
| 671 | |
| 672 | /* |
| 673 | ** Write into the -journal file. |
| 674 | */ |
| 675 | static int kvvfsWriteJrnl( |
| 676 | sqlite3_file *pProtoFile, |
| 677 | const void *zBuf, |
| 678 | int iAmt, |
| 679 | sqlite_int64 iOfst |
| 680 | ){ |
| 681 | KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 682 | sqlite3_int64 iEnd = iOfst+iAmt; |
| 683 | SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); |
| 684 | if( iEnd>=0x10000000 ) return SQLITE_FULL; |
| 685 | if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){ |
| 686 | char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd); |
| 687 | if( aNew==0 ){ |
| 688 | return SQLITE_IOERR_NOMEM; |
| 689 | } |
| 690 | pFile->aJrnl = aNew; |
| 691 | if( pFile->nJrnl<iOfst ){ |
| 692 | memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl); |
| 693 | } |
| 694 | pFile->nJrnl = iEnd; |
| 695 | } |
| 696 | memcpy(pFile->aJrnl+iOfst, zBuf, iAmt); |
| 697 | return SQLITE_OK; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | ** Write into the database file. |
| 702 | */ |
| 703 | static int kvvfsWriteDb( |
| 704 | sqlite3_file *pProtoFile, |
| 705 | const void *zBuf, |
| 706 | int iAmt, |
| 707 | sqlite_int64 iOfst |
| 708 | ){ |
| 709 | KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 710 | unsigned int pgno; |
| 711 | char zKey[30]; |
| 712 | char *aData = pFile->aData; |
| 713 | int rc; |
| 714 | SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); |
| 715 | assert( iAmt>=512 && iAmt<=65536 ); |
| 716 | assert( (iAmt & (iAmt-1))==0 ); |
| 717 | assert( pFile->szPage<0 || pFile->szPage==iAmt ); |
| 718 | pFile->szPage = iAmt; |
| 719 | pgno = 1 + iOfst/iAmt; |
| 720 | sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno); |
| 721 | kvvfsEncode(zBuf, iAmt, aData); |
| 722 | rc = sqlite3KvvfsMethods.xRcrdWrite(pFile->zClass, zKey, aData); |
| 723 | if( 0==rc ){ |
| 724 | if( iOfst+iAmt > pFile->szDb ){ |
| 725 | pFile->szDb = iOfst + iAmt; |
| 726 | } |
| 727 | } |
| 728 | return rc; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | ** Truncate an kvvfs-file. |
| 733 | */ |
| 734 | static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){ |
| 735 | KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 736 | SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size)); |
| 737 | assert( size==0 ); |
| 738 | sqlite3KvvfsMethods.xRcrdDelete(pFile->zClass, "jrnl"); |
| 739 | sqlite3_free(pFile->aJrnl); |
| 740 | pFile->aJrnl = 0; |
| 741 | pFile->nJrnl = 0; |
| 742 | return SQLITE_OK; |
| 743 | } |
| 744 | static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){ |
| 745 | KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 746 | if( pFile->szDb>size |
| 747 | && pFile->szPage>0 |
| 748 | && (size % pFile->szPage)==0 |
| 749 | ){ |
| 750 | char zKey[50]; |
| 751 | unsigned int pgno, pgnoMax; |
| 752 | SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size)); |
| 753 | pgno = 1 + size/pFile->szPage; |
| 754 | pgnoMax = 2 + pFile->szDb/pFile->szPage; |
| 755 | while( pgno<=pgnoMax ){ |
| 756 | sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno); |
| 757 | sqlite3KvvfsMethods.xRcrdDelete(pFile->zClass, zKey); |
| 758 | pgno++; |
| 759 | } |
| 760 | pFile->szDb = size; |
| 761 | return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR : SQLITE_OK; |
| 762 | } |
| 763 | return SQLITE_IOERR; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | ** Sync an kvvfs-file. |
| 768 | */ |
| 769 | static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){ |
| 770 | int i, n; |
| 771 | KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 772 | char *zOut; |
| 773 | SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass)); |
| 774 | if( pFile->nJrnl<=0 ){ |
| 775 | return kvvfsTruncateJrnl(pProtoFile, 0); |
| 776 | } |
| 777 | zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 ); |
| 778 | if( zOut==0 ){ |
| 779 | return SQLITE_IOERR_NOMEM; |
| 780 | } |
| 781 | n = pFile->nJrnl; |
| 782 | i = 0; |
| 783 | do{ |
| 784 | zOut[i++] = 'a' + (n%26); |
| 785 | n /= 26; |
| 786 | }while( n>0 ); |
| 787 | zOut[i++] = ' '; |
| 788 | kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]); |
| 789 | i = sqlite3KvvfsMethods.xRcrdWrite(pFile->zClass, "jrnl", zOut); |
| 790 | sqlite3_free(zOut); |
| 791 | return i ? SQLITE_IOERR : SQLITE_OK; |
| 792 | } |
| 793 | static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){ |
| 794 | return SQLITE_OK; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | ** Return the current file-size of an kvvfs-file. |
| 799 | */ |
| 800 | static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){ |
| 801 | KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 802 | SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass)); |
| 803 | *pSize = pFile->nJrnl; |
| 804 | return SQLITE_OK; |
| 805 | } |
| 806 | static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){ |
| 807 | KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 808 | SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass)); |
| 809 | if( pFile->szDb>=0 ){ |
| 810 | *pSize = pFile->szDb; |
| 811 | }else{ |
| 812 | *pSize = kvvfsReadFileSize(pFile); |
| 813 | } |
| 814 | return SQLITE_OK; |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | ** Lock an kvvfs-file. |
| 819 | */ |
| 820 | static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){ |
| 821 | KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 822 | assert( !pFile->isJournal ); |
| 823 | SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock)); |
| 824 | |
| 825 | if( eLock!=SQLITE_LOCK_NONE ){ |
| 826 | pFile->szDb = kvvfsReadFileSize(pFile); |
| 827 | } |
| 828 | return SQLITE_OK; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | ** Unlock an kvvfs-file. |
| 833 | */ |
| 834 | static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){ |
| 835 | KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 836 | assert( !pFile->isJournal ); |
| 837 | SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock)); |
| 838 | if( eLock==SQLITE_LOCK_NONE ){ |
| 839 | pFile->szDb = -1; |
| 840 | } |
| 841 | return SQLITE_OK; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | ** Check if another file-handle holds a RESERVED lock on an kvvfs-file. |
| 846 | */ |
| 847 | static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){ |
| 848 | SQLITE_KV_LOG(("xCheckReservedLock\n")); |
| 849 | *pResOut = 0; |
| 850 | return SQLITE_OK; |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | ** File control method. For custom operations on an kvvfs-file. |
| 855 | */ |
| 856 | static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){ |
| 857 | SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op)); |
| 858 | return SQLITE_NOTFOUND; |
| 859 | } |
| 860 | static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){ |
| 861 | SQLITE_KV_LOG(("xFileControl(%d) on database\n", op)); |
| 862 | if( op==SQLITE_FCNTL_SYNC ){ |
| 863 | KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 864 | int rc = SQLITE_OK; |
| 865 | SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass)); |
| 866 | if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){ |
| 867 | rc = SQLITE_IOERR; |
| 868 | } |
| 869 | return rc; |
| 870 | } |
| 871 | return SQLITE_NOTFOUND; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | ** Return the sector-size in bytes for an kvvfs-file. |
| 876 | */ |
| 877 | static int kvvfsSectorSize(sqlite3_file *pFile){ |
| 878 | return 512; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | ** Return the device characteristic flags supported by an kvvfs-file. |
| 883 | */ |
| 884 | static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){ |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | /****** sqlite3_vfs methods *************************************************/ |
| 889 | |
| 890 | /* |
| 891 | ** Open an kvvfs file handle. |
| 892 | */ |
| 893 | static int kvvfsOpen( |
| 894 | sqlite3_vfs *pProtoVfs, |
| 895 | const char *zName, |
| 896 | sqlite3_file *pProtoFile, |
| 897 | int flags, |
| 898 | int *pOutFlags |
| 899 | ){ |
| 900 | KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 901 | if( zName==0 ) zName = ""; |
| 902 | SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName)); |
| 903 | assert(!pFile->zClass); |
| 904 | assert(!pFile->aData); |
| 905 | assert(!pFile->aJrnl); |
| 906 | assert(!pFile->nJrnl); |
| 907 | assert(!pFile->base.pMethods); |
| 908 | pFile->szPage = -1; |
| 909 | pFile->szDb = -1; |
| 910 | if( 0==sqlite3_strglob("*-journal", zName) ){ |
| 911 | pFile->isJournal = 1; |
| 912 | pFile->base.pMethods = &kvvfs_jrnl_io_methods; |
| 913 | if( 0==strcmp("session-journal",zName) ){ |
| 914 | pFile->zClass = "session"; |
| 915 | }else if( 0==strcmp("local-journal",zName) ){ |
| 916 | pFile->zClass = "local"; |
| 917 | } |
| 918 | }else{ |
| 919 | pFile->isJournal = 0; |
| 920 | pFile->base.pMethods = &kvvfs_db_io_methods; |
| 921 | } |
| 922 | if( !pFile->zClass ){ |
| 923 | #ifdef SQLITE_WASM |
| 924 | if( strlen(zName) >= (KVRECORD_KEY_SZ |
| 925 | - 6 /* "kvvfs-" */ |
| 926 | - 11 /* "-NNNNNNNNNNN" */) ){ |
| 927 | return SQLITE_CANTOPEN; |
| 928 | } |
| 929 | #else |
| 930 | if( 0!=strcmp(zName, "local") && 0!=strcmp(zName, "session") ){ |
| 931 | /* Historical naming restriction which journaling depends on. */ |
| 932 | return SQLITE_CANTOPEN; |
| 933 | } |
| 934 | #endif |
| 935 | pFile->zClass = zName; |
| 936 | } |
| 937 | pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ); |
| 938 | if( pFile->aData==0 ){ |
| 939 | return SQLITE_NOMEM; |
| 940 | } |
| 941 | return SQLITE_OK; |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | ** Delete the file located at zPath. If the dirSync argument is true, |
| 946 | ** ensure the file-system modifications are synced to disk before |
| 947 | ** returning. |
| 948 | */ |
| 949 | static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 950 | int rc /* The JS impl can fail with OOM in argument conversion */; |
| 951 | if( strcmp(zPath, "local-journal")==0 ){ |
| 952 | rc = sqlite3KvvfsMethods.xRcrdDelete("local", "jrnl"); |
| 953 | }else |
| 954 | if( strcmp(zPath, "session-journal")==0 ){ |
| 955 | rc = sqlite3KvvfsMethods.xRcrdDelete("session", "jrnl"); |
| 956 | } |
| 957 | else{ |
| 958 | rc = 0; |
| 959 | } |
| 960 | return rc; |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | ** Test for access permissions. Return true if the requested permission |
| 965 | ** is available, or false otherwise. |
| 966 | */ |
| 967 | static int kvvfsAccess( |
| 968 | sqlite3_vfs *pProtoVfs, |
| 969 | const char *zPath, |
| 970 | int flags, |
| 971 | int *pResOut |
| 972 | ){ |
| 973 | SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath)); |
| 974 | #if 0 && defined(SQLITE_WASM) |
| 975 | /* |
| 976 | ** This is not having the desired effect in the JS bindings. |
| 977 | ** It's ostensibly the same logic as the #else block, but |
| 978 | ** it's not behaving that way. |
| 979 | ** |
| 980 | ** In JS we map all zPaths to Storage objects, and -journal files |
| 981 | ** are mapped to the storage for the main db (which is is exactly |
| 982 | ** what the mapping of "local-journal" -> "local" is doing). |
| 983 | */ |
| 984 | const char *zKey = (0==sqlite3_strglob("*-journal", zPath)) |
| 985 | ? "jrnl" : "sz"; |
| 986 | *pResOut = |
| 987 | sqlite3KvvfsMethods.xRcrdRead(zPath, zKey, 0, 0)>0; |
| 988 | #else |
| 989 | if( strcmp(zPath, "local-journal")==0 ){ |
| 990 | *pResOut = |
| 991 | sqlite3KvvfsMethods.xRcrdRead("local", "jrnl", 0, 0)>0; |
| 992 | }else |
| 993 | if( strcmp(zPath, "session-journal")==0 ){ |
| 994 | *pResOut = |
| 995 | sqlite3KvvfsMethods.xRcrdRead("session", "jrnl", 0, 0)>0; |
| 996 | }else |
| 997 | if( strcmp(zPath, "local")==0 ){ |
| 998 | *pResOut = |
| 999 | sqlite3KvvfsMethods.xRcrdRead("local", "sz", 0, 0)>0; |
| 1000 | }else |
| 1001 | if( strcmp(zPath, "session")==0 ){ |
| 1002 | *pResOut = |
| 1003 | sqlite3KvvfsMethods.xRcrdRead("session", "sz", 0, 0)>0; |
| 1004 | }else |
| 1005 | { |
| 1006 | *pResOut = 0; |
| 1007 | } |
| 1008 | /*all current JS tests avoid triggering: assert( *pResOut == 0 ); */ |
| 1009 | #endif |
| 1010 | SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut)); |
| 1011 | return SQLITE_OK; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | ** Populate buffer zOut with the full canonical pathname corresponding |
| 1016 | ** to the pathname in zPath. zOut is guaranteed to point to a buffer |
| 1017 | ** of at least (INST_MAX_PATHNAME+1) bytes. |
| 1018 | */ |
| 1019 | static int kvvfsFullPathname( |
| 1020 | sqlite3_vfs *pVfs, |
| 1021 | const char *zPath, |
| 1022 | int nOut, |
| 1023 | char *zOut |
| 1024 | ){ |
| 1025 | size_t nPath; |
| 1026 | #ifdef SQLITE_OS_KV_ALWAYS_LOCAL |
| 1027 | zPath = "local"; |
| 1028 | #endif |
| 1029 | nPath = strlen(zPath); |
| 1030 | SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath)); |
| 1031 | if( nOut<nPath+1 ) nPath = nOut - 1; |
| 1032 | memcpy(zOut, zPath, nPath); |
| 1033 | zOut[nPath] = 0; |
| 1034 | return SQLITE_OK; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | ** Open the dynamic library located at zPath and return a handle. |
| 1039 | */ |
| 1040 | static void *kvvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | ** Populate the buffer pointed to by zBufOut with nByte bytes of |
| 1046 | ** random data. |
| 1047 | */ |
| 1048 | static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 1049 | memset(zBufOut, 0, nByte); |
| 1050 | return nByte; |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | ** Sleep for nMicro microseconds. Return the number of microseconds |
| 1055 | ** actually slept. |
| 1056 | */ |
| 1057 | static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 1058 | return SQLITE_OK; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | ** Return the current time as a Julian Day number in *pTimeOut. |
| 1063 | */ |
| 1064 | static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 1065 | sqlite3_int64 i = 0; |
| 1066 | int rc; |
| 1067 | rc = kvvfsCurrentTimeInt64(0, &i); |
| 1068 | *pTimeOut = i/86400000.0; |
| 1069 | return rc; |
| 1070 | } |
| 1071 | #include <sys/time.h> |
| 1072 | static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ |
| 1073 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 1074 | struct timeval sNow; |
| 1075 | (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ |
| 1076 | *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 1077 | return SQLITE_OK; |
| 1078 | } |
| 1079 | #endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */ |
| 1080 | |
| 1081 | #if SQLITE_OS_KV |
| 1082 | /* |
| 1083 | ** This routine is called initialize the KV-vfs as the default VFS. |
| 1084 | */ |
| 1085 | int sqlite3_os_init(void){ |
| 1086 | return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1); |
| 1087 | } |
| 1088 | int sqlite3_os_end(void){ |
| 1089 | return SQLITE_OK; |
| 1090 | } |
| 1091 | #endif /* SQLITE_OS_KV */ |
| 1092 | |
| 1093 | #if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) |
| 1094 | int sqlite3KvvfsInit(void){ |
| 1095 | return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0); |
| 1096 | } |
| 1097 | #endif |