]> wimlib.net Git - wimlib/blobdiff - programs/imagex.c
wimlib-imagex: try harder to optimize out opening template WIM
[wimlib] / programs / imagex.c
index 5e7dc6dd862974bf6d7e023a1647e922d497d77e..69d7a6c74e7e5760a04c30d44d2d4f5934d1a3b9 100644 (file)
@@ -6,7 +6,7 @@
  */
 
 /*
- * Copyright (C) 2012-2017 Eric Biggers
+ * Copyright (C) 2012-2018 Eric Biggers
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -1867,7 +1867,7 @@ imagex_capture_or_append(int argc, tchar **argv, int cmd)
        STRING_LIST(base_wimfiles);
        WIMStruct **base_wims;
 
-       WIMStruct *template_wim;
+       WIMStruct *template_wim = NULL;
        const tchar *template_wimfile = NULL;
        const tchar *template_image_name_or_num = NULL;
        int template_image = WIMLIB_NO_IMAGE;
@@ -2286,13 +2286,19 @@ imagex_capture_or_append(int argc, tchar **argv, int cmd)
         * 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) {
+               if (cmd == CMD_APPEND && !tstrcmp(template_wimfile, wimfile)) {
                        template_wim = wim;
                } else {
+                       for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
+                               if (!tstrcmp(template_wimfile,
+                                            base_wimfiles.strings[i])) {
+                                       template_wim = base_wims[i];
+                                       break;
+                               }
+                       }
+               }
+
+               if (!template_wim) {
                        ret = wimlib_open_wim_with_progress(template_wimfile,
                                                            open_flags,
                                                            &template_wim,
@@ -2324,8 +2330,6 @@ imagex_capture_or_append(int argc, tchar **argv, int cmd)
                                                        template_wimfile);
                if (ret)
                        goto out_free_template_wim;
-       } else {
-               template_wim = NULL;
        }
 
        ret = wimlib_add_image_multisource(wim,
@@ -2377,10 +2381,13 @@ imagex_capture_or_append(int argc, tchar **argv, int cmd)
                                         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);
+       /* 'template_wim' may alias 'wim' or any of the 'base_wims' */
+       if (template_wim == wim)
+               goto out_free_base_wims;
+       for (size_t i = 0; i < base_wimfiles.num_strings; i++)
+               if (template_wim == base_wims[i])
+                       goto out_free_base_wims;
+       wimlib_free(template_wim);
 out_free_base_wims:
        for (size_t i = 0; i < base_wimfiles.num_strings; i++)
                wimlib_free(base_wims[i]);
@@ -4520,20 +4527,19 @@ static const tchar *get_cmd_string(int cmd, bool only_short_form)
 static void
 version(void)
 {
-       static const tchar * const s =
+       static const tchar * const fmt =
        T(
-"wimlib-imagex (distributed with " PACKAGE " " PACKAGE_VERSION ")\n"
-"Copyright (C) 2012-2017 Eric Biggers\n"
+"wimlib-imagex " PACKAGE_VERSION " (using wimlib %"TS")\n"
+"Copyright (C) 2012-2018 Eric Biggers\n"
 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
 "This is free software: you are free to change and redistribute it.\n"
 "There is NO WARRANTY, to the extent permitted by law.\n"
 "\n"
 "Report bugs to "PACKAGE_BUGREPORT".\n"
        );
-       tfputs(s, stdout);
+       tfprintf(stdout, fmt, wimlib_get_version_string());
 }
 
-
 static void
 do_common_options(int *argc_p, tchar **argv, int cmd)
 {