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_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 /* Add an alternate stream entry to a dentry and return a pointer to it, or NULL
108 struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
110 u16 num_ads = dentry->num_ads + 1;
111 struct ads_entry *ads_entries;
112 struct ads_entry *new_entry;
113 if (num_ads == 0xffff)
115 ads_entries = MALLOC(num_ads * sizeof(struct ads_entry));
119 new_entry = &ads_entries[num_ads - 1];
120 if (change_ads_name(new_entry, stream_name) != 0) {
125 memcpy(ads_entries, dentry->ads_entries,
126 (num_ads - 1) * sizeof(struct ads_entry));
127 FREE(dentry->ads_entries);
128 dentry->ads_entries = ads_entries;
129 dentry->num_ads = num_ads;
130 return memset(new_entry, 0, sizeof(struct ads_entry));
133 void dentry_remove_ads(struct dentry *dentry, struct ads_entry *ads_entry)
135 u16 idx = ads_entry - dentry->ads_entries;
136 u16 following = dentry->num_ads - idx - 1;
138 destroy_ads_entry(ads_entry);
139 memcpy(ads_entry, ads_entry + 1, following * sizeof(struct ads_entry));
141 /* We moved the ADS entries. Adjust the stream lists. */
142 for (u16 i = 0; i < following; i++) {
143 struct list_head *cur = &ads_entry[i].lte_group_list.list;
144 cur->prev->next = cur;
145 cur->next->prev = cur;
152 * Calls a function on all directory entries in a directory tree. It is called
153 * on a parent before its children.
155 int for_dentry_in_tree(struct dentry *root,
156 int (*visitor)(struct dentry*, void*), void *arg)
159 struct dentry *child;
161 ret = visitor(root, arg);
166 child = root->children;
172 ret = for_dentry_in_tree(child, visitor, arg);
176 } while (child != root->children);
181 * Like for_dentry_in_tree(), but the visitor function is always called on a
182 * dentry's children before on itself.
184 int for_dentry_in_tree_depth(struct dentry *root,
185 int (*visitor)(struct dentry*, void*), void *arg)
188 struct dentry *child;
191 child = root->children;
195 ret = for_dentry_in_tree_depth(child, visitor, arg);
199 } while (child != root->children);
201 return visitor(root, arg);
205 * Calculate the full path of @dentry, based on its parent's full path and on
206 * its UTF-8 file name.
208 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
212 if (dentry_is_root(dentry)) {
213 full_path = MALLOC(2);
220 char *parent_full_path;
221 u32 parent_full_path_len;
222 const struct dentry *parent = dentry->parent;
224 if (dentry_is_root(parent)) {
225 parent_full_path = "";
226 parent_full_path_len = 0;
228 parent_full_path = parent->full_path_utf8;
229 parent_full_path_len = parent->full_path_utf8_len;
232 full_path_len = parent_full_path_len + 1 +
233 dentry->file_name_utf8_len;
234 full_path = MALLOC(full_path_len + 1);
238 memcpy(full_path, parent_full_path, parent_full_path_len);
239 full_path[parent_full_path_len] = '/';
240 memcpy(full_path + parent_full_path_len + 1,
241 dentry->file_name_utf8,
242 dentry->file_name_utf8_len);
243 full_path[full_path_len] = '\0';
245 FREE(dentry->full_path_utf8);
246 dentry->full_path_utf8 = full_path;
247 dentry->full_path_utf8_len = full_path_len;
250 ERROR("Out of memory while calculating dentry full path");
251 return WIMLIB_ERR_NOMEM;
255 * Recursively calculates the subdir offsets for a directory tree.
257 * @dentry: The root of the directory tree.
258 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
259 * offset for @dentry.
261 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
263 struct dentry *child;
265 child = dentry->children;
266 dentry->subdir_offset = *subdir_offset_p;
269 /* Advance the subdir offset by the amount of space the children
270 * of this dentry take up. */
272 *subdir_offset_p += dentry_total_length(child);
274 } while (child != dentry->children);
276 /* End-of-directory dentry on disk. */
277 *subdir_offset_p += 8;
279 /* Recursively call calculate_subdir_offsets() on all the
282 calculate_subdir_offsets(child, subdir_offset_p);
284 } while (child != dentry->children);
286 /* On disk, childless directories have a valid subdir_offset
287 * that points to an 8-byte end-of-directory dentry. Regular
288 * files have a subdir_offset of 0. */
289 if (dentry_is_directory(dentry))
290 *subdir_offset_p += 8;
292 dentry->subdir_offset = 0;
297 /* Returns the child of @dentry that has the file name @name.
298 * Returns NULL if no child has the name. */
299 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
302 struct dentry *child;
305 child = dentry->children;
307 name_len = strlen(name);
309 if (dentry_has_name(child, name, name_len))
312 } while (child != dentry->children);
317 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
318 * @cur_dir. Returns NULL if no dentry having the path is found. */
319 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
321 struct dentry *child;
323 const char *new_path;
328 child = cur_dir->children;
330 new_path = path_next_part(path, &base_len);
332 if (dentry_has_name(child, path, base_len))
333 return get_dentry_relative_path(child, new_path);
335 } while (child != cur_dir->children);
340 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
342 struct dentry *get_dentry(WIMStruct *w, const char *path)
344 struct dentry *root = wim_root_dentry(w);
347 return get_dentry_relative_path(root, path);
350 /* Returns the parent directory for the @path. */
351 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
353 size_t path_len = strlen(path);
354 char buf[path_len + 1];
356 memcpy(buf, path, path_len + 1);
358 to_parent_name(buf, path_len);
360 return get_dentry(w, buf);
363 /* Prints the full path of a dentry. */
364 int print_dentry_full_path(struct dentry *dentry, void *ignore)
366 if (dentry->full_path_utf8)
367 puts(dentry->full_path_utf8);
371 struct file_attr_flag {
375 struct file_attr_flag file_attr_flags[] = {
376 {FILE_ATTRIBUTE_READONLY, "READONLY"},
377 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
378 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
379 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
380 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
381 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
382 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
383 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
384 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
385 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
386 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
387 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
388 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
389 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
390 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
393 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
394 * NULL if the resource entry for the dentry is not to be printed. */
395 int print_dentry(struct dentry *dentry, void *lookup_table)
397 struct lookup_table_entry *lte;
400 printf("[DENTRY]\n");
401 printf("Length = %"PRIu64"\n", dentry->length);
402 printf("Attributes = 0x%x\n", dentry->attributes);
403 for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
404 if (file_attr_flags[i].flag & dentry->attributes)
405 printf(" FILE_ATTRIBUTE_%s is set\n",
406 file_attr_flags[i].name);
407 printf("Security ID = %d\n", dentry->security_id);
408 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
409 /*printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);*/
410 /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
411 printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
412 printf("Last Access Time = 0x%"PRIx64"\n", dentry->last_access_time);
413 printf("Last Write Time = 0x%"PRIx64"\n", dentry->last_write_time);
415 print_hash(dentry->hash);
417 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
418 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
419 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
420 printf("Filename = \"");
421 print_string(dentry->file_name, dentry->file_name_len);
423 printf("Filename Length = %hu\n", dentry->file_name_len);
424 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
425 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
426 printf("Short Name = \"");
427 print_string(dentry->short_name, dentry->short_name_len);
429 printf("Short Name Length = %hu\n", dentry->short_name_len);
430 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
431 if (lookup_table && (lte = __lookup_resource(lookup_table, dentry->hash)))
432 print_lookup_table_entry(lte, NULL);
435 for (u16 i = 0; i < dentry->num_ads; i++) {
436 printf("[Alternate Stream Entry %u]\n", i);
437 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
438 printf("Name Length (UTF-16) = %u\n",
439 dentry->ads_entries[i].stream_name_len);
441 print_hash(dentry->ads_entries[i].hash);
443 (lte = __lookup_resource(lookup_table,
444 dentry->ads_entries[i].hash)))
446 print_lookup_table_entry(lte, NULL);
454 static inline void dentry_common_init(struct dentry *dentry)
456 memset(dentry, 0, sizeof(struct dentry));
458 dentry->security_id = -1;
459 dentry->link_group_master_status = GROUP_SLAVE;
463 * Creates an unlinked directory entry.
465 * @name: The base name of the new dentry.
466 * @return: A pointer to the new dentry, or NULL if out of memory.
468 struct dentry *new_dentry(const char *name)
470 struct dentry *dentry;
472 dentry = MALLOC(sizeof(struct dentry));
476 dentry_common_init(dentry);
477 if (change_dentry_name(dentry, name) != 0)
480 dentry_update_all_timestamps(dentry);
481 dentry->next = dentry;
482 dentry->prev = dentry;
483 dentry->parent = dentry;
484 INIT_LIST_HEAD(&dentry->link_group_list);
488 ERROR("Failed to allocate new dentry");
492 void dentry_free_ads_entries(struct dentry *dentry)
494 for (u16 i = 0; i < dentry->num_ads; i++)
495 destroy_ads_entry(&dentry->ads_entries[i]);
496 FREE(dentry->ads_entries);
497 dentry->ads_entries = NULL;
501 static void __destroy_dentry(struct dentry *dentry)
503 FREE(dentry->file_name);
504 FREE(dentry->file_name_utf8);
505 FREE(dentry->short_name);
506 FREE(dentry->full_path_utf8);
507 FREE(dentry->extracted_file);
510 void free_dentry(struct dentry *dentry)
512 __destroy_dentry(dentry);
513 if (dentry->link_group_master_status != GROUP_SLAVE)
514 dentry_free_ads_entries(dentry);
518 void put_dentry(struct dentry *dentry)
520 if (dentry->link_group_master_status == GROUP_MASTER) {
521 struct dentry *new_master;
522 list_for_each_entry(new_master, &dentry->link_group_list,
525 if (new_master->link_group_master_status == GROUP_SLAVE) {
526 new_master->link_group_master_status = GROUP_MASTER;
527 dentry->link_group_master_status = GROUP_SLAVE;
532 struct list_head *next;
533 next = dentry->link_group_list.next;
534 list_del(&dentry->link_group_list);
535 /*if (next->next == next)*/
536 /*container_of(next, struct dentry, link_group_list)->hard_link = 0;*/
540 static bool dentries_have_same_ads(const struct dentry *d1,
541 const struct dentry *d2)
543 /* Verify stream names and hashes are the same */
544 for (u16 i = 0; i < d1->num_ads; i++) {
545 if (strcmp(d1->ads_entries[i].stream_name_utf8,
546 d2->ads_entries[i].stream_name_utf8) != 0)
548 if (memcmp(d1->ads_entries[i].hash,
549 d2->ads_entries[i].hash,
556 /* Share the alternate stream entries between hard-linked dentries. */
557 int share_dentry_ads(struct dentry *master, struct dentry *slave)
559 const char *mismatch_type;
560 wimlib_assert(master->num_ads == 0 ||
561 master->ads_entries != slave->ads_entries);
562 if (master->attributes != slave->attributes) {
563 mismatch_type = "attributes";
566 if (master->attributes & FILE_ATTRIBUTE_DIRECTORY) {
567 WARNING("`%s' is hard-linked to `%s', which is a directory ",
568 slave->full_path_utf8, master->full_path_utf8);
569 return WIMLIB_ERR_INVALID_DENTRY;
571 if (master->security_id != slave->security_id) {
572 mismatch_type = "security ID";
575 if (memcmp(master->hash, slave->hash, WIM_HASH_SIZE) != 0) {
576 mismatch_type = "main file resource";
579 if (!dentries_have_same_ads(master, slave)) {
580 mismatch_type = "Alternate Stream Entries";
583 dentry_free_ads_entries(slave);
584 slave->ads_entries = master->ads_entries;
585 slave->link_group_master_status = GROUP_SLAVE;
588 WARNING("Dentries `%s' and `%s' in the same hard-link group but "
589 "do not share the same %s",
590 master->full_path_utf8, slave->full_path_utf8,
592 return WIMLIB_ERR_INVALID_DENTRY;
598 * - memory for file names is not cloned
599 * - next, prev, and children pointers and not touched
600 * - stream entries are not cloned.
602 struct dentry *clone_dentry(struct dentry *old)
604 struct dentry *new = MALLOC(sizeof(struct dentry));
607 memcpy(new, old, sizeof(struct dentry));
608 new->file_name = NULL;
609 new->file_name_len = 0;
610 new->file_name_utf8 = NULL;
611 new->file_name_utf8_len = 0;
612 new->short_name = NULL;
613 new->short_name_len = 0;
617 /* Arguments for do_free_dentry(). */
618 struct free_dentry_args {
619 struct lookup_table *lookup_table;
620 bool lt_decrement_refcnt;
624 * This function is passed as an argument to for_dentry_in_tree_depth() in order
625 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
627 static int do_free_dentry(struct dentry *dentry, void *__args)
629 struct free_dentry_args *args = (struct free_dentry_args*)__args;
631 if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
632 lookup_table_decrement_refcnt(args->lookup_table,
636 wimlib_assert(dentry->refcnt >= 1);
637 if (--dentry->refcnt == 0)
643 * Unlinks and frees a dentry tree.
645 * @root: The root of the tree.
646 * @lookup_table: The lookup table for dentries.
647 * @decrement_refcnt: True if the dentries in the tree are to have their
648 * reference counts in the lookup table decremented.
650 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table,
651 bool lt_decrement_refcnt)
653 if (!root || !root->parent)
656 struct free_dentry_args args;
657 args.lookup_table = lookup_table;
658 args.lt_decrement_refcnt = lt_decrement_refcnt;
659 for_dentry_in_tree_depth(root, do_free_dentry, &args);
662 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
669 * Links a dentry into the directory tree.
671 * @dentry: The dentry to link.
672 * @parent: The dentry that will be the parent of @dentry.
674 void link_dentry(struct dentry *dentry, struct dentry *parent)
676 dentry->parent = parent;
677 if (parent->children) {
678 /* Not an only child; link to siblings. */
679 dentry->next = parent->children;
680 dentry->prev = parent->children->prev;
681 dentry->next->prev = dentry;
682 dentry->prev->next = dentry;
684 /* Only child; link to parent. */
685 parent->children = dentry;
686 dentry->next = dentry;
687 dentry->prev = dentry;
692 /* Unlink a dentry from the directory tree.
694 * Note: This merely removes it from the in-memory tree structure. See
695 * remove_dentry() in mount.c for a function implemented on top of this one that
696 * frees the dentry and implements reference counting for the lookup table
698 void unlink_dentry(struct dentry *dentry)
700 if (dentry_is_root(dentry))
702 if (dentry_is_only_child(dentry)) {
703 dentry->parent->children = NULL;
705 if (dentry_is_first_sibling(dentry))
706 dentry->parent->children = dentry->next;
707 dentry->next->prev = dentry->prev;
708 dentry->prev->next = dentry->next;
713 /* Recalculates the length of @dentry based on its file name length and short
715 static inline void recalculate_dentry_size(struct dentry *dentry)
717 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
718 2 + dentry->short_name_len;
719 /* Must be multiple of 8. */
720 dentry->length = (dentry->length + 7) & ~7;
723 int get_names(char **name_utf16_ret, char **name_utf8_ret,
724 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
729 char *name_utf16, *name_utf8;
731 utf8_len = strlen(name);
733 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
736 return WIMLIB_ERR_NOMEM;
738 name_utf8 = MALLOC(utf8_len + 1);
741 return WIMLIB_ERR_NOMEM;
743 memcpy(name_utf8, name, utf8_len + 1);
744 FREE(*name_utf8_ret);
745 FREE(*name_utf16_ret);
746 *name_utf8_ret = name_utf8;
747 *name_utf16_ret = name_utf16;
748 *name_utf8_len_ret = utf8_len;
749 *name_utf16_len_ret = utf16_len;
753 /* Changes the name of a dentry to @new_name. Only changes the file_name and
754 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
755 * full_path_utf8 fields. Also recalculates its length. */
756 int change_dentry_name(struct dentry *dentry, const char *new_name)
760 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
761 &dentry->file_name_len, &dentry->file_name_utf8_len,
764 recalculate_dentry_size(dentry);
768 int change_ads_name(struct ads_entry *entry, const char *new_name)
770 return get_names(&entry->stream_name, &entry->stream_name_utf8,
771 &entry->stream_name_len,
772 &entry->stream_name_utf8_len,
776 /* Parameters for calculate_dentry_statistics(). */
777 struct image_statistics {
778 struct lookup_table *lookup_table;
782 u64 *hard_link_bytes;
785 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
787 struct image_statistics *stats;
788 struct lookup_table_entry *lte;
793 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
796 ++*stats->file_count;
798 lte = __lookup_resource(stats->lookup_table, dentry->hash);
802 u64 size = lte->resource_entry.original_size;
803 *stats->total_bytes += size;
804 if (++lte->out_refcnt == 1)
805 *stats->hard_link_bytes += size;
807 if (i == dentry->num_ads)
809 lte = __lookup_resource(stats->lookup_table,
810 dentry->ads_entries[i].hash);
817 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
818 u64 *dir_count_ret, u64 *file_count_ret,
819 u64 *total_bytes_ret,
820 u64 *hard_link_bytes_ret)
822 struct image_statistics stats;
825 *total_bytes_ret = 0;
826 *hard_link_bytes_ret = 0;
827 stats.lookup_table = table;
828 stats.dir_count = dir_count_ret;
829 stats.file_count = file_count_ret;
830 stats.total_bytes = total_bytes_ret;
831 stats.hard_link_bytes = hard_link_bytes_ret;
832 for_lookup_table_entry(table, zero_out_refcnts, NULL);
833 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
836 static int read_ads_entries(const u8 *p, struct dentry *dentry,
839 u16 num_ads = dentry->num_ads;
840 struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
843 ERROR("Could not allocate memory for %"PRIu16" "
844 "alternate data stream entries", num_ads);
845 return WIMLIB_ERR_NOMEM;
847 DEBUG2("Reading %"PRIu16" alternate data streams "
848 "(remaining size = %"PRIu64")", num_ads, remaining_size);
850 for (u16 i = 0; i < num_ads; i++) {
851 struct ads_entry *cur_entry = &ads_entries[i];
854 const char *p_save = p;
855 /* Read the base stream entry, excluding the stream name. */
856 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
857 ERROR("Stream entries go past end of metadata resource");
858 ERROR("(remaining_size = %"PRIu64")", remaining_size);
859 ret = WIMLIB_ERR_INVALID_DENTRY;
860 goto out_free_ads_entries;
862 remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
864 p = get_u64(p, &length); /* ADS entry length */
866 DEBUG2("ADS length = %"PRIu64, length);
869 p = get_bytes(p, WIM_HASH_SIZE, (u8*)cur_entry->hash);
870 p = get_u16(p, &cur_entry->stream_name_len);
872 DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
874 cur_entry->stream_name = NULL;
875 cur_entry->stream_name_utf8 = NULL;
877 if (remaining_size < cur_entry->stream_name_len + 2) {
878 ERROR("Stream entries go past end of metadata resource");
879 ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
880 "= %"PRIu16" bytes", remaining_size,
881 cur_entry->stream_name_len);
882 ret = WIMLIB_ERR_INVALID_DENTRY;
883 goto out_free_ads_entries;
885 remaining_size -= cur_entry->stream_name_len + 2;
887 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
888 if (!cur_entry->stream_name) {
889 ret = WIMLIB_ERR_NOMEM;
890 goto out_free_ads_entries;
892 get_bytes(p, cur_entry->stream_name_len,
893 (u8*)cur_entry->stream_name);
894 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
895 cur_entry->stream_name_len,
897 cur_entry->stream_name_utf8_len = utf8_len;
899 if (!cur_entry->stream_name_utf8) {
900 ret = WIMLIB_ERR_NOMEM;
901 goto out_free_ads_entries;
903 p = p_save + ads_entry_length(cur_entry);
905 dentry->ads_entries = ads_entries;
907 out_free_ads_entries:
908 for (u16 i = 0; i < num_ads; i++) {
909 FREE(ads_entries[i].stream_name);
910 FREE(ads_entries[i].stream_name_utf8);
917 * Reads a directory entry from the metadata resource.
919 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
920 u64 offset, struct dentry *dentry)
925 char *file_name_utf8;
929 size_t file_name_utf8_len;
932 dentry_common_init(dentry);
934 /*Make sure the dentry really fits into the metadata resource.*/
935 if (offset + 8 > metadata_resource_len) {
936 ERROR("Directory entry starting at %"PRIu64" ends past the "
937 "end of the metadata resource (size %"PRIu64")",
938 offset, metadata_resource_len);
939 return WIMLIB_ERR_INVALID_DENTRY;
942 /* Before reading the whole entry, we need to read just the length.
943 * This is because an entry of length 8 (that is, just the length field)
944 * terminates the list of sibling directory entries. */
946 p = get_u64(&metadata_resource[offset], &dentry->length);
948 /* A zero length field (really a length of 8, since that's how big the
949 * directory entry is...) indicates that this is the end of directory
950 * dentry. We do not read it into memory as an actual dentry, so just
951 * return true in that case. */
952 if (dentry->length == 0)
955 if (offset + dentry->length >= metadata_resource_len) {
956 ERROR("Directory entry at offset %"PRIu64" and with size "
957 "%"PRIu64" ends past the end of the metadata resource "
959 offset, dentry->length, metadata_resource_len);
960 return WIMLIB_ERR_INVALID_DENTRY;
963 /* If it is a recognized length, read the rest of the directory entry.
964 * Note: The root directory entry has no name, and its length does not
965 * include the short name length field. */
966 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
967 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
969 return WIMLIB_ERR_INVALID_DENTRY;
972 p = get_u32(p, &dentry->attributes);
973 p = get_u32(p, (u32*)&dentry->security_id);
974 p = get_u64(p, &dentry->subdir_offset);
976 /* 2 unused fields */
977 p += 2 * sizeof(u64);
978 /*p = get_u64(p, &dentry->unused1);*/
979 /*p = get_u64(p, &dentry->unused2);*/
981 p = get_u64(p, &dentry->creation_time);
982 p = get_u64(p, &dentry->last_access_time);
983 p = get_u64(p, &dentry->last_write_time);
985 p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
988 * I don't know what's going on here. It seems like M$ screwed up the
989 * reparse points, then put the fields in the same place and didn't
990 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
991 * have something to do with this, but it's not documented.
993 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
996 p = get_u32(p, &dentry->reparse_tag);
999 p = get_u32(p, &dentry->reparse_tag);
1000 p = get_u64(p, &dentry->hard_link);
1003 /* By the way, the reparse_reserved field does not actually exist (at
1004 * least when the file is not a reparse point) */
1007 p = get_u16(p, &dentry->num_ads);
1009 p = get_u16(p, &short_name_len);
1010 p = get_u16(p, &file_name_len);
1012 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
1015 if (dentry->length < calculated_size) {
1016 ERROR("Unexpected end of directory entry! (Expected "
1017 "%"PRIu64" bytes, got %"PRIu64" bytes. "
1018 "short_name_len = %hu, file_name_len = %hu)",
1019 calculated_size, dentry->length,
1020 short_name_len, file_name_len);
1021 return WIMLIB_ERR_INVALID_DENTRY;
1024 /* Read the filename. */
1025 file_name = MALLOC(file_name_len);
1027 ERROR("Failed to allocate %hu bytes for dentry file name",
1029 return WIMLIB_ERR_NOMEM;
1031 p = get_bytes(p, file_name_len, file_name);
1033 /* Convert filename to UTF-8. */
1034 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1035 &file_name_utf8_len);
1037 if (!file_name_utf8) {
1038 ERROR("Failed to allocate memory to convert UTF-16 "
1039 "filename (%hu bytes) to UTF-8", file_name_len);
1040 ret = WIMLIB_ERR_NOMEM;
1041 goto out_free_file_name;
1044 /* Undocumented padding between file name and short name. This probably
1045 * is supposed to be a terminating null character. */
1048 /* Read the short filename. */
1049 short_name = MALLOC(short_name_len);
1051 ERROR("Failed to allocate %hu bytes for short filename",
1053 ret = WIMLIB_ERR_NOMEM;
1054 goto out_free_file_name_utf8;
1057 p = get_bytes(p, short_name_len, short_name);
1059 /* Some directory entries inexplicibly have a little over 70 bytes of
1060 * extra data. The exact amount of data seems to be 72 bytes, but it is
1061 * aligned on the next 8-byte boundary. Here's an example of the
1064 * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
1065 * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
1066 * 0000000000000000000000
1068 * Here's one interpretation of how the data is laid out.
1071 * u32 field1; (always 0x00000001)
1072 * u32 field2; (always 0x40000000)
1077 * u8 data[48]; (???)
1078 * u64 reserved1; (always 0)
1079 * u64 reserved2; (always 0)
1082 if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
1083 printf("%s: %lu / %lu (", file_name_utf8,
1084 calculated_size, dentry->length);
1085 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
1087 print_byte_field(p, dentry->length - calculated_size);
1092 if (dentry->num_ads != 0) {
1093 calculated_size = (calculated_size + 7) & ~7;
1094 if (calculated_size > metadata_resource_len - offset) {
1095 ERROR("Not enough space in metadata resource for "
1096 "alternate stream entries");
1097 ret = WIMLIB_ERR_INVALID_DENTRY;
1098 goto out_free_short_name;
1100 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1102 metadata_resource_len - offset - calculated_size);
1104 goto out_free_short_name;
1107 dentry->file_name = file_name;
1108 dentry->file_name_utf8 = file_name_utf8;
1109 dentry->short_name = short_name;
1110 dentry->file_name_len = file_name_len;
1111 dentry->file_name_utf8_len = file_name_utf8_len;
1112 dentry->short_name_len = short_name_len;
1114 out_free_short_name:
1116 out_free_file_name_utf8:
1117 FREE(file_name_utf8);
1124 * Writes a dentry to an output buffer.
1126 * @dentry: The dentry structure.
1127 * @p: The memory location to write the data to.
1128 * @return: Pointer to the byte after the last byte we wrote as part of the
1131 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1136 p = put_u64(p, dentry->length);
1137 p = put_u32(p, dentry->attributes);
1138 p = put_u32(p, dentry->security_id);
1139 p = put_u64(p, dentry->subdir_offset);
1140 p = put_u64(p, 0); /* unused1 */
1141 p = put_u64(p, 0); /* unused2 */
1142 p = put_u64(p, dentry->creation_time);
1143 p = put_u64(p, dentry->last_access_time);
1144 p = put_u64(p, dentry->last_write_time);
1145 p = put_bytes(p, WIM_HASH_SIZE, dentry->hash);
1146 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1147 p = put_zeroes(p, 4);
1148 p = put_u32(p, dentry->reparse_tag);
1149 p = put_zeroes(p, 4);
1152 p = put_u32(p, dentry->reparse_tag);
1153 if (dentry->link_group_list.next == &dentry->link_group_list)
1156 hard_link = dentry->hard_link;
1157 p = put_u64(p, hard_link);
1159 p = put_u16(p, dentry->num_ads);
1160 p = put_u16(p, dentry->short_name_len);
1161 p = put_u16(p, dentry->file_name_len);
1162 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1163 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1164 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1166 wimlib_assert(p - orig_p <= dentry->length);
1167 if (p - orig_p < dentry->length)
1168 p = put_zeroes(p, dentry->length - (p - orig_p));
1170 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1172 for (u16 i = 0; i < dentry->num_ads; i++) {
1173 p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
1174 p = put_u64(p, 0); /* Unused */
1175 p = put_bytes(p, WIM_HASH_SIZE, dentry->ads_entries[i].hash);
1176 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
1177 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
1178 (u8*)dentry->ads_entries[i].stream_name);
1179 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1184 /* Recursive function that writes a dentry tree rooted at @tree, not including
1185 * @tree itself, which has already been written, except in the case of the root
1186 * dentry, which is written right away, along with an end-of-directory entry. */
1187 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
1189 const struct dentry *child;
1191 if (dentry_is_root(tree)) {
1192 p = write_dentry(tree, p);
1194 /* write end of directory entry */
1197 /* Nothing to do for non-directories */
1198 if (!dentry_is_directory(tree))
1202 /* Write child dentries and end-of-directory entry. */
1203 child = tree->children;
1206 p = write_dentry(child, p);
1207 child = child->next;
1208 } while (child != tree->children);
1211 /* write end of directory entry */
1214 /* Recurse on children. */
1217 p = write_dentry_tree(child, p);
1218 child = child->next;
1219 } while (child != tree->children);
1224 /* Reads the children of a dentry, and all their children, ..., etc. from the
1225 * metadata resource and into the dentry tree.
1227 * @metadata_resource: An array that contains the uncompressed metadata
1228 * resource for the WIM file.
1229 * @metadata_resource_len: The length of @metadata_resource.
1230 * @dentry: A pointer to a struct dentry that is the root of the directory
1231 * tree and has already been read from the metadata resource. It
1232 * does not need to be the real root because this procedure is
1233 * called recursively.
1235 * @return: Zero on success, nonzero on failure.
1237 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1238 struct dentry *dentry)
1240 u64 cur_offset = dentry->subdir_offset;
1241 struct dentry *prev_child = NULL;
1242 struct dentry *first_child = NULL;
1243 struct dentry *child;
1244 struct dentry cur_child;
1247 /* If @dentry is a regular file, nothing more needs to be done for this
1249 if (cur_offset == 0)
1252 /* Find and read all the children of @dentry. */
1255 /* Read next child of @dentry into @cur_child. */
1256 ret = read_dentry(metadata_resource, metadata_resource_len,
1257 cur_offset, &cur_child);
1261 /* Check for end of directory. */
1262 if (cur_child.length == 0) {
1267 /* Not end of directory. Allocate this child permanently and
1268 * link it to the parent and previous child. */
1269 child = MALLOC(sizeof(struct dentry));
1271 ERROR("Failed to allocate %zu bytes for new dentry",
1272 sizeof(struct dentry));
1273 ret = WIMLIB_ERR_NOMEM;
1276 memcpy(child, &cur_child, sizeof(struct dentry));
1279 prev_child->next = child;
1280 child->prev = prev_child;
1282 first_child = child;
1285 child->parent = dentry;
1288 /* If there are children of this child, call this procedure
1290 if (child->subdir_offset != 0) {
1291 ret = read_dentry_tree(metadata_resource,
1292 metadata_resource_len, child);
1297 /* Advance to the offset of the next child. */
1298 cur_offset += dentry_total_length(child);
1301 /* Link last child to first one, and set parent's
1302 * children pointer to the first child. */
1304 prev_child->next = first_child;
1305 first_child->prev = prev_child;
1307 dentry->children = first_child;