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