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 General Public License as published by the Free
15 * Software Foundation; either version 3 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 General Public License for more
23 * You should have received a copy of the GNU 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)
77 if (lte->staging_list.next)
78 list_del(<e->staging_list);
80 switch (lte->resource_location) {
81 case RESOURCE_IN_STAGING_FILE:
82 case RESOURCE_IN_ATTACHED_BUFFER:
83 case RESOURCE_IN_FILE_ON_DISK:
84 wimlib_assert(((void*)<e->file_on_disk ==
85 (void*)<e->staging_file_name)
86 && ((void*)<e->file_on_disk ==
87 (void*)<e->attached_buffer));
88 FREE(lte->file_on_disk);
91 case RESOURCE_IN_NTFS_VOLUME:
93 FREE(lte->ntfs_loc->path_utf8);
94 FREE(lte->ntfs_loc->stream_name_utf16);
106 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
109 free_lookup_table_entry(entry);
114 void free_lookup_table(struct lookup_table *table)
116 DEBUG2("Freeing lookup table");
119 for_lookup_table_entry(table,
120 do_free_lookup_table_entry,
129 * Inserts an entry into the lookup table.
131 * @table: A pointer to the lookup table.
132 * @entry: A pointer to the entry to insert.
134 void lookup_table_insert(struct lookup_table *table,
135 struct lookup_table_entry *lte)
137 size_t i = lte->hash_short % table->capacity;
138 hlist_add_head(<e->hash_list, &table->array[i]);
140 /* XXX Make the table grow when too many entries have been inserted. */
141 table->num_entries++;
146 /* Decrements the reference count for the lookup table entry @lte. If its
147 * reference count reaches 0, it is unlinked from the lookup table. If,
148 * furthermore, the entry has no opened file descriptors associated with it, the
150 struct lookup_table_entry *
151 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
154 wimlib_assert(lte->refcnt);
155 if (--lte->refcnt == 0) {
156 lookup_table_unlink(table, lte);
158 if (lte->num_opened_fds == 0)
161 free_lookup_table_entry(lte);
170 * Calls a function on all the entries in the lookup table. Stop early and
171 * return nonzero if any call to the function returns nonzero.
173 int for_lookup_table_entry(struct lookup_table *table,
174 int (*visitor)(struct lookup_table_entry *, void *),
177 struct lookup_table_entry *lte;
178 struct hlist_node *pos, *tmp;
181 for (size_t i = 0; i < table->capacity; i++) {
182 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
185 ret = visitor(lte, arg);
195 * Reads the lookup table from a WIM file.
197 int read_lookup_table(WIMStruct *w)
200 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
202 struct lookup_table *table;
203 struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
205 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
206 w->hdr.lookup_table_res_entry.offset,
207 w->hdr.lookup_table_res_entry.original_size);
209 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
210 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
212 w->hdr.lookup_table_res_entry.offset);
213 return WIMLIB_ERR_READ;
216 num_entries = w->hdr.lookup_table_res_entry.original_size /
217 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
218 table = new_lookup_table(num_entries * 2 + 1);
220 return WIMLIB_ERR_NOMEM;
222 while (num_entries--) {
225 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
227 ERROR("Unexpected EOF in WIM lookup table!");
229 ERROR_WITH_ERRNO("Error reading WIM lookup "
232 ret = WIMLIB_ERR_READ;
235 cur_entry = new_lookup_table_entry();
237 ret = WIMLIB_ERR_NOMEM;
241 cur_entry->resource_location = RESOURCE_IN_WIM;
243 p = get_resource_entry(buf, &cur_entry->resource_entry);
244 p = get_u16(p, &cur_entry->part_number);
245 p = get_u32(p, &cur_entry->refcnt);
246 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
248 if (cur_entry->part_number != w->hdr.part_number) {
249 ERROR("A lookup table entry in part %hu of the WIM "
250 "points to part %hu",
251 w->hdr.part_number, cur_entry->part_number);
252 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
253 goto out_free_cur_entry;
257 if (is_zero_hash(cur_entry->hash)) {
258 ERROR("The WIM lookup table contains an entry with a "
259 "SHA1 message digest of all 0's");
260 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
261 goto out_free_cur_entry;
264 duplicate_entry = __lookup_resource(table, cur_entry->hash);
265 if (duplicate_entry) {
266 ERROR("The WIM lookup table contains two entries with the "
267 "same SHA1 message digest!");
268 ERROR("The first entry is:");
269 print_lookup_table_entry(duplicate_entry);
270 ERROR("The second entry is:");
271 print_lookup_table_entry(cur_entry);
272 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
273 goto out_free_cur_entry;
276 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
277 && (cur_entry->resource_entry.size !=
278 cur_entry->resource_entry.original_size))
280 ERROR("Found uncompressed resource with original size "
281 "not the same as compressed size");
282 ERROR("The lookup table entry for the resource is as follows:");
283 print_lookup_table_entry(cur_entry);
284 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
285 goto out_free_cur_entry;
287 lookup_table_insert(table, cur_entry);
290 DEBUG("Done reading lookup table.");
291 w->lookup_table = table;
296 free_lookup_table(table);
302 * Writes a lookup table entry to the output file.
304 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
307 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
312 /* Don't write entries that have not had file resources or metadata
313 * resources written for them. */
314 if (lte->out_refcnt == 0)
317 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
318 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
319 ftello(out), lte->output_resource_entry.original_size);
321 p = put_resource_entry(buf, <e->output_resource_entry);
322 p = put_u16(p, lte->part_number);
323 p = put_u32(p, lte->out_refcnt);
324 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
325 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
326 ERROR_WITH_ERRNO("Failed to write lookup table entry");
327 return WIMLIB_ERR_WRITE;
334 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
336 entry->out_refcnt = 0;
340 void print_lookup_table_entry(const struct lookup_table_entry *lte)
346 printf("Offset = %"PRIu64" bytes\n",
347 lte->resource_entry.offset);
348 printf("Size = %"PRIu64" bytes\n",
349 (u64)lte->resource_entry.size);
350 printf("Original size = %"PRIu64" bytes\n",
351 lte->resource_entry.original_size);
352 printf("Part Number = %hu\n", lte->part_number);
353 printf("Reference Count = %u\n", lte->refcnt);
355 print_hash(lte->hash);
358 u8 flags = lte->resource_entry.flags;
359 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
360 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
361 if (flags & WIM_RESHDR_FLAG_FREE)
362 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
363 if (flags & WIM_RESHDR_FLAG_METADATA)
364 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
365 if (flags & WIM_RESHDR_FLAG_SPANNED)
366 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
368 switch (lte->resource_location) {
369 case RESOURCE_IN_WIM:
370 if (lte->wim->filename) {
371 printf("WIM file = `%s'\n",
375 case RESOURCE_IN_FILE_ON_DISK:
376 printf("File on Disk = `%s'\n", lte->file_on_disk);
378 case RESOURCE_IN_STAGING_FILE:
379 printf("Staging File = `%s'\n", lte->staging_file_name);
387 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
390 print_lookup_table_entry(lte);
395 * Prints the lookup table of a WIM file.
397 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
399 for_lookup_table_entry(w->lookup_table,
400 do_print_lookup_table_entry,
405 * Looks up an entry in the lookup table.
407 struct lookup_table_entry *
408 __lookup_resource(const struct lookup_table *table, const u8 hash[])
411 struct lookup_table_entry *lte;
412 struct hlist_node *pos;
414 i = *(size_t*)hash % table->capacity;
415 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
416 if (hashes_equal(hash, lte->hash))
422 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
425 * This is only for pre-resolved inodes.
427 int lookup_resource(WIMStruct *w, const char *path,
429 struct dentry **dentry_ret,
430 struct lookup_table_entry **lte_ret,
431 unsigned *stream_idx_ret)
433 struct dentry *dentry;
434 struct lookup_table_entry *lte;
436 const char *stream_name = NULL;
440 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
441 stream_name = path_stream_name(path);
443 p = (char*)stream_name - 1;
448 dentry = get_dentry(w, path);
454 inode = dentry->inode;
456 wimlib_assert(inode->resolved);
458 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
459 && inode_is_directory(inode))
465 size_t stream_name_len = strlen(stream_name);
466 for (u16 i = 0; i < inode->num_ads; i++) {
467 if (ads_entry_has_name(inode->ads_entries[i],
472 lte = inode->ads_entries[i]->lte;
480 *dentry_ret = dentry;
484 *stream_idx_ret = stream_idx;
488 static int inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
490 struct lookup_table_entry *lte;
492 /* Resolve the default file stream */
493 lte = __lookup_resource(table, inode->hash);
495 list_add(&inode->lte_group_list.list, <e->lte_group_list);
497 INIT_LIST_HEAD(&inode->lte_group_list.list);
499 inode->lte_group_list.type = STREAM_TYPE_NORMAL;
500 inode->resolved = true;
502 /* Resolve the alternate data streams */
503 for (u16 i = 0; i < inode->num_ads; i++) {
504 struct ads_entry *cur_entry = inode->ads_entries[i];
506 lte = __lookup_resource(table, cur_entry->hash);
508 list_add(&cur_entry->lte_group_list.list,
509 <e->lte_group_list);
511 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
512 cur_entry->lte = lte;
513 cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
518 /* Resolve a dentry's lookup table entries
520 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
521 * lookup table) with pointers directly to the lookup table entries. A circular
522 * linked list of streams sharing the same lookup table entry is created.
524 * This function always succeeds; unresolved lookup table entries are given a
527 int dentry_resolve_ltes(struct dentry *dentry, void *table)
529 if (dentry->inode->resolved)
532 return inode_resolve_ltes(dentry->inode, table);
538 /* Return the lookup table entry for the unnamed data stream of a inode, or
539 * NULL if there is none.
541 * You'd think this would be easier than it actually is, since the unnamed data
542 * stream should be the one referenced from the inode itself. Alas, if there
543 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
544 * data stream in one of the alternate data streams instead of inside the
545 * inode. So we need to check the alternate data streams too.
547 * Also, note that a inode may appear to have than one unnamed stream, but if
548 * the SHA1 message digest is all 0's then the corresponding stream does not
549 * really "count" (this is the case for the inode's own file stream when the
550 * file stream that should be there is actually in one of the alternate stream
551 * entries.). This is despite the fact that we may need to extract such a
552 * missing entry as an empty file or empty named data stream.
554 struct lookup_table_entry *
555 inode_unnamed_lte(const struct inode *inode,
556 const struct lookup_table *table)
559 return inode_unnamed_lte_resolved(inode);
561 return inode_unnamed_lte_unresolved(inode, table);