]> wimlib.net Git - wimlib/blob - src/iterate_dir.c
inode_fixup.c: Only warn when inconsistent inode detected
[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 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include "wimlib.h"
32 #include "wimlib/dentry.h"
33 #include "wimlib/encoding.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/metadata.h"
36 #include "wimlib/paths.h"
37 #include "wimlib/security.h"
38 #include "wimlib/timestamp.h"
39 #include "wimlib/util.h"
40 #include "wimlib/wim.h"
41
42 static int
43 init_wimlib_dentry(struct wimlib_dir_entry *wdentry,
44                    struct wim_dentry *dentry,
45                    const WIMStruct *wim,
46                    int flags)
47 {
48         int ret;
49         size_t dummy;
50         const struct wim_inode *inode = dentry->d_inode;
51         struct wim_lookup_table_entry *lte;
52         const u8 *hash;
53
54 #if TCHAR_IS_UTF16LE
55         wdentry->filename = dentry->file_name;
56         wdentry->dos_name = dentry->short_name;
57 #else
58         if (dentry_has_long_name(dentry)) {
59                 ret = utf16le_to_tstr(dentry->file_name,
60                                       dentry->file_name_nbytes,
61                                       (tchar**)&wdentry->filename,
62                                       &dummy);
63                 if (ret)
64                         return ret;
65         }
66         if (dentry_has_short_name(dentry)) {
67                 ret = utf16le_to_tstr(dentry->short_name,
68                                       dentry->short_name_nbytes,
69                                       (tchar**)&wdentry->dos_name,
70                                       &dummy);
71                 if (ret)
72                         return ret;
73         }
74 #endif
75         ret = calculate_dentry_full_path(dentry);
76         if (ret)
77                 return ret;
78         wdentry->full_path = dentry->_full_path;
79
80         for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->parent)
81                 wdentry->depth++;
82
83         if (inode->i_security_id >= 0) {
84                 const struct wim_security_data *sd = wim_const_security_data(wim);
85                 wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
86                 wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
87         }
88         wdentry->reparse_tag = inode->i_reparse_tag;
89         wdentry->num_links = inode->i_nlink;
90         wdentry->attributes = inode->i_attributes;
91         wdentry->hard_link_group_id = inode->i_ino;
92         wdentry->creation_time = wim_timestamp_to_timespec(inode->i_creation_time);
93         wdentry->last_write_time = wim_timestamp_to_timespec(inode->i_last_write_time);
94         wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
95
96         lte = inode_unnamed_lte(inode, wim->lookup_table);
97         if (lte) {
98                 lte_to_wimlib_resource_entry(lte, &wdentry->streams[0].resource);
99         } else if (!is_zero_hash(hash = inode_unnamed_stream_hash(inode))) {
100                 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
101                         return stream_not_found_error(inode, hash);
102                 copy_hash(wdentry->streams[0].resource.sha1_hash, hash);
103                 wdentry->streams[0].resource.is_missing = 1;
104         }
105
106         for (unsigned i = 0; i < inode->i_num_ads; i++) {
107                 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
108                         continue;
109                 lte = inode_stream_lte(inode, i + 1, wim->lookup_table);
110                 wdentry->num_named_streams++;
111                 if (lte) {
112                         lte_to_wimlib_resource_entry(lte, &wdentry->streams[
113                                                                 wdentry->num_named_streams].resource);
114                 } else if (!is_zero_hash(hash = inode_stream_hash(inode, i + 1))) {
115                         if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
116                                 return stream_not_found_error(inode, hash);
117                         copy_hash(wdentry->streams[
118                                   wdentry->num_named_streams].resource.sha1_hash, hash);
119                         wdentry->streams[
120                                 wdentry->num_named_streams].resource.is_missing = 1;
121                 }
122         #if TCHAR_IS_UTF16LE
123                 wdentry->streams[wdentry->num_named_streams].stream_name =
124                                 inode->i_ads_entries[i].stream_name;
125         #else
126                 size_t dummy;
127
128                 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
129                                       inode->i_ads_entries[i].stream_name_nbytes,
130                                       (tchar**)&wdentry->streams[
131                                                 wdentry->num_named_streams].stream_name,
132                                       &dummy);
133                 if (ret)
134                         return ret;
135         #endif
136         }
137         return 0;
138 }
139
140 static void
141 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
142 {
143 #if !TCHAR_IS_UTF16LE
144         FREE((tchar*)wdentry->filename);
145         FREE((tchar*)wdentry->dos_name);
146         for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
147                 FREE((tchar*)wdentry->streams[i].stream_name);
148 #endif
149         FREE(wdentry);
150 }
151
152 static int
153 do_iterate_dir_tree(WIMStruct *wim,
154                     struct wim_dentry *dentry, int flags,
155                     wimlib_iterate_dir_tree_callback_t cb,
156                     void *user_ctx)
157 {
158         struct wimlib_dir_entry *wdentry;
159         int ret = WIMLIB_ERR_NOMEM;
160
161
162         wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
163                                   (1 + dentry->d_inode->i_num_ads) *
164                                         sizeof(struct wimlib_stream_entry));
165         if (wdentry == NULL)
166                 goto out;
167
168         ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
169         if (ret)
170                 goto out_free_wimlib_dentry;
171
172         if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
173                 ret = (*cb)(wdentry, user_ctx);
174                 if (ret)
175                         goto out_free_wimlib_dentry;
176         }
177
178         if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
179                      WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
180         {
181                 struct wim_dentry *child;
182
183                 ret = 0;
184                 for_dentry_child(child, dentry) {
185                         ret = do_iterate_dir_tree(wim, child,
186                                                   flags & ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
187                                                   cb, user_ctx);
188                         if (ret)
189                                 break;
190                 }
191         }
192 out_free_wimlib_dentry:
193         free_wimlib_dentry(wdentry);
194 out:
195         return ret;
196 }
197
198 struct image_iterate_dir_tree_ctx {
199         const tchar *path;
200         int flags;
201         wimlib_iterate_dir_tree_callback_t cb;
202         void *user_ctx;
203 };
204
205
206 static int
207 image_do_iterate_dir_tree(WIMStruct *wim)
208 {
209         struct image_iterate_dir_tree_ctx *ctx = wim->private;
210         struct wim_dentry *dentry;
211
212         dentry = get_dentry(wim, ctx->path, WIMLIB_CASE_PLATFORM_DEFAULT);
213         if (dentry == NULL)
214                 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
215         return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
216 }
217
218 /* API function documented in wimlib.h  */
219 WIMLIBAPI int
220 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *_path,
221                         int flags,
222                         wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
223 {
224         tchar *path;
225         int ret;
226
227         if (flags & ~(WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
228                       WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN |
229                       WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED))
230                 return WIMLIB_ERR_INVALID_PARAM;
231
232         path = canonicalize_wim_path(_path);
233         if (path == NULL)
234                 return WIMLIB_ERR_NOMEM;
235         struct image_iterate_dir_tree_ctx ctx = {
236                 .path = path,
237                 .flags = flags,
238                 .cb = cb,
239                 .user_ctx = user_ctx,
240         };
241         wim->private = &ctx;
242         ret = for_image(wim, image, image_do_iterate_dir_tree);
243         FREE(path);
244         return ret;
245 }