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