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/>.
34 #include "alpm_list.h"
42 /** \addtogroup alpm_databases Database Functions
43 * @brief Functions to query and manipulate the database of libalpm
47 /** Register a sync database of packages. */
48 alpm_db_t SYMEXPORT
*alpm_db_register_sync(alpm_handle_t
*handle
, const char *treename
,
49 pgp_verify_t check_sig
)
52 CHECK_HANDLE(handle
, return NULL
);
53 ASSERT(treename
!= NULL
&& strlen(treename
) != 0,
54 RET_ERR(handle
, PM_ERR_WRONG_ARGS
, NULL
));
55 /* Do not register a database if a transaction is on-going */
56 ASSERT(handle
->trans
== NULL
, RET_ERR(handle
, PM_ERR_TRANS_NOT_NULL
, NULL
));
58 return _alpm_db_register_sync(handle
, treename
, check_sig
);
61 /* Helper function for alpm_db_unregister{_all} */
62 void _alpm_db_unregister(alpm_db_t
*db
)
68 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "unregistering database '%s'\n", db
->treename
);
72 /** Unregister all package databases. */
73 int SYMEXPORT
alpm_db_unregister_all(alpm_handle_t
*handle
)
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
, PM_ERR_TRANS_NOT_NULL
, -1));
83 /* unregister all sync dbs */
84 for(i
= handle
->dbs_sync
; i
; i
= i
->next
) {
86 db
->ops
->unregister(db
);
89 FREELIST(handle
->dbs_sync
);
93 /** Unregister a package database. */
94 int SYMEXPORT
alpm_db_unregister(alpm_db_t
*db
)
97 alpm_handle_t
*handle
;
100 ASSERT(db
!= NULL
, return -1);
101 /* Do not unregister a database if a transaction is on-going */
103 handle
->pm_errno
= 0;
104 ASSERT(handle
->trans
== NULL
, RET_ERR(handle
, PM_ERR_TRANS_NOT_NULL
, -1));
106 if(db
== handle
->db_local
) {
107 handle
->db_local
= NULL
;
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.
115 handle
->dbs_sync
= alpm_list_remove(handle
->dbs_sync
,
116 db
, _alpm_db_cmp
, &data
);
123 RET_ERR(handle
, PM_ERR_DB_NOT_FOUND
, -1);
126 db
->ops
->unregister(db
);
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
);
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
;
146 static char *sanitize_url(const char *url
)
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';
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
)
169 ASSERT(db
!= NULL
, return -1);
170 db
->handle
->pm_errno
= 0;
171 ASSERT(url
!= NULL
&& strlen(url
) != 0, RET_ERR(db
->handle
, PM_ERR_WRONG_ARGS
, -1));
173 newurl
= sanitize_url(url
);
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
);
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
;
195 ASSERT(db
!= NULL
, return -1);
196 db
->handle
->pm_errno
= 0;
197 ASSERT(url
!= NULL
&& strlen(url
) != 0, RET_ERR(db
->handle
, PM_ERR_WRONG_ARGS
, -1));
199 newurl
= sanitize_url(url
);
203 db
->servers
= alpm_list_remove_str(db
->servers
, newurl
, &vdata
);
206 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "removed server URL from database '%s': %s\n",
207 db
->treename
, newurl
);
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
);
222 /** Get the signature verification level for a database. */
223 pgp_verify_t SYMEXPORT
alpm_db_get_sigverify_level(alpm_db_t
*db
)
225 ASSERT(db
!= NULL
, return -1);
226 if(db
->pgp_verify
== PM_PGP_VERIFY_UNKNOWN
) {
227 return alpm_option_get_default_sigverify(db
->handle
);
229 return db
->pgp_verify
;
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 ASSERT(db
!= NULL
, return NULL
);
245 db
->handle
->pm_errno
= 0;
246 ASSERT(name
!= NULL
&& strlen(name
) != 0,
247 RET_ERR(db
->handle
, PM_ERR_WRONG_ARGS
, NULL
));
249 return _alpm_db_get_pkgfromcache(db
, name
);
252 /** Get the package cache of a package database. */
253 alpm_list_t SYMEXPORT
*alpm_db_get_pkgcache(alpm_db_t
*db
)
255 ASSERT(db
!= NULL
, return NULL
);
256 db
->handle
->pm_errno
= 0;
257 return _alpm_db_get_pkgcache(db
);
260 /** Get a group entry from a package database. */
261 alpm_group_t SYMEXPORT
*alpm_db_readgroup(alpm_db_t
*db
, const char *name
)
263 ASSERT(db
!= NULL
, return NULL
);
264 db
->handle
->pm_errno
= 0;
265 ASSERT(name
!= NULL
&& strlen(name
) != 0,
266 RET_ERR(db
->handle
, PM_ERR_WRONG_ARGS
, NULL
));
268 return _alpm_db_get_groupfromcache(db
, name
);
271 /** Get the group cache of a package database. */
272 alpm_list_t SYMEXPORT
*alpm_db_get_groupcache(alpm_db_t
*db
)
274 ASSERT(db
!= NULL
, return NULL
);
275 db
->handle
->pm_errno
= 0;
277 return _alpm_db_get_groupcache(db
);
280 /** Searches a database. */
281 alpm_list_t SYMEXPORT
*alpm_db_search(alpm_db_t
*db
, const alpm_list_t
* needles
)
283 ASSERT(db
!= NULL
, return NULL
);
284 db
->handle
->pm_errno
= 0;
286 return _alpm_db_search(db
, needles
);
289 /** Set install reason for a package in db. */
290 int SYMEXPORT
alpm_db_set_pkgreason(alpm_db_t
*db
, const char *name
, alpm_pkgreason_t reason
)
292 ASSERT(db
!= NULL
, return -1);
293 db
->handle
->pm_errno
= 0;
294 /* TODO assert db == db_local ? shouldn't need a db param at all here... */
295 ASSERT(name
!= NULL
, RET_ERR(db
->handle
, PM_ERR_WRONG_ARGS
, -1));
297 alpm_pkg_t
*pkg
= _alpm_db_get_pkgfromcache(db
, name
);
299 RET_ERR(db
->handle
, PM_ERR_PKG_NOT_FOUND
, -1);
302 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "setting install reason %u for %s/%s\n", reason
, db
->treename
, name
);
303 if(alpm_pkg_get_reason(pkg
) == reason
) {
307 /* set reason (in pkgcache) */
308 pkg
->reason
= reason
;
310 if(_alpm_local_db_write(db
, pkg
, INFRQ_DESC
)) {
311 RET_ERR(db
->handle
, PM_ERR_DB_WRITE
, -1);
319 alpm_db_t
*_alpm_db_new(const char *treename
, int is_local
)
323 CALLOC(db
, 1, sizeof(alpm_db_t
), return NULL
);
324 STRDUP(db
->treename
, treename
, return NULL
);
325 db
->is_local
= is_local
;
326 db
->pgp_verify
= PM_PGP_VERIFY_UNKNOWN
;
331 void _alpm_db_free(alpm_db_t
*db
)
333 /* cleanup pkgcache */
334 _alpm_db_free_pkgcache(db
);
335 /* cleanup server list */
336 FREELIST(db
->servers
);
344 const char *_alpm_db_path(alpm_db_t
*db
)
353 dbpath
= alpm_option_get_dbpath(db
->handle
);
355 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("database path is undefined\n"));
356 RET_ERR(db
->handle
, PM_ERR_DB_OPEN
, NULL
);
360 pathsize
= strlen(dbpath
) + strlen(db
->treename
) + 2;
361 CALLOC(db
->_path
, 1, pathsize
, RET_ERR(db
->handle
, PM_ERR_MEMORY
, NULL
));
362 sprintf(db
->_path
, "%s%s/", dbpath
, db
->treename
);
364 pathsize
= strlen(dbpath
) + 5 + strlen(db
->treename
) + 4;
365 CALLOC(db
->_path
, 1, pathsize
, RET_ERR(db
->handle
, PM_ERR_MEMORY
, NULL
));
366 /* all sync DBs now reside in the sync/ subdir of the dbpath */
367 sprintf(db
->_path
, "%ssync/%s.db", dbpath
, db
->treename
);
369 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "database path for tree %s set to %s\n",
370 db
->treename
, db
->_path
);
375 char *_alpm_db_sig_path(alpm_db_t
*db
)
379 const char *dbfile
= _alpm_db_path(db
);
383 len
= strlen(dbfile
) + strlen(".sig") + 1;
384 CALLOC(sigpath
, len
, sizeof(char), RET_ERR(db
->handle
, PM_ERR_MEMORY
, NULL
));
385 sprintf(sigpath
, "%s.sig", dbfile
);
389 int _alpm_db_cmp(const void *d1
, const void *d2
)
391 alpm_db_t
*db1
= (alpm_db_t
*)d1
;
392 alpm_db_t
*db2
= (alpm_db_t
*)d2
;
393 return strcmp(db1
->treename
, db2
->treename
);
396 alpm_list_t
*_alpm_db_search(alpm_db_t
*db
, const alpm_list_t
*needles
)
398 const alpm_list_t
*i
, *j
, *k
;
399 alpm_list_t
*ret
= NULL
;
400 /* copy the pkgcache- we will free the list var after each needle */
401 alpm_list_t
*list
= alpm_list_copy(_alpm_db_get_pkgcache(db
));
403 for(i
= needles
; i
; i
= i
->next
) {
407 if(i
->data
== NULL
) {
412 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "searching for target '%s'\n", targ
);
414 if(regcomp(®
, targ
, REG_EXTENDED
| REG_NOSUB
| REG_ICASE
| REG_NEWLINE
) != 0) {
415 RET_ERR(db
->handle
, PM_ERR_INVALID_REGEX
, NULL
);
418 for(j
= list
; j
; j
= j
->next
) {
419 alpm_pkg_t
*pkg
= j
->data
;
420 const char *matched
= NULL
;
421 const char *name
= alpm_pkg_get_name(pkg
);
422 const char *desc
= alpm_pkg_get_desc(pkg
);
424 /* check name as regex AND as plain text */
425 if(name
&& (regexec(®
, name
, 0, 0, 0) == 0 || strstr(name
, targ
))) {
429 else if(desc
&& regexec(®
, desc
, 0, 0, 0) == 0) {
432 /* TODO: should we be doing this, and should we print something
433 * differently when we do match it since it isn't currently printed? */
436 for(k
= alpm_pkg_get_provides(pkg
); k
; k
= k
->next
) {
437 if(regexec(®
, k
->data
, 0, 0, 0) == 0) {
445 for(k
= alpm_pkg_get_groups(pkg
); k
; k
= k
->next
) {
446 if(regexec(®
, k
->data
, 0, 0, 0) == 0) {
453 if(matched
!= NULL
) {
454 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, " search target '%s' matched '%s'\n",
456 ret
= alpm_list_add(ret
, pkg
);
460 /* Free the existing search list, and use the returned list for the
461 * next needle. This allows for AND-based package searching. */
462 alpm_list_free(list
);
470 /* Returns a new package cache from db.
471 * It frees the cache if it already exists.
473 static int load_pkgcache(alpm_db_t
*db
)
475 _alpm_db_free_pkgcache(db
);
477 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "loading package cache for repository '%s'\n",
479 if(db
->ops
->populate(db
) == -1) {
480 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
,
481 "failed to load package cache for repository '%s'\n", db
->treename
);
485 db
->status
|= DB_STATUS_PKGCACHE
;
489 void _alpm_db_free_pkgcache(alpm_db_t
*db
)
491 if(db
== NULL
|| !(db
->status
& DB_STATUS_PKGCACHE
)) {
495 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
,
496 "freeing package cache for repository '%s'\n", db
->treename
);
498 alpm_list_free_inner(_alpm_db_get_pkgcache(db
),
499 (alpm_list_fn_free
)_alpm_pkg_free
);
500 _alpm_pkghash_free(db
->pkgcache
);
501 db
->status
&= ~DB_STATUS_PKGCACHE
;
503 _alpm_db_free_groupcache(db
);
506 alpm_pkghash_t
*_alpm_db_get_pkgcache_hash(alpm_db_t
*db
)
512 if(!(db
->status
& DB_STATUS_VALID
)) {
513 RET_ERR(db
->handle
, PM_ERR_DB_INVALID
, NULL
);
516 if(!(db
->status
& DB_STATUS_PKGCACHE
)) {
523 alpm_list_t
*_alpm_db_get_pkgcache(alpm_db_t
*db
)
525 alpm_pkghash_t
*hash
= _alpm_db_get_pkgcache_hash(db
);
534 /* "duplicate" pkg then add it to pkgcache */
535 int _alpm_db_add_pkgincache(alpm_db_t
*db
, alpm_pkg_t
*pkg
)
539 if(db
== NULL
|| pkg
== NULL
|| !(db
->status
& DB_STATUS_PKGCACHE
)) {
543 newpkg
= _alpm_pkg_dup(pkg
);
548 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "adding entry '%s' in '%s' cache\n",
549 alpm_pkg_get_name(newpkg
), db
->treename
);
550 db
->pkgcache
= _alpm_pkghash_add_sorted(db
->pkgcache
, newpkg
);
552 _alpm_db_free_groupcache(db
);
557 int _alpm_db_remove_pkgfromcache(alpm_db_t
*db
, alpm_pkg_t
*pkg
)
559 alpm_pkg_t
*data
= NULL
;
561 if(db
== NULL
|| pkg
== NULL
|| !(db
->status
& DB_STATUS_PKGCACHE
)) {
565 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "removing entry '%s' from '%s' cache\n",
566 alpm_pkg_get_name(pkg
), db
->treename
);
568 db
->pkgcache
= _alpm_pkghash_remove(db
->pkgcache
, pkg
, &data
);
570 /* package not found */
571 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "cannot remove entry '%s' from '%s' cache: not found\n",
572 alpm_pkg_get_name(pkg
), db
->treename
);
576 _alpm_pkg_free(data
);
578 _alpm_db_free_groupcache(db
);
583 alpm_pkg_t
*_alpm_db_get_pkgfromcache(alpm_db_t
*db
, const char *target
)
589 alpm_pkghash_t
*pkgcache
= _alpm_db_get_pkgcache_hash(db
);
594 return _alpm_pkghash_find(pkgcache
, target
);
597 /* Returns a new group cache from db.
599 static int load_grpcache(alpm_db_t
*db
)
607 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "loading group cache for repository '%s'\n",
610 for(lp
= _alpm_db_get_pkgcache(db
); lp
; lp
= lp
->next
) {
611 const alpm_list_t
*i
;
612 alpm_pkg_t
*pkg
= lp
->data
;
614 for(i
= alpm_pkg_get_groups(pkg
); i
; i
= i
->next
) {
615 const char *grpname
= i
->data
;
617 alpm_group_t
*grp
= NULL
;
620 /* first look through the group cache for a group with this name */
621 for(j
= db
->grpcache
; j
; j
= j
->next
) {
624 if(strcmp(grp
->name
, grpname
) == 0
625 && !alpm_list_find_ptr(grp
->packages
, pkg
)) {
626 grp
->packages
= alpm_list_add(grp
->packages
, pkg
);
634 /* we didn't find the group, so create a new one with this name */
635 grp
= _alpm_group_new(grpname
);
637 _alpm_db_free_groupcache(db
);
640 grp
->packages
= alpm_list_add(grp
->packages
, pkg
);
641 db
->grpcache
= alpm_list_add(db
->grpcache
, grp
);
645 db
->status
|= DB_STATUS_GRPCACHE
;
649 void _alpm_db_free_groupcache(alpm_db_t
*db
)
653 if(db
== NULL
|| !(db
->status
& DB_STATUS_GRPCACHE
)) {
657 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
,
658 "freeing group cache for repository '%s'\n", db
->treename
);
660 for(lg
= db
->grpcache
; lg
; lg
= lg
->next
) {
661 _alpm_group_free(lg
->data
);
664 FREELIST(db
->grpcache
);
665 db
->status
&= ~DB_STATUS_GRPCACHE
;
668 alpm_list_t
*_alpm_db_get_groupcache(alpm_db_t
*db
)
674 if(!(db
->status
& DB_STATUS_VALID
)) {
675 RET_ERR(db
->handle
, PM_ERR_DB_INVALID
, NULL
);
678 if(!(db
->status
& DB_STATUS_GRPCACHE
)) {
685 alpm_group_t
*_alpm_db_get_groupfromcache(alpm_db_t
*db
, const char *target
)
689 if(db
== NULL
|| target
== NULL
|| strlen(target
) == 0) {
693 for(i
= _alpm_db_get_groupcache(db
); i
; i
= i
->next
) {
694 alpm_group_t
*info
= i
->data
;
696 if(strcmp(info
->name
, target
) == 0) {
704 /* vim: set ts=2 sw=2 noet: */