X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fextract.c;h=496d9c9870664ba050902139a942363197956605;hb=b41816aff9a66099bd44c93729c89bbd514f9c3f;hp=6b35a90367d0b106a9a0152804481c47c27c24e1;hpb=9bd0659477b3a1ebad90b46106f743d72689bbd3;p=wimlib diff --git a/src/extract.c b/src/extract.c index 6b35a903..496d9c98 100644 --- a/src/extract.c +++ b/src/extract.c @@ -6,7 +6,7 @@ */ /* - * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers + * Copyright (C) 2012-2016 Eric Biggers * * This file is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free @@ -41,6 +41,7 @@ #include #include +#include #include #include @@ -84,7 +85,12 @@ WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \ WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \ WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \ - WIMLIB_EXTRACT_FLAG_WIMBOOT) + WIMLIB_EXTRACT_FLAG_WIMBOOT | \ + WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | \ + WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | \ + WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | \ + WIMLIB_EXTRACT_FLAG_COMPACT_LZX \ + ) /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */ @@ -254,6 +260,55 @@ read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cb return 0; } +static int +handle_pwm_metadata_resource(WIMStruct *pwm, int image, bool is_needed) +{ + struct blob_descriptor *blob; + struct wim_reshdr reshdr; + struct wim_resource_descriptor *rdesc; + int ret; + + ret = WIMLIB_ERR_NOMEM; + blob = new_blob_descriptor(); + if (!blob) + goto out; + + ret = read_pwm_blob_header(pwm, blob->hash, &reshdr, NULL); + if (ret) + goto out; + + ret = WIMLIB_ERR_INVALID_PIPABLE_WIM; + if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) { + ERROR("Expected metadata resource, but found non-metadata " + "resource"); + goto out; + } + + ret = WIMLIB_ERR_NOMEM; + rdesc = MALLOC(sizeof(*rdesc)); + if (!rdesc) + goto out; + + wim_reshdr_to_desc_and_blob(&reshdr, pwm, rdesc, blob); + pwm->refcnt++; + + ret = WIMLIB_ERR_NOMEM; + pwm->image_metadata[image - 1] = new_unloaded_image_metadata(blob); + if (!pwm->image_metadata[image - 1]) + goto out; + blob = NULL; + + /* If the metadata resource is for the image being extracted, then parse + * it and save the metadata in memory. Otherwise, skip over it. */ + if (is_needed) + ret = select_wim_image(pwm, image); + else + ret = skip_wim_resource(rdesc); +out: + free_blob_descriptor(blob); + return ret; +} + /* Creates a temporary file opened for writing. The open file descriptor is * returned in @fd_ret and its name is returned in @name_ret (dynamically * allocated). */ @@ -261,27 +316,33 @@ static int create_temporary_file(struct filedes *fd_ret, tchar **name_ret) { tchar *name; - int open_flags; int raw_fd; +#ifdef __WIN32__ retry: - name = ttempnam(NULL, T("wimlib")); + name = _wtempnam(NULL, L"wimlib"); if (!name) { ERROR_WITH_ERRNO("Failed to create temporary filename"); return WIMLIB_ERR_NOMEM; } - - open_flags = O_WRONLY | O_CREAT | O_EXCL | O_BINARY; -#ifdef __WIN32__ - open_flags |= _O_SHORT_LIVED; -#endif - raw_fd = topen(name, open_flags, 0600); + raw_fd = _wopen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY | + _O_SHORT_LIVED, 0600); + if (raw_fd < 0 && errno == EEXIST) { + FREE(name); + goto retry; + } +#else /* __WIN32__ */ + const char *tmpdir = getenv("TMPDIR"); + if (!tmpdir) + tmpdir = P_tmpdir; + name = MALLOC(strlen(tmpdir) + 1 + 6 + 6 + 1); + if (!name) + return WIMLIB_ERR_NOMEM; + sprintf(name, "%s/wimlibXXXXXX", tmpdir); + raw_fd = mkstemp(name); +#endif /* !__WIN32__ */ if (raw_fd < 0) { - if (errno == EEXIST) { - FREE(name); - goto retry; - } ERROR_WITH_ERRNO("Failed to create temporary file " "\"%"TS"\"", name); FREE(name); @@ -655,19 +716,20 @@ file_name_valid(utf16lechar *name, size_t num_chars, bool fix) if (num_chars == 0) return true; for (i = 0; i < num_chars; i++) { - switch (name[i]) { + switch (le16_to_cpu(name[i])) { #ifdef __WIN32__ - case cpu_to_le16('\\'): - case cpu_to_le16(':'): - case cpu_to_le16('*'): - case cpu_to_le16('?'): - case cpu_to_le16('"'): - case cpu_to_le16('<'): - case cpu_to_le16('>'): - case cpu_to_le16('|'): + case '\x01'...'\x1F': + case '\\': + case ':': + case '*': + case '?': + case '"': + case '<': + case '>': + case '|': #endif - case cpu_to_le16('/'): - case cpu_to_le16('\0'): + case '/': + case '\0': if (fix) name[i] = replacement_char; else @@ -675,16 +737,6 @@ file_name_valid(utf16lechar *name, size_t num_chars, bool fix) } } -#ifdef __WIN32__ - if (name[num_chars - 1] == cpu_to_le16(' ') || - name[num_chars - 1] == cpu_to_le16('.')) - { - if (fix) - name[num_chars - 1] = replacement_char; - else - return false; - } -#endif return true; } @@ -982,10 +1034,11 @@ ref_stream_if_needed(struct wim_dentry *dentry, struct wim_inode *inode, * - file is a directory * - file is encrypted * - backend needs to create the file as UNIX symlink - * - backend will extract the stream as externally backed + * - backend will extract the stream as externally + * backed from the WIM archive itself */ - if (ctx->apply_ops->will_externally_back) { - int ret = (*ctx->apply_ops->will_externally_back)(dentry, ctx); + if (ctx->apply_ops->will_back_from_wim) { + int ret = (*ctx->apply_ops->will_back_from_wim)(dentry, ctx); if (ret > 0) /* Error? */ return ret; if (ret < 0) /* Won't externally back? */ @@ -1076,12 +1129,14 @@ static void inode_tally_features(const struct wim_inode *inode, struct wim_features *features) { - if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE) - features->archive_files++; + if (inode->i_attributes & FILE_ATTRIBUTE_READONLY) + features->readonly_files++; if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN) features->hidden_files++; if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM) features->system_files++; + if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE) + features->archive_files++; if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED) features->compressed_files++; if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) { @@ -1159,9 +1214,11 @@ do_feature_check(const struct wim_features *required_features, /* File attributes. */ if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) { - /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE. - * We're an archive program, so theoretically we can do what we - * want with it. */ + + if (required_features->readonly_files && + !supported_features->readonly_files) + WARNING("Ignoring FILE_ATTRIBUTE_READONLY of %lu files", + required_features->readonly_files); if (required_features->hidden_files && !supported_features->hidden_files) @@ -1173,6 +1230,10 @@ do_feature_check(const struct wim_features *required_features, WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files", required_features->system_files); + /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE. + * We're an archive program, so theoretically we can do what we + * want with it. */ + if (required_features->compressed_files && !supported_features->compressed_files) WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files", @@ -1389,12 +1450,12 @@ extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees, * subtract from this if hard links are * supported by the extraction mode.) */ ctx->progress.extract.total_bytes = - wim_info_get_image_total_bytes(wim->wim_info, - wim->current_image); + xml_get_image_total_bytes(wim->xml_info, + wim->current_image); if (ctx->supported_features.hard_links) { ctx->progress.extract.total_bytes -= - wim_info_get_image_hard_link_bytes(wim->wim_info, - wim->current_image); + xml_get_image_hard_link_bytes(wim->xml_info, + wim->current_image); } } @@ -1488,6 +1549,34 @@ check_extract_flags(const WIMStruct *wim, int *extract_flags_p) #endif } + if (extract_flags & (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | + WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | + WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | + WIMLIB_EXTRACT_FLAG_COMPACT_LZX)) + { + #ifdef __WIN32__ + int count = 0; + count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K) != 0); + count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K) != 0); + count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K) != 0); + count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_LZX) != 0); + if (count != 1) { + ERROR("Only one compression format can be specified " + "for compact-mode extraction!"); + return WIMLIB_ERR_INVALID_PARAM; + } + if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) { + ERROR("Compact-mode extraction and WIMBoot-mode " + "extraction are mutually exclusive!"); + return WIMLIB_ERR_INVALID_PARAM; + } + #else + ERROR("Compact-mode extraction (System Compression) " + "is only supported on Windows!"); + return WIMLIB_ERR_UNSUPPORTED; + #endif + } + if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX | @@ -1674,7 +1763,8 @@ image_name_ok_as_dir(const tchar *image_name) return image_name && *image_name && !tstrpbrk(image_name, filename_forbidden_chars) && tstrcmp(image_name, T(".")) && - tstrcmp(image_name, T("..")); + tstrcmp(image_name, T("..")) && + tstrlen(image_name) <= 128; } /* Extracts all images from the WIM to the directory @target, with the images @@ -1682,9 +1772,8 @@ image_name_ok_as_dir(const tchar *image_name) static int extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags) { - size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20); size_t output_path_len = tstrlen(target); - tchar buf[output_path_len + 1 + image_name_max_len + 1]; + tchar buf[output_path_len + 1 + 128 + 1]; int ret; int image; const tchar *image_name; @@ -1840,7 +1929,7 @@ wimlib_extract_image_from_pipe_with_progress(int pipe_fd, if (ret) goto out_wimlib_free; - if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) { + if (xml_get_image_count(pwm->xml_info) != pwm->hdr.image_count) { ERROR("Image count in XML data is not the same as in WIM header."); ret = WIMLIB_ERR_IMAGE_COUNT; goto out_wimlib_free; @@ -1873,52 +1962,9 @@ 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 wim_image_metadata *imd; - struct wim_reshdr reshdr; - struct wim_resource_descriptor *metadata_rdesc; - - imd = pwm->image_metadata[i - 1]; - - ret = WIMLIB_ERR_NOMEM; - imd->metadata_blob = new_blob_descriptor(); - if (!imd->metadata_blob) - goto out_wimlib_free; - - imd->metadata_blob->is_metadata = 1; - - ret = read_pwm_blob_header(pwm, imd->metadata_blob->hash, - &reshdr, NULL); + ret = handle_pwm_metadata_resource(pwm, i, i == image); if (ret) goto out_wimlib_free; - - if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) { - ERROR("Expected metadata resource, but found " - "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. */ - ret = read_metadata_resource(imd); - if (ret) - goto out_wimlib_free; - imd->modified = 1; - } else { - /* Metadata resource is not for the image being - * extracted. Skip over it. */ - ret = skip_wim_resource(metadata_rdesc); - if (ret) - goto out_wimlib_free; - } } /* Extract the image. */ extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;