Matteo Cafasso
2016-Dec-11 18:02 UTC
[Libguestfs] [PATCH 0/2] generic function for temporary path generation
Cosmetic change as suggested in this previous patch: https://www.redhat.com/archives/libguestfs/2016-November/msg00111.html Matteo Cafasso (2): lib: generic function for temporary path generation lib: use guestfs_int_make_temp_path to generate temporary files src/drives.c | 5 ++--- src/file.c | 22 +++++++++------------- src/guestfs-internal.h | 1 + src/journal.c | 4 ++-- src/tmpdirs.c | 17 +++++++++++++++++ src/tsk.c | 17 ++--------------- 6 files changed, 33 insertions(+), 33 deletions(-) -- 2.10.2
Matteo Cafasso
2016-Dec-11 18:02 UTC
[Libguestfs] [PATCH 1/2] lib: generic function for temporary path generation
Signed-off-by: Matteo Cafasso <noxdafox@gmail.com> --- src/guestfs-internal.h | 1 + src/tmpdirs.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index fbbfb90..51d3512 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -813,6 +813,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_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/src/tmpdirs.c b/src/tmpdirs.c index 725f683..7d289a6 100644 --- a/src/tmpdirs.c +++ b/src/tmpdirs.c @@ -223,6 +223,23 @@ guestfs_int_lazy_make_sockdir (guestfs_h *g) } /** + * Generate unique temporary paths for temporary files. + * + * Returns a unique path or NULL on error. + */ +char * +guestfs_int_make_temp_path (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); +} + +/** * Create the supermin appliance directory under cachedir, if it does * not exist. * -- 2.10.2
Matteo Cafasso
2016-Dec-11 18:02 UTC
[Libguestfs] [PATCH 2/2] lib: use guestfs_int_make_temp_path to generate temporary files
Signed-off-by: Matteo Cafasso <noxdafox@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
Richard W.M. Jones
2016-Dec-11 19:28 UTC
Re: [Libguestfs] [PATCH 2/2] lib: use guestfs_int_make_temp_path to generate temporary files
On Sun, Dec 11, 2016 at 08:02:23PM +0200, Matteo Cafasso wrote:> Signed-off-by: Matteo Cafasso <noxdafox@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.2ACK (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
Reasonably Related Threads
- [PATCH 4/4] TSK: small refactoring
- [PATCH 6/6] lib: Use guestfs_int_make_temp_path in a few more places.
- Re: [PATCH 6/6] lib: Use guestfs_int_make_temp_path in a few more places.
- [PATCH v3 4/5] appliance: Added filesystem_walk command
- Re: [PATCH v2 4/5] appliance: Added filesystem_walk command