]> wimlib.net Git - wimlib/blob - src/dentry.c
04ed1092900889fc2917d0ffea28c226c55221c2
[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/sha1.h"
42 #include "wimlib/timestamp.h"
43
44 #include <errno.h>
45
46 /* WIM alternate data stream entry (on-disk format) */
47 struct wim_ads_entry_on_disk {
48         /*  Length of the entry, in bytes.  This apparently includes all
49          *  fixed-length fields, plus the stream name and null terminator if
50          *  present, and the padding up to an 8 byte boundary.  wimlib is a
51          *  little less strict when reading the entries, and only requires that
52          *  the number of bytes from this field is at least as large as the size
53          *  of the fixed length fields and stream name without null terminator.
54          *  */
55         le64  length;
56
57         le64  reserved;
58
59         /* SHA1 message digest of the uncompressed stream; or, alternatively,
60          * can be all zeroes if the stream has zero length. */
61         u8 hash[SHA1_HASH_SIZE];
62
63         /* Length of the stream name, in bytes.  0 if the stream is unnamed.  */
64         le16 stream_name_nbytes;
65
66         /* Stream name in UTF-16LE.  It is @stream_name_nbytes bytes long,
67          * excluding the the null terminator.  There is a null terminator
68          * character if @stream_name_nbytes != 0; i.e., if this stream is named.
69          * */
70         utf16lechar stream_name[];
71 } _packed_attribute;
72
73 #define WIM_ADS_ENTRY_DISK_SIZE 38
74
75 /* WIM directory entry (on-disk format) */
76 struct wim_dentry_on_disk {
77         le64 length;
78         le32 attributes;
79         sle32 security_id;
80         le64 subdir_offset;
81         le64 unused_1;
82         le64 unused_2;
83         le64 creation_time;
84         le64 last_access_time;
85         le64 last_write_time;
86         u8 unnamed_stream_hash[SHA1_HASH_SIZE];
87         union {
88                 struct {
89                         le32 rp_unknown_1;
90                         le32 reparse_tag;
91                         le16 rp_unknown_2;
92                         le16 not_rpfixed;
93                 } _packed_attribute reparse;
94                 struct {
95                         le32 rp_unknown_1;
96                         le64 hard_link_group_id;
97                 } _packed_attribute nonreparse;
98         };
99         le16 num_alternate_data_streams;
100         le16 short_name_nbytes;
101         le16 file_name_nbytes;
102
103         /* Follewed by variable length file name, if file_name_nbytes != 0 */
104         utf16lechar file_name[];
105
106         /* Followed by variable length short name, if short_name_nbytes != 0 */
107         /*utf16lechar short_name[];*/
108 } _packed_attribute;
109
110 #define WIM_DENTRY_DISK_SIZE 102
111
112 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
113  * a file name and short name that take the specified numbers of bytes.  This
114  * excludes any alternate data stream entries that may follow the dentry. */
115 static u64
116 _dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
117 {
118         u64 length = sizeof(struct wim_dentry_on_disk);
119         if (file_name_nbytes)
120                 length += file_name_nbytes + 2;
121         if (short_name_nbytes)
122                 length += short_name_nbytes + 2;
123         return length;
124 }
125
126 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
127  * the file name length and short name length.  Note that dentry->length is
128  * ignored; also, this excludes any alternate data stream entries that may
129  * follow the dentry. */
130 static u64
131 dentry_correct_length_unaligned(const struct wim_dentry *dentry)
132 {
133         return _dentry_correct_length_unaligned(dentry->file_name_nbytes,
134                                                 dentry->short_name_nbytes);
135 }
136
137 /* Duplicates a string of system-dependent encoding into a UTF-16LE string and
138  * returns the string and its length, in bytes, in the pointer arguments.  Frees
139  * any existing string at the return location before overwriting it. */
140 static int
141 get_utf16le_name(const tchar *name, utf16lechar **name_utf16le_ret,
142                  u16 *name_utf16le_nbytes_ret)
143 {
144         utf16lechar *name_utf16le;
145         size_t name_utf16le_nbytes;
146         int ret;
147 #if TCHAR_IS_UTF16LE
148         name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
149         name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
150         if (!name_utf16le)
151                 return WIMLIB_ERR_NOMEM;
152         memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
153         ret = 0;
154 #else
155
156         ret = tstr_to_utf16le(name, tstrlen(name), &name_utf16le,
157                               &name_utf16le_nbytes);
158         if (ret == 0) {
159                 if (name_utf16le_nbytes > 0xffff) {
160                         FREE(name_utf16le);
161                         ERROR("Multibyte string \"%"TS"\" is too long!", name);
162                         ret = WIMLIB_ERR_INVALID_UTF8_STRING;
163                 }
164         }
165 #endif
166         if (ret == 0) {
167                 FREE(*name_utf16le_ret);
168                 *name_utf16le_ret = name_utf16le;
169                 *name_utf16le_nbytes_ret = name_utf16le_nbytes;
170         }
171         return ret;
172 }
173
174 /* Sets the name of a WIM dentry from a multibyte string. */
175 int
176 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name)
177 {
178         int ret;
179         ret = get_utf16le_name(new_name, &dentry->file_name,
180                                &dentry->file_name_nbytes);
181         if (ret == 0) {
182                 /* Clear the short name and recalculate the dentry length */
183                 if (dentry_has_short_name(dentry)) {
184                         FREE(dentry->short_name);
185                         dentry->short_name = NULL;
186                         dentry->short_name_nbytes = 0;
187                 }
188         }
189         return ret;
190 }
191
192 /* Returns the total length of a WIM alternate data stream entry on-disk,
193  * including the stream name, the null terminator, AND the padding after the
194  * entry to align the next ADS entry or dentry on an 8-byte boundary. */
195 static u64
196 ads_entry_total_length(const struct wim_ads_entry *entry)
197 {
198         u64 len = sizeof(struct wim_ads_entry_on_disk);
199         if (entry->stream_name_nbytes)
200                 len += entry->stream_name_nbytes + 2;
201         return (len + 7) & ~7;
202 }
203
204
205 static u64
206 _dentry_total_length(const struct wim_dentry *dentry, u64 length)
207 {
208         const struct wim_inode *inode = dentry->d_inode;
209         for (u16 i = 0; i < inode->i_num_ads; i++)
210                 length += ads_entry_total_length(&inode->i_ads_entries[i]);
211         return (length + 7) & ~7;
212 }
213
214 /* Calculate the aligned *total* length of an on-disk WIM dentry.  This includes
215  * all alternate data streams. */
216 u64
217 dentry_correct_total_length(const struct wim_dentry *dentry)
218 {
219         return _dentry_total_length(dentry,
220                                     dentry_correct_length_unaligned(dentry));
221 }
222
223 /* Like dentry_correct_total_length(), but use the existing dentry->length field
224  * instead of calculating its "correct" value. */
225 static u64
226 dentry_total_length(const struct wim_dentry *dentry)
227 {
228         return _dentry_total_length(dentry, dentry->length);
229 }
230
231 int
232 for_dentry_in_rbtree(struct rb_node *root,
233                      int (*visitor)(struct wim_dentry *, void *),
234                      void *arg)
235 {
236         int ret;
237         struct rb_node *node = root;
238         LIST_HEAD(stack);
239         while (1) {
240                 if (node) {
241                         list_add(&rbnode_dentry(node)->tmp_list, &stack);
242                         node = node->rb_left;
243                 } else {
244                         struct list_head *next;
245                         struct wim_dentry *dentry;
246
247                         next = stack.next;
248                         if (next == &stack)
249                                 return 0;
250                         dentry = container_of(next, struct wim_dentry, tmp_list);
251                         list_del(next);
252                         ret = visitor(dentry, arg);
253                         if (ret != 0)
254                                 return ret;
255                         node = dentry->rb_node.rb_right;
256                 }
257         }
258 }
259
260 static int
261 for_dentry_tree_in_rbtree_depth(struct rb_node *node,
262                                 int (*visitor)(struct wim_dentry*, void*),
263                                 void *arg)
264 {
265         int ret;
266         if (node) {
267                 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
268                                                       visitor, arg);
269                 if (ret != 0)
270                         return ret;
271                 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
272                                                       visitor, arg);
273                 if (ret != 0)
274                         return ret;
275                 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
276                 if (ret != 0)
277                         return ret;
278         }
279         return 0;
280 }
281
282 static int
283 for_dentry_tree_in_rbtree(struct rb_node *node,
284                           int (*visitor)(struct wim_dentry*, void*),
285                           void *arg)
286 {
287         int ret;
288         if (node) {
289                 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
290                 if (ret)
291                         return ret;
292                 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
293                 if (ret)
294                         return ret;
295                 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
296                 if (ret)
297                         return ret;
298         }
299         return 0;
300 }
301
302 /* Calls a function on all directory entries in a WIM dentry tree.  Logically,
303  * this is a pre-order traversal (the function is called on a parent dentry
304  * before its children), but sibling dentries will be visited in order as well.
305  * */
306 int
307 for_dentry_in_tree(struct wim_dentry *root,
308                    int (*visitor)(struct wim_dentry*, void*), void *arg)
309 {
310         int ret;
311
312         if (!root)
313                 return 0;
314         ret = (*visitor)(root, arg);
315         if (ret)
316                 return ret;
317         return for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node,
318                                          visitor,
319                                          arg);
320 }
321
322 /* Like for_dentry_in_tree(), but the visitor function is always called on a
323  * dentry's children before on itself. */
324 int
325 for_dentry_in_tree_depth(struct wim_dentry *root,
326                          int (*visitor)(struct wim_dentry*, void*), void *arg)
327 {
328         int ret;
329
330         if (!root)
331                 return 0;
332         ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
333                                               visitor, arg);
334         if (ret)
335                 return ret;
336         return (*visitor)(root, arg);
337 }
338
339 /* Calculate the full path of @dentry.  The full path of its parent must have
340  * already been calculated, or it must be the root dentry. */
341 static int
342 calculate_dentry_full_path(struct wim_dentry *dentry)
343 {
344         tchar *full_path;
345         u32 full_path_nbytes;
346         int ret;
347
348         if (dentry->_full_path)
349                 return 0;
350
351         if (dentry_is_root(dentry)) {
352                 full_path = TSTRDUP(T("/"));
353                 if (!full_path)
354                         return WIMLIB_ERR_NOMEM;
355                 full_path_nbytes = 1 * sizeof(tchar);
356         } else {
357                 struct wim_dentry *parent;
358                 tchar *parent_full_path;
359                 u32 parent_full_path_nbytes;
360                 size_t filename_nbytes;
361
362                 parent = dentry->parent;
363                 if (dentry_is_root(parent)) {
364                         parent_full_path = T("");
365                         parent_full_path_nbytes = 0;
366                 } else {
367                         if (!parent->_full_path) {
368                                 ret = calculate_dentry_full_path(parent);
369                                 if (ret)
370                                         return ret;
371                         }
372                         parent_full_path = parent->_full_path;
373                         parent_full_path_nbytes = parent->full_path_nbytes;
374                 }
375
376                 /* Append this dentry's name as a tchar string to the full path
377                  * of the parent followed by the path separator */
378         #if TCHAR_IS_UTF16LE
379                 filename_nbytes = dentry->file_name_nbytes;
380         #else
381                 {
382                         int ret = utf16le_to_tstr_nbytes(dentry->file_name,
383                                                          dentry->file_name_nbytes,
384                                                          &filename_nbytes);
385                         if (ret)
386                                 return ret;
387                 }
388         #endif
389
390                 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
391                                    filename_nbytes;
392                 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
393                 if (!full_path)
394                         return WIMLIB_ERR_NOMEM;
395                 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
396                 full_path[parent_full_path_nbytes / sizeof(tchar)] = T('/');
397         #if TCHAR_IS_UTF16LE
398                 memcpy(&full_path[parent_full_path_nbytes / sizeof(tchar) + 1],
399                        dentry->file_name,
400                        filename_nbytes + sizeof(tchar));
401         #else
402                 utf16le_to_tstr_buf(dentry->file_name,
403                                     dentry->file_name_nbytes,
404                                     &full_path[parent_full_path_nbytes /
405                                                sizeof(tchar) + 1]);
406         #endif
407         }
408         dentry->_full_path = full_path;
409         dentry->full_path_nbytes= full_path_nbytes;
410         return 0;
411 }
412
413 static int
414 do_calculate_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
415 {
416         return calculate_dentry_full_path(dentry);
417 }
418
419 int
420 calculate_dentry_tree_full_paths(struct wim_dentry *root)
421 {
422         return for_dentry_in_tree(root, do_calculate_dentry_full_path, NULL);
423 }
424
425 tchar *
426 dentry_full_path(struct wim_dentry *dentry)
427 {
428         calculate_dentry_full_path(dentry);
429         return dentry->_full_path;
430 }
431
432 static int
433 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
434 {
435         *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
436         return 0;
437 }
438
439 static int
440 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
441 {
442         calculate_subdir_offsets(dentry, subdir_offset_p);
443         return 0;
444 }
445
446 /*
447  * Recursively calculates the subdir offsets for a directory tree.
448  *
449  * @dentry:  The root of the directory tree.
450  * @subdir_offset_p:  The current subdirectory offset; i.e., the subdirectory
451  *                    offset for @dentry.
452  */
453 void
454 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
455 {
456         struct rb_node *node;
457
458         dentry->subdir_offset = *subdir_offset_p;
459         node = dentry->d_inode->i_children.rb_node;
460         if (node) {
461                 /* Advance the subdir offset by the amount of space the children
462                  * of this dentry take up. */
463                 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
464
465                 /* End-of-directory dentry on disk. */
466                 *subdir_offset_p += 8;
467
468                 /* Recursively call calculate_subdir_offsets() on all the
469                  * children. */
470                 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
471         } else {
472                 /* On disk, childless directories have a valid subdir_offset
473                  * that points to an 8-byte end-of-directory dentry.  Regular
474                  * files or reparse points have a subdir_offset of 0. */
475                 if (dentry_is_directory(dentry))
476                         *subdir_offset_p += 8;
477                 else
478                         dentry->subdir_offset = 0;
479         }
480 }
481
482 /* UNIX: Case-sensitive UTF-16LE dentry or stream name comparison.  We call this
483  * on Windows as well to distinguish true duplicates from names differing by
484  * case only. */
485 static int
486 compare_utf16le_names_case_sensitive(const utf16lechar *name1, size_t nbytes1,
487                                      const utf16lechar *name2, size_t nbytes2)
488 {
489         /* Return the result if the strings differ up to their minimum length.
490          * Note that we cannot strcmp() or strncmp() here, as the strings are in
491          * UTF-16LE format. */
492         int result = memcmp(name1, name2, min(nbytes1, nbytes2));
493         if (result)
494                 return result;
495         /* The strings are the same up to their minimum length, so return a
496          * result based on their lengths. */
497         if (nbytes1 < nbytes2)
498                 return -1;
499         else if (nbytes1 > nbytes2)
500                 return 1;
501         else
502                 return 0;
503 }
504
505 #ifdef __WIN32__
506 /* Windoze: Case-insensitive UTF-16LE dentry or stream name comparison */
507 static int
508 compare_utf16le_names_case_insensitive(const utf16lechar *name1, size_t nbytes1,
509                                        const utf16lechar *name2, size_t nbytes2)
510 {
511         /* Only call _wcsicmp() if both strings are of nonzero length; otherwise
512          * one could be NULL. */
513         if (nbytes1 && nbytes2)
514                 return _wcsicmp((const wchar_t*)name1, (const wchar_t*)name2);
515         /* The strings are the same up to their minimum length, so return a
516          * result based on their lengths. */
517         if (nbytes1 < nbytes2)
518                 return -1;
519         else if (nbytes1 > nbytes2)
520                 return 1;
521         else
522                 return 0;
523 }
524 #endif /* __WIN32__ */
525
526 #ifdef __WIN32__
527 #  define compare_utf16le_names compare_utf16le_names_case_insensitive
528 #else
529 #  define compare_utf16le_names compare_utf16le_names_case_sensitive
530 #endif
531
532
533 #ifdef __WIN32__
534 static int
535 dentry_compare_names_case_insensitive(const struct wim_dentry *d1,
536                                       const struct wim_dentry *d2)
537 {
538         return compare_utf16le_names_case_insensitive(d1->file_name,
539                                                       d1->file_name_nbytes,
540                                                       d2->file_name,
541                                                       d2->file_name_nbytes);
542 }
543 #endif /* __WIN32__ */
544
545 static int
546 dentry_compare_names_case_sensitive(const struct wim_dentry *d1,
547                                     const struct wim_dentry *d2)
548 {
549         return compare_utf16le_names_case_sensitive(d1->file_name,
550                                                     d1->file_name_nbytes,
551                                                     d2->file_name,
552                                                     d2->file_name_nbytes);
553 }
554
555 #ifdef __WIN32__
556 #  define dentry_compare_names dentry_compare_names_case_insensitive
557 #else
558 #  define dentry_compare_names dentry_compare_names_case_sensitive
559 #endif
560
561 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
562  * stream name @name that has length @name_nbytes bytes. */
563 static inline bool
564 ads_entry_has_name(const struct wim_ads_entry *entry,
565                    const utf16lechar *name, size_t name_nbytes)
566 {
567         return !compare_utf16le_names(name, name_nbytes,
568                                       entry->stream_name,
569                                       entry->stream_name_nbytes);
570 }
571
572 struct wim_dentry *
573 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
574                                    const utf16lechar *name,
575                                    size_t name_nbytes)
576 {
577         struct rb_node *node = dentry->d_inode->i_children.rb_node;
578         struct wim_dentry *child;
579         while (node) {
580                 child = rbnode_dentry(node);
581                 int result = compare_utf16le_names(name, name_nbytes,
582                                                    child->file_name,
583                                                    child->file_name_nbytes);
584                 if (result < 0)
585                         node = node->rb_left;
586                 else if (result > 0)
587                         node = node->rb_right;
588                 else
589                         return child;
590         }
591         return NULL;
592 }
593
594 /* Returns the child of @dentry that has the file name @name.  Returns NULL if
595  * no child has the name. */
596 struct wim_dentry *
597 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
598 {
599 #if TCHAR_IS_UTF16LE
600         return get_dentry_child_with_utf16le_name(dentry, name,
601                                                   tstrlen(name) * sizeof(tchar));
602 #else
603         utf16lechar *utf16le_name;
604         size_t utf16le_name_nbytes;
605         int ret;
606         struct wim_dentry *child;
607
608         ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
609                               &utf16le_name, &utf16le_name_nbytes);
610         if (ret) {
611                 child = NULL;
612         } else {
613                 child = get_dentry_child_with_utf16le_name(dentry,
614                                                            utf16le_name,
615                                                            utf16le_name_nbytes);
616                 FREE(utf16le_name);
617         }
618         return child;
619 #endif
620 }
621
622 static struct wim_dentry *
623 get_dentry_utf16le(WIMStruct *wim, const utf16lechar *path)
624 {
625         struct wim_dentry *cur_dentry, *parent_dentry;
626         const utf16lechar *p, *pp;
627
628         cur_dentry = parent_dentry = wim_root_dentry(wim);
629         if (!cur_dentry) {
630                 errno = ENOENT;
631                 return NULL;
632         }
633         p = path;
634         while (1) {
635                 while (*p == cpu_to_le16('/'))
636                         p++;
637                 if (*p == cpu_to_le16('\0'))
638                         break;
639                 pp = p;
640                 while (*pp != cpu_to_le16('/') && *pp != cpu_to_le16('\0'))
641                         pp++;
642
643                 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
644                                                                 (void*)pp - (void*)p);
645                 if (cur_dentry == NULL)
646                         break;
647                 p = pp;
648                 parent_dentry = cur_dentry;
649         }
650         if (cur_dentry == NULL) {
651                 if (dentry_is_directory(parent_dentry))
652                         errno = ENOENT;
653                 else
654                         errno = ENOTDIR;
655         }
656         return cur_dentry;
657 }
658
659 /* Returns the dentry corresponding to the @path, or NULL if there is no such
660  * dentry. */
661 struct wim_dentry *
662 get_dentry(WIMStruct *wim, const tchar *path)
663 {
664 #if TCHAR_IS_UTF16LE
665         return get_dentry_utf16le(wim, path);
666 #else
667         utf16lechar *path_utf16le;
668         size_t path_utf16le_nbytes;
669         int ret;
670         struct wim_dentry *dentry;
671
672         ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
673                               &path_utf16le, &path_utf16le_nbytes);
674         if (ret)
675                 return NULL;
676         dentry = get_dentry_utf16le(wim, path_utf16le);
677         FREE(path_utf16le);
678         return dentry;
679 #endif
680 }
681
682 struct wim_inode *
683 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
684 {
685         struct wim_dentry *dentry;
686         dentry = get_dentry(wim, path);
687         if (dentry)
688                 return dentry->d_inode;
689         else
690                 return NULL;
691 }
692
693 /* Takes in a path of length @len in @buf, and transforms it into a string for
694  * the path of its parent directory. */
695 static void
696 to_parent_name(tchar *buf, size_t len)
697 {
698         ssize_t i = (ssize_t)len - 1;
699         while (i >= 0 && buf[i] == T('/'))
700                 i--;
701         while (i >= 0 && buf[i] != T('/'))
702                 i--;
703         while (i >= 0 && buf[i] == T('/'))
704                 i--;
705         buf[i + 1] = T('\0');
706 }
707
708 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
709  * if the dentry is not found. */
710 struct wim_dentry *
711 get_parent_dentry(WIMStruct *wim, const tchar *path)
712 {
713         size_t path_len = tstrlen(path);
714         tchar buf[path_len + 1];
715
716         tmemcpy(buf, path, path_len + 1);
717         to_parent_name(buf, path_len);
718         return get_dentry(wim, buf);
719 }
720
721 /* Prints the full path of a dentry. */
722 int
723 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
724 {
725         int ret = calculate_dentry_full_path(dentry);
726         if (ret)
727                 return ret;
728         tprintf(T("%"TS"\n"), dentry->_full_path);
729         return 0;
730 }
731
732 /* We want to be able to show the names of the file attribute flags that are
733  * set. */
734 struct file_attr_flag {
735         u32 flag;
736         const tchar *name;
737 };
738 struct file_attr_flag file_attr_flags[] = {
739         {FILE_ATTRIBUTE_READONLY,           T("READONLY")},
740         {FILE_ATTRIBUTE_HIDDEN,             T("HIDDEN")},
741         {FILE_ATTRIBUTE_SYSTEM,             T("SYSTEM")},
742         {FILE_ATTRIBUTE_DIRECTORY,          T("DIRECTORY")},
743         {FILE_ATTRIBUTE_ARCHIVE,            T("ARCHIVE")},
744         {FILE_ATTRIBUTE_DEVICE,             T("DEVICE")},
745         {FILE_ATTRIBUTE_NORMAL,             T("NORMAL")},
746         {FILE_ATTRIBUTE_TEMPORARY,          T("TEMPORARY")},
747         {FILE_ATTRIBUTE_SPARSE_FILE,        T("SPARSE_FILE")},
748         {FILE_ATTRIBUTE_REPARSE_POINT,      T("REPARSE_POINT")},
749         {FILE_ATTRIBUTE_COMPRESSED,         T("COMPRESSED")},
750         {FILE_ATTRIBUTE_OFFLINE,            T("OFFLINE")},
751         {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
752         {FILE_ATTRIBUTE_ENCRYPTED,          T("ENCRYPTED")},
753         {FILE_ATTRIBUTE_VIRTUAL,            T("VIRTUAL")},
754 };
755
756 /* Prints a directory entry.  @lookup_table is a pointer to the lookup table, if
757  * available.  If the dentry is unresolved and the lookup table is NULL, the
758  * lookup table entries will not be printed.  Otherwise, they will be. */
759 int
760 print_dentry(struct wim_dentry *dentry, void *lookup_table)
761 {
762         const u8 *hash;
763         struct wim_lookup_table_entry *lte;
764         const struct wim_inode *inode = dentry->d_inode;
765         tchar buf[50];
766
767         tprintf(T("[DENTRY]\n"));
768         tprintf(T("Length            = %"PRIu64"\n"), dentry->length);
769         tprintf(T("Attributes        = 0x%x\n"), inode->i_attributes);
770         for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
771                 if (file_attr_flags[i].flag & inode->i_attributes)
772                         tprintf(T("    FILE_ATTRIBUTE_%"TS" is set\n"),
773                                 file_attr_flags[i].name);
774         tprintf(T("Security ID       = %d\n"), inode->i_security_id);
775         tprintf(T("Subdir offset     = %"PRIu64"\n"), dentry->subdir_offset);
776
777         wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
778         tprintf(T("Creation Time     = %"TS"\n"), buf);
779
780         wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
781         tprintf(T("Last Access Time  = %"TS"\n"), buf);
782
783         wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
784         tprintf(T("Last Write Time   = %"TS"\n"), buf);
785
786         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
787                 tprintf(T("Reparse Tag       = 0x%"PRIx32"\n"), inode->i_reparse_tag);
788                 tprintf(T("Reparse Point Flags = 0x%"PRIx16"\n"),
789                         inode->i_not_rpfixed);
790                 tprintf(T("Reparse Point Unknown 2 = 0x%"PRIx32"\n"),
791                         inode->i_rp_unknown_2);
792         }
793         tprintf(T("Reparse Point Unknown 1 = 0x%"PRIx32"\n"),
794                 inode->i_rp_unknown_1);
795         tprintf(T("Hard Link Group   = 0x%"PRIx64"\n"), inode->i_ino);
796         tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
797         tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
798         if (dentry_has_long_name(dentry))
799                 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
800         if (dentry_has_short_name(dentry))
801                 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
802         if (dentry->_full_path)
803                 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
804
805         lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
806         if (lte) {
807                 print_lookup_table_entry(lte, stdout);
808         } else {
809                 hash = inode_stream_hash(inode, 0);
810                 if (hash) {
811                         tprintf(T("Hash              = 0x"));
812                         print_hash(hash, stdout);
813                         tputchar(T('\n'));
814                         tputchar(T('\n'));
815                 }
816         }
817         for (u16 i = 0; i < inode->i_num_ads; i++) {
818                 tprintf(T("[Alternate Stream Entry %u]\n"), i);
819                 wimlib_printf(T("Name = \"%"WS"\"\n"),
820                               inode->i_ads_entries[i].stream_name);
821                 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
822                        inode->i_ads_entries[i].stream_name_nbytes);
823                 hash = inode_stream_hash(inode, i + 1);
824                 if (hash) {
825                         tprintf(T("Hash              = 0x"));
826                         print_hash(hash, stdout);
827                         tputchar(T('\n'));
828                 }
829                 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
830                                          stdout);
831         }
832         return 0;
833 }
834
835 /* Initializations done on every `struct wim_dentry'. */
836 static void
837 dentry_common_init(struct wim_dentry *dentry)
838 {
839         memset(dentry, 0, sizeof(struct wim_dentry));
840 }
841
842 struct wim_inode *
843 new_timeless_inode(void)
844 {
845         struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
846         if (inode) {
847                 inode->i_security_id = -1;
848                 inode->i_nlink = 1;
849                 inode->i_next_stream_id = 1;
850                 inode->i_not_rpfixed = 1;
851                 INIT_LIST_HEAD(&inode->i_list);
852         #ifdef WITH_FUSE
853                 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
854                         ERROR_WITH_ERRNO("Error initializing mutex");
855                         FREE(inode);
856                         return NULL;
857                 }
858         #endif
859                 INIT_LIST_HEAD(&inode->i_dentry);
860         }
861         return inode;
862 }
863
864 static struct wim_inode *
865 new_inode(void)
866 {
867         struct wim_inode *inode = new_timeless_inode();
868         if (inode) {
869                 u64 now = get_wim_timestamp();
870                 inode->i_creation_time = now;
871                 inode->i_last_access_time = now;
872                 inode->i_last_write_time = now;
873         }
874         return inode;
875 }
876
877 /* Creates an unlinked directory entry. */
878 int
879 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
880 {
881         struct wim_dentry *dentry;
882         int ret;
883
884         dentry = MALLOC(sizeof(struct wim_dentry));
885         if (!dentry)
886                 return WIMLIB_ERR_NOMEM;
887
888         dentry_common_init(dentry);
889         ret = set_dentry_name(dentry, name);
890         if (ret == 0) {
891                 dentry->parent = dentry;
892                 *dentry_ret = dentry;
893         } else {
894                 FREE(dentry);
895                 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
896                       name);
897         }
898         return ret;
899 }
900
901
902 static int
903 _new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
904                         bool timeless)
905 {
906         struct wim_dentry *dentry;
907         int ret;
908
909         ret = new_dentry(name, &dentry);
910         if (ret)
911                 return ret;
912
913         if (timeless)
914                 dentry->d_inode = new_timeless_inode();
915         else
916                 dentry->d_inode = new_inode();
917         if (!dentry->d_inode) {
918                 free_dentry(dentry);
919                 return WIMLIB_ERR_NOMEM;
920         }
921
922         inode_add_dentry(dentry, dentry->d_inode);
923         *dentry_ret = dentry;
924         return 0;
925 }
926
927 int
928 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
929 {
930         return _new_dentry_with_inode(name, dentry_ret, true);
931 }
932
933 int
934 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
935 {
936         return _new_dentry_with_inode(name, dentry_ret, false);
937 }
938
939 int
940 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
941 {
942         int ret;
943         struct wim_dentry *dentry;
944
945         DEBUG("Creating filler directory \"%"TS"\"", name);
946         ret = new_dentry_with_inode(name, &dentry);
947         if (ret)
948                 return ret;
949         /* Leave the inode number as 0; this is allowed for non
950          * hard-linked files. */
951         dentry->d_inode->i_resolved = 1;
952         dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
953         *dentry_ret = dentry;
954         return 0;
955 }
956
957 static int
958 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
959                size_t name_nbytes, bool is_utf16le)
960 {
961         int ret = 0;
962         memset(ads_entry, 0, sizeof(*ads_entry));
963
964         if (is_utf16le) {
965                 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
966                 if (!p)
967                         return WIMLIB_ERR_NOMEM;
968                 memcpy(p, name, name_nbytes);
969                 p[name_nbytes / 2] = cpu_to_le16(0);
970                 ads_entry->stream_name = p;
971                 ads_entry->stream_name_nbytes = name_nbytes;
972         } else {
973                 if (name && *(const tchar*)name != T('\0')) {
974                         ret = get_utf16le_name(name, &ads_entry->stream_name,
975                                                &ads_entry->stream_name_nbytes);
976                 }
977         }
978         return ret;
979 }
980
981 static void
982 destroy_ads_entry(struct wim_ads_entry *ads_entry)
983 {
984         FREE(ads_entry->stream_name);
985 }
986
987 /* Frees an inode. */
988 void
989 free_inode(struct wim_inode *inode)
990 {
991         if (inode) {
992                 if (inode->i_ads_entries) {
993                         for (u16 i = 0; i < inode->i_num_ads; i++)
994                                 destroy_ads_entry(&inode->i_ads_entries[i]);
995                         FREE(inode->i_ads_entries);
996                 }
997         #ifdef WITH_FUSE
998                 wimlib_assert(inode->i_num_opened_fds == 0);
999                 FREE(inode->i_fds);
1000                 pthread_mutex_destroy(&inode->i_mutex);
1001         #endif
1002                 /* HACK: This may instead delete the inode from i_list, but the
1003                  * hlist_del() behaves the same as list_del(). */
1004                 hlist_del(&inode->i_hlist);
1005                 FREE(inode->i_extracted_file);
1006                 FREE(inode);
1007         }
1008 }
1009
1010 /* Decrements link count on an inode and frees it if the link count reaches 0.
1011  * */
1012 static void
1013 put_inode(struct wim_inode *inode)
1014 {
1015         wimlib_assert(inode->i_nlink != 0);
1016         if (--inode->i_nlink == 0) {
1017         #ifdef WITH_FUSE
1018                 if (inode->i_num_opened_fds == 0)
1019         #endif
1020                 {
1021                         free_inode(inode);
1022                 }
1023         }
1024 }
1025
1026 /* Frees a WIM dentry.
1027  *
1028  * The corresponding inode (if any) is freed only if its link count is
1029  * decremented to 0.
1030  */
1031 void
1032 free_dentry(struct wim_dentry *dentry)
1033 {
1034         if (dentry) {
1035                 FREE(dentry->file_name);
1036                 FREE(dentry->short_name);
1037                 FREE(dentry->_full_path);
1038                 if (dentry->d_inode)
1039                         put_inode(dentry->d_inode);
1040                 FREE(dentry);
1041         }
1042 }
1043
1044 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
1045  * to free a directory tree. */
1046 static int
1047 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
1048 {
1049         struct wim_lookup_table *lookup_table = _lookup_table;
1050
1051         if (lookup_table) {
1052                 struct wim_inode *inode = dentry->d_inode;
1053                 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1054                         struct wim_lookup_table_entry *lte;
1055
1056                         lte = inode_stream_lte(inode, i, lookup_table);
1057                         if (lte)
1058                                 lte_decrement_refcnt(lte, lookup_table);
1059                 }
1060         }
1061         free_dentry(dentry);
1062         return 0;
1063 }
1064
1065 /*
1066  * Unlinks and frees a dentry tree.
1067  *
1068  * @root:               The root of the tree.
1069  * @lookup_table:       The lookup table for dentries.  If non-NULL, the
1070  *                      reference counts in the lookup table for the lookup
1071  *                      table entries corresponding to the dentries will be
1072  *                      decremented.
1073  */
1074 void
1075 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1076 {
1077         for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1078 }
1079
1080 /*
1081  * Links a dentry into the directory tree.
1082  *
1083  * @parent: The dentry that will be the parent of @child.
1084  * @child: The dentry to link.
1085  *
1086  * Returns NULL if successful.  If @parent already contains a dentry with the
1087  * same name as @child (see compare_utf16le_names() for what names are
1088  * considered the "same"), the pointer to this duplicate dentry is returned.
1089  */
1090 struct wim_dentry *
1091 dentry_add_child(struct wim_dentry * restrict parent,
1092                  struct wim_dentry * restrict child)
1093 {
1094         wimlib_assert(dentry_is_directory(parent));
1095         wimlib_assert(parent != child);
1096
1097         struct rb_root *root = &parent->d_inode->i_children;
1098         struct rb_node **new = &(root->rb_node);
1099         struct rb_node *rb_parent = NULL;
1100
1101         while (*new) {
1102                 struct wim_dentry *this = rbnode_dentry(*new);
1103                 int result = dentry_compare_names(child, this);
1104
1105                 rb_parent = *new;
1106
1107                 if (result < 0)
1108                         new = &((*new)->rb_left);
1109                 else if (result > 0)
1110                         new = &((*new)->rb_right);
1111                 else
1112                         return this;
1113         }
1114         child->parent = parent;
1115         rb_link_node(&child->rb_node, rb_parent, new);
1116         rb_insert_color(&child->rb_node, root);
1117         return NULL;
1118 }
1119
1120 /* Unlink a WIM dentry from the directory entry tree. */
1121 void
1122 unlink_dentry(struct wim_dentry *dentry)
1123 {
1124         if (!dentry_is_root(dentry))
1125                 rb_erase(&dentry->rb_node, &dentry->parent->d_inode->i_children);
1126 }
1127
1128 /*
1129  * Returns the alternate data stream entry belonging to @inode that has the
1130  * stream name @stream_name.
1131  */
1132 struct wim_ads_entry *
1133 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1134                     u16 *idx_ret)
1135 {
1136         if (inode->i_num_ads == 0) {
1137                 return NULL;
1138         } else {
1139                 size_t stream_name_utf16le_nbytes;
1140                 u16 i;
1141                 struct wim_ads_entry *result;
1142
1143         #if TCHAR_IS_UTF16LE
1144                 const utf16lechar *stream_name_utf16le;
1145
1146                 stream_name_utf16le = stream_name;
1147                 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1148         #else
1149                 utf16lechar *stream_name_utf16le;
1150
1151                 {
1152                         int ret = tstr_to_utf16le(stream_name,
1153                                                   tstrlen(stream_name) *
1154                                                       sizeof(tchar),
1155                                                   &stream_name_utf16le,
1156                                                   &stream_name_utf16le_nbytes);
1157                         if (ret)
1158                                 return NULL;
1159                 }
1160         #endif
1161                 i = 0;
1162                 result = NULL;
1163                 do {
1164                         if (ads_entry_has_name(&inode->i_ads_entries[i],
1165                                                stream_name_utf16le,
1166                                                stream_name_utf16le_nbytes))
1167                         {
1168                                 if (idx_ret)
1169                                         *idx_ret = i;
1170                                 result = &inode->i_ads_entries[i];
1171                                 break;
1172                         }
1173                 } while (++i != inode->i_num_ads);
1174         #if !TCHAR_IS_UTF16LE
1175                 FREE(stream_name_utf16le);
1176         #endif
1177                 return result;
1178         }
1179 }
1180
1181 static struct wim_ads_entry *
1182 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1183                  size_t stream_name_nbytes, bool is_utf16le)
1184 {
1185         u16 num_ads;
1186         struct wim_ads_entry *ads_entries;
1187         struct wim_ads_entry *new_entry;
1188
1189         if (inode->i_num_ads >= 0xfffe) {
1190                 ERROR("Too many alternate data streams in one inode!");
1191                 return NULL;
1192         }
1193         num_ads = inode->i_num_ads + 1;
1194         ads_entries = REALLOC(inode->i_ads_entries,
1195                               num_ads * sizeof(inode->i_ads_entries[0]));
1196         if (!ads_entries) {
1197                 ERROR("Failed to allocate memory for new alternate data stream");
1198                 return NULL;
1199         }
1200         inode->i_ads_entries = ads_entries;
1201
1202         new_entry = &inode->i_ads_entries[num_ads - 1];
1203         if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1204                 return NULL;
1205         new_entry->stream_id = inode->i_next_stream_id++;
1206         inode->i_num_ads = num_ads;
1207         return new_entry;
1208 }
1209
1210 struct wim_ads_entry *
1211 inode_add_ads_utf16le(struct wim_inode *inode,
1212                       const utf16lechar *stream_name,
1213                       size_t stream_name_nbytes)
1214 {
1215         DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1216         return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1217 }
1218
1219 /*
1220  * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1221  * NULL if memory could not be allocated.
1222  */
1223 struct wim_ads_entry *
1224 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1225 {
1226         DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1227         return do_inode_add_ads(inode, stream_name,
1228                                 tstrlen(stream_name) * sizeof(tchar),
1229                                 TCHAR_IS_UTF16LE);
1230 }
1231
1232 static struct wim_lookup_table_entry *
1233 add_stream_from_data_buffer(const void *buffer, size_t size,
1234                             struct wim_lookup_table *lookup_table)
1235 {
1236         u8 hash[SHA1_HASH_SIZE];
1237         struct wim_lookup_table_entry *lte, *existing_lte;
1238
1239         sha1_buffer(buffer, size, hash);
1240         existing_lte = __lookup_resource(lookup_table, hash);
1241         if (existing_lte) {
1242                 wimlib_assert(wim_resource_size(existing_lte) == size);
1243                 lte = existing_lte;
1244                 lte->refcnt++;
1245         } else {
1246                 void *buffer_copy;
1247                 lte = new_lookup_table_entry();
1248                 if (!lte)
1249                         return NULL;
1250                 buffer_copy = memdup(buffer, size);
1251                 if (!buffer_copy) {
1252                         free_lookup_table_entry(lte);
1253                         return NULL;
1254                 }
1255                 lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
1256                 lte->attached_buffer              = buffer_copy;
1257                 lte->resource_entry.original_size = size;
1258                 copy_hash(lte->hash, hash);
1259                 lookup_table_insert(lookup_table, lte);
1260         }
1261         return lte;
1262 }
1263
1264 int
1265 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1266                         const void *value, size_t size,
1267                         struct wim_lookup_table *lookup_table)
1268 {
1269         struct wim_ads_entry *new_ads_entry;
1270
1271         wimlib_assert(inode->i_resolved);
1272
1273         new_ads_entry = inode_add_ads(inode, name);
1274         if (!new_ads_entry)
1275                 return WIMLIB_ERR_NOMEM;
1276
1277         new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1278                                                          lookup_table);
1279         if (!new_ads_entry->lte) {
1280                 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1281                                  lookup_table);
1282                 return WIMLIB_ERR_NOMEM;
1283         }
1284         return 0;
1285 }
1286
1287 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
1288  * stream contents. */
1289 int
1290 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
1291                          struct wim_lookup_table *lookup_table)
1292 {
1293         inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
1294         if (!inode->i_lte)
1295                 return WIMLIB_ERR_NOMEM;
1296         inode->i_resolved = 1;
1297         return 0;
1298 }
1299
1300 /* Remove an alternate data stream from a WIM inode  */
1301 void
1302 inode_remove_ads(struct wim_inode *inode, u16 idx,
1303                  struct wim_lookup_table *lookup_table)
1304 {
1305         struct wim_ads_entry *ads_entry;
1306         struct wim_lookup_table_entry *lte;
1307
1308         wimlib_assert(idx < inode->i_num_ads);
1309         wimlib_assert(inode->i_resolved);
1310
1311         ads_entry = &inode->i_ads_entries[idx];
1312
1313         DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1314
1315         lte = ads_entry->lte;
1316         if (lte)
1317                 lte_decrement_refcnt(lte, lookup_table);
1318
1319         destroy_ads_entry(ads_entry);
1320
1321         memmove(&inode->i_ads_entries[idx],
1322                 &inode->i_ads_entries[idx + 1],
1323                 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1324         inode->i_num_ads--;
1325 }
1326
1327 #ifndef __WIN32__
1328 int
1329 inode_get_unix_data(const struct wim_inode *inode,
1330                     struct wimlib_unix_data *unix_data,
1331                     u16 *stream_idx_ret)
1332 {
1333         const struct wim_ads_entry *ads_entry;
1334         const struct wim_lookup_table_entry *lte;
1335         size_t size;
1336         int ret;
1337
1338         wimlib_assert(inode->i_resolved);
1339
1340         ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1341                                         WIMLIB_UNIX_DATA_TAG, NULL);
1342         if (!ads_entry)
1343                 return NO_UNIX_DATA;
1344
1345         if (stream_idx_ret)
1346                 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1347
1348         lte = ads_entry->lte;
1349         if (!lte)
1350                 return NO_UNIX_DATA;
1351
1352         size = wim_resource_size(lte);
1353         if (size != sizeof(struct wimlib_unix_data))
1354                 return BAD_UNIX_DATA;
1355
1356         ret = read_full_resource_into_buf(lte, unix_data);
1357         if (ret)
1358                 return ret;
1359
1360         if (unix_data->version != 0)
1361                 return BAD_UNIX_DATA;
1362         return 0;
1363 }
1364
1365 int
1366 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1367                     struct wim_lookup_table *lookup_table, int which)
1368 {
1369         struct wimlib_unix_data unix_data;
1370         int ret;
1371         bool have_good_unix_data = false;
1372         bool have_unix_data = false;
1373         u16 stream_idx;
1374
1375         if (!(which & UNIX_DATA_CREATE)) {
1376                 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1377                 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1378                         have_unix_data = true;
1379                 if (ret == 0)
1380                         have_good_unix_data = true;
1381         }
1382         unix_data.version = 0;
1383         if (which & UNIX_DATA_UID || !have_good_unix_data)
1384                 unix_data.uid = uid;
1385         if (which & UNIX_DATA_GID || !have_good_unix_data)
1386                 unix_data.gid = gid;
1387         if (which & UNIX_DATA_MODE || !have_good_unix_data)
1388                 unix_data.mode = mode;
1389         ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1390                                       &unix_data,
1391                                       sizeof(struct wimlib_unix_data),
1392                                       lookup_table);
1393         if (ret == 0 && have_unix_data)
1394                 inode_remove_ads(inode, stream_idx, lookup_table);
1395         return ret;
1396 }
1397 #endif /* !__WIN32__ */
1398
1399 /* Replace weird characters in filenames and alternate data stream names.
1400  *
1401  * In particular we do not want the path separator to appear in any names, as
1402  * that would make it possible for a "malicious" WIM to extract itself to any
1403  * location it wanted to. */
1404 static void
1405 replace_forbidden_characters(utf16lechar *name)
1406 {
1407         utf16lechar *p;
1408
1409         for (p = name; *p; p++) {
1410         #ifdef __WIN32__
1411                 if (wcschr(L"<>:\"/\\|?*", (wchar_t)*p))
1412         #else
1413                 if (*p == cpu_to_le16('/'))
1414         #endif
1415                 {
1416                         if (name) {
1417                                 WARNING("File, directory, or stream name \"%"WS"\"\n"
1418                                         "          contains forbidden characters; "
1419                                         "substituting replacement characters.",
1420                                         name);
1421                                 name = NULL;
1422                         }
1423                 #ifdef __WIN32__
1424                         *p = cpu_to_le16(0xfffd);
1425                 #else
1426                         *p = cpu_to_le16('?');
1427                 #endif
1428                 }
1429         }
1430 }
1431
1432 /*
1433  * Reads the alternate data stream entries of a WIM dentry.
1434  *
1435  * @p:  Pointer to buffer that starts with the first alternate stream entry.
1436  *
1437  * @inode:      Inode to load the alternate data streams into.
1438  *              @inode->i_num_ads must have been set to the number of
1439  *              alternate data streams that are expected.
1440  *
1441  * @remaining_size:     Number of bytes of data remaining in the buffer pointed
1442  *                      to by @p.
1443  *
1444  *
1445  * Return 0 on success or nonzero on failure.  On success, inode->i_ads_entries
1446  * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads.  On
1447  * failure, @inode is not modified.
1448  */
1449 static int
1450 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1451                  size_t nbytes_remaining)
1452 {
1453         u16 num_ads;
1454         struct wim_ads_entry *ads_entries;
1455         int ret;
1456
1457         BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
1458
1459         /* Allocate an array for our in-memory representation of the alternate
1460          * data stream entries. */
1461         num_ads = inode->i_num_ads;
1462         ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1463         if (!ads_entries)
1464                 goto out_of_memory;
1465
1466         /* Read the entries into our newly allocated buffer. */
1467         for (u16 i = 0; i < num_ads; i++) {
1468                 u64 length;
1469                 struct wim_ads_entry *cur_entry;
1470                 const struct wim_ads_entry_on_disk *disk_entry =
1471                         (const struct wim_ads_entry_on_disk*)p;
1472
1473                 cur_entry = &ads_entries[i];
1474                 ads_entries[i].stream_id = i + 1;
1475
1476                 /* Do we have at least the size of the fixed-length data we know
1477                  * need? */
1478                 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1479                         goto out_invalid;
1480
1481                 /* Read the length field */
1482                 length = le64_to_cpu(disk_entry->length);
1483
1484                 /* Make sure the length field is neither so small it doesn't
1485                  * include all the fixed-length data nor so large it overflows
1486                  * the metadata resource buffer. */
1487                 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1488                     length > nbytes_remaining)
1489                         goto out_invalid;
1490
1491                 /* Read the rest of the fixed-length data. */
1492
1493                 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1494                 copy_hash(cur_entry->hash, disk_entry->hash);
1495                 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1496
1497                 /* If stream_name_nbytes != 0, this is a named stream.
1498                  * Otherwise this is an unnamed stream, or in some cases (bugs
1499                  * in Microsoft's software I guess) a meaningless entry
1500                  * distinguished from the real unnamed stream entry, if any, by
1501                  * the fact that the real unnamed stream entry has a nonzero
1502                  * hash field. */
1503                 if (cur_entry->stream_name_nbytes) {
1504                         /* The name is encoded in UTF16-LE, which uses 2-byte
1505                          * coding units, so the length of the name had better be
1506                          * an even number of bytes... */
1507                         if (cur_entry->stream_name_nbytes & 1)
1508                                 goto out_invalid;
1509
1510                         /* Add the length of the stream name to get the length
1511                          * we actually need to read.  Make sure this isn't more
1512                          * than the specified length of the entry. */
1513                         if (sizeof(struct wim_ads_entry_on_disk) +
1514                             cur_entry->stream_name_nbytes > length)
1515                                 goto out_invalid;
1516
1517                         cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1518                         if (!cur_entry->stream_name)
1519                                 goto out_of_memory;
1520
1521                         memcpy(cur_entry->stream_name,
1522                                disk_entry->stream_name,
1523                                cur_entry->stream_name_nbytes);
1524                         cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1525                         replace_forbidden_characters(cur_entry->stream_name);
1526                 }
1527
1528                 /* It's expected that the size of every ADS entry is a multiple
1529                  * of 8.  However, to be safe, I'm allowing the possibility of
1530                  * an ADS entry at the very end of the metadata resource ending
1531                  * un-aligned.  So although we still need to increment the input
1532                  * pointer by @length to reach the next ADS entry, it's possible
1533                  * that less than @length is actually remaining in the metadata
1534                  * resource. We should set the remaining bytes to 0 if this
1535                  * happens. */
1536                 length = (length + 7) & ~(u64)7;
1537                 p += length;
1538                 if (nbytes_remaining < length)
1539                         nbytes_remaining = 0;
1540                 else
1541                         nbytes_remaining -= length;
1542         }
1543         inode->i_ads_entries = ads_entries;
1544         inode->i_next_stream_id = inode->i_num_ads + 1;
1545         ret = 0;
1546         goto out;
1547 out_of_memory:
1548         ret = WIMLIB_ERR_NOMEM;
1549         goto out_free_ads_entries;
1550 out_invalid:
1551         ERROR("An alternate data stream entry is invalid");
1552         ret = WIMLIB_ERR_INVALID_DENTRY;
1553 out_free_ads_entries:
1554         if (ads_entries) {
1555                 for (u16 i = 0; i < num_ads; i++)
1556                         destroy_ads_entry(&ads_entries[i]);
1557                 FREE(ads_entries);
1558         }
1559 out:
1560         return ret;
1561 }
1562
1563 /*
1564  * Reads a WIM directory entry, including all alternate data stream entries that
1565  * follow it, from the WIM image's metadata resource.
1566  *
1567  * @metadata_resource:
1568  *              Pointer to the metadata resource buffer.
1569  *
1570  * @metadata_resource_len:
1571  *              Length of the metadata resource buffer, in bytes.
1572  *
1573  * @offset:     Offset of the dentry within the metadata resource.
1574  *
1575  * @dentry:     A `struct wim_dentry' that will be filled in by this function.
1576  *
1577  * Return 0 on success or nonzero on failure.  On failure, @dentry will have
1578  * been modified, but it will not be left with pointers to any allocated
1579  * buffers.  On success, the dentry->length field must be examined.  If zero,
1580  * this was a special "end of directory" dentry and not a real dentry.  If
1581  * nonzero, this was a real dentry.
1582  *
1583  * Possible errors include:
1584  *      WIMLIB_ERR_NOMEM
1585  *      WIMLIB_ERR_INVALID_DENTRY
1586  */
1587 int
1588 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1589             u64 offset, struct wim_dentry * restrict dentry)
1590 {
1591
1592         u64 calculated_size;
1593         utf16lechar *file_name;
1594         utf16lechar *short_name;
1595         u16 short_name_nbytes;
1596         u16 file_name_nbytes;
1597         int ret;
1598         struct wim_inode *inode;
1599         const u8 *p = &metadata_resource[offset];
1600         const struct wim_dentry_on_disk *disk_dentry =
1601                         (const struct wim_dentry_on_disk*)p;
1602
1603         BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE);
1604
1605         if ((uintptr_t)p & 7)
1606                 WARNING("WIM dentry is not 8-byte aligned");
1607
1608         dentry_common_init(dentry);
1609
1610         /* Before reading the whole dentry, we need to read just the length.
1611          * This is because a dentry of length 8 (that is, just the length field)
1612          * terminates the list of sibling directory entries. */
1613         if (offset + sizeof(u64) > metadata_resource_len ||
1614             offset + sizeof(u64) < offset)
1615         {
1616                 ERROR("Directory entry starting at %"PRIu64" ends past the "
1617                       "end of the metadata resource (size %"PRIu64")",
1618                       offset, metadata_resource_len);
1619                 return WIMLIB_ERR_INVALID_DENTRY;
1620         }
1621         dentry->length = le64_to_cpu(disk_dentry->length);
1622
1623         /* A zero length field (really a length of 8, since that's how big the
1624          * directory entry is...) indicates that this is the end of directory
1625          * dentry.  We do not read it into memory as an actual dentry, so just
1626          * return successfully in this case. */
1627         if (dentry->length == 8)
1628                 dentry->length = 0;
1629         if (dentry->length == 0)
1630                 return 0;
1631
1632         /* Now that we have the actual length provided in the on-disk structure,
1633          * again make sure it doesn't overflow the metadata resource buffer. */
1634         if (offset + dentry->length > metadata_resource_len ||
1635             offset + dentry->length < offset)
1636         {
1637                 ERROR("Directory entry at offset %"PRIu64" and with size "
1638                       "%"PRIu64" ends past the end of the metadata resource "
1639                       "(size %"PRIu64")",
1640                       offset, dentry->length, metadata_resource_len);
1641                 return WIMLIB_ERR_INVALID_DENTRY;
1642         }
1643
1644         /* Make sure the dentry length is at least as large as the number of
1645          * fixed-length fields */
1646         if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1647                 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1648                       dentry->length);
1649                 return WIMLIB_ERR_INVALID_DENTRY;
1650         }
1651
1652         /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1653         inode = new_timeless_inode();
1654         if (!inode)
1655                 return WIMLIB_ERR_NOMEM;
1656
1657         /* Read more fields; some into the dentry, and some into the inode. */
1658
1659         inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1660         inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1661         dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1662         dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1663         dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1664         inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1665         inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1666         inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1667         copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1668
1669         /* I don't know what's going on here.  It seems like M$ screwed up the
1670          * reparse points, then put the fields in the same place and didn't
1671          * document it.  So we have some fields we read for reparse points, and
1672          * some fields in the same place for non-reparse-point.s */
1673         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1674                 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1675                 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1676                 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1677                 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1678                 /* Leave inode->i_ino at 0.  Note that this means the WIM file
1679                  * cannot archive hard-linked reparse points.  Such a thing
1680                  * doesn't really make sense anyway, although I believe it's
1681                  * theoretically possible to have them on NTFS. */
1682         } else {
1683                 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1684                 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1685         }
1686
1687         inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1688
1689         short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1690         file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1691
1692         if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1693         {
1694                 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1695                 ret = WIMLIB_ERR_INVALID_DENTRY;
1696                 goto out_free_inode;
1697         }
1698
1699         /* We now know the length of the file name and short name.  Make sure
1700          * the length of the dentry is large enough to actually hold them.
1701          *
1702          * The calculated length here is unaligned to allow for the possibility
1703          * that the dentry->length names an unaligned length, although this
1704          * would be unexpected. */
1705         calculated_size = _dentry_correct_length_unaligned(file_name_nbytes,
1706                                                            short_name_nbytes);
1707
1708         if (dentry->length < calculated_size) {
1709                 ERROR("Unexpected end of directory entry! (Expected "
1710                       "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1711                       calculated_size, dentry->length);
1712                 ret = WIMLIB_ERR_INVALID_DENTRY;
1713                 goto out_free_inode;
1714         }
1715
1716         p += sizeof(struct wim_dentry_on_disk);
1717
1718         /* Read the filename if present.  Note: if the filename is empty, there
1719          * is no null terminator following it. */
1720         if (file_name_nbytes) {
1721                 file_name = MALLOC(file_name_nbytes + 2);
1722                 if (!file_name) {
1723                         ERROR("Failed to allocate %d bytes for dentry file name",
1724                               file_name_nbytes + 2);
1725                         ret = WIMLIB_ERR_NOMEM;
1726                         goto out_free_inode;
1727                 }
1728                 memcpy(file_name, p, file_name_nbytes);
1729                 p += file_name_nbytes + 2;
1730                 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
1731                 replace_forbidden_characters(file_name);
1732         } else {
1733                 file_name = NULL;
1734         }
1735
1736
1737         /* Read the short filename if present.  Note: if there is no short
1738          * filename, there is no null terminator following it. */
1739         if (short_name_nbytes) {
1740                 short_name = MALLOC(short_name_nbytes + 2);
1741                 if (!short_name) {
1742                         ERROR("Failed to allocate %d bytes for dentry short name",
1743                               short_name_nbytes + 2);
1744                         ret = WIMLIB_ERR_NOMEM;
1745                         goto out_free_file_name;
1746                 }
1747                 memcpy(short_name, p, short_name_nbytes);
1748                 p += short_name_nbytes + 2;
1749                 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
1750                 replace_forbidden_characters(short_name);
1751         } else {
1752                 short_name = NULL;
1753         }
1754
1755         /* Align the dentry length */
1756         dentry->length = (dentry->length + 7) & ~7;
1757
1758         /*
1759          * Read the alternate data streams, if present.  dentry->num_ads tells
1760          * us how many they are, and they will directly follow the dentry
1761          * on-disk.
1762          *
1763          * Note that each alternate data stream entry begins on an 8-byte
1764          * aligned boundary, and the alternate data stream entries seem to NOT
1765          * be included in the dentry->length field for some reason.
1766          */
1767         if (inode->i_num_ads != 0) {
1768                 ret = WIMLIB_ERR_INVALID_DENTRY;
1769                 if (offset + dentry->length > metadata_resource_len ||
1770                     (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
1771                                             inode,
1772                                             metadata_resource_len - offset - dentry->length)))
1773                 {
1774                         ERROR("Failed to read alternate data stream "
1775                               "entries of WIM dentry \"%"WS"\"", file_name);
1776                         goto out_free_short_name;
1777                 }
1778         }
1779         /* We've read all the data for this dentry.  Set the names and their
1780          * lengths, and we've done. */
1781         dentry->d_inode           = inode;
1782         dentry->file_name         = file_name;
1783         dentry->short_name        = short_name;
1784         dentry->file_name_nbytes  = file_name_nbytes;
1785         dentry->short_name_nbytes = short_name_nbytes;
1786         ret = 0;
1787         goto out;
1788 out_free_short_name:
1789         FREE(short_name);
1790 out_free_file_name:
1791         FREE(file_name);
1792 out_free_inode:
1793         free_inode(inode);
1794 out:
1795         return ret;
1796 }
1797
1798 static const tchar *
1799 dentry_get_file_type_string(const struct wim_dentry *dentry)
1800 {
1801         const struct wim_inode *inode = dentry->d_inode;
1802         if (inode_is_directory(inode))
1803                 return T("directory");
1804         else if (inode_is_symlink(inode))
1805                 return T("symbolic link");
1806         else
1807                 return T("file");
1808 }
1809
1810 /* Reads the children of a dentry, and all their children, ..., etc. from the
1811  * metadata resource and into the dentry tree.
1812  *
1813  * @metadata_resource:  An array that contains the uncompressed metadata
1814  *                      resource for the WIM file.
1815  *
1816  * @metadata_resource_len:  The length of the uncompressed metadata resource, in
1817  *                          bytes.
1818  *
1819  * @dentry:     A pointer to a `struct wim_dentry' that is the root of the directory
1820  *              tree and has already been read from the metadata resource.  It
1821  *              does not need to be the real root because this procedure is
1822  *              called recursively.
1823  *
1824  * Returns zero on success; nonzero on failure.
1825  */
1826 int
1827 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1828                  struct wim_dentry *dentry)
1829 {
1830         u64 cur_offset = dentry->subdir_offset;
1831         struct wim_dentry *child;
1832         struct wim_dentry *duplicate;
1833         struct wim_dentry cur_child;
1834         int ret;
1835
1836         /*
1837          * If @dentry has no child dentries, nothing more needs to be done for
1838          * this branch.  This is the case for regular files, symbolic links, and
1839          * *possibly* empty directories (although an empty directory may also
1840          * have one child dentry that is the special end-of-directory dentry)
1841          */
1842         if (cur_offset == 0)
1843                 return 0;
1844
1845         /* Find and read all the children of @dentry. */
1846         for (;;) {
1847
1848                 /* Read next child of @dentry into @cur_child. */
1849                 ret = read_dentry(metadata_resource, metadata_resource_len,
1850                                   cur_offset, &cur_child);
1851                 if (ret)
1852                         break;
1853
1854                 /* Check for end of directory. */
1855                 if (cur_child.length == 0)
1856                         break;
1857
1858                 /* Not end of directory.  Allocate this child permanently and
1859                  * link it to the parent and previous child. */
1860                 child = memdup(&cur_child, sizeof(struct wim_dentry));
1861                 if (!child) {
1862                         ERROR("Failed to allocate new dentry!");
1863                         ret = WIMLIB_ERR_NOMEM;
1864                         break;
1865                 }
1866
1867                 /* Advance to the offset of the next child.  Note: We need to
1868                  * advance by the TOTAL length of the dentry, not by the length
1869                  * cur_child.length, which although it does take into account
1870                  * the padding, it DOES NOT take into account alternate stream
1871                  * entries. */
1872                 cur_offset += dentry_total_length(child);
1873
1874                 duplicate = dentry_add_child(dentry, child);
1875                 if (duplicate) {
1876                         const tchar *child_type, *duplicate_type;
1877                         child_type = dentry_get_file_type_string(child);
1878                         duplicate_type = dentry_get_file_type_string(duplicate);
1879                         /* On UNIX, duplicates are exact.  On Windows,
1880                          * duplicates may differ by case and we wish to provide
1881                          * a different warning message in this case. */
1882                 #ifdef __WIN32__
1883                         if (dentry_compare_names_case_sensitive(child, duplicate))
1884                         {
1885                                 child->parent = dentry;
1886                                 WARNING("Ignoring %ls \"%ls\", which differs "
1887                                         "only in case from %ls \"%ls\"",
1888                                         child_type,
1889                                         dentry_full_path(child),
1890                                         duplicate_type,
1891                                         dentry_full_path(duplicate));
1892                         }
1893                         else
1894                 #endif
1895                         {
1896                                 WARNING("Ignoring duplicate %"TS" \"%"TS"\" "
1897                                         "(the WIM image already contains a %"TS" "
1898                                         "at that path with the exact same name)",
1899                                         child_type, dentry_full_path(duplicate),
1900                                         duplicate_type);
1901                         }
1902                         free_dentry(child);
1903                 } else {
1904                         inode_add_dentry(child, child->d_inode);
1905                         /* If there are children of this child, call this
1906                          * procedure recursively. */
1907                         if (child->subdir_offset != 0) {
1908                                 if (dentry_is_directory(child)) {
1909                                         ret = read_dentry_tree(metadata_resource,
1910                                                                metadata_resource_len,
1911                                                                child);
1912                                         if (ret)
1913                                                 break;
1914                                 } else {
1915                                         WARNING("Ignoring children of non-directory \"%"TS"\"",
1916                                                 dentry_full_path(child));
1917                                 }
1918                         }
1919
1920                 }
1921         }
1922         return ret;
1923 }
1924
1925 /*
1926  * Writes a WIM dentry to an output buffer.
1927  *
1928  * @dentry:  The dentry structure.
1929  * @p:       The memory location to write the data to.
1930  *
1931  * Returns the pointer to the byte after the last byte we wrote as part of the
1932  * dentry, including any alternate data stream entries.
1933  */
1934 static u8 *
1935 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
1936 {
1937         const struct wim_inode *inode;
1938         struct wim_dentry_on_disk *disk_dentry;
1939         const u8 *orig_p;
1940         const u8 *hash;
1941
1942         wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
1943         orig_p = p;
1944
1945         inode = dentry->d_inode;
1946         disk_dentry = (struct wim_dentry_on_disk*)p;
1947
1948         disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
1949         disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
1950         disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
1951         disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
1952         disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
1953         disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
1954         disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
1955         disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
1956         hash = inode_stream_hash(inode, 0);
1957         copy_hash(disk_dentry->unnamed_stream_hash, hash);
1958         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1959                 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
1960                 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
1961                 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
1962                 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
1963         } else {
1964                 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
1965                 disk_dentry->nonreparse.hard_link_group_id =
1966                         cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
1967         }
1968         disk_dentry->num_alternate_data_streams = cpu_to_le16(inode->i_num_ads);
1969         disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
1970         disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
1971         p += sizeof(struct wim_dentry_on_disk);
1972
1973         if (dentry_has_long_name(dentry))
1974                 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
1975
1976         if (dentry_has_short_name(dentry))
1977                 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
1978
1979         /* Align to 8-byte boundary */
1980         while ((uintptr_t)p & 7)
1981                 *p++ = 0;
1982
1983         /* We calculate the correct length of the dentry ourselves because the
1984          * dentry->length field may been set to an unexpected value from when we
1985          * read the dentry in (for example, there may have been unknown data
1986          * appended to the end of the dentry...).  Furthermore, the dentry may
1987          * have been renamed, thus changing its needed length. */
1988         disk_dentry->length = cpu_to_le64(p - orig_p);
1989
1990         /* Write the alternate data streams entries, if any. */
1991         for (u16 i = 0; i < inode->i_num_ads; i++) {
1992                 const struct wim_ads_entry *ads_entry =
1993                                 &inode->i_ads_entries[i];
1994                 struct wim_ads_entry_on_disk *disk_ads_entry =
1995                                 (struct wim_ads_entry_on_disk*)p;
1996                 orig_p = p;
1997
1998                 disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
1999
2000                 hash = inode_stream_hash(inode, i + 1);
2001                 copy_hash(disk_ads_entry->hash, hash);
2002                 disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
2003                 p += sizeof(struct wim_ads_entry_on_disk);
2004                 if (ads_entry->stream_name_nbytes) {
2005                         p = mempcpy(p, ads_entry->stream_name,
2006                                     ads_entry->stream_name_nbytes + 2);
2007                 }
2008                 /* Align to 8-byte boundary */
2009                 while ((uintptr_t)p & 7)
2010                         *p++ = 0;
2011                 disk_ads_entry->length = cpu_to_le64(p - orig_p);
2012         }
2013         return p;
2014 }
2015
2016 static int
2017 write_dentry_cb(struct wim_dentry *dentry, void *_p)
2018 {
2019         u8 **p = _p;
2020         *p = write_dentry(dentry, *p);
2021         return 0;
2022 }
2023
2024 static u8 *
2025 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
2026
2027 static int
2028 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
2029 {
2030         u8 **p = _p;
2031         *p = write_dentry_tree_recursive(dentry, *p);
2032         return 0;
2033 }
2034
2035 /* Recursive function that writes a dentry tree rooted at @parent, not including
2036  * @parent itself, which has already been written. */
2037 static u8 *
2038 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
2039 {
2040         /* Nothing to do if this dentry has no children. */
2041         if (parent->subdir_offset == 0)
2042                 return p;
2043
2044         /* Write child dentries and end-of-directory entry.
2045          *
2046          * Note: we need to write all of this dentry's children before
2047          * recursively writing the directory trees rooted at each of the child
2048          * dentries, since the on-disk dentries for a dentry's children are
2049          * always located at consecutive positions in the metadata resource! */
2050         for_dentry_child(parent, write_dentry_cb, &p);
2051
2052         /* write end of directory entry */
2053         *(le64*)p = cpu_to_le64(0);
2054         p += 8;
2055
2056         /* Recurse on children. */
2057         for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
2058         return p;
2059 }
2060
2061 /* Writes a directory tree to the metadata resource.
2062  *
2063  * @root:       Root of the dentry tree.
2064  * @p:          Pointer to a buffer with enough space for the dentry tree.
2065  *
2066  * Returns pointer to the byte after the last byte we wrote.
2067  */
2068 u8 *
2069 write_dentry_tree(const struct wim_dentry *root, u8 *p)
2070 {
2071         DEBUG("Writing dentry tree.");
2072         wimlib_assert(dentry_is_root(root));
2073
2074         /* If we're the root dentry, we have no parent that already
2075          * wrote us, so we need to write ourselves. */
2076         p = write_dentry(root, p);
2077
2078         /* Write end of directory entry after the root dentry just to be safe;
2079          * however the root dentry obviously cannot have any siblings. */
2080         *(le64*)p = cpu_to_le64(0);
2081         p += 8;
2082
2083         /* Recursively write the rest of the dentry tree. */
2084         return write_dentry_tree_recursive(root, p);
2085 }