Anonymous avatar Anonymous committed 6d0e8de

We forgot to destroy temporary geometry in the new additions to get_sector from yesterday. Also, make functions to get pointer to PartPos structure as a parameter, instead of start and end. We sometimes need to give the start or end range as a parameter, and we want to reduce the number of function parameters. In this way we fix the bug where cfdisk tried to create partitions with start and end range with length of 1s, unless it was told to ask the user for exact values. (fdisk@sv.gnu.org/fdisk--main--0--patch-39)
fdisk@sv.gnu.org/fdisk--main--0--patch-39
Keywords:

Comments (0)

Files changed (7)

 	};
 	const char type_keys[] = {'p', 'e', ESC, '\0'};
 	PedPartitionType type = 0;
-	PedSector start, end;
+	PartPos pos;
 	int key,success = 1,done;
 	UIOpts opts = UI_DEFAULT;
 	if (!((*part)->type & PED_PARTITION_FREESPACE)) {
 		else type = PED_PARTITION_NORMAL;
 	}
 
-	start = (*part)->geom.start;
-	end = (*part)->geom.end;
+	pos.start.sector = (*part)->geom.start;
+	pos.end.sector = (*part)->geom.end;
 	if(!query_part_position(_("Where do you want to put the partition"),
-	                        part_position,&start,&end,start,end,dev,
-	                        NULL,&opts))
+	                        part_position,&pos,pos.start.sector,
+	                        pos.end.sector,dev,NULL,&opts))
 		return 0;
 
-	if (!perform_mkpart(disk,start,end,type,NULL,part,opts)) {
+	if (!perform_mkpart(disk,&pos,type,NULL,part,opts)) {
 		if (!*part) {
 			do {
 				temp = ped_disk_next_partition(disk,temp);
 	int key;
 	PedConstraint *constraint = NULL;
 	PedFileSystem *fs = NULL;
-	PedSector start,end,first,last;
+	PedSector first,last;
+	PartPos pos;
 	UIOpts opts = UI_WARN_COMMIT;
 	if (!((*part)->type & PED_PARTITION_EXTENDED)) {
 		if ((*part)->fs_type && (*part)->fs_type->ops->resize) {
 	}
 	char buf[SMALLBUF];
 
-	start = (*part)->geom.start;
-	end = (*part)->geom.end;
-	first = start;
+	pos.start.sector = (*part)->geom.start;
+	pos.end.sector = (*part)->geom.end;
 
 	if ((*part)->prev && (*part)->prev->type & PED_PARTITION_FREESPACE)
 		first = (*part)->prev->geom.start;
-	else first = start;
+	else (*part)->geom.start;
 
 	if ((*part)->next && (*part)->next->type & PED_PARTITION_FREESPACE)
 		last = (*part)->next->geom.end;
-	else last = end;
+	else (*part)->geom.end;
+
 	if(!query_part_position(_("Where to place the resized partition"),
-	                        part_position,&start,&end,first,last,dev,
+	                        part_position,&pos,first,last,dev,
 	                        constraint,&opts)) {
 		if (constraint) ped_constraint_destroy (constraint);
 		return 0;
 	if (constraint) ped_constraint_destroy (constraint);
 	
 
-	if (opts & UI_CUSTOM_VALUES || start != (*part)->geom.start || end != (*part)->geom.end)
-		if (!perform_resize(disk,*part,start,end,opts)) {
+	if (opts & UI_CUSTOM_VALUES || pos.start.sector != (*part)->geom.start 
+			|| pos.end.sector != (*part)->geom.end)
+		if (!perform_resize(disk,*part,&pos,opts)) {
 			warning_waitkey(N_("Couldn't resize partition."));
 			/* Ooopsy */
 			if (!(opts & UI_NO_FS_RESIZE) && uiquery.need_commit)
 	};
 	PedPartition *dst = NULL;
 	PedSector start,end,length;
+	PartPos pos;
 	PedConstraint *constraint = NULL;
 	PedFileSystem *fs = NULL;
 	UIOpts opts = UI_WARN_COMMIT;
 	length = MIN(length,(*part)->geom.length);
 	length = MAX(length,constraint->min_size);
 	end = start+length-1;
+	pos.start.sector = start;
+	pos.end.sector = end;
 	if(!query_part_position(_("Where do you want to move the partition"),
-	                        part_position,&start,&end,start,dst->geom.end,
+	                        part_position,&pos,start,dst->geom.end,
 	                        dev,constraint,&opts)) {
 		ped_constraint_destroy (constraint);
 		return 0;
 	}
 	ped_constraint_destroy (constraint);
-	if (!perform_move(disk,*part,start,end,opts)) {
+	if (!perform_move(disk,*part,&pos,opts)) {
 		warning_waitkey(N_("Partition move failed"));
 		/* Oooops */
 		if (uiquery.need_commit) _disk_reread(part);
 
 }
 
+
+/* PedSector is signed, so we can use this to move in both directions */
+static int
+move_sector(PedDevice *dev, SectPos *value, PedSector shift) {
+	/* FIXME: I don't believe that we will have problems with llint overflow
+                  but please note that it is possible. */
+
+	/* We will store the new values in temporary variables */
+	PedSector start, end, sector;
+
+	sector = value->sector + shift;
+
+	/* If there is no range, we simply move the sector */
+	if (!value->range) {
+		if (sector >= dev->length || sector < 0LL)
+			return 0;
+		value->sector = sector;
+		return 1;
+	}
+
+	start = value->range->start + shift;
+	end = value->range->end + shift;
+
+	/* We can't move the sector out of the device */
+	if (end < 0LL) return 0;
+	if (start >= dev->length) return 0;
+
+	sector = value->sector + shift;
+
+	/* We ensure that all values are within the device */
+	if (start < 0LL) {
+		start = 0LL;
+		if (sector < 0LL)
+			sector = 0LL;
+	}
+ 
+	if (end >= dev->length) {
+		end = dev->length;
+		if (sector >= dev->length)
+			sector = dev->length;
+	}
+
+	if (!ped_geometry_init(value->range, dev, start, end - start + 1LL))
+		return 0;
+	value->sector = sector;
+	return 1;
+	
+}
+
 #define ESC	'\033'	/* ^[ */
 /* TODO: This is not quite OK at the moment... */
 /* We ask the user where to put the partition in a region. Used for creating and resizing partitions */
 /* first and last mark the margins of the region, start and end mark the current values */
 int
-query_part_position(const char* prompt, const void* context, PedSector *start,
-                    PedSector *end,  PedSector first, PedSector last,
-                    PedDevice *dev, PedConstraint *constraint, UIOpts *opts) {
+query_part_position(const char* prompt, const void* context, PartPos *pos,
+                    PedSector first, PedSector last, PedDevice *dev,
+                    PedConstraint *constraint, UIOpts *opts) {
 	
-	PedSector length = *end-*start+1, min_len = 0, max_len = dev->length;
+	PedSector length = pos->end.sector - pos->start.sector + 1LL;
+	PedSector min_len = 0, max_len = dev->length;
+	PedGeometry *range = NULL;
 	int where,i,done;
 	char *temp,*temp2,*def_str, possibilities[6], buf[SMALLBUF];
+
+	/* We reset the ranges */
+	pos->start.range = NULL;
+	pos->end.range = NULL;
+
 	i = 0;
 	/* If we have a constraint, check whether we can move the partition start */
 	if (constraint && (constraint->start_align->grain_size == 0 || 
 	                   constraint->start_range->length == 1))
 		where = 's';
 	else {
-		/* If there the first possible sector is not the start of the selected partition,
-			we need one choice in the menu to make the start fixed and one to move it back.
-			When it is, 'b' and 's' do the same. We ditch 'b' for more friendly resize menu,
-			action_new should use 's' as it doesn't matter there. 
-			FIXME: I wrote this fragment better, but ruined it. Now I'm too lazy to fix it */
-		if (first < *start) possibilities[i++] = 'b';
+	/* If there the first possible sector is not the start of the selected partition,
+	we need one choice in the menu to make the start fixed and one to move it back.
+	When it is, 'b' and 's' do the same. We ditch 'b' for more friendly resize menu,
+	action_new should use 's' as it doesn't matter there. 
+	FIXME: I wrote this fragment better, but ruined it. Now I'm too lazy to fix it */
+		if (first < pos->start.sector) possibilities[i++] = 'b';
 		possibilities[i++] = 's';
 		possibilities[i++] = 'e';
 		possibilities[i++] = 'c';
 		} else {
 			*opts &= ~UI_CUSTOM_VALUES;
 			if (where == 's')
-				max_len = last-*start+1;
+				max_len = last - pos->start.sector + 1LL;
 			else
-				max_len = last-first+1;
+				max_len = last - first + 1LL;
 			if (constraint) {
 				if (constraint->max_size < max_len)
 					max_len = constraint->max_size;
 				done = 1;
 				/* Get the new size only if the user modified it */
 				if(strcmp(temp,def_str)) {
-					if (!ped_unit_parse(temp,dev,&length,NULL)) {
+					if (!ped_unit_parse(temp,dev,&length,&range)) {
 						done = !ped_exception_throw(
 							PED_EXCEPTION_ERROR,
 							PED_EXCEPTION_RETRY_CANCEL,
 				if (temp) ped_free(temp);
 				if (def_str) ped_free(def_str);
 			}
+			/* If we place it at the beginning, we will have range
+			   with the end sector and conversly if we place it at
+			   the end. We set the desired range and then we move
+			   the sector with the range so that the sector is equal
+			   to the desired result */
 			if (where == 'b') {
-				*start = first;
-				*end = first+length-1;
+				
+				pos->start.sector = first;
+				/* Desired: end = first+length-1LL */
+				pos->end.sector = length;
+				pos->end.range = range;
+				move_sector(dev,&(pos->end),first-1LL);
 			}
 			else if (where == 'e') {
-				*end = last;
-				*start = last-length+1;
+				pos->end.sector = last;
+				/* Desired: start = last-length+1LL */ 
+				pos->start.sector = length;
+				pos->start.range = range;
+				move_sector(dev,&(pos->start),last-2*length+1LL);
 			}
 			else if (where == 's') {
-				*end = *start+length-1;
+				/* Desired: end = start+length-1LL */
+				pos->end.sector = length;
+				pos->end.range = range;
+				move_sector(dev, (&pos->end),
+				            pos->start.sector-1LL);
 			}
 		}
 	}
 	return 1;
 }
 
+
+/* GET_SECT should have the same value as UI_CUSTOM_VALUES, as
+   we want to query sector when UI_CUSTOM_VALUES is specified */
+typedef enum {
+	/* Query the user for a new sector */
+	GET_SECT = UI_CUSTOM_VALUES, 
+	/* Initialise the range */
+	NO_RANGE = UI_CUSTOM_VALUES*2
+} 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 */
 static int
-get_sector (const char* prompt, PedDevice* dev, PedSector* value,
-            PedGeometry** range, PedSector prev, int custom)
+get_sector (const char* prompt, PedDevice* dev, SectPos *value,
+            PedSector prev, GSOpts opts)
 {
 	char*	def_str;
 	char*	input = NULL;
 	char*	buf;
 	int	valid;
 	
-	if (!custom) {
-		if (range) {
-			*range = ped_geometry_new(dev,*value,1);
-			return *range != NULL;
+	/* If the user should be queried about the sector and a range needs to
+	   be initialised, initialise one with a length of 1 sector, but please,
+	   when using this function, NOTE that this is not always enough */
+	if (!(opts & GET_SECT)) {
+		if (!value->range && !(opts & NO_RANGE)) {
+			value->range = ped_geometry_new(dev,value->sector,1);
+			return value->range != NULL;
 		}
 		return 1;
 	}
 
-	def_str = ped_unit_format (dev, *value);
-	//if (*value) 
-	input = strdup(def_str);
+	def_str = ped_unit_format (dev, value->sector);
+	if (def_str) 
+		input = strdup(def_str);
 	uiquery->getstring(prompt,&input,NULL,NULL,1);
 	
 
-	/* def_str might have rounded *value a little bit.  If the user picked
+	/* def_str might have rounded sector 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);
+	if (input && def_str && !strcmp (input, def_str)) {
+		if (!(opts & NO_RANGE)) {
+			value->range = ped_geometry_new (dev,value->sector,1);
 			ped_free (def_str);
 			ped_free (input);
-			return *range != NULL;
+			return value->range != NULL;
 		}
 		ped_free (def_str);
 		ped_free(input);
 
 	ped_free (def_str);
 	if (!input) {
-		*value = 0;
-		if (range)
-			*range = NULL;
+		value->sector = 0;
+		if (!(opts & NO_RANGE))
+			value->range = NULL;
 		return 0;
 	}
-	/* TODO: Improve... */
+
+	/* If the string starts with +, interpret as size */
 	if (prev >= 0 && input[0] == '+') {
-		PedGeometry *geom;
-		PedSector sector, start, end;
-		/* If the string starts with +, interpret as size */
+		/* TODO: Improve */
+		SectPos pos; 
+		/* We parse it into a SectPos */
 		buf = calloc(128,sizeof(char));
 		valid = sscanf(input, "+%127s", buf);
 		free(input);
 			free(buf);
 			return 0;
 		}
-		valid = ped_unit_parse(buf, dev, &sector, &geom);
+		valid = ped_unit_parse(buf, dev, &(pos.sector), &(pos.range));
 		free(buf);
-		if (!valid) return 0;
-		/* Recalculate the sector and the range */
-		start = geom->start + prev - 1;
-		end = geom->end + prev - 1;
-		sector += prev - 1;
-		/* Check if they are outside the disk */
-		if (start >= dev->length) return 0;
-		if (sector >= dev->length) sector = dev->length - 1;
-		if (end >= dev->length) end = dev->length - 1;
-		valid = ped_geometry_init(geom, dev,
-		                          start, end - start + 1);
+		if (!valid) {
+			if (pos.range) ped_geometry_destroy(pos.range);
+			return 0;
+		}
+		/* We move the sector, so it represents the end sector */
+		valid = move_sector(dev,&pos,prev-1LL);
 		if (valid) {
-			if (range) *range = geom;
-			*value = sector;
+			if (!(opts & NO_RANGE))
+				value->range = pos.range;
+			value->sector = pos.sector;
+		} else {
+			ped_geometry_destroy(pos.range);
 		}
 		
 	} else {
-		valid = ped_unit_parse (input, dev, value, range);
+		valid = ped_unit_parse(input, dev, &(value->sector),
+		                                   &(value->range));
 		ped_free (input);
 	}
 	return valid;
 }
 
+/* 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)
+{
+        if (!get_sector (_("Start"), dev, &(pos->start), -1LL, opts))
+                return 0;
+	/* 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))
+                return 0;
+	return 1;
+}
+
+
 static int
 ask_boolean_question(const char* prompt) {
 	int answer = 0;
  * If the parameter newpart is not NULL, it is set to the newly created partition */
 /* NOTE: On some occassions it changes the *newpart to NULL! */
 int
-perform_mkpart (PedDisk* disk, PedSector start, PedSector end, PedPartitionType part_type,
+perform_mkpart (PedDisk* disk, PartPos *pos, PedPartitionType part_type,
 	const PedFileSystemType* fs_type, PedPartition **newpart, UIOpts opts)
 {
-	PedGeometry		*range_start = NULL, *range_end = NULL;
         PedPartition*            part;
-        /*PedSector                start = 0, end = 0;*/
         PedConstraint*           user_constraint;
         PedConstraint*           dev_constraint;
         PedConstraint*           final_constraint;
                                          PED_DISK_TYPE_PARTITION_NAME)) 
                 uiquery->getstring(_("Partition name"), &part_name, NULL, NULL, 1); 
 
-        if (!get_sector (_("Start"), disk->dev, &start, &range_start, -1,
-	                 opts & UI_CUSTOM_VALUES))
-                goto error;
-        if (!get_sector (_("End"), disk->dev, &end, &range_end, start, 
-	                 opts & UI_CUSTOM_VALUES))
-                goto error;
-        
+	if (!get_position(pos, disk->dev, opts & UI_CUSTOM_VALUES))
+		goto error; 
+	/*ped_exception_throw(PED_EXCEPTION_INFORMATION,
+			PED_EXCEPTION_OK,
+			"Create %llds-%llds (%llds-%llds)-(%llds-%llds)",
+			pos->start.sector, pos->end.sector,
+			pos->start.range->start, pos->start.range->end,
+			pos->end.range->start, pos->end.range->end);
+	*/      
+
         /* processing starts here */
-        part = ped_partition_new (disk, part_type, fs_type, start, end);
+        part = ped_partition_new (disk, part_type, fs_type,
+	                          pos->start.sector, pos->end.sector);
         if (!part)
                 goto error;
 
-        snap_to_boundaries (&part->geom, NULL, disk, range_start, range_end);
+        snap_to_boundaries (&part->geom, NULL, disk,
+	                    pos->start.range, pos->end.range);
 
         /* create constraints */
-        user_constraint = constraint_from_start_end (disk->dev, range_start,
-                        range_end);
+        user_constraint = constraint_from_start_end (disk->dev,
+                        pos->start.range, pos->end.range);
         PED_ASSERT (user_constraint != NULL, return 0);
 
         dev_constraint = ped_device_get_constraint (disk->dev);
                
                 if (ped_disk_add_partition (disk, part,
                                         ped_constraint_any (disk->dev))) {
-                        start_usr = ped_unit_format (disk->dev, start);
-                        end_usr   = ped_unit_format (disk->dev, end);
+                        start_usr = ped_unit_format (disk->dev, pos->start.sector);
+                        end_usr   = ped_unit_format (disk->dev, pos->end.sector);
                         start_sol = ped_unit_format (disk->dev, part->geom.start);
                         end_sol   = ped_unit_format (disk->dev, part->geom.end);
-
+			/* TODO: With the changes from 06/08/06, we might remove 
+			   the "if" and warn the user always if this is out of 
+			   range */
 			if (opts & UI_CUSTOM_VALUES)
 				switch (ped_exception_throw (
 					PED_EXCEPTION_WARNING,
         ped_constraint_destroy (dev_constraint);
 
         
-        if (range_start != NULL)
-                ped_geometry_destroy (range_start);
-        if (range_end != NULL)
-                ped_geometry_destroy (range_end);
+        if (pos->start.range != NULL)
+                ped_geometry_destroy (pos->start.range);
+        if (pos->end.range != NULL)
+                ped_geometry_destroy (pos->end.range);
         
         if (start_usr != NULL)
                 ped_free (start_usr);
 error_destroy_part:
         ped_partition_destroy (part);
 error:
-        if (range_start != NULL)
-                ped_geometry_destroy (range_start);
-        if (range_end != NULL)
-                ped_geometry_destroy (range_end);
+        if (pos->start.range != NULL)
+                ped_geometry_destroy (pos->start.range);
+        if (pos->end.range != NULL)
+                ped_geometry_destroy (pos->end.range);
 
         if (start_usr != NULL)
                 ped_free (start_usr);
 }
 
 int
-perform_mkpartfs (PedDisk* disk, PedSector start, PedSector end, PedPartitionType part_type,
+perform_mkpartfs (PedDisk* disk, PartPos *pos, PedPartitionType part_type,
 	const PedFileSystemType* fs_type, PedPartition **newpart, UIOpts opts)
 {
 
 		(_("WARNING: This writes all data to disk automatically, continue?"))) 
 			return 0;
 	}
-/*
-        if (!ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_EXTENDED)) {
-		if (part_type && part_type != PED_PARTITION_NORMAL)
-			return 0;
-                part_type = PED_PARTITION_NORMAL;
-        } else if (!part_type) {
-                if (!get_part_type (_("Partition type"), disk, &part_type))
-                        return 0;
-        }
 
-        if (part_type == PED_PARTITION_EXTENDED) {
-                ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
-                        _("An extended partition cannot hold a file system."));
-                return 0;
-        }
-*/
         if (!fs_type) {
                 if (!get_fs_type (_("File system type"), &fs_type, 1))
                         return 0;
         }
-	if (!perform_mkpart(disk,start,end,part_type,fs_type,&part,opts | UI_MAKEPARTFS))
+	if (!perform_mkpart(disk,pos,part_type,fs_type,&part,opts | UI_MAKEPARTFS))
 		return 0;
 	if (!perform_mkfs(disk,part,fs_type,opts & ~UI_WARN_COMMIT))
 		return 0;
 
 /* FIXME: This function seems to be problematic, deal with it appropriately in the ui */
 int
-perform_move (PedDisk *disk, PedPartition *part, PedSector start, PedSector end, UIOpts opts)
+perform_move (PedDisk *disk,  PedPartition* part, PartPos *pos, UIOpts opts)
 {
        
         PedFileSystem*  fs;
         PedFileSystem*  fs_copy;
         PedConstraint*  constraint;
-        /*PedSector       start = 0, end = 0;*/
-        PedGeometry     *range_start = NULL, *range_end = NULL;
         PedGeometry     old_geom, new_geom;
 
 
                 goto error;
 
         /* get new target */
-        if (!get_sector (_("Start"), disk->dev, &start, &range_start, -1, 
-	                 opts & UI_CUSTOM_VALUES))
-                goto error_close_fs;
-        if (opts & UI_CUSTOM_VALUES)
-		end = start + old_geom.length - 1;
-        if (!get_sector (_("End"), disk->dev, &end, &range_end, start, 
-	                 opts & UI_CUSTOM_VALUES))
-                goto error_close_fs;
+	if (!get_position(pos, disk->dev, opts & UI_CUSTOM_VALUES))
+		goto error_close_fs; 
 
         /* set / test on "disk" */
-        if (!ped_geometry_init (&new_geom, disk->dev, start, end - start + 1))
+        if (!ped_geometry_init (&new_geom, disk->dev, pos->start.sector,
+                                pos->end.sector - pos->start.sector + 1LL))
                 goto error_close_fs;
-        snap_to_boundaries (&new_geom, NULL, disk, range_start, range_end);
+        snap_to_boundaries (&new_geom, NULL, disk,
+                            pos->start.range, pos->end.range);
 
         constraint = constraint_intersect_and_destroy (
                         ped_file_system_get_copy_constraint (fs, disk->dev),
-                        constraint_from_start_end (disk->dev, range_start, range_end));
-	/* So here we do something worth commiting and dangerous. UI *must* do something if we fail
-	   and need_commit is true... */
+                        constraint_from_start_end (disk->dev, pos->start.range,
+                                                   pos->end.range));
+	/* So here we do something worth commiting and dangerous. UI *must* 
+	   do something if we fail and need_commit is true... */
 	uiquery->need_commit = 1;
         if (!ped_disk_set_partition_geom (disk, part, constraint,
                                           new_geom.start, new_geom.end))
         /* do the move */
         fs_copy = ped_file_system_copy (fs, &part->geom, uiquery->timer);
         if (!fs_copy) {
-		//ped_constraint_destroy (constraint);
-		//constraint = ped_constraint_any(disk->dev);
-		//ped_disk_set_partition_geom(disk,part,constraint,old_geom.start,old_geom.end);
+		#if 0
+		ped_constraint_destroy (constraint);
+		constraint = ped_constraint_any(disk->dev);
+		ped_disk_set_partition_geom(disk,part,constraint,
+		                            old_geom.start,old_geom.end);
+		#endif
 		ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
 			_("Couldn't run the partition copy"));
                 goto error_close_fs;
                 goto error;
 	}
 	uiquery->need_commit = 0;
-        if (range_start != NULL)
-                ped_geometry_destroy (range_start);
-        if (range_end != NULL)
-                ped_geometry_destroy (range_end);
+        if (pos->start.range != NULL)
+                ped_geometry_destroy (pos->start.range);
+        if (pos->end.range != NULL)
+                ped_geometry_destroy (pos->end.range);
         return 1;
 
 error_destroy_constraint:
 error_close_fs:
         ped_file_system_close (fs);
 error:
-        if (range_start != NULL)
-                ped_geometry_destroy (range_start);
-        if (range_end != NULL)
-                ped_geometry_destroy (range_end);
+        if (pos->start.range != NULL)
+                ped_geometry_destroy (pos->start.range);
+        if (pos->end.range != NULL)
+                ped_geometry_destroy (pos->end.range);
         return 0;
 }
 
 	if (uiquery->need_commit)
 		if (!perform_commit(disk,UI_DEFAULT))
 			goto error;
-        if (!get_sector (_("Start"), disk->dev, &start, NULL, -1, 
-	                 opts & UI_CUSTOM_VALUES))
-                goto error;
-        if (!get_sector (_("End"), disk->dev, &end, NULL, start, 
-	                 opts & UI_CUSTOM_VALUES))
-                goto error;
-
+	if (opts & UI_CUSTOM_VALUES) {
+		PartPos pos;
+		pos.start.sector = start;
+		pos.end.sector = end;
+		if (!get_position(&pos, disk->dev, GET_SECT | NO_RANGE ))
+			goto error;
+		start = pos.start.sector;
+		end = pos.end.sector;
+	}
         fuzz = PED_MAX (PED_MIN ((end - start) / 10, MEGABYTE_SECTORS(disk->dev)),
                         MEGABYTE_SECTORS(disk->dev) * 16);
 
 
 
 int
-perform_resize (PedDisk *disk, PedPartition *part, PedSector start, PedSector end, UIOpts opts)
+perform_resize (PedDisk *disk, PedPartition *part, PartPos *pos, UIOpts opts)
 {
 
         PedFileSystem           *fs;
         PedConstraint           *constraint;
-        PedGeometry             *range_start = NULL, *range_end = NULL;
         PedGeometry             new_geom;
 
 
                         goto error;
         }
 
-        if (!get_sector (_("Start"), disk->dev, &start, &range_start, -1,
-	                 opts & UI_CUSTOM_VALUES))
-                goto error;
-        if (!get_sector (_("End"), disk->dev, &end, &range_end, start, 
-	                 opts & UI_CUSTOM_VALUES))
-                goto error;
+ 	if (!get_position(pos, disk->dev,opts & UI_CUSTOM_VALUES))
+		goto error; 
 
-	/* FIXME: Decide when exactly we need to "fuzzify" */
+	/* FIXME: This is most likely not needed */
 	//if (!(opts & UI_CUSTOM_VALUES) && (opts & UI_NO_FS_RESIZE)) {
 	if (opts & UI_RESIZE_INEXACT) {
-		if (part->geom.start != start)
-			fuzzify(range_start,disk->dev,NULL);
-		if (part->geom.end != end)
-			fuzzify(range_end,disk->dev,NULL);
+		if (part->geom.start != pos->start.sector)
+			fuzzify(pos->start.range,disk->dev,NULL);
+		if (part->geom.end != pos->end.sector)
+			fuzzify(pos->end.range,disk->dev,NULL);
 	}
 	//}
-        if (!ped_geometry_init (&new_geom, disk->dev, start, end - start + 1))
+	if (!ped_geometry_init (&new_geom, disk->dev, pos->start.sector,
+	                        pos->end.sector - pos->start.sector + 1LL))
                 goto error;
         snap_to_boundaries (&new_geom, &part->geom, disk,
-                            range_start, range_end);
-	/* If the partition is extended or we have UI_NO_FS_RESIZE, don't try to resize the fs, FIXME */
+                            pos->start.range, pos->end.range);
+	/* If the partition is extended or we have UI_NO_FS_RESIZE,
+	   don't try to resize the fs... FIXME? */
         if (part->type == PED_PARTITION_EXTENDED || (opts & UI_NO_FS_RESIZE)) {
 		/* I really hope this part isn't dangerous */
                 constraint = constraint_from_start_end (disk->dev,
-                                range_start, range_end);
+                                pos->start.range, pos->end.range);
                 if (!ped_disk_set_partition_geom (disk, part, constraint,
                                                   new_geom.start, new_geom.end))
                         goto error_destroy_constraint;
 			goto error;
 		constraint = constraint_intersect_and_destroy (
 				ped_file_system_get_resize_constraint (fs),
-				constraint_from_start_end (
-					disk->dev, range_start, range_end));
+				constraint_from_start_end (disk->dev,
+					pos->start.range, pos->end.range));
 		/* We will play with the geometry */
 		uiquery->need_commit = 1;
                 if (!ped_disk_set_partition_geom (disk, part, constraint,
         }
 
         ped_constraint_destroy (constraint);
-        if (range_start != NULL)
-                ped_geometry_destroy (range_start);
-        if (range_end != NULL)
-                ped_geometry_destroy (range_end);
+        if (pos->start.range != NULL)
+                ped_geometry_destroy (pos->start.range);
+        if (pos->end.range != NULL)
+                ped_geometry_destroy (pos->end.range);
         return 1;
 
 error_close_fs:
 error_destroy_constraint:
         ped_constraint_destroy (constraint);
 error:
-        if (range_start != NULL)
-                ped_geometry_destroy (range_start);
-        if (range_end != NULL)
-                ped_geometry_destroy (range_end);
+        if (pos->start.range != NULL)
+                ped_geometry_destroy (pos->start.range);
+        if (pos->end.range != NULL)
+                ped_geometry_destroy (pos->end.range);
         return 0;
 }
 
 	UI_FUNCTION_SPECIFIC_2 = 16
 };
 
+/* Structures used to specify partition position */
+typedef struct _SectPos SectPos;
+struct _SectPos {
+	PedSector sector;
+	PedGeometry *range;
+};
+
+typedef struct _PartPos PartPos;
+struct _PartPos {
+	SectPos start;
+	SectPos end;
+};
+
 /* Specific for perform_resize: */
 #define UI_NO_FS_RESIZE UI_FUNCTION_SPECIFIC_1	/* Will only change geometry */
 #define UI_RESIZE_INEXACT UI_FUNCTION_SPECIFIC_2 /* Exact resize */
 #define can_create_logical(disk) _can_create_logical(disk)
 
 /* Main functions */
-/* NOTE: resize, move and perhaps copy are dangerous. Please, make your ui check their status.
-   if they FAIL and leave need_commit to true, please reread the partition table from the device.
-   Sorry for the inconvinience. I might change this in the future so that it is not needed */
+
+/*
+ * NOTE: resize, move and perhaps copy are dangerous.
+ * Please, make your ui checks their status. If they FAIL and leave need_commit
+ * to true, please reread the partition table from the device.
+ * Sorry for the inconvinience. I might change this in the future so that it is
+ * not needed
+ */
 extern int perform_check (PedDisk*, PedPartition*);
 /* The parameters are the *destination* disk and partition */
 extern int perform_cp (PedDisk*, PedPartition*, UIOpts);
 extern int perform_mklabel (PedDevice*, PedDisk**, const PedDiskType*);
 
-extern int perform_mkfs (PedDisk*, PedPartition*, const PedFileSystemType*, UIOpts);
-
-extern int perform_mkpart (PedDisk*, PedSector start, PedSector end,
+extern int perform_mkfs (PedDisk*, PedPartition*,
+                         const PedFileSystemType*, UIOpts);
+/* NOTE: The PartPos structs *are* modified when calling these function */
+extern int perform_mkpart (PedDisk*, PartPos* pos,
                       PedPartitionType,	const PedFileSystemType*,
                       PedPartition **newpart, UIOpts);
 
-extern int perform_mkpartfs (PedDisk*, PedSector start, PedSector end,
+extern int perform_mkpartfs (PedDisk*, PartPos* pos,
                         PedPartitionType, const PedFileSystemType*,
                         PedPartition **newpart, UIOpts);
 
-extern int perform_move (PedDisk*, PedPartition*, PedSector start, PedSector end,
+extern int perform_move (PedDisk*, PedPartition*, PartPos* pos,
                     UIOpts);
 extern int perform_name (PedDisk*, PedPartition*, char*);
 extern int perform_rescue (PedDisk*, PedSector start, PedSector end, UIOpts);
-extern int perform_resize (PedDisk*, PedPartition*, PedSector start, PedSector end,
+extern int perform_resize (PedDisk*, PedPartition*, PartPos* pos,
                       UIOpts);
 extern int perform_rm (PedDisk*, PedPartition*);
 extern int perform_set_system (PedDisk*, PedPartition*, const PedFileSystemType*);
 
 /* Choose the partition position */
 extern int
-query_part_position(const char* prompt, const void* context, PedSector *start,
-                    PedSector *end,  PedSector first, PedSector last,
-                    PedDevice *dev, PedConstraint *constraint, UIOpts *opts);
+query_part_position(const char* prompt, const void* context, PartPos* pos,
+                    PedSector first, PedSector last, PedDevice *dev, 
+                    PedConstraint *constraint, UIOpts *opts);
 #endif
 static int
 do_mkpart (PedDisk **disk)
 {
+	PartPos			 pos;
 	PedPartition* 		 part;
 	PedPartitionType	 part_type = 0;
 	const PedFileSystemType* fs_type = ped_file_system_type_get ("ext2");
-	//PedSector		 start = 0, end = 0;
-	//PedGeometry 		 *range_start = NULL, *range_end = NULL;
-	//PedConstraint*		 constraint;
 	char*			 peek_word;
-
+	
 	if (!fdisk_command_line_get_part_type (_("Partition type?"), *disk,
 					       &part_type))
 		return 0;
 	}
 	if (peek_word)
 		ped_free (peek_word);
-	/* TODO: Specify size in fdisk's fashion */
-	return perform_mkpart (*disk, 0LL, 0LL, part_type, fs_type,
+	/* TODO: Make default position more friendly */
+	pos.start.sector = 0LL;
+	pos.end.sector = 0LL;
+	return perform_mkpart (*disk, &pos, part_type, fs_type,
 	                       NULL, UI_CUSTOM_VALUES);
 }
 
 static int
 do_mkpartfs (PedDisk** disk)
 {
-	return perform_mkpartfs (*disk, 0LL, 0LL, 0, NULL, NULL, 
+	PartPos pos;
+	/* TODO: Make default position more friendly */
+	pos.start.sector = 0LL;
+	pos.end.sector = 0LL;
+	return perform_mkpartfs (*disk, &pos, 0, NULL, NULL, 
 	                         UI_CUSTOM_VALUES | UI_WARN_COMMIT | 
 	                         UI_SPECIFY_PART_TYPE);
 }
 static int
 do_move (PedDisk** disk)
 {
-	return perform_move (*disk, NULL, 0LL, 0LL, UI_CUSTOM_VALUES);
+	PartPos pos;
+	/* TODO: Make default position more friendly */
+	pos.start.sector = 0LL;
+	pos.end.sector = 0LL;
+	return perform_move (*disk, NULL, &pos, UI_CUSTOM_VALUES);
 }
 
 static int

tests/check_common.c

 	PedFileSystemType* walk = NULL;
 	PedGeometry *geom = NULL;
 	PedPartition *part = NULL;
+	PartPos pos = { {0LL, NULL}, {20000LL, NULL} };
 	int i;
 	/* First we create an extended partition */
 	//set_exception_error("Juggling partitions");
 	fail_unless(perform_mklabel(dev,&disk,ped_disk_type_get("msdos")),
 	            "We could create the label for juggling partitions");
-	fail_unless(perform_mkpart(disk,0LL,20000LL,PED_PARTITION_EXTENDED,NULL,NULL,
+	fail_unless(perform_mkpart(disk,&pos,PED_PARTITION_EXTENDED,NULL,NULL,
 	                      UI_DEFAULT),
 	            "Failed to create an extended partition");
 
 }
 
 /* TODO: Make this a function */
-#define run_pos_test(part,constraint,desired_opts,poss_ok,pos,strings) \
-	start = 2000LL; \
-	end = 21999LL; \
+#define run_pos_test(part,constraint,desired_opts,poss_ok,posg,strings) \
+	pos.start.sector = 2000LL; \
+	pos.end.sector = 21999LL; \
 	opts = UI_DEFAULT; \
 	pos_gets = 0; string_gets = 0; \
-	fail_unless(query_part_position("", NULL, &start, &end,  \
+	fail_unless(query_part_position("", NULL, &pos,  \
 	                                where.start, where.end, \
 	                                dev, (constraint), &opts), \
 	            "Query partition position %s failed", (part)); \
-	fail_unless(start == desired.start && end == desired.end, \
+	fail_unless(pos.start.sector == desired.start && \
+	            pos.end.sector == desired.end, \
 	            "Partition position %s wrong. Was %llds-%llds, " \
 	            "should be %llds-%llds.", \
-	            (part), start, end, desired.start, desired.end); \
+	            (part), pos.start.sector, pos.end.sector, \
+	            desired.start, desired.end); \
 	fail_unless(opts == (desired_opts), "Test %s opts wrong. " \
 	            "Was %d, should be %d.", opts, (desired_opts)); \
 	fail_unless(string_gets == (strings), "%s prompted wrong times." \
 	            "Strings %d, should be %d. Pos %d should be %d.", \
-	             (part), string_gets, (strings), pos_gets, (pos)); \
+	             (part), string_gets, (strings), pos_gets, (posg)); \
 	fail_unless(poss_compare(pos_poss,(poss_ok)), \
 	            "%s: incorrect possibilities. Got %s, should be %s.", \
 	             (part), pos_poss, (poss_ok))
 START_TEST (test_partpos) {
 	PedGeometry desired;
 	PedGeometry where;
-	PedSector start,end;
+	PartPos pos;
 
 	PedConstraint *constraint = ped_device_get_constraint(dev);
 

tests/functions.c

 _mkpart (int how, PedSector start, PedSector end,
                       PedPartitionType type, const PedFileSystemType* fs_type,
                       PedPartition **newpart,
-                      int ch_mkpart (PedDisk*, PedSector start, PedSector end,
+                      int ch_mkpart (PedDisk*, PartPos *pos,
                                      PedPartitionType, const PedFileSystemType*,
                                      PedPartition **, UIOpts))
 {
 	int status;
 	string_gets = 0;
 	int strings;
+	PartPos pos = { { 0LL, NULL }, { 0LL, NULL } };
 	switch(how % 3) {
 		/* We create the partition by specifying start and end */
 		case 0:
 			uiquery.getstring = getstring;
 			strncpy(string[0], fs_type->name, BUFSIZE-1);
 			
-			status = ch_mkpart(disk,start,end,type,NULL,newpart,
+			pos.start.sector = start;
+			pos.end.sector = end;
+			status = ch_mkpart(disk,&pos,type,NULL,newpart,
 			                 UI_DEFAULT);
 			fail_unless(string_gets == strings,
 				"User queried for %d strings instead of %d",
 		case 1:
 			strncpy(string[0],DEFAULT_STRING,BUFSIZE);
 			uiquery.getstring = getstring;
-			status = ch_mkpart(disk,start,end,type,fs_type,newpart,
+
+			pos.start.sector = start;
+			pos.end.sector = end;
+			status = ch_mkpart(disk,&pos,type,fs_type,newpart,
 			                 UI_CUSTOM_VALUES);
 			fail_unless(string_gets == 2,
 				"User queried for %d strings instead of 2",
 			string[0][BUFSIZE] = 2;
 			snprintf(string[0],BUFSIZE,"%llds",start);
 			snprintf(string[1],BUFSIZE,"%llds",end);
-			status = ch_mkpart(disk,0LL,0LL,type,fs_type,newpart,
+			status = ch_mkpart(disk,&pos,type,fs_type,newpart,
 			                 UI_CUSTOM_VALUES);
 			fail_unless(string_gets == 2,
 				"User queried for %d strings instead of 2",
 int
 _resmov (int how, PedPartition *part, PedSector start, PedSector end,
          UIOpts opts, int action (PedDisk*, PedPartition*,
-                                  PedSector start, PedSector end,
-                                  UIOpts))
+                                  PartPos *pos, UIOpts))
 {
+	PartPos pos = { { 0LL, NULL }, { 0LL, NULL } };
 	switch (how % 3) {
 		/* Specify start, end and partition */
 		case 0:
-			return action (disk, part, start, end, opts);
+			pos.start.sector = start;
+			pos.end.sector = end;
+			return action (disk, part, &pos, opts);
 		/* We specify start and end, ask for partition */
 		case 1:
 			uiquery.getint = NULL;
 			uiquery.getpart = getpart;
 			partition = part;
-			return action (disk, NULL, start, end, opts);
+
+			pos.start.sector = start;
+			pos.end.sector = end;
+			return action (disk, NULL, &pos, opts);
 		/* We ask for everything, the partition with number */
 		case 2:
 			uiquery.getpart = NULL;
 			snprintf(string[0],BUFSIZE,"%llds",start);
 			snprintf(string[1],BUFSIZE,"%llds",end);
 			opts |= UI_CUSTOM_VALUES;
-			return action (disk, NULL, 0LL, 0LL, opts);
+			return action (disk, NULL, &pos, opts);
 	}
 }
 

tests/functions.h

 extern int _mkpart (int how, PedSector start, PedSector end,
                       PedPartitionType type, const PedFileSystemType* fs_type,
                       PedPartition **newpart,
-                      int mkpart (PedDisk*, PedSector start, PedSector end,
-                                  PedPartitionType, const PedFileSystemType*,
+                      int mkpart (PedDisk*, PartPos *pos, PedPartitionType,
+                                  const PedFileSystemType*,
                                   PedPartition **, UIOpts));
 /* This is a function that both resizes and moves */
 extern int _resmov (int how, PedPartition *part, PedSector start, PedSector end,
                     UIOpts opts, int action (PedDisk*, PedPartition*,
-                                             PedSector start, PedSector end,
-                                             UIOpts));
+                                             PartPos *pos, UIOpts));
 
 extern void get_max_geometry(PedGeometry **geom, PedConstraint *constraint, 
                       PedPartition *part);
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.