]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Move more cache logic into Pakfire
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Jan 2018 12:55:56 +0000 (13:55 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Jan 2018 12:55:56 +0000 (13:55 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/cache.c
src/libpakfire/include/pakfire/cache.h
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/libpakfire.sym
src/libpakfire/pakfire.c
src/libpakfire/repo.c
src/libpakfire/repocache.c

index 9b6f540bec6175e5747c204aa3728619504cbe16..ab2ed3fecda589af6182a2832e519ed4abcc55fe 100644 (file)
 
 #include <assert.h>
 #include <errno.h>
-#include <libgen.h>
 #include <stdio.h>
 #include <sys/stat.h>
-#include <time.h>
 #include <unistd.h>
 
 #include <pakfire/cache.h>
@@ -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;
-}
index 78d4c59021a41f5efad5ceb45e365166bcd2b45e..3385b7f83b54f8ac07f044d7b577566988898f09 100644 (file)
@@ -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
 
index 29130e4a2f4fce0f6358f9a7306b7269ab30110a..88769f7d5a3ffb26453dbe729ac085b804090477 100644 (file)
@@ -22,7 +22,9 @@
 #define PAKFIRE_PAKFIRE_H
 
 #include <stddef.h>
+#include <stdio.h>
 #include <sys/stat.h>
+#include <time.h>
 
 #include <pakfire/types.h>
 
@@ -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
 
index 7039c518cf838270642de29732b2e345e3773b06..9dbb252dab068540ad69ef22cd1d740f3045e485 100644 (file)
@@ -22,6 +22,7 @@ LIBPAKFIRE_0 {
 global:
        # pakfire
        pakfire_init;
+       pakfire_cache_age;
        pakfire_cache_stat;
        pakfire_count_packages;
        pakfire_create;
index 62be168fd8bf8fd3fd32ad4db92c9aee8fa22da1..dcdc9006dd50cd82970ecde70f444c8da16934de 100644 (file)
@@ -18,7 +18,9 @@
 #                                                                             #
 #############################################################################*/
 
+#include <ftw.h>
 #include <stddef.h>
+#include <stdio.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -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;
+}
index a386bce0311135bcbd2028e56ef63b4ae16bce92..d1104088db006d2016c454111326c85ab8bcf455 100644 (file)
@@ -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;
 }
index 5795a443d26a502b7801c128f7eb5982c7478c5b..ca3ce006cd4a2dd6bfd03fe0ad5f73e16137ac72 100644 (file)
@@ -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);
-}