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)
354 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
356 struct wim_lookup_table *table;
357 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
359 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
360 ERROR("Didn't expect a compressed lookup table!");
361 ERROR("Ask the author to implement support for this.");
362 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
365 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
366 w->hdr.lookup_table_res_entry.offset,
367 w->hdr.lookup_table_res_entry.original_size);
369 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
371 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
373 w->hdr.lookup_table_res_entry.offset);
374 return WIMLIB_ERR_READ;
377 num_entries = w->hdr.lookup_table_res_entry.original_size /
378 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
379 table = new_lookup_table(num_entries * 2 + 1);
381 return WIMLIB_ERR_NOMEM;
383 w->current_image = 0;
384 while (num_entries--) {
387 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
389 ERROR("Unexpected EOF in WIM lookup table!");
391 ERROR_WITH_ERRNO("Error reading WIM lookup "
394 ret = WIMLIB_ERR_READ;
395 goto out_free_lookup_table;
397 cur_entry = new_lookup_table_entry();
399 ret = WIMLIB_ERR_NOMEM;
400 goto out_free_lookup_table;
404 cur_entry->resource_location = RESOURCE_IN_WIM;
405 p = get_resource_entry(buf, &cur_entry->resource_entry);
406 p = get_u16(p, &cur_entry->part_number);
407 p = get_u32(p, &cur_entry->refcnt);
408 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
410 if (cur_entry->part_number != w->hdr.part_number) {
411 ERROR("A lookup table entry in part %hu of the WIM "
412 "points to part %hu",
413 w->hdr.part_number, cur_entry->part_number);
414 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
415 goto out_free_cur_entry;
418 if (is_zero_hash(cur_entry->hash)) {
419 ERROR("The WIM lookup table contains an entry with a "
420 "SHA1 message digest of all 0's");
421 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
422 goto out_free_cur_entry;
425 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
426 && (cur_entry->resource_entry.size !=
427 cur_entry->resource_entry.original_size))
429 #ifdef ENABLE_ERROR_MESSAGES
430 ERROR("Found uncompressed resource with original size "
431 "not the same as compressed size");
432 ERROR("The lookup table entry for the resource is as follows:");
433 print_lookup_table_entry(cur_entry, stderr);
435 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
436 goto out_free_cur_entry;
439 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
440 /* Lookup table entry for a metadata resource */
441 if (cur_entry->refcnt != 1) {
442 #ifdef ENABLE_ERROR_MESSAGES
443 ERROR("Found metadata resource with refcnt != 1:");
444 print_lookup_table_entry(cur_entry, stderr);
446 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
447 goto out_free_cur_entry;
450 if (w->hdr.part_number != 1) {
451 ERROR("Found a metadata resource in a "
452 "non-first part of the split WIM!");
453 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
454 goto out_free_cur_entry;
456 if (w->current_image == w->hdr.image_count) {
457 ERROR("The WIM header says there are %u images "
458 "in the WIM, but we found more metadata "
459 "resources than this", w->hdr.image_count);
460 ret = WIMLIB_ERR_IMAGE_COUNT;
461 goto out_free_cur_entry;
464 /* Notice very carefully: We are assigning the metadata
465 * resources in the exact order mirrored by their lookup
466 * table entries on disk, which is the behavior of
467 * Microsoft's software. In particular, this overrides
468 * the actual locations of the metadata resources
469 * themselves in the WIM file as well as any information
470 * written in the XML data. */
471 DEBUG("Found metadata resource for image %u at "
473 w->current_image + 1,
474 cur_entry->resource_entry.offset);
476 w->current_image++]->metadata_lte = cur_entry;
478 /* Lookup table entry for a stream that is not a
479 * metadata resource */
480 duplicate_entry = __lookup_resource(table, cur_entry->hash);
481 if (duplicate_entry) {
482 #ifdef ENABLE_ERROR_MESSAGES
483 ERROR("The WIM lookup table contains two entries with the "
484 "same SHA1 message digest!");
485 ERROR("The first entry is:");
486 print_lookup_table_entry(duplicate_entry, stderr);
487 ERROR("The second entry is:");
488 print_lookup_table_entry(cur_entry, stderr);
490 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
491 goto out_free_cur_entry;
493 lookup_table_insert(table, cur_entry);
497 if (w->hdr.part_number == 1 &&
498 w->current_image != w->hdr.image_count)
500 ERROR("The WIM header says there are %u images "
501 "in the WIM, but we only found %d metadata "
502 "resources!", w->hdr.image_count, w->current_image);
503 ret = WIMLIB_ERR_IMAGE_COUNT;
504 goto out_free_lookup_table;
506 DEBUG("Done reading lookup table.");
507 w->lookup_table = table;
512 out_free_lookup_table:
513 free_lookup_table(table);
515 w->current_image = 0;
521 * Writes a lookup table entry to the output file.
524 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_out)
527 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
532 /* Don't write entries that have not had file resources or metadata
533 * resources written for them. */
534 if (lte->out_refcnt == 0)
537 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
538 DEBUG("Writing metadata entry at %"PRIu64" "
539 "(orig size = %"PRIu64")",
540 ftello(out), lte->output_resource_entry.original_size);
543 p = put_resource_entry(buf, <e->output_resource_entry);
544 p = put_u16(p, lte->part_number);
545 p = put_u32(p, lte->out_refcnt);
546 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
547 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
548 ERROR_WITH_ERRNO("Failed to write lookup table entry");
549 return WIMLIB_ERR_WRITE;
554 /* Writes the WIM lookup table to the output file. */
556 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
558 FILE *out = w->out_fp;
559 off_t start_offset, end_offset;
561 int start_image, end_image;
563 start_offset = ftello(out);
564 if (start_offset == -1)
565 return WIMLIB_ERR_WRITE;
567 /* Write lookup table entries for metadata resources */
568 if (image == WIMLIB_ALL_IMAGES) {
570 end_image = w->hdr.image_count;
575 for (int i = start_image; i <= end_image; i++) {
576 struct wim_lookup_table_entry *metadata_lte;
578 metadata_lte = w->image_metadata[i - 1]->metadata_lte;
579 metadata_lte->out_refcnt = 1;
580 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
581 ret = write_lookup_table_entry(metadata_lte, out);
586 /* Write lookup table entries for other resources */
587 ret = for_lookup_table_entry(w->lookup_table, write_lookup_table_entry, out);
591 /* Fill in the resource entry for the lookup table itself */
592 end_offset = ftello(out);
593 if (end_offset == -1)
594 return WIMLIB_ERR_WRITE;
596 out_res_entry->offset = start_offset;
597 out_res_entry->size = end_offset - start_offset;
598 out_res_entry->original_size = end_offset - start_offset;
599 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
604 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
606 lte->real_refcnt = 0;
611 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
618 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
620 if (lte->extracted_file != NULL) {
621 FREE(lte->extracted_file);
622 lte->extracted_file = NULL;
628 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
634 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
635 lte->resource_entry.offset);
637 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
638 (u64)lte->resource_entry.size);
640 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
641 lte->resource_entry.original_size);
643 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
644 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
647 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
648 lte->back_inode, lte->back_stream_id);
650 tfprintf(out, T("Hash = 0x"));
651 print_hash(lte->hash, out);
655 tfprintf(out, T("Flags = "));
656 u8 flags = lte->resource_entry.flags;
657 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
658 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
659 if (flags & WIM_RESHDR_FLAG_FREE)
660 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
661 if (flags & WIM_RESHDR_FLAG_METADATA)
662 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
663 if (flags & WIM_RESHDR_FLAG_SPANNED)
664 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
666 switch (lte->resource_location) {
667 case RESOURCE_IN_WIM:
668 if (lte->wim->filename) {
669 tfprintf(out, T("WIM file = `%"TS"'\n"),
675 case RESOURCE_WIN32_ENCRYPTED:
677 case RESOURCE_IN_FILE_ON_DISK:
679 tfprintf(out, T("File on Disk = `%"TS"'\n"),
683 case RESOURCE_IN_STAGING_FILE:
684 tfprintf(out, T("Staging File = `%"TS"'\n"),
685 lte->staging_file_name);
695 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
697 print_lookup_table_entry(lte, (FILE*)fp);
702 * Prints the lookup table of a WIM file.
705 wimlib_print_lookup_table(WIMStruct *w)
707 for_lookup_table_entry(w->lookup_table,
708 do_print_lookup_table_entry,
712 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
713 * lookup table, or NULL if there is none. */
714 struct wim_lookup_table_entry *
715 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
718 struct wim_lookup_table_entry *lte;
719 struct hlist_node *pos;
721 wimlib_assert(table != NULL);
722 wimlib_assert(hash != NULL);
724 i = *(size_t*)hash % table->capacity;
725 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
726 if (hashes_equal(hash, lte->hash))
733 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
736 * This is only for pre-resolved inodes.
739 lookup_resource(WIMStruct *w,
742 struct wim_dentry **dentry_ret,
743 struct wim_lookup_table_entry **lte_ret,
746 struct wim_dentry *dentry;
747 struct wim_lookup_table_entry *lte;
749 const tchar *stream_name = NULL;
750 struct wim_inode *inode;
753 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
754 stream_name = path_stream_name(path);
756 p = (tchar*)stream_name - 1;
761 dentry = get_dentry(w, path);
767 inode = dentry->d_inode;
769 wimlib_assert(inode->i_resolved);
771 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
772 && inode_is_directory(inode))
776 struct wim_ads_entry *ads_entry;
778 ads_entry = inode_get_ads_entry(inode, stream_name,
781 stream_idx = ads_idx + 1;
782 lte = ads_entry->lte;
793 *dentry_ret = dentry;
797 *stream_idx_ret = stream_idx;
802 /* Resolve an inode's lookup table entries
804 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
805 * lookup table) with pointers directly to the lookup table entries. A circular
806 * linked list of streams sharing the same lookup table entry is created.
808 * This function always succeeds; unresolved lookup table entries are given a
812 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
815 if (!inode->i_resolved) {
816 struct wim_lookup_table_entry *lte;
817 /* Resolve the default file stream */
818 lte = __lookup_resource(table, inode->i_hash);
820 inode->i_resolved = 1;
822 /* Resolve the alternate data streams */
823 for (u16 i = 0; i < inode->i_num_ads; i++) {
824 struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
825 lte = __lookup_resource(table, cur_entry->hash);
826 cur_entry->lte = lte;
832 inode_unresolve_ltes(struct wim_inode *inode)
834 if (inode->i_resolved) {
836 copy_hash(inode->i_hash, inode->i_lte->hash);
838 zero_out_hash(inode->i_hash);
840 for (u16 i = 0; i < inode->i_num_ads; i++) {
841 if (inode->i_ads_entries[i].lte)
842 copy_hash(inode->i_ads_entries[i].hash,
843 inode->i_ads_entries[i].lte->hash);
845 zero_out_hash(inode->i_ads_entries[i].hash);
847 inode->i_resolved = 0;
852 * Returns the lookup table entry for stream @stream_idx of the inode, where
853 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
854 * corresponds to an alternate data stream.
856 * This works for both resolved and un-resolved inodes.
858 struct wim_lookup_table_entry *
859 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
860 const struct wim_lookup_table *table)
862 if (inode->i_resolved)
863 return inode_stream_lte_resolved(inode, stream_idx);
865 return inode_stream_lte_unresolved(inode, stream_idx, table);
868 struct wim_lookup_table_entry *
869 inode_unnamed_lte_resolved(const struct wim_inode *inode)
871 wimlib_assert(inode->i_resolved);
872 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
873 if (inode_stream_name_nbytes(inode, i) == 0 &&
874 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
876 return inode_stream_lte_resolved(inode, i);
882 struct wim_lookup_table_entry *
883 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
884 const struct wim_lookup_table *table)
886 wimlib_assert(!inode->i_resolved);
887 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
888 if (inode_stream_name_nbytes(inode, i) == 0 &&
889 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
891 return inode_stream_lte_unresolved(inode, i, table);
897 /* Return the lookup table entry for the unnamed data stream of an inode, or
898 * NULL if there is none.
900 * You'd think this would be easier than it actually is, since the unnamed data
901 * stream should be the one referenced from the inode itself. Alas, if there
902 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
903 * data stream in one of the alternate data streams instead of inside the WIM
904 * dentry itself. So we need to check the alternate data streams too.
906 * Also, note that a dentry may appear to have more than one unnamed stream, but
907 * if the SHA1 message digest is all 0's then the corresponding stream does not
908 * really "count" (this is the case for the inode's own file stream when the
909 * file stream that should be there is actually in one of the alternate stream
910 * entries.). This is despite the fact that we may need to extract such a
911 * missing entry as an empty file or empty named data stream.
913 struct wim_lookup_table_entry *
914 inode_unnamed_lte(const struct wim_inode *inode,
915 const struct wim_lookup_table *table)
917 if (inode->i_resolved)
918 return inode_unnamed_lte_resolved(inode);
920 return inode_unnamed_lte_unresolved(inode, table);
924 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
926 *(u64*)total_bytes_p += lte->resource_entry.size;
931 lookup_table_total_stream_size(struct wim_lookup_table *table)
934 for_lookup_table_entry(table, lte_add_stream_size, &total_size);
938 struct wim_lookup_table_entry **
939 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
941 wimlib_assert(lte->unhashed);
942 struct wim_inode *inode = lte->back_inode;
943 u32 stream_id = lte->back_stream_id;
945 return &inode->i_lte;
947 for (u16 i = 0; i < inode->i_num_ads; i++)
948 if (inode->i_ads_entries[i].stream_id == stream_id)
949 return &inode->i_ads_entries[i].lte;
954 /* Calculate the SHA1 message digest of a stream and move it from the list of
955 * unhashed streams to the stream lookup table, possibly joining it with an
956 * existing lookup table entry for an identical stream.
958 * @lte: An unhashed lookup table entry.
959 * @lookup_table: Lookup table for the WIM.
960 * @lte_ret: On success, write a pointer to the resulting lookup table
961 * entry to this location. This will be the same as @lte
962 * if it was inserted into the lookup table, or different if
963 * a duplicate stream was found.
965 * Returns 0 on success; nonzero if there is an error reading the stream.
968 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
969 struct wim_lookup_table *lookup_table,
970 struct wim_lookup_table_entry **lte_ret)
973 struct wim_lookup_table_entry *duplicate_lte;
974 struct wim_lookup_table_entry **back_ptr;
976 wimlib_assert(lte->unhashed);
978 /* back_ptr must be saved because @back_inode and @back_stream_id are in
979 * union with the SHA1 message digest and will no longer be valid once
980 * the SHA1 has been calculated. */
981 back_ptr = retrieve_lte_pointer(lte);
983 ret = sha1_resource(lte);
987 /* Look for a duplicate stream */
988 duplicate_lte = __lookup_resource(lookup_table, lte->hash);
989 list_del(<e->unhashed_list);
991 /* We have a duplicate stream. Transfer the reference counts
992 * from this stream to the duplicate, update the reference to
993 * this stream (in an inode or ads_entry) to point to the
994 * duplicate, then free this stream. */
995 wimlib_assert(!(duplicate_lte->unhashed));
996 duplicate_lte->refcnt += lte->refcnt;
997 duplicate_lte->out_refcnt += lte->refcnt;
998 *back_ptr = duplicate_lte;
999 free_lookup_table_entry(lte);
1000 lte = duplicate_lte;
1002 /* No duplicate stream, so we need to insert
1003 * this stream into the lookup table and treat
1004 * it as a hashed stream. */
1005 lookup_table_insert(lookup_table, lte);