4 * In the WIM file format, the dentries are stored in the "metadata resource"
5 * section right after the security data. Each image in the WIM file has its
6 * own metadata resource with its own security data and dentry tree. Dentries
7 * in different images may share file resources by referring to the same lookup
12 * Copyright (C) 2012 Eric Biggers
14 * This file is part of wimlib, a library for working with WIM files.
16 * wimlib is free software; you can redistribute it and/or modify it under the
17 * terms of the GNU General Public License as published by the Free Software
18 * Foundation; either version 3 of the License, or (at your option) any later
21 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
22 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
23 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along with
26 * wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "buffer_io.h"
31 #include "lookup_table.h"
32 #include "timestamp.h"
33 #include "wimlib_internal.h"
36 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
37 * a file name and short name that take the specified numbers of bytes. This
38 * excludes any alternate data stream entries that may follow the dentry. */
39 static u64 __dentry_correct_length_unaligned(u16 file_name_len,
42 u64 length = WIM_DENTRY_DISK_SIZE;
44 length += file_name_len + 2;
46 length += short_name_len + 2;
50 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
51 * the file name length and short name length. Note that dentry->length is
52 * ignored; also, this excludes any alternate data stream entries that may
53 * follow the dentry. */
54 static u64 dentry_correct_length_unaligned(const struct wim_dentry *dentry)
56 return __dentry_correct_length_unaligned(dentry->file_name_len,
57 dentry->short_name_len);
60 /* Return the "correct" value to write in the length field of a WIM dentry,
61 * based on the file name length and short name length. */
62 static u64 dentry_correct_length(const struct wim_dentry *dentry)
64 return (dentry_correct_length_unaligned(dentry) + 7) & ~7;
67 /* Return %true iff the alternate data stream entry @entry has the UTF-8 stream
68 * name @name that has length @name_len bytes. */
69 static inline bool ads_entry_has_name(const struct wim_ads_entry *entry,
70 const char *name, size_t name_len)
72 if (entry->stream_name_utf8_len != name_len)
74 return memcmp(entry->stream_name_utf8, name, name_len) == 0;
77 /* Duplicates a UTF-8 string into UTF-8 and UTF-16 strings and returns the
78 * strings and their lengths in the pointer arguments. (Frees existing strings
80 static int get_names(char **name_utf16_ret, char **name_utf8_ret,
81 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
86 char *name_utf16, *name_utf8;
89 utf8_len = strlen(name);
90 ret = utf8_to_utf16(name, utf8_len, &name_utf16, &utf16_len);
94 name_utf8 = MALLOC(utf8_len + 1);
97 return WIMLIB_ERR_NOMEM;
99 memcpy(name_utf8, name, utf8_len + 1);
100 FREE(*name_utf8_ret);
101 FREE(*name_utf16_ret);
102 *name_utf8_ret = name_utf8;
103 *name_utf16_ret = name_utf16;
104 *name_utf8_len_ret = utf8_len;
105 *name_utf16_len_ret = utf16_len;
109 /* Sets the name of a WIM dentry. */
110 int set_dentry_name(struct wim_dentry *dentry, const char *new_name)
114 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
115 &dentry->file_name_len, &dentry->file_name_utf8_len,
118 if (dentry->short_name_len) {
119 FREE(dentry->short_name);
120 dentry->short_name_len = 0;
122 dentry->length = dentry_correct_length(dentry);
128 * Changes the name of an alternate data stream */
129 static int change_ads_name(struct wim_ads_entry *entry, const char *new_name)
131 return get_names(&entry->stream_name, &entry->stream_name_utf8,
132 &entry->stream_name_len,
133 &entry->stream_name_utf8_len,
137 /* Returns the total length of a WIM alternate data stream entry on-disk,
138 * including the stream name, the null terminator, AND the padding after the
139 * entry to align the next ADS entry or dentry on an 8-byte boundary. */
140 static u64 ads_entry_total_length(const struct wim_ads_entry *entry)
142 u64 len = WIM_ADS_ENTRY_DISK_SIZE;
143 if (entry->stream_name_len)
144 len += entry->stream_name_len + 2;
145 return (len + 7) & ~7;
149 static u64 __dentry_total_length(const struct wim_dentry *dentry, u64 length)
151 const struct wim_inode *inode = dentry->d_inode;
152 for (u16 i = 0; i < inode->i_num_ads; i++)
153 length += ads_entry_total_length(&inode->i_ads_entries[i]);
154 return (length + 7) & ~7;
157 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
158 * all alternate data streams. */
159 u64 dentry_correct_total_length(const struct wim_dentry *dentry)
161 return __dentry_total_length(dentry,
162 dentry_correct_length_unaligned(dentry));
165 /* Like dentry_correct_total_length(), but use the existing dentry->length field
166 * instead of calculating its "correct" value. */
167 static u64 dentry_total_length(const struct wim_dentry *dentry)
169 return __dentry_total_length(dentry, dentry->length);
172 int for_dentry_in_rbtree(struct rb_node *root,
173 int (*visitor)(struct wim_dentry *, void *),
177 struct rb_node *node = root;
181 list_add(&rbnode_dentry(node)->tmp_list, &stack);
182 node = node->rb_left;
184 struct list_head *next;
185 struct wim_dentry *dentry;
190 dentry = container_of(next, struct wim_dentry, tmp_list);
192 ret = visitor(dentry, arg);
195 node = dentry->rb_node.rb_right;
200 static int for_dentry_tree_in_rbtree_depth(struct rb_node *node,
201 int (*visitor)(struct wim_dentry*, void*),
206 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
210 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
214 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
221 /*#define RECURSIVE_FOR_DENTRY_IN_TREE*/
223 #ifdef RECURSIVE_FOR_DENTRY_IN_TREE
224 static int for_dentry_tree_in_rbtree(struct rb_node *node,
225 int (*visitor)(struct wim_dentry*, void*),
230 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
233 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
236 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
245 * Calls a function on all directory entries in a WIM dentry tree. Logically,
246 * this is a pre-order traversal (the function is called on a parent dentry
247 * before its children), but sibling dentries will be visited in order as well.
249 * In reality, the data structures are more complicated than the above might
250 * suggest because there is a separate red-black tree for each dentry that
251 * contains its direct children.
253 int for_dentry_in_tree(struct wim_dentry *root,
254 int (*visitor)(struct wim_dentry*, void*), void *arg)
256 #ifdef RECURSIVE_FOR_DENTRY_IN_TREE
257 int ret = visitor(root, arg);
260 return for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node, visitor, arg);
263 struct list_head main_stack;
264 struct list_head sibling_stack;
265 struct list_head *sibling_stack_bottom;
266 struct wim_dentry *main_dentry;
267 struct rb_node *node;
268 struct list_head *next_sibling;
269 struct wim_dentry *dentry;
271 ret = visitor(root, arg);
276 sibling_stack_bottom = &sibling_stack;
277 INIT_LIST_HEAD(&main_stack);
278 INIT_LIST_HEAD(&sibling_stack);
280 list_add(&root->tmp_list, &main_stack);
281 node = root->d_inode->i_children.rb_node;
284 // Prepare for non-recursive in-order traversal of the red-black
285 // tree of this dentry's children
288 // Push this node to the sibling stack and examine the
289 // left neighbor, if any
290 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
291 node = node->rb_left;
294 next_sibling = sibling_stack.next;
295 if (next_sibling == sibling_stack_bottom) {
296 // Done with all siblings. Pop the main dentry to move
297 // back up one level.
298 main_dentry = container_of(main_stack.next,
301 list_del(&main_dentry->tmp_list);
303 if (main_dentry == root)
306 // Restore sibling stack bottom from the previous level
307 sibling_stack_bottom = (void*)main_dentry->parent;
309 // Restore the just-popped main dentry's parent
310 main_dentry->parent = container_of(main_stack.next,
314 // The next sibling to traverse in the previous level,
315 // in the in-order traversal of the red-black tree, is
316 // the one to the right.
317 node = main_dentry->rb_node.rb_right;
319 // The sibling stack is not empty, so there are more to
322 // Pop a sibling from the stack.
323 list_del(next_sibling);
324 dentry = container_of(next_sibling, struct wim_dentry, tmp_list);
326 // Visit the sibling.
327 ret = visitor(dentry, arg);
329 // Failed. Restore parent pointers for the
330 // dentries in the main stack
331 list_for_each_entry(dentry, &main_stack, tmp_list) {
332 dentry->parent = container_of(dentry->tmp_list.next,
339 // We'd like to recursively visit the dentry tree rooted
340 // at this sibling. To do this, add it to the main
341 // stack, save the bottom of this level's sibling stack
342 // in the dentry->parent field, re-set the bottom of the
343 // sibling stack to be its current height, and set
344 // main_dentry to the sibling so it becomes the parent
345 // dentry in the next iteration through the outer loop.
346 if (inode_has_children(dentry->d_inode)) {
347 list_add(&dentry->tmp_list, &main_stack);
348 dentry->parent = (void*)sibling_stack_bottom;
349 sibling_stack_bottom = sibling_stack.next;
351 main_dentry = dentry;
352 node = main_dentry->d_inode->i_children.rb_node;
354 node = dentry->rb_node.rb_right;
365 * Like for_dentry_in_tree(), but the visitor function is always called on a
366 * dentry's children before on itself.
368 int for_dentry_in_tree_depth(struct wim_dentry *root,
369 int (*visitor)(struct wim_dentry*, void*), void *arg)
373 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
377 return visitor(root, arg);
381 struct list_head main_stack;
382 struct list_head sibling_stack;
383 struct list_head *sibling_stack_bottom;
384 struct wim_dentry *main_dentry;
385 struct rb_node *node;
386 struct list_head *next_sibling;
387 struct wim_dentry *dentry;
390 sibling_stack_bottom = &sibling_stack;
391 INIT_LIST_HEAD(&main_stack);
392 INIT_LIST_HEAD(&sibling_stack);
394 list_add(&main_dentry->tmp_list, &main_stack);
397 node = main_dentry->d_inode->i_children.rb_node;
401 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
402 node = node->rb_left;
405 if (node->rb_right) {
406 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
407 node = node->rb_right;
410 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
414 next_sibling = sibling_stack.next;
415 if (next_sibling == sibling_stack_bottom) {
416 main_dentry = container_of(main_stack.next,
419 list_del(&main_dentry->tmp_list);
422 sibling_stack_bottom = (void*)main_dentry->parent;
424 if (main_dentry == root) {
425 main_dentry->parent = main_dentry;
426 ret = visitor(dentry, arg);
429 main_dentry->parent = container_of(main_stack.next,
434 ret = visitor(main_dentry, arg);
437 list_del(&root->tmp_list);
438 list_for_each_entry(dentry, &main_stack, tmp_list) {
439 dentry->parent = container_of(dentry->tmp_list.next,
449 list_del(next_sibling);
450 dentry = container_of(next_sibling, struct wim_dentry, tmp_list);
453 list_add(&dentry->tmp_list, &main_stack);
454 dentry->parent = (void*)sibling_stack_bottom;
455 sibling_stack_bottom = sibling_stack.next;
457 main_dentry = dentry;
464 * Calculate the full path of @dentry, based on its parent's full path and on
465 * its UTF-8 file name.
467 int calculate_dentry_full_path(struct wim_dentry *dentry, void *ignore)
471 if (dentry_is_root(dentry)) {
472 full_path = MALLOC(2);
479 char *parent_full_path;
480 u32 parent_full_path_len;
481 const struct wim_dentry *parent = dentry->parent;
483 if (dentry_is_root(parent)) {
484 parent_full_path = "";
485 parent_full_path_len = 0;
487 parent_full_path = parent->full_path_utf8;
488 parent_full_path_len = parent->full_path_utf8_len;
491 full_path_len = parent_full_path_len + 1 +
492 dentry->file_name_utf8_len;
493 full_path = MALLOC(full_path_len + 1);
497 memcpy(full_path, parent_full_path, parent_full_path_len);
498 full_path[parent_full_path_len] = '/';
499 memcpy(full_path + parent_full_path_len + 1,
500 dentry->file_name_utf8,
501 dentry->file_name_utf8_len);
502 full_path[full_path_len] = '\0';
504 FREE(dentry->full_path_utf8);
505 dentry->full_path_utf8 = full_path;
506 dentry->full_path_utf8_len = full_path_len;
509 ERROR("Out of memory while calculating dentry full path");
510 return WIMLIB_ERR_NOMEM;
513 static int increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
515 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
519 static int call_calculate_subdir_offsets(struct wim_dentry *dentry,
520 void *subdir_offset_p)
522 calculate_subdir_offsets(dentry, subdir_offset_p);
527 * Recursively calculates the subdir offsets for a directory tree.
529 * @dentry: The root of the directory tree.
530 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
531 * offset for @dentry.
533 void calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
535 struct rb_node *node;
537 dentry->subdir_offset = *subdir_offset_p;
538 node = dentry->d_inode->i_children.rb_node;
540 /* Advance the subdir offset by the amount of space the children
541 * of this dentry take up. */
542 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
544 /* End-of-directory dentry on disk. */
545 *subdir_offset_p += 8;
547 /* Recursively call calculate_subdir_offsets() on all the
549 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
551 /* On disk, childless directories have a valid subdir_offset
552 * that points to an 8-byte end-of-directory dentry. Regular
553 * files or reparse points have a subdir_offset of 0. */
554 if (dentry_is_directory(dentry))
555 *subdir_offset_p += 8;
557 dentry->subdir_offset = 0;
561 static int compare_names(const char *name_1, u16 len_1,
562 const char *name_2, u16 len_2)
564 int result = strncasecmp(name_1, name_2, min(len_1, len_2));
568 return (int)len_1 - (int)len_2;
572 static int dentry_compare_names(const struct wim_dentry *d1, const struct wim_dentry *d2)
574 return compare_names(d1->file_name_utf8, d1->file_name_utf8_len,
575 d2->file_name_utf8, d2->file_name_utf8_len);
579 static struct wim_dentry *
580 get_rbtree_child_with_name(const struct rb_node *node,
581 const char *name, size_t name_len)
584 struct wim_dentry *child = rbnode_dentry(node);
585 int result = compare_names(name, name_len,
586 child->file_name_utf8,
587 child->file_name_utf8_len);
589 node = node->rb_left;
591 node = node->rb_right;
598 /* Returns the child of @dentry that has the file name @name.
599 * Returns NULL if no child has the name. */
600 struct wim_dentry *get_dentry_child_with_name(const struct wim_dentry *dentry,
603 struct rb_node *node = dentry->d_inode->i_children.rb_node;
605 return get_rbtree_child_with_name(node, name, strlen(name));
610 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
611 * @cur_dentry. Returns NULL if no dentry having the path is found. */
612 static struct wim_dentry *get_dentry_relative_path(struct wim_dentry *cur_dentry,
618 struct rb_node *node = cur_dentry->d_inode->i_children.rb_node;
620 struct wim_dentry *child;
622 const char *new_path;
624 new_path = path_next_part(path, &base_len);
626 child = get_rbtree_child_with_name(node, path, base_len);
628 return get_dentry_relative_path(child, new_path);
630 /* errno is set to ENOTDIR if the lookup failed due to reaching a
631 * non-directory, or ENOENT if the lookup failed otherwise. This maybe
632 * should be factored out somehow. */
633 if (dentry_is_directory(cur_dentry))
640 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
642 struct wim_dentry *get_dentry(WIMStruct *w, const char *path)
644 struct wim_dentry *root = wim_root_dentry(w);
647 return get_dentry_relative_path(root, path);
650 struct wim_inode *wim_pathname_to_inode(WIMStruct *w, const char *path)
652 struct wim_dentry *dentry;
653 dentry = get_dentry(w, path);
655 return dentry->d_inode;
660 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
661 * if the dentry is not found. */
662 struct wim_dentry *get_parent_dentry(WIMStruct *w, const char *path)
664 size_t path_len = strlen(path);
665 char buf[path_len + 1];
667 memcpy(buf, path, path_len + 1);
669 to_parent_name(buf, path_len);
671 return get_dentry(w, buf);
674 /* Prints the full path of a dentry. */
675 int print_dentry_full_path(struct wim_dentry *dentry, void *ignore)
677 if (dentry->full_path_utf8)
678 puts(dentry->full_path_utf8);
682 /* We want to be able to show the names of the file attribute flags that are
684 struct file_attr_flag {
688 struct file_attr_flag file_attr_flags[] = {
689 {FILE_ATTRIBUTE_READONLY, "READONLY"},
690 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
691 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
692 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
693 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
694 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
695 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
696 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
697 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
698 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
699 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
700 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
701 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
702 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
703 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
706 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
707 * available. If the dentry is unresolved and the lookup table is NULL, the
708 * lookup table entries will not be printed. Otherwise, they will be. */
709 int print_dentry(struct wim_dentry *dentry, void *lookup_table)
712 struct wim_lookup_table_entry *lte;
713 const struct wim_inode *inode = dentry->d_inode;
716 printf("[DENTRY]\n");
717 printf("Length = %"PRIu64"\n", dentry->length);
718 printf("Attributes = 0x%x\n", inode->i_attributes);
719 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
720 if (file_attr_flags[i].flag & inode->i_attributes)
721 printf(" FILE_ATTRIBUTE_%s is set\n",
722 file_attr_flags[i].name);
723 printf("Security ID = %d\n", inode->i_security_id);
724 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
726 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
727 printf("Creation Time = %s\n", buf);
729 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
730 printf("Last Access Time = %s\n", buf);
732 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
733 printf("Last Write Time = %s\n", buf);
735 printf("Reparse Tag = 0x%"PRIx32"\n", inode->i_reparse_tag);
736 printf("Hard Link Group = 0x%"PRIx64"\n", inode->i_ino);
737 printf("Hard Link Group Size = %"PRIu32"\n", inode->i_nlink);
738 printf("Number of Alternate Data Streams = %hu\n", inode->i_num_ads);
739 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
740 /*printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);*/
741 printf("Short Name (UTF-16LE) = \"");
742 print_string(dentry->short_name, dentry->short_name_len);
744 /*printf("Short Name Length = %hu\n", dentry->short_name_len);*/
745 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
746 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
748 print_lookup_table_entry(lte, stdout);
750 hash = inode_stream_hash(inode, 0);
758 for (u16 i = 0; i < inode->i_num_ads; i++) {
759 printf("[Alternate Stream Entry %u]\n", i);
760 printf("Name = \"%s\"\n", inode->i_ads_entries[i].stream_name_utf8);
761 printf("Name Length (UTF-16) = %u\n",
762 inode->i_ads_entries[i].stream_name_len);
763 hash = inode_stream_hash(inode, i + 1);
769 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
775 /* Initializations done on every `struct wim_dentry'. */
776 static void dentry_common_init(struct wim_dentry *dentry)
778 memset(dentry, 0, sizeof(struct wim_dentry));
782 static struct wim_inode *new_timeless_inode()
784 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
786 inode->i_security_id = -1;
789 inode->i_next_stream_id = 1;
790 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
791 ERROR_WITH_ERRNO("Error initializing mutex");
796 INIT_LIST_HEAD(&inode->i_dentry);
801 static struct wim_inode *new_inode()
803 struct wim_inode *inode = new_timeless_inode();
805 u64 now = get_wim_timestamp();
806 inode->i_creation_time = now;
807 inode->i_last_access_time = now;
808 inode->i_last_write_time = now;
814 * Creates an unlinked directory entry.
816 * @name: The UTF-8 filename of the new dentry.
818 * Returns a pointer to the new dentry, or NULL if out of memory.
820 struct wim_dentry *new_dentry(const char *name)
822 struct wim_dentry *dentry;
824 dentry = MALLOC(sizeof(struct wim_dentry));
828 dentry_common_init(dentry);
829 if (set_dentry_name(dentry, name) != 0)
832 dentry->parent = dentry;
837 ERROR_WITH_ERRNO("Failed to create new dentry with name \"%s\"", name);
842 static struct wim_dentry *
843 __new_dentry_with_inode(const char *name, bool timeless)
845 struct wim_dentry *dentry;
846 dentry = new_dentry(name);
849 dentry->d_inode = new_timeless_inode();
851 dentry->d_inode = new_inode();
852 if (dentry->d_inode) {
853 inode_add_dentry(dentry, dentry->d_inode);
862 struct wim_dentry *new_dentry_with_timeless_inode(const char *name)
864 return __new_dentry_with_inode(name, true);
867 struct wim_dentry *new_dentry_with_inode(const char *name)
869 return __new_dentry_with_inode(name, false);
873 static int init_ads_entry(struct wim_ads_entry *ads_entry, const char *name)
876 memset(ads_entry, 0, sizeof(*ads_entry));
878 ret = change_ads_name(ads_entry, name);
882 static void destroy_ads_entry(struct wim_ads_entry *ads_entry)
884 FREE(ads_entry->stream_name);
885 FREE(ads_entry->stream_name_utf8);
889 /* Frees an inode. */
890 void free_inode(struct wim_inode *inode)
893 if (inode->i_ads_entries) {
894 for (u16 i = 0; i < inode->i_num_ads; i++)
895 destroy_ads_entry(&inode->i_ads_entries[i]);
896 FREE(inode->i_ads_entries);
899 wimlib_assert(inode->i_num_opened_fds == 0);
901 pthread_mutex_destroy(&inode->i_mutex);
902 if (inode->i_hlist.pprev)
903 hlist_del(&inode->i_hlist);
905 FREE(inode->i_extracted_file);
910 /* Decrements link count on an inode and frees it if the link count reaches 0.
912 static void put_inode(struct wim_inode *inode)
914 wimlib_assert(inode->i_nlink != 0);
915 if (--inode->i_nlink == 0) {
917 if (inode->i_num_opened_fds == 0)
925 /* Frees a WIM dentry.
927 * The corresponding inode (if any) is freed only if its link count is
930 void free_dentry(struct wim_dentry *dentry)
932 FREE(dentry->file_name);
933 FREE(dentry->file_name_utf8);
934 FREE(dentry->short_name);
935 FREE(dentry->full_path_utf8);
937 put_inode(dentry->d_inode);
941 void put_dentry(struct wim_dentry *dentry)
943 wimlib_assert(dentry->refcnt != 0);
944 if (--dentry->refcnt == 0)
948 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
949 * to free a directory tree. */
950 static int do_free_dentry(struct wim_dentry *dentry, void *__lookup_table)
952 struct wim_lookup_table *lookup_table = __lookup_table;
956 struct wim_lookup_table_entry *lte;
957 struct wim_inode *inode = dentry->d_inode;
958 wimlib_assert(inode->i_nlink != 0);
959 for (i = 0; i <= inode->i_num_ads; i++) {
960 lte = inode_stream_lte(inode, i, lookup_table);
962 lte_decrement_refcnt(lte, lookup_table);
971 * Unlinks and frees a dentry tree.
973 * @root: The root of the tree.
974 * @lookup_table: The lookup table for dentries. If non-NULL, the
975 * reference counts in the lookup table for the lookup
976 * table entries corresponding to the dentries will be
979 void free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
982 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
985 int increment_dentry_refcnt(struct wim_dentry *dentry, void *ignore)
992 * Links a dentry into the directory tree.
994 * @dentry: The dentry to link.
995 * @parent: The dentry that will be the parent of @dentry.
997 bool dentry_add_child(struct wim_dentry * restrict parent,
998 struct wim_dentry * restrict child)
1000 wimlib_assert(dentry_is_directory(parent));
1002 struct rb_root *root = &parent->d_inode->i_children;
1003 struct rb_node **new = &(root->rb_node);
1004 struct rb_node *rb_parent = NULL;
1007 struct wim_dentry *this = rbnode_dentry(*new);
1008 int result = dentry_compare_names(child, this);
1013 new = &((*new)->rb_left);
1014 else if (result > 0)
1015 new = &((*new)->rb_right);
1019 child->parent = parent;
1020 rb_link_node(&child->rb_node, rb_parent, new);
1021 rb_insert_color(&child->rb_node, root);
1025 /* Unlink a WIM dentry from the directory entry tree. */
1026 void unlink_dentry(struct wim_dentry *dentry)
1028 struct wim_dentry *parent = dentry->parent;
1029 if (parent == dentry)
1031 rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1035 * Returns the alternate data stream entry belonging to @inode that has the
1036 * stream name @stream_name.
1038 struct wim_ads_entry *inode_get_ads_entry(struct wim_inode *inode,
1039 const char *stream_name,
1042 if (inode->i_num_ads != 0) {
1044 size_t stream_name_len = strlen(stream_name);
1046 if (ads_entry_has_name(&inode->i_ads_entries[i],
1047 stream_name, stream_name_len))
1051 return &inode->i_ads_entries[i];
1053 } while (++i != inode->i_num_ads);
1059 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1060 * NULL if memory could not be allocated.
1062 struct wim_ads_entry *inode_add_ads(struct wim_inode *inode, const char *stream_name)
1065 struct wim_ads_entry *ads_entries;
1066 struct wim_ads_entry *new_entry;
1068 DEBUG("Add alternate data stream \"%s\"", stream_name);
1070 if (inode->i_num_ads >= 0xfffe) {
1071 ERROR("Too many alternate data streams in one inode!");
1074 num_ads = inode->i_num_ads + 1;
1075 ads_entries = REALLOC(inode->i_ads_entries,
1076 num_ads * sizeof(inode->i_ads_entries[0]));
1078 ERROR("Failed to allocate memory for new alternate data stream");
1081 inode->i_ads_entries = ads_entries;
1083 new_entry = &inode->i_ads_entries[num_ads - 1];
1084 if (init_ads_entry(new_entry, stream_name) != 0)
1087 new_entry->stream_id = inode->i_next_stream_id++;
1089 inode->i_num_ads = num_ads;
1093 int inode_add_ads_with_data(struct wim_inode *inode, const char *name,
1094 const u8 *value, size_t size,
1095 struct wim_lookup_table *lookup_table)
1097 int ret = WIMLIB_ERR_NOMEM;
1098 struct wim_ads_entry *new_ads_entry;
1099 struct wim_lookup_table_entry *existing_lte;
1100 struct wim_lookup_table_entry *lte;
1101 u8 value_hash[SHA1_HASH_SIZE];
1103 wimlib_assert(inode->i_resolved);
1104 new_ads_entry = inode_add_ads(inode, name);
1107 sha1_buffer((const u8*)value, size, value_hash);
1108 existing_lte = __lookup_resource(lookup_table, value_hash);
1114 lte = new_lookup_table_entry();
1116 goto out_free_ads_entry;
1117 value_copy = MALLOC(size);
1120 goto out_free_ads_entry;
1122 memcpy(value_copy, value, size);
1123 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1124 lte->attached_buffer = value_copy;
1125 lte->resource_entry.original_size = size;
1126 lte->resource_entry.size = size;
1127 lte->resource_entry.flags = 0;
1128 copy_hash(lte->hash, value_hash);
1129 lookup_table_insert(lookup_table, lte);
1131 new_ads_entry->lte = lte;
1135 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1141 /* Remove an alternate data stream from a WIM inode */
1142 void inode_remove_ads(struct wim_inode *inode, u16 idx,
1143 struct wim_lookup_table *lookup_table)
1145 struct wim_ads_entry *ads_entry;
1146 struct wim_lookup_table_entry *lte;
1148 wimlib_assert(idx < inode->i_num_ads);
1149 wimlib_assert(inode->i_resolved);
1151 ads_entry = &inode->i_ads_entries[idx];
1153 DEBUG("Remove alternate data stream \"%s\"", ads_entry->stream_name_utf8);
1155 lte = ads_entry->lte;
1157 lte_decrement_refcnt(lte, lookup_table);
1159 destroy_ads_entry(ads_entry);
1161 memmove(&inode->i_ads_entries[idx],
1162 &inode->i_ads_entries[idx + 1],
1163 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1167 int inode_get_unix_data(const struct wim_inode *inode,
1168 struct wimlib_unix_data *unix_data,
1169 u16 *stream_idx_ret)
1171 const struct wim_ads_entry *ads_entry;
1172 const struct wim_lookup_table_entry *lte;
1176 wimlib_assert(inode->i_resolved);
1178 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1179 WIMLIB_UNIX_DATA_TAG, NULL);
1181 return NO_UNIX_DATA;
1184 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1186 lte = ads_entry->lte;
1188 return NO_UNIX_DATA;
1190 size = wim_resource_size(lte);
1191 if (size != sizeof(struct wimlib_unix_data))
1192 return BAD_UNIX_DATA;
1194 ret = read_full_wim_resource(lte, (u8*)unix_data, 0);
1198 if (unix_data->version != 0)
1199 return BAD_UNIX_DATA;
1203 int inode_set_unix_data(struct wim_inode *inode,
1204 uid_t uid, gid_t gid, mode_t mode,
1205 struct wim_lookup_table *lookup_table,
1208 struct wimlib_unix_data unix_data;
1210 bool have_good_unix_data = false;
1211 bool have_unix_data = false;
1214 if (!(which & UNIX_DATA_CREATE)) {
1215 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1216 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1217 have_unix_data = true;
1219 have_good_unix_data = true;
1221 unix_data.version = 0;
1222 if (which & UNIX_DATA_UID || !have_good_unix_data)
1223 unix_data.uid = uid;
1224 if (which & UNIX_DATA_GID || !have_good_unix_data)
1225 unix_data.gid = gid;
1226 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1227 unix_data.mode = mode;
1228 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1229 (const u8*)&unix_data,
1230 sizeof(struct wimlib_unix_data),
1232 if (ret == 0 && have_unix_data)
1233 inode_remove_ads(inode, stream_idx, lookup_table);
1238 * Reads the alternate data stream entries of a WIM dentry.
1240 * @p: Pointer to buffer that starts with the first alternate stream entry.
1242 * @inode: Inode to load the alternate data streams into.
1243 * @inode->i_num_ads must have been set to the number of
1244 * alternate data streams that are expected.
1246 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1249 * The format of the on-disk alternate stream entries is as follows:
1251 * struct wim_ads_entry_on_disk {
1252 * u64 length; // Length of the entry, in bytes. This includes
1253 * all fields (including the stream name and
1254 * null terminator if present, AND the padding!).
1255 * u64 reserved; // Seems to be unused
1256 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
1257 * u16 stream_name_len; // Length of the stream name, in bytes
1258 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
1259 * not including null terminator
1260 * u16 zero; // UTF-16 null terminator for the stream name, NOT
1261 * included in @stream_name_len. Based on what
1262 * I've observed from filenames in dentries,
1263 * this field should not exist when
1264 * (@stream_name_len == 0), but you can't
1265 * actually tell because of the padding anyway
1266 * (provided that the padding is zeroed, which
1267 * it always seems to be).
1268 * char padding[]; // Padding to make the size a multiple of 8 bytes.
1271 * In addition, the entries are 8-byte aligned.
1273 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1274 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1275 * failure, @inode is not modified.
1277 static int read_ads_entries(const u8 *p, struct wim_inode *inode,
1281 struct wim_ads_entry *ads_entries;
1284 num_ads = inode->i_num_ads;
1285 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1287 ERROR("Could not allocate memory for %"PRIu16" "
1288 "alternate data stream entries", num_ads);
1289 return WIMLIB_ERR_NOMEM;
1292 for (u16 i = 0; i < num_ads; i++) {
1293 struct wim_ads_entry *cur_entry;
1295 u64 length_no_padding;
1298 const u8 *p_save = p;
1300 cur_entry = &ads_entries[i];
1303 ads_entries[i].stream_id = i + 1;
1306 /* Read the base stream entry, excluding the stream name. */
1307 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1308 ERROR("Stream entries go past end of metadata resource");
1309 ERROR("(remaining_size = %"PRIu64")", remaining_size);
1310 ret = WIMLIB_ERR_INVALID_DENTRY;
1311 goto out_free_ads_entries;
1314 p = get_u64(p, &length);
1315 p += 8; /* Skip the reserved field */
1316 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
1317 p = get_u16(p, &cur_entry->stream_name_len);
1319 cur_entry->stream_name = NULL;
1320 cur_entry->stream_name_utf8 = NULL;
1322 /* Length including neither the null terminator nor the padding
1324 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1325 cur_entry->stream_name_len;
1327 /* Length including the null terminator and the padding */
1328 total_length = ((length_no_padding + 2) + 7) & ~7;
1330 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1332 if (remaining_size < length_no_padding) {
1333 ERROR("Stream entries go past end of metadata resource");
1334 ERROR("(remaining_size = %"PRIu64" bytes, "
1335 "length_no_padding = %"PRIu64" bytes)",
1336 remaining_size, length_no_padding);
1337 ret = WIMLIB_ERR_INVALID_DENTRY;
1338 goto out_free_ads_entries;
1341 /* The @length field in the on-disk ADS entry is expected to be
1342 * equal to @total_length, which includes all of the entry and
1343 * the padding that follows it to align the next ADS entry to an
1344 * 8-byte boundary. However, to be safe, we'll accept the
1345 * length field as long as it's not less than the un-padded
1346 * total length and not more than the padded total length. */
1347 if (length < length_no_padding || length > total_length) {
1348 ERROR("Stream entry has unexpected length "
1349 "field (length field = %"PRIu64", "
1350 "unpadded total length = %"PRIu64", "
1351 "padded total length = %"PRIu64")",
1352 length, length_no_padding, total_length);
1353 ret = WIMLIB_ERR_INVALID_DENTRY;
1354 goto out_free_ads_entries;
1357 if (cur_entry->stream_name_len) {
1358 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
1359 if (!cur_entry->stream_name) {
1360 ret = WIMLIB_ERR_NOMEM;
1361 goto out_free_ads_entries;
1363 get_bytes(p, cur_entry->stream_name_len,
1364 (u8*)cur_entry->stream_name);
1366 ret = utf16_to_utf8(cur_entry->stream_name,
1367 cur_entry->stream_name_len,
1368 &cur_entry->stream_name_utf8,
1371 goto out_free_ads_entries;
1372 cur_entry->stream_name_utf8_len = utf8_len;
1374 /* It's expected that the size of every ADS entry is a multiple
1375 * of 8. However, to be safe, I'm allowing the possibility of
1376 * an ADS entry at the very end of the metadata resource ending
1377 * un-aligned. So although we still need to increment the input
1378 * pointer by @total_length to reach the next ADS entry, it's
1379 * possible that less than @total_length is actually remaining
1380 * in the metadata resource. We should set the remaining size to
1381 * 0 bytes if this happens. */
1382 p = p_save + total_length;
1383 if (remaining_size < total_length)
1386 remaining_size -= total_length;
1388 inode->i_ads_entries = ads_entries;
1390 inode->i_next_stream_id = inode->i_num_ads + 1;
1393 out_free_ads_entries:
1394 for (u16 i = 0; i < num_ads; i++)
1395 destroy_ads_entry(&ads_entries[i]);
1401 * Reads a WIM directory entry, including all alternate data stream entries that
1402 * follow it, from the WIM image's metadata resource.
1404 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1405 * @metadata_resource_len: Length of the metadata resource.
1406 * @offset: Offset of this directory entry in the metadata resource.
1407 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1409 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1410 * been modified, but it will not be left with pointers to any allocated
1411 * buffers. On success, the dentry->length field must be examined. If zero,
1412 * this was a special "end of directory" dentry and not a real dentry. If
1413 * nonzero, this was a real dentry.
1415 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1416 u64 offset, struct wim_dentry *dentry)
1419 u64 calculated_size;
1420 char *file_name = NULL;
1421 char *file_name_utf8 = NULL;
1422 char *short_name = NULL;
1425 size_t file_name_utf8_len = 0;
1427 struct wim_inode *inode = NULL;
1429 dentry_common_init(dentry);
1431 /*Make sure the dentry really fits into the metadata resource.*/
1432 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1433 ERROR("Directory entry starting at %"PRIu64" ends past the "
1434 "end of the metadata resource (size %"PRIu64")",
1435 offset, metadata_resource_len);
1436 return WIMLIB_ERR_INVALID_DENTRY;
1439 /* Before reading the whole dentry, we need to read just the length.
1440 * This is because a dentry of length 8 (that is, just the length field)
1441 * terminates the list of sibling directory entries. */
1443 p = get_u64(&metadata_resource[offset], &dentry->length);
1445 /* A zero length field (really a length of 8, since that's how big the
1446 * directory entry is...) indicates that this is the end of directory
1447 * dentry. We do not read it into memory as an actual dentry, so just
1448 * return successfully in that case. */
1449 if (dentry->length == 0)
1452 /* If the dentry does not overflow the metadata resource buffer and is
1453 * not too short, read the rest of it (excluding the alternate data
1454 * streams, but including the file name and short name variable-length
1455 * fields) into memory. */
1456 if (offset + dentry->length >= metadata_resource_len
1457 || offset + dentry->length < offset)
1459 ERROR("Directory entry at offset %"PRIu64" and with size "
1460 "%"PRIu64" ends past the end of the metadata resource "
1462 offset, dentry->length, metadata_resource_len);
1463 return WIMLIB_ERR_INVALID_DENTRY;
1466 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1467 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1469 return WIMLIB_ERR_INVALID_DENTRY;
1472 inode = new_timeless_inode();
1474 return WIMLIB_ERR_NOMEM;
1476 p = get_u32(p, &inode->i_attributes);
1477 p = get_u32(p, (u32*)&inode->i_security_id);
1478 p = get_u64(p, &dentry->subdir_offset);
1480 /* 2 unused fields */
1481 p += 2 * sizeof(u64);
1482 /*p = get_u64(p, &dentry->unused1);*/
1483 /*p = get_u64(p, &dentry->unused2);*/
1485 p = get_u64(p, &inode->i_creation_time);
1486 p = get_u64(p, &inode->i_last_access_time);
1487 p = get_u64(p, &inode->i_last_write_time);
1489 p = get_bytes(p, SHA1_HASH_SIZE, inode->i_hash);
1492 * I don't know what's going on here. It seems like M$ screwed up the
1493 * reparse points, then put the fields in the same place and didn't
1494 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1495 * have something to do with this, but it's not documented.
1497 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1500 p = get_u32(p, &inode->i_reparse_tag);
1503 p = get_u32(p, &inode->i_reparse_tag);
1504 p = get_u64(p, &inode->i_ino);
1507 /* By the way, the reparse_reserved field does not actually exist (at
1508 * least when the file is not a reparse point) */
1510 p = get_u16(p, &inode->i_num_ads);
1512 p = get_u16(p, &short_name_len);
1513 p = get_u16(p, &file_name_len);
1515 /* We now know the length of the file name and short name. Make sure
1516 * the length of the dentry is large enough to actually hold them.
1518 * The calculated length here is unaligned to allow for the possibility
1519 * that the dentry->length names an unaligned length, although this
1520 * would be unexpected. */
1521 calculated_size = __dentry_correct_length_unaligned(file_name_len,
1524 if (dentry->length < calculated_size) {
1525 ERROR("Unexpected end of directory entry! (Expected "
1526 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1527 "short_name_len = %hu, file_name_len = %hu)",
1528 calculated_size, dentry->length,
1529 short_name_len, file_name_len);
1530 ret = WIMLIB_ERR_INVALID_DENTRY;
1531 goto out_free_inode;
1534 /* Read the filename if present. Note: if the filename is empty, there
1535 * is no null terminator following it. */
1536 if (file_name_len) {
1537 file_name = MALLOC(file_name_len);
1539 ERROR("Failed to allocate %hu bytes for dentry file name",
1541 ret = WIMLIB_ERR_NOMEM;
1542 goto out_free_inode;
1544 p = get_bytes(p, file_name_len, file_name);
1546 /* Convert filename to UTF-8. */
1547 ret = utf16_to_utf8(file_name, file_name_len, &file_name_utf8,
1548 &file_name_utf8_len);
1550 goto out_free_file_name;
1552 WARNING("Expected two zero bytes following the file name "
1553 "`%s', but found non-zero bytes", file_name_utf8);
1557 /* Align the calculated size */
1558 calculated_size = (calculated_size + 7) & ~7;
1560 if (dentry->length > calculated_size) {
1561 /* Weird; the dentry says it's longer than it should be. Note
1562 * that the length field does NOT include the size of the
1563 * alternate stream entries. */
1565 /* Strangely, some directory entries inexplicably have a little
1566 * over 70 bytes of extra data. The exact amount of data seems
1567 * to be 72 bytes, but it is aligned on the next 8-byte
1568 * boundary. It does NOT seem to be alternate data stream
1569 * entries. Here's an example of the aligned data:
1571 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1572 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1573 * 00000000 00000000 00000000 00000000
1575 * Here's one interpretation of how the data is laid out.
1578 * u32 field1; (always 0x00000001)
1579 * u32 field2; (always 0x40000000)
1580 * u8 data[48]; (???)
1581 * u64 reserved1; (always 0)
1582 * u64 reserved2; (always 0)
1584 DEBUG("Dentry for file or directory `%s' has %"PRIu64" extra "
1586 file_name_utf8, dentry->length - calculated_size);
1589 /* Read the short filename if present. Note: if there is no short
1590 * filename, there is no null terminator following it. */
1591 if (short_name_len) {
1592 short_name = MALLOC(short_name_len);
1594 ERROR("Failed to allocate %hu bytes for short filename",
1596 ret = WIMLIB_ERR_NOMEM;
1597 goto out_free_file_name_utf8;
1600 p = get_bytes(p, short_name_len, short_name);
1602 WARNING("Expected two zero bytes following the short name of "
1603 "`%s', but found non-zero bytes", file_name_utf8);
1608 * Read the alternate data streams, if present. dentry->num_ads tells
1609 * us how many they are, and they will directly follow the dentry
1612 * Note that each alternate data stream entry begins on an 8-byte
1613 * aligned boundary, and the alternate data stream entries are NOT
1614 * included in the dentry->length field for some reason.
1616 if (inode->i_num_ads != 0) {
1618 /* Trying different lengths is just a hack to make sure we have
1619 * a chance of reading the ADS entries correctly despite the
1620 * poor documentation. */
1622 if (calculated_size != dentry->length) {
1623 WARNING("Trying calculated dentry length (%"PRIu64") "
1624 "instead of dentry->length field (%"PRIu64") "
1625 "to read ADS entries",
1626 calculated_size, dentry->length);
1628 u64 lengths_to_try[3] = {calculated_size,
1629 (dentry->length + 7) & ~7,
1631 ret = WIMLIB_ERR_INVALID_DENTRY;
1632 for (size_t i = 0; i < ARRAY_LEN(lengths_to_try); i++) {
1633 if (lengths_to_try[i] > metadata_resource_len - offset)
1635 ret = read_ads_entries(&metadata_resource[offset + lengths_to_try[i]],
1637 metadata_resource_len - offset - lengths_to_try[i]);
1641 ERROR("Failed to read alternate data stream "
1642 "entries of `%s'", dentry->file_name_utf8);
1643 goto out_free_short_name;
1647 /* We've read all the data for this dentry. Set the names and their
1648 * lengths, and we've done. */
1649 dentry->d_inode = inode;
1650 dentry->file_name = file_name;
1651 dentry->file_name_utf8 = file_name_utf8;
1652 dentry->short_name = short_name;
1653 dentry->file_name_len = file_name_len;
1654 dentry->file_name_utf8_len = file_name_utf8_len;
1655 dentry->short_name_len = short_name_len;
1657 out_free_short_name:
1659 out_free_file_name_utf8:
1660 FREE(file_name_utf8);
1668 /* Reads the children of a dentry, and all their children, ..., etc. from the
1669 * metadata resource and into the dentry tree.
1671 * @metadata_resource: An array that contains the uncompressed metadata
1672 * resource for the WIM file.
1674 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1677 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
1678 * tree and has already been read from the metadata resource. It
1679 * does not need to be the real root because this procedure is
1680 * called recursively.
1682 * @return: Zero on success, nonzero on failure.
1684 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1685 struct wim_dentry *dentry)
1687 u64 cur_offset = dentry->subdir_offset;
1688 struct wim_dentry *child;
1689 struct wim_dentry cur_child;
1693 * If @dentry has no child dentries, nothing more needs to be done for
1694 * this branch. This is the case for regular files, symbolic links, and
1695 * *possibly* empty directories (although an empty directory may also
1696 * have one child dentry that is the special end-of-directory dentry)
1698 if (cur_offset == 0)
1701 /* Find and read all the children of @dentry. */
1704 /* Read next child of @dentry into @cur_child. */
1705 ret = read_dentry(metadata_resource, metadata_resource_len,
1706 cur_offset, &cur_child);
1710 /* Check for end of directory. */
1711 if (cur_child.length == 0)
1714 /* Not end of directory. Allocate this child permanently and
1715 * link it to the parent and previous child. */
1716 child = MALLOC(sizeof(struct wim_dentry));
1718 ERROR("Failed to allocate %zu bytes for new dentry",
1719 sizeof(struct wim_dentry));
1720 ret = WIMLIB_ERR_NOMEM;
1723 memcpy(child, &cur_child, sizeof(struct wim_dentry));
1724 dentry_add_child(dentry, child);
1725 inode_add_dentry(child, child->d_inode);
1727 /* If there are children of this child, call this procedure
1729 if (child->subdir_offset != 0) {
1730 ret = read_dentry_tree(metadata_resource,
1731 metadata_resource_len, child);
1736 /* Advance to the offset of the next child. Note: We need to
1737 * advance by the TOTAL length of the dentry, not by the length
1738 * child->length, which although it does take into account the
1739 * padding, it DOES NOT take into account alternate stream
1741 cur_offset += dentry_total_length(child);
1747 * Writes a WIM dentry to an output buffer.
1749 * @dentry: The dentry structure.
1750 * @p: The memory location to write the data to.
1751 * @return: Pointer to the byte after the last byte we wrote as part of the
1754 static u8 *write_dentry(const struct wim_dentry *dentry, u8 *p)
1758 const struct wim_inode *inode = dentry->d_inode;
1760 /* We calculate the correct length of the dentry ourselves because the
1761 * dentry->length field may been set to an unexpected value from when we
1762 * read the dentry in (for example, there may have been unknown data
1763 * appended to the end of the dentry...) */
1764 u64 length = dentry_correct_length(dentry);
1766 p = put_u64(p, length);
1767 p = put_u32(p, inode->i_attributes);
1768 p = put_u32(p, inode->i_security_id);
1769 p = put_u64(p, dentry->subdir_offset);
1770 p = put_u64(p, 0); /* unused1 */
1771 p = put_u64(p, 0); /* unused2 */
1772 p = put_u64(p, inode->i_creation_time);
1773 p = put_u64(p, inode->i_last_access_time);
1774 p = put_u64(p, inode->i_last_write_time);
1775 hash = inode_stream_hash(inode, 0);
1776 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1777 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1778 p = put_zeroes(p, 4);
1779 p = put_u32(p, inode->i_reparse_tag);
1780 p = put_zeroes(p, 4);
1784 if (inode->i_nlink == 1)
1787 link_group_id = inode->i_ino;
1788 p = put_u64(p, link_group_id);
1790 p = put_u16(p, inode->i_num_ads);
1791 p = put_u16(p, dentry->short_name_len);
1792 p = put_u16(p, dentry->file_name_len);
1793 if (dentry->file_name_len) {
1794 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1795 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1797 if (dentry->short_name) {
1798 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1799 p = put_u16(p, 0); /* short name padding, 2 bytes */
1802 /* Align to 8-byte boundary */
1803 wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1804 p = put_zeroes(p, length - (p - orig_p));
1806 /* Write the alternate data streams, if there are any. Please see
1807 * read_ads_entries() for comments about the format of the on-disk
1808 * alternate data stream entries. */
1809 for (u16 i = 0; i < inode->i_num_ads; i++) {
1810 p = put_u64(p, ads_entry_total_length(&inode->i_ads_entries[i]));
1811 p = put_u64(p, 0); /* Unused */
1812 hash = inode_stream_hash(inode, i + 1);
1813 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1814 p = put_u16(p, inode->i_ads_entries[i].stream_name_len);
1815 if (inode->i_ads_entries[i].stream_name_len) {
1816 p = put_bytes(p, inode->i_ads_entries[i].stream_name_len,
1817 (u8*)inode->i_ads_entries[i].stream_name);
1820 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1822 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1826 static int write_dentry_cb(struct wim_dentry *dentry, void *_p)
1829 *p = write_dentry(dentry, *p);
1833 static u8 *write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
1835 static int write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
1838 *p = write_dentry_tree_recursive(dentry, *p);
1842 /* Recursive function that writes a dentry tree rooted at @parent, not including
1843 * @parent itself, which has already been written. */
1844 static u8 *write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
1846 /* Nothing to do if this dentry has no children. */
1847 if (parent->subdir_offset == 0)
1850 /* Write child dentries and end-of-directory entry.
1852 * Note: we need to write all of this dentry's children before
1853 * recursively writing the directory trees rooted at each of the child
1854 * dentries, since the on-disk dentries for a dentry's children are
1855 * always located at consecutive positions in the metadata resource! */
1856 for_dentry_child(parent, write_dentry_cb, &p);
1858 /* write end of directory entry */
1861 /* Recurse on children. */
1862 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
1866 /* Writes a directory tree to the metadata resource.
1868 * @root: Root of the dentry tree.
1869 * @p: Pointer to a buffer with enough space for the dentry tree.
1871 * Returns pointer to the byte after the last byte we wrote.
1873 u8 *write_dentry_tree(const struct wim_dentry *root, u8 *p)
1875 DEBUG("Writing dentry tree.");
1876 wimlib_assert(dentry_is_root(root));
1878 /* If we're the root dentry, we have no parent that already
1879 * wrote us, so we need to write ourselves. */
1880 p = write_dentry(root, p);
1882 /* Write end of directory entry after the root dentry just to be safe;
1883 * however the root dentry obviously cannot have any siblings. */
1886 /* Recursively write the rest of the dentry tree. */
1887 return write_dentry_tree_recursive(root, p);