bump version to 4.5.5
[sqlcipher.git] / src / test_fs.c
blobddfdc7fb59806182b3fbc5ef248e656062c0f112
1 /*
2 ** 2013 Jan 11
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 *************************************************************************
12 ** Code for testing the virtual table interfaces. This code
13 ** is not included in the SQLite library. It is used for automated
14 ** testing of the SQLite library.
16 ** The FS virtual table is created as follows:
18 ** CREATE VIRTUAL TABLE tbl USING fs(idx);
20 ** where idx is the name of a table in the db with 2 columns. The virtual
21 ** table also has two columns - file path and file contents.
23 ** The first column of table idx must be an IPK, and the second contains file
24 ** paths. For example:
26 ** CREATE TABLE idx(id INTEGER PRIMARY KEY, path TEXT);
27 ** INSERT INTO idx VALUES(4, '/etc/passwd');
29 ** Adding the row to the idx table automatically creates a row in the
30 ** virtual table with rowid=4, path=/etc/passwd and a text field that
31 ** contains data read from file /etc/passwd on disk.
33 *************************************************************************
34 ** Virtual table module "fsdir"
36 ** This module is designed to be used as a read-only eponymous virtual table.
37 ** Its schema is as follows:
39 ** CREATE TABLE fsdir(dir TEXT, name TEXT);
41 ** When queried, a WHERE term of the form "dir = $dir" must be provided. The
42 ** virtual table then appears to have one row for each entry in file-system
43 ** directory $dir. Column dir contains a copy of $dir, and column "name"
44 ** contains the name of the directory entry.
46 ** If the specified $dir cannot be opened or is not a directory, it is not
47 ** an error. The virtual table appears to be empty in this case.
49 *************************************************************************
50 ** Virtual table module "fstree"
52 ** This module is also a read-only eponymous virtual table with the
53 ** following schema:
55 ** CREATE TABLE fstree(path TEXT, size INT, data BLOB);
57 ** Running a "SELECT * FROM fstree" query on this table returns the entire
58 ** contents of the file-system, starting at "/". To restrict the search
59 ** space, the virtual table supports LIKE and GLOB constraints on the
60 ** 'path' column. For example:
62 ** SELECT * FROM fstree WHERE path LIKE '/home/dan/sqlite/%'
64 #include "sqliteInt.h"
65 #if defined(INCLUDE_SQLITE_TCL_H)
66 # include "sqlite_tcl.h"
67 #else
68 # include "tcl.h"
69 #endif
71 #include <stdlib.h>
72 #include <string.h>
73 #include <sys/types.h>
74 #include <sys/stat.h>
75 #include <fcntl.h>
77 #if SQLITE_OS_UNIX || defined(__MINGW_H)
78 # include <unistd.h>
79 # include <dirent.h>
80 # ifndef DIRENT
81 # define DIRENT dirent
82 # endif
83 #endif
84 #if SQLITE_OS_WIN
85 # include <io.h>
86 # if !defined(__MINGW_H)
87 # include "test_windirent.h"
88 # endif
89 # ifndef S_ISREG
90 # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
91 # endif
92 #endif
94 #ifndef SQLITE_OMIT_VIRTUALTABLE
96 typedef struct fs_vtab fs_vtab;
97 typedef struct fs_cursor fs_cursor;
99 /*
100 ** A fs virtual-table object
102 struct fs_vtab {
103 sqlite3_vtab base;
104 sqlite3 *db;
105 char *zDb; /* Name of db containing zTbl */
106 char *zTbl; /* Name of docid->file map table */
109 /* A fs cursor object */
110 struct fs_cursor {
111 sqlite3_vtab_cursor base;
112 sqlite3_stmt *pStmt;
113 char *zBuf;
114 int nBuf;
115 int nAlloc;
118 /*************************************************************************
119 ** Start of fsdir implementation.
121 typedef struct FsdirVtab FsdirVtab;
122 typedef struct FsdirCsr FsdirCsr;
123 struct FsdirVtab {
124 sqlite3_vtab base;
127 struct FsdirCsr {
128 sqlite3_vtab_cursor base;
129 char *zDir; /* Buffer containing directory scanned */
130 DIR *pDir; /* Open directory */
131 sqlite3_int64 iRowid;
132 struct DIRENT *pEntry;
136 ** This function is the implementation of both the xConnect and xCreate
137 ** methods of the fsdir virtual table.
139 ** The argv[] array contains the following:
141 ** argv[0] -> module name ("fs")
142 ** argv[1] -> database name
143 ** argv[2] -> table name
144 ** argv[...] -> other module argument fields.
146 static int fsdirConnect(
147 sqlite3 *db,
148 void *pAux,
149 int argc, const char *const*argv,
150 sqlite3_vtab **ppVtab,
151 char **pzErr
153 FsdirVtab *pTab;
155 if( argc!=3 ){
156 *pzErr = sqlite3_mprintf("wrong number of arguments");
157 return SQLITE_ERROR;
160 pTab = (FsdirVtab *)sqlite3_malloc(sizeof(FsdirVtab));
161 if( !pTab ) return SQLITE_NOMEM;
162 memset(pTab, 0, sizeof(FsdirVtab));
164 *ppVtab = &pTab->base;
165 sqlite3_declare_vtab(db, "CREATE TABLE xyz(dir, name);");
167 return SQLITE_OK;
171 ** xDestroy/xDisconnect implementation.
173 static int fsdirDisconnect(sqlite3_vtab *pVtab){
174 sqlite3_free(pVtab);
175 return SQLITE_OK;
179 ** xBestIndex implementation. The only constraint supported is:
181 ** (dir = ?)
183 static int fsdirBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
184 int ii;
186 pIdxInfo->estimatedCost = 1000000000.0;
188 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
189 struct sqlite3_index_constraint const *p = &pIdxInfo->aConstraint[ii];
190 if( p->iColumn==0 && p->usable && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
191 struct sqlite3_index_constraint_usage *pUsage;
192 pUsage = &pIdxInfo->aConstraintUsage[ii];
193 pUsage->omit = 1;
194 pUsage->argvIndex = 1;
195 pIdxInfo->idxNum = 1;
196 pIdxInfo->estimatedCost = 1.0;
197 break;
201 return SQLITE_OK;
205 ** xOpen implementation.
207 ** Open a new fsdir cursor.
209 static int fsdirOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
210 FsdirCsr *pCur;
211 /* Allocate an extra 256 bytes because it is undefined how big dirent.d_name
212 ** is and we need enough space. Linux provides plenty already, but
213 ** Solaris only provides one byte. */
214 pCur = (FsdirCsr*)sqlite3_malloc(sizeof(FsdirCsr)+256);
215 if( pCur==0 ) return SQLITE_NOMEM;
216 memset(pCur, 0, sizeof(FsdirCsr));
217 *ppCursor = &pCur->base;
218 return SQLITE_OK;
222 ** Close a fsdir cursor.
224 static int fsdirClose(sqlite3_vtab_cursor *cur){
225 FsdirCsr *pCur = (FsdirCsr*)cur;
226 if( pCur->pDir ) closedir(pCur->pDir);
227 sqlite3_free(pCur->zDir);
228 sqlite3_free(pCur);
229 return SQLITE_OK;
233 ** Skip the cursor to the next entry.
235 static int fsdirNext(sqlite3_vtab_cursor *cur){
236 FsdirCsr *pCsr = (FsdirCsr*)cur;
238 if( pCsr->pDir ){
239 pCsr->pEntry = readdir(pCsr->pDir);
240 if( pCsr->pEntry==0 ){
241 closedir(pCsr->pDir);
242 pCsr->pDir = 0;
244 pCsr->iRowid++;
247 return SQLITE_OK;
251 ** xFilter method implementation.
253 static int fsdirFilter(
254 sqlite3_vtab_cursor *pVtabCursor,
255 int idxNum, const char *idxStr,
256 int argc, sqlite3_value **argv
258 FsdirCsr *pCsr = (FsdirCsr*)pVtabCursor;
259 const char *zDir;
260 int nDir;
263 if( idxNum!=1 || argc!=1 ){
264 return SQLITE_ERROR;
267 pCsr->iRowid = 0;
268 sqlite3_free(pCsr->zDir);
269 if( pCsr->pDir ){
270 closedir(pCsr->pDir);
271 pCsr->pDir = 0;
274 zDir = (const char*)sqlite3_value_text(argv[0]);
275 nDir = sqlite3_value_bytes(argv[0]);
276 pCsr->zDir = sqlite3_malloc(nDir+1);
277 if( pCsr->zDir==0 ) return SQLITE_NOMEM;
278 memcpy(pCsr->zDir, zDir, nDir+1);
280 pCsr->pDir = opendir(pCsr->zDir);
281 return fsdirNext(pVtabCursor);
285 ** xEof method implementation.
287 static int fsdirEof(sqlite3_vtab_cursor *cur){
288 FsdirCsr *pCsr = (FsdirCsr*)cur;
289 return pCsr->pDir==0;
293 ** xColumn method implementation.
295 static int fsdirColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
296 FsdirCsr *pCsr = (FsdirCsr*)cur;
297 switch( i ){
298 case 0: /* dir */
299 sqlite3_result_text(ctx, pCsr->zDir, -1, SQLITE_STATIC);
300 break;
302 case 1: /* name */
303 sqlite3_result_text(ctx, pCsr->pEntry->d_name, -1, SQLITE_TRANSIENT);
304 break;
306 default:
307 assert( 0 );
310 return SQLITE_OK;
314 ** xRowid method implementation.
316 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
317 FsdirCsr *pCsr = (FsdirCsr*)cur;
318 *pRowid = pCsr->iRowid;
319 return SQLITE_OK;
322 ** End of fsdir implementation.
323 *************************************************************************/
325 /*************************************************************************
326 ** Start of fstree implementation.
328 typedef struct FstreeVtab FstreeVtab;
329 typedef struct FstreeCsr FstreeCsr;
330 struct FstreeVtab {
331 sqlite3_vtab base;
332 sqlite3 *db;
335 struct FstreeCsr {
336 sqlite3_vtab_cursor base;
337 sqlite3_stmt *pStmt; /* Statement to list paths */
338 int fd; /* File descriptor open on current path */
342 ** This function is the implementation of both the xConnect and xCreate
343 ** methods of the fstree virtual table.
345 ** The argv[] array contains the following:
347 ** argv[0] -> module name ("fs")
348 ** argv[1] -> database name
349 ** argv[2] -> table name
350 ** argv[...] -> other module argument fields.
352 static int fstreeConnect(
353 sqlite3 *db,
354 void *pAux,
355 int argc, const char *const*argv,
356 sqlite3_vtab **ppVtab,
357 char **pzErr
359 FstreeVtab *pTab;
361 if( argc!=3 ){
362 *pzErr = sqlite3_mprintf("wrong number of arguments");
363 return SQLITE_ERROR;
366 pTab = (FstreeVtab *)sqlite3_malloc(sizeof(FstreeVtab));
367 if( !pTab ) return SQLITE_NOMEM;
368 memset(pTab, 0, sizeof(FstreeVtab));
369 pTab->db = db;
371 *ppVtab = &pTab->base;
372 sqlite3_declare_vtab(db, "CREATE TABLE xyz(path, size, data);");
374 return SQLITE_OK;
378 ** xDestroy/xDisconnect implementation.
380 static int fstreeDisconnect(sqlite3_vtab *pVtab){
381 sqlite3_free(pVtab);
382 return SQLITE_OK;
386 ** xBestIndex implementation. The only constraint supported is:
388 ** (dir = ?)
390 static int fstreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
391 int ii;
393 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
394 struct sqlite3_index_constraint const *p = &pIdxInfo->aConstraint[ii];
395 if( p->iColumn==0 && p->usable && (
396 p->op==SQLITE_INDEX_CONSTRAINT_GLOB
397 || p->op==SQLITE_INDEX_CONSTRAINT_LIKE
398 || p->op==SQLITE_INDEX_CONSTRAINT_EQ
400 struct sqlite3_index_constraint_usage *pUsage;
401 pUsage = &pIdxInfo->aConstraintUsage[ii];
402 pIdxInfo->idxNum = p->op;
403 pUsage->argvIndex = 1;
404 pIdxInfo->estimatedCost = 100000.0;
405 return SQLITE_OK;
409 pIdxInfo->estimatedCost = 1000000000.0;
410 return SQLITE_OK;
414 ** xOpen implementation.
416 ** Open a new fstree cursor.
418 static int fstreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
419 FstreeCsr *pCur;
420 pCur = (FstreeCsr*)sqlite3_malloc(sizeof(FstreeCsr));
421 if( pCur==0 ) return SQLITE_NOMEM;
422 memset(pCur, 0, sizeof(FstreeCsr));
423 pCur->fd = -1;
424 *ppCursor = &pCur->base;
425 return SQLITE_OK;
428 static void fstreeCloseFd(FstreeCsr *pCsr){
429 if( pCsr->fd>=0 ){
430 close(pCsr->fd);
431 pCsr->fd = -1;
436 ** Close a fstree cursor.
438 static int fstreeClose(sqlite3_vtab_cursor *cur){
439 FstreeCsr *pCsr = (FstreeCsr*)cur;
440 sqlite3_finalize(pCsr->pStmt);
441 fstreeCloseFd(pCsr);
442 sqlite3_free(pCsr);
443 return SQLITE_OK;
447 ** Skip the cursor to the next entry.
449 static int fstreeNext(sqlite3_vtab_cursor *cur){
450 FstreeCsr *pCsr = (FstreeCsr*)cur;
451 int rc;
453 fstreeCloseFd(pCsr);
454 rc = sqlite3_step(pCsr->pStmt);
455 if( rc!=SQLITE_ROW ){
456 rc = sqlite3_finalize(pCsr->pStmt);
457 pCsr->pStmt = 0;
458 }else{
459 rc = SQLITE_OK;
460 pCsr->fd = open((const char*)sqlite3_column_text(pCsr->pStmt, 0), O_RDONLY);
463 return rc;
467 ** xFilter method implementation.
469 static int fstreeFilter(
470 sqlite3_vtab_cursor *pVtabCursor,
471 int idxNum, const char *idxStr,
472 int argc, sqlite3_value **argv
474 FstreeCsr *pCsr = (FstreeCsr*)pVtabCursor;
475 FstreeVtab *pTab = (FstreeVtab*)(pCsr->base.pVtab);
476 int rc;
477 const char *zSql =
478 "WITH r(d) AS ("
479 " SELECT CASE WHEN dir=?2 THEN ?3 ELSE dir END || '/' || name "
480 " FROM fsdir WHERE dir=?1 AND name NOT LIKE '.%'"
481 " UNION ALL"
482 " SELECT dir || '/' || name FROM r, fsdir WHERE dir=d AND name NOT LIKE '.%'"
483 ") SELECT d FROM r;";
485 char *zRoot;
486 int nRoot;
487 char *zPrefix;
488 int nPrefix;
489 const char *zDir;
490 int nDir;
491 char aWild[2] = { '\0', '\0' };
493 #if SQLITE_OS_WIN
494 const char *zDrive = windirent_getenv("fstreeDrive");
495 if( zDrive==0 ){
496 zDrive = windirent_getenv("SystemDrive");
498 zRoot = sqlite3_mprintf("%s%c", zDrive, '/');
499 nRoot = sqlite3Strlen30(zRoot);
500 zPrefix = sqlite3_mprintf("%s", zDrive);
501 nPrefix = sqlite3Strlen30(zPrefix);
502 #else
503 zRoot = "/";
504 nRoot = 1;
505 zPrefix = "";
506 nPrefix = 0;
507 #endif
509 zDir = zRoot;
510 nDir = nRoot;
512 fstreeCloseFd(pCsr);
513 sqlite3_finalize(pCsr->pStmt);
514 pCsr->pStmt = 0;
515 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
516 if( rc!=SQLITE_OK ) return rc;
518 if( idxNum ){
519 const char *zQuery = (const char*)sqlite3_value_text(argv[0]);
520 switch( idxNum ){
521 case SQLITE_INDEX_CONSTRAINT_GLOB:
522 aWild[0] = '*';
523 aWild[1] = '?';
524 break;
525 case SQLITE_INDEX_CONSTRAINT_LIKE:
526 aWild[0] = '_';
527 aWild[1] = '%';
528 break;
531 if( sqlite3_strnicmp(zQuery, zPrefix, nPrefix)==0 ){
532 int i;
533 for(i=nPrefix; zQuery[i]; i++){
534 if( zQuery[i]==aWild[0] || zQuery[i]==aWild[1] ) break;
535 if( zQuery[i]=='/' ) nDir = i;
537 zDir = zQuery;
540 if( nDir==0 ) nDir = 1;
542 sqlite3_bind_text(pCsr->pStmt, 1, zDir, nDir, SQLITE_TRANSIENT);
543 sqlite3_bind_text(pCsr->pStmt, 2, zRoot, nRoot, SQLITE_TRANSIENT);
544 sqlite3_bind_text(pCsr->pStmt, 3, zPrefix, nPrefix, SQLITE_TRANSIENT);
546 #if SQLITE_OS_WIN
547 sqlite3_free(zPrefix);
548 sqlite3_free(zRoot);
549 #endif
551 return fstreeNext(pVtabCursor);
555 ** xEof method implementation.
557 static int fstreeEof(sqlite3_vtab_cursor *cur){
558 FstreeCsr *pCsr = (FstreeCsr*)cur;
559 return pCsr->pStmt==0;
563 ** xColumn method implementation.
565 static int fstreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
566 FstreeCsr *pCsr = (FstreeCsr*)cur;
567 if( i==0 ){ /* path */
568 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, 0));
569 }else{
570 struct stat sBuf;
571 fstat(pCsr->fd, &sBuf);
573 if( S_ISREG(sBuf.st_mode) ){
574 if( i==1 ){
575 sqlite3_result_int64(ctx, sBuf.st_size);
576 }else{
577 int nRead;
578 char *aBuf = sqlite3_malloc(sBuf.st_mode+1);
579 if( !aBuf ) return SQLITE_NOMEM;
580 nRead = read(pCsr->fd, aBuf, sBuf.st_mode);
581 if( nRead!=sBuf.st_mode ){
582 return SQLITE_IOERR;
584 sqlite3_result_blob(ctx, aBuf, nRead, SQLITE_TRANSIENT);
585 sqlite3_free(aBuf);
590 return SQLITE_OK;
594 ** xRowid method implementation.
596 static int fstreeRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
597 *pRowid = 0;
598 return SQLITE_OK;
601 ** End of fstree implementation.
602 *************************************************************************/
608 ** This function is the implementation of both the xConnect and xCreate
609 ** methods of the fs virtual table.
611 ** The argv[] array contains the following:
613 ** argv[0] -> module name ("fs")
614 ** argv[1] -> database name
615 ** argv[2] -> table name
616 ** argv[...] -> other module argument fields.
618 static int fsConnect(
619 sqlite3 *db,
620 void *pAux,
621 int argc, const char *const*argv,
622 sqlite3_vtab **ppVtab,
623 char **pzErr
625 fs_vtab *pVtab;
626 int nByte;
627 const char *zTbl;
628 const char *zDb = argv[1];
630 if( argc!=4 ){
631 *pzErr = sqlite3_mprintf("wrong number of arguments");
632 return SQLITE_ERROR;
634 zTbl = argv[3];
636 nByte = sizeof(fs_vtab) + (int)strlen(zTbl) + 1 + (int)strlen(zDb) + 1;
637 pVtab = (fs_vtab *)sqlite3MallocZero( nByte );
638 if( !pVtab ) return SQLITE_NOMEM;
640 pVtab->zTbl = (char *)&pVtab[1];
641 pVtab->zDb = &pVtab->zTbl[strlen(zTbl)+1];
642 pVtab->db = db;
643 memcpy(pVtab->zTbl, zTbl, strlen(zTbl));
644 memcpy(pVtab->zDb, zDb, strlen(zDb));
645 *ppVtab = &pVtab->base;
646 sqlite3_declare_vtab(db, "CREATE TABLE x(path TEXT, data TEXT)");
648 return SQLITE_OK;
650 /* Note that for this virtual table, the xCreate and xConnect
651 ** methods are identical. */
653 static int fsDisconnect(sqlite3_vtab *pVtab){
654 sqlite3_free(pVtab);
655 return SQLITE_OK;
657 /* The xDisconnect and xDestroy methods are also the same */
660 ** Open a new fs cursor.
662 static int fsOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
663 fs_cursor *pCur;
664 pCur = sqlite3MallocZero(sizeof(fs_cursor));
665 *ppCursor = &pCur->base;
666 return SQLITE_OK;
670 ** Close a fs cursor.
672 static int fsClose(sqlite3_vtab_cursor *cur){
673 fs_cursor *pCur = (fs_cursor *)cur;
674 sqlite3_finalize(pCur->pStmt);
675 sqlite3_free(pCur->zBuf);
676 sqlite3_free(pCur);
677 return SQLITE_OK;
680 static int fsNext(sqlite3_vtab_cursor *cur){
681 fs_cursor *pCur = (fs_cursor *)cur;
682 int rc;
684 rc = sqlite3_step(pCur->pStmt);
685 if( rc==SQLITE_ROW || rc==SQLITE_DONE ) rc = SQLITE_OK;
687 return rc;
690 static int fsFilter(
691 sqlite3_vtab_cursor *pVtabCursor,
692 int idxNum, const char *idxStr,
693 int argc, sqlite3_value **argv
695 int rc;
696 fs_cursor *pCur = (fs_cursor *)pVtabCursor;
697 fs_vtab *p = (fs_vtab *)(pVtabCursor->pVtab);
699 assert( (idxNum==0 && argc==0) || (idxNum==1 && argc==1) );
700 if( idxNum==1 ){
701 char *zStmt = sqlite3_mprintf(
702 "SELECT * FROM %Q.%Q WHERE rowid=?", p->zDb, p->zTbl);
703 if( !zStmt ) return SQLITE_NOMEM;
704 rc = sqlite3_prepare_v2(p->db, zStmt, -1, &pCur->pStmt, 0);
705 sqlite3_free(zStmt);
706 if( rc==SQLITE_OK ){
707 sqlite3_bind_value(pCur->pStmt, 1, argv[0]);
709 }else{
710 char *zStmt = sqlite3_mprintf("SELECT * FROM %Q.%Q", p->zDb, p->zTbl);
711 if( !zStmt ) return SQLITE_NOMEM;
712 rc = sqlite3_prepare_v2(p->db, zStmt, -1, &pCur->pStmt, 0);
713 sqlite3_free(zStmt);
716 if( rc==SQLITE_OK ){
717 rc = fsNext(pVtabCursor);
719 return rc;
722 static int fsColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
723 fs_cursor *pCur = (fs_cursor*)cur;
725 assert( i==0 || i==1 || i==2 );
726 if( i==0 ){
727 sqlite3_result_value(ctx, sqlite3_column_value(pCur->pStmt, 0));
728 }else{
729 const char *zFile = (const char *)sqlite3_column_text(pCur->pStmt, 1);
730 struct stat sbuf;
731 int fd;
733 int n;
734 fd = open(zFile, O_RDONLY);
735 if( fd<0 ) return SQLITE_IOERR;
736 fstat(fd, &sbuf);
738 if( sbuf.st_size>=pCur->nAlloc ){
739 sqlite3_int64 nNew = sbuf.st_size*2;
740 char *zNew;
741 if( nNew<1024 ) nNew = 1024;
743 zNew = sqlite3Realloc(pCur->zBuf, nNew);
744 if( zNew==0 ){
745 close(fd);
746 return SQLITE_NOMEM;
748 pCur->zBuf = zNew;
749 pCur->nAlloc = nNew;
752 n = (int)read(fd, pCur->zBuf, sbuf.st_size);
753 close(fd);
754 if( n!=sbuf.st_size ) return SQLITE_ERROR;
755 pCur->nBuf = sbuf.st_size;
756 pCur->zBuf[pCur->nBuf] = '\0';
758 sqlite3_result_text(ctx, pCur->zBuf, -1, SQLITE_TRANSIENT);
760 return SQLITE_OK;
763 static int fsRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
764 fs_cursor *pCur = (fs_cursor*)cur;
765 *pRowid = sqlite3_column_int64(pCur->pStmt, 0);
766 return SQLITE_OK;
769 static int fsEof(sqlite3_vtab_cursor *cur){
770 fs_cursor *pCur = (fs_cursor*)cur;
771 return (sqlite3_data_count(pCur->pStmt)==0);
774 static int fsBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
775 int ii;
777 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
778 struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
779 if( pCons->iColumn<0 && pCons->usable
780 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
781 struct sqlite3_index_constraint_usage *pUsage;
782 pUsage = &pIdxInfo->aConstraintUsage[ii];
783 pUsage->omit = 0;
784 pUsage->argvIndex = 1;
785 pIdxInfo->idxNum = 1;
786 pIdxInfo->estimatedCost = 1.0;
787 break;
791 return SQLITE_OK;
795 ** A virtual table module that provides read-only access to a
796 ** Tcl global variable namespace.
798 static sqlite3_module fsModule = {
799 0, /* iVersion */
800 fsConnect,
801 fsConnect,
802 fsBestIndex,
803 fsDisconnect,
804 fsDisconnect,
805 fsOpen, /* xOpen - open a cursor */
806 fsClose, /* xClose - close a cursor */
807 fsFilter, /* xFilter - configure scan constraints */
808 fsNext, /* xNext - advance a cursor */
809 fsEof, /* xEof - check for end of scan */
810 fsColumn, /* xColumn - read data */
811 fsRowid, /* xRowid - read data */
812 0, /* xUpdate */
813 0, /* xBegin */
814 0, /* xSync */
815 0, /* xCommit */
816 0, /* xRollback */
817 0, /* xFindMethod */
818 0, /* xRename */
821 static sqlite3_module fsdirModule = {
822 0, /* iVersion */
823 fsdirConnect, /* xCreate */
824 fsdirConnect, /* xConnect */
825 fsdirBestIndex, /* xBestIndex */
826 fsdirDisconnect, /* xDisconnect */
827 fsdirDisconnect, /* xDestroy */
828 fsdirOpen, /* xOpen - open a cursor */
829 fsdirClose, /* xClose - close a cursor */
830 fsdirFilter, /* xFilter - configure scan constraints */
831 fsdirNext, /* xNext - advance a cursor */
832 fsdirEof, /* xEof - check for end of scan */
833 fsdirColumn, /* xColumn - read data */
834 fsdirRowid, /* xRowid - read data */
835 0, /* xUpdate */
836 0, /* xBegin */
837 0, /* xSync */
838 0, /* xCommit */
839 0, /* xRollback */
840 0, /* xFindMethod */
841 0, /* xRename */
844 static sqlite3_module fstreeModule = {
845 0, /* iVersion */
846 fstreeConnect, /* xCreate */
847 fstreeConnect, /* xConnect */
848 fstreeBestIndex, /* xBestIndex */
849 fstreeDisconnect, /* xDisconnect */
850 fstreeDisconnect, /* xDestroy */
851 fstreeOpen, /* xOpen - open a cursor */
852 fstreeClose, /* xClose - close a cursor */
853 fstreeFilter, /* xFilter - configure scan constraints */
854 fstreeNext, /* xNext - advance a cursor */
855 fstreeEof, /* xEof - check for end of scan */
856 fstreeColumn, /* xColumn - read data */
857 fstreeRowid, /* xRowid - read data */
858 0, /* xUpdate */
859 0, /* xBegin */
860 0, /* xSync */
861 0, /* xCommit */
862 0, /* xRollback */
863 0, /* xFindMethod */
864 0, /* xRename */
868 ** Decode a pointer to an sqlite3 object.
870 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
873 ** Register the echo virtual table module.
875 static int SQLITE_TCLAPI register_fs_module(
876 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
877 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
878 int objc, /* Number of arguments */
879 Tcl_Obj *CONST objv[] /* Command arguments */
881 sqlite3 *db;
882 if( objc!=2 ){
883 Tcl_WrongNumArgs(interp, 1, objv, "DB");
884 return TCL_ERROR;
886 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
887 #ifndef SQLITE_OMIT_VIRTUALTABLE
888 sqlite3_create_module(db, "fs", &fsModule, (void *)interp);
889 sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
890 sqlite3_create_module(db, "fstree", &fstreeModule, 0);
891 #endif
892 return TCL_OK;
895 #endif
899 ** Register commands with the TCL interpreter.
901 int Sqlitetestfs_Init(Tcl_Interp *interp){
902 #ifndef SQLITE_OMIT_VIRTUALTABLE
903 static struct {
904 char *zName;
905 Tcl_ObjCmdProc *xProc;
906 void *clientData;
907 } aObjCmd[] = {
908 { "register_fs_module", register_fs_module, 0 },
910 int i;
911 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
912 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
913 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
915 #endif
916 return TCL_OK;