XBPS Library API 20240111
The X Binary Package System
package_configure.c
1/*-
2 * Copyright (c) 2009-2015 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/stat.h>
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <errno.h>
32
33#include "xbps_api_impl.h"
34
35/**
36 * @file lib/package_configure.c
37 * @brief Package configuration routines
38 * @defgroup configure Package configuration functions
39 *
40 * Configure a package or all packages. Only packages in XBPS_PKG_STATE_UNPACKED
41 * state will be processed (unless overriden). Package configuration steps:
42 * - Its <b>post-install</b> target in the INSTALL script will be executed.
43 * - Its state will be changed to XBPS_PKG_STATE_INSTALLED if previous step
44 * ran successful.
45 *
46 * @note
47 * If the \a XBPS_FLAG_FORCE_CONFIGURE is set through xbps_init() in the flags
48 member, the package (or packages) will be reconfigured even if its
49 * state is XBPS_PKG_STATE_INSTALLED.
50 */
51
52int
53xbps_configure_packages(struct xbps_handle *xhp, xbps_array_t ignpkgs)
54{
55 xbps_dictionary_t pkgd;
56 xbps_object_t obj;
57 xbps_object_iterator_t iter;
58 const char *pkgver;
59 int rv;
60
61 if ((rv = xbps_pkgdb_init(xhp)) != 0)
62 return rv;
63
64 iter = xbps_dictionary_iterator(xhp->pkgdb);
65 assert(iter);
66 while ((obj = xbps_object_iterator_next(iter))) {
67 pkgd = xbps_dictionary_get_keysym(xhp->pkgdb, obj);
68 if (!xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver))
69 continue;
70 if (xbps_array_count(ignpkgs)) {
71 if ((xbps_match_string_in_array(ignpkgs, pkgver)) ||
72 (xbps_match_pkgver_in_array(ignpkgs, pkgver))) {
73 xbps_dbg_printf("%s: ignoring pkg %s\n",
74 __func__, pkgver);
75 continue;
76 }
77 }
78 rv = xbps_configure_pkg(xhp, pkgver, true, false);
79 if (rv != 0) {
80 xbps_dbg_printf("%s: failed to configure %s: %s\n",
81 __func__, pkgver, strerror(rv));
82 break;
83 }
84 }
85 xbps_object_iterator_release(iter);
86
87 return rv;
88}
89
90int
92 const char *pkgver,
93 bool check_state,
94 bool update)
95{
96 xbps_dictionary_t pkgd;
97 const char *p;
98 char pkgname[XBPS_NAME_SIZE];
99 int rv = 0;
100 pkg_state_t state = 0;
101 mode_t myumask;
102
103 assert(pkgver != NULL);
104
105 if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
106 p = pkgver;
107 } else {
108 p = pkgname;
109 }
110
111 pkgd = xbps_pkgdb_get_pkg(xhp, p);
112 if (pkgd == NULL) {
113 xbps_dbg_printf("[configure] cannot find %s (%s) "
114 "in pkgdb\n", p, pkgver);
115 return ENOENT;
116 }
117
118 rv = xbps_pkg_state_dictionary(pkgd, &state);
119 xbps_dbg_printf("%s: state %d rv %d\n", pkgver, state, rv);
120 if (rv != 0) {
121 xbps_dbg_printf("%s: [configure] failed to get "
122 "pkg state: %s\n", pkgver, strerror(rv));
123 return EINVAL;
124 }
125
126 if (check_state) {
127 if (state == XBPS_PKG_STATE_INSTALLED) {
128 if ((xhp->flags & XBPS_FLAG_FORCE_CONFIGURE) == 0) {
129 return 0;
130 }
131 } else if (state != XBPS_PKG_STATE_UNPACKED) {
132 return EINVAL;
133 }
134 }
135
136 myumask = umask(022);
137
138 xbps_set_cb_state(xhp, XBPS_STATE_CONFIGURE, 0, pkgver, NULL);
139
140 rv = xbps_pkg_exec_script(xhp, pkgd, "install-script", "post", update);
141 if (rv != 0) {
142 xbps_set_cb_state(xhp, XBPS_STATE_CONFIGURE_FAIL,
143 errno, pkgver,
144 "%s: [configure] INSTALL script failed to execute "
145 "the post ACTION: %s", pkgver, strerror(rv));
146 umask(myumask);
147 return rv;
148 }
149 rv = xbps_set_pkg_state_dictionary(pkgd, XBPS_PKG_STATE_INSTALLED);
150 if (rv != 0) {
151 xbps_set_cb_state(xhp, XBPS_STATE_CONFIGURE_FAIL, rv,
152 pkgver, "%s: [configure] failed to set state to installed: %s",
153 pkgver, strerror(rv));
154 umask(myumask);
155 return rv;
156 }
157 if (rv == 0)
158 xbps_set_cb_state(xhp, XBPS_STATE_CONFIGURE_DONE, 0, pkgver, NULL);
159
160 umask(myumask);
161 /* show install-msg if exists */
162 return xbps_cb_message(xhp, pkgd, "install-msg");
163}
int xbps_configure_pkg(struct xbps_handle *xhp, const char *pkgver, bool check_state, bool update)
int xbps_configure_packages(struct xbps_handle *xhp, xbps_array_t ignpkgs)
int flags
Definition xbps.h:679
xbps_dictionary_t pkgdb
Definition xbps.h:576
Generic XBPS structure handler for initialization.
Definition xbps.h:550
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:379
int xbps_pkg_exec_script(struct xbps_handle *xhp, xbps_dictionary_t d, const char *script, const char *action, bool update)
int xbps_set_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t state)
pkg_state_t
Definition xbps.h:1768
int xbps_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t *state)
bool xbps_match_pkgver_in_array(xbps_array_t array, const char *pkgver)
bool xbps_match_string_in_array(xbps_array_t array, const char *val)
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
Definition util.c:253