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/.
34 #include "wimlib/dentry.h"
35 #include "wimlib/encoding.h"
36 #include "wimlib/endianness.h"
37 #include "wimlib/error.h"
38 #include "wimlib/lookup_table.h"
39 #include "wimlib/metadata.h"
40 #include "wimlib/resource.h"
41 #include "wimlib/security.h"
42 #include "wimlib/sha1.h"
43 #include "wimlib/timestamp.h"
47 /* WIM alternate data stream entry (on-disk format) */
48 struct wim_ads_entry_on_disk {
49 /* Length of the entry, in bytes. This apparently includes all
50 * fixed-length fields, plus the stream name and null terminator if
51 * present, and the padding up to an 8 byte boundary. wimlib is a
52 * little less strict when reading the entries, and only requires that
53 * the number of bytes from this field is at least as large as the size
54 * of the fixed length fields and stream name without null terminator.
60 /* SHA1 message digest of the uncompressed stream; or, alternatively,
61 * can be all zeroes if the stream has zero length. */
62 u8 hash[SHA1_HASH_SIZE];
64 /* Length of the stream name, in bytes. 0 if the stream is unnamed. */
65 le16 stream_name_nbytes;
67 /* Stream name in UTF-16LE. It is @stream_name_nbytes bytes long,
68 * excluding the the null terminator. There is a null terminator
69 * character if @stream_name_nbytes != 0; i.e., if this stream is named.
71 utf16lechar stream_name[];
74 #define WIM_ADS_ENTRY_DISK_SIZE 38
76 /* On-disk format of a WIM dentry (directory entry), located in the metadata
77 * resource for a WIM image. */
78 struct wim_dentry_on_disk {
80 /* Length of this directory entry in bytes, not including any alternate
81 * data stream entries. Should be a multiple of 8 so that the following
82 * dentry or alternate data stream entry is aligned on an 8-byte
83 * boundary. (If not, wimlib will round it up.) It must be at least as
84 * long as the fixed-length fields of the dentry (WIM_DENTRY_DISK_SIZE),
85 * plus the lengths of the file name and/or short name if present.
87 * It is also possible for this field to be 0. This situation, which is
88 * undocumented, indicates the end of a list of sibling nodes in a
89 * directory. It also means the real length is 8, because the dentry
90 * included only the length field, but that takes up 8 bytes. */
93 /* Attributes of the file or directory. This is a bitwise OR of the
94 * FILE_ATTRIBUTE_* constants and should correspond to the value
95 * retrieved by GetFileAttributes() on Windows. */
98 /* A value that specifies the security descriptor for this file or
99 * directory. If -1, the file or directory has no security descriptor.
100 * Otherwise, it is a 0-based index into the WIM image's table of
101 * security descriptors (see: `struct wim_security_data') */
104 /* Offset, in bytes, from the start of the uncompressed metadata
105 * resource of this directory's child directory entries, or 0 if this
106 * directory entry does not correspond to a directory or otherwise does
107 * not have any children. */
110 /* Reserved fields */
115 /* Creation time, last access time, and last write time, in
116 * 100-nanosecond intervals since 12:00 a.m UTC January 1, 1601. They
117 * should correspond to the times gotten by calling GetFileTime() on
120 le64 last_access_time;
121 le64 last_write_time;
123 /* Vaguely, the SHA-1 message digest ("hash") of the file's contents.
124 * More specifically, this is for the "unnamed data stream" rather than
125 * any "alternate data streams". This hash value is used to look up the
126 * corresponding entry in the WIM's stream lookup table to actually find
127 * the file contents within the WIM.
129 * If the file has no unnamed data stream (e.g. is a directory), then
130 * this field will be all zeroes. If the unnamed data stream is empty
131 * (i.e. an "empty file"), then this field is also expected to be all
132 * zeroes. (It will be if wimlib created the WIM image, at least;
133 * otherwise it can't be ruled out that the SHA-1 message digest of 0
134 * bytes of data is given explicitly.)
136 * If the file has reparse data, then this field will instead specify
137 * the SHA-1 message digest of the reparse data. If it is somehow
138 * possible for a file to have both an unnamed data stream and reparse
139 * data, then this is not handled by wimlib.
141 * As a further special case, if this field is all zeroes but there is
142 * an alternate data stream entry with no name and a nonzero SHA-1
143 * message digest field, then that hash must be used instead of this
144 * one. In fact, when named data streams are present, some versions of
145 * Windows PE contain a bug where they only look in the alternate data
146 * stream entries for the unnamed data stream, not here.
148 u8 unnamed_stream_hash[SHA1_HASH_SIZE];
150 /* The format of the following data is not yet completely known and they
151 * do not correspond to Microsoft's documentation.
153 * If this directory entry is for a reparse point (has
154 * FILE_ATTRIBUTE_REPARSE_POINT set in the attributes field), then the
155 * version of the following fields containing the reparse tag is valid.
156 * Furthermore, the field notated as not_rpfixed, as far as I can tell,
157 * is supposed to be set to 1 if reparse point fixups (a.k.a. fixing the
158 * targets of absolute symbolic links) were *not* done, and otherwise 0.
160 * If this directory entry is not for a reparse point, then the version
161 * of the following fields containing the hard_link_group_id is valid.
162 * All MS says about this field is that "If this file is part of a hard
163 * link set, all the directory entries in the set will share the same
164 * value in this field.". However, more specifically I have observed
166 * - If the file is part of a hard link set of size 1, then the
167 * hard_link_group_id should be set to either 0, which is treated
168 * specially as indicating "not hardlinked", or any unique value.
169 * - The specific nonzero values used to identity hard link sets do
170 * not matter, as long as they are unique.
171 * - However, due to bugs in Microsoft's software, it is actually NOT
172 * guaranteed that directory entries that share the same hard link
173 * group ID are actually hard linked to each either. We have to
174 * handle this by using special code to use distinguishing features
175 * (which is possible because some information about the underlying
176 * inode is repeated in each dentry) to split up these fake hard link
177 * groups into what they actually are supposed to be.
185 } _packed_attribute reparse;
188 le64 hard_link_group_id;
189 } _packed_attribute nonreparse;
192 /* Number of alternate data stream entries that directly follow this
194 le16 num_alternate_data_streams;
196 /* Length of this file's UTF-16LE encoded short name (8.3 DOS-compatible
197 * name), if present, in bytes, excluding the null terminator. If this
198 * file has no short name, then this field should be 0. */
199 le16 short_name_nbytes;
201 /* Length of this file's UTF-16LE encoded "long" name, excluding the
202 * null terminator. If this file has no short name, then this field
203 * should be 0. It's expected that only the root dentry has this field
205 le16 file_name_nbytes;
207 /* Followed by variable length file name, in UTF16-LE, if
208 * file_name_nbytes != 0. Includes null terminator. */
209 /*utf16lechar file_name[];*/
211 /* Followed by variable length short name, in UTF16-LE, if
212 * short_name_nbytes != 0. Includes null terminator. */
213 /*utf16lechar short_name[];*/
216 #define WIM_DENTRY_DISK_SIZE 102
218 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
219 * a file name and short name that take the specified numbers of bytes. This
220 * excludes any alternate data stream entries that may follow the dentry. */
222 dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
224 u64 length = sizeof(struct wim_dentry_on_disk);
225 if (file_name_nbytes)
226 length += file_name_nbytes + 2;
227 if (short_name_nbytes)
228 length += short_name_nbytes + 2;
232 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
233 * the file name length and short name length. Note that dentry->length is
234 * ignored; also, this excludes any alternate data stream entries that may
235 * follow the dentry. */
237 dentry_correct_length_aligned(const struct wim_dentry *dentry)
241 len = dentry_correct_length_unaligned(dentry->file_name_nbytes,
242 dentry->short_name_nbytes);
243 return (len + 7) & ~7;
246 /* Duplicates a string of system-dependent encoding into a UTF-16LE string and
247 * returns the string and its length, in bytes, in the pointer arguments. Frees
248 * any existing string at the return location before overwriting it. */
250 get_utf16le_name(const tchar *name, utf16lechar **name_utf16le_ret,
251 u16 *name_utf16le_nbytes_ret)
253 utf16lechar *name_utf16le;
254 size_t name_utf16le_nbytes;
257 name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
258 name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
259 if (name_utf16le == NULL)
260 return WIMLIB_ERR_NOMEM;
261 memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
265 ret = tstr_to_utf16le(name, tstrlen(name), &name_utf16le,
266 &name_utf16le_nbytes);
268 if (name_utf16le_nbytes > 0xffff) {
270 ERROR("Multibyte string \"%"TS"\" is too long!", name);
271 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
276 FREE(*name_utf16le_ret);
277 *name_utf16le_ret = name_utf16le;
278 *name_utf16le_nbytes_ret = name_utf16le_nbytes;
283 /* Sets the name of a WIM dentry from a multibyte string. */
285 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name)
288 ret = get_utf16le_name(new_name, &dentry->file_name,
289 &dentry->file_name_nbytes);
291 /* Clear the short name and recalculate the dentry length */
292 if (dentry_has_short_name(dentry)) {
293 FREE(dentry->short_name);
294 dentry->short_name = NULL;
295 dentry->short_name_nbytes = 0;
301 /* Returns the total length of a WIM alternate data stream entry on-disk,
302 * including the stream name, the null terminator, AND the padding after the
303 * entry to align the next ADS entry or dentry on an 8-byte boundary. */
305 ads_entry_total_length(const struct wim_ads_entry *entry)
307 u64 len = sizeof(struct wim_ads_entry_on_disk);
308 if (entry->stream_name_nbytes)
309 len += entry->stream_name_nbytes + 2;
310 return (len + 7) & ~7;
314 * Determine whether to include a "dummy" stream when writing a WIM dentry:
316 * Some versions of Microsoft's WIM software (the boot driver(s) in WinPE 3.0,
317 * for example) contain a bug where they assume the first alternate data stream
318 * (ADS) entry of a dentry with a nonzero ADS count specifies the unnamed
319 * stream, even if it has a name and the unnamed stream is already specified in
320 * the hash field of the dentry itself.
322 * wimlib has to work around this behavior by carefully emulating the behavior
323 * of (most versions of) ImageX/WIMGAPI, which move the unnamed stream reference
324 * into the alternate stream entries whenever there are named data streams, even
325 * though there is already a field in the dentry itself for the unnamed stream
326 * reference, which then goes to waste.
328 static inline bool inode_needs_dummy_stream(const struct wim_inode *inode)
330 return (inode->i_num_ads > 0 &&
331 inode->i_num_ads < 0xffff && /* overflow check */
332 inode->i_canonical_streams); /* assume the dentry is okay if it
333 already had an unnamed ADS entry
334 when it was read in */
337 /* Calculate the total number of bytes that will be consumed when a WIM dentry
338 * is written. This includes base dentry and name fields as well as all
339 * alternate data stream entries and alignment bytes. */
341 dentry_out_total_length(const struct wim_dentry *dentry)
343 u64 length = dentry_correct_length_aligned(dentry);
344 const struct wim_inode *inode = dentry->d_inode;
346 if (inode_needs_dummy_stream(inode))
347 length += ads_entry_total_length(&(struct wim_ads_entry){});
349 for (u16 i = 0; i < inode->i_num_ads; i++)
350 length += ads_entry_total_length(&inode->i_ads_entries[i]);
355 /* Calculate the aligned, total length of a dentry, including all alternate data
356 * stream entries. Uses dentry->length. */
358 dentry_in_total_length(const struct wim_dentry *dentry)
360 u64 length = dentry->length;
361 const struct wim_inode *inode = dentry->d_inode;
362 for (u16 i = 0; i < inode->i_num_ads; i++)
363 length += ads_entry_total_length(&inode->i_ads_entries[i]);
364 return (length + 7) & ~7;
368 for_dentry_in_rbtree(struct rb_node *root,
369 int (*visitor)(struct wim_dentry *, void *),
373 struct rb_node *node = root;
377 list_add(&rbnode_dentry(node)->tmp_list, &stack);
378 node = node->rb_left;
380 struct list_head *next;
381 struct wim_dentry *dentry;
386 dentry = container_of(next, struct wim_dentry, tmp_list);
388 ret = visitor(dentry, arg);
391 node = dentry->rb_node.rb_right;
397 for_dentry_tree_in_rbtree_depth(struct rb_node *node,
398 int (*visitor)(struct wim_dentry*, void*),
403 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
407 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
411 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
419 for_dentry_tree_in_rbtree(struct rb_node *node,
420 int (*visitor)(struct wim_dentry*, void*),
425 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
428 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
431 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
439 * Iterate over all children of @dentry, calling the function @visitor, passing
440 * it a child dentry and the extra argument @arg.
442 * Note: this function iterates over ALL child dentries, even those with the
443 * same case-insensitive name.
445 * Note: this function clobbers the tmp_list field of the child dentries. */
447 for_dentry_child(const struct wim_dentry *dentry,
448 int (*visitor)(struct wim_dentry *, void *),
451 return for_dentry_in_rbtree(dentry->d_inode->i_children.rb_node,
456 /* Calls a function on all directory entries in a WIM dentry tree. Logically,
457 * this is a pre-order traversal (the function is called on a parent dentry
458 * before its children), but sibling dentries will be visited in order as well.
461 for_dentry_in_tree(struct wim_dentry *root,
462 int (*visitor)(struct wim_dentry*, void*), void *arg)
468 ret = (*visitor)(root, arg);
471 return for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node,
476 /* Like for_dentry_in_tree(), but the visitor function is always called on a
477 * dentry's children before on itself. */
479 for_dentry_in_tree_depth(struct wim_dentry *root,
480 int (*visitor)(struct wim_dentry*, void*), void *arg)
486 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
490 return (*visitor)(root, arg);
493 /* Calculate the full path of @dentry. The full path of its parent must have
494 * already been calculated, or it must be the root dentry. */
496 calculate_dentry_full_path(struct wim_dentry *dentry)
499 u32 full_path_nbytes;
502 if (dentry->_full_path)
505 if (dentry_is_root(dentry)) {
506 static const tchar _root_path[] = {WIM_PATH_SEPARATOR, T('\0')};
507 full_path = TSTRDUP(_root_path);
508 if (full_path == NULL)
509 return WIMLIB_ERR_NOMEM;
510 full_path_nbytes = 1 * sizeof(tchar);
512 struct wim_dentry *parent;
513 tchar *parent_full_path;
514 u32 parent_full_path_nbytes;
515 size_t filename_nbytes;
517 parent = dentry->parent;
518 if (dentry_is_root(parent)) {
519 parent_full_path = T("");
520 parent_full_path_nbytes = 0;
522 if (parent->_full_path == NULL) {
523 ret = calculate_dentry_full_path(parent);
527 parent_full_path = parent->_full_path;
528 parent_full_path_nbytes = parent->full_path_nbytes;
531 /* Append this dentry's name as a tchar string to the full path
532 * of the parent followed by the path separator */
534 filename_nbytes = dentry->file_name_nbytes;
537 int ret = utf16le_to_tstr_nbytes(dentry->file_name,
538 dentry->file_name_nbytes,
545 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
547 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
548 if (full_path == NULL)
549 return WIMLIB_ERR_NOMEM;
550 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
551 full_path[parent_full_path_nbytes / sizeof(tchar)] = WIM_PATH_SEPARATOR;
553 memcpy(&full_path[parent_full_path_nbytes / sizeof(tchar) + 1],
555 filename_nbytes + sizeof(tchar));
557 utf16le_to_tstr_buf(dentry->file_name,
558 dentry->file_name_nbytes,
559 &full_path[parent_full_path_nbytes /
563 dentry->_full_path = full_path;
564 dentry->full_path_nbytes= full_path_nbytes;
569 do_calculate_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
571 return calculate_dentry_full_path(dentry);
575 calculate_dentry_tree_full_paths(struct wim_dentry *root)
577 return for_dentry_in_tree(root, do_calculate_dentry_full_path, NULL);
581 dentry_full_path(struct wim_dentry *dentry)
583 calculate_dentry_full_path(dentry);
584 return dentry->_full_path;
588 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
590 *(u64*)subdir_offset_p += dentry_out_total_length(dentry);
595 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
597 calculate_subdir_offsets(dentry, subdir_offset_p);
602 * Recursively calculates the subdir offsets for a directory tree.
604 * @dentry: The root of the directory tree.
605 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
606 * offset for @dentry.
609 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
611 struct rb_node *node;
613 dentry->subdir_offset = *subdir_offset_p;
614 node = dentry->d_inode->i_children.rb_node;
616 /* Advance the subdir offset by the amount of space the children
617 * of this dentry take up. */
618 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
620 /* End-of-directory dentry on disk. */
621 *subdir_offset_p += 8;
623 /* Recursively call calculate_subdir_offsets() on all the
625 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
627 /* On disk, childless directories have a valid subdir_offset
628 * that points to an 8-byte end-of-directory dentry. Regular
629 * files or reparse points have a subdir_offset of 0. */
630 if (dentry_is_directory(dentry))
631 *subdir_offset_p += 8;
633 dentry->subdir_offset = 0;
637 /* Case-sensitive UTF-16LE dentry or stream name comparison. Used on both UNIX
638 * (always) and Windows (sometimes) */
640 compare_utf16le_names_case_sensitive(const utf16lechar *name1, size_t nbytes1,
641 const utf16lechar *name2, size_t nbytes2)
643 /* Return the result if the strings differ up to their minimum length.
644 * Note that we cannot use strcmp() or strncmp() here, as the strings
645 * are in UTF-16LE format. */
646 int result = memcmp(name1, name2, min(nbytes1, nbytes2));
650 /* The strings are the same up to their minimum length, so return a
651 * result based on their lengths. */
652 if (nbytes1 < nbytes2)
654 else if (nbytes1 > nbytes2)
661 /* Windoze: Case-insensitive UTF-16LE dentry or stream name comparison */
663 compare_utf16le_names_case_insensitive(const utf16lechar *name1, size_t nbytes1,
664 const utf16lechar *name2, size_t nbytes2)
666 /* Return the result if the strings differ up to their minimum length.
668 int result = _wcsnicmp((const wchar_t*)name1, (const wchar_t*)name2,
669 min(nbytes1 / 2, nbytes2 / 2));
673 /* The strings are the same up to their minimum length, so return a
674 * result based on their lengths. */
675 if (nbytes1 < nbytes2)
677 else if (nbytes1 > nbytes2)
682 #endif /* __WIN32__ */
685 # define compare_utf16le_names compare_utf16le_names_case_insensitive
687 # define compare_utf16le_names compare_utf16le_names_case_sensitive
693 dentry_compare_names_case_insensitive(const struct wim_dentry *d1,
694 const struct wim_dentry *d2)
696 return compare_utf16le_names_case_insensitive(d1->file_name,
697 d1->file_name_nbytes,
699 d2->file_name_nbytes);
701 #endif /* __WIN32__ */
704 dentry_compare_names_case_sensitive(const struct wim_dentry *d1,
705 const struct wim_dentry *d2)
707 return compare_utf16le_names_case_sensitive(d1->file_name,
708 d1->file_name_nbytes,
710 d2->file_name_nbytes);
714 # define dentry_compare_names dentry_compare_names_case_insensitive
716 # define dentry_compare_names dentry_compare_names_case_sensitive
719 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
720 * stream name @name that has length @name_nbytes bytes. */
722 ads_entry_has_name(const struct wim_ads_entry *entry,
723 const utf16lechar *name, size_t name_nbytes)
725 return !compare_utf16le_names(name, name_nbytes,
727 entry->stream_name_nbytes);
730 /* Given a UTF-16LE filename and a directory, look up the dentry for the file.
731 * Return it if found, otherwise NULL. This is case-sensitive on UNIX and
732 * case-insensitive on Windows. */
734 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
735 const utf16lechar *name,
738 struct rb_node *node;
741 node = dentry->d_inode->i_children_case_insensitive.rb_node;
743 node = dentry->d_inode->i_children.rb_node;
746 struct wim_dentry *child;
749 child = rb_entry(node, struct wim_dentry, rb_node_case_insensitive);
751 child = rbnode_dentry(node);
753 int result = compare_utf16le_names(name, name_nbytes,
755 child->file_name_nbytes);
757 node = node->rb_left;
759 node = node->rb_right;
762 if (!list_empty(&child->case_insensitive_conflict_list))
764 WARNING("Result of case-insensitive lookup is ambiguous "
765 "(returning \"%ls\" instead of \"%ls\")",
767 container_of(child->case_insensitive_conflict_list.next,
769 case_insensitive_conflict_list)->file_name);
778 /* Returns the child of @dentry that has the file name @name. Returns NULL if
779 * no child has the name. */
781 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
784 return get_dentry_child_with_utf16le_name(dentry, name,
785 tstrlen(name) * sizeof(tchar));
787 utf16lechar *utf16le_name;
788 size_t utf16le_name_nbytes;
790 struct wim_dentry *child;
792 ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
793 &utf16le_name, &utf16le_name_nbytes);
797 child = get_dentry_child_with_utf16le_name(dentry,
799 utf16le_name_nbytes);
806 static struct wim_dentry *
807 get_dentry_utf16le(WIMStruct *wim, const utf16lechar *path)
809 struct wim_dentry *cur_dentry, *parent_dentry;
810 const utf16lechar *p, *pp;
812 cur_dentry = parent_dentry = wim_root_dentry(wim);
813 if (cur_dentry == NULL) {
819 while (*p == cpu_to_le16(WIM_PATH_SEPARATOR))
821 if (*p == cpu_to_le16('\0'))
824 while (*pp != cpu_to_le16(WIM_PATH_SEPARATOR) &&
825 *pp != cpu_to_le16('\0'))
828 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
829 (void*)pp - (void*)p);
830 if (cur_dentry == NULL)
833 parent_dentry = cur_dentry;
835 if (cur_dentry == NULL) {
836 if (dentry_is_directory(parent_dentry))
845 * Returns the dentry in the currently selected WIM image named by @path
846 * starting from the root of the WIM image, or NULL if there is no such dentry.
848 * On Windows, the search is done case-insensitively.
851 get_dentry(WIMStruct *wim, const tchar *path)
854 return get_dentry_utf16le(wim, path);
856 utf16lechar *path_utf16le;
857 size_t path_utf16le_nbytes;
859 struct wim_dentry *dentry;
861 ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
862 &path_utf16le, &path_utf16le_nbytes);
865 dentry = get_dentry_utf16le(wim, path_utf16le);
872 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
874 struct wim_dentry *dentry;
875 dentry = get_dentry(wim, path);
877 return dentry->d_inode;
882 /* Takes in a path of length @len in @buf, and transforms it into a string for
883 * the path of its parent directory. */
885 to_parent_name(tchar *buf, size_t len)
887 ssize_t i = (ssize_t)len - 1;
888 while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
890 while (i >= 0 && buf[i] != WIM_PATH_SEPARATOR)
892 while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
894 buf[i + 1] = T('\0');
897 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
898 * if the dentry is not found. */
900 get_parent_dentry(WIMStruct *wim, const tchar *path)
902 size_t path_len = tstrlen(path);
903 tchar buf[path_len + 1];
905 tmemcpy(buf, path, path_len + 1);
906 to_parent_name(buf, path_len);
907 return get_dentry(wim, buf);
910 /* Prints the full path of a dentry. */
912 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
914 int ret = calculate_dentry_full_path(dentry);
917 tprintf(T("%"TS"\n"), dentry->_full_path);
921 /* We want to be able to show the names of the file attribute flags that are
923 struct file_attr_flag {
927 struct file_attr_flag file_attr_flags[] = {
928 {FILE_ATTRIBUTE_READONLY, T("READONLY")},
929 {FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
930 {FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
931 {FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
932 {FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
933 {FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
934 {FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
935 {FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
936 {FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
937 {FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
938 {FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
939 {FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
940 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
941 {FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
942 {FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
945 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
946 * available. If the dentry is unresolved and the lookup table is NULL, the
947 * lookup table entries will not be printed. Otherwise, they will be. */
949 print_dentry(struct wim_dentry *dentry, void *lookup_table)
952 struct wim_lookup_table_entry *lte;
953 const struct wim_inode *inode = dentry->d_inode;
956 tprintf(T("[DENTRY]\n"));
957 tprintf(T("Length = %"PRIu64"\n"), dentry->length);
958 tprintf(T("Attributes = 0x%x\n"), inode->i_attributes);
959 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
960 if (file_attr_flags[i].flag & inode->i_attributes)
961 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
962 file_attr_flags[i].name);
963 tprintf(T("Security ID = %d\n"), inode->i_security_id);
964 tprintf(T("Subdir offset = %"PRIu64"\n"), dentry->subdir_offset);
966 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
967 tprintf(T("Creation Time = %"TS"\n"), buf);
969 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
970 tprintf(T("Last Access Time = %"TS"\n"), buf);
972 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
973 tprintf(T("Last Write Time = %"TS"\n"), buf);
975 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
976 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), inode->i_reparse_tag);
977 tprintf(T("Reparse Point Flags = 0x%"PRIx16"\n"),
978 inode->i_not_rpfixed);
979 tprintf(T("Reparse Point Unknown 2 = 0x%"PRIx32"\n"),
980 inode->i_rp_unknown_2);
982 tprintf(T("Reparse Point Unknown 1 = 0x%"PRIx32"\n"),
983 inode->i_rp_unknown_1);
984 tprintf(T("Hard Link Group = 0x%"PRIx64"\n"), inode->i_ino);
985 tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
986 tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
987 if (dentry_has_long_name(dentry))
988 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
989 if (dentry_has_short_name(dentry))
990 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
991 if (dentry->_full_path)
992 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
994 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
996 print_lookup_table_entry(lte, stdout);
998 hash = inode_stream_hash(inode, 0);
1000 tprintf(T("Hash = 0x"));
1001 print_hash(hash, stdout);
1006 for (u16 i = 0; i < inode->i_num_ads; i++) {
1007 tprintf(T("[Alternate Stream Entry %u]\n"), i);
1008 wimlib_printf(T("Name = \"%"WS"\"\n"),
1009 inode->i_ads_entries[i].stream_name);
1010 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
1011 inode->i_ads_entries[i].stream_name_nbytes);
1012 hash = inode_stream_hash(inode, i + 1);
1014 tprintf(T("Hash = 0x"));
1015 print_hash(hash, stdout);
1018 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
1024 /* Initializations done on every `struct wim_dentry'. */
1026 dentry_common_init(struct wim_dentry *dentry)
1028 memset(dentry, 0, sizeof(struct wim_dentry));
1032 new_timeless_inode(void)
1034 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
1036 inode->i_security_id = -1;
1038 inode->i_next_stream_id = 1;
1039 inode->i_not_rpfixed = 1;
1040 inode->i_canonical_streams = 1;
1041 INIT_LIST_HEAD(&inode->i_list);
1042 INIT_LIST_HEAD(&inode->i_dentry);
1047 static struct wim_inode *
1050 struct wim_inode *inode = new_timeless_inode();
1052 u64 now = get_wim_timestamp();
1053 inode->i_creation_time = now;
1054 inode->i_last_access_time = now;
1055 inode->i_last_write_time = now;
1060 /* Creates an unlinked directory entry. */
1062 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
1064 struct wim_dentry *dentry;
1067 dentry = MALLOC(sizeof(struct wim_dentry));
1069 return WIMLIB_ERR_NOMEM;
1071 dentry_common_init(dentry);
1072 ret = set_dentry_name(dentry, name);
1074 dentry->parent = dentry;
1075 *dentry_ret = dentry;
1078 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
1086 _new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
1089 struct wim_dentry *dentry;
1092 ret = new_dentry(name, &dentry);
1097 dentry->d_inode = new_timeless_inode();
1099 dentry->d_inode = new_inode();
1100 if (dentry->d_inode == NULL) {
1101 free_dentry(dentry);
1102 return WIMLIB_ERR_NOMEM;
1105 inode_add_dentry(dentry, dentry->d_inode);
1106 *dentry_ret = dentry;
1111 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
1113 return _new_dentry_with_inode(name, dentry_ret, true);
1117 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
1119 return _new_dentry_with_inode(name, dentry_ret, false);
1123 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
1126 struct wim_dentry *dentry;
1128 DEBUG("Creating filler directory \"%"TS"\"", name);
1129 ret = new_dentry_with_inode(name, &dentry);
1132 /* Leave the inode number as 0; this is allowed for non
1133 * hard-linked files. */
1134 dentry->d_inode->i_resolved = 1;
1135 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
1136 *dentry_ret = dentry;
1141 dentry_clear_inode_visited(struct wim_dentry *dentry, void *_ignore)
1143 dentry->d_inode->i_visited = 0;
1148 dentry_tree_clear_inode_visited(struct wim_dentry *root)
1150 for_dentry_in_tree(root, dentry_clear_inode_visited, NULL);
1154 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
1155 size_t name_nbytes, bool is_utf16le)
1158 memset(ads_entry, 0, sizeof(*ads_entry));
1161 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
1163 return WIMLIB_ERR_NOMEM;
1164 memcpy(p, name, name_nbytes);
1165 p[name_nbytes / 2] = cpu_to_le16(0);
1166 ads_entry->stream_name = p;
1167 ads_entry->stream_name_nbytes = name_nbytes;
1169 if (name && *(const tchar*)name != T('\0')) {
1170 ret = get_utf16le_name(name, &ads_entry->stream_name,
1171 &ads_entry->stream_name_nbytes);
1178 destroy_ads_entry(struct wim_ads_entry *ads_entry)
1180 FREE(ads_entry->stream_name);
1183 /* Frees an inode. */
1185 free_inode(struct wim_inode *inode)
1188 if (inode->i_ads_entries) {
1189 for (u16 i = 0; i < inode->i_num_ads; i++)
1190 destroy_ads_entry(&inode->i_ads_entries[i]);
1191 FREE(inode->i_ads_entries);
1193 /* HACK: This may instead delete the inode from i_list, but the
1194 * hlist_del() behaves the same as list_del(). */
1195 if (!hlist_unhashed(&inode->i_hlist))
1196 hlist_del(&inode->i_hlist);
1201 /* Decrements link count on an inode and frees it if the link count reaches 0.
1204 put_inode(struct wim_inode *inode)
1206 wimlib_assert(inode->i_nlink != 0);
1207 if (--inode->i_nlink == 0) {
1209 if (inode->i_num_opened_fds == 0)
1217 /* Frees a WIM dentry.
1219 * The corresponding inode (if any) is freed only if its link count is
1223 free_dentry(struct wim_dentry *dentry)
1226 FREE(dentry->file_name);
1227 FREE(dentry->short_name);
1228 FREE(dentry->_full_path);
1229 if (dentry->d_inode)
1230 put_inode(dentry->d_inode);
1235 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
1236 * to free a directory tree. */
1238 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
1240 struct wim_lookup_table *lookup_table = _lookup_table;
1243 struct wim_inode *inode = dentry->d_inode;
1244 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1245 struct wim_lookup_table_entry *lte;
1247 lte = inode_stream_lte(inode, i, lookup_table);
1249 lte_decrement_refcnt(lte, lookup_table);
1252 free_dentry(dentry);
1257 * Unlinks and frees a dentry tree.
1260 * The root of the tree.
1263 * The lookup table for dentries. If non-NULL, the reference counts in the
1264 * lookup table for the lookup table entries corresponding to the dentries
1265 * will be decremented.
1268 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1270 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1275 /* Insert a dentry into the case insensitive index for a directory.
1277 * This is a red-black tree, but when multiple dentries share the same
1278 * case-insensitive name, only one is inserted into the tree itself; the rest
1279 * are connected in a list.
1281 static struct wim_dentry *
1282 dentry_add_child_case_insensitive(struct wim_dentry *parent,
1283 struct wim_dentry *child)
1285 struct rb_root *root;
1286 struct rb_node **new;
1287 struct rb_node *rb_parent;
1289 root = &parent->d_inode->i_children_case_insensitive;
1290 new = &root->rb_node;
1293 struct wim_dentry *this = container_of(*new, struct wim_dentry,
1294 rb_node_case_insensitive);
1295 int result = dentry_compare_names_case_insensitive(child, this);
1300 new = &((*new)->rb_left);
1301 else if (result > 0)
1302 new = &((*new)->rb_right);
1306 rb_link_node(&child->rb_node_case_insensitive, rb_parent, new);
1307 rb_insert_color(&child->rb_node_case_insensitive, root);
1313 * Links a dentry into the directory tree.
1315 * @parent: The dentry that will be the parent of @child.
1316 * @child: The dentry to link.
1318 * Returns NULL if successful. If @parent already contains a dentry with the
1319 * same case-sensitive name as @child, the pointer to this duplicate dentry is
1323 dentry_add_child(struct wim_dentry * restrict parent,
1324 struct wim_dentry * restrict child)
1326 struct rb_root *root;
1327 struct rb_node **new;
1328 struct rb_node *rb_parent;
1330 wimlib_assert(dentry_is_directory(parent));
1331 wimlib_assert(parent != child);
1333 /* Case sensitive child dentry index */
1334 root = &parent->d_inode->i_children;
1335 new = &root->rb_node;
1338 struct wim_dentry *this = rbnode_dentry(*new);
1339 int result = dentry_compare_names_case_sensitive(child, this);
1344 new = &((*new)->rb_left);
1345 else if (result > 0)
1346 new = &((*new)->rb_right);
1350 child->parent = parent;
1351 rb_link_node(&child->rb_node, rb_parent, new);
1352 rb_insert_color(&child->rb_node, root);
1356 struct wim_dentry *existing;
1357 existing = dentry_add_child_case_insensitive(parent, child);
1359 list_add(&child->case_insensitive_conflict_list,
1360 &existing->case_insensitive_conflict_list);
1361 child->rb_node_case_insensitive.__rb_parent_color = 0;
1363 INIT_LIST_HEAD(&child->case_insensitive_conflict_list);
1370 /* Unlink a WIM dentry from the directory entry tree. */
1372 unlink_dentry(struct wim_dentry *dentry)
1374 struct wim_dentry *parent = dentry->parent;
1376 if (parent == dentry)
1378 rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1380 if (dentry->rb_node_case_insensitive.__rb_parent_color) {
1381 /* This dentry was in the case-insensitive red-black tree. */
1382 rb_erase(&dentry->rb_node_case_insensitive,
1383 &parent->d_inode->i_children_case_insensitive);
1384 if (!list_empty(&dentry->case_insensitive_conflict_list)) {
1385 /* Make a different case-insensitively-the-same dentry
1386 * be the "representative" in the red-black tree. */
1387 struct list_head *next;
1388 struct wim_dentry *other;
1389 struct wim_dentry *existing;
1391 next = dentry->case_insensitive_conflict_list.next;
1392 other = list_entry(next, struct wim_dentry, case_insensitive_conflict_list);
1393 existing = dentry_add_child_case_insensitive(parent, other);
1394 wimlib_assert(existing == NULL);
1397 list_del(&dentry->case_insensitive_conflict_list);
1402 * Returns the alternate data stream entry belonging to @inode that has the
1403 * stream name @stream_name, or NULL if the inode has no alternate data stream
1406 * If @p stream_name is the empty string, NULL is returned --- that is, this
1407 * function will not return "unnamed" alternate data stream entries.
1409 struct wim_ads_entry *
1410 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1413 if (inode->i_num_ads == 0) {
1416 size_t stream_name_utf16le_nbytes;
1418 struct wim_ads_entry *result;
1420 if (stream_name[0] == T('\0'))
1423 #if TCHAR_IS_UTF16LE
1424 const utf16lechar *stream_name_utf16le;
1426 stream_name_utf16le = stream_name;
1427 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1429 utf16lechar *stream_name_utf16le;
1432 int ret = tstr_to_utf16le(stream_name,
1433 tstrlen(stream_name) *
1435 &stream_name_utf16le,
1436 &stream_name_utf16le_nbytes);
1444 if (ads_entry_has_name(&inode->i_ads_entries[i],
1445 stream_name_utf16le,
1446 stream_name_utf16le_nbytes))
1450 result = &inode->i_ads_entries[i];
1453 } while (++i != inode->i_num_ads);
1454 #if !TCHAR_IS_UTF16LE
1455 FREE(stream_name_utf16le);
1461 static struct wim_ads_entry *
1462 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1463 size_t stream_name_nbytes, bool is_utf16le)
1466 struct wim_ads_entry *ads_entries;
1467 struct wim_ads_entry *new_entry;
1469 wimlib_assert(stream_name_nbytes != 0);
1471 if (inode->i_num_ads >= 0xfffe) {
1472 ERROR("Too many alternate data streams in one inode!");
1475 num_ads = inode->i_num_ads + 1;
1476 ads_entries = REALLOC(inode->i_ads_entries,
1477 num_ads * sizeof(inode->i_ads_entries[0]));
1478 if (ads_entries == NULL) {
1479 ERROR("Failed to allocate memory for new alternate data stream");
1482 inode->i_ads_entries = ads_entries;
1484 new_entry = &inode->i_ads_entries[num_ads - 1];
1485 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1487 new_entry->stream_id = inode->i_next_stream_id++;
1488 inode->i_num_ads = num_ads;
1492 struct wim_ads_entry *
1493 inode_add_ads_utf16le(struct wim_inode *inode,
1494 const utf16lechar *stream_name,
1495 size_t stream_name_nbytes)
1497 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1498 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1502 * Add an alternate stream entry to a WIM inode. On success, returns a pointer
1503 * to the new entry; on failure, returns NULL.
1505 * @stream_name must be a nonempty string.
1507 struct wim_ads_entry *
1508 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1510 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1511 return do_inode_add_ads(inode, stream_name,
1512 tstrlen(stream_name) * sizeof(tchar),
1516 static struct wim_lookup_table_entry *
1517 add_stream_from_data_buffer(const void *buffer, size_t size,
1518 struct wim_lookup_table *lookup_table)
1520 u8 hash[SHA1_HASH_SIZE];
1521 struct wim_lookup_table_entry *lte, *existing_lte;
1523 sha1_buffer(buffer, size, hash);
1524 existing_lte = lookup_resource(lookup_table, hash);
1526 wimlib_assert(existing_lte->size == size);
1531 lte = new_lookup_table_entry();
1534 buffer_copy = memdup(buffer, size);
1535 if (buffer_copy == NULL) {
1536 free_lookup_table_entry(lte);
1539 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1540 lte->attached_buffer = buffer_copy;
1542 copy_hash(lte->hash, hash);
1543 lookup_table_insert(lookup_table, lte);
1549 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1550 const void *value, size_t size,
1551 struct wim_lookup_table *lookup_table)
1553 struct wim_ads_entry *new_ads_entry;
1555 wimlib_assert(inode->i_resolved);
1557 new_ads_entry = inode_add_ads(inode, name);
1558 if (new_ads_entry == NULL)
1559 return WIMLIB_ERR_NOMEM;
1561 new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1563 if (new_ads_entry->lte == NULL) {
1564 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1566 return WIMLIB_ERR_NOMEM;
1572 inode_has_named_stream(const struct wim_inode *inode)
1574 for (u16 i = 0; i < inode->i_num_ads; i++)
1575 if (ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1580 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
1581 * stream contents. */
1583 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
1584 struct wim_lookup_table *lookup_table)
1586 inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
1587 if (inode->i_lte == NULL)
1588 return WIMLIB_ERR_NOMEM;
1589 inode->i_resolved = 1;
1593 /* Remove an alternate data stream from a WIM inode */
1595 inode_remove_ads(struct wim_inode *inode, u16 idx,
1596 struct wim_lookup_table *lookup_table)
1598 struct wim_ads_entry *ads_entry;
1599 struct wim_lookup_table_entry *lte;
1601 wimlib_assert(idx < inode->i_num_ads);
1602 wimlib_assert(inode->i_resolved);
1604 ads_entry = &inode->i_ads_entries[idx];
1606 DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1608 lte = ads_entry->lte;
1610 lte_decrement_refcnt(lte, lookup_table);
1612 destroy_ads_entry(ads_entry);
1614 memmove(&inode->i_ads_entries[idx],
1615 &inode->i_ads_entries[idx + 1],
1616 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1621 inode_has_unix_data(const struct wim_inode *inode)
1623 for (u16 i = 0; i < inode->i_num_ads; i++)
1624 if (ads_entry_is_unix_data(&inode->i_ads_entries[i]))
1631 inode_get_unix_data(const struct wim_inode *inode,
1632 struct wimlib_unix_data *unix_data,
1633 u16 *stream_idx_ret)
1635 const struct wim_ads_entry *ads_entry;
1636 const struct wim_lookup_table_entry *lte;
1640 wimlib_assert(inode->i_resolved);
1642 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1643 WIMLIB_UNIX_DATA_TAG, NULL);
1644 if (ads_entry == NULL)
1645 return NO_UNIX_DATA;
1648 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1650 lte = ads_entry->lte;
1652 return NO_UNIX_DATA;
1655 if (size != sizeof(struct wimlib_unix_data))
1656 return BAD_UNIX_DATA;
1658 ret = read_full_stream_into_buf(lte, unix_data);
1662 if (unix_data->version != 0)
1663 return BAD_UNIX_DATA;
1668 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1669 struct wim_lookup_table *lookup_table, int which)
1671 struct wimlib_unix_data unix_data;
1673 bool have_good_unix_data = false;
1674 bool have_unix_data = false;
1677 if (!(which & UNIX_DATA_CREATE)) {
1678 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1679 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1680 have_unix_data = true;
1682 have_good_unix_data = true;
1684 unix_data.version = 0;
1685 if (which & UNIX_DATA_UID || !have_good_unix_data)
1686 unix_data.uid = uid;
1687 if (which & UNIX_DATA_GID || !have_good_unix_data)
1688 unix_data.gid = gid;
1689 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1690 unix_data.mode = mode;
1691 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1693 sizeof(struct wimlib_unix_data),
1695 if (ret == 0 && have_unix_data)
1696 inode_remove_ads(inode, stream_idx, lookup_table);
1699 #endif /* !__WIN32__ */
1702 * Reads the alternate data stream entries of a WIM dentry.
1705 * Pointer to buffer that starts with the first alternate stream entry.
1708 * Inode to load the alternate data streams into. @inode->i_num_ads must
1709 * have been set to the number of alternate data streams that are expected.
1712 * Number of bytes of data remaining in the buffer pointed to by @p.
1714 * On success, inode->i_ads_entries is set to an array of `struct
1715 * wim_ads_entry's of length inode->i_num_ads. On failure, @inode is not
1719 * WIMLIB_ERR_SUCCESS (0)
1720 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
1724 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1725 size_t nbytes_remaining)
1728 struct wim_ads_entry *ads_entries;
1731 BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
1733 /* Allocate an array for our in-memory representation of the alternate
1734 * data stream entries. */
1735 num_ads = inode->i_num_ads;
1736 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1737 if (ads_entries == NULL)
1740 /* Read the entries into our newly allocated buffer. */
1741 for (u16 i = 0; i < num_ads; i++) {
1743 struct wim_ads_entry *cur_entry;
1744 const struct wim_ads_entry_on_disk *disk_entry =
1745 (const struct wim_ads_entry_on_disk*)p;
1747 cur_entry = &ads_entries[i];
1748 ads_entries[i].stream_id = i + 1;
1750 /* Do we have at least the size of the fixed-length data we know
1752 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1755 /* Read the length field */
1756 length = le64_to_cpu(disk_entry->length);
1758 /* Make sure the length field is neither so small it doesn't
1759 * include all the fixed-length data nor so large it overflows
1760 * the metadata resource buffer. */
1761 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1762 length > nbytes_remaining)
1765 /* Read the rest of the fixed-length data. */
1767 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1768 copy_hash(cur_entry->hash, disk_entry->hash);
1769 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1771 /* If stream_name_nbytes != 0, this is a named stream.
1772 * Otherwise this is an unnamed stream, or in some cases (bugs
1773 * in Microsoft's software I guess) a meaningless entry
1774 * distinguished from the real unnamed stream entry, if any, by
1775 * the fact that the real unnamed stream entry has a nonzero
1777 if (cur_entry->stream_name_nbytes) {
1778 /* The name is encoded in UTF16-LE, which uses 2-byte
1779 * coding units, so the length of the name had better be
1780 * an even number of bytes... */
1781 if (cur_entry->stream_name_nbytes & 1)
1784 /* Add the length of the stream name to get the length
1785 * we actually need to read. Make sure this isn't more
1786 * than the specified length of the entry. */
1787 if (sizeof(struct wim_ads_entry_on_disk) +
1788 cur_entry->stream_name_nbytes > length)
1791 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1792 if (cur_entry->stream_name == NULL)
1795 memcpy(cur_entry->stream_name,
1796 disk_entry->stream_name,
1797 cur_entry->stream_name_nbytes);
1798 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1800 /* Mark inode as having weird stream entries. */
1801 inode->i_canonical_streams = 0;
1804 /* It's expected that the size of every ADS entry is a multiple
1805 * of 8. However, to be safe, I'm allowing the possibility of
1806 * an ADS entry at the very end of the metadata resource ending
1807 * un-aligned. So although we still need to increment the input
1808 * pointer by @length to reach the next ADS entry, it's possible
1809 * that less than @length is actually remaining in the metadata
1810 * resource. We should set the remaining bytes to 0 if this
1812 length = (length + 7) & ~(u64)7;
1814 if (nbytes_remaining < length)
1815 nbytes_remaining = 0;
1817 nbytes_remaining -= length;
1819 inode->i_ads_entries = ads_entries;
1820 inode->i_next_stream_id = inode->i_num_ads + 1;
1824 ret = WIMLIB_ERR_NOMEM;
1825 goto out_free_ads_entries;
1827 ERROR("An alternate data stream entry is invalid");
1828 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1829 out_free_ads_entries:
1831 for (u16 i = 0; i < num_ads; i++)
1832 destroy_ads_entry(&ads_entries[i]);
1840 * Reads a WIM directory entry, including all alternate data stream entries that
1841 * follow it, from the WIM image's metadata resource.
1843 * @metadata_resource:
1844 * Pointer to the metadata resource buffer.
1846 * @metadata_resource_len:
1847 * Length of the metadata resource buffer, in bytes.
1849 * @offset: Offset of the dentry within the metadata resource.
1851 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1853 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1854 * been modified, but it will not be left with pointers to any allocated
1855 * buffers. On success, the dentry->length field must be examined. If zero,
1856 * this was a special "end of directory" dentry and not a real dentry. If
1857 * nonzero, this was a real dentry.
1860 * WIMLIB_ERR_SUCCESS (0)
1861 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
1865 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1866 u64 offset, struct wim_dentry * restrict dentry)
1869 u64 calculated_size;
1870 utf16lechar *file_name;
1871 utf16lechar *short_name;
1872 u16 short_name_nbytes;
1873 u16 file_name_nbytes;
1875 struct wim_inode *inode;
1876 const u8 *p = &metadata_resource[offset];
1877 const struct wim_dentry_on_disk *disk_dentry =
1878 (const struct wim_dentry_on_disk*)p;
1880 BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE);
1882 if ((uintptr_t)p & 7)
1883 WARNING("WIM dentry is not 8-byte aligned");
1885 dentry_common_init(dentry);
1887 /* Before reading the whole dentry, we need to read just the length.
1888 * This is because a dentry of length 8 (that is, just the length field)
1889 * terminates the list of sibling directory entries. */
1890 if (offset + sizeof(u64) > metadata_resource_len ||
1891 offset + sizeof(u64) < offset)
1893 ERROR("Directory entry starting at %"PRIu64" ends past the "
1894 "end of the metadata resource (size %"PRIu64")",
1895 offset, metadata_resource_len);
1896 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1898 dentry->length = le64_to_cpu(disk_dentry->length);
1900 /* A zero length field (really a length of 8, since that's how big the
1901 * directory entry is...) indicates that this is the end of directory
1902 * dentry. We do not read it into memory as an actual dentry, so just
1903 * return successfully in this case. */
1904 if (dentry->length == 8)
1906 if (dentry->length == 0)
1909 /* Now that we have the actual length provided in the on-disk structure,
1910 * again make sure it doesn't overflow the metadata resource buffer. */
1911 if (offset + dentry->length > metadata_resource_len ||
1912 offset + dentry->length < offset)
1914 ERROR("Directory entry at offset %"PRIu64" and with size "
1915 "%"PRIu64" ends past the end of the metadata resource "
1917 offset, dentry->length, metadata_resource_len);
1918 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1921 /* Make sure the dentry length is at least as large as the number of
1922 * fixed-length fields */
1923 if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1924 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1926 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1929 /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1930 inode = new_timeless_inode();
1932 return WIMLIB_ERR_NOMEM;
1934 /* Read more fields; some into the dentry, and some into the inode. */
1936 inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1937 inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1938 dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1939 dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1940 dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1941 inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1942 inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1943 inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1944 copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1946 /* I don't know what's going on here. It seems like M$ screwed up the
1947 * reparse points, then put the fields in the same place and didn't
1948 * document it. So we have some fields we read for reparse points, and
1949 * some fields in the same place for non-reparse-point.s */
1950 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1951 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1952 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1953 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1954 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1955 /* Leave inode->i_ino at 0. Note that this means the WIM file
1956 * cannot archive hard-linked reparse points. Such a thing
1957 * doesn't really make sense anyway, although I believe it's
1958 * theoretically possible to have them on NTFS. */
1960 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1961 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1964 inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1966 short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1967 file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1969 if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1971 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1972 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1973 goto out_free_inode;
1976 /* We now know the length of the file name and short name. Make sure
1977 * the length of the dentry is large enough to actually hold them.
1979 * The calculated length here is unaligned to allow for the possibility
1980 * that the dentry->length names an unaligned length, although this
1981 * would be unexpected. */
1982 calculated_size = dentry_correct_length_unaligned(file_name_nbytes,
1985 if (dentry->length < calculated_size) {
1986 ERROR("Unexpected end of directory entry! (Expected "
1987 "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1988 calculated_size, dentry->length);
1989 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1990 goto out_free_inode;
1993 p += sizeof(struct wim_dentry_on_disk);
1995 /* Read the filename if present. Note: if the filename is empty, there
1996 * is no null terminator following it. */
1997 if (file_name_nbytes) {
1998 file_name = MALLOC(file_name_nbytes + 2);
1999 if (file_name == NULL) {
2000 ERROR("Failed to allocate %d bytes for dentry file name",
2001 file_name_nbytes + 2);
2002 ret = WIMLIB_ERR_NOMEM;
2003 goto out_free_inode;
2005 memcpy(file_name, p, file_name_nbytes);
2006 p += file_name_nbytes + 2;
2007 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
2013 /* Read the short filename if present. Note: if there is no short
2014 * filename, there is no null terminator following it. */
2015 if (short_name_nbytes) {
2016 short_name = MALLOC(short_name_nbytes + 2);
2017 if (short_name == NULL) {
2018 ERROR("Failed to allocate %d bytes for dentry short name",
2019 short_name_nbytes + 2);
2020 ret = WIMLIB_ERR_NOMEM;
2021 goto out_free_file_name;
2023 memcpy(short_name, p, short_name_nbytes);
2024 p += short_name_nbytes + 2;
2025 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
2030 /* Align the dentry length */
2031 dentry->length = (dentry->length + 7) & ~7;
2034 * Read the alternate data streams, if present. dentry->num_ads tells
2035 * us how many they are, and they will directly follow the dentry
2038 * Note that each alternate data stream entry begins on an 8-byte
2039 * aligned boundary, and the alternate data stream entries seem to NOT
2040 * be included in the dentry->length field for some reason.
2042 if (inode->i_num_ads != 0) {
2043 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
2044 if (offset + dentry->length > metadata_resource_len ||
2045 (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
2047 metadata_resource_len - offset - dentry->length)))
2049 ERROR("Failed to read alternate data stream "
2050 "entries of WIM dentry \"%"WS"\"", file_name);
2051 goto out_free_short_name;
2054 /* We've read all the data for this dentry. Set the names and their
2055 * lengths, and we've done. */
2056 dentry->d_inode = inode;
2057 dentry->file_name = file_name;
2058 dentry->short_name = short_name;
2059 dentry->file_name_nbytes = file_name_nbytes;
2060 dentry->short_name_nbytes = short_name_nbytes;
2063 out_free_short_name:
2073 static const tchar *
2074 dentry_get_file_type_string(const struct wim_dentry *dentry)
2076 const struct wim_inode *inode = dentry->d_inode;
2077 if (inode_is_directory(inode))
2078 return T("directory");
2079 else if (inode_is_symlink(inode))
2080 return T("symbolic link");
2085 /* Reads the children of a dentry, and all their children, ..., etc. from the
2086 * metadata resource and into the dentry tree.
2088 * @metadata_resource:
2089 * An array that contains the uncompressed metadata resource for the WIM
2092 * @metadata_resource_len:
2093 * The length of the uncompressed metadata resource, in bytes.
2096 * A pointer to a `struct wim_dentry' that is the root of the directory
2097 * tree and has already been read from the metadata resource. It does not
2098 * need to be the real root because this procedure is called recursively.
2101 * WIMLIB_ERR_SUCCESS (0)
2102 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
2106 read_dentry_tree(const u8 * restrict metadata_resource,
2107 u64 metadata_resource_len,
2108 struct wim_dentry * restrict dentry)
2110 u64 cur_offset = dentry->subdir_offset;
2111 struct wim_dentry *child;
2112 struct wim_dentry *duplicate;
2113 struct wim_dentry *parent;
2114 struct wim_dentry cur_child;
2118 * If @dentry has no child dentries, nothing more needs to be done for
2119 * this branch. This is the case for regular files, symbolic links, and
2120 * *possibly* empty directories (although an empty directory may also
2121 * have one child dentry that is the special end-of-directory dentry)
2123 if (cur_offset == 0)
2126 /* Check for cyclic directory structure */
2127 for (parent = dentry->parent; !dentry_is_root(parent); parent = parent->parent)
2129 if (unlikely(parent->subdir_offset == cur_offset)) {
2130 ERROR("Cyclic directory structure directed: children "
2131 "of \"%"TS"\" coincide with children of \"%"TS"\"",
2132 dentry_full_path(dentry),
2133 dentry_full_path(parent));
2134 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
2138 /* Find and read all the children of @dentry. */
2141 /* Read next child of @dentry into @cur_child. */
2142 ret = read_dentry(metadata_resource, metadata_resource_len,
2143 cur_offset, &cur_child);
2147 /* Check for end of directory. */
2148 if (cur_child.length == 0)
2151 /* Not end of directory. Allocate this child permanently and
2152 * link it to the parent and previous child. */
2153 child = memdup(&cur_child, sizeof(struct wim_dentry));
2154 if (child == NULL) {
2155 ERROR("Failed to allocate new dentry!");
2156 ret = WIMLIB_ERR_NOMEM;
2160 /* Advance to the offset of the next child. Note: We need to
2161 * advance by the TOTAL length of the dentry, not by the length
2162 * cur_child.length, which although it does take into account
2163 * the padding, it DOES NOT take into account alternate stream
2165 cur_offset += dentry_in_total_length(child);
2167 if (unlikely(!dentry_has_long_name(child))) {
2168 WARNING("Ignoring unnamed dentry in "
2169 "directory \"%"TS"\"",
2170 dentry_full_path(dentry));
2175 duplicate = dentry_add_child(dentry, child);
2176 if (unlikely(duplicate)) {
2177 const tchar *child_type, *duplicate_type;
2178 child_type = dentry_get_file_type_string(child);
2179 duplicate_type = dentry_get_file_type_string(duplicate);
2180 WARNING("Ignoring duplicate %"TS" \"%"TS"\" "
2181 "(the WIM image already contains a %"TS" "
2182 "at that path with the exact same name)",
2183 child_type, dentry_full_path(duplicate),
2189 inode_add_dentry(child, child->d_inode);
2190 /* If there are children of this child, call this
2191 * procedure recursively. */
2192 if (child->subdir_offset != 0) {
2193 if (likely(dentry_is_directory(child))) {
2194 ret = read_dentry_tree(metadata_resource,
2195 metadata_resource_len,
2200 WARNING("Ignoring children of non-directory \"%"TS"\"",
2201 dentry_full_path(child));
2209 * Writes a WIM alternate data stream (ADS) entry to an output buffer.
2211 * @ads_entry: The ADS entry structure.
2212 * @hash: The hash field to use (instead of the one in the ADS entry).
2213 * @p: The memory location to write the data to.
2215 * Returns a pointer to the byte after the last byte written.
2218 write_ads_entry(const struct wim_ads_entry *ads_entry,
2219 const u8 *hash, u8 * restrict p)
2221 struct wim_ads_entry_on_disk *disk_ads_entry =
2222 (struct wim_ads_entry_on_disk*)p;
2225 disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
2226 copy_hash(disk_ads_entry->hash, hash);
2227 disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
2228 p += sizeof(struct wim_ads_entry_on_disk);
2229 if (ads_entry->stream_name_nbytes) {
2230 p = mempcpy(p, ads_entry->stream_name,
2231 ads_entry->stream_name_nbytes + 2);
2233 /* Align to 8-byte boundary */
2234 while ((uintptr_t)p & 7)
2236 disk_ads_entry->length = cpu_to_le64(p - orig_p);
2241 * Writes a WIM dentry to an output buffer.
2243 * @dentry: The dentry structure.
2244 * @p: The memory location to write the data to.
2246 * Returns the pointer to the byte after the last byte we wrote as part of the
2247 * dentry, including any alternate data stream entries.
2250 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
2252 const struct wim_inode *inode;
2253 struct wim_dentry_on_disk *disk_dentry;
2256 bool use_dummy_stream;
2259 wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
2262 inode = dentry->d_inode;
2263 use_dummy_stream = inode_needs_dummy_stream(inode);
2264 disk_dentry = (struct wim_dentry_on_disk*)p;
2266 disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
2267 disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
2268 disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
2269 disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
2270 disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
2271 disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
2272 disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
2273 disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
2274 if (use_dummy_stream)
2277 hash = inode_stream_hash(inode, 0);
2278 copy_hash(disk_dentry->unnamed_stream_hash, hash);
2279 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2280 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2281 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
2282 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
2283 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
2285 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2286 disk_dentry->nonreparse.hard_link_group_id =
2287 cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
2289 num_ads = inode->i_num_ads;
2290 if (use_dummy_stream)
2292 disk_dentry->num_alternate_data_streams = cpu_to_le16(num_ads);
2293 disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
2294 disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
2295 p += sizeof(struct wim_dentry_on_disk);
2297 wimlib_assert(dentry_is_root(dentry) != dentry_has_long_name(dentry));
2299 if (dentry_has_long_name(dentry))
2300 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
2302 if (dentry_has_short_name(dentry))
2303 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
2305 /* Align to 8-byte boundary */
2306 while ((uintptr_t)p & 7)
2309 /* We calculate the correct length of the dentry ourselves because the
2310 * dentry->length field may been set to an unexpected value from when we
2311 * read the dentry in (for example, there may have been unknown data
2312 * appended to the end of the dentry...). Furthermore, the dentry may
2313 * have been renamed, thus changing its needed length. */
2314 disk_dentry->length = cpu_to_le64(p - orig_p);
2316 if (use_dummy_stream) {
2317 hash = inode_unnamed_stream_hash(inode);
2318 p = write_ads_entry(&(struct wim_ads_entry){}, hash, p);
2321 /* Write the alternate data streams entries, if any. */
2322 for (u16 i = 0; i < inode->i_num_ads; i++) {
2323 hash = inode_stream_hash(inode, i + 1);
2324 p = write_ads_entry(&inode->i_ads_entries[i], hash, p);
2331 write_dentry_cb(struct wim_dentry *dentry, void *_p)
2334 *p = write_dentry(dentry, *p);
2339 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
2342 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
2345 *p = write_dentry_tree_recursive(dentry, *p);
2349 /* Recursive function that writes a dentry tree rooted at @parent, not including
2350 * @parent itself, which has already been written. */
2352 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
2354 /* Nothing to do if this dentry has no children. */
2355 if (parent->subdir_offset == 0)
2358 /* Write child dentries and end-of-directory entry.
2360 * Note: we need to write all of this dentry's children before
2361 * recursively writing the directory trees rooted at each of the child
2362 * dentries, since the on-disk dentries for a dentry's children are
2363 * always located at consecutive positions in the metadata resource! */
2364 for_dentry_child(parent, write_dentry_cb, &p);
2366 /* write end of directory entry */
2367 *(le64*)p = cpu_to_le64(0);
2370 /* Recurse on children. */
2371 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
2375 /* Writes a directory tree to the metadata resource.
2377 * @root: Root of the dentry tree.
2378 * @p: Pointer to a buffer with enough space for the dentry tree.
2380 * Returns pointer to the byte after the last byte we wrote.
2383 write_dentry_tree(const struct wim_dentry * restrict root, u8 * restrict p)
2385 DEBUG("Writing dentry tree.");
2386 wimlib_assert(dentry_is_root(root));
2388 /* If we're the root dentry, we have no parent that already
2389 * wrote us, so we need to write ourselves. */
2390 p = write_dentry(root, p);
2392 /* Write end of directory entry after the root dentry just to be safe;
2393 * however the root dentry obviously cannot have any siblings. */
2394 *(le64*)p = cpu_to_le64(0);
2397 /* Recursively write the rest of the dentry tree. */
2398 return write_dentry_tree_recursive(root, p);
2403 init_wimlib_dentry(struct wimlib_dir_entry *wdentry,
2404 struct wim_dentry *dentry,
2405 const WIMStruct *wim,
2410 const struct wim_inode *inode = dentry->d_inode;
2411 struct wim_lookup_table_entry *lte;
2414 #if TCHAR_IS_UTF16LE
2415 wdentry->filename = dentry->file_name;
2416 wdentry->dos_name = dentry->short_name;
2418 if (dentry_has_long_name(dentry)) {
2419 ret = utf16le_to_tstr(dentry->file_name,
2420 dentry->file_name_nbytes,
2421 (tchar**)&wdentry->filename,
2426 if (dentry_has_short_name(dentry)) {
2427 ret = utf16le_to_tstr(dentry->short_name,
2428 dentry->short_name_nbytes,
2429 (tchar**)&wdentry->dos_name,
2435 ret = calculate_dentry_full_path(dentry);
2438 wdentry->full_path = dentry->_full_path;
2440 for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->parent)
2443 if (inode->i_security_id >= 0) {
2444 const struct wim_security_data *sd = wim_const_security_data(wim);
2445 wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
2446 wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
2448 wdentry->reparse_tag = inode->i_reparse_tag;
2449 wdentry->num_links = inode->i_nlink;
2450 wdentry->attributes = inode->i_attributes;
2451 wdentry->hard_link_group_id = inode->i_ino;
2452 wdentry->creation_time = wim_timestamp_to_timespec(inode->i_creation_time);
2453 wdentry->last_write_time = wim_timestamp_to_timespec(inode->i_last_write_time);
2454 wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
2456 lte = inode_unnamed_lte(inode, wim->lookup_table);
2458 lte_to_wimlib_resource_entry(lte, &wdentry->streams[0].resource);
2459 } else if (!is_zero_hash(hash = inode_unnamed_stream_hash(inode))) {
2460 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
2461 return resource_not_found_error(inode, hash);
2462 copy_hash(wdentry->streams[0].resource.sha1_hash, hash);
2463 wdentry->streams[0].resource.is_missing = 1;
2466 for (unsigned i = 0; i < inode->i_num_ads; i++) {
2467 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
2469 lte = inode_stream_lte(inode, i + 1, wim->lookup_table);
2470 wdentry->num_named_streams++;
2472 lte_to_wimlib_resource_entry(lte, &wdentry->streams[
2473 wdentry->num_named_streams].resource);
2474 } else if (!is_zero_hash(hash = inode_stream_hash(inode, i + 1))) {
2475 if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
2476 return resource_not_found_error(inode, hash);
2477 copy_hash(wdentry->streams[
2478 wdentry->num_named_streams].resource.sha1_hash, hash);
2480 wdentry->num_named_streams].resource.is_missing = 1;
2482 #if TCHAR_IS_UTF16LE
2483 wdentry->streams[wdentry->num_named_streams].stream_name =
2484 inode->i_ads_entries[i].stream_name;
2488 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
2489 inode->i_ads_entries[i].stream_name_nbytes,
2490 (tchar**)&wdentry->streams[
2491 wdentry->num_named_streams].stream_name,
2501 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
2503 #if !TCHAR_IS_UTF16LE
2504 FREE((tchar*)wdentry->filename);
2505 FREE((tchar*)wdentry->dos_name);
2506 for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
2507 FREE((tchar*)wdentry->streams[i].stream_name);
2512 struct iterate_dir_tree_ctx {
2515 wimlib_iterate_dir_tree_callback_t cb;
2520 do_iterate_dir_tree(WIMStruct *wim,
2521 struct wim_dentry *dentry, int flags,
2522 wimlib_iterate_dir_tree_callback_t cb,
2526 call_do_iterate_dir_tree(struct wim_dentry *dentry, void *_ctx)
2528 struct iterate_dir_tree_ctx *ctx = _ctx;
2529 return do_iterate_dir_tree(ctx->wim, dentry, ctx->flags,
2530 ctx->cb, ctx->user_ctx);
2534 do_iterate_dir_tree(WIMStruct *wim,
2535 struct wim_dentry *dentry, int flags,
2536 wimlib_iterate_dir_tree_callback_t cb,
2539 struct wimlib_dir_entry *wdentry;
2540 int ret = WIMLIB_ERR_NOMEM;
2543 wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
2544 (1 + dentry->d_inode->i_num_ads) *
2545 sizeof(struct wimlib_stream_entry));
2546 if (wdentry == NULL)
2549 ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
2551 goto out_free_wimlib_dentry;
2553 if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
2554 ret = (*cb)(wdentry, user_ctx);
2556 goto out_free_wimlib_dentry;
2559 if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
2560 WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
2562 struct iterate_dir_tree_ctx ctx = {
2564 .flags = flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
2566 .user_ctx = user_ctx,
2568 ret = for_dentry_child(dentry, call_do_iterate_dir_tree, &ctx);
2570 out_free_wimlib_dentry:
2571 free_wimlib_dentry(wdentry);
2576 struct image_iterate_dir_tree_ctx {
2579 wimlib_iterate_dir_tree_callback_t cb;
2585 image_do_iterate_dir_tree(WIMStruct *wim)
2587 struct image_iterate_dir_tree_ctx *ctx = wim->private;
2588 struct wim_dentry *dentry;
2590 dentry = get_dentry(wim, ctx->path);
2592 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
2593 return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
2596 /* API function documented in wimlib.h */
2598 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *path,
2600 wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
2602 struct image_iterate_dir_tree_ctx ctx = {
2606 .user_ctx = user_ctx,
2608 wim->private = &ctx;
2609 return for_image(wim, image, image_do_iterate_dir_tree);
2612 /* Returns %true iff the metadata of @inode and @template_inode are reasonably
2613 * consistent with them being the same, unmodified file. */
2615 inode_metadata_consistent(const struct wim_inode *inode,
2616 const struct wim_inode *template_inode,
2617 const struct wim_lookup_table *template_lookup_table)
2619 /* Must have exact same creation time and last write time. */
2620 if (inode->i_creation_time != template_inode->i_creation_time ||
2621 inode->i_last_write_time != template_inode->i_last_write_time)
2624 /* Last access time may have stayed the same or increased, but certainly
2625 * shouldn't have decreased. */
2626 if (inode->i_last_access_time < template_inode->i_last_access_time)
2629 /* Must have same number of alternate data stream entries. */
2630 if (inode->i_num_ads != template_inode->i_num_ads)
2633 /* If the stream entries for the inode are for some reason not resolved,
2634 * then the hashes are already available and the point of this function
2636 if (!inode->i_resolved)
2639 /* Iterate through each stream and do some more checks. */
2640 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2641 const struct wim_lookup_table_entry *lte, *template_lte;
2643 lte = inode_stream_lte_resolved(inode, i);
2644 template_lte = inode_stream_lte(template_inode, i,
2645 template_lookup_table);
2647 /* Compare stream sizes. */
2648 if (lte && template_lte) {
2649 if (lte->size != template_lte->size)
2652 /* If hash happens to be available, compare with template. */
2653 if (!lte->unhashed && !template_lte->unhashed &&
2654 !hashes_equal(lte->hash, template_lte->hash))
2657 } else if (lte && lte->size) {
2659 } else if (template_lte && template_lte->size) {
2664 /* All right, barring a full checksum and given that the inodes share a
2665 * path and the user isn't trying to trick us, these inodes most likely
2666 * refer to the same file. */
2671 * Given an inode @inode that has been determined to be "the same" as another
2672 * inode @template_inode in either the same WIM or another WIM, retrieve some
2673 * useful stream information (e.g. checksums) from @template_inode.
2675 * This assumes that the streams for @inode have been resolved (to point
2676 * directly to the appropriate `struct wim_lookup_table_entry's) but do not
2677 * necessarily have checksum information filled in.
2680 inode_copy_checksums(struct wim_inode *inode,
2681 struct wim_inode *template_inode,
2683 WIMStruct *template_wim)
2685 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2686 struct wim_lookup_table_entry *lte, *template_lte;
2687 struct wim_lookup_table_entry *replace_lte;
2689 lte = inode_stream_lte_resolved(inode, i);
2690 template_lte = inode_stream_lte(template_inode, i,
2691 template_wim->lookup_table);
2693 /* Only take action if both entries exist, the entry for @inode
2694 * has no checksum calculated, but the entry for @template_inode
2696 if (lte == NULL || template_lte == NULL ||
2697 !lte->unhashed || template_lte->unhashed)
2700 wimlib_assert(lte->refcnt == inode->i_nlink);
2702 /* If the WIM of the template image is the same as the WIM of
2703 * the new image, then @template_lte can be used directly.
2705 * Otherwise, look for a stream with the same hash in the WIM of
2706 * the new image. If found, use it; otherwise re-use the entry
2707 * being discarded, filling in the hash. */
2709 if (wim == template_wim)
2710 replace_lte = template_lte;
2712 replace_lte = lookup_resource(wim->lookup_table,
2713 template_lte->hash);
2715 list_del(<e->unhashed_list);
2717 free_lookup_table_entry(lte);
2719 copy_hash(lte->hash, template_lte->hash);
2721 lookup_table_insert(wim->lookup_table, lte);
2727 inode->i_lte = replace_lte;
2729 inode->i_ads_entries[i - 1].lte = replace_lte;
2731 replace_lte->refcnt += inode->i_nlink;
2736 struct reference_template_args {
2738 WIMStruct *template_wim;
2742 dentry_reference_template(struct wim_dentry *dentry, void *_args)
2745 struct wim_dentry *template_dentry;
2746 struct wim_inode *inode, *template_inode;
2747 struct reference_template_args *args = _args;
2748 WIMStruct *wim = args->wim;
2749 WIMStruct *template_wim = args->template_wim;
2751 if (dentry->d_inode->i_visited)
2754 ret = calculate_dentry_full_path(dentry);
2758 template_dentry = get_dentry(template_wim, dentry->_full_path);
2759 if (template_dentry == NULL) {
2760 DEBUG("\"%"TS"\": newly added file", dentry->_full_path);
2764 inode = dentry->d_inode;
2765 template_inode = template_dentry->d_inode;
2767 if (inode_metadata_consistent(inode, template_inode,
2768 template_wim->lookup_table)) {
2769 /*DEBUG("\"%"TS"\": No change detected", dentry->_full_path);*/
2770 ret = inode_copy_checksums(inode, template_inode,
2772 inode->i_visited = 1;
2774 DEBUG("\"%"TS"\": change detected!", dentry->_full_path);
2780 /* API function documented in wimlib.h */
2782 wimlib_reference_template_image(WIMStruct *wim, int new_image,
2783 WIMStruct *template_wim, int template_image,
2784 int flags, wimlib_progress_func_t progress_func)
2787 struct wim_image_metadata *new_imd;
2789 if (wim == NULL || template_wim == NULL)
2790 return WIMLIB_ERR_INVALID_PARAM;
2792 if (wim == template_wim && new_image == template_image)
2793 return WIMLIB_ERR_INVALID_PARAM;
2795 if (new_image < 1 || new_image > wim->hdr.image_count)
2796 return WIMLIB_ERR_INVALID_IMAGE;
2798 if (!wim_has_metadata(wim))
2799 return WIMLIB_ERR_METADATA_NOT_FOUND;
2801 new_imd = wim->image_metadata[new_image - 1];
2802 if (!new_imd->modified)
2803 return WIMLIB_ERR_INVALID_PARAM;
2805 ret = select_wim_image(template_wim, template_image);
2809 struct reference_template_args args = {
2811 .template_wim = template_wim,
2814 ret = for_dentry_in_tree(new_imd->root_dentry,
2815 dentry_reference_template, &args);
2816 dentry_tree_clear_inode_visited(new_imd->root_dentry);