Skip to content

Commit

Permalink
partial gufi_vt fix
Browse files Browse the repository at this point in the history
gufi_sqlite3 now takes in 1 required positional argument indicating where gufi_vt is
addqueryfuncs.h and histogram.h use SQLITE_EXTENSION_INIT3 when compiling for gufi_vt
  • Loading branch information
calccrypto committed Feb 4, 2025
1 parent 8b22ec0 commit 8cc76f0
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 52 deletions.
2 changes: 2 additions & 0 deletions contrib/longitudinal_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ def create_longitudinal_snapshot(args):
cmd = [
'@CMAKE_BINARY_DIR@/src/gufi_sqlite3',

'@CMAKE_BINARY_DIR@/src/gufi_vt',

args.outdb,

'ATTACH \'file:{0}?mode=ro\' AS {1};'.format(args.flatdb, FLATDB),
Expand Down
5 changes: 5 additions & 0 deletions include/addqueryfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ OF SUCH DAMAGE.
#ifndef ADDQUERYFUNCS_H
#define ADDQUERYFUNCS_H

#ifdef SQLITE_CORE
#include <sqlite3.h>
#else
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT3
#endif

#include "bf.h"
#include "histogram.h"
Expand Down
5 changes: 5 additions & 0 deletions include/histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ OF SUCH DAMAGE.
#include <stddef.h>
#include <time.h>

#ifdef SQLITE_CORE
#include <sqlite3.h>
#else
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT3
#endif

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ set(GUFI_SOURCES
)
add_library(GUFI STATIC ${GUFI_SOURCES})
add_dependencies(GUFI install_dependencies)
target_compile_definitions(GUFI PUBLIC SQLITE_CORE)
set_source_files_properties(bf.c PROPERTIES COMPILE_FLAGS -DGUFI_VERSION="${VERSION_STRING}")

#If the GPFS library exists, build the gpfs scan tool
Expand Down Expand Up @@ -207,6 +208,7 @@ add_library(gufi_query_lib OBJECT
)

add_dependencies(gufi_query_lib GUFI)
target_compile_definitions(gufi_query_lib PUBLIC SQLITE_CORE)

build_and_install_one(${BIN} TRUE gufi_query
gufi_query/main.c
Expand Down
20 changes: 13 additions & 7 deletions src/gufi_sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ OF SUCH DAMAGE.
#include "dbutils.h"
#include "print.h"

/* don't ask */
#define SQLITE_CORE
#include "../src/gufi_vt.c"

static void sub_help(void) {
printf("gufi_vt location of gufi_vt runtime loadable extension\n");
printf("db db file path\n");
printf("SQL SQL statements to run\n");
printf("\n");
Expand All @@ -84,7 +81,9 @@ static void sub_help(void) {

int main(int argc, char *argv[]) {
struct input in;
process_args_and_maybe_exit("hvd:", 0, "[db [SQL]...]", &in);
process_args_and_maybe_exit("hvd:", 1, "gufi_vt [db [SQL]...]", &in);

const char *gufi_vt = argv[idx++];

const int args_left = argc - idx;
const char *dbname = (args_left == 0)?SQLITE_MEMORY:argv[idx++];
Expand All @@ -97,8 +96,16 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

char *err = NULL;

/* this calls addqueryfuncs */
sqlite3_gufivt_init(db, NULL, NULL);
if (sqlite3_load_extension(db, gufi_vt, "sqlite3_gufivt_init", &err) != SQLITE_OK) {
fprintf(stderr, "Error: Could not initialize virtual tables: %s\n", err);
sqlite3_free(err);
input_fini(&in);
closedb(db);
return EXIT_FAILURE;
}

/* no buffering */
struct OutputBuffer ob;
Expand All @@ -114,7 +121,6 @@ int main(int argc, char *argv[]) {
};

/* if using in-memory db or no SQL statements following db path, read from stdin */
char *err = NULL;
if (args_left < 2) {
char *line = NULL;
size_t len = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/gufi_vt.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ OF SUCH DAMAGE.



#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1

#include "addqueryfuncs.h"
#include "bf.h"
#include "dbutils.h"
Expand Down
19 changes: 10 additions & 9 deletions test/regression/gufi_sqlite3.expected
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# gufi_sqlite3 help
$ gufi_sqlite3 -h
usage: gufi_sqlite3 [options] [db [SQL]...]
usage: gufi_sqlite3 [options] gufi_vt [db [SQL]...]
options:
-h help
-v version
-d <delim> delimiter (one char) [use 'x' for 0x1E]

gufi_vt location of gufi_vt runtime loadable extension
db db file path
SQL SQL statements to run

Expand All @@ -14,37 +15,37 @@ Dot commands are not supported


# new db file + argv
$ gufi_sqlite3 "test.db" "CREATE TABLE new_table(i INT);" "INSERT INTO new_table VALUES (0), (1), (2);"
$ gufi_sqlite3 "gufi_vt" "test.db" "CREATE TABLE new_table(i INT);" "INSERT INTO new_table VALUES (0), (1), (2);"

# existing db file + argv
$ gufi_sqlite3 "test.db" "SELECT * FROM new_table ORDER BY i DESC;"
$ gufi_sqlite3 "gufi_vt" "test.db" "SELECT * FROM new_table ORDER BY i DESC;"
2
1
0

$ rm -f "test.db"

# new db file + stdin
$ (echo "CREATE TABLE new_table(i INT);"; echo "INSERT INTO new_table VALUES (3), (4), (5);") | gufi_sqlite3 "test.db"
$ (echo "CREATE TABLE new_table(i INT);"; echo "INSERT INTO new_table VALUES (3), (4), (5);") | gufi_sqlite3 "gufi_vt" "test.db"

# existing db file + stdin
$ echo "SELECT * FROM new_table ORDER BY i DESC;" | gufi_sqlite3 "test.db"
$ echo "SELECT * FROM new_table ORDER BY i DESC;" | gufi_sqlite3 "gufi_vt" "test.db"
5
4
3

# in-memory db
$ (echo "CREATE TABLE new_table(i INT);"; echo "INSERT INTO new_table VALUES (6), (7), (8);"; echo "SELECT * FROM new_table ORDER BY i DESC;") | gufi_sqlite3
$ (echo "CREATE TABLE new_table(i INT);"; echo "INSERT INTO new_table VALUES (6), (7), (8);"; echo "SELECT * FROM new_table ORDER BY i DESC;") | gufi_sqlite3 "gufi_vt"
8
7
6

# directory as db
$ gufi_sqlite3 "search"
$ gufi_sqlite3 "gufi_vt" "search"
Error: Could not open database file "search"

# virtual table
$ echo "SELECT size, path || '/' || name FROM gufi_vt_pentries('prefix') ORDER BY size ASC;" | gufi_sqlite3 -d "|"
$ echo "SELECT size, path || '/' || name FROM gufi_vt_pentries('prefix') ORDER BY size ASC;" | gufi_sqlite3 -d "|" "gufi_vt"
0|prefix/old_file
1|prefix/directory/executable
2|prefix/directory/readonly
Expand All @@ -61,6 +62,6 @@ $ echo "SELECT size, path || '/' || name FROM gufi_vt_pentries('prefix') ORDER B
1048576|prefix/1MB

# bad SQL
$ (echo "CREATE TABLE;") | gufi_sqlite3
$ (echo "CREATE TABLE;") | gufi_sqlite3 "gufi_vt"
Error: SQL error: near ";": syntax error

16 changes: 8 additions & 8 deletions test/regression/gufi_sqlite3.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -86,40 +86,40 @@ echo "# ${GUFI_SQLITE3} help" | replace
run_no_sort "${GUFI_SQLITE3} -h"

echo "# new db file + argv"
run_no_sort "${GUFI_SQLITE3} \"${DBNAME}\"" \
run_no_sort "${GUFI_SQLITE3} \"${GUFI_VT}\" \"${DBNAME}\"" \
"\"CREATE TABLE new_table(i INT);\"" \
"\"INSERT INTO new_table VALUES (0), (1), (2);\""

echo "# existing db file + argv"
run_no_sort "${GUFI_SQLITE3} \"${DBNAME}\"" \
run_no_sort "${GUFI_SQLITE3} \"${GUFI_VT}\" \"${DBNAME}\"" \
"\"SELECT * FROM new_table ORDER BY i DESC;\""

run_no_sort "rm -f \"${DBNAME}\""

echo "# new db file + stdin"
run_no_sort "(echo \"CREATE TABLE new_table(i INT);\";" \
"echo \"INSERT INTO new_table VALUES (3), (4), (5);\") |" \
"${GUFI_SQLITE3} \"${DBNAME}\""
"${GUFI_SQLITE3} \"${GUFI_VT}\" \"${DBNAME}\""

echo "# existing db file + stdin"
run_no_sort "echo \"SELECT * FROM new_table ORDER BY i DESC;\" |" \
"${GUFI_SQLITE3} \"${DBNAME}\""
"${GUFI_SQLITE3} \"${GUFI_VT}\" \"${DBNAME}\""

echo "# in-memory db"
run_no_sort "(echo \"CREATE TABLE new_table(i INT);\";" \
"echo \"INSERT INTO new_table VALUES (6), (7), (8);\";" \
"echo \"SELECT * FROM new_table ORDER BY i DESC;\") |" \
"${GUFI_SQLITE3}"
"${GUFI_SQLITE3} \"${GUFI_VT}\""

echo "# directory as db"
run_no_sort "${GUFI_SQLITE3} \"${SEARCH}\""
run_no_sort "${GUFI_SQLITE3} \"${GUFI_VT}\" \"${SEARCH}\""

echo "# virtual table"
PATH="@CMAKE_BINARY_DIR@/src:${PATH}" run_no_sort "echo \"SELECT size, path || '/' || name FROM gufi_vt_pentries('${INDEXROOT}') ORDER BY size ASC;\" | ${GUFI_SQLITE3} -d \"|\""
PATH="@CMAKE_BINARY_DIR@/src:${PATH}" run_no_sort "echo \"SELECT size, path || '/' || name FROM gufi_vt_pentries('${INDEXROOT}') ORDER BY size ASC;\" | ${GUFI_SQLITE3} -d \"|\" \"${GUFI_VT}\""

echo "# bad SQL"
run_no_sort "(echo \"CREATE TABLE;\") |" \
"${GUFI_SQLITE3}"
"${GUFI_SQLITE3} \"${GUFI_VT}\""
) |& tee "${OUTPUT}"

@DIFF@ @CMAKE_CURRENT_BINARY_DIR@/gufi_sqlite3.expected "${OUTPUT}"
Expand Down
Loading

0 comments on commit 8cc76f0

Please sign in to comment.