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