| | @@ -245,10 +245,20 @@ |
| 245 | 245 | #else |
| 246 | 246 | # define setBinaryMode(X,Y) |
| 247 | 247 | # define setTextMode(X,Y) |
| 248 | 248 | #endif |
| 249 | 249 | |
| 250 | +/* |
| 251 | +** When compiling with emcc (a.k.a. emscripten), we're building a |
| 252 | +** WebAssembly (WASM) bundle and need to disable and rewire a few |
| 253 | +** things. |
| 254 | +*/ |
| 255 | +#ifdef __EMSCRIPTEN__ |
| 256 | +#define SQLITE_SHELL_WASM_MODE |
| 257 | +#else |
| 258 | +#undef SQLITE_SHELL_WASM_MODE |
| 259 | +#endif |
| 250 | 260 | |
| 251 | 261 | /* True if the timer is enabled */ |
| 252 | 262 | static int enableTimer = 0; |
| 253 | 263 | |
| 254 | 264 | /* Return the current wall-clock time */ |
| | @@ -707,10 +717,11 @@ |
| 707 | 717 | ** |
| 708 | 718 | ** The result is stored in space obtained from malloc() and must either |
| 709 | 719 | ** be freed by the caller or else passed back into this routine via the |
| 710 | 720 | ** zPrior argument for reuse. |
| 711 | 721 | */ |
| 722 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 712 | 723 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
| 713 | 724 | char *zPrompt; |
| 714 | 725 | char *zResult; |
| 715 | 726 | if( in!=0 ){ |
| 716 | 727 | zResult = local_getline(zPrior, in); |
| | @@ -726,11 +737,11 @@ |
| 726 | 737 | if( zResult && *zResult ) shell_add_history(zResult); |
| 727 | 738 | #endif |
| 728 | 739 | } |
| 729 | 740 | return zResult; |
| 730 | 741 | } |
| 731 | | - |
| 742 | +#endif /* !SQLITE_SHELL_WASM_MODE */ |
| 732 | 743 | |
| 733 | 744 | /* |
| 734 | 745 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 735 | 746 | ** is not a hex digit. |
| 736 | 747 | */ |
| | @@ -814,11 +825,11 @@ |
| 814 | 825 | ** zIn, if it was not NULL, is freed. |
| 815 | 826 | ** |
| 816 | 827 | ** If the third argument, quote, is not '\0', then it is used as a |
| 817 | 828 | ** quote character for zAppend. |
| 818 | 829 | */ |
| 819 | | -static void appendText(ShellText *p, char const *zAppend, char quote){ |
| 830 | +static void appendText(ShellText *p, const char *zAppend, char quote){ |
| 820 | 831 | int len; |
| 821 | 832 | int i; |
| 822 | 833 | int nAppend = strlen30(zAppend); |
| 823 | 834 | |
| 824 | 835 | len = nAppend+p->n+1; |
| | @@ -1379,10 +1390,121 @@ |
| 1379 | 1390 | #endif /* defined(WIN32) && defined(_MSC_VER) */ |
| 1380 | 1391 | |
| 1381 | 1392 | /************************* End test_windirent.c ********************/ |
| 1382 | 1393 | #define dirent DIRENT |
| 1383 | 1394 | #endif |
| 1395 | +/************************* Begin ../ext/misc/memtrace.c ******************/ |
| 1396 | +/* |
| 1397 | +** 2019-01-21 |
| 1398 | +** |
| 1399 | +** The author disclaims copyright to this source code. In place of |
| 1400 | +** a legal notice, here is a blessing: |
| 1401 | +** |
| 1402 | +** May you do good and not evil. |
| 1403 | +** May you find forgiveness for yourself and forgive others. |
| 1404 | +** May you share freely, never taking more than you give. |
| 1405 | +** |
| 1406 | +************************************************************************* |
| 1407 | +** |
| 1408 | +** This file implements an extension that uses the SQLITE_CONFIG_MALLOC |
| 1409 | +** mechanism to add a tracing layer on top of SQLite. If this extension |
| 1410 | +** is registered prior to sqlite3_initialize(), it will cause all memory |
| 1411 | +** allocation activities to be logged on standard output, or to some other |
| 1412 | +** FILE specified by the initializer. |
| 1413 | +** |
| 1414 | +** This file needs to be compiled into the application that uses it. |
| 1415 | +** |
| 1416 | +** This extension is used to implement the --memtrace option of the |
| 1417 | +** command-line shell. |
| 1418 | +*/ |
| 1419 | +#include <assert.h> |
| 1420 | +#include <string.h> |
| 1421 | +#include <stdio.h> |
| 1422 | + |
| 1423 | +/* The original memory allocation routines */ |
| 1424 | +static sqlite3_mem_methods memtraceBase; |
| 1425 | +static FILE *memtraceOut; |
| 1426 | + |
| 1427 | +/* Methods that trace memory allocations */ |
| 1428 | +static void *memtraceMalloc(int n){ |
| 1429 | + if( memtraceOut ){ |
| 1430 | + fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", |
| 1431 | + memtraceBase.xRoundup(n)); |
| 1432 | + } |
| 1433 | + return memtraceBase.xMalloc(n); |
| 1434 | +} |
| 1435 | +static void memtraceFree(void *p){ |
| 1436 | + if( p==0 ) return; |
| 1437 | + if( memtraceOut ){ |
| 1438 | + fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p)); |
| 1439 | + } |
| 1440 | + memtraceBase.xFree(p); |
| 1441 | +} |
| 1442 | +static void *memtraceRealloc(void *p, int n){ |
| 1443 | + if( p==0 ) return memtraceMalloc(n); |
| 1444 | + if( n==0 ){ |
| 1445 | + memtraceFree(p); |
| 1446 | + return 0; |
| 1447 | + } |
| 1448 | + if( memtraceOut ){ |
| 1449 | + fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", |
| 1450 | + memtraceBase.xSize(p), memtraceBase.xRoundup(n)); |
| 1451 | + } |
| 1452 | + return memtraceBase.xRealloc(p, n); |
| 1453 | +} |
| 1454 | +static int memtraceSize(void *p){ |
| 1455 | + return memtraceBase.xSize(p); |
| 1456 | +} |
| 1457 | +static int memtraceRoundup(int n){ |
| 1458 | + return memtraceBase.xRoundup(n); |
| 1459 | +} |
| 1460 | +static int memtraceInit(void *p){ |
| 1461 | + return memtraceBase.xInit(p); |
| 1462 | +} |
| 1463 | +static void memtraceShutdown(void *p){ |
| 1464 | + memtraceBase.xShutdown(p); |
| 1465 | +} |
| 1466 | + |
| 1467 | +/* The substitute memory allocator */ |
| 1468 | +static sqlite3_mem_methods ersaztMethods = { |
| 1469 | + memtraceMalloc, |
| 1470 | + memtraceFree, |
| 1471 | + memtraceRealloc, |
| 1472 | + memtraceSize, |
| 1473 | + memtraceRoundup, |
| 1474 | + memtraceInit, |
| 1475 | + memtraceShutdown, |
| 1476 | + 0 |
| 1477 | +}; |
| 1478 | + |
| 1479 | +/* Begin tracing memory allocations to out. */ |
| 1480 | +int sqlite3MemTraceActivate(FILE *out){ |
| 1481 | + int rc = SQLITE_OK; |
| 1482 | + if( memtraceBase.xMalloc==0 ){ |
| 1483 | + rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase); |
| 1484 | + if( rc==SQLITE_OK ){ |
| 1485 | + rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods); |
| 1486 | + } |
| 1487 | + } |
| 1488 | + memtraceOut = out; |
| 1489 | + return rc; |
| 1490 | +} |
| 1491 | + |
| 1492 | +/* Deactivate memory tracing */ |
| 1493 | +int sqlite3MemTraceDeactivate(void){ |
| 1494 | + int rc = SQLITE_OK; |
| 1495 | + if( memtraceBase.xMalloc!=0 ){ |
| 1496 | + rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase); |
| 1497 | + if( rc==SQLITE_OK ){ |
| 1498 | + memset(&memtraceBase, 0, sizeof(memtraceBase)); |
| 1499 | + } |
| 1500 | + } |
| 1501 | + memtraceOut = 0; |
| 1502 | + return rc; |
| 1503 | +} |
| 1504 | + |
| 1505 | +/************************* End ../ext/misc/memtrace.c ********************/ |
| 1384 | 1506 | /************************* Begin ../ext/misc/shathree.c ******************/ |
| 1385 | 1507 | /* |
| 1386 | 1508 | ** 2017-03-08 |
| 1387 | 1509 | ** |
| 1388 | 1510 | ** The author disclaims copyright to this source code. In place of |
| | @@ -2106,2333 +2228,10 @@ |
| 2106 | 2228 | } |
| 2107 | 2229 | return rc; |
| 2108 | 2230 | } |
| 2109 | 2231 | |
| 2110 | 2232 | /************************* End ../ext/misc/shathree.c ********************/ |
| 2111 | | -/************************* Begin ../ext/misc/fileio.c ******************/ |
| 2112 | | -/* |
| 2113 | | -** 2014-06-13 |
| 2114 | | -** |
| 2115 | | -** The author disclaims copyright to this source code. In place of |
| 2116 | | -** a legal notice, here is a blessing: |
| 2117 | | -** |
| 2118 | | -** May you do good and not evil. |
| 2119 | | -** May you find forgiveness for yourself and forgive others. |
| 2120 | | -** May you share freely, never taking more than you give. |
| 2121 | | -** |
| 2122 | | -****************************************************************************** |
| 2123 | | -** |
| 2124 | | -** This SQLite extension implements SQL functions readfile() and |
| 2125 | | -** writefile(), and eponymous virtual type "fsdir". |
| 2126 | | -** |
| 2127 | | -** WRITEFILE(FILE, DATA [, MODE [, MTIME]]): |
| 2128 | | -** |
| 2129 | | -** If neither of the optional arguments is present, then this UDF |
| 2130 | | -** function writes blob DATA to file FILE. If successful, the number |
| 2131 | | -** of bytes written is returned. If an error occurs, NULL is returned. |
| 2132 | | -** |
| 2133 | | -** If the first option argument - MODE - is present, then it must |
| 2134 | | -** be passed an integer value that corresponds to a POSIX mode |
| 2135 | | -** value (file type + permissions, as returned in the stat.st_mode |
| 2136 | | -** field by the stat() system call). Three types of files may |
| 2137 | | -** be written/created: |
| 2138 | | -** |
| 2139 | | -** regular files: (mode & 0170000)==0100000 |
| 2140 | | -** symbolic links: (mode & 0170000)==0120000 |
| 2141 | | -** directories: (mode & 0170000)==0040000 |
| 2142 | | -** |
| 2143 | | -** For a directory, the DATA is ignored. For a symbolic link, it is |
| 2144 | | -** interpreted as text and used as the target of the link. For a |
| 2145 | | -** regular file, it is interpreted as a blob and written into the |
| 2146 | | -** named file. Regardless of the type of file, its permissions are |
| 2147 | | -** set to (mode & 0777) before returning. |
| 2148 | | -** |
| 2149 | | -** If the optional MTIME argument is present, then it is interpreted |
| 2150 | | -** as an integer - the number of seconds since the unix epoch. The |
| 2151 | | -** modification-time of the target file is set to this value before |
| 2152 | | -** returning. |
| 2153 | | -** |
| 2154 | | -** If three or more arguments are passed to this function and an |
| 2155 | | -** error is encountered, an exception is raised. |
| 2156 | | -** |
| 2157 | | -** READFILE(FILE): |
| 2158 | | -** |
| 2159 | | -** Read and return the contents of file FILE (type blob) from disk. |
| 2160 | | -** |
| 2161 | | -** FSDIR: |
| 2162 | | -** |
| 2163 | | -** Used as follows: |
| 2164 | | -** |
| 2165 | | -** SELECT * FROM fsdir($path [, $dir]); |
| 2166 | | -** |
| 2167 | | -** Parameter $path is an absolute or relative pathname. If the file that it |
| 2168 | | -** refers to does not exist, it is an error. If the path refers to a regular |
| 2169 | | -** file or symbolic link, it returns a single row. Or, if the path refers |
| 2170 | | -** to a directory, it returns one row for the directory, and one row for each |
| 2171 | | -** file within the hierarchy rooted at $path. |
| 2172 | | -** |
| 2173 | | -** Each row has the following columns: |
| 2174 | | -** |
| 2175 | | -** name: Path to file or directory (text value). |
| 2176 | | -** mode: Value of stat.st_mode for directory entry (an integer). |
| 2177 | | -** mtime: Value of stat.st_mtime for directory entry (an integer). |
| 2178 | | -** data: For a regular file, a blob containing the file data. For a |
| 2179 | | -** symlink, a text value containing the text of the link. For a |
| 2180 | | -** directory, NULL. |
| 2181 | | -** |
| 2182 | | -** If a non-NULL value is specified for the optional $dir parameter and |
| 2183 | | -** $path is a relative path, then $path is interpreted relative to $dir. |
| 2184 | | -** And the paths returned in the "name" column of the table are also |
| 2185 | | -** relative to directory $dir. |
| 2186 | | -** |
| 2187 | | -** Notes on building this extension for Windows: |
| 2188 | | -** Unless linked statically with the SQLite library, a preprocessor |
| 2189 | | -** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone |
| 2190 | | -** DLL form of this extension for WIN32. See its use below for details. |
| 2191 | | -*/ |
| 2192 | | -/* #include "sqlite3ext.h" */ |
| 2193 | | -SQLITE_EXTENSION_INIT1 |
| 2194 | | -#include <stdio.h> |
| 2195 | | -#include <string.h> |
| 2196 | | -#include <assert.h> |
| 2197 | | - |
| 2198 | | -#include <sys/types.h> |
| 2199 | | -#include <sys/stat.h> |
| 2200 | | -#include <fcntl.h> |
| 2201 | | -#if !defined(_WIN32) && !defined(WIN32) |
| 2202 | | -# include <unistd.h> |
| 2203 | | -# include <dirent.h> |
| 2204 | | -# include <utime.h> |
| 2205 | | -# include <sys/time.h> |
| 2206 | | -#else |
| 2207 | | -# include "windows.h" |
| 2208 | | -# include <io.h> |
| 2209 | | -# include <direct.h> |
| 2210 | | -/* # include "test_windirent.h" */ |
| 2211 | | -# define dirent DIRENT |
| 2212 | | -# ifndef chmod |
| 2213 | | -# define chmod _chmod |
| 2214 | | -# endif |
| 2215 | | -# ifndef stat |
| 2216 | | -# define stat _stat |
| 2217 | | -# endif |
| 2218 | | -# define mkdir(path,mode) _mkdir(path) |
| 2219 | | -# define lstat(path,buf) stat(path,buf) |
| 2220 | | -#endif |
| 2221 | | -#include <time.h> |
| 2222 | | -#include <errno.h> |
| 2223 | | - |
| 2224 | | - |
| 2225 | | -/* |
| 2226 | | -** Structure of the fsdir() table-valued function |
| 2227 | | -*/ |
| 2228 | | - /* 0 1 2 3 4 5 */ |
| 2229 | | -#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)" |
| 2230 | | -#define FSDIR_COLUMN_NAME 0 /* Name of the file */ |
| 2231 | | -#define FSDIR_COLUMN_MODE 1 /* Access mode */ |
| 2232 | | -#define FSDIR_COLUMN_MTIME 2 /* Last modification time */ |
| 2233 | | -#define FSDIR_COLUMN_DATA 3 /* File content */ |
| 2234 | | -#define FSDIR_COLUMN_PATH 4 /* Path to top of search */ |
| 2235 | | -#define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */ |
| 2236 | | - |
| 2237 | | - |
| 2238 | | -/* |
| 2239 | | -** Set the result stored by context ctx to a blob containing the |
| 2240 | | -** contents of file zName. Or, leave the result unchanged (NULL) |
| 2241 | | -** if the file does not exist or is unreadable. |
| 2242 | | -** |
| 2243 | | -** If the file exceeds the SQLite blob size limit, through an |
| 2244 | | -** SQLITE_TOOBIG error. |
| 2245 | | -** |
| 2246 | | -** Throw an SQLITE_IOERR if there are difficulties pulling the file |
| 2247 | | -** off of disk. |
| 2248 | | -*/ |
| 2249 | | -static void readFileContents(sqlite3_context *ctx, const char *zName){ |
| 2250 | | - FILE *in; |
| 2251 | | - sqlite3_int64 nIn; |
| 2252 | | - void *pBuf; |
| 2253 | | - sqlite3 *db; |
| 2254 | | - int mxBlob; |
| 2255 | | - |
| 2256 | | - in = fopen(zName, "rb"); |
| 2257 | | - if( in==0 ){ |
| 2258 | | - /* File does not exist or is unreadable. Leave the result set to NULL. */ |
| 2259 | | - return; |
| 2260 | | - } |
| 2261 | | - fseek(in, 0, SEEK_END); |
| 2262 | | - nIn = ftell(in); |
| 2263 | | - rewind(in); |
| 2264 | | - db = sqlite3_context_db_handle(ctx); |
| 2265 | | - mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); |
| 2266 | | - if( nIn>mxBlob ){ |
| 2267 | | - sqlite3_result_error_code(ctx, SQLITE_TOOBIG); |
| 2268 | | - fclose(in); |
| 2269 | | - return; |
| 2270 | | - } |
| 2271 | | - pBuf = sqlite3_malloc64( nIn ? nIn : 1 ); |
| 2272 | | - if( pBuf==0 ){ |
| 2273 | | - sqlite3_result_error_nomem(ctx); |
| 2274 | | - fclose(in); |
| 2275 | | - return; |
| 2276 | | - } |
| 2277 | | - if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){ |
| 2278 | | - sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free); |
| 2279 | | - }else{ |
| 2280 | | - sqlite3_result_error_code(ctx, SQLITE_IOERR); |
| 2281 | | - sqlite3_free(pBuf); |
| 2282 | | - } |
| 2283 | | - fclose(in); |
| 2284 | | -} |
| 2285 | | - |
| 2286 | | -/* |
| 2287 | | -** Implementation of the "readfile(X)" SQL function. The entire content |
| 2288 | | -** of the file named X is read and returned as a BLOB. NULL is returned |
| 2289 | | -** if the file does not exist or is unreadable. |
| 2290 | | -*/ |
| 2291 | | -static void readfileFunc( |
| 2292 | | - sqlite3_context *context, |
| 2293 | | - int argc, |
| 2294 | | - sqlite3_value **argv |
| 2295 | | -){ |
| 2296 | | - const char *zName; |
| 2297 | | - (void)(argc); /* Unused parameter */ |
| 2298 | | - zName = (const char*)sqlite3_value_text(argv[0]); |
| 2299 | | - if( zName==0 ) return; |
| 2300 | | - readFileContents(context, zName); |
| 2301 | | -} |
| 2302 | | - |
| 2303 | | -/* |
| 2304 | | -** Set the error message contained in context ctx to the results of |
| 2305 | | -** vprintf(zFmt, ...). |
| 2306 | | -*/ |
| 2307 | | -static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){ |
| 2308 | | - char *zMsg = 0; |
| 2309 | | - va_list ap; |
| 2310 | | - va_start(ap, zFmt); |
| 2311 | | - zMsg = sqlite3_vmprintf(zFmt, ap); |
| 2312 | | - sqlite3_result_error(ctx, zMsg, -1); |
| 2313 | | - sqlite3_free(zMsg); |
| 2314 | | - va_end(ap); |
| 2315 | | -} |
| 2316 | | - |
| 2317 | | -#if defined(_WIN32) |
| 2318 | | -/* |
| 2319 | | -** This function is designed to convert a Win32 FILETIME structure into the |
| 2320 | | -** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC). |
| 2321 | | -*/ |
| 2322 | | -static sqlite3_uint64 fileTimeToUnixTime( |
| 2323 | | - LPFILETIME pFileTime |
| 2324 | | -){ |
| 2325 | | - SYSTEMTIME epochSystemTime; |
| 2326 | | - ULARGE_INTEGER epochIntervals; |
| 2327 | | - FILETIME epochFileTime; |
| 2328 | | - ULARGE_INTEGER fileIntervals; |
| 2329 | | - |
| 2330 | | - memset(&epochSystemTime, 0, sizeof(SYSTEMTIME)); |
| 2331 | | - epochSystemTime.wYear = 1970; |
| 2332 | | - epochSystemTime.wMonth = 1; |
| 2333 | | - epochSystemTime.wDay = 1; |
| 2334 | | - SystemTimeToFileTime(&epochSystemTime, &epochFileTime); |
| 2335 | | - epochIntervals.LowPart = epochFileTime.dwLowDateTime; |
| 2336 | | - epochIntervals.HighPart = epochFileTime.dwHighDateTime; |
| 2337 | | - |
| 2338 | | - fileIntervals.LowPart = pFileTime->dwLowDateTime; |
| 2339 | | - fileIntervals.HighPart = pFileTime->dwHighDateTime; |
| 2340 | | - |
| 2341 | | - return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000; |
| 2342 | | -} |
| 2343 | | - |
| 2344 | | - |
| 2345 | | -#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32)) |
| 2346 | | -# /* To allow a standalone DLL, use this next replacement function: */ |
| 2347 | | -# undef sqlite3_win32_utf8_to_unicode |
| 2348 | | -# define sqlite3_win32_utf8_to_unicode utf8_to_utf16 |
| 2349 | | -# |
| 2350 | | -LPWSTR utf8_to_utf16(const char *z){ |
| 2351 | | - int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0); |
| 2352 | | - LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR)); |
| 2353 | | - if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) ) |
| 2354 | | - return rv; |
| 2355 | | - sqlite3_free(rv); |
| 2356 | | - return 0; |
| 2357 | | -} |
| 2358 | | -#endif |
| 2359 | | - |
| 2360 | | -/* |
| 2361 | | -** This function attempts to normalize the time values found in the stat() |
| 2362 | | -** buffer to UTC. This is necessary on Win32, where the runtime library |
| 2363 | | -** appears to return these values as local times. |
| 2364 | | -*/ |
| 2365 | | -static void statTimesToUtc( |
| 2366 | | - const char *zPath, |
| 2367 | | - struct stat *pStatBuf |
| 2368 | | -){ |
| 2369 | | - HANDLE hFindFile; |
| 2370 | | - WIN32_FIND_DATAW fd; |
| 2371 | | - LPWSTR zUnicodeName; |
| 2372 | | - extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); |
| 2373 | | - zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath); |
| 2374 | | - if( zUnicodeName ){ |
| 2375 | | - memset(&fd, 0, sizeof(WIN32_FIND_DATAW)); |
| 2376 | | - hFindFile = FindFirstFileW(zUnicodeName, &fd); |
| 2377 | | - if( hFindFile!=NULL ){ |
| 2378 | | - pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime); |
| 2379 | | - pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime); |
| 2380 | | - pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime); |
| 2381 | | - FindClose(hFindFile); |
| 2382 | | - } |
| 2383 | | - sqlite3_free(zUnicodeName); |
| 2384 | | - } |
| 2385 | | -} |
| 2386 | | -#endif |
| 2387 | | - |
| 2388 | | -/* |
| 2389 | | -** This function is used in place of stat(). On Windows, special handling |
| 2390 | | -** is required in order for the included time to be returned as UTC. On all |
| 2391 | | -** other systems, this function simply calls stat(). |
| 2392 | | -*/ |
| 2393 | | -static int fileStat( |
| 2394 | | - const char *zPath, |
| 2395 | | - struct stat *pStatBuf |
| 2396 | | -){ |
| 2397 | | -#if defined(_WIN32) |
| 2398 | | - int rc = stat(zPath, pStatBuf); |
| 2399 | | - if( rc==0 ) statTimesToUtc(zPath, pStatBuf); |
| 2400 | | - return rc; |
| 2401 | | -#else |
| 2402 | | - return stat(zPath, pStatBuf); |
| 2403 | | -#endif |
| 2404 | | -} |
| 2405 | | - |
| 2406 | | -/* |
| 2407 | | -** This function is used in place of lstat(). On Windows, special handling |
| 2408 | | -** is required in order for the included time to be returned as UTC. On all |
| 2409 | | -** other systems, this function simply calls lstat(). |
| 2410 | | -*/ |
| 2411 | | -static int fileLinkStat( |
| 2412 | | - const char *zPath, |
| 2413 | | - struct stat *pStatBuf |
| 2414 | | -){ |
| 2415 | | -#if defined(_WIN32) |
| 2416 | | - int rc = lstat(zPath, pStatBuf); |
| 2417 | | - if( rc==0 ) statTimesToUtc(zPath, pStatBuf); |
| 2418 | | - return rc; |
| 2419 | | -#else |
| 2420 | | - return lstat(zPath, pStatBuf); |
| 2421 | | -#endif |
| 2422 | | -} |
| 2423 | | - |
| 2424 | | -/* |
| 2425 | | -** Argument zFile is the name of a file that will be created and/or written |
| 2426 | | -** by SQL function writefile(). This function ensures that the directory |
| 2427 | | -** zFile will be written to exists, creating it if required. The permissions |
| 2428 | | -** for any path components created by this function are set in accordance |
| 2429 | | -** with the current umask. |
| 2430 | | -** |
| 2431 | | -** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise, |
| 2432 | | -** SQLITE_OK is returned if the directory is successfully created, or |
| 2433 | | -** SQLITE_ERROR otherwise. |
| 2434 | | -*/ |
| 2435 | | -static int makeDirectory( |
| 2436 | | - const char *zFile |
| 2437 | | -){ |
| 2438 | | - char *zCopy = sqlite3_mprintf("%s", zFile); |
| 2439 | | - int rc = SQLITE_OK; |
| 2440 | | - |
| 2441 | | - if( zCopy==0 ){ |
| 2442 | | - rc = SQLITE_NOMEM; |
| 2443 | | - }else{ |
| 2444 | | - int nCopy = (int)strlen(zCopy); |
| 2445 | | - int i = 1; |
| 2446 | | - |
| 2447 | | - while( rc==SQLITE_OK ){ |
| 2448 | | - struct stat sStat; |
| 2449 | | - int rc2; |
| 2450 | | - |
| 2451 | | - for(; zCopy[i]!='/' && i<nCopy; i++); |
| 2452 | | - if( i==nCopy ) break; |
| 2453 | | - zCopy[i] = '\0'; |
| 2454 | | - |
| 2455 | | - rc2 = fileStat(zCopy, &sStat); |
| 2456 | | - if( rc2!=0 ){ |
| 2457 | | - if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR; |
| 2458 | | - }else{ |
| 2459 | | - if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR; |
| 2460 | | - } |
| 2461 | | - zCopy[i] = '/'; |
| 2462 | | - i++; |
| 2463 | | - } |
| 2464 | | - |
| 2465 | | - sqlite3_free(zCopy); |
| 2466 | | - } |
| 2467 | | - |
| 2468 | | - return rc; |
| 2469 | | -} |
| 2470 | | - |
| 2471 | | -/* |
| 2472 | | -** This function does the work for the writefile() UDF. Refer to |
| 2473 | | -** header comments at the top of this file for details. |
| 2474 | | -*/ |
| 2475 | | -static int writeFile( |
| 2476 | | - sqlite3_context *pCtx, /* Context to return bytes written in */ |
| 2477 | | - const char *zFile, /* File to write */ |
| 2478 | | - sqlite3_value *pData, /* Data to write */ |
| 2479 | | - mode_t mode, /* MODE parameter passed to writefile() */ |
| 2480 | | - sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */ |
| 2481 | | -){ |
| 2482 | | - if( zFile==0 ) return 1; |
| 2483 | | -#if !defined(_WIN32) && !defined(WIN32) |
| 2484 | | - if( S_ISLNK(mode) ){ |
| 2485 | | - const char *zTo = (const char*)sqlite3_value_text(pData); |
| 2486 | | - if( zTo==0 || symlink(zTo, zFile)<0 ) return 1; |
| 2487 | | - }else |
| 2488 | | -#endif |
| 2489 | | - { |
| 2490 | | - if( S_ISDIR(mode) ){ |
| 2491 | | - if( mkdir(zFile, mode) ){ |
| 2492 | | - /* The mkdir() call to create the directory failed. This might not |
| 2493 | | - ** be an error though - if there is already a directory at the same |
| 2494 | | - ** path and either the permissions already match or can be changed |
| 2495 | | - ** to do so using chmod(), it is not an error. */ |
| 2496 | | - struct stat sStat; |
| 2497 | | - if( errno!=EEXIST |
| 2498 | | - || 0!=fileStat(zFile, &sStat) |
| 2499 | | - || !S_ISDIR(sStat.st_mode) |
| 2500 | | - || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777)) |
| 2501 | | - ){ |
| 2502 | | - return 1; |
| 2503 | | - } |
| 2504 | | - } |
| 2505 | | - }else{ |
| 2506 | | - sqlite3_int64 nWrite = 0; |
| 2507 | | - const char *z; |
| 2508 | | - int rc = 0; |
| 2509 | | - FILE *out = fopen(zFile, "wb"); |
| 2510 | | - if( out==0 ) return 1; |
| 2511 | | - z = (const char*)sqlite3_value_blob(pData); |
| 2512 | | - if( z ){ |
| 2513 | | - sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out); |
| 2514 | | - nWrite = sqlite3_value_bytes(pData); |
| 2515 | | - if( nWrite!=n ){ |
| 2516 | | - rc = 1; |
| 2517 | | - } |
| 2518 | | - } |
| 2519 | | - fclose(out); |
| 2520 | | - if( rc==0 && mode && chmod(zFile, mode & 0777) ){ |
| 2521 | | - rc = 1; |
| 2522 | | - } |
| 2523 | | - if( rc ) return 2; |
| 2524 | | - sqlite3_result_int64(pCtx, nWrite); |
| 2525 | | - } |
| 2526 | | - } |
| 2527 | | - |
| 2528 | | - if( mtime>=0 ){ |
| 2529 | | -#if defined(_WIN32) |
| 2530 | | -#if !SQLITE_OS_WINRT |
| 2531 | | - /* Windows */ |
| 2532 | | - FILETIME lastAccess; |
| 2533 | | - FILETIME lastWrite; |
| 2534 | | - SYSTEMTIME currentTime; |
| 2535 | | - LONGLONG intervals; |
| 2536 | | - HANDLE hFile; |
| 2537 | | - LPWSTR zUnicodeName; |
| 2538 | | - extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); |
| 2539 | | - |
| 2540 | | - GetSystemTime(¤tTime); |
| 2541 | | - SystemTimeToFileTime(¤tTime, &lastAccess); |
| 2542 | | - intervals = Int32x32To64(mtime, 10000000) + 116444736000000000; |
| 2543 | | - lastWrite.dwLowDateTime = (DWORD)intervals; |
| 2544 | | - lastWrite.dwHighDateTime = intervals >> 32; |
| 2545 | | - zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile); |
| 2546 | | - if( zUnicodeName==0 ){ |
| 2547 | | - return 1; |
| 2548 | | - } |
| 2549 | | - hFile = CreateFileW( |
| 2550 | | - zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, |
| 2551 | | - FILE_FLAG_BACKUP_SEMANTICS, NULL |
| 2552 | | - ); |
| 2553 | | - sqlite3_free(zUnicodeName); |
| 2554 | | - if( hFile!=INVALID_HANDLE_VALUE ){ |
| 2555 | | - BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite); |
| 2556 | | - CloseHandle(hFile); |
| 2557 | | - return !bResult; |
| 2558 | | - }else{ |
| 2559 | | - return 1; |
| 2560 | | - } |
| 2561 | | -#endif |
| 2562 | | -#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */ |
| 2563 | | - /* Recent unix */ |
| 2564 | | - struct timespec times[2]; |
| 2565 | | - times[0].tv_nsec = times[1].tv_nsec = 0; |
| 2566 | | - times[0].tv_sec = time(0); |
| 2567 | | - times[1].tv_sec = mtime; |
| 2568 | | - if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){ |
| 2569 | | - return 1; |
| 2570 | | - } |
| 2571 | | -#else |
| 2572 | | - /* Legacy unix */ |
| 2573 | | - struct timeval times[2]; |
| 2574 | | - times[0].tv_usec = times[1].tv_usec = 0; |
| 2575 | | - times[0].tv_sec = time(0); |
| 2576 | | - times[1].tv_sec = mtime; |
| 2577 | | - if( utimes(zFile, times) ){ |
| 2578 | | - return 1; |
| 2579 | | - } |
| 2580 | | -#endif |
| 2581 | | - } |
| 2582 | | - |
| 2583 | | - return 0; |
| 2584 | | -} |
| 2585 | | - |
| 2586 | | -/* |
| 2587 | | -** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function. |
| 2588 | | -** Refer to header comments at the top of this file for details. |
| 2589 | | -*/ |
| 2590 | | -static void writefileFunc( |
| 2591 | | - sqlite3_context *context, |
| 2592 | | - int argc, |
| 2593 | | - sqlite3_value **argv |
| 2594 | | -){ |
| 2595 | | - const char *zFile; |
| 2596 | | - mode_t mode = 0; |
| 2597 | | - int res; |
| 2598 | | - sqlite3_int64 mtime = -1; |
| 2599 | | - |
| 2600 | | - if( argc<2 || argc>4 ){ |
| 2601 | | - sqlite3_result_error(context, |
| 2602 | | - "wrong number of arguments to function writefile()", -1 |
| 2603 | | - ); |
| 2604 | | - return; |
| 2605 | | - } |
| 2606 | | - |
| 2607 | | - zFile = (const char*)sqlite3_value_text(argv[0]); |
| 2608 | | - if( zFile==0 ) return; |
| 2609 | | - if( argc>=3 ){ |
| 2610 | | - mode = (mode_t)sqlite3_value_int(argv[2]); |
| 2611 | | - } |
| 2612 | | - if( argc==4 ){ |
| 2613 | | - mtime = sqlite3_value_int64(argv[3]); |
| 2614 | | - } |
| 2615 | | - |
| 2616 | | - res = writeFile(context, zFile, argv[1], mode, mtime); |
| 2617 | | - if( res==1 && errno==ENOENT ){ |
| 2618 | | - if( makeDirectory(zFile)==SQLITE_OK ){ |
| 2619 | | - res = writeFile(context, zFile, argv[1], mode, mtime); |
| 2620 | | - } |
| 2621 | | - } |
| 2622 | | - |
| 2623 | | - if( argc>2 && res!=0 ){ |
| 2624 | | - if( S_ISLNK(mode) ){ |
| 2625 | | - ctxErrorMsg(context, "failed to create symlink: %s", zFile); |
| 2626 | | - }else if( S_ISDIR(mode) ){ |
| 2627 | | - ctxErrorMsg(context, "failed to create directory: %s", zFile); |
| 2628 | | - }else{ |
| 2629 | | - ctxErrorMsg(context, "failed to write file: %s", zFile); |
| 2630 | | - } |
| 2631 | | - } |
| 2632 | | -} |
| 2633 | | - |
| 2634 | | -/* |
| 2635 | | -** SQL function: lsmode(MODE) |
| 2636 | | -** |
| 2637 | | -** Given a numberic st_mode from stat(), convert it into a human-readable |
| 2638 | | -** text string in the style of "ls -l". |
| 2639 | | -*/ |
| 2640 | | -static void lsModeFunc( |
| 2641 | | - sqlite3_context *context, |
| 2642 | | - int argc, |
| 2643 | | - sqlite3_value **argv |
| 2644 | | -){ |
| 2645 | | - int i; |
| 2646 | | - int iMode = sqlite3_value_int(argv[0]); |
| 2647 | | - char z[16]; |
| 2648 | | - (void)argc; |
| 2649 | | - if( S_ISLNK(iMode) ){ |
| 2650 | | - z[0] = 'l'; |
| 2651 | | - }else if( S_ISREG(iMode) ){ |
| 2652 | | - z[0] = '-'; |
| 2653 | | - }else if( S_ISDIR(iMode) ){ |
| 2654 | | - z[0] = 'd'; |
| 2655 | | - }else{ |
| 2656 | | - z[0] = '?'; |
| 2657 | | - } |
| 2658 | | - for(i=0; i<3; i++){ |
| 2659 | | - int m = (iMode >> ((2-i)*3)); |
| 2660 | | - char *a = &z[1 + i*3]; |
| 2661 | | - a[0] = (m & 0x4) ? 'r' : '-'; |
| 2662 | | - a[1] = (m & 0x2) ? 'w' : '-'; |
| 2663 | | - a[2] = (m & 0x1) ? 'x' : '-'; |
| 2664 | | - } |
| 2665 | | - z[10] = '\0'; |
| 2666 | | - sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); |
| 2667 | | -} |
| 2668 | | - |
| 2669 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2670 | | - |
| 2671 | | -/* |
| 2672 | | -** Cursor type for recursively iterating through a directory structure. |
| 2673 | | -*/ |
| 2674 | | -typedef struct fsdir_cursor fsdir_cursor; |
| 2675 | | -typedef struct FsdirLevel FsdirLevel; |
| 2676 | | - |
| 2677 | | -struct FsdirLevel { |
| 2678 | | - DIR *pDir; /* From opendir() */ |
| 2679 | | - char *zDir; /* Name of directory (nul-terminated) */ |
| 2680 | | -}; |
| 2681 | | - |
| 2682 | | -struct fsdir_cursor { |
| 2683 | | - sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 2684 | | - |
| 2685 | | - int nLvl; /* Number of entries in aLvl[] array */ |
| 2686 | | - int iLvl; /* Index of current entry */ |
| 2687 | | - FsdirLevel *aLvl; /* Hierarchy of directories being traversed */ |
| 2688 | | - |
| 2689 | | - const char *zBase; |
| 2690 | | - int nBase; |
| 2691 | | - |
| 2692 | | - struct stat sStat; /* Current lstat() results */ |
| 2693 | | - char *zPath; /* Path to current entry */ |
| 2694 | | - sqlite3_int64 iRowid; /* Current rowid */ |
| 2695 | | -}; |
| 2696 | | - |
| 2697 | | -typedef struct fsdir_tab fsdir_tab; |
| 2698 | | -struct fsdir_tab { |
| 2699 | | - sqlite3_vtab base; /* Base class - must be first */ |
| 2700 | | -}; |
| 2701 | | - |
| 2702 | | -/* |
| 2703 | | -** Construct a new fsdir virtual table object. |
| 2704 | | -*/ |
| 2705 | | -static int fsdirConnect( |
| 2706 | | - sqlite3 *db, |
| 2707 | | - void *pAux, |
| 2708 | | - int argc, const char *const*argv, |
| 2709 | | - sqlite3_vtab **ppVtab, |
| 2710 | | - char **pzErr |
| 2711 | | -){ |
| 2712 | | - fsdir_tab *pNew = 0; |
| 2713 | | - int rc; |
| 2714 | | - (void)pAux; |
| 2715 | | - (void)argc; |
| 2716 | | - (void)argv; |
| 2717 | | - (void)pzErr; |
| 2718 | | - rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA); |
| 2719 | | - if( rc==SQLITE_OK ){ |
| 2720 | | - pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) ); |
| 2721 | | - if( pNew==0 ) return SQLITE_NOMEM; |
| 2722 | | - memset(pNew, 0, sizeof(*pNew)); |
| 2723 | | - sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); |
| 2724 | | - } |
| 2725 | | - *ppVtab = (sqlite3_vtab*)pNew; |
| 2726 | | - return rc; |
| 2727 | | -} |
| 2728 | | - |
| 2729 | | -/* |
| 2730 | | -** This method is the destructor for fsdir vtab objects. |
| 2731 | | -*/ |
| 2732 | | -static int fsdirDisconnect(sqlite3_vtab *pVtab){ |
| 2733 | | - sqlite3_free(pVtab); |
| 2734 | | - return SQLITE_OK; |
| 2735 | | -} |
| 2736 | | - |
| 2737 | | -/* |
| 2738 | | -** Constructor for a new fsdir_cursor object. |
| 2739 | | -*/ |
| 2740 | | -static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 2741 | | - fsdir_cursor *pCur; |
| 2742 | | - (void)p; |
| 2743 | | - pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 2744 | | - if( pCur==0 ) return SQLITE_NOMEM; |
| 2745 | | - memset(pCur, 0, sizeof(*pCur)); |
| 2746 | | - pCur->iLvl = -1; |
| 2747 | | - *ppCursor = &pCur->base; |
| 2748 | | - return SQLITE_OK; |
| 2749 | | -} |
| 2750 | | - |
| 2751 | | -/* |
| 2752 | | -** Reset a cursor back to the state it was in when first returned |
| 2753 | | -** by fsdirOpen(). |
| 2754 | | -*/ |
| 2755 | | -static void fsdirResetCursor(fsdir_cursor *pCur){ |
| 2756 | | - int i; |
| 2757 | | - for(i=0; i<=pCur->iLvl; i++){ |
| 2758 | | - FsdirLevel *pLvl = &pCur->aLvl[i]; |
| 2759 | | - if( pLvl->pDir ) closedir(pLvl->pDir); |
| 2760 | | - sqlite3_free(pLvl->zDir); |
| 2761 | | - } |
| 2762 | | - sqlite3_free(pCur->zPath); |
| 2763 | | - sqlite3_free(pCur->aLvl); |
| 2764 | | - pCur->aLvl = 0; |
| 2765 | | - pCur->zPath = 0; |
| 2766 | | - pCur->zBase = 0; |
| 2767 | | - pCur->nBase = 0; |
| 2768 | | - pCur->nLvl = 0; |
| 2769 | | - pCur->iLvl = -1; |
| 2770 | | - pCur->iRowid = 1; |
| 2771 | | -} |
| 2772 | | - |
| 2773 | | -/* |
| 2774 | | -** Destructor for an fsdir_cursor. |
| 2775 | | -*/ |
| 2776 | | -static int fsdirClose(sqlite3_vtab_cursor *cur){ |
| 2777 | | - fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 2778 | | - |
| 2779 | | - fsdirResetCursor(pCur); |
| 2780 | | - sqlite3_free(pCur); |
| 2781 | | - return SQLITE_OK; |
| 2782 | | -} |
| 2783 | | - |
| 2784 | | -/* |
| 2785 | | -** Set the error message for the virtual table associated with cursor |
| 2786 | | -** pCur to the results of vprintf(zFmt, ...). |
| 2787 | | -*/ |
| 2788 | | -static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){ |
| 2789 | | - va_list ap; |
| 2790 | | - va_start(ap, zFmt); |
| 2791 | | - pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); |
| 2792 | | - va_end(ap); |
| 2793 | | -} |
| 2794 | | - |
| 2795 | | - |
| 2796 | | -/* |
| 2797 | | -** Advance an fsdir_cursor to its next row of output. |
| 2798 | | -*/ |
| 2799 | | -static int fsdirNext(sqlite3_vtab_cursor *cur){ |
| 2800 | | - fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 2801 | | - mode_t m = pCur->sStat.st_mode; |
| 2802 | | - |
| 2803 | | - pCur->iRowid++; |
| 2804 | | - if( S_ISDIR(m) ){ |
| 2805 | | - /* Descend into this directory */ |
| 2806 | | - int iNew = pCur->iLvl + 1; |
| 2807 | | - FsdirLevel *pLvl; |
| 2808 | | - if( iNew>=pCur->nLvl ){ |
| 2809 | | - int nNew = iNew+1; |
| 2810 | | - sqlite3_int64 nByte = nNew*sizeof(FsdirLevel); |
| 2811 | | - FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte); |
| 2812 | | - if( aNew==0 ) return SQLITE_NOMEM; |
| 2813 | | - memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl)); |
| 2814 | | - pCur->aLvl = aNew; |
| 2815 | | - pCur->nLvl = nNew; |
| 2816 | | - } |
| 2817 | | - pCur->iLvl = iNew; |
| 2818 | | - pLvl = &pCur->aLvl[iNew]; |
| 2819 | | - |
| 2820 | | - pLvl->zDir = pCur->zPath; |
| 2821 | | - pCur->zPath = 0; |
| 2822 | | - pLvl->pDir = opendir(pLvl->zDir); |
| 2823 | | - if( pLvl->pDir==0 ){ |
| 2824 | | - fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath); |
| 2825 | | - return SQLITE_ERROR; |
| 2826 | | - } |
| 2827 | | - } |
| 2828 | | - |
| 2829 | | - while( pCur->iLvl>=0 ){ |
| 2830 | | - FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl]; |
| 2831 | | - struct dirent *pEntry = readdir(pLvl->pDir); |
| 2832 | | - if( pEntry ){ |
| 2833 | | - if( pEntry->d_name[0]=='.' ){ |
| 2834 | | - if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue; |
| 2835 | | - if( pEntry->d_name[1]=='\0' ) continue; |
| 2836 | | - } |
| 2837 | | - sqlite3_free(pCur->zPath); |
| 2838 | | - pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name); |
| 2839 | | - if( pCur->zPath==0 ) return SQLITE_NOMEM; |
| 2840 | | - if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ |
| 2841 | | - fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); |
| 2842 | | - return SQLITE_ERROR; |
| 2843 | | - } |
| 2844 | | - return SQLITE_OK; |
| 2845 | | - } |
| 2846 | | - closedir(pLvl->pDir); |
| 2847 | | - sqlite3_free(pLvl->zDir); |
| 2848 | | - pLvl->pDir = 0; |
| 2849 | | - pLvl->zDir = 0; |
| 2850 | | - pCur->iLvl--; |
| 2851 | | - } |
| 2852 | | - |
| 2853 | | - /* EOF */ |
| 2854 | | - sqlite3_free(pCur->zPath); |
| 2855 | | - pCur->zPath = 0; |
| 2856 | | - return SQLITE_OK; |
| 2857 | | -} |
| 2858 | | - |
| 2859 | | -/* |
| 2860 | | -** Return values of columns for the row at which the series_cursor |
| 2861 | | -** is currently pointing. |
| 2862 | | -*/ |
| 2863 | | -static int fsdirColumn( |
| 2864 | | - sqlite3_vtab_cursor *cur, /* The cursor */ |
| 2865 | | - sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 2866 | | - int i /* Which column to return */ |
| 2867 | | -){ |
| 2868 | | - fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 2869 | | - switch( i ){ |
| 2870 | | - case FSDIR_COLUMN_NAME: { |
| 2871 | | - sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT); |
| 2872 | | - break; |
| 2873 | | - } |
| 2874 | | - |
| 2875 | | - case FSDIR_COLUMN_MODE: |
| 2876 | | - sqlite3_result_int64(ctx, pCur->sStat.st_mode); |
| 2877 | | - break; |
| 2878 | | - |
| 2879 | | - case FSDIR_COLUMN_MTIME: |
| 2880 | | - sqlite3_result_int64(ctx, pCur->sStat.st_mtime); |
| 2881 | | - break; |
| 2882 | | - |
| 2883 | | - case FSDIR_COLUMN_DATA: { |
| 2884 | | - mode_t m = pCur->sStat.st_mode; |
| 2885 | | - if( S_ISDIR(m) ){ |
| 2886 | | - sqlite3_result_null(ctx); |
| 2887 | | -#if !defined(_WIN32) && !defined(WIN32) |
| 2888 | | - }else if( S_ISLNK(m) ){ |
| 2889 | | - char aStatic[64]; |
| 2890 | | - char *aBuf = aStatic; |
| 2891 | | - sqlite3_int64 nBuf = 64; |
| 2892 | | - int n; |
| 2893 | | - |
| 2894 | | - while( 1 ){ |
| 2895 | | - n = readlink(pCur->zPath, aBuf, nBuf); |
| 2896 | | - if( n<nBuf ) break; |
| 2897 | | - if( aBuf!=aStatic ) sqlite3_free(aBuf); |
| 2898 | | - nBuf = nBuf*2; |
| 2899 | | - aBuf = sqlite3_malloc64(nBuf); |
| 2900 | | - if( aBuf==0 ){ |
| 2901 | | - sqlite3_result_error_nomem(ctx); |
| 2902 | | - return SQLITE_NOMEM; |
| 2903 | | - } |
| 2904 | | - } |
| 2905 | | - |
| 2906 | | - sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT); |
| 2907 | | - if( aBuf!=aStatic ) sqlite3_free(aBuf); |
| 2908 | | -#endif |
| 2909 | | - }else{ |
| 2910 | | - readFileContents(ctx, pCur->zPath); |
| 2911 | | - } |
| 2912 | | - } |
| 2913 | | - case FSDIR_COLUMN_PATH: |
| 2914 | | - default: { |
| 2915 | | - /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters. |
| 2916 | | - ** always return their values as NULL */ |
| 2917 | | - break; |
| 2918 | | - } |
| 2919 | | - } |
| 2920 | | - return SQLITE_OK; |
| 2921 | | -} |
| 2922 | | - |
| 2923 | | -/* |
| 2924 | | -** Return the rowid for the current row. In this implementation, the |
| 2925 | | -** first row returned is assigned rowid value 1, and each subsequent |
| 2926 | | -** row a value 1 more than that of the previous. |
| 2927 | | -*/ |
| 2928 | | -static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 2929 | | - fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 2930 | | - *pRowid = pCur->iRowid; |
| 2931 | | - return SQLITE_OK; |
| 2932 | | -} |
| 2933 | | - |
| 2934 | | -/* |
| 2935 | | -** Return TRUE if the cursor has been moved off of the last |
| 2936 | | -** row of output. |
| 2937 | | -*/ |
| 2938 | | -static int fsdirEof(sqlite3_vtab_cursor *cur){ |
| 2939 | | - fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 2940 | | - return (pCur->zPath==0); |
| 2941 | | -} |
| 2942 | | - |
| 2943 | | -/* |
| 2944 | | -** xFilter callback. |
| 2945 | | -** |
| 2946 | | -** idxNum==1 PATH parameter only |
| 2947 | | -** idxNum==2 Both PATH and DIR supplied |
| 2948 | | -*/ |
| 2949 | | -static int fsdirFilter( |
| 2950 | | - sqlite3_vtab_cursor *cur, |
| 2951 | | - int idxNum, const char *idxStr, |
| 2952 | | - int argc, sqlite3_value **argv |
| 2953 | | -){ |
| 2954 | | - const char *zDir = 0; |
| 2955 | | - fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 2956 | | - (void)idxStr; |
| 2957 | | - fsdirResetCursor(pCur); |
| 2958 | | - |
| 2959 | | - if( idxNum==0 ){ |
| 2960 | | - fsdirSetErrmsg(pCur, "table function fsdir requires an argument"); |
| 2961 | | - return SQLITE_ERROR; |
| 2962 | | - } |
| 2963 | | - |
| 2964 | | - assert( argc==idxNum && (argc==1 || argc==2) ); |
| 2965 | | - zDir = (const char*)sqlite3_value_text(argv[0]); |
| 2966 | | - if( zDir==0 ){ |
| 2967 | | - fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument"); |
| 2968 | | - return SQLITE_ERROR; |
| 2969 | | - } |
| 2970 | | - if( argc==2 ){ |
| 2971 | | - pCur->zBase = (const char*)sqlite3_value_text(argv[1]); |
| 2972 | | - } |
| 2973 | | - if( pCur->zBase ){ |
| 2974 | | - pCur->nBase = (int)strlen(pCur->zBase)+1; |
| 2975 | | - pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir); |
| 2976 | | - }else{ |
| 2977 | | - pCur->zPath = sqlite3_mprintf("%s", zDir); |
| 2978 | | - } |
| 2979 | | - |
| 2980 | | - if( pCur->zPath==0 ){ |
| 2981 | | - return SQLITE_NOMEM; |
| 2982 | | - } |
| 2983 | | - if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ |
| 2984 | | - fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); |
| 2985 | | - return SQLITE_ERROR; |
| 2986 | | - } |
| 2987 | | - |
| 2988 | | - return SQLITE_OK; |
| 2989 | | -} |
| 2990 | | - |
| 2991 | | -/* |
| 2992 | | -** SQLite will invoke this method one or more times while planning a query |
| 2993 | | -** that uses the generate_series virtual table. This routine needs to create |
| 2994 | | -** a query plan for each invocation and compute an estimated cost for that |
| 2995 | | -** plan. |
| 2996 | | -** |
| 2997 | | -** In this implementation idxNum is used to represent the |
| 2998 | | -** query plan. idxStr is unused. |
| 2999 | | -** |
| 3000 | | -** The query plan is represented by values of idxNum: |
| 3001 | | -** |
| 3002 | | -** (1) The path value is supplied by argv[0] |
| 3003 | | -** (2) Path is in argv[0] and dir is in argv[1] |
| 3004 | | -*/ |
| 3005 | | -static int fsdirBestIndex( |
| 3006 | | - sqlite3_vtab *tab, |
| 3007 | | - sqlite3_index_info *pIdxInfo |
| 3008 | | -){ |
| 3009 | | - int i; /* Loop over constraints */ |
| 3010 | | - int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */ |
| 3011 | | - int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */ |
| 3012 | | - int seenPath = 0; /* True if an unusable PATH= constraint is seen */ |
| 3013 | | - int seenDir = 0; /* True if an unusable DIR= constraint is seen */ |
| 3014 | | - const struct sqlite3_index_constraint *pConstraint; |
| 3015 | | - |
| 3016 | | - (void)tab; |
| 3017 | | - pConstraint = pIdxInfo->aConstraint; |
| 3018 | | - for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 3019 | | - if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 3020 | | - switch( pConstraint->iColumn ){ |
| 3021 | | - case FSDIR_COLUMN_PATH: { |
| 3022 | | - if( pConstraint->usable ){ |
| 3023 | | - idxPath = i; |
| 3024 | | - seenPath = 0; |
| 3025 | | - }else if( idxPath<0 ){ |
| 3026 | | - seenPath = 1; |
| 3027 | | - } |
| 3028 | | - break; |
| 3029 | | - } |
| 3030 | | - case FSDIR_COLUMN_DIR: { |
| 3031 | | - if( pConstraint->usable ){ |
| 3032 | | - idxDir = i; |
| 3033 | | - seenDir = 0; |
| 3034 | | - }else if( idxDir<0 ){ |
| 3035 | | - seenDir = 1; |
| 3036 | | - } |
| 3037 | | - break; |
| 3038 | | - } |
| 3039 | | - } |
| 3040 | | - } |
| 3041 | | - if( seenPath || seenDir ){ |
| 3042 | | - /* If input parameters are unusable, disallow this plan */ |
| 3043 | | - return SQLITE_CONSTRAINT; |
| 3044 | | - } |
| 3045 | | - |
| 3046 | | - if( idxPath<0 ){ |
| 3047 | | - pIdxInfo->idxNum = 0; |
| 3048 | | - /* The pIdxInfo->estimatedCost should have been initialized to a huge |
| 3049 | | - ** number. Leave it unchanged. */ |
| 3050 | | - pIdxInfo->estimatedRows = 0x7fffffff; |
| 3051 | | - }else{ |
| 3052 | | - pIdxInfo->aConstraintUsage[idxPath].omit = 1; |
| 3053 | | - pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1; |
| 3054 | | - if( idxDir>=0 ){ |
| 3055 | | - pIdxInfo->aConstraintUsage[idxDir].omit = 1; |
| 3056 | | - pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2; |
| 3057 | | - pIdxInfo->idxNum = 2; |
| 3058 | | - pIdxInfo->estimatedCost = 10.0; |
| 3059 | | - }else{ |
| 3060 | | - pIdxInfo->idxNum = 1; |
| 3061 | | - pIdxInfo->estimatedCost = 100.0; |
| 3062 | | - } |
| 3063 | | - } |
| 3064 | | - |
| 3065 | | - return SQLITE_OK; |
| 3066 | | -} |
| 3067 | | - |
| 3068 | | -/* |
| 3069 | | -** Register the "fsdir" virtual table. |
| 3070 | | -*/ |
| 3071 | | -static int fsdirRegister(sqlite3 *db){ |
| 3072 | | - static sqlite3_module fsdirModule = { |
| 3073 | | - 0, /* iVersion */ |
| 3074 | | - 0, /* xCreate */ |
| 3075 | | - fsdirConnect, /* xConnect */ |
| 3076 | | - fsdirBestIndex, /* xBestIndex */ |
| 3077 | | - fsdirDisconnect, /* xDisconnect */ |
| 3078 | | - 0, /* xDestroy */ |
| 3079 | | - fsdirOpen, /* xOpen - open a cursor */ |
| 3080 | | - fsdirClose, /* xClose - close a cursor */ |
| 3081 | | - fsdirFilter, /* xFilter - configure scan constraints */ |
| 3082 | | - fsdirNext, /* xNext - advance a cursor */ |
| 3083 | | - fsdirEof, /* xEof - check for end of scan */ |
| 3084 | | - fsdirColumn, /* xColumn - read data */ |
| 3085 | | - fsdirRowid, /* xRowid - read data */ |
| 3086 | | - 0, /* xUpdate */ |
| 3087 | | - 0, /* xBegin */ |
| 3088 | | - 0, /* xSync */ |
| 3089 | | - 0, /* xCommit */ |
| 3090 | | - 0, /* xRollback */ |
| 3091 | | - 0, /* xFindMethod */ |
| 3092 | | - 0, /* xRename */ |
| 3093 | | - 0, /* xSavepoint */ |
| 3094 | | - 0, /* xRelease */ |
| 3095 | | - 0, /* xRollbackTo */ |
| 3096 | | - 0, /* xShadowName */ |
| 3097 | | - }; |
| 3098 | | - |
| 3099 | | - int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0); |
| 3100 | | - return rc; |
| 3101 | | -} |
| 3102 | | -#else /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3103 | | -# define fsdirRegister(x) SQLITE_OK |
| 3104 | | -#endif |
| 3105 | | - |
| 3106 | | -#ifdef _WIN32 |
| 3107 | | - |
| 3108 | | -#endif |
| 3109 | | -int sqlite3_fileio_init( |
| 3110 | | - sqlite3 *db, |
| 3111 | | - char **pzErrMsg, |
| 3112 | | - const sqlite3_api_routines *pApi |
| 3113 | | -){ |
| 3114 | | - int rc = SQLITE_OK; |
| 3115 | | - SQLITE_EXTENSION_INIT2(pApi); |
| 3116 | | - (void)pzErrMsg; /* Unused parameter */ |
| 3117 | | - rc = sqlite3_create_function(db, "readfile", 1, |
| 3118 | | - SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
| 3119 | | - readfileFunc, 0, 0); |
| 3120 | | - if( rc==SQLITE_OK ){ |
| 3121 | | - rc = sqlite3_create_function(db, "writefile", -1, |
| 3122 | | - SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
| 3123 | | - writefileFunc, 0, 0); |
| 3124 | | - } |
| 3125 | | - if( rc==SQLITE_OK ){ |
| 3126 | | - rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0, |
| 3127 | | - lsModeFunc, 0, 0); |
| 3128 | | - } |
| 3129 | | - if( rc==SQLITE_OK ){ |
| 3130 | | - rc = fsdirRegister(db); |
| 3131 | | - } |
| 3132 | | - return rc; |
| 3133 | | -} |
| 3134 | | - |
| 3135 | | -#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32)) |
| 3136 | | -/* To allow a standalone DLL, make test_windirent.c use the same |
| 3137 | | - * redefined SQLite API calls as the above extension code does. |
| 3138 | | - * Just pull in this .c to accomplish this. As a beneficial side |
| 3139 | | - * effect, this extension becomes a single translation unit. */ |
| 3140 | | -# include "test_windirent.c" |
| 3141 | | -#endif |
| 3142 | | - |
| 3143 | | -/************************* End ../ext/misc/fileio.c ********************/ |
| 3144 | | -/************************* Begin ../ext/misc/completion.c ******************/ |
| 3145 | | -/* |
| 3146 | | -** 2017-07-10 |
| 3147 | | -** |
| 3148 | | -** The author disclaims copyright to this source code. In place of |
| 3149 | | -** a legal notice, here is a blessing: |
| 3150 | | -** |
| 3151 | | -** May you do good and not evil. |
| 3152 | | -** May you find forgiveness for yourself and forgive others. |
| 3153 | | -** May you share freely, never taking more than you give. |
| 3154 | | -** |
| 3155 | | -************************************************************************* |
| 3156 | | -** |
| 3157 | | -** This file implements an eponymous virtual table that returns suggested |
| 3158 | | -** completions for a partial SQL input. |
| 3159 | | -** |
| 3160 | | -** Suggested usage: |
| 3161 | | -** |
| 3162 | | -** SELECT DISTINCT candidate COLLATE nocase |
| 3163 | | -** FROM completion($prefix,$wholeline) |
| 3164 | | -** ORDER BY 1; |
| 3165 | | -** |
| 3166 | | -** The two query parameters are optional. $prefix is the text of the |
| 3167 | | -** current word being typed and that is to be completed. $wholeline is |
| 3168 | | -** the complete input line, used for context. |
| 3169 | | -** |
| 3170 | | -** The raw completion() table might return the same candidate multiple |
| 3171 | | -** times, for example if the same column name is used to two or more |
| 3172 | | -** tables. And the candidates are returned in an arbitrary order. Hence, |
| 3173 | | -** the DISTINCT and ORDER BY are recommended. |
| 3174 | | -** |
| 3175 | | -** This virtual table operates at the speed of human typing, and so there |
| 3176 | | -** is no attempt to make it fast. Even a slow implementation will be much |
| 3177 | | -** faster than any human can type. |
| 3178 | | -** |
| 3179 | | -*/ |
| 3180 | | -/* #include "sqlite3ext.h" */ |
| 3181 | | -SQLITE_EXTENSION_INIT1 |
| 3182 | | -#include <assert.h> |
| 3183 | | -#include <string.h> |
| 3184 | | -#include <ctype.h> |
| 3185 | | - |
| 3186 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3187 | | - |
| 3188 | | -/* completion_vtab is a subclass of sqlite3_vtab which will |
| 3189 | | -** serve as the underlying representation of a completion virtual table |
| 3190 | | -*/ |
| 3191 | | -typedef struct completion_vtab completion_vtab; |
| 3192 | | -struct completion_vtab { |
| 3193 | | - sqlite3_vtab base; /* Base class - must be first */ |
| 3194 | | - sqlite3 *db; /* Database connection for this completion vtab */ |
| 3195 | | -}; |
| 3196 | | - |
| 3197 | | -/* completion_cursor is a subclass of sqlite3_vtab_cursor which will |
| 3198 | | -** serve as the underlying representation of a cursor that scans |
| 3199 | | -** over rows of the result |
| 3200 | | -*/ |
| 3201 | | -typedef struct completion_cursor completion_cursor; |
| 3202 | | -struct completion_cursor { |
| 3203 | | - sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 3204 | | - sqlite3 *db; /* Database connection for this cursor */ |
| 3205 | | - int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */ |
| 3206 | | - char *zPrefix; /* The prefix for the word we want to complete */ |
| 3207 | | - char *zLine; /* The whole that we want to complete */ |
| 3208 | | - const char *zCurrentRow; /* Current output row */ |
| 3209 | | - int szRow; /* Length of the zCurrentRow string */ |
| 3210 | | - sqlite3_stmt *pStmt; /* Current statement */ |
| 3211 | | - sqlite3_int64 iRowid; /* The rowid */ |
| 3212 | | - int ePhase; /* Current phase */ |
| 3213 | | - int j; /* inter-phase counter */ |
| 3214 | | -}; |
| 3215 | | - |
| 3216 | | -/* Values for ePhase: |
| 3217 | | -*/ |
| 3218 | | -#define COMPLETION_FIRST_PHASE 1 |
| 3219 | | -#define COMPLETION_KEYWORDS 1 |
| 3220 | | -#define COMPLETION_PRAGMAS 2 |
| 3221 | | -#define COMPLETION_FUNCTIONS 3 |
| 3222 | | -#define COMPLETION_COLLATIONS 4 |
| 3223 | | -#define COMPLETION_INDEXES 5 |
| 3224 | | -#define COMPLETION_TRIGGERS 6 |
| 3225 | | -#define COMPLETION_DATABASES 7 |
| 3226 | | -#define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */ |
| 3227 | | -#define COMPLETION_COLUMNS 9 |
| 3228 | | -#define COMPLETION_MODULES 10 |
| 3229 | | -#define COMPLETION_EOF 11 |
| 3230 | | - |
| 3231 | | -/* |
| 3232 | | -** The completionConnect() method is invoked to create a new |
| 3233 | | -** completion_vtab that describes the completion virtual table. |
| 3234 | | -** |
| 3235 | | -** Think of this routine as the constructor for completion_vtab objects. |
| 3236 | | -** |
| 3237 | | -** All this routine needs to do is: |
| 3238 | | -** |
| 3239 | | -** (1) Allocate the completion_vtab object and initialize all fields. |
| 3240 | | -** |
| 3241 | | -** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the |
| 3242 | | -** result set of queries against completion will look like. |
| 3243 | | -*/ |
| 3244 | | -static int completionConnect( |
| 3245 | | - sqlite3 *db, |
| 3246 | | - void *pAux, |
| 3247 | | - int argc, const char *const*argv, |
| 3248 | | - sqlite3_vtab **ppVtab, |
| 3249 | | - char **pzErr |
| 3250 | | -){ |
| 3251 | | - completion_vtab *pNew; |
| 3252 | | - int rc; |
| 3253 | | - |
| 3254 | | - (void)(pAux); /* Unused parameter */ |
| 3255 | | - (void)(argc); /* Unused parameter */ |
| 3256 | | - (void)(argv); /* Unused parameter */ |
| 3257 | | - (void)(pzErr); /* Unused parameter */ |
| 3258 | | - |
| 3259 | | -/* Column numbers */ |
| 3260 | | -#define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */ |
| 3261 | | -#define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */ |
| 3262 | | -#define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */ |
| 3263 | | -#define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ |
| 3264 | | - |
| 3265 | | - sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
| 3266 | | - rc = sqlite3_declare_vtab(db, |
| 3267 | | - "CREATE TABLE x(" |
| 3268 | | - " candidate TEXT," |
| 3269 | | - " prefix TEXT HIDDEN," |
| 3270 | | - " wholeline TEXT HIDDEN," |
| 3271 | | - " phase INT HIDDEN" /* Used for debugging only */ |
| 3272 | | - ")"); |
| 3273 | | - if( rc==SQLITE_OK ){ |
| 3274 | | - pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 3275 | | - *ppVtab = (sqlite3_vtab*)pNew; |
| 3276 | | - if( pNew==0 ) return SQLITE_NOMEM; |
| 3277 | | - memset(pNew, 0, sizeof(*pNew)); |
| 3278 | | - pNew->db = db; |
| 3279 | | - } |
| 3280 | | - return rc; |
| 3281 | | -} |
| 3282 | | - |
| 3283 | | -/* |
| 3284 | | -** This method is the destructor for completion_cursor objects. |
| 3285 | | -*/ |
| 3286 | | -static int completionDisconnect(sqlite3_vtab *pVtab){ |
| 3287 | | - sqlite3_free(pVtab); |
| 3288 | | - return SQLITE_OK; |
| 3289 | | -} |
| 3290 | | - |
| 3291 | | -/* |
| 3292 | | -** Constructor for a new completion_cursor object. |
| 3293 | | -*/ |
| 3294 | | -static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 3295 | | - completion_cursor *pCur; |
| 3296 | | - pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 3297 | | - if( pCur==0 ) return SQLITE_NOMEM; |
| 3298 | | - memset(pCur, 0, sizeof(*pCur)); |
| 3299 | | - pCur->db = ((completion_vtab*)p)->db; |
| 3300 | | - *ppCursor = &pCur->base; |
| 3301 | | - return SQLITE_OK; |
| 3302 | | -} |
| 3303 | | - |
| 3304 | | -/* |
| 3305 | | -** Reset the completion_cursor. |
| 3306 | | -*/ |
| 3307 | | -static void completionCursorReset(completion_cursor *pCur){ |
| 3308 | | - sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; |
| 3309 | | - sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; |
| 3310 | | - sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; |
| 3311 | | - pCur->j = 0; |
| 3312 | | -} |
| 3313 | | - |
| 3314 | | -/* |
| 3315 | | -** Destructor for a completion_cursor. |
| 3316 | | -*/ |
| 3317 | | -static int completionClose(sqlite3_vtab_cursor *cur){ |
| 3318 | | - completionCursorReset((completion_cursor*)cur); |
| 3319 | | - sqlite3_free(cur); |
| 3320 | | - return SQLITE_OK; |
| 3321 | | -} |
| 3322 | | - |
| 3323 | | -/* |
| 3324 | | -** Advance a completion_cursor to its next row of output. |
| 3325 | | -** |
| 3326 | | -** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object |
| 3327 | | -** record the current state of the scan. This routine sets ->zCurrentRow |
| 3328 | | -** to the current row of output and then returns. If no more rows remain, |
| 3329 | | -** then ->ePhase is set to COMPLETION_EOF which will signal the virtual |
| 3330 | | -** table that has reached the end of its scan. |
| 3331 | | -** |
| 3332 | | -** The current implementation just lists potential identifiers and |
| 3333 | | -** keywords and filters them by zPrefix. Future enhancements should |
| 3334 | | -** take zLine into account to try to restrict the set of identifiers and |
| 3335 | | -** keywords based on what would be legal at the current point of input. |
| 3336 | | -*/ |
| 3337 | | -static int completionNext(sqlite3_vtab_cursor *cur){ |
| 3338 | | - completion_cursor *pCur = (completion_cursor*)cur; |
| 3339 | | - int eNextPhase = 0; /* Next phase to try if current phase reaches end */ |
| 3340 | | - int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ |
| 3341 | | - pCur->iRowid++; |
| 3342 | | - while( pCur->ePhase!=COMPLETION_EOF ){ |
| 3343 | | - switch( pCur->ePhase ){ |
| 3344 | | - case COMPLETION_KEYWORDS: { |
| 3345 | | - if( pCur->j >= sqlite3_keyword_count() ){ |
| 3346 | | - pCur->zCurrentRow = 0; |
| 3347 | | - pCur->ePhase = COMPLETION_DATABASES; |
| 3348 | | - }else{ |
| 3349 | | - sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow); |
| 3350 | | - } |
| 3351 | | - iCol = -1; |
| 3352 | | - break; |
| 3353 | | - } |
| 3354 | | - case COMPLETION_DATABASES: { |
| 3355 | | - if( pCur->pStmt==0 ){ |
| 3356 | | - sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, |
| 3357 | | - &pCur->pStmt, 0); |
| 3358 | | - } |
| 3359 | | - iCol = 1; |
| 3360 | | - eNextPhase = COMPLETION_TABLES; |
| 3361 | | - break; |
| 3362 | | - } |
| 3363 | | - case COMPLETION_TABLES: { |
| 3364 | | - if( pCur->pStmt==0 ){ |
| 3365 | | - sqlite3_stmt *pS2; |
| 3366 | | - char *zSql = 0; |
| 3367 | | - const char *zSep = ""; |
| 3368 | | - sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
| 3369 | | - while( sqlite3_step(pS2)==SQLITE_ROW ){ |
| 3370 | | - const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
| 3371 | | - zSql = sqlite3_mprintf( |
| 3372 | | - "%z%s" |
| 3373 | | - "SELECT name FROM \"%w\".sqlite_schema", |
| 3374 | | - zSql, zSep, zDb |
| 3375 | | - ); |
| 3376 | | - if( zSql==0 ) return SQLITE_NOMEM; |
| 3377 | | - zSep = " UNION "; |
| 3378 | | - } |
| 3379 | | - sqlite3_finalize(pS2); |
| 3380 | | - sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
| 3381 | | - sqlite3_free(zSql); |
| 3382 | | - } |
| 3383 | | - iCol = 0; |
| 3384 | | - eNextPhase = COMPLETION_COLUMNS; |
| 3385 | | - break; |
| 3386 | | - } |
| 3387 | | - case COMPLETION_COLUMNS: { |
| 3388 | | - if( pCur->pStmt==0 ){ |
| 3389 | | - sqlite3_stmt *pS2; |
| 3390 | | - char *zSql = 0; |
| 3391 | | - const char *zSep = ""; |
| 3392 | | - sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
| 3393 | | - while( sqlite3_step(pS2)==SQLITE_ROW ){ |
| 3394 | | - const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
| 3395 | | - zSql = sqlite3_mprintf( |
| 3396 | | - "%z%s" |
| 3397 | | - "SELECT pti.name FROM \"%w\".sqlite_schema AS sm" |
| 3398 | | - " JOIN pragma_table_info(sm.name,%Q) AS pti" |
| 3399 | | - " WHERE sm.type='table'", |
| 3400 | | - zSql, zSep, zDb, zDb |
| 3401 | | - ); |
| 3402 | | - if( zSql==0 ) return SQLITE_NOMEM; |
| 3403 | | - zSep = " UNION "; |
| 3404 | | - } |
| 3405 | | - sqlite3_finalize(pS2); |
| 3406 | | - sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
| 3407 | | - sqlite3_free(zSql); |
| 3408 | | - } |
| 3409 | | - iCol = 0; |
| 3410 | | - eNextPhase = COMPLETION_EOF; |
| 3411 | | - break; |
| 3412 | | - } |
| 3413 | | - } |
| 3414 | | - if( iCol<0 ){ |
| 3415 | | - /* This case is when the phase presets zCurrentRow */ |
| 3416 | | - if( pCur->zCurrentRow==0 ) continue; |
| 3417 | | - }else{ |
| 3418 | | - if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ |
| 3419 | | - /* Extract the next row of content */ |
| 3420 | | - pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); |
| 3421 | | - pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol); |
| 3422 | | - }else{ |
| 3423 | | - /* When all rows are finished, advance to the next phase */ |
| 3424 | | - sqlite3_finalize(pCur->pStmt); |
| 3425 | | - pCur->pStmt = 0; |
| 3426 | | - pCur->ePhase = eNextPhase; |
| 3427 | | - continue; |
| 3428 | | - } |
| 3429 | | - } |
| 3430 | | - if( pCur->nPrefix==0 ) break; |
| 3431 | | - if( pCur->nPrefix<=pCur->szRow |
| 3432 | | - && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 |
| 3433 | | - ){ |
| 3434 | | - break; |
| 3435 | | - } |
| 3436 | | - } |
| 3437 | | - |
| 3438 | | - return SQLITE_OK; |
| 3439 | | -} |
| 3440 | | - |
| 3441 | | -/* |
| 3442 | | -** Return values of columns for the row at which the completion_cursor |
| 3443 | | -** is currently pointing. |
| 3444 | | -*/ |
| 3445 | | -static int completionColumn( |
| 3446 | | - sqlite3_vtab_cursor *cur, /* The cursor */ |
| 3447 | | - sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 3448 | | - int i /* Which column to return */ |
| 3449 | | -){ |
| 3450 | | - completion_cursor *pCur = (completion_cursor*)cur; |
| 3451 | | - switch( i ){ |
| 3452 | | - case COMPLETION_COLUMN_CANDIDATE: { |
| 3453 | | - sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT); |
| 3454 | | - break; |
| 3455 | | - } |
| 3456 | | - case COMPLETION_COLUMN_PREFIX: { |
| 3457 | | - sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); |
| 3458 | | - break; |
| 3459 | | - } |
| 3460 | | - case COMPLETION_COLUMN_WHOLELINE: { |
| 3461 | | - sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); |
| 3462 | | - break; |
| 3463 | | - } |
| 3464 | | - case COMPLETION_COLUMN_PHASE: { |
| 3465 | | - sqlite3_result_int(ctx, pCur->ePhase); |
| 3466 | | - break; |
| 3467 | | - } |
| 3468 | | - } |
| 3469 | | - return SQLITE_OK; |
| 3470 | | -} |
| 3471 | | - |
| 3472 | | -/* |
| 3473 | | -** Return the rowid for the current row. In this implementation, the |
| 3474 | | -** rowid is the same as the output value. |
| 3475 | | -*/ |
| 3476 | | -static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 3477 | | - completion_cursor *pCur = (completion_cursor*)cur; |
| 3478 | | - *pRowid = pCur->iRowid; |
| 3479 | | - return SQLITE_OK; |
| 3480 | | -} |
| 3481 | | - |
| 3482 | | -/* |
| 3483 | | -** Return TRUE if the cursor has been moved off of the last |
| 3484 | | -** row of output. |
| 3485 | | -*/ |
| 3486 | | -static int completionEof(sqlite3_vtab_cursor *cur){ |
| 3487 | | - completion_cursor *pCur = (completion_cursor*)cur; |
| 3488 | | - return pCur->ePhase >= COMPLETION_EOF; |
| 3489 | | -} |
| 3490 | | - |
| 3491 | | -/* |
| 3492 | | -** This method is called to "rewind" the completion_cursor object back |
| 3493 | | -** to the first row of output. This method is always called at least |
| 3494 | | -** once prior to any call to completionColumn() or completionRowid() or |
| 3495 | | -** completionEof(). |
| 3496 | | -*/ |
| 3497 | | -static int completionFilter( |
| 3498 | | - sqlite3_vtab_cursor *pVtabCursor, |
| 3499 | | - int idxNum, const char *idxStr, |
| 3500 | | - int argc, sqlite3_value **argv |
| 3501 | | -){ |
| 3502 | | - completion_cursor *pCur = (completion_cursor *)pVtabCursor; |
| 3503 | | - int iArg = 0; |
| 3504 | | - (void)(idxStr); /* Unused parameter */ |
| 3505 | | - (void)(argc); /* Unused parameter */ |
| 3506 | | - completionCursorReset(pCur); |
| 3507 | | - if( idxNum & 1 ){ |
| 3508 | | - pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); |
| 3509 | | - if( pCur->nPrefix>0 ){ |
| 3510 | | - pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
| 3511 | | - if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
| 3512 | | - } |
| 3513 | | - iArg = 1; |
| 3514 | | - } |
| 3515 | | - if( idxNum & 2 ){ |
| 3516 | | - pCur->nLine = sqlite3_value_bytes(argv[iArg]); |
| 3517 | | - if( pCur->nLine>0 ){ |
| 3518 | | - pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
| 3519 | | - if( pCur->zLine==0 ) return SQLITE_NOMEM; |
| 3520 | | - } |
| 3521 | | - } |
| 3522 | | - if( pCur->zLine!=0 && pCur->zPrefix==0 ){ |
| 3523 | | - int i = pCur->nLine; |
| 3524 | | - while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ |
| 3525 | | - i--; |
| 3526 | | - } |
| 3527 | | - pCur->nPrefix = pCur->nLine - i; |
| 3528 | | - if( pCur->nPrefix>0 ){ |
| 3529 | | - pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); |
| 3530 | | - if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
| 3531 | | - } |
| 3532 | | - } |
| 3533 | | - pCur->iRowid = 0; |
| 3534 | | - pCur->ePhase = COMPLETION_FIRST_PHASE; |
| 3535 | | - return completionNext(pVtabCursor); |
| 3536 | | -} |
| 3537 | | - |
| 3538 | | -/* |
| 3539 | | -** SQLite will invoke this method one or more times while planning a query |
| 3540 | | -** that uses the completion virtual table. This routine needs to create |
| 3541 | | -** a query plan for each invocation and compute an estimated cost for that |
| 3542 | | -** plan. |
| 3543 | | -** |
| 3544 | | -** There are two hidden parameters that act as arguments to the table-valued |
| 3545 | | -** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix" |
| 3546 | | -** is available and bit 1 is set if "wholeline" is available. |
| 3547 | | -*/ |
| 3548 | | -static int completionBestIndex( |
| 3549 | | - sqlite3_vtab *tab, |
| 3550 | | - sqlite3_index_info *pIdxInfo |
| 3551 | | -){ |
| 3552 | | - int i; /* Loop over constraints */ |
| 3553 | | - int idxNum = 0; /* The query plan bitmask */ |
| 3554 | | - int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ |
| 3555 | | - int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ |
| 3556 | | - int nArg = 0; /* Number of arguments that completeFilter() expects */ |
| 3557 | | - const struct sqlite3_index_constraint *pConstraint; |
| 3558 | | - |
| 3559 | | - (void)(tab); /* Unused parameter */ |
| 3560 | | - pConstraint = pIdxInfo->aConstraint; |
| 3561 | | - for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 3562 | | - if( pConstraint->usable==0 ) continue; |
| 3563 | | - if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 3564 | | - switch( pConstraint->iColumn ){ |
| 3565 | | - case COMPLETION_COLUMN_PREFIX: |
| 3566 | | - prefixIdx = i; |
| 3567 | | - idxNum |= 1; |
| 3568 | | - break; |
| 3569 | | - case COMPLETION_COLUMN_WHOLELINE: |
| 3570 | | - wholelineIdx = i; |
| 3571 | | - idxNum |= 2; |
| 3572 | | - break; |
| 3573 | | - } |
| 3574 | | - } |
| 3575 | | - if( prefixIdx>=0 ){ |
| 3576 | | - pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; |
| 3577 | | - pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; |
| 3578 | | - } |
| 3579 | | - if( wholelineIdx>=0 ){ |
| 3580 | | - pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; |
| 3581 | | - pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; |
| 3582 | | - } |
| 3583 | | - pIdxInfo->idxNum = idxNum; |
| 3584 | | - pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; |
| 3585 | | - pIdxInfo->estimatedRows = 500 - 100*nArg; |
| 3586 | | - return SQLITE_OK; |
| 3587 | | -} |
| 3588 | | - |
| 3589 | | -/* |
| 3590 | | -** This following structure defines all the methods for the |
| 3591 | | -** completion virtual table. |
| 3592 | | -*/ |
| 3593 | | -static sqlite3_module completionModule = { |
| 3594 | | - 0, /* iVersion */ |
| 3595 | | - 0, /* xCreate */ |
| 3596 | | - completionConnect, /* xConnect */ |
| 3597 | | - completionBestIndex, /* xBestIndex */ |
| 3598 | | - completionDisconnect, /* xDisconnect */ |
| 3599 | | - 0, /* xDestroy */ |
| 3600 | | - completionOpen, /* xOpen - open a cursor */ |
| 3601 | | - completionClose, /* xClose - close a cursor */ |
| 3602 | | - completionFilter, /* xFilter - configure scan constraints */ |
| 3603 | | - completionNext, /* xNext - advance a cursor */ |
| 3604 | | - completionEof, /* xEof - check for end of scan */ |
| 3605 | | - completionColumn, /* xColumn - read data */ |
| 3606 | | - completionRowid, /* xRowid - read data */ |
| 3607 | | - 0, /* xUpdate */ |
| 3608 | | - 0, /* xBegin */ |
| 3609 | | - 0, /* xSync */ |
| 3610 | | - 0, /* xCommit */ |
| 3611 | | - 0, /* xRollback */ |
| 3612 | | - 0, /* xFindMethod */ |
| 3613 | | - 0, /* xRename */ |
| 3614 | | - 0, /* xSavepoint */ |
| 3615 | | - 0, /* xRelease */ |
| 3616 | | - 0, /* xRollbackTo */ |
| 3617 | | - 0 /* xShadowName */ |
| 3618 | | -}; |
| 3619 | | - |
| 3620 | | -#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3621 | | - |
| 3622 | | -int sqlite3CompletionVtabInit(sqlite3 *db){ |
| 3623 | | - int rc = SQLITE_OK; |
| 3624 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3625 | | - rc = sqlite3_create_module(db, "completion", &completionModule, 0); |
| 3626 | | -#endif |
| 3627 | | - return rc; |
| 3628 | | -} |
| 3629 | | - |
| 3630 | | -#ifdef _WIN32 |
| 3631 | | - |
| 3632 | | -#endif |
| 3633 | | -int sqlite3_completion_init( |
| 3634 | | - sqlite3 *db, |
| 3635 | | - char **pzErrMsg, |
| 3636 | | - const sqlite3_api_routines *pApi |
| 3637 | | -){ |
| 3638 | | - int rc = SQLITE_OK; |
| 3639 | | - SQLITE_EXTENSION_INIT2(pApi); |
| 3640 | | - (void)(pzErrMsg); /* Unused parameter */ |
| 3641 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3642 | | - rc = sqlite3CompletionVtabInit(db); |
| 3643 | | -#endif |
| 3644 | | - return rc; |
| 3645 | | -} |
| 3646 | | - |
| 3647 | | -/************************* End ../ext/misc/completion.c ********************/ |
| 3648 | | -/************************* Begin ../ext/misc/appendvfs.c ******************/ |
| 3649 | | -/* |
| 3650 | | -** 2017-10-20 |
| 3651 | | -** |
| 3652 | | -** The author disclaims copyright to this source code. In place of |
| 3653 | | -** a legal notice, here is a blessing: |
| 3654 | | -** |
| 3655 | | -** May you do good and not evil. |
| 3656 | | -** May you find forgiveness for yourself and forgive others. |
| 3657 | | -** May you share freely, never taking more than you give. |
| 3658 | | -** |
| 3659 | | -****************************************************************************** |
| 3660 | | -** |
| 3661 | | -** This file implements a VFS shim that allows an SQLite database to be |
| 3662 | | -** appended onto the end of some other file, such as an executable. |
| 3663 | | -** |
| 3664 | | -** A special record must appear at the end of the file that identifies the |
| 3665 | | -** file as an appended database and provides the offset to the first page |
| 3666 | | -** of the exposed content. (Or, it is the length of the content prefix.) |
| 3667 | | -** For best performance page 1 should be located at a disk page boundary, |
| 3668 | | -** though that is not required. |
| 3669 | | -** |
| 3670 | | -** When opening a database using this VFS, the connection might treat |
| 3671 | | -** the file as an ordinary SQLite database, or it might treat it as a |
| 3672 | | -** database appended onto some other file. The decision is made by |
| 3673 | | -** applying the following rules in order: |
| 3674 | | -** |
| 3675 | | -** (1) An empty file is an ordinary database. |
| 3676 | | -** |
| 3677 | | -** (2) If the file ends with the appendvfs trailer string |
| 3678 | | -** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database. |
| 3679 | | -** |
| 3680 | | -** (3) If the file begins with the standard SQLite prefix string |
| 3681 | | -** "SQLite format 3", that file is an ordinary database. |
| 3682 | | -** |
| 3683 | | -** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is |
| 3684 | | -** set, then a new database is appended to the already existing file. |
| 3685 | | -** |
| 3686 | | -** (5) Otherwise, SQLITE_CANTOPEN is returned. |
| 3687 | | -** |
| 3688 | | -** To avoid unnecessary complications with the PENDING_BYTE, the size of |
| 3689 | | -** the file containing the database is limited to 1GiB. (1073741824 bytes) |
| 3690 | | -** This VFS will not read or write past the 1GiB mark. This restriction |
| 3691 | | -** might be lifted in future versions. For now, if you need a larger |
| 3692 | | -** database, then keep it in a separate file. |
| 3693 | | -** |
| 3694 | | -** If the file being opened is a plain database (not an appended one), then |
| 3695 | | -** this shim is a pass-through into the default underlying VFS. (rule 3) |
| 3696 | | -**/ |
| 3697 | | -/* #include "sqlite3ext.h" */ |
| 3698 | | -SQLITE_EXTENSION_INIT1 |
| 3699 | | -#include <string.h> |
| 3700 | | -#include <assert.h> |
| 3701 | | - |
| 3702 | | -/* The append mark at the end of the database is: |
| 3703 | | -** |
| 3704 | | -** Start-Of-SQLite3-NNNNNNNN |
| 3705 | | -** 123456789 123456789 12345 |
| 3706 | | -** |
| 3707 | | -** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is |
| 3708 | | -** the offset to page 1, and also the length of the prefix content. |
| 3709 | | -*/ |
| 3710 | | -#define APND_MARK_PREFIX "Start-Of-SQLite3-" |
| 3711 | | -#define APND_MARK_PREFIX_SZ 17 |
| 3712 | | -#define APND_MARK_FOS_SZ 8 |
| 3713 | | -#define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ) |
| 3714 | | - |
| 3715 | | -/* |
| 3716 | | -** Maximum size of the combined prefix + database + append-mark. This |
| 3717 | | -** must be less than 0x40000000 to avoid locking issues on Windows. |
| 3718 | | -*/ |
| 3719 | | -#define APND_MAX_SIZE (0x40000000) |
| 3720 | | - |
| 3721 | | -/* |
| 3722 | | -** Try to align the database to an even multiple of APND_ROUNDUP bytes. |
| 3723 | | -*/ |
| 3724 | | -#ifndef APND_ROUNDUP |
| 3725 | | -#define APND_ROUNDUP 4096 |
| 3726 | | -#endif |
| 3727 | | -#define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1)) |
| 3728 | | -#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK) |
| 3729 | | - |
| 3730 | | -/* |
| 3731 | | -** Forward declaration of objects used by this utility |
| 3732 | | -*/ |
| 3733 | | -typedef struct sqlite3_vfs ApndVfs; |
| 3734 | | -typedef struct ApndFile ApndFile; |
| 3735 | | - |
| 3736 | | -/* Access to a lower-level VFS that (might) implement dynamic loading, |
| 3737 | | -** access to randomness, etc. |
| 3738 | | -*/ |
| 3739 | | -#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) |
| 3740 | | -#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1)) |
| 3741 | | - |
| 3742 | | -/* An open appendvfs file |
| 3743 | | -** |
| 3744 | | -** An instance of this structure describes the appended database file. |
| 3745 | | -** A separate sqlite3_file object is always appended. The appended |
| 3746 | | -** sqlite3_file object (which can be accessed using ORIGFILE()) describes |
| 3747 | | -** the entire file, including the prefix, the database, and the |
| 3748 | | -** append-mark. |
| 3749 | | -** |
| 3750 | | -** The structure of an AppendVFS database is like this: |
| 3751 | | -** |
| 3752 | | -** +-------------+---------+----------+-------------+ |
| 3753 | | -** | prefix-file | padding | database | append-mark | |
| 3754 | | -** +-------------+---------+----------+-------------+ |
| 3755 | | -** ^ ^ |
| 3756 | | -** | | |
| 3757 | | -** iPgOne iMark |
| 3758 | | -** |
| 3759 | | -** |
| 3760 | | -** "prefix file" - file onto which the database has been appended. |
| 3761 | | -** "padding" - zero or more bytes inserted so that "database" |
| 3762 | | -** starts on an APND_ROUNDUP boundary |
| 3763 | | -** "database" - The SQLite database file |
| 3764 | | -** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates |
| 3765 | | -** the offset from the start of prefix-file to the start |
| 3766 | | -** of "database". |
| 3767 | | -** |
| 3768 | | -** The size of the database is iMark - iPgOne. |
| 3769 | | -** |
| 3770 | | -** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value |
| 3771 | | -** of iPgOne stored as a big-ending 64-bit integer. |
| 3772 | | -** |
| 3773 | | -** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE). |
| 3774 | | -** Or, iMark is -1 to indicate that it has not yet been written. |
| 3775 | | -*/ |
| 3776 | | -struct ApndFile { |
| 3777 | | - sqlite3_file base; /* Subclass. MUST BE FIRST! */ |
| 3778 | | - sqlite3_int64 iPgOne; /* Offset to the start of the database */ |
| 3779 | | - sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */ |
| 3780 | | - /* Always followed by another sqlite3_file that describes the whole file */ |
| 3781 | | -}; |
| 3782 | | - |
| 3783 | | -/* |
| 3784 | | -** Methods for ApndFile |
| 3785 | | -*/ |
| 3786 | | -static int apndClose(sqlite3_file*); |
| 3787 | | -static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 3788 | | -static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); |
| 3789 | | -static int apndTruncate(sqlite3_file*, sqlite3_int64 size); |
| 3790 | | -static int apndSync(sqlite3_file*, int flags); |
| 3791 | | -static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize); |
| 3792 | | -static int apndLock(sqlite3_file*, int); |
| 3793 | | -static int apndUnlock(sqlite3_file*, int); |
| 3794 | | -static int apndCheckReservedLock(sqlite3_file*, int *pResOut); |
| 3795 | | -static int apndFileControl(sqlite3_file*, int op, void *pArg); |
| 3796 | | -static int apndSectorSize(sqlite3_file*); |
| 3797 | | -static int apndDeviceCharacteristics(sqlite3_file*); |
| 3798 | | -static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**); |
| 3799 | | -static int apndShmLock(sqlite3_file*, int offset, int n, int flags); |
| 3800 | | -static void apndShmBarrier(sqlite3_file*); |
| 3801 | | -static int apndShmUnmap(sqlite3_file*, int deleteFlag); |
| 3802 | | -static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); |
| 3803 | | -static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); |
| 3804 | | - |
| 3805 | | -/* |
| 3806 | | -** Methods for ApndVfs |
| 3807 | | -*/ |
| 3808 | | -static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
| 3809 | | -static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir); |
| 3810 | | -static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
| 3811 | | -static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); |
| 3812 | | -static void *apndDlOpen(sqlite3_vfs*, const char *zFilename); |
| 3813 | | -static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg); |
| 3814 | | -static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); |
| 3815 | | -static void apndDlClose(sqlite3_vfs*, void*); |
| 3816 | | -static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut); |
| 3817 | | -static int apndSleep(sqlite3_vfs*, int microseconds); |
| 3818 | | -static int apndCurrentTime(sqlite3_vfs*, double*); |
| 3819 | | -static int apndGetLastError(sqlite3_vfs*, int, char *); |
| 3820 | | -static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); |
| 3821 | | -static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr); |
| 3822 | | -static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z); |
| 3823 | | -static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName); |
| 3824 | | - |
| 3825 | | -static sqlite3_vfs apnd_vfs = { |
| 3826 | | - 3, /* iVersion (set when registered) */ |
| 3827 | | - 0, /* szOsFile (set when registered) */ |
| 3828 | | - 1024, /* mxPathname */ |
| 3829 | | - 0, /* pNext */ |
| 3830 | | - "apndvfs", /* zName */ |
| 3831 | | - 0, /* pAppData (set when registered) */ |
| 3832 | | - apndOpen, /* xOpen */ |
| 3833 | | - apndDelete, /* xDelete */ |
| 3834 | | - apndAccess, /* xAccess */ |
| 3835 | | - apndFullPathname, /* xFullPathname */ |
| 3836 | | - apndDlOpen, /* xDlOpen */ |
| 3837 | | - apndDlError, /* xDlError */ |
| 3838 | | - apndDlSym, /* xDlSym */ |
| 3839 | | - apndDlClose, /* xDlClose */ |
| 3840 | | - apndRandomness, /* xRandomness */ |
| 3841 | | - apndSleep, /* xSleep */ |
| 3842 | | - apndCurrentTime, /* xCurrentTime */ |
| 3843 | | - apndGetLastError, /* xGetLastError */ |
| 3844 | | - apndCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 3845 | | - apndSetSystemCall, /* xSetSystemCall */ |
| 3846 | | - apndGetSystemCall, /* xGetSystemCall */ |
| 3847 | | - apndNextSystemCall /* xNextSystemCall */ |
| 3848 | | -}; |
| 3849 | | - |
| 3850 | | -static const sqlite3_io_methods apnd_io_methods = { |
| 3851 | | - 3, /* iVersion */ |
| 3852 | | - apndClose, /* xClose */ |
| 3853 | | - apndRead, /* xRead */ |
| 3854 | | - apndWrite, /* xWrite */ |
| 3855 | | - apndTruncate, /* xTruncate */ |
| 3856 | | - apndSync, /* xSync */ |
| 3857 | | - apndFileSize, /* xFileSize */ |
| 3858 | | - apndLock, /* xLock */ |
| 3859 | | - apndUnlock, /* xUnlock */ |
| 3860 | | - apndCheckReservedLock, /* xCheckReservedLock */ |
| 3861 | | - apndFileControl, /* xFileControl */ |
| 3862 | | - apndSectorSize, /* xSectorSize */ |
| 3863 | | - apndDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 3864 | | - apndShmMap, /* xShmMap */ |
| 3865 | | - apndShmLock, /* xShmLock */ |
| 3866 | | - apndShmBarrier, /* xShmBarrier */ |
| 3867 | | - apndShmUnmap, /* xShmUnmap */ |
| 3868 | | - apndFetch, /* xFetch */ |
| 3869 | | - apndUnfetch /* xUnfetch */ |
| 3870 | | -}; |
| 3871 | | - |
| 3872 | | -/* |
| 3873 | | -** Close an apnd-file. |
| 3874 | | -*/ |
| 3875 | | -static int apndClose(sqlite3_file *pFile){ |
| 3876 | | - pFile = ORIGFILE(pFile); |
| 3877 | | - return pFile->pMethods->xClose(pFile); |
| 3878 | | -} |
| 3879 | | - |
| 3880 | | -/* |
| 3881 | | -** Read data from an apnd-file. |
| 3882 | | -*/ |
| 3883 | | -static int apndRead( |
| 3884 | | - sqlite3_file *pFile, |
| 3885 | | - void *zBuf, |
| 3886 | | - int iAmt, |
| 3887 | | - sqlite_int64 iOfst |
| 3888 | | -){ |
| 3889 | | - ApndFile *paf = (ApndFile *)pFile; |
| 3890 | | - pFile = ORIGFILE(pFile); |
| 3891 | | - return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst); |
| 3892 | | -} |
| 3893 | | - |
| 3894 | | -/* |
| 3895 | | -** Add the append-mark onto what should become the end of the file. |
| 3896 | | -* If and only if this succeeds, internal ApndFile.iMark is updated. |
| 3897 | | -* Parameter iWriteEnd is the appendvfs-relative offset of the new mark. |
| 3898 | | -*/ |
| 3899 | | -static int apndWriteMark( |
| 3900 | | - ApndFile *paf, |
| 3901 | | - sqlite3_file *pFile, |
| 3902 | | - sqlite_int64 iWriteEnd |
| 3903 | | -){ |
| 3904 | | - sqlite_int64 iPgOne = paf->iPgOne; |
| 3905 | | - unsigned char a[APND_MARK_SIZE]; |
| 3906 | | - int i = APND_MARK_FOS_SZ; |
| 3907 | | - int rc; |
| 3908 | | - assert(pFile == ORIGFILE(paf)); |
| 3909 | | - memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ); |
| 3910 | | - while( --i >= 0 ){ |
| 3911 | | - a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff); |
| 3912 | | - iPgOne >>= 8; |
| 3913 | | - } |
| 3914 | | - iWriteEnd += paf->iPgOne; |
| 3915 | | - if( SQLITE_OK==(rc = pFile->pMethods->xWrite |
| 3916 | | - (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){ |
| 3917 | | - paf->iMark = iWriteEnd; |
| 3918 | | - } |
| 3919 | | - return rc; |
| 3920 | | -} |
| 3921 | | - |
| 3922 | | -/* |
| 3923 | | -** Write data to an apnd-file. |
| 3924 | | -*/ |
| 3925 | | -static int apndWrite( |
| 3926 | | - sqlite3_file *pFile, |
| 3927 | | - const void *zBuf, |
| 3928 | | - int iAmt, |
| 3929 | | - sqlite_int64 iOfst |
| 3930 | | -){ |
| 3931 | | - ApndFile *paf = (ApndFile *)pFile; |
| 3932 | | - sqlite_int64 iWriteEnd = iOfst + iAmt; |
| 3933 | | - if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL; |
| 3934 | | - pFile = ORIGFILE(pFile); |
| 3935 | | - /* If append-mark is absent or will be overwritten, write it. */ |
| 3936 | | - if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){ |
| 3937 | | - int rc = apndWriteMark(paf, pFile, iWriteEnd); |
| 3938 | | - if( SQLITE_OK!=rc ) return rc; |
| 3939 | | - } |
| 3940 | | - return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst); |
| 3941 | | -} |
| 3942 | | - |
| 3943 | | -/* |
| 3944 | | -** Truncate an apnd-file. |
| 3945 | | -*/ |
| 3946 | | -static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
| 3947 | | - ApndFile *paf = (ApndFile *)pFile; |
| 3948 | | - pFile = ORIGFILE(pFile); |
| 3949 | | - /* The append mark goes out first so truncate failure does not lose it. */ |
| 3950 | | - if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR; |
| 3951 | | - /* Truncate underlying file just past append mark */ |
| 3952 | | - return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE); |
| 3953 | | -} |
| 3954 | | - |
| 3955 | | -/* |
| 3956 | | -** Sync an apnd-file. |
| 3957 | | -*/ |
| 3958 | | -static int apndSync(sqlite3_file *pFile, int flags){ |
| 3959 | | - pFile = ORIGFILE(pFile); |
| 3960 | | - return pFile->pMethods->xSync(pFile, flags); |
| 3961 | | -} |
| 3962 | | - |
| 3963 | | -/* |
| 3964 | | -** Return the current file-size of an apnd-file. |
| 3965 | | -** If the append mark is not yet there, the file-size is 0. |
| 3966 | | -*/ |
| 3967 | | -static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
| 3968 | | - ApndFile *paf = (ApndFile *)pFile; |
| 3969 | | - *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0; |
| 3970 | | - return SQLITE_OK; |
| 3971 | | -} |
| 3972 | | - |
| 3973 | | -/* |
| 3974 | | -** Lock an apnd-file. |
| 3975 | | -*/ |
| 3976 | | -static int apndLock(sqlite3_file *pFile, int eLock){ |
| 3977 | | - pFile = ORIGFILE(pFile); |
| 3978 | | - return pFile->pMethods->xLock(pFile, eLock); |
| 3979 | | -} |
| 3980 | | - |
| 3981 | | -/* |
| 3982 | | -** Unlock an apnd-file. |
| 3983 | | -*/ |
| 3984 | | -static int apndUnlock(sqlite3_file *pFile, int eLock){ |
| 3985 | | - pFile = ORIGFILE(pFile); |
| 3986 | | - return pFile->pMethods->xUnlock(pFile, eLock); |
| 3987 | | -} |
| 3988 | | - |
| 3989 | | -/* |
| 3990 | | -** Check if another file-handle holds a RESERVED lock on an apnd-file. |
| 3991 | | -*/ |
| 3992 | | -static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 3993 | | - pFile = ORIGFILE(pFile); |
| 3994 | | - return pFile->pMethods->xCheckReservedLock(pFile, pResOut); |
| 3995 | | -} |
| 3996 | | - |
| 3997 | | -/* |
| 3998 | | -** File control method. For custom operations on an apnd-file. |
| 3999 | | -*/ |
| 4000 | | -static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 4001 | | - ApndFile *paf = (ApndFile *)pFile; |
| 4002 | | - int rc; |
| 4003 | | - pFile = ORIGFILE(pFile); |
| 4004 | | - if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne; |
| 4005 | | - rc = pFile->pMethods->xFileControl(pFile, op, pArg); |
| 4006 | | - if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){ |
| 4007 | | - *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg); |
| 4008 | | - } |
| 4009 | | - return rc; |
| 4010 | | -} |
| 4011 | | - |
| 4012 | | -/* |
| 4013 | | -** Return the sector-size in bytes for an apnd-file. |
| 4014 | | -*/ |
| 4015 | | -static int apndSectorSize(sqlite3_file *pFile){ |
| 4016 | | - pFile = ORIGFILE(pFile); |
| 4017 | | - return pFile->pMethods->xSectorSize(pFile); |
| 4018 | | -} |
| 4019 | | - |
| 4020 | | -/* |
| 4021 | | -** Return the device characteristic flags supported by an apnd-file. |
| 4022 | | -*/ |
| 4023 | | -static int apndDeviceCharacteristics(sqlite3_file *pFile){ |
| 4024 | | - pFile = ORIGFILE(pFile); |
| 4025 | | - return pFile->pMethods->xDeviceCharacteristics(pFile); |
| 4026 | | -} |
| 4027 | | - |
| 4028 | | -/* Create a shared memory file mapping */ |
| 4029 | | -static int apndShmMap( |
| 4030 | | - sqlite3_file *pFile, |
| 4031 | | - int iPg, |
| 4032 | | - int pgsz, |
| 4033 | | - int bExtend, |
| 4034 | | - void volatile **pp |
| 4035 | | -){ |
| 4036 | | - pFile = ORIGFILE(pFile); |
| 4037 | | - return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); |
| 4038 | | -} |
| 4039 | | - |
| 4040 | | -/* Perform locking on a shared-memory segment */ |
| 4041 | | -static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){ |
| 4042 | | - pFile = ORIGFILE(pFile); |
| 4043 | | - return pFile->pMethods->xShmLock(pFile,offset,n,flags); |
| 4044 | | -} |
| 4045 | | - |
| 4046 | | -/* Memory barrier operation on shared memory */ |
| 4047 | | -static void apndShmBarrier(sqlite3_file *pFile){ |
| 4048 | | - pFile = ORIGFILE(pFile); |
| 4049 | | - pFile->pMethods->xShmBarrier(pFile); |
| 4050 | | -} |
| 4051 | | - |
| 4052 | | -/* Unmap a shared memory segment */ |
| 4053 | | -static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){ |
| 4054 | | - pFile = ORIGFILE(pFile); |
| 4055 | | - return pFile->pMethods->xShmUnmap(pFile,deleteFlag); |
| 4056 | | -} |
| 4057 | | - |
| 4058 | | -/* Fetch a page of a memory-mapped file */ |
| 4059 | | -static int apndFetch( |
| 4060 | | - sqlite3_file *pFile, |
| 4061 | | - sqlite3_int64 iOfst, |
| 4062 | | - int iAmt, |
| 4063 | | - void **pp |
| 4064 | | -){ |
| 4065 | | - ApndFile *p = (ApndFile *)pFile; |
| 4066 | | - if( p->iMark < 0 || iOfst+iAmt > p->iMark ){ |
| 4067 | | - return SQLITE_IOERR; /* Cannot read what is not yet there. */ |
| 4068 | | - } |
| 4069 | | - pFile = ORIGFILE(pFile); |
| 4070 | | - return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp); |
| 4071 | | -} |
| 4072 | | - |
| 4073 | | -/* Release a memory-mapped page */ |
| 4074 | | -static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ |
| 4075 | | - ApndFile *p = (ApndFile *)pFile; |
| 4076 | | - pFile = ORIGFILE(pFile); |
| 4077 | | - return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage); |
| 4078 | | -} |
| 4079 | | - |
| 4080 | | -/* |
| 4081 | | -** Try to read the append-mark off the end of a file. Return the |
| 4082 | | -** start of the appended database if the append-mark is present. |
| 4083 | | -** If there is no valid append-mark, return -1; |
| 4084 | | -** |
| 4085 | | -** An append-mark is only valid if the NNNNNNNN start-of-database offset |
| 4086 | | -** indicates that the appended database contains at least one page. The |
| 4087 | | -** start-of-database value must be a multiple of 512. |
| 4088 | | -*/ |
| 4089 | | -static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){ |
| 4090 | | - int rc, i; |
| 4091 | | - sqlite3_int64 iMark; |
| 4092 | | - int msbs = 8 * (APND_MARK_FOS_SZ-1); |
| 4093 | | - unsigned char a[APND_MARK_SIZE]; |
| 4094 | | - |
| 4095 | | - if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1; |
| 4096 | | - rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE); |
| 4097 | | - if( rc ) return -1; |
| 4098 | | - if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1; |
| 4099 | | - iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs; |
| 4100 | | - for(i=1; i<8; i++){ |
| 4101 | | - msbs -= 8; |
| 4102 | | - iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs; |
| 4103 | | - } |
| 4104 | | - if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1; |
| 4105 | | - if( iMark & 0x1ff ) return -1; |
| 4106 | | - return iMark; |
| 4107 | | -} |
| 4108 | | - |
| 4109 | | -static const char apvfsSqliteHdr[] = "SQLite format 3"; |
| 4110 | | -/* |
| 4111 | | -** Check to see if the file is an appendvfs SQLite database file. |
| 4112 | | -** Return true iff it is such. Parameter sz is the file's size. |
| 4113 | | -*/ |
| 4114 | | -static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){ |
| 4115 | | - int rc; |
| 4116 | | - char zHdr[16]; |
| 4117 | | - sqlite3_int64 iMark = apndReadMark(sz, pFile); |
| 4118 | | - if( iMark>=0 ){ |
| 4119 | | - /* If file has the correct end-marker, the expected odd size, and the |
| 4120 | | - ** SQLite DB type marker where the end-marker puts it, then it |
| 4121 | | - ** is an appendvfs database. |
| 4122 | | - */ |
| 4123 | | - rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark); |
| 4124 | | - if( SQLITE_OK==rc |
| 4125 | | - && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0 |
| 4126 | | - && (sz & 0x1ff) == APND_MARK_SIZE |
| 4127 | | - && sz>=512+APND_MARK_SIZE |
| 4128 | | - ){ |
| 4129 | | - return 1; /* It's an appendvfs database */ |
| 4130 | | - } |
| 4131 | | - } |
| 4132 | | - return 0; |
| 4133 | | -} |
| 4134 | | - |
| 4135 | | -/* |
| 4136 | | -** Check to see if the file is an ordinary SQLite database file. |
| 4137 | | -** Return true iff so. Parameter sz is the file's size. |
| 4138 | | -*/ |
| 4139 | | -static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){ |
| 4140 | | - char zHdr[16]; |
| 4141 | | - if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */ |
| 4142 | | - || (sz & 0x1ff) != 0 |
| 4143 | | - || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0) |
| 4144 | | - || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0 |
| 4145 | | - ){ |
| 4146 | | - return 0; |
| 4147 | | - }else{ |
| 4148 | | - return 1; |
| 4149 | | - } |
| 4150 | | -} |
| 4151 | | - |
| 4152 | | -/* |
| 4153 | | -** Open an apnd file handle. |
| 4154 | | -*/ |
| 4155 | | -static int apndOpen( |
| 4156 | | - sqlite3_vfs *pApndVfs, |
| 4157 | | - const char *zName, |
| 4158 | | - sqlite3_file *pFile, |
| 4159 | | - int flags, |
| 4160 | | - int *pOutFlags |
| 4161 | | -){ |
| 4162 | | - ApndFile *pApndFile = (ApndFile*)pFile; |
| 4163 | | - sqlite3_file *pBaseFile = ORIGFILE(pFile); |
| 4164 | | - sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs); |
| 4165 | | - int rc; |
| 4166 | | - sqlite3_int64 sz = 0; |
| 4167 | | - if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ |
| 4168 | | - /* The appendvfs is not to be used for transient or temporary databases. |
| 4169 | | - ** Just use the base VFS open to initialize the given file object and |
| 4170 | | - ** open the underlying file. (Appendvfs is then unused for this file.) |
| 4171 | | - */ |
| 4172 | | - return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags); |
| 4173 | | - } |
| 4174 | | - memset(pApndFile, 0, sizeof(ApndFile)); |
| 4175 | | - pFile->pMethods = &apnd_io_methods; |
| 4176 | | - pApndFile->iMark = -1; /* Append mark not yet written */ |
| 4177 | | - |
| 4178 | | - rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags); |
| 4179 | | - if( rc==SQLITE_OK ){ |
| 4180 | | - rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz); |
| 4181 | | - if( rc ){ |
| 4182 | | - pBaseFile->pMethods->xClose(pBaseFile); |
| 4183 | | - } |
| 4184 | | - } |
| 4185 | | - if( rc ){ |
| 4186 | | - pFile->pMethods = 0; |
| 4187 | | - return rc; |
| 4188 | | - } |
| 4189 | | - if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){ |
| 4190 | | - /* The file being opened appears to be just an ordinary DB. Copy |
| 4191 | | - ** the base dispatch-table so this instance mimics the base VFS. |
| 4192 | | - */ |
| 4193 | | - memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile); |
| 4194 | | - return SQLITE_OK; |
| 4195 | | - } |
| 4196 | | - pApndFile->iPgOne = apndReadMark(sz, pFile); |
| 4197 | | - if( pApndFile->iPgOne>=0 ){ |
| 4198 | | - pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */ |
| 4199 | | - return SQLITE_OK; |
| 4200 | | - } |
| 4201 | | - if( (flags & SQLITE_OPEN_CREATE)==0 ){ |
| 4202 | | - pBaseFile->pMethods->xClose(pBaseFile); |
| 4203 | | - rc = SQLITE_CANTOPEN; |
| 4204 | | - pFile->pMethods = 0; |
| 4205 | | - }else{ |
| 4206 | | - /* Round newly added appendvfs location to #define'd page boundary. |
| 4207 | | - ** Note that nothing has yet been written to the underlying file. |
| 4208 | | - ** The append mark will be written along with first content write. |
| 4209 | | - ** Until then, paf->iMark value indicates it is not yet written. |
| 4210 | | - */ |
| 4211 | | - pApndFile->iPgOne = APND_START_ROUNDUP(sz); |
| 4212 | | - } |
| 4213 | | - return rc; |
| 4214 | | -} |
| 4215 | | - |
| 4216 | | -/* |
| 4217 | | -** Delete an apnd file. |
| 4218 | | -** For an appendvfs, this could mean delete the appendvfs portion, |
| 4219 | | -** leaving the appendee as it was before it gained an appendvfs. |
| 4220 | | -** For now, this code deletes the underlying file too. |
| 4221 | | -*/ |
| 4222 | | -static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 4223 | | - return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); |
| 4224 | | -} |
| 4225 | | - |
| 4226 | | -/* |
| 4227 | | -** All other VFS methods are pass-thrus. |
| 4228 | | -*/ |
| 4229 | | -static int apndAccess( |
| 4230 | | - sqlite3_vfs *pVfs, |
| 4231 | | - const char *zPath, |
| 4232 | | - int flags, |
| 4233 | | - int *pResOut |
| 4234 | | -){ |
| 4235 | | - return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); |
| 4236 | | -} |
| 4237 | | -static int apndFullPathname( |
| 4238 | | - sqlite3_vfs *pVfs, |
| 4239 | | - const char *zPath, |
| 4240 | | - int nOut, |
| 4241 | | - char *zOut |
| 4242 | | -){ |
| 4243 | | - return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); |
| 4244 | | -} |
| 4245 | | -static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 4246 | | - return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); |
| 4247 | | -} |
| 4248 | | -static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ |
| 4249 | | - ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); |
| 4250 | | -} |
| 4251 | | -static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ |
| 4252 | | - return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); |
| 4253 | | -} |
| 4254 | | -static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 4255 | | - ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); |
| 4256 | | -} |
| 4257 | | -static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 4258 | | - return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); |
| 4259 | | -} |
| 4260 | | -static int apndSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 4261 | | - return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); |
| 4262 | | -} |
| 4263 | | -static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 4264 | | - return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); |
| 4265 | | -} |
| 4266 | | -static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){ |
| 4267 | | - return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); |
| 4268 | | -} |
| 4269 | | -static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ |
| 4270 | | - return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); |
| 4271 | | -} |
| 4272 | | -static int apndSetSystemCall( |
| 4273 | | - sqlite3_vfs *pVfs, |
| 4274 | | - const char *zName, |
| 4275 | | - sqlite3_syscall_ptr pCall |
| 4276 | | -){ |
| 4277 | | - return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); |
| 4278 | | -} |
| 4279 | | -static sqlite3_syscall_ptr apndGetSystemCall( |
| 4280 | | - sqlite3_vfs *pVfs, |
| 4281 | | - const char *zName |
| 4282 | | -){ |
| 4283 | | - return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); |
| 4284 | | -} |
| 4285 | | -static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){ |
| 4286 | | - return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); |
| 4287 | | -} |
| 4288 | | - |
| 4289 | | - |
| 4290 | | -#ifdef _WIN32 |
| 4291 | | - |
| 4292 | | -#endif |
| 4293 | | -/* |
| 4294 | | -** This routine is called when the extension is loaded. |
| 4295 | | -** Register the new VFS. |
| 4296 | | -*/ |
| 4297 | | -int sqlite3_appendvfs_init( |
| 4298 | | - sqlite3 *db, |
| 4299 | | - char **pzErrMsg, |
| 4300 | | - const sqlite3_api_routines *pApi |
| 4301 | | -){ |
| 4302 | | - int rc = SQLITE_OK; |
| 4303 | | - sqlite3_vfs *pOrig; |
| 4304 | | - SQLITE_EXTENSION_INIT2(pApi); |
| 4305 | | - (void)pzErrMsg; |
| 4306 | | - (void)db; |
| 4307 | | - pOrig = sqlite3_vfs_find(0); |
| 4308 | | - if( pOrig==0 ) return SQLITE_ERROR; |
| 4309 | | - apnd_vfs.iVersion = pOrig->iVersion; |
| 4310 | | - apnd_vfs.pAppData = pOrig; |
| 4311 | | - apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile); |
| 4312 | | - rc = sqlite3_vfs_register(&apnd_vfs, 0); |
| 4313 | | -#ifdef APPENDVFS_TEST |
| 4314 | | - if( rc==SQLITE_OK ){ |
| 4315 | | - rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister); |
| 4316 | | - } |
| 4317 | | -#endif |
| 4318 | | - if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY; |
| 4319 | | - return rc; |
| 4320 | | -} |
| 4321 | | - |
| 4322 | | -/************************* End ../ext/misc/appendvfs.c ********************/ |
| 4323 | | -/************************* Begin ../ext/misc/memtrace.c ******************/ |
| 4324 | | -/* |
| 4325 | | -** 2019-01-21 |
| 4326 | | -** |
| 4327 | | -** The author disclaims copyright to this source code. In place of |
| 4328 | | -** a legal notice, here is a blessing: |
| 4329 | | -** |
| 4330 | | -** May you do good and not evil. |
| 4331 | | -** May you find forgiveness for yourself and forgive others. |
| 4332 | | -** May you share freely, never taking more than you give. |
| 4333 | | -** |
| 4334 | | -************************************************************************* |
| 4335 | | -** |
| 4336 | | -** This file implements an extension that uses the SQLITE_CONFIG_MALLOC |
| 4337 | | -** mechanism to add a tracing layer on top of SQLite. If this extension |
| 4338 | | -** is registered prior to sqlite3_initialize(), it will cause all memory |
| 4339 | | -** allocation activities to be logged on standard output, or to some other |
| 4340 | | -** FILE specified by the initializer. |
| 4341 | | -** |
| 4342 | | -** This file needs to be compiled into the application that uses it. |
| 4343 | | -** |
| 4344 | | -** This extension is used to implement the --memtrace option of the |
| 4345 | | -** command-line shell. |
| 4346 | | -*/ |
| 4347 | | -#include <assert.h> |
| 4348 | | -#include <string.h> |
| 4349 | | -#include <stdio.h> |
| 4350 | | - |
| 4351 | | -/* The original memory allocation routines */ |
| 4352 | | -static sqlite3_mem_methods memtraceBase; |
| 4353 | | -static FILE *memtraceOut; |
| 4354 | | - |
| 4355 | | -/* Methods that trace memory allocations */ |
| 4356 | | -static void *memtraceMalloc(int n){ |
| 4357 | | - if( memtraceOut ){ |
| 4358 | | - fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", |
| 4359 | | - memtraceBase.xRoundup(n)); |
| 4360 | | - } |
| 4361 | | - return memtraceBase.xMalloc(n); |
| 4362 | | -} |
| 4363 | | -static void memtraceFree(void *p){ |
| 4364 | | - if( p==0 ) return; |
| 4365 | | - if( memtraceOut ){ |
| 4366 | | - fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p)); |
| 4367 | | - } |
| 4368 | | - memtraceBase.xFree(p); |
| 4369 | | -} |
| 4370 | | -static void *memtraceRealloc(void *p, int n){ |
| 4371 | | - if( p==0 ) return memtraceMalloc(n); |
| 4372 | | - if( n==0 ){ |
| 4373 | | - memtraceFree(p); |
| 4374 | | - return 0; |
| 4375 | | - } |
| 4376 | | - if( memtraceOut ){ |
| 4377 | | - fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", |
| 4378 | | - memtraceBase.xSize(p), memtraceBase.xRoundup(n)); |
| 4379 | | - } |
| 4380 | | - return memtraceBase.xRealloc(p, n); |
| 4381 | | -} |
| 4382 | | -static int memtraceSize(void *p){ |
| 4383 | | - return memtraceBase.xSize(p); |
| 4384 | | -} |
| 4385 | | -static int memtraceRoundup(int n){ |
| 4386 | | - return memtraceBase.xRoundup(n); |
| 4387 | | -} |
| 4388 | | -static int memtraceInit(void *p){ |
| 4389 | | - return memtraceBase.xInit(p); |
| 4390 | | -} |
| 4391 | | -static void memtraceShutdown(void *p){ |
| 4392 | | - memtraceBase.xShutdown(p); |
| 4393 | | -} |
| 4394 | | - |
| 4395 | | -/* The substitute memory allocator */ |
| 4396 | | -static sqlite3_mem_methods ersaztMethods = { |
| 4397 | | - memtraceMalloc, |
| 4398 | | - memtraceFree, |
| 4399 | | - memtraceRealloc, |
| 4400 | | - memtraceSize, |
| 4401 | | - memtraceRoundup, |
| 4402 | | - memtraceInit, |
| 4403 | | - memtraceShutdown, |
| 4404 | | - 0 |
| 4405 | | -}; |
| 4406 | | - |
| 4407 | | -/* Begin tracing memory allocations to out. */ |
| 4408 | | -int sqlite3MemTraceActivate(FILE *out){ |
| 4409 | | - int rc = SQLITE_OK; |
| 4410 | | - if( memtraceBase.xMalloc==0 ){ |
| 4411 | | - rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase); |
| 4412 | | - if( rc==SQLITE_OK ){ |
| 4413 | | - rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods); |
| 4414 | | - } |
| 4415 | | - } |
| 4416 | | - memtraceOut = out; |
| 4417 | | - return rc; |
| 4418 | | -} |
| 4419 | | - |
| 4420 | | -/* Deactivate memory tracing */ |
| 4421 | | -int sqlite3MemTraceDeactivate(void){ |
| 4422 | | - int rc = SQLITE_OK; |
| 4423 | | - if( memtraceBase.xMalloc!=0 ){ |
| 4424 | | - rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase); |
| 4425 | | - if( rc==SQLITE_OK ){ |
| 4426 | | - memset(&memtraceBase, 0, sizeof(memtraceBase)); |
| 4427 | | - } |
| 4428 | | - } |
| 4429 | | - memtraceOut = 0; |
| 4430 | | - return rc; |
| 4431 | | -} |
| 4432 | | - |
| 4433 | | -/************************* End ../ext/misc/memtrace.c ********************/ |
| 4434 | 2233 | /************************* Begin ../ext/misc/uint.c ******************/ |
| 4435 | 2234 | /* |
| 4436 | 2235 | ** 2020-04-14 |
| 4437 | 2236 | ** |
| 4438 | 2237 | ** The author disclaims copyright to this source code. In place of |
| | @@ -6696,10 +4495,2224 @@ |
| 6696 | 4495 | } |
| 6697 | 4496 | return rc; |
| 6698 | 4497 | } |
| 6699 | 4498 | |
| 6700 | 4499 | /************************* End ../ext/misc/regexp.c ********************/ |
| 4500 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 4501 | +/************************* Begin ../ext/misc/fileio.c ******************/ |
| 4502 | +/* |
| 4503 | +** 2014-06-13 |
| 4504 | +** |
| 4505 | +** The author disclaims copyright to this source code. In place of |
| 4506 | +** a legal notice, here is a blessing: |
| 4507 | +** |
| 4508 | +** May you do good and not evil. |
| 4509 | +** May you find forgiveness for yourself and forgive others. |
| 4510 | +** May you share freely, never taking more than you give. |
| 4511 | +** |
| 4512 | +****************************************************************************** |
| 4513 | +** |
| 4514 | +** This SQLite extension implements SQL functions readfile() and |
| 4515 | +** writefile(), and eponymous virtual type "fsdir". |
| 4516 | +** |
| 4517 | +** WRITEFILE(FILE, DATA [, MODE [, MTIME]]): |
| 4518 | +** |
| 4519 | +** If neither of the optional arguments is present, then this UDF |
| 4520 | +** function writes blob DATA to file FILE. If successful, the number |
| 4521 | +** of bytes written is returned. If an error occurs, NULL is returned. |
| 4522 | +** |
| 4523 | +** If the first option argument - MODE - is present, then it must |
| 4524 | +** be passed an integer value that corresponds to a POSIX mode |
| 4525 | +** value (file type + permissions, as returned in the stat.st_mode |
| 4526 | +** field by the stat() system call). Three types of files may |
| 4527 | +** be written/created: |
| 4528 | +** |
| 4529 | +** regular files: (mode & 0170000)==0100000 |
| 4530 | +** symbolic links: (mode & 0170000)==0120000 |
| 4531 | +** directories: (mode & 0170000)==0040000 |
| 4532 | +** |
| 4533 | +** For a directory, the DATA is ignored. For a symbolic link, it is |
| 4534 | +** interpreted as text and used as the target of the link. For a |
| 4535 | +** regular file, it is interpreted as a blob and written into the |
| 4536 | +** named file. Regardless of the type of file, its permissions are |
| 4537 | +** set to (mode & 0777) before returning. |
| 4538 | +** |
| 4539 | +** If the optional MTIME argument is present, then it is interpreted |
| 4540 | +** as an integer - the number of seconds since the unix epoch. The |
| 4541 | +** modification-time of the target file is set to this value before |
| 4542 | +** returning. |
| 4543 | +** |
| 4544 | +** If three or more arguments are passed to this function and an |
| 4545 | +** error is encountered, an exception is raised. |
| 4546 | +** |
| 4547 | +** READFILE(FILE): |
| 4548 | +** |
| 4549 | +** Read and return the contents of file FILE (type blob) from disk. |
| 4550 | +** |
| 4551 | +** FSDIR: |
| 4552 | +** |
| 4553 | +** Used as follows: |
| 4554 | +** |
| 4555 | +** SELECT * FROM fsdir($path [, $dir]); |
| 4556 | +** |
| 4557 | +** Parameter $path is an absolute or relative pathname. If the file that it |
| 4558 | +** refers to does not exist, it is an error. If the path refers to a regular |
| 4559 | +** file or symbolic link, it returns a single row. Or, if the path refers |
| 4560 | +** to a directory, it returns one row for the directory, and one row for each |
| 4561 | +** file within the hierarchy rooted at $path. |
| 4562 | +** |
| 4563 | +** Each row has the following columns: |
| 4564 | +** |
| 4565 | +** name: Path to file or directory (text value). |
| 4566 | +** mode: Value of stat.st_mode for directory entry (an integer). |
| 4567 | +** mtime: Value of stat.st_mtime for directory entry (an integer). |
| 4568 | +** data: For a regular file, a blob containing the file data. For a |
| 4569 | +** symlink, a text value containing the text of the link. For a |
| 4570 | +** directory, NULL. |
| 4571 | +** |
| 4572 | +** If a non-NULL value is specified for the optional $dir parameter and |
| 4573 | +** $path is a relative path, then $path is interpreted relative to $dir. |
| 4574 | +** And the paths returned in the "name" column of the table are also |
| 4575 | +** relative to directory $dir. |
| 4576 | +** |
| 4577 | +** Notes on building this extension for Windows: |
| 4578 | +** Unless linked statically with the SQLite library, a preprocessor |
| 4579 | +** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone |
| 4580 | +** DLL form of this extension for WIN32. See its use below for details. |
| 4581 | +*/ |
| 4582 | +/* #include "sqlite3ext.h" */ |
| 4583 | +SQLITE_EXTENSION_INIT1 |
| 4584 | +#include <stdio.h> |
| 4585 | +#include <string.h> |
| 4586 | +#include <assert.h> |
| 4587 | + |
| 4588 | +#include <sys/types.h> |
| 4589 | +#include <sys/stat.h> |
| 4590 | +#include <fcntl.h> |
| 4591 | +#if !defined(_WIN32) && !defined(WIN32) |
| 4592 | +# include <unistd.h> |
| 4593 | +# include <dirent.h> |
| 4594 | +# include <utime.h> |
| 4595 | +# include <sys/time.h> |
| 4596 | +#else |
| 4597 | +# include "windows.h" |
| 4598 | +# include <io.h> |
| 4599 | +# include <direct.h> |
| 4600 | +/* # include "test_windirent.h" */ |
| 4601 | +# define dirent DIRENT |
| 4602 | +# ifndef chmod |
| 4603 | +# define chmod _chmod |
| 4604 | +# endif |
| 4605 | +# ifndef stat |
| 4606 | +# define stat _stat |
| 4607 | +# endif |
| 4608 | +# define mkdir(path,mode) _mkdir(path) |
| 4609 | +# define lstat(path,buf) stat(path,buf) |
| 4610 | +#endif |
| 4611 | +#include <time.h> |
| 4612 | +#include <errno.h> |
| 4613 | + |
| 4614 | + |
| 4615 | +/* |
| 4616 | +** Structure of the fsdir() table-valued function |
| 4617 | +*/ |
| 4618 | + /* 0 1 2 3 4 5 */ |
| 4619 | +#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)" |
| 4620 | +#define FSDIR_COLUMN_NAME 0 /* Name of the file */ |
| 4621 | +#define FSDIR_COLUMN_MODE 1 /* Access mode */ |
| 4622 | +#define FSDIR_COLUMN_MTIME 2 /* Last modification time */ |
| 4623 | +#define FSDIR_COLUMN_DATA 3 /* File content */ |
| 4624 | +#define FSDIR_COLUMN_PATH 4 /* Path to top of search */ |
| 4625 | +#define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */ |
| 4626 | + |
| 4627 | + |
| 4628 | +/* |
| 4629 | +** Set the result stored by context ctx to a blob containing the |
| 4630 | +** contents of file zName. Or, leave the result unchanged (NULL) |
| 4631 | +** if the file does not exist or is unreadable. |
| 4632 | +** |
| 4633 | +** If the file exceeds the SQLite blob size limit, through an |
| 4634 | +** SQLITE_TOOBIG error. |
| 4635 | +** |
| 4636 | +** Throw an SQLITE_IOERR if there are difficulties pulling the file |
| 4637 | +** off of disk. |
| 4638 | +*/ |
| 4639 | +static void readFileContents(sqlite3_context *ctx, const char *zName){ |
| 4640 | + FILE *in; |
| 4641 | + sqlite3_int64 nIn; |
| 4642 | + void *pBuf; |
| 4643 | + sqlite3 *db; |
| 4644 | + int mxBlob; |
| 4645 | + |
| 4646 | + in = fopen(zName, "rb"); |
| 4647 | + if( in==0 ){ |
| 4648 | + /* File does not exist or is unreadable. Leave the result set to NULL. */ |
| 4649 | + return; |
| 4650 | + } |
| 4651 | + fseek(in, 0, SEEK_END); |
| 4652 | + nIn = ftell(in); |
| 4653 | + rewind(in); |
| 4654 | + db = sqlite3_context_db_handle(ctx); |
| 4655 | + mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); |
| 4656 | + if( nIn>mxBlob ){ |
| 4657 | + sqlite3_result_error_code(ctx, SQLITE_TOOBIG); |
| 4658 | + fclose(in); |
| 4659 | + return; |
| 4660 | + } |
| 4661 | + pBuf = sqlite3_malloc64( nIn ? nIn : 1 ); |
| 4662 | + if( pBuf==0 ){ |
| 4663 | + sqlite3_result_error_nomem(ctx); |
| 4664 | + fclose(in); |
| 4665 | + return; |
| 4666 | + } |
| 4667 | + if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){ |
| 4668 | + sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free); |
| 4669 | + }else{ |
| 4670 | + sqlite3_result_error_code(ctx, SQLITE_IOERR); |
| 4671 | + sqlite3_free(pBuf); |
| 4672 | + } |
| 4673 | + fclose(in); |
| 4674 | +} |
| 4675 | + |
| 4676 | +/* |
| 4677 | +** Implementation of the "readfile(X)" SQL function. The entire content |
| 4678 | +** of the file named X is read and returned as a BLOB. NULL is returned |
| 4679 | +** if the file does not exist or is unreadable. |
| 4680 | +*/ |
| 4681 | +static void readfileFunc( |
| 4682 | + sqlite3_context *context, |
| 4683 | + int argc, |
| 4684 | + sqlite3_value **argv |
| 4685 | +){ |
| 4686 | + const char *zName; |
| 4687 | + (void)(argc); /* Unused parameter */ |
| 4688 | + zName = (const char*)sqlite3_value_text(argv[0]); |
| 4689 | + if( zName==0 ) return; |
| 4690 | + readFileContents(context, zName); |
| 4691 | +} |
| 4692 | + |
| 4693 | +/* |
| 4694 | +** Set the error message contained in context ctx to the results of |
| 4695 | +** vprintf(zFmt, ...). |
| 4696 | +*/ |
| 4697 | +static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){ |
| 4698 | + char *zMsg = 0; |
| 4699 | + va_list ap; |
| 4700 | + va_start(ap, zFmt); |
| 4701 | + zMsg = sqlite3_vmprintf(zFmt, ap); |
| 4702 | + sqlite3_result_error(ctx, zMsg, -1); |
| 4703 | + sqlite3_free(zMsg); |
| 4704 | + va_end(ap); |
| 4705 | +} |
| 4706 | + |
| 4707 | +#if defined(_WIN32) |
| 4708 | +/* |
| 4709 | +** This function is designed to convert a Win32 FILETIME structure into the |
| 4710 | +** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC). |
| 4711 | +*/ |
| 4712 | +static sqlite3_uint64 fileTimeToUnixTime( |
| 4713 | + LPFILETIME pFileTime |
| 4714 | +){ |
| 4715 | + SYSTEMTIME epochSystemTime; |
| 4716 | + ULARGE_INTEGER epochIntervals; |
| 4717 | + FILETIME epochFileTime; |
| 4718 | + ULARGE_INTEGER fileIntervals; |
| 4719 | + |
| 4720 | + memset(&epochSystemTime, 0, sizeof(SYSTEMTIME)); |
| 4721 | + epochSystemTime.wYear = 1970; |
| 4722 | + epochSystemTime.wMonth = 1; |
| 4723 | + epochSystemTime.wDay = 1; |
| 4724 | + SystemTimeToFileTime(&epochSystemTime, &epochFileTime); |
| 4725 | + epochIntervals.LowPart = epochFileTime.dwLowDateTime; |
| 4726 | + epochIntervals.HighPart = epochFileTime.dwHighDateTime; |
| 4727 | + |
| 4728 | + fileIntervals.LowPart = pFileTime->dwLowDateTime; |
| 4729 | + fileIntervals.HighPart = pFileTime->dwHighDateTime; |
| 4730 | + |
| 4731 | + return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000; |
| 4732 | +} |
| 4733 | + |
| 4734 | + |
| 4735 | +#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32)) |
| 4736 | +# /* To allow a standalone DLL, use this next replacement function: */ |
| 4737 | +# undef sqlite3_win32_utf8_to_unicode |
| 4738 | +# define sqlite3_win32_utf8_to_unicode utf8_to_utf16 |
| 4739 | +# |
| 4740 | +LPWSTR utf8_to_utf16(const char *z){ |
| 4741 | + int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0); |
| 4742 | + LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR)); |
| 4743 | + if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) ) |
| 4744 | + return rv; |
| 4745 | + sqlite3_free(rv); |
| 4746 | + return 0; |
| 4747 | +} |
| 4748 | +#endif |
| 4749 | + |
| 4750 | +/* |
| 4751 | +** This function attempts to normalize the time values found in the stat() |
| 4752 | +** buffer to UTC. This is necessary on Win32, where the runtime library |
| 4753 | +** appears to return these values as local times. |
| 4754 | +*/ |
| 4755 | +static void statTimesToUtc( |
| 4756 | + const char *zPath, |
| 4757 | + struct stat *pStatBuf |
| 4758 | +){ |
| 4759 | + HANDLE hFindFile; |
| 4760 | + WIN32_FIND_DATAW fd; |
| 4761 | + LPWSTR zUnicodeName; |
| 4762 | + extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); |
| 4763 | + zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath); |
| 4764 | + if( zUnicodeName ){ |
| 4765 | + memset(&fd, 0, sizeof(WIN32_FIND_DATAW)); |
| 4766 | + hFindFile = FindFirstFileW(zUnicodeName, &fd); |
| 4767 | + if( hFindFile!=NULL ){ |
| 4768 | + pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime); |
| 4769 | + pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime); |
| 4770 | + pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime); |
| 4771 | + FindClose(hFindFile); |
| 4772 | + } |
| 4773 | + sqlite3_free(zUnicodeName); |
| 4774 | + } |
| 4775 | +} |
| 4776 | +#endif |
| 4777 | + |
| 4778 | +/* |
| 4779 | +** This function is used in place of stat(). On Windows, special handling |
| 4780 | +** is required in order for the included time to be returned as UTC. On all |
| 4781 | +** other systems, this function simply calls stat(). |
| 4782 | +*/ |
| 4783 | +static int fileStat( |
| 4784 | + const char *zPath, |
| 4785 | + struct stat *pStatBuf |
| 4786 | +){ |
| 4787 | +#if defined(_WIN32) |
| 4788 | + int rc = stat(zPath, pStatBuf); |
| 4789 | + if( rc==0 ) statTimesToUtc(zPath, pStatBuf); |
| 4790 | + return rc; |
| 4791 | +#else |
| 4792 | + return stat(zPath, pStatBuf); |
| 4793 | +#endif |
| 4794 | +} |
| 4795 | + |
| 4796 | +/* |
| 4797 | +** This function is used in place of lstat(). On Windows, special handling |
| 4798 | +** is required in order for the included time to be returned as UTC. On all |
| 4799 | +** other systems, this function simply calls lstat(). |
| 4800 | +*/ |
| 4801 | +static int fileLinkStat( |
| 4802 | + const char *zPath, |
| 4803 | + struct stat *pStatBuf |
| 4804 | +){ |
| 4805 | +#if defined(_WIN32) |
| 4806 | + int rc = lstat(zPath, pStatBuf); |
| 4807 | + if( rc==0 ) statTimesToUtc(zPath, pStatBuf); |
| 4808 | + return rc; |
| 4809 | +#else |
| 4810 | + return lstat(zPath, pStatBuf); |
| 4811 | +#endif |
| 4812 | +} |
| 4813 | + |
| 4814 | +/* |
| 4815 | +** Argument zFile is the name of a file that will be created and/or written |
| 4816 | +** by SQL function writefile(). This function ensures that the directory |
| 4817 | +** zFile will be written to exists, creating it if required. The permissions |
| 4818 | +** for any path components created by this function are set in accordance |
| 4819 | +** with the current umask. |
| 4820 | +** |
| 4821 | +** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise, |
| 4822 | +** SQLITE_OK is returned if the directory is successfully created, or |
| 4823 | +** SQLITE_ERROR otherwise. |
| 4824 | +*/ |
| 4825 | +static int makeDirectory( |
| 4826 | + const char *zFile |
| 4827 | +){ |
| 4828 | + char *zCopy = sqlite3_mprintf("%s", zFile); |
| 4829 | + int rc = SQLITE_OK; |
| 4830 | + |
| 4831 | + if( zCopy==0 ){ |
| 4832 | + rc = SQLITE_NOMEM; |
| 4833 | + }else{ |
| 4834 | + int nCopy = (int)strlen(zCopy); |
| 4835 | + int i = 1; |
| 4836 | + |
| 4837 | + while( rc==SQLITE_OK ){ |
| 4838 | + struct stat sStat; |
| 4839 | + int rc2; |
| 4840 | + |
| 4841 | + for(; zCopy[i]!='/' && i<nCopy; i++); |
| 4842 | + if( i==nCopy ) break; |
| 4843 | + zCopy[i] = '\0'; |
| 4844 | + |
| 4845 | + rc2 = fileStat(zCopy, &sStat); |
| 4846 | + if( rc2!=0 ){ |
| 4847 | + if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR; |
| 4848 | + }else{ |
| 4849 | + if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR; |
| 4850 | + } |
| 4851 | + zCopy[i] = '/'; |
| 4852 | + i++; |
| 4853 | + } |
| 4854 | + |
| 4855 | + sqlite3_free(zCopy); |
| 4856 | + } |
| 4857 | + |
| 4858 | + return rc; |
| 4859 | +} |
| 4860 | + |
| 4861 | +/* |
| 4862 | +** This function does the work for the writefile() UDF. Refer to |
| 4863 | +** header comments at the top of this file for details. |
| 4864 | +*/ |
| 4865 | +static int writeFile( |
| 4866 | + sqlite3_context *pCtx, /* Context to return bytes written in */ |
| 4867 | + const char *zFile, /* File to write */ |
| 4868 | + sqlite3_value *pData, /* Data to write */ |
| 4869 | + mode_t mode, /* MODE parameter passed to writefile() */ |
| 4870 | + sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */ |
| 4871 | +){ |
| 4872 | + if( zFile==0 ) return 1; |
| 4873 | +#if !defined(_WIN32) && !defined(WIN32) |
| 4874 | + if( S_ISLNK(mode) ){ |
| 4875 | + const char *zTo = (const char*)sqlite3_value_text(pData); |
| 4876 | + if( zTo==0 || symlink(zTo, zFile)<0 ) return 1; |
| 4877 | + }else |
| 4878 | +#endif |
| 4879 | + { |
| 4880 | + if( S_ISDIR(mode) ){ |
| 4881 | + if( mkdir(zFile, mode) ){ |
| 4882 | + /* The mkdir() call to create the directory failed. This might not |
| 4883 | + ** be an error though - if there is already a directory at the same |
| 4884 | + ** path and either the permissions already match or can be changed |
| 4885 | + ** to do so using chmod(), it is not an error. */ |
| 4886 | + struct stat sStat; |
| 4887 | + if( errno!=EEXIST |
| 4888 | + || 0!=fileStat(zFile, &sStat) |
| 4889 | + || !S_ISDIR(sStat.st_mode) |
| 4890 | + || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777)) |
| 4891 | + ){ |
| 4892 | + return 1; |
| 4893 | + } |
| 4894 | + } |
| 4895 | + }else{ |
| 4896 | + sqlite3_int64 nWrite = 0; |
| 4897 | + const char *z; |
| 4898 | + int rc = 0; |
| 4899 | + FILE *out = fopen(zFile, "wb"); |
| 4900 | + if( out==0 ) return 1; |
| 4901 | + z = (const char*)sqlite3_value_blob(pData); |
| 4902 | + if( z ){ |
| 4903 | + sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out); |
| 4904 | + nWrite = sqlite3_value_bytes(pData); |
| 4905 | + if( nWrite!=n ){ |
| 4906 | + rc = 1; |
| 4907 | + } |
| 4908 | + } |
| 4909 | + fclose(out); |
| 4910 | + if( rc==0 && mode && chmod(zFile, mode & 0777) ){ |
| 4911 | + rc = 1; |
| 4912 | + } |
| 4913 | + if( rc ) return 2; |
| 4914 | + sqlite3_result_int64(pCtx, nWrite); |
| 4915 | + } |
| 4916 | + } |
| 4917 | + |
| 4918 | + if( mtime>=0 ){ |
| 4919 | +#if defined(_WIN32) |
| 4920 | +#if !SQLITE_OS_WINRT |
| 4921 | + /* Windows */ |
| 4922 | + FILETIME lastAccess; |
| 4923 | + FILETIME lastWrite; |
| 4924 | + SYSTEMTIME currentTime; |
| 4925 | + LONGLONG intervals; |
| 4926 | + HANDLE hFile; |
| 4927 | + LPWSTR zUnicodeName; |
| 4928 | + extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); |
| 4929 | + |
| 4930 | + GetSystemTime(¤tTime); |
| 4931 | + SystemTimeToFileTime(¤tTime, &lastAccess); |
| 4932 | + intervals = Int32x32To64(mtime, 10000000) + 116444736000000000; |
| 4933 | + lastWrite.dwLowDateTime = (DWORD)intervals; |
| 4934 | + lastWrite.dwHighDateTime = intervals >> 32; |
| 4935 | + zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile); |
| 4936 | + if( zUnicodeName==0 ){ |
| 4937 | + return 1; |
| 4938 | + } |
| 4939 | + hFile = CreateFileW( |
| 4940 | + zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, |
| 4941 | + FILE_FLAG_BACKUP_SEMANTICS, NULL |
| 4942 | + ); |
| 4943 | + sqlite3_free(zUnicodeName); |
| 4944 | + if( hFile!=INVALID_HANDLE_VALUE ){ |
| 4945 | + BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite); |
| 4946 | + CloseHandle(hFile); |
| 4947 | + return !bResult; |
| 4948 | + }else{ |
| 4949 | + return 1; |
| 4950 | + } |
| 4951 | +#endif |
| 4952 | +#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */ |
| 4953 | + /* Recent unix */ |
| 4954 | + struct timespec times[2]; |
| 4955 | + times[0].tv_nsec = times[1].tv_nsec = 0; |
| 4956 | + times[0].tv_sec = time(0); |
| 4957 | + times[1].tv_sec = mtime; |
| 4958 | + if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){ |
| 4959 | + return 1; |
| 4960 | + } |
| 4961 | +#else |
| 4962 | + /* Legacy unix */ |
| 4963 | + struct timeval times[2]; |
| 4964 | + times[0].tv_usec = times[1].tv_usec = 0; |
| 4965 | + times[0].tv_sec = time(0); |
| 4966 | + times[1].tv_sec = mtime; |
| 4967 | + if( utimes(zFile, times) ){ |
| 4968 | + return 1; |
| 4969 | + } |
| 4970 | +#endif |
| 4971 | + } |
| 4972 | + |
| 4973 | + return 0; |
| 4974 | +} |
| 4975 | + |
| 4976 | +/* |
| 4977 | +** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function. |
| 4978 | +** Refer to header comments at the top of this file for details. |
| 4979 | +*/ |
| 4980 | +static void writefileFunc( |
| 4981 | + sqlite3_context *context, |
| 4982 | + int argc, |
| 4983 | + sqlite3_value **argv |
| 4984 | +){ |
| 4985 | + const char *zFile; |
| 4986 | + mode_t mode = 0; |
| 4987 | + int res; |
| 4988 | + sqlite3_int64 mtime = -1; |
| 4989 | + |
| 4990 | + if( argc<2 || argc>4 ){ |
| 4991 | + sqlite3_result_error(context, |
| 4992 | + "wrong number of arguments to function writefile()", -1 |
| 4993 | + ); |
| 4994 | + return; |
| 4995 | + } |
| 4996 | + |
| 4997 | + zFile = (const char*)sqlite3_value_text(argv[0]); |
| 4998 | + if( zFile==0 ) return; |
| 4999 | + if( argc>=3 ){ |
| 5000 | + mode = (mode_t)sqlite3_value_int(argv[2]); |
| 5001 | + } |
| 5002 | + if( argc==4 ){ |
| 5003 | + mtime = sqlite3_value_int64(argv[3]); |
| 5004 | + } |
| 5005 | + |
| 5006 | + res = writeFile(context, zFile, argv[1], mode, mtime); |
| 5007 | + if( res==1 && errno==ENOENT ){ |
| 5008 | + if( makeDirectory(zFile)==SQLITE_OK ){ |
| 5009 | + res = writeFile(context, zFile, argv[1], mode, mtime); |
| 5010 | + } |
| 5011 | + } |
| 5012 | + |
| 5013 | + if( argc>2 && res!=0 ){ |
| 5014 | + if( S_ISLNK(mode) ){ |
| 5015 | + ctxErrorMsg(context, "failed to create symlink: %s", zFile); |
| 5016 | + }else if( S_ISDIR(mode) ){ |
| 5017 | + ctxErrorMsg(context, "failed to create directory: %s", zFile); |
| 5018 | + }else{ |
| 5019 | + ctxErrorMsg(context, "failed to write file: %s", zFile); |
| 5020 | + } |
| 5021 | + } |
| 5022 | +} |
| 5023 | + |
| 5024 | +/* |
| 5025 | +** SQL function: lsmode(MODE) |
| 5026 | +** |
| 5027 | +** Given a numberic st_mode from stat(), convert it into a human-readable |
| 5028 | +** text string in the style of "ls -l". |
| 5029 | +*/ |
| 5030 | +static void lsModeFunc( |
| 5031 | + sqlite3_context *context, |
| 5032 | + int argc, |
| 5033 | + sqlite3_value **argv |
| 5034 | +){ |
| 5035 | + int i; |
| 5036 | + int iMode = sqlite3_value_int(argv[0]); |
| 5037 | + char z[16]; |
| 5038 | + (void)argc; |
| 5039 | + if( S_ISLNK(iMode) ){ |
| 5040 | + z[0] = 'l'; |
| 5041 | + }else if( S_ISREG(iMode) ){ |
| 5042 | + z[0] = '-'; |
| 5043 | + }else if( S_ISDIR(iMode) ){ |
| 5044 | + z[0] = 'd'; |
| 5045 | + }else{ |
| 5046 | + z[0] = '?'; |
| 5047 | + } |
| 5048 | + for(i=0; i<3; i++){ |
| 5049 | + int m = (iMode >> ((2-i)*3)); |
| 5050 | + char *a = &z[1 + i*3]; |
| 5051 | + a[0] = (m & 0x4) ? 'r' : '-'; |
| 5052 | + a[1] = (m & 0x2) ? 'w' : '-'; |
| 5053 | + a[2] = (m & 0x1) ? 'x' : '-'; |
| 5054 | + } |
| 5055 | + z[10] = '\0'; |
| 5056 | + sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); |
| 5057 | +} |
| 5058 | + |
| 5059 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 5060 | + |
| 5061 | +/* |
| 5062 | +** Cursor type for recursively iterating through a directory structure. |
| 5063 | +*/ |
| 5064 | +typedef struct fsdir_cursor fsdir_cursor; |
| 5065 | +typedef struct FsdirLevel FsdirLevel; |
| 5066 | + |
| 5067 | +struct FsdirLevel { |
| 5068 | + DIR *pDir; /* From opendir() */ |
| 5069 | + char *zDir; /* Name of directory (nul-terminated) */ |
| 5070 | +}; |
| 5071 | + |
| 5072 | +struct fsdir_cursor { |
| 5073 | + sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 5074 | + |
| 5075 | + int nLvl; /* Number of entries in aLvl[] array */ |
| 5076 | + int iLvl; /* Index of current entry */ |
| 5077 | + FsdirLevel *aLvl; /* Hierarchy of directories being traversed */ |
| 5078 | + |
| 5079 | + const char *zBase; |
| 5080 | + int nBase; |
| 5081 | + |
| 5082 | + struct stat sStat; /* Current lstat() results */ |
| 5083 | + char *zPath; /* Path to current entry */ |
| 5084 | + sqlite3_int64 iRowid; /* Current rowid */ |
| 5085 | +}; |
| 5086 | + |
| 5087 | +typedef struct fsdir_tab fsdir_tab; |
| 5088 | +struct fsdir_tab { |
| 5089 | + sqlite3_vtab base; /* Base class - must be first */ |
| 5090 | +}; |
| 5091 | + |
| 5092 | +/* |
| 5093 | +** Construct a new fsdir virtual table object. |
| 5094 | +*/ |
| 5095 | +static int fsdirConnect( |
| 5096 | + sqlite3 *db, |
| 5097 | + void *pAux, |
| 5098 | + int argc, const char *const*argv, |
| 5099 | + sqlite3_vtab **ppVtab, |
| 5100 | + char **pzErr |
| 5101 | +){ |
| 5102 | + fsdir_tab *pNew = 0; |
| 5103 | + int rc; |
| 5104 | + (void)pAux; |
| 5105 | + (void)argc; |
| 5106 | + (void)argv; |
| 5107 | + (void)pzErr; |
| 5108 | + rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA); |
| 5109 | + if( rc==SQLITE_OK ){ |
| 5110 | + pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) ); |
| 5111 | + if( pNew==0 ) return SQLITE_NOMEM; |
| 5112 | + memset(pNew, 0, sizeof(*pNew)); |
| 5113 | + sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); |
| 5114 | + } |
| 5115 | + *ppVtab = (sqlite3_vtab*)pNew; |
| 5116 | + return rc; |
| 5117 | +} |
| 5118 | + |
| 5119 | +/* |
| 5120 | +** This method is the destructor for fsdir vtab objects. |
| 5121 | +*/ |
| 5122 | +static int fsdirDisconnect(sqlite3_vtab *pVtab){ |
| 5123 | + sqlite3_free(pVtab); |
| 5124 | + return SQLITE_OK; |
| 5125 | +} |
| 5126 | + |
| 5127 | +/* |
| 5128 | +** Constructor for a new fsdir_cursor object. |
| 5129 | +*/ |
| 5130 | +static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 5131 | + fsdir_cursor *pCur; |
| 5132 | + (void)p; |
| 5133 | + pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 5134 | + if( pCur==0 ) return SQLITE_NOMEM; |
| 5135 | + memset(pCur, 0, sizeof(*pCur)); |
| 5136 | + pCur->iLvl = -1; |
| 5137 | + *ppCursor = &pCur->base; |
| 5138 | + return SQLITE_OK; |
| 5139 | +} |
| 5140 | + |
| 5141 | +/* |
| 5142 | +** Reset a cursor back to the state it was in when first returned |
| 5143 | +** by fsdirOpen(). |
| 5144 | +*/ |
| 5145 | +static void fsdirResetCursor(fsdir_cursor *pCur){ |
| 5146 | + int i; |
| 5147 | + for(i=0; i<=pCur->iLvl; i++){ |
| 5148 | + FsdirLevel *pLvl = &pCur->aLvl[i]; |
| 5149 | + if( pLvl->pDir ) closedir(pLvl->pDir); |
| 5150 | + sqlite3_free(pLvl->zDir); |
| 5151 | + } |
| 5152 | + sqlite3_free(pCur->zPath); |
| 5153 | + sqlite3_free(pCur->aLvl); |
| 5154 | + pCur->aLvl = 0; |
| 5155 | + pCur->zPath = 0; |
| 5156 | + pCur->zBase = 0; |
| 5157 | + pCur->nBase = 0; |
| 5158 | + pCur->nLvl = 0; |
| 5159 | + pCur->iLvl = -1; |
| 5160 | + pCur->iRowid = 1; |
| 5161 | +} |
| 5162 | + |
| 5163 | +/* |
| 5164 | +** Destructor for an fsdir_cursor. |
| 5165 | +*/ |
| 5166 | +static int fsdirClose(sqlite3_vtab_cursor *cur){ |
| 5167 | + fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 5168 | + |
| 5169 | + fsdirResetCursor(pCur); |
| 5170 | + sqlite3_free(pCur); |
| 5171 | + return SQLITE_OK; |
| 5172 | +} |
| 5173 | + |
| 5174 | +/* |
| 5175 | +** Set the error message for the virtual table associated with cursor |
| 5176 | +** pCur to the results of vprintf(zFmt, ...). |
| 5177 | +*/ |
| 5178 | +static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){ |
| 5179 | + va_list ap; |
| 5180 | + va_start(ap, zFmt); |
| 5181 | + pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); |
| 5182 | + va_end(ap); |
| 5183 | +} |
| 5184 | + |
| 5185 | + |
| 5186 | +/* |
| 5187 | +** Advance an fsdir_cursor to its next row of output. |
| 5188 | +*/ |
| 5189 | +static int fsdirNext(sqlite3_vtab_cursor *cur){ |
| 5190 | + fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 5191 | + mode_t m = pCur->sStat.st_mode; |
| 5192 | + |
| 5193 | + pCur->iRowid++; |
| 5194 | + if( S_ISDIR(m) ){ |
| 5195 | + /* Descend into this directory */ |
| 5196 | + int iNew = pCur->iLvl + 1; |
| 5197 | + FsdirLevel *pLvl; |
| 5198 | + if( iNew>=pCur->nLvl ){ |
| 5199 | + int nNew = iNew+1; |
| 5200 | + sqlite3_int64 nByte = nNew*sizeof(FsdirLevel); |
| 5201 | + FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte); |
| 5202 | + if( aNew==0 ) return SQLITE_NOMEM; |
| 5203 | + memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl)); |
| 5204 | + pCur->aLvl = aNew; |
| 5205 | + pCur->nLvl = nNew; |
| 5206 | + } |
| 5207 | + pCur->iLvl = iNew; |
| 5208 | + pLvl = &pCur->aLvl[iNew]; |
| 5209 | + |
| 5210 | + pLvl->zDir = pCur->zPath; |
| 5211 | + pCur->zPath = 0; |
| 5212 | + pLvl->pDir = opendir(pLvl->zDir); |
| 5213 | + if( pLvl->pDir==0 ){ |
| 5214 | + fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath); |
| 5215 | + return SQLITE_ERROR; |
| 5216 | + } |
| 5217 | + } |
| 5218 | + |
| 5219 | + while( pCur->iLvl>=0 ){ |
| 5220 | + FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl]; |
| 5221 | + struct dirent *pEntry = readdir(pLvl->pDir); |
| 5222 | + if( pEntry ){ |
| 5223 | + if( pEntry->d_name[0]=='.' ){ |
| 5224 | + if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue; |
| 5225 | + if( pEntry->d_name[1]=='\0' ) continue; |
| 5226 | + } |
| 5227 | + sqlite3_free(pCur->zPath); |
| 5228 | + pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name); |
| 5229 | + if( pCur->zPath==0 ) return SQLITE_NOMEM; |
| 5230 | + if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ |
| 5231 | + fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); |
| 5232 | + return SQLITE_ERROR; |
| 5233 | + } |
| 5234 | + return SQLITE_OK; |
| 5235 | + } |
| 5236 | + closedir(pLvl->pDir); |
| 5237 | + sqlite3_free(pLvl->zDir); |
| 5238 | + pLvl->pDir = 0; |
| 5239 | + pLvl->zDir = 0; |
| 5240 | + pCur->iLvl--; |
| 5241 | + } |
| 5242 | + |
| 5243 | + /* EOF */ |
| 5244 | + sqlite3_free(pCur->zPath); |
| 5245 | + pCur->zPath = 0; |
| 5246 | + return SQLITE_OK; |
| 5247 | +} |
| 5248 | + |
| 5249 | +/* |
| 5250 | +** Return values of columns for the row at which the series_cursor |
| 5251 | +** is currently pointing. |
| 5252 | +*/ |
| 5253 | +static int fsdirColumn( |
| 5254 | + sqlite3_vtab_cursor *cur, /* The cursor */ |
| 5255 | + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 5256 | + int i /* Which column to return */ |
| 5257 | +){ |
| 5258 | + fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 5259 | + switch( i ){ |
| 5260 | + case FSDIR_COLUMN_NAME: { |
| 5261 | + sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT); |
| 5262 | + break; |
| 5263 | + } |
| 5264 | + |
| 5265 | + case FSDIR_COLUMN_MODE: |
| 5266 | + sqlite3_result_int64(ctx, pCur->sStat.st_mode); |
| 5267 | + break; |
| 5268 | + |
| 5269 | + case FSDIR_COLUMN_MTIME: |
| 5270 | + sqlite3_result_int64(ctx, pCur->sStat.st_mtime); |
| 5271 | + break; |
| 5272 | + |
| 5273 | + case FSDIR_COLUMN_DATA: { |
| 5274 | + mode_t m = pCur->sStat.st_mode; |
| 5275 | + if( S_ISDIR(m) ){ |
| 5276 | + sqlite3_result_null(ctx); |
| 5277 | +#if !defined(_WIN32) && !defined(WIN32) |
| 5278 | + }else if( S_ISLNK(m) ){ |
| 5279 | + char aStatic[64]; |
| 5280 | + char *aBuf = aStatic; |
| 5281 | + sqlite3_int64 nBuf = 64; |
| 5282 | + int n; |
| 5283 | + |
| 5284 | + while( 1 ){ |
| 5285 | + n = readlink(pCur->zPath, aBuf, nBuf); |
| 5286 | + if( n<nBuf ) break; |
| 5287 | + if( aBuf!=aStatic ) sqlite3_free(aBuf); |
| 5288 | + nBuf = nBuf*2; |
| 5289 | + aBuf = sqlite3_malloc64(nBuf); |
| 5290 | + if( aBuf==0 ){ |
| 5291 | + sqlite3_result_error_nomem(ctx); |
| 5292 | + return SQLITE_NOMEM; |
| 5293 | + } |
| 5294 | + } |
| 5295 | + |
| 5296 | + sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT); |
| 5297 | + if( aBuf!=aStatic ) sqlite3_free(aBuf); |
| 5298 | +#endif |
| 5299 | + }else{ |
| 5300 | + readFileContents(ctx, pCur->zPath); |
| 5301 | + } |
| 5302 | + } |
| 5303 | + case FSDIR_COLUMN_PATH: |
| 5304 | + default: { |
| 5305 | + /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters. |
| 5306 | + ** always return their values as NULL */ |
| 5307 | + break; |
| 5308 | + } |
| 5309 | + } |
| 5310 | + return SQLITE_OK; |
| 5311 | +} |
| 5312 | + |
| 5313 | +/* |
| 5314 | +** Return the rowid for the current row. In this implementation, the |
| 5315 | +** first row returned is assigned rowid value 1, and each subsequent |
| 5316 | +** row a value 1 more than that of the previous. |
| 5317 | +*/ |
| 5318 | +static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 5319 | + fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 5320 | + *pRowid = pCur->iRowid; |
| 5321 | + return SQLITE_OK; |
| 5322 | +} |
| 5323 | + |
| 5324 | +/* |
| 5325 | +** Return TRUE if the cursor has been moved off of the last |
| 5326 | +** row of output. |
| 5327 | +*/ |
| 5328 | +static int fsdirEof(sqlite3_vtab_cursor *cur){ |
| 5329 | + fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 5330 | + return (pCur->zPath==0); |
| 5331 | +} |
| 5332 | + |
| 5333 | +/* |
| 5334 | +** xFilter callback. |
| 5335 | +** |
| 5336 | +** idxNum==1 PATH parameter only |
| 5337 | +** idxNum==2 Both PATH and DIR supplied |
| 5338 | +*/ |
| 5339 | +static int fsdirFilter( |
| 5340 | + sqlite3_vtab_cursor *cur, |
| 5341 | + int idxNum, const char *idxStr, |
| 5342 | + int argc, sqlite3_value **argv |
| 5343 | +){ |
| 5344 | + const char *zDir = 0; |
| 5345 | + fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 5346 | + (void)idxStr; |
| 5347 | + fsdirResetCursor(pCur); |
| 5348 | + |
| 5349 | + if( idxNum==0 ){ |
| 5350 | + fsdirSetErrmsg(pCur, "table function fsdir requires an argument"); |
| 5351 | + return SQLITE_ERROR; |
| 5352 | + } |
| 5353 | + |
| 5354 | + assert( argc==idxNum && (argc==1 || argc==2) ); |
| 5355 | + zDir = (const char*)sqlite3_value_text(argv[0]); |
| 5356 | + if( zDir==0 ){ |
| 5357 | + fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument"); |
| 5358 | + return SQLITE_ERROR; |
| 5359 | + } |
| 5360 | + if( argc==2 ){ |
| 5361 | + pCur->zBase = (const char*)sqlite3_value_text(argv[1]); |
| 5362 | + } |
| 5363 | + if( pCur->zBase ){ |
| 5364 | + pCur->nBase = (int)strlen(pCur->zBase)+1; |
| 5365 | + pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir); |
| 5366 | + }else{ |
| 5367 | + pCur->zPath = sqlite3_mprintf("%s", zDir); |
| 5368 | + } |
| 5369 | + |
| 5370 | + if( pCur->zPath==0 ){ |
| 5371 | + return SQLITE_NOMEM; |
| 5372 | + } |
| 5373 | + if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ |
| 5374 | + fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); |
| 5375 | + return SQLITE_ERROR; |
| 5376 | + } |
| 5377 | + |
| 5378 | + return SQLITE_OK; |
| 5379 | +} |
| 5380 | + |
| 5381 | +/* |
| 5382 | +** SQLite will invoke this method one or more times while planning a query |
| 5383 | +** that uses the generate_series virtual table. This routine needs to create |
| 5384 | +** a query plan for each invocation and compute an estimated cost for that |
| 5385 | +** plan. |
| 5386 | +** |
| 5387 | +** In this implementation idxNum is used to represent the |
| 5388 | +** query plan. idxStr is unused. |
| 5389 | +** |
| 5390 | +** The query plan is represented by values of idxNum: |
| 5391 | +** |
| 5392 | +** (1) The path value is supplied by argv[0] |
| 5393 | +** (2) Path is in argv[0] and dir is in argv[1] |
| 5394 | +*/ |
| 5395 | +static int fsdirBestIndex( |
| 5396 | + sqlite3_vtab *tab, |
| 5397 | + sqlite3_index_info *pIdxInfo |
| 5398 | +){ |
| 5399 | + int i; /* Loop over constraints */ |
| 5400 | + int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */ |
| 5401 | + int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */ |
| 5402 | + int seenPath = 0; /* True if an unusable PATH= constraint is seen */ |
| 5403 | + int seenDir = 0; /* True if an unusable DIR= constraint is seen */ |
| 5404 | + const struct sqlite3_index_constraint *pConstraint; |
| 5405 | + |
| 5406 | + (void)tab; |
| 5407 | + pConstraint = pIdxInfo->aConstraint; |
| 5408 | + for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 5409 | + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 5410 | + switch( pConstraint->iColumn ){ |
| 5411 | + case FSDIR_COLUMN_PATH: { |
| 5412 | + if( pConstraint->usable ){ |
| 5413 | + idxPath = i; |
| 5414 | + seenPath = 0; |
| 5415 | + }else if( idxPath<0 ){ |
| 5416 | + seenPath = 1; |
| 5417 | + } |
| 5418 | + break; |
| 5419 | + } |
| 5420 | + case FSDIR_COLUMN_DIR: { |
| 5421 | + if( pConstraint->usable ){ |
| 5422 | + idxDir = i; |
| 5423 | + seenDir = 0; |
| 5424 | + }else if( idxDir<0 ){ |
| 5425 | + seenDir = 1; |
| 5426 | + } |
| 5427 | + break; |
| 5428 | + } |
| 5429 | + } |
| 5430 | + } |
| 5431 | + if( seenPath || seenDir ){ |
| 5432 | + /* If input parameters are unusable, disallow this plan */ |
| 5433 | + return SQLITE_CONSTRAINT; |
| 5434 | + } |
| 5435 | + |
| 5436 | + if( idxPath<0 ){ |
| 5437 | + pIdxInfo->idxNum = 0; |
| 5438 | + /* The pIdxInfo->estimatedCost should have been initialized to a huge |
| 5439 | + ** number. Leave it unchanged. */ |
| 5440 | + pIdxInfo->estimatedRows = 0x7fffffff; |
| 5441 | + }else{ |
| 5442 | + pIdxInfo->aConstraintUsage[idxPath].omit = 1; |
| 5443 | + pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1; |
| 5444 | + if( idxDir>=0 ){ |
| 5445 | + pIdxInfo->aConstraintUsage[idxDir].omit = 1; |
| 5446 | + pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2; |
| 5447 | + pIdxInfo->idxNum = 2; |
| 5448 | + pIdxInfo->estimatedCost = 10.0; |
| 5449 | + }else{ |
| 5450 | + pIdxInfo->idxNum = 1; |
| 5451 | + pIdxInfo->estimatedCost = 100.0; |
| 5452 | + } |
| 5453 | + } |
| 5454 | + |
| 5455 | + return SQLITE_OK; |
| 5456 | +} |
| 5457 | + |
| 5458 | +/* |
| 5459 | +** Register the "fsdir" virtual table. |
| 5460 | +*/ |
| 5461 | +static int fsdirRegister(sqlite3 *db){ |
| 5462 | + static sqlite3_module fsdirModule = { |
| 5463 | + 0, /* iVersion */ |
| 5464 | + 0, /* xCreate */ |
| 5465 | + fsdirConnect, /* xConnect */ |
| 5466 | + fsdirBestIndex, /* xBestIndex */ |
| 5467 | + fsdirDisconnect, /* xDisconnect */ |
| 5468 | + 0, /* xDestroy */ |
| 5469 | + fsdirOpen, /* xOpen - open a cursor */ |
| 5470 | + fsdirClose, /* xClose - close a cursor */ |
| 5471 | + fsdirFilter, /* xFilter - configure scan constraints */ |
| 5472 | + fsdirNext, /* xNext - advance a cursor */ |
| 5473 | + fsdirEof, /* xEof - check for end of scan */ |
| 5474 | + fsdirColumn, /* xColumn - read data */ |
| 5475 | + fsdirRowid, /* xRowid - read data */ |
| 5476 | + 0, /* xUpdate */ |
| 5477 | + 0, /* xBegin */ |
| 5478 | + 0, /* xSync */ |
| 5479 | + 0, /* xCommit */ |
| 5480 | + 0, /* xRollback */ |
| 5481 | + 0, /* xFindMethod */ |
| 5482 | + 0, /* xRename */ |
| 5483 | + 0, /* xSavepoint */ |
| 5484 | + 0, /* xRelease */ |
| 5485 | + 0, /* xRollbackTo */ |
| 5486 | + 0, /* xShadowName */ |
| 5487 | + }; |
| 5488 | + |
| 5489 | + int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0); |
| 5490 | + return rc; |
| 5491 | +} |
| 5492 | +#else /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5493 | +# define fsdirRegister(x) SQLITE_OK |
| 5494 | +#endif |
| 5495 | + |
| 5496 | +#ifdef _WIN32 |
| 5497 | + |
| 5498 | +#endif |
| 5499 | +int sqlite3_fileio_init( |
| 5500 | + sqlite3 *db, |
| 5501 | + char **pzErrMsg, |
| 5502 | + const sqlite3_api_routines *pApi |
| 5503 | +){ |
| 5504 | + int rc = SQLITE_OK; |
| 5505 | + SQLITE_EXTENSION_INIT2(pApi); |
| 5506 | + (void)pzErrMsg; /* Unused parameter */ |
| 5507 | + rc = sqlite3_create_function(db, "readfile", 1, |
| 5508 | + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
| 5509 | + readfileFunc, 0, 0); |
| 5510 | + if( rc==SQLITE_OK ){ |
| 5511 | + rc = sqlite3_create_function(db, "writefile", -1, |
| 5512 | + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
| 5513 | + writefileFunc, 0, 0); |
| 5514 | + } |
| 5515 | + if( rc==SQLITE_OK ){ |
| 5516 | + rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0, |
| 5517 | + lsModeFunc, 0, 0); |
| 5518 | + } |
| 5519 | + if( rc==SQLITE_OK ){ |
| 5520 | + rc = fsdirRegister(db); |
| 5521 | + } |
| 5522 | + return rc; |
| 5523 | +} |
| 5524 | + |
| 5525 | +#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32)) |
| 5526 | +/* To allow a standalone DLL, make test_windirent.c use the same |
| 5527 | + * redefined SQLite API calls as the above extension code does. |
| 5528 | + * Just pull in this .c to accomplish this. As a beneficial side |
| 5529 | + * effect, this extension becomes a single translation unit. */ |
| 5530 | +# include "test_windirent.c" |
| 5531 | +#endif |
| 5532 | + |
| 5533 | +/************************* End ../ext/misc/fileio.c ********************/ |
| 5534 | +/************************* Begin ../ext/misc/completion.c ******************/ |
| 5535 | +/* |
| 5536 | +** 2017-07-10 |
| 5537 | +** |
| 5538 | +** The author disclaims copyright to this source code. In place of |
| 5539 | +** a legal notice, here is a blessing: |
| 5540 | +** |
| 5541 | +** May you do good and not evil. |
| 5542 | +** May you find forgiveness for yourself and forgive others. |
| 5543 | +** May you share freely, never taking more than you give. |
| 5544 | +** |
| 5545 | +************************************************************************* |
| 5546 | +** |
| 5547 | +** This file implements an eponymous virtual table that returns suggested |
| 5548 | +** completions for a partial SQL input. |
| 5549 | +** |
| 5550 | +** Suggested usage: |
| 5551 | +** |
| 5552 | +** SELECT DISTINCT candidate COLLATE nocase |
| 5553 | +** FROM completion($prefix,$wholeline) |
| 5554 | +** ORDER BY 1; |
| 5555 | +** |
| 5556 | +** The two query parameters are optional. $prefix is the text of the |
| 5557 | +** current word being typed and that is to be completed. $wholeline is |
| 5558 | +** the complete input line, used for context. |
| 5559 | +** |
| 5560 | +** The raw completion() table might return the same candidate multiple |
| 5561 | +** times, for example if the same column name is used to two or more |
| 5562 | +** tables. And the candidates are returned in an arbitrary order. Hence, |
| 5563 | +** the DISTINCT and ORDER BY are recommended. |
| 5564 | +** |
| 5565 | +** This virtual table operates at the speed of human typing, and so there |
| 5566 | +** is no attempt to make it fast. Even a slow implementation will be much |
| 5567 | +** faster than any human can type. |
| 5568 | +** |
| 5569 | +*/ |
| 5570 | +/* #include "sqlite3ext.h" */ |
| 5571 | +SQLITE_EXTENSION_INIT1 |
| 5572 | +#include <assert.h> |
| 5573 | +#include <string.h> |
| 5574 | +#include <ctype.h> |
| 5575 | + |
| 5576 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 5577 | + |
| 5578 | +/* completion_vtab is a subclass of sqlite3_vtab which will |
| 5579 | +** serve as the underlying representation of a completion virtual table |
| 5580 | +*/ |
| 5581 | +typedef struct completion_vtab completion_vtab; |
| 5582 | +struct completion_vtab { |
| 5583 | + sqlite3_vtab base; /* Base class - must be first */ |
| 5584 | + sqlite3 *db; /* Database connection for this completion vtab */ |
| 5585 | +}; |
| 5586 | + |
| 5587 | +/* completion_cursor is a subclass of sqlite3_vtab_cursor which will |
| 5588 | +** serve as the underlying representation of a cursor that scans |
| 5589 | +** over rows of the result |
| 5590 | +*/ |
| 5591 | +typedef struct completion_cursor completion_cursor; |
| 5592 | +struct completion_cursor { |
| 5593 | + sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 5594 | + sqlite3 *db; /* Database connection for this cursor */ |
| 5595 | + int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */ |
| 5596 | + char *zPrefix; /* The prefix for the word we want to complete */ |
| 5597 | + char *zLine; /* The whole that we want to complete */ |
| 5598 | + const char *zCurrentRow; /* Current output row */ |
| 5599 | + int szRow; /* Length of the zCurrentRow string */ |
| 5600 | + sqlite3_stmt *pStmt; /* Current statement */ |
| 5601 | + sqlite3_int64 iRowid; /* The rowid */ |
| 5602 | + int ePhase; /* Current phase */ |
| 5603 | + int j; /* inter-phase counter */ |
| 5604 | +}; |
| 5605 | + |
| 5606 | +/* Values for ePhase: |
| 5607 | +*/ |
| 5608 | +#define COMPLETION_FIRST_PHASE 1 |
| 5609 | +#define COMPLETION_KEYWORDS 1 |
| 5610 | +#define COMPLETION_PRAGMAS 2 |
| 5611 | +#define COMPLETION_FUNCTIONS 3 |
| 5612 | +#define COMPLETION_COLLATIONS 4 |
| 5613 | +#define COMPLETION_INDEXES 5 |
| 5614 | +#define COMPLETION_TRIGGERS 6 |
| 5615 | +#define COMPLETION_DATABASES 7 |
| 5616 | +#define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */ |
| 5617 | +#define COMPLETION_COLUMNS 9 |
| 5618 | +#define COMPLETION_MODULES 10 |
| 5619 | +#define COMPLETION_EOF 11 |
| 5620 | + |
| 5621 | +/* |
| 5622 | +** The completionConnect() method is invoked to create a new |
| 5623 | +** completion_vtab that describes the completion virtual table. |
| 5624 | +** |
| 5625 | +** Think of this routine as the constructor for completion_vtab objects. |
| 5626 | +** |
| 5627 | +** All this routine needs to do is: |
| 5628 | +** |
| 5629 | +** (1) Allocate the completion_vtab object and initialize all fields. |
| 5630 | +** |
| 5631 | +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the |
| 5632 | +** result set of queries against completion will look like. |
| 5633 | +*/ |
| 5634 | +static int completionConnect( |
| 5635 | + sqlite3 *db, |
| 5636 | + void *pAux, |
| 5637 | + int argc, const char *const*argv, |
| 5638 | + sqlite3_vtab **ppVtab, |
| 5639 | + char **pzErr |
| 5640 | +){ |
| 5641 | + completion_vtab *pNew; |
| 5642 | + int rc; |
| 5643 | + |
| 5644 | + (void)(pAux); /* Unused parameter */ |
| 5645 | + (void)(argc); /* Unused parameter */ |
| 5646 | + (void)(argv); /* Unused parameter */ |
| 5647 | + (void)(pzErr); /* Unused parameter */ |
| 5648 | + |
| 5649 | +/* Column numbers */ |
| 5650 | +#define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */ |
| 5651 | +#define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */ |
| 5652 | +#define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */ |
| 5653 | +#define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ |
| 5654 | + |
| 5655 | + sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
| 5656 | + rc = sqlite3_declare_vtab(db, |
| 5657 | + "CREATE TABLE x(" |
| 5658 | + " candidate TEXT," |
| 5659 | + " prefix TEXT HIDDEN," |
| 5660 | + " wholeline TEXT HIDDEN," |
| 5661 | + " phase INT HIDDEN" /* Used for debugging only */ |
| 5662 | + ")"); |
| 5663 | + if( rc==SQLITE_OK ){ |
| 5664 | + pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 5665 | + *ppVtab = (sqlite3_vtab*)pNew; |
| 5666 | + if( pNew==0 ) return SQLITE_NOMEM; |
| 5667 | + memset(pNew, 0, sizeof(*pNew)); |
| 5668 | + pNew->db = db; |
| 5669 | + } |
| 5670 | + return rc; |
| 5671 | +} |
| 5672 | + |
| 5673 | +/* |
| 5674 | +** This method is the destructor for completion_cursor objects. |
| 5675 | +*/ |
| 5676 | +static int completionDisconnect(sqlite3_vtab *pVtab){ |
| 5677 | + sqlite3_free(pVtab); |
| 5678 | + return SQLITE_OK; |
| 5679 | +} |
| 5680 | + |
| 5681 | +/* |
| 5682 | +** Constructor for a new completion_cursor object. |
| 5683 | +*/ |
| 5684 | +static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 5685 | + completion_cursor *pCur; |
| 5686 | + pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 5687 | + if( pCur==0 ) return SQLITE_NOMEM; |
| 5688 | + memset(pCur, 0, sizeof(*pCur)); |
| 5689 | + pCur->db = ((completion_vtab*)p)->db; |
| 5690 | + *ppCursor = &pCur->base; |
| 5691 | + return SQLITE_OK; |
| 5692 | +} |
| 5693 | + |
| 5694 | +/* |
| 5695 | +** Reset the completion_cursor. |
| 5696 | +*/ |
| 5697 | +static void completionCursorReset(completion_cursor *pCur){ |
| 5698 | + sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; |
| 5699 | + sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; |
| 5700 | + sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; |
| 5701 | + pCur->j = 0; |
| 5702 | +} |
| 5703 | + |
| 5704 | +/* |
| 5705 | +** Destructor for a completion_cursor. |
| 5706 | +*/ |
| 5707 | +static int completionClose(sqlite3_vtab_cursor *cur){ |
| 5708 | + completionCursorReset((completion_cursor*)cur); |
| 5709 | + sqlite3_free(cur); |
| 5710 | + return SQLITE_OK; |
| 5711 | +} |
| 5712 | + |
| 5713 | +/* |
| 5714 | +** Advance a completion_cursor to its next row of output. |
| 5715 | +** |
| 5716 | +** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object |
| 5717 | +** record the current state of the scan. This routine sets ->zCurrentRow |
| 5718 | +** to the current row of output and then returns. If no more rows remain, |
| 5719 | +** then ->ePhase is set to COMPLETION_EOF which will signal the virtual |
| 5720 | +** table that has reached the end of its scan. |
| 5721 | +** |
| 5722 | +** The current implementation just lists potential identifiers and |
| 5723 | +** keywords and filters them by zPrefix. Future enhancements should |
| 5724 | +** take zLine into account to try to restrict the set of identifiers and |
| 5725 | +** keywords based on what would be legal at the current point of input. |
| 5726 | +*/ |
| 5727 | +static int completionNext(sqlite3_vtab_cursor *cur){ |
| 5728 | + completion_cursor *pCur = (completion_cursor*)cur; |
| 5729 | + int eNextPhase = 0; /* Next phase to try if current phase reaches end */ |
| 5730 | + int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ |
| 5731 | + pCur->iRowid++; |
| 5732 | + while( pCur->ePhase!=COMPLETION_EOF ){ |
| 5733 | + switch( pCur->ePhase ){ |
| 5734 | + case COMPLETION_KEYWORDS: { |
| 5735 | + if( pCur->j >= sqlite3_keyword_count() ){ |
| 5736 | + pCur->zCurrentRow = 0; |
| 5737 | + pCur->ePhase = COMPLETION_DATABASES; |
| 5738 | + }else{ |
| 5739 | + sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow); |
| 5740 | + } |
| 5741 | + iCol = -1; |
| 5742 | + break; |
| 5743 | + } |
| 5744 | + case COMPLETION_DATABASES: { |
| 5745 | + if( pCur->pStmt==0 ){ |
| 5746 | + sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, |
| 5747 | + &pCur->pStmt, 0); |
| 5748 | + } |
| 5749 | + iCol = 1; |
| 5750 | + eNextPhase = COMPLETION_TABLES; |
| 5751 | + break; |
| 5752 | + } |
| 5753 | + case COMPLETION_TABLES: { |
| 5754 | + if( pCur->pStmt==0 ){ |
| 5755 | + sqlite3_stmt *pS2; |
| 5756 | + char *zSql = 0; |
| 5757 | + const char *zSep = ""; |
| 5758 | + sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
| 5759 | + while( sqlite3_step(pS2)==SQLITE_ROW ){ |
| 5760 | + const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
| 5761 | + zSql = sqlite3_mprintf( |
| 5762 | + "%z%s" |
| 5763 | + "SELECT name FROM \"%w\".sqlite_schema", |
| 5764 | + zSql, zSep, zDb |
| 5765 | + ); |
| 5766 | + if( zSql==0 ) return SQLITE_NOMEM; |
| 5767 | + zSep = " UNION "; |
| 5768 | + } |
| 5769 | + sqlite3_finalize(pS2); |
| 5770 | + sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
| 5771 | + sqlite3_free(zSql); |
| 5772 | + } |
| 5773 | + iCol = 0; |
| 5774 | + eNextPhase = COMPLETION_COLUMNS; |
| 5775 | + break; |
| 5776 | + } |
| 5777 | + case COMPLETION_COLUMNS: { |
| 5778 | + if( pCur->pStmt==0 ){ |
| 5779 | + sqlite3_stmt *pS2; |
| 5780 | + char *zSql = 0; |
| 5781 | + const char *zSep = ""; |
| 5782 | + sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
| 5783 | + while( sqlite3_step(pS2)==SQLITE_ROW ){ |
| 5784 | + const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
| 5785 | + zSql = sqlite3_mprintf( |
| 5786 | + "%z%s" |
| 5787 | + "SELECT pti.name FROM \"%w\".sqlite_schema AS sm" |
| 5788 | + " JOIN pragma_table_info(sm.name,%Q) AS pti" |
| 5789 | + " WHERE sm.type='table'", |
| 5790 | + zSql, zSep, zDb, zDb |
| 5791 | + ); |
| 5792 | + if( zSql==0 ) return SQLITE_NOMEM; |
| 5793 | + zSep = " UNION "; |
| 5794 | + } |
| 5795 | + sqlite3_finalize(pS2); |
| 5796 | + sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
| 5797 | + sqlite3_free(zSql); |
| 5798 | + } |
| 5799 | + iCol = 0; |
| 5800 | + eNextPhase = COMPLETION_EOF; |
| 5801 | + break; |
| 5802 | + } |
| 5803 | + } |
| 5804 | + if( iCol<0 ){ |
| 5805 | + /* This case is when the phase presets zCurrentRow */ |
| 5806 | + if( pCur->zCurrentRow==0 ) continue; |
| 5807 | + }else{ |
| 5808 | + if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ |
| 5809 | + /* Extract the next row of content */ |
| 5810 | + pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); |
| 5811 | + pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol); |
| 5812 | + }else{ |
| 5813 | + /* When all rows are finished, advance to the next phase */ |
| 5814 | + sqlite3_finalize(pCur->pStmt); |
| 5815 | + pCur->pStmt = 0; |
| 5816 | + pCur->ePhase = eNextPhase; |
| 5817 | + continue; |
| 5818 | + } |
| 5819 | + } |
| 5820 | + if( pCur->nPrefix==0 ) break; |
| 5821 | + if( pCur->nPrefix<=pCur->szRow |
| 5822 | + && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 |
| 5823 | + ){ |
| 5824 | + break; |
| 5825 | + } |
| 5826 | + } |
| 5827 | + |
| 5828 | + return SQLITE_OK; |
| 5829 | +} |
| 5830 | + |
| 5831 | +/* |
| 5832 | +** Return values of columns for the row at which the completion_cursor |
| 5833 | +** is currently pointing. |
| 5834 | +*/ |
| 5835 | +static int completionColumn( |
| 5836 | + sqlite3_vtab_cursor *cur, /* The cursor */ |
| 5837 | + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 5838 | + int i /* Which column to return */ |
| 5839 | +){ |
| 5840 | + completion_cursor *pCur = (completion_cursor*)cur; |
| 5841 | + switch( i ){ |
| 5842 | + case COMPLETION_COLUMN_CANDIDATE: { |
| 5843 | + sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT); |
| 5844 | + break; |
| 5845 | + } |
| 5846 | + case COMPLETION_COLUMN_PREFIX: { |
| 5847 | + sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); |
| 5848 | + break; |
| 5849 | + } |
| 5850 | + case COMPLETION_COLUMN_WHOLELINE: { |
| 5851 | + sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); |
| 5852 | + break; |
| 5853 | + } |
| 5854 | + case COMPLETION_COLUMN_PHASE: { |
| 5855 | + sqlite3_result_int(ctx, pCur->ePhase); |
| 5856 | + break; |
| 5857 | + } |
| 5858 | + } |
| 5859 | + return SQLITE_OK; |
| 5860 | +} |
| 5861 | + |
| 5862 | +/* |
| 5863 | +** Return the rowid for the current row. In this implementation, the |
| 5864 | +** rowid is the same as the output value. |
| 5865 | +*/ |
| 5866 | +static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 5867 | + completion_cursor *pCur = (completion_cursor*)cur; |
| 5868 | + *pRowid = pCur->iRowid; |
| 5869 | + return SQLITE_OK; |
| 5870 | +} |
| 5871 | + |
| 5872 | +/* |
| 5873 | +** Return TRUE if the cursor has been moved off of the last |
| 5874 | +** row of output. |
| 5875 | +*/ |
| 5876 | +static int completionEof(sqlite3_vtab_cursor *cur){ |
| 5877 | + completion_cursor *pCur = (completion_cursor*)cur; |
| 5878 | + return pCur->ePhase >= COMPLETION_EOF; |
| 5879 | +} |
| 5880 | + |
| 5881 | +/* |
| 5882 | +** This method is called to "rewind" the completion_cursor object back |
| 5883 | +** to the first row of output. This method is always called at least |
| 5884 | +** once prior to any call to completionColumn() or completionRowid() or |
| 5885 | +** completionEof(). |
| 5886 | +*/ |
| 5887 | +static int completionFilter( |
| 5888 | + sqlite3_vtab_cursor *pVtabCursor, |
| 5889 | + int idxNum, const char *idxStr, |
| 5890 | + int argc, sqlite3_value **argv |
| 5891 | +){ |
| 5892 | + completion_cursor *pCur = (completion_cursor *)pVtabCursor; |
| 5893 | + int iArg = 0; |
| 5894 | + (void)(idxStr); /* Unused parameter */ |
| 5895 | + (void)(argc); /* Unused parameter */ |
| 5896 | + completionCursorReset(pCur); |
| 5897 | + if( idxNum & 1 ){ |
| 5898 | + pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); |
| 5899 | + if( pCur->nPrefix>0 ){ |
| 5900 | + pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
| 5901 | + if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
| 5902 | + } |
| 5903 | + iArg = 1; |
| 5904 | + } |
| 5905 | + if( idxNum & 2 ){ |
| 5906 | + pCur->nLine = sqlite3_value_bytes(argv[iArg]); |
| 5907 | + if( pCur->nLine>0 ){ |
| 5908 | + pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
| 5909 | + if( pCur->zLine==0 ) return SQLITE_NOMEM; |
| 5910 | + } |
| 5911 | + } |
| 5912 | + if( pCur->zLine!=0 && pCur->zPrefix==0 ){ |
| 5913 | + int i = pCur->nLine; |
| 5914 | + while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ |
| 5915 | + i--; |
| 5916 | + } |
| 5917 | + pCur->nPrefix = pCur->nLine - i; |
| 5918 | + if( pCur->nPrefix>0 ){ |
| 5919 | + pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); |
| 5920 | + if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
| 5921 | + } |
| 5922 | + } |
| 5923 | + pCur->iRowid = 0; |
| 5924 | + pCur->ePhase = COMPLETION_FIRST_PHASE; |
| 5925 | + return completionNext(pVtabCursor); |
| 5926 | +} |
| 5927 | + |
| 5928 | +/* |
| 5929 | +** SQLite will invoke this method one or more times while planning a query |
| 5930 | +** that uses the completion virtual table. This routine needs to create |
| 5931 | +** a query plan for each invocation and compute an estimated cost for that |
| 5932 | +** plan. |
| 5933 | +** |
| 5934 | +** There are two hidden parameters that act as arguments to the table-valued |
| 5935 | +** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix" |
| 5936 | +** is available and bit 1 is set if "wholeline" is available. |
| 5937 | +*/ |
| 5938 | +static int completionBestIndex( |
| 5939 | + sqlite3_vtab *tab, |
| 5940 | + sqlite3_index_info *pIdxInfo |
| 5941 | +){ |
| 5942 | + int i; /* Loop over constraints */ |
| 5943 | + int idxNum = 0; /* The query plan bitmask */ |
| 5944 | + int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ |
| 5945 | + int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ |
| 5946 | + int nArg = 0; /* Number of arguments that completeFilter() expects */ |
| 5947 | + const struct sqlite3_index_constraint *pConstraint; |
| 5948 | + |
| 5949 | + (void)(tab); /* Unused parameter */ |
| 5950 | + pConstraint = pIdxInfo->aConstraint; |
| 5951 | + for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 5952 | + if( pConstraint->usable==0 ) continue; |
| 5953 | + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 5954 | + switch( pConstraint->iColumn ){ |
| 5955 | + case COMPLETION_COLUMN_PREFIX: |
| 5956 | + prefixIdx = i; |
| 5957 | + idxNum |= 1; |
| 5958 | + break; |
| 5959 | + case COMPLETION_COLUMN_WHOLELINE: |
| 5960 | + wholelineIdx = i; |
| 5961 | + idxNum |= 2; |
| 5962 | + break; |
| 5963 | + } |
| 5964 | + } |
| 5965 | + if( prefixIdx>=0 ){ |
| 5966 | + pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; |
| 5967 | + pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; |
| 5968 | + } |
| 5969 | + if( wholelineIdx>=0 ){ |
| 5970 | + pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; |
| 5971 | + pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; |
| 5972 | + } |
| 5973 | + pIdxInfo->idxNum = idxNum; |
| 5974 | + pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; |
| 5975 | + pIdxInfo->estimatedRows = 500 - 100*nArg; |
| 5976 | + return SQLITE_OK; |
| 5977 | +} |
| 5978 | + |
| 5979 | +/* |
| 5980 | +** This following structure defines all the methods for the |
| 5981 | +** completion virtual table. |
| 5982 | +*/ |
| 5983 | +static sqlite3_module completionModule = { |
| 5984 | + 0, /* iVersion */ |
| 5985 | + 0, /* xCreate */ |
| 5986 | + completionConnect, /* xConnect */ |
| 5987 | + completionBestIndex, /* xBestIndex */ |
| 5988 | + completionDisconnect, /* xDisconnect */ |
| 5989 | + 0, /* xDestroy */ |
| 5990 | + completionOpen, /* xOpen - open a cursor */ |
| 5991 | + completionClose, /* xClose - close a cursor */ |
| 5992 | + completionFilter, /* xFilter - configure scan constraints */ |
| 5993 | + completionNext, /* xNext - advance a cursor */ |
| 5994 | + completionEof, /* xEof - check for end of scan */ |
| 5995 | + completionColumn, /* xColumn - read data */ |
| 5996 | + completionRowid, /* xRowid - read data */ |
| 5997 | + 0, /* xUpdate */ |
| 5998 | + 0, /* xBegin */ |
| 5999 | + 0, /* xSync */ |
| 6000 | + 0, /* xCommit */ |
| 6001 | + 0, /* xRollback */ |
| 6002 | + 0, /* xFindMethod */ |
| 6003 | + 0, /* xRename */ |
| 6004 | + 0, /* xSavepoint */ |
| 6005 | + 0, /* xRelease */ |
| 6006 | + 0, /* xRollbackTo */ |
| 6007 | + 0 /* xShadowName */ |
| 6008 | +}; |
| 6009 | + |
| 6010 | +#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6011 | + |
| 6012 | +int sqlite3CompletionVtabInit(sqlite3 *db){ |
| 6013 | + int rc = SQLITE_OK; |
| 6014 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 6015 | + rc = sqlite3_create_module(db, "completion", &completionModule, 0); |
| 6016 | +#endif |
| 6017 | + return rc; |
| 6018 | +} |
| 6019 | + |
| 6020 | +#ifdef _WIN32 |
| 6021 | + |
| 6022 | +#endif |
| 6023 | +int sqlite3_completion_init( |
| 6024 | + sqlite3 *db, |
| 6025 | + char **pzErrMsg, |
| 6026 | + const sqlite3_api_routines *pApi |
| 6027 | +){ |
| 6028 | + int rc = SQLITE_OK; |
| 6029 | + SQLITE_EXTENSION_INIT2(pApi); |
| 6030 | + (void)(pzErrMsg); /* Unused parameter */ |
| 6031 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 6032 | + rc = sqlite3CompletionVtabInit(db); |
| 6033 | +#endif |
| 6034 | + return rc; |
| 6035 | +} |
| 6036 | + |
| 6037 | +/************************* End ../ext/misc/completion.c ********************/ |
| 6038 | +/************************* Begin ../ext/misc/appendvfs.c ******************/ |
| 6039 | +/* |
| 6040 | +** 2017-10-20 |
| 6041 | +** |
| 6042 | +** The author disclaims copyright to this source code. In place of |
| 6043 | +** a legal notice, here is a blessing: |
| 6044 | +** |
| 6045 | +** May you do good and not evil. |
| 6046 | +** May you find forgiveness for yourself and forgive others. |
| 6047 | +** May you share freely, never taking more than you give. |
| 6048 | +** |
| 6049 | +****************************************************************************** |
| 6050 | +** |
| 6051 | +** This file implements a VFS shim that allows an SQLite database to be |
| 6052 | +** appended onto the end of some other file, such as an executable. |
| 6053 | +** |
| 6054 | +** A special record must appear at the end of the file that identifies the |
| 6055 | +** file as an appended database and provides the offset to the first page |
| 6056 | +** of the exposed content. (Or, it is the length of the content prefix.) |
| 6057 | +** For best performance page 1 should be located at a disk page boundary, |
| 6058 | +** though that is not required. |
| 6059 | +** |
| 6060 | +** When opening a database using this VFS, the connection might treat |
| 6061 | +** the file as an ordinary SQLite database, or it might treat it as a |
| 6062 | +** database appended onto some other file. The decision is made by |
| 6063 | +** applying the following rules in order: |
| 6064 | +** |
| 6065 | +** (1) An empty file is an ordinary database. |
| 6066 | +** |
| 6067 | +** (2) If the file ends with the appendvfs trailer string |
| 6068 | +** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database. |
| 6069 | +** |
| 6070 | +** (3) If the file begins with the standard SQLite prefix string |
| 6071 | +** "SQLite format 3", that file is an ordinary database. |
| 6072 | +** |
| 6073 | +** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is |
| 6074 | +** set, then a new database is appended to the already existing file. |
| 6075 | +** |
| 6076 | +** (5) Otherwise, SQLITE_CANTOPEN is returned. |
| 6077 | +** |
| 6078 | +** To avoid unnecessary complications with the PENDING_BYTE, the size of |
| 6079 | +** the file containing the database is limited to 1GiB. (1073741824 bytes) |
| 6080 | +** This VFS will not read or write past the 1GiB mark. This restriction |
| 6081 | +** might be lifted in future versions. For now, if you need a larger |
| 6082 | +** database, then keep it in a separate file. |
| 6083 | +** |
| 6084 | +** If the file being opened is a plain database (not an appended one), then |
| 6085 | +** this shim is a pass-through into the default underlying VFS. (rule 3) |
| 6086 | +**/ |
| 6087 | +/* #include "sqlite3ext.h" */ |
| 6088 | +SQLITE_EXTENSION_INIT1 |
| 6089 | +#include <string.h> |
| 6090 | +#include <assert.h> |
| 6091 | + |
| 6092 | +/* The append mark at the end of the database is: |
| 6093 | +** |
| 6094 | +** Start-Of-SQLite3-NNNNNNNN |
| 6095 | +** 123456789 123456789 12345 |
| 6096 | +** |
| 6097 | +** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is |
| 6098 | +** the offset to page 1, and also the length of the prefix content. |
| 6099 | +*/ |
| 6100 | +#define APND_MARK_PREFIX "Start-Of-SQLite3-" |
| 6101 | +#define APND_MARK_PREFIX_SZ 17 |
| 6102 | +#define APND_MARK_FOS_SZ 8 |
| 6103 | +#define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ) |
| 6104 | + |
| 6105 | +/* |
| 6106 | +** Maximum size of the combined prefix + database + append-mark. This |
| 6107 | +** must be less than 0x40000000 to avoid locking issues on Windows. |
| 6108 | +*/ |
| 6109 | +#define APND_MAX_SIZE (0x40000000) |
| 6110 | + |
| 6111 | +/* |
| 6112 | +** Try to align the database to an even multiple of APND_ROUNDUP bytes. |
| 6113 | +*/ |
| 6114 | +#ifndef APND_ROUNDUP |
| 6115 | +#define APND_ROUNDUP 4096 |
| 6116 | +#endif |
| 6117 | +#define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1)) |
| 6118 | +#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK) |
| 6119 | + |
| 6120 | +/* |
| 6121 | +** Forward declaration of objects used by this utility |
| 6122 | +*/ |
| 6123 | +typedef struct sqlite3_vfs ApndVfs; |
| 6124 | +typedef struct ApndFile ApndFile; |
| 6125 | + |
| 6126 | +/* Access to a lower-level VFS that (might) implement dynamic loading, |
| 6127 | +** access to randomness, etc. |
| 6128 | +*/ |
| 6129 | +#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) |
| 6130 | +#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1)) |
| 6131 | + |
| 6132 | +/* An open appendvfs file |
| 6133 | +** |
| 6134 | +** An instance of this structure describes the appended database file. |
| 6135 | +** A separate sqlite3_file object is always appended. The appended |
| 6136 | +** sqlite3_file object (which can be accessed using ORIGFILE()) describes |
| 6137 | +** the entire file, including the prefix, the database, and the |
| 6138 | +** append-mark. |
| 6139 | +** |
| 6140 | +** The structure of an AppendVFS database is like this: |
| 6141 | +** |
| 6142 | +** +-------------+---------+----------+-------------+ |
| 6143 | +** | prefix-file | padding | database | append-mark | |
| 6144 | +** +-------------+---------+----------+-------------+ |
| 6145 | +** ^ ^ |
| 6146 | +** | | |
| 6147 | +** iPgOne iMark |
| 6148 | +** |
| 6149 | +** |
| 6150 | +** "prefix file" - file onto which the database has been appended. |
| 6151 | +** "padding" - zero or more bytes inserted so that "database" |
| 6152 | +** starts on an APND_ROUNDUP boundary |
| 6153 | +** "database" - The SQLite database file |
| 6154 | +** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates |
| 6155 | +** the offset from the start of prefix-file to the start |
| 6156 | +** of "database". |
| 6157 | +** |
| 6158 | +** The size of the database is iMark - iPgOne. |
| 6159 | +** |
| 6160 | +** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value |
| 6161 | +** of iPgOne stored as a big-ending 64-bit integer. |
| 6162 | +** |
| 6163 | +** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE). |
| 6164 | +** Or, iMark is -1 to indicate that it has not yet been written. |
| 6165 | +*/ |
| 6166 | +struct ApndFile { |
| 6167 | + sqlite3_file base; /* Subclass. MUST BE FIRST! */ |
| 6168 | + sqlite3_int64 iPgOne; /* Offset to the start of the database */ |
| 6169 | + sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */ |
| 6170 | + /* Always followed by another sqlite3_file that describes the whole file */ |
| 6171 | +}; |
| 6172 | + |
| 6173 | +/* |
| 6174 | +** Methods for ApndFile |
| 6175 | +*/ |
| 6176 | +static int apndClose(sqlite3_file*); |
| 6177 | +static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 6178 | +static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); |
| 6179 | +static int apndTruncate(sqlite3_file*, sqlite3_int64 size); |
| 6180 | +static int apndSync(sqlite3_file*, int flags); |
| 6181 | +static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize); |
| 6182 | +static int apndLock(sqlite3_file*, int); |
| 6183 | +static int apndUnlock(sqlite3_file*, int); |
| 6184 | +static int apndCheckReservedLock(sqlite3_file*, int *pResOut); |
| 6185 | +static int apndFileControl(sqlite3_file*, int op, void *pArg); |
| 6186 | +static int apndSectorSize(sqlite3_file*); |
| 6187 | +static int apndDeviceCharacteristics(sqlite3_file*); |
| 6188 | +static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**); |
| 6189 | +static int apndShmLock(sqlite3_file*, int offset, int n, int flags); |
| 6190 | +static void apndShmBarrier(sqlite3_file*); |
| 6191 | +static int apndShmUnmap(sqlite3_file*, int deleteFlag); |
| 6192 | +static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); |
| 6193 | +static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); |
| 6194 | + |
| 6195 | +/* |
| 6196 | +** Methods for ApndVfs |
| 6197 | +*/ |
| 6198 | +static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
| 6199 | +static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir); |
| 6200 | +static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
| 6201 | +static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); |
| 6202 | +static void *apndDlOpen(sqlite3_vfs*, const char *zFilename); |
| 6203 | +static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg); |
| 6204 | +static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); |
| 6205 | +static void apndDlClose(sqlite3_vfs*, void*); |
| 6206 | +static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut); |
| 6207 | +static int apndSleep(sqlite3_vfs*, int microseconds); |
| 6208 | +static int apndCurrentTime(sqlite3_vfs*, double*); |
| 6209 | +static int apndGetLastError(sqlite3_vfs*, int, char *); |
| 6210 | +static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); |
| 6211 | +static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr); |
| 6212 | +static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z); |
| 6213 | +static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName); |
| 6214 | + |
| 6215 | +static sqlite3_vfs apnd_vfs = { |
| 6216 | + 3, /* iVersion (set when registered) */ |
| 6217 | + 0, /* szOsFile (set when registered) */ |
| 6218 | + 1024, /* mxPathname */ |
| 6219 | + 0, /* pNext */ |
| 6220 | + "apndvfs", /* zName */ |
| 6221 | + 0, /* pAppData (set when registered) */ |
| 6222 | + apndOpen, /* xOpen */ |
| 6223 | + apndDelete, /* xDelete */ |
| 6224 | + apndAccess, /* xAccess */ |
| 6225 | + apndFullPathname, /* xFullPathname */ |
| 6226 | + apndDlOpen, /* xDlOpen */ |
| 6227 | + apndDlError, /* xDlError */ |
| 6228 | + apndDlSym, /* xDlSym */ |
| 6229 | + apndDlClose, /* xDlClose */ |
| 6230 | + apndRandomness, /* xRandomness */ |
| 6231 | + apndSleep, /* xSleep */ |
| 6232 | + apndCurrentTime, /* xCurrentTime */ |
| 6233 | + apndGetLastError, /* xGetLastError */ |
| 6234 | + apndCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 6235 | + apndSetSystemCall, /* xSetSystemCall */ |
| 6236 | + apndGetSystemCall, /* xGetSystemCall */ |
| 6237 | + apndNextSystemCall /* xNextSystemCall */ |
| 6238 | +}; |
| 6239 | + |
| 6240 | +static const sqlite3_io_methods apnd_io_methods = { |
| 6241 | + 3, /* iVersion */ |
| 6242 | + apndClose, /* xClose */ |
| 6243 | + apndRead, /* xRead */ |
| 6244 | + apndWrite, /* xWrite */ |
| 6245 | + apndTruncate, /* xTruncate */ |
| 6246 | + apndSync, /* xSync */ |
| 6247 | + apndFileSize, /* xFileSize */ |
| 6248 | + apndLock, /* xLock */ |
| 6249 | + apndUnlock, /* xUnlock */ |
| 6250 | + apndCheckReservedLock, /* xCheckReservedLock */ |
| 6251 | + apndFileControl, /* xFileControl */ |
| 6252 | + apndSectorSize, /* xSectorSize */ |
| 6253 | + apndDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 6254 | + apndShmMap, /* xShmMap */ |
| 6255 | + apndShmLock, /* xShmLock */ |
| 6256 | + apndShmBarrier, /* xShmBarrier */ |
| 6257 | + apndShmUnmap, /* xShmUnmap */ |
| 6258 | + apndFetch, /* xFetch */ |
| 6259 | + apndUnfetch /* xUnfetch */ |
| 6260 | +}; |
| 6261 | + |
| 6262 | +/* |
| 6263 | +** Close an apnd-file. |
| 6264 | +*/ |
| 6265 | +static int apndClose(sqlite3_file *pFile){ |
| 6266 | + pFile = ORIGFILE(pFile); |
| 6267 | + return pFile->pMethods->xClose(pFile); |
| 6268 | +} |
| 6269 | + |
| 6270 | +/* |
| 6271 | +** Read data from an apnd-file. |
| 6272 | +*/ |
| 6273 | +static int apndRead( |
| 6274 | + sqlite3_file *pFile, |
| 6275 | + void *zBuf, |
| 6276 | + int iAmt, |
| 6277 | + sqlite_int64 iOfst |
| 6278 | +){ |
| 6279 | + ApndFile *paf = (ApndFile *)pFile; |
| 6280 | + pFile = ORIGFILE(pFile); |
| 6281 | + return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst); |
| 6282 | +} |
| 6283 | + |
| 6284 | +/* |
| 6285 | +** Add the append-mark onto what should become the end of the file. |
| 6286 | +* If and only if this succeeds, internal ApndFile.iMark is updated. |
| 6287 | +* Parameter iWriteEnd is the appendvfs-relative offset of the new mark. |
| 6288 | +*/ |
| 6289 | +static int apndWriteMark( |
| 6290 | + ApndFile *paf, |
| 6291 | + sqlite3_file *pFile, |
| 6292 | + sqlite_int64 iWriteEnd |
| 6293 | +){ |
| 6294 | + sqlite_int64 iPgOne = paf->iPgOne; |
| 6295 | + unsigned char a[APND_MARK_SIZE]; |
| 6296 | + int i = APND_MARK_FOS_SZ; |
| 6297 | + int rc; |
| 6298 | + assert(pFile == ORIGFILE(paf)); |
| 6299 | + memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ); |
| 6300 | + while( --i >= 0 ){ |
| 6301 | + a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff); |
| 6302 | + iPgOne >>= 8; |
| 6303 | + } |
| 6304 | + iWriteEnd += paf->iPgOne; |
| 6305 | + if( SQLITE_OK==(rc = pFile->pMethods->xWrite |
| 6306 | + (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){ |
| 6307 | + paf->iMark = iWriteEnd; |
| 6308 | + } |
| 6309 | + return rc; |
| 6310 | +} |
| 6311 | + |
| 6312 | +/* |
| 6313 | +** Write data to an apnd-file. |
| 6314 | +*/ |
| 6315 | +static int apndWrite( |
| 6316 | + sqlite3_file *pFile, |
| 6317 | + const void *zBuf, |
| 6318 | + int iAmt, |
| 6319 | + sqlite_int64 iOfst |
| 6320 | +){ |
| 6321 | + ApndFile *paf = (ApndFile *)pFile; |
| 6322 | + sqlite_int64 iWriteEnd = iOfst + iAmt; |
| 6323 | + if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL; |
| 6324 | + pFile = ORIGFILE(pFile); |
| 6325 | + /* If append-mark is absent or will be overwritten, write it. */ |
| 6326 | + if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){ |
| 6327 | + int rc = apndWriteMark(paf, pFile, iWriteEnd); |
| 6328 | + if( SQLITE_OK!=rc ) return rc; |
| 6329 | + } |
| 6330 | + return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst); |
| 6331 | +} |
| 6332 | + |
| 6333 | +/* |
| 6334 | +** Truncate an apnd-file. |
| 6335 | +*/ |
| 6336 | +static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
| 6337 | + ApndFile *paf = (ApndFile *)pFile; |
| 6338 | + pFile = ORIGFILE(pFile); |
| 6339 | + /* The append mark goes out first so truncate failure does not lose it. */ |
| 6340 | + if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR; |
| 6341 | + /* Truncate underlying file just past append mark */ |
| 6342 | + return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE); |
| 6343 | +} |
| 6344 | + |
| 6345 | +/* |
| 6346 | +** Sync an apnd-file. |
| 6347 | +*/ |
| 6348 | +static int apndSync(sqlite3_file *pFile, int flags){ |
| 6349 | + pFile = ORIGFILE(pFile); |
| 6350 | + return pFile->pMethods->xSync(pFile, flags); |
| 6351 | +} |
| 6352 | + |
| 6353 | +/* |
| 6354 | +** Return the current file-size of an apnd-file. |
| 6355 | +** If the append mark is not yet there, the file-size is 0. |
| 6356 | +*/ |
| 6357 | +static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
| 6358 | + ApndFile *paf = (ApndFile *)pFile; |
| 6359 | + *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0; |
| 6360 | + return SQLITE_OK; |
| 6361 | +} |
| 6362 | + |
| 6363 | +/* |
| 6364 | +** Lock an apnd-file. |
| 6365 | +*/ |
| 6366 | +static int apndLock(sqlite3_file *pFile, int eLock){ |
| 6367 | + pFile = ORIGFILE(pFile); |
| 6368 | + return pFile->pMethods->xLock(pFile, eLock); |
| 6369 | +} |
| 6370 | + |
| 6371 | +/* |
| 6372 | +** Unlock an apnd-file. |
| 6373 | +*/ |
| 6374 | +static int apndUnlock(sqlite3_file *pFile, int eLock){ |
| 6375 | + pFile = ORIGFILE(pFile); |
| 6376 | + return pFile->pMethods->xUnlock(pFile, eLock); |
| 6377 | +} |
| 6378 | + |
| 6379 | +/* |
| 6380 | +** Check if another file-handle holds a RESERVED lock on an apnd-file. |
| 6381 | +*/ |
| 6382 | +static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 6383 | + pFile = ORIGFILE(pFile); |
| 6384 | + return pFile->pMethods->xCheckReservedLock(pFile, pResOut); |
| 6385 | +} |
| 6386 | + |
| 6387 | +/* |
| 6388 | +** File control method. For custom operations on an apnd-file. |
| 6389 | +*/ |
| 6390 | +static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 6391 | + ApndFile *paf = (ApndFile *)pFile; |
| 6392 | + int rc; |
| 6393 | + pFile = ORIGFILE(pFile); |
| 6394 | + if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne; |
| 6395 | + rc = pFile->pMethods->xFileControl(pFile, op, pArg); |
| 6396 | + if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){ |
| 6397 | + *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg); |
| 6398 | + } |
| 6399 | + return rc; |
| 6400 | +} |
| 6401 | + |
| 6402 | +/* |
| 6403 | +** Return the sector-size in bytes for an apnd-file. |
| 6404 | +*/ |
| 6405 | +static int apndSectorSize(sqlite3_file *pFile){ |
| 6406 | + pFile = ORIGFILE(pFile); |
| 6407 | + return pFile->pMethods->xSectorSize(pFile); |
| 6408 | +} |
| 6409 | + |
| 6410 | +/* |
| 6411 | +** Return the device characteristic flags supported by an apnd-file. |
| 6412 | +*/ |
| 6413 | +static int apndDeviceCharacteristics(sqlite3_file *pFile){ |
| 6414 | + pFile = ORIGFILE(pFile); |
| 6415 | + return pFile->pMethods->xDeviceCharacteristics(pFile); |
| 6416 | +} |
| 6417 | + |
| 6418 | +/* Create a shared memory file mapping */ |
| 6419 | +static int apndShmMap( |
| 6420 | + sqlite3_file *pFile, |
| 6421 | + int iPg, |
| 6422 | + int pgsz, |
| 6423 | + int bExtend, |
| 6424 | + void volatile **pp |
| 6425 | +){ |
| 6426 | + pFile = ORIGFILE(pFile); |
| 6427 | + return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); |
| 6428 | +} |
| 6429 | + |
| 6430 | +/* Perform locking on a shared-memory segment */ |
| 6431 | +static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){ |
| 6432 | + pFile = ORIGFILE(pFile); |
| 6433 | + return pFile->pMethods->xShmLock(pFile,offset,n,flags); |
| 6434 | +} |
| 6435 | + |
| 6436 | +/* Memory barrier operation on shared memory */ |
| 6437 | +static void apndShmBarrier(sqlite3_file *pFile){ |
| 6438 | + pFile = ORIGFILE(pFile); |
| 6439 | + pFile->pMethods->xShmBarrier(pFile); |
| 6440 | +} |
| 6441 | + |
| 6442 | +/* Unmap a shared memory segment */ |
| 6443 | +static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){ |
| 6444 | + pFile = ORIGFILE(pFile); |
| 6445 | + return pFile->pMethods->xShmUnmap(pFile,deleteFlag); |
| 6446 | +} |
| 6447 | + |
| 6448 | +/* Fetch a page of a memory-mapped file */ |
| 6449 | +static int apndFetch( |
| 6450 | + sqlite3_file *pFile, |
| 6451 | + sqlite3_int64 iOfst, |
| 6452 | + int iAmt, |
| 6453 | + void **pp |
| 6454 | +){ |
| 6455 | + ApndFile *p = (ApndFile *)pFile; |
| 6456 | + if( p->iMark < 0 || iOfst+iAmt > p->iMark ){ |
| 6457 | + return SQLITE_IOERR; /* Cannot read what is not yet there. */ |
| 6458 | + } |
| 6459 | + pFile = ORIGFILE(pFile); |
| 6460 | + return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp); |
| 6461 | +} |
| 6462 | + |
| 6463 | +/* Release a memory-mapped page */ |
| 6464 | +static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ |
| 6465 | + ApndFile *p = (ApndFile *)pFile; |
| 6466 | + pFile = ORIGFILE(pFile); |
| 6467 | + return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage); |
| 6468 | +} |
| 6469 | + |
| 6470 | +/* |
| 6471 | +** Try to read the append-mark off the end of a file. Return the |
| 6472 | +** start of the appended database if the append-mark is present. |
| 6473 | +** If there is no valid append-mark, return -1; |
| 6474 | +** |
| 6475 | +** An append-mark is only valid if the NNNNNNNN start-of-database offset |
| 6476 | +** indicates that the appended database contains at least one page. The |
| 6477 | +** start-of-database value must be a multiple of 512. |
| 6478 | +*/ |
| 6479 | +static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){ |
| 6480 | + int rc, i; |
| 6481 | + sqlite3_int64 iMark; |
| 6482 | + int msbs = 8 * (APND_MARK_FOS_SZ-1); |
| 6483 | + unsigned char a[APND_MARK_SIZE]; |
| 6484 | + |
| 6485 | + if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1; |
| 6486 | + rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE); |
| 6487 | + if( rc ) return -1; |
| 6488 | + if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1; |
| 6489 | + iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs; |
| 6490 | + for(i=1; i<8; i++){ |
| 6491 | + msbs -= 8; |
| 6492 | + iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs; |
| 6493 | + } |
| 6494 | + if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1; |
| 6495 | + if( iMark & 0x1ff ) return -1; |
| 6496 | + return iMark; |
| 6497 | +} |
| 6498 | + |
| 6499 | +static const char apvfsSqliteHdr[] = "SQLite format 3"; |
| 6500 | +/* |
| 6501 | +** Check to see if the file is an appendvfs SQLite database file. |
| 6502 | +** Return true iff it is such. Parameter sz is the file's size. |
| 6503 | +*/ |
| 6504 | +static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){ |
| 6505 | + int rc; |
| 6506 | + char zHdr[16]; |
| 6507 | + sqlite3_int64 iMark = apndReadMark(sz, pFile); |
| 6508 | + if( iMark>=0 ){ |
| 6509 | + /* If file has the correct end-marker, the expected odd size, and the |
| 6510 | + ** SQLite DB type marker where the end-marker puts it, then it |
| 6511 | + ** is an appendvfs database. |
| 6512 | + */ |
| 6513 | + rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark); |
| 6514 | + if( SQLITE_OK==rc |
| 6515 | + && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0 |
| 6516 | + && (sz & 0x1ff) == APND_MARK_SIZE |
| 6517 | + && sz>=512+APND_MARK_SIZE |
| 6518 | + ){ |
| 6519 | + return 1; /* It's an appendvfs database */ |
| 6520 | + } |
| 6521 | + } |
| 6522 | + return 0; |
| 6523 | +} |
| 6524 | + |
| 6525 | +/* |
| 6526 | +** Check to see if the file is an ordinary SQLite database file. |
| 6527 | +** Return true iff so. Parameter sz is the file's size. |
| 6528 | +*/ |
| 6529 | +static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){ |
| 6530 | + char zHdr[16]; |
| 6531 | + if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */ |
| 6532 | + || (sz & 0x1ff) != 0 |
| 6533 | + || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0) |
| 6534 | + || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0 |
| 6535 | + ){ |
| 6536 | + return 0; |
| 6537 | + }else{ |
| 6538 | + return 1; |
| 6539 | + } |
| 6540 | +} |
| 6541 | + |
| 6542 | +/* |
| 6543 | +** Open an apnd file handle. |
| 6544 | +*/ |
| 6545 | +static int apndOpen( |
| 6546 | + sqlite3_vfs *pApndVfs, |
| 6547 | + const char *zName, |
| 6548 | + sqlite3_file *pFile, |
| 6549 | + int flags, |
| 6550 | + int *pOutFlags |
| 6551 | +){ |
| 6552 | + ApndFile *pApndFile = (ApndFile*)pFile; |
| 6553 | + sqlite3_file *pBaseFile = ORIGFILE(pFile); |
| 6554 | + sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs); |
| 6555 | + int rc; |
| 6556 | + sqlite3_int64 sz = 0; |
| 6557 | + if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ |
| 6558 | + /* The appendvfs is not to be used for transient or temporary databases. |
| 6559 | + ** Just use the base VFS open to initialize the given file object and |
| 6560 | + ** open the underlying file. (Appendvfs is then unused for this file.) |
| 6561 | + */ |
| 6562 | + return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags); |
| 6563 | + } |
| 6564 | + memset(pApndFile, 0, sizeof(ApndFile)); |
| 6565 | + pFile->pMethods = &apnd_io_methods; |
| 6566 | + pApndFile->iMark = -1; /* Append mark not yet written */ |
| 6567 | + |
| 6568 | + rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags); |
| 6569 | + if( rc==SQLITE_OK ){ |
| 6570 | + rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz); |
| 6571 | + if( rc ){ |
| 6572 | + pBaseFile->pMethods->xClose(pBaseFile); |
| 6573 | + } |
| 6574 | + } |
| 6575 | + if( rc ){ |
| 6576 | + pFile->pMethods = 0; |
| 6577 | + return rc; |
| 6578 | + } |
| 6579 | + if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){ |
| 6580 | + /* The file being opened appears to be just an ordinary DB. Copy |
| 6581 | + ** the base dispatch-table so this instance mimics the base VFS. |
| 6582 | + */ |
| 6583 | + memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile); |
| 6584 | + return SQLITE_OK; |
| 6585 | + } |
| 6586 | + pApndFile->iPgOne = apndReadMark(sz, pFile); |
| 6587 | + if( pApndFile->iPgOne>=0 ){ |
| 6588 | + pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */ |
| 6589 | + return SQLITE_OK; |
| 6590 | + } |
| 6591 | + if( (flags & SQLITE_OPEN_CREATE)==0 ){ |
| 6592 | + pBaseFile->pMethods->xClose(pBaseFile); |
| 6593 | + rc = SQLITE_CANTOPEN; |
| 6594 | + pFile->pMethods = 0; |
| 6595 | + }else{ |
| 6596 | + /* Round newly added appendvfs location to #define'd page boundary. |
| 6597 | + ** Note that nothing has yet been written to the underlying file. |
| 6598 | + ** The append mark will be written along with first content write. |
| 6599 | + ** Until then, paf->iMark value indicates it is not yet written. |
| 6600 | + */ |
| 6601 | + pApndFile->iPgOne = APND_START_ROUNDUP(sz); |
| 6602 | + } |
| 6603 | + return rc; |
| 6604 | +} |
| 6605 | + |
| 6606 | +/* |
| 6607 | +** Delete an apnd file. |
| 6608 | +** For an appendvfs, this could mean delete the appendvfs portion, |
| 6609 | +** leaving the appendee as it was before it gained an appendvfs. |
| 6610 | +** For now, this code deletes the underlying file too. |
| 6611 | +*/ |
| 6612 | +static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 6613 | + return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); |
| 6614 | +} |
| 6615 | + |
| 6616 | +/* |
| 6617 | +** All other VFS methods are pass-thrus. |
| 6618 | +*/ |
| 6619 | +static int apndAccess( |
| 6620 | + sqlite3_vfs *pVfs, |
| 6621 | + const char *zPath, |
| 6622 | + int flags, |
| 6623 | + int *pResOut |
| 6624 | +){ |
| 6625 | + return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); |
| 6626 | +} |
| 6627 | +static int apndFullPathname( |
| 6628 | + sqlite3_vfs *pVfs, |
| 6629 | + const char *zPath, |
| 6630 | + int nOut, |
| 6631 | + char *zOut |
| 6632 | +){ |
| 6633 | + return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); |
| 6634 | +} |
| 6635 | +static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 6636 | + return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); |
| 6637 | +} |
| 6638 | +static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ |
| 6639 | + ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); |
| 6640 | +} |
| 6641 | +static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ |
| 6642 | + return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); |
| 6643 | +} |
| 6644 | +static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 6645 | + ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); |
| 6646 | +} |
| 6647 | +static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 6648 | + return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); |
| 6649 | +} |
| 6650 | +static int apndSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 6651 | + return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); |
| 6652 | +} |
| 6653 | +static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 6654 | + return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); |
| 6655 | +} |
| 6656 | +static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){ |
| 6657 | + return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); |
| 6658 | +} |
| 6659 | +static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ |
| 6660 | + return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); |
| 6661 | +} |
| 6662 | +static int apndSetSystemCall( |
| 6663 | + sqlite3_vfs *pVfs, |
| 6664 | + const char *zName, |
| 6665 | + sqlite3_syscall_ptr pCall |
| 6666 | +){ |
| 6667 | + return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); |
| 6668 | +} |
| 6669 | +static sqlite3_syscall_ptr apndGetSystemCall( |
| 6670 | + sqlite3_vfs *pVfs, |
| 6671 | + const char *zName |
| 6672 | +){ |
| 6673 | + return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); |
| 6674 | +} |
| 6675 | +static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){ |
| 6676 | + return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); |
| 6677 | +} |
| 6678 | + |
| 6679 | + |
| 6680 | +#ifdef _WIN32 |
| 6681 | + |
| 6682 | +#endif |
| 6683 | +/* |
| 6684 | +** This routine is called when the extension is loaded. |
| 6685 | +** Register the new VFS. |
| 6686 | +*/ |
| 6687 | +int sqlite3_appendvfs_init( |
| 6688 | + sqlite3 *db, |
| 6689 | + char **pzErrMsg, |
| 6690 | + const sqlite3_api_routines *pApi |
| 6691 | +){ |
| 6692 | + int rc = SQLITE_OK; |
| 6693 | + sqlite3_vfs *pOrig; |
| 6694 | + SQLITE_EXTENSION_INIT2(pApi); |
| 6695 | + (void)pzErrMsg; |
| 6696 | + (void)db; |
| 6697 | + pOrig = sqlite3_vfs_find(0); |
| 6698 | + if( pOrig==0 ) return SQLITE_ERROR; |
| 6699 | + apnd_vfs.iVersion = pOrig->iVersion; |
| 6700 | + apnd_vfs.pAppData = pOrig; |
| 6701 | + apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile); |
| 6702 | + rc = sqlite3_vfs_register(&apnd_vfs, 0); |
| 6703 | +#ifdef APPENDVFS_TEST |
| 6704 | + if( rc==SQLITE_OK ){ |
| 6705 | + rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister); |
| 6706 | + } |
| 6707 | +#endif |
| 6708 | + if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY; |
| 6709 | + return rc; |
| 6710 | +} |
| 6711 | + |
| 6712 | +/************************* End ../ext/misc/appendvfs.c ********************/ |
| 6713 | +#endif |
| 6701 | 6714 | #ifdef SQLITE_HAVE_ZLIB |
| 6702 | 6715 | /************************* Begin ../ext/misc/zipfile.c ******************/ |
| 6703 | 6716 | /* |
| 6704 | 6717 | ** 2017-12-26 |
| 6705 | 6718 | ** |
| | @@ -12235,11 +12248,21 @@ |
| 12235 | 12248 | int nIndent; /* Size of array aiIndent[] */ |
| 12236 | 12249 | int iIndent; /* Index of current op in aiIndent[] */ |
| 12237 | 12250 | char *zNonce; /* Nonce for temporary safe-mode excapes */ |
| 12238 | 12251 | EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ |
| 12239 | 12252 | ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ |
| 12253 | +#ifdef SQLITE_SHELL_WASM_MODE |
| 12254 | + struct { |
| 12255 | + const char * zInput; /* Input string from wasm/JS proxy */ |
| 12256 | + const char * zPos; /* Cursor pos into zInput */ |
| 12257 | + } wasm; |
| 12258 | +#endif |
| 12240 | 12259 | }; |
| 12260 | + |
| 12261 | +#ifdef SQLITE_SHELL_WASM_MODE |
| 12262 | +static ShellState shellState; |
| 12263 | +#endif |
| 12241 | 12264 | |
| 12242 | 12265 | |
| 12243 | 12266 | /* Allowed values for ShellState.autoEQP |
| 12244 | 12267 | */ |
| 12245 | 12268 | #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ |
| | @@ -15304,17 +15327,18 @@ |
| 15304 | 15327 | |
| 15305 | 15328 | /* |
| 15306 | 15329 | ** Text of help messages. |
| 15307 | 15330 | ** |
| 15308 | 15331 | ** The help text for each individual command begins with a line that starts |
| 15309 | | -** with ".". Subsequent lines are supplimental information. |
| 15332 | +** with ".". Subsequent lines are supplemental information. |
| 15310 | 15333 | ** |
| 15311 | 15334 | ** There must be two or more spaces between the end of the command and the |
| 15312 | 15335 | ** start of the description of what that command does. |
| 15313 | 15336 | */ |
| 15314 | 15337 | static const char *(azHelp[]) = { |
| 15315 | | -#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 15338 | +#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \ |
| 15339 | + && !defined(SQLITE_SHELL_WASM_MODE) |
| 15316 | 15340 | ".archive ... Manage SQL archives", |
| 15317 | 15341 | " Each command must have exactly one of the following options:", |
| 15318 | 15342 | " -c, --create Create a new archive", |
| 15319 | 15343 | " -u, --update Add or update files with changed mtime", |
| 15320 | 15344 | " -i, --insert Like -u but always add even if unchanged", |
| | @@ -15336,20 +15360,26 @@ |
| 15336 | 15360 | " http://sqlite.org/cli.html#sqlite_archive_support", |
| 15337 | 15361 | #endif |
| 15338 | 15362 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 15339 | 15363 | ".auth ON|OFF Show authorizer callbacks", |
| 15340 | 15364 | #endif |
| 15365 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15341 | 15366 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE", |
| 15342 | 15367 | " Options:", |
| 15343 | 15368 | " --append Use the appendvfs", |
| 15344 | 15369 | " --async Write to FILE without journal and fsync()", |
| 15370 | +#endif |
| 15345 | 15371 | ".bail on|off Stop after hitting an error. Default OFF", |
| 15346 | 15372 | ".binary on|off Turn binary output on or off. Default OFF", |
| 15373 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15347 | 15374 | ".cd DIRECTORY Change the working directory to DIRECTORY", |
| 15375 | +#endif |
| 15348 | 15376 | ".changes on|off Show number of rows changed by SQL", |
| 15377 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15349 | 15378 | ".check GLOB Fail if output since .testcase does not match", |
| 15350 | 15379 | ".clone NEWDB Clone data into NEWDB from the existing database", |
| 15380 | +#endif |
| 15351 | 15381 | ".connection [close] [#] Open or close an auxiliary database connection", |
| 15352 | 15382 | ".databases List names and files of attached databases", |
| 15353 | 15383 | ".dbconfig ?op? ?val? List or change sqlite3_db_config() options", |
| 15354 | 15384 | ".dbinfo ?DB? Show status information about the database", |
| 15355 | 15385 | ".dump ?OBJECTS? Render database content as SQL", |
| | @@ -15366,21 +15396,26 @@ |
| 15366 | 15396 | #ifdef SQLITE_DEBUG |
| 15367 | 15397 | " test Show raw EXPLAIN QUERY PLAN output", |
| 15368 | 15398 | " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"", |
| 15369 | 15399 | #endif |
| 15370 | 15400 | " trigger Like \"full\" but also show trigger bytecode", |
| 15401 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15371 | 15402 | ".excel Display the output of next command in spreadsheet", |
| 15372 | 15403 | " --bom Put a UTF8 byte-order mark on intermediate file", |
| 15404 | +#endif |
| 15405 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15373 | 15406 | ".exit ?CODE? Exit this program with return-code CODE", |
| 15407 | +#endif |
| 15374 | 15408 | ".expert EXPERIMENTAL. Suggest indexes for queries", |
| 15375 | 15409 | ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto", |
| 15376 | 15410 | ".filectrl CMD ... Run various sqlite3_file_control() operations", |
| 15377 | 15411 | " --schema SCHEMA Use SCHEMA instead of \"main\"", |
| 15378 | 15412 | " --help Show CMD details", |
| 15379 | 15413 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables", |
| 15380 | 15414 | ".headers on|off Turn display of headers on or off", |
| 15381 | 15415 | ".help ?-all? ?PATTERN? Show help text for PATTERN", |
| 15416 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15382 | 15417 | ".import FILE TABLE Import data from FILE into TABLE", |
| 15383 | 15418 | " Options:", |
| 15384 | 15419 | " --ascii Use \\037 and \\036 as column and row separators", |
| 15385 | 15420 | " --csv Use , and \\n as column and row separators", |
| 15386 | 15421 | " --skip N Skip the first N rows of input", |
| | @@ -15391,10 +15426,11 @@ |
| 15391 | 15426 | " determines the column names.", |
| 15392 | 15427 | " * If neither --csv or --ascii are used, the input mode is derived", |
| 15393 | 15428 | " from the \".mode\" output mode", |
| 15394 | 15429 | " * If FILE begins with \"|\" then it is a command that generates the", |
| 15395 | 15430 | " input text.", |
| 15431 | +#endif |
| 15396 | 15432 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 15397 | 15433 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX", |
| 15398 | 15434 | #endif |
| 15399 | 15435 | ".indexes ?TABLE? Show names of indexes", |
| 15400 | 15436 | " If TABLE is specified, only show indexes for", |
| | @@ -15404,14 +15440,16 @@ |
| 15404 | 15440 | #endif |
| 15405 | 15441 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT", |
| 15406 | 15442 | ".lint OPTIONS Report potential schema issues.", |
| 15407 | 15443 | " Options:", |
| 15408 | 15444 | " fkey-indexes Find missing foreign key indexes", |
| 15409 | | -#ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 15445 | +#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE) |
| 15410 | 15446 | ".load FILE ?ENTRY? Load an extension library", |
| 15411 | 15447 | #endif |
| 15448 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15412 | 15449 | ".log FILE|off Turn logging on or off. FILE can be stderr/stdout", |
| 15450 | +#endif |
| 15413 | 15451 | ".mode MODE ?OPTIONS? Set output mode", |
| 15414 | 15452 | " MODE is one of:", |
| 15415 | 15453 | " ascii Columns/rows delimited by 0x1F and 0x1E", |
| 15416 | 15454 | " box Tables using unicode box-drawing characters", |
| 15417 | 15455 | " csv Comma-separated values", |
| | @@ -15432,35 +15470,44 @@ |
| 15432 | 15470 | " --wordwrap B Wrap or not at word boundaries per B (on/off)", |
| 15433 | 15471 | " --ww Shorthand for \"--wordwrap 1\"", |
| 15434 | 15472 | " --quote Quote output text as SQL literals", |
| 15435 | 15473 | " --noquote Do not quote output text", |
| 15436 | 15474 | " TABLE The name of SQL table used for \"insert\" mode", |
| 15475 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15437 | 15476 | ".nonce STRING Suspend safe mode for one command if nonce matches", |
| 15477 | +#endif |
| 15438 | 15478 | ".nullvalue STRING Use STRING in place of NULL values", |
| 15479 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15439 | 15480 | ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE", |
| 15440 | 15481 | " If FILE begins with '|' then open as a pipe", |
| 15441 | 15482 | " --bom Put a UTF8 byte-order mark at the beginning", |
| 15442 | 15483 | " -e Send output to the system text editor", |
| 15443 | 15484 | " -x Send output as CSV to a spreadsheet (same as \".excel\")", |
| 15485 | + /* Note that .open is (partially) available in WASM builds but is |
| 15486 | + ** currently only intended to be used by the fiddle tool, not |
| 15487 | + ** end users, so is "undocumented." */ |
| 15444 | 15488 | ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE", |
| 15445 | 15489 | " Options:", |
| 15446 | 15490 | " --append Use appendvfs to append database to the end of FILE", |
| 15491 | +#endif |
| 15447 | 15492 | #ifndef SQLITE_OMIT_DESERIALIZE |
| 15448 | 15493 | " --deserialize Load into memory using sqlite3_deserialize()", |
| 15449 | 15494 | " --hexdb Load the output of \"dbtotxt\" as an in-memory db", |
| 15450 | 15495 | " --maxsize N Maximum size for --hexdb or --deserialized database", |
| 15451 | 15496 | #endif |
| 15452 | 15497 | " --new Initialize FILE to an empty database", |
| 15453 | 15498 | " --nofollow Do not follow symbolic links", |
| 15454 | 15499 | " --readonly Open FILE readonly", |
| 15455 | 15500 | " --zip FILE is a ZIP archive", |
| 15501 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15456 | 15502 | ".output ?FILE? Send output to FILE or stdout if FILE is omitted", |
| 15457 | 15503 | " If FILE begins with '|' then open it as a pipe.", |
| 15458 | 15504 | " Options:", |
| 15459 | 15505 | " --bom Prefix output with a UTF8 byte-order mark", |
| 15460 | 15506 | " -e Send output to the system text editor", |
| 15461 | 15507 | " -x Send output as CSV to a spreadsheet", |
| 15508 | +#endif |
| 15462 | 15509 | ".parameter CMD ... Manage SQL parameter bindings", |
| 15463 | 15510 | " clear Erase all bindings", |
| 15464 | 15511 | " init Initialize the TEMP table that holds bindings", |
| 15465 | 15512 | " list List the current parameter bindings", |
| 15466 | 15513 | " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE", |
| | @@ -15473,23 +15520,27 @@ |
| 15473 | 15520 | " --once Do no more than one progress interrupt", |
| 15474 | 15521 | " --quiet|-q No output except at interrupts", |
| 15475 | 15522 | " --reset Reset the count for each input and interrupt", |
| 15476 | 15523 | #endif |
| 15477 | 15524 | ".prompt MAIN CONTINUE Replace the standard prompts", |
| 15525 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15478 | 15526 | ".quit Exit this program", |
| 15479 | 15527 | ".read FILE Read input from FILE or command output", |
| 15480 | 15528 | " If FILE begins with \"|\", it is a command that generates the input.", |
| 15529 | +#endif |
| 15481 | 15530 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
| 15482 | 15531 | ".recover Recover as much data as possible from corrupt db.", |
| 15483 | 15532 | " --freelist-corrupt Assume the freelist is corrupt", |
| 15484 | 15533 | " --recovery-db NAME Store recovery metadata in database file NAME", |
| 15485 | 15534 | " --lost-and-found TABLE Alternative name for the lost-and-found table", |
| 15486 | 15535 | " --no-rowids Do not attempt to recover rowid values", |
| 15487 | 15536 | " that are not also INTEGER PRIMARY KEYs", |
| 15488 | 15537 | #endif |
| 15538 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15489 | 15539 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE", |
| 15490 | 15540 | ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)", |
| 15541 | +#endif |
| 15491 | 15542 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off", |
| 15492 | 15543 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN", |
| 15493 | 15544 | " Options:", |
| 15494 | 15545 | " --indent Try to pretty-print the schema", |
| 15495 | 15546 | " --nosys Omit objects whose names start with \"sqlite_\"", |
| | @@ -15519,24 +15570,26 @@ |
| 15519 | 15570 | " --sha3-224 Use the sha3-224 algorithm", |
| 15520 | 15571 | " --sha3-256 Use the sha3-256 algorithm (default)", |
| 15521 | 15572 | " --sha3-384 Use the sha3-384 algorithm", |
| 15522 | 15573 | " --sha3-512 Use the sha3-512 algorithm", |
| 15523 | 15574 | " Any other argument is a LIKE pattern for tables to hash", |
| 15524 | | -#ifndef SQLITE_NOHAVE_SYSTEM |
| 15575 | +#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE) |
| 15525 | 15576 | ".shell CMD ARGS... Run CMD ARGS... in a system shell", |
| 15526 | 15577 | #endif |
| 15527 | 15578 | ".show Show the current values for various settings", |
| 15528 | 15579 | ".stats ?ARG? Show stats or turn stats on or off", |
| 15529 | 15580 | " off Turn off automatic stat display", |
| 15530 | 15581 | " on Turn on automatic stat display", |
| 15531 | 15582 | " stmt Show statement stats", |
| 15532 | 15583 | " vmstep Show the virtual machine step count only", |
| 15533 | | -#ifndef SQLITE_NOHAVE_SYSTEM |
| 15584 | +#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE) |
| 15534 | 15585 | ".system CMD ARGS... Run CMD ARGS... in a system shell", |
| 15535 | 15586 | #endif |
| 15536 | 15587 | ".tables ?TABLE? List names of tables matching LIKE pattern TABLE", |
| 15588 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 15537 | 15589 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'", |
| 15590 | +#endif |
| 15538 | 15591 | ".testctrl CMD ... Run various sqlite3_test_control() operations", |
| 15539 | 15592 | " Run \".testctrl\" with no arguments for details", |
| 15540 | 15593 | ".timeout MS Try opening locked tables for MS milliseconds", |
| 15541 | 15594 | ".timer on|off Turn SQL timer on or off", |
| 15542 | 15595 | #ifndef SQLITE_OMIT_TRACE |
| | @@ -16077,18 +16130,20 @@ |
| 16077 | 16130 | exit(1); |
| 16078 | 16131 | } |
| 16079 | 16132 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 16080 | 16133 | sqlite3_enable_load_extension(p->db, 1); |
| 16081 | 16134 | #endif |
| 16082 | | - sqlite3_fileio_init(p->db, 0, 0); |
| 16083 | 16135 | sqlite3_shathree_init(p->db, 0, 0); |
| 16084 | | - sqlite3_completion_init(p->db, 0, 0); |
| 16085 | 16136 | sqlite3_uint_init(p->db, 0, 0); |
| 16086 | 16137 | sqlite3_decimal_init(p->db, 0, 0); |
| 16087 | 16138 | sqlite3_regexp_init(p->db, 0, 0); |
| 16088 | 16139 | sqlite3_ieee_init(p->db, 0, 0); |
| 16089 | 16140 | sqlite3_series_init(p->db, 0, 0); |
| 16141 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 16142 | + sqlite3_fileio_init(p->db, 0, 0); |
| 16143 | + sqlite3_completion_init(p->db, 0, 0); |
| 16144 | +#endif |
| 16090 | 16145 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
| 16091 | 16146 | sqlite3_dbdata_init(p->db, 0, 0); |
| 16092 | 16147 | #endif |
| 16093 | 16148 | #ifdef SQLITE_HAVE_ZLIB |
| 16094 | 16149 | if( !p->bSafeModePersist ){ |
| | @@ -19207,18 +19262,20 @@ |
| 19207 | 19262 | sqlite3_set_authorizer(p->db, 0, 0); |
| 19208 | 19263 | } |
| 19209 | 19264 | }else |
| 19210 | 19265 | #endif |
| 19211 | 19266 | |
| 19212 | | -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
| 19267 | +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \ |
| 19268 | + && !defined(SQLITE_SHELL_WASM_MODE) |
| 19213 | 19269 | if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){ |
| 19214 | 19270 | open_db(p, 0); |
| 19215 | 19271 | failIfSafeMode(p, "cannot run .archive in safe mode"); |
| 19216 | 19272 | rc = arDotCommand(p, 0, azArg, nArg); |
| 19217 | 19273 | }else |
| 19218 | 19274 | #endif |
| 19219 | 19275 | |
| 19276 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 19220 | 19277 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 19221 | 19278 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 19222 | 19279 | ){ |
| 19223 | 19280 | const char *zDestFile = 0; |
| 19224 | 19281 | const char *zDb = 0; |
| | @@ -19283,10 +19340,11 @@ |
| 19283 | 19340 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 19284 | 19341 | rc = 1; |
| 19285 | 19342 | } |
| 19286 | 19343 | close_db(pDest); |
| 19287 | 19344 | }else |
| 19345 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 19288 | 19346 | |
| 19289 | 19347 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 19290 | 19348 | if( nArg==2 ){ |
| 19291 | 19349 | bail_on_error = booleanValue(azArg[1]); |
| 19292 | 19350 | }else{ |
| | @@ -19313,10 +19371,11 @@ |
| 19313 | 19371 | */ |
| 19314 | 19372 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 19315 | 19373 | test_breakpoint(); |
| 19316 | 19374 | }else |
| 19317 | 19375 | |
| 19376 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 19318 | 19377 | if( c=='c' && strcmp(azArg[0],"cd")==0 ){ |
| 19319 | 19378 | failIfSafeMode(p, "cannot run .cd in safe mode"); |
| 19320 | 19379 | if( nArg==2 ){ |
| 19321 | 19380 | #if defined(_WIN32) || defined(WIN32) |
| 19322 | 19381 | wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
| | @@ -19332,10 +19391,11 @@ |
| 19332 | 19391 | }else{ |
| 19333 | 19392 | raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
| 19334 | 19393 | rc = 1; |
| 19335 | 19394 | } |
| 19336 | 19395 | }else |
| 19396 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 19337 | 19397 | |
| 19338 | 19398 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 19339 | 19399 | if( nArg==2 ){ |
| 19340 | 19400 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
| 19341 | 19401 | }else{ |
| | @@ -19342,10 +19402,11 @@ |
| 19342 | 19402 | raw_printf(stderr, "Usage: .changes on|off\n"); |
| 19343 | 19403 | rc = 1; |
| 19344 | 19404 | } |
| 19345 | 19405 | }else |
| 19346 | 19406 | |
| 19407 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 19347 | 19408 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 19348 | 19409 | ** Then read the content of the testcase-out.txt file and compare against |
| 19349 | 19410 | ** azArg[1]. If there are differences, report an error and exit. |
| 19350 | 19411 | */ |
| 19351 | 19412 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| | @@ -19366,20 +19427,23 @@ |
| 19366 | 19427 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
| 19367 | 19428 | p->nCheck++; |
| 19368 | 19429 | } |
| 19369 | 19430 | sqlite3_free(zRes); |
| 19370 | 19431 | }else |
| 19432 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 19371 | 19433 | |
| 19434 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 19372 | 19435 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 19373 | 19436 | failIfSafeMode(p, "cannot run .clone in safe mode"); |
| 19374 | 19437 | if( nArg==2 ){ |
| 19375 | 19438 | tryToClone(p, azArg[1]); |
| 19376 | 19439 | }else{ |
| 19377 | 19440 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
| 19378 | 19441 | rc = 1; |
| 19379 | 19442 | } |
| 19380 | 19443 | }else |
| 19444 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 19381 | 19445 | |
| 19382 | 19446 | if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){ |
| 19383 | 19447 | if( nArg==1 ){ |
| 19384 | 19448 | /* List available connections */ |
| 19385 | 19449 | int i; |
| | @@ -19664,14 +19728,16 @@ |
| 19664 | 19728 | raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n"); |
| 19665 | 19729 | rc = 1; |
| 19666 | 19730 | } |
| 19667 | 19731 | }else |
| 19668 | 19732 | |
| 19733 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 19669 | 19734 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
| 19670 | 19735 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
| 19671 | 19736 | rc = 2; |
| 19672 | 19737 | }else |
| 19738 | +#endif |
| 19673 | 19739 | |
| 19674 | 19740 | /* The ".explain" command is automatic now. It is largely pointless. It |
| 19675 | 19741 | ** retained purely for backwards compatibility */ |
| 19676 | 19742 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
| 19677 | 19743 | int val = 1; |
| | @@ -19922,10 +19988,11 @@ |
| 19922 | 19988 | }else{ |
| 19923 | 19989 | showHelp(p->out, 0); |
| 19924 | 19990 | } |
| 19925 | 19991 | }else |
| 19926 | 19992 | |
| 19993 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 19927 | 19994 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
| 19928 | 19995 | char *zTable = 0; /* Insert data into this table */ |
| 19929 | 19996 | char *zSchema = 0; /* within this schema (may default to "main") */ |
| 19930 | 19997 | char *zFile = 0; /* Name of file to extra content from */ |
| 19931 | 19998 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
| | @@ -20212,10 +20279,11 @@ |
| 20212 | 20279 | utf8_printf(p->out, |
| 20213 | 20280 | "Added %d rows with %d errors using %d lines of input\n", |
| 20214 | 20281 | sCtx.nRow, sCtx.nErr, sCtx.nLine-1); |
| 20215 | 20282 | } |
| 20216 | 20283 | }else |
| 20284 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 20217 | 20285 | |
| 20218 | 20286 | #ifndef SQLITE_UNTESTABLE |
| 20219 | 20287 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 20220 | 20288 | char *zSql; |
| 20221 | 20289 | char *zCollist = 0; |
| | @@ -20401,11 +20469,11 @@ |
| 20401 | 20469 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
| 20402 | 20470 | open_db(p, 0); |
| 20403 | 20471 | lintDotCommand(p, azArg, nArg); |
| 20404 | 20472 | }else |
| 20405 | 20473 | |
| 20406 | | -#ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 20474 | +#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE) |
| 20407 | 20475 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
| 20408 | 20476 | const char *zFile, *zProc; |
| 20409 | 20477 | char *zErrMsg = 0; |
| 20410 | 20478 | failIfSafeMode(p, "cannot run .load in safe mode"); |
| 20411 | 20479 | if( nArg<2 ){ |
| | @@ -20423,10 +20491,11 @@ |
| 20423 | 20491 | rc = 1; |
| 20424 | 20492 | } |
| 20425 | 20493 | }else |
| 20426 | 20494 | #endif |
| 20427 | 20495 | |
| 20496 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 20428 | 20497 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 20429 | 20498 | failIfSafeMode(p, "cannot run .log in safe mode"); |
| 20430 | 20499 | if( nArg!=2 ){ |
| 20431 | 20500 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
| 20432 | 20501 | rc = 1; |
| | @@ -20434,10 +20503,11 @@ |
| 20434 | 20503 | const char *zFile = azArg[1]; |
| 20435 | 20504 | output_file_close(p->pLog); |
| 20436 | 20505 | p->pLog = output_file_open(zFile, 0); |
| 20437 | 20506 | } |
| 20438 | 20507 | }else |
| 20508 | +#endif |
| 20439 | 20509 | |
| 20440 | 20510 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 20441 | 20511 | const char *zMode = 0; |
| 20442 | 20512 | const char *zTabname = 0; |
| 20443 | 20513 | int i, n2; |
| | @@ -20558,10 +20628,11 @@ |
| 20558 | 20628 | rc = 1; |
| 20559 | 20629 | } |
| 20560 | 20630 | p->cMode = p->mode; |
| 20561 | 20631 | }else |
| 20562 | 20632 | |
| 20633 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 20563 | 20634 | if( c=='n' && strcmp(azArg[0], "nonce")==0 ){ |
| 20564 | 20635 | if( nArg!=2 ){ |
| 20565 | 20636 | raw_printf(stderr, "Usage: .nonce NONCE\n"); |
| 20566 | 20637 | rc = 1; |
| 20567 | 20638 | }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){ |
| | @@ -20572,10 +20643,11 @@ |
| 20572 | 20643 | p->bSafeMode = 0; |
| 20573 | 20644 | return 0; /* Return immediately to bypass the safe mode reset |
| 20574 | 20645 | ** at the end of this procedure */ |
| 20575 | 20646 | } |
| 20576 | 20647 | }else |
| 20648 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 20577 | 20649 | |
| 20578 | 20650 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 20579 | 20651 | if( nArg==2 ){ |
| 20580 | 20652 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 20581 | 20653 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
| | @@ -20593,10 +20665,11 @@ |
| 20593 | 20665 | int openMode = SHELL_OPEN_UNSPEC; |
| 20594 | 20666 | |
| 20595 | 20667 | /* Check for command-line arguments */ |
| 20596 | 20668 | for(iName=1; iName<nArg; iName++){ |
| 20597 | 20669 | const char *z = azArg[iName]; |
| 20670 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 20598 | 20671 | if( optionMatch(z,"new") ){ |
| 20599 | 20672 | newFlag = 1; |
| 20600 | 20673 | #ifdef SQLITE_HAVE_ZLIB |
| 20601 | 20674 | }else if( optionMatch(z, "zip") ){ |
| 20602 | 20675 | openMode = SHELL_OPEN_ZIPFILE; |
| | @@ -20613,11 +20686,13 @@ |
| 20613 | 20686 | }else if( optionMatch(z, "hexdb") ){ |
| 20614 | 20687 | openMode = SHELL_OPEN_HEXDB; |
| 20615 | 20688 | }else if( optionMatch(z, "maxsize") && iName+1<nArg ){ |
| 20616 | 20689 | p->szMax = integerValue(azArg[++iName]); |
| 20617 | 20690 | #endif /* SQLITE_OMIT_DESERIALIZE */ |
| 20618 | | - }else if( z[0]=='-' ){ |
| 20691 | + }else |
| 20692 | +#endif /* !SQLITE_SHELL_WASM_MODE */ |
| 20693 | + if( z[0]=='-' ){ |
| 20619 | 20694 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 20620 | 20695 | rc = 1; |
| 20621 | 20696 | goto meta_command_exit; |
| 20622 | 20697 | }else if( zFN ){ |
| 20623 | 20698 | utf8_printf(stderr, "extra argument: \"%s\"\n", z); |
| | @@ -20640,17 +20715,21 @@ |
| 20640 | 20715 | p->szMax = 0; |
| 20641 | 20716 | |
| 20642 | 20717 | /* If a filename is specified, try to open it first */ |
| 20643 | 20718 | if( zFN || p->openMode==SHELL_OPEN_HEXDB ){ |
| 20644 | 20719 | if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN); |
| 20720 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 20645 | 20721 | if( p->bSafeMode |
| 20646 | 20722 | && p->openMode!=SHELL_OPEN_HEXDB |
| 20647 | 20723 | && zFN |
| 20648 | 20724 | && strcmp(zFN,":memory:")!=0 |
| 20649 | 20725 | ){ |
| 20650 | 20726 | failIfSafeMode(p, "cannot open disk-based database files in safe mode"); |
| 20651 | 20727 | } |
| 20728 | +#else |
| 20729 | + /* WASM mode has its own sandboxed pseudo-filesystem. */ |
| 20730 | +#endif |
| 20652 | 20731 | if( zFN ){ |
| 20653 | 20732 | zNewFilename = sqlite3_mprintf("%s", zFN); |
| 20654 | 20733 | shell_check_oom(zNewFilename); |
| 20655 | 20734 | }else{ |
| 20656 | 20735 | zNewFilename = 0; |
| | @@ -20669,10 +20748,11 @@ |
| 20669 | 20748 | p->pAuxDb->zDbFilename = 0; |
| 20670 | 20749 | open_db(p, 0); |
| 20671 | 20750 | } |
| 20672 | 20751 | }else |
| 20673 | 20752 | |
| 20753 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 20674 | 20754 | if( (c=='o' |
| 20675 | 20755 | && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0)) |
| 20676 | 20756 | || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0) |
| 20677 | 20757 | ){ |
| 20678 | 20758 | char *zFile = 0; |
| | @@ -20784,10 +20864,11 @@ |
| 20784 | 20864 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 20785 | 20865 | } |
| 20786 | 20866 | } |
| 20787 | 20867 | sqlite3_free(zFile); |
| 20788 | 20868 | }else |
| 20869 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 20789 | 20870 | |
| 20790 | 20871 | if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){ |
| 20791 | 20872 | open_db(p,0); |
| 20792 | 20873 | if( nArg<=1 ) goto parameter_syntax_error; |
| 20793 | 20874 | |
| | @@ -20953,14 +21034,17 @@ |
| 20953 | 21034 | if( nArg >= 3) { |
| 20954 | 21035 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 20955 | 21036 | } |
| 20956 | 21037 | }else |
| 20957 | 21038 | |
| 21039 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 20958 | 21040 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
| 20959 | 21041 | rc = 2; |
| 20960 | 21042 | }else |
| 21043 | +#endif |
| 20961 | 21044 | |
| 21045 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 20962 | 21046 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 20963 | 21047 | FILE *inSaved = p->in; |
| 20964 | 21048 | int savedLineno = p->lineno; |
| 20965 | 21049 | failIfSafeMode(p, "cannot run .read in safe mode"); |
| 20966 | 21050 | if( nArg!=2 ){ |
| | @@ -20991,11 +21075,13 @@ |
| 20991 | 21075 | fclose(p->in); |
| 20992 | 21076 | } |
| 20993 | 21077 | p->in = inSaved; |
| 20994 | 21078 | p->lineno = savedLineno; |
| 20995 | 21079 | }else |
| 21080 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 20996 | 21081 | |
| 21082 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 20997 | 21083 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
| 20998 | 21084 | const char *zSrcFile; |
| 20999 | 21085 | const char *zDb; |
| 21000 | 21086 | sqlite3 *pSrc; |
| 21001 | 21087 | sqlite3_backup *pBackup; |
| | @@ -21043,10 +21129,11 @@ |
| 21043 | 21129 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 21044 | 21130 | rc = 1; |
| 21045 | 21131 | } |
| 21046 | 21132 | close_db(pSrc); |
| 21047 | 21133 | }else |
| 21134 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 21048 | 21135 | |
| 21049 | 21136 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 21050 | 21137 | if( nArg==2 ){ |
| 21051 | 21138 | p->scanstatsOn = (u8)booleanValue(azArg[1]); |
| 21052 | 21139 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| | @@ -21668,11 +21755,11 @@ |
| 21668 | 21755 | shell_exec(p, zSql, 0); |
| 21669 | 21756 | } |
| 21670 | 21757 | sqlite3_free(zSql); |
| 21671 | 21758 | }else |
| 21672 | 21759 | |
| 21673 | | -#ifndef SQLITE_NOHAVE_SYSTEM |
| 21760 | +#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE) |
| 21674 | 21761 | if( c=='s' |
| 21675 | 21762 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
| 21676 | 21763 | ){ |
| 21677 | 21764 | char *zCmd; |
| 21678 | 21765 | int i, x; |
| | @@ -21689,11 +21776,11 @@ |
| 21689 | 21776 | } |
| 21690 | 21777 | x = zCmd!=0 ? system(zCmd) : 1; |
| 21691 | 21778 | sqlite3_free(zCmd); |
| 21692 | 21779 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
| 21693 | 21780 | }else |
| 21694 | | -#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
| 21781 | +#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE) */ |
| 21695 | 21782 | |
| 21696 | 21783 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
| 21697 | 21784 | static const char *azBool[] = { "off", "on", "trigger", "full"}; |
| 21698 | 21785 | const char *zOut; |
| 21699 | 21786 | int i; |
| | @@ -21869,10 +21956,11 @@ |
| 21869 | 21956 | |
| 21870 | 21957 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 21871 | 21958 | sqlite3_free(azResult); |
| 21872 | 21959 | }else |
| 21873 | 21960 | |
| 21961 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 21874 | 21962 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 21875 | 21963 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 21876 | 21964 | output_reset(p); |
| 21877 | 21965 | p->out = output_file_open("testcase-out.txt", 0); |
| 21878 | 21966 | if( p->out==0 ){ |
| | @@ -21882,10 +21970,11 @@ |
| 21882 | 21970 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 21883 | 21971 | }else{ |
| 21884 | 21972 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 21885 | 21973 | } |
| 21886 | 21974 | }else |
| 21975 | +#endif /* !defined(SQLITE_SHELL_WASM_MODE) */ |
| 21887 | 21976 | |
| 21888 | 21977 | #ifndef SQLITE_UNTESTABLE |
| 21889 | 21978 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){ |
| 21890 | 21979 | static const struct { |
| 21891 | 21980 | const char *zCtrlName; /* Name of a test-control option */ |
| | @@ -22553,10 +22642,43 @@ |
| 22553 | 22642 | |
| 22554 | 22643 | static void echo_group_input(ShellState *p, const char *zDo){ |
| 22555 | 22644 | if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo); |
| 22556 | 22645 | } |
| 22557 | 22646 | |
| 22647 | +#ifdef SQLITE_SHELL_WASM_MODE |
| 22648 | +/* |
| 22649 | +** Alternate one_input_line() impl for wasm mode. This is not in the primary impl |
| 22650 | +** because we need the global shellState and cannot access it from that function |
| 22651 | +** without moving lots of code around (creating a larger/messier diff). |
| 22652 | +*/ |
| 22653 | +static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
| 22654 | + /* Parse the next line from shellState.wasm.zInput. */ |
| 22655 | + const char *zBegin = shellState.wasm.zPos; |
| 22656 | + const char *z = zBegin; |
| 22657 | + char *zLine = 0; |
| 22658 | + int nZ = 0; |
| 22659 | + |
| 22660 | + UNUSED_PARAMETER(in); |
| 22661 | + UNUSED_PARAMETER(isContinuation); |
| 22662 | + if(!z || !*z){ |
| 22663 | + return 0; |
| 22664 | + } |
| 22665 | + while(*z && isspace(*z)) ++z; |
| 22666 | + zBegin = z; |
| 22667 | + for(; *z && '\n'!=*z; ++nZ, ++z){} |
| 22668 | + if(nZ>0 && '\r'==zBegin[nZ-1]){ |
| 22669 | + --nZ; |
| 22670 | + } |
| 22671 | + shellState.wasm.zPos = z; |
| 22672 | + zLine = realloc(zPrior, nZ+1); |
| 22673 | + shell_check_oom(zLine); |
| 22674 | + memcpy(zLine, zBegin, (size_t)nZ); |
| 22675 | + zLine[nZ] = 0; |
| 22676 | + return zLine; |
| 22677 | +} |
| 22678 | +#endif /* SQLITE_SHELL_WASM_MODE */ |
| 22679 | + |
| 22558 | 22680 | /* |
| 22559 | 22681 | ** Read input from *in and process it. If *in==0 then input |
| 22560 | 22682 | ** is interactive - the user is typing it it. Otherwise, input |
| 22561 | 22683 | ** is coming from a file or device. A prompt is issued and history |
| 22562 | 22684 | ** is saved only if input is interactive. An interrupt signal will |
| | @@ -22933,10 +23055,14 @@ |
| 22933 | 23055 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 22934 | 23056 | # else |
| 22935 | 23057 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 22936 | 23058 | # endif |
| 22937 | 23059 | #endif |
| 23060 | + |
| 23061 | +#ifdef SQLITE_SHELL_WASM_MODE |
| 23062 | +# define main fiddle_main |
| 23063 | +#endif |
| 22938 | 23064 | |
| 22939 | 23065 | #if SQLITE_SHELL_IS_UTF8 |
| 22940 | 23066 | int SQLITE_CDECL main(int argc, char **argv){ |
| 22941 | 23067 | #else |
| 22942 | 23068 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
| | @@ -22944,11 +23070,15 @@ |
| 22944 | 23070 | #endif |
| 22945 | 23071 | #ifdef SQLITE_DEBUG |
| 22946 | 23072 | sqlite3_uint64 mem_main_enter = sqlite3_memory_used(); |
| 22947 | 23073 | #endif |
| 22948 | 23074 | char *zErrMsg = 0; |
| 23075 | +#ifdef SQLITE_SHELL_WASM_MODE |
| 23076 | +# define data shellState |
| 23077 | +#else |
| 22949 | 23078 | ShellState data; |
| 23079 | +#endif |
| 22950 | 23080 | const char *zInitFile = 0; |
| 22951 | 23081 | int i; |
| 22952 | 23082 | int rc = 0; |
| 22953 | 23083 | int warnInmemoryDb = 0; |
| 22954 | 23084 | int readStdin = 1; |
| | @@ -22960,12 +23090,17 @@ |
| 22960 | 23090 | int argcToFree = 0; |
| 22961 | 23091 | #endif |
| 22962 | 23092 | |
| 22963 | 23093 | setBinaryMode(stdin, 0); |
| 22964 | 23094 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
| 23095 | +#ifdef SQLITE_SHELL_WASM_MODE |
| 23096 | + stdin_is_interactive = 0; |
| 23097 | + stdout_is_console = 1; |
| 23098 | +#else |
| 22965 | 23099 | stdin_is_interactive = isatty(0); |
| 22966 | 23100 | stdout_is_console = isatty(1); |
| 23101 | +#endif |
| 22967 | 23102 | |
| 22968 | 23103 | #if !defined(_WIN32_WCE) |
| 22969 | 23104 | if( getenv("SQLITE_DEBUG_BREAK") ){ |
| 22970 | 23105 | if( isatty(0) && isatty(2) ){ |
| 22971 | 23106 | fprintf(stderr, |
| | @@ -23217,11 +23352,13 @@ |
| 23217 | 23352 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
| 23218 | 23353 | return 1; |
| 23219 | 23354 | #endif |
| 23220 | 23355 | } |
| 23221 | 23356 | data.out = stdout; |
| 23357 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 23222 | 23358 | sqlite3_appendvfs_init(0,0,0); |
| 23359 | +#endif |
| 23223 | 23360 | |
| 23224 | 23361 | /* Go ahead and open the database file if it already exists. If the |
| 23225 | 23362 | ** file does not exist, delay opening it. This prevents empty database |
| 23226 | 23363 | ** files from being created if a user mistypes the database name argument |
| 23227 | 23364 | ** to the sqlite command-line tool. |
| | @@ -23483,10 +23620,13 @@ |
| 23483 | 23620 | }else{ |
| 23484 | 23621 | data.in = stdin; |
| 23485 | 23622 | rc = process_input(&data); |
| 23486 | 23623 | } |
| 23487 | 23624 | } |
| 23625 | +#ifndef SQLITE_SHELL_WASM_MODE |
| 23626 | + /* In WASM mode we have to leave the db state in place so that |
| 23627 | + ** client code can "push" SQL into it after this call returns. */ |
| 23488 | 23628 | free(azCmd); |
| 23489 | 23629 | set_table_name(&data, 0); |
| 23490 | 23630 | if( data.db ){ |
| 23491 | 23631 | session_close_all(&data, -1); |
| 23492 | 23632 | close_db(data.db); |
| | @@ -23515,7 +23655,110 @@ |
| 23515 | 23655 | if( sqlite3_memory_used()>mem_main_enter ){ |
| 23516 | 23656 | utf8_printf(stderr, "Memory leaked: %u bytes\n", |
| 23517 | 23657 | (unsigned int)(sqlite3_memory_used()-mem_main_enter)); |
| 23518 | 23658 | } |
| 23519 | 23659 | #endif |
| 23660 | +#endif /* !SQLITE_SHELL_WASM_MODE */ |
| 23520 | 23661 | return rc; |
| 23521 | 23662 | } |
| 23663 | + |
| 23664 | + |
| 23665 | +#ifdef SQLITE_SHELL_WASM_MODE |
| 23666 | +/* Only for emcc experimentation purposes. */ |
| 23667 | +int fiddle_experiment(int a,int b){ |
| 23668 | + return a + b; |
| 23669 | +} |
| 23670 | + |
| 23671 | +/* Only for emcc experimentation purposes. |
| 23672 | + |
| 23673 | + Define this function in JS using: |
| 23674 | + |
| 23675 | + emcc ... --js-library somefile.js |
| 23676 | + |
| 23677 | + containing: |
| 23678 | + |
| 23679 | +mergeInto(LibraryManager.library, { |
| 23680 | + my_foo: function(){ |
| 23681 | + console.debug("my_foo()",arguments); |
| 23682 | + } |
| 23683 | +}); |
| 23684 | +*/ |
| 23685 | +/*extern void my_foo(sqlite3 *);*/ |
| 23686 | +/* Only for emcc experimentation purposes. */ |
| 23687 | +sqlite3 * fiddle_the_db(){ |
| 23688 | + printf("fiddle_the_db(%p)\n", (const void*)globalDb); |
| 23689 | + /*my_foo(globalDb);*/ |
| 23690 | + return globalDb; |
| 23691 | +} |
| 23692 | +/* Only for emcc experimentation purposes. */ |
| 23693 | +sqlite3 * fiddle_db_arg(sqlite3 *arg){ |
| 23694 | + printf("fiddle_db_arg(%p)\n", (const void*)arg); |
| 23695 | + return arg; |
| 23696 | +} |
| 23697 | + |
| 23698 | +/* |
| 23699 | +** Intended to be called via a SharedWorker() while a separate |
| 23700 | +** SharedWorker() (which manages the wasm module) is performing work |
| 23701 | +** which should be interrupted. Unfortunately, SharedWorker is not |
| 23702 | +** portable enough to make real use of. |
| 23703 | +*/ |
| 23704 | +void fiddle_interrupt(void){ |
| 23705 | + if(globalDb) sqlite3_interrupt(globalDb); |
| 23706 | +} |
| 23707 | + |
| 23708 | +/* |
| 23709 | +** Returns the filename of the given db name, assuming "main" if |
| 23710 | +** zDbName is NULL. Returns NULL if globalDb is not opened. |
| 23711 | +*/ |
| 23712 | +const char * fiddle_db_filename(const char * zDbName){ |
| 23713 | + return globalDb |
| 23714 | + ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main") |
| 23715 | + : NULL; |
| 23716 | +} |
| 23717 | + |
| 23718 | +/* |
| 23719 | +** Trivial exportable function for emscripten. Needs to be exported using: |
| 23720 | +** |
| 23721 | +** emcc ..flags... -sEXPORTED_FUNCTIONS=_fiddle_exec -sEXPORTED_RUNTIME_METHODS=ccall,cwrap |
| 23722 | +** |
| 23723 | +** (Note the underscore before the function name.) It processes zSql |
| 23724 | +** as if it were input to the sqlite3 shell and redirects all output |
| 23725 | +** to the wasm binding. |
| 23726 | +*/ |
| 23727 | +void fiddle_exec(const char * zSql){ |
| 23728 | + static int once = 0; |
| 23729 | + int rc = 0; |
| 23730 | + if(!once){ |
| 23731 | + /* Simulate an argv array for main() */ |
| 23732 | + static char * argv[] = {"fiddle", |
| 23733 | + "-bail", |
| 23734 | + "-safe"}; |
| 23735 | + rc = fiddle_main((int)(sizeof(argv)/sizeof(argv[0])), argv); |
| 23736 | + once = rc ? -1 : 1; |
| 23737 | + memset(&shellState.wasm, 0, sizeof(shellState.wasm)); |
| 23738 | + printf( |
| 23739 | + "SQLite version %s %.19s\n" /*extra-version-info*/, |
| 23740 | + sqlite3_libversion(), sqlite3_sourceid() |
| 23741 | + ); |
| 23742 | + puts("WASM shell"); |
| 23743 | + puts("Enter \".help\" for usage hints."); |
| 23744 | + if(once>0){ |
| 23745 | + shellState.pAuxDb->zDbFilename = "/fiddle.sqlite3"; |
| 23746 | + open_db(&shellState, 0); |
| 23747 | + } |
| 23748 | + if(shellState.db){ |
| 23749 | + printf("Connected to %s.\n", fiddle_db_filename(NULL)); |
| 23750 | + }else{ |
| 23751 | + fprintf(stderr,"ERROR initializing db!\n"); |
| 23752 | + return; |
| 23753 | + } |
| 23754 | + } |
| 23755 | + if(once<0){ |
| 23756 | + puts("DB init failed. Not executing SQL."); |
| 23757 | + }else if(zSql && *zSql){ |
| 23758 | + shellState.wasm.zInput = zSql; |
| 23759 | + shellState.wasm.zPos = zSql; |
| 23760 | + process_input(&shellState); |
| 23761 | + memset(&shellState.wasm, 0, sizeof(shellState.wasm)); |
| 23762 | + } |
| 23763 | +} |
| 23764 | +#endif /* SQLITE_SHELL_WASM_MODE */ |
| 23522 | 23765 | |