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