Alternate stream entries fixes
[wimlib] / src / dentry.c
1 /*
2  * dentry.c
3  *
4  * A dentry (directory entry) contains the metadata for a file.  In the WIM file
5  * format, the dentries are stored in the "metadata resource" section right
6  * after the security data.  Each image in the WIM file has its own metadata
7  * resource with its own security data and dentry tree.  Dentries in different
8  * images may share file resources by referring to the same lookup table
9  * entries.
10  */
11
12 /*
13  *
14  * Copyright (C) 2010 Carl Thijssen
15  * Copyright (C) 2012 Eric Biggers
16  *
17  * This file is part of wimlib, a library for working with WIM files.
18  *
19  * wimlib is free software; you can redistribute it and/or modify it under the
20  * terms of the GNU Lesser General Public License as published by the Free
21  * Software Foundation; either version 2.1 of the License, or (at your option)
22  * any later version.
23  *
24  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
25  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
26  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
27  * details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with wimlib; if not, see http://www.gnu.org/licenses/.
31  */
32
33 #include "wimlib_internal.h"
34 #include "dentry.h"
35 #include "io.h"
36 #include "timestamp.h"
37 #include "lookup_table.h"
38 #include "sha1.h"
39 #include <unistd.h>
40 #include <sys/stat.h>
41
42 /* Real length of a dentry, including the alternate data stream entries, which
43  * are not included in the dentry->length field... */
44 u64 dentry_total_length(const struct dentry *dentry)
45 {
46         u64 length = dentry->length;
47         if (dentry->num_ads) {
48                 u16 i = 0;
49                 do {
50                         length += WIM_ADS_ENTRY_DISK_SIZE + 
51                                   dentry->ads_entries[i].stream_name_len;
52                 } while (++i != dentry->num_ads);
53                 /* If there are Alternate Stream Entries, we apparently need to
54                  * round up to the NEXT 8-byte boundary, even if we are already
55                  * aligned on one... */
56                 length++;
57         }
58         /* Round to 8 byte boundary. */
59         return (length + 7) & ~7;
60 }
61
62 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
63 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
64 {
65         if (S_ISDIR(stbuf->st_mode))
66                 dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
67         else
68                 dentry->attributes = FILE_ATTRIBUTE_NORMAL;
69 }
70
71 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
72 void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf, 
73                      const struct lookup_table *table)
74 {
75         struct lookup_table_entry *lte;
76
77         if (dentry_is_directory(dentry))
78                 stbuf->st_mode = S_IFDIR | 0755;
79         else
80                 stbuf->st_mode = S_IFREG | 0644;
81
82         if (table)
83                 lte = lookup_resource(table, dentry->hash);
84         else
85                 lte = NULL;
86
87         if (lte) {
88                 stbuf->st_nlink = lte->refcnt;
89                 stbuf->st_size = lte->resource_entry.original_size;
90         } else {
91                 stbuf->st_nlink = 1;
92                 stbuf->st_size = 0;
93         }
94         stbuf->st_uid     = getuid();
95         stbuf->st_gid     = getgid();
96         stbuf->st_atime   = ms_timestamp_to_unix(dentry->last_access_time);
97         stbuf->st_mtime   = ms_timestamp_to_unix(dentry->last_write_time);
98         stbuf->st_ctime   = ms_timestamp_to_unix(dentry->creation_time);
99         stbuf->st_blocks  = (stbuf->st_size + 511) / 512;
100 }
101
102 /* Makes all timestamp fields for the dentry be the current time. */
103 void dentry_update_all_timestamps(struct dentry *dentry)
104 {
105         u64 now = get_timestamp();
106         dentry->creation_time    = now;
107         dentry->last_access_time = now;
108         dentry->last_write_time  = now;
109 }
110
111 /* 
112  * Calls a function on all directory entries in a directory tree.  It is called
113  * on a parent before its children.
114  */
115 int for_dentry_in_tree(struct dentry *root, 
116                        int (*visitor)(struct dentry*, void*), void *arg)
117 {
118         int ret;
119         struct dentry *child;
120
121         ret = visitor(root, arg);
122
123         if (ret != 0)
124                 return ret;
125
126         child = root->children;
127
128         if (!child)
129                 return 0;
130
131         do {
132                 ret = for_dentry_in_tree(child, visitor, arg);
133                 if (ret != 0)
134                         return ret;
135                 child = child->next;
136         } while (child != root->children);
137         return 0;
138 }
139
140 /* 
141  * Like for_dentry_in_tree(), but the visitor function is always called on a
142  * dentry's children before on itself.
143  */
144 int for_dentry_in_tree_depth(struct dentry *root, 
145                              int (*visitor)(struct dentry*, void*), void *arg)
146 {
147         int ret;
148         struct dentry *child;
149         struct dentry *next;
150
151         child = root->children;
152         if (child) {
153                 do {
154                         next = child->next;
155                         ret = for_dentry_in_tree_depth(child, visitor, arg);
156                         if (ret != 0)
157                                 return ret;
158                         child = next;
159                 } while (child != root->children);
160         }
161         return visitor(root, arg);
162 }
163
164 /* 
165  * Calculate the full path of @dentry, based on its parent's full path and on
166  * its UTF-8 file name. 
167  */
168 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
169 {
170         char *full_path;
171         u32 full_path_len;
172         if (dentry_is_root(dentry)) {
173                 full_path = MALLOC(2);
174                 if (!full_path)
175                         goto oom;
176                 full_path[0] = '/';
177                 full_path[1] = '\0';
178                 full_path_len = 1;
179         } else {
180                 char *parent_full_path;
181                 u32 parent_full_path_len;
182                 const struct dentry *parent = dentry->parent;
183
184                 if (dentry_is_root(parent)) {
185                         parent_full_path = "";
186                         parent_full_path_len = 0;
187                 } else {
188                         parent_full_path = parent->full_path_utf8;
189                         parent_full_path_len = parent->full_path_utf8_len;
190                 }
191
192                 full_path_len = parent_full_path_len + 1 +
193                                 dentry->file_name_utf8_len;
194                 full_path = MALLOC(full_path_len + 1);
195                 if (!full_path)
196                         goto oom;
197
198                 memcpy(full_path, parent_full_path, parent_full_path_len);
199                 full_path[parent_full_path_len] = '/';
200                 memcpy(full_path + parent_full_path_len + 1,
201                        dentry->file_name_utf8,
202                        dentry->file_name_utf8_len);
203                 full_path[full_path_len] = '\0';
204         }
205         FREE(dentry->full_path_utf8);
206         dentry->full_path_utf8 = full_path;
207         dentry->full_path_utf8_len = full_path_len;
208         return 0;
209 oom:
210         ERROR("Out of memory while calculating dentry full path");
211         return WIMLIB_ERR_NOMEM;
212 }
213
214 /* 
215  * Recursively calculates the subdir offsets for a directory tree. 
216  *
217  * @dentry:  The root of the directory tree.
218  * @subdir_offset_p:  The current subdirectory offset; i.e., the subdirectory
219  *      offset for @dentry. 
220  */
221 void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
222 {
223         struct dentry *child;
224
225         child = dentry->children;
226         dentry->subdir_offset = *subdir_offset_p;
227         if (child) {
228
229                 /* Advance the subdir offset by the amount of space the children
230                  * of this dentry take up. */
231                 do {
232                         *subdir_offset_p += dentry_total_length(child);
233                         child = child->next;
234                 } while (child != dentry->children);
235
236                 /* End-of-directory dentry on disk. */
237                 *subdir_offset_p += 8;
238
239                 /* Recursively call calculate_subdir_offsets() on all the
240                  * children. */
241                 do {
242                         calculate_subdir_offsets(child, subdir_offset_p);
243                         child = child->next;
244                 } while (child != dentry->children);
245         } else {
246                 /* On disk, childless directories have a valid subdir_offset
247                  * that points to an 8-byte end-of-directory dentry.  Regular
248                  * files have a subdir_offset of 0. */
249                 if (dentry_is_directory(dentry))
250                         *subdir_offset_p += 8;
251                 else
252                         dentry->subdir_offset = 0;
253         }
254 }
255
256
257 /* Returns the child of @dentry that has the file name @name.  
258  * Returns NULL if no child has the name. */
259 struct dentry *get_dentry_child_with_name(const struct dentry *dentry, 
260                                                         const char *name)
261 {
262         struct dentry *child;
263         size_t name_len;
264         
265         child = dentry->children;
266         if (child) {
267                 name_len = strlen(name);
268                 do {
269                         if (dentry_has_name(child, name, name_len))
270                                 return child;
271                         child = child->next;
272                 } while (child != dentry->children);
273         }
274         return NULL;
275 }
276
277 /* Retrieves the dentry that has the UTF-8 @path relative to the dentry
278  * @cur_dir.  Returns NULL if no dentry having the path is found. */
279 static struct dentry *get_dentry_relative_path(struct dentry *cur_dir, const char *path)
280 {
281         struct dentry *child;
282         size_t base_len;
283         const char *new_path;
284
285         if (*path == '\0')
286                 return cur_dir;
287
288         child = cur_dir->children;
289         if (child) {
290                 new_path = path_next_part(path, &base_len);
291                 do {
292                         if (dentry_has_name(child, path, base_len))
293                                 return get_dentry_relative_path(child, new_path);
294                         child = child->next;
295                 } while (child != cur_dir->children);
296         }
297         return NULL;
298 }
299
300 /* Returns the dentry corresponding to the UTF-8 @path, or NULL if there is no
301  * such dentry. */
302 struct dentry *get_dentry(WIMStruct *w, const char *path)
303 {
304         struct dentry *root = wim_root_dentry(w);
305         while (*path == '/')
306                 path++;
307         return get_dentry_relative_path(root, path);
308 }
309
310 /* Returns the parent directory for the @path. */
311 struct dentry *get_parent_dentry(WIMStruct *w, const char *path)
312 {
313         size_t path_len = strlen(path);
314         char buf[path_len + 1];
315
316         memcpy(buf, path, path_len + 1);
317
318         to_parent_name(buf, path_len);
319
320         return get_dentry(w, buf);
321 }
322
323 /* Prints the full path of a dentry. */
324 int print_dentry_full_path(struct dentry *dentry, void *ignore)
325 {
326         if (dentry->full_path_utf8)
327                 puts(dentry->full_path_utf8);
328         return 0;
329 }
330
331 struct file_attr_flag {
332         u32 flag;
333         const char *name;
334 };
335 struct file_attr_flag file_attr_flags[] = {
336         {FILE_ATTRIBUTE_READONLY,               "READONLY"},
337         {FILE_ATTRIBUTE_HIDDEN,         "HIDDEN"},
338         {FILE_ATTRIBUTE_SYSTEM,         "SYSTEM"},
339         {FILE_ATTRIBUTE_DIRECTORY,              "DIRECTORY"},
340         {FILE_ATTRIBUTE_ARCHIVE,                "ARCHIVE"},
341         {FILE_ATTRIBUTE_DEVICE,         "DEVICE"},
342         {FILE_ATTRIBUTE_NORMAL,         "NORMAL"},
343         {FILE_ATTRIBUTE_TEMPORARY,              "TEMPORARY"},
344         {FILE_ATTRIBUTE_SPARSE_FILE,    "SPARSE_FILE"},
345         {FILE_ATTRIBUTE_REPARSE_POINT,  "REPARSE_POINT"},
346         {FILE_ATTRIBUTE_COMPRESSED,             "COMPRESSED"},
347         {FILE_ATTRIBUTE_OFFLINE,                "OFFLINE"},
348         {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
349         {FILE_ATTRIBUTE_ENCRYPTED,              "ENCRYPTED"},
350         {FILE_ATTRIBUTE_VIRTUAL,                "VIRTUAL"},
351 };
352
353 /* Prints a directory entry.  @lookup_table is a pointer to the lookup table, or
354  * NULL if the resource entry for the dentry is not to be printed. */
355 int print_dentry(struct dentry *dentry, void *lookup_table)
356 {
357         struct lookup_table_entry *lte;
358         unsigned i;
359
360         printf("[DENTRY]\n");
361         printf("Length            = %"PRIu64"\n", dentry->length);
362         printf("Attributes        = 0x%x\n", dentry->attributes);
363         for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
364                 if (file_attr_flags[i].flag & dentry->attributes)
365                         printf("    FILE_ATTRIBUTE_%s is set\n",
366                                 file_attr_flags[i].name);
367         printf("Security ID       = %d\n", dentry->security_id);
368         printf("Subdir offset     = %"PRIu64"\n", dentry->subdir_offset);
369         /*printf("Unused1           = 0x%"PRIu64"\n", dentry->unused1);*/
370         /*printf("Unused2           = %"PRIu64"\n", dentry->unused2);*/
371         printf("Creation Time     = %"PRIu64"\n", dentry->creation_time);
372         printf("Last Access Time  = %"PRIu64"\n", dentry->last_access_time);
373         printf("Last Write Time   = %"PRIu64"\n", dentry->last_write_time);
374         printf("Creation Time     = 0x%"PRIx64"\n", dentry->creation_time);
375         printf("Hash              = "); 
376         print_hash(dentry->hash); 
377         putchar('\n');
378         printf("Reparse Tag       = 0x%"PRIx32"\n", dentry->reparse_tag);
379         printf("Hard Link Group   = 0x%"PRIx64"\n", dentry->hard_link);
380         printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
381         printf("Filename          = \"");
382         print_string(dentry->file_name, dentry->file_name_len);
383         puts("\"");
384         printf("Filename Length   = %hu\n", dentry->file_name_len);
385         printf("Filename (UTF-8)  = \"%s\"\n", dentry->file_name_utf8);
386         printf("Filename (UTF-8) Length = %hu\n", dentry->file_name_utf8_len);
387         printf("Short Name        = \"");
388         print_string(dentry->short_name, dentry->short_name_len);
389         puts("\"");
390         printf("Short Name Length = %hu\n", dentry->short_name_len);
391         printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
392         if (lookup_table) {
393                 lte = lookup_resource(lookup_table, dentry->hash);
394                 if (lte)
395                         print_lookup_table_entry(lte, NULL);
396                 else
397                         putchar('\n');
398         } else {
399                 putchar('\n');
400         }
401         for (u16 i = 0; i < dentry->num_ads; i++) {
402                 printf("[Alternate Stream Entry %u]\n", i);
403                 printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
404                 lte = lookup_resource(lookup_table, dentry->ads_entries[i].hash);
405                 if (lte)
406                         print_lookup_table_entry(lte, NULL);
407                 else
408                         putchar('\n');
409         }
410         return 0;
411 }
412
413 static inline void dentry_common_init(struct dentry *dentry)
414 {
415         memset(dentry, 0, sizeof(struct dentry));
416         dentry->refcnt = 1;
417         dentry->security_id = -1;
418 }
419
420 /* 
421  * Creates an unlinked directory entry.
422  *
423  * @name:    The base name of the new dentry.
424  * @return:  A pointer to the new dentry, or NULL if out of memory.
425  */
426 struct dentry *new_dentry(const char *name)
427 {
428         struct dentry *dentry;
429         
430         dentry = MALLOC(sizeof(struct dentry));
431         if (!dentry)
432                 return NULL;
433
434         dentry_common_init(dentry);
435         if (change_dentry_name(dentry, name) != 0) {
436                 FREE(dentry);
437                 return NULL;
438         }
439
440         dentry_update_all_timestamps(dentry);
441         dentry->next   = dentry;
442         dentry->prev   = dentry;
443         dentry->parent = dentry;
444         return dentry;
445 }
446
447
448 void free_dentry(struct dentry *dentry)
449 {
450         FREE(dentry->file_name);
451         FREE(dentry->file_name_utf8);
452         FREE(dentry->short_name);
453         FREE(dentry->full_path_utf8);
454         FREE(dentry);
455 }
456
457 /* Arguments for do_free_dentry(). */
458 struct free_dentry_args {
459         struct lookup_table *lookup_table;
460         bool lt_decrement_refcnt;
461 };
462
463 /* 
464  * This function is passed as an argument to for_dentry_in_tree_depth() in order
465  * to free a directory tree.  __args is a pointer to a `struct free_dentry_args'.
466  */
467 static int do_free_dentry(struct dentry *dentry, void *__args)
468 {
469         struct free_dentry_args *args = (struct free_dentry_args*)__args;
470
471         if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
472                 lookup_table_decrement_refcnt(args->lookup_table, 
473                                               dentry->hash);
474         }
475
476         wimlib_assert(dentry->refcnt >= 1);
477         if (--dentry->refcnt == 0)
478                 free_dentry(dentry);
479         return 0;
480 }
481
482 /* 
483  * Unlinks and frees a dentry tree.
484  *
485  * @root:               The root of the tree.
486  * @lookup_table:       The lookup table for dentries.
487  * @decrement_refcnt:   True if the dentries in the tree are to have their 
488  *                      reference counts in the lookup table decremented.
489  */
490 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table, 
491                       bool lt_decrement_refcnt)
492 {
493         if (!root || !root->parent)
494                 return;
495
496         struct free_dentry_args args;
497         args.lookup_table        = lookup_table;
498         args.lt_decrement_refcnt = lt_decrement_refcnt;
499         for_dentry_in_tree_depth(root, do_free_dentry, &args);
500 }
501
502 int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
503 {
504         dentry->refcnt++;
505         return 0;
506 }
507
508 /* 
509  * Links a dentry into the directory tree.
510  *
511  * @dentry: The dentry to link.
512  * @parent: The dentry that will be the parent of @dentry.
513  */
514 void link_dentry(struct dentry *dentry, struct dentry *parent)
515 {
516         dentry->parent = parent;
517         if (parent->children) {
518                 /* Not an only child; link to siblings. */
519                 dentry->next = parent->children;
520                 dentry->prev = parent->children->prev;
521                 dentry->next->prev = dentry;
522                 dentry->prev->next = dentry;
523         } else {
524                 /* Only child; link to parent. */
525                 parent->children = dentry;
526                 dentry->next = dentry;
527                 dentry->prev = dentry;
528         }
529 }
530
531 /* Unlink a dentry from the directory tree. */
532 void unlink_dentry(struct dentry *dentry)
533 {
534         if (dentry_is_root(dentry))
535                 return;
536         if (dentry_is_only_child(dentry)) {
537                 dentry->parent->children = NULL;
538         } else {
539                 if (dentry_is_first_sibling(dentry))
540                         dentry->parent->children = dentry->next;
541                 dentry->next->prev = dentry->prev;
542                 dentry->prev->next = dentry->next;
543         }
544 }
545
546
547 /* Recalculates the length of @dentry based on its file name length and short
548  * name length.  */
549 static inline void recalculate_dentry_size(struct dentry *dentry)
550 {
551         dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len + 
552                          2 + dentry->short_name_len;
553         for (u16 i = 0; i < dentry->num_ads; i++)
554                 dentry->length += ads_entry_length(&dentry->ads_entries[i]);
555         /* Must be multiple of 8. */
556         dentry->length = (dentry->length + 7) & ~7;
557 }
558
559 /* Changes the name of a dentry to @new_name.  Only changes the file_name and
560  * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
561  * full_path_utf8 fields.  Also recalculates its length. */
562 int change_dentry_name(struct dentry *dentry, const char *new_name)
563 {
564         size_t utf8_len;
565         size_t utf16_len;
566
567         FREE(dentry->file_name);
568
569         utf8_len = strlen(new_name);
570
571         dentry->file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
572
573         if (!dentry->file_name)
574                 return WIMLIB_ERR_NOMEM;
575
576         FREE(dentry->file_name_utf8);
577         dentry->file_name_utf8 = MALLOC(utf8_len + 1);
578         if (!dentry->file_name_utf8) {
579                 FREE(dentry->file_name);
580                 dentry->file_name = NULL;
581                 return WIMLIB_ERR_NOMEM;
582         }
583
584         dentry->file_name_len = utf16_len;
585         dentry->file_name_utf8_len = utf8_len;
586         memcpy(dentry->file_name_utf8, new_name, utf8_len + 1);
587         recalculate_dentry_size(dentry);
588         return 0;
589 }
590
591 /* Parameters for calculate_dentry_statistics(). */
592 struct image_statistics {
593         struct lookup_table *lookup_table;
594         u64 *dir_count;
595         u64 *file_count;
596         u64 *total_bytes;
597         u64 *hard_link_bytes;
598 };
599
600 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
601 {
602         struct image_statistics *stats;
603         struct lookup_table_entry *lte; 
604         
605         stats = arg;
606         lte = lookup_resource(stats->lookup_table, dentry->hash);
607
608         if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
609                 ++*stats->dir_count;
610         else
611                 ++*stats->file_count;
612
613         if (lte) {
614                 u64 size = lte->resource_entry.original_size;
615                 *stats->total_bytes += size;
616                 if (++lte->out_refcnt == 1)
617                         *stats->hard_link_bytes += size;
618         }
619         return 0;
620 }
621
622 void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *table, 
623                                    u64 *dir_count_ret, u64 *file_count_ret, 
624                                    u64 *total_bytes_ret, 
625                                    u64 *hard_link_bytes_ret)
626 {
627         struct image_statistics stats;
628         *dir_count_ret         = 0;
629         *file_count_ret        = 0;
630         *total_bytes_ret       = 0;
631         *hard_link_bytes_ret   = 0;
632         stats.lookup_table     = table;
633         stats.dir_count       = dir_count_ret;
634         stats.file_count      = file_count_ret;
635         stats.total_bytes     = total_bytes_ret;
636         stats.hard_link_bytes = hard_link_bytes_ret;
637         for_lookup_table_entry(table, zero_out_refcnts, NULL);
638         for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
639 }
640
641 static int read_ads_entries(const u8 *p, struct dentry *dentry,
642                             u64 remaining_size)
643 {
644         u16 num_ads = dentry->num_ads;
645         struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
646         int ret;
647         if (!ads_entries) {
648                 ERROR("Could not allocate memory for %"PRIu16" "
649                       "alternate data stream entries", num_ads);
650                 return WIMLIB_ERR_NOMEM;
651         }
652         DEBUG2("Reading %"PRIu16" alternate data streams "
653                "(remaining size = %"PRIu64")", num_ads, remaining_size);
654
655         for (u16 i = 0; i < num_ads; i++) {
656                 struct ads_entry *cur_entry = &ads_entries[i];
657                 u64 length;
658                 size_t utf8_len;
659                 /* Read the base stream entry, excluding the stream name. */
660                 if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
661                         ERROR("Stream entries go past end of metadata resource");
662                         ret = WIMLIB_ERR_INVALID_DENTRY;
663                         goto out_free_ads_entries;
664                 }
665                 remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
666                 /*print_string(p + 40, 10);*/
667                 /*print_byte_field(p, 50);*/
668
669                 p = get_u64(p, &length); /* ADS entry length */
670
671                 DEBUG2("ADS length = %"PRIu64, length);
672
673                 p += 8; /* Unused */
674                 p = get_bytes(p, WIM_HASH_SIZE, (u8*)cur_entry->hash);
675                 p = get_u16(p, &cur_entry->stream_name_len);
676
677                 DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
678
679                 cur_entry->stream_name = NULL;
680                 cur_entry->stream_name_utf8 = NULL;
681
682                 if (remaining_size < cur_entry->stream_name_len + 2) {
683                         ERROR("Stream entries go past end of metadata resource");
684                         ret = WIMLIB_ERR_INVALID_DENTRY;
685                         goto out_free_ads_entries;
686                 }
687                 remaining_size -= cur_entry->stream_name_len + 2;
688
689                 cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
690                 if (!cur_entry->stream_name) {
691                         ret = WIMLIB_ERR_NOMEM;
692                         goto out_free_ads_entries;
693                 }
694                 p = get_bytes(p, cur_entry->stream_name_len,
695                               (u8*)cur_entry->stream_name);
696                 p += 2; /* NULL terminator of stream name */
697                 cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
698                                                             cur_entry->stream_name_len,
699                                                             &utf8_len);
700                 cur_entry->stream_name_len_utf8 = utf8_len;
701                 print_byte_field(p, 16);
702
703                 if (!cur_entry->stream_name_utf8) {
704                         ret = WIMLIB_ERR_NOMEM;
705                         goto out_free_ads_entries;
706                 }
707         }
708         dentry->ads_entries = ads_entries;
709         return 0;
710 out_free_ads_entries:
711         for (u16 i = 0; i < num_ads; i++) {
712                 FREE(ads_entries[i].stream_name);
713                 FREE(ads_entries[i].stream_name_utf8);
714         }
715         FREE(ads_entries);
716         return ret;
717 }
718
719 /* 
720  * Reads a directory entry from the metadata resource.
721  */
722 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, 
723                 u64 offset, struct dentry *dentry)
724 {
725         const u8 *p;
726         u64 calculated_size;
727         char *file_name;
728         char *file_name_utf8;
729         char *short_name;
730         u16 short_name_len;
731         u16 file_name_len;
732         size_t file_name_utf8_len;
733         int ret;
734
735         dentry_common_init(dentry);
736
737         /*Make sure the dentry really fits into the metadata resource.*/
738         if (offset + 8 > metadata_resource_len) {
739                 ERROR("Directory entry starting at %"PRIu64" ends past the "
740                       "end of the metadata resource (size %"PRIu64")",
741                       offset, metadata_resource_len);
742                 return WIMLIB_ERR_INVALID_DENTRY;
743         }
744
745         /* Before reading the whole entry, we need to read just the length.
746          * This is because an entry of length 8 (that is, just the length field)
747          * terminates the list of sibling directory entries. */
748
749         p = get_u64(&metadata_resource[offset], &dentry->length);
750         DEBUG("length = %zu, %zu\n", dentry->length, *(u64*)(&metadata_resource[offset]));
751
752         /* A zero length field (really a length of 8, since that's how big the
753          * directory entry is...) indicates that this is the end of directory
754          * dentry.  We do not read it into memory as an actual dentry, so just
755          * return true in that case. */
756         if (dentry->length == 0)
757                 return 0;
758
759         if (offset + dentry->length >= metadata_resource_len) {
760                 ERROR("Directory entry at offset %"PRIu64" and with size "
761                       "%"PRIu64" ends past the end of the metadata resource "
762                       "(size %"PRIu64")",
763                       offset, dentry->length, metadata_resource_len);
764                 return WIMLIB_ERR_INVALID_DENTRY;
765         }
766
767         /* If it is a recognized length, read the rest of the directory entry.
768          * Note: The root directory entry has no name, and its length does not
769          * include the short name length field.  */
770         if (dentry->length < WIM_DENTRY_DISK_SIZE) {
771                 ERROR("Directory entry has invalid length of %"PRIu64" bytes",
772                       dentry->length);
773                 return WIMLIB_ERR_INVALID_DENTRY;
774         }
775
776         p = get_u32(p, &dentry->attributes);
777         p = get_u32(p, (u32*)&dentry->security_id);
778         p = get_u64(p, &dentry->subdir_offset);
779
780         /* 2 unused fields */
781         p += 2 * sizeof(u64);
782         /*p = get_u64(p, &dentry->unused1);*/
783         /*p = get_u64(p, &dentry->unused2);*/
784
785         p = get_u64(p, &dentry->creation_time);
786         p = get_u64(p, &dentry->last_access_time);
787         p = get_u64(p, &dentry->last_write_time);
788
789         p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
790         
791         /*
792          * I don't know what's going on here.  It seems like M$ screwed up the
793          * reparse points, then put the fields in the same place and didn't
794          * document it.  The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
795          * have something to do with this, but it's not documented.
796          */
797         if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
798                 /* ??? */
799                 u32 u1, u2;
800                 p = get_u32(p, &u1);
801                 /*p += 4;*/
802                 p = get_u32(p, &dentry->reparse_tag);
803                 p = get_u32(p, &u2);
804                 /*p += 4;*/
805                 dentry->hard_link = (u64)(u1) | ((u64)(u2) << 32);
806         } else {
807                 p = get_u32(p, &dentry->reparse_tag);
808                 p = get_u64(p, &dentry->hard_link);
809         }
810
811         /* By the way, the reparse_reserved field does not actually exist (at
812          * least when the file is not a reparse point) */
813
814         
815         p = get_u16(p, &dentry->num_ads);
816
817         p = get_u16(p, &short_name_len);
818         p = get_u16(p, &file_name_len);
819
820         calculated_size = WIM_DENTRY_DISK_SIZE + file_name_len + 2 +
821                           short_name_len;
822
823         if (dentry->length < calculated_size) {
824                 ERROR("Unexpected end of directory entry! (Expected "
825                       "%"PRIu64" bytes, got %"PRIu64" bytes. "
826                       "short_name_len = %hu, file_name_len = %hu)", 
827                       calculated_size, dentry->length,
828                       short_name_len, file_name_len);
829                 return WIMLIB_ERR_INVALID_DENTRY;
830         }
831
832         /* Read the filename. */
833         file_name = MALLOC(file_name_len);
834         if (!file_name) {
835                 ERROR("Failed to allocate %hu bytes for dentry file name",
836                       file_name_len);
837                 return WIMLIB_ERR_NOMEM;
838         }
839         p = get_bytes(p, file_name_len, file_name);
840
841         /* Convert filename to UTF-8. */
842         file_name_utf8 = utf16_to_utf8(file_name, file_name_len, 
843                                        &file_name_utf8_len);
844
845         if (!file_name_utf8) {
846                 ERROR("Failed to allocate memory to convert UTF-16 "
847                       "filename (%hu bytes) to UTF-8", file_name_len);
848                 ret = WIMLIB_ERR_NOMEM;
849                 goto out_free_file_name;
850         }
851
852         /* Undocumented padding between file name and short name.  This probably
853          * is supposed to be a terminating null character. */
854         p += 2;
855
856         /* Read the short filename. */
857         short_name = MALLOC(short_name_len);
858         if (!short_name) {
859                 ERROR("Failed to allocate %hu bytes for short filename",
860                       short_name_len);
861                 ret = WIMLIB_ERR_NOMEM;
862                 goto out_free_file_name_utf8;
863         }
864
865         p = get_bytes(p, short_name_len, short_name);
866
867         /* Some directory entries inexplicibly have a little over 70 bytes of
868          * extra data.  The exact amount of data seems to be 72 bytes, but it is
869          * aligned on the next 8-byte boundary.  Here's an example of the
870          * aligned data:
871          *
872          * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
873          * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
874          * 0000000000000000000000
875          *
876          * Here's one interpretation of how the data is laid out.
877          *
878          * struct unknown {
879          *      u32 field1; (always 0x00000001)
880          *      u32 field2; (always 0x40000000)
881          *      u16 field3;
882          *      u32 field4;
883          *      u32 field5;
884          *      u32 field6;
885          *      u8  data[48]; (???)
886          *      u64 reserved1; (always 0)
887          *      u64 reserved2; (always 0)
888          * };*/
889 #if 0
890         if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
891                 printf("%s: %lu / %lu (", file_name_utf8, 
892                                 calculated_size, dentry->length);
893                 print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
894                 puts(")");
895                 print_byte_field(p, dentry->length - calculated_size);
896                 putchar('\n');
897         }
898 #endif
899
900         if (dentry->num_ads != 0) {
901                 ret = read_ads_entries(p, dentry,
902                                        metadata_resource_len - offset - calculated_size);
903                 if (ret != 0)
904                         goto out_free_short_name;
905         }
906
907         dentry->file_name          = file_name;
908         dentry->file_name_utf8     = file_name_utf8;
909         dentry->short_name         = short_name;
910         dentry->file_name_len      = file_name_len;
911         dentry->file_name_utf8_len = file_name_utf8_len;
912         dentry->short_name_len     = short_name_len;
913         return 0;
914 out_free_short_name:
915         FREE(short_name);
916 out_free_file_name_utf8:
917         FREE(file_name_utf8);
918 out_free_file_name:
919         FREE(file_name);
920         return ret;
921 }
922
923 /* 
924  * Writes a dentry to an output buffer.
925  *
926  * @dentry:  The dentry structure.
927  * @p:       The memory location to write the data to.
928  * @return:  Pointer to the byte after the last byte we wrote as part of the
929  *              dentry.
930  */
931 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
932 {
933         u8 *orig_p = p;
934         memset(p, 0, dentry->length);
935         p = put_u64(p, dentry->length);
936         p = put_u32(p, dentry->attributes);
937         p = put_u32(p, dentry->security_id);
938         p = put_u64(p, dentry->subdir_offset);
939         p = put_u64(p, 0); /* unused1 */
940         p = put_u64(p, 0); /* unused2 */
941         p = put_u64(p, dentry->creation_time);
942         p = put_u64(p, dentry->last_access_time);
943         p = put_u64(p, dentry->last_write_time);
944         memcpy(p, dentry->hash, WIM_HASH_SIZE);
945         p += WIM_HASH_SIZE;
946         p = put_u32(p, dentry->reparse_tag);
947         p = put_u64(p, dentry->hard_link);
948         p = put_u16(p, dentry->num_ads); /*streams */
949         p = put_u16(p, dentry->short_name_len);
950         p = put_u16(p, dentry->file_name_len);
951         p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
952         p = put_u16(p, 0); /* filename padding, 2 bytes. */
953         p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
954         for (u16 i = 0; i < dentry->num_ads; i++) {
955                 p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
956                 p = put_u64(p, 0); /* Unused */
957                 p = put_bytes(p, WIM_HASH_SIZE, dentry->ads_entries[i].hash);
958                 p = put_u16(p, dentry->ads_entries[i].stream_name_len);
959                 p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
960                                  (u8*)dentry->ads_entries[i].stream_name);
961         }
962         return orig_p + dentry->length;
963 }
964
965 /* Recursive function that writes a dentry tree rooted at @tree, not including
966  * @tree itself, which has already been written, except in the case of the root
967  * dentry, which is written right away, along with an end-of-directory entry. */
968 u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
969 {
970         const struct dentry *child;
971
972         if (dentry_is_root(tree)) {
973                 p = write_dentry(tree, p);
974
975                 /* write end of directory entry */
976                 p = put_u64(p, 0);
977         } else {
978                 /* Nothing to do for a regular file. */
979                 if (dentry_is_regular_file(tree))
980                         return p;
981         }
982
983         /* Write child dentries and end-of-directory entry. */
984         child = tree->children;
985         if (child) {
986                 do {
987                         p = write_dentry(child, p);
988                         child = child->next;
989                 } while (child != tree->children);
990         }
991
992         /* write end of directory entry */
993         p = put_u64(p, 0);
994
995         /* Recurse on children. */
996         if (child) {
997                 do {
998                         p = write_dentry_tree(child, p);
999                         child = child->next;
1000                 } while (child != tree->children);
1001         }
1002         return p;
1003 }
1004
1005 /* Reads the children of a dentry, and all their children, ..., etc. from the
1006  * metadata resource and into the dentry tree.
1007  *
1008  * @metadata_resource:  An array that contains the uncompressed metadata
1009  *                      resource for the WIM file.
1010  * @metadata_resource_len:      The length of @metadata_resource.
1011  * @dentry:     A pointer to a struct dentry that is the root of the directory
1012  *              tree and has already been read from the metadata resource.  It
1013  *              does not need to be the real root because this procedure is
1014  *              called recursively.
1015  *
1016  * @return:     Zero on success, nonzero on failure.
1017  */
1018 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
1019                      struct dentry *dentry)
1020 {
1021         u64 cur_offset = dentry->subdir_offset;
1022         struct dentry *prev_child = NULL;
1023         struct dentry *first_child = NULL;
1024         struct dentry *child;
1025         struct dentry cur_child;
1026         int ret;
1027
1028         /* If @dentry is a regular file, nothing more needs to be done for this
1029          * branch. */
1030         if (cur_offset == 0)
1031                 return 0;
1032
1033         /* Find and read all the children of @dentry. */
1034         while (1) {
1035
1036                 /* Read next child of @dentry into @cur_child. */
1037                 ret = read_dentry(metadata_resource, metadata_resource_len, 
1038                                   cur_offset, &cur_child);
1039                 if (ret != 0)
1040                         break;
1041
1042                 /* Check for end of directory. */
1043                 if (cur_child.length == 0) {
1044                         ret = 0;
1045                         break;
1046                 }
1047
1048                 /* Not end of directory.  Allocate this child permanently and
1049                  * link it to the parent and previous child. */
1050                 child = MALLOC(sizeof(struct dentry));
1051                 if (!child) {
1052                         ERROR("Failed to allocate %zu bytes for new dentry",
1053                               sizeof(struct dentry));
1054                         ret = WIMLIB_ERR_NOMEM;
1055                         break;
1056                 }
1057                 memcpy(child, &cur_child, sizeof(struct dentry));
1058
1059                 if (prev_child) {
1060                         prev_child->next = child;
1061                         child->prev = prev_child;
1062                 } else {
1063                         first_child = child;
1064                 }
1065
1066                 child->parent = dentry;
1067                 prev_child = child;
1068
1069                 /* If there are children of this child, call this procedure
1070                  * recursively. */
1071                 if (child->subdir_offset != 0) {
1072                         ret = read_dentry_tree(metadata_resource, 
1073                                                metadata_resource_len, child);
1074                         if (ret != 0)
1075                                 break;
1076                 }
1077
1078                 /* Advance to the offset of the next child. */
1079                 cur_offset += dentry_total_length(child);
1080         }
1081
1082         /* Link last child to first one, and set parent's
1083          * children pointer to the first child.  */
1084         if (prev_child) {
1085                 prev_child->next = first_child;
1086                 first_child->prev = prev_child;
1087         }
1088         dentry->children = first_child;
1089         return ret;
1090 }