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"
31 struct lookup_table *new_lookup_table(size_t capacity)
33 struct lookup_table *table;
34 struct lookup_table_entry **array;
36 table = MALLOC(sizeof(struct lookup_table));
39 array = CALLOC(capacity, sizeof(array[0]));
44 table->num_entries = 0;
45 table->capacity = capacity;
49 ERROR("Failed to allocate memory for lookup table with capacity %zu",
54 struct lookup_table_entry *new_lookup_table_entry()
56 struct lookup_table_entry *lte;
58 lte = MALLOC(sizeof(struct lookup_table_entry));
60 ERROR("Out of memory (tried to allocate %zu bytes for "
61 "lookup table entry)",
62 sizeof(struct lookup_table_entry));
67 lte->file_on_disk = NULL;
68 lte->other_wim_fp = NULL;
71 lte->staging_num_times_opened = 0;
78 * Inserts an entry into the lookup table.
80 * @lookup_table: A pointer to the lookup table.
81 * @entry: A pointer to the entry to insert.
83 void lookup_table_insert(struct lookup_table *table,
84 struct lookup_table_entry *lte)
87 pos = lte->hash_short % table->capacity;
88 lte->next = table->array[pos];
89 table->array[pos] = lte;
90 /* XXX Make the table grow when too many entries have been inserted. */
95 /* Unlinks a lookup table entry from the table; does not free it. */
96 void lookup_table_unlink(struct lookup_table *table,
97 struct lookup_table_entry *lte)
100 struct lookup_table_entry *prev, *cur_entry, *next;
102 pos = lte->hash_short % table->capacity;
104 cur_entry = table->array[pos];
107 next = cur_entry->next;
108 if (cur_entry == lte) {
112 table->array[pos] = next;
113 table->num_entries--;
122 /* Decrement the reference count for the dentry having hash value @hash in the
123 * lookup table. The lookup table entry is unlinked and freed if there are no
124 * references to in remaining. */
125 void lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
127 size_t pos = *(size_t*)hash % table->capacity;
128 struct lookup_table_entry *prev = NULL;
129 struct lookup_table_entry *entry = table->array[pos];
130 struct lookup_table_entry *next;
133 if (memcmp(hash, entry->hash, WIM_HASH_SIZE) == 0) {
134 wimlib_assert(entry->refcnt != 0);
135 if (--entry->refcnt == 0) {
136 free_lookup_table_entry(entry);
140 table->array[pos] = next;
149 * Looks up an entry in the lookup table.
151 struct lookup_table_entry *lookup_resource(const struct lookup_table *lookup_table,
155 struct lookup_table_entry *lte;
157 pos = *(size_t*)hash % lookup_table->capacity;
158 lte = lookup_table->array[pos];
160 if (memcmp(hash, lte->hash, WIM_HASH_SIZE) == 0)
168 * Calls a function on all the entries in the lookup table. Stop early and
169 * return nonzero if any call to the function returns nonzero.
171 int for_lookup_table_entry(struct lookup_table *table,
172 int (*visitor)(struct lookup_table_entry *, void *),
175 struct lookup_table_entry *entry, *next;
179 for (i = 0; i < table->capacity; i++) {
180 entry = table->array[i];
183 ret = visitor(entry, arg);
194 * Reads the lookup table from a WIM file.
196 * @fp: The FILE* for the WIM file.
197 * @offset: The offset of the lookup table resource.
198 * @size: The size of the lookup table resource.
199 * @lookup_table_ret: A pointer to a struct lookup_table structure into which the
200 * lookup table will be returned.
201 * @return: True on success, false on failure.
203 int read_lookup_table(FILE *fp, u64 offset, u64 size,
204 struct lookup_table **table_ret)
207 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
209 struct lookup_table *table;
211 struct lookup_table_entry *cur_entry;
213 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
216 if (fseeko(fp, offset, SEEK_SET) != 0) {
217 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
218 "lookup table", offset);
219 return WIMLIB_ERR_READ;
222 num_entries = size / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
223 table = new_lookup_table(num_entries * 2 + 1);
225 return WIMLIB_ERR_NOMEM;
227 while (num_entries--) {
228 if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf)) {
230 ERROR("Unexpected EOF in WIM lookup table!");
232 ERROR_WITH_ERRNO("Error reading WIM lookup "
235 ret = WIMLIB_ERR_READ;
238 cur_entry = new_lookup_table_entry();
240 ret = WIMLIB_ERR_NOMEM;
244 p = get_resource_entry(buf, &cur_entry->resource_entry);
245 p = get_u16(p, &cur_entry->part_number);
246 p = get_u32(p, &cur_entry->refcnt);
247 p = get_bytes(p, WIM_HASH_SIZE, cur_entry->hash);
248 lookup_table_insert(table, cur_entry);
250 DEBUG("Done reading lookup table.");
254 free_lookup_table(table);
260 * Writes a lookup table entry to the output file.
262 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
265 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
270 /* do not write lookup table entries for empty files */
271 if (lte->output_resource_entry.original_size == 0)
274 /* Don't write entries that have not had file resources or metadata
275 * resources written for them. */
276 if (lte->out_refcnt == 0)
279 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
280 DEBUG("Writing metadata entry at %lu", ftello(out));
282 p = put_resource_entry(buf, <e->output_resource_entry);
283 p = put_u16(p, lte->part_number);
284 p = put_u32(p, lte->out_refcnt);
285 p = put_bytes(p, WIM_HASH_SIZE, lte->hash);
286 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
287 ERROR_WITH_ERRNO("Failed to write lookup table entry");
288 return WIMLIB_ERR_WRITE;
293 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
296 free_lookup_table_entry(entry);
300 void free_lookup_table(struct lookup_table *table)
305 for_lookup_table_entry(table, do_free_lookup_table_entry, NULL);
311 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
313 entry->out_refcnt = 0;
317 int print_lookup_table_entry(struct lookup_table_entry *entry, void *ignore)
319 printf("Offset = %"PRIu64" bytes\n",
320 entry->resource_entry.offset);
321 printf("Size = %"PRIu64" bytes\n",
322 (u64)entry->resource_entry.size);
323 printf("Original size = %"PRIu64" bytes\n",
324 entry->resource_entry.original_size);
325 printf("Part Number = %hu\n", entry->part_number);
326 printf("Reference Count = %u\n", entry->refcnt);
328 print_hash(entry->hash);
331 u8 flags = entry->resource_entry.flags;
332 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
333 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
334 if (flags & WIM_RESHDR_FLAG_FREE)
335 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
336 if (flags & WIM_RESHDR_FLAG_METADATA)
337 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
338 if (flags & WIM_RESHDR_FLAG_SPANNED)
339 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
341 if (entry->file_on_disk)
342 printf("File on Disk = `%s'\n", entry->file_on_disk);
348 * Prints the lookup table of a WIM file.
350 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
352 for_lookup_table_entry(w->lookup_table,
353 print_lookup_table_entry, NULL);