Version 1.9.16.
[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 <locale.h>
28 #include <assert.h>
29
30 #include "progname.h"
31
32 #include "guestfs.h"
33 #include "options.h"
34
35 /* Currently open libguestfs handle. */
36 guestfs_h *g;
37
38 int read_only = 1;
39 int live = 0;
40 int verbose = 0;
41 int keys_from_stdin = 0;
42 int echo_keys = 0;
43 const char *libvirt_uri = NULL;
44 int inspector = 1;
45
46 static inline char *
47 bad_cast (char const *s)
48 {
49   return (char *) s;
50 }
51
52 static void __attribute__((noreturn))
53 usage (int status)
54 {
55   if (status != EXIT_SUCCESS)
56     fprintf (stderr, _("Try `%s --help' for more information.\n"),
57              program_name);
58   else {
59     fprintf (stdout,
60            _("%s: list files in a virtual machine\n"
61              "Copyright (C) 2010 Red Hat Inc.\n"
62              "Usage:\n"
63              "  %s [--options] -d domname file [dir ...]\n"
64              "  %s [--options] -a disk.img [-a disk.img ...] dir [dir ...]\n"
65              "Options:\n"
66              "  -a|--add image       Add image\n"
67              "  -c|--connect uri     Specify libvirt URI for -d option\n"
68              "  -d|--domain guest    Add disks from libvirt guest\n"
69              "  --echo-keys          Don't turn off echo for passphrases\n"
70              "  --format[=raw|..]    Force disk format for -a option\n"
71              "  --help               Display brief help\n"
72              "  --keys-from-stdin    Read passphrases from stdin\n"
73              "  -l|--long            Long listing\n"
74              "  -R|--recursive       Recursive listing\n"
75              "  -v|--verbose         Verbose messages\n"
76              "  -V|--version         Display version and exit\n"
77              "  -x                   Trace libguestfs API calls\n"
78              "For more information, see the manpage %s(1).\n"),
79              program_name, program_name, program_name,
80              program_name);
81   }
82   exit (status);
83 }
84
85 int
86 main (int argc, char *argv[])
87 {
88   /* Set global program name that is not polluted with libtool artifacts.  */
89   set_program_name (argv[0]);
90
91   setlocale (LC_ALL, "");
92   bindtextdomain (PACKAGE, LOCALEBASEDIR);
93   textdomain (PACKAGE);
94
95   enum { HELP_OPTION = CHAR_MAX + 1 };
96
97   static const char *options = "a:c:d:lRvVx";
98   static const struct option long_options[] = {
99     { "add", 1, 0, 'a' },
100     { "connect", 1, 0, 'c' },
101     { "domain", 1, 0, 'd' },
102     { "echo-keys", 0, 0, 0 },
103     { "format", 2, 0, 0 },
104     { "help", 0, 0, HELP_OPTION },
105     { "keys-from-stdin", 0, 0, 0 },
106     { "long", 0, 0, 'l' },
107     { "recursive", 0, 0, 'R' },
108     { "verbose", 0, 0, 'v' },
109     { "version", 0, 0, 'V' },
110     { 0, 0, 0, 0 }
111   };
112   struct drv *drvs = NULL;
113   struct drv *drv;
114   const char *format = NULL;
115   int c;
116   int option_index;
117   char mode = 0;
118
119   g = guestfs_create ();
120   if (g == NULL) {
121     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
122     exit (EXIT_FAILURE);
123   }
124
125   argv[0] = bad_cast (program_name);
126
127   for (;;) {
128     c = getopt_long (argc, argv, options, long_options, &option_index);
129     if (c == -1) break;
130
131     switch (c) {
132     case 0:                     /* options which are long only */
133       if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
134         keys_from_stdin = 1;
135       } else if (STREQ (long_options[option_index].name, "echo-keys")) {
136         echo_keys = 1;
137       } else if (STREQ (long_options[option_index].name, "format")) {
138         if (!optarg || STREQ (optarg, ""))
139           format = NULL;
140         else
141           format = optarg;
142       } else {
143         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
144                  program_name, long_options[option_index].name, option_index);
145         exit (EXIT_FAILURE);
146       }
147       break;
148
149     case 'a':
150       OPTION_a;
151       break;
152
153     case 'c':
154       OPTION_c;
155       break;
156
157     case 'd':
158       OPTION_d;
159       break;
160
161     case 'h':
162       usage (EXIT_SUCCESS);
163
164     case 'l':
165       mode = 'l';
166       break;
167
168     case 'R':
169       mode = 'R';
170       break;
171
172     case 'v':
173       OPTION_v;
174       break;
175
176     case 'V':
177       OPTION_V;
178       break;
179
180     case 'x':
181       OPTION_x;
182       break;
183
184     case HELP_OPTION:
185       usage (EXIT_SUCCESS);
186
187     default:
188       usage (EXIT_FAILURE);
189     }
190   }
191
192   /* Old-style syntax?  There were no -a or -d options in the old
193    * virt-ls which is how we detect this.
194    */
195   if (drvs == NULL) {
196     /* argc - 1 because last parameter is the single directory name. */
197     while (optind < argc - 1) {
198       if (strchr (argv[optind], '/') ||
199           access (argv[optind], F_OK) == 0) { /* simulate -a option */
200         drv = malloc (sizeof (struct drv));
201         if (!drv) {
202           perror ("malloc");
203           exit (EXIT_FAILURE);
204         }
205         drv->type = drv_a;
206         drv->a.filename = argv[optind];
207         drv->a.format = NULL;
208         drv->next = drvs;
209         drvs = drv;
210       } else {                  /* simulate -d option */
211         drv = malloc (sizeof (struct drv));
212         if (!drv) {
213           perror ("malloc");
214           exit (EXIT_FAILURE);
215         }
216         drv->type = drv_d;
217         drv->d.guest = argv[optind];
218         drv->next = drvs;
219         drvs = drv;
220       }
221
222       optind++;
223     }
224   }
225
226   /* These are really constants, but they have to be variables for the
227    * options parsing code.  Assert here that they have known-good
228    * values.
229    */
230   assert (read_only == 1);
231   assert (inspector == 1);
232   assert (live == 0);
233
234   /* User must specify at least one directory name on the command line. */
235   if (optind >= argc || argc - optind < 1)
236     usage (EXIT_FAILURE);
237
238   /* User must have specified some drives. */
239   if (drvs == NULL)
240     usage (EXIT_FAILURE);
241
242   /* Add drives, inspect and mount.  Note that inspector is always true,
243    * and there is no -m option.
244    */
245   add_drives (drvs, 'a');
246
247   if (guestfs_launch (g) == -1)
248     exit (EXIT_FAILURE);
249
250   inspect_mount ();
251
252   /* Free up data structures, no longer needed after this point. */
253   free_drives (drvs);
254
255   unsigned errors = 0;
256
257   while (optind < argc) {
258     const char *dir = argv[optind];
259
260     if (mode == 0) {
261       char **lines;
262       size_t i;
263
264       if ((lines = guestfs_ls (g, dir)) == NULL)
265         errors++;
266       else {
267         for (i = 0; lines[i] != NULL; ++i) {
268           printf ("%s\n", lines[i]);
269           free (lines[i]);
270         }
271         free (lines);
272       }
273     }
274     else if (mode == 'l') {
275       char *out;
276
277       if ((out = guestfs_ll (g, dir)) == NULL)
278         errors++;
279       else {
280         printf ("%s", out);
281         free (out);
282       }
283     }
284     else if (mode == 'R') {
285       /* This is TMP_TEMPLATE_ON_STACK expanded from fish.h. */
286       const char *tmpdir = guestfs_tmpdir ();
287       char tmpfile[strlen (tmpdir) + 32];
288       sprintf (tmpfile, "%s/virtlsXXXXXX", tmpdir);
289
290       int fd = mkstemp (tmpfile);
291       if (fd == -1) {
292         perror ("mkstemp");
293         exit (EXIT_FAILURE);
294       }
295
296       char buf[BUFSIZ]; /* also used below */
297       snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
298
299       if (guestfs_find0 (g, dir, buf) == -1)
300         errors++;
301       else {
302         if (close (fd) == -1) {
303           perror (tmpfile);
304           exit (EXIT_FAILURE);
305         }
306
307         /* The output of find0 is a \0-separated file.  Turn each \0 into
308          * a \n character.
309          */
310         fd = open (tmpfile, O_RDONLY);
311         if (fd == -1) {
312           perror (tmpfile);
313           exit (EXIT_FAILURE);
314         }
315
316         ssize_t r;
317         while ((r = read (fd, buf, sizeof buf)) > 0) {
318           size_t i;
319           for (i = 0; i < (size_t) r; ++i)
320             if (buf[i] == '\0')
321               buf[i] = '\n';
322
323           size_t n = r;
324           while (n > 0) {
325             r = write (1, buf, n);
326             if (r == -1) {
327               perror ("write");
328               exit (EXIT_FAILURE);
329             }
330             n -= r;
331           }
332         }
333
334         if (r == -1 || close (fd) == -1) {
335           perror (tmpfile);
336           exit (EXIT_FAILURE);
337         }
338       }
339
340       unlink (tmpfile);
341     }
342     optind++;
343   }
344
345   guestfs_close (g);
346
347   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
348 }