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