From 1cfc2efa638bd6e61c196cf6df0bdd1986a79835 Mon Sep 17 00:00:00 2001 From: Matthias Ruester Date: Thu, 1 Dec 2011 16:40:33 +0100 Subject: [PATCH] bee-dep: fix creation of the index.db file the index.db file was created before init_cache was called so if there is an error in init_cache the index.db was left over --- src/bee-dep.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/bee-dep.c b/src/bee-dep.c index cff2836..c94d0fd 100644 --- a/src/bee-dep.c +++ b/src/bee-dep.c @@ -140,12 +140,28 @@ void get_bee_variables(char **bee_cachedir, char **bee_metadir) } } +static FILE *open_and_lock(char *filename, char *mode) +{ + FILE *f; + + if ((f = fopen(filename, mode)) == NULL) { + perror("bee-dep: fopen"); + exit(EXIT_FAILURE); + } + + if (flock(fileno(f), LOCK_EX) == -1) { + perror("bee-dep: flock"); + exit(EXIT_FAILURE); + } + + return f; +} + int main(int argc, char *argv[]) { int c, help, rebuild, update, remove, print, options; char found; char cachefile[PATH_MAX + 1], path[PATH_MAX + 1], tmp[PATH_MAX + 1]; - char mode[] = "r"; char *bee_metadir, *bee_cachedir, *dir, *pkgname; struct hash *graph; struct stat st; @@ -235,26 +251,13 @@ int main(int argc, char *argv[]) found = (stat(cachefile, &st) != -1 && S_ISREG(st.st_mode)); - if (found && !rebuild) - mode[0] = 'r'; - else - mode[0] = 'w'; - - if ((cache = fopen(cachefile, mode)) == NULL) { - perror("bee-dep: fopen"); - exit(EXIT_FAILURE); - } - - if (flock(fileno(cache), LOCK_EX) == -1) { - perror("bee-dep: flock"); - exit(EXIT_FAILURE); - } - graph = hash_new(); if (rebuild) { if (init_cache(graph, bee_metadir, tmp) == EXIT_FAILURE) - cleanup_and_exit(graph, cache, EXIT_FAILURE); + cleanup_and_exit(graph, NULL, EXIT_FAILURE); + + cache = open_and_lock(cachefile, "w"); if (rename(tmp, cachefile) == -1) { perror("bee-dep: rename"); @@ -265,11 +268,15 @@ int main(int argc, char *argv[]) } if (found) { + cache = open_and_lock(cachefile, "r"); + if (load_cache(graph, cache) == EXIT_FAILURE) cleanup_and_exit(graph, cache, EXIT_FAILURE); } else { if (init_cache(graph, bee_metadir, tmp) == EXIT_FAILURE) - cleanup_and_exit(graph, cache, EXIT_FAILURE); + cleanup_and_exit(graph, NULL, EXIT_FAILURE); + + cache = open_and_lock(cachefile, "w"); if (rename(tmp, cachefile) == -1) { perror("bee-dep: rename");