4 * A dentry (directory entry) contains the metadata for a file. In the WIM file
5 * format, the dentries are stored in the "metadata resource" section right
6 * after the security data. Each image in the WIM file has its own metadata
7 * resource with its own security data and dentry tree. Dentries in different
8 * images may share file resources by referring to the same lookup table
14 * Copyright (C) 2010 Carl Thijssen
15 * Copyright (C) 2012 Eric Biggers
17 * This file is part of wimlib, a library for working with WIM files.
19 * wimlib is free software; you can redistribute it and/or modify it under the
20 * terms of the GNU Lesser General Public License as published by the Free
21 * Software Foundation; either version 2.1 of the License, or (at your option)
24 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
25 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
26 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with wimlib; if not, see http://www.gnu.org/licenses/.
40 #include "lookup_table.h"
42 #include "timestamp.h"
43 #include "wimlib_internal.h"
46 * Returns true if @dentry has the UTF-8 file name @name that has length
49 static bool dentry_has_name(const struct dentry *dentry, const char *name,
52 if (dentry->file_name_utf8_len != name_len)
54 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
57 static u64 __dentry_correct_length_unaligned(u16 file_name_len,
60 u64 length = WIM_DENTRY_DISK_SIZE;
62 length += file_name_len + 2;
64 length += short_name_len + 2;
68 static u64 dentry_correct_length_unaligned(const struct dentry *dentry)
70 return __dentry_correct_length_unaligned(dentry->file_name_len,
71 dentry->short_name_len);
74 /* Return the "correct" value to write in the length field of the dentry, based
75 * on the file name length and short name length */
76 static u64 dentry_correct_length(const struct dentry *dentry)
78 return (dentry_correct_length_unaligned(dentry) + 7) & ~7;
81 static u64 __dentry_total_length(const struct dentry *dentry, u64 length)
83 for (u16 i = 0; i < dentry->num_ads; i++)
84 length += ads_entry_total_length(&dentry->ads_entries[i]);
85 return (length + 7) & ~7;
88 u64 dentry_correct_total_length(const struct dentry *dentry)
90 return __dentry_total_length(dentry,
91 dentry_correct_length_unaligned(dentry));
94 /* Real length of a dentry, including the alternate data stream entries, which
95 * are not included in the dentry->length field... */
96 u64 dentry_total_length(const struct dentry *dentry)
98 return __dentry_total_length(dentry, dentry->length);
101 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
102 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
104 if (S_ISLNK(stbuf->st_mode)) {
105 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
106 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
107 } else if (S_ISDIR(stbuf->st_mode)) {
108 dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
110 dentry->attributes = FILE_ATTRIBUTE_NORMAL;
112 if (sizeof(ino_t) >= 8)
113 dentry->hard_link = (u64)stbuf->st_ino;
115 dentry->hard_link = (u64)stbuf->st_ino |
116 ((u64)stbuf->st_dev << (sizeof(ino_t) * 8));
118 dentry->creation_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
119 dentry->last_write_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
120 dentry->last_access_time = timespec_to_wim_timestamp(&stbuf->st_atim);
124 /* Sets all the timestamp fields of the dentry to the current time. */
125 void dentry_update_all_timestamps(struct dentry *dentry)
127 u64 now = get_wim_timestamp();
128 dentry->creation_time = now;
129 dentry->last_access_time = now;
130 dentry->last_write_time = now;
133 /* Returns the alternate data stream entry belonging to @dentry that has the
134 * stream name @stream_name. */
135 struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
136 const char *stream_name)
138 size_t stream_name_len;
141 if (dentry->num_ads) {
143 stream_name_len = strlen(stream_name);
145 if (ads_entry_has_name(&dentry->ads_entries[i],
146 stream_name, stream_name_len))
147 return &dentry->ads_entries[i];
148 } while (++i != dentry->num_ads);
153 static void ads_entry_init(struct ads_entry *ads_entry)
155 memset(ads_entry, 0, sizeof(struct ads_entry));
156 INIT_LIST_HEAD(&ads_entry->lte_group_list.list);
157 ads_entry->lte_group_list.type = STREAM_TYPE_ADS;
161 * Add an alternate stream entry to a dentry and return a pointer to it, or NULL
162 * if memory could not be allocated.
164 struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
167 struct ads_entry *ads_entries;
168 struct ads_entry *new_entry;
170 DEBUG("Add alternate data stream %s:%s",
171 dentry->file_name_utf8, stream_name);
173 if (dentry->num_ads == 0xffff)
175 num_ads = dentry->num_ads + 1;
176 ads_entries = REALLOC(dentry->ads_entries,
177 num_ads * sizeof(struct ads_entry));
180 if (ads_entries != dentry->ads_entries) {
181 /* We moved the ADS entries. Adjust the stream lists. */
182 for (u16 i = 0; i < dentry->num_ads; i++) {
183 struct list_head *cur = &ads_entries[i].lte_group_list.list;
184 cur->prev->next = cur;
185 cur->next->prev = cur;
189 new_entry = &ads_entries[num_ads - 1];
190 ads_entry_init(new_entry);
191 if (change_ads_name(new_entry, stream_name) != 0)
193 dentry->ads_entries = ads_entries;
194 dentry->num_ads = num_ads;
198 /* Remove an alternate data stream from a dentry.
200 * The corresponding lookup table entry for the stream is NOT changed.
202 * @dentry: The dentry
203 * @ads_entry: The alternate data stream entry (it MUST be one of the
204 * ads_entry's in the array dentry->ads_entries).
206 void dentry_remove_ads(struct dentry *dentry, struct ads_entry *ads_entry)
211 wimlib_assert(dentry->num_ads);
212 idx = ads_entry - dentry->ads_entries;
213 wimlib_assert(idx < dentry->num_ads);
214 following = dentry->num_ads - idx - 1;
216 destroy_ads_entry(ads_entry);
217 memcpy(ads_entry, ads_entry + 1, following * sizeof(struct ads_entry));
219 /* We moved the ADS entries. Adjust the stream lists. */
220 for (u16 i = 0; i < following; i++) {
221 struct list_head *cur = &ads_entry[i].lte_group_list.list;
222 cur->prev->next = cur;
223 cur->next->prev = cur;
230 * Calls a function on all directory entries in a directory tree. It is called
231 * on a parent before its children.
233 int for_dentry_in_tree(struct dentry *root,
234 int (*visitor)(struct dentry*, void*), void *arg)
237 struct dentry *child;
239 ret = visitor(root, arg);
244 child = root->children;
250 ret = for_dentry_in_tree(child, visitor, arg);
254 } while (child != root->children);
259 * Like for_dentry_in_tree(), but the visitor function is always called on a
260 * dentry's children before on itself.
262 int for_dentry_in_tree_depth(struct dentry *root,
263 int (*visitor)(struct dentry*, void*), void *arg)
266 struct dentry *child;
269 child = root->children;
273 ret = for_dentry_in_tree_depth(child, visitor, arg);
277 } while (child != root->children);
279 return visitor(root, arg);
283 * Calculate the full path of @dentry, based on its parent's full path and on
284 * its UTF-8 file name.
286 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
290 if (dentry_is_root(dentry)) {
291 full_path = MALLOC(2);
298 char *parent_full_path;
299 u32 parent_full_path_len;
300 const struct dentry *parent = dentry->parent;
302 if (dentry_is_root(parent)) {
303 parent_full_path = "";
304 parent_full_path_len = 0;
306 parent_full_path = parent->full_path_utf8;
307 parent_full_path_len = parent->full_path_utf8_len;
310 full_path_len = parent_full_path_len + 1 +
311 dentry->file_name_utf8_len;
312 full_path = MALLOC(full_path_len + 1);
316 memcpy(full_path, parent_full_path, parent_full_path_len);
317 full_path[parent_full_path_len] = '/';
318 memcpy(full_path + parent_full_path_len + 1,
319 dentry->file_name_utf8,
320 dentry->file_name_utf8_len);
321 full_path[full_path_len] = '\0';
323 FREE(dentry->full_path_utf8);
324 dentry->full_path_utf8 = full_path;
325 dentry->full_path_utf8_len = full_path_len;
328 ERROR("Out of memory while calculating dentry full path");
329 return WIMLIB_ERR_NOMEM;
333 * Recursively calculates the subdir offsets for a directory tree.
335 * @dentry: The root of the directory tree.
336 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
337 * offset for @dentry.
339 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
341 struct dentry *child;
343 child = dentry->children;
344 dentry->subdir_offset = *subdir_offset_p;
347 /* Advance the subdir offset by the amount of space the children
348 * of this dentry take up. */
350 *subdir_offset_p += dentry_correct_total_length(child);
352 } while (child != dentry->children);
354 /* End-of-directory dentry on disk. */
355 *subdir_offset_p += 8;
357 /* Recursively call calculate_subdir_offsets() on all the
360 calculate_subdir_offsets(child, subdir_offset_p);
362 } while (child != dentry->children);
364 /* On disk, childless directories have a valid subdir_offset
365 * that points to an 8-byte end-of-directory dentry. Regular
366 * files or reparse points have a subdir_offset of 0. */
367 if (dentry_is_directory(dentry))
368 *subdir_offset_p += 8;
370 dentry->subdir_offset = 0;
375 /* Returns the child of @dentry that has the file name @name.
376 * Returns NULL if no child has the name. */
377 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
380 struct dentry *child;
383 child = dentry->children;
385 name_len = strlen(name);
387 if (dentry_has_name(child, name, name_len))
390 } while (child != dentry->children);
395 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
396 * @cur_dir. Returns NULL if no dentry having the path is found. */
397 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir,
400 struct dentry *child;
402 const char *new_path;
407 child = cur_dir->children;
409 new_path = path_next_part(path, &base_len);
411 if (dentry_has_name(child, path, base_len))
412 return get_dentry_relative_path(child, new_path);
414 } while (child != cur_dir->children);
419 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
421 struct dentry *get_dentry(WIMStruct *w, const char *path)
423 struct dentry *root = wim_root_dentry(w);
426 return get_dentry_relative_path(root, path);
429 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
430 * if the dentry is not found. */
431 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
433 size_t path_len = strlen(path);
434 char buf[path_len + 1];
436 memcpy(buf, path, path_len + 1);
438 to_parent_name(buf, path_len);
440 return get_dentry(w, buf);
443 /* Prints the full path of a dentry. */
444 int print_dentry_full_path(struct dentry *dentry, void *ignore)
446 if (dentry->full_path_utf8)
447 puts(dentry->full_path_utf8);
451 /* We want to be able to show the names of the file attribute flags that are
453 struct file_attr_flag {
457 struct file_attr_flag file_attr_flags[] = {
458 {FILE_ATTRIBUTE_READONLY, "READONLY"},
459 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
460 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
461 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
462 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
463 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
464 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
465 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
466 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
467 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
468 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
469 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
470 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
471 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
472 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
475 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
476 * available. If the dentry is unresolved and the lookup table is NULL, the
477 * lookup table entries will not be printed. Otherwise, they will be. */
478 int print_dentry(struct dentry *dentry, void *lookup_table)
481 struct lookup_table_entry *lte;
485 printf("[DENTRY]\n");
486 printf("Length = %"PRIu64"\n", dentry->length);
487 printf("Attributes = 0x%x\n", dentry->attributes);
488 for (unsigned i = 0; i < ARRAY_LEN(file_attr_flags); i++)
489 if (file_attr_flags[i].flag & dentry->attributes)
490 printf(" FILE_ATTRIBUTE_%s is set\n",
491 file_attr_flags[i].name);
492 printf("Security ID = %d\n", dentry->security_id);
493 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
495 printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);
496 printf("Unused2 = %"PRIu64"\n", dentry->unused2);
499 printf("Creation Time = 0x%"PRIx64"\n");
500 printf("Last Access Time = 0x%"PRIx64"\n");
501 printf("Last Write Time = 0x%"PRIx64"\n");
504 /* Translate the timestamps into something readable */
505 time_t creat_time = wim_timestamp_to_unix(dentry->creation_time);
506 time_t access_time = wim_timestamp_to_unix(dentry->last_access_time);
507 time_t mod_time = wim_timestamp_to_unix(dentry->last_write_time);
509 time = wim_timestamp_to_unix(dentry->creation_time);
510 p = asctime(gmtime(&time));
511 *(strrchr(p, '\n')) = '\0';
512 printf("Creation Time = %s UTC\n", p);
514 time = wim_timestamp_to_unix(dentry->last_access_time);
515 p = asctime(gmtime(&time));
516 *(strrchr(p, '\n')) = '\0';
517 printf("Last Access Time = %s UTC\n", p);
519 time = wim_timestamp_to_unix(dentry->last_write_time);
520 p = asctime(gmtime(&time));
521 *(strrchr(p, '\n')) = '\0';
522 printf("Last Write Time = %s UTC\n", p);
524 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
525 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
526 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
527 printf("Filename = \"");
528 print_string(dentry->file_name, dentry->file_name_len);
530 printf("Filename Length = %hu\n", dentry->file_name_len);
531 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
532 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
533 printf("Short Name = \"");
534 print_string(dentry->short_name, dentry->short_name_len);
536 printf("Short Name Length = %hu\n", dentry->short_name_len);
537 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
538 lte = dentry_stream_lte(dentry, 0, lookup_table);
540 print_lookup_table_entry(lte);
542 hash = dentry_stream_hash(dentry, 0);
550 for (u16 i = 0; i < dentry->num_ads; i++) {
551 printf("[Alternate Stream Entry %u]\n", i);
552 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
553 printf("Name Length (UTF-16) = %u\n",
554 dentry->ads_entries[i].stream_name_len);
555 hash = dentry_stream_hash(dentry, i + 1);
561 print_lookup_table_entry(dentry_stream_lte(dentry, i + 1,
567 /* Initializations done on every `struct dentry'. */
568 static void dentry_common_init(struct dentry *dentry)
570 memset(dentry, 0, sizeof(struct dentry));
572 dentry->security_id = -1;
573 dentry->ads_entries_status = ADS_ENTRIES_DEFAULT;
574 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
578 * Creates an unlinked directory entry.
580 * @name: The UTF-8 filename of the new dentry.
582 * Returns a pointer to the new dentry, or NULL if out of memory.
584 struct dentry *new_dentry(const char *name)
586 struct dentry *dentry;
588 dentry = MALLOC(sizeof(struct dentry));
592 dentry_common_init(dentry);
593 if (change_dentry_name(dentry, name) != 0)
596 dentry_update_all_timestamps(dentry);
597 dentry->next = dentry;
598 dentry->prev = dentry;
599 dentry->parent = dentry;
600 INIT_LIST_HEAD(&dentry->link_group_list);
604 ERROR("Failed to allocate new dentry");
608 void dentry_free_ads_entries(struct dentry *dentry)
610 for (u16 i = 0; i < dentry->num_ads; i++)
611 destroy_ads_entry(&dentry->ads_entries[i]);
612 FREE(dentry->ads_entries);
613 dentry->ads_entries = NULL;
617 static void __destroy_dentry(struct dentry *dentry)
619 FREE(dentry->file_name);
620 FREE(dentry->file_name_utf8);
621 FREE(dentry->short_name);
622 FREE(dentry->full_path_utf8);
623 FREE(dentry->extracted_file);
626 /* Frees a WIM dentry. */
627 void free_dentry(struct dentry *dentry)
629 wimlib_assert(dentry);
630 __destroy_dentry(dentry);
631 /* Don't destroy the ADS entries if they "belong" to a different dentry
633 if (dentry->ads_entries_status != ADS_ENTRIES_USER)
634 dentry_free_ads_entries(dentry);
638 /* Like free_dentry(), but assigns a new ADS entries owner if this dentry was
639 * the previous owner, and also deletes the dentry from its link_group_list */
640 void put_dentry(struct dentry *dentry)
642 if (dentry->ads_entries_status == ADS_ENTRIES_OWNER) {
643 struct dentry *new_owner;
644 list_for_each_entry(new_owner, &dentry->link_group_list,
647 if (new_owner->ads_entries_status == ADS_ENTRIES_USER) {
648 new_owner->ads_entries_status = ADS_ENTRIES_OWNER;
652 dentry->ads_entries_status = ADS_ENTRIES_USER;
654 struct list_head *next;
655 list_del(&dentry->link_group_list);
660 /* Partically clones a dentry.
663 * - memory for file names is not cloned (the pointers are all set to NULL
664 * and the lengths are set to zero)
665 * - next, prev, and children pointers and not touched
666 * - stream entries are not cloned (pointer left untouched).
668 struct dentry *clone_dentry(struct dentry *old)
670 struct dentry *new = MALLOC(sizeof(struct dentry));
673 memcpy(new, old, sizeof(struct dentry));
674 new->file_name = NULL;
675 new->file_name_len = 0;
676 new->file_name_utf8 = NULL;
677 new->file_name_utf8_len = 0;
678 new->short_name = NULL;
679 new->short_name_len = 0;
684 * This function is passed as an argument to for_dentry_in_tree_depth() in order
685 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
687 static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
689 struct lookup_table *lookup_table = __lookup_table;
690 struct lookup_table_entry *lte;
694 for (i = 0; i <= dentry->num_ads; i++) {
695 lte = dentry_stream_lte(dentry, i, lookup_table);
696 lte_decrement_refcnt(lte, lookup_table);
700 wimlib_assert(dentry->refcnt != 0);
701 if (--dentry->refcnt == 0)
707 * Unlinks and frees a dentry tree.
709 * @root: The root of the tree.
710 * @lookup_table: The lookup table for dentries. If non-NULL, the
711 * reference counts in the lookup table for the lookup
712 * table entries corresponding to the dentries will be
715 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
717 if (!root || !root->parent)
719 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
722 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
729 * Links a dentry into the directory tree.
731 * @dentry: The dentry to link.
732 * @parent: The dentry that will be the parent of @dentry.
734 void link_dentry(struct dentry *dentry, struct dentry *parent)
736 wimlib_assert(dentry_is_directory(parent));
737 dentry->parent = parent;
738 if (parent->children) {
739 /* Not an only child; link to siblings. */
740 dentry->next = parent->children;
741 dentry->prev = parent->children->prev;
742 dentry->next->prev = dentry;
743 dentry->prev->next = dentry;
745 /* Only child; link to parent. */
746 parent->children = dentry;
747 dentry->next = dentry;
748 dentry->prev = dentry;
753 /* Unlink a dentry from the directory tree.
755 * Note: This merely removes it from the in-memory tree structure. See
756 * remove_dentry() in mount.c for a function implemented on top of this one that
757 * frees the dentry and implements reference counting for the lookup table
759 void unlink_dentry(struct dentry *dentry)
761 if (dentry_is_root(dentry))
763 if (dentry_is_only_child(dentry)) {
764 dentry->parent->children = NULL;
766 if (dentry_is_first_sibling(dentry))
767 dentry->parent->children = dentry->next;
768 dentry->next->prev = dentry->prev;
769 dentry->prev->next = dentry->next;
773 /* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
774 * and their lengths in the pointer arguments */
775 int get_names(char **name_utf16_ret, char **name_utf8_ret,
776 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
781 char *name_utf16, *name_utf8;
783 utf8_len = strlen(name);
785 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
788 return WIMLIB_ERR_NOMEM;
790 name_utf8 = MALLOC(utf8_len + 1);
793 return WIMLIB_ERR_NOMEM;
795 memcpy(name_utf8, name, utf8_len + 1);
796 FREE(*name_utf8_ret);
797 FREE(*name_utf16_ret);
798 *name_utf8_ret = name_utf8;
799 *name_utf16_ret = name_utf16;
800 *name_utf8_len_ret = utf8_len;
801 *name_utf16_len_ret = utf16_len;
805 /* Changes the name of a dentry to @new_name. Only changes the file_name and
806 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
807 * full_path_utf8 fields. Also recalculates its length. */
808 int change_dentry_name(struct dentry *dentry, const char *new_name)
812 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
813 &dentry->file_name_len, &dentry->file_name_utf8_len,
816 dentry->length = dentry_correct_length(dentry);
821 * Changes the name of an alternate data stream */
822 int change_ads_name(struct ads_entry *entry, const char *new_name)
824 return get_names(&entry->stream_name, &entry->stream_name_utf8,
825 &entry->stream_name_len,
826 &entry->stream_name_utf8_len,
830 /* Parameters for calculate_dentry_statistics(). */
831 struct image_statistics {
832 struct lookup_table *lookup_table;
836 u64 *hard_link_bytes;
839 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
841 struct image_statistics *stats;
842 struct lookup_table_entry *lte;
846 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
849 ++*stats->file_count;
851 for (unsigned i = 0; i <= dentry->num_ads; i++) {
852 lte = dentry_stream_lte(dentry, i, stats->lookup_table);
854 *stats->total_bytes += wim_resource_size(lte);
855 if (++lte->out_refcnt == 1)
856 *stats->hard_link_bytes += wim_resource_size(lte);
862 /* Calculates some statistics about a dentry tree. */
863 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
864 u64 *dir_count_ret, u64 *file_count_ret,
865 u64 *total_bytes_ret,
866 u64 *hard_link_bytes_ret)
868 struct image_statistics stats;
871 *total_bytes_ret = 0;
872 *hard_link_bytes_ret = 0;
873 stats.lookup_table = table;
874 stats.dir_count = dir_count_ret;
875 stats.file_count = file_count_ret;
876 stats.total_bytes = total_bytes_ret;
877 stats.hard_link_bytes = hard_link_bytes_ret;
878 for_lookup_table_entry(table, zero_out_refcnts, NULL);
879 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
884 * Reads the alternate data stream entries for a dentry.
886 * @p: Pointer to buffer that starts with the first alternate stream entry.
888 * @dentry: Dentry to load the alternate data streams into.
889 * @dentry->num_ads must have been set to the number of
890 * alternate data streams that are expected.
892 * @remaining_size: Number of bytes of data remaining in the buffer pointed
895 * The format of the on-disk alternate stream entries is as follows:
897 * struct ads_entry_on_disk {
898 * u64 length; // Length of the entry, in bytes. This includes
899 * all fields (including the stream name and
900 * null terminator if present, AND the padding!).
901 * u64 reserved; // Seems to be unused
902 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
903 * u16 stream_name_len; // Length of the stream name, in bytes
904 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
905 * not including null terminator
906 * u16 zero; // UTF-16 null terminator for the stream name, NOT
907 * included in @stream_name_len. Based on what
908 * I've observed from filenames in dentries,
909 * this field should not exist when
910 * (@stream_name_len == 0), but you can't
911 * actually tell because of the padding anyway
912 * (provided that the padding is zeroed, which
913 * it always seems to be).
914 * char padding[]; // Padding to make the size a multiple of 8 bytes.
917 * In addition, the entries are 8-byte aligned.
919 * Return 0 on success or nonzero on failure. On success, dentry->ads_entries
920 * is set to an array of `struct ads_entry's of length dentry->num_ads. On
921 * failure, @dentry is not modified.
923 static int read_ads_entries(const u8 *p, struct dentry *dentry,
927 struct ads_entry *ads_entries;
930 num_ads = dentry->num_ads;
931 ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
933 ERROR("Could not allocate memory for %"PRIu16" "
934 "alternate data stream entries", num_ads);
935 return WIMLIB_ERR_NOMEM;
938 for (u16 i = 0; i < num_ads; i++) {
939 struct ads_entry *cur_entry = &ads_entries[i];
941 u64 length_no_padding;
944 const char *p_save = p;
946 /* Read the base stream entry, excluding the stream name. */
947 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
948 ERROR("Stream entries go past end of metadata resource");
949 ERROR("(remaining_size = %"PRIu64")", remaining_size);
950 ret = WIMLIB_ERR_INVALID_DENTRY;
951 goto out_free_ads_entries;
954 p = get_u64(p, &length);
955 p += 8; /* Skip the reserved field */
956 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
957 p = get_u16(p, &cur_entry->stream_name_len);
959 cur_entry->stream_name = NULL;
960 cur_entry->stream_name_utf8 = NULL;
962 /* Length including neither the null terminator nor the padding
964 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
965 cur_entry->stream_name_len;
967 /* Length including the null terminator and the padding */
968 total_length = ((length_no_padding + 2) + 7) & ~7;
970 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
972 if (remaining_size < length_no_padding) {
973 ERROR("Stream entries go past end of metadata resource");
974 ERROR("(remaining_size = %"PRIu64" bytes, "
975 "length_no_padding = %"PRIu16" bytes)",
976 remaining_size, length_no_padding);
977 ret = WIMLIB_ERR_INVALID_DENTRY;
978 goto out_free_ads_entries;
981 /* The @length field in the on-disk ADS entry is expected to be
982 * equal to @total_length, which includes all of the entry and
983 * the padding that follows it to align the next ADS entry to an
984 * 8-byte boundary. However, to be safe, we'll accept the
985 * length field as long as it's not less than the un-padded
986 * total length and not more than the padded total length. */
987 if (length < length_no_padding || length > total_length) {
988 ERROR("Stream entry has unexpected length "
989 "field (length field = %"PRIu64", "
990 "unpadded total length = %"PRIu64", "
991 "padded total length = %"PRIu64")",
992 length, length_no_padding, total_length);
993 ret = WIMLIB_ERR_INVALID_DENTRY;
994 goto out_free_ads_entries;
997 if (cur_entry->stream_name_len) {
998 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
999 if (!cur_entry->stream_name) {
1000 ret = WIMLIB_ERR_NOMEM;
1001 goto out_free_ads_entries;
1003 get_bytes(p, cur_entry->stream_name_len,
1004 (u8*)cur_entry->stream_name);
1005 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
1006 cur_entry->stream_name_len,
1008 cur_entry->stream_name_utf8_len = utf8_len;
1010 if (!cur_entry->stream_name_utf8) {
1011 ret = WIMLIB_ERR_NOMEM;
1012 goto out_free_ads_entries;
1015 /* It's expected that the size of every ADS entry is a multiple
1016 * of 8. However, to be safe, I'm allowing the possibility of
1017 * an ADS entry at the very end of the metadata resource ending
1018 * un-aligned. So although we still need to increment the input
1019 * pointer by @total_length to reach the next ADS entry, it's
1020 * possible that less than @total_length is actually remaining
1021 * in the metadata resource. We should set the remaining size to
1022 * 0 bytes if this happens. */
1023 p = p_save + total_length;
1024 if (remaining_size < total_length)
1027 remaining_size -= total_length;
1029 dentry->ads_entries = ads_entries;
1031 out_free_ads_entries:
1032 for (u16 i = 0; i < num_ads; i++) {
1033 FREE(ads_entries[i].stream_name);
1034 FREE(ads_entries[i].stream_name_utf8);
1041 * Reads a directory entry, including all alternate data stream entries that
1042 * follow it, from the WIM image's metadata resource.
1044 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1045 * @metadata_resource_len: Length of the metadata resource.
1046 * @offset: Offset of this directory entry in the metadata resource.
1047 * @dentry: A `struct dentry' that will be filled in by this function.
1049 * Return 0 on success or nonzero on failure. On failure, @dentry have been
1050 * modified, bu it will be left with no pointers to any allocated buffers.
1051 * On success, the dentry->length field must be examined. If zero, this was a
1052 * special "end of directory" dentry and not a real dentry. If nonzero, this
1053 * was a real dentry.
1055 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1056 u64 offset, struct dentry *dentry)
1059 u64 calculated_size;
1060 char *file_name = NULL;
1061 char *file_name_utf8 = NULL;
1062 char *short_name = NULL;
1065 size_t file_name_utf8_len;
1068 dentry_common_init(dentry);
1070 /*Make sure the dentry really fits into the metadata resource.*/
1071 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1072 ERROR("Directory entry starting at %"PRIu64" ends past the "
1073 "end of the metadata resource (size %"PRIu64")",
1074 offset, metadata_resource_len);
1075 return WIMLIB_ERR_INVALID_DENTRY;
1078 /* Before reading the whole dentry, we need to read just the length.
1079 * This is because a dentry of length 8 (that is, just the length field)
1080 * terminates the list of sibling directory entries. */
1082 p = get_u64(&metadata_resource[offset], &dentry->length);
1084 /* A zero length field (really a length of 8, since that's how big the
1085 * directory entry is...) indicates that this is the end of directory
1086 * dentry. We do not read it into memory as an actual dentry, so just
1087 * return successfully in that case. */
1088 if (dentry->length == 0)
1091 /* If the dentry does not overflow the metadata resource buffer and is
1092 * not too short, read the rest of it (excluding the alternate data
1093 * streams, but including the file name and short name variable-length
1094 * fields) into memory. */
1095 if (offset + dentry->length >= metadata_resource_len
1096 || offset + dentry->length < offset)
1098 ERROR("Directory entry at offset %"PRIu64" and with size "
1099 "%"PRIu64" ends past the end of the metadata resource "
1101 offset, dentry->length, metadata_resource_len);
1102 return WIMLIB_ERR_INVALID_DENTRY;
1105 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1106 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1108 return WIMLIB_ERR_INVALID_DENTRY;
1111 p = get_u32(p, &dentry->attributes);
1112 p = get_u32(p, (u32*)&dentry->security_id);
1113 p = get_u64(p, &dentry->subdir_offset);
1115 /* 2 unused fields */
1116 p += 2 * sizeof(u64);
1117 /*p = get_u64(p, &dentry->unused1);*/
1118 /*p = get_u64(p, &dentry->unused2);*/
1120 p = get_u64(p, &dentry->creation_time);
1121 p = get_u64(p, &dentry->last_access_time);
1122 p = get_u64(p, &dentry->last_write_time);
1124 p = get_bytes(p, SHA1_HASH_SIZE, dentry->hash);
1127 * I don't know what's going on here. It seems like M$ screwed up the
1128 * reparse points, then put the fields in the same place and didn't
1129 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1130 * have something to do with this, but it's not documented.
1132 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1135 p = get_u32(p, &dentry->reparse_tag);
1138 p = get_u32(p, &dentry->reparse_tag);
1139 p = get_u64(p, &dentry->hard_link);
1142 /* By the way, the reparse_reserved field does not actually exist (at
1143 * least when the file is not a reparse point) */
1145 p = get_u16(p, &dentry->num_ads);
1147 p = get_u16(p, &short_name_len);
1148 p = get_u16(p, &file_name_len);
1150 /* We now know the length of the file name and short name. Make sure
1151 * the length of the dentry is large enough to actually hold them.
1153 * The calculated length here is unaligned to allow for the possibility
1154 * that the dentry->length names an unaligned length, although this
1155 * would be unexpected. */
1156 calculated_size = __dentry_correct_length_unaligned(file_name_len,
1159 if (dentry->length < calculated_size) {
1160 ERROR("Unexpected end of directory entry! (Expected "
1161 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1162 "short_name_len = %hu, file_name_len = %hu)",
1163 calculated_size, dentry->length,
1164 short_name_len, file_name_len);
1165 return WIMLIB_ERR_INVALID_DENTRY;
1168 /* Read the filename if present. Note: if the filename is empty, there
1169 * is no null terminator following it. */
1170 if (file_name_len) {
1171 file_name = MALLOC(file_name_len);
1173 ERROR("Failed to allocate %hu bytes for dentry file name",
1175 return WIMLIB_ERR_NOMEM;
1177 p = get_bytes(p, file_name_len, file_name);
1179 /* Convert filename to UTF-8. */
1180 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1181 &file_name_utf8_len);
1183 if (!file_name_utf8) {
1184 ERROR("Failed to allocate memory to convert UTF-16 "
1185 "filename (%hu bytes) to UTF-8", file_name_len);
1186 ret = WIMLIB_ERR_NOMEM;
1187 goto out_free_file_name;
1190 WARNING("Expected two zero bytes following the file name "
1191 "`%s', but found non-zero bytes", file_name_utf8);
1195 /* Align the calculated size */
1196 calculated_size = (calculated_size + 7) & ~7;
1198 if (dentry->length > calculated_size) {
1199 /* Weird; the dentry says it's longer than it should be. Note
1200 * that the length field does NOT include the size of the
1201 * alternate stream entries. */
1203 /* Strangely, some directory entries inexplicably have a little
1204 * over 70 bytes of extra data. The exact amount of data seems
1205 * to be 72 bytes, but it is aligned on the next 8-byte
1206 * boundary. It does NOT seem to be alternate data stream
1207 * entries. Here's an example of the aligned data:
1209 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1210 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1211 * 00000000 00000000 00000000 00000000
1213 * Here's one interpretation of how the data is laid out.
1216 * u32 field1; (always 0x00000001)
1217 * u32 field2; (always 0x40000000)
1218 * u8 data[48]; (???)
1219 * u64 reserved1; (always 0)
1220 * u64 reserved2; (always 0)
1222 WARNING("Dentry for file or directory `%s' has %zu extra "
1224 file_name_utf8, dentry->length - calculated_size);
1227 /* Read the short filename if present. Note: if there is no short
1228 * filename, there is no null terminator following it. */
1229 if (short_name_len) {
1230 short_name = MALLOC(short_name_len);
1232 ERROR("Failed to allocate %hu bytes for short filename",
1234 ret = WIMLIB_ERR_NOMEM;
1235 goto out_free_file_name_utf8;
1238 p = get_bytes(p, short_name_len, short_name);
1240 WARNING("Expected two zero bytes following the file name "
1241 "`%s', but found non-zero bytes", file_name_utf8);
1246 * Read the alternate data streams, if present. dentry->num_ads tells
1247 * us how many they are, and they will directly follow the dentry
1250 * Note that each alternate data stream entry begins on an 8-byte
1251 * aligned boundary, and the alternate data stream entries are NOT
1252 * included in the dentry->length field for some reason.
1254 if (dentry->num_ads != 0) {
1255 if (calculated_size > metadata_resource_len - offset) {
1256 ERROR("Not enough space in metadata resource for "
1257 "alternate stream entries");
1258 ret = WIMLIB_ERR_INVALID_DENTRY;
1259 goto out_free_short_name;
1261 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1263 metadata_resource_len - offset - calculated_size);
1265 goto out_free_short_name;
1268 /* We've read all the data for this dentry. Set the names and their
1269 * lengths, and we've done. */
1270 dentry->file_name = file_name;
1271 dentry->file_name_utf8 = file_name_utf8;
1272 dentry->short_name = short_name;
1273 dentry->file_name_len = file_name_len;
1274 dentry->file_name_utf8_len = file_name_utf8_len;
1275 dentry->short_name_len = short_name_len;
1277 out_free_short_name:
1279 out_free_file_name_utf8:
1280 FREE(file_name_utf8);
1286 /* Run some miscellaneous verifications on a WIM dentry */
1287 int verify_dentry(struct dentry *dentry, void *wim)
1289 const WIMStruct *w = wim;
1290 const struct lookup_table *table = w->lookup_table;
1291 const struct wim_security_data *sd = wim_const_security_data(w);
1292 int ret = WIMLIB_ERR_INVALID_DENTRY;
1294 /* Check the security ID */
1295 if (dentry->security_id < -1) {
1296 ERROR("Dentry `%s' has an invalid security ID (%d)",
1297 dentry->full_path_utf8, dentry->security_id);
1300 if (dentry->security_id >= sd->num_entries) {
1301 ERROR("Dentry `%s' has an invalid security ID (%d) "
1302 "(there are only %u entries in the security table)",
1303 dentry->full_path_utf8, dentry->security_id,
1308 /* Check that lookup table entries for all the resources exist, except
1309 * if the SHA1 message digest is all 0's, which indicates there is
1310 * intentionally no resource there. */
1311 if (w->hdr.total_parts == 1) {
1312 for (unsigned i = 0; i <= dentry->num_ads; i++) {
1313 struct lookup_table_entry *lte;
1315 hash = dentry_stream_hash_unresolved(dentry, i);
1316 lte = __lookup_resource(table, hash);
1317 if (!lte && !is_zero_hash(hash)) {
1318 ERROR("Could not find lookup table entry for stream "
1319 "%u of dentry `%s'", i, dentry->full_path_utf8);
1325 /* Make sure there is only one un-named stream. */
1326 unsigned num_unnamed_streams = 0;
1327 unsigned unnamed_stream_idx;
1328 for (unsigned i = 0; i <= dentry->num_ads; i++) {
1330 hash = dentry_stream_hash_unresolved(dentry, i);
1331 if (!dentry_stream_name_len(dentry, i) && !is_zero_hash(hash)) {
1332 num_unnamed_streams++;
1333 unnamed_stream_idx = i;
1336 if (num_unnamed_streams > 1) {
1337 ERROR("Dentry `%s' has multiple (%u) un-named streams",
1338 dentry->full_path_utf8, num_unnamed_streams);
1343 /* Check timestamps */
1344 if (dentry->last_access_time < dentry->creation_time ||
1345 dentry->last_write_time < dentry->creation_time) {
1346 WARNING("Dentry `%s' was created after it was last accessed or "
1347 "written to", dentry->full_path_utf8);
1357 * Writes a WIM dentry to an output buffer.
1359 * @dentry: The dentry structure.
1360 * @p: The memory location to write the data to.
1361 * @return: Pointer to the byte after the last byte we wrote as part of the
1364 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1370 /* We calculate the correct length of the dentry ourselves because the
1371 * dentry->length field may been set to an unexpected value from when we
1372 * read the dentry in (for example, there may have been unknown data
1373 * appended to the end of the dentry...) */
1374 u64 length = dentry_correct_length(dentry);
1376 p = put_u64(p, length);
1377 p = put_u32(p, dentry->attributes);
1378 p = put_u32(p, dentry->security_id);
1379 p = put_u64(p, dentry->subdir_offset);
1380 p = put_u64(p, 0); /* unused1 */
1381 p = put_u64(p, 0); /* unused2 */
1382 p = put_u64(p, dentry->creation_time);
1383 p = put_u64(p, dentry->last_access_time);
1384 p = put_u64(p, dentry->last_write_time);
1385 if (dentry->resolved && dentry->lte)
1386 hash = dentry->lte->hash;
1388 hash = dentry->hash;
1389 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1390 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1391 p = put_zeroes(p, 4);
1392 p = put_u32(p, dentry->reparse_tag);
1393 p = put_zeroes(p, 4);
1397 if (dentry->link_group_list.next == &dentry->link_group_list)
1400 hard_link = dentry->hard_link;
1401 p = put_u64(p, hard_link);
1403 p = put_u16(p, dentry->num_ads);
1404 p = put_u16(p, dentry->short_name_len);
1405 p = put_u16(p, dentry->file_name_len);
1406 if (dentry->file_name_len) {
1407 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1408 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1410 if (dentry->short_name) {
1411 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1412 p = put_u16(p, 0); /* short name padding, 2 bytes */
1415 /* Align to 8-byte boundary */
1416 wimlib_assert(length >= (p - orig_p)
1417 && length - (p - orig_p) <= 7);
1418 p = put_zeroes(p, length - (p - orig_p));
1420 /* Write the alternate data streams, if there are any. Please see
1421 * read_ads_entries() for comments about the format of the on-disk
1422 * alternate data stream entries. */
1423 for (u16 i = 0; i < dentry->num_ads; i++) {
1424 p = put_u64(p, ads_entry_total_length(&dentry->ads_entries[i]));
1425 p = put_u64(p, 0); /* Unused */
1426 if (dentry->resolved && dentry->ads_entries[i].lte)
1427 hash = dentry->ads_entries[i].lte->hash;
1429 hash = dentry->ads_entries[i].hash;
1430 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1431 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
1432 if (dentry->ads_entries[i].stream_name_len) {
1433 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
1434 (u8*)dentry->ads_entries[i].stream_name);
1437 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1439 #ifdef ENABLE_ASSERTIONS
1440 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1445 /* Recursive function that writes a dentry tree rooted at @parent, not including
1446 * @parent itself, which has already been written. */
1447 static u8 *write_dentry_tree_recursive(const struct dentry *parent, u8 *p)
1449 const struct dentry *child;
1451 /* Nothing to do if this dentry has no children. */
1452 if (parent->subdir_offset == 0)
1455 /* Write child dentries and end-of-directory entry.
1457 * Note: we need to write all of this dentry's children before
1458 * recursively writing the directory trees rooted at each of the child
1459 * dentries, since the on-disk dentries for a dentry's children are
1460 * always located at consecutive positions in the metadata resource! */
1461 child = parent->children;
1464 p = write_dentry(child, p);
1465 child = child->next;
1466 } while (child != parent->children);
1469 /* write end of directory entry */
1472 /* Recurse on children. */
1475 p = write_dentry_tree_recursive(child, p);
1476 child = child->next;
1477 } while (child != parent->children);
1482 /* Writes a directory tree to the metadata resource.
1484 * @root: Root of the dentry tree.
1485 * @p: Pointer to a buffer with enough space for the dentry tree.
1487 * Returns pointer to the byte after the last byte we wrote.
1489 u8 *write_dentry_tree(const struct dentry *root, u8 *p)
1491 wimlib_assert(dentry_is_root(root));
1493 /* If we're the root dentry, we have no parent that already
1494 * wrote us, so we need to write ourselves. */
1495 p = write_dentry(root, p);
1497 /* Write end of directory entry after the root dentry just to be safe;
1498 * however the root dentry obviously cannot have any siblings. */
1501 /* Recursively write the rest of the dentry tree. */
1502 return write_dentry_tree_recursive(root, p);
1505 /* Reads the children of a dentry, and all their children, ..., etc. from the
1506 * metadata resource and into the dentry tree.
1508 * @metadata_resource: An array that contains the uncompressed metadata
1509 * resource for the WIM file.
1511 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1514 * @dentry: A pointer to a `struct dentry' that is the root of the directory
1515 * tree and has already been read from the metadata resource. It
1516 * does not need to be the real root because this procedure is
1517 * called recursively.
1519 * @return: Zero on success, nonzero on failure.
1521 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1522 struct dentry *dentry)
1524 u64 cur_offset = dentry->subdir_offset;
1525 struct dentry *prev_child = NULL;
1526 struct dentry *first_child = NULL;
1527 struct dentry *child;
1528 struct dentry cur_child;
1532 * If @dentry has no child dentries, nothing more needs to be done for
1533 * this branch. This is the case for regular files, symbolic links, and
1534 * *possibly* empty directories (although an empty directory may also
1535 * have one child dentry that is the special end-of-directory dentry)
1537 if (cur_offset == 0)
1540 /* Find and read all the children of @dentry. */
1543 /* Read next child of @dentry into @cur_child. */
1544 ret = read_dentry(metadata_resource, metadata_resource_len,
1545 cur_offset, &cur_child);
1549 /* Check for end of directory. */
1550 if (cur_child.length == 0)
1553 /* Not end of directory. Allocate this child permanently and
1554 * link it to the parent and previous child. */
1555 child = MALLOC(sizeof(struct dentry));
1557 ERROR("Failed to allocate %zu bytes for new dentry",
1558 sizeof(struct dentry));
1559 ret = WIMLIB_ERR_NOMEM;
1562 memcpy(child, &cur_child, sizeof(struct dentry));
1565 prev_child->next = child;
1566 child->prev = prev_child;
1568 first_child = child;
1571 child->parent = dentry;
1574 /* If there are children of this child, call this procedure
1576 if (child->subdir_offset != 0) {
1577 ret = read_dentry_tree(metadata_resource,
1578 metadata_resource_len, child);
1583 /* Advance to the offset of the next child. Note: We need to
1584 * advance by the TOTAL length of the dentry, not by the length
1585 * child->length, which although it does take into account the
1586 * padding, it DOES NOT take into account alternate stream
1588 cur_offset += dentry_total_length(child);
1591 /* Link last child to first one, and set parent's children pointer to
1592 * the first child. */
1594 prev_child->next = first_child;
1595 first_child->prev = prev_child;
1597 dentry->children = first_child;