]> wimlib.net Git - wimlib/blob - src/dentry.c
d7a684dcd7f4d98fb48beec2047dc78556352ff2
[wimlib] / src / dentry.c
1 /*
2  * dentry.c
3  *
4  * In the WIM file format, the dentries are stored in the "metadata resource"
5  * section right after the security data.  Each image in the WIM file has its
6  * own metadata resource with its own security data and dentry tree.  Dentries
7  * in different images may share file resources by referring to the same lookup
8  * table entries.
9  */
10
11 /*
12  * Copyright (C) 2012 Eric Biggers
13  *
14  * This file is part of wimlib, a library for working with WIM files.
15  *
16  * wimlib is free software; you can redistribute it and/or modify it under the
17  * terms of the GNU General Public License as published by the Free Software
18  * Foundation; either version 3 of the License, or (at your option) any later
19  * version.
20  *
21  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
22  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
23  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License along with
26  * wimlib; if not, see http://www.gnu.org/licenses/.
27  */
28
29 #include "buffer_io.h"
30 #include "dentry.h"
31 #include "lookup_table.h"
32 #include "timestamp.h"
33 #include "wimlib_internal.h"
34 #include <errno.h>
35
36 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has
37  * a file name and short name that take the specified numbers of bytes.  This
38  * excludes any alternate data stream entries that may follow the dentry. */
39 static u64 __dentry_correct_length_unaligned(u16 file_name_len,
40                                              u16 short_name_len)
41 {
42         u64 length = WIM_DENTRY_DISK_SIZE;
43         if (file_name_len)
44                 length += file_name_len + 2;
45         if (short_name_len)
46                 length += short_name_len + 2;
47         return length;
48 }
49
50 /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry, based on
51  * the file name length and short name length.  Note that dentry->length is
52  * ignored; also, this excludes any alternate data stream entries that may
53  * follow the dentry. */
54 static u64 dentry_correct_length_unaligned(const struct wim_dentry *dentry)
55 {
56         return __dentry_correct_length_unaligned(dentry->file_name_len,
57                                                  dentry->short_name_len);
58 }
59
60 /* Return the "correct" value to write in the length field of a WIM dentry,
61  * based on the file name length and short name length. */
62 static u64 dentry_correct_length(const struct wim_dentry *dentry)
63 {
64         return (dentry_correct_length_unaligned(dentry) + 7) & ~7;
65 }
66
67 /* Return %true iff the alternate data stream entry @entry has the UTF-8 stream
68  * name @name that has length @name_len bytes. */
69 static inline bool ads_entry_has_name(const struct wim_ads_entry *entry,
70                                       const char *name, size_t name_len)
71 {
72         if (entry->stream_name_utf8_len != name_len)
73                 return false;
74         return memcmp(entry->stream_name_utf8, name, name_len) == 0;
75 }
76
77 /* Duplicates a UTF-8 string into UTF-8 and UTF-16 strings and returns the
78  * strings and their lengths in the pointer arguments.  (Frees existing strings
79  * first.) */
80 static int get_names(char **name_utf16_ret, char **name_utf8_ret,
81                      u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
82                      const char *name)
83 {
84         size_t utf8_len;
85         size_t utf16_len;
86         char *name_utf16, *name_utf8;
87         int ret;
88
89         utf8_len = strlen(name);
90         ret = utf8_to_utf16(name, utf8_len, &name_utf16, &utf16_len);
91         if (ret != 0)
92                 return ret;
93
94         name_utf8 = MALLOC(utf8_len + 1);
95         if (!name_utf8) {
96                 FREE(name_utf16);
97                 return WIMLIB_ERR_NOMEM;
98         }
99         memcpy(name_utf8, name, utf8_len + 1);
100         FREE(*name_utf8_ret);
101         FREE(*name_utf16_ret);
102         *name_utf8_ret      = name_utf8;
103         *name_utf16_ret     = name_utf16;
104         *name_utf8_len_ret  = utf8_len;
105         *name_utf16_len_ret = utf16_len;
106         return 0;
107 }
108
109 /* Sets the name of a WIM dentry. */
110 int set_dentry_name(struct wim_dentry *dentry, const char *new_name)
111 {
112         int ret;
113
114         ret = get_names(&dentry->file_name, &dentry->file_name_utf8,
115                         &dentry->file_name_len, &dentry->file_name_utf8_len,
116                         new_name);
117         if (ret == 0) {
118                 if (dentry->short_name_len) {
119                         FREE(dentry->short_name);
120                         dentry->short_name_len = 0;
121                 }
122                 dentry->length = dentry_correct_length(dentry);
123         }
124         return ret;
125 }
126
127 /*
128  * Changes the name of an alternate data stream */
129 static int change_ads_name(struct wim_ads_entry *entry, const char *new_name)
130 {
131         return get_names(&entry->stream_name, &entry->stream_name_utf8,
132                          &entry->stream_name_len,
133                          &entry->stream_name_utf8_len,
134                          new_name);
135 }
136
137 /* Returns the total length of a WIM alternate data stream entry on-disk,
138  * including the stream name, the null terminator, AND the padding after the
139  * entry to align the next ADS entry or dentry on an 8-byte boundary. */
140 static u64 ads_entry_total_length(const struct wim_ads_entry *entry)
141 {
142         u64 len = WIM_ADS_ENTRY_DISK_SIZE;
143         if (entry->stream_name_len)
144                 len += entry->stream_name_len + 2;
145         return (len + 7) & ~7;
146 }
147
148
149 static u64 __dentry_total_length(const struct wim_dentry *dentry, u64 length)
150 {
151         const struct wim_inode *inode = dentry->d_inode;
152         for (u16 i = 0; i < inode->i_num_ads; i++)
153                 length += ads_entry_total_length(&inode->i_ads_entries[i]);
154         return (length + 7) & ~7;
155 }
156
157 /* Calculate the aligned *total* length of an on-disk WIM dentry.  This includes
158  * all alternate data streams. */
159 u64 dentry_correct_total_length(const struct wim_dentry *dentry)
160 {
161         return __dentry_total_length(dentry,
162                                      dentry_correct_length_unaligned(dentry));
163 }
164
165 /* Like dentry_correct_total_length(), but use the existing dentry->length field
166  * instead of calculating its "correct" value. */
167 static u64 dentry_total_length(const struct wim_dentry *dentry)
168 {
169         return __dentry_total_length(dentry, dentry->length);
170 }
171
172 int for_dentry_in_rbtree(struct rb_node *root,
173                          int (*visitor)(struct wim_dentry *, void *),
174                          void *arg)
175 {
176         int ret;
177         struct rb_node *node = root;
178         LIST_HEAD(stack);
179         while (1) {
180                 if (node) {
181                         list_add(&rbnode_dentry(node)->tmp_list, &stack);
182                         node = node->rb_left;
183                 } else {
184                         struct list_head *next;
185                         struct wim_dentry *dentry;
186
187                         next = stack.next;
188                         if (next == &stack)
189                                 return 0;
190                         dentry = container_of(next, struct wim_dentry, tmp_list);
191                         list_del(next);
192                         ret = visitor(dentry, arg);
193                         if (ret != 0)
194                                 return ret;
195                         node = dentry->rb_node.rb_right;
196                 }
197         }
198 }
199
200 static int for_dentry_tree_in_rbtree_depth(struct rb_node *node,
201                                            int (*visitor)(struct wim_dentry*, void*),
202                                            void *arg)
203 {
204         int ret;
205         if (node) {
206                 ret = for_dentry_tree_in_rbtree_depth(node->rb_left,
207                                                       visitor, arg);
208                 if (ret != 0)
209                         return ret;
210                 ret = for_dentry_tree_in_rbtree_depth(node->rb_right,
211                                                       visitor, arg);
212                 if (ret != 0)
213                         return ret;
214                 ret = for_dentry_in_tree_depth(rbnode_dentry(node), visitor, arg);
215                 if (ret != 0)
216                         return ret;
217         }
218         return 0;
219 }
220
221 /*#define RECURSIVE_FOR_DENTRY_IN_TREE*/
222
223 #ifdef RECURSIVE_FOR_DENTRY_IN_TREE
224 static int for_dentry_tree_in_rbtree(struct rb_node *node,
225                                      int (*visitor)(struct wim_dentry*, void*),
226                                      void *arg)
227 {
228         int ret;
229         if (node) {
230                 ret = for_dentry_tree_in_rbtree(node->rb_left, visitor, arg);
231                 if (ret != 0)
232                         return ret;
233                 ret = for_dentry_in_tree(rbnode_dentry(node), visitor, arg);
234                 if (ret != 0)
235                         return ret;
236                 ret = for_dentry_tree_in_rbtree(node->rb_right, visitor, arg);
237                 if (ret != 0)
238                         return ret;
239         }
240         return 0;
241 }
242 #endif
243
244 /*
245  * Calls a function on all directory entries in a WIM dentry tree.  Logically,
246  * this is a pre-order traversal (the function is called on a parent dentry
247  * before its children), but sibling dentries will be visited in order as well.
248  *
249  * In reality, the data structures are more complicated than the above might
250  * suggest because there is a separate red-black tree for each dentry that
251  * contains its direct children.
252  */
253 int for_dentry_in_tree(struct wim_dentry *root,
254                        int (*visitor)(struct wim_dentry*, void*), void *arg)
255 {
256 #ifdef RECURSIVE_FOR_DENTRY_IN_TREE
257         int ret = visitor(root, arg);
258         if (ret != 0)
259                 return ret;
260         return for_dentry_tree_in_rbtree(root->d_inode->i_children.rb_node, visitor, arg);
261 #else
262         int ret;
263         struct list_head main_stack;
264         struct list_head sibling_stack;
265         struct list_head *sibling_stack_bottom;
266         struct wim_dentry *main_dentry;
267         struct rb_node *node;
268         struct list_head *next_sibling;
269         struct wim_dentry *dentry;
270
271         ret = visitor(root, arg);
272         if (ret != 0)
273                 return ret;
274
275         main_dentry = root;
276         sibling_stack_bottom = &sibling_stack;
277         INIT_LIST_HEAD(&main_stack);
278         INIT_LIST_HEAD(&sibling_stack);
279
280         list_add(&root->tmp_list, &main_stack);
281         node = root->d_inode->i_children.rb_node;
282
283         while (1) {
284                 // Prepare for non-recursive in-order traversal of the red-black
285                 // tree of this dentry's children
286
287                 while (node) {
288                         // Push this node to the sibling stack and examine the
289                         // left neighbor, if any
290                         list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
291                         node = node->rb_left;
292                 }
293
294                 next_sibling = sibling_stack.next;
295                 if (next_sibling == sibling_stack_bottom) {
296                         // Done with all siblings.  Pop the main dentry to move
297                         // back up one level.
298                         main_dentry = container_of(main_stack.next,
299                                                    struct wim_dentry,
300                                                    tmp_list);
301                         list_del(&main_dentry->tmp_list);
302
303                         if (main_dentry == root)
304                                 goto out;
305
306                         // Restore sibling stack bottom from the previous level
307                         sibling_stack_bottom = (void*)main_dentry->parent;
308
309                         // Restore the just-popped main dentry's parent
310                         main_dentry->parent = container_of(main_stack.next,
311                                                            struct wim_dentry,
312                                                            tmp_list);
313
314                         // The next sibling to traverse in the previous level,
315                         // in the in-order traversal of the red-black tree, is
316                         // the one to the right.
317                         node = main_dentry->rb_node.rb_right;
318                 } else {
319                         // The sibling stack is not empty, so there are more to
320                         // go!
321
322                         // Pop a sibling from the stack.
323                         list_del(next_sibling);
324                         dentry = container_of(next_sibling, struct wim_dentry, tmp_list);
325
326                         // Visit the sibling.
327                         ret = visitor(dentry, arg);
328                         if (ret != 0) {
329                                 // Failed.  Restore parent pointers for the
330                                 // dentries in the main stack
331                                 list_for_each_entry(dentry, &main_stack, tmp_list) {
332                                         dentry->parent = container_of(dentry->tmp_list.next,
333                                                                       struct wim_dentry,
334                                                                       tmp_list);
335                                 }
336                                 goto out;
337                         }
338
339                         // We'd like to recursively visit the dentry tree rooted
340                         // at this sibling.  To do this, add it to the main
341                         // stack, save the bottom of this level's sibling stack
342                         // in the dentry->parent field, re-set the bottom of the
343                         // sibling stack to be its current height, and set
344                         // main_dentry to the sibling so it becomes the parent
345                         // dentry in the next iteration through the outer loop.
346                         if (inode_has_children(dentry->d_inode)) {
347                                 list_add(&dentry->tmp_list, &main_stack);
348                                 dentry->parent = (void*)sibling_stack_bottom;
349                                 sibling_stack_bottom = sibling_stack.next;
350
351                                 main_dentry = dentry;
352                                 node = main_dentry->d_inode->i_children.rb_node;
353                         } else {
354                                 node = dentry->rb_node.rb_right;
355                         }
356                 }
357         }
358 out:
359         root->parent = root;
360         return ret;
361 #endif
362 }
363
364 /*
365  * Like for_dentry_in_tree(), but the visitor function is always called on a
366  * dentry's children before on itself.
367  */
368 int for_dentry_in_tree_depth(struct wim_dentry *root,
369                              int (*visitor)(struct wim_dentry*, void*), void *arg)
370 {
371 #if 1
372         int ret;
373         ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
374                                               visitor, arg);
375         if (ret != 0)
376                 return ret;
377         return visitor(root, arg);
378
379 #else
380         int ret;
381         struct list_head main_stack;
382         struct list_head sibling_stack;
383         struct list_head *sibling_stack_bottom;
384         struct wim_dentry *main_dentry;
385         struct rb_node *node;
386         struct list_head *next_sibling;
387         struct wim_dentry *dentry;
388
389         main_dentry = root;
390         sibling_stack_bottom = &sibling_stack;
391         INIT_LIST_HEAD(&main_stack);
392         INIT_LIST_HEAD(&sibling_stack);
393
394         list_add(&main_dentry->tmp_list, &main_stack);
395
396         while (1) {
397                 node = main_dentry->d_inode->i_children.rb_node;
398
399                 while (1) {
400                         if (node->rb_left) {
401                                 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
402                                 node = node->rb_left;
403                                 continue;
404                         }
405                         if (node->rb_right) {
406                                 list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
407                                 node = node->rb_right;
408                                 continue;
409                         }
410                         list_add(&rbnode_dentry(node)->tmp_list, &sibling_stack);
411                 }
412
413         pop_sibling:
414                 next_sibling = sibling_stack.next;
415                 if (next_sibling == sibling_stack_bottom) {
416                         main_dentry = container_of(main_stack.next,
417                                                    struct wim_dentry,
418                                                    tmp_list);
419                         list_del(&main_dentry->tmp_list);
420
421
422                         sibling_stack_bottom = (void*)main_dentry->parent;
423
424                         if (main_dentry == root) {
425                                 main_dentry->parent = main_dentry;
426                                 ret = visitor(dentry, arg);
427                                 return ret;
428                         } else {
429                                 main_dentry->parent = container_of(main_stack.next,
430                                                                    struct wim_dentry,
431                                                                    tmp_list);
432                         }
433
434                         ret = visitor(main_dentry, arg);
435
436                         if (ret != 0) {
437                                 list_del(&root->tmp_list);
438                                 list_for_each_entry(dentry, &main_stack, tmp_list) {
439                                         dentry->parent = container_of(dentry->tmp_list.next,
440                                                                       struct wim_dentry,
441                                                                       tmp_list);
442                                 }
443                                 root->parent = root;
444                                 return ret;
445                         }
446                         goto pop_sibling;
447                 } else {
448
449                         list_del(next_sibling);
450                         dentry = container_of(next_sibling, struct wim_dentry, tmp_list);
451
452
453                         list_add(&dentry->tmp_list, &main_stack);
454                         dentry->parent = (void*)sibling_stack_bottom;
455                         sibling_stack_bottom = sibling_stack.next;
456
457                         main_dentry = dentry;
458                 }
459         }
460 #endif
461 }
462
463 /*
464  * Calculate the full path of @dentry, based on its parent's full path and on
465  * its UTF-8 file name.
466  */
467 int calculate_dentry_full_path(struct wim_dentry *dentry, void *ignore)
468 {
469         char *full_path;
470         u32 full_path_len;
471         if (dentry_is_root(dentry)) {
472                 full_path = MALLOC(2);
473                 if (!full_path)
474                         goto oom;
475                 full_path[0] = '/';
476                 full_path[1] = '\0';
477                 full_path_len = 1;
478         } else {
479                 char *parent_full_path;
480                 u32 parent_full_path_len;
481                 const struct wim_dentry *parent = dentry->parent;
482
483                 if (dentry_is_root(parent)) {
484                         parent_full_path = "";
485                         parent_full_path_len = 0;
486                 } else {
487                         parent_full_path = parent->full_path_utf8;
488                         parent_full_path_len = parent->full_path_utf8_len;
489                 }
490
491                 full_path_len = parent_full_path_len + 1 +
492                                 dentry->file_name_utf8_len;
493                 full_path = MALLOC(full_path_len + 1);
494                 if (!full_path)
495                         goto oom;
496
497                 memcpy(full_path, parent_full_path, parent_full_path_len);
498                 full_path[parent_full_path_len] = '/';
499                 memcpy(full_path + parent_full_path_len + 1,
500                        dentry->file_name_utf8,
501                        dentry->file_name_utf8_len);
502                 full_path[full_path_len] = '\0';
503         }
504         FREE(dentry->full_path_utf8);
505         dentry->full_path_utf8 = full_path;
506         dentry->full_path_utf8_len = full_path_len;
507         return 0;
508 oom:
509         ERROR("Out of memory while calculating dentry full path");
510         return WIMLIB_ERR_NOMEM;
511 }
512
513 static int increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
514 {
515         *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
516         return 0;
517 }
518
519 static int call_calculate_subdir_offsets(struct wim_dentry *dentry,
520                                          void *subdir_offset_p)
521 {
522         calculate_subdir_offsets(dentry, subdir_offset_p);
523         return 0;
524 }
525
526 /*
527  * Recursively calculates the subdir offsets for a directory tree.
528  *
529  * @dentry:  The root of the directory tree.
530  * @subdir_offset_p:  The current subdirectory offset; i.e., the subdirectory
531  *                    offset for @dentry.
532  */
533 void calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p)
534 {
535         struct rb_node *node;
536
537         dentry->subdir_offset = *subdir_offset_p;
538         node = dentry->d_inode->i_children.rb_node;
539         if (node) {
540                 /* Advance the subdir offset by the amount of space the children
541                  * of this dentry take up. */
542                 for_dentry_in_rbtree(node, increment_subdir_offset, subdir_offset_p);
543
544                 /* End-of-directory dentry on disk. */
545                 *subdir_offset_p += 8;
546
547                 /* Recursively call calculate_subdir_offsets() on all the
548                  * children. */
549                 for_dentry_in_rbtree(node, call_calculate_subdir_offsets, subdir_offset_p);
550         } else {
551                 /* On disk, childless directories have a valid subdir_offset
552                  * that points to an 8-byte end-of-directory dentry.  Regular
553                  * files or reparse points have a subdir_offset of 0. */
554                 if (dentry_is_directory(dentry))
555                         *subdir_offset_p += 8;
556                 else
557                         dentry->subdir_offset = 0;
558         }
559 }
560
561 static int compare_names(const char *name_1, u16 len_1,
562                          const char *name_2, u16 len_2)
563 {
564         int result = strncasecmp(name_1, name_2, min(len_1, len_2));
565         if (result) {
566                 return result;
567         } else {
568                 return (int)len_1 - (int)len_2;
569         }
570 }
571
572 static int dentry_compare_names(const struct wim_dentry *d1, const struct wim_dentry *d2)
573 {
574         return compare_names(d1->file_name_utf8, d1->file_name_utf8_len,
575                              d2->file_name_utf8, d2->file_name_utf8_len);
576 }
577
578
579 static struct wim_dentry *
580 get_rbtree_child_with_name(const struct rb_node *node,
581                            const char *name, size_t name_len)
582 {
583         do {
584                 struct wim_dentry *child = rbnode_dentry(node);
585                 int result = compare_names(name, name_len,
586                                            child->file_name_utf8,
587                                            child->file_name_utf8_len);
588                 if (result < 0)
589                         node = node->rb_left;
590                 else if (result > 0)
591                         node = node->rb_right;
592                 else
593                         return child;
594         } while (node);
595         return NULL;
596 }
597
598 /* Returns the child of @dentry that has the file name @name.
599  * Returns NULL if no child has the name. */
600 struct wim_dentry *get_dentry_child_with_name(const struct wim_dentry *dentry,
601                                               const char *name)
602 {
603         struct rb_node *node = dentry->d_inode->i_children.rb_node;
604         if (node)
605                 return get_rbtree_child_with_name(node, name, strlen(name));
606         else
607                 return NULL;
608 }
609
610 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
611  * @cur_dentry.  Returns NULL if no dentry having the path is found. */
612 static struct wim_dentry *get_dentry_relative_path(struct wim_dentry *cur_dentry,
613                                                    const char *path)
614 {
615         if (*path == '\0')
616                 return cur_dentry;
617
618         struct rb_node *node = cur_dentry->d_inode->i_children.rb_node;
619         if (node) {
620                 struct wim_dentry *child;
621                 size_t base_len;
622                 const char *new_path;
623
624                 new_path = path_next_part(path, &base_len);
625
626                 child = get_rbtree_child_with_name(node, path, base_len);
627                 if (child)
628                         return get_dentry_relative_path(child, new_path);
629         }
630         /* errno is set to ENOTDIR if the lookup failed due to reaching a
631          * non-directory, or ENOENT if the lookup failed otherwise.  This maybe
632          * should be factored out somehow. */
633         if (dentry_is_directory(cur_dentry))
634                 errno = ENOENT;
635         else
636                 errno = ENOTDIR;
637         return NULL;
638 }
639
640 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
641  * such dentry. */
642 struct wim_dentry *get_dentry(WIMStruct *w, const char *path)
643 {
644         struct wim_dentry *root = wim_root_dentry(w);
645         while (*path == '/')
646                 path++;
647         return get_dentry_relative_path(root, path);
648 }
649
650 struct wim_inode *wim_pathname_to_inode(WIMStruct *w, const char *path)
651 {
652         struct wim_dentry *dentry;
653         dentry = get_dentry(w, path);
654         if (dentry)
655                 return dentry->d_inode;
656         else
657                 return NULL;
658 }
659
660 /* Returns the dentry that corresponds to the parent directory of @path, or NULL
661  * if the dentry is not found. */
662 struct wim_dentry *get_parent_dentry(WIMStruct *w, const char *path)
663 {
664         size_t path_len = strlen(path);
665         char buf[path_len + 1];
666
667         memcpy(buf, path, path_len + 1);
668
669         to_parent_name(buf, path_len);
670
671         return get_dentry(w, buf);
672 }
673
674 /* Prints the full path of a dentry. */
675 int print_dentry_full_path(struct wim_dentry *dentry, void *ignore)
676 {
677         if (dentry->full_path_utf8)
678                 puts(dentry->full_path_utf8);
679         return 0;
680 }
681
682 /* We want to be able to show the names of the file attribute flags that are
683  * set. */
684 struct file_attr_flag {
685         u32 flag;
686         const char *name;
687 };
688 struct file_attr_flag file_attr_flags[] = {
689         {FILE_ATTRIBUTE_READONLY,           "READONLY"},
690         {FILE_ATTRIBUTE_HIDDEN,             "HIDDEN"},
691         {FILE_ATTRIBUTE_SYSTEM,             "SYSTEM"},
692         {FILE_ATTRIBUTE_DIRECTORY,          "DIRECTORY"},
693         {FILE_ATTRIBUTE_ARCHIVE,            "ARCHIVE"},
694         {FILE_ATTRIBUTE_DEVICE,             "DEVICE"},
695         {FILE_ATTRIBUTE_NORMAL,             "NORMAL"},
696         {FILE_ATTRIBUTE_TEMPORARY,          "TEMPORARY"},
697         {FILE_ATTRIBUTE_SPARSE_FILE,        "SPARSE_FILE"},
698         {FILE_ATTRIBUTE_REPARSE_POINT,      "REPARSE_POINT"},
699         {FILE_ATTRIBUTE_COMPRESSED,         "COMPRESSED"},
700         {FILE_ATTRIBUTE_OFFLINE,            "OFFLINE"},
701         {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
702         {FILE_ATTRIBUTE_ENCRYPTED,          "ENCRYPTED"},
703         {FILE_ATTRIBUTE_VIRTUAL,            "VIRTUAL"},
704 };
705
706 /* Prints a directory entry.  @lookup_table is a pointer to the lookup table, if
707  * available.  If the dentry is unresolved and the lookup table is NULL, the
708  * lookup table entries will not be printed.  Otherwise, they will be. */
709 int print_dentry(struct wim_dentry *dentry, void *lookup_table)
710 {
711         const u8 *hash;
712         struct wim_lookup_table_entry *lte;
713         const struct wim_inode *inode = dentry->d_inode;
714         char buf[50];
715
716         printf("[DENTRY]\n");
717         printf("Length            = %"PRIu64"\n", dentry->length);
718         printf("Attributes        = 0x%x\n", inode->i_attributes);
719         for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
720                 if (file_attr_flags[i].flag & inode->i_attributes)
721                         printf("    FILE_ATTRIBUTE_%s is set\n",
722                                 file_attr_flags[i].name);
723         printf("Security ID       = %d\n", inode->i_security_id);
724         printf("Subdir offset     = %"PRIu64"\n", dentry->subdir_offset);
725
726         wim_timestamp_to_str(inode->i_creation_time, buf, sizeof(buf));
727         printf("Creation Time     = %s\n", buf);
728
729         wim_timestamp_to_str(inode->i_last_access_time, buf, sizeof(buf));
730         printf("Last Access Time  = %s\n", buf);
731
732         wim_timestamp_to_str(inode->i_last_write_time, buf, sizeof(buf));
733         printf("Last Write Time   = %s\n", buf);
734
735         printf("Reparse Tag       = 0x%"PRIx32"\n", inode->i_reparse_tag);
736         printf("Hard Link Group   = 0x%"PRIx64"\n", inode->i_ino);
737         printf("Hard Link Group Size = %"PRIu32"\n", inode->i_nlink);
738         printf("Number of Alternate Data Streams = %hu\n", inode->i_num_ads);
739         printf("Filename (UTF-8)  = \"%s\"\n", dentry->file_name_utf8);
740         /*printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);*/
741         printf("Short Name (UTF-16LE) = \"");
742         print_string(dentry->short_name, dentry->short_name_len);
743         puts("\"");
744         /*printf("Short Name Length = %hu\n", dentry->short_name_len);*/
745         printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
746         lte = inode_stream_lte(dentry->d_inode, 0, lookup_table);
747         if (lte) {
748                 print_lookup_table_entry(lte, stdout);
749         } else {
750                 hash = inode_stream_hash(inode, 0);
751                 if (hash) {
752                         printf("Hash              = 0x");
753                         print_hash(hash);
754                         putchar('\n');
755                         putchar('\n');
756                 }
757         }
758         for (u16 i = 0; i < inode->i_num_ads; i++) {
759                 printf("[Alternate Stream Entry %u]\n", i);
760                 printf("Name = \"%s\"\n", inode->i_ads_entries[i].stream_name_utf8);
761                 printf("Name Length (UTF-16) = %u\n",
762                         inode->i_ads_entries[i].stream_name_len);
763                 hash = inode_stream_hash(inode, i + 1);
764                 if (hash) {
765                         printf("Hash              = 0x");
766                         print_hash(hash);
767                         putchar('\n');
768                 }
769                 print_lookup_table_entry(inode_stream_lte(inode, i + 1, lookup_table),
770                                          stdout);
771         }
772         return 0;
773 }
774
775 /* Initializations done on every `struct wim_dentry'. */
776 static void dentry_common_init(struct wim_dentry *dentry)
777 {
778         memset(dentry, 0, sizeof(struct wim_dentry));
779         dentry->refcnt = 1;
780 }
781
782 static struct wim_inode *new_timeless_inode()
783 {
784         struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
785         if (inode) {
786                 inode->i_security_id = -1;
787                 inode->i_nlink = 1;
788         #ifdef WITH_FUSE
789                 inode->i_next_stream_id = 1;
790                 if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
791                         ERROR_WITH_ERRNO("Error initializing mutex");
792                         FREE(inode);
793                         return NULL;
794                 }
795         #endif
796                 INIT_LIST_HEAD(&inode->i_dentry);
797         }
798         return inode;
799 }
800
801 static struct wim_inode *new_inode()
802 {
803         struct wim_inode *inode = new_timeless_inode();
804         if (inode) {
805                 u64 now = get_wim_timestamp();
806                 inode->i_creation_time = now;
807                 inode->i_last_access_time = now;
808                 inode->i_last_write_time = now;
809         }
810         return inode;
811 }
812
813 /*
814  * Creates an unlinked directory entry.
815  *
816  * @name:  The UTF-8 filename of the new dentry.
817  *
818  * Returns a pointer to the new dentry, or NULL if out of memory.
819  */
820 struct wim_dentry *new_dentry(const char *name)
821 {
822         struct wim_dentry *dentry;
823
824         dentry = MALLOC(sizeof(struct wim_dentry));
825         if (!dentry)
826                 goto err;
827
828         dentry_common_init(dentry);
829         if (set_dentry_name(dentry, name) != 0)
830                 goto err;
831
832         dentry->parent = dentry;
833
834         return dentry;
835 err:
836         FREE(dentry);
837         ERROR_WITH_ERRNO("Failed to create new dentry with name \"%s\"", name);
838         return NULL;
839 }
840
841
842 static struct wim_dentry *
843 __new_dentry_with_inode(const char *name, bool timeless)
844 {
845         struct wim_dentry *dentry;
846         dentry = new_dentry(name);
847         if (dentry) {
848                 if (timeless)
849                         dentry->d_inode = new_timeless_inode();
850                 else
851                         dentry->d_inode = new_inode();
852                 if (dentry->d_inode) {
853                         inode_add_dentry(dentry, dentry->d_inode);
854                 } else {
855                         free_dentry(dentry);
856                         dentry = NULL;
857                 }
858         }
859         return dentry;
860 }
861
862 struct wim_dentry *new_dentry_with_timeless_inode(const char *name)
863 {
864         return __new_dentry_with_inode(name, true);
865 }
866
867 struct wim_dentry *new_dentry_with_inode(const char *name)
868 {
869         return __new_dentry_with_inode(name, false);
870 }
871
872
873 static int init_ads_entry(struct wim_ads_entry *ads_entry, const char *name)
874 {
875         int ret = 0;
876         memset(ads_entry, 0, sizeof(*ads_entry));
877         if (name && *name)
878                 ret = change_ads_name(ads_entry, name);
879         return ret;
880 }
881
882 static void destroy_ads_entry(struct wim_ads_entry *ads_entry)
883 {
884         FREE(ads_entry->stream_name);
885         FREE(ads_entry->stream_name_utf8);
886 }
887
888
889 /* Frees an inode. */
890 void free_inode(struct wim_inode *inode)
891 {
892         if (inode) {
893                 if (inode->i_ads_entries) {
894                         for (u16 i = 0; i < inode->i_num_ads; i++)
895                                 destroy_ads_entry(&inode->i_ads_entries[i]);
896                         FREE(inode->i_ads_entries);
897                 }
898         #ifdef WITH_FUSE
899                 wimlib_assert(inode->i_num_opened_fds == 0);
900                 FREE(inode->i_fds);
901                 pthread_mutex_destroy(&inode->i_mutex);
902                 if (inode->i_hlist.pprev)
903                         hlist_del(&inode->i_hlist);
904         #endif
905                 FREE(inode->i_extracted_file);
906                 FREE(inode);
907         }
908 }
909
910 /* Decrements link count on an inode and frees it if the link count reaches 0.
911  * */
912 static void put_inode(struct wim_inode *inode)
913 {
914         wimlib_assert(inode->i_nlink != 0);
915         if (--inode->i_nlink == 0) {
916         #ifdef WITH_FUSE
917                 if (inode->i_num_opened_fds == 0)
918         #endif
919                 {
920                         free_inode(inode);
921                 }
922         }
923 }
924
925 /* Frees a WIM dentry.
926  *
927  * The corresponding inode (if any) is freed only if its link count is
928  * decremented to 0.
929  */
930 void free_dentry(struct wim_dentry *dentry)
931 {
932         FREE(dentry->file_name);
933         FREE(dentry->file_name_utf8);
934         FREE(dentry->short_name);
935         FREE(dentry->full_path_utf8);
936         if (dentry->d_inode)
937                 put_inode(dentry->d_inode);
938         FREE(dentry);
939 }
940
941 void put_dentry(struct wim_dentry *dentry)
942 {
943         wimlib_assert(dentry->refcnt != 0);
944         if (--dentry->refcnt == 0)
945                 free_dentry(dentry);
946 }
947
948 /* This function is passed as an argument to for_dentry_in_tree_depth() in order
949  * to free a directory tree. */
950 static int do_free_dentry(struct wim_dentry *dentry, void *__lookup_table)
951 {
952         struct wim_lookup_table *lookup_table = __lookup_table;
953         unsigned i;
954
955         if (lookup_table) {
956                 struct wim_lookup_table_entry *lte;
957                 struct wim_inode *inode = dentry->d_inode;
958                 wimlib_assert(inode->i_nlink != 0);
959                 for (i = 0; i <= inode->i_num_ads; i++) {
960                         lte = inode_stream_lte(inode, i, lookup_table);
961                         if (lte)
962                                 lte_decrement_refcnt(lte, lookup_table);
963                 }
964         }
965
966         put_dentry(dentry);
967         return 0;
968 }
969
970 /*
971  * Unlinks and frees a dentry tree.
972  *
973  * @root:               The root of the tree.
974  * @lookup_table:       The lookup table for dentries.  If non-NULL, the
975  *                      reference counts in the lookup table for the lookup
976  *                      table entries corresponding to the dentries will be
977  *                      decremented.
978  */
979 void free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
980 {
981         if (root)
982                 for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
983 }
984
985 int increment_dentry_refcnt(struct wim_dentry *dentry, void *ignore)
986 {
987         dentry->refcnt++;
988         return 0;
989 }
990
991 /*
992  * Links a dentry into the directory tree.
993  *
994  * @dentry: The dentry to link.
995  * @parent: The dentry that will be the parent of @dentry.
996  */
997 bool dentry_add_child(struct wim_dentry * restrict parent,
998                       struct wim_dentry * restrict child)
999 {
1000         wimlib_assert(dentry_is_directory(parent));
1001
1002         struct rb_root *root = &parent->d_inode->i_children;
1003         struct rb_node **new = &(root->rb_node);
1004         struct rb_node *rb_parent = NULL;
1005
1006         while (*new) {
1007                 struct wim_dentry *this = rbnode_dentry(*new);
1008                 int result = dentry_compare_names(child, this);
1009
1010                 rb_parent = *new;
1011
1012                 if (result < 0)
1013                         new = &((*new)->rb_left);
1014                 else if (result > 0)
1015                         new = &((*new)->rb_right);
1016                 else
1017                         return false;
1018         }
1019         child->parent = parent;
1020         rb_link_node(&child->rb_node, rb_parent, new);
1021         rb_insert_color(&child->rb_node, root);
1022         return true;
1023 }
1024
1025 /* Unlink a WIM dentry from the directory entry tree. */
1026 void unlink_dentry(struct wim_dentry *dentry)
1027 {
1028         struct wim_dentry *parent = dentry->parent;
1029         if (parent == dentry)
1030                 return;
1031         rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1032 }
1033
1034 /*
1035  * Returns the alternate data stream entry belonging to @inode that has the
1036  * stream name @stream_name.
1037  */
1038 struct wim_ads_entry *inode_get_ads_entry(struct wim_inode *inode,
1039                                       const char *stream_name,
1040                                       u16 *idx_ret)
1041 {
1042         if (inode->i_num_ads != 0) {
1043                 u16 i = 0;
1044                 size_t stream_name_len = strlen(stream_name);
1045                 do {
1046                         if (ads_entry_has_name(&inode->i_ads_entries[i],
1047                                                stream_name, stream_name_len))
1048                         {
1049                                 if (idx_ret)
1050                                         *idx_ret = i;
1051                                 return &inode->i_ads_entries[i];
1052                         }
1053                 } while (++i != inode->i_num_ads);
1054         }
1055         return NULL;
1056 }
1057
1058 /*
1059  * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1060  * NULL if memory could not be allocated.
1061  */
1062 struct wim_ads_entry *inode_add_ads(struct wim_inode *inode, const char *stream_name)
1063 {
1064         u16 num_ads;
1065         struct wim_ads_entry *ads_entries;
1066         struct wim_ads_entry *new_entry;
1067
1068         DEBUG("Add alternate data stream \"%s\"", stream_name);
1069
1070         if (inode->i_num_ads >= 0xfffe) {
1071                 ERROR("Too many alternate data streams in one inode!");
1072                 return NULL;
1073         }
1074         num_ads = inode->i_num_ads + 1;
1075         ads_entries = REALLOC(inode->i_ads_entries,
1076                               num_ads * sizeof(inode->i_ads_entries[0]));
1077         if (!ads_entries) {
1078                 ERROR("Failed to allocate memory for new alternate data stream");
1079                 return NULL;
1080         }
1081         inode->i_ads_entries = ads_entries;
1082
1083         new_entry = &inode->i_ads_entries[num_ads - 1];
1084         if (init_ads_entry(new_entry, stream_name) != 0)
1085                 return NULL;
1086 #ifdef WITH_FUSE
1087         new_entry->stream_id = inode->i_next_stream_id++;
1088 #endif
1089         inode->i_num_ads = num_ads;
1090         return new_entry;
1091 }
1092
1093 int inode_add_ads_with_data(struct wim_inode *inode, const char *name,
1094                             const u8 *value, size_t size,
1095                             struct wim_lookup_table *lookup_table)
1096 {
1097         int ret = WIMLIB_ERR_NOMEM;
1098         struct wim_ads_entry *new_ads_entry;
1099         struct wim_lookup_table_entry *existing_lte;
1100         struct wim_lookup_table_entry *lte;
1101         u8 value_hash[SHA1_HASH_SIZE];
1102
1103         wimlib_assert(inode->i_resolved);
1104         new_ads_entry = inode_add_ads(inode, name);
1105         if (!new_ads_entry)
1106                 goto out;
1107         sha1_buffer((const u8*)value, size, value_hash);
1108         existing_lte = __lookup_resource(lookup_table, value_hash);
1109         if (existing_lte) {
1110                 lte = existing_lte;
1111                 lte->refcnt++;
1112         } else {
1113                 u8 *value_copy;
1114                 lte = new_lookup_table_entry();
1115                 if (!lte)
1116                         goto out_free_ads_entry;
1117                 value_copy = MALLOC(size);
1118                 if (!value_copy) {
1119                         FREE(lte);
1120                         goto out_free_ads_entry;
1121                 }
1122                 memcpy(value_copy, value, size);
1123                 lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
1124                 lte->attached_buffer              = value_copy;
1125                 lte->resource_entry.original_size = size;
1126                 lte->resource_entry.size          = size;
1127                 lte->resource_entry.flags         = 0;
1128                 copy_hash(lte->hash, value_hash);
1129                 lookup_table_insert(lookup_table, lte);
1130         }
1131         new_ads_entry->lte = lte;
1132         ret = 0;
1133         goto out;
1134 out_free_ads_entry:
1135         inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
1136                          lookup_table);
1137 out:
1138         return ret;
1139 }
1140
1141 /* Remove an alternate data stream from a WIM inode  */
1142 void inode_remove_ads(struct wim_inode *inode, u16 idx,
1143                       struct wim_lookup_table *lookup_table)
1144 {
1145         struct wim_ads_entry *ads_entry;
1146         struct wim_lookup_table_entry *lte;
1147
1148         wimlib_assert(idx < inode->i_num_ads);
1149         wimlib_assert(inode->i_resolved);
1150
1151         ads_entry = &inode->i_ads_entries[idx];
1152
1153         DEBUG("Remove alternate data stream \"%s\"", ads_entry->stream_name_utf8);
1154
1155         lte = ads_entry->lte;
1156         if (lte)
1157                 lte_decrement_refcnt(lte, lookup_table);
1158
1159         destroy_ads_entry(ads_entry);
1160
1161         memmove(&inode->i_ads_entries[idx],
1162                 &inode->i_ads_entries[idx + 1],
1163                 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1164         inode->i_num_ads--;
1165 }
1166
1167 int inode_get_unix_data(const struct wim_inode *inode,
1168                         struct wimlib_unix_data *unix_data,
1169                         u16 *stream_idx_ret)
1170 {
1171         const struct wim_ads_entry *ads_entry;
1172         const struct wim_lookup_table_entry *lte;
1173         size_t size;
1174         int ret;
1175
1176         wimlib_assert(inode->i_resolved);
1177
1178         ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
1179                                         WIMLIB_UNIX_DATA_TAG, NULL);
1180         if (!ads_entry)
1181                 return NO_UNIX_DATA;
1182
1183         if (stream_idx_ret)
1184                 *stream_idx_ret = ads_entry - inode->i_ads_entries;
1185
1186         lte = ads_entry->lte;
1187         if (!lte)
1188                 return NO_UNIX_DATA;
1189
1190         size = wim_resource_size(lte);
1191         if (size != sizeof(struct wimlib_unix_data))
1192                 return BAD_UNIX_DATA;
1193
1194         ret = read_full_wim_resource(lte, (u8*)unix_data, 0);
1195         if (ret)
1196                 return ret;
1197
1198         if (unix_data->version != 0)
1199                 return BAD_UNIX_DATA;
1200         return 0;
1201 }
1202
1203 int inode_set_unix_data(struct wim_inode *inode,
1204                         uid_t uid, gid_t gid, mode_t mode,
1205                         struct wim_lookup_table *lookup_table,
1206                         int which)
1207 {
1208         struct wimlib_unix_data unix_data;
1209         int ret;
1210         bool have_good_unix_data = false;
1211         bool have_unix_data = false;
1212         u16 stream_idx;
1213
1214         if (!(which & UNIX_DATA_CREATE)) {
1215                 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
1216                 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
1217                         have_unix_data = true;
1218                 if (ret == 0)
1219                         have_good_unix_data = true;
1220         }
1221         unix_data.version = 0;
1222         if (which & UNIX_DATA_UID || !have_good_unix_data)
1223                 unix_data.uid = uid;
1224         if (which & UNIX_DATA_GID || !have_good_unix_data)
1225                 unix_data.gid = gid;
1226         if (which & UNIX_DATA_MODE || !have_good_unix_data)
1227                 unix_data.mode = mode;
1228         ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
1229                                       (const u8*)&unix_data,
1230                                       sizeof(struct wimlib_unix_data),
1231                                       lookup_table);
1232         if (ret == 0 && have_unix_data)
1233                 inode_remove_ads(inode, stream_idx, lookup_table);
1234         return ret;
1235 }
1236
1237 /*
1238  * Reads the alternate data stream entries of a WIM dentry.
1239  *
1240  * @p:  Pointer to buffer that starts with the first alternate stream entry.
1241  *
1242  * @inode:      Inode to load the alternate data streams into.
1243  *                      @inode->i_num_ads must have been set to the number of
1244  *                      alternate data streams that are expected.
1245  *
1246  * @remaining_size:     Number of bytes of data remaining in the buffer pointed
1247  *                              to by @p.
1248  *
1249  * The format of the on-disk alternate stream entries is as follows:
1250  *
1251  * struct wim_ads_entry_on_disk {
1252  *      u64  length;          // Length of the entry, in bytes.  This includes
1253  *                                  all fields (including the stream name and
1254  *                                  null terminator if present, AND the padding!).
1255  *      u64  reserved;        // Seems to be unused
1256  *      u8   hash[20];        // SHA1 message digest of the uncompressed stream
1257  *      u16  stream_name_len; // Length of the stream name, in bytes
1258  *      char stream_name[];   // Stream name in UTF-16LE, @stream_name_len bytes long,
1259  *                                  not including null terminator
1260  *      u16  zero;            // UTF-16 null terminator for the stream name, NOT
1261  *                                  included in @stream_name_len.  Based on what
1262  *                                  I've observed from filenames in dentries,
1263  *                                  this field should not exist when
1264  *                                  (@stream_name_len == 0), but you can't
1265  *                                  actually tell because of the padding anyway
1266  *                                  (provided that the padding is zeroed, which
1267  *                                  it always seems to be).
1268  *      char padding[];       // Padding to make the size a multiple of 8 bytes.
1269  * };
1270  *
1271  * In addition, the entries are 8-byte aligned.
1272  *
1273  * Return 0 on success or nonzero on failure.  On success, inode->i_ads_entries
1274  * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads.  On
1275  * failure, @inode is not modified.
1276  */
1277 static int read_ads_entries(const u8 *p, struct wim_inode *inode,
1278                             u64 remaining_size)
1279 {
1280         u16 num_ads;
1281         struct wim_ads_entry *ads_entries;
1282         int ret;
1283
1284         num_ads = inode->i_num_ads;
1285         ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1286         if (!ads_entries) {
1287                 ERROR("Could not allocate memory for %"PRIu16" "
1288                       "alternate data stream entries", num_ads);
1289                 return WIMLIB_ERR_NOMEM;
1290         }
1291
1292         for (u16 i = 0; i < num_ads; i++) {
1293                 struct wim_ads_entry *cur_entry;
1294                 u64 length;
1295                 u64 length_no_padding;
1296                 u64 total_length;
1297                 size_t utf8_len;
1298                 const u8 *p_save = p;
1299
1300                 cur_entry = &ads_entries[i];
1301
1302         #ifdef WITH_FUSE
1303                 ads_entries[i].stream_id = i + 1;
1304         #endif
1305
1306                 /* Read the base stream entry, excluding the stream name. */
1307                 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1308                         ERROR("Stream entries go past end of metadata resource");
1309                         ERROR("(remaining_size = %"PRIu64")", remaining_size);
1310                         ret = WIMLIB_ERR_INVALID_DENTRY;
1311                         goto out_free_ads_entries;
1312                 }
1313
1314                 p = get_u64(p, &length);
1315                 p += 8; /* Skip the reserved field */
1316                 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
1317                 p = get_u16(p, &cur_entry->stream_name_len);
1318
1319                 cur_entry->stream_name = NULL;
1320                 cur_entry->stream_name_utf8 = NULL;
1321
1322                 /* Length including neither the null terminator nor the padding
1323                  * */
1324                 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1325                                     cur_entry->stream_name_len;
1326
1327                 /* Length including the null terminator and the padding */
1328                 total_length = ((length_no_padding + 2) + 7) & ~7;
1329
1330                 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1331
1332                 if (remaining_size < length_no_padding) {
1333                         ERROR("Stream entries go past end of metadata resource");
1334                         ERROR("(remaining_size = %"PRIu64" bytes, "
1335                               "length_no_padding = %"PRIu64" bytes)",
1336                               remaining_size, length_no_padding);
1337                         ret = WIMLIB_ERR_INVALID_DENTRY;
1338                         goto out_free_ads_entries;
1339                 }
1340
1341                 /* The @length field in the on-disk ADS entry is expected to be
1342                  * equal to @total_length, which includes all of the entry and
1343                  * the padding that follows it to align the next ADS entry to an
1344                  * 8-byte boundary.  However, to be safe, we'll accept the
1345                  * length field as long as it's not less than the un-padded
1346                  * total length and not more than the padded total length. */
1347                 if (length < length_no_padding || length > total_length) {
1348                         ERROR("Stream entry has unexpected length "
1349                               "field (length field = %"PRIu64", "
1350                               "unpadded total length = %"PRIu64", "
1351                               "padded total length = %"PRIu64")",
1352                               length, length_no_padding, total_length);
1353                         ret = WIMLIB_ERR_INVALID_DENTRY;
1354                         goto out_free_ads_entries;
1355                 }
1356
1357                 if (cur_entry->stream_name_len) {
1358                         cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
1359                         if (!cur_entry->stream_name) {
1360                                 ret = WIMLIB_ERR_NOMEM;
1361                                 goto out_free_ads_entries;
1362                         }
1363                         get_bytes(p, cur_entry->stream_name_len,
1364                                   (u8*)cur_entry->stream_name);
1365
1366                         ret = utf16_to_utf8(cur_entry->stream_name,
1367                                             cur_entry->stream_name_len,
1368                                             &cur_entry->stream_name_utf8,
1369                                             &utf8_len);
1370                         if (ret != 0)
1371                                 goto out_free_ads_entries;
1372                         cur_entry->stream_name_utf8_len = utf8_len;
1373                 }
1374                 /* It's expected that the size of every ADS entry is a multiple
1375                  * of 8.  However, to be safe, I'm allowing the possibility of
1376                  * an ADS entry at the very end of the metadata resource ending
1377                  * un-aligned.  So although we still need to increment the input
1378                  * pointer by @total_length to reach the next ADS entry, it's
1379                  * possible that less than @total_length is actually remaining
1380                  * in the metadata resource. We should set the remaining size to
1381                  * 0 bytes if this happens. */
1382                 p = p_save + total_length;
1383                 if (remaining_size < total_length)
1384                         remaining_size = 0;
1385                 else
1386                         remaining_size -= total_length;
1387         }
1388         inode->i_ads_entries = ads_entries;
1389 #ifdef WITH_FUSE
1390         inode->i_next_stream_id = inode->i_num_ads + 1;
1391 #endif
1392         return 0;
1393 out_free_ads_entries:
1394         for (u16 i = 0; i < num_ads; i++)
1395                 destroy_ads_entry(&ads_entries[i]);
1396         FREE(ads_entries);
1397         return ret;
1398 }
1399
1400 /*
1401  * Reads a WIM directory entry, including all alternate data stream entries that
1402  * follow it, from the WIM image's metadata resource.
1403  *
1404  * @metadata_resource:  Buffer containing the uncompressed metadata resource.
1405  * @metadata_resource_len:   Length of the metadata resource.
1406  * @offset:     Offset of this directory entry in the metadata resource.
1407  * @dentry:     A `struct wim_dentry' that will be filled in by this function.
1408  *
1409  * Return 0 on success or nonzero on failure.  On failure, @dentry will have
1410  * been modified, but it will not be left with pointers to any allocated
1411  * buffers.  On success, the dentry->length field must be examined.  If zero,
1412  * this was a special "end of directory" dentry and not a real dentry.  If
1413  * nonzero, this was a real dentry.
1414  */
1415 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1416                 u64 offset, struct wim_dentry *dentry)
1417 {
1418         const u8 *p;
1419         u64 calculated_size;
1420         char *file_name = NULL;
1421         char *file_name_utf8 = NULL;
1422         char *short_name = NULL;
1423         u16 short_name_len;
1424         u16 file_name_len;
1425         size_t file_name_utf8_len = 0;
1426         int ret;
1427         struct wim_inode *inode = NULL;
1428
1429         dentry_common_init(dentry);
1430
1431         /*Make sure the dentry really fits into the metadata resource.*/
1432         if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1433                 ERROR("Directory entry starting at %"PRIu64" ends past the "
1434                       "end of the metadata resource (size %"PRIu64")",
1435                       offset, metadata_resource_len);
1436                 return WIMLIB_ERR_INVALID_DENTRY;
1437         }
1438
1439         /* Before reading the whole dentry, we need to read just the length.
1440          * This is because a dentry of length 8 (that is, just the length field)
1441          * terminates the list of sibling directory entries. */
1442
1443         p = get_u64(&metadata_resource[offset], &dentry->length);
1444
1445         /* A zero length field (really a length of 8, since that's how big the
1446          * directory entry is...) indicates that this is the end of directory
1447          * dentry.  We do not read it into memory as an actual dentry, so just
1448          * return successfully in that case. */
1449         if (dentry->length == 0)
1450                 return 0;
1451
1452         /* If the dentry does not overflow the metadata resource buffer and is
1453          * not too short, read the rest of it (excluding the alternate data
1454          * streams, but including the file name and short name variable-length
1455          * fields) into memory. */
1456         if (offset + dentry->length >= metadata_resource_len
1457             || offset + dentry->length < offset)
1458         {
1459                 ERROR("Directory entry at offset %"PRIu64" and with size "
1460                       "%"PRIu64" ends past the end of the metadata resource "
1461                       "(size %"PRIu64")",
1462                       offset, dentry->length, metadata_resource_len);
1463                 return WIMLIB_ERR_INVALID_DENTRY;
1464         }
1465
1466         if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1467                 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1468                       dentry->length);
1469                 return WIMLIB_ERR_INVALID_DENTRY;
1470         }
1471
1472         inode = new_timeless_inode();
1473         if (!inode)
1474                 return WIMLIB_ERR_NOMEM;
1475
1476         p = get_u32(p, &inode->i_attributes);
1477         p = get_u32(p, (u32*)&inode->i_security_id);
1478         p = get_u64(p, &dentry->subdir_offset);
1479
1480         /* 2 unused fields */
1481         p += 2 * sizeof(u64);
1482         /*p = get_u64(p, &dentry->unused1);*/
1483         /*p = get_u64(p, &dentry->unused2);*/
1484
1485         p = get_u64(p, &inode->i_creation_time);
1486         p = get_u64(p, &inode->i_last_access_time);
1487         p = get_u64(p, &inode->i_last_write_time);
1488
1489         p = get_bytes(p, SHA1_HASH_SIZE, inode->i_hash);
1490
1491         /*
1492          * I don't know what's going on here.  It seems like M$ screwed up the
1493          * reparse points, then put the fields in the same place and didn't
1494          * document it.  The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1495          * have something to do with this, but it's not documented.
1496          */
1497         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1498                 /* ??? */
1499                 p += 4;
1500                 p = get_u32(p, &inode->i_reparse_tag);
1501                 p += 4;
1502         } else {
1503                 p = get_u32(p, &inode->i_reparse_tag);
1504                 p = get_u64(p, &inode->i_ino);
1505         }
1506
1507         /* By the way, the reparse_reserved field does not actually exist (at
1508          * least when the file is not a reparse point) */
1509
1510         p = get_u16(p, &inode->i_num_ads);
1511
1512         p = get_u16(p, &short_name_len);
1513         p = get_u16(p, &file_name_len);
1514
1515         /* We now know the length of the file name and short name.  Make sure
1516          * the length of the dentry is large enough to actually hold them.
1517          *
1518          * The calculated length here is unaligned to allow for the possibility
1519          * that the dentry->length names an unaligned length, although this
1520          * would be unexpected. */
1521         calculated_size = __dentry_correct_length_unaligned(file_name_len,
1522                                                             short_name_len);
1523
1524         if (dentry->length < calculated_size) {
1525                 ERROR("Unexpected end of directory entry! (Expected "
1526                       "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1527                       "short_name_len = %hu, file_name_len = %hu)",
1528                       calculated_size, dentry->length,
1529                       short_name_len, file_name_len);
1530                 ret = WIMLIB_ERR_INVALID_DENTRY;
1531                 goto out_free_inode;
1532         }
1533
1534         /* Read the filename if present.  Note: if the filename is empty, there
1535          * is no null terminator following it. */
1536         if (file_name_len) {
1537                 file_name = MALLOC(file_name_len);
1538                 if (!file_name) {
1539                         ERROR("Failed to allocate %hu bytes for dentry file name",
1540                               file_name_len);
1541                         ret = WIMLIB_ERR_NOMEM;
1542                         goto out_free_inode;
1543                 }
1544                 p = get_bytes(p, file_name_len, file_name);
1545
1546                 /* Convert filename to UTF-8. */
1547                 ret = utf16_to_utf8(file_name, file_name_len, &file_name_utf8,
1548                                     &file_name_utf8_len);
1549                 if (ret != 0)
1550                         goto out_free_file_name;
1551                 if (*(u16*)p)
1552                         WARNING("Expected two zero bytes following the file name "
1553                                 "`%s', but found non-zero bytes", file_name_utf8);
1554                 p += 2;
1555         }
1556
1557         /* Align the calculated size */
1558         calculated_size = (calculated_size + 7) & ~7;
1559
1560         if (dentry->length > calculated_size) {
1561                 /* Weird; the dentry says it's longer than it should be.  Note
1562                  * that the length field does NOT include the size of the
1563                  * alternate stream entries. */
1564
1565                 /* Strangely, some directory entries inexplicably have a little
1566                  * over 70 bytes of extra data.  The exact amount of data seems
1567                  * to be 72 bytes, but it is aligned on the next 8-byte
1568                  * boundary.  It does NOT seem to be alternate data stream
1569                  * entries.  Here's an example of the aligned data:
1570                  *
1571                  * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1572                  * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1573                  * 00000000 00000000 00000000 00000000
1574                  *
1575                  * Here's one interpretation of how the data is laid out.
1576                  *
1577                  * struct unknown {
1578                  *      u32 field1; (always 0x00000001)
1579                  *      u32 field2; (always 0x40000000)
1580                  *      u8  data[48]; (???)
1581                  *      u64 reserved1; (always 0)
1582                  *      u64 reserved2; (always 0)
1583                  * };*/
1584                 DEBUG("Dentry for file or directory `%s' has %"PRIu64" extra "
1585                       "bytes of data",
1586                       file_name_utf8, dentry->length - calculated_size);
1587         }
1588
1589         /* Read the short filename if present.  Note: if there is no short
1590          * filename, there is no null terminator following it. */
1591         if (short_name_len) {
1592                 short_name = MALLOC(short_name_len);
1593                 if (!short_name) {
1594                         ERROR("Failed to allocate %hu bytes for short filename",
1595                               short_name_len);
1596                         ret = WIMLIB_ERR_NOMEM;
1597                         goto out_free_file_name_utf8;
1598                 }
1599
1600                 p = get_bytes(p, short_name_len, short_name);
1601                 if (*(u16*)p)
1602                         WARNING("Expected two zero bytes following the short name of "
1603                                 "`%s', but found non-zero bytes", file_name_utf8);
1604                 p += 2;
1605         }
1606
1607         /*
1608          * Read the alternate data streams, if present.  dentry->num_ads tells
1609          * us how many they are, and they will directly follow the dentry
1610          * on-disk.
1611          *
1612          * Note that each alternate data stream entry begins on an 8-byte
1613          * aligned boundary, and the alternate data stream entries are NOT
1614          * included in the dentry->length field for some reason.
1615          */
1616         if (inode->i_num_ads != 0) {
1617
1618                 /* Trying different lengths is just a hack to make sure we have
1619                  * a chance of reading the ADS entries correctly despite the
1620                  * poor documentation. */
1621
1622                 if (calculated_size != dentry->length) {
1623                         WARNING("Trying calculated dentry length (%"PRIu64") "
1624                                 "instead of dentry->length field (%"PRIu64") "
1625                                 "to read ADS entries",
1626                                 calculated_size, dentry->length);
1627                 }
1628                 u64 lengths_to_try[3] = {calculated_size,
1629                                          (dentry->length + 7) & ~7,
1630                                          dentry->length};
1631                 ret = WIMLIB_ERR_INVALID_DENTRY;
1632                 for (size_t i = 0; i < ARRAY_LEN(lengths_to_try); i++) {
1633                         if (lengths_to_try[i] > metadata_resource_len - offset)
1634                                 continue;
1635                         ret = read_ads_entries(&metadata_resource[offset + lengths_to_try[i]],
1636                                                inode,
1637                                                metadata_resource_len - offset - lengths_to_try[i]);
1638                         if (ret == 0)
1639                                 goto out;
1640                 }
1641                 ERROR("Failed to read alternate data stream "
1642                       "entries of `%s'", dentry->file_name_utf8);
1643                 goto out_free_short_name;
1644         }
1645 out:
1646
1647         /* We've read all the data for this dentry.  Set the names and their
1648          * lengths, and we've done. */
1649         dentry->d_inode            = inode;
1650         dentry->file_name          = file_name;
1651         dentry->file_name_utf8     = file_name_utf8;
1652         dentry->short_name         = short_name;
1653         dentry->file_name_len      = file_name_len;
1654         dentry->file_name_utf8_len = file_name_utf8_len;
1655         dentry->short_name_len     = short_name_len;
1656         return 0;
1657 out_free_short_name:
1658         FREE(short_name);
1659 out_free_file_name_utf8:
1660         FREE(file_name_utf8);
1661 out_free_file_name:
1662         FREE(file_name);
1663 out_free_inode:
1664         free_inode(inode);
1665         return ret;
1666 }
1667
1668 /* Reads the children of a dentry, and all their children, ..., etc. from the
1669  * metadata resource and into the dentry tree.
1670  *
1671  * @metadata_resource:  An array that contains the uncompressed metadata
1672  *                      resource for the WIM file.
1673  *
1674  * @metadata_resource_len:  The length of the uncompressed metadata resource, in
1675  *                          bytes.
1676  *
1677  * @dentry:     A pointer to a `struct wim_dentry' that is the root of the directory
1678  *              tree and has already been read from the metadata resource.  It
1679  *              does not need to be the real root because this procedure is
1680  *              called recursively.
1681  *
1682  * @return:     Zero on success, nonzero on failure.
1683  */
1684 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1685                      struct wim_dentry *dentry)
1686 {
1687         u64 cur_offset = dentry->subdir_offset;
1688         struct wim_dentry *child;
1689         struct wim_dentry cur_child;
1690         int ret;
1691
1692         /*
1693          * If @dentry has no child dentries, nothing more needs to be done for
1694          * this branch.  This is the case for regular files, symbolic links, and
1695          * *possibly* empty directories (although an empty directory may also
1696          * have one child dentry that is the special end-of-directory dentry)
1697          */
1698         if (cur_offset == 0)
1699                 return 0;
1700
1701         /* Find and read all the children of @dentry. */
1702         while (1) {
1703
1704                 /* Read next child of @dentry into @cur_child. */
1705                 ret = read_dentry(metadata_resource, metadata_resource_len,
1706                                   cur_offset, &cur_child);
1707                 if (ret != 0)
1708                         break;
1709
1710                 /* Check for end of directory. */
1711                 if (cur_child.length == 0)
1712                         break;
1713
1714                 /* Not end of directory.  Allocate this child permanently and
1715                  * link it to the parent and previous child. */
1716                 child = MALLOC(sizeof(struct wim_dentry));
1717                 if (!child) {
1718                         ERROR("Failed to allocate %zu bytes for new dentry",
1719                               sizeof(struct wim_dentry));
1720                         ret = WIMLIB_ERR_NOMEM;
1721                         break;
1722                 }
1723                 memcpy(child, &cur_child, sizeof(struct wim_dentry));
1724                 dentry_add_child(dentry, child);
1725                 inode_add_dentry(child, child->d_inode);
1726
1727                 /* If there are children of this child, call this procedure
1728                  * recursively. */
1729                 if (child->subdir_offset != 0) {
1730                         ret = read_dentry_tree(metadata_resource,
1731                                                metadata_resource_len, child);
1732                         if (ret != 0)
1733                                 break;
1734                 }
1735
1736                 /* Advance to the offset of the next child.  Note: We need to
1737                  * advance by the TOTAL length of the dentry, not by the length
1738                  * child->length, which although it does take into account the
1739                  * padding, it DOES NOT take into account alternate stream
1740                  * entries. */
1741                 cur_offset += dentry_total_length(child);
1742         }
1743         return ret;
1744 }
1745
1746 /*
1747  * Writes a WIM dentry to an output buffer.
1748  *
1749  * @dentry:  The dentry structure.
1750  * @p:       The memory location to write the data to.
1751  * @return:  Pointer to the byte after the last byte we wrote as part of the
1752  *              dentry.
1753  */
1754 static u8 *write_dentry(const struct wim_dentry *dentry, u8 *p)
1755 {
1756         u8 *orig_p = p;
1757         const u8 *hash;
1758         const struct wim_inode *inode = dentry->d_inode;
1759
1760         /* We calculate the correct length of the dentry ourselves because the
1761          * dentry->length field may been set to an unexpected value from when we
1762          * read the dentry in (for example, there may have been unknown data
1763          * appended to the end of the dentry...) */
1764         u64 length = dentry_correct_length(dentry);
1765
1766         p = put_u64(p, length);
1767         p = put_u32(p, inode->i_attributes);
1768         p = put_u32(p, inode->i_security_id);
1769         p = put_u64(p, dentry->subdir_offset);
1770         p = put_u64(p, 0); /* unused1 */
1771         p = put_u64(p, 0); /* unused2 */
1772         p = put_u64(p, inode->i_creation_time);
1773         p = put_u64(p, inode->i_last_access_time);
1774         p = put_u64(p, inode->i_last_write_time);
1775         hash = inode_stream_hash(inode, 0);
1776         p = put_bytes(p, SHA1_HASH_SIZE, hash);
1777         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1778                 p = put_zeroes(p, 4);
1779                 p = put_u32(p, inode->i_reparse_tag);
1780                 p = put_zeroes(p, 4);
1781         } else {
1782                 u64 link_group_id;
1783                 p = put_u32(p, 0);
1784                 if (inode->i_nlink == 1)
1785                         link_group_id = 0;
1786                 else
1787                         link_group_id = inode->i_ino;
1788                 p = put_u64(p, link_group_id);
1789         }
1790         p = put_u16(p, inode->i_num_ads);
1791         p = put_u16(p, dentry->short_name_len);
1792         p = put_u16(p, dentry->file_name_len);
1793         if (dentry->file_name_len) {
1794                 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1795                 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1796         }
1797         if (dentry->short_name) {
1798                 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1799                 p = put_u16(p, 0); /* short name padding, 2 bytes */
1800         }
1801
1802         /* Align to 8-byte boundary */
1803         wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1804         p = put_zeroes(p, length - (p - orig_p));
1805
1806         /* Write the alternate data streams, if there are any.  Please see
1807          * read_ads_entries() for comments about the format of the on-disk
1808          * alternate data stream entries. */
1809         for (u16 i = 0; i < inode->i_num_ads; i++) {
1810                 p = put_u64(p, ads_entry_total_length(&inode->i_ads_entries[i]));
1811                 p = put_u64(p, 0); /* Unused */
1812                 hash = inode_stream_hash(inode, i + 1);
1813                 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1814                 p = put_u16(p, inode->i_ads_entries[i].stream_name_len);
1815                 if (inode->i_ads_entries[i].stream_name_len) {
1816                         p = put_bytes(p, inode->i_ads_entries[i].stream_name_len,
1817                                          (u8*)inode->i_ads_entries[i].stream_name);
1818                         p = put_u16(p, 0);
1819                 }
1820                 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1821         }
1822         wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1823         return p;
1824 }
1825
1826 static int write_dentry_cb(struct wim_dentry *dentry, void *_p)
1827 {
1828         u8 **p = _p;
1829         *p = write_dentry(dentry, *p);
1830         return 0;
1831 }
1832
1833 static u8 *write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
1834
1835 static int write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
1836 {
1837         u8 **p = _p;
1838         *p = write_dentry_tree_recursive(dentry, *p);
1839         return 0;
1840 }
1841
1842 /* Recursive function that writes a dentry tree rooted at @parent, not including
1843  * @parent itself, which has already been written. */
1844 static u8 *write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
1845 {
1846         /* Nothing to do if this dentry has no children. */
1847         if (parent->subdir_offset == 0)
1848                 return p;
1849
1850         /* Write child dentries and end-of-directory entry.
1851          *
1852          * Note: we need to write all of this dentry's children before
1853          * recursively writing the directory trees rooted at each of the child
1854          * dentries, since the on-disk dentries for a dentry's children are
1855          * always located at consecutive positions in the metadata resource! */
1856         for_dentry_child(parent, write_dentry_cb, &p);
1857
1858         /* write end of directory entry */
1859         p = put_u64(p, 0);
1860
1861         /* Recurse on children. */
1862         for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
1863         return p;
1864 }
1865
1866 /* Writes a directory tree to the metadata resource.
1867  *
1868  * @root:       Root of the dentry tree.
1869  * @p:          Pointer to a buffer with enough space for the dentry tree.
1870  *
1871  * Returns pointer to the byte after the last byte we wrote.
1872  */
1873 u8 *write_dentry_tree(const struct wim_dentry *root, u8 *p)
1874 {
1875         DEBUG("Writing dentry tree.");
1876         wimlib_assert(dentry_is_root(root));
1877
1878         /* If we're the root dentry, we have no parent that already
1879          * wrote us, so we need to write ourselves. */
1880         p = write_dentry(root, p);
1881
1882         /* Write end of directory entry after the root dentry just to be safe;
1883          * however the root dentry obviously cannot have any siblings. */
1884         p = put_u64(p, 0);
1885
1886         /* Recursively write the rest of the dentry tree. */
1887         return write_dentry_tree_recursive(root, p);
1888 }