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