4 * Lookup table, implemented as a hash table, that maps dentries to file
9 * Copyright (C) 2012 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 lookup_table *new_lookup_table(size_t capacity)
38 struct lookup_table *table;
39 struct hlist_head *array;
41 table = MALLOC(sizeof(struct 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 lookup_table_entry *new_lookup_table_entry()
60 struct lookup_table_entry *lte;
62 lte = CALLOC(1, sizeof(struct lookup_table_entry));
67 ERROR("Out of memory (tried to allocate %zu bytes for "
68 "lookup table entry)",
69 sizeof(struct lookup_table_entry));
74 struct lookup_table_entry *
75 clone_lookup_table_entry(const struct lookup_table_entry *old)
77 struct lookup_table_entry *new;
79 new = MALLOC(sizeof(*new));
83 memcpy(new, old, sizeof(*old));
84 new->extracted_file = NULL;
85 switch (new->resource_location) {
86 case RESOURCE_IN_STAGING_FILE:
87 case RESOURCE_IN_FILE_ON_DISK:
88 wimlib_assert((void*)&old->file_on_disk ==
89 (void*)&old->staging_file_name);
90 new->staging_file_name = STRDUP(old->staging_file_name);
91 if (!new->staging_file_name)
94 case RESOURCE_IN_ATTACHED_BUFFER:
95 new->attached_buffer = MALLOC(wim_resource_size(old));
96 if (!new->attached_buffer)
98 memcpy(new->attached_buffer, old->attached_buffer,
99 wim_resource_size(old));
102 case RESOURCE_IN_NTFS_VOLUME:
104 struct ntfs_location *loc;
105 loc = MALLOC(sizeof(*loc));
108 memcpy(loc, old->ntfs_loc, sizeof(*loc));
109 loc->path_utf8 = NULL;
110 loc->stream_name_utf16 = NULL;
112 loc->path_utf8 = STRDUP(old->ntfs_loc->path_utf8);
115 loc->stream_name_utf16 = MALLOC(loc->stream_name_utf16_num_chars * 2);
116 if (!loc->stream_name_utf16)
118 memcpy(loc->stream_name_utf16,
119 old->ntfs_loc->stream_name_utf16,
120 loc->stream_name_utf16_num_chars * 2);
129 free_lookup_table_entry(new);
133 void free_lookup_table_entry(struct lookup_table_entry *lte)
136 switch (lte->resource_location) {
137 case RESOURCE_IN_STAGING_FILE:
138 case RESOURCE_IN_ATTACHED_BUFFER:
139 case RESOURCE_IN_FILE_ON_DISK:
140 wimlib_assert(((void*)<e->file_on_disk ==
141 (void*)<e->staging_file_name)
142 && ((void*)<e->file_on_disk ==
143 (void*)<e->attached_buffer));
144 FREE(lte->file_on_disk);
147 case RESOURCE_IN_NTFS_VOLUME:
149 FREE(lte->ntfs_loc->path_utf8);
150 FREE(lte->ntfs_loc->stream_name_utf16);
162 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
165 free_lookup_table_entry(entry);
170 void free_lookup_table(struct lookup_table *table)
172 DEBUG2("Freeing lookup table");
175 for_lookup_table_entry(table,
176 do_free_lookup_table_entry,
185 * Inserts an entry into the lookup table.
187 * @table: A pointer to the lookup table.
188 * @entry: A pointer to the entry to insert.
190 void lookup_table_insert(struct lookup_table *table,
191 struct lookup_table_entry *lte)
193 size_t i = lte->hash_short % table->capacity;
194 hlist_add_head(<e->hash_list, &table->array[i]);
196 /* XXX Make the table grow when too many entries have been inserted. */
197 table->num_entries++;
200 static void finalize_lte(struct lookup_table_entry *lte)
203 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
204 unlink(lte->staging_file_name);
205 list_del(<e->staging_list);
208 free_lookup_table_entry(lte);
211 /* Decrements the reference count for the lookup table entry @lte. If its
212 * reference count reaches 0, it is unlinked from the lookup table. If,
213 * furthermore, the entry has no opened file descriptors associated with it, the
215 void lte_decrement_refcnt(struct lookup_table_entry *lte,
216 struct lookup_table *table)
218 wimlib_assert(lte != NULL);
219 wimlib_assert(lte->refcnt != 0);
220 if (--lte->refcnt == 0) {
221 lookup_table_unlink(table, lte);
223 if (lte->num_opened_fds == 0)
230 void lte_decrement_num_opened_fds(struct lookup_table_entry *lte)
232 if (lte->num_opened_fds != 0)
233 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
239 * Calls a function on all the entries in the lookup table. Stop early and
240 * return nonzero if any call to the function returns nonzero.
242 int for_lookup_table_entry(struct lookup_table *table,
243 int (*visitor)(struct lookup_table_entry *, void *),
246 struct lookup_table_entry *lte;
247 struct hlist_node *pos, *tmp;
250 for (size_t i = 0; i < table->capacity; i++) {
251 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
254 ret = visitor(lte, arg);
264 * Reads the lookup table from a WIM file.
266 int read_lookup_table(WIMStruct *w)
269 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
271 struct lookup_table *table;
272 struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
274 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
275 ERROR("Didn't expect a compressed lookup table!");
276 ERROR("Ask the author to implement support for this.");
277 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
280 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
281 w->hdr.lookup_table_res_entry.offset,
282 w->hdr.lookup_table_res_entry.original_size);
284 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
286 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
288 w->hdr.lookup_table_res_entry.offset);
289 return WIMLIB_ERR_READ;
292 num_entries = w->hdr.lookup_table_res_entry.original_size /
293 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
294 table = new_lookup_table(num_entries * 2 + 1);
296 return WIMLIB_ERR_NOMEM;
298 while (num_entries--) {
301 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
303 ERROR("Unexpected EOF in WIM lookup table!");
305 ERROR_WITH_ERRNO("Error reading WIM lookup "
308 ret = WIMLIB_ERR_READ;
311 cur_entry = new_lookup_table_entry();
313 ret = WIMLIB_ERR_NOMEM;
317 cur_entry->resource_location = RESOURCE_IN_WIM;
319 p = get_resource_entry(buf, &cur_entry->resource_entry);
320 p = get_u16(p, &cur_entry->part_number);
321 p = get_u32(p, &cur_entry->refcnt);
322 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
324 if (cur_entry->part_number != w->hdr.part_number) {
325 ERROR("A lookup table entry in part %hu of the WIM "
326 "points to part %hu",
327 w->hdr.part_number, cur_entry->part_number);
328 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
329 goto out_free_cur_entry;
332 if (is_zero_hash(cur_entry->hash)) {
333 ERROR("The WIM lookup table contains an entry with a "
334 "SHA1 message digest of all 0's");
335 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
336 goto out_free_cur_entry;
339 /* Ordinarily, no two streams should share the same SHA1 message
340 * digest. However, this constraint can be broken for metadata
341 * resources--- two identical images will have the same metadata
342 * resource, but their lookup table entries are not shared. */
343 duplicate_entry = __lookup_resource(table, cur_entry->hash);
345 && !((duplicate_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
346 && cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA))
348 ERROR("The WIM lookup table contains two entries with the "
349 "same SHA1 message digest!");
350 ERROR("The first entry is:");
351 print_lookup_table_entry(duplicate_entry);
352 ERROR("The second entry is:");
353 print_lookup_table_entry(cur_entry);
354 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
355 goto out_free_cur_entry;
358 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
359 && (cur_entry->resource_entry.size !=
360 cur_entry->resource_entry.original_size))
362 ERROR("Found uncompressed resource with original size "
363 "not the same as compressed size");
364 ERROR("The lookup table entry for the resource is as follows:");
365 print_lookup_table_entry(cur_entry);
366 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
367 goto out_free_cur_entry;
369 if ((cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
370 && cur_entry->refcnt != 1)
372 ERROR("Found metadata resource with refcnt != 1:");
373 print_lookup_table_entry(cur_entry);
374 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
375 goto out_free_cur_entry;
377 lookup_table_insert(table, cur_entry);
380 DEBUG("Done reading lookup table.");
381 w->lookup_table = table;
386 free_lookup_table(table);
392 * Writes a lookup table entry to the output file.
394 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
397 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
402 /* Don't write entries that have not had file resources or metadata
403 * resources written for them. */
404 if (lte->out_refcnt == 0)
407 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
408 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
409 ftello(out), lte->output_resource_entry.original_size);
411 p = put_resource_entry(buf, <e->output_resource_entry);
412 p = put_u16(p, lte->part_number);
413 p = put_u32(p, lte->out_refcnt);
414 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
415 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
416 ERROR_WITH_ERRNO("Failed to write lookup table entry");
417 return WIMLIB_ERR_WRITE;
422 /* Writes the lookup table to the output file. */
423 int write_lookup_table(struct lookup_table *table, FILE *out,
424 struct resource_entry *out_res_entry)
426 off_t start_offset, end_offset;
429 start_offset = ftello(out);
430 if (start_offset == -1)
431 return WIMLIB_ERR_WRITE;
433 ret = for_lookup_table_entry(table, write_lookup_table_entry, out);
437 end_offset = ftello(out);
438 if (end_offset == -1)
439 return WIMLIB_ERR_WRITE;
441 out_res_entry->offset = start_offset;
442 out_res_entry->size = end_offset - start_offset;
443 out_res_entry->original_size = end_offset - start_offset;
444 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
450 int lte_zero_real_refcnt(struct lookup_table_entry *lte, void *ignore)
452 lte->real_refcnt = 0;
456 int lte_zero_out_refcnt(struct lookup_table_entry *lte, void *ignore)
462 int lte_free_extracted_file(struct lookup_table_entry *lte, void *ignore)
464 if (lte->extracted_file != NULL) {
465 FREE(lte->extracted_file);
466 lte->extracted_file = NULL;
471 void print_lookup_table_entry(const struct lookup_table_entry *lte)
477 printf("Offset = %"PRIu64" bytes\n",
478 lte->resource_entry.offset);
479 printf("Size = %"PRIu64" bytes\n",
480 (u64)lte->resource_entry.size);
481 printf("Original size = %"PRIu64" bytes\n",
482 lte->resource_entry.original_size);
483 printf("Part Number = %hu\n", lte->part_number);
484 printf("Reference Count = %u\n", lte->refcnt);
486 print_hash(lte->hash);
489 u8 flags = lte->resource_entry.flags;
490 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
491 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
492 if (flags & WIM_RESHDR_FLAG_FREE)
493 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
494 if (flags & WIM_RESHDR_FLAG_METADATA)
495 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
496 if (flags & WIM_RESHDR_FLAG_SPANNED)
497 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
499 switch (lte->resource_location) {
500 case RESOURCE_IN_WIM:
501 if (lte->wim->filename) {
502 printf("WIM file = `%s'\n",
506 case RESOURCE_IN_FILE_ON_DISK:
507 printf("File on Disk = `%s'\n", lte->file_on_disk);
509 case RESOURCE_IN_STAGING_FILE:
510 printf("Staging File = `%s'\n", lte->staging_file_name);
518 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
521 print_lookup_table_entry(lte);
526 * Prints the lookup table of a WIM file.
528 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
530 for_lookup_table_entry(w->lookup_table,
531 do_print_lookup_table_entry,
536 * Looks up an entry in the lookup table.
538 struct lookup_table_entry *
539 __lookup_resource(const struct lookup_table *table, const u8 hash[])
542 struct lookup_table_entry *lte;
543 struct hlist_node *pos;
545 wimlib_assert(table != NULL);
546 wimlib_assert(hash != NULL);
548 i = *(size_t*)hash % table->capacity;
549 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
550 if (hashes_equal(hash, lte->hash))
557 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
560 * This is only for pre-resolved inodes.
562 int lookup_resource(WIMStruct *w, const char *path,
564 struct dentry **dentry_ret,
565 struct lookup_table_entry **lte_ret,
568 struct dentry *dentry;
569 struct lookup_table_entry *lte;
571 const char *stream_name = NULL;
575 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
576 stream_name = path_stream_name(path);
578 p = (char*)stream_name - 1;
583 dentry = get_dentry(w, path);
589 inode = dentry->d_inode;
591 wimlib_assert(inode->resolved);
593 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
594 && inode_is_directory(inode))
598 struct ads_entry *ads_entry;
600 ads_entry = inode_get_ads_entry(inode, stream_name,
603 stream_idx = ads_idx + 1;
604 lte = ads_entry->lte;
615 *dentry_ret = dentry;
619 *stream_idx_ret = stream_idx;
624 /* Resolve an inode's lookup table entries
626 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
627 * lookup table) with pointers directly to the lookup table entries. A circular
628 * linked list of streams sharing the same lookup table entry is created.
630 * This function always succeeds; unresolved lookup table entries are given a
633 void inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
636 if (!inode->resolved) {
637 struct lookup_table_entry *lte;
638 /* Resolve the default file stream */
639 lte = __lookup_resource(table, inode->hash);
643 /* Resolve the alternate data streams */
644 for (u16 i = 0; i < inode->num_ads; i++) {
645 struct ads_entry *cur_entry = &inode->ads_entries[i];
646 lte = __lookup_resource(table, cur_entry->hash);
647 cur_entry->lte = lte;
652 void inode_unresolve_ltes(struct inode *inode)
654 if (inode->resolved) {
656 copy_hash(inode->hash, inode->lte->hash);
658 zero_out_hash(inode->hash);
660 for (u16 i = 0; i < inode->num_ads; i++) {
661 if (inode->ads_entries[i].lte)
662 copy_hash(inode->ads_entries[i].hash,
663 inode->ads_entries[i].lte->hash);
665 zero_out_hash(inode->ads_entries[i].hash);
672 * Returns the lookup table entry for stream @stream_idx of the inode, where
673 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
674 * corresponds to an alternate data stream.
676 * This works for both resolved and un-resolved dentries.
678 struct lookup_table_entry *
679 inode_stream_lte(const struct inode *inode, unsigned stream_idx,
680 const struct lookup_table *table)
683 return inode_stream_lte_resolved(inode, stream_idx);
685 return inode_stream_lte_unresolved(inode, stream_idx, table);
689 /* Return the lookup table entry for the unnamed data stream of an inode, or
690 * NULL if there is none.
692 * You'd think this would be easier than it actually is, since the unnamed data
693 * stream should be the one referenced from the inode itself. Alas, if there
694 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
695 * data stream in one of the alternate data streams instead of inside the WIM
696 * dentry itself. So we need to check the alternate data streams too.
698 * Also, note that a dentry may appear to have more than one unnamed stream, but
699 * if the SHA1 message digest is all 0's then the corresponding stream does not
700 * really "count" (this is the case for the inode's own file stream when the
701 * file stream that should be there is actually in one of the alternate stream
702 * entries.). This is despite the fact that we may need to extract such a
703 * missing entry as an empty file or empty named data stream.
705 struct lookup_table_entry *
706 inode_unnamed_lte(const struct inode *inode,
707 const struct lookup_table *table)
710 return inode_unnamed_lte_resolved(inode);
712 return inode_unnamed_lte_unresolved(inode, table);
715 static int lte_add_stream_size(struct lookup_table_entry *lte,
718 *(u64*)total_bytes_p += lte->resource_entry.size;
722 u64 lookup_table_total_stream_size(struct lookup_table *table)
725 for_lookup_table_entry(table, lte_add_stream_size, &total_size);