4 * In the WIM file format, the dentries are stored in the "metadata resource"
5 * section right after the security data. Each image in the WIM file has its
6 * own metadata resource with its own security data and dentry tree. Dentries
7 * in different images may share file resources by referring to the same lookup
12 * Copyright (C) 2012, 2013 Eric Biggers
14 * This file is part of wimlib, a library for working with WIM files.
16 * wimlib is free software; you can redistribute it and/or modify it under the
17 * terms of the GNU General Public License as published by the Free Software
18 * Foundation; either version 3 of the License, or (at your option) any later
21 * wimlib 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
23 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along with
26 * wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "buffer_io.h"
31 #include "lookup_table.h"
32 #include "timestamp.h"
33 #include "wimlib_internal.h"
36 #ifdef TCHAR_IS_UTF16LE
40 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
41 * a file name and short name that take the specified numbers of bytes. This
42 * excludes any alternate data stream entries that may follow the dentry. */
44 __dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
46 u64 length = WIM_DENTRY_DISK_SIZE;
48 length += file_name_nbytes + 2;
49 if (short_name_nbytes)
50 length += short_name_nbytes + 2;
54 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
55 * the file name length and short name length. Note that dentry->length is
56 * ignored; also, this excludes any alternate data stream entries that may
57 * follow the dentry. */
59 dentry_correct_length_unaligned(const struct wim_dentry *dentry)
61 return __dentry_correct_length_unaligned(dentry->file_name_nbytes,
62 dentry->short_name_nbytes);
65 /* Return the "correct" value to write in the length field of a WIM dentry,
66 * based on the file name length and short name length. */
68 dentry_correct_length(const struct wim_dentry *dentry)
70 return (dentry_correct_length_unaligned(dentry) + 7) & ~7;
73 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
74 * stream name @name that has length @name_nbytes bytes. */
76 ads_entry_has_name(const struct wim_ads_entry *entry,
77 const utf16lechar *name, size_t name_nbytes)
79 return entry->stream_name_nbytes == name_nbytes &&
80 memcmp(entry->stream_name, name, name_nbytes) == 0;
83 /* Duplicates a string of system-dependent encoding into a UTF-16LE string and
84 * returns the string and its length, in bytes, in the pointer arguments. Frees
85 * any existing string at the return location before overwriting it. */
87 get_utf16le_name(const tchar *name, utf16lechar **name_utf16le_ret,
88 u16 *name_utf16le_nbytes_ret)
90 utf16lechar *name_utf16le;
91 size_t name_utf16le_nbytes;
94 name_utf16le_nbytes = tstrlen(name) * 2;
95 name_utf16le = MALLOC(name_utf16le_nbytes + 2);
97 return WIMLIB_ERR_NOMEM;
98 memcpy(name_utf16le, name, name_utf16le_nbytes + 2);
102 ret = tstr_to_utf16le(name, strlen(name), &name_utf16le,
103 &name_utf16le_nbytes);
105 if (name_utf16le_nbytes > 0xffff) {
107 ERROR("Multibyte string \"%s\" is too long!", name);
108 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
113 FREE(*name_utf16le_ret);
114 *name_utf16le_ret = name_utf16le;
115 *name_utf16le_nbytes_ret = name_utf16le_nbytes;
120 /* Sets the name of a WIM dentry from a multibyte string. */
122 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name)
125 ret = get_utf16le_name(new_name, &dentry->file_name,
126 &dentry->file_name_nbytes);
128 /* Clear the short name and recalculate the dentry length */
129 if (dentry_has_short_name(dentry)) {
130 FREE(dentry->short_name);
131 dentry->short_name = NULL;
132 dentry->short_name_nbytes = 0;
134 dentry->length = dentry_correct_length(dentry);
139 /* Returns the total length of a WIM alternate data stream entry on-disk,
140 * including the stream name, the null terminator, AND the padding after the
141 * entry to align the next ADS entry or dentry on an 8-byte boundary. */
143 ads_entry_total_length(const struct wim_ads_entry *entry)
145 u64 len = WIM_ADS_ENTRY_DISK_SIZE;
146 if (entry->stream_name_nbytes)
147 len += entry->stream_name_nbytes + 2;
148 return (len + 7) & ~7;
153 __dentry_total_length(const struct wim_dentry *dentry, u64 length)
155 const struct wim_inode *inode = dentry->d_inode;
156 for (u16 i = 0; i < inode->i_num_ads; i++)
157 length += ads_entry_total_length(&inode->i_ads_entries[i]);
158 return (length + 7) & ~7;
161 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
162 * all alternate data streams. */
164 dentry_correct_total_length(const struct wim_dentry *dentry)
166 return __dentry_total_length(dentry,
167 dentry_correct_length_unaligned(dentry));
170 /* Like dentry_correct_total_length(), but use the existing dentry->length field
171 * instead of calculating its "correct" value. */
173 dentry_total_length(const struct wim_dentry *dentry)
175 return __dentry_total_length(dentry, dentry->length);
179 for_dentry_in_rbtree(struct rb_node *root,
180 int (*visitor)(struct wim_dentry *, void *),
184 struct rb_node *node = root;
188 list_add(&rbnode_dentry(node)->tmp_list, &stack);
189 node = node->rb_left;
191 struct list_head *next;
192 struct wim_dentry *dentry;
197 dentry = container_of(next, struct wim_dentry, tmp_list);
199 ret = visitor(dentry, arg);
202 node = dentry->rb_node.rb_right;
208 for_dentry_tree_in_rbtree_depth(struct rb_node *node,
209 int (*visitor)(struct wim_dentry*, void*),
214 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
218 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
222 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
230 for_dentry_tree_in_rbtree(struct rb_node *node,
231 int (*visitor)(struct wim_dentry*, void*),
236 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
239 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
242 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
249 /* Calls a function on all directory entries in a WIM dentry tree. Logically,
250 * this is a pre-order traversal (the function is called on a parent dentry
251 * before its children), but sibling dentries will be visited in order as well.
254 for_dentry_in_tree(struct wim_dentry *root,
255 int (*visitor)(struct wim_dentry*, void*), void *arg)
257 int ret = visitor(root, arg);
259 ret = for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node,
266 /* Like for_dentry_in_tree(), but the visitor function is always called on a
267 * dentry's children before on itself. */
269 for_dentry_in_tree_depth(struct wim_dentry *root,
270 int (*visitor)(struct wim_dentry*, void*), void *arg)
273 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
276 ret = visitor(root, arg);
280 /* Calculate the full path of @dentry. The full path of its parent must have
281 * already been calculated. */
283 calculate_dentry_full_path(struct wim_dentry *dentry, void *ignore)
286 u32 full_path_nbytes;
288 wimlib_assert(dentry_is_root(dentry) ||
289 dentry->parent->full_path != NULL);
291 if (dentry_is_root(dentry)) {
292 full_path = TMALLOC(2);
294 return WIMLIB_ERR_NOMEM;
295 full_path[0] = T('/');
296 full_path[1] = T('\0');
297 full_path_nbytes = 1 * sizeof(tchar);
299 tchar *parent_full_path;
300 u32 parent_full_path_nbytes;
301 const struct wim_dentry *parent;
302 size_t filename_nbytes;
305 parent = dentry->parent;
306 if (dentry_is_root(parent)) {
307 parent_full_path = T("");
308 parent_full_path_nbytes = 0;
310 parent_full_path = parent->full_path;
311 parent_full_path_nbytes = parent->full_path_nbytes;
314 filename_nbytes = dentry->file_name_nbytes;
316 ret = utf16le_to_mbs_nbytes(dentry->file_name,
317 dentry->file_name_nbytes,
323 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
324 dentry->file_name_nbytes;
325 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
327 return WIMLIB_ERR_NOMEM;
328 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
329 full_path[parent_full_path_nbytes] = T('/');
331 memcpy(&full_path[parent_full_path_nbytes + 1],
333 dentry->file_name_nbytes + sizeof(tchar));
335 utf16le_to_mbs_buf(dentry->file_name,
336 dentry->file_name_nbytes,
337 &full_path[parent_full_path_nbytes + 1]);
340 FREE(dentry->full_path);
341 dentry->full_path = full_path;
342 dentry->full_path_nbytes= full_path_nbytes;
347 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
349 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
354 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
356 calculate_subdir_offsets(dentry, subdir_offset_p);
361 * Recursively calculates the subdir offsets for a directory tree.
363 * @dentry: The root of the directory tree.
364 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
365 * offset for @dentry.
368 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
370 struct rb_node *node;
372 dentry->subdir_offset = *subdir_offset_p;
373 node = dentry->d_inode->i_children.rb_node;
375 /* Advance the subdir offset by the amount of space the children
376 * of this dentry take up. */
377 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
379 /* End-of-directory dentry on disk. */
380 *subdir_offset_p += 8;
382 /* Recursively call calculate_subdir_offsets() on all the
384 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
386 /* On disk, childless directories have a valid subdir_offset
387 * that points to an 8-byte end-of-directory dentry. Regular
388 * files or reparse points have a subdir_offset of 0. */
389 if (dentry_is_directory(dentry))
390 *subdir_offset_p += 8;
392 dentry->subdir_offset = 0;
397 compare_utf16le_names(const utf16lechar *name1, size_t nbytes1,
398 const utf16lechar *name2, size_t nbytes2)
400 int result = memcmp(name1, name2, min(nbytes1, nbytes2));
404 return (int)nbytes1 - (int)nbytes2;
408 dentry_compare_names(const struct wim_dentry *d1, const struct wim_dentry *d2)
410 return compare_utf16le_names(d1->file_name, d1->file_name_nbytes,
411 d2->file_name, d2->file_name_nbytes);
416 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
417 const utf16lechar *name,
420 struct rb_node *node = dentry->d_inode->i_children.rb_node;
421 struct wim_dentry *child;
423 child = rbnode_dentry(node);
424 int result = compare_utf16le_names(name, name_nbytes,
426 child->file_name_nbytes);
428 node = node->rb_left;
430 node = node->rb_right;
437 /* Returns the child of @dentry that has the file name @name. Returns NULL if
438 * no child has the name. */
440 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
443 return get_dentry_child_with_utf16le_name(dentry, name,
444 tstrlen(name) * sizeof(tchar));
446 utf16lechar *utf16le_name;
447 size_t utf16le_name_nbytes;
449 struct wim_dentry *child;
451 ret = tstr_to_utf16le(name, strlen(name),
452 &utf16le_name, &utf16le_name_nbytes);
456 child = get_dentry_child_with_utf16le_name(dentry,
458 utf16le_name_nbytes);
465 static struct wim_dentry *
466 get_dentry_utf16le(WIMStruct *w, const utf16lechar *path,
469 struct wim_dentry *cur_dentry, *parent_dentry;
470 const utf16lechar *p, *pp;
472 parent_dentry = wim_root_dentry(w);
475 while (*p == cpu_to_le16('/'))
477 cur_dentry = parent_dentry;
481 while (*pp != cpu_to_le16('/') && *pp != cpu_to_le16('\0'))
484 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
485 (void*)pp - (void*)p);
486 if (cur_dentry == NULL)
489 parent_dentry = cur_dentry;
491 if (cur_dentry == NULL) {
492 if (dentry_is_directory(parent_dentry))
500 /* Returns the dentry corresponding to the @path, or NULL if there is no such
503 get_dentry(WIMStruct *w, const tchar *path)
506 return get_dentry_utf16le(w, path, tstrlen(path) * sizeof(tchar));
508 utf16lechar *path_utf16le;
509 size_t path_utf16le_nbytes;
511 struct wim_dentry *dentry;
513 ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
514 &path_utf16le, &path_utf16le_nbytes);
517 dentry = get_dentry_utf16le(w, path_utf16le, path_utf16le_nbytes);
524 wim_pathname_to_inode(WIMStruct *w, const tchar *path)
526 struct wim_dentry *dentry;
527 dentry = get_dentry(w, path);
529 return dentry->d_inode;
534 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
535 * if the dentry is not found. */
537 get_parent_dentry(WIMStruct *w, const tchar *path)
539 size_t path_len = tstrlen(path);
540 tchar buf[path_len + 1];
542 tmemcpy(buf, path, path_len + 1);
543 to_parent_name(buf, path_len);
544 return get_dentry(w, buf);
547 /* Prints the full path of a dentry. */
549 print_dentry_full_path(struct wim_dentry *dentry, void *ignore)
551 if (dentry->full_path)
552 printf("%"TS"\n", dentry->full_path);
556 /* We want to be able to show the names of the file attribute flags that are
558 struct file_attr_flag {
562 struct file_attr_flag file_attr_flags[] = {
563 {FILE_ATTRIBUTE_READONLY, T("READONLY")},
564 {FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
565 {FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
566 {FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
567 {FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
568 {FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
569 {FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
570 {FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
571 {FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
572 {FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
573 {FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
574 {FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
575 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
576 {FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
577 {FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
580 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
581 * available. If the dentry is unresolved and the lookup table is NULL, the
582 * lookup table entries will not be printed. Otherwise, they will be. */
584 print_dentry(struct wim_dentry *dentry, void *lookup_table)
587 struct wim_lookup_table_entry *lte;
588 const struct wim_inode *inode = dentry->d_inode;
591 tprintf(T("[DENTRY]\n"));
592 tprintf(T("Length = %"PRIu64"\n"), dentry->length);
593 tprintf(T("Attributes = 0x%x\n"), inode->i_attributes);
594 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
595 if (file_attr_flags[i].flag & inode->i_attributes)
596 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
597 file_attr_flags[i].name);
598 tprintf(T("Security ID = %d\n"), inode->i_security_id);
599 tprintf(T("Subdir offset = %"PRIu64"\n"), dentry->subdir_offset);
601 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
602 tprintf(T("Creation Time = %"TS"\n"), buf);
604 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
605 tprintf(T("Last Access Time = %"TS"\n"), buf);
607 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
608 tprintf(T("Last Write Time = %"TS"\n"), buf);
610 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), inode->i_reparse_tag);
611 tprintf(T("Hard Link Group = 0x%"PRIx64"\n"), inode->i_ino);
612 tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
613 tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
614 if (dentry_has_long_name(dentry))
615 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
616 if (dentry_has_short_name(dentry))
617 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
618 if (dentry->full_path)
619 tprintf(T("Full Path = \"%"WS"\"\n"), dentry->full_path);
621 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
623 print_lookup_table_entry(lte, stdout);
625 hash = inode_stream_hash(inode, 0);
627 tprintf(T("Hash = 0x"));
633 for (u16 i = 0; i < inode->i_num_ads; i++) {
634 tprintf(T("[Alternate Stream Entry %u]\n"), i);
635 wimlib_printf(T("Name = \"%"WS"\"\n"),
636 inode->i_ads_entries[i].stream_name);
637 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
638 inode->i_ads_entries[i].stream_name_nbytes);
639 hash = inode_stream_hash(inode, i + 1);
641 tprintf(T("Hash = 0x"));
645 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
651 /* Initializations done on every `struct wim_dentry'. */
653 dentry_common_init(struct wim_dentry *dentry)
655 memset(dentry, 0, sizeof(struct wim_dentry));
659 static struct wim_inode *
662 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
664 inode->i_security_id = -1;
667 inode->i_next_stream_id = 1;
668 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
669 ERROR_WITH_ERRNO("Error initializing mutex");
674 INIT_LIST_HEAD(&inode->i_dentry);
679 static struct wim_inode *
682 struct wim_inode *inode = new_timeless_inode();
684 u64 now = get_wim_timestamp();
685 inode->i_creation_time = now;
686 inode->i_last_access_time = now;
687 inode->i_last_write_time = now;
692 /* Creates an unlinked directory entry. */
693 int new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
695 struct wim_dentry *dentry;
698 dentry = MALLOC(sizeof(struct wim_dentry));
700 return WIMLIB_ERR_NOMEM;
702 dentry_common_init(dentry);
703 ret = set_dentry_name(dentry, name);
705 dentry->parent = dentry;
706 *dentry_ret = dentry;
709 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
717 __new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
720 struct wim_dentry *dentry;
723 ret = new_dentry(name, &dentry);
728 dentry->d_inode = new_timeless_inode();
730 dentry->d_inode = new_inode();
731 if (!dentry->d_inode) {
733 return WIMLIB_ERR_NOMEM;
736 inode_add_dentry(dentry, dentry->d_inode);
737 *dentry_ret = dentry;
742 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
744 return __new_dentry_with_inode(name, dentry_ret, true);
748 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
750 return __new_dentry_with_inode(name, dentry_ret, false);
755 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
756 size_t name_nbytes, bool is_utf16le)
759 memset(ads_entry, 0, sizeof(*ads_entry));
762 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
764 return WIMLIB_ERR_NOMEM;
765 memcpy(p, name, name_nbytes);
766 p[name_nbytes / 2] = 0;
767 ads_entry->stream_name = p;
768 ads_entry->stream_name_nbytes = name_nbytes;
770 if (name && *(const tchar*)name == T('\0')) {
771 ret = get_utf16le_name(name, &ads_entry->stream_name,
772 &ads_entry->stream_name_nbytes);
779 destroy_ads_entry(struct wim_ads_entry *ads_entry)
781 FREE(ads_entry->stream_name);
784 /* Frees an inode. */
785 void free_inode(struct wim_inode *inode)
788 if (inode->i_ads_entries) {
789 for (u16 i = 0; i < inode->i_num_ads; i++)
790 destroy_ads_entry(&inode->i_ads_entries[i]);
791 FREE(inode->i_ads_entries);
794 wimlib_assert(inode->i_num_opened_fds == 0);
796 pthread_mutex_destroy(&inode->i_mutex);
797 if (inode->i_hlist.pprev)
798 hlist_del(&inode->i_hlist);
800 FREE(inode->i_extracted_file);
805 /* Decrements link count on an inode and frees it if the link count reaches 0.
807 static void put_inode(struct wim_inode *inode)
809 wimlib_assert(inode->i_nlink != 0);
810 if (--inode->i_nlink == 0) {
812 if (inode->i_num_opened_fds == 0)
820 /* Frees a WIM dentry.
822 * The corresponding inode (if any) is freed only if its link count is
825 void free_dentry(struct wim_dentry *dentry)
827 FREE(dentry->file_name);
828 FREE(dentry->short_name);
829 FREE(dentry->full_path);
831 put_inode(dentry->d_inode);
835 void put_dentry(struct wim_dentry *dentry)
837 wimlib_assert(dentry->refcnt != 0);
838 if (--dentry->refcnt == 0)
842 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
843 * to free a directory tree. */
844 static int do_free_dentry(struct wim_dentry *dentry, void *__lookup_table)
846 struct wim_lookup_table *lookup_table = __lookup_table;
850 struct wim_lookup_table_entry *lte;
851 struct wim_inode *inode = dentry->d_inode;
852 wimlib_assert(inode->i_nlink != 0);
853 for (i = 0; i <= inode->i_num_ads; i++) {
854 lte = inode_stream_lte(inode, i, lookup_table);
856 lte_decrement_refcnt(lte, lookup_table);
865 * Unlinks and frees a dentry tree.
867 * @root: The root of the tree.
868 * @lookup_table: The lookup table for dentries. If non-NULL, the
869 * reference counts in the lookup table for the lookup
870 * table entries corresponding to the dentries will be
873 void free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
876 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
879 int increment_dentry_refcnt(struct wim_dentry *dentry, void *ignore)
886 * Links a dentry into the directory tree.
888 * @parent: The dentry that will be the parent of @dentry.
889 * @dentry: The dentry to link.
892 dentry_add_child(struct wim_dentry * restrict parent,
893 struct wim_dentry * restrict child)
895 wimlib_assert(dentry_is_directory(parent));
897 struct rb_root *root = &parent->d_inode->i_children;
898 struct rb_node **new = &(root->rb_node);
899 struct rb_node *rb_parent = NULL;
902 struct wim_dentry *this = rbnode_dentry(*new);
903 int result = dentry_compare_names(child, this);
908 new = &((*new)->rb_left);
910 new = &((*new)->rb_right);
914 child->parent = parent;
915 rb_link_node(&child->rb_node, rb_parent, new);
916 rb_insert_color(&child->rb_node, root);
920 /* Unlink a WIM dentry from the directory entry tree. */
922 unlink_dentry(struct wim_dentry *dentry)
924 struct wim_dentry *parent = dentry->parent;
925 if (parent == dentry)
927 rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
931 * Returns the alternate data stream entry belonging to @inode that has the
932 * stream name @stream_name.
934 struct wim_ads_entry *
935 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
938 if (inode->i_num_ads == 0) {
942 size_t stream_name_utf16le_nbytes;
944 struct wim_ads_entry *result;
947 const utf16lechar *stream_name_utf16le;
949 stream_name_utf16le = stream_name;
950 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
952 utf16lechar *stream_name_utf16le;
954 ret = tstr_to_utf16le(stream_name, tstrlen(stream_name) * sizeof(tchar),
955 &stream_name_utf16le,
956 &stream_name_utf16le_nbytes);
963 if (ads_entry_has_name(&inode->i_ads_entries[i],
965 stream_name_utf16le_nbytes))
969 result = &inode->i_ads_entries[i];
972 } while (++i != inode->i_num_ads);
973 #if !TCHAR_IS_UTF16LE
974 FREE(stream_name_utf16le);
980 static struct wim_ads_entry *
981 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
982 size_t stream_name_nbytes, bool is_utf16le)
985 struct wim_ads_entry *ads_entries;
986 struct wim_ads_entry *new_entry;
988 if (inode->i_num_ads >= 0xfffe) {
989 ERROR("Too many alternate data streams in one inode!");
992 num_ads = inode->i_num_ads + 1;
993 ads_entries = REALLOC(inode->i_ads_entries,
994 num_ads * sizeof(inode->i_ads_entries[0]));
996 ERROR("Failed to allocate memory for new alternate data stream");
999 inode->i_ads_entries = ads_entries;
1001 new_entry = &inode->i_ads_entries[num_ads - 1];
1002 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1005 new_entry->stream_id = inode->i_next_stream_id++;
1007 inode->i_num_ads = num_ads;
1011 struct wim_ads_entry *
1012 inode_add_ads_utf16le(struct wim_inode *inode,
1013 const utf16lechar *stream_name,
1014 size_t stream_name_nbytes)
1016 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1017 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1021 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1022 * NULL if memory could not be allocated.
1024 struct wim_ads_entry *
1025 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1027 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1028 return do_inode_add_ads(inode, stream_name,
1029 tstrlen(stream_name) * sizeof(tchar),
1034 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1035 const void *value, size_t size,
1036 struct wim_lookup_table *lookup_table)
1038 int ret = WIMLIB_ERR_NOMEM;
1039 struct wim_ads_entry *new_ads_entry;
1040 struct wim_lookup_table_entry *existing_lte;
1041 struct wim_lookup_table_entry *lte;
1042 u8 value_hash[SHA1_HASH_SIZE];
1044 wimlib_assert(inode->i_resolved);
1045 new_ads_entry = inode_add_ads(inode, name);
1048 sha1_buffer((const u8*)value, size, value_hash);
1049 existing_lte = __lookup_resource(lookup_table, value_hash);
1055 lte = new_lookup_table_entry();
1057 goto out_remove_ads_entry;
1058 value_copy = MALLOC(size);
1061 goto out_remove_ads_entry;
1063 memcpy(value_copy, value, size);
1064 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1065 lte->attached_buffer = value_copy;
1066 lte->resource_entry.original_size = size;
1067 lte->resource_entry.size = size;
1068 copy_hash(lte->hash, value_hash);
1069 lookup_table_insert(lookup_table, lte);
1071 new_ads_entry->lte = lte;
1074 out_remove_ads_entry:
1075 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1081 /* Remove an alternate data stream from a WIM inode */
1083 inode_remove_ads(struct wim_inode *inode, u16 idx,
1084 struct wim_lookup_table *lookup_table)
1086 struct wim_ads_entry *ads_entry;
1087 struct wim_lookup_table_entry *lte;
1089 wimlib_assert(idx < inode->i_num_ads);
1090 wimlib_assert(inode->i_resolved);
1092 ads_entry = &inode->i_ads_entries[idx];
1094 DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1096 lte = ads_entry->lte;
1098 lte_decrement_refcnt(lte, lookup_table);
1100 destroy_ads_entry(ads_entry);
1102 memmove(&inode->i_ads_entries[idx],
1103 &inode->i_ads_entries[idx + 1],
1104 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1110 inode_get_unix_data(const struct wim_inode *inode,
1111 struct wimlib_unix_data *unix_data,
1112 u16 *stream_idx_ret)
1114 const struct wim_ads_entry *ads_entry;
1115 const struct wim_lookup_table_entry *lte;
1119 wimlib_assert(inode->i_resolved);
1121 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1122 WIMLIB_UNIX_DATA_TAG, NULL);
1124 return NO_UNIX_DATA;
1127 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1129 lte = ads_entry->lte;
1131 return NO_UNIX_DATA;
1133 size = wim_resource_size(lte);
1134 if (size != sizeof(struct wimlib_unix_data))
1135 return BAD_UNIX_DATA;
1137 ret = read_full_wim_resource(lte, unix_data, 0);
1141 if (unix_data->version != 0)
1142 return BAD_UNIX_DATA;
1147 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1148 struct wim_lookup_table *lookup_table, int which)
1150 struct wimlib_unix_data unix_data;
1152 bool have_good_unix_data = false;
1153 bool have_unix_data = false;
1156 if (!(which & UNIX_DATA_CREATE)) {
1157 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1158 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1159 have_unix_data = true;
1161 have_good_unix_data = true;
1163 unix_data.version = 0;
1164 if (which & UNIX_DATA_UID || !have_good_unix_data)
1165 unix_data.uid = uid;
1166 if (which & UNIX_DATA_GID || !have_good_unix_data)
1167 unix_data.gid = gid;
1168 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1169 unix_data.mode = mode;
1170 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1172 sizeof(struct wimlib_unix_data),
1174 if (ret == 0 && have_unix_data)
1175 inode_remove_ads(inode, stream_idx, lookup_table);
1178 #endif /* !__WIN32__ */
1181 * Reads the alternate data stream entries of a WIM dentry.
1183 * @p: Pointer to buffer that starts with the first alternate stream entry.
1185 * @inode: Inode to load the alternate data streams into.
1186 * @inode->i_num_ads must have been set to the number of
1187 * alternate data streams that are expected.
1189 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1192 * The format of the on-disk alternate stream entries is as follows:
1194 * struct wim_ads_entry_on_disk {
1195 * u64 length; // Length of the entry, in bytes. This includes
1196 * all fields (including the stream name and
1197 * null terminator if present, AND the padding!).
1198 * u64 reserved; // Seems to be unused
1199 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
1200 * u16 stream_name_len; // Length of the stream name, in bytes
1201 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
1202 * not including null terminator
1203 * u16 zero; // UTF-16 null terminator for the stream name, NOT
1204 * included in @stream_name_len. Based on what
1205 * I've observed from filenames in dentries,
1206 * this field should not exist when
1207 * (@stream_name_len == 0), but you can't
1208 * actually tell because of the padding anyway
1209 * (provided that the padding is zeroed, which
1210 * it always seems to be).
1211 * char padding[]; // Padding to make the size a multiple of 8 bytes.
1214 * In addition, the entries are 8-byte aligned.
1216 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1217 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1218 * failure, @inode is not modified.
1221 read_ads_entries(const u8 *p, struct wim_inode *inode, u64 remaining_size)
1224 struct wim_ads_entry *ads_entries;
1227 num_ads = inode->i_num_ads;
1228 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1230 ERROR("Could not allocate memory for %"PRIu16" "
1231 "alternate data stream entries", num_ads);
1232 return WIMLIB_ERR_NOMEM;
1235 for (u16 i = 0; i < num_ads; i++) {
1236 struct wim_ads_entry *cur_entry;
1238 u64 length_no_padding;
1240 const u8 *p_save = p;
1242 cur_entry = &ads_entries[i];
1245 ads_entries[i].stream_id = i + 1;
1248 /* Read the base stream entry, excluding the stream name. */
1249 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1250 ERROR("Stream entries go past end of metadata resource");
1251 ERROR("(remaining_size = %"PRIu64")", remaining_size);
1252 ret = WIMLIB_ERR_INVALID_DENTRY;
1253 goto out_free_ads_entries;
1256 p = get_u64(p, &length);
1257 p += 8; /* Skip the reserved field */
1258 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
1259 p = get_u16(p, &cur_entry->stream_name_nbytes);
1261 cur_entry->stream_name = NULL;
1263 /* Length including neither the null terminator nor the padding
1265 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1266 cur_entry->stream_name_nbytes;
1268 /* Length including the null terminator and the padding */
1269 total_length = ((length_no_padding + 2) + 7) & ~7;
1271 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1273 if (remaining_size < length_no_padding) {
1274 ERROR("Stream entries go past end of metadata resource");
1275 ERROR("(remaining_size = %"PRIu64" bytes, "
1276 "length_no_padding = %"PRIu64" bytes)",
1277 remaining_size, length_no_padding);
1278 ret = WIMLIB_ERR_INVALID_DENTRY;
1279 goto out_free_ads_entries;
1282 /* The @length field in the on-disk ADS entry is expected to be
1283 * equal to @total_length, which includes all of the entry and
1284 * the padding that follows it to align the next ADS entry to an
1285 * 8-byte boundary. However, to be safe, we'll accept the
1286 * length field as long as it's not less than the un-padded
1287 * total length and not more than the padded total length. */
1288 if (length < length_no_padding || length > total_length) {
1289 ERROR("Stream entry has unexpected length "
1290 "field (length field = %"PRIu64", "
1291 "unpadded total length = %"PRIu64", "
1292 "padded total length = %"PRIu64")",
1293 length, length_no_padding, total_length);
1294 ret = WIMLIB_ERR_INVALID_DENTRY;
1295 goto out_free_ads_entries;
1298 if (cur_entry->stream_name_nbytes) {
1299 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1300 if (!cur_entry->stream_name) {
1301 ret = WIMLIB_ERR_NOMEM;
1302 goto out_free_ads_entries;
1304 get_bytes(p, cur_entry->stream_name_nbytes,
1305 cur_entry->stream_name);
1306 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = 0;
1308 /* It's expected that the size of every ADS entry is a multiple
1309 * of 8. However, to be safe, I'm allowing the possibility of
1310 * an ADS entry at the very end of the metadata resource ending
1311 * un-aligned. So although we still need to increment the input
1312 * pointer by @total_length to reach the next ADS entry, it's
1313 * possible that less than @total_length is actually remaining
1314 * in the metadata resource. We should set the remaining size to
1315 * 0 bytes if this happens. */
1316 p = p_save + total_length;
1317 if (remaining_size < total_length)
1320 remaining_size -= total_length;
1322 inode->i_ads_entries = ads_entries;
1324 inode->i_next_stream_id = inode->i_num_ads + 1;
1327 out_free_ads_entries:
1328 for (u16 i = 0; i < num_ads; i++)
1329 destroy_ads_entry(&ads_entries[i]);
1335 * Reads a WIM directory entry, including all alternate data stream entries that
1336 * follow it, from the WIM image's metadata resource.
1338 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1339 * @metadata_resource_len: Length of the metadata resource.
1340 * @offset: Offset of this directory entry in the metadata resource.
1341 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1343 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1344 * been modified, but it will not be left with pointers to any allocated
1345 * buffers. On success, the dentry->length field must be examined. If zero,
1346 * this was a special "end of directory" dentry and not a real dentry. If
1347 * nonzero, this was a real dentry.
1350 read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1351 u64 offset, struct wim_dentry *dentry)
1354 u64 calculated_size;
1355 utf16lechar *file_name = NULL;
1356 utf16lechar *short_name = NULL;
1357 u16 short_name_nbytes;
1358 u16 file_name_nbytes;
1360 struct wim_inode *inode = NULL;
1362 dentry_common_init(dentry);
1364 /*Make sure the dentry really fits into the metadata resource.*/
1365 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1366 ERROR("Directory entry starting at %"PRIu64" ends past the "
1367 "end of the metadata resource (size %"PRIu64")",
1368 offset, metadata_resource_len);
1369 return WIMLIB_ERR_INVALID_DENTRY;
1372 /* Before reading the whole dentry, we need to read just the length.
1373 * This is because a dentry of length 8 (that is, just the length field)
1374 * terminates the list of sibling directory entries. */
1376 p = get_u64(&metadata_resource[offset], &dentry->length);
1378 /* A zero length field (really a length of 8, since that's how big the
1379 * directory entry is...) indicates that this is the end of directory
1380 * dentry. We do not read it into memory as an actual dentry, so just
1381 * return successfully in that case. */
1382 if (dentry->length == 0)
1385 /* If the dentry does not overflow the metadata resource buffer and is
1386 * not too short, read the rest of it (excluding the alternate data
1387 * streams, but including the file name and short name variable-length
1388 * fields) into memory. */
1389 if (offset + dentry->length >= metadata_resource_len
1390 || offset + dentry->length < offset)
1392 ERROR("Directory entry at offset %"PRIu64" and with size "
1393 "%"PRIu64" ends past the end of the metadata resource "
1395 offset, dentry->length, metadata_resource_len);
1396 return WIMLIB_ERR_INVALID_DENTRY;
1399 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1400 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1402 return WIMLIB_ERR_INVALID_DENTRY;
1405 inode = new_timeless_inode();
1407 return WIMLIB_ERR_NOMEM;
1409 p = get_u32(p, &inode->i_attributes);
1410 p = get_u32(p, (u32*)&inode->i_security_id);
1411 p = get_u64(p, &dentry->subdir_offset);
1413 /* 2 unused fields */
1414 p += 2 * sizeof(u64);
1415 /*p = get_u64(p, &dentry->unused1);*/
1416 /*p = get_u64(p, &dentry->unused2);*/
1418 p = get_u64(p, &inode->i_creation_time);
1419 p = get_u64(p, &inode->i_last_access_time);
1420 p = get_u64(p, &inode->i_last_write_time);
1422 p = get_bytes(p, SHA1_HASH_SIZE, inode->i_hash);
1425 * I don't know what's going on here. It seems like M$ screwed up the
1426 * reparse points, then put the fields in the same place and didn't
1427 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1428 * have something to do with this, but it's not documented.
1430 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1433 p = get_u32(p, &inode->i_reparse_tag);
1436 p = get_u32(p, &inode->i_reparse_tag);
1437 p = get_u64(p, &inode->i_ino);
1440 /* By the way, the reparse_reserved field does not actually exist (at
1441 * least when the file is not a reparse point) */
1443 p = get_u16(p, &inode->i_num_ads);
1445 p = get_u16(p, &short_name_nbytes);
1446 p = get_u16(p, &file_name_nbytes);
1448 /* We now know the length of the file name and short name. Make sure
1449 * the length of the dentry is large enough to actually hold them.
1451 * The calculated length here is unaligned to allow for the possibility
1452 * that the dentry->length names an unaligned length, although this
1453 * would be unexpected. */
1454 calculated_size = __dentry_correct_length_unaligned(file_name_nbytes,
1457 if (dentry->length < calculated_size) {
1458 ERROR("Unexpected end of directory entry! (Expected "
1459 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1460 "short_name_nbytes = %hu, file_name_nbytes = %hu)",
1461 calculated_size, dentry->length,
1462 short_name_nbytes, file_name_nbytes);
1463 ret = WIMLIB_ERR_INVALID_DENTRY;
1464 goto out_free_inode;
1467 /* Read the filename if present. Note: if the filename is empty, there
1468 * is no null terminator following it. */
1469 if (file_name_nbytes) {
1470 file_name = MALLOC(file_name_nbytes + 2);
1472 ERROR("Failed to allocate %d bytes for dentry file name",
1473 file_name_nbytes + 2);
1474 ret = WIMLIB_ERR_NOMEM;
1475 goto out_free_inode;
1477 p = get_bytes(p, file_name_nbytes + 2, file_name);
1478 if (file_name[file_name_nbytes / 2] != 0) {
1479 file_name[file_name_nbytes / 2] = 0;
1480 WARNING("File name in WIM dentry \"%"WS"\" is not "
1481 "null-terminated!", file_name);
1485 /* Align the calculated size */
1486 calculated_size = (calculated_size + 7) & ~7;
1488 if (dentry->length > calculated_size) {
1489 /* Weird; the dentry says it's longer than it should be. Note
1490 * that the length field does NOT include the size of the
1491 * alternate stream entries. */
1493 /* Strangely, some directory entries inexplicably have a little
1494 * over 70 bytes of extra data. The exact amount of data seems
1495 * to be 72 bytes, but it is aligned on the next 8-byte
1496 * boundary. It does NOT seem to be alternate data stream
1497 * entries. Here's an example of the aligned data:
1499 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1500 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1501 * 00000000 00000000 00000000 00000000
1503 * Here's one interpretation of how the data is laid out.
1506 * u32 field1; (always 0x00000001)
1507 * u32 field2; (always 0x40000000)
1508 * u8 data[48]; (???)
1509 * u64 reserved1; (always 0)
1510 * u64 reserved2; (always 0)
1512 /*DEBUG("Dentry for file or directory `%s' has %"PRIu64" extra "*/
1513 /*"bytes of data",*/
1514 /*file_name_utf8, dentry->length - calculated_size);*/
1517 /* Read the short filename if present. Note: if there is no short
1518 * filename, there is no null terminator following it. */
1519 if (short_name_nbytes) {
1520 short_name = MALLOC(short_name_nbytes + 2);
1522 ERROR("Failed to allocate %d bytes for dentry short name",
1523 short_name_nbytes + 2);
1524 ret = WIMLIB_ERR_NOMEM;
1525 goto out_free_file_name;
1527 p = get_bytes(p, short_name_nbytes + 2, short_name);
1528 if (short_name[short_name_nbytes / 2] != 0) {
1529 short_name[short_name_nbytes / 2] = 0;
1530 WARNING("Short name in WIM dentry \"%"WS"\" is not "
1531 "null-terminated!", file_name);
1536 * Read the alternate data streams, if present. dentry->num_ads tells
1537 * us how many they are, and they will directly follow the dentry
1540 * Note that each alternate data stream entry begins on an 8-byte
1541 * aligned boundary, and the alternate data stream entries are NOT
1542 * included in the dentry->length field for some reason.
1544 if (inode->i_num_ads != 0) {
1546 /* Trying different lengths is just a hack to make sure we have
1547 * a chance of reading the ADS entries correctly despite the
1548 * poor documentation. */
1550 if (calculated_size != dentry->length) {
1551 WARNING("Trying calculated dentry length (%"PRIu64") "
1552 "instead of dentry->length field (%"PRIu64") "
1553 "to read ADS entries",
1554 calculated_size, dentry->length);
1556 u64 lengths_to_try[3] = {calculated_size,
1557 (dentry->length + 7) & ~7,
1559 ret = WIMLIB_ERR_INVALID_DENTRY;
1560 for (size_t i = 0; i < ARRAY_LEN(lengths_to_try); i++) {
1561 if (lengths_to_try[i] > metadata_resource_len - offset)
1563 ret = read_ads_entries(&metadata_resource[offset + lengths_to_try[i]],
1565 metadata_resource_len - offset - lengths_to_try[i]);
1569 ERROR("Failed to read alternate data stream "
1570 "entries of WIM dentry \"%"WS"\"", file_name);
1571 goto out_free_short_name;
1574 /* We've read all the data for this dentry. Set the names and their
1575 * lengths, and we've done. */
1576 dentry->d_inode = inode;
1577 dentry->file_name = file_name;
1578 dentry->short_name = short_name;
1579 dentry->file_name_nbytes = file_name_nbytes;
1580 dentry->short_name_nbytes = short_name_nbytes;
1582 out_free_short_name:
1591 /* Reads the children of a dentry, and all their children, ..., etc. from the
1592 * metadata resource and into the dentry tree.
1594 * @metadata_resource: An array that contains the uncompressed metadata
1595 * resource for the WIM file.
1597 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1600 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
1601 * tree and has already been read from the metadata resource. It
1602 * does not need to be the real root because this procedure is
1603 * called recursively.
1605 * Returns zero on success; nonzero on failure.
1608 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1609 struct wim_dentry *dentry)
1611 u64 cur_offset = dentry->subdir_offset;
1612 struct wim_dentry *child;
1613 struct wim_dentry cur_child;
1617 * If @dentry has no child dentries, nothing more needs to be done for
1618 * this branch. This is the case for regular files, symbolic links, and
1619 * *possibly* empty directories (although an empty directory may also
1620 * have one child dentry that is the special end-of-directory dentry)
1622 if (cur_offset == 0)
1625 /* Find and read all the children of @dentry. */
1628 /* Read next child of @dentry into @cur_child. */
1629 ret = read_dentry(metadata_resource, metadata_resource_len,
1630 cur_offset, &cur_child);
1634 /* Check for end of directory. */
1635 if (cur_child.length == 0)
1638 /* Not end of directory. Allocate this child permanently and
1639 * link it to the parent and previous child. */
1640 child = MALLOC(sizeof(struct wim_dentry));
1642 ERROR("Failed to allocate %zu bytes for new dentry",
1643 sizeof(struct wim_dentry));
1644 ret = WIMLIB_ERR_NOMEM;
1647 memcpy(child, &cur_child, sizeof(struct wim_dentry));
1648 dentry_add_child(dentry, child);
1649 inode_add_dentry(child, child->d_inode);
1651 /* If there are children of this child, call this procedure
1653 if (child->subdir_offset != 0) {
1654 ret = read_dentry_tree(metadata_resource,
1655 metadata_resource_len, child);
1660 /* Advance to the offset of the next child. Note: We need to
1661 * advance by the TOTAL length of the dentry, not by the length
1662 * child->length, which although it does take into account the
1663 * padding, it DOES NOT take into account alternate stream
1665 cur_offset += dentry_total_length(child);
1671 * Writes a WIM dentry to an output buffer.
1673 * @dentry: The dentry structure.
1674 * @p: The memory location to write the data to.
1675 * @return: Pointer to the byte after the last byte we wrote as part of the
1679 write_dentry(const struct wim_dentry *dentry, u8 *p)
1683 const struct wim_inode *inode = dentry->d_inode;
1685 /* We calculate the correct length of the dentry ourselves because the
1686 * dentry->length field may been set to an unexpected value from when we
1687 * read the dentry in (for example, there may have been unknown data
1688 * appended to the end of the dentry...) */
1689 u64 length = dentry_correct_length(dentry);
1691 p = put_u64(p, length);
1692 p = put_u32(p, inode->i_attributes);
1693 p = put_u32(p, inode->i_security_id);
1694 p = put_u64(p, dentry->subdir_offset);
1695 p = put_u64(p, 0); /* unused1 */
1696 p = put_u64(p, 0); /* unused2 */
1697 p = put_u64(p, inode->i_creation_time);
1698 p = put_u64(p, inode->i_last_access_time);
1699 p = put_u64(p, inode->i_last_write_time);
1700 hash = inode_stream_hash(inode, 0);
1701 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1702 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1703 p = put_zeroes(p, 4);
1704 p = put_u32(p, inode->i_reparse_tag);
1705 p = put_zeroes(p, 4);
1709 if (inode->i_nlink == 1)
1712 link_group_id = inode->i_ino;
1713 p = put_u64(p, link_group_id);
1715 p = put_u16(p, inode->i_num_ads);
1716 p = put_u16(p, dentry->short_name_nbytes);
1717 p = put_u16(p, dentry->file_name_nbytes);
1718 if (dentry_has_long_name(dentry)) {
1719 p = put_bytes(p, dentry->file_name_nbytes + 2,
1722 if (dentry_has_short_name(dentry)) {
1723 p = put_bytes(p, dentry->short_name_nbytes + 2,
1724 dentry->short_name);
1727 /* Align to 8-byte boundary */
1728 wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1729 p = put_zeroes(p, length - (p - orig_p));
1731 /* Write the alternate data streams, if there are any. Please see
1732 * read_ads_entries() for comments about the format of the on-disk
1733 * alternate data stream entries. */
1734 for (u16 i = 0; i < inode->i_num_ads; i++) {
1735 p = put_u64(p, ads_entry_total_length(&inode->i_ads_entries[i]));
1736 p = put_u64(p, 0); /* Unused */
1737 hash = inode_stream_hash(inode, i + 1);
1738 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1739 p = put_u16(p, inode->i_ads_entries[i].stream_name_nbytes);
1740 if (inode->i_ads_entries[i].stream_name_nbytes) {
1742 inode->i_ads_entries[i].stream_name_nbytes + 2,
1743 inode->i_ads_entries[i].stream_name);
1745 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1747 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1752 write_dentry_cb(struct wim_dentry *dentry, void *_p)
1755 *p = write_dentry(dentry, *p);
1760 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
1763 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
1766 *p = write_dentry_tree_recursive(dentry, *p);
1770 /* Recursive function that writes a dentry tree rooted at @parent, not including
1771 * @parent itself, which has already been written. */
1773 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
1775 /* Nothing to do if this dentry has no children. */
1776 if (parent->subdir_offset == 0)
1779 /* Write child dentries and end-of-directory entry.
1781 * Note: we need to write all of this dentry's children before
1782 * recursively writing the directory trees rooted at each of the child
1783 * dentries, since the on-disk dentries for a dentry's children are
1784 * always located at consecutive positions in the metadata resource! */
1785 for_dentry_child(parent, write_dentry_cb, &p);
1787 /* write end of directory entry */
1790 /* Recurse on children. */
1791 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
1795 /* Writes a directory tree to the metadata resource.
1797 * @root: Root of the dentry tree.
1798 * @p: Pointer to a buffer with enough space for the dentry tree.
1800 * Returns pointer to the byte after the last byte we wrote.
1803 write_dentry_tree(const struct wim_dentry *root, u8 *p)
1805 DEBUG("Writing dentry tree.");
1806 wimlib_assert(dentry_is_root(root));
1808 /* If we're the root dentry, we have no parent that already
1809 * wrote us, so we need to write ourselves. */
1810 p = write_dentry(root, p);
1812 /* Write end of directory entry after the root dentry just to be safe;
1813 * however the root dentry obviously cannot have any siblings. */
1816 /* Recursively write the rest of the dentry tree. */
1817 return write_dentry_tree_recursive(root, p);