summaryrefslogtreecommitdiffstats
path: root/src/tmpdirs.c
blob: b7167a9d5b0b89d8ec628bf7e028ff087dc6e728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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);
}