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(WIMStruct *wim)
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));
70 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);
84 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
87 free_lookup_table_entry(entry);
92 void free_lookup_table(struct lookup_table *table)
94 DEBUG("Freeing lookup table");
97 for_lookup_table_entry(table,
98 do_free_lookup_table_entry,
107 * Inserts an entry into the lookup table.
109 * @table: A pointer to the lookup table.
110 * @entry: A pointer to the entry to insert.
112 void lookup_table_insert(struct lookup_table *table,
113 struct lookup_table_entry *lte)
115 size_t i = lte->hash_short % table->capacity;
116 hlist_add_head(<e->hash_list, &table->array[i]);
118 /* XXX Make the table grow when too many entries have been inserted. */
119 table->num_entries++;
124 /* Decrements the reference count for the lookup table entry @lte. If its
125 * reference count reaches 0, it is unlinked from the lookup table. If,
126 * furthermore, the entry has no opened file descriptors associated with it, the
128 struct lookup_table_entry *
129 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
132 wimlib_assert(lte->refcnt);
133 if (--lte->refcnt == 0) {
134 lookup_table_unlink(table, lte);
135 if (lte->num_opened_fds == 0) {
136 free_lookup_table_entry(lte);
145 * Calls a function on all the entries in the lookup table. Stop early and
146 * return nonzero if any call to the function returns nonzero.
148 int for_lookup_table_entry(struct lookup_table *table,
149 int (*visitor)(struct lookup_table_entry *, void *),
152 struct lookup_table_entry *lte;
153 struct hlist_node *pos, *tmp;
156 for (size_t i = 0; i < table->capacity; i++) {
157 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
160 ret = visitor(lte, arg);
170 * Reads the lookup table from a WIM file.
172 int read_lookup_table(WIMStruct *w)
175 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
177 struct lookup_table *table;
179 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
180 w->hdr.lookup_table_res_entry.offset,
181 w->hdr.lookup_table_res_entry.original_size);
183 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
184 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
186 w->hdr.lookup_table_res_entry.offset);
187 return WIMLIB_ERR_READ;
190 num_entries = w->hdr.lookup_table_res_entry.original_size /
191 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
192 table = new_lookup_table(num_entries * 2 + 1);
194 return WIMLIB_ERR_NOMEM;
196 while (num_entries--) {
198 struct lookup_table_entry *cur_entry;
200 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
202 ERROR("Unexpected EOF in WIM lookup table!");
204 ERROR_WITH_ERRNO("Error reading WIM lookup "
207 ret = WIMLIB_ERR_READ;
210 cur_entry = new_lookup_table_entry(w);
212 ret = WIMLIB_ERR_NOMEM;
216 cur_entry->resource_location = RESOURCE_IN_WIM;
218 p = get_resource_entry(buf, &cur_entry->resource_entry);
219 p = get_u16(p, &cur_entry->part_number);
220 p = get_u32(p, &cur_entry->refcnt);
221 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
222 lookup_table_insert(table, cur_entry);
224 DEBUG("Done reading lookup table.");
225 w->lookup_table = table;
228 free_lookup_table(table);
234 * Writes a lookup table entry to the output file.
236 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
239 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
244 /* do not write lookup table entries for empty files */
245 if (lte->output_resource_entry.original_size == 0)
248 /* Don't write entries that have not had file resources or metadata
249 * resources written for them. */
250 if (lte->out_refcnt == 0)
253 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
254 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
255 ftello(out), lte->output_resource_entry.original_size);
257 p = put_resource_entry(buf, <e->output_resource_entry);
258 p = put_u16(p, lte->part_number);
259 p = put_u32(p, lte->out_refcnt);
260 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
261 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
262 ERROR_WITH_ERRNO("Failed to write lookup table entry");
263 return WIMLIB_ERR_WRITE;
270 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
272 entry->out_refcnt = 0;
276 void print_lookup_table_entry(struct lookup_table_entry *lte)
282 printf("Offset = %"PRIu64" bytes\n",
283 lte->resource_entry.offset);
284 printf("Size = %"PRIu64" bytes\n",
285 (u64)lte->resource_entry.size);
286 printf("Original size = %"PRIu64" bytes\n",
287 lte->resource_entry.original_size);
288 printf("Part Number = %hu\n", lte->part_number);
289 printf("Reference Count = %u\n", lte->refcnt);
291 print_hash(lte->hash);
294 u8 flags = lte->resource_entry.flags;
295 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
296 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
297 if (flags & WIM_RESHDR_FLAG_FREE)
298 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
299 if (flags & WIM_RESHDR_FLAG_METADATA)
300 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
301 if (flags & WIM_RESHDR_FLAG_SPANNED)
302 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
304 switch (lte->resource_location) {
305 case RESOURCE_IN_WIM:
306 if (lte->wim->filename) {
307 printf("WIM file = `%s'\n",
311 case RESOURCE_IN_FILE_ON_DISK:
312 printf("File on Disk = `%s'\n", lte->file_on_disk);
314 case RESOURCE_IN_STAGING_FILE:
315 printf("Staging File = `%s'\n", lte->staging_file_name);
321 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
324 print_lookup_table_entry(lte);
329 * Prints the lookup table of a WIM file.
331 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
333 for_lookup_table_entry(w->lookup_table,
334 do_print_lookup_table_entry,
339 * Looks up an entry in the lookup table.
341 struct lookup_table_entry *
342 __lookup_resource(const struct lookup_table *table, const u8 hash[])
345 struct lookup_table_entry *lte;
346 struct hlist_node *pos;
348 i = *(size_t*)hash % table->capacity;
349 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
350 if (hashes_equal(hash, lte->hash))
356 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
359 * This is only for pre-resolved dentries.
361 int lookup_resource(WIMStruct *w, const char *path,
363 struct dentry **dentry_ret,
364 struct lookup_table_entry **lte_ret,
365 unsigned *stream_idx_ret)
367 struct dentry *dentry;
368 struct lookup_table_entry *lte;
370 dentry = get_dentry(w, path);
374 wimlib_assert(dentry->resolved);
377 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
378 && dentry_is_directory(dentry))
381 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
382 const char *stream_name = path_stream_name(path);
384 size_t stream_name_len = strlen(stream_name);
385 for (u16 i = 0; i < dentry->num_ads; i++) {
386 if (ads_entry_has_name(&dentry->ads_entries[i],
391 lte = dentry->ads_entries[i].lte;
400 *dentry_ret = dentry;
404 *stream_idx_ret = stream_idx;
408 /* Resolve a dentry's lookup table entries
410 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
411 * lookup table) with pointers directly to the lookup table entries. A circular
412 * linked list of streams sharing the same lookup table entry is created.
414 * This function always succeeds; unresolved lookup table entries are given a
417 int dentry_resolve_ltes(struct dentry *dentry, void *__table)
419 struct lookup_table *table = __table;
420 struct lookup_table_entry *lte;
422 if (dentry->resolved)
425 /* Resolve the default file stream */
426 lte = __lookup_resource(table, dentry->hash);
428 list_add(&dentry->lte_group_list.list, <e->lte_group_list);
430 INIT_LIST_HEAD(&dentry->lte_group_list.list);
432 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
433 dentry->resolved = true;
435 /* Resolve the alternate data streams */
436 if (dentry->ads_entries_status != ADS_ENTRIES_USER) {
437 for (u16 i = 0; i < dentry->num_ads; i++) {
438 struct ads_entry *cur_entry = &dentry->ads_entries[i];
440 lte = __lookup_resource(table, cur_entry->hash);
442 list_add(&cur_entry->lte_group_list.list,
443 <e->lte_group_list);
445 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
446 cur_entry->lte = lte;
447 cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
453 struct lookup_table_entry *
454 dentry_first_lte(const struct dentry *dentry, const struct lookup_table *table)
456 if (dentry->resolved)
457 return dentry_first_lte_resolved(dentry);
459 return dentry_first_lte_unresolved(dentry, table);