mirror of
https://github.com/sqlite/sqlite.git
synced 2025-11-14 00:22:38 +03:00
Update test_fs.c to include a virtual table that reads a file-system hierarchy. Use it to further test GLOB and LIKE support for virtual tables.
FossilOrigin-Name: 6ef6578c03b7cfbeaaf3627b9eea2febf501ace5
This commit is contained in:
18
manifest
18
manifest
@@ -1,5 +1,5 @@
|
|||||||
C Make\sthe\sxAccess\smethod\sof\sthe\sunix\sVFS\ssmaller\sand\sfaster.
|
C Update\stest_fs.c\sto\sinclude\sa\svirtual\stable\sthat\sreads\sa\sfile-system\shierarchy.\sUse\sit\sto\sfurther\stest\sGLOB\sand\sLIKE\ssupport\sfor\svirtual\stables.
|
||||||
D 2015-11-25T18:03:33.041
|
D 2015-11-25T18:07:46.837
|
||||||
F Makefile.in d828db6afa6c1fa060d01e33e4674408df1942a1
|
F Makefile.in d828db6afa6c1fa060d01e33e4674408df1942a1
|
||||||
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
|
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
|
||||||
F Makefile.msc e928e68168df69b353300ac87c10105206653a03
|
F Makefile.msc e928e68168df69b353300ac87c10105206653a03
|
||||||
@@ -366,7 +366,7 @@ F src/test_btree.c 2e9978eca99a9a4bfa8cae949efb00886860a64f
|
|||||||
F src/test_config.c f2824de39f59d8d621e2d6ec5cc67006d000b2eb
|
F src/test_config.c f2824de39f59d8d621e2d6ec5cc67006d000b2eb
|
||||||
F src/test_demovfs.c 0de72c2c89551629f58486fde5734b7d90758852
|
F src/test_demovfs.c 0de72c2c89551629f58486fde5734b7d90758852
|
||||||
F src/test_devsym.c e7498904e72ba7491d142d5c83b476c4e76993bc
|
F src/test_devsym.c e7498904e72ba7491d142d5c83b476c4e76993bc
|
||||||
F src/test_fs.c ced436e3d4b8e4681328409b8081051ce614e28f
|
F src/test_fs.c 802b1863c77f0531603c4a70f30ac5bca89a60da
|
||||||
F src/test_func.c 0d9c25956152adefee8881c6fadc8354793764d0
|
F src/test_func.c 0d9c25956152adefee8881c6fadc8354793764d0
|
||||||
F src/test_hexio.c abfdecb6fa58c354623978efceb088ca18e379cd
|
F src/test_hexio.c abfdecb6fa58c354623978efceb088ca18e379cd
|
||||||
F src/test_init.c 66b33120ffe9cd853b5a905ec850d51151337b32
|
F src/test_init.c 66b33120ffe9cd853b5a905ec850d51151337b32
|
||||||
@@ -728,7 +728,7 @@ F test/fts3tok_err.test 52273cd193b9036282f7bacb43da78c6be87418d
|
|||||||
F test/fts3varint.test 752c08ed5d32c5d7dc211b056f4ed68a76b7e36e
|
F test/fts3varint.test 752c08ed5d32c5d7dc211b056f4ed68a76b7e36e
|
||||||
F test/fts4aa.test 10aac8e9d62c7357590acfabe3fad01e9a9ce1cb
|
F test/fts4aa.test 10aac8e9d62c7357590acfabe3fad01e9a9ce1cb
|
||||||
F test/fts4check.test 9d9e818fd6cb29c0e007cd6d00447739d4fde430
|
F test/fts4check.test 9d9e818fd6cb29c0e007cd6d00447739d4fde430
|
||||||
F test/fts4content.test abb0c77bc3da3df64fec72e00844d2257a90025d
|
F test/fts4content.test 8707425b926663f8ca81de866c007900442b5ec0
|
||||||
F test/fts4docid.test e33c383cfbdff0284685604d256f347a18fdbf01
|
F test/fts4docid.test e33c383cfbdff0284685604d256f347a18fdbf01
|
||||||
F test/fts4growth.test df10fde9f47cf5c71861e63fd8efcd573c4f7e53
|
F test/fts4growth.test df10fde9f47cf5c71861e63fd8efcd573c4f7e53
|
||||||
F test/fts4growth2.test 2f063be1902a73cd087355837c52fed42ac11a5d
|
F test/fts4growth2.test 2f063be1902a73cd087355837c52fed42ac11a5d
|
||||||
@@ -1274,7 +1274,7 @@ F test/vtabC.test 4528f459a13136f982e75614d120aef165f17292
|
|||||||
F test/vtabD.test 05b3f1d77117271671089e48719524b676842e96
|
F test/vtabD.test 05b3f1d77117271671089e48719524b676842e96
|
||||||
F test/vtabE.test d5024aa42754962f6bb0afd261681686488e7afe
|
F test/vtabE.test d5024aa42754962f6bb0afd261681686488e7afe
|
||||||
F test/vtabF.test fd5ad376f5a34fe0891df1f3cddb4fe7c3eb077e
|
F test/vtabF.test fd5ad376f5a34fe0891df1f3cddb4fe7c3eb077e
|
||||||
F test/vtabH.test 694aa399eb28ed0db2aef59f2f37532781eeb957
|
F test/vtabH.test 2030e7183e41c3cc7521c06aa7bc5a232f7e8986
|
||||||
F test/vtab_alter.test 9e374885248f69e251bdaacf480b04a197f125e5
|
F test/vtab_alter.test 9e374885248f69e251bdaacf480b04a197f125e5
|
||||||
F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8
|
F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8
|
||||||
F test/vtab_shared.test ea8778d5b0df200adef2ca7c00c3c37d4375f772
|
F test/vtab_shared.test ea8778d5b0df200adef2ca7c00c3c37d4375f772
|
||||||
@@ -1405,7 +1405,7 @@ F tool/vdbe_profile.tcl 246d0da094856d72d2c12efec03250d71639d19f
|
|||||||
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
|
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
|
||||||
F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
|
F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
|
||||||
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
|
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
|
||||||
P 228bd15bbb7a1e6e3e0d03832e7f39ba169356a8
|
P 191aef986ffc4ef34d813e417e52a4ec820b0300
|
||||||
R e147c2a2e15689c8fb9a94a36196c06f
|
R 98283e54084b319f4a987e20b98e348d
|
||||||
U drh
|
U dan
|
||||||
Z 62ff0fd6ec1e0622890fd7a98828488f
|
Z 4eb24c0ce56061f040de45e89ced3c69
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
191aef986ffc4ef34d813e417e52a4ec820b0300
|
6ef6578c03b7cfbeaaf3627b9eea2febf501ace5
|
||||||
543
src/test_fs.c
543
src/test_fs.c
@@ -29,6 +29,37 @@
|
|||||||
** Adding the row to the idx table automatically creates a row in the
|
** Adding the row to the idx table automatically creates a row in the
|
||||||
** virtual table with rowid=4, path=/etc/passwd and a text field that
|
** virtual table with rowid=4, path=/etc/passwd and a text field that
|
||||||
** contains data read from file /etc/passwd on disk.
|
** contains data read from file /etc/passwd on disk.
|
||||||
|
**
|
||||||
|
*************************************************************************
|
||||||
|
** Virtual table module "fsdir"
|
||||||
|
**
|
||||||
|
** This module is designed to be used as a read-only eponymous virtual table.
|
||||||
|
** Its schema is as follows:
|
||||||
|
**
|
||||||
|
** CREATE TABLE fsdir(dir TEXT, name TEXT);
|
||||||
|
**
|
||||||
|
** When queried, a WHERE term of the form "dir = $dir" must be provided. The
|
||||||
|
** virtual table then appears to have one row for each entry in file-system
|
||||||
|
** directory $dir. Column dir contains a copy of $dir, and column "name"
|
||||||
|
** contains the name of the directory entry.
|
||||||
|
**
|
||||||
|
** If the specified $dir cannot be opened or is not a directory, it is not
|
||||||
|
** an error. The virtual table appears to be empty in this case.
|
||||||
|
**
|
||||||
|
*************************************************************************
|
||||||
|
** Virtual table module "fstree"
|
||||||
|
**
|
||||||
|
** This module is also a read-only eponymous virtual table with the
|
||||||
|
** following schema:
|
||||||
|
**
|
||||||
|
** CREATE TABLE fstree(path TEXT, size INT, data BLOB);
|
||||||
|
**
|
||||||
|
** Running a "SELECT * FROM fstree" query on this table returns the entire
|
||||||
|
** contents of the file-system, starting at "/". To restrict the search
|
||||||
|
** space, the virtual table supports LIKE and GLOB constraints on the
|
||||||
|
** 'path' column. For example:
|
||||||
|
**
|
||||||
|
** SELECT * FROM fstree WHERE path LIKE '/home/dan/sqlite/%'
|
||||||
*/
|
*/
|
||||||
#include "sqliteInt.h"
|
#include "sqliteInt.h"
|
||||||
#include "tcl.h"
|
#include "tcl.h"
|
||||||
@@ -41,6 +72,7 @@
|
|||||||
|
|
||||||
#if SQLITE_OS_UNIX
|
#if SQLITE_OS_UNIX
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
|
# include <dirent.h>
|
||||||
#endif
|
#endif
|
||||||
#if SQLITE_OS_WIN
|
#if SQLITE_OS_WIN
|
||||||
# include <io.h>
|
# include <io.h>
|
||||||
@@ -70,6 +102,463 @@ struct fs_cursor {
|
|||||||
int nAlloc;
|
int nAlloc;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
** Start of fsdir implementation.
|
||||||
|
*/
|
||||||
|
typedef struct FsdirVtab FsdirVtab;
|
||||||
|
typedef struct FsdirCsr FsdirCsr;
|
||||||
|
struct FsdirVtab {
|
||||||
|
sqlite3_vtab base;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FsdirCsr {
|
||||||
|
sqlite3_vtab_cursor base;
|
||||||
|
char *zDir; /* Buffer containing directory scanned */
|
||||||
|
DIR *pDir; /* Open directory */
|
||||||
|
sqlite3_int64 iRowid;
|
||||||
|
struct dirent entry; /* Current entry */
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
** This function is the implementation of both the xConnect and xCreate
|
||||||
|
** methods of the fsdir virtual table.
|
||||||
|
**
|
||||||
|
** The argv[] array contains the following:
|
||||||
|
**
|
||||||
|
** argv[0] -> module name ("fs")
|
||||||
|
** argv[1] -> database name
|
||||||
|
** argv[2] -> table name
|
||||||
|
** argv[...] -> other module argument fields.
|
||||||
|
*/
|
||||||
|
static int fsdirConnect(
|
||||||
|
sqlite3 *db,
|
||||||
|
void *pAux,
|
||||||
|
int argc, const char *const*argv,
|
||||||
|
sqlite3_vtab **ppVtab,
|
||||||
|
char **pzErr
|
||||||
|
){
|
||||||
|
FsdirVtab *pTab;
|
||||||
|
|
||||||
|
if( argc!=3 ){
|
||||||
|
*pzErr = sqlite3_mprintf("wrong number of arguments");
|
||||||
|
return SQLITE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
pTab = (FsdirVtab *)sqlite3_malloc(sizeof(FsdirVtab));
|
||||||
|
if( !pTab ) return SQLITE_NOMEM;
|
||||||
|
memset(pTab, 0, sizeof(FsdirVtab));
|
||||||
|
|
||||||
|
*ppVtab = &pTab->base;
|
||||||
|
sqlite3_declare_vtab(db, "CREATE TABLE xyz(dir, name);");
|
||||||
|
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xDestroy/xDisconnect implementation.
|
||||||
|
*/
|
||||||
|
static int fsdirDisconnect(sqlite3_vtab *pVtab){
|
||||||
|
sqlite3_free(pVtab);
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xBestIndex implementation. The only constraint supported is:
|
||||||
|
**
|
||||||
|
** (dir = ?)
|
||||||
|
*/
|
||||||
|
static int fsdirBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
|
||||||
|
int ii;
|
||||||
|
|
||||||
|
pIdxInfo->estimatedCost = 1000000000.0;
|
||||||
|
|
||||||
|
for(ii=0; ii<pIdxInfo->nConstraint; ii++){
|
||||||
|
struct sqlite3_index_constraint const *p = &pIdxInfo->aConstraint[ii];
|
||||||
|
if( p->iColumn==0 && p->usable && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
|
||||||
|
struct sqlite3_index_constraint_usage *pUsage;
|
||||||
|
pUsage = &pIdxInfo->aConstraintUsage[ii];
|
||||||
|
pUsage->omit = 1;
|
||||||
|
pUsage->argvIndex = 1;
|
||||||
|
pIdxInfo->idxNum = 1;
|
||||||
|
pIdxInfo->estimatedCost = 1.0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xOpen implementation.
|
||||||
|
**
|
||||||
|
** Open a new fsdir cursor.
|
||||||
|
*/
|
||||||
|
static int fsdirOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
|
||||||
|
FsdirCsr *pCur;
|
||||||
|
pCur = (FsdirCsr*)sqlite3_malloc(sizeof(FsdirCsr));
|
||||||
|
if( pCur==0 ) return SQLITE_NOMEM;
|
||||||
|
memset(pCur, 0, sizeof(FsdirCsr));
|
||||||
|
*ppCursor = &pCur->base;
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Close a fsdir cursor.
|
||||||
|
*/
|
||||||
|
static int fsdirClose(sqlite3_vtab_cursor *cur){
|
||||||
|
FsdirCsr *pCur = (FsdirCsr*)cur;
|
||||||
|
if( pCur->pDir ) closedir(pCur->pDir);
|
||||||
|
sqlite3_free(pCur->zDir);
|
||||||
|
sqlite3_free(pCur);
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Skip the cursor to the next entry.
|
||||||
|
*/
|
||||||
|
static int fsdirNext(sqlite3_vtab_cursor *cur){
|
||||||
|
FsdirCsr *pCsr = (FsdirCsr*)cur;
|
||||||
|
|
||||||
|
if( pCsr->pDir ){
|
||||||
|
struct dirent *pRes = 0;
|
||||||
|
readdir_r(pCsr->pDir, &pCsr->entry, &pRes);
|
||||||
|
if( pRes==0 ){
|
||||||
|
closedir(pCsr->pDir);
|
||||||
|
pCsr->pDir = 0;
|
||||||
|
}
|
||||||
|
pCsr->iRowid++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xFilter method implementation.
|
||||||
|
*/
|
||||||
|
static int fsdirFilter(
|
||||||
|
sqlite3_vtab_cursor *pVtabCursor,
|
||||||
|
int idxNum, const char *idxStr,
|
||||||
|
int argc, sqlite3_value **argv
|
||||||
|
){
|
||||||
|
FsdirCsr *pCsr = (FsdirCsr*)pVtabCursor;
|
||||||
|
int rc;
|
||||||
|
const char *zDir;
|
||||||
|
int nDir;
|
||||||
|
|
||||||
|
|
||||||
|
if( idxNum!=1 || argc!=1 ){
|
||||||
|
return SQLITE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
pCsr->iRowid = 0;
|
||||||
|
sqlite3_free(pCsr->zDir);
|
||||||
|
if( pCsr->pDir ){
|
||||||
|
closedir(pCsr->pDir);
|
||||||
|
pCsr->pDir = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
zDir = (const char*)sqlite3_value_text(argv[0]);
|
||||||
|
nDir = sqlite3_value_bytes(argv[0]);
|
||||||
|
pCsr->zDir = sqlite3_malloc(nDir+1);
|
||||||
|
if( pCsr->zDir==0 ) return SQLITE_NOMEM;
|
||||||
|
memcpy(pCsr->zDir, zDir, nDir+1);
|
||||||
|
|
||||||
|
pCsr->pDir = opendir(pCsr->zDir);
|
||||||
|
return fsdirNext(pVtabCursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xEof method implementation.
|
||||||
|
*/
|
||||||
|
static int fsdirEof(sqlite3_vtab_cursor *cur){
|
||||||
|
FsdirCsr *pCsr = (FsdirCsr*)cur;
|
||||||
|
return pCsr->pDir==0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xColumn method implementation.
|
||||||
|
*/
|
||||||
|
static int fsdirColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
|
||||||
|
FsdirCsr *pCsr = (FsdirCsr*)cur;
|
||||||
|
switch( i ){
|
||||||
|
case 0: /* dir */
|
||||||
|
sqlite3_result_text(ctx, pCsr->zDir, -1, SQLITE_STATIC);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1: /* name */
|
||||||
|
sqlite3_result_text(ctx, pCsr->entry.d_name, -1, SQLITE_TRANSIENT);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
assert( 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xRowid method implementation.
|
||||||
|
*/
|
||||||
|
static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
|
||||||
|
FsdirCsr *pCsr = (FsdirCsr*)cur;
|
||||||
|
*pRowid = pCsr->iRowid;
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
** End of fsdir implementation.
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
** Start of fstree implementation.
|
||||||
|
*/
|
||||||
|
typedef struct FstreeVtab FstreeVtab;
|
||||||
|
typedef struct FstreeCsr FstreeCsr;
|
||||||
|
struct FstreeVtab {
|
||||||
|
sqlite3_vtab base;
|
||||||
|
sqlite3 *db;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FstreeCsr {
|
||||||
|
sqlite3_vtab_cursor base;
|
||||||
|
sqlite3_stmt *pStmt; /* Statement to list paths */
|
||||||
|
int fd; /* File descriptor open on current path */
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
** This function is the implementation of both the xConnect and xCreate
|
||||||
|
** methods of the fstree virtual table.
|
||||||
|
**
|
||||||
|
** The argv[] array contains the following:
|
||||||
|
**
|
||||||
|
** argv[0] -> module name ("fs")
|
||||||
|
** argv[1] -> database name
|
||||||
|
** argv[2] -> table name
|
||||||
|
** argv[...] -> other module argument fields.
|
||||||
|
*/
|
||||||
|
static int fstreeConnect(
|
||||||
|
sqlite3 *db,
|
||||||
|
void *pAux,
|
||||||
|
int argc, const char *const*argv,
|
||||||
|
sqlite3_vtab **ppVtab,
|
||||||
|
char **pzErr
|
||||||
|
){
|
||||||
|
FstreeVtab *pTab;
|
||||||
|
|
||||||
|
if( argc!=3 ){
|
||||||
|
*pzErr = sqlite3_mprintf("wrong number of arguments");
|
||||||
|
return SQLITE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
pTab = (FstreeVtab *)sqlite3_malloc(sizeof(FstreeVtab));
|
||||||
|
if( !pTab ) return SQLITE_NOMEM;
|
||||||
|
memset(pTab, 0, sizeof(FstreeVtab));
|
||||||
|
pTab->db = db;
|
||||||
|
|
||||||
|
*ppVtab = &pTab->base;
|
||||||
|
sqlite3_declare_vtab(db, "CREATE TABLE xyz(path, size, data);");
|
||||||
|
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xDestroy/xDisconnect implementation.
|
||||||
|
*/
|
||||||
|
static int fstreeDisconnect(sqlite3_vtab *pVtab){
|
||||||
|
sqlite3_free(pVtab);
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xBestIndex implementation. The only constraint supported is:
|
||||||
|
**
|
||||||
|
** (dir = ?)
|
||||||
|
*/
|
||||||
|
static int fstreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
|
||||||
|
int ii;
|
||||||
|
|
||||||
|
for(ii=0; ii<pIdxInfo->nConstraint; ii++){
|
||||||
|
struct sqlite3_index_constraint const *p = &pIdxInfo->aConstraint[ii];
|
||||||
|
if( p->iColumn==0 && p->usable && (
|
||||||
|
p->op==SQLITE_INDEX_CONSTRAINT_GLOB
|
||||||
|
|| p->op==SQLITE_INDEX_CONSTRAINT_LIKE
|
||||||
|
|| p->op==SQLITE_INDEX_CONSTRAINT_EQ
|
||||||
|
)){
|
||||||
|
struct sqlite3_index_constraint_usage *pUsage;
|
||||||
|
pUsage = &pIdxInfo->aConstraintUsage[ii];
|
||||||
|
pIdxInfo->idxNum = p->op;
|
||||||
|
pUsage->argvIndex = 1;
|
||||||
|
pIdxInfo->estimatedCost = 100000.0;
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pIdxInfo->estimatedCost = 1000000000.0;
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xOpen implementation.
|
||||||
|
**
|
||||||
|
** Open a new fstree cursor.
|
||||||
|
*/
|
||||||
|
static int fstreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
|
||||||
|
FstreeCsr *pCur;
|
||||||
|
pCur = (FstreeCsr*)sqlite3_malloc(sizeof(FstreeCsr));
|
||||||
|
if( pCur==0 ) return SQLITE_NOMEM;
|
||||||
|
memset(pCur, 0, sizeof(FstreeCsr));
|
||||||
|
pCur->fd = -1;
|
||||||
|
*ppCursor = &pCur->base;
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fstreeCloseFd(FstreeCsr *pCsr){
|
||||||
|
if( pCsr->fd>=0 ){
|
||||||
|
close(pCsr->fd);
|
||||||
|
pCsr->fd = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Close a fstree cursor.
|
||||||
|
*/
|
||||||
|
static int fstreeClose(sqlite3_vtab_cursor *cur){
|
||||||
|
FstreeCsr *pCsr = (FstreeCsr*)cur;
|
||||||
|
sqlite3_finalize(pCsr->pStmt);
|
||||||
|
fstreeCloseFd(pCsr);
|
||||||
|
sqlite3_free(pCsr);
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Skip the cursor to the next entry.
|
||||||
|
*/
|
||||||
|
static int fstreeNext(sqlite3_vtab_cursor *cur){
|
||||||
|
FstreeCsr *pCsr = (FstreeCsr*)cur;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
fstreeCloseFd(pCsr);
|
||||||
|
rc = sqlite3_step(pCsr->pStmt);
|
||||||
|
if( rc!=SQLITE_ROW ){
|
||||||
|
rc = sqlite3_finalize(pCsr->pStmt);
|
||||||
|
pCsr->pStmt = 0;
|
||||||
|
}else{
|
||||||
|
rc = SQLITE_OK;
|
||||||
|
pCsr->fd = open(sqlite3_column_text(pCsr->pStmt, 0), O_RDONLY);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xFilter method implementation.
|
||||||
|
*/
|
||||||
|
static int fstreeFilter(
|
||||||
|
sqlite3_vtab_cursor *pVtabCursor,
|
||||||
|
int idxNum, const char *idxStr,
|
||||||
|
int argc, sqlite3_value **argv
|
||||||
|
){
|
||||||
|
FstreeCsr *pCsr = (FstreeCsr*)pVtabCursor;
|
||||||
|
FstreeVtab *pTab = (FstreeVtab*)(pCsr->base.pVtab);
|
||||||
|
int rc;
|
||||||
|
const char *zSql =
|
||||||
|
"WITH r(d) AS ("
|
||||||
|
" SELECT CASE WHEN dir='/' THEN '' ELSE dir END || '/' || name "
|
||||||
|
" FROM fsdir WHERE dir=? AND name NOT LIKE '.%'"
|
||||||
|
" UNION ALL"
|
||||||
|
" SELECT dir || '/' || name FROM r, fsdir WHERE dir=d AND name NOT LIKE '.%'"
|
||||||
|
") SELECT d FROM r;";
|
||||||
|
|
||||||
|
const char *zDir = "/";
|
||||||
|
int nDir = 1;
|
||||||
|
char aWild[2] = {'\0', '\0' };
|
||||||
|
|
||||||
|
fstreeCloseFd(pCsr);
|
||||||
|
sqlite3_finalize(pCsr->pStmt);
|
||||||
|
pCsr->pStmt = 0;
|
||||||
|
rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
|
||||||
|
if( rc!=SQLITE_OK ) return rc;
|
||||||
|
|
||||||
|
if( idxNum ){
|
||||||
|
const char *zQuery = (const char*)sqlite3_value_text(argv[0]);
|
||||||
|
switch( idxNum ){
|
||||||
|
case SQLITE_INDEX_CONSTRAINT_GLOB:
|
||||||
|
aWild[0] = '*';
|
||||||
|
aWild[1] = '?';
|
||||||
|
break;
|
||||||
|
case SQLITE_INDEX_CONSTRAINT_LIKE:
|
||||||
|
aWild[0] = '_';
|
||||||
|
aWild[1] = '%';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( zQuery[0]=='/' ){
|
||||||
|
int i;
|
||||||
|
for(i=1; zQuery[i]; i++){
|
||||||
|
if( zQuery[i]==aWild[0] || zQuery[i]==aWild[1] ) break;
|
||||||
|
if( zQuery[i]=='/' ) nDir = i;
|
||||||
|
}
|
||||||
|
zDir = zQuery;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlite3_bind_text(pCsr->pStmt, 1, zDir, nDir, SQLITE_TRANSIENT);
|
||||||
|
|
||||||
|
return fstreeNext(pVtabCursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xEof method implementation.
|
||||||
|
*/
|
||||||
|
static int fstreeEof(sqlite3_vtab_cursor *cur){
|
||||||
|
FstreeCsr *pCsr = (FstreeCsr*)cur;
|
||||||
|
return pCsr->pStmt==0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xColumn method implementation.
|
||||||
|
*/
|
||||||
|
static int fstreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
|
||||||
|
FstreeCsr *pCsr = (FstreeCsr*)cur;
|
||||||
|
if( i==0 ){ /* path */
|
||||||
|
sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, 0));
|
||||||
|
}else{
|
||||||
|
struct stat sBuf;
|
||||||
|
fstat(pCsr->fd, &sBuf);
|
||||||
|
|
||||||
|
if( S_ISREG(sBuf.st_mode) ){
|
||||||
|
if( i==1 ){
|
||||||
|
sqlite3_result_int64(ctx, sBuf.st_size);
|
||||||
|
}else{
|
||||||
|
int nRead;
|
||||||
|
char *aBuf = sqlite3_malloc(sBuf.st_mode+1);
|
||||||
|
if( !aBuf ) return SQLITE_NOMEM;
|
||||||
|
nRead = read(pCsr->fd, aBuf, sBuf.st_mode);
|
||||||
|
if( nRead!=sBuf.st_mode ){
|
||||||
|
return SQLITE_IOERR;
|
||||||
|
}
|
||||||
|
sqlite3_result_blob(ctx, aBuf, nRead, SQLITE_TRANSIENT);
|
||||||
|
sqlite3_free(aBuf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** xRowid method implementation.
|
||||||
|
*/
|
||||||
|
static int fstreeRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
|
||||||
|
*pRowid = 0;
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
** End of fstree implementation.
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** This function is the implementation of both the xConnect and xCreate
|
** This function is the implementation of both the xConnect and xCreate
|
||||||
** methods of the fs virtual table.
|
** methods of the fs virtual table.
|
||||||
@@ -109,7 +598,7 @@ static int fsConnect(
|
|||||||
memcpy(pVtab->zTbl, zTbl, strlen(zTbl));
|
memcpy(pVtab->zTbl, zTbl, strlen(zTbl));
|
||||||
memcpy(pVtab->zDb, zDb, strlen(zDb));
|
memcpy(pVtab->zDb, zDb, strlen(zDb));
|
||||||
*ppVtab = &pVtab->base;
|
*ppVtab = &pVtab->base;
|
||||||
sqlite3_declare_vtab(db, "CREATE TABLE xyz(path TEXT, data TEXT)");
|
sqlite3_declare_vtab(db, "CREATE TABLE x(path TEXT, data TEXT)");
|
||||||
|
|
||||||
return SQLITE_OK;
|
return SQLITE_OK;
|
||||||
}
|
}
|
||||||
@@ -188,15 +677,15 @@ static int fsFilter(
|
|||||||
static int fsColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
|
static int fsColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
|
||||||
fs_cursor *pCur = (fs_cursor*)cur;
|
fs_cursor *pCur = (fs_cursor*)cur;
|
||||||
|
|
||||||
assert( i==0 || i==1 );
|
assert( i==0 || i==1 || i==2 );
|
||||||
if( i==0 ){
|
if( i==0 ){
|
||||||
sqlite3_result_value(ctx, sqlite3_column_value(pCur->pStmt, 0));
|
sqlite3_result_value(ctx, sqlite3_column_value(pCur->pStmt, 0));
|
||||||
}else{
|
}else{
|
||||||
const char *zFile = (const char *)sqlite3_column_text(pCur->pStmt, 1);
|
const char *zFile = (const char *)sqlite3_column_text(pCur->pStmt, 1);
|
||||||
struct stat sbuf;
|
struct stat sbuf;
|
||||||
int fd;
|
int fd;
|
||||||
int n;
|
|
||||||
|
|
||||||
|
int n;
|
||||||
fd = open(zFile, O_RDONLY);
|
fd = open(zFile, O_RDONLY);
|
||||||
if( fd<0 ) return SQLITE_IOERR;
|
if( fd<0 ) return SQLITE_IOERR;
|
||||||
fstat(fd, &sbuf);
|
fstat(fd, &sbuf);
|
||||||
@@ -284,6 +773,52 @@ static sqlite3_module fsModule = {
|
|||||||
0, /* xRename */
|
0, /* xRename */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static sqlite3_module fsdirModule = {
|
||||||
|
0, /* iVersion */
|
||||||
|
fsdirConnect, /* xCreate */
|
||||||
|
fsdirConnect, /* xConnect */
|
||||||
|
fsdirBestIndex, /* xBestIndex */
|
||||||
|
fsdirDisconnect, /* xDisconnect */
|
||||||
|
fsdirDisconnect, /* xDestroy */
|
||||||
|
fsdirOpen, /* xOpen - open a cursor */
|
||||||
|
fsdirClose, /* xClose - close a cursor */
|
||||||
|
fsdirFilter, /* xFilter - configure scan constraints */
|
||||||
|
fsdirNext, /* xNext - advance a cursor */
|
||||||
|
fsdirEof, /* xEof - check for end of scan */
|
||||||
|
fsdirColumn, /* xColumn - read data */
|
||||||
|
fsdirRowid, /* xRowid - read data */
|
||||||
|
0, /* xUpdate */
|
||||||
|
0, /* xBegin */
|
||||||
|
0, /* xSync */
|
||||||
|
0, /* xCommit */
|
||||||
|
0, /* xRollback */
|
||||||
|
0, /* xFindMethod */
|
||||||
|
0, /* xRename */
|
||||||
|
};
|
||||||
|
|
||||||
|
static sqlite3_module fstreeModule = {
|
||||||
|
0, /* iVersion */
|
||||||
|
fstreeConnect, /* xCreate */
|
||||||
|
fstreeConnect, /* xConnect */
|
||||||
|
fstreeBestIndex, /* xBestIndex */
|
||||||
|
fstreeDisconnect, /* xDisconnect */
|
||||||
|
fstreeDisconnect, /* xDestroy */
|
||||||
|
fstreeOpen, /* xOpen - open a cursor */
|
||||||
|
fstreeClose, /* xClose - close a cursor */
|
||||||
|
fstreeFilter, /* xFilter - configure scan constraints */
|
||||||
|
fstreeNext, /* xNext - advance a cursor */
|
||||||
|
fstreeEof, /* xEof - check for end of scan */
|
||||||
|
fstreeColumn, /* xColumn - read data */
|
||||||
|
fstreeRowid, /* xRowid - read data */
|
||||||
|
0, /* xUpdate */
|
||||||
|
0, /* xBegin */
|
||||||
|
0, /* xSync */
|
||||||
|
0, /* xCommit */
|
||||||
|
0, /* xRollback */
|
||||||
|
0, /* xFindMethod */
|
||||||
|
0, /* xRename */
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Decode a pointer to an sqlite3 object.
|
** Decode a pointer to an sqlite3 object.
|
||||||
*/
|
*/
|
||||||
@@ -306,6 +841,8 @@ static int register_fs_module(
|
|||||||
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
|
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
|
||||||
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
||||||
sqlite3_create_module(db, "fs", &fsModule, (void *)interp);
|
sqlite3_create_module(db, "fs", &fsModule, (void *)interp);
|
||||||
|
sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
|
||||||
|
sqlite3_create_module(db, "fstree", &fstreeModule, 0);
|
||||||
#endif
|
#endif
|
||||||
return TCL_OK;
|
return TCL_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -587,7 +587,7 @@ do_execsql_test 10.1 {
|
|||||||
INSERT INTO idx VALUES (3, 't3.txt');
|
INSERT INTO idx VALUES (3, 't3.txt');
|
||||||
|
|
||||||
CREATE VIRTUAL TABLE vt USING fs(idx);
|
CREATE VIRTUAL TABLE vt USING fs(idx);
|
||||||
SELECT * FROM vt;
|
SELECT path, data FROM vt;
|
||||||
} {
|
} {
|
||||||
1 {a b c d e f g h i j k l m n o p q r s t u v w x y z}
|
1 {a b c d e f g h i j k l m n o p q r s t u v w x y z}
|
||||||
2 {a b c d e f g h i j k l m a b c d e f g h i j k l m}
|
2 {a b c d e f g h i j k l m a b c d e f g h i j k l m}
|
||||||
@@ -595,7 +595,7 @@ do_execsql_test 10.1 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
do_execsql_test 10.2 {
|
do_execsql_test 10.2 {
|
||||||
SELECT * FROM vt WHERE rowid = 2;
|
SELECT path, data FROM vt WHERE rowid = 2;
|
||||||
} {
|
} {
|
||||||
2 {a b c d e f g h i j k l m a b c d e f g h i j k l m}
|
2 {a b c d e f g h i j k l m a b c d e f g h i j k l m}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,6 @@ foreach ::tclvar_set_omit {0 1} {
|
|||||||
2 {value LIKE '%ac%'} {x1 aback x7 backbone x8 backarrow} 300
|
2 {value LIKE '%ac%'} {x1 aback x7 backbone x8 backarrow} 300
|
||||||
3 {value REGEXP '^......$'} {x5 babble x6 baboon x9 castle} 30000
|
3 {value REGEXP '^......$'} {x5 babble x6 baboon x9 castle} 30000
|
||||||
} {
|
} {
|
||||||
if {$tn==3} breakpoint
|
|
||||||
db cache flush
|
db cache flush
|
||||||
set ::gfunc 0
|
set ::gfunc 0
|
||||||
if {$::tclvar_set_omit} {set cnt 0}
|
if {$::tclvar_set_omit} {set cnt 0}
|
||||||
@@ -107,4 +106,83 @@ foreach ::tclvar_set_omit {0 1} {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
if {$::tcl_platform(platform)=="unix"} {
|
||||||
|
reset_db
|
||||||
|
register_fs_module db
|
||||||
|
do_execsql_test 3.0 {
|
||||||
|
SELECT name FROM fsdir WHERE dir = '.' AND name = 'test.db';
|
||||||
|
SELECT name FROM fsdir WHERE dir = '.' AND name = '.'
|
||||||
|
} {test.db .}
|
||||||
|
|
||||||
|
# Read the first 5 entries from the root directory.
|
||||||
|
#
|
||||||
|
set res [list]
|
||||||
|
foreach p [lrange [exec ls -U /] 0 4] {
|
||||||
|
lappend res "/$p"
|
||||||
|
}
|
||||||
|
do_execsql_test 3.1 {
|
||||||
|
SELECT path FROM fstree LIMIT 5;
|
||||||
|
} $res
|
||||||
|
|
||||||
|
# Read all entries in the current directory.
|
||||||
|
#
|
||||||
|
proc contents {pattern} {
|
||||||
|
set res [list]
|
||||||
|
foreach f [glob -nocomplain $pattern] {
|
||||||
|
lappend res $f
|
||||||
|
if {[file isdir $f]} {
|
||||||
|
set res [concat $res [contents "$f/*"]]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
set res
|
||||||
|
}
|
||||||
|
set pwd "[pwd]/*"
|
||||||
|
set res [contents $pwd]
|
||||||
|
do_execsql_test 3.2 {
|
||||||
|
SELECT path FROM fstree WHERE path GLOB $pwd ORDER BY 1
|
||||||
|
} [lsort $res]
|
||||||
|
|
||||||
|
# Add some sub-directories and files to the current directory.
|
||||||
|
#
|
||||||
|
do_test 3.3 {
|
||||||
|
catch { file delete -force subdir }
|
||||||
|
foreach {path sz} {
|
||||||
|
subdir/x1.txt 143
|
||||||
|
subdir/x2.txt 153
|
||||||
|
} {
|
||||||
|
set dir [file dirname $path]
|
||||||
|
catch { file mkdir $dir }
|
||||||
|
set fd [open $path w]
|
||||||
|
puts -nonewline $fd [string repeat 1 $sz]
|
||||||
|
close $fd
|
||||||
|
}
|
||||||
|
} {}
|
||||||
|
|
||||||
|
set pwd [pwd]
|
||||||
|
do_execsql_test 3.5 {
|
||||||
|
SELECT path, size FROM fstree WHERE path GLOB $pwd || '/subdir/*' ORDER BY 1
|
||||||
|
} [list \
|
||||||
|
"$pwd/subdir/x1.txt" 143 \
|
||||||
|
"$pwd/subdir/x2.txt" 153 \
|
||||||
|
]
|
||||||
|
do_execsql_test 3.6 {
|
||||||
|
SELECT path, size FROM fstree WHERE path LIKE $pwd || '/subdir/%' ORDER BY 1
|
||||||
|
} [list \
|
||||||
|
"$pwd/subdir/x1.txt" 143 \
|
||||||
|
"$pwd/subdir/x2.txt" 153 \
|
||||||
|
]
|
||||||
|
do_execsql_test 3.7 {
|
||||||
|
SELECT sum(size) FROM fstree WHERE path LIKE $pwd || '/subdir/%'
|
||||||
|
} 296
|
||||||
|
do_execsql_test 3.8 {
|
||||||
|
SELECT size FROM fstree WHERE path = $pwd || '/subdir/x1.txt'
|
||||||
|
} 143
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
finish_test
|
finish_test
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user