XBPS Library API 20260225
The X Binary Package System
transaction_commit.c
1/*-
2 * Copyright (c) 2009-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 <stdbool.h>
28#include <stdlib.h>
29#include <string.h>
30#include <errno.h>
31#include <unistd.h>
32#include <limits.h>
33#include <locale.h>
34
35#include "xbps_api_impl.h"
36
37/**
38 * @file lib/transaction_commit.c
39 * @brief Transaction handling routines
40 * @defgroup transaction Transaction handling functions
41 *
42 * The following image shows off the full transaction dictionary returned
43 * by xbps_transaction_prepare().
44 *
45 * @image html images/xbps_transaction_dictionary.png
46 *
47 * Legend:
48 * - <b>Salmon bg box</b>: The transaction dictionary.
49 * - <b>White bg box</b>: mandatory objects.
50 * - <b>Grey bg box</b>: optional objects.
51 * - <b>Green bg box</b>: possible value set in the object, only one of them
52 * will be set.
53 *
54 * Text inside of white boxes are the key associated with the object, its
55 * data type is specified on its edge, i.e string, array, integer, dictionary.
56 */
57
58static int
59run_post_remove_scripts(struct xbps_handle *xhp, xbps_array_t remove_scripts)
60{
61 int rv = 0;
62
63 for (unsigned int i = 0; i < xbps_array_count(remove_scripts); i++) {
64 xbps_dictionary_t dict;
65 xbps_data_t script = NULL;
66 const char *pkgver = NULL;
67 const void *buf;
68 size_t buflen;
69
70 dict = xbps_array_get(remove_scripts, i);
71 assert(dict);
72
73 xbps_dictionary_get_cstring_nocopy(dict, "pkgver", &pkgver);
74 assert(pkgver);
75
76 script = xbps_dictionary_get(dict, "remove-script");
77 assert(script);
78
79 buf = xbps_data_data_nocopy(script);
80 buflen = xbps_data_size(script);
81 rv = xbps_pkg_exec_buffer(xhp, buf, buflen, pkgver, "post", false);
82 if (rv != 0) {
83 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL, rv, pkgver,
84 "%s: [trans] REMOVE script failed to execute pre ACTION: %s",
85 pkgver, strerror(rv));
86 goto out;
87 }
88 rv = xbps_pkg_exec_buffer(xhp, buf, buflen, pkgver, "purge", false);
89 if (rv != 0) {
90 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL, rv, pkgver,
91 "%s: [trans] REMOVE script failed to execute pre ACTION: %s",
92 pkgver, strerror(rv));
93 goto out;
94 }
95 }
96
97out:
98 return rv;
99}
100
101int
103{
104 xbps_array_t remove_scripts;
105 xbps_dictionary_t pkgdb_pkgd;
106 xbps_object_t obj;
107 xbps_object_iterator_t iter;
108 xbps_trans_type_t ttype;
109 const char *pkgver = NULL, *pkgname = NULL;
110 int rv = 0;
111
112 setlocale(LC_ALL, "");
113
114 /*
115 * Store remove scripts and pkgver in this array so
116 * that we can run them after the package has been removed
117 * from the package database.
118 */
119 remove_scripts = xbps_array_create();
120 if (remove_scripts == NULL)
121 return errno ? errno : ENOMEM;
122
123 assert(xbps_object_type(xhp->transd) == XBPS_TYPE_DICTIONARY);
124 /*
125 * Create cachedir if necessary.
126 */
127 if (xbps_mkpath(xhp->cachedir, 0755) == -1) {
128 if (errno != EEXIST) {
129 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL,
130 errno, NULL,
131 "[trans] cannot create cachedir `%s': %s",
132 xhp->cachedir, strerror(errno));
133 return errno;
134 }
135 }
136 if (chdir(xhp->cachedir) == -1) {
137 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL,
138 errno, NULL,
139 "[trans] failed to change dir to cachedir `%s': %s",
140 xhp->cachedir, strerror(errno));
141 return errno;
142 }
143 iter = xbps_array_iter_from_dict(xhp->transd, "packages");
144 if (iter == NULL)
145 return EINVAL;
146
147 /*
148 * Download and verify binary packages.
149 */
150 if ((rv = xbps_transaction_fetch(xhp, iter)) != 0) {
151 xbps_dbg_printf("[trans] failed to fetch and verify binpkgs: "
152 "%s\n", strerror(rv));
153 goto out;
154 }
155 if (xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY) {
156 goto out;
157 }
158
159 /*
160 * After all downloads are finished, clear the connection cache
161 * to avoid file descriptor leaks (see #303)
162 */
163 xbps_fetch_unset_cache_connection();
164
165 /*
166 * Internalize metadata of downloaded binary packages.
167 */
168 if ((rv = xbps_transaction_internalize(xhp, iter)) < 0) {
169 xbps_dbg_printf("[trans] failed to internalize transaction binpkgs: "
170 "%s\n", strerror(-rv));
171 goto out;
172 }
173
174 /*
175 * Collect files in the transaction and find some issues
176 * like multiple packages installing the same file.
177 */
178 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FILES, 0, NULL, NULL);
179 if ((rv = xbps_transaction_files(xhp, iter)) != 0) {
180 xbps_dbg_printf("[trans] failed to verify transaction files: "
181 "%s\n", strerror(rv));
182 goto out;
183 }
184
185 /*
186 * Install, update, configure or remove packages as specified
187 * in the transaction dictionary.
188 */
189 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_RUN, 0, NULL, NULL);
190
191 /*
192 * Create rootdir if necessary.
193 */
194 if (xbps_mkpath(xhp->rootdir, 0750) == -1) {
195 rv = errno;
196 if (rv != EEXIST) {
197 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL, errno, xhp->rootdir,
198 "[trans] failed to create rootdir `%s': %s",
199 xhp->rootdir, strerror(rv));
200 goto out;
201 }
202 }
203 if (chdir(xhp->rootdir) == -1) {
204 rv = errno;
205 xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, xhp->rootdir,
206 "[trans] failed to chdir to rootdir `%s': %s",
207 xhp->rootdir, strerror(errno));
208 goto out;
209 }
210
211 /*
212 * Run all pre-remove scripts.
213 * And store the remove scripts in remove_scripts
214 * so we can execute the post and purge actions
215 * after the package is removed from the pkgdb.
216 */
217 while ((obj = xbps_object_iterator_next(iter)) != NULL) {
218 xbps_dictionary_t dict;
219 xbps_data_t script = NULL;
220 const char *pkgdb_pkgver;
221 bool update;
222
223 xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
224 xbps_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
225
226 ttype = xbps_transaction_pkg_type(obj);
227 if (ttype == XBPS_TRANS_INSTALL || ttype == XBPS_TRANS_HOLD || ttype == XBPS_TRANS_CONFIGURE) {
228 xbps_dbg_printf("%s: skipping pre-remove script for "
229 "%s: %d\n", __func__, pkgver, ttype);
230 continue;
231 }
232
233 if ((pkgdb_pkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) {
234 bool replaced = false;
235 xbps_dictionary_get_bool(obj, "replaced", &replaced);
236 if (replaced) {
237 continue;
238 }
239 rv = ENOENT;
240 xbps_dbg_printf("[trans] cannot find %s in pkgdb: %s\n",
241 pkgname, strerror(rv));
242 goto out;
243 }
244
245 script = xbps_dictionary_get(pkgdb_pkgd, "remove-script");
246 if (script == NULL)
247 continue;
248
249 xbps_dictionary_get_cstring_nocopy(pkgdb_pkgd, "pkgver", &pkgdb_pkgver);
250 assert(pkgdb_pkgver);
251
252 update = ttype == XBPS_TRANS_UPDATE;
253
254 rv = xbps_pkg_exec_script(xhp, pkgdb_pkgd, "remove-script", "pre", update);
255 if (rv != 0) {
256 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL, rv, pkgver,
257 "%s: [trans] REMOVE script failed to execute pre ACTION: %s",
258 pkgver, strerror(rv));
259 goto out;
260 }
261 if (update)
262 continue;
263
264 dict = xbps_dictionary_create();
265 if (dict == NULL) {
266 rv = errno ? errno : ENOMEM;
267 goto out;
268 }
269 if (!xbps_dictionary_set_cstring(dict, "pkgver", pkgdb_pkgver)) {
270 rv = errno ? errno : ENOMEM;
271 goto out;
272 }
273 if (!xbps_dictionary_set(dict, "remove-script", script)) {
274 rv = errno ? errno : ENOMEM;
275 goto out;
276 }
277 if (!xbps_array_add(remove_scripts, dict)) {
278 rv = errno ? errno : ENOMEM;
279 goto out;
280 }
281 xbps_object_release(dict);
282 }
283 xbps_object_iterator_reset(iter);
284
285 /*
286 * Run all pre-install scripts.
287 */
288 while ((obj = xbps_object_iterator_next(iter)) != NULL) {
289 xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
290 ttype = xbps_transaction_pkg_type(obj);
291 if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD) {
292 xbps_dbg_printf("%s: skipping pre-install script for "
293 "%s: %d\n", __func__, pkgver, ttype);
294 continue;
295 }
296 rv = xbps_pkg_exec_script(xhp, obj, "install-script", "pre", ttype == XBPS_TRANS_UPDATE);
297 if (rv != 0) {
298 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL, rv, pkgver,
299 "%s: [trans] INSTALL script failed to execute pre ACTION: %s",
300 pkgver, strerror(rv));
301 goto out;
302 }
303 }
304 xbps_object_iterator_reset(iter);
305
306
307 while ((obj = xbps_object_iterator_next(iter)) != NULL) {
308 xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
309 xbps_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
310
311 ttype = xbps_transaction_pkg_type(obj);
312 if (ttype == XBPS_TRANS_REMOVE) {
313 bool replaced = false;
314 bool update = false;
315 /*
316 * Remove package.
317 */
318 xbps_dictionary_get_bool(obj, "remove-and-update", &update);
319 xbps_dictionary_get_bool(obj, "replaced", &replaced);
320 if (replaced && !xbps_pkgdb_get_pkg(xhp, pkgname)) {
321 continue;
322 }
323 rv = xbps_remove_pkg(xhp, pkgver, update);
324 if (rv != 0) {
325 xbps_dbg_printf("[trans] failed to "
326 "remove %s: %s\n", pkgver, strerror(rv));
327 goto out;
328 }
329 continue;
330
331 } else if (ttype == XBPS_TRANS_UPDATE) {
332 /*
333 * Update a package: execute pre-remove action of
334 * existing package before unpacking new version.
335 */
336 xbps_set_cb_state(xhp, XBPS_STATE_UPDATE, 0, pkgver, NULL);
337 rv = xbps_remove_pkg(xhp, pkgver, true);
338 if (rv != 0) {
339 xbps_set_cb_state(xhp,
340 XBPS_STATE_UPDATE_FAIL,
341 rv, pkgver,
342 "%s: [trans] failed to update "
343 "package `%s'", pkgver,
344 strerror(rv));
345 goto out;
346 }
347 } else if (ttype == XBPS_TRANS_CONFIGURE) {
348 /*
349 * Package just needs to be configured, ignore it.
350 */
351 continue;
352 } else if (ttype == XBPS_TRANS_HOLD) {
353 /*
354 * Package is on hold mode, ignore it.
355 */
356 continue;
357 } else {
358 /* Install or reinstall package */
359 xbps_set_cb_state(xhp, XBPS_STATE_INSTALL, 0,
360 pkgver, NULL);
361 }
362 /*
363 * Unpack binary package.
364 */
365 if ((rv = xbps_unpack_binary_pkg(xhp, obj)) != 0) {
366 xbps_dbg_printf("[trans] failed to unpack "
367 "%s: %s\n", pkgver, strerror(rv));
368 goto out;
369 }
370 /*
371 * Register package.
372 */
373 if ((rv = xbps_register_pkg(xhp, obj)) != 0) {
374 xbps_dbg_printf("[trans] failed to register "
375 "%s: %s\n", pkgver, strerror(rv));
376 goto out;
377 }
378 }
379 /* if there are no packages to install or update we are done */
380 if (!xbps_dictionary_get(xhp->transd, "total-update-pkgs") &&
381 !xbps_dictionary_get(xhp->transd, "total-install-pkgs"))
382 goto out;
383
384 if (xhp->target_arch && strcmp(xhp->native_arch, xhp->target_arch)) {
385 /* if installing packages for target_arch, don't configure anything */
386 goto out;
387 /* do not configure packages if only unpacking is desired */
388 } else if (xhp->flags & XBPS_FLAG_UNPACK_ONLY) {
389 goto out;
390 }
391
392 xbps_object_iterator_reset(iter);
393 /* Force a pkgdb write for all unpacked pkgs in transaction */
394 if ((rv = xbps_pkgdb_update(xhp, true, true)) != 0)
395 goto out;
396
397 /*
398 * Run all post and purge-remove scripts.
399 */
400 rv = run_post_remove_scripts(xhp, remove_scripts);
401 if (rv < 0) {
402 rv = -rv;
403 goto out;
404 }
405
406 /*
407 * Configure all unpacked packages (post-install).
408 */
409 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_CONFIGURE, 0, NULL, NULL);
410
411 while ((obj = xbps_object_iterator_next(iter)) != NULL) {
412 bool update;
413
414 xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
415 ttype = xbps_transaction_pkg_type(obj);
416 if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD) {
417 xbps_dbg_printf("%s: skipping configuration for "
418 "%s: %d\n", __func__, pkgver, ttype);
419 continue;
420 }
421 update = ttype == XBPS_TRANS_UPDATE;
422
423 rv = xbps_configure_pkg(xhp, pkgver, false, update);
424 if (rv != 0) {
425 xbps_dbg_printf("%s: configure failed for "
426 "%s: %s\n", __func__, pkgver, strerror(rv));
427 goto out;
428 }
429 /*
430 * Notify client callback when a package has been
431 * installed or updated.
432 */
433 if (update) {
434 xbps_set_cb_state(xhp, XBPS_STATE_UPDATE_DONE, 0,
435 pkgver, NULL);
436 } else {
437 xbps_set_cb_state(xhp, XBPS_STATE_INSTALL_DONE, 0,
438 pkgver, NULL);
439 }
440 }
441
442out:
443 xbps_object_release(remove_scripts);
444 xbps_object_iterator_release(iter);
445 if (rv == 0) {
446 /* Force a pkgdb write for all unpacked pkgs in transaction */
447 rv = xbps_pkgdb_update(xhp, true, true);
448 }
449 return rv;
450}
int xbps_configure_pkg(struct xbps_handle *xhp, const char *pkgname, bool check_state, bool update)
char rootdir[XBPS_MAXPATH]
Definition xbps.h:664
xbps_dictionary_t transd
Definition xbps.h:597
const char * target_arch
Definition xbps.h:645
int flags
Definition xbps.h:693
char native_arch[64]
Definition xbps.h:685
char cachedir[XBPS_MAXPATH]
Definition xbps.h:671
Generic XBPS structure handler for initialization.
Definition xbps.h:560
void xbps_dbg_printf(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:67
int xbps_pkg_exec_buffer(struct xbps_handle *xhp, const void *blob, const size_t blobsiz, const char *pkgver, const char *action, bool update)
int xbps_pkgdb_update(struct xbps_handle *xhp, bool flush, bool update)
Definition pkgdb.c:288
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:389
int xbps_pkg_exec_script(struct xbps_handle *xhp, xbps_dictionary_t d, const char *script, const char *action, bool update)
xbps_object_iterator_t xbps_array_iter_from_dict(xbps_dictionary_t dict, const char *key)
Definition plist.c:232
int xbps_transaction_commit(struct xbps_handle *xhp)
xbps_trans_type_t xbps_transaction_pkg_type(xbps_dictionary_t pkg_repod)
xbps_trans_type_t
Definition xbps.h:1400
int xbps_mkpath(const char *path, mode_t mode)
Definition mkpath.c:42