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