[Libguestfs] [PATCH V2 1/4] mount: add a macro to resolve path or device

Wanlong Gao gaowanlong at cn.fujitsu.com
Mon Jul 23 03:43:20 UTC 2012


Add a macro STRDUP_RESOLVE_DEVICE_OR_PATH to resolve path or device.

Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com>
---
 daemon/daemon.h | 16 ++++++++++++++++
 daemon/mount.c  | 13 ++-----------
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/daemon/daemon.h b/daemon/daemon.h
index 85eec45..f7d0c75 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -336,6 +336,22 @@ is_zero (const char *buffer, size_t size)
     }									\
   } while (0)
 
+/* Same as REQUIRE_ROOT_OR_RESOLVE_DEVICE but this strdup's the result. */
+#define STRDUP_RESOLVE_DEVICE_OR_PATH(path,buf,cancel_stmt,fail_stmt)   \
+  do {                                                                  \
+    int is_dev;                                                         \
+    is_dev = STREQLEN (path, "/dev/", 5);                               \
+    buf = is_dev ? strdup (path)                                        \
+                 : sysroot_path (path);                                 \
+    if (buf == NULL) {                                                  \
+      cancel_stmt;                                                      \
+      reply_with_perror ("malloc");                                     \
+      fail_stmt;                                                        \
+    }                                                                   \
+    if (is_dev)                                                         \
+      RESOLVE_DEVICE (buf, cancel_stmt, fail_stmt);                     \
+  } while (0)
+
 /* NB:
  * (1) You must match CHROOT_IN and CHROOT_OUT even along error paths.
  * (2) You must not change directory!  cwd must always be "/", otherwise
diff --git a/daemon/mount.c b/daemon/mount.c
index 0661eb8..1843165 100644
--- a/daemon/mount.c
+++ b/daemon/mount.c
@@ -192,18 +192,9 @@ do_umount (const char *pathordevice)
   int r;
   char *err;
   char *buf;
-  int is_dev;
 
-  is_dev = STREQLEN (pathordevice, "/dev/", 5);
-  buf = is_dev ? strdup (pathordevice)
-               : sysroot_path (pathordevice);
-  if (buf == NULL) {
-    reply_with_perror ("malloc");
-    return -1;
-  }
-
-  if (is_dev)
-    RESOLVE_DEVICE (buf, , { free (buf); return -1; });
+  STRDUP_RESOLVE_DEVICE_OR_PATH (pathordevice, buf, ,
+                                 { free (buf); return -1;});
 
   r = command (NULL, &err, "umount", buf, NULL);
   free (buf);
-- 
1.7.11.3.287.ge771946




More information about the Libguestfs mailing list