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);
1026 /* Unlink a WIM dentry from the directory entry tree. */
1027 void unlink_dentry(struct wim_dentry *dentry)
1029 struct wim_dentry *parent = dentry->parent;
1030 if (parent == dentry)
1032 rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1038 * Returns the alternate data stream entry belonging to @inode that has the
1039 * stream name @stream_name.
1041 struct wim_ads_entry *inode_get_ads_entry(struct wim_inode *inode,
1042 const char *stream_name,
1045 if (inode->i_num_ads != 0) {
1047 size_t stream_name_len = strlen(stream_name);
1049 if (ads_entry_has_name(&inode->i_ads_entries[i],
1050 stream_name, stream_name_len))
1054 return &inode->i_ads_entries[i];
1056 } while (++i != inode->i_num_ads);
1062 #if defined(WITH_FUSE) || defined(WITH_NTFS_3G)
1064 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1065 * NULL if memory could not be allocated.
1067 struct wim_ads_entry *inode_add_ads(struct wim_inode *inode, const char *stream_name)
1070 struct wim_ads_entry *ads_entries;
1071 struct wim_ads_entry *new_entry;
1073 DEBUG("Add alternate data stream \"%s\"", stream_name);
1075 if (inode->i_num_ads >= 0xfffe) {
1076 ERROR("Too many alternate data streams in one inode!");
1079 num_ads = inode->i_num_ads + 1;
1080 ads_entries = REALLOC(inode->i_ads_entries,
1081 num_ads * sizeof(inode->i_ads_entries[0]));
1083 ERROR("Failed to allocate memory for new alternate data stream");
1086 inode->i_ads_entries = ads_entries;
1088 new_entry = &inode->i_ads_entries[num_ads - 1];
1089 if (init_ads_entry(new_entry, stream_name) != 0)
1092 new_entry->stream_id = inode->i_next_stream_id++;
1094 inode->i_num_ads = num_ads;
1100 /* Remove an alternate data stream from a WIM inode */
1101 void inode_remove_ads(struct wim_inode *inode, u16 idx,
1102 struct wim_lookup_table *lookup_table)
1104 struct wim_ads_entry *ads_entry;
1105 struct wim_lookup_table_entry *lte;
1107 wimlib_assert(idx < inode->i_num_ads);
1108 wimlib_assert(inode->i_resolved);
1110 ads_entry = &inode->i_ads_entries[idx];
1112 DEBUG("Remove alternate data stream \"%s\"", ads_entry->stream_name_utf8);
1114 lte = ads_entry->lte;
1116 lte_decrement_refcnt(lte, lookup_table);
1118 destroy_ads_entry(ads_entry);
1120 memmove(&inode->i_ads_entries[idx],
1121 &inode->i_ads_entries[idx + 1],
1122 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1130 * Reads the alternate data stream entries of a WIM dentry.
1132 * @p: Pointer to buffer that starts with the first alternate stream entry.
1134 * @inode: Inode to load the alternate data streams into.
1135 * @inode->i_num_ads must have been set to the number of
1136 * alternate data streams that are expected.
1138 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1141 * The format of the on-disk alternate stream entries is as follows:
1143 * struct wim_ads_entry_on_disk {
1144 * u64 length; // Length of the entry, in bytes. This includes
1145 * all fields (including the stream name and
1146 * null terminator if present, AND the padding!).
1147 * u64 reserved; // Seems to be unused
1148 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
1149 * u16 stream_name_len; // Length of the stream name, in bytes
1150 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
1151 * not including null terminator
1152 * u16 zero; // UTF-16 null terminator for the stream name, NOT
1153 * included in @stream_name_len. Based on what
1154 * I've observed from filenames in dentries,
1155 * this field should not exist when
1156 * (@stream_name_len == 0), but you can't
1157 * actually tell because of the padding anyway
1158 * (provided that the padding is zeroed, which
1159 * it always seems to be).
1160 * char padding[]; // Padding to make the size a multiple of 8 bytes.
1163 * In addition, the entries are 8-byte aligned.
1165 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1166 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1167 * failure, @inode is not modified.
1169 static int read_ads_entries(const u8 *p, struct wim_inode *inode,
1173 struct wim_ads_entry *ads_entries;
1176 num_ads = inode->i_num_ads;
1177 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1179 ERROR("Could not allocate memory for %"PRIu16" "
1180 "alternate data stream entries", num_ads);
1181 return WIMLIB_ERR_NOMEM;
1184 for (u16 i = 0; i < num_ads; i++) {
1185 struct wim_ads_entry *cur_entry;
1187 u64 length_no_padding;
1190 const u8 *p_save = p;
1192 cur_entry = &ads_entries[i];
1195 ads_entries[i].stream_id = i + 1;
1198 /* Read the base stream entry, excluding the stream name. */
1199 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1200 ERROR("Stream entries go past end of metadata resource");
1201 ERROR("(remaining_size = %"PRIu64")", remaining_size);
1202 ret = WIMLIB_ERR_INVALID_DENTRY;
1203 goto out_free_ads_entries;
1206 p = get_u64(p, &length);
1207 p += 8; /* Skip the reserved field */
1208 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
1209 p = get_u16(p, &cur_entry->stream_name_len);
1211 cur_entry->stream_name = NULL;
1212 cur_entry->stream_name_utf8 = NULL;
1214 /* Length including neither the null terminator nor the padding
1216 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1217 cur_entry->stream_name_len;
1219 /* Length including the null terminator and the padding */
1220 total_length = ((length_no_padding + 2) + 7) & ~7;
1222 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1224 if (remaining_size < length_no_padding) {
1225 ERROR("Stream entries go past end of metadata resource");
1226 ERROR("(remaining_size = %"PRIu64" bytes, "
1227 "length_no_padding = %"PRIu64" bytes)",
1228 remaining_size, length_no_padding);
1229 ret = WIMLIB_ERR_INVALID_DENTRY;
1230 goto out_free_ads_entries;
1233 /* The @length field in the on-disk ADS entry is expected to be
1234 * equal to @total_length, which includes all of the entry and
1235 * the padding that follows it to align the next ADS entry to an
1236 * 8-byte boundary. However, to be safe, we'll accept the
1237 * length field as long as it's not less than the un-padded
1238 * total length and not more than the padded total length. */
1239 if (length < length_no_padding || length > total_length) {
1240 ERROR("Stream entry has unexpected length "
1241 "field (length field = %"PRIu64", "
1242 "unpadded total length = %"PRIu64", "
1243 "padded total length = %"PRIu64")",
1244 length, length_no_padding, total_length);
1245 ret = WIMLIB_ERR_INVALID_DENTRY;
1246 goto out_free_ads_entries;
1249 if (cur_entry->stream_name_len) {
1250 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
1251 if (!cur_entry->stream_name) {
1252 ret = WIMLIB_ERR_NOMEM;
1253 goto out_free_ads_entries;
1255 get_bytes(p, cur_entry->stream_name_len,
1256 (u8*)cur_entry->stream_name);
1258 ret = utf16_to_utf8(cur_entry->stream_name,
1259 cur_entry->stream_name_len,
1260 &cur_entry->stream_name_utf8,
1263 goto out_free_ads_entries;
1264 cur_entry->stream_name_utf8_len = utf8_len;
1266 /* It's expected that the size of every ADS entry is a multiple
1267 * of 8. However, to be safe, I'm allowing the possibility of
1268 * an ADS entry at the very end of the metadata resource ending
1269 * un-aligned. So although we still need to increment the input
1270 * pointer by @total_length to reach the next ADS entry, it's
1271 * possible that less than @total_length is actually remaining
1272 * in the metadata resource. We should set the remaining size to
1273 * 0 bytes if this happens. */
1274 p = p_save + total_length;
1275 if (remaining_size < total_length)
1278 remaining_size -= total_length;
1280 inode->i_ads_entries = ads_entries;
1282 inode->i_next_stream_id = inode->i_num_ads + 1;
1285 out_free_ads_entries:
1286 for (u16 i = 0; i < num_ads; i++)
1287 destroy_ads_entry(&ads_entries[i]);
1293 * Reads a WIM directory entry, including all alternate data stream entries that
1294 * follow it, from the WIM image's metadata resource.
1296 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1297 * @metadata_resource_len: Length of the metadata resource.
1298 * @offset: Offset of this directory entry in the metadata resource.
1299 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1301 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1302 * been modified, but it will not be left with pointers to any allocated
1303 * buffers. On success, the dentry->length field must be examined. If zero,
1304 * this was a special "end of directory" dentry and not a real dentry. If
1305 * nonzero, this was a real dentry.
1307 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1308 u64 offset, struct wim_dentry *dentry)
1311 u64 calculated_size;
1312 char *file_name = NULL;
1313 char *file_name_utf8 = NULL;
1314 char *short_name = NULL;
1317 size_t file_name_utf8_len = 0;
1319 struct wim_inode *inode = NULL;
1321 dentry_common_init(dentry);
1323 /*Make sure the dentry really fits into the metadata resource.*/
1324 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1325 ERROR("Directory entry starting at %"PRIu64" ends past the "
1326 "end of the metadata resource (size %"PRIu64")",
1327 offset, metadata_resource_len);
1328 return WIMLIB_ERR_INVALID_DENTRY;
1331 /* Before reading the whole dentry, we need to read just the length.
1332 * This is because a dentry of length 8 (that is, just the length field)
1333 * terminates the list of sibling directory entries. */
1335 p = get_u64(&metadata_resource[offset], &dentry->length);
1337 /* A zero length field (really a length of 8, since that's how big the
1338 * directory entry is...) indicates that this is the end of directory
1339 * dentry. We do not read it into memory as an actual dentry, so just
1340 * return successfully in that case. */
1341 if (dentry->length == 0)
1344 /* If the dentry does not overflow the metadata resource buffer and is
1345 * not too short, read the rest of it (excluding the alternate data
1346 * streams, but including the file name and short name variable-length
1347 * fields) into memory. */
1348 if (offset + dentry->length >= metadata_resource_len
1349 || offset + dentry->length < offset)
1351 ERROR("Directory entry at offset %"PRIu64" and with size "
1352 "%"PRIu64" ends past the end of the metadata resource "
1354 offset, dentry->length, metadata_resource_len);
1355 return WIMLIB_ERR_INVALID_DENTRY;
1358 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1359 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1361 return WIMLIB_ERR_INVALID_DENTRY;
1364 inode = new_timeless_inode();
1366 return WIMLIB_ERR_NOMEM;
1368 p = get_u32(p, &inode->i_attributes);
1369 p = get_u32(p, (u32*)&inode->i_security_id);
1370 p = get_u64(p, &dentry->subdir_offset);
1372 /* 2 unused fields */
1373 p += 2 * sizeof(u64);
1374 /*p = get_u64(p, &dentry->unused1);*/
1375 /*p = get_u64(p, &dentry->unused2);*/
1377 p = get_u64(p, &inode->i_creation_time);
1378 p = get_u64(p, &inode->i_last_access_time);
1379 p = get_u64(p, &inode->i_last_write_time);
1381 p = get_bytes(p, SHA1_HASH_SIZE, inode->i_hash);
1384 * I don't know what's going on here. It seems like M$ screwed up the
1385 * reparse points, then put the fields in the same place and didn't
1386 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1387 * have something to do with this, but it's not documented.
1389 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1392 p = get_u32(p, &inode->i_reparse_tag);
1395 p = get_u32(p, &inode->i_reparse_tag);
1396 p = get_u64(p, &inode->i_ino);
1399 /* By the way, the reparse_reserved field does not actually exist (at
1400 * least when the file is not a reparse point) */
1402 p = get_u16(p, &inode->i_num_ads);
1404 p = get_u16(p, &short_name_len);
1405 p = get_u16(p, &file_name_len);
1407 /* We now know the length of the file name and short name. Make sure
1408 * the length of the dentry is large enough to actually hold them.
1410 * The calculated length here is unaligned to allow for the possibility
1411 * that the dentry->length names an unaligned length, although this
1412 * would be unexpected. */
1413 calculated_size = __dentry_correct_length_unaligned(file_name_len,
1416 if (dentry->length < calculated_size) {
1417 ERROR("Unexpected end of directory entry! (Expected "
1418 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1419 "short_name_len = %hu, file_name_len = %hu)",
1420 calculated_size, dentry->length,
1421 short_name_len, file_name_len);
1422 ret = WIMLIB_ERR_INVALID_DENTRY;
1423 goto out_free_inode;
1426 /* Read the filename if present. Note: if the filename is empty, there
1427 * is no null terminator following it. */
1428 if (file_name_len) {
1429 file_name = MALLOC(file_name_len);
1431 ERROR("Failed to allocate %hu bytes for dentry file name",
1433 ret = WIMLIB_ERR_NOMEM;
1434 goto out_free_inode;
1436 p = get_bytes(p, file_name_len, file_name);
1438 /* Convert filename to UTF-8. */
1439 ret = utf16_to_utf8(file_name, file_name_len, &file_name_utf8,
1440 &file_name_utf8_len);
1442 goto out_free_file_name;
1444 WARNING("Expected two zero bytes following the file name "
1445 "`%s', but found non-zero bytes", file_name_utf8);
1449 /* Align the calculated size */
1450 calculated_size = (calculated_size + 7) & ~7;
1452 if (dentry->length > calculated_size) {
1453 /* Weird; the dentry says it's longer than it should be. Note
1454 * that the length field does NOT include the size of the
1455 * alternate stream entries. */
1457 /* Strangely, some directory entries inexplicably have a little
1458 * over 70 bytes of extra data. The exact amount of data seems
1459 * to be 72 bytes, but it is aligned on the next 8-byte
1460 * boundary. It does NOT seem to be alternate data stream
1461 * entries. Here's an example of the aligned data:
1463 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1464 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1465 * 00000000 00000000 00000000 00000000
1467 * Here's one interpretation of how the data is laid out.
1470 * u32 field1; (always 0x00000001)
1471 * u32 field2; (always 0x40000000)
1472 * u8 data[48]; (???)
1473 * u64 reserved1; (always 0)
1474 * u64 reserved2; (always 0)
1476 DEBUG("Dentry for file or directory `%s' has %"PRIu64" extra "
1478 file_name_utf8, dentry->length - calculated_size);
1481 /* Read the short filename if present. Note: if there is no short
1482 * filename, there is no null terminator following it. */
1483 if (short_name_len) {
1484 short_name = MALLOC(short_name_len);
1486 ERROR("Failed to allocate %hu bytes for short filename",
1488 ret = WIMLIB_ERR_NOMEM;
1489 goto out_free_file_name_utf8;
1492 p = get_bytes(p, short_name_len, short_name);
1494 WARNING("Expected two zero bytes following the short name of "
1495 "`%s', but found non-zero bytes", file_name_utf8);
1500 * Read the alternate data streams, if present. dentry->num_ads tells
1501 * us how many they are, and they will directly follow the dentry
1504 * Note that each alternate data stream entry begins on an 8-byte
1505 * aligned boundary, and the alternate data stream entries are NOT
1506 * included in the dentry->length field for some reason.
1508 if (inode->i_num_ads != 0) {
1510 /* Trying different lengths is just a hack to make sure we have
1511 * a chance of reading the ADS entries correctly despite the
1512 * poor documentation. */
1514 if (calculated_size != dentry->length) {
1515 WARNING("Trying calculated dentry length (%"PRIu64") "
1516 "instead of dentry->length field (%"PRIu64") "
1517 "to read ADS entries",
1518 calculated_size, dentry->length);
1520 u64 lengths_to_try[3] = {calculated_size,
1521 (dentry->length + 7) & ~7,
1523 ret = WIMLIB_ERR_INVALID_DENTRY;
1524 for (size_t i = 0; i < ARRAY_LEN(lengths_to_try); i++) {
1525 if (lengths_to_try[i] > metadata_resource_len - offset)
1527 ret = read_ads_entries(&metadata_resource[offset + lengths_to_try[i]],
1529 metadata_resource_len - offset - lengths_to_try[i]);
1533 ERROR("Failed to read alternate data stream "
1534 "entries of `%s'", dentry->file_name_utf8);
1535 goto out_free_short_name;
1539 /* We've read all the data for this dentry. Set the names and their
1540 * lengths, and we've done. */
1541 dentry->d_inode = inode;
1542 dentry->file_name = file_name;
1543 dentry->file_name_utf8 = file_name_utf8;
1544 dentry->short_name = short_name;
1545 dentry->file_name_len = file_name_len;
1546 dentry->file_name_utf8_len = file_name_utf8_len;
1547 dentry->short_name_len = short_name_len;
1549 out_free_short_name:
1551 out_free_file_name_utf8:
1552 FREE(file_name_utf8);
1560 /* Reads the children of a dentry, and all their children, ..., etc. from the
1561 * metadata resource and into the dentry tree.
1563 * @metadata_resource: An array that contains the uncompressed metadata
1564 * resource for the WIM file.
1566 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1569 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
1570 * tree and has already been read from the metadata resource. It
1571 * does not need to be the real root because this procedure is
1572 * called recursively.
1574 * @return: Zero on success, nonzero on failure.
1576 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1577 struct wim_dentry *dentry)
1579 u64 cur_offset = dentry->subdir_offset;
1580 struct wim_dentry *child;
1581 struct wim_dentry cur_child;
1585 * If @dentry has no child dentries, nothing more needs to be done for
1586 * this branch. This is the case for regular files, symbolic links, and
1587 * *possibly* empty directories (although an empty directory may also
1588 * have one child dentry that is the special end-of-directory dentry)
1590 if (cur_offset == 0)
1593 /* Find and read all the children of @dentry. */
1596 /* Read next child of @dentry into @cur_child. */
1597 ret = read_dentry(metadata_resource, metadata_resource_len,
1598 cur_offset, &cur_child);
1602 /* Check for end of directory. */
1603 if (cur_child.length == 0)
1606 /* Not end of directory. Allocate this child permanently and
1607 * link it to the parent and previous child. */
1608 child = MALLOC(sizeof(struct wim_dentry));
1610 ERROR("Failed to allocate %zu bytes for new dentry",
1611 sizeof(struct wim_dentry));
1612 ret = WIMLIB_ERR_NOMEM;
1615 memcpy(child, &cur_child, sizeof(struct wim_dentry));
1616 dentry_add_child(dentry, child);
1617 inode_add_dentry(child, child->d_inode);
1619 /* If there are children of this child, call this procedure
1621 if (child->subdir_offset != 0) {
1622 ret = read_dentry_tree(metadata_resource,
1623 metadata_resource_len, child);
1628 /* Advance to the offset of the next child. Note: We need to
1629 * advance by the TOTAL length of the dentry, not by the length
1630 * child->length, which although it does take into account the
1631 * padding, it DOES NOT take into account alternate stream
1633 cur_offset += dentry_total_length(child);
1639 * Writes a WIM dentry to an output buffer.
1641 * @dentry: The dentry structure.
1642 * @p: The memory location to write the data to.
1643 * @return: Pointer to the byte after the last byte we wrote as part of the
1646 static u8 *write_dentry(const struct wim_dentry *dentry, u8 *p)
1650 const struct wim_inode *inode = dentry->d_inode;
1652 /* We calculate the correct length of the dentry ourselves because the
1653 * dentry->length field may been set to an unexpected value from when we
1654 * read the dentry in (for example, there may have been unknown data
1655 * appended to the end of the dentry...) */
1656 u64 length = dentry_correct_length(dentry);
1658 p = put_u64(p, length);
1659 p = put_u32(p, inode->i_attributes);
1660 p = put_u32(p, inode->i_security_id);
1661 p = put_u64(p, dentry->subdir_offset);
1662 p = put_u64(p, 0); /* unused1 */
1663 p = put_u64(p, 0); /* unused2 */
1664 p = put_u64(p, inode->i_creation_time);
1665 p = put_u64(p, inode->i_last_access_time);
1666 p = put_u64(p, inode->i_last_write_time);
1667 hash = inode_stream_hash(inode, 0);
1668 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1669 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1670 p = put_zeroes(p, 4);
1671 p = put_u32(p, inode->i_reparse_tag);
1672 p = put_zeroes(p, 4);
1676 if (inode->i_nlink == 1)
1679 link_group_id = inode->i_ino;
1680 p = put_u64(p, link_group_id);
1682 p = put_u16(p, inode->i_num_ads);
1683 p = put_u16(p, dentry->short_name_len);
1684 p = put_u16(p, dentry->file_name_len);
1685 if (dentry->file_name_len) {
1686 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1687 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1689 if (dentry->short_name) {
1690 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1691 p = put_u16(p, 0); /* short name padding, 2 bytes */
1694 /* Align to 8-byte boundary */
1695 wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1696 p = put_zeroes(p, length - (p - orig_p));
1698 /* Write the alternate data streams, if there are any. Please see
1699 * read_ads_entries() for comments about the format of the on-disk
1700 * alternate data stream entries. */
1701 for (u16 i = 0; i < inode->i_num_ads; i++) {
1702 p = put_u64(p, ads_entry_total_length(&inode->i_ads_entries[i]));
1703 p = put_u64(p, 0); /* Unused */
1704 hash = inode_stream_hash(inode, i + 1);
1705 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1706 p = put_u16(p, inode->i_ads_entries[i].stream_name_len);
1707 if (inode->i_ads_entries[i].stream_name_len) {
1708 p = put_bytes(p, inode->i_ads_entries[i].stream_name_len,
1709 (u8*)inode->i_ads_entries[i].stream_name);
1712 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1714 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1718 static int write_dentry_cb(struct wim_dentry *dentry, void *_p)
1721 *p = write_dentry(dentry, *p);
1725 static u8 *write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
1727 static int write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
1730 *p = write_dentry_tree_recursive(dentry, *p);
1734 /* Recursive function that writes a dentry tree rooted at @parent, not including
1735 * @parent itself, which has already been written. */
1736 static u8 *write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
1738 /* Nothing to do if this dentry has no children. */
1739 if (parent->subdir_offset == 0)
1742 /* Write child dentries and end-of-directory entry.
1744 * Note: we need to write all of this dentry's children before
1745 * recursively writing the directory trees rooted at each of the child
1746 * dentries, since the on-disk dentries for a dentry's children are
1747 * always located at consecutive positions in the metadata resource! */
1748 for_dentry_child(parent, write_dentry_cb, &p);
1750 /* write end of directory entry */
1753 /* Recurse on children. */
1754 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
1758 /* Writes a directory tree to the metadata resource.
1760 * @root: Root of the dentry tree.
1761 * @p: Pointer to a buffer with enough space for the dentry tree.
1763 * Returns pointer to the byte after the last byte we wrote.
1765 u8 *write_dentry_tree(const struct wim_dentry *root, u8 *p)
1767 DEBUG("Writing dentry tree.");
1768 wimlib_assert(dentry_is_root(root));
1770 /* If we're the root dentry, we have no parent that already
1771 * wrote us, so we need to write ourselves. */
1772 p = write_dentry(root, p);
1774 /* Write end of directory entry after the root dentry just to be safe;
1775 * however the root dentry obviously cannot have any siblings. */
1778 /* Recursively write the rest of the dentry tree. */
1779 return write_dentry_tree_recursive(root, p);