From: Michael Tremer Date: Fri, 12 Feb 2021 16:43:08 +0000 (+0000) Subject: repo: Add function to scan repos for archives X-Git-Tag: 0.9.28~1285^2~753 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=be11aa6e80324e2c22dc2f40f0b8e197ce55bbdd;p=pakfire.git repo: Add function to scan repos for archives Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/repo.h b/src/libpakfire/include/pakfire/repo.h index 58ede26ad..73944ef94 100644 --- a/src/libpakfire/include/pakfire/repo.h +++ b/src/libpakfire/include/pakfire/repo.h @@ -80,6 +80,10 @@ FILE* pakfire_repo_cache_open(PakfireRepo repo, const char* path, const char* mo int pakfire_repo_cache_access(PakfireRepo repo, const char* path, int mode); time_t pakfire_repo_cache_age(PakfireRepo repo, const char* path); +// Scan + +int pakfire_repo_scan(PakfireRepo repo, int flags); + #ifdef PAKFIRE_PRIVATE #include diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 3952cec29..83700d5a8 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -53,4 +53,10 @@ size_t pakfire_string_to_size(const char* s); char** pakfire_split_string(const char* s, char delim); void pakfire_partition_string(const char* s, const char* delim, char** s1, char** s2); +#ifdef PAKFIRE_PRIVATE + +int pakfire_string_endswith(const char* s, const char* suffix); + +#endif + #endif /* PAKFIRE_UTIL_H */ diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 9450119ed..86a7b6249 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -294,6 +294,7 @@ global: pakfire_repo_read_solv; pakfire_repo_read_solv_fp; pakfire_repo_ref; + pakfire_repo_scan; pakfire_repo_set_baseurl; pakfire_repo_set_description; pakfire_repo_set_enabled; diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 3ee2f9604..1f438db80 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -19,6 +19,8 @@ #############################################################################*/ #include +#include +#include #include #include #include @@ -297,6 +299,18 @@ PAKFIRE_EXPORT int pakfire_repo_set_baseurl(PakfireRepo repo, const char* baseur return 0; } +static char* pakfire_repo_get_path(PakfireRepo repo) { + const char* baseurl = pakfire_repo_get_baseurl(repo); + if (!baseurl) + return NULL; + + // Must be a local repository + if (!pakfire_string_startswith(baseurl, "dir://")) + return NULL; + + return strdup(baseurl + strlen("dir://")); +} + PAKFIRE_EXPORT const char* pakfire_repo_get_keyfile(PakfireRepo repo) { return repo->appdata->keyfile; } @@ -629,3 +643,60 @@ PAKFIRE_EXPORT time_t pakfire_repo_cache_age(PakfireRepo repo, const char* path) return t; } + +static int pakfire_repo_scan_file(PakfireRepo repo, const char* path) { + PakfireArchive archive = pakfire_archive_open(repo->pakfire, path); + if (!archive) + return errno; + + // Import package into the repository + PakfirePackage pkg = pakfire_archive_make_package(archive, repo); + if (!pkg) { + pakfire_archive_unref(archive); + return errno; + } + + pakfire_package_unref(pkg); + pakfire_archive_unref(archive); + + return 0; +} + +PAKFIRE_EXPORT int pakfire_repo_scan(PakfireRepo repo, int flags) { + char* path = pakfire_repo_get_path(repo); + if (!path) + return EINVAL; + + int r = 1; + + char* paths[] = { + path, NULL, + }; + + FTS* tree = fts_open(paths, FTS_NOCHDIR|FTS_NOSTAT, 0); + if (!tree) + return errno; + + FTSENT* node; + + while ((node = fts_read(tree))) { + // Skip anything that isn't a file + if (!(node->fts_info & FTS_F)) + continue; + + // Skip any files that do not end in "pfm" + if (!pakfire_string_endswith(node->fts_name, ".pfm")) + continue; + + // Scan the file + r = pakfire_repo_scan_file(repo, node->fts_path); + if (r) + goto ERROR; + } + +ERROR: + fts_close(tree); + free(path); + + return r; +} diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 8d20ad4b6..38c0a71ba 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -39,6 +39,10 @@ PAKFIRE_EXPORT int pakfire_string_startswith(const char* s, const char* prefix) return !strncmp(s, prefix, strlen(prefix)); } +int pakfire_string_endswith(const char* s, const char* suffix) { + return !strcmp(s + strlen(s) - strlen(suffix), suffix); +} + char* pakfire_format_size(double size) { char string[STRING_SIZE]; const char* units[] = {" ", "k", "M", "G", "T", NULL}; diff --git a/tests/libpakfire/repo.c b/tests/libpakfire/repo.c index c882ec5b3..e9a9a464b 100644 --- a/tests/libpakfire/repo.c +++ b/tests/libpakfire/repo.c @@ -18,8 +18,33 @@ # # #############################################################################*/ +#include +#include + #include "../testsuite.h" +static int test_scan(const struct test* t) { + char baseurl[1024]; + snprintf(baseurl, sizeof(baseurl) - 1, "dir://%s/data", TEST_SRC_PATH); + + PakfireRepo repo = pakfire_repo_create(t->pakfire, "test"); + ASSERT(repo); + + pakfire_repo_set_baseurl(repo, baseurl); + + int r = pakfire_repo_scan(repo, 0); + ASSERT(r == 0); + + // There should be one package in this repository now + ASSERT(pakfire_repo_count(repo) == 1); + + pakfire_repo_unref(repo); + + return 0; +} + int main(int argc, char** argv) { + testsuite_add_test(test_scan); + return testsuite_run(); }