@@ -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
522519int
523520xbps_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
559555xbps_trans_type_t
0 commit comments