summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-08-15 15:41:42 +0100
committerRichard W.M. Jones <rjones@redhat.com>2012-08-15 17:49:48 +0100
commit4b8ac437295d5420d3186a913bc3740b69a8d342 (patch)
tree88c766bdf32f2bd51747bb718b65cb3117c6493f
parent22607a97cdf115caabf05c3dc87c5f154a2e914c (diff)
downloadlibguestfs-4b8ac437295d5420d3186a913bc3740b69a8d342.tar.gz
libguestfs-4b8ac437295d5420d3186a913bc3740b69a8d342.tar.xz
libguestfs-4b8ac437295d5420d3186a913bc3740b69a8d342.zip
launch: Add utility function guestfs___lazy_make_tmpdir.
This lets us create g->tmpdir lazily earlier if needed. This commit is just code motion.
-rw-r--r--src/guestfs-internal.h1
-rw-r--r--src/launch.c28
2 files changed, 21 insertions, 8 deletions
diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index a279524b..2355f134 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -433,6 +433,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 int guestfs___lazy_make_tmpdir (guestfs_h *g);
extern void guestfs___remove_tmpdir (const char *dir);
extern int64_t guestfs___timeval_diff (const struct timeval *x, const struct timeval *y);
extern void guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...);
diff --git a/src/launch.c b/src/launch.c
index 44da00b2..8e1c328c 100644
--- a/src/launch.c
+++ b/src/launch.c
@@ -346,14 +346,8 @@ guestfs__launch (guestfs_h *g)
TRACE0 (launch_start);
/* Make the temporary directory. */
- if (!g->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;
- }
- }
+ if (guestfs___lazy_make_tmpdir (g) == -1)
+ return -1;
/* Allow anyone to read the temporary directory. The socket in this
* directory won't be readable but anyone can see it exists if they
@@ -474,6 +468,24 @@ guestfs___persistent_tmpdir (void)
return tmpdir;
}
+/* The g->tmpdir (per-handle temporary directory) is not created when
+ * the handle is created. Instead we create it lazily before the
+ * first time it is used, or during launch.
+ */
+int
+guestfs___lazy_make_tmpdir (guestfs_h *g)
+{
+ if (!g->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 0;
+}
+
/* 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