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