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 FREE(lte->file_on_disk);
83 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
86 free_lookup_table_entry(entry);
91 void free_lookup_table(struct lookup_table *table)
93 DEBUG("Freeing lookup table");
96 for_lookup_table_entry(table,
97 do_free_lookup_table_entry,
106 * Inserts an entry into the lookup table.
108 * @table: A pointer to the lookup table.
109 * @entry: A pointer to the entry to insert.
111 void lookup_table_insert(struct lookup_table *table,
112 struct lookup_table_entry *lte)
114 size_t i = lte->hash_short % table->capacity;
115 hlist_add_head(<e->hash_list, &table->array[i]);
117 /* XXX Make the table grow when too many entries have been inserted. */
118 table->num_entries++;
123 /* Decrements the reference count for the lookup table entry @lte. If its
124 * reference count reaches 0, it is unlinked from the lookup table. If,
125 * furthermore, the entry has no opened file descriptors associated with it, the
127 struct lookup_table_entry *
128 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
131 wimlib_assert(lte->refcnt);
132 if (--lte->refcnt == 0) {
133 lookup_table_unlink(table, lte);
134 if (lte->num_opened_fds == 0) {
135 free_lookup_table_entry(lte);
144 * Calls a function on all the entries in the lookup table. Stop early and
145 * return nonzero if any call to the function returns nonzero.
147 int for_lookup_table_entry(struct lookup_table *table,
148 int (*visitor)(struct lookup_table_entry *, void *),
151 struct lookup_table_entry *lte;
152 struct hlist_node *pos, *tmp;
155 for (size_t i = 0; i < table->capacity; i++) {
156 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
159 ret = visitor(lte, arg);
169 * Reads the lookup table from a WIM file.
171 * @fp: The FILE* for the WIM file.
172 * @offset: The offset of the lookup table resource.
173 * @size: The size of the lookup table resource.
174 * @lookup_table_ret: A pointer to a struct lookup_table structure into which the
175 * lookup table will be returned.
176 * @return: True on success, false on failure.
178 int read_lookup_table(FILE *fp, u64 offset, u64 size,
179 struct lookup_table **table_ret)
182 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
184 struct lookup_table *table;
186 struct lookup_table_entry *cur_entry;
188 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
191 if (fseeko(fp, offset, SEEK_SET) != 0) {
192 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
193 "lookup table", offset);
194 return WIMLIB_ERR_READ;
197 num_entries = size / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
198 table = new_lookup_table(num_entries * 2 + 1);
200 return WIMLIB_ERR_NOMEM;
202 while (num_entries--) {
203 if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf)) {
205 ERROR("Unexpected EOF in WIM lookup table!");
207 ERROR_WITH_ERRNO("Error reading WIM lookup "
210 ret = WIMLIB_ERR_READ;
213 cur_entry = new_lookup_table_entry();
215 ret = WIMLIB_ERR_NOMEM;
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);
223 lookup_table_insert(table, cur_entry);
225 DEBUG("Done reading lookup table.");
229 free_lookup_table(table);
235 * Writes a lookup table entry to the output file.
237 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
240 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
245 /* do not write lookup table entries for empty files */
246 if (lte->output_resource_entry.original_size == 0)
249 /* Don't write entries that have not had file resources or metadata
250 * resources written for them. */
251 if (lte->out_refcnt == 0)
254 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
255 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
256 ftello(out), lte->output_resource_entry.original_size);
258 p = put_resource_entry(buf, <e->output_resource_entry);
259 p = put_u16(p, lte->part_number);
260 p = put_u32(p, lte->out_refcnt);
261 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
262 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
263 ERROR_WITH_ERRNO("Failed to write lookup table entry");
264 return WIMLIB_ERR_WRITE;
271 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
273 entry->out_refcnt = 0;
277 void print_lookup_table_entry(struct lookup_table_entry *lte)
283 printf("Offset = %"PRIu64" bytes\n",
284 lte->resource_entry.offset);
285 printf("Size = %"PRIu64" bytes\n",
286 (u64)lte->resource_entry.size);
287 printf("Original size = %"PRIu64" bytes\n",
288 lte->resource_entry.original_size);
289 printf("Part Number = %hu\n", lte->part_number);
290 printf("Reference Count = %u\n", lte->refcnt);
292 print_hash(lte->hash);
295 u8 flags = lte->resource_entry.flags;
296 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
297 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
298 if (flags & WIM_RESHDR_FLAG_FREE)
299 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
300 if (flags & WIM_RESHDR_FLAG_METADATA)
301 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
302 if (flags & WIM_RESHDR_FLAG_SPANNED)
303 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
305 if (lte->file_on_disk && !lte->is_symlink)
306 printf("File on Disk = `%s'\n", lte->file_on_disk);
310 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
313 print_lookup_table_entry(lte);
318 * Prints the lookup table of a WIM file.
320 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
322 for_lookup_table_entry(w->lookup_table,
323 do_print_lookup_table_entry,
328 * Looks up an entry in the lookup table.
330 struct lookup_table_entry *
331 __lookup_resource(const struct lookup_table *table, const u8 hash[])
334 struct lookup_table_entry *lte;
335 struct hlist_node *pos;
337 i = *(size_t*)hash % table->capacity;
338 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
339 if (hashes_equal(hash, lte->hash))
345 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
348 * This is only for pre-resolved dentries.
350 int lookup_resource(WIMStruct *w, const char *path,
352 struct dentry **dentry_ret,
353 struct lookup_table_entry **lte_ret,
354 unsigned *stream_idx_ret)
356 struct dentry *dentry;
357 struct lookup_table_entry *lte;
359 dentry = get_dentry(w, path);
363 wimlib_assert(dentry->resolved);
366 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
367 && dentry_is_directory(dentry))
370 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
371 const char *stream_name = path_stream_name(path);
373 size_t stream_name_len = strlen(stream_name);
374 for (u16 i = 0; i < dentry->num_ads; i++) {
375 if (ads_entry_has_name(&dentry->ads_entries[i],
380 lte = dentry->ads_entries[i].lte;
389 *dentry_ret = dentry;
393 *stream_idx_ret = stream_idx;
397 /* Resolve a dentry's lookup table entries
399 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
400 * lookup table) with pointers directly to the lookup table entries. A circular
401 * linked list of streams sharing the same lookup table entry is created.
403 * This function always succeeds; unresolved lookup table entries are given a
406 int dentry_resolve_ltes(struct dentry *dentry, void *__table)
408 struct lookup_table *table = __table;
409 struct lookup_table_entry *lte;
411 if (dentry->resolved)
414 /* Resolve the default file stream */
415 lte = __lookup_resource(table, dentry->hash);
417 list_add(&dentry->lte_group_list.list, <e->lte_group_list);
419 INIT_LIST_HEAD(&dentry->lte_group_list.list);
421 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
422 dentry->resolved = true;
424 /* Resolve the alternate data streams */
425 if (dentry->ads_entries_status != ADS_ENTRIES_USER) {
426 for (u16 i = 0; i < dentry->num_ads; i++) {
427 struct ads_entry *cur_entry = &dentry->ads_entries[i];
429 lte = __lookup_resource(table, cur_entry->hash);
431 list_add(&cur_entry->lte_group_list.list,
432 <e->lte_group_list);
434 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
435 cur_entry->lte = lte;
436 cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
442 struct lookup_table_entry *
443 dentry_first_lte(const struct dentry *dentry, const struct lookup_table *table)
445 if (dentry->resolved)
446 return dentry_first_lte_resolved(dentry);
448 return dentry_first_lte_unresolved(dentry, table);