write.c, lookup table.c: cleanup
[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 int
599 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
600 {
601         lte->real_refcnt = 0;
602         return 0;
603 }
604
605 int
606 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
607 {
608         lte->out_refcnt = 0;
609         return 0;
610 }
611
612 int
613 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
614 {
615         if (lte->extracted_file != NULL) {
616                 FREE(lte->extracted_file);
617                 lte->extracted_file = NULL;
618         }
619         return 0;
620 }
621
622 void
623 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
624 {
625         if (!lte) {
626                 tputc(T('\n'), out);
627                 return;
628         }
629         tfprintf(out, T("Offset            = %"PRIu64" bytes\n"),
630                  lte->resource_entry.offset);
631
632         tfprintf(out, T("Size              = %"PRIu64" bytes\n"),
633                  (u64)lte->resource_entry.size);
634
635         tfprintf(out, T("Original size     = %"PRIu64" bytes\n"),
636                  lte->resource_entry.original_size);
637
638         tfprintf(out, T("Part Number       = %hu\n"), lte->part_number);
639         tfprintf(out, T("Reference Count   = %u\n"), lte->refcnt);
640
641         if (lte->unhashed) {
642                 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
643                          lte->back_inode, lte->back_stream_id);
644         } else {
645                 tfprintf(out, T("Hash              = 0x"));
646                 print_hash(lte->hash, out);
647                 tputc(T('\n'), out);
648         }
649
650         tfprintf(out, T("Flags             = "));
651         u8 flags = lte->resource_entry.flags;
652         if (flags & WIM_RESHDR_FLAG_COMPRESSED)
653                 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
654         if (flags & WIM_RESHDR_FLAG_FREE)
655                 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
656         if (flags & WIM_RESHDR_FLAG_METADATA)
657                 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
658         if (flags & WIM_RESHDR_FLAG_SPANNED)
659                 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
660         tputc(T('\n'), out);
661         switch (lte->resource_location) {
662         case RESOURCE_IN_WIM:
663                 if (lte->wim->filename) {
664                         tfprintf(out, T("WIM file          = `%"TS"'\n"),
665                                  lte->wim->filename);
666                 }
667                 break;
668 #ifdef __WIN32__
669         case RESOURCE_WIN32:
670 #endif
671         case RESOURCE_IN_FILE_ON_DISK:
672                 tfprintf(out, T("File on Disk      = `%"TS"'\n"),
673                          lte->file_on_disk);
674                 break;
675         case RESOURCE_IN_STAGING_FILE:
676                 tfprintf(out, T("Staging File      = `%"TS"'\n"),
677                                 lte->staging_file_name);
678                 break;
679         default:
680                 break;
681         }
682         tputc(T('\n'), out);
683 }
684
685 static int
686 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
687 {
688         print_lookup_table_entry(lte, (FILE*)fp);
689         return 0;
690 }
691
692 /*
693  * Prints the lookup table of a WIM file.
694  */
695 WIMLIBAPI void
696 wimlib_print_lookup_table(WIMStruct *w)
697 {
698         for_lookup_table_entry(w->lookup_table,
699                                do_print_lookup_table_entry,
700                                stdout);
701 }
702
703 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
704  * lookup table, or NULL if there is none.  */
705 struct wim_lookup_table_entry *
706 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
707 {
708         size_t i;
709         struct wim_lookup_table_entry *lte;
710         struct hlist_node *pos;
711
712         wimlib_assert(table != NULL);
713         wimlib_assert(hash != NULL);
714
715         i = *(size_t*)hash % table->capacity;
716         hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
717                 if (hashes_equal(hash, lte->hash))
718                         return lte;
719         return NULL;
720 }
721
722 #ifdef WITH_FUSE
723 /*
724  * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
725  * given a path name.
726  *
727  * This is only for pre-resolved inodes.
728  */
729 int
730 lookup_resource(WIMStruct *w,
731                 const tchar *path,
732                 int lookup_flags,
733                 struct wim_dentry **dentry_ret,
734                 struct wim_lookup_table_entry **lte_ret,
735                 u16 *stream_idx_ret)
736 {
737         struct wim_dentry *dentry;
738         struct wim_lookup_table_entry *lte;
739         u16 stream_idx;
740         const tchar *stream_name = NULL;
741         struct wim_inode *inode;
742         tchar *p = NULL;
743
744         if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
745                 stream_name = path_stream_name(path);
746                 if (stream_name) {
747                         p = (tchar*)stream_name - 1;
748                         *p = T('\0');
749                 }
750         }
751
752         dentry = get_dentry(w, path);
753         if (p)
754                 *p = T(':');
755         if (!dentry)
756                 return -errno;
757
758         inode = dentry->d_inode;
759
760         wimlib_assert(inode->i_resolved);
761
762         if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
763               && inode_is_directory(inode))
764                 return -EISDIR;
765
766         if (stream_name) {
767                 struct wim_ads_entry *ads_entry;
768                 u16 ads_idx;
769                 ads_entry = inode_get_ads_entry(inode, stream_name,
770                                                 &ads_idx);
771                 if (ads_entry) {
772                         stream_idx = ads_idx + 1;
773                         lte = ads_entry->lte;
774                         goto out;
775                 } else {
776                         return -ENOENT;
777                 }
778         } else {
779                 lte = inode->i_lte;
780                 stream_idx = 0;
781         }
782 out:
783         if (dentry_ret)
784                 *dentry_ret = dentry;
785         if (lte_ret)
786                 *lte_ret = lte;
787         if (stream_idx_ret)
788                 *stream_idx_ret = stream_idx;
789         return 0;
790 }
791 #endif
792
793 /* Resolve an inode's lookup table entries
794  *
795  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
796  * lookup table) with pointers directly to the lookup table entries.  A circular
797  * linked list of streams sharing the same lookup table entry is created.
798  *
799  * This function always succeeds; unresolved lookup table entries are given a
800  * NULL pointer.
801  */
802 void
803 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
804 {
805
806         if (!inode->i_resolved) {
807                 struct wim_lookup_table_entry *lte;
808                 /* Resolve the default file stream */
809                 lte = __lookup_resource(table, inode->i_hash);
810                 inode->i_lte = lte;
811                 inode->i_resolved = 1;
812
813                 /* Resolve the alternate data streams */
814                 for (u16 i = 0; i < inode->i_num_ads; i++) {
815                         struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
816                         lte = __lookup_resource(table, cur_entry->hash);
817                         cur_entry->lte = lte;
818                 }
819         }
820 }
821
822 void
823 inode_unresolve_ltes(struct wim_inode *inode)
824 {
825         if (inode->i_resolved) {
826                 if (inode->i_lte)
827                         copy_hash(inode->i_hash, inode->i_lte->hash);
828                 else
829                         zero_out_hash(inode->i_hash);
830
831                 for (u16 i = 0; i < inode->i_num_ads; i++) {
832                         if (inode->i_ads_entries[i].lte)
833                                 copy_hash(inode->i_ads_entries[i].hash,
834                                           inode->i_ads_entries[i].lte->hash);
835                         else
836                                 zero_out_hash(inode->i_ads_entries[i].hash);
837                 }
838                 inode->i_resolved = 0;
839         }
840 }
841
842 /*
843  * Returns the lookup table entry for stream @stream_idx of the inode, where
844  * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
845  * corresponds to an alternate data stream.
846  *
847  * This works for both resolved and un-resolved inodes.
848  */
849 struct wim_lookup_table_entry *
850 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
851                  const struct wim_lookup_table *table)
852 {
853         if (inode->i_resolved)
854                 return inode_stream_lte_resolved(inode, stream_idx);
855         else
856                 return inode_stream_lte_unresolved(inode, stream_idx, table);
857 }
858
859
860 /* Return the lookup table entry for the unnamed data stream of an inode, or
861  * NULL if there is none.
862  *
863  * You'd think this would be easier than it actually is, since the unnamed data
864  * stream should be the one referenced from the inode itself.  Alas, if there
865  * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
866  * data stream in one of the alternate data streams instead of inside the WIM
867  * dentry itself.  So we need to check the alternate data streams too.
868  *
869  * Also, note that a dentry may appear to have more than one unnamed stream, but
870  * if the SHA1 message digest is all 0's then the corresponding stream does not
871  * really "count" (this is the case for the inode's own file stream when the
872  * file stream that should be there is actually in one of the alternate stream
873  * entries.).  This is despite the fact that we may need to extract such a
874  * missing entry as an empty file or empty named data stream.
875  */
876 struct wim_lookup_table_entry *
877 inode_unnamed_lte(const struct wim_inode *inode,
878                   const struct wim_lookup_table *table)
879 {
880         if (inode->i_resolved)
881                 return inode_unnamed_lte_resolved(inode);
882         else
883                 return inode_unnamed_lte_unresolved(inode, table);
884 }
885
886 static int
887 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
888 {
889         *(u64*)total_bytes_p += lte->resource_entry.size;
890         return 0;
891 }
892
893 u64
894 lookup_table_total_stream_size(struct wim_lookup_table *table)
895 {
896         u64 total_size = 0;
897         for_lookup_table_entry(table, lte_add_stream_size, &total_size);
898         return total_size;
899 }
900
901 struct wim_lookup_table_entry **
902 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
903 {
904         wimlib_assert(lte->unhashed);
905         struct wim_inode *inode = lte->back_inode;
906         u32 stream_id = lte->back_stream_id;
907         if (stream_id == 0)
908                 return &inode->i_lte;
909         else
910                 for (u16 i = 0; i < inode->i_num_ads; i++)
911                         if (inode->i_ads_entries[i].stream_id == stream_id)
912                                 return &inode->i_ads_entries[i].lte;
913         wimlib_assert(0);
914         return NULL;
915 }
916
917 /* Calculate the SHA1 message digest of a stream and move it from the list of
918  * unhashed streams to the stream lookup table, possibly joining it with an
919  * existing lookup table entry for an identical stream.
920  *
921  * @lte:  An unhashed lookup table entry.
922  * @lookup_table:  Lookup table for the WIM.
923  * @lte_ret:  On success, write a pointer to the resulting lookup table
924  *            entry to this location.  This will be the same as @lte
925  *            if it was inserted into the lookup table, or different if
926  *            a duplicate stream was found.
927  *
928  * Returns 0 on success; nonzero if there is an error reading the stream.
929  */
930 int
931 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
932                      struct wim_lookup_table *lookup_table,
933                      struct wim_lookup_table_entry **lte_ret)
934 {
935         int ret;
936         struct wim_lookup_table_entry *duplicate_lte;
937         struct wim_lookup_table_entry **back_ptr;
938
939         wimlib_assert(lte->unhashed);
940
941         /* back_ptr must be saved because @back_inode and @back_stream_id are in
942          * union with the SHA1 message digest and will no longer be valid once
943          * the SHA1 has been calculated. */
944         back_ptr = retrieve_lte_pointer(lte);
945
946         ret = sha1_resource(lte);
947         if (ret)
948                 return ret;
949
950         /* Look for a duplicate stream */
951         duplicate_lte = __lookup_resource(lookup_table, lte->hash);
952         list_del(&lte->unhashed_list);
953         if (duplicate_lte) {
954                 /* We have a duplicate stream.  Transfer the reference counts
955                  * from this stream to the duplicate, update the reference to
956                  * this stream (in an inode or ads_entry) to point to the
957                  * duplicate, then free this stream. */
958                 wimlib_assert(!(duplicate_lte->unhashed));
959                 duplicate_lte->refcnt += lte->refcnt;
960                 duplicate_lte->out_refcnt += lte->refcnt;
961                 *back_ptr = duplicate_lte;
962                 free_lookup_table_entry(lte);
963                 lte = duplicate_lte;
964         } else {
965                 /* No duplicate stream, so we need to insert
966                  * this stream into the lookup table and treat
967                  * it as a hashed stream. */
968                 lookup_table_insert(lookup_table, lte);
969                 lte->unhashed = 0;
970         }
971         if (lte_ret)
972                 *lte_ret = lte;
973         return 0;
974 }
975