Anonymous avatar Anonymous committed 62dfe9e

fdisk: Detect whether lfdisk or gfdisk was started. Make some functions behave differently (for now only mkpart) and show different functions, depending on which is started. common: Fix some bugs. (fdisk@sv.gnu.org/fdisk--main--0--patch-44)
fdisk@sv.gnu.org/fdisk--main--0--patch-44
Keywords:

Comments (0)

Files changed (4)

 }
 
 
-/* Copied verbatim from parted upto help_on*/
+
 
 static int
 _partition_warn_busy (PedPartition* part)
 			PED_EXCEPTION_ERROR,
 			PED_EXCEPTION_CANCEL,
 			_("Partition %s is being used.  You must unmount it "
-			  "before you modify it with Parted."),
+			  "before you modify it."),
 			path);
 		ped_free (path);
 		return 0;
 	return 1;
 }
 
+static int
+_warn_ext_not_empty (PedPartition *part) {
+	/* If this is not an extended partition, it is ok */
+	if (!(part->type & PED_PARTITION_EXTENDED))
+		return 1;
+	for (part = part->part_list; part;
+	     part = part->next) {
+		if (part->type == PED_PARTITION_LOGICAL) {
+			if(ped_exception_throw (
+				PED_EXCEPTION_WARNING,
+				PED_EXCEPTION_YES_NO,
+				_("The extended partition is not empty. "
+				  "Deleting it will delete any partitions "
+				  "inside it. Do you want to continue?"))
+					== PED_EXCEPTION_NO)
+				return 0;
+			else
+				return 1;
+		}
+	}
+	return 1;
+}
+
 /* This function changes "sector" to "new_sector" if the new value lies
  * within the required range.
  */
                 uiquery->getstring(_("Partition name"), &part_name, NULL, NULL, 1); 
 
 
-	if (pos->start.sector == 0 && pos->end.sector == 0) {
+	if ((opts & UI_CUSTOM_VALUES) &&
+			pos->start.sector == 0LL && pos->end.sector == 0LL) {
 		place_part_start(disk,pos,part_type);
-		if (!get_position(pos, disk, (opts & UI_CUSTOM_VALUES) | 
-		                  PLACE_END_NEWPART))
+		if (!get_position(pos, disk, GET_SECT | PLACE_END_NEWPART))
 			goto error;
 	}
 	else {
                 goto error;
 
         /* get new target */
-	if (!get_position(pos, disk, opts & UI_CUSTOM_VALUES))
-		goto error_close_fs; 
+	if ((opts & UI_CUSTOM_VALUES) &&
+			pos->start.sector == 0LL && pos->end.sector == 0LL) {
+		place_part_start(disk,pos,part->type);
+		if (!get_position(pos, disk, GET_SECT | PLACE_END_NEWPART))
+			goto error_close_fs;
+	}
+	else {
+		if (!get_position(pos, disk, opts & UI_CUSTOM_VALUES))
+			goto error_close_fs;
+	}
+
 
         /* set / test on "disk" */
         if (!ped_geometry_init (&new_geom, disk->dev, pos->start.sector,
                 	goto error;
         if (!_partition_warn_busy (part))
                 goto error;
-
+	if (!_warn_ext_not_empty (part))
+		goto error;
+		
         ped_disk_delete_partition (disk, part);
         uiquery->need_commit = 1;
         return 1;
 		return 1;
 
 	/* We make sure that there are no mounted partitions */
-	if (!_disk_warn_busy (disk))
-		return 0;
+	for (i = 1; i <= 4; i++) {
+		walk = ped_disk_get_partition(disk,i);
+		if (walk && !_partition_warn_busy(walk))
+			return 0; 
+	}
 
 	/* We will issue warning to the user for now */
 	if (PED_EXCEPTION_CANCEL == ped_exception_throw(PED_EXCEPTION_WARNING,
 	/* name, has-arg, string-return-val, char-return-val */
 	{"help",	0, NULL, 'h'},
 	{"list",        0, NULL, 'l'},
+	{"linux-fdisk", 0, NULL, 'L'},
+	{"gnu-fdisk",   0, NULL, 'G'},
 	{"interactive",	0, NULL, 'i'},
 	{"script",	0, NULL, 's'},
 	{"version",	0, NULL, 'v'},
 static char*	options_help [][2] = {
 	{"help",	N_("displays this help message")},
 	{"list",        N_("List partition table(s)")},
+	{"linux-fdisk", N_("Enable Linux fdisk compatibility mode")},
+	{"gnu-fdisk",   N_("Disable Linux fdisk compatibility mode")},
 	{"interactive",	N_("where necessary, prompts for user intervention")},
 	{"script",	N_("never prompts for user intervention")},
 	{"version",	N_("displays the version")},
 	{NULL,		NULL}
 };
 
+int fdisk_compatibility_mode = 0;
 int fdisk_opt_script_mode;
 int fdisk_list_table;
 
 	 int		i;
   
 	if (cmds) {
-	for (i=0; cmds [i]; i++)
-      		fdisk_command_print_summary (cmds [i]);
+		for (i=0; cmds [i]; i++)
+			fdisk_command_print_summary (cmds [i]);
 	} else {
 		if (in_ex_menu) {
 			for (i=0; fdisk_ex_menu_commands [i]; i++)
 				fdisk_command_print_summary (fdisk_ex_menu_commands [i]);
     		} else {
-      			for (i=0; fdisk_ex_menu_commands [i]; i++)
+      			for (i=0; fdisk_main_menu_commands [i]; i++)
 				fdisk_command_print_summary (fdisk_main_menu_commands [i]);
     		}
 	}
 	PartPos			 pos;
 	PedPartition* 		 part;
 	PedPartitionType	 part_type = 0;
-	const PedFileSystemType* fs_type = ped_file_system_type_get ("ext2");
+	const PedFileSystemType	*fs_type = ped_file_system_type_get ("ext2");
 	char*			 peek_word;
+	UIOpts			 opts = UI_CUSTOM_VALUES;
 	
-	//if (!fdisk_command_line_get_part_type (_("Partition type?"), *disk,
+
+	
+
 	if (!get_part_type (_("Partition type"), *disk,
 					       &part_type))
 		return 0;
 	    || (peek_word && isdigit (peek_word[0]))) {
 		fs_type = NULL;
 	}
-	/* This is needed for gfdisk, but not for lfdisk, turn it off for now */
-	#if 0
-	else {
-//		if (!fdisk_command_line_get_fs_type (_("File system type?"),
+	/* gfdisk should ask for filesystem, lfdisk should not */
+	else if (!fdisk_compatibility_mode) {
 		if (!get_fs_type (_("File system type"),
 					       &fs_type, 0))
 			return 0;
 	}
-	#endif
 	if (peek_word)
 		ped_free (peek_word);
-	/* TODO: Make default position more friendly */
+
+
 	pos.start.sector = 0LL;
 	pos.end.sector = 0LL;
 	pos.start.range = NULL;
 	pos.end.range = NULL;
-	return perform_mkpart (*disk, &pos, part_type, fs_type,
-	                       NULL, UI_CUSTOM_VALUES);
+
+	if (!fdisk_compatibility_mode && part_type != PED_PARTITION_EXTENDED &&
+	    command_line_prompt_boolean_question(
+		_("Do you want to create the filesystem on the partition?"))) 
+	{
+		return perform_mkpartfs (*disk, &pos, part_type, fs_type,
+		                         NULL, opts);
+	}
+	else
+	{
+		return perform_mkpart (*disk, &pos, part_type, fs_type,
+		                       NULL, opts);
+	}
+	
 }
 
 static int
 do_mkpartfs (PedDisk** disk)
 {
 	PartPos pos;
-	/* TODO: Make default position more friendly */
+	
 	pos.start.sector = 0LL;
 	pos.end.sector = 0LL;
 	pos.start.range = NULL;
 	pos.end.sector = 0LL;
 	pos.start.range = NULL;
 	pos.end.range = NULL;
-	return perform_move (*disk, NULL, &pos, UI_CUSTOM_VALUES);
+	return perform_move (*disk, NULL, &pos, UI_CUSTOM_VALUES|UI_WARN_COMMIT);
 }
 
 static int
 static int
 do_resize (PedDisk** disk)
 {
-	return (*disk, NULL, 0LL, 0LL, UI_WARN_COMMIT | UI_CUSTOM_VALUES);
+	PartPos pos;
+	pos.start.sector = 0LL;
+	pos.end.sector = 0LL;
+	pos.start.range = NULL;
+	pos.end.range = NULL;
+	return perform_resize (*disk, NULL, &pos, UI_WARN_COMMIT | UI_CUSTOM_VALUES);
 #if 0
 	PedPartition		*part = NULL;
 	PedFileSystem		*fs;
 {
 	if (cylinder_unit) {
 		printf ("%s\n", _("Changing display/entry units to sectors"));
-		/*  PedUnit unit = ped_unit_get_by_name ("s");
-		 *  ped_unit_set_default (unit);
-		 */
 		ped_unit_set_default(PED_UNIT_SECTOR);
 		cylinder_unit = 0;
 		return 1;
 	} else {
 		printf ("%s\n", _("Changing display/entry units to cylinders"));
-		/*  PedUnit unit = ped_unit_get_by_name ("cyl");
-		 *  ped_unit_set_default (unit);
-		 */
 		ped_unit_set_default(PED_UNIT_CYLINDER);
 		cylinder_unit = 1;
 		return 1;
 }
 
 
+
+static int
+do_copy(PedDisk** disk)
+{
+	return perform_cp (*disk, NULL, UI_WARN_COMMIT);
+}
+
+
+
 static int
 do_ex_menu (PedDisk** disk) {
 	int status;
 
 static void
 _init_ex_menu_commands () {
+
+
+  if (!fdisk_compatibility_mode)
+  {
+
 	fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
 		str_list_create_unique ("v", _("v"), NULL),
 		do_move,
 		str_list_create (
-_("v           move a partition."),
-NULL),
+_(" v   move a partition."),
+NULL), 
 		str_list_create (_(number_msg), _(start_end_msg), NULL), 1));
 	fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
 		str_list_create_unique ("c", _("c"), NULL),
 		do_rescue,
 		str_list_create (
-_("c           rescue a lost partition."),
+_(" c   rescue a lost partition."),
 NULL),
 		str_list_create (_(start_end_msg), NULL), 1));
 
 		str_list_create_unique ("z", _("z"), NULL),
 		do_resize,
 		str_list_create (
-_("z           resize a partition and its file system."),
+_(" z   resize a partition and its file system."),
 NULL), NULL, 1));
 
 	fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
+		str_list_create_unique ("h", _("h"), NULL),
+		do_check,
+		str_list_create (
+_(" h   check the consistency of a partition."),
+NULL), NULL, 1));
+
+	fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
+		str_list_create_unique ("o", _("o"), NULL),
+		do_resize,
+		str_list_create (
+_(" o   copy the partition over another partition."),
+NULL), NULL, 1));
+
+
+  }
+
+
+
+	fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
+		str_list_create_unique ("f", _("f"), NULL),
+		do_fix_partition_order,
+		str_list_create (
+_(" f   fix partition order"),
+NULL), NULL, 1));
+
+  if (fdisk_compatibility_mode)
+  {
+
+	fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
 		str_list_create_unique ("m", _("m"), NULL),
 		do_help,
 		str_list_create (
-_("m           prints general help, or help on a COMMAND."),
+_(" m   print this menu"),
 NULL), NULL, 1));
 
+	fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
+		str_list_create_unique ("p", _("p"), NULL),
+		do_print,
+		str_list_create (
+_(" p   print the partition table"),
+NULL), NULL, 1));
+
+	fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
+		str_list_create_unique ("q", _("q"), NULL),
+		do_quit,
+		str_list_create (
+_(" q   quit without saving changes"),
+NULL), NULL, 1));
+  }
+
         fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
 		str_list_create_unique ("r", _("r"), NULL),
 		NULL,
 		str_list_create (
-_("r           return to the main menu."),
+_(" r   return to the main menu."),
 NULL), NULL, 1));
+
+  if (fdisk_compatibility_mode)
+        fdisk_command_register (fdisk_ex_menu_commands, fdisk_command_create (
+		str_list_create_unique ("w", _("w"), NULL),
+		do_commit,
+		str_list_create (
+_(" w   write table to disk and exit"),
+NULL), NULL, 1));
+
+  
 }
 
+
+
 static void
 _init_main_menu_commands () {
 	fdisk_command_register (fdisk_main_menu_commands, fdisk_command_create (
 _(" d   delete a partition"),
 NULL), NULL, 1));
 
-/* TODO: Move */
-	fdisk_command_register (fdisk_main_menu_commands, fdisk_command_create (
-		str_list_create_unique ("f", _("f"), NULL),
-		do_fix_partition_order,
-		str_list_create (
-_(" f   fix partition order"),
-NULL), NULL, 1));
-/* End TODO */
+
 
 	fdisk_command_register (fdisk_main_menu_commands, fdisk_command_create (
 		str_list_create_unique ("l", _("l"), NULL),
 _(" w   write table to disk and exit"),
 NULL), NULL, 1));
 
-/*	fdisk_command_register (fdisk_main_menu_commands, fdisk_command_create (
+	fdisk_command_register (fdisk_main_menu_commands, fdisk_command_create (
 		str_list_create_unique ("x", _("x"), NULL),
 		do_ex_menu,
 		str_list_create (
 _(" x   extra functionality (experts only)"),
-NULL), NULL, 1));*/
+NULL), NULL, 1));
 }
 
 static void
 	while (1)
 	{
 #ifdef HAVE_GETOPT_H
-		opt = getopt_long (*argc_ptr, *argv_ptr, "hlisv",
+		opt = getopt_long (*argc_ptr, *argv_ptr, "hlisvLG",
 				   options, NULL);
 #else
-		opt = getopt (*argc_ptr, *argv_ptr, "hlisv");
+		opt = getopt (*argc_ptr, *argv_ptr, "hlisvLG");
 #endif
 		if (opt == -1)
 			break;
 			case 'l': fdisk_list_table = 1; break;
 			case 'i': fdisk_opt_script_mode = 0; break;
 			case 's': fdisk_opt_script_mode = 1; break;
+			case 'G': fdisk_compatibility_mode = 0; break;
+			case 'L': fdisk_compatibility_mode = 1; break;
 			case 'v': _version (); break;
 		}
 	}
 		goto error;
 
 	_init_messages ();
-	_init_main_menu_commands ();
+
 
 	/* The default units are cylinders */
 	ped_unit_set_default(PED_UNIT_CYLINDER);
 	init_fs_type_str();
 	init_disk_type_str();
 	
-	
-	
 	if (!_parse_options (argc_ptr, argv_ptr))
 		goto error_done_commands;
+
 	dev = _choose_device (argc_ptr, argv_ptr);
+
+	_init_ex_menu_commands ();
+	_init_main_menu_commands ();
+
 	if (!dev)
 		goto error_done_commands;
 
 	        return -1;
 	}
 
+	
+	/* See whether we should enable Linux fdisk compatibility mode */
+	if (argv[0]) {
+		char *program_name = strrchr(argv[0], '/');
+		if (program_name)
+			program_name++;
+		else
+			program_name = argv[0];
+		fdisk_compatibility_mode = !strcmp(program_name,"lfdisk");
+	}
+	else
+		fdisk_compatibility_mode = 0;
+
 	dev = _init (&argc, &argv);
 
 	if (!dev && fdisk_list_table)
 	fdisk_print_options_help ();
 
 	printf ("\n%s\n", _("COMMANDs:"));
-	fdisk_print_commands_help ();
+	fdisk_print_commands_help ((FdiskCommand**)NULL);
 	exit (0);
 }
 
 				printf ("\n");
 				return 1;
 			}
+			prompt_possibilities = 0;
 			fdisk_command_line_prompt_words ("Command (m for help):", NULL,
 							 command_names, 1);
+			prompt_possibilities = 1;
 		}
 
 		word = fdisk_command_line_pop_word ();
 	StrList*	list;
 	StrList*	command_names = fdisk_command_get_names (cmd_list);
 
-	commands = cmd_list;	/* FIXME yucky, nasty, evil hack */
+	/* FIXME yucky, nasty, evil hack */
+	FdiskCommand **saved_commands = commands;
+	commands = cmd_list;
+	
 
 	while (1) {
 		char*		word;
 		while (!fdisk_command_line_get_word_count ()) {
 			if (feof (stdin)) {
 				printf ("\n");
+				commands = saved_commands;
 				return 1;
 			}
+			prompt_possibilities = 0;
 			if (!menu)
-			  fdisk_command_line_prompt_words ("Expert (m for help): ", NULL,
+			  fdisk_command_line_prompt_words ("Expert command (m for help)", NULL,
 							   command_names, 1);
 			else
-			  fdisk_command_line_prompt_words ("Filesystem (m for help): ", NULL,
+			  fdisk_command_line_prompt_words ("Filesystem (m for help)", NULL,
 							   command_names, 1);
+			prompt_possibilities = 1;
 		}
 
 		word = fdisk_command_line_pop_word ();
 			    		if (!fdisk_command_run (cmd, disk))
 			      			fdisk_command_line_flush ();
 			  		} else {
+						commands = saved_commands;
 			    			return 1;
 			  		}
 			} else {
 		}
 	}
 
+	commands = saved_commands;
 	return 1;
 }
 
 extern PedExceptionOption 
 fdisk_command_line_get_ex_opt (const char* prompt, PedExceptionOption options);
 extern void fdisk_print_options_help ();
-extern void fdisk_print_commands_help ();
+extern void fdisk_print_commands_help (FdiskCommand* cmds[]);
 extern void fdisk_print_using_dev (PedDevice* dev);
 extern int fdisk_command_line_get_word_count ();
 extern void fdisk_command_line_prompt_words (const char* prompt, const char* def,
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.