5522a1fcfee417c55d99b07d17e5f715ba824a99
[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 = dentry->_full_path + 1;
519         size_t full_path_nchars = dentry->full_path_nbytes / sizeof(tchar) - 1;
520
521         tchar output_path[args->target_nchars + 1 +
522                          (full_path_nchars - args->wim_source_path_nchars) + 1];
523         p = output_path;
524
525         tmemcpy(p, args->target, args->target_nchars);
526         p += args->target_nchars;
527
528         if (dentry != args->extract_root) {
529                 *p++ = T('/');
530                 tmemcpy(p, full_path + args->wim_source_path_nchars,
531                         full_path_nchars - args->wim_source_path_nchars);
532                 p += full_path_nchars - args->wim_source_path_nchars;
533         }
534         *p = T('\0');
535         return (*apply_dentry_func)(output_path, p - output_path,
536                                     dentry, args);
537 }
538
539
540 /* Extracts a file, directory, or symbolic link from the WIM archive. */
541 static int
542 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
543 {
544 #ifdef __WIN32__
545         return do_apply_op(dentry, arg, win32_do_apply_dentry);
546 #else
547         return do_apply_op(dentry, arg, unix_do_apply_dentry);
548 #endif
549 }
550
551
552 /* Apply timestamps to an extracted file or directory */
553 static int
554 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
555 {
556 #ifdef __WIN32__
557         return do_apply_op(dentry, arg, win32_do_apply_dentry_timestamps);
558 #else
559         return do_apply_op(dentry, arg, unix_do_apply_dentry_timestamps);
560 #endif
561 }
562
563 /* Extract a dentry if it hasn't already been extracted and either
564  * WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified, or the dentry is a directory
565  * and/or has no unnamed stream. */
566 static int
567 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
568 {
569         struct apply_args *args = arg;
570         int ret;
571
572         if (dentry->is_extracted)
573                 return 0;
574
575         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS &&
576             !dentry_is_directory(dentry) &&
577             inode_unnamed_lte_resolved(dentry->d_inode) != NULL)
578                 return 0;
579
580         if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
581              args->progress_func) {
582                 args->progress.extract.cur_path = dentry->_full_path;
583                 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
584                                     &args->progress);
585         }
586         ret = args->apply_dentry(dentry, args);
587         if (ret == 0)
588                 dentry->is_extracted = 1;
589         return ret;
590 }
591
592 static void
593 calculate_bytes_to_extract(struct list_head *stream_list,
594                            int extract_flags,
595                            union wimlib_progress_info *progress)
596 {
597         struct wim_lookup_table_entry *lte;
598         u64 total_bytes = 0;
599         u64 num_streams = 0;
600
601         /* For each stream to be extracted... */
602         list_for_each_entry(lte, stream_list, extraction_list) {
603                 if (extract_flags &
604                     (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
605                 {
606                         /* In the symlink or hard link extraction mode, each
607                          * stream will be extracted one time regardless of how
608                          * many dentries share the stream. */
609                         wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
610                         if (!lte->extracted_file) {
611                                 num_streams++;
612                                 total_bytes += wim_resource_size(lte);
613                         }
614                 } else {
615                         num_streams += lte->out_refcnt;
616                         total_bytes += lte->out_refcnt * wim_resource_size(lte);
617                 }
618         }
619         progress->extract.num_streams = num_streams;
620         progress->extract.total_bytes = total_bytes;
621         progress->extract.completed_bytes = 0;
622 }
623
624 static void
625 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
626                                 struct list_head *stream_list)
627 {
628         if (++lte->out_refcnt == 1) {
629                 INIT_LIST_HEAD(&lte->inode_list);
630                 list_add_tail(&lte->extraction_list, stream_list);
631         }
632 }
633
634 static void
635 inode_find_streams_for_extraction(struct wim_inode *inode,
636                                   struct list_head *stream_list,
637                                   int extract_flags)
638 {
639         struct wim_lookup_table_entry *lte;
640         bool inode_added = false;
641
642         lte = inode_unnamed_lte_resolved(inode);
643         if (lte) {
644                 maybe_add_stream_for_extraction(lte, stream_list);
645                 list_add_tail(&inode->i_lte_inode_list, &lte->inode_list);
646                 inode_added = true;
647         }
648
649         /* Determine whether to include alternate data stream entries or not.
650          *
651          * UNIX:  Include them if extracting using NTFS-3g.
652          *
653          * Windows: Include them undconditionally, although if the filesystem is
654          * not NTFS we won't actually be able to extract them. */
655 #if defined(WITH_NTFS_3G)
656         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
657 #elif defined(__WIN32__)
658         if (1)
659 #else
660         if (0)
661 #endif
662         {
663                 for (unsigned i = 0; i < inode->i_num_ads; i++) {
664                         if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
665                                 lte = inode->i_ads_entries[i].lte;
666                                 if (lte) {
667                                         maybe_add_stream_for_extraction(lte,
668                                                                         stream_list);
669                                         if (!inode_added) {
670                                                 list_add_tail(&inode->i_lte_inode_list,
671                                                               &lte->inode_list);
672                                                 inode_added = true;
673                                         }
674                                 }
675                         }
676                 }
677         }
678 }
679
680 struct find_streams_ctx {
681         struct list_head stream_list;
682         int extract_flags;
683 };
684
685 static int
686 dentry_find_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
687 {
688         struct find_streams_ctx *ctx = _ctx;
689         struct wim_inode *inode = dentry->d_inode;
690
691         dentry->is_extracted = 0;
692         if (!inode->i_visited) {
693                 inode_find_streams_for_extraction(inode, &ctx->stream_list,
694                                                   ctx->extract_flags);
695                 inode->i_visited = 1;
696         }
697         return 0;
698 }
699
700 static int
701 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_lookup_table)
702 {
703         struct wim_inode *inode = dentry->d_inode;
704         struct wim_lookup_table *lookup_table = _lookup_table;
705         struct wim_lookup_table_entry *lte;
706
707         inode_resolve_ltes(inode, lookup_table);
708         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
709                 lte = inode_stream_lte_resolved(inode, i);
710                 if (lte)
711                         lte->out_refcnt = 0;
712         }
713         return 0;
714 }
715
716 static void
717 find_streams_for_extraction(struct wim_dentry *root,
718                             struct list_head *stream_list,
719                             struct wim_lookup_table *lookup_table,
720                             int extract_flags)
721 {
722         struct find_streams_ctx ctx;
723
724         INIT_LIST_HEAD(&ctx.stream_list);
725         ctx.extract_flags = extract_flags;
726         for_dentry_in_tree(root, dentry_resolve_and_zero_lte_refcnt, lookup_table);
727         for_dentry_in_tree(root, dentry_find_streams_to_extract, &ctx);
728         list_transfer(&ctx.stream_list, stream_list);
729 }
730
731 static int
732 dentry_mark_inode_unvisited(struct wim_dentry *dentry, void *_ignore)
733 {
734         dentry->d_inode->i_visited = 0;
735         return 0;
736 }
737
738 struct apply_operations {
739         int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
740         int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
741 };
742
743 static const struct apply_operations normal_apply_operations = {
744         .apply_dentry = apply_dentry_normal,
745         .apply_dentry_timestamps = apply_dentry_timestamps_normal,
746 };
747
748 #ifdef WITH_NTFS_3G
749 static const struct apply_operations ntfs_apply_operations = {
750         .apply_dentry = apply_dentry_ntfs,
751         .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
752 };
753 #endif
754
755 static int
756 apply_stream_list(struct list_head *stream_list,
757                   struct apply_args *args,
758                   const struct apply_operations *ops,
759                   wimlib_progress_func_t progress_func)
760 {
761         uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
762         uint64_t next_progress = bytes_per_progress;
763         struct wim_lookup_table_entry *lte;
764         struct wim_inode *inode;
765         struct wim_dentry *dentry;
766         int ret;
767
768         /* This complicated loop is essentially looping through the dentries,
769          * although dentries may be visited more than once (if a dentry contains
770          * two different nonempty streams) or not at all (if a dentry contains
771          * no non-empty streams).
772          *
773          * The outer loop is over the distinct streams to be extracted so that
774          * sequential reading of the WIM can be implemented. */
775
776         /* For each distinct stream to be extracted */
777         list_for_each_entry(lte, stream_list, extraction_list) {
778                 /* For each inode that contains the stream */
779                 list_for_each_entry(inode, &lte->inode_list, i_lte_inode_list) {
780                         /* For each dentry that points to the inode */
781                         inode_for_each_dentry(dentry, inode) {
782                                 /* Extract the dentry if it was not already
783                                  * extracted */
784                                 ret = maybe_apply_dentry(dentry, args);
785                                 if (ret)
786                                         return ret;
787                                 if (progress_func &&
788                                     args->progress.extract.completed_bytes >= next_progress)
789                                 {
790                                         progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
791                                                       &args->progress);
792                                         if (args->progress.extract.completed_bytes >=
793                                             args->progress.extract.total_bytes)
794                                         {
795                                                 next_progress = ~0ULL;
796                                         } else {
797                                                 next_progress =
798                                                         min (args->progress.extract.completed_bytes +
799                                                              bytes_per_progress,
800                                                              args->progress.extract.total_bytes);
801                                         }
802                                 }
803                         }
804                 }
805         }
806         return 0;
807 }
808
809 static int
810 sort_stream_list_by_wim_position(struct list_head *stream_list)
811 {
812         struct list_head *cur;
813         size_t num_streams;
814         struct wim_lookup_table_entry **array;
815         size_t i;
816         size_t array_size;
817
818         num_streams = 0;
819         list_for_each(cur, stream_list)
820                 num_streams++;
821         array_size = num_streams * sizeof(array[0]);
822         array = MALLOC(array_size);
823         if (!array) {
824                 ERROR("Failed to allocate %zu bytes to sort stream entries",
825                       array_size);
826                 return WIMLIB_ERR_NOMEM;
827         }
828         cur = stream_list->next;
829         for (i = 0; i < num_streams; i++) {
830                 array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
831                 cur = cur->next;
832         }
833
834         qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
835
836         INIT_LIST_HEAD(stream_list);
837         for (i = 0; i < num_streams; i++)
838                 list_add_tail(&array[i]->extraction_list, stream_list);
839         FREE(array);
840         return 0;
841 }
842
843
844 static int
845 extract_tree(WIMStruct *wim, int image,
846              const tchar *wim_source_path, const tchar *target,
847              int extract_flags, wimlib_progress_func_t progress_func)
848 {
849         int ret;
850         struct list_head stream_list;
851         struct apply_args args;
852         const struct apply_operations *ops;
853         struct wim_dentry *root;
854
855         memset(&args, 0, sizeof(args));
856
857         args.w                      = wim;
858         args.target                 = target;
859         args.extract_flags          = extract_flags;
860         args.progress_func          = progress_func;
861         args.target_nchars          = tstrlen(target);
862         args.wim_source_path_nchars = tstrlen(wim_source_path);
863
864         if (progress_func) {
865                 args.progress.extract.wimfile_name = wim->filename;
866                 args.progress.extract.image = image;
867                 args.progress.extract.extract_flags = (extract_flags &
868                                                        WIMLIB_EXTRACT_MASK_PUBLIC);
869                 args.progress.extract.image_name = wimlib_get_image_name(wim, image);
870                 args.progress.extract.extract_root_wim_source_path = wim_source_path;
871                 args.progress.extract.target = target;
872         }
873
874 #ifdef WITH_NTFS_3G
875         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
876                 args.vol = ntfs_mount(target, 0);
877                 if (!args.vol) {
878                         ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
879                                          target);
880                         ret = WIMLIB_ERR_NTFS_3G;
881                         goto out;
882                 }
883                 ops = &ntfs_apply_operations;
884         } else
885 #endif
886                 ops = &normal_apply_operations;
887
888         root = get_dentry(wim, wim_source_path);
889         if (!root) {
890                 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
891                       wim_source_path, image);
892                 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
893                 goto out_ntfs_umount;
894         }
895         args.extract_root = root;
896
897         /* Build a list of the streams that need to be extracted */
898         find_streams_for_extraction(root,
899                                     &stream_list,
900                                     wim->lookup_table, extract_flags);
901
902         /* Calculate the number of bytes of data that will be extracted */
903         calculate_bytes_to_extract(&stream_list, extract_flags,
904                                    &args.progress);
905
906         if (progress_func) {
907                 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN :
908                               WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
909                               &args.progress);
910         }
911
912         /* If a sequential extraction was specified, sort the streams to be
913          * extracted by their position in the WIM file, so that the WIM file can
914          * be read sequentially. */
915         if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
916                 ret = sort_stream_list_by_wim_position(&stream_list);
917                 if (ret != 0) {
918                         WARNING("Falling back to non-sequential extraction");
919                         extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
920                 }
921         }
922
923         if (progress_func) {
924                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
925                               &args.progress);
926         }
927
928         ret = calculate_dentry_tree_full_paths(root);
929         if (ret)
930                 goto out_mark_inodes_unvisited;
931
932         /* Make the directory structure and extract empty files */
933         args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
934         args.apply_dentry = ops->apply_dentry;
935         ret = for_dentry_in_tree(root, maybe_apply_dentry, &args);
936         args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
937         if (ret)
938                 goto out_mark_inodes_unvisited;
939
940         if (progress_func) {
941                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
942                               &args.progress);
943         }
944
945         if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
946                 args.target_realpath = realpath(target, NULL);
947                 if (!args.target_realpath) {
948                         ret = WIMLIB_ERR_NOMEM;
949                         goto out_mark_inodes_unvisited;
950                 }
951                 args.target_realpath_len = tstrlen(args.target_realpath);
952         }
953
954         /* Extract non-empty files */
955         ret = apply_stream_list(&stream_list, &args, ops, progress_func);
956         if (ret)
957                 goto out_free_target_realpath;
958
959         if (progress_func) {
960                 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
961                               &args.progress);
962         }
963
964         /* Apply timestamps */
965         ret = for_dentry_in_tree_depth(root,
966                                        ops->apply_dentry_timestamps, &args);
967         if (ret)
968                 goto out_free_target_realpath;
969
970         if (progress_func) {
971                 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END :
972                               WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
973                               &args.progress);
974         }
975 out_free_target_realpath:
976         FREE(args.target_realpath);
977 out_mark_inodes_unvisited:
978         for_dentry_in_tree(root, dentry_mark_inode_unvisited, NULL);
979 out_ntfs_umount:
980 #ifdef WITH_NTFS_3G
981         /* Unmount the NTFS volume */
982         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
983                 if (ntfs_umount(args.vol, FALSE) != 0) {
984                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
985                                          args.target);
986                         if (ret == 0)
987                                 ret = WIMLIB_ERR_NTFS_3G;
988                 }
989         }
990 #endif
991 out:
992         return ret;
993 }
994
995 static int
996 check_extract_command(struct wimlib_extract_command *cmd,
997                       bool multiple_commands,
998                       int wim_header_flags)
999 {
1000         int extract_flags;
1001         bool is_entire_image = (cmd->wim_source_path == T('\0'));
1002
1003         if (cmd->fs_dest_path[0] == T('\0'))
1004                 return WIMLIB_ERR_INVALID_PARAM;
1005
1006         extract_flags = cmd->extract_flags;
1007
1008         if ((extract_flags &
1009              (WIMLIB_EXTRACT_FLAG_SYMLINK |
1010               WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
1011                                                  WIMLIB_EXTRACT_FLAG_HARDLINK))
1012                 return WIMLIB_ERR_INVALID_PARAM;
1013
1014 #ifdef __WIN32__
1015         if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1016                 ERROR("Extracting UNIX data is not supported on Windows");
1017                 return WIMLIB_ERR_INVALID_PARAM;
1018         }
1019         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1020                              WIMLIB_EXTRACT_FLAG_HARDLINK))
1021         {
1022                 ERROR("Linked extraction modes are not supported on Windows");
1023                 return WIMLIB_ERR_INVALID_PARAM;
1024         }
1025 #endif
1026
1027         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1028 #ifdef WITH_NTFS_3G
1029                 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1030                         ERROR("Cannot specify symlink or hardlink flags when applying\n"
1031                               "        directly to a NTFS volume");
1032                         return WIMLIB_ERR_INVALID_PARAM;
1033                 }
1034                 if (!is_entire_image &&
1035                     (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS))
1036                 {
1037                         ERROR("Can only extract entire image when applying "
1038                               "directly to a NTFS volume");
1039                         return WIMLIB_ERR_INVALID_PARAM;
1040                 }
1041                 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1042                         ERROR("Cannot restore UNIX-specific data in "
1043                               "the NTFS extraction mode");
1044                         return WIMLIB_ERR_INVALID_PARAM;
1045                 }
1046 #else
1047                 ERROR("wimlib was compiled without support for NTFS-3g, so");
1048                 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1049                 return WIMLIB_ERR_UNSUPPORTED;
1050 #endif
1051         }
1052
1053         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1054                               WIMLIB_EXTRACT_FLAG_NORPFIX)) ==
1055                 (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
1056         {
1057                 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
1058                 return WIMLIB_ERR_INVALID_PARAM;
1059         }
1060
1061         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1062                               WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
1063         {
1064                 if ((wim_header_flags & WIM_HDR_FLAG_RP_FIX) && is_entire_image)
1065                         extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1066         }
1067
1068         if (!is_entire_image && (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)) {
1069                 ERROR("Cannot specify --rpfix when not extracting entire image");
1070                 return WIMLIB_ERR_INVALID_PARAM;
1071         }
1072
1073         cmd->extract_flags = extract_flags;
1074         return 0;
1075 }
1076
1077
1078 static int
1079 do_wimlib_extract_files(WIMStruct *wim,
1080                         int image,
1081                         struct wimlib_extract_command *cmds,
1082                         size_t num_cmds,
1083                         wimlib_progress_func_t progress_func)
1084 {
1085         int ret;
1086         bool found_link_cmd = false;
1087         bool found_nolink_cmd = false;
1088
1089         ret = select_wim_image(wim, image);
1090         if (ret)
1091                 return ret;
1092
1093         ret = wim_checksum_unhashed_streams(wim);
1094         if (ret)
1095                 return ret;
1096
1097         for (size_t i = 0; i < num_cmds; i++) {
1098                 ret = check_extract_command(&cmds[i], num_cmds > 1,
1099                                             wim->hdr.flags);
1100                 if (ret)
1101                         return ret;
1102                 if (cmds[i].extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1103                                              WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1104                         found_link_cmd = true;
1105                 } else {
1106                         found_nolink_cmd = true;
1107                 }
1108                 if (found_link_cmd && found_nolink_cmd) {
1109                         ERROR("Symlink or hardlink extraction mode must "
1110                               "be set on all extraction commands");
1111                         return WIMLIB_ERR_INVALID_PARAM;
1112                 }
1113         }
1114
1115         for (size_t i = 0; i < num_cmds; i++) {
1116                 ret = extract_tree(wim, image,
1117                                    cmds[i].wim_source_path,
1118                                    cmds[i].fs_dest_path,
1119                                    cmds[i].extract_flags,
1120                                    progress_func);
1121                 if (ret)
1122                         return ret;
1123         }
1124         return 0;
1125 }
1126
1127 WIMLIBAPI int
1128 wimlib_extract_files(WIMStruct *wim,
1129                      int image,
1130                      int default_extract_flags,
1131                      const struct wimlib_extract_command *cmds,
1132                      size_t num_cmds,
1133                      WIMStruct **additional_swms,
1134                      unsigned num_additional_swms,
1135                      wimlib_progress_func_t progress_func)
1136 {
1137         int ret;
1138         struct wimlib_extract_command *cmds_copy;
1139         struct wim_lookup_table *wim_tab_save, *joined_tab;
1140         int all_flags = 0;
1141
1142         default_extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1143
1144         ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1145         if (ret)
1146                 goto out;
1147
1148         if (num_additional_swms) {
1149                 ret = new_joined_lookup_table(wim, additional_swms,
1150                                               num_additional_swms,
1151                                               &joined_tab);
1152                 if (ret)
1153                         goto out;
1154                 wim_tab_save = wim->lookup_table;
1155                 wim->lookup_table = joined_tab;
1156         }
1157
1158         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1159         if (!cmds_copy) {
1160                 ret = WIMLIB_ERR_NOMEM;
1161                 goto out_restore_lookup_table;
1162         }
1163
1164         for (size_t i = 0; i < num_cmds; i++) {
1165                 cmds_copy[i].extract_flags = (default_extract_flags |
1166                                                  cmds[i].extract_flags)
1167                                                 & WIMLIB_EXTRACT_MASK_PUBLIC;
1168                 all_flags |= cmds_copy[i].extract_flags;
1169
1170                 cmds_copy[i].wim_source_path = canonicalize_wim_path(cmds[i].wim_source_path);
1171                 if (!cmds_copy[i].wim_source_path) {
1172                         ret = WIMLIB_ERR_NOMEM;
1173                         goto out_free_cmds_copy;
1174                 }
1175
1176                 cmds_copy[i].fs_dest_path = canonicalize_fs_path(cmds[i].fs_dest_path);
1177                 if (!cmds_copy[i].fs_dest_path) {
1178                         ret = WIMLIB_ERR_NOMEM;
1179                         goto out_free_cmds_copy;
1180                 }
1181
1182         }
1183         ret = do_wimlib_extract_files(wim, image,
1184                                       cmds_copy, num_cmds,
1185                                       progress_func);
1186
1187         if (all_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1188                          WIMLIB_EXTRACT_FLAG_HARDLINK))
1189         {
1190                 for_lookup_table_entry(wim->lookup_table,
1191                                        lte_free_extracted_file, NULL);
1192         }
1193 out_free_cmds_copy:
1194         for (size_t i = 0; i < num_cmds; i++) {
1195                 FREE(cmds_copy[i].wim_source_path);
1196                 FREE(cmds_copy[i].fs_dest_path);
1197         }
1198         FREE(cmds_copy);
1199 out_restore_lookup_table:
1200         if (num_additional_swms) {
1201                 free_lookup_table(wim->lookup_table);
1202                 wim->lookup_table = wim_tab_save;
1203         }
1204 out:
1205         return ret;
1206 }
1207
1208 static int
1209 extract_single_image(WIMStruct *wim, int image,
1210                      const tchar *target, int extract_flags,
1211                      wimlib_progress_func_t progress_func)
1212 {
1213         int ret;
1214         tchar *target_copy = canonicalize_fs_path(target);
1215         struct wimlib_extract_command cmd = {
1216                 .wim_source_path = T(""),
1217                 .fs_dest_path = target_copy,
1218                 .extract_flags = extract_flags,
1219         };
1220         ret = do_wimlib_extract_files(wim, image, &cmd, 1, progress_func);
1221         FREE(target_copy);
1222         return ret;
1223 }
1224
1225 static const tchar * const filename_forbidden_chars =
1226 T(
1227 #ifdef __WIN32__
1228 "<>:\"/\\|?*"
1229 #else
1230 "/"
1231 #endif
1232 );
1233
1234 /* This function checks if it is okay to use a WIM image's name as a directory
1235  * name.  */
1236 static bool
1237 image_name_ok_as_dir(const tchar *image_name)
1238 {
1239         return image_name && *image_name &&
1240                 !tstrpbrk(image_name, filename_forbidden_chars);
1241 }
1242
1243 /* Extracts all images from the WIM to the directory @target, with the images
1244  * placed in subdirectories named by their image names. */
1245 static int
1246 extract_all_images(WIMStruct *wim,
1247                    const tchar *target,
1248                    int extract_flags,
1249                    wimlib_progress_func_t progress_func)
1250 {
1251         size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
1252         size_t output_path_len = tstrlen(target);
1253         tchar buf[output_path_len + 1 + image_name_max_len + 1];
1254         int ret;
1255         int image;
1256         const tchar *image_name;
1257
1258         ret = extract_directory(NULL, target, true);
1259         if (ret)
1260                 return ret;
1261
1262         tmemcpy(buf, target, output_path_len);
1263         buf[output_path_len] = T('/');
1264         for (image = 1; image <= wim->hdr.image_count; image++) {
1265                 image_name = wimlib_get_image_name(wim, image);
1266                 if (image_name_ok_as_dir(image_name)) {
1267                         tstrcpy(buf + output_path_len + 1, image_name);
1268                 } else {
1269                         /* Image name is empty, or contains forbidden
1270                          * characters. */
1271                         tsprintf(buf + output_path_len + 1, T("%d"), image);
1272                 }
1273                 ret = extract_single_image(wim, image, buf, extract_flags,
1274                                            progress_func);
1275                 if (ret)
1276                         return ret;
1277         }
1278         return 0;
1279 }
1280
1281 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1282  * volume. */
1283 WIMLIBAPI int
1284 wimlib_extract_image(WIMStruct *wim,
1285                      int image,
1286                      const tchar *target,
1287                      int extract_flags,
1288                      WIMStruct **additional_swms,
1289                      unsigned num_additional_swms,
1290                      wimlib_progress_func_t progress_func)
1291 {
1292         struct wim_lookup_table *joined_tab, *wim_tab_save;
1293         int ret;
1294
1295         extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1296
1297         ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1298         if (ret)
1299                 return ret;
1300
1301         if (num_additional_swms) {
1302                 ret = new_joined_lookup_table(wim, additional_swms,
1303                                               num_additional_swms, &joined_tab);
1304                 if (ret)
1305                         return ret;
1306                 wim_tab_save = wim->lookup_table;
1307                 wim->lookup_table = joined_tab;
1308         }
1309
1310         if (image == WIMLIB_ALL_IMAGES) {
1311                 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1312                 ret = extract_all_images(wim, target, extract_flags,
1313                                          progress_func);
1314         } else {
1315                 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1316                 ret = extract_single_image(wim, image, target, extract_flags,
1317                                            progress_func);
1318         }
1319
1320         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1321                              WIMLIB_EXTRACT_FLAG_HARDLINK))
1322         {
1323                 for_lookup_table_entry(wim->lookup_table,
1324                                        lte_free_extracted_file,
1325                                        NULL);
1326         }
1327         if (num_additional_swms) {
1328                 free_lookup_table(wim->lookup_table);
1329                 wim->lookup_table = wim_tab_save;
1330         }
1331         return ret;
1332 }