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 if (!hlist_unhashed(&inode->i_hlist))
1036 hlist_del(&inode->i_hlist);
1037 FREE(inode->i_extracted_file);
1042 /* Decrements link count on an inode and frees it if the link count reaches 0.
1045 put_inode(struct wim_inode *inode)
1047 wimlib_assert(inode->i_nlink != 0);
1048 if (--inode->i_nlink == 0) {
1050 if (inode->i_num_opened_fds == 0)
1058 /* Frees a WIM dentry.
1060 * The corresponding inode (if any) is freed only if its link count is
1064 free_dentry(struct wim_dentry *dentry)
1067 FREE(dentry->file_name);
1068 FREE(dentry->short_name);
1069 FREE(dentry->_full_path);
1070 if (dentry->d_inode)
1071 put_inode(dentry->d_inode);
1076 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
1077 * to free a directory tree. */
1079 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
1081 struct wim_lookup_table *lookup_table = _lookup_table;
1084 struct wim_inode *inode = dentry->d_inode;
1085 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1086 struct wim_lookup_table_entry *lte;
1088 lte = inode_stream_lte(inode, i, lookup_table);
1090 lte_decrement_refcnt(lte, lookup_table);
1093 free_dentry(dentry);
1098 * Unlinks and frees a dentry tree.
1100 * @root: The root of the tree.
1101 * @lookup_table: The lookup table for dentries. If non-NULL, the
1102 * reference counts in the lookup table for the lookup
1103 * table entries corresponding to the dentries will be
1107 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1109 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1114 /* Insert a dentry into the case insensitive index for a directory.
1116 * This is a red-black tree, but when multiple dentries share the same
1117 * case-insensitive name, only one is inserted into the tree itself; the rest
1118 * are connected in a list.
1120 static struct wim_dentry *
1121 dentry_add_child_case_insensitive(struct wim_dentry *parent,
1122 struct wim_dentry *child)
1124 struct rb_root *root;
1125 struct rb_node **new;
1126 struct rb_node *rb_parent;
1128 root = &parent->d_inode->i_children_case_insensitive;
1129 new = &root->rb_node;
1132 struct wim_dentry *this = container_of(*new, struct wim_dentry,
1133 rb_node_case_insensitive);
1134 int result = dentry_compare_names_case_insensitive(child, this);
1139 new = &((*new)->rb_left);
1140 else if (result > 0)
1141 new = &((*new)->rb_right);
1145 rb_link_node(&child->rb_node_case_insensitive, rb_parent, new);
1146 rb_insert_color(&child->rb_node_case_insensitive, root);
1152 * Links a dentry into the directory tree.
1154 * @parent: The dentry that will be the parent of @child.
1155 * @child: The dentry to link.
1157 * Returns NULL if successful. If @parent already contains a dentry with the
1158 * same case-sensitive name as @child, the pointer to this duplicate dentry is
1162 dentry_add_child(struct wim_dentry * restrict parent,
1163 struct wim_dentry * restrict child)
1165 struct rb_root *root;
1166 struct rb_node **new;
1167 struct rb_node *rb_parent;
1169 wimlib_assert(dentry_is_directory(parent));
1170 wimlib_assert(parent != child);
1172 /* Case sensitive child dentry index */
1173 root = &parent->d_inode->i_children;
1174 new = &root->rb_node;
1177 struct wim_dentry *this = rbnode_dentry(*new);
1178 int result = dentry_compare_names_case_sensitive(child, this);
1183 new = &((*new)->rb_left);
1184 else if (result > 0)
1185 new = &((*new)->rb_right);
1189 child->parent = parent;
1190 rb_link_node(&child->rb_node, rb_parent, new);
1191 rb_insert_color(&child->rb_node, root);
1195 struct wim_dentry *existing;
1196 existing = dentry_add_child_case_insensitive(parent, child);
1198 list_add(&child->case_insensitive_conflict_list,
1199 &existing->case_insensitive_conflict_list);
1200 child->rb_node_case_insensitive.__rb_parent_color = 0;
1202 INIT_LIST_HEAD(&child->case_insensitive_conflict_list);
1209 /* Unlink a WIM dentry from the directory entry tree. */
1211 unlink_dentry(struct wim_dentry *dentry)
1213 struct wim_dentry *parent = dentry->parent;
1215 if (parent == dentry)
1217 rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1219 if (dentry->rb_node_case_insensitive.__rb_parent_color) {
1220 /* This dentry was in the case-insensitive red-black tree. */
1221 rb_erase(&dentry->rb_node_case_insensitive,
1222 &parent->d_inode->i_children_case_insensitive);
1223 if (!list_empty(&dentry->case_insensitive_conflict_list)) {
1224 /* Make a different case-insensitively-the-same dentry
1225 * be the "representative" in the red-black tree. */
1226 struct list_head *next;
1227 struct wim_dentry *other;
1228 struct wim_dentry *existing;
1230 next = dentry->case_insensitive_conflict_list.next;
1231 other = list_entry(next, struct wim_dentry, case_insensitive_conflict_list);
1232 existing = dentry_add_child_case_insensitive(parent, other);
1233 wimlib_assert(existing == NULL);
1236 list_del(&dentry->case_insensitive_conflict_list);
1241 * Returns the alternate data stream entry belonging to @inode that has the
1242 * stream name @stream_name.
1244 struct wim_ads_entry *
1245 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1248 if (inode->i_num_ads == 0) {
1251 size_t stream_name_utf16le_nbytes;
1253 struct wim_ads_entry *result;
1255 #if TCHAR_IS_UTF16LE
1256 const utf16lechar *stream_name_utf16le;
1258 stream_name_utf16le = stream_name;
1259 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1261 utf16lechar *stream_name_utf16le;
1264 int ret = tstr_to_utf16le(stream_name,
1265 tstrlen(stream_name) *
1267 &stream_name_utf16le,
1268 &stream_name_utf16le_nbytes);
1276 if (ads_entry_has_name(&inode->i_ads_entries[i],
1277 stream_name_utf16le,
1278 stream_name_utf16le_nbytes))
1282 result = &inode->i_ads_entries[i];
1285 } while (++i != inode->i_num_ads);
1286 #if !TCHAR_IS_UTF16LE
1287 FREE(stream_name_utf16le);
1293 static struct wim_ads_entry *
1294 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1295 size_t stream_name_nbytes, bool is_utf16le)
1298 struct wim_ads_entry *ads_entries;
1299 struct wim_ads_entry *new_entry;
1301 if (inode->i_num_ads >= 0xfffe) {
1302 ERROR("Too many alternate data streams in one inode!");
1305 num_ads = inode->i_num_ads + 1;
1306 ads_entries = REALLOC(inode->i_ads_entries,
1307 num_ads * sizeof(inode->i_ads_entries[0]));
1309 ERROR("Failed to allocate memory for new alternate data stream");
1312 inode->i_ads_entries = ads_entries;
1314 new_entry = &inode->i_ads_entries[num_ads - 1];
1315 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1317 new_entry->stream_id = inode->i_next_stream_id++;
1318 inode->i_num_ads = num_ads;
1322 struct wim_ads_entry *
1323 inode_add_ads_utf16le(struct wim_inode *inode,
1324 const utf16lechar *stream_name,
1325 size_t stream_name_nbytes)
1327 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1328 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1332 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1333 * NULL if memory could not be allocated.
1335 struct wim_ads_entry *
1336 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1338 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1339 return do_inode_add_ads(inode, stream_name,
1340 tstrlen(stream_name) * sizeof(tchar),
1344 static struct wim_lookup_table_entry *
1345 add_stream_from_data_buffer(const void *buffer, size_t size,
1346 struct wim_lookup_table *lookup_table)
1348 u8 hash[SHA1_HASH_SIZE];
1349 struct wim_lookup_table_entry *lte, *existing_lte;
1351 sha1_buffer(buffer, size, hash);
1352 existing_lte = __lookup_resource(lookup_table, hash);
1354 wimlib_assert(wim_resource_size(existing_lte) == size);
1359 lte = new_lookup_table_entry();
1362 buffer_copy = memdup(buffer, size);
1364 free_lookup_table_entry(lte);
1367 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1368 lte->attached_buffer = buffer_copy;
1369 lte->resource_entry.original_size = size;
1370 copy_hash(lte->hash, hash);
1371 lookup_table_insert(lookup_table, lte);
1377 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1378 const void *value, size_t size,
1379 struct wim_lookup_table *lookup_table)
1381 struct wim_ads_entry *new_ads_entry;
1383 wimlib_assert(inode->i_resolved);
1385 new_ads_entry = inode_add_ads(inode, name);
1387 return WIMLIB_ERR_NOMEM;
1389 new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1391 if (!new_ads_entry->lte) {
1392 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1394 return WIMLIB_ERR_NOMEM;
1399 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
1400 * stream contents. */
1402 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
1403 struct wim_lookup_table *lookup_table)
1405 inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
1407 return WIMLIB_ERR_NOMEM;
1408 inode->i_resolved = 1;
1412 /* Remove an alternate data stream from a WIM inode */
1414 inode_remove_ads(struct wim_inode *inode, u16 idx,
1415 struct wim_lookup_table *lookup_table)
1417 struct wim_ads_entry *ads_entry;
1418 struct wim_lookup_table_entry *lte;
1420 wimlib_assert(idx < inode->i_num_ads);
1421 wimlib_assert(inode->i_resolved);
1423 ads_entry = &inode->i_ads_entries[idx];
1425 DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1427 lte = ads_entry->lte;
1429 lte_decrement_refcnt(lte, lookup_table);
1431 destroy_ads_entry(ads_entry);
1433 memmove(&inode->i_ads_entries[idx],
1434 &inode->i_ads_entries[idx + 1],
1435 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1441 inode_get_unix_data(const struct wim_inode *inode,
1442 struct wimlib_unix_data *unix_data,
1443 u16 *stream_idx_ret)
1445 const struct wim_ads_entry *ads_entry;
1446 const struct wim_lookup_table_entry *lte;
1450 wimlib_assert(inode->i_resolved);
1452 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1453 WIMLIB_UNIX_DATA_TAG, NULL);
1455 return NO_UNIX_DATA;
1458 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1460 lte = ads_entry->lte;
1462 return NO_UNIX_DATA;
1464 size = wim_resource_size(lte);
1465 if (size != sizeof(struct wimlib_unix_data))
1466 return BAD_UNIX_DATA;
1468 ret = read_full_resource_into_buf(lte, unix_data);
1472 if (unix_data->version != 0)
1473 return BAD_UNIX_DATA;
1478 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1479 struct wim_lookup_table *lookup_table, int which)
1481 struct wimlib_unix_data unix_data;
1483 bool have_good_unix_data = false;
1484 bool have_unix_data = false;
1487 if (!(which & UNIX_DATA_CREATE)) {
1488 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1489 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1490 have_unix_data = true;
1492 have_good_unix_data = true;
1494 unix_data.version = 0;
1495 if (which & UNIX_DATA_UID || !have_good_unix_data)
1496 unix_data.uid = uid;
1497 if (which & UNIX_DATA_GID || !have_good_unix_data)
1498 unix_data.gid = gid;
1499 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1500 unix_data.mode = mode;
1501 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1503 sizeof(struct wimlib_unix_data),
1505 if (ret == 0 && have_unix_data)
1506 inode_remove_ads(inode, stream_idx, lookup_table);
1509 #endif /* !__WIN32__ */
1512 * Reads the alternate data stream entries of a WIM dentry.
1514 * @p: Pointer to buffer that starts with the first alternate stream entry.
1516 * @inode: Inode to load the alternate data streams into.
1517 * @inode->i_num_ads must have been set to the number of
1518 * alternate data streams that are expected.
1520 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1524 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1525 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1526 * failure, @inode is not modified.
1529 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1530 size_t nbytes_remaining)
1533 struct wim_ads_entry *ads_entries;
1536 BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
1538 /* Allocate an array for our in-memory representation of the alternate
1539 * data stream entries. */
1540 num_ads = inode->i_num_ads;
1541 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1545 /* Read the entries into our newly allocated buffer. */
1546 for (u16 i = 0; i < num_ads; i++) {
1548 struct wim_ads_entry *cur_entry;
1549 const struct wim_ads_entry_on_disk *disk_entry =
1550 (const struct wim_ads_entry_on_disk*)p;
1552 cur_entry = &ads_entries[i];
1553 ads_entries[i].stream_id = i + 1;
1555 /* Do we have at least the size of the fixed-length data we know
1557 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1560 /* Read the length field */
1561 length = le64_to_cpu(disk_entry->length);
1563 /* Make sure the length field is neither so small it doesn't
1564 * include all the fixed-length data nor so large it overflows
1565 * the metadata resource buffer. */
1566 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1567 length > nbytes_remaining)
1570 /* Read the rest of the fixed-length data. */
1572 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1573 copy_hash(cur_entry->hash, disk_entry->hash);
1574 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1576 /* If stream_name_nbytes != 0, this is a named stream.
1577 * Otherwise this is an unnamed stream, or in some cases (bugs
1578 * in Microsoft's software I guess) a meaningless entry
1579 * distinguished from the real unnamed stream entry, if any, by
1580 * the fact that the real unnamed stream entry has a nonzero
1582 if (cur_entry->stream_name_nbytes) {
1583 /* The name is encoded in UTF16-LE, which uses 2-byte
1584 * coding units, so the length of the name had better be
1585 * an even number of bytes... */
1586 if (cur_entry->stream_name_nbytes & 1)
1589 /* Add the length of the stream name to get the length
1590 * we actually need to read. Make sure this isn't more
1591 * than the specified length of the entry. */
1592 if (sizeof(struct wim_ads_entry_on_disk) +
1593 cur_entry->stream_name_nbytes > length)
1596 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1597 if (!cur_entry->stream_name)
1600 memcpy(cur_entry->stream_name,
1601 disk_entry->stream_name,
1602 cur_entry->stream_name_nbytes);
1603 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1606 /* It's expected that the size of every ADS entry is a multiple
1607 * of 8. However, to be safe, I'm allowing the possibility of
1608 * an ADS entry at the very end of the metadata resource ending
1609 * un-aligned. So although we still need to increment the input
1610 * pointer by @length to reach the next ADS entry, it's possible
1611 * that less than @length is actually remaining in the metadata
1612 * resource. We should set the remaining bytes to 0 if this
1614 length = (length + 7) & ~(u64)7;
1616 if (nbytes_remaining < length)
1617 nbytes_remaining = 0;
1619 nbytes_remaining -= length;
1621 inode->i_ads_entries = ads_entries;
1622 inode->i_next_stream_id = inode->i_num_ads + 1;
1626 ret = WIMLIB_ERR_NOMEM;
1627 goto out_free_ads_entries;
1629 ERROR("An alternate data stream entry is invalid");
1630 ret = WIMLIB_ERR_INVALID_DENTRY;
1631 out_free_ads_entries:
1633 for (u16 i = 0; i < num_ads; i++)
1634 destroy_ads_entry(&ads_entries[i]);
1642 * Reads a WIM directory entry, including all alternate data stream entries that
1643 * follow it, from the WIM image's metadata resource.
1645 * @metadata_resource:
1646 * Pointer to the metadata resource buffer.
1648 * @metadata_resource_len:
1649 * Length of the metadata resource buffer, in bytes.
1651 * @offset: Offset of the dentry within the metadata resource.
1653 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1655 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1656 * been modified, but it will not be left with pointers to any allocated
1657 * buffers. On success, the dentry->length field must be examined. If zero,
1658 * this was a special "end of directory" dentry and not a real dentry. If
1659 * nonzero, this was a real dentry.
1661 * Possible errors include:
1663 * WIMLIB_ERR_INVALID_DENTRY
1666 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1667 u64 offset, struct wim_dentry * restrict dentry)
1670 u64 calculated_size;
1671 utf16lechar *file_name;
1672 utf16lechar *short_name;
1673 u16 short_name_nbytes;
1674 u16 file_name_nbytes;
1676 struct wim_inode *inode;
1677 const u8 *p = &metadata_resource[offset];
1678 const struct wim_dentry_on_disk *disk_dentry =
1679 (const struct wim_dentry_on_disk*)p;
1681 BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE);
1683 if ((uintptr_t)p & 7)
1684 WARNING("WIM dentry is not 8-byte aligned");
1686 dentry_common_init(dentry);
1688 /* Before reading the whole dentry, we need to read just the length.
1689 * This is because a dentry of length 8 (that is, just the length field)
1690 * terminates the list of sibling directory entries. */
1691 if (offset + sizeof(u64) > metadata_resource_len ||
1692 offset + sizeof(u64) < offset)
1694 ERROR("Directory entry starting at %"PRIu64" ends past the "
1695 "end of the metadata resource (size %"PRIu64")",
1696 offset, metadata_resource_len);
1697 return WIMLIB_ERR_INVALID_DENTRY;
1699 dentry->length = le64_to_cpu(disk_dentry->length);
1701 /* A zero length field (really a length of 8, since that's how big the
1702 * directory entry is...) indicates that this is the end of directory
1703 * dentry. We do not read it into memory as an actual dentry, so just
1704 * return successfully in this case. */
1705 if (dentry->length == 8)
1707 if (dentry->length == 0)
1710 /* Now that we have the actual length provided in the on-disk structure,
1711 * again make sure it doesn't overflow the metadata resource buffer. */
1712 if (offset + dentry->length > metadata_resource_len ||
1713 offset + dentry->length < offset)
1715 ERROR("Directory entry at offset %"PRIu64" and with size "
1716 "%"PRIu64" ends past the end of the metadata resource "
1718 offset, dentry->length, metadata_resource_len);
1719 return WIMLIB_ERR_INVALID_DENTRY;
1722 /* Make sure the dentry length is at least as large as the number of
1723 * fixed-length fields */
1724 if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1725 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1727 return WIMLIB_ERR_INVALID_DENTRY;
1730 /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1731 inode = new_timeless_inode();
1733 return WIMLIB_ERR_NOMEM;
1735 /* Read more fields; some into the dentry, and some into the inode. */
1737 inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1738 inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1739 dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1740 dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1741 dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1742 inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1743 inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1744 inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1745 copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1747 /* I don't know what's going on here. It seems like M$ screwed up the
1748 * reparse points, then put the fields in the same place and didn't
1749 * document it. So we have some fields we read for reparse points, and
1750 * some fields in the same place for non-reparse-point.s */
1751 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1752 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1753 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1754 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1755 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1756 /* Leave inode->i_ino at 0. Note that this means the WIM file
1757 * cannot archive hard-linked reparse points. Such a thing
1758 * doesn't really make sense anyway, although I believe it's
1759 * theoretically possible to have them on NTFS. */
1761 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1762 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1765 inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1767 short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1768 file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1770 if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1772 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1773 ret = WIMLIB_ERR_INVALID_DENTRY;
1774 goto out_free_inode;
1777 /* We now know the length of the file name and short name. Make sure
1778 * the length of the dentry is large enough to actually hold them.
1780 * The calculated length here is unaligned to allow for the possibility
1781 * that the dentry->length names an unaligned length, although this
1782 * would be unexpected. */
1783 calculated_size = _dentry_correct_length_unaligned(file_name_nbytes,
1786 if (dentry->length < calculated_size) {
1787 ERROR("Unexpected end of directory entry! (Expected "
1788 "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1789 calculated_size, dentry->length);
1790 ret = WIMLIB_ERR_INVALID_DENTRY;
1791 goto out_free_inode;
1794 p += sizeof(struct wim_dentry_on_disk);
1796 /* Read the filename if present. Note: if the filename is empty, there
1797 * is no null terminator following it. */
1798 if (file_name_nbytes) {
1799 file_name = MALLOC(file_name_nbytes + 2);
1801 ERROR("Failed to allocate %d bytes for dentry file name",
1802 file_name_nbytes + 2);
1803 ret = WIMLIB_ERR_NOMEM;
1804 goto out_free_inode;
1806 memcpy(file_name, p, file_name_nbytes);
1807 p += file_name_nbytes + 2;
1808 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
1814 /* Read the short filename if present. Note: if there is no short
1815 * filename, there is no null terminator following it. */
1816 if (short_name_nbytes) {
1817 short_name = MALLOC(short_name_nbytes + 2);
1819 ERROR("Failed to allocate %d bytes for dentry short name",
1820 short_name_nbytes + 2);
1821 ret = WIMLIB_ERR_NOMEM;
1822 goto out_free_file_name;
1824 memcpy(short_name, p, short_name_nbytes);
1825 p += short_name_nbytes + 2;
1826 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
1831 /* Align the dentry length */
1832 dentry->length = (dentry->length + 7) & ~7;
1835 * Read the alternate data streams, if present. dentry->num_ads tells
1836 * us how many they are, and they will directly follow the dentry
1839 * Note that each alternate data stream entry begins on an 8-byte
1840 * aligned boundary, and the alternate data stream entries seem to NOT
1841 * be included in the dentry->length field for some reason.
1843 if (inode->i_num_ads != 0) {
1844 ret = WIMLIB_ERR_INVALID_DENTRY;
1845 if (offset + dentry->length > metadata_resource_len ||
1846 (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
1848 metadata_resource_len - offset - dentry->length)))
1850 ERROR("Failed to read alternate data stream "
1851 "entries of WIM dentry \"%"WS"\"", file_name);
1852 goto out_free_short_name;
1855 /* We've read all the data for this dentry. Set the names and their
1856 * lengths, and we've done. */
1857 dentry->d_inode = inode;
1858 dentry->file_name = file_name;
1859 dentry->short_name = short_name;
1860 dentry->file_name_nbytes = file_name_nbytes;
1861 dentry->short_name_nbytes = short_name_nbytes;
1864 out_free_short_name:
1874 static const tchar *
1875 dentry_get_file_type_string(const struct wim_dentry *dentry)
1877 const struct wim_inode *inode = dentry->d_inode;
1878 if (inode_is_directory(inode))
1879 return T("directory");
1880 else if (inode_is_symlink(inode))
1881 return T("symbolic link");
1886 /* Reads the children of a dentry, and all their children, ..., etc. from the
1887 * metadata resource and into the dentry tree.
1889 * @metadata_resource: An array that contains the uncompressed metadata
1890 * resource for the WIM file.
1892 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1895 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
1896 * tree and has already been read from the metadata resource. It
1897 * does not need to be the real root because this procedure is
1898 * called recursively.
1900 * Returns zero on success; nonzero on failure.
1903 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1904 struct wim_dentry *dentry)
1906 u64 cur_offset = dentry->subdir_offset;
1907 struct wim_dentry *child;
1908 struct wim_dentry *duplicate;
1909 struct wim_dentry *parent;
1910 struct wim_dentry cur_child;
1914 * If @dentry has no child dentries, nothing more needs to be done for
1915 * this branch. This is the case for regular files, symbolic links, and
1916 * *possibly* empty directories (although an empty directory may also
1917 * have one child dentry that is the special end-of-directory dentry)
1919 if (cur_offset == 0)
1922 /* Check for cyclic directory structure */
1923 for (parent = dentry->parent; !dentry_is_root(parent); parent = parent->parent)
1925 if (unlikely(parent->subdir_offset == cur_offset)) {
1926 ERROR("Cyclic directory structure directed: children "
1927 "of \"%"TS"\" coincide with children of \"%"TS"\"",
1928 dentry_full_path(dentry),
1929 dentry_full_path(parent));
1930 return WIMLIB_ERR_INVALID_DENTRY;
1934 /* Find and read all the children of @dentry. */
1937 /* Read next child of @dentry into @cur_child. */
1938 ret = read_dentry(metadata_resource, metadata_resource_len,
1939 cur_offset, &cur_child);
1943 /* Check for end of directory. */
1944 if (cur_child.length == 0)
1947 /* Not end of directory. Allocate this child permanently and
1948 * link it to the parent and previous child. */
1949 child = memdup(&cur_child, sizeof(struct wim_dentry));
1951 ERROR("Failed to allocate new dentry!");
1952 ret = WIMLIB_ERR_NOMEM;
1956 /* Advance to the offset of the next child. Note: We need to
1957 * advance by the TOTAL length of the dentry, not by the length
1958 * cur_child.length, which although it does take into account
1959 * the padding, it DOES NOT take into account alternate stream
1961 cur_offset += dentry_total_length(child);
1963 if (unlikely(!dentry_has_long_name(child))) {
1964 WARNING("Ignoring unnamed dentry in "
1965 "directory \"%"TS"\"",
1966 dentry_full_path(dentry));
1971 duplicate = dentry_add_child(dentry, child);
1972 if (unlikely(duplicate)) {
1973 const tchar *child_type, *duplicate_type;
1974 child_type = dentry_get_file_type_string(child);
1975 duplicate_type = dentry_get_file_type_string(duplicate);
1976 WARNING("Ignoring duplicate %"TS" \"%"TS"\" "
1977 "(the WIM image already contains a %"TS" "
1978 "at that path with the exact same name)",
1979 child_type, dentry_full_path(duplicate),
1985 inode_add_dentry(child, child->d_inode);
1986 /* If there are children of this child, call this
1987 * procedure recursively. */
1988 if (child->subdir_offset != 0) {
1989 if (likely(dentry_is_directory(child))) {
1990 ret = read_dentry_tree(metadata_resource,
1991 metadata_resource_len,
1996 WARNING("Ignoring children of non-directory \"%"TS"\"",
1997 dentry_full_path(child));
2005 * Writes a WIM dentry to an output buffer.
2007 * @dentry: The dentry structure.
2008 * @p: The memory location to write the data to.
2010 * Returns the pointer to the byte after the last byte we wrote as part of the
2011 * dentry, including any alternate data stream entries.
2014 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
2016 const struct wim_inode *inode;
2017 struct wim_dentry_on_disk *disk_dentry;
2021 wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
2024 inode = dentry->d_inode;
2025 disk_dentry = (struct wim_dentry_on_disk*)p;
2027 disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
2028 disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
2029 disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
2030 disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
2031 disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
2032 disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
2033 disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
2034 disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
2035 hash = inode_stream_hash(inode, 0);
2036 copy_hash(disk_dentry->unnamed_stream_hash, hash);
2037 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2038 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2039 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
2040 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
2041 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
2043 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2044 disk_dentry->nonreparse.hard_link_group_id =
2045 cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
2047 disk_dentry->num_alternate_data_streams = cpu_to_le16(inode->i_num_ads);
2048 disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
2049 disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
2050 p += sizeof(struct wim_dentry_on_disk);
2052 wimlib_assert(dentry_is_root(dentry) != dentry_has_long_name(dentry));
2054 if (dentry_has_long_name(dentry))
2055 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
2057 if (dentry_has_short_name(dentry))
2058 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
2060 /* Align to 8-byte boundary */
2061 while ((uintptr_t)p & 7)
2064 /* We calculate the correct length of the dentry ourselves because the
2065 * dentry->length field may been set to an unexpected value from when we
2066 * read the dentry in (for example, there may have been unknown data
2067 * appended to the end of the dentry...). Furthermore, the dentry may
2068 * have been renamed, thus changing its needed length. */
2069 disk_dentry->length = cpu_to_le64(p - orig_p);
2071 /* Write the alternate data streams entries, if any. */
2072 for (u16 i = 0; i < inode->i_num_ads; i++) {
2073 const struct wim_ads_entry *ads_entry =
2074 &inode->i_ads_entries[i];
2075 struct wim_ads_entry_on_disk *disk_ads_entry =
2076 (struct wim_ads_entry_on_disk*)p;
2079 disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
2081 hash = inode_stream_hash(inode, i + 1);
2082 copy_hash(disk_ads_entry->hash, hash);
2083 disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
2084 p += sizeof(struct wim_ads_entry_on_disk);
2085 if (ads_entry->stream_name_nbytes) {
2086 p = mempcpy(p, ads_entry->stream_name,
2087 ads_entry->stream_name_nbytes + 2);
2089 /* Align to 8-byte boundary */
2090 while ((uintptr_t)p & 7)
2092 disk_ads_entry->length = cpu_to_le64(p - orig_p);
2098 write_dentry_cb(struct wim_dentry *dentry, void *_p)
2101 *p = write_dentry(dentry, *p);
2106 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
2109 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
2112 *p = write_dentry_tree_recursive(dentry, *p);
2116 /* Recursive function that writes a dentry tree rooted at @parent, not including
2117 * @parent itself, which has already been written. */
2119 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
2121 /* Nothing to do if this dentry has no children. */
2122 if (parent->subdir_offset == 0)
2125 /* Write child dentries and end-of-directory entry.
2127 * Note: we need to write all of this dentry's children before
2128 * recursively writing the directory trees rooted at each of the child
2129 * dentries, since the on-disk dentries for a dentry's children are
2130 * always located at consecutive positions in the metadata resource! */
2131 for_dentry_child(parent, write_dentry_cb, &p);
2133 /* write end of directory entry */
2134 *(le64*)p = cpu_to_le64(0);
2137 /* Recurse on children. */
2138 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
2142 /* Writes a directory tree to the metadata resource.
2144 * @root: Root of the dentry tree.
2145 * @p: Pointer to a buffer with enough space for the dentry tree.
2147 * Returns pointer to the byte after the last byte we wrote.
2150 write_dentry_tree(const struct wim_dentry *root, u8 *p)
2152 DEBUG("Writing dentry tree.");
2153 wimlib_assert(dentry_is_root(root));
2155 /* If we're the root dentry, we have no parent that already
2156 * wrote us, so we need to write ourselves. */
2157 p = write_dentry(root, p);
2159 /* Write end of directory entry after the root dentry just to be safe;
2160 * however the root dentry obviously cannot have any siblings. */
2161 *(le64*)p = cpu_to_le64(0);
2164 /* Recursively write the rest of the dentry tree. */
2165 return write_dentry_tree_recursive(root, p);