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