]> wimlib.net Git - wimlib/blob - src/iterate_dir.c
c5f5dad7d7946e7e8b1aa465670ec52c6bde84fc
[wimlib] / src / iterate_dir.c
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
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                 size_t dummy;
54                 int ret;
55
56                 ret = utf16le_get_tstr(strm->stream_name,
57                                        utf16le_len_bytes(strm->stream_name),
58                                        &wstream->stream_name, &dummy);
59                 if (ret)
60                         return ret;
61         }
62
63         blob = stream_blob(strm, blob_table);
64         if (blob) {
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;
71         }
72         return 0;
73 }
74
75 static int
76 get_default_stream_type(const struct wim_inode *inode)
77 {
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;
83 }
84
85 static int
86 init_wimlib_dentry(struct wimlib_dir_entry *wdentry, struct wim_dentry *dentry,
87                    WIMStruct *wim, int flags)
88 {
89         int ret;
90         size_t dummy;
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;
95         u32 object_id_len;
96
97         ret = utf16le_get_tstr(dentry->d_name, dentry->d_name_nbytes,
98                                &wdentry->filename, &dummy);
99         if (ret)
100                 return ret;
101
102         ret = utf16le_get_tstr(dentry->d_short_name, dentry->d_short_name_nbytes,
103                                &wdentry->dos_name, &dummy);
104         if (ret)
105                 return ret;
106
107         ret = calculate_dentry_full_path(dentry);
108         if (ret)
109                 return ret;
110         wdentry->full_path = dentry->d_full_path;
111
112         for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->d_parent)
113                 wdentry->depth++;
114
115         if (inode_has_security_descriptor(inode)) {
116                 struct wim_security_data *sd;
117
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];
121         }
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;
134         }
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)));
139         }
140
141         strm = inode_get_unnamed_stream(inode, get_default_stream_type(inode));
142         if (strm) {
143                 ret = stream_to_wimlib_stream_entry(inode, strm,
144                                                     &wdentry->streams[0],
145                                                     wim->blob_table, flags);
146                 if (ret)
147                         return ret;
148         }
149
150         for (unsigned i = 0; i < inode->i_num_streams; i++) {
151
152                 strm = &inode->i_streams[i];
153
154                 if (!stream_is_named_data_stream(strm))
155                         continue;
156
157                 wdentry->num_named_streams++;
158
159                 ret = stream_to_wimlib_stream_entry(inode, strm,
160                                                     &wdentry->streams[
161                                                         wdentry->num_named_streams],
162                                                     wim->blob_table, flags);
163                 if (ret)
164                         return ret;
165         }
166         return 0;
167 }
168
169 static void
170 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
171 {
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);
176         FREE(wdentry);
177 }
178
179 static int
180 do_iterate_dir_tree(WIMStruct *wim,
181                     struct wim_dentry *dentry, int flags,
182                     wimlib_iterate_dir_tree_callback_t cb,
183                     void *user_ctx)
184 {
185         struct wimlib_dir_entry *wdentry;
186         int ret = WIMLIB_ERR_NOMEM;
187
188
189         wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
190                                   (1 + dentry->d_inode->i_num_streams) *
191                                         sizeof(struct wimlib_stream_entry));
192         if (wdentry == NULL)
193                 goto out;
194
195         ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
196         if (ret)
197                 goto out_free_wimlib_dentry;
198
199         if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
200                 ret = (*cb)(wdentry, user_ctx);
201                 if (ret)
202                         goto out_free_wimlib_dentry;
203         }
204
205         if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
206                      WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
207         {
208                 struct wim_dentry *child;
209
210                 ret = 0;
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,
215                                                           cb, user_ctx);
216                                 if (ret)
217                                         break;
218                         }
219                 } else {
220                         for_dentry_child(child, dentry) {
221                                 ret = do_iterate_dir_tree(wim, child,
222                                                           flags & ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
223                                                           cb, user_ctx);
224                                 if (ret)
225                                         break;
226                         }
227                 }
228         }
229 out_free_wimlib_dentry:
230         FREE(dentry->d_full_path);
231         dentry->d_full_path = NULL;
232         free_wimlib_dentry(wdentry);
233 out:
234         return ret;
235 }
236
237 struct image_iterate_dir_tree_ctx {
238         const tchar *path;
239         int flags;
240         wimlib_iterate_dir_tree_callback_t cb;
241         void *user_ctx;
242 };
243
244
245 static int
246 image_do_iterate_dir_tree(WIMStruct *wim)
247 {
248         struct image_iterate_dir_tree_ctx *ctx = wim->private;
249         struct wim_dentry *dentry;
250
251         dentry = get_dentry(wim, ctx->path, WIMLIB_CASE_PLATFORM_DEFAULT);
252         if (dentry == NULL)
253                 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
254         return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
255 }
256
257 /* API function documented in wimlib.h  */
258 WIMLIBAPI int
259 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *_path,
260                         int flags,
261                         wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
262 {
263         tchar *path;
264         int ret;
265
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;
270
271         path = canonicalize_wim_path(_path);
272         if (path == NULL)
273                 return WIMLIB_ERR_NOMEM;
274
275         ret = wim_checksum_unhashed_blobs(wim);
276         if (ret)
277                 return ret;
278
279         struct image_iterate_dir_tree_ctx ctx = {
280                 .path = path,
281                 .flags = flags,
282                 .cb = cb,
283                 .user_ctx = user_ctx,
284         };
285         wim->private = &ctx;
286         ret = for_image(wim, image, image_do_iterate_dir_tree);
287         FREE(path);
288         return ret;
289 }