From: Richard Jones Date: Mon, 23 Aug 2010 11:53:11 +0000 (+0100) Subject: Factor out code for locating the temporary directory. X-Git-Tag: 1.5.4~9 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=a2d4a8bf073c3551d174bd6c406e0cf3d27fb560;p=libguestfs.git Factor out code for locating the temporary directory. This is just code movement. --- diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index 966eeeb..c8db829 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -197,6 +197,7 @@ extern void *guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes); 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 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/launch.c b/src/launch.c index 0d7a3f3..4a479bc 100644 --- a/src/launch.c +++ b/src/launch.c @@ -266,8 +266,6 @@ static const char *initrd_name = "initramfs." REPO "." host_cpu ".img"; int guestfs__launch (guestfs_h *g) { - const char *tmpdir; - char dir_template[PATH_MAX]; int r, pmore; size_t len; int wfd[2], rfd[2]; @@ -293,16 +291,11 @@ guestfs__launch (guestfs_h *g) gettimeofday (&g->launch_t, NULL); /* Make the temporary directory. */ -#ifdef P_tmpdir - tmpdir = P_tmpdir; -#else - tmpdir = "/tmp"; -#endif - - tmpdir = getenv ("TMPDIR") ? : tmpdir; - snprintf (dir_template, sizeof dir_template, "%s/libguestfsXXXXXX", tmpdir); - if (!g->tmpdir) { + const char *tmpdir = guestfs___tmpdir (); + char dir_template[strlen (tmpdir) + 32]; + sprintf (dir_template, "%s/libguestfsXXXXXX", tmpdir); + g->tmpdir = safe_strdup (g, dir_template); if (mkdtemp (g->tmpdir) == NULL) { perrorf (g, _("%s: cannot create temporary directory"), dir_template); @@ -877,6 +870,23 @@ guestfs__launch (guestfs_h *g) return -1; } +const char * +guestfs___tmpdir (void) +{ + const char *tmpdir; + +#ifdef P_tmpdir + tmpdir = P_tmpdir; +#else + tmpdir = "/tmp"; +#endif + + const char *t = getenv ("TMPDIR"); + if (t) tmpdir = t; + + return tmpdir; +} + /* This function is used to print the qemu command line before it gets * executed, when in verbose mode. */