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"
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));
44 array = CALLOC(capacity, sizeof(array[0]));
49 table->num_entries = 0;
50 table->capacity = capacity;
54 ERROR("Failed to allocate memory for lookup table with capacity %zu",
59 struct lookup_table_entry *new_lookup_table_entry()
61 struct lookup_table_entry *lte;
63 lte = CALLOC(1, sizeof(struct lookup_table_entry));
68 ERROR("Out of memory (tried to allocate %zu bytes for "
69 "lookup table entry)",
70 sizeof(struct lookup_table_entry));
75 struct lookup_table_entry *
76 clone_lookup_table_entry(const struct lookup_table_entry *old)
78 struct 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 wimlib_assert((void*)&old->file_on_disk ==
90 (void*)&old->staging_file_name);
91 new->staging_file_name = STRDUP(old->staging_file_name);
92 if (!new->staging_file_name)
95 case RESOURCE_IN_ATTACHED_BUFFER:
96 new->attached_buffer = MALLOC(wim_resource_size(old));
97 if (!new->attached_buffer)
99 memcpy(new->attached_buffer, old->attached_buffer,
100 wim_resource_size(old));
103 case RESOURCE_IN_NTFS_VOLUME:
105 struct ntfs_location *loc;
106 loc = MALLOC(sizeof(*loc));
109 memcpy(loc, old->ntfs_loc, sizeof(*loc));
110 loc->path_utf8 = NULL;
111 loc->stream_name_utf16 = NULL;
113 loc->path_utf8 = STRDUP(old->ntfs_loc->path_utf8);
116 loc->stream_name_utf16 = MALLOC(loc->stream_name_utf16_num_chars * 2);
117 if (!loc->stream_name_utf16)
119 memcpy(loc->stream_name_utf16,
120 old->ntfs_loc->stream_name_utf16,
121 loc->stream_name_utf16_num_chars * 2);
130 free_lookup_table_entry(new);
134 void free_lookup_table_entry(struct lookup_table_entry *lte)
137 switch (lte->resource_location) {
138 case RESOURCE_IN_STAGING_FILE:
139 case RESOURCE_IN_ATTACHED_BUFFER:
140 case RESOURCE_IN_FILE_ON_DISK:
141 wimlib_assert(((void*)<e->file_on_disk ==
142 (void*)<e->staging_file_name)
143 && ((void*)<e->file_on_disk ==
144 (void*)<e->attached_buffer));
145 FREE(lte->file_on_disk);
148 case RESOURCE_IN_NTFS_VOLUME:
150 FREE(lte->ntfs_loc->path_utf8);
151 FREE(lte->ntfs_loc->stream_name_utf16);
163 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
166 free_lookup_table_entry(entry);
171 void free_lookup_table(struct lookup_table *table)
173 DEBUG2("Freeing lookup table");
176 for_lookup_table_entry(table,
177 do_free_lookup_table_entry,
186 * Inserts an entry into the lookup table.
188 * @table: A pointer to the lookup table.
189 * @entry: A pointer to the entry to insert.
191 void lookup_table_insert(struct lookup_table *table,
192 struct lookup_table_entry *lte)
194 size_t i = lte->hash_short % table->capacity;
195 hlist_add_head(<e->hash_list, &table->array[i]);
197 /* XXX Make the table grow when too many entries have been inserted. */
198 table->num_entries++;
201 static void finalize_lte(struct lookup_table_entry *lte)
204 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
205 unlink(lte->staging_file_name);
206 wimlib_assert(lte->staging_list.next);
207 wimlib_assert(lte->staging_list.prev);
208 list_del(<e->staging_list);
211 free_lookup_table_entry(lte);
214 /* Decrements the reference count for the lookup table entry @lte. If its
215 * reference count reaches 0, it is unlinked from the lookup table. If,
216 * furthermore, the entry has no opened file descriptors associated with it, the
218 void lte_decrement_refcnt(struct lookup_table_entry *lte,
219 struct lookup_table *table)
221 wimlib_assert(lte != NULL);
222 wimlib_assert(lte->refcnt != 0);
223 if (--lte->refcnt == 0) {
224 lookup_table_unlink(table, lte);
226 if (lte->num_opened_fds == 0)
233 void lte_decrement_num_opened_fds(struct lookup_table_entry *lte)
235 wimlib_assert(lte != NULL);
236 wimlib_assert(lte->num_opened_fds != 0);
237 if (atomic_dec(<e->num_opened_fds) == 0 && lte->refcnt == 0)
243 * Calls a function on all the entries in the lookup table. Stop early and
244 * return nonzero if any call to the function returns nonzero.
246 int for_lookup_table_entry(struct lookup_table *table,
247 int (*visitor)(struct lookup_table_entry *, void *),
250 struct lookup_table_entry *lte;
251 struct hlist_node *pos, *tmp;
254 for (size_t i = 0; i < table->capacity; i++) {
255 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
258 ret = visitor(lte, arg);
268 * Reads the lookup table from a WIM file.
270 int read_lookup_table(WIMStruct *w)
273 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
275 struct lookup_table *table;
276 struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
278 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
279 w->hdr.lookup_table_res_entry.offset,
280 w->hdr.lookup_table_res_entry.original_size);
282 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
284 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
286 w->hdr.lookup_table_res_entry.offset);
287 return WIMLIB_ERR_READ;
290 num_entries = w->hdr.lookup_table_res_entry.original_size /
291 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
292 table = new_lookup_table(num_entries * 2 + 1);
294 return WIMLIB_ERR_NOMEM;
296 while (num_entries--) {
299 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
301 ERROR("Unexpected EOF in WIM lookup table!");
303 ERROR_WITH_ERRNO("Error reading WIM lookup "
306 ret = WIMLIB_ERR_READ;
309 cur_entry = new_lookup_table_entry();
311 ret = WIMLIB_ERR_NOMEM;
315 cur_entry->resource_location = RESOURCE_IN_WIM;
317 p = get_resource_entry(buf, &cur_entry->resource_entry);
318 p = get_u16(p, &cur_entry->part_number);
319 p = get_u32(p, &cur_entry->refcnt);
320 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
322 if (cur_entry->part_number != w->hdr.part_number) {
323 ERROR("A lookup table entry in part %hu of the WIM "
324 "points to part %hu",
325 w->hdr.part_number, cur_entry->part_number);
326 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
327 goto out_free_cur_entry;
330 if (is_zero_hash(cur_entry->hash)) {
331 ERROR("The WIM lookup table contains an entry with a "
332 "SHA1 message digest of all 0's");
333 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
334 goto out_free_cur_entry;
337 /* Ordinarily, no two streams should share the same SHA1 message
338 * digest. However, this constraint can be broken for metadata
339 * resources--- two identical images will have the same metadata
340 * resource, but their lookup table entries are not shared. */
341 duplicate_entry = __lookup_resource(table, cur_entry->hash);
343 && !((duplicate_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
344 && cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA))
346 ERROR("The WIM lookup table contains two entries with the "
347 "same SHA1 message digest!");
348 ERROR("The first entry is:");
349 print_lookup_table_entry(duplicate_entry);
350 ERROR("The second entry is:");
351 print_lookup_table_entry(cur_entry);
352 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
353 goto out_free_cur_entry;
356 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
357 && (cur_entry->resource_entry.size !=
358 cur_entry->resource_entry.original_size))
360 ERROR("Found uncompressed resource with original size "
361 "not the same as compressed size");
362 ERROR("The lookup table entry for the resource is as follows:");
363 print_lookup_table_entry(cur_entry);
364 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
365 goto out_free_cur_entry;
367 lookup_table_insert(table, cur_entry);
370 DEBUG("Done reading lookup table.");
371 w->lookup_table = table;
376 free_lookup_table(table);
382 * Writes a lookup table entry to the output file.
384 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
387 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
392 /* Don't write entries that have not had file resources or metadata
393 * resources written for them. */
394 if (lte->out_refcnt == 0)
397 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
398 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
399 ftello(out), lte->output_resource_entry.original_size);
401 p = put_resource_entry(buf, <e->output_resource_entry);
402 p = put_u16(p, lte->part_number);
403 p = put_u32(p, lte->out_refcnt);
404 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
405 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
406 ERROR_WITH_ERRNO("Failed to write lookup table entry");
407 return WIMLIB_ERR_WRITE;
413 int lte_zero_real_refcnt(struct lookup_table_entry *lte, void *ignore)
415 lte->real_refcnt = 0;
419 int lte_zero_out_refcnt(struct lookup_table_entry *lte, void *ignore)
425 int lte_zero_extracted_file(struct lookup_table_entry *lte, void *ignore)
427 lte->extracted_file = NULL;
431 int lte_free_extracted_file(struct lookup_table_entry *lte, void *ignore)
433 if (lte->extracted_file != NULL) {
434 FREE(lte->extracted_file);
435 lte->extracted_file = NULL;
440 void print_lookup_table_entry(const struct lookup_table_entry *lte)
446 printf("Offset = %"PRIu64" bytes\n",
447 lte->resource_entry.offset);
448 printf("Size = %"PRIu64" bytes\n",
449 (u64)lte->resource_entry.size);
450 printf("Original size = %"PRIu64" bytes\n",
451 lte->resource_entry.original_size);
452 printf("Part Number = %hu\n", lte->part_number);
453 printf("Reference Count = %u\n", lte->refcnt);
455 print_hash(lte->hash);
458 u8 flags = lte->resource_entry.flags;
459 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
460 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
461 if (flags & WIM_RESHDR_FLAG_FREE)
462 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
463 if (flags & WIM_RESHDR_FLAG_METADATA)
464 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
465 if (flags & WIM_RESHDR_FLAG_SPANNED)
466 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
468 switch (lte->resource_location) {
469 case RESOURCE_IN_WIM:
470 if (lte->wim->filename) {
471 printf("WIM file = `%s'\n",
475 case RESOURCE_IN_FILE_ON_DISK:
476 printf("File on Disk = `%s'\n", lte->file_on_disk);
478 case RESOURCE_IN_STAGING_FILE:
479 printf("Staging File = `%s'\n", lte->staging_file_name);
487 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
490 print_lookup_table_entry(lte);
495 * Prints the lookup table of a WIM file.
497 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
499 for_lookup_table_entry(w->lookup_table,
500 do_print_lookup_table_entry,
505 * Looks up an entry in the lookup table.
507 struct lookup_table_entry *
508 __lookup_resource(const struct lookup_table *table, const u8 hash[])
511 struct lookup_table_entry *lte;
512 struct hlist_node *pos;
514 wimlib_assert(table != NULL);
515 wimlib_assert(hash != NULL);
517 i = *(size_t*)hash % table->capacity;
518 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
519 if (hashes_equal(hash, lte->hash))
526 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
529 * This is only for pre-resolved inodes.
531 int lookup_resource(WIMStruct *w, const char *path,
533 struct dentry **dentry_ret,
534 struct lookup_table_entry **lte_ret,
537 struct dentry *dentry;
538 struct lookup_table_entry *lte;
540 const char *stream_name = NULL;
544 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
545 stream_name = path_stream_name(path);
547 p = (char*)stream_name - 1;
552 dentry = get_dentry(w, path);
558 inode = dentry->d_inode;
560 wimlib_assert(inode->resolved);
562 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
563 && inode_is_directory(inode))
567 struct ads_entry *ads_entry;
569 ads_entry = inode_get_ads_entry(inode, stream_name,
572 stream_idx = ads_idx + 1;
573 lte = ads_entry->lte;
584 *dentry_ret = dentry;
588 *stream_idx_ret = stream_idx;
593 static void inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
595 struct lookup_table_entry *lte;
597 wimlib_assert(!inode->resolved);
599 /* Resolve the default file stream */
600 lte = __lookup_resource(table, inode->hash);
602 inode->resolved = true;
604 /* Resolve the alternate data streams */
605 for (u16 i = 0; i < inode->num_ads; i++) {
606 struct ads_entry *cur_entry = &inode->ads_entries[i];
607 lte = __lookup_resource(table, cur_entry->hash);
608 cur_entry->lte = lte;
612 static void inode_unresolve_ltes(struct inode *inode)
614 wimlib_assert(inode->resolved);
616 copy_hash(inode->hash, inode->lte->hash);
618 zero_out_hash(inode->hash);
620 for (u16 i = 0; i < inode->num_ads; i++) {
621 if (inode->ads_entries[i].lte)
622 copy_hash(inode->ads_entries[i].hash,
623 inode->ads_entries[i].lte->hash);
625 zero_out_hash(inode->ads_entries[i].hash);
627 inode->resolved = false;
630 /* Resolve a dentry's lookup table entries
632 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
633 * lookup table) with pointers directly to the lookup table entries. A circular
634 * linked list of streams sharing the same lookup table entry is created.
636 * This function always succeeds; unresolved lookup table entries are given a
639 int dentry_resolve_ltes(struct dentry *dentry, void *table)
641 if (!dentry->d_inode->resolved)
642 inode_resolve_ltes(dentry->d_inode, table);
646 int dentry_unresolve_ltes(struct dentry *dentry, void *ignore)
648 if (dentry->d_inode->resolved)
649 inode_unresolve_ltes(dentry->d_inode);
653 /* Return the lookup table entry for the unnamed data stream of an inode, or
654 * NULL if there is none.
656 * You'd think this would be easier than it actually is, since the unnamed data
657 * stream should be the one referenced from the inode itself. Alas, if there
658 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
659 * data stream in one of the alternate data streams instead of inside the WIM
660 * dentry itself. So we need to check the alternate data streams too.
662 * Also, note that a dentry may appear to have more than one unnamed stream, but
663 * if the SHA1 message digest is all 0's then the corresponding stream does not
664 * really "count" (this is the case for the inode's own file stream when the
665 * file stream that should be there is actually in one of the alternate stream
666 * entries.). This is despite the fact that we may need to extract such a
667 * missing entry as an empty file or empty named data stream.
669 struct lookup_table_entry *
670 inode_unnamed_lte(const struct inode *inode,
671 const struct lookup_table *table)
674 return inode_unnamed_lte_resolved(inode);
676 return inode_unnamed_lte_unresolved(inode, table);