char* description;
char* baseurl;
- char* keyfile;
char metadata[PATH_MAX];
+ // Key fingerprint
+ char* key;
+
// Mirrorlist
char* mirrorlist_url;
char mirrorlist[PATH_MAX];
return r;
}
-static int pakfire_repo_import_key(struct pakfire_repo* repo, const char* url) {
- const char* name = pakfire_repo_get_name(repo);
- DEBUG(repo->pakfire, "Importing key for repository %s from %s...\n", name, url);
-
- char path[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-key.XXXXXX";
+static int pakfire_repo_import_key(struct pakfire_repo* repo, FILE* f) {
struct pakfire_key** keys = NULL;
+ size_t counter = 0;
+ int r;
- // Allocate a temporary file name
- FILE* f = pakfire_mktemp(path);
- if (!f)
- return 1;
-
- // Try to download the key
- int r = pakfire_repo_retrieve(repo, NULL, url, path,
- 0, NULL, 0, PAKFIRE_TRANSFER_NOTEMP|PAKFIRE_TRANSFER_NOPROGRESS);
- if (r)
- goto ERROR;
-
- // Try importing the key
+ // Import any keys from the file descriptor
r = pakfire_key_import(repo->pakfire, f, &keys);
if (r)
+ return r;
+
+ // Count how many keys were imported
+ for (struct pakfire_key** key = keys; *key; key++)
+ counter++;
+
+ // We should only import one key
+ if (counter != 1) {
+ ERROR(repo->pakfire, "Imported %zu key(s) but expected one\n", counter);
+ r = 1;
goto ERROR;
+ }
- r = 0;
+ for (struct pakfire_key** key = keys; *key; key++) {
+ char* dump = pakfire_key_dump(*key);
-ERROR:
- if (f)
- fclose(f);
- unlink(path);
+ if (dump)
+ DEBUG(repo->pakfire, "Imported key:\n%s\n", dump);
+ }
- // Free keys
+ERROR:
if (keys) {
for (struct pakfire_key** key = keys; *key; key++)
pakfire_key_unref(*key);
pakfire_repo_set_mirrorlist_url(repo, mirrors);
// Key
- const char* key = pakfire_config_get(config, *section, "key", NULL);
+ FILE* key = pakfire_config_get_fopen(config, *section, "key");
if (key) {
r = pakfire_repo_import_key(repo, key);
if (r) {
if (appdata->description)
free(appdata->description);
- if (appdata->keyfile)
- free(appdata->keyfile);
-
if (appdata->mirrorlist_url)
free(appdata->mirrorlist_url);
return repo->appdata->baseurl + strlen("file://");
}
-PAKFIRE_EXPORT const char* pakfire_repo_get_keyfile(struct pakfire_repo* repo) {
- return repo->appdata->keyfile;
-}
-
-PAKFIRE_EXPORT int pakfire_repo_set_keyfile(struct pakfire_repo* repo, const char* keyfile) {
- if (repo->appdata->keyfile)
- free(repo->appdata->keyfile);
-
- if (keyfile)
- repo->appdata->keyfile = strdup(keyfile);
- else
- repo->appdata->keyfile = NULL;
-
- return 0;
+PAKFIRE_EXPORT struct pakfire_key* pakfire_repo_get_key(struct pakfire_repo* repo) {
+ return pakfire_key_get(repo->pakfire, repo->appdata->key);
}
PAKFIRE_EXPORT const char* pakfire_repo_get_mirrorlist_url(struct pakfire_repo* repo) {
return 0;
}
-PAKFIRE_EXPORT char* pakfire_repo_get_config(struct pakfire_repo* repo) {
+PAKFIRE_EXPORT int pakfire_repo_write_config(struct pakfire_repo* repo, FILE* f) {
+ int r;
+
+ // Do nothing for the installed repository
if (pakfire_repo_is_installed_repo(repo) == 0)
- return NULL;
+ return 0;
- char buffer[1024 * 4];
- char* p = buffer;
+ struct pakfire_config* config = NULL;
+ char* section = NULL;
- // Headline
- p += sprintf(p, "[repo:%s]\n", pakfire_repo_get_name(repo));
+ struct pakfire_key* key = NULL;
+ char* buffer = NULL;
+ size_t length = 0;
+
+ // Create section name
+ r = asprintf(§ion, "[repo:%s]", pakfire_repo_get_name(repo));
+ if (r < 0)
+ goto ERROR;
+
+ // Create a new configuration
+ r = pakfire_config_create(&config);
+ if (r)
+ goto ERROR;
+ // Description
const char* description = pakfire_repo_get_description(repo);
- if (description)
- p += sprintf(p, "description = %s\n", description);
+ if (description) {
+ r = pakfire_config_set(config, section, "description", description);
+ if (r) {
+ ERROR(repo->pakfire, "Could not set description: %m\n");
+ goto ERROR;
+ }
+ }
- // Enabled
- p += sprintf(p, "enabled = %d\n", pakfire_repo_get_enabled(repo));
+ // Disabled?
+ 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");
+ goto ERROR;
+ }
+ }
// Base URL
const char* baseurl = pakfire_repo_get_baseurl(repo);
- if (baseurl)
- p += sprintf(p, "baseurl = %s\n", baseurl);
+ if (baseurl) {
+ r = pakfire_config_set(config, section, "baseurl", baseurl);
+ if (r) {
+ ERROR(repo->pakfire, "Could not set base URL: %m\n");
+ goto ERROR;
+ }
+ }
// Mirror List
const char* mirrorlist = pakfire_repo_get_mirrorlist_url(repo);
- if (mirrorlist)
- p += sprintf(p, "mirrors = %s\n", mirrorlist);
+ if (mirrorlist) {
+ r = pakfire_config_set(config, section, "mirrors", mirrorlist);
+ if (r) {
+ ERROR(repo->pakfire, "Could not set mirrors: %m\n");
+ goto ERROR;
+ }
+ }
+
+ // Key
+ key = pakfire_repo_get_key(repo);
+ if (key) {
+ r = pakfire_key_get_public_key(key, &buffer, &length);
+ if (r) {
+ ERROR(repo->pakfire, "Could not fetch public key: %m\n");
+ goto ERROR;
+ }
- // Key File
- const char* keyfile = pakfire_repo_get_keyfile(repo);
- if (keyfile)
- p += sprintf(p, "keyfile = %s\n", keyfile);
+ r = pakfire_config_set(config, section, "key", buffer);
+ if (r) {
+ ERROR(repo->pakfire, "Could not set key: %m\n");
+ goto ERROR;
+ }
+ }
// Priority
- p += sprintf(p, "priority = %d\n", pakfire_repo_get_priority(repo));
+ unsigned int priority = pakfire_repo_get_priority(repo);
+ if (priority) {
+ r = pakfire_config_set_format(config, section, "priority", "%d", priority);
+ if (r) {
+ ERROR(repo->pakfire, "Could not set priority: %m\n");
+ goto ERROR;
+ }
+ }
+
+ // Write the configuration
+ r = pakfire_config_dump(config, f);
- return strdup(buffer);
+ERROR:
+ if (key)
+ pakfire_key_unref(key);
+ if (config)
+ pakfire_config_unref(config);
+ if (section)
+ free(section);
+ if (buffer)
+ free(buffer);
+
+ return r;
}
PAKFIRE_EXPORT int pakfire_repo_is_installed_repo(struct pakfire_repo* repo) {