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 *new_lookup_table(size_t capacity)
38 struct wim_lookup_table *table;
39 struct hlist_head *array;
41 table = MALLOC(sizeof(struct wim_lookup_table));
43 array = CALLOC(capacity, sizeof(array[0]));
45 table->num_entries = 0;
46 table->capacity = capacity;
51 ERROR("Failed to allocate memory for lookup table with capacity %zu",
58 struct wim_lookup_table_entry *
59 new_lookup_table_entry()
61 struct wim_lookup_table_entry *lte;
63 lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
68 ERROR("Out of memory (tried to allocate %zu bytes for "
69 "lookup table entry)",
70 sizeof(struct wim_lookup_table_entry));
75 struct wim_lookup_table_entry *
76 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
78 struct wim_lookup_table_entry *new;
80 new = MALLOC(sizeof(*new));
84 memcpy(new, old, sizeof(*old));
85 new->extracted_file = NULL;
86 switch (new->resource_location) {
88 case RESOURCE_IN_STAGING_FILE:
89 case RESOURCE_IN_FILE_ON_DISK:
90 BUILD_BUG_ON((void*)&old->file_on_disk !=
91 (void*)&old->staging_file_name);
92 new->staging_file_name = TSTRDUP(old->staging_file_name);
93 if (!new->staging_file_name)
96 case RESOURCE_IN_ATTACHED_BUFFER:
97 new->attached_buffer = MALLOC(wim_resource_size(old));
98 if (!new->attached_buffer)
100 memcpy(new->attached_buffer, old->attached_buffer,
101 wim_resource_size(old));
104 case RESOURCE_IN_NTFS_VOLUME:
106 struct ntfs_location *loc;
107 loc = MALLOC(sizeof(*loc));
110 memcpy(loc, old->ntfs_loc, sizeof(*loc));
112 loc->stream_name = NULL;
114 loc->path = STRDUP(old->ntfs_loc->path);
117 loc->stream_name = MALLOC((loc->stream_name_nchars + 1) * 2);
118 if (!loc->stream_name)
120 memcpy(loc->stream_name,
121 old->ntfs_loc->stream_name,
122 (loc->stream_name_nchars + 1) * 2);
131 free_lookup_table_entry(new);
135 void free_lookup_table_entry(struct wim_lookup_table_entry *lte)
138 switch (lte->resource_location) {
139 case RESOURCE_IN_STAGING_FILE:
140 case RESOURCE_IN_ATTACHED_BUFFER:
141 case RESOURCE_IN_FILE_ON_DISK:
145 BUILD_BUG_ON((void*)<e->file_on_disk !=
146 (void*)<e->staging_file_name);
147 BUILD_BUG_ON((void*)<e->file_on_disk !=
148 (void*)<e->attached_buffer);
149 FREE(lte->file_on_disk);
152 case RESOURCE_IN_NTFS_VOLUME:
154 FREE(lte->ntfs_loc->path);
155 FREE(lte->ntfs_loc->stream_name);
167 static int do_free_lookup_table_entry(struct wim_lookup_table_entry *entry,
170 free_lookup_table_entry(entry);
175 void free_lookup_table(struct wim_lookup_table *table)
177 DEBUG2("Freeing lookup table");
180 for_lookup_table_entry(table,
181 do_free_lookup_table_entry,
190 * Inserts an entry into the lookup table.
192 * @table: A pointer to the lookup table.
193 * @lte: A pointer to the entry to insert.
195 void lookup_table_insert(struct wim_lookup_table *table,
196 struct wim_lookup_table_entry *lte)
198 size_t i = lte->hash_short % table->capacity;
199 hlist_add_head(<e->hash_list, &table->array[i]);
201 /* XXX Make the table grow when too many entries have been inserted. */
202 table->num_entries++;
205 static void finalize_lte(struct wim_lookup_table_entry *lte)
208 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
209 unlink(lte->staging_file_name);
210 list_del(<e->staging_list);
213 free_lookup_table_entry(lte);
216 /* Decrements the reference count for the lookup table entry @lte. If its
217 * reference count reaches 0, it is unlinked from the lookup table. If,
218 * furthermore, the entry has no opened file descriptors associated with it, the
220 void lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
221 struct wim_lookup_table *table)
223 wimlib_assert(lte != NULL);
224 wimlib_assert(lte->refcnt != 0);
225 if (--lte->refcnt == 0) {
226 lookup_table_unlink(table, lte);
228 if (lte->num_opened_fds == 0)
235 void lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
237 if (lte->num_opened_fds != 0)
238 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
243 /* Calls a function on all the entries in the WIM lookup table. Stop early and
244 * return nonzero if any call to the function returns nonzero. */
245 int for_lookup_table_entry(struct wim_lookup_table *table,
246 int (*visitor)(struct wim_lookup_table_entry *, void *),
249 struct wim_lookup_table_entry *lte;
250 struct hlist_node *pos, *tmp;
253 for (size_t i = 0; i < table->capacity; i++) {
254 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
257 ret = visitor(lte, arg);
267 * Reads the lookup table from a WIM file.
269 int read_lookup_table(WIMStruct *w)
272 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
274 struct wim_lookup_table *table;
275 struct wim_lookup_table_entry *cur_entry = NULL, *duplicate_entry;
277 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
278 ERROR("Didn't expect a compressed lookup table!");
279 ERROR("Ask the author to implement support for this.");
280 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
283 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
284 w->hdr.lookup_table_res_entry.offset,
285 w->hdr.lookup_table_res_entry.original_size);
287 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
289 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
291 w->hdr.lookup_table_res_entry.offset);
292 return WIMLIB_ERR_READ;
295 num_entries = w->hdr.lookup_table_res_entry.original_size /
296 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
297 table = new_lookup_table(num_entries * 2 + 1);
299 return WIMLIB_ERR_NOMEM;
301 while (num_entries--) {
304 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
306 ERROR("Unexpected EOF in WIM lookup table!");
308 ERROR_WITH_ERRNO("Error reading WIM lookup "
311 ret = WIMLIB_ERR_READ;
314 cur_entry = new_lookup_table_entry();
316 ret = WIMLIB_ERR_NOMEM;
320 cur_entry->resource_location = RESOURCE_IN_WIM;
322 p = get_resource_entry(buf, &cur_entry->resource_entry);
323 p = get_u16(p, &cur_entry->part_number);
324 p = get_u32(p, &cur_entry->refcnt);
325 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
327 if (cur_entry->part_number != w->hdr.part_number) {
328 ERROR("A lookup table entry in part %hu of the WIM "
329 "points to part %hu",
330 w->hdr.part_number, cur_entry->part_number);
331 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
332 goto out_free_cur_entry;
335 if (is_zero_hash(cur_entry->hash)) {
336 ERROR("The WIM lookup table contains an entry with a "
337 "SHA1 message digest of all 0's");
338 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
339 goto out_free_cur_entry;
342 /* Ordinarily, no two streams should share the same SHA1 message
343 * digest. However, this constraint can be broken for metadata
344 * resources--- two identical images will have the same metadata
345 * resource, but their lookup table entries are not shared. */
346 duplicate_entry = __lookup_resource(table, cur_entry->hash);
348 && !((duplicate_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
349 && cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA))
351 #ifdef ENABLE_ERROR_MESSAGES
352 ERROR("The WIM lookup table contains two entries with the "
353 "same SHA1 message digest!");
354 ERROR("The first entry is:");
355 print_lookup_table_entry(duplicate_entry, stderr);
356 ERROR("The second entry is:");
357 print_lookup_table_entry(cur_entry, stderr);
359 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
360 goto out_free_cur_entry;
363 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
364 && (cur_entry->resource_entry.size !=
365 cur_entry->resource_entry.original_size))
367 #ifdef ENABLE_ERROR_MESSAGES
368 ERROR("Found uncompressed resource with original size "
369 "not the same as compressed size");
370 ERROR("The lookup table entry for the resource is as follows:");
371 print_lookup_table_entry(cur_entry, stderr);
373 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
374 goto out_free_cur_entry;
376 if ((cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
377 && cur_entry->refcnt != 1)
379 #ifdef ENABLE_ERROR_MESSAGES
380 ERROR("Found metadata resource with refcnt != 1:");
381 print_lookup_table_entry(cur_entry, stderr);
383 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
384 goto out_free_cur_entry;
386 lookup_table_insert(table, cur_entry);
389 DEBUG("Done reading lookup table.");
390 w->lookup_table = table;
395 free_lookup_table(table);
401 * Writes a lookup table entry to the output file.
403 int write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out)
406 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
411 /* Don't write entries that have not had file resources or metadata
412 * resources written for them. */
413 if (lte->out_refcnt == 0)
416 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
417 DEBUG("Writing metadata entry at %"PRIu64" "
418 "(orig size = %"PRIu64")",
419 ftello(out), lte->output_resource_entry.original_size);
422 p = put_resource_entry(buf, <e->output_resource_entry);
423 p = put_u16(p, lte->part_number);
424 p = put_u32(p, lte->out_refcnt);
425 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
426 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
427 ERROR_WITH_ERRNO("Failed to write lookup table entry");
428 return WIMLIB_ERR_WRITE;
433 /* Writes the lookup table to the output file. */
434 int write_lookup_table(struct wim_lookup_table *table, FILE *out,
435 struct resource_entry *out_res_entry)
437 off_t start_offset, end_offset;
440 start_offset = ftello(out);
441 if (start_offset == -1)
442 return WIMLIB_ERR_WRITE;
444 ret = for_lookup_table_entry(table, write_lookup_table_entry, out);
448 end_offset = ftello(out);
449 if (end_offset == -1)
450 return WIMLIB_ERR_WRITE;
452 out_res_entry->offset = start_offset;
453 out_res_entry->size = end_offset - start_offset;
454 out_res_entry->original_size = end_offset - start_offset;
455 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
461 int lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
463 lte->real_refcnt = 0;
467 int lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
473 int lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore)
475 if (lte->extracted_file != NULL) {
476 FREE(lte->extracted_file);
477 lte->extracted_file = NULL;
482 void print_lookup_table_entry(const struct wim_lookup_table_entry *lte,
489 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
490 lte->resource_entry.offset);
492 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
493 (u64)lte->resource_entry.size);
495 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
496 lte->resource_entry.original_size);
498 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
499 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
501 tfprintf(out, T("Hash = 0x"));
502 print_hash(lte->hash);
505 tfprintf(out, T("Flags = "));
506 u8 flags = lte->resource_entry.flags;
507 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
508 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
509 if (flags & WIM_RESHDR_FLAG_FREE)
510 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
511 if (flags & WIM_RESHDR_FLAG_METADATA)
512 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
513 if (flags & WIM_RESHDR_FLAG_SPANNED)
514 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
516 switch (lte->resource_location) {
517 case RESOURCE_IN_WIM:
518 if (lte->wim->filename) {
519 tfprintf(out, T("WIM file = `%"TS"'\n"),
523 case RESOURCE_IN_FILE_ON_DISK:
524 tfprintf(out, T("File on Disk = `%"TS"'\n"),
527 case RESOURCE_IN_STAGING_FILE:
528 tfprintf(out, T("Staging File = `%"TS"'\n"),
529 lte->staging_file_name);
537 static int do_print_lookup_table_entry(struct wim_lookup_table_entry *lte,
540 print_lookup_table_entry(lte, (FILE*)fp);
545 * Prints the lookup table of a WIM file.
547 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
549 for_lookup_table_entry(w->lookup_table,
550 do_print_lookup_table_entry,
554 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
555 * lookup table, or NULL if there is none. */
556 struct wim_lookup_table_entry *
557 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
560 struct wim_lookup_table_entry *lte;
561 struct hlist_node *pos;
563 wimlib_assert(table != NULL);
564 wimlib_assert(hash != NULL);
566 i = *(size_t*)hash % table->capacity;
567 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
568 if (hashes_equal(hash, lte->hash))
575 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
578 * This is only for pre-resolved inodes.
581 lookup_resource(WIMStruct *w,
584 struct wim_dentry **dentry_ret,
585 struct wim_lookup_table_entry **lte_ret,
588 struct wim_dentry *dentry;
589 struct wim_lookup_table_entry *lte;
591 const tchar *stream_name = NULL;
592 struct wim_inode *inode;
595 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
596 stream_name = path_stream_name(path);
598 p = (tchar*)stream_name - 1;
603 dentry = get_dentry(w, path);
609 inode = dentry->d_inode;
611 wimlib_assert(inode->i_resolved);
613 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
614 && inode_is_directory(inode))
618 struct wim_ads_entry *ads_entry;
620 ads_entry = inode_get_ads_entry(inode, stream_name,
623 stream_idx = ads_idx + 1;
624 lte = ads_entry->lte;
635 *dentry_ret = dentry;
639 *stream_idx_ret = stream_idx;
644 /* Resolve an inode's lookup table entries
646 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
647 * lookup table) with pointers directly to the lookup table entries. A circular
648 * linked list of streams sharing the same lookup table entry is created.
650 * This function always succeeds; unresolved lookup table entries are given a
653 void inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
656 if (!inode->i_resolved) {
657 struct wim_lookup_table_entry *lte;
658 /* Resolve the default file stream */
659 lte = __lookup_resource(table, inode->i_hash);
661 inode->i_resolved = 1;
663 /* Resolve the alternate data streams */
664 for (u16 i = 0; i < inode->i_num_ads; i++) {
665 struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
666 lte = __lookup_resource(table, cur_entry->hash);
667 cur_entry->lte = lte;
672 void inode_unresolve_ltes(struct wim_inode *inode)
674 if (inode->i_resolved) {
676 copy_hash(inode->i_hash, inode->i_lte->hash);
678 zero_out_hash(inode->i_hash);
680 for (u16 i = 0; i < inode->i_num_ads; i++) {
681 if (inode->i_ads_entries[i].lte)
682 copy_hash(inode->i_ads_entries[i].hash,
683 inode->i_ads_entries[i].lte->hash);
685 zero_out_hash(inode->i_ads_entries[i].hash);
687 inode->i_resolved = 0;
692 * Returns the lookup table entry for stream @stream_idx of the inode, where
693 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
694 * corresponds to an alternate data stream.
696 * This works for both resolved and un-resolved dentries.
698 struct wim_lookup_table_entry *
699 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
700 const struct wim_lookup_table *table)
702 if (inode->i_resolved)
703 return inode_stream_lte_resolved(inode, stream_idx);
705 return inode_stream_lte_unresolved(inode, stream_idx, table);
709 /* Return the lookup table entry for the unnamed data stream of an inode, or
710 * NULL if there is none.
712 * You'd think this would be easier than it actually is, since the unnamed data
713 * stream should be the one referenced from the inode itself. Alas, if there
714 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
715 * data stream in one of the alternate data streams instead of inside the WIM
716 * dentry itself. So we need to check the alternate data streams too.
718 * Also, note that a dentry may appear to have more than one unnamed stream, but
719 * if the SHA1 message digest is all 0's then the corresponding stream does not
720 * really "count" (this is the case for the inode's own file stream when the
721 * file stream that should be there is actually in one of the alternate stream
722 * entries.). This is despite the fact that we may need to extract such a
723 * missing entry as an empty file or empty named data stream.
725 struct wim_lookup_table_entry *
726 inode_unnamed_lte(const struct wim_inode *inode,
727 const struct wim_lookup_table *table)
729 if (inode->i_resolved)
730 return inode_unnamed_lte_resolved(inode);
732 return inode_unnamed_lte_unresolved(inode, table);
735 static int lte_add_stream_size(struct wim_lookup_table_entry *lte,
738 *(u64*)total_bytes_p += lte->resource_entry.size;
742 u64 lookup_table_total_stream_size(struct wim_lookup_table *table)
745 for_lookup_table_entry(table, lte_add_stream_size, &total_size);