1 /*
2 * iterate_dir.c
3 *
4 * Iterate through files in a WIM image.
5 * This is the stable API; internal code can just use for_dentry_in_tree().
6 */
7
8 /*
9 * Copyright (C) 2013-2016 Eric Biggers
10 *
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
14 * later version.
15 *
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
19 * details.
20 *
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/.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "wimlib.h"
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"
41
42 static int
stream_to_wimlib_stream_entry(const struct wim_inode * inode,const struct wim_inode_stream * strm,struct wimlib_stream_entry * wstream,const struct blob_table * blob_table,int flags)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,
47 int flags)
48 {
49 const struct blob_descriptor *blob;
50 const u8 *hash;
51
52 if (stream_is_named(strm)) {
53 int ret;
54
55 ret = utf16le_get_tstr(strm->stream_name,
56 utf16le_len_bytes(strm->stream_name),
57 &wstream->stream_name, NULL);
58 if (ret)
59 return ret;
60 }
61
62 blob = stream_blob(strm, blob_table);
63 if (blob) {
64 blob_to_wimlib_resource_entry(blob, &wstream->resource);
65 } else if (!is_zero_hash((hash = stream_hash(strm)))) {
66 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
67 return blob_not_found_error(inode, hash);
68 copy_hash(wstream->resource.sha1_hash, hash);
69 wstream->resource.is_missing = 1;
70 }
71 return 0;
72 }
73
74 static int
get_default_stream_type(const struct wim_inode * inode)75 get_default_stream_type(const struct wim_inode *inode)
76 {
77 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED)
78 return STREAM_TYPE_EFSRPC_RAW_DATA;
79 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
80 return STREAM_TYPE_REPARSE_POINT;
81 return STREAM_TYPE_DATA;
82 }
83
84 static int
init_wimlib_dentry(struct wimlib_dir_entry * wdentry,struct wim_dentry * dentry,WIMStruct * wim,int flags)85 init_wimlib_dentry(struct wimlib_dir_entry *wdentry, struct wim_dentry *dentry,
86 WIMStruct *wim, int flags)
87 {
88 int ret;
89 const struct wim_inode *inode = dentry->d_inode;
90 const struct wim_inode_stream *strm;
91 struct wimlib_unix_data unix_data;
92 const void *object_id;
93 u32 object_id_len;
94
95 ret = utf16le_get_tstr(dentry->d_name, dentry->d_name_nbytes,
96 &wdentry->filename, NULL);
97 if (ret)
98 return ret;
99
100 ret = utf16le_get_tstr(dentry->d_short_name, dentry->d_short_name_nbytes,
101 &wdentry->dos_name, NULL);
102 if (ret)
103 return ret;
104
105 ret = calculate_dentry_full_path(dentry);
106 if (ret)
107 return ret;
108 wdentry->full_path = dentry->d_full_path;
109
110 for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->d_parent)
111 wdentry->depth++;
112
113 if (inode_has_security_descriptor(inode)) {
114 struct wim_security_data *sd;
115
116 sd = wim_get_current_security_data(wim);
117 wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
118 wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
119 }
120 wdentry->reparse_tag = inode->i_reparse_tag;
121 wdentry->num_links = inode->i_nlink;
122 wdentry->attributes = inode->i_attributes;
123 wdentry->hard_link_group_id = inode->i_ino;
124
125 wim_timestamp_to_wimlib_timespec(inode->i_creation_time,
126 &wdentry->creation_time,
127 &wdentry->creation_time_high);
128
129 wim_timestamp_to_wimlib_timespec(inode->i_last_write_time,
130 &wdentry->last_write_time,
131 &wdentry->last_write_time_high);
132
133 wim_timestamp_to_wimlib_timespec(inode->i_last_access_time,
134 &wdentry->last_access_time,
135 &wdentry->last_access_time_high);
136
137 if (inode_get_unix_data(inode, &unix_data)) {
138 wdentry->unix_uid = unix_data.uid;
139 wdentry->unix_gid = unix_data.gid;
140 wdentry->unix_mode = unix_data.mode;
141 wdentry->unix_rdev = unix_data.rdev;
142 }
143 object_id = inode_get_object_id(inode, &object_id_len);
144 if (unlikely(object_id != NULL)) {
145 memcpy(&wdentry->object_id, object_id,
146 min(object_id_len, sizeof(wdentry->object_id)));
147 }
148
149 strm = inode_get_unnamed_stream(inode, get_default_stream_type(inode));
150 if (strm) {
151 ret = stream_to_wimlib_stream_entry(inode, strm,
152 &wdentry->streams[0],
153 wim->blob_table, flags);
154 if (ret)
155 return ret;
156 }
157
158 for (unsigned i = 0; i < inode->i_num_streams; i++) {
159
160 strm = &inode->i_streams[i];
161
162 if (!stream_is_named_data_stream(strm))
163 continue;
164
165 wdentry->num_named_streams++;
166
167 ret = stream_to_wimlib_stream_entry(inode, strm,
168 &wdentry->streams[
169 wdentry->num_named_streams],
170 wim->blob_table, flags);
171 if (ret)
172 return ret;
173 }
174 return 0;
175 }
176
177 static void
free_wimlib_dentry(struct wimlib_dir_entry * wdentry)178 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
179 {
180 utf16le_put_tstr(wdentry->filename);
181 utf16le_put_tstr(wdentry->dos_name);
182 for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
183 utf16le_put_tstr(wdentry->streams[i].stream_name);
184 FREE(wdentry);
185 }
186
187 static int
do_iterate_dir_tree(WIMStruct * wim,struct wim_dentry * dentry,int flags,wimlib_iterate_dir_tree_callback_t cb,void * user_ctx)188 do_iterate_dir_tree(WIMStruct *wim,
189 struct wim_dentry *dentry, int flags,
190 wimlib_iterate_dir_tree_callback_t cb,
191 void *user_ctx)
192 {
193 struct wimlib_dir_entry *wdentry;
194 int ret = WIMLIB_ERR_NOMEM;
195
196
197 wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
198 (1 + dentry->d_inode->i_num_streams) *
199 sizeof(struct wimlib_stream_entry));
200 if (wdentry == NULL)
201 goto out;
202
203 ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
204 if (ret)
205 goto out_free_wimlib_dentry;
206
207 if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
208 ret = (*cb)(wdentry, user_ctx);
209 if (ret)
210 goto out_free_wimlib_dentry;
211 }
212
213 if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
214 WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
215 {
216 struct wim_dentry *child;
217
218 ret = 0;
219 for_dentry_child(child, dentry) {
220 ret = do_iterate_dir_tree(wim, child,
221 flags & ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
222 cb, user_ctx);
223 if (ret)
224 break;
225 }
226 }
227 out_free_wimlib_dentry:
228 FREE(dentry->d_full_path);
229 dentry->d_full_path = NULL;
230 free_wimlib_dentry(wdentry);
231 out:
232 return ret;
233 }
234
235 struct image_iterate_dir_tree_ctx {
236 const tchar *path;
237 int flags;
238 wimlib_iterate_dir_tree_callback_t cb;
239 void *user_ctx;
240 };
241
242
243 static int
image_do_iterate_dir_tree(WIMStruct * wim)244 image_do_iterate_dir_tree(WIMStruct *wim)
245 {
246 struct image_iterate_dir_tree_ctx *ctx = wim->private;
247 struct wim_dentry *dentry;
248
249 dentry = get_dentry(wim, ctx->path, WIMLIB_CASE_PLATFORM_DEFAULT);
250 if (dentry == NULL)
251 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
252 return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
253 }
254
255 /* API function documented in wimlib.h */
256 WIMLIBAPI int
wimlib_iterate_dir_tree(WIMStruct * wim,int image,const tchar * _path,int flags,wimlib_iterate_dir_tree_callback_t cb,void * user_ctx)257 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *_path,
258 int flags,
259 wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
260 {
261 tchar *path;
262 int ret;
263
264 if (flags & ~(WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
265 WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN |
266 WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED))
267 return WIMLIB_ERR_INVALID_PARAM;
268
269 path = canonicalize_wim_path(_path);
270 if (path == NULL)
271 return WIMLIB_ERR_NOMEM;
272
273 ret = wim_checksum_unhashed_blobs(wim);
274 if (ret)
275 return ret;
276
277 struct image_iterate_dir_tree_ctx ctx = {
278 .path = path,
279 .flags = flags,
280 .cb = cb,
281 .user_ctx = user_ctx,
282 };
283 wim->private = &ctx;
284 ret = for_image(wim, image, image_do_iterate_dir_tree);
285 FREE(path);
286 return ret;
287 }
288