2 * Copyright (C) 2010 Red Hat Inc.
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.
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.
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.
34 /* Currently open libguestfs handle. */
39 int keys_from_stdin = 0;
41 const char *libvirt_uri = NULL;
45 bad_cast (char const *s)
50 static void __attribute__((noreturn))
53 if (status != EXIT_SUCCESS)
54 fprintf (stderr, _("Try `%s --help' for more information.\n"),
58 _("%s: list files in a virtual machine\n"
59 "Copyright (C) 2010 Red Hat Inc.\n"
61 " %s [--options] -d domname file [dir ...]\n"
62 " %s [--options] -a disk.img [-a disk.img ...] dir [dir ...]\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,
84 main (int argc, char *argv[])
86 /* Set global program name that is not polluted with libtool artifacts. */
87 set_program_name (argv[0]);
89 setlocale (LC_ALL, "");
90 bindtextdomain (PACKAGE, LOCALEBASEDIR);
93 enum { HELP_OPTION = CHAR_MAX + 1 };
95 static const char *options = "a:c:d:lRvVx";
96 static const struct option long_options[] = {
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' },
110 struct drv *drvs = NULL;
112 const char *format = NULL;
117 g = guestfs_create ();
119 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
123 argv[0] = bad_cast (program_name);
126 c = getopt_long (argc, argv, options, long_options, &option_index);
130 case 0: /* options which are long only */
131 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
133 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
135 } else if (STREQ (long_options[option_index].name, "format")) {
136 if (!optarg || STREQ (optarg, ""))
141 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
142 program_name, long_options[option_index].name, option_index);
160 usage (EXIT_SUCCESS);
183 usage (EXIT_SUCCESS);
186 usage (EXIT_FAILURE);
190 /* Old-style syntax? There were no -a or -d options in the old
191 * virt-ls which is how we detect this.
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));
204 drv->a.filename = argv[optind];
205 drv->a.format = NULL;
208 } else { /* simulate -d option */
209 drv = malloc (sizeof (struct drv));
215 drv->d.guest = argv[optind];
224 /* These are really constants, but they have to be variables for the
225 * options parsing code. Assert here that they have known-good
228 assert (read_only == 1);
229 assert (inspector == 1);
231 /* User must specify at least one directory name on the command line. */
232 if (optind >= argc || argc - optind < 1)
233 usage (EXIT_FAILURE);
235 /* User must have specified some drives. */
237 usage (EXIT_FAILURE);
239 /* Add drives, inspect and mount. Note that inspector is always true,
240 * and there is no -m option.
242 add_drives (drvs, 'a');
244 if (guestfs_launch (g) == -1)
249 /* Free up data structures, no longer needed after this point. */
254 while (optind < argc) {
255 const char *dir = argv[optind];
261 if ((lines = guestfs_ls (g, dir)) == NULL)
264 for (i = 0; lines[i] != NULL; ++i) {
265 printf ("%s\n", lines[i]);
271 else if (mode == 'l') {
274 if ((out = guestfs_ll (g, dir)) == NULL)
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);
287 int fd = mkstemp (tmpfile);
293 char buf[BUFSIZ]; /* also used below */
294 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
296 if (guestfs_find0 (g, dir, buf) == -1)
299 if (close (fd) == -1) {
304 /* The output of find0 is a \0-separated file. Turn each \0 into
307 fd = open (tmpfile, O_RDONLY);
314 while ((r = read (fd, buf, sizeof buf)) > 0) {
316 for (i = 0; i < (size_t) r; ++i)
322 r = write (1, buf, n);
331 if (r == -1 || close (fd) == -1) {
344 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);