Anonymous avatar Anonymous committed 7d386ad

Oops... Fix a bug in get_part_type, now fdisk works again. Remove some things from fdisk, that already exist in common.h (still needs improvement). Make fdisk choose a suitable default partition position for a new partition (place_part_start and place_part_end from common.c). (fdisk@sv.gnu.org/fdisk--main--0--patch-40)
fdisk@sv.gnu.org/fdisk--main--0--patch-40
Keywords:

Comments (0)

Files changed (4)

 			ped_device_get_constraint (dev));
 
 	PedGeometry *new_geom = ped_constraint_solve_nearest(constraint, geom);
-
+	ped_geometry_destroy(geom);
+	ped_geometry_destroy(range);
 	if (new_geom) {
 		if (*start != new_geom->start || *end != new_geom->end) {
 			*start = new_geom->start;
 			*end = new_geom->end;
-			ped_geometry_destroy(new_geom);
+			
 		}
+		ped_geometry_destroy(new_geom);
 		return 1;
 	} 
 
 	
 }
 
-static int
+int
 get_device(const char* prompt, PedDevice** value)
 {
 	char*		dev_name = *value ? strdup((*value)->path) : NULL;
 	return 1;
 }
 
-static int
+int
 get_disk (const char* prompt, PedDisk** value)
 {
 	PedDevice*	dev = *value ? (*value)->dev : NULL;
 /* These two use a different aproach, it leads to a little lack of consistency, but
  * this enables the use of a shiny interface in the UI */
 
-static int
+int
 get_partition (const char* prompt, PedDisk* disk, PedPartition** value)
 {
 	PedPartition*	part;
 	return 1;
 }
 
-static int
+int
 get_disk_type (const char* prompt, const PedDiskType** value)
 {
 	char*		disk_type_name = NULL;
 }
 
 
-static int
+int
 get_part_type (const char* prompt, const PedDisk* disk, PedPartitionType* type)
 {
 	StrList*	opts = NULL;
 
 	if (_can_create_primary (disk)) {
 		opts = str_list_append (opts, "primary");
-		locopts = str_list_append (opts, _("primary"));
+		locopts = str_list_append (locopts, _("primary"));
 	}
 	if (_can_create_extended (disk)) {
 		opts = str_list_append (opts, "extended");
-		locopts = str_list_append (opts, _("extended"));
+		locopts = str_list_append (locopts, _("extended"));
 	}
 	if (_can_create_logical (disk)) {
 		opts = str_list_append (opts, "logical");
-		locopts = str_list_append (opts, _("logical"));
+		locopts = str_list_append (locopts, _("logical"));
 	}
 	if (!opts) {
 		ped_exception_throw (
 
 
 /* FIXME: I don't like this mkfs thing this way. Find a better one. */
-static int
+int
 get_fs_type (const char* prompt, const PedFileSystemType **value, int mkfs)
 {
 
 	return 1;
 }
 
-static int
+int
 get_part_flag (const char* prompt, PedPartition *part, PedPartitionFlag *value)
 {
 
 	/* Query the user for a new sector */
 	GET_SECT = UI_CUSTOM_VALUES, 
 	/* Initialise the range */
-	NO_RANGE = UI_CUSTOM_VALUES*2
+	NO_RANGE = UI_CUSTOM_VALUES*2,
+	/* We are looking where to place the start of a new partition */
+	//PLACE_START_NEWPART = UI_CUSTOM_VALUES*4,
+	/* We are looking where to place the end of a new partition */
+	PLACE_END_NEWPART = UI_CUSTOM_VALUES*8
 } GSOpts;
 
+
+
 /* This function gets sector. The prev option specifies
    the previous selected sector, used if the user 
    wants to specify a size, not a position */
 	return valid;
 }
 
+/* Looks for the first suitable free space for a new partition */
+place_part_start(PedDisk *disk, PartPos *pos, PedPartitionType type) {
+	PedPartition *part = NULL;
+	type = (type & PED_PARTITION_LOGICAL) | PED_PARTITION_FREESPACE; 
+	for (part = ped_disk_next_partition(disk, NULL);
+	     part; part = ped_disk_next_partition(disk,part)) {
+		if (part->type == type) {
+			pos->start.sector = part->geom.start;
+			return 1;
+		}
+	}
+	return 0;
+}
+
+/* This function looks where to place the end of a new partition */
+static int
+place_part_end(PedDisk *disk, PartPos *pos) {
+	PedPartition *part = ped_disk_get_partition_by_sector(disk,
+							pos->start.sector); 
+	if (part->type & PED_PARTITION_FREESPACE) {
+		pos->end.sector = part->geom.end;
+		return 1;	
+	}
+	else
+		return 0;
+}
+
+
 /* Do not confuse this with query_part_position, this queries the user for
    start and end of the partition and is called from the functions in here */ 
 static int
-get_position(PartPos* pos, PedDevice *dev, GSOpts opts)
+get_position(PartPos* pos, PedDisk *disk, GSOpts opts)
 {
-        if (!get_sector (_("Start"), dev, &(pos->start), -1LL, opts))
+        if (!get_sector (_("Start"), disk->dev, &(pos->start), -1LL, opts))
                 return 0;
+
+	/* If we are creating a new partition and want to look where to
+           place its end ... */
+	if (opts & PLACE_END_NEWPART)
+		place_part_end(disk,pos);
+
 	/* We give the sector from the first query, so that the user can
            specify size */
-        if (!get_sector (_("End"), dev, &(pos->end), pos->start.sector, opts))
+        if (!get_sector (_("End"), disk->dev, &(pos->end), pos->start.sector, opts))
                 return 0;
+
 	return 1;
 }
 
                                          PED_DISK_TYPE_PARTITION_NAME)) 
                 uiquery->getstring(_("Partition name"), &part_name, NULL, NULL, 1); 
 
-	if (!get_position(pos, disk->dev, opts & UI_CUSTOM_VALUES))
-		goto error; 
+
+	if (pos->start.sector == 0 && pos->end.sector == 0) {
+		place_part_start(disk,pos,part_type);
+		if (!get_position(pos, disk, (opts & UI_CUSTOM_VALUES) | 
+		                  PLACE_END_NEWPART))
+			goto error;
+	}
+	else {
+		if (!get_position(pos, disk, opts & UI_CUSTOM_VALUES))
+			goto error;
+	}
 	/*ped_exception_throw(PED_EXCEPTION_INFORMATION,
 			PED_EXCEPTION_OK,
 			"Create %llds-%llds (%llds-%llds)-(%llds-%llds)",
                 goto error;
 
         /* get new target */
-	if (!get_position(pos, disk->dev, opts & UI_CUSTOM_VALUES))
+	if (!get_position(pos, disk, opts & UI_CUSTOM_VALUES))
 		goto error_close_fs; 
 
         /* set / test on "disk" */
 		PartPos pos;
 		pos.start.sector = start;
 		pos.end.sector = end;
-		if (!get_position(&pos, disk->dev, GET_SECT | NO_RANGE ))
+		if (!get_position(&pos, disk, GET_SECT | NO_RANGE ))
 			goto error;
 		start = pos.start.sector;
 		end = pos.end.sector;
                         goto error;
         }
 
- 	if (!get_position(pos, disk->dev,opts & UI_CUSTOM_VALUES))
+ 	if (!get_position(pos, disk,opts & UI_CUSTOM_VALUES))
 		goto error; 
 
 	/* FIXME: This is most likely not needed */
 query_part_position(const char* prompt, const void* context, PartPos* pos,
                     PedSector first, PedSector last, PedDevice *dev, 
                     PedConstraint *constraint, UIOpts *opts);
+
+/* Functions used by fdisk at the moment */
+/* TODO: Some of these do things close to the UICalls functions */
+extern int get_device(const char* prompt, PedDevice**);
+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*,
+                          PedPartitionType*);
+extern int get_fs_type (const char* prompt, const PedFileSystemType**,
+                        int mkfs);
+extern int get_part_flag (const char* prompt, PedPartition*, PedPartitionFlag*);
+
 #endif
 	const PedFileSystemType* fs_type = ped_file_system_type_get ("ext2");
 	char*			 peek_word;
 	
-	if (!fdisk_command_line_get_part_type (_("Partition type?"), *disk,
+	//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;
 	} else {
-		if (!fdisk_command_line_get_fs_type (_("File system type?"),
-					       &fs_type))
+//		if (!fdisk_command_line_get_fs_type (_("File system type?"),
+		if (!get_fs_type (_("File system type?"),
+					       &fs_type, 0))
 			return 0;
 	}
 	if (peek_word)
 	command_line = str_list_append (command_line, word);
 }
 
+#if 0
 int
 fdisk_command_line_get_disk (const char* prompt, PedDisk** value)
 {
 	}
 	return 1;
 }
+#endif
 
+#if 0
 int
 fdisk_command_line_get_partition (const char* prompt, PedDisk* disk,
 			    PedPartition** value)
 	*value = part;
 	return 1;
 }
+#endif
 
+#if 0
 int
 fdisk_command_line_get_fs_type (const char* prompt, const PedFileSystemType*(* value))
 {
 	*value = fs_type;
 	return 1;
 }
+#endif
 
+#if 0
 int
 fdisk_command_line_get_disk_type (const char* prompt, const PedDiskType*(* value))
 {
 	PED_ASSERT (*value != NULL, return 0);
 	return 1;
 }
+#endif
 
+#if 0
 int
 fdisk_command_line_get_device (const char* prompt, PedDevice** value)
 {
 	*value = dev;
 	return 1;
 }
+#endif
 
 int
 fdisk_command_line_get_integer (const char* prompt, int* value)
 	return valid;
 }
 
+#if 0
 int
 fdisk_command_line_get_part_type (const char* prompt, const PedDisk* disk,
 	       		    PedPartitionType* type)
 	free (type_name);
 	return 1;
 }
+#endif
 
 char*
 fdisk_command_line_peek_word ()
 		return NULL;
 }
 
+#if 0
 int
 fdisk_command_line_get_sector (const char* prompt, PedDevice* dev, PedSector* value,
 			       PedGeometry** range)
 	free (input);
 	return valid;
 }
+#endif
 
 int
 fdisk_init_ui ()
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.