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