4 * Iterate through files in a WIM image.
5 * This is the stable API; internal code can just use for_dentry_in_tree().
9 * Copyright (C) 2013 Eric Biggers
11 * This file is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
30 #include "wimlib/dentry.h"
31 #include "wimlib/encoding.h"
32 #include "wimlib/lookup_table.h"
33 #include "wimlib/metadata.h"
34 #include "wimlib/paths.h"
35 #include "wimlib/security.h"
36 #include "wimlib/timestamp.h"
37 #include "wimlib/unix_data.h"
38 #include "wimlib/util.h"
39 #include "wimlib/wim.h"
42 init_wimlib_dentry(struct wimlib_dir_entry *wdentry, struct wim_dentry *dentry,
43 WIMStruct *wim, int flags)
47 const struct wim_inode *inode = dentry->d_inode;
48 struct wim_lookup_table_entry *lte;
50 struct wimlib_unix_data unix_data;
52 ret = utf16le_get_tstr(dentry->file_name, dentry->file_name_nbytes,
53 &wdentry->filename, &dummy);
57 ret = utf16le_get_tstr(dentry->short_name, dentry->short_name_nbytes,
58 &wdentry->dos_name, &dummy);
62 ret = calculate_dentry_full_path(dentry);
65 wdentry->full_path = dentry->_full_path;
67 for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->d_parent)
70 if (inode->i_security_id >= 0) {
71 struct wim_security_data *sd;
73 sd = wim_get_current_security_data(wim);
74 wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
75 wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
77 wdentry->reparse_tag = inode->i_reparse_tag;
78 wdentry->num_links = inode->i_nlink;
79 wdentry->attributes = inode->i_attributes;
80 wdentry->hard_link_group_id = inode->i_ino;
81 wdentry->creation_time = wim_timestamp_to_timespec(inode->i_creation_time);
82 wdentry->last_write_time = wim_timestamp_to_timespec(inode->i_last_write_time);
83 wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
84 if (inode_get_unix_data(inode, &unix_data)) {
85 wdentry->unix_uid = unix_data.uid;
86 wdentry->unix_gid = unix_data.gid;
87 wdentry->unix_mode = unix_data.mode;
88 wdentry->unix_rdev = unix_data.rdev;
91 lte = inode_unnamed_lte(inode, wim->lookup_table);
93 lte_to_wimlib_resource_entry(lte, &wdentry->streams[0].resource);
94 } else if (!is_zero_hash(hash = inode_unnamed_stream_hash(inode))) {
95 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
96 return stream_not_found_error(inode, hash);
97 copy_hash(wdentry->streams[0].resource.sha1_hash, hash);
98 wdentry->streams[0].resource.is_missing = 1;
101 for (unsigned i = 0; i < inode->i_num_ads; i++) {
102 if (!inode->i_ads_entries[i].stream_name_nbytes)
104 lte = inode_stream_lte(inode, i + 1, wim->lookup_table);
105 wdentry->num_named_streams++;
107 lte_to_wimlib_resource_entry(lte, &wdentry->streams[
108 wdentry->num_named_streams].resource);
109 } else if (!is_zero_hash(hash = inode_stream_hash(inode, i + 1))) {
110 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
111 return stream_not_found_error(inode, hash);
112 copy_hash(wdentry->streams[
113 wdentry->num_named_streams].resource.sha1_hash, hash);
115 wdentry->num_named_streams].resource.is_missing = 1;
120 ret = utf16le_get_tstr(inode->i_ads_entries[i].stream_name,
121 inode->i_ads_entries[i].stream_name_nbytes,
123 wdentry->num_named_streams].stream_name,
132 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
134 utf16le_put_tstr(wdentry->filename);
135 utf16le_put_tstr(wdentry->dos_name);
136 for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
137 utf16le_put_tstr(wdentry->streams[i].stream_name);
142 do_iterate_dir_tree(WIMStruct *wim,
143 struct wim_dentry *dentry, int flags,
144 wimlib_iterate_dir_tree_callback_t cb,
147 struct wimlib_dir_entry *wdentry;
148 int ret = WIMLIB_ERR_NOMEM;
151 wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
152 (1 + dentry->d_inode->i_num_ads) *
153 sizeof(struct wimlib_stream_entry));
157 ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
159 goto out_free_wimlib_dentry;
161 if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
162 ret = (*cb)(wdentry, user_ctx);
164 goto out_free_wimlib_dentry;
167 if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
168 WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
170 struct wim_dentry *child;
173 for_dentry_child(child, dentry) {
174 ret = do_iterate_dir_tree(wim, child,
175 flags & ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
181 out_free_wimlib_dentry:
182 free_wimlib_dentry(wdentry);
187 struct image_iterate_dir_tree_ctx {
190 wimlib_iterate_dir_tree_callback_t cb;
196 image_do_iterate_dir_tree(WIMStruct *wim)
198 struct image_iterate_dir_tree_ctx *ctx = wim->private;
199 struct wim_dentry *dentry;
201 dentry = get_dentry(wim, ctx->path, WIMLIB_CASE_PLATFORM_DEFAULT);
203 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
204 return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
207 /* API function documented in wimlib.h */
209 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *_path,
211 wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
216 if (flags & ~(WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
217 WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN |
218 WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED))
219 return WIMLIB_ERR_INVALID_PARAM;
221 path = canonicalize_wim_path(_path);
223 return WIMLIB_ERR_NOMEM;
224 struct image_iterate_dir_tree_ctx ctx = {
228 .user_ctx = user_ctx,
231 ret = for_image(wim, image, image_do_iterate_dir_tree);