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/.
40 #include "lookup_table.h"
42 #include "timestamp.h"
43 #include "wimlib_internal.h"
46 * Returns true if @dentry has the UTF-8 file name @name that has length
49 static bool dentry_has_name(const struct dentry *dentry, const char *name,
52 if (dentry->file_name_utf8_len != name_len)
54 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
57 /* Real length of a dentry, including the alternate data stream entries, which
58 * are not included in the dentry->length field... */
59 u64 dentry_total_length(const struct dentry *dentry)
61 u64 length = (dentry->length + 7) & ~7;
62 for (u16 i = 0; i < dentry->num_ads; i++)
63 length += ads_entry_total_length(&dentry->ads_entries[i]);
67 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
68 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
70 if (S_ISLNK(stbuf->st_mode)) {
71 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
72 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
73 } else if (S_ISDIR(stbuf->st_mode)) {
74 dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
76 dentry->attributes = FILE_ATTRIBUTE_NORMAL;
78 if (sizeof(ino_t) >= 8)
79 dentry->hard_link = (u64)stbuf->st_ino;
81 dentry->hard_link = (u64)stbuf->st_ino |
82 ((u64)stbuf->st_dev << (sizeof(ino_t) * 8));
84 dentry->creation_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
85 dentry->last_write_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
86 dentry->last_access_time = timespec_to_wim_timestamp(&stbuf->st_atim);
90 /* Sets all the timestamp fields of the dentry to the current time. */
91 void dentry_update_all_timestamps(struct dentry *dentry)
93 u64 now = get_wim_timestamp();
94 dentry->creation_time = now;
95 dentry->last_access_time = now;
96 dentry->last_write_time = now;
99 /* Returns the alternate data stream entry belonging to @dentry that has the
100 * stream name @stream_name. */
101 struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
102 const char *stream_name)
104 size_t stream_name_len;
107 if (dentry->num_ads) {
109 stream_name_len = strlen(stream_name);
111 if (ads_entry_has_name(&dentry->ads_entries[i],
112 stream_name, stream_name_len))
113 return &dentry->ads_entries[i];
114 } while (++i != dentry->num_ads);
119 static void ads_entry_init(struct ads_entry *ads_entry)
121 memset(ads_entry, 0, sizeof(struct ads_entry));
122 INIT_LIST_HEAD(&ads_entry->lte_group_list.list);
123 ads_entry->lte_group_list.type = STREAM_TYPE_ADS;
127 * Add an alternate stream entry to a dentry and return a pointer to it, or NULL
128 * if memory could not be allocated.
130 struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
133 struct ads_entry *ads_entries;
134 struct ads_entry *new_entry;
136 DEBUG("Add alternate data stream %s:%s",
137 dentry->file_name_utf8, stream_name);
139 if (dentry->num_ads == 0xffff)
141 num_ads = dentry->num_ads + 1;
142 ads_entries = REALLOC(dentry->ads_entries,
143 num_ads * sizeof(struct ads_entry));
146 if (ads_entries != dentry->ads_entries) {
147 /* We moved the ADS entries. Adjust the stream lists. */
148 for (u16 i = 0; i < dentry->num_ads; i++) {
149 struct list_head *cur = &ads_entries[i].lte_group_list.list;
150 cur->prev->next = cur;
151 cur->next->prev = cur;
155 new_entry = &ads_entries[num_ads - 1];
156 ads_entry_init(new_entry);
157 if (change_ads_name(new_entry, stream_name) != 0)
159 dentry->ads_entries = ads_entries;
160 dentry->num_ads = num_ads;
164 /* Remove an alternate data stream from a dentry.
166 * The corresponding lookup table entry for the stream is NOT changed.
168 * @dentry: The dentry
169 * @ads_entry: The alternate data stream entry (it MUST be one of the
170 * ads_entry's in the array dentry->ads_entries).
172 void dentry_remove_ads(struct dentry *dentry, struct ads_entry *ads_entry)
177 wimlib_assert(dentry->num_ads);
178 idx = ads_entry - dentry->ads_entries;
179 wimlib_assert(idx < dentry->num_ads);
180 following = dentry->num_ads - idx - 1;
182 destroy_ads_entry(ads_entry);
183 memcpy(ads_entry, ads_entry + 1, following * sizeof(struct ads_entry));
185 /* We moved the ADS entries. Adjust the stream lists. */
186 for (u16 i = 0; i < following; i++) {
187 struct list_head *cur = &ads_entry[i].lte_group_list.list;
188 cur->prev->next = cur;
189 cur->next->prev = cur;
196 * Calls a function on all directory entries in a directory tree. It is called
197 * on a parent before its children.
199 int for_dentry_in_tree(struct dentry *root,
200 int (*visitor)(struct dentry*, void*), void *arg)
203 struct dentry *child;
205 ret = visitor(root, arg);
210 child = root->children;
216 ret = for_dentry_in_tree(child, visitor, arg);
220 } while (child != root->children);
225 * Like for_dentry_in_tree(), but the visitor function is always called on a
226 * dentry's children before on itself.
228 int for_dentry_in_tree_depth(struct dentry *root,
229 int (*visitor)(struct dentry*, void*), void *arg)
232 struct dentry *child;
235 child = root->children;
239 ret = for_dentry_in_tree_depth(child, visitor, arg);
243 } while (child != root->children);
245 return visitor(root, arg);
249 * Calculate the full path of @dentry, based on its parent's full path and on
250 * its UTF-8 file name.
252 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
256 if (dentry_is_root(dentry)) {
257 full_path = MALLOC(2);
264 char *parent_full_path;
265 u32 parent_full_path_len;
266 const struct dentry *parent = dentry->parent;
268 if (dentry_is_root(parent)) {
269 parent_full_path = "";
270 parent_full_path_len = 0;
272 parent_full_path = parent->full_path_utf8;
273 parent_full_path_len = parent->full_path_utf8_len;
276 full_path_len = parent_full_path_len + 1 +
277 dentry->file_name_utf8_len;
278 full_path = MALLOC(full_path_len + 1);
282 memcpy(full_path, parent_full_path, parent_full_path_len);
283 full_path[parent_full_path_len] = '/';
284 memcpy(full_path + parent_full_path_len + 1,
285 dentry->file_name_utf8,
286 dentry->file_name_utf8_len);
287 full_path[full_path_len] = '\0';
289 FREE(dentry->full_path_utf8);
290 dentry->full_path_utf8 = full_path;
291 dentry->full_path_utf8_len = full_path_len;
294 ERROR("Out of memory while calculating dentry full path");
295 return WIMLIB_ERR_NOMEM;
299 * Recursively calculates the subdir offsets for a directory tree.
301 * @dentry: The root of the directory tree.
302 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
303 * offset for @dentry.
305 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
307 struct dentry *child;
309 child = dentry->children;
310 dentry->subdir_offset = *subdir_offset_p;
313 /* Advance the subdir offset by the amount of space the children
314 * of this dentry take up. */
316 *subdir_offset_p += dentry_total_length(child);
318 } while (child != dentry->children);
320 /* End-of-directory dentry on disk. */
321 *subdir_offset_p += 8;
323 /* Recursively call calculate_subdir_offsets() on all the
326 calculate_subdir_offsets(child, subdir_offset_p);
328 } while (child != dentry->children);
330 /* On disk, childless directories have a valid subdir_offset
331 * that points to an 8-byte end-of-directory dentry. Regular
332 * files or reparse points have a subdir_offset of 0. */
333 if (dentry_is_directory(dentry))
334 *subdir_offset_p += 8;
336 dentry->subdir_offset = 0;
341 /* Returns the child of @dentry that has the file name @name.
342 * Returns NULL if no child has the name. */
343 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
346 struct dentry *child;
349 child = dentry->children;
351 name_len = strlen(name);
353 if (dentry_has_name(child, name, name_len))
356 } while (child != dentry->children);
361 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
362 * @cur_dir. Returns NULL if no dentry having the path is found. */
363 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir,
366 struct dentry *child;
368 const char *new_path;
373 child = cur_dir->children;
375 new_path = path_next_part(path, &base_len);
377 if (dentry_has_name(child, path, base_len))
378 return get_dentry_relative_path(child, new_path);
380 } while (child != cur_dir->children);
385 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
387 struct dentry *get_dentry(WIMStruct *w, const char *path)
389 struct dentry *root = wim_root_dentry(w);
392 return get_dentry_relative_path(root, path);
395 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
396 * if the dentry is not found. */
397 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
399 size_t path_len = strlen(path);
400 char buf[path_len + 1];
402 memcpy(buf, path, path_len + 1);
404 to_parent_name(buf, path_len);
406 return get_dentry(w, buf);
409 /* Prints the full path of a dentry. */
410 int print_dentry_full_path(struct dentry *dentry, void *ignore)
412 if (dentry->full_path_utf8)
413 puts(dentry->full_path_utf8);
417 /* We want to be able to show the names of the file attribute flags that are
419 struct file_attr_flag {
423 struct file_attr_flag file_attr_flags[] = {
424 {FILE_ATTRIBUTE_READONLY, "READONLY"},
425 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
426 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
427 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
428 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
429 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
430 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
431 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
432 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
433 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
434 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
435 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
436 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
437 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
438 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
441 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
442 * available. If the dentry is unresolved and the lookup table is NULL, the
443 * lookup table entries will not be printed. Otherwise, they will be. */
444 int print_dentry(struct dentry *dentry, void *lookup_table)
447 struct lookup_table_entry *lte;
449 printf("[DENTRY]\n");
450 printf("Length = %"PRIu64"\n", dentry->length);
451 printf("Attributes = 0x%x\n", dentry->attributes);
452 for (unsigned i = 0; i < ARRAY_LEN(file_attr_flags); i++)
453 if (file_attr_flags[i].flag & dentry->attributes)
454 printf(" FILE_ATTRIBUTE_%s is set\n",
455 file_attr_flags[i].name);
456 printf("Security ID = %d\n", dentry->security_id);
457 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
459 printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);
460 printf("Unused2 = %"PRIu64"\n", dentry->unused2);
463 printf("Creation Time = 0x%"PRIx64"\n");
464 printf("Last Access Time = 0x%"PRIx64"\n");
465 printf("Last Write Time = 0x%"PRIx64"\n");
468 /* Translate the timestamps into something readable */
469 time_t creat_time = wim_timestamp_to_unix(dentry->creation_time);
470 time_t access_time = wim_timestamp_to_unix(dentry->last_access_time);
471 time_t mod_time = wim_timestamp_to_unix(dentry->last_write_time);
472 printf("Creation Time = %s", asctime(gmtime(&creat_time)));
473 printf("Last Access Time = %s", asctime(gmtime(&access_time)));
474 printf("Last Write Time = %s", asctime(gmtime(&mod_time)));
476 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
477 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
478 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
479 printf("Filename = \"");
480 print_string(dentry->file_name, dentry->file_name_len);
482 printf("Filename Length = %hu\n", dentry->file_name_len);
483 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
484 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
485 printf("Short Name = \"");
486 print_string(dentry->short_name, dentry->short_name_len);
488 printf("Short Name Length = %hu\n", dentry->short_name_len);
489 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
490 lte = dentry_stream_lte(dentry, 0, lookup_table);
492 print_lookup_table_entry(lte);
494 hash = dentry_stream_hash(dentry, 0);
502 for (u16 i = 0; i < dentry->num_ads; i++) {
503 printf("[Alternate Stream Entry %u]\n", i);
504 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
505 printf("Name Length (UTF-16) = %u\n",
506 dentry->ads_entries[i].stream_name_len);
507 hash = dentry_stream_hash(dentry, i + 1);
513 print_lookup_table_entry(dentry_stream_lte(dentry, i + 1,
519 /* Initializations done on every `struct dentry'. */
520 static void dentry_common_init(struct dentry *dentry)
522 memset(dentry, 0, sizeof(struct dentry));
524 dentry->security_id = -1;
525 dentry->ads_entries_status = ADS_ENTRIES_DEFAULT;
526 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
530 * Creates an unlinked directory entry.
532 * @name: The UTF-8 filename of the new dentry.
534 * Returns a pointer to the new dentry, or NULL if out of memory.
536 struct dentry *new_dentry(const char *name)
538 struct dentry *dentry;
540 dentry = MALLOC(sizeof(struct dentry));
544 dentry_common_init(dentry);
545 if (change_dentry_name(dentry, name) != 0)
548 dentry_update_all_timestamps(dentry);
549 dentry->next = dentry;
550 dentry->prev = dentry;
551 dentry->parent = dentry;
552 INIT_LIST_HEAD(&dentry->link_group_list);
556 ERROR("Failed to allocate new dentry");
560 void dentry_free_ads_entries(struct dentry *dentry)
562 for (u16 i = 0; i < dentry->num_ads; i++)
563 destroy_ads_entry(&dentry->ads_entries[i]);
564 FREE(dentry->ads_entries);
565 dentry->ads_entries = NULL;
569 static void __destroy_dentry(struct dentry *dentry)
571 FREE(dentry->file_name);
572 FREE(dentry->file_name_utf8);
573 FREE(dentry->short_name);
574 FREE(dentry->full_path_utf8);
575 FREE(dentry->extracted_file);
578 /* Frees a WIM dentry. */
579 void free_dentry(struct dentry *dentry)
581 wimlib_assert(dentry);
582 __destroy_dentry(dentry);
583 /* Don't destroy the ADS entries if they "belong" to a different dentry
585 if (dentry->ads_entries_status != ADS_ENTRIES_USER)
586 dentry_free_ads_entries(dentry);
590 /* Like free_dentry(), but assigns a new ADS entries owner if this dentry was
591 * the previous owner, and also deletes the dentry from its link_group_list */
592 void put_dentry(struct dentry *dentry)
594 if (dentry->ads_entries_status == ADS_ENTRIES_OWNER) {
595 struct dentry *new_owner;
596 list_for_each_entry(new_owner, &dentry->link_group_list,
599 if (new_owner->ads_entries_status == ADS_ENTRIES_USER) {
600 new_owner->ads_entries_status = ADS_ENTRIES_OWNER;
604 dentry->ads_entries_status = ADS_ENTRIES_USER;
606 struct list_head *next;
607 list_del(&dentry->link_group_list);
612 /* Partically clones a dentry.
615 * - memory for file names is not cloned (the pointers are all set to NULL
616 * and the lengths are set to zero)
617 * - next, prev, and children pointers and not touched
618 * - stream entries are not cloned (pointer left untouched).
620 struct dentry *clone_dentry(struct dentry *old)
622 struct dentry *new = MALLOC(sizeof(struct dentry));
625 memcpy(new, old, sizeof(struct dentry));
626 new->file_name = NULL;
627 new->file_name_len = 0;
628 new->file_name_utf8 = NULL;
629 new->file_name_utf8_len = 0;
630 new->short_name = NULL;
631 new->short_name_len = 0;
636 * This function is passed as an argument to for_dentry_in_tree_depth() in order
637 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
639 static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
641 struct lookup_table *lookup_table = __lookup_table;
642 struct lookup_table_entry *lte;
646 for (i = 0; i <= dentry->num_ads; i++) {
647 lte = dentry_stream_lte(dentry, i, lookup_table);
648 lte_decrement_refcnt(lte, lookup_table);
652 wimlib_assert(dentry->refcnt != 0);
653 if (--dentry->refcnt == 0)
659 * Unlinks and frees a dentry tree.
661 * @root: The root of the tree.
662 * @lookup_table: The lookup table for dentries. If non-NULL, the
663 * reference counts in the lookup table for the lookup
664 * table entries corresponding to the dentries will be
667 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
669 if (!root || !root->parent)
671 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
674 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
681 * Links a dentry into the directory tree.
683 * @dentry: The dentry to link.
684 * @parent: The dentry that will be the parent of @dentry.
686 void link_dentry(struct dentry *dentry, struct dentry *parent)
688 wimlib_assert(dentry_is_directory(parent));
689 dentry->parent = parent;
690 if (parent->children) {
691 /* Not an only child; link to siblings. */
692 dentry->next = parent->children;
693 dentry->prev = parent->children->prev;
694 dentry->next->prev = dentry;
695 dentry->prev->next = dentry;
697 /* Only child; link to parent. */
698 parent->children = dentry;
699 dentry->next = dentry;
700 dentry->prev = dentry;
705 /* Unlink a dentry from the directory tree.
707 * Note: This merely removes it from the in-memory tree structure. See
708 * remove_dentry() in mount.c for a function implemented on top of this one that
709 * frees the dentry and implements reference counting for the lookup table
711 void unlink_dentry(struct dentry *dentry)
713 if (dentry_is_root(dentry))
715 if (dentry_is_only_child(dentry)) {
716 dentry->parent->children = NULL;
718 if (dentry_is_first_sibling(dentry))
719 dentry->parent->children = dentry->next;
720 dentry->next->prev = dentry->prev;
721 dentry->prev->next = dentry->next;
726 /* Recalculates the length of @dentry based on its file name length and short
728 static inline void recalculate_dentry_size(struct dentry *dentry)
730 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
731 2 + dentry->short_name_len;
732 /* Must be multiple of 8. */
733 dentry->length = (dentry->length + 7) & ~7;
736 /* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
737 * and their lengths in the pointer arguments */
738 int get_names(char **name_utf16_ret, char **name_utf8_ret,
739 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
744 char *name_utf16, *name_utf8;
746 utf8_len = strlen(name);
748 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
751 return WIMLIB_ERR_NOMEM;
753 name_utf8 = MALLOC(utf8_len + 1);
756 return WIMLIB_ERR_NOMEM;
758 memcpy(name_utf8, name, utf8_len + 1);
759 FREE(*name_utf8_ret);
760 FREE(*name_utf16_ret);
761 *name_utf8_ret = name_utf8;
762 *name_utf16_ret = name_utf16;
763 *name_utf8_len_ret = utf8_len;
764 *name_utf16_len_ret = utf16_len;
768 /* Changes the name of a dentry to @new_name. Only changes the file_name and
769 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
770 * full_path_utf8 fields. Also recalculates its length. */
771 int change_dentry_name(struct dentry *dentry, const char *new_name)
775 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
776 &dentry->file_name_len, &dentry->file_name_utf8_len,
779 recalculate_dentry_size(dentry);
784 * Changes the name of an alternate data stream */
785 int change_ads_name(struct ads_entry *entry, const char *new_name)
787 return get_names(&entry->stream_name, &entry->stream_name_utf8,
788 &entry->stream_name_len,
789 &entry->stream_name_utf8_len,
793 /* Parameters for calculate_dentry_statistics(). */
794 struct image_statistics {
795 struct lookup_table *lookup_table;
799 u64 *hard_link_bytes;
802 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
804 struct image_statistics *stats;
805 struct lookup_table_entry *lte;
809 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
812 ++*stats->file_count;
814 for (unsigned i = 0; i <= dentry->num_ads; i++) {
815 lte = dentry_stream_lte(dentry, i, stats->lookup_table);
817 *stats->total_bytes += wim_resource_size(lte);
818 if (++lte->out_refcnt == 1)
819 *stats->hard_link_bytes += wim_resource_size(lte);
825 /* Calculates some statistics about a dentry tree. */
826 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
827 u64 *dir_count_ret, u64 *file_count_ret,
828 u64 *total_bytes_ret,
829 u64 *hard_link_bytes_ret)
831 struct image_statistics stats;
834 *total_bytes_ret = 0;
835 *hard_link_bytes_ret = 0;
836 stats.lookup_table = table;
837 stats.dir_count = dir_count_ret;
838 stats.file_count = file_count_ret;
839 stats.total_bytes = total_bytes_ret;
840 stats.hard_link_bytes = hard_link_bytes_ret;
841 for_lookup_table_entry(table, zero_out_refcnts, NULL);
842 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
847 * Reads the alternate data stream entries for a dentry.
849 * @p: Pointer to buffer that starts with the first alternate stream entry.
851 * @dentry: Dentry to load the alternate data streams into.
852 * @dentry->num_ads must have been set to the number of
853 * alternate data streams that are expected.
855 * @remaining_size: Number of bytes of data remaining in the buffer pointed
858 * The format of the on-disk alternate stream entries is as follows:
860 * struct ads_entry_on_disk {
861 * u64 length; // Length of the entry, in bytes. This includes
862 * all fields (including the stream name, the
863 * null terminator, AND the padding!).
864 * u64 reserved; // Seems to be unused
865 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
866 * u16 stream_name_len; // Length of the stream name, in bytes
867 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
868 * not including null terminator
869 * u16 zero; // UTF-16 null terminator for the stream name, NOT
870 * included in @stream_name_len
871 * char padding[]; // Padding to make the size a multiple of 8 bytes.
874 * In addition, the entries are 8-byte aligned.
876 * Return 0 on success or nonzero on failure. On success, dentry->ads_entries
877 * is set to an array of `struct ads_entry's of length dentry->num_ads. On
878 * failure, @dentry is not modified.
880 static int read_ads_entries(const u8 *p, struct dentry *dentry,
884 struct ads_entry *ads_entries;
887 num_ads = dentry->num_ads;
888 ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
890 ERROR("Could not allocate memory for %"PRIu16" "
891 "alternate data stream entries", num_ads);
892 return WIMLIB_ERR_NOMEM;
895 for (u16 i = 0; i < num_ads; i++) {
896 struct ads_entry *cur_entry = &ads_entries[i];
898 u64 length_no_padding;
901 const char *p_save = p;
903 /* Read the base stream entry, excluding the stream name. */
904 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
905 ERROR("Stream entries go past end of metadata resource");
906 ERROR("(remaining_size = %"PRIu64")", remaining_size);
907 ret = WIMLIB_ERR_INVALID_DENTRY;
908 goto out_free_ads_entries;
911 p = get_u64(p, &length);
913 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
914 p = get_u16(p, &cur_entry->stream_name_len);
916 cur_entry->stream_name = NULL;
917 cur_entry->stream_name_utf8 = NULL;
919 /* Length including neither the null terminator nor the padding
921 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
922 cur_entry->stream_name_len;
923 total_length = ((length_no_padding + 2) + 7) & ~7;
925 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
927 if (remaining_size < length_no_padding) {
928 ERROR("Stream entries go past end of metadata resource");
929 ERROR("(remaining_size = %"PRIu64" bytes, "
930 "length_no_padding = %"PRIu16" bytes",
931 remaining_size, length_no_padding);
932 ret = WIMLIB_ERR_INVALID_DENTRY;
933 goto out_free_ads_entries;
936 /* The @length field in the on-disk ADS entry is expected to be
937 * equal to @total_length, which includes all of the entry and
938 * the padding that follows it to align the next ADS entry to an
939 * 8-byte boundary. However, to be safe, we'll accept the
940 * length field as long as it's not less than the un-padded
941 * total length and not more than the padded total length. */
942 if (length < length_no_padding || length > total_length) {
943 ERROR("Stream entry has unexpected length "
944 "field (length field = %"PRIu64", "
945 "unpadded total length = %"PRIu64", "
946 "padded total length = %"PRIu64")",
947 length, length_no_padding, total_length);
948 ret = WIMLIB_ERR_INVALID_DENTRY;
949 goto out_free_ads_entries;
952 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
953 if (!cur_entry->stream_name) {
954 ret = WIMLIB_ERR_NOMEM;
955 goto out_free_ads_entries;
957 get_bytes(p, cur_entry->stream_name_len,
958 (u8*)cur_entry->stream_name);
959 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
960 cur_entry->stream_name_len,
962 cur_entry->stream_name_utf8_len = utf8_len;
964 if (!cur_entry->stream_name_utf8) {
965 ret = WIMLIB_ERR_NOMEM;
966 goto out_free_ads_entries;
968 /* It's expected that the size of every ADS entry is a multiple
969 * of 8. However, to be safe, I'm allowing the possibility of
970 * an ADS entry at the very end of the metadata resource ending
971 * un-aligned. So although we still need to increment the input
972 * pointer by @total_length to reach the next ADS entry, it's
973 * possible that less than @total_length is actually remaining
974 * in the metadata resource. We should set the remaining size to
975 * 0 bytes if this happens. */
976 p = p_save + total_length;
977 if (remaining_size < total_length)
980 remaining_size -= total_length;
982 dentry->ads_entries = ads_entries;
984 out_free_ads_entries:
985 for (u16 i = 0; i < num_ads; i++) {
986 FREE(ads_entries[i].stream_name);
987 FREE(ads_entries[i].stream_name_utf8);
994 * Reads a directory entry, including all alternate data stream entries that
995 * follow it, from the WIM image's metadata resource.
997 * @metadata_resource: Buffer containing the uncompressed metadata resource.
998 * @metadata_resource_len: Length of the metadata resource.
999 * @offset: Offset of this directory entry in the metadata resource.
1000 * @dentry: A `struct dentry' that will be filled in by this function.
1002 * Return 0 on success or nonzero on failure. On failure, @dentry have been
1003 * modified, bu it will be left with no pointers to any allocated buffers.
1004 * On success, the dentry->length field must be examined. If zero, this was a
1005 * special "end of directory" dentry and not a real dentry. If nonzero, this
1006 * was a real dentry.
1008 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1009 u64 offset, struct dentry *dentry)
1012 u64 calculated_size;
1014 char *file_name_utf8;
1018 size_t file_name_utf8_len;
1021 dentry_common_init(dentry);
1023 /*Make sure the dentry really fits into the metadata resource.*/
1024 if (offset + 8 > metadata_resource_len) {
1025 ERROR("Directory entry starting at %"PRIu64" ends past the "
1026 "end of the metadata resource (size %"PRIu64")",
1027 offset, metadata_resource_len);
1028 return WIMLIB_ERR_INVALID_DENTRY;
1031 /* Before reading the whole dentry, we need to read just the length.
1032 * This is because a dentry of length 8 (that is, just the length field)
1033 * terminates the list of sibling directory entries. */
1035 p = get_u64(&metadata_resource[offset], &dentry->length);
1037 /* A zero length field (really a length of 8, since that's how big the
1038 * directory entry is...) indicates that this is the end of directory
1039 * dentry. We do not read it into memory as an actual dentry, so just
1040 * return successfully in that case. */
1041 if (dentry->length == 0)
1044 /* If the dentry does not overflow the metadata resource buffer and is
1045 * not too short, read the rest of it (excluding the alternate data
1046 * streams, but including the file name and short name variable-length
1047 * fields) into memory. */
1048 if (offset + dentry->length >= metadata_resource_len) {
1049 ERROR("Directory entry at offset %"PRIu64" and with size "
1050 "%"PRIu64" ends past the end of the metadata resource "
1052 offset, dentry->length, metadata_resource_len);
1053 return WIMLIB_ERR_INVALID_DENTRY;
1056 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1057 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1059 return WIMLIB_ERR_INVALID_DENTRY;
1062 p = get_u32(p, &dentry->attributes);
1063 p = get_u32(p, (u32*)&dentry->security_id);
1064 p = get_u64(p, &dentry->subdir_offset);
1066 /* 2 unused fields */
1067 p += 2 * sizeof(u64);
1068 /*p = get_u64(p, &dentry->unused1);*/
1069 /*p = get_u64(p, &dentry->unused2);*/
1071 p = get_u64(p, &dentry->creation_time);
1072 p = get_u64(p, &dentry->last_access_time);
1073 p = get_u64(p, &dentry->last_write_time);
1075 p = get_bytes(p, SHA1_HASH_SIZE, dentry->hash);
1078 * I don't know what's going on here. It seems like M$ screwed up the
1079 * reparse points, then put the fields in the same place and didn't
1080 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1081 * have something to do with this, but it's not documented.
1083 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1086 p = get_u32(p, &dentry->reparse_tag);
1089 p = get_u32(p, &dentry->reparse_tag);
1090 p = get_u64(p, &dentry->hard_link);
1093 /* By the way, the reparse_reserved field does not actually exist (at
1094 * least when the file is not a reparse point) */
1096 p = get_u16(p, &dentry->num_ads);
1098 p = get_u16(p, &short_name_len);
1099 p = get_u16(p, &file_name_len);
1101 /* We now know the length of the file name and short name. These should
1102 * be included in the dentry length, but make sure the numbers are
1104 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
1107 if (dentry->length < calculated_size) {
1108 ERROR("Unexpected end of directory entry! (Expected "
1109 "%"PRIu64" bytes, got %"PRIu64" bytes. "
1110 "short_name_len = %hu, file_name_len = %hu)",
1111 calculated_size, dentry->length,
1112 short_name_len, file_name_len);
1113 return WIMLIB_ERR_INVALID_DENTRY;
1116 /* Read the filename. */
1117 file_name = MALLOC(file_name_len);
1119 ERROR("Failed to allocate %hu bytes for dentry file name",
1121 return WIMLIB_ERR_NOMEM;
1123 p = get_bytes(p, file_name_len, file_name);
1125 /* Convert filename to UTF-8. */
1126 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1127 &file_name_utf8_len);
1129 if (!file_name_utf8) {
1130 ERROR("Failed to allocate memory to convert UTF-16 "
1131 "filename (%hu bytes) to UTF-8", file_name_len);
1132 ret = WIMLIB_ERR_NOMEM;
1133 goto out_free_file_name;
1136 /* Undocumented padding between file name and short name. This probably
1137 * is supposed to be a terminating null character. */
1140 /* Read the short filename. */
1141 short_name = MALLOC(short_name_len);
1143 ERROR("Failed to allocate %hu bytes for short filename",
1145 ret = WIMLIB_ERR_NOMEM;
1146 goto out_free_file_name_utf8;
1149 p = get_bytes(p, short_name_len, short_name);
1151 /* Some directory entries inexplicably have a little over 70 bytes of
1152 * extra data. The exact amount of data seems to be 72 bytes, but it is
1153 * aligned on the next 8-byte boundary. It does NOT seem to be
1154 * alternate data stream entries. Here's an example of the aligned
1157 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1158 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1159 * 00000000 00000000 00000000 00000000
1161 * Here's one interpretation of how the data is laid out.
1164 * u32 field1; (always 0x00000001)
1165 * u32 field2; (always 0x40000000)
1166 * u8 data[48]; (???)
1167 * u64 reserved1; (always 0)
1168 * u64 reserved2; (always 0)
1171 if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
1172 printf("%s: %lu / %lu (", file_name_utf8,
1173 calculated_size, dentry->length);
1174 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
1176 print_byte_field(p, dentry->length - calculated_size);
1182 * Read the alternate data streams, if present. dentry->num_ads tells
1183 * us how many they are, and they will directly follow the dentry
1186 * Note that each alternate data stream entry begins on an 8-byte
1187 * aligned boundary, and the alternate data stream entries are NOT
1188 * included in the dentry->length field for some reason.
1190 if (dentry->num_ads != 0) {
1191 calculated_size = (calculated_size + 7) & ~7;
1192 if (calculated_size > metadata_resource_len - offset) {
1193 ERROR("Not enough space in metadata resource for "
1194 "alternate stream entries");
1195 ret = WIMLIB_ERR_INVALID_DENTRY;
1196 goto out_free_short_name;
1198 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1200 metadata_resource_len - offset - calculated_size);
1202 goto out_free_short_name;
1205 /* We've read all the data for this dentry. Set the names and their
1206 * lengths, and we've done. */
1207 dentry->file_name = file_name;
1208 dentry->file_name_utf8 = file_name_utf8;
1209 dentry->short_name = short_name;
1210 dentry->file_name_len = file_name_len;
1211 dentry->file_name_utf8_len = file_name_utf8_len;
1212 dentry->short_name_len = short_name_len;
1214 out_free_short_name:
1216 out_free_file_name_utf8:
1217 FREE(file_name_utf8);
1224 * Writes a WIM dentry to an output buffer.
1226 * @dentry: The dentry structure.
1227 * @p: The memory location to write the data to.
1228 * @return: Pointer to the byte after the last byte we wrote as part of the
1231 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1237 p = put_u64(p, dentry->length);
1238 p = put_u32(p, dentry->attributes);
1239 p = put_u32(p, dentry->security_id);
1240 p = put_u64(p, dentry->subdir_offset);
1241 p = put_u64(p, 0); /* unused1 */
1242 p = put_u64(p, 0); /* unused2 */
1243 p = put_u64(p, dentry->creation_time);
1244 p = put_u64(p, dentry->last_access_time);
1245 p = put_u64(p, dentry->last_write_time);
1246 if (dentry->resolved && dentry->lte)
1247 hash = dentry->lte->hash;
1249 hash = dentry->hash;
1250 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1251 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1252 p = put_zeroes(p, 4);
1253 p = put_u32(p, dentry->reparse_tag);
1254 p = put_zeroes(p, 4);
1258 if (dentry->link_group_list.next == &dentry->link_group_list)
1261 hard_link = dentry->hard_link;
1262 p = put_u64(p, hard_link);
1264 p = put_u16(p, dentry->num_ads);
1265 p = put_u16(p, dentry->short_name_len);
1266 p = put_u16(p, dentry->file_name_len);
1267 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1268 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1269 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1271 wimlib_assert(p - orig_p <= dentry->length);
1272 if (p - orig_p < dentry->length)
1273 p = put_zeroes(p, dentry->length - (p - orig_p));
1275 /* Align to 8-byte boundary */
1276 p = put_zeroes(p, (8 - dentry->length % 8) % 8);
1278 /* Write the alternate data streams, if there are any. Please see
1279 * read_ads_entries() for comments about the format of the on-disk
1280 * alternate data stream entries. */
1281 for (u16 i = 0; i < dentry->num_ads; i++) {
1282 p = put_u64(p, ads_entry_total_length(&dentry->ads_entries[i]));
1283 p = put_u64(p, 0); /* Unused */
1284 if (dentry->resolved && dentry->ads_entries[i].lte)
1285 hash = dentry->ads_entries[i].lte->hash;
1287 hash = dentry->ads_entries[i].hash;
1288 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1289 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
1290 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
1291 (u8*)dentry->ads_entries[i].stream_name);
1293 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1298 /* Recursive function that writes a dentry tree rooted at @parent, not including
1299 * @parent itself, which has already been written. */
1300 static u8 *write_dentry_tree_recursive(const struct dentry *parent, u8 *p)
1302 const struct dentry *child;
1304 /* Nothing to do if this dentry has no children. */
1305 if (parent->subdir_offset == 0)
1308 /* Write child dentries and end-of-directory entry.
1310 * Note: we need to write all of this dentry's children before
1311 * recursively writing the directory trees rooted at each of the child
1312 * dentries, since the on-disk dentries for a dentry's children are
1313 * always located at consecutive positions in the metadata resource! */
1314 child = parent->children;
1317 p = write_dentry(child, p);
1318 child = child->next;
1319 } while (child != parent->children);
1322 /* write end of directory entry */
1325 /* Recurse on children. */
1328 p = write_dentry_tree_recursive(child, p);
1329 child = child->next;
1330 } while (child != parent->children);
1335 /* Writes a directory tree to the metadata resource.
1337 * @root: Root of the dentry tree.
1338 * @p: Pointer to a buffer with enough space for the dentry tree.
1340 * Returns pointer to the byte after the last byte we wrote.
1342 u8 *write_dentry_tree(const struct dentry *root, u8 *p)
1344 wimlib_assert(dentry_is_root(root));
1346 /* If we're the root dentry, we have no parent that already
1347 * wrote us, so we need to write ourselves. */
1348 p = write_dentry(root, p);
1350 /* Write end of directory entry after the root dentry just to be safe;
1351 * however the root dentry obviously cannot have any siblings. */
1354 /* Recursively write the rest of the dentry tree. */
1355 return write_dentry_tree_recursive(root, p);
1358 /* Reads the children of a dentry, and all their children, ..., etc. from the
1359 * metadata resource and into the dentry tree.
1361 * @metadata_resource: An array that contains the uncompressed metadata
1362 * resource for the WIM file.
1364 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1367 * @dentry: A pointer to a `struct dentry' that is the root of the directory
1368 * tree and has already been read from the metadata resource. It
1369 * does not need to be the real root because this procedure is
1370 * called recursively.
1372 * @return: Zero on success, nonzero on failure.
1374 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1375 struct dentry *dentry)
1377 u64 cur_offset = dentry->subdir_offset;
1378 struct dentry *prev_child = NULL;
1379 struct dentry *first_child = NULL;
1380 struct dentry *child;
1381 struct dentry cur_child;
1385 * If @dentry has no child dentries, nothing more needs to be done for
1386 * this branch. This is the case for regular files, symbolic links, and
1387 * *possibly* empty directories (although an empty directory may also
1388 * have one child dentry that is the special end-of-directory dentry)
1390 if (cur_offset == 0)
1393 /* Find and read all the children of @dentry. */
1396 /* Read next child of @dentry into @cur_child. */
1397 ret = read_dentry(metadata_resource, metadata_resource_len,
1398 cur_offset, &cur_child);
1402 /* Check for end of directory. */
1403 if (cur_child.length == 0)
1406 /* Not end of directory. Allocate this child permanently and
1407 * link it to the parent and previous child. */
1408 child = MALLOC(sizeof(struct dentry));
1410 ERROR("Failed to allocate %zu bytes for new dentry",
1411 sizeof(struct dentry));
1412 ret = WIMLIB_ERR_NOMEM;
1415 memcpy(child, &cur_child, sizeof(struct dentry));
1418 prev_child->next = child;
1419 child->prev = prev_child;
1421 first_child = child;
1424 child->parent = dentry;
1427 /* If there are children of this child, call this procedure
1429 if (child->subdir_offset != 0) {
1430 ret = read_dentry_tree(metadata_resource,
1431 metadata_resource_len, child);
1436 /* Advance to the offset of the next child. */
1437 cur_offset += dentry_total_length(child);
1440 /* Link last child to first one, and set parent's children pointer to
1441 * the first child. */
1443 prev_child->next = first_child;
1444 first_child->prev = prev_child;
1446 dentry->children = first_child;