NTFS capture: Get DOS names from ntfs_readdir()
[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);
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,
770                                                           lookup_table));
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 #ifdef WITH_FUSE
1026 /* Unlink a WIM dentry from the directory entry tree. */
1027 void unlink_dentry(struct wim_dentry *dentry)
1028 {
1029         struct wim_dentry *parent = dentry->parent;
1030         if (parent == dentry)
1031                 return;
1032         rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
1033 }
1034 #endif
1035
1036 #ifdef WITH_FUSE
1037 /* 
1038  * Returns the alternate data stream entry belonging to @inode that has the
1039  * stream name @stream_name.
1040  */
1041 struct wim_ads_entry *inode_get_ads_entry(struct wim_inode *inode,
1042                                       const char *stream_name,
1043                                       u16 *idx_ret)
1044 {
1045         if (inode->i_num_ads != 0) {
1046                 u16 i = 0;
1047                 size_t stream_name_len = strlen(stream_name);
1048                 do {
1049                         if (ads_entry_has_name(&inode->i_ads_entries[i],
1050                                                stream_name, stream_name_len))
1051                         {
1052                                 if (idx_ret)
1053                                         *idx_ret = i;
1054                                 return &inode->i_ads_entries[i];
1055                         }
1056                 } while (++i != inode->i_num_ads);
1057         }
1058         return NULL;
1059 }
1060 #endif
1061
1062 #if defined(WITH_FUSE) || defined(WITH_NTFS_3G)
1063 /*
1064  * Add an alternate stream entry to a WIM inode and return a pointer to it, or
1065  * NULL if memory could not be allocated.
1066  */
1067 struct wim_ads_entry *inode_add_ads(struct wim_inode *inode, const char *stream_name)
1068 {
1069         u16 num_ads;
1070         struct wim_ads_entry *ads_entries;
1071         struct wim_ads_entry *new_entry;
1072
1073         DEBUG("Add alternate data stream \"%s\"", stream_name);
1074
1075         if (inode->i_num_ads >= 0xfffe) {
1076                 ERROR("Too many alternate data streams in one inode!");
1077                 return NULL;
1078         }
1079         num_ads = inode->i_num_ads + 1;
1080         ads_entries = REALLOC(inode->i_ads_entries,
1081                               num_ads * sizeof(inode->i_ads_entries[0]));
1082         if (!ads_entries) {
1083                 ERROR("Failed to allocate memory for new alternate data stream");
1084                 return NULL;
1085         }
1086         inode->i_ads_entries = ads_entries;
1087
1088         new_entry = &inode->i_ads_entries[num_ads - 1];
1089         if (init_ads_entry(new_entry, stream_name) != 0)
1090                 return NULL;
1091 #ifdef WITH_FUSE
1092         new_entry->stream_id = inode->i_next_stream_id++;
1093 #endif
1094         inode->i_num_ads = num_ads;
1095         return new_entry;
1096 }
1097 #endif
1098
1099 #ifdef WITH_FUSE
1100 /* Remove an alternate data stream from a WIM inode  */
1101 void inode_remove_ads(struct wim_inode *inode, u16 idx,
1102                       struct wim_lookup_table *lookup_table)
1103 {
1104         struct wim_ads_entry *ads_entry;
1105         struct wim_lookup_table_entry *lte;
1106
1107         wimlib_assert(idx < inode->i_num_ads);
1108         wimlib_assert(inode->i_resolved);
1109
1110         ads_entry = &inode->i_ads_entries[idx];
1111
1112         DEBUG("Remove alternate data stream \"%s\"", ads_entry->stream_name_utf8);
1113
1114         lte = ads_entry->lte;
1115         if (lte)
1116                 lte_decrement_refcnt(lte, lookup_table);
1117
1118         destroy_ads_entry(ads_entry);
1119
1120         memmove(&inode->i_ads_entries[idx],
1121                 &inode->i_ads_entries[idx + 1],
1122                 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
1123         inode->i_num_ads--;
1124 }
1125 #endif
1126
1127
1128
1129 /*
1130  * Reads the alternate data stream entries of a WIM dentry.
1131  *
1132  * @p:  Pointer to buffer that starts with the first alternate stream entry.
1133  *
1134  * @inode:      Inode to load the alternate data streams into.
1135  *                      @inode->i_num_ads must have been set to the number of
1136  *                      alternate data streams that are expected.
1137  *
1138  * @remaining_size:     Number of bytes of data remaining in the buffer pointed
1139  *                              to by @p.
1140  *
1141  * The format of the on-disk alternate stream entries is as follows:
1142  *
1143  * struct wim_ads_entry_on_disk {
1144  *      u64  length;          // Length of the entry, in bytes.  This includes
1145  *                                  all fields (including the stream name and
1146  *                                  null terminator if present, AND the padding!).
1147  *      u64  reserved;        // Seems to be unused
1148  *      u8   hash[20];        // SHA1 message digest of the uncompressed stream
1149  *      u16  stream_name_len; // Length of the stream name, in bytes
1150  *      char stream_name[];   // Stream name in UTF-16LE, @stream_name_len bytes long,
1151  *                                  not including null terminator
1152  *      u16  zero;            // UTF-16 null terminator for the stream name, NOT
1153  *                                  included in @stream_name_len.  Based on what
1154  *                                  I've observed from filenames in dentries,
1155  *                                  this field should not exist when
1156  *                                  (@stream_name_len == 0), but you can't
1157  *                                  actually tell because of the padding anyway
1158  *                                  (provided that the padding is zeroed, which
1159  *                                  it always seems to be).
1160  *      char padding[];       // Padding to make the size a multiple of 8 bytes.
1161  * };
1162  *
1163  * In addition, the entries are 8-byte aligned.
1164  *
1165  * Return 0 on success or nonzero on failure.  On success, inode->i_ads_entries
1166  * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads.  On
1167  * failure, @inode is not modified.
1168  */
1169 static int read_ads_entries(const u8 *p, struct wim_inode *inode,
1170                             u64 remaining_size)
1171 {
1172         u16 num_ads;
1173         struct wim_ads_entry *ads_entries;
1174         int ret;
1175
1176         num_ads = inode->i_num_ads;
1177         ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
1178         if (!ads_entries) {
1179                 ERROR("Could not allocate memory for %"PRIu16" "
1180                       "alternate data stream entries", num_ads);
1181                 return WIMLIB_ERR_NOMEM;
1182         }
1183
1184         for (u16 i = 0; i < num_ads; i++) {
1185                 struct wim_ads_entry *cur_entry;
1186                 u64 length;
1187                 u64 length_no_padding;
1188                 u64 total_length;
1189                 size_t utf8_len;
1190                 const u8 *p_save = p;
1191
1192                 cur_entry = &ads_entries[i];
1193
1194         #ifdef WITH_FUSE
1195                 ads_entries[i].stream_id = i + 1;
1196         #endif
1197
1198                 /* Read the base stream entry, excluding the stream name. */
1199                 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
1200                         ERROR("Stream entries go past end of metadata resource");
1201                         ERROR("(remaining_size = %"PRIu64")", remaining_size);
1202                         ret = WIMLIB_ERR_INVALID_DENTRY;
1203                         goto out_free_ads_entries;
1204                 }
1205
1206                 p = get_u64(p, &length);
1207                 p += 8; /* Skip the reserved field */
1208                 p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
1209                 p = get_u16(p, &cur_entry->stream_name_len);
1210
1211                 cur_entry->stream_name = NULL;
1212                 cur_entry->stream_name_utf8 = NULL;
1213
1214                 /* Length including neither the null terminator nor the padding
1215                  * */
1216                 length_no_padding = WIM_ADS_ENTRY_DISK_SIZE +
1217                                     cur_entry->stream_name_len;
1218
1219                 /* Length including the null terminator and the padding */
1220                 total_length = ((length_no_padding + 2) + 7) & ~7;
1221
1222                 wimlib_assert(total_length == ads_entry_total_length(cur_entry));
1223
1224                 if (remaining_size < length_no_padding) {
1225                         ERROR("Stream entries go past end of metadata resource");
1226                         ERROR("(remaining_size = %"PRIu64" bytes, "
1227                               "length_no_padding = %"PRIu64" bytes)",
1228                               remaining_size, length_no_padding);
1229                         ret = WIMLIB_ERR_INVALID_DENTRY;
1230                         goto out_free_ads_entries;
1231                 }
1232
1233                 /* The @length field in the on-disk ADS entry is expected to be
1234                  * equal to @total_length, which includes all of the entry and
1235                  * the padding that follows it to align the next ADS entry to an
1236                  * 8-byte boundary.  However, to be safe, we'll accept the
1237                  * length field as long as it's not less than the un-padded
1238                  * total length and not more than the padded total length. */
1239                 if (length < length_no_padding || length > total_length) {
1240                         ERROR("Stream entry has unexpected length "
1241                               "field (length field = %"PRIu64", "
1242                               "unpadded total length = %"PRIu64", "
1243                               "padded total length = %"PRIu64")",
1244                               length, length_no_padding, total_length);
1245                         ret = WIMLIB_ERR_INVALID_DENTRY;
1246                         goto out_free_ads_entries;
1247                 }
1248
1249                 if (cur_entry->stream_name_len) {
1250                         cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
1251                         if (!cur_entry->stream_name) {
1252                                 ret = WIMLIB_ERR_NOMEM;
1253                                 goto out_free_ads_entries;
1254                         }
1255                         get_bytes(p, cur_entry->stream_name_len,
1256                                   (u8*)cur_entry->stream_name);
1257
1258                         ret = utf16_to_utf8(cur_entry->stream_name,
1259                                             cur_entry->stream_name_len,
1260                                             &cur_entry->stream_name_utf8,
1261                                             &utf8_len);
1262                         if (ret != 0)
1263                                 goto out_free_ads_entries;
1264                         cur_entry->stream_name_utf8_len = utf8_len;
1265                 }
1266                 /* It's expected that the size of every ADS entry is a multiple
1267                  * of 8.  However, to be safe, I'm allowing the possibility of
1268                  * an ADS entry at the very end of the metadata resource ending
1269                  * un-aligned.  So although we still need to increment the input
1270                  * pointer by @total_length to reach the next ADS entry, it's
1271                  * possible that less than @total_length is actually remaining
1272                  * in the metadata resource. We should set the remaining size to
1273                  * 0 bytes if this happens. */
1274                 p = p_save + total_length;
1275                 if (remaining_size < total_length)
1276                         remaining_size = 0;
1277                 else
1278                         remaining_size -= total_length;
1279         }
1280         inode->i_ads_entries = ads_entries;
1281 #ifdef WITH_FUSE
1282         inode->i_next_stream_id = inode->i_num_ads + 1;
1283 #endif
1284         return 0;
1285 out_free_ads_entries:
1286         for (u16 i = 0; i < num_ads; i++)
1287                 destroy_ads_entry(&ads_entries[i]);
1288         FREE(ads_entries);
1289         return ret;
1290 }
1291
1292 /*
1293  * Reads a WIM directory entry, including all alternate data stream entries that
1294  * follow it, from the WIM image's metadata resource.
1295  *
1296  * @metadata_resource:  Buffer containing the uncompressed metadata resource.
1297  * @metadata_resource_len:   Length of the metadata resource.
1298  * @offset:     Offset of this directory entry in the metadata resource.
1299  * @dentry:     A `struct wim_dentry' that will be filled in by this function.
1300  *
1301  * Return 0 on success or nonzero on failure.  On failure, @dentry will have
1302  * been modified, but it will not be left with pointers to any allocated
1303  * buffers.  On success, the dentry->length field must be examined.  If zero,
1304  * this was a special "end of directory" dentry and not a real dentry.  If
1305  * nonzero, this was a real dentry.
1306  */
1307 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
1308                 u64 offset, struct wim_dentry *dentry)
1309 {
1310         const u8 *p;
1311         u64 calculated_size;
1312         char *file_name = NULL;
1313         char *file_name_utf8 = NULL;
1314         char *short_name = NULL;
1315         u16 short_name_len;
1316         u16 file_name_len;
1317         size_t file_name_utf8_len = 0;
1318         int ret;
1319         struct wim_inode *inode = NULL;
1320
1321         dentry_common_init(dentry);
1322
1323         /*Make sure the dentry really fits into the metadata resource.*/
1324         if (offset + 8 > metadata_resource_len || offset + 8 < offset) {
1325                 ERROR("Directory entry starting at %"PRIu64" ends past the "
1326                       "end of the metadata resource (size %"PRIu64")",
1327                       offset, metadata_resource_len);
1328                 return WIMLIB_ERR_INVALID_DENTRY;
1329         }
1330
1331         /* Before reading the whole dentry, we need to read just the length.
1332          * This is because a dentry of length 8 (that is, just the length field)
1333          * terminates the list of sibling directory entries. */
1334
1335         p = get_u64(&metadata_resource[offset], &dentry->length);
1336
1337         /* A zero length field (really a length of 8, since that's how big the
1338          * directory entry is...) indicates that this is the end of directory
1339          * dentry.  We do not read it into memory as an actual dentry, so just
1340          * return successfully in that case. */
1341         if (dentry->length == 0)
1342                 return 0;
1343
1344         /* If the dentry does not overflow the metadata resource buffer and is
1345          * not too short, read the rest of it (excluding the alternate data
1346          * streams, but including the file name and short name variable-length
1347          * fields) into memory. */
1348         if (offset + dentry->length >= metadata_resource_len
1349             || offset + dentry->length < offset)
1350         {
1351                 ERROR("Directory entry at offset %"PRIu64" and with size "
1352                       "%"PRIu64" ends past the end of the metadata resource "
1353                       "(size %"PRIu64")",
1354                       offset, dentry->length, metadata_resource_len);
1355                 return WIMLIB_ERR_INVALID_DENTRY;
1356         }
1357
1358         if (dentry->length < WIM_DENTRY_DISK_SIZE) {
1359                 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
1360                       dentry->length);
1361                 return WIMLIB_ERR_INVALID_DENTRY;
1362         }
1363
1364         inode = new_timeless_inode();
1365         if (!inode)
1366                 return WIMLIB_ERR_NOMEM;
1367
1368         p = get_u32(p, &inode->i_attributes);
1369         p = get_u32(p, (u32*)&inode->i_security_id);
1370         p = get_u64(p, &dentry->subdir_offset);
1371
1372         /* 2 unused fields */
1373         p += 2 * sizeof(u64);
1374         /*p = get_u64(p, &dentry->unused1);*/
1375         /*p = get_u64(p, &dentry->unused2);*/
1376
1377         p = get_u64(p, &inode->i_creation_time);
1378         p = get_u64(p, &inode->i_last_access_time);
1379         p = get_u64(p, &inode->i_last_write_time);
1380
1381         p = get_bytes(p, SHA1_HASH_SIZE, inode->i_hash);
1382
1383         /*
1384          * I don't know what's going on here.  It seems like M$ screwed up the
1385          * reparse points, then put the fields in the same place and didn't
1386          * document it.  The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
1387          * have something to do with this, but it's not documented.
1388          */
1389         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1390                 /* ??? */
1391                 p += 4;
1392                 p = get_u32(p, &inode->i_reparse_tag);
1393                 p += 4;
1394         } else {
1395                 p = get_u32(p, &inode->i_reparse_tag);
1396                 p = get_u64(p, &inode->i_ino);
1397         }
1398
1399         /* By the way, the reparse_reserved field does not actually exist (at
1400          * least when the file is not a reparse point) */
1401
1402         p = get_u16(p, &inode->i_num_ads);
1403
1404         p = get_u16(p, &short_name_len);
1405         p = get_u16(p, &file_name_len);
1406
1407         /* We now know the length of the file name and short name.  Make sure
1408          * the length of the dentry is large enough to actually hold them.
1409          *
1410          * The calculated length here is unaligned to allow for the possibility
1411          * that the dentry->length names an unaligned length, although this
1412          * would be unexpected. */
1413         calculated_size = __dentry_correct_length_unaligned(file_name_len,
1414                                                             short_name_len);
1415
1416         if (dentry->length < calculated_size) {
1417                 ERROR("Unexpected end of directory entry! (Expected "
1418                       "at least %"PRIu64" bytes, got %"PRIu64" bytes. "
1419                       "short_name_len = %hu, file_name_len = %hu)",
1420                       calculated_size, dentry->length,
1421                       short_name_len, file_name_len);
1422                 ret = WIMLIB_ERR_INVALID_DENTRY;
1423                 goto out_free_inode;
1424         }
1425
1426         /* Read the filename if present.  Note: if the filename is empty, there
1427          * is no null terminator following it. */
1428         if (file_name_len) {
1429                 file_name = MALLOC(file_name_len);
1430                 if (!file_name) {
1431                         ERROR("Failed to allocate %hu bytes for dentry file name",
1432                               file_name_len);
1433                         ret = WIMLIB_ERR_NOMEM;
1434                         goto out_free_inode;
1435                 }
1436                 p = get_bytes(p, file_name_len, file_name);
1437
1438                 /* Convert filename to UTF-8. */
1439                 ret = utf16_to_utf8(file_name, file_name_len, &file_name_utf8,
1440                                     &file_name_utf8_len);
1441                 if (ret != 0)
1442                         goto out_free_file_name;
1443                 if (*(u16*)p)
1444                         WARNING("Expected two zero bytes following the file name "
1445                                 "`%s', but found non-zero bytes", file_name_utf8);
1446                 p += 2;
1447         }
1448
1449         /* Align the calculated size */
1450         calculated_size = (calculated_size + 7) & ~7;
1451
1452         if (dentry->length > calculated_size) {
1453                 /* Weird; the dentry says it's longer than it should be.  Note
1454                  * that the length field does NOT include the size of the
1455                  * alternate stream entries. */
1456
1457                 /* Strangely, some directory entries inexplicably have a little
1458                  * over 70 bytes of extra data.  The exact amount of data seems
1459                  * to be 72 bytes, but it is aligned on the next 8-byte
1460                  * boundary.  It does NOT seem to be alternate data stream
1461                  * entries.  Here's an example of the aligned data:
1462                  *
1463                  * 01000000 40000000 6c786bba c58ede11 b0bb0026 1870892a b6adb76f
1464                  * e63a3e46 8fca8653 0d2effa1 6c786bba c58ede11 b0bb0026 1870892a
1465                  * 00000000 00000000 00000000 00000000
1466                  *
1467                  * Here's one interpretation of how the data is laid out.
1468                  *
1469                  * struct unknown {
1470                  *      u32 field1; (always 0x00000001)
1471                  *      u32 field2; (always 0x40000000)
1472                  *      u8  data[48]; (???)
1473                  *      u64 reserved1; (always 0)
1474                  *      u64 reserved2; (always 0)
1475                  * };*/
1476                 DEBUG("Dentry for file or directory `%s' has %"PRIu64" extra "
1477                       "bytes of data",
1478                       file_name_utf8, dentry->length - calculated_size);
1479         }
1480
1481         /* Read the short filename if present.  Note: if there is no short
1482          * filename, there is no null terminator following it. */
1483         if (short_name_len) {
1484                 short_name = MALLOC(short_name_len);
1485                 if (!short_name) {
1486                         ERROR("Failed to allocate %hu bytes for short filename",
1487                               short_name_len);
1488                         ret = WIMLIB_ERR_NOMEM;
1489                         goto out_free_file_name_utf8;
1490                 }
1491
1492                 p = get_bytes(p, short_name_len, short_name);
1493                 if (*(u16*)p)
1494                         WARNING("Expected two zero bytes following the short name of "
1495                                 "`%s', but found non-zero bytes", file_name_utf8);
1496                 p += 2;
1497         }
1498
1499         /*
1500          * Read the alternate data streams, if present.  dentry->num_ads tells
1501          * us how many they are, and they will directly follow the dentry
1502          * on-disk.
1503          *
1504          * Note that each alternate data stream entry begins on an 8-byte
1505          * aligned boundary, and the alternate data stream entries are NOT
1506          * included in the dentry->length field for some reason.
1507          */
1508         if (inode->i_num_ads != 0) {
1509
1510                 /* Trying different lengths is just a hack to make sure we have
1511                  * a chance of reading the ADS entries correctly despite the
1512                  * poor documentation. */
1513
1514                 if (calculated_size != dentry->length) {
1515                         WARNING("Trying calculated dentry length (%"PRIu64") "
1516                                 "instead of dentry->length field (%"PRIu64") "
1517                                 "to read ADS entries",
1518                                 calculated_size, dentry->length);
1519                 }
1520                 u64 lengths_to_try[3] = {calculated_size,
1521                                          (dentry->length + 7) & ~7,
1522                                          dentry->length};
1523                 ret = WIMLIB_ERR_INVALID_DENTRY;
1524                 for (size_t i = 0; i < ARRAY_LEN(lengths_to_try); i++) {
1525                         if (lengths_to_try[i] > metadata_resource_len - offset)
1526                                 continue;
1527                         ret = read_ads_entries(&metadata_resource[offset + lengths_to_try[i]],
1528                                                inode,
1529                                                metadata_resource_len - offset - lengths_to_try[i]);
1530                         if (ret == 0)
1531                                 goto out;
1532                 }
1533                 ERROR("Failed to read alternate data stream "
1534                       "entries of `%s'", dentry->file_name_utf8);
1535                 goto out_free_short_name;
1536         }
1537 out:
1538
1539         /* We've read all the data for this dentry.  Set the names and their
1540          * lengths, and we've done. */
1541         dentry->d_inode            = inode;
1542         dentry->file_name          = file_name;
1543         dentry->file_name_utf8     = file_name_utf8;
1544         dentry->short_name         = short_name;
1545         dentry->file_name_len      = file_name_len;
1546         dentry->file_name_utf8_len = file_name_utf8_len;
1547         dentry->short_name_len     = short_name_len;
1548         return 0;
1549 out_free_short_name:
1550         FREE(short_name);
1551 out_free_file_name_utf8:
1552         FREE(file_name_utf8);
1553 out_free_file_name:
1554         FREE(file_name);
1555 out_free_inode:
1556         free_inode(inode);
1557         return ret;
1558 }
1559
1560 /* Reads the children of a dentry, and all their children, ..., etc. from the
1561  * metadata resource and into the dentry tree.
1562  *
1563  * @metadata_resource:  An array that contains the uncompressed metadata
1564  *                      resource for the WIM file.
1565  *
1566  * @metadata_resource_len:  The length of the uncompressed metadata resource, in
1567  *                          bytes.
1568  *
1569  * @dentry:     A pointer to a `struct wim_dentry' that is the root of the directory
1570  *              tree and has already been read from the metadata resource.  It
1571  *              does not need to be the real root because this procedure is
1572  *              called recursively.
1573  *
1574  * @return:     Zero on success, nonzero on failure.
1575  */
1576 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1577                      struct wim_dentry *dentry)
1578 {
1579         u64 cur_offset = dentry->subdir_offset;
1580         struct wim_dentry *child;
1581         struct wim_dentry cur_child;
1582         int ret;
1583
1584         /*
1585          * If @dentry has no child dentries, nothing more needs to be done for
1586          * this branch.  This is the case for regular files, symbolic links, and
1587          * *possibly* empty directories (although an empty directory may also
1588          * have one child dentry that is the special end-of-directory dentry)
1589          */
1590         if (cur_offset == 0)
1591                 return 0;
1592
1593         /* Find and read all the children of @dentry. */
1594         while (1) {
1595
1596                 /* Read next child of @dentry into @cur_child. */
1597                 ret = read_dentry(metadata_resource, metadata_resource_len,
1598                                   cur_offset, &cur_child);
1599                 if (ret != 0)
1600                         break;
1601
1602                 /* Check for end of directory. */
1603                 if (cur_child.length == 0)
1604                         break;
1605
1606                 /* Not end of directory.  Allocate this child permanently and
1607                  * link it to the parent and previous child. */
1608                 child = MALLOC(sizeof(struct wim_dentry));
1609                 if (!child) {
1610                         ERROR("Failed to allocate %zu bytes for new dentry",
1611                               sizeof(struct wim_dentry));
1612                         ret = WIMLIB_ERR_NOMEM;
1613                         break;
1614                 }
1615                 memcpy(child, &cur_child, sizeof(struct wim_dentry));
1616                 dentry_add_child(dentry, child);
1617                 inode_add_dentry(child, child->d_inode);
1618
1619                 /* If there are children of this child, call this procedure
1620                  * recursively. */
1621                 if (child->subdir_offset != 0) {
1622                         ret = read_dentry_tree(metadata_resource,
1623                                                metadata_resource_len, child);
1624                         if (ret != 0)
1625                                 break;
1626                 }
1627
1628                 /* Advance to the offset of the next child.  Note: We need to
1629                  * advance by the TOTAL length of the dentry, not by the length
1630                  * child->length, which although it does take into account the
1631                  * padding, it DOES NOT take into account alternate stream
1632                  * entries. */
1633                 cur_offset += dentry_total_length(child);
1634         }
1635         return ret;
1636 }
1637
1638 /*
1639  * Writes a WIM dentry to an output buffer.
1640  *
1641  * @dentry:  The dentry structure.
1642  * @p:       The memory location to write the data to.
1643  * @return:  Pointer to the byte after the last byte we wrote as part of the
1644  *              dentry.
1645  */
1646 static u8 *write_dentry(const struct wim_dentry *dentry, u8 *p)
1647 {
1648         u8 *orig_p = p;
1649         const u8 *hash;
1650         const struct wim_inode *inode = dentry->d_inode;
1651
1652         /* We calculate the correct length of the dentry ourselves because the
1653          * dentry->length field may been set to an unexpected value from when we
1654          * read the dentry in (for example, there may have been unknown data
1655          * appended to the end of the dentry...) */
1656         u64 length = dentry_correct_length(dentry);
1657
1658         p = put_u64(p, length);
1659         p = put_u32(p, inode->i_attributes);
1660         p = put_u32(p, inode->i_security_id);
1661         p = put_u64(p, dentry->subdir_offset);
1662         p = put_u64(p, 0); /* unused1 */
1663         p = put_u64(p, 0); /* unused2 */
1664         p = put_u64(p, inode->i_creation_time);
1665         p = put_u64(p, inode->i_last_access_time);
1666         p = put_u64(p, inode->i_last_write_time);
1667         hash = inode_stream_hash(inode, 0);
1668         p = put_bytes(p, SHA1_HASH_SIZE, hash);
1669         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1670                 p = put_zeroes(p, 4);
1671                 p = put_u32(p, inode->i_reparse_tag);
1672                 p = put_zeroes(p, 4);
1673         } else {
1674                 u64 link_group_id;
1675                 p = put_u32(p, 0);
1676                 if (inode->i_nlink == 1)
1677                         link_group_id = 0;
1678                 else
1679                         link_group_id = inode->i_ino;
1680                 p = put_u64(p, link_group_id);
1681         }
1682         p = put_u16(p, inode->i_num_ads);
1683         p = put_u16(p, dentry->short_name_len);
1684         p = put_u16(p, dentry->file_name_len);
1685         if (dentry->file_name_len) {
1686                 p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
1687                 p = put_u16(p, 0); /* filename padding, 2 bytes. */
1688         }
1689         if (dentry->short_name) {
1690                 p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
1691                 p = put_u16(p, 0); /* short name padding, 2 bytes */
1692         }
1693
1694         /* Align to 8-byte boundary */
1695         wimlib_assert(length >= (p - orig_p) && length - (p - orig_p) <= 7);
1696         p = put_zeroes(p, length - (p - orig_p));
1697
1698         /* Write the alternate data streams, if there are any.  Please see
1699          * read_ads_entries() for comments about the format of the on-disk
1700          * alternate data stream entries. */
1701         for (u16 i = 0; i < inode->i_num_ads; i++) {
1702                 p = put_u64(p, ads_entry_total_length(&inode->i_ads_entries[i]));
1703                 p = put_u64(p, 0); /* Unused */
1704                 hash = inode_stream_hash(inode, i + 1);
1705                 p = put_bytes(p, SHA1_HASH_SIZE, hash);
1706                 p = put_u16(p, inode->i_ads_entries[i].stream_name_len);
1707                 if (inode->i_ads_entries[i].stream_name_len) {
1708                         p = put_bytes(p, inode->i_ads_entries[i].stream_name_len,
1709                                          (u8*)inode->i_ads_entries[i].stream_name);
1710                         p = put_u16(p, 0);
1711                 }
1712                 p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
1713         }
1714         wimlib_assert(p - orig_p == __dentry_total_length(dentry, length));
1715         return p;
1716 }
1717
1718 static int write_dentry_cb(struct wim_dentry *dentry, void *_p)
1719 {
1720         u8 **p = _p;
1721         *p = write_dentry(dentry, *p);
1722         return 0;
1723 }
1724
1725 static u8 *write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p);
1726
1727 static int write_dentry_tree_recursive_cb(struct wim_dentry *dentry, void *_p)
1728 {
1729         u8 **p = _p;
1730         *p = write_dentry_tree_recursive(dentry, *p);
1731         return 0;
1732 }
1733
1734 /* Recursive function that writes a dentry tree rooted at @parent, not including
1735  * @parent itself, which has already been written. */
1736 static u8 *write_dentry_tree_recursive(const struct wim_dentry *parent, u8 *p)
1737 {
1738         /* Nothing to do if this dentry has no children. */
1739         if (parent->subdir_offset == 0)
1740                 return p;
1741
1742         /* Write child dentries and end-of-directory entry.
1743          *
1744          * Note: we need to write all of this dentry's children before
1745          * recursively writing the directory trees rooted at each of the child
1746          * dentries, since the on-disk dentries for a dentry's children are
1747          * always located at consecutive positions in the metadata resource! */
1748         for_dentry_child(parent, write_dentry_cb, &p);
1749
1750         /* write end of directory entry */
1751         p = put_u64(p, 0);
1752
1753         /* Recurse on children. */
1754         for_dentry_child(parent, write_dentry_tree_recursive_cb, &p);
1755         return p;
1756 }
1757
1758 /* Writes a directory tree to the metadata resource.
1759  *
1760  * @root:       Root of the dentry tree.
1761  * @p:          Pointer to a buffer with enough space for the dentry tree.
1762  *
1763  * Returns pointer to the byte after the last byte we wrote.
1764  */
1765 u8 *write_dentry_tree(const struct wim_dentry *root, u8 *p)
1766 {
1767         DEBUG("Writing dentry tree.");
1768         wimlib_assert(dentry_is_root(root));
1769
1770         /* If we're the root dentry, we have no parent that already
1771          * wrote us, so we need to write ourselves. */
1772         p = write_dentry(root, p);
1773
1774         /* Write end of directory entry after the root dentry just to be safe;
1775          * however the root dentry obviously cannot have any siblings. */
1776         p = put_u64(p, 0);
1777
1778         /* Recursively write the rest of the dentry tree. */
1779         return write_dentry_tree_recursive(root, p);
1780 }