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/buffer_io.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 /* Size of each lookup table entry in the WIM file. */
47 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
49 struct wim_lookup_table *
50 new_lookup_table(size_t capacity)
52 struct wim_lookup_table *table;
53 struct hlist_head *array;
55 table = CALLOC(1, sizeof(struct wim_lookup_table));
57 array = CALLOC(capacity, sizeof(array[0]));
59 table->num_entries = 0;
60 table->capacity = capacity;
65 ERROR("Failed to allocate memory for lookup table "
66 "with capacity %zu", capacity);
72 struct wim_lookup_table_entry *
73 new_lookup_table_entry(void)
75 struct wim_lookup_table_entry *lte;
77 lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
82 ERROR("Out of memory (tried to allocate %zu bytes for "
83 "lookup table entry)",
84 sizeof(struct wim_lookup_table_entry));
89 struct wim_lookup_table_entry *
90 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
92 struct wim_lookup_table_entry *new;
94 new = MALLOC(sizeof(*new));
98 memcpy(new, old, sizeof(*old));
99 new->extracted_file = NULL;
100 switch (new->resource_location) {
103 case RESOURCE_WIN32_ENCRYPTED:
105 case RESOURCE_IN_FILE_ON_DISK:
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 = MALLOC(wim_resource_size(old));
118 if (!new->attached_buffer)
120 memcpy(new->attached_buffer, old->attached_buffer,
121 wim_resource_size(old));
124 case RESOURCE_IN_NTFS_VOLUME:
126 struct ntfs_location *loc;
127 loc = MALLOC(sizeof(*loc));
130 memcpy(loc, old->ntfs_loc, sizeof(*loc));
132 loc->stream_name = NULL;
134 loc->path = STRDUP(old->ntfs_loc->path);
137 loc->stream_name = MALLOC((loc->stream_name_nchars + 1) * 2);
138 if (!loc->stream_name)
140 memcpy(loc->stream_name,
141 old->ntfs_loc->stream_name,
142 (loc->stream_name_nchars + 1) * 2);
151 free_lookup_table_entry(new);
156 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
159 switch (lte->resource_location) {
162 case RESOURCE_WIN32_ENCRYPTED:
164 case RESOURCE_IN_FILE_ON_DISK:
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 DEBUG2("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);
301 cmp_streams_by_wim_position(const void *p1, const void *p2)
303 const struct wim_lookup_table_entry *lte1, *lte2;
304 lte1 = *(const struct wim_lookup_table_entry**)p1;
305 lte2 = *(const struct wim_lookup_table_entry**)p2;
306 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
308 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
316 add_lte_to_array(struct wim_lookup_table_entry *lte,
319 struct wim_lookup_table_entry ***pp = _pp;
324 /* Iterate through the lookup table entries, but first sort them by stream
325 * offset in the WIM. Caution: this is intended to be used when the stream
326 * offset field has actually been set. */
328 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
329 int (*visitor)(struct wim_lookup_table_entry *,
333 struct wim_lookup_table_entry **lte_array, **p;
334 size_t num_streams = table->num_entries;
337 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
339 return WIMLIB_ERR_NOMEM;
341 for_lookup_table_entry(table, add_lte_to_array, &p);
343 wimlib_assert(p == lte_array + num_streams);
345 qsort(lte_array, num_streams, sizeof(lte_array[0]),
346 cmp_streams_by_wim_position);
348 for (size_t i = 0; i < num_streams; i++) {
349 ret = visitor(lte_array[i], arg);
358 * Reads the lookup table from a WIM file.
360 * Saves lookup table entries for non-metadata streams in a hash table, and
361 * saves the metadata entry for each image in a special per-image location (the
362 * image_metadata array).
365 read_lookup_table(WIMStruct *w)
369 struct wim_lookup_table *table;
370 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
371 u8 table_buf[(BUFFER_SIZE / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE) *
372 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
375 size_t buf_entries_remaining;
377 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
378 w->hdr.lookup_table_res_entry.offset,
379 w->hdr.lookup_table_res_entry.original_size);
381 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
382 ERROR("Didn't expect a compressed lookup table!");
383 ERROR("Ask the author to implement support for this.");
384 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
387 num_entries = w->hdr.lookup_table_res_entry.size /
388 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
389 table = new_lookup_table(num_entries * 2 + 1);
391 return WIMLIB_ERR_NOMEM;
393 w->current_image = 0;
394 offset = w->hdr.lookup_table_res_entry.offset;
395 buf_entries_remaining = 0;
396 for (; num_entries != 0; num_entries--, buf_entries_remaining--) {
397 if (buf_entries_remaining == 0) {
398 size_t entries_to_read, bytes_to_read;
400 entries_to_read = min(sizeof(table_buf) /
401 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE,
403 bytes_to_read = entries_to_read *
404 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
405 if (full_pread(w->in_fd, table_buf,
406 bytes_to_read, offset) != bytes_to_read)
408 ERROR_WITH_ERRNO("Error reading lookup table "
409 "(offset=%"PRIu64")", offset);
410 ret = WIMLIB_ERR_READ;
411 goto out_free_lookup_table;
413 offset += bytes_to_read;
415 buf_entries_remaining = entries_to_read;
417 cur_entry = new_lookup_table_entry();
419 ret = WIMLIB_ERR_NOMEM;
420 goto out_free_lookup_table;
424 cur_entry->resource_location = RESOURCE_IN_WIM;
425 p = get_resource_entry(p, &cur_entry->resource_entry);
426 p = get_u16(p, &cur_entry->part_number);
427 p = get_u32(p, &cur_entry->refcnt);
428 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
430 if (cur_entry->part_number != w->hdr.part_number) {
431 ERROR("A lookup table entry in part %hu of the WIM "
432 "points to part %hu",
433 w->hdr.part_number, cur_entry->part_number);
434 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
435 goto out_free_cur_entry;
438 if (is_zero_hash(cur_entry->hash)) {
439 ERROR("The WIM lookup table contains an entry with a "
440 "SHA1 message digest of all 0's");
441 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
442 goto out_free_cur_entry;
445 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
446 && (cur_entry->resource_entry.size !=
447 cur_entry->resource_entry.original_size))
449 #ifdef ENABLE_ERROR_MESSAGES
450 ERROR("Found uncompressed resource with original size "
451 "not the same as compressed size");
452 ERROR("The lookup table entry for the resource is as follows:");
453 print_lookup_table_entry(cur_entry, stderr);
455 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
456 goto out_free_cur_entry;
459 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
460 /* Lookup table entry for a metadata resource */
461 if (cur_entry->refcnt != 1) {
462 #ifdef ENABLE_ERROR_MESSAGES
463 ERROR("Found metadata resource with refcnt != 1:");
464 print_lookup_table_entry(cur_entry, stderr);
466 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
467 goto out_free_cur_entry;
470 if (w->hdr.part_number != 1) {
471 ERROR("Found a metadata resource in a "
472 "non-first part of the split WIM!");
473 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
474 goto out_free_cur_entry;
476 if (w->current_image == w->hdr.image_count) {
477 ERROR("The WIM header says there are %u images "
478 "in the WIM, but we found more metadata "
479 "resources than this", w->hdr.image_count);
480 ret = WIMLIB_ERR_IMAGE_COUNT;
481 goto out_free_cur_entry;
484 /* Notice very carefully: We are assigning the metadata
485 * resources in the exact order mirrored by their lookup
486 * table entries on disk, which is the behavior of
487 * Microsoft's software. In particular, this overrides
488 * the actual locations of the metadata resources
489 * themselves in the WIM file as well as any information
490 * written in the XML data. */
491 DEBUG("Found metadata resource for image %u at "
493 w->current_image + 1,
494 cur_entry->resource_entry.offset);
496 w->current_image++]->metadata_lte = cur_entry;
498 /* Lookup table entry for a stream that is not a
499 * metadata resource */
500 duplicate_entry = __lookup_resource(table, cur_entry->hash);
501 if (duplicate_entry) {
502 #ifdef ENABLE_ERROR_MESSAGES
503 ERROR("The WIM lookup table contains two entries with the "
504 "same SHA1 message digest!");
505 ERROR("The first entry is:");
506 print_lookup_table_entry(duplicate_entry, stderr);
507 ERROR("The second entry is:");
508 print_lookup_table_entry(cur_entry, stderr);
510 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
511 goto out_free_cur_entry;
513 lookup_table_insert(table, cur_entry);
517 if (w->hdr.part_number == 1 && w->current_image != w->hdr.image_count)
519 ERROR("The WIM header says there are %u images "
520 "in the WIM, but we only found %d metadata "
521 "resources!", w->hdr.image_count, w->current_image);
522 ret = WIMLIB_ERR_IMAGE_COUNT;
523 goto out_free_lookup_table;
525 DEBUG("Done reading lookup table.");
526 w->lookup_table = table;
531 out_free_lookup_table:
532 free_lookup_table(table);
534 w->current_image = 0;
540 write_lookup_table_entry(const struct wim_lookup_table_entry *lte, u8 *buf_p)
542 buf_p = put_resource_entry(buf_p, <e->output_resource_entry);
543 buf_p = put_u16(buf_p, lte->part_number);
544 buf_p = put_u32(buf_p, lte->out_refcnt);
545 buf_p = put_bytes(buf_p, SHA1_HASH_SIZE, lte->hash);
550 write_lookup_table_from_stream_list(struct list_head *stream_list,
552 struct resource_entry *out_res_entry)
556 u8 table_buf[(BUFFER_SIZE / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE) *
557 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
560 size_t bytes_to_write;
561 struct wim_lookup_table_entry *lte;
563 start_offset = filedes_offset(out_fd);
564 if (start_offset == -1)
569 list_for_each_entry(lte, stream_list, lookup_table_list) {
570 if (buf_p == table_buf + sizeof(table_buf)) {
571 bytes_to_write = sizeof(table_buf);
572 if (full_write(out_fd, table_buf,
573 bytes_to_write) != bytes_to_write)
575 table_size += bytes_to_write;
578 buf_p = write_lookup_table_entry(lte, buf_p);
580 bytes_to_write = buf_p - table_buf;
581 if (bytes_to_write != 0) {
582 if (full_write(out_fd, table_buf,
583 bytes_to_write) != bytes_to_write)
585 table_size += bytes_to_write;
587 out_res_entry->offset = start_offset;
588 out_res_entry->size = table_size;
589 out_res_entry->original_size = table_size;
590 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
595 ERROR_WITH_ERRNO("Failed to write lookup table");
596 ret = WIMLIB_ERR_WRITE;
601 append_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_list)
603 if (lte->out_refcnt != 0)
604 list_add_tail(<e->lookup_table_list, (struct list_head*)_list);
608 /* Writes the WIM lookup table to the output file. */
610 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
612 LIST_HEAD(stream_list);
616 if (image == WIMLIB_ALL_IMAGES) {
618 end_image = w->hdr.image_count;
624 for (int i = start_image; i <= end_image; i++) {
625 struct wim_lookup_table_entry *metadata_lte;
627 metadata_lte = w->image_metadata[i - 1]->metadata_lte;
628 metadata_lte->out_refcnt = 1;
629 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
630 append_lookup_table_entry(metadata_lte, &stream_list);
632 for_lookup_table_entry(w->lookup_table,
633 append_lookup_table_entry,
635 return write_lookup_table_from_stream_list(&stream_list,
641 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
643 lte->real_refcnt = 0;
648 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
655 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
657 if (lte->extracted_file != NULL) {
658 FREE(lte->extracted_file);
659 lte->extracted_file = NULL;
665 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
671 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
672 lte->resource_entry.offset);
674 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
675 (u64)lte->resource_entry.size);
677 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
678 lte->resource_entry.original_size);
680 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
681 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
684 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
685 lte->back_inode, lte->back_stream_id);
687 tfprintf(out, T("Hash = 0x"));
688 print_hash(lte->hash, out);
692 tfprintf(out, T("Flags = "));
693 u8 flags = lte->resource_entry.flags;
694 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
695 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
696 if (flags & WIM_RESHDR_FLAG_FREE)
697 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
698 if (flags & WIM_RESHDR_FLAG_METADATA)
699 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
700 if (flags & WIM_RESHDR_FLAG_SPANNED)
701 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
703 switch (lte->resource_location) {
704 case RESOURCE_IN_WIM:
705 if (lte->wim->filename) {
706 tfprintf(out, T("WIM file = `%"TS"'\n"),
712 case RESOURCE_WIN32_ENCRYPTED:
714 case RESOURCE_IN_FILE_ON_DISK:
716 tfprintf(out, T("File on Disk = `%"TS"'\n"),
720 case RESOURCE_IN_STAGING_FILE:
721 tfprintf(out, T("Staging File = `%"TS"'\n"),
722 lte->staging_file_name);
732 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
734 print_lookup_table_entry(lte, (FILE*)fp);
739 * Prints the lookup table of a WIM file.
742 wimlib_print_lookup_table(WIMStruct *w)
744 for_lookup_table_entry(w->lookup_table,
745 do_print_lookup_table_entry,
749 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
750 * lookup table, or NULL if there is none. */
751 struct wim_lookup_table_entry *
752 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
755 struct wim_lookup_table_entry *lte;
756 struct hlist_node *pos;
758 wimlib_assert(table != NULL);
759 wimlib_assert(hash != NULL);
761 i = *(size_t*)hash % table->capacity;
762 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
763 if (hashes_equal(hash, lte->hash))
770 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
773 * This is only for pre-resolved inodes.
776 lookup_resource(WIMStruct *w,
779 struct wim_dentry **dentry_ret,
780 struct wim_lookup_table_entry **lte_ret,
783 struct wim_dentry *dentry;
784 struct wim_lookup_table_entry *lte;
786 const tchar *stream_name = NULL;
787 struct wim_inode *inode;
790 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
791 stream_name = path_stream_name(path);
793 p = (tchar*)stream_name - 1;
798 dentry = get_dentry(w, path);
804 inode = dentry->d_inode;
806 wimlib_assert(inode->i_resolved);
808 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
809 && inode_is_directory(inode))
813 struct wim_ads_entry *ads_entry;
815 ads_entry = inode_get_ads_entry(inode, stream_name,
818 stream_idx = ads_idx + 1;
819 lte = ads_entry->lte;
830 *dentry_ret = dentry;
834 *stream_idx_ret = stream_idx;
840 * XXX Probably should store the compression type directly in the lookup table
844 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
846 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
847 || lte->resource_location != RESOURCE_IN_WIM)
848 return WIMLIB_COMPRESSION_TYPE_NONE;
849 return wimlib_get_compression_type(lte->wim);
852 /* Resolve an inode's lookup table entries
854 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
855 * lookup table) with pointers directly to the lookup table entries. A circular
856 * linked list of streams sharing the same lookup table entry is created.
858 * This function always succeeds; unresolved lookup table entries are given a
862 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
865 if (!inode->i_resolved) {
866 struct wim_lookup_table_entry *lte;
867 /* Resolve the default file stream */
868 lte = __lookup_resource(table, inode->i_hash);
870 inode->i_resolved = 1;
872 /* Resolve the alternate data streams */
873 for (u16 i = 0; i < inode->i_num_ads; i++) {
874 struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
875 lte = __lookup_resource(table, cur_entry->hash);
876 cur_entry->lte = lte;
882 inode_unresolve_ltes(struct wim_inode *inode)
884 if (inode->i_resolved) {
886 copy_hash(inode->i_hash, inode->i_lte->hash);
888 zero_out_hash(inode->i_hash);
890 for (u16 i = 0; i < inode->i_num_ads; i++) {
891 if (inode->i_ads_entries[i].lte)
892 copy_hash(inode->i_ads_entries[i].hash,
893 inode->i_ads_entries[i].lte->hash);
895 zero_out_hash(inode->i_ads_entries[i].hash);
897 inode->i_resolved = 0;
902 * Returns the lookup table entry for stream @stream_idx of the inode, where
903 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
904 * corresponds to an alternate data stream.
906 * This works for both resolved and un-resolved inodes.
908 struct wim_lookup_table_entry *
909 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
910 const struct wim_lookup_table *table)
912 if (inode->i_resolved)
913 return inode_stream_lte_resolved(inode, stream_idx);
915 return inode_stream_lte_unresolved(inode, stream_idx, table);
918 struct wim_lookup_table_entry *
919 inode_unnamed_lte_resolved(const struct wim_inode *inode)
921 wimlib_assert(inode->i_resolved);
922 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
923 if (inode_stream_name_nbytes(inode, i) == 0 &&
924 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
926 return inode_stream_lte_resolved(inode, i);
932 struct wim_lookup_table_entry *
933 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
934 const struct wim_lookup_table *table)
936 wimlib_assert(!inode->i_resolved);
937 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
938 if (inode_stream_name_nbytes(inode, i) == 0 &&
939 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
941 return inode_stream_lte_unresolved(inode, i, table);
947 /* Return the lookup table entry for the unnamed data stream of an inode, or
948 * NULL if there is none.
950 * You'd think this would be easier than it actually is, since the unnamed data
951 * stream should be the one referenced from the inode itself. Alas, if there
952 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
953 * data stream in one of the alternate data streams instead of inside the WIM
954 * dentry itself. So we need to check the alternate data streams too.
956 * Also, note that a dentry may appear to have more than one unnamed stream, but
957 * if the SHA1 message digest is all 0's then the corresponding stream does not
958 * really "count" (this is the case for the inode's own file stream when the
959 * file stream that should be there is actually in one of the alternate stream
960 * entries.). This is despite the fact that we may need to extract such a
961 * missing entry as an empty file or empty named data stream.
963 struct wim_lookup_table_entry *
964 inode_unnamed_lte(const struct wim_inode *inode,
965 const struct wim_lookup_table *table)
967 if (inode->i_resolved)
968 return inode_unnamed_lte_resolved(inode);
970 return inode_unnamed_lte_unresolved(inode, table);
974 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
976 *(u64*)total_bytes_p += lte->resource_entry.size;
981 lookup_table_total_stream_size(struct wim_lookup_table *table)
984 for_lookup_table_entry(table, lte_add_stream_size, &total_size);
988 struct wim_lookup_table_entry **
989 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
991 wimlib_assert(lte->unhashed);
992 struct wim_inode *inode = lte->back_inode;
993 u32 stream_id = lte->back_stream_id;
995 return &inode->i_lte;
997 for (u16 i = 0; i < inode->i_num_ads; i++)
998 if (inode->i_ads_entries[i].stream_id == stream_id)
999 return &inode->i_ads_entries[i].lte;
1004 /* Calculate the SHA1 message digest of a stream and move it from the list of
1005 * unhashed streams to the stream lookup table, possibly joining it with an
1006 * existing lookup table entry for an identical stream.
1008 * @lte: An unhashed lookup table entry.
1009 * @lookup_table: Lookup table for the WIM.
1010 * @lte_ret: On success, write a pointer to the resulting lookup table
1011 * entry to this location. This will be the same as @lte
1012 * if it was inserted into the lookup table, or different if
1013 * a duplicate stream was found.
1015 * Returns 0 on success; nonzero if there is an error reading the stream.
1018 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
1019 struct wim_lookup_table *lookup_table,
1020 struct wim_lookup_table_entry **lte_ret)
1023 struct wim_lookup_table_entry *duplicate_lte;
1024 struct wim_lookup_table_entry **back_ptr;
1026 wimlib_assert(lte->unhashed);
1028 /* back_ptr must be saved because @back_inode and @back_stream_id are in
1029 * union with the SHA1 message digest and will no longer be valid once
1030 * the SHA1 has been calculated. */
1031 back_ptr = retrieve_lte_pointer(lte);
1033 ret = sha1_resource(lte);
1037 /* Look for a duplicate stream */
1038 duplicate_lte = __lookup_resource(lookup_table, lte->hash);
1039 list_del(<e->unhashed_list);
1040 if (duplicate_lte) {
1041 /* We have a duplicate stream. Transfer the reference counts
1042 * from this stream to the duplicate, update the reference to
1043 * this stream (in an inode or ads_entry) to point to the
1044 * duplicate, then free this stream. */
1045 wimlib_assert(!(duplicate_lte->unhashed));
1046 duplicate_lte->refcnt += lte->refcnt;
1047 duplicate_lte->out_refcnt += lte->refcnt;
1048 *back_ptr = duplicate_lte;
1049 free_lookup_table_entry(lte);
1050 lte = duplicate_lte;
1052 /* No duplicate stream, so we need to insert
1053 * this stream into the lookup table and treat
1054 * it as a hashed stream. */
1055 lookup_table_insert(lookup_table, lte);