Anonymous avatar Anonymous committed b3d1f56

Rewrite part of cfdisk's flag editor code. It was UGLY and it didn't work with partition types with no flags. (fdisk@sv.gnu.org/fdisk--main--0--patch-47)
fdisk@sv.gnu.org/fdisk--main--0--patch-47
Keywords:

Comments (0)

Files changed (1)

 #define TINYBUF 16	/* NOTE: Changing this may lead to buffer overflows. Double-check. */
 #define FSBUF 19	/* Don't touch unless you know what you are doing!! */
 #define SMALLBUF 512
-
+#define MAXFLAGS 63
 
 /* struct for a menu item. I got the idea from Linux cfdisk. Every action is bound to a key. */
 struct _MenuItem {
 
 static int
 do_flag (PedPartition **part) {
-	PedPartitionFlag selected = PED_PARTITION_BOOT;
-	/* This is not quite sexy */
-	/* FIXME FIXME FIXME Make this *sane* */
-	PedPartitionFlag first = 0, temp_a, temp_b;
-	do {
-		first = ped_partition_flag_next(first);
-	} while (first == PED_PARTITION_BOOT || first == PED_PARTITION_HIDDEN ||
-	         !ped_partition_is_flag_available(*part,first));
-	int redraw = 1, key = 0, done = 0;
+	PedPartitionFlag walk;
+	/* We will put the flags in this array */
+	PedPartitionFlag flags[MAXFLAGS+1];
+	int key, done = 0, redraw = 1, i = 0;
+
+	/* Put the boot and hidden flags at the beginning */
+	if (ped_partition_is_flag_available(*part,PED_PARTITION_BOOT))
+		flags[i++] = PED_PARTITION_BOOT; 
+	if (ped_partition_is_flag_available(*part,PED_PARTITION_HIDDEN))
+		flags[i++] = PED_PARTITION_HIDDEN;
+
+	/* Put the other flags */
+	for (walk = ped_partition_flag_next(0); walk && i < MAXFLAGS;
+	     walk = ped_partition_flag_next(walk))
+	{
+		if (walk == PED_PARTITION_BOOT || walk == PED_PARTITION_HIDDEN)
+			continue;
+		if (ped_partition_is_flag_available(*part,walk))
+			flags[i++] = walk;
+	}
+	flags[i] = 0;
+	
+	/* If there are no flags available, sorry */
+	if (i == 0) {
+		warning_waitkey(_("No flags can be changed for this partition"));
+		return 0;
+	}
+
+	/* We select the first one */
+	i = 0;
+
 	while (!done) {
 		if (redraw) {
-			flag_draw(*part,selected);
+			flag_draw(*part,flags[i]);
 			clear_menu();
 			redraw = 0;
 		}
 			if (key == '[' || key == 'O') {
 				key = getch();
 				if (key == 'B') { /* That's down arrow */
-					/* This should be replaced anyway */
-					temp_a = selected;
-					do {
-						if (temp_a == PED_PARTITION_BOOT)
-							temp_a = PED_PARTITION_HIDDEN;
-						else if (temp_a == PED_PARTITION_HIDDEN)
-							temp_a = first;
-						else {
-							do {
-								temp_a = ped_partition_flag_next(temp_a);
-							} while (temp_a == PED_PARTITION_BOOT || 
-								temp_a == PED_PARTITION_HIDDEN);
-						}
-					} while (temp_a && !ped_partition_is_flag_available(*part,temp_a));
-					if (!temp_a)
+
+					if (!flags[i+1])
 						print_warning(_("No more flags"),0);
 					else {
-						selected = temp_a;
+						i++;
 						redraw = 1;
 					}
 				}
 				else if (key == 'A') { /* That's up arrow */
-					/* Better try to solve a 5D rubic *
-					 * cube, than to figure this out. */
-					temp_a = 0;
-					temp_b = 0;
-					do { 
-						/* temp_b tries to reach the selected, starting from 
-						   the boot and hidden. temp_a follows it, skipping the
-						   flags that are unavailable. The whole flag editing 
-						   should be rewritten using an order array or list, 
-						   or at least functions for getting the re-ordered
-						   "next" and "prev" flag should be splitted to make 
-						   the code understandable, though it still works this way.
-						   If you ever happen to edit this code and it is not
-						   fixed yet, do yourself a favour and rewrite it from
-						   scratch. */
-						if (temp_b == 0)
-							temp_b = PED_PARTITION_BOOT;
-						else if (temp_b == PED_PARTITION_BOOT)
-							temp_b = PED_PARTITION_HIDDEN;
-						else if (temp_b == PED_PARTITION_HIDDEN)
-							temp_b = first;
-						else {
-							do {
-								temp_b = ped_partition_flag_next(temp_b);
-							} while (temp_b == PED_PARTITION_BOOT || 
-								temp_b == PED_PARTITION_HIDDEN);
-						}
-						if (!temp_b || ped_partition_is_flag_available(*part,temp_b))
-							temp_a = temp_b;
-						if (temp_b == 0 && selected == PED_PARTITION_BOOT)
-							break;
-						if (temp_b == PED_PARTITION_BOOT && selected == PED_PARTITION_HIDDEN)
-							break;
-						if (temp_b == PED_PARTITION_HIDDEN && selected == first)
-							break;
-					} while (temp_b && ped_partition_flag_next(temp_b) != selected);
-					if (!temp_a)
+					if (i == 0)
 						print_warning(_("No more flags"),0);
 					else {
-						selected = temp_a;
+						i--;
 						redraw = 1;
 					}
 				}
 				print_warning(_("Invalid key"),0);
 		}
 		else if (key == CR || key == ' ') {
-			perform_set(disk,*part,selected,UI_FLAG_TOGGLE);
+			perform_set(disk,*part,flags[i],UI_FLAG_TOGGLE);
 			redraw = 1;
 		}
 		else 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.