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 #define WIM_ADS_ENTRY_DISK_SIZE 38
75 /* WIM directory entry (on-disk format) */
76 struct wim_dentry_on_disk {
84 le64 last_access_time;
86 u8 unnamed_stream_hash[SHA1_HASH_SIZE];
93 } _packed_attribute reparse;
96 le64 hard_link_group_id;
97 } _packed_attribute nonreparse;
99 le16 num_alternate_data_streams;
100 le16 short_name_nbytes;
101 le16 file_name_nbytes;
103 /* Follewed by variable length file name, if file_name_nbytes != 0 */
104 utf16lechar file_name[];
106 /* Followed by variable length short name, if short_name_nbytes != 0 */
107 /*utf16lechar short_name[];*/
110 #define WIM_DENTRY_DISK_SIZE 102
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. */
116 _dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
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;
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. */
131 dentry_correct_length_unaligned(const struct wim_dentry *dentry)
133 return _dentry_correct_length_unaligned(dentry->file_name_nbytes,
134 dentry->short_name_nbytes);
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. */
141 get_utf16le_name(const tchar *name, utf16lechar **name_utf16le_ret,
142 u16 *name_utf16le_nbytes_ret)
144 utf16lechar *name_utf16le;
145 size_t name_utf16le_nbytes;
148 name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
149 name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
151 return WIMLIB_ERR_NOMEM;
152 memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
156 ret = tstr_to_utf16le(name, tstrlen(name), &name_utf16le,
157 &name_utf16le_nbytes);
159 if (name_utf16le_nbytes > 0xffff) {
161 ERROR("Multibyte string \"%"TS"\" is too long!", name);
162 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
167 FREE(*name_utf16le_ret);
168 *name_utf16le_ret = name_utf16le;
169 *name_utf16le_nbytes_ret = name_utf16le_nbytes;
174 /* Sets the name of a WIM dentry from a multibyte string. */
176 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name)
179 ret = get_utf16le_name(new_name, &dentry->file_name,
180 &dentry->file_name_nbytes);
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;
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. */
196 ads_entry_total_length(const struct wim_ads_entry *entry)
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;
206 _dentry_total_length(const struct wim_dentry *dentry, u64 length)
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;
214 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
215 * all alternate data streams. */
217 dentry_correct_total_length(const struct wim_dentry *dentry)
219 return _dentry_total_length(dentry,
220 dentry_correct_length_unaligned(dentry));
223 /* Like dentry_correct_total_length(), but use the existing dentry->length field
224 * instead of calculating its "correct" value. */
226 dentry_total_length(const struct wim_dentry *dentry)
228 return _dentry_total_length(dentry, dentry->length);
232 for_dentry_in_rbtree(struct rb_node *root,
233 int (*visitor)(struct wim_dentry *, void *),
237 struct rb_node *node = root;
241 list_add(&rbnode_dentry(node)->tmp_list, &stack);
242 node = node->rb_left;
244 struct list_head *next;
245 struct wim_dentry *dentry;
250 dentry = container_of(next, struct wim_dentry, tmp_list);
252 ret = visitor(dentry, arg);
255 node = dentry->rb_node.rb_right;
261 for_dentry_tree_in_rbtree_depth(struct rb_node *node,
262 int (*visitor)(struct wim_dentry*, void*),
267 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
271 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
275 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
283 for_dentry_tree_in_rbtree(struct rb_node *node,
284 int (*visitor)(struct wim_dentry*, void*),
289 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
292 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
295 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
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.
307 for_dentry_in_tree(struct wim_dentry *root,
308 int (*visitor)(struct wim_dentry*, void*), void *arg)
314 ret = (*visitor)(root, arg);
317 return for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node,
322 /* Like for_dentry_in_tree(), but the visitor function is always called on a
323 * dentry's children before on itself. */
325 for_dentry_in_tree_depth(struct wim_dentry *root,
326 int (*visitor)(struct wim_dentry*, void*), void *arg)
332 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
336 return (*visitor)(root, arg);
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. */
342 calculate_dentry_full_path(struct wim_dentry *dentry)
345 u32 full_path_nbytes;
348 if (dentry->_full_path)
351 if (dentry_is_root(dentry)) {
352 static const tchar _root_path[] = {WIM_PATH_SEPARATOR, T('\0')};
353 full_path = TSTRDUP(_root_path);
355 return WIMLIB_ERR_NOMEM;
356 full_path_nbytes = 1 * sizeof(tchar);
358 struct wim_dentry *parent;
359 tchar *parent_full_path;
360 u32 parent_full_path_nbytes;
361 size_t filename_nbytes;
363 parent = dentry->parent;
364 if (dentry_is_root(parent)) {
365 parent_full_path = T("");
366 parent_full_path_nbytes = 0;
368 if (!parent->_full_path) {
369 ret = calculate_dentry_full_path(parent);
373 parent_full_path = parent->_full_path;
374 parent_full_path_nbytes = parent->full_path_nbytes;
377 /* Append this dentry's name as a tchar string to the full path
378 * of the parent followed by the path separator */
380 filename_nbytes = dentry->file_name_nbytes;
383 int ret = utf16le_to_tstr_nbytes(dentry->file_name,
384 dentry->file_name_nbytes,
391 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
393 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
395 return WIMLIB_ERR_NOMEM;
396 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
397 full_path[parent_full_path_nbytes / sizeof(tchar)] = WIM_PATH_SEPARATOR;
399 memcpy(&full_path[parent_full_path_nbytes / sizeof(tchar) + 1],
401 filename_nbytes + sizeof(tchar));
403 utf16le_to_tstr_buf(dentry->file_name,
404 dentry->file_name_nbytes,
405 &full_path[parent_full_path_nbytes /
409 dentry->_full_path = full_path;
410 dentry->full_path_nbytes= full_path_nbytes;
415 do_calculate_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
417 return calculate_dentry_full_path(dentry);
421 calculate_dentry_tree_full_paths(struct wim_dentry *root)
423 return for_dentry_in_tree(root, do_calculate_dentry_full_path, NULL);
427 dentry_full_path(struct wim_dentry *dentry)
429 calculate_dentry_full_path(dentry);
430 return dentry->_full_path;
434 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
436 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
441 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
443 calculate_subdir_offsets(dentry, subdir_offset_p);
448 * Recursively calculates the subdir offsets for a directory tree.
450 * @dentry: The root of the directory tree.
451 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
452 * offset for @dentry.
455 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
457 struct rb_node *node;
459 dentry->subdir_offset = *subdir_offset_p;
460 node = dentry->d_inode->i_children.rb_node;
462 /* Advance the subdir offset by the amount of space the children
463 * of this dentry take up. */
464 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
466 /* End-of-directory dentry on disk. */
467 *subdir_offset_p += 8;
469 /* Recursively call calculate_subdir_offsets() on all the
471 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
473 /* On disk, childless directories have a valid subdir_offset
474 * that points to an 8-byte end-of-directory dentry. Regular
475 * files or reparse points have a subdir_offset of 0. */
476 if (dentry_is_directory(dentry))
477 *subdir_offset_p += 8;
479 dentry->subdir_offset = 0;
483 /* Case-sensitive UTF-16LE dentry or stream name comparison. Used on both UNIX
484 * (always) and Windows (sometimes) */
486 compare_utf16le_names_case_sensitive(const utf16lechar *name1, size_t nbytes1,
487 const utf16lechar *name2, size_t nbytes2)
489 /* Return the result if the strings differ up to their minimum length.
490 * Note that we cannot use strcmp() or strncmp() here, as the strings
491 * are in UTF-16LE format. */
492 int result = memcmp(name1, name2, min(nbytes1, nbytes2));
496 /* The strings are the same up to their minimum length, so return a
497 * result based on their lengths. */
498 if (nbytes1 < nbytes2)
500 else if (nbytes1 > nbytes2)
507 /* Windoze: Case-insensitive UTF-16LE dentry or stream name comparison */
509 compare_utf16le_names_case_insensitive(const utf16lechar *name1, size_t nbytes1,
510 const utf16lechar *name2, size_t nbytes2)
512 /* Return the result if the strings differ up to their minimum length.
514 int result = _wcsnicmp((const wchar_t*)name1, (const wchar_t*)name2,
515 min(nbytes1 / 2, nbytes2 / 2));
519 /* The strings are the same up to their minimum length, so return a
520 * result based on their lengths. */
521 if (nbytes1 < nbytes2)
523 else if (nbytes1 > nbytes2)
528 #endif /* __WIN32__ */
531 # define compare_utf16le_names compare_utf16le_names_case_insensitive
533 # define compare_utf16le_names compare_utf16le_names_case_sensitive
539 dentry_compare_names_case_insensitive(const struct wim_dentry *d1,
540 const struct wim_dentry *d2)
542 return compare_utf16le_names_case_insensitive(d1->file_name,
543 d1->file_name_nbytes,
545 d2->file_name_nbytes);
547 #endif /* __WIN32__ */
550 dentry_compare_names_case_sensitive(const struct wim_dentry *d1,
551 const struct wim_dentry *d2)
553 return compare_utf16le_names_case_sensitive(d1->file_name,
554 d1->file_name_nbytes,
556 d2->file_name_nbytes);
560 # define dentry_compare_names dentry_compare_names_case_insensitive
562 # define dentry_compare_names dentry_compare_names_case_sensitive
565 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
566 * stream name @name that has length @name_nbytes bytes. */
568 ads_entry_has_name(const struct wim_ads_entry *entry,
569 const utf16lechar *name, size_t name_nbytes)
571 return !compare_utf16le_names(name, name_nbytes,
573 entry->stream_name_nbytes);
576 /* Given a UTF-16LE filename and a directory, look up the dentry for the file.
577 * Return it if found, otherwise NULL. This is case-sensitive on UNIX and
578 * case-insensitive on Windows. */
580 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
581 const utf16lechar *name,
584 struct rb_node *node;
587 node = dentry->d_inode->i_children_case_insensitive.rb_node;
589 node = dentry->d_inode->i_children.rb_node;
592 struct wim_dentry *child;
595 child = rb_entry(node, struct wim_dentry, rb_node_case_insensitive);
597 child = rbnode_dentry(node);
599 int result = compare_utf16le_names(name, name_nbytes,
601 child->file_name_nbytes);
603 node = node->rb_left;
605 node = node->rb_right;
608 if (!list_empty(&child->case_insensitive_conflict_list))
610 WARNING("Result of case-insensitive lookup is ambiguous "
611 "(returning \"%ls\" instead of \"%ls\")",
613 container_of(child->case_insensitive_conflict_list.next,
615 case_insensitive_conflict_list)->file_name);
624 /* Returns the child of @dentry that has the file name @name. Returns NULL if
625 * no child has the name. */
627 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
630 return get_dentry_child_with_utf16le_name(dentry, name,
631 tstrlen(name) * sizeof(tchar));
633 utf16lechar *utf16le_name;
634 size_t utf16le_name_nbytes;
636 struct wim_dentry *child;
638 ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
639 &utf16le_name, &utf16le_name_nbytes);
643 child = get_dentry_child_with_utf16le_name(dentry,
645 utf16le_name_nbytes);
652 static struct wim_dentry *
653 get_dentry_utf16le(WIMStruct *wim, const utf16lechar *path)
655 struct wim_dentry *cur_dentry, *parent_dentry;
656 const utf16lechar *p, *pp;
658 cur_dentry = parent_dentry = wim_root_dentry(wim);
665 while (*p == cpu_to_le16(WIM_PATH_SEPARATOR))
667 if (*p == cpu_to_le16('\0'))
670 while (*pp != cpu_to_le16(WIM_PATH_SEPARATOR) &&
671 *pp != cpu_to_le16('\0'))
674 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
675 (void*)pp - (void*)p);
676 if (cur_dentry == NULL)
679 parent_dentry = cur_dentry;
681 if (cur_dentry == NULL) {
682 if (dentry_is_directory(parent_dentry))
690 /* Returns the dentry corresponding to the @path, or NULL if there is no such
693 get_dentry(WIMStruct *wim, const tchar *path)
696 return get_dentry_utf16le(wim, path);
698 utf16lechar *path_utf16le;
699 size_t path_utf16le_nbytes;
701 struct wim_dentry *dentry;
703 ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
704 &path_utf16le, &path_utf16le_nbytes);
707 dentry = get_dentry_utf16le(wim, path_utf16le);
714 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
716 struct wim_dentry *dentry;
717 dentry = get_dentry(wim, path);
719 return dentry->d_inode;
724 /* Takes in a path of length @len in @buf, and transforms it into a string for
725 * the path of its parent directory. */
727 to_parent_name(tchar *buf, size_t len)
729 ssize_t i = (ssize_t)len - 1;
730 while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
732 while (i >= 0 && buf[i] != WIM_PATH_SEPARATOR)
734 while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
736 buf[i + 1] = T('\0');
739 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
740 * if the dentry is not found. */
742 get_parent_dentry(WIMStruct *wim, const tchar *path)
744 size_t path_len = tstrlen(path);
745 tchar buf[path_len + 1];
747 tmemcpy(buf, path, path_len + 1);
748 to_parent_name(buf, path_len);
749 return get_dentry(wim, buf);
752 /* Prints the full path of a dentry. */
754 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
756 int ret = calculate_dentry_full_path(dentry);
759 tprintf(T("%"TS"\n"), dentry->_full_path);
763 /* We want to be able to show the names of the file attribute flags that are
765 struct file_attr_flag {
769 struct file_attr_flag file_attr_flags[] = {
770 {FILE_ATTRIBUTE_READONLY, T("READONLY")},
771 {FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
772 {FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
773 {FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
774 {FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
775 {FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
776 {FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
777 {FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
778 {FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
779 {FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
780 {FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
781 {FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
782 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
783 {FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
784 {FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
787 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
788 * available. If the dentry is unresolved and the lookup table is NULL, the
789 * lookup table entries will not be printed. Otherwise, they will be. */
791 print_dentry(struct wim_dentry *dentry, void *lookup_table)
794 struct wim_lookup_table_entry *lte;
795 const struct wim_inode *inode = dentry->d_inode;
798 tprintf(T("[DENTRY]\n"));
799 tprintf(T("Length = %"PRIu64"\n"), dentry->length);
800 tprintf(T("Attributes = 0x%x\n"), inode->i_attributes);
801 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
802 if (file_attr_flags[i].flag & inode->i_attributes)
803 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
804 file_attr_flags[i].name);
805 tprintf(T("Security ID = %d\n"), inode->i_security_id);
806 tprintf(T("Subdir offset = %"PRIu64"\n"), dentry->subdir_offset);
808 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
809 tprintf(T("Creation Time = %"TS"\n"), buf);
811 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
812 tprintf(T("Last Access Time = %"TS"\n"), buf);
814 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
815 tprintf(T("Last Write Time = %"TS"\n"), buf);
817 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
818 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), inode->i_reparse_tag);
819 tprintf(T("Reparse Point Flags = 0x%"PRIx16"\n"),
820 inode->i_not_rpfixed);
821 tprintf(T("Reparse Point Unknown 2 = 0x%"PRIx32"\n"),
822 inode->i_rp_unknown_2);
824 tprintf(T("Reparse Point Unknown 1 = 0x%"PRIx32"\n"),
825 inode->i_rp_unknown_1);
826 tprintf(T("Hard Link Group = 0x%"PRIx64"\n"), inode->i_ino);
827 tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
828 tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
829 if (dentry_has_long_name(dentry))
830 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
831 if (dentry_has_short_name(dentry))
832 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
833 if (dentry->_full_path)
834 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
836 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
838 print_lookup_table_entry(lte, stdout);
840 hash = inode_stream_hash(inode, 0);
842 tprintf(T("Hash = 0x"));
843 print_hash(hash, stdout);
848 for (u16 i = 0; i < inode->i_num_ads; i++) {
849 tprintf(T("[Alternate Stream Entry %u]\n"), i);
850 wimlib_printf(T("Name = \"%"WS"\"\n"),
851 inode->i_ads_entries[i].stream_name);
852 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
853 inode->i_ads_entries[i].stream_name_nbytes);
854 hash = inode_stream_hash(inode, i + 1);
856 tprintf(T("Hash = 0x"));
857 print_hash(hash, stdout);
860 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
866 /* Initializations done on every `struct wim_dentry'. */
868 dentry_common_init(struct wim_dentry *dentry)
870 memset(dentry, 0, sizeof(struct wim_dentry));
874 new_timeless_inode(void)
876 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
878 inode->i_security_id = -1;
880 inode->i_next_stream_id = 1;
881 inode->i_not_rpfixed = 1;
882 INIT_LIST_HEAD(&inode->i_list);
884 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
885 ERROR_WITH_ERRNO("Error initializing mutex");
890 INIT_LIST_HEAD(&inode->i_dentry);
895 static struct wim_inode *
898 struct wim_inode *inode = new_timeless_inode();
900 u64 now = get_wim_timestamp();
901 inode->i_creation_time = now;
902 inode->i_last_access_time = now;
903 inode->i_last_write_time = now;
908 /* Creates an unlinked directory entry. */
910 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
912 struct wim_dentry *dentry;
915 dentry = MALLOC(sizeof(struct wim_dentry));
917 return WIMLIB_ERR_NOMEM;
919 dentry_common_init(dentry);
920 ret = set_dentry_name(dentry, name);
922 dentry->parent = dentry;
923 *dentry_ret = dentry;
926 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
934 _new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
937 struct wim_dentry *dentry;
940 ret = new_dentry(name, &dentry);
945 dentry->d_inode = new_timeless_inode();
947 dentry->d_inode = new_inode();
948 if (!dentry->d_inode) {
950 return WIMLIB_ERR_NOMEM;
953 inode_add_dentry(dentry, dentry->d_inode);
954 *dentry_ret = dentry;
959 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
961 return _new_dentry_with_inode(name, dentry_ret, true);
965 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
967 return _new_dentry_with_inode(name, dentry_ret, false);
971 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
974 struct wim_dentry *dentry;
976 DEBUG("Creating filler directory \"%"TS"\"", name);
977 ret = new_dentry_with_inode(name, &dentry);
980 /* Leave the inode number as 0; this is allowed for non
981 * hard-linked files. */
982 dentry->d_inode->i_resolved = 1;
983 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
984 *dentry_ret = dentry;
989 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
990 size_t name_nbytes, bool is_utf16le)
993 memset(ads_entry, 0, sizeof(*ads_entry));
996 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
998 return WIMLIB_ERR_NOMEM;
999 memcpy(p, name, name_nbytes);
1000 p[name_nbytes / 2] = cpu_to_le16(0);
1001 ads_entry->stream_name = p;
1002 ads_entry->stream_name_nbytes = name_nbytes;
1004 if (name && *(const tchar*)name != T('\0')) {
1005 ret = get_utf16le_name(name, &ads_entry->stream_name,
1006 &ads_entry->stream_name_nbytes);
1013 destroy_ads_entry(struct wim_ads_entry *ads_entry)
1015 FREE(ads_entry->stream_name);
1018 /* Frees an inode. */
1020 free_inode(struct wim_inode *inode)
1023 if (inode->i_ads_entries) {
1024 for (u16 i = 0; i < inode->i_num_ads; i++)
1025 destroy_ads_entry(&inode->i_ads_entries[i]);
1026 FREE(inode->i_ads_entries);
1029 wimlib_assert(inode->i_num_opened_fds == 0);
1031 pthread_mutex_destroy(&inode->i_mutex);
1033 /* HACK: This may instead delete the inode from i_list, but the
1034 * hlist_del() behaves the same as list_del(). */
1035 hlist_del(&inode->i_hlist);
1036 FREE(inode->i_extracted_file);
1041 /* Decrements link count on an inode and frees it if the link count reaches 0.
1044 put_inode(struct wim_inode *inode)
1046 wimlib_assert(inode->i_nlink != 0);
1047 if (--inode->i_nlink == 0) {
1049 if (inode->i_num_opened_fds == 0)
1057 /* Frees a WIM dentry.
1059 * The corresponding inode (if any) is freed only if its link count is
1063 free_dentry(struct wim_dentry *dentry)
1066 FREE(dentry->file_name);
1067 FREE(dentry->short_name);
1068 FREE(dentry->_full_path);
1069 if (dentry->d_inode)
1070 put_inode(dentry->d_inode);
1075 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
1076 * to free a directory tree. */
1078 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
1080 struct wim_lookup_table *lookup_table = _lookup_table;
1083 struct wim_inode *inode = dentry->d_inode;
1084 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1085 struct wim_lookup_table_entry *lte;
1087 lte = inode_stream_lte(inode, i, lookup_table);
1089 lte_decrement_refcnt(lte, lookup_table);
1092 free_dentry(dentry);
1097 * Unlinks and frees a dentry tree.
1099 * @root: The root of the tree.
1100 * @lookup_table: The lookup table for dentries. If non-NULL, the
1101 * reference counts in the lookup table for the lookup
1102 * table entries corresponding to the dentries will be
1106 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1108 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1113 /* Insert a dentry into the case insensitive index for a directory.
1115 * This is a red-black tree, but when multiple dentries share the same
1116 * case-insensitive name, only one is inserted into the tree itself; the rest
1117 * are connected in a list.
1119 static struct wim_dentry *
1120 dentry_add_child_case_insensitive(struct wim_dentry *parent,
1121 struct wim_dentry *child)
1123 struct rb_root *root;
1124 struct rb_node **new;
1125 struct rb_node *rb_parent;
1127 root = &parent->d_inode->i_children_case_insensitive;
1128 new = &root->rb_node;
1131 struct wim_dentry *this = container_of(*new, struct wim_dentry,
1132 rb_node_case_insensitive);
1133 int result = dentry_compare_names_case_insensitive(child, this);
1138 new = &((*new)->rb_left);
1139 else if (result > 0)
1140 new = &((*new)->rb_right);
1144 rb_link_node(&child->rb_node_case_insensitive, rb_parent, new);
1145 rb_insert_color(&child->rb_node_case_insensitive, root);
1151 * Links a dentry into the directory tree.
1153 * @parent: The dentry that will be the parent of @child.
1154 * @child: The dentry to link.
1156 * Returns NULL if successful. If @parent already contains a dentry with the
1157 * same case-sensitive name as @child, the pointer to this duplicate dentry is
1161 dentry_add_child(struct wim_dentry * restrict parent,
1162 struct wim_dentry * restrict child)
1164 struct rb_root *root;
1165 struct rb_node **new;
1166 struct rb_node *rb_parent;
1168 wimlib_assert(dentry_is_directory(parent));
1169 wimlib_assert(parent != child);
1171 /* Case sensitive child dentry index */
1172 root = &parent->d_inode->i_children;
1173 new = &root->rb_node;
1176 struct wim_dentry *this = rbnode_dentry(*new);
1177 int result = dentry_compare_names_case_sensitive(child, this);
1182 new = &((*new)->rb_left);
1183 else if (result > 0)
1184 new = &((*new)->rb_right);
1188 child->parent = parent;
1189 rb_link_node(&child->rb_node, rb_parent, new);
1190 rb_insert_color(&child->rb_node, root);
1194 struct wim_dentry *existing;
1195 existing = dentry_add_child_case_insensitive(parent, child);
1197 list_add(&child->case_insensitive_conflict_list,
1198 &existing->case_insensitive_conflict_list);
1199 child->rb_node_case_insensitive.__rb_parent_color = 0;
1201 INIT_LIST_HEAD(&child->case_insensitive_conflict_list);
1208 /* Unlink a WIM dentry from the directory entry tree. */
1210 unlink_dentry(struct wim_dentry *dentry)
1212 struct wim_dentry *parent = dentry->parent;
1214 if (parent == dentry)
1216 rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1218 if (dentry->rb_node_case_insensitive.__rb_parent_color) {
1219 /* This dentry was in the case-insensitive red-black tree. */
1220 rb_erase(&dentry->rb_node_case_insensitive,
1221 &parent->d_inode->i_children_case_insensitive);
1222 if (!list_empty(&dentry->case_insensitive_conflict_list)) {
1223 /* Make a different case-insensitively-the-same dentry
1224 * be the "representative" in the red-black tree. */
1225 struct list_head *next;
1226 struct wim_dentry *other;
1227 struct wim_dentry *existing;
1229 next = dentry->case_insensitive_conflict_list.next;
1230 other = list_entry(next, struct wim_dentry, case_insensitive_conflict_list);
1231 existing = dentry_add_child_case_insensitive(parent, other);
1232 wimlib_assert(existing == NULL);
1235 list_del(&dentry->case_insensitive_conflict_list);
1240 * Returns the alternate data stream entry belonging to @inode that has the
1241 * stream name @stream_name.
1243 struct wim_ads_entry *
1244 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1247 if (inode->i_num_ads == 0) {
1250 size_t stream_name_utf16le_nbytes;
1252 struct wim_ads_entry *result;
1254 #if TCHAR_IS_UTF16LE
1255 const utf16lechar *stream_name_utf16le;
1257 stream_name_utf16le = stream_name;
1258 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1260 utf16lechar *stream_name_utf16le;
1263 int ret = tstr_to_utf16le(stream_name,
1264 tstrlen(stream_name) *
1266 &stream_name_utf16le,
1267 &stream_name_utf16le_nbytes);
1275 if (ads_entry_has_name(&inode->i_ads_entries[i],
1276 stream_name_utf16le,
1277 stream_name_utf16le_nbytes))
1281 result = &inode->i_ads_entries[i];
1284 } while (++i != inode->i_num_ads);
1285 #if !TCHAR_IS_UTF16LE
1286 FREE(stream_name_utf16le);
1292 static struct wim_ads_entry *
1293 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1294 size_t stream_name_nbytes, bool is_utf16le)
1297 struct wim_ads_entry *ads_entries;
1298 struct wim_ads_entry *new_entry;
1300 if (inode->i_num_ads >= 0xfffe) {
1301 ERROR("Too many alternate data streams in one inode!");
1304 num_ads = inode->i_num_ads + 1;
1305 ads_entries = REALLOC(inode->i_ads_entries,
1306 num_ads * sizeof(inode->i_ads_entries[0]));
1308 ERROR("Failed to allocate memory for new alternate data stream");
1311 inode->i_ads_entries = ads_entries;
1313 new_entry = &inode->i_ads_entries[num_ads - 1];
1314 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1316 new_entry->stream_id = inode->i_next_stream_id++;
1317 inode->i_num_ads = num_ads;
1321 struct wim_ads_entry *
1322 inode_add_ads_utf16le(struct wim_inode *inode,
1323 const utf16lechar *stream_name,
1324 size_t stream_name_nbytes)
1326 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1327 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1331 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1332 * NULL if memory could not be allocated.
1334 struct wim_ads_entry *
1335 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1337 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1338 return do_inode_add_ads(inode, stream_name,
1339 tstrlen(stream_name) * sizeof(tchar),
1343 static struct wim_lookup_table_entry *
1344 add_stream_from_data_buffer(const void *buffer, size_t size,
1345 struct wim_lookup_table *lookup_table)
1347 u8 hash[SHA1_HASH_SIZE];
1348 struct wim_lookup_table_entry *lte, *existing_lte;
1350 sha1_buffer(buffer, size, hash);
1351 existing_lte = __lookup_resource(lookup_table, hash);
1353 wimlib_assert(wim_resource_size(existing_lte) == size);
1358 lte = new_lookup_table_entry();
1361 buffer_copy = memdup(buffer, size);
1363 free_lookup_table_entry(lte);
1366 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1367 lte->attached_buffer = buffer_copy;
1368 lte->resource_entry.original_size = size;
1369 copy_hash(lte->hash, hash);
1370 lookup_table_insert(lookup_table, lte);
1376 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1377 const void *value, size_t size,
1378 struct wim_lookup_table *lookup_table)
1380 struct wim_ads_entry *new_ads_entry;
1382 wimlib_assert(inode->i_resolved);
1384 new_ads_entry = inode_add_ads(inode, name);
1386 return WIMLIB_ERR_NOMEM;
1388 new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1390 if (!new_ads_entry->lte) {
1391 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1393 return WIMLIB_ERR_NOMEM;
1398 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
1399 * stream contents. */
1401 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
1402 struct wim_lookup_table *lookup_table)
1404 inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
1406 return WIMLIB_ERR_NOMEM;
1407 inode->i_resolved = 1;
1411 /* Remove an alternate data stream from a WIM inode */
1413 inode_remove_ads(struct wim_inode *inode, u16 idx,
1414 struct wim_lookup_table *lookup_table)
1416 struct wim_ads_entry *ads_entry;
1417 struct wim_lookup_table_entry *lte;
1419 wimlib_assert(idx < inode->i_num_ads);
1420 wimlib_assert(inode->i_resolved);
1422 ads_entry = &inode->i_ads_entries[idx];
1424 DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1426 lte = ads_entry->lte;
1428 lte_decrement_refcnt(lte, lookup_table);
1430 destroy_ads_entry(ads_entry);
1432 memmove(&inode->i_ads_entries[idx],
1433 &inode->i_ads_entries[idx + 1],
1434 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1440 inode_get_unix_data(const struct wim_inode *inode,
1441 struct wimlib_unix_data *unix_data,
1442 u16 *stream_idx_ret)
1444 const struct wim_ads_entry *ads_entry;
1445 const struct wim_lookup_table_entry *lte;
1449 wimlib_assert(inode->i_resolved);
1451 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1452 WIMLIB_UNIX_DATA_TAG, NULL);
1454 return NO_UNIX_DATA;
1457 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1459 lte = ads_entry->lte;
1461 return NO_UNIX_DATA;
1463 size = wim_resource_size(lte);
1464 if (size != sizeof(struct wimlib_unix_data))
1465 return BAD_UNIX_DATA;
1467 ret = read_full_resource_into_buf(lte, unix_data);
1471 if (unix_data->version != 0)
1472 return BAD_UNIX_DATA;
1477 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1478 struct wim_lookup_table *lookup_table, int which)
1480 struct wimlib_unix_data unix_data;
1482 bool have_good_unix_data = false;
1483 bool have_unix_data = false;
1486 if (!(which & UNIX_DATA_CREATE)) {
1487 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1488 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1489 have_unix_data = true;
1491 have_good_unix_data = true;
1493 unix_data.version = 0;
1494 if (which & UNIX_DATA_UID || !have_good_unix_data)
1495 unix_data.uid = uid;
1496 if (which & UNIX_DATA_GID || !have_good_unix_data)
1497 unix_data.gid = gid;
1498 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1499 unix_data.mode = mode;
1500 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1502 sizeof(struct wimlib_unix_data),
1504 if (ret == 0 && have_unix_data)
1505 inode_remove_ads(inode, stream_idx, lookup_table);
1508 #endif /* !__WIN32__ */
1511 * Reads the alternate data stream entries of a WIM dentry.
1513 * @p: Pointer to buffer that starts with the first alternate stream entry.
1515 * @inode: Inode to load the alternate data streams into.
1516 * @inode->i_num_ads must have been set to the number of
1517 * alternate data streams that are expected.
1519 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1523 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1524 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1525 * failure, @inode is not modified.
1528 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1529 size_t nbytes_remaining)
1532 struct wim_ads_entry *ads_entries;
1535 BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
1537 /* Allocate an array for our in-memory representation of the alternate
1538 * data stream entries. */
1539 num_ads = inode->i_num_ads;
1540 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1544 /* Read the entries into our newly allocated buffer. */
1545 for (u16 i = 0; i < num_ads; i++) {
1547 struct wim_ads_entry *cur_entry;
1548 const struct wim_ads_entry_on_disk *disk_entry =
1549 (const struct wim_ads_entry_on_disk*)p;
1551 cur_entry = &ads_entries[i];
1552 ads_entries[i].stream_id = i + 1;
1554 /* Do we have at least the size of the fixed-length data we know
1556 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1559 /* Read the length field */
1560 length = le64_to_cpu(disk_entry->length);
1562 /* Make sure the length field is neither so small it doesn't
1563 * include all the fixed-length data nor so large it overflows
1564 * the metadata resource buffer. */
1565 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1566 length > nbytes_remaining)
1569 /* Read the rest of the fixed-length data. */
1571 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1572 copy_hash(cur_entry->hash, disk_entry->hash);
1573 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1575 /* If stream_name_nbytes != 0, this is a named stream.
1576 * Otherwise this is an unnamed stream, or in some cases (bugs
1577 * in Microsoft's software I guess) a meaningless entry
1578 * distinguished from the real unnamed stream entry, if any, by
1579 * the fact that the real unnamed stream entry has a nonzero
1581 if (cur_entry->stream_name_nbytes) {
1582 /* The name is encoded in UTF16-LE, which uses 2-byte
1583 * coding units, so the length of the name had better be
1584 * an even number of bytes... */
1585 if (cur_entry->stream_name_nbytes & 1)
1588 /* Add the length of the stream name to get the length
1589 * we actually need to read. Make sure this isn't more
1590 * than the specified length of the entry. */
1591 if (sizeof(struct wim_ads_entry_on_disk) +
1592 cur_entry->stream_name_nbytes > length)
1595 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1596 if (!cur_entry->stream_name)
1599 memcpy(cur_entry->stream_name,
1600 disk_entry->stream_name,
1601 cur_entry->stream_name_nbytes);
1602 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1605 /* It's expected that the size of every ADS entry is a multiple
1606 * of 8. However, to be safe, I'm allowing the possibility of
1607 * an ADS entry at the very end of the metadata resource ending
1608 * un-aligned. So although we still need to increment the input
1609 * pointer by @length to reach the next ADS entry, it's possible
1610 * that less than @length is actually remaining in the metadata
1611 * resource. We should set the remaining bytes to 0 if this
1613 length = (length + 7) & ~(u64)7;
1615 if (nbytes_remaining < length)
1616 nbytes_remaining = 0;
1618 nbytes_remaining -= length;
1620 inode->i_ads_entries = ads_entries;
1621 inode->i_next_stream_id = inode->i_num_ads + 1;
1625 ret = WIMLIB_ERR_NOMEM;
1626 goto out_free_ads_entries;
1628 ERROR("An alternate data stream entry is invalid");
1629 ret = WIMLIB_ERR_INVALID_DENTRY;
1630 out_free_ads_entries:
1632 for (u16 i = 0; i < num_ads; i++)
1633 destroy_ads_entry(&ads_entries[i]);
1641 * Reads a WIM directory entry, including all alternate data stream entries that
1642 * follow it, from the WIM image's metadata resource.
1644 * @metadata_resource:
1645 * Pointer to the metadata resource buffer.
1647 * @metadata_resource_len:
1648 * Length of the metadata resource buffer, in bytes.
1650 * @offset: Offset of the dentry within the metadata resource.
1652 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1654 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1655 * been modified, but it will not be left with pointers to any allocated
1656 * buffers. On success, the dentry->length field must be examined. If zero,
1657 * this was a special "end of directory" dentry and not a real dentry. If
1658 * nonzero, this was a real dentry.
1660 * Possible errors include:
1662 * WIMLIB_ERR_INVALID_DENTRY
1665 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1666 u64 offset, struct wim_dentry * restrict dentry)
1669 u64 calculated_size;
1670 utf16lechar *file_name;
1671 utf16lechar *short_name;
1672 u16 short_name_nbytes;
1673 u16 file_name_nbytes;
1675 struct wim_inode *inode;
1676 const u8 *p = &metadata_resource[offset];
1677 const struct wim_dentry_on_disk *disk_dentry =
1678 (const struct wim_dentry_on_disk*)p;
1680 BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE);
1682 if ((uintptr_t)p & 7)
1683 WARNING("WIM dentry is not 8-byte aligned");
1685 dentry_common_init(dentry);
1687 /* Before reading the whole dentry, we need to read just the length.
1688 * This is because a dentry of length 8 (that is, just the length field)
1689 * terminates the list of sibling directory entries. */
1690 if (offset + sizeof(u64) > metadata_resource_len ||
1691 offset + sizeof(u64) < offset)
1693 ERROR("Directory entry starting at %"PRIu64" ends past the "
1694 "end of the metadata resource (size %"PRIu64")",
1695 offset, metadata_resource_len);
1696 return WIMLIB_ERR_INVALID_DENTRY;
1698 dentry->length = le64_to_cpu(disk_dentry->length);
1700 /* A zero length field (really a length of 8, since that's how big the
1701 * directory entry is...) indicates that this is the end of directory
1702 * dentry. We do not read it into memory as an actual dentry, so just
1703 * return successfully in this case. */
1704 if (dentry->length == 8)
1706 if (dentry->length == 0)
1709 /* Now that we have the actual length provided in the on-disk structure,
1710 * again make sure it doesn't overflow the metadata resource buffer. */
1711 if (offset + dentry->length > metadata_resource_len ||
1712 offset + dentry->length < offset)
1714 ERROR("Directory entry at offset %"PRIu64" and with size "
1715 "%"PRIu64" ends past the end of the metadata resource "
1717 offset, dentry->length, metadata_resource_len);
1718 return WIMLIB_ERR_INVALID_DENTRY;
1721 /* Make sure the dentry length is at least as large as the number of
1722 * fixed-length fields */
1723 if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1724 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1726 return WIMLIB_ERR_INVALID_DENTRY;
1729 /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1730 inode = new_timeless_inode();
1732 return WIMLIB_ERR_NOMEM;
1734 /* Read more fields; some into the dentry, and some into the inode. */
1736 inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1737 inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1738 dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1739 dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1740 dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1741 inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1742 inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1743 inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1744 copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1746 /* I don't know what's going on here. It seems like M$ screwed up the
1747 * reparse points, then put the fields in the same place and didn't
1748 * document it. So we have some fields we read for reparse points, and
1749 * some fields in the same place for non-reparse-point.s */
1750 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1751 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1752 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1753 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1754 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1755 /* Leave inode->i_ino at 0. Note that this means the WIM file
1756 * cannot archive hard-linked reparse points. Such a thing
1757 * doesn't really make sense anyway, although I believe it's
1758 * theoretically possible to have them on NTFS. */
1760 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1761 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1764 inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1766 short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1767 file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1769 if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1771 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1772 ret = WIMLIB_ERR_INVALID_DENTRY;
1773 goto out_free_inode;
1776 /* We now know the length of the file name and short name. Make sure
1777 * the length of the dentry is large enough to actually hold them.
1779 * The calculated length here is unaligned to allow for the possibility
1780 * that the dentry->length names an unaligned length, although this
1781 * would be unexpected. */
1782 calculated_size = _dentry_correct_length_unaligned(file_name_nbytes,
1785 if (dentry->length < calculated_size) {
1786 ERROR("Unexpected end of directory entry! (Expected "
1787 "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1788 calculated_size, dentry->length);
1789 ret = WIMLIB_ERR_INVALID_DENTRY;
1790 goto out_free_inode;
1793 p += sizeof(struct wim_dentry_on_disk);
1795 /* Read the filename if present. Note: if the filename is empty, there
1796 * is no null terminator following it. */
1797 if (file_name_nbytes) {
1798 file_name = MALLOC(file_name_nbytes + 2);
1800 ERROR("Failed to allocate %d bytes for dentry file name",
1801 file_name_nbytes + 2);
1802 ret = WIMLIB_ERR_NOMEM;
1803 goto out_free_inode;
1805 memcpy(file_name, p, file_name_nbytes);
1806 p += file_name_nbytes + 2;
1807 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
1813 /* Read the short filename if present. Note: if there is no short
1814 * filename, there is no null terminator following it. */
1815 if (short_name_nbytes) {
1816 short_name = MALLOC(short_name_nbytes + 2);
1818 ERROR("Failed to allocate %d bytes for dentry short name",
1819 short_name_nbytes + 2);
1820 ret = WIMLIB_ERR_NOMEM;
1821 goto out_free_file_name;
1823 memcpy(short_name, p, short_name_nbytes);
1824 p += short_name_nbytes + 2;
1825 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
1830 /* Align the dentry length */
1831 dentry->length = (dentry->length + 7) & ~7;
1834 * Read the alternate data streams, if present. dentry->num_ads tells
1835 * us how many they are, and they will directly follow the dentry
1838 * Note that each alternate data stream entry begins on an 8-byte
1839 * aligned boundary, and the alternate data stream entries seem to NOT
1840 * be included in the dentry->length field for some reason.
1842 if (inode->i_num_ads != 0) {
1843 ret = WIMLIB_ERR_INVALID_DENTRY;
1844 if (offset + dentry->length > metadata_resource_len ||
1845 (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
1847 metadata_resource_len - offset - dentry->length)))
1849 ERROR("Failed to read alternate data stream "
1850 "entries of WIM dentry \"%"WS"\"", file_name);
1851 goto out_free_short_name;
1854 /* We've read all the data for this dentry. Set the names and their
1855 * lengths, and we've done. */
1856 dentry->d_inode = inode;
1857 dentry->file_name = file_name;
1858 dentry->short_name = short_name;
1859 dentry->file_name_nbytes = file_name_nbytes;
1860 dentry->short_name_nbytes = short_name_nbytes;
1863 out_free_short_name:
1873 static const tchar *
1874 dentry_get_file_type_string(const struct wim_dentry *dentry)
1876 const struct wim_inode *inode = dentry->d_inode;
1877 if (inode_is_directory(inode))
1878 return T("directory");
1879 else if (inode_is_symlink(inode))
1880 return T("symbolic link");
1885 /* Reads the children of a dentry, and all their children, ..., etc. from the
1886 * metadata resource and into the dentry tree.
1888 * @metadata_resource: An array that contains the uncompressed metadata
1889 * resource for the WIM file.
1891 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1894 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
1895 * tree and has already been read from the metadata resource. It
1896 * does not need to be the real root because this procedure is
1897 * called recursively.
1899 * Returns zero on success; nonzero on failure.
1902 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1903 struct wim_dentry *dentry)
1905 u64 cur_offset = dentry->subdir_offset;
1906 struct wim_dentry *child;
1907 struct wim_dentry *duplicate;
1908 struct wim_dentry cur_child;
1912 * If @dentry has no child dentries, nothing more needs to be done for
1913 * this branch. This is the case for regular files, symbolic links, and
1914 * *possibly* empty directories (although an empty directory may also
1915 * have one child dentry that is the special end-of-directory dentry)
1917 if (cur_offset == 0)
1920 /* Find and read all the children of @dentry. */
1923 /* Read next child of @dentry into @cur_child. */
1924 ret = read_dentry(metadata_resource, metadata_resource_len,
1925 cur_offset, &cur_child);
1929 /* Check for end of directory. */
1930 if (cur_child.length == 0)
1933 /* Not end of directory. Allocate this child permanently and
1934 * link it to the parent and previous child. */
1935 child = memdup(&cur_child, sizeof(struct wim_dentry));
1937 ERROR("Failed to allocate new dentry!");
1938 ret = WIMLIB_ERR_NOMEM;
1942 /* Advance to the offset of the next child. Note: We need to
1943 * advance by the TOTAL length of the dentry, not by the length
1944 * cur_child.length, which although it does take into account
1945 * the padding, it DOES NOT take into account alternate stream
1947 cur_offset += dentry_total_length(child);
1949 duplicate = dentry_add_child(dentry, child);
1951 const tchar *child_type, *duplicate_type;
1952 child_type = dentry_get_file_type_string(child);
1953 duplicate_type = dentry_get_file_type_string(duplicate);
1954 WARNING("Ignoring duplicate %"TS" \"%"TS"\" "
1955 "(the WIM image already contains a %"TS" "
1956 "at that path with the exact same name)",
1957 child_type, dentry_full_path(duplicate),
1960 inode_add_dentry(child, child->d_inode);
1961 /* If there are children of this child, call this
1962 * procedure recursively. */
1963 if (child->subdir_offset != 0) {
1964 if (dentry_is_directory(child)) {
1965 ret = read_dentry_tree(metadata_resource,
1966 metadata_resource_len,
1971 WARNING("Ignoring children of non-directory \"%"TS"\"",
1972 dentry_full_path(child));
1982 * Writes a WIM dentry to an output buffer.
1984 * @dentry: The dentry structure.
1985 * @p: The memory location to write the data to.
1987 * Returns the pointer to the byte after the last byte we wrote as part of the
1988 * dentry, including any alternate data stream entries.
1991 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
1993 const struct wim_inode *inode;
1994 struct wim_dentry_on_disk *disk_dentry;
1998 wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
2001 inode = dentry->d_inode;
2002 disk_dentry = (struct wim_dentry_on_disk*)p;
2004 disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
2005 disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
2006 disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
2007 disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
2008 disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
2009 disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
2010 disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
2011 disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
2012 hash = inode_stream_hash(inode, 0);
2013 copy_hash(disk_dentry->unnamed_stream_hash, hash);
2014 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2015 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2016 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
2017 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
2018 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
2020 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2021 disk_dentry->nonreparse.hard_link_group_id =
2022 cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
2024 disk_dentry->num_alternate_data_streams = cpu_to_le16(inode->i_num_ads);
2025 disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
2026 disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
2027 p += sizeof(struct wim_dentry_on_disk);
2029 if (dentry_has_long_name(dentry))
2030 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
2032 if (dentry_has_short_name(dentry))
2033 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
2035 /* Align to 8-byte boundary */
2036 while ((uintptr_t)p & 7)
2039 /* We calculate the correct length of the dentry ourselves because the
2040 * dentry->length field may been set to an unexpected value from when we
2041 * read the dentry in (for example, there may have been unknown data
2042 * appended to the end of the dentry...). Furthermore, the dentry may
2043 * have been renamed, thus changing its needed length. */
2044 disk_dentry->length = cpu_to_le64(p - orig_p);
2046 /* Write the alternate data streams entries, if any. */
2047 for (u16 i = 0; i < inode->i_num_ads; i++) {
2048 const struct wim_ads_entry *ads_entry =
2049 &inode->i_ads_entries[i];
2050 struct wim_ads_entry_on_disk *disk_ads_entry =
2051 (struct wim_ads_entry_on_disk*)p;
2054 disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
2056 hash = inode_stream_hash(inode, i + 1);
2057 copy_hash(disk_ads_entry->hash, hash);
2058 disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
2059 p += sizeof(struct wim_ads_entry_on_disk);
2060 if (ads_entry->stream_name_nbytes) {
2061 p = mempcpy(p, ads_entry->stream_name,
2062 ads_entry->stream_name_nbytes + 2);
2064 /* Align to 8-byte boundary */
2065 while ((uintptr_t)p & 7)
2067 disk_ads_entry->length = cpu_to_le64(p - orig_p);
2073 write_dentry_cb(struct wim_dentry *dentry, void *_p)
2076 *p = write_dentry(dentry, *p);
2081 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
2084 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
2087 *p = write_dentry_tree_recursive(dentry, *p);
2091 /* Recursive function that writes a dentry tree rooted at @parent, not including
2092 * @parent itself, which has already been written. */
2094 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
2096 /* Nothing to do if this dentry has no children. */
2097 if (parent->subdir_offset == 0)
2100 /* Write child dentries and end-of-directory entry.
2102 * Note: we need to write all of this dentry's children before
2103 * recursively writing the directory trees rooted at each of the child
2104 * dentries, since the on-disk dentries for a dentry's children are
2105 * always located at consecutive positions in the metadata resource! */
2106 for_dentry_child(parent, write_dentry_cb, &p);
2108 /* write end of directory entry */
2109 *(le64*)p = cpu_to_le64(0);
2112 /* Recurse on children. */
2113 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
2117 /* Writes a directory tree to the metadata resource.
2119 * @root: Root of the dentry tree.
2120 * @p: Pointer to a buffer with enough space for the dentry tree.
2122 * Returns pointer to the byte after the last byte we wrote.
2125 write_dentry_tree(const struct wim_dentry *root, u8 *p)
2127 DEBUG("Writing dentry tree.");
2128 wimlib_assert(dentry_is_root(root));
2130 /* If we're the root dentry, we have no parent that already
2131 * wrote us, so we need to write ourselves. */
2132 p = write_dentry(root, p);
2134 /* Write end of directory entry after the root dentry just to be safe;
2135 * however the root dentry obviously cannot have any siblings. */
2136 *(le64*)p = cpu_to_le64(0);
2139 /* Recursively write the rest of the dentry tree. */
2140 return write_dentry_tree_recursive(root, p);