38#include "xbps_api_impl.h"
63static int pkgdb_fd = -1;
64static bool pkgdb_map_names_done =
false;
75 prev_umask = umask(022);
77 if (xbps_pkgdb_init(xhp) == ENOENT) {
79 if (access(xhp->
metadir, R_OK|X_OK) == -1) {
80 if (errno != ENOENT) {
86 xbps_dbg_printf(
"[pkgdb] failed to create metadir "
87 "%s: %s\n", xhp->
metadir, strerror(rv));
92 xhp->
pkgdb = xbps_dictionary_create();
95 xbps_dbg_printf(
"[pkgdb] failed to create pkgdb "
101 if ((pkgdb_fd = open(xhp->
pkgdb_plist, O_CREAT|O_RDWR|O_CLOEXEC, 0664)) == -1) {
103 xbps_dbg_printf(
"[pkgdb] cannot open pkgdb for locking "
112 if (lockf(pkgdb_fd, F_TLOCK, 0) == -1) {
114 xbps_dbg_printf(
"[pkgdb] cannot lock pkgdb: %s\n", strerror(rv));
119 if (access(xhp->
rootdir, W_OK) == -1) {
121 xbps_dbg_printf(
"[pkgdb] rootdir %s: %s\n", xhp->
rootdir, strerror(rv));
132 xbps_dbg_printf(
"%s: pkgdb_fd %d\n", __func__, pkgdb_fd);
134 if (pkgdb_fd != -1) {
135 if (lockf(pkgdb_fd, F_ULOCK, 0) == -1)
136 xbps_dbg_printf(
"[pkgdb] failed to unlock pkgdb: %s\n", strerror(errno));
138 (void)close(pkgdb_fd);
146 xbps_object_iterator_t iter;
150 if (!xbps_dictionary_count(xhp->
pkgdb))
153 if (xhp->vpkgd == NULL) {
154 xhp->vpkgd = xbps_dictionary_create();
160 iter = xbps_dictionary_iterator(xhp->
pkgdb);
163 while ((obj = xbps_object_iterator_next(iter))) {
164 xbps_array_t provides;
165 xbps_dictionary_t pkgd;
166 const char *pkgver = NULL;
167 char pkgname[XBPS_NAME_SIZE] = {0};
170 pkgd = xbps_dictionary_get_keysym(xhp->
pkgdb, obj);
171 provides = xbps_dictionary_get(pkgd,
"provides");
172 cnt = xbps_array_count(provides);
176 xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgver", &pkgver);
181 for (
unsigned int i = 0; i < cnt; i++) {
182 const char *vpkg = NULL;
184 xbps_array_get_cstring_nocopy(provides, i, &vpkg);
185 if (!xbps_dictionary_set_cstring(xhp->vpkgd, vpkg, pkgname)) {
186 xbps_dbg_printf(
"%s: set_cstring vpkg "
187 "%s pkgname %s\n", __func__, vpkg, pkgname);
191 xbps_dbg_printf(
"[pkgdb] added vpkg %s for %s\n", vpkg, pkgname);
195 xbps_object_iterator_release(iter);
202 xbps_object_iterator_t iter;
206 if (pkgdb_map_names_done || !xbps_dictionary_count(xhp->
pkgdb))
213 iter = xbps_dictionary_iterator(xhp->
pkgdb);
216 while ((obj = xbps_object_iterator_next(iter))) {
217 xbps_dictionary_t pkgd;
219 char pkgname[XBPS_NAME_SIZE] = {0};
221 pkgd = xbps_dictionary_get_keysym(xhp->
pkgdb, obj);
222 if (!xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgver", &pkgver)) {
229 if (!xbps_dictionary_set_cstring(pkgd,
"pkgname", pkgname)) {
234 xbps_object_iterator_release(iter);
236 pkgdb_map_names_done =
true;
255 if ((rv = xbps_pkgdb_conversion(xhp)) != 0)
262 xbps_dbg_printf(
"[pkgdb] cannot internalize "
263 "pkgdb dictionary: %s\n", strerror(rv));
267 if ((rv = pkgdb_map_names(xhp)) != 0) {
268 xbps_dbg_printf(
"[pkgdb] pkgdb_map_names %s\n", strerror(rv));
271 if ((rv = pkgdb_map_vpkgs(xhp)) != 0) {
272 xbps_dbg_printf(
"[pkgdb] pkgdb_map_vpkgs %s\n", strerror(rv));
276 xbps_dbg_printf(
"[pkgdb] initialized ok.\n");
284 xbps_dictionary_t pkgdb_storage;
286 static int cached_rv;
289 if (cached_rv && !flush)
292 if (xhp->
pkgdb && flush) {
293 pkgdb_storage = xbps_dictionary_internalize_from_file(xhp->
pkgdb_plist);
294 if (pkgdb_storage == NULL ||
295 !xbps_dictionary_equals(xhp->
pkgdb, pkgdb_storage)) {
297 prev_umask = umask(022);
305 xbps_object_release(pkgdb_storage);
307 xbps_object_release(xhp->
pkgdb);
315 if ((xhp->
pkgdb = xbps_dictionary_internalize_from_file(xhp->
pkgdb_plist)) == NULL) {
321 xhp->
pkgdb = xbps_dictionary_create();
323 xbps_error_printf(
"cannot access to pkgdb: %s\n", strerror(rv));
325 cached_rv = rv = errno;
338 xbps_object_release(xhp->
pkgdb);
339 xbps_dbg_printf(
"[pkgdb] released ok.\n");
344 int (*fn)(
struct xbps_handle *, xbps_object_t,
const char *,
void *,
bool *),
347 xbps_array_t allkeys;
350 if ((rv = xbps_pkgdb_init(xhp)) != 0)
353 allkeys = xbps_dictionary_all_keys(xhp->
pkgdb);
356 xbps_object_release(allkeys);
362 int (*fn)(
struct xbps_handle *, xbps_object_t,
const char *,
void *,
bool *),
365 xbps_array_t allkeys;
368 if ((rv = xbps_pkgdb_init(xhp)) != 0)
371 allkeys = xbps_dictionary_all_keys(xhp->
pkgdb);
374 xbps_object_release(allkeys);
381 if (xbps_pkgdb_init(xhp) != 0)
384 return xbps_find_pkg_in_dict(xhp->
pkgdb, pkg);
390 if (xbps_pkgdb_init(xhp) != 0)
393 return xbps_find_virtualpkg_in_dict(xhp, xhp->
pkgdb, vpkg);
400 xbps_object_iterator_t iter;
402 if (xhp->pkgdb_revdeps)
405 xhp->pkgdb_revdeps = xbps_dictionary_create();
406 assert(xhp->pkgdb_revdeps);
408 iter = xbps_dictionary_iterator(xhp->
pkgdb);
411 while ((obj = xbps_object_iterator_next(iter))) {
412 xbps_array_t rundeps;
413 xbps_dictionary_t pkgd;
414 const char *pkgver = NULL;
416 pkgd = xbps_dictionary_get_keysym(xhp->
pkgdb, obj);
417 rundeps = xbps_dictionary_get(pkgd,
"run_depends");
418 if (!xbps_array_count(rundeps))
421 xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgver", &pkgver);
422 for (
unsigned int i = 0; i < xbps_array_count(rundeps); i++) {
424 const char *pkgdep = NULL, *vpkgname = NULL;
425 char *v, curpkgname[XBPS_NAME_SIZE];
428 xbps_array_get_cstring_nocopy(rundeps, i, &pkgdep);
433 vpkgname = vpkg_user_conf(xhp, curpkgname,
false);
434 if (vpkgname == NULL) {
435 v = strdup(curpkgname);
437 v = strdup(vpkgname);
440 pkg = xbps_dictionary_get(xhp->pkgdb_revdeps, v);
443 pkg = xbps_array_create();
446 xbps_array_add_cstring_nocopy(pkg, pkgver);
447 xbps_dictionary_set(xhp->pkgdb_revdeps, v, pkg);
451 xbps_object_release(pkg);
454 xbps_object_iterator_release(iter);
460 xbps_dictionary_t pkgd;
461 const char *pkgver = NULL;
462 char pkgname[XBPS_NAME_SIZE];
467 generate_full_revdeps_tree(xhp);
468 xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgver", &pkgver);
472 return xbps_dictionary_get(xhp->pkgdb_revdeps, pkgname);
478 return xbps_get_pkg_fulldeptree(xhp, pkg,
false);
484 xbps_dictionary_t pkgd;
485 const char *pkgver = NULL;
486 char pkgname[XBPS_NAME_SIZE], plist[PATH_MAX];
495 xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgver", &pkgver);
499 snprintf(plist,
sizeof(plist)-1,
"%s/.%s-files.plist", xhp->
metadir, pkgname);
char metadir[XBPS_MAXPATH+sizeof(XBPS_META_PATH)]
char rootdir[XBPS_MAXPATH]
Generic XBPS structure handler for initialization.
xbps_array_t xbps_pkgdb_get_pkg_fulldeptree(struct xbps_handle *xhp, const char *pkg)
int xbps_pkgdb_foreach_cb_multi(struct xbps_handle *xhp, int(*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *), void *arg)
xbps_dictionary_t xbps_pkgdb_get_pkg_files(struct xbps_handle *xhp, const char *pkg)
void xbps_pkgdb_unlock(struct xbps_handle *xhp)
xbps_dictionary_t xbps_pkgdb_get_virtualpkg(struct xbps_handle *xhp, const char *vpkg)
int xbps_pkgdb_foreach_cb(struct xbps_handle *xhp, int(*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *), void *arg)
xbps_array_t xbps_pkgdb_get_pkg_revdeps(struct xbps_handle *xhp, const char *pkg)
int xbps_pkgdb_update(struct xbps_handle *xhp, bool flush, bool update)
int xbps_pkgdb_lock(struct xbps_handle *xhp)
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
bool xbps_match_string_in_array(xbps_array_t array, const char *val)
int xbps_array_foreach_cb_multi(struct xbps_handle *xhp, xbps_array_t array, xbps_dictionary_t dict, int(*fn)(struct xbps_handle *, xbps_object_t obj, const char *, void *arg, bool *done), void *arg)
int xbps_array_foreach_cb(struct xbps_handle *xhp, xbps_array_t array, xbps_dictionary_t dict, int(*fn)(struct xbps_handle *, xbps_object_t obj, const char *, void *arg, bool *done), void *arg)
xbps_dictionary_t xbps_plist_dictionary_from_file(const char *path)
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
int xbps_mkpath(const char *path, mode_t mode)
bool xbps_pkgpattern_name(char *dst, size_t len, const char *pattern)