]> wimlib.net Git - wimlib/blob - src/iterate_dir.c
wimlib_iterate_dir_tree(): checksum unhashed blobs
[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, 2015 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/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"
40
41 static int
42 stream_to_wimlib_stream_entry(const struct wim_inode *inode,
43                               const struct wim_inode_stream *strm,
44                               struct wimlib_stream_entry *wstream,
45                               const struct blob_table *blob_table,
46                               int flags)
47 {
48         const struct blob_descriptor *blob;
49         const u8 *hash;
50
51         if (stream_is_named(strm)) {
52                 size_t dummy;
53                 int ret;
54
55                 ret = utf16le_get_tstr(strm->stream_name,
56                                        utf16le_len_bytes(strm->stream_name),
57                                        &wstream->stream_name, &dummy);
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
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
85 init_wimlib_dentry(struct wimlib_dir_entry *wdentry, struct wim_dentry *dentry,
86                    WIMStruct *wim, int flags)
87 {
88         int ret;
89         size_t dummy;
90         const struct wim_inode *inode = dentry->d_inode;
91         const struct wim_inode_stream *strm;
92         struct wimlib_unix_data unix_data;
93
94         ret = utf16le_get_tstr(dentry->file_name, dentry->file_name_nbytes,
95                                &wdentry->filename, &dummy);
96         if (ret)
97                 return ret;
98
99         ret = utf16le_get_tstr(dentry->short_name, dentry->short_name_nbytes,
100                                &wdentry->dos_name, &dummy);
101         if (ret)
102                 return ret;
103
104         ret = calculate_dentry_full_path(dentry);
105         if (ret)
106                 return ret;
107         wdentry->full_path = dentry->_full_path;
108
109         for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->d_parent)
110                 wdentry->depth++;
111
112         if (inode->i_security_id >= 0) {
113                 struct wim_security_data *sd;
114
115                 sd = wim_get_current_security_data(wim);
116                 wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
117                 wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
118         }
119         wdentry->reparse_tag = inode->i_reparse_tag;
120         wdentry->num_links = inode->i_nlink;
121         wdentry->attributes = inode->i_attributes;
122         wdentry->hard_link_group_id = inode->i_ino;
123         wdentry->creation_time = wim_timestamp_to_timespec(inode->i_creation_time);
124         wdentry->last_write_time = wim_timestamp_to_timespec(inode->i_last_write_time);
125         wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
126         if (inode_get_unix_data(inode, &unix_data)) {
127                 wdentry->unix_uid = unix_data.uid;
128                 wdentry->unix_gid = unix_data.gid;
129                 wdentry->unix_mode = unix_data.mode;
130                 wdentry->unix_rdev = unix_data.rdev;
131         }
132
133         strm = inode_get_unnamed_stream(inode, get_default_stream_type(inode));
134         if (strm) {
135                 ret = stream_to_wimlib_stream_entry(inode, strm,
136                                                     &wdentry->streams[0],
137                                                     wim->blob_table, flags);
138                 if (ret)
139                         return ret;
140         }
141
142         for (unsigned i = 0; i < inode->i_num_streams; i++) {
143
144                 strm = &inode->i_streams[i];
145
146                 if (!stream_is_named_data_stream(strm))
147                         continue;
148
149                 wdentry->num_named_streams++;
150
151                 ret = stream_to_wimlib_stream_entry(inode, strm,
152                                                     &wdentry->streams[
153                                                         wdentry->num_named_streams],
154                                                     wim->blob_table, flags);
155                 if (ret)
156                         return ret;
157         }
158         return 0;
159 }
160
161 static void
162 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
163 {
164         utf16le_put_tstr(wdentry->filename);
165         utf16le_put_tstr(wdentry->dos_name);
166         for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
167                 utf16le_put_tstr(wdentry->streams[i].stream_name);
168         FREE(wdentry);
169 }
170
171 static int
172 do_iterate_dir_tree(WIMStruct *wim,
173                     struct wim_dentry *dentry, int flags,
174                     wimlib_iterate_dir_tree_callback_t cb,
175                     void *user_ctx)
176 {
177         struct wimlib_dir_entry *wdentry;
178         int ret = WIMLIB_ERR_NOMEM;
179
180
181         wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
182                                   (1 + dentry->d_inode->i_num_streams) *
183                                         sizeof(struct wimlib_stream_entry));
184         if (wdentry == NULL)
185                 goto out;
186
187         ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
188         if (ret)
189                 goto out_free_wimlib_dentry;
190
191         if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
192                 ret = (*cb)(wdentry, user_ctx);
193                 if (ret)
194                         goto out_free_wimlib_dentry;
195         }
196
197         if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
198                      WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
199         {
200                 struct wim_dentry *child;
201
202                 ret = 0;
203                 if (default_ignore_case) {
204                         for_dentry_child_case_insensitive(child, dentry) {
205                                 ret = do_iterate_dir_tree(wim, child,
206                                                           flags & ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
207                                                           cb, user_ctx);
208                                 if (ret)
209                                         break;
210                         }
211                 } else {
212                         for_dentry_child(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                 }
220         }
221 out_free_wimlib_dentry:
222         free_wimlib_dentry(wdentry);
223 out:
224         return ret;
225 }
226
227 struct image_iterate_dir_tree_ctx {
228         const tchar *path;
229         int flags;
230         wimlib_iterate_dir_tree_callback_t cb;
231         void *user_ctx;
232 };
233
234
235 static int
236 image_do_iterate_dir_tree(WIMStruct *wim)
237 {
238         struct image_iterate_dir_tree_ctx *ctx = wim->private;
239         struct wim_dentry *dentry;
240
241         dentry = get_dentry(wim, ctx->path, WIMLIB_CASE_PLATFORM_DEFAULT);
242         if (dentry == NULL)
243                 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
244         return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
245 }
246
247 /* API function documented in wimlib.h  */
248 WIMLIBAPI int
249 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *_path,
250                         int flags,
251                         wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
252 {
253         tchar *path;
254         int ret;
255
256         if (flags & ~(WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
257                       WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN |
258                       WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED))
259                 return WIMLIB_ERR_INVALID_PARAM;
260
261         path = canonicalize_wim_path(_path);
262         if (path == NULL)
263                 return WIMLIB_ERR_NOMEM;
264
265         ret = wim_checksum_unhashed_blobs(wim);
266         if (ret)
267                 return ret;
268
269         struct image_iterate_dir_tree_ctx ctx = {
270                 .path = path,
271                 .flags = flags,
272                 .cb = cb,
273                 .user_ctx = user_ctx,
274         };
275         wim->private = &ctx;
276         ret = for_image(wim, image, image_do_iterate_dir_tree);
277         FREE(path);
278         return ret;
279 }