]> wimlib.net Git - wimlib/blob - src/extract_image.c
17b12809f010a38fff6245371232f978a9c10c84
[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 #if TCHAR_IS_UTF16LE
61 #  include <wchar.h>
62 #endif
63
64 #ifndef __WIN32__
65 static int
66 extract_regular_file_linked(struct wim_dentry *dentry,
67                             const char *output_path,
68                             struct apply_args *args,
69                             struct wim_lookup_table_entry *lte)
70 {
71         /* This mode overrides the normal hard-link extraction and
72          * instead either symlinks or hardlinks *all* identical files in
73          * the WIM, even if they are in a different image (in the case
74          * of a multi-image extraction) */
75
76         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
77                 if (link(lte->extracted_file, output_path) != 0) {
78                         ERROR_WITH_ERRNO("Failed to hard link "
79                                          "`%s' to `%s'",
80                                          output_path, lte->extracted_file);
81                         return WIMLIB_ERR_LINK;
82                 }
83         } else {
84                 int num_path_components;
85                 int num_output_dir_path_components;
86                 size_t extracted_file_len;
87                 char *p;
88                 const char *p2;
89                 size_t i;
90
91                 num_path_components =
92                         get_num_path_components(dentry->full_path) - 1;
93                 num_output_dir_path_components =
94                         get_num_path_components(args->target);
95
96                 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
97                         num_path_components++;
98                         num_output_dir_path_components--;
99                 }
100                 extracted_file_len = strlen(lte->extracted_file);
101
102                 char buf[extracted_file_len + 3 * num_path_components + 1];
103                 p = &buf[0];
104
105                 for (i = 0; i < num_path_components; i++) {
106                         *p++ = '.';
107                         *p++ = '.';
108                         *p++ = '/';
109                 }
110                 p2 = lte->extracted_file;
111                 while (*p2 == '/')
112                         p2++;
113                 while (num_output_dir_path_components--)
114                         p2 = path_next_part(p2, NULL);
115                 strcpy(p, p2);
116                 if (symlink(buf, output_path) != 0) {
117                         ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
118                                          buf, lte->extracted_file);
119                         return WIMLIB_ERR_LINK;
120                 }
121         }
122         return 0;
123 }
124
125 static int
126 symlink_apply_unix_data(const mbchar *link,
127                         const struct wimlib_unix_data *unix_data)
128 {
129         if (lchown(link, unix_data->uid, unix_data->gid)) {
130                 if (errno == EPERM) {
131                         /* Ignore */
132                         WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
133                 } else {
134                         ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
135                         return WIMLIB_ERR_INVALID_DENTRY;
136                 }
137         }
138         return 0;
139 }
140
141 static int
142 fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
143 {
144         if (fchown(fd, unix_data->uid, unix_data->gid)) {
145                 if (errno == EPERM) {
146                         WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
147                         /* Ignore? */
148                 } else {
149                         ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
150                         return WIMLIB_ERR_INVALID_DENTRY;
151                 }
152         }
153
154         if (fchmod(fd, unix_data->mode)) {
155                 if (errno == EPERM) {
156                         WARNING_WITH_ERRNO("failed to set UNIX file mode");
157                         /* Ignore? */
158                 } else {
159                         ERROR_WITH_ERRNO("failed to set UNIX file mode");
160                         return WIMLIB_ERR_INVALID_DENTRY;
161                 }
162         }
163         return 0;
164 }
165
166 static int
167 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data)
168 {
169         int dfd = open(dir, O_RDONLY);
170         int ret;
171         if (dfd >= 0) {
172                 ret = fd_apply_unix_data(dfd, unix_data);
173                 if (close(dfd)) {
174                         ERROR_WITH_ERRNO("can't close directory `%s'", dir);
175                         ret = WIMLIB_ERR_MKDIR;
176                 }
177         } else {
178                 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
179                 ret = WIMLIB_ERR_MKDIR;
180         }
181         return ret;
182 }
183
184 static int
185 extract_regular_file_unlinked(struct wim_dentry *dentry,
186                               struct apply_args *args,
187                               const char *output_path,
188                               struct wim_lookup_table_entry *lte)
189 {
190         /* Normal mode of extraction.  Regular files and hard links are
191          * extracted in the way that they appear in the WIM. */
192
193         int out_fd;
194         int ret;
195         struct wim_inode *inode = dentry->d_inode;
196
197         if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
198                 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
199                                      WIMLIB_EXTRACT_FLAG_HARDLINK))))
200         {
201                 /* If the dentry is part of a hard link set of at least 2
202                  * dentries and one of the other dentries has already been
203                  * extracted, make a hard link to the file corresponding to this
204                  * already-extracted directory.  Otherwise, extract the file and
205                  * set the inode->i_extracted_file field so that other dentries
206                  * in the hard link group can link to it. */
207                 if (inode->i_nlink > 1) {
208                         if (inode->i_extracted_file) {
209                                 DEBUG("Extracting hard link `%s' => `%s'",
210                                       output_path, inode->i_extracted_file);
211                                 if (link(inode->i_extracted_file, output_path) != 0) {
212                                         ERROR_WITH_ERRNO("Failed to hard link "
213                                                          "`%s' to `%s'",
214                                                          output_path,
215                                                          inode->i_extracted_file);
216                                         return WIMLIB_ERR_LINK;
217                                 }
218                                 return 0;
219                         }
220                         FREE(inode->i_extracted_file);
221                         inode->i_extracted_file = STRDUP(output_path);
222                         if (!inode->i_extracted_file) {
223                                 ERROR("Failed to allocate memory for filename");
224                                 return WIMLIB_ERR_NOMEM;
225                         }
226                 }
227         }
228
229         /* Extract the contents of the file to @output_path. */
230
231         out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
232         if (out_fd == -1) {
233                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
234                                  output_path);
235                 return WIMLIB_ERR_OPEN;
236         }
237
238         if (!lte) {
239                 /* Empty file with no lookup table entry */
240                 DEBUG("Empty file `%s'.", output_path);
241                 ret = 0;
242                 goto out_extract_unix_data;
243         }
244
245         ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
246         if (ret != 0) {
247                 ERROR("Failed to extract resource to `%s'", output_path);
248                 goto out;
249         }
250
251 out_extract_unix_data:
252         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
253                 struct wimlib_unix_data unix_data;
254                 ret = inode_get_unix_data(inode, &unix_data, NULL);
255                 if (ret > 0)
256                         ;
257                 else if (ret < 0)
258                         ret = 0;
259                 else
260                         ret = fd_apply_unix_data(out_fd, &unix_data);
261                 if (ret != 0)
262                         goto out;
263         }
264         if (lte)
265                 args->progress.extract.completed_bytes += wim_resource_size(lte);
266 out:
267         if (close(out_fd) != 0) {
268                 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
269                 if (ret == 0)
270                         ret = WIMLIB_ERR_WRITE;
271         }
272         return ret;
273 }
274
275 static int
276 extract_regular_file(struct wim_dentry *dentry,
277                      struct apply_args *args,
278                      const char *output_path)
279 {
280         struct wim_lookup_table_entry *lte;
281         const struct wim_inode *inode = dentry->d_inode;
282
283         lte = inode_unnamed_lte_resolved(inode);
284
285         if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
286                                            WIMLIB_EXTRACT_FLAG_HARDLINK)))
287         {
288                 if (lte->extracted_file) {
289                         return extract_regular_file_linked(dentry, output_path, args, lte);
290                 } else {
291                         lte->extracted_file = STRDUP(output_path);
292                         if (!lte->extracted_file)
293                                 return WIMLIB_ERR_NOMEM;
294                 }
295         }
296         return extract_regular_file_unlinked(dentry, args, output_path, lte);
297 }
298
299 static int
300 extract_symlink(struct wim_dentry *dentry,
301                 struct apply_args *args,
302                 const char *output_path)
303 {
304         char target[4096];
305         ssize_t ret = inode_readlink(dentry->d_inode, target,
306                                      sizeof(target), args->w, 0);
307         struct wim_lookup_table_entry *lte;
308
309         if (ret <= 0) {
310                 ERROR("Could not read the symbolic link from dentry `%s'",
311                       dentry->full_path);
312                 return WIMLIB_ERR_INVALID_DENTRY;
313         }
314         ret = symlink(target, output_path);
315         if (ret != 0) {
316                 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
317                                  output_path, target);
318                 return WIMLIB_ERR_LINK;
319         }
320         lte = inode_unnamed_lte_resolved(dentry->d_inode);
321         wimlib_assert(lte != NULL);
322         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
323                 struct wimlib_unix_data unix_data;
324                 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
325                 if (ret > 0)
326                         ;
327                 else if (ret < 0)
328                         ret = 0;
329                 else
330                         ret = symlink_apply_unix_data(output_path, &unix_data);
331                 if (ret != 0)
332                         return ret;
333         }
334         args->progress.extract.completed_bytes += wim_resource_size(lte);
335         return 0;
336 }
337
338 #endif /* !__WIN32__ */
339
340 static int
341 extract_directory(struct wim_dentry *dentry,
342                   const tchar *output_path, bool is_root)
343 {
344         int ret;
345         struct stat stbuf;
346
347         ret = tstat(output_path, &stbuf);
348         if (ret == 0) {
349                 if (S_ISDIR(stbuf.st_mode)) {
350                         /*if (!is_root)*/
351                                 /*WARNING("`%s' already exists", output_path);*/
352                         goto dir_exists;
353                 } else {
354                         ERROR("`%"TS"' is not a directory", output_path);
355                         return WIMLIB_ERR_MKDIR;
356                 }
357         } else {
358                 if (errno != ENOENT) {
359                         ERROR_WITH_ERRNO("Failed to stat `%"TS"'", output_path);
360                         return WIMLIB_ERR_STAT;
361                 }
362         }
363
364         if (tmkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
365         {
366                 ERROR_WITH_ERRNO("Cannot create directory `%"TS"'", output_path);
367                 return WIMLIB_ERR_MKDIR;
368         }
369 dir_exists:
370         ret = 0;
371 #ifndef __WIN32__
372         if (dentry) {
373                 struct wimlib_unix_data unix_data;
374                 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
375                 if (ret > 0)
376                         ;
377                 else if (ret < 0)
378                         ret = 0;
379                 else
380                         ret = dir_apply_unix_data(output_path, &unix_data);
381         }
382 #endif
383         return ret;
384 }
385
386 #ifndef __WIN32__
387 static int unix_do_apply_dentry(const char *output_path,
388                                 size_t output_path_len,
389                                 struct wim_dentry *dentry,
390                                 struct apply_args *args)
391 {
392         const struct wim_inode *inode = dentry->d_inode;
393
394         if (inode_is_symlink(inode))
395                 return extract_symlink(dentry, args, output_path);
396         else if (inode_is_directory(inode))
397                 return extract_directory((args->extract_flags &
398                                            WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
399                                          output_path, false);
400         else
401                 return extract_regular_file(dentry, args, output_path);
402 }
403
404 static int
405 unix_do_apply_dentry_timestamps(const char *output_path,
406                                 size_t output_path_len,
407                                 const struct wim_dentry *dentry,
408                                 struct apply_args *args)
409 {
410         int ret;
411         const struct wim_inode *inode = dentry->d_inode;
412
413 #ifdef HAVE_UTIMENSAT
414         /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
415          * into `struct timespec's for passing to utimensat(), which is accurate
416          * to 1 nanosecond. */
417
418         struct timespec ts[2];
419         ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
420         ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
421         ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
422         if (ret)
423                 ret = errno;
424 #else
425         ret = ENOSYS;
426 #endif
427
428         if (ret == ENOSYS) {
429                 /* utimensat() not implemented or not available */
430         #ifdef HAVE_LUTIMES
431                 /* Convert the WIM timestamps, which are accurate to 100
432                  * nanoseconds, into `struct timeval's for passing to lutimes(),
433                  * which is accurate to 1 microsecond. */
434                 struct timeval tv[2];
435                 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
436                 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
437                 ret = lutimes(output_path, tv);
438                 if (ret)
439                         ret = errno;
440         #endif
441         }
442
443         if (ret == ENOSYS) {
444                 /* utimensat() and lutimes() both not implemented or not
445                  * available */
446         #ifdef HAVE_UTIME
447                 /* Convert the WIM timestamps, which are accurate to 100
448                  * nanoseconds, into a `struct utimbuf's for passing to
449                  * utime(), which is accurate to 1 second. */
450                 struct utimbuf buf;
451                 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
452                 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
453                 ret = utime(output_path, &buf);
454         #endif
455         }
456         if (ret && args->num_utime_warnings < 10) {
457                 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
458                                     output_path);
459                 args->num_utime_warnings++;
460         }
461         return 0;
462 }
463 #endif /* !__WIN32__ */
464
465 /* Extracts a file, directory, or symbolic link from the WIM archive. */
466 static int
467 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
468 {
469         struct apply_args *args = arg;
470         tchar *output_path;
471         size_t len;
472
473         len = tstrlen(args->target);
474         if (dentry_is_root(dentry)) {
475                 output_path = (tchar*)args->target;
476         } else {
477                 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
478                                      sizeof(tchar));
479                 memcpy(output_path, args->target, len * sizeof(tchar));
480                 memcpy(output_path + len, dentry->full_path, dentry->full_path_nbytes);
481                 len += dentry->full_path_nbytes / sizeof(tchar);
482                 output_path[len] = T('\0');
483         }
484 #ifdef __WIN32__
485         return win32_do_apply_dentry(output_path, len, dentry, args);
486 #else
487         return unix_do_apply_dentry(output_path, len, dentry, args);
488 #endif
489 }
490
491
492 /* Apply timestamps to an extracted file or directory */
493 static int
494 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
495 {
496         struct apply_args *args = arg;
497         size_t len;
498         tchar *output_path;
499
500         len = tstrlen(args->target);
501         if (dentry_is_root(dentry)) {
502                 output_path = (tchar*)args->target;
503         } else {
504                 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
505                                      sizeof(tchar));
506                 memcpy(output_path, args->target, len * sizeof(tchar));
507                 memcpy(output_path + len, dentry->full_path, dentry->full_path_nbytes);
508                 len += dentry->full_path_nbytes / sizeof(tchar);
509                 output_path[len] = T('\0');
510         }
511
512
513 #ifdef __WIN32__
514         return win32_do_apply_dentry_timestamps(output_path, len, dentry, args);
515 #else
516         return unix_do_apply_dentry_timestamps(output_path, len, dentry, args);
517 #endif
518 }
519
520 /* Extract a dentry if it hasn't already been extracted, and either the dentry
521  * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
522 static int
523 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
524 {
525         struct apply_args *args = arg;
526         int ret;
527
528         if (dentry->is_extracted)
529                 return 0;
530
531         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
532                 if (inode_unnamed_lte_resolved(dentry->d_inode))
533                         return 0;
534
535         if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
536              args->progress_func) {
537                 args->progress.extract.cur_path = dentry->full_path;
538                 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
539                                     &args->progress);
540         }
541         ret = args->apply_dentry(dentry, args);
542         if (ret == 0)
543                 dentry->is_extracted = 1;
544         return ret;
545 }
546
547 static int
548 cmp_streams_by_wim_position(const void *p1, const void *p2)
549 {
550         const struct wim_lookup_table_entry *lte1, *lte2;
551         lte1 = *(const struct wim_lookup_table_entry**)p1;
552         lte2 = *(const struct wim_lookup_table_entry**)p2;
553         if (lte1->resource_entry.offset < lte2->resource_entry.offset)
554                 return -1;
555         else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
556                 return 1;
557         else
558                 return 0;
559 }
560
561 static int
562 sort_stream_list_by_wim_position(struct list_head *stream_list)
563 {
564         struct list_head *cur;
565         size_t num_streams;
566         struct wim_lookup_table_entry **array;
567         size_t i;
568         size_t array_size;
569
570         num_streams = 0;
571         list_for_each(cur, stream_list)
572                 num_streams++;
573         array_size = num_streams * sizeof(array[0]);
574         array = MALLOC(array_size);
575         if (!array) {
576                 ERROR("Failed to allocate %zu bytes to sort stream entries",
577                       array_size);
578                 return WIMLIB_ERR_NOMEM;
579         }
580         cur = stream_list->next;
581         for (i = 0; i < num_streams; i++) {
582                 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
583                 cur = cur->next;
584         }
585
586         qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
587
588         INIT_LIST_HEAD(stream_list);
589         for (i = 0; i < num_streams; i++)
590                 list_add_tail(&array[i]->staging_list, stream_list);
591         FREE(array);
592         return 0;
593 }
594
595 static void
596 calculate_bytes_to_extract(struct list_head *stream_list,
597                            int extract_flags,
598                            union wimlib_progress_info *progress)
599 {
600         struct wim_lookup_table_entry *lte;
601         u64 total_bytes = 0;
602         u64 num_streams = 0;
603
604         /* For each stream to be extracted... */
605         list_for_each_entry(lte, stream_list, staging_list) {
606                 if (extract_flags &
607                     (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
608                 {
609                         /* In the symlink or hard link extraction mode, each
610                          * stream will be extracted one time regardless of how
611                          * many dentries share the stream. */
612                         wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
613                         if (!lte->extracted_file) {
614                                 num_streams++;
615                                 total_bytes += wim_resource_size(lte);
616                         }
617                 } else {
618                         num_streams += lte->out_refcnt;
619                         total_bytes += lte->out_refcnt * wim_resource_size(lte);
620                 }
621         }
622         progress->extract.num_streams = num_streams;
623         progress->extract.total_bytes = total_bytes;
624         progress->extract.completed_bytes = 0;
625 }
626
627 static void
628 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
629                                 struct list_head *stream_list)
630 {
631         if (++lte->out_refcnt == 1) {
632                 INIT_LIST_HEAD(&lte->inode_list);
633                 list_add_tail(&lte->staging_list, stream_list);
634         }
635 }
636
637 static void
638 inode_find_streams_for_extraction(struct wim_inode *inode,
639                                   struct list_head *stream_list,
640                                   int extract_flags)
641 {
642         struct wim_lookup_table_entry *lte;
643         bool inode_added = false;
644
645         lte = inode_unnamed_lte_resolved(inode);
646         if (lte) {
647                 maybe_add_stream_for_extraction(lte, stream_list);
648                 list_add_tail(&inode->i_lte_inode_list, &lte->inode_list);
649                 inode_added = true;
650         }
651 #ifdef WITH_NTFS_3G
652         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
653                 for (unsigned i = 0; i < inode->i_num_ads; i++) {
654                         if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
655                                 lte = inode->i_ads_entries[i].lte;
656                                 if (lte) {
657                                         maybe_add_stream_for_extraction(lte,
658                                                                         stream_list);
659                                         if (!inode_added) {
660                                                 list_add_tail(&inode->i_lte_inode_list,
661                                                               &lte->inode_list);
662                                                 inode_added = true;
663                                         }
664                                 }
665                         }
666                 }
667         }
668 #endif
669 }
670
671 static void
672 find_streams_for_extraction(struct hlist_head *inode_list,
673                             struct list_head *stream_list,
674                             struct wim_lookup_table *lookup_table,
675                             int extract_flags)
676 {
677         struct wim_inode *inode;
678         struct hlist_node *cur;
679         struct wim_dentry *dentry;
680
681         for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
682         INIT_LIST_HEAD(stream_list);
683         hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
684                 if (!inode->i_resolved)
685                         inode_resolve_ltes(inode, lookup_table);
686                 inode_for_each_dentry(dentry, inode)
687                         dentry->is_extracted = 0;
688                 inode_find_streams_for_extraction(inode, stream_list,
689                                                   extract_flags);
690         }
691 }
692
693 struct apply_operations {
694         int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
695         int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
696 };
697
698 static const struct apply_operations normal_apply_operations = {
699         .apply_dentry = apply_dentry_normal,
700         .apply_dentry_timestamps = apply_dentry_timestamps_normal,
701 };
702
703 #ifdef WITH_NTFS_3G
704 static const struct apply_operations ntfs_apply_operations = {
705         .apply_dentry = apply_dentry_ntfs,
706         .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
707 };
708 #endif
709
710 static int
711 apply_stream_list(struct list_head *stream_list,
712                   struct apply_args *args,
713                   const struct apply_operations *ops,
714                   wimlib_progress_func_t progress_func)
715 {
716         uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
717         uint64_t next_progress = bytes_per_progress;
718         struct wim_lookup_table_entry *lte;
719         struct wim_inode *inode;
720         struct wim_dentry *dentry;
721         int ret;
722
723         /* This complicated loop is essentially looping through the dentries,
724          * although dentries may be visited more than once (if a dentry contains
725          * two different nonempty streams) or not at all (if a dentry contains
726          * no non-empty streams).
727          *
728          * The outer loop is over the distinct streams to be extracted so that
729          * sequential reading of the WIM can be implemented. */
730
731         /* For each distinct stream to be extracted */
732         list_for_each_entry(lte, stream_list, staging_list) {
733                 /* For each inode that contains the stream */
734                 list_for_each_entry(inode, &lte->inode_list, i_lte_inode_list) {
735                         /* For each dentry that points to the inode */
736                         inode_for_each_dentry(dentry, inode) {
737                                 /* Extract the dentry if it was not already
738                                  * extracted */
739                                 ret = maybe_apply_dentry(dentry, args);
740                                 if (ret != 0)
741                                         return ret;
742                                 if (progress_func &&
743                                     args->progress.extract.completed_bytes >= next_progress)
744                                 {
745                                         progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
746                                                       &args->progress);
747                                         if (args->progress.extract.completed_bytes >=
748                                             args->progress.extract.total_bytes)
749                                         {
750                                                 next_progress = ~0ULL;
751                                         } else {
752                                                 next_progress =
753                                                         min (args->progress.extract.completed_bytes +
754                                                              bytes_per_progress,
755                                                              args->progress.extract.total_bytes);
756                                         }
757                                 }
758                         }
759                 }
760         }
761         return 0;
762 }
763
764 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
765  * @target. */
766 static int
767 extract_single_image(WIMStruct *w, int image,
768                      const tchar *target, int extract_flags,
769                      wimlib_progress_func_t progress_func)
770 {
771         int ret;
772         struct list_head stream_list;
773         struct hlist_head *inode_list;
774
775         struct apply_args args;
776         const struct apply_operations *ops;
777
778         args.w                  = w;
779         args.target             = target;
780         args.extract_flags      = extract_flags;
781         args.num_utime_warnings = 0;
782         args.stream_list        = &stream_list;
783         args.progress_func      = progress_func;
784
785         if (progress_func) {
786                 args.progress.extract.wimfile_name = w->filename;
787                 args.progress.extract.image = image;
788                 args.progress.extract.extract_flags = (extract_flags &
789                                                        WIMLIB_EXTRACT_MASK_PUBLIC);
790                 args.progress.extract.image_name = wimlib_get_image_name(w, image);
791                 args.progress.extract.target = target;
792         }
793
794 #ifdef WITH_NTFS_3G
795         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
796                 args.vol = ntfs_mount(target, 0);
797                 if (!args.vol) {
798                         ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
799                                          target);
800                         return WIMLIB_ERR_NTFS_3G;
801                 }
802                 ops = &ntfs_apply_operations;
803         } else
804 #endif
805                 ops = &normal_apply_operations;
806
807         ret = select_wim_image(w, image);
808         if (ret != 0)
809                 goto out;
810
811         inode_list = &w->image_metadata[image - 1].inode_list;
812
813         /* Build a list of the streams that need to be extracted */
814         find_streams_for_extraction(inode_list, &stream_list,
815                                     w->lookup_table, extract_flags);
816
817         /* Calculate the number of bytes of data that will be extracted */
818         calculate_bytes_to_extract(&stream_list, extract_flags,
819                                    &args.progress);
820
821         if (progress_func) {
822                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
823                               &args.progress);
824         }
825
826         /* If a sequential extraction was specified, sort the streams to be
827          * extracted by their position in the WIM file, so that the WIM file can
828          * be read sequentially. */
829         if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
830                 ret = sort_stream_list_by_wim_position(&stream_list);
831                 if (ret != 0) {
832                         WARNING("Falling back to non-sequential extraction");
833                         extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
834                 }
835         }
836
837         if (progress_func) {
838                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
839                               &args.progress);
840         }
841
842         /* Make the directory structure and extract empty files */
843         args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
844         args.apply_dentry = ops->apply_dentry;
845         ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
846         args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
847         if (ret != 0)
848                 goto out;
849
850         if (progress_func) {
851                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
852                               &args.progress);
853         }
854
855         /* Extract non-empty files */
856         ret = apply_stream_list(&stream_list, &args, ops, progress_func);
857         if (ret != 0)
858                 goto out;
859
860         if (progress_func) {
861                 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
862                               &args.progress);
863         }
864
865         /* Apply timestamps */
866         ret = for_dentry_in_tree_depth(wim_root_dentry(w),
867                                        ops->apply_dentry_timestamps, &args);
868         if (ret != 0)
869                 goto out;
870
871         if (progress_func) {
872                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
873                               &args.progress);
874         }
875 out:
876 #ifdef WITH_NTFS_3G
877         /* Unmount the NTFS volume */
878         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
879                 if (ntfs_umount(args.vol, FALSE) != 0) {
880                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
881                                          args.target);
882                         if (ret == 0)
883                                 ret = WIMLIB_ERR_NTFS_3G;
884                 }
885         }
886 #endif
887         return ret;
888 }
889
890
891 static bool
892 image_name_ok_as_dir(const tchar *image_name)
893 {
894         if (image_name == NULL)
895                 return false;
896         if (image_name[0] == T('\0'))
897                 return false;
898         if (tstrchr(image_name, T('/')))
899                 return false;
900         if (tstrchr(image_name, T('\\')))
901                 return false;
902         return true;
903 }
904
905 /* Extracts all images from the WIM to the directory @target, with the images
906  * placed in subdirectories named by their image names. */
907 static int
908 extract_all_images(WIMStruct *w,
909                    const tchar *target,
910                    int extract_flags,
911                    wimlib_progress_func_t progress_func)
912 {
913         size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
914         size_t output_path_len = tstrlen(target);
915         tchar buf[output_path_len + 1 + image_name_max_len + 1];
916         int ret;
917         int image;
918         const tchar *image_name;
919
920         ret = extract_directory(NULL, target, true);
921         if (ret)
922                 return ret;
923
924         wmemcpy(buf, target, output_path_len);
925         buf[output_path_len] = T('/');
926         for (image = 1; image <= w->hdr.image_count; image++) {
927                 image_name = wimlib_get_image_name(w, image);
928                 if (image_name_ok_as_dir(image_name)) {
929                         tstrcpy(buf + output_path_len + 1, image_name);
930                 } else {
931                         /* Image name is empty, or may not be representable in
932                          * the current locale, or contains path separators.  Use
933                          * the image number instead. */
934                         tsprintf(buf + output_path_len + 1, T("%d"), image);
935                 }
936                 ret = extract_single_image(w, image, buf, extract_flags,
937                                            progress_func);
938                 if (ret != 0)
939                         return ret;
940         }
941         return 0;
942 }
943
944 /* Extracts a single image or all images from a WIM file to a directory or NTFS
945  * volume. */
946 WIMLIBAPI int
947 wimlib_extract_image(WIMStruct *w,
948                      int image,
949                      const tchar *target,
950                      int extract_flags,
951                      WIMStruct **additional_swms,
952                      unsigned num_additional_swms,
953                      wimlib_progress_func_t progress_func)
954 {
955         struct wim_lookup_table *joined_tab, *w_tab_save;
956         int ret;
957
958         if (!target)
959                 return WIMLIB_ERR_INVALID_PARAM;
960
961         extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
962
963         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
964                         == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
965                 return WIMLIB_ERR_INVALID_PARAM;
966
967 #ifdef __WIN32__
968         if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
969                 ERROR("Extracting UNIX data is not supported on Windows");
970                 return WIMLIB_ERR_INVALID_PARAM;
971         }
972         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
973                 ERROR("Linked extraction modes are not supported on Windows");
974                 return WIMLIB_ERR_INVALID_PARAM;
975         }
976 #endif
977
978         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
979 #ifdef WITH_NTFS_3G
980                 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
981                         ERROR("Cannot specify symlink or hardlink flags when applying\n"
982                               "        directly to a NTFS volume");
983                         return WIMLIB_ERR_INVALID_PARAM;
984                 }
985                 if (image == WIMLIB_ALL_IMAGES) {
986                         ERROR("Can only apply a single image when applying "
987                               "directly to a NTFS volume");
988                         return WIMLIB_ERR_INVALID_PARAM;
989                 }
990                 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
991                         ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
992                         return WIMLIB_ERR_INVALID_PARAM;
993                 }
994 #else
995                 ERROR("wimlib was compiled without support for NTFS-3g, so");
996                 ERROR("we cannot apply a WIM image directly to a NTFS volume");
997                 return WIMLIB_ERR_UNSUPPORTED;
998 #endif
999         }
1000
1001         ret = verify_swm_set(w, additional_swms, num_additional_swms);
1002         if (ret != 0)
1003                 return ret;
1004
1005         if (num_additional_swms) {
1006                 ret = new_joined_lookup_table(w, additional_swms,
1007                                               num_additional_swms, &joined_tab);
1008                 if (ret != 0)
1009                         return ret;
1010                 w_tab_save = w->lookup_table;
1011                 w->lookup_table = joined_tab;
1012         }
1013
1014         if (image == WIMLIB_ALL_IMAGES) {
1015                 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1016                 ret = extract_all_images(w, target, extract_flags,
1017                                          progress_func);
1018         } else {
1019                 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1020                 ret = extract_single_image(w, image, target, extract_flags,
1021                                            progress_func);
1022         }
1023
1024         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1025                              WIMLIB_EXTRACT_FLAG_HARDLINK))
1026         {
1027                 for_lookup_table_entry(w->lookup_table,
1028                                        lte_free_extracted_file,
1029                                        NULL);
1030         }
1031
1032         if (num_additional_swms) {
1033                 free_lookup_table(w->lookup_table);
1034                 w->lookup_table = w_tab_save;
1035         }
1036         return ret;
1037 }