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) {
87 case RESOURCE_IN_STAGING_FILE:
88 case RESOURCE_IN_FILE_ON_DISK:
89 #if defined(__CYGWIN__) || defined(__WIN32__)
92 BUILD_BUG_ON((void*)&old->file_on_disk !=
93 (void*)&old->staging_file_name);
94 new->staging_file_name = STRDUP(old->staging_file_name);
95 if (!new->staging_file_name)
98 case RESOURCE_IN_ATTACHED_BUFFER:
99 new->attached_buffer = MALLOC(wim_resource_size(old));
100 if (!new->attached_buffer)
102 memcpy(new->attached_buffer, old->attached_buffer,
103 wim_resource_size(old));
106 case RESOURCE_IN_NTFS_VOLUME:
108 struct ntfs_location *loc;
109 loc = MALLOC(sizeof(*loc));
112 memcpy(loc, old->ntfs_loc, sizeof(*loc));
113 loc->path_utf8 = NULL;
114 loc->stream_name_utf16 = NULL;
116 loc->path_utf8 = STRDUP(old->ntfs_loc->path_utf8);
119 loc->stream_name_utf16 = MALLOC(loc->stream_name_utf16_num_chars * 2);
120 if (!loc->stream_name_utf16)
122 memcpy(loc->stream_name_utf16,
123 old->ntfs_loc->stream_name_utf16,
124 loc->stream_name_utf16_num_chars * 2);
133 free_lookup_table_entry(new);
137 void free_lookup_table_entry(struct wim_lookup_table_entry *lte)
140 switch (lte->resource_location) {
141 case RESOURCE_IN_STAGING_FILE:
142 case RESOURCE_IN_ATTACHED_BUFFER:
143 case RESOURCE_IN_FILE_ON_DISK:
144 #if defined(__CYGWIN__) || defined(__WIN32__)
147 BUILD_BUG_ON((void*)<e->file_on_disk !=
148 (void*)<e->staging_file_name);
149 BUILD_BUG_ON((void*)<e->file_on_disk !=
150 (void*)<e->attached_buffer);
151 FREE(lte->file_on_disk);
154 case RESOURCE_IN_NTFS_VOLUME:
156 FREE(lte->ntfs_loc->path_utf8);
157 FREE(lte->ntfs_loc->stream_name_utf16);
169 static int do_free_lookup_table_entry(struct wim_lookup_table_entry *entry,
172 free_lookup_table_entry(entry);
177 void free_lookup_table(struct wim_lookup_table *table)
179 DEBUG2("Freeing lookup table");
182 for_lookup_table_entry(table,
183 do_free_lookup_table_entry,
192 * Inserts an entry into the lookup table.
194 * @table: A pointer to the lookup table.
195 * @lte: A pointer to the entry to insert.
197 void lookup_table_insert(struct wim_lookup_table *table,
198 struct wim_lookup_table_entry *lte)
200 size_t i = lte->hash_short % table->capacity;
201 hlist_add_head(<e->hash_list, &table->array[i]);
203 /* XXX Make the table grow when too many entries have been inserted. */
204 table->num_entries++;
207 static void finalize_lte(struct wim_lookup_table_entry *lte)
210 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
211 unlink(lte->staging_file_name);
212 list_del(<e->staging_list);
215 free_lookup_table_entry(lte);
218 /* Decrements the reference count for the lookup table entry @lte. If its
219 * reference count reaches 0, it is unlinked from the lookup table. If,
220 * furthermore, the entry has no opened file descriptors associated with it, the
222 void lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
223 struct wim_lookup_table *table)
225 wimlib_assert(lte != NULL);
226 wimlib_assert(lte->refcnt != 0);
227 if (--lte->refcnt == 0) {
228 lookup_table_unlink(table, lte);
230 if (lte->num_opened_fds == 0)
237 void lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
239 if (lte->num_opened_fds != 0)
240 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
245 /* Calls a function on all the entries in the WIM lookup table. Stop early and
246 * return nonzero if any call to the function returns nonzero. */
247 int for_lookup_table_entry(struct wim_lookup_table *table,
248 int (*visitor)(struct wim_lookup_table_entry *, void *),
251 struct wim_lookup_table_entry *lte;
252 struct hlist_node *pos, *tmp;
255 for (size_t i = 0; i < table->capacity; i++) {
256 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
259 ret = visitor(lte, arg);
269 * Reads the lookup table from a WIM file.
271 int read_lookup_table(WIMStruct *w)
274 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
276 struct wim_lookup_table *table;
277 struct wim_lookup_table_entry *cur_entry = NULL, *duplicate_entry;
279 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
280 ERROR("Didn't expect a compressed lookup table!");
281 ERROR("Ask the author to implement support for this.");
282 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
285 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
286 w->hdr.lookup_table_res_entry.offset,
287 w->hdr.lookup_table_res_entry.original_size);
289 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
291 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
293 w->hdr.lookup_table_res_entry.offset);
294 return WIMLIB_ERR_READ;
297 num_entries = w->hdr.lookup_table_res_entry.original_size /
298 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
299 table = new_lookup_table(num_entries * 2 + 1);
301 return WIMLIB_ERR_NOMEM;
303 while (num_entries--) {
306 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
308 ERROR("Unexpected EOF in WIM lookup table!");
310 ERROR_WITH_ERRNO("Error reading WIM lookup "
313 ret = WIMLIB_ERR_READ;
316 cur_entry = new_lookup_table_entry();
318 ret = WIMLIB_ERR_NOMEM;
322 cur_entry->resource_location = RESOURCE_IN_WIM;
324 p = get_resource_entry(buf, &cur_entry->resource_entry);
325 p = get_u16(p, &cur_entry->part_number);
326 p = get_u32(p, &cur_entry->refcnt);
327 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
329 if (cur_entry->part_number != w->hdr.part_number) {
330 ERROR("A lookup table entry in part %hu of the WIM "
331 "points to part %hu",
332 w->hdr.part_number, cur_entry->part_number);
333 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
334 goto out_free_cur_entry;
337 if (is_zero_hash(cur_entry->hash)) {
338 ERROR("The WIM lookup table contains an entry with a "
339 "SHA1 message digest of all 0's");
340 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
341 goto out_free_cur_entry;
344 /* Ordinarily, no two streams should share the same SHA1 message
345 * digest. However, this constraint can be broken for metadata
346 * resources--- two identical images will have the same metadata
347 * resource, but their lookup table entries are not shared. */
348 duplicate_entry = __lookup_resource(table, cur_entry->hash);
350 && !((duplicate_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
351 && cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA))
353 #ifdef ENABLE_ERROR_MESSAGES
354 ERROR("The WIM lookup table contains two entries with the "
355 "same SHA1 message digest!");
356 ERROR("The first entry is:");
357 print_lookup_table_entry(duplicate_entry, stderr);
358 ERROR("The second entry is:");
359 print_lookup_table_entry(cur_entry, stderr);
361 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
362 goto out_free_cur_entry;
365 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
366 && (cur_entry->resource_entry.size !=
367 cur_entry->resource_entry.original_size))
369 #ifdef ENABLE_ERROR_MESSAGES
370 ERROR("Found uncompressed resource with original size "
371 "not the same as compressed size");
372 ERROR("The lookup table entry for the resource is as follows:");
373 print_lookup_table_entry(cur_entry, stderr);
375 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
376 goto out_free_cur_entry;
378 if ((cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
379 && cur_entry->refcnt != 1)
381 #ifdef ENABLE_ERROR_MESSAGES
382 ERROR("Found metadata resource with refcnt != 1:");
383 print_lookup_table_entry(cur_entry, stderr);
385 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
386 goto out_free_cur_entry;
388 lookup_table_insert(table, cur_entry);
391 DEBUG("Done reading lookup table.");
392 w->lookup_table = table;
397 free_lookup_table(table);
403 * Writes a lookup table entry to the output file.
405 int write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out)
408 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
413 /* Don't write entries that have not had file resources or metadata
414 * resources written for them. */
415 if (lte->out_refcnt == 0)
418 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
419 DEBUG("Writing metadata entry at %"PRIu64" "
420 "(orig size = %"PRIu64")",
421 ftello(out), lte->output_resource_entry.original_size);
424 p = put_resource_entry(buf, <e->output_resource_entry);
425 p = put_u16(p, lte->part_number);
426 p = put_u32(p, lte->out_refcnt);
427 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
428 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
429 ERROR_WITH_ERRNO("Failed to write lookup table entry");
430 return WIMLIB_ERR_WRITE;
435 /* Writes the lookup table to the output file. */
436 int write_lookup_table(struct wim_lookup_table *table, FILE *out,
437 struct resource_entry *out_res_entry)
439 off_t start_offset, end_offset;
442 start_offset = ftello(out);
443 if (start_offset == -1)
444 return WIMLIB_ERR_WRITE;
446 ret = for_lookup_table_entry(table, write_lookup_table_entry, out);
450 end_offset = ftello(out);
451 if (end_offset == -1)
452 return WIMLIB_ERR_WRITE;
454 out_res_entry->offset = start_offset;
455 out_res_entry->size = end_offset - start_offset;
456 out_res_entry->original_size = end_offset - start_offset;
457 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
463 int lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
465 lte->real_refcnt = 0;
469 int lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore)
475 int lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore)
477 if (lte->extracted_file != NULL) {
478 FREE(lte->extracted_file);
479 lte->extracted_file = NULL;
484 void print_lookup_table_entry(const struct wim_lookup_table_entry *lte,
491 fprintf(out, "Offset = %"PRIu64" bytes\n",
492 lte->resource_entry.offset);
493 fprintf(out, "Size = %"PRIu64" bytes\n",
494 (u64)lte->resource_entry.size);
495 fprintf(out, "Original size = %"PRIu64" bytes\n",
496 lte->resource_entry.original_size);
497 fprintf(out, "Part Number = %hu\n", lte->part_number);
498 fprintf(out, "Reference Count = %u\n", lte->refcnt);
499 fprintf(out, "Hash = 0x");
500 print_hash(lte->hash);
502 fprintf(out, "Flags = ");
503 u8 flags = lte->resource_entry.flags;
504 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
505 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", out);
506 if (flags & WIM_RESHDR_FLAG_FREE)
507 fputs("WIM_RESHDR_FLAG_FREE, ", out);
508 if (flags & WIM_RESHDR_FLAG_METADATA)
509 fputs("WIM_RESHDR_FLAG_METADATA, ", out);
510 if (flags & WIM_RESHDR_FLAG_SPANNED)
511 fputs("WIM_RESHDR_FLAG_SPANNED, ", out);
513 switch (lte->resource_location) {
514 case RESOURCE_IN_WIM:
515 if (lte->wim->filename) {
516 fprintf(out, "WIM file = `%s'\n",
520 case RESOURCE_IN_FILE_ON_DISK:
521 fprintf(out, "File on Disk = `%s'\n", lte->file_on_disk);
523 case RESOURCE_IN_STAGING_FILE:
524 fprintf(out, "Staging File = `%s'\n", lte->staging_file_name);
532 static int do_print_lookup_table_entry(struct wim_lookup_table_entry *lte,
535 print_lookup_table_entry(lte, (FILE*)fp);
540 * Prints the lookup table of a WIM file.
542 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
544 for_lookup_table_entry(w->lookup_table,
545 do_print_lookup_table_entry,
549 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
550 * lookup table, or NULL if there is none. */
551 struct wim_lookup_table_entry *
552 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
555 struct wim_lookup_table_entry *lte;
556 struct hlist_node *pos;
558 wimlib_assert(table != NULL);
559 wimlib_assert(hash != NULL);
561 i = *(size_t*)hash % table->capacity;
562 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
563 if (hashes_equal(hash, lte->hash))
570 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
573 * This is only for pre-resolved inodes.
575 int lookup_resource(WIMStruct *w, const char *path,
577 struct wim_dentry **dentry_ret,
578 struct wim_lookup_table_entry **lte_ret,
581 struct wim_dentry *dentry;
582 struct wim_lookup_table_entry *lte;
584 const char *stream_name = NULL;
585 struct wim_inode *inode;
588 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
589 stream_name = path_stream_name(path);
591 p = (char*)stream_name - 1;
596 dentry = get_dentry(w, path);
602 inode = dentry->d_inode;
604 wimlib_assert(inode->i_resolved);
606 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
607 && inode_is_directory(inode))
611 struct wim_ads_entry *ads_entry;
613 ads_entry = inode_get_ads_entry(inode, stream_name,
616 stream_idx = ads_idx + 1;
617 lte = ads_entry->lte;
628 *dentry_ret = dentry;
632 *stream_idx_ret = stream_idx;
637 /* Resolve an inode's lookup table entries
639 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
640 * lookup table) with pointers directly to the lookup table entries. A circular
641 * linked list of streams sharing the same lookup table entry is created.
643 * This function always succeeds; unresolved lookup table entries are given a
646 void inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
649 if (!inode->i_resolved) {
650 struct wim_lookup_table_entry *lte;
651 /* Resolve the default file stream */
652 lte = __lookup_resource(table, inode->i_hash);
654 inode->i_resolved = 1;
656 /* Resolve the alternate data streams */
657 for (u16 i = 0; i < inode->i_num_ads; i++) {
658 struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
659 lte = __lookup_resource(table, cur_entry->hash);
660 cur_entry->lte = lte;
665 void inode_unresolve_ltes(struct wim_inode *inode)
667 if (inode->i_resolved) {
669 copy_hash(inode->i_hash, inode->i_lte->hash);
671 zero_out_hash(inode->i_hash);
673 for (u16 i = 0; i < inode->i_num_ads; i++) {
674 if (inode->i_ads_entries[i].lte)
675 copy_hash(inode->i_ads_entries[i].hash,
676 inode->i_ads_entries[i].lte->hash);
678 zero_out_hash(inode->i_ads_entries[i].hash);
680 inode->i_resolved = 0;
685 * Returns the lookup table entry for stream @stream_idx of the inode, where
686 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
687 * corresponds to an alternate data stream.
689 * This works for both resolved and un-resolved dentries.
691 struct wim_lookup_table_entry *
692 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
693 const struct wim_lookup_table *table)
695 if (inode->i_resolved)
696 return inode_stream_lte_resolved(inode, stream_idx);
698 return inode_stream_lte_unresolved(inode, stream_idx, table);
702 /* Return the lookup table entry for the unnamed data stream of an inode, or
703 * NULL if there is none.
705 * You'd think this would be easier than it actually is, since the unnamed data
706 * stream should be the one referenced from the inode itself. Alas, if there
707 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
708 * data stream in one of the alternate data streams instead of inside the WIM
709 * dentry itself. So we need to check the alternate data streams too.
711 * Also, note that a dentry may appear to have more than one unnamed stream, but
712 * if the SHA1 message digest is all 0's then the corresponding stream does not
713 * really "count" (this is the case for the inode's own file stream when the
714 * file stream that should be there is actually in one of the alternate stream
715 * entries.). This is despite the fact that we may need to extract such a
716 * missing entry as an empty file or empty named data stream.
718 struct wim_lookup_table_entry *
719 inode_unnamed_lte(const struct wim_inode *inode,
720 const struct wim_lookup_table *table)
722 if (inode->i_resolved)
723 return inode_unnamed_lte_resolved(inode);
725 return inode_unnamed_lte_unresolved(inode, table);
728 static int lte_add_stream_size(struct wim_lookup_table_entry *lte,
731 *(u64*)total_bytes_p += lte->resource_entry.size;
735 u64 lookup_table_total_stream_size(struct wim_lookup_table *table)
738 for_lookup_table_entry(table, lte_add_stream_size, &total_size);