[Libguestfs] [PATCH 2/3] guestfish: Add win: prefix to use Windows paths.

Richard W.M. Jones rjones at redhat.com
Mon Oct 26 09:20:44 UTC 2009


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v
-------------- next part --------------
>From f94645be0ac751e3fdeafc916803a4626f0a6e91 Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones at redhat.com>
Date: Mon, 26 Oct 2009 09:04:42 +0000
Subject: [PATCH 2/3] guestfish: Add win: prefix to use Windows paths.

Add a win: prefix for path arguments in guestfish:

><fs> file win:c:\windows\system32\config\system.log
MS Windows registry file, NT/2000 or above
---
 fish/fish.c      |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 fish/fish.h      |    1 +
 guestfish.pod    |   17 +++++++++++++++++
 src/generator.ml |   16 ++++++++++++----
 4 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/fish/fish.c b/fish/fish.c
index 3300536..f699603 100644
--- a/fish/fish.c
+++ b/fish/fish.c
@@ -1295,3 +1295,51 @@ xwrite (int fd, const void *v_buf, size_t len)
 
   return 0;
 }
+
+/* Resolve the special "win:..." form for Windows-specific paths.
+ * This always returns a newly allocated string which is freed by the
+ * caller function in "cmds.c".
+ */
+char *
+resolve_win_path (const char *path)
+{
+  char *ret;
+  size_t i;
+
+  if (strncasecmp (path, "win:", 4) != 0) {
+    ret = strdup (path);
+    if (ret == NULL)
+      perror ("strdup");
+    return ret;
+  }
+
+  path += 4;
+
+  /* Drop drive letter, if it's "C:". */
+  if (strncasecmp (path, "c:", 2) == 0)
+    path += 2;
+
+  if (!*path) {
+    ret = strdup ("/");
+    if (ret == NULL)
+      perror ("strdup");
+    return ret;
+  }
+
+  ret = strdup (path);
+  if (ret == NULL) {
+    perror ("strdup");
+    return NULL;
+  }
+
+  /* Blindly convert any backslashes into forward slashes.  Is this good? */
+  for (i = 0; i < strlen (ret); ++i)
+    if (ret[i] == '\\')
+      ret[i] = '/';
+
+  char *t = guestfs_case_sensitive_path (g, ret);
+  free (ret);
+  ret = t;
+
+  return ret;
+}
diff --git a/fish/fish.h b/fish/fish.h
index 642c269..8c5dba1 100644
--- a/fish/fish.h
+++ b/fish/fish.h
@@ -46,6 +46,7 @@ extern int launch (guestfs_h *);
 extern int is_true (const char *str);
 extern char **parse_string_list (const char *str);
 extern int xwrite (int fd, const void *buf, size_t len);
+extern char *resolve_win_path (const char *path);
 
 /* in cmds.c (auto-generated) */
 extern void list_commands (void);
diff --git a/guestfish.pod b/guestfish.pod
index e4b2333..b635419 100644
--- a/guestfish.pod
+++ b/guestfish.pod
@@ -350,6 +350,23 @@ it, eg:
 
  echo "~"
 
+=head1 WINDOWS PATHS
+
+If a path is prefixed with C<win:> then you can use Windows-style
+paths (with some limitations).  The following commands are equivalent:
+
+ file /WINDOWS/system32/config/system.LOG
+
+ file win:/windows/system32/config/system.log
+
+ file win:\windows\system32\config\system.log
+
+ file WIN:C:\Windows\SYSTEM32\conFIG\SYSTEM.LOG
+
+This syntax implicitly calls C<case-sensitive-path> (q.v.) so it also
+handles case insensitivity like Windows would.  This only works in
+argument positions that expect a path.
+
 =head1 EXIT ON ERROR BEHAVIOUR
 
 By default, guestfish will ignore any errors when in interactive mode
diff --git a/src/generator.ml b/src/generator.ml
index 668002e..2b94601 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -6338,12 +6338,13 @@ and generate_fish_cmds () =
       );
       List.iter (
         function
-        | Pathname n
-        | Device n | Dev_or_Path n
+        | Device n
         | String n
         | OptString n
         | FileIn n
         | FileOut n -> pr "  const char *%s;\n" n
+        | Pathname n
+        | Dev_or_Path n -> pr "  char *%s;\n" n
         | StringList n | DeviceList n -> pr "  char **%s;\n" n
         | Bool n -> pr "  int %s;\n" n
         | Int n -> pr "  int %s;\n" n
@@ -6360,8 +6361,13 @@ and generate_fish_cmds () =
       iteri (
         fun i ->
           function
+          | Device name
+          | String name ->
+	      pr "  %s = argv[%d];\n" name i
           | Pathname name
-          | Device name | Dev_or_Path name | String name -> pr "  %s = argv[%d];\n" name i
+          | Dev_or_Path name ->
+	      pr "  %s = resolve_win_path (argv[%d]);\n" name i;
+	      pr "  if (%s == NULL) return -1;\n" name
           | OptString name ->
               pr "  %s = strcmp (argv[%d], \"\") != 0 ? argv[%d] : NULL;\n"
                 name i i
@@ -6390,9 +6396,11 @@ and generate_fish_cmds () =
 
       List.iter (
         function
-        | Pathname name | Device name | Dev_or_Path name | String name
+        | Device name | String name
         | OptString name | FileIn name | FileOut name | Bool name
         | Int name -> ()
+        | Pathname name | Dev_or_Path name ->
+            pr "  free (%s);\n" name
         | StringList name | DeviceList name ->
             pr "  free_strings (%s);\n" name
       ) (snd style);
-- 
1.6.5.rc2



More information about the Libguestfs mailing list