XBPS Library API 20250705
The X Binary Package System
transaction_prepare.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 <sys/statvfs.h>
32
33#include "xbps_api_impl.h"
34
35/**
36 * @file lib/transaction_prepare.c
37 * @brief Transaction handling routines
38 * @defgroup transaction Transaction handling functions
39 *
40 * The following image shows off the full transaction dictionary returned
41 * by xbps_transaction_prepare().
42 *
43 * @image html images/xbps_transaction_dictionary.png
44 *
45 * Legend:
46 * - <b>Salmon bg box</b>: The transaction dictionary.
47 * - <b>White bg box</b>: mandatory objects.
48 * - <b>Grey bg box</b>: optional objects.
49 * - <b>Green bg box</b>: possible value set in the object, only one of them
50 * will be set.
51 *
52 * Text inside of white boxes are the key associated with the object, its
53 * data type is specified on its edge, i.e string, array, integer, dictionary.
54 */
55
56static int
57compute_transaction_stats(struct xbps_handle *xhp)
58{
59 xbps_dictionary_t pkg_metad;
60 xbps_object_iterator_t iter;
61 xbps_object_t obj;
62 struct statvfs svfs;
63 uint64_t rootdir_free_size, tsize, dlsize, instsize, rmsize;
64 uint32_t inst_pkgcnt, up_pkgcnt, cf_pkgcnt, rm_pkgcnt, dl_pkgcnt;
65 uint32_t hold_pkgcnt;
66
67 inst_pkgcnt = up_pkgcnt = cf_pkgcnt = rm_pkgcnt = 0;
68 hold_pkgcnt = dl_pkgcnt = 0;
69 tsize = dlsize = instsize = rmsize = 0;
70
71 iter = xbps_array_iter_from_dict(xhp->transd, "packages");
72 if (iter == NULL)
73 return EINVAL;
74
75 while ((obj = xbps_object_iterator_next(iter)) != NULL) {
76 const char *pkgver = NULL, *repo = NULL, *pkgname = NULL;
77 bool preserve = false;
79 /*
80 * Count number of pkgs to be removed, configured,
81 * installed and updated.
82 */
83 xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
84 xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgname);
85 xbps_dictionary_get_cstring_nocopy(obj, "repository", &repo);
86 xbps_dictionary_get_bool(obj, "preserve", &preserve);
87 ttype = xbps_transaction_pkg_type(obj);
88
89 if (ttype == XBPS_TRANS_REMOVE) {
90 rm_pkgcnt++;
91 } else if (ttype == XBPS_TRANS_CONFIGURE) {
92 cf_pkgcnt++;
93 } else if (ttype == XBPS_TRANS_INSTALL || ttype == XBPS_TRANS_REINSTALL) {
94 inst_pkgcnt++;
95 } else if (ttype == XBPS_TRANS_UPDATE) {
96 up_pkgcnt++;
97 } else if (ttype == XBPS_TRANS_HOLD) {
98 hold_pkgcnt++;
99 }
100
101 if ((ttype != XBPS_TRANS_CONFIGURE) && (ttype != XBPS_TRANS_REMOVE) &&
102 (ttype != XBPS_TRANS_HOLD) &&
103 xbps_repository_is_remote(repo) && !xbps_binpkg_exists(xhp, obj)) {
104 xbps_dictionary_get_uint64(obj, "filename-size", &tsize);
105 tsize += 512;
106 dlsize += tsize;
107 dl_pkgcnt++;
108 xbps_dictionary_set_bool(obj, "download", true);
109 }
110 if (xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY) {
111 continue;
112 }
113 /* installed_size from repo */
114 if (ttype != XBPS_TRANS_REMOVE && ttype != XBPS_TRANS_HOLD &&
115 ttype != XBPS_TRANS_CONFIGURE) {
116 xbps_dictionary_get_uint64(obj, "installed_size", &tsize);
117 instsize += tsize;
118 }
119 /*
120 * If removing or updating a package without preserve,
121 * get installed_size from pkgdb instead.
122 */
123 if (ttype == XBPS_TRANS_REMOVE ||
124 ((ttype == XBPS_TRANS_UPDATE) && !preserve)) {
125 pkg_metad = xbps_pkgdb_get_pkg(xhp, pkgname);
126 if (pkg_metad == NULL)
127 continue;
128 xbps_dictionary_get_uint64(pkg_metad,
129 "installed_size", &tsize);
130 rmsize += tsize;
131 }
132 }
133 xbps_object_iterator_release(iter);
134
135 if (instsize > rmsize) {
136 instsize -= rmsize;
137 rmsize = 0;
138 } else if (rmsize > instsize) {
139 rmsize -= instsize;
140 instsize = 0;
141 } else {
142 instsize = rmsize = 0;
143 }
144
145 if (!xbps_dictionary_set_uint32(xhp->transd,
146 "total-install-pkgs", inst_pkgcnt))
147 return EINVAL;
148 if (!xbps_dictionary_set_uint32(xhp->transd,
149 "total-update-pkgs", up_pkgcnt))
150 return EINVAL;
151 if (!xbps_dictionary_set_uint32(xhp->transd,
152 "total-configure-pkgs", cf_pkgcnt))
153 return EINVAL;
154 if (!xbps_dictionary_set_uint32(xhp->transd,
155 "total-remove-pkgs", rm_pkgcnt))
156 return EINVAL;
157 if (!xbps_dictionary_set_uint32(xhp->transd,
158 "total-download-pkgs", dl_pkgcnt))
159 return EINVAL;
160 if (!xbps_dictionary_set_uint32(xhp->transd,
161 "total-hold-pkgs", hold_pkgcnt))
162 return EINVAL;
163 if (!xbps_dictionary_set_uint64(xhp->transd,
164 "total-installed-size", instsize))
165 return EINVAL;
166 if (!xbps_dictionary_set_uint64(xhp->transd,
167 "total-download-size", dlsize))
168 return EINVAL;
169 if (!xbps_dictionary_set_uint64(xhp->transd,
170 "total-removed-size", rmsize))
171 return EINVAL;
172
173 /* Get free space from target rootdir: return ENOSPC if there's not enough space */
174 if (statvfs(xhp->rootdir, &svfs) == -1) {
175 xbps_dbg_printf("%s: statvfs failed: %s\n", __func__, strerror(errno));
176 return 0;
177 }
178 /* compute free space on disk */
179 rootdir_free_size = svfs.f_bfree * svfs.f_bsize;
180
181 if (!xbps_dictionary_set_uint64(xhp->transd,
182 "disk-free-size", rootdir_free_size))
183 return EINVAL;
184
185 if (instsize > rootdir_free_size)
186 return ENOSPC;
187
188 return 0;
189}
190
191int HIDDEN
192xbps_transaction_init(struct xbps_handle *xhp)
193{
194 xbps_array_t array;
195 xbps_dictionary_t dict;
196
197 if (xhp->transd != NULL)
198 return 0;
199
200 if ((xhp->transd = xbps_dictionary_create()) == NULL)
201 return xbps_error_oom();
202
203 if ((array = xbps_array_create()) == NULL) {
204 xbps_object_release(xhp->transd);
205 xhp->transd = NULL;
206 return xbps_error_oom();
207 }
208 if (!xbps_dictionary_set(xhp->transd, "packages", array)) {
209 xbps_object_release(xhp->transd);
210 xhp->transd = NULL;
211 return EINVAL;
212 }
213 xbps_object_release(array);
214
215 if ((array = xbps_array_create()) == NULL) {
216 xbps_object_release(xhp->transd);
217 xhp->transd = NULL;
218 return xbps_error_oom();
219 }
220 if (!xbps_dictionary_set(xhp->transd, "missing_deps", array)) {
221 xbps_object_release(xhp->transd);
222 xhp->transd = NULL;
223 return EINVAL;
224 }
225 xbps_object_release(array);
226
227 if ((array = xbps_array_create()) == NULL) {
228 xbps_object_release(xhp->transd);
229 xhp->transd = NULL;
230 return xbps_error_oom();
231 }
232 if (!xbps_dictionary_set(xhp->transd, "missing_shlibs", array)) {
233 xbps_object_release(xhp->transd);
234 xhp->transd = NULL;
235 return xbps_error_oom();
236 }
237 xbps_object_release(array);
238
239 if ((array = xbps_array_create()) == NULL) {
240 xbps_object_release(xhp->transd);
241 xhp->transd = NULL;
242 return xbps_error_oom();
243 }
244 if (!xbps_dictionary_set(xhp->transd, "conflicts", array)) {
245 xbps_object_release(xhp->transd);
246 xhp->transd = NULL;
247 return xbps_error_oom();
248 }
249 xbps_object_release(array);
250
251 if ((dict = xbps_dictionary_create()) == NULL) {
252 xbps_object_release(xhp->transd);
253 xhp->transd = NULL;
254 return xbps_error_oom();
255 }
256 if (!xbps_dictionary_set(xhp->transd, "obsolete_files", dict)) {
257 xbps_object_release(xhp->transd);
258 xhp->transd = NULL;
259 return xbps_error_oom();
260 }
261 xbps_object_release(dict);
262
263 if ((dict = xbps_dictionary_create()) == NULL) {
264 xbps_object_release(xhp->transd);
265 xhp->transd = NULL;
266 return xbps_error_oom();
267 }
268 if (!xbps_dictionary_set(xhp->transd, "remove_files", dict)) {
269 xbps_object_release(xhp->transd);
270 xhp->transd = NULL;
271 return xbps_error_oom();
272 }
273 xbps_object_release(dict);
274
275 return 0;
276}
277
278int
280{
281 xbps_array_t pkgs, edges;
282 xbps_dictionary_t tpkgd;
283 xbps_trans_type_t ttype;
284 unsigned int i, cnt;
285 int rv = 0;
286 int r;
287 bool all_on_hold = true;
288
289 if ((rv = xbps_transaction_init(xhp)) != 0)
290 return rv;
291
292 if (xhp->transd == NULL)
293 return ENXIO;
294
295 /*
296 * Collect dependencies for pkgs in transaction.
297 */
298 if ((edges = xbps_array_create()) == NULL)
299 return ENOMEM;
300
301 xbps_dbg_printf("%s: processing deps\n", __func__);
302 /*
303 * The edges are also appended after its dependencies have been
304 * collected; the edges at the original array are removed later.
305 */
306 pkgs = xbps_dictionary_get(xhp->transd, "packages");
307 assert(xbps_object_type(pkgs) == XBPS_TYPE_ARRAY);
308 cnt = xbps_array_count(pkgs);
309 for (i = 0; i < cnt; i++) {
310 xbps_dictionary_t pkgd;
311 xbps_string_t str;
312
313 pkgd = xbps_array_get(pkgs, i);
314 str = xbps_dictionary_get(pkgd, "pkgver");
315 ttype = xbps_transaction_pkg_type(pkgd);
316
317 if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD)
318 continue;
319
320 assert(xbps_object_type(str) == XBPS_TYPE_STRING);
321
322 if (!xbps_array_add(edges, str)) {
323 xbps_object_release(edges);
324 return ENOMEM;
325 }
326 if ((rv = xbps_transaction_pkg_deps(xhp, pkgs, pkgd)) != 0) {
327 xbps_object_release(edges);
328 return rv;
329 }
330 if (!xbps_array_add(pkgs, pkgd)) {
331 xbps_object_release(edges);
332 return ENOMEM;
333 }
334 }
335 /* ... remove dup edges at head */
336 for (i = 0; i < xbps_array_count(edges); i++) {
337 const char *pkgver = NULL;
338 xbps_array_get_cstring_nocopy(edges, i, &pkgver);
339 xbps_remove_pkg_from_array_by_pkgver(pkgs, pkgver);
340 }
341 xbps_object_release(edges);
342
343 /*
344 * Do not perform any checks if XBPS_FLAG_DOWNLOAD_ONLY
345 * is set. We just need to download the archives (dependencies).
346 */
347 if (xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY)
348 goto out;
349
350 /*
351 * If all pkgs in transaction are on hold, no need to check
352 * for anything else.
353 */
354 xbps_dbg_printf("%s: checking on hold pkgs\n", __func__);
355 for (i = 0; i < cnt; i++) {
356 tpkgd = xbps_array_get(pkgs, i);
357 if (xbps_transaction_pkg_type(tpkgd) != XBPS_TRANS_HOLD) {
358 all_on_hold = false;
359 break;
360 }
361 }
362 if (all_on_hold)
363 goto out;
364
365 /*
366 * Check for packages to be replaced.
367 */
368 xbps_dbg_printf("%s: checking replaces\n", __func__);
369 if (!xbps_transaction_check_replaces(xhp, pkgs)) {
370 xbps_object_release(xhp->transd);
371 xhp->transd = NULL;
372 return EINVAL;
373 }
374 /*
375 * Check if there are missing revdeps.
376 */
377 xbps_dbg_printf("%s: checking revdeps\n", __func__);
378 if (!xbps_transaction_check_revdeps(xhp, pkgs)) {
379 xbps_object_release(xhp->transd);
380 xhp->transd = NULL;
381 return EINVAL;
382 }
383 if (xbps_dictionary_get(xhp->transd, "missing_deps")) {
384 if (xhp->flags & XBPS_FLAG_FORCE_REMOVE_REVDEPS) {
385 xbps_dbg_printf("[trans] continuing with broken reverse dependencies!");
386 } else {
387 return ENODEV;
388 }
389 }
390 /*
391 * Check for package conflicts.
392 */
393 xbps_dbg_printf("%s: checking conflicts\n", __func__);
394 r = xbps_transaction_check_conflicts(xhp, pkgs);
395 if (r < 0) {
396 xbps_object_release(xhp->transd);
397 xhp->transd = NULL;
398 return -r;
399 }
400 if (xbps_dictionary_get(xhp->transd, "conflicts")) {
401 return EAGAIN;
402 }
403 /*
404 * Check for unresolved shared libraries.
405 */
406 xbps_dbg_printf("%s: checking shlibs\n", __func__);
407 if (!xbps_transaction_check_shlibs(xhp, pkgs)) {
408 xbps_object_release(xhp->transd);
409 xhp->transd = NULL;
410 return EINVAL;
411 }
412 if (xbps_dictionary_get(xhp->transd, "missing_shlibs")) {
413 if (xhp->flags & XBPS_FLAG_FORCE_REMOVE_REVDEPS) {
414 xbps_dbg_printf("[trans] continuing with unresolved shared libraries!");
415 } else {
416 return ENOEXEC;
417 }
418 }
419out:
420 /*
421 * Add transaction stats for total download/installed size,
422 * number of packages to be installed, updated, configured
423 * and removed to the transaction dictionary.
424 */
425 xbps_dbg_printf("%s: computing stats\n", __func__);
426 if ((rv = compute_transaction_stats(xhp)) != 0) {
427 return rv;
428 }
429 /*
430 * Make transaction dictionary immutable.
431 */
432 xbps_dictionary_make_immutable(xhp->transd);
433
434 return 0;
435}
char rootdir[XBPS_MAXPATH]
Definition xbps.h:664
xbps_dictionary_t transd
Definition xbps.h:597
int flags
Definition xbps.h:693
Generic XBPS structure handler for initialization.
Definition xbps.h:560
#define xbps_error_oom()
Log out of memory condition.
Definition xbps.h:776
void xbps_dbg_printf(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:72
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:401
xbps_object_iterator_t xbps_array_iter_from_dict(xbps_dictionary_t dict, const char *key)
Definition plist.c:232
xbps_trans_type_t xbps_transaction_pkg_type(xbps_dictionary_t pkg_repod)
xbps_trans_type_t
Definition xbps.h:1390
int xbps_transaction_prepare(struct xbps_handle *xhp)
bool xbps_repository_is_remote(const char *uri)
Definition util.c:66
bool xbps_binpkg_exists(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
Definition util.c:429