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