X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fextract.c;h=cf8a2c922379b424a3986b6c6871259f7bb6d6b4;hp=68fab3fd2e95f3e30be19bf6e2dbf5514ae600bf;hb=7b587995ba7f2fb4e3a8d23f4be0ad91f1ab066a;hpb=c268a6111e676141c1eb13432cd46c77441a5b77 diff --git a/src/extract.c b/src/extract.c index 68fab3fd..cf8a2c92 100644 --- a/src/extract.c +++ b/src/extract.c @@ -58,12 +58,10 @@ #include "wimlib/resource.h" #include "wimlib/security.h" #include "wimlib/unix_data.h" -#ifdef __WIN32__ -# include "wimlib/win32.h" /* for realpath() equivalent */ -#endif -#include "wimlib/xml.h" #include "wimlib/wildcard.h" #include "wimlib/wim.h" +#include "wimlib/win32.h" /* for realpath() equivalent */ +#include "wimlib/xml.h" #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x80000000 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x40000000 @@ -98,7 +96,7 @@ do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg) } static int -start_file_phase(struct apply_ctx *ctx, uint64_t end_file_count, enum wimlib_progress_msg msg) +start_file_phase(struct apply_ctx *ctx, u64 end_file_count, enum wimlib_progress_msg msg) { ctx->progress.extract.current_file_count = 0; ctx->progress.extract.end_file_count = end_file_count; @@ -106,13 +104,13 @@ start_file_phase(struct apply_ctx *ctx, uint64_t end_file_count, enum wimlib_pro } int -start_file_structure_phase(struct apply_ctx *ctx, uint64_t end_file_count) +start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count) { return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE); } int -start_file_metadata_phase(struct apply_ctx *ctx, uint64_t end_file_count) +start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count) { return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA); } @@ -136,158 +134,124 @@ end_file_metadata_phase(struct apply_ctx *ctx) return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA); } -#define PWM_ALLOW_WIM_HDR 0x00001 +#define PWM_FOUND_WIM_HDR (-1) -/* Read the header for a blob in a pipable WIM. */ +/* Read the header for a blob in a pipable WIM. If @pwm_hdr_ret is not NULL, + * also look for a pipable WIM header and return PWM_FOUND_WIM_HDR if found. */ static int -read_pwm_blob_header(WIMStruct *pwm, struct blob_descriptor *blob, - struct wim_resource_descriptor *rdesc, - int flags, struct wim_header_disk *hdr_ret) +read_pwm_blob_header(WIMStruct *pwm, u8 hash_ret[SHA1_HASH_SIZE], + struct wim_reshdr *reshdr_ret, + struct wim_header_disk *pwm_hdr_ret) { - union { - struct pwm_blob_hdr blob_hdr; - struct wim_header_disk pwm_hdr; - } buf; - struct wim_reshdr reshdr; int ret; + struct pwm_blob_hdr blob_hdr; + u64 magic; - ret = full_read(&pwm->in_fd, &buf.blob_hdr, sizeof(buf.blob_hdr)); - if (ret) + ret = full_read(&pwm->in_fd, &blob_hdr, sizeof(blob_hdr)); + if (unlikely(ret)) goto read_error; - if ((flags & PWM_ALLOW_WIM_HDR) && - le64_to_cpu(buf.blob_hdr.magic) == PWM_MAGIC) - { - BUILD_BUG_ON(sizeof(buf.pwm_hdr) < sizeof(buf.blob_hdr)); - ret = full_read(&pwm->in_fd, &buf.blob_hdr + 1, - sizeof(buf.pwm_hdr) - sizeof(buf.blob_hdr)); + magic = le64_to_cpu(blob_hdr.magic); - if (ret) + if (magic == PWM_MAGIC && pwm_hdr_ret != NULL) { + memcpy(pwm_hdr_ret, &blob_hdr, sizeof(blob_hdr)); + ret = full_read(&pwm->in_fd, + (u8 *)pwm_hdr_ret + sizeof(blob_hdr), + sizeof(*pwm_hdr_ret) - sizeof(blob_hdr)); + if (unlikely(ret)) goto read_error; - blob->blob_location = BLOB_NONEXISTENT; - memcpy(hdr_ret, &buf.pwm_hdr, sizeof(buf.pwm_hdr)); - return 0; + return PWM_FOUND_WIM_HDR; } - if (le64_to_cpu(buf.blob_hdr.magic) != PWM_BLOB_MAGIC) { - ERROR("Data read on pipe is invalid (expected blob header)."); + if (unlikely(magic != PWM_BLOB_MAGIC)) { + ERROR("Data read on pipe is invalid (expected blob header)"); return WIMLIB_ERR_INVALID_PIPABLE_WIM; } - copy_hash(blob->hash, buf.blob_hdr.hash); + copy_hash(hash_ret, blob_hdr.hash); + + reshdr_ret->size_in_wim = 0; /* Not available */ + reshdr_ret->flags = le32_to_cpu(blob_hdr.flags); + reshdr_ret->offset_in_wim = pwm->in_fd.offset; + reshdr_ret->uncompressed_size = le64_to_cpu(blob_hdr.uncompressed_size); + + if (unlikely(reshdr_ret->uncompressed_size == 0)) { + ERROR("Data read on pipe is invalid (resource is of 0 size)"); + return WIMLIB_ERR_INVALID_PIPABLE_WIM; + } - reshdr.size_in_wim = 0; - reshdr.flags = le32_to_cpu(buf.blob_hdr.flags); - reshdr.offset_in_wim = pwm->in_fd.offset; - reshdr.uncompressed_size = le64_to_cpu(buf.blob_hdr.uncompressed_size); - wim_res_hdr_to_desc(&reshdr, pwm, rdesc); - blob_set_is_located_in_wim_resource(blob, rdesc); - blob->flags = rdesc->flags; - blob->size = rdesc->uncompressed_size; - blob->offset_in_res = 0; return 0; read_error: - ERROR_WITH_ERRNO("Error reading pipable WIM from pipe"); + if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE) + ERROR("The pipe ended before all needed data was sent!"); + else + ERROR_WITH_ERRNO("Error reading pipable WIM from pipe"); return ret; } static int -read_blobs_from_pipe(struct apply_ctx *ctx, - const struct read_blob_list_callbacks *cbs) +read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs) { - struct blob_descriptor *found_blob = NULL; - struct wim_resource_descriptor *rdesc = NULL; - struct blob_table *blob_table; int ret; + u8 hash[SHA1_HASH_SIZE]; + struct wim_reshdr reshdr; + struct wim_header_disk pwm_hdr; + struct wim_resource_descriptor rdesc; + struct blob_descriptor *blob; - ret = WIMLIB_ERR_NOMEM; - found_blob = new_blob_descriptor(); - if (!found_blob) - goto out; - - rdesc = MALLOC(sizeof(struct wim_resource_descriptor)); - if (!rdesc) - goto out; - - blob_table = ctx->wim->blob_table; - memcpy(ctx->progress.extract.guid, ctx->wim->hdr.guid, WIM_GUID_LEN); + copy_guid(ctx->progress.extract.guid, ctx->wim->hdr.guid); ctx->progress.extract.part_number = ctx->wim->hdr.part_number; ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts; ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN); if (ret) - goto out; + return ret; while (ctx->num_blobs_remaining) { - struct wim_header_disk pwm_hdr; - struct blob_descriptor *needed_blob; - if (found_blob->blob_location != BLOB_NONEXISTENT) - blob_unset_is_located_in_wim_resource(found_blob); - ret = read_pwm_blob_header(ctx->wim, found_blob, rdesc, - PWM_ALLOW_WIM_HDR, &pwm_hdr); - if (ret) - goto out; + ret = read_pwm_blob_header(ctx->wim, hash, &reshdr, &pwm_hdr); - if ((found_blob->blob_location != BLOB_NONEXISTENT) - && !(found_blob->flags & WIM_RESHDR_FLAG_METADATA) - && (needed_blob = lookup_blob(blob_table, found_blob->hash)) - && (needed_blob->out_refcnt)) - { - needed_blob->offset_in_res = found_blob->offset_in_res; - needed_blob->flags = found_blob->flags; - needed_blob->size = found_blob->size; + if (ret == PWM_FOUND_WIM_HDR) { + u16 part_number = le16_to_cpu(pwm_hdr.part_number); + u16 total_parts = le16_to_cpu(pwm_hdr.total_parts); - blob_unset_is_located_in_wim_resource(found_blob); - blob_set_is_located_in_wim_resource(needed_blob, rdesc); + if (part_number == ctx->progress.extract.part_number && + total_parts == ctx->progress.extract.total_parts && + guids_equal(pwm_hdr.guid, ctx->progress.extract.guid)) + continue; - ret = (*cbs->begin_blob)(needed_blob, - cbs->begin_blob_ctx); - if (ret) { - blob_unset_is_located_in_wim_resource(needed_blob); - goto out; - } + copy_guid(ctx->progress.extract.guid, pwm_hdr.guid); + ctx->progress.extract.part_number = part_number; + ctx->progress.extract.total_parts = total_parts; + ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN); + if (ret) + return ret; - ret = extract_blob(needed_blob, needed_blob->size, - cbs->consume_chunk, - cbs->consume_chunk_ctx); + continue; + } - ret = (*cbs->end_blob)(needed_blob, ret, - cbs->end_blob_ctx); - blob_unset_is_located_in_wim_resource(needed_blob); + if (ret) + return ret; + + if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA) + && (blob = lookup_blob(ctx->wim->blob_table, hash)) + && (blob->out_refcnt)) + { + wim_reshdr_to_desc_and_blob(&reshdr, ctx->wim, &rdesc, blob); + ret = read_blob_with_sha1(blob, cbs); + blob_unset_is_located_in_wim_resource(blob); if (ret) - goto out; + return ret; ctx->num_blobs_remaining--; - } else if (found_blob->blob_location != BLOB_NONEXISTENT) { - ret = skip_wim_resource(found_blob->rdesc); - if (ret) - goto out; } else { - u16 part_number = le16_to_cpu(pwm_hdr.part_number); - u16 total_parts = le16_to_cpu(pwm_hdr.total_parts); - - if (part_number != ctx->progress.extract.part_number || - total_parts != ctx->progress.extract.total_parts || - memcmp(pwm_hdr.guid, ctx->progress.extract.guid, - WIM_GUID_LEN)) - { - ctx->progress.extract.part_number = part_number; - ctx->progress.extract.total_parts = total_parts; - memcpy(ctx->progress.extract.guid, - pwm_hdr.guid, WIM_GUID_LEN); - ret = extract_progress(ctx, - WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN); - if (ret) - goto out; - } + wim_reshdr_to_desc(&reshdr, ctx->wim, &rdesc); + ret = skip_wim_resource(&rdesc); + if (ret) + return ret; } } - ret = 0; -out: - if (found_blob && found_blob->blob_location != BLOB_IN_WIM) - FREE(rdesc); - free_blob_descriptor(found_blob); - return ret; + + return 0; } /* Creates a temporary file opened for writing. The open file descriptor is @@ -339,8 +303,8 @@ begin_extract_blob_wrapper(struct blob_descriptor *blob, void *_ctx) if (unlikely(blob->out_refcnt > MAX_OPEN_FILES)) return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name); - else - return (*ctx->saved_cbs->begin_blob)(blob, ctx->saved_cbs->begin_blob_ctx); + + return call_begin_blob(blob, ctx->saved_cbs); } static int @@ -415,10 +379,9 @@ extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx) ctx->tmpfile_name); } return ret; - } else { - return (*ctx->saved_cbs->consume_chunk)(chunk, size, - ctx->saved_cbs->consume_chunk_ctx); } + + return call_consume_chunk(chunk, size, ctx->saved_cbs); } static int @@ -426,7 +389,7 @@ extract_from_tmpfile(const tchar *tmpfile_name, struct apply_ctx *ctx) { struct blob_descriptor tmpfile_blob; struct blob_descriptor *orig_blob = ctx->cur_blob; - const struct read_blob_list_callbacks *cbs = ctx->saved_cbs; + const struct read_blob_callbacks *cbs = ctx->saved_cbs; int ret; const u32 orig_refcnt = orig_blob->out_refcnt; @@ -450,7 +413,7 @@ extract_from_tmpfile(const tchar *tmpfile_name, struct apply_ctx *ctx) * blob descriptor to callbacks provided by the extraction * backend as opposed to the tmpfile blob descriptor, since they * shouldn't actually read data from the blob other than through - * the read_blob_prefix() call below. But for + * the read_blob_with_cbs() call below. But for * WIMLIB_EXTRACT_FLAG_WIMBOOT mode on Windows it does matter * because it needs access to the original WIM resource * descriptor in order to create the external backing reference. @@ -459,17 +422,17 @@ extract_from_tmpfile(const tchar *tmpfile_name, struct apply_ctx *ctx) orig_blob->out_refcnt = 1; orig_blob->inline_blob_extraction_targets[0] = targets[i]; - ret = (*cbs->begin_blob)(orig_blob, cbs->begin_blob_ctx); + ret = call_begin_blob(orig_blob, cbs); if (ret) break; - /* Extra SHA-1 isn't necessary here, but it shouldn't hurt as - * this case is very rare anyway. */ - ret = extract_blob(&tmpfile_blob, tmpfile_blob.size, - cbs->consume_chunk, - cbs->consume_chunk_ctx); + struct read_blob_callbacks wrapper_cbs = { + .consume_chunk = cbs->consume_chunk, + .ctx = cbs->ctx, + }; + ret = read_blob_with_cbs(&tmpfile_blob, &wrapper_cbs); - ret = (*cbs->end_blob)(orig_blob, ret, cbs->end_blob_ctx); + ret = call_end_blob(orig_blob, ret, cbs); if (ret) break; } @@ -491,10 +454,9 @@ end_extract_blob_wrapper(struct blob_descriptor *blob, int status, void *_ctx) tunlink(ctx->tmpfile_name); FREE(ctx->tmpfile_name); return status; - } else { - return (*ctx->saved_cbs->end_blob)(blob, status, - ctx->saved_cbs->end_blob_ctx); } + + return call_end_blob(blob, status, ctx->saved_cbs); } /* @@ -505,27 +467,23 @@ end_extract_blob_wrapper(struct blob_descriptor *blob, int status, void *_ctx) * * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS. * - * This also works if the WIM is being read from a pipe, whereas attempting to - * read blobs directly (e.g. with read_full_blob_into_buf()) will not. + * This also works if the WIM is being read from a pipe. * * This also will split up blobs that will need to be extracted to more than * MAX_OPEN_FILES locations, as measured by the 'out_refcnt' of each blob. * Therefore, the apply_operations implementation need not worry about running * out of file descriptors, unless it might open more than one file descriptor - * per nominal destination (e.g. Win32 currently might because the destination - * file system might not support hard links). + * per 'blob_extraction_target' (e.g. Win32 currently might because the + * destination file system might not support hard links). */ int -extract_blob_list(struct apply_ctx *ctx, - const struct read_blob_list_callbacks *cbs) +extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs) { - struct read_blob_list_callbacks wrapper_cbs = { - .begin_blob = begin_extract_blob_wrapper, - .begin_blob_ctx = ctx, - .consume_chunk = extract_chunk_wrapper, - .consume_chunk_ctx = ctx, - .end_blob = end_extract_blob_wrapper, - .end_blob_ctx = ctx, + struct read_blob_callbacks wrapper_cbs = { + .begin_blob = begin_extract_blob_wrapper, + .consume_chunk = extract_chunk_wrapper, + .end_blob = end_extract_blob_wrapper, + .ctx = ctx, }; ctx->saved_cbs = cbs; if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) { @@ -569,7 +527,7 @@ extract_dentry_to_stdout(struct wim_dentry *dentry, } filedes_init(&_stdout, STDOUT_FILENO); - return extract_full_blob_to_fd(blob, &_stdout); + return extract_blob_to_fd(blob, &_stdout); } static int @@ -1199,7 +1157,7 @@ inode_tally_features(const struct wim_inode *inode, else features->other_reparse_points++; } - if (inode->i_security_id != -1) + if (inode_has_security_descriptor(inode)) features->security_descriptors++; if (inode_has_unix_data(inode)) features->unix_data++; @@ -1920,21 +1878,19 @@ wimlib_extract_image_from_pipe_with_progress(int pipe_fd, * write_pipable_wim() for more details about the format of pipable * WIMs.) */ { - struct blob_descriptor xml_blob; - struct wim_resource_descriptor xml_rdesc; - ret = read_pwm_blob_header(pwm, &xml_blob, &xml_rdesc, 0, NULL); + u8 hash[SHA1_HASH_SIZE]; + + ret = read_pwm_blob_header(pwm, hash, + &pwm->hdr.xml_data_reshdr, NULL); if (ret) goto out_wimlib_free; - if (!(xml_blob.flags & WIM_RESHDR_FLAG_METADATA)) - { + if (!(pwm->hdr.xml_data_reshdr.flags & WIM_RESHDR_FLAG_METADATA)) { ERROR("Expected XML data, but found non-metadata resource."); ret = WIMLIB_ERR_INVALID_PIPABLE_WIM; goto out_wimlib_free; } - wim_res_desc_to_hdr(&xml_rdesc, &pwm->hdr.xml_data_reshdr); - ret = read_wim_xml_data(pwm); if (ret) goto out_wimlib_free; @@ -1972,37 +1928,38 @@ wimlib_extract_image_from_pipe_with_progress(int pipe_fd, /* Load the needed metadata resource. */ for (i = 1; i <= pwm->hdr.image_count; i++) { - struct blob_descriptor *metadata_blob; struct wim_image_metadata *imd; + struct wim_reshdr reshdr; struct wim_resource_descriptor *metadata_rdesc; - metadata_blob = new_blob_descriptor(); - if (metadata_blob == NULL) { - ret = WIMLIB_ERR_NOMEM; - goto out_wimlib_free; - } - metadata_rdesc = MALLOC(sizeof(struct wim_resource_descriptor)); - if (metadata_rdesc == NULL) { - ret = WIMLIB_ERR_NOMEM; - free_blob_descriptor(metadata_blob); + imd = pwm->image_metadata[i - 1]; + + ret = WIMLIB_ERR_NOMEM; + imd->metadata_blob = new_blob_descriptor(); + if (!imd->metadata_blob) goto out_wimlib_free; - } - ret = read_pwm_blob_header(pwm, metadata_blob, metadata_rdesc, 0, NULL); - imd = pwm->image_metadata[i - 1]; - imd->metadata_blob = metadata_blob; - if (ret) { - FREE(metadata_rdesc); + imd->metadata_blob->is_metadata = 1; + + ret = read_pwm_blob_header(pwm, imd->metadata_blob->hash, + &reshdr, NULL); + if (ret) goto out_wimlib_free; - } - if (!(metadata_blob->flags & WIM_RESHDR_FLAG_METADATA)) { + if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) { ERROR("Expected metadata resource, but found " - "non-metadata resource."); + "non-metadata resource"); ret = WIMLIB_ERR_INVALID_PIPABLE_WIM; goto out_wimlib_free; } + ret = WIMLIB_ERR_NOMEM; + metadata_rdesc = MALLOC(sizeof(struct wim_resource_descriptor)); + if (!metadata_rdesc) + goto out_wimlib_free; + wim_reshdr_to_desc_and_blob(&reshdr, pwm, metadata_rdesc, + imd->metadata_blob); + if (i == image) { /* Metadata resource is for the image being extracted. * Parse it and save the metadata in memory. */