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