4 * A dentry (directory entry) contains the metadata for a file. In the WIM file
5 * format, the dentries are stored in the "metadata resource" section right
6 * after the security data. Each image in the WIM file has its own metadata
7 * resource with its own security data and dentry tree. Dentries in different
8 * images may share file resources by referring to the same lookup table
14 * Copyright (C) 2010 Carl Thijssen
15 * Copyright (C) 2012 Eric Biggers
17 * This file is part of wimlib, a library for working with WIM files.
19 * wimlib is free software; you can redistribute it and/or modify it under the
20 * terms of the GNU Lesser General Public License as published by the Free
21 * Software Foundation; either version 2.1 of the License, or (at your option)
24 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
25 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
26 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with wimlib; if not, see http://www.gnu.org/licenses/.
33 #include "wimlib_internal.h"
36 #include "timestamp.h"
37 #include "lookup_table.h"
43 * Returns true if @dentry has the UTF-8 file name @name that has length
46 static bool dentry_has_name(const struct dentry *dentry, const char *name,
49 if (dentry->file_name_utf8_len != name_len)
51 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
54 /* Real length of a dentry, including the alternate data stream entries, which
55 * are not included in the dentry->length field... */
56 u64 dentry_total_length(const struct dentry *dentry)
58 u64 length = (dentry->length + 7) & ~7;
59 for (u16 i = 0 ; i < dentry->num_ads; i++)
60 length += ads_entry_length(&dentry->ads_entries[i]);
64 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
65 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
67 if (S_ISLNK(stbuf->st_mode)) {
68 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
69 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
70 } else if (S_ISDIR(stbuf->st_mode)) {
71 dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
73 dentry->attributes = FILE_ATTRIBUTE_NORMAL;
75 if (sizeof(ino_t) >= 8)
76 dentry->hard_link = (u64)stbuf->st_ino;
78 dentry->hard_link = (u64)stbuf->st_ino |
79 ((u64)stbuf->st_dev << (sizeof(ino_t) * 8));
82 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
83 int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
84 const struct lookup_table *table)
86 struct lookup_table_entry *lte;
88 if (dentry_is_symlink(dentry))
89 stbuf->st_mode = S_IFLNK | 0777;
90 else if (dentry_is_directory(dentry))
91 stbuf->st_mode = S_IFDIR | 0755;
93 stbuf->st_mode = S_IFREG | 0644;
95 stbuf->st_ino = (ino_t)dentry->hard_link;
97 stbuf->st_nlink = dentry_link_group_size(dentry);
98 stbuf->st_uid = getuid();
99 stbuf->st_gid = getgid();
101 /* Use the size of the unnamed (default) file stream. */
102 if (table && (lte = __lookup_resource(table, dentry_hash(dentry)))) {
103 if (lte->staging_file_name) {
104 struct stat native_stat;
105 if (stat(lte->staging_file_name, &native_stat) != 0)
107 stbuf->st_size = native_stat.st_size;
109 stbuf->st_size = lte->resource_entry.original_size;
115 stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
116 stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
117 stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
118 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
122 /* Makes all timestamp fields for the dentry be the current time. */
123 void dentry_update_all_timestamps(struct dentry *dentry)
125 u64 now = get_timestamp();
126 dentry->creation_time = now;
127 dentry->last_access_time = now;
128 dentry->last_write_time = now;
131 struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
132 const char *stream_name)
134 size_t stream_name_len = strlen(stream_name);
137 for (u16 i = 0; i < dentry->num_ads; i++)
138 if (ads_entry_has_name(&dentry->ads_entries[i],
139 stream_name, stream_name_len))
140 return &dentry->ads_entries[i];
144 /* Add an alternate stream entry to a dentry and return a pointer to it, or NULL
146 struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
148 u16 num_ads = dentry->num_ads + 1;
149 struct ads_entry *ads_entries;
150 struct ads_entry *new_entry;
151 if (num_ads == 0xffff)
153 ads_entries = MALLOC(num_ads * sizeof(struct ads_entry));
157 new_entry = &ads_entries[num_ads - 1];
158 if (change_ads_name(new_entry, stream_name) != 0) {
163 memcpy(ads_entries, dentry->ads_entries,
164 (num_ads - 1) * sizeof(struct ads_entry));
165 FREE(dentry->ads_entries);
166 dentry->ads_entries = ads_entries;
167 dentry->num_ads = num_ads;
168 return memset(new_entry, 0, sizeof(struct ads_entry));
171 void dentry_remove_ads(struct dentry *dentry, struct ads_entry *sentry)
173 destroy_ads_entry(sentry);
174 memcpy(sentry, sentry + 1,
175 (dentry->num_ads - (sentry - dentry->ads_entries + 1))
176 * sizeof(struct ads_entry));
181 * Calls a function on all directory entries in a directory tree. It is called
182 * on a parent before its children.
184 int for_dentry_in_tree(struct dentry *root,
185 int (*visitor)(struct dentry*, void*), void *arg)
188 struct dentry *child;
190 ret = visitor(root, arg);
195 child = root->children;
201 ret = for_dentry_in_tree(child, visitor, arg);
205 } while (child != root->children);
210 * Like for_dentry_in_tree(), but the visitor function is always called on a
211 * dentry's children before on itself.
213 int for_dentry_in_tree_depth(struct dentry *root,
214 int (*visitor)(struct dentry*, void*), void *arg)
217 struct dentry *child;
220 child = root->children;
224 ret = for_dentry_in_tree_depth(child, visitor, arg);
228 } while (child != root->children);
230 return visitor(root, arg);
234 * Calculate the full path of @dentry, based on its parent's full path and on
235 * its UTF-8 file name.
237 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
241 if (dentry_is_root(dentry)) {
242 full_path = MALLOC(2);
249 char *parent_full_path;
250 u32 parent_full_path_len;
251 const struct dentry *parent = dentry->parent;
253 if (dentry_is_root(parent)) {
254 parent_full_path = "";
255 parent_full_path_len = 0;
257 parent_full_path = parent->full_path_utf8;
258 parent_full_path_len = parent->full_path_utf8_len;
261 full_path_len = parent_full_path_len + 1 +
262 dentry->file_name_utf8_len;
263 full_path = MALLOC(full_path_len + 1);
267 memcpy(full_path, parent_full_path, parent_full_path_len);
268 full_path[parent_full_path_len] = '/';
269 memcpy(full_path + parent_full_path_len + 1,
270 dentry->file_name_utf8,
271 dentry->file_name_utf8_len);
272 full_path[full_path_len] = '\0';
274 FREE(dentry->full_path_utf8);
275 dentry->full_path_utf8 = full_path;
276 dentry->full_path_utf8_len = full_path_len;
279 ERROR("Out of memory while calculating dentry full path");
280 return WIMLIB_ERR_NOMEM;
284 * Recursively calculates the subdir offsets for a directory tree.
286 * @dentry: The root of the directory tree.
287 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
288 * offset for @dentry.
290 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
292 struct dentry *child;
294 child = dentry->children;
295 dentry->subdir_offset = *subdir_offset_p;
298 /* Advance the subdir offset by the amount of space the children
299 * of this dentry take up. */
301 *subdir_offset_p += dentry_total_length(child);
303 } while (child != dentry->children);
305 /* End-of-directory dentry on disk. */
306 *subdir_offset_p += 8;
308 /* Recursively call calculate_subdir_offsets() on all the
311 calculate_subdir_offsets(child, subdir_offset_p);
313 } while (child != dentry->children);
315 /* On disk, childless directories have a valid subdir_offset
316 * that points to an 8-byte end-of-directory dentry. Regular
317 * files have a subdir_offset of 0. */
318 if (dentry_is_directory(dentry))
319 *subdir_offset_p += 8;
321 dentry->subdir_offset = 0;
326 /* Returns the child of @dentry that has the file name @name.
327 * Returns NULL if no child has the name. */
328 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
331 struct dentry *child;
334 child = dentry->children;
336 name_len = strlen(name);
338 if (dentry_has_name(child, name, name_len))
341 } while (child != dentry->children);
346 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
347 * @cur_dir. Returns NULL if no dentry having the path is found. */
348 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
350 struct dentry *child;
352 const char *new_path;
357 child = cur_dir->children;
359 new_path = path_next_part(path, &base_len);
361 if (dentry_has_name(child, path, base_len))
362 return get_dentry_relative_path(child, new_path);
364 } while (child != cur_dir->children);
369 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
371 struct dentry *get_dentry(WIMStruct *w, const char *path)
373 struct dentry *root = wim_root_dentry(w);
376 return get_dentry_relative_path(root, path);
379 /* Returns the parent directory for the @path. */
380 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
382 size_t path_len = strlen(path);
383 char buf[path_len + 1];
385 memcpy(buf, path, path_len + 1);
387 to_parent_name(buf, path_len);
389 return get_dentry(w, buf);
392 /* Prints the full path of a dentry. */
393 int print_dentry_full_path(struct dentry *dentry, void *ignore)
395 if (dentry->full_path_utf8)
396 puts(dentry->full_path_utf8);
400 struct file_attr_flag {
404 struct file_attr_flag file_attr_flags[] = {
405 {FILE_ATTRIBUTE_READONLY, "READONLY"},
406 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
407 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
408 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
409 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
410 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
411 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
412 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
413 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
414 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
415 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
416 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
417 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
418 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
419 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
422 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
423 * NULL if the resource entry for the dentry is not to be printed. */
424 int print_dentry(struct dentry *dentry, void *lookup_table)
426 struct lookup_table_entry *lte;
429 printf("[DENTRY]\n");
430 printf("Length = %"PRIu64"\n", dentry->length);
431 printf("Attributes = 0x%x\n", dentry->attributes);
432 for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
433 if (file_attr_flags[i].flag & dentry->attributes)
434 printf(" FILE_ATTRIBUTE_%s is set\n",
435 file_attr_flags[i].name);
436 printf("Security ID = %d\n", dentry->security_id);
437 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
438 /*printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);*/
439 /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
440 printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
441 printf("Last Access Time = 0x%"PRIx64"\n", dentry->last_access_time);
442 printf("Last Write Time = 0x%"PRIx64"\n", dentry->last_write_time);
444 print_hash(dentry->hash);
446 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
447 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
448 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
449 printf("Filename = \"");
450 print_string(dentry->file_name, dentry->file_name_len);
452 printf("Filename Length = %hu\n", dentry->file_name_len);
453 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
454 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
455 printf("Short Name = \"");
456 print_string(dentry->short_name, dentry->short_name_len);
458 printf("Short Name Length = %hu\n", dentry->short_name_len);
459 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
460 if (lookup_table && (lte = __lookup_resource(lookup_table, dentry->hash)))
461 print_lookup_table_entry(lte, NULL);
464 for (u16 i = 0; i < dentry->num_ads; i++) {
465 printf("[Alternate Stream Entry %u]\n", i);
466 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
467 printf("Name Length (UTF-16) = %u\n",
468 dentry->ads_entries[i].stream_name_len);
470 print_hash(dentry->ads_entries[i].hash);
472 (lte = __lookup_resource(lookup_table,
473 dentry->ads_entries[i].hash)))
475 print_lookup_table_entry(lte, NULL);
483 static inline void dentry_common_init(struct dentry *dentry)
485 memset(dentry, 0, sizeof(struct dentry));
487 dentry->security_id = -1;
488 dentry->link_group_master_status = GROUP_SLAVE;
492 * Creates an unlinked directory entry.
494 * @name: The base name of the new dentry.
495 * @return: A pointer to the new dentry, or NULL if out of memory.
497 struct dentry *new_dentry(const char *name)
499 struct dentry *dentry;
501 dentry = MALLOC(sizeof(struct dentry));
505 dentry_common_init(dentry);
506 if (change_dentry_name(dentry, name) != 0)
509 dentry_update_all_timestamps(dentry);
510 dentry->next = dentry;
511 dentry->prev = dentry;
512 dentry->parent = dentry;
513 INIT_LIST_HEAD(&dentry->link_group_list);
517 ERROR("Failed to allocate new dentry");
521 void dentry_free_ads_entries(struct dentry *dentry)
523 for (u16 i = 0; i < dentry->num_ads; i++)
524 destroy_ads_entry(&dentry->ads_entries[i]);
525 FREE(dentry->ads_entries);
526 dentry->ads_entries = NULL;
530 static void __destroy_dentry(struct dentry *dentry)
532 FREE(dentry->file_name);
533 FREE(dentry->file_name_utf8);
534 FREE(dentry->short_name);
535 FREE(dentry->full_path_utf8);
536 FREE(dentry->extracted_file);
539 void free_dentry(struct dentry *dentry)
541 __destroy_dentry(dentry);
542 if (dentry->link_group_master_status != GROUP_SLAVE)
543 dentry_free_ads_entries(dentry);
547 void put_dentry(struct dentry *dentry)
549 if (dentry->link_group_master_status == GROUP_MASTER) {
550 struct dentry *new_master;
551 list_for_each_entry(new_master, &dentry->link_group_list,
554 if (new_master->link_group_master_status == GROUP_SLAVE) {
555 new_master->link_group_master_status = GROUP_MASTER;
556 dentry->link_group_master_status = GROUP_SLAVE;
561 struct list_head *next;
562 next = dentry->link_group_list.next;
563 list_del(&dentry->link_group_list);
564 /*if (next->next == next)*/
565 /*container_of(next, struct dentry, link_group_list)->hard_link = 0;*/
569 static bool dentries_have_same_ads(const struct dentry *d1,
570 const struct dentry *d2)
572 /* Verify stream names and hashes are the same */
573 for (u16 i = 0; i < d1->num_ads; i++) {
574 if (strcmp(d1->ads_entries[i].stream_name_utf8,
575 d2->ads_entries[i].stream_name_utf8) != 0)
577 if (memcmp(d1->ads_entries[i].hash,
578 d2->ads_entries[i].hash,
585 /* Share the alternate stream entries between hard-linked dentries. */
586 int share_dentry_ads(struct dentry *master, struct dentry *slave)
588 const char *mismatch_type;
589 wimlib_assert(master->num_ads == 0 ||
590 master->ads_entries != slave->ads_entries);
591 if (master->attributes != slave->attributes) {
592 mismatch_type = "attributes";
595 if (master->attributes & FILE_ATTRIBUTE_DIRECTORY) {
596 WARNING("`%s' is hard-linked to `%s', which is a directory ",
597 slave->full_path_utf8, master->full_path_utf8);
598 return WIMLIB_ERR_INVALID_DENTRY;
600 if (master->security_id != slave->security_id) {
601 mismatch_type = "security ID";
604 if (memcmp(master->hash, slave->hash, WIM_HASH_SIZE) != 0) {
605 mismatch_type = "main file resource";
608 if (!dentries_have_same_ads(master, slave)) {
609 mismatch_type = "Alternate Stream Entries";
612 dentry_free_ads_entries(slave);
613 slave->ads_entries = master->ads_entries;
614 slave->link_group_master_status = GROUP_SLAVE;
617 WARNING("Dentries `%s' and `%s' in the same hard-link group but "
618 "do not share the same %s",
619 master->full_path_utf8, slave->full_path_utf8,
621 return WIMLIB_ERR_INVALID_DENTRY;
627 * - memory for file names is not cloned
628 * - next, prev, and children pointers and not touched
629 * - stream entries are not cloned.
631 struct dentry *clone_dentry(struct dentry *old)
633 struct dentry *new = MALLOC(sizeof(struct dentry));
636 memcpy(new, old, sizeof(struct dentry));
637 new->file_name = NULL;
638 new->file_name_len = 0;
639 new->file_name_utf8 = NULL;
640 new->file_name_utf8_len = 0;
641 new->short_name = NULL;
642 new->short_name_len = 0;
646 /* Arguments for do_free_dentry(). */
647 struct free_dentry_args {
648 struct lookup_table *lookup_table;
649 bool lt_decrement_refcnt;
653 * This function is passed as an argument to for_dentry_in_tree_depth() in order
654 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
656 static int do_free_dentry(struct dentry *dentry, void *__args)
658 struct free_dentry_args *args = (struct free_dentry_args*)__args;
660 if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
661 lookup_table_decrement_refcnt(args->lookup_table,
665 wimlib_assert(dentry->refcnt >= 1);
666 if (--dentry->refcnt == 0)
672 * Unlinks and frees a dentry tree.
674 * @root: The root of the tree.
675 * @lookup_table: The lookup table for dentries.
676 * @decrement_refcnt: True if the dentries in the tree are to have their
677 * reference counts in the lookup table decremented.
679 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table,
680 bool lt_decrement_refcnt)
682 if (!root || !root->parent)
685 struct free_dentry_args args;
686 args.lookup_table = lookup_table;
687 args.lt_decrement_refcnt = lt_decrement_refcnt;
688 for_dentry_in_tree_depth(root, do_free_dentry, &args);
691 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
698 * Links a dentry into the directory tree.
700 * @dentry: The dentry to link.
701 * @parent: The dentry that will be the parent of @dentry.
703 void link_dentry(struct dentry *dentry, struct dentry *parent)
705 dentry->parent = parent;
706 if (parent->children) {
707 /* Not an only child; link to siblings. */
708 dentry->next = parent->children;
709 dentry->prev = parent->children->prev;
710 dentry->next->prev = dentry;
711 dentry->prev->next = dentry;
713 /* Only child; link to parent. */
714 parent->children = dentry;
715 dentry->next = dentry;
716 dentry->prev = dentry;
721 /* Unlink a dentry from the directory tree.
723 * Note: This merely removes it from the in-memory tree structure. See
724 * remove_dentry() in mount.c for a function implemented on top of this one that
725 * frees the dentry and implements reference counting for the lookup table
727 void unlink_dentry(struct dentry *dentry)
729 if (dentry_is_root(dentry))
731 if (dentry_is_only_child(dentry)) {
732 dentry->parent->children = NULL;
734 if (dentry_is_first_sibling(dentry))
735 dentry->parent->children = dentry->next;
736 dentry->next->prev = dentry->prev;
737 dentry->prev->next = dentry->next;
742 /* Recalculates the length of @dentry based on its file name length and short
744 static inline void recalculate_dentry_size(struct dentry *dentry)
746 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
747 2 + dentry->short_name_len;
748 /* Must be multiple of 8. */
749 dentry->length = (dentry->length + 7) & ~7;
752 int get_names(char **name_utf16_ret, char **name_utf8_ret,
753 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
758 char *name_utf16, *name_utf8;
760 utf8_len = strlen(name);
762 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
765 return WIMLIB_ERR_NOMEM;
767 name_utf8 = MALLOC(utf8_len + 1);
770 return WIMLIB_ERR_NOMEM;
772 memcpy(name_utf8, name, utf8_len + 1);
773 FREE(*name_utf8_ret);
774 FREE(*name_utf16_ret);
775 *name_utf8_ret = name_utf8;
776 *name_utf16_ret = name_utf16;
777 *name_utf8_len_ret = utf8_len;
778 *name_utf16_len_ret = utf16_len;
782 /* Changes the name of a dentry to @new_name. Only changes the file_name and
783 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
784 * full_path_utf8 fields. Also recalculates its length. */
785 int change_dentry_name(struct dentry *dentry, const char *new_name)
789 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
790 &dentry->file_name_len, &dentry->file_name_utf8_len,
793 recalculate_dentry_size(dentry);
797 int change_ads_name(struct ads_entry *entry, const char *new_name)
799 return get_names(&entry->stream_name, &entry->stream_name_utf8,
800 &entry->stream_name_len,
801 &entry->stream_name_utf8_len,
805 /* Parameters for calculate_dentry_statistics(). */
806 struct image_statistics {
807 struct lookup_table *lookup_table;
811 u64 *hard_link_bytes;
814 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
816 struct image_statistics *stats;
817 struct lookup_table_entry *lte;
822 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
825 ++*stats->file_count;
827 lte = __lookup_resource(stats->lookup_table, dentry->hash);
831 u64 size = lte->resource_entry.original_size;
832 *stats->total_bytes += size;
833 if (++lte->out_refcnt == 1)
834 *stats->hard_link_bytes += size;
836 if (i == dentry->num_ads)
838 lte = __lookup_resource(stats->lookup_table,
839 dentry->ads_entries[i].hash);
846 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
847 u64 *dir_count_ret, u64 *file_count_ret,
848 u64 *total_bytes_ret,
849 u64 *hard_link_bytes_ret)
851 struct image_statistics stats;
854 *total_bytes_ret = 0;
855 *hard_link_bytes_ret = 0;
856 stats.lookup_table = table;
857 stats.dir_count = dir_count_ret;
858 stats.file_count = file_count_ret;
859 stats.total_bytes = total_bytes_ret;
860 stats.hard_link_bytes = hard_link_bytes_ret;
861 for_lookup_table_entry(table, zero_out_refcnts, NULL);
862 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
865 static int read_ads_entries(const u8 *p, struct dentry *dentry,
868 u16 num_ads = dentry->num_ads;
869 struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
872 ERROR("Could not allocate memory for %"PRIu16" "
873 "alternate data stream entries", num_ads);
874 return WIMLIB_ERR_NOMEM;
876 DEBUG2("Reading %"PRIu16" alternate data streams "
877 "(remaining size = %"PRIu64")", num_ads, remaining_size);
879 for (u16 i = 0; i < num_ads; i++) {
880 struct ads_entry *cur_entry = &ads_entries[i];
883 const char *p_save = p;
884 /* Read the base stream entry, excluding the stream name. */
885 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
886 ERROR("Stream entries go past end of metadata resource");
887 ERROR("(remaining_size = %"PRIu64")", remaining_size);
888 ret = WIMLIB_ERR_INVALID_DENTRY;
889 goto out_free_ads_entries;
891 remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
893 p = get_u64(p, &length); /* ADS entry length */
895 DEBUG2("ADS length = %"PRIu64, length);
898 p = get_bytes(p, WIM_HASH_SIZE, (u8*)cur_entry->hash);
899 p = get_u16(p, &cur_entry->stream_name_len);
901 DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
903 cur_entry->stream_name = NULL;
904 cur_entry->stream_name_utf8 = NULL;
906 if (remaining_size < cur_entry->stream_name_len + 2) {
907 ERROR("Stream entries go past end of metadata resource");
908 ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
909 "= %"PRIu16" bytes", remaining_size,
910 cur_entry->stream_name_len);
911 ret = WIMLIB_ERR_INVALID_DENTRY;
912 goto out_free_ads_entries;
914 remaining_size -= cur_entry->stream_name_len + 2;
916 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
917 if (!cur_entry->stream_name) {
918 ret = WIMLIB_ERR_NOMEM;
919 goto out_free_ads_entries;
921 get_bytes(p, cur_entry->stream_name_len,
922 (u8*)cur_entry->stream_name);
923 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
924 cur_entry->stream_name_len,
926 cur_entry->stream_name_utf8_len = utf8_len;
928 if (!cur_entry->stream_name_utf8) {
929 ret = WIMLIB_ERR_NOMEM;
930 goto out_free_ads_entries;
932 p = p_save + ads_entry_length(cur_entry);
934 dentry->ads_entries = ads_entries;
936 out_free_ads_entries:
937 for (u16 i = 0; i < num_ads; i++) {
938 FREE(ads_entries[i].stream_name);
939 FREE(ads_entries[i].stream_name_utf8);
946 * Reads a directory entry from the metadata resource.
948 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
949 u64 offset, struct dentry *dentry)
954 char *file_name_utf8;
958 size_t file_name_utf8_len;
961 dentry_common_init(dentry);
963 /*Make sure the dentry really fits into the metadata resource.*/
964 if (offset + 8 > metadata_resource_len) {
965 ERROR("Directory entry starting at %"PRIu64" ends past the "
966 "end of the metadata resource (size %"PRIu64")",
967 offset, metadata_resource_len);
968 return WIMLIB_ERR_INVALID_DENTRY;
971 /* Before reading the whole entry, we need to read just the length.
972 * This is because an entry of length 8 (that is, just the length field)
973 * terminates the list of sibling directory entries. */
975 p = get_u64(&metadata_resource[offset], &dentry->length);
977 /* A zero length field (really a length of 8, since that's how big the
978 * directory entry is...) indicates that this is the end of directory
979 * dentry. We do not read it into memory as an actual dentry, so just
980 * return true in that case. */
981 if (dentry->length == 0)
984 if (offset + dentry->length >= metadata_resource_len) {
985 ERROR("Directory entry at offset %"PRIu64" and with size "
986 "%"PRIu64" ends past the end of the metadata resource "
988 offset, dentry->length, metadata_resource_len);
989 return WIMLIB_ERR_INVALID_DENTRY;
992 /* If it is a recognized length, read the rest of the directory entry.
993 * Note: The root directory entry has no name, and its length does not
994 * include the short name length field. */
995 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
996 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
998 return WIMLIB_ERR_INVALID_DENTRY;
1001 p = get_u32(p, &dentry->attributes);
1002 p = get_u32(p, (u32*)&dentry->security_id);
1003 p = get_u64(p, &dentry->subdir_offset);
1005 /* 2 unused fields */
1006 p += 2 * sizeof(u64);
1007 /*p = get_u64(p, &dentry->unused1);*/
1008 /*p = get_u64(p, &dentry->unused2);*/
1010 p = get_u64(p, &dentry->creation_time);
1011 p = get_u64(p, &dentry->last_access_time);
1012 p = get_u64(p, &dentry->last_write_time);
1014 p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
1017 * I don't know what's going on here. It seems like M$ screwed up the
1018 * reparse points, then put the fields in the same place and didn't
1019 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1020 * have something to do with this, but it's not documented.
1022 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1025 p = get_u32(p, &dentry->reparse_tag);
1028 p = get_u32(p, &dentry->reparse_tag);
1029 p = get_u64(p, &dentry->hard_link);
1032 /* By the way, the reparse_reserved field does not actually exist (at
1033 * least when the file is not a reparse point) */
1036 p = get_u16(p, &dentry->num_ads);
1038 p = get_u16(p, &short_name_len);
1039 p = get_u16(p, &file_name_len);
1041 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
1044 if (dentry->length < calculated_size) {
1045 ERROR("Unexpected end of directory entry! (Expected "
1046 "%"PRIu64" bytes, got %"PRIu64" bytes. "
1047 "short_name_len = %hu, file_name_len = %hu)",
1048 calculated_size, dentry->length,
1049 short_name_len, file_name_len);
1050 return WIMLIB_ERR_INVALID_DENTRY;
1053 /* Read the filename. */
1054 file_name = MALLOC(file_name_len);
1056 ERROR("Failed to allocate %hu bytes for dentry file name",
1058 return WIMLIB_ERR_NOMEM;
1060 p = get_bytes(p, file_name_len, file_name);
1062 /* Convert filename to UTF-8. */
1063 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1064 &file_name_utf8_len);
1066 if (!file_name_utf8) {
1067 ERROR("Failed to allocate memory to convert UTF-16 "
1068 "filename (%hu bytes) to UTF-8", file_name_len);
1069 ret = WIMLIB_ERR_NOMEM;
1070 goto out_free_file_name;
1073 /* Undocumented padding between file name and short name. This probably
1074 * is supposed to be a terminating null character. */
1077 /* Read the short filename. */
1078 short_name = MALLOC(short_name_len);
1080 ERROR("Failed to allocate %hu bytes for short filename",
1082 ret = WIMLIB_ERR_NOMEM;
1083 goto out_free_file_name_utf8;
1086 p = get_bytes(p, short_name_len, short_name);
1088 /* Some directory entries inexplicibly have a little over 70 bytes of
1089 * extra data. The exact amount of data seems to be 72 bytes, but it is
1090 * aligned on the next 8-byte boundary. Here's an example of the
1093 * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
1094 * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
1095 * 0000000000000000000000
1097 * Here's one interpretation of how the data is laid out.
1100 * u32 field1; (always 0x00000001)
1101 * u32 field2; (always 0x40000000)
1106 * u8 data[48]; (???)
1107 * u64 reserved1; (always 0)
1108 * u64 reserved2; (always 0)
1111 if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
1112 printf("%s: %lu / %lu (", file_name_utf8,
1113 calculated_size, dentry->length);
1114 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
1116 print_byte_field(p, dentry->length - calculated_size);
1121 if (dentry->num_ads != 0) {
1122 calculated_size = (calculated_size + 7) & ~7;
1123 if (calculated_size > metadata_resource_len - offset) {
1124 ERROR("Not enough space in metadata resource for "
1125 "alternate stream entries");
1126 ret = WIMLIB_ERR_INVALID_DENTRY;
1127 goto out_free_short_name;
1129 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1131 metadata_resource_len - offset - calculated_size);
1133 goto out_free_short_name;
1136 dentry->file_name = file_name;
1137 dentry->file_name_utf8 = file_name_utf8;
1138 dentry->short_name = short_name;
1139 dentry->file_name_len = file_name_len;
1140 dentry->file_name_utf8_len = file_name_utf8_len;
1141 dentry->short_name_len = short_name_len;
1143 out_free_short_name:
1145 out_free_file_name_utf8:
1146 FREE(file_name_utf8);
1153 * Writes a dentry to an output buffer.
1155 * @dentry: The dentry structure.
1156 * @p: The memory location to write the data to.
1157 * @return: Pointer to the byte after the last byte we wrote as part of the
1160 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1165 p = put_u64(p, dentry->length);
1166 p = put_u32(p, dentry->attributes);
1167 p = put_u32(p, dentry->security_id);
1168 p = put_u64(p, dentry->subdir_offset);
1169 p = put_u64(p, 0); /* unused1 */
1170 p = put_u64(p, 0); /* unused2 */
1171 p = put_u64(p, dentry->creation_time);
1172 p = put_u64(p, dentry->last_access_time);
1173 p = put_u64(p, dentry->last_write_time);
1174 p = put_bytes(p, WIM_HASH_SIZE, dentry->hash);
1175 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1176 p = put_zeroes(p, 4);
1177 p = put_u32(p, dentry->reparse_tag);
1178 p = put_zeroes(p, 4);
1181 p = put_u32(p, dentry->reparse_tag);
1182 if (dentry->link_group_list.next == &dentry->link_group_list)
1185 hard_link = dentry->hard_link;
1186 p = put_u64(p, hard_link);
1188 p = put_u16(p, dentry->num_ads);
1189 p = put_u16(p, dentry->short_name_len);
1190 p = put_u16(p, dentry->file_name_len);
1191 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1192 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1193 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1195 wimlib_assert(p - orig_p <= dentry->length);
1196 if (p - orig_p < dentry->length)
1197 p = put_zeroes(p, dentry->length - (p - orig_p));
1199 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1201 for (u16 i = 0; i < dentry->num_ads; i++) {
1202 p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
1203 p = put_u64(p, 0); /* Unused */
1204 p = put_bytes(p, WIM_HASH_SIZE, dentry->ads_entries[i].hash);
1205 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
1206 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
1207 (u8*)dentry->ads_entries[i].stream_name);
1208 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1213 /* Recursive function that writes a dentry tree rooted at @tree, not including
1214 * @tree itself, which has already been written, except in the case of the root
1215 * dentry, which is written right away, along with an end-of-directory entry. */
1216 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
1218 const struct dentry *child;
1220 if (dentry_is_root(tree)) {
1221 p = write_dentry(tree, p);
1223 /* write end of directory entry */
1226 /* Nothing to do for non-directories */
1227 if (!dentry_is_directory(tree))
1231 /* Write child dentries and end-of-directory entry. */
1232 child = tree->children;
1235 p = write_dentry(child, p);
1236 child = child->next;
1237 } while (child != tree->children);
1240 /* write end of directory entry */
1243 /* Recurse on children. */
1246 p = write_dentry_tree(child, p);
1247 child = child->next;
1248 } while (child != tree->children);
1253 /* Reads the children of a dentry, and all their children, ..., etc. from the
1254 * metadata resource and into the dentry tree.
1256 * @metadata_resource: An array that contains the uncompressed metadata
1257 * resource for the WIM file.
1258 * @metadata_resource_len: The length of @metadata_resource.
1259 * @dentry: A pointer to a struct dentry that is the root of the directory
1260 * tree and has already been read from the metadata resource. It
1261 * does not need to be the real root because this procedure is
1262 * called recursively.
1264 * @return: Zero on success, nonzero on failure.
1266 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1267 struct dentry *dentry)
1269 u64 cur_offset = dentry->subdir_offset;
1270 struct dentry *prev_child = NULL;
1271 struct dentry *first_child = NULL;
1272 struct dentry *child;
1273 struct dentry cur_child;
1276 /* If @dentry is a regular file, nothing more needs to be done for this
1278 if (cur_offset == 0)
1281 /* Find and read all the children of @dentry. */
1284 /* Read next child of @dentry into @cur_child. */
1285 ret = read_dentry(metadata_resource, metadata_resource_len,
1286 cur_offset, &cur_child);
1290 /* Check for end of directory. */
1291 if (cur_child.length == 0) {
1296 /* Not end of directory. Allocate this child permanently and
1297 * link it to the parent and previous child. */
1298 child = MALLOC(sizeof(struct dentry));
1300 ERROR("Failed to allocate %zu bytes for new dentry",
1301 sizeof(struct dentry));
1302 ret = WIMLIB_ERR_NOMEM;
1305 memcpy(child, &cur_child, sizeof(struct dentry));
1308 prev_child->next = child;
1309 child->prev = prev_child;
1311 first_child = child;
1314 child->parent = dentry;
1317 /* If there are children of this child, call this procedure
1319 if (child->subdir_offset != 0) {
1320 ret = read_dentry_tree(metadata_resource,
1321 metadata_resource_len, child);
1326 /* Advance to the offset of the next child. */
1327 cur_offset += dentry_total_length(child);
1330 /* Link last child to first one, and set parent's
1331 * children pointer to the first child. */
1333 prev_child->next = first_child;
1334 first_child->prev = prev_child;
1336 dentry->children = first_child;