Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
test_fs.c921 linesDownload Raw Back to src
1/*2** 2013 Jan 113**4** The author disclaims copyright to this source code.  In place of5** 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** Code for testing the virtual table interfaces.  This code13** is not included in the SQLite library.  It is used for automated14** testing of the SQLite library.15**16** The FS virtual table is created as follows:17**18**   CREATE VIRTUAL TABLE tbl USING fs(idx);19**20** where idx is the name of a table in the db with 2 columns.  The virtual21** table also has two columns - file path and file contents.22**23** The first column of table idx must be an IPK, and the second contains file24** paths. For example:25**26**   CREATE TABLE idx(id INTEGER PRIMARY KEY, path TEXT);27**   INSERT INTO idx VALUES(4, '/etc/passwd');28**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.32**33*************************************************************************34** Virtual table module "fsdir"35**36** This module is designed to be used as a read-only eponymous virtual table.37** Its schema is as follows:38**39**   CREATE TABLE fsdir(dir TEXT, name TEXT);40**41** When queried, a WHERE term of the form "dir = $dir" must be provided. The42** virtual table then appears to have one row for each entry in file-system43** directory $dir. Column dir contains a copy of $dir, and column "name"44** contains the name of the directory entry.45**46** If the specified $dir cannot be opened or is not a directory, it is not47** an error. The virtual table appears to be empty in this case.48**49*************************************************************************50** Virtual table module "fstree"51**52** This module is also a read-only eponymous virtual table with the 53** following schema:54**55**   CREATE TABLE fstree(path TEXT, size INT, data BLOB);56**57** Running a "SELECT * FROM fstree" query on this table returns the entire58** contents of the file-system, starting at "/". To restrict the search59** space, the virtual table supports LIKE and GLOB constraints on the60** 'path' column. For example:61**62**   SELECT * FROM fstree WHERE path LIKE '/home/dan/sqlite/%'63*/64#include "sqliteInt.h"65#include "tclsqlite.h"66#include <stdlib.h>67#include <string.h>68#include <sys/types.h>69#include <sys/stat.h>70#include <fcntl.h>71 72#if !defined(_WIN32) || defined(__MSVCRT__)73# include <unistd.h>74# include <dirent.h>75#else76# include "windirent.h"77# ifndef S_ISREG78#  define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)79# endif80#endif81 82#ifndef SQLITE_OMIT_VIRTUALTABLE83 84typedef struct fs_vtab fs_vtab;85typedef struct fs_cursor fs_cursor;86 87/* 88** A fs virtual-table object 89*/90struct fs_vtab {91  sqlite3_vtab base;92  sqlite3 *db;93  char *zDb;                      /* Name of db containing zTbl */94  char *zTbl;                     /* Name of docid->file map table */95};96 97/* A fs cursor object */98struct fs_cursor {99  sqlite3_vtab_cursor base;100  sqlite3_stmt *pStmt;101  char *zBuf;102  int nBuf;103  int nAlloc;104};105 106/*************************************************************************107** Start of fsdir implementation.108*/109typedef struct FsdirVtab FsdirVtab;110typedef struct FsdirCsr FsdirCsr;111struct FsdirVtab {112  sqlite3_vtab base;113};114 115struct FsdirCsr {116  sqlite3_vtab_cursor base;117  char *zDir;                     /* Buffer containing directory scanned */118  DIR *pDir;                      /* Open directory */119  sqlite3_int64 iRowid;120  struct dirent *pEntry;121};122 123/*124** This function is the implementation of both the xConnect and xCreate125** methods of the fsdir virtual table.126**127** The argv[] array contains the following:128**129**   argv[0]   -> module name  ("fs")130**   argv[1]   -> database name131**   argv[2]   -> table name132**   argv[...] -> other module argument fields.133*/134static int fsdirConnect(135  sqlite3 *db,136  void *pAux,137  int argc, const char *const*argv,138  sqlite3_vtab **ppVtab,139  char **pzErr140){141  FsdirVtab *pTab;142 143  if( argc!=3 ){144    *pzErr = sqlite3_mprintf("wrong number of arguments");145    return SQLITE_ERROR;146  }147 148  pTab = (FsdirVtab *)sqlite3_malloc(sizeof(FsdirVtab));149  if( !pTab ) return SQLITE_NOMEM;150  memset(pTab, 0, sizeof(FsdirVtab));151 152  *ppVtab = &pTab->base;153  sqlite3_declare_vtab(db, "CREATE TABLE xyz(dir, name);");154 155  return SQLITE_OK;156}157 158/*159** xDestroy/xDisconnect implementation.160*/161static int fsdirDisconnect(sqlite3_vtab *pVtab){162  sqlite3_free(pVtab);163  return SQLITE_OK;164}165 166/*167** xBestIndex implementation. The only constraint supported is:168**169**   (dir = ?)170*/171static int fsdirBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){172  int ii;173 174  pIdxInfo->estimatedCost = 1000000000.0;175 176  for(ii=0; ii<pIdxInfo->nConstraint; ii++){177    struct sqlite3_index_constraint const *p = &pIdxInfo->aConstraint[ii];178    if( p->iColumn==0 && p->usable && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){179      struct sqlite3_index_constraint_usage *pUsage;180      pUsage = &pIdxInfo->aConstraintUsage[ii];181      pUsage->omit = 1;182      pUsage->argvIndex = 1;183      pIdxInfo->idxNum = 1;184      pIdxInfo->estimatedCost = 1.0;185      break;186    }187  }188 189  return SQLITE_OK;190}191 192/*193** xOpen implementation.194**195** Open a new fsdir cursor.196*/197static int fsdirOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){198  FsdirCsr *pCur;199  /* Allocate an extra 256 bytes because it is undefined how big dirent.d_name200  ** is and we need enough space.  Linux provides plenty already, but201  ** Solaris only provides one byte. */202  pCur = (FsdirCsr*)sqlite3_malloc(sizeof(FsdirCsr)+256);203  if( pCur==0 ) return SQLITE_NOMEM;204  memset(pCur, 0, sizeof(FsdirCsr));205  *ppCursor = &pCur->base;206  return SQLITE_OK;207}208 209/*210** Close a fsdir cursor.211*/212static int fsdirClose(sqlite3_vtab_cursor *cur){213  FsdirCsr *pCur = (FsdirCsr*)cur;214  if( pCur->pDir ) closedir(pCur->pDir);215  sqlite3_free(pCur->zDir);216  sqlite3_free(pCur);217  return SQLITE_OK;218}219 220/*221** Skip the cursor to the next entry.222*/223static int fsdirNext(sqlite3_vtab_cursor *cur){224  FsdirCsr *pCsr = (FsdirCsr*)cur;225 226  if( pCsr->pDir ){227    pCsr->pEntry = readdir(pCsr->pDir);228    if( pCsr->pEntry==0 ){229      closedir(pCsr->pDir);230      pCsr->pDir = 0;231    }232    pCsr->iRowid++;233  }234 235  return SQLITE_OK;236}237 238/*239** xFilter method implementation.240*/241static int fsdirFilter(242  sqlite3_vtab_cursor *pVtabCursor, 243  int idxNum, const char *idxStr,244  int argc, sqlite3_value **argv245){246  FsdirCsr *pCsr = (FsdirCsr*)pVtabCursor;247  const char *zDir;248  int nDir;249 250 251  if( idxNum!=1 || argc!=1 ){252    return SQLITE_ERROR;253  }254 255  pCsr->iRowid = 0;256  sqlite3_free(pCsr->zDir);257  if( pCsr->pDir ){258    closedir(pCsr->pDir);259    pCsr->pDir = 0;260  }261 262  zDir = (const char*)sqlite3_value_text(argv[0]);263  nDir = sqlite3_value_bytes(argv[0]);264  pCsr->zDir = sqlite3_malloc(nDir+1);265  if( pCsr->zDir==0 ) return SQLITE_NOMEM;266  memcpy(pCsr->zDir, zDir, nDir+1);267 268  pCsr->pDir = opendir(pCsr->zDir);269  return fsdirNext(pVtabCursor); 270}271 272/*273** xEof method implementation.274*/275static int fsdirEof(sqlite3_vtab_cursor *cur){276  FsdirCsr *pCsr = (FsdirCsr*)cur;277  return pCsr->pDir==0;278}279 280/*281** xColumn method implementation.282*/283static int fsdirColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){284  FsdirCsr *pCsr = (FsdirCsr*)cur;285  switch( i ){286    case 0: /* dir */287      sqlite3_result_text(ctx, pCsr->zDir, -1, SQLITE_STATIC);288      break;289 290    case 1: /* name */291      sqlite3_result_text(ctx, pCsr->pEntry->d_name, -1, SQLITE_TRANSIENT);292      break;293 294    default:295      assert( 0 );296  }297 298  return SQLITE_OK;299}300 301/*302** xRowid method implementation.303*/304static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){305  FsdirCsr *pCsr = (FsdirCsr*)cur;306  *pRowid = pCsr->iRowid;307  return SQLITE_OK;308}309/*310** End of fsdir implementation.311*************************************************************************/312 313/*************************************************************************314** Start of fstree implementation.315*/316typedef struct FstreeVtab FstreeVtab;317typedef struct FstreeCsr FstreeCsr;318struct FstreeVtab {319  sqlite3_vtab base;320  sqlite3 *db;321};322 323struct FstreeCsr {324  sqlite3_vtab_cursor base;325  sqlite3_stmt *pStmt;            /* Statement to list paths */326  int fd;                         /* File descriptor open on current path */327};328 329/*330** This function is the implementation of both the xConnect and xCreate331** methods of the fstree virtual table.332**333** The argv[] array contains the following:334**335**   argv[0]   -> module name  ("fs")336**   argv[1]   -> database name337**   argv[2]   -> table name338**   argv[...] -> other module argument fields.339*/340static int fstreeConnect(341  sqlite3 *db,342  void *pAux,343  int argc, const char *const*argv,344  sqlite3_vtab **ppVtab,345  char **pzErr346){347  FstreeVtab *pTab;348 349  if( argc!=3 ){350    *pzErr = sqlite3_mprintf("wrong number of arguments");351    return SQLITE_ERROR;352  }353 354  pTab = (FstreeVtab *)sqlite3_malloc(sizeof(FstreeVtab));355  if( !pTab ) return SQLITE_NOMEM;356  memset(pTab, 0, sizeof(FstreeVtab));357  pTab->db = db;358 359  *ppVtab = &pTab->base;360  sqlite3_declare_vtab(db, "CREATE TABLE xyz(path, size, data);");361 362  return SQLITE_OK;363}364 365/*366** xDestroy/xDisconnect implementation.367*/368static int fstreeDisconnect(sqlite3_vtab *pVtab){369  sqlite3_free(pVtab);370  return SQLITE_OK;371}372 373/*374** xBestIndex implementation. The only constraint supported is:375**376**   (dir = ?)377*/378static int fstreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){379  int ii;380 381  for(ii=0; ii<pIdxInfo->nConstraint; ii++){382    struct sqlite3_index_constraint const *p = &pIdxInfo->aConstraint[ii];383    if( p->iColumn==0 && p->usable && (384          p->op==SQLITE_INDEX_CONSTRAINT_GLOB385       || p->op==SQLITE_INDEX_CONSTRAINT_LIKE386       || p->op==SQLITE_INDEX_CONSTRAINT_EQ387    )){388      struct sqlite3_index_constraint_usage *pUsage;389      pUsage = &pIdxInfo->aConstraintUsage[ii];390      pIdxInfo->idxNum = p->op;391      pUsage->argvIndex = 1;392      pIdxInfo->estimatedCost = 100000.0;393      return SQLITE_OK;394    }395  }396 397  pIdxInfo->estimatedCost = 1000000000.0;398  return SQLITE_OK;399}400 401/*402** xOpen implementation.403**404** Open a new fstree cursor.405*/406static int fstreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){407  FstreeCsr *pCur;408  pCur = (FstreeCsr*)sqlite3_malloc(sizeof(FstreeCsr));409  if( pCur==0 ) return SQLITE_NOMEM;410  memset(pCur, 0, sizeof(FstreeCsr));411  pCur->fd = -1;412  *ppCursor = &pCur->base;413  return SQLITE_OK;414}415 416static void fstreeCloseFd(FstreeCsr *pCsr){417  if( pCsr->fd>=0 ){418    close(pCsr->fd);419    pCsr->fd = -1;420  }421}422 423/*424** Close a fstree cursor.425*/426static int fstreeClose(sqlite3_vtab_cursor *cur){427  FstreeCsr *pCsr = (FstreeCsr*)cur;428  sqlite3_finalize(pCsr->pStmt);429  fstreeCloseFd(pCsr);430  sqlite3_free(pCsr);431  return SQLITE_OK;432}433 434/*435** Skip the cursor to the next entry.436*/437static int fstreeNext(sqlite3_vtab_cursor *cur){438  FstreeCsr *pCsr = (FstreeCsr*)cur;439  int rc;440 441  fstreeCloseFd(pCsr);442  rc = sqlite3_step(pCsr->pStmt);443  if( rc!=SQLITE_ROW ){444    rc = sqlite3_finalize(pCsr->pStmt);445    pCsr->pStmt = 0;446  }else{447    rc = SQLITE_OK;448    pCsr->fd = open((const char*)sqlite3_column_text(pCsr->pStmt, 0), O_RDONLY);449  }450 451  return rc;452}453 454/*455** xFilter method implementation.456*/457static int fstreeFilter(458  sqlite3_vtab_cursor *pVtabCursor, 459  int idxNum, const char *idxStr,460  int argc, sqlite3_value **argv461){462  FstreeCsr *pCsr = (FstreeCsr*)pVtabCursor;463  FstreeVtab *pTab = (FstreeVtab*)(pCsr->base.pVtab);464  int rc;465  const char *zSql = 466"WITH r(d) AS ("467"  SELECT CASE WHEN dir=?2 THEN ?3 ELSE dir END || '/' || name "468"    FROM fsdir WHERE dir=?1 AND name NOT LIKE '.%'"469"  UNION ALL"470"  SELECT dir || '/' || name FROM r, fsdir WHERE dir=d AND name NOT LIKE '.%'"471") SELECT d FROM r;";472 473  char *zRoot;474  int nRoot;475  char *zPrefix;476  int nPrefix;477  const char *zDir;478  int nDir;479  char aWild[2] = { '\0', '\0' };480 481#ifdef _WIN32482  const char *zDrive = getenv("fstreeDrive");483  if( zDrive==0 ){484    zDrive = getenv("SystemDrive");485  }486  zRoot = sqlite3_mprintf("%s%c", zDrive, '/');487  nRoot = sqlite3Strlen30(zRoot);488  zPrefix = sqlite3_mprintf("%s", zDrive);489  nPrefix = sqlite3Strlen30(zPrefix);490#else491  zRoot = "/";492  nRoot = 1;493  zPrefix = "";494  nPrefix = 0;495#endif496 497  zDir = zRoot;498  nDir = nRoot;499 500  fstreeCloseFd(pCsr);501  sqlite3_finalize(pCsr->pStmt);502  pCsr->pStmt = 0;503  rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);504  if( rc!=SQLITE_OK ) return rc;505 506  if( idxNum ){507    const char *zQuery = (const char*)sqlite3_value_text(argv[0]);508    switch( idxNum ){509      case SQLITE_INDEX_CONSTRAINT_GLOB:510        aWild[0] = '*';511        aWild[1] = '?';512        break;513      case SQLITE_INDEX_CONSTRAINT_LIKE:514        aWild[0] = '_';515        aWild[1] = '%';516        break;517    }518 519    if( sqlite3_strnicmp(zQuery, zPrefix, nPrefix)==0 ){520      int i;521      for(i=nPrefix; zQuery[i]; i++){522        if( zQuery[i]==aWild[0] || zQuery[i]==aWild[1] ) break;523        if( zQuery[i]=='/' ) nDir = i;524      }525      zDir = zQuery;526    }527  }528  if( nDir==0 ) nDir = 1;529 530  sqlite3_bind_text(pCsr->pStmt, 1, zDir, nDir, SQLITE_TRANSIENT);531  sqlite3_bind_text(pCsr->pStmt, 2, zRoot, nRoot, SQLITE_TRANSIENT);532  sqlite3_bind_text(pCsr->pStmt, 3, zPrefix, nPrefix, SQLITE_TRANSIENT);533 534#ifdef _WIN32535  sqlite3_free(zPrefix);536  sqlite3_free(zRoot);537#endif538 539  return fstreeNext(pVtabCursor); 540}541 542/*543** xEof method implementation.544*/545static int fstreeEof(sqlite3_vtab_cursor *cur){546  FstreeCsr *pCsr = (FstreeCsr*)cur;547  return pCsr->pStmt==0;548}549 550/*551** xColumn method implementation.552*/553static int fstreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){554  FstreeCsr *pCsr = (FstreeCsr*)cur;555  if( i==0 ){      /* path */556    sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, 0));557  }else{558    struct stat sBuf;559    fstat(pCsr->fd, &sBuf);560 561    if( S_ISREG(sBuf.st_mode) ){562      if( i==1 ){563        sqlite3_result_int64(ctx, sBuf.st_size);564      }else{565        int nRead;566        char *aBuf = sqlite3_malloc(sBuf.st_mode+1);567        if( !aBuf ) return SQLITE_NOMEM;568        nRead = read(pCsr->fd, aBuf, sBuf.st_mode);569        if( nRead!=sBuf.st_mode ){570          return SQLITE_IOERR;571        }572        sqlite3_result_blob(ctx, aBuf, nRead, SQLITE_TRANSIENT);573        sqlite3_free(aBuf);574      }575    }576  }577 578  return SQLITE_OK;579}580 581/*582** xRowid method implementation.583*/584static int fstreeRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){585  *pRowid = 0;586  return SQLITE_OK;587}588/*589** End of fstree implementation.590*************************************************************************/591 592 593 594 595/*596** This function is the implementation of both the xConnect and xCreate597** methods of the fs virtual table.598**599** The argv[] array contains the following:600**601**   argv[0]   -> module name  ("fs")602**   argv[1]   -> database name603**   argv[2]   -> table name604**   argv[...] -> other module argument fields.605*/606static int fsConnect(607  sqlite3 *db,608  void *pAux,609  int argc, const char *const*argv,610  sqlite3_vtab **ppVtab,611  char **pzErr612){613  fs_vtab *pVtab;614  int nByte;615  const char *zTbl;616  const char *zDb = argv[1];617 618  if( argc!=4 ){619    *pzErr = sqlite3_mprintf("wrong number of arguments");620    return SQLITE_ERROR;621  }622  zTbl = argv[3];623 624  nByte = sizeof(fs_vtab) + (int)strlen(zTbl) + 1 + (int)strlen(zDb) + 1;625  pVtab = (fs_vtab *)sqlite3MallocZero( nByte );626  if( !pVtab ) return SQLITE_NOMEM;627 628  pVtab->zTbl = (char *)&pVtab[1];629  pVtab->zDb = &pVtab->zTbl[strlen(zTbl)+1];630  pVtab->db = db;631  memcpy(pVtab->zTbl, zTbl, strlen(zTbl));632  memcpy(pVtab->zDb, zDb, strlen(zDb));633  *ppVtab = &pVtab->base;634  sqlite3_declare_vtab(db, "CREATE TABLE x(path TEXT, data TEXT)");635 636  return SQLITE_OK;637}638/* Note that for this virtual table, the xCreate and xConnect639** methods are identical. */640 641static int fsDisconnect(sqlite3_vtab *pVtab){642  sqlite3_free(pVtab);643  return SQLITE_OK;644}645/* The xDisconnect and xDestroy methods are also the same */646 647/*648** Open a new fs cursor.649*/650static int fsOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){651  fs_cursor *pCur;652  pCur = sqlite3MallocZero(sizeof(fs_cursor));653  *ppCursor = &pCur->base;654  return SQLITE_OK;655}656 657/*658** Close a fs cursor.659*/660static int fsClose(sqlite3_vtab_cursor *cur){661  fs_cursor *pCur = (fs_cursor *)cur;662  sqlite3_finalize(pCur->pStmt);663  sqlite3_free(pCur->zBuf);664  sqlite3_free(pCur);665  return SQLITE_OK;666}667 668static int fsNext(sqlite3_vtab_cursor *cur){669  fs_cursor *pCur = (fs_cursor *)cur;670  int rc;671 672  rc = sqlite3_step(pCur->pStmt);673  if( rc==SQLITE_ROW || rc==SQLITE_DONE ) rc = SQLITE_OK;674 675  return rc;676}677 678static int fsFilter(679  sqlite3_vtab_cursor *pVtabCursor, 680  int idxNum, const char *idxStr,681  int argc, sqlite3_value **argv682){683  int rc;684  fs_cursor *pCur = (fs_cursor *)pVtabCursor;685  fs_vtab *p = (fs_vtab *)(pVtabCursor->pVtab);686 687  assert( (idxNum==0 && argc==0) || (idxNum==1 && argc==1) );688  if( idxNum==1 ){689    char *zStmt = sqlite3_mprintf(690        "SELECT * FROM %Q.%Q WHERE rowid=?", p->zDb, p->zTbl);691    if( !zStmt ) return SQLITE_NOMEM;692    rc = sqlite3_prepare_v2(p->db, zStmt, -1, &pCur->pStmt, 0);693    sqlite3_free(zStmt);694    if( rc==SQLITE_OK ){695      sqlite3_bind_value(pCur->pStmt, 1, argv[0]);696    }697  }else{698    char *zStmt = sqlite3_mprintf("SELECT * FROM %Q.%Q", p->zDb, p->zTbl);699    if( !zStmt ) return SQLITE_NOMEM;700    rc = sqlite3_prepare_v2(p->db, zStmt, -1, &pCur->pStmt, 0);701    sqlite3_free(zStmt);702  }703 704  if( rc==SQLITE_OK ){705    rc = fsNext(pVtabCursor); 706  }707  return rc;708}709 710static int fsColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){711  fs_cursor *pCur = (fs_cursor*)cur;712 713  assert( i==0 || i==1 || i==2 );714  if( i==0 ){715    sqlite3_result_value(ctx, sqlite3_column_value(pCur->pStmt, 0));716  }else{717    const char *zFile = (const char *)sqlite3_column_text(pCur->pStmt, 1);718    struct stat sbuf;719    int fd;720 721    int n;722    fd = open(zFile, O_RDONLY);723    if( fd<0 ) return SQLITE_IOERR;724    fstat(fd, &sbuf);725 726    if( sbuf.st_size>=pCur->nAlloc ){727      sqlite3_int64 nNew = sbuf.st_size*2;728      char *zNew;729      if( nNew<1024 ) nNew = 1024;730 731      zNew = sqlite3Realloc(pCur->zBuf, nNew);732      if( zNew==0 ){733        close(fd);734        return SQLITE_NOMEM;735      }736      pCur->zBuf = zNew;737      pCur->nAlloc = nNew;738    }739 740    n = (int)read(fd, pCur->zBuf, sbuf.st_size);741    close(fd);742    if( n!=sbuf.st_size ) return SQLITE_ERROR;743    pCur->nBuf = sbuf.st_size;744    pCur->zBuf[pCur->nBuf] = '\0';745 746    sqlite3_result_text(ctx, pCur->zBuf, -1, SQLITE_TRANSIENT);747  }748  return SQLITE_OK;749}750 751static int fsRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){752  fs_cursor *pCur = (fs_cursor*)cur;753  *pRowid = sqlite3_column_int64(pCur->pStmt, 0);754  return SQLITE_OK;755}756 757static int fsEof(sqlite3_vtab_cursor *cur){758  fs_cursor *pCur = (fs_cursor*)cur;759  return (sqlite3_data_count(pCur->pStmt)==0);760}761 762static int fsBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){763  int ii;764 765  for(ii=0; ii<pIdxInfo->nConstraint; ii++){766    struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];767    if( pCons->iColumn<0 && pCons->usable768           && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){769      struct sqlite3_index_constraint_usage *pUsage;770      pUsage = &pIdxInfo->aConstraintUsage[ii];771      pUsage->omit = 0;772      pUsage->argvIndex = 1;773      pIdxInfo->idxNum = 1;774      pIdxInfo->estimatedCost = 1.0;775      break;776    }777  }778 779  return SQLITE_OK;780}781 782/*783** A virtual table module that provides read-only access to a784** Tcl global variable namespace.785*/786static sqlite3_module fsModule = {787  0,                         /* iVersion */788  fsConnect,789  fsConnect,790  fsBestIndex,791  fsDisconnect, 792  fsDisconnect,793  fsOpen,                      /* xOpen - open a cursor */794  fsClose,                     /* xClose - close a cursor */795  fsFilter,                    /* xFilter - configure scan constraints */796  fsNext,                      /* xNext - advance a cursor */797  fsEof,                       /* xEof - check for end of scan */798  fsColumn,                    /* xColumn - read data */799  fsRowid,                     /* xRowid - read data */800  0,                           /* xUpdate */801  0,                           /* xBegin */802  0,                           /* xSync */803  0,                           /* xCommit */804  0,                           /* xRollback */805  0,                           /* xFindMethod */806  0,                           /* xRename */807  0,                           /* xSavepoint */808  0,                           /* xRelease */809  0,                           /* xRollbackTo */810  0,                           /* xShadowName */811  0                            /* xIntegrity */812};813 814static sqlite3_module fsdirModule = {815  0,                              /* iVersion */816  fsdirConnect,                   /* xCreate */817  fsdirConnect,                   /* xConnect */818  fsdirBestIndex,                 /* xBestIndex */819  fsdirDisconnect,                /* xDisconnect */820  fsdirDisconnect,                /* xDestroy */821  fsdirOpen,                      /* xOpen - open a cursor */822  fsdirClose,                     /* xClose - close a cursor */823  fsdirFilter,                    /* xFilter - configure scan constraints */824  fsdirNext,                      /* xNext - advance a cursor */825  fsdirEof,                       /* xEof - check for end of scan */826  fsdirColumn,                    /* xColumn - read data */827  fsdirRowid,                     /* xRowid - read data */828  0,                              /* xUpdate */829  0,                              /* xBegin */830  0,                              /* xSync */831  0,                              /* xCommit */832  0,                              /* xRollback */833  0,                              /* xFindMethod */834  0,                              /* xRename */835  0,                              /* xSavepoint */836  0,                              /* xRelease */837  0,                              /* xRollbackTo */838  0,                              /* xShadowName */839  0                               /* xIntegrity */840};841 842static sqlite3_module fstreeModule = {843  0,                              /* iVersion */844  fstreeConnect,                  /* xCreate */845  fstreeConnect,                  /* xConnect */846  fstreeBestIndex,                /* xBestIndex */847  fstreeDisconnect,               /* xDisconnect */848  fstreeDisconnect,               /* xDestroy */849  fstreeOpen,                     /* xOpen - open a cursor */850  fstreeClose,                    /* xClose - close a cursor */851  fstreeFilter,                   /* xFilter - configure scan constraints */852  fstreeNext,                     /* xNext - advance a cursor */853  fstreeEof,                      /* xEof - check for end of scan */854  fstreeColumn,                   /* xColumn - read data */855  fstreeRowid,                    /* xRowid - read data */856  0,                              /* xUpdate */857  0,                              /* xBegin */858  0,                              /* xSync */859  0,                              /* xCommit */860  0,                              /* xRollback */861  0,                              /* xFindMethod */862  0,                              /* xRename */863  0,                              /* xSavepoint */864  0,                              /* xRelease */865  0,                              /* xRollbackTo */866  0,                              /* xShadowName */867  0                               /* xIntegrity */868};869 870/*871** Decode a pointer to an sqlite3 object.872*/873extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);874 875/*876** Register the echo virtual table module.877*/878static int SQLITE_TCLAPI register_fs_module(879  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */880  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */881  int objc,              /* Number of arguments */882  Tcl_Obj *CONST objv[]  /* Command arguments */883){884  sqlite3 *db;885  if( objc!=2 ){886    Tcl_WrongNumArgs(interp, 1, objv, "DB");887    return TCL_ERROR;888  }889  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;890#ifndef SQLITE_OMIT_VIRTUALTABLE891  sqlite3_create_module(db, "fs", &fsModule, (void *)interp);892  sqlite3_create_module(db, "fsdir", &fsdirModule, 0);893  sqlite3_create_module(db, "fstree", &fstreeModule, 0);894#endif895  return TCL_OK;896}897 898#endif899 900 901/*902** Register commands with the TCL interpreter.903*/904int Sqlitetestfs_Init(Tcl_Interp *interp){905#ifndef SQLITE_OMIT_VIRTUALTABLE906  static struct {907     char *zName;908     Tcl_ObjCmdProc *xProc;909     void *clientData;910  } aObjCmd[] = {911     { "register_fs_module",   register_fs_module, 0 },912  };913  int i;914  for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){915    Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 916        aObjCmd[i].xProc, aObjCmd[i].clientData, 0);917  }918#endif919  return TCL_OK;920}921