4 * A dentry (directory entry) contains the metadata for a file. In the WIM file
5 * format, the dentries are stored in the "metadata resource" section right
6 * after the security data. Each image in the WIM file has its own metadata
7 * resource with its own security data and dentry tree. Dentries in different
8 * images may share file resources by referring to the same lookup table
13 * Copyright (C) 2012 Eric Biggers
15 * This file is part of wimlib, a library for working with WIM files.
17 * wimlib is free software; you can redistribute it and/or modify it under the
18 * terms of the GNU General Public License as published by the Free Software
19 * Foundation; either version 3 of the License, or (at your option) any later
22 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
23 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
24 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along with
27 * wimlib; if not, see http://www.gnu.org/licenses/.
37 #include "lookup_table.h"
39 #include "timestamp.h"
40 #include "wimlib_internal.h"
43 * Returns true if @dentry has the UTF-8 file name @name that has length
46 static bool dentry_has_name(const struct dentry *dentry, const char *name,
49 if (dentry->file_name_utf8_len != name_len)
51 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
54 static u64 __dentry_correct_length_unaligned(u16 file_name_len,
57 u64 length = WIM_DENTRY_DISK_SIZE;
59 length += file_name_len + 2;
61 length += short_name_len + 2;
65 static u64 dentry_correct_length_unaligned(const struct dentry *dentry)
67 return __dentry_correct_length_unaligned(dentry->file_name_len,
68 dentry->short_name_len);
71 /* Return the "correct" value to write in the length field of the dentry, based
72 * on the file name length and short name length */
73 static u64 dentry_correct_length(const struct dentry *dentry)
75 return (dentry_correct_length_unaligned(dentry) + 7) & ~7;
78 static u64 __dentry_total_length(const struct dentry *dentry, u64 length)
80 const struct inode *inode = dentry->inode;
81 for (u16 i = 0; i < inode->num_ads; i++)
82 length += ads_entry_total_length(inode->ads_entries[i]);
83 return (length + 7) & ~7;
86 u64 dentry_correct_total_length(const struct dentry *dentry)
88 return __dentry_total_length(dentry,
89 dentry_correct_length_unaligned(dentry));
92 /* Real length of a dentry, including the alternate data stream entries, which
93 * are not included in the dentry->length field... */
94 u64 dentry_total_length(const struct dentry *dentry)
96 return __dentry_total_length(dentry, dentry->length);
99 /* Transfers file attributes from a `stat' buffer to an inode. */
100 void stbuf_to_inode(const struct stat *stbuf, struct inode *inode)
102 if (S_ISLNK(stbuf->st_mode)) {
103 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
104 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
105 } else if (S_ISDIR(stbuf->st_mode)) {
106 inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
108 inode->attributes = FILE_ATTRIBUTE_NORMAL;
110 if (sizeof(ino_t) >= 8)
111 inode->ino = (u64)stbuf->st_ino;
113 inode->ino = (u64)stbuf->st_ino |
114 ((u64)stbuf->st_dev << (sizeof(ino_t) * 8));
116 inode->creation_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
117 inode->last_write_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
118 inode->last_access_time = timespec_to_wim_timestamp(&stbuf->st_atim);
122 /* Sets all the timestamp fields of the dentry to the current time. */
123 void inode_update_all_timestamps(struct inode *inode)
125 u64 now = get_wim_timestamp();
126 inode->creation_time = now;
127 inode->last_access_time = now;
128 inode->last_write_time = now;
131 /* Returns the alternate data stream entry belonging to @inode that has the
132 * stream name @stream_name. */
133 struct ads_entry *inode_get_ads_entry(struct inode *inode,
134 const char *stream_name,
137 size_t stream_name_len;
140 if (inode->num_ads) {
142 stream_name_len = strlen(stream_name);
144 if (ads_entry_has_name(inode->ads_entries[i],
145 stream_name, stream_name_len))
149 return inode->ads_entries[i];
151 } while (++i != inode->num_ads);
157 static struct ads_entry *new_ads_entry(const char *name)
159 struct ads_entry *ads_entry = CALLOC(1, sizeof(struct ads_entry));
163 if (change_ads_name(ads_entry, name)) {
172 * Add an alternate stream entry to an inode and return a pointer to it, or NULL
173 * if memory could not be allocated.
175 struct ads_entry *inode_add_ads(struct inode *inode, const char *stream_name)
178 struct ads_entry **ads_entries;
179 struct ads_entry *new_entry;
181 if (inode->num_ads >= 0xfffe) {
182 ERROR("Too many alternate data streams in one inode!");
185 num_ads = inode->num_ads + 1;
186 ads_entries = REALLOC(inode->ads_entries,
187 num_ads * sizeof(inode->ads_entries[0]));
189 ERROR("Failed to allocate memory for new alternate data stream");
192 inode->ads_entries = ads_entries;
194 new_entry = new_ads_entry(stream_name);
197 inode->num_ads = num_ads;
198 ads_entries[num_ads - 1] = new_entry;
200 new_entry->stream_id = inode->next_stream_id++;
207 * Calls a function on all directory entries in a directory tree. It is called
208 * on a parent before its children.
210 int for_dentry_in_tree(struct dentry *root,
211 int (*visitor)(struct dentry*, void*), void *arg)
214 struct dentry *child;
216 ret = visitor(root, arg);
221 child = root->inode->children;
227 ret = for_dentry_in_tree(child, visitor, arg);
231 } while (child != root->inode->children);
236 * Like for_dentry_in_tree(), but the visitor function is always called on a
237 * dentry's children before on itself.
239 int for_dentry_in_tree_depth(struct dentry *root,
240 int (*visitor)(struct dentry*, void*), void *arg)
243 struct dentry *child;
246 child = root->inode->children;
250 ret = for_dentry_in_tree_depth(child, visitor, arg);
254 } while (child != root->inode->children);
256 return visitor(root, arg);
260 * Calculate the full path of @dentry, based on its parent's full path and on
261 * its UTF-8 file name.
263 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
267 if (dentry_is_root(dentry)) {
268 full_path = MALLOC(2);
275 char *parent_full_path;
276 u32 parent_full_path_len;
277 const struct dentry *parent = dentry->parent;
279 if (dentry_is_root(parent)) {
280 parent_full_path = "";
281 parent_full_path_len = 0;
283 parent_full_path = parent->full_path_utf8;
284 parent_full_path_len = parent->full_path_utf8_len;
287 full_path_len = parent_full_path_len + 1 +
288 dentry->file_name_utf8_len;
289 full_path = MALLOC(full_path_len + 1);
293 memcpy(full_path, parent_full_path, parent_full_path_len);
294 full_path[parent_full_path_len] = '/';
295 memcpy(full_path + parent_full_path_len + 1,
296 dentry->file_name_utf8,
297 dentry->file_name_utf8_len);
298 full_path[full_path_len] = '\0';
300 FREE(dentry->full_path_utf8);
301 dentry->full_path_utf8 = full_path;
302 dentry->full_path_utf8_len = full_path_len;
305 ERROR("Out of memory while calculating dentry full path");
306 return WIMLIB_ERR_NOMEM;
310 * Recursively calculates the subdir offsets for a directory tree.
312 * @dentry: The root of the directory tree.
313 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
314 * offset for @dentry.
316 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
318 struct dentry *child;
320 child = dentry->inode->children;
321 dentry->subdir_offset = *subdir_offset_p;
324 /* Advance the subdir offset by the amount of space the children
325 * of this dentry take up. */
327 *subdir_offset_p += dentry_correct_total_length(child);
329 } while (child != dentry->inode->children);
331 /* End-of-directory dentry on disk. */
332 *subdir_offset_p += 8;
334 /* Recursively call calculate_subdir_offsets() on all the
337 calculate_subdir_offsets(child, subdir_offset_p);
339 } while (child != dentry->inode->children);
341 /* On disk, childless directories have a valid subdir_offset
342 * that points to an 8-byte end-of-directory dentry. Regular
343 * files or reparse points have a subdir_offset of 0. */
344 if (dentry_is_directory(dentry))
345 *subdir_offset_p += 8;
347 dentry->subdir_offset = 0;
352 /* Returns the child of @dentry that has the file name @name.
353 * Returns NULL if no child has the name. */
354 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
357 struct dentry *child;
360 child = dentry->inode->children;
362 name_len = strlen(name);
364 if (dentry_has_name(child, name, name_len))
367 } while (child != dentry->inode->children);
372 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
373 * @cur_dir. Returns NULL if no dentry having the path is found. */
374 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir,
377 struct dentry *child;
379 const char *new_path;
384 child = cur_dir->inode->children;
386 new_path = path_next_part(path, &base_len);
388 if (dentry_has_name(child, path, base_len))
389 return get_dentry_relative_path(child, new_path);
391 } while (child != cur_dir->inode->children);
396 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
398 struct dentry *get_dentry(WIMStruct *w, const char *path)
400 struct dentry *root = wim_root_dentry(w);
403 return get_dentry_relative_path(root, path);
406 struct inode *wim_pathname_to_inode(WIMStruct *w, const char *path)
408 struct dentry *dentry;
409 dentry = get_dentry(w, path);
413 return dentry->inode;
416 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
417 * if the dentry is not found. */
418 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
420 size_t path_len = strlen(path);
421 char buf[path_len + 1];
423 memcpy(buf, path, path_len + 1);
425 to_parent_name(buf, path_len);
427 return get_dentry(w, buf);
430 /* Prints the full path of a dentry. */
431 int print_dentry_full_path(struct dentry *dentry, void *ignore)
433 if (dentry->full_path_utf8)
434 puts(dentry->full_path_utf8);
438 /* We want to be able to show the names of the file attribute flags that are
440 struct file_attr_flag {
444 struct file_attr_flag file_attr_flags[] = {
445 {FILE_ATTRIBUTE_READONLY, "READONLY"},
446 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
447 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
448 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
449 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
450 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
451 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
452 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
453 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
454 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
455 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
456 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
457 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
458 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
459 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
462 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
463 * available. If the dentry is unresolved and the lookup table is NULL, the
464 * lookup table entries will not be printed. Otherwise, they will be. */
465 int print_dentry(struct dentry *dentry, void *lookup_table)
468 struct lookup_table_entry *lte;
469 const struct inode *inode = dentry->inode;
473 printf("[DENTRY]\n");
474 printf("Length = %"PRIu64"\n", dentry->length);
475 printf("Attributes = 0x%x\n", inode->attributes);
476 for (unsigned i = 0; i < ARRAY_LEN(file_attr_flags); i++)
477 if (file_attr_flags[i].flag & inode->attributes)
478 printf(" FILE_ATTRIBUTE_%s is set\n",
479 file_attr_flags[i].name);
480 printf("Security ID = %d\n", inode->security_id);
481 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
483 /* Translate the timestamps into something readable */
484 time = wim_timestamp_to_unix(inode->creation_time);
485 p = asctime(gmtime(&time));
486 *(strrchr(p, '\n')) = '\0';
487 printf("Creation Time = %s UTC\n", p);
489 time = wim_timestamp_to_unix(inode->last_access_time);
490 p = asctime(gmtime(&time));
491 *(strrchr(p, '\n')) = '\0';
492 printf("Last Access Time = %s UTC\n", p);
494 time = wim_timestamp_to_unix(inode->last_write_time);
495 p = asctime(gmtime(&time));
496 *(strrchr(p, '\n')) = '\0';
497 printf("Last Write Time = %s UTC\n", p);
499 printf("Reparse Tag = 0x%"PRIx32"\n", inode->reparse_tag);
500 printf("Hard Link Group = 0x%"PRIx64"\n", inode->ino);
501 printf("Hard Link Group Size = %"PRIu32"\n", inode->link_count);
502 printf("Number of Alternate Data Streams = %hu\n", inode->num_ads);
503 printf("Filename = \"");
504 print_string(dentry->file_name, dentry->file_name_len);
506 printf("Filename Length = %hu\n", dentry->file_name_len);
507 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
508 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
509 printf("Short Name = \"");
510 print_string(dentry->short_name, dentry->short_name_len);
512 printf("Short Name Length = %hu\n", dentry->short_name_len);
513 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
514 lte = inode_stream_lte(dentry->inode, 0, lookup_table);
516 print_lookup_table_entry(lte);
518 hash = inode_stream_hash(inode, 0);
526 for (u16 i = 0; i < inode->num_ads; i++) {
527 printf("[Alternate Stream Entry %u]\n", i);
528 printf("Name = \"%s\"\n", inode->ads_entries[i]->stream_name_utf8);
529 printf("Name Length (UTF-16) = %u\n",
530 inode->ads_entries[i]->stream_name_len);
531 hash = inode_stream_hash(inode, i + 1);
537 print_lookup_table_entry(inode_stream_lte(inode, i + 1,
543 /* Initializations done on every `struct dentry'. */
544 static void dentry_common_init(struct dentry *dentry)
546 memset(dentry, 0, sizeof(struct dentry));
550 struct inode *new_timeless_inode()
552 struct inode *inode = CALLOC(1, sizeof(struct inode));
555 inode->security_id = -1;
556 inode->link_count = 1;
557 INIT_LIST_HEAD(&inode->dentry_list);
561 struct inode *new_inode()
563 struct inode *inode = new_timeless_inode();
566 u64 now = get_wim_timestamp();
567 inode->creation_time = now;
568 inode->last_access_time = now;
569 inode->last_write_time = now;
574 * Creates an unlinked directory entry.
576 * @name: The UTF-8 filename of the new dentry.
578 * Returns a pointer to the new dentry, or NULL if out of memory.
580 struct dentry *new_dentry(const char *name)
582 struct dentry *dentry;
584 dentry = MALLOC(sizeof(struct dentry));
588 dentry_common_init(dentry);
589 if (change_dentry_name(dentry, name) != 0)
592 dentry->next = dentry;
593 dentry->prev = dentry;
594 dentry->parent = dentry;
599 ERROR("Failed to allocate new dentry");
604 static struct dentry *__new_dentry_with_inode(const char *name, bool timeless)
606 struct dentry *dentry;
607 dentry = new_dentry(name);
610 dentry->inode = new_timeless_inode();
612 dentry->inode = new_inode();
614 inode_add_dentry(dentry, dentry->inode);
623 struct dentry *new_dentry_with_timeless_inode(const char *name)
625 return __new_dentry_with_inode(name, true);
628 struct dentry *new_dentry_with_inode(const char *name)
630 return __new_dentry_with_inode(name, false);
633 void free_ads_entry(struct ads_entry *entry)
636 FREE(entry->stream_name);
637 FREE(entry->stream_name_utf8);
642 void inode_free_ads_entries(struct inode *inode)
644 if (inode->ads_entries) {
645 for (u16 i = 0; i < inode->num_ads; i++)
646 free_ads_entry(inode->ads_entries[i]);
647 FREE(inode->ads_entries);
651 /* Frees an inode. */
652 void free_inode(struct inode *inode)
655 inode_free_ads_entries(inode);
657 wimlib_assert(inode->num_opened_fds == 0);
664 /* Decrements link count on an inode and frees it if the link count reaches 0.
666 struct inode *put_inode(struct inode *inode)
669 wimlib_assert(inode->link_count);
670 if (--inode->link_count == 0) {
672 if (inode->num_opened_fds == 0)
683 /* Frees a WIM dentry.
685 * The inode is freed only if its link count is decremented to 0.
687 struct inode *free_dentry(struct dentry *dentry)
689 wimlib_assert(dentry);
692 FREE(dentry->file_name);
693 FREE(dentry->file_name_utf8);
694 FREE(dentry->short_name);
695 FREE(dentry->full_path_utf8);
696 inode = put_inode(dentry->inode);
701 void put_dentry(struct dentry *dentry)
703 wimlib_assert(dentry);
704 wimlib_assert(dentry->refcnt);
706 if (--dentry->refcnt == 0)
711 /* Partically clones a dentry.
714 * - memory for file names is not cloned (the pointers are all set to NULL
715 * and the lengths are set to zero)
716 * - next, prev, and children pointers and not touched
718 struct dentry *clone_dentry(struct dentry *old)
720 struct dentry *new = MALLOC(sizeof(struct dentry));
723 memcpy(new, old, sizeof(struct dentry));
724 new->file_name = NULL;
725 new->file_name_len = 0;
726 new->file_name_utf8 = NULL;
727 new->file_name_utf8_len = 0;
728 new->short_name = NULL;
729 new->short_name_len = 0;
735 * This function is passed as an argument to for_dentry_in_tree_depth() in order
736 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
738 static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
740 struct lookup_table *lookup_table = __lookup_table;
741 struct lookup_table_entry *lte;
742 struct inode *inode = dentry->inode;
746 wimlib_assert(inode->link_count);
747 for (i = 0; i <= inode->num_ads; i++) {
748 lte = inode_stream_lte(inode, i, lookup_table);
749 lte_decrement_refcnt(lte, lookup_table);
753 wimlib_assert(dentry->refcnt != 0);
754 if (--dentry->refcnt == 0)
760 * Unlinks and frees a dentry tree.
762 * @root: The root of the tree.
763 * @lookup_table: The lookup table for dentries. If non-NULL, the
764 * reference counts in the lookup table for the lookup
765 * table entries corresponding to the dentries will be
768 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
770 if (!root || !root->parent)
772 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
775 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
782 * Links a dentry into the directory tree.
784 * @dentry: The dentry to link.
785 * @parent: The dentry that will be the parent of @dentry.
787 void link_dentry(struct dentry *dentry, struct dentry *parent)
789 wimlib_assert(dentry_is_directory(parent));
790 dentry->parent = parent;
791 if (parent->inode->children) {
792 /* Not an only child; link to siblings. */
793 dentry->next = parent->inode->children;
794 dentry->prev = parent->inode->children->prev;
795 dentry->next->prev = dentry;
796 dentry->prev->next = dentry;
798 /* Only child; link to parent. */
799 parent->inode->children = dentry;
800 dentry->next = dentry;
801 dentry->prev = dentry;
807 * Unlink a dentry from the directory tree.
809 * Note: This merely removes it from the in-memory tree structure.
811 void unlink_dentry(struct dentry *dentry)
813 if (dentry_is_root(dentry))
815 if (dentry_is_only_child(dentry)) {
816 dentry->parent->inode->children = NULL;
818 if (dentry_is_first_sibling(dentry))
819 dentry->parent->inode->children = dentry->next;
820 dentry->next->prev = dentry->prev;
821 dentry->prev->next = dentry->next;
825 /* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
826 * and their lengths in the pointer arguments */
827 int get_names(char **name_utf16_ret, char **name_utf8_ret,
828 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
833 char *name_utf16, *name_utf8;
835 utf8_len = strlen(name);
837 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
840 return WIMLIB_ERR_NOMEM;
842 name_utf8 = MALLOC(utf8_len + 1);
845 return WIMLIB_ERR_NOMEM;
847 memcpy(name_utf8, name, utf8_len + 1);
848 FREE(*name_utf8_ret);
849 FREE(*name_utf16_ret);
850 *name_utf8_ret = name_utf8;
851 *name_utf16_ret = name_utf16;
852 *name_utf8_len_ret = utf8_len;
853 *name_utf16_len_ret = utf16_len;
857 /* Changes the name of a dentry to @new_name. Only changes the file_name and
858 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
859 * full_path_utf8 fields. Also recalculates its length. */
860 int change_dentry_name(struct dentry *dentry, const char *new_name)
864 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
865 &dentry->file_name_len, &dentry->file_name_utf8_len,
867 FREE(dentry->short_name);
868 dentry->short_name_len = 0;
870 dentry->length = dentry_correct_length(dentry);
875 * Changes the name of an alternate data stream */
876 int change_ads_name(struct ads_entry *entry, const char *new_name)
878 return get_names(&entry->stream_name, &entry->stream_name_utf8,
879 &entry->stream_name_len,
880 &entry->stream_name_utf8_len,
884 /* Parameters for calculate_dentry_statistics(). */
885 struct image_statistics {
886 struct lookup_table *lookup_table;
890 u64 *hard_link_bytes;
893 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
895 struct image_statistics *stats;
896 struct lookup_table_entry *lte;
900 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
903 ++*stats->file_count;
905 for (unsigned i = 0; i <= dentry->inode->num_ads; i++) {
906 lte = inode_stream_lte(dentry->inode, i, stats->lookup_table);
908 *stats->total_bytes += wim_resource_size(lte);
909 if (++lte->out_refcnt == 1)
910 *stats->hard_link_bytes += wim_resource_size(lte);
916 /* Calculates some statistics about a dentry tree. */
917 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
918 u64 *dir_count_ret, u64 *file_count_ret,
919 u64 *total_bytes_ret,
920 u64 *hard_link_bytes_ret)
922 struct image_statistics stats;
925 *total_bytes_ret = 0;
926 *hard_link_bytes_ret = 0;
927 stats.lookup_table = table;
928 stats.dir_count = dir_count_ret;
929 stats.file_count = file_count_ret;
930 stats.total_bytes = total_bytes_ret;
931 stats.hard_link_bytes = hard_link_bytes_ret;
932 for_lookup_table_entry(table, lte_zero_out_refcnt, NULL);
933 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
938 * Reads the alternate data stream entries for a dentry.
940 * @p: Pointer to buffer that starts with the first alternate stream entry.
942 * @inode: Inode to load the alternate data streams into.
943 * @inode->num_ads must have been set to the number of
944 * alternate data streams that are expected.
946 * @remaining_size: Number of bytes of data remaining in the buffer pointed
949 * The format of the on-disk alternate stream entries is as follows:
951 * struct ads_entry_on_disk {
952 * u64 length; // Length of the entry, in bytes. This includes
953 * all fields (including the stream name and
954 * null terminator if present, AND the padding!).
955 * u64 reserved; // Seems to be unused
956 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
957 * u16 stream_name_len; // Length of the stream name, in bytes
958 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
959 * not including null terminator
960 * u16 zero; // UTF-16 null terminator for the stream name, NOT
961 * included in @stream_name_len. Based on what
962 * I've observed from filenames in dentries,
963 * this field should not exist when
964 * (@stream_name_len == 0), but you can't
965 * actually tell because of the padding anyway
966 * (provided that the padding is zeroed, which
967 * it always seems to be).
968 * char padding[]; // Padding to make the size a multiple of 8 bytes.
971 * In addition, the entries are 8-byte aligned.
973 * Return 0 on success or nonzero on failure. On success, inode->ads_entries
974 * is set to an array of `struct ads_entry's of length inode->num_ads. On
975 * failure, @inode is not modified.
977 static int read_ads_entries(const u8 *p, struct inode *inode,
981 struct ads_entry **ads_entries;
984 num_ads = inode->num_ads;
985 ads_entries = CALLOC(num_ads, sizeof(inode->ads_entries[0]));
987 ERROR("Could not allocate memory for %"PRIu16" "
988 "alternate data stream entries", num_ads);
989 return WIMLIB_ERR_NOMEM;
992 for (u16 i = 0; i < num_ads; i++) {
993 struct ads_entry *cur_entry;
995 u64 length_no_padding;
998 const u8 *p_save = p;
1000 cur_entry = new_ads_entry(NULL);
1002 ret = WIMLIB_ERR_NOMEM;
1003 goto out_free_ads_entries;
1006 ads_entries[i] = cur_entry;
1009 ads_entries[i]->stream_id = i + 1;
1012 /* Read the base stream entry, excluding the stream name. */
1013 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1014 ERROR("Stream entries go past end of metadata resource");
1015 ERROR("(remaining_size = %"PRIu64")", remaining_size);
1016 ret = WIMLIB_ERR_INVALID_DENTRY;
1017 goto out_free_ads_entries;
1020 p = get_u64(p, &length);
1021 p += 8; /* Skip the reserved field */
1022 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
1023 p = get_u16(p, &cur_entry->stream_name_len);
1025 cur_entry->stream_name = NULL;
1026 cur_entry->stream_name_utf8 = NULL;
1028 /* Length including neither the null terminator nor the padding
1030 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1031 cur_entry->stream_name_len;
1033 /* Length including the null terminator and the padding */
1034 total_length = ((length_no_padding + 2) + 7) & ~7;
1036 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1038 if (remaining_size < length_no_padding) {
1039 ERROR("Stream entries go past end of metadata resource");
1040 ERROR("(remaining_size = %"PRIu64" bytes, "
1041 "length_no_padding = %"PRIu64" bytes)",
1042 remaining_size, length_no_padding);
1043 ret = WIMLIB_ERR_INVALID_DENTRY;
1044 goto out_free_ads_entries;
1047 /* The @length field in the on-disk ADS entry is expected to be
1048 * equal to @total_length, which includes all of the entry and
1049 * the padding that follows it to align the next ADS entry to an
1050 * 8-byte boundary. However, to be safe, we'll accept the
1051 * length field as long as it's not less than the un-padded
1052 * total length and not more than the padded total length. */
1053 if (length < length_no_padding || length > total_length) {
1054 ERROR("Stream entry has unexpected length "
1055 "field (length field = %"PRIu64", "
1056 "unpadded total length = %"PRIu64", "
1057 "padded total length = %"PRIu64")",
1058 length, length_no_padding, total_length);
1059 ret = WIMLIB_ERR_INVALID_DENTRY;
1060 goto out_free_ads_entries;
1063 if (cur_entry->stream_name_len) {
1064 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
1065 if (!cur_entry->stream_name) {
1066 ret = WIMLIB_ERR_NOMEM;
1067 goto out_free_ads_entries;
1069 get_bytes(p, cur_entry->stream_name_len,
1070 (u8*)cur_entry->stream_name);
1071 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
1072 cur_entry->stream_name_len,
1074 cur_entry->stream_name_utf8_len = utf8_len;
1076 if (!cur_entry->stream_name_utf8) {
1077 ret = WIMLIB_ERR_NOMEM;
1078 goto out_free_ads_entries;
1081 /* It's expected that the size of every ADS entry is a multiple
1082 * of 8. However, to be safe, I'm allowing the possibility of
1083 * an ADS entry at the very end of the metadata resource ending
1084 * un-aligned. So although we still need to increment the input
1085 * pointer by @total_length to reach the next ADS entry, it's
1086 * possible that less than @total_length is actually remaining
1087 * in the metadata resource. We should set the remaining size to
1088 * 0 bytes if this happens. */
1089 p = p_save + total_length;
1090 if (remaining_size < total_length)
1093 remaining_size -= total_length;
1095 inode->ads_entries = ads_entries;
1097 inode->next_stream_id = inode->num_ads + 1;
1100 out_free_ads_entries:
1101 for (u16 i = 0; i < num_ads; i++)
1102 free_ads_entry(ads_entries[i]);
1108 * Reads a directory entry, including all alternate data stream entries that
1109 * follow it, from the WIM image's metadata resource.
1111 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1112 * @metadata_resource_len: Length of the metadata resource.
1113 * @offset: Offset of this directory entry in the metadata resource.
1114 * @dentry: A `struct dentry' that will be filled in by this function.
1116 * Return 0 on success or nonzero on failure. On failure, @dentry have been
1117 * modified, bu it will be left with no pointers to any allocated buffers.
1118 * On success, the dentry->length field must be examined. If zero, this was a
1119 * special "end of directory" dentry and not a real dentry. If nonzero, this
1120 * was a real dentry.
1122 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1123 u64 offset, struct dentry *dentry)
1126 u64 calculated_size;
1127 char *file_name = NULL;
1128 char *file_name_utf8 = NULL;
1129 char *short_name = NULL;
1132 size_t file_name_utf8_len = 0;
1134 struct inode *inode = NULL;
1136 dentry_common_init(dentry);
1138 /*Make sure the dentry really fits into the metadata resource.*/
1139 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1140 ERROR("Directory entry starting at %"PRIu64" ends past the "
1141 "end of the metadata resource (size %"PRIu64")",
1142 offset, metadata_resource_len);
1143 return WIMLIB_ERR_INVALID_DENTRY;
1146 /* Before reading the whole dentry, we need to read just the length.
1147 * This is because a dentry of length 8 (that is, just the length field)
1148 * terminates the list of sibling directory entries. */
1150 p = get_u64(&metadata_resource[offset], &dentry->length);
1152 /* A zero length field (really a length of 8, since that's how big the
1153 * directory entry is...) indicates that this is the end of directory
1154 * dentry. We do not read it into memory as an actual dentry, so just
1155 * return successfully in that case. */
1156 if (dentry->length == 0)
1159 /* If the dentry does not overflow the metadata resource buffer and is
1160 * not too short, read the rest of it (excluding the alternate data
1161 * streams, but including the file name and short name variable-length
1162 * fields) into memory. */
1163 if (offset + dentry->length >= metadata_resource_len
1164 || offset + dentry->length < offset)
1166 ERROR("Directory entry at offset %"PRIu64" and with size "
1167 "%"PRIu64" ends past the end of the metadata resource "
1169 offset, dentry->length, metadata_resource_len);
1170 return WIMLIB_ERR_INVALID_DENTRY;
1173 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1174 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1176 return WIMLIB_ERR_INVALID_DENTRY;
1179 inode = new_timeless_inode();
1181 return WIMLIB_ERR_NOMEM;
1183 p = get_u32(p, &inode->attributes);
1184 p = get_u32(p, (u32*)&inode->security_id);
1185 p = get_u64(p, &dentry->subdir_offset);
1187 /* 2 unused fields */
1188 p += 2 * sizeof(u64);
1189 /*p = get_u64(p, &dentry->unused1);*/
1190 /*p = get_u64(p, &dentry->unused2);*/
1192 p = get_u64(p, &inode->creation_time);
1193 p = get_u64(p, &inode->last_access_time);
1194 p = get_u64(p, &inode->last_write_time);
1196 p = get_bytes(p, SHA1_HASH_SIZE, inode->hash);
1199 * I don't know what's going on here. It seems like M$ screwed up the
1200 * reparse points, then put the fields in the same place and didn't
1201 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1202 * have something to do with this, but it's not documented.
1204 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1207 p = get_u32(p, &inode->reparse_tag);
1210 p = get_u32(p, &inode->reparse_tag);
1211 p = get_u64(p, &inode->ino);
1214 /* By the way, the reparse_reserved field does not actually exist (at
1215 * least when the file is not a reparse point) */
1217 p = get_u16(p, &inode->num_ads);
1219 p = get_u16(p, &short_name_len);
1220 p = get_u16(p, &file_name_len);
1222 /* We now know the length of the file name and short name. Make sure
1223 * the length of the dentry is large enough to actually hold them.
1225 * The calculated length here is unaligned to allow for the possibility
1226 * that the dentry->length names an unaligned length, although this
1227 * would be unexpected. */
1228 calculated_size = __dentry_correct_length_unaligned(file_name_len,
1231 if (dentry->length < calculated_size) {
1232 ERROR("Unexpected end of directory entry! (Expected "
1233 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1234 "short_name_len = %hu, file_name_len = %hu)",
1235 calculated_size, dentry->length,
1236 short_name_len, file_name_len);
1237 return WIMLIB_ERR_INVALID_DENTRY;
1240 /* Read the filename if present. Note: if the filename is empty, there
1241 * is no null terminator following it. */
1242 if (file_name_len) {
1243 file_name = MALLOC(file_name_len);
1245 ERROR("Failed to allocate %hu bytes for dentry file name",
1247 return WIMLIB_ERR_NOMEM;
1249 p = get_bytes(p, file_name_len, file_name);
1251 /* Convert filename to UTF-8. */
1252 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1253 &file_name_utf8_len);
1255 if (!file_name_utf8) {
1256 ERROR("Failed to allocate memory to convert UTF-16 "
1257 "filename (%hu bytes) to UTF-8", file_name_len);
1258 ret = WIMLIB_ERR_NOMEM;
1259 goto out_free_file_name;
1262 WARNING("Expected two zero bytes following the file name "
1263 "`%s', but found non-zero bytes", file_name_utf8);
1267 /* Align the calculated size */
1268 calculated_size = (calculated_size + 7) & ~7;
1270 if (dentry->length > calculated_size) {
1271 /* Weird; the dentry says it's longer than it should be. Note
1272 * that the length field does NOT include the size of the
1273 * alternate stream entries. */
1275 /* Strangely, some directory entries inexplicably have a little
1276 * over 70 bytes of extra data. The exact amount of data seems
1277 * to be 72 bytes, but it is aligned on the next 8-byte
1278 * boundary. It does NOT seem to be alternate data stream
1279 * entries. Here's an example of the aligned data:
1281 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1282 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1283 * 00000000 00000000 00000000 00000000
1285 * Here's one interpretation of how the data is laid out.
1288 * u32 field1; (always 0x00000001)
1289 * u32 field2; (always 0x40000000)
1290 * u8 data[48]; (???)
1291 * u64 reserved1; (always 0)
1292 * u64 reserved2; (always 0)
1294 DEBUG("Dentry for file or directory `%s' has %zu extra "
1296 file_name_utf8, dentry->length - calculated_size);
1299 /* Read the short filename if present. Note: if there is no short
1300 * filename, there is no null terminator following it. */
1301 if (short_name_len) {
1302 short_name = MALLOC(short_name_len);
1304 ERROR("Failed to allocate %hu bytes for short filename",
1306 ret = WIMLIB_ERR_NOMEM;
1307 goto out_free_file_name_utf8;
1310 p = get_bytes(p, short_name_len, short_name);
1312 WARNING("Expected two zero bytes following the file name "
1313 "`%s', but found non-zero bytes", file_name_utf8);
1318 * Read the alternate data streams, if present. dentry->num_ads tells
1319 * us how many they are, and they will directly follow the dentry
1322 * Note that each alternate data stream entry begins on an 8-byte
1323 * aligned boundary, and the alternate data stream entries are NOT
1324 * included in the dentry->length field for some reason.
1326 if (inode->num_ads != 0) {
1327 if (calculated_size > metadata_resource_len - offset) {
1328 ERROR("Not enough space in metadata resource for "
1329 "alternate stream entries");
1330 ret = WIMLIB_ERR_INVALID_DENTRY;
1331 goto out_free_short_name;
1333 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1335 metadata_resource_len - offset - calculated_size);
1337 goto out_free_short_name;
1340 /* We've read all the data for this dentry. Set the names and their
1341 * lengths, and we've done. */
1342 dentry->inode = inode;
1343 dentry->file_name = file_name;
1344 dentry->file_name_utf8 = file_name_utf8;
1345 dentry->short_name = short_name;
1346 dentry->file_name_len = file_name_len;
1347 dentry->file_name_utf8_len = file_name_utf8_len;
1348 dentry->short_name_len = short_name_len;
1350 out_free_short_name:
1352 out_free_file_name_utf8:
1353 FREE(file_name_utf8);
1361 int verify_inode(struct inode *inode, const WIMStruct *w)
1363 const struct lookup_table *table = w->lookup_table;
1364 const struct wim_security_data *sd = wim_const_security_data(w);
1365 const struct dentry *first_dentry = inode_first_dentry(inode);
1366 int ret = WIMLIB_ERR_INVALID_DENTRY;
1368 /* Check the security ID */
1369 if (inode->security_id < -1) {
1370 ERROR("Dentry `%s' has an invalid security ID (%d)",
1371 first_dentry->full_path_utf8, inode->security_id);
1374 if (inode->security_id >= sd->num_entries) {
1375 ERROR("Dentry `%s' has an invalid security ID (%d) "
1376 "(there are only %u entries in the security table)",
1377 first_dentry->full_path_utf8, inode->security_id,
1382 /* Check that lookup table entries for all the resources exist, except
1383 * if the SHA1 message digest is all 0's, which indicates there is
1384 * intentionally no resource there. */
1385 if (w->hdr.total_parts == 1) {
1386 for (unsigned i = 0; i <= inode->num_ads; i++) {
1387 struct lookup_table_entry *lte;
1389 hash = inode_stream_hash_unresolved(inode, i);
1390 lte = __lookup_resource(table, hash);
1391 if (!lte && !is_zero_hash(hash)) {
1392 ERROR("Could not find lookup table entry for stream "
1393 "%u of dentry `%s'", i, first_dentry->full_path_utf8);
1396 if (lte && (lte->real_refcnt += inode->link_count) > lte->refcnt)
1398 #ifdef ENABLE_ERROR_MESSAGES
1399 WARNING("The following lookup table entry "
1400 "has a reference count of %u, but",
1402 WARNING("We found %zu references to it",
1404 WARNING("(One dentry referencing it is at `%s')",
1405 first_dentry->full_path_utf8);
1407 print_lookup_table_entry(lte);
1409 /* Guess what! install.wim for Windows 8
1410 * contains a stream with 2 dentries referencing
1411 * it, but the lookup table entry has reference
1412 * count of 1. So we will need to handle this
1413 * case and not just make it be an error... I'm
1414 * just setting the reference count to the
1415 * number of references we found.
1416 * (Unfortunately, even after doing this, the
1417 * reference count could be too low if it's also
1418 * referenced in other WIM images) */
1421 lte->refcnt = lte->real_refcnt;
1422 WARNING("Fixing reference count");
1430 /* Make sure there is only one un-named stream. */
1431 unsigned num_unnamed_streams = 0;
1432 for (unsigned i = 0; i <= inode->num_ads; i++) {
1434 hash = inode_stream_hash_unresolved(inode, i);
1435 if (!inode_stream_name_len(inode, i) && !is_zero_hash(hash))
1436 num_unnamed_streams++;
1438 if (num_unnamed_streams > 1) {
1439 ERROR("Dentry `%s' has multiple (%u) un-named streams",
1440 first_dentry->full_path_utf8, num_unnamed_streams);
1443 inode->verified = true;
1450 /* Run some miscellaneous verifications on a WIM dentry */
1451 int verify_dentry(struct dentry *dentry, void *wim)
1453 const WIMStruct *w = wim;
1454 const struct inode *inode = dentry->inode;
1455 int ret = WIMLIB_ERR_INVALID_DENTRY;
1457 if (!dentry->inode->verified) {
1458 ret = verify_inode(dentry->inode, w);
1463 /* Cannot have a short name but no long name */
1464 if (dentry->short_name_len && !dentry->file_name_len) {
1465 ERROR("Dentry `%s' has a short name but no long name",
1466 dentry->full_path_utf8);
1470 /* Make sure root dentry is unnamed */
1471 if (dentry_is_root(dentry)) {
1472 if (dentry->file_name_len) {
1473 ERROR("The root dentry is named `%s', but it must "
1474 "be unnamed", dentry->file_name_utf8);
1480 /* Check timestamps */
1481 if (inode->last_access_time < inode->creation_time ||
1482 inode->last_write_time < inode->creation_time) {
1483 WARNING("Dentry `%s' was created after it was last accessed or "
1484 "written to", dentry->full_path_utf8);
1494 * Writes a WIM dentry to an output buffer.
1496 * @dentry: The dentry structure.
1497 * @p: The memory location to write the data to.
1498 * @return: Pointer to the byte after the last byte we wrote as part of the
1501 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1505 const struct inode *inode = dentry->inode;
1507 /* We calculate the correct length of the dentry ourselves because the
1508 * dentry->length field may been set to an unexpected value from when we
1509 * read the dentry in (for example, there may have been unknown data
1510 * appended to the end of the dentry...) */
1511 u64 length = dentry_correct_length(dentry);
1513 p = put_u64(p, length);
1514 p = put_u32(p, inode->attributes);
1515 p = put_u32(p, inode->security_id);
1516 p = put_u64(p, dentry->subdir_offset);
1517 p = put_u64(p, 0); /* unused1 */
1518 p = put_u64(p, 0); /* unused2 */
1519 p = put_u64(p, inode->creation_time);
1520 p = put_u64(p, inode->last_access_time);
1521 p = put_u64(p, inode->last_write_time);
1522 hash = inode_stream_hash(inode, 0);
1523 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1524 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1525 p = put_zeroes(p, 4);
1526 p = put_u32(p, inode->reparse_tag);
1527 p = put_zeroes(p, 4);
1531 if (inode->link_count == 1)
1534 link_group_id = inode->ino;
1535 p = put_u64(p, link_group_id);
1537 p = put_u16(p, inode->num_ads);
1538 p = put_u16(p, dentry->short_name_len);
1539 p = put_u16(p, dentry->file_name_len);
1540 if (dentry->file_name_len) {
1541 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1542 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1544 if (dentry->short_name) {
1545 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1546 p = put_u16(p, 0); /* short name padding, 2 bytes */
1549 /* Align to 8-byte boundary */
1550 wimlib_assert(length >= (p - orig_p)
1551 && length - (p - orig_p) <= 7);
1552 p = put_zeroes(p, length - (p - orig_p));
1554 /* Write the alternate data streams, if there are any. Please see
1555 * read_ads_entries() for comments about the format of the on-disk
1556 * alternate data stream entries. */
1557 for (u16 i = 0; i < inode->num_ads; i++) {
1558 p = put_u64(p, ads_entry_total_length(inode->ads_entries[i]));
1559 p = put_u64(p, 0); /* Unused */
1560 hash = inode_stream_hash(inode, i + 1);
1561 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1562 p = put_u16(p, inode->ads_entries[i]->stream_name_len);
1563 if (inode->ads_entries[i]->stream_name_len) {
1564 p = put_bytes(p, inode->ads_entries[i]->stream_name_len,
1565 (u8*)inode->ads_entries[i]->stream_name);
1568 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1570 #ifdef ENABLE_ASSERTIONS
1571 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1576 /* Recursive function that writes a dentry tree rooted at @parent, not including
1577 * @parent itself, which has already been written. */
1578 static u8 *write_dentry_tree_recursive(const struct dentry *parent, u8 *p)
1580 const struct dentry *child;
1582 /* Nothing to do if this dentry has no children. */
1583 if (parent->subdir_offset == 0)
1586 /* Write child dentries and end-of-directory entry.
1588 * Note: we need to write all of this dentry's children before
1589 * recursively writing the directory trees rooted at each of the child
1590 * dentries, since the on-disk dentries for a dentry's children are
1591 * always located at consecutive positions in the metadata resource! */
1592 child = parent->inode->children;
1595 p = write_dentry(child, p);
1596 child = child->next;
1597 } while (child != parent->inode->children);
1600 /* write end of directory entry */
1603 /* Recurse on children. */
1606 p = write_dentry_tree_recursive(child, p);
1607 child = child->next;
1608 } while (child != parent->inode->children);
1613 /* Writes a directory tree to the metadata resource.
1615 * @root: Root of the dentry tree.
1616 * @p: Pointer to a buffer with enough space for the dentry tree.
1618 * Returns pointer to the byte after the last byte we wrote.
1620 u8 *write_dentry_tree(const struct dentry *root, u8 *p)
1622 wimlib_assert(dentry_is_root(root));
1624 /* If we're the root dentry, we have no parent that already
1625 * wrote us, so we need to write ourselves. */
1626 p = write_dentry(root, p);
1628 /* Write end of directory entry after the root dentry just to be safe;
1629 * however the root dentry obviously cannot have any siblings. */
1632 /* Recursively write the rest of the dentry tree. */
1633 return write_dentry_tree_recursive(root, p);
1636 /* Reads the children of a dentry, and all their children, ..., etc. from the
1637 * metadata resource and into the dentry tree.
1639 * @metadata_resource: An array that contains the uncompressed metadata
1640 * resource for the WIM file.
1642 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1645 * @dentry: A pointer to a `struct dentry' that is the root of the directory
1646 * tree and has already been read from the metadata resource. It
1647 * does not need to be the real root because this procedure is
1648 * called recursively.
1650 * @return: Zero on success, nonzero on failure.
1652 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1653 struct dentry *dentry)
1655 u64 cur_offset = dentry->subdir_offset;
1656 struct dentry *prev_child = NULL;
1657 struct dentry *first_child = NULL;
1658 struct dentry *child;
1659 struct dentry cur_child;
1663 * If @dentry has no child dentries, nothing more needs to be done for
1664 * this branch. This is the case for regular files, symbolic links, and
1665 * *possibly* empty directories (although an empty directory may also
1666 * have one child dentry that is the special end-of-directory dentry)
1668 if (cur_offset == 0)
1671 /* Find and read all the children of @dentry. */
1674 /* Read next child of @dentry into @cur_child. */
1675 ret = read_dentry(metadata_resource, metadata_resource_len,
1676 cur_offset, &cur_child);
1680 /* Check for end of directory. */
1681 if (cur_child.length == 0)
1684 /* Not end of directory. Allocate this child permanently and
1685 * link it to the parent and previous child. */
1686 child = MALLOC(sizeof(struct dentry));
1688 ERROR("Failed to allocate %zu bytes for new dentry",
1689 sizeof(struct dentry));
1690 ret = WIMLIB_ERR_NOMEM;
1693 memcpy(child, &cur_child, sizeof(struct dentry));
1696 prev_child->next = child;
1697 child->prev = prev_child;
1699 first_child = child;
1702 child->parent = dentry;
1704 inode_add_dentry(child, child->inode);
1706 /* If there are children of this child, call this procedure
1708 if (child->subdir_offset != 0) {
1709 ret = read_dentry_tree(metadata_resource,
1710 metadata_resource_len, child);
1715 /* Advance to the offset of the next child. Note: We need to
1716 * advance by the TOTAL length of the dentry, not by the length
1717 * child->length, which although it does take into account the
1718 * padding, it DOES NOT take into account alternate stream
1720 cur_offset += dentry_total_length(child);
1723 /* Link last child to first one, and set parent's children pointer to
1724 * the first child. */
1726 prev_child->next = first_child;
1727 first_child->prev = prev_child;
1729 dentry->inode->children = first_child;