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