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