+/* 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)
+{
+ int ret;
+ bool found_link_cmd = false;
+ bool found_nolink_cmd = false;
+
+ /* Select the image from which we are extracting files */
+ ret = select_wim_image(wim, image);
+ if (ret)
+ return ret;
+
+ /* Make sure there are no streams in the WIM that have not been
+ * checksummed yet. */
+ ret = wim_checksum_unhashed_streams(wim);
+ if (ret)
+ return ret;
+
+ /* Check for problems with the extraction commands */
+ for (size_t i = 0; i < num_cmds; i++) {
+ ret = check_extract_command(&cmds[i], 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;
+ }
+ }
+
+ /* Execute the extraction commands */
+ for (size_t i = 0; i < num_cmds; i++) {
+ ret = extract_tree(wim,
+ cmds[i].wim_source_path,
+ cmds[i].fs_dest_path,
+ cmds[i].extract_flags,
+ progress_func);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+/* Extract files or directories from a WIM image. */
+WIMLIBAPI int
+wimlib_extract_files(WIMStruct *wim,
+ int image,
+ const struct wimlib_extract_command *cmds,
+ size_t num_cmds,
+ int default_extract_flags,
+ 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_cmds == 0)
+ 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;
+}
+
+/*
+ * Extracts an image from a WIM file.
+ *
+ * @wim: WIMStruct for the WIM file.
+ *
+ * @image: Number of the single image to extract.
+ *
+ * @target: Directory or NTFS volume to extract the image to.
+ *
+ * @extract_flags: Bitwise or of WIMLIB_EXTRACT_FLAG_*.
+ *
+ * @progress_func: If non-NULL, a progress function to be called
+ * periodically.
+ *
+ * Returns 0 on success; nonzero on failure.
+ */
+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);
+ if (!target_copy)
+ return WIMLIB_ERR_NOMEM;
+ 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);
+ return ret;
+}
+
+static const tchar * const filename_forbidden_chars =