]> wimlib.net Git - wimlib/blob - src/extract.c
c42adac88daf88e7b7d7f8f903e7145954053aa4
[wimlib] / src / extract.c
1 /*
2  * extract.c
3  *
4  * Support for extracting WIM files.
5  *
6  * This code does NOT contain any filesystem-specific features.  In particular,
7  * security information (i.e. file permissions) and alternate data streams are
8  * ignored, except possibly to read an alternate data stream that contains
9  * symbolic link data.
10  */
11
12 /*
13  * Copyright (C) 2010 Carl Thijssen
14  * Copyright (C) 2012 Eric Biggers
15  *
16  * This file is part of wimlib, a library for working with WIM files.
17  *
18  * wimlib is free software; you can redistribute it and/or modify it under the
19  * terms of the GNU General Public License as published by the Free
20  * Software Foundation; either version 3 of the License, or (at your option)
21  * any later version.
22  *
23  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
24  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
25  * A PARTICULAR PURPOSE. See the GNU General Public License for more
26  * details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with wimlib; if not, see http://www.gnu.org/licenses/.
30  */
31
32
33 #include "config.h"
34
35 #include <dirent.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <sys/stat.h>
40 #include <stdlib.h>
41 #include <sys/time.h>
42
43 #ifdef HAVE_UTIME_H
44 #include <utime.h>
45 #endif
46
47
48 #include <unistd.h>
49
50 #include "dentry.h"
51 #include "lookup_table.h"
52 #include "timestamp.h"
53 #include "wimlib_internal.h"
54 #include "xml.h"
55
56 #ifdef WITH_NTFS_3G
57 #include <ntfs-3g/volume.h>
58 #endif
59
60 static int extract_regular_file_linked(struct dentry *dentry,
61                                        const char *output_path,
62                                        struct apply_args *args,
63                                        struct lookup_table_entry *lte)
64 {
65         /* This mode overrides the normal hard-link extraction and
66          * instead either symlinks or hardlinks *all* identical files in
67          * the WIM, even if they are in a different image (in the case
68          * of a multi-image extraction) */
69
70         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
71                 if (link(lte->extracted_file, output_path) != 0) {
72                         ERROR_WITH_ERRNO("Failed to hard link "
73                                          "`%s' to `%s'",
74                                          output_path, lte->extracted_file);
75                         return WIMLIB_ERR_LINK;
76                 }
77         } else {
78                 int num_path_components;
79                 int num_output_dir_path_components;
80                 size_t extracted_file_len;
81                 char *p;
82                 const char *p2;
83                 size_t i;
84
85                 num_path_components =
86                         get_num_path_components(dentry->full_path_utf8) - 1;
87                 num_output_dir_path_components =
88                         get_num_path_components(args->target);
89
90                 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
91                         num_path_components++;
92                         num_output_dir_path_components--;
93                 }
94                 extracted_file_len = strlen(lte->extracted_file);
95
96                 char buf[extracted_file_len + 3 * num_path_components + 1];
97                 p = &buf[0];
98
99                 for (i = 0; i < num_path_components; i++) {
100                         *p++ = '.';
101                         *p++ = '.';
102                         *p++ = '/';
103                 }
104                 p2 = lte->extracted_file;
105                 while (*p2 == '/')
106                         p2++;
107                 while (num_output_dir_path_components--)
108                         p2 = path_next_part(p2, NULL);
109                 strcpy(p, p2);
110                 if (symlink(buf, output_path) != 0) {
111                         ERROR_WITH_ERRNO("Failed to symlink `%s' to "
112                                          "`%s'",
113                                          buf, lte->extracted_file);
114                         return WIMLIB_ERR_LINK;
115                 }
116         }
117         return 0;
118 }
119
120 static int extract_regular_file_unlinked(struct dentry *dentry,
121                                          struct apply_args *args,
122                                          const char *output_path,
123                                          struct lookup_table_entry *lte)
124 {
125         /* Normal mode of extraction.  Regular files and hard links are
126          * extracted in the way that they appear in the WIM. */
127
128         int out_fd;
129         int ret;
130         struct inode *inode = dentry->d_inode;
131
132         if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
133                 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
134                                      WIMLIB_EXTRACT_FLAG_HARDLINK))))
135         {
136                 /* If the dentry is one of a hard link set of at least 2
137                  * dentries and one of the other dentries has already been
138                  * extracted, make a hard link to the file corresponding to this
139                  * already-extracted directory.  Otherwise, extract the file,
140                  * and set the inode->extracted_file field so that other
141                  * dentries in the hard link group can link to it. */
142                 if (inode->link_count > 1) {
143                         if (inode->extracted_file) {
144                                 DEBUG("Extracting hard link `%s' => `%s'",
145                                       output_path, inode->extracted_file);
146                                 if (link(inode->extracted_file, output_path) != 0) {
147                                         ERROR_WITH_ERRNO("Failed to hard link "
148                                                          "`%s' to `%s'",
149                                                          output_path,
150                                                          inode->extracted_file);
151                                         return WIMLIB_ERR_LINK;
152                                 }
153                                 return 0;
154                         }
155                         FREE(inode->extracted_file);
156                         inode->extracted_file = STRDUP(output_path);
157                         if (!inode->extracted_file) {
158                                 ERROR("Failed to allocate memory for filename");
159                                 return WIMLIB_ERR_NOMEM;
160                         }
161                 }
162         }
163
164         /* Extract the contents of the file to @output_path. */
165
166         out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
167         if (out_fd == -1) {
168                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
169                                  output_path);
170                 return WIMLIB_ERR_OPEN;
171         }
172
173         if (!lte) {
174                 /* Empty file with no lookup table entry */
175                 DEBUG("Empty file `%s'.", output_path);
176                 ret = 0;
177                 goto out;
178         }
179
180         ret = extract_full_wim_resource_to_fd(lte, out_fd);
181         if (ret != 0) {
182                 ERROR("Failed to extract resource to `%s'", output_path);
183                 goto out;
184         }
185         args->progress.extract.completed_bytes += wim_resource_size(lte);
186 out:
187         if (close(out_fd) != 0) {
188                 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
189                 ret = WIMLIB_ERR_WRITE;
190         }
191         return ret;
192 }
193
194 /*
195  * Extracts a regular file from the WIM archive.
196  */
197 static int extract_regular_file(struct dentry *dentry,
198                                 struct apply_args *args,
199                                 const char *output_path)
200 {
201         struct lookup_table_entry *lte;
202         const struct inode *inode = dentry->d_inode;
203
204         lte = inode_unnamed_lte_resolved(inode);
205
206         if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
207                                            WIMLIB_EXTRACT_FLAG_HARDLINK)))
208         {
209                 if (lte->extracted_file) {
210                         return extract_regular_file_linked(dentry, output_path, args, lte);
211                 } else {
212                         lte->extracted_file = STRDUP(output_path);
213                         if (!lte->extracted_file)
214                                 return WIMLIB_ERR_NOMEM;
215                 }
216         }
217         return extract_regular_file_unlinked(dentry, args, output_path, lte);
218 }
219
220 static int extract_symlink(struct dentry *dentry,
221                            struct apply_args *args,
222                            const char *output_path)
223 {
224         char target[4096];
225         ssize_t ret = inode_readlink(dentry->d_inode, target,
226                                      sizeof(target), args->w, 0);
227         struct lookup_table_entry *lte;
228
229         if (ret <= 0) {
230                 ERROR("Could not read the symbolic link from dentry `%s'",
231                       dentry->full_path_utf8);
232                 return WIMLIB_ERR_INVALID_DENTRY;
233         }
234         ret = symlink(target, output_path);
235         if (ret != 0) {
236                 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
237                                  output_path, target);
238                 return WIMLIB_ERR_LINK;
239         }
240         lte = inode_unnamed_lte_resolved(dentry->d_inode);
241         args->progress.extract.completed_bytes += wim_resource_size(lte);
242         return 0;
243 }
244
245 /*
246  * Extracts a directory from the WIM archive.
247  *
248  * @dentry:             The directory entry for the directory.
249  * @output_path:        The path to which the directory is to be extracted to.
250  * @return:             True on success, false on failure.
251  */
252 static int extract_directory(const char *output_path, bool is_root)
253 {
254         int ret;
255         struct stat stbuf;
256         ret = stat(output_path, &stbuf);
257         if (ret == 0) {
258                 if (S_ISDIR(stbuf.st_mode)) {
259                         /*if (!is_root)*/
260                                 /*WARNING("`%s' already exists", output_path);*/
261                         return 0;
262                 } else {
263                         ERROR("`%s' is not a directory", output_path);
264                         return WIMLIB_ERR_MKDIR;
265                 }
266         } else {
267                 if (errno != ENOENT) {
268                         ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
269                         return WIMLIB_ERR_STAT;
270                 }
271         }
272         /* Compute the output path directory to the directory. */
273         if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
274                                S_IROTH | S_IXOTH) != 0) {
275                 ERROR_WITH_ERRNO("Cannot create directory `%s'",
276                                  output_path);
277                 return WIMLIB_ERR_MKDIR;
278         }
279         return 0;
280 }
281
282 /*
283  * Extracts a file, directory, or symbolic link from the WIM archive.  For use
284  * in for_dentry_in_tree().
285  */
286 static int apply_dentry_normal(struct dentry *dentry, void *arg)
287 {
288         struct apply_args *args = arg;
289         int extract_flags = args->extract_flags;
290         struct inode *inode = dentry->d_inode;
291         size_t len;
292         int ret;
293
294         if (dentry->is_extracted)
295                 return 0;
296
297         if (extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
298                 if (inode_unnamed_lte_resolved(inode))
299                         return 0;
300
301         if ((extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
302              args->progress_func)
303         {
304                 args->progress.extract.cur_path = dentry->full_path_utf8;
305                 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
306                                     &args->progress);
307         }
308
309         len = strlen(args->target);
310         char output_path[len + dentry->full_path_utf8_len + 1];
311         memcpy(output_path, args->target, len);
312         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
313         output_path[len + dentry->full_path_utf8_len] = '\0';
314
315         if (inode_is_symlink(inode))
316                 ret = extract_symlink(dentry, args, output_path);
317         else if (inode_is_directory(inode))
318                 ret = extract_directory(output_path, false);
319         else
320                 ret = extract_regular_file(dentry, args, output_path);
321         if (ret == 0)
322                 dentry->is_extracted = 1;
323         return ret;
324 }
325
326 /* Apply timestamp to extracted file */
327 static int apply_dentry_timestamps_normal(struct dentry *dentry, void *arg)
328 {
329         struct apply_args *args = arg;
330         size_t len = strlen(args->target);
331         char output_path[len + dentry->full_path_utf8_len + 1];
332         const struct inode *inode = dentry->d_inode;
333         int ret;
334
335         memcpy(output_path, args->target, len);
336         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
337         output_path[len + dentry->full_path_utf8_len] = '\0';
338
339         struct timeval tv[2];
340         wim_timestamp_to_timeval(inode->last_access_time, &tv[0]);
341         wim_timestamp_to_timeval(inode->last_write_time, &tv[1]);
342         #ifdef HAVE_LUTIMES
343         ret = lutimes(output_path, tv);
344         #else
345         ret = -1;
346         errno = ENOSYS;
347         #endif
348         if (ret != 0) {
349                 #ifdef HAVE_UTIME
350                 if (errno == ENOSYS) {
351                         struct utimbuf buf;
352                         buf.actime = wim_timestamp_to_unix(inode->last_access_time);
353                         buf.modtime = wim_timestamp_to_unix(inode->last_write_time);
354                         if (utime(output_path, &buf) == 0)
355                                 return 0;
356                 }
357                 #endif
358                 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
359                         /*WARNING("Failed to set timestamp on file `%s': %s",*/
360                                 /*output_path, strerror(errno));*/
361                         args->num_lutimes_warnings++;
362                 }
363         }
364         return 0;
365 }
366
367 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
368 {
369         const struct lookup_table_entry *lte1, *lte2;
370         lte1 = *(const struct lookup_table_entry**)p1;
371         lte2 = *(const struct lookup_table_entry**)p2;
372         if (lte1->resource_entry.offset < lte2->resource_entry.offset)
373                 return -1;
374         else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
375                 return 1;
376         else
377                 return 0;
378 }
379
380 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
381 {
382         struct list_head *cur;
383         size_t num_streams;
384         struct lookup_table_entry **array;
385         size_t i;
386         size_t array_size;
387
388         num_streams = 0;
389         list_for_each(cur, stream_list)
390                 num_streams++;
391         array_size = num_streams * sizeof(array[0]);
392         array = MALLOC(array_size);
393         if (!array) {
394                 ERROR("Failed to allocate %zu bytes to sort stream entries",
395                       array_size);
396                 return WIMLIB_ERR_NOMEM;
397         }
398         cur = stream_list->next;
399         for (i = 0; i < num_streams; i++) {
400                 array[i] = container_of(cur, struct lookup_table_entry, staging_list);
401                 cur = cur->next;
402         }
403
404         qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
405
406         INIT_LIST_HEAD(stream_list);
407         for (i = 0; i < num_streams; i++)
408                 list_add_tail(&array[i]->staging_list, stream_list);
409         FREE(array);
410         return 0;
411 }
412
413 static void calculate_bytes_to_extract(struct list_head *stream_list,
414                                        int extract_flags,
415                                        union wimlib_progress_info *progress)
416 {
417         struct lookup_table_entry *lte;
418         struct inode *inode;
419         u64 total_bytes = 0;
420         u64 num_streams = 0;
421
422         /* For each stream to be extracted... */
423         list_for_each_entry(lte, stream_list, staging_list) {
424                 if (extract_flags &
425                     (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
426                 {
427                         /* In the symlink or hard link extraction mode, each
428                          * stream will be extracted one time regardless of how
429                          * many dentries share the stream. */
430                         wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
431                         if (!lte->extracted_file) {
432                                 num_streams++;
433                                 total_bytes += wim_resource_size(lte);
434                         }
435                 } else {
436                         num_streams += lte->out_refcnt;
437                         total_bytes += lte->out_refcnt * wim_resource_size(lte);
438                 }
439         }
440         progress->extract.num_streams = num_streams;
441         progress->extract.total_bytes = total_bytes;
442         progress->extract.completed_bytes = 0;
443 }
444
445 static void maybe_add_stream_for_extraction(struct lookup_table_entry *lte,
446                                             struct list_head *stream_list)
447 {
448         if (++lte->out_refcnt == 1) {
449                 INIT_LIST_HEAD(&lte->inode_list);
450                 list_add_tail(&lte->staging_list, stream_list);
451         }
452 }
453
454 static void inode_find_streams_for_extraction(struct inode *inode,
455                                               struct list_head *stream_list,
456                                               int extract_flags)
457 {
458         struct lookup_table_entry *lte;
459         bool inode_added = false;
460
461         lte = inode_unnamed_lte_resolved(inode);
462
463         if (lte) {
464                 maybe_add_stream_for_extraction(lte, stream_list);
465                 list_add_tail(&inode->lte_inode_list, &lte->inode_list);
466                 inode_added = true;
467         }
468 #ifdef WITH_NTFS_3G
469         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
470                 for (unsigned i = 0; i < inode->num_ads; i++) {
471                         if (inode->ads_entries[i].stream_name_len != 0) {
472                                 lte = inode->ads_entries[i].lte;
473                                 if (lte) {
474                                         maybe_add_stream_for_extraction(lte,
475                                                                         stream_list);
476                                         if (!inode_added) {
477                                                 list_add_tail(&inode->lte_inode_list,
478                                                               &lte->inode_list);
479                                                 inode_added = true;
480                                         }
481                                 }
482                         }
483                 }
484         }
485 #endif
486 }
487
488 static void find_streams_for_extraction(struct hlist_head *inode_list,
489                                         struct list_head *stream_list,
490                                         struct lookup_table *lookup_table,
491                                         int extract_flags)
492 {
493         struct inode *inode;
494         struct hlist_node *cur;
495         struct dentry *dentry;
496
497         for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
498         INIT_LIST_HEAD(stream_list);
499         hlist_for_each_entry(inode, cur, inode_list, hlist) {
500                 if (!inode->resolved)
501                         inode_resolve_ltes(inode, lookup_table);
502                 inode_for_each_dentry(dentry, inode)
503                         dentry->is_extracted = 0;
504                 inode_find_streams_for_extraction(inode, stream_list,
505                                                   extract_flags);
506         }
507 }
508
509 struct apply_operations {
510         int (*apply_dentry)(struct dentry *dentry, void *arg);
511         int (*apply_dentry_timestamps)(struct dentry *dentry, void *arg);
512 };
513
514 static const struct apply_operations normal_apply_operations = {
515         .apply_dentry = apply_dentry_normal,
516         .apply_dentry_timestamps = apply_dentry_timestamps_normal,
517 };
518
519 #ifdef WITH_NTFS_3G
520 static const struct apply_operations ntfs_apply_operations = {
521         .apply_dentry = apply_dentry_ntfs,
522         .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
523 };
524 #endif
525
526 static int apply_stream_list(struct list_head *stream_list,
527                              struct apply_args *args,
528                              const struct apply_operations *ops,
529                              wimlib_progress_func_t progress_func)
530 {
531         uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
532         uint64_t next_progress = bytes_per_progress;
533         struct lookup_table_entry *lte;
534         struct inode *inode;
535         struct dentry *dentry;
536         int ret = 0;
537
538         /* This complicated loop is actually just looping through the dentries
539          * (as for_dentry_in_tree() does), but the outer loop is actually over
540          * the distinct streams to be extracted so that sequential reading of
541          * the WIM can be implemented. */
542
543         /* For each distinct stream to be extracted */
544         list_for_each_entry(lte, stream_list, staging_list) {
545                 /* For each inode that contains the stream */
546                 list_for_each_entry(inode, &lte->inode_list, lte_inode_list) {
547                         /* For each dentry that points to the inode */
548                         inode_for_each_dentry(dentry, inode) {
549                                 ret = ops->apply_dentry(dentry, args);
550                                 if (ret != 0)
551                                         goto out;
552                                 if (args->progress.extract.completed_bytes >= next_progress
553                                     && args->progress.extract.total_bytes != 0)
554                                 {
555                                         progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
556                                                       &args->progress);
557                                         next_progress += bytes_per_progress;
558                                 }
559                         }
560                 }
561         }
562 out:
563         return ret;
564 }
565
566 static int extract_single_image(WIMStruct *w, int image,
567                                 const char *target, int extract_flags,
568                                 wimlib_progress_func_t progress_func)
569 {
570         int ret;
571         struct list_head stream_list;
572         struct hlist_head *inode_list;
573
574         struct apply_args args;
575         const struct apply_operations *ops;
576
577         args.w                    = w;
578         args.target               = target;
579         args.extract_flags        = extract_flags;
580         args.num_lutimes_warnings = 0;
581         args.target               = target;
582         args.stream_list          = &stream_list;
583         args.progress_func        = progress_func;
584
585         if (progress_func) {
586                 args.progress.extract.wimfile_name = w->filename;
587                 args.progress.extract.image = image;
588                 args.progress.extract.extract_flags = (extract_flags &
589                                                        WIMLIB_EXTRACT_MASK_PUBLIC);
590                 args.progress.extract.image_name = wimlib_get_image_name(w, image);
591                 args.progress.extract.target = target;
592         }
593
594 #ifdef WITH_NTFS_3G
595         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
596                 args.vol = ntfs_mount(target, 0);
597                 if (!args.vol) {
598                         ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
599                         return WIMLIB_ERR_NTFS_3G;
600                 }
601                 ops = &ntfs_apply_operations;
602         } else
603 #endif
604                 ops = &normal_apply_operations;
605
606         ret = select_wim_image(w, image);
607         if (ret != 0)
608                 goto out;
609
610         inode_list = &w->image_metadata[image - 1].inode_list;
611
612         find_streams_for_extraction(inode_list, &stream_list,
613                                     w->lookup_table, extract_flags);
614
615         calculate_bytes_to_extract(&stream_list, extract_flags,
616                                    &args.progress);
617
618         if (progress_func) {
619                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
620                               &args.progress);
621         }
622
623         if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
624                 ret = sort_stream_list_by_wim_position(&stream_list);
625                 if (ret != 0) {
626                         WARNING("Falling back to non-sequential extraction");
627                         extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
628                 }
629         }
630
631         if (progress_func) {
632                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
633                               &args.progress);
634         }
635
636         args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
637         ret = for_dentry_in_tree(wim_root_dentry(w), ops->apply_dentry, &args);
638         args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
639         if (ret != 0)
640                 goto out;
641
642         if (progress_func) {
643                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
644                               &args.progress);
645         }
646
647         ret = apply_stream_list(&stream_list, &args, ops, progress_func);
648         if (ret != 0)
649                 goto out;
650
651         if (progress_func)
652                 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS, NULL);
653
654         ret = for_dentry_in_tree_depth(wim_root_dentry(w),
655                                        ops->apply_dentry_timestamps, &args);
656         if (ret != 0)
657                 goto out;
658
659         if (progress_func) {
660                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
661                               &args.progress);
662         }
663 out:
664 #ifdef WITH_NTFS_3G
665         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
666                 if (ntfs_umount(args.vol, FALSE) != 0) {
667                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
668                         if (ret == 0)
669                                 ret = WIMLIB_ERR_NTFS_3G;
670                 }
671         }
672 #endif
673         return ret;
674 }
675
676
677 /* Extracts all images from the WIM to @output_dir, with the images placed in
678  * subdirectories named by their image names. */
679 static int extract_all_images(WIMStruct *w, const char *target,
680                               int extract_flags,
681                               wimlib_progress_func_t progress_func)
682 {
683         size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
684         size_t output_path_len = strlen(target);
685         char buf[output_path_len + 1 + image_name_max_len + 1];
686         int ret;
687         int image;
688         const char *image_name;
689
690         ret = extract_directory(target, true);
691         if (ret != 0)
692                 return ret;
693
694         memcpy(buf, target, output_path_len);
695         buf[output_path_len] = '/';
696         for (image = 1; image <= w->hdr.image_count; image++) {
697                 image_name = wimlib_get_image_name(w, image);
698                 if (image_name && *image_name) {
699                         strcpy(buf + output_path_len + 1, image_name);
700                 } else {
701                         /* Image name is empty. Use image number instead */
702                         sprintf(buf + output_path_len + 1, "%d", image);
703                 }
704                 ret = extract_single_image(w, image, buf, extract_flags,
705                                            progress_func);
706                 if (ret != 0)
707                         return ret;
708         }
709         return 0;
710 }
711
712 /* Extracts a single image or all images from a WIM file. */
713 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
714                                    const char *target,
715                                    int extract_flags,
716                                    WIMStruct **additional_swms,
717                                    unsigned num_additional_swms,
718                                    wimlib_progress_func_t progress_func)
719 {
720         struct lookup_table *joined_tab, *w_tab_save;
721         int ret;
722
723         if (!w || !target)
724                 return WIMLIB_ERR_INVALID_PARAM;
725
726         extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
727
728         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
729                         == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
730                 return WIMLIB_ERR_INVALID_PARAM;
731
732         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
733 #ifdef WITH_NTFS_3G
734                 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
735                         ERROR("Cannot specify symlink or hardlink flags when applying\n"
736                               "        directly to a NTFS volume");
737                         return WIMLIB_ERR_INVALID_PARAM;
738                 }
739                 if (image == WIMLIB_ALL_IMAGES) {
740                         ERROR("Can only apply a single image when applying "
741                               "directly to a NTFS volume");
742                         return WIMLIB_ERR_INVALID_PARAM;
743                 }
744 #else
745                 ERROR("wimlib was compiled without support for NTFS-3g, so");
746                 ERROR("we cannot apply a WIM image directly to a NTFS volume");
747                 return WIMLIB_ERR_UNSUPPORTED;
748 #endif
749         }
750
751         ret = verify_swm_set(w, additional_swms, num_additional_swms);
752         if (ret != 0)
753                 return ret;
754
755         if (num_additional_swms) {
756                 ret = new_joined_lookup_table(w, additional_swms,
757                                               num_additional_swms, &joined_tab);
758                 if (ret != 0)
759                         return ret;
760                 w_tab_save = w->lookup_table;
761                 w->lookup_table = joined_tab;
762         }
763
764         if (image == WIMLIB_ALL_IMAGES) {
765                 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
766                 ret = extract_all_images(w, target, extract_flags,
767                                          progress_func);
768         } else {
769                 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
770                 ret = extract_single_image(w, image, target, extract_flags,
771                                            progress_func);
772         }
773
774         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
775                              WIMLIB_EXTRACT_FLAG_HARDLINK))
776         {
777                 for_lookup_table_entry(w->lookup_table,
778                                        lte_free_extracted_file,
779                                        NULL);
780         }
781
782         if (num_additional_swms) {
783                 free_lookup_table(w->lookup_table);
784                 w->lookup_table = w_tab_save;
785         }
786         return ret;
787 }