1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 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.
28 #include "../src/guestfs_protocol.h"
33 input_to_nul (FILE *fp, char *buf, int maxlen)
46 reply_with_error ("input_to_nul: input string too long");
54 int r, len, sysrootdirlen;
58 int size = 0, alloc = 0;
59 char sysrootdir[PATH_MAX];
65 snprintf (sysrootdir, sizeof sysrootdir, "/sysroot%s", dir);
67 r = stat (sysrootdir, &statbuf);
69 reply_with_perror ("%s", dir);
72 if (!S_ISDIR (statbuf.st_mode)) {
73 reply_with_error ("%s: not a directory", dir);
77 sysrootdirlen = strlen (sysrootdir);
79 /* Assemble the external find command. */
80 len = 2 * sysrootdirlen + 32;
83 reply_with_perror ("malloc");
87 strcpy (cmd, "find ");
88 shell_quote (cmd+5, len-5, sysrootdir);
89 strcat (cmd, " -print0");
94 fp = popen (cmd, "r");
96 reply_with_perror ("%s", cmd);
102 while ((r = input_to_nul (fp, str, PATH_MAX)) > 0) {
104 printf ("find string: %s\n", str);
107 if (len <= sysrootdirlen)
110 /* Remove the directory part of the path when adding it. */
111 if (add_string (&res, &size, &alloc, str + sysrootdirlen) == -1) {
116 if (pclose (fp) != 0) {
117 reply_with_perror ("pclose: find");
118 free_stringslen (res, size);
123 free_stringslen (res, size);
127 if (add_string (&res, &size, &alloc, NULL) == -1)
130 sort_strings (res, size-1);
132 return res; /* caller frees */