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