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