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/.
36 #include "buffer_io.h"
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 the alternate data stream entry @entry has the UTF-8 stream
75 * name @name that has length @name_len bytes. */
76 static inline bool ads_entry_has_name(const struct ads_entry *entry,
77 const char *name, size_t name_len)
79 if (entry->stream_name_utf8_len != name_len)
81 return memcmp(entry->stream_name_utf8, name, name_len) == 0;
84 /* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
85 * and their lengths in the pointer arguments */
86 int get_names(char **name_utf16_ret, char **name_utf8_ret,
87 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
92 char *name_utf16, *name_utf8;
94 utf8_len = strlen(name);
96 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
99 return WIMLIB_ERR_NOMEM;
101 name_utf8 = MALLOC(utf8_len + 1);
104 return WIMLIB_ERR_NOMEM;
106 memcpy(name_utf8, name, utf8_len + 1);
107 FREE(*name_utf8_ret);
108 FREE(*name_utf16_ret);
109 *name_utf8_ret = name_utf8;
110 *name_utf16_ret = name_utf16;
111 *name_utf8_len_ret = utf8_len;
112 *name_utf16_len_ret = utf16_len;
116 /* Changes the name of a dentry to @new_name. Only changes the file_name and
117 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
118 * full_path_utf8 fields. Also recalculates its length. */
119 static int change_dentry_name(struct dentry *dentry, const char *new_name)
123 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
124 &dentry->file_name_len, &dentry->file_name_utf8_len,
126 FREE(dentry->short_name);
127 dentry->short_name_len = 0;
129 dentry->length = dentry_correct_length(dentry);
134 * Changes the name of an alternate data stream */
135 static int change_ads_name(struct ads_entry *entry, const char *new_name)
137 return get_names(&entry->stream_name, &entry->stream_name_utf8,
138 &entry->stream_name_len,
139 &entry->stream_name_utf8_len,
143 /* Returns the total length of a WIM alternate data stream entry on-disk,
144 * including the stream name, the null terminator, AND the padding after the
145 * entry to align the next one (or the next dentry) on an 8-byte boundary. */
146 static u64 ads_entry_total_length(const struct ads_entry *entry)
148 u64 len = WIM_ADS_ENTRY_DISK_SIZE;
149 if (entry->stream_name_len)
150 len += entry->stream_name_len + 2;
151 return (len + 7) & ~7;
155 static u64 __dentry_total_length(const struct dentry *dentry, u64 length)
157 const struct inode *inode = dentry->d_inode;
158 for (u16 i = 0; i < inode->num_ads; i++)
159 length += ads_entry_total_length(&inode->ads_entries[i]);
160 return (length + 7) & ~7;
163 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
164 * all alternate data streams. */
165 u64 dentry_correct_total_length(const struct dentry *dentry)
167 return __dentry_total_length(dentry,
168 dentry_correct_length_unaligned(dentry));
171 /* Like dentry_correct_total_length(), but use the existing dentry->length field
172 * instead of calculating its "correct" value. */
173 static u64 dentry_total_length(const struct dentry *dentry)
175 return __dentry_total_length(dentry, dentry->length);
179 /* Transfers file attributes from a struct inode to a `stat' buffer.
181 * The lookup table entry tells us which stream in the inode we are statting.
182 * For a named data stream, everything returned is the same as the unnamed data
183 * stream except possibly the size and block count. */
184 int inode_to_stbuf(const struct inode *inode, struct lookup_table_entry *lte,
187 if (inode_is_symlink(inode))
188 stbuf->st_mode = S_IFLNK | 0777;
189 else if (inode_is_directory(inode))
190 stbuf->st_mode = S_IFDIR | 0755;
192 stbuf->st_mode = S_IFREG | 0755;
194 stbuf->st_ino = (ino_t)inode->ino;
195 stbuf->st_nlink = inode->link_count;
196 stbuf->st_uid = getuid();
197 stbuf->st_gid = getgid();
200 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
201 wimlib_assert(lte->staging_file_name);
202 struct stat native_stat;
203 if (stat(lte->staging_file_name, &native_stat) != 0) {
204 DEBUG("Failed to stat `%s': %m",
205 lte->staging_file_name);
208 stbuf->st_size = native_stat.st_size;
210 stbuf->st_size = wim_resource_size(lte);
216 stbuf->st_atime = wim_timestamp_to_unix(inode->last_access_time);
217 stbuf->st_mtime = wim_timestamp_to_unix(inode->last_write_time);
218 stbuf->st_ctime = wim_timestamp_to_unix(inode->creation_time);
219 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
224 int for_dentry_in_rbtree(struct rb_node *root,
225 int (*visitor)(struct dentry *, void *),
229 struct rb_node *node = root;
233 list_add(&rbnode_dentry(node)->tmp_list, &stack);
234 node = node->rb_left;
236 struct list_head *next;
237 struct dentry *dentry;
242 dentry = container_of(next, struct dentry, tmp_list);
244 ret = visitor(dentry, arg);
247 node = dentry->rb_node.rb_right;
252 static int for_dentry_tree_in_rbtree_depth(struct rb_node *node,
253 int (*visitor)(struct dentry*, void*),
258 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
262 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
266 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
273 /*#define RECURSIVE_FOR_DENTRY_IN_TREE*/
275 #ifdef RECURSIVE_FOR_DENTRY_IN_TREE
276 static int for_dentry_tree_in_rbtree(struct rb_node *node,
277 int (*visitor)(struct dentry*, void*),
282 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
285 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
288 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
297 * Calls a function on all directory entries in a WIM dentry tree. Logically,
298 * this is a pre-order traversal (the function is called on a parent dentry
299 * before its children), but sibling dentries will be visited in order as well.
301 * In reality, the data structures are more complicated than the above might
302 * suggest because there is a separate red-black tree for each dentry that
303 * contains its direct children.
305 int for_dentry_in_tree(struct dentry *root,
306 int (*visitor)(struct dentry*, void*), void *arg)
308 #ifdef RECURSIVE_FOR_DENTRY_IN_TREE
309 int ret = visitor(root, arg);
312 return for_dentry_tree_in_rbtree(root->d_inode->children.rb_node, visitor, arg);
315 struct list_head main_stack;
316 struct list_head sibling_stack;
317 struct list_head *sibling_stack_bottom;
318 struct dentry *main_dentry;
319 struct rb_node *node;
320 struct list_head *next_sibling;
321 struct dentry *dentry;
323 ret = visitor(root, arg);
328 sibling_stack_bottom = &sibling_stack;
329 INIT_LIST_HEAD(&main_stack);
330 INIT_LIST_HEAD(&sibling_stack);
332 list_add(&root->tmp_list, &main_stack);
333 node = root->d_inode->children.rb_node;
336 // Prepare for non-recursive in-order traversal of the red-black
337 // tree of this dentry's children
340 // Push this node to the sibling stack and examine the
341 // left neighbor, if any
342 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
343 node = node->rb_left;
346 next_sibling = sibling_stack.next;
347 if (next_sibling == sibling_stack_bottom) {
348 // Done with all siblings. Pop the main dentry to move
349 // back up one level.
350 main_dentry = container_of(main_stack.next,
353 list_del(&main_dentry->tmp_list);
355 if (main_dentry == root)
358 // Restore sibling stack bottom from the previous level
359 sibling_stack_bottom = (void*)main_dentry->parent;
361 // Restore the just-popped main dentry's parent
362 main_dentry->parent = container_of(main_stack.next,
366 // The next sibling to traverse in the previous level,
367 // in the in-order traversal of the red-black tree, is
368 // the one to the right.
369 node = main_dentry->rb_node.rb_right;
371 // The sibling stack is not empty, so there are more to
374 // Pop a sibling from the stack.
375 list_del(next_sibling);
376 dentry = container_of(next_sibling, struct dentry, tmp_list);
378 // Visit the sibling.
379 ret = visitor(dentry, arg);
381 // Failed. Restore parent pointers for the
382 // dentries in the main stack
383 list_for_each_entry(dentry, &main_stack, tmp_list) {
384 dentry->parent = container_of(dentry->tmp_list.next,
391 // We'd like to recursively visit the dentry tree rooted
392 // at this sibling. To do this, add it to the main
393 // stack, save the bottom of this level's sibling stack
394 // in the dentry->parent field, re-set the bottom of the
395 // sibling stack to be its current height, and set
396 // main_dentry to the sibling so it becomes the parent
397 // dentry in the next iteration through the outer loop.
398 if (inode_has_children(dentry->d_inode)) {
399 list_add(&dentry->tmp_list, &main_stack);
400 dentry->parent = (void*)sibling_stack_bottom;
401 sibling_stack_bottom = sibling_stack.next;
403 main_dentry = dentry;
404 node = main_dentry->d_inode->children.rb_node;
406 node = dentry->rb_node.rb_right;
417 * Like for_dentry_in_tree(), but the visitor function is always called on a
418 * dentry's children before on itself.
420 int for_dentry_in_tree_depth(struct dentry *root,
421 int (*visitor)(struct dentry*, void*), void *arg)
425 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->children.rb_node,
429 return visitor(root, arg);
433 struct list_head main_stack;
434 struct list_head sibling_stack;
435 struct list_head *sibling_stack_bottom;
436 struct dentry *main_dentry;
437 struct rb_node *node;
438 struct list_head *next_sibling;
439 struct dentry *dentry;
442 sibling_stack_bottom = &sibling_stack;
443 INIT_LIST_HEAD(&main_stack);
444 INIT_LIST_HEAD(&sibling_stack);
446 list_add(&main_dentry->tmp_list, &main_stack);
449 node = main_dentry->d_inode->children.rb_node;
453 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
454 node = node->rb_left;
457 if (node->rb_right) {
458 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
459 node = node->rb_right;
462 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
466 next_sibling = sibling_stack.next;
467 if (next_sibling == sibling_stack_bottom) {
468 main_dentry = container_of(main_stack.next,
471 list_del(&main_dentry->tmp_list);
474 sibling_stack_bottom = (void*)main_dentry->parent;
476 if (main_dentry == root) {
477 main_dentry->parent = main_dentry;
478 ret = visitor(dentry, arg);
481 main_dentry->parent = container_of(main_stack.next,
486 ret = visitor(main_dentry, arg);
489 list_del(&root->tmp_list);
490 list_for_each_entry(dentry, &main_stack, tmp_list) {
491 dentry->parent = container_of(dentry->tmp_list.next,
501 list_del(next_sibling);
502 dentry = container_of(next_sibling, struct dentry, tmp_list);
505 list_add(&dentry->tmp_list, &main_stack);
506 dentry->parent = (void*)sibling_stack_bottom;
507 sibling_stack_bottom = sibling_stack.next;
509 main_dentry = dentry;
516 * Calculate the full path of @dentry, based on its parent's full path and on
517 * its UTF-8 file name.
519 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
523 if (dentry_is_root(dentry)) {
524 full_path = MALLOC(2);
531 char *parent_full_path;
532 u32 parent_full_path_len;
533 const struct dentry *parent = dentry->parent;
535 if (dentry_is_root(parent)) {
536 parent_full_path = "";
537 parent_full_path_len = 0;
539 parent_full_path = parent->full_path_utf8;
540 parent_full_path_len = parent->full_path_utf8_len;
543 full_path_len = parent_full_path_len + 1 +
544 dentry->file_name_utf8_len;
545 full_path = MALLOC(full_path_len + 1);
549 memcpy(full_path, parent_full_path, parent_full_path_len);
550 full_path[parent_full_path_len] = '/';
551 memcpy(full_path + parent_full_path_len + 1,
552 dentry->file_name_utf8,
553 dentry->file_name_utf8_len);
554 full_path[full_path_len] = '\0';
556 FREE(dentry->full_path_utf8);
557 dentry->full_path_utf8 = full_path;
558 dentry->full_path_utf8_len = full_path_len;
561 ERROR("Out of memory while calculating dentry full path");
562 return WIMLIB_ERR_NOMEM;
565 static int increment_subdir_offset(struct dentry *dentry, void *subdir_offset_p)
567 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
571 static int call_calculate_subdir_offsets(struct dentry *dentry,
572 void *subdir_offset_p)
574 calculate_subdir_offsets(dentry, subdir_offset_p);
579 * Recursively calculates the subdir offsets for a directory tree.
581 * @dentry: The root of the directory tree.
582 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
583 * offset for @dentry.
585 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
587 struct rb_node *node;
589 dentry->subdir_offset = *subdir_offset_p;
590 node = dentry->d_inode->children.rb_node;
592 /* Advance the subdir offset by the amount of space the children
593 * of this dentry take up. */
594 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
596 /* End-of-directory dentry on disk. */
597 *subdir_offset_p += 8;
599 /* Recursively call calculate_subdir_offsets() on all the
601 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
603 /* On disk, childless directories have a valid subdir_offset
604 * that points to an 8-byte end-of-directory dentry. Regular
605 * files or reparse points have a subdir_offset of 0. */
606 if (dentry_is_directory(dentry))
607 *subdir_offset_p += 8;
609 dentry->subdir_offset = 0;
613 static int compare_names(const char *name_1, u16 len_1,
614 const char *name_2, u16 len_2)
616 int result = strncasecmp(name_1, name_2, min(len_1, len_2));
620 return (int)len_1 - (int)len_2;
624 static int dentry_compare_names(const struct dentry *d1, const struct dentry *d2)
626 return compare_names(d1->file_name_utf8, d1->file_name_utf8_len,
627 d2->file_name_utf8, d2->file_name_utf8_len);
631 static struct dentry *
632 get_rbtree_child_with_name(const struct rb_node *node,
633 const char *name, size_t name_len)
636 struct dentry *child = rbnode_dentry(node);
637 int result = compare_names(name, name_len,
638 child->file_name_utf8,
639 child->file_name_utf8_len);
641 node = node->rb_left;
643 node = node->rb_right;
650 /* Returns the child of @dentry that has the file name @name.
651 * Returns NULL if no child has the name. */
652 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
655 struct rb_node *node = dentry->d_inode->children.rb_node;
657 return get_rbtree_child_with_name(node, name, strlen(name));
662 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
663 * @cur_dentry. Returns NULL if no dentry having the path is found. */
664 static struct dentry *get_dentry_relative_path(struct dentry *cur_dentry,
670 struct rb_node *node = cur_dentry->d_inode->children.rb_node;
672 struct dentry *child;
674 const char *new_path;
676 new_path = path_next_part(path, &base_len);
678 child = get_rbtree_child_with_name(node, path, base_len);
680 return get_dentry_relative_path(child, new_path);
685 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
687 struct dentry *get_dentry(WIMStruct *w, const char *path)
689 struct dentry *root = wim_root_dentry(w);
692 return get_dentry_relative_path(root, path);
695 struct inode *wim_pathname_to_inode(WIMStruct *w, const char *path)
697 struct dentry *dentry;
698 dentry = get_dentry(w, path);
700 return dentry->d_inode;
705 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
706 * if the dentry is not found. */
707 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
709 size_t path_len = strlen(path);
710 char buf[path_len + 1];
712 memcpy(buf, path, path_len + 1);
714 to_parent_name(buf, path_len);
716 return get_dentry(w, buf);
719 /* Prints the full path of a dentry. */
720 int print_dentry_full_path(struct dentry *dentry, void *ignore)
722 if (dentry->full_path_utf8)
723 puts(dentry->full_path_utf8);
727 /* We want to be able to show the names of the file attribute flags that are
729 struct file_attr_flag {
733 struct file_attr_flag file_attr_flags[] = {
734 {FILE_ATTRIBUTE_READONLY, "READONLY"},
735 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
736 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
737 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
738 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
739 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
740 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
741 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
742 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
743 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
744 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
745 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
746 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
747 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
748 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
751 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
752 * available. If the dentry is unresolved and the lookup table is NULL, the
753 * lookup table entries will not be printed. Otherwise, they will be. */
754 int print_dentry(struct dentry *dentry, void *lookup_table)
757 struct lookup_table_entry *lte;
758 const struct inode *inode = dentry->d_inode;
761 printf("[DENTRY]\n");
762 printf("Length = %"PRIu64"\n", dentry->length);
763 printf("Attributes = 0x%x\n", inode->attributes);
764 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
765 if (file_attr_flags[i].flag & inode->attributes)
766 printf(" FILE_ATTRIBUTE_%s is set\n",
767 file_attr_flags[i].name);
768 printf("Security ID = %d\n", inode->security_id);
769 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
771 wim_timestamp_to_str(inode->creation_time, buf, sizeof(buf));
772 printf("Creation Time = %s\n", buf);
774 wim_timestamp_to_str(inode->last_access_time, buf, sizeof(buf));
775 printf("Last Access Time = %s\n", buf);
777 wim_timestamp_to_str(inode->last_write_time, buf, sizeof(buf));
778 printf("Last Write Time = %s\n", buf);
780 printf("Reparse Tag = 0x%"PRIx32"\n", inode->reparse_tag);
781 printf("Hard Link Group = 0x%"PRIx64"\n", inode->ino);
782 printf("Hard Link Group Size = %"PRIu32"\n", inode->link_count);
783 printf("Number of Alternate Data Streams = %hu\n", inode->num_ads);
784 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
785 /*printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);*/
786 printf("Short Name (UTF-16LE) = \"");
787 print_string(dentry->short_name, dentry->short_name_len);
789 /*printf("Short Name Length = %hu\n", dentry->short_name_len);*/
790 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
791 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
793 print_lookup_table_entry(lte);
795 hash = inode_stream_hash(inode, 0);
803 for (u16 i = 0; i < inode->num_ads; i++) {
804 printf("[Alternate Stream Entry %u]\n", i);
805 printf("Name = \"%s\"\n", inode->ads_entries[i].stream_name_utf8);
806 printf("Name Length (UTF-16) = %u\n",
807 inode->ads_entries[i].stream_name_len);
808 hash = inode_stream_hash(inode, i + 1);
814 print_lookup_table_entry(inode_stream_lte(inode, i + 1,
820 /* Initializations done on every `struct dentry'. */
821 static void dentry_common_init(struct dentry *dentry)
823 memset(dentry, 0, sizeof(struct dentry));
827 static struct inode *new_timeless_inode()
829 struct inode *inode = CALLOC(1, sizeof(struct inode));
831 inode->security_id = -1;
832 inode->link_count = 1;
834 inode->next_stream_id = 1;
835 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
836 ERROR_WITH_ERRNO("Error initializing mutex");
841 INIT_LIST_HEAD(&inode->dentry_list);
846 static struct inode *new_inode()
848 struct inode *inode = new_timeless_inode();
850 u64 now = get_wim_timestamp();
851 inode->creation_time = now;
852 inode->last_access_time = now;
853 inode->last_write_time = now;
859 * Creates an unlinked directory entry.
861 * @name: The UTF-8 filename of the new dentry.
863 * Returns a pointer to the new dentry, or NULL if out of memory.
865 struct dentry *new_dentry(const char *name)
867 struct dentry *dentry;
869 dentry = MALLOC(sizeof(struct dentry));
873 dentry_common_init(dentry);
874 if (change_dentry_name(dentry, name) != 0)
877 dentry->parent = dentry;
882 ERROR("Failed to allocate new dentry");
887 static struct dentry *__new_dentry_with_inode(const char *name, bool timeless)
889 struct dentry *dentry;
890 dentry = new_dentry(name);
893 dentry->d_inode = new_timeless_inode();
895 dentry->d_inode = new_inode();
896 if (dentry->d_inode) {
897 inode_add_dentry(dentry, dentry->d_inode);
906 struct dentry *new_dentry_with_timeless_inode(const char *name)
908 return __new_dentry_with_inode(name, true);
911 struct dentry *new_dentry_with_inode(const char *name)
913 return __new_dentry_with_inode(name, false);
917 static int init_ads_entry(struct ads_entry *ads_entry, const char *name)
920 memset(ads_entry, 0, sizeof(*ads_entry));
922 ret = change_ads_name(ads_entry, name);
926 static void destroy_ads_entry(struct ads_entry *ads_entry)
928 FREE(ads_entry->stream_name);
929 FREE(ads_entry->stream_name_utf8);
933 /* Frees an inode. */
934 void free_inode(struct inode *inode)
937 if (inode->ads_entries) {
938 for (u16 i = 0; i < inode->num_ads; i++)
939 destroy_ads_entry(&inode->ads_entries[i]);
940 FREE(inode->ads_entries);
943 wimlib_assert(inode->num_opened_fds == 0);
945 pthread_mutex_destroy(&inode->i_mutex);
947 FREE(inode->extracted_file);
952 /* Decrements link count on an inode and frees it if the link count reaches 0.
954 static void put_inode(struct inode *inode)
956 wimlib_assert(inode->link_count != 0);
957 if (--inode->link_count == 0) {
959 if (inode->num_opened_fds == 0)
967 /* Frees a WIM dentry.
969 * The inode is freed only if its link count is decremented to 0.
971 void free_dentry(struct dentry *dentry)
973 FREE(dentry->file_name);
974 FREE(dentry->file_name_utf8);
975 FREE(dentry->short_name);
976 FREE(dentry->full_path_utf8);
978 put_inode(dentry->d_inode);
982 void put_dentry(struct dentry *dentry)
984 wimlib_assert(dentry->refcnt != 0);
985 if (--dentry->refcnt == 0)
990 * This function is passed as an argument to for_dentry_in_tree_depth() in order
991 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
993 static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
995 struct lookup_table *lookup_table = __lookup_table;
999 struct lookup_table_entry *lte;
1000 struct inode *inode = dentry->d_inode;
1001 wimlib_assert(inode->link_count != 0);
1002 for (i = 0; i <= inode->num_ads; i++) {
1003 lte = inode_stream_lte(inode, i, lookup_table);
1005 lte_decrement_refcnt(lte, lookup_table);
1014 * Unlinks and frees a dentry tree.
1016 * @root: The root of the tree.
1017 * @lookup_table: The lookup table for dentries. If non-NULL, the
1018 * reference counts in the lookup table for the lookup
1019 * table entries corresponding to the dentries will be
1022 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
1025 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1028 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
1035 * Links a dentry into the directory tree.
1037 * @dentry: The dentry to link.
1038 * @parent: The dentry that will be the parent of @dentry.
1040 bool dentry_add_child(struct dentry * restrict parent,
1041 struct dentry * restrict child)
1043 wimlib_assert(dentry_is_directory(parent));
1045 struct rb_root *root = &parent->d_inode->children;
1046 struct rb_node **new = &(root->rb_node);
1047 struct rb_node *rb_parent = NULL;
1050 struct dentry *this = rbnode_dentry(*new);
1051 int result = dentry_compare_names(child, this);
1056 new = &((*new)->rb_left);
1057 else if (result > 0)
1058 new = &((*new)->rb_right);
1062 child->parent = parent;
1063 rb_link_node(&child->rb_node, rb_parent, new);
1064 rb_insert_color(&child->rb_node, root);
1070 * Unlink a dentry from the directory tree.
1072 * Note: This merely removes it from the in-memory tree structure.
1074 void unlink_dentry(struct dentry *dentry)
1076 struct dentry *parent = dentry->parent;
1077 if (parent == dentry)
1079 rb_erase(&dentry->rb_node, &parent->d_inode->children);
1084 /* Returns the alternate data stream entry belonging to @inode that has the
1085 * stream name @stream_name. */
1086 struct ads_entry *inode_get_ads_entry(struct inode *inode,
1087 const char *stream_name,
1090 size_t stream_name_len;
1093 if (inode->num_ads) {
1095 stream_name_len = strlen(stream_name);
1097 if (ads_entry_has_name(&inode->ads_entries[i],
1098 stream_name, stream_name_len))
1102 return &inode->ads_entries[i];
1104 } while (++i != inode->num_ads);
1110 #if defined(WITH_FUSE) || defined(WITH_NTFS_3G)
1112 * Add an alternate stream entry to an inode and return a pointer to it, or NULL
1113 * if memory could not be allocated.
1115 struct ads_entry *inode_add_ads(struct inode *inode, const char *stream_name)
1118 struct ads_entry *ads_entries;
1119 struct ads_entry *new_entry;
1121 DEBUG("Add alternate data stream \"%s\"", stream_name);
1123 if (inode->num_ads >= 0xfffe) {
1124 ERROR("Too many alternate data streams in one inode!");
1127 num_ads = inode->num_ads + 1;
1128 ads_entries = REALLOC(inode->ads_entries,
1129 num_ads * sizeof(inode->ads_entries[0]));
1131 ERROR("Failed to allocate memory for new alternate data stream");
1134 inode->ads_entries = ads_entries;
1136 new_entry = &inode->ads_entries[num_ads - 1];
1137 if (init_ads_entry(new_entry, stream_name) != 0)
1140 new_entry->stream_id = inode->next_stream_id++;
1142 inode->num_ads = num_ads;
1148 /* Remove an alternate data stream from the inode */
1149 void inode_remove_ads(struct inode *inode, u16 idx,
1150 struct lookup_table *lookup_table)
1152 struct ads_entry *ads_entry;
1153 struct lookup_table_entry *lte;
1155 wimlib_assert(idx < inode->num_ads);
1156 wimlib_assert(inode->resolved);
1158 ads_entry = &inode->ads_entries[idx];
1160 DEBUG("Remove alternate data stream \"%s\"", ads_entry->stream_name_utf8);
1162 lte = ads_entry->lte;
1164 lte_decrement_refcnt(lte, lookup_table);
1166 destroy_ads_entry(ads_entry);
1168 memcpy(&inode->ads_entries[idx],
1169 &inode->ads_entries[idx + 1],
1170 (inode->num_ads - idx - 1) * sizeof(inode->ads_entries[0]));
1178 * Reads the alternate data stream entries for a dentry.
1180 * @p: Pointer to buffer that starts with the first alternate stream entry.
1182 * @inode: Inode to load the alternate data streams into.
1183 * @inode->num_ads must have been set to the number of
1184 * alternate data streams that are expected.
1186 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1189 * The format of the on-disk alternate stream entries is as follows:
1191 * struct ads_entry_on_disk {
1192 * u64 length; // Length of the entry, in bytes. This includes
1193 * all fields (including the stream name and
1194 * null terminator if present, AND the padding!).
1195 * u64 reserved; // Seems to be unused
1196 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
1197 * u16 stream_name_len; // Length of the stream name, in bytes
1198 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
1199 * not including null terminator
1200 * u16 zero; // UTF-16 null terminator for the stream name, NOT
1201 * included in @stream_name_len. Based on what
1202 * I've observed from filenames in dentries,
1203 * this field should not exist when
1204 * (@stream_name_len == 0), but you can't
1205 * actually tell because of the padding anyway
1206 * (provided that the padding is zeroed, which
1207 * it always seems to be).
1208 * char padding[]; // Padding to make the size a multiple of 8 bytes.
1211 * In addition, the entries are 8-byte aligned.
1213 * Return 0 on success or nonzero on failure. On success, inode->ads_entries
1214 * is set to an array of `struct ads_entry's of length inode->num_ads. On
1215 * failure, @inode is not modified.
1217 static int read_ads_entries(const u8 *p, struct inode *inode,
1221 struct ads_entry *ads_entries;
1224 num_ads = inode->num_ads;
1225 ads_entries = CALLOC(num_ads, sizeof(inode->ads_entries[0]));
1227 ERROR("Could not allocate memory for %"PRIu16" "
1228 "alternate data stream entries", num_ads);
1229 return WIMLIB_ERR_NOMEM;
1232 for (u16 i = 0; i < num_ads; i++) {
1233 struct ads_entry *cur_entry;
1235 u64 length_no_padding;
1238 const u8 *p_save = p;
1240 cur_entry = &ads_entries[i];
1243 ads_entries[i].stream_id = i + 1;
1246 /* Read the base stream entry, excluding the stream name. */
1247 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1248 ERROR("Stream entries go past end of metadata resource");
1249 ERROR("(remaining_size = %"PRIu64")", remaining_size);
1250 ret = WIMLIB_ERR_INVALID_DENTRY;
1251 goto out_free_ads_entries;
1254 p = get_u64(p, &length);
1255 p += 8; /* Skip the reserved field */
1256 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
1257 p = get_u16(p, &cur_entry->stream_name_len);
1259 cur_entry->stream_name = NULL;
1260 cur_entry->stream_name_utf8 = NULL;
1262 /* Length including neither the null terminator nor the padding
1264 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1265 cur_entry->stream_name_len;
1267 /* Length including the null terminator and the padding */
1268 total_length = ((length_no_padding + 2) + 7) & ~7;
1270 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1272 if (remaining_size < length_no_padding) {
1273 ERROR("Stream entries go past end of metadata resource");
1274 ERROR("(remaining_size = %"PRIu64" bytes, "
1275 "length_no_padding = %"PRIu64" bytes)",
1276 remaining_size, length_no_padding);
1277 ret = WIMLIB_ERR_INVALID_DENTRY;
1278 goto out_free_ads_entries;
1281 /* The @length field in the on-disk ADS entry is expected to be
1282 * equal to @total_length, which includes all of the entry and
1283 * the padding that follows it to align the next ADS entry to an
1284 * 8-byte boundary. However, to be safe, we'll accept the
1285 * length field as long as it's not less than the un-padded
1286 * total length and not more than the padded total length. */
1287 if (length < length_no_padding || length > total_length) {
1288 ERROR("Stream entry has unexpected length "
1289 "field (length field = %"PRIu64", "
1290 "unpadded total length = %"PRIu64", "
1291 "padded total length = %"PRIu64")",
1292 length, length_no_padding, total_length);
1293 ret = WIMLIB_ERR_INVALID_DENTRY;
1294 goto out_free_ads_entries;
1297 if (cur_entry->stream_name_len) {
1298 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
1299 if (!cur_entry->stream_name) {
1300 ret = WIMLIB_ERR_NOMEM;
1301 goto out_free_ads_entries;
1303 get_bytes(p, cur_entry->stream_name_len,
1304 (u8*)cur_entry->stream_name);
1305 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
1306 cur_entry->stream_name_len,
1308 cur_entry->stream_name_utf8_len = utf8_len;
1310 if (!cur_entry->stream_name_utf8) {
1311 ret = WIMLIB_ERR_NOMEM;
1312 goto out_free_ads_entries;
1315 /* It's expected that the size of every ADS entry is a multiple
1316 * of 8. However, to be safe, I'm allowing the possibility of
1317 * an ADS entry at the very end of the metadata resource ending
1318 * un-aligned. So although we still need to increment the input
1319 * pointer by @total_length to reach the next ADS entry, it's
1320 * possible that less than @total_length is actually remaining
1321 * in the metadata resource. We should set the remaining size to
1322 * 0 bytes if this happens. */
1323 p = p_save + total_length;
1324 if (remaining_size < total_length)
1327 remaining_size -= total_length;
1329 inode->ads_entries = ads_entries;
1331 inode->next_stream_id = inode->num_ads + 1;
1334 out_free_ads_entries:
1335 for (u16 i = 0; i < num_ads; i++)
1336 destroy_ads_entry(&ads_entries[i]);
1342 * Reads a directory entry, including all alternate data stream entries that
1343 * follow it, from the WIM image's metadata resource.
1345 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1346 * @metadata_resource_len: Length of the metadata resource.
1347 * @offset: Offset of this directory entry in the metadata resource.
1348 * @dentry: A `struct dentry' that will be filled in by this function.
1350 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1351 * been modified, but it will not be left with pointers to any allocated
1352 * buffers. On success, the dentry->length field must be examined. If zero,
1353 * this was a special "end of directory" dentry and not a real dentry. If
1354 * nonzero, this was a real dentry.
1356 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1357 u64 offset, struct dentry *dentry)
1360 u64 calculated_size;
1361 char *file_name = NULL;
1362 char *file_name_utf8 = NULL;
1363 char *short_name = NULL;
1366 size_t file_name_utf8_len = 0;
1368 struct inode *inode = NULL;
1370 dentry_common_init(dentry);
1372 /*Make sure the dentry really fits into the metadata resource.*/
1373 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1374 ERROR("Directory entry starting at %"PRIu64" ends past the "
1375 "end of the metadata resource (size %"PRIu64")",
1376 offset, metadata_resource_len);
1377 return WIMLIB_ERR_INVALID_DENTRY;
1380 /* Before reading the whole dentry, we need to read just the length.
1381 * This is because a dentry of length 8 (that is, just the length field)
1382 * terminates the list of sibling directory entries. */
1384 p = get_u64(&metadata_resource[offset], &dentry->length);
1386 /* A zero length field (really a length of 8, since that's how big the
1387 * directory entry is...) indicates that this is the end of directory
1388 * dentry. We do not read it into memory as an actual dentry, so just
1389 * return successfully in that case. */
1390 if (dentry->length == 0)
1393 /* If the dentry does not overflow the metadata resource buffer and is
1394 * not too short, read the rest of it (excluding the alternate data
1395 * streams, but including the file name and short name variable-length
1396 * fields) into memory. */
1397 if (offset + dentry->length >= metadata_resource_len
1398 || offset + dentry->length < offset)
1400 ERROR("Directory entry at offset %"PRIu64" and with size "
1401 "%"PRIu64" ends past the end of the metadata resource "
1403 offset, dentry->length, metadata_resource_len);
1404 return WIMLIB_ERR_INVALID_DENTRY;
1407 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1408 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1410 return WIMLIB_ERR_INVALID_DENTRY;
1413 inode = new_timeless_inode();
1415 return WIMLIB_ERR_NOMEM;
1417 p = get_u32(p, &inode->attributes);
1418 p = get_u32(p, (u32*)&inode->security_id);
1419 p = get_u64(p, &dentry->subdir_offset);
1421 /* 2 unused fields */
1422 p += 2 * sizeof(u64);
1423 /*p = get_u64(p, &dentry->unused1);*/
1424 /*p = get_u64(p, &dentry->unused2);*/
1426 p = get_u64(p, &inode->creation_time);
1427 p = get_u64(p, &inode->last_access_time);
1428 p = get_u64(p, &inode->last_write_time);
1430 p = get_bytes(p, SHA1_HASH_SIZE, inode->hash);
1433 * I don't know what's going on here. It seems like M$ screwed up the
1434 * reparse points, then put the fields in the same place and didn't
1435 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1436 * have something to do with this, but it's not documented.
1438 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1441 p = get_u32(p, &inode->reparse_tag);
1444 p = get_u32(p, &inode->reparse_tag);
1445 p = get_u64(p, &inode->ino);
1448 /* By the way, the reparse_reserved field does not actually exist (at
1449 * least when the file is not a reparse point) */
1451 p = get_u16(p, &inode->num_ads);
1453 p = get_u16(p, &short_name_len);
1454 p = get_u16(p, &file_name_len);
1456 /* We now know the length of the file name and short name. Make sure
1457 * the length of the dentry is large enough to actually hold them.
1459 * The calculated length here is unaligned to allow for the possibility
1460 * that the dentry->length names an unaligned length, although this
1461 * would be unexpected. */
1462 calculated_size = __dentry_correct_length_unaligned(file_name_len,
1465 if (dentry->length < calculated_size) {
1466 ERROR("Unexpected end of directory entry! (Expected "
1467 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1468 "short_name_len = %hu, file_name_len = %hu)",
1469 calculated_size, dentry->length,
1470 short_name_len, file_name_len);
1471 ret = WIMLIB_ERR_INVALID_DENTRY;
1472 goto out_free_inode;
1475 /* Read the filename if present. Note: if the filename is empty, there
1476 * is no null terminator following it. */
1477 if (file_name_len) {
1478 file_name = MALLOC(file_name_len);
1480 ERROR("Failed to allocate %hu bytes for dentry file name",
1482 ret = WIMLIB_ERR_NOMEM;
1483 goto out_free_inode;
1485 p = get_bytes(p, file_name_len, file_name);
1487 /* Convert filename to UTF-8. */
1488 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1489 &file_name_utf8_len);
1491 if (!file_name_utf8) {
1492 ERROR("Failed to allocate memory to convert UTF-16 "
1493 "filename (%hu bytes) to UTF-8", file_name_len);
1494 ret = WIMLIB_ERR_NOMEM;
1495 goto out_free_file_name;
1498 WARNING("Expected two zero bytes following the file name "
1499 "`%s', but found non-zero bytes", file_name_utf8);
1503 /* Align the calculated size */
1504 calculated_size = (calculated_size + 7) & ~7;
1506 if (dentry->length > calculated_size) {
1507 /* Weird; the dentry says it's longer than it should be. Note
1508 * that the length field does NOT include the size of the
1509 * alternate stream entries. */
1511 /* Strangely, some directory entries inexplicably have a little
1512 * over 70 bytes of extra data. The exact amount of data seems
1513 * to be 72 bytes, but it is aligned on the next 8-byte
1514 * boundary. It does NOT seem to be alternate data stream
1515 * entries. Here's an example of the aligned data:
1517 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1518 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1519 * 00000000 00000000 00000000 00000000
1521 * Here's one interpretation of how the data is laid out.
1524 * u32 field1; (always 0x00000001)
1525 * u32 field2; (always 0x40000000)
1526 * u8 data[48]; (???)
1527 * u64 reserved1; (always 0)
1528 * u64 reserved2; (always 0)
1530 DEBUG("Dentry for file or directory `%s' has %zu extra "
1532 file_name_utf8, dentry->length - calculated_size);
1535 /* Read the short filename if present. Note: if there is no short
1536 * filename, there is no null terminator following it. */
1537 if (short_name_len) {
1538 short_name = MALLOC(short_name_len);
1540 ERROR("Failed to allocate %hu bytes for short filename",
1542 ret = WIMLIB_ERR_NOMEM;
1543 goto out_free_file_name_utf8;
1546 p = get_bytes(p, short_name_len, short_name);
1548 WARNING("Expected two zero bytes following the short name of "
1549 "`%s', but found non-zero bytes", file_name_utf8);
1554 * Read the alternate data streams, if present. dentry->num_ads tells
1555 * us how many they are, and they will directly follow the dentry
1558 * Note that each alternate data stream entry begins on an 8-byte
1559 * aligned boundary, and the alternate data stream entries are NOT
1560 * included in the dentry->length field for some reason.
1562 if (inode->num_ads != 0) {
1564 /* Trying different lengths is just a hack to make sure we have
1565 * a chance of reading the ADS entries correctly despite the
1566 * poor documentation. */
1568 if (calculated_size != dentry->length) {
1569 WARNING("Trying calculated dentry length (%"PRIu64") "
1570 "instead of dentry->length field (%"PRIu64") "
1571 "to read ADS entries",
1572 calculated_size, dentry->length);
1574 u64 lengths_to_try[3] = {calculated_size,
1575 (dentry->length + 7) & ~7,
1577 ret = WIMLIB_ERR_INVALID_DENTRY;
1578 for (size_t i = 0; i < ARRAY_LEN(lengths_to_try); i++) {
1579 if (lengths_to_try[i] > metadata_resource_len - offset)
1581 ret = read_ads_entries(&metadata_resource[offset + lengths_to_try[i]],
1583 metadata_resource_len - offset - lengths_to_try[i]);
1587 ERROR("Failed to read alternate data stream "
1588 "entries of `%s'", dentry->file_name_utf8);
1589 goto out_free_short_name;
1593 /* We've read all the data for this dentry. Set the names and their
1594 * lengths, and we've done. */
1595 dentry->d_inode = inode;
1596 dentry->file_name = file_name;
1597 dentry->file_name_utf8 = file_name_utf8;
1598 dentry->short_name = short_name;
1599 dentry->file_name_len = file_name_len;
1600 dentry->file_name_utf8_len = file_name_utf8_len;
1601 dentry->short_name_len = short_name_len;
1603 out_free_short_name:
1605 out_free_file_name_utf8:
1606 FREE(file_name_utf8);
1614 /* Reads the children of a dentry, and all their children, ..., etc. from the
1615 * metadata resource and into the dentry tree.
1617 * @metadata_resource: An array that contains the uncompressed metadata
1618 * resource for the WIM file.
1620 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1623 * @dentry: A pointer to a `struct dentry' that is the root of the directory
1624 * tree and has already been read from the metadata resource. It
1625 * does not need to be the real root because this procedure is
1626 * called recursively.
1628 * @return: Zero on success, nonzero on failure.
1630 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1631 struct dentry *dentry)
1633 u64 cur_offset = dentry->subdir_offset;
1634 struct dentry *child;
1635 struct dentry cur_child;
1639 * If @dentry has no child dentries, nothing more needs to be done for
1640 * this branch. This is the case for regular files, symbolic links, and
1641 * *possibly* empty directories (although an empty directory may also
1642 * have one child dentry that is the special end-of-directory dentry)
1644 if (cur_offset == 0)
1647 /* Find and read all the children of @dentry. */
1650 /* Read next child of @dentry into @cur_child. */
1651 ret = read_dentry(metadata_resource, metadata_resource_len,
1652 cur_offset, &cur_child);
1656 /* Check for end of directory. */
1657 if (cur_child.length == 0)
1660 /* Not end of directory. Allocate this child permanently and
1661 * link it to the parent and previous child. */
1662 child = MALLOC(sizeof(struct dentry));
1664 ERROR("Failed to allocate %zu bytes for new dentry",
1665 sizeof(struct dentry));
1666 ret = WIMLIB_ERR_NOMEM;
1669 memcpy(child, &cur_child, sizeof(struct dentry));
1670 dentry_add_child(dentry, child);
1671 inode_add_dentry(child, child->d_inode);
1673 /* If there are children of this child, call this procedure
1675 if (child->subdir_offset != 0) {
1676 ret = read_dentry_tree(metadata_resource,
1677 metadata_resource_len, child);
1682 /* Advance to the offset of the next child. Note: We need to
1683 * advance by the TOTAL length of the dentry, not by the length
1684 * child->length, which although it does take into account the
1685 * padding, it DOES NOT take into account alternate stream
1687 cur_offset += dentry_total_length(child);
1693 * Writes a WIM dentry to an output buffer.
1695 * @dentry: The dentry structure.
1696 * @p: The memory location to write the data to.
1697 * @return: Pointer to the byte after the last byte we wrote as part of the
1700 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1704 const struct inode *inode = dentry->d_inode;
1706 /* We calculate the correct length of the dentry ourselves because the
1707 * dentry->length field may been set to an unexpected value from when we
1708 * read the dentry in (for example, there may have been unknown data
1709 * appended to the end of the dentry...) */
1710 u64 length = dentry_correct_length(dentry);
1712 p = put_u64(p, length);
1713 p = put_u32(p, inode->attributes);
1714 p = put_u32(p, inode->security_id);
1715 p = put_u64(p, dentry->subdir_offset);
1716 p = put_u64(p, 0); /* unused1 */
1717 p = put_u64(p, 0); /* unused2 */
1718 p = put_u64(p, inode->creation_time);
1719 p = put_u64(p, inode->last_access_time);
1720 p = put_u64(p, inode->last_write_time);
1721 hash = inode_stream_hash(inode, 0);
1722 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1723 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1724 p = put_zeroes(p, 4);
1725 p = put_u32(p, inode->reparse_tag);
1726 p = put_zeroes(p, 4);
1730 if (inode->link_count == 1)
1733 link_group_id = inode->ino;
1734 p = put_u64(p, link_group_id);
1736 p = put_u16(p, inode->num_ads);
1737 p = put_u16(p, dentry->short_name_len);
1738 p = put_u16(p, dentry->file_name_len);
1739 if (dentry->file_name_len) {
1740 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1741 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1743 if (dentry->short_name) {
1744 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1745 p = put_u16(p, 0); /* short name padding, 2 bytes */
1748 /* Align to 8-byte boundary */
1749 wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1750 p = put_zeroes(p, length - (p - orig_p));
1752 /* Write the alternate data streams, if there are any. Please see
1753 * read_ads_entries() for comments about the format of the on-disk
1754 * alternate data stream entries. */
1755 for (u16 i = 0; i < inode->num_ads; i++) {
1756 p = put_u64(p, ads_entry_total_length(&inode->ads_entries[i]));
1757 p = put_u64(p, 0); /* Unused */
1758 hash = inode_stream_hash(inode, i + 1);
1759 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1760 p = put_u16(p, inode->ads_entries[i].stream_name_len);
1761 if (inode->ads_entries[i].stream_name_len) {
1762 p = put_bytes(p, inode->ads_entries[i].stream_name_len,
1763 (u8*)inode->ads_entries[i].stream_name);
1766 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1768 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1772 static int write_dentry_cb(struct dentry *dentry, void *_p)
1775 *p = write_dentry(dentry, *p);
1779 static u8 *write_dentry_tree_recursive(const struct dentry *parent, u8 *p);
1781 static int write_dentry_tree_recursive_cb(struct dentry *dentry, void *_p)
1784 *p = write_dentry_tree_recursive(dentry, *p);
1788 /* Recursive function that writes a dentry tree rooted at @parent, not including
1789 * @parent itself, which has already been written. */
1790 static u8 *write_dentry_tree_recursive(const struct dentry *parent, u8 *p)
1792 /* Nothing to do if this dentry has no children. */
1793 if (parent->subdir_offset == 0)
1796 /* Write child dentries and end-of-directory entry.
1798 * Note: we need to write all of this dentry's children before
1799 * recursively writing the directory trees rooted at each of the child
1800 * dentries, since the on-disk dentries for a dentry's children are
1801 * always located at consecutive positions in the metadata resource! */
1802 for_dentry_in_rbtree(parent->d_inode->children.rb_node, write_dentry_cb, &p);
1804 /* write end of directory entry */
1807 /* Recurse on children. */
1808 for_dentry_in_rbtree(parent->d_inode->children.rb_node,
1809 write_dentry_tree_recursive_cb, &p);
1813 /* Writes a directory tree to the metadata resource.
1815 * @root: Root of the dentry tree.
1816 * @p: Pointer to a buffer with enough space for the dentry tree.
1818 * Returns pointer to the byte after the last byte we wrote.
1820 u8 *write_dentry_tree(const struct dentry *root, u8 *p)
1822 DEBUG("Writing dentry tree.");
1823 wimlib_assert(dentry_is_root(root));
1825 /* If we're the root dentry, we have no parent that already
1826 * wrote us, so we need to write ourselves. */
1827 p = write_dentry(root, p);
1829 /* Write end of directory entry after the root dentry just to be safe;
1830 * however the root dentry obviously cannot have any siblings. */
1833 /* Recursively write the rest of the dentry tree. */
1834 return write_dentry_tree_recursive(root, p);