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-2016 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/blob_table.h"
31 #include "wimlib/dentry.h"
32 #include "wimlib/encoding.h"
33 #include "wimlib/metadata.h"
34 #include "wimlib/object_id.h"
35 #include "wimlib/paths.h"
36 #include "wimlib/security.h"
37 #include "wimlib/timestamp.h"
38 #include "wimlib/unix_data.h"
39 #include "wimlib/util.h"
40 #include "wimlib/wim.h"
43 stream_to_wimlib_stream_entry(const struct wim_inode *inode,
44 const struct wim_inode_stream *strm,
45 struct wimlib_stream_entry *wstream,
46 const struct blob_table *blob_table,
49 const struct blob_descriptor *blob;
52 if (stream_is_named(strm)) {
56 ret = utf16le_get_tstr(strm->stream_name,
57 utf16le_len_bytes(strm->stream_name),
58 &wstream->stream_name, &dummy);
63 blob = stream_blob(strm, blob_table);
65 blob_to_wimlib_resource_entry(blob, &wstream->resource);
66 } else if (!is_zero_hash((hash = stream_hash(strm)))) {
67 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
68 return blob_not_found_error(inode, hash);
69 copy_hash(wstream->resource.sha1_hash, hash);
70 wstream->resource.is_missing = 1;
76 get_default_stream_type(const struct wim_inode *inode)
78 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED)
79 return STREAM_TYPE_EFSRPC_RAW_DATA;
80 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
81 return STREAM_TYPE_REPARSE_POINT;
82 return STREAM_TYPE_DATA;
86 init_wimlib_dentry(struct wimlib_dir_entry *wdentry, struct wim_dentry *dentry,
87 WIMStruct *wim, int flags)
91 const struct wim_inode *inode = dentry->d_inode;
92 const struct wim_inode_stream *strm;
93 struct wimlib_unix_data unix_data;
94 const void *object_id;
97 ret = utf16le_get_tstr(dentry->d_name, dentry->d_name_nbytes,
98 &wdentry->filename, &dummy);
102 ret = utf16le_get_tstr(dentry->d_short_name, dentry->d_short_name_nbytes,
103 &wdentry->dos_name, &dummy);
107 ret = calculate_dentry_full_path(dentry);
110 wdentry->full_path = dentry->d_full_path;
112 for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->d_parent)
115 if (inode_has_security_descriptor(inode)) {
116 struct wim_security_data *sd;
118 sd = wim_get_current_security_data(wim);
119 wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
120 wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
122 wdentry->reparse_tag = inode->i_reparse_tag;
123 wdentry->num_links = inode->i_nlink;
124 wdentry->attributes = inode->i_attributes;
125 wdentry->hard_link_group_id = inode->i_ino;
126 wdentry->creation_time = wim_timestamp_to_timespec(inode->i_creation_time);
127 wdentry->last_write_time = wim_timestamp_to_timespec(inode->i_last_write_time);
128 wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
129 if (inode_get_unix_data(inode, &unix_data)) {
130 wdentry->unix_uid = unix_data.uid;
131 wdentry->unix_gid = unix_data.gid;
132 wdentry->unix_mode = unix_data.mode;
133 wdentry->unix_rdev = unix_data.rdev;
135 object_id = inode_get_object_id(inode, &object_id_len);
136 if (unlikely(object_id != NULL)) {
137 memcpy(&wdentry->object_id, object_id,
138 min(object_id_len, sizeof(wdentry->object_id)));
141 strm = inode_get_unnamed_stream(inode, get_default_stream_type(inode));
143 ret = stream_to_wimlib_stream_entry(inode, strm,
144 &wdentry->streams[0],
145 wim->blob_table, flags);
150 for (unsigned i = 0; i < inode->i_num_streams; i++) {
152 strm = &inode->i_streams[i];
154 if (!stream_is_named_data_stream(strm))
157 wdentry->num_named_streams++;
159 ret = stream_to_wimlib_stream_entry(inode, strm,
161 wdentry->num_named_streams],
162 wim->blob_table, flags);
170 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
172 utf16le_put_tstr(wdentry->filename);
173 utf16le_put_tstr(wdentry->dos_name);
174 for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
175 utf16le_put_tstr(wdentry->streams[i].stream_name);
180 do_iterate_dir_tree(WIMStruct *wim,
181 struct wim_dentry *dentry, int flags,
182 wimlib_iterate_dir_tree_callback_t cb,
185 struct wimlib_dir_entry *wdentry;
186 int ret = WIMLIB_ERR_NOMEM;
189 wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
190 (1 + dentry->d_inode->i_num_streams) *
191 sizeof(struct wimlib_stream_entry));
195 ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
197 goto out_free_wimlib_dentry;
199 if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
200 ret = (*cb)(wdentry, user_ctx);
202 goto out_free_wimlib_dentry;
205 if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
206 WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
208 struct wim_dentry *child;
211 if (default_ignore_case) {
212 for_dentry_child_case_insensitive(child, dentry) {
213 ret = do_iterate_dir_tree(wim, child,
214 flags & ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
220 for_dentry_child(child, dentry) {
221 ret = do_iterate_dir_tree(wim, child,
222 flags & ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
229 out_free_wimlib_dentry:
230 FREE(dentry->d_full_path);
231 dentry->d_full_path = NULL;
232 free_wimlib_dentry(wdentry);
237 struct image_iterate_dir_tree_ctx {
240 wimlib_iterate_dir_tree_callback_t cb;
246 image_do_iterate_dir_tree(WIMStruct *wim)
248 struct image_iterate_dir_tree_ctx *ctx = wim->private;
249 struct wim_dentry *dentry;
251 dentry = get_dentry(wim, ctx->path, WIMLIB_CASE_PLATFORM_DEFAULT);
253 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
254 return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
257 /* API function documented in wimlib.h */
259 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *_path,
261 wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
266 if (flags & ~(WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
267 WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN |
268 WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED))
269 return WIMLIB_ERR_INVALID_PARAM;
271 path = canonicalize_wim_path(_path);
273 return WIMLIB_ERR_NOMEM;
275 ret = wim_checksum_unhashed_blobs(wim);
279 struct image_iterate_dir_tree_ctx ctx = {
283 .user_ctx = user_ctx,
286 ret = for_image(wim, image, image_do_iterate_dir_tree);