fish: copy-in, copy-out, edit, more commands can use win:... prefix.
authorRichard W.M. Jones <rjones@redhat.com>
Tue, 5 Apr 2011 18:44:16 +0000 (19:44 +0100)
committerRichard W.M. Jones <rjones@redhat.com>
Tue, 5 Apr 2011 19:29:05 +0000 (20:29 +0100)
fish/copy.c
fish/edit.c
fish/more.c

index 2aa7c0a..c1c1b47 100644 (file)
@@ -1,5 +1,5 @@
 /* guestfish - the filesystem interactive shell
 /* guestfish - the filesystem interactive shell
- * Copyright (C) 2010 Red Hat Inc.
+ * Copyright (C) 2010-2011 Red Hat Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -43,15 +43,24 @@ run_copy_in (const char *cmd, size_t argc, char *argv[])
   }
 
   /* Remote directory is always the last arg. */
   }
 
   /* Remote directory is always the last arg. */
-  const char *remote = argv[argc-1];
+  char *remote = argv[argc-1];
+
+  /* Allow win: prefix on remote. */
+  remote = win_prefix (remote);
+  if (remote == NULL)
+    return -1;
+
   int nr_locals = argc-1;
 
   int remote_is_dir = guestfs_is_dir (g, remote);
   int nr_locals = argc-1;
 
   int remote_is_dir = guestfs_is_dir (g, remote);
-  if (remote_is_dir == -1)
+  if (remote_is_dir == -1) {
+    free (remote);
     return -1;
     return -1;
+  }
 
   if (!remote_is_dir) {
     fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), remote);
 
   if (!remote_is_dir) {
     fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), remote);
+    free (remote);
     return -1;
   }
 
     return -1;
   }
 
@@ -59,8 +68,10 @@ run_copy_in (const char *cmd, size_t argc, char *argv[])
   int i;
   for (i = 0; i < nr_locals; ++i) {
     int fd = make_tar_from_local (argv[i]);
   int i;
   for (i = 0; i < nr_locals; ++i) {
     int fd = make_tar_from_local (argv[i]);
-    if (fd == -1)
+    if (fd == -1) {
+      free (remote);
       return -1;
       return -1;
+    }
 
     char fdbuf[64];
     snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
 
     char fdbuf[64];
     snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
@@ -75,15 +86,22 @@ run_copy_in (const char *cmd, size_t argc, char *argv[])
     int status;
     if (wait (&status) == -1) {
       perror ("wait (tar-from-local subprocess)");
     int status;
     if (wait (&status) == -1) {
       perror ("wait (tar-from-local subprocess)");
+      free (remote);
       return -1;
     }
       return -1;
     }
-    if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0))
+    if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
+      free (remote);
       return -1;
       return -1;
+    }
 
 
-    if (r == -1)
+    if (r == -1) {
+      free (remote);
       return -1;
       return -1;
+    }
   }
 
   }
 
+  free (remote);
+
   return 0;
 }
 
   return 0;
 }
 
@@ -199,64 +217,92 @@ run_copy_out (const char *cmd, size_t argc, char *argv[])
   /* Download each remote one at a time using tar-out. */
   int i, r;
   for (i = 0; i < nr_remotes; ++i) {
   /* Download each remote one at a time using tar-out. */
   int i, r;
   for (i = 0; i < nr_remotes; ++i) {
+    char *remote = argv[i];
+
+    /* Allow win:... prefix on remotes. */
+    remote = win_prefix (remote);
+    if (remote == NULL)
+      return -1;
+
     /* If the remote is a file, download it.  If it's a directory,
      * create the directory in local first before using tar-out.
      */
     /* If the remote is a file, download it.  If it's a directory,
      * create the directory in local first before using tar-out.
      */
-    r = guestfs_is_file (g, argv[i]);
-    if (r == -1)
+    r = guestfs_is_file (g, remote);
+    if (r == -1) {
+      free (remote);
       return -1;
       return -1;
+    }
     if (r == 1) {               /* is file */
       char buf[PATH_MAX];
       const char *basename;
     if (r == 1) {               /* is file */
       char buf[PATH_MAX];
       const char *basename;
-      if (split_path (buf, sizeof buf, argv[i], NULL, &basename) == -1)
+      if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
+        free (remote);
         return -1;
         return -1;
+      }
 
       char filename[PATH_MAX];
       snprintf (filename, sizeof filename, "%s/%s", local, basename);
 
       char filename[PATH_MAX];
       snprintf (filename, sizeof filename, "%s/%s", local, basename);
-      if (guestfs_download (g, argv[i], filename) == -1)
+      if (guestfs_download (g, remote, filename) == -1) {
+        free (remote);
         return -1;
         return -1;
+      }
     }
     else {                      /* not a regular file */
     }
     else {                      /* not a regular file */
-      r = guestfs_is_dir (g, argv[i]);
-      if (r == -1)
+      r = guestfs_is_dir (g, remote);
+      if (r == -1) {
+        free (remote);
         return -1;
         return -1;
+      }
 
       if (r == 0) {
         fprintf (stderr, _("copy-out: '%s' is not a file or directory\n"),
 
       if (r == 0) {
         fprintf (stderr, _("copy-out: '%s' is not a file or directory\n"),
-                 argv[i]);
+                 remote);
+        free (remote);
         return -1;
       }
 
       char buf[PATH_MAX];
       const char *basename;
         return -1;
       }
 
       char buf[PATH_MAX];
       const char *basename;
-      if (split_path (buf, sizeof buf, argv[i], NULL, &basename) == -1)
+      if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
+        free (remote);
         return -1;
         return -1;
+      }
 
       int fd = make_tar_output (local, basename);
 
       int fd = make_tar_output (local, basename);
-      if (fd == -1)
+      if (fd == -1) {
+        free (remote);
         return -1;
         return -1;
+      }
 
       char fdbuf[64];
       snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
 
 
       char fdbuf[64];
       snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
 
-      int r = guestfs_tar_out (g, argv[i], fdbuf);
+      int r = guestfs_tar_out (g, remote, fdbuf);
 
       if (close (fd) == -1) {
         perror ("close (tar-output subprocess)");
 
       if (close (fd) == -1) {
         perror ("close (tar-output subprocess)");
+        free (remote);
         r = -1;
       }
 
       int status;
       if (wait (&status) == -1) {
         perror ("wait (tar-output subprocess)");
         r = -1;
       }
 
       int status;
       if (wait (&status) == -1) {
         perror ("wait (tar-output subprocess)");
+        free (remote);
         return -1;
       }
         return -1;
       }
-      if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0))
+      if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
+        free (remote);
         return -1;
         return -1;
+      }
 
 
-      if (r == -1)
+      if (r == -1) {
+        free (remote);
         return -1;
         return -1;
+      }
     }
     }
+
+    free (remote);
   }
 
   return 0;
   }
 
   return 0;
index a9539ba..80ae44b 100644 (file)
@@ -1,5 +1,5 @@
 /* guestfish - the filesystem interactive shell
 /* guestfish - the filesystem interactive shell
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009-2011 Red Hat Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -37,6 +37,7 @@ run_edit (const char *cmd, size_t argc, char *argv[])
   TMP_TEMPLATE_ON_STACK (filename);
   char buf[256];
   const char *editor;
   TMP_TEMPLATE_ON_STACK (filename);
   char buf[256];
   const char *editor;
+  char *remotefilename;
   struct stat oldstat, newstat;
   int r, fd;
 
   struct stat oldstat, newstat;
   int r, fd;
 
@@ -56,24 +57,32 @@ run_edit (const char *cmd, size_t argc, char *argv[])
       editor = "vi"; /* could be cruel here and choose ed(1) */
   }
 
       editor = "vi"; /* could be cruel here and choose ed(1) */
   }
 
+  /* Handle 'win:...' prefix. */
+  remotefilename = win_prefix (argv[0]);
+  if (remotefilename == NULL)
+    return -1;
+
   /* Download the file and write it to a temporary. */
   fd = mkstemp (filename);
   if (fd == -1) {
     perror ("mkstemp");
   /* Download the file and write it to a temporary. */
   fd = mkstemp (filename);
   if (fd == -1) {
     perror ("mkstemp");
+    free (remotefilename);
     return -1;
   }
 
   snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
 
     return -1;
   }
 
   snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
 
-  if (guestfs_download (g, argv[0], buf) == -1) {
+  if (guestfs_download (g, remotefilename, buf) == -1) {
     close (fd);
     unlink (filename);
     close (fd);
     unlink (filename);
+    free (remotefilename);
     return -1;
   }
 
   if (close (fd) == -1) {
     perror (filename);
     unlink (filename);
     return -1;
   }
 
   if (close (fd) == -1) {
     perror (filename);
     unlink (filename);
+    free (remotefilename);
     return -1;
   }
 
     return -1;
   }
 
@@ -81,6 +90,7 @@ run_edit (const char *cmd, size_t argc, char *argv[])
   if (stat (filename, &oldstat) == -1) {
     perror (filename);
     unlink (filename);
   if (stat (filename, &oldstat) == -1) {
     perror (filename);
     unlink (filename);
+    free (remotefilename);
     return -1;
   }
 
     return -1;
   }
 
@@ -92,6 +102,7 @@ run_edit (const char *cmd, size_t argc, char *argv[])
   if (r != 0) {
     perror (buf);
     unlink (filename);
   if (r != 0) {
     perror (buf);
     unlink (filename);
+    free (remotefilename);
     return -1;
   }
 
     return -1;
   }
 
@@ -99,6 +110,7 @@ run_edit (const char *cmd, size_t argc, char *argv[])
   if (stat (filename, &newstat) == -1) {
     perror (filename);
     unlink (filename);
   if (stat (filename, &newstat) == -1) {
     perror (filename);
     unlink (filename);
+    free (remotefilename);
     return -1;
   }
 
     return -1;
   }
 
@@ -106,15 +118,18 @@ run_edit (const char *cmd, size_t argc, char *argv[])
   if (oldstat.st_ctime == newstat.st_ctime &&
       oldstat.st_size == newstat.st_size) {
     unlink (filename);
   if (oldstat.st_ctime == newstat.st_ctime &&
       oldstat.st_size == newstat.st_size) {
     unlink (filename);
+    free (remotefilename);
     return 0;
   }
 
   /* Write new content. */
     return 0;
   }
 
   /* Write new content. */
-  if (guestfs_upload (g, filename, argv[0]) == -1) {
+  if (guestfs_upload (g, filename, remotefilename) == -1) {
     unlink (filename);
     unlink (filename);
+    free (remotefilename);
     return -1;
   }
 
   unlink (filename);
     return -1;
   }
 
   unlink (filename);
+  free (remotefilename);
   return 0;
 }
   return 0;
 }
index 2065abb..01443ff 100644 (file)
@@ -1,5 +1,5 @@
 /* guestfish - the filesystem interactive shell
 /* guestfish - the filesystem interactive shell
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009-2011 Red Hat Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -32,6 +32,7 @@ run_more (const char *cmd, size_t argc, char *argv[])
 {
   TMP_TEMPLATE_ON_STACK (filename);
   char buf[256];
 {
   TMP_TEMPLATE_ON_STACK (filename);
   char buf[256];
+  char *remote;
   const char *pager;
   int r, fd;
 
   const char *pager;
   int r, fd;
 
@@ -49,24 +50,34 @@ run_more (const char *cmd, size_t argc, char *argv[])
       pager = "more";
   }
 
       pager = "more";
   }
 
+  remote = argv[0];
+
+  /* Allow win:... prefix on remote. */
+  remote = win_prefix (remote);
+  if (remote == NULL)
+    return -1;
+
   /* Download the file and write it to a temporary. */
   fd = mkstemp (filename);
   if (fd == -1) {
     perror ("mkstemp");
   /* Download the file and write it to a temporary. */
   fd = mkstemp (filename);
   if (fd == -1) {
     perror ("mkstemp");
+    free (remote);
     return -1;
   }
 
   snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
 
     return -1;
   }
 
   snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
 
-  if (guestfs_download (g, argv[0], buf) == -1) {
+  if (guestfs_download (g, remote, buf) == -1) {
     close (fd);
     unlink (filename);
     close (fd);
     unlink (filename);
+    free (remote);
     return -1;
   }
 
   if (close (fd) == -1) {
     perror (filename);
     unlink (filename);
     return -1;
   }
 
   if (close (fd) == -1) {
     perror (filename);
     unlink (filename);
+    free (remote);
     return -1;
   }
 
     return -1;
   }
 
@@ -78,8 +89,10 @@ run_more (const char *cmd, size_t argc, char *argv[])
   unlink (filename);
   if (r != 0) {
     perror (buf);
   unlink (filename);
   if (r != 0) {
     perror (buf);
+    free (remote);
     return -1;
   }
 
     return -1;
   }
 
+  free (remote);
   return 0;
 }
   return 0;
 }