Include string.h and libintl.h, as needed.
[libguestfs.git] / df / main.c
1 /* virt-df
2  * Copyright (C) 2010-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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <inttypes.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <locale.h>
29 #include <assert.h>
30 #include <libintl.h>
31
32 #ifdef HAVE_LIBVIRT
33 #include <libvirt/libvirt.h>
34 #include <libvirt/virterror.h>
35 #endif
36
37 #include "progname.h"
38
39 #include "guestfs.h"
40 #include "options.h"
41 #include "virt-df.h"
42
43 /* These globals are shared with options.c. */
44 guestfs_h *g;
45
46 int read_only = 1;
47 int live = 0;
48 int verbose = 0;
49 int keys_from_stdin = 0;
50 int echo_keys = 0;
51 const char *libvirt_uri = NULL;
52 int inspector = 0;
53
54 int csv = 0;                    /* --csv */
55 int human = 0;                  /* --human-readable|-h */
56 int inodes = 0;                 /* --inodes */
57 int one_per_guest = 0;          /* --one-per-guest */
58 int uuid = 0;                   /* --uuid */
59
60 static inline char *
61 bad_cast (char const *s)
62 {
63   return (char *) s;
64 }
65
66 static void __attribute__((noreturn))
67 usage (int status)
68 {
69   if (status != EXIT_SUCCESS)
70     fprintf (stderr, _("Try `%s --help' for more information.\n"),
71              program_name);
72   else {
73     fprintf (stdout,
74            _("%s: display free space on virtual filesystems\n"
75              "Copyright (C) 2010 Red Hat Inc.\n"
76              "Usage:\n"
77              "  %s [--options] -d domname\n"
78              "  %s [--options] -a disk.img [-a disk.img ...]\n"
79              "Options:\n"
80              "  -a|--add image       Add image\n"
81              "  -c|--connect uri     Specify libvirt URI for -d option\n"
82              "  --csv                Output as Comma-Separated Values\n"
83              "  -d|--domain guest    Add disks from libvirt guest\n"
84              "  --format[=raw|..]    Force disk format for -a option\n"
85              "  -h|--human-readable  Human-readable sizes in --long output\n"
86              "  --help               Display brief help\n"
87              "  -i|--inodes          Display inodes\n"
88              "  --one-per-guest      Separate appliance per guest\n"
89              "  --uuid               Add UUIDs to --long output\n"
90              "  -v|--verbose         Verbose messages\n"
91              "  -V|--version         Display version and exit\n"
92              "  -x                   Trace libguestfs API calls\n"
93              "For more information, see the manpage %s(1).\n"),
94              program_name, program_name, program_name,
95              program_name);
96   }
97   exit (status);
98 }
99
100 int
101 main (int argc, char *argv[])
102 {
103   /* Set global program name that is not polluted with libtool artifacts.  */
104   set_program_name (argv[0]);
105
106   setlocale (LC_ALL, "");
107   bindtextdomain (PACKAGE, LOCALEBASEDIR);
108   textdomain (PACKAGE);
109
110   enum { HELP_OPTION = CHAR_MAX + 1 };
111
112   static const char *options = "a:c:d:hivVx";
113   static const struct option long_options[] = {
114     { "add", 1, 0, 'a' },
115     { "connect", 1, 0, 'c' },
116     { "csv", 0, 0, 0 },
117     { "domain", 1, 0, 'd' },
118     { "format", 2, 0, 0 },
119     { "help", 0, 0, HELP_OPTION },
120     { "human-readable", 0, 0, 'h' },
121     { "inodes", 0, 0, 'i' },
122     { "one-per-guest", 0, 0, 0 },
123     { "uuid", 0, 0, 0 },
124     { "verbose", 0, 0, 'v' },
125     { "version", 0, 0, 'V' },
126     { 0, 0, 0, 0 }
127   };
128   struct drv *drvs = NULL;
129   struct drv *drv;
130   const char *format = NULL;
131   int c;
132   int option_index;
133
134   g = guestfs_create ();
135   if (g == NULL) {
136     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
137     exit (EXIT_FAILURE);
138   }
139
140   argv[0] = bad_cast (program_name);
141
142   for (;;) {
143     c = getopt_long (argc, argv, options, long_options, &option_index);
144     if (c == -1) break;
145
146     switch (c) {
147     case 0:                     /* options which are long only */
148       if (STREQ (long_options[option_index].name, "format")) {
149         if (!optarg || STREQ (optarg, ""))
150           format = NULL;
151         else
152           format = optarg;
153       } else if (STREQ (long_options[option_index].name, "csv")) {
154         csv = 1;
155       } else if (STREQ (long_options[option_index].name, "one-per-guest")) {
156         one_per_guest = 1;
157       } else if (STREQ (long_options[option_index].name, "uuid")) {
158         uuid = 1;
159       } else {
160         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
161                  program_name, long_options[option_index].name, option_index);
162         exit (EXIT_FAILURE);
163       }
164       break;
165
166     case 'a':
167       OPTION_a;
168       break;
169
170     case 'c':
171       OPTION_c;
172       break;
173
174     case 'd':
175       OPTION_d;
176       break;
177
178     case 'h':
179       human = 1;
180       break;
181
182     case 'i':
183       inodes = 1;
184       break;
185
186     case 'v':
187       OPTION_v;
188       break;
189
190     case 'V':
191       OPTION_V;
192       break;
193
194     case 'x':
195       OPTION_x;
196       break;
197
198     case HELP_OPTION:
199       usage (EXIT_SUCCESS);
200
201     default:
202       usage (EXIT_FAILURE);
203     }
204   }
205
206   /* Old-style syntax?  There were no -a or -d options in the old
207    * virt-df which is how we detect this.
208    */
209   if (drvs == NULL) {
210     while (optind < argc) {
211       if (strchr (argv[optind], '/') ||
212           access (argv[optind], F_OK) == 0) { /* simulate -a option */
213         drv = malloc (sizeof (struct drv));
214         if (!drv) {
215           perror ("malloc");
216           exit (EXIT_FAILURE);
217         }
218         drv->type = drv_a;
219         drv->a.filename = argv[optind];
220         drv->a.format = NULL;
221         drv->next = drvs;
222         drvs = drv;
223       } else {                  /* simulate -d option */
224         drv = malloc (sizeof (struct drv));
225         if (!drv) {
226           perror ("malloc");
227           exit (EXIT_FAILURE);
228         }
229         drv->type = drv_d;
230         drv->d.guest = argv[optind];
231         drv->next = drvs;
232         drvs = drv;
233       }
234
235       optind++;
236     }
237   }
238
239   /* These are really constants, but they have to be variables for the
240    * options parsing code.  Assert here that they have known-good
241    * values.
242    */
243   assert (read_only == 1);
244   assert (inspector == 0);
245   assert (live == 0);
246
247   /* Must be no extra arguments on the command line. */
248   if (optind != argc)
249     usage (EXIT_FAILURE);
250
251   /* -h and --csv doesn't make sense.  Spreadsheets will corrupt these
252    * fields.  (RHBZ#600977).
253    */
254   if (human && csv) {
255     fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
256              program_name);
257     exit (EXIT_FAILURE);
258   }
259
260   /* If the user didn't specify any drives, then we ask libvirt for
261    * the full list of guests and drives, which we add in batches.
262    */
263   if (drvs == NULL) {
264 #ifdef HAVE_LIBVIRT
265     get_domains_from_libvirt ();
266 #else
267     fprintf (stderr, _("%s: compiled without support for libvirt.\n"),
268              program_name);
269     exit (EXIT_FAILURE);
270 #endif
271   }
272   else {
273     const char *name;
274
275     /* Add domains/drives from the command line (for a single guest). */
276     add_drives (drvs, 'a');
277
278     if (guestfs_launch (g) == -1)
279       exit (EXIT_FAILURE);
280
281     print_title ();
282
283     /* Synthesize a display name. */
284     switch (drvs->type) {
285     case drv_a:
286       name = strrchr (drvs->a.filename, '/');
287       if (name == NULL)
288         name = drvs->a.filename;
289       else
290         name++; /* skip '/' character */
291       break;
292     case drv_d:
293       name = drvs->d.guest;
294       break;
295     case drv_N:
296     default:
297       abort ();
298     }
299
300     /* XXX regression: in the Perl version we cached the UUID from the
301      * libvirt domain handle so it was available to us here.  In this
302      * version the libvirt domain handle is hidden inside
303      * guestfs_add_domain so the UUID is not available easily for
304      * single '-d' command-line options.
305      */
306     (void) df_on_handle (name, NULL, NULL, 0);
307
308     /* Free up data structures, no longer needed after this point. */
309     free_drives (drvs);
310   }
311
312   guestfs_close (g);
313
314   exit (EXIT_SUCCESS);
315 }