Fix bogus string cast in search debug message
[pacman-ng.git] / lib / libalpm / db.c
blob8688a3cdbb4e8d744c773a6965a35ef3958404e7
1 /*
2 * db.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
8 * Copyright (c) 2006 by David Kimpe <dnaku@frugalware.org>
9 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "config.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <regex.h>
32 /* libalpm */
33 #include "db.h"
34 #include "alpm_list.h"
35 #include "log.h"
36 #include "util.h"
37 #include "handle.h"
38 #include "alpm.h"
39 #include "package.h"
40 #include "group.h"
42 /** \addtogroup alpm_databases Database Functions
43 * @brief Functions to query and manipulate the database of libalpm
44 * @{
47 /** Register a sync database of packages. */
48 alpm_db_t SYMEXPORT *alpm_db_register_sync(alpm_handle_t *handle,
49 const char *treename, alpm_siglevel_t level)
51 /* Sanity checks */
52 CHECK_HANDLE(handle, return NULL);
53 ASSERT(treename != NULL && strlen(treename) != 0,
54 RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL));
55 /* Do not register a database if a transaction is on-going */
56 ASSERT(handle->trans == NULL, RET_ERR(handle, ALPM_ERR_TRANS_NOT_NULL, NULL));
58 return _alpm_db_register_sync(handle, treename, level);
61 /* Helper function for alpm_db_unregister{_all} */
62 void _alpm_db_unregister(alpm_db_t *db)
64 if(db == NULL) {
65 return;
68 _alpm_log(db->handle, ALPM_LOG_DEBUG, "unregistering database '%s'\n", db->treename);
69 _alpm_db_free(db);
72 /** Unregister all package databases. */
73 int SYMEXPORT alpm_db_unregister_all(alpm_handle_t *handle)
75 alpm_list_t *i;
76 alpm_db_t *db;
78 /* Sanity checks */
79 CHECK_HANDLE(handle, return -1);
80 /* Do not unregister a database if a transaction is on-going */
81 ASSERT(handle->trans == NULL, RET_ERR(handle, ALPM_ERR_TRANS_NOT_NULL, -1));
83 /* unregister all sync dbs */
84 for(i = handle->dbs_sync; i; i = i->next) {
85 db = i->data;
86 db->ops->unregister(db);
87 i->data = NULL;
89 FREELIST(handle->dbs_sync);
90 return 0;
93 /** Unregister a package database. */
94 int SYMEXPORT alpm_db_unregister(alpm_db_t *db)
96 int found = 0;
97 alpm_handle_t *handle;
99 /* Sanity checks */
100 ASSERT(db != NULL, return -1);
101 /* Do not unregister a database if a transaction is on-going */
102 handle = db->handle;
103 handle->pm_errno = 0;
104 ASSERT(handle->trans == NULL, RET_ERR(handle, ALPM_ERR_TRANS_NOT_NULL, -1));
106 if(db == handle->db_local) {
107 handle->db_local = NULL;
108 found = 1;
109 } else {
110 /* Warning : this function shouldn't be used to unregister all sync
111 * databases by walking through the list returned by
112 * alpm_option_get_syncdbs, because the db is removed from that list here.
114 void *data;
115 handle->dbs_sync = alpm_list_remove(handle->dbs_sync,
116 db, _alpm_db_cmp, &data);
117 if(data) {
118 found = 1;
122 if(!found) {
123 RET_ERR(handle, ALPM_ERR_DB_NOT_FOUND, -1);
126 db->ops->unregister(db);
127 return 0;
130 /** Get the serverlist of a database. */
131 alpm_list_t SYMEXPORT *alpm_db_get_servers(const alpm_db_t *db)
133 ASSERT(db != NULL, return NULL);
134 return db->servers;
137 /** Set the serverlist of a database. */
138 int SYMEXPORT alpm_db_set_servers(alpm_db_t *db, alpm_list_t *servers)
140 ASSERT(db != NULL, return -1);
141 if(db->servers) FREELIST(db->servers);
142 db->servers = servers;
143 return 0;
146 static char *sanitize_url(const char *url)
148 char *newurl;
149 size_t len = strlen(url);
151 STRDUP(newurl, url, return NULL);
152 /* strip the trailing slash if one exists */
153 if(newurl[len - 1] == '/') {
154 newurl[len - 1] = '\0';
156 return newurl;
159 /** Add a download server to a database.
160 * @param db database pointer
161 * @param url url of the server
162 * @return 0 on success, -1 on error (pm_errno is set accordingly)
164 int SYMEXPORT alpm_db_add_server(alpm_db_t *db, const char *url)
166 char *newurl;
168 /* Sanity checks */
169 ASSERT(db != NULL, return -1);
170 db->handle->pm_errno = 0;
171 ASSERT(url != NULL && strlen(url) != 0, RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, -1));
173 newurl = sanitize_url(url);
174 if(!newurl) {
175 return -1;
177 db->servers = alpm_list_add(db->servers, newurl);
178 _alpm_log(db->handle, ALPM_LOG_DEBUG, "adding new server URL to database '%s': %s\n",
179 db->treename, newurl);
181 return 0;
184 /** Remove a download server from a database.
185 * @param db database pointer
186 * @param url url of the server
187 * @return 0 on success, 1 on server not present,
188 * -1 on error (pm_errno is set accordingly)
190 int SYMEXPORT alpm_db_remove_server(alpm_db_t *db, const char *url)
192 char *newurl, *vdata = NULL;
194 /* Sanity checks */
195 ASSERT(db != NULL, return -1);
196 db->handle->pm_errno = 0;
197 ASSERT(url != NULL && strlen(url) != 0, RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, -1));
199 newurl = sanitize_url(url);
200 if(!newurl) {
201 return -1;
203 db->servers = alpm_list_remove_str(db->servers, newurl, &vdata);
204 free(newurl);
205 if(vdata) {
206 _alpm_log(db->handle, ALPM_LOG_DEBUG, "removed server URL from database '%s': %s\n",
207 db->treename, newurl);
208 free(vdata);
209 return 0;
212 return 1;
215 /** Get the name of a package database. */
216 const char SYMEXPORT *alpm_db_get_name(const alpm_db_t *db)
218 ASSERT(db != NULL, return NULL);
219 return db->treename;
222 /** Get the signature verification level for a database. */
223 alpm_siglevel_t SYMEXPORT alpm_db_get_siglevel(alpm_db_t *db)
225 ASSERT(db != NULL, return -1);
226 if(db->siglevel & ALPM_SIG_USE_DEFAULT) {
227 return db->handle->siglevel;
228 } else {
229 return db->siglevel;
233 /** Check the validity of a database. */
234 int SYMEXPORT alpm_db_get_valid(alpm_db_t *db)
236 ASSERT(db != NULL, return -1);
237 db->handle->pm_errno = 0;
238 return db->ops->validate(db);
241 /** Get a package entry from a package database. */
242 alpm_pkg_t SYMEXPORT *alpm_db_get_pkg(alpm_db_t *db, const char *name)
244 alpm_pkg_t *pkg;
245 ASSERT(db != NULL, return NULL);
246 db->handle->pm_errno = 0;
247 ASSERT(name != NULL && strlen(name) != 0,
248 RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, NULL));
250 pkg = _alpm_db_get_pkgfromcache(db, name);
251 if(!pkg) {
252 RET_ERR(db->handle, ALPM_ERR_PKG_NOT_FOUND, NULL);
254 return pkg;
257 /** Get the package cache of a package database. */
258 alpm_list_t SYMEXPORT *alpm_db_get_pkgcache(alpm_db_t *db)
260 ASSERT(db != NULL, return NULL);
261 db->handle->pm_errno = 0;
262 return _alpm_db_get_pkgcache(db);
265 /** Get a group entry from a package database. */
266 alpm_group_t SYMEXPORT *alpm_db_readgroup(alpm_db_t *db, const char *name)
268 ASSERT(db != NULL, return NULL);
269 db->handle->pm_errno = 0;
270 ASSERT(name != NULL && strlen(name) != 0,
271 RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, NULL));
273 return _alpm_db_get_groupfromcache(db, name);
276 /** Get the group cache of a package database. */
277 alpm_list_t SYMEXPORT *alpm_db_get_groupcache(alpm_db_t *db)
279 ASSERT(db != NULL, return NULL);
280 db->handle->pm_errno = 0;
282 return _alpm_db_get_groupcache(db);
285 /** Searches a database. */
286 alpm_list_t SYMEXPORT *alpm_db_search(alpm_db_t *db, const alpm_list_t* needles)
288 ASSERT(db != NULL, return NULL);
289 db->handle->pm_errno = 0;
291 return _alpm_db_search(db, needles);
294 /** Set install reason for a package in db. */
295 int SYMEXPORT alpm_db_set_pkgreason(alpm_handle_t *handle, alpm_pkg_t *pkg,
296 alpm_pkgreason_t reason)
298 CHECK_HANDLE(handle, return -1);
299 ASSERT(pkg != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
300 ASSERT(pkg->origin == PKG_FROM_LOCALDB,
301 RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
302 ASSERT(pkg->origin_data.db == handle->db_local,
303 RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
305 _alpm_log(handle, ALPM_LOG_DEBUG,
306 "setting install reason %u for %s\n", reason, pkg->name);
307 if(alpm_pkg_get_reason(pkg) == reason) {
308 /* we are done */
309 return 0;
311 /* set reason (in pkgcache) */
312 pkg->reason = reason;
313 /* write DESC */
314 if(_alpm_local_db_write(handle->db_local, pkg, INFRQ_DESC)) {
315 RET_ERR(handle, ALPM_ERR_DB_WRITE, -1);
318 return 0;
321 /** @} */
323 alpm_db_t *_alpm_db_new(const char *treename, int is_local)
325 alpm_db_t *db;
327 CALLOC(db, 1, sizeof(alpm_db_t), return NULL);
328 STRDUP(db->treename, treename, return NULL);
329 if(is_local) {
330 db->status |= DB_STATUS_LOCAL;
331 } else {
332 db->status &= ~DB_STATUS_LOCAL;
335 return db;
338 void _alpm_db_free(alpm_db_t *db)
340 /* cleanup pkgcache */
341 _alpm_db_free_pkgcache(db);
342 /* cleanup server list */
343 FREELIST(db->servers);
344 FREE(db->_path);
345 FREE(db->treename);
346 FREE(db);
348 return;
351 const char *_alpm_db_path(alpm_db_t *db)
353 if(!db) {
354 return NULL;
356 if(!db->_path) {
357 const char *dbpath;
358 size_t pathsize;
360 dbpath = db->handle->dbpath;
361 if(!dbpath) {
362 _alpm_log(db->handle, ALPM_LOG_ERROR, _("database path is undefined\n"));
363 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, NULL);
366 if(db->status & DB_STATUS_LOCAL) {
367 pathsize = strlen(dbpath) + strlen(db->treename) + 2;
368 CALLOC(db->_path, 1, pathsize, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
369 sprintf(db->_path, "%s%s/", dbpath, db->treename);
370 } else {
371 pathsize = strlen(dbpath) + 5 + strlen(db->treename) + 4;
372 CALLOC(db->_path, 1, pathsize, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
373 /* all sync DBs now reside in the sync/ subdir of the dbpath */
374 sprintf(db->_path, "%ssync/%s.db", dbpath, db->treename);
376 _alpm_log(db->handle, ALPM_LOG_DEBUG, "database path for tree %s set to %s\n",
377 db->treename, db->_path);
379 return db->_path;
382 int _alpm_db_cmp(const void *d1, const void *d2)
384 const alpm_db_t *db1 = d1;
385 const alpm_db_t *db2 = d2;
386 return strcmp(db1->treename, db2->treename);
389 alpm_list_t *_alpm_db_search(alpm_db_t *db, const alpm_list_t *needles)
391 const alpm_list_t *i, *j, *k;
392 alpm_list_t *ret = NULL;
393 /* copy the pkgcache- we will free the list var after each needle */
394 alpm_list_t *list = alpm_list_copy(_alpm_db_get_pkgcache(db));
396 for(i = needles; i; i = i->next) {
397 char *targ;
398 regex_t reg;
400 if(i->data == NULL) {
401 continue;
403 ret = NULL;
404 targ = i->data;
405 _alpm_log(db->handle, ALPM_LOG_DEBUG, "searching for target '%s'\n", targ);
407 if(regcomp(&reg, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) {
408 RET_ERR(db->handle, ALPM_ERR_INVALID_REGEX, NULL);
411 for(j = list; j; j = j->next) {
412 alpm_pkg_t *pkg = j->data;
413 const char *matched = NULL;
414 const char *name = pkg->name;
415 const char *desc = alpm_pkg_get_desc(pkg);
417 /* check name as regex AND as plain text */
418 if(name && (regexec(&reg, name, 0, 0, 0) == 0 || strstr(name, targ))) {
419 matched = name;
421 /* check desc */
422 else if(desc && regexec(&reg, desc, 0, 0, 0) == 0) {
423 matched = desc;
425 /* TODO: should we be doing this, and should we print something
426 * differently when we do match it since it isn't currently printed? */
427 if(!matched) {
428 /* check provides */
429 for(k = alpm_pkg_get_provides(pkg); k; k = k->next) {
430 alpm_depend_t *provide = k->data;
431 if(regexec(&reg, provide->name, 0, 0, 0) == 0) {
432 matched = provide->name;
433 break;
437 if(!matched) {
438 /* check groups */
439 for(k = alpm_pkg_get_groups(pkg); k; k = k->next) {
440 if(regexec(&reg, k->data, 0, 0, 0) == 0) {
441 matched = k->data;
442 break;
447 if(matched != NULL) {
448 _alpm_log(db->handle, ALPM_LOG_DEBUG,
449 "search target '%s' matched '%s'\n", targ, matched);
450 ret = alpm_list_add(ret, pkg);
454 /* Free the existing search list, and use the returned list for the
455 * next needle. This allows for AND-based package searching. */
456 alpm_list_free(list);
457 list = ret;
458 regfree(&reg);
461 return ret;
464 /* Returns a new package cache from db.
465 * It frees the cache if it already exists.
467 static int load_pkgcache(alpm_db_t *db)
469 _alpm_db_free_pkgcache(db);
471 _alpm_log(db->handle, ALPM_LOG_DEBUG, "loading package cache for repository '%s'\n",
472 db->treename);
473 if(db->ops->populate(db) == -1) {
474 _alpm_log(db->handle, ALPM_LOG_DEBUG,
475 "failed to load package cache for repository '%s'\n", db->treename);
476 return -1;
479 db->status |= DB_STATUS_PKGCACHE;
480 return 0;
483 static void free_groupcache(alpm_db_t *db)
485 alpm_list_t *lg;
487 if(db == NULL || !(db->status & DB_STATUS_GRPCACHE)) {
488 return;
491 _alpm_log(db->handle, ALPM_LOG_DEBUG,
492 "freeing group cache for repository '%s'\n", db->treename);
494 for(lg = db->grpcache; lg; lg = lg->next) {
495 _alpm_group_free(lg->data);
496 lg->data = NULL;
498 FREELIST(db->grpcache);
499 db->status &= ~DB_STATUS_GRPCACHE;
502 void _alpm_db_free_pkgcache(alpm_db_t *db)
504 if(db == NULL || !(db->status & DB_STATUS_PKGCACHE)) {
505 return;
508 _alpm_log(db->handle, ALPM_LOG_DEBUG,
509 "freeing package cache for repository '%s'\n", db->treename);
511 if(db->pkgcache) {
512 alpm_list_free_inner(db->pkgcache->list,
513 (alpm_list_fn_free)_alpm_pkg_free);
514 _alpm_pkghash_free(db->pkgcache);
516 db->status &= ~DB_STATUS_PKGCACHE;
518 free_groupcache(db);
521 alpm_pkghash_t *_alpm_db_get_pkgcache_hash(alpm_db_t *db)
523 if(db == NULL) {
524 return NULL;
527 if(!(db->status & DB_STATUS_VALID)) {
528 RET_ERR(db->handle, ALPM_ERR_DB_INVALID, NULL);
531 if(!(db->status & DB_STATUS_PKGCACHE)) {
532 load_pkgcache(db);
535 return db->pkgcache;
538 alpm_list_t *_alpm_db_get_pkgcache(alpm_db_t *db)
540 alpm_pkghash_t *hash = _alpm_db_get_pkgcache_hash(db);
542 if(hash == NULL) {
543 return NULL;
546 return hash->list;
549 /* "duplicate" pkg then add it to pkgcache */
550 int _alpm_db_add_pkgincache(alpm_db_t *db, alpm_pkg_t *pkg)
552 alpm_pkg_t *newpkg;
554 if(db == NULL || pkg == NULL || !(db->status & DB_STATUS_PKGCACHE)) {
555 return -1;
558 if(_alpm_pkg_dup(pkg, &newpkg)) {
559 return -1;
562 _alpm_log(db->handle, ALPM_LOG_DEBUG, "adding entry '%s' in '%s' cache\n",
563 newpkg->name, db->treename);
564 db->pkgcache = _alpm_pkghash_add_sorted(db->pkgcache, newpkg);
566 free_groupcache(db);
568 return 0;
571 int _alpm_db_remove_pkgfromcache(alpm_db_t *db, alpm_pkg_t *pkg)
573 alpm_pkg_t *data = NULL;
575 if(db == NULL || pkg == NULL || !(db->status & DB_STATUS_PKGCACHE)) {
576 return -1;
579 _alpm_log(db->handle, ALPM_LOG_DEBUG, "removing entry '%s' from '%s' cache\n",
580 pkg->name, db->treename);
582 db->pkgcache = _alpm_pkghash_remove(db->pkgcache, pkg, &data);
583 if(data == NULL) {
584 /* package not found */
585 _alpm_log(db->handle, ALPM_LOG_DEBUG, "cannot remove entry '%s' from '%s' cache: not found\n",
586 pkg->name, db->treename);
587 return -1;
590 _alpm_pkg_free(data);
592 free_groupcache(db);
594 return 0;
597 alpm_pkg_t *_alpm_db_get_pkgfromcache(alpm_db_t *db, const char *target)
599 if(db == NULL) {
600 return NULL;
603 alpm_pkghash_t *pkgcache = _alpm_db_get_pkgcache_hash(db);
604 if(!pkgcache) {
605 return NULL;
608 return _alpm_pkghash_find(pkgcache, target);
611 /* Returns a new group cache from db.
613 static int load_grpcache(alpm_db_t *db)
615 alpm_list_t *lp;
617 if(db == NULL) {
618 return -1;
621 _alpm_log(db->handle, ALPM_LOG_DEBUG, "loading group cache for repository '%s'\n",
622 db->treename);
624 for(lp = _alpm_db_get_pkgcache(db); lp; lp = lp->next) {
625 const alpm_list_t *i;
626 alpm_pkg_t *pkg = lp->data;
628 for(i = alpm_pkg_get_groups(pkg); i; i = i->next) {
629 const char *grpname = i->data;
630 alpm_list_t *j;
631 alpm_group_t *grp = NULL;
632 int found = 0;
634 /* first look through the group cache for a group with this name */
635 for(j = db->grpcache; j; j = j->next) {
636 grp = j->data;
638 if(strcmp(grp->name, grpname) == 0
639 && !alpm_list_find_ptr(grp->packages, pkg)) {
640 grp->packages = alpm_list_add(grp->packages, pkg);
641 found = 1;
642 break;
645 if(found) {
646 continue;
648 /* we didn't find the group, so create a new one with this name */
649 grp = _alpm_group_new(grpname);
650 if(!grp) {
651 free_groupcache(db);
652 return -1;
654 grp->packages = alpm_list_add(grp->packages, pkg);
655 db->grpcache = alpm_list_add(db->grpcache, grp);
659 db->status |= DB_STATUS_GRPCACHE;
660 return 0;
663 alpm_list_t *_alpm_db_get_groupcache(alpm_db_t *db)
665 if(db == NULL) {
666 return NULL;
669 if(!(db->status & DB_STATUS_VALID)) {
670 RET_ERR(db->handle, ALPM_ERR_DB_INVALID, NULL);
673 if(!(db->status & DB_STATUS_GRPCACHE)) {
674 load_grpcache(db);
677 return db->grpcache;
680 alpm_group_t *_alpm_db_get_groupfromcache(alpm_db_t *db, const char *target)
682 alpm_list_t *i;
684 if(db == NULL || target == NULL || strlen(target) == 0) {
685 return NULL;
688 for(i = _alpm_db_get_groupcache(db); i; i = i->next) {
689 alpm_group_t *info = i->data;
691 if(strcmp(info->name, target) == 0) {
692 return info;
696 return NULL;
699 /* vim: set ts=2 sw=2 noet: */