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 (size_t 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);
459 imagex_error_with_errno("Failed to stat `%s'", dir);
463 ret = wimlib_extract_image(w, image, dir, extract_flags,
464 additional_swms, num_additional_swms);
468 for (size_t i = 0; i < num_additional_swms; i++)
469 wimlib_free(additional_swms[i]);
473 static int imagex_capture_or_append(int argc, const char **argv)
476 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
477 int add_image_flags = 0;
478 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
479 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
484 const char *flags_element = NULL;
485 const char *config_file = NULL;
486 char *config_str = NULL;
487 size_t config_len = 0;
492 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
494 for_opt(c, capture_or_append_options) {
497 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
500 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
501 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
504 config_file = optarg;
507 compression_type = get_compression_type(optarg);
508 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
512 flags_element = optarg;
515 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
518 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
519 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
528 if (argc < 2 || argc > 4) {
535 char dir_copy[strlen(dir) + 1];
536 memcpy(dir_copy, dir, strlen(dir) + 1);
537 default_name = basename(dir_copy);
539 name = (argc >= 3) ? argv[2] : default_name;
540 desc = (argc >= 4) ? argv[3] : NULL;
543 config_str = file_get_contents(config_file, &config_len);
549 ret = wimlib_open_wim(wimfile, open_flags, &w);
551 ret = wimlib_create_new_wim(compression_type, &w);
558 ret = stat(dir, &stbuf);
560 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
561 const char *ntfs_device = dir;
562 printf("Capturing WIM image NTFS filesystem on `%s'\n",
564 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
572 if (errno != ENOENT) {
573 imagex_error_with_errno("Failed to stat `%s'", dir);
579 ret = wimlib_add_image(w, dir, name, config_str, config_len,
585 cur_image = wimlib_get_num_images(w);
587 ret = wimlib_set_image_descripton(w, cur_image, desc);
592 ret = wimlib_set_image_flags(w, cur_image, flags_element);
597 ret = wimlib_overwrite(w, write_flags);
599 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
601 imagex_error("Failed to write the WIM file `%s'", wimfile);
608 /* Remove image(s) from a WIM. */
609 static int imagex_delete(int argc, const char **argv)
612 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
613 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
615 const char *image_num_or_name;
620 for_opt(c, delete_options) {
623 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
624 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
636 imagex_error("Must specify a WIM file");
638 imagex_error("Must specify an image");
643 image_num_or_name = argv[1];
645 ret = wimlib_open_wim(wimfile, open_flags, &w);
649 image = wimlib_resolve_image(w, image_num_or_name);
651 ret = verify_image_exists(image);
655 ret = wimlib_delete_image(w, image);
657 imagex_error("Failed to delete image from `%s'", wimfile);
661 ret = wimlib_overwrite(w, write_flags);
663 imagex_error("Failed to write the file `%s' with image "
671 /* Print the files contained in an image(s) in a WIM file. */
672 static int imagex_dir(int argc, const char **argv)
682 imagex_error("Must specify a WIM file");
687 imagex_error("Too many arguments");
693 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
697 part_number = wimlib_get_part_number(w, NULL);
698 if (part_number != 1) {
699 imagex_error("`%s' is part %d of a split WIM! Specify the "
700 "first part to see the files",
701 wimfile, part_number);
702 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
707 image = wimlib_resolve_image(w, argv[2]);
708 ret = verify_image_exists(image);
712 /* Image was not specified. If the WIM only contains one image,
713 * choose that one; otherwise, print an error. */
714 num_images = wimlib_get_num_images(w);
715 if (num_images != 1) {
716 imagex_error("The file `%s' contains %d images; Please "
717 "select one.", wimfile, num_images);
725 ret = wimlib_print_files(w, image);
731 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
733 static int imagex_export(int argc, const char **argv)
736 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
737 int export_flags = 0;
738 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
739 int compression_type;
740 bool compression_type_specified = false;
741 const char *src_wimfile;
742 const char *src_image_num_or_name;
743 const char *dest_wimfile;
744 const char *dest_name;
745 const char *dest_desc;
746 WIMStruct *src_w = NULL;
747 WIMStruct *dest_w = NULL;
752 const char *swm_glob = NULL;
753 WIMStruct **additional_swms = NULL;
754 unsigned num_additional_swms = 0;
756 for_opt(c, export_options) {
759 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
762 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
763 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
766 compression_type = get_compression_type(optarg);
767 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
769 compression_type_specified = true;
781 if (argc < 3 || argc > 5) {
785 src_wimfile = argv[0];
786 src_image_num_or_name = argv[1];
787 dest_wimfile = argv[2];
788 dest_name = (argc >= 4) ? argv[3] : NULL;
789 dest_desc = (argc >= 5) ? argv[4] : NULL;
790 ret = wimlib_open_wim(src_wimfile,
791 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
795 /* Determine if the destination is an existing file or not.
796 * If so, we try to append the exported image(s) to it; otherwise, we
797 * create a new WIM containing the exported image(s). */
798 if (stat(dest_wimfile, &stbuf) == 0) {
800 /* Destination file exists. */
801 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
802 imagex_error("`%s' is not a regular file",
806 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
810 if (compression_type_specified && compression_type !=
811 wimlib_get_compression_type(dest_w)) {
812 imagex_error("Cannot specify a compression type that is "
813 "not the same as that used in the "
818 compression_type = wimlib_get_compression_type(dest_w);
821 /* dest_wimfile is not an existing file, so create a new WIM. */
822 if (!compression_type_specified)
823 compression_type = wimlib_get_compression_type(src_w);
824 if (errno == ENOENT) {
825 ret = wimlib_create_new_wim(compression_type, &dest_w);
829 imagex_error_with_errno("Cannot stat file `%s'",
835 image = wimlib_resolve_image(src_w, src_image_num_or_name);
836 ret = verify_image_exists(image);
841 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
843 &num_additional_swms);
848 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
849 export_flags, additional_swms,
850 num_additional_swms);
856 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
859 ret = wimlib_overwrite(dest_w, write_flags);
864 for (unsigned i = 0; i < num_additional_swms; i++)
865 wimlib_free(additional_swms[i]);
869 /* Prints information about a WIM file; also can mark an image as bootable,
870 * change the name of an image, or change the description of an image. */
871 static int imagex_info(int argc, const char **argv)
877 bool lookup_table = false;
879 bool metadata = false;
880 bool short_header = true;
881 const char *xml_out_file = NULL;
883 const char *image_num_or_name = "all";
884 const char *new_name = NULL;
885 const char *new_desc = NULL;
890 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
891 WIMLIB_OPEN_FLAG_SPLIT_OK;
895 for_opt(c, info_options) {
905 short_header = false;
909 short_header = false;
913 short_header = false;
916 xml_out_file = optarg;
917 short_header = false;
921 short_header = false;
931 if (argc == 0 || argc > 4) {
937 image_num_or_name = argv[1];
947 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
949 ret = wimlib_open_wim(wimfile, open_flags, &w);
953 part_number = wimlib_get_part_number(w, &total_parts);
955 /*if (total_parts > 1 && part_number > 1) {*/
956 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
957 /*" Select the first part if you want to see information\n"*/
958 /*" about images in the WIM.\n", */
959 /*part_number, total_parts);*/
962 image = wimlib_resolve_image(w, image_num_or_name);
963 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
964 imagex_error("The image `%s' does not exist",
967 imagex_error("If you would like to set the boot "
968 "index to 0, specify image \"0\" with "
970 ret = WIMLIB_ERR_INVALID_IMAGE;
974 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
976 imagex_error("Cannot specify the --boot flag "
977 "without specifying a specific "
978 "image in a multi-image WIM");
979 ret = WIMLIB_ERR_INVALID_IMAGE;
983 imagex_error("Cannot specify the NEW_NAME "
984 "without specifying a specific "
985 "image in a multi-image WIM");
986 ret = WIMLIB_ERR_INVALID_IMAGE;
991 /* Operations that print information are separated from operations that
992 * recreate the WIM file. */
993 if (!new_name && !boot) {
995 if (image == WIM_NO_IMAGE) {
996 imagex_error("`%s' is not a valid image",
998 ret = WIMLIB_ERR_INVALID_IMAGE;
1002 if (image == WIM_ALL_IMAGES && short_header)
1003 wimlib_print_wim_information(w);
1006 wimlib_print_header(w);
1009 if (total_parts != 1) {
1010 printf("Warning: Only showing the lookup table "
1011 "for part %d of a %d-part WIM.\n",
1012 part_number, total_parts);
1014 wimlib_print_lookup_table(w);
1018 ret = wimlib_extract_xml_data(w, stdout);
1024 fp = fopen(xml_out_file, "wb");
1026 imagex_error_with_errno("Failed to open the "
1032 ret = wimlib_extract_xml_data(w, fp);
1033 if (fclose(fp) != 0) {
1034 imagex_error("Failed to close the file `%s'",
1044 wimlib_print_available_images(w, image);
1047 ret = wimlib_print_metadata(w, image);
1052 if (total_parts != 1) {
1053 imagex_error("Modifying a split WIM is not supported.");
1056 if (image == WIM_ALL_IMAGES)
1059 if (image == WIM_NO_IMAGE && new_name) {
1060 imagex_error("Cannot specify new_name (`%s') when "
1061 "using image 0", new_name);
1066 if (image == wimlib_get_boot_idx(w)) {
1067 printf("Image %d is already marked as "
1068 "bootable.\n", image);
1071 printf("Marking image %d as bootable.\n",
1073 wimlib_set_boot_idx(w, image);
1077 if (strcmp(wimlib_get_image_name(w, image),
1079 printf("Image %d is already named \"%s\".\n",
1083 printf("Changing the name of image %d to "
1084 "\"%s\".\n", image, new_name);
1085 ret = wimlib_set_image_name(w, image, new_name);
1091 const char *old_desc;
1092 old_desc = wimlib_get_image_description(w, image);
1093 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1094 printf("The description of image %d is already "
1095 "\"%s\".\n", image, new_desc);
1098 printf("Changing the description of image %d "
1099 "to \"%s\".\n", image, new_desc);
1100 ret = wimlib_set_image_descripton(w, image,
1107 /* Only call wimlib_overwrite_xml_and_header() if something
1108 * actually needs to be changed. */
1109 if (boot || new_name || new_desc ||
1110 check != wimlib_has_integrity_table(w)) {
1112 ret = wimlib_overwrite_xml_and_header(w, check ?
1113 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1114 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1116 printf("The file `%s' was not modified because nothing "
1117 "needed to be done.\n", wimfile);
1127 /* Join split WIMs into one part WIM */
1128 static int imagex_join(int argc, const char **argv)
1131 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1132 const char *output_path;
1134 for_opt(c, join_options) {
1137 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1147 imagex_error("Must specify at least one split WIM (.swm) parts "
1151 output_path = argv[0];
1152 return wimlib_join(++argv, --argc, output_path, flags);
1158 /* Mounts an image using a FUSE mount. */
1159 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1162 int mount_flags = 0;
1163 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1164 WIMLIB_OPEN_FLAG_SPLIT_OK;
1165 const char *wimfile;
1171 const char *swm_glob = NULL;
1172 WIMStruct **additional_swms = NULL;
1173 unsigned num_additional_swms = 0;
1175 if (strcmp(argv[0], "mountrw") == 0)
1176 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1177 for_opt(c, mount_options) {
1180 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1183 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1186 if (strcasecmp(optarg, "none") == 0)
1187 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1188 else if (strcasecmp(optarg, "xattr") == 0)
1189 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1190 else if (strcasecmp(optarg, "windows") == 0)
1191 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1193 imagex_error("Unknown stream interface \"%s\"", optarg);
1206 if (argc != 2 && argc != 3)
1211 ret = wimlib_open_wim(wimfile, open_flags, &w);
1216 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1218 &num_additional_swms);
1225 num_images = wimlib_get_num_images(w);
1226 if (num_images != 1) {
1227 imagex_error("The file `%s' contains %d images; Please "
1228 "select one", wimfile, num_images);
1229 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1231 ret = WIMLIB_ERR_INVALID_IMAGE;
1236 image = wimlib_resolve_image(w, argv[1]);
1240 ret = verify_image_exists_and_is_single(image);
1244 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1245 num_additional_swms);
1247 imagex_error("Failed to mount image %d from `%s' on `%s'",
1248 image, wimfile, dir);
1253 if (additional_swms)
1254 for (unsigned i = 0; i < num_additional_swms; i++)
1255 wimlib_free(additional_swms[i]);
1258 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1263 /* Split a WIM into a spanned set */
1264 static int imagex_split(int argc, const char **argv)
1267 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1268 unsigned long part_size;
1270 for_opt(c, split_options) {
1273 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1287 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1288 return wimlib_split(argv[0], argv[1], part_size, flags);
1291 /* Unmounts an image. */
1292 static int imagex_unmount(int argc, const char **argv)
1295 int unmount_flags = 0;
1298 for_opt(c, unmount_options) {
1301 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1304 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1318 ret = wimlib_unmount(argv[0], unmount_flags);
1320 imagex_error("Failed to unmount `%s'", argv[0]);
1324 struct imagex_command {
1326 int (*func)(int , const char **);
1331 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1332 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1334 static struct imagex_command imagex_commands[] = {
1335 {"append", imagex_capture_or_append, APPEND},
1336 {"apply", imagex_apply, APPLY},
1337 {"capture", imagex_capture_or_append, CAPTURE},
1338 {"delete", imagex_delete, DELETE},
1339 {"dir", imagex_dir, DIR},
1340 {"export", imagex_export, EXPORT},
1341 {"info", imagex_info, INFO},
1342 {"join", imagex_join, JOIN},
1343 {"mount", imagex_mount_rw_or_ro, MOUNT},
1344 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1345 {"split", imagex_split, SPLIT},
1346 {"unmount", imagex_unmount, UNMOUNT},
1349 static void version()
1351 static const char *s =
1352 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1353 "Copyright (C) 2012 Eric Biggers\n"
1354 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1355 "This is free software: you are free to change and redistribute it.\n"
1356 "There is NO WARRANTY, to the extent permitted by law.\n"
1358 "Report bugs to "PACKAGE_BUGREPORT".\n";
1363 static void help_or_version(int argc, const char **argv)
1367 struct imagex_command *cmd;
1369 for (i = 1; i < argc; i++) {
1377 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1378 for_imagex_command(cmd) {
1379 if (strcmp(cmd->name, argv[1]) == 0) {
1387 if (strcmp(p, "version") == 0) {
1395 static void usage(int cmd_type)
1397 struct imagex_command *cmd;
1398 puts("IMAGEX: Usage:");
1399 fputs(usage_strings[cmd_type], stdout);
1400 for_imagex_command(cmd)
1401 if (cmd->cmd == cmd_type)
1402 printf("\nTry `man imagex-%s' for more details.\n",
1406 static void usage_all()
1408 puts("IMAGEX: Usage:");
1409 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1410 fputs(usage_strings[i], stdout);
1411 static const char *extra =
1413 " imagex --version\n"
1415 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1417 " Try `man imagex' for more information.\n"
1419 fputs(extra, stdout);
1423 int main(int argc, const char **argv)
1425 struct imagex_command *cmd;
1429 imagex_error("No command specified");
1434 help_or_version(argc, argv);
1438 wimlib_set_print_errors(true);
1440 for_imagex_command(cmd) {
1441 if (strcmp(cmd->name, *argv) == 0) {
1442 ret = cmd->func(argc, argv);
1444 imagex_error("Exiting with error code %d:\n"
1446 wimlib_get_error_string(ret));
1447 if (ret == WIMLIB_ERR_NTFS_3G)
1448 imagex_error_with_errno("errno");
1454 imagex_error("Unrecognized command: `%s'", argv[0]);