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 {"mkntfs-args", required_argument, NULL, 'm'},
139 static const struct option capture_options[] = {
140 {"boot", no_argument, NULL, 'b'},
141 {"check", no_argument, NULL, 'c'},
142 {"compress", optional_argument, NULL, 'x'},
143 {"flags", required_argument, NULL, 'f'},
144 {"verbose", no_argument, NULL,'v'},
145 {"dereference", no_argument, NULL, 'L'},
148 static const struct option delete_options[] = {
149 {"check", no_argument, NULL, 'c'},
153 static const struct option export_options[] = {
154 {"boot", no_argument, NULL, 'b'},
155 {"check", no_argument , NULL, 'c'},
156 {"compress", optional_argument, NULL, 'x'},
160 static const struct option info_options[] = {
161 {"boot", no_argument, NULL, 'b'},
162 {"check", no_argument, NULL, 'c'},
163 {"header", no_argument, NULL, 'h'},
164 {"lookup-table", no_argument, NULL, 'l'},
165 {"xml", no_argument, NULL, 'x'},
166 {"extract-xml", required_argument, NULL, 'X'},
167 {"metadata", no_argument, NULL, 'm'},
171 static const struct option join_options[] = {
172 {"check", no_argument, NULL, 'c'},
176 static const struct option mount_options[] = {
177 {"check", no_argument, NULL, 'c'},
178 {"debug", no_argument, NULL, 'd'},
179 {"stream-interface", required_argument, NULL, 's'},
183 static const struct option split_options[] = {
184 {"check", no_argument, NULL, 'c'},
188 static const struct option unmount_options[] = {
189 {"commit", no_argument, NULL, 'c'},
190 {"check", no_argument, NULL, 'C'},
195 /* Print formatted error message to stderr. */
196 static void imagex_error(const char *format, ...)
199 va_start(va, format);
200 fputs("ERROR: ", stderr);
201 vfprintf(stderr, format, va);
206 /* Print formatted error message to stderr. */
207 static void imagex_error_with_errno(const char *format, ...)
209 int errno_save = errno;
211 va_start(va, format);
212 fputs("ERROR: ", stderr);
213 vfprintf(stderr, format, va);
214 fprintf(stderr, ": %s\n", strerror(errno_save));
219 static inline void version()
221 static const char *s =
222 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
223 "Copyright (C) 2012 Eric Biggers\n"
224 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
225 "This is free software: you are free to change and redistribute it.\n"
226 "There is NO WARRANTY, to the extent permitted by law.\n"
228 "Report bugs to "PACKAGE_BUGREPORT".\n";
232 static inline void usage(int cmd)
234 puts("IMAGEX: Usage:");
235 fputs(usage_strings[cmd], stdout);
238 static void usage_all()
240 puts("IMAGEX: Usage:");
241 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
242 fputs(usage_strings[i], stdout);
243 static const char *extra =
245 " imagex --version\n"
247 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
249 fputs(extra, stdout);
252 static int verify_image_exists(int image)
254 if (image == WIM_NO_IMAGE) {
255 imagex_error("Not a valid image");
256 return WIMLIB_ERR_INVALID_IMAGE;
261 static int verify_image_is_single(int image)
263 if (image == WIM_ALL_IMAGES) {
264 imagex_error("Cannot specify all images for this action");
265 return WIMLIB_ERR_INVALID_IMAGE;
270 static int verify_image_exists_and_is_single(int image)
273 ret = verify_image_exists(image);
275 ret = verify_image_is_single(image);
279 static int get_compression_type(const char *optarg)
282 return WIM_COMPRESSION_TYPE_LZX;
283 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
284 return WIM_COMPRESSION_TYPE_LZX;
285 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
286 return WIM_COMPRESSION_TYPE_XPRESS;
287 else if (strcasecmp(optarg, "none") == 0)
288 return WIM_COMPRESSION_TYPE_NONE;
290 imagex_error("Invalid compression type `%s'! Must be "
291 "\"maximum\", \"fast\", or \"none\".", optarg);
292 return WIM_COMPRESSION_TYPE_INVALID;
296 static int imagex_append(int argc, const char **argv)
299 const char *flags_element = NULL;
300 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
301 int add_image_flags = 0;
302 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
310 for_opt(c, append_options) {
313 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
316 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
317 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
320 flags_element = optarg;
323 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
332 if (argc < 2 || argc > 4) {
338 name = (argc >= 3) ? argv[2] : path_basename(dir);
339 desc = (argc >= 4) ? argv[3] : NULL;
341 ret = wimlib_open_wim(wimfile, open_flags, &w);
345 ret = wimlib_add_image(w, dir, name, desc,
346 flags_element, add_image_flags);
349 ret = wimlib_overwrite(w, write_flags);
355 /* Extract one image, or all images, from a WIM file into a directory. */
356 static int imagex_apply(int argc, const char **argv)
359 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
366 const char *image_num_or_name;
367 const char *mkntfs_args = "";
368 int extract_flags = 0;
371 for_opt(c, apply_options) {
374 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
377 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
380 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
383 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
386 mkntfs_args = optarg;
395 if (argc != 2 && argc != 3) {
402 image_num_or_name = "1";
405 image_num_or_name = argv[1];
410 char tmpdir[strlen(dir) + 50];
414 ret = wimlib_open_wim(wimfile, open_flags, &w);
418 image = wimlib_resolve_image(w, image_num_or_name);
419 ret = verify_image_exists(image);
423 num_images = wimlib_get_num_images(w);
424 if (argc == 2 && num_images != 1) {
425 imagex_error("`%s' contains %d images; Please select one "
426 "(or all)", wimfile, num_images);
433 /* Check to see if a block device file or regular file was specified.
434 * If so, try to create a NTFS filesystem on it, mount it on a temporary
435 * directory, and replace @dir with the name of the temporary directory
436 * so that we extract the files to the NTFS filesystem. */
439 ret = stat(dir, &stbuf);
441 imagex_error_with_errno("Failed to stat `%s'", dir);
442 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
443 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
445 const char *dev = dir;
446 printf("Making NTFS filesystem on `%s'\n", dev);
448 char mkntfs_cmdline[sizeof("mkntfs ") + strlen(mkntfs_args) +
449 sizeof(" ") + strlen(dev)];
450 sprintf(mkntfs_cmdline, "mkntfs %s %s", mkntfs_args, dev);
451 puts(mkntfs_cmdline);
452 ret = system(mkntfs_cmdline);
454 imagex_error_with_errno("Failed to execute the "
457 } else if (ret > 0) {
458 imagex_error("`mkntfs' exited with failure status");
459 imagex_error("Note: You can pass additional arguments "
460 "to `mkntfs' using the --mkntfs-args "
464 sprintf(tmpdir, "/tmp/imagex-%d-ntfsmount-%s-XXXXXX",
466 dir = mkdtemp(tmpdir);
468 imagex_error_with_errno("Failed to create "
469 "temporary directory "
472 char ntfs_3g_cmdline[sizeof("ntfs-3g ") + strlen(dev) +
473 sizeof(" ") + strlen(dir)];
474 sprintf(ntfs_3g_cmdline, "ntfs-3g %s %s", dev, dir);
475 puts(ntfs_3g_cmdline);
476 ret = system(ntfs_3g_cmdline);
478 imagex_error_with_errno("Failed to execute the "
479 "`ntfs-3g' program");
482 } else if (ret > 0) {
483 imagex_error("`ntfs-3g' exited with failure status");
487 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
488 image, wimfile, dev);
492 ret = wimlib_extract_image(w, image, dir, extract_flags);
496 if (tmpdir[0] != '\0') {
497 /* Unmount and remove the NTFS-3g mounted directory */
502 imagex_error_with_errno("Failed to fork()");
506 execlp("fusermount", "fusermount", "-u", dir, NULL);
507 imagex_error_with_errno("Failed to execute `fusermount'");
511 if (waitpid(pid, &status, 0) == -1) {
512 imagex_error_with_errno("Failed to wait for "
513 "fusermount process to "
519 imagex_error("fusermount exited with status %d",
523 if (rmdir(tmpdir) != 0) {
524 imagex_error_with_errno("Failed to remove temporary "
525 "directory `%s'", tmpdir);
533 /* Create a WIM file from a directory. */
534 static int imagex_capture(int argc, const char **argv)
537 int add_image_flags = 0;
538 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
539 int compression_type = WIM_COMPRESSION_TYPE_NONE;
540 const char *flags_element = NULL;
548 for_opt(c, capture_options) {
551 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
554 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
557 compression_type = get_compression_type(optarg);
558 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
562 flags_element = optarg;
565 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
568 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
571 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
581 if (argc < 2 || argc > 4) {
587 name = (argc >= 3) ? argv[2] : dir;
588 desc = (argc >= 4) ? argv[3] : NULL;
590 ret = wimlib_create_new_wim(compression_type, &w);
594 ret = wimlib_add_image(w, dir, name, desc, flags_element,
597 imagex_error("Failed to add the image `%s'", dir);
601 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
603 imagex_error("Failed to write the WIM file `%s'", wimfile);
609 /* Remove image(s) from a WIM. */
610 static int imagex_delete(int argc, const char **argv)
613 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
614 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
616 const char *image_num_or_name;
621 for_opt(c, delete_options) {
624 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
625 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
637 imagex_error("Must specify a WIM file");
639 imagex_error("Must specify an image");
644 image_num_or_name = argv[1];
646 ret = wimlib_open_wim(wimfile, open_flags, &w);
650 image = wimlib_resolve_image(w, image_num_or_name);
652 ret = verify_image_exists(image);
656 ret = wimlib_delete_image(w, image);
658 imagex_error("Failed to delete image from `%s'", wimfile);
662 ret = wimlib_overwrite(w, write_flags);
664 imagex_error("Failed to write the file `%s' with image "
672 /* Print the files contained in an image(s) in a WIM file. */
673 static int imagex_dir(int argc, const char **argv)
683 imagex_error("Must specify a WIM file");
688 imagex_error("Too many arguments");
694 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
698 part_number = wimlib_get_part_number(w, NULL);
699 if (part_number != 1) {
700 imagex_error("`%s' is part %d of a split WIM! Specify the "
701 "first part to see the files",
702 wimfile, part_number);
703 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
708 image = wimlib_resolve_image(w, argv[2]);
709 ret = verify_image_exists(image);
713 /* Image was not specified. If the WIM only contains one image,
714 * choose that one; otherwise, print an error. */
715 num_images = wimlib_get_num_images(w);
716 if (num_images != 1) {
717 imagex_error("The file `%s' contains %d images; Please "
718 "select one.", wimfile, num_images);
726 ret = wimlib_print_files(w, image);
732 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
734 static int imagex_export(int argc, const char **argv)
737 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
738 int export_flags = 0;
739 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
740 int compression_type = WIM_COMPRESSION_TYPE_NONE;
741 bool compression_type_specified = false;
742 const char *src_wimfile;
743 const char *src_image_num_or_name;
744 const char *dest_wimfile;
745 const char *dest_name;
746 const char *dest_desc;
747 WIMStruct *src_w = NULL;
748 WIMStruct *dest_w = NULL;
754 for_opt(c, export_options) {
757 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
760 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
761 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
764 compression_type = get_compression_type(optarg);
765 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
767 compression_type_specified = true;
776 if (argc < 3 || argc > 5) {
780 src_wimfile = argv[0];
781 src_image_num_or_name = argv[1];
782 dest_wimfile = argv[2];
783 dest_name = (argc >= 4) ? argv[3] : NULL;
784 dest_desc = (argc >= 5) ? argv[4] : NULL;
785 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
789 /* Determine if the destination is an existing file or not.
790 * If so, we try to append the exported image(s) to it; otherwise, we
791 * create a new WIM containing the exported image(s). */
792 if (stat(dest_wimfile, &stbuf) == 0) {
794 /* Destination file exists. */
795 if (!S_ISREG(stbuf.st_mode)) {
796 imagex_error("`%s' is not a regular file",
800 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
804 if (compression_type_specified && compression_type !=
805 wimlib_get_compression_type(dest_w)) {
806 imagex_error("Cannot specify a compression type that is "
807 "not the same as that used in the "
812 compression_type = wimlib_get_compression_type(dest_w);
815 /* dest_wimfile is not an existing file, so create a new WIM. */
816 if (errno == ENOENT) {
817 ret = wimlib_create_new_wim(compression_type, &dest_w);
821 imagex_error_with_errno("Cannot stat file `%s'",
827 image = wimlib_resolve_image(src_w, src_image_num_or_name);
828 ret = verify_image_exists(image);
832 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
839 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
842 ret = wimlib_overwrite(dest_w, write_flags);
849 /* Prints information about a WIM file; also can mark an image as bootable,
850 * change the name of an image, or change the description of an image. */
851 static int imagex_info(int argc, const char **argv)
857 bool lookup_table = false;
859 bool metadata = false;
860 bool short_header = true;
861 const char *xml_out_file = NULL;
863 const char *image_num_or_name = "all";
864 const char *new_name = NULL;
865 const char *new_desc = NULL;
870 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
871 WIMLIB_OPEN_FLAG_SPLIT_OK;
875 for_opt(c, info_options) {
885 short_header = false;
889 short_header = false;
893 short_header = false;
896 xml_out_file = optarg;
897 short_header = false;
901 short_header = false;
911 if (argc == 0 || argc > 4) {
917 image_num_or_name = argv[1];
927 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
929 ret = wimlib_open_wim(wimfile, open_flags, &w);
933 part_number = wimlib_get_part_number(w, &total_parts);
935 /*if (total_parts > 1 && part_number > 1) {*/
936 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
937 /*" Select the first part if you want to see information\n"*/
938 /*" about images in the WIM.\n", */
939 /*part_number, total_parts);*/
942 image = wimlib_resolve_image(w, image_num_or_name);
943 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
944 imagex_error("The image `%s' does not exist",
947 imagex_error("If you would like to set the boot "
948 "index to 0, specify image \"0\" with "
950 ret = WIMLIB_ERR_INVALID_IMAGE;
954 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
956 imagex_error("Cannot specify the --boot flag "
957 "without specifying a specific "
958 "image in a multi-image WIM");
959 ret = WIMLIB_ERR_INVALID_IMAGE;
963 imagex_error("Cannot specify the NEW_NAME "
964 "without specifying a specific "
965 "image in a multi-image WIM");
966 ret = WIMLIB_ERR_INVALID_IMAGE;
971 /* Operations that print information are separated from operations that
972 * recreate the WIM file. */
973 if (!new_name && !boot) {
975 if (image == WIM_NO_IMAGE) {
976 imagex_error("`%s' is not a valid image",
978 ret = WIMLIB_ERR_INVALID_IMAGE;
982 if (image == WIM_ALL_IMAGES && short_header)
983 wimlib_print_wim_information(w);
986 wimlib_print_header(w);
989 if (total_parts != 1) {
990 printf("Warning: Only showing the lookup table "
991 "for part %d of a %d-part WIM.\n",
992 part_number, total_parts);
994 wimlib_print_lookup_table(w);
998 ret = wimlib_extract_xml_data(w, stdout);
1004 fp = fopen(xml_out_file, "wb");
1006 imagex_error_with_errno("Failed to open the "
1012 ret = wimlib_extract_xml_data(w, fp);
1013 if (fclose(fp) != 0) {
1014 imagex_error("Failed to close the file `%s'",
1024 wimlib_print_available_images(w, image);
1027 if (total_parts != 1 && part_number != 1) {
1028 imagex_error("Select part 1 of this %d-part WIM "
1029 "to see the image metadata",
1031 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1033 ret = wimlib_print_metadata(w, image);
1038 if (total_parts != 1) {
1039 imagex_error("Modifying a split WIM is not supported.");
1042 if (image == WIM_ALL_IMAGES)
1045 if (image == WIM_NO_IMAGE && new_name) {
1046 imagex_error("Cannot specify new_name (`%s') when "
1047 "using image 0", new_name);
1052 if (image == wimlib_get_boot_idx(w)) {
1053 printf("Image %d is already marked as "
1054 "bootable.\n", image);
1057 printf("Marking image %d as bootable.\n",
1059 wimlib_set_boot_idx(w, image);
1063 if (strcmp(wimlib_get_image_name(w, image),
1065 printf("Image %d is already named \"%s\".\n",
1069 printf("Changing the name of image %d to "
1070 "\"%s\".\n", image, new_name);
1071 ret = wimlib_set_image_name(w, image, new_name);
1077 const char *old_desc;
1078 old_desc = wimlib_get_image_description(w, image);
1079 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1080 printf("The description of image %d is already "
1081 "\"%s\".\n", image, new_desc);
1084 printf("Changing the description of image %d "
1085 "to \"%s\".\n", image, new_desc);
1086 ret = wimlib_set_image_descripton(w, image,
1093 /* Only call wimlib_overwrite_xml_and_header() if something
1094 * actually needs to be changed. */
1095 if (boot || new_name || new_desc ||
1096 check != wimlib_has_integrity_table(w)) {
1098 ret = wimlib_overwrite_xml_and_header(w, check ?
1099 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1100 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1102 printf("The file `%s' was not modified because nothing "
1103 "needed to be done.\n", wimfile);
1113 /* Join split WIMs into one part WIM */
1114 static int imagex_join(int argc, const char **argv)
1117 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1118 const char *output_path;
1120 for_opt(c, join_options) {
1123 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1133 imagex_error("Must specify at least one split WIM (.swm) parts "
1137 output_path = argv[0];
1138 return wimlib_join(++argv, --argc, output_path, flags);
1144 /* Mounts an image using a FUSE mount. */
1145 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1148 int mount_flags = 0;
1149 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1150 const char *wimfile;
1157 if (strcmp(argv[0], "mountrw") == 0)
1158 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1159 for_opt(c, mount_options) {
1162 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1165 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1168 if (strcasecmp(optarg, "none") == 0)
1169 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1170 else if (strcasecmp(optarg, "xattr") == 0)
1171 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1172 else if (strcasecmp(optarg, "windows") == 0)
1173 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1175 imagex_error("Unknown stream interface \"%s\"", optarg);
1185 if (argc != 2 && argc != 3)
1190 ret = wimlib_open_wim(wimfile, open_flags, &w);
1196 num_images = wimlib_get_num_images(w);
1197 if (num_images != 1) {
1198 imagex_error("The file `%s' contains %d images; Please "
1199 "select one", wimfile, num_images);
1200 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1202 ret = WIMLIB_ERR_INVALID_IMAGE;
1207 image = wimlib_resolve_image(w, argv[1]);
1211 ret = verify_image_exists_and_is_single(image);
1215 ret = wimlib_mount(w, image, dir, mount_flags);
1217 imagex_error("Failed to mount image %d from `%s' on `%s'",
1218 image, wimfile, dir);
1225 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1230 /* Split a WIM into a spanned set */
1231 static int imagex_split(int argc, const char **argv)
1234 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1235 unsigned long part_size;
1237 for_opt(c, split_options) {
1240 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1254 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1255 return wimlib_split(argv[0], argv[1], part_size, flags);
1258 /* Unmounts an image. */
1259 static int imagex_unmount(int argc, const char **argv)
1262 int unmount_flags = 0;
1265 for_opt(c, unmount_options) {
1268 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1271 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1285 ret = wimlib_unmount(argv[0], unmount_flags);
1287 imagex_error("Failed to unmount `%s'", argv[0]);
1291 struct imagex_command {
1293 int (*func)(int , const char **);
1297 static struct imagex_command imagex_commands[] = {
1298 {"append", imagex_append, APPEND},
1299 {"apply", imagex_apply, APPLY},
1300 {"capture", imagex_capture, CAPTURE},
1301 {"delete", imagex_delete, DELETE},
1302 {"dir", imagex_dir, DIR},
1303 {"export", imagex_export, EXPORT},
1304 {"info", imagex_info, INFO},
1305 {"join", imagex_join, JOIN},
1306 {"mount", imagex_mount_rw_or_ro, MOUNT},
1307 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1308 {"split", imagex_split, SPLIT},
1309 {"unmount", imagex_unmount, UNMOUNT},
1312 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1313 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1315 static void help_or_version(int argc, const char **argv)
1319 struct imagex_command *cmd;
1321 for (i = 1; i < argc; i++) {
1329 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1330 for_imagex_command(cmd) {
1331 if (strcmp(cmd->name, argv[1]) == 0) {
1339 if (strcmp(p, "version") == 0) {
1347 int main(int argc, const char **argv)
1349 struct imagex_command *cmd;
1353 imagex_error("No command specified");
1358 help_or_version(argc, argv);
1362 wimlib_set_print_errors(true);
1364 for_imagex_command(cmd) {
1365 if (strcmp(cmd->name, *argv) == 0) {
1366 ret = cmd->func(argc, argv);
1368 imagex_error("Exiting with error code %d:\n"
1370 wimlib_get_error_string(ret));
1371 if (ret == WIMLIB_ERR_NTFS_3G)
1372 imagex_error_with_errno("errno");
1378 imagex_error("Unrecognized command: `%s'", argv[0]);
1382 /*#ifndef WITH_NTFS_3G*/
1383 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1384 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1385 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1386 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1387 /*return WIMLIB_ERR_UNSUPPORTED;*/