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. (wimlib does not use this quirk on WIM images it creates.)
146 u8 unnamed_stream_hash[SHA1_HASH_SIZE];
148 /* The format of the following data is not yet completely known and they
149 * do not correspond to Microsoft's documentation.
151 * If this directory entry is for a reparse point (has
152 * FILE_ATTRIBUTE_REPARSE_POINT set in the attributes field), then the
153 * version of the following fields containing the reparse tag is valid.
154 * Furthermore, the field notated as not_rpfixed, as far as I can tell,
155 * is supposed to be set to 1 if reparse point fixups (a.k.a. fixing the
156 * targets of absolute symbolic links) were *not* done, and otherwise 0.
158 * If this directory entry is not for a reparse point, then the version
159 * of the following fields containing the hard_link_group_id is valid.
160 * All MS says about this field is that "If this file is part of a hard
161 * link set, all the directory entries in the set will share the same
162 * value in this field.". However, more specifically I have observed
164 * - If the file is part of a hard link set of size 1, then the
165 * hard_link_group_id should be set to either 0, which is treated
166 * specially as indicating "not hardlinked", or any unique value.
167 * - The specific nonzero values used to identity hard link sets do
168 * not matter, as long as they are unique.
169 * - However, due to bugs in Microsoft's software, it is actually NOT
170 * guaranteed that directory entries that share the same hard link
171 * group ID are actually hard linked to each either. We have to
172 * handle this by using special code to use distinguishing features
173 * (which is possible because some information about the underlying
174 * inode is repeated in each dentry) to split up these fake hard link
175 * groups into what they actually are supposed to be.
183 } _packed_attribute reparse;
186 le64 hard_link_group_id;
187 } _packed_attribute nonreparse;
190 /* Number of alternate data stream entries that directly follow this
192 le16 num_alternate_data_streams;
194 /* Length of this file's UTF-16LE encoded short name (8.3 DOS-compatible
195 * name), if present, in bytes, excluding the null terminator. If this
196 * file has no short name, then this field should be 0. */
197 le16 short_name_nbytes;
199 /* Length of this file's UTF-16LE encoded "long" name, excluding the
200 * null terminator. If this file has no short name, then this field
201 * should be 0. It's expected that only the root dentry has this field
203 le16 file_name_nbytes;
205 /* Followed by variable length file name, in UTF16-LE, if
206 * file_name_nbytes != 0. Includes null terminator. */
207 /*utf16lechar file_name[];*/
209 /* Followed by variable length short name, in UTF16-LE, if
210 * short_name_nbytes != 0. Includes null terminator. */
211 /*utf16lechar short_name[];*/
214 #define WIM_DENTRY_DISK_SIZE 102
216 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
217 * a file name and short name that take the specified numbers of bytes. This
218 * excludes any alternate data stream entries that may follow the dentry. */
220 _dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
222 u64 length = sizeof(struct wim_dentry_on_disk);
223 if (file_name_nbytes)
224 length += file_name_nbytes + 2;
225 if (short_name_nbytes)
226 length += short_name_nbytes + 2;
230 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
231 * the file name length and short name length. Note that dentry->length is
232 * ignored; also, this excludes any alternate data stream entries that may
233 * follow the dentry. */
235 dentry_correct_length_unaligned(const struct wim_dentry *dentry)
237 return _dentry_correct_length_unaligned(dentry->file_name_nbytes,
238 dentry->short_name_nbytes);
241 /* Duplicates a string of system-dependent encoding into a UTF-16LE string and
242 * returns the string and its length, in bytes, in the pointer arguments. Frees
243 * any existing string at the return location before overwriting it. */
245 get_utf16le_name(const tchar *name, utf16lechar **name_utf16le_ret,
246 u16 *name_utf16le_nbytes_ret)
248 utf16lechar *name_utf16le;
249 size_t name_utf16le_nbytes;
252 name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
253 name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
255 return WIMLIB_ERR_NOMEM;
256 memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
260 ret = tstr_to_utf16le(name, tstrlen(name), &name_utf16le,
261 &name_utf16le_nbytes);
263 if (name_utf16le_nbytes > 0xffff) {
265 ERROR("Multibyte string \"%"TS"\" is too long!", name);
266 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
271 FREE(*name_utf16le_ret);
272 *name_utf16le_ret = name_utf16le;
273 *name_utf16le_nbytes_ret = name_utf16le_nbytes;
278 /* Sets the name of a WIM dentry from a multibyte string. */
280 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name)
283 ret = get_utf16le_name(new_name, &dentry->file_name,
284 &dentry->file_name_nbytes);
286 /* Clear the short name and recalculate the dentry length */
287 if (dentry_has_short_name(dentry)) {
288 FREE(dentry->short_name);
289 dentry->short_name = NULL;
290 dentry->short_name_nbytes = 0;
296 /* Returns the total length of a WIM alternate data stream entry on-disk,
297 * including the stream name, the null terminator, AND the padding after the
298 * entry to align the next ADS entry or dentry on an 8-byte boundary. */
300 ads_entry_total_length(const struct wim_ads_entry *entry)
302 u64 len = sizeof(struct wim_ads_entry_on_disk);
303 if (entry->stream_name_nbytes)
304 len += entry->stream_name_nbytes + 2;
305 return (len + 7) & ~7;
310 _dentry_total_length(const struct wim_dentry *dentry, u64 length)
312 const struct wim_inode *inode = dentry->d_inode;
313 for (u16 i = 0; i < inode->i_num_ads; i++)
314 length += ads_entry_total_length(&inode->i_ads_entries[i]);
315 return (length + 7) & ~7;
318 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
319 * all alternate data streams. */
321 dentry_correct_total_length(const struct wim_dentry *dentry)
323 return _dentry_total_length(dentry,
324 dentry_correct_length_unaligned(dentry));
327 /* Like dentry_correct_total_length(), but use the existing dentry->length field
328 * instead of calculating its "correct" value. */
330 dentry_total_length(const struct wim_dentry *dentry)
332 return _dentry_total_length(dentry, dentry->length);
336 for_dentry_in_rbtree(struct rb_node *root,
337 int (*visitor)(struct wim_dentry *, void *),
341 struct rb_node *node = root;
345 list_add(&rbnode_dentry(node)->tmp_list, &stack);
346 node = node->rb_left;
348 struct list_head *next;
349 struct wim_dentry *dentry;
354 dentry = container_of(next, struct wim_dentry, tmp_list);
356 ret = visitor(dentry, arg);
359 node = dentry->rb_node.rb_right;
365 for_dentry_tree_in_rbtree_depth(struct rb_node *node,
366 int (*visitor)(struct wim_dentry*, void*),
371 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
375 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
379 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
387 for_dentry_tree_in_rbtree(struct rb_node *node,
388 int (*visitor)(struct wim_dentry*, void*),
393 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
396 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
399 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
406 /* Calls a function on all directory entries in a WIM dentry tree. Logically,
407 * this is a pre-order traversal (the function is called on a parent dentry
408 * before its children), but sibling dentries will be visited in order as well.
411 for_dentry_in_tree(struct wim_dentry *root,
412 int (*visitor)(struct wim_dentry*, void*), void *arg)
418 ret = (*visitor)(root, arg);
421 return for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node,
426 /* Like for_dentry_in_tree(), but the visitor function is always called on a
427 * dentry's children before on itself. */
429 for_dentry_in_tree_depth(struct wim_dentry *root,
430 int (*visitor)(struct wim_dentry*, void*), void *arg)
436 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
440 return (*visitor)(root, arg);
443 /* Calculate the full path of @dentry. The full path of its parent must have
444 * already been calculated, or it must be the root dentry. */
446 calculate_dentry_full_path(struct wim_dentry *dentry)
449 u32 full_path_nbytes;
452 if (dentry->_full_path)
455 if (dentry_is_root(dentry)) {
456 static const tchar _root_path[] = {WIM_PATH_SEPARATOR, T('\0')};
457 full_path = TSTRDUP(_root_path);
459 return WIMLIB_ERR_NOMEM;
460 full_path_nbytes = 1 * sizeof(tchar);
462 struct wim_dentry *parent;
463 tchar *parent_full_path;
464 u32 parent_full_path_nbytes;
465 size_t filename_nbytes;
467 parent = dentry->parent;
468 if (dentry_is_root(parent)) {
469 parent_full_path = T("");
470 parent_full_path_nbytes = 0;
472 if (!parent->_full_path) {
473 ret = calculate_dentry_full_path(parent);
477 parent_full_path = parent->_full_path;
478 parent_full_path_nbytes = parent->full_path_nbytes;
481 /* Append this dentry's name as a tchar string to the full path
482 * of the parent followed by the path separator */
484 filename_nbytes = dentry->file_name_nbytes;
487 int ret = utf16le_to_tstr_nbytes(dentry->file_name,
488 dentry->file_name_nbytes,
495 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
497 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
499 return WIMLIB_ERR_NOMEM;
500 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
501 full_path[parent_full_path_nbytes / sizeof(tchar)] = WIM_PATH_SEPARATOR;
503 memcpy(&full_path[parent_full_path_nbytes / sizeof(tchar) + 1],
505 filename_nbytes + sizeof(tchar));
507 utf16le_to_tstr_buf(dentry->file_name,
508 dentry->file_name_nbytes,
509 &full_path[parent_full_path_nbytes /
513 dentry->_full_path = full_path;
514 dentry->full_path_nbytes= full_path_nbytes;
519 do_calculate_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
521 return calculate_dentry_full_path(dentry);
525 calculate_dentry_tree_full_paths(struct wim_dentry *root)
527 return for_dentry_in_tree(root, do_calculate_dentry_full_path, NULL);
531 dentry_full_path(struct wim_dentry *dentry)
533 calculate_dentry_full_path(dentry);
534 return dentry->_full_path;
538 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
540 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
545 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
547 calculate_subdir_offsets(dentry, subdir_offset_p);
552 * Recursively calculates the subdir offsets for a directory tree.
554 * @dentry: The root of the directory tree.
555 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
556 * offset for @dentry.
559 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
561 struct rb_node *node;
563 dentry->subdir_offset = *subdir_offset_p;
564 node = dentry->d_inode->i_children.rb_node;
566 /* Advance the subdir offset by the amount of space the children
567 * of this dentry take up. */
568 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
570 /* End-of-directory dentry on disk. */
571 *subdir_offset_p += 8;
573 /* Recursively call calculate_subdir_offsets() on all the
575 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
577 /* On disk, childless directories have a valid subdir_offset
578 * that points to an 8-byte end-of-directory dentry. Regular
579 * files or reparse points have a subdir_offset of 0. */
580 if (dentry_is_directory(dentry))
581 *subdir_offset_p += 8;
583 dentry->subdir_offset = 0;
587 /* Case-sensitive UTF-16LE dentry or stream name comparison. Used on both UNIX
588 * (always) and Windows (sometimes) */
590 compare_utf16le_names_case_sensitive(const utf16lechar *name1, size_t nbytes1,
591 const utf16lechar *name2, size_t nbytes2)
593 /* Return the result if the strings differ up to their minimum length.
594 * Note that we cannot use strcmp() or strncmp() here, as the strings
595 * are in UTF-16LE format. */
596 int result = memcmp(name1, name2, min(nbytes1, nbytes2));
600 /* The strings are the same up to their minimum length, so return a
601 * result based on their lengths. */
602 if (nbytes1 < nbytes2)
604 else if (nbytes1 > nbytes2)
611 /* Windoze: Case-insensitive UTF-16LE dentry or stream name comparison */
613 compare_utf16le_names_case_insensitive(const utf16lechar *name1, size_t nbytes1,
614 const utf16lechar *name2, size_t nbytes2)
616 /* Return the result if the strings differ up to their minimum length.
618 int result = _wcsnicmp((const wchar_t*)name1, (const wchar_t*)name2,
619 min(nbytes1 / 2, nbytes2 / 2));
623 /* The strings are the same up to their minimum length, so return a
624 * result based on their lengths. */
625 if (nbytes1 < nbytes2)
627 else if (nbytes1 > nbytes2)
632 #endif /* __WIN32__ */
635 # define compare_utf16le_names compare_utf16le_names_case_insensitive
637 # define compare_utf16le_names compare_utf16le_names_case_sensitive
643 dentry_compare_names_case_insensitive(const struct wim_dentry *d1,
644 const struct wim_dentry *d2)
646 return compare_utf16le_names_case_insensitive(d1->file_name,
647 d1->file_name_nbytes,
649 d2->file_name_nbytes);
651 #endif /* __WIN32__ */
654 dentry_compare_names_case_sensitive(const struct wim_dentry *d1,
655 const struct wim_dentry *d2)
657 return compare_utf16le_names_case_sensitive(d1->file_name,
658 d1->file_name_nbytes,
660 d2->file_name_nbytes);
664 # define dentry_compare_names dentry_compare_names_case_insensitive
666 # define dentry_compare_names dentry_compare_names_case_sensitive
669 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
670 * stream name @name that has length @name_nbytes bytes. */
672 ads_entry_has_name(const struct wim_ads_entry *entry,
673 const utf16lechar *name, size_t name_nbytes)
675 return !compare_utf16le_names(name, name_nbytes,
677 entry->stream_name_nbytes);
680 /* Given a UTF-16LE filename and a directory, look up the dentry for the file.
681 * Return it if found, otherwise NULL. This is case-sensitive on UNIX and
682 * case-insensitive on Windows. */
684 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
685 const utf16lechar *name,
688 struct rb_node *node;
691 node = dentry->d_inode->i_children_case_insensitive.rb_node;
693 node = dentry->d_inode->i_children.rb_node;
696 struct wim_dentry *child;
699 child = rb_entry(node, struct wim_dentry, rb_node_case_insensitive);
701 child = rbnode_dentry(node);
703 int result = compare_utf16le_names(name, name_nbytes,
705 child->file_name_nbytes);
707 node = node->rb_left;
709 node = node->rb_right;
712 if (!list_empty(&child->case_insensitive_conflict_list))
714 WARNING("Result of case-insensitive lookup is ambiguous "
715 "(returning \"%ls\" instead of \"%ls\")",
717 container_of(child->case_insensitive_conflict_list.next,
719 case_insensitive_conflict_list)->file_name);
728 /* Returns the child of @dentry that has the file name @name. Returns NULL if
729 * no child has the name. */
731 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
734 return get_dentry_child_with_utf16le_name(dentry, name,
735 tstrlen(name) * sizeof(tchar));
737 utf16lechar *utf16le_name;
738 size_t utf16le_name_nbytes;
740 struct wim_dentry *child;
742 ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
743 &utf16le_name, &utf16le_name_nbytes);
747 child = get_dentry_child_with_utf16le_name(dentry,
749 utf16le_name_nbytes);
756 static struct wim_dentry *
757 get_dentry_utf16le(WIMStruct *wim, const utf16lechar *path)
759 struct wim_dentry *cur_dentry, *parent_dentry;
760 const utf16lechar *p, *pp;
762 cur_dentry = parent_dentry = wim_root_dentry(wim);
769 while (*p == cpu_to_le16(WIM_PATH_SEPARATOR))
771 if (*p == cpu_to_le16('\0'))
774 while (*pp != cpu_to_le16(WIM_PATH_SEPARATOR) &&
775 *pp != cpu_to_le16('\0'))
778 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
779 (void*)pp - (void*)p);
780 if (cur_dentry == NULL)
783 parent_dentry = cur_dentry;
785 if (cur_dentry == NULL) {
786 if (dentry_is_directory(parent_dentry))
795 * Returns the dentry in the currently selected WIM image named by @path
796 * starting from the root of the WIM image, or NULL if there is no such dentry.
798 * On Windows, the search is done case-insensitively.
801 get_dentry(WIMStruct *wim, const tchar *path)
804 return get_dentry_utf16le(wim, path);
806 utf16lechar *path_utf16le;
807 size_t path_utf16le_nbytes;
809 struct wim_dentry *dentry;
811 ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
812 &path_utf16le, &path_utf16le_nbytes);
815 dentry = get_dentry_utf16le(wim, path_utf16le);
822 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
824 struct wim_dentry *dentry;
825 dentry = get_dentry(wim, path);
827 return dentry->d_inode;
832 /* Takes in a path of length @len in @buf, and transforms it into a string for
833 * the path of its parent directory. */
835 to_parent_name(tchar *buf, size_t len)
837 ssize_t i = (ssize_t)len - 1;
838 while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
840 while (i >= 0 && buf[i] != WIM_PATH_SEPARATOR)
842 while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
844 buf[i + 1] = T('\0');
847 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
848 * if the dentry is not found. */
850 get_parent_dentry(WIMStruct *wim, const tchar *path)
852 size_t path_len = tstrlen(path);
853 tchar buf[path_len + 1];
855 tmemcpy(buf, path, path_len + 1);
856 to_parent_name(buf, path_len);
857 return get_dentry(wim, buf);
860 /* Prints the full path of a dentry. */
862 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
864 int ret = calculate_dentry_full_path(dentry);
867 tprintf(T("%"TS"\n"), dentry->_full_path);
871 /* We want to be able to show the names of the file attribute flags that are
873 struct file_attr_flag {
877 struct file_attr_flag file_attr_flags[] = {
878 {FILE_ATTRIBUTE_READONLY, T("READONLY")},
879 {FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
880 {FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
881 {FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
882 {FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
883 {FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
884 {FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
885 {FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
886 {FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
887 {FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
888 {FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
889 {FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
890 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
891 {FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
892 {FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
895 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
896 * available. If the dentry is unresolved and the lookup table is NULL, the
897 * lookup table entries will not be printed. Otherwise, they will be. */
899 print_dentry(struct wim_dentry *dentry, void *lookup_table)
902 struct wim_lookup_table_entry *lte;
903 const struct wim_inode *inode = dentry->d_inode;
906 tprintf(T("[DENTRY]\n"));
907 tprintf(T("Length = %"PRIu64"\n"), dentry->length);
908 tprintf(T("Attributes = 0x%x\n"), inode->i_attributes);
909 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
910 if (file_attr_flags[i].flag & inode->i_attributes)
911 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
912 file_attr_flags[i].name);
913 tprintf(T("Security ID = %d\n"), inode->i_security_id);
914 tprintf(T("Subdir offset = %"PRIu64"\n"), dentry->subdir_offset);
916 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
917 tprintf(T("Creation Time = %"TS"\n"), buf);
919 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
920 tprintf(T("Last Access Time = %"TS"\n"), buf);
922 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
923 tprintf(T("Last Write Time = %"TS"\n"), buf);
925 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
926 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), inode->i_reparse_tag);
927 tprintf(T("Reparse Point Flags = 0x%"PRIx16"\n"),
928 inode->i_not_rpfixed);
929 tprintf(T("Reparse Point Unknown 2 = 0x%"PRIx32"\n"),
930 inode->i_rp_unknown_2);
932 tprintf(T("Reparse Point Unknown 1 = 0x%"PRIx32"\n"),
933 inode->i_rp_unknown_1);
934 tprintf(T("Hard Link Group = 0x%"PRIx64"\n"), inode->i_ino);
935 tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
936 tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
937 if (dentry_has_long_name(dentry))
938 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
939 if (dentry_has_short_name(dentry))
940 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
941 if (dentry->_full_path)
942 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
944 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
946 print_lookup_table_entry(lte, stdout);
948 hash = inode_stream_hash(inode, 0);
950 tprintf(T("Hash = 0x"));
951 print_hash(hash, stdout);
956 for (u16 i = 0; i < inode->i_num_ads; i++) {
957 tprintf(T("[Alternate Stream Entry %u]\n"), i);
958 wimlib_printf(T("Name = \"%"WS"\"\n"),
959 inode->i_ads_entries[i].stream_name);
960 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
961 inode->i_ads_entries[i].stream_name_nbytes);
962 hash = inode_stream_hash(inode, i + 1);
964 tprintf(T("Hash = 0x"));
965 print_hash(hash, stdout);
968 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
974 /* Initializations done on every `struct wim_dentry'. */
976 dentry_common_init(struct wim_dentry *dentry)
978 memset(dentry, 0, sizeof(struct wim_dentry));
982 new_timeless_inode(void)
984 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
986 inode->i_security_id = -1;
988 inode->i_next_stream_id = 1;
989 inode->i_not_rpfixed = 1;
990 INIT_LIST_HEAD(&inode->i_list);
991 INIT_LIST_HEAD(&inode->i_dentry);
996 static struct wim_inode *
999 struct wim_inode *inode = new_timeless_inode();
1001 u64 now = get_wim_timestamp();
1002 inode->i_creation_time = now;
1003 inode->i_last_access_time = now;
1004 inode->i_last_write_time = now;
1009 /* Creates an unlinked directory entry. */
1011 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
1013 struct wim_dentry *dentry;
1016 dentry = MALLOC(sizeof(struct wim_dentry));
1018 return WIMLIB_ERR_NOMEM;
1020 dentry_common_init(dentry);
1021 ret = set_dentry_name(dentry, name);
1023 dentry->parent = dentry;
1024 *dentry_ret = dentry;
1027 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
1035 _new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
1038 struct wim_dentry *dentry;
1041 ret = new_dentry(name, &dentry);
1046 dentry->d_inode = new_timeless_inode();
1048 dentry->d_inode = new_inode();
1049 if (!dentry->d_inode) {
1050 free_dentry(dentry);
1051 return WIMLIB_ERR_NOMEM;
1054 inode_add_dentry(dentry, dentry->d_inode);
1055 *dentry_ret = dentry;
1060 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
1062 return _new_dentry_with_inode(name, dentry_ret, true);
1066 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
1068 return _new_dentry_with_inode(name, dentry_ret, false);
1072 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
1075 struct wim_dentry *dentry;
1077 DEBUG("Creating filler directory \"%"TS"\"", name);
1078 ret = new_dentry_with_inode(name, &dentry);
1081 /* Leave the inode number as 0; this is allowed for non
1082 * hard-linked files. */
1083 dentry->d_inode->i_resolved = 1;
1084 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
1085 *dentry_ret = dentry;
1090 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
1091 size_t name_nbytes, bool is_utf16le)
1094 memset(ads_entry, 0, sizeof(*ads_entry));
1097 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
1099 return WIMLIB_ERR_NOMEM;
1100 memcpy(p, name, name_nbytes);
1101 p[name_nbytes / 2] = cpu_to_le16(0);
1102 ads_entry->stream_name = p;
1103 ads_entry->stream_name_nbytes = name_nbytes;
1105 if (name && *(const tchar*)name != T('\0')) {
1106 ret = get_utf16le_name(name, &ads_entry->stream_name,
1107 &ads_entry->stream_name_nbytes);
1114 destroy_ads_entry(struct wim_ads_entry *ads_entry)
1116 FREE(ads_entry->stream_name);
1119 /* Frees an inode. */
1121 free_inode(struct wim_inode *inode)
1124 if (inode->i_ads_entries) {
1125 for (u16 i = 0; i < inode->i_num_ads; i++)
1126 destroy_ads_entry(&inode->i_ads_entries[i]);
1127 FREE(inode->i_ads_entries);
1129 /* HACK: This may instead delete the inode from i_list, but the
1130 * hlist_del() behaves the same as list_del(). */
1131 if (!hlist_unhashed(&inode->i_hlist))
1132 hlist_del(&inode->i_hlist);
1137 /* Decrements link count on an inode and frees it if the link count reaches 0.
1140 put_inode(struct wim_inode *inode)
1142 wimlib_assert(inode->i_nlink != 0);
1143 if (--inode->i_nlink == 0) {
1145 if (inode->i_num_opened_fds == 0)
1153 /* Frees a WIM dentry.
1155 * The corresponding inode (if any) is freed only if its link count is
1159 free_dentry(struct wim_dentry *dentry)
1162 FREE(dentry->file_name);
1163 FREE(dentry->short_name);
1164 FREE(dentry->_full_path);
1165 if (dentry->d_inode)
1166 put_inode(dentry->d_inode);
1171 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
1172 * to free a directory tree. */
1174 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
1176 struct wim_lookup_table *lookup_table = _lookup_table;
1179 struct wim_inode *inode = dentry->d_inode;
1180 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1181 struct wim_lookup_table_entry *lte;
1183 lte = inode_stream_lte(inode, i, lookup_table);
1185 lte_decrement_refcnt(lte, lookup_table);
1188 free_dentry(dentry);
1193 * Unlinks and frees a dentry tree.
1196 * The root of the tree.
1199 * The lookup table for dentries. If non-NULL, the reference counts in the
1200 * lookup table for the lookup table entries corresponding to the dentries
1201 * will be decremented.
1204 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1206 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1211 /* Insert a dentry into the case insensitive index for a directory.
1213 * This is a red-black tree, but when multiple dentries share the same
1214 * case-insensitive name, only one is inserted into the tree itself; the rest
1215 * are connected in a list.
1217 static struct wim_dentry *
1218 dentry_add_child_case_insensitive(struct wim_dentry *parent,
1219 struct wim_dentry *child)
1221 struct rb_root *root;
1222 struct rb_node **new;
1223 struct rb_node *rb_parent;
1225 root = &parent->d_inode->i_children_case_insensitive;
1226 new = &root->rb_node;
1229 struct wim_dentry *this = container_of(*new, struct wim_dentry,
1230 rb_node_case_insensitive);
1231 int result = dentry_compare_names_case_insensitive(child, this);
1236 new = &((*new)->rb_left);
1237 else if (result > 0)
1238 new = &((*new)->rb_right);
1242 rb_link_node(&child->rb_node_case_insensitive, rb_parent, new);
1243 rb_insert_color(&child->rb_node_case_insensitive, root);
1249 * Links a dentry into the directory tree.
1251 * @parent: The dentry that will be the parent of @child.
1252 * @child: The dentry to link.
1254 * Returns NULL if successful. If @parent already contains a dentry with the
1255 * same case-sensitive name as @child, the pointer to this duplicate dentry is
1259 dentry_add_child(struct wim_dentry * restrict parent,
1260 struct wim_dentry * restrict child)
1262 struct rb_root *root;
1263 struct rb_node **new;
1264 struct rb_node *rb_parent;
1266 wimlib_assert(dentry_is_directory(parent));
1267 wimlib_assert(parent != child);
1269 /* Case sensitive child dentry index */
1270 root = &parent->d_inode->i_children;
1271 new = &root->rb_node;
1274 struct wim_dentry *this = rbnode_dentry(*new);
1275 int result = dentry_compare_names_case_sensitive(child, this);
1280 new = &((*new)->rb_left);
1281 else if (result > 0)
1282 new = &((*new)->rb_right);
1286 child->parent = parent;
1287 rb_link_node(&child->rb_node, rb_parent, new);
1288 rb_insert_color(&child->rb_node, root);
1292 struct wim_dentry *existing;
1293 existing = dentry_add_child_case_insensitive(parent, child);
1295 list_add(&child->case_insensitive_conflict_list,
1296 &existing->case_insensitive_conflict_list);
1297 child->rb_node_case_insensitive.__rb_parent_color = 0;
1299 INIT_LIST_HEAD(&child->case_insensitive_conflict_list);
1306 /* Unlink a WIM dentry from the directory entry tree. */
1308 unlink_dentry(struct wim_dentry *dentry)
1310 struct wim_dentry *parent = dentry->parent;
1312 if (parent == dentry)
1314 rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1316 if (dentry->rb_node_case_insensitive.__rb_parent_color) {
1317 /* This dentry was in the case-insensitive red-black tree. */
1318 rb_erase(&dentry->rb_node_case_insensitive,
1319 &parent->d_inode->i_children_case_insensitive);
1320 if (!list_empty(&dentry->case_insensitive_conflict_list)) {
1321 /* Make a different case-insensitively-the-same dentry
1322 * be the "representative" in the red-black tree. */
1323 struct list_head *next;
1324 struct wim_dentry *other;
1325 struct wim_dentry *existing;
1327 next = dentry->case_insensitive_conflict_list.next;
1328 other = list_entry(next, struct wim_dentry, case_insensitive_conflict_list);
1329 existing = dentry_add_child_case_insensitive(parent, other);
1330 wimlib_assert(existing == NULL);
1333 list_del(&dentry->case_insensitive_conflict_list);
1338 * Returns the alternate data stream entry belonging to @inode that has the
1339 * stream name @stream_name.
1341 struct wim_ads_entry *
1342 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1345 if (inode->i_num_ads == 0) {
1348 size_t stream_name_utf16le_nbytes;
1350 struct wim_ads_entry *result;
1352 #if TCHAR_IS_UTF16LE
1353 const utf16lechar *stream_name_utf16le;
1355 stream_name_utf16le = stream_name;
1356 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1358 utf16lechar *stream_name_utf16le;
1361 int ret = tstr_to_utf16le(stream_name,
1362 tstrlen(stream_name) *
1364 &stream_name_utf16le,
1365 &stream_name_utf16le_nbytes);
1373 if (ads_entry_has_name(&inode->i_ads_entries[i],
1374 stream_name_utf16le,
1375 stream_name_utf16le_nbytes))
1379 result = &inode->i_ads_entries[i];
1382 } while (++i != inode->i_num_ads);
1383 #if !TCHAR_IS_UTF16LE
1384 FREE(stream_name_utf16le);
1390 static struct wim_ads_entry *
1391 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1392 size_t stream_name_nbytes, bool is_utf16le)
1395 struct wim_ads_entry *ads_entries;
1396 struct wim_ads_entry *new_entry;
1398 if (inode->i_num_ads >= 0xfffe) {
1399 ERROR("Too many alternate data streams in one inode!");
1402 num_ads = inode->i_num_ads + 1;
1403 ads_entries = REALLOC(inode->i_ads_entries,
1404 num_ads * sizeof(inode->i_ads_entries[0]));
1406 ERROR("Failed to allocate memory for new alternate data stream");
1409 inode->i_ads_entries = ads_entries;
1411 new_entry = &inode->i_ads_entries[num_ads - 1];
1412 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1414 new_entry->stream_id = inode->i_next_stream_id++;
1415 inode->i_num_ads = num_ads;
1419 struct wim_ads_entry *
1420 inode_add_ads_utf16le(struct wim_inode *inode,
1421 const utf16lechar *stream_name,
1422 size_t stream_name_nbytes)
1424 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1425 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1429 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1430 * NULL if memory could not be allocated.
1432 struct wim_ads_entry *
1433 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1435 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1436 return do_inode_add_ads(inode, stream_name,
1437 tstrlen(stream_name) * sizeof(tchar),
1441 static struct wim_lookup_table_entry *
1442 add_stream_from_data_buffer(const void *buffer, size_t size,
1443 struct wim_lookup_table *lookup_table)
1445 u8 hash[SHA1_HASH_SIZE];
1446 struct wim_lookup_table_entry *lte, *existing_lte;
1448 sha1_buffer(buffer, size, hash);
1449 existing_lte = __lookup_resource(lookup_table, hash);
1451 wimlib_assert(wim_resource_size(existing_lte) == size);
1456 lte = new_lookup_table_entry();
1459 buffer_copy = memdup(buffer, size);
1461 free_lookup_table_entry(lte);
1464 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1465 lte->attached_buffer = buffer_copy;
1466 lte->resource_entry.original_size = size;
1467 copy_hash(lte->hash, hash);
1468 lookup_table_insert(lookup_table, lte);
1474 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1475 const void *value, size_t size,
1476 struct wim_lookup_table *lookup_table)
1478 struct wim_ads_entry *new_ads_entry;
1480 wimlib_assert(inode->i_resolved);
1482 new_ads_entry = inode_add_ads(inode, name);
1484 return WIMLIB_ERR_NOMEM;
1486 new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1488 if (!new_ads_entry->lte) {
1489 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1491 return WIMLIB_ERR_NOMEM;
1497 inode_has_named_stream(const struct wim_inode *inode)
1499 for (u16 i = 0; i < inode->i_num_ads; i++)
1500 if (ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1505 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
1506 * stream contents. */
1508 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
1509 struct wim_lookup_table *lookup_table)
1511 inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
1513 return WIMLIB_ERR_NOMEM;
1514 inode->i_resolved = 1;
1518 /* Remove an alternate data stream from a WIM inode */
1520 inode_remove_ads(struct wim_inode *inode, u16 idx,
1521 struct wim_lookup_table *lookup_table)
1523 struct wim_ads_entry *ads_entry;
1524 struct wim_lookup_table_entry *lte;
1526 wimlib_assert(idx < inode->i_num_ads);
1527 wimlib_assert(inode->i_resolved);
1529 ads_entry = &inode->i_ads_entries[idx];
1531 DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
1533 lte = ads_entry->lte;
1535 lte_decrement_refcnt(lte, lookup_table);
1537 destroy_ads_entry(ads_entry);
1539 memmove(&inode->i_ads_entries[idx],
1540 &inode->i_ads_entries[idx + 1],
1541 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1546 inode_has_unix_data(const struct wim_inode *inode)
1548 for (u16 i = 0; i < inode->i_num_ads; i++)
1549 if (ads_entry_is_unix_data(&inode->i_ads_entries[i]))
1556 inode_get_unix_data(const struct wim_inode *inode,
1557 struct wimlib_unix_data *unix_data,
1558 u16 *stream_idx_ret)
1560 const struct wim_ads_entry *ads_entry;
1561 const struct wim_lookup_table_entry *lte;
1565 wimlib_assert(inode->i_resolved);
1567 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1568 WIMLIB_UNIX_DATA_TAG, NULL);
1570 return NO_UNIX_DATA;
1573 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1575 lte = ads_entry->lte;
1577 return NO_UNIX_DATA;
1579 size = wim_resource_size(lte);
1580 if (size != sizeof(struct wimlib_unix_data))
1581 return BAD_UNIX_DATA;
1583 ret = read_full_resource_into_buf(lte, unix_data);
1587 if (unix_data->version != 0)
1588 return BAD_UNIX_DATA;
1593 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1594 struct wim_lookup_table *lookup_table, int which)
1596 struct wimlib_unix_data unix_data;
1598 bool have_good_unix_data = false;
1599 bool have_unix_data = false;
1602 if (!(which & UNIX_DATA_CREATE)) {
1603 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1604 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1605 have_unix_data = true;
1607 have_good_unix_data = true;
1609 unix_data.version = 0;
1610 if (which & UNIX_DATA_UID || !have_good_unix_data)
1611 unix_data.uid = uid;
1612 if (which & UNIX_DATA_GID || !have_good_unix_data)
1613 unix_data.gid = gid;
1614 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1615 unix_data.mode = mode;
1616 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1618 sizeof(struct wimlib_unix_data),
1620 if (ret == 0 && have_unix_data)
1621 inode_remove_ads(inode, stream_idx, lookup_table);
1624 #endif /* !__WIN32__ */
1627 * Reads the alternate data stream entries of a WIM dentry.
1630 * Pointer to buffer that starts with the first alternate stream entry.
1633 * Inode to load the alternate data streams into. @inode->i_num_ads must
1634 * have been set to the number of alternate data streams that are expected.
1637 * Number of bytes of data remaining in the buffer pointed to by @p.
1639 * On success, inode->i_ads_entries is set to an array of `struct
1640 * wim_ads_entry's of length inode->i_num_ads. On failure, @inode is not
1644 * WIMLIB_ERR_SUCCESS (0)
1645 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
1649 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1650 size_t nbytes_remaining)
1653 struct wim_ads_entry *ads_entries;
1656 BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
1658 /* Allocate an array for our in-memory representation of the alternate
1659 * data stream entries. */
1660 num_ads = inode->i_num_ads;
1661 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1665 /* Read the entries into our newly allocated buffer. */
1666 for (u16 i = 0; i < num_ads; i++) {
1668 struct wim_ads_entry *cur_entry;
1669 const struct wim_ads_entry_on_disk *disk_entry =
1670 (const struct wim_ads_entry_on_disk*)p;
1672 cur_entry = &ads_entries[i];
1673 ads_entries[i].stream_id = i + 1;
1675 /* Do we have at least the size of the fixed-length data we know
1677 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1680 /* Read the length field */
1681 length = le64_to_cpu(disk_entry->length);
1683 /* Make sure the length field is neither so small it doesn't
1684 * include all the fixed-length data nor so large it overflows
1685 * the metadata resource buffer. */
1686 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1687 length > nbytes_remaining)
1690 /* Read the rest of the fixed-length data. */
1692 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1693 copy_hash(cur_entry->hash, disk_entry->hash);
1694 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1696 /* If stream_name_nbytes != 0, this is a named stream.
1697 * Otherwise this is an unnamed stream, or in some cases (bugs
1698 * in Microsoft's software I guess) a meaningless entry
1699 * distinguished from the real unnamed stream entry, if any, by
1700 * the fact that the real unnamed stream entry has a nonzero
1702 if (cur_entry->stream_name_nbytes) {
1703 /* The name is encoded in UTF16-LE, which uses 2-byte
1704 * coding units, so the length of the name had better be
1705 * an even number of bytes... */
1706 if (cur_entry->stream_name_nbytes & 1)
1709 /* Add the length of the stream name to get the length
1710 * we actually need to read. Make sure this isn't more
1711 * than the specified length of the entry. */
1712 if (sizeof(struct wim_ads_entry_on_disk) +
1713 cur_entry->stream_name_nbytes > length)
1716 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1717 if (!cur_entry->stream_name)
1720 memcpy(cur_entry->stream_name,
1721 disk_entry->stream_name,
1722 cur_entry->stream_name_nbytes);
1723 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1726 /* It's expected that the size of every ADS entry is a multiple
1727 * of 8. However, to be safe, I'm allowing the possibility of
1728 * an ADS entry at the very end of the metadata resource ending
1729 * un-aligned. So although we still need to increment the input
1730 * pointer by @length to reach the next ADS entry, it's possible
1731 * that less than @length is actually remaining in the metadata
1732 * resource. We should set the remaining bytes to 0 if this
1734 length = (length + 7) & ~(u64)7;
1736 if (nbytes_remaining < length)
1737 nbytes_remaining = 0;
1739 nbytes_remaining -= length;
1741 inode->i_ads_entries = ads_entries;
1742 inode->i_next_stream_id = inode->i_num_ads + 1;
1746 ret = WIMLIB_ERR_NOMEM;
1747 goto out_free_ads_entries;
1749 ERROR("An alternate data stream entry is invalid");
1750 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1751 out_free_ads_entries:
1753 for (u16 i = 0; i < num_ads; i++)
1754 destroy_ads_entry(&ads_entries[i]);
1762 * Reads a WIM directory entry, including all alternate data stream entries that
1763 * follow it, from the WIM image's metadata resource.
1765 * @metadata_resource:
1766 * Pointer to the metadata resource buffer.
1768 * @metadata_resource_len:
1769 * Length of the metadata resource buffer, in bytes.
1771 * @offset: Offset of the dentry within the metadata resource.
1773 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1775 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1776 * been modified, but it will not be left with pointers to any allocated
1777 * buffers. On success, the dentry->length field must be examined. If zero,
1778 * this was a special "end of directory" dentry and not a real dentry. If
1779 * nonzero, this was a real dentry.
1782 * WIMLIB_ERR_SUCCESS (0)
1783 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
1787 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1788 u64 offset, struct wim_dentry * restrict dentry)
1791 u64 calculated_size;
1792 utf16lechar *file_name;
1793 utf16lechar *short_name;
1794 u16 short_name_nbytes;
1795 u16 file_name_nbytes;
1797 struct wim_inode *inode;
1798 const u8 *p = &metadata_resource[offset];
1799 const struct wim_dentry_on_disk *disk_dentry =
1800 (const struct wim_dentry_on_disk*)p;
1802 BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE);
1804 if ((uintptr_t)p & 7)
1805 WARNING("WIM dentry is not 8-byte aligned");
1807 dentry_common_init(dentry);
1809 /* Before reading the whole dentry, we need to read just the length.
1810 * This is because a dentry of length 8 (that is, just the length field)
1811 * terminates the list of sibling directory entries. */
1812 if (offset + sizeof(u64) > metadata_resource_len ||
1813 offset + sizeof(u64) < offset)
1815 ERROR("Directory entry starting at %"PRIu64" ends past the "
1816 "end of the metadata resource (size %"PRIu64")",
1817 offset, metadata_resource_len);
1818 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1820 dentry->length = le64_to_cpu(disk_dentry->length);
1822 /* A zero length field (really a length of 8, since that's how big the
1823 * directory entry is...) indicates that this is the end of directory
1824 * dentry. We do not read it into memory as an actual dentry, so just
1825 * return successfully in this case. */
1826 if (dentry->length == 8)
1828 if (dentry->length == 0)
1831 /* Now that we have the actual length provided in the on-disk structure,
1832 * again make sure it doesn't overflow the metadata resource buffer. */
1833 if (offset + dentry->length > metadata_resource_len ||
1834 offset + dentry->length < offset)
1836 ERROR("Directory entry at offset %"PRIu64" and with size "
1837 "%"PRIu64" ends past the end of the metadata resource "
1839 offset, dentry->length, metadata_resource_len);
1840 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1843 /* Make sure the dentry length is at least as large as the number of
1844 * fixed-length fields */
1845 if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1846 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1848 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1851 /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1852 inode = new_timeless_inode();
1854 return WIMLIB_ERR_NOMEM;
1856 /* Read more fields; some into the dentry, and some into the inode. */
1858 inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1859 inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1860 dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1861 dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1862 dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1863 inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1864 inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1865 inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1866 copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1868 /* I don't know what's going on here. It seems like M$ screwed up the
1869 * reparse points, then put the fields in the same place and didn't
1870 * document it. So we have some fields we read for reparse points, and
1871 * some fields in the same place for non-reparse-point.s */
1872 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1873 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1874 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1875 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1876 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1877 /* Leave inode->i_ino at 0. Note that this means the WIM file
1878 * cannot archive hard-linked reparse points. Such a thing
1879 * doesn't really make sense anyway, although I believe it's
1880 * theoretically possible to have them on NTFS. */
1882 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1883 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1886 inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1888 short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1889 file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1891 if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1893 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1894 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1895 goto out_free_inode;
1898 /* We now know the length of the file name and short name. Make sure
1899 * the length of the dentry is large enough to actually hold them.
1901 * The calculated length here is unaligned to allow for the possibility
1902 * that the dentry->length names an unaligned length, although this
1903 * would be unexpected. */
1904 calculated_size = _dentry_correct_length_unaligned(file_name_nbytes,
1907 if (dentry->length < calculated_size) {
1908 ERROR("Unexpected end of directory entry! (Expected "
1909 "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1910 calculated_size, dentry->length);
1911 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1912 goto out_free_inode;
1915 p += sizeof(struct wim_dentry_on_disk);
1917 /* Read the filename if present. Note: if the filename is empty, there
1918 * is no null terminator following it. */
1919 if (file_name_nbytes) {
1920 file_name = MALLOC(file_name_nbytes + 2);
1922 ERROR("Failed to allocate %d bytes for dentry file name",
1923 file_name_nbytes + 2);
1924 ret = WIMLIB_ERR_NOMEM;
1925 goto out_free_inode;
1927 memcpy(file_name, p, file_name_nbytes);
1928 p += file_name_nbytes + 2;
1929 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
1935 /* Read the short filename if present. Note: if there is no short
1936 * filename, there is no null terminator following it. */
1937 if (short_name_nbytes) {
1938 short_name = MALLOC(short_name_nbytes + 2);
1940 ERROR("Failed to allocate %d bytes for dentry short name",
1941 short_name_nbytes + 2);
1942 ret = WIMLIB_ERR_NOMEM;
1943 goto out_free_file_name;
1945 memcpy(short_name, p, short_name_nbytes);
1946 p += short_name_nbytes + 2;
1947 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
1952 /* Align the dentry length */
1953 dentry->length = (dentry->length + 7) & ~7;
1956 * Read the alternate data streams, if present. dentry->num_ads tells
1957 * us how many they are, and they will directly follow the dentry
1960 * Note that each alternate data stream entry begins on an 8-byte
1961 * aligned boundary, and the alternate data stream entries seem to NOT
1962 * be included in the dentry->length field for some reason.
1964 if (inode->i_num_ads != 0) {
1965 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
1966 if (offset + dentry->length > metadata_resource_len ||
1967 (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
1969 metadata_resource_len - offset - dentry->length)))
1971 ERROR("Failed to read alternate data stream "
1972 "entries of WIM dentry \"%"WS"\"", file_name);
1973 goto out_free_short_name;
1976 /* We've read all the data for this dentry. Set the names and their
1977 * lengths, and we've done. */
1978 dentry->d_inode = inode;
1979 dentry->file_name = file_name;
1980 dentry->short_name = short_name;
1981 dentry->file_name_nbytes = file_name_nbytes;
1982 dentry->short_name_nbytes = short_name_nbytes;
1985 out_free_short_name:
1995 static const tchar *
1996 dentry_get_file_type_string(const struct wim_dentry *dentry)
1998 const struct wim_inode *inode = dentry->d_inode;
1999 if (inode_is_directory(inode))
2000 return T("directory");
2001 else if (inode_is_symlink(inode))
2002 return T("symbolic link");
2007 /* Reads the children of a dentry, and all their children, ..., etc. from the
2008 * metadata resource and into the dentry tree.
2010 * @metadata_resource:
2011 * An array that contains the uncompressed metadata resource for the WIM
2014 * @metadata_resource_len:
2015 * The length of the uncompressed metadata resource, in bytes.
2018 * A pointer to a `struct wim_dentry' that is the root of the directory
2019 * tree and has already been read from the metadata resource. It does not
2020 * need to be the real root because this procedure is called recursively.
2023 * WIMLIB_ERR_SUCCESS (0)
2024 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
2028 read_dentry_tree(const u8 * restrict metadata_resource,
2029 u64 metadata_resource_len,
2030 struct wim_dentry * restrict dentry)
2032 u64 cur_offset = dentry->subdir_offset;
2033 struct wim_dentry *child;
2034 struct wim_dentry *duplicate;
2035 struct wim_dentry *parent;
2036 struct wim_dentry cur_child;
2040 * If @dentry has no child dentries, nothing more needs to be done for
2041 * this branch. This is the case for regular files, symbolic links, and
2042 * *possibly* empty directories (although an empty directory may also
2043 * have one child dentry that is the special end-of-directory dentry)
2045 if (cur_offset == 0)
2048 /* Check for cyclic directory structure */
2049 for (parent = dentry->parent; !dentry_is_root(parent); parent = parent->parent)
2051 if (unlikely(parent->subdir_offset == cur_offset)) {
2052 ERROR("Cyclic directory structure directed: children "
2053 "of \"%"TS"\" coincide with children of \"%"TS"\"",
2054 dentry_full_path(dentry),
2055 dentry_full_path(parent));
2056 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
2060 /* Find and read all the children of @dentry. */
2063 /* Read next child of @dentry into @cur_child. */
2064 ret = read_dentry(metadata_resource, metadata_resource_len,
2065 cur_offset, &cur_child);
2069 /* Check for end of directory. */
2070 if (cur_child.length == 0)
2073 /* Not end of directory. Allocate this child permanently and
2074 * link it to the parent and previous child. */
2075 child = memdup(&cur_child, sizeof(struct wim_dentry));
2077 ERROR("Failed to allocate new dentry!");
2078 ret = WIMLIB_ERR_NOMEM;
2082 /* Advance to the offset of the next child. Note: We need to
2083 * advance by the TOTAL length of the dentry, not by the length
2084 * cur_child.length, which although it does take into account
2085 * the padding, it DOES NOT take into account alternate stream
2087 cur_offset += dentry_total_length(child);
2089 if (unlikely(!dentry_has_long_name(child))) {
2090 WARNING("Ignoring unnamed dentry in "
2091 "directory \"%"TS"\"",
2092 dentry_full_path(dentry));
2097 duplicate = dentry_add_child(dentry, child);
2098 if (unlikely(duplicate)) {
2099 const tchar *child_type, *duplicate_type;
2100 child_type = dentry_get_file_type_string(child);
2101 duplicate_type = dentry_get_file_type_string(duplicate);
2102 WARNING("Ignoring duplicate %"TS" \"%"TS"\" "
2103 "(the WIM image already contains a %"TS" "
2104 "at that path with the exact same name)",
2105 child_type, dentry_full_path(duplicate),
2111 inode_add_dentry(child, child->d_inode);
2112 /* If there are children of this child, call this
2113 * procedure recursively. */
2114 if (child->subdir_offset != 0) {
2115 if (likely(dentry_is_directory(child))) {
2116 ret = read_dentry_tree(metadata_resource,
2117 metadata_resource_len,
2122 WARNING("Ignoring children of non-directory \"%"TS"\"",
2123 dentry_full_path(child));
2131 * Writes a WIM dentry to an output buffer.
2133 * @dentry: The dentry structure.
2134 * @p: The memory location to write the data to.
2136 * Returns the pointer to the byte after the last byte we wrote as part of the
2137 * dentry, including any alternate data stream entries.
2140 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
2142 const struct wim_inode *inode;
2143 struct wim_dentry_on_disk *disk_dentry;
2147 wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
2150 inode = dentry->d_inode;
2151 disk_dentry = (struct wim_dentry_on_disk*)p;
2153 disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
2154 disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
2155 disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
2156 disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
2157 disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
2158 disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
2159 disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
2160 disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
2161 hash = inode_stream_hash(inode, 0);
2162 copy_hash(disk_dentry->unnamed_stream_hash, hash);
2163 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2164 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2165 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
2166 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
2167 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
2169 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2170 disk_dentry->nonreparse.hard_link_group_id =
2171 cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
2173 disk_dentry->num_alternate_data_streams = cpu_to_le16(inode->i_num_ads);
2174 disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
2175 disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
2176 p += sizeof(struct wim_dentry_on_disk);
2178 wimlib_assert(dentry_is_root(dentry) != dentry_has_long_name(dentry));
2180 if (dentry_has_long_name(dentry))
2181 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
2183 if (dentry_has_short_name(dentry))
2184 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
2186 /* Align to 8-byte boundary */
2187 while ((uintptr_t)p & 7)
2190 /* We calculate the correct length of the dentry ourselves because the
2191 * dentry->length field may been set to an unexpected value from when we
2192 * read the dentry in (for example, there may have been unknown data
2193 * appended to the end of the dentry...). Furthermore, the dentry may
2194 * have been renamed, thus changing its needed length. */
2195 disk_dentry->length = cpu_to_le64(p - orig_p);
2197 /* Write the alternate data streams entries, if any. */
2198 for (u16 i = 0; i < inode->i_num_ads; i++) {
2199 const struct wim_ads_entry *ads_entry =
2200 &inode->i_ads_entries[i];
2201 struct wim_ads_entry_on_disk *disk_ads_entry =
2202 (struct wim_ads_entry_on_disk*)p;
2205 disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
2207 hash = inode_stream_hash(inode, i + 1);
2208 copy_hash(disk_ads_entry->hash, hash);
2209 disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
2210 p += sizeof(struct wim_ads_entry_on_disk);
2211 if (ads_entry->stream_name_nbytes) {
2212 p = mempcpy(p, ads_entry->stream_name,
2213 ads_entry->stream_name_nbytes + 2);
2215 /* Align to 8-byte boundary */
2216 while ((uintptr_t)p & 7)
2218 disk_ads_entry->length = cpu_to_le64(p - orig_p);
2224 write_dentry_cb(struct wim_dentry *dentry, void *_p)
2227 *p = write_dentry(dentry, *p);
2232 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
2235 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
2238 *p = write_dentry_tree_recursive(dentry, *p);
2242 /* Recursive function that writes a dentry tree rooted at @parent, not including
2243 * @parent itself, which has already been written. */
2245 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
2247 /* Nothing to do if this dentry has no children. */
2248 if (parent->subdir_offset == 0)
2251 /* Write child dentries and end-of-directory entry.
2253 * Note: we need to write all of this dentry's children before
2254 * recursively writing the directory trees rooted at each of the child
2255 * dentries, since the on-disk dentries for a dentry's children are
2256 * always located at consecutive positions in the metadata resource! */
2257 for_dentry_child(parent, write_dentry_cb, &p);
2259 /* write end of directory entry */
2260 *(le64*)p = cpu_to_le64(0);
2263 /* Recurse on children. */
2264 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
2268 /* Writes a directory tree to the metadata resource.
2270 * @root: Root of the dentry tree.
2271 * @p: Pointer to a buffer with enough space for the dentry tree.
2273 * Returns pointer to the byte after the last byte we wrote.
2276 write_dentry_tree(const struct wim_dentry * restrict root, u8 * restrict p)
2278 DEBUG("Writing dentry tree.");
2279 wimlib_assert(dentry_is_root(root));
2281 /* If we're the root dentry, we have no parent that already
2282 * wrote us, so we need to write ourselves. */
2283 p = write_dentry(root, p);
2285 /* Write end of directory entry after the root dentry just to be safe;
2286 * however the root dentry obviously cannot have any siblings. */
2287 *(le64*)p = cpu_to_le64(0);
2290 /* Recursively write the rest of the dentry tree. */
2291 return write_dentry_tree_recursive(root, p);
2296 init_wimlib_dentry(struct wimlib_dir_entry *wdentry,
2297 struct wim_dentry *dentry,
2298 const WIMStruct *wim)
2302 const struct wim_inode *inode = dentry->d_inode;
2303 struct wim_lookup_table_entry *lte;
2305 #if TCHAR_IS_UTF16LE
2306 wdentry->filename = dentry->file_name;
2307 wdentry->dos_name = dentry->short_name;
2309 if (dentry_has_long_name(dentry)) {
2310 ret = utf16le_to_tstr(dentry->file_name,
2311 dentry->file_name_nbytes,
2312 (tchar**)&wdentry->filename,
2317 if (dentry_has_short_name(dentry)) {
2318 ret = utf16le_to_tstr(dentry->short_name,
2319 dentry->short_name_nbytes,
2320 (tchar**)&wdentry->dos_name,
2326 ret = calculate_dentry_full_path(dentry);
2329 wdentry->full_path = dentry->_full_path;
2331 for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->parent)
2334 if (inode->i_security_id >= 0) {
2335 const struct wim_security_data *sd = wim_const_security_data(wim);
2336 wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
2337 wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
2339 wdentry->reparse_tag = inode->i_reparse_tag;
2340 wdentry->num_links = inode->i_nlink;
2341 wdentry->attributes = inode->i_attributes;
2342 wdentry->hard_link_group_id = inode->i_ino;
2343 wdentry->creation_time = wim_timestamp_to_timespec(inode->i_creation_time);
2344 wdentry->last_write_time = wim_timestamp_to_timespec(inode->i_last_write_time);
2345 wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
2347 lte = inode_unnamed_lte(inode, wim->lookup_table);
2349 lte_to_wimlib_resource_entry(lte, &wdentry->streams[0].resource);
2351 for (unsigned i = 0; i < inode->i_num_ads; i++) {
2352 if (inode->i_ads_entries[i].stream_name == NULL)
2354 lte = inode_stream_lte(inode, i + 1, wim->lookup_table);
2355 wdentry->num_named_streams++;
2357 lte_to_wimlib_resource_entry(lte, &wdentry->streams[
2358 wdentry->num_named_streams].resource);
2360 #if TCHAR_IS_UTF16LE
2361 wdentry->streams[wdentry->num_named_streams].stream_name =
2362 inode->i_ads_entries[i].stream_name;
2366 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
2367 inode->i_ads_entries[i].stream_name_nbytes,
2368 (tchar**)&wdentry->streams[
2369 wdentry->num_named_streams].stream_name,
2379 free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
2381 #if !TCHAR_IS_UTF16LE
2382 FREE((tchar*)wdentry->filename);
2383 FREE((tchar*)wdentry->dos_name);
2384 for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
2385 FREE((tchar*)wdentry->streams[i].stream_name);
2390 struct iterate_dir_tree_ctx {
2393 wimlib_iterate_dir_tree_callback_t cb;
2398 do_iterate_dir_tree(WIMStruct *wim,
2399 struct wim_dentry *dentry, int flags,
2400 wimlib_iterate_dir_tree_callback_t cb,
2404 call_do_iterate_dir_tree(struct wim_dentry *dentry, void *_ctx)
2406 struct iterate_dir_tree_ctx *ctx = _ctx;
2407 return do_iterate_dir_tree(ctx->wim, dentry, ctx->flags,
2408 ctx->cb, ctx->user_ctx);
2412 do_iterate_dir_tree(WIMStruct *wim,
2413 struct wim_dentry *dentry, int flags,
2414 wimlib_iterate_dir_tree_callback_t cb,
2417 struct wimlib_dir_entry *wdentry;
2418 int ret = WIMLIB_ERR_NOMEM;
2421 wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
2422 (1 + dentry->d_inode->i_num_ads) *
2423 sizeof(struct wimlib_stream_entry));
2427 ret = init_wimlib_dentry(wdentry, dentry, wim);
2429 goto out_free_wimlib_dentry;
2431 if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
2432 ret = (*cb)(wdentry, user_ctx);
2434 goto out_free_wimlib_dentry;
2437 if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
2438 WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
2440 struct iterate_dir_tree_ctx ctx = {
2442 .flags = flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
2444 .user_ctx = user_ctx,
2446 ret = for_dentry_child(dentry, call_do_iterate_dir_tree, &ctx);
2448 out_free_wimlib_dentry:
2449 free_wimlib_dentry(wdentry);
2454 struct image_iterate_dir_tree_ctx {
2457 wimlib_iterate_dir_tree_callback_t cb;
2463 image_do_iterate_dir_tree(WIMStruct *wim)
2465 struct image_iterate_dir_tree_ctx *ctx = wim->private;
2466 struct wim_dentry *dentry;
2468 dentry = get_dentry(wim, ctx->path);
2470 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
2471 return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
2474 /* API function documented in wimlib.h */
2476 wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *path,
2478 wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
2480 struct image_iterate_dir_tree_ctx ctx = {
2484 .user_ctx = user_ctx,
2486 wim->private = &ctx;
2487 return for_image(wim, image, image_do_iterate_dir_tree);