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. */
41 int keys_from_stdin = 0;
43 const char *libvirt_uri = NULL;
47 bad_cast (char const *s)
52 static void __attribute__((noreturn))
55 if (status != EXIT_SUCCESS)
56 fprintf (stderr, _("Try `%s --help' for more information.\n"),
60 _("%s: list files in a virtual machine\n"
61 "Copyright (C) 2010 Red Hat Inc.\n"
63 " %s [--options] -d domname file [dir ...]\n"
64 " %s [--options] -a disk.img [-a disk.img ...] dir [dir ...]\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,
86 main (int argc, char *argv[])
88 /* Set global program name that is not polluted with libtool artifacts. */
89 set_program_name (argv[0]);
91 setlocale (LC_ALL, "");
92 bindtextdomain (PACKAGE, LOCALEBASEDIR);
95 enum { HELP_OPTION = CHAR_MAX + 1 };
97 static const char *options = "a:c:d:lRvVx";
98 static const struct option long_options[] = {
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' },
112 struct drv *drvs = NULL;
114 const char *format = NULL;
119 g = guestfs_create ();
121 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
125 argv[0] = bad_cast (program_name);
128 c = getopt_long (argc, argv, options, long_options, &option_index);
132 case 0: /* options which are long only */
133 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
135 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
137 } else if (STREQ (long_options[option_index].name, "format")) {
138 if (!optarg || STREQ (optarg, ""))
143 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
144 program_name, long_options[option_index].name, option_index);
162 usage (EXIT_SUCCESS);
185 usage (EXIT_SUCCESS);
188 usage (EXIT_FAILURE);
192 /* Old-style syntax? There were no -a or -d options in the old
193 * virt-ls which is how we detect this.
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));
206 drv->a.filename = argv[optind];
207 drv->a.format = NULL;
210 } else { /* simulate -d option */
211 drv = malloc (sizeof (struct drv));
217 drv->d.guest = argv[optind];
226 /* These are really constants, but they have to be variables for the
227 * options parsing code. Assert here that they have known-good
230 assert (read_only == 1);
231 assert (inspector == 1);
234 /* User must specify at least one directory name on the command line. */
235 if (optind >= argc || argc - optind < 1)
236 usage (EXIT_FAILURE);
238 /* User must have specified some drives. */
240 usage (EXIT_FAILURE);
242 /* Add drives, inspect and mount. Note that inspector is always true,
243 * and there is no -m option.
245 add_drives (drvs, 'a');
247 if (guestfs_launch (g) == -1)
252 /* Free up data structures, no longer needed after this point. */
257 while (optind < argc) {
258 const char *dir = argv[optind];
264 if ((lines = guestfs_ls (g, dir)) == NULL)
267 for (i = 0; lines[i] != NULL; ++i) {
268 printf ("%s\n", lines[i]);
274 else if (mode == 'l') {
277 if ((out = guestfs_ll (g, dir)) == NULL)
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);
290 int fd = mkstemp (tmpfile);
296 char buf[BUFSIZ]; /* also used below */
297 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
299 if (guestfs_find0 (g, dir, buf) == -1)
302 if (close (fd) == -1) {
307 /* The output of find0 is a \0-separated file. Turn each \0 into
310 fd = open (tmpfile, O_RDONLY);
317 while ((r = read (fd, buf, sizeof buf)) > 0) {
319 for (i = 0; i < (size_t) r; ++i)
325 r = write (1, buf, n);
334 if (r == -1 || close (fd) == -1) {
347 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);