+
+ /* Are the compression types the same? If so, do a raw copy (copy
+ * without decompressing and recompressing the data). */
+ raw = (wim_resource_compression_type(lte) == out_ctype
+ && out_ctype != WIM_COMPRESSION_TYPE_NONE);
+ if (raw)
+ bytes_remaining = old_compressed_size;
+ else
+ bytes_remaining = original_size;
+
+ /* Empty resource; nothing needs to be done, so just return success. */
+ if (bytes_remaining == 0)
+ return 0;
+
+ /* Buffer for reading chunks for the resource */
+ u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
+
+ /* If we are writing a compressed resource and not doing a raw copy, we
+ * need to initialize the chunk table */
+ if (out_ctype != WIM_COMPRESSION_TYPE_NONE && !raw) {
+ ret = begin_wim_resource_chunk_tab(lte, out_fp, file_offset,
+ &chunk_tab);
+ if (ret != 0)
+ goto out;
+ }
+
+ /* If the WIM resource is in an external file, open a FILE * to it so we
+ * don't have to open a temporary one in read_wim_resource() for each
+ * chunk. */
+ if (lte->resource_location == RESOURCE_IN_FILE_ON_DISK
+ && !lte->file_on_disk_fp)
+ {
+ wimlib_assert(lte->file_on_disk);
+ lte->file_on_disk_fp = fopen(lte->file_on_disk, "rb");
+ if (!lte->file_on_disk_fp) {
+ ERROR_WITH_ERRNO("Failed to open the file `%s' for "
+ "reading", lte->file_on_disk);
+ ret = WIMLIB_ERR_OPEN;
+ goto out;
+ }
+ }
+#ifdef WITH_NTFS_3G
+ else if (lte->resource_location == RESOURCE_IN_NTFS_VOLUME
+ && !lte->attr)
+ {
+ struct ntfs_location *loc = lte->ntfs_loc;
+ wimlib_assert(loc);
+ ni = ntfs_pathname_to_inode(*loc->ntfs_vol_p, NULL, loc->path_utf8);
+ if (!ni) {
+ ERROR_WITH_ERRNO("Failed to open inode `%s' in NTFS "
+ "volume", loc->path_utf8);
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out;
+ }
+ lte->attr = ntfs_attr_open(ni,
+ loc->is_reparse_point ? AT_REPARSE_POINT : AT_DATA,
+ (ntfschar*)loc->stream_name_utf16,
+ loc->stream_name_utf16_num_chars);
+ if (!lte->attr) {
+ ERROR_WITH_ERRNO("Failed to open attribute of `%s' in "
+ "NTFS volume", loc->path_utf8);
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out_fclose;
+ }
+ }
+#endif
+
+ /* If we aren't doing a raw copy, we will compute the SHA1 message
+ * digest of the resource as we read it, and verify it's the same as the
+ * hash given in the lookup table entry once we've finished reading the
+ * resource. */
+ SHA_CTX ctx;
+ if (!raw)
+ sha1_init(&ctx);
+
+ /* While there are still bytes remaining in the WIM resource, read a
+ * chunk of the resource, update SHA1, then write that chunk using the
+ * desired compression type. */
+ do {
+ u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
+ ret = read_wim_resource(lte, buf, to_read, offset, raw);
+ if (ret != 0)
+ goto out_fclose;
+ if (!raw)
+ sha1_update(&ctx, buf, to_read);
+ ret = write_wim_resource_chunk(buf, to_read, out_fp,
+ out_ctype, chunk_tab);
+ if (ret != 0)
+ goto out_fclose;
+ bytes_remaining -= to_read;
+ offset += to_read;
+ } while (bytes_remaining);
+
+ /* Raw copy: The new compressed size is the same as the old compressed
+ * size
+ *
+ * Using WIM_COMPRESSION_TYPE_NONE: The new compressed size is the
+ * original size
+ *
+ * Using a different compression type: Call
+ * finish_wim_resource_chunk_tab() and it will provide the new
+ * compressed size.
+ */
+ if (raw) {
+ new_compressed_size = old_compressed_size;
+ } else {
+ if (out_ctype == WIM_COMPRESSION_TYPE_NONE)
+ new_compressed_size = original_size;
+ else {
+ ret = finish_wim_resource_chunk_tab(chunk_tab, out_fp,
+ &new_compressed_size);
+ if (ret != 0)
+ goto out_fclose;
+ }
+ }
+
+ /* Verify SHA1 message digest of the resource, unless we are doing a raw
+ * write (in which case we never even saw the uncompressed data). Or,
+ * if the hash we had before is all 0's, just re-set it to be the new
+ * hash. */
+ if (!raw) {
+ u8 md[SHA1_HASH_SIZE];
+ sha1_final(md, &ctx);
+ if (is_zero_hash(lte->hash)) {
+ copy_hash(lte->hash, md);
+ } else if (!hashes_equal(md, lte->hash)) {
+ ERROR("WIM resource has incorrect hash!");
+ if (lte->resource_location == RESOURCE_IN_FILE_ON_DISK) {
+ ERROR("We were reading it from `%s'; maybe it changed "
+ "while we were reading it.",
+ lte->file_on_disk);
+ }
+ ret = WIMLIB_ERR_INVALID_RESOURCE_HASH;
+ goto out_fclose;
+ }
+ }
+
+ if (!raw && new_compressed_size >= original_size &&
+ out_ctype != WIM_COMPRESSION_TYPE_NONE)
+ {
+ /* Oops! We compressed the resource to larger than the original
+ * size. Write the resource uncompressed instead. */
+ if (fseeko(out_fp, file_offset, SEEK_SET) != 0) {
+ ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" "
+ "of output WIM file", file_offset);
+ ret = WIMLIB_ERR_WRITE;
+ goto out_fclose;
+ }
+ ret = write_wim_resource(lte, out_fp, WIM_COMPRESSION_TYPE_NONE,
+ out_res_entry);
+ if (ret != 0)
+ goto out_fclose;
+ if (fflush(out_fp) != 0) {
+ ERROR_WITH_ERRNO("Failed to flush output WIM file");
+ ret = WIMLIB_ERR_WRITE;
+ goto out_fclose;
+ }
+ if (ftruncate(fileno(out_fp), file_offset + out_res_entry->size) != 0) {
+ ERROR_WITH_ERRNO("Failed to truncate output WIM file");
+ ret = WIMLIB_ERR_WRITE;
+ }
+ goto out_fclose;
+ }
+ wimlib_assert(new_compressed_size <= original_size || raw);
+ if (out_res_entry) {
+ out_res_entry->size = new_compressed_size;
+ out_res_entry->original_size = original_size;
+ out_res_entry->offset = file_offset;
+ out_res_entry->flags = lte->resource_entry.flags
+ & ~WIM_RESHDR_FLAG_COMPRESSED;
+ if (out_ctype != WIM_COMPRESSION_TYPE_NONE)
+ out_res_entry->flags |= WIM_RESHDR_FLAG_COMPRESSED;
+ }
+out_fclose:
+ if (lte->resource_location == RESOURCE_IN_FILE_ON_DISK
+ && lte->file_on_disk_fp) {
+ fclose(lte->file_on_disk_fp);
+ lte->file_on_disk_fp = NULL;
+ }
+#ifdef WITH_NTFS_3G
+ else if (lte->resource_location == RESOURCE_IN_NTFS_VOLUME) {
+ if (lte->attr) {
+ ntfs_attr_close(lte->attr);
+ lte->attr = NULL;
+ } if (ni) {
+ ntfs_inode_close(ni);
+ }
+ }
+#endif
+out:
+ FREE(chunk_tab);
+ return ret;
+}
+
+/* Like write_wim_resource(), but the resource is specified by a buffer of
+ * uncompressed data rather a lookup table entry; also writes the SHA1 hash of
+ * the buffer to @hash. */
+static int write_wim_resource_from_buffer(const u8 *buf, u64 buf_size,
+ FILE *out_fp, int out_ctype,
+ struct resource_entry *out_res_entry,
+ u8 hash[SHA1_HASH_SIZE])
+{
+ /* Set up a temporary lookup table entry that we provide to
+ * write_wim_resource(). */
+ struct lookup_table_entry lte;
+ int ret;
+ lte.resource_entry.flags = 0;
+ lte.resource_entry.original_size = buf_size;
+ lte.resource_entry.size = buf_size;
+ lte.resource_entry.offset = 0;
+ lte.resource_location = RESOURCE_IN_ATTACHED_BUFFER;
+ lte.attached_buffer = (u8*)buf;
+
+ zero_out_hash(lte.hash);
+ ret = write_wim_resource(<e, out_fp, out_ctype, out_res_entry);
+ if (ret != 0)
+ return ret;
+ copy_hash(hash, lte.hash);
+ return 0;
+}
+
+/*
+ * Extracts the first @size bytes of the WIM resource specified by @lte to the
+ * open file descriptor @fd.
+ *
+ * Returns 0 on success; nonzero on failure.
+ */
+int extract_wim_resource_to_fd(const struct lookup_table_entry *lte, int fd,
+ u64 size)
+{
+ u64 bytes_remaining = size;
+ u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
+ u64 offset = 0;
+ int ret = 0;
+ u8 hash[SHA1_HASH_SIZE];
+
+ SHA_CTX ctx;
+ sha1_init(&ctx);
+
+ while (bytes_remaining) {
+ u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
+ ret = read_wim_resource(lte, buf, to_read, offset, false);
+ if (ret != 0)
+ break;
+ sha1_update(&ctx, buf, to_read);
+ if (full_write(fd, buf, to_read) < to_read) {
+ ERROR_WITH_ERRNO("Error extracting WIM resource");
+ return WIMLIB_ERR_WRITE;
+ }
+ bytes_remaining -= to_read;
+ offset += to_read;
+ }
+ sha1_final(hash, &ctx);
+ if (!hashes_equal(hash, lte->hash)) {
+ ERROR("Invalid checksum on a WIM resource "
+ "(detected when extracting to external file)");
+ ERROR("The following WIM resource is invalid:");
+ print_lookup_table_entry(lte);
+ return WIMLIB_ERR_INVALID_RESOURCE_HASH;
+ }
+ return 0;
+}
+
+/*
+ * Extracts the WIM resource specified by @lte to the open file descriptor @fd.
+ *
+ * Returns 0 on success; nonzero on failure.
+ */
+int extract_full_wim_resource_to_fd(const struct lookup_table_entry *lte, int fd)
+{
+ return extract_wim_resource_to_fd(lte, fd, wim_resource_size(lte));
+}
+
+/*
+ * Copies the file resource specified by the lookup table entry @lte from the
+ * input WIM to the output WIM that has its FILE * given by
+ * ((WIMStruct*)wim)->out_fp.
+ *
+ * The output_resource_entry, out_refcnt, and part_number fields of @lte are
+ * updated.
+ *
+ * Metadata resources are not copied (they are handled elsewhere for joining and
+ * splitting).
+ */
+int copy_resource(struct lookup_table_entry *lte, void *wim)
+{
+ WIMStruct *w = wim;
+ int ret;
+
+ if ((lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) &&
+ !w->write_metadata)
+ return 0;
+
+ ret = write_wim_resource(lte, w->out_fp,
+ wim_resource_compression_type(lte),
+ <e->output_resource_entry);
+ if (ret != 0)
+ return ret;
+ lte->out_refcnt = lte->refcnt;
+ lte->part_number = w->hdr.part_number;
+ return 0;
+}
+
+/*
+ * Writes a dentry's resources, including the main file resource as well as all
+ * alternate data streams, to the output file.
+ *
+ * @dentry: The dentry for the file.
+ * @wim_p: A pointer to the WIMStruct containing @dentry.
+ *
+ * @return zero on success, nonzero on failure.
+ */
+int write_dentry_resources(struct dentry *dentry, void *wim_p)
+{
+ WIMStruct *w = wim_p;
+ int ret = 0;
+ struct lookup_table_entry *lte;
+ int ctype = wimlib_get_compression_type(w);
+
+ if (w->write_flags & WIMLIB_WRITE_FLAG_VERBOSE) {
+ wimlib_assert(dentry->full_path_utf8);
+ printf("Writing streams for `%s'\n", dentry->full_path_utf8);
+ }
+
+ for (unsigned i = 0; i <= dentry->d_inode->num_ads; i++) {
+ lte = inode_stream_lte(dentry->d_inode, i, w->lookup_table);
+ if (lte && ++lte->out_refcnt == 1) {
+ ret = write_wim_resource(lte, w->out_fp, ctype,
+ <e->output_resource_entry);
+ if (ret != 0)
+ break;
+ }
+ }
+ return ret;