From: Michael Tremer Date: Sat, 13 Mar 2021 18:59:29 +0000 (+0000) Subject: libpakfire: Have pakfire_make_cache_path write to stack X-Git-Tag: 0.9.28~1285^2~526 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6d0f083625fd821db8b7777876872f023be87f25;p=pakfire.git libpakfire: Have pakfire_make_cache_path write to stack Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/dist.c b/src/libpakfire/dist.c index 1f41640ce..9e5e9f6dd 100644 --- a/src/libpakfire/dist.c +++ b/src/libpakfire/dist.c @@ -152,8 +152,8 @@ static int pakfire_dist_add_source(Pakfire pakfire, struct pakfire_packager* pac const char* name = pakfire_package_get_name(pkg); snprintf(archive_path, sizeof(archive_path) - 1, "files/%s", filename); - snprintf(cache_path, sizeof(cache_path) - 1, "%s/sources/%s/%s", - pakfire_make_cache_path(pakfire, ""), name, filename); + pakfire_make_cache_path(pakfire, cache_path, sizeof(cache_path) - 1, + "sources/%s/%s", name, filename); // 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 afff55f79..00a8edeaf 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -78,7 +78,9 @@ int pakfire_read_makefile(PakfireParser* parser, Pakfire pakfire, const char* pa #include -char* pakfire_make_cache_path(Pakfire pakfire, const char* format, ...); +int pakfire_make_cache_path(Pakfire pakfire, char* path, size_t length, + const char* format, ...) + __attribute__((format(printf, 4, 5))); void pakfire_pool_has_changed(Pakfire pakfire); void pakfire_pool_apply_changes(Pakfire pakfire); diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index c6300446b..4de9ff6cc 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -932,10 +932,10 @@ PAKFIRE_EXPORT char* pakfire_package_get_cache_path(PakfirePackage pkg) { if (strlen(checksum) < 3) return NULL; - snprintf(path, sizeof(path) - 1, "%c%c/%s/%s", checksum[0], checksum[1], - checksum + 2, filename); + pakfire_make_cache_path(pkg->pakfire, path, sizeof(path) - 1, + "%c%c/%s/%s", checksum[0], checksum[1], checksum + 2, filename); - return pakfire_make_cache_path(pkg->pakfire, path); + return strdup(path); } PAKFIRE_EXPORT PakfireArchive pakfire_package_get_archive(PakfirePackage pkg) { diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 87260978c..bcaf06966 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -651,16 +651,27 @@ PAKFIRE_EXPORT PakfirePackageList pakfire_search(Pakfire pakfire, const char* wh // Cache -PAKFIRE_EXPORT char* pakfire_make_cache_path(Pakfire pakfire, const char* format, ...) { - char path[PATH_MAX]; +int pakfire_make_cache_path(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, sizeof(path) - 1, format, args); + vsnprintf(path, length - l - 1, format, args); va_end(args); - // Prepend cache path - return pakfire_path_join(pakfire->cache_path, path); + return 0; } static int _unlink(const char* path, const struct stat* stat, int typeflag, struct FTW* ftwbuf) { @@ -668,11 +679,12 @@ static int _unlink(const char* path, const struct stat* stat, int typeflag, stru } PAKFIRE_EXPORT int pakfire_cache_destroy(Pakfire pakfire, const char* path) { - char* cache_path = pakfire_make_cache_path(pakfire, path); + char cache_path[PATH_MAX]; + + pakfire_make_cache_path(pakfire, cache_path, sizeof(cache_path) - 1, "%s", path); // Completely delete the tree of files int r = nftw(cache_path, _unlink, 64, FTW_DEPTH|FTW_PHYS); - free(cache_path); // It is okay if the path doesn't exist if (r < 0 && errno == ENOENT) diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 4120d6ffa..84772fff3 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -63,11 +63,11 @@ struct pakfire_repo_appdata { char* baseurl; char* keyfile; - char* metadata; + char metadata[PATH_MAX]; // Mirrorlist char* mirrorlist_url; - char* mirrorlist; + char mirrorlist[PATH_MAX]; }; struct _PakfireRepo { @@ -143,6 +143,8 @@ static int pakfire_repo_read_metadata(PakfireRepo repo, const char* path, int re } char database_filename[NAME_MAX] = ""; + char database_cache_path[PATH_MAX]; + struct json_object* database = NULL; // Search for the database name @@ -154,11 +156,10 @@ static int pakfire_repo_read_metadata(PakfireRepo repo, const char* path, int re DEBUG(repo->pakfire, "Using package database %s\n", database_filename); } - char* database_cache_path = NULL; // Try loading the database if (*database_filename) { - database_cache_path = pakfire_make_cache_path(repo->pakfire, + pakfire_make_cache_path(repo->pakfire, database_cache_path, sizeof(database_cache_path) -1, "repodata/%s/%s", pakfire_repo_get_name(repo), database_filename); // Download the database if necessary @@ -178,9 +179,6 @@ static int pakfire_repo_read_metadata(PakfireRepo repo, const char* path, int re r = 0; ERROR: - if (database_cache_path) - free(database_cache_path); - // Free the parsed JSON object json_object_put(json); @@ -275,18 +273,12 @@ static void free_repo_appdata(struct pakfire_repo_appdata* appdata) { if (appdata->description) free(appdata->description); - if (appdata->baseurl) - free(appdata->baseurl); - if (appdata->keyfile) free(appdata->keyfile); if (appdata->mirrorlist_url) free(appdata->mirrorlist_url); - if (appdata->mirrorlist) - free(appdata->mirrorlist); - free(appdata); } @@ -321,6 +313,7 @@ void pakfire_repo_free_all(Pakfire pakfire) { PAKFIRE_EXPORT PakfireRepo pakfire_repo_create(Pakfire pakfire, const char* name) { PakfireRepo repo; + int r; // Return existing repositories with the same name repo = pakfire_get_repo(pakfire, name); @@ -366,19 +359,19 @@ PAKFIRE_EXPORT PakfireRepo pakfire_repo_create(Pakfire pakfire, const char* name return repo; // Make path for metadata - repo->appdata->metadata = pakfire_make_cache_path( - repo->pakfire, "repodata/%s/repomd.json", pakfire_repo_get_name(repo)); - if (!repo->appdata->metadata) + r = pakfire_make_cache_path(pakfire, repo->appdata->metadata, sizeof(repo->appdata->metadata) - 1, + "repodata/%s/repomd.json", pakfire_repo_get_name(repo)); + if (r < 0) goto ERROR; // Make path to mirrorlist - repo->appdata->mirrorlist = pakfire_make_cache_path( - repo->pakfire, "repodata/%s/mirrorlist", pakfire_repo_get_name(repo)); - if (!repo->appdata->mirrorlist) + r = pakfire_make_cache_path(pakfire, repo->appdata->mirrorlist, sizeof(repo->appdata->mirrorlist) - 1, + "repodata/%s/mirrorlist", pakfire_repo_get_name(repo)); + if (r < 0) goto ERROR; // Try loading metadata - int r = pakfire_repo_read_metadata(repo, repo->appdata->metadata, 0); + r = pakfire_repo_read_metadata(repo, repo->appdata->metadata, 0); if (r) { ERROR(repo->pakfire, "Could not initialize repository metadata: %s\n", strerror(errno)); @@ -837,12 +830,14 @@ PAKFIRE_EXPORT PakfirePackage pakfire_repo_add_archive(PakfireRepo repo, Pakfire } PAKFIRE_EXPORT int pakfire_repo_clean(PakfireRepo repo) { - char* cache_path = pakfire_make_cache_path(repo->pakfire, pakfire_repo_get_name(repo)); + char cache_path[PATH_MAX]; - if (cache_path) - return pakfire_cache_destroy(repo->pakfire, cache_path); + int r = pakfire_make_cache_path(repo->pakfire, cache_path, sizeof(cache_path) - 1, + "%s", pakfire_repo_get_name(repo)); + if (r < 0) + return r; - return -1; + return pakfire_cache_destroy(repo->pakfire, cache_path); } static int pakfire_repo_scan_file(PakfireRepo repo, const char* path) {