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/>.
37 #include "alpm_list.h"
45 /** \addtogroup alpm_databases Database Functions
46 * @brief Functions to query and manipulate the database of libalpm
50 /** Register a sync database of packages.
51 * @param treename the name of the sync repository
52 * @return a pmdb_t* on success (the value), NULL on error
54 pmdb_t SYMEXPORT
*alpm_db_register_sync(const char *treename
)
59 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, NULL
));
60 ASSERT(treename
!= NULL
&& strlen(treename
) != 0, RET_ERR(PM_ERR_WRONG_ARGS
, NULL
));
61 /* Do not register a database if a transaction is on-going */
62 ASSERT(handle
->trans
== NULL
, RET_ERR(PM_ERR_TRANS_NOT_NULL
, NULL
));
64 return _alpm_db_register_sync(treename
);
67 /* Helper function for alpm_db_unregister{_all} */
68 void _alpm_db_unregister(pmdb_t
*db
)
74 _alpm_log(PM_LOG_DEBUG
, "unregistering database '%s'\n", db
->treename
);
78 /** Unregister all package databases
79 * @return 0 on success, -1 on error (pm_errno is set accordingly)
81 int SYMEXPORT
alpm_db_unregister_all(void)
89 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
90 /* Do not unregister a database if a transaction is on-going */
91 ASSERT(handle
->trans
== NULL
, RET_ERR(PM_ERR_TRANS_NOT_NULL
, -1));
93 /* close local database */
94 db
= handle
->db_local
;
96 db
->ops
->unregister(db
);
97 handle
->db_local
= NULL
;
100 /* and also sync ones */
101 for(i
= handle
->dbs_sync
; i
; i
= i
->next
) {
103 db
->ops
->unregister(db
);
106 FREELIST(handle
->dbs_sync
);
110 /** Unregister a package database
111 * @param db pointer to the package database to unregister
112 * @return 0 on success, -1 on error (pm_errno is set accordingly)
114 int SYMEXPORT
alpm_db_unregister(pmdb_t
*db
)
121 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
122 ASSERT(db
!= NULL
, RET_ERR(PM_ERR_WRONG_ARGS
, -1));
123 /* Do not unregister a database if a transaction is on-going */
124 ASSERT(handle
->trans
== NULL
, RET_ERR(PM_ERR_TRANS_NOT_NULL
, -1));
126 if(db
== handle
->db_local
) {
127 handle
->db_local
= NULL
;
130 /* Warning : this function shouldn't be used to unregister all sync
131 * databases by walking through the list returned by
132 * alpm_option_get_syncdbs, because the db is removed from that list here.
135 handle
->dbs_sync
= alpm_list_remove(handle
->dbs_sync
,
136 db
, _alpm_db_cmp
, &data
);
143 RET_ERR(PM_ERR_DB_NOT_FOUND
, -1);
146 db
->ops
->unregister(db
);
150 /** Set the serverlist of a database.
151 * @param db database pointer
152 * @param url url of the server
153 * @return 0 on success, -1 on error (pm_errno is set accordingly)
155 int SYMEXPORT
alpm_db_setserver(pmdb_t
*db
, const char *url
)
163 ASSERT(db
!= NULL
, RET_ERR(PM_ERR_DB_NULL
, -1));
169 newurl
= strdup(url
);
170 /* strip the trailing slash if one exists */
171 if(newurl
[len
- 1] == '/') {
172 newurl
[len
- 1] = '\0';
174 db
->servers
= alpm_list_add(db
->servers
, newurl
);
175 _alpm_log(PM_LOG_DEBUG
, "adding new server URL to database '%s': %s\n",
176 db
->treename
, newurl
);
178 FREELIST(db
->servers
);
179 _alpm_log(PM_LOG_DEBUG
, "serverlist flushed for '%s'\n", db
->treename
);
184 /** Set the verify gpg signature option for a database.
185 * @param db database pointer
186 * @param verify enum pgp_verify_t
187 * @return 0 on success, -1 on error (pm_errno is set accordingly)
189 int SYMEXPORT
alpm_db_set_pgp_verify(pmdb_t
*db
, pgp_verify_t verify
)
194 ASSERT(db
!= NULL
, RET_ERR(PM_ERR_DB_NULL
, -1));
196 db
->pgp_verify
= verify
;
197 _alpm_log(PM_LOG_DEBUG
, "adding VerifySig option to database '%s': %d\n",
198 db
->treename
, verify
);
203 /** Get the name of a package database
204 * @param db pointer to the package database
205 * @return the name of the package database, NULL on error
207 const char SYMEXPORT
*alpm_db_get_name(const pmdb_t
*db
)
212 ASSERT(handle
!= NULL
, return NULL
);
213 ASSERT(db
!= NULL
, return NULL
);
218 /** Get a download URL for the package database
219 * @param db pointer to the package database
220 * @return a fully-specified download URL, NULL on error
222 const char SYMEXPORT
*alpm_db_get_url(const pmdb_t
*db
)
229 ASSERT(handle
!= NULL
, return NULL
);
230 ASSERT(db
!= NULL
, return NULL
);
231 ASSERT(db
->servers
!= NULL
, return NULL
);
233 url
= (char*)db
->servers
->data
;
239 /** Get a package entry from a package database
240 * @param db pointer to the package database to get the package from
241 * @param name of the package
242 * @return the package entry on success, NULL on error
244 pmpkg_t SYMEXPORT
*alpm_db_get_pkg(pmdb_t
*db
, const char *name
)
249 ASSERT(handle
!= NULL
, return NULL
);
250 ASSERT(db
!= NULL
, return NULL
);
251 ASSERT(name
!= NULL
&& strlen(name
) != 0, return NULL
);
253 return _alpm_db_get_pkgfromcache(db
, name
);
256 /** Get the package cache of a package database
257 * @param db pointer to the package database to get the package from
258 * @return the list of packages on success, NULL on error
260 alpm_list_t SYMEXPORT
*alpm_db_get_pkgcache(pmdb_t
*db
)
265 ASSERT(handle
!= NULL
, return NULL
);
266 ASSERT(db
!= NULL
, return NULL
);
268 return _alpm_db_get_pkgcache(db
);
271 /** Get a group entry from a package database
272 * @param db pointer to the package database to get the group from
273 * @param name of the group
274 * @return the groups entry on success, NULL on error
276 pmgrp_t SYMEXPORT
*alpm_db_readgrp(pmdb_t
*db
, const char *name
)
281 ASSERT(handle
!= NULL
, return NULL
);
282 ASSERT(db
!= NULL
, return NULL
);
283 ASSERT(name
!= NULL
&& strlen(name
) != 0, return NULL
);
285 return _alpm_db_get_grpfromcache(db
, name
);
288 /** Get the group cache of a package database
289 * @param db pointer to the package database to get the group from
290 * @return the list of groups on success, NULL on error
292 alpm_list_t SYMEXPORT
*alpm_db_get_grpcache(pmdb_t
*db
)
297 ASSERT(handle
!= NULL
, return NULL
);
298 ASSERT(db
!= NULL
, return NULL
);
300 return _alpm_db_get_grpcache(db
);
303 /** Searches a database
304 * @param db pointer to the package database to search in
305 * @param needles the list of strings to search for
306 * @return the list of packages on success, NULL on error
308 alpm_list_t SYMEXPORT
*alpm_db_search(pmdb_t
*db
, const alpm_list_t
* needles
)
313 ASSERT(handle
!= NULL
, return NULL
);
314 ASSERT(db
!= NULL
, return NULL
);
316 return _alpm_db_search(db
, needles
);
319 /** Set install reason for a package in db
320 * @param db pointer to the package database
321 * @param name the name of the package
322 * @param reason the new install reason
323 * @return 0 on success, -1 on error (pm_errno is set accordingly)
325 int SYMEXPORT
alpm_db_set_pkgreason(pmdb_t
*db
, const char *name
, pmpkgreason_t reason
)
330 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
331 ASSERT(db
!= NULL
&& name
!= NULL
, RET_ERR(PM_ERR_WRONG_ARGS
, -1));
333 pmpkg_t
*pkg
= _alpm_db_get_pkgfromcache(db
, name
);
335 RET_ERR(PM_ERR_PKG_NOT_FOUND
, -1);
338 _alpm_log(PM_LOG_DEBUG
, "setting install reason %u for %s/%s\n", reason
, db
->treename
, name
);
339 if(alpm_pkg_get_reason(pkg
) == reason
) {
343 /* set reason (in pkgcache) */
344 pkg
->reason
= reason
;
346 if(_alpm_local_db_write(db
, pkg
, INFRQ_DESC
)) {
355 pmdb_t
*_alpm_db_new(const char *treename
, int is_local
)
361 CALLOC(db
, 1, sizeof(pmdb_t
), RET_ERR(PM_ERR_MEMORY
, NULL
));
362 STRDUP(db
->treename
, treename
, RET_ERR(PM_ERR_MEMORY
, NULL
));
363 db
->is_local
= is_local
;
368 static int load_pgpsig(pmdb_t
*db
) {
373 dbfile
= _alpm_db_path(db
);
374 len
= strlen(dbfile
) + 5;
375 MALLOC(sigfile
, len
, RET_ERR(PM_ERR_MEMORY
, -1));
376 sprintf(sigfile
, "%s.sig", dbfile
);
378 if(access(sigfile
, R_OK
) == 0) {
382 f
= fopen(sigfile
, "rb");
383 fseek(f
, 0L, SEEK_END
);
386 _alpm_log(PM_LOG_WARNING
, _("Failed reading PGP signature file for %s"),
390 fseek(f
, 0L, SEEK_SET
);
391 CALLOC(db
->pgpsig
.rawdata
, bytes
, sizeof(char),
393 bytes_read
= fread(db
->pgpsig
.rawdata
, sizeof(char), bytes
, f
);
394 if(bytes_read
== (size_t)bytes
) {
395 db
->pgpsig
.rawlen
= bytes
;
396 _alpm_log(PM_LOG_DEBUG
,
397 "loaded database .sig file, location %s\n", sigfile
);
399 _alpm_log(PM_LOG_WARNING
, _("Failed reading PGP signature file for %s"),
406 _alpm_log(PM_LOG_DEBUG
, "no database signature file found\n");
412 FREE(db
->pgpsig
.rawdata
);
413 db
->pgpsig
.rawlen
= 0;
417 const pmpgpsig_t
*_alpm_db_pgpsig(pmdb_t
*db
)
422 ASSERT(db
!= NULL
, return(NULL
));
424 if(db
->pgpsig
.rawdata
== NULL
) {
428 return &(db
->pgpsig
);
431 void _alpm_db_free(pmdb_t
*db
)
435 /* cleanup pkgcache */
436 _alpm_db_free_pkgcache(db
);
437 /* cleanup server list */
438 FREELIST(db
->servers
);
439 /* only need to free rawdata */
440 FREE(db
->pgpsig
.rawdata
);
448 const char *_alpm_db_path(pmdb_t
*db
)
457 dbpath
= alpm_option_get_dbpath();
459 _alpm_log(PM_LOG_ERROR
, _("database path is undefined\n"));
460 RET_ERR(PM_ERR_DB_OPEN
, NULL
);
464 pathsize
= strlen(dbpath
) + strlen(db
->treename
) + 2;
465 CALLOC(db
->_path
, 1, pathsize
, RET_ERR(PM_ERR_MEMORY
, NULL
));
466 sprintf(db
->_path
, "%s%s/", dbpath
, db
->treename
);
468 pathsize
= strlen(dbpath
) + 5 + strlen(db
->treename
) + 4;
469 CALLOC(db
->_path
, 1, pathsize
, RET_ERR(PM_ERR_MEMORY
, NULL
));
470 /* all sync DBs now reside in the sync/ subdir of the dbpath */
471 sprintf(db
->_path
, "%ssync/%s.db", dbpath
, db
->treename
);
473 _alpm_log(PM_LOG_DEBUG
, "database path for tree %s set to %s\n",
474 db
->treename
, db
->_path
);
479 int _alpm_db_version(pmdb_t
*db
)
484 return db
->ops
->version(db
);
487 int _alpm_db_cmp(const void *d1
, const void *d2
)
489 pmdb_t
*db1
= (pmdb_t
*)d1
;
490 pmdb_t
*db2
= (pmdb_t
*)d2
;
491 return strcmp(db1
->treename
, db2
->treename
);
494 alpm_list_t
*_alpm_db_search(pmdb_t
*db
, const alpm_list_t
*needles
)
496 const alpm_list_t
*i
, *j
, *k
;
497 alpm_list_t
*ret
= NULL
;
498 /* copy the pkgcache- we will free the list var after each needle */
499 alpm_list_t
*list
= alpm_list_copy(_alpm_db_get_pkgcache(db
));
503 for(i
= needles
; i
; i
= i
->next
) {
507 if(i
->data
== NULL
) {
512 _alpm_log(PM_LOG_DEBUG
, "searching for target '%s'\n", targ
);
514 if(regcomp(®
, targ
, REG_EXTENDED
| REG_NOSUB
| REG_ICASE
| REG_NEWLINE
) != 0) {
515 RET_ERR(PM_ERR_INVALID_REGEX
, NULL
);
518 for(j
= list
; j
; j
= j
->next
) {
519 pmpkg_t
*pkg
= j
->data
;
520 const char *matched
= NULL
;
521 const char *name
= alpm_pkg_get_name(pkg
);
522 const char *desc
= alpm_pkg_get_desc(pkg
);
524 /* check name as regex AND as plain text */
525 if(name
&& (regexec(®
, name
, 0, 0, 0) == 0 || strstr(name
, targ
))) {
529 else if (desc
&& regexec(®
, desc
, 0, 0, 0) == 0) {
532 /* TODO: should we be doing this, and should we print something
533 * differently when we do match it since it isn't currently printed? */
536 for(k
= alpm_pkg_get_provides(pkg
); k
; k
= k
->next
) {
537 if (regexec(®
, k
->data
, 0, 0, 0) == 0) {
545 for(k
= alpm_pkg_get_groups(pkg
); k
; k
= k
->next
) {
546 if (regexec(®
, k
->data
, 0, 0, 0) == 0) {
553 if(matched
!= NULL
) {
554 _alpm_log(PM_LOG_DEBUG
, " search target '%s' matched '%s'\n",
556 ret
= alpm_list_add(ret
, pkg
);
560 /* Free the existing search list, and use the returned list for the
561 * next needle. This allows for AND-based package searching. */
562 alpm_list_free(list
);
570 /* Returns a new package cache from db.
571 * It frees the cache if it already exists.
573 int _alpm_db_load_pkgcache(pmdb_t
*db
)
580 _alpm_db_free_pkgcache(db
);
582 _alpm_log(PM_LOG_DEBUG
, "loading package cache for repository '%s'\n",
584 if(db
->ops
->populate(db
) == -1) {
585 _alpm_log(PM_LOG_DEBUG
,
586 "failed to load package cache for repository '%s'\n", db
->treename
);
590 db
->pkgcache_loaded
= 1;
594 void _alpm_db_free_pkgcache(pmdb_t
*db
)
598 if(db
== NULL
|| !db
->pkgcache_loaded
) {
602 _alpm_log(PM_LOG_DEBUG
, "freeing package cache for repository '%s'\n",
605 alpm_list_free_inner(_alpm_db_get_pkgcache(db
),
606 (alpm_list_fn_free
)_alpm_pkg_free
);
607 _alpm_pkghash_free(db
->pkgcache
);
608 db
->pkgcache_loaded
= 0;
610 _alpm_db_free_grpcache(db
);
613 pmpkghash_t
*_alpm_db_get_pkgcache_hash(pmdb_t
*db
)
621 if(!db
->pkgcache_loaded
) {
622 _alpm_db_load_pkgcache(db
);
625 /* hmmm, still NULL ?*/
627 _alpm_log(PM_LOG_DEBUG
, "warning: pkgcache is NULL for db '%s'\n", db
->treename
);
633 alpm_list_t
*_alpm_db_get_pkgcache(pmdb_t
*db
)
637 pmpkghash_t
*hash
= _alpm_db_get_pkgcache_hash(db
);
646 /* "duplicate" pkg then add it to pkgcache */
647 int _alpm_db_add_pkgincache(pmdb_t
*db
, pmpkg_t
*pkg
)
653 if(db
== NULL
|| !db
->pkgcache_loaded
|| pkg
== NULL
) {
657 newpkg
= _alpm_pkg_dup(pkg
);
662 _alpm_log(PM_LOG_DEBUG
, "adding entry '%s' in '%s' cache\n",
663 alpm_pkg_get_name(newpkg
), db
->treename
);
664 db
->pkgcache
= _alpm_pkghash_add_sorted(db
->pkgcache
, newpkg
);
666 _alpm_db_free_grpcache(db
);
671 int _alpm_db_remove_pkgfromcache(pmdb_t
*db
, pmpkg_t
*pkg
)
673 pmpkg_t
*data
= NULL
;
677 if(db
== NULL
|| !db
->pkgcache_loaded
|| pkg
== NULL
) {
681 _alpm_log(PM_LOG_DEBUG
, "removing entry '%s' from '%s' cache\n",
682 alpm_pkg_get_name(pkg
), db
->treename
);
684 db
->pkgcache
= _alpm_pkghash_remove(db
->pkgcache
, pkg
, &data
);
686 /* package not found */
687 _alpm_log(PM_LOG_DEBUG
, "cannot remove entry '%s' from '%s' cache: not found\n",
688 alpm_pkg_get_name(pkg
), db
->treename
);
692 _alpm_pkg_free(data
);
694 _alpm_db_free_grpcache(db
);
699 pmpkg_t
*_alpm_db_get_pkgfromcache(pmdb_t
*db
, const char *target
)
707 pmpkghash_t
*pkgcache
= _alpm_db_get_pkgcache_hash(db
);
709 _alpm_log(PM_LOG_DEBUG
, "warning: failed to get '%s' from NULL pkgcache\n",
714 return _alpm_pkghash_find(pkgcache
, target
);
717 /* Returns a new group cache from db.
719 int _alpm_db_load_grpcache(pmdb_t
*db
)
729 _alpm_log(PM_LOG_DEBUG
, "loading group cache for repository '%s'\n",
732 for(lp
= _alpm_db_get_pkgcache(db
); lp
; lp
= lp
->next
) {
733 const alpm_list_t
*i
;
734 pmpkg_t
*pkg
= lp
->data
;
736 for(i
= alpm_pkg_get_groups(pkg
); i
; i
= i
->next
) {
737 const char *grpname
= i
->data
;
742 /* first look through the group cache for a group with this name */
743 for(j
= db
->grpcache
; j
; j
= j
->next
) {
746 if(strcmp(grp
->name
, grpname
) == 0
747 && !alpm_list_find_ptr(grp
->packages
, pkg
)) {
748 grp
->packages
= alpm_list_add(grp
->packages
, pkg
);
756 /* we didn't find the group, so create a new one with this name */
757 grp
= _alpm_grp_new(grpname
);
758 grp
->packages
= alpm_list_add(grp
->packages
, pkg
);
759 db
->grpcache
= alpm_list_add(db
->grpcache
, grp
);
763 db
->grpcache_loaded
= 1;
767 void _alpm_db_free_grpcache(pmdb_t
*db
)
773 if(db
== NULL
|| !db
->grpcache_loaded
) {
777 _alpm_log(PM_LOG_DEBUG
, "freeing group cache for repository '%s'\n",
780 for(lg
= db
->grpcache
; lg
; lg
= lg
->next
) {
781 _alpm_grp_free(lg
->data
);
784 FREELIST(db
->grpcache
);
785 db
->grpcache_loaded
= 0;
788 alpm_list_t
*_alpm_db_get_grpcache(pmdb_t
*db
)
796 if(!db
->grpcache_loaded
) {
797 _alpm_db_load_grpcache(db
);
803 pmgrp_t
*_alpm_db_get_grpfromcache(pmdb_t
*db
, const char *target
)
809 if(db
== NULL
|| target
== NULL
|| strlen(target
) == 0) {
813 for(i
= _alpm_db_get_grpcache(db
); i
; i
= i
->next
) {
814 pmgrp_t
*info
= i
->data
;
816 if(strcmp(info
->name
, target
) == 0) {
824 /* vim: set ts=2 sw=2 noet: */