Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib/libalpm/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/db.c')
-rw-r--r--lib/libalpm/db.c247
1 files changed, 157 insertions, 90 deletions
diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c
index 1a3168b4..31336144 100644
--- a/lib/libalpm/db.c
+++ b/lib/libalpm/db.c
@@ -26,11 +26,8 @@
#include <stdio.h>
#include <stdlib.h>
-#include <errno.h>
#include <string.h>
-#include <sys/stat.h>
#include <regex.h>
-#include <time.h>
/* libalpm */
#include "db.h"
@@ -58,7 +55,7 @@ pmdb_t SYMEXPORT *alpm_db_register_sync(const char *treename)
/* Do not register a database if a transaction is on-going */
ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, NULL));
- return(_alpm_db_register_sync(treename));
+ return _alpm_db_register_sync(treename);
}
/* Helper function for alpm_db_unregister{_all} */
@@ -92,7 +89,7 @@ int SYMEXPORT alpm_db_unregister_all(void)
i->data = NULL;
}
FREELIST(handle->dbs_sync);
- return(0);
+ return 0;
}
/** Unregister a package database. */
@@ -129,49 +126,119 @@ int SYMEXPORT alpm_db_unregister(pmdb_t *db)
}
db->ops->unregister(db);
- return(0);
+ return 0;
+}
+
+/** Get the serverlist of a database. */
+alpm_list_t SYMEXPORT *alpm_db_get_servers(const pmdb_t *db)
+{
+ ALPM_LOG_FUNC;
+
+ /* Sanity checks */
+ ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, NULL));
+
+ return(db->servers);
}
/** Set the serverlist of a database. */
-int SYMEXPORT alpm_db_setserver(pmdb_t *db, const char *url)
+int SYMEXPORT alpm_db_set_servers(pmdb_t *db, alpm_list_t *servers)
+{
+ ALPM_LOG_FUNC;
+
+ /* Sanity checks */
+ ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
+
+ if(db->servers) FREELIST(db->servers);
+ db->servers = servers;
+ return 0;
+}
+
+static char *sanitize_url(const char *url)
+{
+ char *newurl;
+ size_t len = strlen(url);
+
+ STRDUP(newurl, url, RET_ERR(PM_ERR_MEMORY, NULL));
+ /* strip the trailing slash if one exists */
+ if(newurl[len - 1] == '/') {
+ newurl[len - 1] = '\0';
+ }
+ return newurl;
+}
+
+/** Add a download server to a database.
+ * @param db database pointer
+ * @param url url of the server
+ * @return 0 on success, -1 on error (pm_errno is set accordingly)
+ */
+int SYMEXPORT alpm_db_add_server(pmdb_t *db, const char *url)
{
- alpm_list_t *i;
- int found = 0;
char *newurl;
- size_t len = 0;
ALPM_LOG_FUNC;
/* Sanity checks */
ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
+ ASSERT(url != NULL && strlen(url) != 0, RET_ERR(PM_ERR_WRONG_ARGS, -1));
- for(i = handle->dbs_sync; i && !found; i = i->next) {
- pmdb_t *sdb = i->data;
- if(strcmp(db->treename, sdb->treename) == 0) {
- found = 1;
- }
- }
- if(!found) {
- RET_ERR(PM_ERR_DB_NOT_FOUND, -1);
+ newurl = sanitize_url(url);
+ if(!newurl) {
+ return -1;
}
+ db->servers = alpm_list_add(db->servers, newurl);
+ _alpm_log(PM_LOG_DEBUG, "adding new server URL to database '%s': %s\n",
+ db->treename, newurl);
+
+ return 0;
+}
+
+/** Remove a download server from a database.
+ * @param db database pointer
+ * @param url url of the server
+ * @return 0 on success, 1 on server not present,
+ * -1 on error (pm_errno is set accordingly)
+ */
+int SYMEXPORT alpm_db_remove_server(pmdb_t *db, const char *url)
+{
+ char *newurl, *vdata = NULL;
- if(url) {
- len = strlen(url);
+ ALPM_LOG_FUNC;
+
+ /* Sanity checks */
+ ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
+ ASSERT(url != NULL && strlen(url) != 0, RET_ERR(PM_ERR_WRONG_ARGS, -1));
+
+ newurl = sanitize_url(url);
+ if(!newurl) {
+ return -1;
}
- if(len) {
- newurl = strdup(url);
- /* strip the trailing slash if one exists */
- if(newurl[len - 1] == '/') {
- newurl[len - 1] = '\0';
- }
- db->servers = alpm_list_add(db->servers, newurl);
- _alpm_log(PM_LOG_DEBUG, "adding new server URL to database '%s': %s\n",
+ db->servers = alpm_list_remove_str(db->servers, newurl, &vdata);
+ free(newurl);
+ if(vdata) {
+ _alpm_log(PM_LOG_DEBUG, "removed server URL from database '%s': %s\n",
db->treename, newurl);
- } else {
- FREELIST(db->servers);
- _alpm_log(PM_LOG_DEBUG, "serverlist flushed for '%s'\n", db->treename);
+ free(vdata);
+ return 0;
}
+ return 1;
+}
+/** Set the verify gpg signature option for a database.
+ * @param db database pointer
+ * @param verify enum pgp_verify_t
+ * @return 0 on success, -1 on error (pm_errno is set accordingly)
+ */
+int SYMEXPORT alpm_db_set_pgp_verify(pmdb_t *db, pgp_verify_t verify)
+{
+ ALPM_LOG_FUNC;
+
+ /* Sanity checks */
+ ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
+
+ db->pgp_verify = verify;
+ _alpm_log(PM_LOG_DEBUG, "adding VerifySig option to database '%s': %d\n",
+ db->treename, verify);
+
return(0);
}
@@ -181,8 +248,8 @@ const char SYMEXPORT *alpm_db_get_name(const pmdb_t *db)
ALPM_LOG_FUNC;
/* Sanity checks */
- ASSERT(handle != NULL, return(NULL));
- ASSERT(db != NULL, return(NULL));
+ ASSERT(handle != NULL, return NULL);
+ ASSERT(db != NULL, return NULL);
return db->treename;
}
@@ -195,13 +262,13 @@ const char SYMEXPORT *alpm_db_get_url(const pmdb_t *db)
ALPM_LOG_FUNC;
/* Sanity checks */
- ASSERT(handle != NULL, return(NULL));
- ASSERT(db != NULL, return(NULL));
- ASSERT(db->servers != NULL, return(NULL));
+ ASSERT(handle != NULL, return NULL);
+ ASSERT(db != NULL, return NULL);
+ ASSERT(db->servers != NULL, return NULL);
- url = (char*)db->servers->data;
+ url = (char *)db->servers->data;
- return(url);
+ return url;
}
@@ -211,11 +278,11 @@ pmpkg_t SYMEXPORT *alpm_db_get_pkg(pmdb_t *db, const char *name)
ALPM_LOG_FUNC;
/* Sanity checks */
- ASSERT(handle != NULL, return(NULL));
- ASSERT(db != NULL, return(NULL));
- ASSERT(name != NULL && strlen(name) != 0, return(NULL));
+ ASSERT(handle != NULL, return NULL);
+ ASSERT(db != NULL, return NULL);
+ ASSERT(name != NULL && strlen(name) != 0, return NULL);
- return(_alpm_db_get_pkgfromcache(db, name));
+ return _alpm_db_get_pkgfromcache(db, name);
}
/** Get the package cache of a package database. */
@@ -224,10 +291,10 @@ alpm_list_t SYMEXPORT *alpm_db_get_pkgcache(pmdb_t *db)
ALPM_LOG_FUNC;
/* Sanity checks */
- ASSERT(handle != NULL, return(NULL));
- ASSERT(db != NULL, return(NULL));
+ ASSERT(handle != NULL, return NULL);
+ ASSERT(db != NULL, return NULL);
- return(_alpm_db_get_pkgcache(db));
+ return _alpm_db_get_pkgcache(db);
}
/** Get a group entry from a package database. */
@@ -236,11 +303,11 @@ pmgrp_t SYMEXPORT *alpm_db_readgrp(pmdb_t *db, const char *name)
ALPM_LOG_FUNC;
/* Sanity checks */
- ASSERT(handle != NULL, return(NULL));
- ASSERT(db != NULL, return(NULL));
- ASSERT(name != NULL && strlen(name) != 0, return(NULL));
+ ASSERT(handle != NULL, return NULL);
+ ASSERT(db != NULL, return NULL);
+ ASSERT(name != NULL && strlen(name) != 0, return NULL);
- return(_alpm_db_get_grpfromcache(db, name));
+ return _alpm_db_get_grpfromcache(db, name);
}
/** Get the group cache of a package database. */
@@ -249,10 +316,10 @@ alpm_list_t SYMEXPORT *alpm_db_get_grpcache(pmdb_t *db)
ALPM_LOG_FUNC;
/* Sanity checks */
- ASSERT(handle != NULL, return(NULL));
- ASSERT(db != NULL, return(NULL));
+ ASSERT(handle != NULL, return NULL);
+ ASSERT(db != NULL, return NULL);
- return(_alpm_db_get_grpcache(db));
+ return _alpm_db_get_grpcache(db);
}
/** Searches a database. */
@@ -261,10 +328,10 @@ alpm_list_t SYMEXPORT *alpm_db_search(pmdb_t *db, const alpm_list_t* needles)
ALPM_LOG_FUNC;
/* Sanity checks */
- ASSERT(handle != NULL, return(NULL));
- ASSERT(db != NULL, return(NULL));
+ ASSERT(handle != NULL, return NULL);
+ ASSERT(db != NULL, return NULL);
- return(_alpm_db_search(db, needles));
+ return _alpm_db_search(db, needles);
}
/** Set install reason for a package in db. */
@@ -284,7 +351,7 @@ int SYMEXPORT alpm_db_set_pkgreason(pmdb_t *db, const char *name, pmpkgreason_t
_alpm_log(PM_LOG_DEBUG, "setting install reason %u for %s/%s\n", reason, db->treename, name);
if(alpm_pkg_get_reason(pkg) == reason) {
/* we are done */
- return(0);
+ return 0;
}
/* set reason (in pkgcache) */
pkg->reason = reason;
@@ -293,7 +360,7 @@ int SYMEXPORT alpm_db_set_pkgreason(pmdb_t *db, const char *name, pmpkgreason_t
RET_ERR(PM_ERR_DB_WRITE, -1);
}
- return(0);
+ return 0;
}
/** @} */
@@ -308,7 +375,7 @@ pmdb_t *_alpm_db_new(const char *treename, int is_local)
STRDUP(db->treename, treename, RET_ERR(PM_ERR_MEMORY, NULL));
db->is_local = is_local;
- return(db);
+ return db;
}
void _alpm_db_free(pmdb_t *db)
@@ -329,7 +396,7 @@ void _alpm_db_free(pmdb_t *db)
const char *_alpm_db_path(pmdb_t *db)
{
if(!db) {
- return(NULL);
+ return NULL;
}
if(!db->_path) {
const char *dbpath;
@@ -354,22 +421,22 @@ const char *_alpm_db_path(pmdb_t *db)
_alpm_log(PM_LOG_DEBUG, "database path for tree %s set to %s\n",
db->treename, db->_path);
}
- return(db->_path);
+ return db->_path;
}
int _alpm_db_version(pmdb_t *db)
{
if(!db) {
- return(-1);
+ return -1;
}
- return(db->ops->version(db));
+ return db->ops->version(db);
}
int _alpm_db_cmp(const void *d1, const void *d2)
{
pmdb_t *db1 = (pmdb_t *)d1;
pmdb_t *db2 = (pmdb_t *)d2;
- return(strcmp(db1->treename, db2->treename));
+ return strcmp(db1->treename, db2->treename);
}
alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles)
@@ -407,7 +474,7 @@ alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles)
matched = name;
}
/* check desc */
- else if (desc && regexec(&reg, desc, 0, 0, 0) == 0) {
+ else if(desc && regexec(&reg, desc, 0, 0, 0) == 0) {
matched = desc;
}
/* TODO: should we be doing this, and should we print something
@@ -415,7 +482,7 @@ alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles)
if(!matched) {
/* check provides */
for(k = alpm_pkg_get_provides(pkg); k; k = k->next) {
- if (regexec(&reg, k->data, 0, 0, 0) == 0) {
+ if(regexec(&reg, k->data, 0, 0, 0) == 0) {
matched = k->data;
break;
}
@@ -424,7 +491,7 @@ alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles)
if(!matched) {
/* check groups */
for(k = alpm_pkg_get_groups(pkg); k; k = k->next) {
- if (regexec(&reg, k->data, 0, 0, 0) == 0) {
+ if(regexec(&reg, k->data, 0, 0, 0) == 0) {
matched = k->data;
break;
}
@@ -445,7 +512,7 @@ alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles)
regfree(&reg);
}
- return(ret);
+ return ret;
}
/* Returns a new package cache from db.
@@ -456,7 +523,7 @@ int _alpm_db_load_pkgcache(pmdb_t *db)
ALPM_LOG_FUNC;
if(db == NULL) {
- return(-1);
+ return -1;
}
_alpm_db_free_pkgcache(db);
@@ -465,11 +532,11 @@ int _alpm_db_load_pkgcache(pmdb_t *db)
if(db->ops->populate(db) == -1) {
_alpm_log(PM_LOG_DEBUG,
"failed to load package cache for repository '%s'\n", db->treename);
- return(-1);
+ return -1;
}
db->pkgcache_loaded = 1;
- return(0);
+ return 0;
}
void _alpm_db_free_pkgcache(pmdb_t *db)
@@ -496,7 +563,7 @@ pmpkghash_t *_alpm_db_get_pkgcache_hash(pmdb_t *db)
ALPM_LOG_FUNC;
if(db == NULL) {
- return(NULL);
+ return NULL;
}
if(!db->pkgcache_loaded) {
@@ -508,7 +575,7 @@ pmpkghash_t *_alpm_db_get_pkgcache_hash(pmdb_t *db)
_alpm_log(PM_LOG_DEBUG, "warning: pkgcache is NULL for db '%s'\n", db->treename);
}
- return(db->pkgcache);
+ return db->pkgcache;
}
alpm_list_t *_alpm_db_get_pkgcache(pmdb_t *db)
@@ -518,10 +585,10 @@ alpm_list_t *_alpm_db_get_pkgcache(pmdb_t *db)
pmpkghash_t *hash = _alpm_db_get_pkgcache_hash(db);
if(hash == NULL) {
- return(NULL);
+ return NULL;
}
- return(hash->list);
+ return hash->list;
}
/* "duplicate" pkg then add it to pkgcache */
@@ -532,12 +599,12 @@ int _alpm_db_add_pkgincache(pmdb_t *db, pmpkg_t *pkg)
ALPM_LOG_FUNC;
if(db == NULL || !db->pkgcache_loaded || pkg == NULL) {
- return(-1);
+ return -1;
}
newpkg = _alpm_pkg_dup(pkg);
if(newpkg == NULL) {
- return(-1);
+ return -1;
}
_alpm_log(PM_LOG_DEBUG, "adding entry '%s' in '%s' cache\n",
@@ -546,7 +613,7 @@ int _alpm_db_add_pkgincache(pmdb_t *db, pmpkg_t *pkg)
_alpm_db_free_grpcache(db);
- return(0);
+ return 0;
}
int _alpm_db_remove_pkgfromcache(pmdb_t *db, pmpkg_t *pkg)
@@ -556,7 +623,7 @@ int _alpm_db_remove_pkgfromcache(pmdb_t *db, pmpkg_t *pkg)
ALPM_LOG_FUNC;
if(db == NULL || !db->pkgcache_loaded || pkg == NULL) {
- return(-1);
+ return -1;
}
_alpm_log(PM_LOG_DEBUG, "removing entry '%s' from '%s' cache\n",
@@ -567,14 +634,14 @@ int _alpm_db_remove_pkgfromcache(pmdb_t *db, pmpkg_t *pkg)
/* package not found */
_alpm_log(PM_LOG_DEBUG, "cannot remove entry '%s' from '%s' cache: not found\n",
alpm_pkg_get_name(pkg), db->treename);
- return(-1);
+ return -1;
}
_alpm_pkg_free(data);
_alpm_db_free_grpcache(db);
- return(0);
+ return 0;
}
pmpkg_t *_alpm_db_get_pkgfromcache(pmdb_t *db, const char *target)
@@ -582,17 +649,17 @@ pmpkg_t *_alpm_db_get_pkgfromcache(pmdb_t *db, const char *target)
ALPM_LOG_FUNC;
if(db == NULL) {
- return(NULL);
+ return NULL;
}
pmpkghash_t *pkgcache = _alpm_db_get_pkgcache_hash(db);
if(!pkgcache) {
_alpm_log(PM_LOG_DEBUG, "warning: failed to get '%s' from NULL pkgcache\n",
target);
- return(NULL);
+ return NULL;
}
- return(_alpm_pkghash_find(pkgcache, target));
+ return _alpm_pkghash_find(pkgcache, target);
}
/* Returns a new group cache from db.
@@ -604,7 +671,7 @@ int _alpm_db_load_grpcache(pmdb_t *db)
ALPM_LOG_FUNC;
if(db == NULL) {
- return(-1);
+ return -1;
}
_alpm_log(PM_LOG_DEBUG, "loading group cache for repository '%s'\n",
@@ -642,7 +709,7 @@ int _alpm_db_load_grpcache(pmdb_t *db)
}
db->grpcache_loaded = 1;
- return(0);
+ return 0;
}
void _alpm_db_free_grpcache(pmdb_t *db)
@@ -671,14 +738,14 @@ alpm_list_t *_alpm_db_get_grpcache(pmdb_t *db)
ALPM_LOG_FUNC;
if(db == NULL) {
- return(NULL);
+ return NULL;
}
if(!db->grpcache_loaded) {
_alpm_db_load_grpcache(db);
}
- return(db->grpcache);
+ return db->grpcache;
}
pmgrp_t *_alpm_db_get_grpfromcache(pmdb_t *db, const char *target)
@@ -688,18 +755,18 @@ pmgrp_t *_alpm_db_get_grpfromcache(pmdb_t *db, const char *target)
ALPM_LOG_FUNC;
if(db == NULL || target == NULL || strlen(target) == 0) {
- return(NULL);
+ return NULL;
}
for(i = _alpm_db_get_grpcache(db); i; i = i->next) {
pmgrp_t *info = i->data;
if(strcmp(info->name, target) == 0) {
- return(info);
+ return info;
}
}
- return(NULL);
+ return NULL;
}
/* vim: set ts=2 sw=2 noet: */