[Libguestfs] [PATCH] Remove temporary directory containing arbitrary files.

Richard W.M. Jones rjones at redhat.com
Wed Apr 13 21:47:11 UTC 2011


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
New in Fedora 11: Fedora Windows cross-compiler. Compile Windows
programs, test, and build Windows installers. Over 70 libraries supprt'd
http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw
-------------- next part --------------
>From 70975981bed8e0c01b5966c10b507bb82086e5f8 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Wed, 13 Apr 2011 22:02:08 +0100
Subject: [PATCH 1/2] Remove temporary directory containing arbitrary files.

In preparation for caching inspection information in the temporary
directory (g->tmpdir), allow the temporary directory to contain
arbitrary files, and remove all of them when the handle is closed.

This just generalizes the previous method of cleaning up the tmpdir.
---
 src/guestfs.c |   49 ++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/src/guestfs.c b/src/guestfs.c
index 9c4f22f..206841e 100644
--- a/src/guestfs.c
+++ b/src/guestfs.c
@@ -72,6 +72,7 @@
 #include "guestfs_protocol.h"
 
 static void default_error_cb (guestfs_h *g, void *data, const char *msg);
+static void remove_tmpdir (guestfs_h *g);
 static void close_handles (void);
 
 gl_lock_define_initialized (static, handles_lock);
@@ -215,15 +216,8 @@ guestfs_close (guestfs_h *g)
   if (g->pid > 0) waitpid (g->pid, NULL, 0);
   if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
 
-  /* Remove tmpfiles. */
-  if (g->tmpdir) {
-    snprintf (filename, sizeof filename, "%s/guestfsd.sock", g->tmpdir);
-    unlink (filename);
-
-    rmdir (g->tmpdir);
-
-    free (g->tmpdir);
-  }
+  /* Remove whole temporary directory. */
+  remove_tmpdir (g);
 
   if (g->cmdline) {
     for (i = 0; i < g->cmdline_size; ++i)
@@ -255,6 +249,43 @@ guestfs_close (guestfs_h *g)
   free (g);
 }
 
+/* g->tmpdir can contain any files (but not subdirectories).  Remove
+ * those and the directory itself.  Note that errors in this function
+ * aren't really that important: if we end up not deleting temporary
+ * files it's only annoying.
+ */
+static void
+remove_tmpdir (guestfs_h *g)
+{
+  DIR *dir;
+  struct dirent *d;
+
+  if (!g->tmpdir)
+    return;
+
+  dir = opendir (g->tmpdir);
+  if (dir == NULL) {
+    perror (g->tmpdir);
+    return;
+  }
+
+  while ((d = readdir (dir)) != NULL) {
+    if (STRNEQ (d->d_name, ".") && STRNEQ (d->d_name, "..")) {
+      if (unlinkat (dirfd (dir), d->d_name, 0) == -1)
+        perror (d->d_name);
+    }
+  }
+
+  if (closedir (dir) == -1)
+    perror (g->tmpdir);
+
+  if (rmdir (g->tmpdir) == -1)
+    perror (g->tmpdir);
+
+  free (g->tmpdir);
+  g->tmpdir = NULL;
+}
+
 /* Close all open handles (called from atexit(3)). */
 static void
 close_handles (void)
-- 
1.7.4.1



More information about the Libguestfs mailing list