]> wimlib.net Git - wimlib/blobdiff - programs/imagex.c
wimlib-imagex append: Generate unique name if default name conflicts
[wimlib] / programs / imagex.c
index 1ee31868161c22aa7cc58910b941b6cc48f9512b..1a3cbbd5a05176975e22ea78764d7b8e43bdbeb6 100644 (file)
@@ -1083,8 +1083,8 @@ imagex_progress_func(enum wimlib_progress_msg msg,
                unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
                percent_done = TO_PERCENT(info->integrity.completed_bytes,
                                          info->integrity.total_bytes);
                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"),
+               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.filename,
                        info->integrity.completed_bytes >> unit_shift,
                        unit_name,
@@ -1659,6 +1659,7 @@ imagex_capture_or_append(int argc, tchar **argv)
        bool capture_sources_malloced;
        struct wimlib_capture_source *capture_sources;
        size_t num_sources;
        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) {
 
        for_opt(c, capture_or_append_options) {
                switch (c) {
@@ -1730,13 +1731,16 @@ imagex_capture_or_append(int argc, tchar **argv)
 
        if (argc >= 3) {
                name = argv[2];
 
        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
        } else {
                /* Set default name to SOURCE argument, omitting any directory
                 * prefixes and trailing slashes.  This requires making a copy
-                * of @source. */
+                * of @source.  Leave some free characters at the end in case we
+                * append a number to keep the name unique. */
                source_name_len = tstrlen(source);
                source_name_len = tstrlen(source);
-               source_copy = alloca((source_name_len + 1) * sizeof(tchar));
+               source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
                name = tbasename(tstrcpy(source_copy, source));
                name = tbasename(tstrcpy(source_copy, source));
+               name_defaulted = true;
        }
        /* Image description defaults to NULL if not given. */
        desc = (argc >= 4) ? argv[3] : NULL;
        }
        /* Image description defaults to NULL if not given. */
        desc = (argc >= 4) ? argv[3] : NULL;
@@ -1815,6 +1819,20 @@ imagex_capture_or_append(int argc, tchar **argv)
                        }
                }
        }
                        }
                }
        }
+
+       if (cmd == APPEND && name_defaulted) {
+               /* 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. */
+               unsigned long conflict_idx;
+               tchar *name_end = tstrchr(name, T('\0'));
+               for (conflict_idx = 1;
+                    wimlib_image_name_in_use(w, name);
+                    conflict_idx++)
+               {
+                       tsprintf(name_end, T(" (%lu)"), conflict_idx);
+               }
+       }
 #ifdef __WIN32__
        win32_acquire_capture_privileges();
 #endif
 #ifdef __WIN32__
        win32_acquire_capture_privileges();
 #endif
@@ -3176,7 +3194,6 @@ out_release_privs:
 #ifdef __WIN32__
        win32_release_capture_privileges();
 #endif
 #ifdef __WIN32__
        win32_release_capture_privileges();
 #endif
-out_free_cmds:
        free(cmds);
 out_free_cmd_file_contents:
        free(cmd_file_contents);
        free(cmds);
 out_free_cmd_file_contents:
        free(cmd_file_contents);