[Libguestfs] [PATCH 4/6] lib: Allow an extension to be specified in guestfs_int_make_temp_path.

Richard W.M. Jones rjones at redhat.com
Tue Sep 19 11:38:25 UTC 2017


In the function ‘guestfs_int_make_temp_path’, allow an optional
extension to be specified.  For example:

  r = guestfs_int_make_temp_path (g, "ls", "txt");

will create paths like ‘<tmpdir>/ls123.txt’.

NULL can also be passed for the extension, which means no extension.
---
 lib/drives.c           | 2 +-
 lib/file.c             | 8 ++++----
 lib/guestfs-internal.h | 2 +-
 lib/journal.c          | 2 +-
 lib/tmpdirs.c          | 8 ++++++--
 lib/tsk.c              | 4 ++--
 lib/yara.c             | 2 +-
 7 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/lib/drives.c b/lib/drives.c
index ec8bdbbe4..117c8bf85 100644
--- a/lib/drives.c
+++ b/lib/drives.c
@@ -421,7 +421,7 @@ create_drive_dev_null (guestfs_h *g,
     data->format = "raw";
   }
 
-  tmpfile = guestfs_int_make_temp_path (g, "devnull");
+  tmpfile = guestfs_int_make_temp_path (g, "devnull", "img");
   if (tmpfile == NULL)
     return NULL;
 
diff --git a/lib/file.c b/lib/file.c
index 73c983c2b..fa3189a71 100644
--- a/lib/file.c
+++ b/lib/file.c
@@ -87,7 +87,7 @@ guestfs_impl_read_file (guestfs_h *g, const char *path, size_t *size_r)
   char *ret = NULL;
   struct stat statbuf;
 
-  tmpfile = guestfs_int_make_temp_path (g, "cat");
+  tmpfile = guestfs_int_make_temp_path (g, "cat", NULL);
   if (tmpfile == NULL)
     goto err;
 
@@ -213,7 +213,7 @@ guestfs_impl_find (guestfs_h *g, const char *directory)
   char **ret = NULL;
   size_t i, count, size;
 
-  tmpfile = guestfs_int_make_temp_path (g, "find");
+  tmpfile = guestfs_int_make_temp_path (g, "find", "txt");
   if (tmpfile == NULL)
     goto err;
 
@@ -317,7 +317,7 @@ write_or_append (guestfs_h *g, const char *path,
       (g, path, content, size);
 
   /* Write the content out to a temporary file. */
-  tmpfile = guestfs_int_make_temp_path (g, "write");
+  tmpfile = guestfs_int_make_temp_path (g, "write", NULL);
   if (tmpfile == NULL)
     goto err;
 
@@ -513,7 +513,7 @@ guestfs_impl_ls (guestfs_h *g, const char *directory)
   char **ret = NULL;
   size_t i, count, size;
 
-  tmpfile = guestfs_int_make_temp_path (g, "ls");
+  tmpfile = guestfs_int_make_temp_path (g, "ls", "txt");
   if (tmpfile == NULL)
     goto err;
 
diff --git a/lib/guestfs-internal.h b/lib/guestfs-internal.h
index d24435011..f3379da7d 100644
--- a/lib/guestfs-internal.h
+++ b/lib/guestfs-internal.h
@@ -661,7 +661,7 @@ extern int guestfs_int_set_env_tmpdir (guestfs_h *g, const char *envname, const
 extern int guestfs_int_set_env_runtimedir (guestfs_h *g, const char *envname, const char *runtimedir);
 extern int guestfs_int_lazy_make_tmpdir (guestfs_h *g);
 extern int guestfs_int_lazy_make_sockdir (guestfs_h *g);
-extern char *guestfs_int_make_temp_path (guestfs_h *g, const char *name);
+extern char *guestfs_int_make_temp_path (guestfs_h *g, const char *name, const char *extension);
 extern char *guestfs_int_lazy_make_supermin_appliance_dir (guestfs_h *g);
 extern void guestfs_int_remove_tmpdir (guestfs_h *g);
 extern void guestfs_int_remove_sockdir (guestfs_h *g);
diff --git a/lib/journal.c b/lib/journal.c
index f36e66117..75a16c8bc 100644
--- a/lib/journal.c
+++ b/lib/journal.c
@@ -66,7 +66,7 @@ guestfs_impl_journal_get (guestfs_h *g)
   size_t i, j, size;
   uint64_t len;
 
-  tmpfile = guestfs_int_make_temp_path (g, "journal");
+  tmpfile = guestfs_int_make_temp_path (g, "journal", NULL);
   if (tmpfile == NULL)
     goto err;
 
diff --git a/lib/tmpdirs.c b/lib/tmpdirs.c
index 344475d1b..605bf90fb 100644
--- a/lib/tmpdirs.c
+++ b/lib/tmpdirs.c
@@ -228,7 +228,8 @@ guestfs_int_lazy_make_sockdir (guestfs_h *g)
  * Returns a unique path or NULL on error.
  */
 char *
-guestfs_int_make_temp_path (guestfs_h *g, const char *name)
+guestfs_int_make_temp_path (guestfs_h *g,
+                            const char *name, const char *extension)
 {
   int ret = 0;
 
@@ -236,7 +237,10 @@ guestfs_int_make_temp_path (guestfs_h *g, const char *name)
   if (ret < 0)
     return NULL;
 
-  return safe_asprintf (g, "%s/%s%d", g->tmpdir, name, ++g->unique);
+  return safe_asprintf (g, "%s/%s%d%s%s",
+                        g->tmpdir, name, ++g->unique,
+                        extension ? "." : "",
+                        extension ? extension : "");
 }
 
 /**
diff --git a/lib/tsk.c b/lib/tsk.c
index da9ca8dad..09e514bd2 100644
--- a/lib/tsk.c
+++ b/lib/tsk.c
@@ -43,7 +43,7 @@ guestfs_impl_filesystem_walk (guestfs_h *g, const char *mountable)
   int ret = 0;
   CLEANUP_UNLINK_FREE char *tmpfile = NULL;
 
-  tmpfile = guestfs_int_make_temp_path (g, "filesystem_walk");
+  tmpfile = guestfs_int_make_temp_path (g, "filesystem_walk", NULL);
   if (tmpfile == NULL)
     return NULL;
 
@@ -60,7 +60,7 @@ guestfs_impl_find_inode (guestfs_h *g, const char *mountable, int64_t inode)
   int ret = 0;
   CLEANUP_UNLINK_FREE char *tmpfile = NULL;
 
-  tmpfile = guestfs_int_make_temp_path (g, "find_inode");
+  tmpfile = guestfs_int_make_temp_path (g, "find_inode", NULL);
   if (tmpfile == NULL)
     return NULL;
 
diff --git a/lib/yara.c b/lib/yara.c
index 7e4683101..edced3be8 100644
--- a/lib/yara.c
+++ b/lib/yara.c
@@ -43,7 +43,7 @@ guestfs_impl_yara_scan (guestfs_h *g, const char *path)
   int r;
   CLEANUP_UNLINK_FREE char *tmpfile = NULL;
 
-  tmpfile = guestfs_int_make_temp_path (g, "yara_scan");
+  tmpfile = guestfs_int_make_temp_path (g, "yara_scan", NULL);
   if (tmpfile == NULL)
     return NULL;
 
-- 
2.13.2




More information about the Libguestfs mailing list