return -1;
}
- rmdir (tmpcd);
+ guestfs___remove_tmpdir (tmpcd);
/* Now finish off by linking to the cached appliance and returning it. */
if (hard_link_to_cached_appliance (g, cachedir,
error (g, "file_architecture: could not determine architecture of cpio archive");
out:
- /* Free up the temporary directory. Note the directory name cannot
- * contain shell meta-characters because of the way it was
- * constructed above.
- */
- snprintf (cmd, cmd_len, "rm -rf %s", dir);
- ignore_value (system (cmd));
+ guestfs___remove_tmpdir (dir);
return ret;
#undef dir_len
extern void guestfs___trace (guestfs_h *g, const char *fs, ...)
__attribute__((format (printf,2,3)));
extern const char *guestfs___persistent_tmpdir (void);
+extern void guestfs___remove_tmpdir (const char *dir);
extern void guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...);
extern void guestfs___free_inspect_info (guestfs_h *g);
extern void guestfs___free_drives (struct drive **drives);
#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);
if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
/* Remove whole temporary directory. */
- remove_tmpdir (g);
+ guestfs___remove_tmpdir (g->tmpdir);
+ free (g->tmpdir);
if (g->cmdline) {
size_t i;
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)
#include <time.h>
#include <sys/stat.h>
#include <sys/select.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include <dirent.h>
#include <signal.h>
#include <assert.h>
return tmpdir;
}
+/* 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.
+ */
+void
+guestfs___remove_tmpdir (const char *dir)
+{
+ pid_t pid = fork ();
+
+ 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;
+ }
+}
+
/* Compute Y - X and return the result in milliseconds.
* Approximately the same as this code:
* http://www.mpp.mpg.de/~huber/util/timevaldiff.c