sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / misc / fileio.c
| 1 | /* |
| 2 | ** 2014-06-13 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** This SQLite extension implements SQL functions readfile() and |
| 14 | ** writefile(), and eponymous virtual type "fsdir". |
| 15 | ** |
| 16 | ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]): |
| 17 | ** |
| 18 | ** If neither of the optional arguments is present, then this UDF |
| 19 | ** function writes blob DATA to file FILE. If successful, the number |
| 20 | ** of bytes written is returned. If an error occurs, NULL is returned. |
| 21 | ** |
| 22 | ** If the first option argument - MODE - is present, then it must |
| 23 | ** be passed an integer value that corresponds to a POSIX mode |
| 24 | ** value (file type + permissions, as returned in the stat.st_mode |
| 25 | ** field by the stat() system call). Three types of files may |
| 26 | ** be written/created: |
| 27 | ** |
| 28 | ** regular files: (mode & 0170000)==0100000 |
| 29 | ** symbolic links: (mode & 0170000)==0120000 |
| 30 | ** directories: (mode & 0170000)==0040000 |
| 31 | ** |
| 32 | ** For a directory, the DATA is ignored. For a symbolic link, it is |
| 33 | ** interpreted as text and used as the target of the link. For a |
| 34 | ** regular file, it is interpreted as a blob and written into the |
| 35 | ** named file. Regardless of the type of file, its permissions are |
| 36 | ** set to (mode & 0777) before returning. |
| 37 | ** |
| 38 | ** If the optional MTIME argument is present, then it is interpreted |
| 39 | ** as an integer - the number of seconds since the unix epoch. The |
| 40 | ** modification-time of the target file is set to this value before |
| 41 | ** returning. |
| 42 | ** |
| 43 | ** If five or more arguments are passed to this function and an |
| 44 | ** error is encountered, an exception is raised. |
| 45 | ** |
| 46 | ** READFILE(FILE): |
| 47 | ** |
| 48 | ** Read and return the contents of file FILE (type blob) from disk. |
| 49 | ** |
| 50 | ** FSDIR: |
| 51 | ** |
| 52 | ** Used as follows: |
| 53 | ** |
| 54 | ** SELECT * FROM fsdir($path [, $dir]); |
| 55 | ** |
| 56 | ** Parameter $path is an absolute or relative pathname. If the file that it |
| 57 | ** refers to does not exist, it is an error. If the path refers to a regular |
| 58 | ** file or symbolic link, it returns a single row. Or, if the path refers |
| 59 | ** to a directory, it returns one row for the directory, and one row for each |
| 60 | ** file within the hierarchy rooted at $path. |
| 61 | ** |
| 62 | ** Each row has the following columns: |
| 63 | ** |
| 64 | ** name: Path to file or directory (text value). |
| 65 | ** mode: Value of stat.st_mode for directory entry (an integer). |
| 66 | ** mtime: Value of stat.st_mtime for directory entry (an integer). |
| 67 | ** data: For a regular file, a blob containing the file data. For a |
| 68 | ** symlink, a text value containing the text of the link. For a |
| 69 | ** directory, NULL. |
| 70 | ** level: Directory hierarchy level. Topmost is 1. |
| 71 | ** |
| 72 | ** If a non-NULL value is specified for the optional $dir parameter and |
| 73 | ** $path is a relative path, then $path is interpreted relative to $dir. |
| 74 | ** And the paths returned in the "name" column of the table are also |
| 75 | ** relative to directory $dir. |
| 76 | */ |
| 77 | #include "sqlite3ext.h" |
| 78 | SQLITE_EXTENSION_INIT1 |
| 79 | #include <stdio.h> |
| 80 | #include <string.h> |
| 81 | #include <assert.h> |
| 82 | |
| 83 | #include <sys/types.h> |
| 84 | #include <sys/stat.h> |
| 85 | #include <fcntl.h> |
| 86 | #if !defined(_WIN32) && !defined(WIN32) |
| 87 | # include <unistd.h> |
| 88 | # include <dirent.h> |
| 89 | # include <utime.h> |
| 90 | # include <sys/time.h> |
| 91 | # define STRUCT_STAT struct stat |
| 92 | # include <limits.h> |
| 93 | # include <stdlib.h> |
| 94 | #else |
| 95 | # include "windirent.h" |
| 96 | # include <direct.h> |
| 97 | # define STRUCT_STAT struct _stat |
| 98 | # define chmod(path,mode) fileio_chmod(path,mode) |
| 99 | # define mkdir(path,mode) fileio_mkdir(path) |
| 100 | #endif |
| 101 | #include <time.h> |
| 102 | #include <errno.h> |
| 103 | |
| 104 | /* When used as part of the CLI, the sqlite3_stdio.h module will have |
| 105 | ** been included before this one. In that case use the sqlite3_stdio.h |
| 106 | ** #defines. If not, create our own for fopen(). |
| 107 | */ |
| 108 | #ifndef _SQLITE3_STDIO_H_ |
| 109 | # define sqlite3_fopen fopen |
| 110 | #endif |
| 111 | |
| 112 | /* |
| 113 | ** Structure of the fsdir() table-valued function |
| 114 | */ |
| 115 | /* 0 1 2 3 4 5 6 */ |
| 116 | #define FSDIR_SCHEMA "(name,mode,mtime,data,level,path HIDDEN,dir HIDDEN)" |
| 117 | |
| 118 | #define FSDIR_COLUMN_NAME 0 /* Name of the file */ |
| 119 | #define FSDIR_COLUMN_MODE 1 /* Access mode */ |
| 120 | #define FSDIR_COLUMN_MTIME 2 /* Last modification time */ |
| 121 | #define FSDIR_COLUMN_DATA 3 /* File content */ |
| 122 | #define FSDIR_COLUMN_LEVEL 4 /* Level. Topmost is 1 */ |
| 123 | #define FSDIR_COLUMN_PATH 5 /* Path to top of search */ |
| 124 | #define FSDIR_COLUMN_DIR 6 /* Path is relative to this directory */ |
| 125 | |
| 126 | #ifdef _WIN32 |
| 127 | /* |
| 128 | ** Convert a UTF-8 string to Microsoft Unicode. |
| 129 | ** |
| 130 | ** Space to hold the returned string is obtained from sqlite3_malloc(). |
| 131 | */ |
| 132 | static wchar_t *winUtf8To16(const char *zText){ |
| 133 | int nChar; |
| 134 | wchar_t *zWideText; |
| 135 | |
| 136 | nChar = MultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0); |
| 137 | if( nChar==0 ){ |
| 138 | return 0; |
| 139 | } |
| 140 | zWideText = sqlite3_malloc64(nChar*sizeof(WCHAR) ); |
| 141 | if( zWideText==0 ){ |
| 142 | return 0; |
| 143 | } |
| 144 | nChar = MultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText, |
| 145 | nChar); |
| 146 | if( nChar==0 ){ |
| 147 | sqlite3_free(zWideText); |
| 148 | zWideText = 0; |
| 149 | } |
| 150 | return zWideText; |
| 151 | } |
| 152 | #endif /* _WIN32 */ |
| 153 | |
| 154 | #ifdef _WIN32 |
| 155 | /* |
| 156 | ** Convert a Microsoft Unicode string to UTF-8. |
| 157 | ** |
| 158 | ** Space to hold the returned string is obtained from sqlite3_malloc(). |
| 159 | */ |
| 160 | static char *winUtf16To8(wchar_t *zWideText){ |
| 161 | int nByte; |
| 162 | char *zText; |
| 163 | |
| 164 | nByte = WideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0); |
| 165 | if( nByte == 0 ){ |
| 166 | return 0; |
| 167 | } |
| 168 | zText = sqlite3_malloc64( nByte ); |
| 169 | if( zText==0 ){ |
| 170 | return 0; |
| 171 | } |
| 172 | nByte = WideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte, |
| 173 | 0, 0); |
| 174 | if( nByte == 0 ){ |
| 175 | sqlite3_free(zText); |
| 176 | zText = 0; |
| 177 | } |
| 178 | return zText; |
| 179 | } |
| 180 | #endif /* _WIN32 */ |
| 181 | |
| 182 | /* |
| 183 | ** UTF8 chmod() function for Windows |
| 184 | */ |
| 185 | #if defined(_WIN32) || defined(WIN32) |
| 186 | static int fileio_chmod(const char *zPath, int pmode){ |
| 187 | int rc; |
| 188 | wchar_t *b1 = winUtf8To16(zPath); |
| 189 | if( b1==0 ) return -1; |
| 190 | rc = _wchmod(b1, pmode); |
| 191 | sqlite3_free(b1); |
| 192 | return rc; |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | /* |
| 197 | ** UTF8 mkdir() function for Windows |
| 198 | */ |
| 199 | #if defined(_WIN32) || defined(WIN32) |
| 200 | static int fileio_mkdir(const char *zPath){ |
| 201 | int rc; |
| 202 | wchar_t *b1 = winUtf8To16(zPath); |
| 203 | if( b1==0 ) return -1; |
| 204 | rc = _wmkdir(b1); |
| 205 | sqlite3_free(b1); |
| 206 | return rc; |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | |
| 211 | /* |
| 212 | ** Set the result stored by context ctx to a blob containing the |
| 213 | ** contents of file zName. Or, leave the result unchanged (NULL) |
| 214 | ** if the file does not exist or is unreadable. |
| 215 | ** |
| 216 | ** If the file exceeds the SQLite blob size limit, through an |
| 217 | ** SQLITE_TOOBIG error. |
| 218 | ** |
| 219 | ** Throw an SQLITE_IOERR if there are difficulties pulling the file |
| 220 | ** off of disk. |
| 221 | */ |
| 222 | static void readFileContents(sqlite3_context *ctx, const char *zName){ |
| 223 | FILE *in; |
| 224 | sqlite3_int64 nIn; /* Size of the file according to ftell() */ |
| 225 | sqlite3_int64 nAlloc; /* Bytes allocated for pBuf */ |
| 226 | sqlite3_int64 nRead = 0; /* Bytes read so far */ |
| 227 | unsigned char *pBuf; |
| 228 | sqlite3 *db; |
| 229 | int mxBlob; |
| 230 | |
| 231 | in = sqlite3_fopen(zName, "rb"); |
| 232 | if( in==0 ){ |
| 233 | /* File does not exist or is unreadable. Leave the result set to NULL. */ |
| 234 | return; |
| 235 | } |
| 236 | /* The size reported by ftell() is only a hint. It is -1 for pipes and |
| 237 | ** FIFOs and 0 for many files under /proc. Keep reading until EOF. */ |
| 238 | fseek(in, 0, SEEK_END); |
| 239 | nIn = ftell(in); |
| 240 | rewind(in); |
| 241 | if( nIn<0 ) nIn = 0; |
| 242 | db = sqlite3_context_db_handle(ctx); |
| 243 | mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); |
| 244 | if( nIn>mxBlob ){ |
| 245 | sqlite3_result_error_code(ctx, SQLITE_TOOBIG); |
| 246 | fclose(in); |
| 247 | return; |
| 248 | } |
| 249 | /* One extra byte so that EOF on a regular file is seen without a |
| 250 | ** reallocation. */ |
| 251 | nAlloc = nIn+1; |
| 252 | pBuf = sqlite3_malloc64( nAlloc ); |
| 253 | if( pBuf==0 ){ |
| 254 | sqlite3_result_error_nomem(ctx); |
| 255 | fclose(in); |
| 256 | return; |
| 257 | } |
| 258 | while( 1 ){ |
| 259 | unsigned char *pNew; |
| 260 | nRead += fread(pBuf+nRead, 1, (size_t)(nAlloc-nRead), in); |
| 261 | if( nRead>mxBlob ){ |
| 262 | sqlite3_result_error_code(ctx, SQLITE_TOOBIG); |
| 263 | sqlite3_free(pBuf); |
| 264 | fclose(in); |
| 265 | return; |
| 266 | } |
| 267 | if( nRead<nAlloc ) break; /* Short read means EOF or an error */ |
| 268 | nAlloc *= 2; |
| 269 | if( nAlloc>(sqlite3_int64)mxBlob+1 ) nAlloc = (sqlite3_int64)mxBlob+1; |
| 270 | pNew = sqlite3_realloc64(pBuf, nAlloc); |
| 271 | if( pNew==0 ){ |
| 272 | sqlite3_result_error_nomem(ctx); |
| 273 | sqlite3_free(pBuf); |
| 274 | fclose(in); |
| 275 | return; |
| 276 | } |
| 277 | pBuf = pNew; |
| 278 | } |
| 279 | if( ferror(in) ){ |
| 280 | sqlite3_result_error_code(ctx, SQLITE_IOERR); |
| 281 | sqlite3_free(pBuf); |
| 282 | }else{ |
| 283 | sqlite3_result_blob64(ctx, pBuf, nRead, sqlite3_free); |
| 284 | } |
| 285 | fclose(in); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 290 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 291 | ** if the file does not exist or is unreadable. |
| 292 | */ |
| 293 | static void readfileFunc( |
| 294 | sqlite3_context *context, |
| 295 | int argc, |
| 296 | sqlite3_value **argv |
| 297 | ){ |
| 298 | const char *zName; |
| 299 | (void)(argc); /* Unused parameter */ |
| 300 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 301 | if( zName==0 ) return; |
| 302 | readFileContents(context, zName); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | ** Set the error message contained in context ctx to the results of |
| 307 | ** vprintf(zFmt, ...). |
| 308 | */ |
| 309 | static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){ |
| 310 | char *zMsg = 0; |
| 311 | va_list ap; |
| 312 | va_start(ap, zFmt); |
| 313 | zMsg = sqlite3_vmprintf(zFmt, ap); |
| 314 | sqlite3_result_error(ctx, zMsg, -1); |
| 315 | sqlite3_free(zMsg); |
| 316 | va_end(ap); |
| 317 | } |
| 318 | |
| 319 | #if defined(_WIN32) |
| 320 | /* |
| 321 | ** This function is designed to convert a Win32 FILETIME structure into the |
| 322 | ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC). |
| 323 | */ |
| 324 | static sqlite3_uint64 fileTimeToUnixTime( |
| 325 | LPFILETIME pFileTime |
| 326 | ){ |
| 327 | SYSTEMTIME epochSystemTime; |
| 328 | ULARGE_INTEGER epochIntervals; |
| 329 | FILETIME epochFileTime; |
| 330 | ULARGE_INTEGER fileIntervals; |
| 331 | |
| 332 | memset(&epochSystemTime, 0, sizeof(SYSTEMTIME)); |
| 333 | epochSystemTime.wYear = 1970; |
| 334 | epochSystemTime.wMonth = 1; |
| 335 | epochSystemTime.wDay = 1; |
| 336 | SystemTimeToFileTime(&epochSystemTime, &epochFileTime); |
| 337 | epochIntervals.LowPart = epochFileTime.dwLowDateTime; |
| 338 | epochIntervals.HighPart = epochFileTime.dwHighDateTime; |
| 339 | |
| 340 | fileIntervals.LowPart = pFileTime->dwLowDateTime; |
| 341 | fileIntervals.HighPart = pFileTime->dwHighDateTime; |
| 342 | |
| 343 | return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000; |
| 344 | } |
| 345 | #endif /* _WIN32 */ |
| 346 | |
| 347 | /* |
| 348 | ** This function is used in place of stat(). On Windows, special handling |
| 349 | ** is required in order for the included time to be returned as UTC. On all |
| 350 | ** other systems, this function simply calls stat(). |
| 351 | */ |
| 352 | static int fileStat( |
| 353 | const char *zPath, |
| 354 | STRUCT_STAT *pStatBuf |
| 355 | ){ |
| 356 | #if defined(_WIN32) |
| 357 | int rc; |
| 358 | wchar_t *b1 = winUtf8To16(zPath); |
| 359 | if( b1==0 ) return 1; |
| 360 | rc = _wstat(b1, pStatBuf); |
| 361 | if( rc==0 ){ |
| 362 | HANDLE hFindFile; |
| 363 | WIN32_FIND_DATAW fd; |
| 364 | memset(&fd, 0, sizeof(WIN32_FIND_DATAW)); |
| 365 | hFindFile = FindFirstFileW(b1, &fd); |
| 366 | if( hFindFile!=NULL ){ |
| 367 | pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime); |
| 368 | pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime); |
| 369 | pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime); |
| 370 | FindClose(hFindFile); |
| 371 | } |
| 372 | } |
| 373 | sqlite3_free(b1); |
| 374 | return rc; |
| 375 | #else |
| 376 | return stat(zPath, pStatBuf); |
| 377 | #endif |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | ** This function is used in place of lstat(). On Windows, special handling |
| 382 | ** is required in order for the included time to be returned as UTC. On all |
| 383 | ** other systems, this function simply calls lstat(). |
| 384 | */ |
| 385 | static int fileLinkStat( |
| 386 | const char *zPath, |
| 387 | STRUCT_STAT *pStatBuf |
| 388 | ){ |
| 389 | #if defined(_WIN32) |
| 390 | return fileStat(zPath, pStatBuf); |
| 391 | #else |
| 392 | return lstat(zPath, pStatBuf); |
| 393 | #endif |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | ** Argument zFile is the name of a file that will be created and/or written |
| 398 | ** by SQL function writefile(). This function ensures that the directory |
| 399 | ** zFile will be written to exists, creating it if required. The permissions |
| 400 | ** for any path components created by this function are set in accordance |
| 401 | ** with the current umask. |
| 402 | ** |
| 403 | ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise, |
| 404 | ** SQLITE_OK is returned if the directory is successfully created, or |
| 405 | ** SQLITE_ERROR otherwise. |
| 406 | */ |
| 407 | static int makeDirectory( |
| 408 | const char *zFile |
| 409 | ){ |
| 410 | char *zCopy = sqlite3_mprintf("%s", zFile); |
| 411 | int rc = SQLITE_OK; |
| 412 | |
| 413 | if( zCopy==0 ){ |
| 414 | rc = SQLITE_NOMEM; |
| 415 | }else if( zCopy[0]==0 ){ |
| 416 | rc = SQLITE_ERROR; |
| 417 | sqlite3_free(zCopy); |
| 418 | }else{ |
| 419 | int nCopy = (int)strlen(zCopy); |
| 420 | int i = 1; |
| 421 | |
| 422 | while( rc==SQLITE_OK ){ |
| 423 | STRUCT_STAT sStat; |
| 424 | int rc2; |
| 425 | |
| 426 | for(; i<nCopy && zCopy[i]!='/'; i++); |
| 427 | if( i>=nCopy ) break; |
| 428 | zCopy[i] = '\0'; |
| 429 | |
| 430 | rc2 = fileStat(zCopy, &sStat); |
| 431 | if( rc2!=0 ){ |
| 432 | if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR; |
| 433 | }else{ |
| 434 | if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR; |
| 435 | } |
| 436 | zCopy[i] = '/'; |
| 437 | i++; |
| 438 | } |
| 439 | |
| 440 | sqlite3_free(zCopy); |
| 441 | } |
| 442 | |
| 443 | return rc; |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | ** This function does the work for the writefile() UDF. Refer to |
| 448 | ** header comments at the top of this file for details. |
| 449 | */ |
| 450 | static int writeFile( |
| 451 | sqlite3_context *pCtx, /* Context to return bytes written in */ |
| 452 | const char *zFile, /* File to write */ |
| 453 | sqlite3_value *pData, /* Data to write */ |
| 454 | mode_t mode, /* MODE parameter passed to writefile() */ |
| 455 | sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */ |
| 456 | ){ |
| 457 | if( zFile==0 ) return 1; |
| 458 | #if !defined(_WIN32) && !defined(WIN32) |
| 459 | if( S_ISLNK(mode) ){ |
| 460 | const char *zTo = (const char*)sqlite3_value_text(pData); |
| 461 | if( zTo==0 ) return 1; |
| 462 | unlink(zFile); |
| 463 | if( symlink(zTo, zFile)<0 ) return 1; |
| 464 | }else |
| 465 | #endif |
| 466 | { |
| 467 | if( S_ISDIR(mode) ){ |
| 468 | if( mkdir(zFile, mode) ){ |
| 469 | /* The mkdir() call to create the directory failed. This might not |
| 470 | ** be an error though - if there is already a directory at the same |
| 471 | ** path and either the permissions already match or can be changed |
| 472 | ** to do so using chmod(), it is not an error. */ |
| 473 | STRUCT_STAT sStat; |
| 474 | if( errno!=EEXIST |
| 475 | || 0!=fileStat(zFile, &sStat) |
| 476 | || !S_ISDIR(sStat.st_mode) |
| 477 | || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777)) |
| 478 | ){ |
| 479 | return 1; |
| 480 | } |
| 481 | } |
| 482 | }else{ |
| 483 | sqlite3_int64 nWrite = 0; |
| 484 | const char *z; |
| 485 | int rc = 0; |
| 486 | FILE *out = sqlite3_fopen(zFile, "wb"); |
| 487 | if( out==0 ) return 1; |
| 488 | z = (const char*)sqlite3_value_blob(pData); |
| 489 | if( z ){ |
| 490 | sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out); |
| 491 | nWrite = sqlite3_value_bytes(pData); |
| 492 | if( nWrite!=n ){ |
| 493 | rc = 1; |
| 494 | } |
| 495 | } |
| 496 | fclose(out); |
| 497 | if( rc==0 && mode && chmod(zFile, mode & 0777) ){ |
| 498 | rc = 1; |
| 499 | } |
| 500 | if( rc ) return 2; |
| 501 | sqlite3_result_int64(pCtx, nWrite); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if( mtime>=0 ){ |
| 506 | #if defined(_WIN32) |
| 507 | /* Windows */ |
| 508 | FILETIME lastAccess; |
| 509 | FILETIME lastWrite; |
| 510 | SYSTEMTIME currentTime; |
| 511 | LONGLONG intervals; |
| 512 | HANDLE hFile; |
| 513 | LPWSTR zUnicodeName; |
| 514 | |
| 515 | GetSystemTime(¤tTime); |
| 516 | SystemTimeToFileTime(¤tTime, &lastAccess); |
| 517 | intervals = (mtime*10000000) + 116444736000000000; |
| 518 | lastWrite.dwLowDateTime = (DWORD)intervals; |
| 519 | lastWrite.dwHighDateTime = intervals >> 32; |
| 520 | zUnicodeName = winUtf8To16(zFile); |
| 521 | if( zUnicodeName==0 ){ |
| 522 | return 1; |
| 523 | } |
| 524 | hFile = CreateFileW( |
| 525 | zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, |
| 526 | FILE_FLAG_BACKUP_SEMANTICS, NULL |
| 527 | ); |
| 528 | sqlite3_free(zUnicodeName); |
| 529 | if( hFile!=INVALID_HANDLE_VALUE ){ |
| 530 | BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite); |
| 531 | CloseHandle(hFile); |
| 532 | return !bResult; |
| 533 | }else{ |
| 534 | return 1; |
| 535 | } |
| 536 | #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */ |
| 537 | /* Recent unix */ |
| 538 | struct timespec times[2]; |
| 539 | times[0].tv_nsec = times[1].tv_nsec = 0; |
| 540 | times[0].tv_sec = time(0); |
| 541 | times[1].tv_sec = mtime; |
| 542 | if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){ |
| 543 | return 1; |
| 544 | } |
| 545 | #else |
| 546 | /* Legacy unix. |
| 547 | ** |
| 548 | ** Do not use utimes() on a symbolic link - it sees through the link and |
| 549 | ** modifies the timestamps on the target. Or fails if the target does |
| 550 | ** not exist. */ |
| 551 | if( 0==S_ISLNK(mode) ){ |
| 552 | struct timeval times[2]; |
| 553 | times[0].tv_usec = times[1].tv_usec = 0; |
| 554 | times[0].tv_sec = time(0); |
| 555 | times[1].tv_sec = mtime; |
| 556 | if( utimes(zFile, times) ){ |
| 557 | return 1; |
| 558 | } |
| 559 | } |
| 560 | #endif |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function. |
| 568 | ** Refer to header comments at the top of this file for details. |
| 569 | */ |
| 570 | static void writefileFunc( |
| 571 | sqlite3_context *context, |
| 572 | int argc, |
| 573 | sqlite3_value **argv |
| 574 | ){ |
| 575 | const char *zFile; |
| 576 | mode_t mode = 0; |
| 577 | int res; |
| 578 | sqlite3_int64 mtime = -1; |
| 579 | |
| 580 | if( argc<2 || argc>4 ){ |
| 581 | sqlite3_result_error(context, |
| 582 | "wrong number of arguments to function writefile()", -1 |
| 583 | ); |
| 584 | return; |
| 585 | } |
| 586 | |
| 587 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 588 | if( zFile==0 ) return; |
| 589 | if( argc>=3 ){ |
| 590 | mode = (mode_t)sqlite3_value_int(argv[2]); |
| 591 | } |
| 592 | if( argc==4 ){ |
| 593 | mtime = sqlite3_value_int64(argv[3]); |
| 594 | } |
| 595 | |
| 596 | res = writeFile(context, zFile, argv[1], mode, mtime); |
| 597 | if( res==1 && errno==ENOENT ){ |
| 598 | res = makeDirectory(zFile); |
| 599 | if( res==SQLITE_OK ){ |
| 600 | res = writeFile(context, zFile, argv[1], mode, mtime); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | if( argc>2 && res!=0 ){ |
| 605 | if( S_ISLNK(mode) ){ |
| 606 | ctxErrorMsg(context, "failed to create symlink: %s", zFile); |
| 607 | }else if( S_ISDIR(mode) ){ |
| 608 | ctxErrorMsg(context, "failed to create directory: %s", zFile); |
| 609 | }else{ |
| 610 | ctxErrorMsg(context, "failed to write file: %s", zFile); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | ** SQL function: lsmode(MODE) |
| 617 | ** |
| 618 | ** Given a numberic st_mode from stat(), convert it into a human-readable |
| 619 | ** text string in the style of "ls -l". |
| 620 | */ |
| 621 | static void lsModeFunc( |
| 622 | sqlite3_context *context, |
| 623 | int argc, |
| 624 | sqlite3_value **argv |
| 625 | ){ |
| 626 | int i; |
| 627 | int iMode = sqlite3_value_int(argv[0]); |
| 628 | char z[16]; |
| 629 | (void)argc; |
| 630 | if( S_ISLNK(iMode) ){ |
| 631 | z[0] = 'l'; |
| 632 | }else if( S_ISREG(iMode) ){ |
| 633 | z[0] = '-'; |
| 634 | }else if( S_ISDIR(iMode) ){ |
| 635 | z[0] = 'd'; |
| 636 | }else{ |
| 637 | z[0] = '?'; |
| 638 | } |
| 639 | for(i=0; i<3; i++){ |
| 640 | int m = (iMode >> ((2-i)*3)); |
| 641 | char *a = &z[1 + i*3]; |
| 642 | a[0] = (m & 0x4) ? 'r' : '-'; |
| 643 | a[1] = (m & 0x2) ? 'w' : '-'; |
| 644 | a[2] = (m & 0x1) ? 'x' : '-'; |
| 645 | } |
| 646 | z[10] = '\0'; |
| 647 | sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); |
| 648 | } |
| 649 | |
| 650 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 651 | |
| 652 | /* |
| 653 | ** Cursor type for recursively iterating through a directory structure. |
| 654 | */ |
| 655 | typedef struct fsdir_cursor fsdir_cursor; |
| 656 | typedef struct FsdirLevel FsdirLevel; |
| 657 | |
| 658 | struct FsdirLevel { |
| 659 | DIR *pDir; /* From opendir() */ |
| 660 | char *zDir; /* Name of directory (nul-terminated) */ |
| 661 | }; |
| 662 | |
| 663 | struct fsdir_cursor { |
| 664 | sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 665 | |
| 666 | int nLvl; /* Number of entries in aLvl[] array */ |
| 667 | int mxLvl; /* Maximum level */ |
| 668 | int iLvl; /* Index of current entry */ |
| 669 | FsdirLevel *aLvl; /* Hierarchy of directories being traversed */ |
| 670 | |
| 671 | const char *zBase; |
| 672 | int nBase; |
| 673 | |
| 674 | STRUCT_STAT sStat; /* Current lstat() results */ |
| 675 | char *zPath; /* Path to current entry */ |
| 676 | sqlite3_int64 iRowid; /* Current rowid */ |
| 677 | }; |
| 678 | |
| 679 | typedef struct fsdir_tab fsdir_tab; |
| 680 | struct fsdir_tab { |
| 681 | sqlite3_vtab base; /* Base class - must be first */ |
| 682 | }; |
| 683 | |
| 684 | /* |
| 685 | ** Construct a new fsdir virtual table object. |
| 686 | */ |
| 687 | static int fsdirConnect( |
| 688 | sqlite3 *db, |
| 689 | void *pAux, |
| 690 | int argc, const char *const*argv, |
| 691 | sqlite3_vtab **ppVtab, |
| 692 | char **pzErr |
| 693 | ){ |
| 694 | fsdir_tab *pNew = 0; |
| 695 | int rc; |
| 696 | (void)pAux; |
| 697 | (void)argc; |
| 698 | (void)argv; |
| 699 | (void)pzErr; |
| 700 | rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA); |
| 701 | if( rc==SQLITE_OK ){ |
| 702 | pNew = (fsdir_tab*)sqlite3_malloc64( sizeof(*pNew) ); |
| 703 | if( pNew==0 ) return SQLITE_NOMEM; |
| 704 | memset(pNew, 0, sizeof(*pNew)); |
| 705 | sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); |
| 706 | } |
| 707 | *ppVtab = (sqlite3_vtab*)pNew; |
| 708 | return rc; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | ** This method is the destructor for fsdir vtab objects. |
| 713 | */ |
| 714 | static int fsdirDisconnect(sqlite3_vtab *pVtab){ |
| 715 | sqlite3_free(pVtab); |
| 716 | return SQLITE_OK; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | ** Constructor for a new fsdir_cursor object. |
| 721 | */ |
| 722 | static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 723 | fsdir_cursor *pCur; |
| 724 | (void)p; |
| 725 | pCur = sqlite3_malloc64( sizeof(*pCur) ); |
| 726 | if( pCur==0 ) return SQLITE_NOMEM; |
| 727 | memset(pCur, 0, sizeof(*pCur)); |
| 728 | pCur->iLvl = -1; |
| 729 | *ppCursor = &pCur->base; |
| 730 | return SQLITE_OK; |
| 731 | } |
| 732 | |
| 733 | /* |
| 734 | ** Reset a cursor back to the state it was in when first returned |
| 735 | ** by fsdirOpen(). |
| 736 | */ |
| 737 | static void fsdirResetCursor(fsdir_cursor *pCur){ |
| 738 | int i; |
| 739 | for(i=0; i<=pCur->iLvl; i++){ |
| 740 | FsdirLevel *pLvl = &pCur->aLvl[i]; |
| 741 | if( pLvl->pDir ) closedir(pLvl->pDir); |
| 742 | sqlite3_free(pLvl->zDir); |
| 743 | } |
| 744 | sqlite3_free(pCur->zPath); |
| 745 | sqlite3_free(pCur->aLvl); |
| 746 | pCur->aLvl = 0; |
| 747 | pCur->zPath = 0; |
| 748 | pCur->zBase = 0; |
| 749 | pCur->nBase = 0; |
| 750 | pCur->nLvl = 0; |
| 751 | pCur->iLvl = -1; |
| 752 | pCur->iRowid = 1; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | ** Destructor for an fsdir_cursor. |
| 757 | */ |
| 758 | static int fsdirClose(sqlite3_vtab_cursor *cur){ |
| 759 | fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 760 | |
| 761 | fsdirResetCursor(pCur); |
| 762 | sqlite3_free(pCur); |
| 763 | return SQLITE_OK; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | ** Set the error message for the virtual table associated with cursor |
| 768 | ** pCur to the results of vprintf(zFmt, ...). |
| 769 | */ |
| 770 | static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){ |
| 771 | va_list ap; |
| 772 | va_start(ap, zFmt); |
| 773 | pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); |
| 774 | va_end(ap); |
| 775 | } |
| 776 | |
| 777 | |
| 778 | /* |
| 779 | ** Advance an fsdir_cursor to its next row of output. |
| 780 | */ |
| 781 | static int fsdirNext(sqlite3_vtab_cursor *cur){ |
| 782 | fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 783 | mode_t m = pCur->sStat.st_mode; |
| 784 | |
| 785 | pCur->iRowid++; |
| 786 | if( S_ISDIR(m) && pCur->iLvl+3<pCur->mxLvl ){ |
| 787 | /* Descend into this directory */ |
| 788 | int iNew = pCur->iLvl + 1; |
| 789 | FsdirLevel *pLvl; |
| 790 | if( iNew>=pCur->nLvl ){ |
| 791 | int nNew = iNew+1; |
| 792 | sqlite3_int64 nByte = nNew*sizeof(FsdirLevel); |
| 793 | FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte); |
| 794 | if( aNew==0 ) return SQLITE_NOMEM; |
| 795 | memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl)); |
| 796 | pCur->aLvl = aNew; |
| 797 | pCur->nLvl = nNew; |
| 798 | } |
| 799 | pCur->iLvl = iNew; |
| 800 | pLvl = &pCur->aLvl[iNew]; |
| 801 | |
| 802 | pLvl->zDir = pCur->zPath; |
| 803 | pCur->zPath = 0; |
| 804 | pLvl->pDir = opendir(pLvl->zDir); |
| 805 | if( pLvl->pDir==0 ){ |
| 806 | fsdirSetErrmsg(pCur, "cannot read directory: %s", pLvl->zDir); |
| 807 | return SQLITE_ERROR; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | while( pCur->iLvl>=0 ){ |
| 812 | FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl]; |
| 813 | struct dirent *pEntry = readdir(pLvl->pDir); |
| 814 | if( pEntry ){ |
| 815 | if( pEntry->d_name[0]=='.' ){ |
| 816 | if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue; |
| 817 | if( pEntry->d_name[1]=='\0' ) continue; |
| 818 | } |
| 819 | sqlite3_free(pCur->zPath); |
| 820 | pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name); |
| 821 | if( pCur->zPath==0 ) return SQLITE_NOMEM; |
| 822 | if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ |
| 823 | fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); |
| 824 | return SQLITE_ERROR; |
| 825 | } |
| 826 | return SQLITE_OK; |
| 827 | } |
| 828 | closedir(pLvl->pDir); |
| 829 | sqlite3_free(pLvl->zDir); |
| 830 | pLvl->pDir = 0; |
| 831 | pLvl->zDir = 0; |
| 832 | pCur->iLvl--; |
| 833 | } |
| 834 | |
| 835 | /* EOF */ |
| 836 | sqlite3_free(pCur->zPath); |
| 837 | pCur->zPath = 0; |
| 838 | return SQLITE_OK; |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | ** Return values of columns for the row at which the series_cursor |
| 843 | ** is currently pointing. |
| 844 | */ |
| 845 | static int fsdirColumn( |
| 846 | sqlite3_vtab_cursor *cur, /* The cursor */ |
| 847 | sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 848 | int i /* Which column to return */ |
| 849 | ){ |
| 850 | fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 851 | switch( i ){ |
| 852 | case FSDIR_COLUMN_NAME: { |
| 853 | sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT); |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | case FSDIR_COLUMN_MODE: |
| 858 | sqlite3_result_int64(ctx, pCur->sStat.st_mode); |
| 859 | break; |
| 860 | |
| 861 | case FSDIR_COLUMN_MTIME: |
| 862 | sqlite3_result_int64(ctx, pCur->sStat.st_mtime); |
| 863 | break; |
| 864 | |
| 865 | case FSDIR_COLUMN_DATA: { |
| 866 | mode_t m = pCur->sStat.st_mode; |
| 867 | if( S_ISDIR(m) ){ |
| 868 | sqlite3_result_null(ctx); |
| 869 | #if !defined(_WIN32) && !defined(WIN32) |
| 870 | }else if( S_ISLNK(m) ){ |
| 871 | char aStatic[64]; |
| 872 | char *aBuf = aStatic; |
| 873 | sqlite3_int64 nBuf = 64; |
| 874 | int n; |
| 875 | |
| 876 | while( 1 ){ |
| 877 | n = readlink(pCur->zPath, aBuf, nBuf); |
| 878 | if( n<nBuf ) break; |
| 879 | if( aBuf!=aStatic ) sqlite3_free(aBuf); |
| 880 | nBuf = nBuf*2; |
| 881 | aBuf = sqlite3_malloc64(nBuf); |
| 882 | if( aBuf==0 ){ |
| 883 | sqlite3_result_error_nomem(ctx); |
| 884 | return SQLITE_NOMEM; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | if( n>0 ){ |
| 889 | sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT); |
| 890 | }else{ |
| 891 | sqlite3_result_null(ctx); |
| 892 | } |
| 893 | if( aBuf!=aStatic ) sqlite3_free(aBuf); |
| 894 | #endif |
| 895 | }else{ |
| 896 | readFileContents(ctx, pCur->zPath); |
| 897 | } |
| 898 | break; |
| 899 | } |
| 900 | case FSDIR_COLUMN_LEVEL: |
| 901 | sqlite3_result_int(ctx, pCur->iLvl+2); |
| 902 | break; |
| 903 | case FSDIR_COLUMN_PATH: |
| 904 | default: { |
| 905 | /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters. |
| 906 | ** always return their values as NULL */ |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | return SQLITE_OK; |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | ** Return the rowid for the current row. In this implementation, the |
| 915 | ** first row returned is assigned rowid value 1, and each subsequent |
| 916 | ** row a value 1 more than that of the previous. |
| 917 | */ |
| 918 | static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 919 | fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 920 | *pRowid = pCur->iRowid; |
| 921 | return SQLITE_OK; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | ** Return TRUE if the cursor has been moved off of the last |
| 926 | ** row of output. |
| 927 | */ |
| 928 | static int fsdirEof(sqlite3_vtab_cursor *cur){ |
| 929 | fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 930 | return (pCur->zPath==0); |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | ** xFilter callback. |
| 935 | ** |
| 936 | ** idxNum bit Meaning |
| 937 | ** 0x01 PATH=N |
| 938 | ** 0x02 DIR=N |
| 939 | ** 0x04 LEVEL<N |
| 940 | ** 0x08 LEVEL<=N |
| 941 | */ |
| 942 | static int fsdirFilter( |
| 943 | sqlite3_vtab_cursor *cur, |
| 944 | int idxNum, const char *idxStr, |
| 945 | int argc, sqlite3_value **argv |
| 946 | ){ |
| 947 | const char *zDir = 0; |
| 948 | fsdir_cursor *pCur = (fsdir_cursor*)cur; |
| 949 | int i; |
| 950 | (void)idxStr; |
| 951 | fsdirResetCursor(pCur); |
| 952 | |
| 953 | if( idxNum==0 ){ |
| 954 | fsdirSetErrmsg(pCur, "table function fsdir requires an argument"); |
| 955 | return SQLITE_ERROR; |
| 956 | } |
| 957 | |
| 958 | assert( (idxNum & 0x01)!=0 && argc>0 ); |
| 959 | zDir = (const char*)sqlite3_value_text(argv[0]); |
| 960 | if( zDir==0 ){ |
| 961 | fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument"); |
| 962 | return SQLITE_ERROR; |
| 963 | } |
| 964 | i = 1; |
| 965 | if( (idxNum & 0x02)!=0 ){ |
| 966 | assert( argc>i ); |
| 967 | pCur->zBase = (const char*)sqlite3_value_text(argv[i++]); |
| 968 | } |
| 969 | pCur->mxLvl = 1000000000; |
| 970 | if( (idxNum & 0x0c)!=0 ){ |
| 971 | /* The LEVEL constraint is only used to limit the depth of the |
| 972 | ** search. SQLite still checks the constraint against every row, so |
| 973 | ** mxLvl may admit rows that do not match, but must never exclude |
| 974 | ** a row that does. Only entries with level<mxLvl are visited. */ |
| 975 | sqlite3_value *pMx; |
| 976 | assert( argc>i ); |
| 977 | pMx = argv[i++]; |
| 978 | switch( sqlite3_value_type(pMx) ){ |
| 979 | case SQLITE_NULL: { |
| 980 | /* No row can match a NULL constraint */ |
| 981 | pCur->mxLvl = 1; |
| 982 | break; |
| 983 | } |
| 984 | case SQLITE_INTEGER: |
| 985 | case SQLITE_FLOAT: { |
| 986 | double r = sqlite3_value_double(pMx); |
| 987 | if( r<1.0 ){ |
| 988 | pCur->mxLvl = 1; |
| 989 | }else if( r<1.0e9 ){ |
| 990 | int iMx = (int)r; |
| 991 | if( (idxNum & 0x08)!=0 || (double)iMx<r ) iMx++; |
| 992 | pCur->mxLvl = iMx; |
| 993 | } |
| 994 | break; |
| 995 | } |
| 996 | default: { |
| 997 | /* TEXT or BLOB. Do not limit the search. */ |
| 998 | break; |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | if( pCur->zBase ){ |
| 1003 | pCur->nBase = (int)strlen(pCur->zBase)+1; |
| 1004 | pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir); |
| 1005 | }else{ |
| 1006 | pCur->zPath = sqlite3_mprintf("%s", zDir); |
| 1007 | } |
| 1008 | |
| 1009 | if( pCur->zPath==0 ){ |
| 1010 | return SQLITE_NOMEM; |
| 1011 | } |
| 1012 | if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ |
| 1013 | fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); |
| 1014 | return SQLITE_ERROR; |
| 1015 | } |
| 1016 | |
| 1017 | return SQLITE_OK; |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | ** SQLite will invoke this method one or more times while planning a query |
| 1022 | ** that uses the generate_series virtual table. This routine needs to create |
| 1023 | ** a query plan for each invocation and compute an estimated cost for that |
| 1024 | ** plan. |
| 1025 | ** |
| 1026 | ** In this implementation idxNum is used to represent the |
| 1027 | ** query plan. idxStr is unused. |
| 1028 | ** |
| 1029 | ** The query plan is represented by bits in idxNum: |
| 1030 | ** |
| 1031 | ** 0x01 The path value is supplied by argv[0] |
| 1032 | ** 0x02 dir is in argv[1] |
| 1033 | ** 0x04 LEVEL< bound is in argv[1] or [2] |
| 1034 | ** 0x08 LEVEL<= or LEVEL= bound is in argv[1] or [2] |
| 1035 | ** |
| 1036 | ** The LEVEL constraint is never omitted. It is used only to limit the |
| 1037 | ** depth of the directory search, and SQLite rechecks it for each row. |
| 1038 | */ |
| 1039 | static int fsdirBestIndex( |
| 1040 | sqlite3_vtab *tab, |
| 1041 | sqlite3_index_info *pIdxInfo |
| 1042 | ){ |
| 1043 | int i; /* Loop over constraints */ |
| 1044 | int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */ |
| 1045 | int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */ |
| 1046 | int idxLevel = -1; /* Index in pIdxInfo->aConstraint of LEVEL< or <= */ |
| 1047 | int idxLevelEQ = 0; /* 0x08 for LEVEL<= or LEVEL=. 0x04 for LEVEL< */ |
| 1048 | int seenPath = 0; /* True if an unusable PATH= constraint is seen */ |
| 1049 | int seenDir = 0; /* True if an unusable DIR= constraint is seen */ |
| 1050 | const struct sqlite3_index_constraint *pConstraint; |
| 1051 | |
| 1052 | (void)tab; |
| 1053 | pConstraint = pIdxInfo->aConstraint; |
| 1054 | for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 1055 | if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
| 1056 | switch( pConstraint->iColumn ){ |
| 1057 | case FSDIR_COLUMN_PATH: { |
| 1058 | if( pConstraint->usable ){ |
| 1059 | idxPath = i; |
| 1060 | seenPath = 0; |
| 1061 | }else if( idxPath<0 ){ |
| 1062 | seenPath = 1; |
| 1063 | } |
| 1064 | break; |
| 1065 | } |
| 1066 | case FSDIR_COLUMN_DIR: { |
| 1067 | if( pConstraint->usable ){ |
| 1068 | idxDir = i; |
| 1069 | seenDir = 0; |
| 1070 | }else if( idxDir<0 ){ |
| 1071 | seenDir = 1; |
| 1072 | } |
| 1073 | break; |
| 1074 | } |
| 1075 | case FSDIR_COLUMN_LEVEL: { |
| 1076 | if( pConstraint->usable && idxLevel<0 ){ |
| 1077 | idxLevel = i; |
| 1078 | idxLevelEQ = 0x08; |
| 1079 | } |
| 1080 | break; |
| 1081 | } |
| 1082 | } |
| 1083 | }else |
| 1084 | if( pConstraint->iColumn==FSDIR_COLUMN_LEVEL |
| 1085 | && pConstraint->usable |
| 1086 | && idxLevel<0 |
| 1087 | ){ |
| 1088 | if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE ){ |
| 1089 | idxLevel = i; |
| 1090 | idxLevelEQ = 0x08; |
| 1091 | }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){ |
| 1092 | idxLevel = i; |
| 1093 | idxLevelEQ = 0x04; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | if( seenPath || seenDir ){ |
| 1098 | /* If input parameters are unusable, disallow this plan */ |
| 1099 | return SQLITE_CONSTRAINT; |
| 1100 | } |
| 1101 | |
| 1102 | if( idxPath<0 ){ |
| 1103 | pIdxInfo->idxNum = 0; |
| 1104 | /* The pIdxInfo->estimatedCost should have been initialized to a huge |
| 1105 | ** number. Leave it unchanged. */ |
| 1106 | pIdxInfo->estimatedRows = 0x7fffffff; |
| 1107 | }else{ |
| 1108 | pIdxInfo->aConstraintUsage[idxPath].omit = 1; |
| 1109 | pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1; |
| 1110 | pIdxInfo->idxNum = 0x01; |
| 1111 | pIdxInfo->estimatedCost = 1.0e9; |
| 1112 | i = 2; |
| 1113 | if( idxDir>=0 ){ |
| 1114 | pIdxInfo->aConstraintUsage[idxDir].omit = 1; |
| 1115 | pIdxInfo->aConstraintUsage[idxDir].argvIndex = i++; |
| 1116 | pIdxInfo->idxNum |= 0x02; |
| 1117 | pIdxInfo->estimatedCost /= 1.0e4; |
| 1118 | } |
| 1119 | if( idxLevel>=0 ){ |
| 1120 | pIdxInfo->aConstraintUsage[idxLevel].argvIndex = i++; |
| 1121 | pIdxInfo->idxNum |= idxLevelEQ; |
| 1122 | pIdxInfo->estimatedCost /= 1.0e4; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | return SQLITE_OK; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | ** Register the "fsdir" virtual table. |
| 1131 | */ |
| 1132 | static int fsdirRegister(sqlite3 *db){ |
| 1133 | static sqlite3_module fsdirModule = { |
| 1134 | 0, /* iVersion */ |
| 1135 | 0, /* xCreate */ |
| 1136 | fsdirConnect, /* xConnect */ |
| 1137 | fsdirBestIndex, /* xBestIndex */ |
| 1138 | fsdirDisconnect, /* xDisconnect */ |
| 1139 | 0, /* xDestroy */ |
| 1140 | fsdirOpen, /* xOpen - open a cursor */ |
| 1141 | fsdirClose, /* xClose - close a cursor */ |
| 1142 | fsdirFilter, /* xFilter - configure scan constraints */ |
| 1143 | fsdirNext, /* xNext - advance a cursor */ |
| 1144 | fsdirEof, /* xEof - check for end of scan */ |
| 1145 | fsdirColumn, /* xColumn - read data */ |
| 1146 | fsdirRowid, /* xRowid - read data */ |
| 1147 | 0, /* xUpdate */ |
| 1148 | 0, /* xBegin */ |
| 1149 | 0, /* xSync */ |
| 1150 | 0, /* xCommit */ |
| 1151 | 0, /* xRollback */ |
| 1152 | 0, /* xFindMethod */ |
| 1153 | 0, /* xRename */ |
| 1154 | 0, /* xSavepoint */ |
| 1155 | 0, /* xRelease */ |
| 1156 | 0, /* xRollbackTo */ |
| 1157 | 0, /* xShadowName */ |
| 1158 | 0 /* xIntegrity */ |
| 1159 | }; |
| 1160 | |
| 1161 | int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0); |
| 1162 | return rc; |
| 1163 | } |
| 1164 | #else /* SQLITE_OMIT_VIRTUALTABLE */ |
| 1165 | # define fsdirRegister(x) SQLITE_OK |
| 1166 | #endif |
| 1167 | |
| 1168 | /* |
| 1169 | ** This version of realpath() works on any system. The string |
| 1170 | ** returned is held in memory allocated using sqlite3_malloc64(). |
| 1171 | ** The caller is responsible for calling sqlite3_free(). |
| 1172 | */ |
| 1173 | static char *portable_realpath(const char *zPath){ |
| 1174 | #if !defined(_WIN32) /* BEGIN unix */ |
| 1175 | |
| 1176 | char *zOut = 0; /* Result */ |
| 1177 | char *z; /* Temporary buffer */ |
| 1178 | #if defined(PATH_MAX) |
| 1179 | char zBuf[PATH_MAX+1]; /* Space for the temporary buffer */ |
| 1180 | #endif |
| 1181 | |
| 1182 | if( zPath==0 ) return 0; |
| 1183 | #if defined(PATH_MAX) |
| 1184 | z = realpath(zPath, zBuf); |
| 1185 | if( z ){ |
| 1186 | zOut = sqlite3_mprintf("%s", zBuf); |
| 1187 | } |
| 1188 | #endif /* defined(PATH_MAX) */ |
| 1189 | if( zOut==0 ){ |
| 1190 | /* Try POSIX.1-2008 malloc behavior */ |
| 1191 | z = realpath(zPath, NULL); |
| 1192 | if( z ){ |
| 1193 | zOut = sqlite3_mprintf("%s", z); |
| 1194 | free(z); |
| 1195 | } |
| 1196 | } |
| 1197 | return zOut; |
| 1198 | |
| 1199 | #else /* End UNIX, Begin WINDOWS */ |
| 1200 | |
| 1201 | wchar_t *zPath16; /* UTF16 translation of zPath */ |
| 1202 | char *zOut = 0; /* Result */ |
| 1203 | wchar_t *z = 0; /* Temporary buffer */ |
| 1204 | |
| 1205 | if( zPath==0 ) return 0; |
| 1206 | |
| 1207 | zPath16 = winUtf8To16(zPath); |
| 1208 | if( zPath16==0 ) return 0; |
| 1209 | z = _wfullpath(NULL, zPath16, 0); |
| 1210 | sqlite3_free(zPath16); |
| 1211 | if( z ){ |
| 1212 | zOut = winUtf16To8(z); |
| 1213 | free(z); |
| 1214 | } |
| 1215 | return zOut; |
| 1216 | |
| 1217 | #endif /* End WINDOWS, Begin common code */ |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | ** SQL function: realpath(X) |
| 1222 | ** |
| 1223 | ** Try to convert file or pathname X into its real, absolute pathname. |
| 1224 | ** Return NULL if unable. |
| 1225 | ** |
| 1226 | ** The file or directory X is not required to exist. The answer is formed |
| 1227 | ** by calling system realpath() on the prefix of X that does exist and |
| 1228 | ** appending the tail of X that does not (yet) exist. |
| 1229 | ** |
| 1230 | ** FIXME: This routine sometimes returns NULL rather than raising |
| 1231 | ** an SQLITE_NOMEM error if an OOM is encountered. |
| 1232 | */ |
| 1233 | static void realpathFunc( |
| 1234 | sqlite3_context *context, |
| 1235 | int argc, |
| 1236 | sqlite3_value **argv |
| 1237 | ){ |
| 1238 | const char *zPath; /* Original input path */ |
| 1239 | char *zCopy; /* An editable copy of zPath */ |
| 1240 | char *zOut; /* The result */ |
| 1241 | char cSep = 0; /* Separator turned into \000 */ |
| 1242 | size_t len; /* Prefix length before cSep */ |
| 1243 | #ifdef _WIN32 |
| 1244 | const int isWin = 1; |
| 1245 | #else |
| 1246 | const int isWin = 0; |
| 1247 | #endif |
| 1248 | |
| 1249 | (void)argc; |
| 1250 | zPath = (const char*)sqlite3_value_text(argv[0]); |
| 1251 | if( zPath==0 ) return; |
| 1252 | if( zPath[0]==0 ) zPath = "."; |
| 1253 | zCopy = sqlite3_mprintf("%s",zPath); |
| 1254 | if( zCopy==0 ) return; |
| 1255 | len = strlen(zCopy); |
| 1256 | while( len>1 && (zCopy[len-1]=='/' || (isWin && zCopy[len-1]=='\\')) ){ |
| 1257 | len--; |
| 1258 | } |
| 1259 | zCopy[len] = 0; |
| 1260 | while( 1 /*exit-by-break*/ ){ |
| 1261 | zOut = portable_realpath(zCopy); |
| 1262 | zCopy[len] = cSep; |
| 1263 | if( zOut ){ |
| 1264 | if( cSep ){ |
| 1265 | zOut = sqlite3_mprintf("%z%s",zOut,&zCopy[len]); |
| 1266 | } |
| 1267 | break; |
| 1268 | }else{ |
| 1269 | size_t i = len-1; |
| 1270 | while( i>0 ){ |
| 1271 | if( zCopy[i]=='/' || (isWin && zCopy[i]=='\\') ) break; |
| 1272 | i--; |
| 1273 | } |
| 1274 | if( i<=0 ){ |
| 1275 | if( zCopy[0]=='/' ){ |
| 1276 | zOut = zCopy; |
| 1277 | zCopy = 0; |
| 1278 | }else if( (zOut = portable_realpath("."))!=0 ){ |
| 1279 | zOut = sqlite3_mprintf("%z/%s", zOut, zCopy); |
| 1280 | } |
| 1281 | break; |
| 1282 | } |
| 1283 | cSep = zCopy[i]; |
| 1284 | zCopy[i] = 0; |
| 1285 | len = i; |
| 1286 | } |
| 1287 | } |
| 1288 | sqlite3_free(zCopy); |
| 1289 | if( zOut ){ |
| 1290 | /* Simplify any "/./" or "/../" that might have snuck into the |
| 1291 | ** pathname due to appending of zCopy. We only have to consider |
| 1292 | ** unix "/" separators, because the _wfilepath() system call on |
| 1293 | ** Windows will have already done this simplification for us. */ |
| 1294 | size_t i, j, n; |
| 1295 | n = strlen(zOut); |
| 1296 | for(i=j=0; i<n; i++){ |
| 1297 | if( zOut[i]=='/' ){ |
| 1298 | if( zOut[i+1]=='/' ) continue; |
| 1299 | if( zOut[i+1]=='.' && i+2<n && zOut[i+2]=='/' ){ |
| 1300 | i += 1; |
| 1301 | continue; |
| 1302 | } |
| 1303 | if( zOut[i+1]=='.' && i+3<n && zOut[i+2]=='.' && zOut[i+3]=='/' ){ |
| 1304 | while( j>0 && zOut[j-1]!='/' ){ j--; } |
| 1305 | if( j>0 ){ j--; } |
| 1306 | i += 2; |
| 1307 | continue; |
| 1308 | } |
| 1309 | } |
| 1310 | zOut[j++] = zOut[i]; |
| 1311 | } |
| 1312 | zOut[j] = 0; |
| 1313 | |
| 1314 | /* Return the result */ |
| 1315 | sqlite3_result_text(context, zOut, -1, sqlite3_free); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | #ifdef _WIN32 |
| 1321 | __declspec(dllexport) |
| 1322 | #endif |
| 1323 | int sqlite3_fileio_init( |
| 1324 | sqlite3 *db, |
| 1325 | char **pzErrMsg, |
| 1326 | const sqlite3_api_routines *pApi |
| 1327 | ){ |
| 1328 | int rc = SQLITE_OK; |
| 1329 | SQLITE_EXTENSION_INIT2(pApi); |
| 1330 | (void)pzErrMsg; /* Unused parameter */ |
| 1331 | |
| 1332 | /* REMINDER: Changes here should be reflected in the ".safe" command |
| 1333 | ** of the CLI. |
| 1334 | */ |
| 1335 | rc = sqlite3_create_function(db, "readfile", 1, |
| 1336 | SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
| 1337 | readfileFunc, 0, 0); |
| 1338 | if( rc==SQLITE_OK ){ |
| 1339 | rc = sqlite3_create_function(db, "writefile", -1, |
| 1340 | SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
| 1341 | writefileFunc, 0, 0); |
| 1342 | } |
| 1343 | if( rc==SQLITE_OK ){ |
| 1344 | rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0, |
| 1345 | lsModeFunc, 0, 0); |
| 1346 | } |
| 1347 | if( rc==SQLITE_OK ){ |
| 1348 | rc = fsdirRegister(db); |
| 1349 | } |
| 1350 | if( rc==SQLITE_OK ){ |
| 1351 | rc = sqlite3_create_function(db, "realpath", 1, |
| 1352 | SQLITE_UTF8, 0, |
| 1353 | realpathFunc, 0, 0); |
| 1354 | } |
| 1355 | return rc; |
| 1356 | } |