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 int verify_image_exists(int image)
204 if (image == WIM_NO_IMAGE) {
205 imagex_error("Not a valid image");
206 return WIMLIB_ERR_INVALID_IMAGE;
211 static int verify_image_is_single(int image)
213 if (image == WIM_ALL_IMAGES) {
214 imagex_error("Cannot specify all images for this action");
215 return WIMLIB_ERR_INVALID_IMAGE;
220 static int verify_image_exists_and_is_single(int image)
223 ret = verify_image_exists(image);
225 ret = verify_image_is_single(image);
229 static int get_compression_type(const char *optarg)
231 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
232 return WIM_COMPRESSION_TYPE_LZX;
233 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
234 return WIM_COMPRESSION_TYPE_XPRESS;
235 else if (strcasecmp(optarg, "none") == 0)
236 return WIM_COMPRESSION_TYPE_NONE;
238 imagex_error("Invalid compression type `%s'! Must be "
239 "\"maximum\", \"fast\", or \"none\".", optarg);
240 return WIM_COMPRESSION_TYPE_INVALID;
244 static char *file_get_contents(const char *filename, size_t *len_ret)
251 if (stat(filename, &stbuf) != 0) {
252 imagex_error_with_errno("Failed to stat the file `%s'", filename);
257 fp = fopen(filename, "rb");
259 imagex_error_with_errno("Failed to open the file `%s'", filename);
265 imagex_error("Failed to allocate buffer of %zu bytes to hold "
266 "contents of file `%s'", len, filename);
269 if (fread(buf, 1, len, fp) != len) {
270 imagex_error_with_errno("Failed to read %lu bytes from the "
271 "file `%s'", len, filename);
283 static int open_swms_from_glob(const char *swm_glob,
284 const char *first_part,
286 WIMStruct ***additional_swms_ret,
287 unsigned *num_additional_swms_ret)
289 unsigned num_additional_swms = 0;
290 WIMStruct **additional_swms = NULL;
294 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
296 if (ret == GLOB_NOMATCH) {
297 imagex_error("Found no files for glob \"%s\"",
300 imagex_error_with_errno("Failed to process glob "
306 num_additional_swms = globbuf.gl_pathc;
307 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
308 if (!additional_swms) {
309 imagex_error("Out of memory");
314 for (unsigned i = 0; i < num_additional_swms; i++) {
315 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
319 ret = wimlib_open_wim(globbuf.gl_pathv[i],
320 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
321 &additional_swms[i - offset]);
325 *additional_swms_ret = additional_swms;
326 *num_additional_swms_ret = num_additional_swms - offset;
330 for (unsigned i = 0; i < num_additional_swms; i++)
331 wimlib_free(additional_swms[i]);
332 free(additional_swms);
340 /* Extract one image, or all images, from a WIM file into a directory. */
341 static int imagex_apply(int argc, const char **argv)
344 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
345 WIMLIB_OPEN_FLAG_SPLIT_OK;
352 const char *image_num_or_name;
353 int extract_flags = 0;
355 const char *swm_glob = NULL;
356 WIMStruct **additional_swms = NULL;
357 unsigned num_additional_swms = 0;
359 for_opt(c, apply_options) {
362 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
365 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
368 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
371 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
383 if (argc != 2 && argc != 3) {
390 image_num_or_name = "1";
393 image_num_or_name = argv[1];
397 ret = wimlib_open_wim(wimfile, open_flags, &w);
401 image = wimlib_resolve_image(w, image_num_or_name);
402 ret = verify_image_exists(image);
406 num_images = wimlib_get_num_images(w);
407 if (argc == 2 && num_images != 1) {
408 imagex_error("`%s' contains %d images; Please select one "
409 "(or all)", wimfile, num_images);
416 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
418 &num_additional_swms);
426 ret = stat(dir, &stbuf);
428 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
429 const char *ntfs_device = dir;
430 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
431 image, wimfile, ntfs_device);
432 ret = wimlib_apply_image_to_ntfs_volume(w, image,
436 num_additional_swms);
440 if (errno != ENOENT) {
441 imagex_error_with_errno("Failed to stat `%s'", dir);
448 ret = wimlib_extract_image(w, image, dir, extract_flags,
449 additional_swms, num_additional_swms);
452 if (additional_swms) {
453 for (unsigned i = 0; i < num_additional_swms; i++)
454 wimlib_free(additional_swms[i]);
455 free(additional_swms);
460 static int imagex_capture_or_append(int argc, const char **argv)
463 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
464 int add_image_flags = 0;
465 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
466 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
471 const char *flags_element = NULL;
472 const char *config_file = NULL;
473 char *config_str = NULL;
474 size_t config_len = 0;
479 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
481 for_opt(c, capture_or_append_options) {
484 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
487 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
488 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
491 config_file = optarg;
494 compression_type = get_compression_type(optarg);
495 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
499 flags_element = optarg;
502 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
505 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
506 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
515 if (argc < 2 || argc > 4) {
522 char dir_copy[strlen(dir) + 1];
523 memcpy(dir_copy, dir, strlen(dir) + 1);
524 default_name = basename(dir_copy);
526 name = (argc >= 3) ? argv[2] : default_name;
527 desc = (argc >= 4) ? argv[3] : NULL;
530 config_str = file_get_contents(config_file, &config_len);
536 ret = wimlib_open_wim(wimfile, open_flags, &w);
538 ret = wimlib_create_new_wim(compression_type, &w);
545 ret = stat(dir, &stbuf);
547 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
548 const char *ntfs_device = dir;
549 printf("Capturing WIM image NTFS filesystem on `%s'\n",
551 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
559 if (errno != ENOENT) {
560 imagex_error_with_errno("Failed to stat `%s'", dir);
566 ret = wimlib_add_image(w, dir, name, config_str, config_len,
572 cur_image = wimlib_get_num_images(w);
574 ret = wimlib_set_image_descripton(w, cur_image, desc);
579 ret = wimlib_set_image_flags(w, cur_image, flags_element);
584 ret = wimlib_overwrite(w, write_flags);
586 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
588 imagex_error("Failed to write the WIM file `%s'", wimfile);
595 /* Remove image(s) from a WIM. */
596 static int imagex_delete(int argc, const char **argv)
599 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
600 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
602 const char *image_num_or_name;
607 for_opt(c, delete_options) {
610 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
611 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
623 imagex_error("Must specify a WIM file");
625 imagex_error("Must specify an image");
630 image_num_or_name = argv[1];
632 ret = wimlib_open_wim(wimfile, open_flags, &w);
636 image = wimlib_resolve_image(w, image_num_or_name);
638 ret = verify_image_exists(image);
642 ret = wimlib_delete_image(w, image);
644 imagex_error("Failed to delete image from `%s'", wimfile);
648 ret = wimlib_overwrite(w, write_flags);
650 imagex_error("Failed to write the file `%s' with image "
658 /* Print the files contained in an image(s) in a WIM file. */
659 static int imagex_dir(int argc, const char **argv)
668 imagex_error("Must specify a WIM file");
673 imagex_error("Too many arguments");
679 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
684 image = wimlib_resolve_image(w, argv[2]);
685 ret = verify_image_exists(image);
689 /* Image was not specified. If the WIM only contains one image,
690 * choose that one; otherwise, print an error. */
691 num_images = wimlib_get_num_images(w);
692 if (num_images != 1) {
693 imagex_error("The file `%s' contains %d images; Please "
694 "select one.", wimfile, num_images);
702 ret = wimlib_print_files(w, image);
708 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
710 static int imagex_export(int argc, const char **argv)
713 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
714 int export_flags = 0;
715 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
716 int compression_type;
717 bool compression_type_specified = false;
718 const char *src_wimfile;
719 const char *src_image_num_or_name;
720 const char *dest_wimfile;
721 const char *dest_name;
722 const char *dest_desc;
723 WIMStruct *src_w = NULL;
724 WIMStruct *dest_w = NULL;
729 const char *swm_glob = NULL;
730 WIMStruct **additional_swms = NULL;
731 unsigned num_additional_swms = 0;
733 for_opt(c, export_options) {
736 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
739 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
740 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
743 compression_type = get_compression_type(optarg);
744 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
746 compression_type_specified = true;
758 if (argc < 3 || argc > 5) {
762 src_wimfile = argv[0];
763 src_image_num_or_name = argv[1];
764 dest_wimfile = argv[2];
765 dest_name = (argc >= 4) ? argv[3] : NULL;
766 dest_desc = (argc >= 5) ? argv[4] : NULL;
767 ret = wimlib_open_wim(src_wimfile,
768 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
772 /* Determine if the destination is an existing file or not.
773 * If so, we try to append the exported image(s) to it; otherwise, we
774 * create a new WIM containing the exported image(s). */
775 if (stat(dest_wimfile, &stbuf) == 0) {
779 /* Destination file exists. */
780 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
781 imagex_error("`%s' is not a regular file",
786 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
790 dest_ctype = wimlib_get_compression_type(dest_w);
791 if (compression_type_specified
792 && compression_type != dest_ctype)
794 imagex_error("Cannot specify a compression type that is "
795 "not the same as that used in the "
800 compression_type = dest_ctype;
803 /* dest_wimfile is not an existing file, so create a new WIM. */
804 if (!compression_type_specified)
805 compression_type = wimlib_get_compression_type(src_w);
806 if (errno == ENOENT) {
807 ret = wimlib_create_new_wim(compression_type, &dest_w);
811 imagex_error_with_errno("Cannot stat file `%s'",
818 image = wimlib_resolve_image(src_w, src_image_num_or_name);
819 ret = verify_image_exists(image);
824 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
826 &num_additional_swms);
831 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
832 export_flags, additional_swms,
833 num_additional_swms);
839 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
842 ret = wimlib_overwrite(dest_w, write_flags);
846 if (additional_swms) {
847 for (unsigned i = 0; i < num_additional_swms; i++)
848 wimlib_free(additional_swms[i]);
849 free(additional_swms);
854 /* Prints information about a WIM file; also can mark an image as bootable,
855 * change the name of an image, or change the description of an image. */
856 static int imagex_info(int argc, const char **argv)
862 bool lookup_table = false;
864 bool metadata = false;
865 bool short_header = true;
866 const char *xml_out_file = NULL;
868 const char *image_num_or_name = "all";
869 const char *new_name = NULL;
870 const char *new_desc = NULL;
875 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
876 WIMLIB_OPEN_FLAG_SPLIT_OK;
880 for_opt(c, info_options) {
890 short_header = false;
894 short_header = false;
898 short_header = false;
901 xml_out_file = optarg;
902 short_header = false;
906 short_header = false;
916 if (argc == 0 || argc > 4) {
922 image_num_or_name = argv[1];
932 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
934 ret = wimlib_open_wim(wimfile, open_flags, &w);
938 part_number = wimlib_get_part_number(w, &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 /* Read-only operations */
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 ret = wimlib_print_metadata(w, image);
1033 /* Modification operations */
1035 if (total_parts != 1) {
1036 imagex_error("Modifying a split WIM is not supported.");
1039 if (image == WIM_ALL_IMAGES)
1042 if (image == WIM_NO_IMAGE && new_name) {
1043 imagex_error("Cannot specify new_name (`%s') when "
1044 "using image 0", new_name);
1049 if (image == wimlib_get_boot_idx(w)) {
1050 printf("Image %d is already marked as "
1051 "bootable.\n", image);
1054 printf("Marking image %d as bootable.\n",
1056 wimlib_set_boot_idx(w, image);
1060 if (strcmp(wimlib_get_image_name(w, image),
1062 printf("Image %d is already named \"%s\".\n",
1066 printf("Changing the name of image %d to "
1067 "\"%s\".\n", image, new_name);
1068 ret = wimlib_set_image_name(w, image, new_name);
1074 const char *old_desc;
1075 old_desc = wimlib_get_image_description(w, image);
1076 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1077 printf("The description of image %d is already "
1078 "\"%s\".\n", image, new_desc);
1081 printf("Changing the description of image %d "
1082 "to \"%s\".\n", image, new_desc);
1083 ret = wimlib_set_image_descripton(w, image,
1090 /* Only call wimlib_overwrite_xml_and_header() if something
1091 * actually needs to be changed. */
1092 if (boot || new_name || new_desc ||
1093 check != wimlib_has_integrity_table(w)) {
1095 ret = wimlib_overwrite_xml_and_header(w, check ?
1096 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1097 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1099 printf("The file `%s' was not modified because nothing "
1100 "needed to be done.\n", wimfile);
1109 /* Join split WIMs into one part WIM */
1110 static int imagex_join(int argc, const char **argv)
1113 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1114 const char *output_path;
1116 for_opt(c, join_options) {
1119 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1129 imagex_error("Must specify at least one split WIM (.swm) parts "
1133 output_path = argv[0];
1134 return wimlib_join(++argv, --argc, output_path, flags);
1140 /* Mounts an image using a FUSE mount. */
1141 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1144 int mount_flags = 0;
1145 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1146 WIMLIB_OPEN_FLAG_SPLIT_OK;
1147 const char *wimfile;
1153 const char *swm_glob = NULL;
1154 WIMStruct **additional_swms = NULL;
1155 unsigned num_additional_swms = 0;
1157 if (strcmp(argv[0], "mountrw") == 0)
1158 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1160 for_opt(c, mount_options) {
1163 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1166 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1169 if (strcasecmp(optarg, "none") == 0)
1170 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1171 else if (strcasecmp(optarg, "xattr") == 0)
1172 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1173 else if (strcasecmp(optarg, "windows") == 0)
1174 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1176 imagex_error("Unknown stream interface \"%s\"", optarg);
1189 if (argc != 2 && argc != 3)
1194 ret = wimlib_open_wim(wimfile, open_flags, &w);
1199 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1201 &num_additional_swms);
1208 num_images = wimlib_get_num_images(w);
1209 if (num_images != 1) {
1210 imagex_error("The file `%s' contains %d images; Please "
1211 "select one", wimfile, num_images);
1212 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1214 ret = WIMLIB_ERR_INVALID_IMAGE;
1219 image = wimlib_resolve_image(w, argv[1]);
1223 ret = verify_image_exists_and_is_single(image);
1227 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1228 num_additional_swms);
1230 imagex_error("Failed to mount image %d from `%s' on `%s'",
1231 image, wimfile, dir);
1236 if (additional_swms)
1237 for (unsigned i = 0; i < num_additional_swms; i++)
1238 wimlib_free(additional_swms[i]);
1241 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1246 /* Split a WIM into a spanned set */
1247 static int imagex_split(int argc, const char **argv)
1250 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1251 unsigned long part_size;
1254 for_opt(c, split_options) {
1257 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1271 part_size = strtod(argv[2], &tmp) * (1 << 20);
1272 if (tmp == argv[2] || *tmp) {
1273 imagex_error("Invalid part size \"%s\"", argv[2]);
1274 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1277 return wimlib_split(argv[0], argv[1], part_size, flags);
1280 /* Unmounts an image. */
1281 static int imagex_unmount(int argc, const char **argv)
1284 int unmount_flags = 0;
1287 for_opt(c, unmount_options) {
1290 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1293 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1307 ret = wimlib_unmount(argv[0], unmount_flags);
1309 imagex_error("Failed to unmount `%s'", argv[0]);
1313 struct imagex_command {
1315 int (*func)(int , const char **);
1320 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1321 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1323 static const struct imagex_command imagex_commands[] = {
1324 {"append", imagex_capture_or_append, APPEND},
1325 {"apply", imagex_apply, APPLY},
1326 {"capture", imagex_capture_or_append, CAPTURE},
1327 {"delete", imagex_delete, DELETE},
1328 {"dir", imagex_dir, DIR},
1329 {"export", imagex_export, EXPORT},
1330 {"info", imagex_info, INFO},
1331 {"join", imagex_join, JOIN},
1332 {"mount", imagex_mount_rw_or_ro, MOUNT},
1333 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1334 {"split", imagex_split, SPLIT},
1335 {"unmount", imagex_unmount, UNMOUNT},
1338 static void version()
1340 static const char *s =
1341 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1342 "Copyright (C) 2012 Eric Biggers\n"
1343 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1344 "This is free software: you are free to change and redistribute it.\n"
1345 "There is NO WARRANTY, to the extent permitted by law.\n"
1347 "Report bugs to "PACKAGE_BUGREPORT".\n";
1352 static void help_or_version(int argc, const char **argv)
1356 const struct imagex_command *cmd;
1358 for (i = 1; i < argc; i++) {
1366 if (strcmp(p, "help") == 0) {
1367 for_imagex_command(cmd) {
1368 if (strcmp(cmd->name, argv[1]) == 0) {
1376 if (strcmp(p, "version") == 0) {
1384 static void usage(int cmd_type)
1386 const struct imagex_command *cmd;
1387 puts("IMAGEX: Usage:");
1388 fputs(usage_strings[cmd_type], stdout);
1389 for_imagex_command(cmd)
1390 if (cmd->cmd == cmd_type)
1391 printf("\nTry `man imagex-%s' for more details.\n",
1395 static void usage_all()
1397 puts("IMAGEX: Usage:");
1398 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1399 fputs(usage_strings[i], stdout);
1400 static const char *extra =
1402 " imagex --version\n"
1404 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1406 " Try `man imagex' for more information.\n"
1408 fputs(extra, stdout);
1412 int main(int argc, const char **argv)
1414 const struct imagex_command *cmd;
1418 imagex_error("No command specified");
1423 help_or_version(argc, argv);
1427 wimlib_set_print_errors(true);
1429 for_imagex_command(cmd) {
1430 if (strcmp(cmd->name, *argv) == 0) {
1431 ret = cmd->func(argc, argv);
1433 imagex_error("Exiting with error code %d:\n"
1435 wimlib_get_error_string(ret));
1436 if (ret == WIMLIB_ERR_NTFS_3G)
1437 imagex_error_with_errno("errno");
1443 imagex_error("Unrecognized command: `%s'", argv[0]);