XBPS Library API 20250531
The X Binary Package System
pkgdb.c
1/*-
2 * Copyright (c) 2012-2020 Juan Romero Pardines.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/types.h>
27#include <sys/stat.h>
28
29#include <errno.h>
30#include <fcntl.h>
31#include <limits.h>
32#include <stdbool.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38#include "xbps.h"
39#include "xbps_api_impl.h"
40
41/**
42 * @file lib/pkgdb.c
43 * @brief Package database handling routines
44 * @defgroup pkgdb Package database handling functions
45 *
46 * Functions to manipulate the main package database plist file (pkgdb).
47 *
48 * The following image shown below shows the proplib structure used
49 * by the main package database plist:
50 *
51 * @image html images/xbps_pkgdb_dictionary.png
52 *
53 * Legend:
54 * - <b>Salmon filled box</b>: \a pkgdb plist internalized.
55 * - <b>White filled box</b>: mandatory objects.
56 * - <b>Grey filled box</b>: optional objects.
57 * - <b>Green filled box</b>: possible value set in the object, only one
58 * of them is set.
59 *
60 * Text inside of white boxes are the key associated with the object, its
61 * data type is specified on its edge, i.e array, bool, integer, string,
62 * dictionary.
63 */
64static int pkgdb_fd = -1;
65static bool pkgdb_map_names_done = false;
66
67int
69{
70 mode_t prev_umask;
71 int rv = 0;
72 /*
73 * Use a mandatory file lock to only allow one writer to pkgdb,
74 * other writers will block.
75 */
76 prev_umask = umask(022);
77 xhp->pkgdb_plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_PKGDB);
78 if (xbps_pkgdb_init(xhp) == ENOENT) {
79 /* if metadir does not exist, create it */
80 if (access(xhp->metadir, R_OK|X_OK) == -1) {
81 if (errno != ENOENT) {
82 rv = errno;
83 goto ret;
84 }
85 if (xbps_mkpath(xhp->metadir, 0755) == -1) {
86 rv = errno;
87 xbps_dbg_printf("[pkgdb] failed to create metadir "
88 "%s: %s\n", xhp->metadir, strerror(rv));
89 goto ret;
90 }
91 }
92 /* if pkgdb is unexistent, create it with an empty dictionary */
93 xhp->pkgdb = xbps_dictionary_create();
94 if (!xbps_dictionary_externalize_to_file(xhp->pkgdb, xhp->pkgdb_plist)) {
95 rv = errno;
96 xbps_dbg_printf("[pkgdb] failed to create pkgdb "
97 "%s: %s\n", xhp->pkgdb_plist, strerror(rv));
98 goto ret;
99 }
100 }
101
102 if ((pkgdb_fd = open(xhp->pkgdb_plist, O_CREAT|O_RDWR|O_CLOEXEC, 0664)) == -1) {
103 rv = errno;
104 xbps_dbg_printf("[pkgdb] cannot open pkgdb for locking "
105 "%s: %s\n", xhp->pkgdb_plist, strerror(rv));
106 free(xhp->pkgdb_plist);
107 goto ret;
108 }
109
110 /*
111 * If we've acquired the file lock, then pkgdb is writable.
112 */
113 if (lockf(pkgdb_fd, F_TLOCK, 0) == -1) {
114 rv = errno;
115 xbps_dbg_printf("[pkgdb] cannot lock pkgdb: %s\n", strerror(rv));
116 }
117 /*
118 * Check if rootdir is writable.
119 */
120 if (access(xhp->rootdir, W_OK) == -1) {
121 rv = errno;
122 xbps_dbg_printf("[pkgdb] rootdir %s: %s\n", xhp->rootdir, strerror(rv));
123 }
124
125ret:
126 umask(prev_umask);
127 return rv;
128}
129
130void
131xbps_pkgdb_unlock(struct xbps_handle *xhp UNUSED)
132{
133 xbps_dbg_printf("%s: pkgdb_fd %d\n", __func__, pkgdb_fd);
134
135 if (pkgdb_fd != -1) {
136 if (lockf(pkgdb_fd, F_ULOCK, 0) == -1)
137 xbps_dbg_printf("[pkgdb] failed to unlock pkgdb: %s\n", strerror(errno));
138
139 (void)close(pkgdb_fd);
140 pkgdb_fd = -1;
141 }
142}
143
144static int
145pkgdb_map_vpkgs(struct xbps_handle *xhp)
146{
147 xbps_object_iterator_t iter;
148 xbps_object_t obj;
149 int r = 0;
150
151 if (!xbps_dictionary_count(xhp->pkgdb))
152 return 0;
153
154 if (xhp->vpkgd == NULL) {
155 xhp->vpkgd = xbps_dictionary_create();
156 if (!xhp->vpkgd) {
157 r = -errno;
158 xbps_error_printf("failed to create dictionary\n");
159 return r;
160 }
161 }
162
163 /*
164 * This maps all pkgs that have virtualpkgs in pkgdb.
165 */
166 iter = xbps_dictionary_iterator(xhp->pkgdb);
167 if (!iter) {
168 r = -errno;
169 xbps_error_printf("failed to create iterator");
170 return r;
171 }
172
173 while ((obj = xbps_object_iterator_next(iter))) {
174 xbps_array_t provides;
175 xbps_dictionary_t pkgd;
176 const char *pkgver = NULL;
177 const char *pkgname = NULL;
178 unsigned int cnt;
179
180 pkgd = xbps_dictionary_get_keysym(xhp->pkgdb, obj);
181 provides = xbps_dictionary_get(pkgd, "provides");
182 cnt = xbps_array_count(provides);
183 if (!cnt)
184 continue;
185
186 xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
187 xbps_dictionary_get_cstring_nocopy(pkgd, "pkgname", &pkgname);
188 assert(pkgname);
189
190 for (unsigned int i = 0; i < cnt; i++) {
191 char vpkgname[XBPS_NAME_SIZE];
192 const char *vpkg = NULL;
193 xbps_dictionary_t providers;
194 bool alloc = false;
195
196 xbps_array_get_cstring_nocopy(provides, i, &vpkg);
197 if (!xbps_pkg_name(vpkgname, sizeof(vpkgname), vpkg)) {
198 xbps_warn_printf("%s: invalid provides: %s\n", pkgver, vpkg);
199 continue;
200 }
201
202 providers = xbps_dictionary_get(xhp->vpkgd, vpkgname);
203 if (!providers) {
204 providers = xbps_dictionary_create();
205 if (!providers) {
206 r = -errno;
207 xbps_error_printf("failed to create dictionary\n");
208 goto out;
209 }
210 if (!xbps_dictionary_set(xhp->vpkgd, vpkgname, providers)) {
211 r = -errno;
212 xbps_error_printf("failed to set dictionary entry\n");
213 xbps_object_release(providers);
214 goto out;
215 }
216 alloc = true;
217 }
218
219 if (!xbps_dictionary_set_cstring(providers, vpkg, pkgname)) {
220 r = -errno;
221 xbps_error_printf("failed to set dictionary entry\n");
222 if (alloc)
223 xbps_object_release(providers);
224 goto out;
225 }
226 if (alloc)
227 xbps_object_release(providers);
228 xbps_dbg_printf("[pkgdb] added vpkg %s for %s\n", vpkg, pkgname);
229 }
230 }
231out:
232 xbps_object_iterator_release(iter);
233 return r;
234}
235
236static int
237pkgdb_map_names(struct xbps_handle *xhp)
238{
239 xbps_object_iterator_t iter;
240 xbps_object_t obj;
241 int rv = 0;
242
243 if (pkgdb_map_names_done || !xbps_dictionary_count(xhp->pkgdb))
244 return 0;
245
246 /*
247 * This maps all pkgs in pkgdb to have the "pkgname" string property.
248 * This way we do it once and not multiple times.
249 */
250 iter = xbps_dictionary_iterator(xhp->pkgdb);
251 assert(iter);
252
253 while ((obj = xbps_object_iterator_next(iter))) {
254 xbps_dictionary_t pkgd;
255 const char *pkgver;
256 char pkgname[XBPS_NAME_SIZE] = {0};
257
258 pkgd = xbps_dictionary_get_keysym(xhp->pkgdb, obj);
259 if (!xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver)) {
260 continue;
261 }
262 if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
263 rv = EINVAL;
264 break;
265 }
266 if (!xbps_dictionary_set_cstring(pkgd, "pkgname", pkgname)) {
267 rv = EINVAL;
268 break;
269 }
270 }
271 xbps_object_iterator_release(iter);
272 if (!rv) {
273 pkgdb_map_names_done = true;
274 }
275 return rv;
276}
277
278int HIDDEN
279xbps_pkgdb_init(struct xbps_handle *xhp)
280{
281 int rv;
282
283 assert(xhp);
284
285 if (xhp->pkgdb)
286 return 0;
287
288 if (!xhp->pkgdb_plist)
289 xhp->pkgdb_plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_PKGDB);
290
291#if 0
292 if ((rv = xbps_pkgdb_conversion(xhp)) != 0)
293 return rv;
294#endif
295
296
297 if ((rv = xbps_pkgdb_update(xhp, false, true)) != 0) {
298 if (rv != ENOENT)
299 xbps_dbg_printf("[pkgdb] cannot internalize "
300 "pkgdb dictionary: %s\n", strerror(rv));
301
302 return rv;
303 }
304 if ((rv = pkgdb_map_names(xhp)) != 0) {
305 xbps_dbg_printf("[pkgdb] pkgdb_map_names %s\n", strerror(rv));
306 return rv;
307 }
308 if ((rv = pkgdb_map_vpkgs(xhp)) != 0) {
309 xbps_dbg_printf("[pkgdb] pkgdb_map_vpkgs %s\n", strerror(rv));
310 return rv;
311 }
312 assert(xhp->pkgdb);
313 xbps_dbg_printf("[pkgdb] initialized ok.\n");
314
315 return 0;
316}
317
318int
319xbps_pkgdb_update(struct xbps_handle *xhp, bool flush, bool update)
320{
321 xbps_dictionary_t pkgdb_storage;
322 mode_t prev_umask;
323 static int cached_rv;
324 int rv = 0;
325
326 if (cached_rv && !flush)
327 return cached_rv;
328
329 if (xhp->pkgdb && flush) {
330 pkgdb_storage = xbps_dictionary_internalize_from_file(xhp->pkgdb_plist);
331 if (pkgdb_storage == NULL ||
332 !xbps_dictionary_equals(xhp->pkgdb, pkgdb_storage)) {
333 /* flush dictionary to storage */
334 prev_umask = umask(022);
335 if (!xbps_dictionary_externalize_to_file(xhp->pkgdb, xhp->pkgdb_plist)) {
336 umask(prev_umask);
337 return errno;
338 }
339 umask(prev_umask);
340 }
341 if (pkgdb_storage)
342 xbps_object_release(pkgdb_storage);
343
344 xbps_object_release(xhp->pkgdb);
345 xhp->pkgdb = NULL;
346 cached_rv = 0;
347 }
348 if (!update)
349 return rv;
350
351 /* update copy in memory */
352 if ((xhp->pkgdb = xbps_dictionary_internalize_from_file(xhp->pkgdb_plist)) == NULL) {
353 rv = errno;
354 if (!rv)
355 rv = EINVAL;
356
357 if (rv == ENOENT)
358 xhp->pkgdb = xbps_dictionary_create();
359 else
360 xbps_error_printf("cannot access to pkgdb: %s\n", strerror(rv));
361
362 cached_rv = rv = errno;
363 }
364
365 return rv;
366}
367
368void HIDDEN
369xbps_pkgdb_release(struct xbps_handle *xhp)
370{
371 assert(xhp);
372
374 if (xhp->pkgdb)
375 xbps_object_release(xhp->pkgdb);
376 xbps_dbg_printf("[pkgdb] released ok.\n");
377}
378
379int
381 int (*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *),
382 void *arg)
383{
384 xbps_array_t allkeys;
385 int rv;
386
387 if ((rv = xbps_pkgdb_init(xhp)) != 0)
388 return rv;
389
390 allkeys = xbps_dictionary_all_keys(xhp->pkgdb);
391 assert(allkeys);
392 rv = xbps_array_foreach_cb(xhp, allkeys, xhp->pkgdb, fn, arg);
393 xbps_object_release(allkeys);
394 return rv;
395}
396
397int
399 int (*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *),
400 void *arg)
401{
402 xbps_array_t allkeys;
403 int rv;
404
405 if ((rv = xbps_pkgdb_init(xhp)) != 0)
406 return rv;
407
408 allkeys = xbps_dictionary_all_keys(xhp->pkgdb);
409 assert(allkeys);
410 rv = xbps_array_foreach_cb_multi(xhp, allkeys, xhp->pkgdb, fn, arg);
411 xbps_object_release(allkeys);
412 return rv;
413}
414
415xbps_dictionary_t
416xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
417{
418 xbps_dictionary_t pkgd;
419
420 if (xbps_pkgdb_init(xhp) != 0)
421 return NULL;
422
423 pkgd = xbps_find_pkg_in_dict(xhp->pkgdb, pkg);
424 if (!pkgd)
425 errno = ENOENT;
426 return pkgd;
427}
428
429xbps_dictionary_t
430xbps_pkgdb_get_virtualpkg(struct xbps_handle *xhp, const char *vpkg)
431{
432 if (xbps_pkgdb_init(xhp) != 0)
433 return NULL;
434
435 return xbps_find_virtualpkg_in_dict(xhp, xhp->pkgdb, vpkg);
436}
437
438static void
439generate_full_revdeps_tree(struct xbps_handle *xhp)
440{
441 xbps_object_t obj;
442 xbps_object_iterator_t iter;
443 xbps_dictionary_t vpkg_cache;
444
445 if (xhp->pkgdb_revdeps)
446 return;
447
448 xhp->pkgdb_revdeps = xbps_dictionary_create();
449 assert(xhp->pkgdb_revdeps);
450
451 vpkg_cache = xbps_dictionary_create();
452 assert(vpkg_cache);
453
454 iter = xbps_dictionary_iterator(xhp->pkgdb);
455 assert(iter);
456
457 while ((obj = xbps_object_iterator_next(iter))) {
458 xbps_array_t rundeps;
459 xbps_dictionary_t pkgd;
460 const char *pkgver = NULL;
461
462 pkgd = xbps_dictionary_get_keysym(xhp->pkgdb, obj);
463 rundeps = xbps_dictionary_get(pkgd, "run_depends");
464 if (!xbps_array_count(rundeps))
465 continue;
466
467 xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
468 for (unsigned int i = 0; i < xbps_array_count(rundeps); i++) {
469 xbps_array_t pkg;
470 const char *pkgdep = NULL, *v;
471 char curpkgname[XBPS_NAME_SIZE];
472 bool alloc = false;
473
474 xbps_array_get_cstring_nocopy(rundeps, i, &pkgdep);
475 if ((!xbps_pkgpattern_name(curpkgname, sizeof(curpkgname), pkgdep)) &&
476 (!xbps_pkg_name(curpkgname, sizeof(curpkgname), pkgdep))) {
477 abort();
478 }
479
480 /* TODO: this is kind of a workaround, to avoid calling vpkg_user_conf
481 * over and over again for the same packages which is slow. A better
482 * solution for itself vpkg_user_conf being slow should probably be
483 * implemented at some point.
484 */
485 if (!xbps_dictionary_get_cstring_nocopy(vpkg_cache, curpkgname, &v)) {
486 const char *vpkgname = vpkg_user_conf(xhp, curpkgname);
487 if (vpkgname) {
488 v = vpkgname;
489 } else {
490 v = curpkgname;
491 }
492 errno = 0;
493 if (!xbps_dictionary_set_cstring_nocopy(vpkg_cache, curpkgname, v)) {
494 xbps_error_printf("%s\n", strerror(errno ? errno : ENOMEM));
495 abort();
496 }
497 }
498
499 pkg = xbps_dictionary_get(xhp->pkgdb_revdeps, v);
500 if (pkg == NULL) {
501 alloc = true;
502 pkg = xbps_array_create();
503 }
504 if (!xbps_match_string_in_array(pkg, pkgver)) {
505 xbps_array_add_cstring_nocopy(pkg, pkgver);
506 xbps_dictionary_set(xhp->pkgdb_revdeps, v, pkg);
507 }
508 if (alloc)
509 xbps_object_release(pkg);
510 }
511 }
512 xbps_object_iterator_release(iter);
513 xbps_object_release(vpkg_cache);
514}
515
516xbps_array_t
517xbps_pkgdb_get_pkg_revdeps(struct xbps_handle *xhp, const char *pkg)
518{
519 xbps_dictionary_t pkgd;
520 const char *pkgver = NULL;
521 char pkgname[XBPS_NAME_SIZE];
522
523 if ((pkgd = xbps_pkgdb_get_pkg(xhp, pkg)) == NULL)
524 return NULL;
525
526 generate_full_revdeps_tree(xhp);
527 xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
528 if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver))
529 return NULL;
530
531 return xbps_dictionary_get(xhp->pkgdb_revdeps, pkgname);
532}
533
534xbps_array_t
535xbps_pkgdb_get_pkg_fulldeptree(struct xbps_handle *xhp, const char *pkg)
536{
537 return xbps_get_pkg_fulldeptree(xhp, pkg, false);
538}
539
540xbps_dictionary_t
541xbps_pkgdb_get_pkg_files(struct xbps_handle *xhp, const char *pkg)
542{
543 xbps_dictionary_t pkgd;
544 const char *pkgver = NULL;
545 char pkgname[XBPS_NAME_SIZE], plist[PATH_MAX];
546
547 if (pkg == NULL)
548 return NULL;
549
550 pkgd = xbps_pkgdb_get_pkg(xhp, pkg);
551 if (pkgd == NULL)
552 return NULL;
553
554 xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
555 if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver))
556 return NULL;
557
558 snprintf(plist, sizeof(plist)-1, "%s/.%s-files.plist", xhp->metadir, pkgname);
560}
char rootdir[XBPS_MAXPATH]
Definition xbps.h:659
char metadir[XBPS_MAXPATH]
Definition xbps.h:673
char * pkgdb_plist
Definition xbps.h:634
xbps_dictionary_t pkgdb
Definition xbps.h:585
Generic XBPS structure handler for initialization.
Definition xbps.h:559
xbps_array_t xbps_pkgdb_get_pkg_fulldeptree(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:535
int xbps_pkgdb_foreach_cb_multi(struct xbps_handle *xhp, int(*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *), void *arg)
Definition pkgdb.c:398
xbps_dictionary_t xbps_pkgdb_get_pkg_files(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:541
void xbps_pkgdb_unlock(struct xbps_handle *xhp)
xbps_dictionary_t xbps_pkgdb_get_virtualpkg(struct xbps_handle *xhp, const char *vpkg)
Definition pkgdb.c:430
int xbps_pkgdb_foreach_cb(struct xbps_handle *xhp, int(*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *), void *arg)
Definition pkgdb.c:380
xbps_array_t xbps_pkgdb_get_pkg_revdeps(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:517
int xbps_pkgdb_update(struct xbps_handle *xhp, bool flush, bool update)
Definition pkgdb.c:319
int xbps_pkgdb_lock(struct xbps_handle *xhp)
Definition pkgdb.c:68
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:416
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)
Definition util.c:253
int xbps_mkpath(const char *path, mode_t mode)
Definition mkpath.c:42
bool xbps_pkgpattern_name(char *dst, size_t len, const char *pattern)
Definition util.c:289