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 | NTFS_VOLUME) WIMFILE [\"IMAGE_NAME\"]\n"
84 " [\"DESCRIPTION\"] [--boot] [--check] [--flags EDITIONID]\n"
85 " [--dereference] [--config=FILE]\n",
87 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
88 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
89 " [--symlink] [--verbose]\n",
91 " imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [\"IMAGE_NAME\"]\n"
92 " [\"DESCRIPTION\"] [--boot] [--check] [--compress[=TYPE]]\n"
93 " [--flags \"EditionID\"] [--verbose] [--dereference]\n"
96 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
98 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | \"all\")\n",
100 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
101 " DEST_WIMFILE [\"DEST_IMAGE_NAME\"] [\"DEST_IMAGE_DESCRIPTION\"]\n"
102 " [--boot] [--check] [--compress[=TYPE]]\n",
104 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
105 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
106 " [--xml] [--extract-xml FILE] [--metadata]\n",
108 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
110 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
111 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
113 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
114 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
116 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE [--check]\n",
118 " imagex unmount DIRECTORY [--commit] [--check]\n",
121 static const struct option common_options[] = {
122 {"help", 0, NULL, 'h'},
123 {"version", 0, NULL, 'v'},
127 static const struct option append_options[] = {
128 {"boot", no_argument, NULL, 'b'},
129 {"check", no_argument, NULL, 'c'},
130 {"flags", required_argument, NULL, 'f'},
131 {"dereference", no_argument, NULL, 'L'},
132 {"config", required_argument, NULL, 'C'},
135 static const struct option apply_options[] = {
136 {"check", no_argument, NULL, 'c'},
137 {"hardlink", no_argument, NULL, 'h'},
138 {"symlink", no_argument, NULL, 's'},
139 {"verbose", no_argument, NULL, 'v'},
142 static const struct option capture_options[] = {
143 {"boot", no_argument, NULL, 'b'},
144 {"check", no_argument, NULL, 'c'},
145 {"compress", optional_argument, NULL, 'x'},
146 {"flags", required_argument, NULL, 'f'},
147 {"verbose", no_argument, NULL,'v'},
148 {"dereference", no_argument, NULL, 'L'},
149 {"config", required_argument, NULL, 'C'},
152 static const struct option delete_options[] = {
153 {"check", no_argument, NULL, 'c'},
157 static const struct option export_options[] = {
158 {"boot", no_argument, NULL, 'b'},
159 {"check", no_argument , NULL, 'c'},
160 {"compress", optional_argument, NULL, 'x'},
164 static const struct option info_options[] = {
165 {"boot", no_argument, NULL, 'b'},
166 {"check", no_argument, NULL, 'c'},
167 {"header", no_argument, NULL, 'h'},
168 {"lookup-table", no_argument, NULL, 'l'},
169 {"xml", no_argument, NULL, 'x'},
170 {"extract-xml", required_argument, NULL, 'X'},
171 {"metadata", no_argument, NULL, 'm'},
175 static const struct option join_options[] = {
176 {"check", no_argument, NULL, 'c'},
180 static const struct option mount_options[] = {
181 {"check", no_argument, NULL, 'c'},
182 {"debug", no_argument, NULL, 'd'},
183 {"stream-interface", required_argument, NULL, 's'},
187 static const struct option split_options[] = {
188 {"check", no_argument, NULL, 'c'},
192 static const struct option unmount_options[] = {
193 {"commit", no_argument, NULL, 'c'},
194 {"check", no_argument, NULL, 'C'},
199 /* Print formatted error message to stderr. */
200 static void imagex_error(const char *format, ...)
203 va_start(va, format);
204 fputs("ERROR: ", stderr);
205 vfprintf(stderr, format, va);
210 /* Print formatted error message to stderr. */
211 static void imagex_error_with_errno(const char *format, ...)
213 int errno_save = errno;
215 va_start(va, format);
216 fputs("ERROR: ", stderr);
217 vfprintf(stderr, format, va);
218 fprintf(stderr, ": %s\n", strerror(errno_save));
223 static inline void version()
225 static const char *s =
226 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
227 "Copyright (C) 2012 Eric Biggers\n"
228 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
229 "This is free software: you are free to change and redistribute it.\n"
230 "There is NO WARRANTY, to the extent permitted by law.\n"
232 "Report bugs to "PACKAGE_BUGREPORT".\n";
236 static inline void usage(int cmd)
238 puts("IMAGEX: Usage:");
239 fputs(usage_strings[cmd], stdout);
242 static void usage_all()
244 puts("IMAGEX: Usage:");
245 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
246 fputs(usage_strings[i], stdout);
247 static const char *extra =
249 " imagex --version\n"
251 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
253 fputs(extra, stdout);
256 static int verify_image_exists(int image)
258 if (image == WIM_NO_IMAGE) {
259 imagex_error("Not a valid image");
260 return WIMLIB_ERR_INVALID_IMAGE;
265 static int verify_image_is_single(int image)
267 if (image == WIM_ALL_IMAGES) {
268 imagex_error("Cannot specify all images for this action");
269 return WIMLIB_ERR_INVALID_IMAGE;
274 static int verify_image_exists_and_is_single(int image)
277 ret = verify_image_exists(image);
279 ret = verify_image_is_single(image);
283 static int get_compression_type(const char *optarg)
286 return WIM_COMPRESSION_TYPE_XPRESS;
287 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
288 return WIM_COMPRESSION_TYPE_LZX;
289 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
290 return WIM_COMPRESSION_TYPE_XPRESS;
291 else if (strcasecmp(optarg, "none") == 0)
292 return WIM_COMPRESSION_TYPE_NONE;
294 imagex_error("Invalid compression type `%s'! Must be "
295 "\"maximum\", \"fast\", or \"none\".", optarg);
296 return WIM_COMPRESSION_TYPE_INVALID;
300 static const char *file_get_contents(const char *filename, size_t *len_ret)
307 if (stat(filename, &stbuf) != 0) {
308 imagex_error_with_errno("Failed to stat the file `%s'", filename);
313 fp = fopen(filename, "rb");
315 imagex_error_with_errno("Failed to open the file `%s'", filename);
321 imagex_error("Failed to allocate buffer of %zu bytes to hold "
322 "contents of file `%s'", len, filename);
325 if (fread(buf, 1, len, fp) != len) {
326 imagex_error_with_errno("Failed to read %lu bytes from the "
327 "file `%s'", len, filename);
339 static int imagex_append(int argc, const char **argv)
342 const char *flags_element = NULL;
343 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
344 int add_image_flags = 0;
345 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
350 const char *config_file = NULL;
351 const char *config_str = NULL;
352 size_t config_len = 0;
356 for_opt(c, append_options) {
359 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
362 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
363 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
366 config_file = optarg;
369 flags_element = optarg;
372 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
381 if (argc < 2 || argc > 4) {
387 name = (argc >= 3) ? argv[2] : path_basename(dir);
388 desc = (argc >= 4) ? argv[3] : NULL;
391 config_str = file_get_contents(config_file, &config_len);
396 ret = wimlib_open_wim(wimfile, open_flags, &w);
403 ret = stat(dir, &stbuf);
405 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
406 const char *ntfs_device = dir;
407 printf("Capturing WIM image NTFS filesystem on `%s'\n",
409 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
418 if (errno != -ENOENT)
419 imagex_error_with_errno("Failed to stat `%s'", dir);
422 ret = wimlib_add_image(w, dir, name, desc,
423 flags_element, config_str, config_len,
429 ret = wimlib_overwrite(w, write_flags);
435 /* Extract one image, or all images, from a WIM file into a directory. */
436 static int imagex_apply(int argc, const char **argv)
439 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
446 const char *image_num_or_name;
447 int extract_flags = 0;
449 for_opt(c, apply_options) {
452 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
455 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
458 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
461 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
470 if (argc != 2 && argc != 3) {
477 image_num_or_name = "1";
480 image_num_or_name = argv[1];
484 ret = wimlib_open_wim(wimfile, open_flags, &w);
488 image = wimlib_resolve_image(w, image_num_or_name);
489 ret = verify_image_exists(image);
493 num_images = wimlib_get_num_images(w);
494 if (argc == 2 && num_images != 1) {
495 imagex_error("`%s' contains %d images; Please select one "
496 "(or all)", wimfile, num_images);
505 ret = stat(dir, &stbuf);
507 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
508 const char *ntfs_device = dir;
509 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
510 image, wimfile, ntfs_device);
511 ret = wimlib_apply_image_to_ntfs_volume(w, image,
517 if (errno != -ENOENT)
518 imagex_error_with_errno("Failed to stat `%s'", dir);
522 ret = wimlib_extract_image(w, image, dir, extract_flags);
529 /* Create a WIM file from a directory. */
530 static int imagex_capture(int argc, const char **argv)
533 int add_image_flags = 0;
534 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
535 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
536 const char *flags_element = NULL;
541 const char *config_file = NULL;
542 const char *config_str = NULL;
543 size_t config_len = 0;
547 for_opt(c, capture_options) {
550 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
553 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
556 config_file = optarg;
559 compression_type = get_compression_type(optarg);
560 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
564 flags_element = optarg;
567 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
568 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
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;
591 config_str = file_get_contents(config_file, &config_len);
596 ret = wimlib_create_new_wim(compression_type, &w);
603 ret = stat(dir, &stbuf);
605 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
606 const char *ntfs_device = dir;
607 printf("Capturing WIM image NTFS filesystem on `%s'\n",
609 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
618 if (errno != -ENOENT)
619 imagex_error_with_errno("Failed to stat `%s'", dir);
622 ret = wimlib_add_image(w, dir, name, desc, flags_element, config_str,
623 config_len, add_image_flags);
627 imagex_error("Failed to add the image `%s'", dir);
631 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
633 imagex_error("Failed to write the WIM file `%s'", wimfile);
639 /* Remove image(s) from a WIM. */
640 static int imagex_delete(int argc, const char **argv)
643 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
644 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
646 const char *image_num_or_name;
651 for_opt(c, delete_options) {
654 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
655 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
667 imagex_error("Must specify a WIM file");
669 imagex_error("Must specify an image");
674 image_num_or_name = argv[1];
676 ret = wimlib_open_wim(wimfile, open_flags, &w);
680 image = wimlib_resolve_image(w, image_num_or_name);
682 ret = verify_image_exists(image);
686 ret = wimlib_delete_image(w, image);
688 imagex_error("Failed to delete image from `%s'", wimfile);
692 ret = wimlib_overwrite(w, write_flags);
694 imagex_error("Failed to write the file `%s' with image "
702 /* Print the files contained in an image(s) in a WIM file. */
703 static int imagex_dir(int argc, const char **argv)
713 imagex_error("Must specify a WIM file");
718 imagex_error("Too many arguments");
724 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
728 part_number = wimlib_get_part_number(w, NULL);
729 if (part_number != 1) {
730 imagex_error("`%s' is part %d of a split WIM! Specify the "
731 "first part to see the files",
732 wimfile, part_number);
733 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
738 image = wimlib_resolve_image(w, argv[2]);
739 ret = verify_image_exists(image);
743 /* Image was not specified. If the WIM only contains one image,
744 * choose that one; otherwise, print an error. */
745 num_images = wimlib_get_num_images(w);
746 if (num_images != 1) {
747 imagex_error("The file `%s' contains %d images; Please "
748 "select one.", wimfile, num_images);
756 ret = wimlib_print_files(w, image);
762 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
764 static int imagex_export(int argc, const char **argv)
767 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
768 int export_flags = 0;
769 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
770 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
771 bool compression_type_specified = false;
772 const char *src_wimfile;
773 const char *src_image_num_or_name;
774 const char *dest_wimfile;
775 const char *dest_name;
776 const char *dest_desc;
777 WIMStruct *src_w = NULL;
778 WIMStruct *dest_w = NULL;
784 for_opt(c, export_options) {
787 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
790 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
791 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
794 compression_type = get_compression_type(optarg);
795 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
797 compression_type_specified = true;
806 if (argc < 3 || argc > 5) {
810 src_wimfile = argv[0];
811 src_image_num_or_name = argv[1];
812 dest_wimfile = argv[2];
813 dest_name = (argc >= 4) ? argv[3] : NULL;
814 dest_desc = (argc >= 5) ? argv[4] : NULL;
815 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
819 /* Determine if the destination is an existing file or not.
820 * If so, we try to append the exported image(s) to it; otherwise, we
821 * create a new WIM containing the exported image(s). */
822 if (stat(dest_wimfile, &stbuf) == 0) {
824 /* Destination file exists. */
825 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
826 imagex_error("`%s' is not a regular file",
830 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
834 if (compression_type_specified && compression_type !=
835 wimlib_get_compression_type(dest_w)) {
836 imagex_error("Cannot specify a compression type that is "
837 "not the same as that used in the "
842 compression_type = wimlib_get_compression_type(dest_w);
845 /* dest_wimfile is not an existing file, so create a new WIM. */
846 if (errno == ENOENT) {
847 ret = wimlib_create_new_wim(compression_type, &dest_w);
851 imagex_error_with_errno("Cannot stat file `%s'",
857 image = wimlib_resolve_image(src_w, src_image_num_or_name);
858 ret = verify_image_exists(image);
862 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
869 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
872 ret = wimlib_overwrite(dest_w, write_flags);
879 /* Prints information about a WIM file; also can mark an image as bootable,
880 * change the name of an image, or change the description of an image. */
881 static int imagex_info(int argc, const char **argv)
887 bool lookup_table = false;
889 bool metadata = false;
890 bool short_header = true;
891 const char *xml_out_file = NULL;
893 const char *image_num_or_name = "all";
894 const char *new_name = NULL;
895 const char *new_desc = NULL;
900 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
901 WIMLIB_OPEN_FLAG_SPLIT_OK;
905 for_opt(c, info_options) {
915 short_header = false;
919 short_header = false;
923 short_header = false;
926 xml_out_file = optarg;
927 short_header = false;
931 short_header = false;
941 if (argc == 0 || argc > 4) {
947 image_num_or_name = argv[1];
957 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
959 ret = wimlib_open_wim(wimfile, open_flags, &w);
963 part_number = wimlib_get_part_number(w, &total_parts);
965 /*if (total_parts > 1 && part_number > 1) {*/
966 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
967 /*" Select the first part if you want to see information\n"*/
968 /*" about images in the WIM.\n", */
969 /*part_number, total_parts);*/
972 image = wimlib_resolve_image(w, image_num_or_name);
973 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
974 imagex_error("The image `%s' does not exist",
977 imagex_error("If you would like to set the boot "
978 "index to 0, specify image \"0\" with "
980 ret = WIMLIB_ERR_INVALID_IMAGE;
984 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
986 imagex_error("Cannot specify the --boot flag "
987 "without specifying a specific "
988 "image in a multi-image WIM");
989 ret = WIMLIB_ERR_INVALID_IMAGE;
993 imagex_error("Cannot specify the NEW_NAME "
994 "without specifying a specific "
995 "image in a multi-image WIM");
996 ret = WIMLIB_ERR_INVALID_IMAGE;
1001 /* Operations that print information are separated from operations that
1002 * recreate the WIM file. */
1003 if (!new_name && !boot) {
1005 if (image == WIM_NO_IMAGE) {
1006 imagex_error("`%s' is not a valid image",
1008 ret = WIMLIB_ERR_INVALID_IMAGE;
1012 if (image == WIM_ALL_IMAGES && short_header)
1013 wimlib_print_wim_information(w);
1016 wimlib_print_header(w);
1019 if (total_parts != 1) {
1020 printf("Warning: Only showing the lookup table "
1021 "for part %d of a %d-part WIM.\n",
1022 part_number, total_parts);
1024 wimlib_print_lookup_table(w);
1028 ret = wimlib_extract_xml_data(w, stdout);
1034 fp = fopen(xml_out_file, "wb");
1036 imagex_error_with_errno("Failed to open the "
1042 ret = wimlib_extract_xml_data(w, fp);
1043 if (fclose(fp) != 0) {
1044 imagex_error("Failed to close the file `%s'",
1054 wimlib_print_available_images(w, image);
1057 if (total_parts != 1 && part_number != 1) {
1058 imagex_error("Select part 1 of this %d-part WIM "
1059 "to see the image metadata",
1061 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1063 ret = wimlib_print_metadata(w, image);
1068 if (total_parts != 1) {
1069 imagex_error("Modifying a split WIM is not supported.");
1072 if (image == WIM_ALL_IMAGES)
1075 if (image == WIM_NO_IMAGE && new_name) {
1076 imagex_error("Cannot specify new_name (`%s') when "
1077 "using image 0", new_name);
1082 if (image == wimlib_get_boot_idx(w)) {
1083 printf("Image %d is already marked as "
1084 "bootable.\n", image);
1087 printf("Marking image %d as bootable.\n",
1089 wimlib_set_boot_idx(w, image);
1093 if (strcmp(wimlib_get_image_name(w, image),
1095 printf("Image %d is already named \"%s\".\n",
1099 printf("Changing the name of image %d to "
1100 "\"%s\".\n", image, new_name);
1101 ret = wimlib_set_image_name(w, image, new_name);
1107 const char *old_desc;
1108 old_desc = wimlib_get_image_description(w, image);
1109 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1110 printf("The description of image %d is already "
1111 "\"%s\".\n", image, new_desc);
1114 printf("Changing the description of image %d "
1115 "to \"%s\".\n", image, new_desc);
1116 ret = wimlib_set_image_descripton(w, image,
1123 /* Only call wimlib_overwrite_xml_and_header() if something
1124 * actually needs to be changed. */
1125 if (boot || new_name || new_desc ||
1126 check != wimlib_has_integrity_table(w)) {
1128 ret = wimlib_overwrite_xml_and_header(w, check ?
1129 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1130 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1132 printf("The file `%s' was not modified because nothing "
1133 "needed to be done.\n", wimfile);
1143 /* Join split WIMs into one part WIM */
1144 static int imagex_join(int argc, const char **argv)
1147 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1148 const char *output_path;
1150 for_opt(c, join_options) {
1153 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1163 imagex_error("Must specify at least one split WIM (.swm) parts "
1167 output_path = argv[0];
1168 return wimlib_join(++argv, --argc, output_path, flags);
1174 /* Mounts an image using a FUSE mount. */
1175 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1178 int mount_flags = 0;
1179 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1180 const char *wimfile;
1187 if (strcmp(argv[0], "mountrw") == 0)
1188 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1189 for_opt(c, mount_options) {
1192 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1195 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1198 if (strcasecmp(optarg, "none") == 0)
1199 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1200 else if (strcasecmp(optarg, "xattr") == 0)
1201 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1202 else if (strcasecmp(optarg, "windows") == 0)
1203 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1205 imagex_error("Unknown stream interface \"%s\"", optarg);
1215 if (argc != 2 && argc != 3)
1220 ret = wimlib_open_wim(wimfile, open_flags, &w);
1226 num_images = wimlib_get_num_images(w);
1227 if (num_images != 1) {
1228 imagex_error("The file `%s' contains %d images; Please "
1229 "select one", wimfile, num_images);
1230 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1232 ret = WIMLIB_ERR_INVALID_IMAGE;
1237 image = wimlib_resolve_image(w, argv[1]);
1241 ret = verify_image_exists_and_is_single(image);
1245 ret = wimlib_mount(w, image, dir, mount_flags);
1247 imagex_error("Failed to mount image %d from `%s' on `%s'",
1248 image, wimfile, dir);
1255 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1260 /* Split a WIM into a spanned set */
1261 static int imagex_split(int argc, const char **argv)
1264 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1265 unsigned long part_size;
1267 for_opt(c, split_options) {
1270 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1284 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1285 return wimlib_split(argv[0], argv[1], part_size, flags);
1288 /* Unmounts an image. */
1289 static int imagex_unmount(int argc, const char **argv)
1292 int unmount_flags = 0;
1295 for_opt(c, unmount_options) {
1298 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1301 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1315 ret = wimlib_unmount(argv[0], unmount_flags);
1317 imagex_error("Failed to unmount `%s'", argv[0]);
1321 struct imagex_command {
1323 int (*func)(int , const char **);
1327 static struct imagex_command imagex_commands[] = {
1328 {"append", imagex_append, APPEND},
1329 {"apply", imagex_apply, APPLY},
1330 {"capture", imagex_capture, CAPTURE},
1331 {"delete", imagex_delete, DELETE},
1332 {"dir", imagex_dir, DIR},
1333 {"export", imagex_export, EXPORT},
1334 {"info", imagex_info, INFO},
1335 {"join", imagex_join, JOIN},
1336 {"mount", imagex_mount_rw_or_ro, MOUNT},
1337 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1338 {"split", imagex_split, SPLIT},
1339 {"unmount", imagex_unmount, UNMOUNT},
1342 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1343 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1345 static void help_or_version(int argc, const char **argv)
1349 struct imagex_command *cmd;
1351 for (i = 1; i < argc; i++) {
1359 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1360 for_imagex_command(cmd) {
1361 if (strcmp(cmd->name, argv[1]) == 0) {
1369 if (strcmp(p, "version") == 0) {
1377 int main(int argc, const char **argv)
1379 struct imagex_command *cmd;
1383 imagex_error("No command specified");
1388 help_or_version(argc, argv);
1392 wimlib_set_print_errors(true);
1394 for_imagex_command(cmd) {
1395 if (strcmp(cmd->name, *argv) == 0) {
1396 ret = cmd->func(argc, argv);
1398 imagex_error("Exiting with error code %d:\n"
1400 wimlib_get_error_string(ret));
1401 if (ret == WIMLIB_ERR_NTFS_3G)
1402 imagex_error_with_errno("errno");
1408 imagex_error("Unrecognized command: `%s'", argv[0]);
1412 /*#ifndef WITH_NTFS_3G*/
1413 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1414 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1415 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1416 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1417 /*return WIMLIB_ERR_UNSUPPORTED;*/