daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / daemon / ntfs.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009-2010 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_ntfs3g_available (void)
35 {
36   return prog_exists ("ntfs-3g.probe");
37 }
38
39 int
40 optgroup_ntfsprogs_available (void)
41 {
42   return prog_exists ("ntfsresize");
43 }
44
45 int
46 do_ntfs_3g_probe (int rw, const char *device)
47 {
48   char *err;
49   int r;
50   const char *rw_flag;
51
52   rw_flag = rw ? "-w" : "-r";
53
54   r = commandr (NULL, &err, "ntfs-3g.probe", rw_flag, device, NULL);
55   if (r == -1) {
56     reply_with_error ("%s: %s", device, err);
57     free (err);
58     return -1;
59   }
60
61   free (err);
62   return r;
63 }
64
65 /* Takes optional arguments, consult optargs_bitmask. */
66 int
67 do_ntfsresize_opts (const char *device, int64_t size, int force)
68 {
69   char *err;
70   int r;
71   const char *argv[MAX_ARGS];
72   size_t i = 0;
73   char size_str[32];
74
75   ADD_ARG (argv, i, "ntfsresize");
76   ADD_ARG (argv, i, "-P");
77
78   if (optargs_bitmask & GUESTFS_NTFSRESIZE_OPTS_SIZE_BITMASK) {
79     if (size <= 0) {
80       reply_with_error ("size is zero or negative");
81       return -1;
82     }
83
84     snprintf (size_str, sizeof size_str, "%" PRIi64, size);
85     ADD_ARG (argv, i, "--size");
86     ADD_ARG (argv, i, size_str);
87   }
88
89   if (optargs_bitmask & GUESTFS_NTFSRESIZE_OPTS_FORCE_BITMASK && force)
90     ADD_ARG (argv, i, "--force");
91
92   ADD_ARG (argv, i, device);
93   ADD_ARG (argv, i, NULL);
94
95   r = commandv (NULL, &err, argv);
96   if (r == -1) {
97     reply_with_error ("%s: %s", device, err);
98     free (err);
99     return -1;
100   }
101
102   free (err);
103   return 0;
104 }
105
106 int
107 do_ntfsresize (const char *device)
108 {
109   return do_ntfsresize_opts (device, 0, 0);
110 }
111
112 int
113 do_ntfsresize_size (const char *device, int64_t size)
114 {
115   optargs_bitmask = GUESTFS_NTFSRESIZE_OPTS_SIZE_BITMASK;
116   return do_ntfsresize_opts (device, size, 0);
117 }