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 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
44 * a file name and short name that take the specified numbers of bytes. This
45 * excludes any alternate data stream entries that may follow the dentry. */
46 static u64 __dentry_correct_length_unaligned(u16 file_name_len,
49 u64 length = WIM_DENTRY_DISK_SIZE;
51 length += file_name_len + 2;
53 length += short_name_len + 2;
57 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
58 * the file name length and short name length. Note that dentry->length is
59 * ignored; also, this excludes any alternate data stream entries that may
60 * follow the dentry. */
61 static u64 dentry_correct_length_unaligned(const struct dentry *dentry)
63 return __dentry_correct_length_unaligned(dentry->file_name_len,
64 dentry->short_name_len);
67 /* Return the "correct" value to write in the length field of a WIM dentry,
68 * based on the file name length and short name length. */
69 static u64 dentry_correct_length(const struct dentry *dentry)
71 return (dentry_correct_length_unaligned(dentry) + 7) & ~7;
74 /* Return %true iff @dentry has the UTF-8 file name @name that has length
76 static bool dentry_has_name(const struct dentry *dentry, const char *name,
79 if (dentry->file_name_utf8_len != name_len)
81 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
84 /* Return %true iff the alternate data stream entry @entry has the UTF-8 stream
85 * name @name that has length @name_len bytes. */
86 static inline bool ads_entry_has_name(const struct ads_entry *entry,
87 const char *name, size_t name_len)
89 if (entry->stream_name_utf8_len != name_len)
91 return memcmp(entry->stream_name_utf8, name, name_len) == 0;
94 /* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
95 * and their lengths in the pointer arguments */
96 int get_names(char **name_utf16_ret, char **name_utf8_ret,
97 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
102 char *name_utf16, *name_utf8;
104 utf8_len = strlen(name);
106 name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
109 return WIMLIB_ERR_NOMEM;
111 name_utf8 = MALLOC(utf8_len + 1);
114 return WIMLIB_ERR_NOMEM;
116 memcpy(name_utf8, name, utf8_len + 1);
117 FREE(*name_utf8_ret);
118 FREE(*name_utf16_ret);
119 *name_utf8_ret = name_utf8;
120 *name_utf16_ret = name_utf16;
121 *name_utf8_len_ret = utf8_len;
122 *name_utf16_len_ret = utf16_len;
126 /* Changes the name of a dentry to @new_name. Only changes the file_name and
127 * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
128 * full_path_utf8 fields. Also recalculates its length. */
129 static int change_dentry_name(struct dentry *dentry, const char *new_name)
133 ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
134 &dentry->file_name_len, &dentry->file_name_utf8_len,
136 FREE(dentry->short_name);
137 dentry->short_name_len = 0;
139 dentry->length = dentry_correct_length(dentry);
144 * Changes the name of an alternate data stream */
145 static int change_ads_name(struct ads_entry *entry, const char *new_name)
147 return get_names(&entry->stream_name, &entry->stream_name_utf8,
148 &entry->stream_name_len,
149 &entry->stream_name_utf8_len,
153 /* Returns the total length of a WIM alternate data stream entry on-disk,
154 * including the stream name, the null terminator, AND the padding after the
155 * entry to align the next one (or the next dentry) on an 8-byte boundary. */
156 static u64 ads_entry_total_length(const struct ads_entry *entry)
158 u64 len = WIM_ADS_ENTRY_DISK_SIZE;
159 if (entry->stream_name_len)
160 len += entry->stream_name_len + 2;
161 return (len + 7) & ~7;
165 static u64 __dentry_total_length(const struct dentry *dentry, u64 length)
167 const struct inode *inode = dentry->d_inode;
168 for (u16 i = 0; i < inode->num_ads; i++)
169 length += ads_entry_total_length(&inode->ads_entries[i]);
170 return (length + 7) & ~7;
173 /* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
174 * all alternate data streams. */
175 u64 dentry_correct_total_length(const struct dentry *dentry)
177 return __dentry_total_length(dentry,
178 dentry_correct_length_unaligned(dentry));
181 /* Like dentry_correct_total_length(), but use the existing dentry->length field
182 * instead of calculating its "correct" value. */
183 static u64 dentry_total_length(const struct dentry *dentry)
185 return __dentry_total_length(dentry, dentry->length);
188 /* Transfers file attributes from a `stat' buffer to a WIM "inode". */
189 void stbuf_to_inode(const struct stat *stbuf, struct inode *inode)
191 if (S_ISLNK(stbuf->st_mode)) {
192 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
193 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
194 } else if (S_ISDIR(stbuf->st_mode)) {
195 inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
197 inode->attributes = FILE_ATTRIBUTE_NORMAL;
199 if (sizeof(ino_t) >= 8)
200 inode->ino = (u64)stbuf->st_ino;
202 inode->ino = (u64)stbuf->st_ino |
203 ((u64)stbuf->st_dev << ((sizeof(ino_t) * 8) & 63));
205 inode->creation_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
206 inode->last_write_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
207 inode->last_access_time = timespec_to_wim_timestamp(&stbuf->st_atim);
211 /* Transfers file attributes from a struct inode to a `stat' buffer.
213 * The lookup table entry tells us which stream in the inode we are statting.
214 * For a named data stream, everything returned is the same as the unnamed data
215 * stream except possibly the size and block count. */
216 int inode_to_stbuf(const struct inode *inode, struct lookup_table_entry *lte,
219 if (inode_is_symlink(inode))
220 stbuf->st_mode = S_IFLNK | 0777;
221 else if (inode_is_directory(inode))
222 stbuf->st_mode = S_IFDIR | 0755;
224 stbuf->st_mode = S_IFREG | 0644;
226 stbuf->st_ino = (ino_t)inode->ino;
227 stbuf->st_nlink = inode->link_count;
228 stbuf->st_uid = getuid();
229 stbuf->st_gid = getgid();
232 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
233 wimlib_assert(lte->staging_file_name);
234 struct stat native_stat;
235 if (stat(lte->staging_file_name, &native_stat) != 0) {
236 DEBUG("Failed to stat `%s': %m",
237 lte->staging_file_name);
240 stbuf->st_size = native_stat.st_size;
242 stbuf->st_size = wim_resource_size(lte);
248 stbuf->st_atime = wim_timestamp_to_unix(inode->last_access_time);
249 stbuf->st_mtime = wim_timestamp_to_unix(inode->last_write_time);
250 stbuf->st_ctime = wim_timestamp_to_unix(inode->creation_time);
251 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
257 * Calls a function on all directory entries in a directory tree. It is called
258 * on a parent before its children.
260 int for_dentry_in_tree(struct dentry *root,
261 int (*visitor)(struct dentry*, void*), void *arg)
264 struct dentry *child;
266 ret = visitor(root, arg);
271 child = root->d_inode->children;
277 ret = for_dentry_in_tree(child, visitor, arg);
281 } while (child != root->d_inode->children);
286 * Like for_dentry_in_tree(), but the visitor function is always called on a
287 * dentry's children before on itself.
289 int for_dentry_in_tree_depth(struct dentry *root,
290 int (*visitor)(struct dentry*, void*), void *arg)
293 struct dentry *child;
296 child = root->d_inode->children;
300 ret = for_dentry_in_tree_depth(child, visitor, arg);
304 } while (child != root->d_inode->children);
306 return visitor(root, arg);
310 * Calculate the full path of @dentry, based on its parent's full path and on
311 * its UTF-8 file name.
313 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
317 if (dentry_is_root(dentry)) {
318 full_path = MALLOC(2);
325 char *parent_full_path;
326 u32 parent_full_path_len;
327 const struct dentry *parent = dentry->parent;
329 if (dentry_is_root(parent)) {
330 parent_full_path = "";
331 parent_full_path_len = 0;
333 parent_full_path = parent->full_path_utf8;
334 parent_full_path_len = parent->full_path_utf8_len;
337 full_path_len = parent_full_path_len + 1 +
338 dentry->file_name_utf8_len;
339 full_path = MALLOC(full_path_len + 1);
343 memcpy(full_path, parent_full_path, parent_full_path_len);
344 full_path[parent_full_path_len] = '/';
345 memcpy(full_path + parent_full_path_len + 1,
346 dentry->file_name_utf8,
347 dentry->file_name_utf8_len);
348 full_path[full_path_len] = '\0';
350 FREE(dentry->full_path_utf8);
351 dentry->full_path_utf8 = full_path;
352 dentry->full_path_utf8_len = full_path_len;
355 ERROR("Out of memory while calculating dentry full path");
356 return WIMLIB_ERR_NOMEM;
360 * Recursively calculates the subdir offsets for a directory tree.
362 * @dentry: The root of the directory tree.
363 * @subdir_offset_p: The current subdirectory offset; i.e., the subdirectory
364 * offset for @dentry.
366 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
368 struct dentry *child, *children;
370 children = dentry->d_inode->children;
372 dentry->subdir_offset = *subdir_offset_p;
375 /* Advance the subdir offset by the amount of space the children
376 * of this dentry take up. */
378 *subdir_offset_p += dentry_correct_total_length(child);
380 } while (child != children);
382 /* End-of-directory dentry on disk. */
383 *subdir_offset_p += 8;
385 /* Recursively call calculate_subdir_offsets() on all the
388 calculate_subdir_offsets(child, subdir_offset_p);
390 } while (child != children);
392 /* On disk, childless directories have a valid subdir_offset
393 * that points to an 8-byte end-of-directory dentry. Regular
394 * files or reparse points have a subdir_offset of 0. */
395 if (dentry_is_directory(dentry))
396 *subdir_offset_p += 8;
398 dentry->subdir_offset = 0;
402 /* Returns the child of @dentry that has the file name @name.
403 * Returns NULL if no child has the name. */
404 struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
407 struct dentry *child;
410 child = dentry->d_inode->children;
412 name_len = strlen(name);
414 if (dentry_has_name(child, name, name_len))
417 } while (child != dentry->d_inode->children);
422 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
423 * @cur_dir. Returns NULL if no dentry having the path is found. */
424 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir,
427 struct dentry *child;
429 const char *new_path;
434 child = cur_dir->d_inode->children;
436 new_path = path_next_part(path, &base_len);
438 if (dentry_has_name(child, path, base_len))
439 return get_dentry_relative_path(child, new_path);
441 } while (child != cur_dir->d_inode->children);
446 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
448 struct dentry *get_dentry(WIMStruct *w, const char *path)
450 struct dentry *root = wim_root_dentry(w);
453 return get_dentry_relative_path(root, path);
456 struct inode *wim_pathname_to_inode(WIMStruct *w, const char *path)
458 struct dentry *dentry;
459 dentry = get_dentry(w, path);
463 return dentry->d_inode;
466 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
467 * if the dentry is not found. */
468 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
470 size_t path_len = strlen(path);
471 char buf[path_len + 1];
473 memcpy(buf, path, path_len + 1);
475 to_parent_name(buf, path_len);
477 return get_dentry(w, buf);
480 /* Prints the full path of a dentry. */
481 int print_dentry_full_path(struct dentry *dentry, void *ignore)
483 if (dentry->full_path_utf8)
484 puts(dentry->full_path_utf8);
488 /* We want to be able to show the names of the file attribute flags that are
490 struct file_attr_flag {
494 struct file_attr_flag file_attr_flags[] = {
495 {FILE_ATTRIBUTE_READONLY, "READONLY"},
496 {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
497 {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
498 {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
499 {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
500 {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
501 {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
502 {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
503 {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
504 {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
505 {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
506 {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
507 {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
508 {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
509 {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
512 /* Prints a directory entry. @lookup_table is a pointer to the lookup table, if
513 * available. If the dentry is unresolved and the lookup table is NULL, the
514 * lookup table entries will not be printed. Otherwise, they will be. */
515 int print_dentry(struct dentry *dentry, void *lookup_table)
518 struct lookup_table_entry *lte;
519 const struct inode *inode = dentry->d_inode;
523 printf("[DENTRY]\n");
524 printf("Length = %"PRIu64"\n", dentry->length);
525 printf("Attributes = 0x%x\n", inode->attributes);
526 for (unsigned i = 0; i < ARRAY_LEN(file_attr_flags); i++)
527 if (file_attr_flags[i].flag & inode->attributes)
528 printf(" FILE_ATTRIBUTE_%s is set\n",
529 file_attr_flags[i].name);
530 printf("Security ID = %d\n", inode->security_id);
531 printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
533 /* Translate the timestamps into something readable */
534 time = wim_timestamp_to_unix(inode->creation_time);
535 p = asctime(gmtime(&time));
536 *(strrchr(p, '\n')) = '\0';
537 printf("Creation Time = %s UTC\n", p);
539 time = wim_timestamp_to_unix(inode->last_access_time);
540 p = asctime(gmtime(&time));
541 *(strrchr(p, '\n')) = '\0';
542 printf("Last Access Time = %s UTC\n", p);
544 time = wim_timestamp_to_unix(inode->last_write_time);
545 p = asctime(gmtime(&time));
546 *(strrchr(p, '\n')) = '\0';
547 printf("Last Write Time = %s UTC\n", p);
549 printf("Reparse Tag = 0x%"PRIx32"\n", inode->reparse_tag);
550 printf("Hard Link Group = 0x%"PRIx64"\n", inode->ino);
551 printf("Hard Link Group Size = %"PRIu32"\n", inode->link_count);
552 printf("Number of Alternate Data Streams = %hu\n", inode->num_ads);
553 printf("Filename = \"");
554 print_string(dentry->file_name, dentry->file_name_len);
556 printf("Filename Length = %hu\n", dentry->file_name_len);
557 printf("Filename (UTF-8) = \"%s\"\n", dentry->file_name_utf8);
558 printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
559 printf("Short Name = \"");
560 print_string(dentry->short_name, dentry->short_name_len);
562 printf("Short Name Length = %hu\n", dentry->short_name_len);
563 printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
564 lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
566 print_lookup_table_entry(lte);
568 hash = inode_stream_hash(inode, 0);
576 for (u16 i = 0; i < inode->num_ads; i++) {
577 printf("[Alternate Stream Entry %u]\n", i);
578 printf("Name = \"%s\"\n", inode->ads_entries[i].stream_name_utf8);
579 printf("Name Length (UTF-16) = %u\n",
580 inode->ads_entries[i].stream_name_len);
581 hash = inode_stream_hash(inode, i + 1);
587 print_lookup_table_entry(inode_stream_lte(inode, i + 1,
593 /* Initializations done on every `struct dentry'. */
594 static void dentry_common_init(struct dentry *dentry)
596 memset(dentry, 0, sizeof(struct dentry));
600 static struct inode *new_timeless_inode()
602 struct inode *inode = CALLOC(1, sizeof(struct inode));
604 inode->security_id = -1;
605 inode->link_count = 1;
607 inode->next_stream_id = 1;
609 INIT_LIST_HEAD(&inode->dentry_list);
614 static struct inode *new_inode()
616 struct inode *inode = new_timeless_inode();
618 u64 now = get_wim_timestamp();
619 inode->creation_time = now;
620 inode->last_access_time = now;
621 inode->last_write_time = now;
627 * Creates an unlinked directory entry.
629 * @name: The UTF-8 filename of the new dentry.
631 * Returns a pointer to the new dentry, or NULL if out of memory.
633 struct dentry *new_dentry(const char *name)
635 struct dentry *dentry;
637 dentry = MALLOC(sizeof(struct dentry));
641 dentry_common_init(dentry);
642 if (change_dentry_name(dentry, name) != 0)
645 dentry->next = dentry;
646 dentry->prev = dentry;
647 dentry->parent = dentry;
652 ERROR("Failed to allocate new dentry");
657 static struct dentry *__new_dentry_with_inode(const char *name, bool timeless)
659 struct dentry *dentry;
660 dentry = new_dentry(name);
663 dentry->d_inode = new_timeless_inode();
665 dentry->d_inode = new_inode();
666 if (dentry->d_inode) {
667 inode_add_dentry(dentry, dentry->d_inode);
676 struct dentry *new_dentry_with_timeless_inode(const char *name)
678 return __new_dentry_with_inode(name, true);
681 struct dentry *new_dentry_with_inode(const char *name)
683 return __new_dentry_with_inode(name, false);
687 static int init_ads_entry(struct ads_entry *ads_entry, const char *name)
690 memset(ads_entry, 0, sizeof(*ads_entry));
692 ret = change_ads_name(ads_entry, name);
696 static void destroy_ads_entry(struct ads_entry *ads_entry)
698 FREE(ads_entry->stream_name);
699 FREE(ads_entry->stream_name_utf8);
703 /* Frees an inode. */
704 void free_inode(struct inode *inode)
707 if (inode->ads_entries) {
708 for (u16 i = 0; i < inode->num_ads; i++)
709 destroy_ads_entry(&inode->ads_entries[i]);
710 FREE(inode->ads_entries);
713 wimlib_assert(inode->num_opened_fds == 0);
716 FREE(inode->extracted_file);
721 /* Decrements link count on an inode and frees it if the link count reaches 0.
723 static void put_inode(struct inode *inode)
725 wimlib_assert(inode);
726 wimlib_assert(inode->link_count);
727 if (--inode->link_count == 0) {
729 if (inode->num_opened_fds == 0)
737 /* Frees a WIM dentry.
739 * The inode is freed only if its link count is decremented to 0.
741 void free_dentry(struct dentry *dentry)
743 wimlib_assert(dentry);
746 FREE(dentry->file_name);
747 FREE(dentry->file_name_utf8);
748 FREE(dentry->short_name);
749 FREE(dentry->full_path_utf8);
751 put_inode(dentry->d_inode);
755 void put_dentry(struct dentry *dentry)
757 wimlib_assert(dentry);
758 wimlib_assert(dentry->refcnt);
760 if (--dentry->refcnt == 0)
765 * This function is passed as an argument to for_dentry_in_tree_depth() in order
766 * to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
768 static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
770 struct lookup_table *lookup_table = __lookup_table;
774 struct lookup_table_entry *lte;
775 struct inode *inode = dentry->d_inode;
776 wimlib_assert(inode->link_count);
777 for (i = 0; i <= inode->num_ads; i++) {
778 lte = inode_stream_lte(inode, i, lookup_table);
780 lte_decrement_refcnt(lte, lookup_table);
789 * Unlinks and frees a dentry tree.
791 * @root: The root of the tree.
792 * @lookup_table: The lookup table for dentries. If non-NULL, the
793 * reference counts in the lookup table for the lookup
794 * table entries corresponding to the dentries will be
797 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
799 if (!root || !root->parent)
801 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
804 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
811 * Links a dentry into the directory tree.
813 * @dentry: The dentry to link.
814 * @parent: The dentry that will be the parent of @dentry.
816 void link_dentry(struct dentry *dentry, struct dentry *parent)
818 wimlib_assert(dentry_is_directory(parent));
819 dentry->parent = parent;
820 if (parent->d_inode->children) {
821 /* Not an only child; link to siblings. */
822 dentry->next = parent->d_inode->children;
823 dentry->prev = parent->d_inode->children->prev;
824 dentry->next->prev = dentry;
825 dentry->prev->next = dentry;
827 /* Only child; link to parent. */
828 parent->d_inode->children = dentry;
829 dentry->next = dentry;
830 dentry->prev = dentry;
837 * Unlink a dentry from the directory tree.
839 * Note: This merely removes it from the in-memory tree structure.
841 void unlink_dentry(struct dentry *dentry)
843 if (dentry_is_root(dentry))
845 if (dentry_is_only_child(dentry)) {
846 dentry->parent->d_inode->children = NULL;
848 if (dentry_is_first_sibling(dentry))
849 dentry->parent->d_inode->children = dentry->next;
850 dentry->next->prev = dentry->prev;
851 dentry->prev->next = dentry->next;
856 static inline struct dentry *inode_first_dentry(struct inode *inode)
858 wimlib_assert(inode->dentry_list.next != &inode->dentry_list);
859 return container_of(inode->dentry_list.next, struct dentry,
863 static int verify_inode(struct inode *inode, const WIMStruct *w)
865 const struct lookup_table *table = w->lookup_table;
866 const struct wim_security_data *sd = wim_const_security_data(w);
867 const struct dentry *first_dentry = inode_first_dentry(inode);
868 int ret = WIMLIB_ERR_INVALID_DENTRY;
870 /* Check the security ID */
871 if (inode->security_id < -1) {
872 ERROR("Dentry `%s' has an invalid security ID (%d)",
873 first_dentry->full_path_utf8, inode->security_id);
876 if (inode->security_id >= sd->num_entries) {
877 ERROR("Dentry `%s' has an invalid security ID (%d) "
878 "(there are only %u entries in the security table)",
879 first_dentry->full_path_utf8, inode->security_id,
884 /* Check that lookup table entries for all the resources exist, except
885 * if the SHA1 message digest is all 0's, which indicates there is
886 * intentionally no resource there. */
887 if (w->hdr.total_parts == 1) {
888 for (unsigned i = 0; i <= inode->num_ads; i++) {
889 struct lookup_table_entry *lte;
891 hash = inode_stream_hash_unresolved(inode, i);
892 lte = __lookup_resource(table, hash);
893 if (!lte && !is_zero_hash(hash)) {
894 ERROR("Could not find lookup table entry for stream "
895 "%u of dentry `%s'", i, first_dentry->full_path_utf8);
898 if (lte && (lte->real_refcnt += inode->link_count) > lte->refcnt)
900 #ifdef ENABLE_ERROR_MESSAGES
901 WARNING("The following lookup table entry "
902 "has a reference count of %u, but",
904 WARNING("We found %zu references to it",
906 WARNING("(One dentry referencing it is at `%s')",
907 first_dentry->full_path_utf8);
909 print_lookup_table_entry(lte);
911 /* Guess what! install.wim for Windows 8
912 * contains a stream with 2 dentries referencing
913 * it, but the lookup table entry has reference
914 * count of 1. So we will need to handle this
915 * case and not just make it be an error... I'm
916 * just setting the reference count to the
917 * number of references we found.
918 * (Unfortunately, even after doing this, the
919 * reference count could be too low if it's also
920 * referenced in other WIM images) */
923 lte->refcnt = lte->real_refcnt;
924 WARNING("Fixing reference count");
932 /* Make sure there is only one un-named stream. */
933 unsigned num_unnamed_streams = 0;
934 for (unsigned i = 0; i <= inode->num_ads; i++) {
936 hash = inode_stream_hash_unresolved(inode, i);
937 if (!inode_stream_name_len(inode, i) && !is_zero_hash(hash))
938 num_unnamed_streams++;
940 if (num_unnamed_streams > 1) {
941 ERROR("Dentry `%s' has multiple (%u) un-named streams",
942 first_dentry->full_path_utf8, num_unnamed_streams);
945 inode->verified = true;
951 /* Run some miscellaneous verifications on a WIM dentry */
952 int verify_dentry(struct dentry *dentry, void *wim)
954 const WIMStruct *w = wim;
955 const struct inode *inode = dentry->d_inode;
956 int ret = WIMLIB_ERR_INVALID_DENTRY;
958 if (!dentry->d_inode->verified) {
959 ret = verify_inode(dentry->d_inode, w);
964 /* Cannot have a short name but no long name */
965 if (dentry->short_name_len && !dentry->file_name_len) {
966 ERROR("Dentry `%s' has a short name but no long name",
967 dentry->full_path_utf8);
971 /* Make sure root dentry is unnamed */
972 if (dentry_is_root(dentry)) {
973 if (dentry->file_name_len) {
974 ERROR("The root dentry is named `%s', but it must "
975 "be unnamed", dentry->file_name_utf8);
981 /* Check timestamps */
982 if (inode->last_access_time < inode->creation_time ||
983 inode->last_write_time < inode->creation_time) {
984 WARNING("Dentry `%s' was created after it was last accessed or "
985 "written to", dentry->full_path_utf8);
996 /* Returns the alternate data stream entry belonging to @inode that has the
997 * stream name @stream_name. */
998 struct ads_entry *inode_get_ads_entry(struct inode *inode,
999 const char *stream_name,
1002 size_t stream_name_len;
1005 if (inode->num_ads) {
1007 stream_name_len = strlen(stream_name);
1009 if (ads_entry_has_name(&inode->ads_entries[i],
1010 stream_name, stream_name_len))
1014 return &inode->ads_entries[i];
1016 } while (++i != inode->num_ads);
1022 #if defined(WITH_FUSE) || defined(WITH_NTFS_3G)
1024 * Add an alternate stream entry to an inode and return a pointer to it, or NULL
1025 * if memory could not be allocated.
1027 struct ads_entry *inode_add_ads(struct inode *inode, const char *stream_name)
1030 struct ads_entry *ads_entries;
1031 struct ads_entry *new_entry;
1033 DEBUG("Add alternate data stream \"%s\"", stream_name);
1035 if (inode->num_ads >= 0xfffe) {
1036 ERROR("Too many alternate data streams in one inode!");
1039 num_ads = inode->num_ads + 1;
1040 ads_entries = REALLOC(inode->ads_entries,
1041 num_ads * sizeof(inode->ads_entries[0]));
1043 ERROR("Failed to allocate memory for new alternate data stream");
1046 inode->ads_entries = ads_entries;
1048 new_entry = &inode->ads_entries[num_ads - 1];
1049 if (init_ads_entry(new_entry, stream_name) != 0)
1052 new_entry->stream_id = inode->next_stream_id++;
1054 inode->num_ads = num_ads;
1060 /* Remove an alternate data stream from the inode */
1061 void inode_remove_ads(struct inode *inode, u16 idx,
1062 struct lookup_table *lookup_table)
1064 struct ads_entry *ads_entry;
1065 struct lookup_table_entry *lte;
1067 wimlib_assert(idx < inode->num_ads);
1068 wimlib_assert(inode->resolved);
1070 ads_entry = &inode->ads_entries[idx];
1072 DEBUG("Remove alternate data stream \"%s\"", ads_entry->stream_name_utf8);
1074 lte = ads_entry->lte;
1076 lte_decrement_refcnt(lte, lookup_table);
1078 destroy_ads_entry(ads_entry);
1080 memcpy(&inode->ads_entries[idx],
1081 &inode->ads_entries[idx + 1],
1082 (inode->num_ads - idx - 1) * sizeof(inode->ads_entries[0]));
1090 * Reads the alternate data stream entries for a dentry.
1092 * @p: Pointer to buffer that starts with the first alternate stream entry.
1094 * @inode: Inode to load the alternate data streams into.
1095 * @inode->num_ads must have been set to the number of
1096 * alternate data streams that are expected.
1098 * @remaining_size: Number of bytes of data remaining in the buffer pointed
1101 * The format of the on-disk alternate stream entries is as follows:
1103 * struct ads_entry_on_disk {
1104 * u64 length; // Length of the entry, in bytes. This includes
1105 * all fields (including the stream name and
1106 * null terminator if present, AND the padding!).
1107 * u64 reserved; // Seems to be unused
1108 * u8 hash[20]; // SHA1 message digest of the uncompressed stream
1109 * u16 stream_name_len; // Length of the stream name, in bytes
1110 * char stream_name[]; // Stream name in UTF-16LE, @stream_name_len bytes long,
1111 * not including null terminator
1112 * u16 zero; // UTF-16 null terminator for the stream name, NOT
1113 * included in @stream_name_len. Based on what
1114 * I've observed from filenames in dentries,
1115 * this field should not exist when
1116 * (@stream_name_len == 0), but you can't
1117 * actually tell because of the padding anyway
1118 * (provided that the padding is zeroed, which
1119 * it always seems to be).
1120 * char padding[]; // Padding to make the size a multiple of 8 bytes.
1123 * In addition, the entries are 8-byte aligned.
1125 * Return 0 on success or nonzero on failure. On success, inode->ads_entries
1126 * is set to an array of `struct ads_entry's of length inode->num_ads. On
1127 * failure, @inode is not modified.
1129 static int read_ads_entries(const u8 *p, struct inode *inode,
1133 struct ads_entry *ads_entries;
1136 num_ads = inode->num_ads;
1137 ads_entries = CALLOC(num_ads, sizeof(inode->ads_entries[0]));
1139 ERROR("Could not allocate memory for %"PRIu16" "
1140 "alternate data stream entries", num_ads);
1141 return WIMLIB_ERR_NOMEM;
1144 for (u16 i = 0; i < num_ads; i++) {
1145 struct ads_entry *cur_entry;
1147 u64 length_no_padding;
1150 const u8 *p_save = p;
1152 cur_entry = &ads_entries[i];
1155 ads_entries[i].stream_id = i + 1;
1158 /* Read the base stream entry, excluding the stream name. */
1159 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1160 ERROR("Stream entries go past end of metadata resource");
1161 ERROR("(remaining_size = %"PRIu64")", remaining_size);
1162 ret = WIMLIB_ERR_INVALID_DENTRY;
1163 goto out_free_ads_entries;
1166 p = get_u64(p, &length);
1167 p += 8; /* Skip the reserved field */
1168 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
1169 p = get_u16(p, &cur_entry->stream_name_len);
1171 cur_entry->stream_name = NULL;
1172 cur_entry->stream_name_utf8 = NULL;
1174 /* Length including neither the null terminator nor the padding
1176 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1177 cur_entry->stream_name_len;
1179 /* Length including the null terminator and the padding */
1180 total_length = ((length_no_padding + 2) + 7) & ~7;
1182 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1184 if (remaining_size < length_no_padding) {
1185 ERROR("Stream entries go past end of metadata resource");
1186 ERROR("(remaining_size = %"PRIu64" bytes, "
1187 "length_no_padding = %"PRIu64" bytes)",
1188 remaining_size, length_no_padding);
1189 ret = WIMLIB_ERR_INVALID_DENTRY;
1190 goto out_free_ads_entries;
1193 /* The @length field in the on-disk ADS entry is expected to be
1194 * equal to @total_length, which includes all of the entry and
1195 * the padding that follows it to align the next ADS entry to an
1196 * 8-byte boundary. However, to be safe, we'll accept the
1197 * length field as long as it's not less than the un-padded
1198 * total length and not more than the padded total length. */
1199 if (length < length_no_padding || length > total_length) {
1200 ERROR("Stream entry has unexpected length "
1201 "field (length field = %"PRIu64", "
1202 "unpadded total length = %"PRIu64", "
1203 "padded total length = %"PRIu64")",
1204 length, length_no_padding, total_length);
1205 ret = WIMLIB_ERR_INVALID_DENTRY;
1206 goto out_free_ads_entries;
1209 if (cur_entry->stream_name_len) {
1210 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
1211 if (!cur_entry->stream_name) {
1212 ret = WIMLIB_ERR_NOMEM;
1213 goto out_free_ads_entries;
1215 get_bytes(p, cur_entry->stream_name_len,
1216 (u8*)cur_entry->stream_name);
1217 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
1218 cur_entry->stream_name_len,
1220 cur_entry->stream_name_utf8_len = utf8_len;
1222 if (!cur_entry->stream_name_utf8) {
1223 ret = WIMLIB_ERR_NOMEM;
1224 goto out_free_ads_entries;
1227 /* It's expected that the size of every ADS entry is a multiple
1228 * of 8. However, to be safe, I'm allowing the possibility of
1229 * an ADS entry at the very end of the metadata resource ending
1230 * un-aligned. So although we still need to increment the input
1231 * pointer by @total_length to reach the next ADS entry, it's
1232 * possible that less than @total_length is actually remaining
1233 * in the metadata resource. We should set the remaining size to
1234 * 0 bytes if this happens. */
1235 p = p_save + total_length;
1236 if (remaining_size < total_length)
1239 remaining_size -= total_length;
1241 inode->ads_entries = ads_entries;
1243 inode->next_stream_id = inode->num_ads + 1;
1246 out_free_ads_entries:
1247 for (u16 i = 0; i < num_ads; i++)
1248 destroy_ads_entry(&ads_entries[i]);
1254 * Reads a directory entry, including all alternate data stream entries that
1255 * follow it, from the WIM image's metadata resource.
1257 * @metadata_resource: Buffer containing the uncompressed metadata resource.
1258 * @metadata_resource_len: Length of the metadata resource.
1259 * @offset: Offset of this directory entry in the metadata resource.
1260 * @dentry: A `struct dentry' that will be filled in by this function.
1262 * Return 0 on success or nonzero on failure. On failure, @dentry have been
1263 * modified, bu it will be left with no pointers to any allocated buffers.
1264 * On success, the dentry->length field must be examined. If zero, this was a
1265 * special "end of directory" dentry and not a real dentry. If nonzero, this
1266 * was a real dentry.
1268 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1269 u64 offset, struct dentry *dentry)
1272 u64 calculated_size;
1273 char *file_name = NULL;
1274 char *file_name_utf8 = NULL;
1275 char *short_name = NULL;
1278 size_t file_name_utf8_len = 0;
1280 struct inode *inode = NULL;
1282 dentry_common_init(dentry);
1284 /*Make sure the dentry really fits into the metadata resource.*/
1285 if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1286 ERROR("Directory entry starting at %"PRIu64" ends past the "
1287 "end of the metadata resource (size %"PRIu64")",
1288 offset, metadata_resource_len);
1289 return WIMLIB_ERR_INVALID_DENTRY;
1292 /* Before reading the whole dentry, we need to read just the length.
1293 * This is because a dentry of length 8 (that is, just the length field)
1294 * terminates the list of sibling directory entries. */
1296 p = get_u64(&metadata_resource[offset], &dentry->length);
1298 /* A zero length field (really a length of 8, since that's how big the
1299 * directory entry is...) indicates that this is the end of directory
1300 * dentry. We do not read it into memory as an actual dentry, so just
1301 * return successfully in that case. */
1302 if (dentry->length == 0)
1305 /* If the dentry does not overflow the metadata resource buffer and is
1306 * not too short, read the rest of it (excluding the alternate data
1307 * streams, but including the file name and short name variable-length
1308 * fields) into memory. */
1309 if (offset + dentry->length >= metadata_resource_len
1310 || offset + dentry->length < offset)
1312 ERROR("Directory entry at offset %"PRIu64" and with size "
1313 "%"PRIu64" ends past the end of the metadata resource "
1315 offset, dentry->length, metadata_resource_len);
1316 return WIMLIB_ERR_INVALID_DENTRY;
1319 if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1320 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1322 return WIMLIB_ERR_INVALID_DENTRY;
1325 inode = new_timeless_inode();
1327 return WIMLIB_ERR_NOMEM;
1329 p = get_u32(p, &inode->attributes);
1330 p = get_u32(p, (u32*)&inode->security_id);
1331 p = get_u64(p, &dentry->subdir_offset);
1333 /* 2 unused fields */
1334 p += 2 * sizeof(u64);
1335 /*p = get_u64(p, &dentry->unused1);*/
1336 /*p = get_u64(p, &dentry->unused2);*/
1338 p = get_u64(p, &inode->creation_time);
1339 p = get_u64(p, &inode->last_access_time);
1340 p = get_u64(p, &inode->last_write_time);
1342 p = get_bytes(p, SHA1_HASH_SIZE, inode->hash);
1345 * I don't know what's going on here. It seems like M$ screwed up the
1346 * reparse points, then put the fields in the same place and didn't
1347 * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1348 * have something to do with this, but it's not documented.
1350 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1353 p = get_u32(p, &inode->reparse_tag);
1356 p = get_u32(p, &inode->reparse_tag);
1357 p = get_u64(p, &inode->ino);
1360 /* By the way, the reparse_reserved field does not actually exist (at
1361 * least when the file is not a reparse point) */
1363 p = get_u16(p, &inode->num_ads);
1365 p = get_u16(p, &short_name_len);
1366 p = get_u16(p, &file_name_len);
1368 /* We now know the length of the file name and short name. Make sure
1369 * the length of the dentry is large enough to actually hold them.
1371 * The calculated length here is unaligned to allow for the possibility
1372 * that the dentry->length names an unaligned length, although this
1373 * would be unexpected. */
1374 calculated_size = __dentry_correct_length_unaligned(file_name_len,
1377 if (dentry->length < calculated_size) {
1378 ERROR("Unexpected end of directory entry! (Expected "
1379 "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1380 "short_name_len = %hu, file_name_len = %hu)",
1381 calculated_size, dentry->length,
1382 short_name_len, file_name_len);
1383 return WIMLIB_ERR_INVALID_DENTRY;
1386 /* Read the filename if present. Note: if the filename is empty, there
1387 * is no null terminator following it. */
1388 if (file_name_len) {
1389 file_name = MALLOC(file_name_len);
1391 ERROR("Failed to allocate %hu bytes for dentry file name",
1393 return WIMLIB_ERR_NOMEM;
1395 p = get_bytes(p, file_name_len, file_name);
1397 /* Convert filename to UTF-8. */
1398 file_name_utf8 = utf16_to_utf8(file_name, file_name_len,
1399 &file_name_utf8_len);
1401 if (!file_name_utf8) {
1402 ERROR("Failed to allocate memory to convert UTF-16 "
1403 "filename (%hu bytes) to UTF-8", file_name_len);
1404 ret = WIMLIB_ERR_NOMEM;
1405 goto out_free_file_name;
1408 WARNING("Expected two zero bytes following the file name "
1409 "`%s', but found non-zero bytes", file_name_utf8);
1413 /* Align the calculated size */
1414 calculated_size = (calculated_size + 7) & ~7;
1416 if (dentry->length > calculated_size) {
1417 /* Weird; the dentry says it's longer than it should be. Note
1418 * that the length field does NOT include the size of the
1419 * alternate stream entries. */
1421 /* Strangely, some directory entries inexplicably have a little
1422 * over 70 bytes of extra data. The exact amount of data seems
1423 * to be 72 bytes, but it is aligned on the next 8-byte
1424 * boundary. It does NOT seem to be alternate data stream
1425 * entries. Here's an example of the aligned data:
1427 * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1428 * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1429 * 00000000 00000000 00000000 00000000
1431 * Here's one interpretation of how the data is laid out.
1434 * u32 field1; (always 0x00000001)
1435 * u32 field2; (always 0x40000000)
1436 * u8 data[48]; (???)
1437 * u64 reserved1; (always 0)
1438 * u64 reserved2; (always 0)
1440 DEBUG("Dentry for file or directory `%s' has %zu extra "
1442 file_name_utf8, dentry->length - calculated_size);
1445 /* Read the short filename if present. Note: if there is no short
1446 * filename, there is no null terminator following it. */
1447 if (short_name_len) {
1448 short_name = MALLOC(short_name_len);
1450 ERROR("Failed to allocate %hu bytes for short filename",
1452 ret = WIMLIB_ERR_NOMEM;
1453 goto out_free_file_name_utf8;
1456 p = get_bytes(p, short_name_len, short_name);
1458 WARNING("Expected two zero bytes following the short name of "
1459 "`%s', but found non-zero bytes", file_name_utf8);
1464 * Read the alternate data streams, if present. dentry->num_ads tells
1465 * us how many they are, and they will directly follow the dentry
1468 * Note that each alternate data stream entry begins on an 8-byte
1469 * aligned boundary, and the alternate data stream entries are NOT
1470 * included in the dentry->length field for some reason.
1472 if (inode->num_ads != 0) {
1474 /* Trying different lengths is just a hack to make sure we have
1475 * a chance of reading the ADS entries correctly despite the
1476 * poor documentation. */
1478 if (calculated_size != dentry->length) {
1479 WARNING("Trying calculated dentry length (%"PRIu64") "
1480 "instead of dentry->length field (%"PRIu64") "
1481 "to read ADS entries",
1482 calculated_size, dentry->length);
1484 u64 lengths_to_try[3] = {calculated_size,
1485 dentry->length + 7 & ~7,
1487 ret = WIMLIB_ERR_INVALID_DENTRY;
1488 for (size_t i = 0; i < ARRAY_LEN(lengths_to_try); i++) {
1489 if (lengths_to_try[i] > metadata_resource_len - offset)
1491 ret = read_ads_entries(&metadata_resource[offset + lengths_to_try[i]],
1493 metadata_resource_len - offset - lengths_to_try[i]);
1497 ERROR("Failed to read alternate data stream "
1498 "entries of `%s'", dentry->file_name_utf8);
1499 goto out_free_short_name;
1503 /* We've read all the data for this dentry. Set the names and their
1504 * lengths, and we've done. */
1505 dentry->d_inode = inode;
1506 dentry->file_name = file_name;
1507 dentry->file_name_utf8 = file_name_utf8;
1508 dentry->short_name = short_name;
1509 dentry->file_name_len = file_name_len;
1510 dentry->file_name_utf8_len = file_name_utf8_len;
1511 dentry->short_name_len = short_name_len;
1513 out_free_short_name:
1515 out_free_file_name_utf8:
1516 FREE(file_name_utf8);
1524 /* Reads the children of a dentry, and all their children, ..., etc. from the
1525 * metadata resource and into the dentry tree.
1527 * @metadata_resource: An array that contains the uncompressed metadata
1528 * resource for the WIM file.
1530 * @metadata_resource_len: The length of the uncompressed metadata resource, in
1533 * @dentry: A pointer to a `struct dentry' that is the root of the directory
1534 * tree and has already been read from the metadata resource. It
1535 * does not need to be the real root because this procedure is
1536 * called recursively.
1538 * @return: Zero on success, nonzero on failure.
1540 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1541 struct dentry *dentry)
1543 u64 cur_offset = dentry->subdir_offset;
1544 struct dentry *prev_child = NULL;
1545 struct dentry *first_child = NULL;
1546 struct dentry *child;
1547 struct dentry cur_child;
1551 * If @dentry has no child dentries, nothing more needs to be done for
1552 * this branch. This is the case for regular files, symbolic links, and
1553 * *possibly* empty directories (although an empty directory may also
1554 * have one child dentry that is the special end-of-directory dentry)
1556 if (cur_offset == 0)
1559 /* Find and read all the children of @dentry. */
1562 /* Read next child of @dentry into @cur_child. */
1563 ret = read_dentry(metadata_resource, metadata_resource_len,
1564 cur_offset, &cur_child);
1568 /* Check for end of directory. */
1569 if (cur_child.length == 0)
1572 /* Not end of directory. Allocate this child permanently and
1573 * link it to the parent and previous child. */
1574 child = MALLOC(sizeof(struct dentry));
1576 ERROR("Failed to allocate %zu bytes for new dentry",
1577 sizeof(struct dentry));
1578 ret = WIMLIB_ERR_NOMEM;
1581 memcpy(child, &cur_child, sizeof(struct dentry));
1584 prev_child->next = child;
1585 child->prev = prev_child;
1587 first_child = child;
1590 child->parent = dentry;
1592 inode_add_dentry(child, child->d_inode);
1594 /* If there are children of this child, call this procedure
1596 if (child->subdir_offset != 0) {
1597 ret = read_dentry_tree(metadata_resource,
1598 metadata_resource_len, child);
1603 /* Advance to the offset of the next child. Note: We need to
1604 * advance by the TOTAL length of the dentry, not by the length
1605 * child->length, which although it does take into account the
1606 * padding, it DOES NOT take into account alternate stream
1608 cur_offset += dentry_total_length(child);
1611 /* Link last child to first one, and set parent's children pointer to
1612 * the first child. */
1614 prev_child->next = first_child;
1615 first_child->prev = prev_child;
1617 dentry->d_inode->children = first_child;
1622 * Writes a WIM dentry to an output buffer.
1624 * @dentry: The dentry structure.
1625 * @p: The memory location to write the data to.
1626 * @return: Pointer to the byte after the last byte we wrote as part of the
1629 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
1633 const struct inode *inode = dentry->d_inode;
1635 /* We calculate the correct length of the dentry ourselves because the
1636 * dentry->length field may been set to an unexpected value from when we
1637 * read the dentry in (for example, there may have been unknown data
1638 * appended to the end of the dentry...) */
1639 u64 length = dentry_correct_length(dentry);
1641 p = put_u64(p, length);
1642 p = put_u32(p, inode->attributes);
1643 p = put_u32(p, inode->security_id);
1644 p = put_u64(p, dentry->subdir_offset);
1645 p = put_u64(p, 0); /* unused1 */
1646 p = put_u64(p, 0); /* unused2 */
1647 p = put_u64(p, inode->creation_time);
1648 p = put_u64(p, inode->last_access_time);
1649 p = put_u64(p, inode->last_write_time);
1650 hash = inode_stream_hash(inode, 0);
1651 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1652 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1653 p = put_zeroes(p, 4);
1654 p = put_u32(p, inode->reparse_tag);
1655 p = put_zeroes(p, 4);
1659 if (inode->link_count == 1)
1662 link_group_id = inode->ino;
1663 p = put_u64(p, link_group_id);
1665 p = put_u16(p, inode->num_ads);
1666 p = put_u16(p, dentry->short_name_len);
1667 p = put_u16(p, dentry->file_name_len);
1668 if (dentry->file_name_len) {
1669 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1670 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1672 if (dentry->short_name) {
1673 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1674 p = put_u16(p, 0); /* short name padding, 2 bytes */
1677 /* Align to 8-byte boundary */
1678 wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1679 p = put_zeroes(p, length - (p - orig_p));
1681 /* Write the alternate data streams, if there are any. Please see
1682 * read_ads_entries() for comments about the format of the on-disk
1683 * alternate data stream entries. */
1684 for (u16 i = 0; i < inode->num_ads; i++) {
1685 p = put_u64(p, ads_entry_total_length(&inode->ads_entries[i]));
1686 p = put_u64(p, 0); /* Unused */
1687 hash = inode_stream_hash(inode, i + 1);
1688 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1689 p = put_u16(p, inode->ads_entries[i].stream_name_len);
1690 if (inode->ads_entries[i].stream_name_len) {
1691 p = put_bytes(p, inode->ads_entries[i].stream_name_len,
1692 (u8*)inode->ads_entries[i].stream_name);
1695 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1697 wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1701 /* Recursive function that writes a dentry tree rooted at @parent, not including
1702 * @parent itself, which has already been written. */
1703 static u8 *write_dentry_tree_recursive(const struct dentry *parent, u8 *p)
1705 const struct dentry *child, *children;
1707 /* Nothing to do if this dentry has no children. */
1708 if (parent->subdir_offset == 0)
1711 /* Write child dentries and end-of-directory entry.
1713 * Note: we need to write all of this dentry's children before
1714 * recursively writing the directory trees rooted at each of the child
1715 * dentries, since the on-disk dentries for a dentry's children are
1716 * always located at consecutive positions in the metadata resource! */
1717 children = parent->d_inode->children;
1721 p = write_dentry(child, p);
1722 child = child->next;
1723 } while (child != children);
1726 /* write end of directory entry */
1729 /* Recurse on children. */
1732 p = write_dentry_tree_recursive(child, p);
1733 child = child->next;
1734 } while (child != children);
1739 /* Writes a directory tree to the metadata resource.
1741 * @root: Root of the dentry tree.
1742 * @p: Pointer to a buffer with enough space for the dentry tree.
1744 * Returns pointer to the byte after the last byte we wrote.
1746 u8 *write_dentry_tree(const struct dentry *root, u8 *p)
1748 DEBUG("Writing dentry tree.");
1749 wimlib_assert(dentry_is_root(root));
1751 /* If we're the root dentry, we have no parent that already
1752 * wrote us, so we need to write ourselves. */
1753 p = write_dentry(root, p);
1755 /* Write end of directory entry after the root dentry just to be safe;
1756 * however the root dentry obviously cannot have any siblings. */
1759 /* Recursively write the rest of the dentry tree. */
1760 return write_dentry_tree_recursive(root, p);