Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=e7a40212cb5f36d3ffc01b8ec6988d851bf7d2e0

commit e7a40212cb5f36d3ffc01b8ec6988d851bf7d2e0
Author: Michel Hermier <[email protected]>
Date:   Thu Aug 14 13:26:05 2014 +0200

pacman-g2: Isolate removepkg/trans_commit differences with switch/case.

diff --git a/src/pacman-g2/pacman-g2.c b/src/pacman-g2/pacman-g2.c
index ea5f72c..b344c78 100644
--- a/src/pacman-g2/pacman-g2.c
+++ b/src/pacman-g2/pacman-g2.c
@@ -612,7 +612,7 @@ int main(int argc, char *argv[])
/* start the requested operation */
switch(config->op) {
case PM_OP_ADD:     ret = trans_commit(PM_TRANS_TYPE_ADD, pm_targets);     
break;
-               case PM_OP_REMOVE:  ret = removepkg(pm_targets);  break;
+               case PM_OP_REMOVE:  ret = removepkg(PM_TRANS_TYPE_REMOVE, 
pm_targets);  break;
case PM_OP_UPGRADE: ret = trans_commit(PM_TRANS_TYPE_UPGRADE, pm_targets); 
break;
case PM_OP_QUERY:   ret = querypkg(pm_targets);   break;
case PM_OP_SYNC:    ret = syncpkg(pm_targets);    break;
diff --git a/src/pacman-g2/remove.c b/src/pacman-g2/remove.c
index 85eb257..61705ed 100644
--- a/src/pacman-g2/remove.c
+++ b/src/pacman-g2/remove.c
@@ -37,7 +37,7 @@ extern config_t *config;

extern PM_DB *db_local;

-int removepkg(list_t *targets)
+int removepkg(pmtranstype_t transtype, list_t *targets)
{
PM_LIST *data;
list_t *i;
@@ -48,36 +48,40 @@ int removepkg(list_t *targets)
return(0);
}

-       /* If the target is a group, ask if its packages should be removed
-        * (the library can't remove groups for now)
-        */
-       for(i = targets; i; i = i->next) {
-               PM_GRP *grp;
-
-               grp = pacman_db_readgrp(db_local, i->data);
-               if(grp) {
-                       PM_LIST *lp, *pkgnames;
-                       int all;
-
-                       pkgnames = pacman_grp_getinfo(grp, PM_GRP_PKGNAMES);
-
-                       MSG(NL, _(":: group %s:\n"), pacman_grp_getinfo(grp, 
PM_GRP_NAME));
-                       PM_LIST_display("   ", pkgnames);
-                       all = yesno(_("    Remove whole content? [Y/n] "));
-                       for(lp = pacman_list_first(pkgnames); lp; lp = 
pacman_list_next(lp)) {
-                               if(all || yesno(_(":: Remove %s from group %s? 
[Y/n] "), (char *)pacman_list_getdata(lp), i->data)) {
-                                       finaltargs = list_add(finaltargs, 
strdup(pacman_list_getdata(lp)));
+       switch (transtype) {
+       case PM_TRANS_TYPE_REMOVE:
+               /* If the target is a group, ask if its packages should be 
removed
+                * (the library can't remove groups for now)
+                */
+               for(i = targets; i; i = i->next) {
+                       PM_GRP *grp;
+
+                       grp = pacman_db_readgrp(db_local, i->data);
+                       if(grp) {
+                               PM_LIST *lp, *pkgnames;
+                               int all;
+
+                               pkgnames = pacman_grp_getinfo(grp, 
PM_GRP_PKGNAMES);
+
+                               MSG(NL, _(":: group %s:\n"), 
pacman_grp_getinfo(grp, PM_GRP_NAME));
+                               PM_LIST_display("   ", pkgnames);
+                               all = yesno(_("    Remove whole content? [Y/n] 
"));
+                               for(lp = pacman_list_first(pkgnames); lp; lp = 
pacman_list_next(lp)) {
+                                       if(all || yesno(_(":: Remove %s from 
group %s? [Y/n] "), (char *)pacman_list_getdata(lp), i->data)) {
+                                               finaltargs = 
list_add(finaltargs, strdup(pacman_list_getdata(lp)));
+                                       }
}
+                       } else {
+                               /* not a group, so add it to the final targets 
*/
+                               finaltargs = list_add(finaltargs, 
strdup(i->data));
}
-               } else {
-                       /* not a group, so add it to the final targets */
-                       finaltargs = list_add(finaltargs, strdup(i->data));
}
+               break;
}

/* Step 1: create a new transaction
*/
-       if(pacman_trans_init(PM_TRANS_TYPE_REMOVE, config->flags,
+       if(pacman_trans_init(transtype, config->flags,
cb_trans_evt, cb_trans_conv, cb_trans_progress) == -1) {
ERR(NL, _("failed to init transaction (%s)\n"), pacman_strerror(pm_errno));
if(pm_errno == PM_ERR_HANDLE_LOCK) {
@@ -90,31 +94,31 @@ int removepkg(list_t *targets)
/* and add targets to it */
for(i = finaltargs; i; i = i->next) {
if(pacman_trans_addtarget(i->data) == -1) {
-                       int found=0;
-
-                       /* check for regex */
-                       if(config->regex) {
-                               PM_LIST *k;
-                               for(k = pacman_db_getpkgcache(db_local); k; k = 
pacman_list_next(k)) {
-                                       PM_PKG *p = pacman_list_getdata(k);
-                                       char *pkgname = pacman_pkg_getinfo(p, 
PM_PKG_NAME);
-                                       int match = pacman_reg_match(pkgname, 
i->data);
-                                       if(match == -1) {
-                                               ERR(NL, _("failed to add target 
'%s' (%s)\n"), (char *)i->data, pacman_strerror(pm_errno));
-                                               retval = 1;
-                                               goto cleanup;
-                                       }
-                                       else if(match) {
-                                               pacman_trans_addtarget(pkgname);
-                                               found++;
+                       int match = 0;
+
+                       switch (transtype) {
+                       case PM_TRANS_TYPE_REMOVE:
+                               /* check for regex */
+                               if(config->regex) {
+                                       PM_LIST *k;
+
+                                       for(k = 
pacman_db_getpkgcache(db_local); k; k = pacman_list_next(k)) {
+                                               PM_PKG *p = 
pacman_list_getdata(k);
+                                               char *pkgname = 
pacman_pkg_getinfo(p, PM_PKG_NAME);
+
+                                               match = 
pacman_reg_match(pkgname, i->data);
+                                               if(match > 0) {
+                                                       
pacman_trans_addtarget(pkgname);
+                                               }
}
}
+                               break;
}
-                       if(!found) {
-                               ERR(NL, _("failed to add target '%s' (%s)\n"), 
(char *)i->data, pacman_strerror(pm_errno));
-                               retval = 1;
-                               goto cleanup;
-                       }
+                       if (match > 0) continue;
+
+                       ERR(NL, _("failed to add target '%s' (%s)\n"), (char 
*)i->data, pacman_strerror(pm_errno));
+                       retval = 1;
+                       goto cleanup;
}
}

@@ -128,8 +132,13 @@ int removepkg(list_t *targets)
case PM_ERR_UNSATISFIED_DEPS:
for(lp = pacman_list_first(data); lp; lp = pacman_list_next(lp)) {
PM_DEPMISS *miss = pacman_list_getdata(lp);
-                                       MSG(NL, _("  %s: is required by %s\n"), 
pacman_dep_getinfo(miss, PM_DEP_TARGET),
-                                           pacman_dep_getinfo(miss, 
PM_DEP_NAME));
+
+                                       switch (transtype) {
+                                       case PM_TRANS_TYPE_REMOVE:
+                                               MSG(NL, _("  %s: is required by 
%s\n"), pacman_dep_getinfo(miss, PM_DEP_TARGET),
+                                                   pacman_dep_getinfo(miss, 
PM_DEP_NAME));
+                                               break;
+                                       }
}
break;
default:
@@ -140,24 +149,28 @@ int removepkg(list_t *targets)
goto cleanup;
}

-       /* Warn user in case of dangerous operation
-        */
-       if(config->flags & PM_TRANS_FLAG_RECURSE || config->flags & 
PM_TRANS_FLAG_CASCADE) {
-               PM_LIST *lp;
-               /* list transaction targets */
-               i = NULL;
-               for(lp = 
pacman_list_first(pacman_trans_getinfo(PM_TRANS_PACKAGES)); lp; lp = 
pacman_list_next(lp)) {
-                       PM_PKG *pkg = pacman_list_getdata(lp);
-                       i = list_add(i, strdup(pacman_pkg_getinfo(pkg, 
PM_PKG_NAME)));
-               }
-               list_display(_("\nTargets:"), i);
-               FREELIST(i);
-               /* get confirmation */
-               if(yesno(_("\nDo you want to remove these packages? [Y/n] ")) 
== 0) {
-                       retval = 1;
-                       goto cleanup;
+       switch (transtype) {
+       case PM_TRANS_TYPE_REMOVE:
+               /* Warn user in case of dangerous operation
+                */
+               if(config->flags & PM_TRANS_FLAG_RECURSE || config->flags & 
PM_TRANS_FLAG_CASCADE) {
+                       PM_LIST *lp;
+                       /* list transaction targets */
+                       i = NULL;
+                       for(lp = 
pacman_list_first(pacman_trans_getinfo(PM_TRANS_PACKAGES)); lp; lp = 
pacman_list_next(lp)) {
+                               PM_PKG *pkg = pacman_list_getdata(lp);
+                               i = list_add(i, strdup(pacman_pkg_getinfo(pkg, 
PM_PKG_NAME)));
+                       }
+                       list_display(_("\nTargets:"), i);
+                       FREELIST(i);
+                       /* get confirmation */
+                       if(yesno(_("\nDo you want to remove these packages? 
[Y/n] ")) == 0) {
+                               retval = 1;
+                               goto cleanup;
+                       }
+                       MSG(NL, "\n");
}
-               MSG(NL, "\n");
+               break;
}

/* Step 3: actually perform the transaction
diff --git a/src/pacman-g2/remove.h b/src/pacman-g2/remove.h
index 93474e3..6ec88a9 100644
--- a/src/pacman-g2/remove.h
+++ b/src/pacman-g2/remove.h
@@ -21,7 +21,7 @@
#ifndef _PM_REMOVE_H
#define _PM_REMOVE_H

-int removepkg(list_t *targets);
+int removepkg(pmtranstype_t transtype, list_t *targets);

#endif /* _PM_REMOVE_H */

diff --git a/src/pacman-g2/trans.c b/src/pacman-g2/trans.c
index 6d702b6..b3e8a31 100644
--- a/src/pacman-g2/trans.c
+++ b/src/pacman-g2/trans.c
@@ -388,19 +388,24 @@ int trans_commit(pmtranstype_t transtype, list_t *targets)
return(0);
}

-       /* Check for URL targets and process them
-        */
-       for(i = targets; i; i = i->next) {
-               if(strstr(i->data, "://")) {
-                       char *str = pacman_fetch_pkgurl(i->data);
-                       if(str == NULL) {
-                               return(1);
+       switch (transtype) {
+       case PM_TRANS_TYPE_ADD:
+       case PM_TRANS_TYPE_UPGRADE:
+               /* Check for URL targets and process them
+                */
+               for(i = targets; i; i = i->next) {
+                       if(strstr(i->data, "://")) {
+                               char *str = pacman_fetch_pkgurl(i->data);
+                               if(str == NULL) {
+                                       return(1);
+                               } else {
+                                       finaltargs = list_add(finaltargs, str);
+                               }
} else {
-                               finaltargs = list_add(finaltargs, str);
+                               finaltargs = list_add(finaltargs, 
strdup(i->data));
}
-               } else {
-                       finaltargs = list_add(finaltargs, strdup(i->data));
}
+               break;
}

/* Step 1: create a new transaction
@@ -437,14 +442,20 @@ int trans_commit(pmtranstype_t transtype, list_t *targets)
case PM_ERR_UNSATISFIED_DEPS:
for(lp = pacman_list_first(data); lp; lp = pacman_list_next(lp)) {
PM_DEPMISS *miss = pacman_list_getdata(lp);
-                                       MSG(NL, _(":: %s: requires %s"), 
pacman_dep_getinfo(miss, PM_DEP_TARGET),
-                                                                     
pacman_dep_getinfo(miss, PM_DEP_NAME));
-                                       switch((long)pacman_dep_getinfo(miss, 
PM_DEP_MOD)) {
-                                               case PM_DEP_MOD_EQ: MSG(CL, 
"=%s", pacman_dep_getinfo(miss, PM_DEP_VERSION));  break;
-                                               case PM_DEP_MOD_GE: MSG(CL, 
">=%s", pacman_dep_getinfo(miss, PM_DEP_VERSION)); break;
-                                               case PM_DEP_MOD_LE: MSG(CL, 
"<=%s", pacman_dep_getinfo(miss, PM_DEP_VERSION)); break;
+
+                                       switch (transtype) {
+                                       case PM_TRANS_TYPE_ADD:
+                                       case PM_TRANS_TYPE_UPGRADE:
+                                               MSG(NL, _(":: %s: requires 
%s"), pacman_dep_getinfo(miss, PM_DEP_TARGET),
+                                                                             
pacman_dep_getinfo(miss, PM_DEP_NAME));
+                                               
switch((long)pacman_dep_getinfo(miss, PM_DEP_MOD)) {
+                                                       case PM_DEP_MOD_EQ: 
MSG(CL, "=%s", pacman_dep_getinfo(miss, PM_DEP_VERSION));  break;
+                                                       case PM_DEP_MOD_GE: 
MSG(CL, ">=%s", pacman_dep_getinfo(miss, PM_DEP_VERSION)); break;
+                                                       case PM_DEP_MOD_LE: 
MSG(CL, "<=%s", pacman_dep_getinfo(miss, PM_DEP_VERSION)); break;
+                                               }
+                                               MSG(CL, "\n");
+                                               break;
}
-                                       MSG(CL, "\n");
}
break;
case PM_ERR_CONFLICTING_DEPS:
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to