[Libguestfs] [PATCH 02/10] lib: Change guestfs___remove_tmpdir function to use command mini-library.

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


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

---
 src/appliance.c        | 20 ++++++++++----------
 src/filearch.c         |  2 +-
 src/guestfs-internal.h |  2 +-
 src/guestfs.c          |  2 +-
 src/launch.c           | 29 ++++++++++-------------------
 5 files changed, 23 insertions(+), 32 deletions(-)

diff --git a/src/appliance.c b/src/appliance.c
index ecbb7af..9db42cb 100644
--- a/src/appliance.c
+++ b/src/appliance.c
@@ -500,7 +500,7 @@ build_supermin_appliance (guestfs_h *g,
 
   int r = run_supermin_helper (g, supermin_path, tmpcd, len);
   if (r == -1) {
-    guestfs___remove_tmpdir (tmpcd);
+    guestfs___remove_tmpdir (g, tmpcd);
     return -1;
   }
 
@@ -519,7 +519,7 @@ build_supermin_appliance (guestfs_h *g,
   int fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0755);
   if (fd == -1) {
     perrorf (g, "open: %s", filename);
-    guestfs___remove_tmpdir (tmpcd);
+    guestfs___remove_tmpdir (g, tmpcd);
     return -1;
   }
   struct flock fl;
@@ -533,7 +533,7 @@ build_supermin_appliance (guestfs_h *g,
       goto again;
     perrorf (g, "fcntl: F_SETLKW: %s", filename);
     close (fd);
-    guestfs___remove_tmpdir (tmpcd);
+    guestfs___remove_tmpdir (g, tmpcd);
     return -1;
   }
 
@@ -545,7 +545,7 @@ build_supermin_appliance (guestfs_h *g,
   if (ftruncate (fd, clen) == -1) {
     perrorf (g, "ftruncate: %s", filename);
     close (fd);
-    guestfs___remove_tmpdir (tmpcd);
+    guestfs___remove_tmpdir (g, tmpcd);
     return -1;
   }
 
@@ -553,13 +553,13 @@ build_supermin_appliance (guestfs_h *g,
   if (rr == -1) {
     perrorf (g, "write: %s", filename);
     close (fd);
-    guestfs___remove_tmpdir (tmpcd);
+    guestfs___remove_tmpdir (g, tmpcd);
     return -1;
   }
   if ((size_t) rr != clen) {
     error (g, "partial write: %s", filename);
     close (fd);
-    guestfs___remove_tmpdir (tmpcd);
+    guestfs___remove_tmpdir (g, tmpcd);
     return -1;
   }
 
@@ -569,7 +569,7 @@ build_supermin_appliance (guestfs_h *g,
   if (rename (filename, filename2) == -1) {
     perrorf (g, "rename: %s %s", filename, filename2);
     close (fd);
-    guestfs___remove_tmpdir (tmpcd);
+    guestfs___remove_tmpdir (g, tmpcd);
     return -1;
   }
 
@@ -579,7 +579,7 @@ build_supermin_appliance (guestfs_h *g,
   if (rename (filename, filename2) == -1) {
     perrorf (g, "rename: %s %s", filename, filename2);
     close (fd);
-    guestfs___remove_tmpdir (tmpcd);
+    guestfs___remove_tmpdir (g, tmpcd);
     return -1;
   }
 
@@ -589,11 +589,11 @@ build_supermin_appliance (guestfs_h *g,
   if (rename (filename, filename2) == -1) {
     perrorf (g, "rename: %s %s", filename, filename2);
     close (fd);
-    guestfs___remove_tmpdir (tmpcd);
+    guestfs___remove_tmpdir (g, tmpcd);
     return -1;
   }
 
-  guestfs___remove_tmpdir (tmpcd);
+  guestfs___remove_tmpdir (g, tmpcd);
 
   /* Now finish off by linking to the cached appliance and returning it. */
   if (hard_link_to_cached_appliance (g, cachedir,
diff --git a/src/filearch.c b/src/filearch.c
index 0b5d37b..10c1d4a 100644
--- a/src/filearch.c
+++ b/src/filearch.c
@@ -214,7 +214,7 @@ cpio_arch (guestfs_h *g, const char *file, const char *path)
   error (g, "file_architecture: could not determine architecture of cpio archive");
 
  out:
-  guestfs___remove_tmpdir (dir);
+  guestfs___remove_tmpdir (g, dir);
 
   return ret;
 #undef dir_len
diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index cf810e1..2cfa35b 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -526,7 +526,7 @@ extern int guestfs___build_appliance (guestfs_h *g, char **kernel, char **initrd
 /* launch.c */
 extern const char *guestfs___persistent_tmpdir (void);
 extern int guestfs___lazy_make_tmpdir (guestfs_h *g);
-extern void guestfs___remove_tmpdir (const char *dir);
+extern void guestfs___remove_tmpdir (guestfs_h *g, const char *dir);
 extern int64_t guestfs___timeval_diff (const struct timeval *x, const struct timeval *y);
 extern void guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...);
 extern void guestfs___launch_send_progress (guestfs_h *g, int perdozen);
diff --git a/src/guestfs.c b/src/guestfs.c
index cc645fd..e755ffa 100644
--- a/src/guestfs.c
+++ b/src/guestfs.c
@@ -302,7 +302,7 @@ guestfs_close (guestfs_h *g)
 
   /* Remove whole temporary directory. */
   if (g->tmpdir)
-    guestfs___remove_tmpdir (g->tmpdir);
+    guestfs___remove_tmpdir (g, g->tmpdir);
 
   /* Test output file used by bindtests. */
   if (g->test_fp != NULL)
diff --git a/src/launch.c b/src/launch.c
index cc45a10..4681707 100644
--- a/src/launch.c
+++ b/src/launch.c
@@ -760,29 +760,20 @@ guestfs___lazy_make_tmpdir (guestfs_h *g)
 /* Recursively remove a temporary directory.  If removal fails, just
  * return (it's a temporary directory so it'll eventually be cleaned
  * up by a temp cleaner).  This is done using "rm -rf" because that's
- * simpler and safer, but we have to exec to ensure that paths don't
- * need to be quoted.
+ * simpler and safer.
  */
 void
-guestfs___remove_tmpdir (const char *dir)
+guestfs___remove_tmpdir (guestfs_h *g, const char *dir)
 {
-  pid_t pid = fork ();
+  struct command *cmd;
 
-  if (pid == -1) {
-    perror ("remove tmpdir: fork");
-    return;
-  }
-  if (pid == 0) {
-    execlp ("rm", "rm", "-rf", dir, NULL);
-    perror ("remove tmpdir: exec: rm");
-    _exit (EXIT_FAILURE);
-  }
-
-  /* Parent. */
-  if (waitpid (pid, NULL, 0) == -1) {
-    perror ("remove tmpdir: waitpid");
-    return;
-  }
+  cmd = guestfs___new_command (g);
+  guestfs___cmd_add_arg (cmd, "rm");
+  guestfs___cmd_add_arg (cmd, "-rf");
+  guestfs___cmd_add_arg (cmd, dir);
+  /* Ignore failures. */
+  guestfs___cmd_run (cmd);
+  guestfs___cmd_close (cmd);
 }
 
 int
-- 
1.7.11.4




More information about the Libguestfs mailing list