2587e563254543de389262af4a4396339c977129
[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                   Echo each command before executing it\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   char *p, *file = NULL;
108   const char *format = NULL;
109   int c;
110   int option_index;
111   int next_prepared_drive = 1;
112
113   g = guestfs_create ();
114   if (g == NULL) {
115     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
116     exit (EXIT_FAILURE);
117   }
118
119   argv[0] = bad_cast (program_name);
120
121   for (;;) {
122     c = getopt_long (argc, argv, options, long_options, &option_index);
123     if (c == -1) break;
124
125     switch (c) {
126     case 0:                     /* options which are long only */
127       if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
128         keys_from_stdin = 1;
129       } else if (STREQ (long_options[option_index].name, "echo-keys")) {
130         echo_keys = 1;
131       } else if (STREQ (long_options[option_index].name, "format")) {
132         if (!optarg || STREQ (optarg, ""))
133           format = NULL;
134         else
135           format = optarg;
136       } else {
137         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
138                  program_name, long_options[option_index].name, option_index);
139         exit (EXIT_FAILURE);
140       }
141       break;
142
143     case 'a':
144       OPTION_a;
145       break;
146
147     case 'c':
148       OPTION_c;
149       break;
150
151     case 'd':
152       OPTION_d;
153       break;
154
155     case 'h':
156       usage (EXIT_SUCCESS);
157
158     case 'v':
159       OPTION_v;
160       break;
161
162     case 'V':
163       OPTION_V;
164       break;
165
166     case 'x':
167       OPTION_x;
168       break;
169
170     case HELP_OPTION:
171       usage (EXIT_SUCCESS);
172
173     default:
174       usage (EXIT_FAILURE);
175     }
176   }
177
178   /* Old-style syntax?  There were no -a or -d options in the old
179    * virt-cat which is how we detect this.
180    */
181   if (drvs == NULL) {
182     /* argc - 1 because last parameter is the single filename. */
183     while (optind < argc - 1) {
184       if (strchr (argv[optind], '/') ||
185           access (argv[optind], F_OK) == 0) { /* simulate -a option */
186         drv = malloc (sizeof (struct drv));
187         if (!drv) {
188           perror ("malloc");
189           exit (EXIT_FAILURE);
190         }
191         drv->type = drv_a;
192         drv->a.filename = argv[optind];
193         drv->a.format = NULL;
194         drv->next = drvs;
195         drvs = drv;
196       } else {                  /* simulate -d option */
197         drv = malloc (sizeof (struct drv));
198         if (!drv) {
199           perror ("malloc");
200           exit (EXIT_FAILURE);
201         }
202         drv->type = drv_d;
203         drv->d.guest = argv[optind];
204         drv->next = drvs;
205         drvs = drv;
206       }
207
208       optind++;
209     }
210   }
211
212   /* These are really constants, but they have to be variables for the
213    * options parsing code.  Assert here that they have known-good
214    * values.
215    */
216   assert (read_only == 1);
217   assert (inspector == 1);
218
219   /* User must specify at least one filename on the command line. */
220   if (optind >= argc || argc - optind < 1)
221     usage (EXIT_FAILURE);
222
223   /* User must have specified some drives. */
224   if (drvs == NULL)
225     usage (EXIT_FAILURE);
226
227   /* Add drives, inspect and mount.  Note that inspector is always true,
228    * and there is no -m option.
229    */
230   add_drives (drvs, 'a');
231
232   if (guestfs_launch (g) == -1)
233     exit (EXIT_FAILURE);
234
235   inspect_mount ();
236
237   /* Free up data structures, no longer needed after this point. */
238   free_drives (drvs);
239
240   unsigned errors = 0;
241
242   while (optind < argc) {
243     if (guestfs_download (g, argv[optind], "/dev/stdout") == -1)
244       errors++;
245     optind++;
246   }
247
248   guestfs_close (g);
249
250   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
251 }