#include <stdlib.h>
#include <stdarg.h>
+#include "dentry.h"
+
#ifdef WITH_NTFS_3G
#include <ntfs-3g/attrib.h>
#include <ntfs-3g/inode.h>
#include "lzx.h"
#include "xpress.h"
#include "sha1.h"
-#include "dentry.h"
#include <unistd.h>
#include <errno.h>
#ifdef HAVE_ALLOCA_H
return p;
}
+static FILE *wim_get_fp(WIMStruct *w)
+{
+ pthread_mutex_lock(&w->fp_tab_mutex);
+ FILE *fp;
+
+ wimlib_assert(w->filename != NULL);
+
+ for (size_t i = 0; i < w->num_allocated_fps; i++) {
+ if (w->fp_tab[i]) {
+ fp = w->fp_tab[i];
+ w->fp_tab[i] = NULL;
+ goto out;
+ }
+ }
+ DEBUG("Opening extra file descriptor to `%s'", w->filename);
+ fp = fopen(w->filename, "rb");
+ if (!fp)
+ ERROR_WITH_ERRNO("Failed to open `%s'", w->filename);
+out:
+ pthread_mutex_unlock(&w->fp_tab_mutex);
+ return fp;
+}
+
+static int wim_release_fp(WIMStruct *w, FILE *fp)
+{
+ int ret = 0;
+ FILE **fp_tab;
+
+ pthread_mutex_lock(&w->fp_tab_mutex);
+
+ for (size_t i = 0; i < w->num_allocated_fps; i++) {
+ if (w->fp_tab[i] == NULL) {
+ w->fp_tab[i] = fp;
+ goto out;
+ }
+ }
+
+ fp_tab = REALLOC(w->fp_tab, sizeof(FILE*) * (w->num_allocated_fps + 4));
+ if (!fp_tab) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out;
+ }
+ w->fp_tab = fp_tab;
+ memset(&w->fp_tab[w->num_allocated_fps], 0, 4 * sizeof(FILE*));
+ w->fp_tab[w->num_allocated_fps] = fp;
+ w->num_allocated_fps += 4;
+out:
+ pthread_mutex_unlock(&w->fp_tab_mutex);
+ return ret;
+}
+
/*
* Reads some data from the resource corresponding to a WIM lookup table entry.
*
* @buf: Buffer into which to write the data.
* @size: Number of bytes to read.
* @offset: Offset at which to start reading the resource.
- * @raw: If %true, compressed data is read literally rather than being
- * decompressed first.
*
* Returns zero on success, nonzero on failure.
*/
int read_wim_resource(const struct lookup_table_entry *lte, u8 buf[],
- size_t size, u64 offset, bool raw)
+ size_t size, u64 offset, int flags)
{
int ctype;
int ret = 0;
/* We shouldn't be allowing read over-runs in any part of the library.
* */
- if (raw)
+ if (flags & WIMLIB_RESOURCE_FLAG_RAW)
wimlib_assert(offset + size <= lte->resource_entry.size);
else
wimlib_assert(offset + size <= lte->resource_entry.original_size);
* the lte->wim member. The resource may be either compressed
* or uncompressed. */
wimlib_assert(lte->wim != NULL);
- wimlib_assert(lte->wim->fp != NULL);
+
+ if (flags & WIMLIB_RESOURCE_FLAG_MULTITHREADED) {
+ fp = wim_get_fp(lte->wim);
+ if (!fp)
+ return WIMLIB_ERR_OPEN;
+ } else {
+ wimlib_assert(lte->wim->fp != NULL);
+ fp = lte->wim->fp;
+ }
+
ctype = wim_resource_compression_type(lte);
wimlib_assert(ctype != WIM_COMPRESSION_TYPE_NONE ||
(lte->resource_entry.original_size ==
lte->resource_entry.size));
- if (raw || ctype == WIM_COMPRESSION_TYPE_NONE)
- ret = read_uncompressed_resource(lte->wim->fp,
+ if ((flags & WIMLIB_RESOURCE_FLAG_RAW)
+ || ctype == WIM_COMPRESSION_TYPE_NONE)
+ ret = read_uncompressed_resource(fp,
lte->resource_entry.offset + offset,
size, buf);
else
- ret = read_compressed_resource(lte->wim->fp,
+ ret = read_compressed_resource(fp,
lte->resource_entry.size,
lte->resource_entry.original_size,
lte->resource_entry.offset,
ctype, size, offset, buf);
+ if (flags & WIMLIB_RESOURCE_FLAG_MULTITHREADED) {
+ int ret2 = wim_release_fp(lte->wim, fp);
+ if (ret == 0)
+ ret = ret2;
+ }
break;
case RESOURCE_IN_STAGING_FILE:
case RESOURCE_IN_FILE_ON_DISK:
wimlib_assert(lte->ntfs_loc != NULL);
wimlib_assert(lte->attr != NULL);
{
- u64 adjusted_offset;
if (lte->ntfs_loc->is_reparse_point)
- adjusted_offset = offset + 8;
- else
- adjusted_offset = offset;
+ offset += 8;
if (ntfs_attr_pread(lte->attr, offset, size, buf) != size) {
ERROR_WITH_ERRNO("Error reading NTFS attribute "
"at `%s'",
*
* Returns 0 on success; nonzero on failure.
*/
-int read_full_wim_resource(const struct lookup_table_entry *lte, u8 buf[])
+int read_full_wim_resource(const struct lookup_table_entry *lte, u8 buf[],
+ int flags)
{
- return read_wim_resource(lte, buf, wim_resource_size(lte), 0, false);
+ return read_wim_resource(lte, buf, wim_resource_size(lte), 0, flags);
}
/* Chunk table that's located at the beginning of each compressed resource in
*/
static int write_wim_resource(struct lookup_table_entry *lte,
FILE *out_fp, int out_ctype,
- struct resource_entry *out_res_entry)
+ struct resource_entry *out_res_entry,
+ int flags)
{
u64 bytes_remaining;
u64 original_size;
* without decompressing and recompressing the data). */
raw = (wim_resource_compression_type(lte) == out_ctype
&& out_ctype != WIM_COMPRESSION_TYPE_NONE);
- if (raw)
+
+ if (raw) {
+ flags |= WIMLIB_RESOURCE_FLAG_RAW;
bytes_remaining = old_compressed_size;
- else
+ } else {
+ flags &= ~WIMLIB_RESOURCE_FLAG_RAW;
bytes_remaining = original_size;
+ }
/* Empty resource; nothing needs to be done, so just return success. */
if (bytes_remaining == 0)
offset = 0;
do {
u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
- ret = read_wim_resource(lte, buf, to_read, offset, raw);
+ ret = read_wim_resource(lte, buf, to_read, offset, flags);
if (ret != 0)
goto out_fclose;
if (!raw)
goto out_fclose;
}
ret = write_wim_resource(lte, out_fp, WIM_COMPRESSION_TYPE_NONE,
- out_res_entry);
+ out_res_entry, flags);
if (ret != 0)
goto out_fclose;
if (fflush(out_fp) != 0) {
lte.attached_buffer = (u8*)buf;
zero_out_hash(lte.hash);
- ret = write_wim_resource(<e, out_fp, out_ctype, out_res_entry);
+ ret = write_wim_resource(<e, out_fp, out_ctype, out_res_entry, 0);
if (ret != 0)
return ret;
copy_hash(hash, lte.hash);
while (bytes_remaining) {
u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
- ret = read_wim_resource(lte, buf, to_read, offset, false);
+ ret = read_wim_resource(lte, buf, to_read, offset, 0);
if (ret != 0)
break;
sha1_update(&ctx, buf, to_read);
ret = write_wim_resource(lte, w->out_fp,
wim_resource_compression_type(lte),
- <e->output_resource_entry);
+ <e->output_resource_entry, 0);
if (ret != 0)
return ret;
lte->out_refcnt = lte->refcnt;
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);
+ <e->output_resource_entry, 0);
if (ret != 0)
break;
}
}
/* Read the metadata resource into memory. (It may be compressed.) */
- ret = read_full_wim_resource(metadata_lte, buf);
+ ret = read_full_wim_resource(metadata_lte, buf, 0);
if (ret != 0)
goto out_free_buf;
if (ret != 0)
goto out_free_buf;
- dentry_offset = imd->security_data->total_length + 7 & ~7;
+ dentry_offset = (imd->security_data->total_length + 7) & ~7;
if (dentry_offset == 0) {
ERROR("Integer overflow while reading metadata resource");
ret = read_dentry(buf, metadata_len, dentry_offset, dentry);
- /* This is the root dentry, so set its pointers correctly. */
+ /* This is the root dentry, so set its parent to itself. */
dentry->parent = dentry;
- dentry->next = dentry;
- dentry->prev = dentry;
+
if (ret != 0)
goto out_free_dentry_tree;
inode_add_dentry(dentry, dentry->d_inode);