+out:
+ return ret;
+
+out_usage:
+ usage(CMD_UNMOUNT, stderr);
+ ret = -1;
+ goto out;
+}
+#endif /* WIM_MOUNTING_SUPPORTED */
+
+/*
+ * Add, delete, or rename files in a WIM image.
+ */
+static int
+imagex_update(int argc, tchar **argv, int cmd)
+{
+ const tchar *wimfile;
+ int image;
+ WIMStruct *wim;
+ int ret;
+ int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
+ int write_flags = 0;
+ int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
+ int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
+ int default_delete_flags = 0;
+ unsigned num_threads = 0;
+ int c;
+ tchar *cmd_file_contents;
+ size_t cmd_file_nchars;
+ struct wimlib_update_command *cmds;
+ size_t num_cmds;
+ tchar *command_str = NULL;
+
+ const tchar *config_file = NULL;
+ tchar *config_str;
+ struct wimlib_capture_config *config;
+
+ for_opt(c, update_options) {
+ switch (c) {
+ /* Generic or write options */
+ case IMAGEX_THREADS_OPTION:
+ num_threads = parse_num_threads(optarg);
+ if (num_threads == UINT_MAX)
+ goto out_err;
+ break;
+ case IMAGEX_CHECK_OPTION:
+ open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
+ write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
+ break;
+ case IMAGEX_REBUILD_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
+ break;
+ case IMAGEX_COMMAND_OPTION:
+ if (command_str) {
+ imagex_error(T("--command may only be specified "
+ "one time. Please provide\n"
+ " the update commands "
+ "on standard input instead."));
+ goto out_err;
+ }
+ command_str = tstrdup(optarg);
+ if (!command_str) {
+ imagex_error(T("Out of memory!"));
+ goto out_err;
+ }
+ break;
+ /* Default delete options */
+ case IMAGEX_FORCE_OPTION:
+ default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
+ break;
+ case IMAGEX_RECURSIVE_OPTION:
+ default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
+ break;
+
+ /* Global add option */
+ case IMAGEX_CONFIG_OPTION:
+ default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
+ config_file = optarg;
+ break;
+
+ /* Default add options */
+ case IMAGEX_VERBOSE_OPTION:
+ default_add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
+ break;
+ case IMAGEX_DEREFERENCE_OPTION:
+ default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
+ break;
+ case IMAGEX_UNIX_DATA_OPTION:
+ default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
+ break;
+ case IMAGEX_NO_ACLS_OPTION:
+ default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
+ break;
+ case IMAGEX_STRICT_ACLS_OPTION:
+ default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
+ break;
+ default:
+ goto out_usage;
+ }
+ }
+ argv += optind;
+ argc -= optind;
+
+ if (argc != 1 && argc != 2)
+ goto out_usage;
+ wimfile = argv[0];
+
+ ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
+ if (ret)
+ goto out_free_command_str;
+
+ if (argc >= 2) {
+ /* Image explicitly specified. */
+ image = wimlib_resolve_image(wim, argv[1]);
+ ret = verify_image_exists_and_is_single(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."),
+ wimfile, info.image_count);
+ wimlib_free(wim);
+ goto out_usage;
+ }
+ image = 1;
+ }
+
+ /* Parse capture configuration file if specified */
+ if (config_file) {
+ size_t config_len;
+
+ config_str = file_get_text_contents(config_file, &config_len);
+ if (!config_str) {
+ ret = -1;
+ goto out_wimlib_free;
+ }
+
+ config = alloca(sizeof(*config));
+ ret = parse_capture_config(&config_str, config_len, config);
+ if (ret)
+ goto out_free_config;
+ } else {
+ config = NULL;
+ default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
+ }
+
+ /* Read update commands from standard input, or the command string if
+ * specified. */
+ if (command_str) {
+ cmd_file_contents = NULL;
+ cmds = parse_update_command_file(&command_str, tstrlen(command_str),
+ &num_cmds);
+ } else {
+ if (isatty(STDIN_FILENO)) {
+ tputs(T("Reading update commands from standard input..."));
+ recommend_man_page(CMD_UPDATE, stdout);
+ }
+ cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
+ if (!cmd_file_contents) {
+ ret = -1;
+ goto out_free_config;
+ }
+
+ /* Parse the update commands */
+ cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
+ &num_cmds);
+ }
+ if (!cmds) {
+ ret = -1;
+ goto out_free_cmd_file_contents;
+ }
+
+ /* Set default flags and capture config on the update commands */
+ for (size_t i = 0; i < num_cmds; i++) {
+ switch (cmds[i].op) {
+ case WIMLIB_UPDATE_OP_ADD:
+ cmds[i].add.add_flags |= default_add_flags;
+ cmds[i].add.config = config;
+ break;
+ case WIMLIB_UPDATE_OP_DELETE:
+ cmds[i].delete_.delete_flags |= default_delete_flags;
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* Execute the update commands */
+ ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
+ imagex_progress_func);
+ if (ret)
+ goto out_free_cmds;
+
+ /* Overwrite the updated WIM */
+ ret = wimlib_overwrite(wim, write_flags, num_threads,
+ imagex_progress_func);
+out_free_cmds:
+ free(cmds);
+out_free_cmd_file_contents:
+ free(cmd_file_contents);
+out_free_config:
+ if (config) {
+ free(config->exclusion_pats.pats);
+ free(config->exclusion_exception_pats.pats);
+ free(config_str);
+ }
+out_wimlib_free:
+ wimlib_free(wim);
+out_free_command_str:
+ free(command_str);