[PATCH] Rationalise linux-swap fs names, and add a "linux-swap" alias

Colin Watson cjwatson at ubuntu.com
Thu Jun 18 12:50:47 UTC 2009


* libparted/filesys.c (ped_file_system_alias_register,
ped_file_system_alias_unregister, ped_file_system_alias_get_next): New
functions.
(ped_file_system_type_get): Walk aliases as well.
* include/parted/filesys.h (struct _PedFileSystemAlias): New structure.
(ped_file_system_alias_register, ped_file_system_alias_unregister,
ped_file_system_alias_get_next): Add prototypes.
* parted/parted.c (_init_messages): Walk file system aliases as well as
types.
* parted/ui.c (init_fs_type_str): Likewise.

* libparted/fs/linux_swap/linux_swap.c (_swap_v1_type, _swap_v1_open,
_swap_v1_probe, _swap_v1_clobber, _swap_v1_ops): Rename to _swap_v0_type
etc. to match version number used in mkswap. Update all users.
(_swap_v2_type, _swap_v2_open, _swap_v2_probe, _swap_v2_clobber,
_swap_v2_ops): Rename to _swap_v1_type etc. to match version number used
in mkswap. Update all users.
(_swap_v0_type): Rename type from "linux-swap(old)" to "linux-swap(v0)".
(_swap_v1_type): Rename type from "linux-swap(new)" to "linux-swap(v1)".
(ped_file_system_linux_swap_init, ped_file_system_linux_swap_done):
Register/unregister a "linux-swap" alias for "linux-swap(v1)".
* libparted/labels/misc.h (is_linux_swap): Update comment.

* tests/t2100-mkswap.sh: Refer to "linux-swap(v1)" rather than
"linux-swap(new)". Test creation via the new alias.
---
 include/parted/filesys.h             |   20 ++++++++++
 libparted/filesys.c                  |   67 +++++++++++++++++++++++++++++++++-
 libparted/fs/linux_swap/linux_swap.c |   64 +++++++++++++++++---------------
 libparted/labels/misc.h              |    2 +-
 parted/parted.c                      |   37 +++++++++++++++++--
 parted/ui.c                          |    9 +++++
 tests/t2100-mkswap.sh                |   11 ++++--
 7 files changed, 172 insertions(+), 38 deletions(-)

diff --git a/include/parted/filesys.h b/include/parted/filesys.h
index 644b13a..22061a3 100644
--- a/include/parted/filesys.h
+++ b/include/parted/filesys.h
@@ -28,6 +28,7 @@
 
 typedef struct _PedFileSystem		PedFileSystem;
 typedef struct _PedFileSystemType	PedFileSystemType;
+typedef struct _PedFileSystemAlias	PedFileSystemAlias;
 typedef const struct _PedFileSystemOps	PedFileSystemOps;
 
 #include <parted/geom.h>
@@ -62,6 +63,17 @@ struct _PedFileSystemType {
 	PedFileSystemOps* const	ops;
 };
 
+/**
+ * Structure describing a file system alias. This is separate from
+ * PedFileSystemType because probing only looks through the list of types,
+ * and does not probe aliases separately.
+ */
+struct _PedFileSystemAlias {
+	PedFileSystemAlias*	next;
+	PedFileSystemType*	fs_type;
+	const char*		alias;
+};
+
 
 /**
  * Structure describing file system
@@ -79,10 +91,18 @@ struct _PedFileSystem {
 extern void ped_file_system_type_register (PedFileSystemType* type);
 extern void ped_file_system_type_unregister (PedFileSystemType* type);
 
+extern void ped_file_system_alias_register (PedFileSystemType* type,
+					    const char* alias);
+extern void ped_file_system_alias_unregister (PedFileSystemType* type,
+					      const char* alias);
+
 extern PedFileSystemType* ped_file_system_type_get (const char* name);
 extern PedFileSystemType*
 ped_file_system_type_get_next (const PedFileSystemType* fs_type);
 
+extern PedFileSystemAlias*
+ped_file_system_alias_get_next (const PedFileSystemAlias* fs_alias);
+
 extern PedFileSystemType* ped_file_system_probe (PedGeometry* geom);
 extern PedGeometry* ped_file_system_probe_specific (
 			const PedFileSystemType* fs_type,
diff --git a/libparted/filesys.c b/libparted/filesys.c
index ba48a79..d8505f1 100644
--- a/libparted/filesys.c
+++ b/libparted/filesys.c
@@ -41,6 +41,7 @@
 #define BUFFER_SIZE	4096		/* in sectors */
 
 static PedFileSystemType*	fs_types = NULL;
+static PedFileSystemAlias*	fs_aliases = NULL;
 
 void
 ped_file_system_type_register (PedFileSystemType* fs_type)
@@ -72,6 +73,47 @@ ped_file_system_type_unregister (PedFileSystemType* fs_type)
 		fs_types = fs_type->next;
 }
 
+void
+ped_file_system_alias_register (PedFileSystemType* fs_type, const char* alias)
+{
+	PedFileSystemAlias*	fs_alias;
+
+	PED_ASSERT (fs_type != NULL, return);
+	PED_ASSERT (alias != NULL, return);
+
+	fs_alias = ped_malloc (sizeof (PedFileSystemAlias));
+	if (!fs_alias)
+		return;
+
+	fs_alias->next = fs_aliases;
+	fs_alias->fs_type = fs_type;
+	fs_alias->alias = alias;
+	fs_aliases = fs_alias;
+}
+
+void
+ped_file_system_alias_unregister (PedFileSystemType* fs_type,
+				  const char* alias)
+{
+	PedFileSystemAlias*	walk;
+	PedFileSystemAlias*	last = NULL;
+
+	PED_ASSERT (fs_aliases != NULL, return);
+	PED_ASSERT (fs_type != NULL, return);
+	PED_ASSERT (alias != NULL, return);
+
+	for (walk = fs_aliases; walk; last = walk, walk = walk->next) {
+		if (walk->fs_type == fs_type && !strcmp (walk->alias, alias))
+			break;
+	}
+
+	PED_ASSERT (walk != NULL, return);
+	if (last)
+		last->next = walk->next;
+	else
+		fs_aliases = walk->next;
+}
+
 /**
  * Get a PedFileSystemType by its @p name.
  *
@@ -81,6 +123,7 @@ PedFileSystemType*
 ped_file_system_type_get (const char* name)
 {
 	PedFileSystemType*	walk;
+	PedFileSystemAlias*	alias_walk;
 
 	PED_ASSERT (name != NULL, return NULL);
 
@@ -88,7 +131,15 @@ ped_file_system_type_get (const char* name)
 		if (!strcasecmp (walk->name, name))
 			break;
 	}
-	return walk;
+	if (walk != NULL)
+		return walk;
+
+	for (alias_walk = fs_aliases; alias_walk != NULL;
+	     alias_walk = alias_walk->next) {
+		if (!strcasecmp (alias_walk->alias, name))
+			break;
+	}
+	return alias_walk->fs_type;
 }
 
 /**
@@ -106,6 +157,20 @@ ped_file_system_type_get_next (const PedFileSystemType* fs_type)
 }
 
 /**
+ * Get the next PedFileSystemAlias after @p fs_alias.
+ *
+ * @return @c NULL if @p fs_alias is the last item in the list.
+ */
+PedFileSystemAlias*
+ped_file_system_alias_get_next (const PedFileSystemAlias* fs_alias)
+{
+	if (fs_alias)
+		return fs_alias->next;
+	else
+		return fs_aliases;
+}
+
+/**
  * Attempt to find a file system and return the region it occupies.
  *
  * @param fs_type The file system type to probe for.
diff --git a/libparted/fs/linux_swap/linux_swap.c b/libparted/fs/linux_swap/linux_swap.c
index 62bb121..fd589b7 100644
--- a/libparted/fs/linux_swap/linux_swap.c
+++ b/libparted/fs/linux_swap/linux_swap.c
@@ -69,12 +69,12 @@ typedef struct {
 	unsigned int	max_bad_pages;
 } SwapSpecific;
 
+static PedFileSystemType _swap_v0_type;
 static PedFileSystemType _swap_v1_type;
-static PedFileSystemType _swap_v2_type;
 static PedFileSystemType _swap_swsusp_type;
 
+static PedFileSystem* _swap_v0_open (PedGeometry* geom);
 static PedFileSystem* _swap_v1_open (PedGeometry* geom);
-static PedFileSystem* _swap_v2_open (PedGeometry* geom);
 static PedFileSystem* _swap_swsusp_open (PedGeometry* geom);
 static int swap_close (PedFileSystem* fs);
 
@@ -89,11 +89,11 @@ _generic_swap_probe (PedGeometry* geom, int kind)
         switch (kind) {
         /* Check for old style swap partitions. */
                 case 0:
-                        fs = _swap_v1_open(geom);
+                        fs = _swap_v0_open(geom);
                         break;
         /* Check for new style swap partitions. */
                 case 1:
-                        fs = _swap_v2_open(geom);
+                        fs = _swap_v1_open(geom);
                         break;
         /* Check for swap partitions containing swsusp data. */
                 case -1:
@@ -135,11 +135,11 @@ _generic_swap_clobber (PedGeometry* geom, int kind)
         switch (kind) {
         /* Check for old style swap partitions. */
                 case 0:
-                        fs = _swap_v1_open(geom);
+                        fs = _swap_v0_open(geom);
                         break;
         /* Check for new style swap partitions. */
                 case 1:
-                        fs = _swap_v2_open(geom);
+                        fs = _swap_v1_open(geom);
                         break;
         /* Check for swap partitions containing swsusp data. */
                 case -1:
@@ -222,7 +222,7 @@ swap_alloc (PedGeometry* geom)
 	fs->geom = ped_geometry_duplicate (geom);
 	if (!fs->geom)
 		goto error_free_buffer;
-	fs->type = &_swap_v2_type;
+	fs->type = &_swap_v1_type;
 	return fs;
 
 error_free_buffer:
@@ -251,7 +251,7 @@ swap_free (PedFileSystem* fs)
 }
 
 static PedFileSystem*
-_swap_v1_open (PedGeometry* geom)
+_swap_v0_open (PedGeometry* geom)
 {
 	PedFileSystem*		fs;
 	SwapSpecific*		fs_info;
@@ -293,7 +293,7 @@ error:
 }
 
 static PedFileSystem*
-_swap_v2_open (PedGeometry* geom)
+_swap_v1_open (PedGeometry* geom)
 {
 	PedFileSystem*		fs;
 	SwapSpecific*		fs_info;
@@ -565,7 +565,7 @@ error:
 static PedFileSystem*
 swap_copy (const PedFileSystem* fs, PedGeometry* geom, PedTimer* timer)
 {
-	return ped_file_system_create (geom, &_swap_v2_type, timer);
+	return ped_file_system_create (geom, &_swap_v1_type, timer);
 }
 
 static int
@@ -602,12 +602,12 @@ swap_get_copy_constraint (const PedFileSystem* fs, const PedDevice* dev)
 #endif /* !DISCOVER_ONLY */
 
 static PedGeometry*
-_swap_v1_probe (PedGeometry* geom) {
+_swap_v0_probe (PedGeometry* geom) {
         return _generic_swap_probe (geom, 0);
 }
 
 static PedGeometry*
-_swap_v2_probe (PedGeometry* geom) {
+_swap_v1_probe (PedGeometry* geom) {
         return _generic_swap_probe (geom, 1);
 }
 
@@ -617,12 +617,12 @@ _swap_swsusp_probe (PedGeometry* geom) {
 }
 
 static int
-_swap_v1_clobber (PedGeometry* geom) {
+_swap_v0_clobber (PedGeometry* geom) {
         return _generic_swap_clobber (geom, 0);
 }
 
 static int
-_swap_v2_clobber (PedGeometry* geom) {
+_swap_v1_clobber (PedGeometry* geom) {
         return _generic_swap_clobber (geom, 1);
 }
 
@@ -631,11 +631,11 @@ _swap_swsusp_clobber (PedGeometry* geom) {
         return _generic_swap_clobber (geom, -1);
 }
 
-static PedFileSystemOps _swap_v1_ops = {
-	probe:		_swap_v1_probe,
+static PedFileSystemOps _swap_v0_ops = {
+	probe:		_swap_v0_probe,
 #ifndef DISCOVER_ONLY
-	clobber:	_swap_v1_clobber,
-	open:		_swap_v1_open,
+	clobber:	_swap_v0_clobber,
+	open:		_swap_v0_open,
 	create:		swap_create,
 	close:		swap_close,
 	check:		swap_check,
@@ -658,11 +658,11 @@ static PedFileSystemOps _swap_v1_ops = {
 #endif /* !DISCOVER_ONLY */
 };
 
-static PedFileSystemOps _swap_v2_ops = {
-	probe:		_swap_v2_probe,
+static PedFileSystemOps _swap_v1_ops = {
+	probe:		_swap_v1_probe,
 #ifndef DISCOVER_ONLY
-	clobber:	_swap_v2_clobber,
-	open:		_swap_v2_open,
+	clobber:	_swap_v1_clobber,
+	open:		_swap_v1_open,
 	create:		swap_create,
 	close:		swap_close,
 	check:		swap_check,
@@ -712,17 +712,17 @@ static PedFileSystemOps _swap_swsusp_ops = {
 #endif /* !DISCOVER_ONLY */
 };
 
-static PedFileSystemType _swap_v1_type = {
+static PedFileSystemType _swap_v0_type = {
 	next:	NULL,
-	ops:	&_swap_v1_ops,
-	name:	"linux-swap(old)",
+	ops:	&_swap_v0_ops,
+	name:	"linux-swap(v0)",
 	block_sizes: LINUXSWAP_BLOCK_SIZES
 };
 
-static PedFileSystemType _swap_v2_type = {
+static PedFileSystemType _swap_v1_type = {
 	next:	NULL,
-	ops:	&_swap_v2_ops,
-	name:	"linux-swap(new)",
+	ops:	&_swap_v1_ops,
+	name:	"linux-swap(v1)",
 	block_sizes: LINUXSWAP_BLOCK_SIZES
 };
 
@@ -736,15 +736,19 @@ static PedFileSystemType _swap_swsusp_type = {
 void
 ped_file_system_linux_swap_init ()
 {
+	ped_file_system_type_register (&_swap_v0_type);
 	ped_file_system_type_register (&_swap_v1_type);
-	ped_file_system_type_register (&_swap_v2_type);
 	ped_file_system_type_register (&_swap_swsusp_type);
+
+	ped_file_system_alias_register (&_swap_v1_type, "linux-swap");
 }
 
 void
 ped_file_system_linux_swap_done ()
 {
+	ped_file_system_alias_unregister (&_swap_v1_type, "linux-swap");
+
+	ped_file_system_type_unregister (&_swap_v0_type);
 	ped_file_system_type_unregister (&_swap_v1_type);
-	ped_file_system_type_unregister (&_swap_v2_type);
 	ped_file_system_type_unregister (&_swap_swsusp_type);
 }
diff --git a/libparted/labels/misc.h b/libparted/labels/misc.h
index a086e88..e69d518 100644
--- a/libparted/labels/misc.h
+++ b/libparted/labels/misc.h
@@ -18,7 +18,7 @@
 
 /* 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)".  */
+   as "linux-swap(v0)" and "linux-swap(v1)".  */
 static inline int
 is_linux_swap (char const *fs_type_name)
 {
diff --git a/parted/parted.c b/parted/parted.c
index e6364bf..17b2b6c 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -1983,6 +1983,7 @@ _init_messages ()
         StrList*                list;
         int                     first;
         PedFileSystemType*      fs_type;
+        PedFileSystemAlias*     fs_alias;
         PedDiskType*            disk_type;
         PedPartitionFlag        part_flag;
         PedUnit                 unit;
@@ -2039,7 +2040,7 @@ _init_messages ()
         label_type_msg = str_list_convert (list);
         str_list_destroy (list);
 
-/* mkfs - file system types */
+/* mkfs - file system types and aliases */
         list = str_list_create (_(fs_type_msg_start), NULL);
 
         first = 1;
@@ -2054,12 +2055,23 @@ _init_messages ()
                         str_list_append (list, ", ");
                 str_list_append (list, fs_type->name);
         }
+        for (fs_alias = ped_file_system_alias_get_next (NULL);
+             fs_alias; fs_alias = ped_file_system_alias_get_next (fs_alias)) {
+                if (fs_alias->fs_type->ops->create == NULL)
+                        continue;
+
+                if (first)
+                        first = 0;
+                else
+                        str_list_append (list, ", ");
+                str_list_append (list, fs_alias->alias);
+        }
         str_list_append (list, "\n");
 
         mkfs_fs_type_msg = str_list_convert (list);
         str_list_destroy (list);
 
-/* mkpart - file system types */
+/* mkpart - file system types and aliases */
         list = str_list_create (_(fs_type_msg_start), NULL);
 
         first = 1;
@@ -2071,12 +2083,20 @@ _init_messages ()
                         str_list_append (list, ", ");
                 str_list_append (list, fs_type->name);
         }
+        for (fs_alias = ped_file_system_alias_get_next (NULL);
+             fs_alias; fs_alias = ped_file_system_alias_get_next (fs_alias)) {
+                if (first)
+                        first = 0;
+                else
+                        str_list_append (list, ", ");
+                str_list_append (list, fs_alias->alias);
+        }
         str_list_append (list, "\n");
 
         mkpart_fs_type_msg = str_list_convert (list);
         str_list_destroy (list);
 
-/* resize - file system types */
+/* resize - file system types and aliases */
         list = str_list_create (_(resize_msg_start), NULL);
 
         first = 1;
@@ -2091,6 +2111,17 @@ _init_messages ()
                         str_list_append (list, ", ");
                 str_list_append (list, fs_type->name);
         }
+        for (fs_alias = ped_file_system_alias_get_next (NULL);
+             fs_alias; fs_alias = ped_file_system_alias_get_next (fs_alias)) {
+                if (fs_alias->fs_type->ops->resize == NULL)
+                        continue;
+
+                if (first)
+                        first = 0;
+                else
+                        str_list_append (list, ", ");
+                str_list_append (list, fs_alias->alias);
+        }
         str_list_append (list, "\n");
 
         resize_fs_type_msg = str_list_convert (list);
diff --git a/parted/ui.c b/parted/ui.c
index 7b2a248..f50536c 100644
--- a/parted/ui.c
+++ b/parted/ui.c
@@ -1347,6 +1347,7 @@ static int
 init_fs_type_str ()
 {
         PedFileSystemType*    walk;
+        PedFileSystemAlias*   alias_walk;
 
         fs_type_list = NULL;
 
@@ -1357,6 +1358,14 @@ init_fs_type_str ()
                 if (!fs_type_list)
                         return 0;
         }
+        for (alias_walk = ped_file_system_alias_get_next (NULL); alias_walk;
+             alias_walk = ped_file_system_alias_get_next (alias_walk))
+        {
+                fs_type_list = str_list_insert (fs_type_list,
+                                                alias_walk->alias);
+                if (!fs_type_list)
+                        return 0;
+        }
 
         return 1;
 }
diff --git a/tests/t2100-mkswap.sh b/tests/t2100-mkswap.sh
index cde1639..af8a15b 100755
--- a/tests/t2100-mkswap.sh
+++ b/tests/t2100-mkswap.sh
@@ -21,7 +21,7 @@ test_description='create linux-swap partitions'
 . $srcdir/test-lib.sh
 
 ######################################################################
-# When creating a partition of type linux-swap(new) in a DOS partition
+# When creating a partition of type linux-swap(v1) 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.
 ######################################################################
@@ -44,7 +44,7 @@ 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'
+    'parted -s $dev mkfs 1 "linux-swap(v1)" > 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.
@@ -69,7 +69,7 @@ test_expect_success 'expect no output' 'compare out /dev/null'
 
 test_expect_success \
     'create another linux-swap file system' \
-    'parted -s $dev2 mkfs 1 "linux-swap(new)" > out 2>&1'
+    'parted -s $dev2 mkfs 1 "linux-swap(v1)" > out 2>&1'
 test_expect_success 'expect no output' 'compare out /dev/null'
 
 # partition starts at offset 16384; swap UUID is 1036 bytes in
@@ -95,4 +95,9 @@ test_expect_success \
     'check preserves linux-swap UUID' \
     'compare uuid2 uuid2-new'
 
+test_expect_success \
+    'create a linux-swap file system via alias' \
+    'parted -s $dev mkfs 1 linux-swap > out 2>&1'
+test_expect_success 'expect no output' 'compare out /dev/null'
+
 test_done
-- 
1.6.3.1


--pW9qtIr+3H2OZPIR--



More information about the parted-devel mailing list