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.
35 /* Currently open libguestfs handle. */
40 int keys_from_stdin = 0;
42 const char *libvirt_uri = NULL;
46 bad_cast (char const *s)
51 static void __attribute__((noreturn))
54 if (status != EXIT_SUCCESS)
55 fprintf (stderr, _("Try `%s --help' for more information.\n"),
59 _("%s: list files in a virtual machine\n"
60 "Copyright (C) 2010 Red Hat Inc.\n"
62 " %s [--options] -d domname dir [dir ...]\n"
63 " %s [--options] -a disk.img [-a disk.img ...] dir [dir ...]\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,
85 main (int argc, char *argv[])
87 /* Set global program name that is not polluted with libtool artifacts. */
88 set_program_name (argv[0]);
90 setlocale (LC_ALL, "");
91 bindtextdomain (PACKAGE, LOCALEBASEDIR);
94 enum { HELP_OPTION = CHAR_MAX + 1 };
96 static const char *options = "a:c:d:lRvVx";
97 static const struct option long_options[] = {
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' },
111 struct drv *drvs = NULL;
113 const char *format = NULL;
118 g = guestfs_create ();
120 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
124 argv[0] = bad_cast (program_name);
127 c = getopt_long (argc, argv, options, long_options, &option_index);
131 case 0: /* options which are long only */
132 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
134 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
136 } else if (STREQ (long_options[option_index].name, "format")) {
137 if (!optarg || STREQ (optarg, ""))
142 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
143 program_name, long_options[option_index].name, option_index);
161 usage (EXIT_SUCCESS);
184 usage (EXIT_SUCCESS);
187 usage (EXIT_FAILURE);
191 /* Old-style syntax? There were no -a or -d options in the old
192 * virt-ls which is how we detect this.
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));
205 drv->a.filename = argv[optind];
206 drv->a.format = NULL;
209 } else { /* simulate -d option */
210 drv = malloc (sizeof (struct drv));
216 drv->d.guest = argv[optind];
225 /* These are really constants, but they have to be variables for the
226 * options parsing code. Assert here that they have known-good
229 assert (read_only == 1);
230 assert (inspector == 1);
232 /* User must specify at least one directory name on the command line. */
233 if (optind >= argc || argc - optind < 1)
234 usage (EXIT_FAILURE);
236 /* User must have specified some drives. */
238 usage (EXIT_FAILURE);
240 /* Add drives, inspect and mount. Note that inspector is always true,
241 * and there is no -m option.
243 add_drives (drvs, 'a');
245 if (guestfs_launch (g) == -1)
250 /* Free up data structures, no longer needed after this point. */
255 while (optind < argc) {
256 const char *dir = argv[optind];
262 if ((lines = guestfs_ls (g, dir)) == NULL)
265 for (i = 0; lines[i] != NULL; ++i) {
266 printf ("%s\n", lines[i]);
272 else if (mode == 'l') {
275 if ((out = guestfs_ll (g, dir)) == NULL)
282 else if (mode == 'R') {
283 /* This is TMP_TEMPLATE_ON_STACK expanded from fish.h. */
284 const char *tmpdir = guestfs_tmpdir ();
285 char tmpfile[strlen (tmpdir) + 32];
286 sprintf (tmpfile, "%s/virtlsXXXXXX", tmpdir);
288 int fd = mkstemp (tmpfile);
294 char buf[BUFSIZ]; /* also used below */
295 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
297 if (guestfs_find0 (g, dir, buf) == -1)
300 if (close (fd) == -1) {
305 /* The output of find0 is a \0-separated file. Turn each \0 into
308 fd = open (tmpfile, O_RDONLY);
315 while ((r = read (fd, buf, sizeof buf)) > 0) {
317 for (i = 0; i < (size_t) r; ++i)
323 r = write (1, buf, n);
332 if (r == -1 || close (fd) == -1) {
345 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);