]> wimlib.net Git - wimlib/blobdiff - src/extract.c
extract: track READONLY attribute in supported_features
[wimlib] / src / extract.c
index b14489379d0275e3b734e73c81e4af02fb7a2a70..c1a5adb62aa095417b3b74874385c96456dfa86e 100644 (file)
 #include "wimlib/metadata.h"
 #include "wimlib/pathlist.h"
 #include "wimlib/paths.h"
+#include "wimlib/pattern.h"
 #include "wimlib/reparse.h"
 #include "wimlib/resource.h"
 #include "wimlib/security.h"
 #include "wimlib/unix_data.h"
-#include "wimlib/wildcard.h"
 #include "wimlib/wim.h"
 #include "wimlib/win32.h" /* for realpath() equivalent */
 #include "wimlib/xml.h"
         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.  */
@@ -96,7 +101,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;
@@ -104,13 +109,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);
 }
@@ -134,152 +139,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_nonsolid_wim_resource(blob, rdesc);
-       blob->is_metadata = (rdesc->flags & WIM_RESHDR_FLAG_METADATA) != 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->is_metadata
-                   && (needed_blob = lookup_blob(blob_table, found_blob->hash))
-                   && (needed_blob->out_refcnt))
-               {
-                       blob_unset_is_located_in_wim_resource(found_blob);
-                       blob_set_is_located_in_nonsolid_wim_resource(needed_blob, rdesc);
+               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);
 
-                       ret = (*cbs->begin_blob)(needed_blob,
-                                                cbs->begin_blob_ctx);
-                       if (ret) {
-                               blob_unset_is_located_in_wim_resource(needed_blob);
-                               goto out;
-                       }
+                       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 = extract_blob(needed_blob, needed_blob->size,
-                                          cbs->consume_chunk,
-                                          cbs->consume_chunk_ctx);
+                       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 = (*cbs->end_blob)(needed_blob, ret,
-                                              cbs->end_blob_ctx);
-                       blob_unset_is_located_in_wim_resource(needed_blob);
+                       continue;
+               }
+
+               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
@@ -331,8 +308,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
@@ -356,10 +333,7 @@ extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx)
                        const struct wim_inode *inode = targets[i].inode;
                        const struct wim_dentry *dentry;
 
-                       list_for_each_entry(dentry,
-                                           &inode->i_extraction_aliases,
-                                           d_extraction_alias_node)
-                       {
+                       inode_for_each_extraction_alias(dentry, inode) {
                                progress->extract.completed_bytes += size;
                                if (ctx->cur_blob_offset == ctx->cur_blob->size)
                                        progress->extract.completed_streams++;
@@ -372,30 +346,9 @@ extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx)
                if (ret)
                        return ret;
 
-               if (progress->extract.completed_bytes >=
-                   progress->extract.total_bytes)
-               {
-                       ctx->next_progress = UINT64_MAX;
-               } else {
-                       /* Send new message as soon as another 1/128 of the
-                        * total has been extracted.  (Arbitrary number.)  */
-                       ctx->next_progress =
-                               progress->extract.completed_bytes +
-                                       progress->extract.total_bytes / 128;
-
-                       /* ... Unless that would be more than 5000000 bytes, in
-                        * which case send the next after the next 5000000
-                        * bytes.  (Another arbitrary number.)  */
-                       if (progress->extract.completed_bytes + 5000000 <
-                           ctx->next_progress)
-                               ctx->next_progress =
-                                       progress->extract.completed_bytes + 5000000;
-
-                       /* ... But always send a message as soon as we're
-                        * completely done.  */
-                       if (progress->extract.total_bytes < ctx->next_progress)
-                               ctx->next_progress = progress->extract.total_bytes;
-               }
+               set_next_progress(progress->extract.completed_bytes,
+                                 progress->extract.total_bytes,
+                                 &ctx->next_progress);
        }
 
        if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
@@ -407,67 +360,36 @@ 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);
 }
 
+/* Copy the blob's data from the temporary file to each of its targets.
+ *
+ * This is executed only in the very uncommon case that a blob is being
+ * extracted to more than MAX_OPEN_FILES targets!  */
 static int
-extract_from_tmpfile(const tchar *tmpfile_name, struct apply_ctx *ctx)
+extract_from_tmpfile(const tchar *tmpfile_name,
+                    const struct blob_descriptor *orig_blob,
+                    const struct read_blob_callbacks *cbs)
 {
        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 blob_extraction_target *targets = blob_extraction_targets(orig_blob);
        int ret;
-       const u32 orig_refcnt = orig_blob->out_refcnt;
-
-       BUILD_BUG_ON(MAX_OPEN_FILES <
-                    ARRAY_LEN(orig_blob->inline_blob_extraction_targets));
-
-       struct blob_extraction_target *targets = orig_blob->blob_extraction_targets;
-
-       /* Copy the blob's data from the temporary file to each of its targets.
-        *
-        * This is executed only in the very uncommon case that a blob is being
-        * extracted to more than MAX_OPEN_FILES targets!  */
 
        memcpy(&tmpfile_blob, orig_blob, sizeof(struct blob_descriptor));
        tmpfile_blob.blob_location = BLOB_IN_FILE_ON_DISK;
-       tmpfile_blob.file_on_disk = ctx->tmpfile_name;
-       ret = 0;
-       for (u32 i = 0; i < orig_refcnt; i++) {
-
-               /* Note: it usually doesn't matter whether we pass the original
-                * 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
-                * 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.
-                */
-
-               orig_blob->out_refcnt = 1;
-               orig_blob->inline_blob_extraction_targets[0] = targets[i];
-
-               ret = (*cbs->begin_blob)(orig_blob, cbs->begin_blob_ctx);
-               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);
+       tmpfile_blob.file_on_disk = (tchar *)tmpfile_name;
+       tmpfile_blob.out_refcnt = 1;
 
-               ret = (*cbs->end_blob)(orig_blob, ret, cbs->end_blob_ctx);
+       for (u32 i = 0; i < orig_blob->out_refcnt; i++) {
+               tmpfile_blob.inline_blob_extraction_targets[0] = targets[i];
+               ret = read_blob_with_cbs(&tmpfile_blob, cbs);
                if (ret)
-                       break;
+                       return ret;
        }
-       FREE(targets);
-       orig_blob->out_refcnt = 0;
-       return ret;
+       return 0;
 }
 
 static int
@@ -478,15 +400,15 @@ end_extract_blob_wrapper(struct blob_descriptor *blob, int status, void *_ctx)
        if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
                filedes_close(&ctx->tmpfile_fd);
                if (!status)
-                       status = extract_from_tmpfile(ctx->tmpfile_name, ctx);
+                       status = extract_from_tmpfile(ctx->tmpfile_name, blob,
+                                                     ctx->saved_cbs);
                filedes_invalidate(&ctx->tmpfile_fd);
                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);
 }
 
 /*
@@ -497,8 +419,7 @@ 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.
@@ -508,16 +429,13 @@ end_extract_blob_wrapper(struct blob_descriptor *blob, int status, void *_ctx)
  * 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) {
@@ -561,7 +479,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
@@ -588,14 +506,14 @@ remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
 {
        size_t i, j = 0;
        for (i = 0; i < num_trees; i++) {
-               if (!trees[i]->tmp_flag) {
+               if (!trees[i]->d_tmp_flag) {
                        /* Found distinct dentry.  */
-                       trees[i]->tmp_flag = 1;
+                       trees[i]->d_tmp_flag = 1;
                        trees[j++] = trees[i];
                }
        }
        for (i = 0; i < j; i++)
-               trees[i]->tmp_flag = 0;
+               trees[i]->d_tmp_flag = 0;
        return j;
 }
 
@@ -609,23 +527,23 @@ remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
 {
        size_t i, j = 0;
        for (i = 0; i < num_trees; i++)
-               trees[i]->tmp_flag = 1;
+               trees[i]->d_tmp_flag = 1;
        for (i = 0; i < num_trees; i++) {
                struct wim_dentry *d = trees[i];
                while (!dentry_is_root(d)) {
                        d = d->d_parent;
-                       if (d->tmp_flag)
+                       if (d->d_tmp_flag)
                                goto tree_contained;
                }
                trees[j++] = trees[i];
                continue;
 
        tree_contained:
-               trees[i]->tmp_flag = 0;
+               trees[i]->d_tmp_flag = 0;
        }
 
        for (i = 0; i < j; i++)
-               trees[i]->tmp_flag = 0;
+               trees[i]->d_tmp_flag = 0;
        return j;
 }
 
@@ -646,8 +564,10 @@ dentry_reset_extraction_list_node(struct wim_dentry *dentry)
 static int
 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
 {
-       list_del(&dentry->d_extraction_list_node);
-       dentry_reset_extraction_list_node(dentry);
+       if (will_extract_dentry(dentry)) {
+               list_del(&dentry->d_extraction_list_node);
+               dentry_reset_extraction_list_node(dentry);
+       }
        return 0;
 }
 
@@ -709,7 +629,7 @@ destroy_dentry_list(struct list_head *dentry_list)
                dentry_reset_extraction_list_node(dentry);
                inode->i_visited = 0;
                inode->i_can_externally_back = 0;
-               if ((void *)dentry->d_extraction_name != (void *)dentry->file_name)
+               if ((void *)dentry->d_extraction_name != (void *)dentry->d_name)
                        FREE(dentry->d_extraction_name);
                dentry->d_extraction_name = NULL;
                dentry->d_extraction_name_nchars = 0;
@@ -740,19 +660,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
@@ -760,16 +681,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;
 }
 
@@ -784,8 +695,8 @@ dentry_calculate_extraction_name(struct wim_dentry *dentry,
 
 #ifdef WITH_NTFS_3G
        if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
-               dentry->d_extraction_name = dentry->file_name;
-               dentry->d_extraction_name_nchars = dentry->file_name_nbytes /
+               dentry->d_extraction_name = dentry->d_name;
+               dentry->d_extraction_name_nchars = dentry->d_name_nbytes /
                                                   sizeof(utf16lechar);
                return 0;
        }
@@ -818,12 +729,13 @@ dentry_calculate_extraction_name(struct wim_dentry *dentry,
                }
        }
 
-       if (file_name_valid(dentry->file_name, dentry->file_name_nbytes / 2, false)) {
-               ret = utf16le_get_tstr(dentry->file_name,
-                                      dentry->file_name_nbytes,
+       if (file_name_valid(dentry->d_name, dentry->d_name_nbytes / 2, false)) {
+               size_t nbytes = 0;
+               ret = utf16le_get_tstr(dentry->d_name,
+                                      dentry->d_name_nbytes,
                                       (const tchar **)&dentry->d_extraction_name,
-                                      &dentry->d_extraction_name_nchars);
-               dentry->d_extraction_name_nchars /= sizeof(tchar);
+                                      &nbytes);
+               dentry->d_extraction_name_nchars = nbytes / sizeof(tchar);
                return ret;
        } else {
                if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
@@ -843,16 +755,16 @@ dentry_calculate_extraction_name(struct wim_dentry *dentry,
 
 out_replace:
        {
-               utf16lechar utf16_name_copy[dentry->file_name_nbytes / 2];
+               utf16lechar utf16_name_copy[dentry->d_name_nbytes / 2];
 
-               memcpy(utf16_name_copy, dentry->file_name, dentry->file_name_nbytes);
-               file_name_valid(utf16_name_copy, dentry->file_name_nbytes / 2, true);
+               memcpy(utf16_name_copy, dentry->d_name, dentry->d_name_nbytes);
+               file_name_valid(utf16_name_copy, dentry->d_name_nbytes / 2, true);
 
                const tchar *tchar_name;
                size_t tchar_nchars;
 
                ret = utf16le_get_tstr(utf16_name_copy,
-                                      dentry->file_name_nbytes,
+                                      dentry->d_name_nbytes,
                                       &tchar_name, &tchar_nchars);
                if (ret)
                        return ret;
@@ -1066,10 +978,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?  */
@@ -1146,30 +1059,28 @@ static void
 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
 {
        struct wim_dentry *dentry;
-       struct wim_inode *inode;
+
+       list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
+               dentry->d_inode->i_first_extraction_alias = NULL;
 
        list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
-               inode = dentry->d_inode;
-               if (!inode->i_visited)
-                       INIT_LIST_HEAD(&inode->i_extraction_aliases);
-               list_add_tail(&dentry->d_extraction_alias_node,
-                             &inode->i_extraction_aliases);
-               inode->i_visited = 1;
+               dentry->d_next_extraction_alias = dentry->d_inode->i_first_extraction_alias;
+               dentry->d_inode->i_first_extraction_alias = dentry;
        }
-       list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
-               dentry->d_inode->i_visited = 0;
 }
 
 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) {
@@ -1191,7 +1102,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++;
@@ -1247,9 +1158,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)
@@ -1261,6 +1174,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",
@@ -1477,12 +1394,12 @@ extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
                 * subtract <HARDLINKBYTES> 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);
                }
        }
 
@@ -1576,6 +1493,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 |
@@ -1592,22 +1537,6 @@ check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
        return 0;
 }
 
-static u32
-get_wildcard_flags(int extract_flags)
-{
-       u32 wildcard_flags = 0;
-
-       if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB)
-               wildcard_flags |= WILDCARD_FLAG_ERROR_IF_NO_MATCH;
-       else
-               wildcard_flags |= WILDCARD_FLAG_WARN_IF_NO_MATCH;
-
-       if (default_ignore_case)
-               wildcard_flags |= WILDCARD_FLAG_CASE_INSENSITIVE;
-
-       return wildcard_flags;
-}
-
 struct append_dentry_ctx {
        struct wim_dentry **dentries;
        size_t num_dentries;
@@ -1636,6 +1565,31 @@ append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
        return 0;
 }
 
+/* Append dentries matched by a path which can contain wildcard characters.  */
+static int
+append_matched_dentries(WIMStruct *wim, const tchar *orig_pattern,
+                       int extract_flags, struct append_dentry_ctx *ctx)
+{
+       const size_t count_before = ctx->num_dentries;
+       tchar *pattern;
+       int ret;
+
+       pattern = canonicalize_wim_path(orig_pattern);
+       if (!pattern)
+               return WIMLIB_ERR_NOMEM;
+       ret = expand_path_pattern(wim_get_current_root_dentry(wim), pattern,
+                                 append_dentry_cb, ctx);
+       FREE(pattern);
+       if (ret || ctx->num_dentries > count_before)
+               return ret;
+       if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB) {
+               ERROR("No matches for path pattern \"%"TS"\"", orig_pattern);
+               return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
+       }
+       WARNING("No matches for path pattern \"%"TS"\"", orig_pattern);
+       return 0;
+}
+
 static int
 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
                        const tchar * const *paths, size_t num_paths,
@@ -1678,20 +1632,10 @@ do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
                        .num_alloc_dentries = 0,
                };
 
-               u32 wildcard_flags = get_wildcard_flags(extract_flags);
-
                for (size_t i = 0; i < num_paths; i++) {
-                       tchar *path = canonicalize_wim_path(paths[i]);
-                       if (path == NULL) {
-                               ret = WIMLIB_ERR_NOMEM;
-                               trees = append_dentry_ctx.dentries;
-                               goto out_free_trees;
-                       }
-                       ret = expand_wildcard(wim, path,
-                                             append_dentry_cb,
-                                             &append_dentry_ctx,
-                                             wildcard_flags);
-                       FREE(path);
+                       ret = append_matched_dentries(wim, paths[i],
+                                                     extract_flags,
+                                                     &append_dentry_ctx);
                        if (ret) {
                                trees = append_dentry_ctx.dentries;
                                goto out_free_trees;
@@ -1763,7 +1707,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
@@ -1771,9 +1716,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;
@@ -1912,25 +1856,24 @@ 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.is_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;
 
-               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;
@@ -1963,44 +1906,45 @@ 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->is_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);
+               pwm->refcnt++;
+
                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.  */