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"
42 /* Real length of a dentry, including the alternate data stream entries, which
43 * are not included in the dentry->length field... */
44 u64 dentry_total_length(const struct dentry *dentry)
46 u64 length = (dentry->length + 7) & ~7;
47 for (u16 i = 0 ; i < dentry->num_ads; i++)
48 length += ads_entry_length(&dentry->ads_entries[i]);
52 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
53 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
55 if (S_ISLNK(stbuf->st_mode)) {
56 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
57 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
58 } else if (S_ISDIR(stbuf->st_mode)) {
59 dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
61 dentry->attributes = FILE_ATTRIBUTE_NORMAL;
65 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
66 void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
67 const struct lookup_table *table)
69 struct lookup_table_entry *lte;
71 if (dentry_is_symlink(dentry))
72 stbuf->st_mode = S_IFLNK | 0777;
73 else if (dentry_is_directory(dentry))
74 stbuf->st_mode = S_IFDIR | 0755;
76 stbuf->st_mode = S_IFREG | 0644;
79 lte = lookup_resource(table, dentry->hash);
84 stbuf->st_nlink = lte->refcnt;
85 stbuf->st_size = lte->resource_entry.original_size;
90 stbuf->st_uid = getuid();
91 stbuf->st_gid = getgid();
92 stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
93 stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
94 stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
95 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
98 /* Makes all timestamp fields for the dentry be the current time. */
99 void dentry_update_all_timestamps(struct dentry *dentry)
101 u64 now = get_timestamp();
102 dentry->creation_time = now;
103 dentry->last_access_time = now;
104 dentry->last_write_time = now;
108 * Calls a function on all directory entries in a directory tree. It is called
109 * on a parent before its children.
111 int for_dentry_in_tree(struct dentry *root,
112 int (*visitor)(struct dentry*, void*), void *arg)
115 struct dentry *child;
117 ret = visitor(root, arg);
122 child = root->children;
128 ret = for_dentry_in_tree(child, visitor, arg);
132 } while (child != root->children);
137 * Like for_dentry_in_tree(), but the visitor function is always called on a
138 * dentry's children before on itself.
140 int for_dentry_in_tree_depth(struct dentry *root,
141 int (*visitor)(struct dentry*, void*), void *arg)
144 struct dentry *child;
147 child = root->children;
151 ret = for_dentry_in_tree_depth(child, visitor, arg);
155 } while (child != root->children);
157 return visitor(root, arg);
161 * Calculate the full path of @dentry, based on its parent's full path and on
162 * its UTF-8 file name.
164 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
168 if (dentry_is_root(dentry)) {
169 full_path = MALLOC(2);
176 char *parent_full_path;
177 u32 parent_full_path_len;
178 const struct dentry *parent = dentry->parent;
180 if (dentry_is_root(parent)) {
181 parent_full_path = "";
182 parent_full_path_len = 0;
184 parent_full_path = parent->full_path_utf8;
185 parent_full_path_len = parent->full_path_utf8_len;
188 full_path_len = parent_full_path_len + 1 +
189 dentry->file_name_utf8_len;
190 full_path = MALLOC(full_path_len + 1);
194 memcpy(full_path, parent_full_path, parent_full_path_len);
195 full_path[parent_full_path_len] = '/';
196 memcpy(full_path + parent_full_path_len + 1,
197 dentry->file_name_utf8,
198 dentry->file_name_utf8_len);
199 full_path[full_path_len] = '\0';
201 FREE(dentry->full_path_utf8);
202 dentry->full_path_utf8 = full_path;
203 dentry->full_path_utf8_len = full_path_len;
206 ERROR("Out of memory while calculating dentry full path");
207 return WIMLIB_ERR_NOMEM;
211 * Recursively calculates the subdir offsets for a directory tree.
213 * @dentry: The root of the directory tree.
214 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
215 * offset for @dentry.
217 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
219 struct dentry *child;
221 child = dentry->children;
222 dentry->subdir_offset = *subdir_offset_p;
225 /* Advance the subdir offset by the amount of space the children
226 * of this dentry take up. */
228 *subdir_offset_p += dentry_total_length(child);
230 } while (child != dentry->children);
232 /* End-of-directory dentry on disk. */
233 *subdir_offset_p += 8;
235 /* Recursively call calculate_subdir_offsets() on all the
238 calculate_subdir_offsets(child, subdir_offset_p);
240 } while (child != dentry->children);
242 /* On disk, childless directories have a valid subdir_offset
243 * that points to an 8-byte end-of-directory dentry. Regular
244 * files have a subdir_offset of 0. */
245 if (dentry_is_directory(dentry))
246 *subdir_offset_p += 8;
248 dentry->subdir_offset = 0;
253 /* Returns the child of @dentry that has the file name @name.
254 * Returns NULL if no child has the name. */
255 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
258 struct dentry *child;
261 child = dentry->children;
263 name_len = strlen(name);
265 if (dentry_has_name(child, name, name_len))
268 } while (child != dentry->children);
273 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
274 * @cur_dir. Returns NULL if no dentry having the path is found. */
275 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
277 struct dentry *child;
279 const char *new_path;
284 child = cur_dir->children;
286 new_path = path_next_part(path, &base_len);
288 if (dentry_has_name(child, path, base_len))
289 return get_dentry_relative_path(child, new_path);
291 } while (child != cur_dir->children);
296 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
298 struct dentry *get_dentry(WIMStruct *w, const char *path)
300 struct dentry *root = wim_root_dentry(w);
303 return get_dentry_relative_path(root, path);
306 /* Returns the parent directory for the @path. */
307 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
309 size_t path_len = strlen(path);
310 char buf[path_len + 1];
312 memcpy(buf, path, path_len + 1);
314 to_parent_name(buf, path_len);
316 return get_dentry(w, buf);
319 /* Prints the full path of a dentry. */
320 int print_dentry_full_path(struct dentry *dentry, void *ignore)
322 if (dentry->full_path_utf8)
323 puts(dentry->full_path_utf8);
327 struct file_attr_flag {
331 struct file_attr_flag file_attr_flags[] = {
332 {FILE_ATTRIBUTE_READONLY, "READONLY"},
333 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
334 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
335 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
336 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
337 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
338 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
339 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
340 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
341 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
342 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
343 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
344 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
345 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
346 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
349 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
350 * NULL if the resource entry for the dentry is not to be printed. */
351 int print_dentry(struct dentry *dentry, void *lookup_table)
353 struct lookup_table_entry *lte;
356 printf("[DENTRY]\n");
357 printf("Length = %"PRIu64"\n", dentry->length);
358 printf("Attributes = 0x%x\n", dentry->attributes);
359 for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
360 if (file_attr_flags[i].flag & dentry->attributes)
361 printf(" FILE_ATTRIBUTE_%s is set\n",
362 file_attr_flags[i].name);
363 printf("Security ID = %d\n", dentry->security_id);
364 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
365 /*printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);*/
366 /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
367 printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
368 printf("Last Access Time = 0x%"PRIx64"\n", dentry->last_access_time);
369 printf("Last Write Time = 0x%"PRIx64"\n", dentry->last_write_time);
371 print_hash(dentry->hash);
373 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
374 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
375 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
376 printf("Filename = \"");
377 print_string(dentry->file_name, dentry->file_name_len);
379 printf("Filename Length = %hu\n", dentry->file_name_len);
380 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
381 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
382 printf("Short Name = \"");
383 print_string(dentry->short_name, dentry->short_name_len);
385 printf("Short Name Length = %hu\n", dentry->short_name_len);
386 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
388 lte = lookup_resource(lookup_table, dentry->hash);
390 print_lookup_table_entry(lte, NULL);
396 for (u16 i = 0; i < dentry->num_ads; i++) {
397 printf("[Alternate Stream Entry %u]\n", i);
398 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
399 printf("Name Length (UTF-16) = %u\n",
400 dentry->ads_entries[i].stream_name_len);
402 print_hash(dentry->ads_entries[i].hash);
404 lte = lookup_resource(lookup_table, dentry->ads_entries[i].hash);
406 print_lookup_table_entry(lte, NULL);
413 static inline void dentry_common_init(struct dentry *dentry)
415 memset(dentry, 0, sizeof(struct dentry));
417 dentry->security_id = -1;
421 * Creates an unlinked directory entry.
423 * @name: The base name of the new dentry.
424 * @return: A pointer to the new dentry, or NULL if out of memory.
426 struct dentry *new_dentry(const char *name)
428 struct dentry *dentry;
430 dentry = MALLOC(sizeof(struct dentry));
434 dentry_common_init(dentry);
435 if (change_dentry_name(dentry, name) != 0) {
440 dentry_update_all_timestamps(dentry);
441 dentry->next = dentry;
442 dentry->prev = dentry;
443 dentry->parent = dentry;
447 static void dentry_free_ads_entries(struct dentry *dentry)
449 for (u16 i = 0; i < dentry->num_ads; i++) {
450 FREE(dentry->ads_entries[i].stream_name);
451 FREE(dentry->ads_entries[i].stream_name_utf8);
453 FREE(dentry->ads_entries);
454 dentry->ads_entries = NULL;
459 void free_dentry(struct dentry *dentry)
461 FREE(dentry->file_name);
462 FREE(dentry->file_name_utf8);
463 FREE(dentry->short_name);
464 FREE(dentry->full_path_utf8);
465 dentry_free_ads_entries(dentry);
469 /* Arguments for do_free_dentry(). */
470 struct free_dentry_args {
471 struct lookup_table *lookup_table;
472 bool lt_decrement_refcnt;
476 * This function is passed as an argument to for_dentry_in_tree_depth() in order
477 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
479 static int do_free_dentry(struct dentry *dentry, void *__args)
481 struct free_dentry_args *args = (struct free_dentry_args*)__args;
483 if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
484 lookup_table_decrement_refcnt(args->lookup_table,
488 wimlib_assert(dentry->refcnt >= 1);
489 if (--dentry->refcnt == 0)
495 * Unlinks and frees a dentry tree.
497 * @root: The root of the tree.
498 * @lookup_table: The lookup table for dentries.
499 * @decrement_refcnt: True if the dentries in the tree are to have their
500 * reference counts in the lookup table decremented.
502 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table,
503 bool lt_decrement_refcnt)
505 if (!root || !root->parent)
508 struct free_dentry_args args;
509 args.lookup_table = lookup_table;
510 args.lt_decrement_refcnt = lt_decrement_refcnt;
511 for_dentry_in_tree_depth(root, do_free_dentry, &args);
514 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
521 * Links a dentry into the directory tree.
523 * @dentry: The dentry to link.
524 * @parent: The dentry that will be the parent of @dentry.
526 void link_dentry(struct dentry *dentry, struct dentry *parent)
528 dentry->parent = parent;
529 if (parent->children) {
530 /* Not an only child; link to siblings. */
531 dentry->next = parent->children;
532 dentry->prev = parent->children->prev;
533 dentry->next->prev = dentry;
534 dentry->prev->next = dentry;
536 /* Only child; link to parent. */
537 parent->children = dentry;
538 dentry->next = dentry;
539 dentry->prev = dentry;
543 /* Unlink a dentry from the directory tree. */
544 void unlink_dentry(struct dentry *dentry)
546 if (dentry_is_root(dentry))
548 if (dentry_is_only_child(dentry)) {
549 dentry->parent->children = NULL;
551 if (dentry_is_first_sibling(dentry))
552 dentry->parent->children = dentry->next;
553 dentry->next->prev = dentry->prev;
554 dentry->prev->next = dentry->next;
559 /* Recalculates the length of @dentry based on its file name length and short
561 static inline void recalculate_dentry_size(struct dentry *dentry)
563 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
564 2 + dentry->short_name_len;
565 /* Must be multiple of 8. */
566 dentry->length = (dentry->length + 7) & ~7;
569 /* Changes the name of a dentry to @new_name. Only changes the file_name and
570 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
571 * full_path_utf8 fields. Also recalculates its length. */
572 int change_dentry_name(struct dentry *dentry, const char *new_name)
577 FREE(dentry->file_name);
579 utf8_len = strlen(new_name);
581 dentry->file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
583 if (!dentry->file_name)
584 return WIMLIB_ERR_NOMEM;
586 FREE(dentry->file_name_utf8);
587 dentry->file_name_utf8 = MALLOC(utf8_len + 1);
588 if (!dentry->file_name_utf8) {
589 FREE(dentry->file_name);
590 dentry->file_name = NULL;
591 return WIMLIB_ERR_NOMEM;
594 dentry->file_name_len = utf16_len;
595 dentry->file_name_utf8_len = utf8_len;
596 memcpy(dentry->file_name_utf8, new_name, utf8_len + 1);
597 recalculate_dentry_size(dentry);
601 /* Parameters for calculate_dentry_statistics(). */
602 struct image_statistics {
603 struct lookup_table *lookup_table;
607 u64 *hard_link_bytes;
610 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
612 struct image_statistics *stats;
613 struct lookup_table_entry *lte;
616 lte = lookup_resource(stats->lookup_table, dentry->hash);
618 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
621 ++*stats->file_count;
624 u64 size = lte->resource_entry.original_size;
625 *stats->total_bytes += size;
626 if (++lte->out_refcnt == 1)
627 *stats->hard_link_bytes += size;
632 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
633 u64 *dir_count_ret, u64 *file_count_ret,
634 u64 *total_bytes_ret,
635 u64 *hard_link_bytes_ret)
637 struct image_statistics stats;
640 *total_bytes_ret = 0;
641 *hard_link_bytes_ret = 0;
642 stats.lookup_table = table;
643 stats.dir_count = dir_count_ret;
644 stats.file_count = file_count_ret;
645 stats.total_bytes = total_bytes_ret;
646 stats.hard_link_bytes = hard_link_bytes_ret;
647 for_lookup_table_entry(table, zero_out_refcnts, NULL);
648 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
651 static int read_ads_entries(const u8 *p, struct dentry *dentry,
654 u16 num_ads = dentry->num_ads;
655 struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
658 ERROR("Could not allocate memory for %"PRIu16" "
659 "alternate data stream entries", num_ads);
660 return WIMLIB_ERR_NOMEM;
662 DEBUG2("Reading %"PRIu16" alternate data streams "
663 "(remaining size = %"PRIu64")", num_ads, remaining_size);
665 for (u16 i = 0; i < num_ads; i++) {
666 struct ads_entry *cur_entry = &ads_entries[i];
669 const char *p_save = p;
670 /* Read the base stream entry, excluding the stream name. */
671 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
672 ERROR("Stream entries go past end of metadata resource");
673 ERROR("(remaining_size = %"PRIu64")", remaining_size);
674 ret = WIMLIB_ERR_INVALID_DENTRY;
675 goto out_free_ads_entries;
677 remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
679 p = get_u64(p, &length); /* ADS entry length */
681 DEBUG2("ADS length = %"PRIu64, length);
684 p = get_bytes(p, WIM_HASH_SIZE, (u8*)cur_entry->hash);
685 p = get_u16(p, &cur_entry->stream_name_len);
687 DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
689 cur_entry->stream_name = NULL;
690 cur_entry->stream_name_utf8 = NULL;
692 if (remaining_size < cur_entry->stream_name_len + 2) {
693 ERROR("Stream entries go past end of metadata resource");
694 ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
695 "= %"PRIu16" bytes", remaining_size,
696 cur_entry->stream_name_len);
697 ret = WIMLIB_ERR_INVALID_DENTRY;
698 goto out_free_ads_entries;
700 remaining_size -= cur_entry->stream_name_len + 2;
702 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
703 if (!cur_entry->stream_name) {
704 ret = WIMLIB_ERR_NOMEM;
705 goto out_free_ads_entries;
707 get_bytes(p, cur_entry->stream_name_len,
708 (u8*)cur_entry->stream_name);
709 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
710 cur_entry->stream_name_len,
712 cur_entry->stream_name_len_utf8 = utf8_len;
714 if (!cur_entry->stream_name_utf8) {
715 ret = WIMLIB_ERR_NOMEM;
716 goto out_free_ads_entries;
718 p = p_save + ads_entry_length(cur_entry);
720 dentry->ads_entries = ads_entries;
722 out_free_ads_entries:
723 for (u16 i = 0; i < num_ads; i++) {
724 FREE(ads_entries[i].stream_name);
725 FREE(ads_entries[i].stream_name_utf8);
732 * Reads a directory entry from the metadata resource.
734 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
735 u64 offset, struct dentry *dentry)
740 char *file_name_utf8;
744 size_t file_name_utf8_len;
747 dentry_common_init(dentry);
749 /*Make sure the dentry really fits into the metadata resource.*/
750 if (offset + 8 > metadata_resource_len) {
751 ERROR("Directory entry starting at %"PRIu64" ends past the "
752 "end of the metadata resource (size %"PRIu64")",
753 offset, metadata_resource_len);
754 return WIMLIB_ERR_INVALID_DENTRY;
757 /* Before reading the whole entry, we need to read just the length.
758 * This is because an entry of length 8 (that is, just the length field)
759 * terminates the list of sibling directory entries. */
761 p = get_u64(&metadata_resource[offset], &dentry->length);
763 /* A zero length field (really a length of 8, since that's how big the
764 * directory entry is...) indicates that this is the end of directory
765 * dentry. We do not read it into memory as an actual dentry, so just
766 * return true in that case. */
767 if (dentry->length == 0)
770 if (offset + dentry->length >= metadata_resource_len) {
771 ERROR("Directory entry at offset %"PRIu64" and with size "
772 "%"PRIu64" ends past the end of the metadata resource "
774 offset, dentry->length, metadata_resource_len);
775 return WIMLIB_ERR_INVALID_DENTRY;
778 /* If it is a recognized length, read the rest of the directory entry.
779 * Note: The root directory entry has no name, and its length does not
780 * include the short name length field. */
781 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
782 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
784 return WIMLIB_ERR_INVALID_DENTRY;
787 p = get_u32(p, &dentry->attributes);
788 p = get_u32(p, (u32*)&dentry->security_id);
789 p = get_u64(p, &dentry->subdir_offset);
791 /* 2 unused fields */
792 p += 2 * sizeof(u64);
793 /*p = get_u64(p, &dentry->unused1);*/
794 /*p = get_u64(p, &dentry->unused2);*/
796 p = get_u64(p, &dentry->creation_time);
797 p = get_u64(p, &dentry->last_access_time);
798 p = get_u64(p, &dentry->last_write_time);
800 p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
803 * I don't know what's going on here. It seems like M$ screwed up the
804 * reparse points, then put the fields in the same place and didn't
805 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
806 * have something to do with this, but it's not documented.
808 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
813 p = get_u32(p, &dentry->reparse_tag);
816 dentry->hard_link = (u64)(u1) | ((u64)(u2) << 32);
818 p = get_u32(p, &dentry->reparse_tag);
819 p = get_u64(p, &dentry->hard_link);
822 /* By the way, the reparse_reserved field does not actually exist (at
823 * least when the file is not a reparse point) */
826 p = get_u16(p, &dentry->num_ads);
828 p = get_u16(p, &short_name_len);
829 p = get_u16(p, &file_name_len);
831 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
834 if (dentry->length < calculated_size) {
835 ERROR("Unexpected end of directory entry! (Expected "
836 "%"PRIu64" bytes, got %"PRIu64" bytes. "
837 "short_name_len = %hu, file_name_len = %hu)",
838 calculated_size, dentry->length,
839 short_name_len, file_name_len);
840 return WIMLIB_ERR_INVALID_DENTRY;
843 /* Read the filename. */
844 file_name = MALLOC(file_name_len);
846 ERROR("Failed to allocate %hu bytes for dentry file name",
848 return WIMLIB_ERR_NOMEM;
850 p = get_bytes(p, file_name_len, file_name);
852 /* Convert filename to UTF-8. */
853 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
854 &file_name_utf8_len);
856 if (!file_name_utf8) {
857 ERROR("Failed to allocate memory to convert UTF-16 "
858 "filename (%hu bytes) to UTF-8", file_name_len);
859 ret = WIMLIB_ERR_NOMEM;
860 goto out_free_file_name;
863 /* Undocumented padding between file name and short name. This probably
864 * is supposed to be a terminating null character. */
867 /* Read the short filename. */
868 short_name = MALLOC(short_name_len);
870 ERROR("Failed to allocate %hu bytes for short filename",
872 ret = WIMLIB_ERR_NOMEM;
873 goto out_free_file_name_utf8;
876 p = get_bytes(p, short_name_len, short_name);
878 /* Some directory entries inexplicibly have a little over 70 bytes of
879 * extra data. The exact amount of data seems to be 72 bytes, but it is
880 * aligned on the next 8-byte boundary. Here's an example of the
883 * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
884 * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
885 * 0000000000000000000000
887 * Here's one interpretation of how the data is laid out.
890 * u32 field1; (always 0x00000001)
891 * u32 field2; (always 0x40000000)
897 * u64 reserved1; (always 0)
898 * u64 reserved2; (always 0)
901 if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
902 printf("%s: %lu / %lu (", file_name_utf8,
903 calculated_size, dentry->length);
904 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
906 print_byte_field(p, dentry->length - calculated_size);
911 if (dentry->num_ads != 0) {
912 calculated_size = (calculated_size + 7) & ~7;
913 if (calculated_size > metadata_resource_len - offset) {
914 ERROR("Not enough space in metadata resource for "
915 "alternate stream entries");
916 ret = WIMLIB_ERR_INVALID_DENTRY;
917 goto out_free_short_name;
919 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
921 metadata_resource_len - offset - calculated_size);
923 goto out_free_short_name;
926 dentry->file_name = file_name;
927 dentry->file_name_utf8 = file_name_utf8;
928 dentry->short_name = short_name;
929 dentry->file_name_len = file_name_len;
930 dentry->file_name_utf8_len = file_name_utf8_len;
931 dentry->short_name_len = short_name_len;
935 out_free_file_name_utf8:
936 FREE(file_name_utf8);
943 * Writes a dentry to an output buffer.
945 * @dentry: The dentry structure.
946 * @p: The memory location to write the data to.
947 * @return: Pointer to the byte after the last byte we wrote as part of the
950 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
954 memset(p, 0, dentry->length);
955 p = put_u64(p, dentry->length);
956 p = put_u32(p, dentry->attributes);
957 p = put_u32(p, dentry->security_id);
958 p = put_u64(p, dentry->subdir_offset);
959 p = put_u64(p, 0); /* unused1 */
960 p = put_u64(p, 0); /* unused2 */
961 p = put_u64(p, dentry->creation_time);
962 p = put_u64(p, dentry->last_access_time);
963 p = put_u64(p, dentry->last_write_time);
964 memcpy(p, dentry->hash, WIM_HASH_SIZE);
966 p = put_u32(p, dentry->reparse_tag);
967 p = put_u64(p, dentry->hard_link);
968 p = put_u16(p, dentry->num_ads); /*streams */
969 p = put_u16(p, dentry->short_name_len);
970 p = put_u16(p, dentry->file_name_len);
971 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
972 p = put_u16(p, 0); /* filename padding, 2 bytes. */
973 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
975 wimlib_assert(p - orig_p <= dentry->length);
976 if (p - orig_p < dentry->length)
977 p = put_zeroes(p, dentry->length - (p - orig_p));
979 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
981 for (u16 i = 0; i < dentry->num_ads; i++) {
982 p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
983 p = put_u64(p, 0); /* Unused */
984 p = put_bytes(p, WIM_HASH_SIZE, dentry->ads_entries[i].hash);
985 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
986 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
987 (u8*)dentry->ads_entries[i].stream_name);
988 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
993 /* Recursive function that writes a dentry tree rooted at @tree, not including
994 * @tree itself, which has already been written, except in the case of the root
995 * dentry, which is written right away, along with an end-of-directory entry. */
996 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
998 const struct dentry *child;
1000 if (dentry_is_root(tree)) {
1001 p = write_dentry(tree, p);
1003 /* write end of directory entry */
1006 /* Nothing to do for a regular file. */
1007 if (dentry_is_regular_file(tree))
1011 /* Write child dentries and end-of-directory entry. */
1012 child = tree->children;
1015 p = write_dentry(child, p);
1016 child = child->next;
1017 } while (child != tree->children);
1020 /* write end of directory entry */
1023 /* Recurse on children. */
1026 p = write_dentry_tree(child, p);
1027 child = child->next;
1028 } while (child != tree->children);
1033 /* Reads the children of a dentry, and all their children, ..., etc. from the
1034 * metadata resource and into the dentry tree.
1036 * @metadata_resource: An array that contains the uncompressed metadata
1037 * resource for the WIM file.
1038 * @metadata_resource_len: The length of @metadata_resource.
1039 * @dentry: A pointer to a struct dentry that is the root of the directory
1040 * tree and has already been read from the metadata resource. It
1041 * does not need to be the real root because this procedure is
1042 * called recursively.
1044 * @return: Zero on success, nonzero on failure.
1046 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1047 struct dentry *dentry)
1049 u64 cur_offset = dentry->subdir_offset;
1050 struct dentry *prev_child = NULL;
1051 struct dentry *first_child = NULL;
1052 struct dentry *child;
1053 struct dentry cur_child;
1056 /* If @dentry is a regular file, nothing more needs to be done for this
1058 if (cur_offset == 0)
1061 /* Find and read all the children of @dentry. */
1064 /* Read next child of @dentry into @cur_child. */
1065 ret = read_dentry(metadata_resource, metadata_resource_len,
1066 cur_offset, &cur_child);
1070 /* Check for end of directory. */
1071 if (cur_child.length == 0) {
1076 /* Not end of directory. Allocate this child permanently and
1077 * link it to the parent and previous child. */
1078 child = MALLOC(sizeof(struct dentry));
1080 ERROR("Failed to allocate %zu bytes for new dentry",
1081 sizeof(struct dentry));
1082 ret = WIMLIB_ERR_NOMEM;
1085 memcpy(child, &cur_child, sizeof(struct dentry));
1088 prev_child->next = child;
1089 child->prev = prev_child;
1091 first_child = child;
1094 child->parent = dentry;
1097 /* If there are children of this child, call this procedure
1099 if (child->subdir_offset != 0) {
1100 ret = read_dentry_tree(metadata_resource,
1101 metadata_resource_len, child);
1106 /* Advance to the offset of the next child. */
1107 cur_offset += dentry_total_length(child);
1110 /* Link last child to first one, and set parent's
1111 * children pointer to the first child. */
1113 prev_child->next = first_child;
1114 first_child->prev = prev_child;
1116 dentry->children = first_child;
1120 int dentry_set_symlink_buf(struct dentry *dentry, const u8 symlink_buf_hash[])
1122 struct ads_entry *ads_entries;
1124 ads_entries = CALLOC(2, sizeof(struct ads_entry));
1126 return WIMLIB_ERR_NOMEM;
1127 memcpy(ads_entries[1].hash, symlink_buf_hash, WIM_HASH_SIZE);
1128 dentry_free_ads_entries(dentry);
1129 dentry->num_ads = 2;
1130 dentry->ads_entries = ads_entries;