[parted-devel] [PATCH] Fix mkpart linux-swap bug: would use 0x83 rather than 0x82

Jim Meyering jim at meyering.net
Tue Aug 14 11:31:57 UTC 2007


As a follow-up to the thread on bug-parted,
  http://thread.gmane.org/gmane.comp.gnu.parted.bugs/9275
here's the patch I outlined there:

Fix mkpart linux-swap bug: would use 0x83 rather than 0x82

* libparted/labels/bsd.c (bsd_partition_set_system): Include "misc.h".
Use is_linux_swap to test whether the type string matches.
* libparted/labels/dasd.c (dasd_read, dasd_partition_set_system): Likewise.
* libparted/labels/dos.c (msdos_partition_set_system): Likewise.
* libparted/labels/mac.c (mac_partition_set_system): Likewise.
* libparted/labels/rdb.c (amiga_partition_set_system): Likewise.
* libparted/labels/sun.c (sun_partition_set_system): Likewise.
Based on a patch by Kenneth MacDonald, from
<http://lists.gnu.org/archive/html/bug-parted/2007-07/msg00012.html>.
* libparted/labels/misc.h (is_linux_swap): New function/file.
* libparted/labels/Makefile.am (liblabels_la_SOURCES): Add misc.h.
* tests/t2100-mkswap.sh: New file, test for the above fix.
* tests/Makefile.am (TESTS): Add t2100-mkswap.sh.

Signed-off-by: Jim Meyering <jim at meyering.net>
---
 libparted/labels/Makefile.am |   28 +++++++++++----------
 libparted/labels/bsd.c       |    4 ++-
 libparted/labels/dasd.c      |    6 +++-
 libparted/labels/dos.c       |    4 ++-
 libparted/labels/mac.c       |    4 ++-
 libparted/labels/misc.h      |   27 +++++++++++++++++++++
 libparted/labels/rdb.c       |    4 ++-
 libparted/labels/sun.c       |    4 ++-
 tests/Makefile.am            |    1 +
 tests/t2100-mkswap.sh        |   54 ++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 116 insertions(+), 20 deletions(-)
 create mode 100644 libparted/labels/misc.h
 create mode 100755 tests/t2100-mkswap.sh

diff --git a/libparted/labels/Makefile.am b/libparted/labels/Makefile.am
index f4d7d14..9acc296 100644
--- a/libparted/labels/Makefile.am
+++ b/libparted/labels/Makefile.am
@@ -12,19 +12,21 @@ endif
 partedincludedir      =	-I$(top_srcdir)/include
 noinst_LTLIBRARIES    =	liblabels.la
 
-liblabels_la_SOURCES  = rdb.c		\
-			bsd.c  		\
-			$(S390_SRCS)	\
-			efi_crc32.c	\
-			dos.c  		\
-			dvh.h		\
-			dvh.c  		\
-			gpt.c  		\
-			loop.c 		\
-			mac.c  		\
-			pc98.c 		\
-			sun.c		\
-			aix.c
+liblabels_la_SOURCES = \
+  $(S390_SRCS)	\
+  aix.c		\
+  bsd.c		\
+  dos.c		\
+  dvh.c		\
+  dvh.h		\
+  efi_crc32.c	\
+  gpt.c		\
+  loop.c	\
+  mac.c		\
+  misc.h	\
+  pc98.c	\
+  rdb.c		\
+  sun.c
 
 liblabels_la_LIBADD = $(OS_LIBS) $(INTLLIBS)
 
diff --git a/libparted/labels/bsd.c b/libparted/labels/bsd.c
index f25353b..7aac5d9 100644
--- a/libparted/labels/bsd.c
+++ b/libparted/labels/bsd.c
@@ -32,6 +32,8 @@
 #  define _(String) (String)
 #endif /* ENABLE_NLS */
 
+#include "misc.h"
+
 /* struct's & #define's stolen from libfdisk, which probably came from
  * Linux...
  */
@@ -445,7 +447,7 @@ bsd_partition_set_system (PedPartition* part, const PedFileSystemType* fs_type)
 
 	if (!fs_type)
 		bsd_data->type = 0x8;
-	else if (!strcmp (fs_type->name, "linux-swap"))
+	else if (is_linux_swap (fs_type->name))
 		bsd_data->type = 0x1;
 	else
 		bsd_data->type = 0x8;
diff --git a/libparted/labels/dasd.c b/libparted/labels/dasd.c
index bb3858f..f6c92f6 100644
--- a/libparted/labels/dasd.c
+++ b/libparted/labels/dasd.c
@@ -46,6 +46,8 @@
 #  define _(String) (String)
 #endif /* ENABLE_NLS */
 
+#include "misc.h"
+
 #define PARTITION_LINUX_SWAP 0x82
 #define PARTITION_LINUX 0x83
 #define PARTITION_LINUX_EXT 0x85
@@ -394,7 +396,7 @@ dasd_read (PedDisk* disk)
 
 		if (strncmp(PART_TYPE_SWAP, str, 6) == 0) {
 			fs = ped_file_system_probe(&part->geom);
-			if (strncmp(fs->name, "linux-swap", 10) == 0) {
+			if (is_linux_swap(fs->name)) {
 				dasd_data->system = PARTITION_LINUX_SWAP;
 				PDEBUG;
 			}
@@ -815,7 +817,7 @@ dasd_partition_set_system (PedPartition* part,
 	if (!fs_type) {
 		dasd_data->system = PARTITION_LINUX;
         PDEBUG;
-	} else if (!strcmp (fs_type->name, "linux-swap")) {
+	} else if (is_linux_swap (fs_type->name)) {
 		dasd_data->system = PARTITION_LINUX_SWAP;
         PDEBUG;
 	} else {
diff --git a/libparted/labels/dos.c b/libparted/labels/dos.c
index c686658..e513a05 100644
--- a/libparted/labels/dos.c
+++ b/libparted/labels/dos.c
@@ -32,6 +32,8 @@
 #  define _(String) (String)
 #endif /* ENABLE_NLS */
 
+#include "misc.h"
+
 /* this MBR boot code is loaded into 0000:7c00 by the BIOS.  See mbr.s for
  * the source, and how to build it
  */
@@ -1318,7 +1320,7 @@ msdos_partition_set_system (PedPartition* part,
 		dos_data->system |= dos_data->hidden ? PART_FLAG_HIDDEN : 0;
 	} else if (!strcmp (fs_type->name, "sun-ufs"))
 		dos_data->system = PARTITION_SUN_UFS;
-	else if (!strcmp (fs_type->name, "linux-swap"))
+	else if (is_linux_swap (fs_type->name))
 		dos_data->system = PARTITION_LINUX_SWAP;
 	else
 		dos_data->system = PARTITION_LINUX;
diff --git a/libparted/labels/mac.c b/libparted/labels/mac.c
index f014194..ae352aa 100644
--- a/libparted/labels/mac.c
+++ b/libparted/labels/mac.c
@@ -29,6 +29,8 @@
 #  define _(String) (String)
 #endif /* ENABLE_NLS */
 
+#include "misc.h"
+
 /* struct's hacked from Linux source:  fs/partitions/mac.h
  * I believe it was originally written by Paul Mackerras (from comments in
  * Quik source)
@@ -1187,7 +1189,7 @@ mac_partition_set_system (PedPartition* part, const PedFileSystemType* fs_type)
 
 	part->fs_type = fs_type;
 
-	if (fs_type && !strcmp (fs_type->name, "linux-swap"))
+	if (fs_type && is_linux_swap (fs_type->name))
 		ped_partition_set_flag (part, PED_PARTITION_SWAP, 1);
 
 	if (mac_data->is_boot) {
diff --git a/libparted/labels/misc.h b/libparted/labels/misc.h
new file mode 100644
index 0000000..a086e88
--- /dev/null
+++ b/libparted/labels/misc.h
@@ -0,0 +1,27 @@
+/* -*- Mode: c; indent-tabs-mode: nil -*-
+
+    libparted - a library for manipulating disk partitions
+    Copyright (C) 2007 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 3 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, see <http://www.gnu.org/licenses/>. */
+
+/* Return nonzero if FS_TYPE_NAME starts with "linux-swap".
+   This must match the NUL-terminated "linux-swap" as well
+   as "linux-swap(old)" and "linux-swap(new)".  */
+static inline int
+is_linux_swap (char const *fs_type_name)
+{
+  char const *prefix = "linux-swap";
+  return strncmp (fs_type_name, prefix, strlen (prefix)) == 0;
+}
diff --git a/libparted/labels/rdb.c b/libparted/labels/rdb.c
index 628b5a3..7d06b4c 100644
--- a/libparted/labels/rdb.c
+++ b/libparted/labels/rdb.c
@@ -37,6 +37,8 @@
 #  define _(String) (String)
 #endif /* ENABLE_NLS */
 
+#include "misc.h"
+
 /* String manipulation */
 static void _amiga_set_bstr (const char *cstr, char *bstr, int maxsize) {
 	int size = strlen (cstr);
@@ -901,7 +903,7 @@ amiga_partition_set_system (PedPartition* part,
 		partition->de_DosType = PED_CPU_TO_BE32(0x4c4e5800); /* 'LNX\0' */
 	else if (!strcmp (fs_type->name, "ext3"))
 		partition->de_DosType = PED_CPU_TO_BE32(0x45585403); /* 'EXT\3' */
-	else if (!strcmp (fs_type->name, "linux-swap"))
+	else if (is_linux_swap (fs_type->name))
 		partition->de_DosType = PED_CPU_TO_BE32(0x53575000); /* 'SWP\0' */
 	else if (!strcmp (fs_type->name, "fat16"))
 		partition->de_DosType = PED_CPU_TO_BE32(0x46415400); /* 'FAT\0' */
diff --git a/libparted/labels/sun.c b/libparted/labels/sun.c
index 02ee949..3abbf1e 100644
--- a/libparted/labels/sun.c
+++ b/libparted/labels/sun.c
@@ -32,6 +32,8 @@
 #  define _(String) (String)
 #endif /* ENABLE_NLS */
 
+#include "misc.h"
+
 /* Most of this came from util-linux's sun support, which was mostly done
    by Jakub Jelinek.  */
 
@@ -547,7 +549,7 @@ sun_partition_set_system (PedPartition* part, const PedFileSystemType* fs_type)
 
 	sun_data->type = 0x83;
 	if (fs_type) {
-		if (!strcmp (fs_type->name, "linux-swap"))
+		if (is_linux_swap (fs_type->name))
 			sun_data->type = 0x82;
 		else if (!strcmp (fs_type->name, "ufs"))
 			sun_data->type = 0x6;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0a42a8d..3a3020e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -5,6 +5,7 @@ TESTS = \
   t1100-busy-label.sh \
   t1500-small-ext2.sh \
   t2000-mkfs.sh \
+  t2100-mkswap.sh \
   t3000-constraints.sh \
   t3100-resize-ext2-partion.sh
 
diff --git a/tests/t2100-mkswap.sh b/tests/t2100-mkswap.sh
new file mode 100755
index 0000000..2bc1e99
--- /dev/null
+++ b/tests/t2100-mkswap.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+# Copyright (C) 2007 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 3 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, see <http://www.gnu.org/licenses/>.
+
+test_description='create linux-swap partitions'
+
+. ./init.sh
+
+######################################################################
+# When creating a partition of type linux-swap(new) in a DOS partition
+# table, ensure that the proper file system type (0x82) is used.
+# Some releases, e.g. parted-1.8.8 would mistakenly use 0x83.
+######################################################################
+N=1M
+dev=loop-file
+test_expect_success \
+    'create a file to simulate the underlying device' \
+    'dd if=/dev/null of=$dev bs=1 seek=$N 2> /dev/null'
+
+test_expect_success \
+    'label the test disk' \
+    'parted -s $dev mklabel msdos > out 2>&1'
+test_expect_success 'expect no output' '$compare out /dev/null'
+
+test_expect_success \
+    'create a partition' \
+    'parted -s $dev mkpart primary 0 1 > out 2>&1'
+test_expect_success 'expect no output' '$compare out /dev/null'
+
+test_expect_success \
+    'create a linux-swap file system' \
+    'parted -s $dev mkfs 1 "linux-swap(new)" > out 2>&1'
+test_expect_success 'expect no output' '$compare out /dev/null'
+
+# Extract the byte at offset 451.  It must be 0x82, not 0x83.
+test_expect_success \
+    'extract byte 451 (fs-type)' \
+    'od -t x1 -An -j450 -N1 $dev > out && echo " 82" > exp'
+test_expect_success 'expect it to be 82, not 83' '$compare out exp'
+
+test_done
-- 
1.5.3.rc4.67.gf9286



More information about the parted-devel mailing list