From: Michael Tremer Date: Fri, 19 Aug 2022 11:19:59 +0000 (+0000) Subject: pakfire: Refactor pakfire_cache_path X-Git-Tag: 0.9.28~433 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df1409effa2fd303db17ca7f7b460ce6f432ba56;p=pakfire.git pakfire: Refactor pakfire_cache_path Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index fead677bf..b24d77327 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -913,8 +913,8 @@ static int pakfire_build_setup_ccache(struct pakfire_build* build) { } // Compose path - r = pakfire_make_cache_path(build->pakfire, path, "%s", "ccache"); - if (r < 0) { + r = pakfire_cache_path(build->pakfire, path, "%s", "ccache"); + if (r) { ERROR(build->pakfire, "Could not compose ccache path: %m\n"); return 1; } @@ -1124,8 +1124,8 @@ static int pakfire_build_init(struct pakfire_build* build) { } // Compose path for snapshot - r = pakfire_make_cache_path(build->pakfire, path, "%s", "snapshot.tar.zst"); - if (r < 0) { + r = pakfire_cache_path(build->pakfire, path, "%s", "snapshot.tar.zst"); + if (r) { ERROR(build->pakfire, "Could not compose snapshot path: %m\n"); return 1; } diff --git a/src/libpakfire/dist.c b/src/libpakfire/dist.c index 81453a689..dc8120b44 100644 --- a/src/libpakfire/dist.c +++ b/src/libpakfire/dist.c @@ -251,7 +251,11 @@ static int pakfire_dist_add_source(struct pakfire* pakfire, struct pakfire_packa char cache_path[PATH_MAX]; const char* name = pakfire_package_get_name(pkg); - pakfire_make_cache_path(pakfire, cache_path, "sources/%s/%s", name, filename); + + // Compose path + r = pakfire_cache_path(pakfire, cache_path, "sources/%s/%s", name, filename); + if (r) + return r; // Download the file if it does not exist in the cache if (access(cache_path, R_OK) != 0) { diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index a0be568c8..4fca1da15 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -167,10 +167,9 @@ int __pakfire_path(struct pakfire* pakfire, char* path, const size_t length, const char* pakfire_relpath(struct pakfire* pakfire, const char* path); -#define pakfire_make_cache_path(pakfire, path, format, ...) \ - __pakfire_make_cache_path(pakfire, path, sizeof(path), format, __VA_ARGS__) - -int __pakfire_make_cache_path(struct pakfire* pakfire, char* path, size_t length, +#define pakfire_cache_path(pakfire, path, format, ...) \ + __pakfire_cache_path(pakfire, path, sizeof(path), format, __VA_ARGS__) +int __pakfire_cache_path(struct pakfire* pakfire, char* path, size_t length, const char* format, ...) __attribute__((format(printf, 4, 5))); void pakfire_pool_has_changed(struct pakfire* pakfire); diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index 0bef43eb5..33f16fc59 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -594,10 +594,10 @@ static int pakfire_package_make_cache_path(struct pakfire_package* pkg) { const char* hexdigest = pakfire_package_get_hexdigest(pkg, &digest); if (hexdigest && strlen(hexdigest) >= 3) - return pakfire_make_cache_path(pkg->pakfire, pkg->path, + return pakfire_cache_path(pkg->pakfire, pkg->path, "%c%c/%s/%s", hexdigest[0], hexdigest[1], hexdigest + 2, filename); - return pakfire_make_cache_path(pkg->pakfire, pkg->path, "%s", filename); + return pakfire_cache_path(pkg->pakfire, pkg->path, "%s", filename); } PAKFIRE_EXPORT const char* pakfire_package_get_path(struct pakfire_package* pkg) { diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index ee5498dc0..936197bae 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -970,6 +970,25 @@ const char* pakfire_relpath(struct pakfire* pakfire, const char* path) { return pakfire_path_relpath(pakfire->path, path); } +int __pakfire_cache_path(struct pakfire* pakfire, char* path, size_t length, + const char* format, ...) { + char buffer[PATH_MAX]; + va_list args; + int r; + + // Format input into buffer + va_start(args, format); + r = __pakfire_string_vformat(buffer, sizeof(buffer), format, args); + va_end(args); + + // Break on any errors + if (r) + return r; + + // Join paths together + return __pakfire_path_join(path, length, pakfire->cache_path, buffer); +} + gpgme_ctx_t pakfire_get_gpgctx(struct pakfire* pakfire) { return pakfire->gpgctx; } @@ -1389,31 +1408,6 @@ ERROR: return r; } -// Cache - -int __pakfire_make_cache_path(struct pakfire* pakfire, char* path, size_t length, - const char* format, ...) { - va_list args; - - // Write cache path - ssize_t l = snprintf(path, length - 1, "%s/", pakfire->cache_path); - if (l < 0) - return l; - - // We have run out of space - if ((size_t)l >= length) - return -1; - - // Append everything after the format string - path += l; - - va_start(args, format); - vsnprintf(path, length - l - 1, format, args); - va_end(args); - - return 0; -} - // Logging PAKFIRE_EXPORT int pakfire_log_get_priority(struct pakfire* pakfire) { diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 0bea02762..0715dfbf9 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -338,8 +338,10 @@ static int pakfire_repo_read_metadata(struct pakfire_repo* repo, const char* pat // Try loading the database if (*database_filename) { - pakfire_make_cache_path(repo->pakfire, database_cache_path, + r = pakfire_cache_path(repo->pakfire, database_cache_path, "repodata/%s/%s", pakfire_repo_get_name(repo), database_filename); + if (r) + goto ERROR; // Download the database if necessary if (refresh) { @@ -516,15 +518,15 @@ PAKFIRE_EXPORT int pakfire_repo_create(struct pakfire_repo** repo, goto SUCCESS; // Make path to mirrorlist - r = pakfire_make_cache_path(pakfire, rep->appdata->mirrorlist, + r = pakfire_cache_path(pakfire, rep->appdata->mirrorlist, "repodata/%s/mirrorlist", pakfire_repo_get_name(rep)); - if (r < 0) + if (r) goto ERROR; // Make path for metadata - r = pakfire_make_cache_path(pakfire, rep->appdata->metadata, + r = pakfire_cache_path(pakfire, rep->appdata->metadata, "repodata/%s/repomd.json", pakfire_repo_get_name(rep)); - if (r < 0) + if (r) goto ERROR; // Try loading metadata @@ -978,9 +980,9 @@ PAKFIRE_EXPORT int pakfire_repo_clean(struct pakfire_repo* repo, int flags) { } // Destroy all files in the cache directory - r = pakfire_make_cache_path(repo->pakfire, cache_path, + r = pakfire_cache_path(repo->pakfire, cache_path, "repodata/%s", pakfire_repo_get_name(repo)); - if (r < 0) + if (r) return r; return pakfire_rmtree(cache_path, 0);