fish: Add guestfish --live, guestmount --live options.
[libguestfs.git] / cat / virt-ls.c
1 /* virt-ls
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 <fcntl.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: list files in a virtual machine\n"
60              "Copyright (C) 2010 Red Hat Inc.\n"
61              "Usage:\n"
62              "  %s [--options] -d domname file [dir ...]\n"
63              "  %s [--options] -a disk.img [-a disk.img ...] dir [dir ...]\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              "  -l|--long            Long listing\n"
73              "  -R|--recursive       Recursive listing\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:lRvVx";
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     { "long", 0, 0, 'l' },
106     { "recursive", 0, 0, 'R' },
107     { "verbose", 0, 0, 'v' },
108     { "version", 0, 0, 'V' },
109     { 0, 0, 0, 0 }
110   };
111   struct drv *drvs = NULL;
112   struct drv *drv;
113   const char *format = NULL;
114   int c;
115   int option_index;
116   char mode = 0;
117
118   g = guestfs_create ();
119   if (g == NULL) {
120     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
121     exit (EXIT_FAILURE);
122   }
123
124   argv[0] = bad_cast (program_name);
125
126   for (;;) {
127     c = getopt_long (argc, argv, options, long_options, &option_index);
128     if (c == -1) break;
129
130     switch (c) {
131     case 0:                     /* options which are long only */
132       if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
133         keys_from_stdin = 1;
134       } else if (STREQ (long_options[option_index].name, "echo-keys")) {
135         echo_keys = 1;
136       } else if (STREQ (long_options[option_index].name, "format")) {
137         if (!optarg || STREQ (optarg, ""))
138           format = NULL;
139         else
140           format = optarg;
141       } else {
142         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
143                  program_name, long_options[option_index].name, option_index);
144         exit (EXIT_FAILURE);
145       }
146       break;
147
148     case 'a':
149       OPTION_a;
150       break;
151
152     case 'c':
153       OPTION_c;
154       break;
155
156     case 'd':
157       OPTION_d;
158       break;
159
160     case 'h':
161       usage (EXIT_SUCCESS);
162
163     case 'l':
164       mode = 'l';
165       break;
166
167     case 'R':
168       mode = 'R';
169       break;
170
171     case 'v':
172       OPTION_v;
173       break;
174
175     case 'V':
176       OPTION_V;
177       break;
178
179     case 'x':
180       OPTION_x;
181       break;
182
183     case HELP_OPTION:
184       usage (EXIT_SUCCESS);
185
186     default:
187       usage (EXIT_FAILURE);
188     }
189   }
190
191   /* Old-style syntax?  There were no -a or -d options in the old
192    * virt-ls which is how we detect this.
193    */
194   if (drvs == NULL) {
195     /* argc - 1 because last parameter is the single directory name. */
196     while (optind < argc - 1) {
197       if (strchr (argv[optind], '/') ||
198           access (argv[optind], F_OK) == 0) { /* simulate -a option */
199         drv = malloc (sizeof (struct drv));
200         if (!drv) {
201           perror ("malloc");
202           exit (EXIT_FAILURE);
203         }
204         drv->type = drv_a;
205         drv->a.filename = argv[optind];
206         drv->a.format = NULL;
207         drv->next = drvs;
208         drvs = drv;
209       } else {                  /* simulate -d option */
210         drv = malloc (sizeof (struct drv));
211         if (!drv) {
212           perror ("malloc");
213           exit (EXIT_FAILURE);
214         }
215         drv->type = drv_d;
216         drv->d.guest = argv[optind];
217         drv->next = drvs;
218         drvs = drv;
219       }
220
221       optind++;
222     }
223   }
224
225   /* These are really constants, but they have to be variables for the
226    * options parsing code.  Assert here that they have known-good
227    * values.
228    */
229   assert (read_only == 1);
230   assert (inspector == 1);
231   assert (live == 0);
232
233   /* User must specify at least one directory name on the command line. */
234   if (optind >= argc || argc - optind < 1)
235     usage (EXIT_FAILURE);
236
237   /* User must have specified some drives. */
238   if (drvs == NULL)
239     usage (EXIT_FAILURE);
240
241   /* Add drives, inspect and mount.  Note that inspector is always true,
242    * and there is no -m option.
243    */
244   add_drives (drvs, 'a');
245
246   if (guestfs_launch (g) == -1)
247     exit (EXIT_FAILURE);
248
249   inspect_mount ();
250
251   /* Free up data structures, no longer needed after this point. */
252   free_drives (drvs);
253
254   unsigned errors = 0;
255
256   while (optind < argc) {
257     const char *dir = argv[optind];
258
259     if (mode == 0) {
260       char **lines;
261       size_t i;
262
263       if ((lines = guestfs_ls (g, dir)) == NULL)
264         errors++;
265       else {
266         for (i = 0; lines[i] != NULL; ++i) {
267           printf ("%s\n", lines[i]);
268           free (lines[i]);
269         }
270         free (lines);
271       }
272     }
273     else if (mode == 'l') {
274       char *out;
275
276       if ((out = guestfs_ll (g, dir)) == NULL)
277         errors++;
278       else {
279         printf ("%s", out);
280         free (out);
281       }
282     }
283     else if (mode == 'R') {
284       /* This is TMP_TEMPLATE_ON_STACK expanded from fish.h. */
285       const char *tmpdir = guestfs_tmpdir ();
286       char tmpfile[strlen (tmpdir) + 32];
287       sprintf (tmpfile, "%s/virtlsXXXXXX", tmpdir);
288
289       int fd = mkstemp (tmpfile);
290       if (fd == -1) {
291         perror ("mkstemp");
292         exit (EXIT_FAILURE);
293       }
294
295       char buf[BUFSIZ]; /* also used below */
296       snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
297
298       if (guestfs_find0 (g, dir, buf) == -1)
299         errors++;
300       else {
301         if (close (fd) == -1) {
302           perror (tmpfile);
303           exit (EXIT_FAILURE);
304         }
305
306         /* The output of find0 is a \0-separated file.  Turn each \0 into
307          * a \n character.
308          */
309         fd = open (tmpfile, O_RDONLY);
310         if (fd == -1) {
311           perror (tmpfile);
312           exit (EXIT_FAILURE);
313         }
314
315         ssize_t r;
316         while ((r = read (fd, buf, sizeof buf)) > 0) {
317           size_t i;
318           for (i = 0; i < (size_t) r; ++i)
319             if (buf[i] == '\0')
320               buf[i] = '\n';
321
322           size_t n = r;
323           while (n > 0) {
324             r = write (1, buf, n);
325             if (r == -1) {
326               perror ("write");
327               exit (EXIT_FAILURE);
328             }
329             n -= r;
330           }
331         }
332
333         if (r == -1 || close (fd) == -1) {
334           perror (tmpfile);
335           exit (EXIT_FAILURE);
336         }
337       }
338
339       unlink (tmpfile);
340     }
341     optind++;
342   }
343
344   guestfs_close (g);
345
346   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
347 }