From ca3e07cc8dcd6c3fbdd4b89e33a9d7b53e83c6ed Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Tue, 11 Nov 2025 18:47:33 +0100 Subject: Add routine for checking database existence --- src/main.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index bef3926..ab10055 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,10 @@ #include +#include #include #include #include #include +#include #include #define DATABASE_PATH (FTAG_ROOT "/ftag.sqlite3") @@ -42,6 +44,26 @@ static void __sqlite3_check(int rc, sqlite3 *db, const char *file, int line, con #define sqlite3_check(RC, DB) __sqlite3_check(RC, DB, __FILE__, __LINE__, __func__) +static void assert_db_exists(void) +{ + struct stat statbuf __attribute__((unused)); + int rc = stat(DATABASE_PATH, &statbuf); + + if (rc == 0) + return; + + if (errno == ENOENT) { + fprintf(stderr, + "ftag: database not found at \"%s\", " + "have you run \"ftag init\"?\n", + DATABASE_PATH); + } + else { + perror(DATABASE_PATH); + } + exit(EXIT_FAILURE); +} + static void ftag_init(int, char **) { char cmd[1024]; @@ -153,6 +175,7 @@ static void ftag_tag_list(int argc, char **argv) static void ftag_tag(int argc, char **argv) { + assert_db_exists(); const struct ftag_command tag_commands[] = { {.name = "add", .func = ftag_tag_add}, {.name = "list", .func = ftag_tag_list} @@ -184,6 +207,5 @@ int main(int argc, char *argv[]) } parse_args(argc-1, argv+1, toplevel_commands, toplevel_command_count); - return EXIT_SUCCESS; } -- cgit v1.2.3