]> wimlib.net Git - wimlib/blobdiff - programs/imagex.c
Implement wimlib_iterate_dir_tree()
[wimlib] / programs / imagex.c
index 9ee6df34ed630e99198f13584456f2f2c823d74a..114424e396d09630e7b4f183cfeedd8c3dd185d2 100644 (file)
 #  include "imagex-win32.h"
 #  define tbasename    win32_wbasename
 #  define tglob                win32_wglob
+#  define OS_PREFERRED_PATH_SEPARATOR L'\\'
+#  define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
 #else /* __WIN32__ */
 #  include <glob.h>
 #  include <getopt.h>
 #  include <langinfo.h>
 #  define tbasename    basename
 #  define tglob                glob
+#  define OS_PREFERRED_PATH_SEPARATOR '/'
+#  define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
 #endif /* !__WIN32 */
 
 
@@ -104,7 +108,7 @@ IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
 "                    (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
 "                    [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
 "                    [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
-"                    [--force-all-files]\n"
+"                    [--include-invalid-names]\n"
 ),
 [CAPTURE] =
 T(
@@ -135,7 +139,7 @@ T(
 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
 "              [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
 "              [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
-"              [--force-all-files]\n"
+"              [--include-invalid-names]\n"
 ),
 [INFO] =
 T(
@@ -176,7 +180,7 @@ IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
 T(
 IMAGEX_PROGNAME" update WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
 "                       [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
-"                       [DEFAULT_DELETE_OPTIONS] < CMDFILE\n"
+"                       [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
 ),
 };
 
@@ -199,6 +203,7 @@ enum {
        IMAGEX_ALLOW_OTHER_OPTION,
        IMAGEX_BOOT_OPTION,
        IMAGEX_CHECK_OPTION,
+       IMAGEX_COMMAND_OPTION,
        IMAGEX_COMMIT_OPTION,
        IMAGEX_COMPRESS_OPTION,
        IMAGEX_CONFIG_OPTION,
@@ -208,9 +213,9 @@ enum {
        IMAGEX_EXTRACT_XML_OPTION,
        IMAGEX_FLAGS_OPTION,
        IMAGEX_FORCE_OPTION,
-       IMAGEX_FORCE_ALL_FILES_OPTION,
        IMAGEX_HARDLINK_OPTION,
        IMAGEX_HEADER_OPTION,
+       IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
        IMAGEX_LAZY_OPTION,
        IMAGEX_LOOKUP_TABLE_OPTION,
        IMAGEX_METADATA_OPTION,
@@ -246,7 +251,7 @@ static const struct option apply_options[] = {
        {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
        {T("rpfix"),       no_argument,       NULL, IMAGEX_RPFIX_OPTION},
        {T("norpfix"),     no_argument,       NULL, IMAGEX_NORPFIX_OPTION},
-       {T("force-all-files"), no_argument,       NULL, IMAGEX_FORCE_ALL_FILES_OPTION},
+       {T("include-invalid-names"), no_argument,       NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
        {NULL, 0, NULL, 0},
 };
 static const struct option capture_or_append_options[] = {
@@ -294,7 +299,7 @@ static const struct option extract_options[] = {
        {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
        {T("dest-dir"),    required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
        {T("to-stdout"),   no_argument,       NULL, IMAGEX_TO_STDOUT_OPTION},
-       {T("force-all-files"), no_argument,       NULL, IMAGEX_FORCE_ALL_FILES_OPTION},
+       {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
        {NULL, 0, NULL, 0},
 };
 
@@ -353,6 +358,7 @@ static const struct option update_options[] = {
        {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
        {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
        {T("rebuild"),     no_argument,       NULL, IMAGEX_REBUILD_OPTION},
+       {T("command"),     required_argument, NULL, IMAGEX_COMMAND_OPTION},
 
        /* Default delete options */
        {T("force"),       no_argument,       NULL, IMAGEX_FORCE_OPTION},
@@ -368,6 +374,7 @@ static const struct option update_options[] = {
        {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
        {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
        {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
+
        {NULL, 0, NULL, 0},
 };
 
@@ -973,18 +980,6 @@ stdin_get_text_contents(size_t *num_tchars_ret)
        return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
 }
 
-/* Return 0 if a path names a file to which the current user has write access;
- * -1 otherwise (and print an error message). */
-static int
-file_writable(const tchar *path)
-{
-       int ret;
-       ret = taccess(path, W_OK);
-       if (ret != 0)
-               imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
-       return ret;
-}
-
 #define TO_PERCENT(numerator, denominator) \
        (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
 
@@ -1062,7 +1057,8 @@ imagex_progress_func(enum wimlib_progress_msg msg,
        case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
                tprintf(T("Scanning \"%"TS"\""), info->scan.source);
                if (*info->scan.wim_target_path) {
-                       tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
+                       tprintf(T(" (loading as WIM path: "
+                                 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
                               info->scan.wim_target_path);
                } else {
                        tprintf(T(" (loading as root of WIM image)...\n"));
@@ -1080,8 +1076,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);
-               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,
@@ -1116,7 +1112,8 @@ imagex_progress_func(enum wimlib_progress_msg msg,
                        info->extract.target);
                break;
        case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
-               tprintf(T("Extracting \"%"TS"\" from image %d (\"%"TS"\") "
+               tprintf(T("Extracting "
+                         "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
                          "in \"%"TS"\" to \"%"TS"\"\n"),
                        info->extract.extract_root_wim_source_path,
                        info->extract.image,
@@ -1189,6 +1186,25 @@ imagex_progress_func(enum wimlib_progress_msg msg,
                                info->split.cur_part_number);
                }
                break;
+       case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
+               switch (info->update.command->op) {
+               case WIMLIB_UPDATE_OP_DELETE:
+                       tprintf(T("Deleted WIM path "
+                                 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
+                               info->update.command->delete.wim_path);
+                       break;
+               case WIMLIB_UPDATE_OP_RENAME:
+                       tprintf(T("Renamed WIM path "
+                                 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
+                                 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
+                               info->update.command->rename.wim_source_path,
+                               info->update.command->rename.wim_target_path);
+                       break;
+               case WIMLIB_UPDATE_OP_ADD:
+               default:
+                       break;
+               }
+               break;
        default:
                break;
        }
@@ -1533,7 +1549,7 @@ imagex_apply(int argc, tchar **argv)
                case IMAGEX_RPFIX_OPTION:
                        extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
                        break;
-               case IMAGEX_FORCE_ALL_FILES_OPTION:
+               case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
                        extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
                        extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
                        break;
@@ -1628,7 +1644,7 @@ static int
 imagex_capture_or_append(int argc, tchar **argv)
 {
        int c;
-       int open_flags = 0;
+       int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
        int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
        int write_flags = 0;
        int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
@@ -1656,6 +1672,7 @@ imagex_capture_or_append(int argc, tchar **argv)
        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) {
@@ -1727,13 +1744,16 @@ imagex_capture_or_append(int argc, tchar **argv)
 
        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. */
+                * 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_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_defaulted = true;
        }
        /* Image description defaults to NULL if not given. */
        desc = (argc >= 4) ? argv[3] : NULL;
@@ -1812,6 +1832,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
@@ -1843,8 +1877,6 @@ imagex_capture_or_append(int argc, tchar **argv)
                ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
                                   num_threads, imagex_progress_func);
        }
-       if (ret == WIMLIB_ERR_REOPEN)
-               ret = 0;
        if (ret != 0)
                imagex_error(T("Failed to write the WIM file \"%"TS"\""),
                             wimfile);
@@ -1873,7 +1905,7 @@ static int
 imagex_delete(int argc, tchar **argv)
 {
        int c;
-       int open_flags = 0;
+       int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
        int write_flags = 0;
        const tchar *wimfile;
        const tchar *image_num_or_name;
@@ -1909,10 +1941,6 @@ imagex_delete(int argc, tchar **argv)
        wimfile = argv[0];
        image_num_or_name = argv[1];
 
-       ret = file_writable(wimfile);
-       if (ret != 0)
-               return ret;
-
        ret = wimlib_open_wim(wimfile, open_flags, &w,
                              imagex_progress_func);
        if (ret != 0)
@@ -1931,8 +1959,6 @@ imagex_delete(int argc, tchar **argv)
        }
 
        ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
-       if (ret == WIMLIB_ERR_REOPEN)
-               ret = 0;
        if (ret != 0) {
                imagex_error(T("Failed to write the file \"%"TS"\" with image "
                               "deleted"), wimfile);
@@ -1942,6 +1968,13 @@ out:
        return ret;
 }
 
+static int
+print_full_path(const struct wimlib_wim_dentry *wdentry, void *_ignore)
+{
+       tprintf(T("%"TS"\n"), wdentry->full_path);
+       return 0;
+}
+
 /* Print the files contained in an image(s) in a WIM file. */
 static int
 imagex_dir(int argc, tchar **argv)
@@ -1988,7 +2021,9 @@ imagex_dir(int argc, tchar **argv)
                image = 1;
        }
 
-       ret = wimlib_print_files(w, image);
+       ret = wimlib_iterate_dir_tree(w, image, T(""),
+                                     WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
+                                     print_full_path, NULL);
 out:
        wimlib_free(w);
        return ret;
@@ -2084,12 +2119,8 @@ imagex_export(int argc, tchar **argv)
                        ret = -1;
                        goto out;
                }
-               ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
-                                     imagex_progress_func);
-               if (ret != 0)
-                       goto out;
-
-               ret = file_writable(dest_wimfile);
+               ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
+                                     &dest_w, imagex_progress_func);
                if (ret != 0)
                        goto out;
 
@@ -2148,8 +2179,6 @@ imagex_export(int argc, tchar **argv)
                ret = wimlib_overwrite(dest_w, write_flags, num_threads,
                                       imagex_progress_func);
 out:
-       if (ret == WIMLIB_ERR_REOPEN)
-               ret = 0;
        wimlib_free(src_w);
        wimlib_free(dest_w);
        if (additional_swms) {
@@ -2212,8 +2241,8 @@ prepare_extract_commands(tchar **paths, unsigned num_paths,
                                free_extract_commands(cmds, num_cmds, dest_dir);
                                return NULL;
                        }
-                       tsprintf(cmds[i].fs_dest_path, T("%"TS"/%"TS), dest_dir,
-                                tbasename(paths[i]));
+                       tsprintf(cmds[i].fs_dest_path, T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
+                                dest_dir, tbasename(paths[i]));
                }
        }
        *num_cmds_ret = num_cmds;
@@ -2268,7 +2297,7 @@ imagex_extract(int argc, tchar **argv)
                        extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
                        imagex_be_quiet = true;
                        break;
-               case IMAGEX_FORCE_ALL_FILES_OPTION:
+               case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
                        extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
                        extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
                        break;
@@ -2545,11 +2574,7 @@ imagex_info(int argc, tchar **argv)
        } else {
 
                /* Modification operations */
-               if (total_parts != 1) {
-                       imagex_error(T("Modifying a split WIM is not supported."));
-                       ret = -1;
-                       goto out;
-               }
+
                if (image == WIMLIB_ALL_IMAGES)
                        image = 1;
 
@@ -2568,7 +2593,9 @@ imagex_info(int argc, tchar **argv)
                        } else {
                                tprintf(T("Marking image %d as bootable.\n"),
                                        image);
-                               wimlib_set_boot_idx(w, image);
+                               ret = wimlib_set_boot_idx(w, image);
+                               if (ret)
+                                       goto out;
                        }
                }
                if (new_name) {
@@ -2607,21 +2634,12 @@ imagex_info(int argc, tchar **argv)
                if (boot || new_name || new_desc ||
                    (check && !wimlib_has_integrity_table(w)))
                {
-                       int write_flags;
-
-                       ret = file_writable(wimfile);
-                       if (ret != 0)
-                               goto out;
+                       int write_flags = 0;
 
                        if (check)
-                               write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
-                       else
-                               write_flags = 0;
-
+                               write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
                        ret = wimlib_overwrite(w, write_flags, 1,
                                               imagex_progress_func);
-                       if (ret == WIMLIB_ERR_REOPEN)
-                               ret = 0;
                } else {
                        tprintf(T("The file \"%"TS"\" was not modified because nothing "
                                  "needed to be done.\n"), wimfile);
@@ -2690,8 +2708,10 @@ imagex_mount_rw_or_ro(int argc, tchar **argv)
        unsigned num_additional_swms = 0;
        const tchar *staging_dir = NULL;
 
-       if (!tstrcmp(argv[0], T("mountrw")))
+       if (!tstrcmp(argv[0], T("mountrw"))) {
                mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
+               open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
+       }
 
        for_opt(c, mount_options) {
                switch (c) {
@@ -2770,12 +2790,6 @@ imagex_mount_rw_or_ro(int argc, tchar **argv)
                        goto out;
        }
 
-       if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
-               ret = file_writable(wimfile);
-               if (ret != 0)
-                       goto out;
-       }
-
        ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
                                 num_additional_swms, staging_dir);
        if (ret != 0) {
@@ -2803,7 +2817,7 @@ static int
 imagex_optimize(int argc, tchar **argv)
 {
        int c;
-       int open_flags = 0;
+       int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
        int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
        int ret;
        WIMStruct *w;
@@ -2841,10 +2855,6 @@ imagex_optimize(int argc, tchar **argv)
 
        wimfile = argv[0];
 
-       ret = file_writable(wimfile);
-       if (ret)
-               return ret;
-
        ret = wimlib_open_wim(wimfile, open_flags, &w,
                              imagex_progress_func);
        if (ret)
@@ -2978,18 +2988,19 @@ imagex_update(int argc, tchar **argv)
        int image;
        WIMStruct *wim;
        int ret;
-       int open_flags = 0;
+       int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
        int write_flags = 0;
-       int update_flags = 0;
+       int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
        int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
        int default_delete_flags = 0;
        unsigned num_threads = 0;
        int c;
-       tchar *cmd_file_contents;
+       tchar *cmd_file_contents = NULL;
        size_t cmd_file_nchars;
        struct wimlib_update_command *cmds;
        size_t num_cmds;
        int num_images;
+       tchar *command_str = NULL;
 
        const tchar *config_file = NULL;
        tchar *config_str;
@@ -3012,7 +3023,22 @@ imagex_update(int argc, tchar **argv)
                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."));
+                               ret = -1;
+                               goto out;
+                       }
+                       command_str = tstrdup(optarg);
+                       if (!command_str) {
+                               imagex_error(T("Out of memory!"));
+                               ret = -1;
+                               goto out;
+                       }
+                       break;
                /* Default delete options */
                case IMAGEX_FORCE_OPTION:
                        default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
@@ -3053,10 +3079,6 @@ imagex_update(int argc, tchar **argv)
                goto out_usage;
        wimfile = argv[0];
 
-       ret = file_writable(wimfile);
-       if (ret)
-               goto out;
-
        ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
        if (ret)
                goto out;
@@ -3100,31 +3122,39 @@ imagex_update(int argc, tchar **argv)
                config = &default_capture_config;
        }
 
-       /* Read update commands from standard input */
-       if (isatty(STDIN_FILENO)) {
-               tputs(T("Reading update commands from standard input..."));
-               recommend_man_page(T("update"));
-       }
-       cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
-       if (!cmd_file_contents) {
-               ret = -1;
-               goto out_free_config;
-       }
+       /* Read update commands from standard input, or the command string if
+        * specified.  */
+       if (command_str) {
+               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(T("update"));
+               }
+               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);
+               /* 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 */
+       bool have_add_command = false;
        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;
+                       have_add_command = true;
                        break;
                case WIMLIB_UPDATE_OP_DELETE:
                        cmds[i].delete.delete_flags |= default_delete_flags;
@@ -3135,7 +3165,8 @@ imagex_update(int argc, tchar **argv)
        }
 
 #ifdef __WIN32__
-       win32_acquire_capture_privileges();
+       if (have_add_command)
+               win32_acquire_capture_privileges();
 #endif
 
        /* Execute the update commands */
@@ -3149,9 +3180,9 @@ imagex_update(int argc, tchar **argv)
                               imagex_progress_func);
 out_release_privs:
 #ifdef __WIN32__
-       win32_release_capture_privileges();
+       if (have_add_command)
+               win32_release_capture_privileges();
 #endif
-out_free_cmds:
        free(cmds);
 out_free_cmd_file_contents:
        free(cmd_file_contents);
@@ -3164,6 +3195,7 @@ out_free_config:
 out_wimlib_free:
        wimlib_free(wim);
 out:
+       free(command_str);
        return ret;
 out_usage:
        usage(UPDATE);