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));
73 void free_lookup_table_entry(struct lookup_table_entry *lte)
76 if (lte->staging_list.next)
77 list_del(<e->staging_list);
78 FREE(lte->file_on_disk);
84 * Inserts an entry into the lookup table.
86 * @lookup_table: A pointer to the lookup table.
87 * @entry: A pointer to the entry to insert.
89 void lookup_table_insert(struct lookup_table *table,
90 struct lookup_table_entry *lte)
93 pos = lte->hash_short % table->capacity;
94 lte->next = table->array[pos];
95 table->array[pos] = lte;
96 /* XXX Make the table grow when too many entries have been inserted. */
101 /* Unlinks a lookup table entry from the table; does not free it. */
102 void lookup_table_unlink(struct lookup_table *table,
103 struct lookup_table_entry *lte)
106 struct lookup_table_entry *prev, *cur_entry, *next;
108 pos = lte->hash_short % table->capacity;
110 cur_entry = table->array[pos];
113 next = cur_entry->next;
114 if (cur_entry == lte) {
118 table->array[pos] = next;
119 table->num_entries--;
128 /* Decrement the reference count for the dentry having hash value @hash in the
129 * lookup table. The lookup table entry is unlinked and freed if there are no
130 * references to in remaining. */
131 struct lookup_table_entry *
132 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
134 size_t pos = *(size_t*)hash % table->capacity;
135 struct lookup_table_entry *prev = NULL;
136 struct lookup_table_entry *entry = table->array[pos];
137 struct lookup_table_entry *next;
140 if (memcmp(hash, entry->hash, WIM_HASH_SIZE) == 0) {
141 wimlib_assert(entry->refcnt != 0);
142 if (--entry->refcnt == 0) {
143 if (entry->num_opened_fds == 0) {
144 free_lookup_table_entry(entry);
150 table->array[pos] = next;
160 /* Like lookup_table_decrement_refcnt(), but for when we already know the lookup
162 struct lookup_table_entry *
163 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
165 wimlib_assert(lte->refcnt);
166 if (lte && --lte->refcnt == 0) {
167 lookup_table_unlink(table, lte);
168 if (lte->num_opened_fds == 0) {
169 free_lookup_table_entry(lte);
178 * Calls a function on all the entries in the lookup table. Stop early and
179 * return nonzero if any call to the function returns nonzero.
181 int for_lookup_table_entry(struct lookup_table *table,
182 int (*visitor)(struct lookup_table_entry *, void *),
185 struct lookup_table_entry *entry, *next;
189 for (i = 0; i < table->capacity; i++) {
190 entry = table->array[i];
193 ret = visitor(entry, arg);
204 * Reads the lookup table from a WIM file.
206 * @fp: The FILE* for the WIM file.
207 * @offset: The offset of the lookup table resource.
208 * @size: The size of the lookup table resource.
209 * @lookup_table_ret: A pointer to a struct lookup_table structure into which the
210 * lookup table will be returned.
211 * @return: True on success, false on failure.
213 int read_lookup_table(FILE *fp, u64 offset, u64 size,
214 struct lookup_table **table_ret)
217 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
219 struct lookup_table *table;
221 struct lookup_table_entry *cur_entry;
223 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
226 if (fseeko(fp, offset, SEEK_SET) != 0) {
227 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
228 "lookup table", offset);
229 return WIMLIB_ERR_READ;
232 num_entries = size / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
233 table = new_lookup_table(num_entries * 2 + 1);
235 return WIMLIB_ERR_NOMEM;
237 while (num_entries--) {
238 if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf)) {
240 ERROR("Unexpected EOF in WIM lookup table!");
242 ERROR_WITH_ERRNO("Error reading WIM lookup "
245 ret = WIMLIB_ERR_READ;
248 cur_entry = new_lookup_table_entry();
250 ret = WIMLIB_ERR_NOMEM;
254 p = get_resource_entry(buf, &cur_entry->resource_entry);
255 p = get_u16(p, &cur_entry->part_number);
256 p = get_u32(p, &cur_entry->refcnt);
257 p = get_bytes(p, WIM_HASH_SIZE, cur_entry->hash);
258 lookup_table_insert(table, cur_entry);
260 DEBUG("Done reading lookup table.");
264 free_lookup_table(table);
270 * Writes a lookup table entry to the output file.
272 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
275 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
280 /* do not write lookup table entries for empty files */
281 if (lte->output_resource_entry.original_size == 0)
284 /* Don't write entries that have not had file resources or metadata
285 * resources written for them. */
286 if (lte->out_refcnt == 0)
289 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
290 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
291 ftello(out), lte->output_resource_entry.original_size);
293 p = put_resource_entry(buf, <e->output_resource_entry);
294 p = put_u16(p, lte->part_number);
295 p = put_u32(p, lte->out_refcnt);
296 p = put_bytes(p, WIM_HASH_SIZE, lte->hash);
297 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
298 ERROR_WITH_ERRNO("Failed to write lookup table entry");
299 return WIMLIB_ERR_WRITE;
304 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
307 free_lookup_table_entry(entry);
311 void free_lookup_table(struct lookup_table *table)
316 for_lookup_table_entry(table, do_free_lookup_table_entry, NULL);
322 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
324 entry->out_refcnt = 0;
328 int print_lookup_table_entry(struct lookup_table_entry *entry, void *ignore)
330 printf("Offset = %"PRIu64" bytes\n",
331 entry->resource_entry.offset);
332 printf("Size = %"PRIu64" bytes\n",
333 (u64)entry->resource_entry.size);
334 printf("Original size = %"PRIu64" bytes\n",
335 entry->resource_entry.original_size);
336 printf("Part Number = %hu\n", entry->part_number);
337 printf("Reference Count = %u\n", entry->refcnt);
339 print_hash(entry->hash);
342 u8 flags = entry->resource_entry.flags;
343 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
344 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
345 if (flags & WIM_RESHDR_FLAG_FREE)
346 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
347 if (flags & WIM_RESHDR_FLAG_METADATA)
348 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
349 if (flags & WIM_RESHDR_FLAG_SPANNED)
350 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
352 if (entry->file_on_disk)
353 printf("File on Disk = `%s'\n", entry->file_on_disk);
359 * Prints the lookup table of a WIM file.
361 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
363 for_lookup_table_entry(w->lookup_table,
364 print_lookup_table_entry, NULL);
368 * Looks up an entry in the lookup table.
370 struct lookup_table_entry *
371 __lookup_resource(const struct lookup_table *lookup_table, const u8 hash[])
374 struct lookup_table_entry *lte;
376 pos = *(size_t*)hash % lookup_table->capacity;
377 lte = lookup_table->array[pos];
379 if (memcmp(hash, lte->hash, WIM_HASH_SIZE) == 0)
386 /* Only for resolved lte's */
387 int lookup_resource(WIMStruct *w, const char *path,
389 struct dentry **dentry_ret,
390 struct lookup_table_entry **lte_ret,
391 unsigned *stream_idx_ret)
393 struct dentry *dentry = get_dentry(w, path);
394 struct lookup_table_entry *lte;
395 unsigned stream_idx = 0;
399 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
400 && dentry_is_directory(dentry))
402 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
403 const char *stream_name = path_stream_name(path);
405 size_t stream_name_len = strlen(stream_name);
406 for (u16 i = 0; i < dentry->num_ads; i++) {
407 if (ads_entry_has_name(&dentry->ads_entries[i],
412 lte = dentry->ads_entries[i].lte;
421 *dentry_ret = dentry;
425 *stream_idx_ret = stream_idx;
429 static int lte_init_lte_group_list(struct lookup_table_entry *lte, void *ignore)
431 INIT_LIST_HEAD(<e->lte_group_list);
435 /* Resolve a dentry's lookup table entries */
436 static int dentry_resolve_ltes(struct dentry *dentry, void *__table)
438 struct lookup_table *table = __table;
439 struct lookup_table_entry *lte;
441 /* Default file stream */
442 lte = __lookup_resource(table, dentry->hash);
444 list_add(&dentry->lte_group_list.list, <e->lte_group_list);
446 INIT_LIST_HEAD(&dentry->lte_group_list.list);
447 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
450 /* Alternate data streams */
451 if (dentry->link_group_master_status != GROUP_SLAVE) {
452 for (u16 i = 0; i < dentry->num_ads; i++) {
453 struct ads_entry *cur_entry = &dentry->ads_entries[i];
455 lte = __lookup_resource(table, cur_entry->hash);
457 list_add(&cur_entry->lte_group_list.list,
458 <e->lte_group_list);
460 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
461 cur_entry->lte = lte;
462 cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
468 /* Resolve all the lookup table entries of a dentry tree */
469 void resolve_lookup_table_entries(struct dentry *root, struct lookup_table *table)
471 for_lookup_table_entry(table, lte_init_lte_group_list, NULL);
472 for_dentry_in_tree(root, dentry_resolve_ltes, table);