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