diff options
| author | Tristan Riehs <tristan.riehs@inria.fr> | 2025-11-11 18:47:33 +0100 |
|---|---|---|
| committer | Tristan Riehs <tristan.riehs@inria.fr> | 2025-11-11 18:47:33 +0100 |
| commit | ca3e07cc8dcd6c3fbdd4b89e33a9d7b53e83c6ed (patch) | |
| tree | 8c8362d26716c00c8bfa6299253b9165fe6408cd | |
| parent | dbaf82a8838e76886bd17419404c6b981cd1bb03 (diff) | |
Add routine for checking database existence
| -rw-r--r-- | src/main.c | 24 |
1 files changed, 23 insertions, 1 deletions
@@ -1,8 +1,10 @@ #include <assert.h> +#include <errno.h> #include <sqlite3.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> #include <unistd.h> #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; } |
