sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / misc / appendvfs.c
| 1 | /* |
| 2 | ** 2017-10-20 |
| 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 implements a VFS shim that allows an SQLite database to be |
| 14 | ** appended onto the end of some other file, such as an executable. |
| 15 | ** |
| 16 | ** A special record must appear at the end of the file that identifies the |
| 17 | ** file as an appended database and provides the offset to the first page |
| 18 | ** of the exposed content. (Or, it is the length of the content prefix.) |
| 19 | ** For best performance page 1 should be located at a disk page boundary, |
| 20 | ** though that is not required. |
| 21 | ** |
| 22 | ** When opening a database using this VFS, the connection might treat |
| 23 | ** the file as an ordinary SQLite database, or it might treat it as a |
| 24 | ** database appended onto some other file. The decision is made by |
| 25 | ** applying the following rules in order: |
| 26 | ** |
| 27 | ** (1) An empty file is an ordinary database. |
| 28 | ** |
| 29 | ** (2) If the file ends with the appendvfs trailer string |
| 30 | ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database. |
| 31 | ** |
| 32 | ** (3) If the file begins with the standard SQLite prefix string |
| 33 | ** "SQLite format 3", that file is an ordinary database. |
| 34 | ** |
| 35 | ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is |
| 36 | ** set, then a new database is appended to the already existing file. |
| 37 | ** |
| 38 | ** (5) Otherwise, SQLITE_CANTOPEN is returned. |
| 39 | ** |
| 40 | ** To avoid unnecessary complications with the PENDING_BYTE, the size of |
| 41 | ** the file containing the database is limited to 1GiB. (1073741824 bytes) |
| 42 | ** This VFS will not read or write past the 1GiB mark. This restriction |
| 43 | ** might be lifted in future versions. For now, if you need a larger |
| 44 | ** database, then keep it in a separate file. |
| 45 | ** |
| 46 | ** If the file being opened is a plain database (not an appended one), then |
| 47 | ** this shim is a pass-through into the default underlying VFS. (rule 3) |
| 48 | **/ |
| 49 | #include "sqlite3ext.h" |
| 50 | SQLITE_EXTENSION_INIT1 |
| 51 | #include <string.h> |
| 52 | #include <assert.h> |
| 53 | |
| 54 | /* The append mark at the end of the database is: |
| 55 | ** |
| 56 | ** Start-Of-SQLite3-NNNNNNNN |
| 57 | ** 123456789 123456789 12345 |
| 58 | ** |
| 59 | ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is |
| 60 | ** the offset to page 1, and also the length of the prefix content. |
| 61 | */ |
| 62 | #define APND_MARK_PREFIX "Start-Of-SQLite3-" |
| 63 | #define APND_MARK_PREFIX_SZ 17 |
| 64 | #define APND_MARK_FOS_SZ 8 |
| 65 | #define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ) |
| 66 | |
| 67 | /* |
| 68 | ** Maximum size of the combined prefix + database + append-mark. This |
| 69 | ** must be less than 0x40000000 to avoid locking issues on Windows. |
| 70 | */ |
| 71 | #define APND_MAX_SIZE (0x40000000) |
| 72 | |
| 73 | /* |
| 74 | ** Try to align the database to an even multiple of APND_ROUNDUP bytes. |
| 75 | */ |
| 76 | #ifndef APND_ROUNDUP |
| 77 | #define APND_ROUNDUP 4096 |
| 78 | #endif |
| 79 | #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1)) |
| 80 | #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK) |
| 81 | |
| 82 | /* |
| 83 | ** Forward declaration of objects used by this utility |
| 84 | */ |
| 85 | typedef struct sqlite3_vfs ApndVfs; |
| 86 | typedef struct ApndFile ApndFile; |
| 87 | |
| 88 | /* Access to a lower-level VFS that (might) implement dynamic loading, |
| 89 | ** access to randomness, etc. |
| 90 | */ |
| 91 | #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) |
| 92 | #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1)) |
| 93 | |
| 94 | /* An open appendvfs file |
| 95 | ** |
| 96 | ** An instance of this structure describes the appended database file. |
| 97 | ** A separate sqlite3_file object is always appended. The appended |
| 98 | ** sqlite3_file object (which can be accessed using ORIGFILE()) describes |
| 99 | ** the entire file, including the prefix, the database, and the |
| 100 | ** append-mark. |
| 101 | ** |
| 102 | ** The structure of an AppendVFS database is like this: |
| 103 | ** |
| 104 | ** +-------------+---------+----------+-------------+ |
| 105 | ** | prefix-file | padding | database | append-mark | |
| 106 | ** +-------------+---------+----------+-------------+ |
| 107 | ** ^ ^ |
| 108 | ** | | |
| 109 | ** iPgOne iMark |
| 110 | ** |
| 111 | ** |
| 112 | ** "prefix file" - file onto which the database has been appended. |
| 113 | ** "padding" - zero or more bytes inserted so that "database" |
| 114 | ** starts on an APND_ROUNDUP boundary |
| 115 | ** "database" - The SQLite database file |
| 116 | ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates |
| 117 | ** the offset from the start of prefix-file to the start |
| 118 | ** of "database". |
| 119 | ** |
| 120 | ** The size of the database is iMark - iPgOne. |
| 121 | ** |
| 122 | ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value |
| 123 | ** of iPgOne stored as a big-ending 64-bit integer. |
| 124 | ** |
| 125 | ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE). |
| 126 | ** Or, iMark is -1 to indicate that it has not yet been written. |
| 127 | */ |
| 128 | struct ApndFile { |
| 129 | sqlite3_file base; /* Subclass. MUST BE FIRST! */ |
| 130 | sqlite3_int64 iPgOne; /* Offset to the start of the database */ |
| 131 | sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */ |
| 132 | /* Always followed by another sqlite3_file that describes the whole file */ |
| 133 | }; |
| 134 | |
| 135 | /* |
| 136 | ** Methods for ApndFile |
| 137 | */ |
| 138 | static int apndClose(sqlite3_file*); |
| 139 | static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 140 | static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); |
| 141 | static int apndTruncate(sqlite3_file*, sqlite3_int64 size); |
| 142 | static int apndSync(sqlite3_file*, int flags); |
| 143 | static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize); |
| 144 | static int apndLock(sqlite3_file*, int); |
| 145 | static int apndUnlock(sqlite3_file*, int); |
| 146 | static int apndCheckReservedLock(sqlite3_file*, int *pResOut); |
| 147 | static int apndFileControl(sqlite3_file*, int op, void *pArg); |
| 148 | static int apndSectorSize(sqlite3_file*); |
| 149 | static int apndDeviceCharacteristics(sqlite3_file*); |
| 150 | static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**); |
| 151 | static int apndShmLock(sqlite3_file*, int offset, int n, int flags); |
| 152 | static void apndShmBarrier(sqlite3_file*); |
| 153 | static int apndShmUnmap(sqlite3_file*, int deleteFlag); |
| 154 | static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); |
| 155 | static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); |
| 156 | |
| 157 | /* |
| 158 | ** Methods for ApndVfs |
| 159 | */ |
| 160 | static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
| 161 | static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir); |
| 162 | static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
| 163 | static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); |
| 164 | static void *apndDlOpen(sqlite3_vfs*, const char *zFilename); |
| 165 | static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg); |
| 166 | static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); |
| 167 | static void apndDlClose(sqlite3_vfs*, void*); |
| 168 | static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut); |
| 169 | static int apndSleep(sqlite3_vfs*, int microseconds); |
| 170 | static int apndCurrentTime(sqlite3_vfs*, double*); |
| 171 | static int apndGetLastError(sqlite3_vfs*, int, char *); |
| 172 | static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); |
| 173 | static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr); |
| 174 | static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z); |
| 175 | static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName); |
| 176 | |
| 177 | static sqlite3_vfs apnd_vfs = { |
| 178 | 3, /* iVersion (set when registered) */ |
| 179 | 0, /* szOsFile (set when registered) */ |
| 180 | 1024, /* mxPathname */ |
| 181 | 0, /* pNext */ |
| 182 | "apndvfs", /* zName */ |
| 183 | 0, /* pAppData (set when registered) */ |
| 184 | apndOpen, /* xOpen */ |
| 185 | apndDelete, /* xDelete */ |
| 186 | apndAccess, /* xAccess */ |
| 187 | apndFullPathname, /* xFullPathname */ |
| 188 | apndDlOpen, /* xDlOpen */ |
| 189 | apndDlError, /* xDlError */ |
| 190 | apndDlSym, /* xDlSym */ |
| 191 | apndDlClose, /* xDlClose */ |
| 192 | apndRandomness, /* xRandomness */ |
| 193 | apndSleep, /* xSleep */ |
| 194 | apndCurrentTime, /* xCurrentTime */ |
| 195 | apndGetLastError, /* xGetLastError */ |
| 196 | apndCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 197 | apndSetSystemCall, /* xSetSystemCall */ |
| 198 | apndGetSystemCall, /* xGetSystemCall */ |
| 199 | apndNextSystemCall /* xNextSystemCall */ |
| 200 | }; |
| 201 | |
| 202 | static const sqlite3_io_methods apnd_io_methods = { |
| 203 | 3, /* iVersion */ |
| 204 | apndClose, /* xClose */ |
| 205 | apndRead, /* xRead */ |
| 206 | apndWrite, /* xWrite */ |
| 207 | apndTruncate, /* xTruncate */ |
| 208 | apndSync, /* xSync */ |
| 209 | apndFileSize, /* xFileSize */ |
| 210 | apndLock, /* xLock */ |
| 211 | apndUnlock, /* xUnlock */ |
| 212 | apndCheckReservedLock, /* xCheckReservedLock */ |
| 213 | apndFileControl, /* xFileControl */ |
| 214 | apndSectorSize, /* xSectorSize */ |
| 215 | apndDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 216 | apndShmMap, /* xShmMap */ |
| 217 | apndShmLock, /* xShmLock */ |
| 218 | apndShmBarrier, /* xShmBarrier */ |
| 219 | apndShmUnmap, /* xShmUnmap */ |
| 220 | apndFetch, /* xFetch */ |
| 221 | apndUnfetch /* xUnfetch */ |
| 222 | }; |
| 223 | |
| 224 | /* |
| 225 | ** Close an apnd-file. |
| 226 | */ |
| 227 | static int apndClose(sqlite3_file *pFile){ |
| 228 | pFile = ORIGFILE(pFile); |
| 229 | return pFile->pMethods->xClose(pFile); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | ** Read data from an apnd-file. |
| 234 | */ |
| 235 | static int apndRead( |
| 236 | sqlite3_file *pFile, |
| 237 | void *zBuf, |
| 238 | int iAmt, |
| 239 | sqlite_int64 iOfst |
| 240 | ){ |
| 241 | ApndFile *paf = (ApndFile *)pFile; |
| 242 | pFile = ORIGFILE(pFile); |
| 243 | return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | ** Add the append-mark onto what should become the end of the file. |
| 248 | * If and only if this succeeds, internal ApndFile.iMark is updated. |
| 249 | * Parameter iWriteEnd is the appendvfs-relative offset of the new mark. |
| 250 | */ |
| 251 | static int apndWriteMark( |
| 252 | ApndFile *paf, |
| 253 | sqlite3_file *pFile, |
| 254 | sqlite_int64 iWriteEnd |
| 255 | ){ |
| 256 | sqlite_int64 iPgOne = paf->iPgOne; |
| 257 | unsigned char a[APND_MARK_SIZE]; |
| 258 | int i = APND_MARK_FOS_SZ; |
| 259 | int rc; |
| 260 | assert(pFile == ORIGFILE(paf)); |
| 261 | memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ); |
| 262 | while( --i >= 0 ){ |
| 263 | a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff); |
| 264 | iPgOne >>= 8; |
| 265 | } |
| 266 | iWriteEnd += paf->iPgOne; |
| 267 | if( SQLITE_OK==(rc = pFile->pMethods->xWrite |
| 268 | (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){ |
| 269 | paf->iMark = iWriteEnd; |
| 270 | } |
| 271 | return rc; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | ** Write data to an apnd-file. |
| 276 | */ |
| 277 | static int apndWrite( |
| 278 | sqlite3_file *pFile, |
| 279 | const void *zBuf, |
| 280 | int iAmt, |
| 281 | sqlite_int64 iOfst |
| 282 | ){ |
| 283 | ApndFile *paf = (ApndFile *)pFile; |
| 284 | sqlite_int64 iWriteEnd = iOfst + iAmt; |
| 285 | if( iWriteEnd + paf->iPgOne >= APND_MAX_SIZE-APND_MARK_SIZE ){ |
| 286 | return SQLITE_FULL; |
| 287 | } |
| 288 | pFile = ORIGFILE(pFile); |
| 289 | /* If append-mark is absent or will be overwritten, write it. */ |
| 290 | if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){ |
| 291 | int rc = apndWriteMark(paf, pFile, iWriteEnd); |
| 292 | if( SQLITE_OK!=rc ) return rc; |
| 293 | } |
| 294 | return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | ** Truncate an apnd-file. |
| 299 | */ |
| 300 | static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
| 301 | ApndFile *paf = (ApndFile *)pFile; |
| 302 | pFile = ORIGFILE(pFile); |
| 303 | /* The append mark goes out first so truncate failure does not lose it. */ |
| 304 | if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR; |
| 305 | /* Truncate underlying file just past append mark */ |
| 306 | return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | ** Sync an apnd-file. |
| 311 | */ |
| 312 | static int apndSync(sqlite3_file *pFile, int flags){ |
| 313 | pFile = ORIGFILE(pFile); |
| 314 | return pFile->pMethods->xSync(pFile, flags); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | ** Return the current file-size of an apnd-file. |
| 319 | ** If the append mark is not yet there, the file-size is 0. |
| 320 | */ |
| 321 | static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
| 322 | ApndFile *paf = (ApndFile *)pFile; |
| 323 | *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0; |
| 324 | return SQLITE_OK; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | ** Lock an apnd-file. |
| 329 | */ |
| 330 | static int apndLock(sqlite3_file *pFile, int eLock){ |
| 331 | pFile = ORIGFILE(pFile); |
| 332 | return pFile->pMethods->xLock(pFile, eLock); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | ** Unlock an apnd-file. |
| 337 | */ |
| 338 | static int apndUnlock(sqlite3_file *pFile, int eLock){ |
| 339 | pFile = ORIGFILE(pFile); |
| 340 | return pFile->pMethods->xUnlock(pFile, eLock); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | ** Check if another file-handle holds a RESERVED lock on an apnd-file. |
| 345 | */ |
| 346 | static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 347 | pFile = ORIGFILE(pFile); |
| 348 | return pFile->pMethods->xCheckReservedLock(pFile, pResOut); |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | ** File control method. For custom operations on an apnd-file. |
| 353 | */ |
| 354 | static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 355 | ApndFile *paf = (ApndFile *)pFile; |
| 356 | int rc; |
| 357 | pFile = ORIGFILE(pFile); |
| 358 | if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne; |
| 359 | rc = pFile->pMethods->xFileControl(pFile, op, pArg); |
| 360 | if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){ |
| 361 | *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg); |
| 362 | } |
| 363 | return rc; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | ** Return the sector-size in bytes for an apnd-file. |
| 368 | */ |
| 369 | static int apndSectorSize(sqlite3_file *pFile){ |
| 370 | pFile = ORIGFILE(pFile); |
| 371 | return pFile->pMethods->xSectorSize(pFile); |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | ** Return the device characteristic flags supported by an apnd-file. |
| 376 | */ |
| 377 | static int apndDeviceCharacteristics(sqlite3_file *pFile){ |
| 378 | pFile = ORIGFILE(pFile); |
| 379 | return pFile->pMethods->xDeviceCharacteristics(pFile); |
| 380 | } |
| 381 | |
| 382 | /* Create a shared memory file mapping */ |
| 383 | static int apndShmMap( |
| 384 | sqlite3_file *pFile, |
| 385 | int iPg, |
| 386 | int pgsz, |
| 387 | int bExtend, |
| 388 | void volatile **pp |
| 389 | ){ |
| 390 | pFile = ORIGFILE(pFile); |
| 391 | return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); |
| 392 | } |
| 393 | |
| 394 | /* Perform locking on a shared-memory segment */ |
| 395 | static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){ |
| 396 | pFile = ORIGFILE(pFile); |
| 397 | return pFile->pMethods->xShmLock(pFile,offset,n,flags); |
| 398 | } |
| 399 | |
| 400 | /* Memory barrier operation on shared memory */ |
| 401 | static void apndShmBarrier(sqlite3_file *pFile){ |
| 402 | pFile = ORIGFILE(pFile); |
| 403 | pFile->pMethods->xShmBarrier(pFile); |
| 404 | } |
| 405 | |
| 406 | /* Unmap a shared memory segment */ |
| 407 | static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){ |
| 408 | pFile = ORIGFILE(pFile); |
| 409 | return pFile->pMethods->xShmUnmap(pFile,deleteFlag); |
| 410 | } |
| 411 | |
| 412 | /* Fetch a page of a memory-mapped file */ |
| 413 | static int apndFetch( |
| 414 | sqlite3_file *pFile, |
| 415 | sqlite3_int64 iOfst, |
| 416 | int iAmt, |
| 417 | void **pp |
| 418 | ){ |
| 419 | ApndFile *p = (ApndFile *)pFile; |
| 420 | if( p->iMark < 0 || iOfst+iAmt > p->iMark ){ |
| 421 | return SQLITE_IOERR; /* Cannot read what is not yet there. */ |
| 422 | } |
| 423 | pFile = ORIGFILE(pFile); |
| 424 | return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp); |
| 425 | } |
| 426 | |
| 427 | /* Release a memory-mapped page */ |
| 428 | static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ |
| 429 | ApndFile *p = (ApndFile *)pFile; |
| 430 | pFile = ORIGFILE(pFile); |
| 431 | return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage); |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | ** Try to read the append-mark off the end of a file. Return the |
| 436 | ** start of the appended database if the append-mark is present. |
| 437 | ** If there is no valid append-mark, return -1; |
| 438 | ** |
| 439 | ** An append-mark is only valid if the NNNNNNNN start-of-database offset |
| 440 | ** indicates that the appended database contains at least one page. The |
| 441 | ** start-of-database value must be a multiple of 512. |
| 442 | */ |
| 443 | static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){ |
| 444 | int rc, i; |
| 445 | sqlite3_int64 iMark; |
| 446 | int msbs = 8 * (APND_MARK_FOS_SZ-1); |
| 447 | unsigned char a[APND_MARK_SIZE]; |
| 448 | |
| 449 | if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1; |
| 450 | rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE); |
| 451 | if( rc ) return -1; |
| 452 | if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1; |
| 453 | iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs; |
| 454 | for(i=1; i<8; i++){ |
| 455 | msbs -= 8; |
| 456 | iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs; |
| 457 | } |
| 458 | if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1; |
| 459 | if( iMark & 0x1ff ) return -1; |
| 460 | return iMark; |
| 461 | } |
| 462 | |
| 463 | static const char apvfsSqliteHdr[] = "SQLite format 3"; |
| 464 | /* |
| 465 | ** Check to see if the file is an appendvfs SQLite database file. |
| 466 | ** Return true iff it is such. Parameter sz is the file's size. |
| 467 | */ |
| 468 | static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){ |
| 469 | int rc; |
| 470 | char zHdr[16]; |
| 471 | sqlite3_int64 iMark = apndReadMark(sz, pFile); |
| 472 | if( iMark>=0 ){ |
| 473 | /* If file has the correct end-marker, the expected odd size, and the |
| 474 | ** SQLite DB type marker where the end-marker puts it, then it |
| 475 | ** is an appendvfs database. |
| 476 | */ |
| 477 | rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark); |
| 478 | if( SQLITE_OK==rc |
| 479 | && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0 |
| 480 | && (sz & 0x1ff) == APND_MARK_SIZE |
| 481 | && sz>=512+APND_MARK_SIZE |
| 482 | ){ |
| 483 | return 1; /* It's an appendvfs database */ |
| 484 | } |
| 485 | } |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | ** Check to see if the file is an ordinary SQLite database file. |
| 491 | ** Return true iff so. Parameter sz is the file's size. |
| 492 | */ |
| 493 | static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){ |
| 494 | char zHdr[16]; |
| 495 | if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */ |
| 496 | || (sz & 0x1ff) != 0 |
| 497 | || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0) |
| 498 | || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0 |
| 499 | ){ |
| 500 | return 0; |
| 501 | }else{ |
| 502 | return 1; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | ** Open an apnd file handle. |
| 508 | */ |
| 509 | static int apndOpen( |
| 510 | sqlite3_vfs *pApndVfs, |
| 511 | const char *zName, |
| 512 | sqlite3_file *pFile, |
| 513 | int flags, |
| 514 | int *pOutFlags |
| 515 | ){ |
| 516 | ApndFile *pApndFile = (ApndFile*)pFile; |
| 517 | sqlite3_file *pBaseFile = ORIGFILE(pFile); |
| 518 | sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs); |
| 519 | int rc; |
| 520 | sqlite3_int64 sz = 0; |
| 521 | if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ |
| 522 | /* The appendvfs is not to be used for transient or temporary databases. |
| 523 | ** Just use the base VFS open to initialize the given file object and |
| 524 | ** open the underlying file. (Appendvfs is then unused for this file.) |
| 525 | */ |
| 526 | return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags); |
| 527 | } |
| 528 | memset(pApndFile, 0, sizeof(ApndFile)); |
| 529 | pFile->pMethods = &apnd_io_methods; |
| 530 | pApndFile->iMark = -1; /* Append mark not yet written */ |
| 531 | |
| 532 | rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags); |
| 533 | if( rc==SQLITE_OK ){ |
| 534 | rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz); |
| 535 | if( rc ){ |
| 536 | pBaseFile->pMethods->xClose(pBaseFile); |
| 537 | } |
| 538 | } |
| 539 | if( rc ){ |
| 540 | pFile->pMethods = 0; |
| 541 | return rc; |
| 542 | } |
| 543 | if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){ |
| 544 | /* The file being opened appears to be just an ordinary DB. Copy |
| 545 | ** the base dispatch-table so this instance mimics the base VFS. |
| 546 | */ |
| 547 | memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile); |
| 548 | return SQLITE_OK; |
| 549 | } |
| 550 | pApndFile->iPgOne = apndReadMark(sz, pFile); |
| 551 | if( pApndFile->iPgOne>=0 ){ |
| 552 | pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */ |
| 553 | return SQLITE_OK; |
| 554 | } |
| 555 | if( (flags & SQLITE_OPEN_CREATE)==0 ){ |
| 556 | pBaseFile->pMethods->xClose(pBaseFile); |
| 557 | rc = SQLITE_CANTOPEN; |
| 558 | pFile->pMethods = 0; |
| 559 | }else{ |
| 560 | /* Round newly added appendvfs location to #define'd page boundary. |
| 561 | ** Note that nothing has yet been written to the underlying file. |
| 562 | ** The append mark will be written along with first content write. |
| 563 | ** Until then, paf->iMark value indicates it is not yet written. |
| 564 | */ |
| 565 | pApndFile->iPgOne = APND_START_ROUNDUP(sz); |
| 566 | } |
| 567 | return rc; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | ** Delete an apnd file. |
| 572 | ** For an appendvfs, this could mean delete the appendvfs portion, |
| 573 | ** leaving the appendee as it was before it gained an appendvfs. |
| 574 | ** For now, this code deletes the underlying file too. |
| 575 | */ |
| 576 | static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 577 | return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | ** All other VFS methods are pass-thrus. |
| 582 | */ |
| 583 | static int apndAccess( |
| 584 | sqlite3_vfs *pVfs, |
| 585 | const char *zPath, |
| 586 | int flags, |
| 587 | int *pResOut |
| 588 | ){ |
| 589 | return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); |
| 590 | } |
| 591 | static int apndFullPathname( |
| 592 | sqlite3_vfs *pVfs, |
| 593 | const char *zPath, |
| 594 | int nOut, |
| 595 | char *zOut |
| 596 | ){ |
| 597 | return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); |
| 598 | } |
| 599 | static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 600 | return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); |
| 601 | } |
| 602 | static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ |
| 603 | ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); |
| 604 | } |
| 605 | static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ |
| 606 | return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); |
| 607 | } |
| 608 | static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 609 | ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); |
| 610 | } |
| 611 | static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 612 | return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); |
| 613 | } |
| 614 | static int apndSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 615 | return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); |
| 616 | } |
| 617 | static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 618 | return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); |
| 619 | } |
| 620 | static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){ |
| 621 | return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); |
| 622 | } |
| 623 | static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ |
| 624 | return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); |
| 625 | } |
| 626 | static int apndSetSystemCall( |
| 627 | sqlite3_vfs *pVfs, |
| 628 | const char *zName, |
| 629 | sqlite3_syscall_ptr pCall |
| 630 | ){ |
| 631 | return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); |
| 632 | } |
| 633 | static sqlite3_syscall_ptr apndGetSystemCall( |
| 634 | sqlite3_vfs *pVfs, |
| 635 | const char *zName |
| 636 | ){ |
| 637 | return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); |
| 638 | } |
| 639 | static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){ |
| 640 | return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); |
| 641 | } |
| 642 | |
| 643 | |
| 644 | #ifdef _WIN32 |
| 645 | __declspec(dllexport) |
| 646 | #endif |
| 647 | /* |
| 648 | ** This routine is called when the extension is loaded. |
| 649 | ** Register the new VFS. |
| 650 | */ |
| 651 | int sqlite3_appendvfs_init( |
| 652 | sqlite3 *db, |
| 653 | char **pzErrMsg, |
| 654 | const sqlite3_api_routines *pApi |
| 655 | ){ |
| 656 | int rc = SQLITE_OK; |
| 657 | sqlite3_vfs *pOrig; |
| 658 | SQLITE_EXTENSION_INIT2(pApi); |
| 659 | (void)pzErrMsg; |
| 660 | (void)db; |
| 661 | pOrig = sqlite3_vfs_find(0); |
| 662 | if( pOrig==0 ) return SQLITE_ERROR; |
| 663 | apnd_vfs.iVersion = pOrig->iVersion; |
| 664 | apnd_vfs.pAppData = pOrig; |
| 665 | apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile); |
| 666 | rc = sqlite3_vfs_register(&apnd_vfs, 0); |
| 667 | #ifdef APPENDVFS_TEST |
| 668 | if( rc==SQLITE_OK ){ |
| 669 | rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister); |
| 670 | } |
| 671 | #endif |
| 672 | if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY; |
| 673 | return rc; |
| 674 | } |