]> wimlib.net Git - wimlib/blob - src/extract_image.c
Minor cleanups
[wimlib] / src / extract_image.c
1 /*
2  * extract_image.c
3  *
4  * Support for extracting WIM files.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31 #include <unistd.h>
32
33 #ifdef __WIN32__
34 #  include "win32.h"
35 #endif
36
37 #include "wimlib_internal.h"
38 #include "dentry.h"
39 #include "lookup_table.h"
40 #include "xml.h"
41
42 #ifdef WITH_NTFS_3G
43 #  include <ntfs-3g/volume.h>
44 #endif
45
46 static int
47 do_apply_op(struct wim_dentry *dentry, struct apply_args *args,
48             int (*apply_dentry_func)(const tchar *, size_t,
49                                      struct wim_dentry *, struct apply_args *))
50 {
51         tchar *p;
52         const tchar *full_path;
53         size_t full_path_nchars;
54
55         wimlib_assert(dentry->_full_path != NULL);
56         full_path = dentry->_full_path + 1;
57         full_path_nchars = dentry->full_path_nbytes / sizeof(tchar) - 1;
58         tchar output_path[args->target_nchars + 1 +
59                          (full_path_nchars - args->wim_source_path_nchars) + 1];
60         p = output_path;
61
62         tmemcpy(p, args->target, args->target_nchars);
63         p += args->target_nchars;
64
65         if (dentry != args->extract_root) {
66                 *p++ = T('/');
67                 tmemcpy(p, full_path + args->wim_source_path_nchars,
68                         full_path_nchars - args->wim_source_path_nchars);
69                 p += full_path_nchars - args->wim_source_path_nchars;
70         }
71         *p = T('\0');
72         return (*apply_dentry_func)(output_path, p - output_path,
73                                     dentry, args);
74 }
75
76
77 /* Extracts a file, directory, or symbolic link from the WIM archive. */
78 static int
79 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
80 {
81 #ifdef __WIN32__
82         return do_apply_op(dentry, arg, win32_do_apply_dentry);
83 #else
84         return do_apply_op(dentry, arg, unix_do_apply_dentry);
85 #endif
86 }
87
88
89 /* Apply timestamps to an extracted file or directory */
90 static int
91 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
92 {
93 #ifdef __WIN32__
94         return do_apply_op(dentry, arg, win32_do_apply_dentry_timestamps);
95 #else
96         return do_apply_op(dentry, arg, unix_do_apply_dentry_timestamps);
97 #endif
98 }
99
100 /* Extract a dentry if it hasn't already been extracted and either
101  * WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified, or the dentry is a directory
102  * and/or has no unnamed stream. */
103 static int
104 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
105 {
106         struct apply_args *args = arg;
107         int ret;
108
109         if (!dentry->needs_extraction)
110                 return 0;
111
112         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS &&
113             !dentry_is_directory(dentry) &&
114             inode_unnamed_lte_resolved(dentry->d_inode) != NULL)
115                 return 0;
116
117         if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
118              args->progress_func) {
119                 args->progress.extract.cur_path = dentry->_full_path;
120                 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
121                                     &args->progress);
122         }
123         ret = args->apply_dentry(dentry, args);
124         if (ret == 0)
125                 dentry->needs_extraction = 0;
126         return ret;
127 }
128
129 static void
130 calculate_bytes_to_extract(struct list_head *stream_list,
131                            int extract_flags,
132                            union wimlib_progress_info *progress)
133 {
134         struct wim_lookup_table_entry *lte;
135         u64 total_bytes = 0;
136         u64 num_streams = 0;
137
138         /* For each stream to be extracted... */
139         list_for_each_entry(lte, stream_list, extraction_list) {
140                 if (extract_flags &
141                     (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
142                 {
143                         /* In the symlink or hard link extraction mode, each
144                          * stream will be extracted one time regardless of how
145                          * many dentries share the stream. */
146                         wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
147                         if (!lte->extracted_file) {
148                                 num_streams++;
149                                 total_bytes += wim_resource_size(lte);
150                         }
151                 } else {
152                         num_streams += lte->out_refcnt;
153                         total_bytes += lte->out_refcnt * wim_resource_size(lte);
154                 }
155         }
156         progress->extract.num_streams = num_streams;
157         progress->extract.total_bytes = total_bytes;
158         progress->extract.completed_bytes = 0;
159 }
160
161 static void
162 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
163                                 struct list_head *stream_list)
164 {
165         if (++lte->out_refcnt == 1) {
166                 INIT_LIST_HEAD(&lte->lte_dentry_list);
167                 list_add_tail(&lte->extraction_list, stream_list);
168         }
169 }
170
171 struct find_streams_ctx {
172         struct list_head stream_list;
173         int extract_flags;
174 };
175
176 static int
177 dentry_find_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
178 {
179         struct find_streams_ctx *ctx = _ctx;
180         struct wim_inode *inode = dentry->d_inode;
181         struct wim_lookup_table_entry *lte;
182         bool dentry_added = false;
183         struct list_head *stream_list = &ctx->stream_list;
184         int extract_flags = ctx->extract_flags;
185
186         dentry->needs_extraction = 1;
187
188         lte = inode_unnamed_lte_resolved(inode);
189         if (lte) {
190                 maybe_add_stream_for_extraction(lte, stream_list);
191                 list_add_tail(&dentry->tmp_list, &lte->lte_dentry_list);
192                 dentry_added = true;
193         }
194
195         /* Determine whether to include alternate data stream entries or not.
196          *
197          * UNIX:  Include them if extracting using NTFS-3g.
198          *
199          * Windows: Include them undconditionally, although if the filesystem is
200          * not NTFS we won't actually be able to extract them. */
201 #if defined(WITH_NTFS_3G)
202         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
203 #elif defined(__WIN32__)
204         if (1)
205 #else
206         if (0)
207 #endif
208         {
209                 for (unsigned i = 0; i < inode->i_num_ads; i++) {
210                         if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
211                                 lte = inode->i_ads_entries[i].lte;
212                                 if (lte) {
213                                         maybe_add_stream_for_extraction(lte,
214                                                                         stream_list);
215                                         if (!dentry_added) {
216                                                 list_add_tail(&dentry->tmp_list,
217                                                               &lte->lte_dentry_list);
218                                                 dentry_added = true;
219                                         }
220                                 }
221                         }
222                 }
223         }
224         return 0;
225 }
226
227 static int
228 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_lookup_table)
229 {
230         struct wim_inode *inode = dentry->d_inode;
231         struct wim_lookup_table *lookup_table = _lookup_table;
232         struct wim_lookup_table_entry *lte;
233
234         inode_resolve_ltes(inode, lookup_table);
235         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
236                 lte = inode_stream_lte_resolved(inode, i);
237                 if (lte)
238                         lte->out_refcnt = 0;
239         }
240         return 0;
241 }
242
243 static void
244 find_streams_for_extraction(struct wim_dentry *root,
245                             struct list_head *stream_list,
246                             struct wim_lookup_table *lookup_table,
247                             int extract_flags)
248 {
249         struct find_streams_ctx ctx;
250
251         INIT_LIST_HEAD(&ctx.stream_list);
252         ctx.extract_flags = extract_flags;
253         for_dentry_in_tree(root, dentry_resolve_and_zero_lte_refcnt, lookup_table);
254         for_dentry_in_tree(root, dentry_find_streams_to_extract, &ctx);
255         list_transfer(&ctx.stream_list, stream_list);
256 }
257
258 static int
259 dentry_reset_needs_extraction(struct wim_dentry *dentry, void *_ignore)
260 {
261         dentry->needs_extraction = 0;
262         return 0;
263 }
264
265 struct apply_operations {
266         int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
267         int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
268 };
269
270 static const struct apply_operations normal_apply_operations = {
271         .apply_dentry = apply_dentry_normal,
272         .apply_dentry_timestamps = apply_dentry_timestamps_normal,
273 };
274
275 #ifdef WITH_NTFS_3G
276 static const struct apply_operations ntfs_apply_operations = {
277         .apply_dentry = apply_dentry_ntfs,
278         .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
279 };
280 #endif
281
282 static int
283 apply_stream_list(struct list_head *stream_list,
284                   struct apply_args *args,
285                   const struct apply_operations *ops,
286                   wimlib_progress_func_t progress_func)
287 {
288         uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
289         uint64_t next_progress = bytes_per_progress;
290         struct wim_lookup_table_entry *lte;
291         struct wim_dentry *dentry;
292         int ret;
293
294         /* This complicated loop is essentially looping through the dentries,
295          * although dentries may be visited more than once (if a dentry contains
296          * two different nonempty streams) or not at all (if a dentry contains
297          * no non-empty streams).
298          *
299          * The outer loop is over the distinct streams to be extracted so that
300          * sequential reading of the WIM can be implemented. */
301
302         /* For each distinct stream to be extracted */
303         list_for_each_entry(lte, stream_list, extraction_list) {
304                 /* For each dentry to be extracted that is a name for an inode
305                  * containing the stream */
306                 list_for_each_entry(dentry, &lte->lte_dentry_list, tmp_list) {
307                         /* Extract the dentry if it was not already
308                          * extracted */
309                         ret = maybe_apply_dentry(dentry, args);
310                         if (ret)
311                                 return ret;
312                         if (progress_func &&
313                             args->progress.extract.completed_bytes >= next_progress)
314                         {
315                                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
316                                               &args->progress);
317                                 if (args->progress.extract.completed_bytes >=
318                                     args->progress.extract.total_bytes)
319                                 {
320                                         next_progress = ~0ULL;
321                                 } else {
322                                         next_progress =
323                                                 min (args->progress.extract.completed_bytes +
324                                                      bytes_per_progress,
325                                                      args->progress.extract.total_bytes);
326                                 }
327                         }
328                 }
329         }
330         return 0;
331 }
332
333 static int
334 sort_stream_list_by_wim_position(struct list_head *stream_list)
335 {
336         struct list_head *cur;
337         size_t num_streams;
338         struct wim_lookup_table_entry **array;
339         size_t i;
340         size_t array_size;
341
342         num_streams = 0;
343         list_for_each(cur, stream_list)
344                 num_streams++;
345         array_size = num_streams * sizeof(array[0]);
346         array = MALLOC(array_size);
347         if (!array) {
348                 ERROR("Failed to allocate %zu bytes to sort stream entries",
349                       array_size);
350                 return WIMLIB_ERR_NOMEM;
351         }
352         cur = stream_list->next;
353         for (i = 0; i < num_streams; i++) {
354                 array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
355                 cur = cur->next;
356         }
357
358         qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
359
360         INIT_LIST_HEAD(stream_list);
361         for (i = 0; i < num_streams; i++)
362                 list_add_tail(&array[i]->extraction_list, stream_list);
363         FREE(array);
364         return 0;
365 }
366
367 /*
368  * Extract a dentry to standard output.
369  *
370  * This obviously doesn't make sense in all cases.  We return an error if the
371  * dentry does not correspond to a regular file.  Otherwise we extract the
372  * unnamed data stream only.
373  */
374 static int
375 extract_dentry_to_stdout(struct wim_dentry *dentry)
376 {
377         int ret = 0;
378         if (!dentry_is_regular_file(dentry)) {
379                 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
380                       "extracted to standard output", dentry->_full_path);
381                 ret = WIMLIB_ERR_NOT_A_REGULAR_FILE;
382         } else {
383                 struct wim_lookup_table_entry *lte;
384
385                 lte = inode_unnamed_lte_resolved(dentry->d_inode);
386                 if (lte) {
387                         ret = extract_wim_resource_to_fd(lte, STDOUT_FILENO,
388                                                          wim_resource_size(lte));
389                 }
390         }
391         return ret;
392 }
393
394 /*
395  * extract_tree - Extract a file or directory tree from the currently selected
396  *                WIM image.
397  *
398  * @wim:        WIMStruct for the WIM file, with the desired image selected
399  *              (as wim->current_image).
400  * @wim_source_path:
401  *              "Canonical" (i.e. no leading or trailing slashes, path
402  *              separators forwald slashes) path inside the WIM image to
403  *              extract.  An empty string means the full image.
404  * @target:
405  *              Filesystem path to extract the file or directory tree to.
406  *
407  * @extract_flags:
408  *              WIMLIB_EXTRACT_FLAG_*.  Also, the private flag
409  *              WIMLIB_EXTRACT_FLAG_MULTI_IMAGE will be set if this is being
410  *              called through wimlib_extract_image() with WIMLIB_ALL_IMAGES as
411  *              the image.
412  *
413  * @progress_func:
414  *              If non-NULL, progress function for the extraction.  The messages
415  *              we may in this function are:
416  *
417  *              WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN or
418  *                      WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
419  *              WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN;
420  *              WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END;
421  *              WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY;
422  *              WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS;
423  *              WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS;
424  *              WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END or
425  *                      WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END.
426  *
427  * Returns 0 on success; nonzero on failure.
428  */
429 static int
430 extract_tree(WIMStruct *wim, const tchar *wim_source_path, const tchar *target,
431              int extract_flags, wimlib_progress_func_t progress_func)
432 {
433         int ret;
434         struct list_head stream_list;
435         struct apply_args args;
436         const struct apply_operations *ops;
437         struct wim_dentry *root;
438
439         memset(&args, 0, sizeof(args));
440
441         args.w                      = wim;
442         args.target                 = target;
443         args.extract_flags          = extract_flags;
444         args.progress_func          = progress_func;
445         args.target_nchars          = tstrlen(target);
446         args.wim_source_path_nchars = tstrlen(wim_source_path);
447
448         if (progress_func) {
449                 args.progress.extract.wimfile_name = wim->filename;
450                 args.progress.extract.image = wim->current_image;
451                 args.progress.extract.extract_flags = (extract_flags &
452                                                        WIMLIB_EXTRACT_MASK_PUBLIC);
453                 args.progress.extract.image_name = wimlib_get_image_name(wim,
454                                                                          wim->current_image);
455                 args.progress.extract.extract_root_wim_source_path = wim_source_path;
456                 args.progress.extract.target = target;
457         }
458
459 #ifdef WITH_NTFS_3G
460         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
461                 args.vol = ntfs_mount(target, 0);
462                 if (!args.vol) {
463                         ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
464                                          target);
465                         ret = WIMLIB_ERR_NTFS_3G;
466                         goto out;
467                 }
468                 ops = &ntfs_apply_operations;
469         } else
470 #endif
471                 ops = &normal_apply_operations;
472
473         root = get_dentry(wim, wim_source_path);
474         if (!root) {
475                 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
476                       wim_source_path, wim->current_image);
477                 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
478                 goto out_ntfs_umount;
479         }
480         args.extract_root = root;
481
482         ret = calculate_dentry_tree_full_paths(root);
483         if (ret)
484                 goto out_ntfs_umount;
485
486         /* Build a list of the streams that need to be extracted */
487         find_streams_for_extraction(root,
488                                     &stream_list,
489                                     wim->lookup_table, extract_flags);
490
491         /* Calculate the number of bytes of data that will be extracted */
492         calculate_bytes_to_extract(&stream_list, extract_flags,
493                                    &args.progress);
494
495         if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
496                 ret = extract_dentry_to_stdout(root);
497                 goto out_dentry_reset_needs_extraction;
498         }
499
500         if (progress_func) {
501                 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN :
502                               WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
503                               &args.progress);
504         }
505
506         /* If a sequential extraction was specified, sort the streams to be
507          * extracted by their position in the WIM file, so that the WIM file can
508          * be read sequentially. */
509         if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
510                 ret = sort_stream_list_by_wim_position(&stream_list);
511                 if (ret != 0) {
512                         WARNING("Falling back to non-sequential extraction");
513                         extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
514                 }
515         }
516
517         if (progress_func) {
518                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
519                               &args.progress);
520         }
521
522         /* Make the directory structure and extract empty files */
523         args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
524         args.apply_dentry = ops->apply_dentry;
525         ret = for_dentry_in_tree(root, maybe_apply_dentry, &args);
526         args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
527         if (ret)
528                 goto out_dentry_reset_needs_extraction;
529
530         if (progress_func) {
531                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
532                               &args.progress);
533         }
534
535         if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
536                 args.target_realpath = realpath(target, NULL);
537                 if (!args.target_realpath) {
538                         ret = WIMLIB_ERR_NOMEM;
539                         goto out_dentry_reset_needs_extraction;
540                 }
541                 args.target_realpath_len = tstrlen(args.target_realpath);
542         }
543
544         /* Extract non-empty files */
545         ret = apply_stream_list(&stream_list, &args, ops, progress_func);
546         if (ret)
547                 goto out_free_target_realpath;
548
549         if (progress_func) {
550                 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
551                               &args.progress);
552         }
553
554         /* Apply timestamps */
555         ret = for_dentry_in_tree_depth(root,
556                                        ops->apply_dentry_timestamps, &args);
557         if (ret)
558                 goto out_free_target_realpath;
559
560         if (progress_func) {
561                 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END :
562                               WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
563                               &args.progress);
564         }
565 out_free_target_realpath:
566         FREE(args.target_realpath);
567 out_dentry_reset_needs_extraction:
568         for_dentry_in_tree(root, dentry_reset_needs_extraction, NULL);
569 out_ntfs_umount:
570 #ifdef WITH_NTFS_3G
571         /* Unmount the NTFS volume */
572         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
573                 if (ntfs_umount(args.vol, FALSE) != 0) {
574                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
575                                          args.target);
576                         if (ret == 0)
577                                 ret = WIMLIB_ERR_NTFS_3G;
578                 }
579         }
580 #endif
581 out:
582         return ret;
583 }
584
585 /* Validates a single wimlib_extract_command, mostly checking to make sure the
586  * extract flags make sense. */
587 static int
588 check_extract_command(struct wimlib_extract_command *cmd, int wim_header_flags)
589 {
590         int extract_flags;
591         bool is_entire_image = (cmd->wim_source_path[0] == T('\0'));
592
593         /* Empty destination path? */
594         if (cmd->fs_dest_path[0] == T('\0'))
595                 return WIMLIB_ERR_INVALID_PARAM;
596
597         extract_flags = cmd->extract_flags;
598
599         /* Specified both symlink and hardlink modes? */
600         if ((extract_flags &
601              (WIMLIB_EXTRACT_FLAG_SYMLINK |
602               WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
603                                                  WIMLIB_EXTRACT_FLAG_HARDLINK))
604                 return WIMLIB_ERR_INVALID_PARAM;
605
606 #ifdef __WIN32__
607         /* Wanted UNIX data on Windows? */
608         if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
609                 ERROR("Extracting UNIX data is not supported on Windows");
610                 return WIMLIB_ERR_INVALID_PARAM;
611         }
612         /* Wanted linked extraction on Windows?  (XXX This is possible, just not
613          * implemented yet.) */
614         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
615                              WIMLIB_EXTRACT_FLAG_HARDLINK))
616         {
617                 ERROR("Linked extraction modes are not supported on Windows");
618                 return WIMLIB_ERR_INVALID_PARAM;
619         }
620 #endif
621
622         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
623                 /* NTFS-3g extraction mode requested */
624 #ifdef WITH_NTFS_3G
625                 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
626                                       WIMLIB_EXTRACT_FLAG_HARDLINK))) {
627                         ERROR("Cannot specify symlink or hardlink flags when applying\n"
628                               "        directly to a NTFS volume");
629                         return WIMLIB_ERR_INVALID_PARAM;
630                 }
631                 if (!is_entire_image &&
632                     (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS))
633                 {
634                         ERROR("When applying directly to a NTFS volume you can "
635                               "only extract a full image, not part of one");
636                         return WIMLIB_ERR_INVALID_PARAM;
637                 }
638                 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
639                         ERROR("Cannot restore UNIX-specific data in "
640                               "the NTFS extraction mode");
641                         return WIMLIB_ERR_INVALID_PARAM;
642                 }
643 #else
644                 ERROR("wimlib was compiled without support for NTFS-3g, so");
645                 ERROR("we cannot apply a WIM image directly to a NTFS volume");
646                 return WIMLIB_ERR_UNSUPPORTED;
647 #endif
648         }
649
650         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
651                               WIMLIB_EXTRACT_FLAG_NORPFIX)) ==
652                 (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
653         {
654                 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
655                 return WIMLIB_ERR_INVALID_PARAM;
656         }
657
658         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
659                               WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
660         {
661                 /* Do reparse point fixups by default if the WIM header says
662                  * they are enabled and we are extracting a full image. */
663                 if ((wim_header_flags & WIM_HDR_FLAG_RP_FIX) && is_entire_image)
664                         extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
665         }
666
667         if (!is_entire_image && (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)) {
668                 ERROR("Cannot specify --rpfix when not extracting entire image");
669                 return WIMLIB_ERR_INVALID_PARAM;
670         }
671
672         cmd->extract_flags = extract_flags;
673         return 0;
674 }
675
676
677 /* Internal function to execute extraction commands for a WIM image. */
678 static int
679 do_wimlib_extract_files(WIMStruct *wim,
680                         int image,
681                         struct wimlib_extract_command *cmds,
682                         size_t num_cmds,
683                         wimlib_progress_func_t progress_func)
684 {
685         int ret;
686         bool found_link_cmd = false;
687         bool found_nolink_cmd = false;
688
689         /* Select the image from which we are extracting files */
690         ret = select_wim_image(wim, image);
691         if (ret)
692                 return ret;
693
694         /* Make sure there are no streams in the WIM that have not been
695          * checksummed yet. */
696         ret = wim_checksum_unhashed_streams(wim);
697         if (ret)
698                 return ret;
699
700         /* Check for problems with the extraction commands */
701         for (size_t i = 0; i < num_cmds; i++) {
702                 ret = check_extract_command(&cmds[i], wim->hdr.flags);
703                 if (ret)
704                         return ret;
705                 if (cmds[i].extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
706                                              WIMLIB_EXTRACT_FLAG_HARDLINK)) {
707                         found_link_cmd = true;
708                 } else {
709                         found_nolink_cmd = true;
710                 }
711                 if (found_link_cmd && found_nolink_cmd) {
712                         ERROR("Symlink or hardlink extraction mode must "
713                               "be set on all extraction commands");
714                         return WIMLIB_ERR_INVALID_PARAM;
715                 }
716         }
717
718         /* Execute the extraction commands */
719         for (size_t i = 0; i < num_cmds; i++) {
720                 ret = extract_tree(wim,
721                                    cmds[i].wim_source_path,
722                                    cmds[i].fs_dest_path,
723                                    cmds[i].extract_flags,
724                                    progress_func);
725                 if (ret)
726                         return ret;
727         }
728         return 0;
729 }
730
731 /* Extract files or directories from a WIM image. */
732 WIMLIBAPI int
733 wimlib_extract_files(WIMStruct *wim,
734                      int image,
735                      const struct wimlib_extract_command *cmds,
736                      size_t num_cmds,
737                      int default_extract_flags,
738                      WIMStruct **additional_swms,
739                      unsigned num_additional_swms,
740                      wimlib_progress_func_t progress_func)
741 {
742         int ret;
743         struct wimlib_extract_command *cmds_copy;
744         struct wim_lookup_table *wim_tab_save, *joined_tab;
745         int all_flags = 0;
746
747         default_extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
748
749         ret = verify_swm_set(wim, additional_swms, num_additional_swms);
750         if (ret)
751                 goto out;
752
753         if (num_cmds == 0)
754                 goto out;
755
756         if (num_additional_swms) {
757                 ret = new_joined_lookup_table(wim, additional_swms,
758                                               num_additional_swms,
759                                               &joined_tab);
760                 if (ret)
761                         goto out;
762                 wim_tab_save = wim->lookup_table;
763                 wim->lookup_table = joined_tab;
764         }
765
766         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
767         if (!cmds_copy) {
768                 ret = WIMLIB_ERR_NOMEM;
769                 goto out_restore_lookup_table;
770         }
771
772         for (size_t i = 0; i < num_cmds; i++) {
773                 cmds_copy[i].extract_flags = (default_extract_flags |
774                                                  cmds[i].extract_flags)
775                                                 & WIMLIB_EXTRACT_MASK_PUBLIC;
776                 all_flags |= cmds_copy[i].extract_flags;
777
778                 cmds_copy[i].wim_source_path = canonicalize_wim_path(cmds[i].wim_source_path);
779                 if (!cmds_copy[i].wim_source_path) {
780                         ret = WIMLIB_ERR_NOMEM;
781                         goto out_free_cmds_copy;
782                 }
783
784                 cmds_copy[i].fs_dest_path = canonicalize_fs_path(cmds[i].fs_dest_path);
785                 if (!cmds_copy[i].fs_dest_path) {
786                         ret = WIMLIB_ERR_NOMEM;
787                         goto out_free_cmds_copy;
788                 }
789
790         }
791         ret = do_wimlib_extract_files(wim, image,
792                                       cmds_copy, num_cmds,
793                                       progress_func);
794
795         if (all_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
796                          WIMLIB_EXTRACT_FLAG_HARDLINK))
797         {
798                 for_lookup_table_entry(wim->lookup_table,
799                                        lte_free_extracted_file, NULL);
800         }
801 out_free_cmds_copy:
802         for (size_t i = 0; i < num_cmds; i++) {
803                 FREE(cmds_copy[i].wim_source_path);
804                 FREE(cmds_copy[i].fs_dest_path);
805         }
806         FREE(cmds_copy);
807 out_restore_lookup_table:
808         if (num_additional_swms) {
809                 free_lookup_table(wim->lookup_table);
810                 wim->lookup_table = wim_tab_save;
811         }
812 out:
813         return ret;
814 }
815
816 /*
817  * Extracts an image from a WIM file.
818  *
819  * @wim:                WIMStruct for the WIM file.
820  *
821  * @image:              Number of the single image to extract.
822  *
823  * @target:             Directory or NTFS volume to extract the image to.
824  *
825  * @extract_flags:      Bitwise or of WIMLIB_EXTRACT_FLAG_*.
826  *
827  * @progress_func:      If non-NULL, a progress function to be called
828  *                      periodically.
829  *
830  * Returns 0 on success; nonzero on failure.
831  */
832 static int
833 extract_single_image(WIMStruct *wim, int image,
834                      const tchar *target, int extract_flags,
835                      wimlib_progress_func_t progress_func)
836 {
837         int ret;
838         tchar *target_copy = canonicalize_fs_path(target);
839         if (!target_copy)
840                 return WIMLIB_ERR_NOMEM;
841         struct wimlib_extract_command cmd = {
842                 .wim_source_path = T(""),
843                 .fs_dest_path = target_copy,
844                 .extract_flags = extract_flags,
845         };
846         ret = do_wimlib_extract_files(wim, image, &cmd, 1, progress_func);
847         FREE(target_copy);
848         return ret;
849 }
850
851 static const tchar * const filename_forbidden_chars =
852 T(
853 #ifdef __WIN32__
854 "<>:\"/\\|?*"
855 #else
856 "/"
857 #endif
858 );
859
860 /* This function checks if it is okay to use a WIM image's name as a directory
861  * name.  */
862 static bool
863 image_name_ok_as_dir(const tchar *image_name)
864 {
865         return image_name && *image_name &&
866                 !tstrpbrk(image_name, filename_forbidden_chars) &&
867                 tstrcmp(image_name, T(".")) &&
868                 tstrcmp(image_name, T(".."));
869 }
870
871 /* Extracts all images from the WIM to the directory @target, with the images
872  * placed in subdirectories named by their image names. */
873 static int
874 extract_all_images(WIMStruct *wim,
875                    const tchar *target,
876                    int extract_flags,
877                    wimlib_progress_func_t progress_func)
878 {
879         size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
880         size_t output_path_len = tstrlen(target);
881         tchar buf[output_path_len + 1 + image_name_max_len + 1];
882         int ret;
883         int image;
884         const tchar *image_name;
885         struct stat stbuf;
886
887         if (tstat(target, &stbuf)) {
888                 if (errno == ENOENT)
889                 {
890                         if (tmkdir(target, S_IRWXU | S_IRGRP | S_IXGRP |
891                                            S_IROTH | S_IXOTH))
892                         {
893                                 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
894                                 return WIMLIB_ERR_MKDIR;
895                         }
896                 } else {
897                         ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
898                         return WIMLIB_ERR_STAT;
899                 }
900         } else if (!S_ISDIR(stbuf.st_mode)) {
901                 ERROR("\"%"TS"\" is not a directory", target);
902                 return WIMLIB_ERR_NOTDIR;
903         }
904
905         tmemcpy(buf, target, output_path_len);
906         buf[output_path_len] = T('/');
907         for (image = 1; image <= wim->hdr.image_count; image++) {
908                 image_name = wimlib_get_image_name(wim, image);
909                 if (image_name_ok_as_dir(image_name)) {
910                         tstrcpy(buf + output_path_len + 1, image_name);
911                 } else {
912                         /* Image name is empty or contains forbidden characters.
913                          * Use image number instead. */
914                         tsprintf(buf + output_path_len + 1, T("%d"), image);
915                 }
916                 ret = extract_single_image(wim, image, buf, extract_flags,
917                                            progress_func);
918                 if (ret)
919                         return ret;
920         }
921         return 0;
922 }
923
924 /* Extracts a single image or all images from a WIM file to a directory or NTFS
925  * volume. */
926 WIMLIBAPI int
927 wimlib_extract_image(WIMStruct *wim,
928                      int image,
929                      const tchar *target,
930                      int extract_flags,
931                      WIMStruct **additional_swms,
932                      unsigned num_additional_swms,
933                      wimlib_progress_func_t progress_func)
934 {
935         struct wim_lookup_table *joined_tab, *wim_tab_save;
936         int ret;
937
938         extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
939
940         ret = verify_swm_set(wim, additional_swms, num_additional_swms);
941         if (ret)
942                 return ret;
943
944         if (num_additional_swms) {
945                 ret = new_joined_lookup_table(wim, additional_swms,
946                                               num_additional_swms, &joined_tab);
947                 if (ret)
948                         return ret;
949                 wim_tab_save = wim->lookup_table;
950                 wim->lookup_table = joined_tab;
951         }
952
953         if (image == WIMLIB_ALL_IMAGES) {
954                 ret = extract_all_images(wim, target,
955                                          extract_flags | WIMLIB_EXTRACT_FLAG_MULTI_IMAGE,
956                                          progress_func);
957         } else {
958                 ret = extract_single_image(wim, image, target, extract_flags,
959                                            progress_func);
960         }
961
962         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
963                              WIMLIB_EXTRACT_FLAG_HARDLINK))
964         {
965                 for_lookup_table_entry(wim->lookup_table,
966                                        lte_free_extracted_file,
967                                        NULL);
968         }
969         if (num_additional_swms) {
970                 free_lookup_table(wim->lookup_table);
971                 wim->lookup_table = wim_tab_save;
972         }
973         return ret;
974 }