]> wimlib.net Git - wimlib/blob - programs/imagex.c
688b1e172dbdef2266f66d68dfe68bed785d1d03
[wimlib] / programs / imagex.c
1 /*
2  * imagex.c
3  *
4  * Use wimlib to create, modify, extract, mount, unmount, or display information
5  * about a WIM file
6  */
7
8 /*
9  * Copyright (C) 2012 Eric Biggers
10  *
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.
15  *
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.
20  *
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/>.
23  */
24
25 #include "wimlib.h"
26 #include "config.h"
27 #include <getopt.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <glob.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <libgen.h>
34 #include <sys/stat.h>
35
36 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
37
38 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
39                                 a = __b; b = __a; })
40
41 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
42                                 opts, NULL)) != -1)
43
44
45 enum imagex_op_type {
46         APPEND,
47         APPLY,
48         CAPTURE,
49         DELETE,
50         DIR,
51         EXPORT,
52         INFO,
53         JOIN,
54         MOUNT,
55         MOUNTRW,
56         SPLIT,
57         UNMOUNT,
58 };
59
60 static void usage(int cmd_type);
61 static void usage_all();
62
63 static const char *usage_strings[] = {
64 [APPEND] =
65 "    imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
66 "                  [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
67 "                  [--verbose] [--dereference] [--config=FILE]\n",
68 [APPLY] =
69 "    imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
70 "                 (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
71 "                 [--symlink] [--verbose] [--ref=\"GLOB\"]\n",
72 [CAPTURE] =
73 "    imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
74 "                   [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
75 "                   [--flags EDITION_ID] [--verbose] [--dereference]\n"
76 "                   [--config=FILE]\n",
77 [DELETE] =
78 "    imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
79 [DIR] =
80 "    imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
81 [EXPORT] =
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",
86 [INFO] =
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",
90 [JOIN] =
91 "    imagex join [--check] WIMFILE SPLIT_WIM...\n",
92 [MOUNT] =
93 "    imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
94 "                 [--check] [--debug] [--streams-interface=INTERFACE]\n"
95 "                 [--ref=\"GLOB\"]\n",
96 [MOUNTRW] =
97 "    imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
98 "                   [--check] [--debug] [--streams-interface=INTERFACE]\n",
99 [SPLIT] =
100 "    imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
101 [UNMOUNT] =
102 "    imagex unmount DIRECTORY [--commit] [--check]\n",
103 };
104
105 static const struct option common_options[] = {
106         {"help", 0, NULL, 'h'},
107         {"version", 0, NULL, 'v'},
108         {NULL, 0, NULL, 0},
109 };
110
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'},
117         {NULL, 0, NULL, 0},
118 };
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'},
127         {NULL, 0, NULL, 0},
128 };
129 static const struct option delete_options[] = {
130         {"check", no_argument, NULL, 'c'},
131         {NULL, 0, NULL, 0},
132 };
133
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'},
139         {NULL, 0, NULL, 0},
140 };
141
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'},
150         {NULL, 0, NULL, 0},
151 };
152
153 static const struct option join_options[] = {
154         {"check", no_argument, NULL, 'c'},
155         {NULL, 0, NULL, 0},
156 };
157
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'},
163         {NULL, 0, NULL, 0},
164 };
165
166 static const struct option split_options[] = {
167         {"check", no_argument, NULL, 'c'},
168         {NULL, 0, NULL, 0},
169 };
170
171 static const struct option unmount_options[] = {
172         {"commit", no_argument, NULL, 'c'},
173         {"check", no_argument, NULL, 'C'},
174         {NULL, 0, NULL, 0},
175 };
176
177
178
179 /* Print formatted error message to stderr. */
180 static void imagex_error(const char *format, ...)
181 {
182         va_list va;
183         va_start(va, format);
184         fputs("ERROR: ", stderr);
185         vfprintf(stderr, format, va);
186         putc('\n', stderr);
187         va_end(va);
188 }
189
190 /* Print formatted error message to stderr. */
191 static void imagex_error_with_errno(const char *format, ...)
192 {
193         int errno_save = errno;
194         va_list va;
195         va_start(va, format);
196         fputs("ERROR: ", stderr);
197         vfprintf(stderr, format, va);
198         fprintf(stderr, ": %s\n", strerror(errno_save));
199         va_end(va);
200 }
201
202 static const char *path_basename(const char *path)
203 {
204         const char *p = path;
205         while (*p)
206                 p++;
207         p--;
208
209         /* Trailing slashes. */
210         while ((p != path - 1) && *p == '/')
211                 p--;
212
213         while ((p != path - 1) && *p != '/')
214                 p--;
215
216         return p + 1;
217 }
218
219
220 static int verify_image_exists(int image)
221 {
222         if (image == WIM_NO_IMAGE) {
223                 imagex_error("Not a valid image");
224                 return WIMLIB_ERR_INVALID_IMAGE;
225         }
226         return 0;
227 }
228
229 static int verify_image_is_single(int image)
230 {
231         if (image == WIM_ALL_IMAGES) {
232                 imagex_error("Cannot specify all images for this action");
233                 return WIMLIB_ERR_INVALID_IMAGE;
234         }
235         return 0;
236 }
237
238 static int verify_image_exists_and_is_single(int image)
239 {
240         int ret;
241         ret = verify_image_exists(image);
242         if (ret == 0)
243                 ret = verify_image_is_single(image);
244         return ret;
245 }
246
247 static int get_compression_type(const char *optarg)
248 {
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;
255         else {
256                 imagex_error("Invalid compression type `%s'! Must be "
257                              "\"maximum\", \"fast\", or \"none\".", optarg);
258                 return WIM_COMPRESSION_TYPE_INVALID;
259         }
260 }
261
262 static char *file_get_contents(const char *filename, size_t *len_ret)
263 {
264         struct stat stbuf;
265         char *buf;
266         size_t len;
267         FILE *fp;
268
269         if (stat(filename, &stbuf) != 0) {
270                 imagex_error_with_errno("Failed to stat the file `%s'", filename);
271                 return NULL;
272         }
273         len = stbuf.st_size;
274
275         fp = fopen(filename, "rb");
276         if (!fp) {
277                 imagex_error_with_errno("Failed to open the file `%s'", filename);
278                 return NULL;
279         }
280
281         buf = malloc(len);
282         if (!buf) {
283                 imagex_error("Failed to allocate buffer of %zu bytes to hold "
284                              "contents of file `%s'", len, filename);
285                 goto out_fclose;
286         }
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);
290                 goto out_free_buf;
291         }
292         *len_ret = len;
293         return buf;
294 out_free_buf:
295         free(buf);
296 out_fclose:
297         fclose(fp);
298         return NULL;
299 }
300
301 static int open_swms_from_glob(const char *swm_glob,
302                                const char *first_part,
303                                int open_flags,
304                                WIMStruct ***additional_swms_ret,
305                                unsigned *num_additional_swms_ret)
306 {
307         unsigned num_additional_swms = 0;
308         WIMStruct **additional_swms = NULL;
309         glob_t globbuf;
310         int ret;
311
312         ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
313         if (ret != 0) {
314                 if (ret == GLOB_NOMATCH) {
315                         imagex_error("Found no files for glob \"%s\"",
316                                      swm_glob);
317                 } else {
318                         imagex_error_with_errno("Failed to process glob "
319                                                 "\"%s\"", swm_glob);
320                 }
321                 ret = -1;
322                 goto out;
323         }
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");
328                 ret = -1;
329                 goto out_globfree;
330         }
331         unsigned offset = 0;
332         for (unsigned i = 0; i < num_additional_swms; i++) {
333                 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
334                         offset++;
335                         continue;
336                 }
337                 ret = wimlib_open_wim(globbuf.gl_pathv[i],
338                                       open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
339                                       &additional_swms[i - offset]);
340                 if (ret != 0)
341                         goto out_close_swms;
342         }
343         *additional_swms_ret = additional_swms;
344         *num_additional_swms_ret = num_additional_swms - offset;
345         ret = 0;
346         goto out_globfree;
347 out_close_swms:
348         for (unsigned i = 0; i < num_additional_swms; i++)
349                 wimlib_free(additional_swms[i]);
350         free(additional_swms);
351 out_globfree:
352         globfree(&globbuf);
353 out:
354         return ret;
355 }
356
357
358 /* Extract one image, or all images, from a WIM file into a directory. */
359 static int imagex_apply(int argc, const char **argv)
360 {
361         int c;
362         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
363                          WIMLIB_OPEN_FLAG_SPLIT_OK;
364         int image;
365         int num_images;
366         WIMStruct *w;
367         int ret;
368         const char *wimfile;
369         const char *dir;
370         const char *image_num_or_name;
371         int extract_flags = 0;
372
373         const char *swm_glob = NULL;
374         WIMStruct **additional_swms = NULL;
375         unsigned num_additional_swms = 0;
376
377         for_opt(c, apply_options) {
378                 switch (c) {
379                 case 'c':
380                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
381                         break;
382                 case 'h':
383                         extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
384                         break;
385                 case 's':
386                         extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
387                         break;
388                 case 'v':
389                         extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
390                         break;
391                 case 'r':
392                         swm_glob = optarg;
393                         break;
394                 default:
395                         usage(APPLY);
396                         return -1;
397                 }
398         }
399         argc -= optind;
400         argv += optind;
401         if (argc != 2 && argc != 3) {
402                 usage(APPLY);
403                 return -1;
404         }
405
406         wimfile = argv[0];
407         if (argc == 2) {
408                 image_num_or_name = "1";
409                 dir = argv[1];
410         } else {
411                 image_num_or_name = argv[1];
412                 dir = argv[2];
413         }
414
415         ret = wimlib_open_wim(wimfile, open_flags, &w);
416         if (ret != 0)
417                 return ret;
418
419         image = wimlib_resolve_image(w, image_num_or_name);
420         ret = verify_image_exists(image);
421         if (ret != 0)
422                 goto out;
423
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);
428                 usage(APPLY);
429                 ret = -1;
430                 goto out;
431         }
432
433         if (swm_glob) {
434                 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
435                                           &additional_swms,
436                                           &num_additional_swms);
437                 if (ret != 0)
438                         goto out;
439         }
440
441 #ifdef WITH_NTFS_3G
442         struct stat stbuf;
443
444         ret = stat(dir, &stbuf);
445         if (ret == 0) {
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,
451                                                                 ntfs_device,
452                                                                 extract_flags,
453                                                                 additional_swms,
454                                                                 num_additional_swms);
455                         goto out;
456                 }
457         } else {
458                 if (errno != ENOENT) {
459                         imagex_error_with_errno("Failed to stat `%s'", dir);
460                         ret = -1;
461                         goto out;
462                 }
463         }
464 #endif
465
466         ret = wimlib_extract_image(w, image, dir, extract_flags,
467                                    additional_swms, num_additional_swms);
468 out:
469         wimlib_free(w);
470         if (additional_swms)
471                 for (unsigned i = 0; i < num_additional_swms; i++)
472                         wimlib_free(additional_swms[i]);
473         return ret;
474 }
475
476 static int imagex_capture_or_append(int argc, const char **argv)
477 {
478         int c;
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;
483         const char *dir;
484         const char *wimfile;
485         const char *name;
486         const char *desc;
487         const char *flags_element = NULL;
488         const char *config_file = NULL;
489         char *config_str = NULL;
490         size_t config_len = 0;
491         WIMStruct *w = NULL;
492         int ret;
493         int cur_image;
494         char *default_name;
495         int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
496
497         for_opt(c, capture_or_append_options) {
498                 switch (c) {
499                 case 'b':
500                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
501                         break;
502                 case 'c':
503                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
504                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
505                         break;
506                 case 'C':
507                         config_file = optarg;
508                         break;
509                 case 'x':
510                         compression_type = get_compression_type(optarg);
511                         if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
512                                 return -1;
513                         break;
514                 case 'f':
515                         flags_element = optarg;
516                         break;
517                 case 'L':
518                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
519                         break;
520                 case 'v':
521                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
522                         write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
523                         break;
524                 default:
525                         usage(cmd);
526                         return -1;
527                 }
528         }
529         argc -= optind;
530         argv += optind;
531         if (argc < 2 || argc > 4) {
532                 usage(cmd);
533                 return -1;
534         }
535         dir = argv[0];
536         wimfile = argv[1];
537
538         char dir_copy[strlen(dir) + 1];
539         memcpy(dir_copy, dir, strlen(dir) + 1);
540         default_name = basename(dir_copy);
541
542         name = (argc >= 3) ? argv[2] : default_name;
543         desc = (argc >= 4) ? argv[3] : NULL;
544
545         if (config_file) {
546                 config_str = file_get_contents(config_file, &config_len);
547                 if (!config_str)
548                         return -1;
549         }
550
551         if (cmd == APPEND)
552                 ret = wimlib_open_wim(wimfile, open_flags, &w);
553         else
554                 ret = wimlib_create_new_wim(compression_type, &w);
555         if (ret != 0)
556                 goto out;
557
558 #ifdef WITH_NTFS_3G
559         struct stat stbuf;
560
561         ret = stat(dir, &stbuf);
562         if (ret == 0) {
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",
566                                ntfs_device);
567                         ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
568                                                                 name,
569                                                                 config_str,
570                                                                 config_len,
571                                                                 add_image_flags);
572                         goto out_write;
573                 }
574         } else {
575                 if (errno != ENOENT) {
576                         imagex_error_with_errno("Failed to stat `%s'", dir);
577                         ret = -1;
578                         goto out;
579                 }
580         }
581 #endif
582         ret = wimlib_add_image(w, dir, name, config_str, config_len,
583                                add_image_flags);
584
585 out_write:
586         if (ret != 0)
587                 goto out;
588         cur_image = wimlib_get_num_images(w);
589         if (desc) {
590                 ret = wimlib_set_image_descripton(w, cur_image, desc);
591                 if (ret != 0)
592                         goto out;
593         }
594         if (flags_element) {
595                 ret = wimlib_set_image_flags(w, cur_image, flags_element);
596                 if (ret != 0)
597                         goto out;
598         }
599         if (cmd == APPEND)
600                 ret = wimlib_overwrite(w, write_flags);
601         else
602                 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
603         if (ret != 0)
604                 imagex_error("Failed to write the WIM file `%s'", wimfile);
605 out:
606         wimlib_free(w);
607         free(config_str);
608         return ret;
609 }
610
611 /* Remove image(s) from a WIM. */
612 static int imagex_delete(int argc, const char **argv)
613 {
614         int c;
615         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
616         int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
617         const char *wimfile;
618         const char *image_num_or_name;
619         WIMStruct *w;
620         int image;
621         int ret;
622
623         for_opt(c, delete_options) {
624                 switch (c) {
625                 case 'c':
626                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
627                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
628                         break;
629                 default:
630                         usage(DELETE);
631                         return -1;
632                 }
633         }
634         argc -= optind;
635         argv += optind;
636
637         if (argc != 2) {
638                 if (argc < 1)
639                         imagex_error("Must specify a WIM file");
640                 if (argc < 2)
641                         imagex_error("Must specify an image");
642                 usage(DELETE);
643                 return -1;
644         }
645         wimfile = argv[0];
646         image_num_or_name = argv[1];
647
648         ret = wimlib_open_wim(wimfile, open_flags, &w);
649         if (ret != 0)
650                 return ret;
651
652         image = wimlib_resolve_image(w, image_num_or_name);
653
654         ret = verify_image_exists(image);
655         if (ret != 0)
656                 goto out;
657
658         ret = wimlib_delete_image(w, image);
659         if (ret != 0) {
660                 imagex_error("Failed to delete image from `%s'", wimfile);
661                 goto out;
662         }
663
664         ret = wimlib_overwrite(w, write_flags);
665         if (ret != 0) {
666                 imagex_error("Failed to write the file `%s' with image "
667                              "deleted", wimfile);
668         }
669 out:
670         wimlib_free(w);
671         return ret;
672 }
673
674 /* Print the files contained in an image(s) in a WIM file. */
675 static int imagex_dir(int argc, const char **argv)
676 {
677         const char *wimfile;
678         WIMStruct *w;
679         int image;
680         int ret;
681         int num_images;
682
683         if (argc < 2) {
684                 imagex_error("Must specify a WIM file");
685                 usage(DIR);
686                 return -1;
687         }
688         if (argc > 3) {
689                 imagex_error("Too many arguments");
690                 usage(DIR);
691                 return -1;
692         }
693
694         wimfile = argv[1];
695         ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
696         if (ret != 0)
697                 return ret;
698
699         if (argc == 3) {
700                 image = wimlib_resolve_image(w, argv[2]);
701                 ret = verify_image_exists(image);
702                 if (ret != 0)
703                         goto out;
704         } else {
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);
711                         usage(DIR);
712                         ret = -1;
713                         goto out;
714                 }
715                 image = 1;
716         }
717
718         ret = wimlib_print_files(w, image);
719 out:
720         wimlib_free(w);
721         return ret;
722 }
723
724 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
725  * WIM file. */
726 static int imagex_export(int argc, const char **argv)
727 {
728         int c;
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;
741         int ret;
742         int image;
743         struct stat stbuf;
744         bool wim_is_new;
745         const char *swm_glob = NULL;
746         WIMStruct **additional_swms = NULL;
747         unsigned num_additional_swms = 0;
748
749         for_opt(c, export_options) {
750                 switch (c) {
751                 case 'b':
752                         export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
753                         break;
754                 case 'c':
755                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
756                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
757                         break;
758                 case 'x':
759                         compression_type = get_compression_type(optarg);
760                         if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
761                                 return -1;
762                         compression_type_specified = true;
763                         break;
764                 case 'r':
765                         swm_glob = optarg;
766                         break;
767                 default:
768                         usage(EXPORT);
769                         return -1;
770                 }
771         }
772         argc -= optind;
773         argv += optind;
774         if (argc < 3 || argc > 5) {
775                 usage(EXPORT);
776                 return -1;
777         }
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);
785         if (ret != 0)
786                 return ret;
787
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) {
792                 int dest_ctype;
793
794                 wim_is_new = false;
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",
798                                         dest_wimfile);
799                         ret = -1;
800                         goto out;
801                 }
802                 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
803                 if (ret != 0)
804                         goto out;
805
806                 dest_ctype = wimlib_get_compression_type(dest_w);
807                 if (compression_type_specified
808                     && compression_type != dest_ctype)
809                 {
810                         imagex_error("Cannot specify a compression type that is "
811                                      "not the same as that used in the "
812                                      "destination WIM");
813                         ret = -1;
814                         goto out;
815                 }
816                 compression_type = dest_ctype;
817         } else {
818                 wim_is_new = true;
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);
824                         if (ret != 0)
825                                 goto out;
826                 } else {
827                         imagex_error_with_errno("Cannot stat file `%s'",
828                                                 dest_wimfile);
829                         ret = -1;
830                         goto out;
831                 }
832         }
833
834         image = wimlib_resolve_image(src_w, src_image_num_or_name);
835         ret = verify_image_exists(image);
836         if (ret != 0)
837                 goto out;
838
839         if (swm_glob) {
840                 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
841                                           &additional_swms,
842                                           &num_additional_swms);
843                 if (ret != 0)
844                         goto out;
845         }
846
847         ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
848                                   export_flags, additional_swms,
849                                   num_additional_swms);
850         if (ret != 0)
851                 goto out;
852
853
854         if (wim_is_new)
855                 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
856                                    write_flags);
857         else
858                 ret = wimlib_overwrite(dest_w, write_flags);
859 out:
860         wimlib_free(src_w);
861         wimlib_free(dest_w);
862         if (additional_swms)
863                 for (unsigned i = 0; i < num_additional_swms; i++)
864                         wimlib_free(additional_swms[i]);
865         return ret;
866 }
867
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)
871 {
872         int c;
873         bool boot         = false;
874         bool check        = false;
875         bool header       = false;
876         bool lookup_table = false;
877         bool xml          = false;
878         bool metadata     = false;
879         bool short_header = true;
880         const char *xml_out_file = NULL;
881         const char *wimfile;
882         const char *image_num_or_name = "all";
883         const char *new_name = NULL;
884         const char *new_desc = NULL;
885         WIMStruct *w;
886         FILE *fp;
887         int image;
888         int ret;
889         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
890                          WIMLIB_OPEN_FLAG_SPLIT_OK;
891         int part_number;
892         int total_parts;
893
894         for_opt(c, info_options) {
895                 switch (c) {
896                 case 'b':
897                         boot = true;
898                         break;
899                 case 'c':
900                         check = true;
901                         break;
902                 case 'h':
903                         header = true;
904                         short_header = false;
905                         break;
906                 case 'l':
907                         lookup_table = true;
908                         short_header = false;
909                         break;
910                 case 'x':
911                         xml = true;
912                         short_header = false;
913                         break;
914                 case 'X':
915                         xml_out_file = optarg;
916                         short_header = false;
917                         break;
918                 case 'm':
919                         metadata = true;
920                         short_header = false;
921                         break;
922                 default:
923                         usage(INFO);
924                         return -1;
925                 }
926         }
927
928         argc -= optind;
929         argv += optind;
930         if (argc == 0 || argc > 4) {
931                 usage(INFO);
932                 return -1;
933         }
934         wimfile = argv[0];
935         if (argc > 1) {
936                 image_num_or_name = argv[1];
937                 if (argc > 2) {
938                         new_name = argv[2];
939                         if (argc > 3) {
940                                 new_desc = argv[3];
941                         }
942                 }
943         }
944
945         if (check)
946                 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
947
948         ret = wimlib_open_wim(wimfile, open_flags, &w);
949         if (ret != 0)
950                 return ret;
951
952         part_number = wimlib_get_part_number(w, &total_parts);
953
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",
957                                                 image_num_or_name);
958                 if (boot)
959                         imagex_error("If you would like to set the boot "
960                                      "index to 0, specify image \"0\" with "
961                                      "the --boot flag.");
962                 ret = WIMLIB_ERR_INVALID_IMAGE;
963                 goto out;
964         }
965
966         if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
967                 if (boot) {
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;
972                         goto out;
973                 }
974                 if (new_name) {
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;
979                         goto out;
980                 }
981         }
982
983         /* Operations that print information are separated from operations that
984          * recreate the WIM file. */
985         if (!new_name && !boot) {
986
987                 /* Read-only operations */
988
989                 if (image == WIM_NO_IMAGE) {
990                         imagex_error("`%s' is not a valid image",
991                                      image_num_or_name);
992                         ret = WIMLIB_ERR_INVALID_IMAGE;
993                         goto out;
994                 }
995
996                 if (image == WIM_ALL_IMAGES && short_header)
997                         wimlib_print_wim_information(w);
998
999                 if (header)
1000                         wimlib_print_header(w);
1001
1002                 if (lookup_table) {
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);
1007                         }
1008                         wimlib_print_lookup_table(w);
1009                 }
1010
1011                 if (xml) {
1012                         ret = wimlib_extract_xml_data(w, stdout);
1013                         if (ret != 0)
1014                                 goto out;
1015                 }
1016
1017                 if (xml_out_file) {
1018                         fp = fopen(xml_out_file, "wb");
1019                         if (!fp) {
1020                                 imagex_error_with_errno("Failed to open the "
1021                                                         "file `%s' for "
1022                                                         "writing ",
1023                                                         xml_out_file);
1024                                 goto out;
1025                         }
1026                         ret = wimlib_extract_xml_data(w, fp);
1027                         if (fclose(fp) != 0) {
1028                                 imagex_error("Failed to close the file `%s'",
1029                                              xml_out_file);
1030                                 goto out;
1031                         }
1032
1033                         if (ret != 0)
1034                                 goto out;
1035                 }
1036
1037                 if (short_header)
1038                         wimlib_print_available_images(w, image);
1039
1040                 if (metadata) {
1041                         ret = wimlib_print_metadata(w, image);
1042                         if (ret != 0)
1043                                 goto out;
1044                 }
1045         } else {
1046
1047                 /* Modification operations */
1048
1049                 if (total_parts != 1) {
1050                         imagex_error("Modifying a split WIM is not supported.");
1051                         return -1;
1052                 }
1053                 if (image == WIM_ALL_IMAGES)
1054                         image = 1;
1055
1056                 if (image == WIM_NO_IMAGE && new_name) {
1057                         imagex_error("Cannot specify new_name (`%s') when "
1058                                      "using image 0", new_name);
1059                         return -1;
1060                 }
1061
1062                 if (boot) {
1063                         if (image == wimlib_get_boot_idx(w)) {
1064                                 printf("Image %d is already marked as "
1065                                        "bootable.\n", image);
1066                                 boot = false;
1067                         } else {
1068                                 printf("Marking image %d as bootable.\n",
1069                                        image);
1070                                 wimlib_set_boot_idx(w, image);
1071                         }
1072                 }
1073                 if (new_name) {
1074                         if (strcmp(wimlib_get_image_name(w, image),
1075                                                 new_name) == 0) {
1076                                 printf("Image %d is already named \"%s\".\n",
1077                                        image, new_name);
1078                                 new_name = NULL;
1079                         } else {
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);
1083                                 if (ret != 0)
1084                                         goto out;
1085                         }
1086                 }
1087                 if (new_desc) {
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);
1093                                 new_desc = NULL;
1094                         } else {
1095                                 printf("Changing the description of image %d "
1096                                        "to \"%s\".\n", image, new_desc);
1097                                 ret = wimlib_set_image_descripton(w, image,
1098                                                                   new_desc);
1099                                 if (ret != 0)
1100                                         goto out;
1101                         }
1102                 }
1103
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)) {
1108
1109                         ret = wimlib_overwrite_xml_and_header(w, check ?
1110                                         WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1111                                         WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1112                 } else {
1113                         printf("The file `%s' was not modified because nothing "
1114                                         "needed to be done.\n", wimfile);
1115                         ret = 0;
1116                 }
1117         }
1118 out:
1119         wimlib_free(w);
1120         return ret;
1121 }
1122
1123 /* Join split WIMs into one part WIM */
1124 static int imagex_join(int argc, const char **argv)
1125 {
1126         int c;
1127         int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1128         const char *output_path;
1129
1130         for_opt(c, join_options) {
1131                 switch (c) {
1132                 case 'c':
1133                         flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1134                         break;
1135                 default:
1136                         goto err;
1137                 }
1138         }
1139         argc -= optind;
1140         argv += optind;
1141
1142         if (argc < 2) {
1143                 imagex_error("Must specify at least one split WIM (.swm) parts "
1144                              "to join");
1145                 goto err;
1146         }
1147         output_path = argv[0];
1148         return wimlib_join(++argv, --argc, output_path, flags);
1149 err:
1150         usage(JOIN);
1151         return -1;
1152 }
1153
1154 /* Mounts an image using a FUSE mount. */
1155 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1156 {
1157         int c;
1158         int mount_flags = 0;
1159         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1160                          WIMLIB_OPEN_FLAG_SPLIT_OK;
1161         const char *wimfile;
1162         const char *dir;
1163         WIMStruct *w;
1164         int image;
1165         int num_images;
1166         int ret;
1167         const char *swm_glob = NULL;
1168         WIMStruct **additional_swms = NULL;
1169         unsigned num_additional_swms = 0;
1170
1171         if (strcmp(argv[0], "mountrw") == 0)
1172                 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1173
1174         for_opt(c, mount_options) {
1175                 switch (c) {
1176                 case 'c':
1177                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1178                         break;
1179                 case 'd':
1180                         mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1181                         break;
1182                 case 's':
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;
1189                         else {
1190                                 imagex_error("Unknown stream interface \"%s\"", optarg);
1191                                 goto mount_usage;
1192                         }
1193                         break;
1194                 case 'r':
1195                         swm_glob = optarg;
1196                         break;
1197                 default:
1198                         goto mount_usage;
1199                 }
1200         }
1201         argc -= optind;
1202         argv += optind;
1203         if (argc != 2 && argc != 3)
1204                 goto mount_usage;
1205
1206         wimfile = argv[0];
1207
1208         ret = wimlib_open_wim(wimfile, open_flags, &w);
1209         if (ret != 0)
1210                 return ret;
1211
1212         if (swm_glob) {
1213                 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1214                                           &additional_swms,
1215                                           &num_additional_swms);
1216                 if (ret != 0)
1217                         goto out;
1218         }
1219
1220         if (argc == 2) {
1221                 image = 1;
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)
1227                                         ? MOUNTRW : MOUNT);
1228                         ret = WIMLIB_ERR_INVALID_IMAGE;
1229                         goto out;
1230                 }
1231                 dir = argv[1];
1232         } else {
1233                 image = wimlib_resolve_image(w, argv[1]);
1234                 dir = argv[2];
1235         }
1236
1237         ret = verify_image_exists_and_is_single(image);
1238         if (ret != 0)
1239                 goto out;
1240
1241         ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1242                            num_additional_swms);
1243         if (ret != 0) {
1244                 imagex_error("Failed to mount image %d from `%s' on `%s'",
1245                              image, wimfile, dir);
1246
1247         }
1248 out:
1249         wimlib_free(w);
1250         if (additional_swms)
1251                 for (unsigned i = 0; i < num_additional_swms; i++)
1252                         wimlib_free(additional_swms[i]);
1253         return ret;
1254 mount_usage:
1255         usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1256                         ? MOUNTRW : MOUNT);
1257         return -1;
1258 }
1259
1260 /* Split a WIM into a spanned set */
1261 static int imagex_split(int argc, const char **argv)
1262 {
1263         int c;
1264         int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1265         unsigned long part_size;
1266         char *tmp;
1267
1268         for_opt(c, split_options) {
1269                 switch (c) {
1270                 case 'c':
1271                         flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1272                         break;
1273                 default:
1274                         usage(SPLIT);
1275                         return -1;
1276                 }
1277         }
1278         argc -= optind;
1279         argv += optind;
1280
1281         if (argc != 3) {
1282                 usage(SPLIT);
1283                 return -1;
1284         }
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.");
1289                 return -1;
1290         }
1291         return wimlib_split(argv[0], argv[1], part_size, flags);
1292 }
1293
1294 /* Unmounts an image. */
1295 static int imagex_unmount(int argc, const char **argv)
1296 {
1297         int c;
1298         int unmount_flags = 0;
1299         int ret;
1300
1301         for_opt(c, unmount_options) {
1302                 switch (c) {
1303                 case 'c':
1304                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1305                         break;
1306                 case 'C':
1307                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1308                         break;
1309                 default:
1310                         usage(UNMOUNT);
1311                         return -1;
1312                 }
1313         }
1314         argc -= optind;
1315         argv += optind;
1316         if (argc != 1) {
1317                 usage(UNMOUNT);
1318                 return -1;
1319         }
1320
1321         ret = wimlib_unmount(argv[0], unmount_flags);
1322         if (ret != 0)
1323                 imagex_error("Failed to unmount `%s'", argv[0]);
1324         return ret;
1325 }
1326
1327 struct imagex_command {
1328         const char *name;
1329         int (*func)(int , const char **);
1330         int cmd;
1331 };
1332
1333
1334 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1335                 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1336
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},
1350 };
1351
1352 static void version()
1353 {
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"
1360         "\n"
1361         "Report bugs to "PACKAGE_BUGREPORT".\n";
1362         fputs(s, stdout);
1363 }
1364
1365
1366 static void help_or_version(int argc, const char **argv)
1367 {
1368         int i;
1369         const char *p;
1370         const struct imagex_command *cmd;
1371
1372         for (i = 1; i < argc; i++) {
1373                 p = argv[i];
1374                 if (*p == '-')
1375                         p++;
1376                 else
1377                         continue;
1378                 if (*p == '-')
1379                         p++;
1380                 if (strcmp(p, "help") == 0) {
1381                         for_imagex_command(cmd) {
1382                                 if (strcmp(cmd->name, argv[1]) == 0) {
1383                                         usage(cmd->cmd);
1384                                         exit(0);
1385                                 }
1386                         }
1387                         usage_all();
1388                         exit(0);
1389                 }
1390                 if (strcmp(p, "version") == 0) {
1391                         version();
1392                         exit(0);
1393                 }
1394         }
1395 }
1396
1397
1398 static void usage(int cmd_type)
1399 {
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",
1406                                cmd->name);
1407 }
1408
1409 static void usage_all()
1410 {
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 =
1415 "    imagex --help\n"
1416 "    imagex --version\n"
1417 "\n"
1418 "    The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1419 "\n"
1420 "    Try `man imagex' for more information.\n"
1421         ;
1422         fputs(extra, stdout);
1423 }
1424
1425
1426 int main(int argc, const char **argv)
1427 {
1428         const struct imagex_command *cmd;
1429         int ret;
1430
1431         if (argc < 2) {
1432                 imagex_error("No command specified");
1433                 usage_all();
1434                 return 1;
1435         }
1436
1437         help_or_version(argc, argv);
1438         argc--;
1439         argv++;
1440
1441         wimlib_set_print_errors(true);
1442
1443         for_imagex_command(cmd) {
1444                 if (strcmp(cmd->name, *argv) == 0) {
1445                         ret = cmd->func(argc, argv);
1446                         if (ret > 0) {
1447                                 imagex_error("Exiting with error code %d:\n"
1448                                              "       %s.", ret,
1449                                              wimlib_get_error_string(ret));
1450                                 if (ret == WIMLIB_ERR_NTFS_3G)
1451                                         imagex_error_with_errno("errno");
1452                         }
1453                         return ret;
1454                 }
1455         }
1456
1457         imagex_error("Unrecognized command: `%s'", argv[0]);
1458         usage_all();
1459         return 1;
1460 }