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