8e7f6cc3878d6c59c8229b20ee65c2dda795cc13
[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 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/dentry.h"
31 #include "wimlib/encoding.h"
32 #include "wimlib/lookup_table.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 init_wimlib_dentry(struct wimlib_dir_entry *wdentry, struct wim_dentry *dentry,
43                    WIMStruct *wim, int flags)
44 {
45         int ret;
46         size_t dummy;
47         const struct wim_inode *inode = dentry->d_inode;
48         struct wim_lookup_table_entry *lte;
49         const u8 *hash;
50         struct wimlib_unix_data unix_data;
51
52         ret = utf16le_get_tstr(dentry->file_name, dentry->file_name_nbytes,
53                                &wdentry->filename, &dummy);
54         if (ret)
55                 return ret;
56
57         ret = utf16le_get_tstr(dentry->short_name, dentry->short_name_nbytes,
58                                &wdentry->dos_name, &dummy);
59         if (ret)
60                 return ret;
61
62         ret = calculate_dentry_full_path(dentry);
63         if (ret)
64                 return ret;
65         wdentry->full_path = dentry->_full_path;
66
67         for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->d_parent)
68                 wdentry->depth++;
69
70         if (inode->i_security_id >= 0) {
71                 struct wim_security_data *sd;
72
73                 sd = wim_get_current_security_data(wim);
74                 wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
75                 wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
76         }
77         wdentry->reparse_tag = inode->i_reparse_tag;
78         wdentry->num_links = inode->i_nlink;
79         wdentry->attributes = inode->i_attributes;
80         wdentry->hard_link_group_id = inode->i_ino;
81         wdentry->creation_time = wim_timestamp_to_timespec(inode->i_creation_time);
82         wdentry->last_write_time = wim_timestamp_to_timespec(inode->i_last_write_time);
83         wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
84         if (inode_get_unix_data(inode, &unix_data)) {
85                 wdentry->unix_uid = unix_data.uid;
86                 wdentry->unix_gid = unix_data.gid;
87                 wdentry->unix_mode = unix_data.mode;
88                 wdentry->unix_rdev = unix_data.rdev;
89         }
90
91         lte = inode_unnamed_lte(inode, wim->lookup_table);
92         if (lte) {
93                 lte_to_wimlib_resource_entry(lte, &wdentry->streams[0].resource);
94         } else if (!is_zero_hash(hash = inode_unnamed_stream_hash(inode))) {
95                 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
96                         return stream_not_found_error(inode, hash);
97                 copy_hash(wdentry->streams[0].resource.sha1_hash, hash);
98                 wdentry->streams[0].resource.is_missing = 1;
99         }
100
101         for (unsigned i = 0; i < inode->i_num_ads; i++) {
102                 if (!inode->i_ads_entries[i].stream_name_nbytes)
103                         continue;
104                 lte = inode_stream_lte(inode, i + 1, wim->lookup_table);
105                 wdentry->num_named_streams++;
106                 if (lte) {
107                         lte_to_wimlib_resource_entry(lte, &wdentry->streams[
108                                                                 wdentry->num_named_streams].resource);
109                 } else if (!is_zero_hash(hash = inode_stream_hash(inode, i + 1))) {
110                         if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
111                                 return stream_not_found_error(inode, hash);
112                         copy_hash(wdentry->streams[
113                                   wdentry->num_named_streams].resource.sha1_hash, hash);
114                         wdentry->streams[
115                                 wdentry->num_named_streams].resource.is_missing = 1;
116                 }
117
118                 size_t dummy;
119
120                 ret = utf16le_get_tstr(inode->i_ads_entries[i].stream_name,
121                                        inode->i_ads_entries[i].stream_name_nbytes,
122                                        &wdentry->streams[
123                                                wdentry->num_named_streams].stream_name,
124                                        &dummy);
125                 if (ret)
126                         return ret;
127         }
128         return 0;
129 }
130
131 static void
132 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
133 {
134         utf16le_put_tstr(wdentry->filename);
135         utf16le_put_tstr(wdentry->dos_name);
136         for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
137                 utf16le_put_tstr(wdentry->streams[i].stream_name);
138         FREE(wdentry);
139 }
140
141 static int
142 do_iterate_dir_tree(WIMStruct *wim,
143                     struct wim_dentry *dentry, int flags,
144                     wimlib_iterate_dir_tree_callback_t cb,
145                     void *user_ctx)
146 {
147         struct wimlib_dir_entry *wdentry;
148         int ret = WIMLIB_ERR_NOMEM;
149
150
151         wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
152                                   (1 + dentry->d_inode->i_num_ads) *
153                                         sizeof(struct wimlib_stream_entry));
154         if (wdentry == NULL)
155                 goto out;
156
157         ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
158         if (ret)
159                 goto out_free_wimlib_dentry;
160
161         if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
162                 ret = (*cb)(wdentry, user_ctx);
163                 if (ret)
164                         goto out_free_wimlib_dentry;
165         }
166
167         if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
168                      WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
169         {
170                 struct wim_dentry *child;
171
172                 ret = 0;
173                 for_dentry_child(child, dentry) {
174                         ret = do_iterate_dir_tree(wim, child,
175                                                   flags & ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
176                                                   cb, user_ctx);
177                         if (ret)
178                                 break;
179                 }
180         }
181 out_free_wimlib_dentry:
182         free_wimlib_dentry(wdentry);
183 out:
184         return ret;
185 }
186
187 struct image_iterate_dir_tree_ctx {
188         const tchar *path;
189         int flags;
190         wimlib_iterate_dir_tree_callback_t cb;
191         void *user_ctx;
192 };
193
194
195 static int
196 image_do_iterate_dir_tree(WIMStruct *wim)
197 {
198         struct image_iterate_dir_tree_ctx *ctx = wim->private;
199         struct wim_dentry *dentry;
200
201         dentry = get_dentry(wim, ctx->path, WIMLIB_CASE_PLATFORM_DEFAULT);
202         if (dentry == NULL)
203                 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
204         return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
205 }
206
207 /* API function documented in wimlib.h  */
208 WIMLIBAPI int
209 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *_path,
210                         int flags,
211                         wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
212 {
213         tchar *path;
214         int ret;
215
216         if (flags & ~(WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
217                       WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN |
218                       WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED))
219                 return WIMLIB_ERR_INVALID_PARAM;
220
221         path = canonicalize_wim_path(_path);
222         if (path == NULL)
223                 return WIMLIB_ERR_NOMEM;
224         struct image_iterate_dir_tree_ctx ctx = {
225                 .path = path,
226                 .flags = flags,
227                 .cb = cb,
228                 .user_ctx = user_ctx,
229         };
230         wim->private = &ctx;
231         ret = for_image(wim, image, image_do_iterate_dir_tree);
232         FREE(path);
233         return ret;
234 }