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;
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");
#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;
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");
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;
"guestfs_safe_malloc";
"guestfs_safe_strdup";
"guestfs_safe_memdup";
+ "guestfs_tmpdir";
] in
let functions =
List.map (fun (name, _, _, _, _, _, _) -> "guestfs_" ^ name)
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];
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);
#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
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);
*/
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
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;
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);
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;
* 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 *
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);
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;
* 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;
}
/* 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);
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;
#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);
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)