From 4ea0bf7dea33f2a4b5e03582c2ca9927626cfceb Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 25 Feb 2023 18:22:55 +0100 Subject: [PATCH] Always update last_disk_db_idx after storing queries on disk as sqlite3_changes() sometimes reports to few rows being inserted. This was a rather tedious debugging... Signed-off-by: DL6ER --- src/config/toml_helper.c | 3 +++ src/config/toml_reader.c | 2 +- src/config/toml_writer.c | 2 +- src/database/query-table.c | 19 ++++++++++++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/config/toml_helper.c b/src/config/toml_helper.c index 52cefb1f..616b8104 100644 --- a/src/config/toml_helper.c +++ b/src/config/toml_helper.c @@ -38,11 +38,14 @@ FILE * __attribute((malloc)) __attribute((nonnull(1))) openFTLtoml(const char *m // Lock file, may block if the file is currently opened if(flock(fileno(fp), LOCK_EX) != 0) { + const int _e = errno; log_err("Cannot open FTL's config file in exclusive mode: %s", strerror(errno)); fclose(fp); + errno = _e; return NULL; } + errno = 0; return fp; } diff --git a/src/config/toml_reader.c b/src/config/toml_reader.c index 1d629773..b6aadfea 100644 --- a/src/config/toml_reader.c +++ b/src/config/toml_reader.c @@ -100,7 +100,7 @@ static toml_table_t *parseTOML(void) if((fp = openFTLtoml("r")) == NULL) { log_warn("No config file available (%s), using defaults", - strerror(errno)); + strerror(errno)); return NULL; } diff --git a/src/config/toml_writer.c b/src/config/toml_writer.c index 6e16e832..b483f13f 100644 --- a/src/config/toml_writer.c +++ b/src/config/toml_writer.c @@ -29,7 +29,7 @@ bool writeFTLtoml(const bool verbose) FILE *fp; if((fp = openFTLtoml("w")) == NULL) { - log_warn("Cannot write to FTL config file, content not updated"); + log_warn("Cannot write to FTL config file (%s), content not updated", strerror(errno)); // Restart watching for changes in the config file watch_config(true); return false; diff --git a/src/database/query-table.c b/src/database/query-table.c index 92f7b643..dbb44ad7 100644 --- a/src/database/query-table.c +++ b/src/database/query-table.c @@ -455,6 +455,23 @@ bool export_queries_to_disk(bool final) sqlite3_reset(stmt); sqlite3_finalize(stmt); + + // Update last_disk_db_idx + // Prepare SQLite3 statement + rc = sqlite3_prepare_v2(memdb, "SELECT MAX(id) FROM disk.query_storage;", -1, &stmt, NULL); + + // Perform step + if((rc = sqlite3_step(stmt)) == SQLITE_ROW) + last_disk_db_idx = sqlite3_column_int64(stmt, 0); + else + log_err("Failed to get MAX(id) from query_storage: %s", + sqlite3_errstr(rc)); + + // Finalize statement + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + + // Export linking tables and current AUTOINCREMENT values to the disk database const char *subtable_names[] = { "domain_by_id", @@ -1029,7 +1046,7 @@ void DB_read_queries(void) if((rc = sqlite3_step(stmt)) == SQLITE_ROW) last_disk_db_idx = sqlite3_column_int64(stmt, 0); else - log_err("DB_read_queries(): Failed to get MAX(id) from queries: %s", + log_err("DB_read_queries(): Failed to get MAX(id) from disk.query_storage: %s", sqlite3_errstr(rc)); // Finalize statement