Skip to content

Commit cd9c303

Browse files
committed
lib: cleanup transaction_store and handle errors
1 parent 316a188 commit cd9c303

4 files changed

Lines changed: 83 additions & 79 deletions

File tree

include/xbps_api_impl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ bool HIDDEN xbps_transaction_check_revdeps(struct xbps_handle *, xbps_array_t);
9090
bool HIDDEN xbps_transaction_check_shlibs(struct xbps_handle *, xbps_array_t);
9191
bool HIDDEN xbps_transaction_check_replaces(struct xbps_handle *, xbps_array_t);
9292
int HIDDEN xbps_transaction_check_conflicts(struct xbps_handle *, xbps_array_t);
93-
int HIDDEN xbps_transaction_store(struct xbps_handle *, xbps_array_t, xbps_dictionary_t, bool);
93+
int HIDDEN transaction_store(struct xbps_handle *xhp, xbps_dictionary_t pkgrd,
94+
bool autoinstall);
9495
int HIDDEN xbps_transaction_init(struct xbps_handle *);
9596
int HIDDEN xbps_transaction_files(struct xbps_handle *,
9697
xbps_object_iterator_t);

lib/transaction_ops.c

Lines changed: 66 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -217,24 +217,16 @@ trans_find_pkg(struct xbps_handle *xhp, const char *pkg, bool force)
217217
if (!force && xbps_dictionary_get(pkg_repod, "hold"))
218218
ttype = XBPS_TRANS_HOLD;
219219

220-
/*
221-
* Store pkgd from repo into the transaction.
222-
*/
223-
if (!xbps_transaction_pkg_type_set(pkg_repod, ttype)) {
224-
return EINVAL;
225-
}
226220

227221
/*
228222
* Set automatic-install to true if it was requested and this is a new install.
229223
*/
230224
if (ttype == XBPS_TRANS_INSTALL)
231225
autoinst = xhp->flags & XBPS_FLAG_INSTALL_AUTO;
232226

233-
if (!xbps_transaction_store(xhp, pkgs, pkg_repod, autoinst)) {
227+
if (!xbps_transaction_pkg_type_set(pkg_repod, ttype))
234228
return EINVAL;
235-
}
236-
237-
return 0;
229+
return transaction_store(xhp, pkg_repod, autoinst);
238230
}
239231

240232
/*
@@ -464,96 +456,100 @@ xbps_transaction_remove_pkg(struct xbps_handle *xhp,
464456
bool recursive)
465457
{
466458
xbps_dictionary_t pkgd;
467-
xbps_array_t pkgs, orphans, orphans_pkg;
468-
xbps_object_t obj;
469-
int rv = 0;
459+
int r;
470460

471461
assert(xhp);
472462
assert(pkgname);
473463

474-
if ((pkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) {
475-
/* pkg not installed */
476-
return ENOENT;
477-
}
478-
/*
479-
* Prepare transaction dictionary and missing deps array.
480-
*/
481-
if ((rv = xbps_transaction_init(xhp)) != 0)
482-
return rv;
464+
r = xbps_transaction_init(xhp);
465+
if (r < 0)
466+
return r;
483467

484-
pkgs = xbps_dictionary_get(xhp->transd, "packages");
468+
pkgd = xbps_pkgdb_get_pkg(xhp, pkgname);
469+
if (!pkgd)
470+
return -errno;
485471

486-
if (!recursive)
487-
goto rmpkg;
488472
/*
489473
* If recursive is set, find out which packages would be orphans
490474
* if the supplied package were already removed.
491475
*/
492-
if ((orphans_pkg = xbps_array_create()) == NULL)
493-
return ENOMEM;
476+
// XXX: this is stupid and incomplete if multiple packages
477+
// are removed at once, in that case orphans would not be correctly
478+
// be identified...
479+
if (recursive) {
480+
xbps_array_t orphans, orphans_pkg;
481+
482+
orphans_pkg = xbps_array_create();
483+
if (!orphans_pkg)
484+
return xbps_error_oom();
485+
486+
if (!xbps_array_set_cstring_nocopy(orphans_pkg, 0, pkgname)) {
487+
xbps_object_release(orphans_pkg);
488+
return xbps_error_oom();
489+
}
494490

495-
xbps_array_set_cstring_nocopy(orphans_pkg, 0, pkgname);
496-
orphans = xbps_find_pkg_orphans(xhp, orphans_pkg);
497-
xbps_object_release(orphans_pkg);
498-
if (xbps_object_type(orphans) != XBPS_TYPE_ARRAY)
499-
return EINVAL;
500491

501-
for (unsigned int i = 0; i < xbps_array_count(orphans); i++) {
502-
obj = xbps_array_get(orphans, i);
503-
xbps_transaction_pkg_type_set(obj, XBPS_TRANS_REMOVE);
504-
if (!xbps_transaction_store(xhp, pkgs, obj, false)) {
505-
return EINVAL;
492+
orphans = xbps_find_pkg_orphans(xhp, orphans_pkg);
493+
if (!orphans) {
494+
xbps_object_release(orphans_pkg);
495+
return -errno;
496+
}
497+
xbps_object_release(orphans_pkg);
498+
for (unsigned int i = 0; i < xbps_array_count(orphans); i++) {
499+
xbps_dictionary_t opkgd;
500+
501+
opkgd = xbps_array_get(orphans, i);
502+
if (!opkgd)
503+
xbps_unreachable();
504+
505+
xbps_transaction_pkg_type_set(opkgd, XBPS_TRANS_REMOVE);
506+
r = transaction_store(xhp, opkgd, false);
507+
if (r < 0) {
508+
xbps_object_release(orphans);
509+
return r;
510+
}
506511
}
507512
}
508-
xbps_object_release(orphans);
509-
return rv;
510513

511-
rmpkg:
512-
/*
513-
* Add pkg dictionary into the transaction pkgs queue.
514-
*/
514+
// XXX: why do we modify supposedly read only pkgd's
515515
xbps_transaction_pkg_type_set(pkgd, XBPS_TRANS_REMOVE);
516-
if (!xbps_transaction_store(xhp, pkgs, pkgd, false)) {
517-
return EINVAL;
518-
}
519-
return rv;
516+
return transaction_store(xhp, pkgd, false);
520517
}
521518

522519
int
523520
xbps_transaction_autoremove_pkgs(struct xbps_handle *xhp)
524521
{
525-
xbps_array_t orphans, pkgs;
526-
xbps_object_t obj;
527-
int rv = 0;
522+
xbps_array_t orphans;
523+
int r;
524+
525+
r = xbps_transaction_init(xhp);
526+
if (r < 0)
527+
return r;
528528

529529
orphans = xbps_find_pkg_orphans(xhp, NULL);
530+
if (!orphans)
531+
return -errno;
530532
if (xbps_array_count(orphans) == 0) {
531-
/* no orphans? we are done */
532-
goto out;
533+
xbps_object_release(orphans);
534+
return 0;
533535
}
534-
/*
535-
* Prepare transaction dictionary and missing deps array.
536-
*/
537-
if ((rv = xbps_transaction_init(xhp)) != 0)
538-
goto out;
539536

540-
pkgs = xbps_dictionary_get(xhp->transd, "packages");
541-
/*
542-
* Add pkg orphan dictionary into the transaction pkgs queue.
543-
*/
544537
for (unsigned int i = 0; i < xbps_array_count(orphans); i++) {
545-
obj = xbps_array_get(orphans, i);
546-
xbps_transaction_pkg_type_set(obj, XBPS_TRANS_REMOVE);
547-
if (!xbps_transaction_store(xhp, pkgs, obj, false)) {
548-
rv = EINVAL;
549-
goto out;
538+
xbps_object_t pkgd;
539+
540+
pkgd = xbps_array_get(orphans, i);
541+
if (!pkgd)
542+
xbps_unreachable();
543+
544+
xbps_transaction_pkg_type_set(pkgd, XBPS_TRANS_REMOVE);
545+
r = transaction_store(xhp, pkgd, false);
546+
if (r < 0) {
547+
return r;
550548
}
551549
}
552-
out:
553-
if (orphans)
554-
xbps_object_release(orphans);
555550

556-
return rv;
551+
xbps_object_release(orphans);
552+
return 0;
557553
}
558554

559555
xbps_trans_type_t

lib/transaction_pkg_deps.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ repo_deps(struct xbps_handle *xhp,
148148
while ((obj = xbps_object_iterator_next(iter))) {
149149
bool error = false, foundvpkg = false;
150150
bool autoinst = true;
151+
int r;
151152

152153
ttype = XBPS_TRANS_UNKNOWN;
153154
reqpkg = xbps_string_cstring_nocopy(obj);
@@ -342,12 +343,13 @@ repo_deps(struct xbps_handle *xhp,
342343
if (ttype == XBPS_TRANS_CONFIGURE) {
343344
if (!xbps_transaction_pkg_type_set(curpkgd, ttype)) {
344345
rv = EINVAL;
345-
xbps_dbg_printf("xbps_transaction_pkg_type_set failed for `%s': %s\n", reqpkg, strerror(rv));
346+
xbps_error_printf("xbps_transaction_pkg_type_set failed for `%s': %s\n", reqpkg, strerror(rv));
346347
break;
347348
}
348-
if (!xbps_transaction_store(xhp, pkgs, curpkgd, autoinst)) {
349+
r = transaction_store(xhp, curpkgd, autoinst);
350+
if (r < 0) {
351+
xbps_error_printf("transaction_store failed for `%s': %s\n", reqpkg, strerror(rv));
349352
rv = EINVAL;
350-
xbps_dbg_printf("xbps_transaction_store failed for `%s': %s\n", reqpkg, strerror(rv));
351353
break;
352354
}
353355
continue;
@@ -482,9 +484,10 @@ repo_deps(struct xbps_handle *xhp,
482484
xbps_dbg_printf("xbps_transaction_pkg_type_set failed for `%s': %s\n", reqpkg, strerror(rv));
483485
break;
484486
}
485-
if (!xbps_transaction_store(xhp, pkgs, repopkgd, autoinst)) {
487+
r = transaction_store(xhp, repopkgd, autoinst);
488+
if (r < 0) {
486489
rv = EINVAL;
487-
xbps_dbg_printf("xbps_transaction_store failed for `%s': %s\n", reqpkg, strerror(rv));
490+
xbps_error_printf("transaction_store failed for `%s': %s\n", reqpkg, strerror(rv));
488491
break;
489492
}
490493
}

lib/transaction_store.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,26 @@ package_self_replace(xbps_dictionary_t pkgd, const char *pkgname)
9090
}
9191

9292
int HIDDEN
93-
xbps_transaction_store(struct xbps_handle *xhp, xbps_array_t pkgs,
94-
xbps_dictionary_t pkgrd, bool autoinst)
93+
transaction_store(struct xbps_handle *xhp, xbps_dictionary_t pkgrd,
94+
bool autoinst)
9595
{
96+
xbps_array_t pkgs;
9697
xbps_dictionary_t pkgd;
9798
const char *pkgver = NULL, *pkgname = NULL, *repo = NULL;
9899
int r;
99100

100101
assert(xhp);
101-
assert(pkgs);
102102
assert(pkgrd);
103103

104104
if (!xbps_dictionary_get_cstring_nocopy(pkgrd, "pkgver", &pkgver))
105105
xbps_unreachable();
106106
if (!xbps_dictionary_get_cstring_nocopy(pkgrd, "pkgname", &pkgname))
107107
xbps_unreachable();
108108

109+
pkgs = xbps_dictionary_get(xhp->transd, "packages");
110+
if (!pkgs)
111+
xbps_unreachable();
112+
109113
r = transaction_replace_package(pkgs, pkgname, pkgver);
110114
if (r < 0)
111115
return r;

0 commit comments

Comments
 (0)