4 * A dentry (directory entry) contains the metadata for a file. In the WIM file
5 * format, the dentries are stored in the "metadata resource" section right
6 * after the security data. Each image in the WIM file has its own metadata
7 * resource with its own security data and dentry tree. Dentries in different
8 * images may share file resources by referring to the same lookup table
13 * Copyright (C) 2012 Eric Biggers
15 * This file is part of wimlib, a library for working with WIM files.
17 * wimlib is free software; you can redistribute it and/or modify it under the
18 * terms of the GNU General Public License as published by the Free Software
19 * Foundation; either version 3 of the License, or (at your option) any later
22 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
23 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
24 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along with
27 * wimlib; if not, see http://www.gnu.org/licenses/.
37 #include "lookup_table.h"
39 #include "timestamp.h"
40 #include "wimlib_internal.h"
43 static u64 __dentry_correct_length_unaligned(u16 file_name_len,
46 u64 length = WIM_DENTRY_DISK_SIZE;
48 length += file_name_len + 2;
50 length += short_name_len + 2;
54 static u64 dentry_correct_length_unaligned(const struct dentry *dentry)
56 return __dentry_correct_length_unaligned(dentry->file_name_len,
57 dentry->short_name_len);
60 /* Return the "correct" value to write in the length field of the dentry, based
61 * on the file name length and short name length */
62 static u64 dentry_correct_length(const struct dentry *dentry)
64 return (dentry_correct_length_unaligned(dentry) + 7) & ~7;
68 * Returns true if @dentry has the UTF-8 file name @name that has length
71 static bool dentry_has_name(const struct dentry *dentry, const char *name,
74 if (dentry->file_name_utf8_len != name_len)
76 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
79 static inline bool ads_entry_has_name(const struct ads_entry *entry,
80 const char *name, size_t name_len)
82 if (entry->stream_name_utf8_len != name_len)
84 return memcmp(entry->stream_name_utf8, name, name_len) == 0;
87 /* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
88 * and their lengths in the pointer arguments */
89 int get_names(char **name_utf16_ret, char **name_utf8_ret,
90 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
95 char *name_utf16, *name_utf8;
97 utf8_len = strlen(name);
99 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
102 return WIMLIB_ERR_NOMEM;
104 name_utf8 = MALLOC(utf8_len + 1);
107 return WIMLIB_ERR_NOMEM;
109 memcpy(name_utf8, name, utf8_len + 1);
110 FREE(*name_utf8_ret);
111 FREE(*name_utf16_ret);
112 *name_utf8_ret = name_utf8;
113 *name_utf16_ret = name_utf16;
114 *name_utf8_len_ret = utf8_len;
115 *name_utf16_len_ret = utf16_len;
119 /* Changes the name of a dentry to @new_name. Only changes the file_name and
120 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
121 * full_path_utf8 fields. Also recalculates its length. */
122 static int change_dentry_name(struct dentry *dentry, const char *new_name)
126 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
127 &dentry->file_name_len, &dentry->file_name_utf8_len,
129 FREE(dentry->short_name);
130 dentry->short_name_len = 0;
132 dentry->length = dentry_correct_length(dentry);
137 * Changes the name of an alternate data stream */
138 static int change_ads_name(struct ads_entry *entry, const char *new_name)
140 return get_names(&entry->stream_name, &entry->stream_name_utf8,
141 &entry->stream_name_len,
142 &entry->stream_name_utf8_len,
146 /* Returns the total length of a WIM alternate data stream entry on-disk,
147 * including the stream name, the null terminator, AND the padding after the
148 * entry to align the next one (or the next dentry) on an 8-byte boundary. */
149 static u64 ads_entry_total_length(const struct ads_entry *entry)
151 u64 len = WIM_ADS_ENTRY_DISK_SIZE;
152 if (entry->stream_name_len)
153 len += entry->stream_name_len + 2;
154 return (len + 7) & ~7;
158 static u64 __dentry_total_length(const struct dentry *dentry, u64 length)
160 const struct inode *inode = dentry->d_inode;
161 for (u16 i = 0; i < inode->num_ads; i++)
162 length += ads_entry_total_length(&inode->ads_entries[i]);
163 return (length + 7) & ~7;
166 u64 dentry_correct_total_length(const struct dentry *dentry)
168 return __dentry_total_length(dentry,
169 dentry_correct_length_unaligned(dentry));
172 /* Real length of a dentry, including the alternate data stream entries, which
173 * are not included in the dentry->length field... */
174 static u64 dentry_total_length(const struct dentry *dentry)
176 return __dentry_total_length(dentry, dentry->length);
179 /* Transfers file attributes from a `stat' buffer to a WIM "inode". */
180 void stbuf_to_inode(const struct stat *stbuf, struct inode *inode)
182 if (S_ISLNK(stbuf->st_mode)) {
183 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
184 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
185 } else if (S_ISDIR(stbuf->st_mode)) {
186 inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
188 inode->attributes = FILE_ATTRIBUTE_NORMAL;
190 if (sizeof(ino_t) >= 8)
191 inode->ino = (u64)stbuf->st_ino;
193 inode->ino = (u64)stbuf->st_ino |
194 ((u64)stbuf->st_dev << ((sizeof(ino_t) * 8) & 63));
196 inode->creation_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
197 inode->last_write_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
198 inode->last_access_time = timespec_to_wim_timestamp(&stbuf->st_atim);
202 /* Transfers file attributes from a struct inode to a `stat' buffer.
204 * The lookup table entry tells us which stream in the inode we are statting.
205 * For a named data stream, everything returned is the same as the unnamed data
206 * stream except possibly the size and block count. */
207 int inode_to_stbuf(const struct inode *inode, struct lookup_table_entry *lte,
210 if (inode_is_symlink(inode))
211 stbuf->st_mode = S_IFLNK | 0777;
212 else if (inode_is_directory(inode))
213 stbuf->st_mode = S_IFDIR | 0755;
215 stbuf->st_mode = S_IFREG | 0644;
217 stbuf->st_ino = (ino_t)inode->ino;
218 stbuf->st_nlink = inode->link_count;
219 stbuf->st_uid = getuid();
220 stbuf->st_gid = getgid();
223 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
224 wimlib_assert(lte->staging_file_name);
225 struct stat native_stat;
226 if (stat(lte->staging_file_name, &native_stat) != 0) {
227 DEBUG("Failed to stat `%s': %m",
228 lte->staging_file_name);
231 stbuf->st_size = native_stat.st_size;
233 stbuf->st_size = wim_resource_size(lte);
239 stbuf->st_atime = wim_timestamp_to_unix(inode->last_access_time);
240 stbuf->st_mtime = wim_timestamp_to_unix(inode->last_write_time);
241 stbuf->st_ctime = wim_timestamp_to_unix(inode->creation_time);
242 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
248 * Calls a function on all directory entries in a directory tree. It is called
249 * on a parent before its children.
251 int for_dentry_in_tree(struct dentry *root,
252 int (*visitor)(struct dentry*, void*), void *arg)
255 struct dentry *child;
257 ret = visitor(root, arg);
262 child = root->d_inode->children;
268 ret = for_dentry_in_tree(child, visitor, arg);
272 } while (child != root->d_inode->children);
277 * Like for_dentry_in_tree(), but the visitor function is always called on a
278 * dentry's children before on itself.
280 int for_dentry_in_tree_depth(struct dentry *root,
281 int (*visitor)(struct dentry*, void*), void *arg)
284 struct dentry *child;
287 child = root->d_inode->children;
291 ret = for_dentry_in_tree_depth(child, visitor, arg);
295 } while (child != root->d_inode->children);
297 return visitor(root, arg);
301 * Calculate the full path of @dentry, based on its parent's full path and on
302 * its UTF-8 file name.
304 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
308 if (dentry_is_root(dentry)) {
309 full_path = MALLOC(2);
316 char *parent_full_path;
317 u32 parent_full_path_len;
318 const struct dentry *parent = dentry->parent;
320 if (dentry_is_root(parent)) {
321 parent_full_path = "";
322 parent_full_path_len = 0;
324 parent_full_path = parent->full_path_utf8;
325 parent_full_path_len = parent->full_path_utf8_len;
328 full_path_len = parent_full_path_len + 1 +
329 dentry->file_name_utf8_len;
330 full_path = MALLOC(full_path_len + 1);
334 memcpy(full_path, parent_full_path, parent_full_path_len);
335 full_path[parent_full_path_len] = '/';
336 memcpy(full_path + parent_full_path_len + 1,
337 dentry->file_name_utf8,
338 dentry->file_name_utf8_len);
339 full_path[full_path_len] = '\0';
341 FREE(dentry->full_path_utf8);
342 dentry->full_path_utf8 = full_path;
343 dentry->full_path_utf8_len = full_path_len;
346 ERROR("Out of memory while calculating dentry full path");
347 return WIMLIB_ERR_NOMEM;
351 * Recursively calculates the subdir offsets for a directory tree.
353 * @dentry: The root of the directory tree.
354 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
355 * offset for @dentry.
357 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
359 struct dentry *child;
361 child = dentry->d_inode->children;
362 dentry->subdir_offset = *subdir_offset_p;
365 /* Advance the subdir offset by the amount of space the children
366 * of this dentry take up. */
368 *subdir_offset_p += dentry_correct_total_length(child);
370 } while (child != dentry->d_inode->children);
372 /* End-of-directory dentry on disk. */
373 *subdir_offset_p += 8;
375 /* Recursively call calculate_subdir_offsets() on all the
378 calculate_subdir_offsets(child, subdir_offset_p);
380 } while (child != dentry->d_inode->children);
382 /* On disk, childless directories have a valid subdir_offset
383 * that points to an 8-byte end-of-directory dentry. Regular
384 * files or reparse points have a subdir_offset of 0. */
385 if (dentry_is_directory(dentry))
386 *subdir_offset_p += 8;
388 dentry->subdir_offset = 0;
392 /* Returns the child of @dentry that has the file name @name.
393 * Returns NULL if no child has the name. */
394 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
397 struct dentry *child;
400 child = dentry->d_inode->children;
402 name_len = strlen(name);
404 if (dentry_has_name(child, name, name_len))
407 } while (child != dentry->d_inode->children);
412 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
413 * @cur_dir. Returns NULL if no dentry having the path is found. */
414 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir,
417 struct dentry *child;
419 const char *new_path;
424 child = cur_dir->d_inode->children;
426 new_path = path_next_part(path, &base_len);
428 if (dentry_has_name(child, path, base_len))
429 return get_dentry_relative_path(child, new_path);
431 } while (child != cur_dir->d_inode->children);
436 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
438 struct dentry *get_dentry(WIMStruct *w, const char *path)
440 struct dentry *root = wim_root_dentry(w);
443 return get_dentry_relative_path(root, path);
446 struct inode *wim_pathname_to_inode(WIMStruct *w, const char *path)
448 struct dentry *dentry;
449 dentry = get_dentry(w, path);
453 return dentry->d_inode;
456 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
457 * if the dentry is not found. */
458 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
460 size_t path_len = strlen(path);
461 char buf[path_len + 1];
463 memcpy(buf, path, path_len + 1);
465 to_parent_name(buf, path_len);
467 return get_dentry(w, buf);
470 /* Prints the full path of a dentry. */
471 int print_dentry_full_path(struct dentry *dentry, void *ignore)
473 if (dentry->full_path_utf8)
474 puts(dentry->full_path_utf8);
478 /* We want to be able to show the names of the file attribute flags that are
480 struct file_attr_flag {
484 struct file_attr_flag file_attr_flags[] = {
485 {FILE_ATTRIBUTE_READONLY, "READONLY"},
486 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
487 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
488 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
489 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
490 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
491 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
492 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
493 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
494 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
495 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
496 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
497 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
498 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
499 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
502 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
503 * available. If the dentry is unresolved and the lookup table is NULL, the
504 * lookup table entries will not be printed. Otherwise, they will be. */
505 int print_dentry(struct dentry *dentry, void *lookup_table)
508 struct lookup_table_entry *lte;
509 const struct inode *inode = dentry->d_inode;
513 printf("[DENTRY]\n");
514 printf("Length = %"PRIu64"\n", dentry->length);
515 printf("Attributes = 0x%x\n", inode->attributes);
516 for (unsigned i = 0; i < ARRAY_LEN(file_attr_flags); i++)
517 if (file_attr_flags[i].flag & inode->attributes)
518 printf(" FILE_ATTRIBUTE_%s is set\n",
519 file_attr_flags[i].name);
520 printf("Security ID = %d\n", inode->security_id);
521 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
523 /* Translate the timestamps into something readable */
524 time = wim_timestamp_to_unix(inode->creation_time);
525 p = asctime(gmtime(&time));
526 *(strrchr(p, '\n')) = '\0';
527 printf("Creation Time = %s UTC\n", p);
529 time = wim_timestamp_to_unix(inode->last_access_time);
530 p = asctime(gmtime(&time));
531 *(strrchr(p, '\n')) = '\0';
532 printf("Last Access Time = %s UTC\n", p);
534 time = wim_timestamp_to_unix(inode->last_write_time);
535 p = asctime(gmtime(&time));
536 *(strrchr(p, '\n')) = '\0';
537 printf("Last Write Time = %s UTC\n", p);
539 printf("Reparse Tag = 0x%"PRIx32"\n", inode->reparse_tag);
540 printf("Hard Link Group = 0x%"PRIx64"\n", inode->ino);
541 printf("Hard Link Group Size = %"PRIu32"\n", inode->link_count);
542 printf("Number of Alternate Data Streams = %hu\n", inode->num_ads);
543 printf("Filename = \"");
544 print_string(dentry->file_name, dentry->file_name_len);
546 printf("Filename Length = %hu\n", dentry->file_name_len);
547 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
548 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
549 printf("Short Name = \"");
550 print_string(dentry->short_name, dentry->short_name_len);
552 printf("Short Name Length = %hu\n", dentry->short_name_len);
553 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
554 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
556 print_lookup_table_entry(lte);
558 hash = inode_stream_hash(inode, 0);
566 for (u16 i = 0; i < inode->num_ads; i++) {
567 printf("[Alternate Stream Entry %u]\n", i);
568 printf("Name = \"%s\"\n", inode->ads_entries[i].stream_name_utf8);
569 printf("Name Length (UTF-16) = %u\n",
570 inode->ads_entries[i].stream_name_len);
571 hash = inode_stream_hash(inode, i + 1);
577 print_lookup_table_entry(inode_stream_lte(inode, i + 1,
583 /* Initializations done on every `struct dentry'. */
584 static void dentry_common_init(struct dentry *dentry)
586 memset(dentry, 0, sizeof(struct dentry));
590 static struct inode *new_timeless_inode()
592 struct inode *inode = CALLOC(1, sizeof(struct inode));
594 inode->security_id = -1;
595 inode->link_count = 1;
597 inode->next_stream_id = 1;
599 INIT_LIST_HEAD(&inode->dentry_list);
604 static struct inode *new_inode()
606 struct inode *inode = new_timeless_inode();
608 u64 now = get_wim_timestamp();
609 inode->creation_time = now;
610 inode->last_access_time = now;
611 inode->last_write_time = now;
617 * Creates an unlinked directory entry.
619 * @name: The UTF-8 filename of the new dentry.
621 * Returns a pointer to the new dentry, or NULL if out of memory.
623 struct dentry *new_dentry(const char *name)
625 struct dentry *dentry;
627 dentry = MALLOC(sizeof(struct dentry));
631 dentry_common_init(dentry);
632 if (change_dentry_name(dentry, name) != 0)
635 dentry->next = dentry;
636 dentry->prev = dentry;
637 dentry->parent = dentry;
642 ERROR("Failed to allocate new dentry");
647 static struct dentry *__new_dentry_with_inode(const char *name, bool timeless)
649 struct dentry *dentry;
650 dentry = new_dentry(name);
653 dentry->d_inode = new_timeless_inode();
655 dentry->d_inode = new_inode();
656 if (dentry->d_inode) {
657 inode_add_dentry(dentry, dentry->d_inode);
666 struct dentry *new_dentry_with_timeless_inode(const char *name)
668 return __new_dentry_with_inode(name, true);
671 struct dentry *new_dentry_with_inode(const char *name)
673 return __new_dentry_with_inode(name, false);
677 static int init_ads_entry(struct ads_entry *ads_entry, const char *name)
680 memset(ads_entry, 0, sizeof(*ads_entry));
682 ret = change_ads_name(ads_entry, name);
686 static void destroy_ads_entry(struct ads_entry *ads_entry)
688 FREE(ads_entry->stream_name);
689 FREE(ads_entry->stream_name_utf8);
693 /* Frees an inode. */
694 void free_inode(struct inode *inode)
697 if (inode->ads_entries) {
698 for (u16 i = 0; i < inode->num_ads; i++)
699 destroy_ads_entry(&inode->ads_entries[i]);
700 FREE(inode->ads_entries);
703 wimlib_assert(inode->num_opened_fds == 0);
706 FREE(inode->extracted_file);
711 /* Decrements link count on an inode and frees it if the link count reaches 0.
713 static void put_inode(struct inode *inode)
715 wimlib_assert(inode);
716 wimlib_assert(inode->link_count);
717 if (--inode->link_count == 0) {
719 if (inode->num_opened_fds == 0)
727 /* Frees a WIM dentry.
729 * The inode is freed only if its link count is decremented to 0.
731 void free_dentry(struct dentry *dentry)
733 wimlib_assert(dentry);
736 FREE(dentry->file_name);
737 FREE(dentry->file_name_utf8);
738 FREE(dentry->short_name);
739 FREE(dentry->full_path_utf8);
741 put_inode(dentry->d_inode);
745 void put_dentry(struct dentry *dentry)
747 wimlib_assert(dentry);
748 wimlib_assert(dentry->refcnt);
750 if (--dentry->refcnt == 0)
755 * This function is passed as an argument to for_dentry_in_tree_depth() in order
756 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
758 static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
760 struct lookup_table *lookup_table = __lookup_table;
764 struct lookup_table_entry *lte;
765 struct inode *inode = dentry->d_inode;
766 wimlib_assert(inode->link_count);
767 for (i = 0; i <= inode->num_ads; i++) {
768 lte = inode_stream_lte(inode, i, lookup_table);
770 lte_decrement_refcnt(lte, lookup_table);
779 * Unlinks and frees a dentry tree.
781 * @root: The root of the tree.
782 * @lookup_table: The lookup table for dentries. If non-NULL, the
783 * reference counts in the lookup table for the lookup
784 * table entries corresponding to the dentries will be
787 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
789 if (!root || !root->parent)
791 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
794 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
801 * Links a dentry into the directory tree.
803 * @dentry: The dentry to link.
804 * @parent: The dentry that will be the parent of @dentry.
806 void link_dentry(struct dentry *dentry, struct dentry *parent)
808 wimlib_assert(dentry_is_directory(parent));
809 dentry->parent = parent;
810 if (parent->d_inode->children) {
811 /* Not an only child; link to siblings. */
812 dentry->next = parent->d_inode->children;
813 dentry->prev = parent->d_inode->children->prev;
814 dentry->next->prev = dentry;
815 dentry->prev->next = dentry;
817 /* Only child; link to parent. */
818 parent->d_inode->children = dentry;
819 dentry->next = dentry;
820 dentry->prev = dentry;
827 * Unlink a dentry from the directory tree.
829 * Note: This merely removes it from the in-memory tree structure.
831 void unlink_dentry(struct dentry *dentry)
833 if (dentry_is_root(dentry))
835 if (dentry_is_only_child(dentry)) {
836 dentry->parent->d_inode->children = NULL;
838 if (dentry_is_first_sibling(dentry))
839 dentry->parent->d_inode->children = dentry->next;
840 dentry->next->prev = dentry->prev;
841 dentry->prev->next = dentry->next;
846 static inline struct dentry *inode_first_dentry(struct inode *inode)
848 wimlib_assert(inode->dentry_list.next != &inode->dentry_list);
849 return container_of(inode->dentry_list.next, struct dentry,
853 static int verify_inode(struct inode *inode, const WIMStruct *w)
855 const struct lookup_table *table = w->lookup_table;
856 const struct wim_security_data *sd = wim_const_security_data(w);
857 const struct dentry *first_dentry = inode_first_dentry(inode);
858 int ret = WIMLIB_ERR_INVALID_DENTRY;
860 /* Check the security ID */
861 if (inode->security_id < -1) {
862 ERROR("Dentry `%s' has an invalid security ID (%d)",
863 first_dentry->full_path_utf8, inode->security_id);
866 if (inode->security_id >= sd->num_entries) {
867 ERROR("Dentry `%s' has an invalid security ID (%d) "
868 "(there are only %u entries in the security table)",
869 first_dentry->full_path_utf8, inode->security_id,
874 /* Check that lookup table entries for all the resources exist, except
875 * if the SHA1 message digest is all 0's, which indicates there is
876 * intentionally no resource there. */
877 if (w->hdr.total_parts == 1) {
878 for (unsigned i = 0; i <= inode->num_ads; i++) {
879 struct lookup_table_entry *lte;
881 hash = inode_stream_hash_unresolved(inode, i);
882 lte = __lookup_resource(table, hash);
883 if (!lte && !is_zero_hash(hash)) {
884 ERROR("Could not find lookup table entry for stream "
885 "%u of dentry `%s'", i, first_dentry->full_path_utf8);
888 if (lte && (lte->real_refcnt += inode->link_count) > lte->refcnt)
890 #ifdef ENABLE_ERROR_MESSAGES
891 WARNING("The following lookup table entry "
892 "has a reference count of %u, but",
894 WARNING("We found %zu references to it",
896 WARNING("(One dentry referencing it is at `%s')",
897 first_dentry->full_path_utf8);
899 print_lookup_table_entry(lte);
901 /* Guess what! install.wim for Windows 8
902 * contains a stream with 2 dentries referencing
903 * it, but the lookup table entry has reference
904 * count of 1. So we will need to handle this
905 * case and not just make it be an error... I'm
906 * just setting the reference count to the
907 * number of references we found.
908 * (Unfortunately, even after doing this, the
909 * reference count could be too low if it's also
910 * referenced in other WIM images) */
913 lte->refcnt = lte->real_refcnt;
914 WARNING("Fixing reference count");
922 /* Make sure there is only one un-named stream. */
923 unsigned num_unnamed_streams = 0;
924 for (unsigned i = 0; i <= inode->num_ads; i++) {
926 hash = inode_stream_hash_unresolved(inode, i);
927 if (!inode_stream_name_len(inode, i) && !is_zero_hash(hash))
928 num_unnamed_streams++;
930 if (num_unnamed_streams > 1) {
931 ERROR("Dentry `%s' has multiple (%u) un-named streams",
932 first_dentry->full_path_utf8, num_unnamed_streams);
935 inode->verified = true;
941 /* Run some miscellaneous verifications on a WIM dentry */
942 int verify_dentry(struct dentry *dentry, void *wim)
944 const WIMStruct *w = wim;
945 const struct inode *inode = dentry->d_inode;
946 int ret = WIMLIB_ERR_INVALID_DENTRY;
948 if (!dentry->d_inode->verified) {
949 ret = verify_inode(dentry->d_inode, w);
954 /* Cannot have a short name but no long name */
955 if (dentry->short_name_len && !dentry->file_name_len) {
956 ERROR("Dentry `%s' has a short name but no long name",
957 dentry->full_path_utf8);
961 /* Make sure root dentry is unnamed */
962 if (dentry_is_root(dentry)) {
963 if (dentry->file_name_len) {
964 ERROR("The root dentry is named `%s', but it must "
965 "be unnamed", dentry->file_name_utf8);
971 /* Check timestamps */
972 if (inode->last_access_time < inode->creation_time ||
973 inode->last_write_time < inode->creation_time) {
974 WARNING("Dentry `%s' was created after it was last accessed or "
975 "written to", dentry->full_path_utf8);
986 /* Returns the alternate data stream entry belonging to @inode that has the
987 * stream name @stream_name. */
988 struct ads_entry *inode_get_ads_entry(struct inode *inode,
989 const char *stream_name,
992 size_t stream_name_len;
995 if (inode->num_ads) {
997 stream_name_len = strlen(stream_name);
999 if (ads_entry_has_name(&inode->ads_entries[i],
1000 stream_name, stream_name_len))
1004 return &inode->ads_entries[i];
1006 } while (++i != inode->num_ads);
1012 #if defined(WITH_FUSE) || defined(WITH_NTFS_3G)
1014 * Add an alternate stream entry to an inode and return a pointer to it, or NULL
1015 * if memory could not be allocated.
1017 struct ads_entry *inode_add_ads(struct inode *inode, const char *stream_name)
1020 struct ads_entry *ads_entries;
1021 struct ads_entry *new_entry;
1023 DEBUG("Add alternate data stream \"%s\"", stream_name);
1025 if (inode->num_ads >= 0xfffe) {
1026 ERROR("Too many alternate data streams in one inode!");
1029 num_ads = inode->num_ads + 1;
1030 ads_entries = REALLOC(inode->ads_entries,
1031 num_ads * sizeof(inode->ads_entries[0]));
1033 ERROR("Failed to allocate memory for new alternate data stream");
1036 inode->ads_entries = ads_entries;
1038 new_entry = &inode->ads_entries[num_ads - 1];
1039 if (init_ads_entry(new_entry, stream_name) != 0)
1042 new_entry->stream_id = inode->next_stream_id++;
1044 inode->num_ads = num_ads;
1050 /* Remove an alternate data stream from the inode */
1051 void inode_remove_ads(struct inode *inode, u16 idx,
1052 struct lookup_table *lookup_table)
1054 struct ads_entry *ads_entry;
1055 struct lookup_table_entry *lte;
1057 wimlib_assert(idx < inode->num_ads);
1058 wimlib_assert(inode->resolved);
1060 ads_entry = &inode->ads_entries[idx];
1062 DEBUG("Remove alternate data stream \"%s\"", ads_entry->stream_name_utf8);
1064 lte = ads_entry->lte;
1066 lte_decrement_refcnt(lte, lookup_table);
1068 destroy_ads_entry(ads_entry);
1070 memcpy(&inode->ads_entries[idx],
1071 &inode->ads_entries[idx + 1],
1072 (inode->num_ads - idx - 1) * sizeof(inode->ads_entries[0]));
1080 * Reads the alternate data stream entries for a dentry.
1082 * @p: Pointer to buffer that starts with the first alternate stream entry.
1084 * @inode: Inode to load the alternate data streams into.
1085 * @inode->num_ads must have been set to the number of
1086 * alternate data streams that are expected.
1088 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1091 * The format of the on-disk alternate stream entries is as follows:
1093 * struct ads_entry_on_disk {
1094 * u64 length; // Length of the entry, in bytes. This includes
1095 * all fields (including the stream name and
1096 * null terminator if present, AND the padding!).
1097 * u64 reserved; // Seems to be unused
1098 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
1099 * u16 stream_name_len; // Length of the stream name, in bytes
1100 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
1101 * not including null terminator
1102 * u16 zero; // UTF-16 null terminator for the stream name, NOT
1103 * included in @stream_name_len. Based on what
1104 * I've observed from filenames in dentries,
1105 * this field should not exist when
1106 * (@stream_name_len == 0), but you can't
1107 * actually tell because of the padding anyway
1108 * (provided that the padding is zeroed, which
1109 * it always seems to be).
1110 * char padding[]; // Padding to make the size a multiple of 8 bytes.
1113 * In addition, the entries are 8-byte aligned.
1115 * Return 0 on success or nonzero on failure. On success, inode->ads_entries
1116 * is set to an array of `struct ads_entry's of length inode->num_ads. On
1117 * failure, @inode is not modified.
1119 static int read_ads_entries(const u8 *p, struct inode *inode,
1123 struct ads_entry *ads_entries;
1126 num_ads = inode->num_ads;
1127 ads_entries = CALLOC(num_ads, sizeof(inode->ads_entries[0]));
1129 ERROR("Could not allocate memory for %"PRIu16" "
1130 "alternate data stream entries", num_ads);
1131 return WIMLIB_ERR_NOMEM;
1134 for (u16 i = 0; i < num_ads; i++) {
1135 struct ads_entry *cur_entry;
1137 u64 length_no_padding;
1140 const u8 *p_save = p;
1142 cur_entry = &ads_entries[i];
1145 ads_entries[i].stream_id = i + 1;
1148 /* Read the base stream entry, excluding the stream name. */
1149 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1150 ERROR("Stream entries go past end of metadata resource");
1151 ERROR("(remaining_size = %"PRIu64")", remaining_size);
1152 ret = WIMLIB_ERR_INVALID_DENTRY;
1153 goto out_free_ads_entries;
1156 p = get_u64(p, &length);
1157 p += 8; /* Skip the reserved field */
1158 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
1159 p = get_u16(p, &cur_entry->stream_name_len);
1161 cur_entry->stream_name = NULL;
1162 cur_entry->stream_name_utf8 = NULL;
1164 /* Length including neither the null terminator nor the padding
1166 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1167 cur_entry->stream_name_len;
1169 /* Length including the null terminator and the padding */
1170 total_length = ((length_no_padding + 2) + 7) & ~7;
1172 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1174 if (remaining_size < length_no_padding) {
1175 ERROR("Stream entries go past end of metadata resource");
1176 ERROR("(remaining_size = %"PRIu64" bytes, "
1177 "length_no_padding = %"PRIu64" bytes)",
1178 remaining_size, length_no_padding);
1179 ret = WIMLIB_ERR_INVALID_DENTRY;
1180 goto out_free_ads_entries;
1183 /* The @length field in the on-disk ADS entry is expected to be
1184 * equal to @total_length, which includes all of the entry and
1185 * the padding that follows it to align the next ADS entry to an
1186 * 8-byte boundary. However, to be safe, we'll accept the
1187 * length field as long as it's not less than the un-padded
1188 * total length and not more than the padded total length. */
1189 if (length < length_no_padding || length > total_length) {
1190 ERROR("Stream entry has unexpected length "
1191 "field (length field = %"PRIu64", "
1192 "unpadded total length = %"PRIu64", "
1193 "padded total length = %"PRIu64")",
1194 length, length_no_padding, total_length);
1195 ret = WIMLIB_ERR_INVALID_DENTRY;
1196 goto out_free_ads_entries;
1199 if (cur_entry->stream_name_len) {
1200 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
1201 if (!cur_entry->stream_name) {
1202 ret = WIMLIB_ERR_NOMEM;
1203 goto out_free_ads_entries;
1205 get_bytes(p, cur_entry->stream_name_len,
1206 (u8*)cur_entry->stream_name);
1207 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
1208 cur_entry->stream_name_len,
1210 cur_entry->stream_name_utf8_len = utf8_len;
1212 if (!cur_entry->stream_name_utf8) {
1213 ret = WIMLIB_ERR_NOMEM;
1214 goto out_free_ads_entries;
1217 /* It's expected that the size of every ADS entry is a multiple
1218 * of 8. However, to be safe, I'm allowing the possibility of
1219 * an ADS entry at the very end of the metadata resource ending
1220 * un-aligned. So although we still need to increment the input
1221 * pointer by @total_length to reach the next ADS entry, it's
1222 * possible that less than @total_length is actually remaining
1223 * in the metadata resource. We should set the remaining size to
1224 * 0 bytes if this happens. */
1225 p = p_save + total_length;
1226 if (remaining_size < total_length)
1229 remaining_size -= total_length;
1231 inode->ads_entries = ads_entries;
1233 inode->next_stream_id = inode->num_ads + 1;
1236 out_free_ads_entries:
1237 for (u16 i = 0; i < num_ads; i++)
1238 destroy_ads_entry(&ads_entries[i]);
1244 * Reads a directory entry, including all alternate data stream entries that
1245 * follow it, from the WIM image's metadata resource.
1247 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1248 * @metadata_resource_len: Length of the metadata resource.
1249 * @offset: Offset of this directory entry in the metadata resource.
1250 * @dentry: A `struct dentry' that will be filled in by this function.
1252 * Return 0 on success or nonzero on failure. On failure, @dentry have been
1253 * modified, bu it will be left with no pointers to any allocated buffers.
1254 * On success, the dentry->length field must be examined. If zero, this was a
1255 * special "end of directory" dentry and not a real dentry. If nonzero, this
1256 * was a real dentry.
1258 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1259 u64 offset, struct dentry *dentry)
1262 u64 calculated_size;
1263 char *file_name = NULL;
1264 char *file_name_utf8 = NULL;
1265 char *short_name = NULL;
1268 size_t file_name_utf8_len = 0;
1270 struct inode *inode = NULL;
1272 dentry_common_init(dentry);
1274 /*Make sure the dentry really fits into the metadata resource.*/
1275 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1276 ERROR("Directory entry starting at %"PRIu64" ends past the "
1277 "end of the metadata resource (size %"PRIu64")",
1278 offset, metadata_resource_len);
1279 return WIMLIB_ERR_INVALID_DENTRY;
1282 /* Before reading the whole dentry, we need to read just the length.
1283 * This is because a dentry of length 8 (that is, just the length field)
1284 * terminates the list of sibling directory entries. */
1286 p = get_u64(&metadata_resource[offset], &dentry->length);
1288 /* A zero length field (really a length of 8, since that's how big the
1289 * directory entry is...) indicates that this is the end of directory
1290 * dentry. We do not read it into memory as an actual dentry, so just
1291 * return successfully in that case. */
1292 if (dentry->length == 0)
1295 /* If the dentry does not overflow the metadata resource buffer and is
1296 * not too short, read the rest of it (excluding the alternate data
1297 * streams, but including the file name and short name variable-length
1298 * fields) into memory. */
1299 if (offset + dentry->length >= metadata_resource_len
1300 || offset + dentry->length < offset)
1302 ERROR("Directory entry at offset %"PRIu64" and with size "
1303 "%"PRIu64" ends past the end of the metadata resource "
1305 offset, dentry->length, metadata_resource_len);
1306 return WIMLIB_ERR_INVALID_DENTRY;
1309 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1310 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1312 return WIMLIB_ERR_INVALID_DENTRY;
1315 inode = new_timeless_inode();
1317 return WIMLIB_ERR_NOMEM;
1319 p = get_u32(p, &inode->attributes);
1320 p = get_u32(p, (u32*)&inode->security_id);
1321 p = get_u64(p, &dentry->subdir_offset);
1323 /* 2 unused fields */
1324 p += 2 * sizeof(u64);
1325 /*p = get_u64(p, &dentry->unused1);*/
1326 /*p = get_u64(p, &dentry->unused2);*/
1328 p = get_u64(p, &inode->creation_time);
1329 p = get_u64(p, &inode->last_access_time);
1330 p = get_u64(p, &inode->last_write_time);
1332 p = get_bytes(p, SHA1_HASH_SIZE, inode->hash);
1335 * I don't know what's going on here. It seems like M$ screwed up the
1336 * reparse points, then put the fields in the same place and didn't
1337 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1338 * have something to do with this, but it's not documented.
1340 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1343 p = get_u32(p, &inode->reparse_tag);
1346 p = get_u32(p, &inode->reparse_tag);
1347 p = get_u64(p, &inode->ino);
1350 /* By the way, the reparse_reserved field does not actually exist (at
1351 * least when the file is not a reparse point) */
1353 p = get_u16(p, &inode->num_ads);
1355 p = get_u16(p, &short_name_len);
1356 p = get_u16(p, &file_name_len);
1358 /* We now know the length of the file name and short name. Make sure
1359 * the length of the dentry is large enough to actually hold them.
1361 * The calculated length here is unaligned to allow for the possibility
1362 * that the dentry->length names an unaligned length, although this
1363 * would be unexpected. */
1364 calculated_size = __dentry_correct_length_unaligned(file_name_len,
1367 if (dentry->length < calculated_size) {
1368 ERROR("Unexpected end of directory entry! (Expected "
1369 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1370 "short_name_len = %hu, file_name_len = %hu)",
1371 calculated_size, dentry->length,
1372 short_name_len, file_name_len);
1373 return WIMLIB_ERR_INVALID_DENTRY;
1376 /* Read the filename if present. Note: if the filename is empty, there
1377 * is no null terminator following it. */
1378 if (file_name_len) {
1379 file_name = MALLOC(file_name_len);
1381 ERROR("Failed to allocate %hu bytes for dentry file name",
1383 return WIMLIB_ERR_NOMEM;
1385 p = get_bytes(p, file_name_len, file_name);
1387 /* Convert filename to UTF-8. */
1388 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1389 &file_name_utf8_len);
1391 if (!file_name_utf8) {
1392 ERROR("Failed to allocate memory to convert UTF-16 "
1393 "filename (%hu bytes) to UTF-8", file_name_len);
1394 ret = WIMLIB_ERR_NOMEM;
1395 goto out_free_file_name;
1398 WARNING("Expected two zero bytes following the file name "
1399 "`%s', but found non-zero bytes", file_name_utf8);
1403 /* Align the calculated size */
1404 calculated_size = (calculated_size + 7) & ~7;
1406 if (dentry->length > calculated_size) {
1407 /* Weird; the dentry says it's longer than it should be. Note
1408 * that the length field does NOT include the size of the
1409 * alternate stream entries. */
1411 /* Strangely, some directory entries inexplicably have a little
1412 * over 70 bytes of extra data. The exact amount of data seems
1413 * to be 72 bytes, but it is aligned on the next 8-byte
1414 * boundary. It does NOT seem to be alternate data stream
1415 * entries. Here's an example of the aligned data:
1417 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1418 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1419 * 00000000 00000000 00000000 00000000
1421 * Here's one interpretation of how the data is laid out.
1424 * u32 field1; (always 0x00000001)
1425 * u32 field2; (always 0x40000000)
1426 * u8 data[48]; (???)
1427 * u64 reserved1; (always 0)
1428 * u64 reserved2; (always 0)
1430 DEBUG("Dentry for file or directory `%s' has %zu extra "
1432 file_name_utf8, dentry->length - calculated_size);
1435 /* Read the short filename if present. Note: if there is no short
1436 * filename, there is no null terminator following it. */
1437 if (short_name_len) {
1438 short_name = MALLOC(short_name_len);
1440 ERROR("Failed to allocate %hu bytes for short filename",
1442 ret = WIMLIB_ERR_NOMEM;
1443 goto out_free_file_name_utf8;
1446 p = get_bytes(p, short_name_len, short_name);
1448 WARNING("Expected two zero bytes following the file name "
1449 "`%s', but found non-zero bytes", file_name_utf8);
1454 * Read the alternate data streams, if present. dentry->num_ads tells
1455 * us how many they are, and they will directly follow the dentry
1458 * Note that each alternate data stream entry begins on an 8-byte
1459 * aligned boundary, and the alternate data stream entries are NOT
1460 * included in the dentry->length field for some reason.
1462 if (inode->num_ads != 0) {
1463 if (calculated_size > metadata_resource_len - offset) {
1464 ERROR("Not enough space in metadata resource for "
1465 "alternate stream entries");
1466 ret = WIMLIB_ERR_INVALID_DENTRY;
1467 goto out_free_short_name;
1469 ret = read_ads_entries(&metadata_resource[offset + calculated_size],
1471 metadata_resource_len - offset - calculated_size);
1473 goto out_free_short_name;
1476 /* We've read all the data for this dentry. Set the names and their
1477 * lengths, and we've done. */
1478 dentry->d_inode = inode;
1479 dentry->file_name = file_name;
1480 dentry->file_name_utf8 = file_name_utf8;
1481 dentry->short_name = short_name;
1482 dentry->file_name_len = file_name_len;
1483 dentry->file_name_utf8_len = file_name_utf8_len;
1484 dentry->short_name_len = short_name_len;
1486 out_free_short_name:
1488 out_free_file_name_utf8:
1489 FREE(file_name_utf8);
1497 /* Reads the children of a dentry, and all their children, ..., etc. from the
1498 * metadata resource and into the dentry tree.
1500 * @metadata_resource: An array that contains the uncompressed metadata
1501 * resource for the WIM file.
1503 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1506 * @dentry: A pointer to a `struct dentry' that is the root of the directory
1507 * tree and has already been read from the metadata resource. It
1508 * does not need to be the real root because this procedure is
1509 * called recursively.
1511 * @return: Zero on success, nonzero on failure.
1513 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1514 struct dentry *dentry)
1516 u64 cur_offset = dentry->subdir_offset;
1517 struct dentry *prev_child = NULL;
1518 struct dentry *first_child = NULL;
1519 struct dentry *child;
1520 struct dentry cur_child;
1524 * If @dentry has no child dentries, nothing more needs to be done for
1525 * this branch. This is the case for regular files, symbolic links, and
1526 * *possibly* empty directories (although an empty directory may also
1527 * have one child dentry that is the special end-of-directory dentry)
1529 if (cur_offset == 0)
1532 /* Find and read all the children of @dentry. */
1535 /* Read next child of @dentry into @cur_child. */
1536 ret = read_dentry(metadata_resource, metadata_resource_len,
1537 cur_offset, &cur_child);
1541 /* Check for end of directory. */
1542 if (cur_child.length == 0)
1545 /* Not end of directory. Allocate this child permanently and
1546 * link it to the parent and previous child. */
1547 child = MALLOC(sizeof(struct dentry));
1549 ERROR("Failed to allocate %zu bytes for new dentry",
1550 sizeof(struct dentry));
1551 ret = WIMLIB_ERR_NOMEM;
1554 memcpy(child, &cur_child, sizeof(struct dentry));
1557 prev_child->next = child;
1558 child->prev = prev_child;
1560 first_child = child;
1563 child->parent = dentry;
1565 inode_add_dentry(child, child->d_inode);
1567 /* If there are children of this child, call this procedure
1569 if (child->subdir_offset != 0) {
1570 ret = read_dentry_tree(metadata_resource,
1571 metadata_resource_len, child);
1576 /* Advance to the offset of the next child. Note: We need to
1577 * advance by the TOTAL length of the dentry, not by the length
1578 * child->length, which although it does take into account the
1579 * padding, it DOES NOT take into account alternate stream
1581 cur_offset += dentry_total_length(child);
1584 /* Link last child to first one, and set parent's children pointer to
1585 * the first child. */
1587 prev_child->next = first_child;
1588 first_child->prev = prev_child;
1590 dentry->d_inode->children = first_child;
1595 * Writes a WIM dentry to an output buffer.
1597 * @dentry: The dentry structure.
1598 * @p: The memory location to write the data to.
1599 * @return: Pointer to the byte after the last byte we wrote as part of the
1602 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1606 const struct inode *inode = dentry->d_inode;
1608 /* We calculate the correct length of the dentry ourselves because the
1609 * dentry->length field may been set to an unexpected value from when we
1610 * read the dentry in (for example, there may have been unknown data
1611 * appended to the end of the dentry...) */
1612 u64 length = dentry_correct_length(dentry);
1614 p = put_u64(p, length);
1615 p = put_u32(p, inode->attributes);
1616 p = put_u32(p, inode->security_id);
1617 p = put_u64(p, dentry->subdir_offset);
1618 p = put_u64(p, 0); /* unused1 */
1619 p = put_u64(p, 0); /* unused2 */
1620 p = put_u64(p, inode->creation_time);
1621 p = put_u64(p, inode->last_access_time);
1622 p = put_u64(p, inode->last_write_time);
1623 hash = inode_stream_hash(inode, 0);
1624 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1625 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1626 p = put_zeroes(p, 4);
1627 p = put_u32(p, inode->reparse_tag);
1628 p = put_zeroes(p, 4);
1632 if (inode->link_count == 1)
1635 link_group_id = inode->ino;
1636 p = put_u64(p, link_group_id);
1638 p = put_u16(p, inode->num_ads);
1639 p = put_u16(p, dentry->short_name_len);
1640 p = put_u16(p, dentry->file_name_len);
1641 if (dentry->file_name_len) {
1642 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1643 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1645 if (dentry->short_name) {
1646 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1647 p = put_u16(p, 0); /* short name padding, 2 bytes */
1650 /* Align to 8-byte boundary */
1651 wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1652 p = put_zeroes(p, length - (p - orig_p));
1654 /* Write the alternate data streams, if there are any. Please see
1655 * read_ads_entries() for comments about the format of the on-disk
1656 * alternate data stream entries. */
1657 for (u16 i = 0; i < inode->num_ads; i++) {
1658 p = put_u64(p, ads_entry_total_length(&inode->ads_entries[i]));
1659 p = put_u64(p, 0); /* Unused */
1660 hash = inode_stream_hash(inode, i + 1);
1661 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1662 p = put_u16(p, inode->ads_entries[i].stream_name_len);
1663 if (inode->ads_entries[i].stream_name_len) {
1664 p = put_bytes(p, inode->ads_entries[i].stream_name_len,
1665 (u8*)inode->ads_entries[i].stream_name);
1668 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1670 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1674 /* Recursive function that writes a dentry tree rooted at @parent, not including
1675 * @parent itself, which has already been written. */
1676 static u8 *write_dentry_tree_recursive(const struct dentry *parent, u8 *p)
1678 const struct dentry *child;
1680 /* Nothing to do if this dentry has no children. */
1681 if (parent->subdir_offset == 0)
1684 /* Write child dentries and end-of-directory entry.
1686 * Note: we need to write all of this dentry's children before
1687 * recursively writing the directory trees rooted at each of the child
1688 * dentries, since the on-disk dentries for a dentry's children are
1689 * always located at consecutive positions in the metadata resource! */
1690 child = parent->d_inode->children;
1693 p = write_dentry(child, p);
1694 child = child->next;
1695 } while (child != parent->d_inode->children);
1698 /* write end of directory entry */
1701 /* Recurse on children. */
1704 p = write_dentry_tree_recursive(child, p);
1705 child = child->next;
1706 } while (child != parent->d_inode->children);
1711 /* Writes a directory tree to the metadata resource.
1713 * @root: Root of the dentry tree.
1714 * @p: Pointer to a buffer with enough space for the dentry tree.
1716 * Returns pointer to the byte after the last byte we wrote.
1718 u8 *write_dentry_tree(const struct dentry *root, u8 *p)
1720 wimlib_assert(dentry_is_root(root));
1722 /* If we're the root dentry, we have no parent that already
1723 * wrote us, so we need to write ourselves. */
1724 p = write_dentry(root, p);
1726 /* Write end of directory entry after the root dentry just to be safe;
1727 * however the root dentry obviously cannot have any siblings. */
1730 /* Recursively write the rest of the dentry tree. */
1731 return write_dentry_tree_recursive(root, p);