diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-11-08 11:36:35 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-11-09 13:11:53 +0000 |
commit | 1efed122c07792f4c66a4083159cfacfb1893212 (patch) | |
tree | c751f26ec9d3c152905fdf34d48a2b79a0af5216 /src/tmpdirs.c | |
parent | e8d937bd739a70394ec72482a8cb1df3b0601dcc (diff) | |
download | libguestfs-1efed122c07792f4c66a4083159cfacfb1893212.tar.gz libguestfs-1efed122c07792f4c66a4083159cfacfb1893212.tar.xz libguestfs-1efed122c07792f4c66a4083159cfacfb1893212.zip |
lib: Rework temporary and cache directory code.
New APIs: set-tmpdir, get-tmpdir, set-cachedir, get-cachedir.
The current code has evolved over time and has a number of problems:
(a) A single environment variable ($TMPDIR) controls the
location of several directories.
(b) It's hard for the library user to predict which directory
libguestfs will use, unless the user simulates the same internal steps
that libguestfs performs.
This commit fixes these issues.
(a) Now three environment variables control the location of all small
temporary files, and the appliance cache:
For temporary files: $LIBGUESTFS_TMPDIR or $TMPDIR or /tmp.
For the appliance cache: $LIBGUESTFS_CACHEDIR or $TMPDIR or /var/tmp.
The user can also set these directories explicitly through API calls
(guestfs_set_tmpdir and guestfs_set_cachedir).
(b) The user can also retrieve the actual directories that libguestfs
will use, by calling guestfs_get_tmpdir and guestfs_get_cachedir.
These functions are also used internally.
This commit also:
- reworks the internal tmpdir code
- removes the internal (undocumented) guestfs_tmpdir call (replacing
it with calls to the documented guestfs_get_tmpdir API instead)
- changes the ./run script to set LIBGUESTFS_TMPDIR and
LIBGUESTFS_CACHEDIR
- adds a test
- fixes a few places like libguestfs-make-fixed-appliance which
depended on $TMPDIR
Diffstat (limited to 'src/tmpdirs.c')
-rw-r--r-- | src/tmpdirs.c | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/src/tmpdirs.c b/src/tmpdirs.c new file mode 100644 index 00000000..b7167a9d --- /dev/null +++ b/src/tmpdirs.c @@ -0,0 +1,119 @@ +/* libguestfs + * Copyright (C) 2012 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "guestfs.h" +#include "guestfs-internal.h" +#include "guestfs-internal-actions.h" + +int +guestfs__set_tmpdir (guestfs_h *g, const char *tmpdir) +{ + free (g->int_tmpdir); + g->int_tmpdir = tmpdir ? safe_strdup (g, tmpdir) : NULL; + return 0; +} + +/* Note this actually calculates the tmpdir, so it never returns NULL. */ +char * +guestfs__get_tmpdir (guestfs_h *g) +{ + const char *str; + + if (g->int_tmpdir) + str = g->int_tmpdir; + else if (g->env_tmpdir) + str = g->env_tmpdir; + else + str = "/tmp"; + + return safe_strdup (g, str); +} + +int +guestfs__set_cachedir (guestfs_h *g, const char *cachedir) +{ + free (g->int_cachedir); + g->int_cachedir = cachedir ? safe_strdup (g, cachedir) : NULL; + return 0; +} + +/* Note this actually calculates the cachedir, so it never returns NULL. */ +char * +guestfs__get_cachedir (guestfs_h *g) +{ + const char *str; + + if (g->int_cachedir) + str = g->int_cachedir; + else if (g->env_tmpdir) + str = g->env_tmpdir; + else + str = "/var/tmp"; + + return safe_strdup (g, str); +} + +/* 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. + */ +int +guestfs___lazy_make_tmpdir (guestfs_h *g) +{ + if (!g->tmpdir) { + TMP_TEMPLATE_ON_STACK (g, dir_template); + g->tmpdir = safe_strdup (g, dir_template); + if (mkdtemp (g->tmpdir) == NULL) { + perrorf (g, _("%s: cannot create temporary directory"), dir_template); + return -1; + } + } + return 0; +} + +/* Recursively remove a temporary directory. If removal fails, just + * return (it's a temporary directory so it'll eventually be cleaned + * up by a temp cleaner). This is done using "rm -rf" because that's + * simpler and safer. + */ +void +guestfs___recursive_remove_dir (guestfs_h *g, const char *dir) +{ + struct command *cmd; + + cmd = guestfs___new_command (g); + guestfs___cmd_add_arg (cmd, "rm"); + guestfs___cmd_add_arg (cmd, "-rf"); + guestfs___cmd_add_arg (cmd, dir); + /* Ignore failures. */ + guestfs___cmd_run (cmd); + guestfs___cmd_close (cmd); +} + +void +guestfs___remove_tmpdir (guestfs_h *g) +{ + if (g->tmpdir) + guestfs___recursive_remove_dir (g, g->tmpdir); +} |