Anonymous avatar Anonymous committed 25b072a

Comments (0)

Files changed (7)

 	return temp_a;
 }
 
+/* TODO: Move this to common? */
+/* 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 */
 static int
 _query_part_position(const MenuItem* menu, PedSector *start, PedSector *end, 
 		     PedSector first, PedSector last, PedConstraint *constraint, UIOpts *opts) {
+	
 	PedSector length = *end-*start+1, min_len = 0, max_len = dev->length;
 	int key,i,done;
 	char *temp,*temp2,*def_str, keys[6], buf[SMALLBUF];
 	return 1;
 	
 }
+/* TODO: Move to common? */
+/* This function returns the closest possible start and end for a partition
+   in the region fist-last */
+static int
+_fix_part_position(PedSector *start, PedSector *end, PedSector first,
+                   PedSector last)
+{
+	/* Current geometry */
+	PedGeometry *geom = ped_geometry_new (dev,*start,*end-*start+1);
+	if (!geom) return 0;
+	/* Possible range */
+	PedGeometry *range = ped_geometry_new (dev, first, last-first+1);
+	if (!range) {
+		ped_geometry_destroy(geom);
+		return 0;
+	}
+	PedConstraint *constraint = constraint_intersect_and_destroy(
+			constraint_from_start_end (dev, range, range),
+			ped_device_get_constraint (dev));
+
+	PedGeometry *new_geom = ped_constraint_solve_nearest(constraint, geom);
+
+	if (new_geom) {
+		if (*start != new_geom->start || *end != new_geom->end) {
+			/*char buf[SMALLBUF];
+			snprintf(buf,SMALLBUF,_("We changed the partition from %llds-%llds to %llds-%llds"),
+				*start,*end,new_geom->start,new_geom->end);
+			warning_waitkey(buf);*/
+			*start = new_geom->start;
+			*end = new_geom->end;
+			ped_geometry_destroy(new_geom);
+		}
+		return 1;
+	} 
+
+	return 0;
+
+}
+
+
 
 /* Emergency function. If some copy, move or resize fail, reread the disk from the device
    They commited it before failing, anyway. It is a bit like a hack, I might make the functions
 	else last = end;
 
 	menu_title(_("Where to place the resized partition"));
-	if(!_query_part_position(part_position,&start,&end,first,last,constraint,&opts)) {
+	if(!_query_part_position(part_position,&start,&end,first,last,
+	   constraint,&opts) || !_fix_part_position(&start,&end,first,last)) {
 		if (constraint) ped_constraint_destroy (constraint);
 		return 0;
 	}
 	if (constraint) ped_constraint_destroy (constraint);
 	
 
-	if (start != (*part)->geom.start || end != (*part)->geom.end)
+	if (opts & UI_CUSTOM_VALUES || start != (*part)->geom.start || end != (*part)->geom.end)
 		if (!do_resize(disk,*part,start,end,opts,&uiquery)) {
 			warning_waitkey(N_("Couldn't resize partition."));
 			/* Ooopsy */
-			if (uiquery.need_commit) _disk_reread(part);
+			if (!(opts & UI_NO_FS_RESIZE) && uiquery.need_commit)
+				_disk_reread(part);
 			return 0;
 		}
 	return 1;
 #define col_type 23
 #define col_fs 35
 
+/* TODO: Use printw, instead of buffer where possible ? */
+
 /* Partition list drawing function */
 static void
 plist_draw (PedDisk *cdisk, PedPartition *selected, int selnum, int *start) {
         ped_geometry_set (new_geom, start, end - start + 1);
 }
 
+/* This function makes a range less strict... */
+/* FIXME: Make this in a better way */
+void
+fuzzify (PedGeometry *geom, PedDevice *dev, PedConstraint *constraint) {
+	PedGeometry *new_geom;
+	if (!constraint)
+		constraint = ped_device_get_constraint(dev);
+	PedSector start = geom->start-127;
+	start = (start > 0 ? start : 0);
+	PedSector end = geom->start+127;
+	end = (end < dev->length-1 ? end : dev->length-1);
+	ped_geometry_set (geom, start, end-start+1);
+	new_geom = ped_constraint_solve_nearest (constraint, geom);
+	ped_geometry_set (geom, new_geom->start, new_geom->length);
+	ped_geometry_destroy(new_geom);
+
+}
 /* This functions constructs a constraint from the following information:
  *      start, is_start_exact, end, is_end_exact.
  *      
 	char*	def_str;
 	char*	input = NULL;
 	int	valid;
-	/* FIXME: Check this more thoroughfully */
-
+	
 	if (!custom) {
 		if (range) {
 			*range = ped_geometry_new(dev,*value,1);
 				error = _("The size of the partition is too big for %s filesystem");
 			}
 			/* FIXME: WHAT THE?.. */
+			/* OK, I got it, but I will leave it here for a while */
 			//else {
 			//	error = _("You can't use this partition for %s filesystem");
 			//}
 
         return 0;
 }
-/* TODO: get rid of warn */
+
 int
 do_mkpartfs (PedDisk* disk, PedSector start, PedSector end, PedPartitionType part_type,
 	const PedFileSystemType* fs_type, PedPartition **newpart, UIOpts opts, UICalls *uiquery)
 {
 
-        PedPartition*            part;
+        PedPartition*            part = NULL;
 
         
         if (!disk)
 		(_("WARNING: This writes all data to disk automatically, continue?"),uiquery)) 
 			return 0;
 	}
-
+/*
         if (!ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_EXTENDED)) {
 		if (part_type && part_type != PED_PARTITION_NORMAL)
 			return 0;
                         _("An extended partition cannot hold a file system."));
                 return 0;
         }
-
+*/
         if (!fs_type) {
                 if (!get_fs_type (_("File system type"), &fs_type, 1, uiquery))
                         return 0;
         return 0;
 }
 
+
+/* FIXME: It doesn't always work as expected */
 int
 do_resize (PedDisk *disk, PedPartition *part, PedSector start, PedSector end, UIOpts opts, UICalls *uiquery)
 {
 
         if (!disk)
                 goto error;
-	if (opts & UI_WARN_COMMIT && !(part->type == PED_PARTITION_EXTENDED || (opts & UI_NO_FS_RESIZE)))
-		if (!ask_boolean_question 
+	if (!(part->type == PED_PARTITION_EXTENDED || (opts & UI_NO_FS_RESIZE)))  {
+		if (opts & UI_WARN_COMMIT && !ask_boolean_question 
 		(_("WARNING: This writes all data to disk automatically, continue?"),uiquery)) 
 			return 0;
-	if (uiquery->need_commit)
-		if (!do_commit(disk,UI_DEFAULT,uiquery))
-			goto error;
+		if (uiquery->need_commit)
+			if (!do_commit(disk,UI_DEFAULT,uiquery))
+				goto error;
+	}
 	if (!part) 
         	if (!get_partition (_("Partition"), disk, &part, uiquery))
                 	goto error;
         if (!get_sector (_("End"), disk->dev, &end, &range_end, opts & UI_CUSTOM_VALUES, uiquery))
                 goto error;
 
+	if (!(opts & UI_CUSTOM_VALUES) && (opts & UI_NO_FS_RESIZE)) {
+		if (part->geom.start != start)
+			fuzzify(range_start,disk->dev,NULL);
+		if (part->geom.end != end)
+			fuzzify(range_end,disk->dev,NULL);
+	}
         if (!ped_geometry_init (&new_geom, disk->dev, start, end - start + 1))
                 goto error;
         snap_to_boundaries (&new_geom, &part->geom, disk,

tests/Makefile.am

 if MAKE_TESTS
 TESTS = \
-	check_common
+	check_common check_advanced
 
 noinst_PROGRAMS = \
-	check_common
+	check_common check_advanced
 
 check_common_SOURCES = \
 	check_common.c \
 	../src/strlist.h \
 	../src/strlist.c
 
+check_advanced_SOURCES = \
+	check_advanced.c \
+	functions.c \
+	functions.h \
+	../src/common.h \
+	../src/common.c \
+	../src/strlist.h \
+	../src/strlist.c
+
 check_common_LDADD = $(PARTED_LIBS) $(CHECK_LIBS)
+check_advanced_LDADD = $(PARTED_LIBS) $(CHECK_LIBS)
 endif MAKE_TESTS

tests/check_advanced.c

 #include "../src/common.h"
 #include "functions.h"
 
-/* TODO: Hack common.c not to commit and test resize, move... can we
-   test copy, too? */
-
-PedDevice* dev = NULL;
-PedDisk* disk = NULL;
-
+/* Well, just in case we need it ... */
+#if 0
 START_TEST (test_null)
 {
 	fail_unless(1 == 1, "If you are reading this message, you have "
 	                  "just won a brand-new pink-coloured supercomputer");
 }
 END_TEST
+#endif
+
+/* This takes ~160MB of disk space. */
+START_TEST (test_resize) {
+	change_size("msdos", UI_DEFAULT);
+}
+END_TEST
+
+
 
 Suite *common_suite(void)
 {
-	Suite *s = suite_create("Common functions");
-	TCase *tc_core = tcase_create("Core");
-	suite_add_tcase (s, tc_core);
-	tcase_add_test(tc_core, test_null);
+	Suite *s = suite_create("Advanced checks");
+	TCase *tc_resize = tcase_create("Resize");
+	suite_add_tcase (s, tc_resize);
+	tcase_set_timeout(tc_resize, 360);
+	tcase_add_test(tc_resize, test_resize);
 	return s;
 }
 
 {
 	int nf;
 	if (!init_tempfile()) return 1;
-	dev = open_device();
-	if (!dev) {
+	if (!open_device()) {
 		unlink_tempfile;
 		return 1;
 	}

tests/check_common.c

 #include "functions.h"
 
 
-/* No chance someone else decides to call this file this way, I hope */
 
-PedDevice *dev = NULL;
-PedDisk *disk = NULL;
-UICalls uiquery;
 
 
-START_TEST (test_null)
-{
-	fail_unless(1 == 1, "If you are reading this message, you have "
-	                  "just won a brand-new pink-coloured supercomputer");
-}
-END_TEST
-
 /* Here we test mklabel */
 START_TEST (test_mklabel)
 {
 END_TEST
 
 
-/* We will give the partitions below different sizes. I'll use rand */
-#define PART_LENGTH (2097151LL - ((unsigned int) rand() % 1048575))
-
-static void
-next_part_geometry(PedGeometry **geom, PedConstraint *constraint, int factor) {
-	PedSector start;
-	PedGeometry *temp;
-	if (!*geom)
-		 start = 0LL;
-	else {
-		start = (*geom)->end+1LL;
-		ped_geometry_destroy(*geom);
-	}
-	temp = ped_geometry_new(dev, start, PART_LENGTH*(long long)factor);
-	*geom = ped_constraint_solve_nearest(constraint,temp);
-	ped_geometry_destroy(temp);
-}
-
-
-static int
-_mkpart (int how, PedSector start, PedSector end,
-                      PedPartitionType type, const PedFileSystemType* fs_type,
-                      PedPartition **newpart) {
-	char *temp;
-	switch(how) {
-		/* We create the partition by giving start and end */
-		case 0:
-			return do_mkpart(disk,start,end,type,fs_type,newpart,
-			                 UI_DEFAULT,&uiquery);
-		/* We test that giving the default string is OK */
-		case 1:
-			strncpy(string[0],DEFAULT_STRING,BUFSIZE);
-			uiquery.getstring = getstring;
-			uiquery.getint = 0;
-			return do_mkpart(disk,start,end,type,fs_type,newpart,
-			                 UI_CUSTOM_VALUES,&uiquery);
-		/* We "let the user" specify and pretend to be the user */
-		case 2:
-			
-			string[0][BUFSIZE] = 2;
-			snprintf(string[0],BUFSIZE,"%llds",start);
-			snprintf(string[1],BUFSIZE,"%llds",end);
-			return do_mkpart(disk,0LL,0LL,type,fs_type,newpart,
-			                 UI_CUSTOM_VALUES,&uiquery);
-	}
-	
-}
-
 /* Here we test mkpart and rmpart */
 START_TEST (test_jugglepart) {
 	PedConstraint *constraint = ped_device_get_constraint(dev);
 	PedPartition *part = NULL;
 	int i;
 	/* First we create an extended partition */
+	//set_exception_error("Juggling partitions");
 	fail_unless(do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery),
 	            "We could create the label for juggling partitions");
 	fail_unless(do_mkpart(disk,0LL,20000LL,PED_PARTITION_EXTENDED,NULL,NULL,
 	/* We first try to create 20 partitions */
 	for (i = 0; i < 20; i++) {
 		next_part_geometry(&geom, constraint, 1);
-
-		fail_unless(_mkpart (i % 3, geom->start,  geom->end,
-				PED_PARTITION_LOGICAL, walk, &part),
+		fail_unless(_mkpart (i, geom->start,  geom->end,
+				PED_PARTITION_LOGICAL, walk, &part, do_mkpart),
 			"Failed to create %s partition. "
 			"Number %d, size %llds, start %llds, end %llds",
 			walk->name, i, geom->length, geom->start, geom->end);
 }
 END_TEST
 
-static void
-get_max_geometry(PedGeometry **geom, PedConstraint *constraint, 
-                 PedPartition *part)
-{
-	PedGeometry *temp = NULL;
-	if (*geom) ped_geometry_destroy(*geom);
-	PedSector start, end;
-	int i = 0;
 
-	if (part->prev && part->prev->type & PED_PARTITION_FREESPACE)
-		start = part->prev->geom.start;
-	else
-		start = part->geom.start;
-	if (part->next && part->next->type & PED_PARTITION_FREESPACE)
-		end = part->next->geom.end;
-	else
-		end = part->geom.end;	
-	temp = ped_geometry_new(dev, start, end-start+1LL);
-	*geom = ped_constraint_solve_nearest(constraint,temp);
-	ped_geometry_destroy(temp);
-
-}
-
-/* NOTE: If you create a rather big test disk, you can make this use llrand 
-         from functions.c instead. */
-static void
-randomize_position(PedSector *start, PedSector *end, PedGeometry *where, int i)
-{
-
-	if (i == 0) {
-		*start = where->start;
-		do {
-			*end = *start+((unsigned int)rand() % where->length)-1;
-		} while (are_near(*start,*end));
-	}
-	else if (i == 3) {
-		*end = where->end;
-		do {
-			*start = *end-((unsigned int)rand() % where->length)+1;
-		} while (are_near(*start,*end));
-	}
-	else {
-		PedSector mid = where->start +
-		                ((unsigned int) rand() % where->length)-1;
-		do {
-			*start = where->start+
-				((unsigned int) rand() % (mid - where->start));
-			*end = mid +
-				((unsigned int) rand() % (where->end - mid));
-		} while (are_near(*start,*end));
-	}
-}
-
-/* TODO: Make it check different partition table types !*/
+/* TODO: This fails on dvh and mac.  Figure out why. */
 /* Here we test (*only*) the size changing with do_resize */
-START_TEST (test_resize_size) {
-	PedConstraint *constraint = ped_device_get_constraint(dev);
-	PedGeometry *geom = NULL;
-	PedSector start, end;
-	PedPartition *part = NULL;
-	PedFileSystemType *fs_type = ped_file_system_type_get("ext2");
-	int i;
-	fail_unless(do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery),
-	            "We could create the label for resizing partitions");
-	srand(82181281);
-	for (i = 0; i < 4; i++) {
-		next_part_geometry(&geom, constraint, 1);
-		string[0][BUFSIZE] = 0;
-		strncpy(string[0],"Label",BUFSIZE);
-		uiquery.getstring = getstring;
-		fail_unless(_mkpart (0, geom->start,  geom->end,
-				PED_PARTITION_NORMAL,
- 				fs_type, &part),
-			"Failed to create partition for resize. "
-			"Number %d, size %llds, start %llds, end %llds",
-			i, geom->length, geom->start, geom->end);
-	}
-	while (part) {
-		get_max_geometry(&geom,constraint,part);
-		for (i = 0; i < 4; i++) {
-			randomize_position(&start,&end,geom,i);
-			fail_unless(do_resize (disk, part, start, end,
-			                       UI_NO_FS_RESIZE, &uiquery),
-			            "Could not do %d resize of partition %d "
-			            "from %llds-%llds to %llds-%llds "
-			            "in region %llds-%llds",
-			            i, part->num, part->geom.start,
-			            part->geom.end, start, end, geom->start,
-			            geom->end);
-			fail_unless(are_near(part->geom.start,start) &&
-			           are_near(part->geom.end,end),
-			           "The %d resize of partition %d was wrong "
-			           "we wanted %llds-%llds, we got %llds-%llds",
-			           i, part->num, start, end,
-			           part->geom.start, part->geom.end);
-		}
-		do {
-			part = part->prev;
-		} while (part && ((part->type & PED_PARTITION_METADATA) ||
-		         (part->type & PED_PARTITION_FREESPACE)));
-	}
-	
-
+START_TEST (test_chsize_msdos) {
+	change_size("msdos", UI_NO_FS_RESIZE);
 }
 END_TEST
 
+START_TEST (test_chsize_amiga) {
+	change_size("amiga", UI_NO_FS_RESIZE);
+}
+END_TEST
+
+START_TEST (test_chsize_mac) {
+	change_size("mac", UI_NO_FS_RESIZE);
+}
+END_TEST
+
+START_TEST (test_chsize_dvh) {
+	change_size("dvh", UI_NO_FS_RESIZE);
+}
+END_TEST
+
+START_TEST (test_chsize_pc98) {
+	change_size("pc98", UI_NO_FS_RESIZE);
+}
+END_TEST
+
+START_TEST (test_chsize_bsd) {
+	change_size("bsd", UI_NO_FS_RESIZE);
+}
+END_TEST
+
+START_TEST (test_chsize_sun) {
+	change_size("sun", UI_NO_FS_RESIZE);
+}
+END_TEST
+
+START_TEST (test_chsize_gpt) {
+	change_size("gpt", UI_NO_FS_RESIZE);
+}
+END_TEST
+
+START_TEST (test_chsize_loop) {
+	change_size("gpt", UI_NO_FS_RESIZE);
+}
+END_TEST
+
+
+
 Suite *common_suite(void)
 {
 	Suite *s = suite_create("Common functions");
-	TCase *tc_core = tcase_create("Core");
-	suite_add_tcase (s, tc_core);
-	tcase_add_test(tc_core, test_null);
-	tcase_add_test(tc_core, test_mklabel);
-	tcase_add_test(tc_core, test_jugglepart);
-	tcase_add_test(tc_core, test_resize_size);
+	TCase *tc_label = tcase_create("Label");
+	TCase *tc_juggle = tcase_create("Creation and deletion of partitions");
+	TCase *tc_chsize = tcase_create("Changing partiton sizes");
+	suite_add_tcase (s, tc_label);
+	suite_add_tcase (s, tc_juggle);
+	suite_add_tcase (s, tc_chsize);
+	tcase_add_test(tc_label, test_mklabel);
+	tcase_add_test(tc_juggle, test_jugglepart);
+	tcase_set_timeout(tc_chsize, 10);
+	tcase_add_test(tc_chsize, test_chsize_msdos);
+	tcase_add_test(tc_chsize, test_chsize_bsd);
+	tcase_add_test(tc_chsize, test_chsize_pc98);
+	tcase_add_test(tc_chsize, test_chsize_amiga);
+	tcase_add_test(tc_chsize, test_chsize_gpt);
+	//tcase_add_test(tc_chsize, test_chsize_dvh);
+	tcase_add_test(tc_chsize, test_chsize_loop);
+	//tcase_add_test(tc_chsize, test_chsize_mac);
 	return s;
 }
 
 {
 	int nf;
 	if (!init_tempfile()) return 1;
-	dev = open_device();
-	if (!dev) {
+	if (!open_device()) {
 		unlink_tempfile;
 		return 1;
 	}

tests/functions.c

 
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdarg.h>
 #include <check.h>
 #include <parted/parted.h>
 /* #include <endian.h> */
 #include "../src/strlist.h"
 #include "functions.h"
+#include "../src/common.h"
 
 /* We can't seek more... */
 #define SIZE 2147483647L
 
+PedDevice *dev = NULL;
+PedDisk *disk = NULL;
+UICalls uiquery;
 
 /* No chance someone else decides to call this file this way, I hope */
 static char tempfile[] = "/tmp/check_cfdisk_temporary_disk_drive";
 
+
+/* We need an exception handler */
 PedExceptionOption ex_opts;
 PedExceptionType ex_type;
+char exception_error[BUFSIZE];// = "%s";
 
-/* We need an exception handler */
+
+int set_exception_error(const char *format, ...) {
+	va_list args;
+	char buf[BUFSIZE];
+	va_start(args, format);
+	if(vsnprintf(buf, BUFSIZE, format, args) < 0) {
+		return -1;
+	}
+	va_end(args);
+	return snprintf(exception_error,BUFSIZE, "%s: %s", buf, "%s");
+}
+
+int reset_exception_error() {
+	strncpy(exception_error, "%s", BUFSIZE);
+}
+
 PedExceptionOption
 exception_handler (PedException* ex) {
 	ex_opts = ex->options;
 		case PED_EXCEPTION_FATAL:
 		case PED_EXCEPTION_BUG:
 		case PED_EXCEPTION_NO_FEATURE:
-			fail(ex->message);
-			break;
+			fail(exception_error, ex->message);
 	}
+	
 	if (ex_opts & PED_EXCEPTION_FIX)
 		return PED_EXCEPTION_FIX;
 	else if (ex_opts & PED_EXCEPTION_RETRY)
 	unlink(tempfile);
 }
 
-PedDevice *
-open_device() {
-	PedDevice *dev;
+int open_device() {
 	dev = ped_device_get(tempfile);
 	if (!ped_device_open(dev))
-		return NULL;
-	return dev;
+		return 0;
+	return dev != NULL;
 }
 
 
 	return result;
 
 }
-#endif
+#endif
+
+
+
+/* We will give the partitions below different sizes. I'll use rand */
+#define PART_LENGTH (2097151LL - ((unsigned int) rand() % 1048575))
+
+void
+next_part_geometry(PedGeometry **geom,
+                   PedConstraint *constraint, int factor) {
+	PedSector start;
+	PedGeometry *temp;
+	if (!*geom)
+		 start = 0LL;
+	else {
+		start = (*geom)->end+1LL;
+		ped_geometry_destroy(*geom);
+	}
+	temp = ped_geometry_new(dev, start, PART_LENGTH*(long long)factor);
+	*geom = ped_constraint_solve_nearest(constraint,temp);
+	ped_geometry_destroy(temp);
+}
+
+
+int
+_mkpart (int how, PedSector start, PedSector end,
+                      PedPartitionType type, const PedFileSystemType* fs_type,
+                      PedPartition **newpart,
+                      int ch_mkpart (PedDisk*, PedSector start, PedSector end,
+                                     PedPartitionType, const PedFileSystemType*,
+                                     PedPartition **, UIOpts, UICalls *))
+{
+	char *temp;
+	switch(how % 3) {
+		/* We create the partition by giving start and end */
+		case 0:
+			uiquery.getstring = getstring;
+			strncpy(string[0], fs_type->name, BUFSIZE-1);
+			string[0][BUFSIZE] = 0;
+			return ch_mkpart(disk,start,end,type,NULL,newpart,
+			                 UI_DEFAULT,&uiquery);
+		/* We test that giving the default string is OK */
+		case 1:
+			strncpy(string[0],DEFAULT_STRING,BUFSIZE);
+			uiquery.getstring = getstring;
+			uiquery.getint = 0;
+			return ch_mkpart(disk,start,end,type,fs_type,newpart,
+			                 UI_CUSTOM_VALUES,&uiquery);
+		/* We "let the user" specify and pretend to be the user */
+		case 2:
+			
+			string[0][BUFSIZE] = 2;
+			snprintf(string[0],BUFSIZE,"%llds",start);
+			snprintf(string[1],BUFSIZE,"%llds",end);
+			return ch_mkpart(disk,0LL,0LL,type,fs_type,newpart,
+			                 UI_CUSTOM_VALUES,&uiquery);
+	}
+	
+}
+
+
+void
+get_max_geometry(PedGeometry **geom, PedConstraint *constraint, 
+                 PedPartition *part)
+{
+	PedGeometry *temp = NULL;
+	if (*geom) ped_geometry_destroy(*geom);
+	PedSector start, end;
+	int i = 0;
+
+	if (part->prev && part->prev->type & PED_PARTITION_FREESPACE)
+		start = part->prev->geom.start;
+	else
+		start = part->geom.start;
+	if (part->next && part->next->type & PED_PARTITION_FREESPACE)
+		end = part->next->geom.end;
+	else
+		end = part->geom.end;	
+	temp = ped_geometry_new(dev, start, end-start+1LL);
+	*geom = ped_constraint_solve_nearest(constraint,temp);
+	ped_geometry_destroy(temp);
+
+}
+#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
+#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
+
+/* NOTE: If you create a rather big test disk, you can make this use llrand 
+         from functions.c instead. */
+void
+randomize_position(PedSector *start, PedSector *end, PedGeometry *where, int i)
+{
+
+	if (i == 0) {
+		*start = where->start;
+		do {
+			*end = *start+((unsigned int)rand() % where->length)-1;
+			*end = MIN(*end,where->end);
+		} while (are_near(*start,*end));
+		
+	}
+	else if (i == 3) {
+		*end = where->end;
+		do {
+			*start = *end-((unsigned int)rand() % where->length)+1;
+			*start = MAX(*start,where->start);
+		} while (are_near(*start,*end));
+	}
+	else {
+		PedSector mid = where->start +
+		                ((unsigned int) rand() % where->length)-1;
+		do {
+			*start = where->start+
+				((unsigned int) rand() % (mid - where->start));
+			*end = mid +
+				((unsigned int) rand() % (where->end - mid));
+			*start = MAX(*start,where->start);
+			*end = MIN(*end,where->end);
+		} while (are_near(*start,*end));
+	}
+}
+
+
+/* This is for our size changing checks. We check on all label types */
+void
+change_size (const char *ltype, UIOpts opts) {
+	PedConstraint *constraint = ped_device_get_constraint(dev);
+	PedGeometry *geom = NULL;
+	PedSector start, end, oldstart = -1, oldend = -1;
+	PedPartition *part = NULL;
+	PedFileSystemType *fs_type = ped_file_system_type_get("fat32");
+	int i;
+	fail_unless(do_mklabel(dev,&disk,ped_disk_type_get(ltype),&uiquery),
+	            "We could create the %s label for resizing partitions",
+	            ltype);
+	/* NOTE: This might blow up if ltype is less than three chars */
+	/* FIXME: This checks works differently on LE and BE platforms */
+	srand(opts + ((int) ltype));
+	for (i = 0; i < 4; i++) {		
+		next_part_geometry(&geom, constraint, 1);
+		string[0][BUFSIZE] = 0;
+		strncpy(string[0],"Label",BUFSIZE);
+		uiquery.getstring = getstring;
+		set_exception_error("Creating partition %d at %llds-%llds "
+			"with previous one at %llds-%llds on %s",
+			i, geom->start, geom->end, oldstart, oldend, ltype);
+		fail_unless(_mkpart (0, geom->start,  geom->end,
+				PED_PARTITION_NORMAL,
+ 				fs_type, &part, (opts & UI_NO_FS_RESIZE ?
+				                 do_mkpart : do_mkpartfs)),
+			"Failed to create partition for resize on %s. "
+			"Number %d, size %llds, start %llds, end %llds",
+			ltype, i, geom->length, geom->start, geom->end);
+		reset_exception_error();
+		oldstart = geom->start;
+		oldend = geom->end;
+	}
+	while (part) {
+		get_max_geometry(&geom,constraint,part);
+		for (i = 0; i < 4; i++) {
+			randomize_position(&start,&end,geom,i);
+			set_exception_error("Resize %d of partition %d "
+			            "from %llds-%llds to %llds-%llds "
+			            "in region %llds-%llds on %s",
+		                    i, part->num, part->geom.start,
+			            part->geom.end, start, end, geom->start,
+			            geom->end, ltype);
+			fail_unless(do_resize (disk, part, start, end,
+			                       opts, &uiquery),
+			            "Could not do %d resize of partition %d "
+			            "from %llds-%llds to %llds-%llds "
+			            "in region %llds-%llds on %s",
+			            i, part->num, part->geom.start,
+			            part->geom.end, start, end, geom->start,
+			            geom->end, ltype);
+			fail_unless(are_near(part->geom.start,start) &&
+			           are_near(part->geom.end,end),
+			           "The %d resize of partition %d on %s wrong."
+			           "we wanted %llds-%llds, we got %llds-%llds",
+			           i, part->num, ltype, start, end,
+			           part->geom.start, part->geom.end);
+			reset_exception_error();
+		}
+		do {
+			part = part->prev;
+		} while (part && ((part->type & PED_PARTITION_METADATA) ||
+		         (part->type & PED_PARTITION_FREESPACE)));
+	}
+}

tests/functions.h

 #define FUNCTION_H_INCLUDED
 
 #include <parted/parted.h>
+#include "../src/common.h"
 
 #define DEFAULT_STRING "RESERVED"
 #define BUFSIZE 4095
 
+/* We have our device here */
+PedDevice *dev;
+PedDisk *disk;
+UICalls uiquery;
+
 /* We will use common exception handling for tests */
 extern PedExceptionOption ex_opts;
 extern PedExceptionType ex_type;
 extern int init_tempfile();
 extern void unlink_tempfile();
 
-extern PedDevice *open_device();
+extern int open_device();
 
 /* Other functions */
 extern int are_near(long long a, long long b);
-extern long long llrand();
+/*extern long long llrand();*/
+
+/* Common check things for both test programs */
+extern void next_part_geometry(PedGeometry **, PedConstraint *, int factor);
+/* This one takes the partition making function as a parameter   *
+ * We use the fact that mkpart and mkpartfs take the same params */
+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*,
+                                  PedPartition **, UIOpts, UICalls *));
+
+extern void get_max_geometry(PedGeometry **geom, PedConstraint *constraint, 
+                      PedPartition *part);
+extern void
+randomize_position(PedSector *start, PedSector *end, PedGeometry *where, int i);
+extern void change_size (const char *label_type, UIOpts opts);
+
+
 #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.