Skip to contentSkip to conversation

fdisk: remove unused 'require' member in enum action

PATCH Applied Francesco Cosoleto 3 participants 18 messages Dec 15, 2011 – Jan 4, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/13 fdisk: remove unused 'require' member in enum action

---
 fdisk/fdisk.c |    2 --
 fdisk/fdisk.h |    2 +-
 2 files changed, 1 insertions(+), 3 deletions(-)
fdisk/fdisk.c +0 -2
index 00dd706..a6de6e2 100644
@@ -1179,8 +1179,6 @@ got_dos_table:
 			create_doslabel();
 #endif
 			return 0;
-		case require:
-			return -1;
 		case try_only:
 		        return -1;
 		case create_empty_dos:
fdisk/fdisk.h +1 -1
index 81ff5a2..67642cd 100644
@@ -54,7 +54,7 @@ enum failure {ioctl_error,
 	unable_to_open, unable_to_read, unable_to_seek,
 	unable_to_write};
 
-enum action {fdisk, require, try_only, create_empty_dos, create_empty_sun};
+enum action {fdisk, try_only, create_empty_dos, create_empty_sun};
 
 struct geom {
 	unsigned int heads;
Signature · 1 line
1.7.7
#1 lore raw

PATCH 2/13 fdisk: remove possibly_osf_label variable that is only assigned

---
 fdisk/fdisk.c |    4 ----
 1 files changed, 0 insertions(+), 4 deletions(-)
fdisk/fdisk.c +0 -4
index a6de6e2..a23f45e 100644
@@ -292,8 +292,6 @@ int has_topology;
 
 enum labeltype disklabel = DOS_LABEL;	/* Current disklabel */
 
-int	possibly_osf_label = 0;
-
 jmp_buf listingbuf;
 
 static void __attribute__ ((__noreturn__)) usage(FILE *out)
@@ -862,7 +860,6 @@ create_doslabel(void) {
 	sun_nolabel();  /* otherwise always recognised as sun */
 	sgi_nolabel();  /* otherwise always recognised as sgi */
 	disklabel = DOS_LABEL;
-	possibly_osf_label = 0;
 	partitions = 4;
 
 	/* Zero out the MBR buffer */
@@ -1155,7 +1152,6 @@ get_boot(enum action what) {
 		return 0;
 
 	if (check_osf_label()) {
-		possibly_osf_label = 1;
 		if (!valid_part_table_flag(MBRbuffer)) {
 			disklabel = OSF_LABEL;
 			return 0;
Signature · 1 line
1.7.7
#2 lore raw

PATCH 3/13 fdisk: move code for renaming SGI bootfile to SGI module

In reply to Francesco Cosoleto

This moves the code for renaming SGI bootfile from command_prompt() to sgi_set_bootfilename() function.

---
 fdisk/fdisk.c         |   12 +++---------
 fdisk/fdisksgilabel.c |   21 +++++++++++----------
 fdisk/fdisksgilabel.h |    3 +--
 3 files changed, 15 insertions(+), 21 deletions(-)
fdisk/fdisk.c +3 -9
index a23f45e..1a4c823 100644
@@ -2906,15 +2906,9 @@ static void command_prompt(void)
 				unknown_command(c);
 			break;
 		case 'b':
-			if (disklabel == SGI_LABEL) {
-				printf(_("\nThe current boot file is: %s\n"),
-				       sgi_get_bootfile());
-				if (read_chars(_("Please enter the name of the "
-					       "new boot file: ")) == '\n')
-					printf(_("Boot file unchanged\n"));
-				else
-					sgi_set_bootfile(line_ptr);
-			} else if (disklabel == DOS_LABEL) {
+			if (disklabel == SGI_LABEL)
+				sgi_set_bootfile();
+			else if (disklabel == DOS_LABEL) {
 				disklabel = OSF_LABEL;
 				bsd_command_prompt();
 				disklabel = DOS_LABEL;
fdisk/fdisksgilabel.c +11 -10
index 002688a..b432b13 100644
@@ -316,20 +316,21 @@ sgi_check_bootfile(const char* aFile) {
 	return 0;	/* filename did not change */
 }
 
-const char *
-sgi_get_bootfile(void) {
-	return (char *) sgilabel->boot_file;
-}
-
 void
-sgi_set_bootfile(const char* aFile) {
+sgi_set_bootfile(void)
+{
+	printf(_("\nThe current boot file is: %s\n"), sgilabel->boot_file);
+	if (read_chars(_("Please enter the name of the new boot file: ")) == '\n') {
+		printf(_("Boot file unchanged\n"));
+		return;
+	}
 
-	if (sgi_check_bootfile(aFile)) {
+	if (sgi_check_bootfile(line_ptr)) {
 		size_t i = 0;
 		while (i < 16) {
-			if ((aFile[i] != '\n')	/* in principle caught again by next line */
-			    &&  (strlen(aFile) > i))
-				sgilabel->boot_file[i] = aFile[i];
+			if ((line_ptr[i] != '\n')	/* in principle caught again by next line */
+			    &&  (strlen(line_ptr) > i))
+				sgilabel->boot_file[i] = line_ptr[i];
 			else
 				sgilabel->boot_file[i] = 0;
 			i++;
fdisk/fdisksgilabel.h +1 -2
index 1c47bb4..255fe9c 100644
@@ -133,7 +133,6 @@ extern void	sgi_set_bootpartition( int i );
 extern void	sgi_set_swappartition( int i );
 extern int	sgi_get_bootpartition( void );
 extern int	sgi_get_swappartition( void );
-extern void	sgi_set_bootfile( const char* aFile );
-extern const char *sgi_get_bootfile( void );
+extern void	sgi_set_bootfile(void);
 
 #endif /* FDISK_SGI_LABEL_H */
Signature · 1 line
1.7.7
#3 lore raw

PATCH 4/13 fdisk: drop one parameter from try() function

In reply to Francesco Cosoleto
---
 fdisk/fdisk.c |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)
fdisk/fdisk.c +5 -6
index 1a4c823..1ecab17 100644
@@ -2800,15 +2800,13 @@ gpt_warning(char *dev)
 }
 
 static void
-try(char *device, int user_specified) {
+try(char *device)
+{
 	int gb;
 
 	disk_device = device;
 	if (setjmp(listingbuf))
 		return;
-	if (!user_specified)
-		if (is_ide_cdrom_or_tape(device))
-			return;
 	gpt_warning(device);
 	if ((fd = open(disk_device, type_open)) >= 0) {
 		gb = get_boot(try_only);
@@ -2859,7 +2857,7 @@ tryprocpt(void) {
 		if (is_whole_disk(devname)) {
 			char *cn = canonicalize_path(devname);
 			if (cn) {
-				try(cn, 0);
+				try(cn);
 				free(cn);
 			}
 		}
@@ -3079,7 +3077,8 @@ main(int argc, char **argv) {
 			dummy(&k);
 			listing = 1;
 			for (k = optind; k < argc; k++)
-				try(argv[k], 1);
+				if (!is_ide_cdrom_or_tape(argv[k]))
+					try(argv[k]);
 		} else {
 			/* we no longer have default device names */
 			/* but we can use /proc/partitions instead */
Signature · 1 line
1.7.7
maintainer
#4 lore raw

Re: [PATCH 04/13] fdisk: drop one parameter from try() function

fdisk/fdisk.c main(int argc, char **argv) { · line 3080

Quoted 1 line · Francesco Cosoleto
fdisk/fdisk.c +1 -0
+				if (!is_ide_cdrom_or_tape(argv[k]))

This function is pretty archaic, we use CDROM_GET_CAPABILITY ioctl to detect cdroms in libblkid/src/probe.c.

It would be nice to add

blkdev_is_cdrom(char *)
blkdev_is_cdrom_fd(int fd)

to lkib/blkdev.c and use it for fdisk and libblkid. Volunteer? :-)

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#5 lore raw

Re: [PATCH 04/13] fdisk: drop one parameter from try() function

In reply to Francesco Cosoleto

Hi!

Quoted 28 lines · Francesco Cosoleto
fdisk/fdisk.c +3 -5
@@ -2800,15 +2800,13 @@ gpt_warning(char *dev)
 }
 
 static void
-try(char *device, int user_specified) {
+try(char *device)
+{
 	int gb;
 
 	disk_device = device;
 	if (setjmp(listingbuf))
 		return;
-	if (!user_specified)
-		if (is_ide_cdrom_or_tape(device))
-			return;
 	gpt_warning(device);
 	if ((fd = open(disk_device, type_open)) >= 0) {
 		gb = get_boot(try_only);
@@ -2859,7 +2857,7 @@ tryprocpt(void) {
 		if (is_whole_disk(devname)) {
 			char *cn = canonicalize_path(devname);
 			if (cn) {
-				try(cn, 0);
+				try(cn);
 				free(cn);
 			}
 		}

fdisk/fdisk.c main(int argc, char **argv) { · line 3084

Quoted 9 lines
fdisk/fdisk.c +2 -1
@@ -3079,7 +3077,8 @@ main(int argc, char **argv) {
 			dummy(&k);
 			listing = 1;
 			for (k = optind; k < argc; k++)
-				try(argv[k], 1);
+				if (!is_ide_cdrom_or_tape(argv[k]))
+					try(argv[k]);
 		} else {
 			/* we no longer have default device names */
 			/* but we can use /proc/partitions instead */

The logic on user_specified is inverted so it seems the checks on the call sites would need to be swapped?

regards,
guillem

#6 lore raw

Re: [PATCH 04/13] fdisk: drop one parameter from try() function

2011/12/22 Guillem Jover <[email protected]>:

Quoted 2 lines

The logic on user_specified is inverted so it seems the checks on the call sites would need to be swapped?

Correct. Thank you for pointing out this.

Attached a patch to fix this regression.

Francesco

From 2cf578cd4abfbe0609e1de97de754cfcc19a558c Mon Sep 17 00:00:00 2001
From: Francesco Cosoleto <[email protected]>
Date: Thu, 22 Dec 2011 15:53:25 +0100
Subject: [PATCH] fdisk: fix incorrect position of is_ide_cdrom_or_tape() call

---
 fdisk/fdisk.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
fdisk/fdisk.c +3 -3
index c41da7a..bde60a9 100644
@@ -2848,7 +2848,8 @@ print_all_partition_table_from_option(void)
 		if (is_whole_disk(devname)) {
 			char *cn = canonicalize_path(devname);
 			if (cn) {
-				print_partition_table_from_option(cn);
+				if (!is_ide_cdrom_or_tape(cn))
+					print_partition_table_from_option(cn);
 				free(cn);
 			}
 		}
@@ -3057,8 +3058,7 @@ main(int argc, char **argv) {
 			dummy(&k);
 			listing = 1;
 			for (k = optind; k < argc; k++)
-				if (!is_ide_cdrom_or_tape(argv[k]))
-					print_partition_table_from_option(argv[k]);
+				print_partition_table_from_option(argv[k]);
 		} else
 			print_all_partition_table_from_option();
 		exit(0);
Signature · 1 line
1.7.7
maintainer Applied
#7 lore raw

Re: [PATCH 04/13] fdisk: drop one parameter from try() function

Quoted 2 lines · Francesco Cosoleto
 fdisk/fdisk.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

Applied, thanks. (Please, don't use attachment next time.)

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#8 lore raw

PATCH 5/13 fdisk: rename try() and tryprocpt() functions

In reply to Francesco Cosoleto
---
 fdisk/fdisk.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
fdisk/fdisk.c +9 -9
index 1ecab17..a8a1800 100644
@@ -2799,8 +2799,10 @@ gpt_warning(char *dev)
 			"The util fdisk doesn't support GPT. Use GNU Parted.\n\n"), dev);
 }
 
+/* Print disk geometry and partition table of a specified device (-l option) */
+
 static void
-try(char *device)
+print_partition_table_from_option(char *device)
 {
 	int gb;
 
@@ -2837,7 +2839,8 @@ try(char *device)
  * try all things in /proc/partitions that look like a full disk
  */
 static void
-tryprocpt(void) {
+print_all_partition_table_from_option(void)
+{
 	FILE *procpt;
 	char line[128], ptname[128], devname[256];
 	int ma, mi;
@@ -2857,7 +2860,7 @@ tryprocpt(void) {
 		if (is_whole_disk(devname)) {
 			char *cn = canonicalize_path(devname);
 			if (cn) {
-				try(cn);
+				print_partition_table_from_option(cn);
 				free(cn);
 			}
 		}
@@ -3078,12 +3081,9 @@ main(int argc, char **argv) {
 			listing = 1;
 			for (k = optind; k < argc; k++)
 				if (!is_ide_cdrom_or_tape(argv[k]))
-					try(argv[k]);
-		} else {
-			/* we no longer have default device names */
-			/* but we can use /proc/partitions instead */
-			tryprocpt();
-		}
+					print_partition_table_from_option(argv[k]);
+		} else
+			print_all_partition_table_from_option();
 		exit(0);
 	}
 
Signature · 1 line
1.7.7
#9 lore raw

PATCH 6/13 fdisk: get rid of type_open global variable

In reply to Francesco Cosoleto
---
 fdisk/fdisk.c |   11 +++--------
 1 files changed, 3 insertions(+), 8 deletions(-)
fdisk/fdisk.c +3 -8
index a8a1800..ab2e68d 100644
@@ -230,9 +230,6 @@ get_random_id(void) {
 	return (unsigned int)(tv.tv_sec + (tv.tv_usec << 12) + getpid());
 }
 
-/* normally O_RDWR, -l option gives O_RDONLY */
-static int type_open = O_RDWR;
-
 /*
  * Raw disk label. For DOS-type partition tables the MBR,
  * with descriptions of the primary partitions.
@@ -1120,7 +1117,7 @@ get_boot(enum action what) {
 		goto got_dos_table;		/* skip reading disk */
 
 	if (what != try_only) {
-		if ((fd = open(disk_device, type_open)) < 0) {
+		if ((fd = open(disk_device, O_RDWR)) < 0) {
 			if ((fd = open(disk_device, O_RDONLY)) < 0)
 				fatal(unable_to_open);
 			else
@@ -2810,7 +2807,7 @@ print_partition_table_from_option(char *device)
 	if (setjmp(listingbuf))
 		return;
 	gpt_warning(device);
-	if ((fd = open(disk_device, type_open)) >= 0) {
+	if ((fd = open(disk_device, O_RDONLY)) >= 0) {
 		gb = get_boot(try_only);
 		if (gb > 0) { /* I/O error */
 		} else if (gb < 0) { /* no DOS signature */
@@ -3072,7 +3069,6 @@ main(int argc, char **argv) {
 
 	if (optl) {
 		nowarn = 1;
-		type_open = O_RDONLY;
 		if (argc > optind) {
 			int k;
 			/* avoid gcc warning:
@@ -3091,7 +3087,6 @@ main(int argc, char **argv) {
 		unsigned long long size;
 
 		nowarn = 1;
-		type_open = O_RDONLY;
 
 		opts = argc - optind;
 		if (opts <= 0)
@@ -3099,7 +3094,7 @@ main(int argc, char **argv) {
 
 		for (j = optind; j < argc; j++) {
 			disk_device = argv[j];
-			if ((fd = open(disk_device, type_open)) < 0)
+			if ((fd = open(disk_device, O_RDONLY)) < 0)
 				fatal(unable_to_open);
 			if (blkdev_get_sectors(fd, &size) == -1)
 				fatal(ioctl_error);
Signature · 1 line
1.7.7
#10 lore raw

PATCH 7/13 fdisk: get rid of get_boot(create_empty_sun) call

In reply to Francesco Cosoleto

create_sunlabel() should create a new empty SUN disklabel without checking data itself writes to memory and initialize internal related fdisk variables.

---
 fdisk/fdisk.c         |    7 ++-----
 fdisk/fdisk.h         |    3 +--
 fdisk/fdisksunlabel.c |   17 +++++++++++------
 3 files changed, 14 insertions(+), 13 deletions(-)
fdisk/fdisk.c +2 -5
index ab2e68d..aacd73f 100644
@@ -51,6 +51,7 @@
 
 #include "gpt.h"
 
+static int get_boot(enum action what);
 static void delete_partition(int i);
 
 #define hex_val(c)	({ \
@@ -1090,7 +1091,7 @@ void zeroize_mbr_buffer(void)
  *    0: found or created label
  *    1: I/O error
  */
-int
+static int
 get_boot(enum action what) {
 	int i;
 
@@ -1108,9 +1109,6 @@ get_boot(enum action what) {
 		pe->changed = (what == create_empty_dos);
 	}
 
-	if (what == create_empty_sun && check_sun_label())
-		return 0;
-
 	memset(MBRbuffer, 0, 512);
 
 	if (what == create_empty_dos)
@@ -1175,7 +1173,6 @@ got_dos_table:
 		case try_only:
 		        return -1;
 		case create_empty_dos:
-		case create_empty_sun:
 			break;
 		default:
 			fprintf(stderr, _("Internal error\n"));
fdisk/fdisk.h +1 -2
index 67642cd..6641880 100644
@@ -54,7 +54,7 @@ enum failure {ioctl_error,
 	unable_to_open, unable_to_read, unable_to_seek,
 	unable_to_write};
 
-enum action {fdisk, try_only, create_empty_dos, create_empty_sun};
+enum action {fdisk, try_only, create_empty_dos};
 
 struct geom {
 	unsigned int heads;
@@ -69,7 +69,6 @@ extern unsigned int display_in_cyl_units, units_per_sector;
 extern void change_units(void);
 extern void fatal(enum failure why);
 extern void get_geometry(int fd, struct geom *);
-extern int get_boot(enum action what);
 extern int  get_partition(int warn, int max);
 extern void list_types(struct systypes *sys);
 extern int read_line (int *asked);
fdisk/fdisksunlabel.c +11 -6
index 80408dd..4fd8545 100644
@@ -109,6 +109,12 @@ static void set_sun_partition(int i, uint32_t start, uint32_t stop, uint16_t sys
 	print_partition_size(i + 1, start, stop, sysid);
 }
 
+static void init(void)
+{
+	disklabel = SUN_LABEL;
+	partitions = SUN_NUM_PARTITIONS;
+}
+
 void sun_nolabel(void)
 {
 	sunlabel->magic = 0;
@@ -125,6 +131,8 @@ int check_sun_label(void)
 		other_endian = 0;
 		return 0;
 	}
+
+	init();
 	other_endian = (sunlabel->magic == SUN_LABEL_MAGIC_SWAPPED);
 
 	ush = ((unsigned short *) (sunlabel + 1)) - 1;
@@ -176,8 +184,6 @@ int check_sun_label(void)
 		}
 	}
 	update_units();
-	disklabel = SUN_LABEL;
-	partitions = SUN_NUM_PARTITIONS;
 	return 1;
 }
 
@@ -195,7 +201,10 @@ void create_sunlabel(void)
 #else
 	other_endian = 0;
 #endif
+
+	init();
 	zeroize_mbr_buffer();
+
 	sunlabel->magic = SSWAP16(SUN_LABEL_MAGIC);
 	sunlabel->sanity = SSWAP32(SUN_LABEL_SANE);
 	sunlabel->version = SSWAP32(SUN_LABEL_VERSION);
@@ -249,9 +258,6 @@ void create_sunlabel(void)
 	} else
 	        ndiv = cylinders * 2 / 3;
 
-	/* Make sure print_partition_size() uses correct sysid names */
-	disklabel = SUN_LABEL;
-
 	set_sun_partition(0, 0, ndiv * heads * sectors,
 			  SUN_TAG_LINUX_NATIVE);
 	set_sun_partition(1, ndiv * heads * sectors,
@@ -270,7 +276,6 @@ void create_sunlabel(void)
 	}
 
 	set_all_unchanged();
-	get_boot(create_empty_sun);
 	set_changed(0);
 }
 
Signature · 1 line
1.7.7
#11 lore raw

PATCH 8/13 fdisk: remove unused guess_device_type() function

In reply to Francesco Cosoleto
---
 fdisk/fdisk.c         |    1 -
 fdisk/fdisksunlabel.c |   38 --------------------------------------
 fdisk/fdisksunlabel.h |    1 -
 3 files changed, 0 insertions(+), 40 deletions(-)
fdisk/fdisk.c +0 -1
index aacd73f..8f5869d 100644
@@ -1031,7 +1031,6 @@ get_geometry(int fd, struct geom *g) {
 	unsigned long long llcyls, nsects = 0;
 
 	get_topology(fd);
-	guess_device_type(fd);
 	heads = cylinders = sectors = 0;
 	kern_heads = kern_sectors = 0;
 	pt_heads = pt_sectors = 0;
fdisk/fdisksunlabel.c +0 -38
index 4fd8545..4a6db35 100644
@@ -15,24 +15,17 @@
 #include <string.h>		/* strstr */
 #include <unistd.h>		/* write */
 #include <sys/ioctl.h>		/* ioctl */
-#include <sys/stat.h>		/* stat */
-#include <sys/sysmacros.h>	/* major */
 
 #include "nls.h"
 #include "blkdev.h"
 
 #include <endian.h>
-#ifdef HAVE_LINUX_MAJOR_H
-#include <linux/major.h>	/* FLOPPY_MAJOR */
-#endif
 
 #include "common.h"
 #include "fdisk.h"
 #include "fdisksunlabel.h"
 
 static int     other_endian = 0;
-static int     scsi_disk = 0;
-static int     floppy = 0;
 
 struct systypes sun_sys_types[] = {
 	{SUN_TAG_UNASSIGNED, N_("Unassigned")},
@@ -66,37 +59,6 @@ static inline uint32_t __swap32(uint32_t x) {
 #define SSWAP32(x) (other_endian ? __swap32(x) \
 				 : (uint32_t)(x))
 
-#ifndef FLOPPY_MAJOR
-#define FLOPPY_MAJOR 2
-#endif
-#ifndef IDE0_MAJOR
-#define IDE0_MAJOR 3
-#endif
-#ifndef IDE1_MAJOR
-#define IDE1_MAJOR 22
-#endif
-void guess_device_type(int fd)
-{
-	struct stat bootstat;
-
-	if (fstat (fd, &bootstat) < 0) {
-                scsi_disk = 0;
-                floppy = 0;
-	} else if (S_ISBLK(bootstat.st_mode)
-		   && (major(bootstat.st_rdev) == IDE0_MAJOR ||
-		       major(bootstat.st_rdev) == IDE1_MAJOR)) {
-                scsi_disk = 0;
-                floppy = 0;
-	} else if (S_ISBLK(bootstat.st_mode)
-		   && major(bootstat.st_rdev) == FLOPPY_MAJOR) {
-                scsi_disk = 0;
-                floppy = 1;
-	} else {
-                scsi_disk = 1;
-                floppy = 0;
-	}
-}
-
 static void set_sun_partition(int i, uint32_t start, uint32_t stop, uint16_t sysid)
 {
 	sunlabel->part_tags[i].tag = SSWAP16(sysid);
fdisk/fdisksunlabel.h +0 -1
index b7419cb..fa3f964 100644
@@ -77,7 +77,6 @@ struct sun_disk_label {
 
 /* fdisksunlabel.c */
 extern struct systypes sun_sys_types[];
-extern void guess_device_type(int fd);
 extern int check_sun_label(void);
 extern void sun_nolabel(void);
 extern void create_sunlabel(void);
Signature · 1 line
1.7.7
#12 lore raw

PATCH 9/13 fdisk: don't use get_existing_partition() if disk label isn't dos

In reply to Francesco Cosoleto
---
 fdisk/fdisk.c |   27 +++++++++------------------
 1 files changed, 9 insertions(+), 18 deletions(-)
fdisk/fdisk.c +9 -18
index 8f5869d..53eed25 100644
@@ -1472,11 +1472,16 @@ get_partition(int warn, int max) {
 	return get_partition_dflt(warn, max, 0);
 }
 
+/* User partition selection unless one partition only is available */
+
 static int
 get_existing_partition(int warn, int max) {
 	int pno = -1;
 	int i;
 
+	if (disklabel != DOS_LABEL)
+		goto not_implemented;
+
 	for (i = 0; i < max; i++) {
 		struct pte *pe = &ptes[i];
 		struct partition *p = pe->part_table;
@@ -1495,7 +1500,8 @@ get_existing_partition(int warn, int max) {
 	printf(_("No partition is defined yet!\n"));
 	return -1;
 
- not_unique:
+not_implemented:
+not_unique:
 	return get_partition(warn, max);
 }
 
@@ -1650,14 +1656,7 @@ change_sysid(void) {
 	int i, sys, origsys;
 	struct partition *p;
 
-	/* If sgi_label then don't use get_existing_partition,
-	   let the user select a partition, since get_existing_partition()
-	   only works for Linux like partition tables. */
-	if (disklabel != SGI_LABEL) {
-		i = get_existing_partition(0, partitions);
-	} else {
-		i = get_partition(0, partitions);
-	}
+	i = get_existing_partition(0, partitions);
 
 	if (i == -1)
 		return;
@@ -2922,15 +2921,7 @@ static void command_prompt(void)
 				unknown_command(c);
 			break;
 		case 'd':
-			/* If sgi_label then don't use get_existing_partition,
-			   let the user select a partition, since
-			   get_existing_partition() only works for Linux-like
-			   partition tables */
-			if (disklabel != SGI_LABEL) {
-				j = get_existing_partition(1, partitions);
-			} else {
-				j = get_partition(1, partitions);
-			}
+			j = get_existing_partition(1, partitions);
 			if (j >= 0)
 				delete_partition(j);
 			break;
Signature · 1 line
1.7.7
#13 lore raw

PATCH 10/13 fdisk: avoid an intermediate variable in command_prompt()

In reply to Francesco Cosoleto
---
 fdisk/fdisk.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
fdisk/fdisk.c +5 -4
index 53eed25..acf0e67 100644
@@ -1635,6 +1635,9 @@ static void dos_delete_partition(int i)
 static void
 delete_partition(int i)
 {
+	if (i < 0)
+		return;
+
 	if (warn_geometry())
 		return;		/* C/H/S not set */
 
@@ -2869,7 +2872,7 @@ unknown_command(int c) {
 
 static void command_prompt(void)
 {
-	int c, j;
+	int c;
 
 	if (disklabel == OSF_LABEL) {
 		putchar('\n');
@@ -2921,9 +2924,7 @@ static void command_prompt(void)
 				unknown_command(c);
 			break;
 		case 'd':
-			j = get_existing_partition(1, partitions);
-			if (j >= 0)
-				delete_partition(j);
+			delete_partition(get_existing_partition(1, partitions));
 			break;
 		case 'i':
 			if (disklabel == SGI_LABEL)
Signature · 1 line
1.7.7
#14 lore raw

PATCH 11/13 fdisk: Fix bad invalid flag 0x00000 warning message

In reply to Francesco Cosoleto

This splits check_dos_label() and dos_init() off from get_boot() and gets rid of the invalid flag 0x00000 warning message due to a check for MBR signs in zeroized buffer:

	memset(MBRbuffer, 0, 512);

	if (what == create_empty_dos)
		goto got_dos_table;
[...]
got_dos_table:
	if (!valid_part_table_flag(MBRbuffer)) {
[...]
	if (!valid_part_table_flag(pe->sectorbuffer))
		fprintf(stderr, _("Warning: invalid flag 0x%04x of partition "

[...]

---
 fdisk/fdisk.c |  161 +++++++++++++++++++++++++++------------------------------
 fdisk/fdisk.h |    2 +-
 2 files changed, 78 insertions(+), 85 deletions(-)
fdisk/fdisk.c +77 -84
index acf0e67..800ed02 100644
@@ -288,7 +288,7 @@ unsigned long grain = DEFAULT_SECTOR_SIZE,
 	      alignment_offset;
 int has_topology;
 
-enum labeltype disklabel = DOS_LABEL;	/* Current disklabel */
+enum labeltype disklabel;	/* Current disklabel */
 
 jmp_buf listingbuf;
 
@@ -850,6 +850,30 @@ dos_set_mbr_id(void) {
 	dos_print_mbr_id();
 }
 
+static void dos_init(void)
+{
+	int i;
+
+	disklabel = DOS_LABEL;
+	partitions = 4;
+	ext_index = 0;
+	extended_offset = 0;
+
+	for (i = 0; i < 4; i++) {
+		struct pte *pe = &ptes[i];
+
+		pe->part_table = pt_offset(MBRbuffer, i);
+		pe->ext_pointer = NULL;
+		pe->offset = 0;
+		pe->sectorbuffer = MBRbuffer;
+		pe->changed = 0;
+	}
+
+	warn_geometry();
+	warn_limits();
+	warn_alignment();
+}
+
 static void
 create_doslabel(void) {
 	unsigned int id = get_random_id();
@@ -857,19 +881,17 @@ create_doslabel(void) {
 	fprintf(stderr, _("Building a new DOS disklabel with disk identifier 0x%08x.\n"), id);
 	sun_nolabel();  /* otherwise always recognised as sun */
 	sgi_nolabel();  /* otherwise always recognised as sgi */
-	disklabel = DOS_LABEL;
-	partitions = 4;
 
-	/* Zero out the MBR buffer */
-	extended_offset = 0;
+	dos_init();
+	zeroize_mbr_buffer();
+
 	set_all_unchanged();
 	set_changed(0);
-	get_boot(create_empty_dos);
 
 	/* Generate an MBR ID for this disk */
 	dos_write_mbr_id(MBRbuffer, id);
 
-	/* Mark it bootable (unfortunately required) */
+	/* Put MBR signature */
 	write_part_table_flag(MBRbuffer);
 }
 
@@ -1084,6 +1106,42 @@ void zeroize_mbr_buffer(void)
 		memset(MBRbuffer, 0, MAX_SECTOR_SIZE);
 }
 
+static int check_dos_label(void)
+{
+	int i;
+
+	if (!valid_part_table_flag(MBRbuffer))
+		return 0;
+
+	dos_init();
+
+	for (i = 0; i < 4; i++) {
+		struct pte *pe = &ptes[i];
+
+		if (IS_EXTENDED (pe->part_table->sys_ind)) {
+			if (partitions != 4)
+				fprintf(stderr, _("Ignoring extra extended "
+					"partition %d\n"), i + 1);
+			else
+				read_extended(i);
+		}
+	}
+
+	for (i = 3; i < partitions; i++) {
+		struct pte *pe = &ptes[i];
+
+		if (!valid_part_table_flag(pe->sectorbuffer)) {
+			fprintf(stderr,
+				_("Warning: invalid flag 0x%04x of partition "
+				"table %d will be corrected by w(rite)\n"),
+				part_table_flag(pe->sectorbuffer), i + 1);
+			pe->changed = 1;
+		}
+	}
+
+	return 1;
+}
+
 /*
  * Read MBR.  Returns:
  *   -1: no 0xaa55 flag present (possibly entire disk BSD)
@@ -1092,27 +1150,10 @@ void zeroize_mbr_buffer(void)
  */
 static int
 get_boot(enum action what) {
-	int i;
-
-	partitions = 4;
-	ext_index = 0;
-	extended_offset = 0;
-
-	for (i = 0; i < 4; i++) {
-		struct pte *pe = &ptes[i];
-
-		pe->part_table = pt_offset(MBRbuffer, i);
-		pe->ext_pointer = NULL;
-		pe->offset = 0;
-		pe->sectorbuffer = MBRbuffer;
-		pe->changed = (what == create_empty_dos);
-	}
 
+	disklabel = ANY_LABEL;
 	memset(MBRbuffer, 0, 512);
 
-	if (what == create_empty_dos)
-		goto got_dos_table;		/* skip reading disk */
-
 	if (what != try_only) {
 		if ((fd = open(disk_device, O_RDWR)) < 0) {
 			if ((fd = open(disk_device, O_RDONLY)) < 0)
@@ -1133,17 +1174,9 @@ get_boot(enum action what) {
 
 	update_units();
 
-	if (check_sun_label())
-		return 0;
-
-	if (check_sgi_label())
-		return 0;
-
-	if (check_aix_label())
-		return 0;
-
-	if (check_mac_label())
-		return 0;
+	if (!check_dos_label())
+		if (check_sun_label() || check_sgi_label() || check_aix_label() || check_mac_label())
+			return 0;
 
 	if (check_osf_label()) {
 		if (!valid_part_table_flag(MBRbuffer)) {
@@ -1152,61 +1185,21 @@ get_boot(enum action what) {
 		}
 		printf(_("This disk has both DOS and BSD magic.\n"
 			 "Give the 'b' command to go to BSD mode.\n"));
+		return 0;
 	}
 
-got_dos_table:
+	if (disklabel == ANY_LABEL) {
+		if (what == try_only)
+			return -1;
 
-	if (!valid_part_table_flag(MBRbuffer)) {
-		switch(what) {
-		case fdisk:
-			fprintf(stderr,
-				_("Device contains neither a valid DOS "
-				  "partition table, nor Sun, SGI or OSF "
-				  "disklabel\n"));
+		fprintf(stderr,
+			_("Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel\n"));
 #ifdef __sparc__
-			create_sunlabel();
+		create_sunlabel();
 #else
-			create_doslabel();
+		create_doslabel();
 #endif
-			return 0;
-		case try_only:
-		        return -1;
-		case create_empty_dos:
-			break;
-		default:
-			fprintf(stderr, _("Internal error\n"));
-			exit(1);
-		}
-	}
-
-	for (i = 0; i < 4; i++) {
-		struct pte *pe = &ptes[i];
-
-		if (IS_EXTENDED (pe->part_table->sys_ind)) {
-			if (partitions != 4)
-				fprintf(stderr, _("Ignoring extra extended "
-					"partition %d\n"), i + 1);
-			else
-				read_extended(i);
-		}
-	}
-
-	for (i = 3; i < partitions; i++) {
-		struct pte *pe = &ptes[i];
-
-		if (!valid_part_table_flag(pe->sectorbuffer)) {
-			fprintf(stderr,
-				_("Warning: invalid flag 0x%04x of partition "
-				"table %d will be corrected by w(rite)\n"),
-				part_table_flag(pe->sectorbuffer), i + 1);
-			pe->changed = 1;
-		}
 	}
-
-	warn_geometry();
-	warn_limits();
-	warn_alignment();
-
 	return 0;
 }
 
fdisk/fdisk.h +1 -1
index 6641880..84be09c 100644
@@ -54,7 +54,7 @@ enum failure {ioctl_error,
 	unable_to_open, unable_to_read, unable_to_seek,
 	unable_to_write};
 
-enum action {fdisk, try_only, create_empty_dos};
+enum action {fdisk, try_only};
 
 struct geom {
 	unsigned int heads;
Signature · 1 line
1.7.7
#15 lore raw

PATCH 12/13 fdisk: improve some messages

In reply to Francesco Cosoleto
---
 fdisk/fdisk.c         |    2 +-
 fdisk/fdisksgilabel.c |    4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
fdisk/fdisk.c +1 -1
index 800ed02..c41da7a 100644
@@ -1193,7 +1193,7 @@ get_boot(enum action what) {
 			return -1;
 
 		fprintf(stderr,
-			_("Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel\n"));
+			_("Device does not contain a recognized partition table\n"));
 #ifdef __sparc__
 		create_sunlabel();
 #else
fdisk/fdisksgilabel.c +2 -2
index b432b13..dd3efd0 100644
@@ -452,7 +452,7 @@ verify_sgi(int verbose)
 		lastblock = sgi_get_num_sectors(Index[0]);
 	} else {
 		if (verbose)
-			printf(_("One Partition (#11) should cover the entire disk.\n"));
+			printf(_("Partition 11 should cover the entire disk.\n"));
 		if (debug>2)
 			printf("sysid=%d\tpartition=%d\n",
 			       sgi_get_sysid(Index[0]), Index[0]+1);
@@ -570,7 +570,7 @@ sgi_set_partition(int i, unsigned int start, unsigned int length, int sys) {
 	sgilabel->partitions[i].start_sector = SSWAP32(start);
 	set_changed(i);
 	if (sgi_gaps() < 0)	/* rebuild freelist */
-		printf(_("Do You know, You got a partition overlap on the disk?\n"));
+		printf(_("Partition overlap on the disk.\n"));
 	if (length)
 		print_partition_size(i + 1, start, start + length, sys);
 }
Signature · 1 line
1.7.7
#16 lore raw

PATCH 13/13 tests: update fdisk tests

In reply to Francesco Cosoleto
---
 tests/expected/fdisk/align-512-4K           |   12 ++++++------
 tests/expected/fdisk/align-512-4K-63        |   12 ++++++------
 tests/expected/fdisk/align-512-4K-md        |   24 ++++++++++++------------
 tests/expected/fdisk/align-512-512          |    6 +++---
 tests/expected/fdisk/align-512-512-topology |    6 +++---
 tests/expected/fdisk/oddinput               |    1 -
 6 files changed, 30 insertions(+), 31 deletions(-)
tests/expected/fdisk/align-512-4K +6 -6
index 615d609..a510c73 100644
@@ -1,16 +1,16 @@
 Create partitions
-Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 
-Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
+
+Changes will remain in memory only, until you decide to write them.
+Be careful before using the write command.
+
+Device does not contain a recognized partition table
+
 
 The device presents a logical sector size that is smaller than
 the physical sector size. Aligning to a physical sector (or optimal
 I/O) size boundary is recommended, or performance may be impacted.
 
-
-Changes will remain in memory only, until you decide to write them.
-Be careful before using the write command.
-
 Command (m for help): Partition type:
    p   primary (0 primary, 0 extended, 4 free)
    e   extended
tests/expected/fdisk/align-512-4K-63 +6 -6
index 63cef9c..e8192ce 100644
@@ -1,16 +1,16 @@
 Create partitions
-Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 
-Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
+
+Changes will remain in memory only, until you decide to write them.
+Be careful before using the write command.
+
+Device does not contain a recognized partition table
+
 
 The device presents a logical sector size that is smaller than
 the physical sector size. Aligning to a physical sector (or optimal
 I/O) size boundary is recommended, or performance may be impacted.
 
-
-Changes will remain in memory only, until you decide to write them.
-Be careful before using the write command.
-
 Command (m for help): Partition type:
    p   primary (0 primary, 0 extended, 4 free)
    e   extended
tests/expected/fdisk/align-512-4K-md +12 -12
index 3a0e7d5..b0081c6 100644
@@ -1,16 +1,16 @@
 Create partitions
-Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 
-Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
+
+Changes will remain in memory only, until you decide to write them.
+Be careful before using the write command.
+
+Device does not contain a recognized partition table
+
 
 The device presents a logical sector size that is smaller than
 the physical sector size. Aligning to a physical sector (or optimal
 I/O) size boundary is recommended, or performance may be impacted.
 
-
-Changes will remain in memory only, until you decide to write them.
-Be careful before using the write command.
-
 Command (m for help): Partition type:
    p   primary (0 primary, 0 extended, 4 free)
    e   extended
@@ -44,18 +44,18 @@ Alignment offsets:
 0
 0
 Create partitions (MD)
-Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 
-Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
+
+Changes will remain in memory only, until you decide to write them.
+Be careful before using the write command.
+
+Device does not contain a recognized partition table
+
 
 The device presents a logical sector size that is smaller than
 the physical sector size. Aligning to a physical sector (or optimal
 I/O) size boundary is recommended, or performance may be impacted.
 
-
-Changes will remain in memory only, until you decide to write them.
-Be careful before using the write command.
-
 Command (m for help): Partition type:
    p   primary (0 primary, 0 extended, 4 free)
    e   extended
tests/expected/fdisk/align-512-512 +3 -3
index 7f1cdb9..190ad0a 100644
@@ -1,12 +1,12 @@
 Create partitions
-Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
-
-Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
 
 Changes will remain in memory only, until you decide to write them.
 Be careful before using the write command.
 
+Device does not contain a recognized partition table
+
+
 Command (m for help): Partition type:
    p   primary (0 primary, 0 extended, 4 free)
    e   extended
tests/expected/fdisk/align-512-512-topology +3 -3
index 3623f0b..2180081 100644
@@ -1,12 +1,12 @@
 Create partitions
-Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
-
-Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
 
 Changes will remain in memory only, until you decide to write them.
 Be careful before using the write command.
 
+Device does not contain a recognized partition table
+
+
 Command (m for help): Partition type:
    p   primary (0 primary, 0 extended, 4 free)
    e   extended
tests/expected/fdisk/oddinput +0 -1
index 7fd14b5..8714f63 100644
@@ -8,7 +8,6 @@ Disk testimage: 10 MB, 10485760 bytes
 Units = cylinders of 16065 * 512 = 8225280 bytes
 Sector size (logical/physical): 512 bytes / 512 bytes
 I/O size (minimum/optimal): 512 bytes / 512 bytes
-Disk identifier: 0x00000000
 
 Nonexistant file
 Too small file
Signature · 1 line
1.7.7
maintainer
#17 lore raw
In reply to Francesco Cosoleto

fdisk/fdisk.c

Quoted 2 lines · Francesco Cosoleto
fdisk/fdisk.c +0 -0

All (13) patches applied. Good work, you fixed exactly the things that I have hated (poor separation of DOS and non-DOS code).

Thanks!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com