ALPM API adjustments for sanity and consistency
[pacman-ng.git] / lib / libalpm / db.c
blob261c2a45edd2a2703141088e3e504af7ff8d65ad
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 <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <regex.h>
30 /* libalpm */
31 #include "db.h"
32 #include "alpm_list.h"
33 #include "log.h"
34 #include "util.h"
35 #include "handle.h"
36 #include "alpm.h"
37 #include "package.h"
38 #include "group.h"
40 /** \addtogroup alpm_databases Database Functions
41 * @brief Functions to query and manipulate the database of libalpm
42 * @{
45 /** Register a sync database of packages. */
46 alpm_db_t SYMEXPORT *alpm_register_syncdb(alpm_handle_t *handle,
47 const char *treename, alpm_siglevel_t level)
49 /* Sanity checks */
50 CHECK_HANDLE(handle, return NULL);
51 ASSERT(treename != NULL && strlen(treename) != 0,
52 RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL));
53 /* Do not register a database if a transaction is on-going */
54 ASSERT(handle->trans == NULL, RET_ERR(handle, ALPM_ERR_TRANS_NOT_NULL, NULL));
56 return _alpm_db_register_sync(handle, treename, level);
59 /* Helper function for alpm_db_unregister{_all} */
60 void _alpm_db_unregister(alpm_db_t *db)
62 if(db == NULL) {
63 return;
66 _alpm_log(db->handle, ALPM_LOG_DEBUG, "unregistering database '%s'\n", db->treename);
67 _alpm_db_free(db);
70 /** Unregister all package databases. */
71 int SYMEXPORT alpm_unregister_all_syncdbs(alpm_handle_t *handle)
73 alpm_list_t *i;
74 alpm_db_t *db;
76 /* Sanity checks */
77 CHECK_HANDLE(handle, return -1);
78 /* Do not unregister a database if a transaction is on-going */
79 ASSERT(handle->trans == NULL, RET_ERR(handle, ALPM_ERR_TRANS_NOT_NULL, -1));
81 /* unregister all sync dbs */
82 for(i = handle->dbs_sync; i; i = i->next) {
83 db = i->data;
84 db->ops->unregister(db);
85 i->data = NULL;
87 FREELIST(handle->dbs_sync);
88 return 0;
91 /** Unregister a package database. */
92 int SYMEXPORT alpm_db_unregister(alpm_db_t *db)
94 int found = 0;
95 alpm_handle_t *handle;
97 /* Sanity checks */
98 ASSERT(db != NULL, return -1);
99 /* Do not unregister a database if a transaction is on-going */
100 handle = db->handle;
101 handle->pm_errno = 0;
102 ASSERT(handle->trans == NULL, RET_ERR(handle, ALPM_ERR_TRANS_NOT_NULL, -1));
104 if(db == handle->db_local) {
105 handle->db_local = NULL;
106 found = 1;
107 } else {
108 /* Warning : this function shouldn't be used to unregister all sync
109 * databases by walking through the list returned by
110 * alpm_option_get_syncdbs, because the db is removed from that list here.
112 void *data;
113 handle->dbs_sync = alpm_list_remove(handle->dbs_sync,
114 db, _alpm_db_cmp, &data);
115 if(data) {
116 found = 1;
120 if(!found) {
121 RET_ERR(handle, ALPM_ERR_DB_NOT_FOUND, -1);
124 db->ops->unregister(db);
125 return 0;
128 /** Get the serverlist of a database. */
129 alpm_list_t SYMEXPORT *alpm_db_get_servers(const alpm_db_t *db)
131 ASSERT(db != NULL, return NULL);
132 return db->servers;
135 /** Set the serverlist of a database. */
136 int SYMEXPORT alpm_db_set_servers(alpm_db_t *db, alpm_list_t *servers)
138 ASSERT(db != NULL, return -1);
139 if(db->servers) FREELIST(db->servers);
140 db->servers = servers;
141 return 0;
144 static char *sanitize_url(const char *url)
146 char *newurl;
147 size_t len = strlen(url);
149 STRDUP(newurl, url, return NULL);
150 /* strip the trailing slash if one exists */
151 if(newurl[len - 1] == '/') {
152 newurl[len - 1] = '\0';
154 return newurl;
157 /** Add a download server to a database.
158 * @param db database pointer
159 * @param url url of the server
160 * @return 0 on success, -1 on error (pm_errno is set accordingly)
162 int SYMEXPORT alpm_db_add_server(alpm_db_t *db, const char *url)
164 char *newurl;
166 /* Sanity checks */
167 ASSERT(db != NULL, return -1);
168 db->handle->pm_errno = 0;
169 ASSERT(url != NULL && strlen(url) != 0, RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, -1));
171 newurl = sanitize_url(url);
172 if(!newurl) {
173 return -1;
175 db->servers = alpm_list_add(db->servers, newurl);
176 _alpm_log(db->handle, ALPM_LOG_DEBUG, "adding new server URL to database '%s': %s\n",
177 db->treename, newurl);
179 return 0;
182 /** Remove a download server from a database.
183 * @param db database pointer
184 * @param url url of the server
185 * @return 0 on success, 1 on server not present,
186 * -1 on error (pm_errno is set accordingly)
188 int SYMEXPORT alpm_db_remove_server(alpm_db_t *db, const char *url)
190 char *newurl, *vdata = NULL;
192 /* Sanity checks */
193 ASSERT(db != NULL, return -1);
194 db->handle->pm_errno = 0;
195 ASSERT(url != NULL && strlen(url) != 0, RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, -1));
197 newurl = sanitize_url(url);
198 if(!newurl) {
199 return -1;
201 db->servers = alpm_list_remove_str(db->servers, newurl, &vdata);
202 free(newurl);
203 if(vdata) {
204 _alpm_log(db->handle, ALPM_LOG_DEBUG, "removed server URL from database '%s': %s\n",
205 db->treename, newurl);
206 free(vdata);
207 return 0;
210 return 1;
213 /** Get the name of a package database. */
214 const char SYMEXPORT *alpm_db_get_name(const alpm_db_t *db)
216 ASSERT(db != NULL, return NULL);
217 return db->treename;
220 /** Get the signature verification level for a database. */
221 alpm_siglevel_t SYMEXPORT alpm_db_get_siglevel(alpm_db_t *db)
223 ASSERT(db != NULL, return -1);
224 if(db->siglevel & ALPM_SIG_USE_DEFAULT) {
225 return db->handle->siglevel;
226 } else {
227 return db->siglevel;
231 /** Check the validity of a database. */
232 int SYMEXPORT alpm_db_get_valid(alpm_db_t *db)
234 ASSERT(db != NULL, return -1);
235 db->handle->pm_errno = 0;
236 return db->ops->validate(db);
239 /** Get a package entry from a package database. */
240 alpm_pkg_t SYMEXPORT *alpm_db_get_pkg(alpm_db_t *db, const char *name)
242 alpm_pkg_t *pkg;
243 ASSERT(db != NULL, return NULL);
244 db->handle->pm_errno = 0;
245 ASSERT(name != NULL && strlen(name) != 0,
246 RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, NULL));
248 pkg = _alpm_db_get_pkgfromcache(db, name);
249 if(!pkg) {
250 RET_ERR(db->handle, ALPM_ERR_PKG_NOT_FOUND, NULL);
252 return pkg;
255 /** Get the package cache of a package database. */
256 alpm_list_t SYMEXPORT *alpm_db_get_pkgcache(alpm_db_t *db)
258 ASSERT(db != NULL, return NULL);
259 db->handle->pm_errno = 0;
260 return _alpm_db_get_pkgcache(db);
263 /** Get a group entry from a package database. */
264 alpm_group_t SYMEXPORT *alpm_db_get_group(alpm_db_t *db, const char *name)
266 ASSERT(db != NULL, return NULL);
267 db->handle->pm_errno = 0;
268 ASSERT(name != NULL && strlen(name) != 0,
269 RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, NULL));
271 return _alpm_db_get_groupfromcache(db, name);
274 /** Get the group cache of a package database. */
275 alpm_list_t SYMEXPORT *alpm_db_get_groupcache(alpm_db_t *db)
277 ASSERT(db != NULL, return NULL);
278 db->handle->pm_errno = 0;
280 return _alpm_db_get_groupcache(db);
283 /** Searches a database. */
284 alpm_list_t SYMEXPORT *alpm_db_search(alpm_db_t *db, const alpm_list_t* needles)
286 ASSERT(db != NULL, return NULL);
287 db->handle->pm_errno = 0;
289 return _alpm_db_search(db, needles);
293 /** @} */
295 alpm_db_t *_alpm_db_new(const char *treename, int is_local)
297 alpm_db_t *db;
299 CALLOC(db, 1, sizeof(alpm_db_t), return NULL);
300 STRDUP(db->treename, treename, return NULL);
301 if(is_local) {
302 db->status |= DB_STATUS_LOCAL;
303 } else {
304 db->status &= ~DB_STATUS_LOCAL;
307 return db;
310 void _alpm_db_free(alpm_db_t *db)
312 /* cleanup pkgcache */
313 _alpm_db_free_pkgcache(db);
314 /* cleanup server list */
315 FREELIST(db->servers);
316 FREE(db->_path);
317 FREE(db->treename);
318 FREE(db);
320 return;
323 const char *_alpm_db_path(alpm_db_t *db)
325 if(!db) {
326 return NULL;
328 if(!db->_path) {
329 const char *dbpath;
330 size_t pathsize;
332 dbpath = db->handle->dbpath;
333 if(!dbpath) {
334 _alpm_log(db->handle, ALPM_LOG_ERROR, _("database path is undefined\n"));
335 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, NULL);
338 if(db->status & DB_STATUS_LOCAL) {
339 pathsize = strlen(dbpath) + strlen(db->treename) + 2;
340 CALLOC(db->_path, 1, pathsize, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
341 sprintf(db->_path, "%s%s/", dbpath, db->treename);
342 } else {
343 pathsize = strlen(dbpath) + 5 + strlen(db->treename) + 4;
344 CALLOC(db->_path, 1, pathsize, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
345 /* all sync DBs now reside in the sync/ subdir of the dbpath */
346 sprintf(db->_path, "%ssync/%s.db", dbpath, db->treename);
348 _alpm_log(db->handle, ALPM_LOG_DEBUG, "database path for tree %s set to %s\n",
349 db->treename, db->_path);
351 return db->_path;
354 int _alpm_db_cmp(const void *d1, const void *d2)
356 const alpm_db_t *db1 = d1;
357 const alpm_db_t *db2 = d2;
358 return strcmp(db1->treename, db2->treename);
361 alpm_list_t *_alpm_db_search(alpm_db_t *db, const alpm_list_t *needles)
363 const alpm_list_t *i, *j, *k;
364 alpm_list_t *ret = NULL;
365 /* copy the pkgcache- we will free the list var after each needle */
366 alpm_list_t *list = alpm_list_copy(_alpm_db_get_pkgcache(db));
368 for(i = needles; i; i = i->next) {
369 char *targ;
370 regex_t reg;
372 if(i->data == NULL) {
373 continue;
375 ret = NULL;
376 targ = i->data;
377 _alpm_log(db->handle, ALPM_LOG_DEBUG, "searching for target '%s'\n", targ);
379 if(regcomp(&reg, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) {
380 RET_ERR(db->handle, ALPM_ERR_INVALID_REGEX, NULL);
383 for(j = list; j; j = j->next) {
384 alpm_pkg_t *pkg = j->data;
385 const char *matched = NULL;
386 const char *name = pkg->name;
387 const char *desc = alpm_pkg_get_desc(pkg);
389 /* check name as regex AND as plain text */
390 if(name && (regexec(&reg, name, 0, 0, 0) == 0 || strstr(name, targ))) {
391 matched = name;
393 /* check desc */
394 else if(desc && regexec(&reg, desc, 0, 0, 0) == 0) {
395 matched = desc;
397 /* TODO: should we be doing this, and should we print something
398 * differently when we do match it since it isn't currently printed? */
399 if(!matched) {
400 /* check provides */
401 for(k = alpm_pkg_get_provides(pkg); k; k = k->next) {
402 alpm_depend_t *provide = k->data;
403 if(regexec(&reg, provide->name, 0, 0, 0) == 0) {
404 matched = provide->name;
405 break;
409 if(!matched) {
410 /* check groups */
411 for(k = alpm_pkg_get_groups(pkg); k; k = k->next) {
412 if(regexec(&reg, k->data, 0, 0, 0) == 0) {
413 matched = k->data;
414 break;
419 if(matched != NULL) {
420 _alpm_log(db->handle, ALPM_LOG_DEBUG,
421 "search target '%s' matched '%s'\n", targ, matched);
422 ret = alpm_list_add(ret, pkg);
426 /* Free the existing search list, and use the returned list for the
427 * next needle. This allows for AND-based package searching. */
428 alpm_list_free(list);
429 list = ret;
430 regfree(&reg);
433 return ret;
436 /* Returns a new package cache from db.
437 * It frees the cache if it already exists.
439 static int load_pkgcache(alpm_db_t *db)
441 _alpm_db_free_pkgcache(db);
443 _alpm_log(db->handle, ALPM_LOG_DEBUG, "loading package cache for repository '%s'\n",
444 db->treename);
445 if(db->ops->populate(db) == -1) {
446 _alpm_log(db->handle, ALPM_LOG_DEBUG,
447 "failed to load package cache for repository '%s'\n", db->treename);
448 return -1;
451 db->status |= DB_STATUS_PKGCACHE;
452 return 0;
455 static void free_groupcache(alpm_db_t *db)
457 alpm_list_t *lg;
459 if(db == NULL || !(db->status & DB_STATUS_GRPCACHE)) {
460 return;
463 _alpm_log(db->handle, ALPM_LOG_DEBUG,
464 "freeing group cache for repository '%s'\n", db->treename);
466 for(lg = db->grpcache; lg; lg = lg->next) {
467 _alpm_group_free(lg->data);
468 lg->data = NULL;
470 FREELIST(db->grpcache);
471 db->status &= ~DB_STATUS_GRPCACHE;
474 void _alpm_db_free_pkgcache(alpm_db_t *db)
476 if(db == NULL || !(db->status & DB_STATUS_PKGCACHE)) {
477 return;
480 _alpm_log(db->handle, ALPM_LOG_DEBUG,
481 "freeing package cache for repository '%s'\n", db->treename);
483 if(db->pkgcache) {
484 alpm_list_free_inner(db->pkgcache->list,
485 (alpm_list_fn_free)_alpm_pkg_free);
486 _alpm_pkghash_free(db->pkgcache);
488 db->status &= ~DB_STATUS_PKGCACHE;
490 free_groupcache(db);
493 alpm_pkghash_t *_alpm_db_get_pkgcache_hash(alpm_db_t *db)
495 if(db == NULL) {
496 return NULL;
499 if(!(db->status & DB_STATUS_VALID)) {
500 RET_ERR(db->handle, ALPM_ERR_DB_INVALID, NULL);
503 if(!(db->status & DB_STATUS_PKGCACHE)) {
504 load_pkgcache(db);
507 return db->pkgcache;
510 alpm_list_t *_alpm_db_get_pkgcache(alpm_db_t *db)
512 alpm_pkghash_t *hash = _alpm_db_get_pkgcache_hash(db);
514 if(hash == NULL) {
515 return NULL;
518 return hash->list;
521 /* "duplicate" pkg then add it to pkgcache */
522 int _alpm_db_add_pkgincache(alpm_db_t *db, alpm_pkg_t *pkg)
524 alpm_pkg_t *newpkg;
526 if(db == NULL || pkg == NULL || !(db->status & DB_STATUS_PKGCACHE)) {
527 return -1;
530 if(_alpm_pkg_dup(pkg, &newpkg)) {
531 return -1;
534 _alpm_log(db->handle, ALPM_LOG_DEBUG, "adding entry '%s' in '%s' cache\n",
535 newpkg->name, db->treename);
536 db->pkgcache = _alpm_pkghash_add_sorted(db->pkgcache, newpkg);
538 free_groupcache(db);
540 return 0;
543 int _alpm_db_remove_pkgfromcache(alpm_db_t *db, alpm_pkg_t *pkg)
545 alpm_pkg_t *data = NULL;
547 if(db == NULL || pkg == NULL || !(db->status & DB_STATUS_PKGCACHE)) {
548 return -1;
551 _alpm_log(db->handle, ALPM_LOG_DEBUG, "removing entry '%s' from '%s' cache\n",
552 pkg->name, db->treename);
554 db->pkgcache = _alpm_pkghash_remove(db->pkgcache, pkg, &data);
555 if(data == NULL) {
556 /* package not found */
557 _alpm_log(db->handle, ALPM_LOG_DEBUG, "cannot remove entry '%s' from '%s' cache: not found\n",
558 pkg->name, db->treename);
559 return -1;
562 _alpm_pkg_free(data);
564 free_groupcache(db);
566 return 0;
569 alpm_pkg_t *_alpm_db_get_pkgfromcache(alpm_db_t *db, const char *target)
571 if(db == NULL) {
572 return NULL;
575 alpm_pkghash_t *pkgcache = _alpm_db_get_pkgcache_hash(db);
576 if(!pkgcache) {
577 return NULL;
580 return _alpm_pkghash_find(pkgcache, target);
583 /* Returns a new group cache from db.
585 static int load_grpcache(alpm_db_t *db)
587 alpm_list_t *lp;
589 if(db == NULL) {
590 return -1;
593 _alpm_log(db->handle, ALPM_LOG_DEBUG, "loading group cache for repository '%s'\n",
594 db->treename);
596 for(lp = _alpm_db_get_pkgcache(db); lp; lp = lp->next) {
597 const alpm_list_t *i;
598 alpm_pkg_t *pkg = lp->data;
600 for(i = alpm_pkg_get_groups(pkg); i; i = i->next) {
601 const char *grpname = i->data;
602 alpm_list_t *j;
603 alpm_group_t *grp = NULL;
604 int found = 0;
606 /* first look through the group cache for a group with this name */
607 for(j = db->grpcache; j; j = j->next) {
608 grp = j->data;
610 if(strcmp(grp->name, grpname) == 0
611 && !alpm_list_find_ptr(grp->packages, pkg)) {
612 grp->packages = alpm_list_add(grp->packages, pkg);
613 found = 1;
614 break;
617 if(found) {
618 continue;
620 /* we didn't find the group, so create a new one with this name */
621 grp = _alpm_group_new(grpname);
622 if(!grp) {
623 free_groupcache(db);
624 return -1;
626 grp->packages = alpm_list_add(grp->packages, pkg);
627 db->grpcache = alpm_list_add(db->grpcache, grp);
631 db->status |= DB_STATUS_GRPCACHE;
632 return 0;
635 alpm_list_t *_alpm_db_get_groupcache(alpm_db_t *db)
637 if(db == NULL) {
638 return NULL;
641 if(!(db->status & DB_STATUS_VALID)) {
642 RET_ERR(db->handle, ALPM_ERR_DB_INVALID, NULL);
645 if(!(db->status & DB_STATUS_GRPCACHE)) {
646 load_grpcache(db);
649 return db->grpcache;
652 alpm_group_t *_alpm_db_get_groupfromcache(alpm_db_t *db, const char *target)
654 alpm_list_t *i;
656 if(db == NULL || target == NULL || strlen(target) == 0) {
657 return NULL;
660 for(i = _alpm_db_get_groupcache(db); i; i = i->next) {
661 alpm_group_t *info = i->data;
663 if(strcmp(info->name, target) == 0) {
664 return info;
668 return NULL;
671 /* vim: set ts=2 sw=2 noet: */