[Libguestfs] [PATCH 2/3] lib: extract lazy tmpdir creation helper
Richard W.M. Jones
rjones at redhat.com
Tue Feb 2 19:48:15 UTC 2016
On Tue, Feb 02, 2016 at 03:27:40PM +0100, Pino Toscano wrote:
> Extract the bit of code for lazy creation of a temporary directory, so
> it can be used for more directories as well.
>
> This is just code motion, with no behaviour changes.
> ---
> src/tmpdirs.c | 28 +++++++++++++++++-----------
> 1 file changed, 17 insertions(+), 11 deletions(-)
>
> diff --git a/src/tmpdirs.c b/src/tmpdirs.c
> index 9154d8b..2ae9c74 100644
> --- a/src/tmpdirs.c
> +++ b/src/tmpdirs.c
> @@ -119,6 +119,22 @@ guestfs_impl_get_cachedir (guestfs_h *g)
> return safe_strdup (g, str);
> }
>
> +static int
> +lazy_make_tmpdir (guestfs_h *g, char *(*getdir) (guestfs_h *g), char **dest)
> +{
> + if (!*dest) {
> + CLEANUP_FREE char *tmpdir = getdir (g);
> + char *tmppath = safe_asprintf (g, "%s/libguestfsXXXXXX", tmpdir);
> + if (mkdtemp (tmppath) == NULL) {
> + perrorf (g, _("%s: cannot create temporary directory"), tmppath);
> + free (tmpdir);
> + return -1;
> + }
> + *dest = tmppath;
> + }
> + return 0;
> +}
> +
> /* 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.
> @@ -126,17 +142,7 @@ guestfs_impl_get_cachedir (guestfs_h *g)
> int
> guestfs_int_lazy_make_tmpdir (guestfs_h *g)
> {
> - if (!g->tmpdir) {
> - CLEANUP_FREE char *tmpdir = guestfs_get_tmpdir (g);
> - g->tmpdir = safe_asprintf (g, "%s/libguestfsXXXXXX", tmpdir);
> - if (mkdtemp (g->tmpdir) == NULL) {
> - perrorf (g, _("%s: cannot create temporary directory"), g->tmpdir);
> - free (g->tmpdir);
> - g->tmpdir = NULL;
> - return -1;
> - }
> - }
> - return 0;
> + return lazy_make_tmpdir (g, guestfs_get_tmpdir, &g->tmpdir);
> }
>
> /* Recursively remove a temporary directory. If removal fails, just
Looks like a simple refactor, so ACK.
Rich.
--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html
More information about the Libguestfs
mailing list