4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012 Eric Biggers
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
41 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
44 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
63 static const char *path_basename(const char *path)
70 /* Trailing slashes. */
71 while ((p != path - 1) && *p == '/')
74 while ((p != path - 1) && *p != '/')
81 static const char *usage_strings[] = {
83 " imagex append DIRECTORY WIMFILE [\"IMAGE_NAME\"] [\"DESCRIPTION\"] [--boot]\n"
84 " [--check] [--flags EDITIONID] [--dereference]\n",
86 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all] DIRECTORY [--check]\n"
87 " [--hardlink] [--symlink] [--verbose]\n",
89 " imagex capture DIRECTORY WIMFILE [\"IMAGE_NAME\"] [\"DESCRIPTION\"]\n"
90 " l [--boot] [--check] [--compress[=TYPE]]\n"
91 " [--flags \"EditionID\"] [--verbose] [--dereference]\n",
93 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
95 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | \"all\")\n",
97 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
98 " DEST_WIMFILE [\"DEST_IMAGE_NAME\"] [\"DEST_IMAGE_DESCRIPTION\"]\n"
99 " [--boot] [--check] [--compress[=TYPE]]\n",
101 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
102 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
103 " [--xml] [--extract-xml FILE] [--metadata]\n",
105 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
107 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
108 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
110 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
111 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
113 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE [--check]\n",
115 " imagex unmount DIRECTORY [--commit] [--check]\n",
118 static const struct option common_options[] = {
119 {"help", 0, NULL, 'h'},
120 {"version", 0, NULL, 'v'},
124 static const struct option append_options[] = {
125 {"boot", no_argument, NULL, 'b'},
126 {"check", no_argument, NULL, 'c'},
127 {"flags", required_argument, NULL, 'f'},
128 {"dereference", no_argument, NULL, 'L'},
131 static const struct option apply_options[] = {
132 {"check", no_argument, NULL, 'c'},
133 {"hardlink", no_argument, NULL, 'h'},
134 {"symlink", no_argument, NULL, 's'},
135 {"verbose", no_argument, NULL, 'v'},
136 {"ntfs", no_argument, NULL, 'N'},
137 {"mkntfs-args", required_argument, NULL, 'm'},
140 static const struct option capture_options[] = {
141 {"boot", no_argument, NULL, 'b'},
142 {"check", no_argument, NULL, 'c'},
143 {"compress", optional_argument, NULL, 'x'},
144 {"flags", required_argument, NULL, 'f'},
145 {"verbose", no_argument, NULL,'v'},
146 {"ntfs", no_argument, NULL, 'N'},
147 {"dereference", no_argument, NULL, 'L'},
150 static const struct option delete_options[] = {
151 {"check", no_argument, NULL, 'c'},
155 static const struct option export_options[] = {
156 {"boot", no_argument, NULL, 'b'},
157 {"check", no_argument , NULL, 'c'},
158 {"compress", optional_argument, NULL, 'x'},
162 static const struct option info_options[] = {
163 {"boot", no_argument, NULL, 'b'},
164 {"check", no_argument, NULL, 'c'},
165 {"header", no_argument, NULL, 'h'},
166 {"lookup-table", no_argument, NULL, 'l'},
167 {"xml", no_argument, NULL, 'x'},
168 {"extract-xml", required_argument, NULL, 'X'},
169 {"metadata", no_argument, NULL, 'm'},
173 static const struct option join_options[] = {
174 {"check", no_argument, NULL, 'c'},
178 static const struct option mount_options[] = {
179 {"check", no_argument, NULL, 'c'},
180 {"debug", no_argument, NULL, 'd'},
181 {"stream-interface", required_argument, NULL, 's'},
185 static const struct option split_options[] = {
186 {"check", no_argument, NULL, 'c'},
190 static const struct option unmount_options[] = {
191 {"commit", no_argument, NULL, 'c'},
192 {"check", no_argument, NULL, 'C'},
197 /* Print formatted error message to stderr. */
198 static void imagex_error(const char *format, ...)
201 va_start(va, format);
202 fputs("ERROR: ", stderr);
203 vfprintf(stderr, format, va);
208 /* Print formatted error message to stderr. */
209 static void imagex_error_with_errno(const char *format, ...)
211 int errno_save = errno;
213 va_start(va, format);
214 fputs("ERROR: ", stderr);
215 vfprintf(stderr, format, va);
216 fprintf(stderr, ": %s\n", strerror(errno_save));
221 static inline void version()
223 static const char *s =
224 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
225 "Copyright (C) 2012 Eric Biggers\n"
226 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
227 "This is free software: you are free to change and redistribute it.\n"
228 "There is NO WARRANTY, to the extent permitted by law.\n"
230 "Report bugs to "PACKAGE_BUGREPORT".\n";
234 static inline void usage(int cmd)
236 puts("IMAGEX: Usage:");
237 fputs(usage_strings[cmd], stdout);
240 static void usage_all()
242 puts("IMAGEX: Usage:");
243 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
244 fputs(usage_strings[i], stdout);
245 static const char *extra =
247 " imagex --version\n"
249 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
251 fputs(extra, stdout);
254 static int verify_image_exists(int image)
256 if (image == WIM_NO_IMAGE) {
257 imagex_error("Not a valid image");
258 return WIMLIB_ERR_INVALID_IMAGE;
263 static int verify_image_is_single(int image)
265 if (image == WIM_ALL_IMAGES) {
266 imagex_error("Cannot specify all images for this action");
267 return WIMLIB_ERR_INVALID_IMAGE;
272 static int verify_image_exists_and_is_single(int image)
275 ret = verify_image_exists(image);
277 ret = verify_image_is_single(image);
281 static int get_compression_type(const char *optarg)
284 return WIM_COMPRESSION_TYPE_LZX;
285 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
286 return WIM_COMPRESSION_TYPE_LZX;
287 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
288 return WIM_COMPRESSION_TYPE_XPRESS;
289 else if (strcasecmp(optarg, "none") == 0)
290 return WIM_COMPRESSION_TYPE_NONE;
292 imagex_error("Invalid compression type `%s'! Must be "
293 "\"maximum\", \"fast\", or \"none\".", optarg);
294 return WIM_COMPRESSION_TYPE_INVALID;
298 static int imagex_append(int argc, const char **argv)
301 const char *flags_element = NULL;
302 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
303 int add_image_flags = 0;
304 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
312 for_opt(c, append_options) {
315 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
318 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
319 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
322 flags_element = optarg;
325 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
334 if (argc < 2 || argc > 4) {
340 name = (argc >= 3) ? argv[2] : path_basename(dir);
341 desc = (argc >= 4) ? argv[3] : NULL;
343 ret = wimlib_open_wim(wimfile, open_flags, &w);
347 ret = wimlib_add_image(w, dir, name, desc,
348 flags_element, add_image_flags);
351 ret = wimlib_overwrite(w, write_flags);
357 /* Extract one image, or all images, from a WIM file into a directory. */
358 static int imagex_apply(int argc, const char **argv)
361 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
368 const char *image_num_or_name;
369 const char *mkntfs_args = "";
370 int extract_flags = 0;
372 for_opt(c, apply_options) {
375 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
378 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
381 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
384 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
387 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
390 mkntfs_args = optarg;
399 if (argc != 2 && argc != 3) {
406 image_num_or_name = "1";
409 image_num_or_name = argv[1];
414 char tmpdir[strlen(dir) + 50];
417 /* Check to see if a block device file was specified. If so,
418 * create a NTFS filesystem on it. */
421 ret = stat(dir, &stbuf);
423 imagex_error_with_errno("Failed to stat `%s'", dir);
424 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
425 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
427 const char *dev = dir;
428 printf("Making NTFS filesystem on `%s'\n", dev);
430 char mkntfs_cmdline[sizeof("mkntfs ") + strlen(mkntfs_args) +
431 sizeof(" ") + strlen(dev)];
432 sprintf(mkntfs_cmdline, "mkntfs %s %s", mkntfs_args, dev);
433 puts(mkntfs_cmdline);
434 ret = system(mkntfs_cmdline);
436 imagex_error_with_errno("Failed to execute the "
439 } else if (ret > 0) {
440 imagex_error("`mkntfs' exited with failure status");
441 imagex_error("Note: You can pass additional arguments "
442 "to `mkntfs' using the --mkntfs-args "
446 sprintf(tmpdir, "/tmp/imagex-%d-ntfsmount-%s-XXXXXX",
448 dir = mkdtemp(tmpdir);
450 imagex_error_with_errno("Failed to create "
451 "temporary directory "
454 char ntfs_3g_cmdline[sizeof("ntfs-3g ") + strlen(dev) +
455 sizeof(" ") + strlen(dir)];
456 sprintf(ntfs_3g_cmdline, "ntfs-3g %s %s", dev, dir);
457 puts(ntfs_3g_cmdline);
458 ret = system(ntfs_3g_cmdline);
460 imagex_error_with_errno("Failed to execute the "
461 "`ntfs-3g' program");
464 } else if (ret > 0) {
465 imagex_error("`ntfs-3g' exited with failure status");
472 ret = wimlib_open_wim(wimfile, open_flags, &w);
476 image = wimlib_resolve_image(w, image_num_or_name);
477 ret = verify_image_exists(image);
481 num_images = wimlib_get_num_images(w);
482 if (argc == 2 && num_images != 1) {
483 imagex_error("`%s' contains %d images; Please select one "
484 "(or all)", wimfile, num_images);
489 ret = wimlib_extract_image(w, image, dir, extract_flags);
494 if (tmpdir[0] != '\0') {
495 /* Unmount and remove the NTFS-3g mounted directory */
500 imagex_error_with_errno("Failed to fork()");
504 execlp("fusermount", "fusermount", "-u", dir, NULL);
505 imagex_error_with_errno("Failed to execute `fusermount'");
509 if (waitpid(pid, &status, 0) == -1) {
510 imagex_error_with_errno("Failed to wait for "
511 "fusermount process to "
517 imagex_error("fusermount exited with status %d",
521 if (rmdir(tmpdir) != 0) {
522 imagex_error_with_errno("Failed to remove temporary "
523 "directory `%s'", tmpdir);
531 /* Create a WIM file from a directory. */
532 static int imagex_capture(int argc, const char **argv)
535 int add_image_flags = 0;
536 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
537 int compression_type = WIM_COMPRESSION_TYPE_NONE;
538 const char *flags_element = NULL;
546 for_opt(c, capture_options) {
549 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
552 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
555 compression_type = get_compression_type(optarg);
556 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
560 flags_element = optarg;
563 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
566 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
569 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
579 if (argc < 2 || argc > 4) {
585 name = (argc >= 3) ? argv[2] : dir;
586 desc = (argc >= 4) ? argv[3] : NULL;
588 ret = wimlib_create_new_wim(compression_type, &w);
592 ret = wimlib_add_image(w, dir, name, desc, flags_element,
595 imagex_error("Failed to add the image `%s'", dir);
599 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
601 imagex_error("Failed to write the WIM file `%s'", wimfile);
607 /* Remove image(s) from a WIM. */
608 static int imagex_delete(int argc, const char **argv)
611 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
612 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
614 const char *image_num_or_name;
619 for_opt(c, delete_options) {
622 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
623 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
635 imagex_error("Must specify a WIM file");
637 imagex_error("Must specify an image");
642 image_num_or_name = argv[1];
644 ret = wimlib_open_wim(wimfile, open_flags, &w);
648 image = wimlib_resolve_image(w, image_num_or_name);
650 ret = verify_image_exists(image);
654 ret = wimlib_delete_image(w, image);
656 imagex_error("Failed to delete image from `%s'", wimfile);
660 ret = wimlib_overwrite(w, write_flags);
662 imagex_error("Failed to write the file `%s' with image "
670 /* Print the files contained in an image(s) in a WIM file. */
671 static int imagex_dir(int argc, const char **argv)
681 imagex_error("Must specify a WIM file");
686 imagex_error("Too many arguments");
692 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
696 part_number = wimlib_get_part_number(w, NULL);
697 if (part_number != 1) {
698 imagex_error("`%s' is part %d of a split WIM! Specify the "
699 "first part to see the files",
700 wimfile, part_number);
701 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
706 image = wimlib_resolve_image(w, argv[2]);
707 ret = verify_image_exists(image);
711 /* Image was not specified. If the WIM only contains one image,
712 * choose that one; otherwise, print an error. */
713 num_images = wimlib_get_num_images(w);
714 if (num_images != 1) {
715 imagex_error("The file `%s' contains %d images; Please "
716 "select one.", wimfile, num_images);
724 ret = wimlib_print_files(w, image);
730 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
732 static int imagex_export(int argc, const char **argv)
735 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
736 int export_flags = 0;
737 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
738 int compression_type = WIM_COMPRESSION_TYPE_NONE;
739 bool compression_type_specified = false;
740 const char *src_wimfile;
741 const char *src_image_num_or_name;
742 const char *dest_wimfile;
743 const char *dest_name;
744 const char *dest_desc;
745 WIMStruct *src_w = NULL;
746 WIMStruct *dest_w = NULL;
752 for_opt(c, export_options) {
755 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
758 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
759 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
762 compression_type = get_compression_type(optarg);
763 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
765 compression_type_specified = true;
774 if (argc < 3 || argc > 5) {
778 src_wimfile = argv[0];
779 src_image_num_or_name = argv[1];
780 dest_wimfile = argv[2];
781 dest_name = (argc >= 4) ? argv[3] : NULL;
782 dest_desc = (argc >= 5) ? argv[4] : NULL;
783 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
787 /* Determine if the destination is an existing file or not.
788 * If so, we try to append the exported image(s) to it; otherwise, we
789 * create a new WIM containing the exported image(s). */
790 if (stat(dest_wimfile, &stbuf) == 0) {
792 /* Destination file exists. */
793 if (!S_ISREG(stbuf.st_mode)) {
794 imagex_error("`%s' is not a regular file",
798 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
802 if (compression_type_specified && compression_type !=
803 wimlib_get_compression_type(dest_w)) {
804 imagex_error("Cannot specify a compression type that is "
805 "not the same as that used in the "
810 compression_type = wimlib_get_compression_type(dest_w);
813 /* dest_wimfile is not an existing file, so create a new WIM. */
814 if (errno == ENOENT) {
815 ret = wimlib_create_new_wim(compression_type, &dest_w);
819 imagex_error_with_errno("Cannot stat file `%s'",
825 image = wimlib_resolve_image(src_w, src_image_num_or_name);
826 ret = verify_image_exists(image);
830 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
837 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
840 ret = wimlib_overwrite(dest_w, write_flags);
847 /* Prints information about a WIM file; also can mark an image as bootable,
848 * change the name of an image, or change the description of an image. */
849 static int imagex_info(int argc, const char **argv)
855 bool lookup_table = false;
857 bool metadata = false;
858 bool short_header = true;
859 const char *xml_out_file = NULL;
861 const char *image_num_or_name = "all";
862 const char *new_name = NULL;
863 const char *new_desc = NULL;
868 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
869 WIMLIB_OPEN_FLAG_SPLIT_OK;
873 for_opt(c, info_options) {
883 short_header = false;
887 short_header = false;
891 short_header = false;
894 xml_out_file = optarg;
895 short_header = false;
899 short_header = false;
909 if (argc == 0 || argc > 4) {
915 image_num_or_name = argv[1];
925 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
927 ret = wimlib_open_wim(wimfile, open_flags, &w);
931 part_number = wimlib_get_part_number(w, &total_parts);
933 /*if (total_parts > 1 && part_number > 1) {*/
934 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
935 /*" Select the first part if you want to see information\n"*/
936 /*" about images in the WIM.\n", */
937 /*part_number, total_parts);*/
940 image = wimlib_resolve_image(w, image_num_or_name);
941 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
942 imagex_error("The image `%s' does not exist",
945 imagex_error("If you would like to set the boot "
946 "index to 0, specify image \"0\" with "
948 ret = WIMLIB_ERR_INVALID_IMAGE;
952 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
954 imagex_error("Cannot specify the --boot flag "
955 "without specifying a specific "
956 "image in a multi-image WIM");
957 ret = WIMLIB_ERR_INVALID_IMAGE;
961 imagex_error("Cannot specify the NEW_NAME "
962 "without specifying a specific "
963 "image in a multi-image WIM");
964 ret = WIMLIB_ERR_INVALID_IMAGE;
969 /* Operations that print information are separated from operations that
970 * recreate the WIM file. */
971 if (!new_name && !boot) {
973 if (image == WIM_NO_IMAGE) {
974 imagex_error("`%s' is not a valid image",
976 ret = WIMLIB_ERR_INVALID_IMAGE;
980 if (image == WIM_ALL_IMAGES && short_header)
981 wimlib_print_wim_information(w);
984 wimlib_print_header(w);
987 if (total_parts != 1) {
988 printf("Warning: Only showing the lookup table "
989 "for part %d of a %d-part WIM.\n",
990 part_number, total_parts);
992 wimlib_print_lookup_table(w);
996 ret = wimlib_extract_xml_data(w, stdout);
1002 fp = fopen(xml_out_file, "wb");
1004 imagex_error_with_errno("Failed to open the "
1010 ret = wimlib_extract_xml_data(w, fp);
1011 if (fclose(fp) != 0) {
1012 imagex_error("Failed to close the file `%s'",
1022 wimlib_print_available_images(w, image);
1025 if (total_parts != 1 && part_number != 1) {
1026 imagex_error("Select part 1 of this %d-part WIM "
1027 "to see the image metadata",
1029 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1031 ret = wimlib_print_metadata(w, image);
1036 if (total_parts != 1) {
1037 imagex_error("Modifying a split WIM is not supported.");
1040 if (image == WIM_ALL_IMAGES)
1043 if (image == WIM_NO_IMAGE && new_name) {
1044 imagex_error("Cannot specify new_name (`%s') when "
1045 "using image 0", new_name);
1050 if (image == wimlib_get_boot_idx(w)) {
1051 printf("Image %d is already marked as "
1052 "bootable.\n", image);
1055 printf("Marking image %d as bootable.\n",
1057 wimlib_set_boot_idx(w, image);
1061 if (strcmp(wimlib_get_image_name(w, image),
1063 printf("Image %d is already named \"%s\".\n",
1067 printf("Changing the name of image %d to "
1068 "\"%s\".\n", image, new_name);
1069 ret = wimlib_set_image_name(w, image, new_name);
1075 const char *old_desc;
1076 old_desc = wimlib_get_image_description(w, image);
1077 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1078 printf("The description of image %d is already "
1079 "\"%s\".\n", image, new_desc);
1082 printf("Changing the description of image %d "
1083 "to \"%s\".\n", image, new_desc);
1084 ret = wimlib_set_image_descripton(w, image,
1091 /* Only call wimlib_overwrite_xml_and_header() if something
1092 * actually needs to be changed. */
1093 if (boot || new_name || new_desc ||
1094 check != wimlib_has_integrity_table(w)) {
1096 ret = wimlib_overwrite_xml_and_header(w, check ?
1097 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1098 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1100 printf("The file `%s' was not modified because nothing "
1101 "needed to be done.\n", wimfile);
1111 /* Join split WIMs into one part WIM */
1112 static int imagex_join(int argc, const char **argv)
1115 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1116 const char *output_path;
1118 for_opt(c, join_options) {
1121 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1131 imagex_error("Must specify at least one split WIM (.swm) parts "
1135 output_path = argv[0];
1136 return wimlib_join(++argv, --argc, output_path, flags);
1142 /* Mounts an image using a FUSE mount. */
1143 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1146 int mount_flags = 0;
1147 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1148 const char *wimfile;
1155 if (strcmp(argv[0], "mountrw") == 0)
1156 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1157 for_opt(c, mount_options) {
1160 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1163 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1166 if (strcasecmp(optarg, "none") == 0)
1167 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1168 else if (strcasecmp(optarg, "xattr") == 0)
1169 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1170 else if (strcasecmp(optarg, "windows") == 0)
1171 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1173 imagex_error("Unknown stream interface \"%s\"", optarg);
1183 if (argc != 2 && argc != 3)
1188 ret = wimlib_open_wim(wimfile, open_flags, &w);
1194 num_images = wimlib_get_num_images(w);
1195 if (num_images != 1) {
1196 imagex_error("The file `%s' contains %d images; Please "
1197 "select one", wimfile, num_images);
1198 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1200 ret = WIMLIB_ERR_INVALID_IMAGE;
1205 image = wimlib_resolve_image(w, argv[1]);
1209 ret = verify_image_exists_and_is_single(image);
1213 ret = wimlib_mount(w, image, dir, mount_flags);
1215 imagex_error("Failed to mount image %d from `%s' on `%s'",
1216 image, wimfile, dir);
1223 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1228 /* Split a WIM into a spanned set */
1229 static int imagex_split(int argc, const char **argv)
1232 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1233 unsigned long part_size;
1235 for_opt(c, split_options) {
1238 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1252 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1253 return wimlib_split(argv[0], argv[1], part_size, flags);
1256 /* Unmounts an image. */
1257 static int imagex_unmount(int argc, const char **argv)
1260 int unmount_flags = 0;
1263 for_opt(c, unmount_options) {
1266 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1269 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1283 ret = wimlib_unmount(argv[0], unmount_flags);
1285 imagex_error("Failed to unmount `%s'", argv[0]);
1289 struct imagex_command {
1291 int (*func)(int , const char **);
1295 static struct imagex_command imagex_commands[] = {
1296 {"append", imagex_append, APPEND},
1297 {"apply", imagex_apply, APPLY},
1298 {"capture", imagex_capture, CAPTURE},
1299 {"delete", imagex_delete, DELETE},
1300 {"dir", imagex_dir, DIR},
1301 {"export", imagex_export, EXPORT},
1302 {"info", imagex_info, INFO},
1303 {"join", imagex_join, JOIN},
1304 {"mount", imagex_mount_rw_or_ro, MOUNT},
1305 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1306 {"split", imagex_split, SPLIT},
1307 {"unmount", imagex_unmount, UNMOUNT},
1310 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1311 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1313 static void help_or_version(int argc, const char **argv)
1317 struct imagex_command *cmd;
1319 for (i = 1; i < argc; i++) {
1327 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1328 for_imagex_command(cmd) {
1329 if (strcmp(cmd->name, argv[1]) == 0) {
1337 if (strcmp(p, "version") == 0) {
1345 int main(int argc, const char **argv)
1347 struct imagex_command *cmd;
1351 imagex_error("No command specified");
1356 help_or_version(argc, argv);
1360 wimlib_set_print_errors(true);
1362 for_imagex_command(cmd) {
1363 if (strcmp(cmd->name, *argv) == 0) {
1364 ret = cmd->func(argc, argv);
1366 imagex_error("Exiting with error code %d:\n"
1368 wimlib_get_error_string(ret));
1369 if (ret == WIMLIB_ERR_NTFS_3G)
1370 imagex_error_with_errno("errno");
1376 imagex_error("Unrecognized command: `%s'", argv[0]);
1380 /*#ifndef WITH_NTFS_3G*/
1381 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1382 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1383 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1384 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1385 /*return WIMLIB_ERR_UNSUPPORTED;*/