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"
36 struct lookup_table *new_lookup_table(size_t capacity)
38 struct lookup_table *table;
39 struct hlist_head *array;
41 table = MALLOC(sizeof(struct lookup_table));
44 array = CALLOC(capacity, sizeof(array[0]));
49 table->num_entries = 0;
50 table->capacity = capacity;
54 ERROR("Failed to allocate memory for lookup table with capacity %zu",
59 struct lookup_table_entry *new_lookup_table_entry()
61 struct lookup_table_entry *lte;
63 lte = CALLOC(1, sizeof(struct lookup_table_entry));
65 ERROR("Out of memory (tried to allocate %zu bytes for "
66 "lookup table entry)",
67 sizeof(struct lookup_table_entry));
76 void free_lookup_table_entry(struct lookup_table_entry *lte)
79 switch (lte->resource_location) {
80 case RESOURCE_IN_STAGING_FILE:
81 case RESOURCE_IN_ATTACHED_BUFFER:
82 case RESOURCE_IN_FILE_ON_DISK:
83 wimlib_assert(((void*)<e->file_on_disk ==
84 (void*)<e->staging_file_name)
85 && ((void*)<e->file_on_disk ==
86 (void*)<e->attached_buffer));
87 FREE(lte->file_on_disk);
90 case RESOURCE_IN_NTFS_VOLUME:
92 FREE(lte->ntfs_loc->path_utf8);
93 FREE(lte->ntfs_loc->stream_name_utf16);
101 FREE(lte->extracted_file);
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++;
144 static void finalize_lte(struct lookup_table_entry *lte)
147 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
148 unlink(lte->staging_file_name);
149 wimlib_assert(lte->staging_list.next);
150 list_del(<e->staging_list);
153 free_lookup_table_entry(lte);
156 /* Decrements the reference count for the lookup table entry @lte. If its
157 * reference count reaches 0, it is unlinked from the lookup table. If,
158 * furthermore, the entry has no opened file descriptors associated with it, the
160 struct lookup_table_entry *
161 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
164 wimlib_assert(lte->refcnt);
165 if (--lte->refcnt == 0) {
166 lookup_table_unlink(table, lte);
168 if (lte->num_opened_fds == 0)
180 struct lookup_table_entry *
181 lte_decrement_num_opened_fds(struct lookup_table_entry *lte,
182 struct lookup_table *table)
185 wimlib_assert(lte->num_opened_fds);
186 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
187 lookup_table_unlink(table, lte);
197 * Calls a function on all the entries in the lookup table. Stop early and
198 * return nonzero if any call to the function returns nonzero.
200 int for_lookup_table_entry(struct lookup_table *table,
201 int (*visitor)(struct lookup_table_entry *, void *),
204 struct lookup_table_entry *lte;
205 struct hlist_node *pos, *tmp;
208 for (size_t i = 0; i < table->capacity; i++) {
209 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
212 ret = visitor(lte, arg);
222 * Reads the lookup table from a WIM file.
224 int read_lookup_table(WIMStruct *w)
227 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
229 struct lookup_table *table;
230 struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
232 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
233 w->hdr.lookup_table_res_entry.offset,
234 w->hdr.lookup_table_res_entry.original_size);
236 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
237 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
239 w->hdr.lookup_table_res_entry.offset);
240 return WIMLIB_ERR_READ;
243 num_entries = w->hdr.lookup_table_res_entry.original_size /
244 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
245 table = new_lookup_table(num_entries * 2 + 1);
247 return WIMLIB_ERR_NOMEM;
249 while (num_entries--) {
252 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
254 ERROR("Unexpected EOF in WIM lookup table!");
256 ERROR_WITH_ERRNO("Error reading WIM lookup "
259 ret = WIMLIB_ERR_READ;
262 cur_entry = new_lookup_table_entry();
264 ret = WIMLIB_ERR_NOMEM;
268 cur_entry->resource_location = RESOURCE_IN_WIM;
270 p = get_resource_entry(buf, &cur_entry->resource_entry);
271 p = get_u16(p, &cur_entry->part_number);
272 p = get_u32(p, &cur_entry->refcnt);
273 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
275 if (cur_entry->part_number != w->hdr.part_number) {
276 ERROR("A lookup table entry in part %hu of the WIM "
277 "points to part %hu",
278 w->hdr.part_number, cur_entry->part_number);
279 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
280 goto out_free_cur_entry;
284 if (is_zero_hash(cur_entry->hash)) {
285 ERROR("The WIM lookup table contains an entry with a "
286 "SHA1 message digest of all 0's");
287 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
288 goto out_free_cur_entry;
291 duplicate_entry = __lookup_resource(table, cur_entry->hash);
292 if (duplicate_entry) {
293 ERROR("The WIM lookup table contains two entries with the "
294 "same SHA1 message digest!");
295 ERROR("The first entry is:");
296 print_lookup_table_entry(duplicate_entry);
297 ERROR("The second entry is:");
298 print_lookup_table_entry(cur_entry);
299 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
300 goto out_free_cur_entry;
303 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
304 && (cur_entry->resource_entry.size !=
305 cur_entry->resource_entry.original_size))
307 ERROR("Found uncompressed resource with original size "
308 "not the same as compressed size");
309 ERROR("The lookup table entry for the resource is as follows:");
310 print_lookup_table_entry(cur_entry);
311 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
312 goto out_free_cur_entry;
314 lookup_table_insert(table, cur_entry);
317 DEBUG("Done reading lookup table.");
318 w->lookup_table = table;
323 free_lookup_table(table);
329 * Writes a lookup table entry to the output file.
331 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
334 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
339 /* Don't write entries that have not had file resources or metadata
340 * resources written for them. */
341 if (lte->out_refcnt == 0)
344 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
345 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
346 ftello(out), lte->output_resource_entry.original_size);
348 p = put_resource_entry(buf, <e->output_resource_entry);
349 p = put_u16(p, lte->part_number);
350 p = put_u32(p, lte->out_refcnt);
351 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
352 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
353 ERROR_WITH_ERRNO("Failed to write lookup table entry");
354 return WIMLIB_ERR_WRITE;
360 int lte_zero_real_refcnt(struct lookup_table_entry *lte, void *ignore)
362 lte->real_refcnt = 0;
366 int lte_zero_out_refcnt(struct lookup_table_entry *lte, void *ignore)
372 int lte_free_extracted_file(struct lookup_table_entry *lte, void *ignone)
374 FREE(lte->extracted_file);
375 lte->extracted_file = NULL;
379 void print_lookup_table_entry(const struct lookup_table_entry *lte)
385 printf("Offset = %"PRIu64" bytes\n",
386 lte->resource_entry.offset);
387 printf("Size = %"PRIu64" bytes\n",
388 (u64)lte->resource_entry.size);
389 printf("Original size = %"PRIu64" bytes\n",
390 lte->resource_entry.original_size);
391 printf("Part Number = %hu\n", lte->part_number);
392 printf("Reference Count = %u\n", lte->refcnt);
394 print_hash(lte->hash);
397 u8 flags = lte->resource_entry.flags;
398 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
399 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
400 if (flags & WIM_RESHDR_FLAG_FREE)
401 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
402 if (flags & WIM_RESHDR_FLAG_METADATA)
403 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
404 if (flags & WIM_RESHDR_FLAG_SPANNED)
405 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
407 switch (lte->resource_location) {
408 case RESOURCE_IN_WIM:
409 if (lte->wim->filename) {
410 printf("WIM file = `%s'\n",
414 case RESOURCE_IN_FILE_ON_DISK:
415 printf("File on Disk = `%s'\n", lte->file_on_disk);
417 case RESOURCE_IN_STAGING_FILE:
418 printf("Staging File = `%s'\n", lte->staging_file_name);
426 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
429 print_lookup_table_entry(lte);
434 * Prints the lookup table of a WIM file.
436 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
438 for_lookup_table_entry(w->lookup_table,
439 do_print_lookup_table_entry,
444 * Looks up an entry in the lookup table.
446 struct lookup_table_entry *
447 __lookup_resource(const struct lookup_table *table, const u8 hash[])
450 struct lookup_table_entry *lte;
451 struct hlist_node *pos;
453 i = *(size_t*)hash % table->capacity;
454 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
455 if (hashes_equal(hash, lte->hash))
462 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
465 * This is only for pre-resolved inodes.
467 int lookup_resource(WIMStruct *w, const char *path,
469 struct dentry **dentry_ret,
470 struct lookup_table_entry **lte_ret,
473 struct dentry *dentry;
474 struct lookup_table_entry *lte;
476 const char *stream_name = NULL;
480 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
481 stream_name = path_stream_name(path);
483 p = (char*)stream_name - 1;
488 dentry = get_dentry(w, path);
494 inode = dentry->inode;
496 wimlib_assert(inode->resolved);
498 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
499 && inode_is_directory(inode))
503 struct ads_entry *ads_entry;
505 ads_entry = inode_get_ads_entry(inode, stream_name,
508 stream_idx = ads_idx + 1;
509 lte = ads_entry->lte;
520 *dentry_ret = dentry;
524 *stream_idx_ret = stream_idx;
529 static void inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
531 struct lookup_table_entry *lte;
533 /* Resolve the default file stream */
534 lte = __lookup_resource(table, inode->hash);
536 inode->resolved = true;
538 /* Resolve the alternate data streams */
539 for (u16 i = 0; i < inode->num_ads; i++) {
540 struct ads_entry *cur_entry = &inode->ads_entries[i];
541 lte = __lookup_resource(table, cur_entry->hash);
542 cur_entry->lte = lte;
546 /* Resolve a dentry's lookup table entries
548 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
549 * lookup table) with pointers directly to the lookup table entries. A circular
550 * linked list of streams sharing the same lookup table entry is created.
552 * This function always succeeds; unresolved lookup table entries are given a
555 int dentry_resolve_ltes(struct dentry *dentry, void *table)
557 if (!dentry->inode->resolved)
558 inode_resolve_ltes(dentry->inode, table);
562 /* Return the lookup table entry for the unnamed data stream of an inode, or
563 * NULL if there is none.
565 * You'd think this would be easier than it actually is, since the unnamed data
566 * stream should be the one referenced from the inode itself. Alas, if there
567 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
568 * data stream in one of the alternate data streams instead of inside the WIM
569 * dentry itself. So we need to check the alternate data streams too.
571 * Also, note that a dentry may appear to have than one unnamed stream, but if
572 * the SHA1 message digest is all 0's then the corresponding stream does not
573 * really "count" (this is the case for the inode's own file stream when the
574 * file stream that should be there is actually in one of the alternate stream
575 * entries.). This is despite the fact that we may need to extract such a
576 * missing entry as an empty file or empty named data stream.
578 struct lookup_table_entry *
579 inode_unnamed_lte(const struct inode *inode,
580 const struct lookup_table *table)
583 return inode_unnamed_lte_resolved(inode);
585 return inode_unnamed_lte_unresolved(inode, table);