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.4.3~6 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=7a999281d7174486f63c9f7ff924ea7688db1be8;p=libguestfs.git Factor out code for locating the temporary directory. This is just code movement. For stable-1.4 branch, cherry picked from commit a2d4a8bf073c3551d174bd6c406e0cf3d27fb560 and backported. --- diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index a8d1a34..9d76edb 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -144,6 +144,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 int guestfs___set_busy (guestfs_h *g); extern int guestfs___end_busy (guestfs_h *g); extern int guestfs___send (guestfs_h *g, int proc_nr, xdrproc_t xdrp, char *args); 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. */