[Libguestfs] [PATCH 10/10] filearch: Use command mini-library to run external cpio command.

Richard W.M. Jones rjones at redhat.com
Thu Oct 18 21:14:11 UTC 2012


From: "Richard W.M. Jones" <rjones at redhat.com>

---
 src/filearch.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/filearch.c b/src/filearch.c
index 10c1d4a..93a697f 100644
--- a/src/filearch.c
+++ b/src/filearch.c
@@ -131,8 +131,7 @@ cpio_arch (guestfs_h *g, const char *file, const char *path)
 #define dir_len (strlen (dir))
 #define initrd_len (dir_len + 16)
   char initrd[initrd_len];
-#define cmd_len (dir_len + 256)
-  char cmd[cmd_len];
+  struct command *cmd = NULL;
 #define bin_len (dir_len + 32)
   char bin[bin_len];
   char *ret = NULL;
@@ -166,11 +165,16 @@ cpio_arch (guestfs_h *g, const char *file, const char *path)
   if (guestfs_download (g, path, initrd) == -1)
     goto out;
 
-  snprintf (cmd, cmd_len,
-            "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
-            dir, method);
-  r = system (cmd);
-  if (r == -1 || WEXITSTATUS (r) != 0) {
+  cmd = guestfs___new_command (g);
+  guestfs___cmd_add_string_unquoted (cmd, "cd ");
+  guestfs___cmd_add_string_quoted   (cmd, dir);
+  guestfs___cmd_add_string_unquoted (cmd, " && ");
+  guestfs___cmd_add_string_unquoted (cmd, method);
+  guestfs___cmd_add_string_unquoted (cmd,
+                                     " initrd | cpio --quiet -id "
+                                     INITRD_BINARIES1);
+  r = guestfs___cmd_run (cmd);
+  if (r == -1 || !WIFEXITED (r) || WEXITSTATUS (r) != 0) {
     perrorf (g, "cpio command failed");
     goto out;
   }
@@ -214,12 +218,14 @@ cpio_arch (guestfs_h *g, const char *file, const char *path)
   error (g, "file_architecture: could not determine architecture of cpio archive");
 
  out:
+  if (cmd)
+    guestfs___cmd_close (cmd);
+
   guestfs___remove_tmpdir (g, dir);
 
   return ret;
 #undef dir_len
 #undef initrd_len
-#undef cmd_len
 #undef bin_len
 }
 
-- 
1.7.11.4




More information about the Libguestfs mailing list