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/>.
36 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
38 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
41 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
60 static void usage(int cmd_type);
61 static void usage_all();
63 static const char *usage_strings[] = {
65 " imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
66 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
67 " [--verbose] [--dereference] [--config=FILE]\n",
69 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
70 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
71 " [--symlink] [--verbose] [--ref=\"GLOB\"]\n",
73 " imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
74 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
75 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
78 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
80 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
82 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
83 " DEST_WIMFILE [DEST_IMAGE_NAME]\n"
84 " [DEST_IMAGE_DESCRIPTION] [--boot] [--check]\n"
85 " [--compress=TYPE] [--ref=\"GLOB\"]\n",
87 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
88 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
89 " [--xml] [--extract-xml FILE] [--metadata]\n",
91 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
93 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
94 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
95 " [--ref=\"GLOB\"]\n",
97 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
98 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
100 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
102 " imagex unmount DIRECTORY [--commit] [--check]\n",
105 static const struct option common_options[] = {
106 {"help", 0, NULL, 'h'},
107 {"version", 0, NULL, 'v'},
111 static const struct option apply_options[] = {
112 {"check", no_argument, NULL, 'c'},
113 {"hardlink", no_argument, NULL, 'h'},
114 {"symlink", no_argument, NULL, 's'},
115 {"verbose", no_argument, NULL, 'v'},
116 {"ref", required_argument, NULL, 'r'},
119 static const struct option capture_or_append_options[] = {
120 {"boot", no_argument, NULL, 'b'},
121 {"check", no_argument, NULL, 'c'},
122 {"compress", required_argument, NULL, 'x'},
123 {"config", required_argument, NULL, 'C'},
124 {"dereference", no_argument, NULL, 'L'},
125 {"flags", required_argument, NULL, 'f'},
126 {"verbose", no_argument, NULL, 'v'},
129 static const struct option delete_options[] = {
130 {"check", no_argument, NULL, 'c'},
134 static const struct option export_options[] = {
135 {"boot", no_argument, NULL, 'b'},
136 {"check", no_argument, NULL, 'c'},
137 {"compress", required_argument, NULL, 'x'},
138 {"ref", required_argument, NULL, 'r'},
142 static const struct option info_options[] = {
143 {"boot", no_argument, NULL, 'b'},
144 {"check", no_argument, NULL, 'c'},
145 {"extract-xml", required_argument, NULL, 'X'},
146 {"header", no_argument, NULL, 'h'},
147 {"lookup-table", no_argument, NULL, 'l'},
148 {"metadata", no_argument, NULL, 'm'},
149 {"xml", no_argument, NULL, 'x'},
153 static const struct option join_options[] = {
154 {"check", no_argument, NULL, 'c'},
158 static const struct option mount_options[] = {
159 {"check", no_argument, NULL, 'c'},
160 {"debug", no_argument, NULL, 'd'},
161 {"streams-interface", required_argument, NULL, 's'},
162 {"ref", required_argument, NULL, 'r'},
166 static const struct option split_options[] = {
167 {"check", no_argument, NULL, 'c'},
171 static const struct option unmount_options[] = {
172 {"commit", no_argument, NULL, 'c'},
173 {"check", no_argument, NULL, 'C'},
179 /* Print formatted error message to stderr. */
180 static void imagex_error(const char *format, ...)
183 va_start(va, format);
184 fputs("ERROR: ", stderr);
185 vfprintf(stderr, format, va);
190 /* Print formatted error message to stderr. */
191 static void imagex_error_with_errno(const char *format, ...)
193 int errno_save = errno;
195 va_start(va, format);
196 fputs("ERROR: ", stderr);
197 vfprintf(stderr, format, va);
198 fprintf(stderr, ": %s\n", strerror(errno_save));
202 static const char *path_basename(const char *path)
204 const char *p = path;
209 /* Trailing slashes. */
210 while ((p != path - 1) && *p == '/')
213 while ((p != path - 1) && *p != '/')
220 static int verify_image_exists(int image)
222 if (image == WIM_NO_IMAGE) {
223 imagex_error("Not a valid image");
224 return WIMLIB_ERR_INVALID_IMAGE;
229 static int verify_image_is_single(int image)
231 if (image == WIM_ALL_IMAGES) {
232 imagex_error("Cannot specify all images for this action");
233 return WIMLIB_ERR_INVALID_IMAGE;
238 static int verify_image_exists_and_is_single(int image)
241 ret = verify_image_exists(image);
243 ret = verify_image_is_single(image);
247 static int get_compression_type(const char *optarg)
249 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
250 return WIM_COMPRESSION_TYPE_LZX;
251 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
252 return WIM_COMPRESSION_TYPE_XPRESS;
253 else if (strcasecmp(optarg, "none") == 0)
254 return WIM_COMPRESSION_TYPE_NONE;
256 imagex_error("Invalid compression type `%s'! Must be "
257 "\"maximum\", \"fast\", or \"none\".", optarg);
258 return WIM_COMPRESSION_TYPE_INVALID;
262 static char *file_get_contents(const char *filename, size_t *len_ret)
269 if (stat(filename, &stbuf) != 0) {
270 imagex_error_with_errno("Failed to stat the file `%s'", filename);
275 fp = fopen(filename, "rb");
277 imagex_error_with_errno("Failed to open the file `%s'", filename);
283 imagex_error("Failed to allocate buffer of %zu bytes to hold "
284 "contents of file `%s'", len, filename);
287 if (fread(buf, 1, len, fp) != len) {
288 imagex_error_with_errno("Failed to read %lu bytes from the "
289 "file `%s'", len, filename);
301 static int open_swms_from_glob(const char *swm_glob,
302 const char *first_part,
304 WIMStruct ***additional_swms_ret,
305 unsigned *num_additional_swms_ret)
307 unsigned num_additional_swms = 0;
308 WIMStruct **additional_swms = NULL;
312 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
314 if (ret == GLOB_NOMATCH) {
315 imagex_error("Found no files for glob \"%s\"",
318 imagex_error_with_errno("Failed to process glob "
324 num_additional_swms = globbuf.gl_pathc;
325 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
326 if (!additional_swms) {
327 imagex_error("Out of memory");
332 for (unsigned i = 0; i < num_additional_swms; i++) {
333 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
337 ret = wimlib_open_wim(globbuf.gl_pathv[i],
338 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
339 &additional_swms[i - offset]);
343 *additional_swms_ret = additional_swms;
344 *num_additional_swms_ret = num_additional_swms - offset;
348 for (unsigned i = 0; i < num_additional_swms; i++)
349 wimlib_free(additional_swms[i]);
350 free(additional_swms);
358 /* Extract one image, or all images, from a WIM file into a directory. */
359 static int imagex_apply(int argc, const char **argv)
362 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
363 WIMLIB_OPEN_FLAG_SPLIT_OK;
370 const char *image_num_or_name;
371 int extract_flags = 0;
373 const char *swm_glob = NULL;
374 WIMStruct **additional_swms = NULL;
375 unsigned num_additional_swms = 0;
377 for_opt(c, apply_options) {
380 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
383 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
386 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
389 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
401 if (argc != 2 && argc != 3) {
408 image_num_or_name = "1";
411 image_num_or_name = argv[1];
415 ret = wimlib_open_wim(wimfile, open_flags, &w);
419 image = wimlib_resolve_image(w, image_num_or_name);
420 ret = verify_image_exists(image);
424 num_images = wimlib_get_num_images(w);
425 if (argc == 2 && num_images != 1) {
426 imagex_error("`%s' contains %d images; Please select one "
427 "(or all)", wimfile, num_images);
434 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
436 &num_additional_swms);
444 ret = stat(dir, &stbuf);
446 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
447 const char *ntfs_device = dir;
448 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
449 image, wimfile, ntfs_device);
450 ret = wimlib_apply_image_to_ntfs_volume(w, image,
454 num_additional_swms);
458 if (errno != ENOENT) {
459 imagex_error_with_errno("Failed to stat `%s'", dir);
466 ret = wimlib_extract_image(w, image, dir, extract_flags,
467 additional_swms, num_additional_swms);
471 for (unsigned i = 0; i < num_additional_swms; i++)
472 wimlib_free(additional_swms[i]);
476 static int imagex_capture_or_append(int argc, const char **argv)
479 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
480 int add_image_flags = 0;
481 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
482 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
487 const char *flags_element = NULL;
488 const char *config_file = NULL;
489 char *config_str = NULL;
490 size_t config_len = 0;
495 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
497 for_opt(c, capture_or_append_options) {
500 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
503 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
504 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
507 config_file = optarg;
510 compression_type = get_compression_type(optarg);
511 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
515 flags_element = optarg;
518 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
521 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
522 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
531 if (argc < 2 || argc > 4) {
538 char dir_copy[strlen(dir) + 1];
539 memcpy(dir_copy, dir, strlen(dir) + 1);
540 default_name = basename(dir_copy);
542 name = (argc >= 3) ? argv[2] : default_name;
543 desc = (argc >= 4) ? argv[3] : NULL;
546 config_str = file_get_contents(config_file, &config_len);
552 ret = wimlib_open_wim(wimfile, open_flags, &w);
554 ret = wimlib_create_new_wim(compression_type, &w);
561 ret = stat(dir, &stbuf);
563 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
564 const char *ntfs_device = dir;
565 printf("Capturing WIM image NTFS filesystem on `%s'\n",
567 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
575 if (errno != ENOENT) {
576 imagex_error_with_errno("Failed to stat `%s'", dir);
582 ret = wimlib_add_image(w, dir, name, config_str, config_len,
588 cur_image = wimlib_get_num_images(w);
590 ret = wimlib_set_image_descripton(w, cur_image, desc);
595 ret = wimlib_set_image_flags(w, cur_image, flags_element);
600 ret = wimlib_overwrite(w, write_flags);
602 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
604 imagex_error("Failed to write the WIM file `%s'", wimfile);
611 /* Remove image(s) from a WIM. */
612 static int imagex_delete(int argc, const char **argv)
615 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
616 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
618 const char *image_num_or_name;
623 for_opt(c, delete_options) {
626 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
627 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
639 imagex_error("Must specify a WIM file");
641 imagex_error("Must specify an image");
646 image_num_or_name = argv[1];
648 ret = wimlib_open_wim(wimfile, open_flags, &w);
652 image = wimlib_resolve_image(w, image_num_or_name);
654 ret = verify_image_exists(image);
658 ret = wimlib_delete_image(w, image);
660 imagex_error("Failed to delete image from `%s'", wimfile);
664 ret = wimlib_overwrite(w, write_flags);
666 imagex_error("Failed to write the file `%s' with image "
674 /* Print the files contained in an image(s) in a WIM file. */
675 static int imagex_dir(int argc, const char **argv)
684 imagex_error("Must specify a WIM file");
689 imagex_error("Too many arguments");
695 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
700 image = wimlib_resolve_image(w, argv[2]);
701 ret = verify_image_exists(image);
705 /* Image was not specified. If the WIM only contains one image,
706 * choose that one; otherwise, print an error. */
707 num_images = wimlib_get_num_images(w);
708 if (num_images != 1) {
709 imagex_error("The file `%s' contains %d images; Please "
710 "select one.", wimfile, num_images);
718 ret = wimlib_print_files(w, image);
724 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
726 static int imagex_export(int argc, const char **argv)
729 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
730 int export_flags = 0;
731 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
732 int compression_type;
733 bool compression_type_specified = false;
734 const char *src_wimfile;
735 const char *src_image_num_or_name;
736 const char *dest_wimfile;
737 const char *dest_name;
738 const char *dest_desc;
739 WIMStruct *src_w = NULL;
740 WIMStruct *dest_w = NULL;
745 const char *swm_glob = NULL;
746 WIMStruct **additional_swms = NULL;
747 unsigned num_additional_swms = 0;
749 for_opt(c, export_options) {
752 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
755 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
756 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
759 compression_type = get_compression_type(optarg);
760 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
762 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,
784 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
788 /* Determine if the destination is an existing file or not.
789 * If so, we try to append the exported image(s) to it; otherwise, we
790 * create a new WIM containing the exported image(s). */
791 if (stat(dest_wimfile, &stbuf) == 0) {
795 /* Destination file exists. */
796 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
797 imagex_error("`%s' is not a regular file",
802 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
806 dest_ctype = wimlib_get_compression_type(dest_w);
807 if (compression_type_specified
808 && compression_type != dest_ctype)
810 imagex_error("Cannot specify a compression type that is "
811 "not the same as that used in the "
816 compression_type = dest_ctype;
819 /* dest_wimfile is not an existing file, so create a new WIM. */
820 if (!compression_type_specified)
821 compression_type = wimlib_get_compression_type(src_w);
822 if (errno == ENOENT) {
823 ret = wimlib_create_new_wim(compression_type, &dest_w);
827 imagex_error_with_errno("Cannot stat file `%s'",
834 image = wimlib_resolve_image(src_w, src_image_num_or_name);
835 ret = verify_image_exists(image);
840 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
842 &num_additional_swms);
847 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
848 export_flags, additional_swms,
849 num_additional_swms);
855 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
858 ret = wimlib_overwrite(dest_w, write_flags);
863 for (unsigned i = 0; i < num_additional_swms; i++)
864 wimlib_free(additional_swms[i]);
868 /* Prints information about a WIM file; also can mark an image as bootable,
869 * change the name of an image, or change the description of an image. */
870 static int imagex_info(int argc, const char **argv)
876 bool lookup_table = false;
878 bool metadata = false;
879 bool short_header = true;
880 const char *xml_out_file = NULL;
882 const char *image_num_or_name = "all";
883 const char *new_name = NULL;
884 const char *new_desc = NULL;
889 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
890 WIMLIB_OPEN_FLAG_SPLIT_OK;
894 for_opt(c, info_options) {
904 short_header = false;
908 short_header = false;
912 short_header = false;
915 xml_out_file = optarg;
916 short_header = false;
920 short_header = false;
930 if (argc == 0 || argc > 4) {
936 image_num_or_name = argv[1];
946 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
948 ret = wimlib_open_wim(wimfile, open_flags, &w);
952 part_number = wimlib_get_part_number(w, &total_parts);
954 image = wimlib_resolve_image(w, image_num_or_name);
955 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
956 imagex_error("The image `%s' does not exist",
959 imagex_error("If you would like to set the boot "
960 "index to 0, specify image \"0\" with "
962 ret = WIMLIB_ERR_INVALID_IMAGE;
966 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
968 imagex_error("Cannot specify the --boot flag "
969 "without specifying a specific "
970 "image in a multi-image WIM");
971 ret = WIMLIB_ERR_INVALID_IMAGE;
975 imagex_error("Cannot specify the NEW_NAME "
976 "without specifying a specific "
977 "image in a multi-image WIM");
978 ret = WIMLIB_ERR_INVALID_IMAGE;
983 /* Operations that print information are separated from operations that
984 * recreate the WIM file. */
985 if (!new_name && !boot) {
987 /* Read-only operations */
989 if (image == WIM_NO_IMAGE) {
990 imagex_error("`%s' is not a valid image",
992 ret = WIMLIB_ERR_INVALID_IMAGE;
996 if (image == WIM_ALL_IMAGES && short_header)
997 wimlib_print_wim_information(w);
1000 wimlib_print_header(w);
1003 if (total_parts != 1) {
1004 printf("Warning: Only showing the lookup table "
1005 "for part %d of a %d-part WIM.\n",
1006 part_number, total_parts);
1008 wimlib_print_lookup_table(w);
1012 ret = wimlib_extract_xml_data(w, stdout);
1018 fp = fopen(xml_out_file, "wb");
1020 imagex_error_with_errno("Failed to open the "
1026 ret = wimlib_extract_xml_data(w, fp);
1027 if (fclose(fp) != 0) {
1028 imagex_error("Failed to close the file `%s'",
1038 wimlib_print_available_images(w, image);
1041 ret = wimlib_print_metadata(w, image);
1047 /* Modification operations */
1049 if (total_parts != 1) {
1050 imagex_error("Modifying a split WIM is not supported.");
1053 if (image == WIM_ALL_IMAGES)
1056 if (image == WIM_NO_IMAGE && new_name) {
1057 imagex_error("Cannot specify new_name (`%s') when "
1058 "using image 0", new_name);
1063 if (image == wimlib_get_boot_idx(w)) {
1064 printf("Image %d is already marked as "
1065 "bootable.\n", image);
1068 printf("Marking image %d as bootable.\n",
1070 wimlib_set_boot_idx(w, image);
1074 if (strcmp(wimlib_get_image_name(w, image),
1076 printf("Image %d is already named \"%s\".\n",
1080 printf("Changing the name of image %d to "
1081 "\"%s\".\n", image, new_name);
1082 ret = wimlib_set_image_name(w, image, new_name);
1088 const char *old_desc;
1089 old_desc = wimlib_get_image_description(w, image);
1090 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1091 printf("The description of image %d is already "
1092 "\"%s\".\n", image, new_desc);
1095 printf("Changing the description of image %d "
1096 "to \"%s\".\n", image, new_desc);
1097 ret = wimlib_set_image_descripton(w, image,
1104 /* Only call wimlib_overwrite_xml_and_header() if something
1105 * actually needs to be changed. */
1106 if (boot || new_name || new_desc ||
1107 check != wimlib_has_integrity_table(w)) {
1109 ret = wimlib_overwrite_xml_and_header(w, check ?
1110 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1111 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1113 printf("The file `%s' was not modified because nothing "
1114 "needed to be done.\n", wimfile);
1123 /* Join split WIMs into one part WIM */
1124 static int imagex_join(int argc, const char **argv)
1127 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1128 const char *output_path;
1130 for_opt(c, join_options) {
1133 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1143 imagex_error("Must specify at least one split WIM (.swm) parts "
1147 output_path = argv[0];
1148 return wimlib_join(++argv, --argc, output_path, flags);
1154 /* Mounts an image using a FUSE mount. */
1155 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1158 int mount_flags = 0;
1159 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1160 WIMLIB_OPEN_FLAG_SPLIT_OK;
1161 const char *wimfile;
1167 const char *swm_glob = NULL;
1168 WIMStruct **additional_swms = NULL;
1169 unsigned num_additional_swms = 0;
1171 if (strcmp(argv[0], "mountrw") == 0)
1172 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1174 for_opt(c, mount_options) {
1177 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1180 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1183 if (strcasecmp(optarg, "none") == 0)
1184 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1185 else if (strcasecmp(optarg, "xattr") == 0)
1186 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1187 else if (strcasecmp(optarg, "windows") == 0)
1188 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1190 imagex_error("Unknown stream interface \"%s\"", optarg);
1203 if (argc != 2 && argc != 3)
1208 ret = wimlib_open_wim(wimfile, open_flags, &w);
1213 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1215 &num_additional_swms);
1222 num_images = wimlib_get_num_images(w);
1223 if (num_images != 1) {
1224 imagex_error("The file `%s' contains %d images; Please "
1225 "select one", wimfile, num_images);
1226 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1228 ret = WIMLIB_ERR_INVALID_IMAGE;
1233 image = wimlib_resolve_image(w, argv[1]);
1237 ret = verify_image_exists_and_is_single(image);
1241 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1242 num_additional_swms);
1244 imagex_error("Failed to mount image %d from `%s' on `%s'",
1245 image, wimfile, dir);
1250 if (additional_swms)
1251 for (unsigned i = 0; i < num_additional_swms; i++)
1252 wimlib_free(additional_swms[i]);
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;
1268 for_opt(c, split_options) {
1271 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1285 part_size = strtod(argv[2], &tmp) * (1 << 20);
1286 if (tmp == argv[2] || *tmp) {
1287 imagex_error("Invalid part size \"%s\"", argv[2]);
1288 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1291 return wimlib_split(argv[0], argv[1], part_size, flags);
1294 /* Unmounts an image. */
1295 static int imagex_unmount(int argc, const char **argv)
1298 int unmount_flags = 0;
1301 for_opt(c, unmount_options) {
1304 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1307 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1321 ret = wimlib_unmount(argv[0], unmount_flags);
1323 imagex_error("Failed to unmount `%s'", argv[0]);
1327 struct imagex_command {
1329 int (*func)(int , const char **);
1334 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1335 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1337 static const struct imagex_command imagex_commands[] = {
1338 {"append", imagex_capture_or_append, APPEND},
1339 {"apply", imagex_apply, APPLY},
1340 {"capture", imagex_capture_or_append, CAPTURE},
1341 {"delete", imagex_delete, DELETE},
1342 {"dir", imagex_dir, DIR},
1343 {"export", imagex_export, EXPORT},
1344 {"info", imagex_info, INFO},
1345 {"join", imagex_join, JOIN},
1346 {"mount", imagex_mount_rw_or_ro, MOUNT},
1347 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1348 {"split", imagex_split, SPLIT},
1349 {"unmount", imagex_unmount, UNMOUNT},
1352 static void version()
1354 static const char *s =
1355 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1356 "Copyright (C) 2012 Eric Biggers\n"
1357 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1358 "This is free software: you are free to change and redistribute it.\n"
1359 "There is NO WARRANTY, to the extent permitted by law.\n"
1361 "Report bugs to "PACKAGE_BUGREPORT".\n";
1366 static void help_or_version(int argc, const char **argv)
1370 const struct imagex_command *cmd;
1372 for (i = 1; i < argc; i++) {
1380 if (strcmp(p, "help") == 0) {
1381 for_imagex_command(cmd) {
1382 if (strcmp(cmd->name, argv[1]) == 0) {
1390 if (strcmp(p, "version") == 0) {
1398 static void usage(int cmd_type)
1400 const struct imagex_command *cmd;
1401 puts("IMAGEX: Usage:");
1402 fputs(usage_strings[cmd_type], stdout);
1403 for_imagex_command(cmd)
1404 if (cmd->cmd == cmd_type)
1405 printf("\nTry `man imagex-%s' for more details.\n",
1409 static void usage_all()
1411 puts("IMAGEX: Usage:");
1412 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1413 fputs(usage_strings[i], stdout);
1414 static const char *extra =
1416 " imagex --version\n"
1418 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1420 " Try `man imagex' for more information.\n"
1422 fputs(extra, stdout);
1426 int main(int argc, const char **argv)
1428 const struct imagex_command *cmd;
1432 imagex_error("No command specified");
1437 help_or_version(argc, argv);
1441 wimlib_set_print_errors(true);
1443 for_imagex_command(cmd) {
1444 if (strcmp(cmd->name, *argv) == 0) {
1445 ret = cmd->func(argc, argv);
1447 imagex_error("Exiting with error code %d:\n"
1449 wimlib_get_error_string(ret));
1450 if (ret == WIMLIB_ERR_NTFS_3G)
1451 imagex_error_with_errno("errno");
1457 imagex_error("Unrecognized command: `%s'", argv[0]);