[Libguestfs] [PATCH v2 6/6] lib: Use guestfs_int_make_temp_path in a few more places.

Richard W.M. Jones rjones at redhat.com
Wed Sep 20 16:57:17 UTC 2017


There were various places in the library where we open coded making
temporary filenames.  This uses the utility function instead.
---
 lib/appliance-uefi.c |  4 +++-
 lib/command.c        |  6 ++----
 lib/drives.c         |  4 ++--
 lib/inspect-icon.c   | 12 +++++++++---
 lib/launch-direct.c  |  5 ++---
 lib/launch-libvirt.c |  5 ++---
 lib/launch-uml.c     |  5 ++---
 7 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/lib/appliance-uefi.c b/lib/appliance-uefi.c
index 986989e67..38dd2ef6d 100644
--- a/lib/appliance-uefi.c
+++ b/lib/appliance-uefi.c
@@ -74,7 +74,9 @@ guestfs_int_get_uefi (guestfs_h *g, char **code, char **vars, int *flags)
        * into the address space read-only as that triggers a different
        * path inside UEFI.
        */
-      varst = safe_asprintf (g, "%s/vars.fd.%d", g->tmpdir, ++g->unique);
+      varst = guestfs_int_make_temp_path (g, "vars", "fd");
+      if (!varst)
+        return -1;
       guestfs_int_cmd_add_arg (copycmd, "cp");
       guestfs_int_cmd_add_arg (copycmd, varsfile);
       guestfs_int_cmd_add_arg (copycmd, varst);
diff --git a/lib/command.c b/lib/command.c
index bc469de59..bfec76f19 100644
--- a/lib/command.c
+++ b/lib/command.c
@@ -812,11 +812,9 @@ guestfs_int_cmd_pipe_run (struct command *cmd, const char *mode)
    * we write them into a temporary file and provide a separate
    * function for the caller to read the error messages.
    */
-  if (guestfs_int_lazy_make_tmpdir (cmd->g) == -1)
+  cmd->error_file = guestfs_int_make_temp_path (cmd->g, "cmderr", "txt");
+  if (!cmd->error_file)
     goto error;
-
-  cmd->error_file =
-    safe_asprintf (cmd->g, "%s/cmderr.%d", cmd->g->tmpdir, ++cmd->g->unique);
   errfd = open (cmd->error_file,
                 O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC|O_CLOEXEC, 0600);
   if (errfd == -1) {
diff --git a/lib/drives.c b/lib/drives.c
index 117c8bf85..82ef30093 100644
--- a/lib/drives.c
+++ b/lib/drives.c
@@ -987,9 +987,9 @@ guestfs_impl_add_drive_scratch (guestfs_h *g, int64_t size,
    * because everything in g->tmpdir is 'rm -rf'd when the handle is
    * closed.
    */
-  if (guestfs_int_lazy_make_tmpdir (g) == -1)
+  filename = guestfs_int_make_temp_path (g, "scratch", "img");
+  if (!filename)
     return -1;
-  filename = safe_asprintf (g, "%s/scratch.%d", g->tmpdir, ++g->unique);
 
   /* Create a raw format temporary disk. */
   if (guestfs_disk_create (g, filename, "raw", size, -1) == -1)
diff --git a/lib/inspect-icon.c b/lib/inspect-icon.c
index 3e44b23eb..533e9fb4b 100644
--- a/lib/inspect-icon.c
+++ b/lib/inspect-icon.c
@@ -390,7 +390,9 @@ icon_cirros (guestfs_h *g, size_t *size_r)
     return NOT_FOUND;
 
   /* Use pbmtext to render it. */
-  pngfile = safe_asprintf (g, "%s/cirros.png", g->tmpdir);
+  pngfile = guestfs_int_make_temp_path (g, "cirros", "png");
+  if (!pngfile)
+    return NOT_FOUND;
 
   guestfs_int_cmd_add_string_unquoted (cmd, PBMTEXT " < ");
   guestfs_int_cmd_add_string_quoted   (cmd, local);
@@ -474,7 +476,9 @@ icon_windows_xp (guestfs_h *g, const char *systemroot, size_t *size_r)
   if (filename_downloaded == NULL)
     return NOT_FOUND;
 
-  pngfile = safe_asprintf (g, "%s/windows-xp-icon.png", g->tmpdir);
+  pngfile = guestfs_int_make_temp_path (g, "windows-xp-icon", "png");
+  if (!pngfile)
+    return NOT_FOUND;
 
   guestfs_int_cmd_add_string_unquoted (cmd, WRESTOOL " -x --type=2 --name=143 ");
   guestfs_int_cmd_add_string_quoted   (cmd, filename_downloaded);
@@ -542,7 +546,9 @@ icon_windows_7 (guestfs_h *g, const char *systemroot, size_t *size_r)
   if (filename_downloaded == NULL)
     return NOT_FOUND;
 
-  pngfile = safe_asprintf (g, "%s/windows-7-icon.png", g->tmpdir);
+  pngfile = guestfs_int_make_temp_path (g, "windows-7-icon", "png");
+  if (!pngfile)
+    return NOT_FOUND;
 
   guestfs_int_cmd_add_string_unquoted (cmd,
                                        WRESTOOL " -x --type=2 --name=6801 ");
diff --git a/lib/launch-direct.c b/lib/launch-direct.c
index 00cb25077..87ac121c7 100644
--- a/lib/launch-direct.c
+++ b/lib/launch-direct.c
@@ -86,11 +86,10 @@ create_cow_overlay_direct (guestfs_h *g, void *datav, struct drive *drv)
   if (!backing_drive)
     return NULL;
 
-  if (guestfs_int_lazy_make_tmpdir (g) == -1)
+  overlay = guestfs_int_make_temp_path (g, "overlay", "qcow2");
+  if (!overlay)
     return NULL;
 
-  overlay = safe_asprintf (g, "%s/overlay%d", g->tmpdir, ++g->unique);
-
   optargs.bitmask = GUESTFS_DISK_CREATE_BACKINGFILE_BITMASK;
   optargs.backingfile = backing_drive;
   if (drv->src.format) {
diff --git a/lib/launch-libvirt.c b/lib/launch-libvirt.c
index 98c947288..e5121799e 100644
--- a/lib/launch-libvirt.c
+++ b/lib/launch-libvirt.c
@@ -224,11 +224,10 @@ make_qcow2_overlay (guestfs_h *g, const char *backing_drive,
   char *overlay;
   struct guestfs_disk_create_argv optargs;
 
-  if (guestfs_int_lazy_make_tmpdir (g) == -1)
+  overlay = guestfs_int_make_temp_path (g, "overlay", "qcow2");
+  if (!overlay)
     return NULL;
 
-  overlay = safe_asprintf (g, "%s/overlay%d", g->tmpdir, ++g->unique);
-
   optargs.bitmask = GUESTFS_DISK_CREATE_BACKINGFILE_BITMASK;
   optargs.backingfile = backing_drive;
   if (format) {
diff --git a/lib/launch-uml.c b/lib/launch-uml.c
index 7c2b276d8..db68fac7b 100644
--- a/lib/launch-uml.c
+++ b/lib/launch-uml.c
@@ -54,11 +54,10 @@ make_cow_overlay (guestfs_h *g, const char *original)
   char *overlay;
   int r;
 
-  if (guestfs_int_lazy_make_tmpdir (g) == -1)
+  overlay = guestfs_int_make_temp_path (g, "overlay", "qcow2");
+  if (!overlay)
     return NULL;
 
-  overlay = safe_asprintf (g, "%s/overlay%d", g->tmpdir, g->unique++);
-
   guestfs_int_cmd_add_arg (cmd, "uml_mkcow");
   guestfs_int_cmd_add_arg (cmd, overlay);
   guestfs_int_cmd_add_arg (cmd, original);
-- 
2.13.2




More information about the Libguestfs mailing list