]> wimlib.net Git - wimlib/blobdiff - programs/imagex.c
Update docs
[wimlib] / programs / imagex.c
index b16579e74090782e7a50052dea7a02d563ce9cc6..c8fa94af86a9ab7ee6a58ef873564465260f6de6 100644 (file)
@@ -29,7 +29,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <getopt.h>
-#include <glob.h>
+
 #include <inttypes.h>
 #include <libgen.h>
 #include <limits.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <locale.h>
 
 #ifdef HAVE_ALLOCA_H
 #include <alloca.h>
 #endif
 
+#ifdef __WIN32__
+#  include "imagex-win32.h"
+#else
+#  include <glob.h>
+#endif
+
 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
 
 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
@@ -348,7 +355,7 @@ enum {
 /*
  * Parses a filename in the source list file format.  (See the man page for
  * 'imagex capture' for details on this format and the meaning.)  Accepted
- * formats for filenames are an unquoted string, whitespace-delimited, or a
+ * formats for filenames are an unquoted string (whitespace-delimited), or a
  * double or single-quoted string.
  *
  * @line_p:  Pointer to the pointer to the line of data.  Will be updated
@@ -494,7 +501,9 @@ parse_source_list(char *source_list_contents, size_t source_list_nbytes,
        p = source_list_contents;
        j = 0;
        for (i = 0; i < nlines; i++) {
-               char *endp = strchr(p, '\n');
+               /* XXX: Could use rawmemchr() here instead, but it may not be
+                * available on all platforms. */
+               char *endp = memchr(p, '\n', source_list_nbytes);
                size_t len = endp - p + 1;
                *endp = '\0';
                if (!is_comment_line(p, len)) {
@@ -731,6 +740,7 @@ static int open_swms_from_glob(const char *swm_glob,
        glob_t globbuf;
        int ret;
 
+       /* Warning: glob() is replaced in Windows native builds */
        ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
        if (ret != 0) {
                if (ret == GLOB_NOMATCH) {
@@ -891,11 +901,17 @@ static int imagex_apply(int argc, char **argv)
                }
        }
 
+#ifdef __WIN32__
+       win32_acquire_restore_privileges();
+#endif
        ret = wimlib_extract_image(w, image, target, extract_flags,
                                   additional_swms, num_additional_swms,
                                   imagex_progress_func);
        if (ret == 0)
                printf("Done applying WIM image.\n");
+#ifdef __WIN32__
+       win32_release_restore_privileges();
+#endif
 out:
        wimlib_free(w);
        if (additional_swms) {
@@ -1072,6 +1088,9 @@ static int imagex_capture_or_append(int argc, char **argv)
                        }
                }
        }
+#ifdef __WIN32__
+       win32_acquire_capture_privileges();
+#endif
 
        ret = wimlib_add_image_multisource(w, capture_sources,
                                           num_sources, name,
@@ -1082,17 +1101,17 @@ static int imagex_capture_or_append(int argc, char **argv)
                                           add_image_flags,
                                           imagex_progress_func);
        if (ret != 0)
-               goto out;
+               goto out_release_privs;
        cur_image = wimlib_get_num_images(w);
        if (desc) {
                ret = wimlib_set_image_descripton(w, cur_image, desc);
                if (ret != 0)
-                       goto out;
+                       goto out_release_privs;
        }
        if (flags_element) {
                ret = wimlib_set_image_flags(w, cur_image, flags_element);
                if (ret != 0)
-                       goto out;
+                       goto out_release_privs;
        }
        if (cmd == APPEND) {
                ret = wimlib_overwrite(w, write_flags, num_threads,
@@ -1105,6 +1124,10 @@ static int imagex_capture_or_append(int argc, char **argv)
                ret = 0;
        if (ret != 0)
                imagex_error("Failed to write the WIM file `%s'", wimfile);
+out_release_privs:
+#ifdef __WIN32__
+       win32_release_capture_privileges();
+#endif
 out:
        wimlib_free(w);
        free(config_str);
@@ -1320,9 +1343,10 @@ static int imagex_export(int argc, char **argv)
 
                wim_is_new = false;
                /* Destination file exists. */
-               if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
+
+               if (!S_ISREG(stbuf.st_mode)) {
                        imagex_error("`%s' is not a regular file",
-                                       dest_wimfile);
+                                    dest_wimfile);
                        ret = -1;
                        goto out;
                }
@@ -2097,6 +2121,8 @@ int main(int argc, char **argv)
        const struct imagex_command *cmd;
        int ret;
 
+       setlocale(LC_ALL, "");
+
        if (argc < 2) {
                imagex_error("No command specified");
                usage_all();
@@ -2122,14 +2148,14 @@ int main(int argc, char **argv)
        for_imagex_command(cmd) {
                if (strcmp(cmd->name, *argv) == 0) {
                        ret = cmd->func(argc, argv);
-                       goto out;
+                       goto out_check_write_error;
                }
        }
 
        imagex_error("Unrecognized command: `%s'", argv[0]);
        usage_all();
        return 1;
-out:
+out_check_write_error:
        /* For 'imagex info' and 'imagex dir', data printed to standard output
         * is part of the program's actual behavior and not just for
         * informational purposes, so we should set a failure exit status if
@@ -2141,7 +2167,7 @@ out:
                                ret = -1;
                }
        }
-
+out:
        /* Exit status (ret):  -1 indicates an error found by 'imagex' outside
         * of the wimlib library code.  0 indicates success.  > 0 indicates a
         * wimlib error code from which an error message can be printed. */