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