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