From 4fc12547392978956709f15b3be95ecd00089f9e Mon Sep 17 00:00:00 2001 From: Siguza Date: Sun, 19 May 2019 18:23:22 +0200 Subject: [PATCH] fixes --- Makefile | 2 +- src/db.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/main.c | 2 +- 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index dd7cdb0..7162022 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION = 1.0.0 +VERSION = 1.0.1 TARGET = imobax SRCDIR = src FLAGS ?= -Wall -O3 -DVERSION=$(VERSION) -DTIMESTAMP="`date +'%d. %B %Y %H:%M:%S'`" -flto -lsqlite3 $(CFLAGS) diff --git a/src/db.c b/src/db.c index 6ea3d5f..08a976d 100644 --- a/src/db.c +++ b/src/db.c @@ -12,7 +12,7 @@ #include #include #include // malloc, free -#include // strdup, memcmp +#include // strcat, strdup, memcmp #include #include // close #include // mmap, munmap @@ -67,6 +67,74 @@ out:; return retval; } +static void db_debug_log(void *arg, int code, const char *msg) +{ + LOG("[DBG] (%u) %s", code, msg); +} + +static char* db_make_uri(const char *path) +{ + if(path[0] != '/') + { + LOG("db_make_uri: not an absolute path"); + return NULL; + } + size_t len = 7 // file:// + + 12 // ?immutable=1 + + 1; // NUL + for(size_t i = 0; path[i] != '\0'; ++i) + { + char c = path[i]; + if + ( + (c >= '0' && c <= '9') || + (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + c == '_' || c == '-' || c == '.' || c == '~' || c == '/' + ) + { + len += 1; + } + else + { + len += 3; // %xx + } + } + char *uri = malloc(len); + if(!uri) + { + ERRNO("malloc(uri)"); + return NULL; + } + uri[0] = '\0'; + strcat(uri, "file://"); + size_t u = 7; + for(size_t i = 0; path[i] != '\0'; ++i) + { + char c = path[i]; + if + ( + (c >= '0' && c <= '9') || + (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + c == '_' || c == '-' || c == '.' || c == '~' || c == '/' + ) + { + uri[u++] = c; + } + else + { + const char hex[] = "0123456789ABCDEF"; + uri[u++] = '%'; + uri[u++] = hex[(c >> 4) & 0xf]; + uri[u++] = hex[(c ) & 0xf]; + } + } + uri[u] = '\0'; + strcat(uri, "?immutable=1"); + return uri; +} + db_ent_t* db_getlist_sqlite3(int srcdir) { // Ideally we'd want to pass a file handle to SQLite, @@ -79,6 +147,7 @@ db_ent_t* db_getlist_sqlite3(int srcdir) // But if the API can't do better, what are we supposed to do? char buf[MAXPATHLEN]; int fd = -1; + char *uri = NULL; sqlite3 *db = NULL; char *err = NULL; db_ent_t *retval = NULL; @@ -94,8 +163,20 @@ db_ent_t* db_getlist_sqlite3(int srcdir) ERRNO("fcntl(" DBFILE_SQLITE ")"); goto out; } + uri = db_make_uri(buf); + if(!uri) + { + goto out; + } + + int r = sqlite3_config(SQLITE_CONFIG_LOG, &db_debug_log, NULL); + if(r != SQLITE_OK) + { + LOG("sqlite3_config(SQLITE_CONFIG_LOG): %s", sqlite3_errstr(r)); + goto out; + } - int r = sqlite3_open_v2(buf, &db, SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX, NULL); // yolo + r = sqlite3_open_v2(uri, &db, SQLITE_OPEN_URI | SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX, NULL); // yolo if(r != SQLITE_OK) { LOG("sqlite3_open: %s", sqlite3_errstr(r)); @@ -114,6 +195,7 @@ db_ent_t* db_getlist_sqlite3(int srcdir) out:; if(err) sqlite3_free(err); if(db) sqlite3_close(db); + if(uri) free(uri); if(fd != -1) close(fd); return retval; } diff --git a/src/main.c b/src/main.c index eb76b38..a58927c 100644 --- a/src/main.c +++ b/src/main.c @@ -185,7 +185,7 @@ int main(int argc, const char **argv) goto out; } } - fromfd = openat(auxfd == -1 ? AT_FDCWD : auxfd, ent->fileID, O_RDONLY); + fromfd = openat(auxfd == -1 ? srcdir : auxfd, ent->fileID, O_RDONLY); if(fromfd == -1) { ERRNO("open(%s)", ent->fileID);