]> wimlib.net Git - wimlib/blob - src/lookup_table.c
build_dentry_tree(): Allow root to be symlink
[wimlib] / src / lookup_table.c
1 /*
2  * lookup_table.c
3  *
4  * Lookup table, implemented as a hash table, that maps dentries to file
5  * resources.
6  */
7
8 /*
9  * Copyright (C) 2012 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #include "wimlib_internal.h"
28 #include "lookup_table.h"
29 #include "buffer_io.h"
30 #include <errno.h>
31
32 #ifdef WITH_FUSE
33 #include <unistd.h>
34 #endif
35
36 struct lookup_table *new_lookup_table(size_t capacity)
37 {
38         struct lookup_table *table;
39         struct hlist_head *array;
40
41         table = MALLOC(sizeof(struct lookup_table));
42         if (table) {
43                 array = CALLOC(capacity, sizeof(array[0]));
44                 if (array) {
45                         table->num_entries = 0;
46                         table->capacity = capacity;
47                         table->array = array;
48                 } else {
49                         FREE(table);
50                         table = NULL;
51                         ERROR("Failed to allocate memory for lookup table with capacity %zu",
52                               capacity);
53                 }
54         }
55         return table;
56 }
57
58 struct lookup_table_entry *new_lookup_table_entry()
59 {
60         struct lookup_table_entry *lte;
61
62         lte = CALLOC(1, sizeof(struct lookup_table_entry));
63         if (lte) {
64                 lte->part_number  = 1;
65                 lte->refcnt       = 1;
66         } else {
67                 ERROR("Out of memory (tried to allocate %zu bytes for "
68                       "lookup table entry)",
69                       sizeof(struct lookup_table_entry));
70         }
71         return lte;
72 }
73
74 struct lookup_table_entry *
75 clone_lookup_table_entry(const struct lookup_table_entry *old)
76 {
77         struct lookup_table_entry *new;
78
79         new = MALLOC(sizeof(*new));
80         if (!new)
81                 return NULL;
82
83         memcpy(new, old, sizeof(*old));
84         new->extracted_file = NULL;
85         switch (new->resource_location) {
86         case RESOURCE_IN_STAGING_FILE:
87         case RESOURCE_IN_FILE_ON_DISK:
88                 wimlib_assert((void*)&old->file_on_disk ==
89                               (void*)&old->staging_file_name);
90                 new->staging_file_name = STRDUP(old->staging_file_name);
91                 if (!new->staging_file_name)
92                         goto out_free;
93                 break;
94         case RESOURCE_IN_ATTACHED_BUFFER:
95                 new->attached_buffer = MALLOC(wim_resource_size(old));
96                 if (!new->attached_buffer)
97                         goto out_free;
98                 memcpy(new->attached_buffer, old->attached_buffer,
99                        wim_resource_size(old));
100                 break;
101 #ifdef WITH_NTFS_3G
102         case RESOURCE_IN_NTFS_VOLUME:
103                 if (old->ntfs_loc) {
104                         struct ntfs_location *loc;
105                         loc = MALLOC(sizeof(*loc));
106                         if (!loc)
107                                 goto out_free;
108                         memcpy(loc, old->ntfs_loc, sizeof(*loc));
109                         loc->path_utf8 = NULL;
110                         loc->stream_name_utf16 = NULL;
111                         new->ntfs_loc = loc;
112                         loc->path_utf8 = STRDUP(old->ntfs_loc->path_utf8);
113                         if (!loc->path_utf8)
114                                 goto out_free;
115                         loc->stream_name_utf16 = MALLOC(loc->stream_name_utf16_num_chars * 2);
116                         if (!loc->stream_name_utf16)
117                                 goto out_free;
118                         memcpy(loc->stream_name_utf16,
119                                old->ntfs_loc->stream_name_utf16,
120                                loc->stream_name_utf16_num_chars * 2);
121                 }
122                 break;
123 #endif
124         default:
125                 break;
126         }
127         return new;
128 out_free:
129         free_lookup_table_entry(new);
130         return NULL;
131 }
132
133 void free_lookup_table_entry(struct lookup_table_entry *lte)
134 {
135         if (lte) {
136                 switch (lte->resource_location) {
137                 case RESOURCE_IN_STAGING_FILE:
138                 case RESOURCE_IN_ATTACHED_BUFFER:
139                 case RESOURCE_IN_FILE_ON_DISK:
140                         wimlib_assert(((void*)&lte->file_on_disk ==
141                                       (void*)&lte->staging_file_name)
142                                       && ((void*)&lte->file_on_disk ==
143                                       (void*)&lte->attached_buffer));
144                         FREE(lte->file_on_disk);
145                         break;
146 #ifdef WITH_NTFS_3G
147                 case RESOURCE_IN_NTFS_VOLUME:
148                         if (lte->ntfs_loc) {
149                                 FREE(lte->ntfs_loc->path_utf8);
150                                 FREE(lte->ntfs_loc->stream_name_utf16);
151                                 FREE(lte->ntfs_loc);
152                         }
153                         break;
154 #endif
155                 default:
156                         break;
157                 }
158                 FREE(lte);
159         }
160 }
161
162 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
163                                       void *ignore)
164 {
165         free_lookup_table_entry(entry);
166         return 0;
167 }
168
169
170 void free_lookup_table(struct lookup_table *table)
171 {
172         DEBUG2("Freeing lookup table");
173         if (table) {
174                 if (table->array) {
175                         for_lookup_table_entry(table,
176                                                do_free_lookup_table_entry,
177                                                NULL);
178                         FREE(table->array);
179                 }
180                 FREE(table);
181         }
182 }
183
184 /*
185  * Inserts an entry into the lookup table.
186  *
187  * @table:      A pointer to the lookup table.
188  * @entry:      A pointer to the entry to insert.
189  */
190 void lookup_table_insert(struct lookup_table *table,
191                          struct lookup_table_entry *lte)
192 {
193         size_t i = lte->hash_short % table->capacity;
194         hlist_add_head(&lte->hash_list, &table->array[i]);
195
196         /* XXX Make the table grow when too many entries have been inserted. */
197         table->num_entries++;
198 }
199
200 static void finalize_lte(struct lookup_table_entry *lte)
201 {
202         #ifdef WITH_FUSE
203         if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
204                 unlink(lte->staging_file_name);
205                 wimlib_assert(lte->staging_list.next);
206                 wimlib_assert(lte->staging_list.prev);
207                 list_del(&lte->staging_list);
208         }
209         #endif
210         free_lookup_table_entry(lte);
211 }
212
213 /* Decrements the reference count for the lookup table entry @lte.  If its
214  * reference count reaches 0, it is unlinked from the lookup table.  If,
215  * furthermore, the entry has no opened file descriptors associated with it, the
216  * entry is freed.  */
217 void lte_decrement_refcnt(struct lookup_table_entry *lte,
218                           struct lookup_table *table)
219 {
220         wimlib_assert(lte != NULL);
221         wimlib_assert(lte->refcnt != 0);
222         if (--lte->refcnt == 0) {
223                 lookup_table_unlink(table, lte);
224         #ifdef WITH_FUSE
225                 if (lte->num_opened_fds == 0)
226         #endif
227                         finalize_lte(lte);
228         }
229 }
230
231 #ifdef WITH_FUSE
232 void lte_decrement_num_opened_fds(struct lookup_table_entry *lte)
233 {
234         wimlib_assert(lte != NULL);
235         if (lte->num_opened_fds != 0) {
236                 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
237                         finalize_lte(lte);
238         }
239 }
240 #endif
241
242 /*
243  * Calls a function on all the entries in the lookup table.  Stop early and
244  * return nonzero if any call to the function returns nonzero.
245  */
246 int for_lookup_table_entry(struct lookup_table *table,
247                            int (*visitor)(struct lookup_table_entry *, void *),
248                            void *arg)
249 {
250         struct lookup_table_entry *lte;
251         struct hlist_node *pos, *tmp;
252         int ret;
253
254         for (size_t i = 0; i < table->capacity; i++) {
255                 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
256                                           hash_list)
257                 {
258                         ret = visitor(lte, arg);
259                         if (ret != 0)
260                                 return ret;
261                 }
262         }
263         return 0;
264 }
265
266
267 /*
268  * Reads the lookup table from a WIM file.
269  */
270 int read_lookup_table(WIMStruct *w)
271 {
272         u64 num_entries;
273         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
274         int ret;
275         struct lookup_table *table;
276         struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
277
278         if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
279                 ERROR("Didn't expect a compressed lookup table!");
280                 ERROR("Ask the author to implement support for this.");
281                 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
282         }
283
284         DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
285               w->hdr.lookup_table_res_entry.offset,
286               w->hdr.lookup_table_res_entry.original_size);
287
288         if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
289         {
290                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
291                                  "lookup table",
292                                  w->hdr.lookup_table_res_entry.offset);
293                 return WIMLIB_ERR_READ;
294         }
295
296         num_entries = w->hdr.lookup_table_res_entry.original_size /
297                       WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
298         table = new_lookup_table(num_entries * 2 + 1);
299         if (!table)
300                 return WIMLIB_ERR_NOMEM;
301
302         while (num_entries--) {
303                 const u8 *p;
304
305                 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
306                         if (feof(w->fp)) {
307                                 ERROR("Unexpected EOF in WIM lookup table!");
308                         } else {
309                                 ERROR_WITH_ERRNO("Error reading WIM lookup "
310                                                  "table");
311                         }
312                         ret = WIMLIB_ERR_READ;
313                         goto out;
314                 }
315                 cur_entry = new_lookup_table_entry();
316                 if (!cur_entry) {
317                         ret = WIMLIB_ERR_NOMEM;
318                         goto out;
319                 }
320                 cur_entry->wim = w;
321                 cur_entry->resource_location = RESOURCE_IN_WIM;
322
323                 p = get_resource_entry(buf, &cur_entry->resource_entry);
324                 p = get_u16(p, &cur_entry->part_number);
325                 p = get_u32(p, &cur_entry->refcnt);
326                 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
327
328                 if (cur_entry->part_number != w->hdr.part_number) {
329                         ERROR("A lookup table entry in part %hu of the WIM "
330                               "points to part %hu",
331                               w->hdr.part_number, cur_entry->part_number);
332                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
333                         goto out_free_cur_entry;
334                 }
335
336                 if (is_zero_hash(cur_entry->hash)) {
337                         ERROR("The WIM lookup table contains an entry with a "
338                               "SHA1 message digest of all 0's");
339                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
340                         goto out_free_cur_entry;
341                 }
342
343                 /* Ordinarily, no two streams should share the same SHA1 message
344                  * digest.  However, this constraint can be broken for metadata
345                  * resources--- two identical images will have the same metadata
346                  * resource, but their lookup table entries are not shared. */
347                 duplicate_entry = __lookup_resource(table, cur_entry->hash);
348                 if (duplicate_entry
349                     && !((duplicate_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
350                           && cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA))
351                 {
352                         ERROR("The WIM lookup table contains two entries with the "
353                               "same SHA1 message digest!");
354                         ERROR("The first entry is:");
355                         print_lookup_table_entry(duplicate_entry);
356                         ERROR("The second entry is:");
357                         print_lookup_table_entry(cur_entry);
358                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
359                         goto out_free_cur_entry;
360                 }
361
362                 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
363                     && (cur_entry->resource_entry.size !=
364                         cur_entry->resource_entry.original_size))
365                 {
366                         ERROR("Found uncompressed resource with original size "
367                               "not the same as compressed size");
368                         ERROR("The lookup table entry for the resource is as follows:");
369                         print_lookup_table_entry(cur_entry);
370                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
371                         goto out_free_cur_entry;
372                 }
373                 if ((cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
374                     && cur_entry->refcnt != 1)
375                 {
376                         ERROR("Found metadata resource with refcnt != 1:");
377                         print_lookup_table_entry(cur_entry);
378                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
379                         goto out_free_cur_entry;
380                 }
381                 lookup_table_insert(table, cur_entry);
382
383         }
384         DEBUG("Done reading lookup table.");
385         w->lookup_table = table;
386         return 0;
387 out_free_cur_entry:
388         FREE(cur_entry);
389 out:
390         free_lookup_table(table);
391         return ret;
392 }
393
394
395 /*
396  * Writes a lookup table entry to the output file.
397  */
398 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
399 {
400         FILE *out;
401         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
402         u8 *p;
403
404         out = __out;
405
406         /* Don't write entries that have not had file resources or metadata
407          * resources written for them. */
408         if (lte->out_refcnt == 0)
409                 return 0;
410
411         if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
412                 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
413                       ftello(out), lte->output_resource_entry.original_size);
414
415         p = put_resource_entry(buf, &lte->output_resource_entry);
416         p = put_u16(p, lte->part_number);
417         p = put_u32(p, lte->out_refcnt);
418         p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
419         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
420                 ERROR_WITH_ERRNO("Failed to write lookup table entry");
421                 return WIMLIB_ERR_WRITE;
422         }
423         return 0;
424 }
425
426 /* Writes the lookup table to the output file. */
427 int write_lookup_table(struct lookup_table *table, FILE *out,
428                        struct resource_entry *out_res_entry)
429 {
430         off_t start_offset, end_offset;
431         int ret;
432
433         start_offset = ftello(out);
434         if (start_offset == -1)
435                 return WIMLIB_ERR_WRITE;
436
437         ret = for_lookup_table_entry(table, write_lookup_table_entry, out);
438         if (ret != 0)
439                 return ret;
440
441         end_offset = ftello(out);
442         if (end_offset == -1)
443                 return WIMLIB_ERR_WRITE;
444
445         out_res_entry->offset        = start_offset;
446         out_res_entry->size          = end_offset - start_offset;
447         out_res_entry->original_size = end_offset - start_offset;
448         out_res_entry->flags         = WIM_RESHDR_FLAG_METADATA;
449
450         return 0;
451 }
452
453
454 int lte_zero_real_refcnt(struct lookup_table_entry *lte, void *ignore)
455 {
456         lte->real_refcnt = 0;
457         return 0;
458 }
459
460 int lte_zero_out_refcnt(struct lookup_table_entry *lte, void *ignore)
461 {
462         lte->out_refcnt = 0;
463         return 0;
464 }
465
466 int lte_zero_extracted_file(struct lookup_table_entry *lte, void *ignore)
467 {
468         lte->extracted_file = NULL;
469         return 0;
470 }
471
472 int lte_free_extracted_file(struct lookup_table_entry *lte, void *ignore)
473 {
474         if (lte->extracted_file != NULL) {
475                 FREE(lte->extracted_file);
476                 lte->extracted_file = NULL;
477         }
478         return 0;
479 }
480
481 void print_lookup_table_entry(const struct lookup_table_entry *lte)
482 {
483         if (!lte) {
484                 putchar('\n');
485                 return;
486         }
487         printf("Offset            = %"PRIu64" bytes\n",
488                lte->resource_entry.offset);
489         printf("Size              = %"PRIu64" bytes\n",
490                (u64)lte->resource_entry.size);
491         printf("Original size     = %"PRIu64" bytes\n",
492                lte->resource_entry.original_size);
493         printf("Part Number       = %hu\n", lte->part_number);
494         printf("Reference Count   = %u\n", lte->refcnt);
495         printf("Hash              = 0x");
496         print_hash(lte->hash);
497         putchar('\n');
498         printf("Flags             = ");
499         u8 flags = lte->resource_entry.flags;
500         if (flags & WIM_RESHDR_FLAG_COMPRESSED)
501                 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
502         if (flags & WIM_RESHDR_FLAG_FREE)
503                 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
504         if (flags & WIM_RESHDR_FLAG_METADATA)
505                 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
506         if (flags & WIM_RESHDR_FLAG_SPANNED)
507                 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
508         putchar('\n');
509         switch (lte->resource_location) {
510         case RESOURCE_IN_WIM:
511                 if (lte->wim->filename) {
512                         printf("WIM file          = `%s'\n",
513                                lte->wim->filename);
514                 }
515                 break;
516         case RESOURCE_IN_FILE_ON_DISK:
517                 printf("File on Disk      = `%s'\n", lte->file_on_disk);
518                 break;
519         case RESOURCE_IN_STAGING_FILE:
520                 printf("Staging File      = `%s'\n", lte->staging_file_name);
521                 break;
522         default:
523                 break;
524         }
525         putchar('\n');
526 }
527
528 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
529                                        void *ignore)
530 {
531         print_lookup_table_entry(lte);
532         return 0;
533 }
534
535 /*
536  * Prints the lookup table of a WIM file.
537  */
538 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
539 {
540         for_lookup_table_entry(w->lookup_table,
541                                do_print_lookup_table_entry,
542                                NULL);
543 }
544
545 /*
546  * Looks up an entry in the lookup table.
547  */
548 struct lookup_table_entry *
549 __lookup_resource(const struct lookup_table *table, const u8 hash[])
550 {
551         size_t i;
552         struct lookup_table_entry *lte;
553         struct hlist_node *pos;
554
555         wimlib_assert(table != NULL);
556         wimlib_assert(hash != NULL);
557
558         i = *(size_t*)hash % table->capacity;
559         hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
560                 if (hashes_equal(hash, lte->hash))
561                         return lte;
562         return NULL;
563 }
564
565 #ifdef WITH_FUSE
566 /*
567  * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
568  * given a path name.
569  *
570  * This is only for pre-resolved inodes.
571  */
572 int lookup_resource(WIMStruct *w, const char *path,
573                     int lookup_flags,
574                     struct dentry **dentry_ret,
575                     struct lookup_table_entry **lte_ret,
576                     u16 *stream_idx_ret)
577 {
578         struct dentry *dentry;
579         struct lookup_table_entry *lte;
580         u16 stream_idx;
581         const char *stream_name = NULL;
582         struct inode *inode;
583         char *p = NULL;
584
585         if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
586                 stream_name = path_stream_name(path);
587                 if (stream_name) {
588                         p = (char*)stream_name - 1;
589                         *p = '\0';
590                 }
591         }
592
593         dentry = get_dentry(w, path);
594         if (p)
595                 *p = ':';
596         if (!dentry)
597                 return -ENOENT;
598
599         inode = dentry->d_inode;
600
601         wimlib_assert(inode->resolved);
602
603         if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
604               && inode_is_directory(inode))
605                 return -EISDIR;
606
607         if (stream_name) {
608                 struct ads_entry *ads_entry;
609                 u16 ads_idx;
610                 ads_entry = inode_get_ads_entry(inode, stream_name,
611                                                 &ads_idx);
612                 if (ads_entry) {
613                         stream_idx = ads_idx + 1;
614                         lte = ads_entry->lte;
615                         goto out;
616                 } else {
617                         return -ENOENT;
618                 }
619         } else {
620                 lte = inode->lte;
621                 stream_idx = 0;
622         }
623 out:
624         if (dentry_ret)
625                 *dentry_ret = dentry;
626         if (lte_ret)
627                 *lte_ret = lte;
628         if (stream_idx_ret)
629                 *stream_idx_ret = stream_idx;
630         return 0;
631 }
632 #endif
633
634 void inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
635 {
636         struct lookup_table_entry *lte;
637
638         wimlib_assert(!inode->resolved);
639
640         /* Resolve the default file stream */
641         lte = __lookup_resource(table, inode->hash);
642         inode->lte = lte;
643         inode->resolved = true;
644
645         /* Resolve the alternate data streams */
646         for (u16 i = 0; i < inode->num_ads; i++) {
647                 struct ads_entry *cur_entry = &inode->ads_entries[i];
648                 lte = __lookup_resource(table, cur_entry->hash);
649                 cur_entry->lte = lte;
650         }
651 }
652
653 static void inode_unresolve_ltes(struct inode *inode)
654 {
655         wimlib_assert(inode->resolved);
656         if (inode->lte)
657                 copy_hash(inode->hash, inode->lte->hash);
658         else
659                 zero_out_hash(inode->hash);
660
661         for (u16 i = 0; i < inode->num_ads; i++) {
662                 if (inode->ads_entries[i].lte)
663                         copy_hash(inode->ads_entries[i].hash,
664                                   inode->ads_entries[i].lte->hash);
665                 else
666                         zero_out_hash(inode->ads_entries[i].hash);
667         }
668         inode->resolved = false;
669 }
670
671 /* Resolve a dentry's lookup table entries
672  *
673  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
674  * lookup table) with pointers directly to the lookup table entries.  A circular
675  * linked list of streams sharing the same lookup table entry is created.
676  *
677  * This function always succeeds; unresolved lookup table entries are given a
678  * NULL pointer.
679  */
680 int dentry_resolve_ltes(struct dentry *dentry, void *table)
681 {
682         if (!dentry->d_inode->resolved)
683                 inode_resolve_ltes(dentry->d_inode, table);
684         return 0;
685 }
686
687 int dentry_unresolve_ltes(struct dentry *dentry, void *ignore)
688 {
689         if (dentry->d_inode->resolved)
690                 inode_unresolve_ltes(dentry->d_inode);
691         return 0;
692 }
693
694 /*
695  * Returns the lookup table entry for stream @stream_idx of the inode, where
696  * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
697  * corresponds to an alternate data stream.
698  *
699  * This works for both resolved and un-resolved dentries.
700  */
701 struct lookup_table_entry *
702 inode_stream_lte(const struct inode *inode, unsigned stream_idx,
703                  const struct lookup_table *table)
704 {
705         if (inode->resolved)
706                 return inode_stream_lte_resolved(inode, stream_idx);
707         else
708                 return inode_stream_lte_unresolved(inode, stream_idx, table);
709 }
710
711
712 /* Return the lookup table entry for the unnamed data stream of an inode, or
713  * NULL if there is none.
714  *
715  * You'd think this would be easier than it actually is, since the unnamed data
716  * stream should be the one referenced from the inode itself.  Alas, if there
717  * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
718  * data stream in one of the alternate data streams instead of inside the WIM
719  * dentry itself.  So we need to check the alternate data streams too.
720  *
721  * Also, note that a dentry may appear to have more than one unnamed stream, but
722  * if the SHA1 message digest is all 0's then the corresponding stream does not
723  * really "count" (this is the case for the inode's own file stream when the
724  * file stream that should be there is actually in one of the alternate stream
725  * entries.).  This is despite the fact that we may need to extract such a
726  * missing entry as an empty file or empty named data stream.
727  */
728 struct lookup_table_entry *
729 inode_unnamed_lte(const struct inode *inode,
730                   const struct lookup_table *table)
731 {
732         if (inode->resolved)
733                 return inode_unnamed_lte_resolved(inode);
734         else
735                 return inode_unnamed_lte_unresolved(inode, table);
736 }
737
738 static int lte_add_stream_size(struct lookup_table_entry *lte,
739                                void *total_bytes_p)
740 {
741         *(u64*)total_bytes_p += lte->resource_entry.size;
742         return 0;
743 }
744
745 u64 lookup_table_total_stream_size(struct lookup_table *table)
746 {
747         u64 total_size = 0;
748         for_lookup_table_entry(table, lte_add_stream_size, &total_size);
749         return total_size;
750 }