mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-22 07:03:38 +00:00
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.
This commit is contained in:
@@ -534,7 +534,7 @@ build_supermin_appliance (guestfs_h *g,
|
||||
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,
|
||||
|
||||
@@ -213,12 +213,7 @@ cpio_arch (guestfs_h *g, const char *file, const char *path)
|
||||
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
|
||||
|
||||
@@ -374,6 +374,7 @@ extern void guestfs___debug (guestfs_h *g, const char *fs, ...)
|
||||
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);
|
||||
|
||||
@@ -72,7 +72,6 @@
|
||||
#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);
|
||||
@@ -224,7 +223,8 @@ guestfs_close (guestfs_h *g)
|
||||
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;
|
||||
@@ -260,43 +260,6 @@ 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)
|
||||
|
||||
30
src/launch.c
30
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
|
||||
|
||||
Reference in New Issue
Block a user