1 /* virt-ls visitor function (from libguestfs)
2 * Copyright (C) 2010-2014 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 https://rwmj.wordpress.com/2010/12/15/tip-audit-virtual-machine-for-setuid-files/
37 static int _visit (guestfs_h *g, int depth, const char *dir, visitor_function f, void *opaque);
40 visit (guestfs_h *g, const char *dir, visitor_function f, void *opaque)
42 return _visit (g, 0, dir, f, opaque);
46 _visit (guestfs_h *g, int depth, const char *dir,
47 visitor_function f, void *opaque)
49 /* Call 'f' with the top directory. Note that ordinary recursive
50 * visits will not otherwise do this, so we have to have a special
54 CLEANUP_FREE_STATNS struct guestfs_statns *stat = NULL;
55 CLEANUP_FREE_XATTR_LIST struct guestfs_xattr_list *xattrs = NULL;
58 stat = guestfs_lstatns (g, dir);
62 xattrs = guestfs_lgetxattrs (g, dir);
66 r = f (dir, NULL, stat, xattrs, opaque);
73 CLEANUP_FREE_STRING_LIST char **names = NULL;
74 CLEANUP_FREE_STAT_LIST struct guestfs_statns_list *stats = NULL;
75 CLEANUP_FREE_XATTR_LIST struct guestfs_xattr_list *xattrs = NULL;
77 names = guestfs_ls (g, dir);
81 stats = guestfs_lstatnslist (g, dir, names);
85 xattrs = guestfs_lxattrlist (g, dir, names);
89 /* Call function on everything in this directory. */
90 for (i = 0, xattrp = 0; names[i] != NULL; ++i, ++xattrp) {
91 CLEANUP_FREE char *path = NULL;
92 struct guestfs_xattr_list file_xattrs;
95 assert (stats->len >= i);
96 assert (xattrs->len >= xattrp);
98 /* Find the list of extended attributes for this file. */
99 assert (strlen (xattrs->val[xattrp].attrname) == 0);
101 if (xattrs->val[xattrp].attrval_len == 0) {
102 fprintf (stderr, "virt-bmap: error getting extended attrs for %s %s\n",
106 /* attrval is not \0-terminated. */
107 char attrval[xattrs->val[xattrp].attrval_len+1];
108 memcpy (attrval, xattrs->val[xattrp].attrval,
109 xattrs->val[xattrp].attrval_len);
110 attrval[xattrs->val[xattrp].attrval_len] = '\0';
111 if (sscanf (attrval, "%zu", &nr_xattrs) != 1) {
112 fprintf (stderr, "virt-bmap: error: cannot parse xattr count for %s %s\n",
117 file_xattrs.len = nr_xattrs;
118 file_xattrs.val = &xattrs->val[xattrp+1];
121 /* Call the function. */
122 if (f (dir, names[i], &stats->val[i], &file_xattrs, opaque) == -1)
125 /* Recursively call visit, but only on directories. */
126 if (is_dir (stats->val[i].st_mode)) {
127 path = full_path (dir, names[i]);
130 if (_visit (g, depth + 1, path, f, opaque) == -1)
139 full_path (const char *dir, const char *name)
146 if (len > 0 && dir[len - 1] == '/')
149 if (strcmp (dir, "/") == 0)
150 r = asprintf (&path, "/%s", name ? name : "");
152 r = asprintf (&path, "%.*s/%s", len, dir, name);
154 r = asprintf (&path, "%.*s", len, dir);
164 /* In the libguestfs API, modes returned by lstat and friends are
165 * defined to contain Linux ABI values. However since the "current
166 * operating system" might not be Linux, we have to hard-code those
170 is_reg (int64_t mode)
172 return (mode & 0170000) == 0100000;
176 is_dir (int64_t mode)
178 return (mode & 0170000) == 0040000;
182 is_chr (int64_t mode)
184 return (mode & 0170000) == 0020000;
188 is_blk (int64_t mode)
190 return (mode & 0170000) == 0060000;
194 is_fifo (int64_t mode)
196 return (mode & 0170000) == 0010000;
201 is_lnk (int64_t mode)
203 return (mode & 0170000) == 0120000;
207 is_sock (int64_t mode)
209 return (mode & 0170000) == 0140000;