diff options
| author | Tristan Riehs <tristan.riehs@inria.fr> | 2026-01-03 22:57:13 +0100 |
|---|---|---|
| committer | Tristan Riehs <tristan.riehs@inria.fr> | 2026-01-03 22:57:13 +0100 |
| commit | 07d0f65cac8483911da2ac692c106d6061509e9d (patch) | |
| tree | ffc0bd858065a616be5e93d480dedb8556e45acf | |
| parent | 813349475dfb35d640bcbfbe76e57b8db18dbc12 (diff) | |
Change file_exists and use it more
| -rw-r--r-- | src/main.c | 31 |
1 files changed, 10 insertions, 21 deletions
@@ -56,20 +56,24 @@ 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__) +/* Return whether PATH exists in the file system. Exit if any non-"file not + * fount" error occurs. */ static int file_exists(const char *path) { struct stat statbuf __attribute__((unused)); int rc = stat(path, &statbuf); if (rc == 0) return 1; - else + else if ((rc == -1) && (errno == ENOENT)) return 0; + fprintf(stderr, "stat: \"%s\": ", path); + perror(""); + exit(EXIT_FAILURE); } static void assert_db_exists(void) { - errno = 0; - if (file_exists(DATABASE_PATH) && (errno == 0)) + if (file_exists(DATABASE_PATH)) return; if (errno == ENOENT) { @@ -398,30 +402,15 @@ static void ftag_file_get_extension(char *out, int size, const char *file) /* Get the path to FILE in the ftag database. */ static void ftag_file_get_path(char *out, int size, const char *file) { - struct stat st; - int rc; /* look for the encrypted version first */ strbuild_with_size(out, size, "%s/files/%s.gpg", FTAG_ROOT, file); - rc = stat(out, &st); - if (rc == 0) { + if (file_exists(out)) return; - } - else if (rc == -1 && errno != ENOENT) { - fprintf(stderr, "stat: %s:", out); - perror(""); - exit(EXIT_FAILURE); - } /* look for the clear version after */ out[strlen(out) - 4] = '\0'; /* remove ".gpg" extension */ - rc = stat(out, &st); - if (rc == 0) { + if (file_exists(out)) return; - } - else if (rc == -1 && errno != ENOENT) { - fprintf(stderr, "stat: %s:", out); - perror(""); - exit(EXIT_FAILURE); - } + fprintf(stderr, "ftag export: cannot find \"%s\" in the database\n", file); exit(EXIT_FAILURE); |
