4 * A dentry (directory entry) contains the metadata for a file. In the WIM file
5 * format, the dentries are stored in the "metadata resource" section right
6 * after the security data. Each image in the WIM file has its own metadata
7 * resource with its own security data and dentry tree. Dentries in different
8 * images may share file resources by referring to the same lookup table
14 * Copyright (C) 2010 Carl Thijssen
15 * Copyright (C) 2012 Eric Biggers
17 * This file is part of wimlib, a library for working with WIM files.
19 * wimlib is free software; you can redistribute it and/or modify it under the
20 * terms of the GNU Lesser General Public License as published by the Free
21 * Software Foundation; either version 2.1 of the License, or (at your option)
24 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
25 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
26 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with wimlib; if not, see http://www.gnu.org/licenses/.
33 #include "wimlib_internal.h"
36 #include "timestamp.h"
37 #include "lookup_table.h"
43 * Returns true if @dentry has the UTF-8 file name @name that has length
46 static bool dentry_has_name(const struct dentry *dentry, const char *name,
49 if (dentry->file_name_utf8_len != name_len)
51 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
54 /* Real length of a dentry, including the alternate data stream entries, which
55 * are not included in the dentry->length field... */
56 u64 dentry_total_length(const struct dentry *dentry)
58 u64 length = (dentry->length + 7) & ~7;
59 for (u16 i = 0 ; i < dentry->num_ads; i++)
60 length += ads_entry_length(&dentry->ads_entries[i]);
64 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
65 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
67 if (S_ISLNK(stbuf->st_mode)) {
68 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
69 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
70 } else if (S_ISDIR(stbuf->st_mode)) {
71 dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
73 dentry->attributes = FILE_ATTRIBUTE_NORMAL;
75 if (sizeof(ino_t) >= 8)
76 dentry->hard_link = (u64)stbuf->st_ino;
78 dentry->hard_link = (u64)stbuf->st_ino |
79 ((u64)stbuf->st_dev << (sizeof(ino_t) * 8));
82 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
83 void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
84 const struct lookup_table *table)
86 struct lookup_table_entry *lte;
88 if (dentry_is_symlink(dentry))
89 stbuf->st_mode = S_IFLNK | 0777;
90 else if (dentry_is_directory(dentry))
91 stbuf->st_mode = S_IFDIR | 0755;
93 stbuf->st_mode = S_IFREG | 0644;
95 /* Use the size of the unnamed (default) file stream. */
96 if (table && (lte = __lookup_resource(table, dentry_hash(dentry))))
97 stbuf->st_size = lte->resource_entry.original_size;
101 stbuf->st_nlink = dentry_link_group_size(dentry);
102 stbuf->st_ino = dentry->hard_link;
103 stbuf->st_uid = getuid();
104 stbuf->st_gid = getgid();
105 stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
106 stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
107 stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
108 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
111 /* Makes all timestamp fields for the dentry be the current time. */
112 void dentry_update_all_timestamps(struct dentry *dentry)
114 u64 now = get_timestamp();
115 dentry->creation_time = now;
116 dentry->last_access_time = now;
117 dentry->last_write_time = now;
120 struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
121 const char *stream_name)
123 size_t stream_name_len = strlen(stream_name);
126 for (u16 i = 0; i < dentry->num_ads; i++)
127 if (ads_entry_has_name(&dentry->ads_entries[i],
128 stream_name, stream_name_len))
129 return &dentry->ads_entries[i];
133 /* Add an alternate stream entry to a dentry and return a pointer to it, or NULL
135 struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
137 u16 num_ads = dentry->num_ads + 1;
138 struct ads_entry *ads_entries;
139 struct ads_entry *new_entry;
140 if (num_ads == 0xffff)
142 ads_entries = MALLOC(num_ads * sizeof(struct ads_entry));
146 new_entry = &ads_entries[num_ads - 1];
147 if (change_ads_name(new_entry, stream_name) != 0) {
152 memcpy(ads_entries, dentry->ads_entries,
153 (num_ads - 1) * sizeof(struct ads_entry));
154 FREE(dentry->ads_entries);
155 dentry->ads_entries = ads_entries;
156 dentry->num_ads = num_ads;
157 return memset(new_entry, 0, sizeof(struct ads_entry));
160 void dentry_remove_ads(struct dentry *dentry, struct ads_entry *sentry)
162 destroy_ads_entry(sentry);
163 memcpy(sentry, sentry + 1,
164 (dentry->num_ads - (sentry - dentry->ads_entries))
165 * sizeof(struct ads_entry));
170 * Calls a function on all directory entries in a directory tree. It is called
171 * on a parent before its children.
173 int for_dentry_in_tree(struct dentry *root,
174 int (*visitor)(struct dentry*, void*), void *arg)
177 struct dentry *child;
179 ret = visitor(root, arg);
184 child = root->children;
190 ret = for_dentry_in_tree(child, visitor, arg);
194 } while (child != root->children);
199 * Like for_dentry_in_tree(), but the visitor function is always called on a
200 * dentry's children before on itself.
202 int for_dentry_in_tree_depth(struct dentry *root,
203 int (*visitor)(struct dentry*, void*), void *arg)
206 struct dentry *child;
209 child = root->children;
213 ret = for_dentry_in_tree_depth(child, visitor, arg);
217 } while (child != root->children);
219 return visitor(root, arg);
223 * Calculate the full path of @dentry, based on its parent's full path and on
224 * its UTF-8 file name.
226 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
230 if (dentry_is_root(dentry)) {
231 full_path = MALLOC(2);
238 char *parent_full_path;
239 u32 parent_full_path_len;
240 const struct dentry *parent = dentry->parent;
242 if (dentry_is_root(parent)) {
243 parent_full_path = "";
244 parent_full_path_len = 0;
246 parent_full_path = parent->full_path_utf8;
247 parent_full_path_len = parent->full_path_utf8_len;
250 full_path_len = parent_full_path_len + 1 +
251 dentry->file_name_utf8_len;
252 full_path = MALLOC(full_path_len + 1);
256 memcpy(full_path, parent_full_path, parent_full_path_len);
257 full_path[parent_full_path_len] = '/';
258 memcpy(full_path + parent_full_path_len + 1,
259 dentry->file_name_utf8,
260 dentry->file_name_utf8_len);
261 full_path[full_path_len] = '\0';
263 FREE(dentry->full_path_utf8);
264 dentry->full_path_utf8 = full_path;
265 dentry->full_path_utf8_len = full_path_len;
268 ERROR("Out of memory while calculating dentry full path");
269 return WIMLIB_ERR_NOMEM;
273 * Recursively calculates the subdir offsets for a directory tree.
275 * @dentry: The root of the directory tree.
276 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
277 * offset for @dentry.
279 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
281 struct dentry *child;
283 child = dentry->children;
284 dentry->subdir_offset = *subdir_offset_p;
287 /* Advance the subdir offset by the amount of space the children
288 * of this dentry take up. */
290 *subdir_offset_p += dentry_total_length(child);
292 } while (child != dentry->children);
294 /* End-of-directory dentry on disk. */
295 *subdir_offset_p += 8;
297 /* Recursively call calculate_subdir_offsets() on all the
300 calculate_subdir_offsets(child, subdir_offset_p);
302 } while (child != dentry->children);
304 /* On disk, childless directories have a valid subdir_offset
305 * that points to an 8-byte end-of-directory dentry. Regular
306 * files have a subdir_offset of 0. */
307 if (dentry_is_directory(dentry))
308 *subdir_offset_p += 8;
310 dentry->subdir_offset = 0;
315 /* Returns the child of @dentry that has the file name @name.
316 * Returns NULL if no child has the name. */
317 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
320 struct dentry *child;
323 child = dentry->children;
325 name_len = strlen(name);
327 if (dentry_has_name(child, name, name_len))
330 } while (child != dentry->children);
335 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
336 * @cur_dir. Returns NULL if no dentry having the path is found. */
337 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
339 struct dentry *child;
341 const char *new_path;
346 child = cur_dir->children;
348 new_path = path_next_part(path, &base_len);
350 if (dentry_has_name(child, path, base_len))
351 return get_dentry_relative_path(child, new_path);
353 } while (child != cur_dir->children);
358 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
360 struct dentry *get_dentry(WIMStruct *w, const char *path)
362 struct dentry *root = wim_root_dentry(w);
365 return get_dentry_relative_path(root, path);
368 /* Returns the parent directory for the @path. */
369 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
371 size_t path_len = strlen(path);
372 char buf[path_len + 1];
374 memcpy(buf, path, path_len + 1);
376 to_parent_name(buf, path_len);
378 return get_dentry(w, buf);
381 /* Prints the full path of a dentry. */
382 int print_dentry_full_path(struct dentry *dentry, void *ignore)
384 if (dentry->full_path_utf8)
385 puts(dentry->full_path_utf8);
389 struct file_attr_flag {
393 struct file_attr_flag file_attr_flags[] = {
394 {FILE_ATTRIBUTE_READONLY, "READONLY"},
395 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
396 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
397 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
398 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
399 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
400 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
401 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
402 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
403 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
404 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
405 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
406 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
407 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
408 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
411 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
412 * NULL if the resource entry for the dentry is not to be printed. */
413 int print_dentry(struct dentry *dentry, void *lookup_table)
415 struct lookup_table_entry *lte;
418 printf("[DENTRY]\n");
419 printf("Length = %"PRIu64"\n", dentry->length);
420 printf("Attributes = 0x%x\n", dentry->attributes);
421 for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
422 if (file_attr_flags[i].flag & dentry->attributes)
423 printf(" FILE_ATTRIBUTE_%s is set\n",
424 file_attr_flags[i].name);
425 printf("Security ID = %d\n", dentry->security_id);
426 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
427 /*printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);*/
428 /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
429 printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
430 printf("Last Access Time = 0x%"PRIx64"\n", dentry->last_access_time);
431 printf("Last Write Time = 0x%"PRIx64"\n", dentry->last_write_time);
433 print_hash(dentry->hash);
435 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
436 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
437 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
438 printf("Filename = \"");
439 print_string(dentry->file_name, dentry->file_name_len);
441 printf("Filename Length = %hu\n", dentry->file_name_len);
442 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
443 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
444 printf("Short Name = \"");
445 print_string(dentry->short_name, dentry->short_name_len);
447 printf("Short Name Length = %hu\n", dentry->short_name_len);
448 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
449 if (lookup_table && (lte = __lookup_resource(lookup_table, dentry->hash)))
450 print_lookup_table_entry(lte, NULL);
453 for (u16 i = 0; i < dentry->num_ads; i++) {
454 printf("[Alternate Stream Entry %u]\n", i);
455 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
456 printf("Name Length (UTF-16) = %u\n",
457 dentry->ads_entries[i].stream_name_len);
459 print_hash(dentry->ads_entries[i].hash);
461 (lte = __lookup_resource(lookup_table,
462 dentry->ads_entries[i].hash)))
464 print_lookup_table_entry(lte, NULL);
472 static inline void dentry_common_init(struct dentry *dentry)
474 memset(dentry, 0, sizeof(struct dentry));
476 dentry->security_id = -1;
480 * Creates an unlinked directory entry.
482 * @name: The base name of the new dentry.
483 * @return: A pointer to the new dentry, or NULL if out of memory.
485 struct dentry *new_dentry(const char *name)
487 struct dentry *dentry;
489 dentry = MALLOC(sizeof(struct dentry));
493 dentry_common_init(dentry);
494 if (change_dentry_name(dentry, name) != 0)
497 dentry_update_all_timestamps(dentry);
498 dentry->next = dentry;
499 dentry->prev = dentry;
500 dentry->parent = dentry;
504 ERROR("Failed to allocate new dentry");
508 void dentry_free_ads_entries(struct dentry *dentry)
510 for (u16 i = 0; i < dentry->num_ads; i++)
511 destroy_ads_entry(&dentry->ads_entries[i]);
512 FREE(dentry->ads_entries);
513 dentry->ads_entries = NULL;
518 void free_dentry(struct dentry *dentry)
520 FREE(dentry->file_name);
521 FREE(dentry->file_name_utf8);
522 FREE(dentry->short_name);
523 FREE(dentry->full_path_utf8);
524 dentry_free_ads_entries(dentry);
531 * - memory for file names is not cloned
532 * - next, prev, and children pointers and not touched
533 * - stream entries are not cloned.
535 struct dentry *clone_dentry(struct dentry *old)
537 struct dentry *new = MALLOC(sizeof(struct dentry));
540 memcpy(new, old, sizeof(struct dentry));
541 new->file_name = NULL;
542 new->file_name_len = 0;
543 new->file_name_utf8 = NULL;
544 new->file_name_utf8_len = 0;
545 new->short_name = NULL;
546 new->short_name_len = 0;
550 /* Arguments for do_free_dentry(). */
551 struct free_dentry_args {
552 struct lookup_table *lookup_table;
553 bool lt_decrement_refcnt;
557 * This function is passed as an argument to for_dentry_in_tree_depth() in order
558 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
560 static int do_free_dentry(struct dentry *dentry, void *__args)
562 struct free_dentry_args *args = (struct free_dentry_args*)__args;
564 if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
565 lookup_table_decrement_refcnt(args->lookup_table,
569 wimlib_assert(dentry->refcnt >= 1);
570 if (--dentry->refcnt == 0)
576 * Unlinks and frees a dentry tree.
578 * @root: The root of the tree.
579 * @lookup_table: The lookup table for dentries.
580 * @decrement_refcnt: True if the dentries in the tree are to have their
581 * reference counts in the lookup table decremented.
583 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table,
584 bool lt_decrement_refcnt)
586 if (!root || !root->parent)
589 struct free_dentry_args args;
590 args.lookup_table = lookup_table;
591 args.lt_decrement_refcnt = lt_decrement_refcnt;
592 for_dentry_in_tree_depth(root, do_free_dentry, &args);
595 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
602 * Links a dentry into the directory tree.
604 * @dentry: The dentry to link.
605 * @parent: The dentry that will be the parent of @dentry.
607 void link_dentry(struct dentry *dentry, struct dentry *parent)
609 dentry->parent = parent;
610 if (parent->children) {
611 /* Not an only child; link to siblings. */
612 dentry->next = parent->children;
613 dentry->prev = parent->children->prev;
614 dentry->next->prev = dentry;
615 dentry->prev->next = dentry;
617 /* Only child; link to parent. */
618 parent->children = dentry;
619 dentry->next = dentry;
620 dentry->prev = dentry;
624 /* Unlink a dentry from the directory tree. */
625 void unlink_dentry(struct dentry *dentry)
627 if (dentry_is_root(dentry))
629 if (dentry_is_only_child(dentry)) {
630 dentry->parent->children = NULL;
632 if (dentry_is_first_sibling(dentry))
633 dentry->parent->children = dentry->next;
634 dentry->next->prev = dentry->prev;
635 dentry->prev->next = dentry->next;
640 /* Recalculates the length of @dentry based on its file name length and short
642 static inline void recalculate_dentry_size(struct dentry *dentry)
644 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
645 2 + dentry->short_name_len;
646 /* Must be multiple of 8. */
647 dentry->length = (dentry->length + 7) & ~7;
650 static int do_name_change(char **file_name_ret,
651 char **file_name_utf8_ret,
652 u16 *file_name_len_ret,
653 u16 *file_name_utf8_len_ret,
654 const char *new_name)
658 char *file_name, *file_name_utf8;
660 utf8_len = strlen(new_name);
662 file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
665 return WIMLIB_ERR_NOMEM;
667 file_name_utf8 = MALLOC(utf8_len + 1);
668 if (!file_name_utf8) {
670 return WIMLIB_ERR_NOMEM;
672 memcpy(file_name_utf8, new_name, utf8_len + 1);
674 FREE(*file_name_ret);
675 FREE(*file_name_utf8_ret);
676 *file_name_ret = file_name;
677 *file_name_utf8_ret = file_name_utf8;
678 *file_name_len_ret = utf16_len;
679 *file_name_utf8_len_ret = utf8_len;
683 /* Changes the name of a dentry to @new_name. Only changes the file_name and
684 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
685 * full_path_utf8 fields. Also recalculates its length. */
686 int change_dentry_name(struct dentry *dentry, const char *new_name)
690 ret = do_name_change(&dentry->file_name, &dentry->file_name_utf8,
691 &dentry->file_name_len, &dentry->file_name_utf8_len,
694 recalculate_dentry_size(dentry);
698 int change_ads_name(struct ads_entry *entry, const char *new_name)
700 return do_name_change(&entry->stream_name, &entry->stream_name_utf8,
701 &entry->stream_name_len,
702 &entry->stream_name_utf8_len,
706 /* Parameters for calculate_dentry_statistics(). */
707 struct image_statistics {
708 struct lookup_table *lookup_table;
712 u64 *hard_link_bytes;
715 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
717 struct image_statistics *stats;
718 struct lookup_table_entry *lte;
723 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
726 ++*stats->file_count;
728 lte = __lookup_resource(stats->lookup_table, dentry->hash);
732 u64 size = lte->resource_entry.original_size;
733 *stats->total_bytes += size;
734 if (++lte->out_refcnt == 1)
735 *stats->hard_link_bytes += size;
737 if (i == dentry->num_ads)
739 lte = __lookup_resource(stats->lookup_table,
740 dentry->ads_entries[i].hash);
747 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
748 u64 *dir_count_ret, u64 *file_count_ret,
749 u64 *total_bytes_ret,
750 u64 *hard_link_bytes_ret)
752 struct image_statistics stats;
755 *total_bytes_ret = 0;
756 *hard_link_bytes_ret = 0;
757 stats.lookup_table = table;
758 stats.dir_count = dir_count_ret;
759 stats.file_count = file_count_ret;
760 stats.total_bytes = total_bytes_ret;
761 stats.hard_link_bytes = hard_link_bytes_ret;
762 for_lookup_table_entry(table, zero_out_refcnts, NULL);
763 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
766 static int read_ads_entries(const u8 *p, struct dentry *dentry,
769 u16 num_ads = dentry->num_ads;
770 struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
773 ERROR("Could not allocate memory for %"PRIu16" "
774 "alternate data stream entries", num_ads);
775 return WIMLIB_ERR_NOMEM;
777 DEBUG2("Reading %"PRIu16" alternate data streams "
778 "(remaining size = %"PRIu64")", num_ads, remaining_size);
780 for (u16 i = 0; i < num_ads; i++) {
781 struct ads_entry *cur_entry = &ads_entries[i];
784 const char *p_save = p;
785 /* Read the base stream entry, excluding the stream name. */
786 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
787 ERROR("Stream entries go past end of metadata resource");
788 ERROR("(remaining_size = %"PRIu64")", remaining_size);
789 ret = WIMLIB_ERR_INVALID_DENTRY;
790 goto out_free_ads_entries;
792 remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
794 p = get_u64(p, &length); /* ADS entry length */
796 DEBUG2("ADS length = %"PRIu64, length);
799 p = get_bytes(p, WIM_HASH_SIZE, (u8*)cur_entry->hash);
800 p = get_u16(p, &cur_entry->stream_name_len);
802 DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
804 cur_entry->stream_name = NULL;
805 cur_entry->stream_name_utf8 = NULL;
807 if (remaining_size < cur_entry->stream_name_len + 2) {
808 ERROR("Stream entries go past end of metadata resource");
809 ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
810 "= %"PRIu16" bytes", remaining_size,
811 cur_entry->stream_name_len);
812 ret = WIMLIB_ERR_INVALID_DENTRY;
813 goto out_free_ads_entries;
815 remaining_size -= cur_entry->stream_name_len + 2;
817 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
818 if (!cur_entry->stream_name) {
819 ret = WIMLIB_ERR_NOMEM;
820 goto out_free_ads_entries;
822 get_bytes(p, cur_entry->stream_name_len,
823 (u8*)cur_entry->stream_name);
824 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
825 cur_entry->stream_name_len,
827 cur_entry->stream_name_utf8_len = utf8_len;
829 if (!cur_entry->stream_name_utf8) {
830 ret = WIMLIB_ERR_NOMEM;
831 goto out_free_ads_entries;
833 p = p_save + ads_entry_length(cur_entry);
835 dentry->ads_entries = ads_entries;
837 out_free_ads_entries:
838 for (u16 i = 0; i < num_ads; i++) {
839 FREE(ads_entries[i].stream_name);
840 FREE(ads_entries[i].stream_name_utf8);
847 * Reads a directory entry from the metadata resource.
849 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
850 u64 offset, struct dentry *dentry)
855 char *file_name_utf8;
859 size_t file_name_utf8_len;
862 dentry_common_init(dentry);
864 /*Make sure the dentry really fits into the metadata resource.*/
865 if (offset + 8 > metadata_resource_len) {
866 ERROR("Directory entry starting at %"PRIu64" ends past the "
867 "end of the metadata resource (size %"PRIu64")",
868 offset, metadata_resource_len);
869 return WIMLIB_ERR_INVALID_DENTRY;
872 /* Before reading the whole entry, we need to read just the length.
873 * This is because an entry of length 8 (that is, just the length field)
874 * terminates the list of sibling directory entries. */
876 p = get_u64(&metadata_resource[offset], &dentry->length);
878 /* A zero length field (really a length of 8, since that's how big the
879 * directory entry is...) indicates that this is the end of directory
880 * dentry. We do not read it into memory as an actual dentry, so just
881 * return true in that case. */
882 if (dentry->length == 0)
885 if (offset + dentry->length >= metadata_resource_len) {
886 ERROR("Directory entry at offset %"PRIu64" and with size "
887 "%"PRIu64" ends past the end of the metadata resource "
889 offset, dentry->length, metadata_resource_len);
890 return WIMLIB_ERR_INVALID_DENTRY;
893 /* If it is a recognized length, read the rest of the directory entry.
894 * Note: The root directory entry has no name, and its length does not
895 * include the short name length field. */
896 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
897 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
899 return WIMLIB_ERR_INVALID_DENTRY;
902 p = get_u32(p, &dentry->attributes);
903 p = get_u32(p, (u32*)&dentry->security_id);
904 p = get_u64(p, &dentry->subdir_offset);
906 /* 2 unused fields */
907 p += 2 * sizeof(u64);
908 /*p = get_u64(p, &dentry->unused1);*/
909 /*p = get_u64(p, &dentry->unused2);*/
911 p = get_u64(p, &dentry->creation_time);
912 p = get_u64(p, &dentry->last_access_time);
913 p = get_u64(p, &dentry->last_write_time);
915 p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
918 * I don't know what's going on here. It seems like M$ screwed up the
919 * reparse points, then put the fields in the same place and didn't
920 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
921 * have something to do with this, but it's not documented.
923 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
926 p = get_u32(p, &dentry->reparse_tag);
929 p = get_u32(p, &dentry->reparse_tag);
930 p = get_u64(p, &dentry->hard_link);
933 /* By the way, the reparse_reserved field does not actually exist (at
934 * least when the file is not a reparse point) */
937 p = get_u16(p, &dentry->num_ads);
939 p = get_u16(p, &short_name_len);
940 p = get_u16(p, &file_name_len);
942 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
945 if (dentry->length < calculated_size) {
946 ERROR("Unexpected end of directory entry! (Expected "
947 "%"PRIu64" bytes, got %"PRIu64" bytes. "
948 "short_name_len = %hu, file_name_len = %hu)",
949 calculated_size, dentry->length,
950 short_name_len, file_name_len);
951 return WIMLIB_ERR_INVALID_DENTRY;
954 /* Read the filename. */
955 file_name = MALLOC(file_name_len);
957 ERROR("Failed to allocate %hu bytes for dentry file name",
959 return WIMLIB_ERR_NOMEM;
961 p = get_bytes(p, file_name_len, file_name);
963 /* Convert filename to UTF-8. */
964 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
965 &file_name_utf8_len);
967 if (!file_name_utf8) {
968 ERROR("Failed to allocate memory to convert UTF-16 "
969 "filename (%hu bytes) to UTF-8", file_name_len);
970 ret = WIMLIB_ERR_NOMEM;
971 goto out_free_file_name;
974 /* Undocumented padding between file name and short name. This probably
975 * is supposed to be a terminating null character. */
978 /* Read the short filename. */
979 short_name = MALLOC(short_name_len);
981 ERROR("Failed to allocate %hu bytes for short filename",
983 ret = WIMLIB_ERR_NOMEM;
984 goto out_free_file_name_utf8;
987 p = get_bytes(p, short_name_len, short_name);
989 /* Some directory entries inexplicibly have a little over 70 bytes of
990 * extra data. The exact amount of data seems to be 72 bytes, but it is
991 * aligned on the next 8-byte boundary. Here's an example of the
994 * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
995 * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
996 * 0000000000000000000000
998 * Here's one interpretation of how the data is laid out.
1001 * u32 field1; (always 0x00000001)
1002 * u32 field2; (always 0x40000000)
1007 * u8 data[48]; (???)
1008 * u64 reserved1; (always 0)
1009 * u64 reserved2; (always 0)
1012 if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
1013 printf("%s: %lu / %lu (", file_name_utf8,
1014 calculated_size, dentry->length);
1015 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
1017 print_byte_field(p, dentry->length - calculated_size);
1022 if (dentry->num_ads != 0) {
1023 calculated_size = (calculated_size + 7) & ~7;
1024 if (calculated_size > metadata_resource_len - offset) {
1025 ERROR("Not enough space in metadata resource for "
1026 "alternate stream entries");
1027 ret = WIMLIB_ERR_INVALID_DENTRY;
1028 goto out_free_short_name;
1030 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1032 metadata_resource_len - offset - calculated_size);
1034 goto out_free_short_name;
1037 dentry->file_name = file_name;
1038 dentry->file_name_utf8 = file_name_utf8;
1039 dentry->short_name = short_name;
1040 dentry->file_name_len = file_name_len;
1041 dentry->file_name_utf8_len = file_name_utf8_len;
1042 dentry->short_name_len = short_name_len;
1044 out_free_short_name:
1046 out_free_file_name_utf8:
1047 FREE(file_name_utf8);
1054 * Writes a dentry to an output buffer.
1056 * @dentry: The dentry structure.
1057 * @p: The memory location to write the data to.
1058 * @return: Pointer to the byte after the last byte we wrote as part of the
1061 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1066 p = put_u64(p, dentry->length);
1067 p = put_u32(p, dentry->attributes);
1068 p = put_u32(p, dentry->security_id);
1069 p = put_u64(p, dentry->subdir_offset);
1070 p = put_u64(p, 0); /* unused1 */
1071 p = put_u64(p, 0); /* unused2 */
1072 p = put_u64(p, dentry->creation_time);
1073 p = put_u64(p, dentry->last_access_time);
1074 p = put_u64(p, dentry->last_write_time);
1075 p = put_bytes(p, WIM_HASH_SIZE, dentry->hash);
1076 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1077 p = put_zeroes(p, 4);
1078 p = put_u32(p, dentry->reparse_tag);
1079 p = put_zeroes(p, 4);
1081 p = put_u32(p, dentry->reparse_tag);
1082 p = put_u64(p, dentry->hard_link);
1084 p = put_u16(p, dentry->num_ads);
1085 p = put_u16(p, dentry->short_name_len);
1086 p = put_u16(p, dentry->file_name_len);
1087 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1088 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1089 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1091 wimlib_assert(p - orig_p <= dentry->length);
1092 if (p - orig_p < dentry->length)
1093 p = put_zeroes(p, dentry->length - (p - orig_p));
1095 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1097 for (u16 i = 0; i < dentry->num_ads; i++) {
1098 p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
1099 p = put_u64(p, 0); /* Unused */
1100 p = put_bytes(p, WIM_HASH_SIZE, dentry->ads_entries[i].hash);
1101 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
1102 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
1103 (u8*)dentry->ads_entries[i].stream_name);
1104 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1109 /* Recursive function that writes a dentry tree rooted at @tree, not including
1110 * @tree itself, which has already been written, except in the case of the root
1111 * dentry, which is written right away, along with an end-of-directory entry. */
1112 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
1114 const struct dentry *child;
1116 if (dentry_is_root(tree)) {
1117 p = write_dentry(tree, p);
1119 /* write end of directory entry */
1122 /* Nothing to do for non-directories */
1123 if (!dentry_is_directory(tree))
1127 /* Write child dentries and end-of-directory entry. */
1128 child = tree->children;
1131 p = write_dentry(child, p);
1132 child = child->next;
1133 } while (child != tree->children);
1136 /* write end of directory entry */
1139 /* Recurse on children. */
1142 p = write_dentry_tree(child, p);
1143 child = child->next;
1144 } while (child != tree->children);
1149 /* Reads the children of a dentry, and all their children, ..., etc. from the
1150 * metadata resource and into the dentry tree.
1152 * @metadata_resource: An array that contains the uncompressed metadata
1153 * resource for the WIM file.
1154 * @metadata_resource_len: The length of @metadata_resource.
1155 * @dentry: A pointer to a struct dentry that is the root of the directory
1156 * tree and has already been read from the metadata resource. It
1157 * does not need to be the real root because this procedure is
1158 * called recursively.
1160 * @return: Zero on success, nonzero on failure.
1162 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1163 struct dentry *dentry)
1165 u64 cur_offset = dentry->subdir_offset;
1166 struct dentry *prev_child = NULL;
1167 struct dentry *first_child = NULL;
1168 struct dentry *child;
1169 struct dentry cur_child;
1172 /* If @dentry is a regular file, nothing more needs to be done for this
1174 if (cur_offset == 0)
1177 /* Find and read all the children of @dentry. */
1180 /* Read next child of @dentry into @cur_child. */
1181 ret = read_dentry(metadata_resource, metadata_resource_len,
1182 cur_offset, &cur_child);
1186 /* Check for end of directory. */
1187 if (cur_child.length == 0) {
1192 /* Not end of directory. Allocate this child permanently and
1193 * link it to the parent and previous child. */
1194 child = MALLOC(sizeof(struct dentry));
1196 ERROR("Failed to allocate %zu bytes for new dentry",
1197 sizeof(struct dentry));
1198 ret = WIMLIB_ERR_NOMEM;
1201 memcpy(child, &cur_child, sizeof(struct dentry));
1204 prev_child->next = child;
1205 child->prev = prev_child;
1207 first_child = child;
1210 child->parent = dentry;
1213 /* If there are children of this child, call this procedure
1215 if (child->subdir_offset != 0) {
1216 ret = read_dentry_tree(metadata_resource,
1217 metadata_resource_len, child);
1222 /* Advance to the offset of the next child. */
1223 cur_offset += dentry_total_length(child);
1226 /* Link last child to first one, and set parent's
1227 * children pointer to the first child. */
1229 prev_child->next = first_child;
1230 first_child->prev = prev_child;
1232 dentry->children = first_child;