4 * Support for extracting WIM files.
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
13 * Copyright (C) 2012, 2013 Eric Biggers
15 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
33 #if defined(__CYGWIN__) || defined(__WIN32__)
56 #include "lookup_table.h"
57 #include "timestamp.h"
58 #include "wimlib_internal.h"
62 #include <ntfs-3g/volume.h>
71 #if defined(__CYGWIN__) || defined(__WIN32__)
73 static int win32_set_reparse_data(HANDLE h,
75 const struct wim_lookup_table_entry *lte,
77 const char *path_utf8)
84 WARNING("\"%s\" is marked as a reparse point but had no reparse data",
88 len = wim_resource_size(lte);
89 if (len > 16 * 1024 - 8) {
90 WARNING("\"%s\": reparse data too long!", path_utf8);
93 buf = alloca(len + 8);
94 ret = read_full_wim_resource(lte, buf + 8, 0);
97 *(u32*)(buf + 0) = reparse_tag;
98 *(u16*)(buf + 4) = len;
99 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, buf, len + 8,
100 NULL, 0, NULL, NULL))
102 DWORD err = GetLastError();
103 ERROR("Failed to set reparse data on \"%s\"",
106 ret = WIMLIB_ERR_WRITE;
113 static int win32_extract_chunk(const u8 *buf, size_t len, u64 offset, void *arg)
115 HANDLE hStream = arg;
117 DWORD nbytes_written;
118 wimlib_assert(len <= 0xffffffff);
120 if (!WriteFile(hStream, buf, len, &nbytes_written, NULL) ||
121 nbytes_written != len)
123 DWORD err = GetLastError();
124 ERROR("WriteFile(): write error");
126 return WIMLIB_ERR_WRITE;
131 static int do_win32_extract_stream(HANDLE hStream, struct wim_lookup_table_entry *lte)
133 return extract_wim_resource(lte, wim_resource_size(lte),
134 win32_extract_chunk, hStream);
137 static int win32_extract_stream(const struct wim_inode *inode,
139 const wchar_t *stream_name_utf16,
140 struct wim_lookup_table_entry *lte,
141 const char *path_utf8)
143 wchar_t *stream_path;
147 DWORD creationDisposition = CREATE_ALWAYS;
149 if (stream_name_utf16) {
150 size_t stream_path_nchars;
151 size_t path_nchars = wcslen(path);
152 size_t stream_name_nchars = wcslen(stream_name_utf16);
154 stream_path_nchars = path_nchars + 1 + stream_name_nchars;
156 stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
158 memcpy(stream_path, path, path_nchars * sizeof(wchar_t));
159 stream_path[path_nchars] = L':';
160 memcpy(&stream_path[path_nchars + 1], stream_name_utf16,
161 stream_name_nchars * sizeof(wchar_t));
162 stream_path[stream_path_nchars] = L'\0';
164 /*wsprintf(stream_path, stream_path_nchars, L"%ls:%ls",*/
165 /*path, stream_name_utf16);*/
167 stream_path = (wchar_t*)path;
168 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
169 if (!CreateDirectoryW(stream_path, NULL)) {
170 err = GetLastError();
171 if (err == ERROR_FILE_EXISTS &&
172 dentry_is_root(inode_first_dentry(inode)))
177 ERROR("Failed to create directory \"%s\"", path_utf8);
179 ret = WIMLIB_ERR_MKDIR;
182 creationDisposition = OPEN_EXISTING;
186 h = CreateFileW(stream_path,
187 GENERIC_WRITE | WRITE_OWNER | WRITE_DAC,
191 FILE_FLAG_OPEN_REPARSE_POINT |
192 FILE_FLAG_BACKUP_SEMANTICS |
195 if (h == INVALID_HANDLE_VALUE) {
196 err = GetLastError();
197 if (err == ERROR_FILE_EXISTS &&
198 dentry_is_root(inode_first_dentry(inode)))
203 ERROR("Failed to create \"%s\"", path_utf8);
205 ret = WIMLIB_ERR_OPEN;
209 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
210 DEBUG("Setting reparse data on \"%s\"", path_utf8);
211 ret = win32_set_reparse_data(h, inode->i_reparse_tag,
212 lte, path, path_utf8);
214 goto fail_close_handle;
217 DEBUG("Extracting stream for \"%s\" (len = %zu)",
218 path_utf8, wim_resource_size(lte));
219 ret = do_win32_extract_stream(h, lte);
221 goto fail_close_handle;
225 if (!CloseHandle(h)) {
226 err = GetLastError();
227 ERROR("Failed to close \"%s\"", path_utf8);
229 ret = WIMLIB_ERR_WRITE;
237 ERROR("Error extracting %s", path_utf8);
242 static int win32_extract_streams(struct wim_inode *inode,
244 const char *path_utf8)
246 struct wim_lookup_table_entry *unnamed_lte;
249 ret = win32_extract_stream(inode, path, NULL, unnamed_lte, path_utf8);
253 for (u16 i = 0; i < inode->i_num_ads; i++) {
254 const struct wim_ads_entry *ads_entry = &inode->i_ads_entries[i];
255 if (ads_entry->stream_name_len != 0) {
256 ret = win32_extract_stream(inode,
258 (const wchar_t*)ads_entry->stream_name,
270 static int win32_set_security_data(const struct wim_inode *inode,
272 const struct wim_security_data *sd,
273 const char *path_utf8)
275 SECURITY_INFORMATION securityInformation = DACL_SECURITY_INFORMATION |
276 SACL_SECURITY_INFORMATION |
277 OWNER_SECURITY_INFORMATION |
278 GROUP_SECURITY_INFORMATION;
279 if (!SetFileSecurityW(path, securityInformation,
280 (PSECURITY_DESCRIPTOR)sd->descriptors[inode->i_security_id]))
282 DWORD err = GetLastError();
283 ERROR("Can't set security descriptor on \"%s\"", path_utf8);
285 return WIMLIB_ERR_WRITE;
291 static int extract_regular_file_linked(struct wim_dentry *dentry,
292 const char *output_path,
293 struct apply_args *args,
294 struct wim_lookup_table_entry *lte)
296 /* This mode overrides the normal hard-link extraction and
297 * instead either symlinks or hardlinks *all* identical files in
298 * the WIM, even if they are in a different image (in the case
299 * of a multi-image extraction) */
301 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
302 if (link(lte->extracted_file, output_path) != 0) {
303 ERROR_WITH_ERRNO("Failed to hard link "
305 output_path, lte->extracted_file);
306 return WIMLIB_ERR_LINK;
309 int num_path_components;
310 int num_output_dir_path_components;
311 size_t extracted_file_len;
316 num_path_components =
317 get_num_path_components(dentry->full_path_utf8) - 1;
318 num_output_dir_path_components =
319 get_num_path_components(args->target);
321 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
322 num_path_components++;
323 num_output_dir_path_components--;
325 extracted_file_len = strlen(lte->extracted_file);
327 char buf[extracted_file_len + 3 * num_path_components + 1];
330 for (i = 0; i < num_path_components; i++) {
335 p2 = lte->extracted_file;
338 while (num_output_dir_path_components--)
339 p2 = path_next_part(p2, NULL);
341 if (symlink(buf, output_path) != 0) {
342 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
344 buf, lte->extracted_file);
345 return WIMLIB_ERR_LINK;
351 static int symlink_apply_unix_data(const char *link,
352 const struct wimlib_unix_data *unix_data)
354 if (lchown(link, unix_data->uid, unix_data->gid)) {
355 if (errno == EPERM) {
357 WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
359 ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
360 return WIMLIB_ERR_INVALID_DENTRY;
366 static int fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
368 if (fchown(fd, unix_data->uid, unix_data->gid)) {
369 if (errno == EPERM) {
370 WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
373 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
374 return WIMLIB_ERR_INVALID_DENTRY;
378 if (fchmod(fd, unix_data->mode)) {
379 if (errno == EPERM) {
380 WARNING_WITH_ERRNO("failed to set UNIX file mode");
383 ERROR_WITH_ERRNO("failed to set UNIX file mode");
384 return WIMLIB_ERR_INVALID_DENTRY;
390 static int dir_apply_unix_data(const char *dir,
391 const struct wimlib_unix_data *unix_data)
393 int dfd = open(dir, O_RDONLY);
396 ret = fd_apply_unix_data(dfd, unix_data);
398 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
399 ret = WIMLIB_ERR_MKDIR;
402 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
403 ret = WIMLIB_ERR_MKDIR;
408 static int extract_regular_file_unlinked(struct wim_dentry *dentry,
409 struct apply_args *args,
410 const char *output_path,
411 struct wim_lookup_table_entry *lte)
413 /* Normal mode of extraction. Regular files and hard links are
414 * extracted in the way that they appear in the WIM. */
418 struct wim_inode *inode = dentry->d_inode;
420 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
421 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
422 WIMLIB_EXTRACT_FLAG_HARDLINK))))
424 /* If the dentry is part of a hard link set of at least 2
425 * dentries and one of the other dentries has already been
426 * extracted, make a hard link to the file corresponding to this
427 * already-extracted directory. Otherwise, extract the file and
428 * set the inode->i_extracted_file field so that other dentries
429 * in the hard link group can link to it. */
430 if (inode->i_nlink > 1) {
431 if (inode->i_extracted_file) {
432 DEBUG("Extracting hard link `%s' => `%s'",
433 output_path, inode->i_extracted_file);
434 if (link(inode->i_extracted_file, output_path) != 0) {
435 ERROR_WITH_ERRNO("Failed to hard link "
438 inode->i_extracted_file);
439 return WIMLIB_ERR_LINK;
443 FREE(inode->i_extracted_file);
444 inode->i_extracted_file = STRDUP(output_path);
445 if (!inode->i_extracted_file) {
446 ERROR("Failed to allocate memory for filename");
447 return WIMLIB_ERR_NOMEM;
452 /* Extract the contents of the file to @output_path. */
454 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
456 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
458 return WIMLIB_ERR_OPEN;
462 /* Empty file with no lookup table entry */
463 DEBUG("Empty file `%s'.", output_path);
465 goto out_extract_unix_data;
468 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
470 ERROR("Failed to extract resource to `%s'", output_path);
474 out_extract_unix_data:
475 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
476 struct wimlib_unix_data unix_data;
477 ret = inode_get_unix_data(inode, &unix_data, NULL);
483 ret = fd_apply_unix_data(out_fd, &unix_data);
488 args->progress.extract.completed_bytes += wim_resource_size(lte);
490 if (close(out_fd) != 0) {
491 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
493 ret = WIMLIB_ERR_WRITE;
498 static int extract_regular_file(struct wim_dentry *dentry,
499 struct apply_args *args,
500 const char *output_path)
502 struct wim_lookup_table_entry *lte;
503 const struct wim_inode *inode = dentry->d_inode;
505 lte = inode_unnamed_lte_resolved(inode);
507 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
508 WIMLIB_EXTRACT_FLAG_HARDLINK)))
510 if (lte->extracted_file) {
511 return extract_regular_file_linked(dentry, output_path, args, lte);
513 lte->extracted_file = STRDUP(output_path);
514 if (!lte->extracted_file)
515 return WIMLIB_ERR_NOMEM;
518 return extract_regular_file_unlinked(dentry, args, output_path, lte);
521 static int extract_symlink(struct wim_dentry *dentry,
522 struct apply_args *args,
523 const char *output_path)
526 ssize_t ret = inode_readlink(dentry->d_inode, target,
527 sizeof(target), args->w, 0);
528 struct wim_lookup_table_entry *lte;
531 ERROR("Could not read the symbolic link from dentry `%s'",
532 dentry->full_path_utf8);
533 return WIMLIB_ERR_INVALID_DENTRY;
535 ret = symlink(target, output_path);
537 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
538 output_path, target);
539 return WIMLIB_ERR_LINK;
541 lte = inode_unnamed_lte_resolved(dentry->d_inode);
542 wimlib_assert(lte != NULL);
543 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
544 struct wimlib_unix_data unix_data;
545 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
551 ret = symlink_apply_unix_data(output_path, &unix_data);
555 args->progress.extract.completed_bytes += wim_resource_size(lte);
559 #endif /* !__CYGWIN__ && !__WIN32__ */
561 static int extract_directory(struct wim_dentry *dentry,
562 const char *output_path, bool is_root)
567 ret = stat(output_path, &stbuf);
569 if (S_ISDIR(stbuf.st_mode)) {
571 /*WARNING("`%s' already exists", output_path);*/
574 ERROR("`%s' is not a directory", output_path);
575 return WIMLIB_ERR_MKDIR;
578 if (errno != ENOENT) {
579 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
580 return WIMLIB_ERR_STAT;
583 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
584 S_IROTH | S_IXOTH) != 0) {
585 ERROR_WITH_ERRNO("Cannot create directory `%s'",
587 return WIMLIB_ERR_MKDIR;
591 #if !defined(__CYGWIN__) && !defined(__WIN32__)
593 struct wimlib_unix_data unix_data;
594 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
600 ret = dir_apply_unix_data(output_path, &unix_data);
606 /* Extracts a file, directory, or symbolic link from the WIM archive. */
607 static int apply_dentry_normal(struct wim_dentry *dentry, void *arg)
609 struct apply_args *args = arg;
610 struct wim_inode *inode = dentry->d_inode;
614 len = strlen(args->target);
615 if (dentry_is_root(dentry)) {
616 output_path = (char*)args->target;
618 output_path = alloca(len + dentry->full_path_utf8_len + 1);
619 memcpy(output_path, args->target, len);
620 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
621 output_path[len + dentry->full_path_utf8_len] = '\0';
622 len += dentry->full_path_utf8_len;
625 #if defined(__CYGWIN__) || defined(__WIN32__)
627 size_t utf16_path_len;
630 ret = utf8_to_utf16(output_path, len, &utf16_path, &utf16_path_len);
634 if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
635 /* Linked file, with another name already extracted */
636 if (!CreateHardLinkW((const wchar_t*)inode->i_extracted_file,
637 (const wchar_t*)utf16_path,
640 err = GetLastError();
641 ERROR("Can't create hard link \"%s\"", output_path);
642 ret = WIMLIB_ERR_LINK;
644 goto out_free_utf16_path;
647 goto out_free_utf16_path;
649 ret = win32_extract_streams(inode, (const wchar_t*)utf16_path,
652 goto out_free_utf16_path;
654 if (inode->i_security_id != -1) {
655 DEBUG("Setting security descriptor %d on %s",
656 inode->i_security_id, output_path);
657 ret = win32_set_security_data(inode,
658 (const wchar_t*)utf16_path,
659 wim_const_security_data(args->w),
662 goto out_free_utf16_path;
665 /*if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {*/
666 /*ret = win32_set_reparse_data(inode, path, output_path);*/
668 /*goto out_free_utf16_path;*/
671 if (inode->i_nlink > 1) {
672 inode->i_extracted_file = utf16_path;
680 if (inode_is_symlink(inode))
681 return extract_symlink(dentry, args, output_path);
682 else if (inode_is_directory(inode))
683 return extract_directory((args->extract_flags &
684 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
687 return extract_regular_file(dentry, args, output_path);
691 /* Apply timestamps to an extracted file or directory */
692 static int apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
694 struct apply_args *args = arg;
695 size_t len = strlen(args->target);
696 char output_path[len + dentry->full_path_utf8_len + 1];
697 const struct wim_inode *inode = dentry->d_inode;
700 memcpy(output_path, args->target, len);
701 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
702 output_path[len + dentry->full_path_utf8_len] = '\0';
704 #if defined(__CYGWIN__) || defined(__WIN32__)
707 size_t utf16_path_len;
712 ret = utf8_to_utf16(output_path, len + dentry->full_path_utf8_len,
713 &utf16_path, &utf16_path_len);
716 h = CreateFile(utf16_path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
717 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
720 if (h == INVALID_HANDLE_VALUE)
721 err = GetLastError();
723 if (h == INVALID_HANDLE_VALUE)
726 FILETIME creationTime = {.dwLowDateTime = dentry->d_inode->i_creation_time & 0xffffffff,
727 .dwHighDateTime = dentry->d_inode->i_creation_time >> 32};
728 FILETIME lastAccessTime = {.dwLowDateTime = dentry->d_inode->i_last_access_time & 0xffffffff,
729 .dwHighDateTime = dentry->d_inode->i_last_access_time >> 32};
730 FILETIME lastWriteTime = {.dwLowDateTime = dentry->d_inode->i_last_write_time & 0xffffffff,
731 .dwHighDateTime = dentry->d_inode->i_last_write_time >> 32};
733 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
734 err = GetLastError();
738 if (!CloseHandle(h)) {
739 err = GetLastError();
744 err = GetLastError();
745 ERROR("Can't set timestamps on \"%s\"", output_path);
747 return WIMLIB_ERR_WRITE;
751 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
752 * into struct timeval's. */
753 struct timeval tv[2];
754 wim_timestamp_to_timeval(inode->i_last_access_time, &tv[0]);
755 wim_timestamp_to_timeval(inode->i_last_write_time, &tv[1]);
757 ret = lutimes(output_path, tv);
764 if (errno == ENOSYS) {
766 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
767 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
768 if (utime(output_path, &buf) == 0)
772 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
773 /*WARNING_WITH_ERRNO("Failed to set timestamp on file `%s',*/
775 args->num_lutimes_warnings++;
782 /* Extract a dentry if it hasn't already been extracted, and either the dentry
783 * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
784 static int maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
786 struct apply_args *args = arg;
789 if (dentry->is_extracted)
792 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
793 if (inode_unnamed_lte_resolved(dentry->d_inode))
796 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
797 args->progress_func) {
798 args->progress.extract.cur_path = dentry->full_path_utf8;
799 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
802 ret = args->apply_dentry(dentry, args);
804 dentry->is_extracted = 1;
808 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
810 const struct wim_lookup_table_entry *lte1, *lte2;
811 lte1 = *(const struct wim_lookup_table_entry**)p1;
812 lte2 = *(const struct wim_lookup_table_entry**)p2;
813 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
815 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
821 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
823 struct list_head *cur;
825 struct wim_lookup_table_entry **array;
830 list_for_each(cur, stream_list)
832 array_size = num_streams * sizeof(array[0]);
833 array = MALLOC(array_size);
835 ERROR("Failed to allocate %zu bytes to sort stream entries",
837 return WIMLIB_ERR_NOMEM;
839 cur = stream_list->next;
840 for (i = 0; i < num_streams; i++) {
841 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
845 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
847 INIT_LIST_HEAD(stream_list);
848 for (i = 0; i < num_streams; i++)
849 list_add_tail(&array[i]->staging_list, stream_list);
854 static void calculate_bytes_to_extract(struct list_head *stream_list,
856 union wimlib_progress_info *progress)
858 struct wim_lookup_table_entry *lte;
862 /* For each stream to be extracted... */
863 list_for_each_entry(lte, stream_list, staging_list) {
865 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
867 /* In the symlink or hard link extraction mode, each
868 * stream will be extracted one time regardless of how
869 * many dentries share the stream. */
870 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
871 if (!lte->extracted_file) {
873 total_bytes += wim_resource_size(lte);
876 num_streams += lte->out_refcnt;
877 total_bytes += lte->out_refcnt * wim_resource_size(lte);
880 progress->extract.num_streams = num_streams;
881 progress->extract.total_bytes = total_bytes;
882 progress->extract.completed_bytes = 0;
885 static void maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
886 struct list_head *stream_list)
888 if (++lte->out_refcnt == 1) {
889 INIT_LIST_HEAD(<e->inode_list);
890 list_add_tail(<e->staging_list, stream_list);
894 static void inode_find_streams_for_extraction(struct wim_inode *inode,
895 struct list_head *stream_list,
898 struct wim_lookup_table_entry *lte;
899 bool inode_added = false;
901 lte = inode_unnamed_lte_resolved(inode);
903 maybe_add_stream_for_extraction(lte, stream_list);
904 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
908 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
909 for (unsigned i = 0; i < inode->i_num_ads; i++) {
910 if (inode->i_ads_entries[i].stream_name_len != 0) {
911 lte = inode->i_ads_entries[i].lte;
913 maybe_add_stream_for_extraction(lte,
916 list_add_tail(&inode->i_lte_inode_list,
927 static void find_streams_for_extraction(struct hlist_head *inode_list,
928 struct list_head *stream_list,
929 struct wim_lookup_table *lookup_table,
932 struct wim_inode *inode;
933 struct hlist_node *cur;
934 struct wim_dentry *dentry;
936 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
937 INIT_LIST_HEAD(stream_list);
938 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
939 if (!inode->i_resolved)
940 inode_resolve_ltes(inode, lookup_table);
941 inode_for_each_dentry(dentry, inode)
942 dentry->is_extracted = 0;
943 inode_find_streams_for_extraction(inode, stream_list,
948 struct apply_operations {
949 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
950 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
953 static const struct apply_operations normal_apply_operations = {
954 .apply_dentry = apply_dentry_normal,
955 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
959 static const struct apply_operations ntfs_apply_operations = {
960 .apply_dentry = apply_dentry_ntfs,
961 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
965 static int apply_stream_list(struct list_head *stream_list,
966 struct apply_args *args,
967 const struct apply_operations *ops,
968 wimlib_progress_func_t progress_func)
970 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
971 uint64_t next_progress = bytes_per_progress;
972 struct wim_lookup_table_entry *lte;
973 struct wim_inode *inode;
974 struct wim_dentry *dentry;
977 /* This complicated loop is essentially looping through the dentries,
978 * although dentries may be visited more than once (if a dentry contains
979 * two different nonempty streams) or not at all (if a dentry contains
980 * no non-empty streams).
982 * The outer loop is over the distinct streams to be extracted so that
983 * sequential reading of the WIM can be implemented. */
985 /* For each distinct stream to be extracted */
986 list_for_each_entry(lte, stream_list, staging_list) {
987 /* For each inode that contains the stream */
988 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
989 /* For each dentry that points to the inode */
990 inode_for_each_dentry(dentry, inode) {
991 /* Extract the dentry if it was not already
993 ret = maybe_apply_dentry(dentry, args);
997 args->progress.extract.completed_bytes >= next_progress)
999 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
1001 if (args->progress.extract.completed_bytes >=
1002 args->progress.extract.total_bytes)
1004 next_progress = ~0ULL;
1007 min (args->progress.extract.completed_bytes +
1009 args->progress.extract.total_bytes);
1018 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
1020 static int extract_single_image(WIMStruct *w, int image,
1021 const char *target, int extract_flags,
1022 wimlib_progress_func_t progress_func)
1025 struct list_head stream_list;
1026 struct hlist_head *inode_list;
1028 struct apply_args args;
1029 const struct apply_operations *ops;
1032 args.target = target;
1033 args.extract_flags = extract_flags;
1034 args.num_lutimes_warnings = 0;
1035 args.stream_list = &stream_list;
1036 args.progress_func = progress_func;
1038 if (progress_func) {
1039 args.progress.extract.wimfile_name = w->filename;
1040 args.progress.extract.image = image;
1041 args.progress.extract.extract_flags = (extract_flags &
1042 WIMLIB_EXTRACT_MASK_PUBLIC);
1043 args.progress.extract.image_name = wimlib_get_image_name(w, image);
1044 args.progress.extract.target = target;
1048 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1049 args.vol = ntfs_mount(target, 0);
1051 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
1052 return WIMLIB_ERR_NTFS_3G;
1054 ops = &ntfs_apply_operations;
1057 ops = &normal_apply_operations;
1059 ret = select_wim_image(w, image);
1063 inode_list = &w->image_metadata[image - 1].inode_list;
1065 /* Build a list of the streams that need to be extracted */
1066 find_streams_for_extraction(inode_list, &stream_list,
1067 w->lookup_table, extract_flags);
1069 /* Calculate the number of bytes of data that will be extracted */
1070 calculate_bytes_to_extract(&stream_list, extract_flags,
1073 if (progress_func) {
1074 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
1078 /* If a sequential extraction was specified, sort the streams to be
1079 * extracted by their position in the WIM file, so that the WIM file can
1080 * be read sequentially. */
1081 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
1082 ret = sort_stream_list_by_wim_position(&stream_list);
1084 WARNING("Falling back to non-sequential extraction");
1085 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1089 if (progress_func) {
1090 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
1094 /* Make the directory structure and extract empty files */
1095 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1096 args.apply_dentry = ops->apply_dentry;
1097 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
1098 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1102 if (progress_func) {
1103 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
1107 /* Extract non-empty files */
1108 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
1112 if (progress_func) {
1113 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
1117 /* Apply timestamps */
1118 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
1119 ops->apply_dentry_timestamps, &args);
1123 if (progress_func) {
1124 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
1129 /* Unmount the NTFS volume */
1130 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1131 if (ntfs_umount(args.vol, FALSE) != 0) {
1132 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
1134 ret = WIMLIB_ERR_NTFS_3G;
1142 /* Extracts all images from the WIM to the directory @target, with the images
1143 * placed in subdirectories named by their image names. */
1144 static int extract_all_images(WIMStruct *w, const char *target,
1146 wimlib_progress_func_t progress_func)
1148 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
1149 size_t output_path_len = strlen(target);
1150 char buf[output_path_len + 1 + image_name_max_len + 1];
1153 const char *image_name;
1155 ret = extract_directory(NULL, target, true);
1159 memcpy(buf, target, output_path_len);
1160 buf[output_path_len] = '/';
1161 for (image = 1; image <= w->hdr.image_count; image++) {
1162 image_name = wimlib_get_image_name(w, image);
1163 if (image_name && *image_name) {
1164 strcpy(buf + output_path_len + 1, image_name);
1166 /* Image name is empty. Use image number instead */
1167 sprintf(buf + output_path_len + 1, "%d", image);
1169 ret = extract_single_image(w, image, buf, extract_flags,
1177 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1179 WIMLIBAPI int wimlib_extract_image(WIMStruct *w,
1183 WIMStruct **additional_swms,
1184 unsigned num_additional_swms,
1185 wimlib_progress_func_t progress_func)
1187 struct wim_lookup_table *joined_tab, *w_tab_save;
1191 return WIMLIB_ERR_INVALID_PARAM;
1193 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1195 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1196 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1197 return WIMLIB_ERR_INVALID_PARAM;
1199 #if defined(__CYGWIN__) || defined(__WIN32__)
1200 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1201 ERROR("Extracting UNIX data is not supported on Windows");
1202 return WIMLIB_ERR_INVALID_PARAM;
1204 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1205 ERROR("Linked extraction modes are not supported on Windows");
1206 return WIMLIB_ERR_INVALID_PARAM;
1210 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1212 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1213 ERROR("Cannot specify symlink or hardlink flags when applying\n"
1214 " directly to a NTFS volume");
1215 return WIMLIB_ERR_INVALID_PARAM;
1217 if (image == WIMLIB_ALL_IMAGES) {
1218 ERROR("Can only apply a single image when applying "
1219 "directly to a NTFS volume");
1220 return WIMLIB_ERR_INVALID_PARAM;
1222 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1223 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
1224 return WIMLIB_ERR_INVALID_PARAM;
1227 ERROR("wimlib was compiled without support for NTFS-3g, so");
1228 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1229 return WIMLIB_ERR_UNSUPPORTED;
1233 ret = verify_swm_set(w, additional_swms, num_additional_swms);
1237 if (num_additional_swms) {
1238 ret = new_joined_lookup_table(w, additional_swms,
1239 num_additional_swms, &joined_tab);
1242 w_tab_save = w->lookup_table;
1243 w->lookup_table = joined_tab;
1246 if (image == WIMLIB_ALL_IMAGES) {
1247 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1248 ret = extract_all_images(w, target, extract_flags,
1251 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1252 ret = extract_single_image(w, image, target, extract_flags,
1256 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1257 WIMLIB_EXTRACT_FLAG_HARDLINK))
1259 for_lookup_table_entry(w->lookup_table,
1260 lte_free_extracted_file,
1264 if (num_additional_swms) {
1265 free_lookup_table(w->lookup_table);
1266 w->lookup_table = w_tab_save;