4 * A dentry (directory entry) contains the metadata for a file. In the WIM file
5 * format, the dentries are stored in the "metadata resource" section right
6 * after the security data. Each image in the WIM file has its own metadata
7 * resource with its own security data and dentry tree. Dentries in different
8 * images may share file resources by referring to the same lookup table
13 * Copyright (C) 2012 Eric Biggers
15 * This file is part of wimlib, a library for working with WIM files.
17 * wimlib is free software; you can redistribute it and/or modify it under the
18 * terms of the GNU General Public License as published by the Free Software
19 * Foundation; either version 3 of the License, or (at your option) any later
22 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
23 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
24 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along with
27 * wimlib; if not, see http://www.gnu.org/licenses/.
37 #include "lookup_table.h"
39 #include "timestamp.h"
40 #include "wimlib_internal.h"
43 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
44 * a file name and short name that take the specified numbers of bytes. This
45 * excludes any alternate data stream entries that may follow the dentry. */
46 static u64 __dentry_correct_length_unaligned(u16 file_name_len,
49 u64 length = WIM_DENTRY_DISK_SIZE;
51 length += file_name_len + 2;
53 length += short_name_len + 2;
57 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
58 * the file name length and short name length. Note that dentry->length is
59 * ignored; also, this excludes any alternate data stream entries that may
60 * follow the dentry. */
61 static u64 dentry_correct_length_unaligned(const struct dentry *dentry)
63 return __dentry_correct_length_unaligned(dentry->file_name_len,
64 dentry->short_name_len);
67 /* Return the "correct" value to write in the length field of a WIM dentry,
68 * based on the file name length and short name length. */
69 static u64 dentry_correct_length(const struct dentry *dentry)
71 return (dentry_correct_length_unaligned(dentry) + 7) & ~7;
74 /* Return %true iff @dentry has the UTF-8 file name @name that has length
76 static bool dentry_has_name(const struct dentry *dentry, const char *name,
79 if (dentry->file_name_utf8_len != name_len)
81 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
84 /* Return %true iff the alternate data stream entry @entry has the UTF-8 stream
85 * name @name that has length @name_len bytes. */
86 static inline bool ads_entry_has_name(const struct ads_entry *entry,
87 const char *name, size_t name_len)
89 if (entry->stream_name_utf8_len != name_len)
91 return memcmp(entry->stream_name_utf8, name, name_len) == 0;
94 /* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
95 * and their lengths in the pointer arguments */
96 int get_names(char **name_utf16_ret, char **name_utf8_ret,
97 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
102 char *name_utf16, *name_utf8;
104 utf8_len = strlen(name);
106 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
109 return WIMLIB_ERR_NOMEM;
111 name_utf8 = MALLOC(utf8_len + 1);
114 return WIMLIB_ERR_NOMEM;
116 memcpy(name_utf8, name, utf8_len + 1);
117 FREE(*name_utf8_ret);
118 FREE(*name_utf16_ret);
119 *name_utf8_ret = name_utf8;
120 *name_utf16_ret = name_utf16;
121 *name_utf8_len_ret = utf8_len;
122 *name_utf16_len_ret = utf16_len;
126 /* Changes the name of a dentry to @new_name. Only changes the file_name and
127 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
128 * full_path_utf8 fields. Also recalculates its length. */
129 static int change_dentry_name(struct dentry *dentry, const char *new_name)
133 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
134 &dentry->file_name_len, &dentry->file_name_utf8_len,
136 FREE(dentry->short_name);
137 dentry->short_name_len = 0;
139 dentry->length = dentry_correct_length(dentry);
144 * Changes the name of an alternate data stream */
145 static int change_ads_name(struct ads_entry *entry, const char *new_name)
147 return get_names(&entry->stream_name, &entry->stream_name_utf8,
148 &entry->stream_name_len,
149 &entry->stream_name_utf8_len,
153 /* Returns the total length of a WIM alternate data stream entry on-disk,
154 * including the stream name, the null terminator, AND the padding after the
155 * entry to align the next one (or the next dentry) on an 8-byte boundary. */
156 static u64 ads_entry_total_length(const struct ads_entry *entry)
158 u64 len = WIM_ADS_ENTRY_DISK_SIZE;
159 if (entry->stream_name_len)
160 len += entry->stream_name_len + 2;
161 return (len + 7) & ~7;
165 static u64 __dentry_total_length(const struct dentry *dentry, u64 length)
167 const struct inode *inode = dentry->d_inode;
168 for (u16 i = 0; i < inode->num_ads; i++)
169 length += ads_entry_total_length(&inode->ads_entries[i]);
170 return (length + 7) & ~7;
173 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
174 * all alternate data streams. */
175 u64 dentry_correct_total_length(const struct dentry *dentry)
177 return __dentry_total_length(dentry,
178 dentry_correct_length_unaligned(dentry));
181 /* Like dentry_correct_total_length(), but use the existing dentry->length field
182 * instead of calculating its "correct" value. */
183 static u64 dentry_total_length(const struct dentry *dentry)
185 return __dentry_total_length(dentry, dentry->length);
188 /* Transfers file attributes from a `stat' buffer to a WIM "inode". */
189 void stbuf_to_inode(const struct stat *stbuf, struct inode *inode)
191 if (S_ISLNK(stbuf->st_mode)) {
192 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
193 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
194 } else if (S_ISDIR(stbuf->st_mode)) {
195 inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
197 inode->attributes = FILE_ATTRIBUTE_NORMAL;
199 if (sizeof(ino_t) >= 8)
200 inode->ino = (u64)stbuf->st_ino;
202 inode->ino = (u64)stbuf->st_ino |
203 ((u64)stbuf->st_dev << ((sizeof(ino_t) * 8) & 63));
205 inode->creation_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
206 inode->last_write_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
207 inode->last_access_time = timespec_to_wim_timestamp(&stbuf->st_atim);
211 /* Transfers file attributes from a struct inode to a `stat' buffer.
213 * The lookup table entry tells us which stream in the inode we are statting.
214 * For a named data stream, everything returned is the same as the unnamed data
215 * stream except possibly the size and block count. */
216 int inode_to_stbuf(const struct inode *inode, struct lookup_table_entry *lte,
219 if (inode_is_symlink(inode))
220 stbuf->st_mode = S_IFLNK | 0777;
221 else if (inode_is_directory(inode))
222 stbuf->st_mode = S_IFDIR | 0755;
224 stbuf->st_mode = S_IFREG | 0755;
226 stbuf->st_ino = (ino_t)inode->ino;
227 stbuf->st_nlink = inode->link_count;
228 stbuf->st_uid = getuid();
229 stbuf->st_gid = getgid();
232 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
233 wimlib_assert(lte->staging_file_name);
234 struct stat native_stat;
235 if (stat(lte->staging_file_name, &native_stat) != 0) {
236 DEBUG("Failed to stat `%s': %m",
237 lte->staging_file_name);
240 stbuf->st_size = native_stat.st_size;
242 stbuf->st_size = wim_resource_size(lte);
248 stbuf->st_atime = wim_timestamp_to_unix(inode->last_access_time);
249 stbuf->st_mtime = wim_timestamp_to_unix(inode->last_write_time);
250 stbuf->st_ctime = wim_timestamp_to_unix(inode->creation_time);
251 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
256 int for_dentry_in_rbtree(struct rb_node *root,
257 int (*visitor)(struct dentry *, void *),
261 struct rb_node *node = root;
265 list_add(&rbnode_dentry(node)->tmp_list, &stack);
266 node = node->rb_left;
268 struct list_head *next;
269 struct dentry *dentry;
274 dentry = container_of(next, struct dentry, tmp_list);
276 ret = visitor(dentry, arg);
279 node = dentry->rb_node.rb_right;
284 static int for_dentry_tree_in_rbtree_depth(struct rb_node *node,
285 int (*visitor)(struct dentry*, void*),
290 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
294 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
298 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
306 * Calls a function on all directory entries in a WIM dentry tree. Logically,
307 * this is a pre-order traversal (the function is called on a parent dentry
308 * before its children), but sibling dentries will be visited in order as well.
310 * In reality, the data structures are more complicated than the above might
311 * suggest because there is a separate red-black tree for each dentry that
312 * contains its direct children.
314 int for_dentry_in_tree(struct dentry *root,
315 int (*visitor)(struct dentry*, void*), void *arg)
318 struct list_head main_stack;
319 struct list_head sibling_stack;
320 struct list_head *sibling_stack_bottom;
321 struct dentry *main_dentry;
322 struct rb_node *node;
323 struct list_head *next_sibling;
324 struct dentry *dentry;
326 ret = visitor(root, arg);
331 sibling_stack_bottom = &sibling_stack;
332 INIT_LIST_HEAD(&main_stack);
333 INIT_LIST_HEAD(&sibling_stack);
335 list_add(&root->tmp_list, &main_stack);
336 node = root->d_inode->children.rb_node;
339 // Prepare for non-recursive in-order traversal of the red-black
340 // tree of this dentry's children
343 // Push this node to the sibling stack and examine the
344 // left neighbor, if any
345 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
346 node = node->rb_left;
349 next_sibling = sibling_stack.next;
350 if (next_sibling == sibling_stack_bottom) {
351 // Done with all siblings. Pop the main dentry to move
352 // back up one level.
353 main_dentry = container_of(main_stack.next,
356 list_del(&main_dentry->tmp_list);
358 if (main_dentry == root)
361 // Restore sibling stack bottom from the previous level
362 sibling_stack_bottom = (void*)main_dentry->parent;
364 // Restore the just-popped main dentry's parent
365 main_dentry->parent = container_of(main_stack.next,
369 // The next sibling to traverse in the previous level,
370 // in the in-order traversal of the red-black tree, is
371 // the one to the right.
372 node = main_dentry->rb_node.rb_right;
374 // The sibling stack is not empty, so there are more to
377 // Pop a sibling from the stack.
378 list_del(next_sibling);
379 dentry = container_of(next_sibling, struct dentry, tmp_list);
381 // Visit the sibling.
382 ret = visitor(dentry, arg);
384 // Failed. Restore parent pointers for the
385 // dentries in the main stack
386 list_del(&root->tmp_list);
387 list_for_each_entry(dentry, &main_stack, tmp_list) {
388 dentry->parent = container_of(dentry->tmp_list.next,
395 // We'd like to recursively visit the dentry tree rooted
396 // at this sibling. To do this, add it to the main
397 // stack, save the bottom of this level's sibling stack
398 // in the dentry->parent field, re-set the bottom of the
399 // sibling stack to be its current height, and set
400 // main_dentry to the sibling so it becomes the parent
401 // dentry in the next iteration through the outer loop.
402 if (inode_has_children(dentry->d_inode)) {
403 list_add(&dentry->tmp_list, &main_stack);
404 dentry->parent = (void*)sibling_stack_bottom;
405 sibling_stack_bottom = sibling_stack.next;
407 main_dentry = dentry;
408 node = main_dentry->d_inode->children.rb_node;
410 node = dentry->rb_node.rb_right;
420 * Like for_dentry_in_tree(), but the visitor function is always called on a
421 * dentry's children before on itself.
423 int for_dentry_in_tree_depth(struct dentry *root,
424 int (*visitor)(struct dentry*, void*), void *arg)
428 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->children.rb_node,
432 return visitor(root, arg);
436 struct list_head main_stack;
437 struct list_head sibling_stack;
438 struct list_head *sibling_stack_bottom;
439 struct dentry *main_dentry;
440 struct rb_node *node;
441 struct list_head *next_sibling;
442 struct dentry *dentry;
445 sibling_stack_bottom = &sibling_stack;
446 INIT_LIST_HEAD(&main_stack);
447 INIT_LIST_HEAD(&sibling_stack);
449 list_add(&main_dentry->tmp_list, &main_stack);
452 node = main_dentry->d_inode->children.rb_node;
456 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
457 node = node->rb_left;
460 if (node->rb_right) {
461 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
462 node = node->rb_right;
465 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
469 next_sibling = sibling_stack.next;
470 if (next_sibling == sibling_stack_bottom) {
471 main_dentry = container_of(main_stack.next,
474 list_del(&main_dentry->tmp_list);
477 sibling_stack_bottom = (void*)main_dentry->parent;
479 if (main_dentry == root) {
480 main_dentry->parent = main_dentry;
481 ret = visitor(dentry, arg);
484 main_dentry->parent = container_of(main_stack.next,
489 ret = visitor(main_dentry, arg);
492 list_del(&root->tmp_list);
493 list_for_each_entry(dentry, &main_stack, tmp_list) {
494 dentry->parent = container_of(dentry->tmp_list.next,
504 list_del(next_sibling);
505 dentry = container_of(next_sibling, struct dentry, tmp_list);
508 list_add(&dentry->tmp_list, &main_stack);
509 dentry->parent = (void*)sibling_stack_bottom;
510 sibling_stack_bottom = sibling_stack.next;
512 main_dentry = dentry;
519 * Calculate the full path of @dentry, based on its parent's full path and on
520 * its UTF-8 file name.
522 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
526 if (dentry_is_root(dentry)) {
527 full_path = MALLOC(2);
534 char *parent_full_path;
535 u32 parent_full_path_len;
536 const struct dentry *parent = dentry->parent;
538 if (dentry_is_root(parent)) {
539 parent_full_path = "";
540 parent_full_path_len = 0;
542 parent_full_path = parent->full_path_utf8;
543 parent_full_path_len = parent->full_path_utf8_len;
546 full_path_len = parent_full_path_len + 1 +
547 dentry->file_name_utf8_len;
548 full_path = MALLOC(full_path_len + 1);
552 memcpy(full_path, parent_full_path, parent_full_path_len);
553 full_path[parent_full_path_len] = '/';
554 memcpy(full_path + parent_full_path_len + 1,
555 dentry->file_name_utf8,
556 dentry->file_name_utf8_len);
557 full_path[full_path_len] = '\0';
559 FREE(dentry->full_path_utf8);
560 dentry->full_path_utf8 = full_path;
561 dentry->full_path_utf8_len = full_path_len;
564 ERROR("Out of memory while calculating dentry full path");
565 return WIMLIB_ERR_NOMEM;
568 static int increment_subdir_offset(struct dentry *dentry, void *subdir_offset_p)
570 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
574 static int call_calculate_subdir_offsets(struct dentry *dentry,
575 void *subdir_offset_p)
577 calculate_subdir_offsets(dentry, subdir_offset_p);
582 * Recursively calculates the subdir offsets for a directory tree.
584 * @dentry: The root of the directory tree.
585 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
586 * offset for @dentry.
588 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
590 struct rb_node *node;
592 dentry->subdir_offset = *subdir_offset_p;
593 node = dentry->d_inode->children.rb_node;
595 /* Advance the subdir offset by the amount of space the children
596 * of this dentry take up. */
597 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
599 /* End-of-directory dentry on disk. */
600 *subdir_offset_p += 8;
602 /* Recursively call calculate_subdir_offsets() on all the
604 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
606 /* On disk, childless directories have a valid subdir_offset
607 * that points to an 8-byte end-of-directory dentry. Regular
608 * files or reparse points have a subdir_offset of 0. */
609 if (dentry_is_directory(dentry))
610 *subdir_offset_p += 8;
612 dentry->subdir_offset = 0;
616 static int compare_names(const char *name_1, size_t len_1,
617 const char *name_2, size_t len_2)
621 else if (len_1 > len_2)
624 return memcmp(name_1, name_2, len_1);
627 static int dentry_compare_names(const struct dentry *d1, const struct dentry *d2)
629 return compare_names(d1->file_name_utf8, d1->file_name_utf8_len,
630 d2->file_name_utf8, d2->file_name_utf8_len);
634 static struct dentry *
635 get_rbtree_child_with_name(const struct rb_node *node,
636 const char *name, size_t name_len)
639 struct dentry *child = rbnode_dentry(node);
640 int result = compare_names(name, name_len,
641 child->file_name_utf8,
642 child->file_name_utf8_len);
644 node = node->rb_left;
646 node = node->rb_right;
653 /* Returns the child of @dentry that has the file name @name.
654 * Returns NULL if no child has the name. */
655 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
658 struct rb_node *node = dentry->d_inode->children.rb_node;
660 return get_rbtree_child_with_name(node, name, strlen(name));
665 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
666 * @cur_dentry. Returns NULL if no dentry having the path is found. */
667 static struct dentry *get_dentry_relative_path(struct dentry *cur_dentry,
673 struct rb_node *node = cur_dentry->d_inode->children.rb_node;
675 struct dentry *child;
677 const char *new_path;
679 new_path = path_next_part(path, &base_len);
681 child = get_rbtree_child_with_name(node, path, base_len);
683 return get_dentry_relative_path(child, new_path);
688 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
690 struct dentry *get_dentry(WIMStruct *w, const char *path)
692 struct dentry *root = wim_root_dentry(w);
695 return get_dentry_relative_path(root, path);
698 struct inode *wim_pathname_to_inode(WIMStruct *w, const char *path)
700 struct dentry *dentry;
701 dentry = get_dentry(w, path);
703 return dentry->d_inode;
708 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
709 * if the dentry is not found. */
710 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
712 size_t path_len = strlen(path);
713 char buf[path_len + 1];
715 memcpy(buf, path, path_len + 1);
717 to_parent_name(buf, path_len);
719 return get_dentry(w, buf);
722 /* Prints the full path of a dentry. */
723 int print_dentry_full_path(struct dentry *dentry, void *ignore)
725 if (dentry->full_path_utf8)
726 puts(dentry->full_path_utf8);
730 /* We want to be able to show the names of the file attribute flags that are
732 struct file_attr_flag {
736 struct file_attr_flag file_attr_flags[] = {
737 {FILE_ATTRIBUTE_READONLY, "READONLY"},
738 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
739 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
740 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
741 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
742 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
743 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
744 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
745 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
746 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
747 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
748 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
749 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
750 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
751 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
754 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
755 * available. If the dentry is unresolved and the lookup table is NULL, the
756 * lookup table entries will not be printed. Otherwise, they will be. */
757 int print_dentry(struct dentry *dentry, void *lookup_table)
760 struct lookup_table_entry *lte;
761 const struct inode *inode = dentry->d_inode;
765 printf("[DENTRY]\n");
766 printf("Length = %"PRIu64"\n", dentry->length);
767 printf("Attributes = 0x%x\n", inode->attributes);
768 for (unsigned i = 0; i < ARRAY_LEN(file_attr_flags); i++)
769 if (file_attr_flags[i].flag & inode->attributes)
770 printf(" FILE_ATTRIBUTE_%s is set\n",
771 file_attr_flags[i].name);
772 printf("Security ID = %d\n", inode->security_id);
773 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
775 /* Translate the timestamps into something readable */
776 time = wim_timestamp_to_unix(inode->creation_time);
777 p = asctime(gmtime(&time));
778 *(strrchr(p, '\n')) = '\0';
779 printf("Creation Time = %s UTC\n", p);
781 time = wim_timestamp_to_unix(inode->last_access_time);
782 p = asctime(gmtime(&time));
783 *(strrchr(p, '\n')) = '\0';
784 printf("Last Access Time = %s UTC\n", p);
786 time = wim_timestamp_to_unix(inode->last_write_time);
787 p = asctime(gmtime(&time));
788 *(strrchr(p, '\n')) = '\0';
789 printf("Last Write Time = %s UTC\n", p);
791 printf("Reparse Tag = 0x%"PRIx32"\n", inode->reparse_tag);
792 printf("Hard Link Group = 0x%"PRIx64"\n", inode->ino);
793 printf("Hard Link Group Size = %"PRIu32"\n", inode->link_count);
794 printf("Number of Alternate Data Streams = %hu\n", inode->num_ads);
795 printf("Filename = \"");
796 print_string(dentry->file_name, dentry->file_name_len);
798 printf("Filename Length = %hu\n", dentry->file_name_len);
799 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
800 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
801 printf("Short Name = \"");
802 print_string(dentry->short_name, dentry->short_name_len);
804 printf("Short Name Length = %hu\n", dentry->short_name_len);
805 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
806 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
808 print_lookup_table_entry(lte);
810 hash = inode_stream_hash(inode, 0);
818 for (u16 i = 0; i < inode->num_ads; i++) {
819 printf("[Alternate Stream Entry %u]\n", i);
820 printf("Name = \"%s\"\n", inode->ads_entries[i].stream_name_utf8);
821 printf("Name Length (UTF-16) = %u\n",
822 inode->ads_entries[i].stream_name_len);
823 hash = inode_stream_hash(inode, i + 1);
829 print_lookup_table_entry(inode_stream_lte(inode, i + 1,
835 /* Initializations done on every `struct dentry'. */
836 static void dentry_common_init(struct dentry *dentry)
838 memset(dentry, 0, sizeof(struct dentry));
842 static struct inode *new_timeless_inode()
844 struct inode *inode = CALLOC(1, sizeof(struct inode));
846 inode->security_id = -1;
847 inode->link_count = 1;
849 inode->next_stream_id = 1;
851 INIT_LIST_HEAD(&inode->dentry_list);
856 static struct inode *new_inode()
858 struct inode *inode = new_timeless_inode();
860 u64 now = get_wim_timestamp();
861 inode->creation_time = now;
862 inode->last_access_time = now;
863 inode->last_write_time = now;
869 * Creates an unlinked directory entry.
871 * @name: The UTF-8 filename of the new dentry.
873 * Returns a pointer to the new dentry, or NULL if out of memory.
875 struct dentry *new_dentry(const char *name)
877 struct dentry *dentry;
879 dentry = MALLOC(sizeof(struct dentry));
883 dentry_common_init(dentry);
884 if (change_dentry_name(dentry, name) != 0)
887 dentry->parent = dentry;
892 ERROR("Failed to allocate new dentry");
897 static struct dentry *__new_dentry_with_inode(const char *name, bool timeless)
899 struct dentry *dentry;
900 dentry = new_dentry(name);
903 dentry->d_inode = new_timeless_inode();
905 dentry->d_inode = new_inode();
906 if (dentry->d_inode) {
907 inode_add_dentry(dentry, dentry->d_inode);
916 struct dentry *new_dentry_with_timeless_inode(const char *name)
918 return __new_dentry_with_inode(name, true);
921 struct dentry *new_dentry_with_inode(const char *name)
923 return __new_dentry_with_inode(name, false);
927 static int init_ads_entry(struct ads_entry *ads_entry, const char *name)
930 memset(ads_entry, 0, sizeof(*ads_entry));
932 ret = change_ads_name(ads_entry, name);
936 static void destroy_ads_entry(struct ads_entry *ads_entry)
938 FREE(ads_entry->stream_name);
939 FREE(ads_entry->stream_name_utf8);
943 /* Frees an inode. */
944 void free_inode(struct inode *inode)
947 if (inode->ads_entries) {
948 for (u16 i = 0; i < inode->num_ads; i++)
949 destroy_ads_entry(&inode->ads_entries[i]);
950 FREE(inode->ads_entries);
953 wimlib_assert(inode->num_opened_fds == 0);
956 FREE(inode->extracted_file);
961 /* Decrements link count on an inode and frees it if the link count reaches 0.
963 static void put_inode(struct inode *inode)
965 wimlib_assert(inode);
966 wimlib_assert(inode->link_count);
967 if (--inode->link_count == 0) {
969 if (inode->num_opened_fds == 0)
977 /* Frees a WIM dentry.
979 * The inode is freed only if its link count is decremented to 0.
981 void free_dentry(struct dentry *dentry)
983 wimlib_assert(dentry != NULL);
984 FREE(dentry->file_name);
985 FREE(dentry->file_name_utf8);
986 FREE(dentry->short_name);
987 FREE(dentry->full_path_utf8);
989 put_inode(dentry->d_inode);
993 void put_dentry(struct dentry *dentry)
995 wimlib_assert(dentry != NULL);
996 wimlib_assert(dentry->refcnt != 0);
998 if (--dentry->refcnt == 0)
1003 * This function is passed as an argument to for_dentry_in_tree_depth() in order
1004 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
1006 static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
1008 struct lookup_table *lookup_table = __lookup_table;
1012 struct lookup_table_entry *lte;
1013 struct inode *inode = dentry->d_inode;
1014 wimlib_assert(inode->link_count);
1015 for (i = 0; i <= inode->num_ads; i++) {
1016 lte = inode_stream_lte(inode, i, lookup_table);
1018 lte_decrement_refcnt(lte, lookup_table);
1027 * Unlinks and frees a dentry tree.
1029 * @root: The root of the tree.
1030 * @lookup_table: The lookup table for dentries. If non-NULL, the
1031 * reference counts in the lookup table for the lookup
1032 * table entries corresponding to the dentries will be
1035 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
1037 if (!root || !root->parent)
1039 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1042 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
1049 * Links a dentry into the directory tree.
1051 * @dentry: The dentry to link.
1052 * @parent: The dentry that will be the parent of @dentry.
1054 bool dentry_add_child(struct dentry * restrict parent,
1055 struct dentry * restrict child)
1057 wimlib_assert(dentry_is_directory(parent));
1059 struct rb_root *root = &parent->d_inode->children;
1060 struct rb_node **new = &(root->rb_node);
1061 struct rb_node *rb_parent = NULL;
1064 struct dentry *this = rbnode_dentry(*new);
1065 int result = dentry_compare_names(child, this);
1070 new = &((*new)->rb_left);
1071 else if (result > 0)
1072 new = &((*new)->rb_right);
1076 child->parent = parent;
1077 rb_link_node(&child->rb_node, rb_parent, new);
1078 rb_insert_color(&child->rb_node, root);
1084 * Unlink a dentry from the directory tree.
1086 * Note: This merely removes it from the in-memory tree structure.
1088 void unlink_dentry(struct dentry *dentry)
1090 struct dentry *parent = dentry->parent;
1091 if (parent == dentry)
1093 rb_erase(&dentry->rb_node, &parent->d_inode->children);
1097 static inline struct dentry *inode_first_dentry(struct inode *inode)
1099 wimlib_assert(inode->dentry_list.next != &inode->dentry_list);
1100 return container_of(inode->dentry_list.next, struct dentry,
1104 static int verify_inode(struct inode *inode, const WIMStruct *w)
1106 const struct lookup_table *table = w->lookup_table;
1107 const struct wim_security_data *sd = wim_const_security_data(w);
1108 const struct dentry *first_dentry = inode_first_dentry(inode);
1109 int ret = WIMLIB_ERR_INVALID_DENTRY;
1111 /* Check the security ID */
1112 if (inode->security_id < -1) {
1113 ERROR("Dentry `%s' has an invalid security ID (%d)",
1114 first_dentry->full_path_utf8, inode->security_id);
1117 if (inode->security_id >= sd->num_entries) {
1118 ERROR("Dentry `%s' has an invalid security ID (%d) "
1119 "(there are only %u entries in the security table)",
1120 first_dentry->full_path_utf8, inode->security_id,
1125 /* Check that lookup table entries for all the resources exist, except
1126 * if the SHA1 message digest is all 0's, which indicates there is
1127 * intentionally no resource there. */
1128 if (w->hdr.total_parts == 1) {
1129 for (unsigned i = 0; i <= inode->num_ads; i++) {
1130 struct lookup_table_entry *lte;
1132 hash = inode_stream_hash_unresolved(inode, i);
1133 lte = __lookup_resource(table, hash);
1134 if (!lte && !is_zero_hash(hash)) {
1135 ERROR("Could not find lookup table entry for stream "
1136 "%u of dentry `%s'", i, first_dentry->full_path_utf8);
1139 if (lte && (lte->real_refcnt += inode->link_count) > lte->refcnt)
1141 #ifdef ENABLE_ERROR_MESSAGES
1142 WARNING("The following lookup table entry "
1143 "has a reference count of %u, but",
1145 WARNING("We found %u references to it",
1147 WARNING("(One dentry referencing it is at `%s')",
1148 first_dentry->full_path_utf8);
1150 print_lookup_table_entry(lte);
1152 /* Guess what! install.wim for Windows 8
1153 * contains a stream with 2 dentries referencing
1154 * it, but the lookup table entry has reference
1155 * count of 1. So we will need to handle this
1156 * case and not just make it be an error... I'm
1157 * just setting the reference count to the
1158 * number of references we found.
1159 * (Unfortunately, even after doing this, the
1160 * reference count could be too low if it's also
1161 * referenced in other WIM images) */
1164 lte->refcnt = lte->real_refcnt;
1165 WARNING("Fixing reference count");
1173 /* Make sure there is only one un-named stream. */
1174 unsigned num_unnamed_streams = 0;
1175 for (unsigned i = 0; i <= inode->num_ads; i++) {
1177 hash = inode_stream_hash_unresolved(inode, i);
1178 if (!inode_stream_name_len(inode, i) && !is_zero_hash(hash))
1179 num_unnamed_streams++;
1181 if (num_unnamed_streams > 1) {
1182 ERROR("Dentry `%s' has multiple (%u) un-named streams",
1183 first_dentry->full_path_utf8, num_unnamed_streams);
1186 inode->verified = true;
1192 /* Run some miscellaneous verifications on a WIM dentry */
1193 int verify_dentry(struct dentry *dentry, void *wim)
1197 if (!dentry->d_inode->verified) {
1198 ret = verify_inode(dentry->d_inode, wim);
1203 /* Cannot have a short name but no long name */
1204 if (dentry->short_name_len && !dentry->file_name_len) {
1205 ERROR("Dentry `%s' has a short name but no long name",
1206 dentry->full_path_utf8);
1207 return WIMLIB_ERR_INVALID_DENTRY;
1210 /* Make sure root dentry is unnamed */
1211 if (dentry_is_root(dentry)) {
1212 if (dentry->file_name_len) {
1213 ERROR("The root dentry is named `%s', but it must "
1214 "be unnamed", dentry->file_name_utf8);
1215 return WIMLIB_ERR_INVALID_DENTRY;
1220 /* Check timestamps */
1221 if (inode->last_access_time < inode->creation_time ||
1222 inode->last_write_time < inode->creation_time) {
1223 WARNING("Dentry `%s' was created after it was last accessed or "
1224 "written to", dentry->full_path_utf8);
1233 /* Returns the alternate data stream entry belonging to @inode that has the
1234 * stream name @stream_name. */
1235 struct ads_entry *inode_get_ads_entry(struct inode *inode,
1236 const char *stream_name,
1239 size_t stream_name_len;
1242 if (inode->num_ads) {
1244 stream_name_len = strlen(stream_name);
1246 if (ads_entry_has_name(&inode->ads_entries[i],
1247 stream_name, stream_name_len))
1251 return &inode->ads_entries[i];
1253 } while (++i != inode->num_ads);
1259 #if defined(WITH_FUSE) || defined(WITH_NTFS_3G)
1261 * Add an alternate stream entry to an inode and return a pointer to it, or NULL
1262 * if memory could not be allocated.
1264 struct ads_entry *inode_add_ads(struct inode *inode, const char *stream_name)
1267 struct ads_entry *ads_entries;
1268 struct ads_entry *new_entry;
1270 DEBUG("Add alternate data stream \"%s\"", stream_name);
1272 if (inode->num_ads >= 0xfffe) {
1273 ERROR("Too many alternate data streams in one inode!");
1276 num_ads = inode->num_ads + 1;
1277 ads_entries = REALLOC(inode->ads_entries,
1278 num_ads * sizeof(inode->ads_entries[0]));
1280 ERROR("Failed to allocate memory for new alternate data stream");
1283 inode->ads_entries = ads_entries;
1285 new_entry = &inode->ads_entries[num_ads - 1];
1286 if (init_ads_entry(new_entry, stream_name) != 0)
1289 new_entry->stream_id = inode->next_stream_id++;
1291 inode->num_ads = num_ads;
1297 /* Remove an alternate data stream from the inode */
1298 void inode_remove_ads(struct inode *inode, u16 idx,
1299 struct lookup_table *lookup_table)
1301 struct ads_entry *ads_entry;
1302 struct lookup_table_entry *lte;
1304 wimlib_assert(idx < inode->num_ads);
1305 wimlib_assert(inode->resolved);
1307 ads_entry = &inode->ads_entries[idx];
1309 DEBUG("Remove alternate data stream \"%s\"", ads_entry->stream_name_utf8);
1311 lte = ads_entry->lte;
1313 lte_decrement_refcnt(lte, lookup_table);
1315 destroy_ads_entry(ads_entry);
1317 memcpy(&inode->ads_entries[idx],
1318 &inode->ads_entries[idx + 1],
1319 (inode->num_ads - idx - 1) * sizeof(inode->ads_entries[0]));
1327 * Reads the alternate data stream entries for a dentry.
1329 * @p: Pointer to buffer that starts with the first alternate stream entry.
1331 * @inode: Inode to load the alternate data streams into.
1332 * @inode->num_ads must have been set to the number of
1333 * alternate data streams that are expected.
1335 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1338 * The format of the on-disk alternate stream entries is as follows:
1340 * struct ads_entry_on_disk {
1341 * u64 length; // Length of the entry, in bytes. This includes
1342 * all fields (including the stream name and
1343 * null terminator if present, AND the padding!).
1344 * u64 reserved; // Seems to be unused
1345 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
1346 * u16 stream_name_len; // Length of the stream name, in bytes
1347 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
1348 * not including null terminator
1349 * u16 zero; // UTF-16 null terminator for the stream name, NOT
1350 * included in @stream_name_len. Based on what
1351 * I've observed from filenames in dentries,
1352 * this field should not exist when
1353 * (@stream_name_len == 0), but you can't
1354 * actually tell because of the padding anyway
1355 * (provided that the padding is zeroed, which
1356 * it always seems to be).
1357 * char padding[]; // Padding to make the size a multiple of 8 bytes.
1360 * In addition, the entries are 8-byte aligned.
1362 * Return 0 on success or nonzero on failure. On success, inode->ads_entries
1363 * is set to an array of `struct ads_entry's of length inode->num_ads. On
1364 * failure, @inode is not modified.
1366 static int read_ads_entries(const u8 *p, struct inode *inode,
1370 struct ads_entry *ads_entries;
1373 num_ads = inode->num_ads;
1374 ads_entries = CALLOC(num_ads, sizeof(inode->ads_entries[0]));
1376 ERROR("Could not allocate memory for %"PRIu16" "
1377 "alternate data stream entries", num_ads);
1378 return WIMLIB_ERR_NOMEM;
1381 for (u16 i = 0; i < num_ads; i++) {
1382 struct ads_entry *cur_entry;
1384 u64 length_no_padding;
1387 const u8 *p_save = p;
1389 cur_entry = &ads_entries[i];
1392 ads_entries[i].stream_id = i + 1;
1395 /* Read the base stream entry, excluding the stream name. */
1396 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1397 ERROR("Stream entries go past end of metadata resource");
1398 ERROR("(remaining_size = %"PRIu64")", remaining_size);
1399 ret = WIMLIB_ERR_INVALID_DENTRY;
1400 goto out_free_ads_entries;
1403 p = get_u64(p, &length);
1404 p += 8; /* Skip the reserved field */
1405 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
1406 p = get_u16(p, &cur_entry->stream_name_len);
1408 cur_entry->stream_name = NULL;
1409 cur_entry->stream_name_utf8 = NULL;
1411 /* Length including neither the null terminator nor the padding
1413 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1414 cur_entry->stream_name_len;
1416 /* Length including the null terminator and the padding */
1417 total_length = ((length_no_padding + 2) + 7) & ~7;
1419 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1421 if (remaining_size < length_no_padding) {
1422 ERROR("Stream entries go past end of metadata resource");
1423 ERROR("(remaining_size = %"PRIu64" bytes, "
1424 "length_no_padding = %"PRIu64" bytes)",
1425 remaining_size, length_no_padding);
1426 ret = WIMLIB_ERR_INVALID_DENTRY;
1427 goto out_free_ads_entries;
1430 /* The @length field in the on-disk ADS entry is expected to be
1431 * equal to @total_length, which includes all of the entry and
1432 * the padding that follows it to align the next ADS entry to an
1433 * 8-byte boundary. However, to be safe, we'll accept the
1434 * length field as long as it's not less than the un-padded
1435 * total length and not more than the padded total length. */
1436 if (length < length_no_padding || length > total_length) {
1437 ERROR("Stream entry has unexpected length "
1438 "field (length field = %"PRIu64", "
1439 "unpadded total length = %"PRIu64", "
1440 "padded total length = %"PRIu64")",
1441 length, length_no_padding, total_length);
1442 ret = WIMLIB_ERR_INVALID_DENTRY;
1443 goto out_free_ads_entries;
1446 if (cur_entry->stream_name_len) {
1447 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
1448 if (!cur_entry->stream_name) {
1449 ret = WIMLIB_ERR_NOMEM;
1450 goto out_free_ads_entries;
1452 get_bytes(p, cur_entry->stream_name_len,
1453 (u8*)cur_entry->stream_name);
1454 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
1455 cur_entry->stream_name_len,
1457 cur_entry->stream_name_utf8_len = utf8_len;
1459 if (!cur_entry->stream_name_utf8) {
1460 ret = WIMLIB_ERR_NOMEM;
1461 goto out_free_ads_entries;
1464 /* It's expected that the size of every ADS entry is a multiple
1465 * of 8. However, to be safe, I'm allowing the possibility of
1466 * an ADS entry at the very end of the metadata resource ending
1467 * un-aligned. So although we still need to increment the input
1468 * pointer by @total_length to reach the next ADS entry, it's
1469 * possible that less than @total_length is actually remaining
1470 * in the metadata resource. We should set the remaining size to
1471 * 0 bytes if this happens. */
1472 p = p_save + total_length;
1473 if (remaining_size < total_length)
1476 remaining_size -= total_length;
1478 inode->ads_entries = ads_entries;
1480 inode->next_stream_id = inode->num_ads + 1;
1483 out_free_ads_entries:
1484 for (u16 i = 0; i < num_ads; i++)
1485 destroy_ads_entry(&ads_entries[i]);
1491 * Reads a directory entry, including all alternate data stream entries that
1492 * follow it, from the WIM image's metadata resource.
1494 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1495 * @metadata_resource_len: Length of the metadata resource.
1496 * @offset: Offset of this directory entry in the metadata resource.
1497 * @dentry: A `struct dentry' that will be filled in by this function.
1499 * Return 0 on success or nonzero on failure. On failure, @dentry have been
1500 * modified, bu it will be left with no pointers to any allocated buffers.
1501 * On success, the dentry->length field must be examined. If zero, this was a
1502 * special "end of directory" dentry and not a real dentry. If nonzero, this
1503 * was a real dentry.
1505 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1506 u64 offset, struct dentry *dentry)
1509 u64 calculated_size;
1510 char *file_name = NULL;
1511 char *file_name_utf8 = NULL;
1512 char *short_name = NULL;
1515 size_t file_name_utf8_len = 0;
1517 struct inode *inode = NULL;
1519 dentry_common_init(dentry);
1521 /*Make sure the dentry really fits into the metadata resource.*/
1522 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1523 ERROR("Directory entry starting at %"PRIu64" ends past the "
1524 "end of the metadata resource (size %"PRIu64")",
1525 offset, metadata_resource_len);
1526 return WIMLIB_ERR_INVALID_DENTRY;
1529 /* Before reading the whole dentry, we need to read just the length.
1530 * This is because a dentry of length 8 (that is, just the length field)
1531 * terminates the list of sibling directory entries. */
1533 p = get_u64(&metadata_resource[offset], &dentry->length);
1535 /* A zero length field (really a length of 8, since that's how big the
1536 * directory entry is...) indicates that this is the end of directory
1537 * dentry. We do not read it into memory as an actual dentry, so just
1538 * return successfully in that case. */
1539 if (dentry->length == 0)
1542 /* If the dentry does not overflow the metadata resource buffer and is
1543 * not too short, read the rest of it (excluding the alternate data
1544 * streams, but including the file name and short name variable-length
1545 * fields) into memory. */
1546 if (offset + dentry->length >= metadata_resource_len
1547 || offset + dentry->length < offset)
1549 ERROR("Directory entry at offset %"PRIu64" and with size "
1550 "%"PRIu64" ends past the end of the metadata resource "
1552 offset, dentry->length, metadata_resource_len);
1553 return WIMLIB_ERR_INVALID_DENTRY;
1556 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1557 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1559 return WIMLIB_ERR_INVALID_DENTRY;
1562 inode = new_timeless_inode();
1564 return WIMLIB_ERR_NOMEM;
1566 p = get_u32(p, &inode->attributes);
1567 p = get_u32(p, (u32*)&inode->security_id);
1568 p = get_u64(p, &dentry->subdir_offset);
1570 /* 2 unused fields */
1571 p += 2 * sizeof(u64);
1572 /*p = get_u64(p, &dentry->unused1);*/
1573 /*p = get_u64(p, &dentry->unused2);*/
1575 p = get_u64(p, &inode->creation_time);
1576 p = get_u64(p, &inode->last_access_time);
1577 p = get_u64(p, &inode->last_write_time);
1579 p = get_bytes(p, SHA1_HASH_SIZE, inode->hash);
1582 * I don't know what's going on here. It seems like M$ screwed up the
1583 * reparse points, then put the fields in the same place and didn't
1584 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1585 * have something to do with this, but it's not documented.
1587 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1590 p = get_u32(p, &inode->reparse_tag);
1593 p = get_u32(p, &inode->reparse_tag);
1594 p = get_u64(p, &inode->ino);
1597 /* By the way, the reparse_reserved field does not actually exist (at
1598 * least when the file is not a reparse point) */
1600 p = get_u16(p, &inode->num_ads);
1602 p = get_u16(p, &short_name_len);
1603 p = get_u16(p, &file_name_len);
1605 /* We now know the length of the file name and short name. Make sure
1606 * the length of the dentry is large enough to actually hold them.
1608 * The calculated length here is unaligned to allow for the possibility
1609 * that the dentry->length names an unaligned length, although this
1610 * would be unexpected. */
1611 calculated_size = __dentry_correct_length_unaligned(file_name_len,
1614 if (dentry->length < calculated_size) {
1615 ERROR("Unexpected end of directory entry! (Expected "
1616 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1617 "short_name_len = %hu, file_name_len = %hu)",
1618 calculated_size, dentry->length,
1619 short_name_len, file_name_len);
1620 ret = WIMLIB_ERR_INVALID_DENTRY;
1621 goto out_free_inode;
1624 /* Read the filename if present. Note: if the filename is empty, there
1625 * is no null terminator following it. */
1626 if (file_name_len) {
1627 file_name = MALLOC(file_name_len);
1629 ERROR("Failed to allocate %hu bytes for dentry file name",
1631 ret = WIMLIB_ERR_NOMEM;
1632 goto out_free_inode;
1634 p = get_bytes(p, file_name_len, file_name);
1636 /* Convert filename to UTF-8. */
1637 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1638 &file_name_utf8_len);
1640 if (!file_name_utf8) {
1641 ERROR("Failed to allocate memory to convert UTF-16 "
1642 "filename (%hu bytes) to UTF-8", file_name_len);
1643 ret = WIMLIB_ERR_NOMEM;
1644 goto out_free_file_name;
1647 WARNING("Expected two zero bytes following the file name "
1648 "`%s', but found non-zero bytes", file_name_utf8);
1652 /* Align the calculated size */
1653 calculated_size = (calculated_size + 7) & ~7;
1655 if (dentry->length > calculated_size) {
1656 /* Weird; the dentry says it's longer than it should be. Note
1657 * that the length field does NOT include the size of the
1658 * alternate stream entries. */
1660 /* Strangely, some directory entries inexplicably have a little
1661 * over 70 bytes of extra data. The exact amount of data seems
1662 * to be 72 bytes, but it is aligned on the next 8-byte
1663 * boundary. It does NOT seem to be alternate data stream
1664 * entries. Here's an example of the aligned data:
1666 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1667 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1668 * 00000000 00000000 00000000 00000000
1670 * Here's one interpretation of how the data is laid out.
1673 * u32 field1; (always 0x00000001)
1674 * u32 field2; (always 0x40000000)
1675 * u8 data[48]; (???)
1676 * u64 reserved1; (always 0)
1677 * u64 reserved2; (always 0)
1679 DEBUG("Dentry for file or directory `%s' has %zu extra "
1681 file_name_utf8, dentry->length - calculated_size);
1684 /* Read the short filename if present. Note: if there is no short
1685 * filename, there is no null terminator following it. */
1686 if (short_name_len) {
1687 short_name = MALLOC(short_name_len);
1689 ERROR("Failed to allocate %hu bytes for short filename",
1691 ret = WIMLIB_ERR_NOMEM;
1692 goto out_free_file_name_utf8;
1695 p = get_bytes(p, short_name_len, short_name);
1697 WARNING("Expected two zero bytes following the short name of "
1698 "`%s', but found non-zero bytes", file_name_utf8);
1703 * Read the alternate data streams, if present. dentry->num_ads tells
1704 * us how many they are, and they will directly follow the dentry
1707 * Note that each alternate data stream entry begins on an 8-byte
1708 * aligned boundary, and the alternate data stream entries are NOT
1709 * included in the dentry->length field for some reason.
1711 if (inode->num_ads != 0) {
1713 /* Trying different lengths is just a hack to make sure we have
1714 * a chance of reading the ADS entries correctly despite the
1715 * poor documentation. */
1717 if (calculated_size != dentry->length) {
1718 WARNING("Trying calculated dentry length (%"PRIu64") "
1719 "instead of dentry->length field (%"PRIu64") "
1720 "to read ADS entries",
1721 calculated_size, dentry->length);
1723 u64 lengths_to_try[3] = {calculated_size,
1724 (dentry->length + 7) & ~7,
1726 ret = WIMLIB_ERR_INVALID_DENTRY;
1727 for (size_t i = 0; i < ARRAY_LEN(lengths_to_try); i++) {
1728 if (lengths_to_try[i] > metadata_resource_len - offset)
1730 ret = read_ads_entries(&metadata_resource[offset + lengths_to_try[i]],
1732 metadata_resource_len - offset - lengths_to_try[i]);
1736 ERROR("Failed to read alternate data stream "
1737 "entries of `%s'", dentry->file_name_utf8);
1738 goto out_free_short_name;
1742 /* We've read all the data for this dentry. Set the names and their
1743 * lengths, and we've done. */
1744 dentry->d_inode = inode;
1745 dentry->file_name = file_name;
1746 dentry->file_name_utf8 = file_name_utf8;
1747 dentry->short_name = short_name;
1748 dentry->file_name_len = file_name_len;
1749 dentry->file_name_utf8_len = file_name_utf8_len;
1750 dentry->short_name_len = short_name_len;
1752 out_free_short_name:
1754 out_free_file_name_utf8:
1755 FREE(file_name_utf8);
1763 /* Reads the children of a dentry, and all their children, ..., etc. from the
1764 * metadata resource and into the dentry tree.
1766 * @metadata_resource: An array that contains the uncompressed metadata
1767 * resource for the WIM file.
1769 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1772 * @dentry: A pointer to a `struct dentry' that is the root of the directory
1773 * tree and has already been read from the metadata resource. It
1774 * does not need to be the real root because this procedure is
1775 * called recursively.
1777 * @return: Zero on success, nonzero on failure.
1779 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1780 struct dentry *dentry)
1782 u64 cur_offset = dentry->subdir_offset;
1783 struct dentry *prev_child = NULL;
1784 struct dentry *first_child = NULL;
1785 struct dentry *child;
1786 struct dentry cur_child;
1790 * If @dentry has no child dentries, nothing more needs to be done for
1791 * this branch. This is the case for regular files, symbolic links, and
1792 * *possibly* empty directories (although an empty directory may also
1793 * have one child dentry that is the special end-of-directory dentry)
1795 if (cur_offset == 0)
1798 /* Find and read all the children of @dentry. */
1801 /* Read next child of @dentry into @cur_child. */
1802 ret = read_dentry(metadata_resource, metadata_resource_len,
1803 cur_offset, &cur_child);
1807 /* Check for end of directory. */
1808 if (cur_child.length == 0)
1811 /* Not end of directory. Allocate this child permanently and
1812 * link it to the parent and previous child. */
1813 child = MALLOC(sizeof(struct dentry));
1815 ERROR("Failed to allocate %zu bytes for new dentry",
1816 sizeof(struct dentry));
1817 ret = WIMLIB_ERR_NOMEM;
1820 memcpy(child, &cur_child, sizeof(struct dentry));
1822 dentry_add_child(dentry, child);
1824 inode_add_dentry(child, child->d_inode);
1826 /* If there are children of this child, call this procedure
1828 if (child->subdir_offset != 0) {
1829 ret = read_dentry_tree(metadata_resource,
1830 metadata_resource_len, child);
1835 /* Advance to the offset of the next child. Note: We need to
1836 * advance by the TOTAL length of the dentry, not by the length
1837 * child->length, which although it does take into account the
1838 * padding, it DOES NOT take into account alternate stream
1840 cur_offset += dentry_total_length(child);
1846 * Writes a WIM dentry to an output buffer.
1848 * @dentry: The dentry structure.
1849 * @p: The memory location to write the data to.
1850 * @return: Pointer to the byte after the last byte we wrote as part of the
1853 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1857 const struct inode *inode = dentry->d_inode;
1859 /* We calculate the correct length of the dentry ourselves because the
1860 * dentry->length field may been set to an unexpected value from when we
1861 * read the dentry in (for example, there may have been unknown data
1862 * appended to the end of the dentry...) */
1863 u64 length = dentry_correct_length(dentry);
1865 p = put_u64(p, length);
1866 p = put_u32(p, inode->attributes);
1867 p = put_u32(p, inode->security_id);
1868 p = put_u64(p, dentry->subdir_offset);
1869 p = put_u64(p, 0); /* unused1 */
1870 p = put_u64(p, 0); /* unused2 */
1871 p = put_u64(p, inode->creation_time);
1872 p = put_u64(p, inode->last_access_time);
1873 p = put_u64(p, inode->last_write_time);
1874 hash = inode_stream_hash(inode, 0);
1875 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1876 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1877 p = put_zeroes(p, 4);
1878 p = put_u32(p, inode->reparse_tag);
1879 p = put_zeroes(p, 4);
1883 if (inode->link_count == 1)
1886 link_group_id = inode->ino;
1887 p = put_u64(p, link_group_id);
1889 p = put_u16(p, inode->num_ads);
1890 p = put_u16(p, dentry->short_name_len);
1891 p = put_u16(p, dentry->file_name_len);
1892 if (dentry->file_name_len) {
1893 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1894 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1896 if (dentry->short_name) {
1897 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1898 p = put_u16(p, 0); /* short name padding, 2 bytes */
1901 /* Align to 8-byte boundary */
1902 wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1903 p = put_zeroes(p, length - (p - orig_p));
1905 /* Write the alternate data streams, if there are any. Please see
1906 * read_ads_entries() for comments about the format of the on-disk
1907 * alternate data stream entries. */
1908 for (u16 i = 0; i < inode->num_ads; i++) {
1909 p = put_u64(p, ads_entry_total_length(&inode->ads_entries[i]));
1910 p = put_u64(p, 0); /* Unused */
1911 hash = inode_stream_hash(inode, i + 1);
1912 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1913 p = put_u16(p, inode->ads_entries[i].stream_name_len);
1914 if (inode->ads_entries[i].stream_name_len) {
1915 p = put_bytes(p, inode->ads_entries[i].stream_name_len,
1916 (u8*)inode->ads_entries[i].stream_name);
1919 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1921 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1925 static int write_dentry_cb(struct dentry *dentry, void *_p)
1928 *p = write_dentry(dentry, *p);
1932 static u8 *write_dentry_tree_recursive(const struct dentry *parent, u8 *p);
1934 static int write_dentry_tree_recursive_cb(struct dentry *dentry, void *_p)
1937 *p = write_dentry_tree_recursive(dentry, *p);
1941 /* Recursive function that writes a dentry tree rooted at @parent, not including
1942 * @parent itself, which has already been written. */
1943 static u8 *write_dentry_tree_recursive(const struct dentry *parent, u8 *p)
1945 /* Nothing to do if this dentry has no children. */
1946 if (parent->subdir_offset == 0)
1949 /* Write child dentries and end-of-directory entry.
1951 * Note: we need to write all of this dentry's children before
1952 * recursively writing the directory trees rooted at each of the child
1953 * dentries, since the on-disk dentries for a dentry's children are
1954 * always located at consecutive positions in the metadata resource! */
1955 for_dentry_in_rbtree(parent->d_inode->children.rb_node, write_dentry_cb, &p);
1957 /* write end of directory entry */
1960 /* Recurse on children. */
1961 for_dentry_in_rbtree(parent->d_inode->children.rb_node,
1962 write_dentry_tree_recursive_cb, &p);
1966 /* Writes a directory tree to the metadata resource.
1968 * @root: Root of the dentry tree.
1969 * @p: Pointer to a buffer with enough space for the dentry tree.
1971 * Returns pointer to the byte after the last byte we wrote.
1973 u8 *write_dentry_tree(const struct dentry *root, u8 *p)
1975 DEBUG("Writing dentry tree.");
1976 wimlib_assert(dentry_is_root(root));
1978 /* If we're the root dentry, we have no parent that already
1979 * wrote us, so we need to write ourselves. */
1980 p = write_dentry(root, p);
1982 /* Write end of directory entry after the root dentry just to be safe;
1983 * however the root dentry obviously cannot have any siblings. */
1986 /* Recursively write the rest of the dentry tree. */
1987 return write_dentry_tree_recursive(root, p);