imagex-apply.1 updates
[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         size_t offset = 0;
332         for (size_t 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         }
461 #endif
462
463         ret = wimlib_extract_image(w, image, dir, extract_flags,
464                                    additional_swms, num_additional_swms);
465 out:
466         wimlib_free(w);
467         if (additional_swms)
468                 for (size_t i = 0; i < num_additional_swms; i++)
469                         wimlib_free(additional_swms[i]);
470         return ret;
471 }
472
473 static int imagex_capture_or_append(int argc, const char **argv)
474 {
475         int c;
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;
480         const char *dir;
481         const char *wimfile;
482         const char *name;
483         const char *desc;
484         const char *flags_element = NULL;
485         const char *config_file = NULL;
486         char *config_str = NULL;
487         size_t config_len = 0;
488         WIMStruct *w = NULL;
489         int ret;
490         int cur_image;
491         char *default_name;
492         int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
493
494         for_opt(c, capture_or_append_options) {
495                 switch (c) {
496                 case 'b':
497                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
498                         break;
499                 case 'c':
500                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
501                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
502                         break;
503                 case 'C':
504                         config_file = optarg;
505                         break;
506                 case 'x':
507                         compression_type = get_compression_type(optarg);
508                         if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
509                                 return -1;
510                         break;
511                 case 'f':
512                         flags_element = optarg;
513                         break;
514                 case 'L':
515                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
516                         break;
517                 case 'v':
518                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
519                         break;
520                 default:
521                         usage(cmd);
522                         return -1;
523                 }
524         }
525         argc -= optind;
526         argv += optind;
527         if (argc < 2 || argc > 4) {
528                 usage(cmd);
529                 return -1;
530         }
531         dir     = argv[0];
532         wimfile = argv[1];
533
534         char dir_copy[strlen(dir) + 1];
535         memcpy(dir_copy, dir, strlen(dir) + 1);
536         default_name = basename(dir_copy);
537
538         name    = (argc >= 3) ? argv[2] : default_name;
539         desc    = (argc >= 4) ? argv[3] : NULL;
540
541         if (config_file) {
542                 config_str = file_get_contents(config_file, &config_len);
543                 if (!config_str)
544                         return -1;
545         }
546
547         if (cmd == APPEND)
548                 ret = wimlib_open_wim(wimfile, open_flags, &w);
549         else
550                 ret = wimlib_create_new_wim(compression_type, &w);
551         if (ret != 0)
552                 goto out;
553
554 #ifdef WITH_NTFS_3G
555         struct stat stbuf;
556
557         ret = stat(dir, &stbuf);
558         if (ret == 0) {
559                 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
560                         const char *ntfs_device = dir;
561                         printf("Capturing WIM image NTFS filesystem on `%s'\n",
562                                ntfs_device);
563                         ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
564                                                                 name,
565                                                                 config_str,
566                                                                 config_len,
567                                                                 add_image_flags);
568                         goto out_write;
569                 }
570         } else {
571                 if (errno != ENOENT) {
572                         imagex_error_with_errno("Failed to stat `%s'", dir);
573                         ret = -1;
574                         goto out;
575                 }
576         }
577 #endif
578         ret = wimlib_add_image(w, dir, name, config_str, config_len,
579                                add_image_flags);
580
581 out_write:
582         if (ret != 0)
583                 goto out;
584         cur_image = wimlib_get_num_images(w);
585         if (desc) {
586                 ret = wimlib_set_image_descripton(w, cur_image, desc);
587                 if (ret != 0)
588                         goto out;
589         }
590         if (flags_element) {
591                 ret = wimlib_set_image_flags(w, cur_image, flags_element);
592                 if (ret != 0)
593                         goto out;
594         }
595         if (cmd == APPEND)
596                 ret = wimlib_overwrite(w, write_flags);
597         else
598                 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
599         if (ret != 0)
600                 imagex_error("Failed to write the WIM file `%s'", wimfile);
601 out:
602         wimlib_free(w);
603         free(config_str);
604         return ret;
605 }
606
607 /* Remove image(s) from a WIM. */
608 static int imagex_delete(int argc, const char **argv)
609 {
610         int c;
611         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
612         int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
613         const char *wimfile;
614         const char *image_num_or_name;
615         WIMStruct *w;
616         int image;
617         int ret;
618
619         for_opt(c, delete_options) {
620                 switch (c) {
621                 case 'c':
622                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
623                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
624                         break;
625                 default:
626                         usage(DELETE);
627                         return -1;
628                 }
629         }
630         argc -= optind;
631         argv += optind;
632
633         if (argc != 2) {
634                 if (argc < 1)
635                         imagex_error("Must specify a WIM file");
636                 if (argc < 2)
637                         imagex_error("Must specify an image");
638                 usage(DELETE);
639                 return -1;
640         }
641         wimfile = argv[0];
642         image_num_or_name = argv[1];
643
644         ret = wimlib_open_wim(wimfile, open_flags, &w);
645         if (ret != 0)
646                 return ret;
647
648         image = wimlib_resolve_image(w, image_num_or_name);
649
650         ret = verify_image_exists(image);
651         if (ret != 0)
652                 goto done;
653
654         ret = wimlib_delete_image(w, image);
655         if (ret != 0) {
656                 imagex_error("Failed to delete image from `%s'", wimfile);
657                 goto done;
658         }
659
660         ret = wimlib_overwrite(w, write_flags);
661         if (ret != 0) {
662                 imagex_error("Failed to write the file `%s' with image "
663                              "deleted", wimfile);
664         }
665 done:
666         wimlib_free(w);
667         return ret;
668 }
669
670 /* Print the files contained in an image(s) in a WIM file. */
671 static int imagex_dir(int argc, const char **argv)
672 {
673         const char *wimfile;
674         WIMStruct *w;
675         int image;
676         int ret;
677         int num_images;
678         int part_number;
679
680         if (argc < 2) {
681                 imagex_error("Must specify a WIM file");
682                 usage(DIR);
683                 return -1;
684         }
685         if (argc > 3) {
686                 imagex_error("Too many arguments");
687                 usage(DIR);
688                 return -1;
689         }
690
691         wimfile = argv[1];
692         ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
693         if (ret != 0)
694                 return ret;
695
696         part_number = wimlib_get_part_number(w, NULL);
697         if (part_number != 1) {
698                 imagex_error("`%s' is part %d of a split WIM!  Specify the "
699                              "first part to see the files",
700                              wimfile, part_number);
701                 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
702                 goto done;
703         }
704
705         if (argc == 3) {
706                 image = wimlib_resolve_image(w, argv[2]);
707                 ret = verify_image_exists(image);
708                 if (ret != 0)
709                         goto done;
710         } else {
711                 /* Image was not specified.  If the WIM only contains one image,
712                  * choose that one; otherwise, print an error. */
713                 num_images = wimlib_get_num_images(w);
714                 if (num_images != 1) {
715                         imagex_error("The file `%s' contains %d images; Please "
716                                      "select one.", wimfile, num_images);
717                         usage(DIR);
718                         ret = -1;
719                         goto done;
720                 }
721                 image = 1;
722         }
723
724         ret = wimlib_print_files(w, image);
725 done:
726         wimlib_free(w);
727         return ret;
728 }
729
730 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
731  * WIM file. */
732 static int imagex_export(int argc, const char **argv)
733 {
734         int c;
735         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
736         int export_flags = 0;
737         int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
738         int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
739         bool compression_type_specified = false;
740         const char *src_wimfile;
741         const char *src_image_num_or_name;
742         const char *dest_wimfile;
743         const char *dest_name;
744         const char *dest_desc;
745         WIMStruct *src_w = NULL;
746         WIMStruct *dest_w = NULL;
747         int ret;
748         int image;
749         struct stat stbuf;
750         bool wim_is_new;
751         const char *swm_glob = NULL;
752         WIMStruct **additional_swms = NULL;
753         unsigned num_additional_swms = 0;
754
755         for_opt(c, export_options) {
756                 switch (c) {
757                 case 'b':
758                         export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
759                         break;
760                 case 'c':
761                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
762                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
763                         break;
764                 case 'x':
765                         compression_type = get_compression_type(optarg);
766                         if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
767                                 return -1;
768                         compression_type_specified = true;
769                         break;
770                 case 'r':
771                         swm_glob = optarg;
772                         break;
773                 default:
774                         usage(EXPORT);
775                         return -1;
776                 }
777         }
778         argc -= optind;
779         argv += optind;
780         if (argc < 3 || argc > 5) {
781                 usage(EXPORT);
782                 return -1;
783         }
784         src_wimfile           = argv[0];
785         src_image_num_or_name = argv[1];
786         dest_wimfile          = argv[2];
787         dest_name             = (argc >= 4) ? argv[3] : NULL;
788         dest_desc             = (argc >= 5) ? argv[4] : NULL;
789         ret = wimlib_open_wim(src_wimfile,
790                               open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
791         if (ret != 0)
792                 return ret;
793
794         /* Determine if the destination is an existing file or not.  
795          * If so, we try to append the exported image(s) to it; otherwise, we
796          * create a new WIM containing the exported image(s). */
797         if (stat(dest_wimfile, &stbuf) == 0) {
798                 wim_is_new = false;
799                 /* Destination file exists. */
800                 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
801                         imagex_error("`%s' is not a regular file",
802                                         dest_wimfile);
803                         goto out;
804                 }
805                 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
806                 if (ret != 0)
807                         goto out;
808
809                 if (compression_type_specified && compression_type != 
810                                 wimlib_get_compression_type(dest_w)) {
811                         imagex_error("Cannot specify a compression type that is "
812                                      "not the same as that used in the "
813                                      "destination WIM");
814                         ret = -1;
815                         goto out;
816                 }
817                 compression_type = wimlib_get_compression_type(dest_w);
818         } else {
819                 wim_is_new = true;
820                 /* dest_wimfile is not an existing file, so create a new WIM. */
821                 if (errno == ENOENT) {
822                         ret = wimlib_create_new_wim(compression_type, &dest_w);
823                         if (ret != 0)
824                                 goto out;
825                 } else {
826                         imagex_error_with_errno("Cannot stat file `%s'",
827                                                 dest_wimfile);
828                         goto out;
829                 }
830         }
831
832         image = wimlib_resolve_image(src_w, src_image_num_or_name);
833         ret = verify_image_exists(image);
834         if (ret != 0)
835                 goto out;
836
837         if (swm_glob) {
838                 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
839                                           &additional_swms,
840                                           &num_additional_swms);
841                 if (ret != 0)
842                         goto out;
843         }
844
845         ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc, 
846                                   export_flags, additional_swms,
847                                   num_additional_swms);
848         if (ret != 0)
849                 goto out;
850
851
852         if (wim_is_new)
853                 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES, 
854                                    write_flags);
855         else
856                 ret = wimlib_overwrite(dest_w, write_flags);
857 out:
858         wimlib_free(src_w);
859         wimlib_free(dest_w);
860         if (additional_swms)
861                 for (unsigned i = 0; i < num_additional_swms; i++)
862                         wimlib_free(additional_swms[i]);
863         return ret;
864 }
865
866 /* Prints information about a WIM file; also can mark an image as bootable,
867  * change the name of an image, or change the description of an image. */
868 static int imagex_info(int argc, const char **argv)
869 {
870         int c;
871         bool boot         = false;
872         bool check        = false;
873         bool header       = false;
874         bool lookup_table = false;
875         bool xml          = false;
876         bool metadata     = false;
877         bool short_header = true;
878         const char *xml_out_file = NULL;
879         const char *wimfile;
880         const char *image_num_or_name = "all";
881         const char *new_name = NULL;
882         const char *new_desc = NULL;
883         WIMStruct *w;
884         FILE *fp;
885         int image;
886         int ret;
887         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS | 
888                          WIMLIB_OPEN_FLAG_SPLIT_OK;
889         int part_number;
890         int total_parts;
891
892         for_opt(c, info_options) {
893                 switch (c) {
894                 case 'b':
895                         boot = true;
896                         break;
897                 case 'c':
898                         check = true;
899                         break;
900                 case 'h':
901                         header = true;
902                         short_header = false;
903                         break;
904                 case 'l':
905                         lookup_table = true;
906                         short_header = false;
907                         break;
908                 case 'x':
909                         xml = true;
910                         short_header = false;
911                         break;
912                 case 'X':
913                         xml_out_file = optarg;
914                         short_header = false;
915                         break;
916                 case 'm':
917                         metadata = true;
918                         short_header = false;
919                         break;
920                 default:
921                         usage(INFO);
922                         return -1;
923                 }
924         }
925
926         argc -= optind;
927         argv += optind;
928         if (argc == 0 || argc > 4) {
929                 usage(INFO);
930                 return -1;
931         }
932         wimfile = argv[0];
933         if (argc > 1) {
934                 image_num_or_name = argv[1];
935                 if (argc > 2) {
936                         new_name = argv[2];
937                         if (argc > 3) {
938                                 new_desc = argv[3];
939                         } 
940                 }
941         }
942
943         if (check)
944                 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
945
946         ret = wimlib_open_wim(wimfile, open_flags, &w);
947         if (ret != 0)
948                 return ret;
949
950         part_number = wimlib_get_part_number(w, &total_parts);
951
952         /*if (total_parts > 1 && part_number > 1) {*/
953                 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
954                        /*"         Select the first part if you want to see information\n"*/
955                        /*"         about images in the WIM.\n", */
956                        /*part_number, total_parts);*/
957         /*}*/
958
959         image = wimlib_resolve_image(w, image_num_or_name);
960         if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
961                 imagex_error("The image `%s' does not exist", 
962                                                 image_num_or_name);
963                 if (boot)
964                         imagex_error("If you would like to set the boot "
965                                      "index to 0, specify image \"0\" with "
966                                      "the --boot flag.");
967                 ret = WIMLIB_ERR_INVALID_IMAGE;
968                 goto done;
969         }
970
971         if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
972                 if (boot) {
973                         imagex_error("Cannot specify the --boot flag "
974                                      "without specifying a specific "
975                                      "image in a multi-image WIM");
976                         ret = WIMLIB_ERR_INVALID_IMAGE;
977                         goto done;
978                 }
979                 if (new_name) {
980                         imagex_error("Cannot specify the NEW_NAME "
981                                      "without specifying a specific "
982                                      "image in a multi-image WIM");
983                         ret = WIMLIB_ERR_INVALID_IMAGE;
984                         goto done;
985                 }
986         }
987
988         /* Operations that print information are separated from operations that
989          * recreate the WIM file. */
990         if (!new_name && !boot) {
991
992                 if (image == WIM_NO_IMAGE) {
993                         imagex_error("`%s' is not a valid image",
994                                      image_num_or_name);
995                         ret = WIMLIB_ERR_INVALID_IMAGE;
996                         goto done;
997                 }
998
999                 if (image == WIM_ALL_IMAGES && short_header)
1000                         wimlib_print_wim_information(w);
1001
1002                 if (header)
1003                         wimlib_print_header(w);
1004
1005                 if (lookup_table) {
1006                         if (total_parts != 1) {
1007                                 printf("Warning: Only showing the lookup table "
1008                                        "for part %d of a %d-part WIM.\n",
1009                                        part_number, total_parts);
1010                         }
1011                         wimlib_print_lookup_table(w);
1012                 }
1013
1014                 if (xml) {
1015                         ret = wimlib_extract_xml_data(w, stdout);
1016                         if (ret != 0)
1017                                 goto done;
1018                 }
1019
1020                 if (xml_out_file) {
1021                         fp = fopen(xml_out_file, "wb");
1022                         if (!fp) {
1023                                 imagex_error_with_errno("Failed to open the "
1024                                                         "file `%s' for "
1025                                                         "writing ",
1026                                                         xml_out_file);
1027                                 goto done;
1028                         }
1029                         ret = wimlib_extract_xml_data(w, fp);
1030                         if (fclose(fp) != 0) {
1031                                 imagex_error("Failed to close the file `%s'",
1032                                              xml_out_file);
1033                                 goto done;
1034                         }
1035
1036                         if (ret != 0)
1037                                 goto done;
1038                 }
1039
1040                 if (short_header)
1041                         wimlib_print_available_images(w, image);
1042
1043                 if (metadata) {
1044                         ret = wimlib_print_metadata(w, image);
1045                         if (ret != 0)
1046                                 goto done;
1047                 }
1048         } else {
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 done;
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 done;
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
1119 done:
1120         wimlib_free(w);
1121         return ret;
1122 }
1123
1124 /* Join split WIMs into one part WIM */
1125 static int imagex_join(int argc, const char **argv)
1126 {
1127         int c;
1128         int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1129         const char *output_path;
1130
1131         for_opt(c, join_options) {
1132                 switch (c) {
1133                 case 'c':
1134                         flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1135                         break;
1136                 default:
1137                         goto err;
1138                 }
1139         }
1140         argc -= optind;
1141         argv += optind;
1142
1143         if (argc < 2) {
1144                 imagex_error("Must specify at least one split WIM (.swm) parts "
1145                              "to join");
1146                 goto err;
1147         }
1148         output_path = argv[0];
1149         return wimlib_join(++argv, --argc, output_path, flags);
1150 err:
1151         usage(JOIN);
1152         return -1;
1153 }
1154
1155 /* Mounts an image using a FUSE mount. */
1156 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1157 {
1158         int c;
1159         int mount_flags = 0;
1160         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1161                          WIMLIB_OPEN_FLAG_SPLIT_OK;
1162         const char *wimfile;
1163         const char *dir;
1164         WIMStruct *w;
1165         int image;
1166         int num_images;
1167         int ret;
1168         const char *swm_glob = NULL;
1169         WIMStruct **additional_swms = NULL;
1170         unsigned num_additional_swms = 0;
1171
1172         if (strcmp(argv[0], "mountrw") == 0)
1173                 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
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
1267         for_opt(c, split_options) {
1268                 switch (c) {
1269                 case 'c':
1270                         flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1271                         break;
1272                 default:
1273                         usage(SPLIT);
1274                         return -1;
1275                 }
1276         }
1277         argc -= optind;
1278         argv += optind;
1279
1280         if (argc != 3) {
1281                 usage(SPLIT);
1282                 return -1;
1283         }
1284         part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1285         return wimlib_split(argv[0], argv[1], part_size, flags);
1286 }
1287
1288 /* Unmounts an image. */
1289 static int imagex_unmount(int argc, const char **argv)
1290 {
1291         int c;
1292         int unmount_flags = 0;
1293         int ret;
1294
1295         for_opt(c, unmount_options) {
1296                 switch (c) {
1297                 case 'c':
1298                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1299                         break;
1300                 case 'C':
1301                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1302                         break;
1303                 default:
1304                         usage(UNMOUNT);
1305                         return -1;
1306                 }
1307         }
1308         argc -= optind;
1309         argv += optind;
1310         if (argc != 1) {
1311                 usage(UNMOUNT);
1312                 return -1;
1313         }
1314
1315         ret = wimlib_unmount(argv[0], unmount_flags);
1316         if (ret != 0)
1317                 imagex_error("Failed to unmount `%s'", argv[0]);
1318         return ret;
1319 }
1320
1321 struct imagex_command {
1322         const char *name;
1323         int (*func)(int , const char **);
1324         int cmd;
1325 };
1326
1327
1328 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1329                 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1330
1331 static struct imagex_command imagex_commands[] = {
1332         {"append",  imagex_capture_or_append, APPEND},
1333         {"apply",   imagex_apply,          APPLY},
1334         {"capture", imagex_capture_or_append, CAPTURE},
1335         {"delete",  imagex_delete,         DELETE},
1336         {"dir",     imagex_dir,            DIR},
1337         {"export",  imagex_export,         EXPORT},
1338         {"info",    imagex_info,           INFO},
1339         {"join",    imagex_join,           JOIN},
1340         {"mount",   imagex_mount_rw_or_ro, MOUNT},
1341         {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1342         {"split",   imagex_split,          SPLIT},
1343         {"unmount", imagex_unmount,        UNMOUNT},
1344 };
1345
1346 static void version()
1347 {
1348         static const char *s = 
1349         "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1350         "Copyright (C) 2012 Eric Biggers\n"
1351         "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1352         "This is free software: you are free to change and redistribute it.\n"
1353         "There is NO WARRANTY, to the extent permitted by law.\n"
1354         "\n"
1355         "Report bugs to "PACKAGE_BUGREPORT".\n";
1356         fputs(s, stdout);
1357 }
1358
1359
1360 static void help_or_version(int argc, const char **argv)
1361 {
1362         int i;
1363         const char *p;
1364         struct imagex_command *cmd;
1365
1366         for (i = 1; i < argc; i++) {
1367                 p = argv[i];
1368                 if (*p == '-')
1369                         p++;
1370                 else
1371                         continue;
1372                 if (*p == '-')
1373                         p++;
1374                 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1375                         for_imagex_command(cmd) {
1376                                 if (strcmp(cmd->name, argv[1]) == 0) {
1377                                         usage(cmd->cmd);
1378                                         exit(0);
1379                                 }
1380                         }
1381                         usage_all();
1382                         exit(0);
1383                 }
1384                 if (strcmp(p, "version") == 0) {
1385                         version();
1386                         exit(0);
1387                 }
1388         }
1389 }
1390
1391
1392 static void usage(int cmd_type)
1393 {
1394         struct imagex_command *cmd;
1395         puts("IMAGEX: Usage:");
1396         fputs(usage_strings[cmd_type], stdout);
1397         for_imagex_command(cmd)
1398                 if (cmd->cmd == cmd_type)
1399                         printf("\nTry `man imagex-%s' for more details.\n",
1400                                cmd->name);
1401 }
1402
1403 static void usage_all()
1404 {
1405         puts("IMAGEX: Usage:");
1406         for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1407                 fputs(usage_strings[i], stdout);
1408         static const char *extra = 
1409 "    imagex --help\n"
1410 "    imagex --version\n"
1411 "\n"
1412 "    The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1413 "\n"
1414 "    Try `man imagex' for more information.\n"
1415         ;
1416         fputs(extra, stdout);
1417 }
1418
1419
1420 int main(int argc, const char **argv)
1421 {
1422         struct imagex_command *cmd;
1423         int ret;
1424
1425         if (argc < 2) {
1426                 imagex_error("No command specified");
1427                 usage_all();
1428                 return 1;
1429         }
1430
1431         help_or_version(argc, argv);
1432         argc--;
1433         argv++;
1434
1435         wimlib_set_print_errors(true);
1436
1437         for_imagex_command(cmd) {
1438                 if (strcmp(cmd->name, *argv) == 0) {
1439                         ret = cmd->func(argc, argv);
1440                         if (ret > 0) {
1441                                 imagex_error("Exiting with error code %d:\n"
1442                                              "       %s.", ret,
1443                                              wimlib_get_error_string(ret));
1444                                 if (ret == WIMLIB_ERR_NTFS_3G)
1445                                         imagex_error_with_errno("errno");
1446                         }
1447                         return ret;
1448                 }
1449         }
1450
1451         imagex_error("Unrecognized command: `%s'", argv[0]);
1452         usage_all();
1453         return 1;
1454 }