[Libguestfs] [PATCH] virt-ls: support drive letters on Windows

Maros Zatko mzatko at redhat.com
Tue Mar 17 14:33:09 UTC 2015


---
 cat/ls.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/cat/ls.c b/cat/ls.c
index 9161fb6..151c11d 100644
--- a/cat/ls.c
+++ b/cat/ls.c
@@ -37,6 +37,7 @@
 
 #include "options.h"
 #include "visit.h"
+#include "windows.h"
 
 /* Currently open libguestfs handle. */
 guestfs_h *g;
@@ -76,6 +77,8 @@ static void output_int64_uid (int64_t);
 static void output_string (const char *);
 static void output_string_link (const char *);
 
+static char *from_maybe_windows_path (const char *);
+
 static void __attribute__((noreturn))
 usage (int status)
 {
@@ -374,7 +377,7 @@ main (int argc, char *argv[])
   unsigned errors = 0;
 
   while (optind < argc) {
-    const char *dir = argv[optind];
+    const char *dir = from_maybe_windows_path(argv[optind]);
 
     switch (mode) {
     case 0:                     /* no -l or -R option */
@@ -401,6 +404,7 @@ main (int argc, char *argv[])
       abort ();                 /* can't happen */
     }
 
+    free (dir);
     optind++;
   }
 
@@ -409,6 +413,31 @@ main (int argc, char *argv[])
   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
+static char *
+from_maybe_windows_path (const char *dir)
+{
+  CLEANUP_FREE_STRING_LIST char **roots = NULL;
+  char *path = NULL;
+  char *root;
+
+  roots = guestfs_inspect_get_roots (g);
+  assert (roots);
+  assert (roots[0] != NULL);
+  assert (roots[1] == NULL);
+  root = roots[0];
+
+  if (is_windows (g, root)) {
+    path = windows_path (g, root, dir, 1 /* readonly */ );
+    if (path == NULL) {
+      guestfs_close (g);
+      exit (EXIT_FAILURE);
+    }
+    return path;
+  }
+
+  return safe_strdup (g, dir);
+}
+
 static int
 do_ls (const char *dir)
 {
-- 
1.9.3




More information about the Libguestfs mailing list