+ 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_wimfile) {
+ /* Capturing delta WIM: default to base WIM. */
+ template_wimfile = base_wimfile;
+ } 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. */
+ 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;
+
+#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 WIM
+ * before adding the new image. */
+ if (base_wimfile) {
+ ret = wimlib_open_wim(base_wimfile, open_flags,
+ &base_wim, imagex_progress_func);
+ if (ret)
+ goto out_free_wim;
+
+ imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
+ base_wimfile);
+
+ ret = wimlib_reference_resources(wim, &base_wim, 1, 0);
+ if (ret)
+ goto out_free_base_wim;
+ } else {
+ base_wim = 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 (template_wimfile == base_wimfile) {
+ template_wim = base_wim;
+ } 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_wim;
+ }
+
+ 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(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;
+ }