summaryrefslogtreecommitdiffstats
path: root/daemon/mktemp.c
blob: cacb48d5eb619e0430ebd02ad23c8cba2f478595 (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
/* libguestfs - the guestfsd daemon
 * Copyright (C) 2012 FUJITSU LTD.
 * Copyright (C) 2009 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; 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 "guestfs_protocol.h"
#include "daemon.h"
#include "actions.h"

char *
do_mkdtemp (const char *template)
{
  char *writable = strdup (template);
  if (writable == NULL) {
    reply_with_perror ("strdup");
    return NULL;
  }

  CHROOT_IN;
  char *r = mkdtemp (writable);
  CHROOT_OUT;

  if (r == NULL) {
    reply_with_perror ("%s", template);
    free (writable);
  }

  return r;
}

char *
do_mktemp (const char *template,
           const char *suffix)
{
  char *dest_name = NULL;
  size_t suffix_len = 0;
  int fd;
  size_t len;

  if (optargs_bitmask & GUESTFS_MKTEMP_SUFFIX_BITMASK) {
    if (suffix) {
      len = strlen (template);
      if (len == 0 || template[len - 1] != 'X') {
        reply_with_error ("template %s must end in X", template);
        return NULL;
      }

      /* Append suffix to template. */
      suffix_len = strlen (suffix);
      if (asprintf (&dest_name, "%s%s", template, suffix) == -1) {
        reply_with_perror ("asprintf");
        return NULL;
      }
    }
  }

  if (dest_name == NULL) {
    dest_name = strdup (template);
    if (dest_name == NULL) {
      reply_with_perror ("strdup");
      return NULL;
    }
  }

  CHROOT_IN;
  fd = mkstemps (dest_name, (int) suffix_len);
  CHROOT_OUT;

  if (fd == -1) {
    reply_with_perror ("%s", dest_name);
    free (dest_name);
    return NULL;
  }

  close (fd);

  return dest_name;
}