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(__WIN32__)
65 # define swprintf _snwprintf
66 # define mkdir(path, mode) (!CreateDirectoryA(path, NULL))
69 #if defined(__CYGWIN__) || defined(__WIN32__)
71 static int win32_set_reparse_data(HANDLE h,
73 const struct wim_lookup_table_entry *lte,
81 WARNING("\"%ls\" is marked as a reparse point but had no reparse data",
85 len = wim_resource_size(lte);
86 if (len > 16 * 1024 - 8) {
87 WARNING("\"%ls\": reparse data too long!", path);
91 /* The WIM stream omits the ReparseTag and ReparseDataLength fields, so
92 * leave 8 bytes of space for them at the beginning of the buffer, then
93 * set them manually. */
94 buf = alloca(len + 8);
95 ret = read_full_wim_resource(lte, buf + 8, 0);
98 *(u32*)(buf + 0) = reparse_tag;
99 *(u16*)(buf + 4) = len;
100 *(u16*)(buf + 6) = 0;
102 /* Set the reparse data on the open file using the
103 * FSCTL_SET_REPARSE_POINT ioctl.
105 * There are contradictions in Microsoft's documentation for this:
107 * "If hDevice was opened without specifying FILE_FLAG_OVERLAPPED,
108 * lpOverlapped is ignored."
110 * --- So setting lpOverlapped to NULL is okay since it's ignored.
112 * "If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an
113 * operation returns no output data and lpOutBuffer is NULL,
114 * DeviceIoControl makes use of lpBytesReturned. After such an
115 * operation, the value of lpBytesReturned is meaningless."
117 * --- So lpOverlapped not really ignored, as it affects another
118 * parameter. This is the actual behavior: lpBytesReturned must be
119 * specified, even though lpBytesReturned is documented as:
121 * "Not used with this operation; set to NULL."
124 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, buf, len + 8,
126 &bytesReturned /* lpBytesReturned */,
127 NULL /* lpOverlapped */))
129 DWORD err = GetLastError();
130 ERROR("Failed to set reparse data on \"%ls\"", path);
132 return WIMLIB_ERR_WRITE;
138 static int win32_extract_chunk(const u8 *buf, size_t len, u64 offset, void *arg)
140 HANDLE hStream = arg;
142 DWORD nbytes_written;
143 wimlib_assert(len <= 0xffffffff);
145 if (!WriteFile(hStream, buf, len, &nbytes_written, NULL) ||
146 nbytes_written != len)
148 DWORD err = GetLastError();
149 ERROR("WriteFile(): write error");
151 return WIMLIB_ERR_WRITE;
156 static int do_win32_extract_stream(HANDLE hStream, struct wim_lookup_table_entry *lte)
158 return extract_wim_resource(lte, wim_resource_size(lte),
159 win32_extract_chunk, hStream);
162 static int win32_extract_stream(const struct wim_inode *inode,
164 const wchar_t *stream_name_utf16,
165 struct wim_lookup_table_entry *lte)
167 wchar_t *stream_path;
171 DWORD creationDisposition = CREATE_ALWAYS;
173 if (stream_name_utf16) {
174 /* Named stream. Create a buffer that contains the UTF-16LE
175 * string [./]@path:@stream_name_utf16. This is needed to
176 * create and open the stream using CreateFileW(). I'm not
177 * aware of any other APIs to do this. Note: the '$DATA' suffix
178 * seems to be unneeded. Additional note: a "./" prefix needs
179 * to be added when the path is not absolute to avoid ambiguity
180 * with drive letters. */
181 size_t stream_path_nchars;
183 size_t stream_name_nchars;
184 const wchar_t *prefix;
186 path_nchars = wcslen(path);
187 stream_name_nchars = wcslen(stream_name_utf16);
188 stream_path_nchars = path_nchars + 1 + stream_name_nchars;
189 if (path[0] != L'/' && path[0] != L'\\') {
191 stream_path_nchars += 2;
195 stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
196 swprintf(stream_path, stream_path_nchars + 1, L"%ls%ls:%ls",
197 prefix, path, stream_name_utf16);
199 /* Unnamed stream; its path is just the path to the file itself.
201 stream_path = (wchar_t*)path;
203 /* Directories must be created with CreateDirectoryW(). Then
204 * the call to CreateFileW() will merely open the directory that
205 * was already created rather than creating a new file. */
206 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
207 if (!CreateDirectoryW(stream_path, NULL)) {
208 err = GetLastError();
209 if (err != ERROR_ALREADY_EXISTS) {
210 ERROR("Failed to create directory \"%ls\"",
213 ret = WIMLIB_ERR_MKDIR;
217 DEBUG("Created directory \"%ls\"", stream_path);
218 if (!(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
222 creationDisposition = OPEN_EXISTING;
226 DEBUG("Opening \"%ls\"", stream_path);
227 h = CreateFileW(stream_path,
228 GENERIC_WRITE | WRITE_OWNER | WRITE_DAC | ACCESS_SYSTEM_SECURITY,
232 FILE_FLAG_OPEN_REPARSE_POINT |
233 FILE_FLAG_BACKUP_SEMANTICS |
236 if (h == INVALID_HANDLE_VALUE) {
237 err = GetLastError();
238 ERROR("Failed to create \"%ls\"", stream_path);
240 ret = WIMLIB_ERR_OPEN;
244 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
245 stream_name_utf16 == NULL)
247 DEBUG("Setting reparse data on \"%ls\"", path);
248 ret = win32_set_reparse_data(h, inode->i_reparse_tag, lte, path);
250 goto fail_close_handle;
253 DEBUG("Extracting \"%ls\" (len = %"PRIu64")",
254 stream_path, wim_resource_size(lte));
255 ret = do_win32_extract_stream(h, lte);
257 goto fail_close_handle;
261 DEBUG("Closing \"%ls\"", stream_path);
262 if (!CloseHandle(h)) {
263 err = GetLastError();
264 ERROR("Failed to close \"%ls\"", stream_path);
266 ret = WIMLIB_ERR_WRITE;
274 ERROR("Error extracting %ls", stream_path);
280 * Creates a file, directory, or reparse point and extracts all streams to it
281 * (unnamed data stream and/or reparse point stream, plus any alternate data
282 * streams). This in Win32-specific code.
284 * @inode: WIM inode for this file or directory.
285 * @path: UTF-16LE external path to extract the inode to.
287 * Returns 0 on success; nonzero on failure.
289 static int win32_extract_streams(struct wim_inode *inode,
290 const wchar_t *path, u64 *completed_bytes_p)
292 struct wim_lookup_table_entry *unnamed_lte;
295 unnamed_lte = inode_unnamed_lte_resolved(inode);
296 ret = win32_extract_stream(inode, path, NULL, unnamed_lte);
300 *completed_bytes_p += wim_resource_size(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,
318 *completed_bytes_p += wim_resource_size(ads_entry->lte);
326 * Sets the security descriptor on an extracted file. This is Win32-specific
329 * @inode: The WIM inode that was extracted and has a security descriptor.
330 * @path: UTF-16LE external path that the inode was extracted to.
331 * @sd: Security data for the WIM image.
333 * Returns 0 on success; nonzero on failure.
335 static int win32_set_security_data(const struct wim_inode *inode,
337 const struct wim_security_data *sd)
339 SECURITY_INFORMATION securityInformation = DACL_SECURITY_INFORMATION |
340 SACL_SECURITY_INFORMATION |
341 OWNER_SECURITY_INFORMATION |
342 GROUP_SECURITY_INFORMATION;
343 if (!SetFileSecurityW(path, securityInformation,
344 (PSECURITY_DESCRIPTOR)sd->descriptors[inode->i_security_id]))
346 DWORD err = GetLastError();
347 ERROR("Can't set security descriptor on \"%ls\"", path);
349 return WIMLIB_ERR_WRITE;
354 #else /* __CYGWIN__ || __WIN32__ */
355 static int extract_regular_file_linked(struct wim_dentry *dentry,
356 const char *output_path,
357 struct apply_args *args,
358 struct wim_lookup_table_entry *lte)
360 /* This mode overrides the normal hard-link extraction and
361 * instead either symlinks or hardlinks *all* identical files in
362 * the WIM, even if they are in a different image (in the case
363 * of a multi-image extraction) */
365 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
366 if (link(lte->extracted_file, output_path) != 0) {
367 ERROR_WITH_ERRNO("Failed to hard link "
369 output_path, lte->extracted_file);
370 return WIMLIB_ERR_LINK;
373 int num_path_components;
374 int num_output_dir_path_components;
375 size_t extracted_file_len;
380 num_path_components =
381 get_num_path_components(dentry->full_path_utf8) - 1;
382 num_output_dir_path_components =
383 get_num_path_components(args->target);
385 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
386 num_path_components++;
387 num_output_dir_path_components--;
389 extracted_file_len = strlen(lte->extracted_file);
391 char buf[extracted_file_len + 3 * num_path_components + 1];
394 for (i = 0; i < num_path_components; i++) {
399 p2 = lte->extracted_file;
402 while (num_output_dir_path_components--)
403 p2 = path_next_part(p2, NULL);
405 if (symlink(buf, output_path) != 0) {
406 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
408 buf, lte->extracted_file);
409 return WIMLIB_ERR_LINK;
415 static int symlink_apply_unix_data(const char *link,
416 const struct wimlib_unix_data *unix_data)
418 if (lchown(link, unix_data->uid, unix_data->gid)) {
419 if (errno == EPERM) {
421 WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
423 ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
424 return WIMLIB_ERR_INVALID_DENTRY;
430 static int fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
432 if (fchown(fd, unix_data->uid, unix_data->gid)) {
433 if (errno == EPERM) {
434 WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
437 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
438 return WIMLIB_ERR_INVALID_DENTRY;
442 if (fchmod(fd, unix_data->mode)) {
443 if (errno == EPERM) {
444 WARNING_WITH_ERRNO("failed to set UNIX file mode");
447 ERROR_WITH_ERRNO("failed to set UNIX file mode");
448 return WIMLIB_ERR_INVALID_DENTRY;
454 static int dir_apply_unix_data(const char *dir,
455 const struct wimlib_unix_data *unix_data)
457 int dfd = open(dir, O_RDONLY);
460 ret = fd_apply_unix_data(dfd, unix_data);
462 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
463 ret = WIMLIB_ERR_MKDIR;
466 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
467 ret = WIMLIB_ERR_MKDIR;
472 static int extract_regular_file_unlinked(struct wim_dentry *dentry,
473 struct apply_args *args,
474 const char *output_path,
475 struct wim_lookup_table_entry *lte)
477 /* Normal mode of extraction. Regular files and hard links are
478 * extracted in the way that they appear in the WIM. */
482 struct wim_inode *inode = dentry->d_inode;
484 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
485 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
486 WIMLIB_EXTRACT_FLAG_HARDLINK))))
488 /* If the dentry is part of a hard link set of at least 2
489 * dentries and one of the other dentries has already been
490 * extracted, make a hard link to the file corresponding to this
491 * already-extracted directory. Otherwise, extract the file and
492 * set the inode->i_extracted_file field so that other dentries
493 * in the hard link group can link to it. */
494 if (inode->i_nlink > 1) {
495 if (inode->i_extracted_file) {
496 DEBUG("Extracting hard link `%s' => `%s'",
497 output_path, inode->i_extracted_file);
498 if (link(inode->i_extracted_file, output_path) != 0) {
499 ERROR_WITH_ERRNO("Failed to hard link "
502 inode->i_extracted_file);
503 return WIMLIB_ERR_LINK;
507 FREE(inode->i_extracted_file);
508 inode->i_extracted_file = STRDUP(output_path);
509 if (!inode->i_extracted_file) {
510 ERROR("Failed to allocate memory for filename");
511 return WIMLIB_ERR_NOMEM;
516 /* Extract the contents of the file to @output_path. */
518 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
520 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
522 return WIMLIB_ERR_OPEN;
526 /* Empty file with no lookup table entry */
527 DEBUG("Empty file `%s'.", output_path);
529 goto out_extract_unix_data;
532 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
534 ERROR("Failed to extract resource to `%s'", output_path);
538 out_extract_unix_data:
539 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
540 struct wimlib_unix_data unix_data;
541 ret = inode_get_unix_data(inode, &unix_data, NULL);
547 ret = fd_apply_unix_data(out_fd, &unix_data);
552 args->progress.extract.completed_bytes += wim_resource_size(lte);
554 if (close(out_fd) != 0) {
555 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
557 ret = WIMLIB_ERR_WRITE;
562 static int extract_regular_file(struct wim_dentry *dentry,
563 struct apply_args *args,
564 const char *output_path)
566 struct wim_lookup_table_entry *lte;
567 const struct wim_inode *inode = dentry->d_inode;
569 lte = inode_unnamed_lte_resolved(inode);
571 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
572 WIMLIB_EXTRACT_FLAG_HARDLINK)))
574 if (lte->extracted_file) {
575 return extract_regular_file_linked(dentry, output_path, args, lte);
577 lte->extracted_file = STRDUP(output_path);
578 if (!lte->extracted_file)
579 return WIMLIB_ERR_NOMEM;
582 return extract_regular_file_unlinked(dentry, args, output_path, lte);
585 static int extract_symlink(struct wim_dentry *dentry,
586 struct apply_args *args,
587 const char *output_path)
590 ssize_t ret = inode_readlink(dentry->d_inode, target,
591 sizeof(target), args->w, 0);
592 struct wim_lookup_table_entry *lte;
595 ERROR("Could not read the symbolic link from dentry `%s'",
596 dentry->full_path_utf8);
597 return WIMLIB_ERR_INVALID_DENTRY;
599 ret = symlink(target, output_path);
601 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
602 output_path, target);
603 return WIMLIB_ERR_LINK;
605 lte = inode_unnamed_lte_resolved(dentry->d_inode);
606 wimlib_assert(lte != NULL);
607 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
608 struct wimlib_unix_data unix_data;
609 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
615 ret = symlink_apply_unix_data(output_path, &unix_data);
619 args->progress.extract.completed_bytes += wim_resource_size(lte);
623 #endif /* !(__CYGWIN__ || __WIN32__) */
625 static int extract_directory(struct wim_dentry *dentry,
626 const char *output_path, bool is_root)
631 ret = stat(output_path, &stbuf);
633 if (S_ISDIR(stbuf.st_mode)) {
635 /*WARNING("`%s' already exists", output_path);*/
638 ERROR("`%s' is not a directory", output_path);
639 return WIMLIB_ERR_MKDIR;
642 if (errno != ENOENT) {
643 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
644 return WIMLIB_ERR_STAT;
647 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
648 S_IROTH | S_IXOTH) != 0) {
649 ERROR_WITH_ERRNO("Cannot create directory `%s'",
651 return WIMLIB_ERR_MKDIR;
655 #if !defined(__CYGWIN__) && !defined(__WIN32__)
657 struct wimlib_unix_data unix_data;
658 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
664 ret = dir_apply_unix_data(output_path, &unix_data);
670 /* Extracts a file, directory, or symbolic link from the WIM archive. */
671 static int apply_dentry_normal(struct wim_dentry *dentry, void *arg)
673 struct apply_args *args = arg;
674 struct wim_inode *inode = dentry->d_inode;
678 len = strlen(args->target);
679 if (dentry_is_root(dentry)) {
680 output_path = (char*)args->target;
682 output_path = alloca(len + dentry->full_path_utf8_len + 1);
683 memcpy(output_path, args->target, len);
684 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
685 output_path[len + dentry->full_path_utf8_len] = '\0';
686 len += dentry->full_path_utf8_len;
689 #if defined(__CYGWIN__) || defined(__WIN32__)
691 size_t utf16_path_len;
694 ret = utf8_to_utf16(output_path, len, &utf16_path, &utf16_path_len);
698 if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
699 /* Linked file, with another name already extracted. Create a
701 DEBUG("Creating hard link \"%ls => %ls\"",
702 (const wchar_t*)utf16_path,
703 (const wchar_t*)inode->i_extracted_file);
704 if (!CreateHardLinkW((const wchar_t*)utf16_path,
705 (const wchar_t*)inode->i_extracted_file,
708 err = GetLastError();
709 ERROR("Can't create hard link \"%ls => %ls\"",
710 (const wchar_t*)utf16_path,
711 (const wchar_t*)inode->i_extracted_file);
712 ret = WIMLIB_ERR_LINK;
716 /* Create the file, directory, or reparse point, and extract the
718 ret = win32_extract_streams(inode, (const wchar_t*)utf16_path,
719 &args->progress.extract.completed_bytes);
721 goto out_free_utf16_path;
723 /* Set security descriptor if present */
724 if (inode->i_security_id != -1) {
725 DEBUG("Setting security descriptor %d on %s",
726 inode->i_security_id, output_path);
727 ret = win32_set_security_data(inode,
728 (const wchar_t*)utf16_path,
729 wim_const_security_data(args->w));
731 goto out_free_utf16_path;
733 if (inode->i_nlink > 1) {
734 /* Save extracted path for a later call to
735 * CreateHardLinkW() if this inode has multiple links.
737 inode->i_extracted_file = utf16_path;
746 if (inode_is_symlink(inode))
747 return extract_symlink(dentry, args, output_path);
748 else if (inode_is_directory(inode))
749 return extract_directory((args->extract_flags &
750 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
753 return extract_regular_file(dentry, args, output_path);
757 /* Apply timestamps to an extracted file or directory */
758 static int apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
760 struct apply_args *args = arg;
764 const struct wim_inode *inode = dentry->d_inode;
766 len = strlen(args->target);
767 if (dentry_is_root(dentry)) {
768 output_path = (char*)args->target;
770 output_path = alloca(len + dentry->full_path_utf8_len + 1);
771 memcpy(output_path, args->target, len);
772 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
773 output_path[len + dentry->full_path_utf8_len] = '\0';
774 len += dentry->full_path_utf8_len;
777 #if defined(__CYGWIN__) || defined(__WIN32__)
780 size_t utf16_path_len;
784 ret = utf8_to_utf16(output_path, len, &utf16_path, &utf16_path_len);
788 DEBUG("Opening \"%s\" to set timestamps", output_path);
789 h = CreateFileW((const wchar_t*)utf16_path,
790 GENERIC_WRITE | WRITE_OWNER | WRITE_DAC | ACCESS_SYSTEM_SECURITY,
794 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
797 if (h == INVALID_HANDLE_VALUE)
798 err = GetLastError();
800 if (h == INVALID_HANDLE_VALUE)
803 FILETIME creationTime = {.dwLowDateTime = inode->i_creation_time & 0xffffffff,
804 .dwHighDateTime = inode->i_creation_time >> 32};
805 FILETIME lastAccessTime = {.dwLowDateTime = inode->i_last_access_time & 0xffffffff,
806 .dwHighDateTime = inode->i_last_access_time >> 32};
807 FILETIME lastWriteTime = {.dwLowDateTime = inode->i_last_write_time & 0xffffffff,
808 .dwHighDateTime = inode->i_last_write_time >> 32};
810 DEBUG("Calling SetFileTime() on \"%s\"", output_path);
811 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
812 err = GetLastError();
816 DEBUG("Closing \"%s\"", output_path);
817 if (!CloseHandle(h)) {
818 err = GetLastError();
823 /* Only warn if setting timestamps failed. */
824 WARNING("Can't set timestamps on \"%s\"", output_path);
831 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
832 * into struct timeval's. */
833 struct timeval tv[2];
834 wim_timestamp_to_timeval(inode->i_last_access_time, &tv[0]);
835 wim_timestamp_to_timeval(inode->i_last_write_time, &tv[1]);
837 ret = lutimes(output_path, tv);
844 if (errno == ENOSYS) {
846 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
847 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
848 if (utime(output_path, &buf) == 0)
852 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
853 /*WARNING_WITH_ERRNO("Failed to set timestamp on file `%s',*/
855 args->num_lutimes_warnings++;
862 /* Extract a dentry if it hasn't already been extracted, and either the dentry
863 * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
864 static int maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
866 struct apply_args *args = arg;
869 if (dentry->is_extracted)
872 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
873 if (inode_unnamed_lte_resolved(dentry->d_inode))
876 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
877 args->progress_func) {
878 args->progress.extract.cur_path = dentry->full_path_utf8;
879 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
882 ret = args->apply_dentry(dentry, args);
884 dentry->is_extracted = 1;
888 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
890 const struct wim_lookup_table_entry *lte1, *lte2;
891 lte1 = *(const struct wim_lookup_table_entry**)p1;
892 lte2 = *(const struct wim_lookup_table_entry**)p2;
893 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
895 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
901 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
903 struct list_head *cur;
905 struct wim_lookup_table_entry **array;
910 list_for_each(cur, stream_list)
912 array_size = num_streams * sizeof(array[0]);
913 array = MALLOC(array_size);
915 ERROR("Failed to allocate %zu bytes to sort stream entries",
917 return WIMLIB_ERR_NOMEM;
919 cur = stream_list->next;
920 for (i = 0; i < num_streams; i++) {
921 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
925 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
927 INIT_LIST_HEAD(stream_list);
928 for (i = 0; i < num_streams; i++)
929 list_add_tail(&array[i]->staging_list, stream_list);
934 static void calculate_bytes_to_extract(struct list_head *stream_list,
936 union wimlib_progress_info *progress)
938 struct wim_lookup_table_entry *lte;
942 /* For each stream to be extracted... */
943 list_for_each_entry(lte, stream_list, staging_list) {
945 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
947 /* In the symlink or hard link extraction mode, each
948 * stream will be extracted one time regardless of how
949 * many dentries share the stream. */
950 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
951 if (!lte->extracted_file) {
953 total_bytes += wim_resource_size(lte);
956 num_streams += lte->out_refcnt;
957 total_bytes += lte->out_refcnt * wim_resource_size(lte);
960 progress->extract.num_streams = num_streams;
961 progress->extract.total_bytes = total_bytes;
962 progress->extract.completed_bytes = 0;
965 static void maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
966 struct list_head *stream_list)
968 if (++lte->out_refcnt == 1) {
969 INIT_LIST_HEAD(<e->inode_list);
970 list_add_tail(<e->staging_list, stream_list);
974 static void inode_find_streams_for_extraction(struct wim_inode *inode,
975 struct list_head *stream_list,
978 struct wim_lookup_table_entry *lte;
979 bool inode_added = false;
981 lte = inode_unnamed_lte_resolved(inode);
983 maybe_add_stream_for_extraction(lte, stream_list);
984 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
988 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
989 for (unsigned i = 0; i < inode->i_num_ads; i++) {
990 if (inode->i_ads_entries[i].stream_name_len != 0) {
991 lte = inode->i_ads_entries[i].lte;
993 maybe_add_stream_for_extraction(lte,
996 list_add_tail(&inode->i_lte_inode_list,
1007 static void find_streams_for_extraction(struct hlist_head *inode_list,
1008 struct list_head *stream_list,
1009 struct wim_lookup_table *lookup_table,
1012 struct wim_inode *inode;
1013 struct hlist_node *cur;
1014 struct wim_dentry *dentry;
1016 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
1017 INIT_LIST_HEAD(stream_list);
1018 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
1019 if (!inode->i_resolved)
1020 inode_resolve_ltes(inode, lookup_table);
1021 inode_for_each_dentry(dentry, inode)
1022 dentry->is_extracted = 0;
1023 inode_find_streams_for_extraction(inode, stream_list,
1028 struct apply_operations {
1029 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
1030 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
1033 static const struct apply_operations normal_apply_operations = {
1034 .apply_dentry = apply_dentry_normal,
1035 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
1039 static const struct apply_operations ntfs_apply_operations = {
1040 .apply_dentry = apply_dentry_ntfs,
1041 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
1045 static int apply_stream_list(struct list_head *stream_list,
1046 struct apply_args *args,
1047 const struct apply_operations *ops,
1048 wimlib_progress_func_t progress_func)
1050 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
1051 uint64_t next_progress = bytes_per_progress;
1052 struct wim_lookup_table_entry *lte;
1053 struct wim_inode *inode;
1054 struct wim_dentry *dentry;
1057 /* This complicated loop is essentially looping through the dentries,
1058 * although dentries may be visited more than once (if a dentry contains
1059 * two different nonempty streams) or not at all (if a dentry contains
1060 * no non-empty streams).
1062 * The outer loop is over the distinct streams to be extracted so that
1063 * sequential reading of the WIM can be implemented. */
1065 /* For each distinct stream to be extracted */
1066 list_for_each_entry(lte, stream_list, staging_list) {
1067 /* For each inode that contains the stream */
1068 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
1069 /* For each dentry that points to the inode */
1070 inode_for_each_dentry(dentry, inode) {
1071 /* Extract the dentry if it was not already
1073 ret = maybe_apply_dentry(dentry, args);
1076 if (progress_func &&
1077 args->progress.extract.completed_bytes >= next_progress)
1079 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
1081 if (args->progress.extract.completed_bytes >=
1082 args->progress.extract.total_bytes)
1084 next_progress = ~0ULL;
1087 min (args->progress.extract.completed_bytes +
1089 args->progress.extract.total_bytes);
1098 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
1100 static int extract_single_image(WIMStruct *w, int image,
1101 const char *target, int extract_flags,
1102 wimlib_progress_func_t progress_func)
1105 struct list_head stream_list;
1106 struct hlist_head *inode_list;
1108 struct apply_args args;
1109 const struct apply_operations *ops;
1112 args.target = target;
1113 args.extract_flags = extract_flags;
1114 args.num_lutimes_warnings = 0;
1115 args.stream_list = &stream_list;
1116 args.progress_func = progress_func;
1118 if (progress_func) {
1119 args.progress.extract.wimfile_name = w->filename;
1120 args.progress.extract.image = image;
1121 args.progress.extract.extract_flags = (extract_flags &
1122 WIMLIB_EXTRACT_MASK_PUBLIC);
1123 args.progress.extract.image_name = wimlib_get_image_name(w, image);
1124 args.progress.extract.target = target;
1128 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1129 args.vol = ntfs_mount(target, 0);
1131 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
1132 return WIMLIB_ERR_NTFS_3G;
1134 ops = &ntfs_apply_operations;
1137 ops = &normal_apply_operations;
1139 ret = select_wim_image(w, image);
1143 inode_list = &w->image_metadata[image - 1].inode_list;
1145 /* Build a list of the streams that need to be extracted */
1146 find_streams_for_extraction(inode_list, &stream_list,
1147 w->lookup_table, extract_flags);
1149 /* Calculate the number of bytes of data that will be extracted */
1150 calculate_bytes_to_extract(&stream_list, extract_flags,
1153 if (progress_func) {
1154 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
1158 /* If a sequential extraction was specified, sort the streams to be
1159 * extracted by their position in the WIM file, so that the WIM file can
1160 * be read sequentially. */
1161 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
1162 ret = sort_stream_list_by_wim_position(&stream_list);
1164 WARNING("Falling back to non-sequential extraction");
1165 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1169 if (progress_func) {
1170 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
1174 /* Make the directory structure and extract empty files */
1175 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1176 args.apply_dentry = ops->apply_dentry;
1177 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
1178 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
1182 if (progress_func) {
1183 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
1187 /* Extract non-empty files */
1188 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
1192 if (progress_func) {
1193 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
1197 /* Apply timestamps */
1198 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
1199 ops->apply_dentry_timestamps, &args);
1203 if (progress_func) {
1204 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
1209 /* Unmount the NTFS volume */
1210 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1211 if (ntfs_umount(args.vol, FALSE) != 0) {
1212 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
1214 ret = WIMLIB_ERR_NTFS_3G;
1222 /* Extracts all images from the WIM to the directory @target, with the images
1223 * placed in subdirectories named by their image names. */
1224 static int extract_all_images(WIMStruct *w, const char *target,
1226 wimlib_progress_func_t progress_func)
1228 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
1229 size_t output_path_len = strlen(target);
1230 char buf[output_path_len + 1 + image_name_max_len + 1];
1233 const char *image_name;
1235 ret = extract_directory(NULL, target, true);
1239 memcpy(buf, target, output_path_len);
1240 buf[output_path_len] = '/';
1241 for (image = 1; image <= w->hdr.image_count; image++) {
1242 image_name = wimlib_get_image_name(w, image);
1243 if (image_name && *image_name) {
1244 strcpy(buf + output_path_len + 1, image_name);
1246 /* Image name is empty. Use image number instead */
1247 sprintf(buf + output_path_len + 1, "%d", image);
1249 ret = extract_single_image(w, image, buf, extract_flags,
1257 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1259 WIMLIBAPI int wimlib_extract_image(WIMStruct *w,
1263 WIMStruct **additional_swms,
1264 unsigned num_additional_swms,
1265 wimlib_progress_func_t progress_func)
1267 struct wim_lookup_table *joined_tab, *w_tab_save;
1271 return WIMLIB_ERR_INVALID_PARAM;
1273 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1275 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1276 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1277 return WIMLIB_ERR_INVALID_PARAM;
1279 #if defined(__CYGWIN__) || defined(__WIN32__)
1280 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1281 ERROR("Extracting UNIX data is not supported on Windows");
1282 return WIMLIB_ERR_INVALID_PARAM;
1284 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1285 ERROR("Linked extraction modes are not supported on Windows");
1286 return WIMLIB_ERR_INVALID_PARAM;
1290 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1292 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1293 ERROR("Cannot specify symlink or hardlink flags when applying\n"
1294 " directly to a NTFS volume");
1295 return WIMLIB_ERR_INVALID_PARAM;
1297 if (image == WIMLIB_ALL_IMAGES) {
1298 ERROR("Can only apply a single image when applying "
1299 "directly to a NTFS volume");
1300 return WIMLIB_ERR_INVALID_PARAM;
1302 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1303 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
1304 return WIMLIB_ERR_INVALID_PARAM;
1307 ERROR("wimlib was compiled without support for NTFS-3g, so");
1308 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1309 return WIMLIB_ERR_UNSUPPORTED;
1313 ret = verify_swm_set(w, additional_swms, num_additional_swms);
1317 if (num_additional_swms) {
1318 ret = new_joined_lookup_table(w, additional_swms,
1319 num_additional_swms, &joined_tab);
1322 w_tab_save = w->lookup_table;
1323 w->lookup_table = joined_tab;
1326 #if defined(__CYGWIN__) || defined(__WIN32__)
1327 win32_acquire_privilege(SE_RESTORE_NAME);
1328 win32_acquire_privilege(SE_SECURITY_NAME);
1329 win32_acquire_privilege(SE_TAKE_OWNERSHIP_NAME);
1331 if (image == WIMLIB_ALL_IMAGES) {
1332 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1333 ret = extract_all_images(w, target, extract_flags,
1336 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1337 ret = extract_single_image(w, image, target, extract_flags,
1340 #if defined(__CYGWIN__) || defined(__WIN32__)
1341 win32_release_privilege(SE_RESTORE_NAME);
1342 win32_release_privilege(SE_SECURITY_NAME);
1343 win32_release_privilege(SE_TAKE_OWNERSHIP_NAME);
1346 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1347 WIMLIB_EXTRACT_FLAG_HARDLINK))
1349 for_lookup_table_entry(w->lookup_table,
1350 lte_free_extracted_file,
1354 if (num_additional_swms) {
1355 free_lookup_table(w->lookup_table);
1356 w->lookup_table = w_tab_save;