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) {
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) {
// 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) {
}
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)
char* baseurl;
char* keyfile;
- char* metadata;
+ char metadata[PATH_MAX];
// Mirrorlist
char* mirrorlist_url;
- char* mirrorlist;
+ char mirrorlist[PATH_MAX];
};
struct _PakfireRepo {
}
char database_filename[NAME_MAX] = "";
+ char database_cache_path[PATH_MAX];
+
struct json_object* database = NULL;
// Search for the database name
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
r = 0;
ERROR:
- if (database_cache_path)
- free(database_cache_path);
-
// Free the parsed JSON object
json_object_put(json);
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);
}
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);
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));
}
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) {