X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Flookup_table.c;h=f0359961604fe9e268e01dc4ec477bd13e744da3;hb=d364e4a9e3a18a144b35c47ce213e09087978d1b;hp=f320e2dde128fa58d932a41f8cd96f276019eadb;hpb=b61e9afca45dacd9baefbcfa17dbea2af75259b8;p=wimlib diff --git a/src/lookup_table.c b/src/lookup_table.c index f320e2dd..f0359961 100644 --- a/src/lookup_table.c +++ b/src/lookup_table.c @@ -26,7 +26,7 @@ #include "wimlib_internal.h" #include "lookup_table.h" -#include "io.h" +#include "buffer_io.h" #include #ifdef WITH_FUSE @@ -39,21 +39,20 @@ struct lookup_table *new_lookup_table(size_t capacity) struct hlist_head *array; table = MALLOC(sizeof(struct lookup_table)); - if (!table) - goto err; - array = CALLOC(capacity, sizeof(array[0])); - if (!array) { - FREE(table); - goto err; + if (table) { + array = CALLOC(capacity, sizeof(array[0])); + if (array) { + table->num_entries = 0; + table->capacity = capacity; + table->array = array; + } else { + FREE(table); + table = NULL; + ERROR("Failed to allocate memory for lookup table with capacity %zu", + capacity); + } } - table->num_entries = 0; - table->capacity = capacity; - table->array = array; return table; -err: - ERROR("Failed to allocate memory for lookup table with capacity %zu", - capacity); - return NULL; } struct lookup_table_entry *new_lookup_table_entry() @@ -270,12 +269,18 @@ int for_lookup_table_entry(struct lookup_table *table, */ int read_lookup_table(WIMStruct *w) { - u64 num_entries; - u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE]; - int ret; + u64 num_entries; + u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE]; + int ret; struct lookup_table *table; struct lookup_table_entry *cur_entry = NULL, *duplicate_entry; + if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) { + ERROR("Didn't expect a compressed lookup table!"); + ERROR("Ask the author to implement support for this."); + return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE; + } + DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"", w->hdr.lookup_table_res_entry.offset, w->hdr.lookup_table_res_entry.original_size); @@ -365,6 +370,14 @@ int read_lookup_table(WIMStruct *w) ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY; goto out_free_cur_entry; } + if ((cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) + && cur_entry->refcnt != 1) + { + ERROR("Found metadata resource with refcnt != 1:"); + print_lookup_table_entry(cur_entry); + ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY; + goto out_free_cur_entry; + } lookup_table_insert(table, cur_entry); } @@ -410,6 +423,33 @@ int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out) return 0; } +/* Writes the lookup table to the output file. */ +int write_lookup_table(struct lookup_table *table, FILE *out, + struct resource_entry *out_res_entry) +{ + off_t start_offset, end_offset; + int ret; + + start_offset = ftello(out); + if (start_offset == -1) + return WIMLIB_ERR_WRITE; + + ret = for_lookup_table_entry(table, write_lookup_table_entry, out); + if (ret != 0) + return ret; + + end_offset = ftello(out); + if (end_offset == -1) + return WIMLIB_ERR_WRITE; + + out_res_entry->offset = start_offset; + out_res_entry->size = end_offset - start_offset; + out_res_entry->original_size = end_offset - start_offset; + out_res_entry->flags = WIM_RESHDR_FLAG_METADATA; + + return 0; +} + int lte_zero_real_refcnt(struct lookup_table_entry *lte, void *ignore) { @@ -591,7 +631,7 @@ out: } #endif -static void inode_resolve_ltes(struct inode *inode, struct lookup_table *table) +void inode_resolve_ltes(struct inode *inode, struct lookup_table *table) { struct lookup_table_entry *lte; @@ -600,7 +640,7 @@ static void inode_resolve_ltes(struct inode *inode, struct lookup_table *table) /* Resolve the default file stream */ lte = __lookup_resource(table, inode->hash); inode->lte = lte; - inode->resolved = true; + inode->resolved = 1; /* Resolve the alternate data streams */ for (u16 i = 0; i < inode->num_ads; i++) { @@ -625,7 +665,7 @@ static void inode_unresolve_ltes(struct inode *inode) else zero_out_hash(inode->ads_entries[i].hash); } - inode->resolved = false; + inode->resolved = 0; } /* Resolve a dentry's lookup table entries @@ -651,6 +691,24 @@ int dentry_unresolve_ltes(struct dentry *dentry, void *ignore) return 0; } +/* + * Returns the lookup table entry for stream @stream_idx of the inode, where + * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1 + * corresponds to an alternate data stream. + * + * This works for both resolved and un-resolved dentries. + */ +struct lookup_table_entry * +inode_stream_lte(const struct inode *inode, unsigned stream_idx, + const struct lookup_table *table) +{ + if (inode->resolved) + return inode_stream_lte_resolved(inode, stream_idx); + else + return inode_stream_lte_unresolved(inode, stream_idx, table); +} + + /* Return the lookup table entry for the unnamed data stream of an inode, or * NULL if there is none. * @@ -669,7 +727,7 @@ int dentry_unresolve_ltes(struct dentry *dentry, void *ignore) */ struct lookup_table_entry * inode_unnamed_lte(const struct inode *inode, - const struct lookup_table *table) + const struct lookup_table *table) { if (inode->resolved) return inode_unnamed_lte_resolved(inode); @@ -677,3 +735,16 @@ inode_unnamed_lte(const struct inode *inode, return inode_unnamed_lte_unresolved(inode, table); } +static int lte_add_stream_size(struct lookup_table_entry *lte, + void *total_bytes_p) +{ + *(u64*)total_bytes_p += lte->resource_entry.size; + return 0; +} + +u64 lookup_table_total_stream_size(struct lookup_table *table) +{ + u64 total_size = 0; + for_lookup_table_entry(table, lte_add_stream_size, &total_size); + return total_size; +}