Anonymous avatar Anonymous committed ff17591

Comments (0)

Files changed (8)

 
 #define TINYBUF 16	/* NOTE: Changing this may lead to buffer overflows. Double-check. */
 #define FSBUF 19	/* Don't touch unless you know what you are doing!! */
-#define SMALLBUF 128
+#define SMALLBUF 512
 
 
 /* struct for a menu item. I got the idea from Linux cfdisk. Every action is bound to a key. */
 	const char* temp;
 	char buf[SMALLBUF],fsbuf[FSBUF];
 
-	int col_label = ((float)53/(80-col_fs))*(COLS-col_fs);
-	int col_size = ((float)66/(80-col_fs))*(COLS-col_fs);
+	int col_label = ((double)(53-col_fs)/(80-col_fs))*(COLS-col_fs)+col_fs;
+	int col_size = ((double)(66-col_fs)/(80-col_fs))*(COLS-col_fs)+col_fs;
 	int n,i,x,y = INFOSIZE;
 
 	int can_primary = can_create_primary(disk);
  * "start".  Otherwise, the constraint does not set any requirements on the
  * start.
  */
-static PedConstraint*
+PedConstraint*
 constraint_from_start_end (PedDevice* dev, PedGeometry* range_start,
                            PedGeometry* range_end)
 {
                 range_start, range_end, 1, dev->length);
 }
 
-static PedConstraint*
+PedConstraint*
 constraint_intersect_and_destroy (PedConstraint* a, PedConstraint* b)
 {
         PedConstraint* result = ped_constraint_intersect (a, b);
 	}
 
 	/* We check whether we can create this filesystem here */
-	fs_constraint = ped_file_system_get_create_constraint(fs_type,disk->dev);
+	if (fs_type)
+		fs_constraint = ped_file_system_get_create_constraint(fs_type,disk->dev);
+	else
+		fs_constraint = NULL;
 	if (fs_constraint) {
 		if (!ped_constraint_is_solution(fs_constraint,&(part->geom))) {
 			const char *error = NULL;
 struct _UICalls {
 	/* The first parameter specifies the prompt text */
 	/* All functions should return 1 on success, 0 otherwise */
-	/* getstring: 	The second parameter is the string that is read.                 *
-	 * The two StrLists specify thevalid strings, the second contains the localized. *
-	 * The integer specified how to treat multiwords, TODO: explain                  */
-	int (*getstring)(const char*, char**, const StrList*, const StrList*, int);
-	int (*getint)(const char*, int*);
-	int (*getbool)(const char*, int*);
-	int (*getdev)(const char*, PedDevice**);
-	/* getpart, if the disk is NULL, it should be specified. This won't actually be used */
-	int (*getpart)(const char*, PedDisk**, PedPartition**);
-	int (*getdisktype)(const char*, PedDiskType**);
-	int (*getfstype)(const char*, PedFileSystemType**);
+	/* getstring: 	The second parameter is the string that is read. *
+	 * There are two valid string lists, the second is localized     *
+	 * The integer specified how to treat multiwords, TODO: explain  */
+	int (*getstring)(const char* prompt, char** value, const StrList*,
+	                 const StrList* loc, int multi_word);
+	int (*getint)(const char* prompt, int* value);
+	int (*getbool)(const char* prompt, int* value);
+	int (*getdev)(const char* prompt, PedDevice** value);
+	/* getpart, if the disk is NULL, it should be specified.
+	            This won't actually be used */
+	int (*getpart)(const char* prompt, PedDisk**, PedPartition** value);
+	int (*getdisktype)(const char* prompt, PedDiskType** value);
+	int (*getfstype)(const char* prompt, PedFileSystemType** value);
 	PedTimer* timer;
 	int need_commit;
 };
 typedef enum _UIOpts UIOpts;
 enum _UIOpts {
 	UI_DEFAULT = 0,
-	UI_WARN_COMMIT = 1,	/* Warn the user if the operation commits the data to the disk */
-	UI_CUSTOM_VALUES = 2, 	/* Allow the user to adjust start/end on resize, etc */
-	UI_SPECIFY_PART_TYPE = 4, /* Allow the user to specify the partition type */
-	UI_FUNCTION_SPECIFIC_1 = 8 /* Option specific for each function */
+	/* Warn the user if the operation commits the data to the disk */
+	UI_WARN_COMMIT = 1,
+	/* Allow the user to adjust start/end on resize, etc */
+	UI_CUSTOM_VALUES = 2,
+	/* Allow the user to specify the partition type */
+	UI_SPECIFY_PART_TYPE = 4,
+	/* Option specific for each function */
+	UI_FUNCTION_SPECIFIC_1 = 8
 };
 
 /* Specific for do_resize: */
-#define UI_NO_FS_RESIZE UI_FUNCTION_SPECIFIC_1	/* Will only change geometry, will not resize */
+#define UI_NO_FS_RESIZE UI_FUNCTION_SPECIFIC_1	/* Will only change geometry */
 /* Specific for do_set: */
-#define UI_FLAG_TOGGLE UI_FUNCTION_SPECIFIC_1	/* Make do_set to toggle the flags */
+#define UI_FLAG_TOGGLE UI_FUNCTION_SPECIFIC_1	/* do_set must toggle flags */
 
 
 /* Lists. Should be initialized. */
 extern int do_check (PedDisk*, PedPartition*, UICalls *);
 extern int do_cp (PedDisk*, PedPartition*, UIOpts, UICalls *);
 extern int do_mklabel (PedDevice*, PedDisk**, const PedDiskType*, UICalls *);
-extern int do_mkfs (PedDisk*, PedPartition*, const PedFileSystemType*, UIOpts, UICalls *);
-extern int do_mkpart (PedDisk*, PedSector start, PedSector end, PedPartitionType,
-	const PedFileSystemType*, PedPartition **newpart, UIOpts, UICalls *);
-extern int do_mkpartfs (PedDisk*, PedSector start, PedSector end, PedPartitionType,
-	const PedFileSystemType*, PedPartition **newpart, UIOpts, UICalls *);
-extern int do_move (PedDisk*, PedPartition*, PedSector start, PedSector end, UIOpts, UICalls*);
+
+extern int do_mkfs (PedDisk*, PedPartition*, const PedFileSystemType*, UIOpts,
+                    UICalls *);
+
+extern int do_mkpart (PedDisk*, PedSector start, PedSector end,
+                      PedPartitionType,	const PedFileSystemType*,
+                      PedPartition **newpart, UIOpts, UICalls *);
+
+extern int do_mkpartfs (PedDisk*, PedSector start, PedSector end,
+                        PedPartitionType, const PedFileSystemType*,
+                        PedPartition **newpart, UIOpts, UICalls *);
+
+extern int do_move (PedDisk*, PedPartition*, PedSector start, PedSector end,
+                    UIOpts, UICalls*);
 extern int do_name (PedDisk*, PedPartition*, char*, UICalls*);
-extern int do_rescue (PedDisk*, PedSector start, PedSector end, UIOpts, UICalls *);
-extern int do_resize (PedDisk*, PedPartition*, PedSector start, PedSector end, UIOpts, UICalls*);
+extern int do_rescue (PedDisk*, PedSector start, PedSector end, UIOpts,
+                      UICalls *);
+extern int do_resize (PedDisk*, PedPartition*, PedSector start, PedSector end,
+                      UIOpts, UICalls*);
 extern int do_rm (PedDisk*, PedPartition*, UICalls*);
-extern int do_set_system (PedDisk*, PedPartition*, const PedFileSystemType*, UICalls*);
-extern int do_set (PedDisk*, PedPartition*, PedPartitionFlag, UIOpts, UICalls*);
+extern int do_set_system (PedDisk*, PedPartition*, const PedFileSystemType*,
+                          UICalls*);
+extern int do_set (PedDisk*, PedPartition*, PedPartitionFlag, UIOpts,UICalls*);
 extern int do_commit (PedDisk*, UIOpts, UICalls*);
 extern int do_maximize (PedDisk*, PedPartition*, UICalls*);
 
 /* Additional functions */
 extern char* partition_print_flags (PedPartition*);
 
+extern PedConstraint*
+constraint_from_start_end (PedDevice*, PedGeometry* range_start,
+				PedGeometry* range_end);
+extern PedConstraint*
+constraint_intersect_and_destroy (PedConstraint* a, PedConstraint* b);
+
 #endif

tests/Makefile.am

 
 check_common_SOURCES = \
 	check_common.c \
+	functions.c \
+	functions.h \
 	../src/common.h \
 	../src/common.c \
 	../src/strlist.h \

tests/check_advanced.c

+/*
+    GNU fdisk - a clone of Linux fdisk.
+
+    Copyright (C) 2006
+    Free Software Foundation, Inc.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+*/
+
+#include <stdlib.h>
+#include <check.h>
+#include <stdio.h>
+#include "../src/strlist.h"
+#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;
+
+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
+
+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);
+	return s;
+}
+
+
+int main (void)
+{
+	int nf;
+	if (!init_tempfile()) return 1;
+	dev = open_device();
+	if (!dev) {
+		unlink_tempfile;
+		return 1;
+	}
+	if (!do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery))
+		return EXIT_FAILURE;
+	ped_exception_set_handler(exception_handler);
+	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

+/*
+    GNU fdisk - a clone of Linux fdisk.
+
+    Copyright (C) 2006
+    Free Software Foundation, Inc.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+*/
+
 #include <stdlib.h>
 #include <check.h>
 #include <stdio.h>
+#include <parted/parted.h>
 #include "../src/strlist.h"
 #include "../src/common.h"
+#include "functions.h"
 
-#define SIZE 2000000000LL
-#define BUFSIZE 4096
 
 /* No chance someone else decides to call this file this way, I hope */
-char tempfile[] = "/tmp/check_cfdisk_temporary_disk_drive";
 
 PedDevice *dev = NULL;
 PedDisk *disk = NULL;
 UICalls uiquery;
 
 
-/* This functions will return what's needed */
-char string[BUFSIZE];
-int getstring (const char* prompt, char** value, const StrList* str_list, 
-               const StrList* loc_str_list, int multi_word)
-{
-	if (str_list || loc_str_list) {
-		int found = 0;
-		char *temp = NULL;
-		const StrList *c;
-		/* Check if the test string is not found in the string list */
-		for (c = str_list; !found && c != NULL; c = c->next) {
-			temp = str_list_convert_node(c);
-			if (!strcmp(temp,string)) found = 1;
-			if (temp) free(temp);
-		}
-		for (c = loc_str_list; !found && c != NULL; c = c->next) {
-			temp = str_list_convert_node(c);
-			if (!strcmp(temp,string)) found = 1;
-			if (temp) free(temp);
-		}
-		if (!found) return 0;
-	}
-	if (*value) ped_free (*value);
-	*value = strdup(string);
-	return 1;
-}
-
-int integer = 0;
-int getint (const char* prompt, int *value)
-{
-	*value = integer;
-	return 1;
-}
-
-int bool = 1;
-int getbool (const char* prompt, int *value)
-{
-	*value = bool;
-	return 1;
-}
-
-PedDevice *device = NULL;
-int getdev (const char* prompt, PedDevice** value)
-{
-	*value = device;
-	return 1;
-}
-
-PedPartition *partition;
-int getpart (const char* prompt, PedPartition** value)
-{
-	*value = partition;
-	return 1;
-}
-
-PedDiskType *disk_type;
-int getdisktype (const char* prompt, PedDiskType **value)
-{
-	*value = disk_type;
-	return 1;
-}
-
-PedFileSystemType *fs_type;
-int getfstype (const char* prompt, PedFileSystemType **value)
-{
-	*value = fs_type;
-	return 1;
-}
-
-int
-init_tempfile() {
-	FILE *fp;
-	fp = fopen(tempfile,"w");
-	if (!fp) return 0;
-	fseek (fp, SIZE, SEEK_SET);
-	fwrite ("", 1, 1, fp);
-	fclose (fp);
-	return 1;
-	
-}
-
-int
-init_device() {
-	dev = ped_device_get(tempfile);
-	return ped_device_open(dev);
-}
-
-
-
 START_TEST (test_null)
 {
-	fail_unless(1 == 1, "Your computer is insane");
+	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)
 {
-	strncpy(string,"msdos", BUFSIZE);
-	disk_type = ped_disk_type_get(string);
+
+	strncpy(string[0],"msdos", BUFSIZE);
+	disk_type = ped_disk_type_get(string[0]);
 	/* First we will test with giving it the string */
 	uiquery.getstring = getstring;
 	uiquery.getdisktype = NULL;
 	fail_unless (do_mklabel(dev, &disk, NULL, &uiquery), 
 	             "Failed to create a label with a getstring query");
 	fail_unless (disk != NULL, "mklabel reported success, but failed");
-	fail_unless (!strcmp(string,disk->type->name),
+	fail_unless (!strcmp(string[0],disk->type->name),
 	             "mklabel created wrong disk type");
 	/* Then we do it the other way */
 	uiquery.getstring = NULL;
 	             "Failed to create a label with a getdisktype query");
 	fail_unless (disk != NULL,
 	             "mklabel reported success, but failed, getdisktype");
-	fail_unless (!strcmp(string,disk->type->name),
+	fail_unless (!strcmp(string[0],disk->type->name),
 	             "mklabel created wrong disk type, using getdisktype");
 	if (disk) ped_disk_destroy(disk);
 	disk = NULL;
 	             "Failed to create a label with a specified type");
 	fail_unless (disk != NULL,
 	             "mklabel reported success, but failed, specified");
-	fail_unless (!strcmp(string,disk->type->name),
+	fail_unless (!strcmp(string[0],disk->type->name),
 	             "mklabel created wrong disk type when we specified it");
 	
 }
 END_TEST
 
+#define PART_LENGTH (2097151LL - ((unsigned int) rand() % 1048575))
+
+static void
+next_part_geometry(PedGeometry **geom, PedConstraint *constraint) {
+	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);
+	*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,"%llus",start);
+			snprintf(string[1],BUFSIZE,"%llus",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);
+	PedFileSystemType* walk = NULL;
+	PedGeometry *geom = NULL;
+	PedPartition *part = NULL;
+	int i;
+	/* First we create an extended partition */
+	fail_unless(do_mkpart(disk,0LL,20000LL,PED_PARTITION_EXTENDED,NULL,NULL,
+	                      UI_DEFAULT,&uiquery),
+	            "Failed to create an extended partition");
+
+	/* Check if it is there */
+	do {
+		part = ped_disk_next_partition (disk, part);
+	} while (part && !(part->type & PED_PARTITION_EXTENDED));
+	fail_unless(part != NULL, "Can't find the extended partition");
+
+	/* Then we maximize it */
+	fail_unless(do_maximize(disk,part,&uiquery), "Could not maximize the extended");
+
+	/* We check if our partition is "near" to what they should */
+	fail_unless(are_near(constraint->start_range->start,part->geom.start)
+	            && are_near(constraint->end_range->end,part->geom.end),
+	            "Maximize didn't work correctly.\n"
+	            "We wanted %llu-%llu, we got %llu-%llu",
+	            constraint->start_range->start, constraint->end_range->end,
+	            part->geom.start, part->geom.end);
+	constraint = constraint_intersect_and_destroy(constraint,
+	           constraint_from_start_end(dev,&(part->geom),&(part->geom)));
+
+	/* We will give the partitions below different sizes. I'll use rand */
+	srand(218872305);
+	walk = ped_file_system_type_get_next (NULL);
+	/* We first try to create 20 partitions */
+	for (i = 0; i < 20; i++) {
+		next_part_geometry(&geom, constraint);
+
+		fail_unless(_mkpart (i % 3, geom->start,  geom->end,
+				PED_PARTITION_LOGICAL, walk, &part),
+			"Failed to create %s partition. "
+			"Number %d, size %llus, start %llus, end %llus",
+			walk->name, i, geom->length, geom->start, geom->end);
+
+		fail_unless(are_near(geom->start,part->geom.start) &&
+			are_near(geom->end,part->geom.end),
+			"Failed to create %s partition where we want. "
+			"Number %d, we wanted %llu-%llu, we got %llu-%llu",
+			walk->name, i, geom->start, geom->end,
+			part->geom.start, part->geom.end);
+		ped_geometry_set (geom,part->geom.start,part->geom.length);
+		do {
+			walk = ped_file_system_type_get_next (walk);
+		} while (walk == NULL);
+		
+	}
+	/* Now, this should delete all the partitions */	
+	while (part) {
+		PedPartition *temp = part;
+		do {
+			part = part->prev;
+		} while (part && part->type & PED_PARTITION_METADATA);
+		if (part && (part->type & PED_PARTITION_FREESPACE))
+			fail("We reached a hole at %llus-%llus, number %d",
+			     part->geom.start, part->geom.end, i);
+		fail_unless (do_rm(disk,temp,&uiquery),
+		             "Failed to delete partition after %d",
+		             (part ? part->num : -1));
+		i--;
+	}
+	fail_unless(i == 0, "Not all partitions were deleted. %d remain.", i);
+	ped_geometry_destroy(geom);
+	
+}
+END_TEST
+
+
+/* Here we test (*only*) the size changing with do_resize */
+/*START_TEST (test_resize_size) {
+
+}*/
+
 Suite *common_suite(void)
 {
 	Suite *s = suite_create("Common functions");
 	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);
 	return s;
 }
 
 {
 	int nf;
 	if (!init_tempfile()) return 1;
-	if (!init_device()) {
-		unlink(tempfile);
+	dev = open_device();
+	if (!dev) {
+		unlink_tempfile;
 		return 1;
 	}
+	if (!do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery))
+		return EXIT_FAILURE;
+	ped_exception_set_handler(exception_handler);
+	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/functions.c

+/*
+    GNU fdisk - a clone of Linux fdisk.
+
+    Copyright (C) 2006
+    Free Software Foundation, Inc.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <parted/parted.h>
+#include "../src/strlist.h"
+#include "functions.h"
+
+/* We can't seek more... */
+#define SIZE 2147483647L
+
+
+/* No chance someone else decides to call this file this way, I hope */
+static char tempfile[] = "/tmp/check_cfdisk_temporary_disk_drive";
+
+PedExceptionOption ex_opts;
+PedExceptionType ex_type;
+
+/* We need an exception handler */
+PedExceptionOption
+exception_handler (PedException* ex) {
+	ex_opts = ex->options;
+	ex_type = ex->type;
+	if (ex_opts & PED_EXCEPTION_FIX)
+		return PED_EXCEPTION_FIX;
+	else if (ex_opts & PED_EXCEPTION_RETRY)
+		return PED_EXCEPTION_RETRY;
+	else if (ex_opts & PED_EXCEPTION_YES)
+		return PED_EXCEPTION_YES;
+	else if (ex_opts & PED_EXCEPTION_OK)
+		return PED_EXCEPTION_OK;
+	else if (ex_opts & PED_EXCEPTION_IGNORE)
+		return PED_EXCEPTION_IGNORE;
+	else
+		return PED_EXCEPTION_UNHANDLED;
+	
+}
+
+/* This functions will return what's needed */
+/* NOTE: If the string is DEFAULT_STRING, we return the default string */
+/* Also note that we have two strings here, if the last character is 1, we
+   should use the second string, if it is 2, we should change it to 1, and
+   if 0, we should use the first. I know it's ugly, but it works. */
+char string[2][BUFSIZE+1];
+int getstring (const char* prompt, char** value, const StrList* str_list, 
+               const StrList* loc_str_list, int multi_word)
+{
+	char *s;
+	if (string[0][BUFSIZE] == 1) {
+		s = string[1];
+		string[0][BUFSIZE] = 0;
+	}
+	else
+		s = string[0];
+	if (string[0][BUFSIZE] == 2)
+		string[0][BUFSIZE] = 1;
+	
+	if (!*value || strcmp(s, DEFAULT_STRING)) {
+		if (str_list || loc_str_list) {
+			int found = 0;
+			char *temp = NULL;
+			const StrList *c;
+			/* Check if the test string is not found in the string list */
+			for (c = str_list; !found && c != NULL; c = c->next) {
+				temp = str_list_convert_node(c);
+				if (!strcmp(temp,s)) found = 1;
+				if (temp) free(temp);
+			}
+			for (c = loc_str_list; !found && c != NULL; c = c->next) {
+				temp = str_list_convert_node(c);
+				if (!strcmp(temp,s)) found = 1;
+				if (temp) free(temp);
+			}
+			if (!found) return 0;
+		}
+		if (*value) ped_free (*value);
+		*value = strdup(s);
+	}
+	return 1;
+}
+
+int integer = 0;
+int getint (const char* prompt, int *value)
+{
+	*value = integer;
+	return 1;
+}
+
+int bool = 1;
+int getbool (const char* prompt, int *value)
+{
+	*value = bool;
+	return 1;
+}
+
+PedDevice *device = NULL;
+int getdev (const char* prompt, PedDevice** value)
+{
+	*value = device;
+	return 1;
+}
+
+PedPartition *partition;
+int getpart (const char* prompt, PedPartition** value)
+{
+	*value = partition;
+	return 1;
+}
+
+PedDiskType *disk_type;
+int getdisktype (const char* prompt, PedDiskType **value)
+{
+	*value = disk_type;
+	return 1;
+}
+
+PedFileSystemType *fs_type;
+int getfstype (const char* prompt, PedFileSystemType **value)
+{
+	*value = fs_type;
+	return 1;
+}
+
+int
+init_tempfile() {
+	int i;
+	FILE *fp;
+	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 */
+	for (i = 0; i < 9; i++) {
+		fseek (fp, SIZE, SEEK_CUR);
+	}
+	fwrite ("", 1, 1, fp);
+	fclose (fp);
+	return 1;
+	
+}
+
+void
+unlink_tempfile() {
+	unlink(tempfile);
+}
+
+PedDevice *
+open_device() {
+	PedDevice *dev;
+	dev = ped_device_get(tempfile);
+	if (!ped_device_open(dev))
+		return NULL;
+	return dev;
+}
+
+
+#define FAR 128LL
+/* If these are too far apart, return false */
+int
+are_near(long long a, long long b) {
+	a -= b;
+	if (a < 0LL) a = -a;
+	return (a < FAR); 
+}

tests/functions.h

+/*
+    GNU fdisk - a clone of Linux fdisk.
+
+    Copyright (C) 2006
+    Free Software Foundation, Inc.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+*/
+
+
+#ifndef FUNCTION_H_INCLUDED
+#define FUNCTION_H_INCLUDED
+
+#include <parted/parted.h>
+
+#define DEFAULT_STRING "RESERVED"
+#define BUFSIZE 4095
+
+/* We will use common exception handling for tests */
+extern PedExceptionOption ex_opts;
+extern PedExceptionType ex_type;
+extern PedExceptionOption exception_handler (PedException*);
+
+/* This are functions for UICalls */
+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 integer;
+extern int getint (const char* prompt, int *value);
+
+extern int bool;
+extern int getbool (const char* prompt, int *value);
+
+extern PedDevice *device;
+extern int getdev (const char* prompt, PedDevice** value);
+
+extern PedPartition *partition;
+extern int getpart (const char* prompt, PedPartition** value);
+
+extern PedDiskType *disk_type;
+extern int getdisktype (const char* prompt, PedDiskType **value);
+
+extern PedFileSystemType *fs_type;
+extern int getfstype (const char* prompt, PedFileSystemType **value);
+
+/* Device init functions, we should make them one */
+extern int init_tempfile();
+extern void unlink_tempfile();
+
+extern PedDevice *open_device();
+
+/* Other functions */
+extern int are_near(long long a, long long b);
+
+#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.