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->wim_resource_list, &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)
116 case RESOURCE_IN_ATTACHED_BUFFER:
117 new->attached_buffer = memdup(old->attached_buffer, old->size);
118 if (!new->attached_buffer)
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);
134 if (loc->stream_name_nchars) {
135 loc->stream_name = memdup(old->ntfs_loc->stream_name,
136 loc->stream_name_nchars * 2);
137 if (!loc->stream_name)
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->wim_resource_list);
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 and writing.
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;
313 u64 offset1, offset2;
315 lte1 = *(const struct wim_lookup_table_entry**)p1;
316 lte2 = *(const struct wim_lookup_table_entry**)p2;
318 v = (int)lte1->resource_location - (int)lte2->resource_location;
320 /* Different resource locations? */
324 switch (lte1->resource_location) {
325 case RESOURCE_IN_WIM:
326 wim1 = lte1->rspec->wim;
327 wim2 = lte2->rspec->wim;
329 /* Different (possibly split) WIMs? */
331 v = memcmp(wim1->hdr.guid, wim2->hdr.guid, WIM_GID_LEN);
336 /* Different part numbers in the same WIM? */
337 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
341 /* Compare by offset. */
342 offset1 = lte1->rspec->offset_in_wim + lte1->offset_in_res;
343 offset2 = lte2->rspec->offset_in_wim + lte2->offset_in_res;
345 if (offset1 < offset2)
347 if (offset1 > offset2)
350 case RESOURCE_IN_FILE_ON_DISK:
352 case RESOURCE_IN_STAGING_FILE:
355 case RESOURCE_WIN32_ENCRYPTED:
357 /* Compare files by path: just a heuristic that will place files
358 * in the same directory next to each other. */
359 return tstrcmp(lte1->file_on_disk, lte2->file_on_disk);
361 case RESOURCE_IN_NTFS_VOLUME:
362 return tstrcmp(lte1->ntfs_loc->path, lte2->ntfs_loc->path);
365 /* No additional sorting order defined for this resource
366 * location (e.g. RESOURCE_IN_ATTACHED_BUFFER); simply compare
367 * everything equal to each other. */
373 sort_stream_list_by_sequential_order(struct list_head *stream_list,
374 size_t list_head_offset)
376 struct list_head *cur;
377 struct wim_lookup_table_entry **array;
380 size_t num_streams = 0;
382 list_for_each(cur, stream_list)
385 array_size = num_streams * sizeof(array[0]);
386 array = MALLOC(array_size);
388 return WIMLIB_ERR_NOMEM;
389 cur = stream_list->next;
390 for (i = 0; i < num_streams; i++) {
391 array[i] = (struct wim_lookup_table_entry*)((u8*)cur -
396 qsort(array, num_streams, sizeof(array[0]),
397 cmp_streams_by_sequential_order);
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),
411 add_lte_to_array(struct wim_lookup_table_entry *lte,
414 struct wim_lookup_table_entry ***pp = _pp;
419 /* Iterate through the lookup table entries, but first sort them by stream
420 * offset in the WIM. Caution: this is intended to be used when the stream
421 * offset field has actually been set. */
423 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
424 int (*visitor)(struct wim_lookup_table_entry *,
428 struct wim_lookup_table_entry **lte_array, **p;
429 size_t num_streams = table->num_entries;
432 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
434 return WIMLIB_ERR_NOMEM;
436 for_lookup_table_entry(table, add_lte_to_array, &p);
438 wimlib_assert(p == lte_array + num_streams);
440 qsort(lte_array, num_streams, sizeof(lte_array[0]),
441 cmp_streams_by_sequential_order);
443 for (size_t i = 0; i < num_streams; i++) {
444 ret = visitor(lte_array[i], arg);
452 /* On-disk format of a WIM lookup table entry (stream entry). */
453 struct wim_lookup_table_entry_disk {
454 /* Size, offset, and flags of the stream. */
455 struct wim_reshdr_disk reshdr;
457 /* Which part of the split WIM this stream is in; indexed from 1. */
460 /* Reference count of this stream over all WIM images. */
463 /* SHA1 message digest of the uncompressed data of this stream, or
464 * optionally all zeroes if this stream is of zero length. */
465 u8 hash[SHA1_HASH_SIZE];
468 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
470 /* Validate the size and location of a WIM resource. */
472 validate_resource(const struct wim_resource_spec *rspec)
474 struct wim_lookup_table_entry *lte;
477 /* Verify that calculating the offset of the end of the resource doesn't
479 if (rspec->offset_in_wim + rspec->size_in_wim < rspec->size_in_wim)
482 /* Verify that each stream in the resource has a valid offset and size,
483 * and that no streams overlap. */
485 list_for_each_entry(lte, &rspec->stream_list, wim_resource_list) {
486 if (lte->offset_in_res + lte->size < lte->size ||
487 lte->offset_in_res + lte->size > rspec->uncompressed_size ||
488 lte->offset_in_res < cur_offset)
491 cur_offset = lte->offset_in_res;
497 ERROR("Invalid resource entry!");
498 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
502 * Reads the lookup table from a WIM file. Each entry specifies a stream that
503 * the WIM file contains, along with its location and SHA1 message digest.
505 * Saves lookup table entries for non-metadata streams in a hash table, and
506 * saves the metadata entry for each image in a special per-image location (the
507 * image_metadata array).
510 * WIMLIB_ERR_SUCCESS (0)
511 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
512 * WIMLIB_ERR_RESOURCE_NOT_FOUND
514 * Or an error code caused by failure to read the lookup table into memory.
517 read_wim_lookup_table(WIMStruct *wim)
522 struct wim_lookup_table *table;
523 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
524 struct wim_resource_spec *cur_rspec;
527 DEBUG("Reading lookup table.");
529 /* Sanity check: lookup table entries are 50 bytes each. */
530 BUILD_BUG_ON(sizeof(struct wim_lookup_table_entry_disk) !=
531 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE);
533 /* Calculate number of entries in the lookup table. */
534 num_entries = wim->hdr.lookup_table_reshdr.uncompressed_size /
535 sizeof(struct wim_lookup_table_entry_disk);
537 /* Read the lookup table into a buffer. */
538 ret = wim_reshdr_to_data(&wim->hdr.lookup_table_reshdr, wim, &buf);
542 /* Allocate a hash table to map SHA1 message digests into stream
543 * specifications. This is the in-memory "lookup table". */
544 table = new_lookup_table(num_entries * 2 + 1);
546 ERROR("Not enough memory to read lookup table.");
547 ret = WIMLIB_ERR_NOMEM;
551 /* Allocate and initalize stream entries from the raw lookup table
553 wim->current_image = 0;
555 for (i = 0; i < num_entries; i++) {
556 const struct wim_lookup_table_entry_disk *disk_entry =
557 &((const struct wim_lookup_table_entry_disk*)buf)[i];
559 struct wim_reshdr reshdr;
561 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
563 DEBUG("reshdr: size_in_wim=%"PRIu64", "
564 "uncompressed_size=%"PRIu64", "
565 "offset_in_wim=%"PRIu64", "
567 reshdr.size_in_wim, reshdr.uncompressed_size,
568 reshdr.offset_in_wim, reshdr.flags);
570 cur_entry = new_lookup_table_entry();
571 if (cur_entry == NULL) {
572 ERROR("Not enough memory to read lookup table!");
573 ret = WIMLIB_ERR_NOMEM;
574 goto out_free_lookup_table;
577 part_number = le16_to_cpu(disk_entry->part_number);
578 cur_entry->refcnt = le32_to_cpu(disk_entry->refcnt);
579 copy_hash(cur_entry->hash, disk_entry->hash);
581 if (part_number != wim->hdr.part_number) {
582 WARNING("A lookup table entry in part %hu of the WIM "
583 "points to part %hu (ignoring it)",
584 wim->hdr.part_number, part_number);
585 free_lookup_table_entry(cur_entry);
589 if (!(reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) ||
592 /* Starting new run of streams that share the same WIM
594 if (cur_rspec != NULL) {
595 ret = validate_resource(cur_rspec);
597 goto out_free_cur_entry;
600 /* Allocate the resource specification and initialize it
601 * with values from the current stream entry. */
602 cur_rspec = MALLOC(sizeof(*cur_rspec));
603 if (cur_rspec == NULL) {
604 ERROR("Not enough memory to read lookup table!");
605 ret = WIMLIB_ERR_NOMEM;
606 goto out_free_cur_entry;
608 wim_res_hdr_to_spec(&reshdr, wim, cur_rspec);
610 /* If this is a packed run, the current stream entry may
611 * specify a stream within the resource, and not the
612 * resource itself. Zero possibly irrelevant data until
613 * it is read for certain. */
614 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
615 cur_rspec->size_in_wim = 0;
616 cur_rspec->uncompressed_size = 0;
617 cur_rspec->flags = 0;
621 if (is_zero_hash(cur_entry->hash)) {
622 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
623 /* Found the specification for the packed resource.
624 * Transfer the values to the `struct
625 * wim_resource_spec', and discard the current stream
626 * since this lookup table entry did not, in fact,
627 * correspond to a "stream". */
628 cur_rspec->offset_in_wim = reshdr.offset_in_wim;
629 cur_rspec->size_in_wim = reshdr.size_in_wim;
630 cur_rspec->flags = reshdr.flags;
631 DEBUG("Full run is %"PRIu64" compressed bytes "
632 "at file offset %"PRIu64" (flags 0x%02x)",
633 cur_rspec->size_in_wim,
634 cur_rspec->offset_in_wim,
637 free_lookup_table_entry(cur_entry);
641 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
642 /* Continuing the run with another stream. */
643 DEBUG("Continuing packed run with stream: "
644 "%"PRIu64" uncompressed bytes @ resource offset %"PRIu64")",
645 reshdr.size_in_wim, reshdr.offset_in_wim);
646 cur_rspec->uncompressed_size += reshdr.size_in_wim;
649 lte_bind_wim_resource_spec(cur_entry, cur_rspec);
650 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
651 /* In packed runs, the offset field is used for
652 * in-resource offset, not the in-WIM offset, and the
653 * size field is used for the uncompressed size, not the
654 * compressed size. */
655 cur_entry->offset_in_res = reshdr.offset_in_wim;
656 cur_entry->size = reshdr.size_in_wim;
657 cur_entry->flags = reshdr.flags;
659 /* Normal case: The stream corresponds one-to-one with
660 * the resource entry. */
661 cur_entry->offset_in_res = 0;
662 cur_entry->size = reshdr.uncompressed_size;
663 cur_entry->flags = reshdr.flags;
667 if (cur_entry->flags & WIM_RESHDR_FLAG_METADATA) {
668 /* Lookup table entry for a metadata resource */
669 if (cur_entry->refcnt != 1) {
670 /* Metadata entries with no references must be
671 * ignored. See for example the WinPE WIMs from
673 if (cur_entry->refcnt == 0) {
674 free_lookup_table_entry(cur_entry);
677 if (wimlib_print_errors) {
678 ERROR("Found metadata resource with refcnt != 1:");
679 print_lookup_table_entry(cur_entry, stderr);
681 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
682 goto out_free_cur_entry;
685 if (wim->hdr.part_number != 1) {
686 WARNING("Ignoring metadata resource found in a "
687 "non-first part of the split WIM");
688 free_lookup_table_entry(cur_entry);
691 if (wim->current_image == wim->hdr.image_count) {
692 WARNING("The WIM header says there are %u images "
693 "in the WIM, but we found more metadata "
694 "resources than this (ignoring the extra)",
695 wim->hdr.image_count);
696 free_lookup_table_entry(cur_entry);
700 /* Notice very carefully: We are assigning the metadata
701 * resources in the exact order mirrored by their lookup
702 * table entries on disk, which is the behavior of
703 * Microsoft's software. In particular, this overrides
704 * the actual locations of the metadata resources
705 * themselves in the WIM file as well as any information
706 * written in the XML data. */
707 DEBUG("Found metadata resource for image %u at "
709 wim->current_image + 1,
710 cur_entry->rspec->offset_in_wim);
712 wim->current_image++]->metadata_lte = cur_entry;
716 /* Lookup table entry for a stream that is not a metadata
718 duplicate_entry = lookup_resource(table, cur_entry->hash);
719 if (duplicate_entry) {
720 if (wimlib_print_errors) {
721 WARNING("The WIM lookup table contains two entries with the "
722 "same SHA1 message digest!");
723 WARNING("The first entry is:");
724 print_lookup_table_entry(duplicate_entry, stderr);
725 WARNING("The second entry is:");
726 print_lookup_table_entry(cur_entry, stderr);
728 free_lookup_table_entry(cur_entry);
732 /* Finally, insert the stream into the lookup table, keyed by
733 * its SHA1 message digest. */
734 lookup_table_insert(table, cur_entry);
737 /* Validate the last resource. */
738 if (cur_rspec != NULL) {
739 ret = validate_resource(cur_rspec);
741 goto out_free_cur_entry;
744 if (wim->hdr.part_number == 1 && wim->current_image != wim->hdr.image_count) {
745 WARNING("The header of \"%"TS"\" says there are %u images in\n"
746 " the WIM, but we only found %d metadata resources! Acting as if\n"
747 " the header specified only %d images instead.",
748 wim->filename, wim->hdr.image_count,
749 wim->current_image, wim->current_image);
750 for (int i = wim->current_image; i < wim->hdr.image_count; i++)
751 put_image_metadata(wim->image_metadata[i], NULL);
752 wim->hdr.image_count = wim->current_image;
754 DEBUG("Done reading lookup table.");
755 wim->lookup_table = table;
761 out_free_lookup_table:
762 free_lookup_table(table);
766 wim->current_image = 0;
772 write_wim_lookup_table_entry(const struct wim_lookup_table_entry *lte,
773 struct wim_lookup_table_entry_disk *disk_entry,
776 put_wim_reshdr(<e->out_reshdr, &disk_entry->reshdr);
777 disk_entry->part_number = cpu_to_le16(part_number);
778 disk_entry->refcnt = cpu_to_le32(lte->out_refcnt);
779 copy_hash(disk_entry->hash, lte->hash);
783 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
784 struct filedes *out_fd,
786 struct wim_reshdr *out_reshdr,
787 int write_resource_flags,
788 struct wimlib_lzx_context **comp_ctx)
791 struct wim_lookup_table_entry *lte;
792 struct wim_lookup_table_entry_disk *table_buf;
793 struct wim_lookup_table_entry_disk *table_buf_ptr;
797 list_for_each_entry(lte, stream_list, lookup_table_list)
798 table_size += sizeof(struct wim_lookup_table_entry_disk);
800 DEBUG("Writing WIM lookup table (size=%zu, offset=%"PRIu64")",
801 table_size, out_fd->offset);
803 table_buf = MALLOC(table_size);
805 ERROR("Failed to allocate %zu bytes for temporary lookup table",
807 return WIMLIB_ERR_NOMEM;
809 table_buf_ptr = table_buf;
810 list_for_each_entry(lte, stream_list, lookup_table_list)
811 write_wim_lookup_table_entry(lte, table_buf_ptr++, part_number);
813 /* Write the lookup table uncompressed. Although wimlib can handle a
814 * compressed lookup table, MS software cannot. */
815 ret = write_wim_resource_from_buffer(table_buf,
817 WIM_RESHDR_FLAG_METADATA,
819 WIMLIB_COMPRESSION_TYPE_NONE,
823 write_resource_flags,
826 DEBUG("ret=%d", ret);
831 append_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_list)
833 /* Lookup table entries with 'out_refcnt' == 0 correspond to streams not
834 * written and not present in the resulting WIM file, and should not be
835 * included in the lookup table.
837 * Lookup table entries marked as filtered (EXTERNAL_WIM) with
838 * 'out_refcnt != 0' were referenced as part of the logical write but
839 * correspond to streams that were not in fact written, and should not
840 * be included in the lookup table.
842 * Lookup table entries marked as filtered (SAME_WIM) with 'out_refcnt
843 * != 0' were referenced as part of the logical write but correspond to
844 * streams that were not in fact written, but nevertheless were already
845 * present in the WIM being overwritten in-place. These entries must be
846 * included in the lookup table, and the resource information to write
847 * needs to be copied from the resource information read originally.
849 if (lte->out_refcnt != 0 && !(lte->filtered & FILTERED_EXTERNAL_WIM)) {
850 if (lte->filtered & FILTERED_SAME_WIM)
851 wim_res_spec_to_hdr(lte->rspec, <e->out_reshdr);
852 list_add_tail(<e->lookup_table_list, (struct list_head*)_list);
858 write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
859 struct wim_reshdr *out_reshdr,
860 struct list_head *stream_list_override)
862 int write_resource_flags;
863 struct list_head _stream_list;
864 struct list_head *stream_list;
866 if (stream_list_override) {
867 stream_list = stream_list_override;
869 stream_list = &_stream_list;
870 INIT_LIST_HEAD(stream_list);
873 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_METADATA)) {
877 if (image == WIMLIB_ALL_IMAGES) {
879 end_image = wim->hdr.image_count;
885 /* Push metadata resource lookup table entries onto the front of
886 * the list in reverse order, so that they're written in order.
888 for (int i = end_image; i >= start_image; i--) {
889 struct wim_lookup_table_entry *metadata_lte;
891 metadata_lte = wim->image_metadata[i - 1]->metadata_lte;
892 metadata_lte->out_refcnt = 1;
893 metadata_lte->out_reshdr.flags |= WIM_RESHDR_FLAG_METADATA;
894 list_add(&metadata_lte->lookup_table_list, stream_list);
898 /* Append additional lookup table entries that need to be written, with
899 * some special handling for streams that have been marked as filtered.
901 if (!stream_list_override) {
902 for_lookup_table_entry(wim->lookup_table,
903 append_lookup_table_entry, stream_list);
906 write_resource_flags = 0;
907 if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
908 write_resource_flags |= WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE;
909 return write_wim_lookup_table_from_stream_list(stream_list,
911 wim->hdr.part_number,
913 write_resource_flags,
919 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
921 lte->real_refcnt = 0;
926 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
933 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
935 if (lte->extracted_file != NULL) {
936 FREE(lte->extracted_file);
937 lte->extracted_file = NULL;
943 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
951 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
953 if (lte_is_partial(lte)) {
954 tprintf(T("Offset = %"PRIu64" bytes\n"),
957 tprintf(T("Raw uncompressed size = %"PRIu64" bytes\n"),
958 lte->rspec->uncompressed_size);
960 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
961 lte->rspec->size_in_wim);
963 tprintf(T("Raw offset = %"PRIu64" bytes\n"),
964 lte->rspec->offset_in_wim);
965 } else if (lte->resource_location == RESOURCE_IN_WIM) {
966 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
967 lte->rspec->size_in_wim);
969 tprintf(T("Offset = %"PRIu64" bytes\n"),
970 lte->rspec->offset_in_wim);
973 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
976 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
977 lte->back_inode, lte->back_stream_id);
979 tfprintf(out, T("Hash = 0x"));
980 print_hash(lte->hash, out);
984 tfprintf(out, T("Flags = "));
985 u8 flags = lte->flags;
986 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
987 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
988 if (flags & WIM_RESHDR_FLAG_FREE)
989 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
990 if (flags & WIM_RESHDR_FLAG_METADATA)
991 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
992 if (flags & WIM_RESHDR_FLAG_SPANNED)
993 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
994 if (flags & WIM_RESHDR_FLAG_PACKED_STREAMS)
995 tfputs(T("WIM_RESHDR_FLAG_PACKED_STREAMS, "), out);
997 switch (lte->resource_location) {
998 case RESOURCE_IN_WIM:
999 if (lte->rspec->wim->filename) {
1000 tfprintf(out, T("WIM file = `%"TS"'\n"),
1001 lte->rspec->wim->filename);
1005 case RESOURCE_WIN32_ENCRYPTED:
1007 case RESOURCE_IN_FILE_ON_DISK:
1008 tfprintf(out, T("File on Disk = `%"TS"'\n"),
1012 case RESOURCE_IN_STAGING_FILE:
1013 tfprintf(out, T("Staging File = `%"TS"'\n"),
1014 lte->staging_file_name);
1020 tputc(T('\n'), out);
1024 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
1025 struct wimlib_resource_entry *wentry)
1027 memset(wentry, 0, sizeof(*wentry));
1029 wentry->uncompressed_size = lte->size;
1030 if (lte->resource_location == RESOURCE_IN_WIM) {
1031 wentry->part_number = lte->rspec->wim->hdr.part_number;
1032 if (lte_is_partial(lte)) {
1033 wentry->compressed_size = 0;
1034 wentry->offset = lte->offset_in_res;
1036 wentry->compressed_size = lte->rspec->size_in_wim;
1037 wentry->offset = lte->rspec->offset_in_wim;
1039 wentry->raw_resource_offset_in_wim = lte->rspec->offset_in_wim;
1040 wentry->raw_resource_uncompressed_size = lte->rspec->uncompressed_size;
1041 wentry->raw_resource_compressed_size = lte->rspec->size_in_wim;
1043 copy_hash(wentry->sha1_hash, lte->hash);
1044 wentry->reference_count = lte->refcnt;
1045 wentry->is_compressed = (lte->flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1046 wentry->is_metadata = (lte->flags & WIM_RESHDR_FLAG_METADATA) != 0;
1047 wentry->is_free = (lte->flags & WIM_RESHDR_FLAG_FREE) != 0;
1048 wentry->is_spanned = (lte->flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1049 wentry->is_partial = lte_is_partial(lte);
1052 struct iterate_lte_context {
1053 wimlib_iterate_lookup_table_callback_t cb;
1058 do_iterate_lte(struct wim_lookup_table_entry *lte, void *_ctx)
1060 struct iterate_lte_context *ctx = _ctx;
1061 struct wimlib_resource_entry entry;
1063 lte_to_wimlib_resource_entry(lte, &entry);
1064 return (*ctx->cb)(&entry, ctx->user_ctx);
1067 /* API function documented in wimlib.h */
1069 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1070 wimlib_iterate_lookup_table_callback_t cb,
1073 struct iterate_lte_context ctx = {
1075 .user_ctx = user_ctx,
1077 if (wim->hdr.part_number == 1) {
1079 for (int i = 0; i < wim->hdr.image_count; i++) {
1080 ret = do_iterate_lte(wim->image_metadata[i]->metadata_lte,
1086 return for_lookup_table_entry(wim->lookup_table, do_iterate_lte, &ctx);
1089 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
1090 * lookup table, or NULL if there is none. */
1091 struct wim_lookup_table_entry *
1092 lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
1095 struct wim_lookup_table_entry *lte;
1096 struct hlist_node *pos;
1098 wimlib_assert(table != NULL);
1099 wimlib_assert(hash != NULL);
1101 i = *(size_t*)hash % table->capacity;
1102 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
1103 if (hashes_equal(hash, lte->hash))
1110 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
1111 * given a path name.
1113 * This is only for pre-resolved inodes.
1116 wim_pathname_to_stream(WIMStruct *wim,
1119 struct wim_dentry **dentry_ret,
1120 struct wim_lookup_table_entry **lte_ret,
1121 u16 *stream_idx_ret)
1123 struct wim_dentry *dentry;
1124 struct wim_lookup_table_entry *lte;
1126 const tchar *stream_name = NULL;
1127 struct wim_inode *inode;
1130 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
1131 stream_name = path_stream_name(path);
1133 p = (tchar*)stream_name - 1;
1138 dentry = get_dentry(wim, path);
1144 inode = dentry->d_inode;
1146 if (!inode->i_resolved)
1147 if (inode_resolve_ltes(inode, wim->lookup_table, false))
1150 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
1151 && inode_is_directory(inode))
1155 struct wim_ads_entry *ads_entry;
1157 ads_entry = inode_get_ads_entry(inode, stream_name,
1160 stream_idx = ads_idx + 1;
1161 lte = ads_entry->lte;
1167 lte = inode_unnamed_stream_resolved(inode, &stream_idx);
1171 *dentry_ret = dentry;
1175 *stream_idx_ret = stream_idx;
1181 resource_not_found_error(const struct wim_inode *inode, const u8 *hash)
1183 if (wimlib_print_errors) {
1184 ERROR("\"%"TS"\": resource not found", inode_first_full_path(inode));
1185 tfprintf(stderr, T(" SHA-1 message digest of missing resource:\n "));
1186 print_hash(hash, stderr);
1187 tputc(T('\n'), stderr);
1189 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
1193 * Resolve an inode's lookup table entries.
1195 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
1196 * lookup table) with pointers directly to the lookup table entries.
1198 * If @force is %false:
1199 * If any needed SHA1 message digests are not found in the lookup table,
1200 * WIMLIB_ERR_RESOURCE_NOT_FOUND is returned and the inode is left
1202 * If @force is %true:
1203 * If any needed SHA1 message digests are not found in the lookup table,
1204 * new entries are allocated and inserted into the lookup table.
1207 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
1212 if (!inode->i_resolved) {
1213 struct wim_lookup_table_entry *lte, *ads_lte;
1215 /* Resolve the default file stream */
1217 hash = inode->i_hash;
1218 if (!is_zero_hash(hash)) {
1219 lte = lookup_resource(table, hash);
1222 lte = new_lookup_table_entry();
1224 return WIMLIB_ERR_NOMEM;
1225 copy_hash(lte->hash, hash);
1226 lookup_table_insert(table, lte);
1228 goto resource_not_found;
1233 /* Resolve the alternate data streams */
1234 struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
1235 for (u16 i = 0; i < inode->i_num_ads; i++) {
1236 struct wim_ads_entry *cur_entry;
1239 cur_entry = &inode->i_ads_entries[i];
1240 hash = cur_entry->hash;
1241 if (!is_zero_hash(hash)) {
1242 ads_lte = lookup_resource(table, hash);
1245 ads_lte = new_lookup_table_entry();
1247 return WIMLIB_ERR_NOMEM;
1248 copy_hash(ads_lte->hash, hash);
1249 lookup_table_insert(table, ads_lte);
1251 goto resource_not_found;
1255 ads_ltes[i] = ads_lte;
1258 for (u16 i = 0; i < inode->i_num_ads; i++)
1259 inode->i_ads_entries[i].lte = ads_ltes[i];
1260 inode->i_resolved = 1;
1265 return resource_not_found_error(inode, hash);
1269 inode_unresolve_ltes(struct wim_inode *inode)
1271 if (inode->i_resolved) {
1273 copy_hash(inode->i_hash, inode->i_lte->hash);
1275 zero_out_hash(inode->i_hash);
1277 for (u16 i = 0; i < inode->i_num_ads; i++) {
1278 if (inode->i_ads_entries[i].lte)
1279 copy_hash(inode->i_ads_entries[i].hash,
1280 inode->i_ads_entries[i].lte->hash);
1282 zero_out_hash(inode->i_ads_entries[i].hash);
1284 inode->i_resolved = 0;
1289 * Returns the lookup table entry for stream @stream_idx of the inode, where
1290 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
1291 * corresponds to an alternate data stream.
1293 * This works for both resolved and un-resolved inodes.
1295 struct wim_lookup_table_entry *
1296 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
1297 const struct wim_lookup_table *table)
1299 if (inode->i_resolved)
1300 return inode_stream_lte_resolved(inode, stream_idx);
1302 return inode_stream_lte_unresolved(inode, stream_idx, table);
1305 struct wim_lookup_table_entry *
1306 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret)
1308 wimlib_assert(inode->i_resolved);
1309 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1310 if (inode_stream_name_nbytes(inode, i) == 0 &&
1311 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
1313 *stream_idx_ret = i;
1314 return inode_stream_lte_resolved(inode, i);
1317 *stream_idx_ret = 0;
1321 struct wim_lookup_table_entry *
1322 inode_unnamed_lte_resolved(const struct wim_inode *inode)
1325 return inode_unnamed_stream_resolved(inode, &stream_idx);
1328 struct wim_lookup_table_entry *
1329 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
1330 const struct wim_lookup_table *table)
1332 wimlib_assert(!inode->i_resolved);
1333 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1334 if (inode_stream_name_nbytes(inode, i) == 0 &&
1335 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
1337 return inode_stream_lte_unresolved(inode, i, table);
1343 /* Return the lookup table entry for the unnamed data stream of an inode, or
1344 * NULL if there is none.
1346 * You'd think this would be easier than it actually is, since the unnamed data
1347 * stream should be the one referenced from the inode itself. Alas, if there
1348 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
1349 * data stream in one of the alternate data streams instead of inside the WIM
1350 * dentry itself. So we need to check the alternate data streams too.
1352 * Also, note that a dentry may appear to have more than one unnamed stream, but
1353 * if the SHA1 message digest is all 0's then the corresponding stream does not
1354 * really "count" (this is the case for the inode's own file stream when the
1355 * file stream that should be there is actually in one of the alternate stream
1356 * entries.). This is despite the fact that we may need to extract such a
1357 * missing entry as an empty file or empty named data stream.
1359 struct wim_lookup_table_entry *
1360 inode_unnamed_lte(const struct wim_inode *inode,
1361 const struct wim_lookup_table *table)
1363 if (inode->i_resolved)
1364 return inode_unnamed_lte_resolved(inode);
1366 return inode_unnamed_lte_unresolved(inode, table);
1369 /* Returns the SHA1 message digest of the unnamed data stream of a WIM inode, or
1370 * 'zero_hash' if the unnamed data stream is missing has all zeroes in its SHA1
1371 * message digest field. */
1373 inode_unnamed_stream_hash(const struct wim_inode *inode)
1377 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1378 if (inode_stream_name_nbytes(inode, i) == 0) {
1379 hash = inode_stream_hash(inode, i);
1380 if (!is_zero_hash(hash))
1387 struct wim_lookup_table_entry **
1388 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
1390 wimlib_assert(lte->unhashed);
1391 struct wim_inode *inode = lte->back_inode;
1392 u32 stream_id = lte->back_stream_id;
1394 return &inode->i_lte;
1396 for (u16 i = 0; i < inode->i_num_ads; i++)
1397 if (inode->i_ads_entries[i].stream_id == stream_id)
1398 return &inode->i_ads_entries[i].lte;
1403 /* Calculate the SHA1 message digest of a stream and move it from the list of
1404 * unhashed streams to the stream lookup table, possibly joining it with an
1405 * existing lookup table entry for an identical stream.
1407 * @lte: An unhashed lookup table entry.
1408 * @lookup_table: Lookup table for the WIM.
1409 * @lte_ret: On success, write a pointer to the resulting lookup table
1410 * entry to this location. This will be the same as @lte
1411 * if it was inserted into the lookup table, or different if
1412 * a duplicate stream was found.
1414 * Returns 0 on success; nonzero if there is an error reading the stream.
1417 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
1418 struct wim_lookup_table *lookup_table,
1419 struct wim_lookup_table_entry **lte_ret)
1422 struct wim_lookup_table_entry *duplicate_lte;
1423 struct wim_lookup_table_entry **back_ptr;
1425 wimlib_assert(lte->unhashed);
1427 /* back_ptr must be saved because @back_inode and @back_stream_id are in
1428 * union with the SHA1 message digest and will no longer be valid once
1429 * the SHA1 has been calculated. */
1430 back_ptr = retrieve_lte_pointer(lte);
1432 ret = sha1_stream(lte);
1436 /* Look for a duplicate stream */
1437 duplicate_lte = lookup_resource(lookup_table, lte->hash);
1438 list_del(<e->unhashed_list);
1439 if (duplicate_lte) {
1440 /* We have a duplicate stream. Transfer the reference counts
1441 * from this stream to the duplicate, update the reference to
1442 * this stream (in an inode or ads_entry) to point to the
1443 * duplicate, then free this stream. */
1444 wimlib_assert(!(duplicate_lte->unhashed));
1445 duplicate_lte->refcnt += lte->refcnt;
1446 duplicate_lte->out_refcnt += lte->out_refcnt;
1447 *back_ptr = duplicate_lte;
1448 free_lookup_table_entry(lte);
1449 lte = duplicate_lte;
1451 /* No duplicate stream, so we need to insert
1452 * this stream into the lookup table and treat
1453 * it as a hashed stream. */
1454 lookup_table_insert(lookup_table, lte);
1463 lte_clone_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
1465 struct wim_lookup_table *lookup_table = _lookup_table;
1467 if (lookup_resource(lookup_table, lte->hash))
1468 return 0; /* Resource already present. */
1470 lte = clone_lookup_table_entry(lte);
1472 return WIMLIB_ERR_NOMEM;
1473 lte->out_refcnt = 1;
1474 lookup_table_insert(lookup_table, lte);
1479 lte_delete_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
1481 struct wim_lookup_table *lookup_table = _lookup_table;
1483 if (lte->out_refcnt) {
1484 lookup_table_unlink(lookup_table, lte);
1485 free_lookup_table_entry(lte);
1490 /* API function documented in wimlib.h */
1492 wimlib_reference_resources(WIMStruct *wim,
1493 WIMStruct **resource_wims, unsigned num_resource_wims,
1500 return WIMLIB_ERR_INVALID_PARAM;
1502 if (num_resource_wims != 0 && resource_wims == NULL)
1503 return WIMLIB_ERR_INVALID_PARAM;
1505 for (i = 0; i < num_resource_wims; i++)
1506 if (resource_wims[i] == NULL)
1507 return WIMLIB_ERR_INVALID_PARAM;
1509 for_lookup_table_entry(wim->lookup_table, lte_zero_out_refcnt, NULL);
1511 for (i = 0; i < num_resource_wims; i++) {
1512 ret = for_lookup_table_entry(resource_wims[i]->lookup_table,
1521 for_lookup_table_entry(wim->lookup_table, lte_delete_if_new,
1527 reference_resource_paths(WIMStruct *wim,
1528 const tchar * const *resource_wimfiles,
1529 unsigned num_resource_wimfiles,
1532 wimlib_progress_func_t progress_func)
1534 WIMStruct **resource_wims;
1538 resource_wims = CALLOC(num_resource_wimfiles, sizeof(resource_wims[0]));
1540 return WIMLIB_ERR_NOMEM;
1542 for (i = 0; i < num_resource_wimfiles; i++) {
1543 DEBUG("Referencing resources from path \"%"TS"\"",
1544 resource_wimfiles[i]);
1545 ret = wimlib_open_wim(resource_wimfiles[i], open_flags,
1546 &resource_wims[i], progress_func);
1548 goto out_free_resource_wims;
1551 ret = wimlib_reference_resources(wim, resource_wims,
1552 num_resource_wimfiles, ref_flags);
1554 goto out_free_resource_wims;
1556 for (i = 0; i < num_resource_wimfiles; i++)
1557 list_add_tail(&resource_wims[i]->subwim_node, &wim->subwims);
1560 goto out_free_array;
1562 out_free_resource_wims:
1563 for (i = 0; i < num_resource_wimfiles; i++)
1564 wimlib_free(resource_wims[i]);
1566 FREE(resource_wims);
1571 reference_resource_glob(WIMStruct *wim, const tchar *refglob,
1572 int ref_flags, int open_flags,
1573 wimlib_progress_func_t progress_func)
1578 /* Note: glob() is replaced in Windows native builds. */
1579 ret = tglob(refglob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1581 if (ret == GLOB_NOMATCH) {
1582 if (ref_flags & WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH) {
1583 ERROR("Found no files for glob \"%"TS"\"", refglob);
1584 return WIMLIB_ERR_GLOB_HAD_NO_MATCHES;
1586 return reference_resource_paths(wim,
1594 ERROR_WITH_ERRNO("Failed to process glob \"%"TS"\"", refglob);
1595 if (ret == GLOB_NOSPACE)
1596 return WIMLIB_ERR_NOMEM;
1598 return WIMLIB_ERR_READ;
1602 ret = reference_resource_paths(wim,
1603 (const tchar * const *)globbuf.gl_pathv,
1612 /* API function documented in wimlib.h */
1614 wimlib_reference_resource_files(WIMStruct *wim,
1615 const tchar * const * resource_wimfiles_or_globs,
1619 wimlib_progress_func_t progress_func)
1624 if (ref_flags & WIMLIB_REF_FLAG_GLOB_ENABLE) {
1625 for (i = 0; i < count; i++) {
1626 ret = reference_resource_glob(wim,
1627 resource_wimfiles_or_globs[i],
1636 return reference_resource_paths(wim, resource_wimfiles_or_globs,
1638 open_flags, progress_func);