+#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);
+ tprintf(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"));
+ }
+ tprintf(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);
+ if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
+ tputchar(T('\n'));
+ break;
+ case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
+ tprintf(T("Scanning \"%"TS"\""), info->scan.source);
+ if (*info->scan.wim_target_path) {
+ tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
+ info->scan.wim_target_path);
+ } else {
+ tprintf(T(" (loading as root of WIM image)...\n"));
+ }
+ break;
+ case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
+ if (info->scan.excluded)
+ tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
+ else
+ tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
+ break;
+ /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
+ /*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);
+ tprintf(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)
+ tputchar(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);
+ tprintf(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)
+ tputchar(T('\n'));
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
+ tprintf(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:
+ tprintf(T("Extracting \"%"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_DIR_STRUCTURE_BEGIN:*/
+ /*tprintf(T("Applying directory structure to %"TS"\n"),*/
+ /*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);
+ tprintf(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)
+ tputchar(T('\n'));
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
+ tprintf(T("%"TS"\n"), info->extract.cur_path);
+ break;
+ case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
+ if (info->extract.extract_root_wim_source_path[0] == T('\0'))
+ tprintf(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) {
+ tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
+ info->extract.target);
+ }
+ break;
+ case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
+ percent_done = TO_PERCENT(info->join.completed_bytes,
+ info->join.total_bytes);
+ unit_shift = get_unit(info->join.total_bytes, &unit_name);
+ tprintf(T("Writing resources from part %u of %u: "
+ "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
+ (info->join.completed_parts == info->join.total_parts) ?
+ info->join.completed_parts : info->join.completed_parts + 1,
+ info->join.total_parts,
+ info->join.completed_bytes >> unit_shift,
+ unit_name,
+ info->join.total_bytes >> unit_shift,
+ unit_name,
+ percent_done);
+ 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);
+ tprintf(T("Writing \"%"TS"\": %"PRIu64" %"TS" of "
+ "%"PRIu64" %"TS" (%u%%) written\n"),
+ info->split.part_name,
+ 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) {
+ tprintf(T("Finished writing %u split WIM parts\n"),
+ info->split.cur_part_number);
+ }
+ break;
+ default:
+ break;
+ }
+ fflush(stdout);
+ return 0;
+}
+
+/* Open all the split WIM parts that correspond to a file glob.
+ *
+ * @first_part specifies the first part of the split WIM and it may be either
+ * included or omitted from the glob. */
+static int
+open_swms_from_glob(const tchar *swm_glob,
+ const tchar *first_part,
+ int open_flags,
+ WIMStruct ***additional_swms_ret,
+ unsigned *num_additional_swms_ret)
+{
+ unsigned num_additional_swms = 0;
+ WIMStruct **additional_swms = NULL;
+ glob_t globbuf;
+ int ret;
+
+ /* Warning: glob() is replaced in Windows native builds */
+ ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
+ if (ret != 0) {
+ if (ret == GLOB_NOMATCH) {
+ imagex_error(T("Found no files for glob \"%"TS"\""),
+ swm_glob);
+ } else {
+ imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
+ swm_glob);
+ }
+ ret = -1;
+ goto out;
+ }
+ num_additional_swms = globbuf.gl_pathc;
+ additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
+ if (!additional_swms) {
+ imagex_error(T("Out of memory"));
+ ret = -1;
+ goto out_globfree;
+ }
+ unsigned offset = 0;
+ for (unsigned i = 0; i < num_additional_swms; i++) {
+ if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
+ offset++;
+ continue;
+ }
+ ret = wimlib_open_wim(globbuf.gl_pathv[i],
+ open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
+ &additional_swms[i - offset],
+ imagex_progress_func);
+ if (ret != 0)
+ goto out_close_swms;
+ }
+ *additional_swms_ret = additional_swms;
+ *num_additional_swms_ret = num_additional_swms - offset;
+ ret = 0;
+ goto out_globfree;
+out_close_swms:
+ for (unsigned i = 0; i < num_additional_swms; i++)
+ wimlib_free(additional_swms[i]);
+ free(additional_swms);
+out_globfree:
+ globfree(&globbuf);
+out:
+ return ret;
+}
+
+
+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;
+ }
+}
+
+/*
+ * 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_IMAGE_FLAG_VERBOSE;
+ else if (!tstrcmp(option, T("--unix-data")))
+ cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
+ else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
+ cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
+ else if (!tstrcmp(option, T("--strict-acls")))
+ cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
+ else if (!tstrcmp(option, T("--dereference")))
+ cmd->add.add_flags |= WIMLIB_ADD_IMAGE_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 %u 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)