On Sun, Dec 11, 2016 at 08:02:23PM +0200, Matteo Cafasso wrote:
Signed-off-by: Matteo Cafasso <noxdafox(a)gmail.com>
---
src/drives.c | 5 ++---
src/file.c | 22 +++++++++-------------
src/journal.c | 4 ++--
src/tsk.c | 17 ++---------------
4 files changed, 15 insertions(+), 33 deletions(-)
diff --git a/src/drives.c b/src/drives.c
index 1e3b0af..1e04f81 100644
--- a/src/drives.c
+++ b/src/drives.c
@@ -421,7 +421,8 @@ create_drive_dev_null (guestfs_h *g,
data->format = "raw";
}
- if (guestfs_int_lazy_make_tmpdir (g) == -1)
+ tmpfile = guestfs_int_make_temp_path (g, "devnull");
+ if (tmpfile == NULL)
return NULL;
/* Because we create a special file, there is no point forcing qemu
@@ -429,8 +430,6 @@ create_drive_dev_null (guestfs_h *g,
*/
data->readonly = false;
- tmpfile = safe_asprintf (g, "%s/devnull%d", g->tmpdir, ++g->unique);
-
if (guestfs_disk_create (g, tmpfile, "raw", 4096, -1) == -1)
return NULL;
diff --git a/src/file.c b/src/file.c
index d57c4e1..53b859d 100644
--- a/src/file.c
+++ b/src/file.c
@@ -86,11 +86,10 @@ guestfs_impl_read_file (guestfs_h *g, const char *path, size_t
*size_r)
char *ret = NULL;
struct stat statbuf;
- if (guestfs_int_lazy_make_tmpdir (g) == -1)
+ tmpfile = guestfs_int_make_temp_path (g, "cat");
+ if (tmpfile == NULL)
goto err;
- tmpfile = safe_asprintf (g, "%s/cat%d", g->tmpdir, ++g->unique);
-
if (guestfs_download (g, path, tmpfile) == -1)
goto err;
@@ -213,11 +212,10 @@ guestfs_impl_find (guestfs_h *g, const char *directory)
char **ret = NULL;
size_t i, count, size;
- if (guestfs_int_lazy_make_tmpdir (g) == -1)
+ tmpfile = guestfs_int_make_temp_path (g, "find");
+ if (tmpfile == NULL)
goto err;
- tmpfile = safe_asprintf (g, "%s/find%d", g->tmpdir, ++g->unique);
-
if (guestfs_find0 (g, directory, tmpfile) == -1)
goto err;
@@ -317,11 +315,10 @@ write_or_append (guestfs_h *g, const char *path,
(!append ? guestfs_internal_write : guestfs_internal_write_append)
(g, path, content, size);
- if (guestfs_int_lazy_make_tmpdir (g) == -1)
- goto err;
-
/* Write the content out to a temporary file. */
- tmpfile = safe_asprintf (g, "%s/write%d", g->tmpdir, ++g->unique);
+ tmpfile = guestfs_int_make_temp_path (g, "write");
+ if (tmpfile == NULL)
+ goto err;
fd = open (tmpfile, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0600);
if (fd == -1) {
@@ -515,11 +512,10 @@ guestfs_impl_ls (guestfs_h *g, const char *directory)
char **ret = NULL;
size_t i, count, size;
- if (guestfs_int_lazy_make_tmpdir (g) == -1)
+ tmpfile = guestfs_int_make_temp_path (g, "ls");
+ if (tmpfile == NULL)
goto err;
- tmpfile = safe_asprintf (g, "%s/ls%d", g->tmpdir, ++g->unique);
-
if (guestfs_ls0 (g, directory, tmpfile) == -1)
goto err;
diff --git a/src/journal.c b/src/journal.c
index 22b81de..f36e661 100644
--- a/src/journal.c
+++ b/src/journal.c
@@ -66,10 +66,10 @@ guestfs_impl_journal_get (guestfs_h *g)
size_t i, j, size;
uint64_t len;
- if (guestfs_int_lazy_make_tmpdir (g) == -1)
+ tmpfile = guestfs_int_make_temp_path (g, "journal");
+ if (tmpfile == NULL)
goto err;
- tmpfile = safe_asprintf (g, "%s/journal%d", g->tmpdir, ++g->unique);
if (guestfs_internal_journal_get (g, tmpfile) == -1)
goto err;
diff --git a/src/tsk.c b/src/tsk.c
index 1def9c9..da9ca8d 100644
--- a/src/tsk.c
+++ b/src/tsk.c
@@ -36,7 +36,6 @@
static struct guestfs_tsk_dirent_list *parse_dirent_file (guestfs_h *, const char *);
static int deserialise_dirent_list (guestfs_h *, FILE *, struct guestfs_tsk_dirent_list
*);
-static char *make_temp_file (guestfs_h *, const char *);
struct guestfs_tsk_dirent_list *
guestfs_impl_filesystem_walk (guestfs_h *g, const char *mountable)
@@ -44,7 +43,7 @@ guestfs_impl_filesystem_walk (guestfs_h *g, const char *mountable)
int ret = 0;
CLEANUP_UNLINK_FREE char *tmpfile = NULL;
- tmpfile = make_temp_file (g, "filesystem_walk");
+ tmpfile = guestfs_int_make_temp_path (g, "filesystem_walk");
if (tmpfile == NULL)
return NULL;
@@ -61,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 = make_temp_file (g, "find_inode");
+ tmpfile = guestfs_int_make_temp_path (g, "find_inode");
if (tmpfile == NULL)
return NULL;
@@ -142,15 +141,3 @@ deserialise_dirent_list (guestfs_h *g, FILE *fp,
return ret ? 0 : -1;
}
-
-static char *
-make_temp_file (guestfs_h *g, const char *name)
-{
- int ret = 0;
-
- ret = guestfs_int_lazy_make_tmpdir (g);
- if (ret < 0)
- return NULL;
-
- return safe_asprintf (g, "%s/%s%d", g->tmpdir, name, ++g->unique);
-}
--
2.10.2
ACK (both patches).
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
libguestfs lets you edit virtual machines. Supports shell scripting,
bindings from many languages.
http://libguestfs.org