+out:
+ return ret;
+}
+
+static int
+check_extract_command(struct wimlib_extract_command *cmd,
+ bool multiple_commands,
+ int wim_header_flags)
+{
+ int extract_flags;
+ bool is_entire_image = (cmd->wim_source_path == T('\0'));
+
+ if (cmd->fs_dest_path[0] == T('\0'))
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ extract_flags = cmd->extract_flags;
+
+ 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__
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
+ ERROR("Extracting UNIX data is not supported on Windows");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+ 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) {
+#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("Can only extract entire image when applying "
+ "directly to a NTFS volume");
+ 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)
+ {
+ 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;
+}
+
+
+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)
+{
+ int ret;
+ bool found_link_cmd = false;
+ bool found_nolink_cmd = false;
+
+ ret = select_wim_image(wim, image);
+ if (ret)
+ return ret;
+
+ ret = wim_checksum_unhashed_streams(wim);
+ if (ret)
+ return ret;
+
+ for (size_t i = 0; i < num_cmds; i++) {
+ ret = check_extract_command(&cmds[i], num_cmds > 1,
+ wim->hdr.flags);
+ if (ret)
+ return ret;
+ if (cmds[i].extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
+ WIMLIB_EXTRACT_FLAG_HARDLINK)) {
+ found_link_cmd = true;
+ } else {
+ found_nolink_cmd = true;
+ }
+ if (found_link_cmd && found_nolink_cmd) {
+ ERROR("Symlink or hardlink extraction mode must "
+ "be set on all extraction commands");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+ }
+
+ for (size_t i = 0; i < num_cmds; i++) {
+ ret = extract_tree(wim, image,
+ cmds[i].wim_source_path,
+ cmds[i].fs_dest_path,
+ cmds[i].extract_flags,
+ progress_func);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+WIMLIBAPI int
+wimlib_extract_files(WIMStruct *wim,
+ int image,
+ int default_extract_flags,
+ const struct wimlib_extract_command *cmds,
+ size_t num_cmds,
+ WIMStruct **additional_swms,
+ unsigned num_additional_swms,
+ wimlib_progress_func_t progress_func)
+{
+ int ret;
+ struct wimlib_extract_command *cmds_copy;
+ struct wim_lookup_table *wim_tab_save, *joined_tab;
+ int all_flags = 0;
+
+ default_extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
+
+ ret = verify_swm_set(wim, additional_swms, num_additional_swms);
+ if (ret)
+ goto out;
+
+ if (num_additional_swms) {
+ ret = new_joined_lookup_table(wim, additional_swms,
+ num_additional_swms,
+ &joined_tab);
+ if (ret)
+ goto out;
+ wim_tab_save = wim->lookup_table;
+ wim->lookup_table = joined_tab;
+ }
+
+ cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
+ if (!cmds_copy) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_restore_lookup_table;
+ }
+
+ for (size_t i = 0; i < num_cmds; i++) {
+ cmds_copy[i].extract_flags = (default_extract_flags |
+ cmds[i].extract_flags)
+ & WIMLIB_EXTRACT_MASK_PUBLIC;
+ all_flags |= cmds_copy[i].extract_flags;
+
+ cmds_copy[i].wim_source_path = canonicalize_wim_path(cmds[i].wim_source_path);
+ if (!cmds_copy[i].wim_source_path) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_free_cmds_copy;
+ }
+
+ cmds_copy[i].fs_dest_path = canonicalize_fs_path(cmds[i].fs_dest_path);
+ if (!cmds_copy[i].fs_dest_path) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_free_cmds_copy;
+ }
+
+ }
+ ret = do_wimlib_extract_files(wim, image,
+ cmds_copy, num_cmds,
+ progress_func);
+
+ if (all_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
+ WIMLIB_EXTRACT_FLAG_HARDLINK))
+ {
+ for_lookup_table_entry(wim->lookup_table,
+ lte_free_extracted_file, NULL);
+ }
+out_free_cmds_copy:
+ for (size_t i = 0; i < num_cmds; i++) {
+ FREE(cmds_copy[i].wim_source_path);
+ FREE(cmds_copy[i].fs_dest_path);
+ }
+ FREE(cmds_copy);
+out_restore_lookup_table:
+ if (num_additional_swms) {
+ free_lookup_table(wim->lookup_table);
+ wim->lookup_table = wim_tab_save;
+ }
+out:
+ return ret;
+}
+
+static int
+extract_single_image(WIMStruct *wim, int image,
+ const tchar *target, int extract_flags,
+ wimlib_progress_func_t progress_func)
+{
+ int ret;
+ tchar *target_copy = canonicalize_fs_path(target);
+ struct wimlib_extract_command cmd = {
+ .wim_source_path = T(""),
+ .fs_dest_path = target_copy,
+ .extract_flags = extract_flags,
+ };
+ ret = do_wimlib_extract_files(wim, image, &cmd, 1, progress_func);
+ FREE(target_copy);