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