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/sha1.h"
42 #include "wimlib/timestamp.h"
46 /* WIM alternate data stream entry (on-disk format) */
47 struct wim_ads_entry_on_disk {
48 /* Length of the entry, in bytes. This apparently includes all
49 * fixed-length fields, plus the stream name and null terminator if
50 * present, and the padding up to an 8 byte boundary. wimlib is a
51 * little less strict when reading the entries, and only requires that
52 * the number of bytes from this field is at least as large as the size
53 * of the fixed length fields and stream name without null terminator.
59 /* SHA1 message digest of the uncompressed stream; or, alternatively,
60 * can be all zeroes if the stream has zero length. */
61 u8 hash[SHA1_HASH_SIZE];
63 /* Length of the stream name, in bytes. 0 if the stream is unnamed. */
64 le16 stream_name_nbytes;
66 /* Stream name in UTF-16LE. It is @stream_name_nbytes bytes long,
67 * excluding the the null terminator. There is a null terminator
68 * character if @stream_name_nbytes != 0; i.e., if this stream is named.
70 utf16lechar stream_name[];
73 #define WIM_ADS_ENTRY_DISK_SIZE 38
75 /* On-disk format of a WIM dentry (directory entry), located in the metadata
76 * resource for a WIM image. */
77 struct wim_dentry_on_disk {
79 /* Length of this directory entry in bytes, not including any alternate
80 * data stream entries. Should be a multiple of 8 so that the following
81 * dentry or alternate data stream entry is aligned on an 8-byte
82 * boundary. (If not, wimlib will round it up.) It must be at least as
83 * long as the fixed-length fields of the dentry (WIM_DENTRY_DISK_SIZE),
84 * plus the lengths of the file name and/or short name if present.
86 * It is also possible for this field to be 0. This situation, which is
87 * undocumented, indicates the end of a list of sibling nodes in a
88 * directory. It also means the real length is 8, because the dentry
89 * included only the length field, but that takes up 8 bytes. */
92 /* Attributes of the file or directory. This is a bitwise OR of the
93 * FILE_ATTRIBUTE_* constants and should correspond to the value
94 * retrieved by GetFileAttributes() on Windows. */
97 /* A value that specifies the security descriptor for this file or
98 * directory. If -1, the file or directory has no security descriptor.
99 * Otherwise, it is a 0-based index into the WIM image's table of
100 * security descriptors (see: `struct wim_security_data') */
103 /* Offset, in bytes, from the start of the uncompressed metadata
104 * resource of this directory's child directory entries, or 0 if this
105 * directory entry does not correspond to a directory or otherwise does
106 * not have any children. */
109 /* Reserved fields */
113 /* The following three time fields should correspond to those gotten by
114 * calling GetFileTime() on Windows. */
116 /* Creation time, in 100-nanosecond intervals since January 1, 1601. */
119 /* Last access time, in 100-nanosecond intervals since January 1, 1601. */
120 le64 last_access_time;
122 /* Last write time, in 100-nanosecond intervals since January 1, 1601. */
123 le64 last_write_time;
125 /* Vaguely, the SHA-1 message digest ("hash") of the file's contents.
126 * More specifically, this is for the "unnamed data stream" rather than
127 * any "alternate data streams". This hash value is used to look up the
128 * corresponding entry in the WIM's stream lookup table to actually find
129 * the file contents within the WIM.
131 * If the file has no unnamed data stream (e.g. is a directory), then
132 * this field will be all zeroes. If the unnamed data stream is empty
133 * (i.e. an "empty file"), then this field is also expected to be all
134 * zeroes. (It will be if wimlib created the WIM image, at least;
135 * otherwise it can't be ruled out that the SHA-1 message digest of 0
136 * bytes of data is given explicitly.)
138 * If the file has reparse data, then this field will instead specify
139 * the SHA-1 message digest of the reparse data. If it is somehow
140 * possible for a file to have both an unnamed data stream and reparse
141 * data, then this is not handled by wimlib.
143 * As a further special case, if this field is all zeroes but there is
144 * an alternate data stream entry with no name and a nonzero SHA-1
145 * message digest field, then that hash must be used instead of this
146 * one. (wimlib does not use this quirk on WIM images it creates.)
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 /* Follewed 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_unaligned(const struct wim_dentry *dentry)
239 return _dentry_correct_length_unaligned(dentry->file_name_nbytes,
240 dentry->short_name_nbytes);
243 /* Duplicates a string of system-dependent encoding into a UTF-16LE string and
244 * returns the string and its length, in bytes, in the pointer arguments. Frees
245 * any existing string at the return location before overwriting it. */
247 get_utf16le_name(const tchar *name, utf16lechar **name_utf16le_ret,
248 u16 *name_utf16le_nbytes_ret)
250 utf16lechar *name_utf16le;
251 size_t name_utf16le_nbytes;
254 name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
255 name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
257 return WIMLIB_ERR_NOMEM;
258 memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
262 ret = tstr_to_utf16le(name, tstrlen(name), &name_utf16le,
263 &name_utf16le_nbytes);
265 if (name_utf16le_nbytes > 0xffff) {
267 ERROR("Multibyte string \"%"TS"\" is too long!", name);
268 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
273 FREE(*name_utf16le_ret);
274 *name_utf16le_ret = name_utf16le;
275 *name_utf16le_nbytes_ret = name_utf16le_nbytes;
280 /* Sets the name of a WIM dentry from a multibyte string. */
282 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name)
285 ret = get_utf16le_name(new_name, &dentry->file_name,
286 &dentry->file_name_nbytes);
288 /* Clear the short name and recalculate the dentry length */
289 if (dentry_has_short_name(dentry)) {
290 FREE(dentry->short_name);
291 dentry->short_name = NULL;
292 dentry->short_name_nbytes = 0;
298 /* Returns the total length of a WIM alternate data stream entry on-disk,
299 * including the stream name, the null terminator, AND the padding after the
300 * entry to align the next ADS entry or dentry on an 8-byte boundary. */
302 ads_entry_total_length(const struct wim_ads_entry *entry)
304 u64 len = sizeof(struct wim_ads_entry_on_disk);
305 if (entry->stream_name_nbytes)
306 len += entry->stream_name_nbytes + 2;
307 return (len + 7) & ~7;
312 _dentry_total_length(const struct wim_dentry *dentry, u64 length)
314 const struct wim_inode *inode = dentry->d_inode;
315 for (u16 i = 0; i < inode->i_num_ads; i++)
316 length += ads_entry_total_length(&inode->i_ads_entries[i]);
317 return (length + 7) & ~7;
320 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
321 * all alternate data streams. */
323 dentry_correct_total_length(const struct wim_dentry *dentry)
325 return _dentry_total_length(dentry,
326 dentry_correct_length_unaligned(dentry));
329 /* Like dentry_correct_total_length(), but use the existing dentry->length field
330 * instead of calculating its "correct" value. */
332 dentry_total_length(const struct wim_dentry *dentry)
334 return _dentry_total_length(dentry, dentry->length);
338 for_dentry_in_rbtree(struct rb_node *root,
339 int (*visitor)(struct wim_dentry *, void *),
343 struct rb_node *node = root;
347 list_add(&rbnode_dentry(node)->tmp_list, &stack);
348 node = node->rb_left;
350 struct list_head *next;
351 struct wim_dentry *dentry;
356 dentry = container_of(next, struct wim_dentry, tmp_list);
358 ret = visitor(dentry, arg);
361 node = dentry->rb_node.rb_right;
367 for_dentry_tree_in_rbtree_depth(struct rb_node *node,
368 int (*visitor)(struct wim_dentry*, void*),
373 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
377 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
381 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
389 for_dentry_tree_in_rbtree(struct rb_node *node,
390 int (*visitor)(struct wim_dentry*, void*),
395 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
398 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
401 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
408 /* Calls a function on all directory entries in a WIM dentry tree. Logically,
409 * this is a pre-order traversal (the function is called on a parent dentry
410 * before its children), but sibling dentries will be visited in order as well.
413 for_dentry_in_tree(struct wim_dentry *root,
414 int (*visitor)(struct wim_dentry*, void*), void *arg)
420 ret = (*visitor)(root, arg);
423 return for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node,
428 /* Like for_dentry_in_tree(), but the visitor function is always called on a
429 * dentry's children before on itself. */
431 for_dentry_in_tree_depth(struct wim_dentry *root,
432 int (*visitor)(struct wim_dentry*, void*), void *arg)
438 ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
442 return (*visitor)(root, arg);
445 /* Calculate the full path of @dentry. The full path of its parent must have
446 * already been calculated, or it must be the root dentry. */
448 calculate_dentry_full_path(struct wim_dentry *dentry)
451 u32 full_path_nbytes;
454 if (dentry->_full_path)
457 if (dentry_is_root(dentry)) {
458 static const tchar _root_path[] = {WIM_PATH_SEPARATOR, T('\0')};
459 full_path = TSTRDUP(_root_path);
461 return WIMLIB_ERR_NOMEM;
462 full_path_nbytes = 1 * sizeof(tchar);
464 struct wim_dentry *parent;
465 tchar *parent_full_path;
466 u32 parent_full_path_nbytes;
467 size_t filename_nbytes;
469 parent = dentry->parent;
470 if (dentry_is_root(parent)) {
471 parent_full_path = T("");
472 parent_full_path_nbytes = 0;
474 if (!parent->_full_path) {
475 ret = calculate_dentry_full_path(parent);
479 parent_full_path = parent->_full_path;
480 parent_full_path_nbytes = parent->full_path_nbytes;
483 /* Append this dentry's name as a tchar string to the full path
484 * of the parent followed by the path separator */
486 filename_nbytes = dentry->file_name_nbytes;
489 int ret = utf16le_to_tstr_nbytes(dentry->file_name,
490 dentry->file_name_nbytes,
497 full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
499 full_path = MALLOC(full_path_nbytes + sizeof(tchar));
501 return WIMLIB_ERR_NOMEM;
502 memcpy(full_path, parent_full_path, parent_full_path_nbytes);
503 full_path[parent_full_path_nbytes / sizeof(tchar)] = WIM_PATH_SEPARATOR;
505 memcpy(&full_path[parent_full_path_nbytes / sizeof(tchar) + 1],
507 filename_nbytes + sizeof(tchar));
509 utf16le_to_tstr_buf(dentry->file_name,
510 dentry->file_name_nbytes,
511 &full_path[parent_full_path_nbytes /
515 dentry->_full_path = full_path;
516 dentry->full_path_nbytes= full_path_nbytes;
521 do_calculate_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
523 return calculate_dentry_full_path(dentry);
527 calculate_dentry_tree_full_paths(struct wim_dentry *root)
529 return for_dentry_in_tree(root, do_calculate_dentry_full_path, NULL);
533 dentry_full_path(struct wim_dentry *dentry)
535 calculate_dentry_full_path(dentry);
536 return dentry->_full_path;
540 increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
542 *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
547 call_calculate_subdir_offsets(struct wim_dentry *dentry, void *subdir_offset_p)
549 calculate_subdir_offsets(dentry, subdir_offset_p);
554 * Recursively calculates the subdir offsets for a directory tree.
556 * @dentry: The root of the directory tree.
557 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
558 * offset for @dentry.
561 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
563 struct rb_node *node;
565 dentry->subdir_offset = *subdir_offset_p;
566 node = dentry->d_inode->i_children.rb_node;
568 /* Advance the subdir offset by the amount of space the children
569 * of this dentry take up. */
570 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
572 /* End-of-directory dentry on disk. */
573 *subdir_offset_p += 8;
575 /* Recursively call calculate_subdir_offsets() on all the
577 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
579 /* On disk, childless directories have a valid subdir_offset
580 * that points to an 8-byte end-of-directory dentry. Regular
581 * files or reparse points have a subdir_offset of 0. */
582 if (dentry_is_directory(dentry))
583 *subdir_offset_p += 8;
585 dentry->subdir_offset = 0;
589 /* Case-sensitive UTF-16LE dentry or stream name comparison. Used on both UNIX
590 * (always) and Windows (sometimes) */
592 compare_utf16le_names_case_sensitive(const utf16lechar *name1, size_t nbytes1,
593 const utf16lechar *name2, size_t nbytes2)
595 /* Return the result if the strings differ up to their minimum length.
596 * Note that we cannot use strcmp() or strncmp() here, as the strings
597 * are in UTF-16LE format. */
598 int result = memcmp(name1, name2, min(nbytes1, nbytes2));
602 /* The strings are the same up to their minimum length, so return a
603 * result based on their lengths. */
604 if (nbytes1 < nbytes2)
606 else if (nbytes1 > nbytes2)
613 /* Windoze: Case-insensitive UTF-16LE dentry or stream name comparison */
615 compare_utf16le_names_case_insensitive(const utf16lechar *name1, size_t nbytes1,
616 const utf16lechar *name2, size_t nbytes2)
618 /* Return the result if the strings differ up to their minimum length.
620 int result = _wcsnicmp((const wchar_t*)name1, (const wchar_t*)name2,
621 min(nbytes1 / 2, nbytes2 / 2));
625 /* The strings are the same up to their minimum length, so return a
626 * result based on their lengths. */
627 if (nbytes1 < nbytes2)
629 else if (nbytes1 > nbytes2)
634 #endif /* __WIN32__ */
637 # define compare_utf16le_names compare_utf16le_names_case_insensitive
639 # define compare_utf16le_names compare_utf16le_names_case_sensitive
645 dentry_compare_names_case_insensitive(const struct wim_dentry *d1,
646 const struct wim_dentry *d2)
648 return compare_utf16le_names_case_insensitive(d1->file_name,
649 d1->file_name_nbytes,
651 d2->file_name_nbytes);
653 #endif /* __WIN32__ */
656 dentry_compare_names_case_sensitive(const struct wim_dentry *d1,
657 const struct wim_dentry *d2)
659 return compare_utf16le_names_case_sensitive(d1->file_name,
660 d1->file_name_nbytes,
662 d2->file_name_nbytes);
666 # define dentry_compare_names dentry_compare_names_case_insensitive
668 # define dentry_compare_names dentry_compare_names_case_sensitive
671 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
672 * stream name @name that has length @name_nbytes bytes. */
674 ads_entry_has_name(const struct wim_ads_entry *entry,
675 const utf16lechar *name, size_t name_nbytes)
677 return !compare_utf16le_names(name, name_nbytes,
679 entry->stream_name_nbytes);
682 /* Given a UTF-16LE filename and a directory, look up the dentry for the file.
683 * Return it if found, otherwise NULL. This is case-sensitive on UNIX and
684 * case-insensitive on Windows. */
686 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
687 const utf16lechar *name,
690 struct rb_node *node;
693 node = dentry->d_inode->i_children_case_insensitive.rb_node;
695 node = dentry->d_inode->i_children.rb_node;
698 struct wim_dentry *child;
701 child = rb_entry(node, struct wim_dentry, rb_node_case_insensitive);
703 child = rbnode_dentry(node);
705 int result = compare_utf16le_names(name, name_nbytes,
707 child->file_name_nbytes);
709 node = node->rb_left;
711 node = node->rb_right;
714 if (!list_empty(&child->case_insensitive_conflict_list))
716 WARNING("Result of case-insensitive lookup is ambiguous "
717 "(returning \"%ls\" instead of \"%ls\")",
719 container_of(child->case_insensitive_conflict_list.next,
721 case_insensitive_conflict_list)->file_name);
730 /* Returns the child of @dentry that has the file name @name. Returns NULL if
731 * no child has the name. */
733 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name)
736 return get_dentry_child_with_utf16le_name(dentry, name,
737 tstrlen(name) * sizeof(tchar));
739 utf16lechar *utf16le_name;
740 size_t utf16le_name_nbytes;
742 struct wim_dentry *child;
744 ret = tstr_to_utf16le(name, tstrlen(name) * sizeof(tchar),
745 &utf16le_name, &utf16le_name_nbytes);
749 child = get_dentry_child_with_utf16le_name(dentry,
751 utf16le_name_nbytes);
758 static struct wim_dentry *
759 get_dentry_utf16le(WIMStruct *wim, const utf16lechar *path)
761 struct wim_dentry *cur_dentry, *parent_dentry;
762 const utf16lechar *p, *pp;
764 cur_dentry = parent_dentry = wim_root_dentry(wim);
771 while (*p == cpu_to_le16(WIM_PATH_SEPARATOR))
773 if (*p == cpu_to_le16('\0'))
776 while (*pp != cpu_to_le16(WIM_PATH_SEPARATOR) &&
777 *pp != cpu_to_le16('\0'))
780 cur_dentry = get_dentry_child_with_utf16le_name(parent_dentry, p,
781 (void*)pp - (void*)p);
782 if (cur_dentry == NULL)
785 parent_dentry = cur_dentry;
787 if (cur_dentry == NULL) {
788 if (dentry_is_directory(parent_dentry))
796 /* Returns the dentry corresponding to the @path, or NULL if there is no such
799 get_dentry(WIMStruct *wim, const tchar *path)
802 return get_dentry_utf16le(wim, path);
804 utf16lechar *path_utf16le;
805 size_t path_utf16le_nbytes;
807 struct wim_dentry *dentry;
809 ret = tstr_to_utf16le(path, tstrlen(path) * sizeof(tchar),
810 &path_utf16le, &path_utf16le_nbytes);
813 dentry = get_dentry_utf16le(wim, path_utf16le);
820 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
822 struct wim_dentry *dentry;
823 dentry = get_dentry(wim, path);
825 return dentry->d_inode;
830 /* Takes in a path of length @len in @buf, and transforms it into a string for
831 * the path of its parent directory. */
833 to_parent_name(tchar *buf, size_t len)
835 ssize_t i = (ssize_t)len - 1;
836 while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
838 while (i >= 0 && buf[i] != WIM_PATH_SEPARATOR)
840 while (i >= 0 && buf[i] == WIM_PATH_SEPARATOR)
842 buf[i + 1] = T('\0');
845 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
846 * if the dentry is not found. */
848 get_parent_dentry(WIMStruct *wim, const tchar *path)
850 size_t path_len = tstrlen(path);
851 tchar buf[path_len + 1];
853 tmemcpy(buf, path, path_len + 1);
854 to_parent_name(buf, path_len);
855 return get_dentry(wim, buf);
858 /* Prints the full path of a dentry. */
860 print_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
862 int ret = calculate_dentry_full_path(dentry);
865 tprintf(T("%"TS"\n"), dentry->_full_path);
869 /* We want to be able to show the names of the file attribute flags that are
871 struct file_attr_flag {
875 struct file_attr_flag file_attr_flags[] = {
876 {FILE_ATTRIBUTE_READONLY, T("READONLY")},
877 {FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
878 {FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
879 {FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
880 {FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
881 {FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
882 {FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
883 {FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
884 {FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
885 {FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
886 {FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
887 {FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
888 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,T("NOT_CONTENT_INDEXED")},
889 {FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
890 {FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
893 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
894 * available. If the dentry is unresolved and the lookup table is NULL, the
895 * lookup table entries will not be printed. Otherwise, they will be. */
897 print_dentry(struct wim_dentry *dentry, void *lookup_table)
900 struct wim_lookup_table_entry *lte;
901 const struct wim_inode *inode = dentry->d_inode;
904 tprintf(T("[DENTRY]\n"));
905 tprintf(T("Length = %"PRIu64"\n"), dentry->length);
906 tprintf(T("Attributes = 0x%x\n"), inode->i_attributes);
907 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
908 if (file_attr_flags[i].flag & inode->i_attributes)
909 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
910 file_attr_flags[i].name);
911 tprintf(T("Security ID = %d\n"), inode->i_security_id);
912 tprintf(T("Subdir offset = %"PRIu64"\n"), dentry->subdir_offset);
914 wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
915 tprintf(T("Creation Time = %"TS"\n"), buf);
917 wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
918 tprintf(T("Last Access Time = %"TS"\n"), buf);
920 wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
921 tprintf(T("Last Write Time = %"TS"\n"), buf);
923 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
924 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), inode->i_reparse_tag);
925 tprintf(T("Reparse Point Flags = 0x%"PRIx16"\n"),
926 inode->i_not_rpfixed);
927 tprintf(T("Reparse Point Unknown 2 = 0x%"PRIx32"\n"),
928 inode->i_rp_unknown_2);
930 tprintf(T("Reparse Point Unknown 1 = 0x%"PRIx32"\n"),
931 inode->i_rp_unknown_1);
932 tprintf(T("Hard Link Group = 0x%"PRIx64"\n"), inode->i_ino);
933 tprintf(T("Hard Link Group Size = %"PRIu32"\n"), inode->i_nlink);
934 tprintf(T("Number of Alternate Data Streams = %hu\n"), inode->i_num_ads);
935 if (dentry_has_long_name(dentry))
936 wimlib_printf(T("Filename = \"%"WS"\"\n"), dentry->file_name);
937 if (dentry_has_short_name(dentry))
938 wimlib_printf(T("Short Name \"%"WS"\"\n"), dentry->short_name);
939 if (dentry->_full_path)
940 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->_full_path);
942 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
944 print_lookup_table_entry(lte, stdout);
946 hash = inode_stream_hash(inode, 0);
948 tprintf(T("Hash = 0x"));
949 print_hash(hash, stdout);
954 for (u16 i = 0; i < inode->i_num_ads; i++) {
955 tprintf(T("[Alternate Stream Entry %u]\n"), i);
956 wimlib_printf(T("Name = \"%"WS"\"\n"),
957 inode->i_ads_entries[i].stream_name);
958 tprintf(T("Name Length (UTF16 bytes) = %hu\n"),
959 inode->i_ads_entries[i].stream_name_nbytes);
960 hash = inode_stream_hash(inode, i + 1);
962 tprintf(T("Hash = 0x"));
963 print_hash(hash, stdout);
966 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
972 /* Initializations done on every `struct wim_dentry'. */
974 dentry_common_init(struct wim_dentry *dentry)
976 memset(dentry, 0, sizeof(struct wim_dentry));
980 new_timeless_inode(void)
982 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
984 inode->i_security_id = -1;
986 inode->i_next_stream_id = 1;
987 inode->i_not_rpfixed = 1;
988 INIT_LIST_HEAD(&inode->i_list);
990 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
991 ERROR_WITH_ERRNO("Error initializing mutex");
996 INIT_LIST_HEAD(&inode->i_dentry);
1001 static struct wim_inode *
1004 struct wim_inode *inode = new_timeless_inode();
1006 u64 now = get_wim_timestamp();
1007 inode->i_creation_time = now;
1008 inode->i_last_access_time = now;
1009 inode->i_last_write_time = now;
1014 /* Creates an unlinked directory entry. */
1016 new_dentry(const tchar *name, struct wim_dentry **dentry_ret)
1018 struct wim_dentry *dentry;
1021 dentry = MALLOC(sizeof(struct wim_dentry));
1023 return WIMLIB_ERR_NOMEM;
1025 dentry_common_init(dentry);
1026 ret = set_dentry_name(dentry, name);
1028 dentry->parent = dentry;
1029 *dentry_ret = dentry;
1032 ERROR("Failed to set name on new dentry with name \"%"TS"\"",
1040 _new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret,
1043 struct wim_dentry *dentry;
1046 ret = new_dentry(name, &dentry);
1051 dentry->d_inode = new_timeless_inode();
1053 dentry->d_inode = new_inode();
1054 if (!dentry->d_inode) {
1055 free_dentry(dentry);
1056 return WIMLIB_ERR_NOMEM;
1059 inode_add_dentry(dentry, dentry->d_inode);
1060 *dentry_ret = dentry;
1065 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret)
1067 return _new_dentry_with_inode(name, dentry_ret, true);
1071 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret)
1073 return _new_dentry_with_inode(name, dentry_ret, false);
1077 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
1080 struct wim_dentry *dentry;
1082 DEBUG("Creating filler directory \"%"TS"\"", name);
1083 ret = new_dentry_with_inode(name, &dentry);
1086 /* Leave the inode number as 0; this is allowed for non
1087 * hard-linked files. */
1088 dentry->d_inode->i_resolved = 1;
1089 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
1090 *dentry_ret = dentry;
1095 init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
1096 size_t name_nbytes, bool is_utf16le)
1099 memset(ads_entry, 0, sizeof(*ads_entry));
1102 utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
1104 return WIMLIB_ERR_NOMEM;
1105 memcpy(p, name, name_nbytes);
1106 p[name_nbytes / 2] = cpu_to_le16(0);
1107 ads_entry->stream_name = p;
1108 ads_entry->stream_name_nbytes = name_nbytes;
1110 if (name && *(const tchar*)name != T('\0')) {
1111 ret = get_utf16le_name(name, &ads_entry->stream_name,
1112 &ads_entry->stream_name_nbytes);
1119 destroy_ads_entry(struct wim_ads_entry *ads_entry)
1121 FREE(ads_entry->stream_name);
1124 /* Frees an inode. */
1126 free_inode(struct wim_inode *inode)
1129 if (inode->i_ads_entries) {
1130 for (u16 i = 0; i < inode->i_num_ads; i++)
1131 destroy_ads_entry(&inode->i_ads_entries[i]);
1132 FREE(inode->i_ads_entries);
1135 wimlib_assert(inode->i_num_opened_fds == 0);
1137 pthread_mutex_destroy(&inode->i_mutex);
1139 /* HACK: This may instead delete the inode from i_list, but the
1140 * hlist_del() behaves the same as list_del(). */
1141 if (!hlist_unhashed(&inode->i_hlist))
1142 hlist_del(&inode->i_hlist);
1143 FREE(inode->i_extracted_file);
1148 /* Decrements link count on an inode and frees it if the link count reaches 0.
1151 put_inode(struct wim_inode *inode)
1153 wimlib_assert(inode->i_nlink != 0);
1154 if (--inode->i_nlink == 0) {
1156 if (inode->i_num_opened_fds == 0)
1164 /* Frees a WIM dentry.
1166 * The corresponding inode (if any) is freed only if its link count is
1170 free_dentry(struct wim_dentry *dentry)
1173 FREE(dentry->file_name);
1174 FREE(dentry->short_name);
1175 FREE(dentry->_full_path);
1176 if (dentry->d_inode)
1177 put_inode(dentry->d_inode);
1182 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
1183 * to free a directory tree. */
1185 do_free_dentry(struct wim_dentry *dentry, void *_lookup_table)
1187 struct wim_lookup_table *lookup_table = _lookup_table;
1190 struct wim_inode *inode = dentry->d_inode;
1191 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1192 struct wim_lookup_table_entry *lte;
1194 lte = inode_stream_lte(inode, i, lookup_table);
1196 lte_decrement_refcnt(lte, lookup_table);
1199 free_dentry(dentry);
1204 * Unlinks and frees a dentry tree.
1206 * @root: The root of the tree.
1207 * @lookup_table: The lookup table for dentries. If non-NULL, the
1208 * reference counts in the lookup table for the lookup
1209 * table entries corresponding to the dentries will be
1213 free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
1215 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
1220 /* Insert a dentry into the case insensitive index for a directory.
1222 * This is a red-black tree, but when multiple dentries share the same
1223 * case-insensitive name, only one is inserted into the tree itself; the rest
1224 * are connected in a list.
1226 static struct wim_dentry *
1227 dentry_add_child_case_insensitive(struct wim_dentry *parent,
1228 struct wim_dentry *child)
1230 struct rb_root *root;
1231 struct rb_node **new;
1232 struct rb_node *rb_parent;
1234 root = &parent->d_inode->i_children_case_insensitive;
1235 new = &root->rb_node;
1238 struct wim_dentry *this = container_of(*new, struct wim_dentry,
1239 rb_node_case_insensitive);
1240 int result = dentry_compare_names_case_insensitive(child, this);
1245 new = &((*new)->rb_left);
1246 else if (result > 0)
1247 new = &((*new)->rb_right);
1251 rb_link_node(&child->rb_node_case_insensitive, rb_parent, new);
1252 rb_insert_color(&child->rb_node_case_insensitive, root);
1258 * Links a dentry into the directory tree.
1260 * @parent: The dentry that will be the parent of @child.
1261 * @child: The dentry to link.
1263 * Returns NULL if successful. If @parent already contains a dentry with the
1264 * same case-sensitive name as @child, the pointer to this duplicate dentry is
1268 dentry_add_child(struct wim_dentry * restrict parent,
1269 struct wim_dentry * restrict child)
1271 struct rb_root *root;
1272 struct rb_node **new;
1273 struct rb_node *rb_parent;
1275 wimlib_assert(dentry_is_directory(parent));
1276 wimlib_assert(parent != child);
1278 /* Case sensitive child dentry index */
1279 root = &parent->d_inode->i_children;
1280 new = &root->rb_node;
1283 struct wim_dentry *this = rbnode_dentry(*new);
1284 int result = dentry_compare_names_case_sensitive(child, this);
1289 new = &((*new)->rb_left);
1290 else if (result > 0)
1291 new = &((*new)->rb_right);
1295 child->parent = parent;
1296 rb_link_node(&child->rb_node, rb_parent, new);
1297 rb_insert_color(&child->rb_node, root);
1301 struct wim_dentry *existing;
1302 existing = dentry_add_child_case_insensitive(parent, child);
1304 list_add(&child->case_insensitive_conflict_list,
1305 &existing->case_insensitive_conflict_list);
1306 child->rb_node_case_insensitive.__rb_parent_color = 0;
1308 INIT_LIST_HEAD(&child->case_insensitive_conflict_list);
1315 /* Unlink a WIM dentry from the directory entry tree. */
1317 unlink_dentry(struct wim_dentry *dentry)
1319 struct wim_dentry *parent = dentry->parent;
1321 if (parent == dentry)
1323 rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1325 if (dentry->rb_node_case_insensitive.__rb_parent_color) {
1326 /* This dentry was in the case-insensitive red-black tree. */
1327 rb_erase(&dentry->rb_node_case_insensitive,
1328 &parent->d_inode->i_children_case_insensitive);
1329 if (!list_empty(&dentry->case_insensitive_conflict_list)) {
1330 /* Make a different case-insensitively-the-same dentry
1331 * be the "representative" in the red-black tree. */
1332 struct list_head *next;
1333 struct wim_dentry *other;
1334 struct wim_dentry *existing;
1336 next = dentry->case_insensitive_conflict_list.next;
1337 other = list_entry(next, struct wim_dentry, case_insensitive_conflict_list);
1338 existing = dentry_add_child_case_insensitive(parent, other);
1339 wimlib_assert(existing == NULL);
1342 list_del(&dentry->case_insensitive_conflict_list);
1347 * Returns the alternate data stream entry belonging to @inode that has the
1348 * stream name @stream_name.
1350 struct wim_ads_entry *
1351 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
1354 if (inode->i_num_ads == 0) {
1357 size_t stream_name_utf16le_nbytes;
1359 struct wim_ads_entry *result;
1361 #if TCHAR_IS_UTF16LE
1362 const utf16lechar *stream_name_utf16le;
1364 stream_name_utf16le = stream_name;
1365 stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
1367 utf16lechar *stream_name_utf16le;
1370 int ret = tstr_to_utf16le(stream_name,
1371 tstrlen(stream_name) *
1373 &stream_name_utf16le,
1374 &stream_name_utf16le_nbytes);
1382 if (ads_entry_has_name(&inode->i_ads_entries[i],
1383 stream_name_utf16le,
1384 stream_name_utf16le_nbytes))
1388 result = &inode->i_ads_entries[i];
1391 } while (++i != inode->i_num_ads);
1392 #if !TCHAR_IS_UTF16LE
1393 FREE(stream_name_utf16le);
1399 static struct wim_ads_entry *
1400 do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
1401 size_t stream_name_nbytes, bool is_utf16le)
1404 struct wim_ads_entry *ads_entries;
1405 struct wim_ads_entry *new_entry;
1407 if (inode->i_num_ads >= 0xfffe) {
1408 ERROR("Too many alternate data streams in one inode!");
1411 num_ads = inode->i_num_ads + 1;
1412 ads_entries = REALLOC(inode->i_ads_entries,
1413 num_ads * sizeof(inode->i_ads_entries[0]));
1415 ERROR("Failed to allocate memory for new alternate data stream");
1418 inode->i_ads_entries = ads_entries;
1420 new_entry = &inode->i_ads_entries[num_ads - 1];
1421 if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
1423 new_entry->stream_id = inode->i_next_stream_id++;
1424 inode->i_num_ads = num_ads;
1428 struct wim_ads_entry *
1429 inode_add_ads_utf16le(struct wim_inode *inode,
1430 const utf16lechar *stream_name,
1431 size_t stream_name_nbytes)
1433 DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
1434 return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
1438 * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1439 * NULL if memory could not be allocated.
1441 struct wim_ads_entry *
1442 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
1444 DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
1445 return do_inode_add_ads(inode, stream_name,
1446 tstrlen(stream_name) * sizeof(tchar),
1450 static struct wim_lookup_table_entry *
1451 add_stream_from_data_buffer(const void *buffer, size_t size,
1452 struct wim_lookup_table *lookup_table)
1454 u8 hash[SHA1_HASH_SIZE];
1455 struct wim_lookup_table_entry *lte, *existing_lte;
1457 sha1_buffer(buffer, size, hash);
1458 existing_lte = __lookup_resource(lookup_table, hash);
1460 wimlib_assert(wim_resource_size(existing_lte) == size);
1465 lte = new_lookup_table_entry();
1468 buffer_copy = memdup(buffer, size);
1470 free_lookup_table_entry(lte);
1473 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1474 lte->attached_buffer = buffer_copy;
1475 lte->resource_entry.original_size = size;
1476 copy_hash(lte->hash, hash);
1477 lookup_table_insert(lookup_table, lte);
1483 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
1484 const void *value, size_t size,
1485 struct wim_lookup_table *lookup_table)
1487 struct wim_ads_entry *new_ads_entry;
1489 wimlib_assert(inode->i_resolved);
1491 new_ads_entry = inode_add_ads(inode, name);
1493 return WIMLIB_ERR_NOMEM;
1495 new_ads_entry->lte = add_stream_from_data_buffer(value, size,
1497 if (!new_ads_entry->lte) {
1498 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1500 return WIMLIB_ERR_NOMEM;
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]));
1547 inode_get_unix_data(const struct wim_inode *inode,
1548 struct wimlib_unix_data *unix_data,
1549 u16 *stream_idx_ret)
1551 const struct wim_ads_entry *ads_entry;
1552 const struct wim_lookup_table_entry *lte;
1556 wimlib_assert(inode->i_resolved);
1558 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1559 WIMLIB_UNIX_DATA_TAG, NULL);
1561 return NO_UNIX_DATA;
1564 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1566 lte = ads_entry->lte;
1568 return NO_UNIX_DATA;
1570 size = wim_resource_size(lte);
1571 if (size != sizeof(struct wimlib_unix_data))
1572 return BAD_UNIX_DATA;
1574 ret = read_full_resource_into_buf(lte, unix_data);
1578 if (unix_data->version != 0)
1579 return BAD_UNIX_DATA;
1584 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
1585 struct wim_lookup_table *lookup_table, int which)
1587 struct wimlib_unix_data unix_data;
1589 bool have_good_unix_data = false;
1590 bool have_unix_data = false;
1593 if (!(which & UNIX_DATA_CREATE)) {
1594 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1595 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1596 have_unix_data = true;
1598 have_good_unix_data = true;
1600 unix_data.version = 0;
1601 if (which & UNIX_DATA_UID || !have_good_unix_data)
1602 unix_data.uid = uid;
1603 if (which & UNIX_DATA_GID || !have_good_unix_data)
1604 unix_data.gid = gid;
1605 if (which & UNIX_DATA_MODE || !have_good_unix_data)
1606 unix_data.mode = mode;
1607 ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1609 sizeof(struct wimlib_unix_data),
1611 if (ret == 0 && have_unix_data)
1612 inode_remove_ads(inode, stream_idx, lookup_table);
1615 #endif /* !__WIN32__ */
1618 * Reads the alternate data stream entries of a WIM dentry.
1620 * @p: Pointer to buffer that starts with the first alternate stream entry.
1622 * @inode: Inode to load the alternate data streams into.
1623 * @inode->i_num_ads must have been set to the number of
1624 * alternate data streams that are expected.
1626 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1630 * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
1631 * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
1632 * failure, @inode is not modified.
1635 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
1636 size_t nbytes_remaining)
1639 struct wim_ads_entry *ads_entries;
1642 BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
1644 /* Allocate an array for our in-memory representation of the alternate
1645 * data stream entries. */
1646 num_ads = inode->i_num_ads;
1647 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1651 /* Read the entries into our newly allocated buffer. */
1652 for (u16 i = 0; i < num_ads; i++) {
1654 struct wim_ads_entry *cur_entry;
1655 const struct wim_ads_entry_on_disk *disk_entry =
1656 (const struct wim_ads_entry_on_disk*)p;
1658 cur_entry = &ads_entries[i];
1659 ads_entries[i].stream_id = i + 1;
1661 /* Do we have at least the size of the fixed-length data we know
1663 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
1666 /* Read the length field */
1667 length = le64_to_cpu(disk_entry->length);
1669 /* Make sure the length field is neither so small it doesn't
1670 * include all the fixed-length data nor so large it overflows
1671 * the metadata resource buffer. */
1672 if (length < sizeof(struct wim_ads_entry_on_disk) ||
1673 length > nbytes_remaining)
1676 /* Read the rest of the fixed-length data. */
1678 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
1679 copy_hash(cur_entry->hash, disk_entry->hash);
1680 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
1682 /* If stream_name_nbytes != 0, this is a named stream.
1683 * Otherwise this is an unnamed stream, or in some cases (bugs
1684 * in Microsoft's software I guess) a meaningless entry
1685 * distinguished from the real unnamed stream entry, if any, by
1686 * the fact that the real unnamed stream entry has a nonzero
1688 if (cur_entry->stream_name_nbytes) {
1689 /* The name is encoded in UTF16-LE, which uses 2-byte
1690 * coding units, so the length of the name had better be
1691 * an even number of bytes... */
1692 if (cur_entry->stream_name_nbytes & 1)
1695 /* Add the length of the stream name to get the length
1696 * we actually need to read. Make sure this isn't more
1697 * than the specified length of the entry. */
1698 if (sizeof(struct wim_ads_entry_on_disk) +
1699 cur_entry->stream_name_nbytes > length)
1702 cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
1703 if (!cur_entry->stream_name)
1706 memcpy(cur_entry->stream_name,
1707 disk_entry->stream_name,
1708 cur_entry->stream_name_nbytes);
1709 cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
1712 /* It's expected that the size of every ADS entry is a multiple
1713 * of 8. However, to be safe, I'm allowing the possibility of
1714 * an ADS entry at the very end of the metadata resource ending
1715 * un-aligned. So although we still need to increment the input
1716 * pointer by @length to reach the next ADS entry, it's possible
1717 * that less than @length is actually remaining in the metadata
1718 * resource. We should set the remaining bytes to 0 if this
1720 length = (length + 7) & ~(u64)7;
1722 if (nbytes_remaining < length)
1723 nbytes_remaining = 0;
1725 nbytes_remaining -= length;
1727 inode->i_ads_entries = ads_entries;
1728 inode->i_next_stream_id = inode->i_num_ads + 1;
1732 ret = WIMLIB_ERR_NOMEM;
1733 goto out_free_ads_entries;
1735 ERROR("An alternate data stream entry is invalid");
1736 ret = WIMLIB_ERR_INVALID_DENTRY;
1737 out_free_ads_entries:
1739 for (u16 i = 0; i < num_ads; i++)
1740 destroy_ads_entry(&ads_entries[i]);
1748 * Reads a WIM directory entry, including all alternate data stream entries that
1749 * follow it, from the WIM image's metadata resource.
1751 * @metadata_resource:
1752 * Pointer to the metadata resource buffer.
1754 * @metadata_resource_len:
1755 * Length of the metadata resource buffer, in bytes.
1757 * @offset: Offset of the dentry within the metadata resource.
1759 * @dentry: A `struct wim_dentry' that will be filled in by this function.
1761 * Return 0 on success or nonzero on failure. On failure, @dentry will have
1762 * been modified, but it will not be left with pointers to any allocated
1763 * buffers. On success, the dentry->length field must be examined. If zero,
1764 * this was a special "end of directory" dentry and not a real dentry. If
1765 * nonzero, this was a real dentry.
1767 * Possible errors include:
1769 * WIMLIB_ERR_INVALID_DENTRY
1772 read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
1773 u64 offset, struct wim_dentry * restrict dentry)
1776 u64 calculated_size;
1777 utf16lechar *file_name;
1778 utf16lechar *short_name;
1779 u16 short_name_nbytes;
1780 u16 file_name_nbytes;
1782 struct wim_inode *inode;
1783 const u8 *p = &metadata_resource[offset];
1784 const struct wim_dentry_on_disk *disk_dentry =
1785 (const struct wim_dentry_on_disk*)p;
1787 BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE);
1789 if ((uintptr_t)p & 7)
1790 WARNING("WIM dentry is not 8-byte aligned");
1792 dentry_common_init(dentry);
1794 /* Before reading the whole dentry, we need to read just the length.
1795 * This is because a dentry of length 8 (that is, just the length field)
1796 * terminates the list of sibling directory entries. */
1797 if (offset + sizeof(u64) > metadata_resource_len ||
1798 offset + sizeof(u64) < offset)
1800 ERROR("Directory entry starting at %"PRIu64" ends past the "
1801 "end of the metadata resource (size %"PRIu64")",
1802 offset, metadata_resource_len);
1803 return WIMLIB_ERR_INVALID_DENTRY;
1805 dentry->length = le64_to_cpu(disk_dentry->length);
1807 /* A zero length field (really a length of 8, since that's how big the
1808 * directory entry is...) indicates that this is the end of directory
1809 * dentry. We do not read it into memory as an actual dentry, so just
1810 * return successfully in this case. */
1811 if (dentry->length == 8)
1813 if (dentry->length == 0)
1816 /* Now that we have the actual length provided in the on-disk structure,
1817 * again make sure it doesn't overflow the metadata resource buffer. */
1818 if (offset + dentry->length > metadata_resource_len ||
1819 offset + dentry->length < offset)
1821 ERROR("Directory entry at offset %"PRIu64" and with size "
1822 "%"PRIu64" ends past the end of the metadata resource "
1824 offset, dentry->length, metadata_resource_len);
1825 return WIMLIB_ERR_INVALID_DENTRY;
1828 /* Make sure the dentry length is at least as large as the number of
1829 * fixed-length fields */
1830 if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
1831 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1833 return WIMLIB_ERR_INVALID_DENTRY;
1836 /* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
1837 inode = new_timeless_inode();
1839 return WIMLIB_ERR_NOMEM;
1841 /* Read more fields; some into the dentry, and some into the inode. */
1843 inode->i_attributes = le32_to_cpu(disk_dentry->attributes);
1844 inode->i_security_id = le32_to_cpu(disk_dentry->security_id);
1845 dentry->subdir_offset = le64_to_cpu(disk_dentry->subdir_offset);
1846 dentry->d_unused_1 = le64_to_cpu(disk_dentry->unused_1);
1847 dentry->d_unused_2 = le64_to_cpu(disk_dentry->unused_2);
1848 inode->i_creation_time = le64_to_cpu(disk_dentry->creation_time);
1849 inode->i_last_access_time = le64_to_cpu(disk_dentry->last_access_time);
1850 inode->i_last_write_time = le64_to_cpu(disk_dentry->last_write_time);
1851 copy_hash(inode->i_hash, disk_dentry->unnamed_stream_hash);
1853 /* I don't know what's going on here. It seems like M$ screwed up the
1854 * reparse points, then put the fields in the same place and didn't
1855 * document it. So we have some fields we read for reparse points, and
1856 * some fields in the same place for non-reparse-point.s */
1857 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1858 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->reparse.rp_unknown_1);
1859 inode->i_reparse_tag = le32_to_cpu(disk_dentry->reparse.reparse_tag);
1860 inode->i_rp_unknown_2 = le16_to_cpu(disk_dentry->reparse.rp_unknown_2);
1861 inode->i_not_rpfixed = le16_to_cpu(disk_dentry->reparse.not_rpfixed);
1862 /* Leave inode->i_ino at 0. Note that this means the WIM file
1863 * cannot archive hard-linked reparse points. Such a thing
1864 * doesn't really make sense anyway, although I believe it's
1865 * theoretically possible to have them on NTFS. */
1867 inode->i_rp_unknown_1 = le32_to_cpu(disk_dentry->nonreparse.rp_unknown_1);
1868 inode->i_ino = le64_to_cpu(disk_dentry->nonreparse.hard_link_group_id);
1871 inode->i_num_ads = le16_to_cpu(disk_dentry->num_alternate_data_streams);
1873 short_name_nbytes = le16_to_cpu(disk_dentry->short_name_nbytes);
1874 file_name_nbytes = le16_to_cpu(disk_dentry->file_name_nbytes);
1876 if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
1878 ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
1879 ret = WIMLIB_ERR_INVALID_DENTRY;
1880 goto out_free_inode;
1883 /* We now know the length of the file name and short name. Make sure
1884 * the length of the dentry is large enough to actually hold them.
1886 * The calculated length here is unaligned to allow for the possibility
1887 * that the dentry->length names an unaligned length, although this
1888 * would be unexpected. */
1889 calculated_size = _dentry_correct_length_unaligned(file_name_nbytes,
1892 if (dentry->length < calculated_size) {
1893 ERROR("Unexpected end of directory entry! (Expected "
1894 "at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
1895 calculated_size, dentry->length);
1896 ret = WIMLIB_ERR_INVALID_DENTRY;
1897 goto out_free_inode;
1900 p += sizeof(struct wim_dentry_on_disk);
1902 /* Read the filename if present. Note: if the filename is empty, there
1903 * is no null terminator following it. */
1904 if (file_name_nbytes) {
1905 file_name = MALLOC(file_name_nbytes + 2);
1907 ERROR("Failed to allocate %d bytes for dentry file name",
1908 file_name_nbytes + 2);
1909 ret = WIMLIB_ERR_NOMEM;
1910 goto out_free_inode;
1912 memcpy(file_name, p, file_name_nbytes);
1913 p += file_name_nbytes + 2;
1914 file_name[file_name_nbytes / 2] = cpu_to_le16(0);
1920 /* Read the short filename if present. Note: if there is no short
1921 * filename, there is no null terminator following it. */
1922 if (short_name_nbytes) {
1923 short_name = MALLOC(short_name_nbytes + 2);
1925 ERROR("Failed to allocate %d bytes for dentry short name",
1926 short_name_nbytes + 2);
1927 ret = WIMLIB_ERR_NOMEM;
1928 goto out_free_file_name;
1930 memcpy(short_name, p, short_name_nbytes);
1931 p += short_name_nbytes + 2;
1932 short_name[short_name_nbytes / 2] = cpu_to_le16(0);
1937 /* Align the dentry length */
1938 dentry->length = (dentry->length + 7) & ~7;
1941 * Read the alternate data streams, if present. dentry->num_ads tells
1942 * us how many they are, and they will directly follow the dentry
1945 * Note that each alternate data stream entry begins on an 8-byte
1946 * aligned boundary, and the alternate data stream entries seem to NOT
1947 * be included in the dentry->length field for some reason.
1949 if (inode->i_num_ads != 0) {
1950 ret = WIMLIB_ERR_INVALID_DENTRY;
1951 if (offset + dentry->length > metadata_resource_len ||
1952 (ret = read_ads_entries(&metadata_resource[offset + dentry->length],
1954 metadata_resource_len - offset - dentry->length)))
1956 ERROR("Failed to read alternate data stream "
1957 "entries of WIM dentry \"%"WS"\"", file_name);
1958 goto out_free_short_name;
1961 /* We've read all the data for this dentry. Set the names and their
1962 * lengths, and we've done. */
1963 dentry->d_inode = inode;
1964 dentry->file_name = file_name;
1965 dentry->short_name = short_name;
1966 dentry->file_name_nbytes = file_name_nbytes;
1967 dentry->short_name_nbytes = short_name_nbytes;
1970 out_free_short_name:
1980 static const tchar *
1981 dentry_get_file_type_string(const struct wim_dentry *dentry)
1983 const struct wim_inode *inode = dentry->d_inode;
1984 if (inode_is_directory(inode))
1985 return T("directory");
1986 else if (inode_is_symlink(inode))
1987 return T("symbolic link");
1992 /* Reads the children of a dentry, and all their children, ..., etc. from the
1993 * metadata resource and into the dentry tree.
1995 * @metadata_resource: An array that contains the uncompressed metadata
1996 * resource for the WIM file.
1998 * @metadata_resource_len: The length of the uncompressed metadata resource, in
2001 * @dentry: A pointer to a `struct wim_dentry' that is the root of the directory
2002 * tree and has already been read from the metadata resource. It
2003 * does not need to be the real root because this procedure is
2004 * called recursively.
2006 * Returns zero on success; nonzero on failure.
2009 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
2010 struct wim_dentry *dentry)
2012 u64 cur_offset = dentry->subdir_offset;
2013 struct wim_dentry *child;
2014 struct wim_dentry *duplicate;
2015 struct wim_dentry *parent;
2016 struct wim_dentry cur_child;
2020 * If @dentry has no child dentries, nothing more needs to be done for
2021 * this branch. This is the case for regular files, symbolic links, and
2022 * *possibly* empty directories (although an empty directory may also
2023 * have one child dentry that is the special end-of-directory dentry)
2025 if (cur_offset == 0)
2028 /* Check for cyclic directory structure */
2029 for (parent = dentry->parent; !dentry_is_root(parent); parent = parent->parent)
2031 if (unlikely(parent->subdir_offset == cur_offset)) {
2032 ERROR("Cyclic directory structure directed: children "
2033 "of \"%"TS"\" coincide with children of \"%"TS"\"",
2034 dentry_full_path(dentry),
2035 dentry_full_path(parent));
2036 return WIMLIB_ERR_INVALID_DENTRY;
2040 /* Find and read all the children of @dentry. */
2043 /* Read next child of @dentry into @cur_child. */
2044 ret = read_dentry(metadata_resource, metadata_resource_len,
2045 cur_offset, &cur_child);
2049 /* Check for end of directory. */
2050 if (cur_child.length == 0)
2053 /* Not end of directory. Allocate this child permanently and
2054 * link it to the parent and previous child. */
2055 child = memdup(&cur_child, sizeof(struct wim_dentry));
2057 ERROR("Failed to allocate new dentry!");
2058 ret = WIMLIB_ERR_NOMEM;
2062 /* Advance to the offset of the next child. Note: We need to
2063 * advance by the TOTAL length of the dentry, not by the length
2064 * cur_child.length, which although it does take into account
2065 * the padding, it DOES NOT take into account alternate stream
2067 cur_offset += dentry_total_length(child);
2069 if (unlikely(!dentry_has_long_name(child))) {
2070 WARNING("Ignoring unnamed dentry in "
2071 "directory \"%"TS"\"",
2072 dentry_full_path(dentry));
2077 duplicate = dentry_add_child(dentry, child);
2078 if (unlikely(duplicate)) {
2079 const tchar *child_type, *duplicate_type;
2080 child_type = dentry_get_file_type_string(child);
2081 duplicate_type = dentry_get_file_type_string(duplicate);
2082 WARNING("Ignoring duplicate %"TS" \"%"TS"\" "
2083 "(the WIM image already contains a %"TS" "
2084 "at that path with the exact same name)",
2085 child_type, dentry_full_path(duplicate),
2091 inode_add_dentry(child, child->d_inode);
2092 /* If there are children of this child, call this
2093 * procedure recursively. */
2094 if (child->subdir_offset != 0) {
2095 if (likely(dentry_is_directory(child))) {
2096 ret = read_dentry_tree(metadata_resource,
2097 metadata_resource_len,
2102 WARNING("Ignoring children of non-directory \"%"TS"\"",
2103 dentry_full_path(child));
2111 * Writes a WIM dentry to an output buffer.
2113 * @dentry: The dentry structure.
2114 * @p: The memory location to write the data to.
2116 * Returns the pointer to the byte after the last byte we wrote as part of the
2117 * dentry, including any alternate data stream entries.
2120 write_dentry(const struct wim_dentry * restrict dentry, u8 * restrict p)
2122 const struct wim_inode *inode;
2123 struct wim_dentry_on_disk *disk_dentry;
2127 wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
2130 inode = dentry->d_inode;
2131 disk_dentry = (struct wim_dentry_on_disk*)p;
2133 disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
2134 disk_dentry->security_id = cpu_to_le32(inode->i_security_id);
2135 disk_dentry->subdir_offset = cpu_to_le64(dentry->subdir_offset);
2136 disk_dentry->unused_1 = cpu_to_le64(dentry->d_unused_1);
2137 disk_dentry->unused_2 = cpu_to_le64(dentry->d_unused_2);
2138 disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
2139 disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
2140 disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
2141 hash = inode_stream_hash(inode, 0);
2142 copy_hash(disk_dentry->unnamed_stream_hash, hash);
2143 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2144 disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2145 disk_dentry->reparse.reparse_tag = cpu_to_le32(inode->i_reparse_tag);
2146 disk_dentry->reparse.rp_unknown_2 = cpu_to_le16(inode->i_rp_unknown_2);
2147 disk_dentry->reparse.not_rpfixed = cpu_to_le16(inode->i_not_rpfixed);
2149 disk_dentry->nonreparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
2150 disk_dentry->nonreparse.hard_link_group_id =
2151 cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
2153 disk_dentry->num_alternate_data_streams = cpu_to_le16(inode->i_num_ads);
2154 disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
2155 disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
2156 p += sizeof(struct wim_dentry_on_disk);
2158 wimlib_assert(dentry_is_root(dentry) != dentry_has_long_name(dentry));
2160 if (dentry_has_long_name(dentry))
2161 p = mempcpy(p, dentry->file_name, dentry->file_name_nbytes + 2);
2163 if (dentry_has_short_name(dentry))
2164 p = mempcpy(p, dentry->short_name, dentry->short_name_nbytes + 2);
2166 /* Align to 8-byte boundary */
2167 while ((uintptr_t)p & 7)
2170 /* We calculate the correct length of the dentry ourselves because the
2171 * dentry->length field may been set to an unexpected value from when we
2172 * read the dentry in (for example, there may have been unknown data
2173 * appended to the end of the dentry...). Furthermore, the dentry may
2174 * have been renamed, thus changing its needed length. */
2175 disk_dentry->length = cpu_to_le64(p - orig_p);
2177 /* Write the alternate data streams entries, if any. */
2178 for (u16 i = 0; i < inode->i_num_ads; i++) {
2179 const struct wim_ads_entry *ads_entry =
2180 &inode->i_ads_entries[i];
2181 struct wim_ads_entry_on_disk *disk_ads_entry =
2182 (struct wim_ads_entry_on_disk*)p;
2185 disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
2187 hash = inode_stream_hash(inode, i + 1);
2188 copy_hash(disk_ads_entry->hash, hash);
2189 disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
2190 p += sizeof(struct wim_ads_entry_on_disk);
2191 if (ads_entry->stream_name_nbytes) {
2192 p = mempcpy(p, ads_entry->stream_name,
2193 ads_entry->stream_name_nbytes + 2);
2195 /* Align to 8-byte boundary */
2196 while ((uintptr_t)p & 7)
2198 disk_ads_entry->length = cpu_to_le64(p - orig_p);
2204 write_dentry_cb(struct wim_dentry *dentry, void *_p)
2207 *p = write_dentry(dentry, *p);
2212 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
2215 write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
2218 *p = write_dentry_tree_recursive(dentry, *p);
2222 /* Recursive function that writes a dentry tree rooted at @parent, not including
2223 * @parent itself, which has already been written. */
2225 write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
2227 /* Nothing to do if this dentry has no children. */
2228 if (parent->subdir_offset == 0)
2231 /* Write child dentries and end-of-directory entry.
2233 * Note: we need to write all of this dentry's children before
2234 * recursively writing the directory trees rooted at each of the child
2235 * dentries, since the on-disk dentries for a dentry's children are
2236 * always located at consecutive positions in the metadata resource! */
2237 for_dentry_child(parent, write_dentry_cb, &p);
2239 /* write end of directory entry */
2240 *(le64*)p = cpu_to_le64(0);
2243 /* Recurse on children. */
2244 for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
2248 /* Writes a directory tree to the metadata resource.
2250 * @root: Root of the dentry tree.
2251 * @p: Pointer to a buffer with enough space for the dentry tree.
2253 * Returns pointer to the byte after the last byte we wrote.
2256 write_dentry_tree(const struct wim_dentry *root, u8 *p)
2258 DEBUG("Writing dentry tree.");
2259 wimlib_assert(dentry_is_root(root));
2261 /* If we're the root dentry, we have no parent that already
2262 * wrote us, so we need to write ourselves. */
2263 p = write_dentry(root, p);
2265 /* Write end of directory entry after the root dentry just to be safe;
2266 * however the root dentry obviously cannot have any siblings. */
2267 *(le64*)p = cpu_to_le64(0);
2270 /* Recursively write the rest of the dentry tree. */
2271 return write_dentry_tree_recursive(root, p);