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"
44 * Returns true if @dentry has the UTF-8 file name @name that has length
47 static bool dentry_has_name(const struct dentry *dentry, const char *name,
50 if (dentry->file_name_utf8_len != name_len)
52 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
55 /* Real length of a dentry, including the alternate data stream entries, which
56 * are not included in the dentry->length field... */
57 u64 dentry_total_length(const struct dentry *dentry)
59 u64 length = (dentry->length + 7) & ~7;
60 for (u16 i = 0 ; i < dentry->num_ads; i++)
61 length += ads_entry_length(&dentry->ads_entries[i]);
65 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
66 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
68 if (S_ISLNK(stbuf->st_mode)) {
69 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
70 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
71 } else if (S_ISDIR(stbuf->st_mode)) {
72 dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
74 dentry->attributes = FILE_ATTRIBUTE_NORMAL;
76 if (sizeof(ino_t) >= 8)
77 dentry->hard_link = (u64)stbuf->st_ino;
79 dentry->hard_link = (u64)stbuf->st_ino |
80 ((u64)stbuf->st_dev << (sizeof(ino_t) * 8));
84 /* Makes all timestamp fields for the dentry be the current time. */
85 void dentry_update_all_timestamps(struct dentry *dentry)
87 u64 now = get_wim_timestamp();
88 dentry->creation_time = now;
89 dentry->last_access_time = now;
90 dentry->last_write_time = now;
93 struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
94 const char *stream_name)
96 size_t stream_name_len = strlen(stream_name);
99 for (u16 i = 0; i < dentry->num_ads; i++)
100 if (ads_entry_has_name(&dentry->ads_entries[i],
101 stream_name, stream_name_len))
102 return &dentry->ads_entries[i];
106 static void ads_entry_init(struct ads_entry *ads_entry)
108 memset(ads_entry, 0, sizeof(struct ads_entry));
109 INIT_LIST_HEAD(&ads_entry->lte_group_list.list);
110 ads_entry->lte_group_list.type = STREAM_TYPE_ADS;
113 /* Add an alternate stream entry to a dentry and return a pointer to it, or NULL
115 struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
118 struct ads_entry *ads_entries;
119 struct ads_entry *new_entry;
121 if (dentry->num_ads == 0xffff)
123 num_ads = dentry->num_ads + 1;
124 ads_entries = REALLOC(dentry->ads_entries,
125 num_ads * sizeof(struct ads_entry));
128 if (ads_entries != dentry->ads_entries) {
129 /* We moved the ADS entries. Adjust the stream lists. */
130 for (u16 i = 0; i < dentry->num_ads; i++) {
131 struct list_head *cur = &ads_entries[i].lte_group_list.list;
132 cur->prev->next = cur;
133 cur->next->prev = cur;
136 dentry->ads_entries = ads_entries;
138 new_entry = &ads_entries[num_ads - 1];
139 if (change_ads_name(new_entry, stream_name) != 0)
141 dentry->num_ads = num_ads;
142 ads_entry_init(new_entry);
146 void dentry_remove_ads(struct dentry *dentry, struct ads_entry *ads_entry)
151 wimlib_assert(dentry->num_ads);
152 idx = ads_entry - dentry->ads_entries;
153 wimlib_assert(idx < dentry->num_ads);
154 following = dentry->num_ads - idx - 1;
156 destroy_ads_entry(ads_entry);
157 memcpy(ads_entry, ads_entry + 1, following * sizeof(struct ads_entry));
159 /* We moved the ADS entries. Adjust the stream lists. */
160 for (u16 i = 0; i < following; i++) {
161 struct list_head *cur = &ads_entry[i].lte_group_list.list;
162 cur->prev->next = cur;
163 cur->next->prev = cur;
170 * Calls a function on all directory entries in a directory tree. It is called
171 * on a parent before its children.
173 int for_dentry_in_tree(struct dentry *root,
174 int (*visitor)(struct dentry*, void*), void *arg)
177 struct dentry *child;
179 ret = visitor(root, arg);
184 child = root->children;
190 ret = for_dentry_in_tree(child, visitor, arg);
194 } while (child != root->children);
199 * Like for_dentry_in_tree(), but the visitor function is always called on a
200 * dentry's children before on itself.
202 int for_dentry_in_tree_depth(struct dentry *root,
203 int (*visitor)(struct dentry*, void*), void *arg)
206 struct dentry *child;
209 child = root->children;
213 ret = for_dentry_in_tree_depth(child, visitor, arg);
217 } while (child != root->children);
219 return visitor(root, arg);
223 * Calculate the full path of @dentry, based on its parent's full path and on
224 * its UTF-8 file name.
226 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
230 if (dentry_is_root(dentry)) {
231 full_path = MALLOC(2);
238 char *parent_full_path;
239 u32 parent_full_path_len;
240 const struct dentry *parent = dentry->parent;
242 if (dentry_is_root(parent)) {
243 parent_full_path = "";
244 parent_full_path_len = 0;
246 parent_full_path = parent->full_path_utf8;
247 parent_full_path_len = parent->full_path_utf8_len;
250 full_path_len = parent_full_path_len + 1 +
251 dentry->file_name_utf8_len;
252 full_path = MALLOC(full_path_len + 1);
256 memcpy(full_path, parent_full_path, parent_full_path_len);
257 full_path[parent_full_path_len] = '/';
258 memcpy(full_path + parent_full_path_len + 1,
259 dentry->file_name_utf8,
260 dentry->file_name_utf8_len);
261 full_path[full_path_len] = '\0';
263 FREE(dentry->full_path_utf8);
264 dentry->full_path_utf8 = full_path;
265 dentry->full_path_utf8_len = full_path_len;
268 ERROR("Out of memory while calculating dentry full path");
269 return WIMLIB_ERR_NOMEM;
273 * Recursively calculates the subdir offsets for a directory tree.
275 * @dentry: The root of the directory tree.
276 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
277 * offset for @dentry.
279 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
281 struct dentry *child;
283 child = dentry->children;
284 dentry->subdir_offset = *subdir_offset_p;
287 /* Advance the subdir offset by the amount of space the children
288 * of this dentry take up. */
290 *subdir_offset_p += dentry_total_length(child);
292 } while (child != dentry->children);
294 /* End-of-directory dentry on disk. */
295 *subdir_offset_p += 8;
297 /* Recursively call calculate_subdir_offsets() on all the
300 calculate_subdir_offsets(child, subdir_offset_p);
302 } while (child != dentry->children);
304 /* On disk, childless directories have a valid subdir_offset
305 * that points to an 8-byte end-of-directory dentry. Regular
306 * files have a subdir_offset of 0. */
307 if (dentry_is_directory(dentry))
308 *subdir_offset_p += 8;
310 dentry->subdir_offset = 0;
315 /* Returns the child of @dentry that has the file name @name.
316 * Returns NULL if no child has the name. */
317 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
320 struct dentry *child;
323 child = dentry->children;
325 name_len = strlen(name);
327 if (dentry_has_name(child, name, name_len))
330 } while (child != dentry->children);
335 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
336 * @cur_dir. Returns NULL if no dentry having the path is found. */
337 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
339 struct dentry *child;
341 const char *new_path;
346 child = cur_dir->children;
348 new_path = path_next_part(path, &base_len);
350 if (dentry_has_name(child, path, base_len))
351 return get_dentry_relative_path(child, new_path);
353 } while (child != cur_dir->children);
358 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
360 struct dentry *get_dentry(WIMStruct *w, const char *path)
362 struct dentry *root = wim_root_dentry(w);
365 return get_dentry_relative_path(root, path);
368 /* Returns the parent directory for the @path. */
369 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
371 size_t path_len = strlen(path);
372 char buf[path_len + 1];
374 memcpy(buf, path, path_len + 1);
376 to_parent_name(buf, path_len);
378 return get_dentry(w, buf);
381 /* Prints the full path of a dentry. */
382 int print_dentry_full_path(struct dentry *dentry, void *ignore)
384 if (dentry->full_path_utf8)
385 puts(dentry->full_path_utf8);
389 struct file_attr_flag {
393 struct file_attr_flag file_attr_flags[] = {
394 {FILE_ATTRIBUTE_READONLY, "READONLY"},
395 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
396 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
397 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
398 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
399 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
400 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
401 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
402 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
403 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
404 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
405 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
406 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
407 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
408 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
411 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
412 * NULL if the resource entry for the dentry is not to be printed. */
413 int print_dentry(struct dentry *dentry, void *lookup_table)
417 printf("[DENTRY]\n");
418 printf("Length = %"PRIu64"\n", dentry->length);
419 printf("Attributes = 0x%x\n", dentry->attributes);
420 for (unsigned i = 0; i < ARRAY_LEN(file_attr_flags); i++)
421 if (file_attr_flags[i].flag & dentry->attributes)
422 printf(" FILE_ATTRIBUTE_%s is set\n",
423 file_attr_flags[i].name);
424 printf("Security ID = %d\n", dentry->security_id);
425 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
426 /*printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);*/
427 /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
428 printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
429 printf("Last Access Time = 0x%"PRIx64"\n", dentry->last_access_time);
430 printf("Last Write Time = 0x%"PRIx64"\n", dentry->last_write_time);
431 hash = dentry_stream_hash(dentry, 0);
437 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
438 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
439 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
440 printf("Filename = \"");
441 print_string(dentry->file_name, dentry->file_name_len);
443 printf("Filename Length = %hu\n", dentry->file_name_len);
444 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
445 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
446 printf("Short Name = \"");
447 print_string(dentry->short_name, dentry->short_name_len);
449 printf("Short Name Length = %hu\n", dentry->short_name_len);
450 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
451 print_lookup_table_entry(dentry_stream_lte(dentry, 0, lookup_table));
452 for (u16 i = 0; i < dentry->num_ads; i++) {
453 printf("[Alternate Stream Entry %u]\n", i);
454 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
455 printf("Name Length (UTF-16) = %u\n",
456 dentry->ads_entries[i].stream_name_len);
457 hash = dentry_stream_hash(dentry, i + 1);
463 print_lookup_table_entry(dentry_stream_lte(dentry, i + 1,
469 static inline void dentry_common_init(struct dentry *dentry)
471 memset(dentry, 0, sizeof(struct dentry));
473 dentry->security_id = -1;
474 dentry->ads_entries_status = ADS_ENTRIES_DEFAULT;
475 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
479 * Creates an unlinked directory entry.
481 * @name: The base name of the new dentry.
482 * @return: A pointer to the new dentry, or NULL if out of memory.
484 struct dentry *new_dentry(const char *name)
486 struct dentry *dentry;
488 dentry = MALLOC(sizeof(struct dentry));
492 dentry_common_init(dentry);
493 if (change_dentry_name(dentry, name) != 0)
496 dentry_update_all_timestamps(dentry);
497 dentry->next = dentry;
498 dentry->prev = dentry;
499 dentry->parent = dentry;
500 INIT_LIST_HEAD(&dentry->link_group_list);
504 ERROR("Failed to allocate new dentry");
508 void dentry_free_ads_entries(struct dentry *dentry)
510 for (u16 i = 0; i < dentry->num_ads; i++)
511 destroy_ads_entry(&dentry->ads_entries[i]);
512 FREE(dentry->ads_entries);
513 dentry->ads_entries = NULL;
517 static void __destroy_dentry(struct dentry *dentry)
519 FREE(dentry->file_name);
520 FREE(dentry->file_name_utf8);
521 FREE(dentry->short_name);
522 FREE(dentry->full_path_utf8);
523 FREE(dentry->extracted_file);
526 void free_dentry(struct dentry *dentry)
528 wimlib_assert(dentry);
529 __destroy_dentry(dentry);
530 if (dentry->ads_entries_status != ADS_ENTRIES_USER)
531 dentry_free_ads_entries(dentry);
535 /* Like free_dentry(), but assigns a new ADS entries owner if this dentry was
536 * the previous owner, and also deletes the dentry from its link_group_list */
537 void put_dentry(struct dentry *dentry)
539 if (dentry->ads_entries_status == ADS_ENTRIES_OWNER) {
540 struct dentry *new_owner;
541 list_for_each_entry(new_owner, &dentry->link_group_list,
544 if (new_owner->ads_entries_status == ADS_ENTRIES_USER) {
545 new_owner->ads_entries_status = ADS_ENTRIES_OWNER;
549 dentry->ads_entries_status = ADS_ENTRIES_USER;
551 struct list_head *next;
552 list_del(&dentry->link_group_list);
560 * - memory for file names is not cloned
561 * - next, prev, and children pointers and not touched
562 * - stream entries are not cloned.
564 struct dentry *clone_dentry(struct dentry *old)
566 struct dentry *new = MALLOC(sizeof(struct dentry));
569 memcpy(new, old, sizeof(struct dentry));
570 new->file_name = NULL;
571 new->file_name_len = 0;
572 new->file_name_utf8 = NULL;
573 new->file_name_utf8_len = 0;
574 new->short_name = NULL;
575 new->short_name_len = 0;
580 * This function is passed as an argument to for_dentry_in_tree_depth() in order
581 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
583 static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
585 struct lookup_table *lookup_table = __lookup_table;
587 struct lookup_table_entry *lte;
588 if (dentry->resolved)
591 lte = __lookup_resource(lookup_table, dentry->hash);
592 lte_decrement_refcnt(lte, lookup_table);
595 wimlib_assert(dentry->refcnt != 0);
596 if (--dentry->refcnt == 0)
602 * Unlinks and frees a dentry tree.
604 * @root: The root of the tree.
605 * @lookup_table: The lookup table for dentries. If non-NULL, the
606 * reference counts in the lookup table for the lookup
607 * table entries corresponding to the dentries will be
610 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
612 if (!root || !root->parent)
614 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
617 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
624 * Links a dentry into the directory tree.
626 * @dentry: The dentry to link.
627 * @parent: The dentry that will be the parent of @dentry.
629 void link_dentry(struct dentry *dentry, struct dentry *parent)
631 wimlib_assert(dentry_is_directory(parent));
632 dentry->parent = parent;
633 if (parent->children) {
634 /* Not an only child; link to siblings. */
635 dentry->next = parent->children;
636 dentry->prev = parent->children->prev;
637 dentry->next->prev = dentry;
638 dentry->prev->next = dentry;
640 /* Only child; link to parent. */
641 parent->children = dentry;
642 dentry->next = dentry;
643 dentry->prev = dentry;
648 /* Unlink a dentry from the directory tree.
650 * Note: This merely removes it from the in-memory tree structure. See
651 * remove_dentry() in mount.c for a function implemented on top of this one that
652 * frees the dentry and implements reference counting for the lookup table
654 void unlink_dentry(struct dentry *dentry)
656 if (dentry_is_root(dentry))
658 if (dentry_is_only_child(dentry)) {
659 dentry->parent->children = NULL;
661 if (dentry_is_first_sibling(dentry))
662 dentry->parent->children = dentry->next;
663 dentry->next->prev = dentry->prev;
664 dentry->prev->next = dentry->next;
669 /* Recalculates the length of @dentry based on its file name length and short
671 static inline void recalculate_dentry_size(struct dentry *dentry)
673 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
674 2 + dentry->short_name_len;
675 /* Must be multiple of 8. */
676 dentry->length = (dentry->length + 7) & ~7;
679 /* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
680 * and their lengths in the pointer arguments */
681 int get_names(char **name_utf16_ret, char **name_utf8_ret,
682 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
687 char *name_utf16, *name_utf8;
689 utf8_len = strlen(name);
691 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
694 return WIMLIB_ERR_NOMEM;
696 name_utf8 = MALLOC(utf8_len + 1);
699 return WIMLIB_ERR_NOMEM;
701 memcpy(name_utf8, name, utf8_len + 1);
702 FREE(*name_utf8_ret);
703 FREE(*name_utf16_ret);
704 *name_utf8_ret = name_utf8;
705 *name_utf16_ret = name_utf16;
706 *name_utf8_len_ret = utf8_len;
707 *name_utf16_len_ret = utf16_len;
711 /* Changes the name of a dentry to @new_name. Only changes the file_name and
712 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
713 * full_path_utf8 fields. Also recalculates its length. */
714 int change_dentry_name(struct dentry *dentry, const char *new_name)
718 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
719 &dentry->file_name_len, &dentry->file_name_utf8_len,
722 recalculate_dentry_size(dentry);
726 int change_ads_name(struct ads_entry *entry, const char *new_name)
728 return get_names(&entry->stream_name, &entry->stream_name_utf8,
729 &entry->stream_name_len,
730 &entry->stream_name_utf8_len,
734 /* Parameters for calculate_dentry_statistics(). */
735 struct image_statistics {
736 struct lookup_table *lookup_table;
740 u64 *hard_link_bytes;
743 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
745 struct image_statistics *stats;
746 struct lookup_table_entry *lte;
751 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
754 ++*stats->file_count;
756 if (dentry->resolved)
759 lte = __lookup_resource(stats->lookup_table, dentry->hash);
763 u64 size = lte->resource_entry.original_size;
764 *stats->total_bytes += size;
765 if (++lte->out_refcnt == 1)
766 *stats->hard_link_bytes += size;
768 if (i == dentry->num_ads)
770 lte = __lookup_resource(stats->lookup_table,
771 dentry->ads_entries[i].hash);
778 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
779 u64 *dir_count_ret, u64 *file_count_ret,
780 u64 *total_bytes_ret,
781 u64 *hard_link_bytes_ret)
783 struct image_statistics stats;
786 *total_bytes_ret = 0;
787 *hard_link_bytes_ret = 0;
788 stats.lookup_table = table;
789 stats.dir_count = dir_count_ret;
790 stats.file_count = file_count_ret;
791 stats.total_bytes = total_bytes_ret;
792 stats.hard_link_bytes = hard_link_bytes_ret;
793 for_lookup_table_entry(table, zero_out_refcnts, NULL);
794 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
797 static int read_ads_entries(const u8 *p, struct dentry *dentry,
800 u16 num_ads = dentry->num_ads;
801 struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
804 ERROR("Could not allocate memory for %"PRIu16" "
805 "alternate data stream entries", num_ads);
806 return WIMLIB_ERR_NOMEM;
808 DEBUG2("Reading %"PRIu16" alternate data streams "
809 "(remaining size = %"PRIu64")", num_ads, remaining_size);
811 for (u16 i = 0; i < num_ads; i++) {
812 struct ads_entry *cur_entry = &ads_entries[i];
815 const char *p_save = p;
816 /* Read the base stream entry, excluding the stream name. */
817 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
818 ERROR("Stream entries go past end of metadata resource");
819 ERROR("(remaining_size = %"PRIu64")", remaining_size);
820 ret = WIMLIB_ERR_INVALID_DENTRY;
821 goto out_free_ads_entries;
823 remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
825 p = get_u64(p, &length); /* ADS entry length */
827 DEBUG2("ADS length = %"PRIu64, length);
830 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
831 p = get_u16(p, &cur_entry->stream_name_len);
833 DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
835 cur_entry->stream_name = NULL;
836 cur_entry->stream_name_utf8 = NULL;
838 if (remaining_size < cur_entry->stream_name_len + 2) {
839 ERROR("Stream entries go past end of metadata resource");
840 ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
841 "= %"PRIu16" bytes", remaining_size,
842 cur_entry->stream_name_len);
843 ret = WIMLIB_ERR_INVALID_DENTRY;
844 goto out_free_ads_entries;
846 remaining_size -= cur_entry->stream_name_len + 2;
848 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
849 if (!cur_entry->stream_name) {
850 ret = WIMLIB_ERR_NOMEM;
851 goto out_free_ads_entries;
853 get_bytes(p, cur_entry->stream_name_len,
854 (u8*)cur_entry->stream_name);
855 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
856 cur_entry->stream_name_len,
858 cur_entry->stream_name_utf8_len = utf8_len;
860 if (!cur_entry->stream_name_utf8) {
861 ret = WIMLIB_ERR_NOMEM;
862 goto out_free_ads_entries;
864 p = p_save + ads_entry_length(cur_entry);
866 dentry->ads_entries = ads_entries;
868 out_free_ads_entries:
869 for (u16 i = 0; i < num_ads; i++) {
870 FREE(ads_entries[i].stream_name);
871 FREE(ads_entries[i].stream_name_utf8);
878 * Reads a directory entry from the metadata resource.
880 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
881 u64 offset, struct dentry *dentry)
886 char *file_name_utf8;
890 size_t file_name_utf8_len;
893 dentry_common_init(dentry);
895 /*Make sure the dentry really fits into the metadata resource.*/
896 if (offset + 8 > metadata_resource_len) {
897 ERROR("Directory entry starting at %"PRIu64" ends past the "
898 "end of the metadata resource (size %"PRIu64")",
899 offset, metadata_resource_len);
900 return WIMLIB_ERR_INVALID_DENTRY;
903 /* Before reading the whole entry, we need to read just the length.
904 * This is because an entry of length 8 (that is, just the length field)
905 * terminates the list of sibling directory entries. */
907 p = get_u64(&metadata_resource[offset], &dentry->length);
909 /* A zero length field (really a length of 8, since that's how big the
910 * directory entry is...) indicates that this is the end of directory
911 * dentry. We do not read it into memory as an actual dentry, so just
912 * return true in that case. */
913 if (dentry->length == 0)
916 if (offset + dentry->length >= metadata_resource_len) {
917 ERROR("Directory entry at offset %"PRIu64" and with size "
918 "%"PRIu64" ends past the end of the metadata resource "
920 offset, dentry->length, metadata_resource_len);
921 return WIMLIB_ERR_INVALID_DENTRY;
924 /* If it is a recognized length, read the rest of the directory entry.
925 * Note: The root directory entry has no name, and its length does not
926 * include the short name length field. */
927 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
928 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
930 return WIMLIB_ERR_INVALID_DENTRY;
933 p = get_u32(p, &dentry->attributes);
934 p = get_u32(p, (u32*)&dentry->security_id);
935 p = get_u64(p, &dentry->subdir_offset);
937 /* 2 unused fields */
938 p += 2 * sizeof(u64);
939 /*p = get_u64(p, &dentry->unused1);*/
940 /*p = get_u64(p, &dentry->unused2);*/
942 p = get_u64(p, &dentry->creation_time);
943 p = get_u64(p, &dentry->last_access_time);
944 p = get_u64(p, &dentry->last_write_time);
946 p = get_bytes(p, SHA1_HASH_SIZE, dentry->hash);
949 * I don't know what's going on here. It seems like M$ screwed up the
950 * reparse points, then put the fields in the same place and didn't
951 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
952 * have something to do with this, but it's not documented.
954 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
957 p = get_u32(p, &dentry->reparse_tag);
960 p = get_u32(p, &dentry->reparse_tag);
961 p = get_u64(p, &dentry->hard_link);
964 /* By the way, the reparse_reserved field does not actually exist (at
965 * least when the file is not a reparse point) */
968 p = get_u16(p, &dentry->num_ads);
970 p = get_u16(p, &short_name_len);
971 p = get_u16(p, &file_name_len);
973 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
976 if (dentry->length < calculated_size) {
977 ERROR("Unexpected end of directory entry! (Expected "
978 "%"PRIu64" bytes, got %"PRIu64" bytes. "
979 "short_name_len = %hu, file_name_len = %hu)",
980 calculated_size, dentry->length,
981 short_name_len, file_name_len);
982 return WIMLIB_ERR_INVALID_DENTRY;
985 /* Read the filename. */
986 file_name = MALLOC(file_name_len);
988 ERROR("Failed to allocate %hu bytes for dentry file name",
990 return WIMLIB_ERR_NOMEM;
992 p = get_bytes(p, file_name_len, file_name);
994 /* Convert filename to UTF-8. */
995 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
996 &file_name_utf8_len);
998 if (!file_name_utf8) {
999 ERROR("Failed to allocate memory to convert UTF-16 "
1000 "filename (%hu bytes) to UTF-8", file_name_len);
1001 ret = WIMLIB_ERR_NOMEM;
1002 goto out_free_file_name;
1005 /* Undocumented padding between file name and short name. This probably
1006 * is supposed to be a terminating null character. */
1009 /* Read the short filename. */
1010 short_name = MALLOC(short_name_len);
1012 ERROR("Failed to allocate %hu bytes for short filename",
1014 ret = WIMLIB_ERR_NOMEM;
1015 goto out_free_file_name_utf8;
1018 p = get_bytes(p, short_name_len, short_name);
1020 /* Some directory entries inexplicibly have a little over 70 bytes of
1021 * extra data. The exact amount of data seems to be 72 bytes, but it is
1022 * aligned on the next 8-byte boundary. Here's an example of the
1025 * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
1026 * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
1027 * 0000000000000000000000
1029 * Here's one interpretation of how the data is laid out.
1032 * u32 field1; (always 0x00000001)
1033 * u32 field2; (always 0x40000000)
1038 * u8 data[48]; (???)
1039 * u64 reserved1; (always 0)
1040 * u64 reserved2; (always 0)
1043 if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
1044 printf("%s: %lu / %lu (", file_name_utf8,
1045 calculated_size, dentry->length);
1046 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
1048 print_byte_field(p, dentry->length - calculated_size);
1053 if (dentry->num_ads != 0) {
1054 calculated_size = (calculated_size + 7) & ~7;
1055 if (calculated_size > metadata_resource_len - offset) {
1056 ERROR("Not enough space in metadata resource for "
1057 "alternate stream entries");
1058 ret = WIMLIB_ERR_INVALID_DENTRY;
1059 goto out_free_short_name;
1061 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1063 metadata_resource_len - offset - calculated_size);
1065 goto out_free_short_name;
1068 dentry->file_name = file_name;
1069 dentry->file_name_utf8 = file_name_utf8;
1070 dentry->short_name = short_name;
1071 dentry->file_name_len = file_name_len;
1072 dentry->file_name_utf8_len = file_name_utf8_len;
1073 dentry->short_name_len = short_name_len;
1075 out_free_short_name:
1077 out_free_file_name_utf8:
1078 FREE(file_name_utf8);
1085 * Writes a dentry to an output buffer.
1087 * @dentry: The dentry structure.
1088 * @p: The memory location to write the data to.
1089 * @return: Pointer to the byte after the last byte we wrote as part of the
1092 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1098 p = put_u64(p, dentry->length);
1099 p = put_u32(p, dentry->attributes);
1100 p = put_u32(p, dentry->security_id);
1101 p = put_u64(p, dentry->subdir_offset);
1102 p = put_u64(p, 0); /* unused1 */
1103 p = put_u64(p, 0); /* unused2 */
1104 p = put_u64(p, dentry->creation_time);
1105 p = put_u64(p, dentry->last_access_time);
1106 p = put_u64(p, dentry->last_write_time);
1107 if (dentry->resolved && dentry->lte)
1108 hash = dentry->lte->hash;
1110 hash = dentry->hash;
1111 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1112 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1113 p = put_zeroes(p, 4);
1114 p = put_u32(p, dentry->reparse_tag);
1115 p = put_zeroes(p, 4);
1118 p = put_u32(p, dentry->reparse_tag);
1119 if (dentry->link_group_list.next == &dentry->link_group_list)
1122 hard_link = dentry->hard_link;
1123 p = put_u64(p, hard_link);
1125 p = put_u16(p, dentry->num_ads);
1126 p = put_u16(p, dentry->short_name_len);
1127 p = put_u16(p, dentry->file_name_len);
1128 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1129 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1130 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1132 wimlib_assert(p - orig_p <= dentry->length);
1133 if (p - orig_p < dentry->length)
1134 p = put_zeroes(p, dentry->length - (p - orig_p));
1136 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1138 for (u16 i = 0; i < dentry->num_ads; i++) {
1139 p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
1140 p = put_u64(p, 0); /* Unused */
1141 if (dentry->resolved && dentry->ads_entries[i].lte)
1142 hash = dentry->ads_entries[i].lte->hash;
1144 hash = dentry->ads_entries[i].hash;
1145 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1146 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
1147 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
1148 (u8*)dentry->ads_entries[i].stream_name);
1149 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1154 /* Recursive function that writes a dentry tree rooted at @tree, not including
1155 * @tree itself, which has already been written, except in the case of the root
1156 * dentry, which is written right away, along with an end-of-directory entry. */
1157 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
1159 const struct dentry *child;
1161 if (dentry_is_root(tree)) {
1162 p = write_dentry(tree, p);
1164 /* write end of directory entry */
1167 /* Nothing to do for non-directories */
1168 if (!dentry_is_directory(tree))
1172 /* Write child dentries and end-of-directory entry. */
1173 child = tree->children;
1176 p = write_dentry(child, p);
1177 child = child->next;
1178 } while (child != tree->children);
1181 /* write end of directory entry */
1184 /* Recurse on children. */
1187 p = write_dentry_tree(child, p);
1188 child = child->next;
1189 } while (child != tree->children);
1194 /* Reads the children of a dentry, and all their children, ..., etc. from the
1195 * metadata resource and into the dentry tree.
1197 * @metadata_resource: An array that contains the uncompressed metadata
1198 * resource for the WIM file.
1199 * @metadata_resource_len: The length of @metadata_resource.
1200 * @dentry: A pointer to a struct dentry that is the root of the directory
1201 * tree and has already been read from the metadata resource. It
1202 * does not need to be the real root because this procedure is
1203 * called recursively.
1205 * @return: Zero on success, nonzero on failure.
1207 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1208 struct dentry *dentry)
1210 u64 cur_offset = dentry->subdir_offset;
1211 struct dentry *prev_child = NULL;
1212 struct dentry *first_child = NULL;
1213 struct dentry *child;
1214 struct dentry cur_child;
1217 /* If @dentry is a regular file, nothing more needs to be done for this
1219 if (cur_offset == 0)
1222 /* Find and read all the children of @dentry. */
1225 /* Read next child of @dentry into @cur_child. */
1226 ret = read_dentry(metadata_resource, metadata_resource_len,
1227 cur_offset, &cur_child);
1231 /* Check for end of directory. */
1232 if (cur_child.length == 0) {
1237 /* Not end of directory. Allocate this child permanently and
1238 * link it to the parent and previous child. */
1239 child = MALLOC(sizeof(struct dentry));
1241 ERROR("Failed to allocate %zu bytes for new dentry",
1242 sizeof(struct dentry));
1243 ret = WIMLIB_ERR_NOMEM;
1246 memcpy(child, &cur_child, sizeof(struct dentry));
1249 prev_child->next = child;
1250 child->prev = prev_child;
1252 first_child = child;
1255 child->parent = dentry;
1258 /* If there are children of this child, call this procedure
1260 if (child->subdir_offset != 0) {
1261 ret = read_dentry_tree(metadata_resource,
1262 metadata_resource_len, child);
1267 /* Advance to the offset of the next child. */
1268 cur_offset += dentry_total_length(child);
1271 /* Link last child to first one, and set parent's
1272 * children pointer to the first child. */
1274 prev_child->next = first_child;
1275 first_child->prev = prev_child;
1277 dentry->children = first_child;