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