Anonymous avatar Anonymous committed 9097a2d

Create some new checks and fix a few bugs (fdisk@sv.gnu.org/fdisk--main--0--patch-27)
fdisk@sv.gnu.org/fdisk--main--0--patch-27
Keywords:

Comments (0)

Files changed (5)

         /* get new target */
         if (!get_sector (_("Start"), disk->dev, &start, &range_start, opts & UI_CUSTOM_VALUES, uiquery))
                 goto error_close_fs;
-        end = start + old_geom.length - 1;
+        if (opts & UI_CUSTOM_VALUES)
+		end = start + old_geom.length - 1;
         if (!get_sector (_("End"), disk->dev, &end, &range_end, opts & UI_CUSTOM_VALUES, uiquery))
                 goto error_close_fs;
 
 
         if (!disk)
                 goto error;
+
+	if (!part) 
+        	if (!get_partition (_("Partition"), disk, &part, uiquery))
+                	goto error;
+
 	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)) 
 			if (!do_commit(disk,UI_DEFAULT,uiquery))
 				goto error;
 	}
-	if (!part) 
-        	if (!get_partition (_("Partition"), disk, &part, uiquery))
-                	goto error;
+
         if (part->type != PED_PARTITION_EXTENDED) {
                 if (!_partition_warn_busy (part))
                         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)) {
+	/* FIXME: Decide when exactly we need to "fuzzify" */
+	//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/check_advanced.c

 END_TEST
 #endif
 
+/* We need to create a new tempfile for each test, so we can save disk space */
+static void
+open_tempfile(int mklabel) {
+	if (!init_tempfile()) fail("Could not init tempfile");
+	if (!open_device()) {
+		unlink_tempfile;
+		fail("Could not open the tempfile");
+	}
+	if (mklabel && !do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery))
+		fail("Could not create a label on the tempfile");
+	
+}
+static void
+close_tempfile() {
+	unlink_tempfile();
+}
+
 /* This takes ~160MB of disk space. */
 START_TEST (test_resize) {
+	open_tempfile(0);
 	change_size("msdos", UI_DEFAULT);
+	close_tempfile();
 }
 END_TEST
 
+/* This takes ~200MB of disk space */
+#define CAN_USE(x) ((x) && ((x)->type & PED_PARTITION_FREESPACE))
+START_TEST (test_move) {
+	int i;
+	PedGeometry where;
+	PedPartition *part;
+	PedFileSystemType *fs_type = ped_file_system_type_get("fat32");
+	open_tempfile(1);
+	//PedSector maxlen = dev->length/4LL;
+	PedSector start = 0LL;
+	PedSector end = dev->length/4LL - 1;
+	PedSector oldstart,oldend;
+	fail_unless(_mkpart (0, start,  end,
+				PED_PARTITION_NORMAL,
+ 				fs_type, &part, do_mkpartfs),
+	            "Failed to create the partition");
+	/* We seed and move 16 times */
+	srand(173813981);
+	for (i = 0; i < 16; i++) {
+		/* We move the partition to the larger free space before
+		   or after it */
+		if (CAN_USE(part->next)) {
+			if (CAN_USE(part->prev) &&
+			    part->prev->geom.length > part->next->geom.length)
+				where = part->prev->geom;
+			else
+				where = part->next->geom;
+		} else
+			where = part->prev->geom;
+		oldstart = start;
+		oldend = end;
+		randomize_position(&start, &end, &where, 1);
+		set_exception_error("Trying to move %d from %llds-%llds "
+		                    "to %llds-%llds in %llds-%llds",
+		                     i, oldstart, oldend, start, end,
+		                     where.start, where.end);
+		fail_unless(_resmov(i, part, start, end, UI_DEFAULT, do_move),
+		            "Failed to move %d partition "
+		            "from %llds-%llds to %llds-%llds in %llds-%llds",
+		            i, oldstart, oldend, start, end,
+		            where.start, where.end);
+		fail_unless(are_near(start,part->geom.start) && 
+		            are_near(end,part->geom.end),
+		            "Partition %d was moved from %llds-%llds to "
+		            "%llds-%llds instead of %llds-%llds in "
+		            "%llds-%llds", i,
+	                    oldstart, oldend, part->geom.start, part->geom.end, 
+		            start, end, where.start, where.end);
+	}
+	close_tempfile();
+}
+END_TEST
 
 
 Suite *common_suite(void)
 {
 	Suite *s = suite_create("Advanced checks");
 	TCase *tc_resize = tcase_create("Resize");
+	TCase *tc_move = tcase_create("move");
 	suite_add_tcase (s, tc_resize);
+	suite_add_tcase (s, tc_move);
 	tcase_set_timeout(tc_resize, 360);
+	tcase_set_timeout(tc_move, 360);
 	tcase_add_test(tc_resize, test_resize);
+	tcase_add_test(tc_move, test_move);
 	return s;
 }
 
 int main (void)
 {
 	int nf;
-	if (!init_tempfile()) return 1;
-	if (!open_device()) {
-		unlink_tempfile;
-		return 1;
-	}
-	if (!do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery))
-		return EXIT_FAILURE;
 	ped_exception_set_handler(exception_handler);
+	reset_exception_error();
 	string[0][BUFSIZE] = 0;
 	Suite *s = common_suite();
 	SRunner *sr = srunner_create(s);
 	srunner_run_all (sr, CK_NORMAL);
 	nf = srunner_ntests_failed(sr);
 	srunner_free(sr);
-	unlink_tempfile;
 	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 }

tests/check_common.c

 		return 1;
 	}
 	ped_exception_set_handler(exception_handler);
+	reset_exception_error();
 	string[0][BUFSIZE] = 0;
 	Suite *s = common_suite();
 	SRunner *sr = srunner_create(s);
 	srunner_run_all (sr, CK_NORMAL);
 	nf = srunner_ntests_failed(sr);
 	srunner_free(sr);
-	unlink_tempfile;
+	unlink_tempfile();
 
 	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 }

tests/functions.c

 
 /* We can't seek more... */
 #define SIZE 2147483647L
+/* We will give the partitions below different sizes. I'll use rand */
+#define PART_LENGTH (2097151LL - ((unsigned int) rand() % 1048575))
 
 PedDevice *dev = NULL;
 PedDisk *disk = NULL;
 }
 
 PedPartition *partition;
-int getpart (const char* prompt, PedPartition** value)
+int getpart (const char* prompt, PedDisk **disk, PedPartition** value)
 {
 	*value = partition;
 	return 1;
 	fp = fopen(tempfile,"w");
 	if (!fp) return 0;
 	fseek (fp, SIZE, SEEK_SET);
-	/* NOTE: We want a ~11 GB file, this takes about 11 MB from the disk, this should be 41943039s */
+	/* NOTE: We want a ~21 GB file, this takes about 22 MB from the disk, this should be 41943039s */
 	for (i = 0; i < 9; i++) {
 		fseek (fp, SIZE, SEEK_CUR);
 	}
 
 
 
-/* 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,
                                      PedPartitionType, const PedFileSystemType*,
                                      PedPartition **, UIOpts, UICalls *))
 {
-	char *temp;
 	switch(how % 3) {
 		/* We create the partition by giving start and end */
 		case 0:
 		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:
-			
+			uiquery.getstring = getstring;
 			string[0][BUFSIZE] = 2;
 			snprintf(string[0],BUFSIZE,"%llds",start);
 			snprintf(string[1],BUFSIZE,"%llds",end);
 	
 }
 
+/* We use this for both resize and move, as they take the same params */
+int
+_resmov (int how, PedPartition *part, PedSector start, PedSector end,
+         UIOpts opts, int action (PedDisk*, PedPartition*,
+                                  PedSector start, PedSector end,
+                                  UIOpts, UICalls*))
+{
+	switch (how % 3) {
+		/* Specify start, end and partition */
+		case 0:
+			return action (disk, part, start, end, opts,
+			                  &uiquery);
+		/* 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,
+			                  &uiquery);
+		/* We ask for everything, the partition with number */
+		case 2:
+			uiquery.getpart = NULL;
+			uiquery.getstring = getstring;
+			uiquery.getint = getint;
+			integer = part->num;
+			string[0][BUFSIZE] = 2;
+			snprintf(string[0],BUFSIZE,"%llds",start);
+			snprintf(string[1],BUFSIZE,"%llds",end);
+			opts |= UI_CUSTOM_VALUES;
+			return action (disk, NULL, 0LL, 0LL, opts,
+			                  &uiquery);
+	}
+}
 
 void
 get_max_geometry(PedGeometry **geom, PedConstraint *constraint, 
 /* 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;
 	/* 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));
+	/* We will resize create 4 partitions */
 	for (i = 0; i < 4; i++) {		
 		next_part_geometry(&geom, constraint, 1);
 		string[0][BUFSIZE] = 0;
 		oldstart = geom->start;
 		oldend = geom->end;
 	}
+	/* And we will resize them, 4 times each, moving them as near possible
+	   to end of the disk as a final result */
 	while (part) {
 		get_max_geometry(&geom,constraint,part);
 		for (i = 0; i < 4; i++) {
 		                    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),
+			/* Note that exact values fail on some partition 
+			   types, and when we are querying the user,
+			   exact values should be used... Or? FIXME? */
+			//fail_unless(_resmov (strncmp("ms",ltype,2) ? 0 : i, 
+			fail_unless(_resmov (i,
+			                     part, start, end, opts, do_resize),
 			            "Could not do %d resize of partition %d "
 			            "from %llds-%llds to %llds-%llds "
 			            "in region %llds-%llds on %s",

tests/functions.h

 #include "../src/common.h"
 
 #define DEFAULT_STRING "RESERVED"
-#define BUFSIZE 4095
+#define BUFSIZE 4093
 
 /* We have our device here */
 PedDevice *dev;
 extern PedExceptionOption exception_handler (PedException*);
 
 /* This are functions for UICalls */
+/* NOTE: This macros are only for reference */
+#define PUT_STRING(x) string[0][BUFSIZE] = 0; \
+                      strncpy(string[0], x, BUFSIZE-1)
+#define PUT_TWO_STRINGS(x,y) string[0][BUFSIZE] = 2; \
+                             strncpy(string[0], x, BUFSIZE-1); \
+                             strncpy(string[1], x, BUFSIZE-1)
+
+			
 extern char string[2][BUFSIZE+1];
 extern int getstring (const char* prompt, char** val, const StrList* str_list,
                       const StrList* loc_str_list, int multi_word);
 extern int getdev (const char* prompt, PedDevice** value);
 
 extern PedPartition *partition;
-extern int getpart (const char* prompt, PedPartition** value);
+extern int getpart (const char* prompt, PedDisk**disk, PedPartition** value);
 
 extern PedDiskType *disk_type;
 extern int getdisktype (const char* prompt, PedDiskType **value);
                       int mkpart (PedDisk*, PedSector start, PedSector end,
                                   PedPartitionType, const PedFileSystemType*,
                                   PedPartition **, UIOpts, UICalls *));
+/* 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, UICalls*));
 
 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.