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
11 * Copyright (C) 2010 Carl Thijssen
12 * Copyright (C) 2012 Eric Biggers
14 * wimlib - Library for working with WIM files
16 * This library is free software; you can redistribute it and/or modify it under
17 * the terms of the GNU Lesser General Public License as published by the Free
18 * Software Foundation; either version 2.1 of the License, or (at your option) any
21 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
22 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
23 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public License along
26 * with this library; if not, write to the Free Software Foundation, Inc., 59
27 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wimlib_internal.h"
33 #include "timestamp.h"
34 #include "lookup_table.h"
39 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
40 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
42 if (S_ISDIR(stbuf->st_mode))
43 dentry->attributes = WIM_FILE_ATTRIBUTE_DIRECTORY;
45 dentry->attributes = WIM_FILE_ATTRIBUTE_NORMAL;
48 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
49 void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
50 const struct lookup_table *table)
52 struct lookup_table_entry *lte;
54 if (dentry_is_directory(dentry))
55 stbuf->st_mode = S_IFDIR | 0755;
57 stbuf->st_mode = S_IFREG | 0644;
60 lte = lookup_resource(table, dentry->hash);
65 stbuf->st_nlink = lte->refcnt;
66 stbuf->st_size = lte->resource_entry.original_size;
71 stbuf->st_uid = getuid();
72 stbuf->st_gid = getgid();
73 stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
74 stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
75 stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
76 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
79 /* Makes all timestamp fields for the dentry be the current time. */
80 void dentry_update_all_timestamps(struct dentry *dentry)
82 u64 now = get_timestamp();
83 dentry->creation_time = now;
84 dentry->last_access_time = now;
85 dentry->last_write_time = now;
89 * Calls a function on all directory entries in a directory tree. It is called
90 * on a parent before its children.
92 int for_dentry_in_tree(struct dentry *root,
93 int (*visitor)(struct dentry*, void*), void *arg)
98 ret = visitor(root, arg);
103 child = root->children;
109 ret = for_dentry_in_tree(child, visitor, arg);
113 } while (child != root->children);
118 * Like for_dentry_in_tree(), but the visitor function is always called on a
119 * dentry's children before on itself.
121 int for_dentry_in_tree_depth(struct dentry *root,
122 int (*visitor)(struct dentry*, void*), void *arg)
125 struct dentry *child;
128 child = root->children;
132 ret = for_dentry_in_tree_depth(child, visitor, arg);
136 } while (child != root->children);
138 return visitor(root, arg);
142 * Calculate the full path of @dentry, based on its parent's full path and on
143 * its UTF-8 file name.
145 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
149 char *parent_full_path;
152 FREE(dentry->full_path_utf8);
154 if (dentry_is_root(dentry)) {
155 dentry->full_path_utf8 = MALLOC(2);
156 if (!dentry->full_path_utf8) {
157 ERROR("Out of memory!\n");
158 return WIMLIB_ERR_NOMEM;
161 dentry->full_path_utf8[0] = '/';
162 dentry->full_path_utf8[1] = '\0';
163 dentry->full_path_utf8_len = 1;
167 if (dentry_is_root(dentry->parent)) {
169 parent_full_path = "";
171 parent_len = dentry->parent->full_path_utf8_len;
172 parent_full_path = dentry->parent->full_path_utf8;
175 len = parent_len + 1 + dentry->file_name_utf8_len;
176 full_path = MALLOC(len + 1);
178 ERROR("Out of memory!\n");
179 return WIMLIB_ERR_NOMEM;
182 memcpy(full_path, parent_full_path, parent_len);
183 full_path[parent_len] = '/';
184 memcpy(full_path + parent_len + 1, dentry->file_name_utf8,
185 dentry->file_name_utf8_len);
186 full_path[len] = '\0';
187 dentry->full_path_utf8 = full_path;
188 dentry->full_path_utf8_len = len;
193 * Recursively calculates the subdir offsets for a directory tree.
195 * @dentry: The root of the directory tree.
196 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
197 * offset for @dentry.
199 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
201 struct dentry *child;
203 child = dentry->children;
204 dentry->subdir_offset = *subdir_offset_p;
207 /* Advance the subdir offset by the amount of space the children
208 * of this dentry take up. */
210 *subdir_offset_p += child->length;
212 } while (child != dentry->children);
214 /* End-of-directory dentry on disk. */
215 *subdir_offset_p += 8;
217 /* Recursively call calculate_subdir_offsets() on all the
220 calculate_subdir_offsets(child, subdir_offset_p);
222 } while (child != dentry->children);
224 /* On disk, childless directories have a valid subdir_offset
225 * that points to an 8-byte end-of-directory dentry. Regular
226 * files have a subdir_offset of 0. */
227 if (dentry_is_directory(dentry))
228 *subdir_offset_p += 8;
230 dentry->subdir_offset = 0;
235 /* Returns the child of @dentry that has the file name @name.
236 * Returns NULL if no child has the name. */
237 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
240 struct dentry *child;
243 child = dentry->children;
245 name_len = strlen(name);
247 if (dentry_has_name(child, name, name_len))
250 } while (child != dentry->children);
255 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
256 * @cur_dir. Returns NULL if no dentry having the path is found. */
257 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
259 struct dentry *child;
261 const char *new_path;
266 child = cur_dir->children;
268 new_path = path_next_part(path, &base_len);
270 if (dentry_has_name(child, path, base_len))
271 return get_dentry_relative_path(child, new_path);
273 } while (child != cur_dir->children);
278 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
280 struct dentry *get_dentry(WIMStruct *w, const char *path)
282 struct dentry *root = wim_root_dentry(w);
285 return get_dentry_relative_path(root, path);
288 /* Returns the parent directory for the @path. */
289 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
291 size_t path_len = strlen(path);
292 char buf[path_len + 1];
294 memcpy(buf, path, path_len + 1);
296 to_parent_name(buf, path_len);
298 return get_dentry(w, buf);
301 /* Prints the full path of a dentry. */
302 int print_dentry_full_path(struct dentry *dentry, void *ignore)
304 if (dentry->full_path_utf8)
305 puts(dentry->full_path_utf8);
309 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
310 * NULL if the resource entry for the dentry is not to be printed. */
311 int print_dentry(struct dentry *dentry, void *lookup_table)
313 struct lookup_table_entry *lte;
314 printf("[DENTRY]\n");
315 printf("Length = %"PRIu64"\n", dentry->length);
316 printf("Attributes = 0x%x\n", dentry->attributes);
317 #ifdef ENABLE_SECURITY_DATA
318 printf("Security ID = %d\n", dentry->security_id);
320 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
321 /*printf("Unused1 = %"PRIu64"\n", dentry->unused1);*/
322 /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
323 printf("Creation Time = %"PRIu64"\n", dentry->creation_time);
324 printf("Last Access Time = %"PRIu64"\n", dentry->last_access_time);
325 printf("Last Write Time = %"PRIu64"\n", dentry->last_write_time);
326 printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
328 print_hash(dentry->hash);
330 /*printf("Reparse Tag = %u\n", dentry->reparse_tag);*/
331 printf("Hard Link Group = %"PRIu64"\n", dentry->hard_link);
332 /*printf("Number of Streams = %hu\n", dentry->streams);*/
333 printf("Filename = \"");
334 print_string(dentry->file_name, dentry->file_name_len);
336 printf("Filename Length = %hu\n", dentry->file_name_len);
337 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
338 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
339 printf("Short Name = \"");
340 print_string(dentry->short_name, dentry->short_name_len);
342 printf("Short Name Length = %hu\n", dentry->short_name_len);
343 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
345 lte = lookup_resource(lookup_table, dentry->hash);
347 print_lookup_table_entry(lte, NULL);
356 static inline void dentry_common_init(struct dentry *dentry)
358 memset(dentry, 0, sizeof(struct dentry));
360 #ifdef ENABLE_SECURITY_DATA
361 dentry->security_id = -1;
366 * Creates an unlinked directory entry.
368 * @name: The base name of the new dentry.
369 * @return: A pointer to the new dentry, or NULL if out of memory.
371 struct dentry *new_dentry(const char *name)
373 struct dentry *dentry;
375 dentry = MALLOC(sizeof(struct dentry));
379 dentry_common_init(dentry);
380 if (change_dentry_name(dentry, name) != 0) {
385 dentry_update_all_timestamps(dentry);
386 dentry->next = dentry;
387 dentry->prev = dentry;
388 dentry->parent = dentry;
393 void free_dentry(struct dentry *dentry)
395 FREE(dentry->file_name);
396 FREE(dentry->file_name_utf8);
397 FREE(dentry->short_name);
398 FREE(dentry->full_path_utf8);
402 /* Arguments for do_free_dentry(). */
403 struct free_dentry_args {
404 struct lookup_table *lookup_table;
405 bool decrement_refcnt;
409 * This function is passed as an argument to for_dentry_in_tree_depth() in order
410 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
412 static int do_free_dentry(struct dentry *dentry, void *__args)
414 struct free_dentry_args *args = (struct free_dentry_args*)__args;
416 if (args->decrement_refcnt && !dentry_is_directory(dentry)) {
417 lookup_table_decrement_refcnt(args->lookup_table,
421 wimlib_assert(dentry->refcnt >= 1);
422 if (--dentry->refcnt == 0)
428 * Unlinks and frees a dentry tree.
430 * @root: The root of the tree.
431 * @lookup_table: The lookup table for dentries.
432 * @decrement_refcnt: True if the dentries in the tree are to have their
433 * reference counts in the lookup table decremented.
435 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table,
436 bool decrement_refcnt)
438 if (!root || !root->parent)
441 struct free_dentry_args args;
442 args.lookup_table = lookup_table;
443 args.decrement_refcnt = decrement_refcnt;
444 for_dentry_in_tree_depth(root, do_free_dentry, &args);
447 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
454 * Links a dentry into the directory tree.
456 * @dentry: The dentry to link.
457 * @parent: The dentry that will be the parent of @dentry.
459 void link_dentry(struct dentry *dentry, struct dentry *parent)
461 dentry->parent = parent;
462 if (parent->children) {
463 /* Not an only child; link to siblings. */
464 dentry->next = parent->children;
465 dentry->prev = parent->children->prev;
466 dentry->next->prev = dentry;
467 dentry->prev->next = dentry;
469 /* Only child; link to parent. */
470 parent->children = dentry;
471 dentry->next = dentry;
472 dentry->prev = dentry;
476 /* Unlink a dentry from the directory tree. */
477 void unlink_dentry(struct dentry *dentry)
479 if (dentry_is_root(dentry))
481 if (dentry_is_only_child(dentry)) {
482 dentry->parent->children = NULL;
484 if (dentry_is_first_sibling(dentry))
485 dentry->parent->children = dentry->next;
486 dentry->next->prev = dentry->prev;
487 dentry->prev->next = dentry->next;
492 /* Recalculates the length of @dentry based on its file name length and short
494 static inline void recalculate_dentry_size(struct dentry *dentry)
496 dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
497 2 + dentry->short_name_len;
498 /* Must be multiple of 8. */
499 dentry->length += (8 - dentry->length % 8) % 8;
502 /* Changes the name of a dentry to @new_name. Only changes the file_name and
503 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
504 * full_path_utf8 fields. Also recalculates its length. */
505 int change_dentry_name(struct dentry *dentry, const char *new_name)
510 FREE(dentry->file_name);
512 utf8_len = strlen(new_name);
514 dentry->file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
516 if (!dentry->file_name)
517 return WIMLIB_ERR_NOMEM;
519 FREE(dentry->file_name_utf8);
520 dentry->file_name_utf8 = MALLOC(utf8_len + 1);
521 if (!dentry->file_name_utf8) {
522 FREE(dentry->file_name);
523 dentry->file_name = NULL;
524 return WIMLIB_ERR_NOMEM;
527 dentry->file_name_len = utf16_len;
528 dentry->file_name_utf8_len = utf8_len;
529 memcpy(dentry->file_name_utf8, new_name, utf8_len + 1);
530 recalculate_dentry_size(dentry);
534 /* Parameters for calculate_dentry_statistics(). */
535 struct image_statistics {
536 struct lookup_table *lookup_table;
540 u64 *hard_link_bytes;
543 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
545 struct image_statistics *stats;
546 struct lookup_table_entry *lte;
549 lte = lookup_resource(stats->lookup_table, dentry->hash);
551 if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
554 ++*stats->file_count;
557 u64 size = lte->resource_entry.original_size;
558 *stats->total_bytes += size;
559 if (++lte->out_refcnt == 1)
560 *stats->hard_link_bytes += size;
565 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table,
566 u64 *dir_count_ret, u64 *file_count_ret,
567 u64 *total_bytes_ret,
568 u64 *hard_link_bytes_ret)
570 struct image_statistics stats;
573 *total_bytes_ret = 0;
574 *hard_link_bytes_ret = 0;
575 stats.lookup_table = table;
576 stats.dir_count = dir_count_ret;
577 stats.file_count = file_count_ret;
578 stats.total_bytes = total_bytes_ret;
579 stats.hard_link_bytes = hard_link_bytes_ret;
580 for_lookup_table_entry(table, zero_out_refcnts, NULL);
581 for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
585 * Reads a directory entry from the metadata resource.
587 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
588 u64 offset, struct dentry *dentry)
593 char *file_name_utf8;
597 size_t file_name_utf8_len;
599 dentry_common_init(dentry);
601 /*Make sure the dentry really fits into the metadata resource.*/
602 if (offset + 8 > metadata_resource_len) {
603 ERROR("Directory entry starting at %"PRIu64" ends past the "
604 "end of the metadata resource (size %"PRIu64")!\n",
605 offset, metadata_resource_len);
606 return WIMLIB_ERR_INVALID_DENTRY;
609 /* Before reading the whole entry, we need to read just the length.
610 * This is because an entry of length 8 (that is, just the length field)
611 * terminates the list of sibling directory entries. */
613 p = get_u64(&metadata_resource[offset], &dentry->length);
615 /* A zero length field (really a length of 8, since that's how big the
616 * directory entry is...) indicates that this is the end of directory
617 * dentry. We do not read it into memory as an actual dentry, so just
618 * return true in that case. */
619 if (dentry->length == 0)
622 if (offset + dentry->length >= metadata_resource_len) {
623 ERROR("Directory entry at offset %"PRIu64" and with size "
624 "%"PRIu64" ends past the end of the metadata resource "
625 "(size %"PRIu64")!\n", offset, dentry->length,
626 metadata_resource_len);
627 return WIMLIB_ERR_INVALID_DENTRY;
630 /* If it is a recognized length, read the rest of the directory entry.
631 * Note: The root directory entry has no name, and its length does not
632 * include the short name length field. */
633 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
634 ERROR("Directory entry has invalid length of "
635 "%"PRIu64" bytes\n", dentry->length);
636 return WIMLIB_ERR_INVALID_DENTRY;
639 p = get_u32(p, &dentry->attributes);
640 #ifdef ENABLE_SECURITY_DATA
641 p = get_u32(p, &dentry->security_id);
645 p = get_u64(p, &dentry->subdir_offset);
647 /* 2 unused fields */
648 p += 2 * sizeof(u64);
650 p = get_u64(p, &dentry->creation_time);
651 p = get_u64(p, &dentry->last_access_time);
652 p = get_u64(p, &dentry->last_write_time);
654 p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
656 /* Currently ignoring reparse_tag. */
659 /* The reparse_reserved field does not actually exist. */
661 p = get_u64(p, &dentry->hard_link);
663 /* Currently ignoring streams. */
666 p = get_u16(p, &short_name_len);
667 p = get_u16(p, &file_name_len);
669 calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
672 if (dentry->length < calculated_size) {
673 ERROR("Unexpected end of directory entry! (Expected "
674 "%"PRIu64" bytes, got %"PRIu64" bytes. "
675 "short_name_len = %hu, file_name_len = %hu)\n",
676 calculated_size, dentry->length,
677 short_name_len, file_name_len);
678 return WIMLIB_ERR_INVALID_DENTRY;
681 /* Read the filename. */
682 file_name = MALLOC(file_name_len);
684 ERROR("Failed to allocate %hu bytes for dentry file name!\n",
686 return WIMLIB_ERR_NOMEM;
688 p = get_bytes(p, file_name_len, file_name);
690 /* Convert filename to UTF-8. */
691 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
692 &file_name_utf8_len);
694 if (!file_name_utf8) {
695 ERROR("Failed to allocate memory to convert UTF16 "
696 "filename (%hu bytes) to UTF8\n",
701 /* Undocumented padding between file name and short name. This probably
702 * is supposed to be a terminating NULL character. */
705 /* Read the short filename. */
706 short_name = MALLOC(short_name_len);
708 ERROR("Failed to allocate %hu bytes for short filename\n",
713 get_bytes(p, short_name_len, short_name);
715 dentry->file_name = file_name;
716 dentry->file_name_utf8 = file_name_utf8;
717 dentry->short_name = short_name;
718 dentry->file_name_len = file_name_len;
719 dentry->file_name_utf8_len = file_name_utf8_len;
720 dentry->short_name_len = short_name_len;
723 FREE(dentry->file_name_utf8);
725 FREE(dentry->file_name);
726 return WIMLIB_ERR_NOMEM;
730 * Writes a dentry to an output buffer.
732 * @dentry: The dentry structure.
733 * @p: The memory location to write the data to.
734 * @return: True on success, false on failure.
736 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
739 memset(p, 0, dentry->length);
740 p = put_u64(p, dentry->length);
741 p = put_u32(p, dentry->attributes);
742 #ifdef ENABLE_SECURITY_DATA
743 p = put_u32(p, dentry->security_id);
745 p = put_u32(p, (u32)(-1));
747 p = put_u64(p, dentry->subdir_offset);
748 p = put_u64(p, 0); /* unused1 */
749 p = put_u64(p, 0); /* unused2 */
750 p = put_u64(p, dentry->creation_time);
751 p = put_u64(p, dentry->last_access_time);
752 p = put_u64(p, dentry->last_write_time);
753 p = put_bytes(p, WIM_HASH_SIZE, dentry->hash);
754 p = put_u32(p, 0); /* reparse_tag */
755 p = put_u64(p, dentry->hard_link);
756 p = put_u16(p, 0); /*streams */
757 p = put_u16(p, dentry->short_name_len);
758 p = put_u16(p, dentry->file_name_len);
759 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
760 p = put_u16(p, 0); /* filename padding, 2 bytes. */
761 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
762 return orig_p + dentry->length;
765 /* Recursive function that writes a dentry tree rooted at @tree, not including
766 * @tree itself, which has already been written, except in the case of the root
767 * dentry, which is written right away, along with an end-of-directory entry. */
768 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
770 const struct dentry *child;
772 if (dentry_is_root(tree)) {
773 p = write_dentry(tree, p);
775 /* write end of directory entry */
778 /* Nothing to do for a regular file. */
779 if (dentry_is_regular_file(tree))
783 /* Write child dentries and end-of-directory entry. */
784 child = tree->children;
787 p = write_dentry(child, p);
789 } while (child != tree->children);
792 /* write end of directory entry */
795 /* Recurse on children. */
798 p = write_dentry_tree(child, p);
800 } while (child != tree->children);
805 /* Reads the children of a dentry, and all their children, ..., etc. from the
806 * metadata resource and into the dentry tree.
808 * @metadata_resource: An array that contains the uncompressed metadata
809 * resource for the WIM file.
810 * @metadata_resource_len: The length of @metadata_resource.
811 * @dentry: A pointer to a struct dentry that is the root of the directory tree
812 * and has already been read from the metadata resource. It does not
813 * need to be the real root, because this procedure is called
815 * @return: True on success, false on failure.
817 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
818 struct dentry *dentry)
820 u64 cur_offset = dentry->subdir_offset;
821 struct dentry *prev_child = NULL;
822 struct dentry *first_child = NULL;
823 struct dentry *child;
824 struct dentry cur_child;
827 /* If @dentry is a regular file, nothing more needs to be done for this
832 /* Find and read all the children of @dentry. */
835 /* Read next child of @dentry into @cur_child. */
836 ret = read_dentry(metadata_resource, metadata_resource_len,
837 cur_offset, &cur_child);
841 /* Check for end of directory. */
842 if (cur_child.length == 0) {
847 /* Not end of directory. Allocate this child permanently and
848 * link it to the parent and previous child. */
849 child = MALLOC(sizeof(struct dentry));
851 ERROR("Failed to allocate %zu bytes for new dentry!\n",
852 sizeof(struct dentry));
853 ret = WIMLIB_ERR_NOMEM;
856 memcpy(child, &cur_child, sizeof(struct dentry));
859 prev_child->next = child;
860 child->prev = prev_child;
865 child->parent = dentry;
868 /* If there are children of this child, call this procedure
870 if (child->subdir_offset != 0) {
871 ret = read_dentry_tree(metadata_resource,
872 metadata_resource_len, child);
877 /* Advance to the offset of the next child. */
878 cur_offset += child->length;
881 /* Link last child to first one, and set parent's
882 * children pointer to the first child. */
884 prev_child->next = first_child;
885 first_child->prev = prev_child;
887 dentry->children = first_child;