4 * Lookup table, implemented as a hash table, that maps dentries to file
7 * Copyright (C) 2012 Eric Biggers
9 * wimlib - Library for working with WIM files
11 * This library is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or (at your option) any
16 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
18 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License along
21 * with this library; if not, write to the Free Software Foundation, Inc., 59
22 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wimlib_internal.h"
26 #include "lookup_table.h"
29 struct lookup_table *new_lookup_table(size_t capacity)
31 struct lookup_table *table;
32 struct lookup_table_entry **array;
34 table = CALLOC(1, sizeof(struct lookup_table));
37 array = CALLOC(capacity, sizeof(array[0]));
42 table->num_entries = 0;
43 table->capacity = capacity;
47 ERROR("Failed to allocate memory for lookup table with capacity "
52 struct lookup_table_entry *new_lookup_table_entry()
54 struct lookup_table_entry *lte;
56 lte = MALLOC(sizeof(struct lookup_table_entry));
58 ERROR("Out of memory (tried to allocate %zu bytes for "
59 "lookup table entry)\n",
60 sizeof(struct lookup_table_entry));
64 lte->file_on_disk = NULL;
65 lte->other_wim_fp = NULL;
69 lte->staging_num_times_opened = 0;
76 * Inserts an entry into the lookup table.
78 * @lookup_table: A pointer to the lookup table.
79 * @entry: A pointer to the entry to insert.
81 void lookup_table_insert(struct lookup_table *table,
82 struct lookup_table_entry *lte)
85 pos = *(size_t*)lte->hash % table->capacity;
86 lte->next = table->array[pos];
87 table->array[pos] = lte;
88 /* XXX Make the table grow when too many entries have been inserted. */
93 /* Unlinks a lookup table entry from the table; does not free it. */
94 void lookup_table_unlink(struct lookup_table *table,
95 struct lookup_table_entry *lte)
98 struct lookup_table_entry *prev, *cur_entry, *next;
100 pos = *(size_t*)lte->hash % table->capacity;
102 cur_entry = table->array[pos];
105 next = cur_entry->next;
106 if (cur_entry == lte) {
110 table->array[pos] = next;
111 table->num_entries--;
120 /* Decrement the reference count for the dentry having hash value @hash in the
121 * lookup table. The lookup table entry is unlinked and freed if there are no
122 * references to in remaining. */
123 void lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
125 size_t pos = *(size_t*)hash % table->capacity;
126 struct lookup_table_entry *prev = NULL;
127 struct lookup_table_entry *entry = table->array[pos];
128 struct lookup_table_entry *next;
131 if (memcmp(hash, entry->hash, WIM_HASH_SIZE) == 0) {
132 wimlib_assert(entry->refcnt != 0);
133 if (--entry->refcnt == 0) {
134 free_lookup_table_entry(entry);
138 table->array[pos] = next;
147 * Looks up an entry in the lookup table.
149 struct lookup_table_entry *lookup_resource(const struct lookup_table *lookup_table,
153 struct lookup_table_entry *lte;
155 pos = *(size_t*)hash % lookup_table->capacity;
156 lte = lookup_table->array[pos];
158 if (memcmp(hash, lte->hash, WIM_HASH_SIZE) == 0)
166 * Calls a function on all the entries in the lookup table. Stop early and
167 * return nonzero if any call to the function returns nonzero.
169 int for_lookup_table_entry(struct lookup_table *table,
170 int (*visitor)(struct lookup_table_entry *, void *), void *arg)
172 struct lookup_table_entry *entry, *next;
176 for (i = 0; i < table->capacity; i++) {
177 entry = table->array[i];
180 ret = visitor(entry, arg);
191 * Reads the lookup table from a WIM file.
193 * @fp: The FILE* for the WIM file.
194 * @offset: The offset of the lookup table resource.
195 * @size: The size of the lookup table resource.
196 * @lookup_table_ret: A pointer to a struct lookup_table structure into which the
197 * lookup table will be returned.
198 * @return: True on success, false on failure.
200 int read_lookup_table(FILE *fp, u64 offset, u64 size,
201 struct lookup_table **table_ret)
204 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
206 struct lookup_table *table;
208 struct lookup_table_entry *cur_entry;
210 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"\n",
213 if (fseeko(fp, offset, SEEK_SET) != 0) {
214 ERROR("Failed to seek to byte %"PRIu64" to read lookup table: "
216 return WIMLIB_ERR_READ;
219 num_entries = size / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
220 table = new_lookup_table(num_entries * 2 + 1);
222 return WIMLIB_ERR_NOMEM;
224 while (num_entries--) {
225 if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf)) {
227 ERROR("Unexpected EOF in lookup table!\n");
229 ERROR("Stream read error while reading "
232 ret = WIMLIB_ERR_READ;
235 cur_entry = new_lookup_table_entry();
237 ret = WIMLIB_ERR_NOMEM;
241 p = get_resource_entry(buf, &cur_entry->resource_entry);
242 p = get_u16(p, &cur_entry->part_number);
243 p = get_u32(p, &cur_entry->refcnt);
244 p = get_bytes(p, sizeof(cur_entry->hash), cur_entry->hash);
245 lookup_table_insert(table, cur_entry);
247 DEBUG("Done reading lookup table.\n");
251 free_lookup_table(table);
257 * Writes a lookup table entry to the output file.
259 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
262 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
267 /* do not write lookup table entries for empty files */
268 if (lte->output_resource_entry.original_size == 0)
271 /* Don't write entries that have not had file resources or metadata
272 * resources written for them. */
273 if (lte->out_refcnt == 0)
276 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
277 DEBUG("Writing metadata entry at %lu\n", ftello(out));
279 p = put_resource_entry(buf, <e->output_resource_entry);
280 p = put_u16(p, lte->part_number);
281 p = put_u32(p, lte->out_refcnt);
282 p = put_bytes(p, WIM_HASH_SIZE, lte->hash);
283 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
284 ERROR("Failed to write lookup table entry: %m\n");
285 return WIMLIB_ERR_WRITE;
290 static int do_free_lookup_table_entry(struct lookup_table_entry *entry, void *ignore)
292 free_lookup_table_entry(entry);
296 void free_lookup_table(struct lookup_table *table)
300 for_lookup_table_entry(table,
301 do_free_lookup_table_entry,
309 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
311 entry->out_refcnt = 0;
315 int print_lookup_table_entry(struct lookup_table_entry *entry, void *ignore)
317 printf("Offset = %"PRIu64" bytes\n",
318 entry->resource_entry.offset);
319 printf("Size = %"PRIu64" bytes\n",
320 (u64)entry->resource_entry.size);
321 printf("Original size = %"PRIu64" bytes\n",
322 entry->resource_entry.original_size);
323 printf("Part Number = %hu\n", entry->part_number);
324 printf("Reference Count = %u\n", entry->refcnt);
326 print_hash(entry->hash);
329 u8 flags = entry->resource_entry.flags;
330 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
331 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
332 if (flags & WIM_RESHDR_FLAG_FREE)
333 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
334 if (flags & WIM_RESHDR_FLAG_METADATA)
335 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
336 if (flags & WIM_RESHDR_FLAG_SPANNED)
337 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
339 if (entry->file_on_disk)
340 printf("File on Disk = \"%s\"\n", entry->file_on_disk);
346 * Prints the lookup table of a WIM file.
348 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
350 for_lookup_table_entry(w->lookup_table,
351 print_lookup_table_entry, NULL);