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