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));
83 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
84 int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
85 const struct lookup_table *table)
87 struct lookup_table_entry *lte;
89 if (dentry_is_symlink(dentry))
90 stbuf->st_mode = S_IFLNK | 0777;
91 else if (dentry_is_directory(dentry))
92 stbuf->st_mode = S_IFDIR | 0755;
94 stbuf->st_mode = S_IFREG | 0644;
96 stbuf->st_ino = (ino_t)dentry->hard_link;
98 stbuf->st_nlink = dentry_link_group_size(dentry);
99 stbuf->st_uid = getuid();
100 stbuf->st_gid = getgid();
102 /* Use the size of the unnamed (default) file stream. */
103 if (table && (lte = __lookup_resource(table, dentry_hash(dentry)))) {
104 if (lte->staging_file_name) {
105 struct stat native_stat;
106 if (stat(lte->staging_file_name, &native_stat) != 0)
108 stbuf->st_size = native_stat.st_size;
110 stbuf->st_size = lte->resource_entry.original_size;
116 stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
117 stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
118 stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
119 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
123 /* Makes all timestamp fields for the dentry be the current time. */
124 void dentry_update_all_timestamps(struct dentry *dentry)
126 u64 now = get_timestamp();
127 dentry->creation_time = now;
128 dentry->last_access_time = now;
129 dentry->last_write_time = now;
132 struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
133 const char *stream_name)
135 size_t stream_name_len = strlen(stream_name);
138 for (u16 i = 0; i < dentry->num_ads; i++)
139 if (ads_entry_has_name(&dentry->ads_entries[i],
140 stream_name, stream_name_len))
141 return &dentry->ads_entries[i];
145 /* Add an alternate stream entry to a dentry and return a pointer to it, or NULL
147 struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
149 u16 num_ads = dentry->num_ads + 1;
150 struct ads_entry *ads_entries;
151 struct ads_entry *new_entry;
152 if (num_ads == 0xffff)
154 ads_entries = MALLOC(num_ads * sizeof(struct ads_entry));
158 new_entry = &ads_entries[num_ads - 1];
159 if (change_ads_name(new_entry, stream_name) != 0) {
164 memcpy(ads_entries, dentry->ads_entries,
165 (num_ads - 1) * sizeof(struct ads_entry));
166 FREE(dentry->ads_entries);
167 dentry->ads_entries = ads_entries;
168 dentry->num_ads = num_ads;
169 return memset(new_entry, 0, sizeof(struct ads_entry));
172 void dentry_remove_ads(struct dentry *dentry, struct ads_entry *sentry)
174 destroy_ads_entry(sentry);
175 memcpy(sentry, sentry + 1,
176 (dentry->num_ads - (sentry - dentry->ads_entries + 1))
177 * sizeof(struct ads_entry));
182 * Calls a function on all directory entries in a directory tree. It is called
183 * on a parent before its children.
185 int for_dentry_in_tree(struct dentry *root,
186 int (*visitor)(struct dentry*, void*), void *arg)
189 struct dentry *child;
191 ret = visitor(root, arg);
196 child = root->children;
202 ret = for_dentry_in_tree(child, visitor, arg);
206 } while (child != root->children);
211 * Like for_dentry_in_tree(), but the visitor function is always called on a
212 * dentry's children before on itself.
214 int for_dentry_in_tree_depth(struct dentry *root,
215 int (*visitor)(struct dentry*, void*), void *arg)
218 struct dentry *child;
221 child = root->children;
225 ret = for_dentry_in_tree_depth(child, visitor, arg);
229 } while (child != root->children);
231 return visitor(root, arg);
235 * Calculate the full path of @dentry, based on its parent's full path and on
236 * its UTF-8 file name.
238 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
242 if (dentry_is_root(dentry)) {
243 full_path = MALLOC(2);
250 char *parent_full_path;
251 u32 parent_full_path_len;
252 const struct dentry *parent = dentry->parent;
254 if (dentry_is_root(parent)) {
255 parent_full_path = "";
256 parent_full_path_len = 0;
258 parent_full_path = parent->full_path_utf8;
259 parent_full_path_len = parent->full_path_utf8_len;
262 full_path_len = parent_full_path_len + 1 +
263 dentry->file_name_utf8_len;
264 full_path = MALLOC(full_path_len + 1);
268 memcpy(full_path, parent_full_path, parent_full_path_len);
269 full_path[parent_full_path_len] = '/';
270 memcpy(full_path + parent_full_path_len + 1,
271 dentry->file_name_utf8,
272 dentry->file_name_utf8_len);
273 full_path[full_path_len] = '\0';
275 FREE(dentry->full_path_utf8);
276 dentry->full_path_utf8 = full_path;
277 dentry->full_path_utf8_len = full_path_len;
280 ERROR("Out of memory while calculating dentry full path");
281 return WIMLIB_ERR_NOMEM;
285 * Recursively calculates the subdir offsets for a directory tree.
287 * @dentry: The root of the directory tree.
288 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
289 * offset for @dentry.
291 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
293 struct dentry *child;
295 child = dentry->children;
296 dentry->subdir_offset = *subdir_offset_p;
299 /* Advance the subdir offset by the amount of space the children
300 * of this dentry take up. */
302 *subdir_offset_p += dentry_total_length(child);
304 } while (child != dentry->children);
306 /* End-of-directory dentry on disk. */
307 *subdir_offset_p += 8;
309 /* Recursively call calculate_subdir_offsets() on all the
312 calculate_subdir_offsets(child, subdir_offset_p);
314 } while (child != dentry->children);
316 /* On disk, childless directories have a valid subdir_offset
317 * that points to an 8-byte end-of-directory dentry. Regular
318 * files have a subdir_offset of 0. */
319 if (dentry_is_directory(dentry))
320 *subdir_offset_p += 8;
322 dentry->subdir_offset = 0;
327 /* Returns the child of @dentry that has the file name @name.
328 * Returns NULL if no child has the name. */
329 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
332 struct dentry *child;
335 child = dentry->children;
337 name_len = strlen(name);
339 if (dentry_has_name(child, name, name_len))
342 } while (child != dentry->children);
347 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
348 * @cur_dir. Returns NULL if no dentry having the path is found. */
349 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
351 struct dentry *child;
353 const char *new_path;
358 child = cur_dir->children;
360 new_path = path_next_part(path, &base_len);
362 if (dentry_has_name(child, path, base_len))
363 return get_dentry_relative_path(child, new_path);
365 } while (child != cur_dir->children);
370 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
372 struct dentry *get_dentry(WIMStruct *w, const char *path)
374 struct dentry *root = wim_root_dentry(w);
377 return get_dentry_relative_path(root, path);
380 /* Returns the parent directory for the @path. */
381 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
383 size_t path_len = strlen(path);
384 char buf[path_len + 1];
386 memcpy(buf, path, path_len + 1);
388 to_parent_name(buf, path_len);
390 return get_dentry(w, buf);
393 /* Prints the full path of a dentry. */
394 int print_dentry_full_path(struct dentry *dentry, void *ignore)
396 if (dentry->full_path_utf8)
397 puts(dentry->full_path_utf8);
401 struct file_attr_flag {
405 struct file_attr_flag file_attr_flags[] = {
406 {FILE_ATTRIBUTE_READONLY, "READONLY"},
407 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
408 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
409 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
410 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
411 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
412 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
413 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
414 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
415 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
416 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
417 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
418 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
419 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
420 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
423 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
424 * NULL if the resource entry for the dentry is not to be printed. */
425 int print_dentry(struct dentry *dentry, void *lookup_table)
427 struct lookup_table_entry *lte;
430 printf("[DENTRY]\n");
431 printf("Length = %"PRIu64"\n", dentry->length);
432 printf("Attributes = 0x%x\n", dentry->attributes);
433 for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
434 if (file_attr_flags[i].flag & dentry->attributes)
435 printf(" FILE_ATTRIBUTE_%s is set\n",
436 file_attr_flags[i].name);
437 printf("Security ID = %d\n", dentry->security_id);
438 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
439 /*printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);*/
440 /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
441 printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
442 printf("Last Access Time = 0x%"PRIx64"\n", dentry->last_access_time);
443 printf("Last Write Time = 0x%"PRIx64"\n", dentry->last_write_time);
445 print_hash(dentry->hash);
447 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
448 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
449 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
450 printf("Filename = \"");
451 print_string(dentry->file_name, dentry->file_name_len);
453 printf("Filename Length = %hu\n", dentry->file_name_len);
454 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
455 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
456 printf("Short Name = \"");
457 print_string(dentry->short_name, dentry->short_name_len);
459 printf("Short Name Length = %hu\n", dentry->short_name_len);
460 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
461 if (lookup_table && (lte = __lookup_resource(lookup_table, dentry->hash)))
462 print_lookup_table_entry(lte, NULL);
465 for (u16 i = 0; i < dentry->num_ads; i++) {
466 printf("[Alternate Stream Entry %u]\n", i);
467 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
468 printf("Name Length (UTF-16) = %u\n",
469 dentry->ads_entries[i].stream_name_len);
471 print_hash(dentry->ads_entries[i].hash);
473 (lte = __lookup_resource(lookup_table,
474 dentry->ads_entries[i].hash)))
476 print_lookup_table_entry(lte, NULL);
484 static inline void dentry_common_init(struct dentry *dentry)
486 memset(dentry, 0, sizeof(struct dentry));
488 dentry->security_id = -1;
489 dentry->link_group_master_status = GROUP_SLAVE;
493 * Creates an unlinked directory entry.
495 * @name: The base name of the new dentry.
496 * @return: A pointer to the new dentry, or NULL if out of memory.
498 struct dentry *new_dentry(const char *name)
500 struct dentry *dentry;
502 dentry = MALLOC(sizeof(struct dentry));
506 dentry_common_init(dentry);
507 if (change_dentry_name(dentry, name) != 0)
510 dentry_update_all_timestamps(dentry);
511 dentry->next = dentry;
512 dentry->prev = dentry;
513 dentry->parent = dentry;
514 INIT_LIST_HEAD(&dentry->link_group_list);
518 ERROR("Failed to allocate new dentry");
522 void dentry_free_ads_entries(struct dentry *dentry)
524 for (u16 i = 0; i < dentry->num_ads; i++)
525 destroy_ads_entry(&dentry->ads_entries[i]);
526 FREE(dentry->ads_entries);
527 dentry->ads_entries = NULL;
531 static void __destroy_dentry(struct dentry *dentry)
533 FREE(dentry->file_name);
534 FREE(dentry->file_name_utf8);
535 FREE(dentry->short_name);
536 FREE(dentry->full_path_utf8);
537 FREE(dentry->extracted_file);
540 void free_dentry(struct dentry *dentry)
542 __destroy_dentry(dentry);
543 if (dentry->link_group_master_status != GROUP_SLAVE)
544 dentry_free_ads_entries(dentry);
548 void put_dentry(struct dentry *dentry)
550 if (dentry->link_group_master_status == GROUP_MASTER) {
551 struct dentry *new_master;
552 list_for_each_entry(new_master, &dentry->link_group_list,
555 if (new_master->link_group_master_status == GROUP_SLAVE) {
556 new_master->link_group_master_status = GROUP_MASTER;
557 dentry->link_group_master_status = GROUP_SLAVE;
562 struct list_head *next;
563 next = dentry->link_group_list.next;
564 list_del(&dentry->link_group_list);
565 /*if (next->next == next)*/
566 /*container_of(next, struct dentry, link_group_list)->hard_link = 0;*/
570 static bool dentries_have_same_ads(const struct dentry *d1,
571 const struct dentry *d2)
573 /* Verify stream names and hashes are the same */
574 for (u16 i = 0; i < d1->num_ads; i++) {
575 if (strcmp(d1->ads_entries[i].stream_name_utf8,
576 d2->ads_entries[i].stream_name_utf8) != 0)
578 if (memcmp(d1->ads_entries[i].hash,
579 d2->ads_entries[i].hash,
586 /* Share the alternate stream entries between hard-linked dentries. */
587 int share_dentry_ads(struct dentry *master, struct dentry *slave)
589 const char *mismatch_type;
590 wimlib_assert(master->num_ads == 0 ||
591 master->ads_entries != slave->ads_entries);
592 if (master->attributes != slave->attributes) {
593 mismatch_type = "attributes";
596 if (master->attributes & FILE_ATTRIBUTE_DIRECTORY) {
597 WARNING("`%s' is hard-linked to `%s', which is a directory ",
598 slave->full_path_utf8, master->full_path_utf8);
599 return WIMLIB_ERR_INVALID_DENTRY;
601 if (master->security_id != slave->security_id) {
602 mismatch_type = "security ID";
605 if (memcmp(master->hash, slave->hash, WIM_HASH_SIZE) != 0) {
606 mismatch_type = "main file resource";
609 if (!dentries_have_same_ads(master, slave)) {
610 mismatch_type = "Alternate Stream Entries";
613 dentry_free_ads_entries(slave);
614 slave->ads_entries = master->ads_entries;
615 slave->link_group_master_status = GROUP_SLAVE;
618 WARNING("Dentries `%s' and `%s' in the same hard-link group but "
619 "do not share the same %s",
620 master->full_path_utf8, slave->full_path_utf8,
622 return WIMLIB_ERR_INVALID_DENTRY;
628 * - memory for file names is not cloned
629 * - next, prev, and children pointers and not touched
630 * - stream entries are not cloned.
632 struct dentry *clone_dentry(struct dentry *old)
634 struct dentry *new = MALLOC(sizeof(struct dentry));
637 memcpy(new, old, sizeof(struct dentry));
638 new->file_name = NULL;
639 new->file_name_len = 0;
640 new->file_name_utf8 = NULL;
641 new->file_name_utf8_len = 0;
642 new->short_name = NULL;
643 new->short_name_len = 0;
647 /* Arguments for do_free_dentry(). */
648 struct free_dentry_args {
649 struct lookup_table *lookup_table;
650 bool lt_decrement_refcnt;
654 * This function is passed as an argument to for_dentry_in_tree_depth() in order
655 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
657 static int do_free_dentry(struct dentry *dentry, void *__args)
659 struct free_dentry_args *args = (struct free_dentry_args*)__args;
661 if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
662 lookup_table_decrement_refcnt(args->lookup_table,
666 wimlib_assert(dentry->refcnt >= 1);
667 if (--dentry->refcnt == 0)
673 * Unlinks and frees a dentry tree.
675 * @root: The root of the tree.
676 * @lookup_table: The lookup table for dentries.
677 * @decrement_refcnt: True if the dentries in the tree are to have their
678 * reference counts in the lookup table decremented.
680 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table,
681 bool lt_decrement_refcnt)
683 if (!root || !root->parent)
686 struct free_dentry_args args;
687 args.lookup_table = lookup_table;
688 args.lt_decrement_refcnt = lt_decrement_refcnt;
689 for_dentry_in_tree_depth(root, do_free_dentry, &args);
692 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
699 * Links a dentry into the directory tree.
701 * @dentry: The dentry to link.
702 * @parent: The dentry that will be the parent of @dentry.
704 void link_dentry(struct dentry *dentry, struct dentry *parent)
706 dentry->parent = parent;
707 if (parent->children) {
708 /* Not an only child; link to siblings. */
709 dentry->next = parent->children;
710 dentry->prev = parent->children->prev;
711 dentry->next->prev = dentry;
712 dentry->prev->next = dentry;
714 /* Only child; link to parent. */
715 parent->children = dentry;
716 dentry->next = dentry;
717 dentry->prev = dentry;
722 /* Unlink a dentry from the directory tree.
724 * Note: This merely removes it from the in-memory tree structure. See
725 * remove_dentry() in mount.c for a function implemented on top of this one that
726 * frees the dentry and implements reference counting for the lookup table
728 void unlink_dentry(struct dentry *dentry)
730 if (dentry_is_root(dentry))
732 if (dentry_is_only_child(dentry)) {
733 dentry->parent->children = NULL;
735 if (dentry_is_first_sibling(dentry))
736 dentry->parent->children = dentry->next;
737 dentry->next->prev = dentry->prev;
738 dentry->prev->next = dentry->next;
743 /* Recalculates the length of @dentry based on its file name length and short
745 static inline void recalculate_dentry_size(struct dentry *dentry)
747 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
748 2 + dentry->short_name_len;
749 /* Must be multiple of 8. */
750 dentry->length = (dentry->length + 7) & ~7;
753 int get_names(char **name_utf16_ret, char **name_utf8_ret,
754 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
759 char *name_utf16, *name_utf8;
761 utf8_len = strlen(name);
763 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
766 return WIMLIB_ERR_NOMEM;
768 name_utf8 = MALLOC(utf8_len + 1);
771 return WIMLIB_ERR_NOMEM;
773 memcpy(name_utf8, name, utf8_len + 1);
774 FREE(*name_utf8_ret);
775 FREE(*name_utf16_ret);
776 *name_utf8_ret = name_utf8;
777 *name_utf16_ret = name_utf16;
778 *name_utf8_len_ret = utf8_len;
779 *name_utf16_len_ret = utf16_len;
783 /* Changes the name of a dentry to @new_name. Only changes the file_name and
784 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
785 * full_path_utf8 fields. Also recalculates its length. */
786 int change_dentry_name(struct dentry *dentry, const char *new_name)
790 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
791 &dentry->file_name_len, &dentry->file_name_utf8_len,
794 recalculate_dentry_size(dentry);
798 int change_ads_name(struct ads_entry *entry, const char *new_name)
800 return get_names(&entry->stream_name, &entry->stream_name_utf8,
801 &entry->stream_name_len,
802 &entry->stream_name_utf8_len,
806 /* Parameters for calculate_dentry_statistics(). */
807 struct image_statistics {
808 struct lookup_table *lookup_table;
812 u64 *hard_link_bytes;
815 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
817 struct image_statistics *stats;
818 struct lookup_table_entry *lte;
823 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
826 ++*stats->file_count;
828 lte = __lookup_resource(stats->lookup_table, dentry->hash);
832 u64 size = lte->resource_entry.original_size;
833 *stats->total_bytes += size;
834 if (++lte->out_refcnt == 1)
835 *stats->hard_link_bytes += size;
837 if (i == dentry->num_ads)
839 lte = __lookup_resource(stats->lookup_table,
840 dentry->ads_entries[i].hash);
847 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
848 u64 *dir_count_ret, u64 *file_count_ret,
849 u64 *total_bytes_ret,
850 u64 *hard_link_bytes_ret)
852 struct image_statistics stats;
855 *total_bytes_ret = 0;
856 *hard_link_bytes_ret = 0;
857 stats.lookup_table = table;
858 stats.dir_count = dir_count_ret;
859 stats.file_count = file_count_ret;
860 stats.total_bytes = total_bytes_ret;
861 stats.hard_link_bytes = hard_link_bytes_ret;
862 for_lookup_table_entry(table, zero_out_refcnts, NULL);
863 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
866 static int read_ads_entries(const u8 *p, struct dentry *dentry,
869 u16 num_ads = dentry->num_ads;
870 struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
873 ERROR("Could not allocate memory for %"PRIu16" "
874 "alternate data stream entries", num_ads);
875 return WIMLIB_ERR_NOMEM;
877 DEBUG2("Reading %"PRIu16" alternate data streams "
878 "(remaining size = %"PRIu64")", num_ads, remaining_size);
880 for (u16 i = 0; i < num_ads; i++) {
881 struct ads_entry *cur_entry = &ads_entries[i];
884 const char *p_save = p;
885 /* Read the base stream entry, excluding the stream name. */
886 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
887 ERROR("Stream entries go past end of metadata resource");
888 ERROR("(remaining_size = %"PRIu64")", remaining_size);
889 ret = WIMLIB_ERR_INVALID_DENTRY;
890 goto out_free_ads_entries;
892 remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
894 p = get_u64(p, &length); /* ADS entry length */
896 DEBUG2("ADS length = %"PRIu64, length);
899 p = get_bytes(p, WIM_HASH_SIZE, (u8*)cur_entry->hash);
900 p = get_u16(p, &cur_entry->stream_name_len);
902 DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
904 cur_entry->stream_name = NULL;
905 cur_entry->stream_name_utf8 = NULL;
907 if (remaining_size < cur_entry->stream_name_len + 2) {
908 ERROR("Stream entries go past end of metadata resource");
909 ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
910 "= %"PRIu16" bytes", remaining_size,
911 cur_entry->stream_name_len);
912 ret = WIMLIB_ERR_INVALID_DENTRY;
913 goto out_free_ads_entries;
915 remaining_size -= cur_entry->stream_name_len + 2;
917 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
918 if (!cur_entry->stream_name) {
919 ret = WIMLIB_ERR_NOMEM;
920 goto out_free_ads_entries;
922 get_bytes(p, cur_entry->stream_name_len,
923 (u8*)cur_entry->stream_name);
924 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
925 cur_entry->stream_name_len,
927 cur_entry->stream_name_utf8_len = utf8_len;
929 if (!cur_entry->stream_name_utf8) {
930 ret = WIMLIB_ERR_NOMEM;
931 goto out_free_ads_entries;
933 p = p_save + ads_entry_length(cur_entry);
935 dentry->ads_entries = ads_entries;
937 out_free_ads_entries:
938 for (u16 i = 0; i < num_ads; i++) {
939 FREE(ads_entries[i].stream_name);
940 FREE(ads_entries[i].stream_name_utf8);
947 * Reads a directory entry from the metadata resource.
949 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
950 u64 offset, struct dentry *dentry)
955 char *file_name_utf8;
959 size_t file_name_utf8_len;
962 dentry_common_init(dentry);
964 /*Make sure the dentry really fits into the metadata resource.*/
965 if (offset + 8 > metadata_resource_len) {
966 ERROR("Directory entry starting at %"PRIu64" ends past the "
967 "end of the metadata resource (size %"PRIu64")",
968 offset, metadata_resource_len);
969 return WIMLIB_ERR_INVALID_DENTRY;
972 /* Before reading the whole entry, we need to read just the length.
973 * This is because an entry of length 8 (that is, just the length field)
974 * terminates the list of sibling directory entries. */
976 p = get_u64(&metadata_resource[offset], &dentry->length);
978 /* A zero length field (really a length of 8, since that's how big the
979 * directory entry is...) indicates that this is the end of directory
980 * dentry. We do not read it into memory as an actual dentry, so just
981 * return true in that case. */
982 if (dentry->length == 0)
985 if (offset + dentry->length >= metadata_resource_len) {
986 ERROR("Directory entry at offset %"PRIu64" and with size "
987 "%"PRIu64" ends past the end of the metadata resource "
989 offset, dentry->length, metadata_resource_len);
990 return WIMLIB_ERR_INVALID_DENTRY;
993 /* If it is a recognized length, read the rest of the directory entry.
994 * Note: The root directory entry has no name, and its length does not
995 * include the short name length field. */
996 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
997 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
999 return WIMLIB_ERR_INVALID_DENTRY;
1002 p = get_u32(p, &dentry->attributes);
1003 p = get_u32(p, (u32*)&dentry->security_id);
1004 p = get_u64(p, &dentry->subdir_offset);
1006 /* 2 unused fields */
1007 p += 2 * sizeof(u64);
1008 /*p = get_u64(p, &dentry->unused1);*/
1009 /*p = get_u64(p, &dentry->unused2);*/
1011 p = get_u64(p, &dentry->creation_time);
1012 p = get_u64(p, &dentry->last_access_time);
1013 p = get_u64(p, &dentry->last_write_time);
1015 p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
1018 * I don't know what's going on here. It seems like M$ screwed up the
1019 * reparse points, then put the fields in the same place and didn't
1020 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1021 * have something to do with this, but it's not documented.
1023 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1026 p = get_u32(p, &dentry->reparse_tag);
1029 p = get_u32(p, &dentry->reparse_tag);
1030 p = get_u64(p, &dentry->hard_link);
1033 /* By the way, the reparse_reserved field does not actually exist (at
1034 * least when the file is not a reparse point) */
1037 p = get_u16(p, &dentry->num_ads);
1039 p = get_u16(p, &short_name_len);
1040 p = get_u16(p, &file_name_len);
1042 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
1045 if (dentry->length < calculated_size) {
1046 ERROR("Unexpected end of directory entry! (Expected "
1047 "%"PRIu64" bytes, got %"PRIu64" bytes. "
1048 "short_name_len = %hu, file_name_len = %hu)",
1049 calculated_size, dentry->length,
1050 short_name_len, file_name_len);
1051 return WIMLIB_ERR_INVALID_DENTRY;
1054 /* Read the filename. */
1055 file_name = MALLOC(file_name_len);
1057 ERROR("Failed to allocate %hu bytes for dentry file name",
1059 return WIMLIB_ERR_NOMEM;
1061 p = get_bytes(p, file_name_len, file_name);
1063 /* Convert filename to UTF-8. */
1064 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1065 &file_name_utf8_len);
1067 if (!file_name_utf8) {
1068 ERROR("Failed to allocate memory to convert UTF-16 "
1069 "filename (%hu bytes) to UTF-8", file_name_len);
1070 ret = WIMLIB_ERR_NOMEM;
1071 goto out_free_file_name;
1074 /* Undocumented padding between file name and short name. This probably
1075 * is supposed to be a terminating null character. */
1078 /* Read the short filename. */
1079 short_name = MALLOC(short_name_len);
1081 ERROR("Failed to allocate %hu bytes for short filename",
1083 ret = WIMLIB_ERR_NOMEM;
1084 goto out_free_file_name_utf8;
1087 p = get_bytes(p, short_name_len, short_name);
1089 /* Some directory entries inexplicibly have a little over 70 bytes of
1090 * extra data. The exact amount of data seems to be 72 bytes, but it is
1091 * aligned on the next 8-byte boundary. Here's an example of the
1094 * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
1095 * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
1096 * 0000000000000000000000
1098 * Here's one interpretation of how the data is laid out.
1101 * u32 field1; (always 0x00000001)
1102 * u32 field2; (always 0x40000000)
1107 * u8 data[48]; (???)
1108 * u64 reserved1; (always 0)
1109 * u64 reserved2; (always 0)
1112 if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
1113 printf("%s: %lu / %lu (", file_name_utf8,
1114 calculated_size, dentry->length);
1115 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
1117 print_byte_field(p, dentry->length - calculated_size);
1122 if (dentry->num_ads != 0) {
1123 calculated_size = (calculated_size + 7) & ~7;
1124 if (calculated_size > metadata_resource_len - offset) {
1125 ERROR("Not enough space in metadata resource for "
1126 "alternate stream entries");
1127 ret = WIMLIB_ERR_INVALID_DENTRY;
1128 goto out_free_short_name;
1130 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1132 metadata_resource_len - offset - calculated_size);
1134 goto out_free_short_name;
1137 dentry->file_name = file_name;
1138 dentry->file_name_utf8 = file_name_utf8;
1139 dentry->short_name = short_name;
1140 dentry->file_name_len = file_name_len;
1141 dentry->file_name_utf8_len = file_name_utf8_len;
1142 dentry->short_name_len = short_name_len;
1144 out_free_short_name:
1146 out_free_file_name_utf8:
1147 FREE(file_name_utf8);
1154 * Writes a dentry to an output buffer.
1156 * @dentry: The dentry structure.
1157 * @p: The memory location to write the data to.
1158 * @return: Pointer to the byte after the last byte we wrote as part of the
1161 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1166 p = put_u64(p, dentry->length);
1167 p = put_u32(p, dentry->attributes);
1168 p = put_u32(p, dentry->security_id);
1169 p = put_u64(p, dentry->subdir_offset);
1170 p = put_u64(p, 0); /* unused1 */
1171 p = put_u64(p, 0); /* unused2 */
1172 p = put_u64(p, dentry->creation_time);
1173 p = put_u64(p, dentry->last_access_time);
1174 p = put_u64(p, dentry->last_write_time);
1175 p = put_bytes(p, WIM_HASH_SIZE, dentry->hash);
1176 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1177 p = put_zeroes(p, 4);
1178 p = put_u32(p, dentry->reparse_tag);
1179 p = put_zeroes(p, 4);
1182 p = put_u32(p, dentry->reparse_tag);
1183 if (dentry->link_group_list.next == &dentry->link_group_list)
1186 hard_link = dentry->hard_link;
1187 p = put_u64(p, hard_link);
1189 p = put_u16(p, dentry->num_ads);
1190 p = put_u16(p, dentry->short_name_len);
1191 p = put_u16(p, dentry->file_name_len);
1192 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1193 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1194 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1196 wimlib_assert(p - orig_p <= dentry->length);
1197 if (p - orig_p < dentry->length)
1198 p = put_zeroes(p, dentry->length - (p - orig_p));
1200 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1202 for (u16 i = 0; i < dentry->num_ads; i++) {
1203 p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
1204 p = put_u64(p, 0); /* Unused */
1205 p = put_bytes(p, WIM_HASH_SIZE, dentry->ads_entries[i].hash);
1206 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
1207 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
1208 (u8*)dentry->ads_entries[i].stream_name);
1209 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1214 /* Recursive function that writes a dentry tree rooted at @tree, not including
1215 * @tree itself, which has already been written, except in the case of the root
1216 * dentry, which is written right away, along with an end-of-directory entry. */
1217 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
1219 const struct dentry *child;
1221 if (dentry_is_root(tree)) {
1222 p = write_dentry(tree, p);
1224 /* write end of directory entry */
1227 /* Nothing to do for non-directories */
1228 if (!dentry_is_directory(tree))
1232 /* Write child dentries and end-of-directory entry. */
1233 child = tree->children;
1236 p = write_dentry(child, p);
1237 child = child->next;
1238 } while (child != tree->children);
1241 /* write end of directory entry */
1244 /* Recurse on children. */
1247 p = write_dentry_tree(child, p);
1248 child = child->next;
1249 } while (child != tree->children);
1254 /* Reads the children of a dentry, and all their children, ..., etc. from the
1255 * metadata resource and into the dentry tree.
1257 * @metadata_resource: An array that contains the uncompressed metadata
1258 * resource for the WIM file.
1259 * @metadata_resource_len: The length of @metadata_resource.
1260 * @dentry: A pointer to a struct dentry that is the root of the directory
1261 * tree and has already been read from the metadata resource. It
1262 * does not need to be the real root because this procedure is
1263 * called recursively.
1265 * @return: Zero on success, nonzero on failure.
1267 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1268 struct dentry *dentry)
1270 u64 cur_offset = dentry->subdir_offset;
1271 struct dentry *prev_child = NULL;
1272 struct dentry *first_child = NULL;
1273 struct dentry *child;
1274 struct dentry cur_child;
1277 /* If @dentry is a regular file, nothing more needs to be done for this
1279 if (cur_offset == 0)
1282 /* Find and read all the children of @dentry. */
1285 /* Read next child of @dentry into @cur_child. */
1286 ret = read_dentry(metadata_resource, metadata_resource_len,
1287 cur_offset, &cur_child);
1291 /* Check for end of directory. */
1292 if (cur_child.length == 0) {
1297 /* Not end of directory. Allocate this child permanently and
1298 * link it to the parent and previous child. */
1299 child = MALLOC(sizeof(struct dentry));
1301 ERROR("Failed to allocate %zu bytes for new dentry",
1302 sizeof(struct dentry));
1303 ret = WIMLIB_ERR_NOMEM;
1306 memcpy(child, &cur_child, sizeof(struct dentry));
1309 prev_child->next = child;
1310 child->prev = prev_child;
1312 first_child = child;
1315 child->parent = dentry;
1318 /* If there are children of this child, call this procedure
1320 if (child->subdir_offset != 0) {
1321 ret = read_dentry_tree(metadata_resource,
1322 metadata_resource_len, child);
1327 /* Advance to the offset of the next child. */
1328 cur_offset += dentry_total_length(child);
1331 /* Link last child to first one, and set parent's
1332 * children pointer to the first child. */
1334 prev_child->next = first_child;
1335 first_child->prev = prev_child;
1337 dentry->children = first_child;