4 * Lookup table, implemented as a hash table, that maps SHA1 message digests to
5 * data streams; plus code to read and write the corresponding on-disk data.
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
31 #include "wimlib/endianness.h"
32 #include "wimlib/error.h"
33 #include "wimlib/file_io.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/metadata.h"
36 #include "wimlib/paths.h"
37 #include "wimlib/resource.h"
38 #include "wimlib/util.h"
39 #include "wimlib/write.h"
44 # include <unistd.h> /* for unlink() */
47 struct wim_lookup_table *
48 new_lookup_table(size_t capacity)
50 struct wim_lookup_table *table;
51 struct hlist_head *array;
53 table = CALLOC(1, sizeof(struct wim_lookup_table));
55 array = CALLOC(capacity, sizeof(array[0]));
57 table->num_entries = 0;
58 table->capacity = capacity;
63 ERROR("Failed to allocate memory for lookup table "
64 "with capacity %zu", capacity);
70 struct wim_lookup_table_entry *
71 new_lookup_table_entry(void)
73 struct wim_lookup_table_entry *lte;
75 lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
80 ERROR("Out of memory (tried to allocate %zu bytes for "
81 "lookup table entry)",
82 sizeof(struct wim_lookup_table_entry));
87 struct wim_lookup_table_entry *
88 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
90 struct wim_lookup_table_entry *new;
92 new = memdup(old, sizeof(struct wim_lookup_table_entry));
96 new->extracted_file = NULL;
97 switch (new->resource_location) {
98 case RESOURCE_IN_FILE_ON_DISK:
100 case RESOURCE_WIN32_ENCRYPTED:
103 case RESOURCE_IN_STAGING_FILE:
104 BUILD_BUG_ON((void*)&old->file_on_disk !=
105 (void*)&old->staging_file_name);
107 new->file_on_disk = TSTRDUP(old->file_on_disk);
108 if (!new->file_on_disk)
111 case RESOURCE_IN_ATTACHED_BUFFER:
112 new->attached_buffer = memdup(old->attached_buffer,
113 wim_resource_size(old));
114 if (!new->attached_buffer)
118 case RESOURCE_IN_NTFS_VOLUME:
120 struct ntfs_location *loc;
121 loc = memdup(old->ntfs_loc, sizeof(struct ntfs_location));
125 loc->stream_name = NULL;
127 loc->path = STRDUP(old->ntfs_loc->path);
130 if (loc->stream_name_nchars) {
131 loc->stream_name = memdup(old->ntfs_loc->stream_name,
132 loc->stream_name_nchars * 2);
133 if (!loc->stream_name)
144 free_lookup_table_entry(new);
149 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
152 switch (lte->resource_location) {
153 case RESOURCE_IN_FILE_ON_DISK:
155 case RESOURCE_WIN32_ENCRYPTED:
158 case RESOURCE_IN_STAGING_FILE:
159 BUILD_BUG_ON((void*)<e->file_on_disk !=
160 (void*)<e->staging_file_name);
162 case RESOURCE_IN_ATTACHED_BUFFER:
163 BUILD_BUG_ON((void*)<e->file_on_disk !=
164 (void*)<e->attached_buffer);
165 FREE(lte->file_on_disk);
168 case RESOURCE_IN_NTFS_VOLUME:
170 FREE(lte->ntfs_loc->path);
171 FREE(lte->ntfs_loc->stream_name);
184 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
186 free_lookup_table_entry(entry);
192 free_lookup_table(struct wim_lookup_table *table)
194 DEBUG2("Freeing lookup table");
197 for_lookup_table_entry(table,
198 do_free_lookup_table_entry,
207 * Inserts an entry into the lookup table.
209 * @table: A pointer to the lookup table.
210 * @lte: A pointer to the entry to insert.
213 lookup_table_insert(struct wim_lookup_table *table,
214 struct wim_lookup_table_entry *lte)
216 size_t i = lte->hash_short % table->capacity;
217 hlist_add_head(<e->hash_list, &table->array[i]);
219 /* XXX Make the table grow when too many entries have been inserted. */
220 table->num_entries++;
224 finalize_lte(struct wim_lookup_table_entry *lte)
227 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
228 unlink(lte->staging_file_name);
229 list_del(<e->unhashed_list);
232 free_lookup_table_entry(lte);
235 /* Decrements the reference count for the lookup table entry @lte. If its
236 * reference count reaches 0, it is unlinked from the lookup table. If,
237 * furthermore, the entry has no opened file descriptors associated with it, the
240 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
241 struct wim_lookup_table *table)
243 wimlib_assert(lte != NULL);
244 wimlib_assert(lte->refcnt != 0);
245 if (--lte->refcnt == 0) {
247 list_del(<e->unhashed_list);
249 lookup_table_unlink(table, lte);
251 if (lte->num_opened_fds == 0)
259 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
261 if (lte->num_opened_fds != 0)
262 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
267 /* Calls a function on all the entries in the WIM lookup table. Stop early and
268 * return nonzero if any call to the function returns nonzero. */
270 for_lookup_table_entry(struct wim_lookup_table *table,
271 int (*visitor)(struct wim_lookup_table_entry *, void *),
274 struct wim_lookup_table_entry *lte;
275 struct hlist_node *pos, *tmp;
278 for (size_t i = 0; i < table->capacity; i++) {
279 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
282 wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
283 ret = visitor(lte, arg);
291 /* qsort() callback that sorts streams (represented by `struct
292 * wim_lookup_table_entry's) into an order optimized for reading and writing.
294 * Sorting is done primarily by resource location, then secondarily by a
295 * per-resource location order. For example, resources in WIM files are sorted
296 * primarily by part number, then secondarily by offset, as to implement optimal
297 * reading of either a standalone or split WIM. */
299 cmp_streams_by_sequential_order(const void *p1, const void *p2)
301 const struct wim_lookup_table_entry *lte1, *lte2;
304 lte1 = *(const struct wim_lookup_table_entry**)p1;
305 lte2 = *(const struct wim_lookup_table_entry**)p2;
307 v = (int)lte1->resource_location - (int)lte2->resource_location;
309 /* Different resource locations? */
313 switch (lte1->resource_location) {
314 case RESOURCE_IN_WIM:
316 /* Different (possibly split) WIMs? */
317 if (lte1->wim != lte2->wim) {
318 v = memcmp(lte1->wim->hdr.guid, lte2->wim->hdr.guid,
324 /* Different part numbers in the same WIM? */
325 v = (int)lte1->wim->hdr.part_number - (int)lte2->wim->hdr.part_number;
329 /* Compare by offset. */
330 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
332 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
335 case RESOURCE_IN_FILE_ON_DISK:
337 case RESOURCE_WIN32_ENCRYPTED:
339 /* Compare files by path: just a heuristic that will place files
340 * in the same directory next to each other. */
341 return tstrcmp(lte1->file_on_disk, lte2->file_on_disk);
343 case RESOURCE_IN_NTFS_VOLUME:
344 return tstrcmp(lte1->ntfs_loc->path, lte2->ntfs_loc->path);
347 /* No additional sorting order defined for this resource
348 * location (e.g. RESOURCE_IN_ATTACHED_BUFFER); simply compare
349 * everything equal to each other. */
355 sort_stream_list_by_sequential_order(struct list_head *stream_list,
356 size_t list_head_offset)
358 struct list_head *cur;
359 struct wim_lookup_table_entry **array;
362 size_t num_streams = 0;
364 list_for_each(cur, stream_list)
367 array_size = num_streams * sizeof(array[0]);
368 array = MALLOC(array_size);
370 return WIMLIB_ERR_NOMEM;
371 cur = stream_list->next;
372 for (i = 0; i < num_streams; i++) {
373 array[i] = (struct wim_lookup_table_entry*)((u8*)cur -
378 qsort(array, num_streams, sizeof(array[0]),
379 cmp_streams_by_sequential_order);
381 INIT_LIST_HEAD(stream_list);
382 for (i = 0; i < num_streams; i++) {
383 list_add_tail((struct list_head*)
384 ((u8*)array[i] + list_head_offset),
393 add_lte_to_array(struct wim_lookup_table_entry *lte,
396 struct wim_lookup_table_entry ***pp = _pp;
401 /* Iterate through the lookup table entries, but first sort them by stream
402 * offset in the WIM. Caution: this is intended to be used when the stream
403 * offset field has actually been set. */
405 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
406 int (*visitor)(struct wim_lookup_table_entry *,
410 struct wim_lookup_table_entry **lte_array, **p;
411 size_t num_streams = table->num_entries;
414 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
416 return WIMLIB_ERR_NOMEM;
418 for_lookup_table_entry(table, add_lte_to_array, &p);
420 wimlib_assert(p == lte_array + num_streams);
422 qsort(lte_array, num_streams, sizeof(lte_array[0]),
423 cmp_streams_by_sequential_order);
425 for (size_t i = 0; i < num_streams; i++) {
426 ret = visitor(lte_array[i], arg);
434 /* On-disk format of a WIM lookup table entry (stream entry). */
435 struct wim_lookup_table_entry_disk {
436 /* Location, offset, compression status, and metadata status of the
438 struct resource_entry_disk resource_entry;
440 /* Which part of the split WIM this stream is in; indexed from 1. */
443 /* Reference count of this stream over all WIM images. */
446 /* SHA1 message digest of the uncompressed data of this stream, or
447 * optionally all zeroes if this stream is of zero length. */
448 u8 hash[SHA1_HASH_SIZE];
451 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
454 lte_init_wim(struct wim_lookup_table_entry *lte, WIMStruct *wim)
456 lte->resource_location = RESOURCE_IN_WIM;
458 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
459 lte->compression_type = wim->compression_type;
461 lte->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
463 if (wim_is_pipable(wim))
468 * Reads the lookup table from a WIM file.
470 * Saves lookup table entries for non-metadata streams in a hash table, and
471 * saves the metadata entry for each image in a special per-image location (the
472 * image_metadata array).
475 * WIMLIB_ERR_SUCCESS (0)
476 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
477 * WIMLIB_ERR_RESOURCE_NOT_FOUND
480 read_wim_lookup_table(WIMStruct *wim)
485 struct wim_lookup_table *table;
486 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
487 struct wim_lookup_table_entry_disk *buf;
489 BUILD_BUG_ON(sizeof(struct wim_lookup_table_entry_disk) !=
490 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE);
492 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
493 wim->hdr.lookup_table_res_entry.offset,
494 wim->hdr.lookup_table_res_entry.size);
496 /* Calculate number of entries in the lookup table. */
497 num_entries = wim->hdr.lookup_table_res_entry.size /
498 sizeof(struct wim_lookup_table_entry_disk);
501 /* Read the lookup table into a buffer. */
502 ret = res_entry_to_data(&wim->hdr.lookup_table_res_entry, wim,
507 /* Allocate hash table. */
508 table = new_lookup_table(num_entries * 2 + 1);
510 ERROR("Not enough memory to read lookup table.");
511 ret = WIMLIB_ERR_NOMEM;
515 /* Allocate and initalize `struct wim_lookup_table_entry's from the
516 * on-disk lookup table. */
517 wim->current_image = 0;
518 for (i = 0; i < num_entries; i++) {
519 const struct wim_lookup_table_entry_disk *disk_entry = &buf[i];
521 cur_entry = new_lookup_table_entry();
523 ERROR("Not enough memory to read lookup table.");
524 ret = WIMLIB_ERR_NOMEM;
525 goto out_free_lookup_table;
528 cur_entry->wim = wim;
529 cur_entry->resource_location = RESOURCE_IN_WIM;
530 get_resource_entry(&disk_entry->resource_entry, &cur_entry->resource_entry);
531 cur_entry->part_number = le16_to_cpu(disk_entry->part_number);
532 cur_entry->refcnt = le32_to_cpu(disk_entry->refcnt);
533 copy_hash(cur_entry->hash, disk_entry->hash);
534 lte_init_wim(cur_entry, wim);
536 if (cur_entry->part_number != wim->hdr.part_number) {
537 WARNING("A lookup table entry in part %hu of the WIM "
538 "points to part %hu (ignoring it)",
539 wim->hdr.part_number, cur_entry->part_number);
540 free_lookup_table_entry(cur_entry);
544 if (is_zero_hash(cur_entry->hash)) {
545 WARNING("The WIM lookup table contains an entry with a "
546 "SHA1 message digest of all 0's (ignoring it)");
547 free_lookup_table_entry(cur_entry);
551 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
552 && (cur_entry->resource_entry.size !=
553 cur_entry->resource_entry.original_size))
555 if (wimlib_print_errors) {
556 WARNING("Found uncompressed resource with "
557 "original size (%"PRIu64") not the same "
558 "as compressed size (%"PRIu64")",
559 cur_entry->resource_entry.original_size,
560 cur_entry->resource_entry.size);
561 if (cur_entry->resource_entry.original_size) {
562 WARNING("Overriding compressed size with original size.");
563 cur_entry->resource_entry.size =
564 cur_entry->resource_entry.original_size;
566 WARNING("Overriding original size with compressed size");
567 cur_entry->resource_entry.original_size =
568 cur_entry->resource_entry.size;
573 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
574 /* Lookup table entry for a metadata resource */
575 if (cur_entry->refcnt != 1) {
576 if (wimlib_print_errors) {
577 ERROR("Found metadata resource with refcnt != 1:");
578 print_lookup_table_entry(cur_entry, stderr);
580 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
581 goto out_free_cur_entry;
584 if (wim->hdr.part_number != 1) {
585 WARNING("Ignoring metadata resource found in a "
586 "non-first part of the split WIM");
587 free_lookup_table_entry(cur_entry);
590 if (wim->current_image == wim->hdr.image_count) {
591 WARNING("The WIM header says there are %u images "
592 "in the WIM, but we found more metadata "
593 "resources than this (ignoring the extra)",
594 wim->hdr.image_count);
595 free_lookup_table_entry(cur_entry);
599 /* Notice very carefully: We are assigning the metadata
600 * resources in the exact order mirrored by their lookup
601 * table entries on disk, which is the behavior of
602 * Microsoft's software. In particular, this overrides
603 * the actual locations of the metadata resources
604 * themselves in the WIM file as well as any information
605 * written in the XML data. */
606 DEBUG("Found metadata resource for image %u at "
608 wim->current_image + 1,
609 cur_entry->resource_entry.offset);
611 wim->current_image++]->metadata_lte = cur_entry;
613 /* Lookup table entry for a stream that is not a
614 * metadata resource */
615 duplicate_entry = __lookup_resource(table, cur_entry->hash);
616 if (duplicate_entry) {
617 if (wimlib_print_errors) {
618 WARNING("The WIM lookup table contains two entries with the "
619 "same SHA1 message digest!");
620 WARNING("The first entry is:");
621 print_lookup_table_entry(duplicate_entry, stderr);
622 WARNING("The second entry is:");
623 print_lookup_table_entry(cur_entry, stderr);
625 free_lookup_table_entry(cur_entry);
628 lookup_table_insert(table, cur_entry);
633 if (wim->hdr.part_number == 1 && wim->current_image != wim->hdr.image_count) {
634 WARNING("The header of \"%"TS"\" says there are %u images in\n"
635 " the WIM, but we only found %d metadata resources! Acting as if\n"
636 " the header specified only %d images instead.",
637 wim->filename, wim->hdr.image_count,
638 wim->current_image, wim->current_image);
639 for (int i = wim->current_image; i < wim->hdr.image_count; i++)
640 put_image_metadata(wim->image_metadata[i], NULL);
641 wim->hdr.image_count = wim->current_image;
643 DEBUG("Done reading lookup table.");
644 wim->lookup_table = table;
649 out_free_lookup_table:
650 free_lookup_table(table);
654 wim->current_image = 0;
660 write_wim_lookup_table_entry(const struct wim_lookup_table_entry *lte,
661 struct wim_lookup_table_entry_disk *disk_entry)
663 put_resource_entry(<e->output_resource_entry, &disk_entry->resource_entry);
664 disk_entry->part_number = cpu_to_le16(lte->part_number);
665 disk_entry->refcnt = cpu_to_le32(lte->out_refcnt);
666 copy_hash(disk_entry->hash, lte->hash);
670 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
671 struct filedes *out_fd,
672 struct resource_entry *out_res_entry,
673 int write_resource_flags)
676 struct wim_lookup_table_entry *lte;
677 struct wim_lookup_table_entry_disk *table_buf;
678 struct wim_lookup_table_entry_disk *table_buf_ptr;
682 list_for_each_entry(lte, stream_list, lookup_table_list)
683 table_size += sizeof(struct wim_lookup_table_entry_disk);
685 DEBUG("Writing WIM lookup table (size=%zu, offset=%"PRIu64")",
686 table_size, out_fd->offset);
688 table_buf = MALLOC(table_size);
690 ERROR("Failed to allocate %zu bytes for temporary lookup table",
692 return WIMLIB_ERR_NOMEM;
694 table_buf_ptr = table_buf;
695 list_for_each_entry(lte, stream_list, lookup_table_list)
696 write_wim_lookup_table_entry(lte, table_buf_ptr++);
698 /* Write the lookup table uncompressed. Although wimlib can handle a
699 * compressed lookup table, MS software cannot. */
700 ret = write_wim_resource_from_buffer(table_buf,
702 WIM_RESHDR_FLAG_METADATA,
704 WIMLIB_COMPRESSION_TYPE_NONE,
707 write_resource_flags);
713 append_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_list)
715 if (lte->out_refcnt != 0)
716 list_add_tail(<e->lookup_table_list, (struct list_head*)_list);
721 write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
722 struct resource_entry *out_res_entry,
723 struct list_head *stream_list_override)
725 int write_resource_flags;
726 struct list_head _stream_list;
727 struct list_head *stream_list;
729 if (stream_list_override) {
730 stream_list = stream_list_override;
732 stream_list = &_stream_list;
733 INIT_LIST_HEAD(stream_list);
736 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_METADATA)) {
740 if (image == WIMLIB_ALL_IMAGES) {
742 end_image = wim->hdr.image_count;
748 /* Push metadata resource lookup table entries onto the front of
749 * the list in reverse order, so that they're written in order.
751 for (int i = end_image; i >= start_image; i--) {
752 struct wim_lookup_table_entry *metadata_lte;
754 metadata_lte = wim->image_metadata[i - 1]->metadata_lte;
755 metadata_lte->out_refcnt = 1;
756 metadata_lte->part_number = wim->hdr.part_number;
757 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
759 list_add(&metadata_lte->lookup_table_list, stream_list);
763 /* Append additional lookup table entries that have out_refcnt != 0. */
764 if (!stream_list_override) {
765 for_lookup_table_entry(wim->lookup_table,
766 append_lookup_table_entry, stream_list);
769 write_resource_flags = 0;
770 if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
771 write_resource_flags |= WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE;
772 return write_wim_lookup_table_from_stream_list(stream_list,
775 write_resource_flags);
780 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
782 lte->real_refcnt = 0;
787 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
794 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
796 if (lte->extracted_file != NULL) {
797 FREE(lte->extracted_file);
798 lte->extracted_file = NULL;
804 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
810 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
811 lte->resource_entry.offset);
813 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
814 (u64)lte->resource_entry.size);
816 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
817 lte->resource_entry.original_size);
819 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
820 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
823 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
824 lte->back_inode, lte->back_stream_id);
826 tfprintf(out, T("Hash = 0x"));
827 print_hash(lte->hash, out);
831 tfprintf(out, T("Flags = "));
832 u8 flags = lte->resource_entry.flags;
833 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
834 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
835 if (flags & WIM_RESHDR_FLAG_FREE)
836 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
837 if (flags & WIM_RESHDR_FLAG_METADATA)
838 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
839 if (flags & WIM_RESHDR_FLAG_SPANNED)
840 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
842 switch (lte->resource_location) {
843 case RESOURCE_IN_WIM:
844 if (lte->wim->filename) {
845 tfprintf(out, T("WIM file = `%"TS"'\n"),
850 case RESOURCE_WIN32_ENCRYPTED:
852 case RESOURCE_IN_FILE_ON_DISK:
853 tfprintf(out, T("File on Disk = `%"TS"'\n"),
857 case RESOURCE_IN_STAGING_FILE:
858 tfprintf(out, T("Staging File = `%"TS"'\n"),
859 lte->staging_file_name);
869 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
870 struct wimlib_resource_entry *wentry)
872 wentry->uncompressed_size = lte->resource_entry.original_size;
873 wentry->compressed_size = lte->resource_entry.size;
874 wentry->offset = lte->resource_entry.offset;
875 copy_hash(wentry->sha1_hash, lte->hash);
876 wentry->part_number = lte->part_number;
877 wentry->reference_count = lte->refcnt;
878 wentry->is_compressed = (lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
879 wentry->is_metadata = (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) != 0;
880 wentry->is_free = (lte->resource_entry.flags & WIM_RESHDR_FLAG_FREE) != 0;
881 wentry->is_spanned = (lte->resource_entry.flags & WIM_RESHDR_FLAG_SPANNED) != 0;
884 struct iterate_lte_context {
885 wimlib_iterate_lookup_table_callback_t cb;
890 do_iterate_lte(struct wim_lookup_table_entry *lte, void *_ctx)
892 struct iterate_lte_context *ctx = _ctx;
893 struct wimlib_resource_entry entry;
895 lte_to_wimlib_resource_entry(lte, &entry);
896 return (*ctx->cb)(&entry, ctx->user_ctx);
899 /* API function documented in wimlib.h */
901 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
902 wimlib_iterate_lookup_table_callback_t cb,
905 struct iterate_lte_context ctx = {
907 .user_ctx = user_ctx,
909 if (wim->hdr.part_number == 1) {
911 for (int i = 0; i < wim->hdr.image_count; i++) {
912 ret = do_iterate_lte(wim->image_metadata[i]->metadata_lte,
918 return for_lookup_table_entry(wim->lookup_table, do_iterate_lte, &ctx);
922 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
924 print_lookup_table_entry(lte, (FILE*)fp);
928 /* API function documented in wimlib.h */
930 wimlib_print_lookup_table(WIMStruct *wim)
932 for (int i = 0; i < wim->hdr.image_count; i++)
933 print_lookup_table_entry(wim->image_metadata[i]->metadata_lte, stdout);
934 for_lookup_table_entry(wim->lookup_table,
935 do_print_lookup_table_entry,
939 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
940 * lookup table, or NULL if there is none. */
941 struct wim_lookup_table_entry *
942 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
945 struct wim_lookup_table_entry *lte;
946 struct hlist_node *pos;
948 wimlib_assert(table != NULL);
949 wimlib_assert(hash != NULL);
951 i = *(size_t*)hash % table->capacity;
952 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
953 if (hashes_equal(hash, lte->hash))
960 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
963 * This is only for pre-resolved inodes.
966 lookup_resource(WIMStruct *wim,
969 struct wim_dentry **dentry_ret,
970 struct wim_lookup_table_entry **lte_ret,
973 struct wim_dentry *dentry;
974 struct wim_lookup_table_entry *lte;
976 const tchar *stream_name = NULL;
977 struct wim_inode *inode;
980 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
981 stream_name = path_stream_name(path);
983 p = (tchar*)stream_name - 1;
988 dentry = get_dentry(wim, path);
994 inode = dentry->d_inode;
996 if (!inode->i_resolved)
997 if (inode_resolve_ltes(inode, wim->lookup_table, false))
1000 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
1001 && inode_is_directory(inode))
1005 struct wim_ads_entry *ads_entry;
1007 ads_entry = inode_get_ads_entry(inode, stream_name,
1010 stream_idx = ads_idx + 1;
1011 lte = ads_entry->lte;
1022 *dentry_ret = dentry;
1026 *stream_idx_ret = stream_idx;
1032 * Resolve an inode's lookup table entries.
1034 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
1035 * lookup table) with pointers directly to the lookup table entries.
1037 * If @force is %false:
1038 * If any needed SHA1 message digests are not found in the lookup table,
1039 * WIMLIB_ERR_RESOURCE_NOT_FOUND is returned and the inode is left
1041 * If @force is %true:
1042 * If any needed SHA1 message digests are not found in the lookup table,
1043 * new entries are allocated and inserted into the lookup table.
1046 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
1051 if (!inode->i_resolved) {
1052 struct wim_lookup_table_entry *lte, *ads_lte;
1054 /* Resolve the default file stream */
1056 hash = inode->i_hash;
1057 if (!is_zero_hash(hash)) {
1058 lte = __lookup_resource(table, hash);
1061 lte = new_lookup_table_entry();
1063 return WIMLIB_ERR_NOMEM;
1064 copy_hash(lte->hash, hash);
1065 lookup_table_insert(table, lte);
1067 goto resource_not_found;
1072 /* Resolve the alternate data streams */
1073 struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
1074 for (u16 i = 0; i < inode->i_num_ads; i++) {
1075 struct wim_ads_entry *cur_entry;
1078 cur_entry = &inode->i_ads_entries[i];
1079 hash = cur_entry->hash;
1080 if (!is_zero_hash(hash)) {
1081 ads_lte = __lookup_resource(table, hash);
1084 ads_lte = new_lookup_table_entry();
1086 return WIMLIB_ERR_NOMEM;
1087 copy_hash(ads_lte->hash, hash);
1088 lookup_table_insert(table, ads_lte);
1090 goto resource_not_found;
1094 ads_ltes[i] = ads_lte;
1097 for (u16 i = 0; i < inode->i_num_ads; i++)
1098 inode->i_ads_entries[i].lte = ads_ltes[i];
1099 inode->i_resolved = 1;
1103 if (wimlib_print_errors) {
1104 ERROR("\"%"TS"\": resource not found", inode_first_full_path(inode));
1105 tfprintf(stderr, T(" SHA-1 message digest of missing resource:\n "));
1106 print_hash(hash, stderr);
1107 tputc(T('\n'), stderr);
1109 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
1113 inode_unresolve_ltes(struct wim_inode *inode)
1115 if (inode->i_resolved) {
1117 copy_hash(inode->i_hash, inode->i_lte->hash);
1119 zero_out_hash(inode->i_hash);
1121 for (u16 i = 0; i < inode->i_num_ads; i++) {
1122 if (inode->i_ads_entries[i].lte)
1123 copy_hash(inode->i_ads_entries[i].hash,
1124 inode->i_ads_entries[i].lte->hash);
1126 zero_out_hash(inode->i_ads_entries[i].hash);
1128 inode->i_resolved = 0;
1133 * Returns the lookup table entry for stream @stream_idx of the inode, where
1134 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
1135 * corresponds to an alternate data stream.
1137 * This works for both resolved and un-resolved inodes.
1139 struct wim_lookup_table_entry *
1140 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
1141 const struct wim_lookup_table *table)
1143 if (inode->i_resolved)
1144 return inode_stream_lte_resolved(inode, stream_idx);
1146 return inode_stream_lte_unresolved(inode, stream_idx, table);
1149 struct wim_lookup_table_entry *
1150 inode_unnamed_lte_resolved(const struct wim_inode *inode)
1152 wimlib_assert(inode->i_resolved);
1153 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1154 if (inode_stream_name_nbytes(inode, i) == 0 &&
1155 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
1157 return inode_stream_lte_resolved(inode, i);
1163 struct wim_lookup_table_entry *
1164 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
1165 const struct wim_lookup_table *table)
1167 wimlib_assert(!inode->i_resolved);
1168 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1169 if (inode_stream_name_nbytes(inode, i) == 0 &&
1170 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
1172 return inode_stream_lte_unresolved(inode, i, table);
1178 /* Return the lookup table entry for the unnamed data stream of an inode, or
1179 * NULL if there is none.
1181 * You'd think this would be easier than it actually is, since the unnamed data
1182 * stream should be the one referenced from the inode itself. Alas, if there
1183 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
1184 * data stream in one of the alternate data streams instead of inside the WIM
1185 * dentry itself. So we need to check the alternate data streams too.
1187 * Also, note that a dentry may appear to have more than one unnamed stream, but
1188 * if the SHA1 message digest is all 0's then the corresponding stream does not
1189 * really "count" (this is the case for the inode's own file stream when the
1190 * file stream that should be there is actually in one of the alternate stream
1191 * entries.). This is despite the fact that we may need to extract such a
1192 * missing entry as an empty file or empty named data stream.
1194 struct wim_lookup_table_entry *
1195 inode_unnamed_lte(const struct wim_inode *inode,
1196 const struct wim_lookup_table *table)
1198 if (inode->i_resolved)
1199 return inode_unnamed_lte_resolved(inode);
1201 return inode_unnamed_lte_unresolved(inode, table);
1205 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
1207 *(u64*)total_bytes_p += lte->resource_entry.size;
1212 lookup_table_total_stream_size(struct wim_lookup_table *table)
1215 for_lookup_table_entry(table, lte_add_stream_size, &total_size);
1219 struct wim_lookup_table_entry **
1220 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
1222 wimlib_assert(lte->unhashed);
1223 struct wim_inode *inode = lte->back_inode;
1224 u32 stream_id = lte->back_stream_id;
1226 return &inode->i_lte;
1228 for (u16 i = 0; i < inode->i_num_ads; i++)
1229 if (inode->i_ads_entries[i].stream_id == stream_id)
1230 return &inode->i_ads_entries[i].lte;
1235 /* Calculate the SHA1 message digest of a stream and move it from the list of
1236 * unhashed streams to the stream lookup table, possibly joining it with an
1237 * existing lookup table entry for an identical stream.
1239 * @lte: An unhashed lookup table entry.
1240 * @lookup_table: Lookup table for the WIM.
1241 * @lte_ret: On success, write a pointer to the resulting lookup table
1242 * entry to this location. This will be the same as @lte
1243 * if it was inserted into the lookup table, or different if
1244 * a duplicate stream was found.
1246 * Returns 0 on success; nonzero if there is an error reading the stream.
1249 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
1250 struct wim_lookup_table *lookup_table,
1251 struct wim_lookup_table_entry **lte_ret)
1254 struct wim_lookup_table_entry *duplicate_lte;
1255 struct wim_lookup_table_entry **back_ptr;
1257 wimlib_assert(lte->unhashed);
1259 /* back_ptr must be saved because @back_inode and @back_stream_id are in
1260 * union with the SHA1 message digest and will no longer be valid once
1261 * the SHA1 has been calculated. */
1262 back_ptr = retrieve_lte_pointer(lte);
1264 ret = sha1_resource(lte);
1268 /* Look for a duplicate stream */
1269 duplicate_lte = __lookup_resource(lookup_table, lte->hash);
1270 list_del(<e->unhashed_list);
1271 if (duplicate_lte) {
1272 /* We have a duplicate stream. Transfer the reference counts
1273 * from this stream to the duplicate, update the reference to
1274 * this stream (in an inode or ads_entry) to point to the
1275 * duplicate, then free this stream. */
1276 wimlib_assert(!(duplicate_lte->unhashed));
1277 duplicate_lte->refcnt += lte->refcnt;
1278 duplicate_lte->out_refcnt += lte->refcnt;
1279 *back_ptr = duplicate_lte;
1280 free_lookup_table_entry(lte);
1281 lte = duplicate_lte;
1283 /* No duplicate stream, so we need to insert
1284 * this stream into the lookup table and treat
1285 * it as a hashed stream. */
1286 lookup_table_insert(lookup_table, lte);