Browse Source

u-a: Merge all action handling into a single if-else-if block

Introduce two boolean variables to track when to log the command-line
arguments when modifying anything on the system, and another one when
modifying a single alternative to initialize and update it.

This unifies the code, and will make it easier to switch it into a
shared library in the future.
Guillem Jover 8 years ago
parent
commit
9c311e1a44
2 changed files with 30 additions and 17 deletions
  1. 2 0
      debian/changelog
  2. 28 17
      utils/update-alternatives.c

+ 2 - 0
debian/changelog

@@ -9,6 +9,8 @@ dpkg (1.18.4) UNRELEASED; urgency=low
     to «postinst configure», which might consider this a first install instead
     of an upgrade. Closes: #801156
   * Fix memory leaks in «dpkg --verify» and dpkg infodb format upgrade logic.
+  * Merge all update-alternatives action handling into a single if-else-if
+    block, to unify the code an allow a future switch into a shared library.
   * Build system:
     - Set PERL5LIB globally for the test suite to the local modules directory,
       to avoid using the system modules. Regression introduced in dpkg 1.17.8.

+ 28 - 17
utils/update-alternatives.c

@@ -2569,6 +2569,8 @@ main(int argc, char **argv)
 	char *path = NULL;
 	const char *current_choice = NULL;
 	const char *new_choice = NULL;
+	bool modifies_alt = false;
+	bool modifies_sys = false;
 	int i = 0;
 
 	setlocale(LC_ALL, "");
@@ -2720,6 +2722,21 @@ main(int argc, char **argv)
 		           "--config, --set, --set-selections, --install, "
 		           "--remove, --all, --remove-all or --auto"));
 
+	/* The following actions might modify the current alternative. */
+	if (strcmp(action, "set") == 0 ||
+	    strcmp(action, "auto") == 0 ||
+	    strcmp(action, "config") == 0 ||
+	    strcmp(action, "remove") == 0 ||
+	    strcmp(action, "remove-all") == 0 ||
+	    strcmp(action, "install") == 0)
+		modifies_alt = true;
+
+	/* The following actions might modify the system somehow. */
+	if (modifies_alt ||
+	    strcmp(action, "all") == 0 ||
+	    strcmp(action, "set-selections") == 0)
+		modifies_sys = true;
+
 	if (strcmp(action, "install") == 0)
 		alternative_check_install_args(inst_alt, fileset);
 
@@ -2746,35 +2763,28 @@ main(int argc, char **argv)
 		alternative_load(a, ALTDB_WARN_PARSER);
 	}
 
+	if (modifies_sys)
+		log_msg("run with %s", get_argv_string(argc, argv));
+
+	if (modifies_alt) {
+		current_choice = alternative_get_current(a);
+		alternative_select_mode(a, current_choice);
+	}
+
 	/* Handle actions. */
 	if (strcmp(action, "all") == 0) {
-		log_msg("run with %s", get_argv_string(argc, argv));
 		alternative_config_all();
-		exit(0);
 	} else if (strcmp(action, "get-selections") == 0) {
 		alternative_get_selections();
-		exit(0);
 	} else if (strcmp(action, "set-selections") == 0) {
-		log_msg("run with %s", get_argv_string(argc, argv));
 		alternative_set_selections(stdin, _("<standard input>"));
-		exit(0);
 	} else if (strcmp(action, "display") == 0) {
 		alternative_display_user(a);
-		exit(0);
 	} else if (strcmp(action, "query") == 0) {
 		alternative_display_query(a);
-		exit(0);
 	} else if (strcmp(action, "list") == 0) {
 		alternative_display_list(a);
-		exit(0);
-	}
-
-	/* Actions below might modify the system. */
-	log_msg("run with %s", get_argv_string(argc, argv));
-	current_choice = alternative_get_current(a);
-	alternative_select_mode(a, current_choice);
-
-	if (strcmp(action, "set") == 0) {
+	} else if (strcmp(action, "set") == 0) {
 		new_choice = alternative_set_manual(a, path);
 	} else if (strcmp(action, "auto") == 0) {
 		new_choice = alternative_set_auto(a);
@@ -2805,7 +2815,8 @@ main(int argc, char **argv)
 		}
 	}
 
-	alternative_update(a, current_choice, new_choice);
+	if (modifies_alt)
+		alternative_update(a, current_choice, new_choice);
 
 	return 0;
 }