2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* db.c
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2011-01-05 23:45:15 -05:00
|
|
|
* Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
|
2009-07-01 03:08:33 -04:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2006-10-15 15:31:03 -04:00
|
|
|
* Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
|
|
|
|
* Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
|
|
|
|
* Copyright (c) 2006 by David Kimpe <dnaku@frugalware.org>
|
|
|
|
* Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2005-03-14 20:51:43 -05:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-03-14 20:51:43 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2007-03-05 17:13:33 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-01-16 22:57:53 -05:00
|
|
|
#include <regex.h>
|
2007-03-05 17:13:33 -05:00
|
|
|
|
|
|
|
/* libalpm */
|
|
|
|
#include "db.h"
|
|
|
|
#include "alpm_list.h"
|
2005-03-28 03:21:17 -05:00
|
|
|
#include "log.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
#include "util.h"
|
2006-10-15 15:31:03 -04:00
|
|
|
#include "handle.h"
|
2005-03-28 03:21:17 -05:00
|
|
|
#include "alpm.h"
|
2008-05-11 17:49:01 -04:00
|
|
|
#include "package.h"
|
2010-10-13 08:56:35 -04:00
|
|
|
#include "group.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2011-06-03 13:23:32 -04:00
|
|
|
/* global handle variable */
|
|
|
|
extern pmhandle_t *handle;
|
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** \addtogroup alpm_databases Database Functions
|
|
|
|
* @brief Functions to query and manipulate the database of libalpm
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Register a sync database of packages. */
|
2007-08-26 22:42:17 -04:00
|
|
|
pmdb_t SYMEXPORT *alpm_db_register_sync(const char *treename)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, NULL));
|
|
|
|
ASSERT(treename != NULL && strlen(treename) != 0, RET_ERR(PM_ERR_WRONG_ARGS, NULL));
|
|
|
|
/* Do not register a database if a transaction is on-going */
|
|
|
|
ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, NULL));
|
|
|
|
|
2011-06-03 13:44:01 -04:00
|
|
|
return _alpm_db_register_sync(handle, treename);
|
2007-08-26 22:42:17 -04:00
|
|
|
}
|
|
|
|
|
2007-08-21 23:29:10 -04:00
|
|
|
/* Helper function for alpm_db_unregister{_all} */
|
2010-07-10 22:06:21 -04:00
|
|
|
void _alpm_db_unregister(pmdb_t *db)
|
2007-08-21 23:29:10 -04:00
|
|
|
{
|
|
|
|
if(db == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-23 22:26:55 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "unregistering database '%s'\n", db->treename);
|
2007-08-21 23:29:10 -04:00
|
|
|
_alpm_db_free(db);
|
|
|
|
}
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Unregister all package databases. */
|
2011-06-03 16:24:01 -04:00
|
|
|
int SYMEXPORT alpm_db_unregister_all(pmhandle_t *handle)
|
2007-07-09 11:02:29 -04:00
|
|
|
{
|
|
|
|
alpm_list_t *i;
|
2010-07-10 22:06:21 -04:00
|
|
|
pmdb_t *db;
|
2007-07-09 11:02:29 -04:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
|
|
|
|
/* Do not unregister a database if a transaction is on-going */
|
2007-09-07 18:36:38 -04:00
|
|
|
ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
|
2007-07-09 11:02:29 -04:00
|
|
|
|
2011-03-29 03:20:54 -04:00
|
|
|
/* unregister all sync dbs */
|
2007-07-09 11:02:29 -04:00
|
|
|
for(i = handle->dbs_sync; i; i = i->next) {
|
2010-07-10 22:06:21 -04:00
|
|
|
db = i->data;
|
|
|
|
db->ops->unregister(db);
|
2007-07-09 11:02:29 -04:00
|
|
|
i->data = NULL;
|
|
|
|
}
|
|
|
|
FREELIST(handle->dbs_sync);
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-07-09 11:02:29 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Unregister a package database. */
|
2007-06-04 23:32:38 -04:00
|
|
|
int SYMEXPORT alpm_db_unregister(pmdb_t *db)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(db != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
|
|
|
|
/* Do not unregister a database if a transaction is on-going */
|
2011-06-03 13:44:01 -04:00
|
|
|
ASSERT(db->handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2011-06-03 13:44:01 -04:00
|
|
|
if(db == db->handle->db_local) {
|
|
|
|
db->handle->db_local = NULL;
|
2007-06-03 23:57:38 -04:00
|
|
|
found = 1;
|
|
|
|
} else {
|
2007-08-21 23:29:10 -04:00
|
|
|
/* Warning : this function shouldn't be used to unregister all sync
|
|
|
|
* databases by walking through the list returned by
|
|
|
|
* alpm_option_get_syncdbs, because the db is removed from that list here.
|
2007-07-09 11:02:29 -04:00
|
|
|
*/
|
2007-06-03 23:57:38 -04:00
|
|
|
void *data;
|
2011-06-03 13:44:01 -04:00
|
|
|
db->handle->dbs_sync = alpm_list_remove(db->handle->dbs_sync,
|
2007-08-21 23:29:10 -04:00
|
|
|
db, _alpm_db_cmp, &data);
|
2007-06-03 23:57:38 -04:00
|
|
|
if(data) {
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found) {
|
|
|
|
RET_ERR(PM_ERR_DB_NOT_FOUND, -1);
|
|
|
|
}
|
|
|
|
|
2010-07-10 22:06:21 -04:00
|
|
|
db->ops->unregister(db);
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2010-10-18 17:39:04 -04:00
|
|
|
/** Get the serverlist of a database. */
|
|
|
|
alpm_list_t SYMEXPORT *alpm_db_get_servers(const pmdb_t *db)
|
|
|
|
{
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, NULL));
|
|
|
|
|
|
|
|
return(db->servers);
|
|
|
|
}
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Set the serverlist of a database. */
|
2010-10-18 17:39:04 -04:00
|
|
|
int SYMEXPORT alpm_db_set_servers(pmdb_t *db, alpm_list_t *servers)
|
|
|
|
{
|
|
|
|
/* 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)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
2008-08-03 19:16:08 -04:00
|
|
|
char *newurl;
|
2007-06-03 23:57:38 -04:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
|
2010-10-18 17:39:04 -04:00
|
|
|
ASSERT(url != NULL && strlen(url) != 0, RET_ERR(PM_ERR_WRONG_ARGS, -1));
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2010-10-18 17:39:04 -04:00
|
|
|
newurl = sanitize_url(url);
|
|
|
|
if(!newurl) {
|
|
|
|
return -1;
|
2008-08-03 19:16:08 -04:00
|
|
|
}
|
2010-10-18 17:39:04 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
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",
|
2008-08-03 19:16:08 -04:00
|
|
|
db->treename, newurl);
|
2010-10-18 17:39:04 -04:00
|
|
|
free(vdata);
|
|
|
|
return 0;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2010-10-18 17:39:04 -04:00
|
|
|
return 1;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
2008-12-17 05:55:07 -05:00
|
|
|
/** 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)
|
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
}
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Get the name of a package database. */
|
2007-06-05 17:34:33 -04:00
|
|
|
const char SYMEXPORT *alpm_db_get_name(const pmdb_t *db)
|
|
|
|
{
|
|
|
|
/* Sanity checks */
|
2011-03-20 20:45:57 -04:00
|
|
|
ASSERT(db != NULL, return NULL);
|
2007-06-05 17:34:33 -04:00
|
|
|
|
|
|
|
return db->treename;
|
|
|
|
}
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Get a download URL for the package database. */
|
2007-06-05 17:34:33 -04:00
|
|
|
const char SYMEXPORT *alpm_db_get_url(const pmdb_t *db)
|
|
|
|
{
|
2008-01-22 23:49:45 -05:00
|
|
|
char *url;
|
2007-06-05 17:34:33 -04:00
|
|
|
|
|
|
|
/* Sanity checks */
|
2011-03-20 20:45:57 -04:00
|
|
|
ASSERT(db != NULL, return NULL);
|
|
|
|
ASSERT(db->servers != NULL, return NULL);
|
2007-06-05 17:34:33 -04:00
|
|
|
|
2011-03-25 11:57:20 -04:00
|
|
|
url = (char *)db->servers->data;
|
2007-06-05 17:34:33 -04:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return url;
|
2007-06-05 17:34:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Get a package entry from a package database. */
|
2007-06-03 23:57:38 -04:00
|
|
|
pmpkg_t SYMEXPORT *alpm_db_get_pkg(pmdb_t *db, const char *name)
|
|
|
|
{
|
|
|
|
/* Sanity checks */
|
2011-03-20 20:45:57 -04:00
|
|
|
ASSERT(db != NULL, return NULL);
|
|
|
|
ASSERT(name != NULL && strlen(name) != 0, return NULL);
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return _alpm_db_get_pkgfromcache(db, name);
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Get the package cache of a package database. */
|
2011-02-25 09:22:09 -05:00
|
|
|
alpm_list_t SYMEXPORT *alpm_db_get_pkgcache(pmdb_t *db)
|
2011-01-24 20:49:34 -05:00
|
|
|
{
|
|
|
|
/* Sanity checks */
|
2011-03-20 20:45:57 -04:00
|
|
|
ASSERT(db != NULL, return NULL);
|
2011-01-24 20:49:34 -05:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return _alpm_db_get_pkgcache(db);
|
2011-01-24 20:49:34 -05:00
|
|
|
}
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Get a group entry from a package database. */
|
2007-06-03 23:57:38 -04:00
|
|
|
pmgrp_t SYMEXPORT *alpm_db_readgrp(pmdb_t *db, const char *name)
|
|
|
|
{
|
|
|
|
/* Sanity checks */
|
2011-03-20 20:45:57 -04:00
|
|
|
ASSERT(db != NULL, return NULL);
|
|
|
|
ASSERT(name != NULL && strlen(name) != 0, return NULL);
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return _alpm_db_get_grpfromcache(db, name);
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Get the group cache of a package database. */
|
2008-08-17 13:36:28 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_db_get_grpcache(pmdb_t *db)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
|
|
|
/* Sanity checks */
|
2011-03-20 20:45:57 -04:00
|
|
|
ASSERT(db != NULL, return NULL);
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return _alpm_db_get_grpcache(db);
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Searches a database. */
|
2007-06-05 17:34:33 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_db_search(pmdb_t *db, const alpm_list_t* needles)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
|
|
|
/* Sanity checks */
|
2011-03-20 20:45:57 -04:00
|
|
|
ASSERT(db != NULL, return NULL);
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return _alpm_db_search(db, needles);
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 12:44:03 -05:00
|
|
|
/** Set install reason for a package in db. */
|
2009-05-20 15:46:23 -04:00
|
|
|
int SYMEXPORT alpm_db_set_pkgreason(pmdb_t *db, const char *name, pmpkgreason_t reason)
|
|
|
|
{
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(db != NULL && name != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
|
|
|
|
|
|
|
|
pmpkg_t *pkg = _alpm_db_get_pkgfromcache(db, name);
|
|
|
|
if(pkg == NULL) {
|
|
|
|
RET_ERR(PM_ERR_PKG_NOT_FOUND, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "setting install reason %u for %s/%s\n", reason, db->treename, name);
|
2010-12-13 22:06:42 -05:00
|
|
|
if(alpm_pkg_get_reason(pkg) == reason) {
|
2009-05-20 15:46:23 -04:00
|
|
|
/* we are done */
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2009-05-20 15:46:23 -04:00
|
|
|
}
|
|
|
|
/* set reason (in pkgcache) */
|
|
|
|
pkg->reason = reason;
|
|
|
|
/* write DESC */
|
2010-10-13 04:04:07 -04:00
|
|
|
if(_alpm_local_db_write(db, pkg, INFRQ_DESC)) {
|
2011-04-04 19:38:21 -04:00
|
|
|
RET_ERR(PM_ERR_DB_WRITE, -1);
|
2009-05-20 15:46:23 -04:00
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2009-05-20 15:46:23 -04:00
|
|
|
}
|
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** @} */
|
|
|
|
|
2010-10-13 04:04:07 -04:00
|
|
|
pmdb_t *_alpm_db_new(const char *treename, int is_local)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
pmdb_t *db;
|
|
|
|
|
2007-10-29 02:00:52 -04:00
|
|
|
CALLOC(db, 1, sizeof(pmdb_t), RET_ERR(PM_ERR_MEMORY, NULL));
|
2008-03-26 21:19:44 -04:00
|
|
|
STRDUP(db->treename, treename, RET_ERR(PM_ERR_MEMORY, NULL));
|
2009-09-14 23:44:51 -04:00
|
|
|
db->is_local = is_local;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return db;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-02-28 22:08:41 -05:00
|
|
|
void _alpm_db_free(pmdb_t *db)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2007-08-21 23:29:10 -04:00
|
|
|
/* cleanup pkgcache */
|
|
|
|
_alpm_db_free_pkgcache(db);
|
|
|
|
/* cleanup server list */
|
2008-01-22 23:49:45 -05:00
|
|
|
FREELIST(db->servers);
|
2009-09-14 23:44:51 -04:00
|
|
|
FREE(db->_path);
|
2008-03-26 21:19:44 -04:00
|
|
|
FREE(db->treename);
|
2006-10-31 01:39:59 -05:00
|
|
|
FREE(db);
|
2006-10-15 15:31:03 -04:00
|
|
|
|
|
|
|
return;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2009-09-14 23:44:51 -04:00
|
|
|
const char *_alpm_db_path(pmdb_t *db)
|
|
|
|
{
|
|
|
|
if(!db) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2009-09-14 23:44:51 -04:00
|
|
|
}
|
|
|
|
if(!db->_path) {
|
|
|
|
const char *dbpath;
|
|
|
|
size_t pathsize;
|
|
|
|
|
|
|
|
dbpath = alpm_option_get_dbpath();
|
|
|
|
if(!dbpath) {
|
|
|
|
_alpm_log(PM_LOG_ERROR, _("database path is undefined\n"));
|
|
|
|
RET_ERR(PM_ERR_DB_OPEN, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(db->is_local) {
|
|
|
|
pathsize = strlen(dbpath) + strlen(db->treename) + 2;
|
|
|
|
CALLOC(db->_path, 1, pathsize, RET_ERR(PM_ERR_MEMORY, NULL));
|
|
|
|
sprintf(db->_path, "%s%s/", dbpath, db->treename);
|
|
|
|
} else {
|
2010-10-09 09:31:29 -04:00
|
|
|
pathsize = strlen(dbpath) + 5 + strlen(db->treename) + 4;
|
2009-09-14 23:44:51 -04:00
|
|
|
CALLOC(db->_path, 1, pathsize, RET_ERR(PM_ERR_MEMORY, NULL));
|
|
|
|
/* all sync DBs now reside in the sync/ subdir of the dbpath */
|
2010-10-09 09:31:29 -04:00
|
|
|
sprintf(db->_path, "%ssync/%s.db", dbpath, db->treename);
|
2009-09-14 23:44:51 -04:00
|
|
|
}
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "database path for tree %s set to %s\n",
|
|
|
|
db->treename, db->_path);
|
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return db->_path;
|
2009-09-14 23:44:51 -04:00
|
|
|
}
|
|
|
|
|
2011-02-28 11:46:00 -05:00
|
|
|
int _alpm_db_version(pmdb_t *db)
|
|
|
|
{
|
|
|
|
if(!db) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2011-02-28 11:46:00 -05:00
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return db->ops->version(db);
|
2011-02-28 11:46:00 -05:00
|
|
|
}
|
|
|
|
|
Cleanup usages of alpm_list_find and alpm_list_remove.
* remove obsolete and unused *_cmp helper functions like deppkg_cmp and
_alpm_grp_cmp
* new alpm_list_remove_str function, used 6 times in handle.c
* remove _alpm_prov_cmp / _alpm_db_whatprovides and replace them by
a more general alpm_find_pkg_satisfiers with a cleaner implementation.
before: alpm_db_whatprovides(db, targ)
after: alpm_find_pkg_satisfiers(alpm_db_getpkgcache(db), targ)
* remove satisfycmp and replace alpm_list_find + satisfycmp usage by
_alpm_find_dep_satisfiers.
before : alpm_list_find(_alpm_db_get_pkgcache(db), dep, satisfycmp)
after : _alpm_find_dep_satisfiers(_alpm_db_get_pkgcache(db), dep)
* remove _alpm_pkgname_pkg_cmp, which was used with alpm_list_remove, and
use _alpm_pkg_find + alpm_list_remove with _alpm_pkg_cmp instead.
This commit actually get rids of all complicated and asymmetric _cmp
functions. I first thought these functions were worth it, be caused it
allowed us to reuse list_find and list_remove. But this was at the detriment
of the clarity and also the ease of use of these functions, dangerous
because of their asymmetricity.
Signed-off-by: Chantry Xavier <shiningxc@gmail.com>
Signed-off-by: Dan McGee <dan@archlinux.org>
2008-05-10 12:47:42 -04:00
|
|
|
int _alpm_db_cmp(const void *d1, const void *d2)
|
2006-03-08 13:07:58 -05:00
|
|
|
{
|
2008-08-08 14:45:46 -04:00
|
|
|
pmdb_t *db1 = (pmdb_t *)d1;
|
|
|
|
pmdb_t *db2 = (pmdb_t *)d2;
|
2011-03-20 20:45:57 -04:00
|
|
|
return strcmp(db1->treename, db2->treename);
|
2006-03-08 13:07:58 -05:00
|
|
|
}
|
|
|
|
|
2007-06-05 17:34:33 -04:00
|
|
|
alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles)
|
2006-10-15 15:31:03 -04:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *i, *j, *k;
|
|
|
|
alpm_list_t *ret = NULL;
|
2008-07-24 06:26:09 -04:00
|
|
|
/* copy the pkgcache- we will free the list var after each needle */
|
2011-02-25 09:22:09 -05:00
|
|
|
alpm_list_t *list = alpm_list_copy(_alpm_db_get_pkgcache(db));
|
2006-10-15 15:31:03 -04:00
|
|
|
|
|
|
|
for(i = needles; i; i = i->next) {
|
|
|
|
char *targ;
|
2007-02-26 18:32:08 -05:00
|
|
|
regex_t reg;
|
2006-10-15 15:31:03 -04:00
|
|
|
|
|
|
|
if(i->data == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-24 06:26:09 -04:00
|
|
|
ret = NULL;
|
2007-01-16 22:57:53 -05:00
|
|
|
targ = i->data;
|
2007-08-23 22:26:55 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "searching for target '%s'\n", targ);
|
2007-11-16 21:18:45 -05:00
|
|
|
|
2007-02-26 18:32:08 -05:00
|
|
|
if(regcomp(®, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) {
|
|
|
|
RET_ERR(PM_ERR_INVALID_REGEX, NULL);
|
|
|
|
}
|
2006-10-15 15:31:03 -04:00
|
|
|
|
2008-07-24 06:26:09 -04:00
|
|
|
for(j = list; j; j = j->next) {
|
2006-10-15 15:31:03 -04:00
|
|
|
pmpkg_t *pkg = j->data;
|
2007-03-03 03:13:59 -05:00
|
|
|
const char *matched = NULL;
|
2008-01-11 01:01:58 -05:00
|
|
|
const char *name = alpm_pkg_get_name(pkg);
|
|
|
|
const char *desc = alpm_pkg_get_desc(pkg);
|
2006-10-15 15:31:03 -04:00
|
|
|
|
2008-01-11 01:01:58 -05:00
|
|
|
/* check name as regex AND as plain text */
|
|
|
|
if(name && (regexec(®, name, 0, 0, 0) == 0 || strstr(name, targ))) {
|
|
|
|
matched = name;
|
2007-11-15 01:07:31 -05:00
|
|
|
}
|
2007-01-16 22:57:53 -05:00
|
|
|
/* check desc */
|
2011-04-20 20:45:16 -04:00
|
|
|
else if(desc && regexec(®, desc, 0, 0, 0) == 0) {
|
2008-01-11 01:01:58 -05:00
|
|
|
matched = desc;
|
2006-10-15 15:31:03 -04:00
|
|
|
}
|
2007-02-26 18:32:08 -05:00
|
|
|
/* TODO: should we be doing this, and should we print something
|
|
|
|
* differently when we do match it since it isn't currently printed? */
|
2009-06-06 13:30:30 -04:00
|
|
|
if(!matched) {
|
|
|
|
/* check provides */
|
2007-03-03 03:13:59 -05:00
|
|
|
for(k = alpm_pkg_get_provides(pkg); k; k = k->next) {
|
2011-04-20 20:45:16 -04:00
|
|
|
if(regexec(®, k->data, 0, 0, 0) == 0) {
|
2007-01-16 22:57:53 -05:00
|
|
|
matched = k->data;
|
|
|
|
break;
|
2006-10-15 15:31:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-06 13:30:30 -04:00
|
|
|
if(!matched) {
|
|
|
|
/* check groups */
|
|
|
|
for(k = alpm_pkg_get_groups(pkg); k; k = k->next) {
|
2011-04-20 20:45:16 -04:00
|
|
|
if(regexec(®, k->data, 0, 0, 0) == 0) {
|
2009-06-06 13:30:30 -04:00
|
|
|
matched = k->data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-15 15:31:03 -04:00
|
|
|
|
2007-01-16 22:57:53 -05:00
|
|
|
if(matched != NULL) {
|
2007-08-23 22:26:55 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, " search target '%s' matched '%s'\n",
|
2007-02-26 18:32:08 -05:00
|
|
|
targ, matched);
|
2007-01-19 04:28:44 -05:00
|
|
|
ret = alpm_list_add(ret, pkg);
|
2006-10-15 15:31:03 -04:00
|
|
|
}
|
|
|
|
}
|
2007-02-26 18:32:08 -05:00
|
|
|
|
2008-07-24 06:26:09 -04:00
|
|
|
/* Free the existing search list, and use the returned list for the
|
|
|
|
* next needle. This allows for AND-based package searching. */
|
|
|
|
alpm_list_free(list);
|
|
|
|
list = ret;
|
2007-02-26 18:32:08 -05:00
|
|
|
regfree(®);
|
2006-10-15 15:31:03 -04:00
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2006-10-15 15:31:03 -04:00
|
|
|
}
|
2006-10-20 02:26:55 -04:00
|
|
|
|
2010-10-13 08:56:35 -04:00
|
|
|
/* Returns a new package cache from db.
|
|
|
|
* It frees the cache if it already exists.
|
|
|
|
*/
|
|
|
|
int _alpm_db_load_pkgcache(pmdb_t *db)
|
|
|
|
{
|
|
|
|
if(db == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
_alpm_db_free_pkgcache(db);
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "loading package cache for repository '%s'\n",
|
|
|
|
db->treename);
|
|
|
|
if(db->ops->populate(db) == -1) {
|
|
|
|
_alpm_log(PM_LOG_DEBUG,
|
|
|
|
"failed to load package cache for repository '%s'\n", db->treename);
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
db->pkgcache_loaded = 1;
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void _alpm_db_free_pkgcache(pmdb_t *db)
|
|
|
|
{
|
|
|
|
if(db == NULL || !db->pkgcache_loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "freeing package cache for repository '%s'\n",
|
|
|
|
db->treename);
|
|
|
|
|
2011-02-25 09:22:09 -05:00
|
|
|
alpm_list_free_inner(_alpm_db_get_pkgcache(db),
|
2011-01-24 20:49:34 -05:00
|
|
|
(alpm_list_fn_free)_alpm_pkg_free);
|
|
|
|
_alpm_pkghash_free(db->pkgcache);
|
2010-10-13 08:56:35 -04:00
|
|
|
db->pkgcache_loaded = 0;
|
|
|
|
|
|
|
|
_alpm_db_free_grpcache(db);
|
|
|
|
}
|
|
|
|
|
2011-02-25 09:11:37 -05:00
|
|
|
pmpkghash_t *_alpm_db_get_pkgcache_hash(pmdb_t *db)
|
2010-10-13 08:56:35 -04:00
|
|
|
{
|
|
|
|
if(db == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!db->pkgcache_loaded) {
|
|
|
|
_alpm_db_load_pkgcache(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hmmm, still NULL ?*/
|
|
|
|
if(!db->pkgcache) {
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "warning: pkgcache is NULL for db '%s'\n", db->treename);
|
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return db->pkgcache;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
2011-02-25 09:22:09 -05:00
|
|
|
alpm_list_t *_alpm_db_get_pkgcache(pmdb_t *db)
|
2011-01-24 20:49:34 -05:00
|
|
|
{
|
2011-02-25 09:11:37 -05:00
|
|
|
pmpkghash_t *hash = _alpm_db_get_pkgcache_hash(db);
|
2011-01-24 20:49:34 -05:00
|
|
|
|
|
|
|
if(hash == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2011-01-24 20:49:34 -05:00
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return hash->list;
|
2011-01-24 20:49:34 -05:00
|
|
|
}
|
|
|
|
|
2010-10-13 08:56:35 -04:00
|
|
|
/* "duplicate" pkg then add it to pkgcache */
|
|
|
|
int _alpm_db_add_pkgincache(pmdb_t *db, pmpkg_t *pkg)
|
|
|
|
{
|
|
|
|
pmpkg_t *newpkg;
|
|
|
|
|
|
|
|
if(db == NULL || !db->pkgcache_loaded || pkg == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
newpkg = _alpm_pkg_dup(pkg);
|
|
|
|
if(newpkg == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "adding entry '%s' in '%s' cache\n",
|
|
|
|
alpm_pkg_get_name(newpkg), db->treename);
|
2011-01-24 20:49:34 -05:00
|
|
|
db->pkgcache = _alpm_pkghash_add_sorted(db->pkgcache, newpkg);
|
2010-10-13 08:56:35 -04:00
|
|
|
|
|
|
|
_alpm_db_free_grpcache(db);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int _alpm_db_remove_pkgfromcache(pmdb_t *db, pmpkg_t *pkg)
|
|
|
|
{
|
2011-01-24 20:49:34 -05:00
|
|
|
pmpkg_t *data = NULL;
|
2010-10-13 08:56:35 -04:00
|
|
|
|
|
|
|
if(db == NULL || !db->pkgcache_loaded || pkg == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "removing entry '%s' from '%s' cache\n",
|
|
|
|
alpm_pkg_get_name(pkg), db->treename);
|
|
|
|
|
2011-01-30 07:42:45 -05:00
|
|
|
db->pkgcache = _alpm_pkghash_remove(db->pkgcache, pkg, &data);
|
2010-10-13 08:56:35 -04:00
|
|
|
if(data == NULL) {
|
|
|
|
/* 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);
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_pkg_free(data);
|
|
|
|
|
|
|
|
_alpm_db_free_grpcache(db);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pmpkg_t *_alpm_db_get_pkgfromcache(pmdb_t *db, const char *target)
|
|
|
|
{
|
|
|
|
if(db == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
2011-02-25 09:11:37 -05:00
|
|
|
pmpkghash_t *pkgcache = _alpm_db_get_pkgcache_hash(db);
|
2010-10-13 08:56:35 -04:00
|
|
|
if(!pkgcache) {
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "warning: failed to get '%s' from NULL pkgcache\n",
|
|
|
|
target);
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return _alpm_pkghash_find(pkgcache, target);
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns a new group cache from db.
|
|
|
|
*/
|
|
|
|
int _alpm_db_load_grpcache(pmdb_t *db)
|
|
|
|
{
|
|
|
|
alpm_list_t *lp;
|
|
|
|
|
|
|
|
if(db == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "loading group cache for repository '%s'\n",
|
|
|
|
db->treename);
|
|
|
|
|
2011-02-25 09:22:09 -05:00
|
|
|
for(lp = _alpm_db_get_pkgcache(db); lp; lp = lp->next) {
|
2010-10-13 08:56:35 -04:00
|
|
|
const alpm_list_t *i;
|
|
|
|
pmpkg_t *pkg = lp->data;
|
|
|
|
|
|
|
|
for(i = alpm_pkg_get_groups(pkg); i; i = i->next) {
|
|
|
|
const char *grpname = i->data;
|
|
|
|
alpm_list_t *j;
|
|
|
|
pmgrp_t *grp = NULL;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
/* first look through the group cache for a group with this name */
|
|
|
|
for(j = db->grpcache; j; j = j->next) {
|
|
|
|
grp = j->data;
|
|
|
|
|
|
|
|
if(strcmp(grp->name, grpname) == 0
|
|
|
|
&& !alpm_list_find_ptr(grp->packages, pkg)) {
|
|
|
|
grp->packages = alpm_list_add(grp->packages, pkg);
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(found) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* we didn't find the group, so create a new one with this name */
|
|
|
|
grp = _alpm_grp_new(grpname);
|
|
|
|
grp->packages = alpm_list_add(grp->packages, pkg);
|
|
|
|
db->grpcache = alpm_list_add(db->grpcache, grp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db->grpcache_loaded = 1;
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void _alpm_db_free_grpcache(pmdb_t *db)
|
|
|
|
{
|
|
|
|
alpm_list_t *lg;
|
|
|
|
|
|
|
|
if(db == NULL || !db->grpcache_loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "freeing group cache for repository '%s'\n",
|
|
|
|
db->treename);
|
|
|
|
|
|
|
|
for(lg = db->grpcache; lg; lg = lg->next) {
|
|
|
|
_alpm_grp_free(lg->data);
|
|
|
|
lg->data = NULL;
|
|
|
|
}
|
|
|
|
FREELIST(db->grpcache);
|
|
|
|
db->grpcache_loaded = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
alpm_list_t *_alpm_db_get_grpcache(pmdb_t *db)
|
|
|
|
{
|
|
|
|
if(db == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!db->grpcache_loaded) {
|
|
|
|
_alpm_db_load_grpcache(db);
|
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return db->grpcache;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pmgrp_t *_alpm_db_get_grpfromcache(pmdb_t *db, const char *target)
|
|
|
|
{
|
|
|
|
alpm_list_t *i;
|
|
|
|
|
|
|
|
if(db == NULL || target == NULL || strlen(target) == 0) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for(i = _alpm_db_get_grpcache(db); i; i = i->next) {
|
|
|
|
pmgrp_t *info = i->data;
|
|
|
|
|
|
|
|
if(strcmp(info->name, target) == 0) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return info;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2010-10-13 08:56:35 -04:00
|
|
|
}
|
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|