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,
83 WARNING("\"%ls\" is marked as a reparse point but had no reparse data",
87 len = wim_resource_size(lte);
88 if (len > 16 * 1024 - 8) {
89 WARNING("\"%ls\": reparse data too long!", path);
93 /* The WIM stream omits the ReparseTag and ReparseDataLength fields, so
94 * leave 8 bytes of space for them at the beginning of the buffer, then
95 * set them manually. */
96 buf = alloca(len + 8);
97 ret = read_full_wim_resource(lte, buf + 8, 0);
100 *(u32*)(buf + 0) = reparse_tag;
101 *(u16*)(buf + 4) = len;
102 *(u16*)(buf + 6) = 0;
104 /* Set the reparse data on the open file using the
105 * FSCTL_SET_REPARSE_POINT ioctl.
107 * There are contradictions in Microsoft's documentation for this:
109 * "If hDevice was opened without specifying FILE_FLAG_OVERLAPPED,
110 * lpOverlapped is ignored."
112 * --- So setting lpOverlapped to NULL is okay since it's ignored.
114 * "If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an
115 * operation returns no output data and lpOutBuffer is NULL,
116 * DeviceIoControl makes use of lpBytesReturned. After such an
117 * operation, the value of lpBytesReturned is meaningless."
119 * --- So lpOverlapped not really ignored, as it affects another
120 * parameter. This is the actual behavior: lpBytesReturned must be
121 * specified, even though lpBytesReturned is documented as:
123 * "Not used with this operation; set to NULL."
126 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, buf, len + 8,
128 &bytesReturned /* lpBytesReturned */,
129 NULL /* lpOverlapped */))
131 DWORD err = GetLastError();
132 ERROR("Failed to set reparse data on \"%ls\"", path);
134 return WIMLIB_ERR_WRITE;
140 static int win32_extract_chunk(const u8 *buf, size_t len, u64 offset, void *arg)
142 HANDLE hStream = arg;
144 DWORD nbytes_written;
145 wimlib_assert(len <= 0xffffffff);
147 if (!WriteFile(hStream, buf, len, &nbytes_written, NULL) ||
148 nbytes_written != len)
150 DWORD err = GetLastError();
151 ERROR("WriteFile(): write error");
153 return WIMLIB_ERR_WRITE;
158 static int do_win32_extract_stream(HANDLE hStream, struct wim_lookup_table_entry *lte)
160 return extract_wim_resource(lte, wim_resource_size(lte),
161 win32_extract_chunk, hStream);
164 static int win32_extract_stream(const struct wim_inode *inode,
166 const wchar_t *stream_name_utf16,
167 struct wim_lookup_table_entry *lte)
169 wchar_t *stream_path;
173 DWORD creationDisposition = CREATE_ALWAYS;
175 if (stream_name_utf16) {
176 /* Named stream. Create a buffer that contains the UTF-16LE
177 * string [./]@path:@stream_name_utf16. This is needed to
178 * create and open the stream using CreateFileW(). I'm not
179 * aware of any other APIs to do this. Note: note that the
180 * '$DATA' suffix seems to be unneeded; Additional note: a "./"
181 * prefix needs to be added when the path is not absolute to
182 * avoid ambiguity with drive letters. */
183 size_t stream_path_nchars;
185 size_t stream_name_nchars;
186 const wchar_t *prefix;
188 path_nchars = wcslen(path);
189 stream_name_nchars = wcslen(stream_name_utf16);
190 stream_path_nchars = path_nchars + 1 + stream_name_nchars;
191 if (path[0] != L'/' && path[1] != L'\\') {
193 stream_path_nchars += 2;
197 stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
198 swprintf(stream_path, stream_path_nchars + 1, L"%ls%ls:%ls",
199 prefix, path, stream_name_utf16);
201 /* Unnamed stream; it's path is just the path to the file
203 stream_path = (wchar_t*)path;
205 /* Directories must be created with CreateDirectoryW(). Then
206 * the call to CreateFileW() will merely open the directory that
207 * was already created rather than creating a new file. */
208 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
209 if (!CreateDirectoryW(stream_path, NULL)) {
210 err = GetLastError();
211 if (err != ERROR_ALREADY_EXISTS) {
212 ERROR("Failed to create directory \"%ls\"",
215 ret = WIMLIB_ERR_MKDIR;
219 DEBUG("Created directory \"%ls\"", stream_path);
220 if (!(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
224 creationDisposition = OPEN_EXISTING;
228 DEBUG("Opening \"%ls\"", stream_path);
229 h = CreateFileW(stream_path,
230 GENERIC_WRITE | WRITE_OWNER | WRITE_DAC,
234 FILE_FLAG_OPEN_REPARSE_POINT |
235 FILE_FLAG_BACKUP_SEMANTICS |
238 if (h == INVALID_HANDLE_VALUE) {
239 err = GetLastError();
240 ERROR("Failed to create \"%ls\"", stream_path);
242 ret = WIMLIB_ERR_OPEN;
246 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
247 stream_name_utf16 == NULL)
249 DEBUG("Setting reparse data on \"%ls\"", path);
250 ret = win32_set_reparse_data(h, inode->i_reparse_tag, lte, path);
252 goto fail_close_handle;
255 DEBUG("Extracting \"%ls\" (len = %zu)",
256 stream_path, wim_resource_size(lte));
257 ret = do_win32_extract_stream(h, lte);
259 goto fail_close_handle;
263 DEBUG("Closing \"%ls\"", stream_path);
264 if (!CloseHandle(h)) {
265 err = GetLastError();
266 ERROR("Failed to close \"%ls\"", stream_path);
268 ret = WIMLIB_ERR_WRITE;
276 ERROR("Error extracting %ls", stream_path);
282 * Creates a file, directory, or reparse point and extracts all streams to it
283 * (unnamed data stream and/or reparse point stream, plus any alternate data
284 * streams). This in Win32-specific code.
286 * @inode: WIM inode for this file or directory.
287 * @path: UTF-16LE external path to extract the inode to.
289 * Returns 0 on success; nonzero on failure.
291 static int win32_extract_streams(struct wim_inode *inode,
294 struct wim_lookup_table_entry *unnamed_lte;
297 unnamed_lte = inode_unnamed_lte_resolved(inode);
298 ret = win32_extract_stream(inode, path, NULL, unnamed_lte);
301 for (u16 i = 0; i < inode->i_num_ads; i++) {
302 const struct wim_ads_entry *ads_entry = &inode->i_ads_entries[i];
303 if (ads_entry->stream_name_len != 0) {
304 /* Skip special UNIX data entries (see documentation for
305 * WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) */
306 if (ads_entry->stream_name_len == WIMLIB_UNIX_DATA_TAG_LEN
307 && !memcmp(ads_entry->stream_name_utf8,
308 WIMLIB_UNIX_DATA_TAG,
309 WIMLIB_UNIX_DATA_TAG_LEN))
311 ret = win32_extract_stream(inode,
313 (const wchar_t*)ads_entry->stream_name,
324 * Sets the security descriptor on an extracted file. This is Win32-specific
327 * @inode: The WIM inode that was extracted and has a security descriptor.
328 * @path: UTF-16LE external path that the inode was extracted to.
329 * @sd: Security data for the WIM image.
330 * @path_utf8: @path in UTF-8 for error messages only.
332 * Returns 0 on success; nonzero on failure.
334 static int win32_set_security_data(const struct wim_inode *inode,
336 const struct wim_security_data *sd)
338 SECURITY_INFORMATION securityInformation = DACL_SECURITY_INFORMATION |
339 SACL_SECURITY_INFORMATION |
340 OWNER_SECURITY_INFORMATION |
341 GROUP_SECURITY_INFORMATION;
342 if (!SetFileSecurityW(path, securityInformation,
343 (PSECURITY_DESCRIPTOR)sd->descriptors[inode->i_security_id]))
345 DWORD err = GetLastError();
346 ERROR("Can't set security descriptor on \"%ls\"", path);
348 return WIMLIB_ERR_WRITE;
354 static int extract_regular_file_linked(struct wim_dentry *dentry,
355 const char *output_path,
356 struct apply_args *args,
357 struct wim_lookup_table_entry *lte)
359 /* This mode overrides the normal hard-link extraction and
360 * instead either symlinks or hardlinks *all* identical files in
361 * the WIM, even if they are in a different image (in the case
362 * of a multi-image extraction) */
364 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
365 if (link(lte->extracted_file, output_path) != 0) {
366 ERROR_WITH_ERRNO("Failed to hard link "
368 output_path, lte->extracted_file);
369 return WIMLIB_ERR_LINK;
372 int num_path_components;
373 int num_output_dir_path_components;
374 size_t extracted_file_len;
379 num_path_components =
380 get_num_path_components(dentry->full_path_utf8) - 1;
381 num_output_dir_path_components =
382 get_num_path_components(args->target);
384 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
385 num_path_components++;
386 num_output_dir_path_components--;
388 extracted_file_len = strlen(lte->extracted_file);
390 char buf[extracted_file_len + 3 * num_path_components + 1];
393 for (i = 0; i < num_path_components; i++) {
398 p2 = lte->extracted_file;
401 while (num_output_dir_path_components--)
402 p2 = path_next_part(p2, NULL);
404 if (symlink(buf, output_path) != 0) {
405 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
407 buf, lte->extracted_file);
408 return WIMLIB_ERR_LINK;
414 static int symlink_apply_unix_data(const char *link,
415 const struct wimlib_unix_data *unix_data)
417 if (lchown(link, unix_data->uid, unix_data->gid)) {
418 if (errno == EPERM) {
420 WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
422 ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
423 return WIMLIB_ERR_INVALID_DENTRY;
429 static int fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
431 if (fchown(fd, unix_data->uid, unix_data->gid)) {
432 if (errno == EPERM) {
433 WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
436 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
437 return WIMLIB_ERR_INVALID_DENTRY;
441 if (fchmod(fd, unix_data->mode)) {
442 if (errno == EPERM) {
443 WARNING_WITH_ERRNO("failed to set UNIX file mode");
446 ERROR_WITH_ERRNO("failed to set UNIX file mode");
447 return WIMLIB_ERR_INVALID_DENTRY;
453 static int dir_apply_unix_data(const char *dir,
454 const struct wimlib_unix_data *unix_data)
456 int dfd = open(dir, O_RDONLY);
459 ret = fd_apply_unix_data(dfd, unix_data);
461 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
462 ret = WIMLIB_ERR_MKDIR;
465 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
466 ret = WIMLIB_ERR_MKDIR;
471 static int extract_regular_file_unlinked(struct wim_dentry *dentry,
472 struct apply_args *args,
473 const char *output_path,
474 struct wim_lookup_table_entry *lte)
476 /* Normal mode of extraction. Regular files and hard links are
477 * extracted in the way that they appear in the WIM. */
481 struct wim_inode *inode = dentry->d_inode;
483 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
484 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
485 WIMLIB_EXTRACT_FLAG_HARDLINK))))
487 /* If the dentry is part of a hard link set of at least 2
488 * dentries and one of the other dentries has already been
489 * extracted, make a hard link to the file corresponding to this
490 * already-extracted directory. Otherwise, extract the file and
491 * set the inode->i_extracted_file field so that other dentries
492 * in the hard link group can link to it. */
493 if (inode->i_nlink > 1) {
494 if (inode->i_extracted_file) {
495 DEBUG("Extracting hard link `%s' => `%s'",
496 output_path, inode->i_extracted_file);
497 if (link(inode->i_extracted_file, output_path) != 0) {
498 ERROR_WITH_ERRNO("Failed to hard link "
501 inode->i_extracted_file);
502 return WIMLIB_ERR_LINK;
506 FREE(inode->i_extracted_file);
507 inode->i_extracted_file = STRDUP(output_path);
508 if (!inode->i_extracted_file) {
509 ERROR("Failed to allocate memory for filename");
510 return WIMLIB_ERR_NOMEM;
515 /* Extract the contents of the file to @output_path. */
517 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
519 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
521 return WIMLIB_ERR_OPEN;
525 /* Empty file with no lookup table entry */
526 DEBUG("Empty file `%s'.", output_path);
528 goto out_extract_unix_data;
531 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
533 ERROR("Failed to extract resource to `%s'", output_path);
537 out_extract_unix_data:
538 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
539 struct wimlib_unix_data unix_data;
540 ret = inode_get_unix_data(inode, &unix_data, NULL);
546 ret = fd_apply_unix_data(out_fd, &unix_data);
551 args->progress.extract.completed_bytes += wim_resource_size(lte);
553 if (close(out_fd) != 0) {
554 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
556 ret = WIMLIB_ERR_WRITE;
561 static int extract_regular_file(struct wim_dentry *dentry,
562 struct apply_args *args,
563 const char *output_path)
565 struct wim_lookup_table_entry *lte;
566 const struct wim_inode *inode = dentry->d_inode;
568 lte = inode_unnamed_lte_resolved(inode);
570 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
571 WIMLIB_EXTRACT_FLAG_HARDLINK)))
573 if (lte->extracted_file) {
574 return extract_regular_file_linked(dentry, output_path, args, lte);
576 lte->extracted_file = STRDUP(output_path);
577 if (!lte->extracted_file)
578 return WIMLIB_ERR_NOMEM;
581 return extract_regular_file_unlinked(dentry, args, output_path, lte);
584 static int extract_symlink(struct wim_dentry *dentry,
585 struct apply_args *args,
586 const char *output_path)
589 ssize_t ret = inode_readlink(dentry->d_inode, target,
590 sizeof(target), args->w, 0);
591 struct wim_lookup_table_entry *lte;
594 ERROR("Could not read the symbolic link from dentry `%s'",
595 dentry->full_path_utf8);
596 return WIMLIB_ERR_INVALID_DENTRY;
598 ret = symlink(target, output_path);
600 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
601 output_path, target);
602 return WIMLIB_ERR_LINK;
604 lte = inode_unnamed_lte_resolved(dentry->d_inode);
605 wimlib_assert(lte != NULL);
606 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
607 struct wimlib_unix_data unix_data;
608 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
614 ret = symlink_apply_unix_data(output_path, &unix_data);
618 args->progress.extract.completed_bytes += wim_resource_size(lte);
622 #endif /* !__CYGWIN__ && !__WIN32__ */
624 static int extract_directory(struct wim_dentry *dentry,
625 const char *output_path, bool is_root)
630 ret = stat(output_path, &stbuf);
632 if (S_ISDIR(stbuf.st_mode)) {
634 /*WARNING("`%s' already exists", output_path);*/
637 ERROR("`%s' is not a directory", output_path);
638 return WIMLIB_ERR_MKDIR;
641 if (errno != ENOENT) {
642 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
643 return WIMLIB_ERR_STAT;
646 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
647 S_IROTH | S_IXOTH) != 0) {
648 ERROR_WITH_ERRNO("Cannot create directory `%s'",
650 return WIMLIB_ERR_MKDIR;
654 #if !defined(__CYGWIN__) && !defined(__WIN32__)
656 struct wimlib_unix_data unix_data;
657 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
663 ret = dir_apply_unix_data(output_path, &unix_data);
669 /* Extracts a file, directory, or symbolic link from the WIM archive. */
670 static int apply_dentry_normal(struct wim_dentry *dentry, void *arg)
672 struct apply_args *args = arg;
673 struct wim_inode *inode = dentry->d_inode;
677 len = strlen(args->target);
678 if (dentry_is_root(dentry)) {
679 output_path = (char*)args->target;
681 output_path = alloca(len + dentry->full_path_utf8_len + 1);
682 memcpy(output_path, args->target, len);
683 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
684 output_path[len + dentry->full_path_utf8_len] = '\0';
685 len += dentry->full_path_utf8_len;
688 #if defined(__CYGWIN__) || defined(__WIN32__)
690 size_t utf16_path_len;
693 ret = utf8_to_utf16(output_path, len, &utf16_path, &utf16_path_len);
697 if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
698 /* Linked file, with another name already extracted. Create a
700 DEBUG("Creating hard link \"%ls => %ls\"",
701 (const wchar_t*)utf16_path,
702 (const wchar_t*)inode->i_extracted_file);
703 if (!CreateHardLinkW((const wchar_t*)utf16_path,
704 (const wchar_t*)inode->i_extracted_file,
707 err = GetLastError();
708 ERROR("Can't create hard link \"%ls => %ls\"",
709 (const wchar_t*)utf16_path,
710 (const wchar_t*)inode->i_extracted_file);
711 ret = WIMLIB_ERR_LINK;
715 /* Create the file, directory, or reparse point, and extract the
717 ret = win32_extract_streams(inode, (const wchar_t*)utf16_path);
719 goto out_free_utf16_path;
721 /* Set security descriptor if present */
722 if (inode->i_security_id != -1) {
723 DEBUG("Setting security descriptor %d on %s",
724 inode->i_security_id, output_path);
725 ret = win32_set_security_data(inode,
726 (const wchar_t*)utf16_path,
727 wim_const_security_data(args->w));
729 goto out_free_utf16_path;
731 if (inode->i_nlink > 1) {
732 /* Save extracted path for a later call to
733 * CreateHardLinkW() if this inode has multiple links.
735 inode->i_extracted_file = utf16_path;
744 if (inode_is_symlink(inode))
745 return extract_symlink(dentry, args, output_path);
746 else if (inode_is_directory(inode))
747 return extract_directory((args->extract_flags &
748 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
751 return extract_regular_file(dentry, args, output_path);
755 /* Apply timestamps to an extracted file or directory */
756 static int apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
758 struct apply_args *args = arg;
762 const struct wim_inode *inode = dentry->d_inode;
764 len = strlen(args->target);
765 if (dentry_is_root(dentry)) {
766 output_path = (char*)args->target;
768 output_path = alloca(len + dentry->full_path_utf8_len + 1);
769 memcpy(output_path, args->target, len);
770 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
771 output_path[len + dentry->full_path_utf8_len] = '\0';
772 len += dentry->full_path_utf8_len;
775 #if defined(__CYGWIN__) || defined(__WIN32__)
778 size_t utf16_path_len;
783 ret = utf8_to_utf16(output_path, len, &utf16_path, &utf16_path_len);
787 DEBUG("Opening \"%ls\" to set timestamps", utf16_path);
788 h = CreateFileW(utf16_path, GENERIC_WRITE, FILE_SHARE_READ,
790 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
793 if (h == INVALID_HANDLE_VALUE)
794 err = GetLastError();
796 if (h == INVALID_HANDLE_VALUE)
799 FILETIME creationTime = {.dwLowDateTime = dentry->d_inode->i_creation_time & 0xffffffff,
800 .dwHighDateTime = dentry->d_inode->i_creation_time >> 32};
801 FILETIME lastAccessTime = {.dwLowDateTime = dentry->d_inode->i_last_access_time & 0xffffffff,
802 .dwHighDateTime = dentry->d_inode->i_last_access_time >> 32};
803 FILETIME lastWriteTime = {.dwLowDateTime = dentry->d_inode->i_last_write_time & 0xffffffff,
804 .dwHighDateTime = dentry->d_inode->i_last_write_time >> 32};
806 DEBUG("Calling SetFileTime() on \"%s\"", output_path);
807 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
808 err = GetLastError();
812 DEBUG("Closing \"%s\"", output_path);
813 if (!CloseHandle(h)) {
814 err = GetLastError();
819 ERROR("Can't set timestamps on \"%s\"", output_path);
821 return WIMLIB_ERR_WRITE;
825 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
826 * into struct timeval's. */
827 struct timeval tv[2];
828 wim_timestamp_to_timeval(inode->i_last_access_time, &tv[0]);
829 wim_timestamp_to_timeval(inode->i_last_write_time, &tv[1]);
831 ret = lutimes(output_path, tv);
838 if (errno == ENOSYS) {
840 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
841 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
842 if (utime(output_path, &buf) == 0)
846 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
847 /*WARNING_WITH_ERRNO("Failed to set timestamp on file `%s',*/
849 args->num_lutimes_warnings++;
856 /* Extract a dentry if it hasn't already been extracted, and either the dentry
857 * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
858 static int maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
860 struct apply_args *args = arg;
863 if (dentry->is_extracted)
866 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
867 if (inode_unnamed_lte_resolved(dentry->d_inode))
870 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
871 args->progress_func) {
872 args->progress.extract.cur_path = dentry->full_path_utf8;
873 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
876 ret = args->apply_dentry(dentry, args);
878 dentry->is_extracted = 1;
882 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
884 const struct wim_lookup_table_entry *lte1, *lte2;
885 lte1 = *(const struct wim_lookup_table_entry**)p1;
886 lte2 = *(const struct wim_lookup_table_entry**)p2;
887 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
889 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
895 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
897 struct list_head *cur;
899 struct wim_lookup_table_entry **array;
904 list_for_each(cur, stream_list)
906 array_size = num_streams * sizeof(array[0]);
907 array = MALLOC(array_size);
909 ERROR("Failed to allocate %zu bytes to sort stream entries",
911 return WIMLIB_ERR_NOMEM;
913 cur = stream_list->next;
914 for (i = 0; i < num_streams; i++) {
915 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
919 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
921 INIT_LIST_HEAD(stream_list);
922 for (i = 0; i < num_streams; i++)
923 list_add_tail(&array[i]->staging_list, stream_list);
928 static void calculate_bytes_to_extract(struct list_head *stream_list,
930 union wimlib_progress_info *progress)
932 struct wim_lookup_table_entry *lte;
936 /* For each stream to be extracted... */
937 list_for_each_entry(lte, stream_list, staging_list) {
939 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
941 /* In the symlink or hard link extraction mode, each
942 * stream will be extracted one time regardless of how
943 * many dentries share the stream. */
944 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
945 if (!lte->extracted_file) {
947 total_bytes += wim_resource_size(lte);
950 num_streams += lte->out_refcnt;
951 total_bytes += lte->out_refcnt * wim_resource_size(lte);
954 progress->extract.num_streams = num_streams;
955 progress->extract.total_bytes = total_bytes;
956 progress->extract.completed_bytes = 0;
959 static void maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
960 struct list_head *stream_list)
962 if (++lte->out_refcnt == 1) {
963 INIT_LIST_HEAD(<e->inode_list);
964 list_add_tail(<e->staging_list, stream_list);
968 static void inode_find_streams_for_extraction(struct wim_inode *inode,
969 struct list_head *stream_list,
972 struct wim_lookup_table_entry *lte;
973 bool inode_added = false;
975 lte = inode_unnamed_lte_resolved(inode);
977 maybe_add_stream_for_extraction(lte, stream_list);
978 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
982 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
983 for (unsigned i = 0; i < inode->i_num_ads; i++) {
984 if (inode->i_ads_entries[i].stream_name_len != 0) {
985 lte = inode->i_ads_entries[i].lte;
987 maybe_add_stream_for_extraction(lte,
990 list_add_tail(&inode->i_lte_inode_list,
1001 static void find_streams_for_extraction(struct hlist_head *inode_list,
1002 struct list_head *stream_list,
1003 struct wim_lookup_table *lookup_table,
1006 struct wim_inode *inode;
1007 struct hlist_node *cur;
1008 struct wim_dentry *dentry;
1010 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
1011 INIT_LIST_HEAD(stream_list);
1012 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
1013 if (!inode->i_resolved)
1014 inode_resolve_ltes(inode, lookup_table);
1015 inode_for_each_dentry(dentry, inode)
1016 dentry->is_extracted = 0;
1017 inode_find_streams_for_extraction(inode, stream_list,
1022 struct apply_operations {
1023 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
1024 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
1027 static const struct apply_operations normal_apply_operations = {
1028 .apply_dentry = apply_dentry_normal,
1029 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
1033 static const struct apply_operations ntfs_apply_operations = {
1034 .apply_dentry = apply_dentry_ntfs,
1035 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
1039 static int apply_stream_list(struct list_head *stream_list,
1040 struct apply_args *args,
1041 const struct apply_operations *ops,
1042 wimlib_progress_func_t progress_func)
1044 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
1045 uint64_t next_progress = bytes_per_progress;
1046 struct wim_lookup_table_entry *lte;
1047 struct wim_inode *inode;
1048 struct wim_dentry *dentry;
1051 /* This complicated loop is essentially looping through the dentries,
1052 * although dentries may be visited more than once (if a dentry contains
1053 * two different nonempty streams) or not at all (if a dentry contains
1054 * no non-empty streams).
1056 * The outer loop is over the distinct streams to be extracted so that
1057 * sequential reading of the WIM can be implemented. */
1059 /* For each distinct stream to be extracted */
1060 list_for_each_entry(lte, stream_list, staging_list) {
1061 /* For each inode that contains the stream */
1062 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
1063 /* For each dentry that points to the inode */
1064 inode_for_each_dentry(dentry, inode) {
1065 /* Extract the dentry if it was not already
1067 ret = maybe_apply_dentry(dentry, args);
1070 if (progress_func &&
1071 args->progress.extract.completed_bytes >= next_progress)
1073 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
1075 if (args->progress.extract.completed_bytes >=
1076 args->progress.extract.total_bytes)
1078 next_progress = ~0ULL;
1081 min (args->progress.extract.completed_bytes +
1083 args->progress.extract.total_bytes);
1092 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
1094 static int extract_single_image(WIMStruct *w, int image,
1095 const char *target, int extract_flags,
1096 wimlib_progress_func_t progress_func)
1099 struct list_head stream_list;
1100 struct hlist_head *inode_list;
1102 struct apply_args args;
1103 const struct apply_operations *ops;
1106 args.target = target;
1107 args.extract_flags = extract_flags;
1108 args.num_lutimes_warnings = 0;
1109 args.stream_list = &stream_list;
1110 args.progress_func = progress_func;
1112 if (progress_func) {
1113 args.progress.extract.wimfile_name = w->filename;
1114 args.progress.extract.image = image;
1115 args.progress.extract.extract_flags = (extract_flags &
1116 WIMLIB_EXTRACT_MASK_PUBLIC);
1117 args.progress.extract.image_name = wimlib_get_image_name(w, image);
1118 args.progress.extract.target = target;
1122 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1123 args.vol = ntfs_mount(target, 0);
1125 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
1126 return WIMLIB_ERR_NTFS_3G;
1128 ops = &ntfs_apply_operations;
1131 ops = &normal_apply_operations;
1133 ret = select_wim_image(w, image);
1137 inode_list = &w->image_metadata[image - 1].inode_list;
1139 /* Build a list of the streams that need to be extracted */
1140 find_streams_for_extraction(inode_list, &stream_list,
1141 w->lookup_table, extract_flags);
1143 /* Calculate the number of bytes of data that will be extracted */
1144 calculate_bytes_to_extract(&stream_list, extract_flags,
1147 if (progress_func) {
1148 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
1152 /* If a sequential extraction was specified, sort the streams to be
1153 * extracted by their position in the WIM file, so that the WIM file can
1154 * be read sequentially. */
1155 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
1156 ret = sort_stream_list_by_wim_position(&stream_list);
1158 WARNING("Falling back to non-sequential extraction");
1159 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1163 if (progress_func) {
1164 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
1168 /* Make the directory structure and extract empty files */
1169 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1170 args.apply_dentry = ops->apply_dentry;
1171 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
1172 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1176 if (progress_func) {
1177 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
1181 /* Extract non-empty files */
1182 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
1186 if (progress_func) {
1187 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
1191 /* Apply timestamps */
1192 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
1193 ops->apply_dentry_timestamps, &args);
1197 if (progress_func) {
1198 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
1203 /* Unmount the NTFS volume */
1204 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1205 if (ntfs_umount(args.vol, FALSE) != 0) {
1206 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
1208 ret = WIMLIB_ERR_NTFS_3G;
1216 /* Extracts all images from the WIM to the directory @target, with the images
1217 * placed in subdirectories named by their image names. */
1218 static int extract_all_images(WIMStruct *w, const char *target,
1220 wimlib_progress_func_t progress_func)
1222 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
1223 size_t output_path_len = strlen(target);
1224 char buf[output_path_len + 1 + image_name_max_len + 1];
1227 const char *image_name;
1229 ret = extract_directory(NULL, target, true);
1233 memcpy(buf, target, output_path_len);
1234 buf[output_path_len] = '/';
1235 for (image = 1; image <= w->hdr.image_count; image++) {
1236 image_name = wimlib_get_image_name(w, image);
1237 if (image_name && *image_name) {
1238 strcpy(buf + output_path_len + 1, image_name);
1240 /* Image name is empty. Use image number instead */
1241 sprintf(buf + output_path_len + 1, "%d", image);
1243 ret = extract_single_image(w, image, buf, extract_flags,
1251 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1253 WIMLIBAPI int wimlib_extract_image(WIMStruct *w,
1257 WIMStruct **additional_swms,
1258 unsigned num_additional_swms,
1259 wimlib_progress_func_t progress_func)
1261 struct wim_lookup_table *joined_tab, *w_tab_save;
1265 return WIMLIB_ERR_INVALID_PARAM;
1267 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1269 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1270 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1271 return WIMLIB_ERR_INVALID_PARAM;
1273 #if defined(__CYGWIN__) || defined(__WIN32__)
1274 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1275 ERROR("Extracting UNIX data is not supported on Windows");
1276 return WIMLIB_ERR_INVALID_PARAM;
1278 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1279 ERROR("Linked extraction modes are not supported on Windows");
1280 return WIMLIB_ERR_INVALID_PARAM;
1284 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1286 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1287 ERROR("Cannot specify symlink or hardlink flags when applying\n"
1288 " directly to a NTFS volume");
1289 return WIMLIB_ERR_INVALID_PARAM;
1291 if (image == WIMLIB_ALL_IMAGES) {
1292 ERROR("Can only apply a single image when applying "
1293 "directly to a NTFS volume");
1294 return WIMLIB_ERR_INVALID_PARAM;
1296 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1297 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
1298 return WIMLIB_ERR_INVALID_PARAM;
1301 ERROR("wimlib was compiled without support for NTFS-3g, so");
1302 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1303 return WIMLIB_ERR_UNSUPPORTED;
1307 ret = verify_swm_set(w, additional_swms, num_additional_swms);
1311 if (num_additional_swms) {
1312 ret = new_joined_lookup_table(w, additional_swms,
1313 num_additional_swms, &joined_tab);
1316 w_tab_save = w->lookup_table;
1317 w->lookup_table = joined_tab;
1320 if (image == WIMLIB_ALL_IMAGES) {
1321 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1322 ret = extract_all_images(w, target, extract_flags,
1325 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1326 ret = extract_single_image(w, image, target, extract_flags,
1330 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1331 WIMLIB_EXTRACT_FLAG_HARDLINK))
1333 for_lookup_table_entry(w->lookup_table,
1334 lte_free_extracted_file,
1338 if (num_additional_swms) {
1339 free_lookup_table(w->lookup_table);
1340 w->lookup_table = w_tab_save;