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/.
33 #include "wimlib_internal.h"
36 #include "timestamp.h"
37 #include "lookup_table.h"
43 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
44 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
46 if (S_ISDIR(stbuf->st_mode))
47 dentry->attributes = WIM_FILE_ATTRIBUTE_DIRECTORY;
49 dentry->attributes = WIM_FILE_ATTRIBUTE_NORMAL;
52 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
53 void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
54 const struct lookup_table *table)
56 struct lookup_table_entry *lte;
58 if (dentry_is_directory(dentry))
59 stbuf->st_mode = S_IFDIR | 0755;
61 stbuf->st_mode = S_IFREG | 0644;
64 lte = lookup_resource(table, dentry->hash);
69 stbuf->st_nlink = lte->refcnt;
70 stbuf->st_size = lte->resource_entry.original_size;
75 stbuf->st_uid = getuid();
76 stbuf->st_gid = getgid();
77 stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
78 stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
79 stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
80 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
83 /* Makes all timestamp fields for the dentry be the current time. */
84 void dentry_update_all_timestamps(struct dentry *dentry)
86 u64 now = get_timestamp();
87 dentry->creation_time = now;
88 dentry->last_access_time = now;
89 dentry->last_write_time = now;
93 * Calls a function on all directory entries in a directory tree. It is called
94 * on a parent before its children.
96 int for_dentry_in_tree(struct dentry *root,
97 int (*visitor)(struct dentry*, void*), void *arg)
100 struct dentry *child;
102 ret = visitor(root, arg);
107 child = root->children;
113 ret = for_dentry_in_tree(child, visitor, arg);
117 } while (child != root->children);
122 * Like for_dentry_in_tree(), but the visitor function is always called on a
123 * dentry's children before on itself.
125 int for_dentry_in_tree_depth(struct dentry *root,
126 int (*visitor)(struct dentry*, void*), void *arg)
129 struct dentry *child;
132 child = root->children;
136 ret = for_dentry_in_tree_depth(child, visitor, arg);
140 } while (child != root->children);
142 return visitor(root, arg);
146 * Calculate the full path of @dentry, based on its parent's full path and on
147 * its UTF-8 file name.
149 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
153 if (dentry_is_root(dentry)) {
154 full_path = MALLOC(2);
161 char *parent_full_path;
162 u32 parent_full_path_len;
163 const struct dentry *parent = dentry->parent;
165 if (dentry_is_root(parent)) {
166 parent_full_path = "";
167 parent_full_path_len = 0;
169 parent_full_path = parent->full_path_utf8;
170 parent_full_path_len = parent->full_path_utf8_len;
173 full_path_len = parent_full_path_len + 1 +
174 dentry->file_name_utf8_len;
175 full_path = MALLOC(full_path_len + 1);
179 memcpy(full_path, parent_full_path, parent_full_path_len);
180 full_path[parent_full_path_len] = '/';
181 memcpy(full_path + parent_full_path_len + 1,
182 dentry->file_name_utf8,
183 dentry->file_name_utf8_len);
184 full_path[full_path_len] = '\0';
186 FREE(dentry->full_path_utf8);
187 dentry->full_path_utf8 = full_path;
188 dentry->full_path_utf8_len = full_path_len;
191 ERROR("Out of memory while calculating dentry full path");
192 return WIMLIB_ERR_NOMEM;
196 * Recursively calculates the subdir offsets for a directory tree.
198 * @dentry: The root of the directory tree.
199 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
200 * offset for @dentry.
202 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
204 struct dentry *child;
206 child = dentry->children;
207 dentry->subdir_offset = *subdir_offset_p;
210 /* Advance the subdir offset by the amount of space the children
211 * of this dentry take up. */
213 *subdir_offset_p += child->length;
215 } while (child != dentry->children);
217 /* End-of-directory dentry on disk. */
218 *subdir_offset_p += 8;
220 /* Recursively call calculate_subdir_offsets() on all the
223 calculate_subdir_offsets(child, subdir_offset_p);
225 } while (child != dentry->children);
227 /* On disk, childless directories have a valid subdir_offset
228 * that points to an 8-byte end-of-directory dentry. Regular
229 * files have a subdir_offset of 0. */
230 if (dentry_is_directory(dentry))
231 *subdir_offset_p += 8;
233 dentry->subdir_offset = 0;
238 /* Returns the child of @dentry that has the file name @name.
239 * Returns NULL if no child has the name. */
240 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
243 struct dentry *child;
246 child = dentry->children;
248 name_len = strlen(name);
250 if (dentry_has_name(child, name, name_len))
253 } while (child != dentry->children);
258 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
259 * @cur_dir. Returns NULL if no dentry having the path is found. */
260 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
262 struct dentry *child;
264 const char *new_path;
269 child = cur_dir->children;
271 new_path = path_next_part(path, &base_len);
273 if (dentry_has_name(child, path, base_len))
274 return get_dentry_relative_path(child, new_path);
276 } while (child != cur_dir->children);
281 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
283 struct dentry *get_dentry(WIMStruct *w, const char *path)
285 struct dentry *root = wim_root_dentry(w);
288 return get_dentry_relative_path(root, path);
291 /* Returns the parent directory for the @path. */
292 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
294 size_t path_len = strlen(path);
295 char buf[path_len + 1];
297 memcpy(buf, path, path_len + 1);
299 to_parent_name(buf, path_len);
301 return get_dentry(w, buf);
304 /* Prints the full path of a dentry. */
305 int print_dentry_full_path(struct dentry *dentry, void *ignore)
307 if (dentry->full_path_utf8)
308 puts(dentry->full_path_utf8);
312 struct file_attr_flag {
316 struct file_attr_flag file_attr_flags[] = {
317 {WIM_FILE_ATTRIBUTE_READONLY, "READONLY"},
318 {WIM_FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
319 {WIM_FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
320 {WIM_FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
321 {WIM_FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
322 {WIM_FILE_ATTRIBUTE_DEVICE, "DEVICE"},
323 {WIM_FILE_ATTRIBUTE_NORMAL, "NORMAL"},
324 {WIM_FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
325 {WIM_FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
326 {WIM_FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
327 {WIM_FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
328 {WIM_FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
329 {WIM_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
330 {WIM_FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
331 {WIM_FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
334 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
335 * NULL if the resource entry for the dentry is not to be printed. */
336 int print_dentry(struct dentry *dentry, void *lookup_table)
338 struct lookup_table_entry *lte;
341 printf("[DENTRY]\n");
342 printf("Length = %"PRIu64"\n", dentry->length);
343 printf("Attributes = 0x%x\n", dentry->attributes);
344 for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
345 if (file_attr_flags[i].flag & dentry->attributes)
346 printf(" WIM_FILE_ATTRIBUTE_%s is set\n",
347 file_attr_flags[i].name);
348 #ifdef ENABLE_SECURITY_DATA
349 printf("Security ID = %d\n", dentry->security_id);
351 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
352 /*printf("Unused1 = %"PRIu64"\n", dentry->unused1);*/
353 /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
354 printf("Creation Time = %"PRIu64"\n", dentry->creation_time);
355 printf("Last Access Time = %"PRIu64"\n", dentry->last_access_time);
356 printf("Last Write Time = %"PRIu64"\n", dentry->last_write_time);
357 printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
359 print_hash(dentry->hash);
361 /*printf("Reparse Tag = %u\n", dentry->reparse_tag);*/
362 printf("Hard Link Group = %"PRIu64"\n", dentry->hard_link);
363 /*printf("Number of Streams = %hu\n", dentry->streams);*/
364 printf("Filename = \"");
365 print_string(dentry->file_name, dentry->file_name_len);
367 printf("Filename Length = %hu\n", dentry->file_name_len);
368 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
369 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
370 printf("Short Name = \"");
371 print_string(dentry->short_name, dentry->short_name_len);
373 printf("Short Name Length = %hu\n", dentry->short_name_len);
374 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
376 lte = lookup_resource(lookup_table, dentry->hash);
378 print_lookup_table_entry(lte, NULL);
387 static inline void dentry_common_init(struct dentry *dentry)
389 memset(dentry, 0, sizeof(struct dentry));
394 * Creates an unlinked directory entry.
396 * @name: The base name of the new dentry.
397 * @return: A pointer to the new dentry, or NULL if out of memory.
399 struct dentry *new_dentry(const char *name)
401 struct dentry *dentry;
403 dentry = MALLOC(sizeof(struct dentry));
407 dentry_common_init(dentry);
408 if (change_dentry_name(dentry, name) != 0) {
413 dentry_update_all_timestamps(dentry);
414 dentry->next = dentry;
415 dentry->prev = dentry;
416 dentry->parent = dentry;
421 void free_dentry(struct dentry *dentry)
423 FREE(dentry->file_name);
424 FREE(dentry->file_name_utf8);
425 FREE(dentry->short_name);
426 FREE(dentry->full_path_utf8);
430 /* Arguments for do_free_dentry(). */
431 struct free_dentry_args {
432 struct lookup_table *lookup_table;
433 bool lt_decrement_refcnt;
437 * This function is passed as an argument to for_dentry_in_tree_depth() in order
438 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
440 static int do_free_dentry(struct dentry *dentry, void *__args)
442 struct free_dentry_args *args = (struct free_dentry_args*)__args;
444 if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
445 lookup_table_decrement_refcnt(args->lookup_table,
449 wimlib_assert(dentry->refcnt >= 1);
450 if (--dentry->refcnt == 0)
456 * Unlinks and frees a dentry tree.
458 * @root: The root of the tree.
459 * @lookup_table: The lookup table for dentries.
460 * @decrement_refcnt: True if the dentries in the tree are to have their
461 * reference counts in the lookup table decremented.
463 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table,
464 bool lt_decrement_refcnt)
466 if (!root || !root->parent)
469 struct free_dentry_args args;
470 args.lookup_table = lookup_table;
471 args.lt_decrement_refcnt = lt_decrement_refcnt;
472 for_dentry_in_tree_depth(root, do_free_dentry, &args);
475 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
482 * Links a dentry into the directory tree.
484 * @dentry: The dentry to link.
485 * @parent: The dentry that will be the parent of @dentry.
487 void link_dentry(struct dentry *dentry, struct dentry *parent)
489 dentry->parent = parent;
490 if (parent->children) {
491 /* Not an only child; link to siblings. */
492 dentry->next = parent->children;
493 dentry->prev = parent->children->prev;
494 dentry->next->prev = dentry;
495 dentry->prev->next = dentry;
497 /* Only child; link to parent. */
498 parent->children = dentry;
499 dentry->next = dentry;
500 dentry->prev = dentry;
504 /* Unlink a dentry from the directory tree. */
505 void unlink_dentry(struct dentry *dentry)
507 if (dentry_is_root(dentry))
509 if (dentry_is_only_child(dentry)) {
510 dentry->parent->children = NULL;
512 if (dentry_is_first_sibling(dentry))
513 dentry->parent->children = dentry->next;
514 dentry->next->prev = dentry->prev;
515 dentry->prev->next = dentry->next;
520 /* Recalculates the length of @dentry based on its file name length and short
522 static inline void recalculate_dentry_size(struct dentry *dentry)
524 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
525 2 + dentry->short_name_len;
526 /* Must be multiple of 8. */
527 dentry->length += (8 - dentry->length % 8) % 8;
530 /* Changes the name of a dentry to @new_name. Only changes the file_name and
531 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
532 * full_path_utf8 fields. Also recalculates its length. */
533 int change_dentry_name(struct dentry *dentry, const char *new_name)
538 FREE(dentry->file_name);
540 utf8_len = strlen(new_name);
542 dentry->file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
544 if (!dentry->file_name)
545 return WIMLIB_ERR_NOMEM;
547 FREE(dentry->file_name_utf8);
548 dentry->file_name_utf8 = MALLOC(utf8_len + 1);
549 if (!dentry->file_name_utf8) {
550 FREE(dentry->file_name);
551 dentry->file_name = NULL;
552 return WIMLIB_ERR_NOMEM;
555 dentry->file_name_len = utf16_len;
556 dentry->file_name_utf8_len = utf8_len;
557 memcpy(dentry->file_name_utf8, new_name, utf8_len + 1);
558 recalculate_dentry_size(dentry);
562 /* Parameters for calculate_dentry_statistics(). */
563 struct image_statistics {
564 struct lookup_table *lookup_table;
568 u64 *hard_link_bytes;
571 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
573 struct image_statistics *stats;
574 struct lookup_table_entry *lte;
577 lte = lookup_resource(stats->lookup_table, dentry->hash);
579 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
582 ++*stats->file_count;
585 u64 size = lte->resource_entry.original_size;
586 *stats->total_bytes += size;
587 if (++lte->out_refcnt == 1)
588 *stats->hard_link_bytes += size;
593 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
594 u64 *dir_count_ret, u64 *file_count_ret,
595 u64 *total_bytes_ret,
596 u64 *hard_link_bytes_ret)
598 struct image_statistics stats;
601 *total_bytes_ret = 0;
602 *hard_link_bytes_ret = 0;
603 stats.lookup_table = table;
604 stats.dir_count = dir_count_ret;
605 stats.file_count = file_count_ret;
606 stats.total_bytes = total_bytes_ret;
607 stats.hard_link_bytes = hard_link_bytes_ret;
608 for_lookup_table_entry(table, zero_out_refcnts, NULL);
609 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
613 * Reads a directory entry from the metadata resource.
615 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
616 u64 offset, struct dentry *dentry)
621 char *file_name_utf8;
625 size_t file_name_utf8_len;
627 dentry_common_init(dentry);
629 /*Make sure the dentry really fits into the metadata resource.*/
630 if (offset + 8 > metadata_resource_len) {
631 ERROR("Directory entry starting at %"PRIu64" ends past the "
632 "end of the metadata resource (size %"PRIu64")",
633 offset, metadata_resource_len);
634 return WIMLIB_ERR_INVALID_DENTRY;
637 /* Before reading the whole entry, we need to read just the length.
638 * This is because an entry of length 8 (that is, just the length field)
639 * terminates the list of sibling directory entries. */
641 p = get_u64(&metadata_resource[offset], &dentry->length);
643 /* A zero length field (really a length of 8, since that's how big the
644 * directory entry is...) indicates that this is the end of directory
645 * dentry. We do not read it into memory as an actual dentry, so just
646 * return true in that case. */
647 if (dentry->length == 0)
650 if (offset + dentry->length >= metadata_resource_len) {
651 ERROR("Directory entry at offset %"PRIu64" and with size "
652 "%"PRIu64" ends past the end of the metadata resource "
654 offset, dentry->length, metadata_resource_len);
655 return WIMLIB_ERR_INVALID_DENTRY;
658 /* If it is a recognized length, read the rest of the directory entry.
659 * Note: The root directory entry has no name, and its length does not
660 * include the short name length field. */
661 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
662 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
664 return WIMLIB_ERR_INVALID_DENTRY;
667 p = get_u32(p, &dentry->attributes);
668 #ifdef ENABLE_SECURITY_DATA
669 p = get_u32(p, (u32*)&dentry->security_id);
673 p = get_u64(p, &dentry->subdir_offset);
675 /* 2 unused fields */
676 p += 2 * sizeof(u64);
678 p = get_u64(p, &dentry->creation_time);
679 p = get_u64(p, &dentry->last_access_time);
680 p = get_u64(p, &dentry->last_write_time);
682 p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
684 /* Currently ignoring reparse_tag. */
687 /* The reparse_reserved field does not actually exist. */
689 p = get_u64(p, &dentry->hard_link);
691 /* Currently ignoring streams. */
694 p = get_u16(p, &short_name_len);
695 p = get_u16(p, &file_name_len);
697 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
700 if (dentry->length < calculated_size) {
701 ERROR("Unexpected end of directory entry! (Expected "
702 "%"PRIu64" bytes, got %"PRIu64" bytes. "
703 "short_name_len = %hu, file_name_len = %hu)",
704 calculated_size, dentry->length,
705 short_name_len, file_name_len);
706 return WIMLIB_ERR_INVALID_DENTRY;
709 /* Read the filename. */
710 file_name = MALLOC(file_name_len);
712 ERROR("Failed to allocate %hu bytes for dentry file name",
714 return WIMLIB_ERR_NOMEM;
716 p = get_bytes(p, file_name_len, file_name);
718 /* Convert filename to UTF-8. */
719 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
720 &file_name_utf8_len);
722 if (!file_name_utf8) {
723 ERROR("Failed to allocate memory to convert UTF-16 "
724 "filename (%hu bytes) to UTF-8", file_name_len);
725 goto out_free_file_name;
728 /* Undocumented padding between file name and short name. This probably
729 * is supposed to be a terminating null character. */
732 /* Read the short filename. */
733 short_name = MALLOC(short_name_len);
735 ERROR("Failed to allocate %hu bytes for short filename",
737 goto out_free_file_name_utf8;
740 get_bytes(p, short_name_len, short_name);
742 dentry->file_name = file_name;
743 dentry->file_name_utf8 = file_name_utf8;
744 dentry->short_name = short_name;
745 dentry->file_name_len = file_name_len;
746 dentry->file_name_utf8_len = file_name_utf8_len;
747 dentry->short_name_len = short_name_len;
749 out_free_file_name_utf8:
750 FREE(dentry->file_name_utf8);
752 FREE(dentry->file_name);
753 return WIMLIB_ERR_NOMEM;
757 * Writes a dentry to an output buffer.
759 * @dentry: The dentry structure.
760 * @p: The memory location to write the data to.
761 * @return: True on success, false on failure.
763 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
766 memset(p, 0, dentry->length);
767 p = put_u64(p, dentry->length);
768 p = put_u32(p, dentry->attributes);
769 #ifdef ENABLE_SECURITY_DATA
770 p = put_u32(p, dentry->security_id);
772 p = put_u32(p, (u32)(-1));
774 p = put_u64(p, dentry->subdir_offset);
775 p = put_u64(p, 0); /* unused1 */
776 p = put_u64(p, 0); /* unused2 */
777 p = put_u64(p, dentry->creation_time);
778 p = put_u64(p, dentry->last_access_time);
779 p = put_u64(p, dentry->last_write_time);
780 if (!is_empty_file_hash(dentry->hash))
781 memcpy(p, dentry->hash, WIM_HASH_SIZE);
783 DEBUG("zero hash for %s\n", dentry->file_name_utf8);
785 p = put_u32(p, 0); /* reparse_tag */
786 p = put_u64(p, dentry->hard_link);
787 p = put_u16(p, 0); /*streams */
788 p = put_u16(p, dentry->short_name_len);
789 p = put_u16(p, dentry->file_name_len);
790 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
791 p = put_u16(p, 0); /* filename padding, 2 bytes. */
792 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
793 return orig_p + dentry->length;
796 /* Recursive function that writes a dentry tree rooted at @tree, not including
797 * @tree itself, which has already been written, except in the case of the root
798 * dentry, which is written right away, along with an end-of-directory entry. */
799 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
801 const struct dentry *child;
803 if (dentry_is_root(tree)) {
804 p = write_dentry(tree, p);
806 /* write end of directory entry */
809 /* Nothing to do for a regular file. */
810 if (dentry_is_regular_file(tree))
814 /* Write child dentries and end-of-directory entry. */
815 child = tree->children;
818 p = write_dentry(child, p);
820 } while (child != tree->children);
823 /* write end of directory entry */
826 /* Recurse on children. */
829 p = write_dentry_tree(child, p);
831 } while (child != tree->children);
836 /* Reads the children of a dentry, and all their children, ..., etc. from the
837 * metadata resource and into the dentry tree.
839 * @metadata_resource: An array that contains the uncompressed metadata
840 * resource for the WIM file.
841 * @metadata_resource_len: The length of @metadata_resource.
842 * @dentry: A pointer to a struct dentry that is the root of the directory
843 * tree and has already been read from the metadata resource. It
844 * does not need to be the real root because this procedure is
845 * called recursively.
847 * @return: Zero on success, nonzero on failure.
849 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
850 struct dentry *dentry)
852 u64 cur_offset = dentry->subdir_offset;
853 struct dentry *prev_child = NULL;
854 struct dentry *first_child = NULL;
855 struct dentry *child;
856 struct dentry cur_child;
859 /* If @dentry is a regular file, nothing more needs to be done for this
864 /* Find and read all the children of @dentry. */
867 /* Read next child of @dentry into @cur_child. */
868 ret = read_dentry(metadata_resource, metadata_resource_len,
869 cur_offset, &cur_child);
873 /* Check for end of directory. */
874 if (cur_child.length == 0) {
879 /* Not end of directory. Allocate this child permanently and
880 * link it to the parent and previous child. */
881 child = MALLOC(sizeof(struct dentry));
883 ERROR("Failed to allocate %zu bytes for new dentry",
884 sizeof(struct dentry));
885 ret = WIMLIB_ERR_NOMEM;
888 memcpy(child, &cur_child, sizeof(struct dentry));
891 prev_child->next = child;
892 child->prev = prev_child;
897 child->parent = dentry;
900 /* If there are children of this child, call this procedure
902 if (child->subdir_offset != 0) {
903 ret = read_dentry_tree(metadata_resource,
904 metadata_resource_len, child);
909 /* Advance to the offset of the next child. */
910 cur_offset += child->length;
913 /* Link last child to first one, and set parent's
914 * children pointer to the first child. */
916 prev_child->next = first_child;
917 first_child->prev = prev_child;
919 dentry->children = first_child;