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 Lesser General Public License as published by the Free
15 * Software Foundation; either version 2.1 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 Lesser General Public License for more
23 * You should have received a copy of the GNU Lesser 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)
76 if (lte->staging_list.next)
77 list_del(<e->staging_list);
78 if (lte->resource_location != RESOURCE_IN_WIM &&
79 lte->resource_location != RESOURCE_NONEXISTENT)
80 FREE(lte->file_on_disk);
85 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
88 free_lookup_table_entry(entry);
93 void free_lookup_table(struct lookup_table *table)
95 DEBUG("Freeing lookup table");
98 for_lookup_table_entry(table,
99 do_free_lookup_table_entry,
108 * Inserts an entry into the lookup table.
110 * @table: A pointer to the lookup table.
111 * @entry: A pointer to the entry to insert.
113 void lookup_table_insert(struct lookup_table *table,
114 struct lookup_table_entry *lte)
116 size_t i = lte->hash_short % table->capacity;
117 hlist_add_head(<e->hash_list, &table->array[i]);
119 /* XXX Make the table grow when too many entries have been inserted. */
120 table->num_entries++;
125 /* Decrements the reference count for the lookup table entry @lte. If its
126 * reference count reaches 0, it is unlinked from the lookup table. If,
127 * furthermore, the entry has no opened file descriptors associated with it, the
129 struct lookup_table_entry *
130 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
133 wimlib_assert(lte->refcnt);
134 if (--lte->refcnt == 0) {
135 lookup_table_unlink(table, lte);
136 if (lte->num_opened_fds == 0) {
137 free_lookup_table_entry(lte);
146 * Calls a function on all the entries in the lookup table. Stop early and
147 * return nonzero if any call to the function returns nonzero.
149 int for_lookup_table_entry(struct lookup_table *table,
150 int (*visitor)(struct lookup_table_entry *, void *),
153 struct lookup_table_entry *lte;
154 struct hlist_node *pos, *tmp;
157 for (size_t i = 0; i < table->capacity; i++) {
158 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
161 ret = visitor(lte, arg);
171 * Reads the lookup table from a WIM file.
173 int read_lookup_table(WIMStruct *w)
176 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
178 struct lookup_table *table;
180 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
181 w->hdr.lookup_table_res_entry.offset,
182 w->hdr.lookup_table_res_entry.original_size);
184 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
185 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
187 w->hdr.lookup_table_res_entry.offset);
188 return WIMLIB_ERR_READ;
191 num_entries = w->hdr.lookup_table_res_entry.original_size /
192 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
193 table = new_lookup_table(num_entries * 2 + 1);
195 return WIMLIB_ERR_NOMEM;
197 while (num_entries--) {
199 struct lookup_table_entry *cur_entry, *duplicate_entry;
201 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
203 ERROR("Unexpected EOF in WIM lookup table!");
205 ERROR_WITH_ERRNO("Error reading WIM lookup "
208 ret = WIMLIB_ERR_READ;
211 cur_entry = new_lookup_table_entry();
213 ret = WIMLIB_ERR_NOMEM;
217 cur_entry->resource_location = RESOURCE_IN_WIM;
219 p = get_resource_entry(buf, &cur_entry->resource_entry);
220 p = get_u16(p, &cur_entry->part_number);
221 p = get_u32(p, &cur_entry->refcnt);
222 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
224 duplicate_entry = __lookup_resource(table, cur_entry->hash);
225 if (duplicate_entry) {
226 ERROR("The WIM lookup table contains two entries with the "
227 "same SHA1 message digest!");
228 ERROR("The first entry is:");
229 print_lookup_table_entry(duplicate_entry);
230 ERROR("The second entry is:");
231 print_lookup_table_entry(cur_entry);
232 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
235 lookup_table_insert(table, cur_entry);
237 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
238 && (cur_entry->resource_entry.size !=
239 cur_entry->resource_entry.original_size))
241 ERROR("Found uncompressed resource with original size "
242 "not the same as compressed size");
243 ERROR("The lookup table entry for the resource is as follows:");
244 print_lookup_table_entry(cur_entry);
245 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
249 DEBUG("Done reading lookup table.");
250 w->lookup_table = table;
253 free_lookup_table(table);
259 * Writes a lookup table entry to the output file.
261 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
264 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
269 /* Don't write entries that have not had file resources or metadata
270 * resources written for them. */
271 if (lte->out_refcnt == 0)
274 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
275 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
276 ftello(out), lte->output_resource_entry.original_size);
278 p = put_resource_entry(buf, <e->output_resource_entry);
279 p = put_u16(p, lte->part_number);
280 p = put_u32(p, lte->out_refcnt);
281 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
282 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
283 ERROR_WITH_ERRNO("Failed to write lookup table entry");
284 return WIMLIB_ERR_WRITE;
291 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
293 entry->out_refcnt = 0;
297 void print_lookup_table_entry(const struct lookup_table_entry *lte)
303 printf("Offset = %"PRIu64" bytes\n",
304 lte->resource_entry.offset);
305 printf("Size = %"PRIu64" bytes\n",
306 (u64)lte->resource_entry.size);
307 printf("Original size = %"PRIu64" bytes\n",
308 lte->resource_entry.original_size);
309 printf("Part Number = %hu\n", lte->part_number);
310 printf("Reference Count = %u\n", lte->refcnt);
312 print_hash(lte->hash);
315 u8 flags = lte->resource_entry.flags;
316 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
317 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
318 if (flags & WIM_RESHDR_FLAG_FREE)
319 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
320 if (flags & WIM_RESHDR_FLAG_METADATA)
321 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
322 if (flags & WIM_RESHDR_FLAG_SPANNED)
323 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
325 switch (lte->resource_location) {
326 case RESOURCE_IN_WIM:
327 if (lte->wim->filename) {
328 printf("WIM file = `%s'\n",
332 case RESOURCE_IN_FILE_ON_DISK:
333 printf("File on Disk = `%s'\n", lte->file_on_disk);
335 case RESOURCE_IN_STAGING_FILE:
336 printf("Staging File = `%s'\n", lte->staging_file_name);
342 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
345 print_lookup_table_entry(lte);
350 * Prints the lookup table of a WIM file.
352 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
354 for_lookup_table_entry(w->lookup_table,
355 do_print_lookup_table_entry,
360 * Looks up an entry in the lookup table.
362 struct lookup_table_entry *
363 __lookup_resource(const struct lookup_table *table, const u8 hash[])
366 struct lookup_table_entry *lte;
367 struct hlist_node *pos;
369 i = *(size_t*)hash % table->capacity;
370 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
371 if (hashes_equal(hash, lte->hash))
377 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
380 * This is only for pre-resolved dentries.
382 int lookup_resource(WIMStruct *w, const char *path,
384 struct dentry **dentry_ret,
385 struct lookup_table_entry **lte_ret,
386 unsigned *stream_idx_ret)
388 struct dentry *dentry;
389 struct lookup_table_entry *lte;
391 const char *stream_name;
394 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
395 stream_name = path_stream_name(path);
397 p = (char*)stream_name - 1;
402 dentry = get_dentry(w, path);
408 wimlib_assert(dentry->resolved);
411 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
412 && dentry_is_directory(dentry))
416 size_t stream_name_len = strlen(stream_name);
417 for (u16 i = 0; i < dentry->num_ads; i++) {
418 if (ads_entry_has_name(&dentry->ads_entries[i],
423 lte = dentry->ads_entries[i].lte;
431 *dentry_ret = dentry;
435 *stream_idx_ret = stream_idx;
439 /* Resolve a dentry's lookup table entries
441 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
442 * lookup table) with pointers directly to the lookup table entries. A circular
443 * linked list of streams sharing the same lookup table entry is created.
445 * This function always succeeds; unresolved lookup table entries are given a
448 int dentry_resolve_ltes(struct dentry *dentry, void *__table)
450 struct lookup_table *table = __table;
451 struct lookup_table_entry *lte;
453 if (dentry->resolved)
456 /* Resolve the default file stream */
457 lte = __lookup_resource(table, dentry->hash);
459 list_add(&dentry->lte_group_list.list, <e->lte_group_list);
461 INIT_LIST_HEAD(&dentry->lte_group_list.list);
463 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
464 dentry->resolved = true;
466 /* Resolve the alternate data streams */
467 if (dentry->ads_entries_status != ADS_ENTRIES_USER) {
468 for (u16 i = 0; i < dentry->num_ads; i++) {
469 struct ads_entry *cur_entry = &dentry->ads_entries[i];
471 lte = __lookup_resource(table, cur_entry->hash);
473 list_add(&cur_entry->lte_group_list.list,
474 <e->lte_group_list);
476 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
477 cur_entry->lte = lte;
478 cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
484 struct lookup_table_entry *
485 dentry_first_lte(const struct dentry *dentry, const struct lookup_table *table)
487 if (dentry->resolved)
488 return dentry_first_lte_resolved(dentry);
490 return dentry_first_lte_unresolved(dentry, table);