From c9c0ac7d362fd19214c2c5e5bb7dcd9059950887 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 24 Sep 2010 18:54:37 +0100 Subject: [PATCH] Allow $TMPDIR to override most temporary directory uses. Be more consistent in allowing the user to override use of the temporary directory by specifying $TMPDIR. Also prefer P_tmpdir macro (defined in ) if that is defined, rather than hard-coding "/tmp" for the fallback location. --- fish/edit.c | 2 +- fish/fish.c | 2 +- fish/fish.h | 5 +++++ fish/hexedit.c | 2 +- fish/more.c | 2 +- generator/generator_c.ml | 1 + src/appliance.c | 4 ++-- src/guestfs-internal.h | 6 +++++- src/guestfs.h | 1 + src/inspect.c | 44 ++++++++++++++++++++++++++++---------------- src/launch.c | 10 +++++----- test-tool/test-tool.c | 10 +++++++--- 12 files changed, 58 insertions(+), 31 deletions(-) diff --git a/fish/edit.c b/fish/edit.c index 4c06e86..58b4a59 100644 --- a/fish/edit.c +++ b/fish/edit.c @@ -34,7 +34,7 @@ int run_edit (const char *cmd, int argc, char *argv[]) { - char filename[] = "/tmp/guestfishXXXXXX"; + TMP_TEMPLATE_ON_STACK (filename); char buf[256]; const char *editor; struct stat oldstat, newstat; diff --git a/fish/fish.c b/fish/fish.c index 4379845..6aadd56 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -1561,7 +1561,7 @@ file_in (const char *arg) static char * file_in_heredoc (const char *endmarker) { - static const char template[] = "/tmp/heredocXXXXXX"; + TMP_TEMPLATE_ON_STACK (template); file_in_tmpfile = strdup (template); if (file_in_tmpfile == NULL) { perror ("strdup"); diff --git a/fish/fish.h b/fish/fish.h index b941c49..2340357 100644 --- a/fish/fish.h +++ b/fish/fish.h @@ -47,6 +47,11 @@ #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0) #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0) +#define TMP_TEMPLATE_ON_STACK(var) \ + const char *ttos_tmpdir = guestfs_tmpdir (); \ + char var[strlen (ttos_tmpdir) + 32]; \ + sprintf (var, "%s/guestfishXXXXXX", ttos_tmpdir) \ + /* in fish.c */ extern guestfs_h *g; extern int read_only; diff --git a/fish/hexedit.c b/fish/hexedit.c index dfa1889..bd3efc8 100644 --- a/fish/hexedit.c +++ b/fish/hexedit.c @@ -100,7 +100,7 @@ run_hexedit (const char *cmd, int argc, char *argv[]) int r; struct stat oldstat, newstat; char buf[BUFSIZ]; - char tmp[] = "/tmp/guestfishXXXXXX"; + TMP_TEMPLATE_ON_STACK (tmp); int fd = mkstemp (tmp); if (fd == -1) { perror ("mkstemp"); diff --git a/fish/more.c b/fish/more.c index 27fc548..e8a6d05 100644 --- a/fish/more.c +++ b/fish/more.c @@ -30,7 +30,7 @@ int run_more (const char *cmd, int argc, char *argv[]) { - char filename[] = "/tmp/guestfishXXXXXX"; + TMP_TEMPLATE_ON_STACK (filename); char buf[256]; const char *pager; int r, fd; diff --git a/generator/generator_c.ml b/generator/generator_c.ml index 2b4a179..78ba5ec 100644 --- a/generator/generator_c.ml +++ b/generator/generator_c.ml @@ -759,6 +759,7 @@ and generate_linker_script () = "guestfs_safe_malloc"; "guestfs_safe_strdup"; "guestfs_safe_memdup"; + "guestfs_tmpdir"; ] in let functions = List.map (fun (name, _, _, _, _, _, _) -> "guestfs_" ^ name) diff --git a/src/appliance.c b/src/appliance.c index 2c1d7c9..163d770 100644 --- a/src/appliance.c +++ b/src/appliance.c @@ -235,7 +235,7 @@ check_for_cached_appliance (guestfs_h *g, const char *supermin_path, const char *checksum, char **kernel, char **initrd, char **appliance) { - const char *tmpdir = guestfs___tmpdir (); + const char *tmpdir = guestfs_tmpdir (); size_t len = strlen (tmpdir) + strlen (checksum) + 2; char cachedir[len]; @@ -329,7 +329,7 @@ build_supermin_appliance (guestfs_h *g, if (g->verbose) guestfs___print_timestamped_message (g, "begin building supermin appliance"); - const char *tmpdir = guestfs___tmpdir (); + const char *tmpdir = guestfs_tmpdir (); size_t cdlen = strlen (tmpdir) + strlen (checksum) + 2; char cachedir[cdlen]; snprintf (cachedir, cdlen, "%s/%s", tmpdir, checksum); diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index ed82fde..212006d 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -38,6 +38,11 @@ #define N_(str) str #endif +#define TMP_TEMPLATE_ON_STACK(var) \ + const char *ttos_tmpdir = guestfs_tmpdir (); \ + char var[strlen (ttos_tmpdir) + 32]; \ + sprintf (var, "%s/libguestfsXXXXXX", ttos_tmpdir) \ + #define UNIX_PATH_MAX 108 #ifndef MAX @@ -196,7 +201,6 @@ extern char *guestfs_safe_strdup (guestfs_h *g, const char *str); extern char *guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n); extern void *guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size); extern void guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...); -extern const char *guestfs___tmpdir (void); extern void guestfs___free_inspect_info (guestfs_h *g); extern int guestfs___set_busy (guestfs_h *g); extern int guestfs___end_busy (guestfs_h *g); diff --git a/src/guestfs.h b/src/guestfs.h index c23b9eb..911e956 100644 --- a/src/guestfs.h +++ b/src/guestfs.h @@ -89,6 +89,7 @@ extern void *guestfs_get_private (guestfs_h *g, const char *key); */ extern void *guestfs_safe_malloc (guestfs_h *g, size_t nbytes); extern void *guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s); +extern const char *guestfs_tmpdir (void); /* End of private functions. */ #ifdef __cplusplus diff --git a/src/inspect.c b/src/inspect.c index bce0fd6..1f4096d 100644 --- a/src/inspect.c +++ b/src/inspect.c @@ -227,6 +227,15 @@ is_regular_file (const char *filename) static char * cpio_arch (guestfs_h *g, const char *file, const char *path) { + TMP_TEMPLATE_ON_STACK (dir); +#define dir_len (strlen (dir)) +#define initrd_len (dir_len + 16) + char initrd[initrd_len]; +#define cmd_len (dir_len + 256) + char cmd[cmd_len]; +#define bin_len (dir_len + 32) + char bin[bin_len]; + char *ret = NULL; const char *method; @@ -237,20 +246,16 @@ cpio_arch (guestfs_h *g, const char *file, const char *path) else method = "cat"; - char dir[] = "/tmp/initrd.XXXXXX"; -#define dir_len (sizeof dir) if (mkdtemp (dir) == NULL) { perrorf (g, "mkdtemp"); goto out; } - char dir_initrd[dir_len + 16]; - snprintf (dir_initrd, dir_len + 16, "%s/initrd", dir); - if (guestfs_download (g, path, dir_initrd) == -1) + snprintf (initrd, initrd_len, "%s/initrd", dir); + if (guestfs_download (g, path, initrd) == -1) goto out; - char cmd[dir_len + 256]; - snprintf (cmd, dir_len + 256, + snprintf (cmd, cmd_len, "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1, dir, method); int r = system (cmd); @@ -259,11 +264,10 @@ cpio_arch (guestfs_h *g, const char *file, const char *path) goto out; } - char bin[dir_len + 32]; const char *bins[] = INITRD_BINARIES2; size_t i; for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) { - snprintf (bin, dir_len + 32, "%s/%s", dir, bins[i]); + snprintf (bin, bin_len, "%s/%s", dir, bins[i]); if (is_regular_file (bin)) { int flags = g->verbose ? MAGIC_DEBUG : 0; @@ -305,11 +309,14 @@ cpio_arch (guestfs_h *g, const char *file, const char *path) * contain shell meta-characters because of the way it was * constructed above. */ - snprintf (cmd, dir_len + 256, "rm -rf %s", dir); + snprintf (cmd, cmd_len, "rm -rf %s", dir); ignore_value (system (cmd)); return ret; #undef dir_len +#undef initrd_len +#undef cmd_len +#undef bin_len } char * @@ -983,6 +990,13 @@ static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs, const char *systemroot) { + TMP_TEMPLATE_ON_STACK (dir); +#define dir_len (strlen (dir)) +#define software_hive_len (dir_len + 16) + char software_hive[software_hive_len]; +#define cmd_len (dir_len + 16) + char cmd[cmd_len]; + size_t len = strlen (systemroot) + 64; char software[len]; snprintf (software, len, "%s/system32/config/software", systemroot); @@ -998,15 +1012,12 @@ check_windows_registry (guestfs_h *g, struct inspect_fs *fs, hive_h *h = NULL; hive_value_h *values = NULL; - char dir[] = "/tmp/winreg.XXXXXX"; -#define dir_len 18 if (mkdtemp (dir) == NULL) { perrorf (g, "mkdtemp"); goto out; } - char software_hive[dir_len + 16]; - snprintf (software_hive, dir_len + 16, "%s/software", dir); + snprintf (software_hive, software_hive_len, "%s/software", dir); if (guestfs_download (g, software_path, software_hive) == -1) goto out; @@ -1092,10 +1103,11 @@ check_windows_registry (guestfs_h *g, struct inspect_fs *fs, * contain shell meta-characters because of the way it was * constructed above. */ - char cmd[dir_len + 16]; - snprintf (cmd, dir_len + 16, "rm -rf %s", dir); + snprintf (cmd, cmd_len, "rm -rf %s", dir); ignore_value (system (cmd)); #undef dir_len +#undef software_hive_len +#undef cmd_len return ret; } diff --git a/src/launch.c b/src/launch.c index e0ad165..f9d8329 100644 --- a/src/launch.c +++ b/src/launch.c @@ -249,10 +249,7 @@ guestfs__launch (guestfs_h *g) /* Make the temporary directory. */ if (!g->tmpdir) { - const char *tmpdir = guestfs___tmpdir (); - char dir_template[strlen (tmpdir) + 32]; - sprintf (dir_template, "%s/libguestfsXXXXXX", tmpdir); - + TMP_TEMPLATE_ON_STACK (dir_template); g->tmpdir = safe_strdup (g, dir_template); if (mkdtemp (g->tmpdir) == NULL) { perrorf (g, _("%s: cannot create temporary directory"), dir_template); @@ -649,8 +646,11 @@ guestfs__launch (guestfs_h *g) return -1; } +/* Return the location of the tmpdir (eg. "/tmp") and allow users + * to override it at runtime using $TMPDIR. + */ const char * -guestfs___tmpdir (void) +guestfs_tmpdir (void) { const char *tmpdir; diff --git a/test-tool/test-tool.c b/test-tool/test-tool.c index 223cafc..7b49dbe 100644 --- a/test-tool/test-tool.c +++ b/test-tool/test-tool.c @@ -60,12 +60,16 @@ #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0) #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0) +#ifndef P_tmpdir +#define P_tmpdir "/tmp" +#endif + #define DEFAULT_TIMEOUT 120 static const char *helper = DEFAULT_HELPER; static int timeout = DEFAULT_TIMEOUT; -static char tmpf[] = "/tmp/libguestfs-test-tool-sda-XXXXXX"; -static char isof[] = "/tmp/libguestfs-test-tool-iso-XXXXXX"; +static char tmpf[] = P_tmpdir "/libguestfs-test-tool-sda-XXXXXX"; +static char isof[] = P_tmpdir "/libguestfs-test-tool-iso-XXXXXX"; static guestfs_h *g; static void preruncheck (void); @@ -269,7 +273,7 @@ main (int argc, char *argv[]) exit (EXIT_SUCCESS); } -static char qemuwrapper[] = "/tmp/libguestfs-test-tool-wrapper-XXXXXX"; +static char qemuwrapper[] = P_tmpdir "/libguestfs-test-tool-wrapper-XXXXXX"; static void cleanup_wrapper (void) -- 1.8.3.1