-
-
- tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
- lte->size);
- if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
- tprintf(T("Offset = %"PRIu64" bytes\n"),
- lte->offset_in_res);
-
- tprintf(T("Raw uncompressed size = %"PRIu64" bytes\n"),
- lte->rspec->uncompressed_size);
-
- tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
- lte->rspec->size_in_wim);
-
- tprintf(T("Raw offset = %"PRIu64" bytes\n"),
- lte->rspec->offset_in_wim);
- } else if (lte->resource_location == RESOURCE_IN_WIM) {
- tprintf(T("Compressed size = %"PRIu64" bytes\n"),
- lte->rspec->size_in_wim);
-
- tprintf(T("Offset = %"PRIu64" bytes\n"),
- lte->rspec->offset_in_wim);
- }
-
- tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
-
- if (lte->unhashed) {
- tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
- lte->back_inode, lte->back_stream_id);
- } else {
- tfprintf(out, T("Hash = 0x"));
- print_hash(lte->hash, out);
- tputc(T('\n'), out);
- }
-
- tfprintf(out, T("Flags = "));
- u8 flags = lte->flags;
- if (flags & WIM_RESHDR_FLAG_COMPRESSED)
- tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
- if (flags & WIM_RESHDR_FLAG_FREE)
- tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
- if (flags & WIM_RESHDR_FLAG_METADATA)
- tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
- if (flags & WIM_RESHDR_FLAG_SPANNED)
- tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
- if (flags & WIM_RESHDR_FLAG_PACKED_STREAMS)
- tfputs(T("WIM_RESHDR_FLAG_PACKED_STREAMS, "), out);
- tputc(T('\n'), out);
- switch (lte->resource_location) {
- case RESOURCE_IN_WIM:
- if (lte->rspec->wim->filename) {
- tfprintf(out, T("WIM file = `%"TS"'\n"),
- lte->rspec->wim->filename);
- }
- break;
-#ifdef __WIN32__
- case RESOURCE_WIN32_ENCRYPTED:
-#endif
- case RESOURCE_IN_FILE_ON_DISK:
- tfprintf(out, T("File on Disk = `%"TS"'\n"),
- lte->file_on_disk);
- break;
-#ifdef WITH_FUSE
- case RESOURCE_IN_STAGING_FILE:
- tfprintf(out, T("Staging File = `%"TS"'\n"),
- lte->staging_file_name);
- break;
-#endif
- default:
- break;
- }
- tputc(T('\n'), out);
-}
-
-void
-lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
- struct wimlib_resource_entry *wentry)
-{
- memset(wentry, 0, sizeof(*wentry));
-
- wentry->uncompressed_size = lte->size;
- if (lte->resource_location == RESOURCE_IN_WIM) {
- wentry->part_number = lte->rspec->wim->hdr.part_number;
- if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
- wentry->compressed_size = 0;
- wentry->offset = lte->offset_in_res;
- } else {
- wentry->compressed_size = lte->rspec->size_in_wim;
- wentry->offset = lte->rspec->offset_in_wim;
- }
- wentry->raw_resource_offset_in_wim = lte->rspec->offset_in_wim;
- /*wentry->raw_resource_uncompressed_size = lte->rspec->uncompressed_size;*/
- wentry->raw_resource_compressed_size = lte->rspec->size_in_wim;
- }
- copy_hash(wentry->sha1_hash, lte->hash);
- wentry->reference_count = lte->refcnt;
- wentry->is_compressed = (lte->flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
- wentry->is_metadata = (lte->flags & WIM_RESHDR_FLAG_METADATA) != 0;
- wentry->is_free = (lte->flags & WIM_RESHDR_FLAG_FREE) != 0;
- wentry->is_spanned = (lte->flags & WIM_RESHDR_FLAG_SPANNED) != 0;
- wentry->packed = (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) != 0;
-}
-
-struct iterate_lte_context {
- wimlib_iterate_lookup_table_callback_t cb;
- void *user_ctx;
-};
-
-static int
-do_iterate_lte(struct wim_lookup_table_entry *lte, void *_ctx)
-{
- struct iterate_lte_context *ctx = _ctx;
- struct wimlib_resource_entry entry;
-
- lte_to_wimlib_resource_entry(lte, &entry);
- return (*ctx->cb)(&entry, ctx->user_ctx);
-}
-
-/* API function documented in wimlib.h */
-WIMLIBAPI int
-wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
- wimlib_iterate_lookup_table_callback_t cb,
- void *user_ctx)
-{
- struct iterate_lte_context ctx = {
- .cb = cb,
- .user_ctx = user_ctx,
- };
- if (wim->hdr.part_number == 1) {
- int ret;
- for (int i = 0; i < wim->hdr.image_count; i++) {
- ret = do_iterate_lte(wim->image_metadata[i]->metadata_lte,
- &ctx);
- if (ret)
- return ret;
- }
- }
- return for_lookup_table_entry(wim->lookup_table, do_iterate_lte, &ctx);
-}
-
-/* Given a SHA1 message digest, return the corresponding entry in the WIM's
- * lookup table, or NULL if there is none. */
-struct wim_lookup_table_entry *
-lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
-{
- size_t i;
- struct wim_lookup_table_entry *lte;
- struct hlist_node *pos;
-
- wimlib_assert(table != NULL);
- wimlib_assert(hash != NULL);
-
- i = *(size_t*)hash % table->capacity;
- hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
- if (hashes_equal(hash, lte->hash))
- return lte;
- return NULL;
-}
-
-#ifdef WITH_FUSE
-/*
- * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
- * given a path name.
- *
- * This is only for pre-resolved inodes.
- */
-int
-wim_pathname_to_stream(WIMStruct *wim,
- const tchar *path,
- int lookup_flags,
- struct wim_dentry **dentry_ret,
- struct wim_lookup_table_entry **lte_ret,
- u16 *stream_idx_ret)
-{
- struct wim_dentry *dentry;
- struct wim_lookup_table_entry *lte;
- u16 stream_idx;
- const tchar *stream_name = NULL;
- struct wim_inode *inode;
- tchar *p = NULL;
-
- if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
- stream_name = path_stream_name(path);
- if (stream_name) {
- p = (tchar*)stream_name - 1;
- *p = T('\0');
- }
- }
-
- dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
- if (p)
- *p = T(':');
- if (!dentry)
- return -errno;
-
- inode = dentry->d_inode;
-
- if (!inode->i_resolved)
- if (inode_resolve_ltes(inode, wim->lookup_table, false))
- return -EIO;
-
- if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
- && inode_is_directory(inode))
- return -EISDIR;
-
- if (stream_name) {
- struct wim_ads_entry *ads_entry;
- u16 ads_idx;
- ads_entry = inode_get_ads_entry(inode, stream_name,
- &ads_idx);
- if (ads_entry) {
- stream_idx = ads_idx + 1;
- lte = ads_entry->lte;
- goto out;
- } else {
- return -ENOENT;
- }
- } else {
- lte = inode_unnamed_stream_resolved(inode, &stream_idx);
- }
-out:
- if (dentry_ret)
- *dentry_ret = dentry;
- if (lte_ret)
- *lte_ret = lte;
- if (stream_idx_ret)
- *stream_idx_ret = stream_idx;
- return 0;
-}
-#endif
-
-int
-resource_not_found_error(const struct wim_inode *inode, const u8 *hash)
-{
- if (wimlib_print_errors) {
- ERROR("\"%"TS"\": resource not found", inode_first_full_path(inode));
- tfprintf(stderr, T(" SHA-1 message digest of missing resource:\n "));
- print_hash(hash, stderr);
- tputc(T('\n'), stderr);
- }
- return WIMLIB_ERR_RESOURCE_NOT_FOUND;
-}
-
-/*
- * Resolve an inode's lookup table entries.
- *
- * This replaces the SHA1 hash fields (which are used to lookup an entry in the
- * lookup table) with pointers directly to the lookup table entries.
- *
- * If @force is %false:
- * If any needed SHA1 message digests are not found in the lookup table,
- * WIMLIB_ERR_RESOURCE_NOT_FOUND is returned and the inode is left
- * unmodified.
- * If @force is %true:
- * If any needed SHA1 message digests are not found in the lookup table,
- * new entries are allocated and inserted into the lookup table.
- */
-int
-inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
- bool force)
-{
- const u8 *hash;
-
- if (!inode->i_resolved) {
- struct wim_lookup_table_entry *lte, *ads_lte;
-
- /* Resolve the default file stream */
- lte = NULL;
- hash = inode->i_hash;
- if (!is_zero_hash(hash)) {
- lte = lookup_resource(table, hash);
- if (!lte) {
- if (force) {
- lte = new_lookup_table_entry();
- if (!lte)
- return WIMLIB_ERR_NOMEM;
- copy_hash(lte->hash, hash);
- lookup_table_insert(table, lte);
- } else {
- goto resource_not_found;
- }
- }
- }
-
- /* Resolve the alternate data streams */
- struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
- for (u16 i = 0; i < inode->i_num_ads; i++) {
- struct wim_ads_entry *cur_entry;
-
- ads_lte = NULL;
- cur_entry = &inode->i_ads_entries[i];
- hash = cur_entry->hash;
- if (!is_zero_hash(hash)) {
- ads_lte = lookup_resource(table, hash);
- if (!ads_lte) {
- if (force) {
- ads_lte = new_lookup_table_entry();
- if (!ads_lte)
- return WIMLIB_ERR_NOMEM;
- copy_hash(ads_lte->hash, hash);
- lookup_table_insert(table, ads_lte);
- } else {
- goto resource_not_found;
- }
- }
- }
- ads_ltes[i] = ads_lte;
- }
- inode->i_lte = lte;
- for (u16 i = 0; i < inode->i_num_ads; i++)
- inode->i_ads_entries[i].lte = ads_ltes[i];
- inode->i_resolved = 1;
- }
- return 0;
-
-resource_not_found:
- return resource_not_found_error(inode, hash);
-}
-
-void
-inode_unresolve_ltes(struct wim_inode *inode)
-{
- if (inode->i_resolved) {
- if (inode->i_lte)
- copy_hash(inode->i_hash, inode->i_lte->hash);
- else
- zero_out_hash(inode->i_hash);
-
- for (u16 i = 0; i < inode->i_num_ads; i++) {
- if (inode->i_ads_entries[i].lte)
- copy_hash(inode->i_ads_entries[i].hash,
- inode->i_ads_entries[i].lte->hash);
- else
- zero_out_hash(inode->i_ads_entries[i].hash);
- }
- inode->i_resolved = 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 inodes.
- */
-struct wim_lookup_table_entry *
-inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
- const struct wim_lookup_table *table)
-{
- if (inode->i_resolved)
- return inode_stream_lte_resolved(inode, stream_idx);
- else
- return inode_stream_lte_unresolved(inode, stream_idx, table);
-}
-
-struct wim_lookup_table_entry *
-inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret)
-{
- wimlib_assert(inode->i_resolved);
- for (unsigned i = 0; i <= inode->i_num_ads; i++) {
- if (inode_stream_name_nbytes(inode, i) == 0 &&
- !is_zero_hash(inode_stream_hash_resolved(inode, i)))
- {
- *stream_idx_ret = i;
- return inode_stream_lte_resolved(inode, i);
- }
- }
- *stream_idx_ret = 0;
- return NULL;
-}
-
-struct wim_lookup_table_entry *
-inode_unnamed_lte_resolved(const struct wim_inode *inode)
-{
- u16 stream_idx;
- return inode_unnamed_stream_resolved(inode, &stream_idx);
-}
-
-struct wim_lookup_table_entry *
-inode_unnamed_lte_unresolved(const struct wim_inode *inode,
- const struct wim_lookup_table *table)
-{
- wimlib_assert(!inode->i_resolved);
- for (unsigned i = 0; i <= inode->i_num_ads; i++) {
- if (inode_stream_name_nbytes(inode, i) == 0 &&
- !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
- {
- return inode_stream_lte_unresolved(inode, i, table);
- }
- }
- return NULL;
-}
-
-/* Return the lookup table entry for the unnamed data stream of an inode, or
- * NULL if there is none.
- *
- * You'd think this would be easier than it actually is, since the unnamed data
- * stream should be the one referenced from the inode itself. Alas, if there
- * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
- * data stream in one of the alternate data streams instead of inside the WIM
- * dentry itself. So we need to check the alternate data streams too.
- *
- * Also, note that a dentry may appear to have more than one unnamed stream, but
- * if the SHA1 message digest is all 0's then the corresponding stream does not
- * really "count" (this is the case for the inode's own file stream when the
- * file stream that should be there is actually in one of the alternate stream
- * entries.). This is despite the fact that we may need to extract such a
- * missing entry as an empty file or empty named data stream.
- */
-struct wim_lookup_table_entry *
-inode_unnamed_lte(const struct wim_inode *inode,
- const struct wim_lookup_table *table)
-{
- if (inode->i_resolved)
- return inode_unnamed_lte_resolved(inode);
- else
- return inode_unnamed_lte_unresolved(inode, table);
-}
-
-/* Returns the SHA1 message digest of the unnamed data stream of a WIM inode, or
- * 'zero_hash' if the unnamed data stream is missing has all zeroes in its SHA1
- * message digest field. */
-const u8 *
-inode_unnamed_stream_hash(const struct wim_inode *inode)
-{
- const u8 *hash;
-
- for (unsigned i = 0; i <= inode->i_num_ads; i++) {
- if (inode_stream_name_nbytes(inode, i) == 0) {
- hash = inode_stream_hash(inode, i);
- if (!is_zero_hash(hash))
- return hash;
- }
- }
- return zero_hash;
-}
-
-struct wim_lookup_table_entry **
-retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
-{
- wimlib_assert(lte->unhashed);
- struct wim_inode *inode = lte->back_inode;
- u32 stream_id = lte->back_stream_id;
- if (stream_id == 0)
- return &inode->i_lte;
- else
- for (u16 i = 0; i < inode->i_num_ads; i++)
- if (inode->i_ads_entries[i].stream_id == stream_id)
- return &inode->i_ads_entries[i].lte;
- wimlib_assert(0);
- return NULL;
-}