Refactor some of the dentry, inode, and lookup table code
[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 struct iterate_dir_tree_ctx {
153         WIMStruct *wim;
154         int flags;
155         wimlib_iterate_dir_tree_callback_t cb;
156         void *user_ctx;
157 };
158
159 static int
160 do_iterate_dir_tree(WIMStruct *wim,
161                     struct wim_dentry *dentry, int flags,
162                     wimlib_iterate_dir_tree_callback_t cb,
163                     void *user_ctx);
164
165 static int
166 call_do_iterate_dir_tree(struct wim_dentry *dentry, void *_ctx)
167 {
168         struct iterate_dir_tree_ctx *ctx = _ctx;
169         return do_iterate_dir_tree(ctx->wim, dentry, ctx->flags,
170                                    ctx->cb, ctx->user_ctx);
171 }
172
173 static int
174 do_iterate_dir_tree(WIMStruct *wim,
175                     struct wim_dentry *dentry, int flags,
176                     wimlib_iterate_dir_tree_callback_t cb,
177                     void *user_ctx)
178 {
179         struct wimlib_dir_entry *wdentry;
180         int ret = WIMLIB_ERR_NOMEM;
181
182
183         wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
184                                   (1 + dentry->d_inode->i_num_ads) *
185                                         sizeof(struct wimlib_stream_entry));
186         if (wdentry == NULL)
187                 goto out;
188
189         ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
190         if (ret)
191                 goto out_free_wimlib_dentry;
192
193         if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
194                 ret = (*cb)(wdentry, user_ctx);
195                 if (ret)
196                         goto out_free_wimlib_dentry;
197         }
198
199         if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
200                      WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
201         {
202                 struct iterate_dir_tree_ctx ctx = {
203                         .wim      = wim,
204                         .flags    = flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
205                         .cb       = cb,
206                         .user_ctx = user_ctx,
207                 };
208                 ret = for_dentry_child(dentry, call_do_iterate_dir_tree, &ctx);
209         }
210 out_free_wimlib_dentry:
211         free_wimlib_dentry(wdentry);
212 out:
213         return ret;
214 }
215
216 struct image_iterate_dir_tree_ctx {
217         const tchar *path;
218         int flags;
219         wimlib_iterate_dir_tree_callback_t cb;
220         void *user_ctx;
221 };
222
223
224 static int
225 image_do_iterate_dir_tree(WIMStruct *wim)
226 {
227         struct image_iterate_dir_tree_ctx *ctx = wim->private;
228         struct wim_dentry *dentry;
229
230         dentry = get_dentry(wim, ctx->path, WIMLIB_CASE_PLATFORM_DEFAULT);
231         if (dentry == NULL)
232                 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
233         return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
234 }
235
236 /* API function documented in wimlib.h  */
237 WIMLIBAPI int
238 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *_path,
239                         int flags,
240                         wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
241 {
242         tchar *path;
243         int ret;
244
245         path = canonicalize_wim_path(_path);
246         if (path == NULL)
247                 return WIMLIB_ERR_NOMEM;
248         struct image_iterate_dir_tree_ctx ctx = {
249                 .path = path,
250                 .flags = flags,
251                 .cb = cb,
252                 .user_ctx = user_ctx,
253         };
254         wim->private = &ctx;
255         ret = for_image(wim, image, image_do_iterate_dir_tree);
256         FREE(path);
257         return ret;
258 }