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/.
38 #include "wimlib_internal.h"
41 #include "timestamp.h"
42 #include "lookup_table.h"
46 * Returns true if @dentry has the UTF-8 file name @name that has length
49 static bool dentry_has_name(const struct dentry *dentry, const char *name,
52 if (dentry->file_name_utf8_len != name_len)
54 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
57 /* Real length of a dentry, including the alternate data stream entries, which
58 * are not included in the dentry->length field... */
59 u64 dentry_total_length(const struct dentry *dentry)
61 u64 length = (dentry->length + 7) & ~7;
62 for (u16 i = 0 ; i < dentry->num_ads; i++)
63 length += ads_entry_length(&dentry->ads_entries[i]);
67 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
68 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
70 if (S_ISLNK(stbuf->st_mode)) {
71 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
72 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
73 } else if (S_ISDIR(stbuf->st_mode)) {
74 dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
76 dentry->attributes = FILE_ATTRIBUTE_NORMAL;
78 if (sizeof(ino_t) >= 8)
79 dentry->hard_link = (u64)stbuf->st_ino;
81 dentry->hard_link = (u64)stbuf->st_ino |
82 ((u64)stbuf->st_dev << (sizeof(ino_t) * 8));
84 dentry->creation_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
85 dentry->last_write_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
86 dentry->last_access_time = timespec_to_wim_timestamp(&stbuf->st_atim);
90 /* Makes all timestamp fields for the dentry be the current time. */
91 void dentry_update_all_timestamps(struct dentry *dentry)
93 u64 now = get_wim_timestamp();
94 dentry->creation_time = now;
95 dentry->last_access_time = now;
96 dentry->last_write_time = now;
99 struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
100 const char *stream_name)
102 size_t stream_name_len = strlen(stream_name);
105 for (u16 i = 0; i < dentry->num_ads; i++)
106 if (ads_entry_has_name(&dentry->ads_entries[i],
107 stream_name, stream_name_len))
108 return &dentry->ads_entries[i];
112 static void ads_entry_init(struct ads_entry *ads_entry)
114 memset(ads_entry, 0, sizeof(struct ads_entry));
115 INIT_LIST_HEAD(&ads_entry->lte_group_list.list);
116 ads_entry->lte_group_list.type = STREAM_TYPE_ADS;
119 /* Add an alternate stream entry to a dentry and return a pointer to it, or NULL
121 struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
124 struct ads_entry *ads_entries;
125 struct ads_entry *new_entry;
127 if (dentry->num_ads == 0xffff)
129 num_ads = dentry->num_ads + 1;
130 ads_entries = REALLOC(dentry->ads_entries,
131 num_ads * sizeof(struct ads_entry));
134 if (ads_entries != dentry->ads_entries) {
135 /* We moved the ADS entries. Adjust the stream lists. */
136 for (u16 i = 0; i < dentry->num_ads; i++) {
137 struct list_head *cur = &ads_entries[i].lte_group_list.list;
138 cur->prev->next = cur;
139 cur->next->prev = cur;
142 dentry->ads_entries = ads_entries;
144 new_entry = &ads_entries[num_ads - 1];
145 if (change_ads_name(new_entry, stream_name) != 0)
147 dentry->num_ads = num_ads;
148 ads_entry_init(new_entry);
152 void dentry_remove_ads(struct dentry *dentry, struct ads_entry *ads_entry)
157 wimlib_assert(dentry->num_ads);
158 idx = ads_entry - dentry->ads_entries;
159 wimlib_assert(idx < dentry->num_ads);
160 following = dentry->num_ads - idx - 1;
162 destroy_ads_entry(ads_entry);
163 memcpy(ads_entry, ads_entry + 1, following * sizeof(struct ads_entry));
165 /* We moved the ADS entries. Adjust the stream lists. */
166 for (u16 i = 0; i < following; i++) {
167 struct list_head *cur = &ads_entry[i].lte_group_list.list;
168 cur->prev->next = cur;
169 cur->next->prev = cur;
176 * Calls a function on all directory entries in a directory tree. It is called
177 * on a parent before its children.
179 int for_dentry_in_tree(struct dentry *root,
180 int (*visitor)(struct dentry*, void*), void *arg)
183 struct dentry *child;
185 ret = visitor(root, arg);
190 child = root->children;
196 ret = for_dentry_in_tree(child, visitor, arg);
200 } while (child != root->children);
205 * Like for_dentry_in_tree(), but the visitor function is always called on a
206 * dentry's children before on itself.
208 int for_dentry_in_tree_depth(struct dentry *root,
209 int (*visitor)(struct dentry*, void*), void *arg)
212 struct dentry *child;
215 child = root->children;
219 ret = for_dentry_in_tree_depth(child, visitor, arg);
223 } while (child != root->children);
225 return visitor(root, arg);
229 * Calculate the full path of @dentry, based on its parent's full path and on
230 * its UTF-8 file name.
232 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
236 if (dentry_is_root(dentry)) {
237 full_path = MALLOC(2);
244 char *parent_full_path;
245 u32 parent_full_path_len;
246 const struct dentry *parent = dentry->parent;
248 if (dentry_is_root(parent)) {
249 parent_full_path = "";
250 parent_full_path_len = 0;
252 parent_full_path = parent->full_path_utf8;
253 parent_full_path_len = parent->full_path_utf8_len;
256 full_path_len = parent_full_path_len + 1 +
257 dentry->file_name_utf8_len;
258 full_path = MALLOC(full_path_len + 1);
262 memcpy(full_path, parent_full_path, parent_full_path_len);
263 full_path[parent_full_path_len] = '/';
264 memcpy(full_path + parent_full_path_len + 1,
265 dentry->file_name_utf8,
266 dentry->file_name_utf8_len);
267 full_path[full_path_len] = '\0';
269 FREE(dentry->full_path_utf8);
270 dentry->full_path_utf8 = full_path;
271 dentry->full_path_utf8_len = full_path_len;
274 ERROR("Out of memory while calculating dentry full path");
275 return WIMLIB_ERR_NOMEM;
279 * Recursively calculates the subdir offsets for a directory tree.
281 * @dentry: The root of the directory tree.
282 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
283 * offset for @dentry.
285 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
287 struct dentry *child;
289 child = dentry->children;
290 dentry->subdir_offset = *subdir_offset_p;
293 /* Advance the subdir offset by the amount of space the children
294 * of this dentry take up. */
296 *subdir_offset_p += dentry_total_length(child);
298 } while (child != dentry->children);
300 /* End-of-directory dentry on disk. */
301 *subdir_offset_p += 8;
303 /* Recursively call calculate_subdir_offsets() on all the
306 calculate_subdir_offsets(child, subdir_offset_p);
308 } while (child != dentry->children);
310 /* On disk, childless directories have a valid subdir_offset
311 * that points to an 8-byte end-of-directory dentry. Regular
312 * files have a subdir_offset of 0. */
313 if (dentry_is_directory(dentry))
314 *subdir_offset_p += 8;
316 dentry->subdir_offset = 0;
321 /* Returns the child of @dentry that has the file name @name.
322 * Returns NULL if no child has the name. */
323 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
326 struct dentry *child;
329 child = dentry->children;
331 name_len = strlen(name);
333 if (dentry_has_name(child, name, name_len))
336 } while (child != dentry->children);
341 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
342 * @cur_dir. Returns NULL if no dentry having the path is found. */
343 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
345 struct dentry *child;
347 const char *new_path;
352 child = cur_dir->children;
354 new_path = path_next_part(path, &base_len);
356 if (dentry_has_name(child, path, base_len))
357 return get_dentry_relative_path(child, new_path);
359 } while (child != cur_dir->children);
364 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
366 struct dentry *get_dentry(WIMStruct *w, const char *path)
368 struct dentry *root = wim_root_dentry(w);
371 return get_dentry_relative_path(root, path);
374 /* Returns the parent directory for the @path. */
375 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
377 size_t path_len = strlen(path);
378 char buf[path_len + 1];
380 memcpy(buf, path, path_len + 1);
382 to_parent_name(buf, path_len);
384 return get_dentry(w, buf);
387 /* Prints the full path of a dentry. */
388 int print_dentry_full_path(struct dentry *dentry, void *ignore)
390 if (dentry->full_path_utf8)
391 puts(dentry->full_path_utf8);
395 struct file_attr_flag {
399 struct file_attr_flag file_attr_flags[] = {
400 {FILE_ATTRIBUTE_READONLY, "READONLY"},
401 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
402 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
403 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
404 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
405 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
406 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
407 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
408 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
409 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
410 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
411 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
412 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
413 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
414 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
417 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
418 * NULL if the resource entry for the dentry is not to be printed. */
419 int print_dentry(struct dentry *dentry, void *lookup_table)
423 printf("[DENTRY]\n");
424 printf("Length = %"PRIu64"\n", dentry->length);
425 printf("Attributes = 0x%x\n", dentry->attributes);
426 for (unsigned i = 0; i < ARRAY_LEN(file_attr_flags); i++)
427 if (file_attr_flags[i].flag & dentry->attributes)
428 printf(" FILE_ATTRIBUTE_%s is set\n",
429 file_attr_flags[i].name);
430 printf("Security ID = %d\n", dentry->security_id);
431 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
433 printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);
434 printf("Unused2 = %"PRIu64"\n", dentry->unused2);
437 printf("Creation Time = 0x%"PRIx64"\n");
438 printf("Last Access Time = 0x%"PRIx64"\n");
439 printf("Last Write Time = 0x%"PRIx64"\n");
442 time_t creat_time = wim_timestamp_to_unix(dentry->creation_time);
443 time_t access_time = wim_timestamp_to_unix(dentry->last_access_time);
444 time_t mod_time = wim_timestamp_to_unix(dentry->last_write_time);
445 printf("Creation Time = %s", asctime(localtime(&creat_time)));
446 printf("Last Access Time = %s", asctime(localtime(&access_time)));
447 printf("Last Write Time = %s", asctime(localtime(&mod_time)));
449 hash = dentry_stream_hash(dentry, 0);
455 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
456 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
457 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
458 printf("Filename = \"");
459 print_string(dentry->file_name, dentry->file_name_len);
461 printf("Filename Length = %hu\n", dentry->file_name_len);
462 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
463 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
464 printf("Short Name = \"");
465 print_string(dentry->short_name, dentry->short_name_len);
467 printf("Short Name Length = %hu\n", dentry->short_name_len);
468 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
469 print_lookup_table_entry(dentry_stream_lte(dentry, 0, lookup_table));
470 for (u16 i = 0; i < dentry->num_ads; i++) {
471 printf("[Alternate Stream Entry %u]\n", i);
472 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
473 printf("Name Length (UTF-16) = %u\n",
474 dentry->ads_entries[i].stream_name_len);
475 hash = dentry_stream_hash(dentry, i + 1);
481 print_lookup_table_entry(dentry_stream_lte(dentry, i + 1,
487 static inline void dentry_common_init(struct dentry *dentry)
489 memset(dentry, 0, sizeof(struct dentry));
491 dentry->security_id = -1;
492 dentry->ads_entries_status = ADS_ENTRIES_DEFAULT;
493 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
497 * Creates an unlinked directory entry.
499 * @name: The base name of the new dentry.
500 * @return: A pointer to the new dentry, or NULL if out of memory.
502 struct dentry *new_dentry(const char *name)
504 struct dentry *dentry;
506 dentry = MALLOC(sizeof(struct dentry));
510 dentry_common_init(dentry);
511 if (change_dentry_name(dentry, name) != 0)
514 dentry_update_all_timestamps(dentry);
515 dentry->next = dentry;
516 dentry->prev = dentry;
517 dentry->parent = dentry;
518 INIT_LIST_HEAD(&dentry->link_group_list);
522 ERROR("Failed to allocate new dentry");
526 void dentry_free_ads_entries(struct dentry *dentry)
528 for (u16 i = 0; i < dentry->num_ads; i++)
529 destroy_ads_entry(&dentry->ads_entries[i]);
530 FREE(dentry->ads_entries);
531 dentry->ads_entries = NULL;
535 static void __destroy_dentry(struct dentry *dentry)
537 FREE(dentry->file_name);
538 FREE(dentry->file_name_utf8);
539 FREE(dentry->short_name);
540 FREE(dentry->full_path_utf8);
541 FREE(dentry->extracted_file);
544 void free_dentry(struct dentry *dentry)
546 wimlib_assert(dentry);
547 __destroy_dentry(dentry);
548 if (dentry->ads_entries_status != ADS_ENTRIES_USER)
549 dentry_free_ads_entries(dentry);
553 /* Like free_dentry(), but assigns a new ADS entries owner if this dentry was
554 * the previous owner, and also deletes the dentry from its link_group_list */
555 void put_dentry(struct dentry *dentry)
557 if (dentry->ads_entries_status == ADS_ENTRIES_OWNER) {
558 struct dentry *new_owner;
559 list_for_each_entry(new_owner, &dentry->link_group_list,
562 if (new_owner->ads_entries_status == ADS_ENTRIES_USER) {
563 new_owner->ads_entries_status = ADS_ENTRIES_OWNER;
567 dentry->ads_entries_status = ADS_ENTRIES_USER;
569 struct list_head *next;
570 list_del(&dentry->link_group_list);
578 * - memory for file names is not cloned
579 * - next, prev, and children pointers and not touched
580 * - stream entries are not cloned.
582 struct dentry *clone_dentry(struct dentry *old)
584 struct dentry *new = MALLOC(sizeof(struct dentry));
587 memcpy(new, old, sizeof(struct dentry));
588 new->file_name = NULL;
589 new->file_name_len = 0;
590 new->file_name_utf8 = NULL;
591 new->file_name_utf8_len = 0;
592 new->short_name = NULL;
593 new->short_name_len = 0;
598 * This function is passed as an argument to for_dentry_in_tree_depth() in order
599 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
601 static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
603 struct lookup_table *lookup_table = __lookup_table;
605 struct lookup_table_entry *lte;
606 if (dentry->resolved)
609 lte = __lookup_resource(lookup_table, dentry->hash);
610 lte_decrement_refcnt(lte, lookup_table);
613 wimlib_assert(dentry->refcnt != 0);
614 if (--dentry->refcnt == 0)
620 * Unlinks and frees a dentry tree.
622 * @root: The root of the tree.
623 * @lookup_table: The lookup table for dentries. If non-NULL, the
624 * reference counts in the lookup table for the lookup
625 * table entries corresponding to the dentries will be
628 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
630 if (!root || !root->parent)
632 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
635 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
642 * Links a dentry into the directory tree.
644 * @dentry: The dentry to link.
645 * @parent: The dentry that will be the parent of @dentry.
647 void link_dentry(struct dentry *dentry, struct dentry *parent)
649 wimlib_assert(dentry_is_directory(parent));
650 dentry->parent = parent;
651 if (parent->children) {
652 /* Not an only child; link to siblings. */
653 dentry->next = parent->children;
654 dentry->prev = parent->children->prev;
655 dentry->next->prev = dentry;
656 dentry->prev->next = dentry;
658 /* Only child; link to parent. */
659 parent->children = dentry;
660 dentry->next = dentry;
661 dentry->prev = dentry;
666 /* Unlink a dentry from the directory tree.
668 * Note: This merely removes it from the in-memory tree structure. See
669 * remove_dentry() in mount.c for a function implemented on top of this one that
670 * frees the dentry and implements reference counting for the lookup table
672 void unlink_dentry(struct dentry *dentry)
674 if (dentry_is_root(dentry))
676 if (dentry_is_only_child(dentry)) {
677 dentry->parent->children = NULL;
679 if (dentry_is_first_sibling(dentry))
680 dentry->parent->children = dentry->next;
681 dentry->next->prev = dentry->prev;
682 dentry->prev->next = dentry->next;
687 /* Recalculates the length of @dentry based on its file name length and short
689 static inline void recalculate_dentry_size(struct dentry *dentry)
691 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
692 2 + dentry->short_name_len;
693 /* Must be multiple of 8. */
694 dentry->length = (dentry->length + 7) & ~7;
697 /* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
698 * and their lengths in the pointer arguments */
699 int get_names(char **name_utf16_ret, char **name_utf8_ret,
700 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
705 char *name_utf16, *name_utf8;
707 utf8_len = strlen(name);
709 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
712 return WIMLIB_ERR_NOMEM;
714 name_utf8 = MALLOC(utf8_len + 1);
717 return WIMLIB_ERR_NOMEM;
719 memcpy(name_utf8, name, utf8_len + 1);
720 FREE(*name_utf8_ret);
721 FREE(*name_utf16_ret);
722 *name_utf8_ret = name_utf8;
723 *name_utf16_ret = name_utf16;
724 *name_utf8_len_ret = utf8_len;
725 *name_utf16_len_ret = utf16_len;
729 /* Changes the name of a dentry to @new_name. Only changes the file_name and
730 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
731 * full_path_utf8 fields. Also recalculates its length. */
732 int change_dentry_name(struct dentry *dentry, const char *new_name)
736 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
737 &dentry->file_name_len, &dentry->file_name_utf8_len,
740 recalculate_dentry_size(dentry);
744 int change_ads_name(struct ads_entry *entry, const char *new_name)
746 return get_names(&entry->stream_name, &entry->stream_name_utf8,
747 &entry->stream_name_len,
748 &entry->stream_name_utf8_len,
752 /* Parameters for calculate_dentry_statistics(). */
753 struct image_statistics {
754 struct lookup_table *lookup_table;
758 u64 *hard_link_bytes;
761 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
763 struct image_statistics *stats;
764 struct lookup_table_entry *lte;
769 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
772 ++*stats->file_count;
774 if (dentry->resolved)
777 lte = __lookup_resource(stats->lookup_table, dentry->hash);
781 u64 size = lte->resource_entry.original_size;
782 *stats->total_bytes += size;
783 if (++lte->out_refcnt == 1)
784 *stats->hard_link_bytes += size;
786 if (i == dentry->num_ads)
788 lte = __lookup_resource(stats->lookup_table,
789 dentry->ads_entries[i].hash);
796 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
797 u64 *dir_count_ret, u64 *file_count_ret,
798 u64 *total_bytes_ret,
799 u64 *hard_link_bytes_ret)
801 struct image_statistics stats;
804 *total_bytes_ret = 0;
805 *hard_link_bytes_ret = 0;
806 stats.lookup_table = table;
807 stats.dir_count = dir_count_ret;
808 stats.file_count = file_count_ret;
809 stats.total_bytes = total_bytes_ret;
810 stats.hard_link_bytes = hard_link_bytes_ret;
811 for_lookup_table_entry(table, zero_out_refcnts, NULL);
812 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
815 static int read_ads_entries(const u8 *p, struct dentry *dentry,
818 u16 num_ads = dentry->num_ads;
819 struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
822 ERROR("Could not allocate memory for %"PRIu16" "
823 "alternate data stream entries", num_ads);
824 return WIMLIB_ERR_NOMEM;
826 DEBUG2("Reading %"PRIu16" alternate data streams "
827 "(remaining size = %"PRIu64")", num_ads, remaining_size);
829 for (u16 i = 0; i < num_ads; i++) {
830 struct ads_entry *cur_entry = &ads_entries[i];
833 const char *p_save = p;
834 /* Read the base stream entry, excluding the stream name. */
835 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
836 ERROR("Stream entries go past end of metadata resource");
837 ERROR("(remaining_size = %"PRIu64")", remaining_size);
838 ret = WIMLIB_ERR_INVALID_DENTRY;
839 goto out_free_ads_entries;
841 remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
843 p = get_u64(p, &length); /* ADS entry length */
845 DEBUG2("ADS length = %"PRIu64, length);
848 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
849 p = get_u16(p, &cur_entry->stream_name_len);
851 DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
853 cur_entry->stream_name = NULL;
854 cur_entry->stream_name_utf8 = NULL;
856 if (remaining_size < cur_entry->stream_name_len + 2) {
857 ERROR("Stream entries go past end of metadata resource");
858 ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
859 "= %"PRIu16" bytes", remaining_size,
860 cur_entry->stream_name_len);
861 ret = WIMLIB_ERR_INVALID_DENTRY;
862 goto out_free_ads_entries;
864 remaining_size -= cur_entry->stream_name_len + 2;
866 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
867 if (!cur_entry->stream_name) {
868 ret = WIMLIB_ERR_NOMEM;
869 goto out_free_ads_entries;
871 get_bytes(p, cur_entry->stream_name_len,
872 (u8*)cur_entry->stream_name);
873 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
874 cur_entry->stream_name_len,
876 cur_entry->stream_name_utf8_len = utf8_len;
878 if (!cur_entry->stream_name_utf8) {
879 ret = WIMLIB_ERR_NOMEM;
880 goto out_free_ads_entries;
882 p = p_save + ads_entry_length(cur_entry);
884 dentry->ads_entries = ads_entries;
886 out_free_ads_entries:
887 for (u16 i = 0; i < num_ads; i++) {
888 FREE(ads_entries[i].stream_name);
889 FREE(ads_entries[i].stream_name_utf8);
896 * Reads a directory entry from the metadata resource.
898 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
899 u64 offset, struct dentry *dentry)
904 char *file_name_utf8;
908 size_t file_name_utf8_len;
911 dentry_common_init(dentry);
913 /*Make sure the dentry really fits into the metadata resource.*/
914 if (offset + 8 > metadata_resource_len) {
915 ERROR("Directory entry starting at %"PRIu64" ends past the "
916 "end of the metadata resource (size %"PRIu64")",
917 offset, metadata_resource_len);
918 return WIMLIB_ERR_INVALID_DENTRY;
921 /* Before reading the whole entry, we need to read just the length.
922 * This is because an entry of length 8 (that is, just the length field)
923 * terminates the list of sibling directory entries. */
925 p = get_u64(&metadata_resource[offset], &dentry->length);
927 /* A zero length field (really a length of 8, since that's how big the
928 * directory entry is...) indicates that this is the end of directory
929 * dentry. We do not read it into memory as an actual dentry, so just
930 * return true in that case. */
931 if (dentry->length == 0)
934 if (offset + dentry->length >= metadata_resource_len) {
935 ERROR("Directory entry at offset %"PRIu64" and with size "
936 "%"PRIu64" ends past the end of the metadata resource "
938 offset, dentry->length, metadata_resource_len);
939 return WIMLIB_ERR_INVALID_DENTRY;
942 /* If it is a recognized length, read the rest of the directory entry.
943 * Note: The root directory entry has no name, and its length does not
944 * include the short name length field. */
945 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
946 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
948 return WIMLIB_ERR_INVALID_DENTRY;
951 p = get_u32(p, &dentry->attributes);
952 p = get_u32(p, (u32*)&dentry->security_id);
953 p = get_u64(p, &dentry->subdir_offset);
955 /* 2 unused fields */
956 p += 2 * sizeof(u64);
957 /*p = get_u64(p, &dentry->unused1);*/
958 /*p = get_u64(p, &dentry->unused2);*/
960 p = get_u64(p, &dentry->creation_time);
961 p = get_u64(p, &dentry->last_access_time);
962 p = get_u64(p, &dentry->last_write_time);
964 p = get_bytes(p, SHA1_HASH_SIZE, dentry->hash);
967 * I don't know what's going on here. It seems like M$ screwed up the
968 * reparse points, then put the fields in the same place and didn't
969 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
970 * have something to do with this, but it's not documented.
972 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
975 p = get_u32(p, &dentry->reparse_tag);
978 p = get_u32(p, &dentry->reparse_tag);
979 p = get_u64(p, &dentry->hard_link);
982 /* By the way, the reparse_reserved field does not actually exist (at
983 * least when the file is not a reparse point) */
986 p = get_u16(p, &dentry->num_ads);
988 p = get_u16(p, &short_name_len);
989 p = get_u16(p, &file_name_len);
991 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
994 if (dentry->length < calculated_size) {
995 ERROR("Unexpected end of directory entry! (Expected "
996 "%"PRIu64" bytes, got %"PRIu64" bytes. "
997 "short_name_len = %hu, file_name_len = %hu)",
998 calculated_size, dentry->length,
999 short_name_len, file_name_len);
1000 return WIMLIB_ERR_INVALID_DENTRY;
1003 /* Read the filename. */
1004 file_name = MALLOC(file_name_len);
1006 ERROR("Failed to allocate %hu bytes for dentry file name",
1008 return WIMLIB_ERR_NOMEM;
1010 p = get_bytes(p, file_name_len, file_name);
1012 /* Convert filename to UTF-8. */
1013 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1014 &file_name_utf8_len);
1016 if (!file_name_utf8) {
1017 ERROR("Failed to allocate memory to convert UTF-16 "
1018 "filename (%hu bytes) to UTF-8", file_name_len);
1019 ret = WIMLIB_ERR_NOMEM;
1020 goto out_free_file_name;
1023 /* Undocumented padding between file name and short name. This probably
1024 * is supposed to be a terminating null character. */
1027 /* Read the short filename. */
1028 short_name = MALLOC(short_name_len);
1030 ERROR("Failed to allocate %hu bytes for short filename",
1032 ret = WIMLIB_ERR_NOMEM;
1033 goto out_free_file_name_utf8;
1036 p = get_bytes(p, short_name_len, short_name);
1038 /* Some directory entries inexplicibly have a little over 70 bytes of
1039 * extra data. The exact amount of data seems to be 72 bytes, but it is
1040 * aligned on the next 8-byte boundary. Here's an example of the
1043 * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
1044 * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
1045 * 0000000000000000000000
1047 * Here's one interpretation of how the data is laid out.
1050 * u32 field1; (always 0x00000001)
1051 * u32 field2; (always 0x40000000)
1056 * u8 data[48]; (???)
1057 * u64 reserved1; (always 0)
1058 * u64 reserved2; (always 0)
1061 if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
1062 printf("%s: %lu / %lu (", file_name_utf8,
1063 calculated_size, dentry->length);
1064 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
1066 print_byte_field(p, dentry->length - calculated_size);
1071 if (dentry->num_ads != 0) {
1072 calculated_size = (calculated_size + 7) & ~7;
1073 if (calculated_size > metadata_resource_len - offset) {
1074 ERROR("Not enough space in metadata resource for "
1075 "alternate stream entries");
1076 ret = WIMLIB_ERR_INVALID_DENTRY;
1077 goto out_free_short_name;
1079 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1081 metadata_resource_len - offset - calculated_size);
1083 goto out_free_short_name;
1086 dentry->file_name = file_name;
1087 dentry->file_name_utf8 = file_name_utf8;
1088 dentry->short_name = short_name;
1089 dentry->file_name_len = file_name_len;
1090 dentry->file_name_utf8_len = file_name_utf8_len;
1091 dentry->short_name_len = short_name_len;
1093 out_free_short_name:
1095 out_free_file_name_utf8:
1096 FREE(file_name_utf8);
1103 * Writes a dentry to an output buffer.
1105 * @dentry: The dentry structure.
1106 * @p: The memory location to write the data to.
1107 * @return: Pointer to the byte after the last byte we wrote as part of the
1110 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1116 p = put_u64(p, dentry->length);
1117 p = put_u32(p, dentry->attributes);
1118 p = put_u32(p, dentry->security_id);
1119 p = put_u64(p, dentry->subdir_offset);
1120 p = put_u64(p, 0); /* unused1 */
1121 p = put_u64(p, 0); /* unused2 */
1122 p = put_u64(p, dentry->creation_time);
1123 p = put_u64(p, dentry->last_access_time);
1124 p = put_u64(p, dentry->last_write_time);
1125 if (dentry->resolved && dentry->lte)
1126 hash = dentry->lte->hash;
1128 hash = dentry->hash;
1129 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1130 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1131 p = put_zeroes(p, 4);
1132 p = put_u32(p, dentry->reparse_tag);
1133 p = put_zeroes(p, 4);
1136 p = put_u32(p, dentry->reparse_tag);
1137 if (dentry->link_group_list.next == &dentry->link_group_list)
1140 hard_link = dentry->hard_link;
1141 p = put_u64(p, hard_link);
1143 p = put_u16(p, dentry->num_ads);
1144 p = put_u16(p, dentry->short_name_len);
1145 p = put_u16(p, dentry->file_name_len);
1146 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1147 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1148 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1150 wimlib_assert(p - orig_p <= dentry->length);
1151 if (p - orig_p < dentry->length)
1152 p = put_zeroes(p, dentry->length - (p - orig_p));
1154 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1156 for (u16 i = 0; i < dentry->num_ads; i++) {
1157 p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
1158 p = put_u64(p, 0); /* Unused */
1159 if (dentry->resolved && dentry->ads_entries[i].lte)
1160 hash = dentry->ads_entries[i].lte->hash;
1162 hash = dentry->ads_entries[i].hash;
1163 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1164 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
1165 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
1166 (u8*)dentry->ads_entries[i].stream_name);
1167 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1172 /* Recursive function that writes a dentry tree rooted at @tree, not including
1173 * @tree itself, which has already been written, except in the case of the root
1174 * dentry, which is written right away, along with an end-of-directory entry. */
1175 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
1177 const struct dentry *child;
1179 if (dentry_is_root(tree)) {
1180 p = write_dentry(tree, p);
1182 /* write end of directory entry */
1185 /* Nothing to do for non-directories */
1186 if (!dentry_is_directory(tree))
1190 /* Write child dentries and end-of-directory entry. */
1191 child = tree->children;
1194 p = write_dentry(child, p);
1195 child = child->next;
1196 } while (child != tree->children);
1199 /* write end of directory entry */
1202 /* Recurse on children. */
1205 p = write_dentry_tree(child, p);
1206 child = child->next;
1207 } while (child != tree->children);
1212 /* Reads the children of a dentry, and all their children, ..., etc. from the
1213 * metadata resource and into the dentry tree.
1215 * @metadata_resource: An array that contains the uncompressed metadata
1216 * resource for the WIM file.
1217 * @metadata_resource_len: The length of @metadata_resource.
1218 * @dentry: A pointer to a struct dentry that is the root of the directory
1219 * tree and has already been read from the metadata resource. It
1220 * does not need to be the real root because this procedure is
1221 * called recursively.
1223 * @return: Zero on success, nonzero on failure.
1225 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1226 struct dentry *dentry)
1228 u64 cur_offset = dentry->subdir_offset;
1229 struct dentry *prev_child = NULL;
1230 struct dentry *first_child = NULL;
1231 struct dentry *child;
1232 struct dentry cur_child;
1235 /* If @dentry is a regular file, nothing more needs to be done for this
1237 if (cur_offset == 0)
1240 /* Find and read all the children of @dentry. */
1243 /* Read next child of @dentry into @cur_child. */
1244 ret = read_dentry(metadata_resource, metadata_resource_len,
1245 cur_offset, &cur_child);
1249 /* Check for end of directory. */
1250 if (cur_child.length == 0) {
1255 /* Not end of directory. Allocate this child permanently and
1256 * link it to the parent and previous child. */
1257 child = MALLOC(sizeof(struct dentry));
1259 ERROR("Failed to allocate %zu bytes for new dentry",
1260 sizeof(struct dentry));
1261 ret = WIMLIB_ERR_NOMEM;
1264 memcpy(child, &cur_child, sizeof(struct dentry));
1267 prev_child->next = child;
1268 child->prev = prev_child;
1270 first_child = child;
1273 child->parent = dentry;
1276 /* If there are children of this child, call this procedure
1278 if (child->subdir_offset != 0) {
1279 ret = read_dentry_tree(metadata_resource,
1280 metadata_resource_len, child);
1285 /* Advance to the offset of the next child. */
1286 cur_offset += dentry_total_length(child);
1289 /* Link last child to first one, and set parent's
1290 * children pointer to the first child. */
1292 prev_child->next = first_child;
1293 first_child->prev = prev_child;
1295 dentry->children = first_child;