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