A few final changes for the 3.0.6 release
[pacman.git] / lib / libalpm / db.c
blob148c32dc666c7da3ea5f4e172dc7ce11df3548f8
1 /*
2 * db.c
3 *
4 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
5 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
6 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
7 * Copyright (c) 2006 by David Kimpe <dnaku@frugalware.org>
8 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 * USA.
26 #include "config.h"
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #if defined(__APPLE__) || defined(__OpenBSD__)
35 #include <sys/syslimits.h>
36 #include <sys/stat.h>
37 #endif
38 #include <dirent.h>
39 #include <libintl.h>
40 #include <regex.h>
41 #ifdef CYGWIN
42 #include <limits.h> /* PATH_MAX */
43 #endif
45 /* libalpm */
46 #include "db.h"
47 #include "alpm_list.h"
48 #include "log.h"
49 #include "util.h"
50 #include "error.h"
51 #include "server.h"
52 #include "handle.h"
53 #include "cache.h"
54 #include "alpm.h"
56 pmdb_t *_alpm_db_new(const char *root, const char *dbpath, const char *treename)
58 pmdb_t *db;
60 ALPM_LOG_FUNC;
62 db = calloc(1, sizeof(pmdb_t));
63 if(db == NULL) {
64 _alpm_log(PM_LOG_ERROR, _("malloc failed: could not allocate %d bytes"),
65 sizeof(pmdb_t));
66 RET_ERR(PM_ERR_MEMORY, NULL);
69 db->path = calloc(1, strlen(root)+strlen(dbpath)+strlen(treename)+2);
70 if(db->path == NULL) {
71 _alpm_log(PM_LOG_ERROR, _("malloc failed: could not allocate %d bytes"),
72 strlen(root)+strlen(dbpath)+strlen(treename)+2);
73 FREE(db);
74 RET_ERR(PM_ERR_MEMORY, NULL);
76 sprintf(db->path, "%s%s%s/", root, dbpath, treename);
78 STRNCPY(db->treename, treename, PATH_MAX);
80 return(db);
83 void _alpm_db_free(pmdb_t *db)
85 ALPM_LOG_FUNC;
87 _FREELIST(db->servers, _alpm_server_free);
88 FREE(db->path);
89 FREE(db);
91 return;
94 int _alpm_db_cmp(const void *db1, const void *db2)
96 ALPM_LOG_FUNC;
97 return(strcmp(((pmdb_t *)db1)->treename, ((pmdb_t *)db2)->treename));
100 alpm_list_t *_alpm_db_search(pmdb_t *db, alpm_list_t *needles)
102 alpm_list_t *i, *j, *k, *ret = NULL;
104 ALPM_LOG_FUNC;
106 for(i = needles; i; i = i->next) {
107 char *targ;
108 regex_t reg;
110 if(i->data == NULL) {
111 continue;
113 targ = i->data;
114 _alpm_log(PM_LOG_DEBUG, "searching for target '%s'", targ);
116 if(regcomp(&reg, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) {
117 RET_ERR(PM_ERR_INVALID_REGEX, NULL);
120 for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
121 pmpkg_t *pkg = j->data;
122 const char *matched = NULL;
124 /* check name */
125 if (regexec(&reg, alpm_pkg_get_name(pkg), 0, 0, 0) == 0) {
126 matched = alpm_pkg_get_name(pkg);
128 /* check desc */
129 else if (regexec(&reg, alpm_pkg_get_desc(pkg), 0, 0, 0) == 0) {
130 matched = alpm_pkg_get_desc(pkg);
132 /* check provides */
133 /* TODO: should we be doing this, and should we print something
134 * differently when we do match it since it isn't currently printed? */
135 else {
136 for(k = alpm_pkg_get_provides(pkg); k; k = k->next) {
137 if (regexec(&reg, k->data, 0, 0, 0) == 0) {
138 matched = k->data;
139 break;
144 if(matched != NULL) {
145 _alpm_log(PM_LOG_DEBUG, " search target '%s' matched '%s'",
146 targ, matched);
147 ret = alpm_list_add(ret, pkg);
151 regfree(&reg);
154 return(ret);
157 pmdb_t *_alpm_db_register(const char *treename, alpm_cb_db_register callback)
159 struct stat buf;
160 pmdb_t *db;
161 char path[PATH_MAX];
163 ALPM_LOG_FUNC;
165 if(strcmp(treename, "local") == 0) {
166 if(handle->db_local != NULL) {
167 _alpm_log(PM_LOG_WARNING, _("attempt to re-register the 'local' DB"));
168 RET_ERR(PM_ERR_DB_NOT_NULL, NULL);
170 } else {
171 alpm_list_t *i;
172 for(i = handle->dbs_sync; i; i = i->next) {
173 pmdb_t *sdb = i->data;
174 if(strcmp(treename, sdb->treename) == 0) {
175 _alpm_log(PM_LOG_DEBUG, _("attempt to re-register the '%s' database, using existing"), sdb->treename);
176 return sdb;
181 _alpm_log(PM_LOG_DEBUG, _("registering database '%s'"), treename);
183 /* make sure the database directory exists */
184 snprintf(path, PATH_MAX, "%s%s/%s", handle->root, handle->dbpath, treename);
185 if(stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) {
186 _alpm_log(PM_LOG_DEBUG, _("database directory '%s' does not exist, creating it"), path);
187 if(_alpm_makepath(path) != 0) {
188 RET_ERR(PM_ERR_SYSTEM, NULL);
192 db = _alpm_db_new(handle->root, handle->dbpath, treename);
193 if(db == NULL) {
194 RET_ERR(PM_ERR_DB_CREATE, NULL);
197 _alpm_log(PM_LOG_DEBUG, _("opening database '%s'"), db->treename);
198 if(_alpm_db_open(db) == -1) {
199 _alpm_db_free(db);
200 RET_ERR(PM_ERR_DB_OPEN, NULL);
203 /* Only call callback on NEW registration. */
204 if(callback) callback(treename, db);
206 if(strcmp(treename, "local") == 0) {
207 handle->db_local = db;
208 } else {
209 handle->dbs_sync = alpm_list_add(handle->dbs_sync, db);
212 return(db);
215 const char SYMEXPORT *alpm_db_get_name(pmdb_t *db)
217 ALPM_LOG_FUNC;
219 /* Sanity checks */
220 ASSERT(handle != NULL, return(NULL));
221 ASSERT(db != NULL, return(NULL));
223 return db->treename;
226 const char *alpm_db_get_url(pmdb_t *db)
228 char path[PATH_MAX];
229 pmserver_t *s;
231 ALPM_LOG_FUNC;
233 /* Sanity checks */
234 ASSERT(handle != NULL, return(NULL));
235 ASSERT(db != NULL, return(NULL));
237 s = (pmserver_t*)db->servers->data;
239 snprintf(path, PATH_MAX, "%s://%s%s", s->s_url->scheme, s->s_url->host, s->s_url->doc);
240 return strdup(path);
243 /* vim: set ts=2 sw=2 noet: */