-
-/* Write the metadata resource for the current image. */
-int write_metadata_resource(WIMStruct *w)
-{
- FILE *out;
- u8 *buf;
- u8 *p;
- int ret;
- u64 subdir_offset;
- struct dentry *root;
- struct lookup_table_entry *lte;
- off_t metadata_offset;
- u64 metadata_original_size;
- u64 metadata_compressed_size;
- int metadata_ctype;
- u8 hash[SHA1_HASH_SIZE];
-
- DEBUG("Writing metadata resource for image %d", w->current_image);
-
- out = w->out_fp;
- root = wim_root_dentry(w);
- metadata_ctype = wimlib_get_compression_type(w);
- metadata_offset = ftello(out);
- if (metadata_offset == -1)
- return WIMLIB_ERR_WRITE;
-
- struct wim_security_data *sd = wim_security_data(w);
- if (sd)
- subdir_offset = sd->total_length + root->length + 8;
- else
- subdir_offset = 8 + root->length + 8;
- calculate_subdir_offsets(root, &subdir_offset);
- metadata_original_size = subdir_offset;
- buf = MALLOC(metadata_original_size);
- if (!buf) {
- ERROR("Failed to allocate %"PRIu64" bytes for "
- "metadata resource", metadata_original_size);
- return WIMLIB_ERR_NOMEM;
- }
-
- p = write_security_data(sd, buf);
-
- DEBUG("Writing dentry tree.");
- p = write_dentry_tree(root, p);
-
- /* Like file resources, the lookup table entry for a metadata resource
- * uses for the hash code a SHA1 message digest of its uncompressed
- * contents. */
- sha1_buffer(buf, metadata_original_size, hash);
-
-
- lte = wim_metadata_lookup_table_entry(w);
-
- ret = write_wim_resource_from_buffer(buf, metadata_original_size,
- hash, out, metadata_ctype,
- <e->output_resource_entry);
-
- lookup_table_unlink(w->lookup_table, lte);
- copy_hash(lte->hash, hash);
- lookup_table_insert(w->lookup_table, lte);
- lte->out_refcnt++;
- lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
- FREE(buf);
- if (ret != 0)
- return ret;
-
- return 0;
-}