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