+ /* 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;
+ 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)
+ merge_lookup_tables(wim, additional_swms, num_additional_swms);
+
+ 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)
+ unmerge_lookup_table(wim);
+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 =
+T(
+#ifdef __WIN32__
+"<>:\"/\\|?*"
+#else
+"/"
+#endif
+);
+
+/* This function checks if it is okay to use a WIM image's name as a directory
+ * name. */
+static bool
+image_name_ok_as_dir(const tchar *image_name)
+{
+ return image_name && *image_name &&
+ !tstrpbrk(image_name, filename_forbidden_chars) &&
+ tstrcmp(image_name, T(".")) &&
+ tstrcmp(image_name, T(".."));
+}
+
+/* Extracts all images from the WIM to the directory @target, with the images
+ * placed in subdirectories named by their image names. */
+static int
+extract_all_images(WIMStruct *wim,
+ const tchar *target,
+ int extract_flags,
+ wimlib_progress_func_t progress_func)
+{
+ size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
+ size_t output_path_len = tstrlen(target);
+ tchar buf[output_path_len + 1 + image_name_max_len + 1];
+ int ret;
+ int image;
+ const tchar *image_name;
+ struct stat stbuf;
+
+ if (tstat(target, &stbuf)) {
+ if (errno == ENOENT)
+ {
+ if (tmkdir(target, S_IRWXU | S_IRGRP | S_IXGRP |
+ S_IROTH | S_IXOTH))
+ {
+ ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
+ return WIMLIB_ERR_MKDIR;
+ }
+ } else {
+ ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
+ return WIMLIB_ERR_STAT;
+ }
+ } else if (!S_ISDIR(stbuf.st_mode)) {
+ ERROR("\"%"TS"\" is not a directory", target);
+ return WIMLIB_ERR_NOTDIR;
+ }
+
+ tmemcpy(buf, target, output_path_len);
+ buf[output_path_len] = T('/');
+ for (image = 1; image <= wim->hdr.image_count; image++) {
+ image_name = wimlib_get_image_name(wim, image);
+ if (image_name_ok_as_dir(image_name)) {
+ tstrcpy(buf + output_path_len + 1, image_name);
+ } else {
+ /* Image name is empty or contains forbidden characters.
+ * Use image number instead. */
+ tsprintf(buf + output_path_len + 1, T("%d"), image);
+ }
+ ret = extract_single_image(wim, image, buf, extract_flags,
+ progress_func);
+ if (ret)