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 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
37 * a file name and short name that take the specified numbers of bytes. This
38 * excludes any alternate data stream entries that may follow the dentry. */
40 __dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
42 u64 length = WIM_DENTRY_DISK_SIZE;
44 length += file_name_nbytes + 2;
45 if (short_name_nbytes)
46 length += short_name_nbytes + 2;
50 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
51 * the file name length and short name length. Note that dentry->length is
52 * ignored; also, this excludes any alternate data stream entries that may
53 * follow the dentry. */
55 dentry_correct_length_unaligned(const struct wim_dentry *dentry)
57 return __dentry_correct_length_unaligned(dentry->file_name_nbytes,
58 dentry->short_name_nbytes);
61 /* Return the "correct" value to write in the length field of a WIM dentry,
62 * based on the file name length and short name length. */
64 dentry_correct_length(const struct wim_dentry *dentry)
66 return (dentry_correct_length_unaligned(dentry) + 7) & ~7;
69 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
70 * stream name @name that has length @name_nbytes bytes. */
72 ads_entry_has_name(const struct wim_ads_entry *entry,
73 const utf16lechar *name, size_t name_nbytes)
75 return entry->stream_name_nbytes == name_nbytes &&
76 memcmp(entry->stream_name, name, name_nbytes) == 0;
79 /* Duplicates a string of system-dependent encoding into a UTF-16LE string and
80 * returns the string and its length, in bytes, in the pointer arguments. Frees
81 * any existing string at the return location before overwriting it. */
83 get_utf16le_name(const tchar *name, utf16lechar **name_utf16le_ret,
84 u16 *name_utf16le_nbytes_ret)
86 utf16lechar *name_utf16le;
87 size_t name_utf16le_nbytes;
90 name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
91 name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
93 return WIMLIB_ERR_NOMEM;
94 memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
98 ret = tstr_to_utf16le(name, tstrlen(name), &name_utf16le,
99 &name_utf16le_nbytes);
101 if (name_utf16le_nbytes > 0xffff) {
103 ERROR("Multibyte string \"%"TS"\" is too long!", name);
104 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
109 FREE(*name_utf16le_ret);
110 *name_utf16le_ret = name_utf16le;
111 *name_utf16le_nbytes_ret = name_utf16le_nbytes;
116 /* Sets the name of a WIM dentry from a multibyte string. */
118 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name)
121 ret = get_utf16le_name(new_name, &dentry->file_name,
122 &dentry->file_name_nbytes);
124 /* Clear the short name and recalculate the dentry length */
125 if (dentry_has_short_name(dentry)) {
126 FREE(dentry->short_name);
127 dentry->short_name = NULL;
128 dentry->short_name_nbytes = 0;
130 dentry->length = dentry_correct_length(dentry);
135 /* Returns the total length of a WIM alternate data stream entry on-disk,
136 * including the stream name, the null terminator, AND the padding after the
137 * entry to align the next ADS entry or dentry on an 8-byte boundary. */
139 ads_entry_total_length(const struct wim_ads_entry *entry)
141 u64 len = WIM_ADS_ENTRY_DISK_SIZE;
142 if (entry->stream_name_nbytes)
143 len += entry->stream_name_nbytes + 2;
144 return (len + 7) & ~7;
149 __dentry_total_length(const struct wim_dentry *dentry, u64 length)
151 const struct wim_inode *inode = dentry->d_inode;
152 for (u16 i = 0; i < inode->i_num_ads; i++)
153 length += ads_entry_total_length(&inode->i_ads_entries[i]);
154 return (length + 7) & ~7;
157 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
158 * all alternate data streams. */
160 dentry_correct_total_length(const struct wim_dentry *dentry)
162 return __dentry_total_length(dentry,
163 dentry_correct_length_unaligned(dentry));
166 /* Like dentry_correct_total_length(), but use the existing dentry->length field
167 * instead of calculating its "correct" value. */
169 dentry_total_length(const struct wim_dentry *dentry)
171 return __dentry_total_length(dentry, dentry->length);
175 for_dentry_in_rbtree(struct rb_node *root,
176 int (*visitor)(struct wim_dentry *, void *),
180 struct rb_node *node = root;
184 list_add(&rbnode_dentry(node)->tmp_list, &stack);
185 node = node->rb_left;
187 struct list_head *next;
188 struct wim_dentry *dentry;
193 dentry = container_of(next, struct wim_dentry, tmp_list);
195 ret = visitor(dentry, arg);
198 node = dentry->rb_node.rb_right;
204 for_dentry_tree_in_rbtree_depth(struct rb_node *node,
205 int (*visitor)(struct wim_dentry*, void*),
210 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
214 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
218 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
226 for_dentry_tree_in_rbtree(struct rb_node *node,
227 int (*visitor)(struct wim_dentry*, void*),
232 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
235 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
238 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
245 /* Calls a function on all directory entries in a WIM dentry tree. Logically,
246 * this is a pre-order traversal (the function is called on a parent dentry
247 * before its children), but sibling dentries will be visited in order as well.
250 for_dentry_in_tree(struct wim_dentry *root,
251 int (*visitor)(struct wim_dentry*, void*), void *arg)
253 int ret = visitor(root, arg);
255 ret = for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node,
262 /* Like for_dentry_in_tree(), but the visitor function is always called on a
263 * dentry's children before on itself. */
265 for_dentry_in_tree_depth(struct wim_dentry *root,
266 int (*visitor)(struct wim_dentry*, void*), void *arg)
269 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
272 ret = visitor(root, arg);
276 /* Calculate the full path of @dentry. The full path of its parent must have
277 * already been calculated, or it must be the root dentry. */
279 calculate_dentry_full_path(struct wim_dentry *dentry)
282 u32 full_path_nbytes;
285 if (dentry->_full_path)
288 if (dentry_is_root(dentry)) {
289 full_path = TSTRDUP(T("/"));
291 return WIMLIB_ERR_NOMEM;
292 full_path_nbytes = 1 * sizeof(tchar);
294 struct wim_dentry *parent;
295 tchar *parent_full_path;
296 u32 parent_full_path_nbytes;
297 size_t filename_nbytes;
299 parent = dentry->parent;
300 if (dentry_is_root(parent)) {
301 parent_full_path = T("");
302 parent_full_path_nbytes = 0;
304 if (!parent->_full_path) {
305 ret = calculate_dentry_full_path(parent);
309 parent_full_path = parent->_full_path;
310 parent_full_path_nbytes = parent->full_path_nbytes;
313 /* Append this dentry's name as a tchar string to the full path
314 * of the parent followed by the path separator */
316 filename_nbytes = dentry->file_name_nbytes;
319 int ret = utf16le_to_tstr_nbytes(dentry->file_name,
320 dentry->file_name_nbytes,
327 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
329 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
331 return WIMLIB_ERR_NOMEM;
332 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
333 full_path[parent_full_path_nbytes / sizeof(tchar)] = T('/');
335 memcpy(&full_path[parent_full_path_nbytes / sizeof(tchar) + 1],
337 filename_nbytes + sizeof(tchar));
339 utf16le_to_tstr_buf(dentry->file_name,
340 dentry->file_name_nbytes,
341 &full_path[parent_full_path_nbytes /
345 dentry->_full_path = full_path;
346 dentry->full_path_nbytes= full_path_nbytes;
351 do_calculate_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
353 return calculate_dentry_full_path(dentry);
357 calculate_dentry_tree_full_paths(struct wim_dentry *root)
359 return for_dentry_in_tree(root, do_calculate_dentry_full_path, NULL);
363 dentry_full_path(struct wim_dentry *dentry)
365 calculate_dentry_full_path(dentry);
366 return dentry->_full_path;
370 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
372 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
377 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
379 calculate_subdir_offsets(dentry, subdir_offset_p);
384 * Recursively calculates the subdir offsets for a directory tree.
386 * @dentry: The root of the directory tree.
387 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
388 * offset for @dentry.
391 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
393 struct rb_node *node;
395 dentry->subdir_offset = *subdir_offset_p;
396 node = dentry->d_inode->i_children.rb_node;
398 /* Advance the subdir offset by the amount of space the children
399 * of this dentry take up. */
400 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
402 /* End-of-directory dentry on disk. */
403 *subdir_offset_p += 8;
405 /* Recursively call calculate_subdir_offsets() on all the
407 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
409 /* On disk, childless directories have a valid subdir_offset
410 * that points to an 8-byte end-of-directory dentry. Regular
411 * files or reparse points have a subdir_offset of 0. */
412 if (dentry_is_directory(dentry))
413 *subdir_offset_p += 8;
415 dentry->subdir_offset = 0;
420 compare_utf16le_names(const utf16lechar *name1, size_t nbytes1,
421 const utf16lechar *name2, size_t nbytes2)
423 int result = memcmp(name1, name2, min(nbytes1, nbytes2));
427 return (int)nbytes1 - (int)nbytes2;
431 dentry_compare_names(const struct wim_dentry *d1, const struct wim_dentry *d2)
433 return compare_utf16le_names(d1->file_name, d1->file_name_nbytes,
434 d2->file_name, d2->file_name_nbytes);
439 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
440 const utf16lechar *name,
443 struct rb_node *node = dentry->d_inode->i_children.rb_node;
444 struct wim_dentry *child;
446 child = rbnode_dentry(node);
447 int result = compare_utf16le_names(name, name_nbytes,
449 child->file_name_nbytes);
451 node = node->rb_left;
453 node = node->rb_right;
460 /* Returns the child of @dentry that has the file name @name. Returns NULL if
461 * no child has the name. */
463 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
466 return get_dentry_child_with_utf16le_name(dentry, name,
467 tstrlen(name) * sizeof(tchar));
469 utf16lechar *utf16le_name;
470 size_t utf16le_name_nbytes;
472 struct wim_dentry *child;
474 ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
475 &utf16le_name, &utf16le_name_nbytes);
479 child = get_dentry_child_with_utf16le_name(dentry,
481 utf16le_name_nbytes);
488 static struct wim_dentry *
489 get_dentry_utf16le(WIMStruct *w, const utf16lechar *path,
492 struct wim_dentry *cur_dentry, *parent_dentry;
493 const utf16lechar *p, *pp;
495 cur_dentry = parent_dentry = wim_root_dentry(w);
498 while (*p == cpu_to_le16('/'))
503 while (*pp != cpu_to_le16('/') && *pp != cpu_to_le16('\0'))
506 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
507 (void*)pp - (void*)p);
508 if (cur_dentry == NULL)
511 parent_dentry = cur_dentry;
513 if (cur_dentry == NULL) {
514 if (dentry_is_directory(parent_dentry))
522 /* Returns the dentry corresponding to the @path, or NULL if there is no such
525 get_dentry(WIMStruct *w, const tchar *path)
528 return get_dentry_utf16le(w, path, tstrlen(path) * sizeof(tchar));
530 utf16lechar *path_utf16le;
531 size_t path_utf16le_nbytes;
533 struct wim_dentry *dentry;
535 ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
536 &path_utf16le, &path_utf16le_nbytes);
539 dentry = get_dentry_utf16le(w, path_utf16le, path_utf16le_nbytes);
546 wim_pathname_to_inode(WIMStruct *w, const tchar *path)
548 struct wim_dentry *dentry;
549 dentry = get_dentry(w, path);
551 return dentry->d_inode;
556 /* Takes in a path of length @len in @buf, and transforms it into a string for
557 * the path of its parent directory. */
559 to_parent_name(tchar *buf, size_t len)
561 ssize_t i = (ssize_t)len - 1;
562 while (i >= 0 && buf[i] == T('/'))
564 while (i >= 0 && buf[i] != T('/'))
566 while (i >= 0 && buf[i] == T('/'))
568 buf[i + 1] = T('\0');
571 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
572 * if the dentry is not found. */
574 get_parent_dentry(WIMStruct *w, const tchar *path)
576 size_t path_len = tstrlen(path);
577 tchar buf[path_len + 1];
579 tmemcpy(buf, path, path_len + 1);
580 to_parent_name(buf, path_len);
581 return get_dentry(w, buf);
584 /* Prints the full path of a dentry. */
586 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
588 int ret = calculate_dentry_full_path(dentry);
591 tprintf(T("%"TS"\n"), dentry->_full_path);
595 /* We want to be able to show the names of the file attribute flags that are
597 struct file_attr_flag {
601 struct file_attr_flag file_attr_flags[] = {
602 {FILE_ATTRIBUTE_READONLY, T("READONLY")},
603 {FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
604 {FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
605 {FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
606 {FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
607 {FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
608 {FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
609 {FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
610 {FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
611 {FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
612 {FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
613 {FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
614 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
615 {FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
616 {FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
619 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
620 * available. If the dentry is unresolved and the lookup table is NULL, the
621 * lookup table entries will not be printed. Otherwise, they will be. */
623 print_dentry(struct wim_dentry *dentry, void *lookup_table)
626 struct wim_lookup_table_entry *lte;
627 const struct wim_inode *inode = dentry->d_inode;
630 tprintf(T("[DENTRY]\n"));
631 tprintf(T("Length = %"PRIu64"\n"), dentry->length);
632 tprintf(T("Attributes = 0x%x\n"), inode->i_attributes);
633 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
634 if (file_attr_flags[i].flag & inode->i_attributes)
635 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
636 file_attr_flags[i].name);
637 tprintf(T("Security ID = %d\n"), inode->i_security_id);
638 tprintf(T("Subdir offset = %"PRIu64"\n"), dentry->subdir_offset);
640 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
641 tprintf(T("Creation Time = %"TS"\n"), buf);
643 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
644 tprintf(T("Last Access Time = %"TS"\n"), buf);
646 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
647 tprintf(T("Last Write Time = %"TS"\n"), buf);
649 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), inode->i_reparse_tag);
650 tprintf(T("Hard Link Group = 0x%"PRIx64"\n"), inode->i_ino);
651 tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
652 tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
653 if (dentry_has_long_name(dentry))
654 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
655 if (dentry_has_short_name(dentry))
656 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
657 if (dentry->_full_path)
658 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
660 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
662 print_lookup_table_entry(lte, stdout);
664 hash = inode_stream_hash(inode, 0);
666 tprintf(T("Hash = 0x"));
667 print_hash(hash, stdout);
672 for (u16 i = 0; i < inode->i_num_ads; i++) {
673 tprintf(T("[Alternate Stream Entry %u]\n"), i);
674 wimlib_printf(T("Name = \"%"WS"\"\n"),
675 inode->i_ads_entries[i].stream_name);
676 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
677 inode->i_ads_entries[i].stream_name_nbytes);
678 hash = inode_stream_hash(inode, i + 1);
680 tprintf(T("Hash = 0x"));
681 print_hash(hash, stdout);
684 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
690 /* Initializations done on every `struct wim_dentry'. */
692 dentry_common_init(struct wim_dentry *dentry)
694 memset(dentry, 0, sizeof(struct wim_dentry));
700 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
702 inode->i_security_id = -1;
704 inode->i_next_stream_id = 1;
705 inode->i_not_rpfixed = 1;
707 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
708 ERROR_WITH_ERRNO("Error initializing mutex");
713 INIT_LIST_HEAD(&inode->i_dentry);
718 static struct wim_inode *
721 struct wim_inode *inode = new_timeless_inode();
723 u64 now = get_wim_timestamp();
724 inode->i_creation_time = now;
725 inode->i_last_access_time = now;
726 inode->i_last_write_time = now;
731 /* Creates an unlinked directory entry. */
733 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
735 struct wim_dentry *dentry;
738 dentry = MALLOC(sizeof(struct wim_dentry));
740 return WIMLIB_ERR_NOMEM;
742 dentry_common_init(dentry);
743 ret = set_dentry_name(dentry, name);
745 dentry->parent = dentry;
746 *dentry_ret = dentry;
749 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
757 __new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
760 struct wim_dentry *dentry;
763 ret = new_dentry(name, &dentry);
768 dentry->d_inode = new_timeless_inode();
770 dentry->d_inode = new_inode();
771 if (!dentry->d_inode) {
773 return WIMLIB_ERR_NOMEM;
776 inode_add_dentry(dentry, dentry->d_inode);
777 *dentry_ret = dentry;
782 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
784 return __new_dentry_with_inode(name, dentry_ret, true);
788 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
790 return __new_dentry_with_inode(name, dentry_ret, false);
795 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
796 size_t name_nbytes, bool is_utf16le)
799 memset(ads_entry, 0, sizeof(*ads_entry));
802 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
804 return WIMLIB_ERR_NOMEM;
805 memcpy(p, name, name_nbytes);
806 p[name_nbytes / 2] = 0;
807 ads_entry->stream_name = p;
808 ads_entry->stream_name_nbytes = name_nbytes;
810 if (name && *(const tchar*)name != T('\0')) {
811 ret = get_utf16le_name(name, &ads_entry->stream_name,
812 &ads_entry->stream_name_nbytes);
819 destroy_ads_entry(struct wim_ads_entry *ads_entry)
821 FREE(ads_entry->stream_name);
824 /* Frees an inode. */
826 free_inode(struct wim_inode *inode)
829 if (inode->i_ads_entries) {
830 for (u16 i = 0; i < inode->i_num_ads; i++)
831 destroy_ads_entry(&inode->i_ads_entries[i]);
832 FREE(inode->i_ads_entries);
835 wimlib_assert(inode->i_num_opened_fds == 0);
837 pthread_mutex_destroy(&inode->i_mutex);
838 if (inode->i_hlist.pprev)
839 hlist_del(&inode->i_hlist);
841 FREE(inode->i_extracted_file);
846 /* Decrements link count on an inode and frees it if the link count reaches 0.
849 put_inode(struct wim_inode *inode)
851 wimlib_assert(inode->i_nlink != 0);
852 if (--inode->i_nlink == 0) {
854 if (inode->i_num_opened_fds == 0)
862 /* Frees a WIM dentry.
864 * The corresponding inode (if any) is freed only if its link count is
868 free_dentry(struct wim_dentry *dentry)
870 FREE(dentry->file_name);
871 FREE(dentry->short_name);
872 FREE(dentry->_full_path);
874 put_inode(dentry->d_inode);
878 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
879 * to free a directory tree. */
881 do_free_dentry(struct wim_dentry *dentry, void *__lookup_table)
883 struct wim_lookup_table *lookup_table = __lookup_table;
887 struct wim_lookup_table_entry *lte;
888 struct wim_inode *inode = dentry->d_inode;
889 wimlib_assert(inode->i_nlink != 0);
890 for (i = 0; i <= inode->i_num_ads; i++) {
891 lte = inode_stream_lte(inode, i, lookup_table);
893 lte_decrement_refcnt(lte, lookup_table);
901 * Unlinks and frees a dentry tree.
903 * @root: The root of the tree.
904 * @lookup_table: The lookup table for dentries. If non-NULL, the
905 * reference counts in the lookup table for the lookup
906 * table entries corresponding to the dentries will be
910 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
913 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
917 * Links a dentry into the directory tree.
919 * @parent: The dentry that will be the parent of @dentry.
920 * @dentry: The dentry to link.
923 dentry_add_child(struct wim_dentry * restrict parent,
924 struct wim_dentry * restrict child)
926 wimlib_assert(dentry_is_directory(parent));
928 struct rb_root *root = &parent->d_inode->i_children;
929 struct rb_node **new = &(root->rb_node);
930 struct rb_node *rb_parent = NULL;
933 struct wim_dentry *this = rbnode_dentry(*new);
934 int result = dentry_compare_names(child, this);
939 new = &((*new)->rb_left);
941 new = &((*new)->rb_right);
945 child->parent = parent;
946 rb_link_node(&child->rb_node, rb_parent, new);
947 rb_insert_color(&child->rb_node, root);
951 /* Unlink a WIM dentry from the directory entry tree. */
953 unlink_dentry(struct wim_dentry *dentry)
955 struct wim_dentry *parent = dentry->parent;
956 if (parent == dentry)
958 rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
962 * Returns the alternate data stream entry belonging to @inode that has the
963 * stream name @stream_name.
965 struct wim_ads_entry *
966 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
969 if (inode->i_num_ads == 0) {
972 size_t stream_name_utf16le_nbytes;
974 struct wim_ads_entry *result;
977 const utf16lechar *stream_name_utf16le;
979 stream_name_utf16le = stream_name;
980 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
982 utf16lechar *stream_name_utf16le;
985 int ret = tstr_to_utf16le(stream_name,
986 tstrlen(stream_name) *
988 &stream_name_utf16le,
989 &stream_name_utf16le_nbytes);
997 if (ads_entry_has_name(&inode->i_ads_entries[i],
999 stream_name_utf16le_nbytes))
1003 result = &inode->i_ads_entries[i];
1006 } while (++i != inode->i_num_ads);
1007 #if !TCHAR_IS_UTF16LE
1008 FREE(stream_name_utf16le);
1014 static struct wim_ads_entry *
1015 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1016 size_t stream_name_nbytes, bool is_utf16le)
1019 struct wim_ads_entry *ads_entries;
1020 struct wim_ads_entry *new_entry;
1022 if (inode->i_num_ads >= 0xfffe) {
1023 ERROR("Too many alternate data streams in one inode!");
1026 num_ads = inode->i_num_ads + 1;
1027 ads_entries = REALLOC(inode->i_ads_entries,
1028 num_ads * sizeof(inode->i_ads_entries[0]));
1030 ERROR("Failed to allocate memory for new alternate data stream");
1033 inode->i_ads_entries = ads_entries;
1035 new_entry = &inode->i_ads_entries[num_ads - 1];
1036 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1038 new_entry->stream_id = inode->i_next_stream_id++;
1039 inode->i_num_ads = num_ads;
1043 struct wim_ads_entry *
1044 inode_add_ads_utf16le(struct wim_inode *inode,
1045 const utf16lechar *stream_name,
1046 size_t stream_name_nbytes)
1048 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1049 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1053 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1054 * NULL if memory could not be allocated.
1056 struct wim_ads_entry *
1057 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1059 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1060 return do_inode_add_ads(inode, stream_name,
1061 tstrlen(stream_name) * sizeof(tchar),
1066 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1067 const void *value, size_t size,
1068 struct wim_lookup_table *lookup_table)
1070 int ret = WIMLIB_ERR_NOMEM;
1071 struct wim_ads_entry *new_ads_entry;
1072 struct wim_lookup_table_entry *existing_lte;
1073 struct wim_lookup_table_entry *lte;
1074 u8 value_hash[SHA1_HASH_SIZE];
1076 wimlib_assert(inode->i_resolved);
1077 new_ads_entry = inode_add_ads(inode, name);
1080 sha1_buffer((const u8*)value, size, value_hash);
1081 existing_lte = __lookup_resource(lookup_table, value_hash);
1087 lte = new_lookup_table_entry();
1089 goto out_remove_ads_entry;
1090 value_copy = MALLOC(size);
1093 goto out_remove_ads_entry;
1095 memcpy(value_copy, value, size);
1096 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1097 lte->attached_buffer = value_copy;
1098 lte->resource_entry.original_size = size;
1099 lte->resource_entry.size = size;
1100 copy_hash(lte->hash, value_hash);
1101 lookup_table_insert(lookup_table, lte);
1103 new_ads_entry->lte = lte;
1106 out_remove_ads_entry:
1107 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1113 /* Remove an alternate data stream from a WIM inode */
1115 inode_remove_ads(struct wim_inode *inode, u16 idx,
1116 struct wim_lookup_table *lookup_table)
1118 struct wim_ads_entry *ads_entry;
1119 struct wim_lookup_table_entry *lte;
1121 wimlib_assert(idx < inode->i_num_ads);
1122 wimlib_assert(inode->i_resolved);
1124 ads_entry = &inode->i_ads_entries[idx];
1126 DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1128 lte = ads_entry->lte;
1130 lte_decrement_refcnt(lte, lookup_table);
1132 destroy_ads_entry(ads_entry);
1134 memmove(&inode->i_ads_entries[idx],
1135 &inode->i_ads_entries[idx + 1],
1136 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1142 inode_get_unix_data(const struct wim_inode *inode,
1143 struct wimlib_unix_data *unix_data,
1144 u16 *stream_idx_ret)
1146 const struct wim_ads_entry *ads_entry;
1147 const struct wim_lookup_table_entry *lte;
1151 wimlib_assert(inode->i_resolved);
1153 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1154 WIMLIB_UNIX_DATA_TAG, NULL);
1156 return NO_UNIX_DATA;
1159 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1161 lte = ads_entry->lte;
1163 return NO_UNIX_DATA;
1165 size = wim_resource_size(lte);
1166 if (size != sizeof(struct wimlib_unix_data))
1167 return BAD_UNIX_DATA;
1169 ret = read_full_resource_into_buf(lte, unix_data, true);
1173 if (unix_data->version != 0)
1174 return BAD_UNIX_DATA;
1179 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1180 struct wim_lookup_table *lookup_table, int which)
1182 struct wimlib_unix_data unix_data;
1184 bool have_good_unix_data = false;
1185 bool have_unix_data = false;
1188 if (!(which & UNIX_DATA_CREATE)) {
1189 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1190 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1191 have_unix_data = true;
1193 have_good_unix_data = true;
1195 unix_data.version = 0;
1196 if (which & UNIX_DATA_UID || !have_good_unix_data)
1197 unix_data.uid = uid;
1198 if (which & UNIX_DATA_GID || !have_good_unix_data)
1199 unix_data.gid = gid;
1200 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1201 unix_data.mode = mode;
1202 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1204 sizeof(struct wimlib_unix_data),
1206 if (ret == 0 && have_unix_data)
1207 inode_remove_ads(inode, stream_idx, lookup_table);
1210 #endif /* !__WIN32__ */
1213 replace_forbidden_characters(utf16lechar *name)
1217 for (p = name; *p; p++) {
1219 if (wcschr(L"<>:\"/\\|?*", (wchar_t)*p))
1225 WARNING("File, directory, or stream name \"%"WS"\"\n"
1226 " contains forbidden characters; "
1227 "replacing them with Unicode codepoint U+001A",
1237 * Reads the alternate data stream entries of a WIM dentry.
1239 * @p: Pointer to buffer that starts with the first alternate stream entry.
1241 * @inode: Inode to load the alternate data streams into.
1242 * @inode->i_num_ads must have been set to the number of
1243 * alternate data streams that are expected.
1245 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1248 * The format of the on-disk alternate stream entries is as follows:
1250 * struct wim_ads_entry_on_disk {
1251 * u64 length; // Length of the entry, in bytes. This includes
1252 * all fields (including the stream name and
1253 * null terminator if present, AND the padding!).
1254 * u64 reserved; // Seems to be unused
1255 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
1256 * u16 stream_name_len; // Length of the stream name, in bytes
1257 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
1258 * not including null terminator
1259 * u16 zero; // UTF-16 null terminator for the stream name, NOT
1260 * included in @stream_name_len. Based on what
1261 * I've observed from filenames in dentries,
1262 * this field should not exist when
1263 * (@stream_name_len == 0), but you can't
1264 * actually tell because of the padding anyway
1265 * (provided that the padding is zeroed, which
1266 * it always seems to be).
1267 * char padding[]; // Padding to make the size a multiple of 8 bytes.
1270 * In addition, the entries are 8-byte aligned.
1272 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1273 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1274 * failure, @inode is not modified.
1277 read_ads_entries(const u8 *p, struct wim_inode *inode, u64 remaining_size)
1280 struct wim_ads_entry *ads_entries;
1283 num_ads = inode->i_num_ads;
1284 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1286 ERROR("Could not allocate memory for %"PRIu16" "
1287 "alternate data stream entries", num_ads);
1288 return WIMLIB_ERR_NOMEM;
1291 for (u16 i = 0; i < num_ads; i++) {
1292 struct wim_ads_entry *cur_entry;
1294 u64 length_no_padding;
1296 const u8 *p_save = p;
1298 cur_entry = &ads_entries[i];
1301 ads_entries[i].stream_id = i + 1;
1304 /* Read the base stream entry, excluding the stream name. */
1305 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1306 ERROR("Stream entries go past end of metadata resource");
1307 ERROR("(remaining_size = %"PRIu64")", remaining_size);
1308 ret = WIMLIB_ERR_INVALID_DENTRY;
1309 goto out_free_ads_entries;
1312 p = get_u64(p, &length);
1313 p += 8; /* Skip the reserved field */
1314 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
1315 p = get_u16(p, &cur_entry->stream_name_nbytes);
1317 cur_entry->stream_name = NULL;
1319 /* Length including neither the null terminator nor the padding
1321 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1322 cur_entry->stream_name_nbytes;
1324 /* Length including the null terminator and the padding */
1325 total_length = ((length_no_padding + 2) + 7) & ~7;
1327 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1329 if (remaining_size < length_no_padding) {
1330 ERROR("Stream entries go past end of metadata resource");
1331 ERROR("(remaining_size = %"PRIu64" bytes, "
1332 "length_no_padding = %"PRIu64" bytes)",
1333 remaining_size, length_no_padding);
1334 ret = WIMLIB_ERR_INVALID_DENTRY;
1335 goto out_free_ads_entries;
1338 /* The @length field in the on-disk ADS entry is expected to be
1339 * equal to @total_length, which includes all of the entry and
1340 * the padding that follows it to align the next ADS entry to an
1341 * 8-byte boundary. However, to be safe, we'll accept the
1342 * length field as long as it's not less than the un-padded
1343 * total length and not more than the padded total length. */
1344 if (length < length_no_padding || length > total_length) {
1345 ERROR("Stream entry has unexpected length "
1346 "field (length field = %"PRIu64", "
1347 "unpadded total length = %"PRIu64", "
1348 "padded total length = %"PRIu64")",
1349 length, length_no_padding, total_length);
1350 ret = WIMLIB_ERR_INVALID_DENTRY;
1351 goto out_free_ads_entries;
1354 if (cur_entry->stream_name_nbytes) {
1355 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1356 if (!cur_entry->stream_name) {
1357 ret = WIMLIB_ERR_NOMEM;
1358 goto out_free_ads_entries;
1360 get_bytes(p, cur_entry->stream_name_nbytes,
1361 cur_entry->stream_name);
1362 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = 0;
1363 replace_forbidden_characters(cur_entry->stream_name);
1365 /* It's expected that the size of every ADS entry is a multiple
1366 * of 8. However, to be safe, I'm allowing the possibility of
1367 * an ADS entry at the very end of the metadata resource ending
1368 * un-aligned. So although we still need to increment the input
1369 * pointer by @total_length to reach the next ADS entry, it's
1370 * possible that less than @total_length is actually remaining
1371 * in the metadata resource. We should set the remaining size to
1372 * 0 bytes if this happens. */
1373 p = p_save + total_length;
1374 if (remaining_size < total_length)
1377 remaining_size -= total_length;
1379 inode->i_ads_entries = ads_entries;
1381 inode->i_next_stream_id = inode->i_num_ads + 1;
1384 out_free_ads_entries:
1385 for (u16 i = 0; i < num_ads; i++)
1386 destroy_ads_entry(&ads_entries[i]);
1392 * Reads a WIM directory entry, including all alternate data stream entries that
1393 * follow it, from the WIM image's metadata resource.
1395 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1396 * @metadata_resource_len: Length of the metadata resource.
1397 * @offset: Offset of this directory entry in the metadata resource.
1398 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1400 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1401 * been modified, but it will not be left with pointers to any allocated
1402 * buffers. On success, the dentry->length field must be examined. If zero,
1403 * this was a special "end of directory" dentry and not a real dentry. If
1404 * nonzero, this was a real dentry.
1407 read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1408 u64 offset, struct wim_dentry *dentry)
1411 u64 calculated_size;
1412 utf16lechar *file_name = NULL;
1413 utf16lechar *short_name = NULL;
1414 u16 short_name_nbytes;
1415 u16 file_name_nbytes;
1417 struct wim_inode *inode = NULL;
1419 dentry_common_init(dentry);
1421 /*Make sure the dentry really fits into the metadata resource.*/
1422 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1423 ERROR("Directory entry starting at %"PRIu64" ends past the "
1424 "end of the metadata resource (size %"PRIu64")",
1425 offset, metadata_resource_len);
1426 return WIMLIB_ERR_INVALID_DENTRY;
1429 /* Before reading the whole dentry, we need to read just the length.
1430 * This is because a dentry of length 8 (that is, just the length field)
1431 * terminates the list of sibling directory entries. */
1433 p = get_u64(&metadata_resource[offset], &dentry->length);
1435 /* A zero length field (really a length of 8, since that's how big the
1436 * directory entry is...) indicates that this is the end of directory
1437 * dentry. We do not read it into memory as an actual dentry, so just
1438 * return successfully in that case. */
1439 if (dentry->length == 0)
1442 /* If the dentry does not overflow the metadata resource buffer and is
1443 * not too short, read the rest of it (excluding the alternate data
1444 * streams, but including the file name and short name variable-length
1445 * fields) into memory. */
1446 if (offset + dentry->length >= metadata_resource_len
1447 || offset + dentry->length < offset)
1449 ERROR("Directory entry at offset %"PRIu64" and with size "
1450 "%"PRIu64" ends past the end of the metadata resource "
1452 offset, dentry->length, metadata_resource_len);
1453 return WIMLIB_ERR_INVALID_DENTRY;
1456 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1457 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1459 return WIMLIB_ERR_INVALID_DENTRY;
1462 inode = new_timeless_inode();
1464 return WIMLIB_ERR_NOMEM;
1466 p = get_u32(p, &inode->i_attributes);
1467 p = get_u32(p, (u32*)&inode->i_security_id);
1468 p = get_u64(p, &dentry->subdir_offset);
1470 /* 2 unused fields */
1471 p += 2 * sizeof(u64);
1472 /*p = get_u64(p, &dentry->unused1);*/
1473 /*p = get_u64(p, &dentry->unused2);*/
1475 p = get_u64(p, &inode->i_creation_time);
1476 p = get_u64(p, &inode->i_last_access_time);
1477 p = get_u64(p, &inode->i_last_write_time);
1479 p = get_bytes(p, SHA1_HASH_SIZE, inode->i_hash);
1481 /* I don't know what's going on here. It seems like M$ screwed up the
1482 * reparse points, then put the fields in the same place and didn't
1484 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1486 p = get_u32(p, &inode->i_reparse_tag);
1488 p = get_u16(p, &inode->i_not_rpfixed);
1491 /* i_reparse_tag is irrelevant; just leave it at 0. */
1492 p = get_u64(p, &inode->i_ino);
1495 /* By the way, the reparse_reserved field does not actually exist (at
1496 * least when the file is not a reparse point) */
1498 p = get_u16(p, &inode->i_num_ads);
1500 p = get_u16(p, &short_name_nbytes);
1501 p = get_u16(p, &file_name_nbytes);
1503 /* We now know the length of the file name and short name. Make sure
1504 * the length of the dentry is large enough to actually hold them.
1506 * The calculated length here is unaligned to allow for the possibility
1507 * that the dentry->length names an unaligned length, although this
1508 * would be unexpected. */
1509 calculated_size = __dentry_correct_length_unaligned(file_name_nbytes,
1512 if (dentry->length < calculated_size) {
1513 ERROR("Unexpected end of directory entry! (Expected "
1514 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1515 "short_name_nbytes = %hu, file_name_nbytes = %hu)",
1516 calculated_size, dentry->length,
1517 short_name_nbytes, file_name_nbytes);
1518 ret = WIMLIB_ERR_INVALID_DENTRY;
1519 goto out_free_inode;
1522 /* Read the filename if present. Note: if the filename is empty, there
1523 * is no null terminator following it. */
1524 if (file_name_nbytes) {
1525 file_name = MALLOC(file_name_nbytes + 2);
1527 ERROR("Failed to allocate %d bytes for dentry file name",
1528 file_name_nbytes + 2);
1529 ret = WIMLIB_ERR_NOMEM;
1530 goto out_free_inode;
1532 p = get_bytes(p, file_name_nbytes + 2, file_name);
1533 if (file_name[file_name_nbytes / 2] != 0) {
1534 file_name[file_name_nbytes / 2] = 0;
1535 WARNING("File name in WIM dentry \"%"WS"\" is not "
1536 "null-terminated!", file_name);
1538 replace_forbidden_characters(file_name);
1541 /* Align the calculated size */
1542 calculated_size = (calculated_size + 7) & ~7;
1544 if (dentry->length > calculated_size) {
1545 /* Weird; the dentry says it's longer than it should be. Note
1546 * that the length field does NOT include the size of the
1547 * alternate stream entries. */
1549 /* Strangely, some directory entries inexplicably have a little
1550 * over 70 bytes of extra data. The exact amount of data seems
1551 * to be 72 bytes, but it is aligned on the next 8-byte
1552 * boundary. It does NOT seem to be alternate data stream
1553 * entries. Here's an example of the aligned data:
1555 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1556 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1557 * 00000000 00000000 00000000 00000000
1559 * Here's one interpretation of how the data is laid out.
1562 * u32 field1; (always 0x00000001)
1563 * u32 field2; (always 0x40000000)
1564 * u8 data[48]; (???)
1565 * u64 reserved1; (always 0)
1566 * u64 reserved2; (always 0)
1568 /*DEBUG("Dentry for file or directory `%"WS"' has %"PRIu64" "*/
1569 /*"extra bytes of data", file_name,*/
1570 /*dentry->length - calculated_size);*/
1573 /* Read the short filename if present. Note: if there is no short
1574 * filename, there is no null terminator following it. */
1575 if (short_name_nbytes) {
1576 short_name = MALLOC(short_name_nbytes + 2);
1578 ERROR("Failed to allocate %d bytes for dentry short name",
1579 short_name_nbytes + 2);
1580 ret = WIMLIB_ERR_NOMEM;
1581 goto out_free_file_name;
1583 p = get_bytes(p, short_name_nbytes + 2, short_name);
1584 if (short_name[short_name_nbytes / 2] != 0) {
1585 short_name[short_name_nbytes / 2] = 0;
1586 WARNING("Short name in WIM dentry \"%"WS"\" is not "
1587 "null-terminated!", file_name);
1589 replace_forbidden_characters(short_name);
1593 * Read the alternate data streams, if present. dentry->num_ads tells
1594 * us how many they are, and they will directly follow the dentry
1597 * Note that each alternate data stream entry begins on an 8-byte
1598 * aligned boundary, and the alternate data stream entries are NOT
1599 * included in the dentry->length field for some reason.
1601 if (inode->i_num_ads != 0) {
1603 /* Trying different lengths is just a hack to make sure we have
1604 * a chance of reading the ADS entries correctly despite the
1605 * poor documentation. */
1607 if (calculated_size != dentry->length) {
1608 WARNING("Trying calculated dentry length (%"PRIu64") "
1609 "instead of dentry->length field (%"PRIu64") "
1610 "to read ADS entries",
1611 calculated_size, dentry->length);
1613 u64 lengths_to_try[3] = {calculated_size,
1614 (dentry->length + 7) & ~7,
1616 ret = WIMLIB_ERR_INVALID_DENTRY;
1617 for (size_t i = 0; i < ARRAY_LEN(lengths_to_try); i++) {
1618 if (lengths_to_try[i] > metadata_resource_len - offset)
1620 ret = read_ads_entries(&metadata_resource[offset + lengths_to_try[i]],
1622 metadata_resource_len - offset - lengths_to_try[i]);
1626 ERROR("Failed to read alternate data stream "
1627 "entries of WIM dentry \"%"WS"\"", file_name);
1628 goto out_free_short_name;
1631 /* We've read all the data for this dentry. Set the names and their
1632 * lengths, and we've done. */
1633 dentry->d_inode = inode;
1634 dentry->file_name = file_name;
1635 dentry->short_name = short_name;
1636 dentry->file_name_nbytes = file_name_nbytes;
1637 dentry->short_name_nbytes = short_name_nbytes;
1639 out_free_short_name:
1648 /* Reads the children of a dentry, and all their children, ..., etc. from the
1649 * metadata resource and into the dentry tree.
1651 * @metadata_resource: An array that contains the uncompressed metadata
1652 * resource for the WIM file.
1654 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1657 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
1658 * tree and has already been read from the metadata resource. It
1659 * does not need to be the real root because this procedure is
1660 * called recursively.
1662 * Returns zero on success; nonzero on failure.
1665 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1666 struct wim_dentry *dentry)
1668 u64 cur_offset = dentry->subdir_offset;
1669 struct wim_dentry *child;
1670 struct wim_dentry cur_child;
1674 * If @dentry has no child dentries, nothing more needs to be done for
1675 * this branch. This is the case for regular files, symbolic links, and
1676 * *possibly* empty directories (although an empty directory may also
1677 * have one child dentry that is the special end-of-directory dentry)
1679 if (cur_offset == 0)
1682 /* Find and read all the children of @dentry. */
1685 /* Read next child of @dentry into @cur_child. */
1686 ret = read_dentry(metadata_resource, metadata_resource_len,
1687 cur_offset, &cur_child);
1691 /* Check for end of directory. */
1692 if (cur_child.length == 0)
1695 /* Not end of directory. Allocate this child permanently and
1696 * link it to the parent and previous child. */
1697 child = MALLOC(sizeof(struct wim_dentry));
1699 ERROR("Failed to allocate %zu bytes for new dentry",
1700 sizeof(struct wim_dentry));
1701 ret = WIMLIB_ERR_NOMEM;
1704 memcpy(child, &cur_child, sizeof(struct wim_dentry));
1705 dentry_add_child(dentry, child);
1706 inode_add_dentry(child, child->d_inode);
1708 /* If there are children of this child, call this procedure
1710 if (child->subdir_offset != 0) {
1711 ret = read_dentry_tree(metadata_resource,
1712 metadata_resource_len, child);
1717 /* Advance to the offset of the next child. Note: We need to
1718 * advance by the TOTAL length of the dentry, not by the length
1719 * child->length, which although it does take into account the
1720 * padding, it DOES NOT take into account alternate stream
1722 cur_offset += dentry_total_length(child);
1728 * Writes a WIM dentry to an output buffer.
1730 * @dentry: The dentry structure.
1731 * @p: The memory location to write the data to.
1732 * @return: Pointer to the byte after the last byte we wrote as part of the
1736 write_dentry(const struct wim_dentry *dentry, u8 *p)
1740 const struct wim_inode *inode = dentry->d_inode;
1742 /* We calculate the correct length of the dentry ourselves because the
1743 * dentry->length field may been set to an unexpected value from when we
1744 * read the dentry in (for example, there may have been unknown data
1745 * appended to the end of the dentry...) */
1746 u64 length = dentry_correct_length(dentry);
1748 p = put_u64(p, length);
1749 p = put_u32(p, inode->i_attributes);
1750 p = put_u32(p, inode->i_security_id);
1751 p = put_u64(p, dentry->subdir_offset);
1752 p = put_u64(p, 0); /* unused1 */
1753 p = put_u64(p, 0); /* unused2 */
1754 p = put_u64(p, inode->i_creation_time);
1755 p = put_u64(p, inode->i_last_access_time);
1756 p = put_u64(p, inode->i_last_write_time);
1757 hash = inode_stream_hash(inode, 0);
1758 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1759 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1761 p = put_u32(p, inode->i_reparse_tag);
1763 p = put_u16(p, inode->i_not_rpfixed);
1767 if (inode->i_nlink == 1)
1770 link_group_id = inode->i_ino;
1771 p = put_u64(p, link_group_id);
1773 p = put_u16(p, inode->i_num_ads);
1774 p = put_u16(p, dentry->short_name_nbytes);
1775 p = put_u16(p, dentry->file_name_nbytes);
1776 if (dentry_has_long_name(dentry)) {
1777 p = put_bytes(p, dentry->file_name_nbytes + 2,
1780 if (dentry_has_short_name(dentry)) {
1781 p = put_bytes(p, dentry->short_name_nbytes + 2,
1782 dentry->short_name);
1785 /* Align to 8-byte boundary */
1786 wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1787 p = put_zeroes(p, length - (p - orig_p));
1789 /* Write the alternate data streams, if there are any. Please see
1790 * read_ads_entries() for comments about the format of the on-disk
1791 * alternate data stream entries. */
1792 for (u16 i = 0; i < inode->i_num_ads; i++) {
1793 p = put_u64(p, ads_entry_total_length(&inode->i_ads_entries[i]));
1794 p = put_u64(p, 0); /* Unused */
1795 hash = inode_stream_hash(inode, i + 1);
1796 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1797 p = put_u16(p, inode->i_ads_entries[i].stream_name_nbytes);
1798 if (inode->i_ads_entries[i].stream_name_nbytes) {
1800 inode->i_ads_entries[i].stream_name_nbytes + 2,
1801 inode->i_ads_entries[i].stream_name);
1803 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1805 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1810 write_dentry_cb(struct wim_dentry *dentry, void *_p)
1813 *p = write_dentry(dentry, *p);
1818 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
1821 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
1824 *p = write_dentry_tree_recursive(dentry, *p);
1828 /* Recursive function that writes a dentry tree rooted at @parent, not including
1829 * @parent itself, which has already been written. */
1831 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
1833 /* Nothing to do if this dentry has no children. */
1834 if (parent->subdir_offset == 0)
1837 /* Write child dentries and end-of-directory entry.
1839 * Note: we need to write all of this dentry's children before
1840 * recursively writing the directory trees rooted at each of the child
1841 * dentries, since the on-disk dentries for a dentry's children are
1842 * always located at consecutive positions in the metadata resource! */
1843 for_dentry_child(parent, write_dentry_cb, &p);
1845 /* write end of directory entry */
1848 /* Recurse on children. */
1849 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
1853 /* Writes a directory tree to the metadata resource.
1855 * @root: Root of the dentry tree.
1856 * @p: Pointer to a buffer with enough space for the dentry tree.
1858 * Returns pointer to the byte after the last byte we wrote.
1861 write_dentry_tree(const struct wim_dentry *root, u8 *p)
1863 DEBUG("Writing dentry tree.");
1864 wimlib_assert(dentry_is_root(root));
1866 /* If we're the root dentry, we have no parent that already
1867 * wrote us, so we need to write ourselves. */
1868 p = write_dentry(root, p);
1870 /* Write end of directory entry after the root dentry just to be safe;
1871 * however the root dentry obviously cannot have any siblings. */
1874 /* Recursively write the rest of the dentry tree. */
1875 return write_dentry_tree_recursive(root, p);