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