]> wimlib.net Git - wimlib/blob - src/extract_image.c
Misc. Win32 fixes, comment updates
[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 #if defined(__CYGWIN__) || defined(__WIN32__)
29 #       include <windows.h>
30 #       ifdef ERROR
31 #               undef ERROR
32 #       endif
33 #       include <wchar.h>
34 #else
35 #       include <dirent.h>
36 #       ifdef HAVE_UTIME_H
37 #               include <utime.h>
38 #       endif
39 #       include "timestamp.h"
40 #       include <sys/time.h>
41 #endif
42
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/stat.h>
48
49 #include <unistd.h>
50
51 #include "dentry.h"
52 #include "lookup_table.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 #ifdef HAVE_ALLOCA_H
61 #include <alloca.h>
62 #endif
63
64 #if defined(__CYGWIN__) || defined(__WIN32__)
65
66 static int win32_set_reparse_data(HANDLE h,
67                                   u32 reparse_tag,
68                                   const struct wim_lookup_table_entry *lte,
69                                   const wchar_t *path)
70 {
71         int ret;
72         u8 *buf;
73         size_t len;
74
75         if (!lte) {
76                 WARNING("\"%ls\" is marked as a reparse point but had no reparse data",
77                         path);
78                 return 0;
79         }
80         len = wim_resource_size(lte);
81         if (len > 16 * 1024 - 8) {
82                 WARNING("\"%ls\": reparse data too long!", path);
83                 return 0;
84         }
85
86         /* The WIM stream omits the ReparseTag and ReparseDataLength fields, so
87          * leave 8 bytes of space for them at the beginning of the buffer, then
88          * set them manually. */
89         buf = alloca(len + 8);
90         ret = read_full_wim_resource(lte, buf + 8, 0);
91         if (ret)
92                 return ret;
93         *(u32*)(buf + 0) = reparse_tag;
94         *(u16*)(buf + 4) = len;
95         *(u16*)(buf + 6) = 0;
96
97         /* Set the reparse data on the open file using the
98          * FSCTL_SET_REPARSE_POINT ioctl.
99          *
100          * There are contradictions in Microsoft's documentation for this:
101          *
102          * "If hDevice was opened without specifying FILE_FLAG_OVERLAPPED,
103          * lpOverlapped is ignored."
104          *
105          * --- So setting lpOverlapped to NULL is okay since it's ignored.
106          *
107          * "If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an
108          * operation returns no output data and lpOutBuffer is NULL,
109          * DeviceIoControl makes use of lpBytesReturned. After such an
110          * operation, the value of lpBytesReturned is meaningless."
111          *
112          * --- So lpOverlapped not really ignored, as it affects another
113          *  parameter.  This is the actual behavior: lpBytesReturned must be
114          *  specified, even though lpBytesReturned is documented as:
115          *
116          *  "Not used with this operation; set to NULL."
117          */
118         DWORD bytesReturned;
119         if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, buf, len + 8,
120                              NULL, 0,
121                              &bytesReturned /* lpBytesReturned */,
122                              NULL /* lpOverlapped */))
123         {
124                 DWORD err = GetLastError();
125                 ERROR("Failed to set reparse data on \"%ls\"", path);
126                 win32_error(err);
127                 return WIMLIB_ERR_WRITE;
128         }
129         return 0;
130 }
131
132
133 static int win32_extract_chunk(const u8 *buf, size_t len, u64 offset, void *arg)
134 {
135         HANDLE hStream = arg;
136
137         DWORD nbytes_written;
138         wimlib_assert(len <= 0xffffffff);
139
140         if (!WriteFile(hStream, buf, len, &nbytes_written, NULL) ||
141             nbytes_written != len)
142         {
143                 DWORD err = GetLastError();
144                 ERROR("WriteFile(): write error");
145                 win32_error(err);
146                 return WIMLIB_ERR_WRITE;
147         }
148         return 0;
149 }
150
151 static int do_win32_extract_stream(HANDLE hStream, struct wim_lookup_table_entry *lte)
152 {
153         return extract_wim_resource(lte, wim_resource_size(lte),
154                                     win32_extract_chunk, hStream);
155 }
156
157 static int win32_extract_stream(const struct wim_inode *inode,
158                                 const wchar_t *path,
159                                 const wchar_t *stream_name_utf16,
160                                 struct wim_lookup_table_entry *lte)
161 {
162         wchar_t *stream_path;
163         HANDLE h;
164         int ret;
165         DWORD err;
166         DWORD creationDisposition = CREATE_ALWAYS;
167
168         if (stream_name_utf16) {
169                 /* Named stream.  Create a buffer that contains the UTF-16LE
170                  * string [./]@path:@stream_name_utf16.  This is needed to
171                  * create and open the stream using CreateFileW().  I'm not
172                  * aware of any other APIs to do this.  Note: note that the
173                  * '$DATA' suffix seems to be unneeded; Additional note: a "./"
174                  * prefix needs to be added when the path is not absolute to
175                  * avoid ambiguity with drive letters. */
176                 size_t stream_path_nchars;
177                 size_t path_nchars;
178                 size_t stream_name_nchars;
179                 const wchar_t *prefix;
180
181                 path_nchars = wcslen(path);
182                 stream_name_nchars = wcslen(stream_name_utf16);
183                 stream_path_nchars = path_nchars + 1 + stream_name_nchars;
184                 if (path[0] != L'/' && path[1] != L'\\') {
185                         prefix = L"./";
186                         stream_path_nchars += 2;
187                 } else {
188                         prefix = L"";
189                 }
190                 stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
191                 swprintf(stream_path, stream_path_nchars + 1, L"%ls%ls:%ls",
192                          prefix, path, stream_name_utf16);
193         } else {
194                 /* Unnamed stream; it's path is just the path to the file
195                  * itself. */
196                 stream_path = (wchar_t*)path;
197
198                 /* Directories must be created with CreateDirectoryW().  Then
199                  * the call to CreateFileW() will merely open the directory that
200                  * was already created rather than creating a new file. */
201                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
202                         if (!CreateDirectoryW(stream_path, NULL)) {
203                                 err = GetLastError();
204                                 if (err != ERROR_ALREADY_EXISTS) {
205                                         ERROR("Failed to create directory \"%ls\"",
206                                               path);
207                                         win32_error(err);
208                                         ret = WIMLIB_ERR_MKDIR;
209                                         goto fail;
210                                 }
211                         }
212                         DEBUG("Created directory \"%ls\"", stream_path);
213                         if (!(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
214                                 ret = 0;
215                                 goto out;
216                         }
217                         creationDisposition = OPEN_EXISTING;
218                 }
219         }
220
221         DEBUG("Opening \"%ls\"", stream_path);
222         h = CreateFileW(stream_path,
223                         GENERIC_WRITE | WRITE_OWNER | WRITE_DAC,
224                         0,
225                         NULL,
226                         creationDisposition,
227                         FILE_FLAG_OPEN_REPARSE_POINT |
228                             FILE_FLAG_BACKUP_SEMANTICS |
229                             inode->i_attributes,
230                         NULL);
231         if (h == INVALID_HANDLE_VALUE) {
232                 err = GetLastError();
233                 ERROR("Failed to create \"%ls\"", stream_path);
234                 win32_error(err);
235                 ret = WIMLIB_ERR_OPEN;
236                 goto fail;
237         }
238
239         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
240             stream_name_utf16 == NULL)
241         {
242                 DEBUG("Setting reparse data on \"%ls\"", path);
243                 ret = win32_set_reparse_data(h, inode->i_reparse_tag, lte, path);
244                 if (ret)
245                         goto fail_close_handle;
246         } else {
247                 if (lte) {
248                         DEBUG("Extracting \"%ls\" (len = %zu)",
249                               stream_path, wim_resource_size(lte));
250                         ret = do_win32_extract_stream(h, lte);
251                         if (ret)
252                                 goto fail_close_handle;
253                 }
254         }
255
256         DEBUG("Closing \"%ls\"", stream_path);
257         if (!CloseHandle(h)) {
258                 err = GetLastError();
259                 ERROR("Failed to close \"%ls\"", stream_path);
260                 win32_error(err);
261                 ret = WIMLIB_ERR_WRITE;
262                 goto fail;
263         }
264         ret = 0;
265         goto out;
266 fail_close_handle:
267         CloseHandle(h);
268 fail:
269         ERROR("Error extracting %ls", stream_path);
270 out:
271         return ret;
272 }
273
274 /*
275  * Creates a file, directory, or reparse point and extracts all streams to it
276  * (unnamed data stream and/or reparse point stream, plus any alternate data
277  * streams).  This in Win32-specific code.
278  *
279  * @inode:      WIM inode for this file or directory.
280  * @path:       UTF-16LE external path to extract the inode to.
281  *
282  * Returns 0 on success; nonzero on failure.
283  */
284 static int win32_extract_streams(struct wim_inode *inode,
285                                  const wchar_t *path)
286 {
287         struct wim_lookup_table_entry *unnamed_lte;
288         int ret;
289
290         unnamed_lte = inode_unnamed_lte_resolved(inode);
291         ret = win32_extract_stream(inode, path, NULL, unnamed_lte);
292         if (ret)
293                 goto out;
294         for (u16 i = 0; i < inode->i_num_ads; i++) {
295                 const struct wim_ads_entry *ads_entry = &inode->i_ads_entries[i];
296                 if (ads_entry->stream_name_len != 0) {
297                         /* Skip special UNIX data entries (see documentation for
298                          * WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) */
299                         if (ads_entry->stream_name_len == WIMLIB_UNIX_DATA_TAG_LEN
300                             && !memcmp(ads_entry->stream_name_utf8,
301                                        WIMLIB_UNIX_DATA_TAG,
302                                        WIMLIB_UNIX_DATA_TAG_LEN))
303                                 continue;
304                         ret = win32_extract_stream(inode,
305                                                    path,
306                                                    (const wchar_t*)ads_entry->stream_name,
307                                                    ads_entry->lte);
308                         if (ret)
309                                 break;
310                 }
311         }
312 out:
313         return ret;
314 }
315
316 /*
317  * Sets the security descriptor on an extracted file.  This is Win32-specific
318  * code.
319  *
320  * @inode:      The WIM inode that was extracted and has a security descriptor.
321  * @path:       UTF-16LE external path that the inode was extracted to.
322  * @sd:         Security data for the WIM image.
323  * @path_utf8:  @path in UTF-8 for error messages only.
324  *
325  * Returns 0 on success; nonzero on failure.
326  */
327 static int win32_set_security_data(const struct wim_inode *inode,
328                                    const wchar_t *path,
329                                    const struct wim_security_data *sd)
330 {
331         SECURITY_INFORMATION securityInformation = DACL_SECURITY_INFORMATION |
332                                                    SACL_SECURITY_INFORMATION |
333                                                    OWNER_SECURITY_INFORMATION |
334                                                    GROUP_SECURITY_INFORMATION;
335         if (!SetFileSecurityW(path, securityInformation,
336                               (PSECURITY_DESCRIPTOR)sd->descriptors[inode->i_security_id]))
337         {
338                 DWORD err = GetLastError();
339                 ERROR("Can't set security descriptor on \"%ls\"", path);
340                 win32_error(err);
341                 return WIMLIB_ERR_WRITE;
342         }
343         return 0;
344 }
345
346 #else /* __CYGWIN__ || __WIN32__ */
347 static int extract_regular_file_linked(struct wim_dentry *dentry,
348                                        const char *output_path,
349                                        struct apply_args *args,
350                                        struct wim_lookup_table_entry *lte)
351 {
352         /* This mode overrides the normal hard-link extraction and
353          * instead either symlinks or hardlinks *all* identical files in
354          * the WIM, even if they are in a different image (in the case
355          * of a multi-image extraction) */
356
357         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
358                 if (link(lte->extracted_file, output_path) != 0) {
359                         ERROR_WITH_ERRNO("Failed to hard link "
360                                          "`%s' to `%s'",
361                                          output_path, lte->extracted_file);
362                         return WIMLIB_ERR_LINK;
363                 }
364         } else {
365                 int num_path_components;
366                 int num_output_dir_path_components;
367                 size_t extracted_file_len;
368                 char *p;
369                 const char *p2;
370                 size_t i;
371
372                 num_path_components =
373                         get_num_path_components(dentry->full_path_utf8) - 1;
374                 num_output_dir_path_components =
375                         get_num_path_components(args->target);
376
377                 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
378                         num_path_components++;
379                         num_output_dir_path_components--;
380                 }
381                 extracted_file_len = strlen(lte->extracted_file);
382
383                 char buf[extracted_file_len + 3 * num_path_components + 1];
384                 p = &buf[0];
385
386                 for (i = 0; i < num_path_components; i++) {
387                         *p++ = '.';
388                         *p++ = '.';
389                         *p++ = '/';
390                 }
391                 p2 = lte->extracted_file;
392                 while (*p2 == '/')
393                         p2++;
394                 while (num_output_dir_path_components--)
395                         p2 = path_next_part(p2, NULL);
396                 strcpy(p, p2);
397                 if (symlink(buf, output_path) != 0) {
398                         ERROR_WITH_ERRNO("Failed to symlink `%s' to "
399                                          "`%s'",
400                                          buf, lte->extracted_file);
401                         return WIMLIB_ERR_LINK;
402                 }
403         }
404         return 0;
405 }
406
407 static int symlink_apply_unix_data(const char *link,
408                                    const struct wimlib_unix_data *unix_data)
409 {
410         if (lchown(link, unix_data->uid, unix_data->gid)) {
411                 if (errno == EPERM) {
412                         /* Ignore */
413                         WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
414                 } else {
415                         ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
416                         return WIMLIB_ERR_INVALID_DENTRY;
417                 }
418         }
419         return 0;
420 }
421
422 static int fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
423 {
424         if (fchown(fd, unix_data->uid, unix_data->gid)) {
425                 if (errno == EPERM) {
426                         WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
427                         /* Ignore? */
428                 } else {
429                         ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
430                         return WIMLIB_ERR_INVALID_DENTRY;
431                 }
432         }
433
434         if (fchmod(fd, unix_data->mode)) {
435                 if (errno == EPERM) {
436                         WARNING_WITH_ERRNO("failed to set UNIX file mode");
437                         /* Ignore? */
438                 } else {
439                         ERROR_WITH_ERRNO("failed to set UNIX file mode");
440                         return WIMLIB_ERR_INVALID_DENTRY;
441                 }
442         }
443         return 0;
444 }
445
446 static int dir_apply_unix_data(const char *dir,
447                                const struct wimlib_unix_data *unix_data)
448 {
449         int dfd = open(dir, O_RDONLY);
450         int ret;
451         if (dfd >= 0) {
452                 ret = fd_apply_unix_data(dfd, unix_data);
453                 if (close(dfd)) {
454                         ERROR_WITH_ERRNO("can't close directory `%s'", dir);
455                         ret = WIMLIB_ERR_MKDIR;
456                 }
457         } else {
458                 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
459                 ret = WIMLIB_ERR_MKDIR;
460         }
461         return ret;
462 }
463
464 static int extract_regular_file_unlinked(struct wim_dentry *dentry,
465                                          struct apply_args *args,
466                                          const char *output_path,
467                                          struct wim_lookup_table_entry *lte)
468 {
469         /* Normal mode of extraction.  Regular files and hard links are
470          * extracted in the way that they appear in the WIM. */
471
472         int out_fd;
473         int ret;
474         struct wim_inode *inode = dentry->d_inode;
475
476         if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
477                 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
478                                      WIMLIB_EXTRACT_FLAG_HARDLINK))))
479         {
480                 /* If the dentry is part of a hard link set of at least 2
481                  * dentries and one of the other dentries has already been
482                  * extracted, make a hard link to the file corresponding to this
483                  * already-extracted directory.  Otherwise, extract the file and
484                  * set the inode->i_extracted_file field so that other dentries
485                  * in the hard link group can link to it. */
486                 if (inode->i_nlink > 1) {
487                         if (inode->i_extracted_file) {
488                                 DEBUG("Extracting hard link `%s' => `%s'",
489                                       output_path, inode->i_extracted_file);
490                                 if (link(inode->i_extracted_file, output_path) != 0) {
491                                         ERROR_WITH_ERRNO("Failed to hard link "
492                                                          "`%s' to `%s'",
493                                                          output_path,
494                                                          inode->i_extracted_file);
495                                         return WIMLIB_ERR_LINK;
496                                 }
497                                 return 0;
498                         }
499                         FREE(inode->i_extracted_file);
500                         inode->i_extracted_file = STRDUP(output_path);
501                         if (!inode->i_extracted_file) {
502                                 ERROR("Failed to allocate memory for filename");
503                                 return WIMLIB_ERR_NOMEM;
504                         }
505                 }
506         }
507
508         /* Extract the contents of the file to @output_path. */
509
510         out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
511         if (out_fd == -1) {
512                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
513                                  output_path);
514                 return WIMLIB_ERR_OPEN;
515         }
516
517         if (!lte) {
518                 /* Empty file with no lookup table entry */
519                 DEBUG("Empty file `%s'.", output_path);
520                 ret = 0;
521                 goto out_extract_unix_data;
522         }
523
524         ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
525         if (ret != 0) {
526                 ERROR("Failed to extract resource to `%s'", output_path);
527                 goto out;
528         }
529
530 out_extract_unix_data:
531         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
532                 struct wimlib_unix_data unix_data;
533                 ret = inode_get_unix_data(inode, &unix_data, NULL);
534                 if (ret > 0)
535                         ;
536                 else if (ret < 0)
537                         ret = 0;
538                 else
539                         ret = fd_apply_unix_data(out_fd, &unix_data);
540                 if (ret != 0)
541                         goto out;
542         }
543         if (lte)
544                 args->progress.extract.completed_bytes += wim_resource_size(lte);
545 out:
546         if (close(out_fd) != 0) {
547                 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
548                 if (ret == 0)
549                         ret = WIMLIB_ERR_WRITE;
550         }
551         return ret;
552 }
553
554 static int extract_regular_file(struct wim_dentry *dentry,
555                                 struct apply_args *args,
556                                 const char *output_path)
557 {
558         struct wim_lookup_table_entry *lte;
559         const struct wim_inode *inode = dentry->d_inode;
560
561         lte = inode_unnamed_lte_resolved(inode);
562
563         if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
564                                            WIMLIB_EXTRACT_FLAG_HARDLINK)))
565         {
566                 if (lte->extracted_file) {
567                         return extract_regular_file_linked(dentry, output_path, args, lte);
568                 } else {
569                         lte->extracted_file = STRDUP(output_path);
570                         if (!lte->extracted_file)
571                                 return WIMLIB_ERR_NOMEM;
572                 }
573         }
574         return extract_regular_file_unlinked(dentry, args, output_path, lte);
575 }
576
577 static int extract_symlink(struct wim_dentry *dentry,
578                            struct apply_args *args,
579                            const char *output_path)
580 {
581         char target[4096];
582         ssize_t ret = inode_readlink(dentry->d_inode, target,
583                                      sizeof(target), args->w, 0);
584         struct wim_lookup_table_entry *lte;
585
586         if (ret <= 0) {
587                 ERROR("Could not read the symbolic link from dentry `%s'",
588                       dentry->full_path_utf8);
589                 return WIMLIB_ERR_INVALID_DENTRY;
590         }
591         ret = symlink(target, output_path);
592         if (ret != 0) {
593                 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
594                                  output_path, target);
595                 return WIMLIB_ERR_LINK;
596         }
597         lte = inode_unnamed_lte_resolved(dentry->d_inode);
598         wimlib_assert(lte != NULL);
599         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
600                 struct wimlib_unix_data unix_data;
601                 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
602                 if (ret > 0)
603                         ;
604                 else if (ret < 0)
605                         ret = 0;
606                 else
607                         ret = symlink_apply_unix_data(output_path, &unix_data);
608                 if (ret != 0)
609                         return ret;
610         }
611         args->progress.extract.completed_bytes += wim_resource_size(lte);
612         return 0;
613 }
614
615 #endif /* !(__CYGWIN__ || __WIN32__) */
616
617 static int extract_directory(struct wim_dentry *dentry,
618                              const char *output_path, bool is_root)
619 {
620         int ret;
621         struct stat stbuf;
622
623         ret = stat(output_path, &stbuf);
624         if (ret == 0) {
625                 if (S_ISDIR(stbuf.st_mode)) {
626                         /*if (!is_root)*/
627                                 /*WARNING("`%s' already exists", output_path);*/
628                         goto dir_exists;
629                 } else {
630                         ERROR("`%s' is not a directory", output_path);
631                         return WIMLIB_ERR_MKDIR;
632                 }
633         } else {
634                 if (errno != ENOENT) {
635                         ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
636                         return WIMLIB_ERR_STAT;
637                 }
638         }
639         if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
640                                S_IROTH | S_IXOTH) != 0) {
641                 ERROR_WITH_ERRNO("Cannot create directory `%s'",
642                                  output_path);
643                 return WIMLIB_ERR_MKDIR;
644         }
645 dir_exists:
646         ret = 0;
647 #if !defined(__CYGWIN__) && !defined(__WIN32__)
648         if (dentry) {
649                 struct wimlib_unix_data unix_data;
650                 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
651                 if (ret > 0)
652                         ;
653                 else if (ret < 0)
654                         ret = 0;
655                 else
656                         ret = dir_apply_unix_data(output_path, &unix_data);
657         }
658 #endif
659         return ret;
660 }
661
662 /* Extracts a file, directory, or symbolic link from the WIM archive. */
663 static int apply_dentry_normal(struct wim_dentry *dentry, void *arg)
664 {
665         struct apply_args *args = arg;
666         struct wim_inode *inode = dentry->d_inode;
667         size_t len;
668         char *output_path;
669
670         len = strlen(args->target);
671         if (dentry_is_root(dentry)) {
672                 output_path = (char*)args->target;
673         } else {
674                 output_path = alloca(len + dentry->full_path_utf8_len + 1);
675                 memcpy(output_path, args->target, len);
676                 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
677                 output_path[len + dentry->full_path_utf8_len] = '\0';
678                 len += dentry->full_path_utf8_len;
679         }
680
681 #if defined(__CYGWIN__) || defined(__WIN32__)
682         char *utf16_path;
683         size_t utf16_path_len;
684         DWORD err;
685         int ret;
686         ret = utf8_to_utf16(output_path, len, &utf16_path, &utf16_path_len);
687         if (ret)
688                 return ret;
689
690         if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
691                 /* Linked file, with another name already extracted.  Create a
692                  * hard link. */
693                 DEBUG("Creating hard link \"%ls => %ls\"",
694                       (const wchar_t*)utf16_path,
695                       (const wchar_t*)inode->i_extracted_file);
696                 if (!CreateHardLinkW((const wchar_t*)utf16_path,
697                                      (const wchar_t*)inode->i_extracted_file,
698                                      NULL))
699                 {
700                         err = GetLastError();
701                         ERROR("Can't create hard link \"%ls => %ls\"",
702                               (const wchar_t*)utf16_path,
703                               (const wchar_t*)inode->i_extracted_file);
704                         ret = WIMLIB_ERR_LINK;
705                         win32_error(err);
706                 }
707         } else {
708                 /* Create the file, directory, or reparse point, and extract the
709                  * data streams. */
710                 ret = win32_extract_streams(inode, (const wchar_t*)utf16_path);
711                 if (ret)
712                         goto out_free_utf16_path;
713
714                 /* Set security descriptor if present */
715                 if (inode->i_security_id != -1) {
716                         DEBUG("Setting security descriptor %d on %s",
717                               inode->i_security_id, output_path);
718                         ret = win32_set_security_data(inode,
719                                                       (const wchar_t*)utf16_path,
720                                                       wim_const_security_data(args->w));
721                         if (ret)
722                                 goto out_free_utf16_path;
723                 }
724                 if (inode->i_nlink > 1) {
725                         /* Save extracted path for a later call to
726                          * CreateHardLinkW() if this inode has multiple links.
727                          * */
728                         inode->i_extracted_file = utf16_path;
729                         goto out;
730                 }
731         }
732 out_free_utf16_path:
733         FREE(utf16_path);
734 out:
735         return ret;
736 #else
737         if (inode_is_symlink(inode))
738                 return extract_symlink(dentry, args, output_path);
739         else if (inode_is_directory(inode))
740                 return extract_directory((args->extract_flags &
741                                            WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
742                                          output_path, false);
743         else
744                 return extract_regular_file(dentry, args, output_path);
745 #endif
746 }
747
748 /* Apply timestamps to an extracted file or directory */
749 static int apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
750 {
751         struct apply_args *args = arg;
752         size_t len;
753         char *output_path;
754         int ret;
755         const struct wim_inode *inode = dentry->d_inode;
756
757         len = strlen(args->target);
758         if (dentry_is_root(dentry)) {
759                 output_path = (char*)args->target;
760         } else {
761                 output_path = alloca(len + dentry->full_path_utf8_len + 1);
762                 memcpy(output_path, args->target, len);
763                 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
764                 output_path[len + dentry->full_path_utf8_len] = '\0';
765                 len += dentry->full_path_utf8_len;
766         }
767
768 #if defined(__CYGWIN__) || defined(__WIN32__)
769         /* Win32 */
770         char *utf16_path;
771         size_t utf16_path_len;
772         DWORD err;
773         HANDLE h;
774         BOOL bret1, bret2;
775
776         ret = utf8_to_utf16(output_path, len, &utf16_path, &utf16_path_len);
777         if (ret)
778                 return ret;
779
780         DEBUG("Opening \"%ls\" to set timestamps", utf16_path);
781         h = CreateFileW((const wchar_t*)utf16_path,
782                         GENERIC_WRITE,
783                         FILE_SHARE_READ,
784                         NULL,
785                         OPEN_EXISTING,
786                         FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
787                         NULL);
788
789         if (h == INVALID_HANDLE_VALUE)
790                 err = GetLastError();
791         FREE(utf16_path);
792         if (h == INVALID_HANDLE_VALUE)
793                 goto fail;
794
795         FILETIME creationTime = {.dwLowDateTime = dentry->d_inode->i_creation_time & 0xffffffff,
796                                  .dwHighDateTime = dentry->d_inode->i_creation_time >> 32};
797         FILETIME lastAccessTime = {.dwLowDateTime = dentry->d_inode->i_last_access_time & 0xffffffff,
798                                   .dwHighDateTime = dentry->d_inode->i_last_access_time >> 32};
799         FILETIME lastWriteTime = {.dwLowDateTime = dentry->d_inode->i_last_write_time & 0xffffffff,
800                                   .dwHighDateTime = dentry->d_inode->i_last_write_time >> 32};
801
802         DEBUG("Calling SetFileTime() on \"%s\"", output_path);
803         if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
804                 err = GetLastError();
805                 CloseHandle(h);
806                 goto fail;
807         }
808         DEBUG("Closing \"%s\"", output_path);
809         if (!CloseHandle(h)) {
810                 err = GetLastError();
811                 goto fail;
812         }
813         return 0;
814 fail:
815         ERROR("Can't set timestamps on \"%s\"", output_path);
816         win32_error(err);
817         return WIMLIB_ERR_WRITE;
818 #else
819         /* UNIX */
820
821         /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
822          * into struct timeval's. */
823         struct timeval tv[2];
824         wim_timestamp_to_timeval(inode->i_last_access_time, &tv[0]);
825         wim_timestamp_to_timeval(inode->i_last_write_time, &tv[1]);
826         #ifdef HAVE_LUTIMES
827         ret = lutimes(output_path, tv);
828         #else
829         ret = -1;
830         errno = ENOSYS;
831         #endif
832         if (ret != 0) {
833                 #ifdef HAVE_UTIME
834                 if (errno == ENOSYS) {
835                         struct utimbuf buf;
836                         buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
837                         buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
838                         if (utime(output_path, &buf) == 0)
839                                 return 0;
840                 }
841                 #endif
842                 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
843                         /*WARNING_WITH_ERRNO("Failed to set timestamp on file `%s',*/
844                                             /*output_path");*/
845                         args->num_lutimes_warnings++;
846                 }
847         }
848         return 0;
849 #endif
850 }
851
852 /* Extract a dentry if it hasn't already been extracted, and either the dentry
853  * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
854 static int maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
855 {
856         struct apply_args *args = arg;
857         int ret;
858
859         if (dentry->is_extracted)
860                 return 0;
861
862         if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
863                 if (inode_unnamed_lte_resolved(dentry->d_inode))
864                         return 0;
865
866         if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
867              args->progress_func) {
868                 args->progress.extract.cur_path = dentry->full_path_utf8;
869                 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
870                                     &args->progress);
871         }
872         ret = args->apply_dentry(dentry, args);
873         if (ret == 0)
874                 dentry->is_extracted = 1;
875         return ret;
876 }
877
878 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
879 {
880         const struct wim_lookup_table_entry *lte1, *lte2;
881         lte1 = *(const struct wim_lookup_table_entry**)p1;
882         lte2 = *(const struct wim_lookup_table_entry**)p2;
883         if (lte1->resource_entry.offset < lte2->resource_entry.offset)
884                 return -1;
885         else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
886                 return 1;
887         else
888                 return 0;
889 }
890
891 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
892 {
893         struct list_head *cur;
894         size_t num_streams;
895         struct wim_lookup_table_entry **array;
896         size_t i;
897         size_t array_size;
898
899         num_streams = 0;
900         list_for_each(cur, stream_list)
901                 num_streams++;
902         array_size = num_streams * sizeof(array[0]);
903         array = MALLOC(array_size);
904         if (!array) {
905                 ERROR("Failed to allocate %zu bytes to sort stream entries",
906                       array_size);
907                 return WIMLIB_ERR_NOMEM;
908         }
909         cur = stream_list->next;
910         for (i = 0; i < num_streams; i++) {
911                 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
912                 cur = cur->next;
913         }
914
915         qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
916
917         INIT_LIST_HEAD(stream_list);
918         for (i = 0; i < num_streams; i++)
919                 list_add_tail(&array[i]->staging_list, stream_list);
920         FREE(array);
921         return 0;
922 }
923
924 static void calculate_bytes_to_extract(struct list_head *stream_list,
925                                        int extract_flags,
926                                        union wimlib_progress_info *progress)
927 {
928         struct wim_lookup_table_entry *lte;
929         u64 total_bytes = 0;
930         u64 num_streams = 0;
931
932         /* For each stream to be extracted... */
933         list_for_each_entry(lte, stream_list, staging_list) {
934                 if (extract_flags &
935                     (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
936                 {
937                         /* In the symlink or hard link extraction mode, each
938                          * stream will be extracted one time regardless of how
939                          * many dentries share the stream. */
940                         wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
941                         if (!lte->extracted_file) {
942                                 num_streams++;
943                                 total_bytes += wim_resource_size(lte);
944                         }
945                 } else {
946                         num_streams += lte->out_refcnt;
947                         total_bytes += lte->out_refcnt * wim_resource_size(lte);
948                 }
949         }
950         progress->extract.num_streams = num_streams;
951         progress->extract.total_bytes = total_bytes;
952         progress->extract.completed_bytes = 0;
953 }
954
955 static void maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
956                                             struct list_head *stream_list)
957 {
958         if (++lte->out_refcnt == 1) {
959                 INIT_LIST_HEAD(&lte->inode_list);
960                 list_add_tail(&lte->staging_list, stream_list);
961         }
962 }
963
964 static void inode_find_streams_for_extraction(struct wim_inode *inode,
965                                               struct list_head *stream_list,
966                                               int extract_flags)
967 {
968         struct wim_lookup_table_entry *lte;
969         bool inode_added = false;
970
971         lte = inode_unnamed_lte_resolved(inode);
972         if (lte) {
973                 maybe_add_stream_for_extraction(lte, stream_list);
974                 list_add_tail(&inode->i_lte_inode_list, &lte->inode_list);
975                 inode_added = true;
976         }
977 #ifdef WITH_NTFS_3G
978         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
979                 for (unsigned i = 0; i < inode->i_num_ads; i++) {
980                         if (inode->i_ads_entries[i].stream_name_len != 0) {
981                                 lte = inode->i_ads_entries[i].lte;
982                                 if (lte) {
983                                         maybe_add_stream_for_extraction(lte,
984                                                                         stream_list);
985                                         if (!inode_added) {
986                                                 list_add_tail(&inode->i_lte_inode_list,
987                                                               &lte->inode_list);
988                                                 inode_added = true;
989                                         }
990                                 }
991                         }
992                 }
993         }
994 #endif
995 }
996
997 static void find_streams_for_extraction(struct hlist_head *inode_list,
998                                         struct list_head *stream_list,
999                                         struct wim_lookup_table *lookup_table,
1000                                         int extract_flags)
1001 {
1002         struct wim_inode *inode;
1003         struct hlist_node *cur;
1004         struct wim_dentry *dentry;
1005
1006         for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
1007         INIT_LIST_HEAD(stream_list);
1008         hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
1009                 if (!inode->i_resolved)
1010                         inode_resolve_ltes(inode, lookup_table);
1011                 inode_for_each_dentry(dentry, inode)
1012                         dentry->is_extracted = 0;
1013                 inode_find_streams_for_extraction(inode, stream_list,
1014                                                   extract_flags);
1015         }
1016 }
1017
1018 struct apply_operations {
1019         int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
1020         int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
1021 };
1022
1023 static const struct apply_operations normal_apply_operations = {
1024         .apply_dentry = apply_dentry_normal,
1025         .apply_dentry_timestamps = apply_dentry_timestamps_normal,
1026 };
1027
1028 #ifdef WITH_NTFS_3G
1029 static const struct apply_operations ntfs_apply_operations = {
1030         .apply_dentry = apply_dentry_ntfs,
1031         .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
1032 };
1033 #endif
1034
1035 static int apply_stream_list(struct list_head *stream_list,
1036                              struct apply_args *args,
1037                              const struct apply_operations *ops,
1038                              wimlib_progress_func_t progress_func)
1039 {
1040         uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
1041         uint64_t next_progress = bytes_per_progress;
1042         struct wim_lookup_table_entry *lte;
1043         struct wim_inode *inode;
1044         struct wim_dentry *dentry;
1045         int ret;
1046
1047         /* This complicated loop is essentially looping through the dentries,
1048          * although dentries may be visited more than once (if a dentry contains
1049          * two different nonempty streams) or not at all (if a dentry contains
1050          * no non-empty streams).
1051          *
1052          * The outer loop is over the distinct streams to be extracted so that
1053          * sequential reading of the WIM can be implemented. */
1054
1055         /* For each distinct stream to be extracted */
1056         list_for_each_entry(lte, stream_list, staging_list) {
1057                 /* For each inode that contains the stream */
1058                 list_for_each_entry(inode, &lte->inode_list, i_lte_inode_list) {
1059                         /* For each dentry that points to the inode */
1060                         inode_for_each_dentry(dentry, inode) {
1061                                 /* Extract the dentry if it was not already
1062                                  * extracted */
1063                                 ret = maybe_apply_dentry(dentry, args);
1064                                 if (ret != 0)
1065                                         return ret;
1066                                 if (progress_func &&
1067                                     args->progress.extract.completed_bytes >= next_progress)
1068                                 {
1069                                         progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
1070                                                       &args->progress);
1071                                         if (args->progress.extract.completed_bytes >=
1072                                             args->progress.extract.total_bytes)
1073                                         {
1074                                                 next_progress = ~0ULL;
1075                                         } else {
1076                                                 next_progress =
1077                                                         min (args->progress.extract.completed_bytes +
1078                                                              bytes_per_progress,
1079                                                              args->progress.extract.total_bytes);
1080                                         }
1081                                 }
1082                         }
1083                 }
1084         }
1085         return 0;
1086 }
1087
1088 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
1089  * @target. */
1090 static int extract_single_image(WIMStruct *w, int image,
1091                                 const char *target, int extract_flags,
1092                                 wimlib_progress_func_t progress_func)
1093 {
1094         int ret;
1095         struct list_head stream_list;
1096         struct hlist_head *inode_list;
1097
1098         struct apply_args args;
1099         const struct apply_operations *ops;
1100
1101         args.w                    = w;
1102         args.target               = target;
1103         args.extract_flags        = extract_flags;
1104         args.num_lutimes_warnings = 0;
1105         args.stream_list          = &stream_list;
1106         args.progress_func        = progress_func;
1107
1108         if (progress_func) {
1109                 args.progress.extract.wimfile_name = w->filename;
1110                 args.progress.extract.image = image;
1111                 args.progress.extract.extract_flags = (extract_flags &
1112                                                        WIMLIB_EXTRACT_MASK_PUBLIC);
1113                 args.progress.extract.image_name = wimlib_get_image_name(w, image);
1114                 args.progress.extract.target = target;
1115         }
1116
1117 #ifdef WITH_NTFS_3G
1118         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1119                 args.vol = ntfs_mount(target, 0);
1120                 if (!args.vol) {
1121                         ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
1122                         return WIMLIB_ERR_NTFS_3G;
1123                 }
1124                 ops = &ntfs_apply_operations;
1125         } else
1126 #endif
1127                 ops = &normal_apply_operations;
1128
1129         ret = select_wim_image(w, image);
1130         if (ret != 0)
1131                 goto out;
1132
1133         inode_list = &w->image_metadata[image - 1].inode_list;
1134
1135         /* Build a list of the streams that need to be extracted */
1136         find_streams_for_extraction(inode_list, &stream_list,
1137                                     w->lookup_table, extract_flags);
1138
1139         /* Calculate the number of bytes of data that will be extracted */
1140         calculate_bytes_to_extract(&stream_list, extract_flags,
1141                                    &args.progress);
1142
1143         if (progress_func) {
1144                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
1145                               &args.progress);
1146         }
1147
1148         /* If a sequential extraction was specified, sort the streams to be
1149          * extracted by their position in the WIM file, so that the WIM file can
1150          * be read sequentially. */
1151         if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
1152                 ret = sort_stream_list_by_wim_position(&stream_list);
1153                 if (ret != 0) {
1154                         WARNING("Falling back to non-sequential extraction");
1155                         extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1156                 }
1157         }
1158
1159         if (progress_func) {
1160                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
1161                               &args.progress);
1162         }
1163
1164         /* Make the directory structure and extract empty files */
1165         args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1166         args.apply_dentry = ops->apply_dentry;
1167         ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
1168         args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1169         if (ret != 0)
1170                 goto out;
1171
1172         if (progress_func) {
1173                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
1174                               &args.progress);
1175         }
1176
1177         /* Extract non-empty files */
1178         ret = apply_stream_list(&stream_list, &args, ops, progress_func);
1179         if (ret != 0)
1180                 goto out;
1181
1182         if (progress_func) {
1183                 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
1184                               &args.progress);
1185         }
1186
1187         /* Apply timestamps */
1188         ret = for_dentry_in_tree_depth(wim_root_dentry(w),
1189                                        ops->apply_dentry_timestamps, &args);
1190         if (ret != 0)
1191                 goto out;
1192
1193         if (progress_func) {
1194                 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
1195                               &args.progress);
1196         }
1197 out:
1198 #ifdef WITH_NTFS_3G
1199         /* Unmount the NTFS volume */
1200         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1201                 if (ntfs_umount(args.vol, FALSE) != 0) {
1202                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
1203                         if (ret == 0)
1204                                 ret = WIMLIB_ERR_NTFS_3G;
1205                 }
1206         }
1207 #endif
1208         return ret;
1209 }
1210
1211
1212 /* Extracts all images from the WIM to the directory @target, with the images
1213  * placed in subdirectories named by their image names. */
1214 static int extract_all_images(WIMStruct *w, const char *target,
1215                               int extract_flags,
1216                               wimlib_progress_func_t progress_func)
1217 {
1218         size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
1219         size_t output_path_len = strlen(target);
1220         char buf[output_path_len + 1 + image_name_max_len + 1];
1221         int ret;
1222         int image;
1223         const char *image_name;
1224
1225         ret = extract_directory(NULL, target, true);
1226         if (ret != 0)
1227                 return ret;
1228
1229         memcpy(buf, target, output_path_len);
1230         buf[output_path_len] = '/';
1231         for (image = 1; image <= w->hdr.image_count; image++) {
1232                 image_name = wimlib_get_image_name(w, image);
1233                 if (image_name && *image_name) {
1234                         strcpy(buf + output_path_len + 1, image_name);
1235                 } else {
1236                         /* Image name is empty. Use image number instead */
1237                         sprintf(buf + output_path_len + 1, "%d", image);
1238                 }
1239                 ret = extract_single_image(w, image, buf, extract_flags,
1240                                            progress_func);
1241                 if (ret != 0)
1242                         return ret;
1243         }
1244         return 0;
1245 }
1246
1247 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1248  * volume. */
1249 WIMLIBAPI int wimlib_extract_image(WIMStruct *w,
1250                                    int image,
1251                                    const char *target,
1252                                    int extract_flags,
1253                                    WIMStruct **additional_swms,
1254                                    unsigned num_additional_swms,
1255                                    wimlib_progress_func_t progress_func)
1256 {
1257         struct wim_lookup_table *joined_tab, *w_tab_save;
1258         int ret;
1259
1260         if (!target)
1261                 return WIMLIB_ERR_INVALID_PARAM;
1262
1263         extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1264
1265         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1266                         == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1267                 return WIMLIB_ERR_INVALID_PARAM;
1268
1269 #if defined(__CYGWIN__) || defined(__WIN32__)
1270         if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1271                 ERROR("Extracting UNIX data is not supported on Windows");
1272                 return WIMLIB_ERR_INVALID_PARAM;
1273         }
1274         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1275                 ERROR("Linked extraction modes are not supported on Windows");
1276                 return WIMLIB_ERR_INVALID_PARAM;
1277         }
1278 #endif
1279
1280         if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1281 #ifdef WITH_NTFS_3G
1282                 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1283                         ERROR("Cannot specify symlink or hardlink flags when applying\n"
1284                               "        directly to a NTFS volume");
1285                         return WIMLIB_ERR_INVALID_PARAM;
1286                 }
1287                 if (image == WIMLIB_ALL_IMAGES) {
1288                         ERROR("Can only apply a single image when applying "
1289                               "directly to a NTFS volume");
1290                         return WIMLIB_ERR_INVALID_PARAM;
1291                 }
1292                 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1293                         ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
1294                         return WIMLIB_ERR_INVALID_PARAM;
1295                 }
1296 #else
1297                 ERROR("wimlib was compiled without support for NTFS-3g, so");
1298                 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1299                 return WIMLIB_ERR_UNSUPPORTED;
1300 #endif
1301         }
1302
1303         ret = verify_swm_set(w, additional_swms, num_additional_swms);
1304         if (ret != 0)
1305                 return ret;
1306
1307         if (num_additional_swms) {
1308                 ret = new_joined_lookup_table(w, additional_swms,
1309                                               num_additional_swms, &joined_tab);
1310                 if (ret != 0)
1311                         return ret;
1312                 w_tab_save = w->lookup_table;
1313                 w->lookup_table = joined_tab;
1314         }
1315
1316         if (image == WIMLIB_ALL_IMAGES) {
1317                 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1318                 ret = extract_all_images(w, target, extract_flags,
1319                                          progress_func);
1320         } else {
1321                 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1322                 ret = extract_single_image(w, image, target, extract_flags,
1323                                            progress_func);
1324         }
1325
1326         if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1327                              WIMLIB_EXTRACT_FLAG_HARDLINK))
1328         {
1329                 for_lookup_table_entry(w->lookup_table,
1330                                        lte_free_extracted_file,
1331                                        NULL);
1332         }
1333
1334         if (num_additional_swms) {
1335                 free_lookup_table(w->lookup_table);
1336                 w->lookup_table = w_tab_save;
1337         }
1338         return ret;
1339 }