]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Refactor pakfire_cache_path
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Aug 2022 11:19:59 +0000 (11:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Aug 2022 11:19:59 +0000 (11:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/dist.c
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/package.c
src/libpakfire/pakfire.c
src/libpakfire/repo.c

index fead677bfcd33d28cf3589045ba1adf6d0969972..b24d7732768a29672d3dc05d74e63de7e3c62af6 100644 (file)
@@ -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;
        }
index 81453a6895b7ea38682ebbeebb82fb5eeb8ba3fb..dc8120b4405a5e2097875bf060b5b93b9c4a62f4 100644 (file)
@@ -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) {
index a0be568c86bf8968ca2290af7fa73a324fb553bf..4fca1da15138f1f329cdeaa0c6d9738438dbe235 100644 (file)
@@ -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);
index 0bef43eb5d5c48a14ce3846e765fa80e8b3f4037..33f16fc5910db0dcb47a4aec3c9b0655c1fd63b2 100644 (file)
@@ -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) {
index ee5498dc0011972364aa47bab225a2b38e1b546a..936197bae742d99639cce3095e7aa42116307017 100644 (file)
@@ -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) {
index 0bea02762f2735648879c7500d42aa7a30c04383..0715dfbf99b06052da85950cdbce66fafea43a40 100644 (file)
@@ -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);