2 * Copyright (C) 2010-2011 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.
37 /* Currently open libguestfs handle. */
43 int keys_from_stdin = 0;
45 const char *libvirt_uri = NULL;
48 static int do_ls (const char *dir);
49 static int do_ls_l (const char *dir);
50 static int do_ls_R (const char *dir);
53 bad_cast (char const *s)
58 static void __attribute__((noreturn))
61 if (status != EXIT_SUCCESS)
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
66 _("%s: list files in a virtual machine\n"
67 "Copyright (C) 2010-2011 Red Hat Inc.\n"
69 " %s [--options] -d domname dir [dir ...]\n"
70 " %s [--options] -a disk.img [-a disk.img ...] dir [dir ...]\n"
72 " -a|--add image Add image\n"
73 " -c|--connect uri Specify libvirt URI for -d option\n"
74 " -d|--domain guest Add disks from libvirt guest\n"
75 " --echo-keys Don't turn off echo for passphrases\n"
76 " --format[=raw|..] Force disk format for -a option\n"
77 " --help Display brief help\n"
78 " --keys-from-stdin Read passphrases from stdin\n"
79 " -l|--long Long listing\n"
80 " -R|--recursive Recursive listing\n"
81 " -v|--verbose Verbose messages\n"
82 " -V|--version Display version and exit\n"
83 " -x Trace libguestfs API calls\n"
84 "For more information, see the manpage %s(1).\n"),
85 program_name, program_name, program_name,
92 main (int argc, char *argv[])
94 /* Set global program name that is not polluted with libtool artifacts. */
95 set_program_name (argv[0]);
97 setlocale (LC_ALL, "");
98 bindtextdomain (PACKAGE, LOCALEBASEDIR);
101 enum { HELP_OPTION = CHAR_MAX + 1 };
103 static const char *options = "a:c:d:lRvVx";
104 static const struct option long_options[] = {
105 { "add", 1, 0, 'a' },
106 { "connect", 1, 0, 'c' },
107 { "domain", 1, 0, 'd' },
108 { "echo-keys", 0, 0, 0 },
109 { "format", 2, 0, 0 },
110 { "help", 0, 0, HELP_OPTION },
111 { "keys-from-stdin", 0, 0, 0 },
112 { "long", 0, 0, 'l' },
113 { "recursive", 0, 0, 'R' },
114 { "verbose", 0, 0, 'v' },
115 { "version", 0, 0, 'V' },
118 struct drv *drvs = NULL;
120 const char *format = NULL;
125 #define MODE_LS_LR (MODE_LS_L|MODE_LS_R)
128 g = guestfs_create ();
130 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
134 argv[0] = bad_cast (program_name);
137 c = getopt_long (argc, argv, options, long_options, &option_index);
141 case 0: /* options which are long only */
142 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
144 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
146 } else if (STREQ (long_options[option_index].name, "format")) {
147 if (!optarg || STREQ (optarg, ""))
152 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
153 program_name, long_options[option_index].name, option_index);
171 usage (EXIT_SUCCESS);
194 usage (EXIT_SUCCESS);
197 usage (EXIT_FAILURE);
201 /* Old-style syntax? There were no -a or -d options in the old
202 * virt-ls which is how we detect this.
205 /* argc - 1 because last parameter is the single directory name. */
206 while (optind < argc - 1) {
207 if (strchr (argv[optind], '/') ||
208 access (argv[optind], F_OK) == 0) { /* simulate -a option */
209 drv = malloc (sizeof (struct drv));
215 drv->a.filename = argv[optind];
216 drv->a.format = NULL;
219 } else { /* simulate -d option */
220 drv = malloc (sizeof (struct drv));
226 drv->d.guest = argv[optind];
235 if (mode == MODE_LS_LR) {
236 fprintf (stderr, _("%s: cannot combine -l and -R options\n"),
241 /* These are really constants, but they have to be variables for the
242 * options parsing code. Assert here that they have known-good
245 assert (read_only == 1);
246 assert (inspector == 1);
249 /* User must specify at least one directory name on the command line. */
250 if (optind >= argc || argc - optind < 1)
251 usage (EXIT_FAILURE);
253 /* User must have specified some drives. */
255 usage (EXIT_FAILURE);
257 /* Add drives, inspect and mount. Note that inspector is always true,
258 * and there is no -m option.
260 add_drives (drvs, 'a');
262 if (guestfs_launch (g) == -1)
267 /* Free up data structures, no longer needed after this point. */
272 while (optind < argc) {
273 const char *dir = argv[optind];
276 case 0: /* no -l or -R option */
277 if (do_ls (dir) == -1)
281 case MODE_LS_L: /* virt-ls -l */
282 if (do_ls_l (dir) == -1)
286 case MODE_LS_R: /* virt-ls -R */
287 if (do_ls_R (dir) == -1)
292 abort (); /* can't happen */
300 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
304 do_ls (const char *dir)
309 if ((lines = guestfs_ls (g, dir)) == NULL) {
313 for (i = 0; lines[i] != NULL; ++i) {
314 printf ("%s\n", lines[i]);
323 do_ls_l (const char *dir)
327 if ((out = guestfs_ll (g, dir)) == NULL)
337 do_ls_R (const char *dir)
339 /* This is TMP_TEMPLATE_ON_STACK expanded from fish.h. */
340 const char *tmpdir = guestfs_tmpdir ();
341 char tmpfile[strlen (tmpdir) + 32];
342 sprintf (tmpfile, "%s/virtlsXXXXXX", tmpdir);
344 int fd = mkstemp (tmpfile);
350 char buf[BUFSIZ]; /* also used below */
351 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
353 if (guestfs_find0 (g, dir, buf) == -1)
356 if (close (fd) == -1) {
361 /* The output of find0 is a \0-separated file. Turn each \0 into
364 fd = open (tmpfile, O_RDONLY);
371 while ((r = read (fd, buf, sizeof buf)) > 0) {
373 for (i = 0; i < (size_t) r; ++i)
379 r = write (1, buf, n);
388 if (r == -1 || close (fd) == -1) {