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/glob.h"
35 #include "wimlib/lookup_table.h"
36 #include "wimlib/metadata.h"
37 #include "wimlib/paths.h"
38 #include "wimlib/resource.h"
39 #include "wimlib/util.h"
40 #include "wimlib/write.h"
45 # include <unistd.h> /* for unlink() */
48 struct wim_lookup_table *
49 new_lookup_table(size_t capacity)
51 struct wim_lookup_table *table;
52 struct hlist_head *array;
54 table = CALLOC(1, sizeof(struct wim_lookup_table));
56 array = CALLOC(capacity, sizeof(array[0]));
58 table->num_entries = 0;
59 table->capacity = capacity;
64 ERROR("Failed to allocate memory for lookup table "
65 "with capacity %zu", capacity);
71 struct wim_lookup_table_entry *
72 new_lookup_table_entry(void)
74 struct wim_lookup_table_entry *lte;
76 lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
81 ERROR("Out of memory (tried to allocate %zu bytes for "
82 "lookup table entry)",
83 sizeof(struct wim_lookup_table_entry));
88 struct wim_lookup_table_entry *
89 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
91 struct wim_lookup_table_entry *new;
93 new = memdup(old, sizeof(struct wim_lookup_table_entry));
97 new->extracted_file = NULL;
98 switch (new->resource_location) {
99 case RESOURCE_IN_FILE_ON_DISK:
101 case RESOURCE_WIN32_ENCRYPTED:
104 case RESOURCE_IN_STAGING_FILE:
105 BUILD_BUG_ON((void*)&old->file_on_disk !=
106 (void*)&old->staging_file_name);
108 new->file_on_disk = TSTRDUP(old->file_on_disk);
109 if (!new->file_on_disk)
112 case RESOURCE_IN_ATTACHED_BUFFER:
113 new->attached_buffer = memdup(old->attached_buffer,
114 wim_resource_size(old));
115 if (!new->attached_buffer)
119 case RESOURCE_IN_NTFS_VOLUME:
121 struct ntfs_location *loc;
122 loc = memdup(old->ntfs_loc, sizeof(struct ntfs_location));
126 loc->stream_name = NULL;
128 loc->path = STRDUP(old->ntfs_loc->path);
131 if (loc->stream_name_nchars) {
132 loc->stream_name = memdup(old->ntfs_loc->stream_name,
133 loc->stream_name_nchars * 2);
134 if (!loc->stream_name)
145 free_lookup_table_entry(new);
150 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
153 switch (lte->resource_location) {
154 case RESOURCE_IN_FILE_ON_DISK:
156 case RESOURCE_WIN32_ENCRYPTED:
159 case RESOURCE_IN_STAGING_FILE:
160 BUILD_BUG_ON((void*)<e->file_on_disk !=
161 (void*)<e->staging_file_name);
163 case RESOURCE_IN_ATTACHED_BUFFER:
164 BUILD_BUG_ON((void*)<e->file_on_disk !=
165 (void*)<e->attached_buffer);
166 FREE(lte->file_on_disk);
169 case RESOURCE_IN_NTFS_VOLUME:
171 FREE(lte->ntfs_loc->path);
172 FREE(lte->ntfs_loc->stream_name);
185 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
187 free_lookup_table_entry(entry);
193 free_lookup_table(struct wim_lookup_table *table)
195 DEBUG2("Freeing lookup table");
198 for_lookup_table_entry(table,
199 do_free_lookup_table_entry,
208 * Inserts an entry into the lookup table.
210 * @table: A pointer to the lookup table.
211 * @lte: A pointer to the entry to insert.
214 lookup_table_insert(struct wim_lookup_table *table,
215 struct wim_lookup_table_entry *lte)
217 size_t i = lte->hash_short % table->capacity;
218 hlist_add_head(<e->hash_list, &table->array[i]);
220 /* XXX Make the table grow when too many entries have been inserted. */
221 table->num_entries++;
225 finalize_lte(struct wim_lookup_table_entry *lte)
228 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
229 unlink(lte->staging_file_name);
230 list_del(<e->unhashed_list);
233 free_lookup_table_entry(lte);
236 /* Decrements the reference count for the lookup table entry @lte. If its
237 * reference count reaches 0, it is unlinked from the lookup table. If,
238 * furthermore, the entry has no opened file descriptors associated with it, the
241 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
242 struct wim_lookup_table *table)
244 wimlib_assert(lte != NULL);
245 wimlib_assert(lte->refcnt != 0);
246 if (--lte->refcnt == 0) {
248 list_del(<e->unhashed_list);
250 lookup_table_unlink(table, lte);
252 if (lte->num_opened_fds == 0)
260 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
262 if (lte->num_opened_fds != 0)
263 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
268 /* Calls a function on all the entries in the WIM lookup table. Stop early and
269 * return nonzero if any call to the function returns nonzero. */
271 for_lookup_table_entry(struct wim_lookup_table *table,
272 int (*visitor)(struct wim_lookup_table_entry *, void *),
275 struct wim_lookup_table_entry *lte;
276 struct hlist_node *pos, *tmp;
279 for (size_t i = 0; i < table->capacity; i++) {
280 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
283 wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
284 ret = visitor(lte, arg);
292 /* qsort() callback that sorts streams (represented by `struct
293 * wim_lookup_table_entry's) into an order optimized for reading and writing.
295 * Sorting is done primarily by resource location, then secondarily by a
296 * per-resource location order. For example, resources in WIM files are sorted
297 * primarily by part number, then secondarily by offset, as to implement optimal
298 * reading of either a standalone or split WIM. */
300 cmp_streams_by_sequential_order(const void *p1, const void *p2)
302 const struct wim_lookup_table_entry *lte1, *lte2;
305 lte1 = *(const struct wim_lookup_table_entry**)p1;
306 lte2 = *(const struct wim_lookup_table_entry**)p2;
308 v = (int)lte1->resource_location - (int)lte2->resource_location;
310 /* Different resource locations? */
314 switch (lte1->resource_location) {
315 case RESOURCE_IN_WIM:
317 /* Different (possibly split) WIMs? */
318 if (lte1->wim != lte2->wim) {
319 v = memcmp(lte1->wim->hdr.guid, lte2->wim->hdr.guid,
325 /* Different part numbers in the same WIM? */
326 v = (int)lte1->wim->hdr.part_number - (int)lte2->wim->hdr.part_number;
330 /* Compare by offset. */
331 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
333 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
336 case RESOURCE_IN_FILE_ON_DISK:
338 case RESOURCE_WIN32_ENCRYPTED:
340 /* Compare files by path: just a heuristic that will place files
341 * in the same directory next to each other. */
342 return tstrcmp(lte1->file_on_disk, lte2->file_on_disk);
344 case RESOURCE_IN_NTFS_VOLUME:
345 return tstrcmp(lte1->ntfs_loc->path, lte2->ntfs_loc->path);
348 /* No additional sorting order defined for this resource
349 * location (e.g. RESOURCE_IN_ATTACHED_BUFFER); simply compare
350 * everything equal to each other. */
356 sort_stream_list_by_sequential_order(struct list_head *stream_list,
357 size_t list_head_offset)
359 struct list_head *cur;
360 struct wim_lookup_table_entry **array;
363 size_t num_streams = 0;
365 list_for_each(cur, stream_list)
368 array_size = num_streams * sizeof(array[0]);
369 array = MALLOC(array_size);
371 return WIMLIB_ERR_NOMEM;
372 cur = stream_list->next;
373 for (i = 0; i < num_streams; i++) {
374 array[i] = (struct wim_lookup_table_entry*)((u8*)cur -
379 qsort(array, num_streams, sizeof(array[0]),
380 cmp_streams_by_sequential_order);
382 INIT_LIST_HEAD(stream_list);
383 for (i = 0; i < num_streams; i++) {
384 list_add_tail((struct list_head*)
385 ((u8*)array[i] + list_head_offset),
394 add_lte_to_array(struct wim_lookup_table_entry *lte,
397 struct wim_lookup_table_entry ***pp = _pp;
402 /* Iterate through the lookup table entries, but first sort them by stream
403 * offset in the WIM. Caution: this is intended to be used when the stream
404 * offset field has actually been set. */
406 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
407 int (*visitor)(struct wim_lookup_table_entry *,
411 struct wim_lookup_table_entry **lte_array, **p;
412 size_t num_streams = table->num_entries;
415 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
417 return WIMLIB_ERR_NOMEM;
419 for_lookup_table_entry(table, add_lte_to_array, &p);
421 wimlib_assert(p == lte_array + num_streams);
423 qsort(lte_array, num_streams, sizeof(lte_array[0]),
424 cmp_streams_by_sequential_order);
426 for (size_t i = 0; i < num_streams; i++) {
427 ret = visitor(lte_array[i], arg);
435 /* On-disk format of a WIM lookup table entry (stream entry). */
436 struct wim_lookup_table_entry_disk {
437 /* Location, offset, compression status, and metadata status of the
439 struct resource_entry_disk resource_entry;
441 /* Which part of the split WIM this stream is in; indexed from 1. */
444 /* Reference count of this stream over all WIM images. */
447 /* SHA1 message digest of the uncompressed data of this stream, or
448 * optionally all zeroes if this stream is of zero length. */
449 u8 hash[SHA1_HASH_SIZE];
452 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
455 lte_init_wim(struct wim_lookup_table_entry *lte, WIMStruct *wim)
457 lte->resource_location = RESOURCE_IN_WIM;
459 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
460 lte->compression_type = wim->compression_type;
462 lte->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
464 if (wim_is_pipable(wim))
469 * Reads the lookup table from a WIM file.
471 * Saves lookup table entries for non-metadata streams in a hash table, and
472 * saves the metadata entry for each image in a special per-image location (the
473 * image_metadata array).
476 * WIMLIB_ERR_SUCCESS (0)
477 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
478 * WIMLIB_ERR_RESOURCE_NOT_FOUND
481 read_wim_lookup_table(WIMStruct *wim)
486 struct wim_lookup_table *table;
487 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
490 BUILD_BUG_ON(sizeof(struct wim_lookup_table_entry_disk) !=
491 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE);
493 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
494 wim->hdr.lookup_table_res_entry.offset,
495 wim->hdr.lookup_table_res_entry.size);
497 /* Calculate number of entries in the lookup table. */
498 num_entries = wim->hdr.lookup_table_res_entry.size /
499 sizeof(struct wim_lookup_table_entry_disk);
502 /* Read the lookup table into a buffer. */
503 ret = res_entry_to_data(&wim->hdr.lookup_table_res_entry, wim, &buf);
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 =
520 &((const struct wim_lookup_table_entry_disk*)buf)[i];
522 cur_entry = new_lookup_table_entry();
524 ERROR("Not enough memory to read lookup table.");
525 ret = WIMLIB_ERR_NOMEM;
526 goto out_free_lookup_table;
529 cur_entry->wim = wim;
530 cur_entry->resource_location = RESOURCE_IN_WIM;
531 get_resource_entry(&disk_entry->resource_entry, &cur_entry->resource_entry);
532 cur_entry->part_number = le16_to_cpu(disk_entry->part_number);
533 cur_entry->refcnt = le32_to_cpu(disk_entry->refcnt);
534 copy_hash(cur_entry->hash, disk_entry->hash);
535 lte_init_wim(cur_entry, wim);
537 if (cur_entry->part_number != wim->hdr.part_number) {
538 WARNING("A lookup table entry in part %hu of the WIM "
539 "points to part %hu (ignoring it)",
540 wim->hdr.part_number, cur_entry->part_number);
541 free_lookup_table_entry(cur_entry);
545 if (is_zero_hash(cur_entry->hash)) {
546 WARNING("The WIM lookup table contains an entry with a "
547 "SHA1 message digest of all 0's (ignoring it)");
548 free_lookup_table_entry(cur_entry);
552 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
553 && (cur_entry->resource_entry.size !=
554 cur_entry->resource_entry.original_size))
556 if (wimlib_print_errors) {
557 WARNING("Found uncompressed resource with "
558 "original size (%"PRIu64") not the same "
559 "as compressed size (%"PRIu64")",
560 cur_entry->resource_entry.original_size,
561 cur_entry->resource_entry.size);
562 if (cur_entry->resource_entry.original_size) {
563 WARNING("Overriding compressed size with original size.");
564 cur_entry->resource_entry.size =
565 cur_entry->resource_entry.original_size;
567 WARNING("Overriding original size with compressed size");
568 cur_entry->resource_entry.original_size =
569 cur_entry->resource_entry.size;
574 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
575 /* Lookup table entry for a metadata resource */
576 if (cur_entry->refcnt != 1) {
577 if (wimlib_print_errors) {
578 ERROR("Found metadata resource with refcnt != 1:");
579 print_lookup_table_entry(cur_entry, stderr);
581 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
582 goto out_free_cur_entry;
585 if (wim->hdr.part_number != 1) {
586 WARNING("Ignoring metadata resource found in a "
587 "non-first part of the split WIM");
588 free_lookup_table_entry(cur_entry);
591 if (wim->current_image == wim->hdr.image_count) {
592 WARNING("The WIM header says there are %u images "
593 "in the WIM, but we found more metadata "
594 "resources than this (ignoring the extra)",
595 wim->hdr.image_count);
596 free_lookup_table_entry(cur_entry);
600 /* Notice very carefully: We are assigning the metadata
601 * resources in the exact order mirrored by their lookup
602 * table entries on disk, which is the behavior of
603 * Microsoft's software. In particular, this overrides
604 * the actual locations of the metadata resources
605 * themselves in the WIM file as well as any information
606 * written in the XML data. */
607 DEBUG("Found metadata resource for image %u at "
609 wim->current_image + 1,
610 cur_entry->resource_entry.offset);
612 wim->current_image++]->metadata_lte = cur_entry;
614 /* Lookup table entry for a stream that is not a
615 * metadata resource */
616 duplicate_entry = lookup_resource(table, cur_entry->hash);
617 if (duplicate_entry) {
618 if (wimlib_print_errors) {
619 WARNING("The WIM lookup table contains two entries with the "
620 "same SHA1 message digest!");
621 WARNING("The first entry is:");
622 print_lookup_table_entry(duplicate_entry, stderr);
623 WARNING("The second entry is:");
624 print_lookup_table_entry(cur_entry, stderr);
626 free_lookup_table_entry(cur_entry);
629 lookup_table_insert(table, cur_entry);
634 if (wim->hdr.part_number == 1 && wim->current_image != wim->hdr.image_count) {
635 WARNING("The header of \"%"TS"\" says there are %u images in\n"
636 " the WIM, but we only found %d metadata resources! Acting as if\n"
637 " the header specified only %d images instead.",
638 wim->filename, wim->hdr.image_count,
639 wim->current_image, wim->current_image);
640 for (int i = wim->current_image; i < wim->hdr.image_count; i++)
641 put_image_metadata(wim->image_metadata[i], NULL);
642 wim->hdr.image_count = wim->current_image;
644 DEBUG("Done reading lookup table.");
645 wim->lookup_table = table;
650 out_free_lookup_table:
651 free_lookup_table(table);
655 wim->current_image = 0;
661 write_wim_lookup_table_entry(const struct wim_lookup_table_entry *lte,
662 struct wim_lookup_table_entry_disk *disk_entry)
664 put_resource_entry(<e->output_resource_entry, &disk_entry->resource_entry);
665 disk_entry->part_number = cpu_to_le16(lte->part_number);
666 disk_entry->refcnt = cpu_to_le32(lte->out_refcnt);
667 copy_hash(disk_entry->hash, lte->hash);
671 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
672 struct filedes *out_fd,
673 struct resource_entry *out_res_entry,
674 int write_resource_flags)
677 struct wim_lookup_table_entry *lte;
678 struct wim_lookup_table_entry_disk *table_buf;
679 struct wim_lookup_table_entry_disk *table_buf_ptr;
683 list_for_each_entry(lte, stream_list, lookup_table_list)
684 table_size += sizeof(struct wim_lookup_table_entry_disk);
686 DEBUG("Writing WIM lookup table (size=%zu, offset=%"PRIu64")",
687 table_size, out_fd->offset);
689 table_buf = MALLOC(table_size);
691 ERROR("Failed to allocate %zu bytes for temporary lookup table",
693 return WIMLIB_ERR_NOMEM;
695 table_buf_ptr = table_buf;
696 list_for_each_entry(lte, stream_list, lookup_table_list)
697 write_wim_lookup_table_entry(lte, table_buf_ptr++);
699 /* Write the lookup table uncompressed. Although wimlib can handle a
700 * compressed lookup table, MS software cannot. */
701 ret = write_wim_resource_from_buffer(table_buf,
703 WIM_RESHDR_FLAG_METADATA,
705 WIMLIB_COMPRESSION_TYPE_NONE,
708 write_resource_flags);
714 append_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_list)
716 /* Lookup table entries with 'out_refcnt' == 0 correspond to streams not
717 * written and not present in the resulting WIM file, and should not be
718 * included in the lookup table.
720 * Lookup table entries marked as filtered (EXTERNAL_WIM) with
721 * 'out_refcnt != 0' were referenced as part of the logical write but
722 * correspond to streams that were not in fact written, and should not
723 * be included in the lookup table.
725 * Lookup table entries marked as filtered (SAME_WIM) with 'out_refcnt
726 * != 0' were referenced as part of the logical write but correspond to
727 * streams that were not in fact written, but nevertheless were already
728 * present in the WIM being overwritten in-place. These entries must be
729 * included in the lookup table, and the resource information to write
730 * needs to be copied from the resource information read originally.
732 if (lte->out_refcnt != 0 && !(lte->filtered & FILTERED_EXTERNAL_WIM)) {
733 if (lte->filtered & FILTERED_SAME_WIM) {
734 copy_resource_entry(<e->output_resource_entry,
735 <e->resource_entry);
737 list_add_tail(<e->lookup_table_list, (struct list_head*)_list);
743 write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
744 struct resource_entry *out_res_entry,
745 struct list_head *stream_list_override)
747 int write_resource_flags;
748 struct list_head _stream_list;
749 struct list_head *stream_list;
751 if (stream_list_override) {
752 stream_list = stream_list_override;
754 stream_list = &_stream_list;
755 INIT_LIST_HEAD(stream_list);
758 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_METADATA)) {
762 if (image == WIMLIB_ALL_IMAGES) {
764 end_image = wim->hdr.image_count;
770 /* Push metadata resource lookup table entries onto the front of
771 * the list in reverse order, so that they're written in order.
773 for (int i = end_image; i >= start_image; i--) {
774 struct wim_lookup_table_entry *metadata_lte;
776 metadata_lte = wim->image_metadata[i - 1]->metadata_lte;
777 metadata_lte->out_refcnt = 1;
778 metadata_lte->part_number = wim->hdr.part_number;
779 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
781 list_add(&metadata_lte->lookup_table_list, stream_list);
785 /* Append additional lookup table entries that need to be written, with
786 * some special handling for streams that have been marked as filtered.
788 if (!stream_list_override) {
789 for_lookup_table_entry(wim->lookup_table,
790 append_lookup_table_entry, stream_list);
793 write_resource_flags = 0;
794 if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
795 write_resource_flags |= WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE;
796 return write_wim_lookup_table_from_stream_list(stream_list,
799 write_resource_flags);
804 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
806 lte->real_refcnt = 0;
811 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
818 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
820 if (lte->extracted_file != NULL) {
821 FREE(lte->extracted_file);
822 lte->extracted_file = NULL;
828 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
834 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
835 lte->resource_entry.offset);
837 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
838 (u64)lte->resource_entry.size);
840 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
841 lte->resource_entry.original_size);
843 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
844 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
847 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
848 lte->back_inode, lte->back_stream_id);
850 tfprintf(out, T("Hash = 0x"));
851 print_hash(lte->hash, out);
855 tfprintf(out, T("Flags = "));
856 u8 flags = lte->resource_entry.flags;
857 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
858 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
859 if (flags & WIM_RESHDR_FLAG_FREE)
860 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
861 if (flags & WIM_RESHDR_FLAG_METADATA)
862 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
863 if (flags & WIM_RESHDR_FLAG_SPANNED)
864 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
866 switch (lte->resource_location) {
867 case RESOURCE_IN_WIM:
868 if (lte->wim->filename) {
869 tfprintf(out, T("WIM file = `%"TS"'\n"),
874 case RESOURCE_WIN32_ENCRYPTED:
876 case RESOURCE_IN_FILE_ON_DISK:
877 tfprintf(out, T("File on Disk = `%"TS"'\n"),
881 case RESOURCE_IN_STAGING_FILE:
882 tfprintf(out, T("Staging File = `%"TS"'\n"),
883 lte->staging_file_name);
893 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
894 struct wimlib_resource_entry *wentry)
896 wentry->uncompressed_size = lte->resource_entry.original_size;
897 wentry->compressed_size = lte->resource_entry.size;
898 wentry->offset = lte->resource_entry.offset;
899 copy_hash(wentry->sha1_hash, lte->hash);
900 wentry->part_number = lte->part_number;
901 wentry->reference_count = lte->refcnt;
902 wentry->is_compressed = (lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
903 wentry->is_metadata = (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) != 0;
904 wentry->is_free = (lte->resource_entry.flags & WIM_RESHDR_FLAG_FREE) != 0;
905 wentry->is_spanned = (lte->resource_entry.flags & WIM_RESHDR_FLAG_SPANNED) != 0;
908 struct iterate_lte_context {
909 wimlib_iterate_lookup_table_callback_t cb;
914 do_iterate_lte(struct wim_lookup_table_entry *lte, void *_ctx)
916 struct iterate_lte_context *ctx = _ctx;
917 struct wimlib_resource_entry entry;
919 lte_to_wimlib_resource_entry(lte, &entry);
920 return (*ctx->cb)(&entry, ctx->user_ctx);
923 /* API function documented in wimlib.h */
925 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
926 wimlib_iterate_lookup_table_callback_t cb,
929 struct iterate_lte_context ctx = {
931 .user_ctx = user_ctx,
933 if (wim->hdr.part_number == 1) {
935 for (int i = 0; i < wim->hdr.image_count; i++) {
936 ret = do_iterate_lte(wim->image_metadata[i]->metadata_lte,
942 return for_lookup_table_entry(wim->lookup_table, do_iterate_lte, &ctx);
945 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
946 * lookup table, or NULL if there is none. */
947 struct wim_lookup_table_entry *
948 lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
951 struct wim_lookup_table_entry *lte;
952 struct hlist_node *pos;
954 wimlib_assert(table != NULL);
955 wimlib_assert(hash != NULL);
957 i = *(size_t*)hash % table->capacity;
958 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
959 if (hashes_equal(hash, lte->hash))
966 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
969 * This is only for pre-resolved inodes.
972 wim_pathname_to_stream(WIMStruct *wim,
975 struct wim_dentry **dentry_ret,
976 struct wim_lookup_table_entry **lte_ret,
979 struct wim_dentry *dentry;
980 struct wim_lookup_table_entry *lte;
982 const tchar *stream_name = NULL;
983 struct wim_inode *inode;
986 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
987 stream_name = path_stream_name(path);
989 p = (tchar*)stream_name - 1;
994 dentry = get_dentry(wim, path);
1000 inode = dentry->d_inode;
1002 if (!inode->i_resolved)
1003 if (inode_resolve_ltes(inode, wim->lookup_table, false))
1006 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
1007 && inode_is_directory(inode))
1011 struct wim_ads_entry *ads_entry;
1013 ads_entry = inode_get_ads_entry(inode, stream_name,
1016 stream_idx = ads_idx + 1;
1017 lte = ads_entry->lte;
1028 *dentry_ret = dentry;
1032 *stream_idx_ret = stream_idx;
1038 resource_not_found_error(struct wim_inode *inode, const u8 *hash)
1040 if (wimlib_print_errors) {
1041 ERROR("\"%"TS"\": resource not found", inode_first_full_path(inode));
1042 tfprintf(stderr, T(" SHA-1 message digest of missing resource:\n "));
1043 print_hash(hash, stderr);
1044 tputc(T('\n'), stderr);
1046 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
1050 * Resolve an inode's lookup table entries.
1052 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
1053 * lookup table) with pointers directly to the lookup table entries.
1055 * If @force is %false:
1056 * If any needed SHA1 message digests are not found in the lookup table,
1057 * WIMLIB_ERR_RESOURCE_NOT_FOUND is returned and the inode is left
1059 * If @force is %true:
1060 * If any needed SHA1 message digests are not found in the lookup table,
1061 * new entries are allocated and inserted into the lookup table.
1064 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
1069 if (!inode->i_resolved) {
1070 struct wim_lookup_table_entry *lte, *ads_lte;
1072 /* Resolve the default file stream */
1074 hash = inode->i_hash;
1075 if (!is_zero_hash(hash)) {
1076 lte = lookup_resource(table, hash);
1079 lte = new_lookup_table_entry();
1081 return WIMLIB_ERR_NOMEM;
1082 copy_hash(lte->hash, hash);
1083 lookup_table_insert(table, lte);
1085 goto resource_not_found;
1090 /* Resolve the alternate data streams */
1091 struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
1092 for (u16 i = 0; i < inode->i_num_ads; i++) {
1093 struct wim_ads_entry *cur_entry;
1096 cur_entry = &inode->i_ads_entries[i];
1097 hash = cur_entry->hash;
1098 if (!is_zero_hash(hash)) {
1099 ads_lte = lookup_resource(table, hash);
1102 ads_lte = new_lookup_table_entry();
1104 return WIMLIB_ERR_NOMEM;
1105 copy_hash(ads_lte->hash, hash);
1106 lookup_table_insert(table, ads_lte);
1108 goto resource_not_found;
1112 ads_ltes[i] = ads_lte;
1115 for (u16 i = 0; i < inode->i_num_ads; i++)
1116 inode->i_ads_entries[i].lte = ads_ltes[i];
1117 inode->i_resolved = 1;
1122 return resource_not_found_error(inode, hash);
1126 inode_unresolve_ltes(struct wim_inode *inode)
1128 if (inode->i_resolved) {
1130 copy_hash(inode->i_hash, inode->i_lte->hash);
1132 zero_out_hash(inode->i_hash);
1134 for (u16 i = 0; i < inode->i_num_ads; i++) {
1135 if (inode->i_ads_entries[i].lte)
1136 copy_hash(inode->i_ads_entries[i].hash,
1137 inode->i_ads_entries[i].lte->hash);
1139 zero_out_hash(inode->i_ads_entries[i].hash);
1141 inode->i_resolved = 0;
1146 * Returns the lookup table entry for stream @stream_idx of the inode, where
1147 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
1148 * corresponds to an alternate data stream.
1150 * This works for both resolved and un-resolved inodes.
1152 struct wim_lookup_table_entry *
1153 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
1154 const struct wim_lookup_table *table)
1156 if (inode->i_resolved)
1157 return inode_stream_lte_resolved(inode, stream_idx);
1159 return inode_stream_lte_unresolved(inode, stream_idx, table);
1162 struct wim_lookup_table_entry *
1163 inode_unnamed_lte_resolved(const struct wim_inode *inode)
1165 wimlib_assert(inode->i_resolved);
1166 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1167 if (inode_stream_name_nbytes(inode, i) == 0 &&
1168 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
1170 return inode_stream_lte_resolved(inode, i);
1176 struct wim_lookup_table_entry *
1177 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
1178 const struct wim_lookup_table *table)
1180 wimlib_assert(!inode->i_resolved);
1181 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1182 if (inode_stream_name_nbytes(inode, i) == 0 &&
1183 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
1185 return inode_stream_lte_unresolved(inode, i, table);
1191 /* Return the lookup table entry for the unnamed data stream of an inode, or
1192 * NULL if there is none.
1194 * You'd think this would be easier than it actually is, since the unnamed data
1195 * stream should be the one referenced from the inode itself. Alas, if there
1196 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
1197 * data stream in one of the alternate data streams instead of inside the WIM
1198 * dentry itself. So we need to check the alternate data streams too.
1200 * Also, note that a dentry may appear to have more than one unnamed stream, but
1201 * if the SHA1 message digest is all 0's then the corresponding stream does not
1202 * really "count" (this is the case for the inode's own file stream when the
1203 * file stream that should be there is actually in one of the alternate stream
1204 * entries.). This is despite the fact that we may need to extract such a
1205 * missing entry as an empty file or empty named data stream.
1207 struct wim_lookup_table_entry *
1208 inode_unnamed_lte(const struct wim_inode *inode,
1209 const struct wim_lookup_table *table)
1211 if (inode->i_resolved)
1212 return inode_unnamed_lte_resolved(inode);
1214 return inode_unnamed_lte_unresolved(inode, table);
1218 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
1220 *(u64*)total_bytes_p += lte->resource_entry.size;
1225 lookup_table_total_stream_size(struct wim_lookup_table *table)
1228 for_lookup_table_entry(table, lte_add_stream_size, &total_size);
1232 struct wim_lookup_table_entry **
1233 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
1235 wimlib_assert(lte->unhashed);
1236 struct wim_inode *inode = lte->back_inode;
1237 u32 stream_id = lte->back_stream_id;
1239 return &inode->i_lte;
1241 for (u16 i = 0; i < inode->i_num_ads; i++)
1242 if (inode->i_ads_entries[i].stream_id == stream_id)
1243 return &inode->i_ads_entries[i].lte;
1248 /* Calculate the SHA1 message digest of a stream and move it from the list of
1249 * unhashed streams to the stream lookup table, possibly joining it with an
1250 * existing lookup table entry for an identical stream.
1252 * @lte: An unhashed lookup table entry.
1253 * @lookup_table: Lookup table for the WIM.
1254 * @lte_ret: On success, write a pointer to the resulting lookup table
1255 * entry to this location. This will be the same as @lte
1256 * if it was inserted into the lookup table, or different if
1257 * a duplicate stream was found.
1259 * Returns 0 on success; nonzero if there is an error reading the stream.
1262 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
1263 struct wim_lookup_table *lookup_table,
1264 struct wim_lookup_table_entry **lte_ret)
1267 struct wim_lookup_table_entry *duplicate_lte;
1268 struct wim_lookup_table_entry **back_ptr;
1270 wimlib_assert(lte->unhashed);
1272 /* back_ptr must be saved because @back_inode and @back_stream_id are in
1273 * union with the SHA1 message digest and will no longer be valid once
1274 * the SHA1 has been calculated. */
1275 back_ptr = retrieve_lte_pointer(lte);
1277 ret = sha1_resource(lte);
1281 /* Look for a duplicate stream */
1282 duplicate_lte = lookup_resource(lookup_table, lte->hash);
1283 list_del(<e->unhashed_list);
1284 if (duplicate_lte) {
1285 /* We have a duplicate stream. Transfer the reference counts
1286 * from this stream to the duplicate, update the reference to
1287 * this stream (in an inode or ads_entry) to point to the
1288 * duplicate, then free this stream. */
1289 wimlib_assert(!(duplicate_lte->unhashed));
1290 duplicate_lte->refcnt += lte->refcnt;
1291 duplicate_lte->out_refcnt += lte->out_refcnt;
1292 *back_ptr = duplicate_lte;
1293 free_lookup_table_entry(lte);
1294 lte = duplicate_lte;
1296 /* No duplicate stream, so we need to insert
1297 * this stream into the lookup table and treat
1298 * it as a hashed stream. */
1299 lookup_table_insert(lookup_table, lte);
1308 lte_clone_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
1310 struct wim_lookup_table *lookup_table = _lookup_table;
1312 if (lookup_resource(lookup_table, lte->hash))
1313 return 0; /* Resource already present. */
1315 lte = clone_lookup_table_entry(lte);
1317 return WIMLIB_ERR_NOMEM;
1318 lte->out_refcnt = 1;
1319 lookup_table_insert(lookup_table, lte);
1324 lte_delete_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
1326 struct wim_lookup_table *lookup_table = _lookup_table;
1328 if (lte->out_refcnt) {
1329 lookup_table_unlink(lookup_table, lte);
1330 free_lookup_table_entry(lte);
1335 /* API function documented in wimlib.h */
1337 wimlib_reference_resources(WIMStruct *wim,
1338 WIMStruct **resource_wims, unsigned num_resource_wims,
1345 return WIMLIB_ERR_INVALID_PARAM;
1347 if (num_resource_wims != 0 && resource_wims == NULL)
1348 return WIMLIB_ERR_INVALID_PARAM;
1350 for (i = 0; i < num_resource_wims; i++)
1351 if (resource_wims[i] == NULL)
1352 return WIMLIB_ERR_INVALID_PARAM;
1354 for_lookup_table_entry(wim->lookup_table, lte_zero_out_refcnt, NULL);
1356 for (i = 0; i < num_resource_wims; i++) {
1357 ret = for_lookup_table_entry(resource_wims[i]->lookup_table,
1366 for_lookup_table_entry(wim->lookup_table, lte_delete_if_new,
1372 reference_resource_paths(WIMStruct *wim,
1373 const tchar * const *resource_wimfiles,
1374 unsigned num_resource_wimfiles,
1377 wimlib_progress_func_t progress_func)
1379 WIMStruct **resource_wims;
1383 open_flags |= WIMLIB_OPEN_FLAG_SPLIT_OK;
1385 resource_wims = CALLOC(num_resource_wimfiles, sizeof(resource_wims[0]));
1387 return WIMLIB_ERR_NOMEM;
1389 for (i = 0; i < num_resource_wimfiles; i++) {
1390 DEBUG("Referencing resources from path \"%"TS"\"",
1391 resource_wimfiles[i]);
1392 ret = wimlib_open_wim(resource_wimfiles[i], open_flags,
1393 &resource_wims[i], progress_func);
1395 goto out_free_resource_wims;
1398 ret = wimlib_reference_resources(wim, resource_wims,
1399 num_resource_wimfiles, ref_flags);
1401 goto out_free_resource_wims;
1403 for (i = 0; i < num_resource_wimfiles; i++)
1404 list_add_tail(&resource_wims[i]->subwim_node, &wim->subwims);
1407 goto out_free_array;
1409 out_free_resource_wims:
1410 for (i = 0; i < num_resource_wimfiles; i++)
1411 wimlib_free(resource_wims[i]);
1413 FREE(resource_wims);
1418 reference_resource_glob(WIMStruct *wim, const tchar *refglob,
1419 int ref_flags, int open_flags,
1420 wimlib_progress_func_t progress_func)
1425 /* Note: glob() is replaced in Windows native builds. */
1426 ret = tglob(refglob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1428 if (ret == GLOB_NOMATCH) {
1429 if (ref_flags & WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH) {
1430 ERROR("Found no files for glob \"%"TS"\"", refglob);
1431 return WIMLIB_ERR_GLOB_HAD_NO_MATCHES;
1433 return reference_resource_paths(wim,
1441 ERROR_WITH_ERRNO("Failed to process glob \"%"TS"\"", refglob);
1442 if (ret == GLOB_NOSPACE)
1443 return WIMLIB_ERR_NOMEM;
1445 return WIMLIB_ERR_READ;
1449 ret = reference_resource_paths(wim,
1450 (const tchar * const *)globbuf.gl_pathv,
1459 /* API function documented in wimlib.h */
1461 wimlib_reference_resource_files(WIMStruct *wim,
1462 const tchar * const * resource_wimfiles_or_globs,
1466 wimlib_progress_func_t progress_func)
1471 if (ref_flags & WIMLIB_REF_FLAG_GLOB_ENABLE) {
1472 for (i = 0; i < count; i++) {
1473 ret = reference_resource_glob(wim,
1474 resource_wimfiles_or_globs[i],
1483 return reference_resource_paths(wim, resource_wimfiles_or_globs,
1485 open_flags, progress_func);