4 * Lookup table, implemented as a hash table, that maps SHA1 message digests to
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/endianness.h"
32 #include "wimlib/error.h"
33 #include "wimlib/file_io.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/metadata.h"
36 #include "wimlib/paths.h"
37 #include "wimlib/resource.h"
38 #include "wimlib/util.h"
43 # include <unistd.h> /* for unlink() */
46 struct wim_lookup_table *
47 new_lookup_table(size_t capacity)
49 struct wim_lookup_table *table;
50 struct hlist_head *array;
52 table = CALLOC(1, sizeof(struct wim_lookup_table));
54 array = CALLOC(capacity, sizeof(array[0]));
56 table->num_entries = 0;
57 table->capacity = capacity;
62 ERROR("Failed to allocate memory for lookup table "
63 "with capacity %zu", capacity);
69 struct wim_lookup_table_entry *
70 new_lookup_table_entry(void)
72 struct wim_lookup_table_entry *lte;
74 lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
79 ERROR("Out of memory (tried to allocate %zu bytes for "
80 "lookup table entry)",
81 sizeof(struct wim_lookup_table_entry));
86 struct wim_lookup_table_entry *
87 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
89 struct wim_lookup_table_entry *new;
91 new = memdup(old, sizeof(struct wim_lookup_table_entry));
95 new->extracted_file = NULL;
96 switch (new->resource_location) {
99 case RESOURCE_WIN32_ENCRYPTED:
101 case RESOURCE_IN_FILE_ON_DISK:
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) {
156 case RESOURCE_WIN32_ENCRYPTED:
158 case RESOURCE_IN_FILE_ON_DISK:
161 case RESOURCE_IN_STAGING_FILE:
162 BUILD_BUG_ON((void*)<e->file_on_disk !=
163 (void*)<e->staging_file_name);
165 case RESOURCE_IN_ATTACHED_BUFFER:
166 BUILD_BUG_ON((void*)<e->file_on_disk !=
167 (void*)<e->attached_buffer);
168 FREE(lte->file_on_disk);
171 case RESOURCE_IN_NTFS_VOLUME:
173 FREE(lte->ntfs_loc->path);
174 FREE(lte->ntfs_loc->stream_name);
187 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
189 free_lookup_table_entry(entry);
195 free_lookup_table(struct wim_lookup_table *table)
197 DEBUG2("Freeing lookup table");
200 for_lookup_table_entry(table,
201 do_free_lookup_table_entry,
210 * Inserts an entry into the lookup table.
212 * @table: A pointer to the lookup table.
213 * @lte: A pointer to the entry to insert.
216 lookup_table_insert(struct wim_lookup_table *table,
217 struct wim_lookup_table_entry *lte)
219 size_t i = lte->hash_short % table->capacity;
220 hlist_add_head(<e->hash_list, &table->array[i]);
222 /* XXX Make the table grow when too many entries have been inserted. */
223 table->num_entries++;
227 finalize_lte(struct wim_lookup_table_entry *lte)
230 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
231 unlink(lte->staging_file_name);
232 list_del(<e->unhashed_list);
235 free_lookup_table_entry(lte);
238 /* Decrements the reference count for the lookup table entry @lte. If its
239 * reference count reaches 0, it is unlinked from the lookup table. If,
240 * furthermore, the entry has no opened file descriptors associated with it, the
243 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
244 struct wim_lookup_table *table)
246 wimlib_assert(lte != NULL);
247 wimlib_assert(lte->refcnt != 0);
248 if (--lte->refcnt == 0) {
250 list_del(<e->unhashed_list);
252 lookup_table_unlink(table, lte);
254 if (lte->num_opened_fds == 0)
262 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
264 if (lte->num_opened_fds != 0)
265 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
270 /* Calls a function on all the entries in the WIM lookup table. Stop early and
271 * return nonzero if any call to the function returns nonzero. */
273 for_lookup_table_entry(struct wim_lookup_table *table,
274 int (*visitor)(struct wim_lookup_table_entry *, void *),
277 struct wim_lookup_table_entry *lte;
278 struct hlist_node *pos, *tmp;
281 for (size_t i = 0; i < table->capacity; i++) {
282 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
285 wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
286 ret = visitor(lte, arg);
295 cmp_streams_by_wim_position(const void *p1, const void *p2)
297 const struct wim_lookup_table_entry *lte1, *lte2;
298 lte1 = *(const struct wim_lookup_table_entry**)p1;
299 lte2 = *(const struct wim_lookup_table_entry**)p2;
300 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
302 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
310 add_lte_to_array(struct wim_lookup_table_entry *lte,
313 struct wim_lookup_table_entry ***pp = _pp;
318 /* Iterate through the lookup table entries, but first sort them by stream
319 * offset in the WIM. Caution: this is intended to be used when the stream
320 * offset field has actually been set. */
322 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
323 int (*visitor)(struct wim_lookup_table_entry *,
327 struct wim_lookup_table_entry **lte_array, **p;
328 size_t num_streams = table->num_entries;
331 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
333 return WIMLIB_ERR_NOMEM;
335 for_lookup_table_entry(table, add_lte_to_array, &p);
337 wimlib_assert(p == lte_array + num_streams);
339 qsort(lte_array, num_streams, sizeof(lte_array[0]),
340 cmp_streams_by_wim_position);
342 for (size_t i = 0; i < num_streams; i++) {
343 ret = visitor(lte_array[i], arg);
351 /* On-disk format of a WIM lookup table entry (stream entry). */
352 struct wim_lookup_table_entry_disk {
353 /* Location, offset, compression status, and metadata status of the
355 struct resource_entry_disk resource_entry;
357 /* Which part of the split WIM this stream is in; indexed from 1. */
360 /* Reference count of this stream over all WIM images. */
363 /* SHA1 message digest of the uncompressed data of this stream, or
364 * optionally all zeroes if this stream is of zero length. */
365 u8 hash[SHA1_HASH_SIZE];
368 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
371 * Reads the lookup table from a WIM file.
373 * Saves lookup table entries for non-metadata streams in a hash table, and
374 * saves the metadata entry for each image in a special per-image location (the
375 * image_metadata array).
378 read_lookup_table(WIMStruct *w)
382 struct wim_lookup_table *table;
383 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
384 struct wim_lookup_table_entry_disk
385 table_buf[BUFFER_SIZE / sizeof(struct wim_lookup_table_entry_disk)]
386 _aligned_attribute(8);
388 BUILD_BUG_ON(sizeof(struct wim_lookup_table_entry_disk) !=
389 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE);
392 size_t buf_entries_remaining;
393 const struct wim_lookup_table_entry_disk *disk_entry;
395 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
396 w->hdr.lookup_table_res_entry.offset,
397 w->hdr.lookup_table_res_entry.original_size);
399 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
400 ERROR("Didn't expect a compressed lookup table!");
401 ERROR("Ask the author to implement support for this.");
402 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
405 num_entries = w->hdr.lookup_table_res_entry.size /
406 sizeof(struct wim_lookup_table_entry_disk);
407 table = new_lookup_table(num_entries * 2 + 1);
409 ERROR("Failed to allocate stream hash table of size %zu",
410 num_entries * 2 + 1);
411 return WIMLIB_ERR_NOMEM;
414 w->current_image = 0;
415 offset = w->hdr.lookup_table_res_entry.offset;
416 buf_entries_remaining = 0;
417 for (; num_entries != 0;
418 num_entries--, buf_entries_remaining--, disk_entry++)
420 if (buf_entries_remaining == 0) {
421 size_t entries_to_read, bytes_to_read;
423 entries_to_read = min(ARRAY_LEN(table_buf), num_entries);
424 bytes_to_read = entries_to_read * sizeof(struct wim_lookup_table_entry_disk);
425 if (full_pread(w->in_fd, table_buf,
426 bytes_to_read, offset) != bytes_to_read)
428 ERROR_WITH_ERRNO("Error reading lookup table "
429 "(offset=%"PRIu64")", offset);
430 ret = WIMLIB_ERR_READ;
431 goto out_free_lookup_table;
433 offset += bytes_to_read;
434 disk_entry = table_buf;
435 buf_entries_remaining = entries_to_read;
437 cur_entry = new_lookup_table_entry();
439 ret = WIMLIB_ERR_NOMEM;
440 goto out_free_lookup_table;
444 cur_entry->resource_location = RESOURCE_IN_WIM;
445 get_resource_entry(&disk_entry->resource_entry, &cur_entry->resource_entry);
446 cur_entry->part_number = le16_to_cpu(disk_entry->part_number);
447 cur_entry->refcnt = le32_to_cpu(disk_entry->refcnt);
448 copy_hash(cur_entry->hash, disk_entry->hash);
450 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
451 cur_entry->compression_type = w->compression_type;
453 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
455 if (cur_entry->part_number != w->hdr.part_number) {
456 WARNING("A lookup table entry in part %hu of the WIM "
457 "points to part %hu (ignoring it)",
458 w->hdr.part_number, cur_entry->part_number);
459 free_lookup_table_entry(cur_entry);
463 if (is_zero_hash(cur_entry->hash)) {
464 WARNING("The WIM lookup table contains an entry with a "
465 "SHA1 message digest of all 0's (ignoring it)");
466 free_lookup_table_entry(cur_entry);
470 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
471 && (cur_entry->resource_entry.size !=
472 cur_entry->resource_entry.original_size))
474 if (wimlib_print_errors) {
475 WARNING("Found uncompressed resource with "
476 "original size (%"PRIu64") not the same "
477 "as compressed size (%"PRIu64")",
478 cur_entry->resource_entry.original_size,
479 cur_entry->resource_entry.size);
480 if (cur_entry->resource_entry.original_size) {
481 WARNING("Overriding compressed size with original size.");
482 cur_entry->resource_entry.size =
483 cur_entry->resource_entry.original_size;
485 WARNING("Overriding original size with compressed size");
486 cur_entry->resource_entry.original_size =
487 cur_entry->resource_entry.size;
492 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
493 /* Lookup table entry for a metadata resource */
494 if (cur_entry->refcnt != 1) {
495 if (wimlib_print_errors) {
496 ERROR("Found metadata resource with refcnt != 1:");
497 print_lookup_table_entry(cur_entry, stderr);
499 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
500 goto out_free_cur_entry;
503 if (w->hdr.part_number != 1) {
504 WARNING("Ignoring metadata resource found in a "
505 "non-first part of the split WIM");
506 free_lookup_table_entry(cur_entry);
509 if (w->current_image == w->hdr.image_count) {
510 WARNING("The WIM header says there are %u images "
511 "in the WIM, but we found more metadata "
512 "resources than this (ignoring the extra)",
514 free_lookup_table_entry(cur_entry);
518 /* Notice very carefully: We are assigning the metadata
519 * resources in the exact order mirrored by their lookup
520 * table entries on disk, which is the behavior of
521 * Microsoft's software. In particular, this overrides
522 * the actual locations of the metadata resources
523 * themselves in the WIM file as well as any information
524 * written in the XML data. */
525 DEBUG("Found metadata resource for image %u at "
527 w->current_image + 1,
528 cur_entry->resource_entry.offset);
530 w->current_image++]->metadata_lte = cur_entry;
532 /* Lookup table entry for a stream that is not a
533 * metadata resource */
534 duplicate_entry = __lookup_resource(table, cur_entry->hash);
535 if (duplicate_entry) {
536 if (wimlib_print_errors) {
537 WARNING("The WIM lookup table contains two entries with the "
538 "same SHA1 message digest!");
539 WARNING("The first entry is:");
540 print_lookup_table_entry(duplicate_entry, stderr);
541 WARNING("The second entry is:");
542 print_lookup_table_entry(cur_entry, stderr);
544 free_lookup_table_entry(cur_entry);
547 lookup_table_insert(table, cur_entry);
552 if (w->hdr.part_number == 1 && w->current_image != w->hdr.image_count)
554 ERROR("The WIM header says there are %u images "
555 "in the WIM, but we only found %d metadata "
556 "resources!", w->hdr.image_count, w->current_image);
557 ret = WIMLIB_ERR_IMAGE_COUNT;
558 goto out_free_lookup_table;
560 DEBUG("Done reading lookup table.");
561 w->lookup_table = table;
566 out_free_lookup_table:
567 free_lookup_table(table);
569 w->current_image = 0;
575 write_lookup_table_entry(const struct wim_lookup_table_entry *lte,
576 struct wim_lookup_table_entry_disk *disk_entry)
578 put_resource_entry(<e->output_resource_entry, &disk_entry->resource_entry);
579 disk_entry->part_number = cpu_to_le16(lte->part_number);
580 disk_entry->refcnt = cpu_to_le32(lte->out_refcnt);
581 copy_hash(disk_entry->hash, lte->hash);
585 write_lookup_table_from_stream_list(struct list_head *stream_list,
587 struct resource_entry *out_res_entry)
591 struct wim_lookup_table_entry_disk
592 table_buf[BUFFER_SIZE / sizeof(struct wim_lookup_table_entry_disk)]
593 _aligned_attribute(8);
595 size_t bytes_to_write;
596 struct wim_lookup_table_entry *lte;
599 start_offset = filedes_offset(out_fd);
600 if (start_offset == -1)
605 list_for_each_entry(lte, stream_list, lookup_table_list) {
606 if (cur_idx == ARRAY_LEN(table_buf)) {
607 bytes_to_write = sizeof(table_buf);
608 if (full_write(out_fd, table_buf,
609 bytes_to_write) != bytes_to_write)
611 table_size += bytes_to_write;
614 write_lookup_table_entry(lte, &table_buf[cur_idx]);
618 bytes_to_write = cur_idx * sizeof(struct wim_lookup_table_entry_disk);
619 if (full_write(out_fd, table_buf,
620 bytes_to_write) != bytes_to_write)
622 table_size += bytes_to_write;
624 out_res_entry->offset = start_offset;
625 out_res_entry->size = table_size;
626 out_res_entry->original_size = table_size;
627 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
632 ERROR_WITH_ERRNO("Failed to write lookup table");
633 ret = WIMLIB_ERR_WRITE;
638 append_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_list)
640 if (lte->out_refcnt != 0)
641 list_add_tail(<e->lookup_table_list, (struct list_head*)_list);
645 /* Writes the WIM lookup table to the output file. */
647 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
649 LIST_HEAD(stream_list);
653 if (image == WIMLIB_ALL_IMAGES) {
655 end_image = w->hdr.image_count;
661 for (int i = start_image; i <= end_image; i++) {
662 struct wim_lookup_table_entry *metadata_lte;
664 metadata_lte = w->image_metadata[i - 1]->metadata_lte;
665 metadata_lte->out_refcnt = 1;
666 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
667 append_lookup_table_entry(metadata_lte, &stream_list);
669 for_lookup_table_entry(w->lookup_table,
670 append_lookup_table_entry,
672 return write_lookup_table_from_stream_list(&stream_list,
678 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
680 lte->real_refcnt = 0;
685 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
692 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
694 if (lte->extracted_file != NULL) {
695 FREE(lte->extracted_file);
696 lte->extracted_file = NULL;
702 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
708 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
709 lte->resource_entry.offset);
711 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
712 (u64)lte->resource_entry.size);
714 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
715 lte->resource_entry.original_size);
717 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
718 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
721 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
722 lte->back_inode, lte->back_stream_id);
724 tfprintf(out, T("Hash = 0x"));
725 print_hash(lte->hash, out);
729 tfprintf(out, T("Flags = "));
730 u8 flags = lte->resource_entry.flags;
731 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
732 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
733 if (flags & WIM_RESHDR_FLAG_FREE)
734 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
735 if (flags & WIM_RESHDR_FLAG_METADATA)
736 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
737 if (flags & WIM_RESHDR_FLAG_SPANNED)
738 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
740 switch (lte->resource_location) {
741 case RESOURCE_IN_WIM:
742 if (lte->wim->filename) {
743 tfprintf(out, T("WIM file = `%"TS"'\n"),
749 case RESOURCE_WIN32_ENCRYPTED:
751 case RESOURCE_IN_FILE_ON_DISK:
753 tfprintf(out, T("File on Disk = `%"TS"'\n"),
757 case RESOURCE_IN_STAGING_FILE:
758 tfprintf(out, T("Staging File = `%"TS"'\n"),
759 lte->staging_file_name);
769 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
771 print_lookup_table_entry(lte, (FILE*)fp);
776 * Prints the lookup table of a WIM file.
779 wimlib_print_lookup_table(WIMStruct *wim)
781 for (int i = 0; i < wim->hdr.image_count; i++)
782 print_lookup_table_entry(wim->image_metadata[i]->metadata_lte, stdout);
783 for_lookup_table_entry(wim->lookup_table,
784 do_print_lookup_table_entry,
788 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
789 * lookup table, or NULL if there is none. */
790 struct wim_lookup_table_entry *
791 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
794 struct wim_lookup_table_entry *lte;
795 struct hlist_node *pos;
797 wimlib_assert(table != NULL);
798 wimlib_assert(hash != NULL);
800 i = *(size_t*)hash % table->capacity;
801 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
802 if (hashes_equal(hash, lte->hash))
809 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
812 * This is only for pre-resolved inodes.
815 lookup_resource(WIMStruct *w,
818 struct wim_dentry **dentry_ret,
819 struct wim_lookup_table_entry **lte_ret,
822 struct wim_dentry *dentry;
823 struct wim_lookup_table_entry *lte;
825 const tchar *stream_name = NULL;
826 struct wim_inode *inode;
829 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
830 stream_name = path_stream_name(path);
832 p = (tchar*)stream_name - 1;
837 dentry = get_dentry(w, path);
843 inode = dentry->d_inode;
845 if (!inode->i_resolved)
846 if (inode_resolve_ltes(inode, w->lookup_table))
849 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
850 && inode_is_directory(inode))
854 struct wim_ads_entry *ads_entry;
856 ads_entry = inode_get_ads_entry(inode, stream_name,
859 stream_idx = ads_idx + 1;
860 lte = ads_entry->lte;
871 *dentry_ret = dentry;
875 *stream_idx_ret = stream_idx;
880 /* Resolve an inode's lookup table entries
882 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
883 * lookup table) with pointers directly to the lookup table entries. A circular
884 * linked list of streams sharing the same lookup table entry is created.
886 * This function always succeeds; unresolved lookup table entries are given a
890 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
894 if (!inode->i_resolved) {
895 struct wim_lookup_table_entry *lte, *ads_lte;
897 /* Resolve the default file stream */
899 hash = inode->i_hash;
900 if (!is_zero_hash(hash)) {
901 lte = __lookup_resource(table, hash);
903 goto resource_not_found;
906 /* Resolve the alternate data streams */
907 struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
908 for (u16 i = 0; i < inode->i_num_ads; i++) {
909 struct wim_ads_entry *cur_entry;
912 cur_entry = &inode->i_ads_entries[i];
913 hash = cur_entry->hash;
914 if (!is_zero_hash(hash)) {
915 ads_lte = __lookup_resource(table, hash);
916 if (unlikely(!ads_lte))
917 goto resource_not_found;
919 ads_ltes[i] = ads_lte;
922 for (u16 i = 0; i < inode->i_num_ads; i++)
923 inode->i_ads_entries[i].lte = ads_ltes[i];
924 inode->i_resolved = 1;
928 if (wimlib_print_errors) {
929 ERROR("\"%"TS"\": resource not found", inode_first_full_path(inode));
930 tfprintf(stderr, T(" SHA-1 message digest of missing resource:\n "));
931 print_hash(hash, stderr);
932 tputc(T('\n'), stderr);
934 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
938 inode_unresolve_ltes(struct wim_inode *inode)
940 if (inode->i_resolved) {
942 copy_hash(inode->i_hash, inode->i_lte->hash);
944 zero_out_hash(inode->i_hash);
946 for (u16 i = 0; i < inode->i_num_ads; i++) {
947 if (inode->i_ads_entries[i].lte)
948 copy_hash(inode->i_ads_entries[i].hash,
949 inode->i_ads_entries[i].lte->hash);
951 zero_out_hash(inode->i_ads_entries[i].hash);
953 inode->i_resolved = 0;
958 * Returns the lookup table entry for stream @stream_idx of the inode, where
959 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
960 * corresponds to an alternate data stream.
962 * This works for both resolved and un-resolved inodes.
964 struct wim_lookup_table_entry *
965 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
966 const struct wim_lookup_table *table)
968 if (inode->i_resolved)
969 return inode_stream_lte_resolved(inode, stream_idx);
971 return inode_stream_lte_unresolved(inode, stream_idx, table);
974 struct wim_lookup_table_entry *
975 inode_unnamed_lte_resolved(const struct wim_inode *inode)
977 wimlib_assert(inode->i_resolved);
978 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
979 if (inode_stream_name_nbytes(inode, i) == 0 &&
980 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
982 return inode_stream_lte_resolved(inode, i);
988 struct wim_lookup_table_entry *
989 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
990 const struct wim_lookup_table *table)
992 wimlib_assert(!inode->i_resolved);
993 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
994 if (inode_stream_name_nbytes(inode, i) == 0 &&
995 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
997 return inode_stream_lte_unresolved(inode, i, table);
1003 /* Return the lookup table entry for the unnamed data stream of an inode, or
1004 * NULL if there is none.
1006 * You'd think this would be easier than it actually is, since the unnamed data
1007 * stream should be the one referenced from the inode itself. Alas, if there
1008 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
1009 * data stream in one of the alternate data streams instead of inside the WIM
1010 * dentry itself. So we need to check the alternate data streams too.
1012 * Also, note that a dentry may appear to have more than one unnamed stream, but
1013 * if the SHA1 message digest is all 0's then the corresponding stream does not
1014 * really "count" (this is the case for the inode's own file stream when the
1015 * file stream that should be there is actually in one of the alternate stream
1016 * entries.). This is despite the fact that we may need to extract such a
1017 * missing entry as an empty file or empty named data stream.
1019 struct wim_lookup_table_entry *
1020 inode_unnamed_lte(const struct wim_inode *inode,
1021 const struct wim_lookup_table *table)
1023 if (inode->i_resolved)
1024 return inode_unnamed_lte_resolved(inode);
1026 return inode_unnamed_lte_unresolved(inode, table);
1030 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
1032 *(u64*)total_bytes_p += lte->resource_entry.size;
1037 lookup_table_total_stream_size(struct wim_lookup_table *table)
1040 for_lookup_table_entry(table, lte_add_stream_size, &total_size);
1044 struct wim_lookup_table_entry **
1045 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
1047 wimlib_assert(lte->unhashed);
1048 struct wim_inode *inode = lte->back_inode;
1049 u32 stream_id = lte->back_stream_id;
1051 return &inode->i_lte;
1053 for (u16 i = 0; i < inode->i_num_ads; i++)
1054 if (inode->i_ads_entries[i].stream_id == stream_id)
1055 return &inode->i_ads_entries[i].lte;
1060 /* Calculate the SHA1 message digest of a stream and move it from the list of
1061 * unhashed streams to the stream lookup table, possibly joining it with an
1062 * existing lookup table entry for an identical stream.
1064 * @lte: An unhashed lookup table entry.
1065 * @lookup_table: Lookup table for the WIM.
1066 * @lte_ret: On success, write a pointer to the resulting lookup table
1067 * entry to this location. This will be the same as @lte
1068 * if it was inserted into the lookup table, or different if
1069 * a duplicate stream was found.
1071 * Returns 0 on success; nonzero if there is an error reading the stream.
1074 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
1075 struct wim_lookup_table *lookup_table,
1076 struct wim_lookup_table_entry **lte_ret)
1079 struct wim_lookup_table_entry *duplicate_lte;
1080 struct wim_lookup_table_entry **back_ptr;
1082 wimlib_assert(lte->unhashed);
1084 /* back_ptr must be saved because @back_inode and @back_stream_id are in
1085 * union with the SHA1 message digest and will no longer be valid once
1086 * the SHA1 has been calculated. */
1087 back_ptr = retrieve_lte_pointer(lte);
1089 ret = sha1_resource(lte);
1093 /* Look for a duplicate stream */
1094 duplicate_lte = __lookup_resource(lookup_table, lte->hash);
1095 list_del(<e->unhashed_list);
1096 if (duplicate_lte) {
1097 /* We have a duplicate stream. Transfer the reference counts
1098 * from this stream to the duplicate, update the reference to
1099 * this stream (in an inode or ads_entry) to point to the
1100 * duplicate, then free this stream. */
1101 wimlib_assert(!(duplicate_lte->unhashed));
1102 duplicate_lte->refcnt += lte->refcnt;
1103 duplicate_lte->out_refcnt += lte->refcnt;
1104 *back_ptr = duplicate_lte;
1105 free_lookup_table_entry(lte);
1106 lte = duplicate_lte;
1108 /* No duplicate stream, so we need to insert
1109 * this stream into the lookup table and treat
1110 * it as a hashed stream. */
1111 lookup_table_insert(lookup_table, lte);