53a44cf185df83c6a9f15edcda7c2245923b523b
[wimlib] / src / dentry.c
1 /*
2  * dentry.c
3  *
4  * In the WIM file format, the dentries are stored in the "metadata resource"
5  * section right after the security data.  Each image in the WIM file has its
6  * own metadata resource with its own security data and dentry tree.  Dentries
7  * in different images may share file resources by referring to the same lookup
8  * table entries.
9  */
10
11 /*
12  * Copyright (C) 2012, 2013 Eric Biggers
13  *
14  * This file is part of wimlib, a library for working with WIM files.
15  *
16  * wimlib is free software; you can redistribute it and/or modify it under the
17  * terms of the GNU General Public License as published by the Free Software
18  * Foundation; either version 3 of the License, or (at your option) any later
19  * version.
20  *
21  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
22  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
23  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License along with
26  * wimlib; if not, see http://www.gnu.org/licenses/.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #  include "config.h"
31 #endif
32
33 #include "wimlib.h"
34 #include "wimlib/dentry.h"
35 #include "wimlib/encoding.h"
36 #include "wimlib/endianness.h"
37 #include "wimlib/error.h"
38 #include "wimlib/lookup_table.h"
39 #include "wimlib/metadata.h"
40 #include "wimlib/resource.h"
41 #include "wimlib/security.h"
42 #include "wimlib/sha1.h"
43 #include "wimlib/timestamp.h"
44
45 #include <errno.h>
46
47 /* WIM alternate data stream entry (on-disk format) */
48 struct wim_ads_entry_on_disk {
49         /*  Length of the entry, in bytes.  This apparently includes all
50          *  fixed-length fields, plus the stream name and null terminator if
51          *  present, and the padding up to an 8 byte boundary.  wimlib is a
52          *  little less strict when reading the entries, and only requires that
53          *  the number of bytes from this field is at least as large as the size
54          *  of the fixed length fields and stream name without null terminator.
55          *  */
56         le64  length;
57
58         le64  reserved;
59
60         /* SHA1 message digest of the uncompressed stream; or, alternatively,
61          * can be all zeroes if the stream has zero length. */
62         u8 hash[SHA1_HASH_SIZE];
63
64         /* Length of the stream name, in bytes.  0 if the stream is unnamed.  */
65         le16 stream_name_nbytes;
66
67         /* Stream name in UTF-16LE.  It is @stream_name_nbytes bytes long,
68          * excluding the the null terminator.  There is a null terminator
69          * character if @stream_name_nbytes != 0; i.e., if this stream is named.
70          * */
71         utf16lechar stream_name[];
72 } _packed_attribute;
73
74 #define WIM_ADS_ENTRY_DISK_SIZE 38
75
76 /* On-disk format of a WIM dentry (directory entry), located in the metadata
77  * resource for a WIM image.  */
78 struct wim_dentry_on_disk {
79
80         /* Length of this directory entry in bytes, not including any alternate
81          * data stream entries.  Should be a multiple of 8 so that the following
82          * dentry or alternate data stream entry is aligned on an 8-byte
83          * boundary.  (If not, wimlib will round it up.)  It must be at least as
84          * long as the fixed-length fields of the dentry (WIM_DENTRY_DISK_SIZE),
85          * plus the lengths of the file name and/or short name if present.
86          *
87          * It is also possible for this field to be 0.  This situation, which is
88          * undocumented, indicates the end of a list of sibling nodes in a
89          * directory.  It also means the real length is 8, because the dentry
90          * included only the length field, but that takes up 8 bytes.  */
91         le64 length;
92
93         /* Attributes of the file or directory.  This is a bitwise OR of the
94          * FILE_ATTRIBUTE_* constants and should correspond to the value
95          * retrieved by GetFileAttributes() on Windows. */
96         le32 attributes;
97
98         /* A value that specifies the security descriptor for this file or
99          * directory.  If -1, the file or directory has no security descriptor.
100          * Otherwise, it is a 0-based index into the WIM image's table of
101          * security descriptors (see: `struct wim_security_data') */
102         sle32 security_id;
103
104         /* Offset, in bytes, from the start of the uncompressed metadata
105          * resource of this directory's child directory entries, or 0 if this
106          * directory entry does not correspond to a directory or otherwise does
107          * not have any children. */
108         le64 subdir_offset;
109
110         /* Reserved fields */
111         le64 unused_1;
112         le64 unused_2;
113
114
115         /* Creation time, last access time, and last write time, in
116          * 100-nanosecond intervals since 12:00 a.m UTC January 1, 1601.  They
117          * should correspond to the times gotten by calling GetFileTime() on
118          * Windows. */
119         le64 creation_time;
120         le64 last_access_time;
121         le64 last_write_time;
122
123         /* Vaguely, the SHA-1 message digest ("hash") of the file's contents.
124          * More specifically, this is for the "unnamed data stream" rather than
125          * any "alternate data streams".  This hash value is used to look up the
126          * corresponding entry in the WIM's stream lookup table to actually find
127          * the file contents within the WIM.
128          *
129          * If the file has no unnamed data stream (e.g. is a directory), then
130          * this field will be all zeroes.  If the unnamed data stream is empty
131          * (i.e. an "empty file"), then this field is also expected to be all
132          * zeroes.  (It will be if wimlib created the WIM image, at least;
133          * otherwise it can't be ruled out that the SHA-1 message digest of 0
134          * bytes of data is given explicitly.)
135          *
136          * If the file has reparse data, then this field will instead specify
137          * the SHA-1 message digest of the reparse data.  If it is somehow
138          * possible for a file to have both an unnamed data stream and reparse
139          * data, then this is not handled by wimlib.
140          *
141          * As a further special case, if this field is all zeroes but there is
142          * an alternate data stream entry with no name and a nonzero SHA-1
143          * message digest field, then that hash must be used instead of this
144          * one.  In fact, when named data streams are present, some versions of
145          * Windows PE contain a bug where they only look in the alternate data
146          * stream entries for the unnamed data stream, not here.
147          */
148         u8 unnamed_stream_hash[SHA1_HASH_SIZE];
149
150         /* The format of the following data is not yet completely known and they
151          * do not correspond to Microsoft's documentation.
152          *
153          * If this directory entry is for a reparse point (has
154          * FILE_ATTRIBUTE_REPARSE_POINT set in the attributes field), then the
155          * version of the following fields containing the reparse tag is valid.
156          * Furthermore, the field notated as not_rpfixed, as far as I can tell,
157          * is supposed to be set to 1 if reparse point fixups (a.k.a. fixing the
158          * targets of absolute symbolic links) were *not* done, and otherwise 0.
159          *
160          * If this directory entry is not for a reparse point, then the version
161          * of the following fields containing the hard_link_group_id is valid.
162          * All MS says about this field is that "If this file is part of a hard
163          * link set, all the directory entries in the set will share the same
164          * value in this field.".  However, more specifically I have observed
165          * the following:
166          *    - If the file is part of a hard link set of size 1, then the
167          *    hard_link_group_id should be set to either 0, which is treated
168          *    specially as indicating "not hardlinked", or any unique value.
169          *    - The specific nonzero values used to identity hard link sets do
170          *    not matter, as long as they are unique.
171          *    - However, due to bugs in Microsoft's software, it is actually NOT
172          *    guaranteed that directory entries that share the same hard link
173          *    group ID are actually hard linked to each either.  We have to
174          *    handle this by using special code to use distinguishing features
175          *    (which is possible because some information about the underlying
176          *    inode is repeated in each dentry) to split up these fake hard link
177          *    groups into what they actually are supposed to be.
178          */
179         union {
180                 struct {
181                         le32 rp_unknown_1;
182                         le32 reparse_tag;
183                         le16 rp_unknown_2;
184                         le16 not_rpfixed;
185                 } _packed_attribute reparse;
186                 struct {
187                         le32 rp_unknown_1;
188                         le64 hard_link_group_id;
189                 } _packed_attribute nonreparse;
190         };
191
192         /* Number of alternate data stream entries that directly follow this
193          * dentry on-disk. */
194         le16 num_alternate_data_streams;
195
196         /* Length of this file's UTF-16LE encoded short name (8.3 DOS-compatible
197          * name), if present, in bytes, excluding the null terminator.  If this
198          * file has no short name, then this field should be 0.  */
199         le16 short_name_nbytes;
200
201         /* Length of this file's UTF-16LE encoded "long" name, excluding the
202          * null terminator.  If this file has no short name, then this field
203          * should be 0.  It's expected that only the root dentry has this field
204          * set to 0.  */
205         le16 file_name_nbytes;
206
207         /* Followed by variable length file name, in UTF16-LE, if
208          * file_name_nbytes != 0.  Includes null terminator. */
209         /*utf16lechar file_name[];*/
210
211         /* Followed by variable length short name, in UTF16-LE, if
212          * short_name_nbytes != 0.  Includes null terminator. */
213         /*utf16lechar short_name[];*/
214 } _packed_attribute;
215
216 #define WIM_DENTRY_DISK_SIZE 102
217
218 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
219  * a file name and short name that take the specified numbers of bytes.  This
220  * excludes any alternate data stream entries that may follow the dentry. */
221 static u64
222 dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
223 {
224         u64 length = sizeof(struct wim_dentry_on_disk);
225         if (file_name_nbytes)
226                 length += file_name_nbytes + 2;
227         if (short_name_nbytes)
228                 length += short_name_nbytes + 2;
229         return length;
230 }
231
232 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
233  * the file name length and short name length.  Note that dentry->length is
234  * ignored; also, this excludes any alternate data stream entries that may
235  * follow the dentry. */
236 static u64
237 dentry_correct_length_aligned(const struct wim_dentry *dentry)
238 {
239         u64 len;
240
241         len = dentry_correct_length_unaligned(dentry->file_name_nbytes,
242                                               dentry->short_name_nbytes);
243         return (len + 7) & ~7;
244 }
245
246 /* Duplicates a string of system-dependent encoding into a UTF-16LE string and
247  * returns the string and its length, in bytes, in the pointer arguments.  Frees
248  * any existing string at the return location before overwriting it. */
249 static int
250 get_utf16le_name(const tchar *name, utf16lechar **name_utf16le_ret,
251                  u16 *name_utf16le_nbytes_ret)
252 {
253         utf16lechar *name_utf16le;
254         size_t name_utf16le_nbytes;
255         int ret;
256 #if TCHAR_IS_UTF16LE
257         name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
258         name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
259         if (name_utf16le == NULL)
260                 return WIMLIB_ERR_NOMEM;
261         memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
262         ret = 0;
263 #else
264
265         ret = tstr_to_utf16le(name, tstrlen(name), &name_utf16le,
266                               &name_utf16le_nbytes);
267         if (ret == 0) {
268                 if (name_utf16le_nbytes > 0xffff) {
269                         FREE(name_utf16le);
270                         ERROR("Multibyte string \"%"TS"\" is too long!", name);
271                         ret = WIMLIB_ERR_INVALID_UTF8_STRING;
272                 }
273         }
274 #endif
275         if (ret == 0) {
276                 FREE(*name_utf16le_ret);
277                 *name_utf16le_ret = name_utf16le;
278                 *name_utf16le_nbytes_ret = name_utf16le_nbytes;
279         }
280         return ret;
281 }
282
283 /* Sets the name of a WIM dentry from a multibyte string. */
284 int
285 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name)
286 {
287         int ret;
288         ret = get_utf16le_name(new_name, &dentry->file_name,
289                                &dentry->file_name_nbytes);
290         if (ret == 0) {
291                 /* Clear the short name and recalculate the dentry length */
292                 if (dentry_has_short_name(dentry)) {
293                         FREE(dentry->short_name);
294                         dentry->short_name = NULL;
295                         dentry->short_name_nbytes = 0;
296                 }
297         }
298         return ret;
299 }
300
301 /* Returns the total length of a WIM alternate data stream entry on-disk,
302  * including the stream name, the null terminator, AND the padding after the
303  * entry to align the next ADS entry or dentry on an 8-byte boundary. */
304 static u64
305 ads_entry_total_length(const struct wim_ads_entry *entry)
306 {
307         u64 len = sizeof(struct wim_ads_entry_on_disk);
308         if (entry->stream_name_nbytes)
309                 len += entry->stream_name_nbytes + 2;
310         return (len + 7) & ~7;
311 }
312
313 /*
314  * Determine whether to include a "dummy" stream when writing a WIM dentry:
315  *
316  * Some versions of Microsoft's WIM software (the boot driver(s) in WinPE 3.0,
317  * for example) contain a bug where they assume the first alternate data stream
318  * (ADS) entry of a dentry with a nonzero ADS count specifies the unnamed
319  * stream, even if it has a name and the unnamed stream is already specified in
320  * the hash field of the dentry itself.
321  *
322  * wimlib has to work around this behavior by carefully emulating the behavior
323  * of (most versions of) ImageX/WIMGAPI, which move the unnamed stream reference
324  * into the alternate stream entries whenever there are named data streams, even
325  * though there is already a field in the dentry itself for the unnamed stream
326  * reference, which then goes to waste.
327  */
328 static inline bool inode_needs_dummy_stream(const struct wim_inode *inode)
329 {
330         return (inode->i_num_ads > 0 &&
331                 inode->i_num_ads < 0xffff && /* overflow check */
332                 inode->i_canonical_streams); /* assume the dentry is okay if it
333                                                 already had an unnamed ADS entry
334                                                 when it was read in  */
335 }
336
337 /* Calculate the total number of bytes that will be consumed when a WIM dentry
338  * is written.  This includes base dentry and name fields as well as all
339  * alternate data stream entries and alignment bytes.  */
340 u64
341 dentry_out_total_length(const struct wim_dentry *dentry)
342 {
343         u64 length = dentry_correct_length_aligned(dentry);
344         const struct wim_inode *inode = dentry->d_inode;
345
346         if (inode_needs_dummy_stream(inode))
347                 length += ads_entry_total_length(&(struct wim_ads_entry){});
348
349         for (u16 i = 0; i < inode->i_num_ads; i++)
350                 length += ads_entry_total_length(&inode->i_ads_entries[i]);
351
352         return length;
353 }
354
355 /* Calculate the aligned, total length of a dentry, including all alternate data
356  * stream entries.  Uses dentry->length.  */
357 static u64
358 dentry_in_total_length(const struct wim_dentry *dentry)
359 {
360         u64 length = dentry->length;
361         const struct wim_inode *inode = dentry->d_inode;
362         for (u16 i = 0; i < inode->i_num_ads; i++)
363                 length += ads_entry_total_length(&inode->i_ads_entries[i]);
364         return (length + 7) & ~7;
365 }
366
367 int
368 for_dentry_in_rbtree(struct rb_node *root,
369                      int (*visitor)(struct wim_dentry *, void *),
370                      void *arg)
371 {
372         int ret;
373         struct rb_node *node = root;
374         LIST_HEAD(stack);
375         while (1) {
376                 if (node) {
377                         list_add(&rbnode_dentry(node)->tmp_list, &stack);
378                         node = node->rb_left;
379                 } else {
380                         struct list_head *next;
381                         struct wim_dentry *dentry;
382
383                         next = stack.next;
384                         if (next == &stack)
385                                 return 0;
386                         dentry = container_of(next, struct wim_dentry, tmp_list);
387                         list_del(next);
388                         ret = visitor(dentry, arg);
389                         if (ret != 0)
390                                 return ret;
391                         node = dentry->rb_node.rb_right;
392                 }
393         }
394 }
395
396 static int
397 for_dentry_tree_in_rbtree_depth(struct rb_node *node,
398                                 int (*visitor)(struct wim_dentry*, void*),
399                                 void *arg)
400 {
401         int ret;
402         if (node) {
403                 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
404                                                       visitor, arg);
405                 if (ret != 0)
406                         return ret;
407                 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
408                                                       visitor, arg);
409                 if (ret != 0)
410                         return ret;
411                 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
412                 if (ret != 0)
413                         return ret;
414         }
415         return 0;
416 }
417
418 static int
419 for_dentry_tree_in_rbtree(struct rb_node *node,
420                           int (*visitor)(struct wim_dentry*, void*),
421                           void *arg)
422 {
423         int ret;
424         if (node) {
425                 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
426                 if (ret)
427                         return ret;
428                 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
429                 if (ret)
430                         return ret;
431                 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
432                 if (ret)
433                         return ret;
434         }
435         return 0;
436 }
437
438 /*
439  * Iterate over all children of @dentry, calling the function @visitor, passing
440  * it a child dentry and the extra argument @arg.
441  *
442  * Note: this function iterates over ALL child dentries, even those with the
443  * same case-insensitive name.
444  *
445  * Note: this function clobbers the tmp_list field of the child dentries.  */
446 int
447 for_dentry_child(const struct wim_dentry *dentry,
448                  int (*visitor)(struct wim_dentry *, void *),
449                  void *arg)
450 {
451         return for_dentry_in_rbtree(dentry->d_inode->i_children.rb_node,
452                                     visitor,
453                                     arg);
454 }
455
456 /* Calls a function on all directory entries in a WIM dentry tree.  Logically,
457  * this is a pre-order traversal (the function is called on a parent dentry
458  * before its children), but sibling dentries will be visited in order as well.
459  * */
460 int
461 for_dentry_in_tree(struct wim_dentry *root,
462                    int (*visitor)(struct wim_dentry*, void*), void *arg)
463 {
464         int ret;
465
466         if (root == NULL)
467                 return 0;
468         ret = (*visitor)(root, arg);
469         if (ret)
470                 return ret;
471         return for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node,
472                                          visitor,
473                                          arg);
474 }
475
476 /* Like for_dentry_in_tree(), but the visitor function is always called on a
477  * dentry's children before on itself. */
478 int
479 for_dentry_in_tree_depth(struct wim_dentry *root,
480                          int (*visitor)(struct wim_dentry*, void*), void *arg)
481 {
482         int ret;
483
484         if (root == NULL)
485                 return 0;
486         ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
487                                               visitor, arg);
488         if (ret)
489                 return ret;
490         return (*visitor)(root, arg);
491 }
492
493 /* Calculate the full path of @dentry.  The full path of its parent must have
494  * already been calculated, or it must be the root dentry. */
495 int
496 calculate_dentry_full_path(struct wim_dentry *dentry)
497 {
498         tchar *full_path;
499         u32 full_path_nbytes;
500         int ret;
501
502         if (dentry->_full_path)
503                 return 0;
504
505         if (dentry_is_root(dentry)) {
506                 static const tchar _root_path[] = {WIM_PATH_SEPARATOR, T('\0')};
507                 full_path = TSTRDUP(_root_path);
508                 if (full_path == NULL)
509                         return WIMLIB_ERR_NOMEM;
510                 full_path_nbytes = 1 * sizeof(tchar);
511         } else {
512                 struct wim_dentry *parent;
513                 tchar *parent_full_path;
514                 u32 parent_full_path_nbytes;
515                 size_t filename_nbytes;
516
517                 parent = dentry->parent;
518                 if (dentry_is_root(parent)) {
519                         parent_full_path = T("");
520                         parent_full_path_nbytes = 0;
521                 } else {
522                         if (parent->_full_path == NULL) {
523                                 ret = calculate_dentry_full_path(parent);
524                                 if (ret)
525                                         return ret;
526                         }
527                         parent_full_path = parent->_full_path;
528                         parent_full_path_nbytes = parent->full_path_nbytes;
529                 }
530
531                 /* Append this dentry's name as a tchar string to the full path
532                  * of the parent followed by the path separator */
533         #if TCHAR_IS_UTF16LE
534                 filename_nbytes = dentry->file_name_nbytes;
535         #else
536                 {
537                         int ret = utf16le_to_tstr_nbytes(dentry->file_name,
538                                                          dentry->file_name_nbytes,
539                                                          &filename_nbytes);
540                         if (ret)
541                                 return ret;
542                 }
543         #endif
544
545                 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
546                                    filename_nbytes;
547                 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
548                 if (full_path == NULL)
549                         return WIMLIB_ERR_NOMEM;
550                 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
551                 full_path[parent_full_path_nbytes / sizeof(tchar)] = WIM_PATH_SEPARATOR;
552         #if TCHAR_IS_UTF16LE
553                 memcpy(&full_path[parent_full_path_nbytes / sizeof(tchar) + 1],
554                        dentry->file_name,
555                        filename_nbytes + sizeof(tchar));
556         #else
557                 utf16le_to_tstr_buf(dentry->file_name,
558                                     dentry->file_name_nbytes,
559                                     &full_path[parent_full_path_nbytes /
560                                                sizeof(tchar) + 1]);
561         #endif
562         }
563         dentry->_full_path = full_path;
564         dentry->full_path_nbytes= full_path_nbytes;
565         return 0;
566 }
567
568 static int
569 do_calculate_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
570 {
571         return calculate_dentry_full_path(dentry);
572 }
573
574 int
575 calculate_dentry_tree_full_paths(struct wim_dentry *root)
576 {
577         return for_dentry_in_tree(root, do_calculate_dentry_full_path, NULL);
578 }
579
580 tchar *
581 dentry_full_path(struct wim_dentry *dentry)
582 {
583         calculate_dentry_full_path(dentry);
584         return dentry->_full_path;
585 }
586
587 static int
588 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
589 {
590         *(u64*)subdir_offset_p += dentry_out_total_length(dentry);
591         return 0;
592 }
593
594 static int
595 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
596 {
597         calculate_subdir_offsets(dentry, subdir_offset_p);
598         return 0;
599 }
600
601 /*
602  * Recursively calculates the subdir offsets for a directory tree.
603  *
604  * @dentry:  The root of the directory tree.
605  * @subdir_offset_p:  The current subdirectory offset; i.e., the subdirectory
606  *                    offset for @dentry.
607  */
608 void
609 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
610 {
611         struct rb_node *node;
612
613         dentry->subdir_offset = *subdir_offset_p;
614         node = dentry->d_inode->i_children.rb_node;
615         if (node) {
616                 /* Advance the subdir offset by the amount of space the children
617                  * of this dentry take up. */
618                 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
619
620                 /* End-of-directory dentry on disk. */
621                 *subdir_offset_p += 8;
622
623                 /* Recursively call calculate_subdir_offsets() on all the
624                  * children. */
625                 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
626         } else {
627                 /* On disk, childless directories have a valid subdir_offset
628                  * that points to an 8-byte end-of-directory dentry.  Regular
629                  * files or reparse points have a subdir_offset of 0. */
630                 if (dentry_is_directory(dentry))
631                         *subdir_offset_p += 8;
632                 else
633                         dentry->subdir_offset = 0;
634         }
635 }
636
637 /* Case-sensitive UTF-16LE dentry or stream name comparison.  Used on both UNIX
638  * (always) and Windows (sometimes) */
639 static int
640 compare_utf16le_names_case_sensitive(const utf16lechar *name1, size_t nbytes1,
641                                      const utf16lechar *name2, size_t nbytes2)
642 {
643         /* Return the result if the strings differ up to their minimum length.
644          * Note that we cannot use strcmp() or strncmp() here, as the strings
645          * are in UTF-16LE format. */
646         int result = memcmp(name1, name2, min(nbytes1, nbytes2));
647         if (result)
648                 return result;
649
650         /* The strings are the same up to their minimum length, so return a
651          * result based on their lengths. */
652         if (nbytes1 < nbytes2)
653                 return -1;
654         else if (nbytes1 > nbytes2)
655                 return 1;
656         else
657                 return 0;
658 }
659
660 #ifdef __WIN32__
661 /* Windoze: Case-insensitive UTF-16LE dentry or stream name comparison */
662 static int
663 compare_utf16le_names_case_insensitive(const utf16lechar *name1, size_t nbytes1,
664                                        const utf16lechar *name2, size_t nbytes2)
665 {
666         /* Return the result if the strings differ up to their minimum length.
667          * */
668         int result = _wcsnicmp((const wchar_t*)name1, (const wchar_t*)name2,
669                                min(nbytes1 / 2, nbytes2 / 2));
670         if (result)
671                 return result;
672
673         /* The strings are the same up to their minimum length, so return a
674          * result based on their lengths. */
675         if (nbytes1 < nbytes2)
676                 return -1;
677         else if (nbytes1 > nbytes2)
678                 return 1;
679         else
680                 return 0;
681 }
682 #endif /* __WIN32__ */
683
684 #ifdef __WIN32__
685 #  define compare_utf16le_names compare_utf16le_names_case_insensitive
686 #else
687 #  define compare_utf16le_names compare_utf16le_names_case_sensitive
688 #endif
689
690
691 #ifdef __WIN32__
692 static int
693 dentry_compare_names_case_insensitive(const struct wim_dentry *d1,
694                                       const struct wim_dentry *d2)
695 {
696         return compare_utf16le_names_case_insensitive(d1->file_name,
697                                                       d1->file_name_nbytes,
698                                                       d2->file_name,
699                                                       d2->file_name_nbytes);
700 }
701 #endif /* __WIN32__ */
702
703 static int
704 dentry_compare_names_case_sensitive(const struct wim_dentry *d1,
705                                     const struct wim_dentry *d2)
706 {
707         return compare_utf16le_names_case_sensitive(d1->file_name,
708                                                     d1->file_name_nbytes,
709                                                     d2->file_name,
710                                                     d2->file_name_nbytes);
711 }
712
713 #ifdef __WIN32__
714 #  define dentry_compare_names dentry_compare_names_case_insensitive
715 #else
716 #  define dentry_compare_names dentry_compare_names_case_sensitive
717 #endif
718
719 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
720  * stream name @name that has length @name_nbytes bytes. */
721 static inline bool
722 ads_entry_has_name(const struct wim_ads_entry *entry,
723                    const utf16lechar *name, size_t name_nbytes)
724 {
725         return !compare_utf16le_names(name, name_nbytes,
726                                       entry->stream_name,
727                                       entry->stream_name_nbytes);
728 }
729
730 /* Given a UTF-16LE filename and a directory, look up the dentry for the file.
731  * Return it if found, otherwise NULL.  This is case-sensitive on UNIX and
732  * case-insensitive on Windows. */
733 struct wim_dentry *
734 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
735                                    const utf16lechar *name,
736                                    size_t name_nbytes)
737 {
738         struct rb_node *node;
739
740 #ifdef __WIN32__
741         node = dentry->d_inode->i_children_case_insensitive.rb_node;
742 #else
743         node = dentry->d_inode->i_children.rb_node;
744 #endif
745
746         struct wim_dentry *child;
747         while (node) {
748         #ifdef __WIN32__
749                 child = rb_entry(node, struct wim_dentry, rb_node_case_insensitive);
750         #else
751                 child = rbnode_dentry(node);
752         #endif
753                 int result = compare_utf16le_names(name, name_nbytes,
754                                                    child->file_name,
755                                                    child->file_name_nbytes);
756                 if (result < 0)
757                         node = node->rb_left;
758                 else if (result > 0)
759                         node = node->rb_right;
760                 else {
761                 #ifdef __WIN32__
762                         if (!list_empty(&child->case_insensitive_conflict_list))
763                         {
764                                 WARNING("Result of case-insensitive lookup is ambiguous "
765                                         "(returning \"%ls\" instead of \"%ls\")",
766                                         child->file_name,
767                                         container_of(child->case_insensitive_conflict_list.next,
768                                                      struct wim_dentry,
769                                                      case_insensitive_conflict_list)->file_name);
770                         }
771                 #endif
772                         return child;
773                 }
774         }
775         return NULL;
776 }
777
778 /* Returns the child of @dentry that has the file name @name.  Returns NULL if
779  * no child has the name. */
780 struct wim_dentry *
781 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
782 {
783 #if TCHAR_IS_UTF16LE
784         return get_dentry_child_with_utf16le_name(dentry, name,
785                                                   tstrlen(name) * sizeof(tchar));
786 #else
787         utf16lechar *utf16le_name;
788         size_t utf16le_name_nbytes;
789         int ret;
790         struct wim_dentry *child;
791
792         ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
793                               &utf16le_name, &utf16le_name_nbytes);
794         if (ret) {
795                 child = NULL;
796         } else {
797                 child = get_dentry_child_with_utf16le_name(dentry,
798                                                            utf16le_name,
799                                                            utf16le_name_nbytes);
800                 FREE(utf16le_name);
801         }
802         return child;
803 #endif
804 }
805
806 static struct wim_dentry *
807 get_dentry_utf16le(WIMStruct *wim, const utf16lechar *path)
808 {
809         struct wim_dentry *cur_dentry, *parent_dentry;
810         const utf16lechar *p, *pp;
811
812         cur_dentry = parent_dentry = wim_root_dentry(wim);
813         if (cur_dentry == NULL) {
814                 errno = ENOENT;
815                 return NULL;
816         }
817         p = path;
818         while (1) {
819                 while (*p == cpu_to_le16(WIM_PATH_SEPARATOR))
820                         p++;
821                 if (*p == cpu_to_le16('\0'))
822                         break;
823                 pp = p;
824                 while (*pp != cpu_to_le16(WIM_PATH_SEPARATOR) &&
825                        *pp != cpu_to_le16('\0'))
826                         pp++;
827
828                 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
829                                                                 (void*)pp - (void*)p);
830                 if (cur_dentry == NULL)
831                         break;
832                 p = pp;
833                 parent_dentry = cur_dentry;
834         }
835         if (cur_dentry == NULL) {
836                 if (dentry_is_directory(parent_dentry))
837                         errno = ENOENT;
838                 else
839                         errno = ENOTDIR;
840         }
841         return cur_dentry;
842 }
843
844 /*
845  * Returns the dentry in the currently selected WIM image named by @path
846  * starting from the root of the WIM image, or NULL if there is no such dentry.
847  *
848  * On Windows, the search is done case-insensitively.
849  */
850 struct wim_dentry *
851 get_dentry(WIMStruct *wim, const tchar *path)
852 {
853 #if TCHAR_IS_UTF16LE
854         return get_dentry_utf16le(wim, path);
855 #else
856         utf16lechar *path_utf16le;
857         size_t path_utf16le_nbytes;
858         int ret;
859         struct wim_dentry *dentry;
860
861         ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
862                               &path_utf16le, &path_utf16le_nbytes);
863         if (ret)
864                 return NULL;
865         dentry = get_dentry_utf16le(wim, path_utf16le);
866         FREE(path_utf16le);
867         return dentry;
868 #endif
869 }
870
871 struct wim_inode *
872 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
873 {
874         struct wim_dentry *dentry;
875         dentry = get_dentry(wim, path);
876         if (dentry)
877                 return dentry->d_inode;
878         else
879                 return NULL;
880 }
881
882 /* Takes in a path of length @len in @buf, and transforms it into a string for
883  * the path of its parent directory. */
884 static void
885 to_parent_name(tchar *buf, size_t len)
886 {
887         ssize_t i = (ssize_t)len - 1;
888         while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
889                 i--;
890         while (i >= 0 && buf[i] != WIM_PATH_SEPARATOR)
891                 i--;
892         while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
893                 i--;
894         buf[i + 1] = T('\0');
895 }
896
897 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
898  * if the dentry is not found. */
899 struct wim_dentry *
900 get_parent_dentry(WIMStruct *wim, const tchar *path)
901 {
902         size_t path_len = tstrlen(path);
903         tchar buf[path_len + 1];
904
905         tmemcpy(buf, path, path_len + 1);
906         to_parent_name(buf, path_len);
907         return get_dentry(wim, buf);
908 }
909
910 /* Prints the full path of a dentry. */
911 int
912 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
913 {
914         int ret = calculate_dentry_full_path(dentry);
915         if (ret)
916                 return ret;
917         tprintf(T("%"TS"\n"), dentry->_full_path);
918         return 0;
919 }
920
921 /* We want to be able to show the names of the file attribute flags that are
922  * set. */
923 struct file_attr_flag {
924         u32 flag;
925         const tchar *name;
926 };
927 struct file_attr_flag file_attr_flags[] = {
928         {FILE_ATTRIBUTE_READONLY,           T("READONLY")},
929         {FILE_ATTRIBUTE_HIDDEN,             T("HIDDEN")},
930         {FILE_ATTRIBUTE_SYSTEM,             T("SYSTEM")},
931         {FILE_ATTRIBUTE_DIRECTORY,          T("DIRECTORY")},
932         {FILE_ATTRIBUTE_ARCHIVE,            T("ARCHIVE")},
933         {FILE_ATTRIBUTE_DEVICE,             T("DEVICE")},
934         {FILE_ATTRIBUTE_NORMAL,             T("NORMAL")},
935         {FILE_ATTRIBUTE_TEMPORARY,          T("TEMPORARY")},
936         {FILE_ATTRIBUTE_SPARSE_FILE,        T("SPARSE_FILE")},
937         {FILE_ATTRIBUTE_REPARSE_POINT,      T("REPARSE_POINT")},
938         {FILE_ATTRIBUTE_COMPRESSED,         T("COMPRESSED")},
939         {FILE_ATTRIBUTE_OFFLINE,            T("OFFLINE")},
940         {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
941         {FILE_ATTRIBUTE_ENCRYPTED,          T("ENCRYPTED")},
942         {FILE_ATTRIBUTE_VIRTUAL,            T("VIRTUAL")},
943 };
944
945 /* Prints a directory entry.  @lookup_table is a pointer to the lookup table, if
946  * available.  If the dentry is unresolved and the lookup table is NULL, the
947  * lookup table entries will not be printed.  Otherwise, they will be. */
948 int
949 print_dentry(struct wim_dentry *dentry, void *lookup_table)
950 {
951         const u8 *hash;
952         struct wim_lookup_table_entry *lte;
953         const struct wim_inode *inode = dentry->d_inode;
954         tchar buf[50];
955
956         tprintf(T("[DENTRY]\n"));
957         tprintf(T("Length            = %"PRIu64"\n"), dentry->length);
958         tprintf(T("Attributes        = 0x%x\n"), inode->i_attributes);
959         for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
960                 if (file_attr_flags[i].flag & inode->i_attributes)
961                         tprintf(T("    FILE_ATTRIBUTE_%"TS" is set\n"),
962                                 file_attr_flags[i].name);
963         tprintf(T("Security ID       = %d\n"), inode->i_security_id);
964         tprintf(T("Subdir offset     = %"PRIu64"\n"), dentry->subdir_offset);
965
966         wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
967         tprintf(T("Creation Time     = %"TS"\n"), buf);
968
969         wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
970         tprintf(T("Last Access Time  = %"TS"\n"), buf);
971
972         wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
973         tprintf(T("Last Write Time   = %"TS"\n"), buf);
974
975         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
976                 tprintf(T("Reparse Tag       = 0x%"PRIx32"\n"), inode->i_reparse_tag);
977                 tprintf(T("Reparse Point Flags = 0x%"PRIx16"\n"),
978                         inode->i_not_rpfixed);
979                 tprintf(T("Reparse Point Unknown 2 = 0x%"PRIx32"\n"),
980                         inode->i_rp_unknown_2);
981         }
982         tprintf(T("Reparse Point Unknown 1 = 0x%"PRIx32"\n"),
983                 inode->i_rp_unknown_1);
984         tprintf(T("Hard Link Group   = 0x%"PRIx64"\n"), inode->i_ino);
985         tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
986         tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
987         if (dentry_has_long_name(dentry))
988                 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
989         if (dentry_has_short_name(dentry))
990                 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
991         if (dentry->_full_path)
992                 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
993
994         lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
995         if (lte) {
996                 print_lookup_table_entry(lte, stdout);
997         } else {
998                 hash = inode_stream_hash(inode, 0);
999                 if (hash) {
1000                         tprintf(T("Hash              = 0x"));
1001                         print_hash(hash, stdout);
1002                         tputchar(T('\n'));
1003                         tputchar(T('\n'));
1004                 }
1005         }
1006         for (u16 i = 0; i < inode->i_num_ads; i++) {
1007                 tprintf(T("[Alternate Stream Entry %u]\n"), i);
1008                 wimlib_printf(T("Name = \"%"WS"\"\n"),
1009                               inode->i_ads_entries[i].stream_name);
1010                 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
1011                        inode->i_ads_entries[i].stream_name_nbytes);
1012                 hash = inode_stream_hash(inode, i + 1);
1013                 if (hash) {
1014                         tprintf(T("Hash              = 0x"));
1015                         print_hash(hash, stdout);
1016                         tputchar(T('\n'));
1017                 }
1018                 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
1019                                          stdout);
1020         }
1021         return 0;
1022 }
1023
1024 /* Initializations done on every `struct wim_dentry'. */
1025 static void
1026 dentry_common_init(struct wim_dentry *dentry)
1027 {
1028         memset(dentry, 0, sizeof(struct wim_dentry));
1029 }
1030
1031 struct wim_inode *
1032 new_timeless_inode(void)
1033 {
1034         struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
1035         if (inode) {
1036                 inode->i_security_id = -1;
1037                 inode->i_nlink = 1;
1038                 inode->i_next_stream_id = 1;
1039                 inode->i_not_rpfixed = 1;
1040                 inode->i_canonical_streams = 1;
1041                 INIT_LIST_HEAD(&inode->i_list);
1042                 INIT_LIST_HEAD(&inode->i_dentry);
1043         }
1044         return inode;
1045 }
1046
1047 static struct wim_inode *
1048 new_inode(void)
1049 {
1050         struct wim_inode *inode = new_timeless_inode();
1051         if (inode) {
1052                 u64 now = get_wim_timestamp();
1053                 inode->i_creation_time = now;
1054                 inode->i_last_access_time = now;
1055                 inode->i_last_write_time = now;
1056         }
1057         return inode;
1058 }
1059
1060 /* Creates an unlinked directory entry. */
1061 int
1062 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
1063 {
1064         struct wim_dentry *dentry;
1065         int ret;
1066
1067         dentry = MALLOC(sizeof(struct wim_dentry));
1068         if (dentry == NULL)
1069                 return WIMLIB_ERR_NOMEM;
1070
1071         dentry_common_init(dentry);
1072         ret = set_dentry_name(dentry, name);
1073         if (ret == 0) {
1074                 dentry->parent = dentry;
1075                 *dentry_ret = dentry;
1076         } else {
1077                 FREE(dentry);
1078                 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
1079                       name);
1080         }
1081         return ret;
1082 }
1083
1084
1085 static int
1086 _new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
1087                         bool timeless)
1088 {
1089         struct wim_dentry *dentry;
1090         int ret;
1091
1092         ret = new_dentry(name, &dentry);
1093         if (ret)
1094                 return ret;
1095
1096         if (timeless)
1097                 dentry->d_inode = new_timeless_inode();
1098         else
1099                 dentry->d_inode = new_inode();
1100         if (dentry->d_inode == NULL) {
1101                 free_dentry(dentry);
1102                 return WIMLIB_ERR_NOMEM;
1103         }
1104
1105         inode_add_dentry(dentry, dentry->d_inode);
1106         *dentry_ret = dentry;
1107         return 0;
1108 }
1109
1110 int
1111 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
1112 {
1113         return _new_dentry_with_inode(name, dentry_ret, true);
1114 }
1115
1116 int
1117 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
1118 {
1119         return _new_dentry_with_inode(name, dentry_ret, false);
1120 }
1121
1122 int
1123 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
1124 {
1125         int ret;
1126         struct wim_dentry *dentry;
1127
1128         DEBUG("Creating filler directory \"%"TS"\"", name);
1129         ret = new_dentry_with_inode(name, &dentry);
1130         if (ret)
1131                 return ret;
1132         /* Leave the inode number as 0; this is allowed for non
1133          * hard-linked files. */
1134         dentry->d_inode->i_resolved = 1;
1135         dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
1136         *dentry_ret = dentry;
1137         return 0;
1138 }
1139
1140 static int
1141 dentry_clear_inode_visited(struct wim_dentry *dentry, void *_ignore)
1142 {
1143         dentry->d_inode->i_visited = 0;
1144         return 0;
1145 }
1146
1147 void
1148 dentry_tree_clear_inode_visited(struct wim_dentry *root)
1149 {
1150         for_dentry_in_tree(root, dentry_clear_inode_visited, NULL);
1151 }
1152
1153 static int
1154 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
1155                size_t name_nbytes, bool is_utf16le)
1156 {
1157         int ret = 0;
1158         memset(ads_entry, 0, sizeof(*ads_entry));
1159
1160         if (is_utf16le) {
1161                 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
1162                 if (p == NULL)
1163                         return WIMLIB_ERR_NOMEM;
1164                 memcpy(p, name, name_nbytes);
1165                 p[name_nbytes / 2] = cpu_to_le16(0);
1166                 ads_entry->stream_name = p;
1167                 ads_entry->stream_name_nbytes = name_nbytes;
1168         } else {
1169                 if (name && *(const tchar*)name != T('\0')) {
1170                         ret = get_utf16le_name(name, &ads_entry->stream_name,
1171                                                &ads_entry->stream_name_nbytes);
1172                 }
1173         }
1174         return ret;
1175 }
1176
1177 static void
1178 destroy_ads_entry(struct wim_ads_entry *ads_entry)
1179 {
1180         FREE(ads_entry->stream_name);
1181 }
1182
1183 /* Frees an inode. */
1184 void
1185 free_inode(struct wim_inode *inode)
1186 {
1187         if (inode) {
1188                 if (inode->i_ads_entries) {
1189                         for (u16 i = 0; i < inode->i_num_ads; i++)
1190                                 destroy_ads_entry(&inode->i_ads_entries[i]);
1191                         FREE(inode->i_ads_entries);
1192                 }
1193                 /* HACK: This may instead delete the inode from i_list, but the
1194                  * hlist_del() behaves the same as list_del(). */
1195                 if (!hlist_unhashed(&inode->i_hlist))
1196                         hlist_del(&inode->i_hlist);
1197                 FREE(inode);
1198         }
1199 }
1200
1201 /* Decrements link count on an inode and frees it if the link count reaches 0.
1202  * */
1203 static void
1204 put_inode(struct wim_inode *inode)
1205 {
1206         wimlib_assert(inode->i_nlink != 0);
1207         if (--inode->i_nlink == 0) {
1208         #ifdef WITH_FUSE
1209                 if (inode->i_num_opened_fds == 0)
1210         #endif
1211                 {
1212                         free_inode(inode);
1213                 }
1214         }
1215 }
1216
1217 /* Frees a WIM dentry.
1218  *
1219  * The corresponding inode (if any) is freed only if its link count is
1220  * decremented to 0.
1221  */
1222 void
1223 free_dentry(struct wim_dentry *dentry)
1224 {
1225         if (dentry) {
1226                 FREE(dentry->file_name);
1227                 FREE(dentry->short_name);
1228                 FREE(dentry->_full_path);
1229                 if (dentry->d_inode)
1230                         put_inode(dentry->d_inode);
1231                 FREE(dentry);
1232         }
1233 }
1234
1235 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
1236  * to free a directory tree. */
1237 static int
1238 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
1239 {
1240         struct wim_lookup_table *lookup_table = _lookup_table;
1241
1242         if (lookup_table) {
1243                 struct wim_inode *inode = dentry->d_inode;
1244                 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1245                         struct wim_lookup_table_entry *lte;
1246
1247                         lte = inode_stream_lte(inode, i, lookup_table);
1248                         if (lte)
1249                                 lte_decrement_refcnt(lte, lookup_table);
1250                 }
1251         }
1252         free_dentry(dentry);
1253         return 0;
1254 }
1255
1256 /*
1257  * Unlinks and frees a dentry tree.
1258  *
1259  * @root:
1260  *      The root of the tree.
1261  *
1262  * @lookup_table:
1263  *      The lookup table for dentries.  If non-NULL, the reference counts in the
1264  *      lookup table for the lookup table entries corresponding to the dentries
1265  *      will be decremented.
1266  */
1267 void
1268 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1269 {
1270         for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1271 }
1272
1273 #ifdef __WIN32__
1274
1275 /* Insert a dentry into the case insensitive index for a directory.
1276  *
1277  * This is a red-black tree, but when multiple dentries share the same
1278  * case-insensitive name, only one is inserted into the tree itself; the rest
1279  * are connected in a list.
1280  */
1281 static struct wim_dentry *
1282 dentry_add_child_case_insensitive(struct wim_dentry *parent,
1283                                   struct wim_dentry *child)
1284 {
1285         struct rb_root *root;
1286         struct rb_node **new;
1287         struct rb_node *rb_parent;
1288
1289         root = &parent->d_inode->i_children_case_insensitive;
1290         new = &root->rb_node;
1291         rb_parent = NULL;
1292         while (*new) {
1293                 struct wim_dentry *this = container_of(*new, struct wim_dentry,
1294                                                        rb_node_case_insensitive);
1295                 int result = dentry_compare_names_case_insensitive(child, this);
1296
1297                 rb_parent = *new;
1298
1299                 if (result < 0)
1300                         new = &((*new)->rb_left);
1301                 else if (result > 0)
1302                         new = &((*new)->rb_right);
1303                 else
1304                         return this;
1305         }
1306         rb_link_node(&child->rb_node_case_insensitive, rb_parent, new);
1307         rb_insert_color(&child->rb_node_case_insensitive, root);
1308         return NULL;
1309 }
1310 #endif
1311
1312 /*
1313  * Links a dentry into the directory tree.
1314  *
1315  * @parent: The dentry that will be the parent of @child.
1316  * @child: The dentry to link.
1317  *
1318  * Returns NULL if successful.  If @parent already contains a dentry with the
1319  * same case-sensitive name as @child, the pointer to this duplicate dentry is
1320  * returned.
1321  */
1322 struct wim_dentry *
1323 dentry_add_child(struct wim_dentry * restrict parent,
1324                  struct wim_dentry * restrict child)
1325 {
1326         struct rb_root *root;
1327         struct rb_node **new;
1328         struct rb_node *rb_parent;
1329
1330         wimlib_assert(dentry_is_directory(parent));
1331         wimlib_assert(parent != child);
1332
1333         /* Case sensitive child dentry index */
1334         root = &parent->d_inode->i_children;
1335         new = &root->rb_node;
1336         rb_parent = NULL;
1337         while (*new) {
1338                 struct wim_dentry *this = rbnode_dentry(*new);
1339                 int result = dentry_compare_names_case_sensitive(child, this);
1340
1341                 rb_parent = *new;
1342
1343                 if (result < 0)
1344                         new = &((*new)->rb_left);
1345                 else if (result > 0)
1346                         new = &((*new)->rb_right);
1347                 else
1348                         return this;
1349         }
1350         child->parent = parent;
1351         rb_link_node(&child->rb_node, rb_parent, new);
1352         rb_insert_color(&child->rb_node, root);
1353
1354 #ifdef __WIN32__
1355         {
1356                 struct wim_dentry *existing;
1357                 existing = dentry_add_child_case_insensitive(parent, child);
1358                 if (existing) {
1359                         list_add(&child->case_insensitive_conflict_list,
1360                                  &existing->case_insensitive_conflict_list);
1361                         child->rb_node_case_insensitive.__rb_parent_color = 0;
1362                 } else {
1363                         INIT_LIST_HEAD(&child->case_insensitive_conflict_list);
1364                 }
1365         }
1366 #endif
1367         return NULL;
1368 }
1369
1370 /* Unlink a WIM dentry from the directory entry tree. */
1371 void
1372 unlink_dentry(struct wim_dentry *dentry)
1373 {
1374         struct wim_dentry *parent = dentry->parent;
1375
1376         if (parent == dentry)
1377                 return;
1378         rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1379 #ifdef __WIN32__
1380         if (dentry->rb_node_case_insensitive.__rb_parent_color) {
1381                 /* This dentry was in the case-insensitive red-black tree. */
1382                 rb_erase(&dentry->rb_node_case_insensitive,
1383                          &parent->d_inode->i_children_case_insensitive);
1384                 if (!list_empty(&dentry->case_insensitive_conflict_list)) {
1385                         /* Make a different case-insensitively-the-same dentry
1386                          * be the "representative" in the red-black tree. */
1387                         struct list_head *next;
1388                         struct wim_dentry *other;
1389                         struct wim_dentry *existing;
1390
1391                         next = dentry->case_insensitive_conflict_list.next;
1392                         other = list_entry(next, struct wim_dentry, case_insensitive_conflict_list);
1393                         existing = dentry_add_child_case_insensitive(parent, other);
1394                         wimlib_assert(existing == NULL);
1395                 }
1396         }
1397         list_del(&dentry->case_insensitive_conflict_list);
1398 #endif
1399 }
1400
1401 /*
1402  * Returns the alternate data stream entry belonging to @inode that has the
1403  * stream name @stream_name, or NULL if the inode has no alternate data stream
1404  * with that name.
1405  *
1406  * If @p stream_name is the empty string, NULL is returned --- that is, this
1407  * function will not return "unnamed" alternate data stream entries.
1408  */
1409 struct wim_ads_entry *
1410 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1411                     u16 *idx_ret)
1412 {
1413         if (inode->i_num_ads == 0) {
1414                 return NULL;
1415         } else {
1416                 size_t stream_name_utf16le_nbytes;
1417                 u16 i;
1418                 struct wim_ads_entry *result;
1419
1420                 if (stream_name[0] == T('\0'))
1421                         return NULL;
1422
1423         #if TCHAR_IS_UTF16LE
1424                 const utf16lechar *stream_name_utf16le;
1425
1426                 stream_name_utf16le = stream_name;
1427                 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1428         #else
1429                 utf16lechar *stream_name_utf16le;
1430
1431                 {
1432                         int ret = tstr_to_utf16le(stream_name,
1433                                                   tstrlen(stream_name) *
1434                                                       sizeof(tchar),
1435                                                   &stream_name_utf16le,
1436                                                   &stream_name_utf16le_nbytes);
1437                         if (ret)
1438                                 return NULL;
1439                 }
1440         #endif
1441                 i = 0;
1442                 result = NULL;
1443                 do {
1444                         if (ads_entry_has_name(&inode->i_ads_entries[i],
1445                                                stream_name_utf16le,
1446                                                stream_name_utf16le_nbytes))
1447                         {
1448                                 if (idx_ret)
1449                                         *idx_ret = i;
1450                                 result = &inode->i_ads_entries[i];
1451                                 break;
1452                         }
1453                 } while (++i != inode->i_num_ads);
1454         #if !TCHAR_IS_UTF16LE
1455                 FREE(stream_name_utf16le);
1456         #endif
1457                 return result;
1458         }
1459 }
1460
1461 static struct wim_ads_entry *
1462 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1463                  size_t stream_name_nbytes, bool is_utf16le)
1464 {
1465         u16 num_ads;
1466         struct wim_ads_entry *ads_entries;
1467         struct wim_ads_entry *new_entry;
1468
1469         wimlib_assert(stream_name_nbytes != 0);
1470
1471         if (inode->i_num_ads >= 0xfffe) {
1472                 ERROR("Too many alternate data streams in one inode!");
1473                 return NULL;
1474         }
1475         num_ads = inode->i_num_ads + 1;
1476         ads_entries = REALLOC(inode->i_ads_entries,
1477                               num_ads * sizeof(inode->i_ads_entries[0]));
1478         if (ads_entries == NULL) {
1479                 ERROR("Failed to allocate memory for new alternate data stream");
1480                 return NULL;
1481         }
1482         inode->i_ads_entries = ads_entries;
1483
1484         new_entry = &inode->i_ads_entries[num_ads - 1];
1485         if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1486                 return NULL;
1487         new_entry->stream_id = inode->i_next_stream_id++;
1488         inode->i_num_ads = num_ads;
1489         return new_entry;
1490 }
1491
1492 struct wim_ads_entry *
1493 inode_add_ads_utf16le(struct wim_inode *inode,
1494                       const utf16lechar *stream_name,
1495                       size_t stream_name_nbytes)
1496 {
1497         DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1498         return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1499 }
1500
1501 /*
1502  * Add an alternate stream entry to a WIM inode.  On success, returns a pointer
1503  * to the new entry; on failure, returns NULL.
1504  *
1505  * @stream_name must be a nonempty string.
1506  */
1507 struct wim_ads_entry *
1508 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1509 {
1510         DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1511         return do_inode_add_ads(inode, stream_name,
1512                                 tstrlen(stream_name) * sizeof(tchar),
1513                                 TCHAR_IS_UTF16LE);
1514 }
1515
1516 static struct wim_lookup_table_entry *
1517 add_stream_from_data_buffer(const void *buffer, size_t size,
1518                             struct wim_lookup_table *lookup_table)
1519 {
1520         u8 hash[SHA1_HASH_SIZE];
1521         struct wim_lookup_table_entry *lte, *existing_lte;
1522
1523         sha1_buffer(buffer, size, hash);
1524         existing_lte = lookup_resource(lookup_table, hash);
1525         if (existing_lte) {
1526                 wimlib_assert(existing_lte->size == size);
1527                 lte = existing_lte;
1528                 lte->refcnt++;
1529         } else {
1530                 void *buffer_copy;
1531                 lte = new_lookup_table_entry();
1532                 if (lte == NULL)
1533                         return NULL;
1534                 buffer_copy = memdup(buffer, size);
1535                 if (buffer_copy == NULL) {
1536                         free_lookup_table_entry(lte);
1537                         return NULL;
1538                 }
1539                 lte->resource_location  = RESOURCE_IN_ATTACHED_BUFFER;
1540                 lte->attached_buffer    = buffer_copy;
1541                 lte->size               = size;
1542                 copy_hash(lte->hash, hash);
1543                 lookup_table_insert(lookup_table, lte);
1544         }
1545         return lte;
1546 }
1547
1548 int
1549 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1550                         const void *value, size_t size,
1551                         struct wim_lookup_table *lookup_table)
1552 {
1553         struct wim_ads_entry *new_ads_entry;
1554
1555         wimlib_assert(inode->i_resolved);
1556
1557         new_ads_entry = inode_add_ads(inode, name);
1558         if (new_ads_entry == NULL)
1559                 return WIMLIB_ERR_NOMEM;
1560
1561         new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1562                                                          lookup_table);
1563         if (new_ads_entry->lte == NULL) {
1564                 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1565                                  lookup_table);
1566                 return WIMLIB_ERR_NOMEM;
1567         }
1568         return 0;
1569 }
1570
1571 bool
1572 inode_has_named_stream(const struct wim_inode *inode)
1573 {
1574         for (u16 i = 0; i < inode->i_num_ads; i++)
1575                 if (ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1576                         return true;
1577         return false;
1578 }
1579
1580 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
1581  * stream contents. */
1582 int
1583 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
1584                          struct wim_lookup_table *lookup_table)
1585 {
1586         inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
1587         if (inode->i_lte == NULL)
1588                 return WIMLIB_ERR_NOMEM;
1589         inode->i_resolved = 1;
1590         return 0;
1591 }
1592
1593 /* Remove an alternate data stream from a WIM inode  */
1594 void
1595 inode_remove_ads(struct wim_inode *inode, u16 idx,
1596                  struct wim_lookup_table *lookup_table)
1597 {
1598         struct wim_ads_entry *ads_entry;
1599         struct wim_lookup_table_entry *lte;
1600
1601         wimlib_assert(idx < inode->i_num_ads);
1602         wimlib_assert(inode->i_resolved);
1603
1604         ads_entry = &inode->i_ads_entries[idx];
1605
1606         DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1607
1608         lte = ads_entry->lte;
1609         if (lte)
1610                 lte_decrement_refcnt(lte, lookup_table);
1611
1612         destroy_ads_entry(ads_entry);
1613
1614         memmove(&inode->i_ads_entries[idx],
1615                 &inode->i_ads_entries[idx + 1],
1616                 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1617         inode->i_num_ads--;
1618 }
1619
1620 bool
1621 inode_has_unix_data(const struct wim_inode *inode)
1622 {
1623         for (u16 i = 0; i < inode->i_num_ads; i++)
1624                 if (ads_entry_is_unix_data(&inode->i_ads_entries[i]))
1625                         return true;
1626         return false;
1627 }
1628
1629 #ifndef __WIN32__
1630 int
1631 inode_get_unix_data(const struct wim_inode *inode,
1632                     struct wimlib_unix_data *unix_data,
1633                     u16 *stream_idx_ret)
1634 {
1635         const struct wim_ads_entry *ads_entry;
1636         const struct wim_lookup_table_entry *lte;
1637         size_t size;
1638         int ret;
1639
1640         wimlib_assert(inode->i_resolved);
1641
1642         ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1643                                         WIMLIB_UNIX_DATA_TAG, NULL);
1644         if (ads_entry == NULL)
1645                 return NO_UNIX_DATA;
1646
1647         if (stream_idx_ret)
1648                 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1649
1650         lte = ads_entry->lte;
1651         if (lte == NULL)
1652                 return NO_UNIX_DATA;
1653
1654         size = lte->size;
1655         if (size != sizeof(struct wimlib_unix_data))
1656                 return BAD_UNIX_DATA;
1657
1658         ret = read_full_stream_into_buf(lte, unix_data);
1659         if (ret)
1660                 return ret;
1661
1662         if (unix_data->version != 0)
1663                 return BAD_UNIX_DATA;
1664         return 0;
1665 }
1666
1667 int
1668 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1669                     struct wim_lookup_table *lookup_table, int which)
1670 {
1671         struct wimlib_unix_data unix_data;
1672         int ret;
1673         bool have_good_unix_data = false;
1674         bool have_unix_data = false;
1675         u16 stream_idx;
1676
1677         if (!(which & UNIX_DATA_CREATE)) {
1678                 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1679                 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1680                         have_unix_data = true;
1681                 if (ret == 0)
1682                         have_good_unix_data = true;
1683         }
1684         unix_data.version = 0;
1685         if (which & UNIX_DATA_UID || !have_good_unix_data)
1686                 unix_data.uid = uid;
1687         if (which & UNIX_DATA_GID || !have_good_unix_data)
1688                 unix_data.gid = gid;
1689         if (which & UNIX_DATA_MODE || !have_good_unix_data)
1690                 unix_data.mode = mode;
1691         ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1692                                       &unix_data,
1693                                       sizeof(struct wimlib_unix_data),
1694                                       lookup_table);
1695         if (ret == 0 && have_unix_data)
1696                 inode_remove_ads(inode, stream_idx, lookup_table);
1697         return ret;
1698 }
1699 #endif /* !__WIN32__ */
1700
1701 /*
1702  * Reads the alternate data stream entries of a WIM dentry.
1703  *
1704  * @p:
1705  *      Pointer to buffer that starts with the first alternate stream entry.
1706  *
1707  * @inode:
1708  *      Inode to load the alternate data streams into.  @inode->i_num_ads must
1709  *      have been set to the number of alternate data streams that are expected.
1710  *
1711  * @remaining_size:
1712  *      Number of bytes of data remaining in the buffer pointed to by @p.
1713  *
1714  * On success, inode->i_ads_entries is set to an array of `struct
1715  * wim_ads_entry's of length inode->i_num_ads.  On failure, @inode is not
1716  * modified.
1717  *
1718  * Return values:
1719  *      WIMLIB_ERR_SUCCESS (0)
1720  *      WIMLIB_ERR_INVALID_METADATA_RESOURCE
1721  *      WIMLIB_ERR_NOMEM
1722  */
1723 static int
1724 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1725                  size_t nbytes_remaining)
1726 {
1727         u16 num_ads;
1728         struct wim_ads_entry *ads_entries;
1729         int ret;
1730
1731         BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
1732
1733         /* Allocate an array for our in-memory representation of the alternate
1734          * data stream entries. */
1735         num_ads = inode->i_num_ads;
1736         ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1737         if (ads_entries == NULL)
1738                 goto out_of_memory;
1739
1740         /* Read the entries into our newly allocated buffer. */
1741         for (u16 i = 0; i < num_ads; i++) {
1742                 u64 length;
1743                 struct wim_ads_entry *cur_entry;
1744                 const struct wim_ads_entry_on_disk *disk_entry =
1745                         (const struct wim_ads_entry_on_disk*)p;
1746
1747                 cur_entry = &ads_entries[i];
1748                 ads_entries[i].stream_id = i + 1;
1749
1750                 /* Do we have at least the size of the fixed-length data we know
1751                  * need? */
1752                 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1753                         goto out_invalid;
1754
1755                 /* Read the length field */
1756                 length = le64_to_cpu(disk_entry->length);
1757
1758                 /* Make sure the length field is neither so small it doesn't
1759                  * include all the fixed-length data nor so large it overflows
1760                  * the metadata resource buffer. */
1761                 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1762                     length > nbytes_remaining)
1763                         goto out_invalid;
1764
1765                 /* Read the rest of the fixed-length data. */
1766
1767                 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1768                 copy_hash(cur_entry->hash, disk_entry->hash);
1769                 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1770
1771                 /* If stream_name_nbytes != 0, this is a named stream.
1772                  * Otherwise this is an unnamed stream, or in some cases (bugs
1773                  * in Microsoft's software I guess) a meaningless entry
1774                  * distinguished from the real unnamed stream entry, if any, by
1775                  * the fact that the real unnamed stream entry has a nonzero
1776                  * hash field. */
1777                 if (cur_entry->stream_name_nbytes) {
1778                         /* The name is encoded in UTF16-LE, which uses 2-byte
1779                          * coding units, so the length of the name had better be
1780                          * an even number of bytes... */
1781                         if (cur_entry->stream_name_nbytes & 1)
1782                                 goto out_invalid;
1783
1784                         /* Add the length of the stream name to get the length
1785                          * we actually need to read.  Make sure this isn't more
1786                          * than the specified length of the entry. */
1787                         if (sizeof(struct wim_ads_entry_on_disk) +
1788                             cur_entry->stream_name_nbytes > length)
1789                                 goto out_invalid;
1790
1791                         cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1792                         if (cur_entry->stream_name == NULL)
1793                                 goto out_of_memory;
1794
1795                         memcpy(cur_entry->stream_name,
1796                                disk_entry->stream_name,
1797                                cur_entry->stream_name_nbytes);
1798                         cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1799                 } else {
1800                         /* Mark inode as having weird stream entries.  */
1801                         inode->i_canonical_streams = 0;
1802                 }
1803
1804                 /* It's expected that the size of every ADS entry is a multiple
1805                  * of 8.  However, to be safe, I'm allowing the possibility of
1806                  * an ADS entry at the very end of the metadata resource ending
1807                  * un-aligned.  So although we still need to increment the input
1808                  * pointer by @length to reach the next ADS entry, it's possible
1809                  * that less than @length is actually remaining in the metadata
1810                  * resource. We should set the remaining bytes to 0 if this
1811                  * happens. */
1812                 length = (length + 7) & ~(u64)7;
1813                 p += length;
1814                 if (nbytes_remaining < length)
1815                         nbytes_remaining = 0;
1816                 else
1817                         nbytes_remaining -= length;
1818         }
1819         inode->i_ads_entries = ads_entries;
1820         inode->i_next_stream_id = inode->i_num_ads + 1;
1821         ret = 0;
1822         goto out;
1823 out_of_memory:
1824         ret = WIMLIB_ERR_NOMEM;
1825         goto out_free_ads_entries;
1826 out_invalid:
1827         ERROR("An alternate data stream entry is invalid");
1828         ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1829 out_free_ads_entries:
1830         if (ads_entries) {
1831                 for (u16 i = 0; i < num_ads; i++)
1832                         destroy_ads_entry(&ads_entries[i]);
1833                 FREE(ads_entries);
1834         }
1835 out:
1836         return ret;
1837 }
1838
1839 /*
1840  * Reads a WIM directory entry, including all alternate data stream entries that
1841  * follow it, from the WIM image's metadata resource.
1842  *
1843  * @metadata_resource:
1844  *              Pointer to the metadata resource buffer.
1845  *
1846  * @metadata_resource_len:
1847  *              Length of the metadata resource buffer, in bytes.
1848  *
1849  * @offset:     Offset of the dentry within the metadata resource.
1850  *
1851  * @dentry:     A `struct wim_dentry' that will be filled in by this function.
1852  *
1853  * Return 0 on success or nonzero on failure.  On failure, @dentry will have
1854  * been modified, but it will not be left with pointers to any allocated
1855  * buffers.  On success, the dentry->length field must be examined.  If zero,
1856  * this was a special "end of directory" dentry and not a real dentry.  If
1857  * nonzero, this was a real dentry.
1858  *
1859  * Return values:
1860  *      WIMLIB_ERR_SUCCESS (0)
1861  *      WIMLIB_ERR_INVALID_METADATA_RESOURCE
1862  *      WIMLIB_ERR_NOMEM
1863  */
1864 int
1865 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1866             u64 offset, struct wim_dentry * restrict dentry)
1867 {
1868
1869         u64 calculated_size;
1870         utf16lechar *file_name;
1871         utf16lechar *short_name;
1872         u16 short_name_nbytes;
1873         u16 file_name_nbytes;
1874         int ret;
1875         struct wim_inode *inode;
1876         const u8 *p = &metadata_resource[offset];
1877         const struct wim_dentry_on_disk *disk_dentry =
1878                         (const struct wim_dentry_on_disk*)p;
1879
1880         BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE);
1881
1882         if ((uintptr_t)p & 7)
1883                 WARNING("WIM dentry is not 8-byte aligned");
1884
1885         dentry_common_init(dentry);
1886
1887         /* Before reading the whole dentry, we need to read just the length.
1888          * This is because a dentry of length 8 (that is, just the length field)
1889          * terminates the list of sibling directory entries. */
1890         if (offset + sizeof(u64) > metadata_resource_len ||
1891             offset + sizeof(u64) < offset)
1892         {
1893                 ERROR("Directory entry starting at %"PRIu64" ends past the "
1894                       "end of the metadata resource (size %"PRIu64")",
1895                       offset, metadata_resource_len);
1896                 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1897         }
1898         dentry->length = le64_to_cpu(disk_dentry->length);
1899
1900         /* A zero length field (really a length of 8, since that's how big the
1901          * directory entry is...) indicates that this is the end of directory
1902          * dentry.  We do not read it into memory as an actual dentry, so just
1903          * return successfully in this case. */
1904         if (dentry->length == 8)
1905                 dentry->length = 0;
1906         if (dentry->length == 0)
1907                 return 0;
1908
1909         /* Now that we have the actual length provided in the on-disk structure,
1910          * again make sure it doesn't overflow the metadata resource buffer. */
1911         if (offset + dentry->length > metadata_resource_len ||
1912             offset + dentry->length < offset)
1913         {
1914                 ERROR("Directory entry at offset %"PRIu64" and with size "
1915                       "%"PRIu64" ends past the end of the metadata resource "
1916                       "(size %"PRIu64")",
1917                       offset, dentry->length, metadata_resource_len);
1918                 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1919         }
1920
1921         /* Make sure the dentry length is at least as large as the number of
1922          * fixed-length fields */
1923         if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1924                 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1925                       dentry->length);
1926                 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1927         }
1928
1929         /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1930         inode = new_timeless_inode();
1931         if (inode == NULL)
1932                 return WIMLIB_ERR_NOMEM;
1933
1934         /* Read more fields; some into the dentry, and some into the inode. */
1935
1936         inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1937         inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1938         dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1939         dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1940         dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1941         inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1942         inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1943         inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1944         copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1945
1946         /* I don't know what's going on here.  It seems like M$ screwed up the
1947          * reparse points, then put the fields in the same place and didn't
1948          * document it.  So we have some fields we read for reparse points, and
1949          * some fields in the same place for non-reparse-point.s */
1950         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1951                 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1952                 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1953                 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1954                 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1955                 /* Leave inode->i_ino at 0.  Note that this means the WIM file
1956                  * cannot archive hard-linked reparse points.  Such a thing
1957                  * doesn't really make sense anyway, although I believe it's
1958                  * theoretically possible to have them on NTFS. */
1959         } else {
1960                 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1961                 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1962         }
1963
1964         inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1965
1966         short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1967         file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1968
1969         if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1970         {
1971                 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1972                 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1973                 goto out_free_inode;
1974         }
1975
1976         /* We now know the length of the file name and short name.  Make sure
1977          * the length of the dentry is large enough to actually hold them.
1978          *
1979          * The calculated length here is unaligned to allow for the possibility
1980          * that the dentry->length names an unaligned length, although this
1981          * would be unexpected. */
1982         calculated_size = dentry_correct_length_unaligned(file_name_nbytes,
1983                                                           short_name_nbytes);
1984
1985         if (dentry->length < calculated_size) {
1986                 ERROR("Unexpected end of directory entry! (Expected "
1987                       "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1988                       calculated_size, dentry->length);
1989                 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1990                 goto out_free_inode;
1991         }
1992
1993         p += sizeof(struct wim_dentry_on_disk);
1994
1995         /* Read the filename if present.  Note: if the filename is empty, there
1996          * is no null terminator following it. */
1997         if (file_name_nbytes) {
1998                 file_name = MALLOC(file_name_nbytes + 2);
1999                 if (file_name == NULL) {
2000                         ERROR("Failed to allocate %d bytes for dentry file name",
2001                               file_name_nbytes + 2);
2002                         ret = WIMLIB_ERR_NOMEM;
2003                         goto out_free_inode;
2004                 }
2005                 memcpy(file_name, p, file_name_nbytes);
2006                 p += file_name_nbytes + 2;
2007                 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
2008         } else {
2009                 file_name = NULL;
2010         }
2011
2012
2013         /* Read the short filename if present.  Note: if there is no short
2014          * filename, there is no null terminator following it. */
2015         if (short_name_nbytes) {
2016                 short_name = MALLOC(short_name_nbytes + 2);
2017                 if (short_name == NULL) {
2018                         ERROR("Failed to allocate %d bytes for dentry short name",
2019                               short_name_nbytes + 2);
2020                         ret = WIMLIB_ERR_NOMEM;
2021                         goto out_free_file_name;
2022                 }
2023                 memcpy(short_name, p, short_name_nbytes);
2024                 p += short_name_nbytes + 2;
2025                 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
2026         } else {
2027                 short_name = NULL;
2028         }
2029
2030         /* Align the dentry length */
2031         dentry->length = (dentry->length + 7) & ~7;
2032
2033         /*
2034          * Read the alternate data streams, if present.  dentry->num_ads tells
2035          * us how many they are, and they will directly follow the dentry
2036          * on-disk.
2037          *
2038          * Note that each alternate data stream entry begins on an 8-byte
2039          * aligned boundary, and the alternate data stream entries seem to NOT
2040          * be included in the dentry->length field for some reason.
2041          */
2042         if (inode->i_num_ads != 0) {
2043                 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
2044                 if (offset + dentry->length > metadata_resource_len ||
2045                     (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
2046                                             inode,
2047                                             metadata_resource_len - offset - dentry->length)))
2048                 {
2049                         ERROR("Failed to read alternate data stream "
2050                               "entries of WIM dentry \"%"WS"\"", file_name);
2051                         goto out_free_short_name;
2052                 }
2053         }
2054         /* We've read all the data for this dentry.  Set the names and their
2055          * lengths, and we've done. */
2056         dentry->d_inode           = inode;
2057         dentry->file_name         = file_name;
2058         dentry->short_name        = short_name;
2059         dentry->file_name_nbytes  = file_name_nbytes;
2060         dentry->short_name_nbytes = short_name_nbytes;
2061         ret = 0;
2062         goto out;
2063 out_free_short_name:
2064         FREE(short_name);
2065 out_free_file_name:
2066         FREE(file_name);
2067 out_free_inode:
2068         free_inode(inode);
2069 out:
2070         return ret;
2071 }
2072
2073 static const tchar *
2074 dentry_get_file_type_string(const struct wim_dentry *dentry)
2075 {
2076         const struct wim_inode *inode = dentry->d_inode;
2077         if (inode_is_directory(inode))
2078                 return T("directory");
2079         else if (inode_is_symlink(inode))
2080                 return T("symbolic link");
2081         else
2082                 return T("file");
2083 }
2084
2085 /* Reads the children of a dentry, and all their children, ..., etc. from the
2086  * metadata resource and into the dentry tree.
2087  *
2088  * @metadata_resource:
2089  *      An array that contains the uncompressed metadata resource for the WIM
2090  *      file.
2091  *
2092  * @metadata_resource_len:
2093  *      The length of the uncompressed metadata resource, in bytes.
2094  *
2095  * @dentry:
2096  *      A pointer to a `struct wim_dentry' that is the root of the directory
2097  *      tree and has already been read from the metadata resource.  It does not
2098  *      need to be the real root because this procedure is called recursively.
2099  *
2100  * Return values:
2101  *      WIMLIB_ERR_SUCCESS (0)
2102  *      WIMLIB_ERR_INVALID_METADATA_RESOURCE
2103  *      WIMLIB_ERR_NOMEM
2104  */
2105 int
2106 read_dentry_tree(const u8 * restrict metadata_resource,
2107                  u64 metadata_resource_len,
2108                  struct wim_dentry * restrict dentry)
2109 {
2110         u64 cur_offset = dentry->subdir_offset;
2111         struct wim_dentry *child;
2112         struct wim_dentry *duplicate;
2113         struct wim_dentry *parent;
2114         struct wim_dentry cur_child;
2115         int ret;
2116
2117         /*
2118          * If @dentry has no child dentries, nothing more needs to be done for
2119          * this branch.  This is the case for regular files, symbolic links, and
2120          * *possibly* empty directories (although an empty directory may also
2121          * have one child dentry that is the special end-of-directory dentry)
2122          */
2123         if (cur_offset == 0)
2124                 return 0;
2125
2126         /* Check for cyclic directory structure */
2127         for (parent = dentry->parent; !dentry_is_root(parent); parent = parent->parent)
2128         {
2129                 if (unlikely(parent->subdir_offset == cur_offset)) {
2130                         ERROR("Cyclic directory structure directed: children "
2131                               "of \"%"TS"\" coincide with children of \"%"TS"\"",
2132                               dentry_full_path(dentry),
2133                               dentry_full_path(parent));
2134                         return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
2135                 }
2136         }
2137
2138         /* Find and read all the children of @dentry. */
2139         for (;;) {
2140
2141                 /* Read next child of @dentry into @cur_child. */
2142                 ret = read_dentry(metadata_resource, metadata_resource_len,
2143                                   cur_offset, &cur_child);
2144                 if (ret)
2145                         break;
2146
2147                 /* Check for end of directory. */
2148                 if (cur_child.length == 0)
2149                         break;
2150
2151                 /* Not end of directory.  Allocate this child permanently and
2152                  * link it to the parent and previous child. */
2153                 child = memdup(&cur_child, sizeof(struct wim_dentry));
2154                 if (child == NULL) {
2155                         ERROR("Failed to allocate new dentry!");
2156                         ret = WIMLIB_ERR_NOMEM;
2157                         break;
2158                 }
2159
2160                 /* Advance to the offset of the next child.  Note: We need to
2161                  * advance by the TOTAL length of the dentry, not by the length
2162                  * cur_child.length, which although it does take into account
2163                  * the padding, it DOES NOT take into account alternate stream
2164                  * entries. */
2165                 cur_offset += dentry_in_total_length(child);
2166
2167                 if (unlikely(!dentry_has_long_name(child))) {
2168                         WARNING("Ignoring unnamed dentry in "
2169                                 "directory \"%"TS"\"",
2170                                 dentry_full_path(dentry));
2171                         free_dentry(child);
2172                         continue;
2173                 }
2174
2175                 duplicate = dentry_add_child(dentry, child);
2176                 if (unlikely(duplicate)) {
2177                         const tchar *child_type, *duplicate_type;
2178                         child_type = dentry_get_file_type_string(child);
2179                         duplicate_type = dentry_get_file_type_string(duplicate);
2180                         WARNING("Ignoring duplicate %"TS" \"%"TS"\" "
2181                                 "(the WIM image already contains a %"TS" "
2182                                 "at that path with the exact same name)",
2183                                 child_type, dentry_full_path(duplicate),
2184                                 duplicate_type);
2185                         free_dentry(child);
2186                         continue;
2187                 }
2188
2189                 inode_add_dentry(child, child->d_inode);
2190                 /* If there are children of this child, call this
2191                  * procedure recursively. */
2192                 if (child->subdir_offset != 0) {
2193                         if (likely(dentry_is_directory(child))) {
2194                                 ret = read_dentry_tree(metadata_resource,
2195                                                        metadata_resource_len,
2196                                                        child);
2197                                 if (ret)
2198                                         break;
2199                         } else {
2200                                 WARNING("Ignoring children of non-directory \"%"TS"\"",
2201                                         dentry_full_path(child));
2202                         }
2203                 }
2204         }
2205         return ret;
2206 }
2207
2208 /*
2209  * Writes a WIM alternate data stream (ADS) entry to an output buffer.
2210  *
2211  * @ads_entry:  The ADS entry structure.
2212  * @hash:       The hash field to use (instead of the one in the ADS entry).
2213  * @p:          The memory location to write the data to.
2214  *
2215  * Returns a pointer to the byte after the last byte written.
2216  */
2217 static u8 *
2218 write_ads_entry(const struct wim_ads_entry *ads_entry,
2219                 const u8 *hash, u8 * restrict p)
2220 {
2221         struct wim_ads_entry_on_disk *disk_ads_entry =
2222                         (struct wim_ads_entry_on_disk*)p;
2223         u8 *orig_p = p;
2224
2225         disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
2226         copy_hash(disk_ads_entry->hash, hash);
2227         disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
2228         p += sizeof(struct wim_ads_entry_on_disk);
2229         if (ads_entry->stream_name_nbytes) {
2230                 p = mempcpy(p, ads_entry->stream_name,
2231                             ads_entry->stream_name_nbytes + 2);
2232         }
2233         /* Align to 8-byte boundary */
2234         while ((uintptr_t)p & 7)
2235                 *p++ = 0;
2236         disk_ads_entry->length = cpu_to_le64(p - orig_p);
2237         return p;
2238 }
2239
2240 /*
2241  * Writes a WIM dentry to an output buffer.
2242  *
2243  * @dentry:  The dentry structure.
2244  * @p:       The memory location to write the data to.
2245  *
2246  * Returns the pointer to the byte after the last byte we wrote as part of the
2247  * dentry, including any alternate data stream entries.
2248  */
2249 static u8 *
2250 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
2251 {
2252         const struct wim_inode *inode;
2253         struct wim_dentry_on_disk *disk_dentry;
2254         const u8 *orig_p;
2255         const u8 *hash;
2256         bool use_dummy_stream;
2257         u16 num_ads;
2258
2259         wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
2260         orig_p = p;
2261
2262         inode = dentry->d_inode;
2263         use_dummy_stream = inode_needs_dummy_stream(inode);
2264         disk_dentry = (struct wim_dentry_on_disk*)p;
2265
2266         disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
2267         disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
2268         disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
2269         disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
2270         disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
2271         disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
2272         disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
2273         disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
2274         if (use_dummy_stream)
2275                 hash = zero_hash;
2276         else
2277                 hash = inode_stream_hash(inode, 0);
2278         copy_hash(disk_dentry->unnamed_stream_hash, hash);
2279         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2280                 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2281                 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
2282                 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
2283                 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
2284         } else {
2285                 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2286                 disk_dentry->nonreparse.hard_link_group_id =
2287                         cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
2288         }
2289         num_ads = inode->i_num_ads;
2290         if (use_dummy_stream)
2291                 num_ads++;
2292         disk_dentry->num_alternate_data_streams = cpu_to_le16(num_ads);
2293         disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
2294         disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
2295         p += sizeof(struct wim_dentry_on_disk);
2296
2297         wimlib_assert(dentry_is_root(dentry) != dentry_has_long_name(dentry));
2298
2299         if (dentry_has_long_name(dentry))
2300                 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
2301
2302         if (dentry_has_short_name(dentry))
2303                 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
2304
2305         /* Align to 8-byte boundary */
2306         while ((uintptr_t)p & 7)
2307                 *p++ = 0;
2308
2309         /* We calculate the correct length of the dentry ourselves because the
2310          * dentry->length field may been set to an unexpected value from when we
2311          * read the dentry in (for example, there may have been unknown data
2312          * appended to the end of the dentry...).  Furthermore, the dentry may
2313          * have been renamed, thus changing its needed length. */
2314         disk_dentry->length = cpu_to_le64(p - orig_p);
2315
2316         if (use_dummy_stream) {
2317                 hash = inode_unnamed_stream_hash(inode);
2318                 p = write_ads_entry(&(struct wim_ads_entry){}, hash, p);
2319         }
2320
2321         /* Write the alternate data streams entries, if any. */
2322         for (u16 i = 0; i < inode->i_num_ads; i++) {
2323                 hash = inode_stream_hash(inode, i + 1);
2324                 p = write_ads_entry(&inode->i_ads_entries[i], hash, p);
2325         }
2326
2327         return p;
2328 }
2329
2330 static int
2331 write_dentry_cb(struct wim_dentry *dentry, void *_p)
2332 {
2333         u8 **p = _p;
2334         *p = write_dentry(dentry, *p);
2335         return 0;
2336 }
2337
2338 static u8 *
2339 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
2340
2341 static int
2342 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
2343 {
2344         u8 **p = _p;
2345         *p = write_dentry_tree_recursive(dentry, *p);
2346         return 0;
2347 }
2348
2349 /* Recursive function that writes a dentry tree rooted at @parent, not including
2350  * @parent itself, which has already been written. */
2351 static u8 *
2352 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
2353 {
2354         /* Nothing to do if this dentry has no children. */
2355         if (parent->subdir_offset == 0)
2356                 return p;
2357
2358         /* Write child dentries and end-of-directory entry.
2359          *
2360          * Note: we need to write all of this dentry's children before
2361          * recursively writing the directory trees rooted at each of the child
2362          * dentries, since the on-disk dentries for a dentry's children are
2363          * always located at consecutive positions in the metadata resource! */
2364         for_dentry_child(parent, write_dentry_cb, &p);
2365
2366         /* write end of directory entry */
2367         *(le64*)p = cpu_to_le64(0);
2368         p += 8;
2369
2370         /* Recurse on children. */
2371         for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
2372         return p;
2373 }
2374
2375 /* Writes a directory tree to the metadata resource.
2376  *
2377  * @root:       Root of the dentry tree.
2378  * @p:          Pointer to a buffer with enough space for the dentry tree.
2379  *
2380  * Returns pointer to the byte after the last byte we wrote.
2381  */
2382 u8 *
2383 write_dentry_tree(const struct wim_dentry * restrict root, u8 * restrict p)
2384 {
2385         DEBUG("Writing dentry tree.");
2386         wimlib_assert(dentry_is_root(root));
2387
2388         /* If we're the root dentry, we have no parent that already
2389          * wrote us, so we need to write ourselves. */
2390         p = write_dentry(root, p);
2391
2392         /* Write end of directory entry after the root dentry just to be safe;
2393          * however the root dentry obviously cannot have any siblings. */
2394         *(le64*)p = cpu_to_le64(0);
2395         p += 8;
2396
2397         /* Recursively write the rest of the dentry tree. */
2398         return write_dentry_tree_recursive(root, p);
2399 }
2400
2401
2402 static int
2403 init_wimlib_dentry(struct wimlib_dir_entry *wdentry,
2404                    struct wim_dentry *dentry,
2405                    const WIMStruct *wim,
2406                    int flags)
2407 {
2408         int ret;
2409         size_t dummy;
2410         const struct wim_inode *inode = dentry->d_inode;
2411         struct wim_lookup_table_entry *lte;
2412         const u8 *hash;
2413
2414 #if TCHAR_IS_UTF16LE
2415         wdentry->filename = dentry->file_name;
2416         wdentry->dos_name = dentry->short_name;
2417 #else
2418         if (dentry_has_long_name(dentry)) {
2419                 ret = utf16le_to_tstr(dentry->file_name,
2420                                       dentry->file_name_nbytes,
2421                                       (tchar**)&wdentry->filename,
2422                                       &dummy);
2423                 if (ret)
2424                         return ret;
2425         }
2426         if (dentry_has_short_name(dentry)) {
2427                 ret = utf16le_to_tstr(dentry->short_name,
2428                                       dentry->short_name_nbytes,
2429                                       (tchar**)&wdentry->dos_name,
2430                                       &dummy);
2431                 if (ret)
2432                         return ret;
2433         }
2434 #endif
2435         ret = calculate_dentry_full_path(dentry);
2436         if (ret)
2437                 return ret;
2438         wdentry->full_path = dentry->_full_path;
2439
2440         for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->parent)
2441                 wdentry->depth++;
2442
2443         if (inode->i_security_id >= 0) {
2444                 const struct wim_security_data *sd = wim_const_security_data(wim);
2445                 wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
2446                 wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
2447         }
2448         wdentry->reparse_tag = inode->i_reparse_tag;
2449         wdentry->num_links = inode->i_nlink;
2450         wdentry->attributes = inode->i_attributes;
2451         wdentry->hard_link_group_id = inode->i_ino;
2452         wdentry->creation_time = wim_timestamp_to_timespec(inode->i_creation_time);
2453         wdentry->last_write_time = wim_timestamp_to_timespec(inode->i_last_write_time);
2454         wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
2455
2456         lte = inode_unnamed_lte(inode, wim->lookup_table);
2457         if (lte) {
2458                 lte_to_wimlib_resource_entry(lte, &wdentry->streams[0].resource);
2459         } else if (!is_zero_hash(hash = inode_unnamed_stream_hash(inode))) {
2460                 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
2461                         return resource_not_found_error(inode, hash);
2462                 copy_hash(wdentry->streams[0].resource.sha1_hash, hash);
2463                 wdentry->streams[0].resource.is_missing = 1;
2464         }
2465
2466         for (unsigned i = 0; i < inode->i_num_ads; i++) {
2467                 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
2468                         continue;
2469                 lte = inode_stream_lte(inode, i + 1, wim->lookup_table);
2470                 wdentry->num_named_streams++;
2471                 if (lte) {
2472                         lte_to_wimlib_resource_entry(lte, &wdentry->streams[
2473                                                                 wdentry->num_named_streams].resource);
2474                 } else if (!is_zero_hash(hash = inode_stream_hash(inode, i + 1))) {
2475                         if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
2476                                 return resource_not_found_error(inode, hash);
2477                         copy_hash(wdentry->streams[
2478                                   wdentry->num_named_streams].resource.sha1_hash, hash);
2479                         wdentry->streams[
2480                                 wdentry->num_named_streams].resource.is_missing = 1;
2481                 }
2482         #if TCHAR_IS_UTF16LE
2483                 wdentry->streams[wdentry->num_named_streams].stream_name =
2484                                 inode->i_ads_entries[i].stream_name;
2485         #else
2486                 size_t dummy;
2487
2488                 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
2489                                       inode->i_ads_entries[i].stream_name_nbytes,
2490                                       (tchar**)&wdentry->streams[
2491                                                 wdentry->num_named_streams].stream_name,
2492                                       &dummy);
2493                 if (ret)
2494                         return ret;
2495         #endif
2496         }
2497         return 0;
2498 }
2499
2500 static void
2501 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
2502 {
2503 #if !TCHAR_IS_UTF16LE
2504         FREE((tchar*)wdentry->filename);
2505         FREE((tchar*)wdentry->dos_name);
2506         for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
2507                 FREE((tchar*)wdentry->streams[i].stream_name);
2508 #endif
2509         FREE(wdentry);
2510 }
2511
2512 struct iterate_dir_tree_ctx {
2513         WIMStruct *wim;
2514         int flags;
2515         wimlib_iterate_dir_tree_callback_t cb;
2516         void *user_ctx;
2517 };
2518
2519 static int
2520 do_iterate_dir_tree(WIMStruct *wim,
2521                     struct wim_dentry *dentry, int flags,
2522                     wimlib_iterate_dir_tree_callback_t cb,
2523                     void *user_ctx);
2524
2525 static int
2526 call_do_iterate_dir_tree(struct wim_dentry *dentry, void *_ctx)
2527 {
2528         struct iterate_dir_tree_ctx *ctx = _ctx;
2529         return do_iterate_dir_tree(ctx->wim, dentry, ctx->flags,
2530                                    ctx->cb, ctx->user_ctx);
2531 }
2532
2533 static int
2534 do_iterate_dir_tree(WIMStruct *wim,
2535                     struct wim_dentry *dentry, int flags,
2536                     wimlib_iterate_dir_tree_callback_t cb,
2537                     void *user_ctx)
2538 {
2539         struct wimlib_dir_entry *wdentry;
2540         int ret = WIMLIB_ERR_NOMEM;
2541
2542
2543         wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
2544                                   (1 + dentry->d_inode->i_num_ads) *
2545                                         sizeof(struct wimlib_stream_entry));
2546         if (wdentry == NULL)
2547                 goto out;
2548
2549         ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
2550         if (ret)
2551                 goto out_free_wimlib_dentry;
2552
2553         if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
2554                 ret = (*cb)(wdentry, user_ctx);
2555                 if (ret)
2556                         goto out_free_wimlib_dentry;
2557         }
2558
2559         if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
2560                      WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
2561         {
2562                 struct iterate_dir_tree_ctx ctx = {
2563                         .wim      = wim,
2564                         .flags    = flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
2565                         .cb       = cb,
2566                         .user_ctx = user_ctx,
2567                 };
2568                 ret = for_dentry_child(dentry, call_do_iterate_dir_tree, &ctx);
2569         }
2570 out_free_wimlib_dentry:
2571         free_wimlib_dentry(wdentry);
2572 out:
2573         return ret;
2574 }
2575
2576 struct image_iterate_dir_tree_ctx {
2577         const tchar *path;
2578         int flags;
2579         wimlib_iterate_dir_tree_callback_t cb;
2580         void *user_ctx;
2581 };
2582
2583
2584 static int
2585 image_do_iterate_dir_tree(WIMStruct *wim)
2586 {
2587         struct image_iterate_dir_tree_ctx *ctx = wim->private;
2588         struct wim_dentry *dentry;
2589
2590         dentry = get_dentry(wim, ctx->path);
2591         if (dentry == NULL)
2592                 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
2593         return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
2594 }
2595
2596 /* API function documented in wimlib.h  */
2597 WIMLIBAPI int
2598 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *path,
2599                         int flags,
2600                         wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
2601 {
2602         struct image_iterate_dir_tree_ctx ctx = {
2603                 .path = path,
2604                 .flags = flags,
2605                 .cb = cb,
2606                 .user_ctx = user_ctx,
2607         };
2608         wim->private = &ctx;
2609         return for_image(wim, image, image_do_iterate_dir_tree);
2610 }
2611
2612 /* Returns %true iff the metadata of @inode and @template_inode are reasonably
2613  * consistent with them being the same, unmodified file.  */
2614 static bool
2615 inode_metadata_consistent(const struct wim_inode *inode,
2616                           const struct wim_inode *template_inode,
2617                           const struct wim_lookup_table *template_lookup_table)
2618 {
2619         /* Must have exact same creation time and last write time.  */
2620         if (inode->i_creation_time != template_inode->i_creation_time ||
2621             inode->i_last_write_time != template_inode->i_last_write_time)
2622                 return false;
2623
2624         /* Last access time may have stayed the same or increased, but certainly
2625          * shouldn't have decreased.  */
2626         if (inode->i_last_access_time < template_inode->i_last_access_time)
2627                 return false;
2628
2629         /* Must have same number of alternate data stream entries.  */
2630         if (inode->i_num_ads != template_inode->i_num_ads)
2631                 return false;
2632
2633         /* If the stream entries for the inode are for some reason not resolved,
2634          * then the hashes are already available and the point of this function
2635          * is defeated.  */
2636         if (!inode->i_resolved)
2637                 return false;
2638
2639         /* Iterate through each stream and do some more checks.  */
2640         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2641                 const struct wim_lookup_table_entry *lte, *template_lte;
2642
2643                 lte = inode_stream_lte_resolved(inode, i);
2644                 template_lte = inode_stream_lte(template_inode, i,
2645                                                 template_lookup_table);
2646
2647                 /* Compare stream sizes.  */
2648                 if (lte && template_lte) {
2649                         if (lte->size != template_lte->size)
2650                                 return false;
2651
2652                         /* If hash happens to be available, compare with template.  */
2653                         if (!lte->unhashed && !template_lte->unhashed &&
2654                             !hashes_equal(lte->hash, template_lte->hash))
2655                                 return false;
2656
2657                 } else if (lte && lte->size) {
2658                         return false;
2659                 } else if (template_lte && template_lte->size) {
2660                         return false;
2661                 }
2662         }
2663
2664         /* All right, barring a full checksum and given that the inodes share a
2665          * path and the user isn't trying to trick us, these inodes most likely
2666          * refer to the same file.  */
2667         return true;
2668 }
2669
2670 /**
2671  * Given an inode @inode that has been determined to be "the same" as another
2672  * inode @template_inode in either the same WIM or another WIM, retrieve some
2673  * useful stream information (e.g. checksums) from @template_inode.
2674  *
2675  * This assumes that the streams for @inode have been resolved (to point
2676  * directly to the appropriate `struct wim_lookup_table_entry's)  but do not
2677  * necessarily have checksum information filled in.
2678  */
2679 static int
2680 inode_copy_checksums(struct wim_inode *inode,
2681                      struct wim_inode *template_inode,
2682                      WIMStruct *wim,
2683                      WIMStruct *template_wim)
2684 {
2685         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2686                 struct wim_lookup_table_entry *lte, *template_lte;
2687                 struct wim_lookup_table_entry *replace_lte;
2688
2689                 lte = inode_stream_lte_resolved(inode, i);
2690                 template_lte = inode_stream_lte(template_inode, i,
2691                                                 template_wim->lookup_table);
2692
2693                 /* Only take action if both entries exist, the entry for @inode
2694                  * has no checksum calculated, but the entry for @template_inode
2695                  * does.  */
2696                 if (lte == NULL || template_lte == NULL ||
2697                     !lte->unhashed || template_lte->unhashed)
2698                         continue;
2699
2700                 wimlib_assert(lte->refcnt == inode->i_nlink);
2701
2702                 /* If the WIM of the template image is the same as the WIM of
2703                  * the new image, then @template_lte can be used directly.
2704                  *
2705                  * Otherwise, look for a stream with the same hash in the WIM of
2706                  * the new image.  If found, use it; otherwise re-use the entry
2707                  * being discarded, filling in the hash.  */
2708
2709                 if (wim == template_wim)
2710                         replace_lte = template_lte;
2711                 else
2712                         replace_lte = lookup_resource(wim->lookup_table,
2713                                                       template_lte->hash);
2714
2715                 list_del(&lte->unhashed_list);
2716                 if (replace_lte) {
2717                         free_lookup_table_entry(lte);
2718                 } else {
2719                         copy_hash(lte->hash, template_lte->hash);
2720                         lte->unhashed = 0;
2721                         lookup_table_insert(wim->lookup_table, lte);
2722                         lte->refcnt = 0;
2723                         replace_lte = lte;
2724                 }
2725
2726                 if (i == 0)
2727                         inode->i_lte = replace_lte;
2728                 else
2729                         inode->i_ads_entries[i - 1].lte = replace_lte;
2730
2731                 replace_lte->refcnt += inode->i_nlink;
2732         }
2733         return 0;
2734 }
2735
2736 struct reference_template_args {
2737         WIMStruct *wim;
2738         WIMStruct *template_wim;
2739 };
2740
2741 static int
2742 dentry_reference_template(struct wim_dentry *dentry, void *_args)
2743 {
2744         int ret;
2745         struct wim_dentry *template_dentry;
2746         struct wim_inode *inode, *template_inode;
2747         struct reference_template_args *args = _args;
2748         WIMStruct *wim = args->wim;
2749         WIMStruct *template_wim = args->template_wim;
2750
2751         if (dentry->d_inode->i_visited)
2752                 return 0;
2753
2754         ret = calculate_dentry_full_path(dentry);
2755         if (ret)
2756                 return ret;
2757
2758         template_dentry = get_dentry(template_wim, dentry->_full_path);
2759         if (template_dentry == NULL) {
2760                 DEBUG("\"%"TS"\": newly added file", dentry->_full_path);
2761                 return 0;
2762         }
2763
2764         inode = dentry->d_inode;
2765         template_inode = template_dentry->d_inode;
2766
2767         if (inode_metadata_consistent(inode, template_inode,
2768                                       template_wim->lookup_table)) {
2769                 /*DEBUG("\"%"TS"\": No change detected", dentry->_full_path);*/
2770                 ret = inode_copy_checksums(inode, template_inode,
2771                                            wim, template_wim);
2772                 inode->i_visited = 1;
2773         } else {
2774                 DEBUG("\"%"TS"\": change detected!", dentry->_full_path);
2775                 ret = 0;
2776         }
2777         return ret;
2778 }
2779
2780 /* API function documented in wimlib.h  */
2781 WIMLIBAPI int
2782 wimlib_reference_template_image(WIMStruct *wim, int new_image,
2783                                 WIMStruct *template_wim, int template_image,
2784                                 int flags, wimlib_progress_func_t progress_func)
2785 {
2786         int ret;
2787         struct wim_image_metadata *new_imd;
2788
2789         if (wim == NULL || template_wim == NULL)
2790                 return WIMLIB_ERR_INVALID_PARAM;
2791
2792         if (wim == template_wim && new_image == template_image)
2793                 return WIMLIB_ERR_INVALID_PARAM;
2794
2795         if (new_image < 1 || new_image > wim->hdr.image_count)
2796                 return WIMLIB_ERR_INVALID_IMAGE;
2797
2798         if (!wim_has_metadata(wim))
2799                 return WIMLIB_ERR_METADATA_NOT_FOUND;
2800
2801         new_imd = wim->image_metadata[new_image - 1];
2802         if (!new_imd->modified)
2803                 return WIMLIB_ERR_INVALID_PARAM;
2804
2805         ret = select_wim_image(template_wim, template_image);
2806         if (ret)
2807                 return ret;
2808
2809         struct reference_template_args args = {
2810                 .wim = wim,
2811                 .template_wim = template_wim,
2812         };
2813
2814         ret = for_dentry_in_tree(new_imd->root_dentry,
2815                                  dentry_reference_template, &args);
2816         dentry_tree_clear_inode_visited(new_imd->root_dentry);
2817         return ret;
2818 }