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