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