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