regressions: Enable both tests for bug 576879 (not fixed).
[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 <locale.h>
27 #include <assert.h>
28
29 #include "progname.h"
30
31 #include "guestfs.h"
32 #include "options.h"
33
34 /* Currently open libguestfs handle. */
35 guestfs_h *g;
36
37 int read_only = 1;
38 int live = 0;
39 int verbose = 0;
40 int keys_from_stdin = 0;
41 int echo_keys = 0;
42 const char *libvirt_uri = NULL;
43 int inspector = 1;
44
45 static inline char *
46 bad_cast (char const *s)
47 {
48   return (char *) s;
49 }
50
51 static void __attribute__((noreturn))
52 usage (int status)
53 {
54   if (status != EXIT_SUCCESS)
55     fprintf (stderr, _("Try `%s --help' for more information.\n"),
56              program_name);
57   else {
58     fprintf (stdout,
59            _("%s: display files in a virtual machine\n"
60              "Copyright (C) 2010 Red Hat Inc.\n"
61              "Usage:\n"
62              "  %s [--options] -d domname file [file ...]\n"
63              "  %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
64              "Options:\n"
65              "  -a|--add image       Add image\n"
66              "  -c|--connect uri     Specify libvirt URI for -d option\n"
67              "  -d|--domain guest    Add disks from libvirt guest\n"
68              "  --echo-keys          Don't turn off echo for passphrases\n"
69              "  --format[=raw|..]    Force disk format for -a option\n"
70              "  --help               Display brief help\n"
71              "  --keys-from-stdin    Read passphrases from stdin\n"
72              "  -v|--verbose         Verbose messages\n"
73              "  -V|--version         Display version and exit\n"
74              "  -x                   Trace libguestfs API calls\n"
75              "For more information, see the manpage %s(1).\n"),
76              program_name, program_name, program_name,
77              program_name);
78   }
79   exit (status);
80 }
81
82 int
83 main (int argc, char *argv[])
84 {
85   /* Set global program name that is not polluted with libtool artifacts.  */
86   set_program_name (argv[0]);
87
88   setlocale (LC_ALL, "");
89   bindtextdomain (PACKAGE, LOCALEBASEDIR);
90   textdomain (PACKAGE);
91
92   enum { HELP_OPTION = CHAR_MAX + 1 };
93
94   static const char *options = "a:c:d:vVx";
95   static const struct option long_options[] = {
96     { "add", 1, 0, 'a' },
97     { "connect", 1, 0, 'c' },
98     { "domain", 1, 0, 'd' },
99     { "echo-keys", 0, 0, 0 },
100     { "format", 2, 0, 0 },
101     { "help", 0, 0, HELP_OPTION },
102     { "keys-from-stdin", 0, 0, 0 },
103     { "verbose", 0, 0, 'v' },
104     { "version", 0, 0, 'V' },
105     { 0, 0, 0, 0 }
106   };
107   struct drv *drvs = NULL;
108   struct drv *drv;
109   const char *format = NULL;
110   int c;
111   int option_index;
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   assert (live == 0);
219
220   /* User must specify at least one filename on the command line. */
221   if (optind >= argc || argc - optind < 1)
222     usage (EXIT_FAILURE);
223
224   /* User must have specified some drives. */
225   if (drvs == NULL)
226     usage (EXIT_FAILURE);
227
228   /* Add drives, inspect and mount.  Note that inspector is always true,
229    * and there is no -m option.
230    */
231   add_drives (drvs, 'a');
232
233   if (guestfs_launch (g) == -1)
234     exit (EXIT_FAILURE);
235
236   inspect_mount ();
237
238   /* Free up data structures, no longer needed after this point. */
239   free_drives (drvs);
240
241   unsigned errors = 0;
242
243   while (optind < argc) {
244     if (guestfs_download (g, argv[optind], "/dev/stdout") == -1)
245       errors++;
246     optind++;
247   }
248
249   guestfs_close (g);
250
251   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
252 }