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