XBPS Library API 20240111
The X Binary Package System
package_register.c
1/*-
2 * Copyright (c) 2008-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 <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
30#include <time.h>
31
32#include "xbps_api_impl.h"
33
34int HIDDEN
35xbps_register_pkg(struct xbps_handle *xhp, xbps_dictionary_t pkgrd)
36{
37 xbps_array_t replaces;
38 xbps_dictionary_t pkgd;
39 time_t t;
40 struct tm tm, *tmp;
41 const char *pkgver, *pkgname;
42 char sha256[XBPS_SHA256_SIZE], outstr[64], *buf;
43 int rv = 0;
44
45 assert(xbps_object_type(pkgrd) == XBPS_TYPE_DICTIONARY);
46
47 xbps_dictionary_make_immutable(pkgrd);
48 if ((pkgd = xbps_dictionary_copy_mutable(pkgrd)) == NULL) {
49 goto out;
50 }
51
52 xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
53 xbps_dictionary_get_cstring_nocopy(pkgd, "pkgname", &pkgname);
54
55 if (xhp->flags & XBPS_FLAG_INSTALL_REPRO) {
56 /*
57 * Reproducible mode. Some objects must not be recorded:
58 * - install-date
59 * - repository
60 */
61 xbps_dictionary_remove(pkgd, "repository");
62 } else {
63 /*
64 * Set the "install-date" object to know the pkg installation date.
65 */
66 t = time(NULL);
67 if ((tmp = localtime_r(&t, &tm)) == NULL) {
68 xbps_dbg_printf("%s: localtime_r failed: %s\n",
69 pkgver, strerror(errno));
70 rv = EINVAL;
71 goto out;
72 }
73 if (strftime(outstr, sizeof(outstr)-1, "%F %R %Z", tmp) == 0) {
74 xbps_dbg_printf("%s: strftime failed: %s\n",
75 pkgver, strerror(errno));
76 rv = EINVAL;
77 goto out;
78 }
79 if (!xbps_dictionary_set_cstring(pkgd, "install-date", outstr)) {
80 xbps_dbg_printf("%s: install-date set failed!\n", pkgver);
81 rv = EINVAL;
82 goto out;
83 }
84 }
85 /*
86 * Create a hash for the pkg's metafile if it exists.
87 */
88 buf = xbps_xasprintf("%s/.%s-files.plist", xhp->metadir, pkgname);
89 if (xbps_file_sha256(sha256, sizeof sha256, buf)) {
90 xbps_dictionary_set_cstring(pkgd, "metafile-sha256", sha256);
91 }
92 free(buf);
93 /*
94 * Remove self replacement when applicable.
95 */
96 if ((replaces = xbps_dictionary_get(pkgd, "replaces"))) {
97 buf = xbps_xasprintf("%s>=0", pkgname);
98 xbps_remove_string_from_array(replaces, buf);
99 free(buf);
100 if (!xbps_array_count(replaces))
101 xbps_dictionary_remove(pkgd, "replaces");
102 }
103 /*
104 * Remove unneeded objs from pkg dictionary.
105 */
106 xbps_dictionary_remove(pkgd, "download");
107 xbps_dictionary_remove(pkgd, "remove-and-update");
108 xbps_dictionary_remove(pkgd, "transaction");
109 xbps_dictionary_remove(pkgd, "skip-obsoletes");
110 xbps_dictionary_remove(pkgd, "pkgname");
111 xbps_dictionary_remove(pkgd, "version");
112
113 if (!xbps_dictionary_set(xhp->pkgdb, pkgname, pkgd)) {
114 xbps_dbg_printf("%s: failed to set pkgd for %s\n", __func__, pkgver);
115 }
116out:
117 xbps_object_release(pkgd);
118
119 return rv;
120}
char metadir[XBPS_MAXPATH+sizeof(XBPS_META_PATH)]
Definition xbps.h:664
int flags
Definition xbps.h:679
xbps_dictionary_t pkgdb
Definition xbps.h:576
Generic XBPS structure handler for initialization.
Definition xbps.h:550
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
bool xbps_file_sha256(char *dst, size_t len, const char *file)
Definition util_hash.c:148
bool xbps_remove_string_from_array(xbps_array_t array, const char *str)