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 /* UNIX: Case-sensitive UTF-16LE dentry or stream name comparison. We call this
483 * on Windows as well to distinguish true duplicates from names differing by
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 strcmp() or strncmp() here, as the strings are in
491 * UTF-16LE format. */
492 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 /* Only call _wcsicmp() if both strings are of nonzero length; otherwise
512 * one could be NULL. */
513 if (nbytes1 && nbytes2)
514 return _wcsicmp((const wchar_t*)name1, (const wchar_t*)name2);
515 /* The strings are the same up to their minimum length, so return a
516 * result based on their lengths. */
517 if (nbytes1 < nbytes2)
519 else if (nbytes1 > nbytes2)
524 #endif /* __WIN32__ */
527 # define compare_utf16le_names compare_utf16le_names_case_insensitive
529 # define compare_utf16le_names compare_utf16le_names_case_sensitive
535 dentry_compare_names_case_insensitive(const struct wim_dentry *d1,
536 const struct wim_dentry *d2)
538 return compare_utf16le_names_case_insensitive(d1->file_name,
539 d1->file_name_nbytes,
541 d2->file_name_nbytes);
543 #endif /* __WIN32__ */
546 dentry_compare_names_case_sensitive(const struct wim_dentry *d1,
547 const struct wim_dentry *d2)
549 return compare_utf16le_names_case_sensitive(d1->file_name,
550 d1->file_name_nbytes,
552 d2->file_name_nbytes);
556 # define dentry_compare_names dentry_compare_names_case_insensitive
558 # define dentry_compare_names dentry_compare_names_case_sensitive
561 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
562 * stream name @name that has length @name_nbytes bytes. */
564 ads_entry_has_name(const struct wim_ads_entry *entry,
565 const utf16lechar *name, size_t name_nbytes)
567 return !compare_utf16le_names(name, name_nbytes,
569 entry->stream_name_nbytes);
573 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
574 const utf16lechar *name,
577 struct rb_node *node = dentry->d_inode->i_children.rb_node;
578 struct wim_dentry *child;
580 child = rbnode_dentry(node);
581 int result = compare_utf16le_names(name, name_nbytes,
583 child->file_name_nbytes);
585 node = node->rb_left;
587 node = node->rb_right;
594 /* Returns the child of @dentry that has the file name @name. Returns NULL if
595 * no child has the name. */
597 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
600 return get_dentry_child_with_utf16le_name(dentry, name,
601 tstrlen(name) * sizeof(tchar));
603 utf16lechar *utf16le_name;
604 size_t utf16le_name_nbytes;
606 struct wim_dentry *child;
608 ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
609 &utf16le_name, &utf16le_name_nbytes);
613 child = get_dentry_child_with_utf16le_name(dentry,
615 utf16le_name_nbytes);
622 static struct wim_dentry *
623 get_dentry_utf16le(WIMStruct *wim, const utf16lechar *path)
625 struct wim_dentry *cur_dentry, *parent_dentry;
626 const utf16lechar *p, *pp;
628 cur_dentry = parent_dentry = wim_root_dentry(wim);
635 while (*p == cpu_to_le16('/'))
637 if (*p == cpu_to_le16('\0'))
640 while (*pp != cpu_to_le16('/') && *pp != cpu_to_le16('\0'))
643 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
644 (void*)pp - (void*)p);
645 if (cur_dentry == NULL)
648 parent_dentry = cur_dentry;
650 if (cur_dentry == NULL) {
651 if (dentry_is_directory(parent_dentry))
659 /* Returns the dentry corresponding to the @path, or NULL if there is no such
662 get_dentry(WIMStruct *wim, const tchar *path)
665 return get_dentry_utf16le(wim, path);
667 utf16lechar *path_utf16le;
668 size_t path_utf16le_nbytes;
670 struct wim_dentry *dentry;
672 ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
673 &path_utf16le, &path_utf16le_nbytes);
676 dentry = get_dentry_utf16le(wim, path_utf16le);
683 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
685 struct wim_dentry *dentry;
686 dentry = get_dentry(wim, path);
688 return dentry->d_inode;
693 /* Takes in a path of length @len in @buf, and transforms it into a string for
694 * the path of its parent directory. */
696 to_parent_name(tchar *buf, size_t len)
698 ssize_t i = (ssize_t)len - 1;
699 while (i >= 0 && buf[i] == T('/'))
701 while (i >= 0 && buf[i] != T('/'))
703 while (i >= 0 && buf[i] == T('/'))
705 buf[i + 1] = T('\0');
708 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
709 * if the dentry is not found. */
711 get_parent_dentry(WIMStruct *wim, const tchar *path)
713 size_t path_len = tstrlen(path);
714 tchar buf[path_len + 1];
716 tmemcpy(buf, path, path_len + 1);
717 to_parent_name(buf, path_len);
718 return get_dentry(wim, buf);
721 /* Prints the full path of a dentry. */
723 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
725 int ret = calculate_dentry_full_path(dentry);
728 tprintf(T("%"TS"\n"), dentry->_full_path);
732 /* We want to be able to show the names of the file attribute flags that are
734 struct file_attr_flag {
738 struct file_attr_flag file_attr_flags[] = {
739 {FILE_ATTRIBUTE_READONLY, T("READONLY")},
740 {FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
741 {FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
742 {FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
743 {FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
744 {FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
745 {FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
746 {FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
747 {FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
748 {FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
749 {FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
750 {FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
751 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
752 {FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
753 {FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
756 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
757 * available. If the dentry is unresolved and the lookup table is NULL, the
758 * lookup table entries will not be printed. Otherwise, they will be. */
760 print_dentry(struct wim_dentry *dentry, void *lookup_table)
763 struct wim_lookup_table_entry *lte;
764 const struct wim_inode *inode = dentry->d_inode;
767 tprintf(T("[DENTRY]\n"));
768 tprintf(T("Length = %"PRIu64"\n"), dentry->length);
769 tprintf(T("Attributes = 0x%x\n"), inode->i_attributes);
770 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
771 if (file_attr_flags[i].flag & inode->i_attributes)
772 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
773 file_attr_flags[i].name);
774 tprintf(T("Security ID = %d\n"), inode->i_security_id);
775 tprintf(T("Subdir offset = %"PRIu64"\n"), dentry->subdir_offset);
777 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
778 tprintf(T("Creation Time = %"TS"\n"), buf);
780 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
781 tprintf(T("Last Access Time = %"TS"\n"), buf);
783 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
784 tprintf(T("Last Write Time = %"TS"\n"), buf);
786 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
787 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), inode->i_reparse_tag);
788 tprintf(T("Reparse Point Flags = 0x%"PRIx16"\n"),
789 inode->i_not_rpfixed);
790 tprintf(T("Reparse Point Unknown 2 = 0x%"PRIx32"\n"),
791 inode->i_rp_unknown_2);
793 tprintf(T("Reparse Point Unknown 1 = 0x%"PRIx32"\n"),
794 inode->i_rp_unknown_1);
795 tprintf(T("Hard Link Group = 0x%"PRIx64"\n"), inode->i_ino);
796 tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
797 tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
798 if (dentry_has_long_name(dentry))
799 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
800 if (dentry_has_short_name(dentry))
801 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
802 if (dentry->_full_path)
803 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
805 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
807 print_lookup_table_entry(lte, stdout);
809 hash = inode_stream_hash(inode, 0);
811 tprintf(T("Hash = 0x"));
812 print_hash(hash, stdout);
817 for (u16 i = 0; i < inode->i_num_ads; i++) {
818 tprintf(T("[Alternate Stream Entry %u]\n"), i);
819 wimlib_printf(T("Name = \"%"WS"\"\n"),
820 inode->i_ads_entries[i].stream_name);
821 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
822 inode->i_ads_entries[i].stream_name_nbytes);
823 hash = inode_stream_hash(inode, i + 1);
825 tprintf(T("Hash = 0x"));
826 print_hash(hash, stdout);
829 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
835 /* Initializations done on every `struct wim_dentry'. */
837 dentry_common_init(struct wim_dentry *dentry)
839 memset(dentry, 0, sizeof(struct wim_dentry));
843 new_timeless_inode(void)
845 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
847 inode->i_security_id = -1;
849 inode->i_next_stream_id = 1;
850 inode->i_not_rpfixed = 1;
851 INIT_LIST_HEAD(&inode->i_list);
853 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
854 ERROR_WITH_ERRNO("Error initializing mutex");
859 INIT_LIST_HEAD(&inode->i_dentry);
864 static struct wim_inode *
867 struct wim_inode *inode = new_timeless_inode();
869 u64 now = get_wim_timestamp();
870 inode->i_creation_time = now;
871 inode->i_last_access_time = now;
872 inode->i_last_write_time = now;
877 /* Creates an unlinked directory entry. */
879 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
881 struct wim_dentry *dentry;
884 dentry = MALLOC(sizeof(struct wim_dentry));
886 return WIMLIB_ERR_NOMEM;
888 dentry_common_init(dentry);
889 ret = set_dentry_name(dentry, name);
891 dentry->parent = dentry;
892 *dentry_ret = dentry;
895 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
903 _new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
906 struct wim_dentry *dentry;
909 ret = new_dentry(name, &dentry);
914 dentry->d_inode = new_timeless_inode();
916 dentry->d_inode = new_inode();
917 if (!dentry->d_inode) {
919 return WIMLIB_ERR_NOMEM;
922 inode_add_dentry(dentry, dentry->d_inode);
923 *dentry_ret = dentry;
928 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
930 return _new_dentry_with_inode(name, dentry_ret, true);
934 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
936 return _new_dentry_with_inode(name, dentry_ret, false);
940 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
943 struct wim_dentry *dentry;
945 DEBUG("Creating filler directory \"%"TS"\"", name);
946 ret = new_dentry_with_inode(name, &dentry);
949 /* Leave the inode number as 0; this is allowed for non
950 * hard-linked files. */
951 dentry->d_inode->i_resolved = 1;
952 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
953 *dentry_ret = dentry;
958 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
959 size_t name_nbytes, bool is_utf16le)
962 memset(ads_entry, 0, sizeof(*ads_entry));
965 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
967 return WIMLIB_ERR_NOMEM;
968 memcpy(p, name, name_nbytes);
969 p[name_nbytes / 2] = cpu_to_le16(0);
970 ads_entry->stream_name = p;
971 ads_entry->stream_name_nbytes = name_nbytes;
973 if (name && *(const tchar*)name != T('\0')) {
974 ret = get_utf16le_name(name, &ads_entry->stream_name,
975 &ads_entry->stream_name_nbytes);
982 destroy_ads_entry(struct wim_ads_entry *ads_entry)
984 FREE(ads_entry->stream_name);
987 /* Frees an inode. */
989 free_inode(struct wim_inode *inode)
992 if (inode->i_ads_entries) {
993 for (u16 i = 0; i < inode->i_num_ads; i++)
994 destroy_ads_entry(&inode->i_ads_entries[i]);
995 FREE(inode->i_ads_entries);
998 wimlib_assert(inode->i_num_opened_fds == 0);
1000 pthread_mutex_destroy(&inode->i_mutex);
1002 /* HACK: This may instead delete the inode from i_list, but the
1003 * hlist_del() behaves the same as list_del(). */
1004 hlist_del(&inode->i_hlist);
1005 FREE(inode->i_extracted_file);
1010 /* Decrements link count on an inode and frees it if the link count reaches 0.
1013 put_inode(struct wim_inode *inode)
1015 wimlib_assert(inode->i_nlink != 0);
1016 if (--inode->i_nlink == 0) {
1018 if (inode->i_num_opened_fds == 0)
1026 /* Frees a WIM dentry.
1028 * The corresponding inode (if any) is freed only if its link count is
1032 free_dentry(struct wim_dentry *dentry)
1035 FREE(dentry->file_name);
1036 FREE(dentry->short_name);
1037 FREE(dentry->_full_path);
1038 if (dentry->d_inode)
1039 put_inode(dentry->d_inode);
1044 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
1045 * to free a directory tree. */
1047 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
1049 struct wim_lookup_table *lookup_table = _lookup_table;
1052 struct wim_inode *inode = dentry->d_inode;
1053 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1054 struct wim_lookup_table_entry *lte;
1056 lte = inode_stream_lte(inode, i, lookup_table);
1058 lte_decrement_refcnt(lte, lookup_table);
1061 free_dentry(dentry);
1066 * Unlinks and frees a dentry tree.
1068 * @root: The root of the tree.
1069 * @lookup_table: The lookup table for dentries. If non-NULL, the
1070 * reference counts in the lookup table for the lookup
1071 * table entries corresponding to the dentries will be
1075 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1077 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1081 * Links a dentry into the directory tree.
1083 * @parent: The dentry that will be the parent of @child.
1084 * @child: The dentry to link.
1086 * Returns NULL if successful. If @parent already contains a dentry with the
1087 * same name as @child (see compare_utf16le_names() for what names are
1088 * considered the "same"), the pointer to this duplicate dentry is returned.
1091 dentry_add_child(struct wim_dentry * restrict parent,
1092 struct wim_dentry * restrict child)
1094 wimlib_assert(dentry_is_directory(parent));
1095 wimlib_assert(parent != child);
1097 struct rb_root *root = &parent->d_inode->i_children;
1098 struct rb_node **new = &(root->rb_node);
1099 struct rb_node *rb_parent = NULL;
1102 struct wim_dentry *this = rbnode_dentry(*new);
1103 int result = dentry_compare_names(child, this);
1108 new = &((*new)->rb_left);
1109 else if (result > 0)
1110 new = &((*new)->rb_right);
1114 child->parent = parent;
1115 rb_link_node(&child->rb_node, rb_parent, new);
1116 rb_insert_color(&child->rb_node, root);
1120 /* Unlink a WIM dentry from the directory entry tree. */
1122 unlink_dentry(struct wim_dentry *dentry)
1124 if (!dentry_is_root(dentry))
1125 rb_erase(&dentry->rb_node, &dentry->parent->d_inode->i_children);
1129 * Returns the alternate data stream entry belonging to @inode that has the
1130 * stream name @stream_name.
1132 struct wim_ads_entry *
1133 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1136 if (inode->i_num_ads == 0) {
1139 size_t stream_name_utf16le_nbytes;
1141 struct wim_ads_entry *result;
1143 #if TCHAR_IS_UTF16LE
1144 const utf16lechar *stream_name_utf16le;
1146 stream_name_utf16le = stream_name;
1147 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1149 utf16lechar *stream_name_utf16le;
1152 int ret = tstr_to_utf16le(stream_name,
1153 tstrlen(stream_name) *
1155 &stream_name_utf16le,
1156 &stream_name_utf16le_nbytes);
1164 if (ads_entry_has_name(&inode->i_ads_entries[i],
1165 stream_name_utf16le,
1166 stream_name_utf16le_nbytes))
1170 result = &inode->i_ads_entries[i];
1173 } while (++i != inode->i_num_ads);
1174 #if !TCHAR_IS_UTF16LE
1175 FREE(stream_name_utf16le);
1181 static struct wim_ads_entry *
1182 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1183 size_t stream_name_nbytes, bool is_utf16le)
1186 struct wim_ads_entry *ads_entries;
1187 struct wim_ads_entry *new_entry;
1189 if (inode->i_num_ads >= 0xfffe) {
1190 ERROR("Too many alternate data streams in one inode!");
1193 num_ads = inode->i_num_ads + 1;
1194 ads_entries = REALLOC(inode->i_ads_entries,
1195 num_ads * sizeof(inode->i_ads_entries[0]));
1197 ERROR("Failed to allocate memory for new alternate data stream");
1200 inode->i_ads_entries = ads_entries;
1202 new_entry = &inode->i_ads_entries[num_ads - 1];
1203 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1205 new_entry->stream_id = inode->i_next_stream_id++;
1206 inode->i_num_ads = num_ads;
1210 struct wim_ads_entry *
1211 inode_add_ads_utf16le(struct wim_inode *inode,
1212 const utf16lechar *stream_name,
1213 size_t stream_name_nbytes)
1215 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1216 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1220 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1221 * NULL if memory could not be allocated.
1223 struct wim_ads_entry *
1224 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1226 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1227 return do_inode_add_ads(inode, stream_name,
1228 tstrlen(stream_name) * sizeof(tchar),
1232 static struct wim_lookup_table_entry *
1233 add_stream_from_data_buffer(const void *buffer, size_t size,
1234 struct wim_lookup_table *lookup_table)
1236 u8 hash[SHA1_HASH_SIZE];
1237 struct wim_lookup_table_entry *lte, *existing_lte;
1239 sha1_buffer(buffer, size, hash);
1240 existing_lte = __lookup_resource(lookup_table, hash);
1242 wimlib_assert(wim_resource_size(existing_lte) == size);
1247 lte = new_lookup_table_entry();
1250 buffer_copy = memdup(buffer, size);
1252 free_lookup_table_entry(lte);
1255 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1256 lte->attached_buffer = buffer_copy;
1257 lte->resource_entry.original_size = size;
1258 copy_hash(lte->hash, hash);
1259 lookup_table_insert(lookup_table, lte);
1265 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1266 const void *value, size_t size,
1267 struct wim_lookup_table *lookup_table)
1269 struct wim_ads_entry *new_ads_entry;
1271 wimlib_assert(inode->i_resolved);
1273 new_ads_entry = inode_add_ads(inode, name);
1275 return WIMLIB_ERR_NOMEM;
1277 new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1279 if (!new_ads_entry->lte) {
1280 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1282 return WIMLIB_ERR_NOMEM;
1287 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
1288 * stream contents. */
1290 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
1291 struct wim_lookup_table *lookup_table)
1293 inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
1295 return WIMLIB_ERR_NOMEM;
1296 inode->i_resolved = 1;
1300 /* Remove an alternate data stream from a WIM inode */
1302 inode_remove_ads(struct wim_inode *inode, u16 idx,
1303 struct wim_lookup_table *lookup_table)
1305 struct wim_ads_entry *ads_entry;
1306 struct wim_lookup_table_entry *lte;
1308 wimlib_assert(idx < inode->i_num_ads);
1309 wimlib_assert(inode->i_resolved);
1311 ads_entry = &inode->i_ads_entries[idx];
1313 DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1315 lte = ads_entry->lte;
1317 lte_decrement_refcnt(lte, lookup_table);
1319 destroy_ads_entry(ads_entry);
1321 memmove(&inode->i_ads_entries[idx],
1322 &inode->i_ads_entries[idx + 1],
1323 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1329 inode_get_unix_data(const struct wim_inode *inode,
1330 struct wimlib_unix_data *unix_data,
1331 u16 *stream_idx_ret)
1333 const struct wim_ads_entry *ads_entry;
1334 const struct wim_lookup_table_entry *lte;
1338 wimlib_assert(inode->i_resolved);
1340 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1341 WIMLIB_UNIX_DATA_TAG, NULL);
1343 return NO_UNIX_DATA;
1346 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1348 lte = ads_entry->lte;
1350 return NO_UNIX_DATA;
1352 size = wim_resource_size(lte);
1353 if (size != sizeof(struct wimlib_unix_data))
1354 return BAD_UNIX_DATA;
1356 ret = read_full_resource_into_buf(lte, unix_data);
1360 if (unix_data->version != 0)
1361 return BAD_UNIX_DATA;
1366 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1367 struct wim_lookup_table *lookup_table, int which)
1369 struct wimlib_unix_data unix_data;
1371 bool have_good_unix_data = false;
1372 bool have_unix_data = false;
1375 if (!(which & UNIX_DATA_CREATE)) {
1376 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1377 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1378 have_unix_data = true;
1380 have_good_unix_data = true;
1382 unix_data.version = 0;
1383 if (which & UNIX_DATA_UID || !have_good_unix_data)
1384 unix_data.uid = uid;
1385 if (which & UNIX_DATA_GID || !have_good_unix_data)
1386 unix_data.gid = gid;
1387 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1388 unix_data.mode = mode;
1389 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1391 sizeof(struct wimlib_unix_data),
1393 if (ret == 0 && have_unix_data)
1394 inode_remove_ads(inode, stream_idx, lookup_table);
1397 #endif /* !__WIN32__ */
1399 /* Replace weird characters in filenames and alternate data stream names.
1401 * In particular we do not want the path separator to appear in any names, as
1402 * that would make it possible for a "malicious" WIM to extract itself to any
1403 * location it wanted to. */
1405 replace_forbidden_characters(utf16lechar *name)
1409 for (p = name; *p; p++) {
1411 if (wcschr(L"<>:\"/\\|?*", (wchar_t)*p))
1413 if (*p == cpu_to_le16('/'))
1417 WARNING("File, directory, or stream name \"%"WS"\"\n"
1418 " contains forbidden characters; "
1419 "substituting replacement characters.",
1424 *p = cpu_to_le16(0xfffd);
1426 *p = cpu_to_le16('?');
1433 * Reads the alternate data stream entries of a WIM dentry.
1435 * @p: Pointer to buffer that starts with the first alternate stream entry.
1437 * @inode: Inode to load the alternate data streams into.
1438 * @inode->i_num_ads must have been set to the number of
1439 * alternate data streams that are expected.
1441 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1445 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1446 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1447 * failure, @inode is not modified.
1450 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1451 size_t nbytes_remaining)
1454 struct wim_ads_entry *ads_entries;
1457 BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
1459 /* Allocate an array for our in-memory representation of the alternate
1460 * data stream entries. */
1461 num_ads = inode->i_num_ads;
1462 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1466 /* Read the entries into our newly allocated buffer. */
1467 for (u16 i = 0; i < num_ads; i++) {
1469 struct wim_ads_entry *cur_entry;
1470 const struct wim_ads_entry_on_disk *disk_entry =
1471 (const struct wim_ads_entry_on_disk*)p;
1473 cur_entry = &ads_entries[i];
1474 ads_entries[i].stream_id = i + 1;
1476 /* Do we have at least the size of the fixed-length data we know
1478 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1481 /* Read the length field */
1482 length = le64_to_cpu(disk_entry->length);
1484 /* Make sure the length field is neither so small it doesn't
1485 * include all the fixed-length data nor so large it overflows
1486 * the metadata resource buffer. */
1487 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1488 length > nbytes_remaining)
1491 /* Read the rest of the fixed-length data. */
1493 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1494 copy_hash(cur_entry->hash, disk_entry->hash);
1495 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1497 /* If stream_name_nbytes != 0, this is a named stream.
1498 * Otherwise this is an unnamed stream, or in some cases (bugs
1499 * in Microsoft's software I guess) a meaningless entry
1500 * distinguished from the real unnamed stream entry, if any, by
1501 * the fact that the real unnamed stream entry has a nonzero
1503 if (cur_entry->stream_name_nbytes) {
1504 /* The name is encoded in UTF16-LE, which uses 2-byte
1505 * coding units, so the length of the name had better be
1506 * an even number of bytes... */
1507 if (cur_entry->stream_name_nbytes & 1)
1510 /* Add the length of the stream name to get the length
1511 * we actually need to read. Make sure this isn't more
1512 * than the specified length of the entry. */
1513 if (sizeof(struct wim_ads_entry_on_disk) +
1514 cur_entry->stream_name_nbytes > length)
1517 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1518 if (!cur_entry->stream_name)
1521 memcpy(cur_entry->stream_name,
1522 disk_entry->stream_name,
1523 cur_entry->stream_name_nbytes);
1524 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1525 replace_forbidden_characters(cur_entry->stream_name);
1528 /* It's expected that the size of every ADS entry is a multiple
1529 * of 8. However, to be safe, I'm allowing the possibility of
1530 * an ADS entry at the very end of the metadata resource ending
1531 * un-aligned. So although we still need to increment the input
1532 * pointer by @length to reach the next ADS entry, it's possible
1533 * that less than @length is actually remaining in the metadata
1534 * resource. We should set the remaining bytes to 0 if this
1536 length = (length + 7) & ~(u64)7;
1538 if (nbytes_remaining < length)
1539 nbytes_remaining = 0;
1541 nbytes_remaining -= length;
1543 inode->i_ads_entries = ads_entries;
1544 inode->i_next_stream_id = inode->i_num_ads + 1;
1548 ret = WIMLIB_ERR_NOMEM;
1549 goto out_free_ads_entries;
1551 ERROR("An alternate data stream entry is invalid");
1552 ret = WIMLIB_ERR_INVALID_DENTRY;
1553 out_free_ads_entries:
1555 for (u16 i = 0; i < num_ads; i++)
1556 destroy_ads_entry(&ads_entries[i]);
1564 * Reads a WIM directory entry, including all alternate data stream entries that
1565 * follow it, from the WIM image's metadata resource.
1567 * @metadata_resource:
1568 * Pointer to the metadata resource buffer.
1570 * @metadata_resource_len:
1571 * Length of the metadata resource buffer, in bytes.
1573 * @offset: Offset of the dentry within the metadata resource.
1575 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1577 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1578 * been modified, but it will not be left with pointers to any allocated
1579 * buffers. On success, the dentry->length field must be examined. If zero,
1580 * this was a special "end of directory" dentry and not a real dentry. If
1581 * nonzero, this was a real dentry.
1583 * Possible errors include:
1585 * WIMLIB_ERR_INVALID_DENTRY
1588 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1589 u64 offset, struct wim_dentry * restrict dentry)
1592 u64 calculated_size;
1593 utf16lechar *file_name;
1594 utf16lechar *short_name;
1595 u16 short_name_nbytes;
1596 u16 file_name_nbytes;
1598 struct wim_inode *inode;
1599 const u8 *p = &metadata_resource[offset];
1600 const struct wim_dentry_on_disk *disk_dentry =
1601 (const struct wim_dentry_on_disk*)p;
1603 BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE);
1605 if ((uintptr_t)p & 7)
1606 WARNING("WIM dentry is not 8-byte aligned");
1608 dentry_common_init(dentry);
1610 /* Before reading the whole dentry, we need to read just the length.
1611 * This is because a dentry of length 8 (that is, just the length field)
1612 * terminates the list of sibling directory entries. */
1613 if (offset + sizeof(u64) > metadata_resource_len ||
1614 offset + sizeof(u64) < offset)
1616 ERROR("Directory entry starting at %"PRIu64" ends past the "
1617 "end of the metadata resource (size %"PRIu64")",
1618 offset, metadata_resource_len);
1619 return WIMLIB_ERR_INVALID_DENTRY;
1621 dentry->length = le64_to_cpu(disk_dentry->length);
1623 /* A zero length field (really a length of 8, since that's how big the
1624 * directory entry is...) indicates that this is the end of directory
1625 * dentry. We do not read it into memory as an actual dentry, so just
1626 * return successfully in this case. */
1627 if (dentry->length == 8)
1629 if (dentry->length == 0)
1632 /* Now that we have the actual length provided in the on-disk structure,
1633 * again make sure it doesn't overflow the metadata resource buffer. */
1634 if (offset + dentry->length > metadata_resource_len ||
1635 offset + dentry->length < offset)
1637 ERROR("Directory entry at offset %"PRIu64" and with size "
1638 "%"PRIu64" ends past the end of the metadata resource "
1640 offset, dentry->length, metadata_resource_len);
1641 return WIMLIB_ERR_INVALID_DENTRY;
1644 /* Make sure the dentry length is at least as large as the number of
1645 * fixed-length fields */
1646 if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1647 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1649 return WIMLIB_ERR_INVALID_DENTRY;
1652 /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1653 inode = new_timeless_inode();
1655 return WIMLIB_ERR_NOMEM;
1657 /* Read more fields; some into the dentry, and some into the inode. */
1659 inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1660 inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1661 dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1662 dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1663 dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1664 inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1665 inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1666 inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1667 copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1669 /* I don't know what's going on here. It seems like M$ screwed up the
1670 * reparse points, then put the fields in the same place and didn't
1671 * document it. So we have some fields we read for reparse points, and
1672 * some fields in the same place for non-reparse-point.s */
1673 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1674 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1675 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1676 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1677 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1678 /* Leave inode->i_ino at 0. Note that this means the WIM file
1679 * cannot archive hard-linked reparse points. Such a thing
1680 * doesn't really make sense anyway, although I believe it's
1681 * theoretically possible to have them on NTFS. */
1683 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1684 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1687 inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1689 short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1690 file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1692 if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1694 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1695 ret = WIMLIB_ERR_INVALID_DENTRY;
1696 goto out_free_inode;
1699 /* We now know the length of the file name and short name. Make sure
1700 * the length of the dentry is large enough to actually hold them.
1702 * The calculated length here is unaligned to allow for the possibility
1703 * that the dentry->length names an unaligned length, although this
1704 * would be unexpected. */
1705 calculated_size = _dentry_correct_length_unaligned(file_name_nbytes,
1708 if (dentry->length < calculated_size) {
1709 ERROR("Unexpected end of directory entry! (Expected "
1710 "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1711 calculated_size, dentry->length);
1712 ret = WIMLIB_ERR_INVALID_DENTRY;
1713 goto out_free_inode;
1716 p += sizeof(struct wim_dentry_on_disk);
1718 /* Read the filename if present. Note: if the filename is empty, there
1719 * is no null terminator following it. */
1720 if (file_name_nbytes) {
1721 file_name = MALLOC(file_name_nbytes + 2);
1723 ERROR("Failed to allocate %d bytes for dentry file name",
1724 file_name_nbytes + 2);
1725 ret = WIMLIB_ERR_NOMEM;
1726 goto out_free_inode;
1728 memcpy(file_name, p, file_name_nbytes);
1729 p += file_name_nbytes + 2;
1730 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
1731 replace_forbidden_characters(file_name);
1737 /* Read the short filename if present. Note: if there is no short
1738 * filename, there is no null terminator following it. */
1739 if (short_name_nbytes) {
1740 short_name = MALLOC(short_name_nbytes + 2);
1742 ERROR("Failed to allocate %d bytes for dentry short name",
1743 short_name_nbytes + 2);
1744 ret = WIMLIB_ERR_NOMEM;
1745 goto out_free_file_name;
1747 memcpy(short_name, p, short_name_nbytes);
1748 p += short_name_nbytes + 2;
1749 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
1750 replace_forbidden_characters(short_name);
1755 /* Align the dentry length */
1756 dentry->length = (dentry->length + 7) & ~7;
1759 * Read the alternate data streams, if present. dentry->num_ads tells
1760 * us how many they are, and they will directly follow the dentry
1763 * Note that each alternate data stream entry begins on an 8-byte
1764 * aligned boundary, and the alternate data stream entries seem to NOT
1765 * be included in the dentry->length field for some reason.
1767 if (inode->i_num_ads != 0) {
1768 ret = WIMLIB_ERR_INVALID_DENTRY;
1769 if (offset + dentry->length > metadata_resource_len ||
1770 (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
1772 metadata_resource_len - offset - dentry->length)))
1774 ERROR("Failed to read alternate data stream "
1775 "entries of WIM dentry \"%"WS"\"", file_name);
1776 goto out_free_short_name;
1779 /* We've read all the data for this dentry. Set the names and their
1780 * lengths, and we've done. */
1781 dentry->d_inode = inode;
1782 dentry->file_name = file_name;
1783 dentry->short_name = short_name;
1784 dentry->file_name_nbytes = file_name_nbytes;
1785 dentry->short_name_nbytes = short_name_nbytes;
1788 out_free_short_name:
1798 static const tchar *
1799 dentry_get_file_type_string(const struct wim_dentry *dentry)
1801 const struct wim_inode *inode = dentry->d_inode;
1802 if (inode_is_directory(inode))
1803 return T("directory");
1804 else if (inode_is_symlink(inode))
1805 return T("symbolic link");
1810 /* Reads the children of a dentry, and all their children, ..., etc. from the
1811 * metadata resource and into the dentry tree.
1813 * @metadata_resource: An array that contains the uncompressed metadata
1814 * resource for the WIM file.
1816 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1819 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
1820 * tree and has already been read from the metadata resource. It
1821 * does not need to be the real root because this procedure is
1822 * called recursively.
1824 * Returns zero on success; nonzero on failure.
1827 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1828 struct wim_dentry *dentry)
1830 u64 cur_offset = dentry->subdir_offset;
1831 struct wim_dentry *child;
1832 struct wim_dentry *duplicate;
1833 struct wim_dentry cur_child;
1837 * If @dentry has no child dentries, nothing more needs to be done for
1838 * this branch. This is the case for regular files, symbolic links, and
1839 * *possibly* empty directories (although an empty directory may also
1840 * have one child dentry that is the special end-of-directory dentry)
1842 if (cur_offset == 0)
1845 /* Find and read all the children of @dentry. */
1848 /* Read next child of @dentry into @cur_child. */
1849 ret = read_dentry(metadata_resource, metadata_resource_len,
1850 cur_offset, &cur_child);
1854 /* Check for end of directory. */
1855 if (cur_child.length == 0)
1858 /* Not end of directory. Allocate this child permanently and
1859 * link it to the parent and previous child. */
1860 child = memdup(&cur_child, sizeof(struct wim_dentry));
1862 ERROR("Failed to allocate new dentry!");
1863 ret = WIMLIB_ERR_NOMEM;
1867 /* Advance to the offset of the next child. Note: We need to
1868 * advance by the TOTAL length of the dentry, not by the length
1869 * cur_child.length, which although it does take into account
1870 * the padding, it DOES NOT take into account alternate stream
1872 cur_offset += dentry_total_length(child);
1874 duplicate = dentry_add_child(dentry, child);
1876 const tchar *child_type, *duplicate_type;
1877 child_type = dentry_get_file_type_string(child);
1878 duplicate_type = dentry_get_file_type_string(duplicate);
1879 /* On UNIX, duplicates are exact. On Windows,
1880 * duplicates may differ by case and we wish to provide
1881 * a different warning message in this case. */
1883 if (dentry_compare_names_case_sensitive(child, duplicate))
1885 child->parent = dentry;
1886 WARNING("Ignoring %ls \"%ls\", which differs "
1887 "only in case from %ls \"%ls\"",
1889 dentry_full_path(child),
1891 dentry_full_path(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),
1904 inode_add_dentry(child, child->d_inode);
1905 /* If there are children of this child, call this
1906 * procedure recursively. */
1907 if (child->subdir_offset != 0) {
1908 if (dentry_is_directory(child)) {
1909 ret = read_dentry_tree(metadata_resource,
1910 metadata_resource_len,
1915 WARNING("Ignoring children of non-directory \"%"TS"\"",
1916 dentry_full_path(child));
1926 * Writes a WIM dentry to an output buffer.
1928 * @dentry: The dentry structure.
1929 * @p: The memory location to write the data to.
1931 * Returns the pointer to the byte after the last byte we wrote as part of the
1932 * dentry, including any alternate data stream entries.
1935 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
1937 const struct wim_inode *inode;
1938 struct wim_dentry_on_disk *disk_dentry;
1942 wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
1945 inode = dentry->d_inode;
1946 disk_dentry = (struct wim_dentry_on_disk*)p;
1948 disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
1949 disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
1950 disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
1951 disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
1952 disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
1953 disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
1954 disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
1955 disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
1956 hash = inode_stream_hash(inode, 0);
1957 copy_hash(disk_dentry->unnamed_stream_hash, hash);
1958 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1959 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
1960 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
1961 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
1962 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
1964 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
1965 disk_dentry->nonreparse.hard_link_group_id =
1966 cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
1968 disk_dentry->num_alternate_data_streams = cpu_to_le16(inode->i_num_ads);
1969 disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
1970 disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
1971 p += sizeof(struct wim_dentry_on_disk);
1973 if (dentry_has_long_name(dentry))
1974 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
1976 if (dentry_has_short_name(dentry))
1977 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
1979 /* Align to 8-byte boundary */
1980 while ((uintptr_t)p & 7)
1983 /* We calculate the correct length of the dentry ourselves because the
1984 * dentry->length field may been set to an unexpected value from when we
1985 * read the dentry in (for example, there may have been unknown data
1986 * appended to the end of the dentry...). Furthermore, the dentry may
1987 * have been renamed, thus changing its needed length. */
1988 disk_dentry->length = cpu_to_le64(p - orig_p);
1990 /* Write the alternate data streams entries, if any. */
1991 for (u16 i = 0; i < inode->i_num_ads; i++) {
1992 const struct wim_ads_entry *ads_entry =
1993 &inode->i_ads_entries[i];
1994 struct wim_ads_entry_on_disk *disk_ads_entry =
1995 (struct wim_ads_entry_on_disk*)p;
1998 disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
2000 hash = inode_stream_hash(inode, i + 1);
2001 copy_hash(disk_ads_entry->hash, hash);
2002 disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
2003 p += sizeof(struct wim_ads_entry_on_disk);
2004 if (ads_entry->stream_name_nbytes) {
2005 p = mempcpy(p, ads_entry->stream_name,
2006 ads_entry->stream_name_nbytes + 2);
2008 /* Align to 8-byte boundary */
2009 while ((uintptr_t)p & 7)
2011 disk_ads_entry->length = cpu_to_le64(p - orig_p);
2017 write_dentry_cb(struct wim_dentry *dentry, void *_p)
2020 *p = write_dentry(dentry, *p);
2025 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
2028 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
2031 *p = write_dentry_tree_recursive(dentry, *p);
2035 /* Recursive function that writes a dentry tree rooted at @parent, not including
2036 * @parent itself, which has already been written. */
2038 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
2040 /* Nothing to do if this dentry has no children. */
2041 if (parent->subdir_offset == 0)
2044 /* Write child dentries and end-of-directory entry.
2046 * Note: we need to write all of this dentry's children before
2047 * recursively writing the directory trees rooted at each of the child
2048 * dentries, since the on-disk dentries for a dentry's children are
2049 * always located at consecutive positions in the metadata resource! */
2050 for_dentry_child(parent, write_dentry_cb, &p);
2052 /* write end of directory entry */
2053 *(le64*)p = cpu_to_le64(0);
2056 /* Recurse on children. */
2057 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
2061 /* Writes a directory tree to the metadata resource.
2063 * @root: Root of the dentry tree.
2064 * @p: Pointer to a buffer with enough space for the dentry tree.
2066 * Returns pointer to the byte after the last byte we wrote.
2069 write_dentry_tree(const struct wim_dentry *root, u8 *p)
2071 DEBUG("Writing dentry tree.");
2072 wimlib_assert(dentry_is_root(root));
2074 /* If we're the root dentry, we have no parent that already
2075 * wrote us, so we need to write ourselves. */
2076 p = write_dentry(root, p);
2078 /* Write end of directory entry after the root dentry just to be safe;
2079 * however the root dentry obviously cannot have any siblings. */
2080 *(le64*)p = cpu_to_le64(0);
2083 /* Recursively write the rest of the dentry tree. */
2084 return write_dentry_tree_recursive(root, p);