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/.
27 #include "wimlib_internal.h"
28 #include "lookup_table.h"
29 #include "buffer_io.h"
37 struct wim_lookup_table *
38 new_lookup_table(size_t capacity)
40 struct wim_lookup_table *table;
41 struct hlist_head *array;
43 table = CALLOC(1, sizeof(struct wim_lookup_table));
45 array = CALLOC(capacity, sizeof(array[0]));
47 table->num_entries = 0;
48 table->capacity = capacity;
53 ERROR("Failed to allocate memory for lookup table with capacity %zu",
60 struct wim_lookup_table_entry *
61 new_lookup_table_entry()
63 struct wim_lookup_table_entry *lte;
65 lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
70 ERROR("Out of memory (tried to allocate %zu bytes for "
71 "lookup table entry)",
72 sizeof(struct wim_lookup_table_entry));
77 struct wim_lookup_table_entry *
78 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
80 struct wim_lookup_table_entry *new;
82 new = MALLOC(sizeof(*new));
86 memcpy(new, old, sizeof(*old));
87 new->extracted_file = NULL;
88 switch (new->resource_location) {
91 case RESOURCE_WIN32_ENCRYPTED:
93 case RESOURCE_IN_FILE_ON_DISK:
96 case RESOURCE_IN_STAGING_FILE:
97 BUILD_BUG_ON((void*)&old->file_on_disk !=
98 (void*)&old->staging_file_name);
100 new->file_on_disk = TSTRDUP(old->file_on_disk);
101 if (!new->file_on_disk)
104 case RESOURCE_IN_ATTACHED_BUFFER:
105 new->attached_buffer = MALLOC(wim_resource_size(old));
106 if (!new->attached_buffer)
108 memcpy(new->attached_buffer, old->attached_buffer,
109 wim_resource_size(old));
112 case RESOURCE_IN_NTFS_VOLUME:
114 struct ntfs_location *loc;
115 loc = MALLOC(sizeof(*loc));
118 memcpy(loc, old->ntfs_loc, sizeof(*loc));
120 loc->stream_name = NULL;
122 loc->path = STRDUP(old->ntfs_loc->path);
125 loc->stream_name = MALLOC((loc->stream_name_nchars + 1) * 2);
126 if (!loc->stream_name)
128 memcpy(loc->stream_name,
129 old->ntfs_loc->stream_name,
130 (loc->stream_name_nchars + 1) * 2);
139 free_lookup_table_entry(new);
144 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
147 switch (lte->resource_location) {
150 case RESOURCE_WIN32_ENCRYPTED:
152 case RESOURCE_IN_FILE_ON_DISK:
155 case RESOURCE_IN_STAGING_FILE:
156 BUILD_BUG_ON((void*)<e->file_on_disk !=
157 (void*)<e->staging_file_name);
159 case RESOURCE_IN_ATTACHED_BUFFER:
160 BUILD_BUG_ON((void*)<e->file_on_disk !=
161 (void*)<e->attached_buffer);
162 FREE(lte->file_on_disk);
165 case RESOURCE_IN_NTFS_VOLUME:
167 FREE(lte->ntfs_loc->path);
168 FREE(lte->ntfs_loc->stream_name);
181 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
183 free_lookup_table_entry(entry);
189 free_lookup_table(struct wim_lookup_table *table)
191 DEBUG2("Freeing lookup table");
194 for_lookup_table_entry(table,
195 do_free_lookup_table_entry,
204 * Inserts an entry into the lookup table.
206 * @table: A pointer to the lookup table.
207 * @lte: A pointer to the entry to insert.
210 lookup_table_insert(struct wim_lookup_table *table,
211 struct wim_lookup_table_entry *lte)
213 size_t i = lte->hash_short % table->capacity;
214 hlist_add_head(<e->hash_list, &table->array[i]);
216 /* XXX Make the table grow when too many entries have been inserted. */
217 table->num_entries++;
221 finalize_lte(struct wim_lookup_table_entry *lte)
224 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
225 unlink(lte->staging_file_name);
226 list_del(<e->unhashed_list);
229 free_lookup_table_entry(lte);
232 /* Decrements the reference count for the lookup table entry @lte. If its
233 * reference count reaches 0, it is unlinked from the lookup table. If,
234 * furthermore, the entry has no opened file descriptors associated with it, the
237 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
238 struct wim_lookup_table *table)
240 wimlib_assert(lte != NULL);
241 wimlib_assert(lte->refcnt != 0);
242 if (--lte->refcnt == 0) {
244 lookup_table_unlink(table, lte);
246 if (lte->num_opened_fds == 0)
254 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
256 if (lte->num_opened_fds != 0)
257 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
262 /* Calls a function on all the entries in the WIM lookup table. Stop early and
263 * return nonzero if any call to the function returns nonzero. */
265 for_lookup_table_entry(struct wim_lookup_table *table,
266 int (*visitor)(struct wim_lookup_table_entry *, void *),
269 struct wim_lookup_table_entry *lte;
270 struct hlist_node *pos, *tmp;
273 for (size_t i = 0; i < table->capacity; i++) {
274 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
277 wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
278 ret = visitor(lte, arg);
287 cmp_streams_by_wim_position(const void *p1, const void *p2)
289 const struct wim_lookup_table_entry *lte1, *lte2;
290 lte1 = *(const struct wim_lookup_table_entry**)p1;
291 lte2 = *(const struct wim_lookup_table_entry**)p2;
292 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
294 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
302 add_lte_to_array(struct wim_lookup_table_entry *lte,
305 struct wim_lookup_table_entry ***pp = _pp;
310 /* Iterate through the lookup table entries, but first sort them by stream
311 * offset in the WIM. Caution: this is intended to be used when the stream
312 * offset field has actually been set. */
314 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
315 int (*visitor)(struct wim_lookup_table_entry *,
319 struct wim_lookup_table_entry **lte_array, **p;
320 size_t num_streams = table->num_entries;
323 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
325 return WIMLIB_ERR_NOMEM;
327 for_lookup_table_entry(table, add_lte_to_array, &p);
329 wimlib_assert(p == lte_array + num_streams);
331 qsort(lte_array, num_streams, sizeof(lte_array[0]),
332 cmp_streams_by_wim_position);
334 for (size_t i = 0; i < num_streams; i++) {
335 ret = visitor(lte_array[i], arg);
344 * Reads the lookup table from a WIM file.
346 * Saves lookup table entries for non-metadata streams in a hash table, and
347 * saves the metadata entry for each image in a special per-image location (the
348 * image_metadata array).
351 read_lookup_table(WIMStruct *w)
355 struct wim_lookup_table *table;
356 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
361 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
362 w->hdr.lookup_table_res_entry.offset,
363 w->hdr.lookup_table_res_entry.original_size);
365 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
366 ERROR("Didn't expect a compressed lookup table!");
367 ERROR("Ask the author to implement support for this.");
368 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
371 table_size = w->hdr.lookup_table_res_entry.size;
372 if (table_size != w->hdr.lookup_table_res_entry.size) {
373 ERROR("Lookup table is invalid");
374 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
377 table_buf = MALLOC(table_size);
379 return WIMLIB_ERR_NOMEM;
381 if (full_pread(w->in_fd, table_buf, table_size,
382 w->hdr.lookup_table_res_entry.offset) != table_size)
384 ret = WIMLIB_ERR_READ;
385 goto out_free_table_buf;
388 num_entries = w->hdr.lookup_table_res_entry.original_size /
389 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
390 table = new_lookup_table(num_entries * 2 + 1);
392 ret = WIMLIB_ERR_NOMEM;
393 goto out_free_table_buf;
396 w->current_image = 0;
398 while (num_entries--) {
399 cur_entry = new_lookup_table_entry();
401 ret = WIMLIB_ERR_NOMEM;
402 goto out_free_lookup_table;
406 cur_entry->resource_location = RESOURCE_IN_WIM;
407 p = get_resource_entry(p, &cur_entry->resource_entry);
408 p = get_u16(p, &cur_entry->part_number);
409 p = get_u32(p, &cur_entry->refcnt);
410 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
412 if (cur_entry->part_number != w->hdr.part_number) {
413 ERROR("A lookup table entry in part %hu of the WIM "
414 "points to part %hu",
415 w->hdr.part_number, cur_entry->part_number);
416 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
417 goto out_free_cur_entry;
420 if (is_zero_hash(cur_entry->hash)) {
421 ERROR("The WIM lookup table contains an entry with a "
422 "SHA1 message digest of all 0's");
423 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
424 goto out_free_cur_entry;
427 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
428 && (cur_entry->resource_entry.size !=
429 cur_entry->resource_entry.original_size))
431 #ifdef ENABLE_ERROR_MESSAGES
432 ERROR("Found uncompressed resource with original size "
433 "not the same as compressed size");
434 ERROR("The lookup table entry for the resource is as follows:");
435 print_lookup_table_entry(cur_entry, stderr);
437 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
438 goto out_free_cur_entry;
441 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
442 /* Lookup table entry for a metadata resource */
443 if (cur_entry->refcnt != 1) {
444 #ifdef ENABLE_ERROR_MESSAGES
445 ERROR("Found metadata resource with refcnt != 1:");
446 print_lookup_table_entry(cur_entry, stderr);
448 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
449 goto out_free_cur_entry;
452 if (w->hdr.part_number != 1) {
453 ERROR("Found a metadata resource in a "
454 "non-first part of the split WIM!");
455 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
456 goto out_free_cur_entry;
458 if (w->current_image == w->hdr.image_count) {
459 ERROR("The WIM header says there are %u images "
460 "in the WIM, but we found more metadata "
461 "resources than this", w->hdr.image_count);
462 ret = WIMLIB_ERR_IMAGE_COUNT;
463 goto out_free_cur_entry;
466 /* Notice very carefully: We are assigning the metadata
467 * resources in the exact order mirrored by their lookup
468 * table entries on disk, which is the behavior of
469 * Microsoft's software. In particular, this overrides
470 * the actual locations of the metadata resources
471 * themselves in the WIM file as well as any information
472 * written in the XML data. */
473 DEBUG("Found metadata resource for image %u at "
475 w->current_image + 1,
476 cur_entry->resource_entry.offset);
478 w->current_image++]->metadata_lte = cur_entry;
480 /* Lookup table entry for a stream that is not a
481 * metadata resource */
482 duplicate_entry = __lookup_resource(table, cur_entry->hash);
483 if (duplicate_entry) {
484 #ifdef ENABLE_ERROR_MESSAGES
485 ERROR("The WIM lookup table contains two entries with the "
486 "same SHA1 message digest!");
487 ERROR("The first entry is:");
488 print_lookup_table_entry(duplicate_entry, stderr);
489 ERROR("The second entry is:");
490 print_lookup_table_entry(cur_entry, stderr);
492 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
493 goto out_free_cur_entry;
495 lookup_table_insert(table, cur_entry);
499 if (w->hdr.part_number == 1 &&
500 w->current_image != w->hdr.image_count)
502 ERROR("The WIM header says there are %u images "
503 "in the WIM, but we only found %d metadata "
504 "resources!", w->hdr.image_count, w->current_image);
505 ret = WIMLIB_ERR_IMAGE_COUNT;
506 goto out_free_lookup_table;
508 DEBUG("Done reading lookup table.");
509 w->lookup_table = table;
511 goto out_free_table_buf;
514 out_free_lookup_table:
515 free_lookup_table(table);
518 w->current_image = 0;
524 * Writes a lookup table entry to the output file.
527 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *buf_p)
529 buf_p = put_resource_entry(buf_p, <e->output_resource_entry);
530 buf_p = put_u16(buf_p, lte->part_number);
531 buf_p = put_u32(buf_p, lte->out_refcnt);
532 buf_p = put_bytes(buf_p, SHA1_HASH_SIZE, lte->hash);
537 write_lookup_table_from_stream_list(struct list_head *stream_list,
539 struct resource_entry *out_res_entry)
542 struct list_head *pos;
546 struct wim_lookup_table_entry *lte;
550 start_offset = filedes_offset(out_fd);
551 if (start_offset == -1)
552 return WIMLIB_ERR_WRITE;
555 list_for_each(pos, stream_list)
557 table_size = num_entries * WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
559 table_buf = MALLOC(table_size);
561 ERROR("Failed to allocate lookup table buffer of %zu bytes", table_size);
562 return WIMLIB_ERR_NOMEM;
566 list_for_each_entry(lte, stream_list, lookup_table_list)
567 buf_p = write_lookup_table_entry(lte, buf_p);
569 wimlib_assert(buf_p - table_buf == table_size);
571 if (full_write(out_fd, table_buf, table_size) != table_size) {
572 ERROR_WITH_ERRNO("Failed to write lookup table");
573 ret = WIMLIB_ERR_WRITE;
574 goto out_free_table_buf;
577 out_res_entry->offset = start_offset;
578 out_res_entry->size = table_size;
579 out_res_entry->original_size = table_size;
580 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
588 append_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_list)
590 struct list_head *list = _list;
592 if (lte->out_refcnt != 0)
593 list_add_tail(<e->lookup_table_list, list);
598 /* Writes the WIM lookup table to the output file. */
600 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
602 LIST_HEAD(stream_list);
606 if (image == WIMLIB_ALL_IMAGES) {
608 end_image = w->hdr.image_count;
614 for (int i = start_image; i <= end_image; i++) {
615 struct wim_lookup_table_entry *metadata_lte;
617 metadata_lte = w->image_metadata[i - 1]->metadata_lte;
618 metadata_lte->out_refcnt = 1;
619 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
620 append_lookup_table_entry(metadata_lte, &stream_list);
623 for_lookup_table_entry(w->lookup_table,
624 append_lookup_table_entry,
626 return write_lookup_table_from_stream_list(&stream_list,
632 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
634 lte->real_refcnt = 0;
639 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
646 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
648 if (lte->extracted_file != NULL) {
649 FREE(lte->extracted_file);
650 lte->extracted_file = NULL;
656 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
662 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
663 lte->resource_entry.offset);
665 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
666 (u64)lte->resource_entry.size);
668 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
669 lte->resource_entry.original_size);
671 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
672 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
675 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
676 lte->back_inode, lte->back_stream_id);
678 tfprintf(out, T("Hash = 0x"));
679 print_hash(lte->hash, out);
683 tfprintf(out, T("Flags = "));
684 u8 flags = lte->resource_entry.flags;
685 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
686 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
687 if (flags & WIM_RESHDR_FLAG_FREE)
688 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
689 if (flags & WIM_RESHDR_FLAG_METADATA)
690 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
691 if (flags & WIM_RESHDR_FLAG_SPANNED)
692 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
694 switch (lte->resource_location) {
695 case RESOURCE_IN_WIM:
696 if (lte->wim->filename) {
697 tfprintf(out, T("WIM file = `%"TS"'\n"),
703 case RESOURCE_WIN32_ENCRYPTED:
705 case RESOURCE_IN_FILE_ON_DISK:
707 tfprintf(out, T("File on Disk = `%"TS"'\n"),
711 case RESOURCE_IN_STAGING_FILE:
712 tfprintf(out, T("Staging File = `%"TS"'\n"),
713 lte->staging_file_name);
723 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
725 print_lookup_table_entry(lte, (FILE*)fp);
730 * Prints the lookup table of a WIM file.
733 wimlib_print_lookup_table(WIMStruct *w)
735 for_lookup_table_entry(w->lookup_table,
736 do_print_lookup_table_entry,
740 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
741 * lookup table, or NULL if there is none. */
742 struct wim_lookup_table_entry *
743 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
746 struct wim_lookup_table_entry *lte;
747 struct hlist_node *pos;
749 wimlib_assert(table != NULL);
750 wimlib_assert(hash != NULL);
752 i = *(size_t*)hash % table->capacity;
753 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
754 if (hashes_equal(hash, lte->hash))
761 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
764 * This is only for pre-resolved inodes.
767 lookup_resource(WIMStruct *w,
770 struct wim_dentry **dentry_ret,
771 struct wim_lookup_table_entry **lte_ret,
774 struct wim_dentry *dentry;
775 struct wim_lookup_table_entry *lte;
777 const tchar *stream_name = NULL;
778 struct wim_inode *inode;
781 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
782 stream_name = path_stream_name(path);
784 p = (tchar*)stream_name - 1;
789 dentry = get_dentry(w, path);
795 inode = dentry->d_inode;
797 wimlib_assert(inode->i_resolved);
799 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
800 && inode_is_directory(inode))
804 struct wim_ads_entry *ads_entry;
806 ads_entry = inode_get_ads_entry(inode, stream_name,
809 stream_idx = ads_idx + 1;
810 lte = ads_entry->lte;
821 *dentry_ret = dentry;
825 *stream_idx_ret = stream_idx;
830 /* Resolve an inode's lookup table entries
832 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
833 * lookup table) with pointers directly to the lookup table entries. A circular
834 * linked list of streams sharing the same lookup table entry is created.
836 * This function always succeeds; unresolved lookup table entries are given a
840 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
843 if (!inode->i_resolved) {
844 struct wim_lookup_table_entry *lte;
845 /* Resolve the default file stream */
846 lte = __lookup_resource(table, inode->i_hash);
848 inode->i_resolved = 1;
850 /* Resolve the alternate data streams */
851 for (u16 i = 0; i < inode->i_num_ads; i++) {
852 struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
853 lte = __lookup_resource(table, cur_entry->hash);
854 cur_entry->lte = lte;
860 inode_unresolve_ltes(struct wim_inode *inode)
862 if (inode->i_resolved) {
864 copy_hash(inode->i_hash, inode->i_lte->hash);
866 zero_out_hash(inode->i_hash);
868 for (u16 i = 0; i < inode->i_num_ads; i++) {
869 if (inode->i_ads_entries[i].lte)
870 copy_hash(inode->i_ads_entries[i].hash,
871 inode->i_ads_entries[i].lte->hash);
873 zero_out_hash(inode->i_ads_entries[i].hash);
875 inode->i_resolved = 0;
880 * Returns the lookup table entry for stream @stream_idx of the inode, where
881 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
882 * corresponds to an alternate data stream.
884 * This works for both resolved and un-resolved inodes.
886 struct wim_lookup_table_entry *
887 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
888 const struct wim_lookup_table *table)
890 if (inode->i_resolved)
891 return inode_stream_lte_resolved(inode, stream_idx);
893 return inode_stream_lte_unresolved(inode, stream_idx, table);
896 struct wim_lookup_table_entry *
897 inode_unnamed_lte_resolved(const struct wim_inode *inode)
899 wimlib_assert(inode->i_resolved);
900 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
901 if (inode_stream_name_nbytes(inode, i) == 0 &&
902 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
904 return inode_stream_lte_resolved(inode, i);
910 struct wim_lookup_table_entry *
911 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
912 const struct wim_lookup_table *table)
914 wimlib_assert(!inode->i_resolved);
915 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
916 if (inode_stream_name_nbytes(inode, i) == 0 &&
917 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
919 return inode_stream_lte_unresolved(inode, i, table);
925 /* Return the lookup table entry for the unnamed data stream of an inode, or
926 * NULL if there is none.
928 * You'd think this would be easier than it actually is, since the unnamed data
929 * stream should be the one referenced from the inode itself. Alas, if there
930 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
931 * data stream in one of the alternate data streams instead of inside the WIM
932 * dentry itself. So we need to check the alternate data streams too.
934 * Also, note that a dentry may appear to have more than one unnamed stream, but
935 * if the SHA1 message digest is all 0's then the corresponding stream does not
936 * really "count" (this is the case for the inode's own file stream when the
937 * file stream that should be there is actually in one of the alternate stream
938 * entries.). This is despite the fact that we may need to extract such a
939 * missing entry as an empty file or empty named data stream.
941 struct wim_lookup_table_entry *
942 inode_unnamed_lte(const struct wim_inode *inode,
943 const struct wim_lookup_table *table)
945 if (inode->i_resolved)
946 return inode_unnamed_lte_resolved(inode);
948 return inode_unnamed_lte_unresolved(inode, table);
952 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
954 *(u64*)total_bytes_p += lte->resource_entry.size;
959 lookup_table_total_stream_size(struct wim_lookup_table *table)
962 for_lookup_table_entry(table, lte_add_stream_size, &total_size);
966 struct wim_lookup_table_entry **
967 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
969 wimlib_assert(lte->unhashed);
970 struct wim_inode *inode = lte->back_inode;
971 u32 stream_id = lte->back_stream_id;
973 return &inode->i_lte;
975 for (u16 i = 0; i < inode->i_num_ads; i++)
976 if (inode->i_ads_entries[i].stream_id == stream_id)
977 return &inode->i_ads_entries[i].lte;
982 /* Calculate the SHA1 message digest of a stream and move it from the list of
983 * unhashed streams to the stream lookup table, possibly joining it with an
984 * existing lookup table entry for an identical stream.
986 * @lte: An unhashed lookup table entry.
987 * @lookup_table: Lookup table for the WIM.
988 * @lte_ret: On success, write a pointer to the resulting lookup table
989 * entry to this location. This will be the same as @lte
990 * if it was inserted into the lookup table, or different if
991 * a duplicate stream was found.
993 * Returns 0 on success; nonzero if there is an error reading the stream.
996 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
997 struct wim_lookup_table *lookup_table,
998 struct wim_lookup_table_entry **lte_ret)
1001 struct wim_lookup_table_entry *duplicate_lte;
1002 struct wim_lookup_table_entry **back_ptr;
1004 wimlib_assert(lte->unhashed);
1006 /* back_ptr must be saved because @back_inode and @back_stream_id are in
1007 * union with the SHA1 message digest and will no longer be valid once
1008 * the SHA1 has been calculated. */
1009 back_ptr = retrieve_lte_pointer(lte);
1011 ret = sha1_resource(lte);
1015 /* Look for a duplicate stream */
1016 duplicate_lte = __lookup_resource(lookup_table, lte->hash);
1017 list_del(<e->unhashed_list);
1018 if (duplicate_lte) {
1019 /* We have a duplicate stream. Transfer the reference counts
1020 * from this stream to the duplicate, update the reference to
1021 * this stream (in an inode or ads_entry) to point to the
1022 * duplicate, then free this stream. */
1023 wimlib_assert(!(duplicate_lte->unhashed));
1024 duplicate_lte->refcnt += lte->refcnt;
1025 duplicate_lte->out_refcnt += lte->refcnt;
1026 *back_ptr = duplicate_lte;
1027 free_lookup_table_entry(lte);
1028 lte = duplicate_lte;
1030 /* No duplicate stream, so we need to insert
1031 * this stream into the lookup table and treat
1032 * it as a hashed stream. */
1033 lookup_table_insert(lookup_table, lte);