XBPS Library API 20240111
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 ENOMEM;
202
203 if ((array = xbps_array_create()) == NULL) {
204 xbps_object_release(xhp->transd);
205 xhp->transd = NULL;
206 return ENOMEM;
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 ENOMEM;
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 ENOMEM;
231 }
232 if (!xbps_dictionary_set(xhp->transd, "missing_shlibs", array)) {
233 xbps_object_release(xhp->transd);
234 xhp->transd = NULL;
235 return EINVAL;
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 ENOMEM;
243 }
244 if (!xbps_dictionary_set(xhp->transd, "conflicts", array)) {
245 xbps_object_release(xhp->transd);
246 xhp->transd = NULL;
247 return EINVAL;
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 ENOMEM;
255 }
256 if (!xbps_dictionary_set(xhp->transd, "obsolete_files", dict)) {
257 xbps_object_release(xhp->transd);
258 xhp->transd = NULL;
259 return EINVAL;
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 ENOMEM;
267 }
268 if (!xbps_dictionary_set(xhp->transd, "remove_files", dict)) {
269 xbps_object_release(xhp->transd);
270 xhp->transd = NULL;
271 return EINVAL;
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 bool all_on_hold = true;
287
288 if ((rv = xbps_transaction_init(xhp)) != 0)
289 return rv;
290
291 if (xhp->transd == NULL)
292 return ENXIO;
293
294 /*
295 * Collect dependencies for pkgs in transaction.
296 */
297 if ((edges = xbps_array_create()) == NULL)
298 return ENOMEM;
299
300 xbps_dbg_printf("%s: processing deps\n", __func__);
301 /*
302 * The edges are also appended after its dependencies have been
303 * collected; the edges at the original array are removed later.
304 */
305 pkgs = xbps_dictionary_get(xhp->transd, "packages");
306 assert(xbps_object_type(pkgs) == XBPS_TYPE_ARRAY);
307 cnt = xbps_array_count(pkgs);
308 for (i = 0; i < cnt; i++) {
309 xbps_dictionary_t pkgd;
310 xbps_string_t str;
311
312 pkgd = xbps_array_get(pkgs, i);
313 str = xbps_dictionary_get(pkgd, "pkgver");
314 ttype = xbps_transaction_pkg_type(pkgd);
315
316 if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD)
317 continue;
318
319 assert(xbps_object_type(str) == XBPS_TYPE_STRING);
320
321 if (!xbps_array_add(edges, str)) {
322 xbps_object_release(edges);
323 return ENOMEM;
324 }
325 if ((rv = xbps_transaction_pkg_deps(xhp, pkgs, pkgd)) != 0) {
326 xbps_object_release(edges);
327 return rv;
328 }
329 if (!xbps_array_add(pkgs, pkgd)) {
330 xbps_object_release(edges);
331 return ENOMEM;
332 }
333 }
334 /* ... remove dup edges at head */
335 for (i = 0; i < xbps_array_count(edges); i++) {
336 const char *pkgver = NULL;
337 xbps_array_get_cstring_nocopy(edges, i, &pkgver);
338 xbps_remove_pkg_from_array_by_pkgver(pkgs, pkgver);
339 }
340 xbps_object_release(edges);
341
342 /*
343 * Do not perform any checks if XBPS_FLAG_DOWNLOAD_ONLY
344 * is set. We just need to download the archives (dependencies).
345 */
346 if (xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY)
347 goto out;
348
349 /*
350 * If all pkgs in transaction are on hold, no need to check
351 * for anything else.
352 */
353 xbps_dbg_printf("%s: checking on hold pkgs\n", __func__);
354 for (i = 0; i < cnt; i++) {
355 tpkgd = xbps_array_get(pkgs, i);
356 if (xbps_transaction_pkg_type(tpkgd) != XBPS_TRANS_HOLD) {
357 all_on_hold = false;
358 break;
359 }
360 }
361 if (all_on_hold)
362 goto out;
363
364 /*
365 * Check for packages to be replaced.
366 */
367 xbps_dbg_printf("%s: checking replaces\n", __func__);
368 if (!xbps_transaction_check_replaces(xhp, pkgs)) {
369 xbps_object_release(xhp->transd);
370 xhp->transd = NULL;
371 return EINVAL;
372 }
373 /*
374 * Check if there are missing revdeps.
375 */
376 xbps_dbg_printf("%s: checking revdeps\n", __func__);
377 if (!xbps_transaction_check_revdeps(xhp, pkgs)) {
378 xbps_object_release(xhp->transd);
379 xhp->transd = NULL;
380 return EINVAL;
381 }
382 if (xbps_dictionary_get(xhp->transd, "missing_deps")) {
383 if (xhp->flags & XBPS_FLAG_FORCE_REMOVE_REVDEPS) {
384 xbps_dbg_printf("[trans] continuing with broken reverse dependencies!");
385 } else {
386 return ENODEV;
387 }
388 }
389 /*
390 * Check for package conflicts.
391 */
392 xbps_dbg_printf("%s: checking conflicts\n", __func__);
393 if (!xbps_transaction_check_conflicts(xhp, pkgs)) {
394 xbps_object_release(xhp->transd);
395 xhp->transd = NULL;
396 return EINVAL;
397 }
398 if (xbps_dictionary_get(xhp->transd, "conflicts")) {
399 return EAGAIN;
400 }
401 /*
402 * Check for unresolved shared libraries.
403 */
404 xbps_dbg_printf("%s: checking shlibs\n", __func__);
405 if (!xbps_transaction_check_shlibs(xhp, pkgs)) {
406 xbps_object_release(xhp->transd);
407 xhp->transd = NULL;
408 return EINVAL;
409 }
410 if (xbps_dictionary_get(xhp->transd, "missing_shlibs")) {
411 if (xhp->flags & XBPS_FLAG_FORCE_REMOVE_REVDEPS) {
412 xbps_dbg_printf("[trans] continuing with unresolved shared libraries!");
413 } else {
414 return ENOEXEC;
415 }
416 }
417out:
418 /*
419 * Add transaction stats for total download/installed size,
420 * number of packages to be installed, updated, configured
421 * and removed to the transaction dictionary.
422 */
423 xbps_dbg_printf("%s: computing stats\n", __func__);
424 if ((rv = compute_transaction_stats(xhp)) != 0) {
425 return rv;
426 }
427 /*
428 * Make transaction dictionary immutable.
429 */
430 xbps_dictionary_make_immutable(xhp->transd);
431
432 return 0;
433}
char rootdir[XBPS_MAXPATH]
Definition xbps.h:650
int flags
Definition xbps.h:679
xbps_dictionary_t transd
Definition xbps.h:583
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
xbps_object_iterator_t xbps_array_iter_from_dict(xbps_dictionary_t dict, const char *key)
Definition plist.c:202
xbps_trans_type_t xbps_transaction_pkg_type(xbps_dictionary_t pkg_repod)
xbps_trans_type_t
Definition xbps.h:1320
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