diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2011-12-23 10:33:25 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2011-12-23 10:35:34 +0000 |
commit | 690ff694ca6db586c06ec484ea158261c156aa2f (patch) | |
tree | 0b2de2288deb1b6af1b51519da6cf8293ff9cfde /src/launch.c | |
parent | 486cafd4acf4945bbf3fea541eaa320f5c419406 (diff) | |
download | libguestfs-690ff694ca6db586c06ec484ea158261c156aa2f.tar.gz libguestfs-690ff694ca6db586c06ec484ea158261c156aa2f.tar.xz libguestfs-690ff694ca6db586c06ec484ea158261c156aa2f.zip |
lib: Add guestfs___remove_tmpdir helper function.
This function does 'rm -rf <dir>' for temporary directories, safely
working if '<dir>' contains shell meta-characters.
Replace existing code for removing directories with this.
Diffstat (limited to 'src/launch.c')
-rw-r--r-- | src/launch.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/launch.c b/src/launch.c index 9add092a..ca89b638 100644 --- a/src/launch.c +++ b/src/launch.c @@ -32,6 +32,8 @@ #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> @@ -1092,6 +1094,34 @@ guestfs___persistent_tmpdir (void) 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 |