#include <json.h>
-// Enable legacy logging
-#define PAKFIRE_LEGACY_LOGGING
-
#include <pakfire/archive.h>
#include <pakfire/compress.h>
#include <pakfire/config.h>
#ifdef ENABLE_DEBUG
if (strcmp(url, buffer) != 0) {
- DEBUG(repo->pakfire, "Repository URL updated:");
- DEBUG(repo->pakfire, " From: %s\n", url);
- DEBUG(repo->pakfire, " To : %s\n", buffer);
+ CTX_DEBUG(repo->ctx, "Repository URL updated:");
+ CTX_DEBUG(repo->ctx, " From: %s\n", url);
+ CTX_DEBUG(repo->ctx, " To : %s\n", buffer);
}
#endif
// Import the key
r = pakfire_key_import_from_string(&repo->key, repo->ctx, data, strlen(data));
- if (r) {
- ERROR(repo->pakfire, "Could not import key for repository '%s': %m\n",
- pakfire_repo_get_name(repo));
+ if (r < 0) {
+ CTX_ERROR(repo->ctx, "Could not import key for repository '%s': %s\n",
+ pakfire_repo_get_name(repo), strerror(-r));
goto ERROR;
}
// If the key could be successfully imported, we will store it in the appdata
r = pakfire_string_set(repo->appdata->key, data);
- if (r) {
- ERROR(repo->pakfire, "Could not copy the key to appdata: %m\n");
+ if (r < 0) {
+ CTX_ERROR(repo->ctx, "Could not copy the key to appdata: %s\n", strerror(-r));
goto ERROR;
}
if (!*name)
return 0;
- DEBUG(pakfire, "Creating repository %s\n", name);
+ // Fetch context
+ struct pakfire_ctx* ctx = pakfire_ctx(pakfire);
+
+ CTX_DEBUG(ctx, "Creating repository %s\n", name);
// Create a new repository
r = pakfire_repo_create(&repo, pakfire, name);
if (r) {
- ERROR(pakfire, "Could not create repository '%s': %m\n", name);
+ CTX_ERROR(ctx, "Could not create repository '%s': %m\n", name);
goto ERROR;
}
ERROR:
if (repo)
pakfire_repo_unref(repo);
+ if (ctx)
+ pakfire_ctx_unref(ctx);
return r;
}
return NULL;
int r = pakfire_mirrorlist_create(&repo->mirrorlist, repo->ctx);
- if (r) {
- ERROR(repo->pakfire, "Could not create mirrorlist: %m\n");
+ if (r < 0) {
+ CTX_ERROR(repo->ctx, "Could not create mirrorlist: %s\n", strerror(-r));
return NULL;
}
// Read the mirrorlist
r = pakfire_mirrorlist_read(repo->mirrorlist, repo->appdata->mirrorlist);
- if (r) {
- ERROR(repo->pakfire, "Could not read mirrorlist %s: %m\n",
- repo->appdata->mirrorlist);
+ if (r < 0) {
+ CTX_ERROR(repo->ctx, "Could not read mirrorlist %s: %s\n",
+ repo->appdata->mirrorlist, strerror(-r));
// Destroy what we have created
pakfire_mirrorlist_unref(repo->mirrorlist);
// Do nothing if the file already exists
if (pakfire_path_exists(path)) {
- DEBUG(repo->pakfire, "Database %s already present. Skipping download\n", filename);
+ CTX_DEBUG(repo->ctx, "Database %s already present. Skipping download\n", filename);
return 0;
}
FILE* f = NULL;
int r;
- DEBUG(repo->pakfire, "Read package database from %s...\n", path);
+ CTX_DEBUG(repo->ctx, "Read package database from %s...\n", path);
// Open the database file
f = fopen(path, "r");
if (!f) {
- ERROR(repo->pakfire, "Could not open package database at %s: %m\n", path);
+ CTX_ERROR(repo->ctx, "Could not open package database at %s: %m\n", path);
r = 1;
goto ERROR;
}
char database_path[PATH_MAX];
int r;
- DEBUG(repo->pakfire, "Reading repository metadata from %s...\n", path);
+ CTX_DEBUG(repo->ctx, "Reading repository metadata from %s...\n", path);
struct json_object* json = pakfire_json_parse_from_file(repo->ctx, path);
if (!json) {
case ENOENT:
// If the repository is local, we will just scan it
if (pakfire_repo_is_local(repo)) {
- DEBUG(repo->pakfire, "No metadata available on local repository."
+ CTX_DEBUG(repo->ctx, "No metadata available on local repository."
" Falling back to scan...\n");
return pakfire_repo_scan(repo, 0);
break;
}
- ERROR(repo->pakfire, "Could not parse metadata from %s: %m\n", path);
+ CTX_ERROR(repo->ctx, "Could not parse metadata from %s: %m\n", path);
return 1;
}
// Search for the database name
int found = json_object_object_get_ex(json, "database", &database);
if (!found) {
- ERROR(repo->pakfire, "Could not read database name from metadata\n");
+ CTX_ERROR(repo->ctx, "Could not read database name from metadata\n");
r = 1;
goto ERROR;
}
time_t age = pakfire_path_age(repo->appdata->mirrorlist);
if (age > 0 && age < REFRESH_AGE_MIRRORLIST) {
- DEBUG(repo->pakfire, "Skip refreshing mirrorlist which is %lds old\n", age);
+ CTX_DEBUG(repo->ctx, "Skip refreshing mirrorlist which is %lds old\n", age);
return 0;
}
}
// Replace any variables in the URL
url = pakfire_repo_url_replace(repo, repo->appdata->mirrorlist_url);
if (!url) {
- ERROR(repo->pakfire, "Could not expend the mirror list URL: %m\n");
+ CTX_ERROR(repo->ctx, "Could not expend the mirror list URL: %m\n");
r = -errno;
goto ERROR;
}
time_t age = pakfire_path_age(path);
if (age > 0 && age < refresh) {
- DEBUG(repo->pakfire, "Skip refreshing metadata which is %lds old\n", age);
+ CTX_DEBUG(repo->ctx, "Skip refreshing metadata which is %lds old\n", age);
return 0;
}
}
// Allocate a libsolv repository
rep->repo = repo_create(pool, name);
if (!rep->repo) {
- ERROR(rep->pakfire, "Could not allocate repo: %m\n");
+ CTX_ERROR(rep->ctx, "Could not allocate repo: %m\n");
goto ERROR;
}
// Allocate repository appdata
rep->appdata = rep->repo->appdata = pakfire_repo_setup_appdata(rep);
if (!rep->appdata) {
- ERROR(rep->pakfire, "Could not setup repository appdata for %s\n", name);
+ CTX_ERROR(rep->ctx, "Could not setup repository appdata for %s\n", name);
goto ERROR;
}
if (description) {
r = pakfire_config_set(config, section, "description", description);
if (r) {
- ERROR(repo->pakfire, "Could not set description: %m\n");
+ CTX_ERROR(repo->ctx, "Could not set description: %m\n");
goto ERROR;
}
}
if (!pakfire_repo_get_enabled(repo)) {
r = pakfire_config_set(config, section, "enabled", "0");
if (r) {
- ERROR(repo->pakfire, "Could not set enabled: %m\n");
+ CTX_ERROR(repo->ctx, "Could not set enabled: %m\n");
goto ERROR;
}
}
if (baseurl) {
r = pakfire_config_set(config, section, "baseurl", baseurl);
if (r) {
- ERROR(repo->pakfire, "Could not set base URL: %m\n");
+ CTX_ERROR(repo->ctx, "Could not set base URL: %m\n");
goto ERROR;
}
}
free(refresh);
if (r) {
- ERROR(repo->pakfire, "Could not set refresh interval: %m\n");
+ CTX_ERROR(repo->ctx, "Could not set refresh interval: %m\n");
goto ERROR;
}
}
if (mirrorlist) {
r = pakfire_config_set(config, section, "mirrors", mirrorlist);
if (r) {
- ERROR(repo->pakfire, "Could not set mirrors: %m\n");
+ CTX_ERROR(repo->ctx, "Could not set mirrors: %m\n");
goto ERROR;
}
}
if (key) {
r = pakfire_key_export(key, f, PAKFIRE_KEY_EXPORT_MODE_PUBLIC);
if (r) {
- ERROR(repo->pakfire, "Could not export the key: %m\n");
+ CTX_ERROR(repo->ctx, "Could not export the key: %m\n");
goto ERROR;
}
if (buffer) {
r = pakfire_config_set_format(config, section, "key", "%.*s", (int)length, buffer);
if (r) {
- ERROR(repo->pakfire, "Could not set key: %m\n");
+ CTX_ERROR(repo->ctx, "Could not set key: %m\n");
goto ERROR;
}
}
if (priority) {
r = pakfire_config_set_format(config, section, "priority", "%u", priority);
if (r) {
- ERROR(repo->pakfire, "Could not set priority: %m\n");
+ CTX_ERROR(repo->ctx, "Could not set priority: %m\n");
goto ERROR;
}
}
// Where do we store the package?
cache_path = pakfire_package_get_string(pkg, PAKFIRE_PKG_CACHE_PATH);
if (!cache_path) {
- ERROR(repo->pakfire, "Could not retrieve package cache path for %s: %m\n", nevra);
+ CTX_ERROR(repo->ctx, "Could not retrieve package cache path for %s: %m\n", nevra);
goto ERROR;
}
// Where do we find the package on the server?
url = pakfire_package_get_string(pkg, PAKFIRE_PKG_PATH);
if (!url) {
- ERROR(repo->pakfire, "Could not retrieve package path for %s: %m\n", nevra);
+ CTX_ERROR(repo->ctx, "Could not retrieve package path for %s: %m\n", nevra);
goto ERROR;
}
// Retrieve package digest
digest = pakfire_package_get_digest(pkg, &digest_type, &digest_length);
if (!digest) {
- ERROR(repo->pakfire, "Package %s has no digest set: %m\n", nevra);
+ CTX_ERROR(repo->ctx, "Package %s has no digest set: %m\n", nevra);
goto ERROR;
}
if (pakfire_string_is_url(path)) {
r = pakfire_repo_download(repo, path, package);
if (r)
- ERROR(repo->pakfire, "Could not download %s\n", path);
+ CTX_ERROR(repo->ctx, "Could not download %s\n", path);
return r;
}
goto ERROR;
}
- DEBUG(repo->pakfire, "Added %s to repository '%s'\n",
+ CTX_DEBUG(repo->ctx, "Added %s to repository '%s'\n",
nevra, pakfire_repo_get_name(repo));
ERROR:
// Create another repodata section for meta information
meta = repo_add_repodata(repo->repo, 0);
if (!meta) {
- ERROR(repo->pakfire, "Could not create meta repodata: %m\n");
- r = 1;
+ CTX_ERROR(repo->ctx, "Could not create meta repodata: %m\n");
+ r = -errno;
goto ERROR;
}
// Flush buffers
r = fflush(f);
if (r) {
- ERROR(repo->pakfire, "Could not flush after writing repository: %m\n");
+ CTX_ERROR(repo->ctx, "Could not flush after writing repository: %m\n");
goto ERROR;
}
if (prefix && !pakfire_string_startswith(path, prefix))
goto NEXT;
- DEBUG(repo->pakfire, "Removing %s at %s\n", nevra, path);
+ CTX_DEBUG(repo->ctx, "Removing %s at %s\n", nevra, path);
// Delete the file
r = unlink(path);
if (r) {
- ERROR(repo->pakfire, "Could not unlink %s at %s: %m\n",
- nevra, path);
+ CTX_ERROR(repo->ctx, "Could not unlink %s at %s: %m\n", nevra, path);
}
NEXT:
// Do nothing if this repository is not enabled
int enabled = pakfire_repo_get_enabled(repo);
if (!enabled) {
- DEBUG(repo->pakfire, "Skip refreshing repository '%s'\n", name);
+ CTX_DEBUG(repo->ctx, "Skip refreshing repository '%s'\n", name);
return 0;
}
// Refresh mirrorlist
r = pakfire_repo_refresh_mirrorlist(repo, force);
if (r) {
- ERROR(repo->pakfire, "Could not refresh mirrorlist, but will continue anyways...\n");
+ CTX_ERROR(repo->ctx, "Could not refresh mirrorlist, but will continue anyways...\n");
}
// Refresh metadata
// Open the signature file for writing
s = fopen(signature_path, "w");
if (!s) {
- ERROR(repo->pakfire, "Could not open %s for writing: %m\n", signature_path);
+ CTX_ERROR(repo->ctx, "Could not open %s for writing: %m\n", signature_path);
r = 1;
goto ERROR;
}
// Open the database for reading
f = fopen(database_path, "r");
if (!f) {
- ERROR(repo->pakfire, "Could not open %s for reading: %m\n", database_path);
+ CTX_ERROR(repo->ctx, "Could not open %s for reading: %m\n", database_path);
r = 1;
goto ERROR;
}
// Create the signature
r = pakfire_key_signf(key, s, f, "Database Signature");
if (r) {
- ERROR(repo->pakfire, "Could not sign the database: %m\n");
+ CTX_ERROR(repo->ctx, "Could not sign the database: %m\n");
goto ERROR;
}
// Create a temporary file to write to
FILE* f = pakfire_mktemp(tmp, 0644);
if (!f) {
- ERROR(repo->pakfire, "Could not open temporary file for writing: %m\n");
+ CTX_ERROR(repo->ctx, "Could not open temporary file for writing: %m\n");
return 1;
}
// Initialize the output being compressed
f = pakfire_zstdfopen(f, "w");
if (!f) {
- ERROR(repo->pakfire, "Could not initialize compression: %m\n");
+ CTX_ERROR(repo->ctx, "Could not initialize compression: %m\n");
return 1;
}
// Write the SOLV database to the temporary file
r = pakfire_repo_write_solv(repo, f, 0);
if (r) {
- ERROR(repo->pakfire, "Could not write SOLV data: %m\n");
+ CTX_ERROR(repo->ctx, "Could not write SOLV data: %m\n");
goto ERROR;
}
// Create a filename for the database file
r = __pakfire_strftime_now(filename, length, "%Y-%m-%d-%H%M.%s.solv.zst");
if (r) {
- ERROR(repo->pakfire, "Could not format database filename: %m\n");
+ CTX_ERROR(repo->ctx, "Could not format database filename: %m\n");
goto ERROR;
}
// Make final database path
r = pakfire_string_format(database, "%s/repodata/%s", path, filename);
if (r) {
- ERROR(repo->pakfire, "Could not join database path: %m\n");
+ CTX_ERROR(repo->ctx, "Could not join database path: %m\n");
goto ERROR;
}
// Link database to its destination
r = link(tmp, database);
if (r) {
- ERROR(repo->pakfire, "Could not link database %s: %m\n", database);
+ CTX_ERROR(repo->ctx, "Could not link database %s: %m\n", database);
goto ERROR;
}
if (key) {
r = pakfire_repo_sign_database(repo, key, database);
if (r) {
- ERROR(repo->pakfire, "Could not sign the database: %m\n");
+ CTX_ERROR(repo->ctx, "Could not sign the database: %m\n");
goto ERROR;
}
}
// Compose JSON object
repomd = json_object_new_object();
if (!repomd) {
- ERROR(repo->pakfire, "Could not allocate a new JSON object: %m\n");
+ CTX_ERROR(repo->ctx, "Could not allocate a new JSON object: %m\n");
r = 1;
goto ERROR;
}
// Set version
struct json_object* repomd_version = json_object_new_int64(0);
if (!repomd_version) {
- ERROR(repo->pakfire, "Could not create version: %m\n");
+ CTX_ERROR(repo->ctx, "Could not create version: %m\n");
r = 1;
goto ERROR;
}
r = json_object_object_add(repomd, "version", repomd_version);
if (r) {
- ERROR(repo->pakfire, "Could not add version to repomd.json: %m\n");
+ CTX_ERROR(repo->ctx, "Could not add version to repomd.json: %m\n");
goto ERROR;
}
// Set revision
struct json_object* repomd_revision = json_object_new_int64(revision);
if (!repomd_revision) {
- ERROR(repo->pakfire, "Could not create revision: %m\n");
+ CTX_ERROR(repo->ctx, "Could not create revision: %m\n");
r = 1;
goto ERROR;
}
r = json_object_object_add(repomd, "revision", repomd_revision);
if (r) {
- ERROR(repo->pakfire, "Could not add revision to repomd.json: %m\n");
+ CTX_ERROR(repo->ctx, "Could not add revision to repomd.json: %m\n");
goto ERROR;
}
// Set database
struct json_object* repomd_database = json_object_new_string(database);
if (!repomd_database) {
- ERROR(repo->pakfire, "Could not create database string: %m\n");
+ CTX_ERROR(repo->ctx, "Could not create database string: %m\n");
r = 1;
goto ERROR;
}
r = json_object_object_add(repomd, "database", repomd_database);
if (r) {
- ERROR(repo->pakfire, "Could not add database to repomd.json: %m\n");
+ CTX_ERROR(repo->ctx, "Could not add database to repomd.json: %m\n");
goto ERROR;
}
// Open repomd.json for writing
f = fopen(repomd_path, "w");
if (!f) {
- ERROR(repo->pakfire, "Could not open %s for writing: %m\n", repomd_path);
+ CTX_ERROR(repo->ctx, "Could not open %s for writing: %m\n", repomd_path);
r = 1;
goto ERROR;
}
// Write out repomd.json
r = json_object_to_fd(fileno(f), repomd, JSON_C_TO_STRING_PRETTY);
if (r) {
- ERROR(repo->pakfire, "Could not write repomd.json: %m\n");
+ CTX_ERROR(repo->ctx, "Could not write repomd.json: %m\n");
goto ERROR;
}
return 1;
}
+ // Fetch context
+ struct pakfire_ctx* ctx = pakfire_ctx(pakfire);
+
// XXX Check if the key is a secret key
size_t num_files = 0;
// Make path absolute
r = pakfire_path_realpath(realpath, path);
if (r)
- return r;
+ goto ERROR;
// Prefix path with file:// to form baseurl
r = pakfire_string_format(baseurl, "file://%s", realpath);
// Create a new temporary repository at path
r = pakfire_repo_create(&repo, pakfire, "tmp");
if (r) {
- ERROR(pakfire, "Could not create a temporary repository: %m\n");
+ CTX_ERROR(ctx, "Could not create a temporary repository: %m\n");
goto ERROR;
}
// Set baseurl to path
r = pakfire_repo_set_baseurl(repo, baseurl);
if (r) {
- ERROR(pakfire, "Could not set baseurl %s: %m\n", baseurl);
+ CTX_ERROR(ctx, "Could not set baseurl %s: %m\n", baseurl);
goto ERROR;
}
// Find everything that is already part of the repository
r = pakfire_repo_scan(repo, 0);
if (r) {
- ERROR(pakfire, "Could not refresh repository: %m\n");
+ CTX_ERROR(ctx, "Could not refresh repository: %m\n");
goto ERROR;
}
char destination_path[PATH_MAX];
char repo_path[PATH_MAX];
- DEBUG(pakfire, "Adding %zu file(s) to the repository\n", num_files);
+ CTX_DEBUG(ctx, "Adding %zu file(s) to the repository\n", num_files);
// Add more files
if (files) {
for (const char** file = files; *file; file++) {
- DEBUG(pakfire, "Adding %s to repository...\n", *file);
+ CTX_DEBUG(ctx, "Adding %s to repository...\n", *file);
// Open source archive
r = pakfire_archive_open(&archive, pakfire, *file);
if (r) {
- ERROR(pakfire, "Could not open %s: %m\n", *file);
+ CTX_ERROR(ctx, "Could not open %s: %m\n", *file);
goto OUT;
}
// Add package metadata
r = pakfire_repo_add_archive(repo, archive, &package);
if (r) {
- ERROR(pakfire, "Could not add %s to the repository: %m\n", *file);
+ CTX_ERROR(ctx, "Could not add %s to the repository: %m\n", *file);
goto OUT;
}
// Fetch the UUID
uuid = pakfire_package_get_string(package, PAKFIRE_PKG_UUID);
if (!uuid) {
- ERROR(pakfire, "Could not retrieve the UUID of %s: %m\n", *file);
+ CTX_ERROR(ctx, "Could not retrieve the UUID of %s: %m\n", *file);
r = -EINVAL;
goto OUT;
}
// Fetch the filename
filename = pakfire_package_get_string(package, PAKFIRE_PKG_FILENAME);
if (!filename) {
- ERROR(pakfire, "Could not retrieve filename of %s: %m\n", *file);
+ CTX_ERROR(ctx, "Could not retrieve filename of %s: %m\n", *file);
r = -EINVAL;
goto OUT;
}
// Copying archive to destination
r = pakfire_archive_link_or_copy(archive, destination_path);
if (r) {
- ERROR(pakfire, "Could not copy archive to %s: %m\n", destination_path);
+ CTX_ERROR(ctx, "Could not copy archive to %s: %m\n", destination_path);
goto OUT;
}
pakfire_repo_clear(repo);
pakfire_repo_unref(repo);
}
+ if (ctx)
+ pakfire_ctx_unref(ctx);
return r;
}