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