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 full_path = TSTRDUP(T("/"));
354 return WIMLIB_ERR_NOMEM;
355 full_path_nbytes = 1 * sizeof(tchar);
357 struct wim_dentry *parent;
358 tchar *parent_full_path;
359 u32 parent_full_path_nbytes;
360 size_t filename_nbytes;
362 parent = dentry->parent;
363 if (dentry_is_root(parent)) {
364 parent_full_path = T("");
365 parent_full_path_nbytes = 0;
367 if (!parent->_full_path) {
368 ret = calculate_dentry_full_path(parent);
372 parent_full_path = parent->_full_path;
373 parent_full_path_nbytes = parent->full_path_nbytes;
376 /* Append this dentry's name as a tchar string to the full path
377 * of the parent followed by the path separator */
379 filename_nbytes = dentry->file_name_nbytes;
382 int ret = utf16le_to_tstr_nbytes(dentry->file_name,
383 dentry->file_name_nbytes,
390 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
392 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
394 return WIMLIB_ERR_NOMEM;
395 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
396 full_path[parent_full_path_nbytes / sizeof(tchar)] = T('/');
398 memcpy(&full_path[parent_full_path_nbytes / sizeof(tchar) + 1],
400 filename_nbytes + sizeof(tchar));
402 utf16le_to_tstr_buf(dentry->file_name,
403 dentry->file_name_nbytes,
404 &full_path[parent_full_path_nbytes /
408 dentry->_full_path = full_path;
409 dentry->full_path_nbytes= full_path_nbytes;
414 do_calculate_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
416 return calculate_dentry_full_path(dentry);
420 calculate_dentry_tree_full_paths(struct wim_dentry *root)
422 return for_dentry_in_tree(root, do_calculate_dentry_full_path, NULL);
426 dentry_full_path(struct wim_dentry *dentry)
428 calculate_dentry_full_path(dentry);
429 return dentry->_full_path;
433 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
435 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
440 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
442 calculate_subdir_offsets(dentry, subdir_offset_p);
447 * Recursively calculates the subdir offsets for a directory tree.
449 * @dentry: The root of the directory tree.
450 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
451 * offset for @dentry.
454 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
456 struct rb_node *node;
458 dentry->subdir_offset = *subdir_offset_p;
459 node = dentry->d_inode->i_children.rb_node;
461 /* Advance the subdir offset by the amount of space the children
462 * of this dentry take up. */
463 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
465 /* End-of-directory dentry on disk. */
466 *subdir_offset_p += 8;
468 /* Recursively call calculate_subdir_offsets() on all the
470 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
472 /* On disk, childless directories have a valid subdir_offset
473 * that points to an 8-byte end-of-directory dentry. Regular
474 * files or reparse points have a subdir_offset of 0. */
475 if (dentry_is_directory(dentry))
476 *subdir_offset_p += 8;
478 dentry->subdir_offset = 0;
482 /* Case-sensitive UTF-16LE dentry or stream name comparison. Used on both UNIX
483 * (always) and Windows (sometimes) */
485 compare_utf16le_names_case_sensitive(const utf16lechar *name1, size_t nbytes1,
486 const utf16lechar *name2, size_t nbytes2)
488 /* Return the result if the strings differ up to their minimum length.
489 * Note that we cannot use strcmp() or strncmp() here, as the strings
490 * are in UTF-16LE format. */
491 int result = memcmp(name1, name2, min(nbytes1, nbytes2));
495 /* The strings are the same up to their minimum length, so return a
496 * result based on their lengths. */
497 if (nbytes1 < nbytes2)
499 else if (nbytes1 > nbytes2)
506 /* Windoze: Case-insensitive UTF-16LE dentry or stream name comparison */
508 compare_utf16le_names_case_insensitive(const utf16lechar *name1, size_t nbytes1,
509 const utf16lechar *name2, size_t nbytes2)
511 /* Return the result if the strings differ up to their minimum length.
513 int result = _wcsnicmp((const wchar_t*)name1, (const wchar_t*)name2,
514 min(nbytes1 / 2, nbytes2 / 2));
518 /* The strings are the same up to their minimum length, so return a
519 * result based on their lengths. */
520 if (nbytes1 < nbytes2)
522 else if (nbytes1 > nbytes2)
527 #endif /* __WIN32__ */
530 # define compare_utf16le_names compare_utf16le_names_case_insensitive
532 # define compare_utf16le_names compare_utf16le_names_case_sensitive
538 dentry_compare_names_case_insensitive(const struct wim_dentry *d1,
539 const struct wim_dentry *d2)
541 return compare_utf16le_names_case_insensitive(d1->file_name,
542 d1->file_name_nbytes,
544 d2->file_name_nbytes);
546 #endif /* __WIN32__ */
549 dentry_compare_names_case_sensitive(const struct wim_dentry *d1,
550 const struct wim_dentry *d2)
552 return compare_utf16le_names_case_sensitive(d1->file_name,
553 d1->file_name_nbytes,
555 d2->file_name_nbytes);
559 # define dentry_compare_names dentry_compare_names_case_insensitive
561 # define dentry_compare_names dentry_compare_names_case_sensitive
564 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
565 * stream name @name that has length @name_nbytes bytes. */
567 ads_entry_has_name(const struct wim_ads_entry *entry,
568 const utf16lechar *name, size_t name_nbytes)
570 return !compare_utf16le_names(name, name_nbytes,
572 entry->stream_name_nbytes);
576 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
577 const utf16lechar *name,
580 struct rb_node *node = dentry->d_inode->i_children.rb_node;
581 struct wim_dentry *child;
583 child = rbnode_dentry(node);
584 int result = compare_utf16le_names(name, name_nbytes,
586 child->file_name_nbytes);
588 node = node->rb_left;
590 node = node->rb_right;
593 if (!list_empty(&child->case_insensitive_conflict_list))
595 WARNING("Result of case-insensitive lookup is ambiguous "
596 "(returning \"%ls\" instead of \"%ls\")",
598 container_of(child->case_insensitive_conflict_list.next,
600 case_insensitive_conflict_list)->file_name);
609 /* Returns the child of @dentry that has the file name @name. Returns NULL if
610 * no child has the name. */
612 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
615 return get_dentry_child_with_utf16le_name(dentry, name,
616 tstrlen(name) * sizeof(tchar));
618 utf16lechar *utf16le_name;
619 size_t utf16le_name_nbytes;
621 struct wim_dentry *child;
623 ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
624 &utf16le_name, &utf16le_name_nbytes);
628 child = get_dentry_child_with_utf16le_name(dentry,
630 utf16le_name_nbytes);
637 static struct wim_dentry *
638 get_dentry_utf16le(WIMStruct *wim, const utf16lechar *path)
640 struct wim_dentry *cur_dentry, *parent_dentry;
641 const utf16lechar *p, *pp;
643 cur_dentry = parent_dentry = wim_root_dentry(wim);
650 while (*p == cpu_to_le16('/'))
652 if (*p == cpu_to_le16('\0'))
655 while (*pp != cpu_to_le16('/') && *pp != cpu_to_le16('\0'))
658 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
659 (void*)pp - (void*)p);
660 if (cur_dentry == NULL)
663 parent_dentry = cur_dentry;
665 if (cur_dentry == NULL) {
666 if (dentry_is_directory(parent_dentry))
674 /* Returns the dentry corresponding to the @path, or NULL if there is no such
677 get_dentry(WIMStruct *wim, const tchar *path)
680 return get_dentry_utf16le(wim, path);
682 utf16lechar *path_utf16le;
683 size_t path_utf16le_nbytes;
685 struct wim_dentry *dentry;
687 ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
688 &path_utf16le, &path_utf16le_nbytes);
691 dentry = get_dentry_utf16le(wim, path_utf16le);
698 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
700 struct wim_dentry *dentry;
701 dentry = get_dentry(wim, path);
703 return dentry->d_inode;
708 /* Takes in a path of length @len in @buf, and transforms it into a string for
709 * the path of its parent directory. */
711 to_parent_name(tchar *buf, size_t len)
713 ssize_t i = (ssize_t)len - 1;
714 while (i >= 0 && buf[i] == T('/'))
716 while (i >= 0 && buf[i] != T('/'))
718 while (i >= 0 && buf[i] == T('/'))
720 buf[i + 1] = T('\0');
723 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
724 * if the dentry is not found. */
726 get_parent_dentry(WIMStruct *wim, const tchar *path)
728 size_t path_len = tstrlen(path);
729 tchar buf[path_len + 1];
731 tmemcpy(buf, path, path_len + 1);
732 to_parent_name(buf, path_len);
733 return get_dentry(wim, buf);
736 /* Prints the full path of a dentry. */
738 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
740 int ret = calculate_dentry_full_path(dentry);
743 tprintf(T("%"TS"\n"), dentry->_full_path);
747 /* We want to be able to show the names of the file attribute flags that are
749 struct file_attr_flag {
753 struct file_attr_flag file_attr_flags[] = {
754 {FILE_ATTRIBUTE_READONLY, T("READONLY")},
755 {FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
756 {FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
757 {FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
758 {FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
759 {FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
760 {FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
761 {FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
762 {FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
763 {FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
764 {FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
765 {FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
766 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
767 {FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
768 {FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
771 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
772 * available. If the dentry is unresolved and the lookup table is NULL, the
773 * lookup table entries will not be printed. Otherwise, they will be. */
775 print_dentry(struct wim_dentry *dentry, void *lookup_table)
778 struct wim_lookup_table_entry *lte;
779 const struct wim_inode *inode = dentry->d_inode;
782 tprintf(T("[DENTRY]\n"));
783 tprintf(T("Length = %"PRIu64"\n"), dentry->length);
784 tprintf(T("Attributes = 0x%x\n"), inode->i_attributes);
785 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
786 if (file_attr_flags[i].flag & inode->i_attributes)
787 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
788 file_attr_flags[i].name);
789 tprintf(T("Security ID = %d\n"), inode->i_security_id);
790 tprintf(T("Subdir offset = %"PRIu64"\n"), dentry->subdir_offset);
792 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
793 tprintf(T("Creation Time = %"TS"\n"), buf);
795 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
796 tprintf(T("Last Access Time = %"TS"\n"), buf);
798 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
799 tprintf(T("Last Write Time = %"TS"\n"), buf);
801 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
802 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), inode->i_reparse_tag);
803 tprintf(T("Reparse Point Flags = 0x%"PRIx16"\n"),
804 inode->i_not_rpfixed);
805 tprintf(T("Reparse Point Unknown 2 = 0x%"PRIx32"\n"),
806 inode->i_rp_unknown_2);
808 tprintf(T("Reparse Point Unknown 1 = 0x%"PRIx32"\n"),
809 inode->i_rp_unknown_1);
810 tprintf(T("Hard Link Group = 0x%"PRIx64"\n"), inode->i_ino);
811 tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
812 tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
813 if (dentry_has_long_name(dentry))
814 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
815 if (dentry_has_short_name(dentry))
816 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
817 if (dentry->_full_path)
818 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
820 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
822 print_lookup_table_entry(lte, stdout);
824 hash = inode_stream_hash(inode, 0);
826 tprintf(T("Hash = 0x"));
827 print_hash(hash, stdout);
832 for (u16 i = 0; i < inode->i_num_ads; i++) {
833 tprintf(T("[Alternate Stream Entry %u]\n"), i);
834 wimlib_printf(T("Name = \"%"WS"\"\n"),
835 inode->i_ads_entries[i].stream_name);
836 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
837 inode->i_ads_entries[i].stream_name_nbytes);
838 hash = inode_stream_hash(inode, i + 1);
840 tprintf(T("Hash = 0x"));
841 print_hash(hash, stdout);
844 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
850 /* Initializations done on every `struct wim_dentry'. */
852 dentry_common_init(struct wim_dentry *dentry)
854 memset(dentry, 0, sizeof(struct wim_dentry));
858 new_timeless_inode(void)
860 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
862 inode->i_security_id = -1;
864 inode->i_next_stream_id = 1;
865 inode->i_not_rpfixed = 1;
866 INIT_LIST_HEAD(&inode->i_list);
868 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
869 ERROR_WITH_ERRNO("Error initializing mutex");
874 INIT_LIST_HEAD(&inode->i_dentry);
879 static struct wim_inode *
882 struct wim_inode *inode = new_timeless_inode();
884 u64 now = get_wim_timestamp();
885 inode->i_creation_time = now;
886 inode->i_last_access_time = now;
887 inode->i_last_write_time = now;
892 /* Creates an unlinked directory entry. */
894 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
896 struct wim_dentry *dentry;
899 dentry = MALLOC(sizeof(struct wim_dentry));
901 return WIMLIB_ERR_NOMEM;
903 dentry_common_init(dentry);
904 ret = set_dentry_name(dentry, name);
906 dentry->parent = dentry;
907 *dentry_ret = dentry;
910 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
918 _new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
921 struct wim_dentry *dentry;
924 ret = new_dentry(name, &dentry);
929 dentry->d_inode = new_timeless_inode();
931 dentry->d_inode = new_inode();
932 if (!dentry->d_inode) {
934 return WIMLIB_ERR_NOMEM;
937 inode_add_dentry(dentry, dentry->d_inode);
938 *dentry_ret = dentry;
943 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
945 return _new_dentry_with_inode(name, dentry_ret, true);
949 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
951 return _new_dentry_with_inode(name, dentry_ret, false);
955 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
958 struct wim_dentry *dentry;
960 DEBUG("Creating filler directory \"%"TS"\"", name);
961 ret = new_dentry_with_inode(name, &dentry);
964 /* Leave the inode number as 0; this is allowed for non
965 * hard-linked files. */
966 dentry->d_inode->i_resolved = 1;
967 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
968 *dentry_ret = dentry;
973 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
974 size_t name_nbytes, bool is_utf16le)
977 memset(ads_entry, 0, sizeof(*ads_entry));
980 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
982 return WIMLIB_ERR_NOMEM;
983 memcpy(p, name, name_nbytes);
984 p[name_nbytes / 2] = cpu_to_le16(0);
985 ads_entry->stream_name = p;
986 ads_entry->stream_name_nbytes = name_nbytes;
988 if (name && *(const tchar*)name != T('\0')) {
989 ret = get_utf16le_name(name, &ads_entry->stream_name,
990 &ads_entry->stream_name_nbytes);
997 destroy_ads_entry(struct wim_ads_entry *ads_entry)
999 FREE(ads_entry->stream_name);
1002 /* Frees an inode. */
1004 free_inode(struct wim_inode *inode)
1007 if (inode->i_ads_entries) {
1008 for (u16 i = 0; i < inode->i_num_ads; i++)
1009 destroy_ads_entry(&inode->i_ads_entries[i]);
1010 FREE(inode->i_ads_entries);
1013 wimlib_assert(inode->i_num_opened_fds == 0);
1015 pthread_mutex_destroy(&inode->i_mutex);
1017 /* HACK: This may instead delete the inode from i_list, but the
1018 * hlist_del() behaves the same as list_del(). */
1019 hlist_del(&inode->i_hlist);
1020 FREE(inode->i_extracted_file);
1025 /* Decrements link count on an inode and frees it if the link count reaches 0.
1028 put_inode(struct wim_inode *inode)
1030 wimlib_assert(inode->i_nlink != 0);
1031 if (--inode->i_nlink == 0) {
1033 if (inode->i_num_opened_fds == 0)
1041 /* Frees a WIM dentry.
1043 * The corresponding inode (if any) is freed only if its link count is
1047 free_dentry(struct wim_dentry *dentry)
1050 FREE(dentry->file_name);
1051 FREE(dentry->short_name);
1052 FREE(dentry->_full_path);
1053 if (dentry->d_inode)
1054 put_inode(dentry->d_inode);
1059 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
1060 * to free a directory tree. */
1062 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
1064 struct wim_lookup_table *lookup_table = _lookup_table;
1067 struct wim_inode *inode = dentry->d_inode;
1068 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1069 struct wim_lookup_table_entry *lte;
1071 lte = inode_stream_lte(inode, i, lookup_table);
1073 lte_decrement_refcnt(lte, lookup_table);
1076 free_dentry(dentry);
1081 * Unlinks and frees a dentry tree.
1083 * @root: The root of the tree.
1084 * @lookup_table: The lookup table for dentries. If non-NULL, the
1085 * reference counts in the lookup table for the lookup
1086 * table entries corresponding to the dentries will be
1090 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1092 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1096 * Links a dentry into the directory tree.
1098 * @parent: The dentry that will be the parent of @child.
1099 * @child: The dentry to link.
1101 * Returns NULL if successful. If @parent already contains a dentry with the
1102 * same case-sensitive name as @child, the pointer to this duplicate dentry is
1106 dentry_add_child(struct wim_dentry * restrict parent,
1107 struct wim_dentry * restrict child)
1109 struct rb_root *root;
1110 struct rb_node **new;
1111 struct rb_node *rb_parent;
1113 wimlib_assert(dentry_is_directory(parent));
1114 wimlib_assert(parent != child);
1116 /* Case sensitive child dentry index */
1117 root = &parent->d_inode->i_children;
1118 new = &root->rb_node;
1121 struct wim_dentry *this = rbnode_dentry(*new);
1122 int result = dentry_compare_names_case_sensitive(child, this);
1127 new = &((*new)->rb_left);
1128 else if (result > 0)
1129 new = &((*new)->rb_right);
1133 child->parent = parent;
1134 rb_link_node(&child->rb_node, rb_parent, new);
1135 rb_insert_color(&child->rb_node, root);
1138 /* Case insensitive child dentry index */
1139 root = &parent->d_inode->i_children_case_insensitive;
1140 new = &root->rb_node;
1143 struct wim_dentry *this = container_of(*new, struct wim_dentry,
1144 rb_node_case_insensitive);
1145 int result = dentry_compare_names_case_insensitive(child, this);
1150 new = &((*new)->rb_left);
1151 else if (result > 0)
1152 new = &((*new)->rb_right);
1154 list_add(&child->case_insensitive_conflict_list,
1155 &this->case_insensitive_conflict_list);
1160 rb_link_node(&child->rb_node_case_insensitive, rb_parent, new);
1161 rb_insert_color(&child->rb_node_case_insensitive, root);
1162 INIT_LIST_HEAD(&child->case_insensitive_conflict_list);
1167 /* Unlink a WIM dentry from the directory entry tree. */
1169 unlink_dentry(struct wim_dentry *dentry)
1171 if (!dentry_is_root(dentry)) {
1172 rb_erase(&dentry->rb_node, &dentry->parent->d_inode->i_children);
1174 rb_erase(&dentry->rb_node_case_insensitive,
1175 &dentry->parent->d_inode->i_children_case_insensitive);
1176 list_del(&dentry->case_insensitive_conflict_list);
1182 * Returns the alternate data stream entry belonging to @inode that has the
1183 * stream name @stream_name.
1185 struct wim_ads_entry *
1186 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1189 if (inode->i_num_ads == 0) {
1192 size_t stream_name_utf16le_nbytes;
1194 struct wim_ads_entry *result;
1196 #if TCHAR_IS_UTF16LE
1197 const utf16lechar *stream_name_utf16le;
1199 stream_name_utf16le = stream_name;
1200 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1202 utf16lechar *stream_name_utf16le;
1205 int ret = tstr_to_utf16le(stream_name,
1206 tstrlen(stream_name) *
1208 &stream_name_utf16le,
1209 &stream_name_utf16le_nbytes);
1217 if (ads_entry_has_name(&inode->i_ads_entries[i],
1218 stream_name_utf16le,
1219 stream_name_utf16le_nbytes))
1223 result = &inode->i_ads_entries[i];
1226 } while (++i != inode->i_num_ads);
1227 #if !TCHAR_IS_UTF16LE
1228 FREE(stream_name_utf16le);
1234 static struct wim_ads_entry *
1235 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1236 size_t stream_name_nbytes, bool is_utf16le)
1239 struct wim_ads_entry *ads_entries;
1240 struct wim_ads_entry *new_entry;
1242 if (inode->i_num_ads >= 0xfffe) {
1243 ERROR("Too many alternate data streams in one inode!");
1246 num_ads = inode->i_num_ads + 1;
1247 ads_entries = REALLOC(inode->i_ads_entries,
1248 num_ads * sizeof(inode->i_ads_entries[0]));
1250 ERROR("Failed to allocate memory for new alternate data stream");
1253 inode->i_ads_entries = ads_entries;
1255 new_entry = &inode->i_ads_entries[num_ads - 1];
1256 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1258 new_entry->stream_id = inode->i_next_stream_id++;
1259 inode->i_num_ads = num_ads;
1263 struct wim_ads_entry *
1264 inode_add_ads_utf16le(struct wim_inode *inode,
1265 const utf16lechar *stream_name,
1266 size_t stream_name_nbytes)
1268 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1269 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1273 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1274 * NULL if memory could not be allocated.
1276 struct wim_ads_entry *
1277 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1279 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1280 return do_inode_add_ads(inode, stream_name,
1281 tstrlen(stream_name) * sizeof(tchar),
1285 static struct wim_lookup_table_entry *
1286 add_stream_from_data_buffer(const void *buffer, size_t size,
1287 struct wim_lookup_table *lookup_table)
1289 u8 hash[SHA1_HASH_SIZE];
1290 struct wim_lookup_table_entry *lte, *existing_lte;
1292 sha1_buffer(buffer, size, hash);
1293 existing_lte = __lookup_resource(lookup_table, hash);
1295 wimlib_assert(wim_resource_size(existing_lte) == size);
1300 lte = new_lookup_table_entry();
1303 buffer_copy = memdup(buffer, size);
1305 free_lookup_table_entry(lte);
1308 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1309 lte->attached_buffer = buffer_copy;
1310 lte->resource_entry.original_size = size;
1311 copy_hash(lte->hash, hash);
1312 lookup_table_insert(lookup_table, lte);
1318 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1319 const void *value, size_t size,
1320 struct wim_lookup_table *lookup_table)
1322 struct wim_ads_entry *new_ads_entry;
1324 wimlib_assert(inode->i_resolved);
1326 new_ads_entry = inode_add_ads(inode, name);
1328 return WIMLIB_ERR_NOMEM;
1330 new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1332 if (!new_ads_entry->lte) {
1333 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1335 return WIMLIB_ERR_NOMEM;
1340 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
1341 * stream contents. */
1343 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
1344 struct wim_lookup_table *lookup_table)
1346 inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
1348 return WIMLIB_ERR_NOMEM;
1349 inode->i_resolved = 1;
1353 /* Remove an alternate data stream from a WIM inode */
1355 inode_remove_ads(struct wim_inode *inode, u16 idx,
1356 struct wim_lookup_table *lookup_table)
1358 struct wim_ads_entry *ads_entry;
1359 struct wim_lookup_table_entry *lte;
1361 wimlib_assert(idx < inode->i_num_ads);
1362 wimlib_assert(inode->i_resolved);
1364 ads_entry = &inode->i_ads_entries[idx];
1366 DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1368 lte = ads_entry->lte;
1370 lte_decrement_refcnt(lte, lookup_table);
1372 destroy_ads_entry(ads_entry);
1374 memmove(&inode->i_ads_entries[idx],
1375 &inode->i_ads_entries[idx + 1],
1376 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1382 inode_get_unix_data(const struct wim_inode *inode,
1383 struct wimlib_unix_data *unix_data,
1384 u16 *stream_idx_ret)
1386 const struct wim_ads_entry *ads_entry;
1387 const struct wim_lookup_table_entry *lte;
1391 wimlib_assert(inode->i_resolved);
1393 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1394 WIMLIB_UNIX_DATA_TAG, NULL);
1396 return NO_UNIX_DATA;
1399 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1401 lte = ads_entry->lte;
1403 return NO_UNIX_DATA;
1405 size = wim_resource_size(lte);
1406 if (size != sizeof(struct wimlib_unix_data))
1407 return BAD_UNIX_DATA;
1409 ret = read_full_resource_into_buf(lte, unix_data);
1413 if (unix_data->version != 0)
1414 return BAD_UNIX_DATA;
1419 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1420 struct wim_lookup_table *lookup_table, int which)
1422 struct wimlib_unix_data unix_data;
1424 bool have_good_unix_data = false;
1425 bool have_unix_data = false;
1428 if (!(which & UNIX_DATA_CREATE)) {
1429 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1430 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1431 have_unix_data = true;
1433 have_good_unix_data = true;
1435 unix_data.version = 0;
1436 if (which & UNIX_DATA_UID || !have_good_unix_data)
1437 unix_data.uid = uid;
1438 if (which & UNIX_DATA_GID || !have_good_unix_data)
1439 unix_data.gid = gid;
1440 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1441 unix_data.mode = mode;
1442 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1444 sizeof(struct wimlib_unix_data),
1446 if (ret == 0 && have_unix_data)
1447 inode_remove_ads(inode, stream_idx, lookup_table);
1450 #endif /* !__WIN32__ */
1453 * Reads the alternate data stream entries of a WIM dentry.
1455 * @p: Pointer to buffer that starts with the first alternate stream entry.
1457 * @inode: Inode to load the alternate data streams into.
1458 * @inode->i_num_ads must have been set to the number of
1459 * alternate data streams that are expected.
1461 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1465 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1466 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1467 * failure, @inode is not modified.
1470 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1471 size_t nbytes_remaining)
1474 struct wim_ads_entry *ads_entries;
1477 BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
1479 /* Allocate an array for our in-memory representation of the alternate
1480 * data stream entries. */
1481 num_ads = inode->i_num_ads;
1482 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1486 /* Read the entries into our newly allocated buffer. */
1487 for (u16 i = 0; i < num_ads; i++) {
1489 struct wim_ads_entry *cur_entry;
1490 const struct wim_ads_entry_on_disk *disk_entry =
1491 (const struct wim_ads_entry_on_disk*)p;
1493 cur_entry = &ads_entries[i];
1494 ads_entries[i].stream_id = i + 1;
1496 /* Do we have at least the size of the fixed-length data we know
1498 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1501 /* Read the length field */
1502 length = le64_to_cpu(disk_entry->length);
1504 /* Make sure the length field is neither so small it doesn't
1505 * include all the fixed-length data nor so large it overflows
1506 * the metadata resource buffer. */
1507 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1508 length > nbytes_remaining)
1511 /* Read the rest of the fixed-length data. */
1513 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1514 copy_hash(cur_entry->hash, disk_entry->hash);
1515 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1517 /* If stream_name_nbytes != 0, this is a named stream.
1518 * Otherwise this is an unnamed stream, or in some cases (bugs
1519 * in Microsoft's software I guess) a meaningless entry
1520 * distinguished from the real unnamed stream entry, if any, by
1521 * the fact that the real unnamed stream entry has a nonzero
1523 if (cur_entry->stream_name_nbytes) {
1524 /* The name is encoded in UTF16-LE, which uses 2-byte
1525 * coding units, so the length of the name had better be
1526 * an even number of bytes... */
1527 if (cur_entry->stream_name_nbytes & 1)
1530 /* Add the length of the stream name to get the length
1531 * we actually need to read. Make sure this isn't more
1532 * than the specified length of the entry. */
1533 if (sizeof(struct wim_ads_entry_on_disk) +
1534 cur_entry->stream_name_nbytes > length)
1537 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1538 if (!cur_entry->stream_name)
1541 memcpy(cur_entry->stream_name,
1542 disk_entry->stream_name,
1543 cur_entry->stream_name_nbytes);
1544 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1547 /* It's expected that the size of every ADS entry is a multiple
1548 * of 8. However, to be safe, I'm allowing the possibility of
1549 * an ADS entry at the very end of the metadata resource ending
1550 * un-aligned. So although we still need to increment the input
1551 * pointer by @length to reach the next ADS entry, it's possible
1552 * that less than @length is actually remaining in the metadata
1553 * resource. We should set the remaining bytes to 0 if this
1555 length = (length + 7) & ~(u64)7;
1557 if (nbytes_remaining < length)
1558 nbytes_remaining = 0;
1560 nbytes_remaining -= length;
1562 inode->i_ads_entries = ads_entries;
1563 inode->i_next_stream_id = inode->i_num_ads + 1;
1567 ret = WIMLIB_ERR_NOMEM;
1568 goto out_free_ads_entries;
1570 ERROR("An alternate data stream entry is invalid");
1571 ret = WIMLIB_ERR_INVALID_DENTRY;
1572 out_free_ads_entries:
1574 for (u16 i = 0; i < num_ads; i++)
1575 destroy_ads_entry(&ads_entries[i]);
1583 * Reads a WIM directory entry, including all alternate data stream entries that
1584 * follow it, from the WIM image's metadata resource.
1586 * @metadata_resource:
1587 * Pointer to the metadata resource buffer.
1589 * @metadata_resource_len:
1590 * Length of the metadata resource buffer, in bytes.
1592 * @offset: Offset of the dentry within the metadata resource.
1594 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1596 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1597 * been modified, but it will not be left with pointers to any allocated
1598 * buffers. On success, the dentry->length field must be examined. If zero,
1599 * this was a special "end of directory" dentry and not a real dentry. If
1600 * nonzero, this was a real dentry.
1602 * Possible errors include:
1604 * WIMLIB_ERR_INVALID_DENTRY
1607 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1608 u64 offset, struct wim_dentry * restrict dentry)
1611 u64 calculated_size;
1612 utf16lechar *file_name;
1613 utf16lechar *short_name;
1614 u16 short_name_nbytes;
1615 u16 file_name_nbytes;
1617 struct wim_inode *inode;
1618 const u8 *p = &metadata_resource[offset];
1619 const struct wim_dentry_on_disk *disk_dentry =
1620 (const struct wim_dentry_on_disk*)p;
1622 BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE);
1624 if ((uintptr_t)p & 7)
1625 WARNING("WIM dentry is not 8-byte aligned");
1627 dentry_common_init(dentry);
1629 /* Before reading the whole dentry, we need to read just the length.
1630 * This is because a dentry of length 8 (that is, just the length field)
1631 * terminates the list of sibling directory entries. */
1632 if (offset + sizeof(u64) > metadata_resource_len ||
1633 offset + sizeof(u64) < offset)
1635 ERROR("Directory entry starting at %"PRIu64" ends past the "
1636 "end of the metadata resource (size %"PRIu64")",
1637 offset, metadata_resource_len);
1638 return WIMLIB_ERR_INVALID_DENTRY;
1640 dentry->length = le64_to_cpu(disk_dentry->length);
1642 /* A zero length field (really a length of 8, since that's how big the
1643 * directory entry is...) indicates that this is the end of directory
1644 * dentry. We do not read it into memory as an actual dentry, so just
1645 * return successfully in this case. */
1646 if (dentry->length == 8)
1648 if (dentry->length == 0)
1651 /* Now that we have the actual length provided in the on-disk structure,
1652 * again make sure it doesn't overflow the metadata resource buffer. */
1653 if (offset + dentry->length > metadata_resource_len ||
1654 offset + dentry->length < offset)
1656 ERROR("Directory entry at offset %"PRIu64" and with size "
1657 "%"PRIu64" ends past the end of the metadata resource "
1659 offset, dentry->length, metadata_resource_len);
1660 return WIMLIB_ERR_INVALID_DENTRY;
1663 /* Make sure the dentry length is at least as large as the number of
1664 * fixed-length fields */
1665 if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1666 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1668 return WIMLIB_ERR_INVALID_DENTRY;
1671 /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1672 inode = new_timeless_inode();
1674 return WIMLIB_ERR_NOMEM;
1676 /* Read more fields; some into the dentry, and some into the inode. */
1678 inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1679 inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1680 dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1681 dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1682 dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1683 inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1684 inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1685 inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1686 copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1688 /* I don't know what's going on here. It seems like M$ screwed up the
1689 * reparse points, then put the fields in the same place and didn't
1690 * document it. So we have some fields we read for reparse points, and
1691 * some fields in the same place for non-reparse-point.s */
1692 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1693 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1694 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1695 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1696 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1697 /* Leave inode->i_ino at 0. Note that this means the WIM file
1698 * cannot archive hard-linked reparse points. Such a thing
1699 * doesn't really make sense anyway, although I believe it's
1700 * theoretically possible to have them on NTFS. */
1702 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1703 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1706 inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1708 short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1709 file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1711 if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1713 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1714 ret = WIMLIB_ERR_INVALID_DENTRY;
1715 goto out_free_inode;
1718 /* We now know the length of the file name and short name. Make sure
1719 * the length of the dentry is large enough to actually hold them.
1721 * The calculated length here is unaligned to allow for the possibility
1722 * that the dentry->length names an unaligned length, although this
1723 * would be unexpected. */
1724 calculated_size = _dentry_correct_length_unaligned(file_name_nbytes,
1727 if (dentry->length < calculated_size) {
1728 ERROR("Unexpected end of directory entry! (Expected "
1729 "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1730 calculated_size, dentry->length);
1731 ret = WIMLIB_ERR_INVALID_DENTRY;
1732 goto out_free_inode;
1735 p += sizeof(struct wim_dentry_on_disk);
1737 /* Read the filename if present. Note: if the filename is empty, there
1738 * is no null terminator following it. */
1739 if (file_name_nbytes) {
1740 file_name = MALLOC(file_name_nbytes + 2);
1742 ERROR("Failed to allocate %d bytes for dentry file name",
1743 file_name_nbytes + 2);
1744 ret = WIMLIB_ERR_NOMEM;
1745 goto out_free_inode;
1747 memcpy(file_name, p, file_name_nbytes);
1748 p += file_name_nbytes + 2;
1749 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
1755 /* Read the short filename if present. Note: if there is no short
1756 * filename, there is no null terminator following it. */
1757 if (short_name_nbytes) {
1758 short_name = MALLOC(short_name_nbytes + 2);
1760 ERROR("Failed to allocate %d bytes for dentry short name",
1761 short_name_nbytes + 2);
1762 ret = WIMLIB_ERR_NOMEM;
1763 goto out_free_file_name;
1765 memcpy(short_name, p, short_name_nbytes);
1766 p += short_name_nbytes + 2;
1767 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
1772 /* Align the dentry length */
1773 dentry->length = (dentry->length + 7) & ~7;
1776 * Read the alternate data streams, if present. dentry->num_ads tells
1777 * us how many they are, and they will directly follow the dentry
1780 * Note that each alternate data stream entry begins on an 8-byte
1781 * aligned boundary, and the alternate data stream entries seem to NOT
1782 * be included in the dentry->length field for some reason.
1784 if (inode->i_num_ads != 0) {
1785 ret = WIMLIB_ERR_INVALID_DENTRY;
1786 if (offset + dentry->length > metadata_resource_len ||
1787 (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
1789 metadata_resource_len - offset - dentry->length)))
1791 ERROR("Failed to read alternate data stream "
1792 "entries of WIM dentry \"%"WS"\"", file_name);
1793 goto out_free_short_name;
1796 /* We've read all the data for this dentry. Set the names and their
1797 * lengths, and we've done. */
1798 dentry->d_inode = inode;
1799 dentry->file_name = file_name;
1800 dentry->short_name = short_name;
1801 dentry->file_name_nbytes = file_name_nbytes;
1802 dentry->short_name_nbytes = short_name_nbytes;
1805 out_free_short_name:
1815 static const tchar *
1816 dentry_get_file_type_string(const struct wim_dentry *dentry)
1818 const struct wim_inode *inode = dentry->d_inode;
1819 if (inode_is_directory(inode))
1820 return T("directory");
1821 else if (inode_is_symlink(inode))
1822 return T("symbolic link");
1827 /* Reads the children of a dentry, and all their children, ..., etc. from the
1828 * metadata resource and into the dentry tree.
1830 * @metadata_resource: An array that contains the uncompressed metadata
1831 * resource for the WIM file.
1833 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1836 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
1837 * tree and has already been read from the metadata resource. It
1838 * does not need to be the real root because this procedure is
1839 * called recursively.
1841 * Returns zero on success; nonzero on failure.
1844 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1845 struct wim_dentry *dentry)
1847 u64 cur_offset = dentry->subdir_offset;
1848 struct wim_dentry *child;
1849 struct wim_dentry *duplicate;
1850 struct wim_dentry cur_child;
1854 * If @dentry has no child dentries, nothing more needs to be done for
1855 * this branch. This is the case for regular files, symbolic links, and
1856 * *possibly* empty directories (although an empty directory may also
1857 * have one child dentry that is the special end-of-directory dentry)
1859 if (cur_offset == 0)
1862 /* Find and read all the children of @dentry. */
1865 /* Read next child of @dentry into @cur_child. */
1866 ret = read_dentry(metadata_resource, metadata_resource_len,
1867 cur_offset, &cur_child);
1871 /* Check for end of directory. */
1872 if (cur_child.length == 0)
1875 /* Not end of directory. Allocate this child permanently and
1876 * link it to the parent and previous child. */
1877 child = memdup(&cur_child, sizeof(struct wim_dentry));
1879 ERROR("Failed to allocate new dentry!");
1880 ret = WIMLIB_ERR_NOMEM;
1884 /* Advance to the offset of the next child. Note: We need to
1885 * advance by the TOTAL length of the dentry, not by the length
1886 * cur_child.length, which although it does take into account
1887 * the padding, it DOES NOT take into account alternate stream
1889 cur_offset += dentry_total_length(child);
1891 duplicate = dentry_add_child(dentry, child);
1893 const tchar *child_type, *duplicate_type;
1894 child_type = dentry_get_file_type_string(child);
1895 duplicate_type = dentry_get_file_type_string(duplicate);
1896 WARNING("Ignoring duplicate %"TS" \"%"TS"\" "
1897 "(the WIM image already contains a %"TS" "
1898 "at that path with the exact same name)",
1899 child_type, dentry_full_path(duplicate),
1902 inode_add_dentry(child, child->d_inode);
1903 /* If there are children of this child, call this
1904 * procedure recursively. */
1905 if (child->subdir_offset != 0) {
1906 if (dentry_is_directory(child)) {
1907 ret = read_dentry_tree(metadata_resource,
1908 metadata_resource_len,
1913 WARNING("Ignoring children of non-directory \"%"TS"\"",
1914 dentry_full_path(child));
1924 * Writes a WIM dentry to an output buffer.
1926 * @dentry: The dentry structure.
1927 * @p: The memory location to write the data to.
1929 * Returns the pointer to the byte after the last byte we wrote as part of the
1930 * dentry, including any alternate data stream entries.
1933 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
1935 const struct wim_inode *inode;
1936 struct wim_dentry_on_disk *disk_dentry;
1940 wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
1943 inode = dentry->d_inode;
1944 disk_dentry = (struct wim_dentry_on_disk*)p;
1946 disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
1947 disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
1948 disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
1949 disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
1950 disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
1951 disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
1952 disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
1953 disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
1954 hash = inode_stream_hash(inode, 0);
1955 copy_hash(disk_dentry->unnamed_stream_hash, hash);
1956 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1957 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
1958 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
1959 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
1960 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
1962 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
1963 disk_dentry->nonreparse.hard_link_group_id =
1964 cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
1966 disk_dentry->num_alternate_data_streams = cpu_to_le16(inode->i_num_ads);
1967 disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
1968 disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
1969 p += sizeof(struct wim_dentry_on_disk);
1971 if (dentry_has_long_name(dentry))
1972 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
1974 if (dentry_has_short_name(dentry))
1975 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
1977 /* Align to 8-byte boundary */
1978 while ((uintptr_t)p & 7)
1981 /* We calculate the correct length of the dentry ourselves because the
1982 * dentry->length field may been set to an unexpected value from when we
1983 * read the dentry in (for example, there may have been unknown data
1984 * appended to the end of the dentry...). Furthermore, the dentry may
1985 * have been renamed, thus changing its needed length. */
1986 disk_dentry->length = cpu_to_le64(p - orig_p);
1988 /* Write the alternate data streams entries, if any. */
1989 for (u16 i = 0; i < inode->i_num_ads; i++) {
1990 const struct wim_ads_entry *ads_entry =
1991 &inode->i_ads_entries[i];
1992 struct wim_ads_entry_on_disk *disk_ads_entry =
1993 (struct wim_ads_entry_on_disk*)p;
1996 disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
1998 hash = inode_stream_hash(inode, i + 1);
1999 copy_hash(disk_ads_entry->hash, hash);
2000 disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
2001 p += sizeof(struct wim_ads_entry_on_disk);
2002 if (ads_entry->stream_name_nbytes) {
2003 p = mempcpy(p, ads_entry->stream_name,
2004 ads_entry->stream_name_nbytes + 2);
2006 /* Align to 8-byte boundary */
2007 while ((uintptr_t)p & 7)
2009 disk_ads_entry->length = cpu_to_le64(p - orig_p);
2015 write_dentry_cb(struct wim_dentry *dentry, void *_p)
2018 *p = write_dentry(dentry, *p);
2023 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
2026 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
2029 *p = write_dentry_tree_recursive(dentry, *p);
2033 /* Recursive function that writes a dentry tree rooted at @parent, not including
2034 * @parent itself, which has already been written. */
2036 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
2038 /* Nothing to do if this dentry has no children. */
2039 if (parent->subdir_offset == 0)
2042 /* Write child dentries and end-of-directory entry.
2044 * Note: we need to write all of this dentry's children before
2045 * recursively writing the directory trees rooted at each of the child
2046 * dentries, since the on-disk dentries for a dentry's children are
2047 * always located at consecutive positions in the metadata resource! */
2048 for_dentry_child(parent, write_dentry_cb, &p);
2050 /* write end of directory entry */
2051 *(le64*)p = cpu_to_le64(0);
2054 /* Recurse on children. */
2055 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
2059 /* Writes a directory tree to the metadata resource.
2061 * @root: Root of the dentry tree.
2062 * @p: Pointer to a buffer with enough space for the dentry tree.
2064 * Returns pointer to the byte after the last byte we wrote.
2067 write_dentry_tree(const struct wim_dentry *root, u8 *p)
2069 DEBUG("Writing dentry tree.");
2070 wimlib_assert(dentry_is_root(root));
2072 /* If we're the root dentry, we have no parent that already
2073 * wrote us, so we need to write ourselves. */
2074 p = write_dentry(root, p);
2076 /* Write end of directory entry after the root dentry just to be safe;
2077 * however the root dentry obviously cannot have any siblings. */
2078 *(le64*)p = cpu_to_le64(0);
2081 /* Recursively write the rest of the dentry tree. */
2082 return write_dentry_tree_recursive(root, p);