dfb8e4c04ef692d8457bf0d264275df34c6e79c1
[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 <dirent.h>
29
30 #ifdef __WIN32__
31 #  include "win32.h"
32 #else
33 #  ifdef HAVE_UTIME_H
34 #    include <utime.h>
35 #  endif
36 #  include "timestamp.h"
37 #  include <sys/time.h>
38 #endif
39
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46
47 #include "dentry.h"
48 #include "lookup_table.h"
49 #include "wimlib_internal.h"
50 #include "xml.h"
51
52 #ifdef WITH_NTFS_3G
53 #  include <ntfs-3g/volume.h>
54 #endif
55
56 #ifdef HAVE_ALLOCA_H
57 #  include <alloca.h>
58 #endif
59
60
61 #ifndef __WIN32__
62
63 /* Returns the number of components of @path.  */
64 static unsigned
65 get_num_path_components(const char *path)
66 {
67         unsigned num_components = 0;
68         while (*path) {
69                 while (*path == '/')
70                         path++;
71                 if (*path)
72                         num_components++;
73                 while (*path && *path != '/')
74                         path++;
75         }
76         return num_components;
77 }
78
79 static const char *
80 path_next_part(const char *path)
81 {
82         while (*path && *path != '/')
83                 path++;
84         while (*path && *path == '/')
85                 path++;
86         return path;
87 }
88
89 static int
90 extract_regular_file_linked(struct wim_dentry *dentry,
91                             const char *output_path,
92                             struct apply_args *args,
93                             struct wim_lookup_table_entry *lte)
94 {
95         /* This mode overrides the normal hard-link extraction and
96          * instead either symlinks or hardlinks *all* identical files in
97          * the WIM, even if they are in a different image (in the case
98          * of a multi-image extraction) */
99
100         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
101                 if (link(lte->extracted_file, output_path) != 0) {
102                         ERROR_WITH_ERRNO("Failed to hard link "
103                                          "`%s' to `%s'",
104                                          output_path, lte->extracted_file);
105                         return WIMLIB_ERR_LINK;
106                 }
107         } else {
108                 int num_path_components;
109                 int num_output_dir_path_components;
110                 size_t extracted_file_len;
111                 char *p;
112                 const char *p2;
113                 size_t i;
114
115                 num_path_components = get_num_path_components(dentry->_full_path) - 1;
116                 num_output_dir_path_components = get_num_path_components(args->target);
117
118                 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
119                         num_path_components++;
120                         num_output_dir_path_components--;
121                 }
122                 extracted_file_len = strlen(lte->extracted_file);
123
124                 char buf[extracted_file_len + 3 * num_path_components + 1];
125                 p = &buf[0];
126
127                 for (i = 0; i < num_path_components; i++) {
128                         *p++ = '.';
129                         *p++ = '.';
130                         *p++ = '/';
131                 }
132                 p2 = lte->extracted_file;
133                 while (*p2 == '/')
134                         p2++;
135                 while (num_output_dir_path_components > 0) {
136                         p2 = path_next_part(p2);
137                         num_output_dir_path_components--;
138                 }
139                 strcpy(p, p2);
140                 if (symlink(buf, output_path) != 0) {
141                         ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
142                                          buf, lte->extracted_file);
143                         return WIMLIB_ERR_LINK;
144                 }
145         }
146         return 0;
147 }
148
149 static int
150 symlink_apply_unix_data(const char *link,
151                         const struct wimlib_unix_data *unix_data)
152 {
153         if (lchown(link, unix_data->uid, unix_data->gid)) {
154                 if (errno == EPERM) {
155                         /* Ignore */
156                         WARNING_WITH_ERRNO("failed to set symlink UNIX "
157                                            "owner/group on \"%s\"", link);
158                 } else {
159                         ERROR_WITH_ERRNO("failed to set symlink UNIX "
160                                          "owner/group on \"%s\"", link);
161                         return WIMLIB_ERR_INVALID_DENTRY;
162                 }
163         }
164         return 0;
165 }
166
167 static int
168 fd_apply_unix_data(int fd, const char *path,
169                    const struct wimlib_unix_data *unix_data)
170 {
171         if (fchown(fd, unix_data->uid, unix_data->gid)) {
172                 if (errno == EPERM) {
173                         WARNING_WITH_ERRNO("failed to set file UNIX "
174                                            "owner/group on \"%s\"", path);
175                         /* Ignore? */
176                 } else {
177                         ERROR_WITH_ERRNO("failed to set file UNIX "
178                                          "owner/group on \"%s\"", path);
179                         return WIMLIB_ERR_INVALID_DENTRY;
180                 }
181         }
182
183         if (fchmod(fd, unix_data->mode)) {
184                 if (errno == EPERM) {
185                         WARNING_WITH_ERRNO("failed to set UNIX file mode "
186                                            "on \"%s\"", path);
187                         /* Ignore? */
188                 } else {
189                         ERROR_WITH_ERRNO("failed to set UNIX file mode "
190                                          "on \"%s\"", path);
191                         return WIMLIB_ERR_INVALID_DENTRY;
192                 }
193         }
194         return 0;
195 }
196
197 static int
198 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data)
199 {
200         int dfd = open(dir, O_RDONLY);
201         int ret;
202         if (dfd >= 0) {
203                 ret = fd_apply_unix_data(dfd, dir, unix_data);
204                 if (close(dfd)) {
205                         ERROR_WITH_ERRNO("can't close directory `%s'", dir);
206                         ret = WIMLIB_ERR_MKDIR;
207                 }
208         } else {
209                 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
210                 ret = WIMLIB_ERR_MKDIR;
211         }
212         return ret;
213 }
214
215 static int
216 extract_regular_file_unlinked(struct wim_dentry *dentry,
217                               struct apply_args *args,
218                               const char *output_path,
219                               struct wim_lookup_table_entry *lte)
220 {
221         /* Normal mode of extraction.  Regular files and hard links are
222          * extracted in the way that they appear in the WIM. */
223
224         int out_fd;
225         int ret;
226         struct wim_inode *inode = dentry->d_inode;
227
228         if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
229                 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
230                                      WIMLIB_EXTRACT_FLAG_HARDLINK))))
231         {
232                 /* If the dentry is part of a hard link set of at least 2
233                  * dentries and one of the other dentries has already been
234                  * extracted, make a hard link to the file corresponding to this
235                  * already-extracted directory.  Otherwise, extract the file and
236                  * set the inode->i_extracted_file field so that other dentries
237                  * in the hard link group can link to it. */
238                 if (inode->i_nlink > 1) {
239                         if (inode->i_extracted_file) {
240                                 DEBUG("Extracting hard link `%s' => `%s'",
241                                       output_path, inode->i_extracted_file);
242                                 if (link(inode->i_extracted_file, output_path) != 0) {
243                                         ERROR_WITH_ERRNO("Failed to hard link "
244                                                          "`%s' to `%s'",
245                                                          output_path,
246                                                          inode->i_extracted_file);
247                                         return WIMLIB_ERR_LINK;
248                                 }
249                                 return 0;
250                         }
251                         FREE(inode->i_extracted_file);
252                         inode->i_extracted_file = STRDUP(output_path);
253                         if (!inode->i_extracted_file) {
254                                 ERROR("Failed to allocate memory for filename");
255                                 return WIMLIB_ERR_NOMEM;
256                         }
257                 }
258         }
259
260         /* Extract the contents of the file to @output_path. */
261
262         out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
263         if (out_fd == -1) {
264                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
265                                  output_path);
266                 return WIMLIB_ERR_OPEN;
267         }
268
269         if (!lte) {
270                 /* Empty file with no lookup table entry */
271                 DEBUG("Empty file `%s'.", output_path);
272                 ret = 0;
273                 goto out_extract_unix_data;
274         }
275
276         ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
277         if (ret) {
278                 ERROR("Failed to extract resource to `%s'", output_path);
279                 goto out;
280         }
281
282 out_extract_unix_data:
283         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
284                 struct wimlib_unix_data unix_data;
285                 ret = inode_get_unix_data(inode, &unix_data, NULL);
286                 if (ret > 0)
287                         ;
288                 else if (ret < 0)
289                         ret = 0;
290                 else
291                         ret = fd_apply_unix_data(out_fd, output_path, &unix_data);
292                 if (ret)
293                         goto out;
294         }
295         if (lte)
296                 args->progress.extract.completed_bytes += wim_resource_size(lte);
297 out:
298         if (close(out_fd) != 0) {
299                 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
300                 if (ret == 0)
301                         ret = WIMLIB_ERR_WRITE;
302         }
303         return ret;
304 }
305
306 static int
307 extract_regular_file(struct wim_dentry *dentry,
308                      struct apply_args *args,
309                      const char *output_path)
310 {
311         struct wim_lookup_table_entry *lte;
312         const struct wim_inode *inode = dentry->d_inode;
313
314         lte = inode_unnamed_lte_resolved(inode);
315
316         if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
317                                            WIMLIB_EXTRACT_FLAG_HARDLINK)))
318         {
319                 if (lte->extracted_file) {
320                         return extract_regular_file_linked(dentry, output_path, args, lte);
321                 } else {
322                         lte->extracted_file = STRDUP(output_path);
323                         if (!lte->extracted_file)
324                                 return WIMLIB_ERR_NOMEM;
325                 }
326         }
327         return extract_regular_file_unlinked(dentry, args, output_path, lte);
328 }
329
330 static int
331 extract_symlink(struct wim_dentry *dentry,
332                 struct apply_args *args,
333                 const char *output_path)
334 {
335         char target[4096 + args->target_realpath_len];
336         char *fixed_target;
337         const struct wim_inode *inode = dentry->d_inode;
338
339         ssize_t ret = wim_inode_readlink(inode,
340                                          target + args->target_realpath_len,
341                                          sizeof(target) - args->target_realpath_len - 1);
342         struct wim_lookup_table_entry *lte;
343
344         if (ret <= 0) {
345                 ERROR("Could not read the symbolic link from dentry `%s'",
346                       dentry->_full_path);
347                 return WIMLIB_ERR_INVALID_DENTRY;
348         }
349         target[args->target_realpath_len + ret] = '\0';
350         if (target[args->target_realpath_len] == '/' &&
351             args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
352         {
353                 /* Fix absolute symbolic link target to point into the actual
354                  * extraction destination */
355                 memcpy(target, args->target_realpath,
356                        args->target_realpath_len);
357                 fixed_target = target;
358         } else {
359                 /* Keep same link target */
360                 fixed_target = target + args->target_realpath_len;
361         }
362         ret = symlink(fixed_target, output_path);
363         if (ret) {
364                 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
365                                  output_path, fixed_target);
366                 return WIMLIB_ERR_LINK;
367         }
368         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
369                 struct wimlib_unix_data unix_data;
370                 ret = inode_get_unix_data(inode, &unix_data, NULL);
371                 if (ret > 0)
372                         ;
373                 else if (ret < 0)
374                         ret = 0;
375                 else
376                         ret = symlink_apply_unix_data(output_path, &unix_data);
377                 if (ret)
378                         return ret;
379         }
380         lte = inode_unnamed_lte_resolved(inode);
381         wimlib_assert(lte != NULL);
382         args->progress.extract.completed_bytes += wim_resource_size(lte);
383         return 0;
384 }
385
386 #endif /* !__WIN32__ */
387
388 static int
389 extract_directory(struct wim_dentry *dentry,
390                   const tchar *output_path, bool is_root)
391 {
392         int ret;
393         struct stat stbuf;
394
395         ret = tstat(output_path, &stbuf);
396         if (ret == 0) {
397                 if (S_ISDIR(stbuf.st_mode)) {
398                         /*if (!is_root)*/
399                                 /*WARNING("`%s' already exists", output_path);*/
400                         goto dir_exists;
401                 } else {
402                         ERROR("`%"TS"' is not a directory", output_path);
403                         return WIMLIB_ERR_MKDIR;
404                 }
405         } else {
406                 if (errno != ENOENT) {
407                         ERROR_WITH_ERRNO("Failed to stat `%"TS"'", output_path);
408                         return WIMLIB_ERR_STAT;
409                 }
410         }
411
412         if (tmkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
413         {
414                 ERROR_WITH_ERRNO("Cannot create directory `%"TS"'", output_path);
415                 return WIMLIB_ERR_MKDIR;
416         }
417 dir_exists:
418         ret = 0;
419 #ifndef __WIN32__
420         if (dentry) {
421                 struct wimlib_unix_data unix_data;
422                 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
423                 if (ret > 0)
424                         ;
425                 else if (ret < 0)
426                         ret = 0;
427                 else
428                         ret = dir_apply_unix_data(output_path, &unix_data);
429         }
430 #endif
431         return ret;
432 }
433
434 #ifndef __WIN32__
435 static int
436 unix_do_apply_dentry(const char *output_path, size_t output_path_len,
437                      struct wim_dentry *dentry, struct apply_args *args)
438 {
439         const struct wim_inode *inode = dentry->d_inode;
440
441         if (inode_is_symlink(inode))
442                 return extract_symlink(dentry, args, output_path);
443         else if (inode_is_directory(inode))
444                 return extract_directory((args->extract_flags &
445                                            WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
446                                          output_path, false);
447         else
448                 return extract_regular_file(dentry, args, output_path);
449 }
450
451 static int
452 unix_do_apply_dentry_timestamps(const char *output_path,
453                                 size_t output_path_len,
454                                 struct wim_dentry *dentry,
455                                 struct apply_args *args)
456 {
457         int ret;
458         const struct wim_inode *inode = dentry->d_inode;
459
460 #ifdef HAVE_UTIMENSAT
461         /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
462          * into `struct timespec's for passing to utimensat(), which is accurate
463          * to 1 nanosecond. */
464
465         struct timespec ts[2];
466         ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
467         ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
468         ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
469         if (ret)
470                 ret = errno;
471 #else
472         ret = ENOSYS;
473 #endif
474
475         if (ret == ENOSYS) {
476                 /* utimensat() not implemented or not available */
477         #ifdef HAVE_LUTIMES
478                 /* Convert the WIM timestamps, which are accurate to 100
479                  * nanoseconds, into `struct timeval's for passing to lutimes(),
480                  * which is accurate to 1 microsecond. */
481                 struct timeval tv[2];
482                 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
483                 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
484                 ret = lutimes(output_path, tv);
485                 if (ret)
486                         ret = errno;
487         #endif
488         }
489
490         if (ret == ENOSYS) {
491                 /* utimensat() and lutimes() both not implemented or not
492                  * available */
493         #ifdef HAVE_UTIME
494                 /* Convert the WIM timestamps, which are accurate to 100
495                  * nanoseconds, into a `struct utimbuf's for passing to
496                  * utime(), which is accurate to 1 second. */
497                 struct utimbuf buf;
498                 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
499                 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
500                 ret = utime(output_path, &buf);
501         #endif
502         }
503         if (ret && args->num_utime_warnings < 10) {
504                 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
505                                     output_path);
506                 args->num_utime_warnings++;
507         }
508         return 0;
509 }
510 #endif /* !__WIN32__ */
511
512 static int
513 do_apply_op(struct wim_dentry *dentry, struct apply_args *args,
514             int (*apply_dentry_func)(const tchar *, size_t,
515                                      struct wim_dentry *, struct apply_args *))
516 {
517         tchar *p;
518         const tchar *full_path;
519         size_t full_path_nchars;
520
521         wimlib_assert(dentry->_full_path != NULL);
522         full_path = dentry->_full_path + 1;
523         full_path_nchars = dentry->full_path_nbytes / sizeof(tchar) - 1;
524         tchar output_path[args->target_nchars + 1 +
525                          (full_path_nchars - args->wim_source_path_nchars) + 1];
526         p = output_path;
527
528         /*print_dentry(dentry, NULL);*/
529         /*ERROR("%"TS" %"TS, args->target, dentry->_full_path);*/
530         /*ERROR("");*/
531
532         tmemcpy(p, args->target, args->target_nchars);
533         p += args->target_nchars;
534
535         if (dentry != args->extract_root) {
536                 *p++ = T('/');
537                 tmemcpy(p, full_path + args->wim_source_path_nchars,
538                         full_path_nchars - args->wim_source_path_nchars);
539                 p += full_path_nchars - args->wim_source_path_nchars;
540         }
541         *p = T('\0');
542         return (*apply_dentry_func)(output_path, p - output_path,
543                                     dentry, args);
544 }
545
546
547 /* Extracts a file, directory, or symbolic link from the WIM archive. */
548 static int
549 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
550 {
551 #ifdef __WIN32__
552         return do_apply_op(dentry, arg, win32_do_apply_dentry);
553 #else
554         return do_apply_op(dentry, arg, unix_do_apply_dentry);
555 #endif
556 }
557
558
559 /* Apply timestamps to an extracted file or directory */
560 static int
561 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
562 {
563 #ifdef __WIN32__
564         return do_apply_op(dentry, arg, win32_do_apply_dentry_timestamps);
565 #else
566         return do_apply_op(dentry, arg, unix_do_apply_dentry_timestamps);
567 #endif
568 }
569
570 static bool
571 dentry_is_descendent(const struct wim_dentry *dentry,
572                      const struct wim_dentry *ancestor)
573 {
574         for (;;) {
575                 if (dentry == ancestor)
576                         return true;
577                 if (dentry_is_root(dentry))
578                         return false;
579                 dentry = dentry->parent;
580         }
581 }
582
583 /* Extract a dentry if it hasn't already been extracted and either
584  * WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified, or the dentry is a directory
585  * and/or has no unnamed stream. */
586 static int
587 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
588 {
589         struct apply_args *args = arg;
590         int ret;
591
592         if (dentry->is_extracted)
593                 return 0;
594
595         if (!dentry_is_descendent(dentry, args->extract_root))
596                 return 0;
597
598         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS &&
599             !dentry_is_directory(dentry) &&
600             inode_unnamed_lte_resolved(dentry->d_inode) != NULL)
601                 return 0;
602
603         if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
604              args->progress_func) {
605                 args->progress.extract.cur_path = dentry->_full_path;
606                 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
607                                     &args->progress);
608         }
609         ret = args->apply_dentry(dentry, args);
610         if (ret == 0)
611                 dentry->is_extracted = 1;
612         return ret;
613 }
614
615 static void
616 calculate_bytes_to_extract(struct list_head *stream_list,
617                            int extract_flags,
618                            union wimlib_progress_info *progress)
619 {
620         struct wim_lookup_table_entry *lte;
621         u64 total_bytes = 0;
622         u64 num_streams = 0;
623
624         /* For each stream to be extracted... */
625         list_for_each_entry(lte, stream_list, extraction_list) {
626                 if (extract_flags &
627                     (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
628                 {
629                         /* In the symlink or hard link extraction mode, each
630                          * stream will be extracted one time regardless of how
631                          * many dentries share the stream. */
632                         wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
633                         if (!lte->extracted_file) {
634                                 num_streams++;
635                                 total_bytes += wim_resource_size(lte);
636                         }
637                 } else {
638                         num_streams += lte->out_refcnt;
639                         total_bytes += lte->out_refcnt * wim_resource_size(lte);
640                 }
641         }
642         progress->extract.num_streams = num_streams;
643         progress->extract.total_bytes = total_bytes;
644         progress->extract.completed_bytes = 0;
645 }
646
647 static void
648 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
649                                 struct list_head *stream_list)
650 {
651         if (++lte->out_refcnt == 1) {
652                 INIT_LIST_HEAD(&lte->inode_list);
653                 list_add_tail(&lte->extraction_list, stream_list);
654         }
655 }
656
657 static void
658 inode_find_streams_for_extraction(struct wim_inode *inode,
659                                   struct list_head *stream_list,
660                                   int extract_flags)
661 {
662         struct wim_lookup_table_entry *lte;
663         bool inode_added = false;
664
665         lte = inode_unnamed_lte_resolved(inode);
666         if (lte) {
667                 maybe_add_stream_for_extraction(lte, stream_list);
668                 list_add_tail(&inode->i_lte_inode_list, &lte->inode_list);
669                 inode_added = true;
670         }
671
672         /* Determine whether to include alternate data stream entries or not.
673          *
674          * UNIX:  Include them if extracting using NTFS-3g.
675          *
676          * Windows: Include them undconditionally, although if the filesystem is
677          * not NTFS we won't actually be able to extract them. */
678 #if defined(WITH_NTFS_3G)
679         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
680 #elif defined(__WIN32__)
681         if (1)
682 #else
683         if (0)
684 #endif
685         {
686                 for (unsigned i = 0; i < inode->i_num_ads; i++) {
687                         if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
688                                 lte = inode->i_ads_entries[i].lte;
689                                 if (lte) {
690                                         maybe_add_stream_for_extraction(lte,
691                                                                         stream_list);
692                                         if (!inode_added) {
693                                                 list_add_tail(&inode->i_lte_inode_list,
694                                                               &lte->inode_list);
695                                                 inode_added = true;
696                                         }
697                                 }
698                         }
699                 }
700         }
701 }
702
703 struct find_streams_ctx {
704         struct list_head stream_list;
705         int extract_flags;
706 };
707
708 static int
709 dentry_find_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
710 {
711         struct find_streams_ctx *ctx = _ctx;
712         struct wim_inode *inode = dentry->d_inode;
713
714         dentry->is_extracted = 0;
715         if (!inode->i_visited) {
716                 inode_find_streams_for_extraction(inode, &ctx->stream_list,
717                                                   ctx->extract_flags);
718                 inode->i_visited = 1;
719         }
720         return 0;
721 }
722
723 static int
724 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_lookup_table)
725 {
726         struct wim_inode *inode = dentry->d_inode;
727         struct wim_lookup_table *lookup_table = _lookup_table;
728         struct wim_lookup_table_entry *lte;
729
730         inode_resolve_ltes(inode, lookup_table);
731         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
732                 lte = inode_stream_lte_resolved(inode, i);
733                 if (lte)
734                         lte->out_refcnt = 0;
735         }
736         return 0;
737 }
738
739 static void
740 find_streams_for_extraction(struct wim_dentry *root,
741                             struct list_head *stream_list,
742                             struct wim_lookup_table *lookup_table,
743                             int extract_flags)
744 {
745         struct find_streams_ctx ctx;
746
747         INIT_LIST_HEAD(&ctx.stream_list);
748         ctx.extract_flags = extract_flags;
749         for_dentry_in_tree(root, dentry_resolve_and_zero_lte_refcnt, lookup_table);
750         for_dentry_in_tree(root, dentry_find_streams_to_extract, &ctx);
751         list_transfer(&ctx.stream_list, stream_list);
752 }
753
754 static int
755 dentry_mark_inode_unvisited(struct wim_dentry *dentry, void *_ignore)
756 {
757         dentry->d_inode->i_visited = 0;
758         return 0;
759 }
760
761 struct apply_operations {
762         int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
763         int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
764 };
765
766 static const struct apply_operations normal_apply_operations = {
767         .apply_dentry = apply_dentry_normal,
768         .apply_dentry_timestamps = apply_dentry_timestamps_normal,
769 };
770
771 #ifdef WITH_NTFS_3G
772 static const struct apply_operations ntfs_apply_operations = {
773         .apply_dentry = apply_dentry_ntfs,
774         .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
775 };
776 #endif
777
778 static int
779 apply_stream_list(struct list_head *stream_list,
780                   struct apply_args *args,
781                   const struct apply_operations *ops,
782                   wimlib_progress_func_t progress_func)
783 {
784         uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
785         uint64_t next_progress = bytes_per_progress;
786         struct wim_lookup_table_entry *lte;
787         struct wim_inode *inode;
788         struct wim_dentry *dentry;
789         int ret;
790
791         /* This complicated loop is essentially looping through the dentries,
792          * although dentries may be visited more than once (if a dentry contains
793          * two different nonempty streams) or not at all (if a dentry contains
794          * no non-empty streams).
795          *
796          * The outer loop is over the distinct streams to be extracted so that
797          * sequential reading of the WIM can be implemented. */
798
799         /* For each distinct stream to be extracted */
800         list_for_each_entry(lte, stream_list, extraction_list) {
801                 /* For each inode that contains the stream */
802                 list_for_each_entry(inode, &lte->inode_list, i_lte_inode_list) {
803                         /* For each dentry that points to the inode */
804                         inode_for_each_dentry(dentry, inode) {
805                                 /* Extract the dentry if it was not already
806                                  * extracted */
807                                 ret = maybe_apply_dentry(dentry, args);
808                                 if (ret)
809                                         return ret;
810                                 if (progress_func &&
811                                     args->progress.extract.completed_bytes >= next_progress)
812                                 {
813                                         progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
814                                                       &args->progress);
815                                         if (args->progress.extract.completed_bytes >=
816                                             args->progress.extract.total_bytes)
817                                         {
818                                                 next_progress = ~0ULL;
819                                         } else {
820                                                 next_progress =
821                                                         min (args->progress.extract.completed_bytes +
822                                                              bytes_per_progress,
823                                                              args->progress.extract.total_bytes);
824                                         }
825                                 }
826                         }
827                 }
828         }
829         return 0;
830 }
831
832 static int
833 sort_stream_list_by_wim_position(struct list_head *stream_list)
834 {
835         struct list_head *cur;
836         size_t num_streams;
837         struct wim_lookup_table_entry **array;
838         size_t i;
839         size_t array_size;
840
841         num_streams = 0;
842         list_for_each(cur, stream_list)
843                 num_streams++;
844         array_size = num_streams * sizeof(array[0]);
845         array = MALLOC(array_size);
846         if (!array) {
847                 ERROR("Failed to allocate %zu bytes to sort stream entries",
848                       array_size);
849                 return WIMLIB_ERR_NOMEM;
850         }
851         cur = stream_list->next;
852         for (i = 0; i < num_streams; i++) {
853                 array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
854                 cur = cur->next;
855         }
856
857         qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
858
859         INIT_LIST_HEAD(stream_list);
860         for (i = 0; i < num_streams; i++)
861                 list_add_tail(&array[i]->extraction_list, stream_list);
862         FREE(array);
863         return 0;
864 }
865
866 /*
867  * Extract a dentry to standard output.
868  *
869  * This obviously doesn't make sense in all cases.  We return an error if the
870  * dentry does not correspond to a regular file.  Otherwise we extract the
871  * unnamed data stream only.
872  */
873 static int
874 extract_dentry_to_stdout(struct wim_dentry *dentry)
875 {
876         int ret = 0;
877         if (!dentry_is_regular_file(dentry)) {
878                 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
879                       "extracted to standard output", dentry->_full_path);
880                 ret = WIMLIB_ERR_NOT_A_REGULAR_FILE;
881         } else {
882                 struct wim_lookup_table_entry *lte;
883
884                 lte = inode_unnamed_lte_resolved(dentry->d_inode);
885                 if (lte) {
886                         ret = extract_wim_resource_to_fd(lte, STDOUT_FILENO,
887                                                          wim_resource_size(lte));
888                 }
889         }
890         return ret;
891 }
892
893 /*
894  * extract_tree - Extract a file or directory tree from the currently selected
895  *                WIM image.
896  *
897  * @wim:        WIMStruct for the WIM file, with the desired image selected
898  *              (as wim->current_image).
899  * @wim_source_path:
900  *              "Canonical" (i.e. no leading or trailing slashes, path
901  *              separators forwald slashes) path inside the WIM image to
902  *              extract.  An empty string means the full image.
903  * @target:
904  *              Filesystem path to extract the file or directory tree to.
905  *
906  * @extract_flags:
907  *              WIMLIB_EXTRACT_FLAG_*.  Also, the private flag
908  *              WIMLIB_EXTRACT_FLAG_MULTI_IMAGE will be set if this is being
909  *              called through wimlib_extract_image() with WIMLIB_ALL_IMAGES as
910  *              the image.
911  *
912  * @progress_func:
913  *              If non-NULL, progress function for the extraction.  The messages
914  *              we may in this function are:
915  *
916  *              WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN or
917  *                      WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
918  *              WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN;
919  *              WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END;
920  *              WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY;
921  *              WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS;
922  *              WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS;
923  *              WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END or
924  *                      WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END.
925  *
926  * Returns 0 on success; nonzero on failure.
927  */
928 static int
929 extract_tree(WIMStruct *wim, const tchar *wim_source_path, const tchar *target,
930              int extract_flags, wimlib_progress_func_t progress_func)
931 {
932         int ret;
933         struct list_head stream_list;
934         struct apply_args args;
935         const struct apply_operations *ops;
936         struct wim_dentry *root;
937
938         memset(&args, 0, sizeof(args));
939
940         args.w                      = wim;
941         args.target                 = target;
942         args.extract_flags          = extract_flags;
943         args.progress_func          = progress_func;
944         args.target_nchars          = tstrlen(target);
945         args.wim_source_path_nchars = tstrlen(wim_source_path);
946
947         if (progress_func) {
948                 args.progress.extract.wimfile_name = wim->filename;
949                 args.progress.extract.image = wim->current_image;
950                 args.progress.extract.extract_flags = (extract_flags &
951                                                        WIMLIB_EXTRACT_MASK_PUBLIC);
952                 args.progress.extract.image_name = wimlib_get_image_name(wim,
953                                                                          wim->current_image);
954                 args.progress.extract.extract_root_wim_source_path = wim_source_path;
955                 args.progress.extract.target = target;
956         }
957
958 #ifdef WITH_NTFS_3G
959         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
960                 args.vol = ntfs_mount(target, 0);
961                 if (!args.vol) {
962                         ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
963                                          target);
964                         ret = WIMLIB_ERR_NTFS_3G;
965                         goto out;
966                 }
967                 ops = &ntfs_apply_operations;
968         } else
969 #endif
970                 ops = &normal_apply_operations;
971
972         root = get_dentry(wim, wim_source_path);
973         if (!root) {
974                 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
975                       wim_source_path, wim->current_image);
976                 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
977                 goto out_ntfs_umount;
978         }
979         args.extract_root = root;
980
981         ret = calculate_dentry_tree_full_paths(root);
982         if (ret)
983                 goto out_ntfs_umount;
984
985
986         /* Build a list of the streams that need to be extracted */
987         find_streams_for_extraction(root,
988                                     &stream_list,
989                                     wim->lookup_table, extract_flags);
990
991         /* Calculate the number of bytes of data that will be extracted */
992         calculate_bytes_to_extract(&stream_list, extract_flags,
993                                    &args.progress);
994
995         if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
996                 ret = extract_dentry_to_stdout(root);
997                 goto out_mark_inodes_unvisited;
998         }
999
1000         if (progress_func) {
1001                 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN :
1002                               WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
1003                               &args.progress);
1004         }
1005
1006         /* If a sequential extraction was specified, sort the streams to be
1007          * extracted by their position in the WIM file, so that the WIM file can
1008          * be read sequentially. */
1009         if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
1010                 ret = sort_stream_list_by_wim_position(&stream_list);
1011                 if (ret != 0) {
1012                         WARNING("Falling back to non-sequential extraction");
1013                         extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1014                 }
1015         }
1016
1017         if (progress_func) {
1018                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
1019                               &args.progress);
1020         }
1021
1022         /* Make the directory structure and extract empty files */
1023         args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1024         args.apply_dentry = ops->apply_dentry;
1025         ret = for_dentry_in_tree(root, maybe_apply_dentry, &args);
1026         args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1027         if (ret)
1028                 goto out_mark_inodes_unvisited;
1029
1030         if (progress_func) {
1031                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
1032                               &args.progress);
1033         }
1034
1035         if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
1036                 args.target_realpath = realpath(target, NULL);
1037                 if (!args.target_realpath) {
1038                         ret = WIMLIB_ERR_NOMEM;
1039                         goto out_mark_inodes_unvisited;
1040                 }
1041                 args.target_realpath_len = tstrlen(args.target_realpath);
1042         }
1043
1044         /* Extract non-empty files */
1045         ret = apply_stream_list(&stream_list, &args, ops, progress_func);
1046         if (ret)
1047                 goto out_free_target_realpath;
1048
1049         if (progress_func) {
1050                 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
1051                               &args.progress);
1052         }
1053
1054         /* Apply timestamps */
1055         ret = for_dentry_in_tree_depth(root,
1056                                        ops->apply_dentry_timestamps, &args);
1057         if (ret)
1058                 goto out_free_target_realpath;
1059
1060         if (progress_func) {
1061                 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END :
1062                               WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
1063                               &args.progress);
1064         }
1065 out_free_target_realpath:
1066         FREE(args.target_realpath);
1067 out_mark_inodes_unvisited:
1068         for_dentry_in_tree(root, dentry_mark_inode_unvisited, NULL);
1069 out_ntfs_umount:
1070 #ifdef WITH_NTFS_3G
1071         /* Unmount the NTFS volume */
1072         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1073                 if (ntfs_umount(args.vol, FALSE) != 0) {
1074                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
1075                                          args.target);
1076                         if (ret == 0)
1077                                 ret = WIMLIB_ERR_NTFS_3G;
1078                 }
1079         }
1080 #endif
1081 out:
1082         return ret;
1083 }
1084
1085 /* Validates a single wimlib_extract_command, mostly checking to make sure the
1086  * extract flags make sense. */
1087 static int
1088 check_extract_command(struct wimlib_extract_command *cmd, int wim_header_flags)
1089 {
1090         int extract_flags;
1091         bool is_entire_image = (cmd->wim_source_path[0] == T('\0'));
1092
1093         /* Empty destination path? */
1094         if (cmd->fs_dest_path[0] == T('\0'))
1095                 return WIMLIB_ERR_INVALID_PARAM;
1096
1097         extract_flags = cmd->extract_flags;
1098
1099         /* Specified both symlink and hardlink modes? */
1100         if ((extract_flags &
1101              (WIMLIB_EXTRACT_FLAG_SYMLINK |
1102               WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
1103                                                  WIMLIB_EXTRACT_FLAG_HARDLINK))
1104                 return WIMLIB_ERR_INVALID_PARAM;
1105
1106 #ifdef __WIN32__
1107         /* Wanted UNIX data on Win32? */
1108         if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1109                 ERROR("Extracting UNIX data is not supported on Windows");
1110                 return WIMLIB_ERR_INVALID_PARAM;
1111         }
1112         /* Wanted linked extraction on Windows?  (XXX This is possible, just not
1113          * implemented yet.) */
1114         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1115                              WIMLIB_EXTRACT_FLAG_HARDLINK))
1116         {
1117                 ERROR("Linked extraction modes are not supported on Windows");
1118                 return WIMLIB_ERR_INVALID_PARAM;
1119         }
1120 #endif
1121
1122         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1123                 /* NTFS-3g extraction mode requested */
1124 #ifdef WITH_NTFS_3G
1125                 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1126                                       WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1127                         ERROR("Cannot specify symlink or hardlink flags when applying\n"
1128                               "        directly to a NTFS volume");
1129                         return WIMLIB_ERR_INVALID_PARAM;
1130                 }
1131                 if (!is_entire_image &&
1132                     (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS))
1133                 {
1134                         ERROR("When applying directly to a NTFS volume you can "
1135                               "only extract a full image, not part of one");
1136                         return WIMLIB_ERR_INVALID_PARAM;
1137                 }
1138                 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1139                         ERROR("Cannot restore UNIX-specific data in "
1140                               "the NTFS extraction mode");
1141                         return WIMLIB_ERR_INVALID_PARAM;
1142                 }
1143 #else
1144                 ERROR("wimlib was compiled without support for NTFS-3g, so");
1145                 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1146                 return WIMLIB_ERR_UNSUPPORTED;
1147 #endif
1148         }
1149
1150         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1151                               WIMLIB_EXTRACT_FLAG_NORPFIX)) ==
1152                 (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
1153         {
1154                 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
1155                 return WIMLIB_ERR_INVALID_PARAM;
1156         }
1157
1158         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1159                               WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
1160         {
1161                 /* Do reparse point fixups by default if the WIM header says
1162                  * they are enabled and we are extracting a full image. */
1163                 if ((wim_header_flags & WIM_HDR_FLAG_RP_FIX) && is_entire_image)
1164                         extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1165         }
1166
1167         if (!is_entire_image && (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)) {
1168                 ERROR("Cannot specify --rpfix when not extracting entire image");
1169                 return WIMLIB_ERR_INVALID_PARAM;
1170         }
1171
1172         cmd->extract_flags = extract_flags;
1173         return 0;
1174 }
1175
1176
1177 /* Internal function to execute extraction commands for a WIM image. */
1178 static int
1179 do_wimlib_extract_files(WIMStruct *wim,
1180                         int image,
1181                         struct wimlib_extract_command *cmds,
1182                         size_t num_cmds,
1183                         wimlib_progress_func_t progress_func)
1184 {
1185         int ret;
1186         bool found_link_cmd = false;
1187         bool found_nolink_cmd = false;
1188
1189         /* Select the image from which we are extracting files */
1190         ret = select_wim_image(wim, image);
1191         if (ret)
1192                 return ret;
1193
1194         /* Make sure there are no streams in the WIM that have not been
1195          * checksummed yet. */
1196         ret = wim_checksum_unhashed_streams(wim);
1197         if (ret)
1198                 return ret;
1199
1200         /* Check for problems with the extraction commands */
1201         for (size_t i = 0; i < num_cmds; i++) {
1202                 ret = check_extract_command(&cmds[i], wim->hdr.flags);
1203                 if (ret)
1204                         return ret;
1205                 if (cmds[i].extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1206                                              WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1207                         found_link_cmd = true;
1208                 } else {
1209                         found_nolink_cmd = true;
1210                 }
1211                 if (found_link_cmd && found_nolink_cmd) {
1212                         ERROR("Symlink or hardlink extraction mode must "
1213                               "be set on all extraction commands");
1214                         return WIMLIB_ERR_INVALID_PARAM;
1215                 }
1216         }
1217
1218         /* Execute the extraction commands */
1219         for (size_t i = 0; i < num_cmds; i++) {
1220                 ret = extract_tree(wim,
1221                                    cmds[i].wim_source_path,
1222                                    cmds[i].fs_dest_path,
1223                                    cmds[i].extract_flags,
1224                                    progress_func);
1225                 if (ret)
1226                         return ret;
1227         }
1228         return 0;
1229 }
1230
1231 /* Extract files or directories from a WIM image. */
1232 WIMLIBAPI int
1233 wimlib_extract_files(WIMStruct *wim,
1234                      int image,
1235                      int default_extract_flags,
1236                      const struct wimlib_extract_command *cmds,
1237                      size_t num_cmds,
1238                      WIMStruct **additional_swms,
1239                      unsigned num_additional_swms,
1240                      wimlib_progress_func_t progress_func)
1241 {
1242         int ret;
1243         struct wimlib_extract_command *cmds_copy;
1244         struct wim_lookup_table *wim_tab_save, *joined_tab;
1245         int all_flags = 0;
1246
1247         default_extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1248
1249         ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1250         if (ret)
1251                 goto out;
1252
1253         if (num_cmds == 0)
1254                 goto out;
1255
1256         if (num_additional_swms) {
1257                 ret = new_joined_lookup_table(wim, additional_swms,
1258                                               num_additional_swms,
1259                                               &joined_tab);
1260                 if (ret)
1261                         goto out;
1262                 wim_tab_save = wim->lookup_table;
1263                 wim->lookup_table = joined_tab;
1264         }
1265
1266         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1267         if (!cmds_copy) {
1268                 ret = WIMLIB_ERR_NOMEM;
1269                 goto out_restore_lookup_table;
1270         }
1271
1272         for (size_t i = 0; i < num_cmds; i++) {
1273                 cmds_copy[i].extract_flags = (default_extract_flags |
1274                                                  cmds[i].extract_flags)
1275                                                 & WIMLIB_EXTRACT_MASK_PUBLIC;
1276                 all_flags |= cmds_copy[i].extract_flags;
1277
1278                 cmds_copy[i].wim_source_path = canonicalize_wim_path(cmds[i].wim_source_path);
1279                 if (!cmds_copy[i].wim_source_path) {
1280                         ret = WIMLIB_ERR_NOMEM;
1281                         goto out_free_cmds_copy;
1282                 }
1283
1284                 cmds_copy[i].fs_dest_path = canonicalize_fs_path(cmds[i].fs_dest_path);
1285                 if (!cmds_copy[i].fs_dest_path) {
1286                         ret = WIMLIB_ERR_NOMEM;
1287                         goto out_free_cmds_copy;
1288                 }
1289
1290         }
1291         ret = do_wimlib_extract_files(wim, image,
1292                                       cmds_copy, num_cmds,
1293                                       progress_func);
1294
1295         if (all_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1296                          WIMLIB_EXTRACT_FLAG_HARDLINK))
1297         {
1298                 for_lookup_table_entry(wim->lookup_table,
1299                                        lte_free_extracted_file, NULL);
1300         }
1301 out_free_cmds_copy:
1302         for (size_t i = 0; i < num_cmds; i++) {
1303                 FREE(cmds_copy[i].wim_source_path);
1304                 FREE(cmds_copy[i].fs_dest_path);
1305         }
1306         FREE(cmds_copy);
1307 out_restore_lookup_table:
1308         if (num_additional_swms) {
1309                 free_lookup_table(wim->lookup_table);
1310                 wim->lookup_table = wim_tab_save;
1311         }
1312 out:
1313         return ret;
1314 }
1315
1316 /*
1317  * Extracts an image from a WIM file.
1318  *
1319  * @wim:                WIMStruct for the WIM file.
1320  *
1321  * @image:              Number of the single image to extract.
1322  *
1323  * @target:             Directory or NTFS volume to extract the image to.
1324  *
1325  * @extract_flags:      Bitwise or of WIMLIB_EXTRACT_FLAG_*.
1326  *
1327  * @progress_func:      If non-NULL, a progress function to be called
1328  *                      periodically.
1329  *
1330  * Returns 0 on success; nonzero on failure.
1331  */
1332 static int
1333 extract_single_image(WIMStruct *wim, int image,
1334                      const tchar *target, int extract_flags,
1335                      wimlib_progress_func_t progress_func)
1336 {
1337         int ret;
1338         tchar *target_copy = canonicalize_fs_path(target);
1339         if (!target_copy)
1340                 return WIMLIB_ERR_NOMEM;
1341         struct wimlib_extract_command cmd = {
1342                 .wim_source_path = T(""),
1343                 .fs_dest_path = target_copy,
1344                 .extract_flags = extract_flags,
1345         };
1346         ret = do_wimlib_extract_files(wim, image, &cmd, 1, progress_func);
1347         FREE(target_copy);
1348         return ret;
1349 }
1350
1351 static const tchar * const filename_forbidden_chars =
1352 T(
1353 #ifdef __WIN32__
1354 "<>:\"/\\|?*"
1355 #else
1356 "/"
1357 #endif
1358 );
1359
1360 /* This function checks if it is okay to use a WIM image's name as a directory
1361  * name.  */
1362 static bool
1363 image_name_ok_as_dir(const tchar *image_name)
1364 {
1365         return image_name && *image_name &&
1366                 !tstrpbrk(image_name, filename_forbidden_chars);
1367 }
1368
1369 /* Extracts all images from the WIM to the directory @target, with the images
1370  * placed in subdirectories named by their image names. */
1371 static int
1372 extract_all_images(WIMStruct *wim,
1373                    const tchar *target,
1374                    int extract_flags,
1375                    wimlib_progress_func_t progress_func)
1376 {
1377         size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
1378         size_t output_path_len = tstrlen(target);
1379         tchar buf[output_path_len + 1 + image_name_max_len + 1];
1380         int ret;
1381         int image;
1382         const tchar *image_name;
1383
1384         ret = extract_directory(NULL, target, true);
1385         if (ret)
1386                 return ret;
1387
1388         tmemcpy(buf, target, output_path_len);
1389         buf[output_path_len] = T('/');
1390         for (image = 1; image <= wim->hdr.image_count; image++) {
1391                 image_name = wimlib_get_image_name(wim, image);
1392                 if (image_name_ok_as_dir(image_name)) {
1393                         tstrcpy(buf + output_path_len + 1, image_name);
1394                 } else {
1395                         /* Image name is empty or contains forbidden characters.
1396                          * Use image number instead. */
1397                         tsprintf(buf + output_path_len + 1, T("%d"), image);
1398                 }
1399                 ret = extract_single_image(wim, image, buf, extract_flags,
1400                                            progress_func);
1401                 if (ret)
1402                         return ret;
1403         }
1404         return 0;
1405 }
1406
1407 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1408  * volume. */
1409 WIMLIBAPI int
1410 wimlib_extract_image(WIMStruct *wim,
1411                      int image,
1412                      const tchar *target,
1413                      int extract_flags,
1414                      WIMStruct **additional_swms,
1415                      unsigned num_additional_swms,
1416                      wimlib_progress_func_t progress_func)
1417 {
1418         struct wim_lookup_table *joined_tab, *wim_tab_save;
1419         int ret;
1420
1421         extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1422
1423         ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1424         if (ret)
1425                 return ret;
1426
1427         if (num_additional_swms) {
1428                 ret = new_joined_lookup_table(wim, additional_swms,
1429                                               num_additional_swms, &joined_tab);
1430                 if (ret)
1431                         return ret;
1432                 wim_tab_save = wim->lookup_table;
1433                 wim->lookup_table = joined_tab;
1434         }
1435
1436         if (image == WIMLIB_ALL_IMAGES) {
1437                 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1438                 ret = extract_all_images(wim, target, extract_flags,
1439                                          progress_func);
1440         } else {
1441                 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1442                 ret = extract_single_image(wim, image, target, extract_flags,
1443                                            progress_func);
1444         }
1445
1446         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1447                              WIMLIB_EXTRACT_FLAG_HARDLINK))
1448         {
1449                 for_lookup_table_entry(wim->lookup_table,
1450                                        lte_free_extracted_file,
1451                                        NULL);
1452         }
1453         if (num_additional_swms) {
1454                 free_lookup_table(wim->lookup_table);
1455                 wim->lookup_table = wim_tab_save;
1456         }
1457         return ret;
1458 }