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