Blob Blame History Raw
From e728971a9fb62c0fd845a3a5a0a0653dd3df763a Mon Sep 17 00:00:00 2001
From: Vratislav Podzimek <vpodzime@redhat.com>
Date: Thu, 3 Nov 2016 12:33:30 +0100
Subject: [PATCH 1/2] Fix md_name_from_node to work with the "/dev/" prefix

Also update the tests to test this case.
---
 src/plugins/mdraid.c | 10 +++-------
 tests/md_test.py     |  1 +
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/src/plugins/mdraid.c b/src/plugins/mdraid.c
index c4780eb..d0144cf 100644
--- a/src/plugins/mdraid.c
+++ b/src/plugins/mdraid.c
@@ -1105,7 +1105,6 @@ gchar* bd_md_node_from_name (gchar *name, GError **error) {
  * Returns: @name of the MD RAID the device node belongs to or %NULL in case of error
  */
 gchar* bd_md_name_from_node (gchar *node, GError **error) {
-    gchar *node_path = NULL;
     glob_t glob_buf;
     gchar **path_p;
     gboolean found = FALSE;
@@ -1113,13 +1112,11 @@ gchar* bd_md_name_from_node (gchar *node, GError **error) {
     gchar *name = NULL;
     gchar *node_name = NULL;
 
-    if (!g_str_has_prefix (node, "/dev/"))
-        node_path = g_strdup_printf ("/dev/%s", node);
-    else
-        node_path = g_strdup (node);
+    /* get rid of the "/dev/" prefix (if any) */
+    if (g_str_has_prefix (node, "/dev/"))
+        node = node + 5;
 
     if (glob ("/dev/md/*", GLOB_NOSORT, NULL, &glob_buf) != 0) {
-        g_free (node_path);
         g_set_error (error, BD_MD_ERROR, BD_MD_ERROR_NO_MATCH,
                      "No name found for the node '%s'", node);
         return NULL;
@@ -1136,7 +1133,6 @@ gchar* bd_md_name_from_node (gchar *node, GError **error) {
         g_free (node_name);
     }
     globfree (&glob_buf);
-    g_free (node_path);
 
     if (!found)
         g_set_error (error, BD_MD_ERROR, BD_MD_ERROR_NO_MATCH,
diff --git a/tests/md_test.py b/tests/md_test.py
index 926d0c5..78edc48 100644
--- a/tests/md_test.py
+++ b/tests/md_test.py
@@ -376,6 +376,7 @@ class MDTestNameNodeBijection(MDTestCase):
 
         node = BlockDev.md_node_from_name("bd_test_md")
         self.assertEqual(BlockDev.md_name_from_node(node), "bd_test_md")
+        self.assertEqual(BlockDev.md_name_from_node("/dev/" + node), "bd_test_md")
 
         with self.assertRaises(GLib.GError):
             node = BlockDev.md_node_from_name("made_up_md")
-- 
2.7.4