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)
79 switch (lte->resource_location) {
80 case RESOURCE_IN_STAGING_FILE:
81 case RESOURCE_IN_ATTACHED_BUFFER:
82 case RESOURCE_IN_FILE_ON_DISK:
83 wimlib_assert(((void*)<e->file_on_disk ==
84 (void*)<e->staging_file_name)
85 && ((void*)<e->file_on_disk ==
86 (void*)<e->attached_buffer));
87 FREE(lte->file_on_disk);
90 case RESOURCE_IN_NTFS_VOLUME:
92 FREE(lte->ntfs_loc->path_utf8);
93 FREE(lte->ntfs_loc->stream_name_utf16);
101 FREE(lte->extracted_file);
106 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
109 free_lookup_table_entry(entry);
114 void free_lookup_table(struct lookup_table *table)
116 DEBUG2("Freeing lookup table");
119 for_lookup_table_entry(table,
120 do_free_lookup_table_entry,
129 * Inserts an entry into the lookup table.
131 * @table: A pointer to the lookup table.
132 * @entry: A pointer to the entry to insert.
134 void lookup_table_insert(struct lookup_table *table,
135 struct lookup_table_entry *lte)
137 size_t i = lte->hash_short % table->capacity;
138 hlist_add_head(<e->hash_list, &table->array[i]);
140 /* XXX Make the table grow when too many entries have been inserted. */
141 table->num_entries++;
144 static void finalize_lte(struct lookup_table_entry *lte)
147 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
148 unlink(lte->staging_file_name);
149 wimlib_assert(lte->staging_list.next);
150 wimlib_assert(lte->staging_list.prev);
151 list_del(<e->staging_list);
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 void lte_decrement_refcnt(struct lookup_table_entry *lte,
162 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)
176 void lte_decrement_num_opened_fds(struct lookup_table_entry *lte,
177 struct lookup_table *table)
180 wimlib_assert(lte->num_opened_fds);
181 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
187 * Calls a function on all the entries in the lookup table. Stop early and
188 * return nonzero if any call to the function returns nonzero.
190 int for_lookup_table_entry(struct lookup_table *table,
191 int (*visitor)(struct lookup_table_entry *, void *),
194 struct lookup_table_entry *lte;
195 struct hlist_node *pos, *tmp;
198 for (size_t i = 0; i < table->capacity; i++) {
199 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
202 ret = visitor(lte, arg);
212 * Reads the lookup table from a WIM file.
214 int read_lookup_table(WIMStruct *w)
217 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
219 struct lookup_table *table;
220 struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
222 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
223 w->hdr.lookup_table_res_entry.offset,
224 w->hdr.lookup_table_res_entry.original_size);
226 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
227 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
229 w->hdr.lookup_table_res_entry.offset);
230 return WIMLIB_ERR_READ;
233 num_entries = w->hdr.lookup_table_res_entry.original_size /
234 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
235 table = new_lookup_table(num_entries * 2 + 1);
237 return WIMLIB_ERR_NOMEM;
239 while (num_entries--) {
242 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
244 ERROR("Unexpected EOF in WIM lookup table!");
246 ERROR_WITH_ERRNO("Error reading WIM lookup "
249 ret = WIMLIB_ERR_READ;
252 cur_entry = new_lookup_table_entry();
254 ret = WIMLIB_ERR_NOMEM;
258 cur_entry->resource_location = RESOURCE_IN_WIM;
260 p = get_resource_entry(buf, &cur_entry->resource_entry);
261 p = get_u16(p, &cur_entry->part_number);
262 p = get_u32(p, &cur_entry->refcnt);
263 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
265 if (cur_entry->part_number != w->hdr.part_number) {
266 ERROR("A lookup table entry in part %hu of the WIM "
267 "points to part %hu",
268 w->hdr.part_number, cur_entry->part_number);
269 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
270 goto out_free_cur_entry;
274 if (is_zero_hash(cur_entry->hash)) {
275 ERROR("The WIM lookup table contains an entry with a "
276 "SHA1 message digest of all 0's");
277 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
278 goto out_free_cur_entry;
281 duplicate_entry = __lookup_resource(table, cur_entry->hash);
282 if (duplicate_entry) {
283 ERROR("The WIM lookup table contains two entries with the "
284 "same SHA1 message digest!");
285 ERROR("The first entry is:");
286 print_lookup_table_entry(duplicate_entry);
287 ERROR("The second entry is:");
288 print_lookup_table_entry(cur_entry);
289 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
290 goto out_free_cur_entry;
293 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
294 && (cur_entry->resource_entry.size !=
295 cur_entry->resource_entry.original_size))
297 ERROR("Found uncompressed resource with original size "
298 "not the same as compressed size");
299 ERROR("The lookup table entry for the resource is as follows:");
300 print_lookup_table_entry(cur_entry);
301 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
302 goto out_free_cur_entry;
304 lookup_table_insert(table, cur_entry);
307 DEBUG("Done reading lookup table.");
308 w->lookup_table = table;
313 free_lookup_table(table);
319 * Writes a lookup table entry to the output file.
321 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
324 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
329 /* Don't write entries that have not had file resources or metadata
330 * resources written for them. */
331 if (lte->out_refcnt == 0)
334 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
335 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
336 ftello(out), lte->output_resource_entry.original_size);
338 p = put_resource_entry(buf, <e->output_resource_entry);
339 p = put_u16(p, lte->part_number);
340 p = put_u32(p, lte->out_refcnt);
341 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
342 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
343 ERROR_WITH_ERRNO("Failed to write lookup table entry");
344 return WIMLIB_ERR_WRITE;
350 int lte_zero_real_refcnt(struct lookup_table_entry *lte, void *ignore)
352 lte->real_refcnt = 0;
356 int lte_zero_out_refcnt(struct lookup_table_entry *lte, void *ignore)
362 int lte_free_extracted_file(struct lookup_table_entry *lte, void *ignone)
364 FREE(lte->extracted_file);
365 lte->extracted_file = NULL;
369 void print_lookup_table_entry(const struct lookup_table_entry *lte)
375 printf("Offset = %"PRIu64" bytes\n",
376 lte->resource_entry.offset);
377 printf("Size = %"PRIu64" bytes\n",
378 (u64)lte->resource_entry.size);
379 printf("Original size = %"PRIu64" bytes\n",
380 lte->resource_entry.original_size);
381 printf("Part Number = %hu\n", lte->part_number);
382 printf("Reference Count = %u\n", lte->refcnt);
384 print_hash(lte->hash);
387 u8 flags = lte->resource_entry.flags;
388 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
389 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
390 if (flags & WIM_RESHDR_FLAG_FREE)
391 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
392 if (flags & WIM_RESHDR_FLAG_METADATA)
393 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
394 if (flags & WIM_RESHDR_FLAG_SPANNED)
395 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
397 switch (lte->resource_location) {
398 case RESOURCE_IN_WIM:
399 if (lte->wim->filename) {
400 printf("WIM file = `%s'\n",
404 case RESOURCE_IN_FILE_ON_DISK:
405 printf("File on Disk = `%s'\n", lte->file_on_disk);
407 case RESOURCE_IN_STAGING_FILE:
408 printf("Staging File = `%s'\n", lte->staging_file_name);
416 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
419 print_lookup_table_entry(lte);
424 * Prints the lookup table of a WIM file.
426 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
428 for_lookup_table_entry(w->lookup_table,
429 do_print_lookup_table_entry,
434 * Looks up an entry in the lookup table.
436 struct lookup_table_entry *
437 __lookup_resource(const struct lookup_table *table, const u8 hash[])
440 struct lookup_table_entry *lte;
441 struct hlist_node *pos;
443 wimlib_assert(table);
445 i = *(size_t*)hash % table->capacity;
446 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
447 if (hashes_equal(hash, lte->hash))
454 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
457 * This is only for pre-resolved inodes.
459 int lookup_resource(WIMStruct *w, const char *path,
461 struct dentry **dentry_ret,
462 struct lookup_table_entry **lte_ret,
465 struct dentry *dentry;
466 struct lookup_table_entry *lte;
468 const char *stream_name = NULL;
472 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
473 stream_name = path_stream_name(path);
475 p = (char*)stream_name - 1;
480 dentry = get_dentry(w, path);
486 inode = dentry->d_inode;
488 wimlib_assert(inode->resolved);
490 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
491 && inode_is_directory(inode))
495 struct ads_entry *ads_entry;
497 ads_entry = inode_get_ads_entry(inode, stream_name,
500 stream_idx = ads_idx + 1;
501 lte = ads_entry->lte;
512 *dentry_ret = dentry;
516 *stream_idx_ret = stream_idx;
521 static void inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
523 struct lookup_table_entry *lte;
525 /* Resolve the default file stream */
526 lte = __lookup_resource(table, inode->hash);
528 inode->resolved = true;
530 /* Resolve the alternate data streams */
531 for (u16 i = 0; i < inode->num_ads; i++) {
532 struct ads_entry *cur_entry = &inode->ads_entries[i];
533 lte = __lookup_resource(table, cur_entry->hash);
534 cur_entry->lte = lte;
538 /* Resolve a dentry's lookup table entries
540 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
541 * lookup table) with pointers directly to the lookup table entries. A circular
542 * linked list of streams sharing the same lookup table entry is created.
544 * This function always succeeds; unresolved lookup table entries are given a
547 int dentry_resolve_ltes(struct dentry *dentry, void *table)
549 if (!dentry->d_inode->resolved)
550 inode_resolve_ltes(dentry->d_inode, table);
554 /* Return the lookup table entry for the unnamed data stream of an inode, or
555 * NULL if there is none.
557 * You'd think this would be easier than it actually is, since the unnamed data
558 * stream should be the one referenced from the inode itself. Alas, if there
559 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
560 * data stream in one of the alternate data streams instead of inside the WIM
561 * dentry itself. So we need to check the alternate data streams too.
563 * Also, note that a dentry may appear to have more than one unnamed stream, but
564 * if the SHA1 message digest is all 0's then the corresponding stream does not
565 * really "count" (this is the case for the inode's own file stream when the
566 * file stream that should be there is actually in one of the alternate stream
567 * entries.). This is despite the fact that we may need to extract such a
568 * missing entry as an empty file or empty named data stream.
570 struct lookup_table_entry *
571 inode_unnamed_lte(const struct inode *inode,
572 const struct lookup_table *table)
575 return inode_unnamed_lte_resolved(inode);
577 return inode_unnamed_lte_unresolved(inode, table);