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