470c70cc58368863dca1230a30a0f98cf3b7fb44
[libguestfs.git] / cat / virt-cat.c
1 /* virt-cat
2  * Copyright (C) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <inttypes.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <assert.h>
27
28 #include "progname.h"
29
30 #include "guestfs.h"
31 #include "options.h"
32
33 /* Currently open libguestfs handle. */
34 guestfs_h *g;
35
36 int read_only = 1;
37 int verbose = 0;
38 int keys_from_stdin = 0;
39 int echo_keys = 0;
40 const char *libvirt_uri = NULL;
41 int inspector = 1;
42
43 static inline char *
44 bad_cast (char const *s)
45 {
46   return (char *) s;
47 }
48
49 static void __attribute__((noreturn))
50 usage (int status)
51 {
52   if (status != EXIT_SUCCESS)
53     fprintf (stderr, _("Try `%s --help' for more information.\n"),
54              program_name);
55   else {
56     fprintf (stdout,
57            _("%s: display files in a virtual machine\n"
58              "Copyright (C) 2010 Red Hat Inc.\n"
59              "Usage:\n"
60              "  %s [--options] -d domname file [file ...]\n"
61              "  %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
62              "Options:\n"
63              "  -a|--add image       Add image\n"
64              "  -c|--connect uri     Specify libvirt URI for -d option\n"
65              "  -d|--domain guest    Add disks from libvirt guest\n"
66              "  --echo-keys          Don't turn off echo for passphrases\n"
67              "  --format[=raw|..]    Force disk format for -a option\n"
68              "  --help               Display brief help\n"
69              "  --keys-from-stdin    Read passphrases from stdin\n"
70              "  -v|--verbose         Verbose messages\n"
71              "  -V|--version         Display version and exit\n"
72              "  -x                   Trace libguestfs API calls\n"
73              "For more information, see the manpage %s(1).\n"),
74              program_name, program_name, program_name,
75              program_name);
76   }
77   exit (status);
78 }
79
80 int
81 main (int argc, char *argv[])
82 {
83   /* Set global program name that is not polluted with libtool artifacts.  */
84   set_program_name (argv[0]);
85
86   setlocale (LC_ALL, "");
87   bindtextdomain (PACKAGE, LOCALEBASEDIR);
88   textdomain (PACKAGE);
89
90   enum { HELP_OPTION = CHAR_MAX + 1 };
91
92   static const char *options = "a:c:d:vVx";
93   static const struct option long_options[] = {
94     { "add", 1, 0, 'a' },
95     { "connect", 1, 0, 'c' },
96     { "domain", 1, 0, 'd' },
97     { "echo-keys", 0, 0, 0 },
98     { "format", 2, 0, 0 },
99     { "help", 0, 0, HELP_OPTION },
100     { "keys-from-stdin", 0, 0, 0 },
101     { "verbose", 0, 0, 'v' },
102     { "version", 0, 0, 'V' },
103     { 0, 0, 0, 0 }
104   };
105   struct drv *drvs = NULL;
106   struct drv *drv;
107   const char *format = NULL;
108   int c;
109   int option_index;
110
111   g = guestfs_create ();
112   if (g == NULL) {
113     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
114     exit (EXIT_FAILURE);
115   }
116
117   argv[0] = bad_cast (program_name);
118
119   for (;;) {
120     c = getopt_long (argc, argv, options, long_options, &option_index);
121     if (c == -1) break;
122
123     switch (c) {
124     case 0:                     /* options which are long only */
125       if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
126         keys_from_stdin = 1;
127       } else if (STREQ (long_options[option_index].name, "echo-keys")) {
128         echo_keys = 1;
129       } else if (STREQ (long_options[option_index].name, "format")) {
130         if (!optarg || STREQ (optarg, ""))
131           format = NULL;
132         else
133           format = optarg;
134       } else {
135         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
136                  program_name, long_options[option_index].name, option_index);
137         exit (EXIT_FAILURE);
138       }
139       break;
140
141     case 'a':
142       OPTION_a;
143       break;
144
145     case 'c':
146       OPTION_c;
147       break;
148
149     case 'd':
150       OPTION_d;
151       break;
152
153     case 'h':
154       usage (EXIT_SUCCESS);
155
156     case 'v':
157       OPTION_v;
158       break;
159
160     case 'V':
161       OPTION_V;
162       break;
163
164     case 'x':
165       OPTION_x;
166       break;
167
168     case HELP_OPTION:
169       usage (EXIT_SUCCESS);
170
171     default:
172       usage (EXIT_FAILURE);
173     }
174   }
175
176   /* Old-style syntax?  There were no -a or -d options in the old
177    * virt-cat which is how we detect this.
178    */
179   if (drvs == NULL) {
180     /* argc - 1 because last parameter is the single filename. */
181     while (optind < argc - 1) {
182       if (strchr (argv[optind], '/') ||
183           access (argv[optind], F_OK) == 0) { /* simulate -a option */
184         drv = malloc (sizeof (struct drv));
185         if (!drv) {
186           perror ("malloc");
187           exit (EXIT_FAILURE);
188         }
189         drv->type = drv_a;
190         drv->a.filename = argv[optind];
191         drv->a.format = NULL;
192         drv->next = drvs;
193         drvs = drv;
194       } else {                  /* simulate -d option */
195         drv = malloc (sizeof (struct drv));
196         if (!drv) {
197           perror ("malloc");
198           exit (EXIT_FAILURE);
199         }
200         drv->type = drv_d;
201         drv->d.guest = argv[optind];
202         drv->next = drvs;
203         drvs = drv;
204       }
205
206       optind++;
207     }
208   }
209
210   /* These are really constants, but they have to be variables for the
211    * options parsing code.  Assert here that they have known-good
212    * values.
213    */
214   assert (read_only == 1);
215   assert (inspector == 1);
216
217   /* User must specify at least one filename on the command line. */
218   if (optind >= argc || argc - optind < 1)
219     usage (EXIT_FAILURE);
220
221   /* User must have specified some drives. */
222   if (drvs == NULL)
223     usage (EXIT_FAILURE);
224
225   /* Add drives, inspect and mount.  Note that inspector is always true,
226    * and there is no -m option.
227    */
228   add_drives (drvs, 'a');
229
230   if (guestfs_launch (g) == -1)
231     exit (EXIT_FAILURE);
232
233   inspect_mount ();
234
235   /* Free up data structures, no longer needed after this point. */
236   free_drives (drvs);
237
238   unsigned errors = 0;
239
240   while (optind < argc) {
241     if (guestfs_download (g, argv[optind], "/dev/stdout") == -1)
242       errors++;
243     optind++;
244   }
245
246   guestfs_close (g);
247
248   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
249 }