#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>
// 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;
-}
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
#define PAKFIRE_PAKFIRE_H
#include <stddef.h>
+#include <stdio.h>
#include <sys/stat.h>
+#include <time.h>
#include <pakfire/types.h>
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
global:
# pakfire
pakfire_init;
+ pakfire_cache_age;
pakfire_cache_stat;
pakfire_count_packages;
pakfire_create;
# #
#############################################################################*/
+#include <ftw.h>
#include <stddef.h>
+#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
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);
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;
+}
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;
}
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);
-}