From: Ben Hutchings <ben@decadent.org.uk>
Forwarded: http://lists.gnu.org/archive/html/bug-parted/2008-10/msg00005.html
Bug-Debian: http://bugs.debian.org/491797
Last-Update: 2010-02-02
Description: Avoid overwriting empty or hidden partitions
 It changes the DOS partition rewriting code to avoid overwriting
 empty or hidden primary partitions so long as their entries aren't
 reused.  This makes fatresize and similar front-ends safe to use in
 the presence of hidden partitions.

Index: b/libparted/labels/dos.c
===================================================================
--- a/libparted/labels/dos.c
+++ b/libparted/labels/dos.c
@@ -1185,13 +1185,18 @@
 	if (!table->mbr_signature)
 		table->mbr_signature = generate_random_id();
 
-	memset (table->partitions, 0, sizeof (table->partitions));
-	table->magic = PED_CPU_TO_LE16 (MSDOS_MAGIC);
+	if (table->magic != PED_CPU_TO_LE16 (MSDOS_MAGIC)) {
+		memset (table->partitions, 0, sizeof (table->partitions));
+		table->magic = PED_CPU_TO_LE16 (MSDOS_MAGIC);
+	}
 
 	for (i=1; i<=DOS_N_PRI_PARTITIONS; i++) {
 		part = ped_disk_get_partition (disk, i);
-		if (!part)
+		if (!part) {
+			if (table->partitions [i - 1].type != PARTITION_EMPTY)
+				memset (&table->partitions [i - 1], 0, sizeof (DosRawPartition));
 			continue;
+		}
 
 		if (!fill_raw_part (&table->partitions [i - 1], part, 0))
 			goto write_fail;
