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 lookup_table_entry **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);
74 void free_lookup_table_entry(struct lookup_table_entry *lte)
77 if (lte->staging_list.next)
78 list_del(<e->staging_list);
79 FREE(lte->file_on_disk);
85 * Inserts an entry into the lookup table.
87 * @lookup_table: A pointer to the lookup table.
88 * @entry: A pointer to the entry to insert.
90 void lookup_table_insert(struct lookup_table *table,
91 struct lookup_table_entry *lte)
94 pos = lte->hash_short % table->capacity;
95 lte->next = table->array[pos];
96 table->array[pos] = lte;
97 /* XXX Make the table grow when too many entries have been inserted. */
102 /* Unlinks a lookup table entry from the table; does not free it. */
103 void lookup_table_unlink(struct lookup_table *table,
104 struct lookup_table_entry *lte)
107 struct lookup_table_entry *prev, *cur_entry, *next;
109 pos = lte->hash_short % table->capacity;
111 cur_entry = table->array[pos];
114 next = cur_entry->next;
115 if (cur_entry == lte) {
119 table->array[pos] = next;
120 table->num_entries--;
129 /* Decrement the reference count for the dentry having hash value @hash in the
130 * lookup table. The lookup table entry is unlinked and freed if there are no
131 * references to in remaining. */
132 struct lookup_table_entry *
133 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
135 size_t pos = *(size_t*)hash % table->capacity;
136 struct lookup_table_entry *prev = NULL;
137 struct lookup_table_entry *entry = table->array[pos];
138 struct lookup_table_entry *next;
141 if (memcmp(hash, entry->hash, WIM_HASH_SIZE) == 0) {
142 wimlib_assert(entry->refcnt != 0);
143 if (--entry->refcnt == 0) {
144 if (entry->num_opened_fds == 0) {
145 free_lookup_table_entry(entry);
151 table->array[pos] = next;
161 /* Like lookup_table_decrement_refcnt(), but for when we already know the lookup
163 struct lookup_table_entry *
164 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
167 wimlib_assert(lte->refcnt);
168 if (--lte->refcnt == 0) {
169 lookup_table_unlink(table, lte);
170 if (lte->num_opened_fds == 0) {
171 free_lookup_table_entry(lte);
180 * Calls a function on all the entries in the lookup table. Stop early and
181 * return nonzero if any call to the function returns nonzero.
183 int for_lookup_table_entry(struct lookup_table *table,
184 int (*visitor)(struct lookup_table_entry *, void *),
187 struct lookup_table_entry *entry, *next;
191 for (i = 0; i < table->capacity; i++) {
192 entry = table->array[i];
195 ret = visitor(entry, arg);
206 * Reads the lookup table from a WIM file.
208 * @fp: The FILE* for the WIM file.
209 * @offset: The offset of the lookup table resource.
210 * @size: The size of the lookup table resource.
211 * @lookup_table_ret: A pointer to a struct lookup_table structure into which the
212 * lookup table will be returned.
213 * @return: True on success, false on failure.
215 int read_lookup_table(FILE *fp, u64 offset, u64 size,
216 struct lookup_table **table_ret)
219 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
221 struct lookup_table *table;
223 struct lookup_table_entry *cur_entry;
225 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
228 if (fseeko(fp, offset, SEEK_SET) != 0) {
229 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
230 "lookup table", offset);
231 return WIMLIB_ERR_READ;
234 num_entries = size / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
235 table = new_lookup_table(num_entries * 2 + 1);
237 return WIMLIB_ERR_NOMEM;
239 while (num_entries--) {
240 if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf)) {
242 ERROR("Unexpected EOF in WIM lookup table!");
244 ERROR_WITH_ERRNO("Error reading WIM lookup "
247 ret = WIMLIB_ERR_READ;
250 cur_entry = new_lookup_table_entry();
252 ret = WIMLIB_ERR_NOMEM;
256 p = get_resource_entry(buf, &cur_entry->resource_entry);
257 p = get_u16(p, &cur_entry->part_number);
258 p = get_u32(p, &cur_entry->refcnt);
259 p = get_bytes(p, WIM_HASH_SIZE, cur_entry->hash);
260 lookup_table_insert(table, cur_entry);
262 DEBUG("Done reading lookup table.");
266 free_lookup_table(table);
272 * Writes a lookup table entry to the output file.
274 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
277 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
282 /* do not write lookup table entries for empty files */
283 if (lte->output_resource_entry.original_size == 0)
286 /* Don't write entries that have not had file resources or metadata
287 * resources written for them. */
288 if (lte->out_refcnt == 0)
291 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
292 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
293 ftello(out), lte->output_resource_entry.original_size);
295 p = put_resource_entry(buf, <e->output_resource_entry);
296 p = put_u16(p, lte->part_number);
297 p = put_u32(p, lte->out_refcnt);
298 p = put_bytes(p, WIM_HASH_SIZE, lte->hash);
299 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
300 ERROR_WITH_ERRNO("Failed to write lookup table entry");
301 return WIMLIB_ERR_WRITE;
306 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
309 free_lookup_table_entry(entry);
313 void free_lookup_table(struct lookup_table *table)
318 for_lookup_table_entry(table, do_free_lookup_table_entry, NULL);
324 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
326 entry->out_refcnt = 0;
330 int print_lookup_table_entry(struct lookup_table_entry *entry, void *ignore)
332 printf("Offset = %"PRIu64" bytes\n",
333 entry->resource_entry.offset);
334 printf("Size = %"PRIu64" bytes\n",
335 (u64)entry->resource_entry.size);
336 printf("Original size = %"PRIu64" bytes\n",
337 entry->resource_entry.original_size);
338 printf("Part Number = %hu\n", entry->part_number);
339 printf("Reference Count = %u\n", entry->refcnt);
341 print_hash(entry->hash);
344 u8 flags = entry->resource_entry.flags;
345 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
346 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
347 if (flags & WIM_RESHDR_FLAG_FREE)
348 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
349 if (flags & WIM_RESHDR_FLAG_METADATA)
350 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
351 if (flags & WIM_RESHDR_FLAG_SPANNED)
352 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
354 if (entry->file_on_disk)
355 printf("File on Disk = `%s'\n", entry->file_on_disk);
361 * Prints the lookup table of a WIM file.
363 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
365 for_lookup_table_entry(w->lookup_table,
366 print_lookup_table_entry, NULL);
370 * Looks up an entry in the lookup table.
372 struct lookup_table_entry *
373 __lookup_resource(const struct lookup_table *lookup_table, const u8 hash[])
376 struct lookup_table_entry *lte;
378 pos = *(size_t*)hash % lookup_table->capacity;
379 lte = lookup_table->array[pos];
381 if (memcmp(hash, lte->hash, WIM_HASH_SIZE) == 0)
389 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
392 * This is only for pre-resolved dentries.
394 int lookup_resource(WIMStruct *w, const char *path,
396 struct dentry **dentry_ret,
397 struct lookup_table_entry **lte_ret,
398 unsigned *stream_idx_ret)
400 struct dentry *dentry;
401 struct lookup_table_entry *lte;
403 dentry = get_dentry(w, path);
407 wimlib_assert(dentry->resolved);
410 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
411 && dentry_is_directory(dentry))
414 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
415 const char *stream_name = path_stream_name(path);
417 size_t stream_name_len = strlen(stream_name);
418 for (u16 i = 0; i < dentry->num_ads; i++) {
419 if (ads_entry_has_name(&dentry->ads_entries[i],
424 lte = dentry->ads_entries[i].lte;
433 *dentry_ret = dentry;
437 *stream_idx_ret = stream_idx;
441 /* Resolve a dentry's lookup table entries
443 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
444 * lookup table) with pointers directly to the lookup table entries. A circular
445 * linked list of streams sharing the same lookup table entry is created.
447 * This function always succeeds; unresolved lookup table entries are given a
450 int dentry_resolve_ltes(struct dentry *dentry, void *__table)
452 struct lookup_table *table = __table;
453 struct lookup_table_entry *lte;
455 wimlib_assert(!dentry->resolved);
457 /* Resolve the default file stream */
458 lte = __lookup_resource(table, dentry->hash);
460 list_add(&dentry->lte_group_list.list, <e->lte_group_list);
462 INIT_LIST_HEAD(&dentry->lte_group_list.list);
464 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
465 dentry->resolved = true;
467 /* Resolve the alternate data streams */
468 if (dentry->ads_entries_status != ADS_ENTRIES_USER) {
469 for (u16 i = 0; i < dentry->num_ads; i++) {
470 struct ads_entry *cur_entry = &dentry->ads_entries[i];
472 lte = __lookup_resource(table, cur_entry->hash);
474 list_add(&cur_entry->lte_group_list.list,
475 <e->lte_group_list);
477 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
478 cur_entry->lte = lte;
479 cur_entry->lte_group_list.type = STREAM_TYPE_ADS;