[Libguestfs] [PATCH 3/3] launch: don't add a drive twice

Wanlong Gao gaowanlong at cn.fujitsu.com
Mon Jan 9 07:22:43 UTC 2012


1. Change the g->path to restore a absolute path instead of the mixed.
2. Check that if the adding drive is duplicated with the added drive.

Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com>
---
 src/launch.c |   13 +++++++++++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/launch.c b/src/launch.c
index 2840767..86e6241 100644
--- a/src/launch.c
+++ b/src/launch.c
@@ -335,24 +335,33 @@ guestfs__add_drive_opts (guestfs_h *g, const char *filename,
     }
   }
 
+  char *abs_path = realpath (filename, NULL);
   struct drive **i = &(g->drives);
-  while (*i != NULL) i = &((*i)->next);
+  while (*i != NULL) {
+    if (STREQ((*i)->path, abs_path)) {
+      error (g, _("drive %s can't be added twice"), abs_path);
+      goto err_out;
+    }
+    i = &((*i)->next);
+  }
 
   *i = safe_malloc (g, sizeof (struct drive));
   (*i)->next = NULL;
-  (*i)->path = safe_strdup (g, filename);
+  (*i)->path = safe_strdup (g, abs_path);
   (*i)->readonly = readonly;
   (*i)->format = format;
   (*i)->iface = iface;
   (*i)->name = name;
   (*i)->use_cache_off = use_cache_off;
 
+  free (abs_path);
   return 0;
 
 err_out:
   free (format);
   free (iface);
   free (name);
+  free (abs_path);
   return -1;
 }
 
-- 
1.7.8




More information about the Libguestfs mailing list