0ebaf61eb503848ff6d5262e7751013b34715056
[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 #include <stdlib.h>
32
33 #ifdef WITH_FUSE
34 #include <unistd.h>
35 #endif
36
37 struct wim_lookup_table *
38 new_lookup_table(size_t capacity)
39 {
40         struct wim_lookup_table *table;
41         struct hlist_head *array;
42
43         table = CALLOC(1, sizeof(struct wim_lookup_table));
44         if (table) {
45                 array = CALLOC(capacity, sizeof(array[0]));
46                 if (array) {
47                         table->num_entries = 0;
48                         table->capacity = capacity;
49                         table->array = array;
50                 } else {
51                         FREE(table);
52                         table = NULL;
53                         ERROR("Failed to allocate memory for lookup table with capacity %zu",
54                               capacity);
55                 }
56         }
57         return table;
58 }
59
60 struct wim_lookup_table_entry *
61 new_lookup_table_entry()
62 {
63         struct wim_lookup_table_entry *lte;
64
65         lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
66         if (lte) {
67                 lte->part_number  = 1;
68                 lte->refcnt       = 1;
69         } else {
70                 ERROR("Out of memory (tried to allocate %zu bytes for "
71                       "lookup table entry)",
72                       sizeof(struct wim_lookup_table_entry));
73         }
74         return lte;
75 }
76
77 struct wim_lookup_table_entry *
78 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
79 {
80         struct wim_lookup_table_entry *new;
81
82         new = MALLOC(sizeof(*new));
83         if (!new)
84                 return NULL;
85
86         memcpy(new, old, sizeof(*old));
87         new->extracted_file = NULL;
88         switch (new->resource_location) {
89 #ifdef __WIN32__
90         case RESOURCE_WIN32:
91         case RESOURCE_WIN32_ENCRYPTED:
92 #endif
93 #ifdef WITH_FUSE
94         case RESOURCE_IN_STAGING_FILE:
95 #endif
96         case RESOURCE_IN_FILE_ON_DISK:
97                 BUILD_BUG_ON((void*)&old->file_on_disk !=
98                              (void*)&old->staging_file_name);
99                 new->staging_file_name = TSTRDUP(old->staging_file_name);
100                 if (!new->staging_file_name)
101                         goto out_free;
102                 break;
103         case RESOURCE_IN_ATTACHED_BUFFER:
104                 new->attached_buffer = MALLOC(wim_resource_size(old));
105                 if (!new->attached_buffer)
106                         goto out_free;
107                 memcpy(new->attached_buffer, old->attached_buffer,
108                        wim_resource_size(old));
109                 break;
110 #ifdef WITH_NTFS_3G
111         case RESOURCE_IN_NTFS_VOLUME:
112                 if (old->ntfs_loc) {
113                         struct ntfs_location *loc;
114                         loc = MALLOC(sizeof(*loc));
115                         if (!loc)
116                                 goto out_free;
117                         memcpy(loc, old->ntfs_loc, sizeof(*loc));
118                         loc->path = NULL;
119                         loc->stream_name = NULL;
120                         new->ntfs_loc = loc;
121                         loc->path = STRDUP(old->ntfs_loc->path);
122                         if (!loc->path)
123                                 goto out_free;
124                         loc->stream_name = MALLOC((loc->stream_name_nchars + 1) * 2);
125                         if (!loc->stream_name)
126                                 goto out_free;
127                         memcpy(loc->stream_name,
128                                old->ntfs_loc->stream_name,
129                                (loc->stream_name_nchars + 1) * 2);
130                 }
131                 break;
132 #endif
133         default:
134                 break;
135         }
136         return new;
137 out_free:
138         free_lookup_table_entry(new);
139         return NULL;
140 }
141
142 void
143 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
144 {
145         if (lte) {
146                 switch (lte->resource_location) {
147                 case RESOURCE_IN_STAGING_FILE:
148                 case RESOURCE_IN_ATTACHED_BUFFER:
149                 case RESOURCE_IN_FILE_ON_DISK:
150 #ifdef __WIN32__
151                 case RESOURCE_WIN32:
152 #endif
153                         BUILD_BUG_ON((void*)&lte->file_on_disk !=
154                                      (void*)&lte->staging_file_name);
155                         BUILD_BUG_ON((void*)&lte->file_on_disk !=
156                                      (void*)&lte->attached_buffer);
157                         FREE(lte->file_on_disk);
158                         break;
159 #ifdef WITH_NTFS_3G
160                 case RESOURCE_IN_NTFS_VOLUME:
161                         if (lte->ntfs_loc) {
162                                 FREE(lte->ntfs_loc->path);
163                                 FREE(lte->ntfs_loc->stream_name);
164                                 FREE(lte->ntfs_loc);
165                         }
166                         break;
167 #endif
168                 default:
169                         break;
170                 }
171                 FREE(lte);
172         }
173 }
174
175 static int
176 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
177 {
178         free_lookup_table_entry(entry);
179         return 0;
180 }
181
182
183 void
184 free_lookup_table(struct wim_lookup_table *table)
185 {
186         DEBUG2("Freeing lookup table");
187         if (table) {
188                 if (table->array) {
189                         for_lookup_table_entry(table,
190                                                do_free_lookup_table_entry,
191                                                NULL);
192                         FREE(table->array);
193                 }
194                 FREE(table);
195         }
196 }
197
198 /*
199  * Inserts an entry into the lookup table.
200  *
201  * @table:      A pointer to the lookup table.
202  * @lte:        A pointer to the entry to insert.
203  */
204 void
205 lookup_table_insert(struct wim_lookup_table *table,
206                     struct wim_lookup_table_entry *lte)
207 {
208         size_t i = lte->hash_short % table->capacity;
209         hlist_add_head(&lte->hash_list, &table->array[i]);
210
211         /* XXX Make the table grow when too many entries have been inserted. */
212         table->num_entries++;
213 }
214
215 static void
216 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->unhashed_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
232 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
233                      struct wim_lookup_table *table)
234 {
235         wimlib_assert(lte != NULL);
236         wimlib_assert(lte->refcnt != 0);
237         if (--lte->refcnt == 0) {
238                 if (!lte->unhashed)
239                         lookup_table_unlink(table, lte);
240         #ifdef WITH_FUSE
241                 if (lte->num_opened_fds == 0)
242         #endif
243                         finalize_lte(lte);
244         }
245 }
246
247 #ifdef WITH_FUSE
248 void
249 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
250 {
251         if (lte->num_opened_fds != 0)
252                 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
253                         finalize_lte(lte);
254 }
255 #endif
256
257 /* Calls a function on all the entries in the WIM lookup table.  Stop early and
258  * return nonzero if any call to the function returns nonzero. */
259 int
260 for_lookup_table_entry(struct wim_lookup_table *table,
261                        int (*visitor)(struct wim_lookup_table_entry *, void *),
262                        void *arg)
263 {
264         struct wim_lookup_table_entry *lte;
265         struct hlist_node *pos, *tmp;
266         int ret;
267
268         for (size_t i = 0; i < table->capacity; i++) {
269                 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
270                                           hash_list)
271                 {
272                         wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
273                         ret = visitor(lte, arg);
274                         if (ret)
275                                 return ret;
276                 }
277         }
278         return 0;
279 }
280
281 int
282 cmp_streams_by_wim_position(const void *p1, const void *p2)
283 {
284         const struct wim_lookup_table_entry *lte1, *lte2;
285         lte1 = *(const struct wim_lookup_table_entry**)p1;
286         lte2 = *(const struct wim_lookup_table_entry**)p2;
287         if (lte1->resource_entry.offset < lte2->resource_entry.offset)
288                 return -1;
289         else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
290                 return 1;
291         else
292                 return 0;
293 }
294
295
296 static int
297 add_lte_to_array(struct wim_lookup_table_entry *lte,
298                  void *_pp)
299 {
300         struct wim_lookup_table_entry ***pp = _pp;
301         *(*pp)++ = lte;
302         return 0;
303 }
304
305 /* Iterate through the lookup table entries, but first sort them by stream
306  * offset in the WIM.  Caution: this is intended to be used when the stream
307  * offset field has actually been set. */
308 int
309 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
310                                   int (*visitor)(struct wim_lookup_table_entry *,
311                                                  void *),
312                                   void *arg)
313 {
314         struct wim_lookup_table_entry **lte_array, **p;
315         size_t num_streams = table->num_entries;
316         int ret;
317
318         lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
319         if (!lte_array)
320                 return WIMLIB_ERR_NOMEM;
321         p = lte_array;
322         for_lookup_table_entry(table, add_lte_to_array, &p);
323
324         wimlib_assert(p == lte_array + num_streams);
325
326         qsort(lte_array, num_streams, sizeof(lte_array[0]),
327               cmp_streams_by_wim_position);
328         ret = 0;
329         for (size_t i = 0; i < num_streams; i++) {
330                 ret = visitor(lte_array[i], arg);
331                 if (ret)
332                         break;
333         }
334         FREE(lte_array);
335         return ret;
336 }
337
338 /*
339  * Reads the lookup table from a WIM file.
340  *
341  * Saves lookup table entries for non-metadata streams in a hash table, and
342  * saves the metadata entry for each image in a special per-image location (the
343  * image_metadata array).
344  */
345 int
346 read_lookup_table(WIMStruct *w)
347 {
348         u64 num_entries;
349         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
350         int ret;
351         struct wim_lookup_table *table;
352         struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
353
354         if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
355                 ERROR("Didn't expect a compressed lookup table!");
356                 ERROR("Ask the author to implement support for this.");
357                 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
358         }
359
360         DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
361               w->hdr.lookup_table_res_entry.offset,
362               w->hdr.lookup_table_res_entry.original_size);
363
364         if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
365         {
366                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
367                                  "lookup table",
368                                  w->hdr.lookup_table_res_entry.offset);
369                 return WIMLIB_ERR_READ;
370         }
371
372         num_entries = w->hdr.lookup_table_res_entry.original_size /
373                       WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
374         table = new_lookup_table(num_entries * 2 + 1);
375         if (!table)
376                 return WIMLIB_ERR_NOMEM;
377
378         w->current_image = 0;
379         while (num_entries--) {
380                 const u8 *p;
381
382                 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
383                         if (feof(w->fp)) {
384                                 ERROR("Unexpected EOF in WIM lookup table!");
385                         } else {
386                                 ERROR_WITH_ERRNO("Error reading WIM lookup "
387                                                  "table");
388                         }
389                         ret = WIMLIB_ERR_READ;
390                         goto out_free_lookup_table;
391                 }
392                 cur_entry = new_lookup_table_entry();
393                 if (!cur_entry) {
394                         ret = WIMLIB_ERR_NOMEM;
395                         goto out_free_lookup_table;
396                 }
397
398                 cur_entry->wim = w;
399                 cur_entry->resource_location = RESOURCE_IN_WIM;
400                 p = get_resource_entry(buf, &cur_entry->resource_entry);
401                 p = get_u16(p, &cur_entry->part_number);
402                 p = get_u32(p, &cur_entry->refcnt);
403                 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
404
405                 if (cur_entry->part_number != w->hdr.part_number) {
406                         ERROR("A lookup table entry in part %hu of the WIM "
407                               "points to part %hu",
408                               w->hdr.part_number, cur_entry->part_number);
409                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
410                         goto out_free_cur_entry;
411                 }
412
413                 if (is_zero_hash(cur_entry->hash)) {
414                         ERROR("The WIM lookup table contains an entry with a "
415                               "SHA1 message digest of all 0's");
416                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
417                         goto out_free_cur_entry;
418                 }
419
420                 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
421                     && (cur_entry->resource_entry.size !=
422                         cur_entry->resource_entry.original_size))
423                 {
424                 #ifdef ENABLE_ERROR_MESSAGES
425                         ERROR("Found uncompressed resource with original size "
426                               "not the same as compressed size");
427                         ERROR("The lookup table entry for the resource is as follows:");
428                         print_lookup_table_entry(cur_entry, stderr);
429                 #endif
430                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
431                         goto out_free_cur_entry;
432                 }
433
434                 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
435                         /* Lookup table entry for a metadata resource */
436                         if (cur_entry->refcnt != 1) {
437                         #ifdef ENABLE_ERROR_MESSAGES
438                                 ERROR("Found metadata resource with refcnt != 1:");
439                                 print_lookup_table_entry(cur_entry, stderr);
440                         #endif
441                                 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
442                                 goto out_free_cur_entry;
443                         }
444
445                         if (w->hdr.part_number != 1) {
446                                 ERROR("Found a metadata resource in a "
447                                       "non-first part of the split WIM!");
448                                 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
449                                 goto out_free_cur_entry;
450                         }
451                         if (w->current_image == w->hdr.image_count) {
452                                 ERROR("The WIM header says there are %u images "
453                                       "in the WIM, but we found more metadata "
454                                       "resources than this", w->hdr.image_count);
455                                 ret = WIMLIB_ERR_IMAGE_COUNT;
456                                 goto out_free_cur_entry;
457                         }
458
459                         /* Notice very carefully:  We are assigning the metadata
460                          * resources in the exact order mirrored by their lookup
461                          * table entries on disk, which is the behavior of
462                          * Microsoft's software.  In particular, this overrides
463                          * the actual locations of the metadata resources
464                          * themselves in the WIM file as well as any information
465                          * written in the XML data. */
466                         DEBUG("Found metadata resource for image %u at "
467                               "offset %"PRIu64".",
468                               w->current_image + 1,
469                               cur_entry->resource_entry.offset);
470                         w->image_metadata[
471                                 w->current_image++]->metadata_lte = cur_entry;
472                 } else {
473                         /* Lookup table entry for a stream that is not a
474                          * metadata resource */
475                         duplicate_entry = __lookup_resource(table, cur_entry->hash);
476                         if (duplicate_entry) {
477                         #ifdef ENABLE_ERROR_MESSAGES
478                                 ERROR("The WIM lookup table contains two entries with the "
479                                       "same SHA1 message digest!");
480                                 ERROR("The first entry is:");
481                                 print_lookup_table_entry(duplicate_entry, stderr);
482                                 ERROR("The second entry is:");
483                                 print_lookup_table_entry(cur_entry, stderr);
484                         #endif
485                                 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
486                                 goto out_free_cur_entry;
487                         }
488                         lookup_table_insert(table, cur_entry);
489                 }
490         }
491
492         if (w->hdr.part_number == 1 &&
493             w->current_image != w->hdr.image_count)
494         {
495                 ERROR("The WIM header says there are %u images "
496                       "in the WIM, but we only found %d metadata "
497                       "resources!", w->hdr.image_count, w->current_image);
498                 ret = WIMLIB_ERR_IMAGE_COUNT;
499                 goto out_free_lookup_table;
500         }
501         DEBUG("Done reading lookup table.");
502         w->lookup_table = table;
503         ret = 0;
504         goto out;
505 out_free_cur_entry:
506         FREE(cur_entry);
507 out_free_lookup_table:
508         free_lookup_table(table);
509 out:
510         w->current_image = 0;
511         return ret;
512 }
513
514
515 /*
516  * Writes a lookup table entry to the output file.
517  */
518 int
519 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_out)
520 {
521         FILE *out;
522         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
523         u8 *p;
524
525         out = _out;
526
527         /* Don't write entries that have not had file resources or metadata
528          * resources written for them. */
529         if (lte->out_refcnt == 0)
530                 return 0;
531
532         if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
533                 DEBUG("Writing metadata entry at %"PRIu64" "
534                       "(orig size = %"PRIu64")",
535                       ftello(out), lte->output_resource_entry.original_size);
536         }
537
538         p = put_resource_entry(buf, &lte->output_resource_entry);
539         p = put_u16(p, lte->part_number);
540         p = put_u32(p, lte->out_refcnt);
541         p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
542         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
543                 ERROR_WITH_ERRNO("Failed to write lookup table entry");
544                 return WIMLIB_ERR_WRITE;
545         }
546         return 0;
547 }
548
549 /* Writes the WIM lookup table to the output file. */
550 int
551 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
552 {
553         FILE *out = w->out_fp;
554         off_t start_offset, end_offset;
555         int ret;
556         int start_image, end_image;
557
558         start_offset = ftello(out);
559         if (start_offset == -1)
560                 return WIMLIB_ERR_WRITE;
561
562         /* Write lookup table entries for metadata resources */
563         if (image == WIMLIB_ALL_IMAGES) {
564                 start_image = 1;
565                 end_image = w->hdr.image_count;
566         } else {
567                 start_image = image;
568                 end_image = image;
569         }
570         for (int i = start_image; i <= end_image; i++) {
571                 struct wim_lookup_table_entry *metadata_lte;
572
573                 metadata_lte = w->image_metadata[i - 1]->metadata_lte;
574                 metadata_lte->out_refcnt = 1;
575                 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
576                 ret = write_lookup_table_entry(metadata_lte, out);
577                 if (ret)
578                         return ret;
579         }
580
581         /* Write lookup table entries for other resources */
582         ret = for_lookup_table_entry(w->lookup_table, write_lookup_table_entry, out);
583         if (ret)
584                 return ret;
585
586         /* Fill in the resource entry for the lookup table itself */
587         end_offset = ftello(out);
588         if (end_offset == -1)
589                 return WIMLIB_ERR_WRITE;
590
591         out_res_entry->offset        = start_offset;
592         out_res_entry->size          = end_offset - start_offset;
593         out_res_entry->original_size = end_offset - start_offset;
594         out_res_entry->flags         = WIM_RESHDR_FLAG_METADATA;
595         return 0;
596 }
597
598
599 int
600 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
601 {
602         lte->real_refcnt = 0;
603         return 0;
604 }
605
606 int
607 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
608 {
609         lte->out_refcnt = 0;
610         return 0;
611 }
612
613 int
614 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore)
615 {
616         if (lte->extracted_file != NULL) {
617                 FREE(lte->extracted_file);
618                 lte->extracted_file = NULL;
619         }
620         return 0;
621 }
622
623 void
624 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
625 {
626         if (!lte) {
627                 tputc(T('\n'), out);
628                 return;
629         }
630         tfprintf(out, T("Offset            = %"PRIu64" bytes\n"),
631                  lte->resource_entry.offset);
632
633         tfprintf(out, T("Size              = %"PRIu64" bytes\n"),
634                  (u64)lte->resource_entry.size);
635
636         tfprintf(out, T("Original size     = %"PRIu64" bytes\n"),
637                  lte->resource_entry.original_size);
638
639         tfprintf(out, T("Part Number       = %hu\n"), lte->part_number);
640         tfprintf(out, T("Reference Count   = %u\n"), lte->refcnt);
641
642         if (lte->unhashed) {
643                 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
644                          lte->back_inode, lte->back_stream_id);
645         } else {
646                 tfprintf(out, T("Hash              = 0x"));
647                 print_hash(lte->hash, out);
648                 tputc(T('\n'), out);
649         }
650
651         tfprintf(out, T("Flags             = "));
652         u8 flags = lte->resource_entry.flags;
653         if (flags & WIM_RESHDR_FLAG_COMPRESSED)
654                 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
655         if (flags & WIM_RESHDR_FLAG_FREE)
656                 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
657         if (flags & WIM_RESHDR_FLAG_METADATA)
658                 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
659         if (flags & WIM_RESHDR_FLAG_SPANNED)
660                 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
661         tputc(T('\n'), out);
662         switch (lte->resource_location) {
663         case RESOURCE_IN_WIM:
664                 if (lte->wim->filename) {
665                         tfprintf(out, T("WIM file          = `%"TS"'\n"),
666                                  lte->wim->filename);
667                 }
668                 break;
669 #ifdef __WIN32__
670         case RESOURCE_WIN32:
671 #endif
672         case RESOURCE_IN_FILE_ON_DISK:
673                 tfprintf(out, T("File on Disk      = `%"TS"'\n"),
674                          lte->file_on_disk);
675                 break;
676         case RESOURCE_IN_STAGING_FILE:
677                 tfprintf(out, T("Staging File      = `%"TS"'\n"),
678                                 lte->staging_file_name);
679                 break;
680         default:
681                 break;
682         }
683         tputc(T('\n'), out);
684 }
685
686 static int
687 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
688 {
689         print_lookup_table_entry(lte, (FILE*)fp);
690         return 0;
691 }
692
693 /*
694  * Prints the lookup table of a WIM file.
695  */
696 WIMLIBAPI void
697 wimlib_print_lookup_table(WIMStruct *w)
698 {
699         for_lookup_table_entry(w->lookup_table,
700                                do_print_lookup_table_entry,
701                                stdout);
702 }
703
704 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
705  * lookup table, or NULL if there is none.  */
706 struct wim_lookup_table_entry *
707 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
708 {
709         size_t i;
710         struct wim_lookup_table_entry *lte;
711         struct hlist_node *pos;
712
713         wimlib_assert(table != NULL);
714         wimlib_assert(hash != NULL);
715
716         i = *(size_t*)hash % table->capacity;
717         hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
718                 if (hashes_equal(hash, lte->hash))
719                         return lte;
720         return NULL;
721 }
722
723 #ifdef WITH_FUSE
724 /*
725  * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
726  * given a path name.
727  *
728  * This is only for pre-resolved inodes.
729  */
730 int
731 lookup_resource(WIMStruct *w,
732                 const tchar *path,
733                 int lookup_flags,
734                 struct wim_dentry **dentry_ret,
735                 struct wim_lookup_table_entry **lte_ret,
736                 u16 *stream_idx_ret)
737 {
738         struct wim_dentry *dentry;
739         struct wim_lookup_table_entry *lte;
740         u16 stream_idx;
741         const tchar *stream_name = NULL;
742         struct wim_inode *inode;
743         tchar *p = NULL;
744
745         if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
746                 stream_name = path_stream_name(path);
747                 if (stream_name) {
748                         p = (tchar*)stream_name - 1;
749                         *p = T('\0');
750                 }
751         }
752
753         dentry = get_dentry(w, path);
754         if (p)
755                 *p = T(':');
756         if (!dentry)
757                 return -errno;
758
759         inode = dentry->d_inode;
760
761         wimlib_assert(inode->i_resolved);
762
763         if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
764               && inode_is_directory(inode))
765                 return -EISDIR;
766
767         if (stream_name) {
768                 struct wim_ads_entry *ads_entry;
769                 u16 ads_idx;
770                 ads_entry = inode_get_ads_entry(inode, stream_name,
771                                                 &ads_idx);
772                 if (ads_entry) {
773                         stream_idx = ads_idx + 1;
774                         lte = ads_entry->lte;
775                         goto out;
776                 } else {
777                         return -ENOENT;
778                 }
779         } else {
780                 lte = inode->i_lte;
781                 stream_idx = 0;
782         }
783 out:
784         if (dentry_ret)
785                 *dentry_ret = dentry;
786         if (lte_ret)
787                 *lte_ret = lte;
788         if (stream_idx_ret)
789                 *stream_idx_ret = stream_idx;
790         return 0;
791 }
792 #endif
793
794 /* Resolve an inode's lookup table entries
795  *
796  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
797  * lookup table) with pointers directly to the lookup table entries.  A circular
798  * linked list of streams sharing the same lookup table entry is created.
799  *
800  * This function always succeeds; unresolved lookup table entries are given a
801  * NULL pointer.
802  */
803 void
804 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
805 {
806
807         if (!inode->i_resolved) {
808                 struct wim_lookup_table_entry *lte;
809                 /* Resolve the default file stream */
810                 lte = __lookup_resource(table, inode->i_hash);
811                 inode->i_lte = lte;
812                 inode->i_resolved = 1;
813
814                 /* Resolve the alternate data streams */
815                 for (u16 i = 0; i < inode->i_num_ads; i++) {
816                         struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
817                         lte = __lookup_resource(table, cur_entry->hash);
818                         cur_entry->lte = lte;
819                 }
820         }
821 }
822
823 void
824 inode_unresolve_ltes(struct wim_inode *inode)
825 {
826         if (inode->i_resolved) {
827                 if (inode->i_lte)
828                         copy_hash(inode->i_hash, inode->i_lte->hash);
829                 else
830                         zero_out_hash(inode->i_hash);
831
832                 for (u16 i = 0; i < inode->i_num_ads; i++) {
833                         if (inode->i_ads_entries[i].lte)
834                                 copy_hash(inode->i_ads_entries[i].hash,
835                                           inode->i_ads_entries[i].lte->hash);
836                         else
837                                 zero_out_hash(inode->i_ads_entries[i].hash);
838                 }
839                 inode->i_resolved = 0;
840         }
841 }
842
843 /*
844  * Returns the lookup table entry for stream @stream_idx of the inode, where
845  * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
846  * corresponds to an alternate data stream.
847  *
848  * This works for both resolved and un-resolved inodes.
849  */
850 struct wim_lookup_table_entry *
851 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
852                  const struct wim_lookup_table *table)
853 {
854         if (inode->i_resolved)
855                 return inode_stream_lte_resolved(inode, stream_idx);
856         else
857                 return inode_stream_lte_unresolved(inode, stream_idx, table);
858 }
859
860
861 /* Return the lookup table entry for the unnamed data stream of an inode, or
862  * NULL if there is none.
863  *
864  * You'd think this would be easier than it actually is, since the unnamed data
865  * stream should be the one referenced from the inode itself.  Alas, if there
866  * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
867  * data stream in one of the alternate data streams instead of inside the WIM
868  * dentry itself.  So we need to check the alternate data streams too.
869  *
870  * Also, note that a dentry may appear to have more than one unnamed stream, but
871  * if the SHA1 message digest is all 0's then the corresponding stream does not
872  * really "count" (this is the case for the inode's own file stream when the
873  * file stream that should be there is actually in one of the alternate stream
874  * entries.).  This is despite the fact that we may need to extract such a
875  * missing entry as an empty file or empty named data stream.
876  */
877 struct wim_lookup_table_entry *
878 inode_unnamed_lte(const struct wim_inode *inode,
879                   const struct wim_lookup_table *table)
880 {
881         if (inode->i_resolved)
882                 return inode_unnamed_lte_resolved(inode);
883         else
884                 return inode_unnamed_lte_unresolved(inode, table);
885 }
886
887 static int
888 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
889 {
890         *(u64*)total_bytes_p += lte->resource_entry.size;
891         return 0;
892 }
893
894 u64
895 lookup_table_total_stream_size(struct wim_lookup_table *table)
896 {
897         u64 total_size = 0;
898         for_lookup_table_entry(table, lte_add_stream_size, &total_size);
899         return total_size;
900 }
901
902 struct wim_lookup_table_entry **
903 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
904 {
905         wimlib_assert(lte->unhashed);
906         struct wim_inode *inode = lte->back_inode;
907         u32 stream_id = lte->back_stream_id;
908         if (stream_id == 0)
909                 return &inode->i_lte;
910         else
911                 for (u16 i = 0; i < inode->i_num_ads; i++)
912                         if (inode->i_ads_entries[i].stream_id == stream_id)
913                                 return &inode->i_ads_entries[i].lte;
914         wimlib_assert(0);
915         return NULL;
916 }
917
918 int
919 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
920                      struct wim_lookup_table *lookup_table,
921                      struct wim_lookup_table_entry **lte_ret)
922 {
923         int ret;
924         struct wim_lookup_table_entry *duplicate_lte;
925         struct wim_lookup_table_entry **back_ptr;
926
927         wimlib_assert(lte->unhashed);
928
929         /* back_ptr must be saved because @back_inode and @back_stream_id are in
930          * union with the SHA1 message digest and will no longer be valid once
931          * the SHA1 has been calculated. */
932         back_ptr = retrieve_lte_pointer(lte);
933
934         ret = sha1_resource(lte);
935         if (ret)
936                 return ret;
937
938         /* Look for a duplicate stream */
939         duplicate_lte = __lookup_resource(lookup_table, lte->hash);
940         list_del(&lte->unhashed_list);
941         if (duplicate_lte) {
942                 /* We have a duplicate stream.  Transfer the reference counts
943                  * from this stream to the duplicate, update the reference to
944                  * this stream (in an inode or ads_entry) to point to the
945                  * duplicate, then free this stream. */
946                 wimlib_assert(!(duplicate_lte->unhashed));
947                 duplicate_lte->refcnt += lte->refcnt;
948                 duplicate_lte->out_refcnt += lte->refcnt;
949                 *back_ptr = duplicate_lte;
950                 free_lookup_table_entry(lte);
951                 lte = duplicate_lte;
952         } else {
953                 /* No duplicate stream, so we need to insert
954                  * this stream into the lookup table and treat
955                  * it as a hashed stream. */
956                 list_del(&lte->unhashed_list);
957                 lookup_table_insert(lookup_table, lte);
958                 lte->out_refcnt = lte->refcnt;
959                 lte->unhashed = 0;
960         }
961         if (lte_ret)
962                 *lte_ret = lte;
963         return 0;
964 }
965