Anonymous avatar Anonymous committed 33ac0a8

Comments (0)

Files changed (5)

 	menu_title(_("This will try to rescue broken or deleted partitions")); 
 	key = do_menu(rescue_menu, 8, MENU_BUTTON | MENU_TITLE, keys, NULL);
 	if (key == ESC) return 0;
-
+#if 0
 	/* Remember either the previous partition so we can select the free space */
 	temp = disk_get_prev_nmd_partition(disk, *part);
 	if (temp && temp->type & PED_PARTITION_FREESPACE)
 		temp = disk_get_prev_nmd_partition(disk, temp); 
-
+#endif
 	if(!do_rescue (disk, (*part)->geom.start, (*part)->geom.end, 
 	           UI_WARN_COMMIT | (key == 'c' ? UI_CUSTOM_VALUES : 0), &uiquery)) {
 		warning_waitkey(N_("There was an error during rescue"));
 		return 0;
 	}
+#if 0
 	do {
 		temp = ped_disk_next_partition(disk,temp);
 	} while (temp && temp->type & PED_PARTITION_METADATA); 
 	*part = temp;
+#endif
+	*part = NULL;
+	do {
+		*part = ped_disk_next_partition(disk,*part);
+	} while (*part && (*part)->type & PED_PARTITION_METADATA);
 	notice_waitkey(N_("Finished looking for partitions"));
 	return 1;
 }

tests/check_advanced.c

 static void
 warn () {
 	printf("We are about to run a very CPU and disk intensive test.\n"
-	       "It can take about 500 MB of free space and continue half an hour.\n"
+	       "It can take about 500 MB of free space.\n"
                "Are you sure you want to continue (y/N)? ");
 	if (getchar() != 'y' && getchar() != 'Y') {
 		exit(0);
 	//fail_unless(1, "hm");
 
 
-	srand(892791);
+	srand(129312);
 	int i;
 	/* We copy all partitions to the new disk */
 	for (i = 0; i < 4; i++) {
 		uiquery.getpart = NULL;
 		uiquery.getint = getint;
 		integer = i+1;
+		set_exception_error("Copying %d to %llds-%llds",
+		                    i, part->geom.start, part->geom.end);
 		fail_unless(do_cp (disk, part, UI_DEFAULT, &uiquery), 
-		            "%d->%d partition copy failed (from %llds-%llds "
-		            "to %llds-%llds)", 0LL, 0LL, part->geom.start,
+		            "%d partition copy failed (from %llds-%llds "
+		            "to %llds-%llds)", i, 0LL, 0LL, part->geom.start,
 		            part->geom.end);
 		fs = ped_file_system_open(&(part->geom));
 		fail_unless(fs != NULL, "No filesystem on partition %d", i);
 	ped_device_destroy(dev2);
 	/* We don't need this anymore. We won't unlink the tempfile, because *
          * we will test the rescue on it.                                    */
+	//close_tempfile();
 	unlink(tempfile2_name());
 }
 END_TEST
 
+
+/* I'm lazy to type too much, so */
+#define CREATE_PART(num,start,end) \
+	fail_unless(_mkpart(0, start, end, PED_PARTITION_NORMAL, \
+	                    fs_type, NULL, do_mkpartfs), \
+	            "Failed to create num partition for rescue")
 /* This does not take any additional disk space */
 START_TEST (test_rescue) {
 	int i;
 	PedPartition *part = NULL;
 	PedGeometry geom;
-
+	PedFileSystemType *fs_type = ped_file_system_type_get("ext2");
+#if 0	
 	dev = ped_device_get(tempfile_name());
 	fail_unless(ped_device_open(dev), 
 		            "Failed to open the device for rescue");
 	disk = ped_disk_new(dev);
+	/* We create the partitions */
+#endif
+	open_tempfile(1);
 
-	/* First we delete the partitions */
+	CREATE_PART(1,0LL,400LL);
+	CREATE_PART(2,401LL,1000LL);
+	CREATE_PART(3,1001LL,1200LL);
+	CREATE_PART(4,1201LL,2000LL);
+
+
+	/* Then we delete the partitions */
 	part = ped_disk_get_partition(disk,
 		ped_disk_get_last_partition_num (disk));
+	while(part->type)
+		part = part->prev;
 	geom = part->geom;
 	while (part) {
 		PedPartition *temp = part;
 			            part->num);
 		}
 	}
+	
 	/* Then we try to undelete the last one */
 	fail_unless(do_rescue(disk, geom.start, geom.end, UI_DEFAULT, &uiquery),
 	            "Failed to rescue a single partition at %llds-%llds",
 	            geom.start, geom.end);
-	part = NULL;
-	do {
-		part = ped_disk_next_partition(disk,NULL);
-	} while (part && part->type);
+	/* part should be the undeleted partition */
+	part = ped_disk_get_partition(disk,
+		ped_disk_get_last_partition_num (disk));
+	while (part && part->type) {
+		part = part->prev;
+	}
+
 	fail_unless(geom.start==part->geom.start && geom.end==part->geom.end,
                     "Rescued partition is not the same as the deleted. "
                     "Deleted was %llds-%llds, we got %llds-%llds.",
 	fail_unless(do_rm(disk, part, &uiquery),
 	            "Rescue could not delete %d partition",
 	            part->num);
+
 	/* The end of the last partition is before 8388604s */
 	fail_unless(do_rescue(disk, 0LL, geom.end, UI_DEFAULT, &uiquery),
 	            "Failed to rescue the partitions");
 	/* Now we count them, if they aren't four, we did something wrong */
 	for (part = ped_disk_next_partition(disk,NULL), i = 0;
-	     part; part = ped_disk_next_partition(disk,part), i++);
+	     part; part = ped_disk_next_partition(disk,part)) {
+		if (!part->type)
+			i++;
+	}
 	fail_unless(i == 4, "We got %d partitions instead of 4", i);
 	close_tempfile();
 }
 	suite_add_tcase (s, tc_resize);
 	suite_add_tcase (s, tc_move);
 	suite_add_tcase (s, tc_copy);
-	/* FIXME: Too slow. Make faster and turn on */
-	//suite_add_tcase (s, tc_rescue);
+	suite_add_tcase (s, tc_rescue);
 	tcase_set_timeout(tc_resize, 360);
 	tcase_set_timeout(tc_move, 360);
 	tcase_set_timeout(tc_copy, 360);
 int main (void)
 {
 	warn();
+	check_safety ();
 	int nf;
 	ped_exception_set_handler(exception_handler);
 	reset_exception_error();

tests/check_common.c

 
 int main (void)
 {
+	check_safety ();
 	int nf;
 	if (!init_tempfile()) return 1;
 	if (!open_device()) {

tests/functions.c

 #include <stdio.h>
 #include <stdarg.h>
 #include <check.h>
+#include <sys/stat.h>
 #include <parted/parted.h>
 /* #include <endian.h> */
 #include "../src/strlist.h"
 {
 	return tempfile2;
 }
+/* We should try not to ruin a file, or more importantly, some block device */
+static int is_blockdev(const char *file) {
+	struct stat file_stat;
+	if (stat(file,&file_stat) < 0)
+		return -1;
+	if(S_ISBLK(file_stat.st_mode))
+		return 1;
+	else
+		return 0;
+}
+
+void check_safety () {
+	int a = is_blockdev(tempfile);
+	int b = is_blockdev(tempfile2);
+	if (a > 0 || b > 0) {
+		printf("ERROR: Found a block device with the name of our temp "
+		       "file. Aborting.");
+		exit(1);
+	}
+	else {
+		const char msg[] = "%s exists and will be overwritten. "
+		                  "Are you sure you want to continue (y/N)?";
+		if (!a) {
+			printf(msg, tempfile);
+			if (getchar() != 'y' && getchar() != 'Y') {
+				exit(0);
+			}
+		}
+		if (!b) {
+			printf(msg, tempfile2);
+			if (getchar() != 'y' && getchar() != 'Y') {
+				exit(0);
+			}
+		}
+	}
+	if (!getuid() || !geteuid()) {
+		printf("You should not run these tests as root.\n"
+		       "Are you sure you want to continue (y/N)? ");
+		if (getchar() != 'y' && getchar() != 'Y') {
+			exit(0);
+		}
+	}
+}
+
+
 
 
 /* We need an exception handler */
 	            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));
+	srand(1 + opts + ((int) ltype));
 	/* We will resize create 4 partitions */
 	for (i = 0; i < 4; i++) {		
 		next_part_geometry(&geom, constraint, 1);

tests/functions.h

 extern const char* tempfile_name();
 extern const char* tempfile2_name();
 
+
 /* We have our device here */
 extern PedDevice *dev;
 extern PedDisk *disk;
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.