4 * Use wimlib to create, modify, extract, mount, unmount, or display information
7 * Copyright (C) 2012 Eric Biggers
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
34 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
37 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
53 static const char *path_basename(const char *path)
60 /* Trailing slashes. */
61 while ((p != path - 1) && *p == '/')
64 while ((p != path - 1) && *p != '/')
71 static const char *usage_strings[] = {
73 " imagex append DIRECTORY WIMFILE [\"IMAGE_NAME\"] [\"DESCRIPTION\"] [--boot]\n"
74 " [--check] [--config CONFIG_FILE] [--flags EDITIONID] [--verify]\n",
76 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all] DIRECTORY [--check]\n"
77 " [--verify] [--hardlink] [--symlink] [--verbose]\n",
79 " imagex capture DIRECTORY WIMFILE [\"IMAGE_NAME\"] [\"DESCRIPTION\"]\n"
80 " [--boot] [--check] [--compress[=TYPE]] [--config CONFIG_FILE]\n"
81 " [--flags \"EditionID\"] [--norpfix] [--verify] [--verbose]\n",
83 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
85 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | \"all\")\n",
87 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
88 " DEST_WIMFILE [\"DEST_IMAGE_NAME\"] [\"DEST_IMAGE_DESCRIPTION\"]\n"
89 " [--boot] [--check] [--compress[=TYPE]]\n",
91 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
92 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
93 " [--xml] [--extract-xml FILE] [--metadata]\n",
95 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
96 " [--check] [--debug]\n",
98 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
99 " [--check] [--debug]\n",
101 " imagex unmount DIRECTORY [--commit] [--check]\n",
104 static const struct option common_options[] = {
105 {"help", 0, NULL, 'h'},
106 {"version", 0, NULL, 'v'},
110 static const struct option append_options[] = {
111 {"boot", no_argument, NULL, 'b'},
112 {"check", no_argument, NULL, 'c'},
113 {"config", required_argument, NULL, 'C'},
114 {"flags", required_argument, NULL, 'f'},
115 {"verify", no_argument, NULL, 'V'},
118 static const struct option apply_options[] = {
119 {"check", no_argument, NULL, 'c'},
120 {"verify", no_argument, NULL, 'V'},
121 {"hardlink", no_argument, NULL, 'h'},
122 {"symlink", no_argument, NULL, 's'},
123 {"verbose", no_argument, NULL, 'v'},
126 static const struct option capture_options[] = {
127 {"boot", no_argument, NULL, 'b'},
128 {"check", no_argument, NULL, 'c'},
129 {"compress", optional_argument, NULL, 'x'},
130 {"config", required_argument, NULL, 'C'},
131 {"flags", required_argument, NULL, 'f'},
132 {"norpfix", no_argument, NULL, 'n'},
133 {"verify", no_argument, NULL, 'V'},
134 {"verbose", no_argument, NULL, 'v'},
137 static const struct option delete_options[] = {
138 {"check", no_argument, NULL, 'c'},
142 static const struct option export_options[] = {
143 {"boot", no_argument, NULL, 'b'},
144 {"check", no_argument , NULL, 'c'},
145 {"compress", optional_argument, NULL, 'x'},
149 static const struct option info_options[] = {
150 {"boot", no_argument, NULL, 'b'},
151 {"check", no_argument, NULL, 'c'},
152 {"header", no_argument, NULL, 'h'},
153 {"lookup-table", no_argument, NULL, 'l'},
154 {"xml", no_argument, NULL, 'x'},
155 {"extract-xml", required_argument, NULL, 'X'},
156 {"metadata", no_argument, NULL, 'm'},
159 static const struct option mount_options[] = {
160 {"check", no_argument, NULL, 'c'},
161 {"debug", no_argument, NULL, 'd'},
165 static const struct option unmount_options[] = {
166 {"commit", no_argument, NULL, 'c'},
167 {"check", no_argument, NULL, 'C'},
172 /* Print formatted error message to stderr. */
173 static void imagex_error(const char *format, ...)
176 va_start(va, format);
177 fputs("ERROR: ", stderr);
178 vfprintf(stderr, format, va);
183 static inline void version()
185 static const char *s =
186 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
187 "Copyright (C) 2012 Eric Biggers\n"
188 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
189 "This is free software: you are free to change and redistribute it.\n"
190 "There is NO WARRANTY, to the extent permitted by law.\n"
192 "Report bugs to "PACKAGE_BUGREPORT".\n";
196 static inline void usage(int cmd)
198 puts("IMAGEX: Usage:");
199 fputs(usage_strings[cmd], stdout);
202 static void usage_all()
204 puts("IMAGEX: Usage:");
205 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
206 fputs(usage_strings[i], stdout);
207 static const char *extra =
209 " imagex --version\n"
211 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
213 fputs(extra, stdout);
216 static int verify_image_exists(int image)
218 if (image == WIM_NO_IMAGE) {
219 imagex_error("Not a valid image!\n");
220 return WIMLIB_ERR_INVALID_IMAGE;
225 static int verify_image_is_single(int image)
227 if (image == WIM_ALL_IMAGES) {
228 imagex_error("Cannot specify all images for this action!\n");
229 return WIMLIB_ERR_INVALID_IMAGE;
234 static int verify_image_exists_and_is_single(int image)
237 ret = verify_image_exists(image);
239 ret = verify_image_is_single(image);
243 static int get_compression_type(const char *optarg)
246 return WIM_COMPRESSION_TYPE_LZX;
247 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
248 return WIM_COMPRESSION_TYPE_LZX;
249 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
250 return WIM_COMPRESSION_TYPE_XPRESS;
251 else if (strcasecmp(optarg, "none") == 0)
252 return WIM_COMPRESSION_TYPE_NONE;
254 imagex_error("Invalid compression type `%s'! Must be "
255 "\"maximum\", \"fast\", or \"none\".\n",
257 return WIM_COMPRESSION_TYPE_INVALID;
261 static int imagex_append(int argc, const char **argv)
264 const char *flags_element = NULL;
265 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
266 int add_image_flags = 0;
267 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
275 for_opt(c, append_options) {
278 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
281 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
282 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
288 flags_element = optarg;
300 if (argc < 2 || argc > 4) {
306 name = (argc >= 3) ? argv[2] : path_basename(dir);
307 desc = (argc >= 4) ? argv[3] : NULL;
309 ret = wimlib_open_wim(wimfile, open_flags, &w);
313 ret = wimlib_add_image(w, dir, name, desc,
314 flags_element, add_image_flags);
317 ret = wimlib_overwrite(w, write_flags);
323 /* Extract one image, or all images, from a WIM file into a directory. */
324 static int imagex_apply(int argc, const char **argv)
327 int link_type = WIM_LINK_TYPE_NONE;
328 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
329 bool verbose = false;
336 const char *image_num_or_name;
338 for_opt(c, apply_options) {
341 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
347 link_type = WIM_LINK_TYPE_HARD;
350 link_type = WIM_LINK_TYPE_SYMBOLIC;
362 if (argc != 2 && argc != 3) {
369 image_num_or_name = "1";
372 image_num_or_name = argv[1];
376 ret = wimlib_open_wim(wimfile, open_flags, &w);
380 image = wimlib_resolve_image(w, image_num_or_name);
381 ret = verify_image_exists(image);
385 num_images = wimlib_get_num_images(w);
386 if (argc == 2 && num_images != 1) {
387 imagex_error("`%s' contains %d images; Please select one "
388 "(or all).\n", wimfile, num_images);
393 ret = wimlib_set_output_dir(w, dir);
396 wimlib_set_verbose(w, verbose);
397 wimlib_set_link_type(w, link_type);
398 ret = wimlib_extract_image(w, image);
405 /* Create a WIM file from a directory. */
406 static int imagex_capture(int argc, const char **argv)
409 int add_image_flags = 0;
410 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
411 int compression_type = WIM_COMPRESSION_TYPE_NONE;
412 const char *flags_element = NULL;
413 bool verbose = false;
421 for_opt(c, capture_options) {
424 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
427 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
430 compression_type = get_compression_type(optarg);
431 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
438 flags_element = optarg;
457 if (argc < 2 || argc > 4) {
463 name = (argc >= 3) ? argv[2] : dir;
464 desc = (argc >= 4) ? argv[3] : NULL;
466 ret = wimlib_create_new_wim(compression_type, &w);
470 wimlib_set_verbose(w, verbose);
472 ret = wimlib_add_image(w, dir, name, desc, flags_element,
475 imagex_error("Failed to add the image `%s'!\n", dir);
479 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
481 imagex_error("Failed to write the WIM file `%s'!\n", wimfile);
487 /* Remove image(s) from a WIM. */
488 static int imagex_delete(int argc, const char **argv)
491 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
492 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
494 const char *image_num_or_name;
499 for_opt(c, delete_options) {
502 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
503 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
515 imagex_error("Must specify a WIM file!\n");
517 imagex_error("Must specify an image!\n");
522 image_num_or_name = argv[1];
524 ret = wimlib_open_wim(wimfile, open_flags, &w);
528 image = wimlib_resolve_image(w, image_num_or_name);
530 ret = verify_image_exists(image);
534 ret = wimlib_delete_image(w, image);
536 imagex_error("Failed to delete image from `%s'!\n",
541 ret = wimlib_overwrite(w, write_flags);
543 imagex_error("Failed to write the file `%s' with image "
544 "deleted!\n", wimfile);
551 /* Print the files contained in an image(s) in a WIM file. */
552 static int imagex_dir(int argc, const char **argv)
561 imagex_error("Must specify a WIM file!\n");
566 imagex_error("Too many arguments!\n");
572 ret = wimlib_open_wim(wimfile, 0, &w);
577 image = wimlib_resolve_image(w, argv[2]);
578 ret = verify_image_exists(image);
582 /* Image was not specified. If the WIM only contains one image,
583 * choose that one; otherwise, print an error. */
584 num_images = wimlib_get_num_images(w);
585 if (num_images != 1) {
586 imagex_error("The file `%s' contains %d images; Please "
587 "select one.\n", wimfile, num_images);
595 ret = wimlib_print_files(w, image);
601 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
603 static int imagex_export(int argc, const char **argv)
606 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
607 int export_flags = 0;
608 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
609 int compression_type = WIM_COMPRESSION_TYPE_NONE;
610 bool compression_type_specified = false;
611 const char *src_wimfile;
612 const char *src_image_num_or_name;
613 const char *dest_wimfile;
614 const char *dest_name;
615 const char *dest_desc;
616 WIMStruct *src_w = NULL;
617 WIMStruct *dest_w = NULL;
623 for_opt(c, export_options) {
626 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
629 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
630 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
633 compression_type = get_compression_type(optarg);
634 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
636 compression_type_specified = true;
645 if (argc < 3 || argc > 5) {
649 src_wimfile = argv[0];
650 src_image_num_or_name = argv[1];
651 dest_wimfile = argv[2];
652 dest_name = (argc >= 4) ? argv[3] : NULL;
653 dest_desc = (argc >= 5) ? argv[4] : NULL;
654 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
658 /* Determine if the destination is an existing file or not.
659 * If so, we try to append the exported image(s) to it; otherwise, we
660 * create a new WIM containing the exported image(s). */
661 if (stat(dest_wimfile, &stbuf) == 0) {
663 /* Destination file exists. */
664 if (!S_ISREG(stbuf.st_mode)) {
665 imagex_error("`%s' is not a regular file!\n",
669 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
673 if (compression_type_specified && compression_type !=
674 wimlib_get_compression_type(dest_w)) {
675 imagex_error("Cannot specify a compression type that is "
676 "not the same as that used in the "
677 "destination WIM!\n");
680 compression_type = wimlib_get_compression_type(dest_w);
683 /* dest_wimfile is not an existing file, so create a new WIM. */
684 if (errno == ENOENT) {
685 ret = wimlib_create_new_wim(compression_type, &dest_w);
689 imagex_error("Cannot stat file `%s': %m\n",
695 image = wimlib_resolve_image(src_w, src_image_num_or_name);
696 ret = verify_image_exists(image);
700 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
707 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
710 ret = wimlib_overwrite(dest_w, write_flags);
717 /* Prints information about a WIM file; also can mark an image as bootable,
718 * change the name of an image, or change the description of an image. */
719 static int imagex_info(int argc, const char **argv)
725 bool lookup_table = false;
727 bool metadata = false;
728 bool short_header = true;
729 const char *xml_out_file = NULL;
731 const char *image_num_or_name = "all";
732 const char *new_name = NULL;
733 const char *new_desc = NULL;
739 for_opt(c, info_options) {
749 short_header = false;
753 short_header = false;
757 short_header = false;
760 xml_out_file = optarg;
761 short_header = false;
765 short_header = false;
775 if (argc == 0 || argc > 4) {
781 image_num_or_name = argv[1];
790 ret = wimlib_open_wim(wimfile,
792 WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
793 WIMLIB_OPEN_FLAG_SHOW_PROGRESS
799 image = wimlib_resolve_image(w, image_num_or_name);
800 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
801 imagex_error("The image `%s' does not exist!\n",
804 imagex_error("If you would like to set the boot "
805 "index to 0, specify image \"0\" with "
806 "the --boot flag.\n");
807 ret = WIMLIB_ERR_INVALID_IMAGE;
811 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
813 imagex_error("Cannot specify the --boot flag "
814 "without specifying a specific "
815 "image in a multi-image WIM!\n");
816 ret = WIMLIB_ERR_INVALID_IMAGE;
820 imagex_error("Cannot specify the NEW_NAME "
821 "without specifying a specific "
822 "image in a multi-image WIM!\n");
823 ret = WIMLIB_ERR_INVALID_IMAGE;
828 /* Operations that print information are separated from operations that
829 * recreate the WIM file. */
830 if (!new_name && !boot) {
832 if (image == WIM_NO_IMAGE) {
833 imagex_error("`%s' is not a valid image!\n",
835 ret = WIMLIB_ERR_INVALID_IMAGE;
839 if (image == WIM_ALL_IMAGES && short_header)
840 wimlib_print_wim_information(w);
843 wimlib_print_header(w);
846 wimlib_print_lookup_table(w);
849 ret = wimlib_extract_xml_data(w, stdout);
855 fp = fopen(xml_out_file, "wb");
857 imagex_error("Failed to open the file `%s' for "
858 "writing: %m\n", xml_out_file);
861 ret = wimlib_extract_xml_data(w, fp);
862 if (fclose(fp) != 0) {
863 imagex_error("Failed to close the "
874 wimlib_print_available_images(w, image);
877 ret = wimlib_print_metadata(w, image);
882 if (image == WIM_ALL_IMAGES)
885 if (image == WIM_NO_IMAGE && new_name) {
886 imagex_error("Cannot specify new_name (`%s') when "
892 if (image == wimlib_get_boot_idx(w)) {
893 printf("Image %d is already marked as "
894 "bootable.\n", image);
897 printf("Marking image %d as bootable.\n",
899 wimlib_set_boot_idx(w, image);
903 if (strcmp(wimlib_get_image_name(w, image),
905 printf("Image %d is already named \"%s\".\n",
909 printf("Changing the name of image %d to \"%s\".\n",
911 ret = wimlib_set_image_name(w, image, new_name);
917 const char *old_desc;
918 old_desc = wimlib_get_image_description(w, image);
919 if (old_desc && strcmp(old_desc, new_desc) == 0) {
920 printf("The description of image %d is already "
921 "\"%s\".\n", image, new_desc);
924 printf("Changing the description of image %d "
925 "to \"%s\".\n", image, new_desc);
926 ret = wimlib_set_image_descripton(w, image,
933 /* Only call wimlib_overwrite_xml_and_header() if something
934 * actually needs to be changed. */
935 if (boot || new_name || new_desc ||
936 check != wimlib_has_integrity_table(w)) {
938 ret = wimlib_overwrite_xml_and_header(w, check ?
939 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
940 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
942 printf("The file `%s' was not modified because nothing "
943 "needed to be done.\n", wimfile);
953 /* Mounts an image using a FUSE mount. */
954 static int imagex_mount_rw_or_ro(int argc, const char **argv)
959 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
967 if (strcmp(argv[0], "mountrw") == 0)
968 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
969 for_opt(c, mount_options) {
972 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
975 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
978 usage(ro ? MOUNT : MOUNTRW);
984 if (argc != 2 && argc != 3) {
985 usage(ro ? MOUNT : MOUNTRW);
991 ret = wimlib_open_wim(wimfile, open_flags, &w);
997 num_images = wimlib_get_num_images(w);
998 if (num_images != 1) {
999 imagex_error("The file `%s' contains %d images; Please "
1000 "select one.\n", wimfile, num_images);
1001 usage(ro ? MOUNT : MOUNTRW);
1002 ret = WIMLIB_ERR_INVALID_IMAGE;
1007 image = wimlib_resolve_image(w, argv[1]);
1011 ret = verify_image_exists_and_is_single(image);
1015 ret = wimlib_mount(w, image, dir, mount_flags);
1017 imagex_error("Failed to mount image %d from `%s' on `%s'!\n",
1018 image, wimfile, dir);
1026 /* Unmounts an image. */
1027 static int imagex_unmount(int argc, const char **argv)
1030 int unmount_flags = 0;
1033 for_opt(c, unmount_options) {
1036 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1039 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1053 ret = wimlib_unmount(argv[0], unmount_flags);
1055 imagex_error("Failed to unmount `%s'!\n", argv[0]);
1059 struct imagex_command {
1061 int (*func)(int , const char **);
1065 static struct imagex_command imagex_commands[] = {
1066 {"append", imagex_append, APPEND},
1067 {"apply", imagex_apply, APPLY},
1068 {"capture", imagex_capture, CAPTURE},
1069 {"delete", imagex_delete, DELETE},
1070 {"dir", imagex_dir, DIR},
1071 {"export", imagex_export, EXPORT},
1072 {"info", imagex_info, INFO},
1073 {"mount", imagex_mount_rw_or_ro, MOUNT},
1074 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1075 {"unmount", imagex_unmount, UNMOUNT},
1078 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1079 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1081 static void help_or_version(int argc, const char **argv)
1085 struct imagex_command *cmd;
1087 for (i = 1; i < argc; i++) {
1095 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1096 for_imagex_command(cmd) {
1097 if (strcmp(cmd->name, argv[1]) == 0) {
1105 if (strcmp(p, "version") == 0) {
1113 int main(int argc, const char **argv)
1115 struct imagex_command *cmd;
1119 imagex_error("No command specified!\n");
1124 help_or_version(argc, argv);
1128 wimlib_set_print_errors(true);
1130 for_imagex_command(cmd) {
1131 if (strcmp(cmd->name, *argv) == 0) {
1132 ret = cmd->func(argc, argv);
1134 imagex_error("Exiting with error code %d:\n"
1136 wimlib_get_error_string(ret));
1142 imagex_error("Unrecognized command: `%s'\n", argv[0]);