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)
80 if (lte->staging_list.next)
81 list_del(<e->staging_list);
83 switch (lte->resource_location) {
84 case RESOURCE_IN_STAGING_FILE:
85 case RESOURCE_IN_ATTACHED_BUFFER:
86 case RESOURCE_IN_FILE_ON_DISK:
87 wimlib_assert(((void*)<e->file_on_disk ==
88 (void*)<e->staging_file_name)
89 && ((void*)<e->file_on_disk ==
90 (void*)<e->attached_buffer));
91 FREE(lte->file_on_disk);
94 case RESOURCE_IN_NTFS_VOLUME:
96 FREE(lte->ntfs_loc->path_utf8);
97 FREE(lte->ntfs_loc->stream_name_utf16);
105 FREE(lte->extracted_file);
110 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
113 free_lookup_table_entry(entry);
118 void free_lookup_table(struct lookup_table *table)
120 DEBUG2("Freeing lookup table");
123 for_lookup_table_entry(table,
124 do_free_lookup_table_entry,
133 * Inserts an entry into the lookup table.
135 * @table: A pointer to the lookup table.
136 * @entry: A pointer to the entry to insert.
138 void lookup_table_insert(struct lookup_table *table,
139 struct lookup_table_entry *lte)
141 size_t i = lte->hash_short % table->capacity;
142 hlist_add_head(<e->hash_list, &table->array[i]);
144 /* XXX Make the table grow when too many entries have been inserted. */
145 table->num_entries++;
148 static void finalize_lte(struct lookup_table_entry *lte)
151 if (lte->resource_location == RESOURCE_IN_STAGING_FILE)
152 unlink(lte->staging_file_name);
154 free_lookup_table_entry(lte);
157 /* Decrements the reference count for the lookup table entry @lte. If its
158 * reference count reaches 0, it is unlinked from the lookup table. If,
159 * furthermore, the entry has no opened file descriptors associated with it, the
161 struct lookup_table_entry *
162 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
165 wimlib_assert(lte->refcnt);
166 if (--lte->refcnt == 0) {
167 lookup_table_unlink(table, lte);
169 if (lte->num_opened_fds == 0)
181 struct lookup_table_entry *
182 lte_decrement_num_opened_fds(struct lookup_table_entry *lte,
183 struct lookup_table *table)
186 wimlib_assert(lte->num_opened_fds);
187 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
188 lookup_table_unlink(table, lte);
198 * Calls a function on all the entries in the lookup table. Stop early and
199 * return nonzero if any call to the function returns nonzero.
201 int for_lookup_table_entry(struct lookup_table *table,
202 int (*visitor)(struct lookup_table_entry *, void *),
205 struct lookup_table_entry *lte;
206 struct hlist_node *pos, *tmp;
209 for (size_t i = 0; i < table->capacity; i++) {
210 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
213 ret = visitor(lte, arg);
223 * Reads the lookup table from a WIM file.
225 int read_lookup_table(WIMStruct *w)
228 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
230 struct lookup_table *table;
231 struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
233 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
234 w->hdr.lookup_table_res_entry.offset,
235 w->hdr.lookup_table_res_entry.original_size);
237 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
238 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
240 w->hdr.lookup_table_res_entry.offset);
241 return WIMLIB_ERR_READ;
244 num_entries = w->hdr.lookup_table_res_entry.original_size /
245 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
246 table = new_lookup_table(num_entries * 2 + 1);
248 return WIMLIB_ERR_NOMEM;
250 while (num_entries--) {
253 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
255 ERROR("Unexpected EOF in WIM lookup table!");
257 ERROR_WITH_ERRNO("Error reading WIM lookup "
260 ret = WIMLIB_ERR_READ;
263 cur_entry = new_lookup_table_entry();
265 ret = WIMLIB_ERR_NOMEM;
269 cur_entry->resource_location = RESOURCE_IN_WIM;
271 p = get_resource_entry(buf, &cur_entry->resource_entry);
272 p = get_u16(p, &cur_entry->part_number);
273 p = get_u32(p, &cur_entry->refcnt);
274 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
276 if (cur_entry->part_number != w->hdr.part_number) {
277 ERROR("A lookup table entry in part %hu of the WIM "
278 "points to part %hu",
279 w->hdr.part_number, cur_entry->part_number);
280 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
281 goto out_free_cur_entry;
285 if (is_zero_hash(cur_entry->hash)) {
286 ERROR("The WIM lookup table contains an entry with a "
287 "SHA1 message digest of all 0's");
288 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
289 goto out_free_cur_entry;
292 duplicate_entry = __lookup_resource(table, cur_entry->hash);
293 if (duplicate_entry) {
294 ERROR("The WIM lookup table contains two entries with the "
295 "same SHA1 message digest!");
296 ERROR("The first entry is:");
297 print_lookup_table_entry(duplicate_entry);
298 ERROR("The second entry is:");
299 print_lookup_table_entry(cur_entry);
300 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
301 goto out_free_cur_entry;
304 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
305 && (cur_entry->resource_entry.size !=
306 cur_entry->resource_entry.original_size))
308 ERROR("Found uncompressed resource with original size "
309 "not the same as compressed size");
310 ERROR("The lookup table entry for the resource is as follows:");
311 print_lookup_table_entry(cur_entry);
312 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
313 goto out_free_cur_entry;
315 lookup_table_insert(table, cur_entry);
318 DEBUG("Done reading lookup table.");
319 w->lookup_table = table;
324 free_lookup_table(table);
330 * Writes a lookup table entry to the output file.
332 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
335 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
340 /* Don't write entries that have not had file resources or metadata
341 * resources written for them. */
342 if (lte->out_refcnt == 0)
345 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
346 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
347 ftello(out), lte->output_resource_entry.original_size);
349 p = put_resource_entry(buf, <e->output_resource_entry);
350 p = put_u16(p, lte->part_number);
351 p = put_u32(p, lte->out_refcnt);
352 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
353 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
354 ERROR_WITH_ERRNO("Failed to write lookup table entry");
355 return WIMLIB_ERR_WRITE;
362 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
364 entry->out_refcnt = 0;
368 void print_lookup_table_entry(const struct lookup_table_entry *lte)
374 printf("Offset = %"PRIu64" bytes\n",
375 lte->resource_entry.offset);
376 printf("Size = %"PRIu64" bytes\n",
377 (u64)lte->resource_entry.size);
378 printf("Original size = %"PRIu64" bytes\n",
379 lte->resource_entry.original_size);
380 printf("Part Number = %hu\n", lte->part_number);
381 printf("Reference Count = %u\n", lte->refcnt);
383 print_hash(lte->hash);
386 u8 flags = lte->resource_entry.flags;
387 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
388 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
389 if (flags & WIM_RESHDR_FLAG_FREE)
390 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
391 if (flags & WIM_RESHDR_FLAG_METADATA)
392 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
393 if (flags & WIM_RESHDR_FLAG_SPANNED)
394 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
396 switch (lte->resource_location) {
397 case RESOURCE_IN_WIM:
398 if (lte->wim->filename) {
399 printf("WIM file = `%s'\n",
403 case RESOURCE_IN_FILE_ON_DISK:
404 printf("File on Disk = `%s'\n", lte->file_on_disk);
406 case RESOURCE_IN_STAGING_FILE:
407 printf("Staging File = `%s'\n", lte->staging_file_name);
415 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
418 print_lookup_table_entry(lte);
423 * Prints the lookup table of a WIM file.
425 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
427 for_lookup_table_entry(w->lookup_table,
428 do_print_lookup_table_entry,
433 * Looks up an entry in the lookup table.
435 struct lookup_table_entry *
436 __lookup_resource(const struct lookup_table *table, const u8 hash[])
439 struct lookup_table_entry *lte;
440 struct hlist_node *pos;
442 i = *(size_t*)hash % table->capacity;
443 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
444 if (hashes_equal(hash, lte->hash))
450 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
453 * This is only for pre-resolved inodes.
455 int lookup_resource(WIMStruct *w, const char *path,
457 struct dentry **dentry_ret,
458 struct lookup_table_entry **lte_ret,
461 struct dentry *dentry;
462 struct lookup_table_entry *lte;
464 const char *stream_name = NULL;
468 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
469 stream_name = path_stream_name(path);
471 p = (char*)stream_name - 1;
476 dentry = get_dentry(w, path);
482 inode = dentry->inode;
484 wimlib_assert(inode->resolved);
486 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
487 && inode_is_directory(inode))
491 struct ads_entry *ads_entry;
493 ads_entry = inode_get_ads_entry(inode, stream_name,
496 stream_idx = ads_idx + 1;
497 lte = ads_entry->lte;
508 *dentry_ret = dentry;
512 *stream_idx_ret = stream_idx;
516 static int inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
518 struct lookup_table_entry *lte;
520 /* Resolve the default file stream */
521 lte = __lookup_resource(table, inode->hash);
523 inode->resolved = true;
525 /* Resolve the alternate data streams */
526 for (u16 i = 0; i < inode->num_ads; i++) {
527 struct ads_entry *cur_entry = inode->ads_entries[i];
529 lte = __lookup_resource(table, cur_entry->hash);
530 cur_entry->lte = lte;
535 /* Resolve a dentry's lookup table entries
537 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
538 * lookup table) with pointers directly to the lookup table entries. A circular
539 * linked list of streams sharing the same lookup table entry is created.
541 * This function always succeeds; unresolved lookup table entries are given a
544 int dentry_resolve_ltes(struct dentry *dentry, void *table)
546 if (dentry->inode->resolved)
549 return inode_resolve_ltes(dentry->inode, table);
555 /* Return the lookup table entry for the unnamed data stream of a inode, or
556 * NULL if there is none.
558 * You'd think this would be easier than it actually is, since the unnamed data
559 * stream should be the one referenced from the inode itself. Alas, if there
560 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
561 * data stream in one of the alternate data streams instead of inside the
562 * inode. So we need to check the alternate data streams too.
564 * Also, note that a inode may appear to have than one unnamed stream, but if
565 * the SHA1 message digest is all 0's then the corresponding stream does not
566 * really "count" (this is the case for the inode's own file stream when the
567 * file stream that should be there is actually in one of the alternate stream
568 * entries.). This is despite the fact that we may need to extract such a
569 * missing entry as an empty file or empty named data stream.
571 struct lookup_table_entry *
572 inode_unnamed_lte(const struct inode *inode,
573 const struct lookup_table *table)
576 return inode_unnamed_lte_resolved(inode);
578 return inode_unnamed_lte_unresolved(inode, table);