4 * Support for extracting WIM files.
8 * Copyright (C) 2012, 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
28 #if defined(__CYGWIN__) || defined(__WIN32__)
39 # include "timestamp.h"
40 # include <sys/time.h>
52 #include "lookup_table.h"
53 #include "wimlib_internal.h"
57 #include <ntfs-3g/volume.h>
64 #if defined(__CYGWIN__) || defined(__WIN32__)
66 static int win32_set_reparse_data(HANDLE h,
68 const struct wim_lookup_table_entry *lte,
76 WARNING("\"%ls\" is marked as a reparse point but had no reparse data",
80 len = wim_resource_size(lte);
81 if (len > 16 * 1024 - 8) {
82 WARNING("\"%ls\": reparse data too long!", path);
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);
93 *(u32*)(buf + 0) = reparse_tag;
94 *(u16*)(buf + 4) = len;
97 /* Set the reparse data on the open file using the
98 * FSCTL_SET_REPARSE_POINT ioctl.
100 * There are contradictions in Microsoft's documentation for this:
102 * "If hDevice was opened without specifying FILE_FLAG_OVERLAPPED,
103 * lpOverlapped is ignored."
105 * --- So setting lpOverlapped to NULL is okay since it's ignored.
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."
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:
116 * "Not used with this operation; set to NULL."
119 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, buf, len + 8,
121 &bytesReturned /* lpBytesReturned */,
122 NULL /* lpOverlapped */))
124 DWORD err = GetLastError();
125 ERROR("Failed to set reparse data on \"%ls\"", path);
127 return WIMLIB_ERR_WRITE;
133 static int win32_extract_chunk(const u8 *buf, size_t len, u64 offset, void *arg)
135 HANDLE hStream = arg;
137 DWORD nbytes_written;
138 wimlib_assert(len <= 0xffffffff);
140 if (!WriteFile(hStream, buf, len, &nbytes_written, NULL) ||
141 nbytes_written != len)
143 DWORD err = GetLastError();
144 ERROR("WriteFile(): write error");
146 return WIMLIB_ERR_WRITE;
151 static int do_win32_extract_stream(HANDLE hStream, struct wim_lookup_table_entry *lte)
153 return extract_wim_resource(lte, wim_resource_size(lte),
154 win32_extract_chunk, hStream);
157 static int win32_extract_stream(const struct wim_inode *inode,
159 const wchar_t *stream_name_utf16,
160 struct wim_lookup_table_entry *lte)
162 wchar_t *stream_path;
166 DWORD creationDisposition = CREATE_ALWAYS;
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;
178 size_t stream_name_nchars;
179 const wchar_t *prefix;
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'\\') {
186 stream_path_nchars += 2;
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);
194 /* Unnamed stream; it's path is just the path to the file
196 stream_path = (wchar_t*)path;
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\"",
208 ret = WIMLIB_ERR_MKDIR;
212 DEBUG("Created directory \"%ls\"", stream_path);
213 if (!(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
217 creationDisposition = OPEN_EXISTING;
221 DEBUG("Opening \"%ls\"", stream_path);
222 h = CreateFileW(stream_path,
223 GENERIC_WRITE | WRITE_OWNER | WRITE_DAC,
227 FILE_FLAG_OPEN_REPARSE_POINT |
228 FILE_FLAG_BACKUP_SEMANTICS |
231 if (h == INVALID_HANDLE_VALUE) {
232 err = GetLastError();
233 ERROR("Failed to create \"%ls\"", stream_path);
235 ret = WIMLIB_ERR_OPEN;
239 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
240 stream_name_utf16 == NULL)
242 DEBUG("Setting reparse data on \"%ls\"", path);
243 ret = win32_set_reparse_data(h, inode->i_reparse_tag, lte, path);
245 goto fail_close_handle;
248 DEBUG("Extracting \"%ls\" (len = %zu)",
249 stream_path, wim_resource_size(lte));
250 ret = do_win32_extract_stream(h, lte);
252 goto fail_close_handle;
256 DEBUG("Closing \"%ls\"", stream_path);
257 if (!CloseHandle(h)) {
258 err = GetLastError();
259 ERROR("Failed to close \"%ls\"", stream_path);
261 ret = WIMLIB_ERR_WRITE;
269 ERROR("Error extracting %ls", stream_path);
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.
279 * @inode: WIM inode for this file or directory.
280 * @path: UTF-16LE external path to extract the inode to.
282 * Returns 0 on success; nonzero on failure.
284 static int win32_extract_streams(struct wim_inode *inode,
287 struct wim_lookup_table_entry *unnamed_lte;
290 unnamed_lte = inode_unnamed_lte_resolved(inode);
291 ret = win32_extract_stream(inode, path, NULL, unnamed_lte);
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))
304 ret = win32_extract_stream(inode,
306 (const wchar_t*)ads_entry->stream_name,
317 * Sets the security descriptor on an extracted file. This is Win32-specific
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.
325 * Returns 0 on success; nonzero on failure.
327 static int win32_set_security_data(const struct wim_inode *inode,
329 const struct wim_security_data *sd)
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]))
338 DWORD err = GetLastError();
339 ERROR("Can't set security descriptor on \"%ls\"", path);
341 return WIMLIB_ERR_WRITE;
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)
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) */
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 "
361 output_path, lte->extracted_file);
362 return WIMLIB_ERR_LINK;
365 int num_path_components;
366 int num_output_dir_path_components;
367 size_t extracted_file_len;
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);
377 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
378 num_path_components++;
379 num_output_dir_path_components--;
381 extracted_file_len = strlen(lte->extracted_file);
383 char buf[extracted_file_len + 3 * num_path_components + 1];
386 for (i = 0; i < num_path_components; i++) {
391 p2 = lte->extracted_file;
394 while (num_output_dir_path_components--)
395 p2 = path_next_part(p2, NULL);
397 if (symlink(buf, output_path) != 0) {
398 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
400 buf, lte->extracted_file);
401 return WIMLIB_ERR_LINK;
407 static int symlink_apply_unix_data(const char *link,
408 const struct wimlib_unix_data *unix_data)
410 if (lchown(link, unix_data->uid, unix_data->gid)) {
411 if (errno == EPERM) {
413 WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
415 ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
416 return WIMLIB_ERR_INVALID_DENTRY;
422 static int fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
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");
429 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
430 return WIMLIB_ERR_INVALID_DENTRY;
434 if (fchmod(fd, unix_data->mode)) {
435 if (errno == EPERM) {
436 WARNING_WITH_ERRNO("failed to set UNIX file mode");
439 ERROR_WITH_ERRNO("failed to set UNIX file mode");
440 return WIMLIB_ERR_INVALID_DENTRY;
446 static int dir_apply_unix_data(const char *dir,
447 const struct wimlib_unix_data *unix_data)
449 int dfd = open(dir, O_RDONLY);
452 ret = fd_apply_unix_data(dfd, unix_data);
454 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
455 ret = WIMLIB_ERR_MKDIR;
458 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
459 ret = WIMLIB_ERR_MKDIR;
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)
469 /* Normal mode of extraction. Regular files and hard links are
470 * extracted in the way that they appear in the WIM. */
474 struct wim_inode *inode = dentry->d_inode;
476 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
477 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
478 WIMLIB_EXTRACT_FLAG_HARDLINK))))
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 "
494 inode->i_extracted_file);
495 return WIMLIB_ERR_LINK;
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;
508 /* Extract the contents of the file to @output_path. */
510 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
512 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
514 return WIMLIB_ERR_OPEN;
518 /* Empty file with no lookup table entry */
519 DEBUG("Empty file `%s'.", output_path);
521 goto out_extract_unix_data;
524 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
526 ERROR("Failed to extract resource to `%s'", output_path);
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);
539 ret = fd_apply_unix_data(out_fd, &unix_data);
544 args->progress.extract.completed_bytes += wim_resource_size(lte);
546 if (close(out_fd) != 0) {
547 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
549 ret = WIMLIB_ERR_WRITE;
554 static int extract_regular_file(struct wim_dentry *dentry,
555 struct apply_args *args,
556 const char *output_path)
558 struct wim_lookup_table_entry *lte;
559 const struct wim_inode *inode = dentry->d_inode;
561 lte = inode_unnamed_lte_resolved(inode);
563 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
564 WIMLIB_EXTRACT_FLAG_HARDLINK)))
566 if (lte->extracted_file) {
567 return extract_regular_file_linked(dentry, output_path, args, lte);
569 lte->extracted_file = STRDUP(output_path);
570 if (!lte->extracted_file)
571 return WIMLIB_ERR_NOMEM;
574 return extract_regular_file_unlinked(dentry, args, output_path, lte);
577 static int extract_symlink(struct wim_dentry *dentry,
578 struct apply_args *args,
579 const char *output_path)
582 ssize_t ret = inode_readlink(dentry->d_inode, target,
583 sizeof(target), args->w, 0);
584 struct wim_lookup_table_entry *lte;
587 ERROR("Could not read the symbolic link from dentry `%s'",
588 dentry->full_path_utf8);
589 return WIMLIB_ERR_INVALID_DENTRY;
591 ret = symlink(target, output_path);
593 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
594 output_path, target);
595 return WIMLIB_ERR_LINK;
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);
607 ret = symlink_apply_unix_data(output_path, &unix_data);
611 args->progress.extract.completed_bytes += wim_resource_size(lte);
615 #endif /* !(__CYGWIN__ || __WIN32__) */
617 static int extract_directory(struct wim_dentry *dentry,
618 const char *output_path, bool is_root)
623 ret = stat(output_path, &stbuf);
625 if (S_ISDIR(stbuf.st_mode)) {
627 /*WARNING("`%s' already exists", output_path);*/
630 ERROR("`%s' is not a directory", output_path);
631 return WIMLIB_ERR_MKDIR;
634 if (errno != ENOENT) {
635 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
636 return WIMLIB_ERR_STAT;
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'",
643 return WIMLIB_ERR_MKDIR;
647 #if !defined(__CYGWIN__) && !defined(__WIN32__)
649 struct wimlib_unix_data unix_data;
650 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
656 ret = dir_apply_unix_data(output_path, &unix_data);
662 /* Extracts a file, directory, or symbolic link from the WIM archive. */
663 static int apply_dentry_normal(struct wim_dentry *dentry, void *arg)
665 struct apply_args *args = arg;
666 struct wim_inode *inode = dentry->d_inode;
670 len = strlen(args->target);
671 if (dentry_is_root(dentry)) {
672 output_path = (char*)args->target;
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;
681 #if defined(__CYGWIN__) || defined(__WIN32__)
683 size_t utf16_path_len;
686 ret = utf8_to_utf16(output_path, len, &utf16_path, &utf16_path_len);
690 if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
691 /* Linked file, with another name already extracted. Create a
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,
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;
708 /* Create the file, directory, or reparse point, and extract the
710 ret = win32_extract_streams(inode, (const wchar_t*)utf16_path);
712 goto out_free_utf16_path;
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));
722 goto out_free_utf16_path;
724 if (inode->i_nlink > 1) {
725 /* Save extracted path for a later call to
726 * CreateHardLinkW() if this inode has multiple links.
728 inode->i_extracted_file = utf16_path;
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,
744 return extract_regular_file(dentry, args, output_path);
748 /* Apply timestamps to an extracted file or directory */
749 static int apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
751 struct apply_args *args = arg;
755 const struct wim_inode *inode = dentry->d_inode;
757 len = strlen(args->target);
758 if (dentry_is_root(dentry)) {
759 output_path = (char*)args->target;
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;
768 #if defined(__CYGWIN__) || defined(__WIN32__)
771 size_t utf16_path_len;
776 ret = utf8_to_utf16(output_path, len, &utf16_path, &utf16_path_len);
780 DEBUG("Opening \"%ls\" to set timestamps", utf16_path);
781 h = CreateFileW((const wchar_t*)utf16_path,
786 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
789 if (h == INVALID_HANDLE_VALUE)
790 err = GetLastError();
792 if (h == INVALID_HANDLE_VALUE)
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};
802 DEBUG("Calling SetFileTime() on \"%s\"", output_path);
803 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
804 err = GetLastError();
808 DEBUG("Closing \"%s\"", output_path);
809 if (!CloseHandle(h)) {
810 err = GetLastError();
815 ERROR("Can't set timestamps on \"%s\"", output_path);
817 return WIMLIB_ERR_WRITE;
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]);
827 ret = lutimes(output_path, tv);
834 if (errno == ENOSYS) {
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)
842 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
843 /*WARNING_WITH_ERRNO("Failed to set timestamp on file `%s',*/
845 args->num_lutimes_warnings++;
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)
856 struct apply_args *args = arg;
859 if (dentry->is_extracted)
862 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
863 if (inode_unnamed_lte_resolved(dentry->d_inode))
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,
872 ret = args->apply_dentry(dentry, args);
874 dentry->is_extracted = 1;
878 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
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)
885 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
891 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
893 struct list_head *cur;
895 struct wim_lookup_table_entry **array;
900 list_for_each(cur, stream_list)
902 array_size = num_streams * sizeof(array[0]);
903 array = MALLOC(array_size);
905 ERROR("Failed to allocate %zu bytes to sort stream entries",
907 return WIMLIB_ERR_NOMEM;
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);
915 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
917 INIT_LIST_HEAD(stream_list);
918 for (i = 0; i < num_streams; i++)
919 list_add_tail(&array[i]->staging_list, stream_list);
924 static void calculate_bytes_to_extract(struct list_head *stream_list,
926 union wimlib_progress_info *progress)
928 struct wim_lookup_table_entry *lte;
932 /* For each stream to be extracted... */
933 list_for_each_entry(lte, stream_list, staging_list) {
935 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
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) {
943 total_bytes += wim_resource_size(lte);
946 num_streams += lte->out_refcnt;
947 total_bytes += lte->out_refcnt * wim_resource_size(lte);
950 progress->extract.num_streams = num_streams;
951 progress->extract.total_bytes = total_bytes;
952 progress->extract.completed_bytes = 0;
955 static void maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
956 struct list_head *stream_list)
958 if (++lte->out_refcnt == 1) {
959 INIT_LIST_HEAD(<e->inode_list);
960 list_add_tail(<e->staging_list, stream_list);
964 static void inode_find_streams_for_extraction(struct wim_inode *inode,
965 struct list_head *stream_list,
968 struct wim_lookup_table_entry *lte;
969 bool inode_added = false;
971 lte = inode_unnamed_lte_resolved(inode);
973 maybe_add_stream_for_extraction(lte, stream_list);
974 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
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;
983 maybe_add_stream_for_extraction(lte,
986 list_add_tail(&inode->i_lte_inode_list,
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,
1002 struct wim_inode *inode;
1003 struct hlist_node *cur;
1004 struct wim_dentry *dentry;
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,
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);
1023 static const struct apply_operations normal_apply_operations = {
1024 .apply_dentry = apply_dentry_normal,
1025 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
1029 static const struct apply_operations ntfs_apply_operations = {
1030 .apply_dentry = apply_dentry_ntfs,
1031 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
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)
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;
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).
1052 * The outer loop is over the distinct streams to be extracted so that
1053 * sequential reading of the WIM can be implemented. */
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, <e->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
1063 ret = maybe_apply_dentry(dentry, args);
1066 if (progress_func &&
1067 args->progress.extract.completed_bytes >= next_progress)
1069 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
1071 if (args->progress.extract.completed_bytes >=
1072 args->progress.extract.total_bytes)
1074 next_progress = ~0ULL;
1077 min (args->progress.extract.completed_bytes +
1079 args->progress.extract.total_bytes);
1088 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
1090 static int extract_single_image(WIMStruct *w, int image,
1091 const char *target, int extract_flags,
1092 wimlib_progress_func_t progress_func)
1095 struct list_head stream_list;
1096 struct hlist_head *inode_list;
1098 struct apply_args args;
1099 const struct apply_operations *ops;
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;
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;
1118 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1119 args.vol = ntfs_mount(target, 0);
1121 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
1122 return WIMLIB_ERR_NTFS_3G;
1124 ops = &ntfs_apply_operations;
1127 ops = &normal_apply_operations;
1129 ret = select_wim_image(w, image);
1133 inode_list = &w->image_metadata[image - 1].inode_list;
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);
1139 /* Calculate the number of bytes of data that will be extracted */
1140 calculate_bytes_to_extract(&stream_list, extract_flags,
1143 if (progress_func) {
1144 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
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);
1154 WARNING("Falling back to non-sequential extraction");
1155 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1159 if (progress_func) {
1160 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
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;
1172 if (progress_func) {
1173 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
1177 /* Extract non-empty files */
1178 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
1182 if (progress_func) {
1183 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
1187 /* Apply timestamps */
1188 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
1189 ops->apply_dentry_timestamps, &args);
1193 if (progress_func) {
1194 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
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);
1204 ret = WIMLIB_ERR_NTFS_3G;
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,
1216 wimlib_progress_func_t progress_func)
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];
1223 const char *image_name;
1225 ret = extract_directory(NULL, target, true);
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);
1236 /* Image name is empty. Use image number instead */
1237 sprintf(buf + output_path_len + 1, "%d", image);
1239 ret = extract_single_image(w, image, buf, extract_flags,
1247 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1249 WIMLIBAPI int wimlib_extract_image(WIMStruct *w,
1253 WIMStruct **additional_swms,
1254 unsigned num_additional_swms,
1255 wimlib_progress_func_t progress_func)
1257 struct wim_lookup_table *joined_tab, *w_tab_save;
1261 return WIMLIB_ERR_INVALID_PARAM;
1263 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
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;
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;
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;
1280 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
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;
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;
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;
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;
1303 ret = verify_swm_set(w, additional_swms, num_additional_swms);
1307 if (num_additional_swms) {
1308 ret = new_joined_lookup_table(w, additional_swms,
1309 num_additional_swms, &joined_tab);
1312 w_tab_save = w->lookup_table;
1313 w->lookup_table = joined_tab;
1316 if (image == WIMLIB_ALL_IMAGES) {
1317 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1318 ret = extract_all_images(w, target, extract_flags,
1321 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1322 ret = extract_single_image(w, image, target, extract_flags,
1326 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1327 WIMLIB_EXTRACT_FLAG_HARDLINK))
1329 for_lookup_table_entry(w->lookup_table,
1330 lte_free_extracted_file,
1334 if (num_additional_swms) {
1335 free_lookup_table(w->lookup_table);
1336 w->lookup_table = w_tab_save;