daemon: Define safe ADD_ARG macro for constructing arg lists on the stack.
[libguestfs.git] / daemon / btrfs.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <inttypes.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "daemon.h"
28 #include "actions.h"
29 #include "optgroups.h"
30
31 #define MAX_ARGS 64
32
33 int
34 optgroup_btrfs_available (void)
35 {
36   return prog_exists ("btrfs");
37 }
38
39 /* Takes optional arguments, consult optargs_bitmask. */
40 int
41 do_btrfs_filesystem_resize (const char *filesystem, int64_t size)
42 {
43   char *buf;
44   char *err;
45   int r;
46   const char *argv[MAX_ARGS];
47   size_t i = 0;
48   char size_str[32];
49
50   ADD_ARG (argv, i, "btrfs");
51   ADD_ARG (argv, i, "filesystem");
52   ADD_ARG (argv, i, "resize");
53
54   if (optargs_bitmask & GUESTFS_BTRFS_FILESYSTEM_RESIZE_SIZE_BITMASK) {
55     if (size <= 0) {
56       reply_with_error ("size is zero or negative");
57       return -1;
58     }
59
60     snprintf (size_str, sizeof size_str, "%" PRIi64, size);
61     ADD_ARG (argv, i, size_str);
62   }
63   else
64     ADD_ARG (argv, i, "max");
65
66   buf = sysroot_path (filesystem);
67   if (!buf) {
68     reply_with_error ("malloc");
69     return -1;
70   }
71
72   ADD_ARG (argv, i, buf);
73   ADD_ARG (argv, i, NULL);
74
75   r = commandv (NULL, &err, argv);
76   free (buf);
77
78   if (r == -1) {
79     reply_with_error ("%s: %s", filesystem, err);
80     free (err);
81     return -1;
82   }
83
84   free (err);
85   return 0;
86 }