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));
78 ERROR("Out of memory (tried to allocate %zu bytes for "
79 "lookup table entry)",
80 sizeof(struct wim_lookup_table_entry));
84 BUILD_BUG_ON(RESOURCE_NONEXISTENT != 0);
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) {
100 list_add(&new->rspec_node, &new->rspec->stream_list);
103 case RESOURCE_IN_FILE_ON_DISK:
105 case RESOURCE_WIN32_ENCRYPTED:
108 case RESOURCE_IN_STAGING_FILE:
109 BUILD_BUG_ON((void*)&old->file_on_disk !=
110 (void*)&old->staging_file_name);
112 new->file_on_disk = TSTRDUP(old->file_on_disk);
113 if (new->file_on_disk == NULL)
116 case RESOURCE_IN_ATTACHED_BUFFER:
117 new->attached_buffer = memdup(old->attached_buffer, old->size);
118 if (new->attached_buffer == NULL)
122 case RESOURCE_IN_NTFS_VOLUME:
124 struct ntfs_location *loc;
125 loc = memdup(old->ntfs_loc, sizeof(struct ntfs_location));
129 loc->stream_name = NULL;
131 loc->path = STRDUP(old->ntfs_loc->path);
132 if (loc->path == NULL)
134 if (loc->stream_name_nchars != 0) {
135 loc->stream_name = memdup(old->ntfs_loc->stream_name,
136 loc->stream_name_nchars * 2);
137 if (loc->stream_name == NULL)
148 free_lookup_table_entry(new);
153 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
156 switch (lte->resource_location) {
157 case RESOURCE_IN_WIM:
158 list_del(<e->rspec_node);
159 if (list_empty(<e->rspec->stream_list))
162 case RESOURCE_IN_FILE_ON_DISK:
164 case RESOURCE_WIN32_ENCRYPTED:
167 case RESOURCE_IN_STAGING_FILE:
168 BUILD_BUG_ON((void*)<e->file_on_disk !=
169 (void*)<e->staging_file_name);
171 case RESOURCE_IN_ATTACHED_BUFFER:
172 BUILD_BUG_ON((void*)<e->file_on_disk !=
173 (void*)<e->attached_buffer);
174 FREE(lte->file_on_disk);
177 case RESOURCE_IN_NTFS_VOLUME:
179 FREE(lte->ntfs_loc->path);
180 FREE(lte->ntfs_loc->stream_name);
193 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
195 free_lookup_table_entry(entry);
201 free_lookup_table(struct wim_lookup_table *table)
203 DEBUG("Freeing lookup table.");
206 for_lookup_table_entry(table,
207 do_free_lookup_table_entry,
216 * Inserts an entry into the lookup table.
218 * @table: A pointer to the lookup table.
219 * @lte: A pointer to the entry to insert.
222 lookup_table_insert(struct wim_lookup_table *table,
223 struct wim_lookup_table_entry *lte)
225 size_t i = lte->hash_short % table->capacity;
226 hlist_add_head(<e->hash_list, &table->array[i]);
228 /* XXX Make the table grow when too many entries have been inserted. */
229 table->num_entries++;
233 finalize_lte(struct wim_lookup_table_entry *lte)
236 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
237 unlink(lte->staging_file_name);
238 list_del(<e->unhashed_list);
241 free_lookup_table_entry(lte);
244 /* Decrements the reference count for the lookup table entry @lte. If its
245 * reference count reaches 0, it is unlinked from the lookup table. If,
246 * furthermore, the entry has no opened file descriptors associated with it, the
249 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
250 struct wim_lookup_table *table)
252 wimlib_assert(lte != NULL);
253 wimlib_assert(lte->refcnt != 0);
254 if (--lte->refcnt == 0) {
256 list_del(<e->unhashed_list);
258 lookup_table_unlink(table, lte);
260 if (lte->num_opened_fds == 0)
268 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
270 if (lte->num_opened_fds != 0)
271 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
276 /* Calls a function on all the entries in the WIM lookup table. Stop early and
277 * return nonzero if any call to the function returns nonzero. */
279 for_lookup_table_entry(struct wim_lookup_table *table,
280 int (*visitor)(struct wim_lookup_table_entry *, void *),
283 struct wim_lookup_table_entry *lte;
284 struct hlist_node *pos, *tmp;
287 for (size_t i = 0; i < table->capacity; i++) {
288 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
291 wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
292 ret = visitor(lte, arg);
300 /* qsort() callback that sorts streams (represented by `struct
301 * wim_lookup_table_entry's) into an order optimized for reading.
303 * Sorting is done primarily by resource location, then secondarily by a
304 * per-resource location order. For example, resources in WIM files are sorted
305 * primarily by part number, then secondarily by offset, as to implement optimal
306 * reading of either a standalone or split WIM. */
308 cmp_streams_by_sequential_order(const void *p1, const void *p2)
310 const struct wim_lookup_table_entry *lte1, *lte2;
312 WIMStruct *wim1, *wim2;
314 lte1 = *(const struct wim_lookup_table_entry**)p1;
315 lte2 = *(const struct wim_lookup_table_entry**)p2;
317 v = (int)lte1->resource_location - (int)lte2->resource_location;
319 /* Different resource locations? */
323 switch (lte1->resource_location) {
324 case RESOURCE_IN_WIM:
325 wim1 = lte1->rspec->wim;
326 wim2 = lte2->rspec->wim;
328 /* Different (possibly split) WIMs? */
330 v = memcmp(wim1->hdr.guid, wim2->hdr.guid, WIM_GID_LEN);
335 /* Different part numbers in the same WIM? */
336 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
340 if (lte1->rspec->offset_in_wim != lte2->rspec->offset_in_wim)
341 return cmp_u64(lte1->rspec->offset_in_wim,
342 lte2->rspec->offset_in_wim);
344 return cmp_u64(lte1->offset_in_res, lte2->offset_in_res);
346 case RESOURCE_IN_FILE_ON_DISK:
348 case RESOURCE_IN_STAGING_FILE:
351 case RESOURCE_WIN32_ENCRYPTED:
353 /* Compare files by path: just a heuristic that will place files
354 * in the same directory next to each other. */
355 return tstrcmp(lte1->file_on_disk, lte2->file_on_disk);
357 case RESOURCE_IN_NTFS_VOLUME:
358 return tstrcmp(lte1->ntfs_loc->path, lte2->ntfs_loc->path);
361 /* No additional sorting order defined for this resource
362 * location (e.g. RESOURCE_IN_ATTACHED_BUFFER); simply compare
363 * everything equal to each other. */
369 sort_stream_list(struct list_head *stream_list,
370 size_t list_head_offset,
371 int (*compar)(const void *, const void*))
373 struct list_head *cur;
374 struct wim_lookup_table_entry **array;
377 size_t num_streams = 0;
379 list_for_each(cur, stream_list)
382 if (num_streams <= 1)
385 array_size = num_streams * sizeof(array[0]);
386 array = MALLOC(array_size);
388 return WIMLIB_ERR_NOMEM;
390 cur = stream_list->next;
391 for (i = 0; i < num_streams; i++) {
392 array[i] = (struct wim_lookup_table_entry*)((u8*)cur -
397 qsort(array, num_streams, sizeof(array[0]), compar);
399 INIT_LIST_HEAD(stream_list);
400 for (i = 0; i < num_streams; i++) {
401 list_add_tail((struct list_head*)
402 ((u8*)array[i] + list_head_offset),
409 /* Sort the specified list of streams in an order optimized for reading. */
411 sort_stream_list_by_sequential_order(struct list_head *stream_list,
412 size_t list_head_offset)
414 return sort_stream_list(stream_list, list_head_offset,
415 cmp_streams_by_sequential_order);
420 add_lte_to_array(struct wim_lookup_table_entry *lte,
423 struct wim_lookup_table_entry ***pp = _pp;
428 /* Iterate through the lookup table entries, but first sort them by stream
429 * offset in the WIM. Caution: this is intended to be used when the stream
430 * offset field has actually been set. */
432 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
433 int (*visitor)(struct wim_lookup_table_entry *,
437 struct wim_lookup_table_entry **lte_array, **p;
438 size_t num_streams = table->num_entries;
441 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
443 return WIMLIB_ERR_NOMEM;
445 for_lookup_table_entry(table, add_lte_to_array, &p);
447 wimlib_assert(p == lte_array + num_streams);
449 qsort(lte_array, num_streams, sizeof(lte_array[0]),
450 cmp_streams_by_sequential_order);
452 for (size_t i = 0; i < num_streams; i++) {
453 ret = visitor(lte_array[i], arg);
461 /* On-disk format of a WIM lookup table entry (stream entry). */
462 struct wim_lookup_table_entry_disk {
463 /* Size, offset, and flags of the stream. */
464 struct wim_reshdr_disk reshdr;
466 /* Which part of the split WIM this stream is in; indexed from 1. */
469 /* Reference count of this stream over all WIM images. */
472 /* SHA1 message digest of the uncompressed data of this stream, or
473 * optionally all zeroes if this stream is of zero length. */
474 u8 hash[SHA1_HASH_SIZE];
477 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
479 /* Validate the size and location of a WIM resource. */
481 validate_resource(const struct wim_resource_spec *rspec)
483 struct wim_lookup_table_entry *lte;
486 /* Verify that calculating the offset of the end of the resource doesn't
488 if (rspec->offset_in_wim + rspec->size_in_wim < rspec->size_in_wim)
491 /* Verify that each stream in the resource has a valid offset and size,
492 * and that no streams overlap, and that the streams were added in order
493 * of increasing offset. */
495 list_for_each_entry(lte, &rspec->stream_list, rspec_node) {
496 if (lte->offset_in_res + lte->size < lte->size ||
497 lte->offset_in_res + lte->size > rspec->uncompressed_size ||
498 lte->offset_in_res < cur_offset)
501 cur_offset = lte->offset_in_res + lte->size;
507 ERROR("Invalid resource entry!");
508 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
512 * Reads the lookup table from a WIM file. Each entry specifies a stream that
513 * the WIM file contains, along with its location and SHA1 message digest.
515 * Saves lookup table entries for non-metadata streams in a hash table, and
516 * saves the metadata entry for each image in a special per-image location (the
517 * image_metadata array).
520 * WIMLIB_ERR_SUCCESS (0)
521 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
522 * WIMLIB_ERR_RESOURCE_NOT_FOUND
524 * Or an error code caused by failure to read the lookup table into memory.
527 read_wim_lookup_table(WIMStruct *wim)
532 struct wim_lookup_table *table;
533 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
534 struct wim_resource_spec *cur_rspec;
536 bool back_to_back_pack;
538 DEBUG("Reading lookup table.");
540 /* Sanity check: lookup table entries are 50 bytes each. */
541 BUILD_BUG_ON(sizeof(struct wim_lookup_table_entry_disk) !=
542 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE);
544 /* Calculate number of entries in the lookup table. */
545 num_entries = wim->hdr.lookup_table_reshdr.uncompressed_size /
546 sizeof(struct wim_lookup_table_entry_disk);
548 /* Read the lookup table into a buffer. */
549 ret = wim_reshdr_to_data(&wim->hdr.lookup_table_reshdr, wim, &buf);
553 /* Allocate a hash table to map SHA1 message digests into stream
554 * specifications. This is the in-memory "lookup table". */
555 table = new_lookup_table(num_entries * 2 + 1);
557 ERROR("Not enough memory to read lookup table.");
558 ret = WIMLIB_ERR_NOMEM;
562 /* Allocate and initalize stream entries from the raw lookup table
564 wim->current_image = 0;
566 for (i = 0; i < num_entries; i++) {
567 const struct wim_lookup_table_entry_disk *disk_entry =
568 &((const struct wim_lookup_table_entry_disk*)buf)[i];
570 struct wim_reshdr reshdr;
572 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
574 DEBUG("reshdr: size_in_wim=%"PRIu64", "
575 "uncompressed_size=%"PRIu64", "
576 "offset_in_wim=%"PRIu64", "
578 reshdr.size_in_wim, reshdr.uncompressed_size,
579 reshdr.offset_in_wim, reshdr.flags);
581 cur_entry = new_lookup_table_entry();
582 if (cur_entry == NULL) {
583 ERROR("Not enough memory to read lookup table!");
584 ret = WIMLIB_ERR_NOMEM;
585 goto out_free_lookup_table;
588 part_number = le16_to_cpu(disk_entry->part_number);
589 cur_entry->refcnt = le32_to_cpu(disk_entry->refcnt);
590 copy_hash(cur_entry->hash, disk_entry->hash);
592 if (part_number != wim->hdr.part_number) {
593 WARNING("A lookup table entry in part %hu of the WIM "
594 "points to part %hu (ignoring it)",
595 wim->hdr.part_number, part_number);
596 free_lookup_table_entry(cur_entry);
600 if (!(reshdr.flags & (WIM_RESHDR_FLAG_PACKED_STREAMS |
601 WIM_RESHDR_FLAG_COMPRESSED))) {
602 if (reshdr.uncompressed_size != reshdr.size_in_wim) {
603 ERROR("Invalid resource entry!");
604 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
605 goto out_free_cur_entry;
609 back_to_back_pack = false;
610 if (!(reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) ||
613 ((reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
614 reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER &&
616 cur_rspec->size_in_wim != 0)))
618 /* Starting new run of streams that share the same WIM
620 struct wim_lookup_table_entry *prev_entry = NULL;
622 if (back_to_back_pack) {
623 prev_entry = list_entry(cur_rspec->stream_list.prev,
624 struct wim_lookup_table_entry,
626 lte_unbind_wim_resource_spec(prev_entry);
627 cur_rspec->uncompressed_size -= prev_entry->size;
629 if (cur_rspec != NULL) {
630 ret = validate_resource(cur_rspec);
632 goto out_free_cur_entry;
635 /* Allocate the resource specification and initialize it
636 * with values from the current stream entry. */
637 cur_rspec = MALLOC(sizeof(*cur_rspec));
638 if (cur_rspec == NULL) {
639 ERROR("Not enough memory to read lookup table!");
640 ret = WIMLIB_ERR_NOMEM;
641 goto out_free_cur_entry;
643 wim_res_hdr_to_spec(&reshdr, wim, cur_rspec);
645 /* If this is a packed run, the current stream entry may
646 * specify a stream within the resource, and not the
647 * resource itself. Zero possibly irrelevant data until
648 * it is read for certain. */
649 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
650 cur_rspec->size_in_wim = 0;
651 cur_rspec->uncompressed_size = 0;
652 cur_rspec->offset_in_wim = 0;
656 lte_bind_wim_resource_spec(prev_entry, cur_rspec);
657 cur_rspec->uncompressed_size = prev_entry->size;
661 if ((reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
662 reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER)
664 /* Found the specification for the packed resource.
665 * Transfer the values to the `struct
666 * wim_resource_spec', and discard the current stream
667 * since this lookup table entry did not, in fact,
668 * correspond to a "stream". */
670 cur_rspec->offset_in_wim = reshdr.offset_in_wim;
671 cur_rspec->size_in_wim = reshdr.size_in_wim;
672 cur_rspec->flags = reshdr.flags;
673 DEBUG("Full pack is %"PRIu64" compressed bytes "
674 "at file offset %"PRIu64" (flags 0x%02x)",
675 cur_rspec->size_in_wim,
676 cur_rspec->offset_in_wim,
678 free_lookup_table_entry(cur_entry);
682 if (is_zero_hash(cur_entry->hash)) {
683 free_lookup_table_entry(cur_entry);
687 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
688 /* Continuing the pack with another stream. */
689 DEBUG("Continuing packed run with stream: "
690 "%"PRIu64" uncompressed bytes @ resource offset %"PRIu64")",
691 reshdr.size_in_wim, reshdr.offset_in_wim);
692 cur_rspec->uncompressed_size += reshdr.size_in_wim;
695 lte_bind_wim_resource_spec(cur_entry, cur_rspec);
696 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
697 /* In packed runs, the offset field is used for
698 * in-resource offset, not the in-WIM offset, and the
699 * size field is used for the uncompressed size, not the
700 * compressed size. */
701 cur_entry->offset_in_res = reshdr.offset_in_wim;
702 cur_entry->size = reshdr.size_in_wim;
703 cur_entry->flags = reshdr.flags;
705 /* Normal case: The stream corresponds one-to-one with
706 * the resource entry. */
707 cur_entry->offset_in_res = 0;
708 cur_entry->size = reshdr.uncompressed_size;
709 cur_entry->flags = reshdr.flags;
713 if (cur_entry->flags & WIM_RESHDR_FLAG_METADATA) {
714 /* Lookup table entry for a metadata resource */
716 /* Metadata entries with no references must be ignored;
717 * see for example the WinPE WIMs from the WAIK v2.1.
719 if (cur_entry->refcnt == 0) {
720 free_lookup_table_entry(cur_entry);
724 if (cur_entry->refcnt != 1) {
725 if (wimlib_print_errors) {
726 ERROR("Found metadata resource with refcnt != 1:");
727 print_lookup_table_entry(cur_entry, stderr);
729 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
730 goto out_free_cur_entry;
733 if (wim->hdr.part_number != 1) {
734 WARNING("Ignoring metadata resource found in a "
735 "non-first part of the split WIM");
736 free_lookup_table_entry(cur_entry);
739 if (wim->current_image == wim->hdr.image_count) {
740 WARNING("The WIM header says there are %u images "
741 "in the WIM, but we found more metadata "
742 "resources than this (ignoring the extra)",
743 wim->hdr.image_count);
744 free_lookup_table_entry(cur_entry);
748 /* Notice very carefully: We are assigning the metadata
749 * resources in the exact order mirrored by their lookup
750 * table entries on disk, which is the behavior of
751 * Microsoft's software. In particular, this overrides
752 * the actual locations of the metadata resources
753 * themselves in the WIM file as well as any information
754 * written in the XML data. */
755 DEBUG("Found metadata resource for image %u at "
757 wim->current_image + 1,
758 cur_entry->rspec->offset_in_wim);
760 wim->current_image++]->metadata_lte = cur_entry;
764 /* Lookup table entry for a stream that is not a metadata
766 duplicate_entry = lookup_resource(table, cur_entry->hash);
767 if (duplicate_entry) {
768 if (wimlib_print_errors) {
769 WARNING("The WIM lookup table contains two entries with the "
770 "same SHA1 message digest!");
771 WARNING("The first entry is:");
772 print_lookup_table_entry(duplicate_entry, stderr);
773 WARNING("The second entry is:");
774 print_lookup_table_entry(cur_entry, stderr);
776 free_lookup_table_entry(cur_entry);
780 /* Finally, insert the stream into the lookup table, keyed by
781 * its SHA1 message digest. */
782 lookup_table_insert(table, cur_entry);
785 /* Validate the last resource. */
786 if (cur_rspec != NULL) {
787 ret = validate_resource(cur_rspec);
789 goto out_free_lookup_table;
792 if (wim->hdr.part_number == 1 && wim->current_image != wim->hdr.image_count) {
793 WARNING("The header of \"%"TS"\" says there are %u images in\n"
794 " the WIM, but we only found %d metadata resources! Acting as if\n"
795 " the header specified only %d images instead.",
796 wim->filename, wim->hdr.image_count,
797 wim->current_image, wim->current_image);
798 for (int i = wim->current_image; i < wim->hdr.image_count; i++)
799 put_image_metadata(wim->image_metadata[i], NULL);
800 wim->hdr.image_count = wim->current_image;
802 DEBUG("Done reading lookup table.");
803 wim->lookup_table = table;
809 out_free_lookup_table:
810 free_lookup_table(table);
814 wim->current_image = 0;
819 put_wim_lookup_table_entry(struct wim_lookup_table_entry_disk *disk_entry,
820 const struct wim_reshdr *out_reshdr,
821 u16 part_number, u32 refcnt, const u8 *hash)
823 put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
824 disk_entry->part_number = cpu_to_le16(part_number);
825 disk_entry->refcnt = cpu_to_le32(refcnt);
826 copy_hash(disk_entry->hash, hash);
830 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
831 struct filedes *out_fd,
833 struct wim_reshdr *out_reshdr,
834 int write_resource_flags,
835 struct wimlib_lzx_context **comp_ctx)
838 struct wim_lookup_table_entry *lte;
839 struct wim_lookup_table_entry_disk *table_buf;
840 struct wim_lookup_table_entry_disk *table_buf_ptr;
842 u64 prev_res_offset_in_wim = ~0ULL;
845 list_for_each_entry(lte, stream_list, lookup_table_list) {
846 table_size += sizeof(struct wim_lookup_table_entry_disk);
848 if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
849 lte->out_res_offset_in_wim != prev_res_offset_in_wim)
851 table_size += sizeof(struct wim_lookup_table_entry_disk);
852 prev_res_offset_in_wim = lte->out_res_offset_in_wim;
856 DEBUG("Writing WIM lookup table (size=%zu, offset=%"PRIu64")",
857 table_size, out_fd->offset);
859 table_buf = MALLOC(table_size);
860 if (table_buf == NULL) {
861 ERROR("Failed to allocate %zu bytes for temporary lookup table",
863 return WIMLIB_ERR_NOMEM;
865 table_buf_ptr = table_buf;
867 prev_res_offset_in_wim = ~0ULL;
868 list_for_each_entry(lte, stream_list, lookup_table_list) {
870 put_wim_lookup_table_entry(table_buf_ptr++,
875 if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
876 lte->out_res_offset_in_wim != prev_res_offset_in_wim)
878 /* Put the main resource entry for the pack. */
880 struct wim_reshdr reshdr;
882 reshdr.offset_in_wim = lte->out_res_offset_in_wim;
883 reshdr.size_in_wim = lte->out_res_size_in_wim;
884 reshdr.uncompressed_size = WIM_PACK_MAGIC_NUMBER;
885 reshdr.flags = WIM_RESHDR_FLAG_PACKED_STREAMS;
887 DEBUG("Putting main entry for pack: "
888 "size_in_wim=%"PRIu64", "
889 "offset_in_wim=%"PRIu64", "
890 "uncompressed_size=%"PRIu64,
892 reshdr.offset_in_wim,
893 reshdr.uncompressed_size);
895 put_wim_lookup_table_entry(table_buf_ptr++,
899 prev_res_offset_in_wim = lte->out_res_offset_in_wim;
903 wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
905 /* Write the lookup table uncompressed. Although wimlib can handle a
906 * compressed lookup table, MS software cannot. */
907 ret = write_wim_resource_from_buffer(table_buf,
909 WIM_RESHDR_FLAG_METADATA,
911 WIMLIB_COMPRESSION_TYPE_NONE,
915 write_resource_flags,
918 DEBUG("ret=%d", ret);
923 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
925 lte->real_refcnt = 0;
930 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
937 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
939 if (lte->extracted_file != NULL) {
940 FREE(lte->extracted_file);
941 lte->extracted_file = NULL;
947 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
955 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
957 if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
958 tprintf(T("Offset = %"PRIu64" bytes\n"),
961 tprintf(T("Raw uncompressed size = %"PRIu64" bytes\n"),
962 lte->rspec->uncompressed_size);
964 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
965 lte->rspec->size_in_wim);
967 tprintf(T("Raw offset = %"PRIu64" bytes\n"),
968 lte->rspec->offset_in_wim);
969 } else if (lte->resource_location == RESOURCE_IN_WIM) {
970 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
971 lte->rspec->size_in_wim);
973 tprintf(T("Offset = %"PRIu64" bytes\n"),
974 lte->rspec->offset_in_wim);
977 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
980 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
981 lte->back_inode, lte->back_stream_id);
983 tfprintf(out, T("Hash = 0x"));
984 print_hash(lte->hash, out);
988 tfprintf(out, T("Flags = "));
989 u8 flags = lte->flags;
990 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
991 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
992 if (flags & WIM_RESHDR_FLAG_FREE)
993 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
994 if (flags & WIM_RESHDR_FLAG_METADATA)
995 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
996 if (flags & WIM_RESHDR_FLAG_SPANNED)
997 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
998 if (flags & WIM_RESHDR_FLAG_PACKED_STREAMS)
999 tfputs(T("WIM_RESHDR_FLAG_PACKED_STREAMS, "), out);
1000 tputc(T('\n'), out);
1001 switch (lte->resource_location) {
1002 case RESOURCE_IN_WIM:
1003 if (lte->rspec->wim->filename) {
1004 tfprintf(out, T("WIM file = `%"TS"'\n"),
1005 lte->rspec->wim->filename);
1009 case RESOURCE_WIN32_ENCRYPTED:
1011 case RESOURCE_IN_FILE_ON_DISK:
1012 tfprintf(out, T("File on Disk = `%"TS"'\n"),
1016 case RESOURCE_IN_STAGING_FILE:
1017 tfprintf(out, T("Staging File = `%"TS"'\n"),
1018 lte->staging_file_name);
1024 tputc(T('\n'), out);
1028 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
1029 struct wimlib_resource_entry *wentry)
1031 memset(wentry, 0, sizeof(*wentry));
1033 wentry->uncompressed_size = lte->size;
1034 if (lte->resource_location == RESOURCE_IN_WIM) {
1035 wentry->part_number = lte->rspec->wim->hdr.part_number;
1036 if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
1037 wentry->compressed_size = 0;
1038 wentry->offset = lte->offset_in_res;
1040 wentry->compressed_size = lte->rspec->size_in_wim;
1041 wentry->offset = lte->rspec->offset_in_wim;
1043 wentry->raw_resource_offset_in_wim = lte->rspec->offset_in_wim;
1044 /*wentry->raw_resource_uncompressed_size = lte->rspec->uncompressed_size;*/
1045 wentry->raw_resource_compressed_size = lte->rspec->size_in_wim;
1047 copy_hash(wentry->sha1_hash, lte->hash);
1048 wentry->reference_count = lte->refcnt;
1049 wentry->is_compressed = (lte->flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1050 wentry->is_metadata = (lte->flags & WIM_RESHDR_FLAG_METADATA) != 0;
1051 wentry->is_free = (lte->flags & WIM_RESHDR_FLAG_FREE) != 0;
1052 wentry->is_spanned = (lte->flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1053 wentry->packed = (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) != 0;
1056 struct iterate_lte_context {
1057 wimlib_iterate_lookup_table_callback_t cb;
1062 do_iterate_lte(struct wim_lookup_table_entry *lte, void *_ctx)
1064 struct iterate_lte_context *ctx = _ctx;
1065 struct wimlib_resource_entry entry;
1067 lte_to_wimlib_resource_entry(lte, &entry);
1068 return (*ctx->cb)(&entry, ctx->user_ctx);
1071 /* API function documented in wimlib.h */
1073 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1074 wimlib_iterate_lookup_table_callback_t cb,
1077 struct iterate_lte_context ctx = {
1079 .user_ctx = user_ctx,
1081 if (wim->hdr.part_number == 1) {
1083 for (int i = 0; i < wim->hdr.image_count; i++) {
1084 ret = do_iterate_lte(wim->image_metadata[i]->metadata_lte,
1090 return for_lookup_table_entry(wim->lookup_table, do_iterate_lte, &ctx);
1093 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
1094 * lookup table, or NULL if there is none. */
1095 struct wim_lookup_table_entry *
1096 lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
1099 struct wim_lookup_table_entry *lte;
1100 struct hlist_node *pos;
1102 wimlib_assert(table != NULL);
1103 wimlib_assert(hash != NULL);
1105 i = *(size_t*)hash % table->capacity;
1106 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
1107 if (hashes_equal(hash, lte->hash))
1114 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
1115 * given a path name.
1117 * This is only for pre-resolved inodes.
1120 wim_pathname_to_stream(WIMStruct *wim,
1123 struct wim_dentry **dentry_ret,
1124 struct wim_lookup_table_entry **lte_ret,
1125 u16 *stream_idx_ret)
1127 struct wim_dentry *dentry;
1128 struct wim_lookup_table_entry *lte;
1130 const tchar *stream_name = NULL;
1131 struct wim_inode *inode;
1134 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
1135 stream_name = path_stream_name(path);
1137 p = (tchar*)stream_name - 1;
1142 dentry = get_dentry(wim, path);
1148 inode = dentry->d_inode;
1150 if (!inode->i_resolved)
1151 if (inode_resolve_ltes(inode, wim->lookup_table, false))
1154 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
1155 && inode_is_directory(inode))
1159 struct wim_ads_entry *ads_entry;
1161 ads_entry = inode_get_ads_entry(inode, stream_name,
1164 stream_idx = ads_idx + 1;
1165 lte = ads_entry->lte;
1171 lte = inode_unnamed_stream_resolved(inode, &stream_idx);
1175 *dentry_ret = dentry;
1179 *stream_idx_ret = stream_idx;
1185 resource_not_found_error(const struct wim_inode *inode, const u8 *hash)
1187 if (wimlib_print_errors) {
1188 ERROR("\"%"TS"\": resource not found", inode_first_full_path(inode));
1189 tfprintf(stderr, T(" SHA-1 message digest of missing resource:\n "));
1190 print_hash(hash, stderr);
1191 tputc(T('\n'), stderr);
1193 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
1197 * Resolve an inode's lookup table entries.
1199 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
1200 * lookup table) with pointers directly to the lookup table entries.
1202 * If @force is %false:
1203 * If any needed SHA1 message digests are not found in the lookup table,
1204 * WIMLIB_ERR_RESOURCE_NOT_FOUND is returned and the inode is left
1206 * If @force is %true:
1207 * If any needed SHA1 message digests are not found in the lookup table,
1208 * new entries are allocated and inserted into the lookup table.
1211 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
1216 if (!inode->i_resolved) {
1217 struct wim_lookup_table_entry *lte, *ads_lte;
1219 /* Resolve the default file stream */
1221 hash = inode->i_hash;
1222 if (!is_zero_hash(hash)) {
1223 lte = lookup_resource(table, hash);
1226 lte = new_lookup_table_entry();
1228 return WIMLIB_ERR_NOMEM;
1229 copy_hash(lte->hash, hash);
1230 lookup_table_insert(table, lte);
1232 goto resource_not_found;
1237 /* Resolve the alternate data streams */
1238 struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
1239 for (u16 i = 0; i < inode->i_num_ads; i++) {
1240 struct wim_ads_entry *cur_entry;
1243 cur_entry = &inode->i_ads_entries[i];
1244 hash = cur_entry->hash;
1245 if (!is_zero_hash(hash)) {
1246 ads_lte = lookup_resource(table, hash);
1249 ads_lte = new_lookup_table_entry();
1251 return WIMLIB_ERR_NOMEM;
1252 copy_hash(ads_lte->hash, hash);
1253 lookup_table_insert(table, ads_lte);
1255 goto resource_not_found;
1259 ads_ltes[i] = ads_lte;
1262 for (u16 i = 0; i < inode->i_num_ads; i++)
1263 inode->i_ads_entries[i].lte = ads_ltes[i];
1264 inode->i_resolved = 1;
1269 return resource_not_found_error(inode, hash);
1273 inode_unresolve_ltes(struct wim_inode *inode)
1275 if (inode->i_resolved) {
1277 copy_hash(inode->i_hash, inode->i_lte->hash);
1279 zero_out_hash(inode->i_hash);
1281 for (u16 i = 0; i < inode->i_num_ads; i++) {
1282 if (inode->i_ads_entries[i].lte)
1283 copy_hash(inode->i_ads_entries[i].hash,
1284 inode->i_ads_entries[i].lte->hash);
1286 zero_out_hash(inode->i_ads_entries[i].hash);
1288 inode->i_resolved = 0;
1293 * Returns the lookup table entry for stream @stream_idx of the inode, where
1294 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
1295 * corresponds to an alternate data stream.
1297 * This works for both resolved and un-resolved inodes.
1299 struct wim_lookup_table_entry *
1300 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
1301 const struct wim_lookup_table *table)
1303 if (inode->i_resolved)
1304 return inode_stream_lte_resolved(inode, stream_idx);
1306 return inode_stream_lte_unresolved(inode, stream_idx, table);
1309 struct wim_lookup_table_entry *
1310 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret)
1312 wimlib_assert(inode->i_resolved);
1313 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1314 if (inode_stream_name_nbytes(inode, i) == 0 &&
1315 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
1317 *stream_idx_ret = i;
1318 return inode_stream_lte_resolved(inode, i);
1321 *stream_idx_ret = 0;
1325 struct wim_lookup_table_entry *
1326 inode_unnamed_lte_resolved(const struct wim_inode *inode)
1329 return inode_unnamed_stream_resolved(inode, &stream_idx);
1332 struct wim_lookup_table_entry *
1333 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
1334 const struct wim_lookup_table *table)
1336 wimlib_assert(!inode->i_resolved);
1337 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1338 if (inode_stream_name_nbytes(inode, i) == 0 &&
1339 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
1341 return inode_stream_lte_unresolved(inode, i, table);
1347 /* Return the lookup table entry for the unnamed data stream of an inode, or
1348 * NULL if there is none.
1350 * You'd think this would be easier than it actually is, since the unnamed data
1351 * stream should be the one referenced from the inode itself. Alas, if there
1352 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
1353 * data stream in one of the alternate data streams instead of inside the WIM
1354 * dentry itself. So we need to check the alternate data streams too.
1356 * Also, note that a dentry may appear to have more than one unnamed stream, but
1357 * if the SHA1 message digest is all 0's then the corresponding stream does not
1358 * really "count" (this is the case for the inode's own file stream when the
1359 * file stream that should be there is actually in one of the alternate stream
1360 * entries.). This is despite the fact that we may need to extract such a
1361 * missing entry as an empty file or empty named data stream.
1363 struct wim_lookup_table_entry *
1364 inode_unnamed_lte(const struct wim_inode *inode,
1365 const struct wim_lookup_table *table)
1367 if (inode->i_resolved)
1368 return inode_unnamed_lte_resolved(inode);
1370 return inode_unnamed_lte_unresolved(inode, table);
1373 /* Returns the SHA1 message digest of the unnamed data stream of a WIM inode, or
1374 * 'zero_hash' if the unnamed data stream is missing has all zeroes in its SHA1
1375 * message digest field. */
1377 inode_unnamed_stream_hash(const struct wim_inode *inode)
1381 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1382 if (inode_stream_name_nbytes(inode, i) == 0) {
1383 hash = inode_stream_hash(inode, i);
1384 if (!is_zero_hash(hash))
1391 struct wim_lookup_table_entry **
1392 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
1394 wimlib_assert(lte->unhashed);
1395 struct wim_inode *inode = lte->back_inode;
1396 u32 stream_id = lte->back_stream_id;
1398 return &inode->i_lte;
1400 for (u16 i = 0; i < inode->i_num_ads; i++)
1401 if (inode->i_ads_entries[i].stream_id == stream_id)
1402 return &inode->i_ads_entries[i].lte;
1407 /* Calculate the SHA1 message digest of a stream and move it from the list of
1408 * unhashed streams to the stream lookup table, possibly joining it with an
1409 * existing lookup table entry for an identical stream.
1411 * @lte: An unhashed lookup table entry.
1412 * @lookup_table: Lookup table for the WIM.
1413 * @lte_ret: On success, write a pointer to the resulting lookup table
1414 * entry to this location. This will be the same as @lte
1415 * if it was inserted into the lookup table, or different if
1416 * a duplicate stream was found.
1418 * Returns 0 on success; nonzero if there is an error reading the stream.
1421 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
1422 struct wim_lookup_table *lookup_table,
1423 struct wim_lookup_table_entry **lte_ret)
1426 struct wim_lookup_table_entry *duplicate_lte;
1427 struct wim_lookup_table_entry **back_ptr;
1429 wimlib_assert(lte->unhashed);
1431 /* back_ptr must be saved because @back_inode and @back_stream_id are in
1432 * union with the SHA1 message digest and will no longer be valid once
1433 * the SHA1 has been calculated. */
1434 back_ptr = retrieve_lte_pointer(lte);
1436 ret = sha1_stream(lte);
1440 /* Look for a duplicate stream */
1441 duplicate_lte = lookup_resource(lookup_table, lte->hash);
1442 list_del(<e->unhashed_list);
1443 if (duplicate_lte) {
1444 /* We have a duplicate stream. Transfer the reference counts
1445 * from this stream to the duplicate and update the reference to
1446 * this stream (in an inode or ads_entry) to point to the
1447 * duplicate. The caller is responsible for freeing @lte if
1449 wimlib_assert(!(duplicate_lte->unhashed));
1450 wimlib_assert(duplicate_lte->size == lte->size);
1451 duplicate_lte->refcnt += lte->refcnt;
1453 *back_ptr = duplicate_lte;
1454 lte = duplicate_lte;
1456 /* No duplicate stream, so we need to insert this stream into
1457 * the lookup table and treat it as a hashed stream. */
1458 lookup_table_insert(lookup_table, lte);
1466 lte_clone_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
1468 struct wim_lookup_table *lookup_table = _lookup_table;
1470 if (lookup_resource(lookup_table, lte->hash))
1471 return 0; /* Resource already present. */
1473 lte = clone_lookup_table_entry(lte);
1475 return WIMLIB_ERR_NOMEM;
1476 lte->out_refcnt = 1;
1477 lookup_table_insert(lookup_table, lte);
1482 lte_delete_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
1484 struct wim_lookup_table *lookup_table = _lookup_table;
1486 if (lte->out_refcnt) {
1487 lookup_table_unlink(lookup_table, lte);
1488 free_lookup_table_entry(lte);
1493 /* API function documented in wimlib.h */
1495 wimlib_reference_resources(WIMStruct *wim,
1496 WIMStruct **resource_wims, unsigned num_resource_wims,
1503 return WIMLIB_ERR_INVALID_PARAM;
1505 if (num_resource_wims != 0 && resource_wims == NULL)
1506 return WIMLIB_ERR_INVALID_PARAM;
1508 for (i = 0; i < num_resource_wims; i++)
1509 if (resource_wims[i] == NULL)
1510 return WIMLIB_ERR_INVALID_PARAM;
1512 for_lookup_table_entry(wim->lookup_table, lte_zero_out_refcnt, NULL);
1514 for (i = 0; i < num_resource_wims; i++) {
1515 ret = for_lookup_table_entry(resource_wims[i]->lookup_table,
1524 for_lookup_table_entry(wim->lookup_table, lte_delete_if_new,
1530 reference_resource_paths(WIMStruct *wim,
1531 const tchar * const *resource_wimfiles,
1532 unsigned num_resource_wimfiles,
1535 wimlib_progress_func_t progress_func)
1537 WIMStruct **resource_wims;
1541 resource_wims = CALLOC(num_resource_wimfiles, sizeof(resource_wims[0]));
1543 return WIMLIB_ERR_NOMEM;
1545 for (i = 0; i < num_resource_wimfiles; i++) {
1546 DEBUG("Referencing resources from path \"%"TS"\"",
1547 resource_wimfiles[i]);
1548 ret = wimlib_open_wim(resource_wimfiles[i], open_flags,
1549 &resource_wims[i], progress_func);
1551 goto out_free_resource_wims;
1554 ret = wimlib_reference_resources(wim, resource_wims,
1555 num_resource_wimfiles, ref_flags);
1557 goto out_free_resource_wims;
1559 for (i = 0; i < num_resource_wimfiles; i++)
1560 list_add_tail(&resource_wims[i]->subwim_node, &wim->subwims);
1563 goto out_free_array;
1565 out_free_resource_wims:
1566 for (i = 0; i < num_resource_wimfiles; i++)
1567 wimlib_free(resource_wims[i]);
1569 FREE(resource_wims);
1574 reference_resource_glob(WIMStruct *wim, const tchar *refglob,
1575 int ref_flags, int open_flags,
1576 wimlib_progress_func_t progress_func)
1581 /* Note: glob() is replaced in Windows native builds. */
1582 ret = tglob(refglob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1584 if (ret == GLOB_NOMATCH) {
1585 if (ref_flags & WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH) {
1586 ERROR("Found no files for glob \"%"TS"\"", refglob);
1587 return WIMLIB_ERR_GLOB_HAD_NO_MATCHES;
1589 return reference_resource_paths(wim,
1597 ERROR_WITH_ERRNO("Failed to process glob \"%"TS"\"", refglob);
1598 if (ret == GLOB_NOSPACE)
1599 return WIMLIB_ERR_NOMEM;
1601 return WIMLIB_ERR_READ;
1605 ret = reference_resource_paths(wim,
1606 (const tchar * const *)globbuf.gl_pathv,
1615 /* API function documented in wimlib.h */
1617 wimlib_reference_resource_files(WIMStruct *wim,
1618 const tchar * const * resource_wimfiles_or_globs,
1622 wimlib_progress_func_t progress_func)
1627 if (ref_flags & WIMLIB_REF_FLAG_GLOB_ENABLE) {
1628 for (i = 0; i < count; i++) {
1629 ret = reference_resource_glob(wim,
1630 resource_wimfiles_or_globs[i],
1639 return reference_resource_paths(wim, resource_wimfiles_or_globs,
1641 open_flags, progress_func);