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
12 * Copyright (C) 2012, 2013 Eric Biggers
14 * This file is part of wimlib, a library for working with WIM files.
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
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.
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/.
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"
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.
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];
63 /* Length of the stream name, in bytes. 0 if the stream is unnamed. */
64 le16 stream_name_nbytes;
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.
70 utf16lechar stream_name[];
73 /* WIM directory entry (on-disk format) */
74 struct wim_dentry_on_disk {
82 le64 last_access_time;
84 u8 unnamed_stream_hash[SHA1_HASH_SIZE];
91 } _packed_attribute reparse;
94 le64 hard_link_group_id;
95 } _packed_attribute nonreparse;
97 le16 num_alternate_data_streams;
98 le16 short_name_nbytes;
99 le16 file_name_nbytes;
101 /* Follewed by variable length file name, if file_name_nbytes != 0 */
102 utf16lechar file_name[];
104 /* Followed by variable length short name, if short_name_nbytes != 0 */
105 /*utf16lechar short_name[];*/
108 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
109 * a file name and short name that take the specified numbers of bytes. This
110 * excludes any alternate data stream entries that may follow the dentry. */
112 _dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
114 u64 length = sizeof(struct wim_dentry_on_disk);
115 if (file_name_nbytes)
116 length += file_name_nbytes + 2;
117 if (short_name_nbytes)
118 length += short_name_nbytes + 2;
122 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
123 * the file name length and short name length. Note that dentry->length is
124 * ignored; also, this excludes any alternate data stream entries that may
125 * follow the dentry. */
127 dentry_correct_length_unaligned(const struct wim_dentry *dentry)
129 return _dentry_correct_length_unaligned(dentry->file_name_nbytes,
130 dentry->short_name_nbytes);
133 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
134 * stream name @name that has length @name_nbytes bytes. */
136 ads_entry_has_name(const struct wim_ads_entry *entry,
137 const utf16lechar *name, size_t name_nbytes)
139 return entry->stream_name_nbytes == name_nbytes &&
140 memcmp(entry->stream_name, name, name_nbytes) == 0;
143 /* Duplicates a string of system-dependent encoding into a UTF-16LE string and
144 * returns the string and its length, in bytes, in the pointer arguments. Frees
145 * any existing string at the return location before overwriting it. */
147 get_utf16le_name(const tchar *name, utf16lechar **name_utf16le_ret,
148 u16 *name_utf16le_nbytes_ret)
150 utf16lechar *name_utf16le;
151 size_t name_utf16le_nbytes;
154 name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
155 name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
157 return WIMLIB_ERR_NOMEM;
158 memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
162 ret = tstr_to_utf16le(name, tstrlen(name), &name_utf16le,
163 &name_utf16le_nbytes);
165 if (name_utf16le_nbytes > 0xffff) {
167 ERROR("Multibyte string \"%"TS"\" is too long!", name);
168 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
173 FREE(*name_utf16le_ret);
174 *name_utf16le_ret = name_utf16le;
175 *name_utf16le_nbytes_ret = name_utf16le_nbytes;
180 /* Sets the name of a WIM dentry from a multibyte string. */
182 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name)
185 ret = get_utf16le_name(new_name, &dentry->file_name,
186 &dentry->file_name_nbytes);
188 /* Clear the short name and recalculate the dentry length */
189 if (dentry_has_short_name(dentry)) {
190 FREE(dentry->short_name);
191 dentry->short_name = NULL;
192 dentry->short_name_nbytes = 0;
198 /* Returns the total length of a WIM alternate data stream entry on-disk,
199 * including the stream name, the null terminator, AND the padding after the
200 * entry to align the next ADS entry or dentry on an 8-byte boundary. */
202 ads_entry_total_length(const struct wim_ads_entry *entry)
204 u64 len = sizeof(struct wim_ads_entry_on_disk);
205 if (entry->stream_name_nbytes)
206 len += entry->stream_name_nbytes + 2;
207 return (len + 7) & ~7;
212 _dentry_total_length(const struct wim_dentry *dentry, u64 length)
214 const struct wim_inode *inode = dentry->d_inode;
215 for (u16 i = 0; i < inode->i_num_ads; i++)
216 length += ads_entry_total_length(&inode->i_ads_entries[i]);
217 return (length + 7) & ~7;
220 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
221 * all alternate data streams. */
223 dentry_correct_total_length(const struct wim_dentry *dentry)
225 return _dentry_total_length(dentry,
226 dentry_correct_length_unaligned(dentry));
229 /* Like dentry_correct_total_length(), but use the existing dentry->length field
230 * instead of calculating its "correct" value. */
232 dentry_total_length(const struct wim_dentry *dentry)
234 return _dentry_total_length(dentry, dentry->length);
238 for_dentry_in_rbtree(struct rb_node *root,
239 int (*visitor)(struct wim_dentry *, void *),
243 struct rb_node *node = root;
247 list_add(&rbnode_dentry(node)->tmp_list, &stack);
248 node = node->rb_left;
250 struct list_head *next;
251 struct wim_dentry *dentry;
256 dentry = container_of(next, struct wim_dentry, tmp_list);
258 ret = visitor(dentry, arg);
261 node = dentry->rb_node.rb_right;
267 for_dentry_tree_in_rbtree_depth(struct rb_node *node,
268 int (*visitor)(struct wim_dentry*, void*),
273 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
277 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
281 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
289 for_dentry_tree_in_rbtree(struct rb_node *node,
290 int (*visitor)(struct wim_dentry*, void*),
295 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
298 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
301 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
308 /* Calls a function on all directory entries in a WIM dentry tree. Logically,
309 * this is a pre-order traversal (the function is called on a parent dentry
310 * before its children), but sibling dentries will be visited in order as well.
313 for_dentry_in_tree(struct wim_dentry *root,
314 int (*visitor)(struct wim_dentry*, void*), void *arg)
320 ret = (*visitor)(root, arg);
323 return for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node,
328 /* Like for_dentry_in_tree(), but the visitor function is always called on a
329 * dentry's children before on itself. */
331 for_dentry_in_tree_depth(struct wim_dentry *root,
332 int (*visitor)(struct wim_dentry*, void*), void *arg)
338 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
342 return (*visitor)(root, arg);
345 /* Calculate the full path of @dentry. The full path of its parent must have
346 * already been calculated, or it must be the root dentry. */
348 calculate_dentry_full_path(struct wim_dentry *dentry)
351 u32 full_path_nbytes;
354 if (dentry->_full_path)
357 if (dentry_is_root(dentry)) {
358 full_path = TSTRDUP(T("/"));
360 return WIMLIB_ERR_NOMEM;
361 full_path_nbytes = 1 * sizeof(tchar);
363 struct wim_dentry *parent;
364 tchar *parent_full_path;
365 u32 parent_full_path_nbytes;
366 size_t filename_nbytes;
368 parent = dentry->parent;
369 if (dentry_is_root(parent)) {
370 parent_full_path = T("");
371 parent_full_path_nbytes = 0;
373 if (!parent->_full_path) {
374 ret = calculate_dentry_full_path(parent);
378 parent_full_path = parent->_full_path;
379 parent_full_path_nbytes = parent->full_path_nbytes;
382 /* Append this dentry's name as a tchar string to the full path
383 * of the parent followed by the path separator */
385 filename_nbytes = dentry->file_name_nbytes;
388 int ret = utf16le_to_tstr_nbytes(dentry->file_name,
389 dentry->file_name_nbytes,
396 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
398 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
400 return WIMLIB_ERR_NOMEM;
401 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
402 full_path[parent_full_path_nbytes / sizeof(tchar)] = T('/');
404 memcpy(&full_path[parent_full_path_nbytes / sizeof(tchar) + 1],
406 filename_nbytes + sizeof(tchar));
408 utf16le_to_tstr_buf(dentry->file_name,
409 dentry->file_name_nbytes,
410 &full_path[parent_full_path_nbytes /
414 dentry->_full_path = full_path;
415 dentry->full_path_nbytes= full_path_nbytes;
420 do_calculate_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
422 return calculate_dentry_full_path(dentry);
426 calculate_dentry_tree_full_paths(struct wim_dentry *root)
428 return for_dentry_in_tree(root, do_calculate_dentry_full_path, NULL);
432 dentry_full_path(struct wim_dentry *dentry)
434 calculate_dentry_full_path(dentry);
435 return dentry->_full_path;
439 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
441 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
446 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
448 calculate_subdir_offsets(dentry, subdir_offset_p);
453 * Recursively calculates the subdir offsets for a directory tree.
455 * @dentry: The root of the directory tree.
456 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
457 * offset for @dentry.
460 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
462 struct rb_node *node;
464 dentry->subdir_offset = *subdir_offset_p;
465 node = dentry->d_inode->i_children.rb_node;
467 /* Advance the subdir offset by the amount of space the children
468 * of this dentry take up. */
469 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
471 /* End-of-directory dentry on disk. */
472 *subdir_offset_p += 8;
474 /* Recursively call calculate_subdir_offsets() on all the
476 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
478 /* On disk, childless directories have a valid subdir_offset
479 * that points to an 8-byte end-of-directory dentry. Regular
480 * files or reparse points have a subdir_offset of 0. */
481 if (dentry_is_directory(dentry))
482 *subdir_offset_p += 8;
484 dentry->subdir_offset = 0;
489 compare_utf16le_names(const utf16lechar *name1, size_t nbytes1,
490 const utf16lechar *name2, size_t nbytes2)
492 int result = memcmp(name1, name2, min(nbytes1, nbytes2));
496 return (int)nbytes1 - (int)nbytes2;
500 dentry_compare_names(const struct wim_dentry *d1, const struct wim_dentry *d2)
502 return compare_utf16le_names(d1->file_name, d1->file_name_nbytes,
503 d2->file_name, d2->file_name_nbytes);
508 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
509 const utf16lechar *name,
512 struct rb_node *node = dentry->d_inode->i_children.rb_node;
513 struct wim_dentry *child;
515 child = rbnode_dentry(node);
516 int result = compare_utf16le_names(name, name_nbytes,
518 child->file_name_nbytes);
520 node = node->rb_left;
522 node = node->rb_right;
529 /* Returns the child of @dentry that has the file name @name. Returns NULL if
530 * no child has the name. */
532 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
535 return get_dentry_child_with_utf16le_name(dentry, name,
536 tstrlen(name) * sizeof(tchar));
538 utf16lechar *utf16le_name;
539 size_t utf16le_name_nbytes;
541 struct wim_dentry *child;
543 ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
544 &utf16le_name, &utf16le_name_nbytes);
548 child = get_dentry_child_with_utf16le_name(dentry,
550 utf16le_name_nbytes);
557 static struct wim_dentry *
558 get_dentry_utf16le(WIMStruct *wim, const utf16lechar *path)
560 struct wim_dentry *cur_dentry, *parent_dentry;
561 const utf16lechar *p, *pp;
563 cur_dentry = parent_dentry = wim_root_dentry(wim);
570 while (*p == cpu_to_le16('/'))
572 if (*p == cpu_to_le16('\0'))
575 while (*pp != cpu_to_le16('/') && *pp != cpu_to_le16('\0'))
578 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
579 (void*)pp - (void*)p);
580 if (cur_dentry == NULL)
583 parent_dentry = cur_dentry;
585 if (cur_dentry == NULL) {
586 if (dentry_is_directory(parent_dentry))
594 /* Returns the dentry corresponding to the @path, or NULL if there is no such
597 get_dentry(WIMStruct *wim, const tchar *path)
600 return get_dentry_utf16le(wim, path);
602 utf16lechar *path_utf16le;
603 size_t path_utf16le_nbytes;
605 struct wim_dentry *dentry;
607 ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
608 &path_utf16le, &path_utf16le_nbytes);
611 dentry = get_dentry_utf16le(wim, path_utf16le);
618 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
620 struct wim_dentry *dentry;
621 dentry = get_dentry(wim, path);
623 return dentry->d_inode;
628 /* Takes in a path of length @len in @buf, and transforms it into a string for
629 * the path of its parent directory. */
631 to_parent_name(tchar *buf, size_t len)
633 ssize_t i = (ssize_t)len - 1;
634 while (i >= 0 && buf[i] == T('/'))
636 while (i >= 0 && buf[i] != T('/'))
638 while (i >= 0 && buf[i] == T('/'))
640 buf[i + 1] = T('\0');
643 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
644 * if the dentry is not found. */
646 get_parent_dentry(WIMStruct *wim, const tchar *path)
648 size_t path_len = tstrlen(path);
649 tchar buf[path_len + 1];
651 tmemcpy(buf, path, path_len + 1);
652 to_parent_name(buf, path_len);
653 return get_dentry(wim, buf);
656 /* Prints the full path of a dentry. */
658 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
660 int ret = calculate_dentry_full_path(dentry);
663 tprintf(T("%"TS"\n"), dentry->_full_path);
667 /* We want to be able to show the names of the file attribute flags that are
669 struct file_attr_flag {
673 struct file_attr_flag file_attr_flags[] = {
674 {FILE_ATTRIBUTE_READONLY, T("READONLY")},
675 {FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
676 {FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
677 {FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
678 {FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
679 {FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
680 {FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
681 {FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
682 {FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
683 {FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
684 {FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
685 {FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
686 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
687 {FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
688 {FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
691 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
692 * available. If the dentry is unresolved and the lookup table is NULL, the
693 * lookup table entries will not be printed. Otherwise, they will be. */
695 print_dentry(struct wim_dentry *dentry, void *lookup_table)
698 struct wim_lookup_table_entry *lte;
699 const struct wim_inode *inode = dentry->d_inode;
702 tprintf(T("[DENTRY]\n"));
703 tprintf(T("Length = %"PRIu64"\n"), dentry->length);
704 tprintf(T("Attributes = 0x%x\n"), inode->i_attributes);
705 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
706 if (file_attr_flags[i].flag & inode->i_attributes)
707 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
708 file_attr_flags[i].name);
709 tprintf(T("Security ID = %d\n"), inode->i_security_id);
710 tprintf(T("Subdir offset = %"PRIu64"\n"), dentry->subdir_offset);
712 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
713 tprintf(T("Creation Time = %"TS"\n"), buf);
715 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
716 tprintf(T("Last Access Time = %"TS"\n"), buf);
718 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
719 tprintf(T("Last Write Time = %"TS"\n"), buf);
721 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
722 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), inode->i_reparse_tag);
723 tprintf(T("Reparse Point Flags = 0x%"PRIx16"\n"),
724 inode->i_not_rpfixed);
725 tprintf(T("Reparse Point Unknown 2 = 0x%"PRIx32"\n"),
726 inode->i_rp_unknown_2);
728 tprintf(T("Reparse Point Unknown 1 = 0x%"PRIx32"\n"),
729 inode->i_rp_unknown_1);
730 tprintf(T("Hard Link Group = 0x%"PRIx64"\n"), inode->i_ino);
731 tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
732 tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
733 if (dentry_has_long_name(dentry))
734 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
735 if (dentry_has_short_name(dentry))
736 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
737 if (dentry->_full_path)
738 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
740 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
742 print_lookup_table_entry(lte, stdout);
744 hash = inode_stream_hash(inode, 0);
746 tprintf(T("Hash = 0x"));
747 print_hash(hash, stdout);
752 for (u16 i = 0; i < inode->i_num_ads; i++) {
753 tprintf(T("[Alternate Stream Entry %u]\n"), i);
754 wimlib_printf(T("Name = \"%"WS"\"\n"),
755 inode->i_ads_entries[i].stream_name);
756 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
757 inode->i_ads_entries[i].stream_name_nbytes);
758 hash = inode_stream_hash(inode, i + 1);
760 tprintf(T("Hash = 0x"));
761 print_hash(hash, stdout);
764 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
770 /* Initializations done on every `struct wim_dentry'. */
772 dentry_common_init(struct wim_dentry *dentry)
774 memset(dentry, 0, sizeof(struct wim_dentry));
778 new_timeless_inode(void)
780 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
782 inode->i_security_id = -1;
784 inode->i_next_stream_id = 1;
785 inode->i_not_rpfixed = 1;
786 INIT_LIST_HEAD(&inode->i_list);
788 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
789 ERROR_WITH_ERRNO("Error initializing mutex");
794 INIT_LIST_HEAD(&inode->i_dentry);
799 static struct wim_inode *
802 struct wim_inode *inode = new_timeless_inode();
804 u64 now = get_wim_timestamp();
805 inode->i_creation_time = now;
806 inode->i_last_access_time = now;
807 inode->i_last_write_time = now;
812 /* Creates an unlinked directory entry. */
814 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
816 struct wim_dentry *dentry;
819 dentry = MALLOC(sizeof(struct wim_dentry));
821 return WIMLIB_ERR_NOMEM;
823 dentry_common_init(dentry);
824 ret = set_dentry_name(dentry, name);
826 dentry->parent = dentry;
827 *dentry_ret = dentry;
830 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
838 _new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
841 struct wim_dentry *dentry;
844 ret = new_dentry(name, &dentry);
849 dentry->d_inode = new_timeless_inode();
851 dentry->d_inode = new_inode();
852 if (!dentry->d_inode) {
854 return WIMLIB_ERR_NOMEM;
857 inode_add_dentry(dentry, dentry->d_inode);
858 *dentry_ret = dentry;
863 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
865 return _new_dentry_with_inode(name, dentry_ret, true);
869 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
871 return _new_dentry_with_inode(name, dentry_ret, false);
875 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
878 struct wim_dentry *dentry;
880 DEBUG("Creating filler directory \"%"TS"\"", name);
881 ret = new_dentry_with_inode(name, &dentry);
884 /* Leave the inode number as 0; this is allowed for non
885 * hard-linked files. */
886 dentry->d_inode->i_resolved = 1;
887 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
888 *dentry_ret = dentry;
893 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
894 size_t name_nbytes, bool is_utf16le)
897 memset(ads_entry, 0, sizeof(*ads_entry));
900 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
902 return WIMLIB_ERR_NOMEM;
903 memcpy(p, name, name_nbytes);
904 p[name_nbytes / 2] = cpu_to_le16(0);
905 ads_entry->stream_name = p;
906 ads_entry->stream_name_nbytes = name_nbytes;
908 if (name && *(const tchar*)name != T('\0')) {
909 ret = get_utf16le_name(name, &ads_entry->stream_name,
910 &ads_entry->stream_name_nbytes);
917 destroy_ads_entry(struct wim_ads_entry *ads_entry)
919 FREE(ads_entry->stream_name);
922 /* Frees an inode. */
924 free_inode(struct wim_inode *inode)
927 if (inode->i_ads_entries) {
928 for (u16 i = 0; i < inode->i_num_ads; i++)
929 destroy_ads_entry(&inode->i_ads_entries[i]);
930 FREE(inode->i_ads_entries);
933 wimlib_assert(inode->i_num_opened_fds == 0);
935 pthread_mutex_destroy(&inode->i_mutex);
937 /* HACK: This may instead delete the inode from i_list, but the
938 * hlist_del() behaves the same as list_del(). */
939 hlist_del(&inode->i_hlist);
940 FREE(inode->i_extracted_file);
945 /* Decrements link count on an inode and frees it if the link count reaches 0.
948 put_inode(struct wim_inode *inode)
950 wimlib_assert(inode->i_nlink != 0);
951 if (--inode->i_nlink == 0) {
953 if (inode->i_num_opened_fds == 0)
961 /* Frees a WIM dentry.
963 * The corresponding inode (if any) is freed only if its link count is
967 free_dentry(struct wim_dentry *dentry)
970 FREE(dentry->file_name);
971 FREE(dentry->short_name);
972 FREE(dentry->_full_path);
974 put_inode(dentry->d_inode);
979 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
980 * to free a directory tree. */
982 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
984 struct wim_lookup_table *lookup_table = _lookup_table;
987 struct wim_inode *inode = dentry->d_inode;
988 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
989 struct wim_lookup_table_entry *lte;
991 lte = inode_stream_lte(inode, i, lookup_table);
993 lte_decrement_refcnt(lte, lookup_table);
1001 * Unlinks and frees a dentry tree.
1003 * @root: The root of the tree.
1004 * @lookup_table: The lookup table for dentries. If non-NULL, the
1005 * reference counts in the lookup table for the lookup
1006 * table entries corresponding to the dentries will be
1010 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1012 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1016 * Links a dentry into the directory tree.
1018 * @parent: The dentry that will be the parent of @child.
1019 * @child: The dentry to link.
1021 * Returns non-NULL if a duplicate dentry was detected.
1024 dentry_add_child(struct wim_dentry * restrict parent,
1025 struct wim_dentry * restrict child)
1027 wimlib_assert(dentry_is_directory(parent));
1028 wimlib_assert(parent != child);
1030 struct rb_root *root = &parent->d_inode->i_children;
1031 struct rb_node **new = &(root->rb_node);
1032 struct rb_node *rb_parent = NULL;
1035 struct wim_dentry *this = rbnode_dentry(*new);
1036 int result = dentry_compare_names(child, this);
1041 new = &((*new)->rb_left);
1042 else if (result > 0)
1043 new = &((*new)->rb_right);
1047 child->parent = parent;
1048 rb_link_node(&child->rb_node, rb_parent, new);
1049 rb_insert_color(&child->rb_node, root);
1053 /* Unlink a WIM dentry from the directory entry tree. */
1055 unlink_dentry(struct wim_dentry *dentry)
1057 if (!dentry_is_root(dentry))
1058 rb_erase(&dentry->rb_node, &dentry->parent->d_inode->i_children);
1062 * Returns the alternate data stream entry belonging to @inode that has the
1063 * stream name @stream_name.
1065 struct wim_ads_entry *
1066 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1069 if (inode->i_num_ads == 0) {
1072 size_t stream_name_utf16le_nbytes;
1074 struct wim_ads_entry *result;
1076 #if TCHAR_IS_UTF16LE
1077 const utf16lechar *stream_name_utf16le;
1079 stream_name_utf16le = stream_name;
1080 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1082 utf16lechar *stream_name_utf16le;
1085 int ret = tstr_to_utf16le(stream_name,
1086 tstrlen(stream_name) *
1088 &stream_name_utf16le,
1089 &stream_name_utf16le_nbytes);
1097 if (ads_entry_has_name(&inode->i_ads_entries[i],
1098 stream_name_utf16le,
1099 stream_name_utf16le_nbytes))
1103 result = &inode->i_ads_entries[i];
1106 } while (++i != inode->i_num_ads);
1107 #if !TCHAR_IS_UTF16LE
1108 FREE(stream_name_utf16le);
1114 static struct wim_ads_entry *
1115 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1116 size_t stream_name_nbytes, bool is_utf16le)
1119 struct wim_ads_entry *ads_entries;
1120 struct wim_ads_entry *new_entry;
1122 if (inode->i_num_ads >= 0xfffe) {
1123 ERROR("Too many alternate data streams in one inode!");
1126 num_ads = inode->i_num_ads + 1;
1127 ads_entries = REALLOC(inode->i_ads_entries,
1128 num_ads * sizeof(inode->i_ads_entries[0]));
1130 ERROR("Failed to allocate memory for new alternate data stream");
1133 inode->i_ads_entries = ads_entries;
1135 new_entry = &inode->i_ads_entries[num_ads - 1];
1136 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1138 new_entry->stream_id = inode->i_next_stream_id++;
1139 inode->i_num_ads = num_ads;
1143 struct wim_ads_entry *
1144 inode_add_ads_utf16le(struct wim_inode *inode,
1145 const utf16lechar *stream_name,
1146 size_t stream_name_nbytes)
1148 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1149 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1153 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1154 * NULL if memory could not be allocated.
1156 struct wim_ads_entry *
1157 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1159 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1160 return do_inode_add_ads(inode, stream_name,
1161 tstrlen(stream_name) * sizeof(tchar),
1165 static struct wim_lookup_table_entry *
1166 add_stream_from_data_buffer(const void *buffer, size_t size,
1167 struct wim_lookup_table *lookup_table)
1169 u8 hash[SHA1_HASH_SIZE];
1170 struct wim_lookup_table_entry *lte, *existing_lte;
1172 sha1_buffer(buffer, size, hash);
1173 existing_lte = __lookup_resource(lookup_table, hash);
1175 wimlib_assert(wim_resource_size(existing_lte) == size);
1180 lte = new_lookup_table_entry();
1183 buffer_copy = memdup(buffer, size);
1185 free_lookup_table_entry(lte);
1188 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1189 lte->attached_buffer = buffer_copy;
1190 lte->resource_entry.original_size = size;
1191 copy_hash(lte->hash, hash);
1192 lookup_table_insert(lookup_table, lte);
1198 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1199 const void *value, size_t size,
1200 struct wim_lookup_table *lookup_table)
1202 struct wim_ads_entry *new_ads_entry;
1204 wimlib_assert(inode->i_resolved);
1206 new_ads_entry = inode_add_ads(inode, name);
1208 return WIMLIB_ERR_NOMEM;
1210 new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1212 if (!new_ads_entry->lte) {
1213 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1215 return WIMLIB_ERR_NOMEM;
1220 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
1221 * stream contents. */
1223 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
1224 struct wim_lookup_table *lookup_table)
1226 inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
1228 return WIMLIB_ERR_NOMEM;
1229 inode->i_resolved = 1;
1233 /* Remove an alternate data stream from a WIM inode */
1235 inode_remove_ads(struct wim_inode *inode, u16 idx,
1236 struct wim_lookup_table *lookup_table)
1238 struct wim_ads_entry *ads_entry;
1239 struct wim_lookup_table_entry *lte;
1241 wimlib_assert(idx < inode->i_num_ads);
1242 wimlib_assert(inode->i_resolved);
1244 ads_entry = &inode->i_ads_entries[idx];
1246 DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1248 lte = ads_entry->lte;
1250 lte_decrement_refcnt(lte, lookup_table);
1252 destroy_ads_entry(ads_entry);
1254 memmove(&inode->i_ads_entries[idx],
1255 &inode->i_ads_entries[idx + 1],
1256 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1262 inode_get_unix_data(const struct wim_inode *inode,
1263 struct wimlib_unix_data *unix_data,
1264 u16 *stream_idx_ret)
1266 const struct wim_ads_entry *ads_entry;
1267 const struct wim_lookup_table_entry *lte;
1271 wimlib_assert(inode->i_resolved);
1273 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1274 WIMLIB_UNIX_DATA_TAG, NULL);
1276 return NO_UNIX_DATA;
1279 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1281 lte = ads_entry->lte;
1283 return NO_UNIX_DATA;
1285 size = wim_resource_size(lte);
1286 if (size != sizeof(struct wimlib_unix_data))
1287 return BAD_UNIX_DATA;
1289 ret = read_full_resource_into_buf(lte, unix_data);
1293 if (unix_data->version != 0)
1294 return BAD_UNIX_DATA;
1299 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1300 struct wim_lookup_table *lookup_table, int which)
1302 struct wimlib_unix_data unix_data;
1304 bool have_good_unix_data = false;
1305 bool have_unix_data = false;
1308 if (!(which & UNIX_DATA_CREATE)) {
1309 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1310 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1311 have_unix_data = true;
1313 have_good_unix_data = true;
1315 unix_data.version = 0;
1316 if (which & UNIX_DATA_UID || !have_good_unix_data)
1317 unix_data.uid = uid;
1318 if (which & UNIX_DATA_GID || !have_good_unix_data)
1319 unix_data.gid = gid;
1320 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1321 unix_data.mode = mode;
1322 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1324 sizeof(struct wimlib_unix_data),
1326 if (ret == 0 && have_unix_data)
1327 inode_remove_ads(inode, stream_idx, lookup_table);
1330 #endif /* !__WIN32__ */
1332 /* Replace weird characters in filenames and alternate data stream names.
1334 * In particular we do not want the path separator to appear in any names, as
1335 * that would make it possible for a "malicious" WIM to extract itself to any
1336 * location it wanted to. */
1338 replace_forbidden_characters(utf16lechar *name)
1342 for (p = name; *p; p++) {
1344 if (wcschr(L"<>:\"/\\|?*", (wchar_t)*p))
1346 if (*p == cpu_to_le16('/'))
1350 WARNING("File, directory, or stream name \"%"WS"\"\n"
1351 " contains forbidden characters; "
1352 "substituting replacement characters.",
1357 *p = cpu_to_le16(0xfffd);
1359 *p = cpu_to_le16('?');
1366 * Reads the alternate data stream entries of a WIM dentry.
1368 * @p: Pointer to buffer that starts with the first alternate stream entry.
1370 * @inode: Inode to load the alternate data streams into.
1371 * @inode->i_num_ads must have been set to the number of
1372 * alternate data streams that are expected.
1374 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1378 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1379 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1380 * failure, @inode is not modified.
1383 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1384 size_t nbytes_remaining)
1387 struct wim_ads_entry *ads_entries;
1390 /* Allocate an array for our in-memory representation of the alternate
1391 * data stream entries. */
1392 num_ads = inode->i_num_ads;
1393 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1397 /* Read the entries into our newly allocated buffer. */
1398 for (u16 i = 0; i < num_ads; i++) {
1400 struct wim_ads_entry *cur_entry;
1401 const struct wim_ads_entry_on_disk *disk_entry =
1402 (const struct wim_ads_entry_on_disk*)p;
1404 cur_entry = &ads_entries[i];
1405 ads_entries[i].stream_id = i + 1;
1407 /* Do we have at least the size of the fixed-length data we know
1409 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1412 /* Read the length field */
1413 length = le64_to_cpu(disk_entry->length);
1415 /* Make sure the length field is neither so small it doesn't
1416 * include all the fixed-length data nor so large it overflows
1417 * the metadata resource buffer. */
1418 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1419 length > nbytes_remaining)
1422 /* Read the rest of the fixed-length data. */
1424 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1425 copy_hash(cur_entry->hash, disk_entry->hash);
1426 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1428 /* If stream_name_nbytes != 0, this is a named stream.
1429 * Otherwise this is an unnamed stream, or in some cases (bugs
1430 * in Microsoft's software I guess) a meaningless entry
1431 * distinguished from the real unnamed stream entry, if any, by
1432 * the fact that the real unnamed stream entry has a nonzero
1434 if (cur_entry->stream_name_nbytes) {
1435 /* The name is encoded in UTF16-LE, which uses 2-byte
1436 * coding units, so the length of the name had better be
1437 * an even number of bytes... */
1438 if (cur_entry->stream_name_nbytes & 1)
1441 /* Add the length of the stream name to get the length
1442 * we actually need to read. Make sure this isn't more
1443 * than the specified length of the entry. */
1444 if (sizeof(struct wim_ads_entry_on_disk) +
1445 cur_entry->stream_name_nbytes > length)
1448 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1449 if (!cur_entry->stream_name)
1452 memcpy(cur_entry->stream_name,
1453 disk_entry->stream_name,
1454 cur_entry->stream_name_nbytes);
1455 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1456 replace_forbidden_characters(cur_entry->stream_name);
1459 /* It's expected that the size of every ADS entry is a multiple
1460 * of 8. However, to be safe, I'm allowing the possibility of
1461 * an ADS entry at the very end of the metadata resource ending
1462 * un-aligned. So although we still need to increment the input
1463 * pointer by @length to reach the next ADS entry, it's possible
1464 * that less than @length is actually remaining in the metadata
1465 * resource. We should set the remaining bytes to 0 if this
1467 length = (length + 7) & ~(u64)7;
1469 if (nbytes_remaining < length)
1470 nbytes_remaining = 0;
1472 nbytes_remaining -= length;
1474 inode->i_ads_entries = ads_entries;
1475 inode->i_next_stream_id = inode->i_num_ads + 1;
1479 ret = WIMLIB_ERR_NOMEM;
1480 goto out_free_ads_entries;
1482 ERROR("An alternate data stream entry is invalid");
1483 ret = WIMLIB_ERR_INVALID_DENTRY;
1484 out_free_ads_entries:
1486 for (u16 i = 0; i < num_ads; i++)
1487 destroy_ads_entry(&ads_entries[i]);
1495 * Reads a WIM directory entry, including all alternate data stream entries that
1496 * follow it, from the WIM image's metadata resource.
1498 * @metadata_resource:
1499 * Pointer to the metadata resource buffer.
1501 * @metadata_resource_len:
1502 * Length of the metadata resource buffer, in bytes.
1504 * @offset: Offset of the dentry within the metadata resource.
1506 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1508 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1509 * been modified, but it will not be left with pointers to any allocated
1510 * buffers. On success, the dentry->length field must be examined. If zero,
1511 * this was a special "end of directory" dentry and not a real dentry. If
1512 * nonzero, this was a real dentry.
1514 * Possible errors include:
1516 * WIMLIB_ERR_INVALID_DENTRY
1519 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1520 u64 offset, struct wim_dentry * restrict dentry)
1523 u64 calculated_size;
1524 utf16lechar *file_name;
1525 utf16lechar *short_name;
1526 u16 short_name_nbytes;
1527 u16 file_name_nbytes;
1529 struct wim_inode *inode;
1530 const u8 *p = &metadata_resource[offset];
1531 const struct wim_dentry_on_disk *disk_dentry =
1532 (const struct wim_dentry_on_disk*)p;
1534 if ((uintptr_t)p & 7)
1535 WARNING("WIM dentry is not 8-byte aligned");
1537 dentry_common_init(dentry);
1539 /* Before reading the whole dentry, we need to read just the length.
1540 * This is because a dentry of length 8 (that is, just the length field)
1541 * terminates the list of sibling directory entries. */
1542 if (offset + sizeof(u64) > metadata_resource_len ||
1543 offset + sizeof(u64) < offset)
1545 ERROR("Directory entry starting at %"PRIu64" ends past the "
1546 "end of the metadata resource (size %"PRIu64")",
1547 offset, metadata_resource_len);
1548 return WIMLIB_ERR_INVALID_DENTRY;
1550 dentry->length = le64_to_cpu(disk_dentry->length);
1552 /* A zero length field (really a length of 8, since that's how big the
1553 * directory entry is...) indicates that this is the end of directory
1554 * dentry. We do not read it into memory as an actual dentry, so just
1555 * return successfully in this case. */
1556 if (dentry->length == 8)
1558 if (dentry->length == 0)
1561 /* Now that we have the actual length provided in the on-disk structure,
1562 * again make sure it doesn't overflow the metadata resource buffer. */
1563 if (offset + dentry->length > metadata_resource_len ||
1564 offset + dentry->length < offset)
1566 ERROR("Directory entry at offset %"PRIu64" and with size "
1567 "%"PRIu64" ends past the end of the metadata resource "
1569 offset, dentry->length, metadata_resource_len);
1570 return WIMLIB_ERR_INVALID_DENTRY;
1573 /* Make sure the dentry length is at least as large as the number of
1574 * fixed-length fields */
1575 if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1576 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1578 return WIMLIB_ERR_INVALID_DENTRY;
1581 /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1582 inode = new_timeless_inode();
1584 return WIMLIB_ERR_NOMEM;
1586 /* Read more fields; some into the dentry, and some into the inode. */
1588 inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1589 inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1590 dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1591 dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1592 dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1593 inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1594 inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1595 inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1596 copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1598 /* I don't know what's going on here. It seems like M$ screwed up the
1599 * reparse points, then put the fields in the same place and didn't
1600 * document it. So we have some fields we read for reparse points, and
1601 * some fields in the same place for non-reparse-point.s */
1602 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1603 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1604 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1605 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1606 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1607 /* Leave inode->i_ino at 0. Note that this means the WIM file
1608 * cannot archive hard-linked reparse points. Such a thing
1609 * doesn't really make sense anyway, although I believe it's
1610 * theoretically possible to have them on NTFS. */
1612 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1613 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1616 inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1618 short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1619 file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1621 if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1623 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1624 ret = WIMLIB_ERR_INVALID_DENTRY;
1625 goto out_free_inode;
1628 /* We now know the length of the file name and short name. Make sure
1629 * the length of the dentry is large enough to actually hold them.
1631 * The calculated length here is unaligned to allow for the possibility
1632 * that the dentry->length names an unaligned length, although this
1633 * would be unexpected. */
1634 calculated_size = _dentry_correct_length_unaligned(file_name_nbytes,
1637 if (dentry->length < calculated_size) {
1638 ERROR("Unexpected end of directory entry! (Expected "
1639 "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1640 calculated_size, dentry->length);
1641 ret = WIMLIB_ERR_INVALID_DENTRY;
1642 goto out_free_inode;
1645 p += sizeof(struct wim_dentry_on_disk);
1647 /* Read the filename if present. Note: if the filename is empty, there
1648 * is no null terminator following it. */
1649 if (file_name_nbytes) {
1650 file_name = MALLOC(file_name_nbytes + 2);
1652 ERROR("Failed to allocate %d bytes for dentry file name",
1653 file_name_nbytes + 2);
1654 ret = WIMLIB_ERR_NOMEM;
1655 goto out_free_inode;
1657 memcpy(file_name, p, file_name_nbytes);
1658 p += file_name_nbytes + 2;
1659 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
1660 replace_forbidden_characters(file_name);
1666 /* Read the short filename if present. Note: if there is no short
1667 * filename, there is no null terminator following it. */
1668 if (short_name_nbytes) {
1669 short_name = MALLOC(short_name_nbytes + 2);
1671 ERROR("Failed to allocate %d bytes for dentry short name",
1672 short_name_nbytes + 2);
1673 ret = WIMLIB_ERR_NOMEM;
1674 goto out_free_file_name;
1676 memcpy(short_name, p, short_name_nbytes);
1677 p += short_name_nbytes + 2;
1678 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
1679 replace_forbidden_characters(short_name);
1684 /* Align the dentry length */
1685 dentry->length = (dentry->length + 7) & ~7;
1688 * Read the alternate data streams, if present. dentry->num_ads tells
1689 * us how many they are, and they will directly follow the dentry
1692 * Note that each alternate data stream entry begins on an 8-byte
1693 * aligned boundary, and the alternate data stream entries seem to NOT
1694 * be included in the dentry->length field for some reason.
1696 if (inode->i_num_ads != 0) {
1697 ret = WIMLIB_ERR_INVALID_DENTRY;
1698 if (offset + dentry->length > metadata_resource_len ||
1699 (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
1701 metadata_resource_len - offset - dentry->length)))
1703 ERROR("Failed to read alternate data stream "
1704 "entries of WIM dentry \"%"WS"\"", file_name);
1705 goto out_free_short_name;
1708 /* We've read all the data for this dentry. Set the names and their
1709 * lengths, and we've done. */
1710 dentry->d_inode = inode;
1711 dentry->file_name = file_name;
1712 dentry->short_name = short_name;
1713 dentry->file_name_nbytes = file_name_nbytes;
1714 dentry->short_name_nbytes = short_name_nbytes;
1717 out_free_short_name:
1727 /* Reads the children of a dentry, and all their children, ..., etc. from the
1728 * metadata resource and into the dentry tree.
1730 * @metadata_resource: An array that contains the uncompressed metadata
1731 * resource for the WIM file.
1733 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1736 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
1737 * tree and has already been read from the metadata resource. It
1738 * does not need to be the real root because this procedure is
1739 * called recursively.
1741 * Returns zero on success; nonzero on failure.
1744 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1745 struct wim_dentry *dentry)
1747 u64 cur_offset = dentry->subdir_offset;
1748 struct wim_dentry *child;
1749 struct wim_dentry cur_child;
1753 * If @dentry has no child dentries, nothing more needs to be done for
1754 * this branch. This is the case for regular files, symbolic links, and
1755 * *possibly* empty directories (although an empty directory may also
1756 * have one child dentry that is the special end-of-directory dentry)
1758 if (cur_offset == 0)
1761 /* Find and read all the children of @dentry. */
1764 /* Read next child of @dentry into @cur_child. */
1765 ret = read_dentry(metadata_resource, metadata_resource_len,
1766 cur_offset, &cur_child);
1770 /* Check for end of directory. */
1771 if (cur_child.length == 0)
1774 /* Not end of directory. Allocate this child permanently and
1775 * link it to the parent and previous child. */
1776 child = memdup(&cur_child, sizeof(struct wim_dentry));
1778 ERROR("Failed to allocate new dentry!");
1779 ret = WIMLIB_ERR_NOMEM;
1783 /* Advance to the offset of the next child. Note: We need to
1784 * advance by the TOTAL length of the dentry, not by the length
1785 * cur_child.length, which although it does take into account
1786 * the padding, it DOES NOT take into account alternate stream
1788 cur_offset += dentry_total_length(child);
1790 if (dentry_add_child(dentry, child)) {
1791 WARNING("Ignoring duplicate dentry \"%"WS"\"",
1793 WARNING("(In directory \"%"TS"\")", dentry_full_path(dentry));
1796 inode_add_dentry(child, child->d_inode);
1797 /* If there are children of this child, call this
1798 * procedure recursively. */
1799 if (child->subdir_offset != 0) {
1800 if (dentry_is_directory(child)) {
1801 ret = read_dentry_tree(metadata_resource,
1802 metadata_resource_len,
1807 WARNING("Ignoring children of non-directory \"%"TS"\"",
1808 dentry_full_path(child));
1818 * Writes a WIM dentry to an output buffer.
1820 * @dentry: The dentry structure.
1821 * @p: The memory location to write the data to.
1823 * Returns the pointer to the byte after the last byte we wrote as part of the
1824 * dentry, including any alternate data stream entries.
1827 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
1829 const struct wim_inode *inode;
1830 struct wim_dentry_on_disk *disk_dentry;
1834 wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
1837 inode = dentry->d_inode;
1838 disk_dentry = (struct wim_dentry_on_disk*)p;
1840 disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
1841 disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
1842 disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
1843 disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
1844 disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
1845 disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
1846 disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
1847 disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
1848 hash = inode_stream_hash(inode, 0);
1849 copy_hash(disk_dentry->unnamed_stream_hash, hash);
1850 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1851 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
1852 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
1853 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
1854 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
1856 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
1857 disk_dentry->nonreparse.hard_link_group_id =
1858 cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
1860 disk_dentry->num_alternate_data_streams = cpu_to_le16(inode->i_num_ads);
1861 disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
1862 disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
1863 p += sizeof(struct wim_dentry_on_disk);
1865 if (dentry_has_long_name(dentry))
1866 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
1868 if (dentry_has_short_name(dentry))
1869 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
1871 /* Align to 8-byte boundary */
1872 while ((uintptr_t)p & 7)
1875 /* We calculate the correct length of the dentry ourselves because the
1876 * dentry->length field may been set to an unexpected value from when we
1877 * read the dentry in (for example, there may have been unknown data
1878 * appended to the end of the dentry...). Furthermore, the dentry may
1879 * have been renamed, thus changing its needed length. */
1880 disk_dentry->length = cpu_to_le64(p - orig_p);
1882 /* Write the alternate data streams entries, if any. */
1883 for (u16 i = 0; i < inode->i_num_ads; i++) {
1884 const struct wim_ads_entry *ads_entry =
1885 &inode->i_ads_entries[i];
1886 struct wim_ads_entry_on_disk *disk_ads_entry =
1887 (struct wim_ads_entry_on_disk*)p;
1890 disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
1892 hash = inode_stream_hash(inode, i + 1);
1893 copy_hash(disk_ads_entry->hash, hash);
1894 disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
1895 p += sizeof(struct wim_ads_entry_on_disk);
1896 if (ads_entry->stream_name_nbytes) {
1897 p = mempcpy(p, ads_entry->stream_name,
1898 ads_entry->stream_name_nbytes + 2);
1900 /* Align to 8-byte boundary */
1901 while ((uintptr_t)p & 7)
1903 disk_ads_entry->length = cpu_to_le64(p - orig_p);
1909 write_dentry_cb(struct wim_dentry *dentry, void *_p)
1912 *p = write_dentry(dentry, *p);
1917 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
1920 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
1923 *p = write_dentry_tree_recursive(dentry, *p);
1927 /* Recursive function that writes a dentry tree rooted at @parent, not including
1928 * @parent itself, which has already been written. */
1930 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
1932 /* Nothing to do if this dentry has no children. */
1933 if (parent->subdir_offset == 0)
1936 /* Write child dentries and end-of-directory entry.
1938 * Note: we need to write all of this dentry's children before
1939 * recursively writing the directory trees rooted at each of the child
1940 * dentries, since the on-disk dentries for a dentry's children are
1941 * always located at consecutive positions in the metadata resource! */
1942 for_dentry_child(parent, write_dentry_cb, &p);
1944 /* write end of directory entry */
1945 *(le64*)p = cpu_to_le64(0);
1948 /* Recurse on children. */
1949 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
1953 /* Writes a directory tree to the metadata resource.
1955 * @root: Root of the dentry tree.
1956 * @p: Pointer to a buffer with enough space for the dentry tree.
1958 * Returns pointer to the byte after the last byte we wrote.
1961 write_dentry_tree(const struct wim_dentry *root, u8 *p)
1963 DEBUG("Writing dentry tree.");
1964 wimlib_assert(dentry_is_root(root));
1966 /* If we're the root dentry, we have no parent that already
1967 * wrote us, so we need to write ourselves. */
1968 p = write_dentry(root, p);
1970 /* Write end of directory entry after the root dentry just to be safe;
1971 * however the root dentry obviously cannot have any siblings. */
1972 *(le64*)p = cpu_to_le64(0);
1975 /* Recursively write the rest of the dentry tree. */
1976 return write_dentry_tree_recursive(root, p);