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) {
92 case RESOURCE_IN_STAGING_FILE:
93 case RESOURCE_IN_FILE_ON_DISK:
94 BUILD_BUG_ON((void*)&old->file_on_disk !=
95 (void*)&old->staging_file_name);
96 new->staging_file_name = TSTRDUP(old->staging_file_name);
97 if (!new->staging_file_name)
100 case RESOURCE_IN_ATTACHED_BUFFER:
101 new->attached_buffer = MALLOC(wim_resource_size(old));
102 if (!new->attached_buffer)
104 memcpy(new->attached_buffer, old->attached_buffer,
105 wim_resource_size(old));
108 case RESOURCE_IN_NTFS_VOLUME:
110 struct ntfs_location *loc;
111 loc = MALLOC(sizeof(*loc));
114 memcpy(loc, old->ntfs_loc, sizeof(*loc));
116 loc->stream_name = NULL;
118 loc->path = STRDUP(old->ntfs_loc->path);
121 loc->stream_name = MALLOC((loc->stream_name_nchars + 1) * 2);
122 if (!loc->stream_name)
124 memcpy(loc->stream_name,
125 old->ntfs_loc->stream_name,
126 (loc->stream_name_nchars + 1) * 2);
135 free_lookup_table_entry(new);
140 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
143 switch (lte->resource_location) {
144 case RESOURCE_IN_STAGING_FILE:
145 case RESOURCE_IN_ATTACHED_BUFFER:
146 case RESOURCE_IN_FILE_ON_DISK:
150 BUILD_BUG_ON((void*)<e->file_on_disk !=
151 (void*)<e->staging_file_name);
152 BUILD_BUG_ON((void*)<e->file_on_disk !=
153 (void*)<e->attached_buffer);
154 FREE(lte->file_on_disk);
157 case RESOURCE_IN_NTFS_VOLUME:
159 FREE(lte->ntfs_loc->path);
160 FREE(lte->ntfs_loc->stream_name);
173 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
175 free_lookup_table_entry(entry);
181 free_lookup_table(struct wim_lookup_table *table)
183 DEBUG2("Freeing lookup table");
186 for_lookup_table_entry(table,
187 do_free_lookup_table_entry,
196 * Inserts an entry into the lookup table.
198 * @table: A pointer to the lookup table.
199 * @lte: A pointer to the entry to insert.
202 lookup_table_insert(struct wim_lookup_table *table,
203 struct wim_lookup_table_entry *lte)
205 size_t i = lte->hash_short % table->capacity;
206 hlist_add_head(<e->hash_list, &table->array[i]);
208 /* XXX Make the table grow when too many entries have been inserted. */
209 table->num_entries++;
213 finalize_lte(struct wim_lookup_table_entry *lte)
216 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
217 unlink(lte->staging_file_name);
218 list_del(<e->unhashed_list);
221 free_lookup_table_entry(lte);
224 /* Decrements the reference count for the lookup table entry @lte. If its
225 * reference count reaches 0, it is unlinked from the lookup table. If,
226 * furthermore, the entry has no opened file descriptors associated with it, the
229 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
230 struct wim_lookup_table *table)
232 wimlib_assert(lte != NULL);
233 wimlib_assert(lte->refcnt != 0);
234 if (--lte->refcnt == 0) {
236 lookup_table_unlink(table, lte);
238 if (lte->num_opened_fds == 0)
246 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
248 if (lte->num_opened_fds != 0)
249 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
254 /* Calls a function on all the entries in the WIM lookup table. Stop early and
255 * return nonzero if any call to the function returns nonzero. */
257 for_lookup_table_entry(struct wim_lookup_table *table,
258 int (*visitor)(struct wim_lookup_table_entry *, void *),
261 struct wim_lookup_table_entry *lte;
262 struct hlist_node *pos, *tmp;
265 for (size_t i = 0; i < table->capacity; i++) {
266 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
269 wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
270 ret = visitor(lte, arg);
279 cmp_streams_by_wim_position(const void *p1, const void *p2)
281 const struct wim_lookup_table_entry *lte1, *lte2;
282 lte1 = *(const struct wim_lookup_table_entry**)p1;
283 lte2 = *(const struct wim_lookup_table_entry**)p2;
284 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
286 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
294 add_lte_to_array(struct wim_lookup_table_entry *lte,
297 struct wim_lookup_table_entry ***pp = _pp;
302 /* Iterate through the lookup table entries, but first sort them by stream
303 * offset in the WIM. Caution: this is intended to be used when the stream
304 * offset field has actually been set. */
306 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
307 int (*visitor)(struct wim_lookup_table_entry *,
311 struct wim_lookup_table_entry **lte_array, **p;
312 size_t num_streams = table->num_entries;
315 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
317 return WIMLIB_ERR_NOMEM;
319 for_lookup_table_entry(table, add_lte_to_array, &p);
321 wimlib_assert(p == lte_array + num_streams);
323 qsort(lte_array, num_streams, sizeof(lte_array[0]),
324 cmp_streams_by_wim_position);
326 for (size_t i = 0; i < num_streams && ret == 0; i++)
327 ret = visitor(lte_array[i], arg);
333 * Reads the lookup table from a WIM file.
335 * Saves lookup table entries for non-metadata streams in a hash table, and
336 * saves the metadata entry for each image in a special per-image location (the
337 * image_metadata array).
340 read_lookup_table(WIMStruct *w)
343 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
345 struct wim_lookup_table *table;
346 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
348 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
349 ERROR("Didn't expect a compressed lookup table!");
350 ERROR("Ask the author to implement support for this.");
351 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
354 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
355 w->hdr.lookup_table_res_entry.offset,
356 w->hdr.lookup_table_res_entry.original_size);
358 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
360 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
362 w->hdr.lookup_table_res_entry.offset);
363 return WIMLIB_ERR_READ;
366 num_entries = w->hdr.lookup_table_res_entry.original_size /
367 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
368 table = new_lookup_table(num_entries * 2 + 1);
370 return WIMLIB_ERR_NOMEM;
372 w->current_image = 0;
373 while (num_entries--) {
376 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
378 ERROR("Unexpected EOF in WIM lookup table!");
380 ERROR_WITH_ERRNO("Error reading WIM lookup "
383 ret = WIMLIB_ERR_READ;
384 goto out_free_lookup_table;
386 cur_entry = new_lookup_table_entry();
388 ret = WIMLIB_ERR_NOMEM;
389 goto out_free_lookup_table;
393 cur_entry->resource_location = RESOURCE_IN_WIM;
394 p = get_resource_entry(buf, &cur_entry->resource_entry);
395 p = get_u16(p, &cur_entry->part_number);
396 p = get_u32(p, &cur_entry->refcnt);
397 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
399 if (cur_entry->part_number != w->hdr.part_number) {
400 ERROR("A lookup table entry in part %hu of the WIM "
401 "points to part %hu",
402 w->hdr.part_number, cur_entry->part_number);
403 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
404 goto out_free_cur_entry;
407 if (is_zero_hash(cur_entry->hash)) {
408 ERROR("The WIM lookup table contains an entry with a "
409 "SHA1 message digest of all 0's");
410 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
411 goto out_free_cur_entry;
414 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
415 && (cur_entry->resource_entry.size !=
416 cur_entry->resource_entry.original_size))
418 #ifdef ENABLE_ERROR_MESSAGES
419 ERROR("Found uncompressed resource with original size "
420 "not the same as compressed size");
421 ERROR("The lookup table entry for the resource is as follows:");
422 print_lookup_table_entry(cur_entry, stderr);
424 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
425 goto out_free_cur_entry;
428 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
429 /* Lookup table entry for a metadata resource */
430 if (cur_entry->refcnt != 1) {
431 #ifdef ENABLE_ERROR_MESSAGES
432 ERROR("Found metadata resource with refcnt != 1:");
433 print_lookup_table_entry(cur_entry, stderr);
435 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
436 goto out_free_cur_entry;
439 if (w->hdr.part_number != 1) {
440 ERROR("Found a metadata resource in a "
441 "non-first part of the split WIM!");
442 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
443 goto out_free_cur_entry;
445 if (w->current_image == w->hdr.image_count) {
446 ERROR("The WIM header says there are %u images "
447 "in the WIM, but we found more metadata "
448 "resources than this", w->hdr.image_count);
449 ret = WIMLIB_ERR_IMAGE_COUNT;
450 goto out_free_cur_entry;
453 /* Notice very carefully: We are assigning the metadata
454 * resources in the exact order mirrored by their lookup
455 * table entries on disk, which is the behavior of
456 * Microsoft's software. In particular, this overrides
457 * the actual locations of the metadata resources
458 * themselves in the WIM file as well as any information
459 * written in the XML data. */
460 DEBUG("Found metadata resource for image %u at "
462 w->current_image + 1,
463 cur_entry->resource_entry.offset);
465 w->current_image++]->metadata_lte = cur_entry;
467 /* Lookup table entry for a stream that is not a
468 * metadata resource */
469 duplicate_entry = __lookup_resource(table, cur_entry->hash);
470 if (duplicate_entry) {
471 #ifdef ENABLE_ERROR_MESSAGES
472 ERROR("The WIM lookup table contains two entries with the "
473 "same SHA1 message digest!");
474 ERROR("The first entry is:");
475 print_lookup_table_entry(duplicate_entry, stderr);
476 ERROR("The second entry is:");
477 print_lookup_table_entry(cur_entry, stderr);
479 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
480 goto out_free_cur_entry;
482 lookup_table_insert(table, cur_entry);
486 if (w->hdr.part_number == 1 &&
487 w->current_image != w->hdr.image_count)
489 ERROR("The WIM header says there are %u images "
490 "in the WIM, but we only found %d metadata "
491 "resources!", w->hdr.image_count, w->current_image);
492 ret = WIMLIB_ERR_IMAGE_COUNT;
493 goto out_free_lookup_table;
495 DEBUG("Done reading lookup table.");
496 w->lookup_table = table;
501 out_free_lookup_table:
502 free_lookup_table(table);
504 w->current_image = 0;
510 * Writes a lookup table entry to the output file.
513 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_out)
516 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
521 /* Don't write entries that have not had file resources or metadata
522 * resources written for them. */
523 if (lte->out_refcnt == 0)
526 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
527 DEBUG("Writing metadata entry at %"PRIu64" "
528 "(orig size = %"PRIu64")",
529 ftello(out), lte->output_resource_entry.original_size);
532 p = put_resource_entry(buf, <e->output_resource_entry);
533 p = put_u16(p, lte->part_number);
534 p = put_u32(p, lte->out_refcnt);
535 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
536 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
537 ERROR_WITH_ERRNO("Failed to write lookup table entry");
538 return WIMLIB_ERR_WRITE;
543 /* Writes the WIM lookup table to the output file. */
545 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
547 FILE *out = w->out_fp;
548 off_t start_offset, end_offset;
550 int start_image, end_image;
552 start_offset = ftello(out);
553 if (start_offset == -1)
554 return WIMLIB_ERR_WRITE;
556 /* Write lookup table entries for metadata resources */
557 if (image == WIMLIB_ALL_IMAGES) {
559 end_image = w->hdr.image_count;
564 for (int i = start_image; i <= end_image; i++) {
565 struct wim_lookup_table_entry *metadata_lte;
567 metadata_lte = w->image_metadata[i - 1]->metadata_lte;
568 metadata_lte->out_refcnt = 1;
569 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
570 ret = write_lookup_table_entry(metadata_lte, out);
575 /* Write lookup table entries for other resources */
576 ret = for_lookup_table_entry(w->lookup_table, write_lookup_table_entry, out);
580 /* Fill in the resource entry for the lookup table itself */
581 end_offset = ftello(out);
582 if (end_offset == -1)
583 return WIMLIB_ERR_WRITE;
585 out_res_entry->offset = start_offset;
586 out_res_entry->size = end_offset - start_offset;
587 out_res_entry->original_size = end_offset - start_offset;
588 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
594 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
596 lte->real_refcnt = 0;
601 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
608 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore)
610 if (lte->extracted_file != NULL) {
611 FREE(lte->extracted_file);
612 lte->extracted_file = NULL;
618 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
624 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
625 lte->resource_entry.offset);
627 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
628 (u64)lte->resource_entry.size);
630 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
631 lte->resource_entry.original_size);
633 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
634 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
637 tfprintf(out, T("(Unhashed, back ptr at %p)\n"), lte->back_ptr);
639 tfprintf(out, T("Hash = 0x"));
640 print_hash(lte->hash, out);
644 tfprintf(out, T("Flags = "));
645 u8 flags = lte->resource_entry.flags;
646 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
647 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
648 if (flags & WIM_RESHDR_FLAG_FREE)
649 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
650 if (flags & WIM_RESHDR_FLAG_METADATA)
651 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
652 if (flags & WIM_RESHDR_FLAG_SPANNED)
653 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
655 switch (lte->resource_location) {
656 case RESOURCE_IN_WIM:
657 if (lte->wim->filename) {
658 tfprintf(out, T("WIM file = `%"TS"'\n"),
665 case RESOURCE_IN_FILE_ON_DISK:
666 tfprintf(out, T("File on Disk = `%"TS"'\n"),
669 case RESOURCE_IN_STAGING_FILE:
670 tfprintf(out, T("Staging File = `%"TS"'\n"),
671 lte->staging_file_name);
680 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
682 print_lookup_table_entry(lte, (FILE*)fp);
687 * Prints the lookup table of a WIM file.
690 wimlib_print_lookup_table(WIMStruct *w)
692 for_lookup_table_entry(w->lookup_table,
693 do_print_lookup_table_entry,
697 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
698 * lookup table, or NULL if there is none. */
699 struct wim_lookup_table_entry *
700 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
703 struct wim_lookup_table_entry *lte;
704 struct hlist_node *pos;
706 wimlib_assert(table != NULL);
707 wimlib_assert(hash != NULL);
709 i = *(size_t*)hash % table->capacity;
710 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
711 if (hashes_equal(hash, lte->hash))
718 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
721 * This is only for pre-resolved inodes.
724 lookup_resource(WIMStruct *w,
727 struct wim_dentry **dentry_ret,
728 struct wim_lookup_table_entry **lte_ret,
731 struct wim_dentry *dentry;
732 struct wim_lookup_table_entry *lte;
734 const tchar *stream_name = NULL;
735 struct wim_inode *inode;
738 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
739 stream_name = path_stream_name(path);
741 p = (tchar*)stream_name - 1;
746 dentry = get_dentry(w, path);
752 inode = dentry->d_inode;
754 wimlib_assert(inode->i_resolved);
756 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
757 && inode_is_directory(inode))
761 struct wim_ads_entry *ads_entry;
763 ads_entry = inode_get_ads_entry(inode, stream_name,
766 stream_idx = ads_idx + 1;
767 lte = ads_entry->lte;
778 *dentry_ret = dentry;
782 *stream_idx_ret = stream_idx;
787 /* Resolve an inode's lookup table entries
789 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
790 * lookup table) with pointers directly to the lookup table entries. A circular
791 * linked list of streams sharing the same lookup table entry is created.
793 * This function always succeeds; unresolved lookup table entries are given a
797 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
800 if (!inode->i_resolved) {
801 struct wim_lookup_table_entry *lte;
802 /* Resolve the default file stream */
803 lte = __lookup_resource(table, inode->i_hash);
805 inode->i_resolved = 1;
807 /* Resolve the alternate data streams */
808 for (u16 i = 0; i < inode->i_num_ads; i++) {
809 struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
810 lte = __lookup_resource(table, cur_entry->hash);
811 cur_entry->lte = lte;
817 inode_unresolve_ltes(struct wim_inode *inode)
819 if (inode->i_resolved) {
821 copy_hash(inode->i_hash, inode->i_lte->hash);
823 zero_out_hash(inode->i_hash);
825 for (u16 i = 0; i < inode->i_num_ads; i++) {
826 if (inode->i_ads_entries[i].lte)
827 copy_hash(inode->i_ads_entries[i].hash,
828 inode->i_ads_entries[i].lte->hash);
830 zero_out_hash(inode->i_ads_entries[i].hash);
832 inode->i_resolved = 0;
837 * Returns the lookup table entry for stream @stream_idx of the inode, where
838 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
839 * corresponds to an alternate data stream.
841 * This works for both resolved and un-resolved inodes.
843 struct wim_lookup_table_entry *
844 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
845 const struct wim_lookup_table *table)
847 if (inode->i_resolved)
848 return inode_stream_lte_resolved(inode, stream_idx);
850 return inode_stream_lte_unresolved(inode, stream_idx, table);
854 /* Return the lookup table entry for the unnamed data stream of an inode, or
855 * NULL if there is none.
857 * You'd think this would be easier than it actually is, since the unnamed data
858 * stream should be the one referenced from the inode itself. Alas, if there
859 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
860 * data stream in one of the alternate data streams instead of inside the WIM
861 * dentry itself. So we need to check the alternate data streams too.
863 * Also, note that a dentry may appear to have more than one unnamed stream, but
864 * if the SHA1 message digest is all 0's then the corresponding stream does not
865 * really "count" (this is the case for the inode's own file stream when the
866 * file stream that should be there is actually in one of the alternate stream
867 * entries.). This is despite the fact that we may need to extract such a
868 * missing entry as an empty file or empty named data stream.
870 struct wim_lookup_table_entry *
871 inode_unnamed_lte(const struct wim_inode *inode,
872 const struct wim_lookup_table *table)
874 if (inode->i_resolved)
875 return inode_unnamed_lte_resolved(inode);
877 return inode_unnamed_lte_unresolved(inode, table);
881 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
883 *(u64*)total_bytes_p += lte->resource_entry.size;
888 lookup_table_total_stream_size(struct wim_lookup_table *table)
891 for_lookup_table_entry(table, lte_add_stream_size, &total_size);