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));
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);
126 free_lookup_table_entry(new);
130 void free_lookup_table_entry(struct lookup_table_entry *lte)
133 switch (lte->resource_location) {
134 case RESOURCE_IN_STAGING_FILE:
135 case RESOURCE_IN_ATTACHED_BUFFER:
136 case RESOURCE_IN_FILE_ON_DISK:
137 wimlib_assert(((void*)<e->file_on_disk ==
138 (void*)<e->staging_file_name)
139 && ((void*)<e->file_on_disk ==
140 (void*)<e->attached_buffer));
141 FREE(lte->file_on_disk);
144 case RESOURCE_IN_NTFS_VOLUME:
146 FREE(lte->ntfs_loc->path_utf8);
147 FREE(lte->ntfs_loc->stream_name_utf16);
155 FREE(lte->extracted_file);
160 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
163 free_lookup_table_entry(entry);
168 void free_lookup_table(struct lookup_table *table)
170 DEBUG2("Freeing lookup table");
173 for_lookup_table_entry(table,
174 do_free_lookup_table_entry,
183 * Inserts an entry into the lookup table.
185 * @table: A pointer to the lookup table.
186 * @entry: A pointer to the entry to insert.
188 void lookup_table_insert(struct lookup_table *table,
189 struct lookup_table_entry *lte)
191 size_t i = lte->hash_short % table->capacity;
192 hlist_add_head(<e->hash_list, &table->array[i]);
194 /* XXX Make the table grow when too many entries have been inserted. */
195 table->num_entries++;
198 static void finalize_lte(struct lookup_table_entry *lte)
201 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
202 unlink(lte->staging_file_name);
203 wimlib_assert(lte->staging_list.next);
204 wimlib_assert(lte->staging_list.prev);
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)
219 wimlib_assert(lte->refcnt);
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,
231 struct lookup_table *table)
234 wimlib_assert(lte->num_opened_fds);
235 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
241 * Calls a function on all the entries in the lookup table. Stop early and
242 * return nonzero if any call to the function returns nonzero.
244 int for_lookup_table_entry(struct lookup_table *table,
245 int (*visitor)(struct lookup_table_entry *, void *),
248 struct lookup_table_entry *lte;
249 struct hlist_node *pos, *tmp;
252 for (size_t i = 0; i < table->capacity; i++) {
253 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
256 ret = visitor(lte, arg);
266 * Reads the lookup table from a WIM file.
268 int read_lookup_table(WIMStruct *w)
271 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
273 struct lookup_table *table;
274 struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
276 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
277 w->hdr.lookup_table_res_entry.offset,
278 w->hdr.lookup_table_res_entry.original_size);
280 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
282 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
284 w->hdr.lookup_table_res_entry.offset);
285 return WIMLIB_ERR_READ;
288 num_entries = w->hdr.lookup_table_res_entry.original_size /
289 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
290 table = new_lookup_table(num_entries * 2 + 1);
292 return WIMLIB_ERR_NOMEM;
294 while (num_entries--) {
297 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
299 ERROR("Unexpected EOF in WIM lookup table!");
301 ERROR_WITH_ERRNO("Error reading WIM lookup "
304 ret = WIMLIB_ERR_READ;
307 cur_entry = new_lookup_table_entry();
309 ret = WIMLIB_ERR_NOMEM;
313 cur_entry->resource_location = RESOURCE_IN_WIM;
315 p = get_resource_entry(buf, &cur_entry->resource_entry);
316 p = get_u16(p, &cur_entry->part_number);
317 p = get_u32(p, &cur_entry->refcnt);
318 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
320 if (cur_entry->part_number != w->hdr.part_number) {
321 ERROR("A lookup table entry in part %hu of the WIM "
322 "points to part %hu",
323 w->hdr.part_number, cur_entry->part_number);
324 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
325 goto out_free_cur_entry;
328 if (is_zero_hash(cur_entry->hash)) {
329 ERROR("The WIM lookup table contains an entry with a "
330 "SHA1 message digest of all 0's");
331 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
332 goto out_free_cur_entry;
335 /* Ordinarily, no two streams should share the same SHA1 message
336 * digest. However, this constraint can be broken for metadata
337 * resources--- two identical images will have the same metadata
338 * resource, but their lookup table entries are not shared. */
339 duplicate_entry = __lookup_resource(table, cur_entry->hash);
341 && !((duplicate_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
342 && cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA))
344 ERROR("The WIM lookup table contains two entries with the "
345 "same SHA1 message digest!");
346 ERROR("The first entry is:");
347 print_lookup_table_entry(duplicate_entry);
348 ERROR("The second entry is:");
349 print_lookup_table_entry(cur_entry);
350 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
351 goto out_free_cur_entry;
354 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
355 && (cur_entry->resource_entry.size !=
356 cur_entry->resource_entry.original_size))
358 ERROR("Found uncompressed resource with original size "
359 "not the same as compressed size");
360 ERROR("The lookup table entry for the resource is as follows:");
361 print_lookup_table_entry(cur_entry);
362 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
363 goto out_free_cur_entry;
365 lookup_table_insert(table, cur_entry);
368 DEBUG("Done reading lookup table.");
369 w->lookup_table = table;
374 free_lookup_table(table);
380 * Writes a lookup table entry to the output file.
382 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
385 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
390 /* Don't write entries that have not had file resources or metadata
391 * resources written for them. */
392 if (lte->out_refcnt == 0)
395 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
396 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
397 ftello(out), lte->output_resource_entry.original_size);
399 p = put_resource_entry(buf, <e->output_resource_entry);
400 p = put_u16(p, lte->part_number);
401 p = put_u32(p, lte->out_refcnt);
402 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
403 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
404 ERROR_WITH_ERRNO("Failed to write lookup table entry");
405 return WIMLIB_ERR_WRITE;
411 int lte_zero_real_refcnt(struct lookup_table_entry *lte, void *ignore)
413 lte->real_refcnt = 0;
417 int lte_zero_out_refcnt(struct lookup_table_entry *lte, void *ignore)
423 int lte_free_extracted_file(struct lookup_table_entry *lte, void *ignone)
425 FREE(lte->extracted_file);
426 lte->extracted_file = NULL;
430 void print_lookup_table_entry(const struct lookup_table_entry *lte)
436 printf("Offset = %"PRIu64" bytes\n",
437 lte->resource_entry.offset);
438 printf("Size = %"PRIu64" bytes\n",
439 (u64)lte->resource_entry.size);
440 printf("Original size = %"PRIu64" bytes\n",
441 lte->resource_entry.original_size);
442 printf("Part Number = %hu\n", lte->part_number);
443 printf("Reference Count = %u\n", lte->refcnt);
445 print_hash(lte->hash);
448 u8 flags = lte->resource_entry.flags;
449 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
450 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
451 if (flags & WIM_RESHDR_FLAG_FREE)
452 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
453 if (flags & WIM_RESHDR_FLAG_METADATA)
454 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
455 if (flags & WIM_RESHDR_FLAG_SPANNED)
456 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
458 switch (lte->resource_location) {
459 case RESOURCE_IN_WIM:
460 if (lte->wim->filename) {
461 printf("WIM file = `%s'\n",
465 case RESOURCE_IN_FILE_ON_DISK:
466 printf("File on Disk = `%s'\n", lte->file_on_disk);
468 case RESOURCE_IN_STAGING_FILE:
469 printf("Staging File = `%s'\n", lte->staging_file_name);
477 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
480 print_lookup_table_entry(lte);
485 * Prints the lookup table of a WIM file.
487 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
489 for_lookup_table_entry(w->lookup_table,
490 do_print_lookup_table_entry,
495 * Looks up an entry in the lookup table.
497 struct lookup_table_entry *
498 __lookup_resource(const struct lookup_table *table, const u8 hash[])
501 struct lookup_table_entry *lte;
502 struct hlist_node *pos;
504 wimlib_assert(table != NULL);
505 wimlib_assert(hash != NULL);
507 i = *(size_t*)hash % table->capacity;
508 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
509 if (hashes_equal(hash, lte->hash))
516 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
519 * This is only for pre-resolved inodes.
521 int lookup_resource(WIMStruct *w, const char *path,
523 struct dentry **dentry_ret,
524 struct lookup_table_entry **lte_ret,
527 struct dentry *dentry;
528 struct lookup_table_entry *lte;
530 const char *stream_name = NULL;
534 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
535 stream_name = path_stream_name(path);
537 p = (char*)stream_name - 1;
542 dentry = get_dentry(w, path);
548 inode = dentry->d_inode;
550 wimlib_assert(inode->resolved);
552 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
553 && inode_is_directory(inode))
557 struct ads_entry *ads_entry;
559 ads_entry = inode_get_ads_entry(inode, stream_name,
562 stream_idx = ads_idx + 1;
563 lte = ads_entry->lte;
574 *dentry_ret = dentry;
578 *stream_idx_ret = stream_idx;
583 static void inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
585 struct lookup_table_entry *lte;
587 wimlib_assert(!inode->resolved);
589 /* Resolve the default file stream */
590 lte = __lookup_resource(table, inode->hash);
592 inode->resolved = true;
594 /* Resolve the alternate data streams */
595 for (u16 i = 0; i < inode->num_ads; i++) {
596 struct ads_entry *cur_entry = &inode->ads_entries[i];
597 lte = __lookup_resource(table, cur_entry->hash);
598 cur_entry->lte = lte;
602 static void inode_unresolve_ltes(struct inode *inode)
604 wimlib_assert(inode->resolved);
606 copy_hash(inode->hash, inode->lte->hash);
608 zero_out_hash(inode->hash);
610 for (u16 i = 0; i < inode->num_ads; i++) {
611 if (inode->ads_entries[i].lte)
612 copy_hash(inode->ads_entries[i].hash,
613 inode->ads_entries[i].lte->hash);
615 zero_out_hash(inode->ads_entries[i].hash);
617 inode->resolved = false;
620 /* Resolve a dentry's lookup table entries
622 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
623 * lookup table) with pointers directly to the lookup table entries. A circular
624 * linked list of streams sharing the same lookup table entry is created.
626 * This function always succeeds; unresolved lookup table entries are given a
629 int dentry_resolve_ltes(struct dentry *dentry, void *table)
631 if (!dentry->d_inode->resolved)
632 inode_resolve_ltes(dentry->d_inode, table);
636 int dentry_unresolve_ltes(struct dentry *dentry, void *ignore)
638 if (dentry->d_inode->resolved)
639 inode_unresolve_ltes(dentry->d_inode);
643 /* Return the lookup table entry for the unnamed data stream of an inode, or
644 * NULL if there is none.
646 * You'd think this would be easier than it actually is, since the unnamed data
647 * stream should be the one referenced from the inode itself. Alas, if there
648 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
649 * data stream in one of the alternate data streams instead of inside the WIM
650 * dentry itself. So we need to check the alternate data streams too.
652 * Also, note that a dentry may appear to have more than one unnamed stream, but
653 * if the SHA1 message digest is all 0's then the corresponding stream does not
654 * really "count" (this is the case for the inode's own file stream when the
655 * file stream that should be there is actually in one of the alternate stream
656 * entries.). This is despite the fact that we may need to extract such a
657 * missing entry as an empty file or empty named data stream.
659 struct lookup_table_entry *
660 inode_unnamed_lte(const struct inode *inode,
661 const struct lookup_table *table)
664 return inode_unnamed_lte_resolved(inode);
666 return inode_unnamed_lte_unresolved(inode, table);