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 static bool ads_entry_has_name(const struct ads_entry *entry,
55 const char *name, size_t name_len)
57 if (entry->stream_name_utf8_len != name_len)
59 return memcmp(entry->stream_name_utf8, name, name_len) == 0;
62 /* Real length of a dentry, including the alternate data stream entries, which
63 * are not included in the dentry->length field... */
64 u64 dentry_total_length(const struct dentry *dentry)
66 u64 length = (dentry->length + 7) & ~7;
67 for (u16 i = 0 ; i < dentry->num_ads; i++)
68 length += ads_entry_length(&dentry->ads_entries[i]);
72 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
73 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
75 if (S_ISLNK(stbuf->st_mode)) {
76 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
77 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
78 } else if (S_ISDIR(stbuf->st_mode)) {
79 dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
81 dentry->attributes = FILE_ATTRIBUTE_NORMAL;
83 if (sizeof(ino_t) >= 8)
84 dentry->hard_link = (u64)stbuf->st_ino;
86 dentry->hard_link = (u64)stbuf->st_ino |
87 ((u64)stbuf->st_dev << (sizeof(ino_t) * 8));
90 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
91 void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
92 const struct lookup_table *table)
94 struct lookup_table_entry *lte;
96 if (dentry_is_symlink(dentry))
97 stbuf->st_mode = S_IFLNK | 0777;
98 else if (dentry_is_directory(dentry))
99 stbuf->st_mode = S_IFDIR | 0755;
101 stbuf->st_mode = S_IFREG | 0644;
104 lte = __lookup_resource(table, dentry_hash(dentry));
109 stbuf->st_nlink = lte->refcnt;
110 stbuf->st_size = lte->resource_entry.original_size;
115 stbuf->st_uid = getuid();
116 stbuf->st_gid = getgid();
117 stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
118 stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
119 stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
120 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
123 /* Makes all timestamp fields for the dentry be the current time. */
124 void dentry_update_all_timestamps(struct dentry *dentry)
126 u64 now = get_timestamp();
127 dentry->creation_time = now;
128 dentry->last_access_time = now;
129 dentry->last_write_time = now;
132 struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
133 const char *stream_name)
135 size_t stream_name_len = strlen(stream_name);
138 for (u16 i = 0; i < dentry->num_ads; i++)
139 if (ads_entry_has_name(&dentry->ads_entries[i],
140 stream_name, stream_name_len))
141 return &dentry->ads_entries[i];
145 /* Add an alternate stream entry to a dentry and return a pointer to it, or NULL
147 struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
149 u16 num_ads = dentry->num_ads + 1;
150 struct ads_entry *ads_entries;
151 struct ads_entry *new_entry;
152 if (num_ads == 0xffff)
154 ads_entries = MALLOC(num_ads * sizeof(struct ads_entry));
158 new_entry = &ads_entries[num_ads - 1];
159 if (change_ads_name(new_entry, stream_name) != 0) {
164 memcpy(ads_entries, dentry->ads_entries,
165 (num_ads - 1) * sizeof(struct ads_entry));
166 FREE(dentry->ads_entries);
167 dentry->ads_entries = ads_entries;
168 dentry->num_ads = num_ads;
169 return memset(new_entry, 0, sizeof(struct ads_entry));
172 void dentry_remove_ads(struct dentry *dentry, struct ads_entry *sentry)
174 destroy_ads_entry(sentry);
175 memcpy(sentry, sentry + 1,
176 (dentry->num_ads - (sentry - dentry->ads_entries))
177 * sizeof(struct ads_entry));
182 * Calls a function on all directory entries in a directory tree. It is called
183 * on a parent before its children.
185 int for_dentry_in_tree(struct dentry *root,
186 int (*visitor)(struct dentry*, void*), void *arg)
189 struct dentry *child;
191 ret = visitor(root, arg);
196 child = root->children;
202 ret = for_dentry_in_tree(child, visitor, arg);
206 } while (child != root->children);
211 * Like for_dentry_in_tree(), but the visitor function is always called on a
212 * dentry's children before on itself.
214 int for_dentry_in_tree_depth(struct dentry *root,
215 int (*visitor)(struct dentry*, void*), void *arg)
218 struct dentry *child;
221 child = root->children;
225 ret = for_dentry_in_tree_depth(child, visitor, arg);
229 } while (child != root->children);
231 return visitor(root, arg);
235 * Calculate the full path of @dentry, based on its parent's full path and on
236 * its UTF-8 file name.
238 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
242 if (dentry_is_root(dentry)) {
243 full_path = MALLOC(2);
250 char *parent_full_path;
251 u32 parent_full_path_len;
252 const struct dentry *parent = dentry->parent;
254 if (dentry_is_root(parent)) {
255 parent_full_path = "";
256 parent_full_path_len = 0;
258 parent_full_path = parent->full_path_utf8;
259 parent_full_path_len = parent->full_path_utf8_len;
262 full_path_len = parent_full_path_len + 1 +
263 dentry->file_name_utf8_len;
264 full_path = MALLOC(full_path_len + 1);
268 memcpy(full_path, parent_full_path, parent_full_path_len);
269 full_path[parent_full_path_len] = '/';
270 memcpy(full_path + parent_full_path_len + 1,
271 dentry->file_name_utf8,
272 dentry->file_name_utf8_len);
273 full_path[full_path_len] = '\0';
275 FREE(dentry->full_path_utf8);
276 dentry->full_path_utf8 = full_path;
277 dentry->full_path_utf8_len = full_path_len;
280 ERROR("Out of memory while calculating dentry full path");
281 return WIMLIB_ERR_NOMEM;
285 * Recursively calculates the subdir offsets for a directory tree.
287 * @dentry: The root of the directory tree.
288 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
289 * offset for @dentry.
291 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
293 struct dentry *child;
295 child = dentry->children;
296 dentry->subdir_offset = *subdir_offset_p;
299 /* Advance the subdir offset by the amount of space the children
300 * of this dentry take up. */
302 *subdir_offset_p += dentry_total_length(child);
304 } while (child != dentry->children);
306 /* End-of-directory dentry on disk. */
307 *subdir_offset_p += 8;
309 /* Recursively call calculate_subdir_offsets() on all the
312 calculate_subdir_offsets(child, subdir_offset_p);
314 } while (child != dentry->children);
316 /* On disk, childless directories have a valid subdir_offset
317 * that points to an 8-byte end-of-directory dentry. Regular
318 * files have a subdir_offset of 0. */
319 if (dentry_is_directory(dentry))
320 *subdir_offset_p += 8;
322 dentry->subdir_offset = 0;
327 /* Returns the child of @dentry that has the file name @name.
328 * Returns NULL if no child has the name. */
329 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
332 struct dentry *child;
335 child = dentry->children;
337 name_len = strlen(name);
339 if (dentry_has_name(child, name, name_len))
342 } while (child != dentry->children);
347 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
348 * @cur_dir. Returns NULL if no dentry having the path is found. */
349 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
351 struct dentry *child;
353 const char *new_path;
358 child = cur_dir->children;
360 new_path = path_next_part(path, &base_len);
362 if (dentry_has_name(child, path, base_len))
363 return get_dentry_relative_path(child, new_path);
365 } while (child != cur_dir->children);
370 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
372 struct dentry *get_dentry(WIMStruct *w, const char *path)
374 struct dentry *root = wim_root_dentry(w);
377 return get_dentry_relative_path(root, path);
380 /* Returns the parent directory for the @path. */
381 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
383 size_t path_len = strlen(path);
384 char buf[path_len + 1];
386 memcpy(buf, path, path_len + 1);
388 to_parent_name(buf, path_len);
390 return get_dentry(w, buf);
393 /* Prints the full path of a dentry. */
394 int print_dentry_full_path(struct dentry *dentry, void *ignore)
396 if (dentry->full_path_utf8)
397 puts(dentry->full_path_utf8);
401 struct file_attr_flag {
405 struct file_attr_flag file_attr_flags[] = {
406 {FILE_ATTRIBUTE_READONLY, "READONLY"},
407 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
408 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
409 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
410 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
411 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
412 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
413 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
414 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
415 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
416 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
417 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
418 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
419 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
420 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
423 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
424 * NULL if the resource entry for the dentry is not to be printed. */
425 int print_dentry(struct dentry *dentry, void *lookup_table)
427 struct lookup_table_entry *lte;
430 printf("[DENTRY]\n");
431 printf("Length = %"PRIu64"\n", dentry->length);
432 printf("Attributes = 0x%x\n", dentry->attributes);
433 for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
434 if (file_attr_flags[i].flag & dentry->attributes)
435 printf(" FILE_ATTRIBUTE_%s is set\n",
436 file_attr_flags[i].name);
437 printf("Security ID = %d\n", dentry->security_id);
438 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
439 /*printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);*/
440 /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
441 printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
442 printf("Last Access Time = 0x%"PRIx64"\n", dentry->last_access_time);
443 printf("Last Write Time = 0x%"PRIx64"\n", dentry->last_write_time);
445 print_hash(dentry->hash);
447 printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
448 printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
449 printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
450 printf("Filename = \"");
451 print_string(dentry->file_name, dentry->file_name_len);
453 printf("Filename Length = %hu\n", dentry->file_name_len);
454 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
455 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
456 printf("Short Name = \"");
457 print_string(dentry->short_name, dentry->short_name_len);
459 printf("Short Name Length = %hu\n", dentry->short_name_len);
460 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
461 if (lookup_table && (lte = __lookup_resource(lookup_table, dentry->hash)))
462 print_lookup_table_entry(lte, NULL);
465 for (u16 i = 0; i < dentry->num_ads; i++) {
466 printf("[Alternate Stream Entry %u]\n", i);
467 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
468 printf("Name Length (UTF-16) = %u\n",
469 dentry->ads_entries[i].stream_name_len);
471 print_hash(dentry->ads_entries[i].hash);
473 (lte = __lookup_resource(lookup_table,
474 dentry->ads_entries[i].hash)))
476 print_lookup_table_entry(lte, NULL);
484 static inline void dentry_common_init(struct dentry *dentry)
486 memset(dentry, 0, sizeof(struct dentry));
488 dentry->security_id = -1;
492 * Creates an unlinked directory entry.
494 * @name: The base name of the new dentry.
495 * @return: A pointer to the new dentry, or NULL if out of memory.
497 struct dentry *new_dentry(const char *name)
499 struct dentry *dentry;
501 dentry = MALLOC(sizeof(struct dentry));
505 dentry_common_init(dentry);
506 if (change_dentry_name(dentry, name) != 0)
509 dentry_update_all_timestamps(dentry);
510 dentry->next = dentry;
511 dentry->prev = dentry;
512 dentry->parent = dentry;
516 ERROR("Failed to allocate new dentry");
520 static void dentry_free_ads_entries(struct dentry *dentry)
522 for (u16 i = 0; i < dentry->num_ads; i++)
523 destroy_ads_entry(&dentry->ads_entries[i]);
524 FREE(dentry->ads_entries);
525 dentry->ads_entries = NULL;
530 void free_dentry(struct dentry *dentry)
532 FREE(dentry->file_name);
533 FREE(dentry->file_name_utf8);
534 FREE(dentry->short_name);
535 FREE(dentry->full_path_utf8);
536 dentry_free_ads_entries(dentry);
540 /* Arguments for do_free_dentry(). */
541 struct free_dentry_args {
542 struct lookup_table *lookup_table;
543 bool lt_decrement_refcnt;
547 * This function is passed as an argument to for_dentry_in_tree_depth() in order
548 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
550 static int do_free_dentry(struct dentry *dentry, void *__args)
552 struct free_dentry_args *args = (struct free_dentry_args*)__args;
554 if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
555 lookup_table_decrement_refcnt(args->lookup_table,
559 wimlib_assert(dentry->refcnt >= 1);
560 if (--dentry->refcnt == 0)
566 * Unlinks and frees a dentry tree.
568 * @root: The root of the tree.
569 * @lookup_table: The lookup table for dentries.
570 * @decrement_refcnt: True if the dentries in the tree are to have their
571 * reference counts in the lookup table decremented.
573 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table,
574 bool lt_decrement_refcnt)
576 if (!root || !root->parent)
579 struct free_dentry_args args;
580 args.lookup_table = lookup_table;
581 args.lt_decrement_refcnt = lt_decrement_refcnt;
582 for_dentry_in_tree_depth(root, do_free_dentry, &args);
585 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
592 * Links a dentry into the directory tree.
594 * @dentry: The dentry to link.
595 * @parent: The dentry that will be the parent of @dentry.
597 void link_dentry(struct dentry *dentry, struct dentry *parent)
599 dentry->parent = parent;
600 if (parent->children) {
601 /* Not an only child; link to siblings. */
602 dentry->next = parent->children;
603 dentry->prev = parent->children->prev;
604 dentry->next->prev = dentry;
605 dentry->prev->next = dentry;
607 /* Only child; link to parent. */
608 parent->children = dentry;
609 dentry->next = dentry;
610 dentry->prev = dentry;
614 /* Unlink a dentry from the directory tree. */
615 void unlink_dentry(struct dentry *dentry)
617 if (dentry_is_root(dentry))
619 if (dentry_is_only_child(dentry)) {
620 dentry->parent->children = NULL;
622 if (dentry_is_first_sibling(dentry))
623 dentry->parent->children = dentry->next;
624 dentry->next->prev = dentry->prev;
625 dentry->prev->next = dentry->next;
630 /* Recalculates the length of @dentry based on its file name length and short
632 static inline void recalculate_dentry_size(struct dentry *dentry)
634 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
635 2 + dentry->short_name_len;
636 /* Must be multiple of 8. */
637 dentry->length = (dentry->length + 7) & ~7;
640 static int do_name_change(char **file_name_ret,
641 char **file_name_utf8_ret,
642 u16 *file_name_len_ret,
643 u16 *file_name_utf8_len_ret,
644 const char *new_name)
648 char *file_name, *file_name_utf8;
650 utf8_len = strlen(new_name);
652 file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
655 return WIMLIB_ERR_NOMEM;
657 file_name_utf8 = MALLOC(utf8_len + 1);
658 if (!file_name_utf8) {
660 return WIMLIB_ERR_NOMEM;
662 memcpy(file_name_utf8, new_name, utf8_len + 1);
664 FREE(*file_name_ret);
665 FREE(*file_name_utf8_ret);
666 *file_name_ret = file_name;
667 *file_name_utf8_ret = file_name_utf8;
668 *file_name_len_ret = utf16_len;
669 *file_name_utf8_len_ret = utf8_len;
673 /* Changes the name of a dentry to @new_name. Only changes the file_name and
674 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
675 * full_path_utf8 fields. Also recalculates its length. */
676 int change_dentry_name(struct dentry *dentry, const char *new_name)
680 ret = do_name_change(&dentry->file_name, &dentry->file_name_utf8,
681 &dentry->file_name_len, &dentry->file_name_utf8_len,
684 recalculate_dentry_size(dentry);
688 int change_ads_name(struct ads_entry *entry, const char *new_name)
690 return do_name_change(&entry->stream_name, &entry->stream_name_utf8,
691 &entry->stream_name_len,
692 &entry->stream_name_utf8_len,
696 /* Parameters for calculate_dentry_statistics(). */
697 struct image_statistics {
698 struct lookup_table *lookup_table;
702 u64 *hard_link_bytes;
705 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
707 struct image_statistics *stats;
708 struct lookup_table_entry *lte;
713 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
716 ++*stats->file_count;
718 lte = __lookup_resource(stats->lookup_table, dentry->hash);
722 u64 size = lte->resource_entry.original_size;
723 *stats->total_bytes += size;
724 if (++lte->out_refcnt == 1)
725 *stats->hard_link_bytes += size;
727 if (i == dentry->num_ads)
729 lte = __lookup_resource(stats->lookup_table,
730 dentry->ads_entries[i].hash);
737 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
738 u64 *dir_count_ret, u64 *file_count_ret,
739 u64 *total_bytes_ret,
740 u64 *hard_link_bytes_ret)
742 struct image_statistics stats;
745 *total_bytes_ret = 0;
746 *hard_link_bytes_ret = 0;
747 stats.lookup_table = table;
748 stats.dir_count = dir_count_ret;
749 stats.file_count = file_count_ret;
750 stats.total_bytes = total_bytes_ret;
751 stats.hard_link_bytes = hard_link_bytes_ret;
752 for_lookup_table_entry(table, zero_out_refcnts, NULL);
753 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
756 static int read_ads_entries(const u8 *p, struct dentry *dentry,
759 u16 num_ads = dentry->num_ads;
760 struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
763 ERROR("Could not allocate memory for %"PRIu16" "
764 "alternate data stream entries", num_ads);
765 return WIMLIB_ERR_NOMEM;
767 DEBUG2("Reading %"PRIu16" alternate data streams "
768 "(remaining size = %"PRIu64")", num_ads, remaining_size);
770 for (u16 i = 0; i < num_ads; i++) {
771 struct ads_entry *cur_entry = &ads_entries[i];
774 const char *p_save = p;
775 /* Read the base stream entry, excluding the stream name. */
776 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
777 ERROR("Stream entries go past end of metadata resource");
778 ERROR("(remaining_size = %"PRIu64")", remaining_size);
779 ret = WIMLIB_ERR_INVALID_DENTRY;
780 goto out_free_ads_entries;
782 remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
784 p = get_u64(p, &length); /* ADS entry length */
786 DEBUG2("ADS length = %"PRIu64, length);
789 p = get_bytes(p, WIM_HASH_SIZE, (u8*)cur_entry->hash);
790 p = get_u16(p, &cur_entry->stream_name_len);
792 DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
794 cur_entry->stream_name = NULL;
795 cur_entry->stream_name_utf8 = NULL;
797 if (remaining_size < cur_entry->stream_name_len + 2) {
798 ERROR("Stream entries go past end of metadata resource");
799 ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
800 "= %"PRIu16" bytes", remaining_size,
801 cur_entry->stream_name_len);
802 ret = WIMLIB_ERR_INVALID_DENTRY;
803 goto out_free_ads_entries;
805 remaining_size -= cur_entry->stream_name_len + 2;
807 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
808 if (!cur_entry->stream_name) {
809 ret = WIMLIB_ERR_NOMEM;
810 goto out_free_ads_entries;
812 get_bytes(p, cur_entry->stream_name_len,
813 (u8*)cur_entry->stream_name);
814 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
815 cur_entry->stream_name_len,
817 cur_entry->stream_name_utf8_len = utf8_len;
819 if (!cur_entry->stream_name_utf8) {
820 ret = WIMLIB_ERR_NOMEM;
821 goto out_free_ads_entries;
823 p = p_save + ads_entry_length(cur_entry);
825 dentry->ads_entries = ads_entries;
827 out_free_ads_entries:
828 for (u16 i = 0; i < num_ads; i++) {
829 FREE(ads_entries[i].stream_name);
830 FREE(ads_entries[i].stream_name_utf8);
837 * Reads a directory entry from the metadata resource.
839 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
840 u64 offset, struct dentry *dentry)
845 char *file_name_utf8;
849 size_t file_name_utf8_len;
852 dentry_common_init(dentry);
854 /*Make sure the dentry really fits into the metadata resource.*/
855 if (offset + 8 > metadata_resource_len) {
856 ERROR("Directory entry starting at %"PRIu64" ends past the "
857 "end of the metadata resource (size %"PRIu64")",
858 offset, metadata_resource_len);
859 return WIMLIB_ERR_INVALID_DENTRY;
862 /* Before reading the whole entry, we need to read just the length.
863 * This is because an entry of length 8 (that is, just the length field)
864 * terminates the list of sibling directory entries. */
866 p = get_u64(&metadata_resource[offset], &dentry->length);
868 /* A zero length field (really a length of 8, since that's how big the
869 * directory entry is...) indicates that this is the end of directory
870 * dentry. We do not read it into memory as an actual dentry, so just
871 * return true in that case. */
872 if (dentry->length == 0)
875 if (offset + dentry->length >= metadata_resource_len) {
876 ERROR("Directory entry at offset %"PRIu64" and with size "
877 "%"PRIu64" ends past the end of the metadata resource "
879 offset, dentry->length, metadata_resource_len);
880 return WIMLIB_ERR_INVALID_DENTRY;
883 /* If it is a recognized length, read the rest of the directory entry.
884 * Note: The root directory entry has no name, and its length does not
885 * include the short name length field. */
886 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
887 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
889 return WIMLIB_ERR_INVALID_DENTRY;
892 p = get_u32(p, &dentry->attributes);
893 p = get_u32(p, (u32*)&dentry->security_id);
894 p = get_u64(p, &dentry->subdir_offset);
896 /* 2 unused fields */
897 p += 2 * sizeof(u64);
898 /*p = get_u64(p, &dentry->unused1);*/
899 /*p = get_u64(p, &dentry->unused2);*/
901 p = get_u64(p, &dentry->creation_time);
902 p = get_u64(p, &dentry->last_access_time);
903 p = get_u64(p, &dentry->last_write_time);
905 p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
908 * I don't know what's going on here. It seems like M$ screwed up the
909 * reparse points, then put the fields in the same place and didn't
910 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
911 * have something to do with this, but it's not documented.
913 if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
918 p = get_u32(p, &dentry->reparse_tag);
921 dentry->hard_link = (u64)(u1) | ((u64)(u2) << 32);
923 p = get_u32(p, &dentry->reparse_tag);
924 p = get_u64(p, &dentry->hard_link);
927 /* By the way, the reparse_reserved field does not actually exist (at
928 * least when the file is not a reparse point) */
931 p = get_u16(p, &dentry->num_ads);
933 p = get_u16(p, &short_name_len);
934 p = get_u16(p, &file_name_len);
936 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
939 if (dentry->length < calculated_size) {
940 ERROR("Unexpected end of directory entry! (Expected "
941 "%"PRIu64" bytes, got %"PRIu64" bytes. "
942 "short_name_len = %hu, file_name_len = %hu)",
943 calculated_size, dentry->length,
944 short_name_len, file_name_len);
945 return WIMLIB_ERR_INVALID_DENTRY;
948 /* Read the filename. */
949 file_name = MALLOC(file_name_len);
951 ERROR("Failed to allocate %hu bytes for dentry file name",
953 return WIMLIB_ERR_NOMEM;
955 p = get_bytes(p, file_name_len, file_name);
957 /* Convert filename to UTF-8. */
958 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
959 &file_name_utf8_len);
961 if (!file_name_utf8) {
962 ERROR("Failed to allocate memory to convert UTF-16 "
963 "filename (%hu bytes) to UTF-8", file_name_len);
964 ret = WIMLIB_ERR_NOMEM;
965 goto out_free_file_name;
968 /* Undocumented padding between file name and short name. This probably
969 * is supposed to be a terminating null character. */
972 /* Read the short filename. */
973 short_name = MALLOC(short_name_len);
975 ERROR("Failed to allocate %hu bytes for short filename",
977 ret = WIMLIB_ERR_NOMEM;
978 goto out_free_file_name_utf8;
981 p = get_bytes(p, short_name_len, short_name);
983 /* Some directory entries inexplicibly have a little over 70 bytes of
984 * extra data. The exact amount of data seems to be 72 bytes, but it is
985 * aligned on the next 8-byte boundary. Here's an example of the
988 * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
989 * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
990 * 0000000000000000000000
992 * Here's one interpretation of how the data is laid out.
995 * u32 field1; (always 0x00000001)
996 * u32 field2; (always 0x40000000)
1001 * u8 data[48]; (???)
1002 * u64 reserved1; (always 0)
1003 * u64 reserved2; (always 0)
1006 if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
1007 printf("%s: %lu / %lu (", file_name_utf8,
1008 calculated_size, dentry->length);
1009 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
1011 print_byte_field(p, dentry->length - calculated_size);
1016 if (dentry->num_ads != 0) {
1017 calculated_size = (calculated_size + 7) & ~7;
1018 if (calculated_size > metadata_resource_len - offset) {
1019 ERROR("Not enough space in metadata resource for "
1020 "alternate stream entries");
1021 ret = WIMLIB_ERR_INVALID_DENTRY;
1022 goto out_free_short_name;
1024 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1026 metadata_resource_len - offset - calculated_size);
1028 goto out_free_short_name;
1031 dentry->file_name = file_name;
1032 dentry->file_name_utf8 = file_name_utf8;
1033 dentry->short_name = short_name;
1034 dentry->file_name_len = file_name_len;
1035 dentry->file_name_utf8_len = file_name_utf8_len;
1036 dentry->short_name_len = short_name_len;
1038 out_free_short_name:
1040 out_free_file_name_utf8:
1041 FREE(file_name_utf8);
1048 * Writes a dentry to an output buffer.
1050 * @dentry: The dentry structure.
1051 * @p: The memory location to write the data to.
1052 * @return: Pointer to the byte after the last byte we wrote as part of the
1055 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1059 memset(p, 0, dentry->length);
1060 p = put_u64(p, dentry->length);
1061 p = put_u32(p, dentry->attributes);
1062 p = put_u32(p, dentry->security_id);
1063 p = put_u64(p, dentry->subdir_offset);
1064 p = put_u64(p, 0); /* unused1 */
1065 p = put_u64(p, 0); /* unused2 */
1066 p = put_u64(p, dentry->creation_time);
1067 p = put_u64(p, dentry->last_access_time);
1068 p = put_u64(p, dentry->last_write_time);
1069 memcpy(p, dentry->hash, WIM_HASH_SIZE);
1071 p = put_u32(p, dentry->reparse_tag);
1072 p = put_u64(p, dentry->hard_link);
1073 p = put_u16(p, dentry->num_ads); /*streams */
1074 p = put_u16(p, dentry->short_name_len);
1075 p = put_u16(p, dentry->file_name_len);
1076 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1077 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1078 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1080 wimlib_assert(p - orig_p <= dentry->length);
1081 if (p - orig_p < dentry->length)
1082 p = put_zeroes(p, dentry->length - (p - orig_p));
1084 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1086 for (u16 i = 0; i < dentry->num_ads; i++) {
1087 p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
1088 p = put_u64(p, 0); /* Unused */
1089 p = put_bytes(p, WIM_HASH_SIZE, dentry->ads_entries[i].hash);
1090 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
1091 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
1092 (u8*)dentry->ads_entries[i].stream_name);
1093 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1098 /* Recursive function that writes a dentry tree rooted at @tree, not including
1099 * @tree itself, which has already been written, except in the case of the root
1100 * dentry, which is written right away, along with an end-of-directory entry. */
1101 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
1103 const struct dentry *child;
1105 if (dentry_is_root(tree)) {
1106 p = write_dentry(tree, p);
1108 /* write end of directory entry */
1111 /* Nothing to do for a regular file. */
1112 if (dentry_is_regular_file(tree))
1116 /* Write child dentries and end-of-directory entry. */
1117 child = tree->children;
1120 p = write_dentry(child, p);
1121 child = child->next;
1122 } while (child != tree->children);
1125 /* write end of directory entry */
1128 /* Recurse on children. */
1131 p = write_dentry_tree(child, p);
1132 child = child->next;
1133 } while (child != tree->children);
1138 /* Reads the children of a dentry, and all their children, ..., etc. from the
1139 * metadata resource and into the dentry tree.
1141 * @metadata_resource: An array that contains the uncompressed metadata
1142 * resource for the WIM file.
1143 * @metadata_resource_len: The length of @metadata_resource.
1144 * @dentry: A pointer to a struct dentry that is the root of the directory
1145 * tree and has already been read from the metadata resource. It
1146 * does not need to be the real root because this procedure is
1147 * called recursively.
1149 * @return: Zero on success, nonzero on failure.
1151 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1152 struct dentry *dentry)
1154 u64 cur_offset = dentry->subdir_offset;
1155 struct dentry *prev_child = NULL;
1156 struct dentry *first_child = NULL;
1157 struct dentry *child;
1158 struct dentry cur_child;
1161 /* If @dentry is a regular file, nothing more needs to be done for this
1163 if (cur_offset == 0)
1166 /* Find and read all the children of @dentry. */
1169 /* Read next child of @dentry into @cur_child. */
1170 ret = read_dentry(metadata_resource, metadata_resource_len,
1171 cur_offset, &cur_child);
1175 /* Check for end of directory. */
1176 if (cur_child.length == 0) {
1181 /* Not end of directory. Allocate this child permanently and
1182 * link it to the parent and previous child. */
1183 child = MALLOC(sizeof(struct dentry));
1185 ERROR("Failed to allocate %zu bytes for new dentry",
1186 sizeof(struct dentry));
1187 ret = WIMLIB_ERR_NOMEM;
1190 memcpy(child, &cur_child, sizeof(struct dentry));
1193 prev_child->next = child;
1194 child->prev = prev_child;
1196 first_child = child;
1199 child->parent = dentry;
1202 /* If there are children of this child, call this procedure
1204 if (child->subdir_offset != 0) {
1205 ret = read_dentry_tree(metadata_resource,
1206 metadata_resource_len, child);
1211 /* Advance to the offset of the next child. */
1212 cur_offset += dentry_total_length(child);
1215 /* Link last child to first one, and set parent's
1216 * children pointer to the first child. */
1218 prev_child->next = first_child;
1219 first_child->prev = prev_child;
1221 dentry->children = first_child;
1225 int dentry_set_symlink_buf(struct dentry *dentry, const u8 symlink_buf_hash[])
1227 struct ads_entry *ads_entries;
1229 ads_entries = CALLOC(2, sizeof(struct ads_entry));
1231 return WIMLIB_ERR_NOMEM;
1232 memcpy(ads_entries[1].hash, symlink_buf_hash, WIM_HASH_SIZE);
1233 dentry_free_ads_entries(dentry);
1234 dentry->num_ads = 2;
1235 dentry->ads_entries = ads_entries;