Anonymous avatar Anonymous committed bc313fe

Small update since yesterday, I hope I will finish with this this week... (fdisk@sv.gnu.org/fdisk--main--0--patch-4)
fdisk@sv.gnu.org/fdisk--main--0--patch-4
Keywords:

Comments (0)

Files changed (2)

 } TimerContext;
 
 
+/* TODO: Decide if these should be moved to UICalls */
+StrList *disk_type_list;
+StrList *fs_type_list;
+
+int
+init_fs_type_str ()
+{
+	PedFileSystemType*	walk;
+
+	fs_type_list = NULL;
+
+	for (walk = ped_file_system_type_get_next (NULL); walk;
+	     walk = ped_file_system_type_get_next (walk))
+	{
+		fs_type_list = str_list_insert (fs_type_list, walk->name);
+		if (!fs_type_list)
+			return 0;
+	}
+
+	return 1;
+}
+
+int
+init_disk_type_str ()
+{
+	PedDiskType*	walk;
+
+	disk_type_list = NULL;
+
+	for (walk = ped_disk_type_get_next (NULL); walk;
+	     walk = ped_disk_type_get_next (walk))
+	{
+		disk_type_list = str_list_insert (disk_type_list, walk->name);
+		if (!disk_type_list)
+			return 0;
+	}
+
+	return 1;
+}
+
+
+static int
+_can_create_primary (const PedDisk* disk)
+{
+	int	i;
+
+	for (i = 1; i <= ped_disk_get_max_primary_partition_count (disk); i++) {
+		if (!ped_disk_get_partition (disk, i))
+			return 1;
+	}
+
+	return 0;
+}
+
+static int
+_can_create_extended (const PedDisk* disk)
+{
+	if (!_can_create_primary (disk))
+		return 0;
+	if (!ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_EXTENDED))
+		return 0;
+	if (ped_disk_extended_partition (disk))
+		return 0;
+	return 1;
+}
+
+static int
+_can_create_logical (const PedDisk* disk)
+{
+	if (!ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_EXTENDED))
+		return 0;
+	return ped_disk_extended_partition (disk) != 0;
+}
+
 
 /* Copied verbatim from parted upto help_on*/
 
 {
 	char*		dev_name = *value ? (*value)->path : NULL;
 	PedDevice*	dev;
-
-	if (uiquery->getstring (prompt, &dev_name, NULL, NULL, 1))
-		return 0;
-	if (!dev_name)
-		return 0;
-	dev = ped_device_get (dev_name);
-	free (dev_name);
-	if (!dev)
-		return 0;
+	if (!uiquery->getdev) {
+		if (!uiquery->getstring (prompt, &dev_name, NULL, NULL, 1))
+			return 0;
+		if (!dev_name)
+			return 0;
+		dev = ped_device_get (dev_name);
+		free (dev_name);
+		if (!dev)
+			return 0;
+	}
+	else {
+		if (!uiquery->getdev(prompt, &dev))
+			return 0;
+	}
 	*value = dev;
 	return 1;
 }
 {
 	PedPartition*	part;
 	int		num; 
+	char*		buf;
 	if (!disk) 
 		return 0;
 	if (!uiquery->getpart) {
+		sprintf(buf,_("%s number"),prompt);
 		num = (*value) ? (*value)->num : 0;
-		if (!uiquery->getint (prompt, &num)) {
+		if (!uiquery->getint (buf, &num)) {
+			free(buf);
 			ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
 					     _("Expecting a partition number."));
 			return 0;
 		}
+		free(buf);
 		part = ped_disk_get_partition (disk, num);
 		if (!part) {
 			ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
 	StrList*	opts = NULL;
 	StrList*	locopts = NULL;
 	char*		type_name;
+	int		status;
 
 	if (_can_create_primary (disk)) {
 		opts = str_list_append_unique (opts, "primary");
 		return 0;
 	}
 
-	uiquery->getstring(prompt,&type_name,opts,locopts,-1);
+	status = uiquery->getstring(prompt,&type_name,opts,locopts,-1);
 	
 	str_list_destroy (opts);
 	str_list_destroy (locopts);
+	if (!status) {
+		if(type_name) free(type_name);
+		return 0;
+	}
+
 
 	if (!type_name) {
 		ped_exception_throw (
 }
 
 
+
+static int
+get_sector (const char* prompt, PedDevice* dev, PedSector* value,
+			 PedGeometry** range,  int custom, UICalls *uiquery)
+{
+	char*	def_str;
+	char*	input;
+	int	valid;
+	/* FIXME: Check this more thoroughfully */
+
+	if (!custom && *value) {
+		if (range) {
+			*range = ped_geometry_new(dev,*value,1);
+			return *range != NULL;
+		}
+		return 1;
+	}
+
+	def_str = ped_unit_format (dev, *value);
+	if (*value) {
+		strcpy(input,def_str);
+	}
+	if (custom || !input) {
+		uiquery->getstring(prompt,&input,NULL,NULL,1);
+	}
+
+	/* def_str might have rounded *value a little bit.  If the user picked
+	 * the default, make sure the selected sector is identical to the
+	 * default.
+	 */
+	if (input && *value && !strcmp (input, def_str)) {
+		if (range) {
+			*range = ped_geometry_new (dev, *value, 1);
+			ped_free (def_str);
+			return *range != NULL;
+		}
+
+		ped_free (def_str);
+		return 1;
+	}
+
+	ped_free (def_str);
+	if (!input) {
+		*value = 0;
+		if (range)
+			*range = NULL;
+		return 0;
+	}
+
+	valid = ped_unit_parse (input, dev, value, range);
+
+	free (input);
+	return valid;
+}
+
 static int
 ask_boolean_question(const char* prompt, UICalls *uiquery) {
 	int answer = 0;
 
 /* Check partition consistency */
 int
-do_check (PedDisk* disk, PedPartition* part, PedTimer* timer, UICalls *uiquery)
+do_check (PedDisk* disk, PedPartition* part, UICalls *uiquery)
 {
         PedFileSystem*  fs;
 	if (!part) {
         fs = ped_file_system_open (&part->geom);
         if (!fs)
                 goto error;
-        if (!ped_file_system_check (fs, timer))
+        if (!ped_file_system_check (fs, uiquery->timer))
                 goto error_close_fs;
         ped_file_system_close (fs);
         return 1;
    The source partition is get using a callback function prompt.
    Writes partition table to disk */
 int
-do_cp (PedDisk* dst_disk, PedPartition* dst, PedTimer* timer, UICalls *uiquery)
+do_cp (PedDisk* dst_disk, PedPartition* dst, UICalls *uiquery)
 {
         PedDisk*                src_disk;
         PedDevice*              src_device;
         src_fs = ped_file_system_open (&src->geom);
         if (!src_fs)
                 goto error_destroy_disk;
-        dst_fs = ped_file_system_copy (src_fs, &dst->geom, timer);
+        dst_fs = ped_file_system_copy (src_fs, &dst->geom, uiquery->timer);
         if (!dst_fs)
                 goto error_close_src_fs;
         dst_fs_type = dst_fs->type;     /* may be different to src_fs->type */
 }
 
 /* Create a filesystem. Takes filesystem type as an optional parameter */
-static int
-do_mkfs (PedDisk* disk, PedPartition* part, const PedFileSystemType* type,
-				PedTimer* timer, UICalls *uiquery)
+int
+do_mkfs (PedDisk* disk, PedPartition* part, const PedFileSystemType* type, UICalls *uiquery)
 {
 
         PedFileSystem*          fs;
 	}
 
 
-        fs = ped_file_system_create (&part->geom, type, timer);
+        fs = ped_file_system_create (&part->geom, type, uiquery->timer);
         if (!fs)
                 goto error;
         ped_file_system_close (fs);
 }
 
 /* The parameter custom is 1, then the user is asked about the start and end sector */
-static int
+int
 do_mkpart (PedDisk* disk, PedSector start, PedSector end, int custom, PedPartitionType part_type,
-		const PedFileSystemType* fs_type, PedTimer* timer, UICalls *uiquery)
+		const PedFileSystemType* fs_type, UICalls *uiquery)
 {
 	PedGeometry		*range_start = NULL, *range_end = NULL;
         PedPartition*            part;
             /*|| (peek_word && isdigit (peek_word[0]))) {*/
                 fs_type = NULL;
         } else {
-                if (!command_line_get_fs_type (_("File system type?"),
-                                               &fs_type))
+                if (!get_fs_type (_("File system type?"), &fs_type, uiquery))
                         goto error;
         }
         /*if (peek_word)
                 ped_free (peek_word);*/
 
-        if (!get_sector (_("Start?"), disk->dev, &start, &range_start, custom))
+        if (!get_sector (_("Start?"), disk->dev, &start, &range_start, custom, uiquery))
                 goto error;
-        if (!get_sector (_("End?"), disk->dev, &end, &range_end, custom))
+        if (!get_sector (_("End?"), disk->dev, &end, &range_end, custom, uiquery))
                 goto error;
         
         /* processing starts here */
                         range_end);
         PED_ASSERT (user_constraint != NULL, return 0);
 
-        dev_constraint = ped_device_get_constraint (*dev);
+        dev_constraint = ped_device_get_constraint (disk->dev);
         PED_ASSERT (dev_constraint != NULL, return 0);
 
         final_constraint = ped_constraint_intersect (user_constraint,
                 ped_exception_leave_all();
                
                 if (ped_disk_add_partition (disk, part,
-                                        ped_constraint_any (*dev))) {
-                        start_usr = ped_unit_format (*dev, start);
-                        end_usr   = ped_unit_format (*dev, end);
-                        start_sol = ped_unit_format (*dev, part->geom.start);
-                        end_sol   = ped_unit_format (*dev, part->geom.end);
+                                        ped_constraint_any (disk->dev))) {
+                        start_usr = ped_unit_format (disk->dev, start);
+                        end_usr   = ped_unit_format (disk->dev, end);
+                        start_sol = ped_unit_format (disk->dev, part->geom.start);
+                        end_sol   = ped_unit_format (disk->dev, part->geom.end);
 
                         switch (ped_exception_throw (
                                 PED_EXCEPTION_WARNING,
 
         return 0;
 }
-
 typedef struct _UICalls UICalls;
 struct _UICalls {
 	/* The first parameter specifies the prompt text */
+	/* All functions should return 1 on success, 0 otherwise */
 	/* getstring: 	The second parameter is the string that is read.                 *
 	 * The two StrLists specify thevalid strings, the second contains the localized. *
 	 * The integer specified how to treat multiwords, TODO: explain                  */
 	int (*getstring)(const char*, char**, const StrList*, const StrList*, int);
 	int (*getint)(const char*, int*);
 	int (*getbool)(const char*, int*);
+	int (*getdev)(const char*, PedDevice**);
 	/* getpart, if the disk is NULL, it should be specified. This won't actually be used */
 	int (*getpart)(const char*, PedDisk**, PedPartition**);
 	int (*getdisktype)(const char*, PedDiskType**);
 	int (*getfstype)(const char*, PedFileSystemType**);
+	PedTimer* timer;
 };
 
-extern int do_check (PedDisk*, PedPartition*, PedTimer*, UICalls *);
-extern int do_cp (PedDisk*, PedPartition*, PedTimer*, UICalls *);
+extern int do_check (PedDisk*, PedPartition*, UICalls *);
+extern int do_cp (PedDisk*, PedPartition*, UICalls *);
 
 
 #endif
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.