2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* db.c
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2007-12-10 23:55:22 -05:00
|
|
|
* Copyright (c) 2002-2007 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 <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2007-11-16 12:51:26 -05:00
|
|
|
#include <stdint.h> /* uintmax_t */
|
2005-03-14 20:51:43 -05:00
|
|
|
#include <sys/stat.h>
|
2006-03-02 14:06:52 -05:00
|
|
|
#include <dirent.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"
|
|
|
|
#include "cache.h"
|
2005-03-28 03:21:17 -05:00
|
|
|
#include "alpm.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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Register the local package database.
|
|
|
|
* @return a pmdb_t* representing the local database, or NULL on error
|
|
|
|
*/
|
|
|
|
pmdb_t SYMEXPORT *alpm_db_register_local(void)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, NULL));
|
|
|
|
/* 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_local());
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2007-08-21 23:29:10 -04:00
|
|
|
/* Helper function for alpm_db_unregister{_all} */
|
|
|
|
static void _alpm_db_unregister(pmdb_t *db)
|
|
|
|
{
|
|
|
|
if(db == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-23 22:26:55 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "closing database '%s'\n", db->treename);
|
2007-08-21 23:29:10 -04:00
|
|
|
_alpm_db_close(db);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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 */
|
|
|
|
_alpm_db_unregister(handle->db_local);
|
|
|
|
handle->db_local = NULL;
|
|
|
|
|
|
|
|
/* and also sync ones */
|
|
|
|
for(i = handle->dbs_sync; i; i = i->next) {
|
|
|
|
pmdb_t *db = i->data;
|
|
|
|
_alpm_db_unregister(db);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-07-09 11:02:29 -04:00
|
|
|
_alpm_db_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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(url && strlen(url)) {
|
2008-01-22 23:49:45 -05:00
|
|
|
db->servers = alpm_list_add(db->servers, strdup(url));
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "adding new server URL to database '%s': %s\n",
|
|
|
|
db->treename, url);
|
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));
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
*/
|
|
|
|
alpm_list_t SYMEXPORT *alpm_db_getpkgcache(pmdb_t *db)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(handle != NULL, return(NULL));
|
|
|
|
ASSERT(db != NULL, return(NULL));
|
|
|
|
|
|
|
|
return(_alpm_db_get_pkgcache(db));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
*/
|
|
|
|
alpm_list_t SYMEXPORT *alpm_db_getgrpcache(pmdb_t *db)
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
2007-05-31 02:51:28 -04:00
|
|
|
pmdb_t *_alpm_db_new(const char *dbpath, const char *treename)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
pmdb_t *db;
|
2007-05-31 02:51:28 -04:00
|
|
|
const size_t pathsize = strlen(dbpath) + strlen(treename) + 2;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
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));
|
|
|
|
CALLOC(db->path, 1, pathsize, RET_ERR(PM_ERR_MEMORY, NULL));
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-05-31 02:51:28 -04:00
|
|
|
sprintf(db->path, "%s%s/", dbpath, treename);
|
2008-03-26 21:19:44 -04:00
|
|
|
STRDUP(db->treename, treename, RET_ERR(PM_ERR_MEMORY, NULL));
|
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);
|
2006-10-31 01:39:59 -05: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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
pmdb_t *db1 = (pmdb_t *)db1;
|
|
|
|
pmdb_t *db2 = (pmdb_t *)db2;
|
|
|
|
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;
|
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;
|
|
|
|
}
|
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
|
|
|
|
2007-03-03 03:13:59 -05:00
|
|
|
for(j = _alpm_db_get_pkgcache(db); 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
|
|
|
}
|
|
|
|
/* check provides */
|
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? */
|
2007-01-16 22:57:53 -05:00
|
|
|
else {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
regfree(®);
|
2006-10-15 15:31:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
2006-10-20 02:26:55 -04:00
|
|
|
|
2007-08-26 22:42:17 -04:00
|
|
|
pmdb_t *_alpm_db_register_local(void)
|
2006-10-20 02:26:55 -04:00
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
pmdb_t *db;
|
2007-05-31 02:51:28 -04:00
|
|
|
const char *dbpath;
|
2006-10-20 02:26:55 -04:00
|
|
|
char path[PATH_MAX];
|
|
|
|
|
2007-01-30 03:14:10 -05:00
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
2007-08-26 22:42:17 -04:00
|
|
|
if(handle->db_local != NULL) {
|
|
|
|
_alpm_log(PM_LOG_WARNING, _("attempt to re-register the 'local' DB\n"));
|
|
|
|
RET_ERR(PM_ERR_DB_NOT_NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "registering local database\n");
|
|
|
|
|
|
|
|
/* make sure the database directory exists */
|
|
|
|
dbpath = alpm_option_get_dbpath();
|
|
|
|
if(!dbpath) {
|
2007-11-05 21:57:43 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, _("database path is undefined\n"));
|
2007-08-26 22:42:17 -04:00
|
|
|
RET_ERR(PM_ERR_DB_OPEN, NULL);
|
|
|
|
}
|
|
|
|
snprintf(path, PATH_MAX, "%slocal", dbpath);
|
|
|
|
/* TODO this is rediculous, we try to do this even if we can't */
|
|
|
|
if(stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) {
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
|
|
|
|
path);
|
|
|
|
if(_alpm_makepath(path) != 0) {
|
|
|
|
RET_ERR(PM_ERR_SYSTEM, NULL);
|
2006-10-20 02:26:55 -04:00
|
|
|
}
|
2007-08-26 22:42:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
db = _alpm_db_new(dbpath, "local");
|
|
|
|
if(db == NULL) {
|
|
|
|
RET_ERR(PM_ERR_DB_CREATE, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "opening database '%s'\n", db->treename);
|
|
|
|
if(_alpm_db_open(db) == -1) {
|
|
|
|
_alpm_db_free(db);
|
|
|
|
RET_ERR(PM_ERR_DB_OPEN, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
handle->db_local = db;
|
|
|
|
return(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
pmdb_t *_alpm_db_register_sync(const char *treename)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
pmdb_t *db;
|
|
|
|
const char *dbpath;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
alpm_list_t *i;
|
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
for(i = handle->dbs_sync; i; i = i->next) {
|
|
|
|
pmdb_t *sdb = i->data;
|
|
|
|
if(strcmp(treename, sdb->treename) == 0) {
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "attempt to re-register the '%s' database, using existing\n", sdb->treename);
|
|
|
|
return sdb;
|
2006-10-20 02:26:55 -04:00
|
|
|
}
|
|
|
|
}
|
2007-08-26 22:42:17 -04:00
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "registering sync database '%s'\n", treename);
|
2006-10-20 02:26:55 -04:00
|
|
|
|
|
|
|
/* make sure the database directory exists */
|
2007-05-31 02:51:28 -04:00
|
|
|
dbpath = alpm_option_get_dbpath();
|
2007-06-04 15:39:00 -04:00
|
|
|
if(!dbpath) {
|
2007-11-05 21:57:43 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, _("database path is undefined\n"));
|
2007-06-04 15:39:00 -04:00
|
|
|
RET_ERR(PM_ERR_DB_OPEN, NULL);
|
|
|
|
}
|
2007-08-26 22:42:17 -04:00
|
|
|
/* all sync DBs now reside in the sync/ subdir of the dbpath */
|
|
|
|
snprintf(path, PATH_MAX, "%ssync/%s", dbpath, treename);
|
2007-08-21 21:28:05 -04:00
|
|
|
/* TODO this is rediculous, we try to do this even if we can't */
|
2006-10-20 02:26:55 -04:00
|
|
|
if(stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) {
|
2007-08-23 22:26:55 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
|
2007-05-31 02:51:28 -04:00
|
|
|
path);
|
2006-10-20 02:26:55 -04:00
|
|
|
if(_alpm_makepath(path) != 0) {
|
|
|
|
RET_ERR(PM_ERR_SYSTEM, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-03 21:28:17 -04:00
|
|
|
/* Ensure the db gets the real path. */
|
|
|
|
path[0] = '\0';
|
|
|
|
snprintf(path, PATH_MAX, "%ssync/", dbpath);
|
|
|
|
|
|
|
|
db = _alpm_db_new(path, treename);
|
2006-10-20 02:26:55 -04:00
|
|
|
if(db == NULL) {
|
|
|
|
RET_ERR(PM_ERR_DB_CREATE, NULL);
|
|
|
|
}
|
|
|
|
|
2007-08-23 22:26:55 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "opening database '%s'\n", db->treename);
|
2006-10-20 02:26:55 -04:00
|
|
|
if(_alpm_db_open(db) == -1) {
|
|
|
|
_alpm_db_free(db);
|
|
|
|
RET_ERR(PM_ERR_DB_OPEN, NULL);
|
|
|
|
}
|
|
|
|
|
2007-08-26 22:42:17 -04:00
|
|
|
handle->dbs_sync = alpm_list_add(handle->dbs_sync, db);
|
2006-10-20 02:26:55 -04:00
|
|
|
return(db);
|
|
|
|
}
|
2006-11-20 04:10:23 -05:00
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|