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 if (stream_name_utf16) {
148 size_t stream_path_nchars;
149 size_t path_nchars = wcslen(path);
150 size_t stream_name_nchars = wcslen(stream_name_utf16);
152 stream_path_nchars = path_nchars + 1 + stream_name_nchars;
154 stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
156 memcpy(stream_path, path, path_nchars * sizeof(wchar_t));
157 stream_path[path_nchars] = L':';
158 memcpy(&stream_path[path_nchars + 1], stream_name_utf16,
159 stream_name_nchars * sizeof(wchar_t));
160 stream_path[stream_path_nchars] = L'\0';
162 /*wsprintf(stream_path, stream_path_nchars, L"%ls:%ls",*/
163 /*path, stream_name_utf16);*/
165 stream_path = (wchar_t*)path;
168 h = CreateFileW(stream_path,
169 GENERIC_WRITE | WRITE_OWNER | WRITE_DAC,
173 FILE_FLAG_OPEN_REPARSE_POINT |
174 FILE_FLAG_BACKUP_SEMANTICS |
178 win32_error(GetLastError());
179 ret = WIMLIB_ERR_OPEN;
183 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
184 ret = win32_set_reparse_data(h, inode->i_reparse_tag,
185 lte, path, path_utf8);
187 goto fail_close_handle;
190 ret = do_win32_extract_stream(h, lte);
192 goto fail_close_handle;
196 if (!CloseHandle(h)) {
197 win32_error(GetLastError());
198 ret = WIMLIB_ERR_WRITE;
206 ERROR("Error extracting %s", path_utf8);
211 static int win32_extract_streams(struct wim_inode *inode,
213 const char *path_utf8)
215 struct wim_lookup_table_entry *unnamed_lte;
218 ret = win32_extract_stream(inode, path, NULL, unnamed_lte, path_utf8);
222 for (u16 i = 0; i < inode->i_num_ads; i++) {
223 const struct wim_ads_entry *ads_entry = &inode->i_ads_entries[i];
224 if (ads_entry->stream_name_len != 0) {
225 ret = win32_extract_stream(inode,
227 (const wchar_t*)ads_entry->stream_name,
239 static int win32_set_security_data(const struct wim_inode *inode,
241 const struct wim_security_data *sd,
242 const char *path_utf8)
244 SECURITY_INFORMATION securityInformation = DACL_SECURITY_INFORMATION |
245 SACL_SECURITY_INFORMATION |
246 OWNER_SECURITY_INFORMATION |
247 GROUP_SECURITY_INFORMATION;
248 if (!SetFileSecurityW(path, securityInformation,
249 (PSECURITY_DESCRIPTOR)sd->descriptors[inode->i_security_id]))
251 DWORD err = GetLastError();
252 ERROR("Can't set security descriptor on \"%s\"", path_utf8);
254 return WIMLIB_ERR_WRITE;
260 static int extract_regular_file_linked(struct wim_dentry *dentry,
261 const char *output_path,
262 struct apply_args *args,
263 struct wim_lookup_table_entry *lte)
265 /* This mode overrides the normal hard-link extraction and
266 * instead either symlinks or hardlinks *all* identical files in
267 * the WIM, even if they are in a different image (in the case
268 * of a multi-image extraction) */
270 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
271 if (link(lte->extracted_file, output_path) != 0) {
272 ERROR_WITH_ERRNO("Failed to hard link "
274 output_path, lte->extracted_file);
275 return WIMLIB_ERR_LINK;
278 int num_path_components;
279 int num_output_dir_path_components;
280 size_t extracted_file_len;
285 num_path_components =
286 get_num_path_components(dentry->full_path_utf8) - 1;
287 num_output_dir_path_components =
288 get_num_path_components(args->target);
290 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
291 num_path_components++;
292 num_output_dir_path_components--;
294 extracted_file_len = strlen(lte->extracted_file);
296 char buf[extracted_file_len + 3 * num_path_components + 1];
299 for (i = 0; i < num_path_components; i++) {
304 p2 = lte->extracted_file;
307 while (num_output_dir_path_components--)
308 p2 = path_next_part(p2, NULL);
310 if (symlink(buf, output_path) != 0) {
311 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
313 buf, lte->extracted_file);
314 return WIMLIB_ERR_LINK;
320 static int symlink_apply_unix_data(const char *link,
321 const struct wimlib_unix_data *unix_data)
323 if (lchown(link, unix_data->uid, unix_data->gid)) {
324 if (errno == EPERM) {
326 WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
328 ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
329 return WIMLIB_ERR_INVALID_DENTRY;
335 static int fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
337 if (fchown(fd, unix_data->uid, unix_data->gid)) {
338 if (errno == EPERM) {
339 WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
342 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
343 return WIMLIB_ERR_INVALID_DENTRY;
347 if (fchmod(fd, unix_data->mode)) {
348 if (errno == EPERM) {
349 WARNING_WITH_ERRNO("failed to set UNIX file mode");
352 ERROR_WITH_ERRNO("failed to set UNIX file mode");
353 return WIMLIB_ERR_INVALID_DENTRY;
359 static int dir_apply_unix_data(const char *dir,
360 const struct wimlib_unix_data *unix_data)
362 int dfd = open(dir, O_RDONLY);
365 ret = fd_apply_unix_data(dfd, unix_data);
367 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
368 ret = WIMLIB_ERR_MKDIR;
371 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
372 ret = WIMLIB_ERR_MKDIR;
377 static int extract_regular_file_unlinked(struct wim_dentry *dentry,
378 struct apply_args *args,
379 const char *output_path,
380 struct wim_lookup_table_entry *lte)
382 /* Normal mode of extraction. Regular files and hard links are
383 * extracted in the way that they appear in the WIM. */
387 struct wim_inode *inode = dentry->d_inode;
389 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
390 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
391 WIMLIB_EXTRACT_FLAG_HARDLINK))))
393 /* If the dentry is part of a hard link set of at least 2
394 * dentries and one of the other dentries has already been
395 * extracted, make a hard link to the file corresponding to this
396 * already-extracted directory. Otherwise, extract the file and
397 * set the inode->i_extracted_file field so that other dentries
398 * in the hard link group can link to it. */
399 if (inode->i_nlink > 1) {
400 if (inode->i_extracted_file) {
401 DEBUG("Extracting hard link `%s' => `%s'",
402 output_path, inode->i_extracted_file);
403 if (link(inode->i_extracted_file, output_path) != 0) {
404 ERROR_WITH_ERRNO("Failed to hard link "
407 inode->i_extracted_file);
408 return WIMLIB_ERR_LINK;
412 FREE(inode->i_extracted_file);
413 inode->i_extracted_file = STRDUP(output_path);
414 if (!inode->i_extracted_file) {
415 ERROR("Failed to allocate memory for filename");
416 return WIMLIB_ERR_NOMEM;
421 /* Extract the contents of the file to @output_path. */
423 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
425 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
427 return WIMLIB_ERR_OPEN;
431 /* Empty file with no lookup table entry */
432 DEBUG("Empty file `%s'.", output_path);
434 goto out_extract_unix_data;
437 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
439 ERROR("Failed to extract resource to `%s'", output_path);
443 out_extract_unix_data:
444 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
445 struct wimlib_unix_data unix_data;
446 ret = inode_get_unix_data(inode, &unix_data, NULL);
452 ret = fd_apply_unix_data(out_fd, &unix_data);
457 args->progress.extract.completed_bytes += wim_resource_size(lte);
459 if (close(out_fd) != 0) {
460 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
462 ret = WIMLIB_ERR_WRITE;
467 static int extract_regular_file(struct wim_dentry *dentry,
468 struct apply_args *args,
469 const char *output_path)
471 struct wim_lookup_table_entry *lte;
472 const struct wim_inode *inode = dentry->d_inode;
474 lte = inode_unnamed_lte_resolved(inode);
476 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
477 WIMLIB_EXTRACT_FLAG_HARDLINK)))
479 if (lte->extracted_file) {
480 return extract_regular_file_linked(dentry, output_path, args, lte);
482 lte->extracted_file = STRDUP(output_path);
483 if (!lte->extracted_file)
484 return WIMLIB_ERR_NOMEM;
487 return extract_regular_file_unlinked(dentry, args, output_path, lte);
490 static int extract_symlink(struct wim_dentry *dentry,
491 struct apply_args *args,
492 const char *output_path)
495 ssize_t ret = inode_readlink(dentry->d_inode, target,
496 sizeof(target), args->w, 0);
497 struct wim_lookup_table_entry *lte;
500 ERROR("Could not read the symbolic link from dentry `%s'",
501 dentry->full_path_utf8);
502 return WIMLIB_ERR_INVALID_DENTRY;
504 ret = symlink(target, output_path);
506 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
507 output_path, target);
508 return WIMLIB_ERR_LINK;
510 lte = inode_unnamed_lte_resolved(dentry->d_inode);
511 wimlib_assert(lte != NULL);
512 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
513 struct wimlib_unix_data unix_data;
514 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
520 ret = symlink_apply_unix_data(output_path, &unix_data);
524 args->progress.extract.completed_bytes += wim_resource_size(lte);
528 #endif /* !__CYGWIN__ && !__WIN32__ */
530 static int extract_directory(struct wim_dentry *dentry,
531 const char *output_path, bool is_root)
536 ret = stat(output_path, &stbuf);
538 if (S_ISDIR(stbuf.st_mode)) {
540 /*WARNING("`%s' already exists", output_path);*/
543 ERROR("`%s' is not a directory", output_path);
544 return WIMLIB_ERR_MKDIR;
547 if (errno != ENOENT) {
548 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
549 return WIMLIB_ERR_STAT;
552 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
553 S_IROTH | S_IXOTH) != 0) {
554 ERROR_WITH_ERRNO("Cannot create directory `%s'",
556 return WIMLIB_ERR_MKDIR;
560 struct wimlib_unix_data unix_data;
561 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
567 ret = dir_apply_unix_data(output_path, &unix_data);
574 /* Extracts a file, directory, or symbolic link from the WIM archive. */
575 static int apply_dentry_normal(struct wim_dentry *dentry, void *arg)
577 struct apply_args *args = arg;
578 struct wim_inode *inode = dentry->d_inode;
581 len = strlen(args->target);
582 char output_path[len + dentry->full_path_utf8_len + 1];
583 memcpy(output_path, args->target, len);
584 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
585 output_path[len + dentry->full_path_utf8_len] = '\0';
587 #if defined(__CYGWIN__) || defined(__WIN32__)
589 size_t utf16_path_len;
592 ret = utf8_to_utf16(output_path, len + dentry->full_path_utf8_len,
593 &utf16_path, &utf16_path_len);
597 if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
598 /* Linked file, with another name already extracted */
599 if (!CreateHardLinkW((const wchar_t*)inode->i_extracted_file,
600 (const wchar_t*)utf16_path,
603 err = GetLastError();
604 ERROR("Can't create hard link \"%s\"", output_path);
605 ret = WIMLIB_ERR_LINK;
607 goto out_free_utf16_path;
610 goto out_free_utf16_path;
612 ret = win32_extract_streams(inode, (const wchar_t*)utf16_path,
615 goto out_free_utf16_path;
617 if (inode->i_security_id != -1) {
618 ret = win32_set_security_data(inode,
619 (const wchar_t*)utf16_path,
620 wim_const_security_data(args->w),
623 goto out_free_utf16_path;
626 /*if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {*/
627 /*ret = win32_set_reparse_data(inode, path, output_path);*/
629 /*goto out_free_utf16_path;*/
632 if (inode->i_nlink > 1) {
633 inode->i_extracted_file = utf16_path;
641 if (inode_is_symlink(inode))
642 return extract_symlink(dentry, args, output_path);
643 else if (inode_is_directory(inode))
644 return extract_directory((args->extract_flags &
645 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
648 return extract_regular_file(dentry, args, output_path);
652 /* Apply timestamps to an extracted file or directory */
653 static int apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
655 struct apply_args *args = arg;
656 size_t len = strlen(args->target);
657 char output_path[len + dentry->full_path_utf8_len + 1];
658 const struct wim_inode *inode = dentry->d_inode;
661 memcpy(output_path, args->target, len);
662 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
663 output_path[len + dentry->full_path_utf8_len] = '\0';
665 #if defined(__CYGWIN__) || defined(__WIN32__)
668 size_t utf16_path_len;
673 ret = utf8_to_utf16(output_path, len + dentry->full_path_utf8_len,
674 &utf16_path, &utf16_path_len);
677 h = CreateFile(utf16_path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
678 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
682 err = GetLastError();
687 FILETIME creationTime = {.dwLowDateTime = dentry->d_inode->i_creation_time & 0xffffffff,
688 .dwHighDateTime = dentry->d_inode->i_creation_time >> 32};
689 FILETIME lastAccessTime = {.dwLowDateTime = dentry->d_inode->i_last_access_time & 0xffffffff,
690 .dwHighDateTime = dentry->d_inode->i_last_access_time >> 32};
691 FILETIME lastWriteTime = {.dwLowDateTime = dentry->d_inode->i_last_write_time & 0xffffffff,
692 .dwHighDateTime = dentry->d_inode->i_last_write_time >> 32};
694 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
695 err = GetLastError();
699 if (!CloseHandle(h)) {
700 err = GetLastError();
705 err = GetLastError();
706 ERROR("Can't set timestamps on \"%s\"", output_path);
708 return WIMLIB_ERR_WRITE;
712 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
713 * into struct timeval's. */
714 struct timeval tv[2];
715 wim_timestamp_to_timeval(inode->i_last_access_time, &tv[0]);
716 wim_timestamp_to_timeval(inode->i_last_write_time, &tv[1]);
718 ret = lutimes(output_path, tv);
725 if (errno == ENOSYS) {
727 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
728 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
729 if (utime(output_path, &buf) == 0)
733 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
734 /*WARNING_WITH_ERRNO("Failed to set timestamp on file `%s',*/
736 args->num_lutimes_warnings++;
743 /* Extract a dentry if it hasn't already been extracted, and either the dentry
744 * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
745 static int maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
747 struct apply_args *args = arg;
750 if (dentry->is_extracted)
753 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
754 if (inode_unnamed_lte_resolved(dentry->d_inode))
757 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
758 args->progress_func) {
759 args->progress.extract.cur_path = dentry->full_path_utf8;
760 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
763 ret = args->apply_dentry(dentry, args);
765 dentry->is_extracted = 1;
769 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
771 const struct wim_lookup_table_entry *lte1, *lte2;
772 lte1 = *(const struct wim_lookup_table_entry**)p1;
773 lte2 = *(const struct wim_lookup_table_entry**)p2;
774 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
776 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
782 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
784 struct list_head *cur;
786 struct wim_lookup_table_entry **array;
791 list_for_each(cur, stream_list)
793 array_size = num_streams * sizeof(array[0]);
794 array = MALLOC(array_size);
796 ERROR("Failed to allocate %zu bytes to sort stream entries",
798 return WIMLIB_ERR_NOMEM;
800 cur = stream_list->next;
801 for (i = 0; i < num_streams; i++) {
802 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
806 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
808 INIT_LIST_HEAD(stream_list);
809 for (i = 0; i < num_streams; i++)
810 list_add_tail(&array[i]->staging_list, stream_list);
815 static void calculate_bytes_to_extract(struct list_head *stream_list,
817 union wimlib_progress_info *progress)
819 struct wim_lookup_table_entry *lte;
823 /* For each stream to be extracted... */
824 list_for_each_entry(lte, stream_list, staging_list) {
826 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
828 /* In the symlink or hard link extraction mode, each
829 * stream will be extracted one time regardless of how
830 * many dentries share the stream. */
831 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
832 if (!lte->extracted_file) {
834 total_bytes += wim_resource_size(lte);
837 num_streams += lte->out_refcnt;
838 total_bytes += lte->out_refcnt * wim_resource_size(lte);
841 progress->extract.num_streams = num_streams;
842 progress->extract.total_bytes = total_bytes;
843 progress->extract.completed_bytes = 0;
846 static void maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
847 struct list_head *stream_list)
849 if (++lte->out_refcnt == 1) {
850 INIT_LIST_HEAD(<e->inode_list);
851 list_add_tail(<e->staging_list, stream_list);
855 static void inode_find_streams_for_extraction(struct wim_inode *inode,
856 struct list_head *stream_list,
859 struct wim_lookup_table_entry *lte;
860 bool inode_added = false;
862 lte = inode_unnamed_lte_resolved(inode);
864 maybe_add_stream_for_extraction(lte, stream_list);
865 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
869 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
870 for (unsigned i = 0; i < inode->i_num_ads; i++) {
871 if (inode->i_ads_entries[i].stream_name_len != 0) {
872 lte = inode->i_ads_entries[i].lte;
874 maybe_add_stream_for_extraction(lte,
877 list_add_tail(&inode->i_lte_inode_list,
888 static void find_streams_for_extraction(struct hlist_head *inode_list,
889 struct list_head *stream_list,
890 struct wim_lookup_table *lookup_table,
893 struct wim_inode *inode;
894 struct hlist_node *cur;
895 struct wim_dentry *dentry;
897 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
898 INIT_LIST_HEAD(stream_list);
899 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
900 if (!inode->i_resolved)
901 inode_resolve_ltes(inode, lookup_table);
902 inode_for_each_dentry(dentry, inode)
903 dentry->is_extracted = 0;
904 inode_find_streams_for_extraction(inode, stream_list,
909 struct apply_operations {
910 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
911 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
914 static const struct apply_operations normal_apply_operations = {
915 .apply_dentry = apply_dentry_normal,
916 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
920 static const struct apply_operations ntfs_apply_operations = {
921 .apply_dentry = apply_dentry_ntfs,
922 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
926 static int apply_stream_list(struct list_head *stream_list,
927 struct apply_args *args,
928 const struct apply_operations *ops,
929 wimlib_progress_func_t progress_func)
931 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
932 uint64_t next_progress = bytes_per_progress;
933 struct wim_lookup_table_entry *lte;
934 struct wim_inode *inode;
935 struct wim_dentry *dentry;
938 /* This complicated loop is essentially looping through the dentries,
939 * although dentries may be visited more than once (if a dentry contains
940 * two different nonempty streams) or not at all (if a dentry contains
941 * no non-empty streams).
943 * The outer loop is over the distinct streams to be extracted so that
944 * sequential reading of the WIM can be implemented. */
946 /* For each distinct stream to be extracted */
947 list_for_each_entry(lte, stream_list, staging_list) {
948 /* For each inode that contains the stream */
949 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
950 /* For each dentry that points to the inode */
951 inode_for_each_dentry(dentry, inode) {
952 /* Extract the dentry if it was not already
954 ret = maybe_apply_dentry(dentry, args);
958 args->progress.extract.completed_bytes >= next_progress)
960 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
962 if (args->progress.extract.completed_bytes >=
963 args->progress.extract.total_bytes)
965 next_progress = ~0ULL;
968 min (args->progress.extract.completed_bytes +
970 args->progress.extract.total_bytes);
979 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
981 static int extract_single_image(WIMStruct *w, int image,
982 const char *target, int extract_flags,
983 wimlib_progress_func_t progress_func)
986 struct list_head stream_list;
987 struct hlist_head *inode_list;
989 struct apply_args args;
990 const struct apply_operations *ops;
993 args.target = target;
994 args.extract_flags = extract_flags;
995 args.num_lutimes_warnings = 0;
996 args.stream_list = &stream_list;
997 args.progress_func = progress_func;
1000 args.progress.extract.wimfile_name = w->filename;
1001 args.progress.extract.image = image;
1002 args.progress.extract.extract_flags = (extract_flags &
1003 WIMLIB_EXTRACT_MASK_PUBLIC);
1004 args.progress.extract.image_name = wimlib_get_image_name(w, image);
1005 args.progress.extract.target = target;
1009 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1010 args.vol = ntfs_mount(target, 0);
1012 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
1013 return WIMLIB_ERR_NTFS_3G;
1015 ops = &ntfs_apply_operations;
1018 ops = &normal_apply_operations;
1020 ret = select_wim_image(w, image);
1024 inode_list = &w->image_metadata[image - 1].inode_list;
1026 /* Build a list of the streams that need to be extracted */
1027 find_streams_for_extraction(inode_list, &stream_list,
1028 w->lookup_table, extract_flags);
1030 /* Calculate the number of bytes of data that will be extracted */
1031 calculate_bytes_to_extract(&stream_list, extract_flags,
1034 if (progress_func) {
1035 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
1039 /* If a sequential extraction was specified, sort the streams to be
1040 * extracted by their position in the WIM file, so that the WIM file can
1041 * be read sequentially. */
1042 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
1043 ret = sort_stream_list_by_wim_position(&stream_list);
1045 WARNING("Falling back to non-sequential extraction");
1046 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1050 if (progress_func) {
1051 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
1055 /* Make the directory structure and extract empty files */
1056 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1057 args.apply_dentry = ops->apply_dentry;
1058 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
1059 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1063 if (progress_func) {
1064 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
1068 /* Extract non-empty files */
1069 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
1073 if (progress_func) {
1074 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
1078 /* Apply timestamps */
1079 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
1080 ops->apply_dentry_timestamps, &args);
1084 if (progress_func) {
1085 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
1090 /* Unmount the NTFS volume */
1091 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1092 if (ntfs_umount(args.vol, FALSE) != 0) {
1093 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
1095 ret = WIMLIB_ERR_NTFS_3G;
1103 /* Extracts all images from the WIM to the directory @target, with the images
1104 * placed in subdirectories named by their image names. */
1105 static int extract_all_images(WIMStruct *w, const char *target,
1107 wimlib_progress_func_t progress_func)
1109 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
1110 size_t output_path_len = strlen(target);
1111 char buf[output_path_len + 1 + image_name_max_len + 1];
1114 const char *image_name;
1116 ret = extract_directory(NULL, target, true);
1120 memcpy(buf, target, output_path_len);
1121 buf[output_path_len] = '/';
1122 for (image = 1; image <= w->hdr.image_count; image++) {
1123 image_name = wimlib_get_image_name(w, image);
1124 if (image_name && *image_name) {
1125 strcpy(buf + output_path_len + 1, image_name);
1127 /* Image name is empty. Use image number instead */
1128 sprintf(buf + output_path_len + 1, "%d", image);
1130 ret = extract_single_image(w, image, buf, extract_flags,
1138 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1140 WIMLIBAPI int wimlib_extract_image(WIMStruct *w,
1144 WIMStruct **additional_swms,
1145 unsigned num_additional_swms,
1146 wimlib_progress_func_t progress_func)
1148 struct wim_lookup_table *joined_tab, *w_tab_save;
1152 return WIMLIB_ERR_INVALID_PARAM;
1154 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1156 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1157 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1158 return WIMLIB_ERR_INVALID_PARAM;
1160 #if defined(__CYGWIN__) || defined(__WIN32__)
1161 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1162 ERROR("Extracting UNIX data is not supported on Windows");
1163 return WIMLIB_ERR_INVALID_PARAM;
1167 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1169 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1170 ERROR("Cannot specify symlink or hardlink flags when applying\n"
1171 " directly to a NTFS volume");
1172 return WIMLIB_ERR_INVALID_PARAM;
1174 if (image == WIMLIB_ALL_IMAGES) {
1175 ERROR("Can only apply a single image when applying "
1176 "directly to a NTFS volume");
1177 return WIMLIB_ERR_INVALID_PARAM;
1179 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1180 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
1181 return WIMLIB_ERR_INVALID_PARAM;
1184 ERROR("wimlib was compiled without support for NTFS-3g, so");
1185 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1186 return WIMLIB_ERR_UNSUPPORTED;
1190 ret = verify_swm_set(w, additional_swms, num_additional_swms);
1194 if (num_additional_swms) {
1195 ret = new_joined_lookup_table(w, additional_swms,
1196 num_additional_swms, &joined_tab);
1199 w_tab_save = w->lookup_table;
1200 w->lookup_table = joined_tab;
1203 if (image == WIMLIB_ALL_IMAGES) {
1204 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1205 ret = extract_all_images(w, target, extract_flags,
1208 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1209 ret = extract_single_image(w, image, target, extract_flags,
1213 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1214 WIMLIB_EXTRACT_FLAG_HARDLINK))
1216 for_lookup_table_entry(w->lookup_table,
1217 lte_free_extracted_file,
1221 if (num_additional_swms) {
1222 free_lookup_table(w->lookup_table);
1223 w->lookup_table = w_tab_save;