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 <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2007-01-16 22:57:53 -05:00
|
|
|
#include <regex.h>
|
2007-11-16 12:51:26 -05:00
|
|
|
#include <time.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
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** \addtogroup alpm_databases Database Functions
|
|
|
|
* @brief Functions to query and manipulate the database of libalpm
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-08-26 22:42:17 -04:00
|
|
|
/** Register a sync database of packages.
|
|
|
|
* @param treename the name of the sync repository
|
2007-06-03 23:57:38 -04:00
|
|
|
* @return a pmdb_t* on success (the value), NULL on error
|
|
|
|
*/
|
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
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
2007-08-26 22:42:17 -04:00
|
|
|
return(_alpm_db_register_sync(treename));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-07-09 11:02:29 -04:00
|
|
|
/** Unregister all package databases
|
|
|
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
|
|
|
*/
|
2007-08-21 21:28:05 -04:00
|
|
|
int SYMEXPORT alpm_db_unregister_all(void)
|
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
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* close local database */
|
2010-07-10 22:06:21 -04:00
|
|
|
db = handle->db_local;
|
|
|
|
if(db) {
|
|
|
|
db->ops->unregister(db);
|
|
|
|
handle->db_local = NULL;
|
|
|
|
}
|
2007-07-09 11:02:29 -04:00
|
|
|
|
|
|
|
/* and also sync ones */
|
|
|
|
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);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** Unregister a package database
|
|
|
|
* @param db pointer to the package database to unregister
|
|
|
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
|
|
|
*/
|
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;
|
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
|
|
|
|
ASSERT(db != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -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-06-03 23:57:38 -04:00
|
|
|
|
|
|
|
if(db == handle->db_local) {
|
|
|
|
handle->db_local = NULL;
|
|
|
|
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;
|
2007-08-21 23:29:10 -04:00
|
|
|
handle->dbs_sync = alpm_list_remove(handle->dbs_sync,
|
|
|
|
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);
|
2007-06-03 23:57:38 -04:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the serverlist of a database.
|
|
|
|
* @param db database pointer
|
|
|
|
* @param url url of the server
|
|
|
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
|
|
|
*/
|
2007-06-04 23:32:38 -04:00
|
|
|
int SYMEXPORT alpm_db_setserver(pmdb_t *db, const char *url)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
2007-08-26 22:42:17 -04:00
|
|
|
alpm_list_t *i;
|
2007-06-03 23:57:38 -04:00
|
|
|
int found = 0;
|
2008-08-03 19:16:08 -04:00
|
|
|
char *newurl;
|
2011-01-07 22:06:06 -05:00
|
|
|
size_t len = 0;
|
2007-06-03 23:57:38 -04:00
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
|
|
|
|
|
2007-08-26 22:42:17 -04:00
|
|
|
for(i = handle->dbs_sync; i && !found; i = i->next) {
|
|
|
|
pmdb_t *sdb = i->data;
|
|
|
|
if(strcmp(db->treename, sdb->treename) == 0) {
|
2007-06-03 23:57:38 -04:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found) {
|
|
|
|
RET_ERR(PM_ERR_DB_NOT_FOUND, -1);
|
|
|
|
}
|
|
|
|
|
2008-08-03 19:16:08 -04:00
|
|
|
if(url) {
|
|
|
|
len = strlen(url);
|
|
|
|
}
|
|
|
|
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);
|
2008-01-22 23:49:45 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "adding new server URL to database '%s': %s\n",
|
2008-08-03 19:16:08 -04:00
|
|
|
db->treename, newurl);
|
2007-06-03 23:57:38 -04:00
|
|
|
} else {
|
|
|
|
FREELIST(db->servers);
|
2007-08-23 22:26:55 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "serverlist flushed for '%s'\n", db->treename);
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2007-06-14 11:12:21 -04:00
|
|
|
/** Get the name of a package database
|
|
|
|
* @param db pointer to the package database
|
|
|
|
* @return the name of the package database, NULL on error
|
|
|
|
*/
|
2007-06-05 17:34:33 -04:00
|
|
|
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));
|
|
|
|
|
|
|
|
return db->treename;
|
|
|
|
}
|
|
|
|
|
2007-06-14 11:12:21 -04:00
|
|
|
/** Get a download URL for the package database
|
|
|
|
* @param db pointer to the package database
|
|
|
|
* @return a fully-specified download URL, NULL on error
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, return(NULL));
|
|
|
|
ASSERT(db != NULL, return(NULL));
|
2009-06-06 12:03:29 -04:00
|
|
|
ASSERT(db->servers != NULL, return(NULL));
|
2007-06-05 17:34:33 -04:00
|
|
|
|
2008-01-22 23:49:45 -05:00
|
|
|
url = (char*)db->servers->data;
|
2007-06-05 17:34:33 -04:00
|
|
|
|
2008-01-22 23:49:45 -05:00
|
|
|
return(url);
|
2007-06-05 17:34:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** Get a package entry from a package database
|
|
|
|
* @param db pointer to the package database to get the package from
|
|
|
|
* @param name of the package
|
|
|
|
* @return the package entry on success, NULL on error
|
|
|
|
*/
|
|
|
|
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));
|
|
|
|
|
|
|
|
return(_alpm_db_get_pkgfromcache(db, name));
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:49:34 -05:00
|
|
|
/** Get the package cache of a package database
|
|
|
|
* @param db pointer to the package database to get the package from
|
|
|
|
* @return the list of packages on success, NULL on error
|
|
|
|
*/
|
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
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, return(NULL));
|
|
|
|
ASSERT(db != NULL, return(NULL));
|
|
|
|
|
2011-02-25 09:22:09 -05:00
|
|
|
return(_alpm_db_get_pkgcache(db));
|
2011-01-24 20:49:34 -05:00
|
|
|
}
|
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** Get a group entry from a package database
|
|
|
|
* @param db pointer to the package database to get the group from
|
|
|
|
* @param name of the group
|
|
|
|
* @return the groups entry on success, NULL on error
|
|
|
|
*/
|
|
|
|
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));
|
|
|
|
|
|
|
|
return(_alpm_db_get_grpfromcache(db, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the group cache of a package database
|
|
|
|
* @param db pointer to the package database to get the group from
|
|
|
|
* @return the list of groups on success, NULL on error
|
|
|
|
*/
|
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
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, return(NULL));
|
|
|
|
ASSERT(db != NULL, return(NULL));
|
|
|
|
|
|
|
|
return(_alpm_db_get_grpcache(db));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Searches a database
|
|
|
|
* @param db pointer to the package database to search in
|
|
|
|
* @param needles the list of strings to search for
|
|
|
|
* @return the list of packages on success, NULL on error
|
|
|
|
*/
|
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
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, return(NULL));
|
|
|
|
ASSERT(db != NULL, return(NULL));
|
|
|
|
|
|
|
|
return(_alpm_db_search(db, needles));
|
|
|
|
}
|
|
|
|
|
2010-08-02 00:40:04 -04:00
|
|
|
/** Set install reason for a package in db
|
2009-05-20 15:46:23 -04:00
|
|
|
* @param db pointer to the package database
|
|
|
|
* @param name the name of the package
|
|
|
|
* @param reason the new install reason
|
|
|
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
|
|
|
*/
|
|
|
|
int SYMEXPORT alpm_db_set_pkgreason(pmdb_t *db, const char *name, pmpkgreason_t reason)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
|
|
|
|
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 */
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
/* 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)) {
|
2009-05-20 15:46:23 -04:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
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-01-30 03:14:10 -05:00
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
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
|
|
|
|
|
|
|
return(db);
|
|
|
|
}
|
|
|
|
|
2007-02-28 22:08:41 -05:00
|
|
|
void _alpm_db_free(pmdb_t *db)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2007-01-30 03:14:10 -05:00
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
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) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return(db->_path);
|
|
|
|
}
|
|
|
|
|
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;
|
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
|
|
|
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
|
|
|
|
2007-01-30 03:14:10 -05:00
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
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 */
|
2008-01-11 01:01:58 -05:00
|
|
|
else if (desc && regexec(®, desc, 0, 0, 0) == 0) {
|
|
|
|
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) {
|
2007-01-16 22:57:53 -05:00
|
|
|
if (regexec(®, k->data, 0, 0, 0) == 0) {
|
|
|
|
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) {
|
|
|
|
if (regexec(®, k->data, 0, 0, 0) == 0) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(db == NULL) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
_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);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
db->pkgcache_loaded = 1;
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _alpm_db_free_pkgcache(pmdb_t *db)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(db == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(db->pkgcache);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
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) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(hash->list);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(db == NULL || !db->pkgcache_loaded || pkg == NULL) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
newpkg = _alpm_pkg_dup(pkg);
|
|
|
|
if(newpkg == NULL) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
_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);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(db == NULL || !db->pkgcache_loaded || pkg == NULL) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
_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);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_pkg_free(data);
|
|
|
|
|
|
|
|
_alpm_db_free_grpcache(db);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pmpkg_t *_alpm_db_get_pkgfromcache(pmdb_t *db, const char *target)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(db == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:49:34 -05: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;
|
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(db == NULL) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
_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;
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _alpm_db_free_grpcache(pmdb_t *db)
|
|
|
|
{
|
|
|
|
alpm_list_t *lg;
|
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(db == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!db->grpcache_loaded) {
|
|
|
|
_alpm_db_load_grpcache(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(db->grpcache);
|
|
|
|
}
|
|
|
|
|
|
|
|
pmgrp_t *_alpm_db_get_grpfromcache(pmdb_t *db, const char *target)
|
|
|
|
{
|
|
|
|
alpm_list_t *i;
|
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(db == NULL || target == NULL || strlen(target) == 0) {
|
|
|
|
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(NULL);
|
|
|
|
}
|
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|