]> wimlib.net Git - wimlib/blobdiff - programs/imagex.c
imagex_extract(): Set WIMLIB_EXTRACT_FLAG_NORPFIX
[wimlib] / programs / imagex.c
index 372e115b64509d8b6a85ba72061038160c353aef..59335996c57b7bf9a9d3950d7ef61dc073be59e6 100644 (file)
@@ -68,6 +68,7 @@ enum imagex_op_type {
        DELETE,
        DIR,
        EXPORT,
+       EXTRACT,
        INFO,
        JOIN,
        MOUNT,
@@ -80,6 +81,8 @@ enum imagex_op_type {
 static void usage(int cmd_type);
 static void usage_all();
 
+static bool imagex_be_quiet = false;
+
 
 static const tchar *usage_strings[] = {
 [APPEND] =
@@ -89,13 +92,14 @@ IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
 "                     [--verbose] [--dereference] [--config=FILE]\n"
 "                     [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
 "                     [--source-list] [--no-acls] [--strict-acls]\n"
+"                     [--rpfix] [--norpfix]\n"
 ),
 [APPLY] =
 T(
 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]\n"
+"                    [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
 ),
 [CAPTURE] =
 T(
@@ -104,6 +108,7 @@ IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
 "                      [--flags EDITION_ID] [--verbose] [--dereference]\n"
 "                      [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
 "                      [--source-list] [--no-acls] [--strict-acls]\n"
+"                      [--rpfix] [--norpfix]\n"
 ),
 [DELETE] =
 T(
@@ -120,6 +125,12 @@ IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
 "              [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
 "              [--threads=NUM_THREADS] [--rebuild]\n"
 ),
+[EXTRACT] =
+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"
+),
 [INFO] =
 T(
 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
@@ -166,16 +177,19 @@ enum {
        IMAGEX_CONFIG_OPTION,
        IMAGEX_DEBUG_OPTION,
        IMAGEX_DEREFERENCE_OPTION,
+       IMAGEX_DEST_DIR_OPTION,
        IMAGEX_EXTRACT_XML_OPTION,
        IMAGEX_FLAGS_OPTION,
        IMAGEX_HARDLINK_OPTION,
        IMAGEX_HEADER_OPTION,
        IMAGEX_LOOKUP_TABLE_OPTION,
        IMAGEX_METADATA_OPTION,
+       IMAGEX_NORPFIX_OPTION,
        IMAGEX_NO_ACLS_OPTION,
        IMAGEX_REBULID_OPTION,
        IMAGEX_RECOMPRESS_OPTION,
        IMAGEX_REF_OPTION,
+       IMAGEX_RPFIX_OPTION,
        IMAGEX_SOFT_OPTION,
        IMAGEX_SOURCE_LIST_OPTION,
        IMAGEX_STAGING_DIR_OPTION,
@@ -183,6 +197,7 @@ enum {
        IMAGEX_STRICT_ACLS_OPTION,
        IMAGEX_SYMLINK_OPTION,
        IMAGEX_THREADS_OPTION,
+       IMAGEX_TO_STDOUT_OPTION,
        IMAGEX_UNIX_DATA_OPTION,
        IMAGEX_VERBOSE_OPTION,
        IMAGEX_XML_OPTION,
@@ -198,6 +213,8 @@ static const struct option apply_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},
+       {T("rpfix"),       no_argument,       NULL, IMAGEX_RPFIX_OPTION},
+       {T("norpfix"),     no_argument,       NULL, IMAGEX_NORPFIX_OPTION},
        {NULL, 0, NULL, 0},
 };
 static const struct option capture_or_append_options[] = {
@@ -215,6 +232,8 @@ static const struct option capture_or_append_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},
+       {T("rpfix"),       no_argument,       NULL, IMAGEX_RPFIX_OPTION},
+       {T("norpfix"),     no_argument,       NULL, IMAGEX_NORPFIX_OPTION},
        {NULL, 0, NULL, 0},
 };
 static const struct option delete_options[] = {
@@ -233,6 +252,19 @@ static const struct option export_options[] = {
        {NULL, 0, NULL, 0},
 };
 
+static const struct option extract_options[] = {
+       {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
+       {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
+       {T("ref"),         required_argument, NULL, IMAGEX_REF_OPTION},
+       {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
+       {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},
+       {T("dest-dir"),    required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
+       {T("to-stdout"),   no_argument,       NULL, IMAGEX_TO_STDOUT_OPTION},
+       {NULL, 0, NULL, 0},
+};
+
 static const struct option info_options[] = {
        {T("boot"),         no_argument,       NULL, IMAGEX_BOOT_OPTION},
        {T("check"),        no_argument,       NULL, IMAGEX_CHECK_OPTION},
@@ -922,6 +954,8 @@ imagex_progress_func(enum wimlib_progress_msg msg,
                     const union wimlib_progress_info *info)
 {
        unsigned percent_done;
+       if (imagex_be_quiet)
+               return 0;
        switch (msg) {
        case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
                percent_done = TO_PERCENT(info->write_streams.completed_bytes,
@@ -983,7 +1017,7 @@ imagex_progress_func(enum wimlib_progress_msg msg,
                        tputchar(T('\n'));
                break;
        case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
-               tprintf(T("Applying image %d (%"TS") from \"%"TS"\" "
+               tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
                          "to %"TS" \"%"TS"\"\n"),
                        info->extract.image,
                        info->extract.image_name,
@@ -992,6 +1026,15 @@ imagex_progress_func(enum wimlib_progress_msg msg,
                         T("NTFS volume") : T("directory")),
                        info->extract.target);
                break;
+       case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
+               tprintf(T("Extracting \"%"TS"\" from image %d (\"%"TS"\") "
+                         "in \"%"TS"\" to \"%"TS"\"\n"),
+                       info->extract.extract_root_wim_source_path,
+                       info->extract.image,
+                       info->extract.image_name,
+                       info->extract.wimfile_name,
+                       info->extract.target);
+               break;
        /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
                /*tprintf(T("Applying directory structure to %"TS"\n"),*/
                        /*info->extract.target);*/
@@ -1011,7 +1054,8 @@ imagex_progress_func(enum wimlib_progress_msg msg,
                tprintf(T("%"TS"\n"), info->extract.cur_path);
                break;
        case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
-               tprintf(T("Setting timestamps on all extracted files...\n"));
+               if (info->extract.extract_root_wim_source_path[0] == T('\0'))
+                       tprintf(T("Setting timestamps on all extracted files...\n"));
                break;
        case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
                if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
@@ -1178,6 +1222,12 @@ imagex_apply(int argc, tchar **argv)
                case IMAGEX_STRICT_ACLS_OPTION:
                        extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
                        break;
+               case IMAGEX_NORPFIX_OPTION:
+                       extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
+                       break;
+               case IMAGEX_RPFIX_OPTION:
+                       extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
+                       break;
                default:
                        usage(APPLY);
                        return -1;
@@ -1344,6 +1394,12 @@ imagex_capture_or_append(int argc, tchar **argv)
                case IMAGEX_STRICT_ACLS_OPTION:
                        add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
                        break;
+               case IMAGEX_RPFIX_OPTION:
+                       add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
+                       break;
+               case IMAGEX_NORPFIX_OPTION:
+                       add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
+                       break;
                default:
                        usage(cmd);
                        return -1;
@@ -1795,6 +1851,190 @@ out:
        return ret;
 }
 
+static bool
+is_root_wim_path(const tchar *path)
+{
+       const tchar *p;
+       for (p = path; *p; p++)
+               if (*p != T('\\') && *p != T('/'))
+                       return false;
+       return true;
+}
+
+static void
+free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
+                     const tchar *dest_dir)
+{
+       for (size_t i = 0; i < num_cmds; i++)
+               if (cmds[i].fs_dest_path != dest_dir)
+                       free(cmds[i].fs_dest_path);
+       free(cmds);
+}
+
+static struct wimlib_extract_command *
+prepare_extract_commands(tchar **argv, int argc, int extract_flags,
+                        tchar *dest_dir, size_t *num_cmds_ret)
+{
+       struct wimlib_extract_command *cmds;
+       size_t num_cmds;
+       tchar *emptystr = T("");
+
+       if (argc == 0) {
+               argc = 1;
+               argv = &emptystr;
+       }
+       num_cmds = argc;
+       cmds = calloc(num_cmds, sizeof(cmds[0]));
+       if (!cmds) {
+               imagex_error(T("Out of memory!"));
+               return NULL;
+       }
+
+       for (size_t i = 0; i < num_cmds; i++) {
+               cmds[i].extract_flags = extract_flags;
+               cmds[i].wim_source_path = argv[i];
+               if (is_root_wim_path(argv[i])) {
+                       cmds[i].fs_dest_path = dest_dir;
+               } else {
+                       size_t len = tstrlen(dest_dir) + 1 + tstrlen(argv[i]);
+                       cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
+                       if (!cmds[i].fs_dest_path) {
+                               free_extract_commands(cmds, num_cmds, dest_dir);
+                               return NULL;
+                       }
+                       tsprintf(cmds[i].fs_dest_path, T("%"TS"/%"TS), dest_dir,
+                                tbasename(argv[i]));
+               }
+       }
+       *num_cmds_ret = num_cmds;
+       return cmds;
+}
+
+/* Extract files or directories from a WIM image */
+static int
+imagex_extract(int argc, tchar **argv)
+{
+       int c;
+       int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
+       int image;
+       WIMStruct *wim;
+       int ret;
+       const tchar *wimfile;
+       const tchar *image_num_or_name;
+       tchar *dest_dir = T(".");
+       int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
+
+       const tchar *swm_glob = NULL;
+       WIMStruct **additional_swms = NULL;
+       unsigned num_additional_swms = 0;
+
+       struct wimlib_extract_command *cmds;
+       size_t num_cmds;
+
+       for_opt(c, extract_options) {
+               switch (c) {
+               case IMAGEX_CHECK_OPTION:
+                       open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
+                       break;
+               case IMAGEX_VERBOSE_OPTION:
+                       extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
+                       break;
+               case IMAGEX_REF_OPTION:
+                       swm_glob = optarg;
+                       break;
+               case IMAGEX_UNIX_DATA_OPTION:
+                       extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
+                       break;
+               case IMAGEX_NO_ACLS_OPTION:
+                       extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
+                       break;
+               case IMAGEX_STRICT_ACLS_OPTION:
+                       extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
+                       break;
+               case IMAGEX_DEST_DIR_OPTION:
+                       dest_dir = optarg;
+                       break;
+               case IMAGEX_TO_STDOUT_OPTION:
+                       extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
+                       imagex_be_quiet = true;
+                       break;
+               default:
+                       usage(EXTRACT);
+                       ret = -1;
+                       goto out;
+               }
+       }
+       argc -= optind;
+       argv += optind;
+
+       if (argc < 2) {
+               usage(EXTRACT);
+               ret = -1;
+               goto out;
+       }
+       wimfile = argv[0];
+       image_num_or_name = argv[1];
+
+       argc -= 2;
+       argv += 2;
+
+       cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
+                                       &num_cmds);
+       if (!cmds) {
+               ret = -1;
+               goto out;
+       }
+
+       ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
+       if (ret)
+               goto out_free_cmds;
+
+       image = wimlib_resolve_image(wim, image_num_or_name);
+       ret = verify_image_exists_and_is_single(image,
+                                               image_num_or_name,
+                                               wimfile);
+       if (ret)
+               goto out_wimlib_free;
+
+       if (swm_glob) {
+               ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
+                                         &additional_swms,
+                                         &num_additional_swms);
+               if (ret)
+                       goto out_wimlib_free;
+       }
+
+#ifdef __WIN32__
+       win32_acquire_restore_privileges();
+#endif
+
+       ret = wimlib_extract_files(wim, image, 0, cmds, num_cmds,
+                                  additional_swms, num_additional_swms,
+                                  imagex_progress_func);
+       if (ret == 0) {
+               if (!imagex_be_quiet)
+                       tprintf(T("Done extracting files.\n"));
+       } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
+               tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
+                                  "files and directories\n"
+                                  "      are in the WIM image.\n"));
+       }
+#ifdef __WIN32__
+       win32_release_restore_privileges();
+#endif
+       if (additional_swms) {
+               for (unsigned i = 0; i < num_additional_swms; i++)
+                       wimlib_free(additional_swms[i]);
+               free(additional_swms);
+       }
+out_wimlib_free:
+       wimlib_free(wim);
+out_free_cmds:
+       free_extract_commands(cmds, num_cmds, dest_dir);
+out:
+       return ret;
+}
+
 /* Prints information about a WIM file; also can mark an image as bootable,
  * change the name of an image, or change the description of an image. */
 static int
@@ -2057,7 +2297,7 @@ imagex_info(int argc, tchar **argv)
 
                        ret = file_writable(wimfile);
                        if (ret != 0)
-                               return ret;
+                               goto out;
 
                        if (check)
                                write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
@@ -2423,6 +2663,7 @@ static const struct imagex_command imagex_commands[] = {
        {T("delete"),  imagex_delete,            DELETE},
        {T("dir"),     imagex_dir,               DIR},
        {T("export"),  imagex_export,            EXPORT},
+       {T("extract"), imagex_extract,           EXTRACT},
        {T("info"),    imagex_info,              INFO},
        {T("join"),    imagex_join,              JOIN},
        {T("mount"),   imagex_mount_rw_or_ro,    MOUNT},
@@ -2526,11 +2767,16 @@ main(int argc, char **argv)
 {
        const struct imagex_command *cmd;
        int ret;
+       int init_flags = 0;
 
 #ifndef __WIN32__
-       setlocale(LC_ALL, "");
-       {
-               char *codeset = nl_langinfo(CODESET);
+       if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
+               init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
+       } else {
+               char *codeset;
+
+               setlocale(LC_ALL, "");
+               codeset = nl_langinfo(CODESET);
                if (!strstr(codeset, "UTF-8") &&
                    !strstr(codeset, "UTF8") &&
                    !strstr(codeset, "utf-8") &&
@@ -2538,7 +2784,10 @@ main(int argc, char **argv)
                {
                        fputs(
 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
-"         (Maybe try: `export LANG=en_US.UTF-8'?\n", stderr);
+"         Maybe try: `export LANG=en_US.UTF-8'?\n"
+"         Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
+"         to any value to force wimlib to use UTF-8.\n",
+                       stderr);
 
                }
        }
@@ -2561,7 +2810,7 @@ main(int argc, char **argv)
        wimlib_set_print_errors(true);
 
        /* Do any initializations that the library needs */
-       ret = wimlib_global_init();
+       ret = wimlib_global_init(init_flags);
        if (ret)
                goto out_check_status;