]> wimlib.net Git - wimlib/blob - src/extract.c
resource: pass blob and offset to consume_chunk
[wimlib] / src / extract.c
1 /*
2  * extract.c
3  *
4  * Support for extracting WIM images, or files or directories contained in a WIM
5  * image.
6  */
7
8 /*
9  * Copyright (C) 2012-2016 Eric Biggers
10  *
11  * This file is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the Free
13  * Software Foundation; either version 3 of the License, or (at your option) any
14  * later version.
15  *
16  * This file is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this file; if not, see http://www.gnu.org/licenses/.
23  */
24
25 /*
26  * This file provides the API functions wimlib_extract_image(),
27  * wimlib_extract_image_from_pipe(), wimlib_extract_paths(), and
28  * wimlib_extract_pathlist().  Internally, all end up calling
29  * do_wimlib_extract_paths() and extract_trees().
30  *
31  * Although wimlib supports multiple extraction modes/backends (NTFS-3G, UNIX,
32  * Win32), this file does not itself have code to extract files or directories
33  * to any specific target; instead, it handles generic functionality and relies
34  * on lower-level callback functions declared in `struct apply_operations' to do
35  * the actual extraction.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #  include "config.h"
40 #endif
41
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47
48 #include "wimlib/apply.h"
49 #include "wimlib/assert.h"
50 #include "wimlib/blob_table.h"
51 #include "wimlib/dentry.h"
52 #include "wimlib/encoding.h"
53 #include "wimlib/endianness.h"
54 #include "wimlib/error.h"
55 #include "wimlib/metadata.h"
56 #include "wimlib/object_id.h"
57 #include "wimlib/pathlist.h"
58 #include "wimlib/paths.h"
59 #include "wimlib/pattern.h"
60 #include "wimlib/reparse.h"
61 #include "wimlib/resource.h"
62 #include "wimlib/security.h"
63 #include "wimlib/unix_data.h"
64 #include "wimlib/wim.h"
65 #include "wimlib/win32.h" /* for realpath() equivalent */
66 #include "wimlib/xml.h"
67
68 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE   0x80000000
69 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE   0x40000000
70
71 /* Keep in sync with wimlib.h  */
72 #define WIMLIB_EXTRACT_MASK_PUBLIC                              \
73         (WIMLIB_EXTRACT_FLAG_NTFS                       |       \
74          WIMLIB_EXTRACT_FLAG_UNIX_DATA                  |       \
75          WIMLIB_EXTRACT_FLAG_NO_ACLS                    |       \
76          WIMLIB_EXTRACT_FLAG_STRICT_ACLS                |       \
77          WIMLIB_EXTRACT_FLAG_RPFIX                      |       \
78          WIMLIB_EXTRACT_FLAG_NORPFIX                    |       \
79          WIMLIB_EXTRACT_FLAG_TO_STDOUT                  |       \
80          WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES  |       \
81          WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS         |       \
82          WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS          |       \
83          WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES         |       \
84          WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS            |       \
85          WIMLIB_EXTRACT_FLAG_GLOB_PATHS                 |       \
86          WIMLIB_EXTRACT_FLAG_STRICT_GLOB                |       \
87          WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES              |       \
88          WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE  |       \
89          WIMLIB_EXTRACT_FLAG_WIMBOOT                    |       \
90          WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K           |       \
91          WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K           |       \
92          WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K          |       \
93          WIMLIB_EXTRACT_FLAG_COMPACT_LZX                        \
94          )
95
96 /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
97  * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA.  */
98 int
99 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
100 {
101         ctx->count_until_file_progress = 500;  /* Arbitrary value to limit calls  */
102         return extract_progress(ctx, msg);
103 }
104
105 static int
106 start_file_phase(struct apply_ctx *ctx, u64 end_file_count, enum wimlib_progress_msg msg)
107 {
108         ctx->progress.extract.current_file_count = 0;
109         ctx->progress.extract.end_file_count = end_file_count;
110         return do_file_extract_progress(ctx, msg);
111 }
112
113 int
114 start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count)
115 {
116         return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
117 }
118
119 int
120 start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count)
121 {
122         return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
123 }
124
125 static int
126 end_file_phase(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
127 {
128         ctx->progress.extract.current_file_count = ctx->progress.extract.end_file_count;
129         return do_file_extract_progress(ctx, msg);
130 }
131
132 int
133 end_file_structure_phase(struct apply_ctx *ctx)
134 {
135         return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
136 }
137
138 int
139 end_file_metadata_phase(struct apply_ctx *ctx)
140 {
141         return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
142 }
143
144 #define PWM_FOUND_WIM_HDR (-1)
145
146 /* Read the header for a blob in a pipable WIM.  If @pwm_hdr_ret is not NULL,
147  * also look for a pipable WIM header and return PWM_FOUND_WIM_HDR if found.  */
148 static int
149 read_pwm_blob_header(WIMStruct *pwm, u8 hash_ret[SHA1_HASH_SIZE],
150                      struct wim_reshdr *reshdr_ret,
151                      struct wim_header_disk *pwm_hdr_ret)
152 {
153         int ret;
154         struct pwm_blob_hdr blob_hdr;
155         u64 magic;
156
157         ret = full_read(&pwm->in_fd, &blob_hdr, sizeof(blob_hdr));
158         if (unlikely(ret))
159                 goto read_error;
160
161         magic = le64_to_cpu(blob_hdr.magic);
162
163         if (magic == PWM_MAGIC && pwm_hdr_ret != NULL) {
164                 memcpy(pwm_hdr_ret, &blob_hdr, sizeof(blob_hdr));
165                 ret = full_read(&pwm->in_fd,
166                                 (u8 *)pwm_hdr_ret + sizeof(blob_hdr),
167                                 sizeof(*pwm_hdr_ret) - sizeof(blob_hdr));
168                 if (unlikely(ret))
169                         goto read_error;
170                 return PWM_FOUND_WIM_HDR;
171         }
172
173         if (unlikely(magic != PWM_BLOB_MAGIC)) {
174                 ERROR("Data read on pipe is invalid (expected blob header)");
175                 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
176         }
177
178         copy_hash(hash_ret, blob_hdr.hash);
179
180         reshdr_ret->size_in_wim = 0; /* Not available  */
181         reshdr_ret->flags = le32_to_cpu(blob_hdr.flags);
182         reshdr_ret->offset_in_wim = pwm->in_fd.offset;
183         reshdr_ret->uncompressed_size = le64_to_cpu(blob_hdr.uncompressed_size);
184
185         if (unlikely(reshdr_ret->uncompressed_size == 0)) {
186                 ERROR("Data read on pipe is invalid (resource is of 0 size)");
187                 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
188         }
189
190         return 0;
191
192 read_error:
193         if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE)
194                 ERROR("The pipe ended before all needed data was sent!");
195         else
196                 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
197         return ret;
198 }
199
200 static int
201 read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
202 {
203         int ret;
204         u8 hash[SHA1_HASH_SIZE];
205         struct wim_reshdr reshdr;
206         struct wim_header_disk pwm_hdr;
207         struct wim_resource_descriptor rdesc;
208         struct blob_descriptor *blob;
209
210         copy_guid(ctx->progress.extract.guid, ctx->wim->hdr.guid);
211         ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
212         ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
213         ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
214         if (ret)
215                 return ret;
216
217         while (ctx->num_blobs_remaining) {
218
219                 ret = read_pwm_blob_header(ctx->wim, hash, &reshdr, &pwm_hdr);
220
221                 if (ret == PWM_FOUND_WIM_HDR) {
222                         u16 part_number = le16_to_cpu(pwm_hdr.part_number);
223                         u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
224
225                         if (part_number == ctx->progress.extract.part_number &&
226                             total_parts == ctx->progress.extract.total_parts &&
227                             guids_equal(pwm_hdr.guid, ctx->progress.extract.guid))
228                                 continue;
229
230                         copy_guid(ctx->progress.extract.guid, pwm_hdr.guid);
231                         ctx->progress.extract.part_number = part_number;
232                         ctx->progress.extract.total_parts = total_parts;
233                         ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
234                         if (ret)
235                                 return ret;
236
237                         continue;
238                 }
239
240                 if (ret)
241                         return ret;
242
243                 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)
244                     && (blob = lookup_blob(ctx->wim->blob_table, hash))
245                     && (blob->out_refcnt))
246                 {
247                         wim_reshdr_to_desc_and_blob(&reshdr, ctx->wim, &rdesc, blob);
248                         ret = read_blob_with_sha1(blob, cbs);
249                         blob_unset_is_located_in_wim_resource(blob);
250                         if (ret)
251                                 return ret;
252                         ctx->num_blobs_remaining--;
253                 } else {
254                         wim_reshdr_to_desc(&reshdr, ctx->wim, &rdesc);
255                         ret = skip_wim_resource(&rdesc);
256                         if (ret)
257                                 return ret;
258                 }
259         }
260
261         return 0;
262 }
263
264 static int
265 handle_pwm_metadata_resource(WIMStruct *pwm, int image, bool is_needed)
266 {
267         struct blob_descriptor *blob;
268         struct wim_reshdr reshdr;
269         struct wim_resource_descriptor *rdesc;
270         int ret;
271
272         ret = WIMLIB_ERR_NOMEM;
273         blob = new_blob_descriptor();
274         if (!blob)
275                 goto out;
276
277         ret = read_pwm_blob_header(pwm, blob->hash, &reshdr, NULL);
278         if (ret)
279                 goto out;
280
281         ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
282         if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
283                 ERROR("Expected metadata resource, but found non-metadata "
284                       "resource");
285                 goto out;
286         }
287
288         ret = WIMLIB_ERR_NOMEM;
289         rdesc = MALLOC(sizeof(*rdesc));
290         if (!rdesc)
291                 goto out;
292
293         wim_reshdr_to_desc_and_blob(&reshdr, pwm, rdesc, blob);
294         pwm->refcnt++;
295
296         ret = WIMLIB_ERR_NOMEM;
297         pwm->image_metadata[image - 1] = new_unloaded_image_metadata(blob);
298         if (!pwm->image_metadata[image - 1])
299                 goto out;
300         blob = NULL;
301
302         /* If the metadata resource is for the image being extracted, then parse
303          * it and save the metadata in memory.  Otherwise, skip over it.  */
304         if (is_needed)
305                 ret = select_wim_image(pwm, image);
306         else
307                 ret = skip_wim_resource(rdesc);
308 out:
309         free_blob_descriptor(blob);
310         return ret;
311 }
312
313 /* Creates a temporary file opened for writing.  The open file descriptor is
314  * returned in @fd_ret and its name is returned in @name_ret (dynamically
315  * allocated).  */
316 static int
317 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
318 {
319         tchar *name;
320         int raw_fd;
321
322 #ifdef __WIN32__
323 retry:
324         name = _wtempnam(NULL, L"wimlib");
325         if (!name) {
326                 ERROR_WITH_ERRNO("Failed to create temporary filename");
327                 return WIMLIB_ERR_NOMEM;
328         }
329         raw_fd = _wopen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY |
330                         _O_SHORT_LIVED, 0600);
331         if (raw_fd < 0 && errno == EEXIST) {
332                 FREE(name);
333                 goto retry;
334         }
335 #else /* __WIN32__ */
336         const char *tmpdir = getenv("TMPDIR");
337         if (!tmpdir)
338                 tmpdir = P_tmpdir;
339         name = MALLOC(strlen(tmpdir) + 1 + 6 + 6 + 1);
340         if (!name)
341                 return WIMLIB_ERR_NOMEM;
342         sprintf(name, "%s/wimlibXXXXXX", tmpdir);
343         raw_fd = mkstemp(name);
344 #endif /* !__WIN32__ */
345
346         if (raw_fd < 0) {
347                 ERROR_WITH_ERRNO("Failed to create temporary file "
348                                  "\"%"TS"\"", name);
349                 FREE(name);
350                 return WIMLIB_ERR_OPEN;
351         }
352
353         filedes_init(fd_ret, raw_fd);
354         *name_ret = name;
355         return 0;
356 }
357
358 static int
359 begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
360 {
361         struct apply_ctx *ctx = _ctx;
362
363         if (unlikely(blob->out_refcnt > MAX_OPEN_FILES))
364                 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
365
366         return call_begin_blob(blob, ctx->saved_cbs);
367 }
368
369 static int
370 extract_chunk(const struct blob_descriptor *blob, u64 offset,
371               const void *chunk, size_t size, void *_ctx)
372 {
373         struct apply_ctx *ctx = _ctx;
374         union wimlib_progress_info *progress = &ctx->progress;
375         bool last = (offset + size == blob->size);
376         int ret;
377
378         if (likely(ctx->supported_features.hard_links)) {
379                 progress->extract.completed_bytes +=
380                         (u64)size * blob->out_refcnt;
381                 if (last)
382                         progress->extract.completed_streams += blob->out_refcnt;
383         } else {
384                 const struct blob_extraction_target *targets =
385                         blob_extraction_targets(blob);
386                 for (u32 i = 0; i < blob->out_refcnt; i++) {
387                         const struct wim_inode *inode = targets[i].inode;
388                         const struct wim_dentry *dentry;
389
390                         inode_for_each_extraction_alias(dentry, inode) {
391                                 progress->extract.completed_bytes += size;
392                                 if (last)
393                                         progress->extract.completed_streams++;
394                         }
395                 }
396         }
397         if (progress->extract.completed_bytes >= ctx->next_progress) {
398
399                 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
400                 if (ret)
401                         return ret;
402
403                 set_next_progress(progress->extract.completed_bytes,
404                                   progress->extract.total_bytes,
405                                   &ctx->next_progress);
406         }
407
408         if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
409                 /* Just extracting to temporary file for now.  */
410                 ret = full_write(&ctx->tmpfile_fd, chunk, size);
411                 if (ret) {
412                         ERROR_WITH_ERRNO("Error writing data to "
413                                          "temporary file \"%"TS"\"",
414                                          ctx->tmpfile_name);
415                 }
416                 return ret;
417         }
418
419         return call_continue_blob(blob, offset, chunk, size, ctx->saved_cbs);
420 }
421
422 /* Copy the blob's data from the temporary file to each of its targets.
423  *
424  * This is executed only in the very uncommon case that a blob is being
425  * extracted to more than MAX_OPEN_FILES targets!  */
426 static int
427 extract_from_tmpfile(const tchar *tmpfile_name,
428                      const struct blob_descriptor *orig_blob,
429                      const struct read_blob_callbacks *cbs)
430 {
431         struct blob_descriptor tmpfile_blob;
432         const struct blob_extraction_target *targets = blob_extraction_targets(orig_blob);
433         int ret;
434
435         memcpy(&tmpfile_blob, orig_blob, sizeof(struct blob_descriptor));
436         tmpfile_blob.blob_location = BLOB_IN_FILE_ON_DISK;
437         tmpfile_blob.file_on_disk = (tchar *)tmpfile_name;
438         tmpfile_blob.out_refcnt = 1;
439
440         for (u32 i = 0; i < orig_blob->out_refcnt; i++) {
441                 tmpfile_blob.inline_blob_extraction_targets[0] = targets[i];
442                 ret = read_blob_with_cbs(&tmpfile_blob, cbs);
443                 if (ret)
444                         return ret;
445         }
446         return 0;
447 }
448
449 static int
450 end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
451 {
452         struct apply_ctx *ctx = _ctx;
453
454         if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
455                 filedes_close(&ctx->tmpfile_fd);
456                 if (!status)
457                         status = extract_from_tmpfile(ctx->tmpfile_name, blob,
458                                                       ctx->saved_cbs);
459                 filedes_invalidate(&ctx->tmpfile_fd);
460                 tunlink(ctx->tmpfile_name);
461                 FREE(ctx->tmpfile_name);
462                 return status;
463         }
464
465         return call_end_blob(blob, status, ctx->saved_cbs);
466 }
467
468 /*
469  * Read the list of blobs to extract and feed their data into the specified
470  * callback functions.
471  *
472  * This handles checksumming each blob.
473  *
474  * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
475  *
476  * This also works if the WIM is being read from a pipe.
477  *
478  * This also will split up blobs that will need to be extracted to more than
479  * MAX_OPEN_FILES locations, as measured by the 'out_refcnt' of each blob.
480  * Therefore, the apply_operations implementation need not worry about running
481  * out of file descriptors, unless it might open more than one file descriptor
482  * per 'blob_extraction_target' (e.g. Win32 currently might because the
483  * destination file system might not support hard links).
484  */
485 int
486 extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
487 {
488         struct read_blob_callbacks wrapper_cbs = {
489                 .begin_blob     = begin_extract_blob,
490                 .continue_blob  = extract_chunk,
491                 .end_blob       = end_extract_blob,
492                 .ctx            = ctx,
493         };
494         ctx->saved_cbs = cbs;
495         if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
496                 return read_blobs_from_pipe(ctx, &wrapper_cbs);
497         } else {
498                 return read_blob_list(&ctx->blob_list,
499                                       offsetof(struct blob_descriptor,
500                                                extraction_list),
501                                       &wrapper_cbs, VERIFY_BLOB_HASHES);
502         }
503 }
504
505 /* Extract a WIM dentry to standard output.
506  *
507  * This obviously doesn't make sense in all cases.  We return an error if the
508  * dentry does not correspond to a regular file.  Otherwise we extract the
509  * unnamed data stream only.  */
510 static int
511 extract_dentry_to_stdout(struct wim_dentry *dentry,
512                          const struct blob_table *blob_table)
513 {
514         struct wim_inode *inode = dentry->d_inode;
515         struct blob_descriptor *blob;
516         struct filedes _stdout;
517
518         if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
519                                    FILE_ATTRIBUTE_DIRECTORY |
520                                    FILE_ATTRIBUTE_ENCRYPTED))
521         {
522                 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
523                       "extracted to standard output", dentry_full_path(dentry));
524                 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
525         }
526
527         blob = inode_get_blob_for_unnamed_data_stream(inode, blob_table);
528         if (!blob) {
529                 const u8 *hash = inode_get_hash_of_unnamed_data_stream(inode);
530                 if (!is_zero_hash(hash))
531                         return blob_not_found_error(inode, hash);
532                 return 0;
533         }
534
535         filedes_init(&_stdout, STDOUT_FILENO);
536         return extract_blob_to_fd(blob, &_stdout);
537 }
538
539 static int
540 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
541                            const struct blob_table *blob_table)
542 {
543         for (size_t i = 0; i < num_dentries; i++) {
544                 int ret = extract_dentry_to_stdout(dentries[i], blob_table);
545                 if (ret)
546                         return ret;
547         }
548         return 0;
549 }
550
551 /**********************************************************************/
552
553 /*
554  * Removes duplicate dentries from the array.
555  *
556  * Returns the new number of dentries, packed at the front of the array.
557  */
558 static size_t
559 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
560 {
561         size_t i, j = 0;
562         for (i = 0; i < num_trees; i++) {
563                 if (!trees[i]->d_tmp_flag) {
564                         /* Found distinct dentry.  */
565                         trees[i]->d_tmp_flag = 1;
566                         trees[j++] = trees[i];
567                 }
568         }
569         for (i = 0; i < j; i++)
570                 trees[i]->d_tmp_flag = 0;
571         return j;
572 }
573
574 /*
575  * Remove dentries that are descendants of other dentries in the array.
576  *
577  * Returns the new number of dentries, packed at the front of the array.
578  */
579 static size_t
580 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
581 {
582         size_t i, j = 0;
583         for (i = 0; i < num_trees; i++)
584                 trees[i]->d_tmp_flag = 1;
585         for (i = 0; i < num_trees; i++) {
586                 struct wim_dentry *d = trees[i];
587                 while (!dentry_is_root(d)) {
588                         d = d->d_parent;
589                         if (d->d_tmp_flag)
590                                 goto tree_contained;
591                 }
592                 trees[j++] = trees[i];
593                 continue;
594
595         tree_contained:
596                 trees[i]->d_tmp_flag = 0;
597         }
598
599         for (i = 0; i < j; i++)
600                 trees[i]->d_tmp_flag = 0;
601         return j;
602 }
603
604 static int
605 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
606 {
607         struct list_head *dentry_list = _dentry_list;
608         list_add_tail(&dentry->d_extraction_list_node, dentry_list);
609         return 0;
610 }
611
612 static void
613 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
614 {
615         dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
616 }
617
618 static int
619 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
620 {
621         if (will_extract_dentry(dentry)) {
622                 list_del(&dentry->d_extraction_list_node);
623                 dentry_reset_extraction_list_node(dentry);
624         }
625         return 0;
626 }
627
628 /*
629  * Build the preliminary list of dentries to be extracted.
630  *
631  * The list maintains the invariant that if d1 and d2 are in the list and d1 is
632  * an ancestor of d2, then d1 appears before d2 in the list.
633  */
634 static void
635 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
636                   size_t num_trees, bool add_ancestors)
637 {
638         INIT_LIST_HEAD(dentry_list);
639
640         /* Add the trees recursively.  */
641         for (size_t i = 0; i < num_trees; i++)
642                 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
643
644         /* If requested, add ancestors of the trees.  */
645         if (add_ancestors) {
646                 for (size_t i = 0; i < num_trees; i++) {
647                         struct wim_dentry *dentry = trees[i];
648                         struct wim_dentry *ancestor;
649                         struct list_head *place_after;
650
651                         if (dentry_is_root(dentry))
652                                 continue;
653
654                         place_after = dentry_list;
655                         ancestor = dentry;
656                         do {
657                                 ancestor = ancestor->d_parent;
658                                 if (will_extract_dentry(ancestor)) {
659                                         place_after = &ancestor->d_extraction_list_node;
660                                         break;
661                                 }
662                         } while (!dentry_is_root(ancestor));
663
664                         ancestor = dentry;
665                         do {
666                                 ancestor = ancestor->d_parent;
667                                 if (will_extract_dentry(ancestor))
668                                         break;
669                                 list_add(&ancestor->d_extraction_list_node, place_after);
670                         } while (!dentry_is_root(ancestor));
671                 }
672         }
673 }
674
675 static void
676 destroy_dentry_list(struct list_head *dentry_list)
677 {
678         struct wim_dentry *dentry, *tmp;
679         struct wim_inode *inode;
680
681         list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
682                 inode = dentry->d_inode;
683                 dentry_reset_extraction_list_node(dentry);
684                 inode->i_visited = 0;
685                 inode->i_can_externally_back = 0;
686                 if ((void *)dentry->d_extraction_name != (void *)dentry->d_name)
687                         FREE(dentry->d_extraction_name);
688                 dentry->d_extraction_name = NULL;
689                 dentry->d_extraction_name_nchars = 0;
690         }
691 }
692
693 static void
694 destroy_blob_list(struct list_head *blob_list)
695 {
696         struct blob_descriptor *blob;
697
698         list_for_each_entry(blob, blob_list, extraction_list)
699                 if (blob->out_refcnt > ARRAY_LEN(blob->inline_blob_extraction_targets))
700                         FREE(blob->blob_extraction_targets);
701 }
702
703 #ifdef __WIN32__
704 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
705 #else
706 static const utf16lechar replacement_char = cpu_to_le16('?');
707 #endif
708
709 static bool
710 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
711 {
712         size_t i;
713
714         if (num_chars == 0)
715                 return true;
716         for (i = 0; i < num_chars; i++) {
717                 switch (le16_to_cpu(name[i])) {
718         #ifdef __WIN32__
719                 case '\x01'...'\x1F':
720                 case '\\':
721                 case ':':
722                 case '*':
723                 case '?':
724                 case '"':
725                 case '<':
726                 case '>':
727                 case '|':
728         #endif
729                 case '/':
730                 case '\0':
731                         if (fix)
732                                 name[i] = replacement_char;
733                         else
734                                 return false;
735                 }
736         }
737
738         return true;
739 }
740
741 static int
742 dentry_calculate_extraction_name(struct wim_dentry *dentry,
743                                  struct apply_ctx *ctx)
744 {
745         int ret;
746
747         if (dentry_is_root(dentry))
748                 return 0;
749
750 #ifdef WITH_NTFS_3G
751         if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
752                 dentry->d_extraction_name = dentry->d_name;
753                 dentry->d_extraction_name_nchars = dentry->d_name_nbytes /
754                                                    sizeof(utf16lechar);
755                 return 0;
756         }
757 #endif
758
759         if (!ctx->supported_features.case_sensitive_filenames) {
760                 struct wim_dentry *other;
761                 dentry_for_each_ci_match(other, dentry) {
762                         if (will_extract_dentry(other)) {
763                                 if (ctx->extract_flags &
764                                     WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
765                                         WARNING("\"%"TS"\" has the same "
766                                                 "case-insensitive name as "
767                                                 "\"%"TS"\"; extracting "
768                                                 "dummy name instead",
769                                                 dentry_full_path(dentry),
770                                                 dentry_full_path(other));
771                                         goto out_replace;
772                                 } else {
773                                         WARNING("Not extracting \"%"TS"\": "
774                                                 "has same case-insensitive "
775                                                 "name as \"%"TS"\"",
776                                                 dentry_full_path(dentry),
777                                                 dentry_full_path(other));
778                                         goto skip_dentry;
779                                 }
780                         }
781                 }
782         }
783
784         if (file_name_valid(dentry->d_name, dentry->d_name_nbytes / 2, false)) {
785                 size_t nbytes = 0;
786                 ret = utf16le_get_tstr(dentry->d_name,
787                                        dentry->d_name_nbytes,
788                                        (const tchar **)&dentry->d_extraction_name,
789                                        &nbytes);
790                 dentry->d_extraction_name_nchars = nbytes / sizeof(tchar);
791                 return ret;
792         } else {
793                 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
794                 {
795                         WARNING("\"%"TS"\" has an invalid filename "
796                                 "that is not supported on this platform; "
797                                 "extracting dummy name instead",
798                                 dentry_full_path(dentry));
799                         goto out_replace;
800                 } else {
801                         WARNING("Not extracting \"%"TS"\": has an invalid filename "
802                                 "that is not supported on this platform",
803                                 dentry_full_path(dentry));
804                         goto skip_dentry;
805                 }
806         }
807
808 out_replace:
809         {
810                 utf16lechar utf16_name_copy[dentry->d_name_nbytes / 2];
811
812                 memcpy(utf16_name_copy, dentry->d_name, dentry->d_name_nbytes);
813                 file_name_valid(utf16_name_copy, dentry->d_name_nbytes / 2, true);
814
815                 const tchar *tchar_name;
816                 size_t tchar_nchars;
817
818                 ret = utf16le_get_tstr(utf16_name_copy,
819                                        dentry->d_name_nbytes,
820                                        &tchar_name, &tchar_nchars);
821                 if (ret)
822                         return ret;
823
824                 tchar_nchars /= sizeof(tchar);
825
826                 size_t fixed_name_num_chars = tchar_nchars;
827                 tchar fixed_name[tchar_nchars + 50];
828
829                 tmemcpy(fixed_name, tchar_name, tchar_nchars);
830                 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
831                                                  T(" (invalid filename #%lu)"),
832                                                  ++ctx->invalid_sequence);
833
834                 utf16le_put_tstr(tchar_name);
835
836                 dentry->d_extraction_name = TSTRDUP(fixed_name);
837                 if (!dentry->d_extraction_name)
838                         return WIMLIB_ERR_NOMEM;
839                 dentry->d_extraction_name_nchars = fixed_name_num_chars;
840         }
841         return 0;
842
843 skip_dentry:
844         for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
845         return 0;
846 }
847
848 /*
849  * Calculate the actual filename component at which each WIM dentry will be
850  * extracted, with special handling for dentries that are unsupported by the
851  * extraction backend or have invalid names.
852  *
853  * ctx->supported_features must be filled in.
854  *
855  * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
856  */
857 static int
858 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
859                                        struct apply_ctx *ctx)
860 {
861         struct list_head *prev, *cur;
862
863         /* Can't use list_for_each_entry() because a call to
864          * dentry_calculate_extraction_name() may delete the current dentry and
865          * its children from the list.  */
866
867         prev = dentry_list;
868         for (;;) {
869                 struct wim_dentry *dentry;
870                 int ret;
871
872                 cur = prev->next;
873                 if (cur == dentry_list)
874                         break;
875
876                 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
877
878                 ret = dentry_calculate_extraction_name(dentry, ctx);
879                 if (ret)
880                         return ret;
881
882                 if (prev->next == cur)
883                         prev = cur;
884                 else
885                         ; /* Current dentry and its children (which follow in
886                              the list) were deleted.  prev stays the same.  */
887         }
888         return 0;
889 }
890
891 static int
892 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
893                        struct blob_table *blob_table)
894 {
895         struct wim_inode *inode = dentry->d_inode;
896         struct blob_descriptor *blob;
897         int ret;
898         bool force = false;
899
900         /* Special case:  when extracting from a pipe, the WIM blob table is
901          * initially empty, so "resolving" an inode's streams is initially not
902          * possible.  However, we still need to keep track of which blobs,
903          * identified by SHA-1 message digests, need to be extracted, so we
904          * "resolve" the inode's streams anyway by allocating a 'struct
905          * blob_descriptor' for each one.  */
906         if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
907                 force = true;
908         ret = inode_resolve_streams(inode, blob_table, force);
909         if (ret)
910                 return ret;
911         for (unsigned i = 0; i < inode->i_num_streams; i++) {
912                 blob = stream_blob_resolved(&inode->i_streams[i]);
913                 if (blob)
914                         blob->out_refcnt = 0;
915         }
916         return 0;
917 }
918
919 /*
920  * For each dentry to be extracted, resolve all streams in the corresponding
921  * inode and set 'out_refcnt' in all referenced blob_descriptors to 0.
922  *
923  * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
924  */
925 static int
926 dentry_list_resolve_streams(struct list_head *dentry_list,
927                             struct apply_ctx *ctx)
928 {
929         struct wim_dentry *dentry;
930         int ret;
931
932         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
933                 ret = dentry_resolve_streams(dentry,
934                                              ctx->extract_flags,
935                                              ctx->wim->blob_table);
936                 if (ret)
937                         return ret;
938         }
939         return 0;
940 }
941
942 static int
943 ref_stream(struct wim_inode_stream *strm, struct wim_dentry *dentry,
944            struct apply_ctx *ctx)
945 {
946         struct wim_inode *inode = dentry->d_inode;
947         struct blob_descriptor *blob = stream_blob_resolved(strm);
948         struct blob_extraction_target *targets;
949
950         if (!blob)
951                 return 0;
952
953         /* Tally the size only for each actual extraction of the stream (not
954          * additional hard links to the inode).  */
955         if (inode->i_visited && ctx->supported_features.hard_links)
956                 return 0;
957
958         ctx->progress.extract.total_bytes += blob->size;
959         ctx->progress.extract.total_streams++;
960
961         if (inode->i_visited)
962                 return 0;
963
964         /* Add each blob to 'ctx->blob_list' only one time, regardless of how
965          * many extraction targets it will have.  */
966         if (blob->out_refcnt == 0) {
967                 list_add_tail(&blob->extraction_list, &ctx->blob_list);
968                 ctx->num_blobs_remaining++;
969         }
970
971         /* Set this stream as an extraction target of 'blob'.  */
972
973         if (blob->out_refcnt < ARRAY_LEN(blob->inline_blob_extraction_targets)) {
974                 targets = blob->inline_blob_extraction_targets;
975         } else {
976                 struct blob_extraction_target *prev_targets;
977                 size_t alloc_blob_extraction_targets;
978
979                 if (blob->out_refcnt == ARRAY_LEN(blob->inline_blob_extraction_targets)) {
980                         prev_targets = NULL;
981                         alloc_blob_extraction_targets = ARRAY_LEN(blob->inline_blob_extraction_targets);
982                 } else {
983                         prev_targets = blob->blob_extraction_targets;
984                         alloc_blob_extraction_targets = blob->alloc_blob_extraction_targets;
985                 }
986
987                 if (blob->out_refcnt == alloc_blob_extraction_targets) {
988                         alloc_blob_extraction_targets *= 2;
989                         targets = REALLOC(prev_targets,
990                                           alloc_blob_extraction_targets *
991                                           sizeof(targets[0]));
992                         if (!targets)
993                                 return WIMLIB_ERR_NOMEM;
994                         if (!prev_targets) {
995                                 memcpy(targets,
996                                        blob->inline_blob_extraction_targets,
997                                        sizeof(blob->inline_blob_extraction_targets));
998                         }
999                         blob->blob_extraction_targets = targets;
1000                         blob->alloc_blob_extraction_targets = alloc_blob_extraction_targets;
1001                 }
1002                 targets = blob->blob_extraction_targets;
1003         }
1004         targets[blob->out_refcnt].inode = inode;
1005         targets[blob->out_refcnt].stream = strm;
1006         blob->out_refcnt++;
1007         return 0;
1008 }
1009
1010 static int
1011 ref_stream_if_needed(struct wim_dentry *dentry, struct wim_inode *inode,
1012                      struct wim_inode_stream *strm, struct apply_ctx *ctx)
1013 {
1014         bool need_stream = false;
1015         switch (strm->stream_type) {
1016         case STREAM_TYPE_DATA:
1017                 if (stream_is_named(strm)) {
1018                         /* Named data stream  */
1019                         if (ctx->supported_features.named_data_streams)
1020                                 need_stream = true;
1021                 } else if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1022                                                     FILE_ATTRIBUTE_ENCRYPTED))
1023                            && !(inode_is_symlink(inode)
1024                                 && !ctx->supported_features.reparse_points
1025                                 && ctx->supported_features.symlink_reparse_points))
1026                 {
1027                         /*
1028                          * Unnamed data stream.  Skip if any of the following is true:
1029                          *
1030                          * - file is a directory
1031                          * - file is encrypted
1032                          * - backend needs to create the file as UNIX symlink
1033                          * - backend will extract the stream as externally
1034                          *   backed from the WIM archive itself
1035                          */
1036                         if (ctx->apply_ops->will_back_from_wim) {
1037                                 int ret = (*ctx->apply_ops->will_back_from_wim)(dentry, ctx);
1038                                 if (ret > 0) /* Error?  */
1039                                         return ret;
1040                                 if (ret < 0) /* Won't externally back?  */
1041                                         need_stream = true;
1042                         } else {
1043                                 need_stream = true;
1044                         }
1045                 }
1046                 break;
1047         case STREAM_TYPE_REPARSE_POINT:
1048                 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
1049                 if (ctx->supported_features.reparse_points ||
1050                     (inode_is_symlink(inode) &&
1051                      ctx->supported_features.symlink_reparse_points))
1052                         need_stream = true;
1053                 break;
1054         case STREAM_TYPE_EFSRPC_RAW_DATA:
1055                 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED);
1056                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1057                         if (ctx->supported_features.encrypted_directories)
1058                                 need_stream = true;
1059                 } else {
1060                         if (ctx->supported_features.encrypted_files)
1061                                 need_stream = true;
1062                 }
1063                 break;
1064         }
1065         if (need_stream)
1066                 return ref_stream(strm, dentry, ctx);
1067         return 0;
1068 }
1069
1070 static int
1071 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
1072 {
1073         struct wim_inode *inode = dentry->d_inode;
1074         for (unsigned i = 0; i < inode->i_num_streams; i++) {
1075                 int ret = ref_stream_if_needed(dentry, inode,
1076                                                &inode->i_streams[i], ctx);
1077                 if (ret)
1078                         return ret;
1079         }
1080         inode->i_visited = 1;
1081         return 0;
1082 }
1083
1084 /*
1085  * Given a list of dentries to be extracted, build the list of blobs that need
1086  * to be extracted, and for each blob determine the streams to which that blob
1087  * will be extracted.
1088  *
1089  * This also initializes the extract progress info with byte and blob
1090  * information.
1091  *
1092  * ctx->supported_features must be filled in.
1093  */
1094 static int
1095 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
1096 {
1097         struct wim_dentry *dentry;
1098         int ret;
1099
1100         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1101                 ret = dentry_ref_streams(dentry, ctx);
1102                 if (ret)
1103                         return ret;
1104         }
1105         list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1106                 dentry->d_inode->i_visited = 0;
1107         return 0;
1108 }
1109
1110 static void
1111 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
1112 {
1113         struct wim_dentry *dentry;
1114
1115         list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1116                 dentry->d_inode->i_first_extraction_alias = NULL;
1117
1118         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1119                 dentry->d_next_extraction_alias = dentry->d_inode->i_first_extraction_alias;
1120                 dentry->d_inode->i_first_extraction_alias = dentry;
1121         }
1122 }
1123
1124 static void
1125 inode_tally_features(const struct wim_inode *inode,
1126                      struct wim_features *features)
1127 {
1128         if (inode->i_attributes & FILE_ATTRIBUTE_READONLY)
1129                 features->readonly_files++;
1130         if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1131                 features->hidden_files++;
1132         if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1133                 features->system_files++;
1134         if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1135                 features->archive_files++;
1136         if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1137                 features->compressed_files++;
1138         if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1139                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1140                         features->encrypted_directories++;
1141                 else
1142                         features->encrypted_files++;
1143         }
1144         if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1145                 features->not_context_indexed_files++;
1146         if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1147                 features->sparse_files++;
1148         if (inode_has_named_data_stream(inode))
1149                 features->named_data_streams++;
1150         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1151                 features->reparse_points++;
1152                 if (inode_is_symlink(inode))
1153                         features->symlink_reparse_points++;
1154                 else
1155                         features->other_reparse_points++;
1156         }
1157         if (inode_has_security_descriptor(inode))
1158                 features->security_descriptors++;
1159         if (inode_has_unix_data(inode))
1160                 features->unix_data++;
1161         if (inode_has_object_id(inode))
1162                 features->object_ids++;
1163 }
1164
1165 /* Tally features necessary to extract a dentry and the corresponding inode.  */
1166 static void
1167 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1168 {
1169         struct wim_inode *inode = dentry->d_inode;
1170
1171         if (dentry_has_short_name(dentry))
1172                 features->short_names++;
1173
1174         if (inode->i_visited) {
1175                 features->hard_links++;
1176         } else {
1177                 inode_tally_features(inode, features);
1178                 inode->i_visited = 1;
1179         }
1180 }
1181
1182 /* Tally the features necessary to extract the specified dentries.  */
1183 static void
1184 dentry_list_get_features(struct list_head *dentry_list,
1185                          struct wim_features *features)
1186 {
1187         struct wim_dentry *dentry;
1188
1189         list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1190                 dentry_tally_features(dentry, features);
1191
1192         list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1193                 dentry->d_inode->i_visited = 0;
1194 }
1195
1196 static int
1197 do_feature_check(const struct wim_features *required_features,
1198                  const struct wim_features *supported_features,
1199                  int extract_flags)
1200 {
1201         /* Encrypted files.  */
1202         if (required_features->encrypted_files &&
1203             !supported_features->encrypted_files)
1204                 WARNING("Ignoring EFS-encrypted data of %lu files",
1205                         required_features->encrypted_files);
1206
1207         /* Named data streams.  */
1208         if (required_features->named_data_streams &&
1209             !supported_features->named_data_streams)
1210                 WARNING("Ignoring named data streams of %lu files",
1211                         required_features->named_data_streams);
1212
1213         /* File attributes.  */
1214         if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1215
1216                 if (required_features->readonly_files &&
1217                     !supported_features->readonly_files)
1218                         WARNING("Ignoring FILE_ATTRIBUTE_READONLY of %lu files",
1219                                 required_features->readonly_files);
1220
1221                 if (required_features->hidden_files &&
1222                     !supported_features->hidden_files)
1223                         WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1224                                 required_features->hidden_files);
1225
1226                 if (required_features->system_files &&
1227                     !supported_features->system_files)
1228                         WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1229                                 required_features->system_files);
1230
1231                 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1232                  * We're an archive program, so theoretically we can do what we
1233                  * want with it.  */
1234
1235                 if (required_features->compressed_files &&
1236                     !supported_features->compressed_files)
1237                         WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1238                                 required_features->compressed_files);
1239
1240                 if (required_features->not_context_indexed_files &&
1241                     !supported_features->not_context_indexed_files)
1242                         WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1243                                 required_features->not_context_indexed_files);
1244
1245                 if (required_features->sparse_files &&
1246                     !supported_features->sparse_files)
1247                         WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1248                                 required_features->sparse_files);
1249
1250                 if (required_features->encrypted_directories &&
1251                     !supported_features->encrypted_directories)
1252                         WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1253                                 required_features->encrypted_directories);
1254         }
1255
1256         /* Hard links.  */
1257         if (required_features->hard_links && !supported_features->hard_links)
1258                 WARNING("Extracting %lu hard links as independent files",
1259                         required_features->hard_links);
1260
1261         /* Symbolic links and reparse points.  */
1262         if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1263             required_features->symlink_reparse_points &&
1264             !supported_features->symlink_reparse_points &&
1265             !supported_features->reparse_points)
1266         {
1267                 ERROR("Extraction backend does not support symbolic links!");
1268                 return WIMLIB_ERR_UNSUPPORTED;
1269         }
1270         if (required_features->reparse_points &&
1271             !supported_features->reparse_points)
1272         {
1273                 if (supported_features->symlink_reparse_points) {
1274                         if (required_features->other_reparse_points) {
1275                                 WARNING("Ignoring reparse data of %lu non-symlink/junction files",
1276                                         required_features->other_reparse_points);
1277                         }
1278                 } else {
1279                         WARNING("Ignoring reparse data of %lu files",
1280                                 required_features->reparse_points);
1281                 }
1282         }
1283
1284         /* Security descriptors.  */
1285         if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1286                                WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1287              == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1288             required_features->security_descriptors &&
1289             !supported_features->security_descriptors)
1290         {
1291                 ERROR("Extraction backend does not support security descriptors!");
1292                 return WIMLIB_ERR_UNSUPPORTED;
1293         }
1294         if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1295             required_features->security_descriptors &&
1296             !supported_features->security_descriptors)
1297                 WARNING("Ignoring Windows NT security descriptors of %lu files",
1298                         required_features->security_descriptors);
1299
1300         /* UNIX data.  */
1301         if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
1302             required_features->unix_data && !supported_features->unix_data)
1303         {
1304                 ERROR("Extraction backend does not support UNIX data!");
1305                 return WIMLIB_ERR_UNSUPPORTED;
1306         }
1307
1308         if (required_features->unix_data &&
1309             !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1310         {
1311                 WARNING("Ignoring UNIX metadata of %lu files",
1312                         required_features->unix_data);
1313         }
1314
1315         /* Object IDs.  */
1316         if (required_features->object_ids && !supported_features->object_ids) {
1317                 WARNING("Ignoring object IDs of %lu files",
1318                         required_features->object_ids);
1319         }
1320
1321         /* DOS Names.  */
1322         if (required_features->short_names &&
1323             !supported_features->short_names)
1324         {
1325                 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1326                         ERROR("Extraction backend does not support DOS names!");
1327                         return WIMLIB_ERR_UNSUPPORTED;
1328                 }
1329                 WARNING("Ignoring DOS names of %lu files",
1330                         required_features->short_names);
1331         }
1332
1333         /* Timestamps.  */
1334         if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1335             !supported_features->timestamps)
1336         {
1337                 ERROR("Extraction backend does not support timestamps!");
1338                 return WIMLIB_ERR_UNSUPPORTED;
1339         }
1340
1341         return 0;
1342 }
1343
1344 static const struct apply_operations *
1345 select_apply_operations(int extract_flags)
1346 {
1347 #ifdef WITH_NTFS_3G
1348         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1349                 return &ntfs_3g_apply_ops;
1350 #endif
1351 #ifdef __WIN32__
1352         return &win32_apply_ops;
1353 #else
1354         return &unix_apply_ops;
1355 #endif
1356 }
1357
1358 static int
1359 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1360               const tchar *target, int extract_flags)
1361 {
1362         const struct apply_operations *ops;
1363         struct apply_ctx *ctx;
1364         int ret;
1365         LIST_HEAD(dentry_list);
1366
1367         if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1368                 ret = extract_dentries_to_stdout(trees, num_trees,
1369                                                  wim->blob_table);
1370                 goto out;
1371         }
1372
1373         num_trees = remove_duplicate_trees(trees, num_trees);
1374         num_trees = remove_contained_trees(trees, num_trees);
1375
1376         ops = select_apply_operations(extract_flags);
1377
1378         if (num_trees > 1 && ops->single_tree_only) {
1379                 ERROR("Extracting multiple directory trees "
1380                       "at once is not supported in %s extraction mode!",
1381                       ops->name);
1382                 ret = WIMLIB_ERR_UNSUPPORTED;
1383                 goto out;
1384         }
1385
1386         ctx = CALLOC(1, ops->context_size);
1387         if (!ctx) {
1388                 ret = WIMLIB_ERR_NOMEM;
1389                 goto out;
1390         }
1391
1392         ctx->wim = wim;
1393         ctx->target = target;
1394         ctx->target_nchars = tstrlen(target);
1395         ctx->extract_flags = extract_flags;
1396         if (ctx->wim->progfunc) {
1397                 ctx->progfunc = ctx->wim->progfunc;
1398                 ctx->progctx = ctx->wim->progctx;
1399                 ctx->progress.extract.image = wim->current_image;
1400                 ctx->progress.extract.extract_flags = (extract_flags &
1401                                                        WIMLIB_EXTRACT_MASK_PUBLIC);
1402                 ctx->progress.extract.wimfile_name = wim->filename;
1403                 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1404                                                                          wim->current_image);
1405                 ctx->progress.extract.target = target;
1406         }
1407         INIT_LIST_HEAD(&ctx->blob_list);
1408         filedes_invalidate(&ctx->tmpfile_fd);
1409         ctx->apply_ops = ops;
1410
1411         ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1412         if (ret)
1413                 goto out_cleanup;
1414
1415         build_dentry_list(&dentry_list, trees, num_trees,
1416                           !(extract_flags &
1417                             WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1418
1419         dentry_list_get_features(&dentry_list, &ctx->required_features);
1420
1421         ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1422                                ctx->extract_flags);
1423         if (ret)
1424                 goto out_cleanup;
1425
1426         ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1427         if (ret)
1428                 goto out_cleanup;
1429
1430         if (unlikely(list_empty(&dentry_list))) {
1431                 WARNING("There is nothing to extract!");
1432                 goto out_cleanup;
1433         }
1434
1435         ret = dentry_list_resolve_streams(&dentry_list, ctx);
1436         if (ret)
1437                 goto out_cleanup;
1438
1439         dentry_list_build_inode_alias_lists(&dentry_list);
1440
1441         ret = dentry_list_ref_streams(&dentry_list, ctx);
1442         if (ret)
1443                 goto out_cleanup;
1444
1445         if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1446                 /* When extracting from a pipe, the number of bytes of data to
1447                  * extract can't be determined in the normal way (examining the
1448                  * blob table), since at this point all we have is a set of
1449                  * SHA-1 message digests of blobs that need to be extracted.
1450                  * However, we can get a reasonably accurate estimate by taking
1451                  * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1452                  * data.  This does assume that a full image is being extracted,
1453                  * but currently there is no API for doing otherwise.  (Also,
1454                  * subtract <HARDLINKBYTES> from this if hard links are
1455                  * supported by the extraction mode.)  */
1456                 ctx->progress.extract.total_bytes =
1457                         xml_get_image_total_bytes(wim->xml_info,
1458                                                   wim->current_image);
1459                 if (ctx->supported_features.hard_links) {
1460                         ctx->progress.extract.total_bytes -=
1461                                 xml_get_image_hard_link_bytes(wim->xml_info,
1462                                                               wim->current_image);
1463                 }
1464         }
1465
1466         ret = extract_progress(ctx,
1467                                ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1468                                        WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1469                                        WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1470         if (ret)
1471                 goto out_cleanup;
1472
1473         ret = (*ops->extract)(&dentry_list, ctx);
1474         if (ret)
1475                 goto out_cleanup;
1476
1477         if (ctx->progress.extract.completed_bytes <
1478             ctx->progress.extract.total_bytes)
1479         {
1480                 ctx->progress.extract.completed_bytes =
1481                         ctx->progress.extract.total_bytes;
1482                 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1483                 if (ret)
1484                         goto out_cleanup;
1485         }
1486
1487         ret = extract_progress(ctx,
1488                                ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1489                                        WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1490                                        WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1491 out_cleanup:
1492         destroy_blob_list(&ctx->blob_list);
1493         destroy_dentry_list(&dentry_list);
1494         FREE(ctx);
1495 out:
1496         return ret;
1497 }
1498
1499 static int
1500 mkdir_if_needed(const tchar *target)
1501 {
1502         if (!tmkdir(target, 0755))
1503                 return 0;
1504
1505         if (errno == EEXIST)
1506                 return 0;
1507
1508 #ifdef __WIN32__
1509         /* _wmkdir() fails with EACCES if called on a drive root directory.  */
1510         if (errno == EACCES)
1511                 return 0;
1512 #endif
1513
1514         ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
1515         return WIMLIB_ERR_MKDIR;
1516 }
1517
1518 /* Make sure the extraction flags make sense, and update them if needed.  */
1519 static int
1520 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1521 {
1522         int extract_flags = *extract_flags_p;
1523
1524         /* Check for invalid flag combinations  */
1525
1526         if ((extract_flags &
1527              (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1528               WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1529                                                     WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1530                 return WIMLIB_ERR_INVALID_PARAM;
1531
1532         if ((extract_flags &
1533              (WIMLIB_EXTRACT_FLAG_RPFIX |
1534               WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1535                                                 WIMLIB_EXTRACT_FLAG_NORPFIX))
1536                 return WIMLIB_ERR_INVALID_PARAM;
1537
1538 #ifndef WITH_NTFS_3G
1539         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1540                 ERROR("wimlib was compiled without support for NTFS-3G, so\n"
1541                       "        it cannot apply a WIM image directly to an NTFS volume.");
1542                 return WIMLIB_ERR_UNSUPPORTED;
1543         }
1544 #endif
1545
1546         if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1547 #ifdef __WIN32__
1548                 if (!wim->filename)
1549                         return WIMLIB_ERR_NO_FILENAME;
1550 #else
1551                 ERROR("WIMBoot extraction is only supported on Windows!");
1552                 return WIMLIB_ERR_UNSUPPORTED;
1553 #endif
1554         }
1555
1556         if (extract_flags & (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K |
1557                              WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K |
1558                              WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K |
1559                              WIMLIB_EXTRACT_FLAG_COMPACT_LZX))
1560         {
1561         #ifdef __WIN32__
1562                 int count = 0;
1563                 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K) != 0);
1564                 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K) != 0);
1565                 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K) != 0);
1566                 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_LZX) != 0);
1567                 if (count != 1) {
1568                         ERROR("Only one compression format can be specified "
1569                               "for compact-mode extraction!");
1570                         return WIMLIB_ERR_INVALID_PARAM;
1571                 }
1572                 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1573                         ERROR("Compact-mode extraction and WIMBoot-mode "
1574                               "extraction are mutually exclusive!");
1575                         return WIMLIB_ERR_INVALID_PARAM;
1576                 }
1577         #else
1578                 ERROR("Compact-mode extraction (System Compression) "
1579                       "is only supported on Windows!");
1580                 return WIMLIB_ERR_UNSUPPORTED;
1581         #endif
1582         }
1583
1584
1585         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1586                               WIMLIB_EXTRACT_FLAG_NORPFIX |
1587                               WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1588                                         WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1589         {
1590                 /* For full-image extraction, do reparse point fixups by default
1591                  * if the WIM header says they are enabled.  */
1592                 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1593                         extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1594         }
1595
1596         *extract_flags_p = extract_flags;
1597         return 0;
1598 }
1599
1600 struct append_dentry_ctx {
1601         struct wim_dentry **dentries;
1602         size_t num_dentries;
1603         size_t num_alloc_dentries;
1604 };
1605
1606 static int
1607 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1608 {
1609         struct append_dentry_ctx *ctx = _ctx;
1610
1611         if (ctx->num_dentries == ctx->num_alloc_dentries) {
1612                 struct wim_dentry **new_dentries;
1613                 size_t new_length;
1614
1615                 new_length = max(ctx->num_alloc_dentries + 8,
1616                                  ctx->num_alloc_dentries * 3 / 2);
1617                 new_dentries = REALLOC(ctx->dentries,
1618                                        new_length * sizeof(ctx->dentries[0]));
1619                 if (new_dentries == NULL)
1620                         return WIMLIB_ERR_NOMEM;
1621                 ctx->dentries = new_dentries;
1622                 ctx->num_alloc_dentries = new_length;
1623         }
1624         ctx->dentries[ctx->num_dentries++] = dentry;
1625         return 0;
1626 }
1627
1628 /* Append dentries matched by a path which can contain wildcard characters.  */
1629 static int
1630 append_matched_dentries(WIMStruct *wim, const tchar *orig_pattern,
1631                         int extract_flags, struct append_dentry_ctx *ctx)
1632 {
1633         const size_t count_before = ctx->num_dentries;
1634         tchar *pattern;
1635         int ret;
1636
1637         pattern = canonicalize_wim_path(orig_pattern);
1638         if (!pattern)
1639                 return WIMLIB_ERR_NOMEM;
1640         ret = expand_path_pattern(wim_get_current_root_dentry(wim), pattern,
1641                                   append_dentry_cb, ctx);
1642         FREE(pattern);
1643         if (ret || ctx->num_dentries > count_before)
1644                 return ret;
1645         if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB) {
1646                 ERROR("No matches for path pattern \"%"TS"\"", orig_pattern);
1647                 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1648         }
1649         WARNING("No matches for path pattern \"%"TS"\"", orig_pattern);
1650         return 0;
1651 }
1652
1653 static int
1654 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1655                         const tchar * const *paths, size_t num_paths,
1656                         int extract_flags)
1657 {
1658         int ret;
1659         struct wim_dentry **trees;
1660         size_t num_trees;
1661
1662         if (wim == NULL || target == NULL || target[0] == T('\0') ||
1663             (num_paths != 0 && paths == NULL))
1664                 return WIMLIB_ERR_INVALID_PARAM;
1665
1666         ret = check_extract_flags(wim, &extract_flags);
1667         if (ret)
1668                 return ret;
1669
1670         ret = select_wim_image(wim, image);
1671         if (ret)
1672                 return ret;
1673
1674         ret = wim_checksum_unhashed_blobs(wim);
1675         if (ret)
1676                 return ret;
1677
1678         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1679                               WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1680             (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1681         {
1682                 ret = mkdir_if_needed(target);
1683                 if (ret)
1684                         return ret;
1685         }
1686
1687         if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1688
1689                 struct append_dentry_ctx append_dentry_ctx = {
1690                         .dentries = NULL,
1691                         .num_dentries = 0,
1692                         .num_alloc_dentries = 0,
1693                 };
1694
1695                 for (size_t i = 0; i < num_paths; i++) {
1696                         ret = append_matched_dentries(wim, paths[i],
1697                                                       extract_flags,
1698                                                       &append_dentry_ctx);
1699                         if (ret) {
1700                                 trees = append_dentry_ctx.dentries;
1701                                 goto out_free_trees;
1702                         }
1703                 }
1704                 trees = append_dentry_ctx.dentries;
1705                 num_trees = append_dentry_ctx.num_dentries;
1706         } else {
1707                 trees = MALLOC(num_paths * sizeof(trees[0]));
1708                 if (trees == NULL)
1709                         return WIMLIB_ERR_NOMEM;
1710
1711                 for (size_t i = 0; i < num_paths; i++) {
1712
1713                         tchar *path = canonicalize_wim_path(paths[i]);
1714                         if (path == NULL) {
1715                                 ret = WIMLIB_ERR_NOMEM;
1716                                 goto out_free_trees;
1717                         }
1718
1719                         trees[i] = get_dentry(wim, path,
1720                                               WIMLIB_CASE_PLATFORM_DEFAULT);
1721                         FREE(path);
1722                         if (trees[i] == NULL) {
1723                                   ERROR("Path \"%"TS"\" does not exist "
1724                                         "in WIM image %d",
1725                                         paths[i], wim->current_image);
1726                                   ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1727                                   goto out_free_trees;
1728                         }
1729                 }
1730                 num_trees = num_paths;
1731         }
1732
1733         if (num_trees == 0) {
1734                 ret = 0;
1735                 goto out_free_trees;
1736         }
1737
1738         ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1739 out_free_trees:
1740         FREE(trees);
1741         return ret;
1742 }
1743
1744 static int
1745 extract_single_image(WIMStruct *wim, int image,
1746                      const tchar *target, int extract_flags)
1747 {
1748         const tchar *path = WIMLIB_WIM_ROOT_PATH;
1749         extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1750         return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1751 }
1752
1753 static const tchar * const filename_forbidden_chars =
1754 #ifdef __WIN32__
1755 T("<>:\"/\\|?*");
1756 #else
1757 T("/");
1758 #endif
1759
1760 /* This function checks if it is okay to use a WIM image's name as a directory
1761  * name.  */
1762 static bool
1763 image_name_ok_as_dir(const tchar *image_name)
1764 {
1765         return image_name && *image_name &&
1766                 !tstrpbrk(image_name, filename_forbidden_chars) &&
1767                 tstrcmp(image_name, T(".")) &&
1768                 tstrcmp(image_name, T("..")) &&
1769                 tstrlen(image_name) <= 128;
1770 }
1771
1772 /* Extracts all images from the WIM to the directory @target, with the images
1773  * placed in subdirectories named by their image names. */
1774 static int
1775 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1776 {
1777         size_t output_path_len = tstrlen(target);
1778         tchar buf[output_path_len + 1 + 128 + 1];
1779         int ret;
1780         int image;
1781         const tchar *image_name;
1782
1783         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1784                 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1785                 return WIMLIB_ERR_INVALID_PARAM;
1786         }
1787
1788         ret = mkdir_if_needed(target);
1789         if (ret)
1790                 return ret;
1791         tmemcpy(buf, target, output_path_len);
1792         buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1793         for (image = 1; image <= wim->hdr.image_count; image++) {
1794                 image_name = wimlib_get_image_name(wim, image);
1795                 if (image_name_ok_as_dir(image_name)) {
1796                         tstrcpy(buf + output_path_len + 1, image_name);
1797                 } else {
1798                         /* Image name is empty or contains forbidden characters.
1799                          * Use image number instead. */
1800                         tsprintf(buf + output_path_len + 1, T("%d"), image);
1801                 }
1802                 ret = extract_single_image(wim, image, buf, extract_flags);
1803                 if (ret)
1804                         return ret;
1805         }
1806         return 0;
1807 }
1808
1809 static int
1810 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1811                         int extract_flags)
1812 {
1813         if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1814                              WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1815                              WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1816                 return WIMLIB_ERR_INVALID_PARAM;
1817
1818         if (image == WIMLIB_ALL_IMAGES)
1819                 return extract_all_images(wim, target, extract_flags);
1820         else
1821                 return extract_single_image(wim, image, target, extract_flags);
1822 }
1823
1824
1825 /****************************************************************************
1826  *                          Extraction API                                  *
1827  ****************************************************************************/
1828
1829 WIMLIBAPI int
1830 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1831                      const tchar * const *paths, size_t num_paths,
1832                      int extract_flags)
1833 {
1834         if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1835                 return WIMLIB_ERR_INVALID_PARAM;
1836
1837         return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1838                                        extract_flags);
1839 }
1840
1841 WIMLIBAPI int
1842 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1843                         const tchar *path_list_file, int extract_flags)
1844 {
1845         int ret;
1846         tchar **paths;
1847         size_t num_paths;
1848         void *mem;
1849
1850         ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1851         if (ret) {
1852                 ERROR("Failed to read path list file \"%"TS"\"",
1853                       path_list_file);
1854                 return ret;
1855         }
1856
1857         ret = wimlib_extract_paths(wim, image, target,
1858                                    (const tchar * const *)paths, num_paths,
1859                                    extract_flags);
1860         FREE(paths);
1861         FREE(mem);
1862         return ret;
1863 }
1864
1865 WIMLIBAPI int
1866 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1867                                              const tchar *image_num_or_name,
1868                                              const tchar *target,
1869                                              int extract_flags,
1870                                              wimlib_progress_func_t progfunc,
1871                                              void *progctx)
1872 {
1873         int ret;
1874         WIMStruct *pwm;
1875         struct filedes *in_fd;
1876         int image;
1877         unsigned i;
1878
1879         if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1880                 return WIMLIB_ERR_INVALID_PARAM;
1881
1882         /* Read the WIM header from the pipe and get a WIMStruct to represent
1883          * the pipable WIM.  Caveats:  Unlike getting a WIMStruct with
1884          * wimlib_open_wim(), getting a WIMStruct in this way will result in an
1885          * empty blob table, no XML data read, and no filename set.  */
1886         ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1887                                     progfunc, progctx);
1888         if (ret)
1889                 return ret;
1890
1891         /* Sanity check to make sure this is a pipable WIM.  */
1892         if (pwm->hdr.magic != PWM_MAGIC) {
1893                 ERROR("The WIM being read from file descriptor %d "
1894                       "is not pipable!", pipe_fd);
1895                 ret = WIMLIB_ERR_NOT_PIPABLE;
1896                 goto out_wimlib_free;
1897         }
1898
1899         /* Sanity check to make sure the first part of a pipable split WIM is
1900          * sent over the pipe first.  */
1901         if (pwm->hdr.part_number != 1) {
1902                 ERROR("The first part of the split WIM must be "
1903                       "sent over the pipe first.");
1904                 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1905                 goto out_wimlib_free;
1906         }
1907
1908         in_fd = &pwm->in_fd;
1909         wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1910
1911         /* As mentioned, the WIMStruct we created from the pipe does not have
1912          * XML data yet.  Fix this by reading the extra copy of the XML data
1913          * that directly follows the header in pipable WIMs.  (Note: see
1914          * write_pipable_wim() for more details about the format of pipable
1915          * WIMs.)  */
1916         {
1917                 u8 hash[SHA1_HASH_SIZE];
1918
1919                 ret = read_pwm_blob_header(pwm, hash,
1920                                            &pwm->hdr.xml_data_reshdr, NULL);
1921                 if (ret)
1922                         goto out_wimlib_free;
1923
1924                 if (!(pwm->hdr.xml_data_reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
1925                         ERROR("Expected XML data, but found non-metadata resource.");
1926                         ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1927                         goto out_wimlib_free;
1928                 }
1929
1930                 ret = read_wim_xml_data(pwm);
1931                 if (ret)
1932                         goto out_wimlib_free;
1933
1934                 if (xml_get_image_count(pwm->xml_info) != pwm->hdr.image_count) {
1935                         ERROR("Image count in XML data is not the same as in WIM header.");
1936                         ret = WIMLIB_ERR_IMAGE_COUNT;
1937                         goto out_wimlib_free;
1938                 }
1939         }
1940
1941         /* Get image index (this may use the XML data that was just read to
1942          * resolve an image name).  */
1943         if (image_num_or_name) {
1944                 image = wimlib_resolve_image(pwm, image_num_or_name);
1945                 if (image == WIMLIB_NO_IMAGE) {
1946                         ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
1947                               image_num_or_name);
1948                         ret = WIMLIB_ERR_INVALID_IMAGE;
1949                         goto out_wimlib_free;
1950                 } else if (image == WIMLIB_ALL_IMAGES) {
1951                         ERROR("Applying all images from a pipe is not supported!");
1952                         ret = WIMLIB_ERR_INVALID_IMAGE;
1953                         goto out_wimlib_free;
1954                 }
1955         } else {
1956                 if (pwm->hdr.image_count != 1) {
1957                         ERROR("No image was specified, but the pipable WIM "
1958                               "did not contain exactly 1 image");
1959                         ret = WIMLIB_ERR_INVALID_IMAGE;
1960                         goto out_wimlib_free;
1961                 }
1962                 image = 1;
1963         }
1964
1965         /* Load the needed metadata resource.  */
1966         for (i = 1; i <= pwm->hdr.image_count; i++) {
1967                 ret = handle_pwm_metadata_resource(pwm, i, i == image);
1968                 if (ret)
1969                         goto out_wimlib_free;
1970         }
1971         /* Extract the image.  */
1972         extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
1973         ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
1974         /* Clean up and return.  */
1975 out_wimlib_free:
1976         wimlib_free(pwm);
1977         return ret;
1978 }
1979
1980
1981 WIMLIBAPI int
1982 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
1983                                const tchar *target, int extract_flags)
1984 {
1985         return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
1986                                                             image_num_or_name,
1987                                                             target,
1988                                                             extract_flags,
1989                                                             NULL,
1990                                                             NULL);
1991 }
1992
1993 WIMLIBAPI int
1994 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1995                      int extract_flags)
1996 {
1997         if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1998                 return WIMLIB_ERR_INVALID_PARAM;
1999         return do_wimlib_extract_image(wim, image, target, extract_flags);
2000 }