+ *num_tchars_ret = num_wchars;
+ return text_wstr;
+#endif /* __WIN32__ */
+}
+
+static tchar *
+file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
+{
+ char *contents;
+ size_t num_bytes;
+
+ contents = file_get_contents(filename, &num_bytes);
+ if (!contents)
+ return NULL;
+ return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
+}
+
+static tchar *
+stdin_get_text_contents(size_t *num_tchars_ret)
+{
+ char *contents;
+ size_t num_bytes;
+
+ contents = stdin_get_contents(&num_bytes);
+ if (!contents)
+ return NULL;
+ return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
+}
+
+#define TO_PERCENT(numerator, denominator) \
+ (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
+
+/* Given an enumerated value for WIM compression type, return a descriptive
+ * string. */
+static const tchar *
+get_data_type(int ctype)
+{
+ switch (ctype) {
+ case WIMLIB_COMPRESSION_TYPE_NONE:
+ return T("uncompressed");
+ case WIMLIB_COMPRESSION_TYPE_LZX:
+ return T("LZX-compressed");
+ case WIMLIB_COMPRESSION_TYPE_XPRESS:
+ return T("XPRESS-compressed");
+ }
+ return NULL;
+}
+
+#define GIBIBYTE_MIN_NBYTES 10000000000ULL
+#define MEBIBYTE_MIN_NBYTES 10000000ULL
+#define KIBIBYTE_MIN_NBYTES 10000ULL
+
+static unsigned
+get_unit(uint64_t total_bytes, const tchar **name_ret)
+{
+ if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
+ *name_ret = T("GiB");
+ return 30;
+ } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
+ *name_ret = T("MiB");
+ return 20;
+ } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
+ *name_ret = T("KiB");
+ return 10;
+ } else {
+ *name_ret = T("bytes");
+ return 0;
+ }
+}
+
+/* Progress callback function passed to various wimlib functions. */
+static int
+imagex_progress_func(enum wimlib_progress_msg msg,
+ const union wimlib_progress_info *info)
+{
+ unsigned percent_done;
+ unsigned unit_shift;
+ const tchar *unit_name;
+ if (imagex_be_quiet)
+ return 0;
+ switch (msg) {
+ case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
+ unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
+ percent_done = TO_PERCENT(info->write_streams.completed_bytes,
+ info->write_streams.total_bytes);
+
+ if (info->write_streams.completed_streams == 0) {
+ const tchar *data_type;
+
+ data_type = get_data_type(info->write_streams.compression_type);
+ imagex_printf(T("Writing %"TS" data using %u thread%"TS"\n"),
+ data_type, info->write_streams.num_threads,
+ (info->write_streams.num_threads == 1) ? T("") : T("s"));
+ }
+ if (info->write_streams.total_parts <= 1) {
+ imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
+ "written (%u%% done)"),
+ info->write_streams.completed_bytes >> unit_shift,
+ unit_name,
+ info->write_streams.total_bytes >> unit_shift,
+ unit_name,
+ percent_done);
+ } else {
+ imagex_printf(T("\rWriting resources from part %u of %u: "
+ "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
+ (info->write_streams.completed_parts ==
+ info->write_streams.total_parts) ?
+ info->write_streams.completed_parts :
+ info->write_streams.completed_parts + 1,
+ info->write_streams.total_parts,
+ info->write_streams.completed_bytes >> unit_shift,
+ unit_name,
+ info->write_streams.total_bytes >> unit_shift,
+ unit_name,
+ percent_done);
+ }
+ if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
+ imagex_printf(T("\n"));
+ break;
+ case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
+ imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
+ if (*info->scan.wim_target_path) {
+ imagex_printf(T(" (loading as WIM path: "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
+ info->scan.wim_target_path);
+ } else {
+ imagex_printf(T(" (loading as root of WIM image)...\n"));
+ }
+ break;
+ case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
+ switch (info->scan.status) {
+ case WIMLIB_SCAN_DENTRY_OK:
+ imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
+ break;
+ case WIMLIB_SCAN_DENTRY_EXCLUDED:
+ imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
+ break;
+ case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
+ imagex_printf(T("WARNING: Excluding unsupported file or directory\n"
+ " \"%"TS"\" from capture\n"), info->scan.cur_path);
+ break;
+ }
+ break;
+ case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
+ unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
+ percent_done = TO_PERCENT(info->integrity.completed_bytes,
+ info->integrity.total_bytes);
+ imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
+ "of %"PRIu64" %"TS" (%u%%) done"),
+ info->integrity.filename,
+ info->integrity.completed_bytes >> unit_shift,
+ unit_name,
+ info->integrity.total_bytes >> unit_shift,
+ unit_name,
+ percent_done);
+ if (info->integrity.completed_bytes == info->integrity.total_bytes)
+ imagex_printf(T("\n"));
+ break;
+ case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
+ unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
+ percent_done = TO_PERCENT(info->integrity.completed_bytes,
+ info->integrity.total_bytes);
+ imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
+ "of %"PRIu64" %"TS" (%u%%) done"),
+ info->integrity.completed_bytes >> unit_shift,
+ unit_name,
+ info->integrity.total_bytes >> unit_shift,
+ unit_name,
+ percent_done);
+ if (info->integrity.completed_bytes == info->integrity.total_bytes)
+ imagex_printf(T("\n"));
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
+ imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
+ "to %"TS" \"%"TS"\"\n"),
+ info->extract.image,
+ info->extract.image_name,
+ info->extract.wimfile_name,
+ ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
+ T("NTFS volume") : T("directory")),
+ info->extract.target);
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
+ imagex_printf(T("Extracting "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
+ "in \"%"TS"\" to \"%"TS"\"\n"),
+ info->extract.extract_root_wim_source_path,
+ info->extract.image,
+ info->extract.image_name,
+ info->extract.wimfile_name,
+ info->extract.target);
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
+ percent_done = TO_PERCENT(info->extract.completed_bytes,
+ info->extract.total_bytes);
+ unit_shift = get_unit(info->extract.total_bytes, &unit_name);
+ imagex_printf(T("\rExtracting files: "
+ "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
+ info->extract.completed_bytes >> unit_shift,
+ unit_name,
+ info->extract.total_bytes >> unit_shift,
+ unit_name,
+ percent_done);
+ if (info->extract.completed_bytes >= info->extract.total_bytes)
+ imagex_printf(T("\n"));
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
+ if (info->extract.total_parts != 1) {
+ imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
+ info->extract.part_number,
+ info->extract.total_parts);
+ }
+ break;
+ case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
+ if (info->extract.extract_root_wim_source_path[0] == T('\0'))
+ imagex_printf(T("Setting timestamps on all extracted files...\n"));
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
+ if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
+ imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
+ info->extract.target);
+ }
+ break;
+ case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
+ percent_done = TO_PERCENT(info->split.completed_bytes,
+ info->split.total_bytes);
+ unit_shift = get_unit(info->split.total_bytes, &unit_name);
+ imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
+ "%"PRIu64" %"TS" (%u%%) written\n"),
+ info->split.part_name,
+ info->split.cur_part_number,
+ info->split.total_parts,
+ info->split.completed_bytes >> unit_shift,
+ unit_name,
+ info->split.total_bytes >> unit_shift,
+ unit_name,
+ percent_done);
+ break;
+ case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
+ if (info->split.completed_bytes == info->split.total_bytes) {
+ imagex_printf(T("Finished writing split WIM part %u of %u\n"),
+ info->split.cur_part_number,
+ info->split.total_parts);
+ }
+ break;
+ case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
+ switch (info->update.command->op) {
+ case WIMLIB_UPDATE_OP_DELETE:
+ imagex_printf(T("Deleted WIM path "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
+ info->update.command->delete_.wim_path);
+ break;
+ case WIMLIB_UPDATE_OP_RENAME:
+ imagex_printf(T("Renamed WIM path "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
+ info->update.command->rename.wim_source_path,
+ info->update.command->rename.wim_target_path);
+ break;
+ case WIMLIB_UPDATE_OP_ADD:
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+ fflush(imagex_info_file);
+ return 0;
+}
+
+static unsigned
+parse_num_threads(const tchar *optarg)
+{
+ tchar *tmp;
+ unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
+ if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
+ imagex_error(T("Number of threads must be a non-negative integer!"));
+ return UINT_MAX;
+ } else {
+ return ul_nthreads;
+ }
+}
+
+static uint32_t parse_chunk_size(const char *optarg)
+{
+ char *tmp;
+ unsigned long chunk_size = strtoul(optarg, &tmp, 10);
+ if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
+ imagex_error(T("Chunk size must be a non-negative integer!"));
+ return UINT32_MAX;
+ } else {
+ return chunk_size;
+ }
+}
+
+
+/*
+ * Parse an option passed to an update command.
+ *
+ * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
+ * parsed.
+ *
+ * @option: Text string for the option (beginning with --)
+ *
+ * @cmd: `struct wimlib_update_command' that is being constructed for
+ * this command.
+ *
+ * Returns true if the option was recognized; false if not.
+ */
+static bool
+update_command_add_option(int op, const tchar *option,
+ struct wimlib_update_command *cmd)
+{
+ bool recognized = true;
+ switch (op) {
+ case WIMLIB_UPDATE_OP_ADD:
+ if (!tstrcmp(option, T("--verbose")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
+ else if (!tstrcmp(option, T("--unix-data")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
+ else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
+ else if (!tstrcmp(option, T("--strict-acls")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
+ else if (!tstrcmp(option, T("--dereference")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
+ else
+ recognized = false;
+ break;
+ case WIMLIB_UPDATE_OP_DELETE:
+ if (!tstrcmp(option, T("--force")))
+ cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
+ else if (!tstrcmp(option, T("--recursive")))
+ cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
+ else
+ recognized = false;
+ break;
+ default:
+ recognized = false;
+ break;
+ }
+ return recognized;
+}
+
+/* How many nonoption arguments each `imagex update' command expects */
+static const unsigned update_command_num_nonoptions[] = {
+ [WIMLIB_UPDATE_OP_ADD] = 2,
+ [WIMLIB_UPDATE_OP_DELETE] = 1,
+ [WIMLIB_UPDATE_OP_RENAME] = 2,
+};
+
+static void
+update_command_add_nonoption(int op, const tchar *nonoption,
+ struct wimlib_update_command *cmd,
+ unsigned num_nonoptions)
+{
+ switch (op) {
+ case WIMLIB_UPDATE_OP_ADD:
+ if (num_nonoptions == 0)
+ cmd->add.fs_source_path = (tchar*)nonoption;
+ else
+ cmd->add.wim_target_path = (tchar*)nonoption;
+ break;
+ case WIMLIB_UPDATE_OP_DELETE:
+ cmd->delete_.wim_path = (tchar*)nonoption;
+ break;
+ case WIMLIB_UPDATE_OP_RENAME:
+ if (num_nonoptions == 0)
+ cmd->rename.wim_source_path = (tchar*)nonoption;
+ else
+ cmd->rename.wim_target_path = (tchar*)nonoption;
+ break;
+ }
+}
+
+/*
+ * Parse a command passed on stdin to `imagex update'.
+ *
+ * @line: Text of the command.
+ * @len: Length of the line, including a null terminator
+ * at line[len - 1].
+ *
+ * @command: A `struct wimlib_update_command' to fill in from the parsed
+ * line.
+ *
+ * @line_number: Line number of the command, for diagnostics.
+ *
+ * Returns true on success; returns false on parse error.
+ */
+static bool
+parse_update_command(tchar *line, size_t len,
+ struct wimlib_update_command *command,
+ size_t line_number)
+{
+ int ret;
+ tchar *command_name;
+ int op;
+ size_t num_nonoptions;
+
+ /* Get the command name ("add", "delete", "rename") */
+ ret = parse_string(&line, &len, &command_name);
+ if (ret != PARSE_STRING_SUCCESS)
+ return false;
+
+ if (!tstrcasecmp(command_name, T("add"))) {
+ op = WIMLIB_UPDATE_OP_ADD;
+ } else if (!tstrcasecmp(command_name, T("delete"))) {
+ op = WIMLIB_UPDATE_OP_DELETE;
+ } else if (!tstrcasecmp(command_name, T("rename"))) {
+ op = WIMLIB_UPDATE_OP_RENAME;
+ } else {
+ imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
+ command_name, line_number);
+ return false;
+ }
+ command->op = op;
+
+ /* Parse additional options and non-options as needed */
+ num_nonoptions = 0;
+ for (;;) {
+ tchar *next_string;
+
+ ret = parse_string(&line, &len, &next_string);
+ if (ret == PARSE_STRING_NONE) /* End of line */
+ break;
+ else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
+ return false;
+ if (next_string[0] == T('-') && next_string[1] == T('-')) {
+ /* Option */
+ if (!update_command_add_option(op, next_string, command))
+ {
+ imagex_error(T("Unrecognized option \"%"TS"\" to "
+ "update command \"%"TS"\" on line %zu"),
+ next_string, command_name, line_number);
+
+ return false;
+ }
+ } else {
+ /* Nonoption */
+ if (num_nonoptions == update_command_num_nonoptions[op])
+ {
+ imagex_error(T("Unexpected argument \"%"TS"\" in "
+ "update command on line %zu\n"
+ " (The \"%"TS"\" command only "
+ "takes %zu nonoption arguments!)\n"),
+ next_string, line_number,
+ command_name, num_nonoptions);
+ return false;
+ }
+ update_command_add_nonoption(op, next_string,
+ command, num_nonoptions);
+ num_nonoptions++;
+ }
+ }
+
+ if (num_nonoptions != update_command_num_nonoptions[op]) {
+ imagex_error(T("Not enough arguments to update command "
+ "\"%"TS"\" on line %zu"), command_name, line_number);
+ return false;
+ }
+ return true;
+}
+
+static struct wimlib_update_command *
+parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
+ size_t *num_cmds_ret)
+{
+ ssize_t nlines;
+ tchar *p;
+ struct wimlib_update_command *cmds;
+ size_t i, j;
+
+ nlines = text_file_count_lines(cmd_file_contents_p,
+ &cmd_file_nchars);
+ if (nlines < 0)
+ return NULL;
+
+ /* Always allocate at least 1 slot, just in case the implementation of
+ * calloc() returns NULL if 0 bytes are requested. */
+ cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
+ if (!cmds) {
+ imagex_error(T("out of memory"));
+ return NULL;
+ }
+ p = *cmd_file_contents_p;
+ j = 0;
+ for (i = 0; i < nlines; i++) {
+ /* XXX: Could use rawmemchr() here instead, but it may not be
+ * available on all platforms. */
+ tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
+ size_t len = endp - p + 1;
+ *endp = T('\0');
+ if (!is_comment_line(p, len)) {
+ if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
+ free(cmds);
+ return NULL;
+ }
+ }
+ p = endp + 1;
+ }
+ *num_cmds_ret = j;
+ return cmds;
+}
+
+/* Apply one image, or all images, from a WIM file into a directory, OR apply
+ * one image from a WIM file to a NTFS volume. */
+static int
+imagex_apply(int argc, tchar **argv, int cmd)
+{
+ int c;
+ int open_flags = 0;
+ int image = WIMLIB_NO_IMAGE;
+ WIMStruct *wim;
+ struct wimlib_wim_info info;
+ int ret;
+ const tchar *wimfile;
+ const tchar *target;
+ const tchar *image_num_or_name = NULL;
+ int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
+
+ STRING_SET(refglobs);
+
+ for_opt(c, apply_options) {
+ switch (c) {
+ case IMAGEX_CHECK_OPTION:
+ open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
+ break;
+ case IMAGEX_HARDLINK_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
+ break;
+ case IMAGEX_SYMLINK_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
+ break;
+ case IMAGEX_VERBOSE_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
+ break;
+ case IMAGEX_REF_OPTION:
+ ret = string_set_append(&refglobs, optarg);
+ if (ret)
+ goto out_free_refglobs;
+ break;
+ case IMAGEX_UNIX_DATA_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
+ break;
+ case IMAGEX_NO_ACLS_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
+ break;
+ case IMAGEX_STRICT_ACLS_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
+ break;
+ case IMAGEX_NORPFIX_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
+ break;
+ case IMAGEX_RPFIX_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
+ break;
+ case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
+ extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
+ break;
+ case IMAGEX_RESUME_OPTION:
+ extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
+ break;
+ default:
+ goto out_usage;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ if (argc != 2 && argc != 3)
+ goto out_usage;
+
+ wimfile = argv[0];
+
+ if (!tstrcmp(wimfile, T("-"))) {
+ /* Attempt to apply pipable WIM from standard input. */
+ if (argc == 2) {
+ image_num_or_name = NULL;
+ target = argv[1];
+ } else {
+ image_num_or_name = argv[1];
+ target = argv[2];
+ }
+ wim = NULL;
+ } else {
+ ret = wimlib_open_wim(wimfile, open_flags, &wim,
+ imagex_progress_func);
+ if (ret)
+ goto out_free_refglobs;
+
+ wimlib_get_wim_info(wim, &info);
+
+ if (argc >= 3) {
+ /* Image explicitly specified. */
+ image_num_or_name = argv[1];
+ image = wimlib_resolve_image(wim, image_num_or_name);
+ ret = verify_image_exists(image, image_num_or_name, wimfile);
+ if (ret)
+ goto out_wimlib_free;
+ target = argv[2];
+ } else {
+ /* No image specified; default to image 1, but only if the WIM
+ * contains exactly one image. */
+
+ if (info.image_count != 1) {
+ imagex_error(T("\"%"TS"\" contains %d images; "
+ "Please select one (or all)."),
+ wimfile, info.image_count);
+ wimlib_free(wim);
+ goto out_usage;
+ }
+ image = 1;
+ target = argv[1];
+ }
+ }
+
+ if (refglobs.num_strings) {
+ if (wim == NULL) {
+ imagex_error(T("Can't specify --ref when applying from stdin!"));
+ ret = -1;
+ goto out_wimlib_free;
+ }
+ ret = wim_reference_globs(wim, &refglobs, open_flags);
+ if (ret)
+ goto out_wimlib_free;
+ }
+
+#ifndef __WIN32__
+ {
+ /* Interpret a regular file or block device target as a NTFS
+ * volume. */
+ struct stat stbuf;
+
+ if (tstat(target, &stbuf)) {
+ if (errno != ENOENT) {
+ imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
+ target);
+ ret = -1;
+ goto out_wimlib_free;
+ }
+ } else {
+ if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
+ extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
+ }
+ }
+#endif
+
+ if (wim) {
+ ret = wimlib_extract_image(wim, image, target, extract_flags,
+ imagex_progress_func);
+ } else {
+ set_fd_to_binary_mode(STDIN_FILENO);
+ ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
+ image_num_or_name,
+ target, extract_flags,
+ imagex_progress_func);
+ }
+ if (ret == 0) {
+ imagex_printf(T("Done applying WIM image.\n"));
+ } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
+ if (wim) {
+ do_resource_not_found_warning(wimfile, &info, &refglobs);
+ } else {
+ imagex_error(T( "If you are applying an image "
+ "from a split pipable WIM,\n"
+ " make sure you have "
+ "concatenated together all parts."));
+ }
+ }
+out_wimlib_free:
+ wimlib_free(wim);
+out_free_refglobs:
+ string_set_destroy(&refglobs);
+ return ret;
+
+out_usage:
+ usage(CMD_APPLY, stderr);
+ ret = -1;
+ goto out_free_refglobs;
+}
+
+/* Create a WIM image from a directory tree, NTFS volume, or multiple files or
+ * directory trees. 'wimlib-imagex capture': create a new WIM file containing
+ * the desired image. 'wimlib-imagex append': add a new image to an existing
+ * WIM file. */
+static int
+imagex_capture_or_append(int argc, tchar **argv, int cmd)
+{
+ int c;
+ int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
+ int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
+ WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
+ int write_flags = 0;
+ int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
+ uint32_t chunk_size = UINT32_MAX;
+ const tchar *wimfile;
+ int wim_fd;
+ const tchar *name;
+ const tchar *desc;
+ const tchar *flags_element = NULL;
+
+ WIMStruct *wim;
+ STRING_SET(base_wimfiles);
+ WIMStruct **base_wims;
+
+ WIMStruct *template_wim;
+ const tchar *template_wimfile = NULL;
+ const tchar *template_image_name_or_num = NULL;
+ int template_image = WIMLIB_NO_IMAGE;
+
+ int ret;
+ unsigned num_threads = 0;
+
+ tchar *source;
+ tchar *source_copy;
+
+ const tchar *config_file = NULL;
+ tchar *config_str;
+ struct wimlib_capture_config *config;
+
+ bool source_list = false;
+ size_t source_list_nchars = 0;
+ tchar *source_list_contents;
+ bool capture_sources_malloced;
+ struct wimlib_capture_source *capture_sources;
+ size_t num_sources;
+ bool name_defaulted;
+
+ for_opt(c, capture_or_append_options) {
+ switch (c) {
+ case IMAGEX_BOOT_OPTION:
+ add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
+ break;
+ case IMAGEX_CHECK_OPTION:
+ open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
+ write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
+ break;
+ case IMAGEX_NOCHECK_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
+ break;
+ case IMAGEX_CONFIG_OPTION:
+ config_file = optarg;
+ add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
+ break;
+ case IMAGEX_COMPRESS_OPTION:
+ compression_type = get_compression_type(optarg);
+ if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
+ goto out_err;
+ break;
+ case IMAGEX_COMPRESS_SLOW_OPTION:
+ ret = set_compress_slow();
+ if (ret)
+ goto out_err;
+ compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
+ break;
+ case IMAGEX_CHUNK_SIZE_OPTION:
+ chunk_size = parse_chunk_size(optarg);
+ if (chunk_size == UINT32_MAX)
+ goto out_err;
+ break;
+ case IMAGEX_FLAGS_OPTION:
+ flags_element = optarg;
+ break;
+ case IMAGEX_DEREFERENCE_OPTION:
+ add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
+ break;
+ case IMAGEX_VERBOSE_OPTION:
+ add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
+ break;
+ case IMAGEX_THREADS_OPTION:
+ num_threads = parse_num_threads(optarg);
+ if (num_threads == UINT_MAX)
+ goto out_err;
+ break;
+ case IMAGEX_REBUILD_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
+ break;
+ case IMAGEX_UNIX_DATA_OPTION:
+ add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
+ break;
+ case IMAGEX_SOURCE_LIST_OPTION:
+ source_list = true;
+ break;
+ case IMAGEX_NO_ACLS_OPTION:
+ add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
+ break;
+ case IMAGEX_STRICT_ACLS_OPTION:
+ add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
+ break;
+ case IMAGEX_RPFIX_OPTION:
+ add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
+ break;
+ case IMAGEX_NORPFIX_OPTION:
+ add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
+ break;
+ case IMAGEX_PIPABLE_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
+ break;
+ case IMAGEX_NOT_PIPABLE_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
+ break;
+ case IMAGEX_UPDATE_OF_OPTION:
+ if (template_image_name_or_num) {
+ imagex_error(T("'--update-of' can only be "
+ "specified one time!"));
+ goto out_err;
+ } else {
+ tchar *colon;
+ colon = tstrrchr(optarg, T(':'));
+
+ if (colon) {
+ template_wimfile = optarg;
+ *colon = T('\0');
+ template_image_name_or_num = colon + 1;
+ } else {
+ template_wimfile = NULL;
+ template_image_name_or_num = optarg;
+ }
+ }
+ break;
+ case IMAGEX_DELTA_FROM_OPTION:
+ if (cmd != CMD_CAPTURE) {
+ imagex_error(T("'--delta-from' is only "
+ "valid for capture!"));
+ goto out_usage;
+ }
+ ret = string_set_append(&base_wimfiles, optarg);
+ if (ret)
+ goto out_free_base_wimfiles;
+ write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
+ break;
+ default:
+ goto out_usage;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc < 2 || argc > 4)
+ goto out_usage;
+
+ source = argv[0];
+ wimfile = argv[1];
+
+ /* Set default compression type. */
+ if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
+ struct wimlib_lzx_params params;
+ memset(¶ms, 0, sizeof(params));
+ params.size_of_this = sizeof(params);
+ params.algorithm = WIMLIB_LZX_ALGORITHM_FAST;
+ params.use_defaults = 1;
+
+ wimlib_lzx_set_default_params(¶ms);
+ compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
+ }
+
+ if (!tstrcmp(wimfile, T("-"))) {
+ /* Writing captured WIM to standard output. */
+ #if 0
+ if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
+ imagex_error("Can't write a non-pipable WIM to "
+ "standard output! Specify --pipable\n"
+ " if you want to create a pipable WIM "
+ "(but read the docs first).");
+ goto out_err;
+ }
+ #else
+ write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
+ #endif
+ if (cmd == CMD_APPEND) {
+ imagex_error(T("Using standard output for append does "
+ "not make sense."));
+ goto out_err;
+ }
+ wim_fd = STDOUT_FILENO;
+ wimfile = NULL;
+ imagex_info_file = stderr;
+ set_fd_to_binary_mode(wim_fd);
+ }
+
+ /* If template image was specified using --update-of=IMAGE rather
+ * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
+ if (template_image_name_or_num && !template_wimfile) {
+ if (base_wimfiles.num_strings == 1) {
+ /* Capturing delta WIM based on single WIM: default to
+ * base WIM. */
+ template_wimfile = base_wimfiles.strings[0];
+ } else if (cmd == CMD_APPEND) {
+ /* Appending to WIM: default to WIM being appended to.
+ */
+ template_wimfile = wimfile;
+ } else {
+ /* Capturing a normal (non-delta) WIM, so the WIM file
+ * *must* be explicitly specified. */
+ if (base_wimfiles.num_strings > 1) {
+ imagex_error(T("For capture of delta WIM "
+ "based on multiple existing "
+ "WIMs,\n"
+ " '--update-of' must "
+ "specify WIMFILE:IMAGE!"));
+ } else {
+ imagex_error(T("For capture of non-delta WIM, "
+ "'--update-of' must specify "
+ "WIMFILE:IMAGE!"));
+ }
+ goto out_usage;
+ }
+ }
+
+ if (argc >= 3) {
+ name = argv[2];
+ name_defaulted = false;
+ } else {
+ /* Set default name to SOURCE argument, omitting any directory
+ * prefixes and trailing slashes. This requires making a copy
+ * of @source. Leave some free characters at the end in case we
+ * append a number to keep the name unique. */
+ size_t source_name_len;
+
+ source_name_len = tstrlen(source);
+ source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
+ name = tbasename(tstrcpy(source_copy, source));
+ name_defaulted = true;
+ }
+ /* Image description defaults to NULL if not given. */
+ if (argc >= 4)
+ desc = argv[3];
+ else
+ desc = NULL;
+
+ if (source_list) {
+ /* Set up capture sources in source list mode */
+ if (source[0] == T('-') && source[1] == T('\0')) {
+ source_list_contents = stdin_get_text_contents(&source_list_nchars);
+ } else {
+ source_list_contents = file_get_text_contents(source,
+ &source_list_nchars);
+ }
+ if (!source_list_contents)
+ goto out_err;
+
+ capture_sources = parse_source_list(&source_list_contents,
+ source_list_nchars,
+ &num_sources);
+ if (!capture_sources) {
+ ret = -1;
+ goto out_free_source_list_contents;
+ }
+ capture_sources_malloced = true;
+ } else {
+ /* Set up capture source in non-source-list mode. */
+ capture_sources = alloca(sizeof(struct wimlib_capture_source));
+ capture_sources[0].fs_source_path = source;
+ capture_sources[0].wim_target_path = NULL;
+ capture_sources[0].reserved = 0;
+ num_sources = 1;
+ capture_sources_malloced = false;
+ source_list_contents = NULL;
+ }
+
+ if (config_file) {
+ /* Read and parse capture configuration file. */
+ size_t config_len;
+
+ config_str = file_get_text_contents(config_file, &config_len);
+ if (!config_str) {
+ ret = -1;
+ goto out_free_capture_sources;
+ }
+
+ config = alloca(sizeof(*config));
+ ret = parse_capture_config(&config_str, config_len, config);
+ if (ret)
+ goto out_free_config;
+ } else {
+ /* No capture configuration file specified; use default
+ * configuration for capturing Windows operating systems. */
+ config = NULL;
+ add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
+ }
+
+ /* Open the existing WIM, or create a new one. */
+ if (cmd == CMD_APPEND)
+ ret = wimlib_open_wim(wimfile, open_flags, &wim,
+ imagex_progress_func);
+ else
+ ret = wimlib_create_new_wim(compression_type, &wim);
+ if (ret)
+ goto out_free_config;
+
+ /* Set chunk size if non-default. */
+ if (chunk_size != UINT32_MAX) {
+ ret = wimlib_set_output_chunk_size(wim, chunk_size);
+ if (ret)
+ goto out_free_wim;
+ }
+
+#ifndef __WIN32__
+ /* Detect if source is regular file or block device and set NTFS volume
+ * capture mode. */
+ if (!source_list) {
+ struct stat stbuf;
+
+ if (tstat(source, &stbuf) == 0) {
+ if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
+ imagex_printf(T("Capturing WIM image from NTFS "
+ "filesystem on \"%"TS"\"\n"), source);
+ add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
+ }
+ } else {
+ if (errno != ENOENT) {
+ imagex_error_with_errno(T("Failed to stat "
+ "\"%"TS"\""), source);
+ ret = -1;
+ goto out_free_wim;
+ }
+ }
+ }
+#endif
+
+ /* If the user did not specify an image name, and the basename of the
+ * source already exists as an image name in the WIM file, append a
+ * suffix to make it unique. */
+ if (cmd == CMD_APPEND && name_defaulted) {
+ unsigned long conflict_idx;
+ tchar *name_end = tstrchr(name, T('\0'));
+ for (conflict_idx = 1;
+ wimlib_image_name_in_use(wim, name);
+ conflict_idx++)
+ {
+ tsprintf(name_end, T(" (%lu)"), conflict_idx);
+ }
+ }
+
+ /* If capturing a delta WIM, reference resources from the base WIMs
+ * before adding the new image. */
+ if (base_wimfiles.num_strings) {
+ base_wims = calloc(base_wimfiles.num_strings,
+ sizeof(base_wims[0]));
+ if (base_wims == NULL) {
+ imagex_error(T("Out of memory!"));
+ ret = -1;
+ goto out_free_wim;
+ }
+
+ for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
+ ret = wimlib_open_wim(base_wimfiles.strings[i],
+ open_flags, &base_wims[i],
+ imagex_progress_func);
+ if (ret)
+ goto out_free_base_wims;
+
+ }
+
+ ret = wimlib_reference_resources(wim, base_wims,
+ base_wimfiles.num_strings, 0);
+ if (ret)
+ goto out_free_base_wims;
+
+ if (base_wimfiles.num_strings == 1) {
+ imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
+ base_wimfiles.strings[0]);
+ } else {
+ imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
+ base_wimfiles.num_strings);
+ }
+
+ } else {
+ base_wims = NULL;
+ }
+
+ /* If capturing or appending as an update of an existing (template) image,
+ * open the WIM if needed and parse the image index. */
+ if (template_image_name_or_num) {
+
+
+ if (base_wimfiles.num_strings == 1 &&
+ template_wimfile == base_wimfiles.strings[0]) {
+ template_wim = base_wims[0];
+ } else if (template_wimfile == wimfile) {
+ template_wim = wim;
+ } else {
+ ret = wimlib_open_wim(template_wimfile, open_flags,
+ &template_wim, imagex_progress_func);
+ if (ret)
+ goto out_free_base_wims;
+ }
+
+ template_image = wimlib_resolve_image(template_wim,
+ template_image_name_or_num);
+
+ if (template_image_name_or_num[0] == T('-')) {
+ tchar *tmp;
+ unsigned long n;
+ struct wimlib_wim_info info;
+
+ wimlib_get_wim_info(template_wim, &info);
+ n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
+ if (n >= 1 && n <= info.image_count &&
+ *tmp == T('\0') &&
+ tmp != template_image_name_or_num + 1)
+ {
+ template_image = info.image_count - (n - 1);
+ }
+ }
+ ret = verify_image_exists_and_is_single(template_image,
+ template_image_name_or_num,
+ template_wimfile);
+ if (ret)
+ goto out_free_template_wim;
+ } else {
+ template_wim = NULL;
+ }
+
+ ret = wimlib_add_image_multisource(wim,
+ capture_sources,
+ num_sources,
+ name,
+ config,
+ add_image_flags,
+ imagex_progress_func);
+ if (ret)
+ goto out_free_template_wim;
+
+ if (desc || flags_element || template_image_name_or_num) {
+ /* User provided <DESCRIPTION> or <FLAGS> element, or an image
+ * on which the added one is to be based has been specified with
+ * --update-of. Get the index of the image we just
+ * added, then use it to call the appropriate functions. */
+ struct wimlib_wim_info info;
+
+ wimlib_get_wim_info(wim, &info);
+
+ if (desc) {
+ ret = wimlib_set_image_descripton(wim,
+ info.image_count,
+ desc);
+ if (ret)
+ goto out_free_template_wim;
+ }
+
+ if (flags_element) {
+ ret = wimlib_set_image_flags(wim, info.image_count,
+ flags_element);
+ if (ret)
+ goto out_free_template_wim;
+ }
+
+ /* Reference template image if the user provided one. */
+ if (template_image_name_or_num) {
+ imagex_printf(T("Using image %d "
+ "from \"%"TS"\" as template\n"),
+ template_image, template_wimfile);
+ ret = wimlib_reference_template_image(wim,
+ info.image_count,
+ template_wim,
+ template_image,
+ 0, NULL);
+ if (ret)
+ goto out_free_template_wim;
+ }
+ }
+
+ /* Write the new WIM or overwrite the existing WIM with the new image
+ * appended. */
+ if (cmd == CMD_APPEND) {
+ ret = wimlib_overwrite(wim, write_flags, num_threads,
+ imagex_progress_func);
+ } else if (wimfile) {
+ ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
+ write_flags, num_threads,
+ imagex_progress_func);
+ } else {
+ ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
+ write_flags, num_threads,
+ imagex_progress_func);
+ }
+out_free_template_wim:
+ /* template_wim may alias base_wims[0] or wim. */
+ if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
+ template_wim != wim)
+ wimlib_free(template_wim);
+out_free_base_wims:
+ for (size_t i = 0; i < base_wimfiles.num_strings; i++)
+ wimlib_free(base_wims[i]);
+ free(base_wims);
+out_free_wim:
+ wimlib_free(wim);
+out_free_config:
+ if (config) {
+ free(config->exclusion_pats.pats);
+ free(config->exclusion_exception_pats.pats);
+ free(config_str);
+ }
+out_free_capture_sources:
+ if (capture_sources_malloced)
+ free(capture_sources);
+out_free_source_list_contents:
+ free(source_list_contents);
+out_free_base_wimfiles:
+ string_set_destroy(&base_wimfiles);
+ return ret;
+
+out_usage:
+ usage(cmd, stderr);
+out_err:
+ ret = -1;
+ goto out_free_base_wimfiles;
+}
+
+/* Remove image(s) from a WIM. */
+static int
+imagex_delete(int argc, tchar **argv, int cmd)
+{
+ int c;
+ int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
+ int write_flags = 0;
+ const tchar *wimfile;
+ const tchar *image_num_or_name;
+ WIMStruct *wim;
+ int image;
+ int ret;
+
+ for_opt(c, delete_options) {
+ switch (c) {
+ case IMAGEX_CHECK_OPTION:
+ open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
+ write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
+ break;
+ case IMAGEX_SOFT_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
+ break;
+ default:
+ goto out_usage;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 2) {
+ if (argc < 1)
+ imagex_error(T("Must specify a WIM file"));
+ if (argc < 2)
+ imagex_error(T("Must specify an image"));
+ goto out_usage;
+ }
+ wimfile = argv[0];
+ image_num_or_name = argv[1];
+
+ ret = wimlib_open_wim(wimfile, open_flags, &wim,
+ imagex_progress_func);
+ if (ret)
+ goto out;
+
+ image = wimlib_resolve_image(wim, image_num_or_name);
+
+ ret = verify_image_exists(image, image_num_or_name, wimfile);
+ if (ret)
+ goto out_wimlib_free;
+
+ ret = wimlib_delete_image(wim, image);
+ if (ret) {
+ imagex_error(T("Failed to delete image from \"%"TS"\""),
+ wimfile);
+ goto out_wimlib_free;
+ }
+
+ ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
+ if (ret) {
+ imagex_error(T("Failed to write the file \"%"TS"\" with image "
+ "deleted"), wimfile);
+ }
+out_wimlib_free:
+ wimlib_free(wim);
+out:
+ return ret;
+
+out_usage:
+ usage(CMD_DELETE, stderr);
+ ret = -1;
+ goto out;
+}
+
+static int
+print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
+{
+ int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
+ return (ret >= 0) ? 0 : -1;
+}
+
+/* Print the files contained in an image(s) in a WIM file. */
+static int
+imagex_dir(int argc, tchar **argv, int cmd)
+{
+ const tchar *wimfile;
+ WIMStruct *wim = NULL;
+ int image;
+ int ret;
+ const tchar *path = T("");
+ int c;
+
+ for_opt(c, dir_options) {
+ switch (c) {
+ case IMAGEX_PATH_OPTION:
+ path = optarg;
+ break;
+ default:
+ goto out_usage;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc < 1) {
+ imagex_error(T("Must specify a WIM file"));
+ goto out_usage;
+ }
+ if (argc > 2) {
+ imagex_error(T("Too many arguments"));
+ goto out_usage;
+ }
+
+ wimfile = argv[0];
+ ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
+ if (ret)
+ goto out;
+
+ if (argc >= 2) {
+ image = wimlib_resolve_image(wim, argv[1]);
+ ret = verify_image_exists(image, argv[1], wimfile);
+ if (ret)
+ goto out_wimlib_free;
+ } else {
+ /* No image specified; default to image 1, but only if the WIM
+ * contains exactly one image. */
+
+ struct wimlib_wim_info info;
+
+ wimlib_get_wim_info(wim, &info);
+ if (info.image_count != 1) {
+ imagex_error(T("\"%"TS"\" contains %d images; Please "
+ "select one (or all)."),
+ wimfile, info.image_count);
+ wimlib_free(wim);
+ goto out_usage;
+ }
+ image = 1;
+ }
+
+ ret = wimlib_iterate_dir_tree(wim, image, path,
+ WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
+ print_full_path, NULL);
+out_wimlib_free:
+ wimlib_free(wim);
+out:
+ return ret;
+
+out_usage:
+ usage(CMD_DIR, stderr);
+ ret = -1;
+ goto out;
+}
+
+/* Exports one, or all, images from a WIM file to a new WIM file or an existing
+ * WIM file. */
+static int
+imagex_export(int argc, tchar **argv, int cmd)
+{
+ int c;
+ int open_flags = 0;
+ int export_flags = 0;
+ int write_flags = 0;
+ int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
+ const tchar *src_wimfile;
+ const tchar *src_image_num_or_name;
+ const tchar *dest_wimfile;
+ int dest_wim_fd;
+ const tchar *dest_name;
+ const tchar *dest_desc;
+ WIMStruct *src_wim;
+ struct wimlib_wim_info src_info;
+ WIMStruct *dest_wim;
+ int ret;
+ int image;
+ struct stat stbuf;
+ bool wim_is_new;
+ STRING_SET(refglobs);
+ unsigned num_threads = 0;
+
+ for_opt(c, export_options) {
+ switch (c) {
+ case IMAGEX_BOOT_OPTION:
+ export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
+ break;
+ case IMAGEX_CHECK_OPTION:
+ open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
+ write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
+ break;
+ case IMAGEX_NOCHECK_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
+ break;
+ case IMAGEX_COMPRESS_OPTION:
+ compression_type = get_compression_type(optarg);
+ if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
+ goto out_err;
+ break;
+ case IMAGEX_REF_OPTION:
+ ret = string_set_append(&refglobs, optarg);
+ if (ret)
+ goto out_free_refglobs;
+ break;
+ case IMAGEX_THREADS_OPTION:
+ num_threads = parse_num_threads(optarg);
+ if (num_threads == UINT_MAX)
+ goto out_err;
+ break;
+ case IMAGEX_REBUILD_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
+ break;
+ case IMAGEX_PIPABLE_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
+ break;
+ case IMAGEX_NOT_PIPABLE_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
+ break;
+ default:
+ goto out_usage;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ if (argc < 3 || argc > 5)
+ goto out_usage;
+ src_wimfile = argv[0];
+ src_image_num_or_name = argv[1];
+ dest_wimfile = argv[2];
+ dest_name = (argc >= 4) ? argv[3] : NULL;