In the fileio.c extension, change the filetype(MODE) function into lsmode(MODE).
[sqlite.git] / ext / misc / fileio.c
blobf126a2e435c6d939d89c65c3a26fc39f28df5e38
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.
11 ******************************************************************************
13 ** This SQLite extension implements SQL functions readfile() and
14 ** writefile(), and eponymous virtual type "fsdir".
16 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
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.
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:
28 ** regular files: (mode & 0170000)==0100000
29 ** symbolic links: (mode & 0170000)==0120000
30 ** directories: (mode & 0170000)==0040000
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.
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.
43 ** If three or more arguments are passed to this function and an
44 ** error is encountered, an exception is raised.
46 ** READFILE(FILE):
48 ** Read and return the contents of file FILE (type blob) from disk.
50 ** FSDIR:
52 ** Used as follows:
54 ** SELECT * FROM fsdir($path [, $dir]);
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.
62 ** Each row has the following columns:
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.
71 ** If a non-NULL value is specified for the optional $dir parameter and
72 ** $path is a relative path, then $path is interpreted relative to $dir.
73 ** And the paths returned in the "name" column of the table are also
74 ** relative to directory $dir.
76 #include "sqlite3ext.h"
77 SQLITE_EXTENSION_INIT1
78 #include <stdio.h>
79 #include <string.h>
80 #include <assert.h>
82 #include <sys/types.h>
83 #include <sys/stat.h>
84 #include <fcntl.h>
85 #if !defined(_WIN32) && !defined(WIN32)
86 # include <unistd.h>
87 # include <dirent.h>
88 # include <utime.h>
89 #else
90 # include "windows.h"
91 # include <io.h>
92 # include <direct.h>
93 # include "test_windirent.h"
94 # define dirent DIRENT
95 # ifndef stat
96 # define stat _stat
97 # endif
98 # define mkdir(path,mode) _mkdir(path)
99 # define lstat(path,buf) stat(path,buf)
100 #endif
101 #include <time.h>
102 #include <errno.h>
105 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
108 ** Set the result stored by context ctx to a blob containing the
109 ** contents of file zName.
111 static void readFileContents(sqlite3_context *ctx, const char *zName){
112 FILE *in;
113 long nIn;
114 void *pBuf;
116 in = fopen(zName, "rb");
117 if( in==0 ) return;
118 fseek(in, 0, SEEK_END);
119 nIn = ftell(in);
120 rewind(in);
121 pBuf = sqlite3_malloc( nIn );
122 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
123 sqlite3_result_blob(ctx, pBuf, nIn, sqlite3_free);
124 }else{
125 sqlite3_free(pBuf);
127 fclose(in);
131 ** Implementation of the "readfile(X)" SQL function. The entire content
132 ** of the file named X is read and returned as a BLOB. NULL is returned
133 ** if the file does not exist or is unreadable.
135 static void readfileFunc(
136 sqlite3_context *context,
137 int argc,
138 sqlite3_value **argv
140 const char *zName;
141 (void)(argc); /* Unused parameter */
142 zName = (const char*)sqlite3_value_text(argv[0]);
143 if( zName==0 ) return;
144 readFileContents(context, zName);
148 ** Set the error message contained in context ctx to the results of
149 ** vprintf(zFmt, ...).
151 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
152 char *zMsg = 0;
153 va_list ap;
154 va_start(ap, zFmt);
155 zMsg = sqlite3_vmprintf(zFmt, ap);
156 sqlite3_result_error(ctx, zMsg, -1);
157 sqlite3_free(zMsg);
158 va_end(ap);
162 ** Argument zFile is the name of a file that will be created and/or written
163 ** by SQL function writefile(). This function ensures that the directory
164 ** zFile will be written to exists, creating it if required. The permissions
165 ** for any path components created by this function are set to (mode&0777).
167 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
168 ** SQLITE_OK is returned if the directory is successfully created, or
169 ** SQLITE_ERROR otherwise.
171 static int makeDirectory(
172 const char *zFile,
173 mode_t mode
175 char *zCopy = sqlite3_mprintf("%s", zFile);
176 int rc = SQLITE_OK;
178 if( zCopy==0 ){
179 rc = SQLITE_NOMEM;
180 }else{
181 int nCopy = (int)strlen(zCopy);
182 int i = 1;
184 while( rc==SQLITE_OK ){
185 struct stat sStat;
186 int rc2;
188 for(; zCopy[i]!='/' && i<nCopy; i++);
189 if( i==nCopy ) break;
190 zCopy[i] = '\0';
192 rc2 = stat(zCopy, &sStat);
193 if( rc2!=0 ){
194 if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
195 }else{
196 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
198 zCopy[i] = '/';
199 i++;
202 sqlite3_free(zCopy);
205 return rc;
209 ** This function does the work for the writefile() UDF. Refer to
210 ** header comments at the top of this file for details.
212 static int writeFile(
213 sqlite3_context *pCtx, /* Context to return bytes written in */
214 const char *zFile, /* File to write */
215 sqlite3_value *pData, /* Data to write */
216 mode_t mode, /* MODE parameter passed to writefile() */
217 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
219 #if !defined(_WIN32) && !defined(WIN32)
220 if( S_ISLNK(mode) ){
221 const char *zTo = (const char*)sqlite3_value_text(pData);
222 if( symlink(zTo, zFile)<0 ) return 1;
223 }else
224 #endif
226 if( S_ISDIR(mode) ){
227 if( mkdir(zFile, mode) ){
228 /* The mkdir() call to create the directory failed. This might not
229 ** be an error though - if there is already a directory at the same
230 ** path and either the permissions already match or can be changed
231 ** to do so using chmod(), it is not an error. */
232 struct stat sStat;
233 if( errno!=EEXIST
234 || 0!=stat(zFile, &sStat)
235 || !S_ISDIR(sStat.st_mode)
236 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
238 return 1;
241 }else{
242 sqlite3_int64 nWrite = 0;
243 const char *z;
244 int rc = 0;
245 FILE *out = fopen(zFile, "wb");
246 if( out==0 ) return 1;
247 z = (const char*)sqlite3_value_blob(pData);
248 if( z ){
249 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
250 nWrite = sqlite3_value_bytes(pData);
251 if( nWrite!=n ){
252 rc = 1;
255 fclose(out);
256 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
257 rc = 1;
259 if( rc ) return 2;
260 sqlite3_result_int64(pCtx, nWrite);
264 if( mtime>=0 ){
265 #if defined(_WIN32)
266 /* Windows */
267 FILETIME lastAccess;
268 FILETIME lastWrite;
269 SYSTEMTIME currentTime;
270 LONGLONG intervals;
271 HANDLE hFile;
272 GetSystemTime(&currentTime);
273 SystemTimeToFileTime(&currentTime, &lastAccess);
274 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
275 lastWrite.dwLowDateTime = (DWORD)intervals;
276 lastWrite.dwHighDateTime = intervals >> 32;
277 hFile = CreateFile(
278 zFile, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
279 FILE_FLAG_BACKUP_SEMANTICS, NULL
281 if( hFile!=INVALID_HANDLE_VALUE ){
282 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
283 CloseHandle(hFile);
284 return !bResult;
285 }else{
286 return 1;
288 #elif defined(AT_FDCWD)
289 /* Recent unix */
290 struct timespec times[2];
291 times[0].tv_nsec = times[1].tv_nsec = 0;
292 times[0].tv_sec = time(0);
293 times[1].tv_sec = mtime;
294 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
295 return 1;
297 #else
298 /* Legacy unix */
299 struct timeval times[2];
300 times[0].tv_usec = times[1].tv_usec = 0;
301 times[0].tv_sec = time(0);
302 times[1].tv_sec = mtime;
303 if( utimes(zFile, times) ){
304 return 1;
306 #endif
309 return 0;
313 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
314 ** Refer to header comments at the top of this file for details.
316 static void writefileFunc(
317 sqlite3_context *context,
318 int argc,
319 sqlite3_value **argv
321 const char *zFile;
322 mode_t mode = 0;
323 int res;
324 sqlite3_int64 mtime = -1;
326 if( argc<2 || argc>4 ){
327 sqlite3_result_error(context,
328 "wrong number of arguments to function writefile()", -1
330 return;
333 zFile = (const char*)sqlite3_value_text(argv[0]);
334 if( zFile==0 ) return;
335 if( argc>=3 ){
336 mode = (mode_t)sqlite3_value_int(argv[2]);
338 if( argc==4 ){
339 mtime = sqlite3_value_int64(argv[3]);
342 res = writeFile(context, zFile, argv[1], mode, mtime);
343 if( res==1 && errno==ENOENT ){
344 if( makeDirectory(zFile, mode)==SQLITE_OK ){
345 res = writeFile(context, zFile, argv[1], mode, mtime);
349 if( argc>2 && res!=0 ){
350 if( S_ISLNK(mode) ){
351 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
352 }else if( S_ISDIR(mode) ){
353 ctxErrorMsg(context, "failed to create directory: %s", zFile);
354 }else{
355 ctxErrorMsg(context, "failed to write file: %s", zFile);
361 ** SQL function: lsmode(MODE)
363 ** Given a numberic st_mode from stat(), convert it into a human-readable
364 ** text string in the style of "ls -l".
366 static void lsModeFunc(
367 sqlite3_context *context,
368 int argc,
369 sqlite3_value **argv
371 int i;
372 int iMode = sqlite3_value_int(argv[0]);
373 char z[16];
374 if( S_ISLNK(iMode) ){
375 z[0] = 'l';
376 }else if( S_ISREG(iMode) ){
377 z[0] = '-';
378 }else if( S_ISDIR(iMode) ){
379 z[0] = 'd';
380 }else{
381 z[0] = '?';
383 for(i=0; i<3; i++){
384 int m = (iMode >> ((2-i)*3));
385 char *a = &z[1 + i*3];
386 a[0] = (m & 0x4) ? 'r' : '-';
387 a[1] = (m & 0x2) ? 'w' : '-';
388 a[2] = (m & 0x1) ? 'x' : '-';
390 z[10] = '\0';
391 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
394 #ifndef SQLITE_OMIT_VIRTUALTABLE
397 ** Cursor type for recursively iterating through a directory structure.
399 typedef struct fsdir_cursor fsdir_cursor;
400 typedef struct FsdirLevel FsdirLevel;
402 struct FsdirLevel {
403 DIR *pDir; /* From opendir() */
404 char *zDir; /* Name of directory (nul-terminated) */
407 struct fsdir_cursor {
408 sqlite3_vtab_cursor base; /* Base class - must be first */
410 int nLvl; /* Number of entries in aLvl[] array */
411 int iLvl; /* Index of current entry */
412 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
414 const char *zBase;
415 int nBase;
417 struct stat sStat; /* Current lstat() results */
418 char *zPath; /* Path to current entry */
419 sqlite3_int64 iRowid; /* Current rowid */
422 typedef struct fsdir_tab fsdir_tab;
423 struct fsdir_tab {
424 sqlite3_vtab base; /* Base class - must be first */
428 ** Construct a new fsdir virtual table object.
430 static int fsdirConnect(
431 sqlite3 *db,
432 void *pAux,
433 int argc, const char *const*argv,
434 sqlite3_vtab **ppVtab,
435 char **pzErr
437 fsdir_tab *pNew = 0;
438 int rc;
440 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
441 if( rc==SQLITE_OK ){
442 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
443 if( pNew==0 ) return SQLITE_NOMEM;
444 memset(pNew, 0, sizeof(*pNew));
446 *ppVtab = (sqlite3_vtab*)pNew;
447 return rc;
451 ** This method is the destructor for fsdir vtab objects.
453 static int fsdirDisconnect(sqlite3_vtab *pVtab){
454 sqlite3_free(pVtab);
455 return SQLITE_OK;
459 ** Constructor for a new fsdir_cursor object.
461 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
462 fsdir_cursor *pCur;
463 pCur = sqlite3_malloc( sizeof(*pCur) );
464 if( pCur==0 ) return SQLITE_NOMEM;
465 memset(pCur, 0, sizeof(*pCur));
466 pCur->iLvl = -1;
467 *ppCursor = &pCur->base;
468 return SQLITE_OK;
472 ** Reset a cursor back to the state it was in when first returned
473 ** by fsdirOpen().
475 static void fsdirResetCursor(fsdir_cursor *pCur){
476 int i;
477 for(i=0; i<=pCur->iLvl; i++){
478 FsdirLevel *pLvl = &pCur->aLvl[i];
479 if( pLvl->pDir ) closedir(pLvl->pDir);
480 sqlite3_free(pLvl->zDir);
482 sqlite3_free(pCur->zPath);
483 pCur->aLvl = 0;
484 pCur->zPath = 0;
485 pCur->zBase = 0;
486 pCur->nBase = 0;
487 pCur->iLvl = -1;
488 pCur->iRowid = 1;
492 ** Destructor for an fsdir_cursor.
494 static int fsdirClose(sqlite3_vtab_cursor *cur){
495 fsdir_cursor *pCur = (fsdir_cursor*)cur;
497 fsdirResetCursor(pCur);
498 sqlite3_free(pCur->aLvl);
499 sqlite3_free(pCur);
500 return SQLITE_OK;
504 ** Set the error message for the virtual table associated with cursor
505 ** pCur to the results of vprintf(zFmt, ...).
507 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
508 va_list ap;
509 va_start(ap, zFmt);
510 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
511 va_end(ap);
516 ** Advance an fsdir_cursor to its next row of output.
518 static int fsdirNext(sqlite3_vtab_cursor *cur){
519 fsdir_cursor *pCur = (fsdir_cursor*)cur;
520 mode_t m = pCur->sStat.st_mode;
522 pCur->iRowid++;
523 if( S_ISDIR(m) ){
524 /* Descend into this directory */
525 int iNew = pCur->iLvl + 1;
526 FsdirLevel *pLvl;
527 if( iNew>=pCur->nLvl ){
528 int nNew = iNew+1;
529 int nByte = nNew*sizeof(FsdirLevel);
530 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc(pCur->aLvl, nByte);
531 if( aNew==0 ) return SQLITE_NOMEM;
532 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
533 pCur->aLvl = aNew;
534 pCur->nLvl = nNew;
536 pCur->iLvl = iNew;
537 pLvl = &pCur->aLvl[iNew];
539 pLvl->zDir = pCur->zPath;
540 pCur->zPath = 0;
541 pLvl->pDir = opendir(pLvl->zDir);
542 if( pLvl->pDir==0 ){
543 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
544 return SQLITE_ERROR;
548 while( pCur->iLvl>=0 ){
549 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
550 struct dirent *pEntry = readdir(pLvl->pDir);
551 if( pEntry ){
552 if( pEntry->d_name[0]=='.' ){
553 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
554 if( pEntry->d_name[1]=='\0' ) continue;
556 sqlite3_free(pCur->zPath);
557 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
558 if( pCur->zPath==0 ) return SQLITE_NOMEM;
559 if( lstat(pCur->zPath, &pCur->sStat) ){
560 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
561 return SQLITE_ERROR;
563 return SQLITE_OK;
565 closedir(pLvl->pDir);
566 sqlite3_free(pLvl->zDir);
567 pLvl->pDir = 0;
568 pLvl->zDir = 0;
569 pCur->iLvl--;
572 /* EOF */
573 sqlite3_free(pCur->zPath);
574 pCur->zPath = 0;
575 return SQLITE_OK;
579 ** Return values of columns for the row at which the series_cursor
580 ** is currently pointing.
582 static int fsdirColumn(
583 sqlite3_vtab_cursor *cur, /* The cursor */
584 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
585 int i /* Which column to return */
587 fsdir_cursor *pCur = (fsdir_cursor*)cur;
588 switch( i ){
589 case 0: { /* name */
590 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
591 break;
594 case 1: /* mode */
595 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
596 break;
598 case 2: /* mtime */
599 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
600 break;
602 case 3: { /* data */
603 mode_t m = pCur->sStat.st_mode;
604 if( S_ISDIR(m) ){
605 sqlite3_result_null(ctx);
606 #if !defined(_WIN32) && !defined(WIN32)
607 }else if( S_ISLNK(m) ){
608 char aStatic[64];
609 char *aBuf = aStatic;
610 int nBuf = 64;
611 int n;
613 while( 1 ){
614 n = readlink(pCur->zPath, aBuf, nBuf);
615 if( n<nBuf ) break;
616 if( aBuf!=aStatic ) sqlite3_free(aBuf);
617 nBuf = nBuf*2;
618 aBuf = sqlite3_malloc(nBuf);
619 if( aBuf==0 ){
620 sqlite3_result_error_nomem(ctx);
621 return SQLITE_NOMEM;
625 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
626 if( aBuf!=aStatic ) sqlite3_free(aBuf);
627 #endif
628 }else{
629 readFileContents(ctx, pCur->zPath);
633 return SQLITE_OK;
637 ** Return the rowid for the current row. In this implementation, the
638 ** first row returned is assigned rowid value 1, and each subsequent
639 ** row a value 1 more than that of the previous.
641 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
642 fsdir_cursor *pCur = (fsdir_cursor*)cur;
643 *pRowid = pCur->iRowid;
644 return SQLITE_OK;
648 ** Return TRUE if the cursor has been moved off of the last
649 ** row of output.
651 static int fsdirEof(sqlite3_vtab_cursor *cur){
652 fsdir_cursor *pCur = (fsdir_cursor*)cur;
653 return (pCur->zPath==0);
657 ** xFilter callback.
659 static int fsdirFilter(
660 sqlite3_vtab_cursor *cur,
661 int idxNum, const char *idxStr,
662 int argc, sqlite3_value **argv
664 const char *zDir = 0;
665 fsdir_cursor *pCur = (fsdir_cursor*)cur;
667 fsdirResetCursor(pCur);
669 if( idxNum==0 ){
670 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
671 return SQLITE_ERROR;
674 assert( argc==idxNum && (argc==1 || argc==2) );
675 zDir = (const char*)sqlite3_value_text(argv[0]);
676 if( zDir==0 ){
677 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
678 return SQLITE_ERROR;
680 if( argc==2 ){
681 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
683 if( pCur->zBase ){
684 pCur->nBase = (int)strlen(pCur->zBase)+1;
685 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
686 }else{
687 pCur->zPath = sqlite3_mprintf("%s", zDir);
690 if( pCur->zPath==0 ){
691 return SQLITE_NOMEM;
693 if( lstat(pCur->zPath, &pCur->sStat) ){
694 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
695 return SQLITE_ERROR;
698 return SQLITE_OK;
702 ** SQLite will invoke this method one or more times while planning a query
703 ** that uses the generate_series virtual table. This routine needs to create
704 ** a query plan for each invocation and compute an estimated cost for that
705 ** plan.
707 ** In this implementation idxNum is used to represent the
708 ** query plan. idxStr is unused.
710 ** The query plan is represented by bits in idxNum:
712 ** (1) start = $value -- constraint exists
713 ** (2) stop = $value -- constraint exists
714 ** (4) step = $value -- constraint exists
715 ** (8) output in descending order
717 static int fsdirBestIndex(
718 sqlite3_vtab *tab,
719 sqlite3_index_info *pIdxInfo
721 int i; /* Loop over constraints */
722 int idx4 = -1;
723 int idx5 = -1;
725 const struct sqlite3_index_constraint *pConstraint;
726 pConstraint = pIdxInfo->aConstraint;
727 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
728 if( pConstraint->usable==0 ) continue;
729 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
730 if( pConstraint->iColumn==4 ) idx4 = i;
731 if( pConstraint->iColumn==5 ) idx5 = i;
734 if( idx4<0 ){
735 pIdxInfo->idxNum = 0;
736 pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
737 }else{
738 pIdxInfo->aConstraintUsage[idx4].omit = 1;
739 pIdxInfo->aConstraintUsage[idx4].argvIndex = 1;
740 if( idx5>=0 ){
741 pIdxInfo->aConstraintUsage[idx5].omit = 1;
742 pIdxInfo->aConstraintUsage[idx5].argvIndex = 2;
743 pIdxInfo->idxNum = 2;
744 pIdxInfo->estimatedCost = 10.0;
745 }else{
746 pIdxInfo->idxNum = 1;
747 pIdxInfo->estimatedCost = 100.0;
751 return SQLITE_OK;
755 ** Register the "fsdir" virtual table.
757 static int fsdirRegister(sqlite3 *db){
758 static sqlite3_module fsdirModule = {
759 0, /* iVersion */
760 0, /* xCreate */
761 fsdirConnect, /* xConnect */
762 fsdirBestIndex, /* xBestIndex */
763 fsdirDisconnect, /* xDisconnect */
764 0, /* xDestroy */
765 fsdirOpen, /* xOpen - open a cursor */
766 fsdirClose, /* xClose - close a cursor */
767 fsdirFilter, /* xFilter - configure scan constraints */
768 fsdirNext, /* xNext - advance a cursor */
769 fsdirEof, /* xEof - check for end of scan */
770 fsdirColumn, /* xColumn - read data */
771 fsdirRowid, /* xRowid - read data */
772 0, /* xUpdate */
773 0, /* xBegin */
774 0, /* xSync */
775 0, /* xCommit */
776 0, /* xRollback */
777 0, /* xFindMethod */
778 0, /* xRename */
781 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
782 return rc;
784 #else /* SQLITE_OMIT_VIRTUALTABLE */
785 # define fsdirRegister(x) SQLITE_OK
786 #endif
788 #ifdef _WIN32
789 __declspec(dllexport)
790 #endif
791 int sqlite3_fileio_init(
792 sqlite3 *db,
793 char **pzErrMsg,
794 const sqlite3_api_routines *pApi
796 int rc = SQLITE_OK;
797 SQLITE_EXTENSION_INIT2(pApi);
798 (void)pzErrMsg; /* Unused parameter */
799 rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
800 readfileFunc, 0, 0);
801 if( rc==SQLITE_OK ){
802 rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
803 writefileFunc, 0, 0);
805 if( rc==SQLITE_OK ){
806 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
807 lsModeFunc, 0, 0);
809 if( rc==SQLITE_OK ){
810 rc = fsdirRegister(db);
812 return rc;