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));
65 ERROR("Out of memory (tried to allocate %zu bytes for "
66 "lookup table entry)",
67 sizeof(struct lookup_table_entry));
76 void free_lookup_table_entry(struct lookup_table_entry *lte)
80 if (lte->staging_list.next)
81 list_del(<e->staging_list);
83 switch (lte->resource_location) {
84 case RESOURCE_IN_STAGING_FILE:
85 case RESOURCE_IN_ATTACHED_BUFFER:
86 case RESOURCE_IN_FILE_ON_DISK:
87 wimlib_assert(((void*)<e->file_on_disk ==
88 (void*)<e->staging_file_name)
89 && ((void*)<e->file_on_disk ==
90 (void*)<e->attached_buffer));
91 FREE(lte->file_on_disk);
94 case RESOURCE_IN_NTFS_VOLUME:
96 FREE(lte->ntfs_loc->path_utf8);
97 FREE(lte->ntfs_loc->stream_name_utf16);
105 FREE(lte->extracted_file);
110 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
113 free_lookup_table_entry(entry);
118 void free_lookup_table(struct lookup_table *table)
120 DEBUG2("Freeing lookup table");
123 for_lookup_table_entry(table,
124 do_free_lookup_table_entry,
133 * Inserts an entry into the lookup table.
135 * @table: A pointer to the lookup table.
136 * @entry: A pointer to the entry to insert.
138 void lookup_table_insert(struct lookup_table *table,
139 struct lookup_table_entry *lte)
141 size_t i = lte->hash_short % table->capacity;
142 hlist_add_head(<e->hash_list, &table->array[i]);
144 /* XXX Make the table grow when too many entries have been inserted. */
145 table->num_entries++;
148 static void finalize_lte(struct lookup_table_entry *lte)
151 if (lte->resource_location == RESOURCE_IN_STAGING_FILE)
152 unlink(lte->staging_file_name);
154 free_lookup_table_entry(lte);
157 /* Decrements the reference count for the lookup table entry @lte. If its
158 * reference count reaches 0, it is unlinked from the lookup table. If,
159 * furthermore, the entry has no opened file descriptors associated with it, the
161 struct lookup_table_entry *
162 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
165 wimlib_assert(lte->refcnt);
166 if (--lte->refcnt == 0) {
167 lookup_table_unlink(table, lte);
169 if (lte->num_opened_fds == 0)
181 struct lookup_table_entry *
182 lte_decrement_num_opened_fds(struct lookup_table_entry *lte,
183 struct lookup_table *table)
186 wimlib_assert(lte->num_opened_fds);
187 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
188 lookup_table_unlink(table, lte);
198 * Calls a function on all the entries in the lookup table. Stop early and
199 * return nonzero if any call to the function returns nonzero.
201 int for_lookup_table_entry(struct lookup_table *table,
202 int (*visitor)(struct lookup_table_entry *, void *),
205 struct lookup_table_entry *lte;
206 struct hlist_node *pos, *tmp;
209 for (size_t i = 0; i < table->capacity; i++) {
210 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
213 ret = visitor(lte, arg);
223 * Reads the lookup table from a WIM file.
225 int read_lookup_table(WIMStruct *w)
228 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
230 struct lookup_table *table;
231 struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
233 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
234 w->hdr.lookup_table_res_entry.offset,
235 w->hdr.lookup_table_res_entry.original_size);
237 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
238 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
240 w->hdr.lookup_table_res_entry.offset);
241 return WIMLIB_ERR_READ;
244 num_entries = w->hdr.lookup_table_res_entry.original_size /
245 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
246 table = new_lookup_table(num_entries * 2 + 1);
248 return WIMLIB_ERR_NOMEM;
250 while (num_entries--) {
253 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
255 ERROR("Unexpected EOF in WIM lookup table!");
257 ERROR_WITH_ERRNO("Error reading WIM lookup "
260 ret = WIMLIB_ERR_READ;
263 cur_entry = new_lookup_table_entry();
265 ret = WIMLIB_ERR_NOMEM;
269 cur_entry->resource_location = RESOURCE_IN_WIM;
271 p = get_resource_entry(buf, &cur_entry->resource_entry);
272 p = get_u16(p, &cur_entry->part_number);
273 p = get_u32(p, &cur_entry->refcnt);
274 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
276 if (cur_entry->part_number != w->hdr.part_number) {
277 ERROR("A lookup table entry in part %hu of the WIM "
278 "points to part %hu",
279 w->hdr.part_number, cur_entry->part_number);
280 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
281 goto out_free_cur_entry;
285 if (is_zero_hash(cur_entry->hash)) {
286 ERROR("The WIM lookup table contains an entry with a "
287 "SHA1 message digest of all 0's");
288 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
289 goto out_free_cur_entry;
292 duplicate_entry = __lookup_resource(table, cur_entry->hash);
293 if (duplicate_entry) {
294 ERROR("The WIM lookup table contains two entries with the "
295 "same SHA1 message digest!");
296 ERROR("The first entry is:");
297 print_lookup_table_entry(duplicate_entry);
298 ERROR("The second entry is:");
299 print_lookup_table_entry(cur_entry);
300 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
301 goto out_free_cur_entry;
304 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
305 && (cur_entry->resource_entry.size !=
306 cur_entry->resource_entry.original_size))
308 ERROR("Found uncompressed resource with original size "
309 "not the same as compressed size");
310 ERROR("The lookup table entry for the resource is as follows:");
311 print_lookup_table_entry(cur_entry);
312 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
313 goto out_free_cur_entry;
315 lookup_table_insert(table, cur_entry);
318 DEBUG("Done reading lookup table.");
319 w->lookup_table = table;
324 free_lookup_table(table);
330 * Writes a lookup table entry to the output file.
332 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
335 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
340 /* Don't write entries that have not had file resources or metadata
341 * resources written for them. */
342 if (lte->out_refcnt == 0)
345 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
346 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
347 ftello(out), lte->output_resource_entry.original_size);
349 p = put_resource_entry(buf, <e->output_resource_entry);
350 p = put_u16(p, lte->part_number);
351 p = put_u32(p, lte->out_refcnt);
352 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
353 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
354 ERROR_WITH_ERRNO("Failed to write lookup table entry");
355 return WIMLIB_ERR_WRITE;
361 int lte_zero_real_refcnt(struct lookup_table_entry *lte, void *ignore)
363 lte->real_refcnt = 0;
367 int lte_zero_out_refcnt(struct lookup_table_entry *lte, void *ignore)
373 int lte_free_extracted_file(struct lookup_table_entry *lte, void *ignone)
375 FREE(lte->extracted_file);
376 lte->extracted_file = NULL;
380 void print_lookup_table_entry(const struct lookup_table_entry *lte)
386 printf("Offset = %"PRIu64" bytes\n",
387 lte->resource_entry.offset);
388 printf("Size = %"PRIu64" bytes\n",
389 (u64)lte->resource_entry.size);
390 printf("Original size = %"PRIu64" bytes\n",
391 lte->resource_entry.original_size);
392 printf("Part Number = %hu\n", lte->part_number);
393 printf("Reference Count = %u\n", lte->refcnt);
395 print_hash(lte->hash);
398 u8 flags = lte->resource_entry.flags;
399 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
400 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
401 if (flags & WIM_RESHDR_FLAG_FREE)
402 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
403 if (flags & WIM_RESHDR_FLAG_METADATA)
404 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
405 if (flags & WIM_RESHDR_FLAG_SPANNED)
406 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
408 switch (lte->resource_location) {
409 case RESOURCE_IN_WIM:
410 if (lte->wim->filename) {
411 printf("WIM file = `%s'\n",
415 case RESOURCE_IN_FILE_ON_DISK:
416 printf("File on Disk = `%s'\n", lte->file_on_disk);
418 case RESOURCE_IN_STAGING_FILE:
419 printf("Staging File = `%s'\n", lte->staging_file_name);
427 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
430 print_lookup_table_entry(lte);
435 * Prints the lookup table of a WIM file.
437 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
439 for_lookup_table_entry(w->lookup_table,
440 do_print_lookup_table_entry,
445 * Looks up an entry in the lookup table.
447 struct lookup_table_entry *
448 __lookup_resource(const struct lookup_table *table, const u8 hash[])
451 struct lookup_table_entry *lte;
452 struct hlist_node *pos;
454 i = *(size_t*)hash % table->capacity;
455 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
456 if (hashes_equal(hash, lte->hash))
462 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
465 * This is only for pre-resolved inodes.
467 int lookup_resource(WIMStruct *w, const char *path,
469 struct dentry **dentry_ret,
470 struct lookup_table_entry **lte_ret,
473 struct dentry *dentry;
474 struct lookup_table_entry *lte;
476 const char *stream_name = NULL;
480 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
481 stream_name = path_stream_name(path);
483 p = (char*)stream_name - 1;
488 dentry = get_dentry(w, path);
494 inode = dentry->inode;
496 wimlib_assert(inode->resolved);
498 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
499 && inode_is_directory(inode))
503 struct ads_entry *ads_entry;
505 ads_entry = inode_get_ads_entry(inode, stream_name,
508 stream_idx = ads_idx + 1;
509 lte = ads_entry->lte;
520 *dentry_ret = dentry;
524 *stream_idx_ret = stream_idx;
528 static void inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
530 struct lookup_table_entry *lte;
532 /* Resolve the default file stream */
533 lte = __lookup_resource(table, inode->hash);
535 inode->resolved = true;
537 /* Resolve the alternate data streams */
538 for (u16 i = 0; i < inode->num_ads; i++) {
539 struct ads_entry *cur_entry = inode->ads_entries[i];
541 lte = __lookup_resource(table, cur_entry->hash);
542 cur_entry->lte = lte;
546 /* Resolve a dentry's lookup table entries
548 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
549 * lookup table) with pointers directly to the lookup table entries. A circular
550 * linked list of streams sharing the same lookup table entry is created.
552 * This function always succeeds; unresolved lookup table entries are given a
555 int dentry_resolve_ltes(struct dentry *dentry, void *table)
557 if (!dentry->inode->resolved)
558 inode_resolve_ltes(dentry->inode, table);
562 /* Return the lookup table entry for the unnamed data stream of an inode, or
563 * NULL if there is none.
565 * You'd think this would be easier than it actually is, since the unnamed data
566 * stream should be the one referenced from the inode itself. Alas, if there
567 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
568 * data stream in one of the alternate data streams instead of inside the WIM
569 * dentry itself. So we need to check the alternate data streams too.
571 * Also, note that a dentry may appear to have than one unnamed stream, but if
572 * the SHA1 message digest is all 0's then the corresponding stream does not
573 * really "count" (this is the case for the inode's own file stream when the
574 * file stream that should be there is actually in one of the alternate stream
575 * entries.). This is despite the fact that we may need to extract such a
576 * missing entry as an empty file or empty named data stream.
578 struct lookup_table_entry *
579 inode_unnamed_lte(const struct inode *inode,
580 const struct lookup_table *table)
583 return inode_unnamed_lte_resolved(inode);
585 return inode_unnamed_lte_unresolved(inode, table);