Skip to contentSkip to conversation

fdisk: create DOS specific write table function

PATCH Applied Davidlohr Bueso 3 participants 4 messages May 6, 2012 – May 10, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 3/5 fdisk: create DOS specific write table function

From: Davidlohr Bueso <[email protected]>

---
 fdisk/fdisk.c         |   27 ++++-----------------------
 fdisk/fdiskdoslabel.c |   27 +++++++++++++++++++++++++++
 fdisk/fdiskdoslabel.h |    1 +
 3 files changed, 32 insertions(+), 23 deletions(-)
fdisk/fdisk.c +4 -23
index 8c2a162..3044879 100644
@@ -1672,31 +1672,12 @@ static void
 write_table(void) {
 	int i;
 
-	if (disklabel == DOS_LABEL) {
-		/* MBR (primary partitions) */
-		if (!MBRbuffer_changed) {
-			for (i = 0; i < 4; i++)
-				if (ptes[i].changed)
-					MBRbuffer_changed = 1;
-		}
-		if (MBRbuffer_changed) {
-			write_part_table_flag(MBRbuffer);
-			write_sector(fd, 0, MBRbuffer);
-		}
-		/* EBR (logical partitions) */
-		for (i = 4; i < partitions; i++) {
-			struct pte *pe = &ptes[i];
-
-			if (pe->changed) {
-				write_part_table_flag(pe->sectorbuffer);
-				write_sector(fd, pe->offset, pe->sectorbuffer);
-			}
-		}
-	}
-	else if (disklabel == SGI_LABEL) {
+	if (disklabel == DOS_LABEL)
+		dos_write_table();
+	else if (disklabel == SGI_LABEL)
 		/* no test on change? the printf below might be mistaken */
 		sgi_write_table();
-	} else if (disklabel == SUN_LABEL) {
+	else if (disklabel == SUN_LABEL) {
 		int needw = 0;
 
 		for (i=0; i<8; i++)
fdisk/fdiskdoslabel.c +27 -0
index 3f820db..a13a83a 100644
@@ -657,3 +657,30 @@ void dos_new_partition(void)
 			printf(_("Invalid partition type `%c'\n"), c);
 	}
 }
+
+void dos_write_table(void)
+{
+	int i;
+
+	if (disklabel == DOS_LABEL) {
+		/* MBR (primary partitions) */
+		if (!MBRbuffer_changed) {
+			for (i = 0; i < 4; i++)
+				if (ptes[i].changed)
+					MBRbuffer_changed = 1;
+		}
+		if (MBRbuffer_changed) {
+			write_part_table_flag(MBRbuffer);
+			write_sector(fd, 0, MBRbuffer);
+		}
+		/* EBR (logical partitions) */
+		for (i = 4; i < partitions; i++) {
+			struct pte *pe = &ptes[i];
+
+			if (pe->changed) {
+				write_part_table_flag(pe->sectorbuffer);
+				write_sector(fd, pe->offset, pe->sectorbuffer);
+			}
+		}
+	}
+}
fdisk/fdiskdoslabel.h +1 -0
index e45a026..8c116f7 100644
@@ -52,5 +52,6 @@ extern int is_dos_partition(int t);
 extern void dos_init(void);
 extern void dos_add_partition(int n, int sys);
 extern void dos_new_partition(void);
+extern void dos_write_table(void);
 
 #endif
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 54 lines · Davidlohr Bueso

From: Davidlohr Bueso <[email protected]>

---
 fdisk/fdisk.c         |   27 ++++-----------------------
 fdisk/fdiskdoslabel.c |   27 +++++++++++++++++++++++++++
 fdisk/fdiskdoslabel.h |    1 +
 3 files changed, 32 insertions(+), 23 deletions(-)
fdisk/fdisk.c +4 -23
index 8c2a162..3044879 100644
@@ -1672,31 +1672,12 @@ static void
 write_table(void) {
 	int i;
 
-	if (disklabel == DOS_LABEL) {
-		/* MBR (primary partitions) */
-		if (!MBRbuffer_changed) {
-			for (i = 0; i < 4; i++)
-				if (ptes[i].changed)
-					MBRbuffer_changed = 1;
-		}
-		if (MBRbuffer_changed) {
-			write_part_table_flag(MBRbuffer);
-			write_sector(fd, 0, MBRbuffer);
-		}
-		/* EBR (logical partitions) */
-		for (i = 4; i < partitions; i++) {
-			struct pte *pe = &ptes[i];
-
-			if (pe->changed) {
-				write_part_table_flag(pe->sectorbuffer);
-				write_sector(fd, pe->offset, pe->sectorbuffer);
-			}
-		}
-	}
-	else if (disklabel == SGI_LABEL) {
+	if (disklabel == DOS_LABEL)
+		dos_write_table();
+	else if (disklabel == SGI_LABEL)
 		/* no test on change? the printf below might be mistaken */
 		sgi_write_table();
-	} else if (disklabel == SUN_LABEL) {
+	else if (disklabel == SUN_LABEL) {
 		int needw = 0;
 
 		for (i=0; i<8; i++)
fdisk/fdiskdoslabel.c +0 -0
index 3f820db..a13a83a 100644

fdisk/fdiskdoslabel.c void dos_new_partition(void) · line 665

Quoted 9 lines
fdisk/fdiskdoslabel.c +6 -0
@@ -657,3 +657,30 @@ void dos_new_partition(void)
 			printf(_("Invalid partition type `%c'\n"), c);
 	}
 }
+
+void dos_write_table(void)
+{
+	int i;
+
+	if (disklabel == DOS_LABEL) {

This if is unnecessary - the caller should make sure that disklabel == DOS_LABEL as write_table() does. What about

assert(disklabel == DOS_LABEL) instead?

Quoted 34 lines
fdisk/fdiskdoslabel.c +21 -0
+		/* MBR (primary partitions) */
+		if (!MBRbuffer_changed) {
+			for (i = 0; i < 4; i++)
+				if (ptes[i].changed)
+					MBRbuffer_changed = 1;
+		}
+		if (MBRbuffer_changed) {
+			write_part_table_flag(MBRbuffer);
+			write_sector(fd, 0, MBRbuffer);
+		}
+		/* EBR (logical partitions) */
+		for (i = 4; i < partitions; i++) {
+			struct pte *pe = &ptes[i];
+
+			if (pe->changed) {
+				write_part_table_flag(pe->sectorbuffer);
+				write_sector(fd, pe->offset, pe->sectorbuffer);
+			}
+		}
+	}
+}
fdisk/fdiskdoslabel.h +1 -0
index e45a026..8c116f7 100644
@@ -52,5 +52,6 @@ extern int is_dos_partition(int t);
 extern void dos_init(void);
 extern void dos_add_partition(int n, int sys);
 extern void dos_new_partition(void);
+extern void dos_write_table(void);
 
 #endif
Signature · 1 line
1.7.4.1

Petr

--
Petr Uzel
IRC: ptr_uzl @ freenode

#2 lore raw
Quoted 68 lines · Petr Uzel
Quoted 54 lines · Davidlohr Bueso

From: Davidlohr Bueso <[email protected]>

---
 fdisk/fdisk.c         |   27 ++++-----------------------
 fdisk/fdiskdoslabel.c |   27 +++++++++++++++++++++++++++
 fdisk/fdiskdoslabel.h |    1 +
 3 files changed, 32 insertions(+), 23 deletions(-)
fdisk/fdisk.c +4 -23
index 8c2a162..3044879 100644
@@ -1672,31 +1672,12 @@ static void
 write_table(void) {
 	int i;
 
-	if (disklabel == DOS_LABEL) {
-		/* MBR (primary partitions) */
-		if (!MBRbuffer_changed) {
-			for (i = 0; i < 4; i++)
-				if (ptes[i].changed)
-					MBRbuffer_changed = 1;
-		}
-		if (MBRbuffer_changed) {
-			write_part_table_flag(MBRbuffer);
-			write_sector(fd, 0, MBRbuffer);
-		}
-		/* EBR (logical partitions) */
-		for (i = 4; i < partitions; i++) {
-			struct pte *pe = &ptes[i];
-
-			if (pe->changed) {
-				write_part_table_flag(pe->sectorbuffer);
-				write_sector(fd, pe->offset, pe->sectorbuffer);
-			}
-		}
-	}
-	else if (disklabel == SGI_LABEL) {
+	if (disklabel == DOS_LABEL)
+		dos_write_table();
+	else if (disklabel == SGI_LABEL)
 		/* no test on change? the printf below might be mistaken */
 		sgi_write_table();
-	} else if (disklabel == SUN_LABEL) {
+	else if (disklabel == SUN_LABEL) {
 		int needw = 0;
 
 		for (i=0; i<8; i++)
fdisk/fdiskdoslabel.c +0 -0
index 3f820db..a13a83a 100644

fdisk/fdiskdoslabel.c void dos_new_partition(void) · line 665

Quoted 9 lines
fdisk/fdiskdoslabel.c +6 -0
@@ -657,3 +657,30 @@ void dos_new_partition(void)
 			printf(_("Invalid partition type `%c'\n"), c);
 	}
 }
+
+void dos_write_table(void)
+{
+	int i;
+
+	if (disklabel == DOS_LABEL) {

This if is unnecessary - the caller should make sure that disklabel == DOS_LABEL as write_table() does. What about

Quoted 1 line

assert(disklabel == DOS_LABEL) instead?

Absolutely right. I don't think we even need an assert here since this is only called from DOS context and fdisk.c does that check - plus no other functions or labels do it anyway.

From: Davidlohr Bueso <[email protected]>
Date: Sun, 6 May 2012 21:00:16 +0200
Subject: [PATCH] fdisk: create DOS specific write table function

---
 fdisk/fdisk.c         |   27 ++++-----------------------
 fdisk/fdiskdoslabel.c |   25 +++++++++++++++++++++++++
 fdisk/fdiskdoslabel.h |    1 +
 3 files changed, 30 insertions(+), 23 deletions(-)
fdisk/fdisk.c +4 -23
index 8c2a162..3044879 100644
@@ -1672,31 +1672,12 @@ static void
 write_table(void) {
 	int i;
 
-	if (disklabel == DOS_LABEL) {
-		/* MBR (primary partitions) */
-		if (!MBRbuffer_changed) {
-			for (i = 0; i < 4; i++)
-				if (ptes[i].changed)
-					MBRbuffer_changed = 1;
-		}
-		if (MBRbuffer_changed) {
-			write_part_table_flag(MBRbuffer);
-			write_sector(fd, 0, MBRbuffer);
-		}
-		/* EBR (logical partitions) */
-		for (i = 4; i < partitions; i++) {
-			struct pte *pe = &ptes[i];
-
-			if (pe->changed) {
-				write_part_table_flag(pe->sectorbuffer);
-				write_sector(fd, pe->offset, pe->sectorbuffer);
-			}
-		}
-	}
-	else if (disklabel == SGI_LABEL) {
+	if (disklabel == DOS_LABEL)
+		dos_write_table();
+	else if (disklabel == SGI_LABEL)
 		/* no test on change? the printf below might be mistaken */
 		sgi_write_table();
-	} else if (disklabel == SUN_LABEL) {
+	else if (disklabel == SUN_LABEL) {
 		int needw = 0;
 
 		for (i=0; i<8; i++)
fdisk/fdiskdoslabel.c +25 -0
index 3f820db..4e12d94 100644
@@ -657,3 +657,28 @@ void dos_new_partition(void)
 			printf(_("Invalid partition type `%c'\n"), c);
 	}
 }
+
+void dos_write_table(void)
+{
+	int i;
+	
+	/* MBR (primary partitions) */
+	if (!MBRbuffer_changed) {
+		for (i = 0; i < 4; i++)
+			if (ptes[i].changed)
+				MBRbuffer_changed = 1;
+	}
+	if (MBRbuffer_changed) {
+		write_part_table_flag(MBRbuffer);
+		write_sector(fd, 0, MBRbuffer);
+	}
+	/* EBR (logical partitions) */
+	for (i = 4; i < partitions; i++) {
+		struct pte *pe = &ptes[i];
+
+		if (pe->changed) {
+			write_part_table_flag(pe->sectorbuffer);
+			write_sector(fd, pe->offset, pe->sectorbuffer);
+		}
+	}
+}
fdisk/fdiskdoslabel.h +1 -0
index e45a026..8c116f7 100644
@@ -52,5 +52,6 @@ extern int is_dos_partition(int t);
 extern void dos_init(void);
 extern void dos_add_partition(int n, int sys);
 extern void dos_new_partition(void);
+extern void dos_write_table(void);
 
 #endif
Signature · 1 line
1.7.4.1
maintainer Applied
#3 lore raw
Quoted 4 lines · Davidlohr Bueso
 fdisk/fdisk.c         |   27 ++++-----------------------
 fdisk/fdiskdoslabel.c |   25 +++++++++++++++++++++++++
 fdisk/fdiskdoslabel.h |    1 +
 3 files changed, 30 insertions(+), 23 deletions(-)

Applied, thanks.

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