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"
32 struct lookup_table *new_lookup_table(size_t capacity)
34 struct lookup_table *table;
35 struct hlist_head *array;
37 table = MALLOC(sizeof(struct lookup_table));
40 array = CALLOC(capacity, sizeof(array[0]));
45 table->num_entries = 0;
46 table->capacity = capacity;
50 ERROR("Failed to allocate memory for lookup table with capacity %zu",
55 struct lookup_table_entry *new_lookup_table_entry()
57 struct lookup_table_entry *lte;
59 lte = CALLOC(1, sizeof(struct lookup_table_entry));
61 ERROR("Out of memory (tried to allocate %zu bytes for "
62 "lookup table entry)",
63 sizeof(struct lookup_table_entry));
69 INIT_LIST_HEAD(<e->lte_group_list);
73 void free_lookup_table_entry(struct lookup_table_entry *lte)
77 if (lte->staging_list.next)
78 list_del(<e->staging_list);
80 switch (lte->resource_location) {
81 case RESOURCE_IN_STAGING_FILE:
82 case RESOURCE_IN_ATTACHED_BUFFER:
83 case RESOURCE_IN_FILE_ON_DISK:
84 wimlib_assert(((void*)<e->file_on_disk ==
85 (void*)<e->staging_file_name)
86 && ((void*)<e->file_on_disk ==
87 (void*)<e->attached_buffer));
88 FREE(lte->file_on_disk);
91 case RESOURCE_IN_NTFS_VOLUME:
93 FREE(lte->ntfs_loc->path_utf8);
94 FREE(lte->ntfs_loc->stream_name_utf16);
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 DEBUG("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++;
146 /* Decrements the reference count for the lookup table entry @lte. If its
147 * reference count reaches 0, it is unlinked from the lookup table. If,
148 * furthermore, the entry has no opened file descriptors associated with it, the
150 struct lookup_table_entry *
151 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
154 wimlib_assert(lte->refcnt);
155 if (--lte->refcnt == 0) {
156 lookup_table_unlink(table, lte);
158 if (lte->num_opened_fds == 0)
161 free_lookup_table_entry(lte);
170 * Calls a function on all the entries in the lookup table. Stop early and
171 * return nonzero if any call to the function returns nonzero.
173 int for_lookup_table_entry(struct lookup_table *table,
174 int (*visitor)(struct lookup_table_entry *, void *),
177 struct lookup_table_entry *lte;
178 struct hlist_node *pos, *tmp;
181 for (size_t i = 0; i < table->capacity; i++) {
182 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
185 ret = visitor(lte, arg);
195 * Reads the lookup table from a WIM file.
197 int read_lookup_table(WIMStruct *w)
200 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
202 struct lookup_table *table;
204 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
205 w->hdr.lookup_table_res_entry.offset,
206 w->hdr.lookup_table_res_entry.original_size);
208 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
209 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
211 w->hdr.lookup_table_res_entry.offset);
212 return WIMLIB_ERR_READ;
215 num_entries = w->hdr.lookup_table_res_entry.original_size /
216 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
217 table = new_lookup_table(num_entries * 2 + 1);
219 return WIMLIB_ERR_NOMEM;
221 while (num_entries--) {
223 struct lookup_table_entry *cur_entry, *duplicate_entry;
225 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
227 ERROR("Unexpected EOF in WIM lookup table!");
229 ERROR_WITH_ERRNO("Error reading WIM lookup "
232 ret = WIMLIB_ERR_READ;
235 cur_entry = new_lookup_table_entry();
237 ret = WIMLIB_ERR_NOMEM;
241 cur_entry->resource_location = RESOURCE_IN_WIM;
243 p = get_resource_entry(buf, &cur_entry->resource_entry);
244 p = get_u16(p, &cur_entry->part_number);
245 p = get_u32(p, &cur_entry->refcnt);
246 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
248 if (is_zero_hash(cur_entry->hash)) {
249 ERROR("The WIM lookup table contains an entry with a "
250 "SHA1 message digest of all 0's");
251 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
256 duplicate_entry = __lookup_resource(table, cur_entry->hash);
257 if (duplicate_entry) {
258 ERROR("The WIM lookup table contains two entries with the "
259 "same SHA1 message digest!");
260 ERROR("The first entry is:");
261 print_lookup_table_entry(duplicate_entry);
262 ERROR("The second entry is:");
263 print_lookup_table_entry(cur_entry);
264 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
268 lookup_table_insert(table, cur_entry);
270 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
271 && (cur_entry->resource_entry.size !=
272 cur_entry->resource_entry.original_size))
274 ERROR("Found uncompressed resource with original size "
275 "not the same as compressed size");
276 ERROR("The lookup table entry for the resource is as follows:");
277 print_lookup_table_entry(cur_entry);
278 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
282 DEBUG("Done reading lookup table.");
283 w->lookup_table = table;
286 free_lookup_table(table);
292 * Writes a lookup table entry to the output file.
294 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
297 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
302 /* Don't write entries that have not had file resources or metadata
303 * resources written for them. */
304 if (lte->out_refcnt == 0)
307 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
308 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
309 ftello(out), lte->output_resource_entry.original_size);
311 p = put_resource_entry(buf, <e->output_resource_entry);
312 p = put_u16(p, lte->part_number);
313 p = put_u32(p, lte->out_refcnt);
314 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
315 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
316 ERROR_WITH_ERRNO("Failed to write lookup table entry");
317 return WIMLIB_ERR_WRITE;
324 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
326 entry->out_refcnt = 0;
330 void print_lookup_table_entry(const struct lookup_table_entry *lte)
336 printf("Offset = %"PRIu64" bytes\n",
337 lte->resource_entry.offset);
338 printf("Size = %"PRIu64" bytes\n",
339 (u64)lte->resource_entry.size);
340 printf("Original size = %"PRIu64" bytes\n",
341 lte->resource_entry.original_size);
342 printf("Part Number = %hu\n", lte->part_number);
343 printf("Reference Count = %u\n", lte->refcnt);
345 print_hash(lte->hash);
348 u8 flags = lte->resource_entry.flags;
349 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
350 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
351 if (flags & WIM_RESHDR_FLAG_FREE)
352 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
353 if (flags & WIM_RESHDR_FLAG_METADATA)
354 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
355 if (flags & WIM_RESHDR_FLAG_SPANNED)
356 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
358 switch (lte->resource_location) {
359 case RESOURCE_IN_WIM:
360 if (lte->wim->filename) {
361 printf("WIM file = `%s'\n",
365 case RESOURCE_IN_FILE_ON_DISK:
366 printf("File on Disk = `%s'\n", lte->file_on_disk);
368 case RESOURCE_IN_STAGING_FILE:
369 printf("Staging File = `%s'\n", lte->staging_file_name);
377 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
380 print_lookup_table_entry(lte);
385 * Prints the lookup table of a WIM file.
387 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
389 for_lookup_table_entry(w->lookup_table,
390 do_print_lookup_table_entry,
395 * Looks up an entry in the lookup table.
397 struct lookup_table_entry *
398 __lookup_resource(const struct lookup_table *table, const u8 hash[])
401 struct lookup_table_entry *lte;
402 struct hlist_node *pos;
404 i = *(size_t*)hash % table->capacity;
405 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
406 if (hashes_equal(hash, lte->hash))
412 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
415 * This is only for pre-resolved dentries.
417 int lookup_resource(WIMStruct *w, const char *path,
419 struct dentry **dentry_ret,
420 struct lookup_table_entry **lte_ret,
421 unsigned *stream_idx_ret)
423 struct dentry *dentry;
424 struct lookup_table_entry *lte;
426 const char *stream_name = NULL;
429 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
430 stream_name = path_stream_name(path);
432 p = (char*)stream_name - 1;
437 dentry = get_dentry(w, path);
443 wimlib_assert(dentry->resolved);
446 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
447 && dentry_is_directory(dentry))
451 size_t stream_name_len = strlen(stream_name);
452 for (u16 i = 0; i < dentry->num_ads; i++) {
453 if (ads_entry_has_name(&dentry->ads_entries[i],
458 lte = dentry->ads_entries[i].lte;
466 *dentry_ret = dentry;
470 *stream_idx_ret = stream_idx;
474 /* Resolve a dentry's lookup table entries
476 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
477 * lookup table) with pointers directly to the lookup table entries. A circular
478 * linked list of streams sharing the same lookup table entry is created.
480 * This function always succeeds; unresolved lookup table entries are given a
483 int dentry_resolve_ltes(struct dentry *dentry, void *__table)
485 struct lookup_table *table = __table;
486 struct lookup_table_entry *lte;
488 if (dentry->resolved)
491 /* Resolve the default file stream */
492 lte = __lookup_resource(table, dentry->hash);
494 list_add(&dentry->lte_group_list.list, <e->lte_group_list);
496 INIT_LIST_HEAD(&dentry->lte_group_list.list);
498 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
499 dentry->resolved = true;
501 /* Resolve the alternate data streams */
502 if (dentry->ads_entries_status != ADS_ENTRIES_USER) {
503 for (u16 i = 0; i < dentry->num_ads; i++) {
504 struct ads_entry *cur_entry = &dentry->ads_entries[i];
506 lte = __lookup_resource(table, cur_entry->hash);
508 list_add(&cur_entry->lte_group_list.list,
509 <e->lte_group_list);
511 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
512 cur_entry->lte = lte;
513 cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
519 /* Return the lookup table entry for the unnamed data stream of a dentry, or
520 * NULL if there is none.
522 * You'd think this would be easier than it actually is, since the unnamed data
523 * stream should be the one referenced from the dentry itself. Alas, if there
524 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
525 * data stream in one of the alternate data streams instead of inside the
526 * dentry. So we need to check the alternate data streams too.
528 * Also, note that a dentry may appear to have than one unnamed stream, but if
529 * the SHA1 message digest is all 0's then the corresponding stream does not
530 * really "count" (this is the case for the dentry's own file stream when the
531 * file stream that should be there is actually in one of the alternate stream
532 * entries.). This is despite the fact that we may need to extract such a
533 * missing entry as an empty file or empty named data stream.
535 struct lookup_table_entry *
536 dentry_unnamed_lte(const struct dentry *dentry,
537 const struct lookup_table *table)
539 if (dentry->resolved)
540 return dentry_unnamed_lte_resolved(dentry);
542 return dentry_unnamed_lte_unresolved(dentry, table);