From 8301098bff618760250f403217766210e8883885 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 19 Jan 2018 13:55:56 +0100 Subject: [PATCH] libpakfire: Move more cache logic into Pakfire Signed-off-by: Michael Tremer --- src/libpakfire/cache.c | 67 ------------------------ src/libpakfire/include/pakfire/cache.h | 3 -- src/libpakfire/include/pakfire/pakfire.h | 5 ++ src/libpakfire/libpakfire.sym | 1 + src/libpakfire/pakfire.c | 54 +++++++++++++++++++ src/libpakfire/repo.c | 26 +++++++-- src/libpakfire/repocache.c | 9 ---- 7 files changed, 82 insertions(+), 83 deletions(-) diff --git a/src/libpakfire/cache.c b/src/libpakfire/cache.c index 9b6f540be..ab2ed3fec 100644 --- a/src/libpakfire/cache.c +++ b/src/libpakfire/cache.c @@ -20,10 +20,8 @@ #include #include -#include #include #include -#include #include #include @@ -73,68 +71,3 @@ PAKFIRE_EXPORT int pakfire_cache_has_file(PakfireCache cache, const char* filena // Just check if stat() was sucessful. return (r == 0); } - -PAKFIRE_EXPORT int pakfire_cache_age(PakfireCache cache, const char* filename) { - struct stat buf; - int r = pakfire_cache_stat(cache, filename, &buf); - - if (r == 0) { - // Get timestamp. - time_t now = time(NULL); - - // Calculate the difference since the file has been created and now. - time_t age = now - buf.st_ctime; - - return (int)age; - } - - return -1; -} - -static int pakfire_cache_mkdir(PakfireCache cache, const char* path, mode_t mode) { - int r = 0; - - if ((strcmp(path, "/") == 0) || (strcmp(path, ".") == 0)) { - return 0; - } - - // If parent does not exists, we try to create it. - char* parent_path = pakfire_dirname(path); - r = access(parent_path, F_OK); - if (r) { - r = pakfire_cache_mkdir(cache, parent_path, mode); - } - pakfire_free(parent_path); - - if (!r) { - // Finally, create the directory we want. - r = mkdir(path, mode); - - if (r) { - switch (errno) { - // If the directory already exists, this is fine. - case EEXIST: - r = 0; - break; - } - } - } - - return r; -} - -PAKFIRE_EXPORT FILE* pakfire_cache_open(PakfireCache cache, const char* filename, const char* flags) { - assert(filename); - - char* cache_filename = pakfire_cache_get_full_path(cache, filename); - - char* cache_dirname = pakfire_dirname(cache_filename); - pakfire_cache_mkdir(cache, cache_dirname, S_IRUSR|S_IWUSR|S_IXUSR); - - FILE* fp = fopen(cache_filename, flags); - - pakfire_free(cache_filename); - pakfire_free(cache_dirname); - - return fp; -} diff --git a/src/libpakfire/include/pakfire/cache.h b/src/libpakfire/include/pakfire/cache.h index 78d4c5902..3385b7f83 100644 --- a/src/libpakfire/include/pakfire/cache.h +++ b/src/libpakfire/include/pakfire/cache.h @@ -32,9 +32,6 @@ const char* pakfire_cache_get_path(PakfireCache cache); char* pakfire_cache_get_full_path(PakfireCache cache, const char* path); int pakfire_cache_has_file(PakfireCache cache, const char* filename); -int pakfire_cache_age(PakfireCache cache, const char* filename); - -FILE* pakfire_cache_open(PakfireCache cache, const char* filename, const char* flags); #ifdef PAKFIRE_PRIVATE diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index 29130e4a2..88769f7d5 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -22,7 +22,9 @@ #define PAKFIRE_PAKFIRE_H #include +#include #include +#include #include @@ -56,7 +58,10 @@ PakfirePackageList pakfire_search(Pakfire pakfire, const char* what, int flags); char* pakfire_get_cache_path(Pakfire pakfire, const char* path); void pakfire_set_cache_path(Pakfire pakfire, const char* path); +int pakfire_cache_destroy(Pakfire pakfire, const char* path); int pakfire_cache_stat(Pakfire pakfire, const char* path, struct stat* buffer); +time_t pakfire_cache_age(Pakfire pakfire, const char* path); +FILE* pakfire_cache_open(Pakfire pakfire, const char* path, const char* flags); #ifdef PAKFIRE_PRIVATE diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 7039c518c..9dbb252da 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -22,6 +22,7 @@ LIBPAKFIRE_0 { global: # pakfire pakfire_init; + pakfire_cache_age; pakfire_cache_stat; pakfire_count_packages; pakfire_create; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 62be168fd..dcdc9006d 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -18,7 +18,9 @@ # # #############################################################################*/ +#include #include +#include #include #include #include @@ -334,6 +336,22 @@ PAKFIRE_EXPORT void pakfire_set_cache_path(Pakfire pakfire, const char* path) { DEBUG("Set cache path to %s\n", pakfire->cache_path); } +static int _unlink(const char* path, const struct stat* stat, int typeflag, struct FTW* ftwbuf) { + DEBUG("Deleting %s...\n", path); + + return remove(path); +} + +PAKFIRE_EXPORT int pakfire_cache_destroy(Pakfire pakfire, const char* path) { + char* cache_path = pakfire_get_cache_path(pakfire, path); + + // Completely delete the tree of files + int r = nftw(cache_path, _unlink, 64, FTW_DEPTH|FTW_PHYS); + pakfire_free(cache_path); + + return r; +} + PAKFIRE_EXPORT int pakfire_cache_stat(Pakfire pakfire, const char* path, struct stat* buffer) { char* cache_path = pakfire_get_cache_path(pakfire, path); @@ -342,3 +360,39 @@ PAKFIRE_EXPORT int pakfire_cache_stat(Pakfire pakfire, const char* path, struct return r; } + +PAKFIRE_EXPORT time_t pakfire_cache_age(Pakfire pakfire, const char* path) { + struct stat buffer; + + int r = pakfire_cache_stat(pakfire, path, &buffer); + if (r == 0) { + // Get current timestamp + time_t now = time(NULL); + + // Calculate the difference since the file has been created and now. + return now - buffer.st_ctime; + } + + return -1; +} + +PAKFIRE_EXPORT FILE* pakfire_cache_open(Pakfire pakfire, const char* path, const char* flags) { + FILE* f = NULL; + char* cache_path = pakfire_get_cache_path(pakfire, path); + + // Ensure that the parent directory exists + char* cache_dirname = pakfire_dirname(cache_path); + + int r = pakfire_mkdir(cache_dirname, S_IRUSR|S_IWUSR|S_IXUSR); + if (r) + goto FAIL; + + // Open the file + f = fopen(cache_path, flags); + +FAIL: + pakfire_free(cache_path); + pakfire_free(cache_dirname); + + return f; +} diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index a386bce03..d1104088d 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -436,11 +436,29 @@ PAKFIRE_EXPORT PakfireRepoCache pakfire_repo_get_cache(PakfireRepo repo) { return repo->cache; } +static char* pakfire_repo_get_cache_prefix(PakfireRepo repo) { + char* prefix = pakfire_calloc(1, STRING_SIZE + 1); + + snprintf(prefix, STRING_SIZE, "repodata/%s", pakfire_repo_get_name(repo)); + + return prefix; +} + +static char* pakfire_repo_make_cache_path(PakfireRepo repo, const char* path) { + char* prefix = pakfire_repo_get_cache_prefix(repo); + + // Add the prefix for the repository first + char* cache_path = pakfire_path_join(prefix, path); + pakfire_free(prefix); + + return cache_path; +} + PAKFIRE_EXPORT int pakfire_repo_clean(PakfireRepo repo) { - PakfireRepoCache cache = pakfire_repo_get_cache(repo); + char* cache_path = pakfire_repo_make_cache_path(repo, NULL); - if (cache) - return pakfire_repocache_destroy(cache); + if (cache_path) + return pakfire_cache_destroy(repo->pakfire, cache_path); - return 0; + return -1; } diff --git a/src/libpakfire/repocache.c b/src/libpakfire/repocache.c index 5795a443d..ca3ce006c 100644 --- a/src/libpakfire/repocache.c +++ b/src/libpakfire/repocache.c @@ -99,12 +99,3 @@ PAKFIRE_EXPORT FILE* pakfire_repocache_open(PakfireRepoCache repo_cache, const c return fp; } - -static int _unlink(const char* path, const struct stat* stat, int typeflag, struct FTW* ftwbuf) { - return remove(path); -} - -PAKFIRE_EXPORT int pakfire_repocache_destroy(PakfireRepoCache repo_cache) { - // Completely delete the tree of files - return nftw(repo_cache->prefix, _unlink, 64, FTW_DEPTH|FTW_PHYS); -} -- 2.39.5