+ 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 (if given). */
+ if (argc >= 4) {
+ tchar *p = alloca((12 + tstrlen(argv[3]) + 1) * sizeof(tchar));
+ tsprintf(p, T("DESCRIPTION=%"TS), argv[3]);
+ ret = string_set_append(&image_properties, p);
+ if (ret)
+ goto out;
+ }
+
+ 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 = WIMLIB_WIM_ROOT_PATH;
+ capture_sources[0].reserved = 0;
+ num_sources = 1;
+ capture_sources_malloced = false;
+ source_list_contents = NULL;
+ }
+
+ /* Open the existing WIM, or create a new one. */
+ if (cmd == CMD_APPEND) {
+ ret = wimlib_open_wim_with_progress(wimfile,
+ open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
+ &wim,
+ imagex_progress_func,
+ NULL);
+ if (ret)
+ goto out_free_capture_sources;
+ } else {
+ ret = wimlib_create_new_wim(compression_type, &wim);
+ if (ret)
+ goto out_free_capture_sources;
+ wimlib_register_progress_function(wim, imagex_progress_func, NULL);
+ }
+
+ /* 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;
+ } else if ((add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
+
+ int ctype = compression_type;
+
+ if (cmd == CMD_APPEND) {
+ struct wimlib_wim_info info;
+ wimlib_get_wim_info(wim, &info);
+ ctype = info.compression_type;
+ }
+
+ if (ctype == WIMLIB_COMPRESSION_TYPE_XPRESS) {
+ ret = wimlib_set_output_chunk_size(wim, 4096);
+ if (ret)
+ goto out_free_wim;
+ }
+ }
+ if (solid_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
+ ret = wimlib_set_output_pack_compression_type(wim, solid_ctype);
+ if (ret)
+ goto out_free_wim;
+ }
+ if (solid_chunk_size != UINT32_MAX) {
+ ret = wimlib_set_output_pack_chunk_size(wim, solid_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_flags |= WIMLIB_ADD_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_with_progress(
+ base_wimfiles.strings[i], open_flags,
+ &base_wims[i], imagex_progress_func, NULL);
+ 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_with_progress(template_wimfile,
+ open_flags,
+ &template_wim,
+ imagex_progress_func,
+ NULL);
+ 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_file,
+ add_flags);
+ if (ret)
+ goto out_free_template_wim;
+
+ if (image_properties.num_strings || template_image_name_or_num) {
+ /* User asked to set additional image properties, or an image on
+ * which the added one is to be based has been specified with
+ * --update-of. */
+ struct wimlib_wim_info info;
+
+ wimlib_get_wim_info(wim, &info);
+
+ ret = apply_image_properties(&image_properties, wim,
+ info.image_count, NULL);
+ 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);
+ 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);
+ } else if (wimfile) {
+ ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
+ write_flags, num_threads);
+ } else {
+ ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
+ write_flags, num_threads);
+ }
+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_capture_sources:
+ if (capture_sources_malloced)
+ free(capture_sources);
+out_free_source_list_contents:
+ free(source_list_contents);
+out:
+ string_set_destroy(&image_properties);
+ string_set_destroy(&base_wimfiles);
+ return ret;
+
+out_usage:
+ usage(cmd, stderr);
+out_err:
+ ret = -1;
+ goto out;
+}
+
+/* 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;
+ case IMAGEX_UNSAFE_COMPACT_OPTION:
+ write_flags |= WIMLIB_WRITE_FLAG_UNSAFE_COMPACT;
+ 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_with_progress(wimfile, open_flags, &wim,
+ imagex_progress_func, NULL);
+ if (ret)