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"
36 struct wim_lookup_table *
37 new_lookup_table(size_t capacity)
39 struct wim_lookup_table *table;
40 struct hlist_head *array;
42 table = MALLOC(sizeof(struct wim_lookup_table));
44 array = CALLOC(capacity, sizeof(array[0]));
46 table->num_entries = 0;
47 table->capacity = capacity;
52 ERROR("Failed to allocate memory for lookup table with capacity %zu",
59 struct wim_lookup_table_entry *
60 new_lookup_table_entry()
62 struct wim_lookup_table_entry *lte;
64 lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
69 ERROR("Out of memory (tried to allocate %zu bytes for "
70 "lookup table entry)",
71 sizeof(struct wim_lookup_table_entry));
76 struct wim_lookup_table_entry *
77 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
79 struct wim_lookup_table_entry *new;
81 new = MALLOC(sizeof(*new));
85 memcpy(new, old, sizeof(*old));
86 new->extracted_file = NULL;
87 switch (new->resource_location) {
91 case RESOURCE_IN_STAGING_FILE:
92 case RESOURCE_IN_FILE_ON_DISK:
93 BUILD_BUG_ON((void*)&old->file_on_disk !=
94 (void*)&old->staging_file_name);
95 new->staging_file_name = TSTRDUP(old->staging_file_name);
96 if (!new->staging_file_name)
99 case RESOURCE_IN_ATTACHED_BUFFER:
100 new->attached_buffer = MALLOC(wim_resource_size(old));
101 if (!new->attached_buffer)
103 memcpy(new->attached_buffer, old->attached_buffer,
104 wim_resource_size(old));
107 case RESOURCE_IN_NTFS_VOLUME:
109 struct ntfs_location *loc;
110 loc = MALLOC(sizeof(*loc));
113 memcpy(loc, old->ntfs_loc, sizeof(*loc));
115 loc->stream_name = NULL;
117 loc->path = STRDUP(old->ntfs_loc->path);
120 loc->stream_name = MALLOC((loc->stream_name_nchars + 1) * 2);
121 if (!loc->stream_name)
123 memcpy(loc->stream_name,
124 old->ntfs_loc->stream_name,
125 (loc->stream_name_nchars + 1) * 2);
134 free_lookup_table_entry(new);
139 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
142 switch (lte->resource_location) {
143 case RESOURCE_IN_STAGING_FILE:
144 case RESOURCE_IN_ATTACHED_BUFFER:
145 case RESOURCE_IN_FILE_ON_DISK:
149 BUILD_BUG_ON((void*)<e->file_on_disk !=
150 (void*)<e->staging_file_name);
151 BUILD_BUG_ON((void*)<e->file_on_disk !=
152 (void*)<e->attached_buffer);
153 FREE(lte->file_on_disk);
156 case RESOURCE_IN_NTFS_VOLUME:
158 FREE(lte->ntfs_loc->path);
159 FREE(lte->ntfs_loc->stream_name);
172 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
174 free_lookup_table_entry(entry);
180 free_lookup_table(struct wim_lookup_table *table)
182 DEBUG2("Freeing lookup table");
185 for_lookup_table_entry(table,
186 do_free_lookup_table_entry,
195 * Inserts an entry into the lookup table.
197 * @table: A pointer to the lookup table.
198 * @lte: A pointer to the entry to insert.
201 lookup_table_insert(struct wim_lookup_table *table,
202 struct wim_lookup_table_entry *lte)
204 size_t i = lte->hash_short % table->capacity;
205 hlist_add_head(<e->hash_list, &table->array[i]);
207 /* XXX Make the table grow when too many entries have been inserted. */
208 table->num_entries++;
212 finalize_lte(struct wim_lookup_table_entry *lte)
215 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
216 unlink(lte->staging_file_name);
217 list_del(<e->staging_list);
220 free_lookup_table_entry(lte);
223 /* Decrements the reference count for the lookup table entry @lte. If its
224 * reference count reaches 0, it is unlinked from the lookup table. If,
225 * furthermore, the entry has no opened file descriptors associated with it, the
228 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
229 struct wim_lookup_table *table)
231 wimlib_assert(lte != NULL);
232 wimlib_assert(lte->refcnt != 0);
233 if (--lte->refcnt == 0) {
234 lookup_table_unlink(table, lte);
236 if (lte->num_opened_fds == 0)
244 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
246 if (lte->num_opened_fds != 0)
247 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
252 /* Calls a function on all the entries in the WIM lookup table. Stop early and
253 * return nonzero if any call to the function returns nonzero. */
255 for_lookup_table_entry(struct wim_lookup_table *table,
256 int (*visitor)(struct wim_lookup_table_entry *, void *),
259 struct wim_lookup_table_entry *lte;
260 struct hlist_node *pos, *tmp;
263 for (size_t i = 0; i < table->capacity; i++) {
264 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
267 wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
268 ret = visitor(lte, arg);
278 * Reads the lookup table from a WIM file.
280 * Saves lookup table entries for non-metadata streams in a hash table, and
281 * saves the metadata entry for each image in a special per-image location (the
282 * image_metadata array).
285 read_lookup_table(WIMStruct *w)
288 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
290 struct wim_lookup_table *table;
291 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
293 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
294 ERROR("Didn't expect a compressed lookup table!");
295 ERROR("Ask the author to implement support for this.");
296 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
299 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
300 w->hdr.lookup_table_res_entry.offset,
301 w->hdr.lookup_table_res_entry.original_size);
303 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
305 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
307 w->hdr.lookup_table_res_entry.offset);
308 return WIMLIB_ERR_READ;
311 num_entries = w->hdr.lookup_table_res_entry.original_size /
312 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
313 table = new_lookup_table(num_entries * 2 + 1);
315 return WIMLIB_ERR_NOMEM;
317 w->current_image = 0;
318 while (num_entries--) {
321 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
323 ERROR("Unexpected EOF in WIM lookup table!");
325 ERROR_WITH_ERRNO("Error reading WIM lookup "
328 ret = WIMLIB_ERR_READ;
329 goto out_free_lookup_table;
331 cur_entry = new_lookup_table_entry();
333 ret = WIMLIB_ERR_NOMEM;
334 goto out_free_lookup_table;
338 cur_entry->resource_location = RESOURCE_IN_WIM;
339 p = get_resource_entry(buf, &cur_entry->resource_entry);
340 p = get_u16(p, &cur_entry->part_number);
341 p = get_u32(p, &cur_entry->refcnt);
342 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
344 if (cur_entry->part_number != w->hdr.part_number) {
345 ERROR("A lookup table entry in part %hu of the WIM "
346 "points to part %hu",
347 w->hdr.part_number, cur_entry->part_number);
348 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
349 goto out_free_cur_entry;
352 if (is_zero_hash(cur_entry->hash)) {
353 ERROR("The WIM lookup table contains an entry with a "
354 "SHA1 message digest of all 0's");
355 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
356 goto out_free_cur_entry;
359 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
360 && (cur_entry->resource_entry.size !=
361 cur_entry->resource_entry.original_size))
363 #ifdef ENABLE_ERROR_MESSAGES
364 ERROR("Found uncompressed resource with original size "
365 "not the same as compressed size");
366 ERROR("The lookup table entry for the resource is as follows:");
367 print_lookup_table_entry(cur_entry, stderr);
369 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
370 goto out_free_cur_entry;
373 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
374 /* Lookup table entry for a metadata resource */
375 if (cur_entry->refcnt != 1) {
376 #ifdef ENABLE_ERROR_MESSAGES
377 ERROR("Found metadata resource with refcnt != 1:");
378 print_lookup_table_entry(cur_entry, stderr);
380 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
381 goto out_free_cur_entry;
384 if (w->hdr.part_number != 1) {
385 ERROR("Found a metadata resource in a "
386 "non-first part of the split WIM!");
387 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
388 goto out_free_cur_entry;
390 if (w->current_image == w->hdr.image_count) {
391 ERROR("The WIM header says there are %u images "
392 "in the WIM, but we found more metadata "
393 "resources than this", w->hdr.image_count);
394 ret = WIMLIB_ERR_IMAGE_COUNT;
395 goto out_free_cur_entry;
398 /* Notice very carefully: We are assigning the metadata
399 * resources in the exact order mirrored by their lookup
400 * table entries on disk, which is the behavior of
401 * Microsoft's software. In particular, this overrides
402 * the actual locations of the metadata resources
403 * themselves in the WIM file as well as any information
404 * written in the XML data. */
405 DEBUG("Found metadata resource for image %u at "
407 w->current_image + 1,
408 cur_entry->resource_entry.offset);
410 w->current_image++].metadata_lte = cur_entry;
412 /* Lookup table entry for a stream that is not a
413 * metadata resource */
414 duplicate_entry = __lookup_resource(table, cur_entry->hash);
415 if (duplicate_entry) {
416 #ifdef ENABLE_ERROR_MESSAGES
417 ERROR("The WIM lookup table contains two entries with the "
418 "same SHA1 message digest!");
419 ERROR("The first entry is:");
420 print_lookup_table_entry(duplicate_entry, stderr);
421 ERROR("The second entry is:");
422 print_lookup_table_entry(cur_entry, stderr);
424 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
425 goto out_free_cur_entry;
427 lookup_table_insert(table, cur_entry);
431 if (w->hdr.part_number == 1 &&
432 w->current_image != w->hdr.image_count)
434 ERROR("The WIM header says there are %u images "
435 "in the WIM, but we only found %d metadata "
436 "resources!", w->hdr.image_count, w->current_image);
437 ret = WIMLIB_ERR_IMAGE_COUNT;
438 goto out_free_lookup_table;
440 DEBUG("Done reading lookup table.");
441 w->lookup_table = table;
446 out_free_lookup_table:
447 free_lookup_table(table);
449 w->current_image = 0;
455 * Writes a lookup table entry to the output file.
458 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_out)
461 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
466 /* Don't write entries that have not had file resources or metadata
467 * resources written for them. */
468 if (lte->out_refcnt == 0)
471 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
472 DEBUG("Writing metadata entry at %"PRIu64" "
473 "(orig size = %"PRIu64")",
474 ftello(out), lte->output_resource_entry.original_size);
477 p = put_resource_entry(buf, <e->output_resource_entry);
478 p = put_u16(p, lte->part_number);
479 p = put_u32(p, lte->out_refcnt);
480 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
481 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
482 ERROR_WITH_ERRNO("Failed to write lookup table entry");
483 return WIMLIB_ERR_WRITE;
488 /* Writes the WIM lookup table to the output file. */
490 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
492 FILE *out = w->out_fp;
493 off_t start_offset, end_offset;
495 int start_image, end_image;
497 start_offset = ftello(out);
498 if (start_offset == -1)
499 return WIMLIB_ERR_WRITE;
501 /* Write lookup table entries for metadata resources */
502 if (image == WIMLIB_ALL_IMAGES) {
504 end_image = w->hdr.image_count;
509 for (int i = start_image; i <= end_image; i++) {
510 struct wim_lookup_table_entry *metadata_lte;
512 metadata_lte = w->image_metadata[i - 1].metadata_lte;
513 metadata_lte->out_refcnt = 1;
514 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
515 ret = write_lookup_table_entry(metadata_lte, out);
520 /* Write lookup table entries for other resources */
521 ret = for_lookup_table_entry(w->lookup_table, write_lookup_table_entry, out);
525 /* Fill in the resource entry for the lookup table itself */
526 end_offset = ftello(out);
527 if (end_offset == -1)
528 return WIMLIB_ERR_WRITE;
530 out_res_entry->offset = start_offset;
531 out_res_entry->size = end_offset - start_offset;
532 out_res_entry->original_size = end_offset - start_offset;
533 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
539 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
541 lte->real_refcnt = 0;
546 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
553 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore)
555 if (lte->extracted_file != NULL) {
556 FREE(lte->extracted_file);
557 lte->extracted_file = NULL;
563 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
569 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
570 lte->resource_entry.offset);
572 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
573 (u64)lte->resource_entry.size);
575 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
576 lte->resource_entry.original_size);
578 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
579 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
581 tfprintf(out, T("Hash = 0x"));
582 print_hash(lte->hash, out);
585 tfprintf(out, T("Flags = "));
586 u8 flags = lte->resource_entry.flags;
587 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
588 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
589 if (flags & WIM_RESHDR_FLAG_FREE)
590 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
591 if (flags & WIM_RESHDR_FLAG_METADATA)
592 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
593 if (flags & WIM_RESHDR_FLAG_SPANNED)
594 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
596 switch (lte->resource_location) {
597 case RESOURCE_IN_WIM:
598 if (lte->wim->filename) {
599 tfprintf(out, T("WIM file = `%"TS"'\n"),
606 case RESOURCE_IN_FILE_ON_DISK:
607 tfprintf(out, T("File on Disk = `%"TS"'\n"),
610 case RESOURCE_IN_STAGING_FILE:
611 tfprintf(out, T("Staging File = `%"TS"'\n"),
612 lte->staging_file_name);
621 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
623 print_lookup_table_entry(lte, (FILE*)fp);
628 * Prints the lookup table of a WIM file.
631 wimlib_print_lookup_table(WIMStruct *w)
633 for_lookup_table_entry(w->lookup_table,
634 do_print_lookup_table_entry,
638 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
639 * lookup table, or NULL if there is none. */
640 struct wim_lookup_table_entry *
641 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
644 struct wim_lookup_table_entry *lte;
645 struct hlist_node *pos;
647 wimlib_assert(table != NULL);
648 wimlib_assert(hash != NULL);
650 i = *(size_t*)hash % table->capacity;
651 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
652 if (hashes_equal(hash, lte->hash))
659 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
662 * This is only for pre-resolved inodes.
665 lookup_resource(WIMStruct *w,
668 struct wim_dentry **dentry_ret,
669 struct wim_lookup_table_entry **lte_ret,
672 struct wim_dentry *dentry;
673 struct wim_lookup_table_entry *lte;
675 const tchar *stream_name = NULL;
676 struct wim_inode *inode;
679 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
680 stream_name = path_stream_name(path);
682 p = (tchar*)stream_name - 1;
687 dentry = get_dentry(w, path);
693 inode = dentry->d_inode;
695 wimlib_assert(inode->i_resolved);
697 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
698 && inode_is_directory(inode))
702 struct wim_ads_entry *ads_entry;
704 ads_entry = inode_get_ads_entry(inode, stream_name,
707 stream_idx = ads_idx + 1;
708 lte = ads_entry->lte;
719 *dentry_ret = dentry;
723 *stream_idx_ret = stream_idx;
728 /* Resolve an inode's lookup table entries
730 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
731 * lookup table) with pointers directly to the lookup table entries. A circular
732 * linked list of streams sharing the same lookup table entry is created.
734 * This function always succeeds; unresolved lookup table entries are given a
738 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
741 if (!inode->i_resolved) {
742 struct wim_lookup_table_entry *lte;
743 /* Resolve the default file stream */
744 lte = __lookup_resource(table, inode->i_hash);
746 inode->i_resolved = 1;
748 /* Resolve the alternate data streams */
749 for (u16 i = 0; i < inode->i_num_ads; i++) {
750 struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
751 lte = __lookup_resource(table, cur_entry->hash);
752 cur_entry->lte = lte;
758 inode_unresolve_ltes(struct wim_inode *inode)
760 if (inode->i_resolved) {
762 copy_hash(inode->i_hash, inode->i_lte->hash);
764 zero_out_hash(inode->i_hash);
766 for (u16 i = 0; i < inode->i_num_ads; i++) {
767 if (inode->i_ads_entries[i].lte)
768 copy_hash(inode->i_ads_entries[i].hash,
769 inode->i_ads_entries[i].lte->hash);
771 zero_out_hash(inode->i_ads_entries[i].hash);
773 inode->i_resolved = 0;
778 * Returns the lookup table entry for stream @stream_idx of the inode, where
779 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
780 * corresponds to an alternate data stream.
782 * This works for both resolved and un-resolved inodes.
784 struct wim_lookup_table_entry *
785 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
786 const struct wim_lookup_table *table)
788 if (inode->i_resolved)
789 return inode_stream_lte_resolved(inode, stream_idx);
791 return inode_stream_lte_unresolved(inode, stream_idx, table);
795 /* Return the lookup table entry for the unnamed data stream of an inode, or
796 * NULL if there is none.
798 * You'd think this would be easier than it actually is, since the unnamed data
799 * stream should be the one referenced from the inode itself. Alas, if there
800 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
801 * data stream in one of the alternate data streams instead of inside the WIM
802 * dentry itself. So we need to check the alternate data streams too.
804 * Also, note that a dentry may appear to have more than one unnamed stream, but
805 * if the SHA1 message digest is all 0's then the corresponding stream does not
806 * really "count" (this is the case for the inode's own file stream when the
807 * file stream that should be there is actually in one of the alternate stream
808 * entries.). This is despite the fact that we may need to extract such a
809 * missing entry as an empty file or empty named data stream.
811 struct wim_lookup_table_entry *
812 inode_unnamed_lte(const struct wim_inode *inode,
813 const struct wim_lookup_table *table)
815 if (inode->i_resolved)
816 return inode_unnamed_lte_resolved(inode);
818 return inode_unnamed_lte_unresolved(inode, table);
822 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
824 *(u64*)total_bytes_p += lte->resource_entry.size;
829 lookup_table_total_stream_size(struct wim_lookup_table *table)
832 for_lookup_table_entry(table, lte_add_stream_size, &total_size);