+ if (progress_func) {
+ args.progress.extract.wimfile_name = wim->filename;
+ args.progress.extract.image = wim->current_image;
+ args.progress.extract.extract_flags = (extract_flags &
+ WIMLIB_EXTRACT_MASK_PUBLIC);
+ args.progress.extract.image_name = wimlib_get_image_name(wim,
+ wim->current_image);
+ args.progress.extract.extract_root_wim_source_path = wim_source_path;
+ args.progress.extract.target = target;
+ }
+
+#ifdef WITH_NTFS_3G
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
+ args.vol = ntfs_mount(target, 0);
+ if (!args.vol) {
+ ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
+ target);
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out;
+ }
+ ops = &ntfs_apply_operations;
+ } else
+#endif
+ ops = &normal_apply_operations;
+
+ root = get_dentry(wim, wim_source_path);
+ if (!root) {
+ ERROR("Path \"%"TS"\" does not exist in WIM image %d",
+ wim_source_path, wim->current_image);
+ ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
+ goto out_ntfs_umount;
+ }
+ args.extract_root = root;
+
+ /* Calculate the actual filename component of each extracted dentry, and
+ * in the process set the dentry->needs_extraction flag on dentries that
+ * will be extracted. */
+ ret = for_dentry_in_tree(root, dentry_calculate_extraction_path, &args);
+ if (ret)
+ goto out_dentry_reset_needs_extraction;
+
+ /* Build a list of the streams that need to be extracted */
+ find_streams_for_extraction(root,
+ &stream_list,
+ wim->lookup_table, extract_flags);
+
+ /* Calculate the number of bytes of data that will be extracted */
+ calculate_bytes_to_extract(&stream_list, extract_flags,
+ &args.progress);
+
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
+ ret = extract_dentry_to_stdout(root);
+ goto out_dentry_reset_needs_extraction;
+ }
+
+ if (progress_func) {
+ progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN :
+ WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
+ &args.progress);
+ }
+
+ /* If a sequential extraction was specified, sort the streams to be
+ * extracted by their position in the WIM file, so that the WIM file can
+ * be read sequentially. */
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
+ ret = sort_stream_list_by_wim_position(&stream_list);
+ if (ret != 0) {
+ WARNING("Falling back to non-sequential extraction");
+ extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
+ }
+ }
+
+ if (progress_func) {
+ progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
+ &args.progress);
+ }
+
+ /* Make the directory structure and extract empty files */
+ args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
+ args.apply_dentry = ops->apply_dentry;
+ ret = for_dentry_in_tree(root, maybe_apply_dentry, &args);
+ args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
+ if (ret)
+ goto out_dentry_reset_needs_extraction;
+
+ if (progress_func) {
+ progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
+ &args.progress);
+ }
+
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
+ args.target_realpath = realpath(target, NULL);
+ if (!args.target_realpath) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_dentry_reset_needs_extraction;
+ }
+ args.target_realpath_len = tstrlen(args.target_realpath);
+ }
+
+ /* Extract non-empty files */
+ ret = apply_stream_list(&stream_list, &args, ops, progress_func);
+ if (ret)
+ goto out_free_target_realpath;
+
+ if (progress_func) {
+ progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
+ &args.progress);
+ }
+
+ /* Apply timestamps */
+ ret = for_dentry_in_tree_depth(root,
+ ops->apply_dentry_timestamps, &args);
+ if (ret)
+ goto out_free_target_realpath;
+
+ if (progress_func) {
+ progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END :
+ WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
+ &args.progress);
+ }
+out_free_target_realpath:
+ FREE(args.target_realpath);
+out_dentry_reset_needs_extraction:
+ for_dentry_in_tree(root, dentry_reset_needs_extraction, NULL);
+out_ntfs_umount:
+#ifdef WITH_NTFS_3G
+ /* Unmount the NTFS volume */
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
+ if (ntfs_umount(args.vol, FALSE) != 0) {
+ ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
+ args.target);
+ if (ret == 0)
+ ret = WIMLIB_ERR_NTFS_3G;
+ }
+ }
+#endif
+out:
+ return ret;
+}
+
+/* Validates a single wimlib_extract_command, mostly checking to make sure the
+ * extract flags make sense. */
+static int
+check_extract_command(struct wimlib_extract_command *cmd, int wim_header_flags)
+{
+ int extract_flags;
+ bool is_entire_image = (cmd->wim_source_path[0] == T('\0'));
+
+ /* Empty destination path? */
+ if (cmd->fs_dest_path[0] == T('\0'))
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ extract_flags = cmd->extract_flags;
+
+ /* Specified both symlink and hardlink modes? */
+ if ((extract_flags &
+ (WIMLIB_EXTRACT_FLAG_SYMLINK |
+ WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
+ WIMLIB_EXTRACT_FLAG_HARDLINK))
+ return WIMLIB_ERR_INVALID_PARAM;
+
+#ifdef __WIN32__
+ /* Wanted UNIX data on Windows? */
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
+ ERROR("Extracting UNIX data is not supported on Windows");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+ /* Wanted linked extraction on Windows? (XXX This is possible, just not
+ * implemented yet.) */
+ if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
+ WIMLIB_EXTRACT_FLAG_HARDLINK))
+ {
+ ERROR("Linked extraction modes are not supported on Windows");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+#endif
+
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
+ /* NTFS-3g extraction mode requested */
+#ifdef WITH_NTFS_3G
+ if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
+ WIMLIB_EXTRACT_FLAG_HARDLINK))) {
+ ERROR("Cannot specify symlink or hardlink flags when applying\n"
+ " directly to a NTFS volume");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+ if (!is_entire_image &&
+ (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS))
+ {
+ ERROR("When applying directly to a NTFS volume you can "
+ "only extract a full image, not part of one");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
+ ERROR("Cannot restore UNIX-specific data in "
+ "the NTFS extraction mode");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+#else
+ ERROR("wimlib was compiled without support for NTFS-3g, so");
+ ERROR("we cannot apply a WIM image directly to a NTFS volume");
+ return WIMLIB_ERR_UNSUPPORTED;
+#endif
+ }
+
+ if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
+ WIMLIB_EXTRACT_FLAG_NORPFIX)) ==
+ (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
+ {
+ ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+
+ if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
+ WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
+ {
+ /* Do reparse point fixups by default if the WIM header says
+ * they are enabled and we are extracting a full image. */
+ if ((wim_header_flags & WIM_HDR_FLAG_RP_FIX) && is_entire_image)
+ extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
+ }
+
+ if (!is_entire_image && (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)) {
+ ERROR("Cannot specify --rpfix when not extracting entire image");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+
+ cmd->extract_flags = extract_flags;
+ return 0;
+}
+
+
+/* Internal function to execute extraction commands for a WIM image. */
+static int
+do_wimlib_extract_files(WIMStruct *wim,
+ int image,
+ struct wimlib_extract_command *cmds,
+ size_t num_cmds,
+ wimlib_progress_func_t progress_func)
+{