]> wimlib.net Git - wimlib/blob - src/iterate_dir.c
New format for UNIX data
[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, struct wim_dentry *dentry,
44                    WIMStruct *wim, int flags)
45 {
46         int ret;
47         size_t dummy;
48         const struct wim_inode *inode = dentry->d_inode;
49         struct wim_lookup_table_entry *lte;
50         const u8 *hash;
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->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
85         wdentry->unix_uid = inode->i_unix_data.uid;
86         wdentry->unix_gid = inode->i_unix_data.gid;
87         wdentry->unix_mode = inode->i_unix_data.mode;
88         wdentry->unix_reserved = inode->i_unix_data.reserved;
89
90         lte = inode_unnamed_lte(inode, wim->lookup_table);
91         if (lte) {
92                 lte_to_wimlib_resource_entry(lte, &wdentry->streams[0].resource);
93         } else if (!is_zero_hash(hash = inode_unnamed_stream_hash(inode))) {
94                 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
95                         return stream_not_found_error(inode, hash);
96                 copy_hash(wdentry->streams[0].resource.sha1_hash, hash);
97                 wdentry->streams[0].resource.is_missing = 1;
98         }
99
100         for (unsigned i = 0; i < inode->i_num_ads; i++) {
101                 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
102                         continue;
103                 lte = inode_stream_lte(inode, i + 1, wim->lookup_table);
104                 wdentry->num_named_streams++;
105                 if (lte) {
106                         lte_to_wimlib_resource_entry(lte, &wdentry->streams[
107                                                                 wdentry->num_named_streams].resource);
108                 } else if (!is_zero_hash(hash = inode_stream_hash(inode, i + 1))) {
109                         if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
110                                 return stream_not_found_error(inode, hash);
111                         copy_hash(wdentry->streams[
112                                   wdentry->num_named_streams].resource.sha1_hash, hash);
113                         wdentry->streams[
114                                 wdentry->num_named_streams].resource.is_missing = 1;
115                 }
116
117                 size_t dummy;
118
119                 ret = utf16le_get_tstr(inode->i_ads_entries[i].stream_name,
120                                        inode->i_ads_entries[i].stream_name_nbytes,
121                                        &wdentry->streams[
122                                                wdentry->num_named_streams].stream_name,
123                                        &dummy);
124                 if (ret)
125                         return ret;
126         }
127         return 0;
128 }
129
130 static void
131 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
132 {
133         utf16le_put_tstr(wdentry->filename);
134         utf16le_put_tstr(wdentry->dos_name);
135         for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
136                 utf16le_put_tstr(wdentry->streams[i].stream_name);
137         FREE(wdentry);
138 }
139
140 static int
141 do_iterate_dir_tree(WIMStruct *wim,
142                     struct wim_dentry *dentry, int flags,
143                     wimlib_iterate_dir_tree_callback_t cb,
144                     void *user_ctx)
145 {
146         struct wimlib_dir_entry *wdentry;
147         int ret = WIMLIB_ERR_NOMEM;
148
149
150         wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
151                                   (1 + dentry->d_inode->i_num_ads) *
152                                         sizeof(struct wimlib_stream_entry));
153         if (wdentry == NULL)
154                 goto out;
155
156         ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
157         if (ret)
158                 goto out_free_wimlib_dentry;
159
160         if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
161                 ret = (*cb)(wdentry, user_ctx);
162                 if (ret)
163                         goto out_free_wimlib_dentry;
164         }
165
166         if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
167                      WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
168         {
169                 struct wim_dentry *child;
170
171                 ret = 0;
172                 for_dentry_child(child, dentry) {
173                         ret = do_iterate_dir_tree(wim, child,
174                                                   flags & ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
175                                                   cb, user_ctx);
176                         if (ret)
177                                 break;
178                 }
179         }
180 out_free_wimlib_dentry:
181         free_wimlib_dentry(wdentry);
182 out:
183         return ret;
184 }
185
186 struct image_iterate_dir_tree_ctx {
187         const tchar *path;
188         int flags;
189         wimlib_iterate_dir_tree_callback_t cb;
190         void *user_ctx;
191 };
192
193
194 static int
195 image_do_iterate_dir_tree(WIMStruct *wim)
196 {
197         struct image_iterate_dir_tree_ctx *ctx = wim->private;
198         struct wim_dentry *dentry;
199
200         dentry = get_dentry(wim, ctx->path, WIMLIB_CASE_PLATFORM_DEFAULT);
201         if (dentry == NULL)
202                 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
203         return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
204 }
205
206 /* API function documented in wimlib.h  */
207 WIMLIBAPI int
208 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *_path,
209                         int flags,
210                         wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
211 {
212         tchar *path;
213         int ret;
214
215         if (flags & ~(WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
216                       WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN |
217                       WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED))
218                 return WIMLIB_ERR_INVALID_PARAM;
219
220         path = canonicalize_wim_path(_path);
221         if (path == NULL)
222                 return WIMLIB_ERR_NOMEM;
223         struct image_iterate_dir_tree_ctx ctx = {
224                 .path = path,
225                 .flags = flags,
226                 .cb = cb,
227                 .user_ctx = user_ctx,
228         };
229         wim->private = &ctx;
230         ret = for_image(wim, image, image_do_iterate_dir_tree);
231         FREE(path);
232         return ret;
233 }