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