Anonymous avatar Anonymous committed f5663ef

Make partition list get the system id and name from the partition table, when possible. Implement function to change the partition id. This is still a bit messy. Implement partition id list. Make partition type choosing like fdisk's - little changes in _construct_prompt, also implement fdisk_command_line_get_option and rework fdisk_command_line_get_part_type to do it fdisk style. (fdisk@sv.gnu.org/fdisk--main--0--patch-41)
fdisk@sv.gnu.org/fdisk--main--0--patch-41
Keywords:

Comments (0)

Files changed (12)

 		fdisk.c  	\
 		common.c	\
 		common.h	\
+		hacks.c		\
+		hacks.h		\
 		strlist.c	\
 		strlist.h	\
 		ui.c		\
 	uiquery.getstring = getstring;
 	uiquery.getpart = getpart;
 	uiquery.getpartpos = getpartpos;
+	uiquery.getparttype = NULL;
+	uiquery.getdisktype = NULL;
+	uiquery.getint = NULL;
+	uiquery.getdev = NULL;
+	uiquery.getfstype = NULL;
 	uiquery.need_commit = 0;
 	uiquery.timer = ped_timer_new (_timer_handler, &timer_context);
 	ped_exception_set_handler(exception_handler);
 		}
 	}
 	if (!perform_mkfs (disk, *part, type, UI_WARN_COMMIT)) {
-		warning_waitkey(N_("Partition not formatted successfully"));
+		warning_waitkey(N_("The filesystem was created successfully"));
 		return 0;
 	}
-	notice_waitkey(N_("Partition formatted successfully."));
+	notice_waitkey(N_("The filesystem was not created successfully."));
 	return 1;
 }
 
 	{ 'b', N_("Flags"), N_("Change the flags of the current partition") },
 	{ 'n', N_("New"), N_("Create new partition from free space") },
 	{ 's', N_("Rescue"), N_("Look for deleted and corrupted partitions in the free space") },
-	{ 'f', N_("Format"), N_("Formats the partition with a filesystem") },
+	{ 'f', N_("Make FS"), N_("Creates a filesystem on the partition") },
 	{ 'c', N_("Check"), N_("Check partition for consistency") },
 	{ 'm', N_("Rename"), N_("Change partition name") },
 	{ 'y', N_("Copy"), N_("Write another partition over this one (requires commit)") },
 /* Here we store the struct with interface functions */
 static UICalls *uiquery;
 
-/* This is the function that sets it */
-void
-set_uicalls(UICalls *uiq) {
-	uiquery = uiq;
-}
-
 static int MEGABYTE_SECTORS (PedDevice* dev)
 {
         return PED_MEGABYTE_SIZE / dev->sector_size;
 	if (!disk) 
 		return 0;
 	if (!uiquery->getpart) {
-		snprintf(buf,SMALLBUF,_("%s number"),prompt);
+		snprintf(buf,SMALLBUF,_("%s number (1-%d)"),
+		         prompt,ped_disk_get_last_partition_num (disk));
 		num = (*value) ? (*value)->num : 0;
 		if (!uiquery->getint (buf, &num)) {
-			ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
-					     _("Expecting a partition number."));
+			ped_exception_throw (PED_EXCEPTION_ERROR,
+					     PED_EXCEPTION_CANCEL,
+					    _("Expecting a partition number."));
 			return 0;
 		}
 		part = ped_disk_get_partition (disk, num);
 		if (!part) {
-			ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+			ped_exception_throw (PED_EXCEPTION_ERROR,
+					     PED_EXCEPTION_CANCEL,
 					     _("Partition doesn't exist."));
 			return 0;
 		}
 
 
 int
-get_part_type (const char* prompt, const PedDisk* disk, PedPartitionType* type)
+_get_part_type (const char* prompt, const PedDisk* disk, PedPartitionType* type)
 {
 	StrList*	opts = NULL;
 	StrList*	locopts = NULL;
 	return 1;
 }
 
+int
+(*get_part_type) (const char* prompt, const PedDisk* disk,
+                  PedPartitionType* type) = _get_part_type;
 
 /* FIXME: I don't like this mkfs thing this way. Find a better one. */
 int
 
         return res;
 }
+
+
+/* This is the function that sets UICalls */
+void
+set_uicalls(UICalls *uiq) {
+	uiquery = uiq;
+	/* TODO: Maybe we do everything else like this */
+	if (uiquery->getparttype)
+		get_part_type = uiquery->getparttype;
+}
+
 	int (*getdev)(const char* prompt, PedDevice** value);
 	/* getpart, if the disk is NULL, it should be specified.
 	            This won't actually be used */
+	int (*getparttype) (const char* prompt, const PedDisk*,
+                            PedPartitionType*);
 	int (*getpart)(const char* prompt, PedDisk**, PedPartition** value);
 	int (*getdisktype)(const char* prompt, PedDiskType** value);
 	int (*getfstype)(const char* prompt, PedFileSystemType** value);
 extern int get_disk (const char* prompt, PedDisk**);
 extern int get_partition (const char* prompt, PedDisk*, PedPartition**);
 extern int get_disk_type (const char* prompt, const PedDiskType**);
-extern int get_part_type (const char* prompt, const PedDisk*,
+extern int (*get_part_type) (const char* prompt, const PedDisk*,
                           PedPartitionType*);
 extern int get_fs_type (const char* prompt, const PedFileSystemType**,
                         int mkfs);
 
 #include "../config.h"
 #include "common.h"
+#include "hacks.h"
 #include "command.h"
 #include "ui.h"
 
 	char*			 peek_word;
 	
 	//if (!fdisk_command_line_get_part_type (_("Partition type?"), *disk,
-	if (!get_part_type (_("Partition type?"), *disk,
+	if (!get_part_type (_("Partition type"), *disk,
 					       &part_type))
 		return 0;
 
 	if (part_type == PED_PARTITION_EXTENDED
 	    || (peek_word && isdigit (peek_word[0]))) {
 		fs_type = NULL;
-	} else {
+	}
+	/* 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?"),
-		if (!get_fs_type (_("File system type?"),
+		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 */
 
 		printf ("%s%d ", (*disk)->dev->path, part->num);
 
+		if (ped_partition_get_flag(part,PED_PARTITION_BOOT))
+			printf("  *  ");
+		else
+			printf("     ");
+		
+		printf ("%10lld %11lld %11lu ", start, end, blocks);
+
+		int type_size;
+		unsigned int part_type = get_disk_specific_system_type (part, &type_size);
+		type_size *= 2;
+		if (type_size) {
+			printf("  %*x  %s", type_size, part_type,
+			       _(get_disk_specific_system_name(part)));
+		}
+		else {
+			char *type = (char *)ped_partition_type_get_name (part->type);
+			if (part->fs_type 
+				&& !strcmp (part->fs_type->name, "linux-swap"))
+				printf ("%+4s %+21s ", _("82"), _("Linux Swap / Solaris"));
+			else if (is_bsd_partition ((*disk)->dev->path, 
+				part->geom.start * sect_size, sect_size))
+				printf ("%+4s %+17s ", _("a5"), _("Free/Net/OpenBSD"));
+			else if (!strcmp (type, "primary")) 
+				printf ("%+4s %+6s ", _("83"), _("Linux"));
+			else if (!strcmp (type, "extended"))
+				printf ("%+4s %+9s ", _("5"), _("Extended"));
+		}
+		#if 0
 		/* Check to see if we have a boot flag. */
 		if (ped_partition_get_flag (part, PED_PARTITION_BOOT)) {
 	  		printf ("  *  ");
 			else if (!strcmp (type, "extended"))
 				printf ("%+4s %+9s ", _("5"), _("Extended"));
 		}
+		#endif
 		printf ("\n");
 		start = end = blocks = 0;
 	}
 	return status;
 }
 
+/* TODO: This is a temporary solution, we should turn it around... */
+static int
+do_list_msdos_systypes (PedDisk** disk)
+{
+	int i,j = 0;
+	for (i = 0; msdos_systypes[i].name; i++) {
+		printf("%2x  %-16s%s", (unsigned int) msdos_systypes[i].type,
+		       msdos_systypes[i].name, j == 3 ? "\n" : " ");
+		j = (j + 1) % 4;
+	}
+	return 1;
+}
+
+/* This changes the filesystem type. For now, only on msdos partition type */
+static int
+do_change_system_type (PedDisk **disk)
+{
+	int type = 0, i;
+	PedPartition *part = NULL;
+	char *input = NULL;
+	const char *type_name = NULL;
+
+	const char prompt[] = "Hex code (type L to list codes)";
+
+	if(!get_partition(_("Partition"), *disk, &part) || !part)
+		return 0;
+	do {
+		input = fdisk_command_line_get_word (prompt, NULL, NULL, 1);
+		if (!input)
+			return 0;
+		if (!strcmp(input,"L")) {
+			free(input);
+			do_list_msdos_systypes(disk);
+		}
+		else
+			break;
+	} while(1);
+	if (!sscanf(input,"%x",&type)) {
+		free(input);
+		return 0;
+	}
+	set_disk_specific_system_type(part,type);
+	for (i = 0; msdos_systypes[i].name; i++) {
+		if (msdos_systypes[i].type == type)
+			type_name = _(msdos_systypes[i].name);
+	}
+	if (!type_name)
+		type_name = _("Unknown");
+	
+	printf(_("Changed type of partition %d to %x (%s)\n"),
+		part->num, type, type_name);
+	return 1;
+}
+
 static void
 _init_messages ()
 {
 _(" d   delete a partition"),
 NULL), NULL, 1));
 
+
+	fdisk_command_register (fdisk_main_menu_commands, fdisk_command_create (
+		str_list_create_unique ("l", _("l"), NULL),
+		do_list_msdos_systypes,
+		str_list_create (
+_(" l   list known partition types"),
+NULL), NULL, 1));
+
 	fdisk_command_register (fdisk_main_menu_commands, fdisk_command_create (
 		str_list_create_unique ("m", _("m"), NULL),
 		do_help,
 _(" s   create a new empty Sun disklabel"),
 NULL), NULL, 1));
 
+
+	/* TODO: This should be available only on msdos and sun disklabels */
+	fdisk_command_register (fdisk_main_menu_commands, fdisk_command_create (
+		str_list_create_unique ("t", _("t"), NULL),
+		do_change_system_type,
+		str_list_create (
+_(" t   change a partition's system id"),
+NULL), NULL, 1));
+
 	fdisk_command_register (fdisk_main_menu_commands, fdisk_command_create (
 		str_list_create_unique ("u", _("u"), NULL),
 		do_unit,
 	uiquery.getstring = getstring;
 	uiquery.getint = fdisk_command_line_get_integer;
 	uiquery.getbool = getbool;
+	uiquery.getparttype = fdisk_command_line_get_part_type;
 	uiquery.getdev = NULL;
 	uiquery.getpart = NULL;
 	uiquery.getdisktype = NULL;
 
 	_init_messages ();
 	_init_main_menu_commands ();
+
+	/* TODO: Make _init_messages init these, remove messages from ui.c */
+	init_flag_str();
+	init_fs_type_str();
+	init_disk_type_str();
+	
 	
 	
 	if (!_parse_options (argc_ptr, argv_ptr))
 
 
 /* FIXME: As the file is needed for lfdisk, calling it "hacks" might
- * not be the greatest idea
+ * not be the greatest idea. Rename to something.
  */
 
 /* Check if it is a block device. Returns -1 if not found. */
 #define HACKS_H_INCLUDED
 #include <parted/parted.h>
 
+
+#ifndef SYS_TYPES_H_INCLUDED
+struct systypes {
+	unsigned char type;
+	const char *name;
+};
+#endif
+
 #define MSDOS_HACK 1
 #define MAC_HACK 1
 #define SUN_HACK 1
 extern int is_blockdev(const char *file);
 extern const char* get_disk_specific_system_name (PedPartition*);
 extern unsigned int get_disk_specific_system_type (PedPartition *part, int *size);
+extern struct systypes msdos_systypes[];
 #endif
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
 
+#ifndef SYS_TYPES_H_INCLUDED
+#define SYS_TYPES_H_INCLUDED
+#include <parted/parted.h>
+
+
+#ifndef HACKS_H_INCLUDED
 struct systypes {
 	unsigned char type;
 	const char *name;
 };
+#endif
+
 
 /* The system types for msdos  partition tables
  * Needed for lfdisk and some interface improvements
 	{ 0x75, "PC/IX" }, 
 	{ 0x80, "Minix <1.4a" }, 
 	{ 0x81, "Minix >1.4b" }, 
-	{ 0x82, "Linux swap" }, 
+	{ 0x82, "Linux swap" }, /* FIXME: Or solaris */
 	{ 0x83, "Linux" }, 
 	{ 0x84, "OS/2 hidden C:" }, 
 	{ 0x85, "Linux extended" }, 
 	{ 0xff, "XENIX BBT" },
 	{ 0, NULL }
 };
+
+#endif
 
 static PedExceptionOption	current_exception_opt = 0;
 
+/* If we don't want the possibilities to appear on the prompt, this 
+   should be set to 0. TODO: Making this without a global variable
+   might be a good idea */
+static prompt_possibilities = 1;
+
 #ifdef HAVE_LIBREADLINE
 /* returns matching commands for text */
 static char*
 		PED_ASSERT (str_list_match_any (possibilities, def),
 			    return NULL);
 
-	if (possibilities && str_list_length (possibilities) < 8) {
+	if (possibilities && prompt_possibilities && 
+	    str_list_length (possibilities) < 8) {
 		const StrList*	walk;
 		if (strlen (prompt))
 			prompt = realloc_and_cat (prompt, " ");
 				free (text);
 			}
 		}
-		prompt = realloc_and_cat (prompt, "? ");
+		prompt = realloc_and_cat (prompt, ": ");
 	} else if (def) {
 		if (strlen (prompt))
 			prompt = realloc_and_cat (prompt, "  ");
 		prompt = realloc_and_cat (prompt, "[");
 		prompt = realloc_and_cat (prompt, def);
-		prompt = realloc_and_cat (prompt, "]? ");
+		prompt = realloc_and_cat (prompt, "]: ");
 	} else {
-		if (strlen (prompt))
-			prompt = realloc_and_cat (prompt, " ");
+		size_t len = strlen (prompt);
+		if (len && prompt[--len] != '\n') {
+			if (prompt[len] == ':' || prompt[len] == '?')
+				prompt = realloc_and_cat (prompt, " ");
+			else
+				prompt = realloc_and_cat (prompt, ": ");
+		}
 	}
 
 	return prompt;
 	return valid;
 }
 
+/* Struct for a single option, Linux fdisk-style */
+struct _Option {
+	int option;
+	char *description;
+};
+typedef struct _Option Option;
+
+int
+fdisk_command_line_get_option (const char* head, const Option* opts)
+{
+	StrList *possibilities = NULL;
+	char *prompt = strdup(head);
+	char *result;
+	char buf[128]; 
+	int i;
+
+	buf[1] = '\0';
+
+	for (i = 0; opts[i].option; i++) {
+		/* Initialise the possibilities StrList */
+		buf[0] = opts[i].option;
+		buf[1] = '\0';
+
+		possibilities = str_list_append (possibilities, buf);
+
+		/* Initialise the prompt string */
+		snprintf(buf,sizeof(buf),"\n   %c   %s",
+		         opts[i].option, opts[i].description);
+		prompt = realloc_and_cat (prompt, buf);
+	}
+	/* We add a newline at the end, so _construct_prompt
+           handles it as it should */
+	prompt = realloc_and_cat (prompt, "\n");
+	
+
+	/* Queries the user about the option, we turn off the automatic
+	   adding of possibilities to the prompt */
+	prompt_possibilities = 0;
+	result = fdisk_command_line_get_word (prompt, NULL, possibilities, 1);
+	prompt_possibilities = 1;
+	
+	/* We can use i, not very intuitive to the coder, but who cares */
+	i = result[0];
+	free(prompt);
+	free(result);
+	return i;
+} 
+
+int
+fdisk_command_line_get_part_type (const char* prompt, const PedDisk *disk,
+                                  PedPartitionType *type)
+{
+	Option opts[4];
+	int i = 0;
+	if (_can_create_extended (disk)) {
+		opts[i].option = 'e';
+		opts[i].description = _("extended");
+		i++;
+	}
+	if (_can_create_logical (disk)) {
+		opts[i].option = 'l';
+		opts[i].description = _("logical (5 or over)");
+		i++;
+	}
+	if (_can_create_primary (disk)) {
+		opts[i].option = 'p';
+		opts[i].description = _("primary partition (1-4)");
+		i++; 
+	}
+	opts[i].option = 0;
+
+	*type = fdisk_command_line_get_option(prompt, opts);
+	return 1;
+}
 #if 0
 int
 fdisk_command_line_get_part_type (const char* prompt, const PedDisk* disk,
 					     PedPartition** value);
 extern int fdisk_command_line_is_integer ();
 extern int command_line_prompt_boolean_question (const char* prompt);
-extern int fdisk_command_line_get_disk (const char* prompt, PedDisk** value);
-extern int fdisk_command_line_get_partition (const char* prompt, PedDisk* disk,
-				      PedPartition** value);
-extern int fdisk_command_line_get_fs_type (const char* prompt, 
-					   const PedFileSystemType*(* value));
-extern int fdisk_command_line_get_disk_type (const char* prompt, const PedDiskType*(* value));
-extern int fdisk_command_line_get_device (const char* prompt, PedDevice** value);
+/*extern int fdisk_command_line_get_disk (const char* prompt, PedDisk** value);*/
+/*extern int fdisk_command_line_get_partition (const char* prompt, PedDisk* disk,
+				      PedPartition** value);*/
+/*extern int fdisk_command_line_get_fs_type (const char* prompt, 
+					   const PedFileSystemType*(* value));*/
+/*extern int fdisk_command_line_get_disk_type (const char* prompt, const PedDiskType*(* value));*/
+/*extern int fdisk_command_line_get_device (const char* prompt, PedDevice** value);*/
 extern int fdisk_command_line_get_integer (const char* prompt, int* value);
 extern int fdisk_command_line_get_part_type (const char* prompt, const PedDisk* disk,
 				      PedPartitionType* type);
 extern char* fdisk_command_line_peek_word ();
-extern int fdisk_command_line_get_sector (const char* prompt, PedDevice* dev, PedSector* value,
-					  PedGeometry** range);
+/*extern int fdisk_command_line_get_sector (const char* prompt, PedDevice* dev, PedSector* value,
+					  PedGeometry** range);*/
 extern int fdisk_init_ui ();
 extern void fdisk_done_ui ();
 extern int fdisk_command_line_get_part_flag (const char* prompt, const PedPartition* part,

tests/check_advanced.c

 	check_safety ();
 	int nf;
 	ped_exception_set_handler(exception_handler);
+	uiquery.getpartpos = NULL;
 	set_uicalls(&uiquery);
 	reset_exception_error();
 	string[0][BUFSIZE] = 0;

tests/check_common.c

 		unlink_tempfile;
 		return 1;
 	}
+	
 	ped_exception_set_handler(exception_handler);
+	uiquery.getpartpos = NULL;
 	set_uicalls(&uiquery);
 	reset_exception_error();
 	string[0][BUFSIZE] = 0;
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.