]> wimlib.net Git - wimlib/blob - programs/imagex.c
Symbolic links (IN PROGRESS)
[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                         goto done;
703                 }
704                 compression_type = wimlib_get_compression_type(dest_w);
705         } else {
706                 wim_is_new = true;
707                 /* dest_wimfile is not an existing file, so create a new WIM. */
708                 if (errno == ENOENT) {
709                         ret = wimlib_create_new_wim(compression_type, &dest_w);
710                         if (ret != 0)
711                                 goto done;
712                 } else {
713                         imagex_error_with_errno("Cannot stat file `%s'",
714                                                 dest_wimfile);
715                         goto done;
716                 }
717         }
718
719         image = wimlib_resolve_image(src_w, src_image_num_or_name);
720         ret = verify_image_exists(image);
721         if (ret != 0)
722                 goto done;
723
724         ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc, 
725                                   export_flags);
726         if (ret != 0)
727                 goto done;
728
729
730         if (wim_is_new)
731                 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES, 
732                                    write_flags);
733         else
734                 ret = wimlib_overwrite(dest_w, write_flags);
735 done:
736         wimlib_free(src_w);
737         wimlib_free(dest_w);
738         return ret;
739 }
740
741 /* Prints information about a WIM file; also can mark an image as bootable,
742  * change the name of an image, or change the description of an image. */
743 static int imagex_info(int argc, const char **argv)
744 {
745         int c;
746         bool boot         = false;
747         bool check        = false;
748         bool header       = false;
749         bool lookup_table = false;
750         bool xml          = false;
751         bool metadata     = false;
752         bool short_header = true;
753         const char *xml_out_file = NULL;
754         const char *wimfile;
755         const char *image_num_or_name = "all";
756         const char *new_name = NULL;
757         const char *new_desc = NULL;
758         WIMStruct *w;
759         FILE *fp;
760         int image;
761         int ret;
762         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS | 
763                          WIMLIB_OPEN_FLAG_SPLIT_OK;
764         int part_number;
765         int total_parts;
766
767         for_opt(c, info_options) {
768                 switch (c) {
769                 case 'b':
770                         boot = true;
771                         break;
772                 case 'c':
773                         check = true;
774                         break;
775                 case 'h':
776                         header = true;
777                         short_header = false;
778                         break;
779                 case 'l':
780                         lookup_table = true;
781                         short_header = false;
782                         break;
783                 case 'x':
784                         xml = true;
785                         short_header = false;
786                         break;
787                 case 'X':
788                         xml_out_file = optarg;
789                         short_header = false;
790                         break;
791                 case 'm':
792                         metadata = true;
793                         short_header = false;
794                         break;
795                 default:
796                         usage(INFO);
797                         return -1;
798                 }
799         }
800
801         argc -= optind;
802         argv += optind;
803         if (argc == 0 || argc > 4) {
804                 usage(INFO);
805                 return -1;
806         }
807         wimfile = argv[0];
808         if (argc > 1) {
809                 image_num_or_name = argv[1];
810                 if (argc > 2) {
811                         new_name = argv[2];
812                         if (argc > 3) {
813                                 new_desc = argv[3];
814                         } 
815                 }
816         }
817
818         if (check)
819                 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
820
821         ret = wimlib_open_wim(wimfile, open_flags, &w);
822         if (ret != 0)
823                 return ret;
824
825         part_number = wimlib_get_part_number(w, &total_parts);
826
827         /*if (total_parts > 1 && part_number > 1) {*/
828                 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
829                        /*"         Select the first part if you want to see information\n"*/
830                        /*"         about images in the WIM.\n", */
831                        /*part_number, total_parts);*/
832         /*}*/
833
834         image = wimlib_resolve_image(w, image_num_or_name);
835         if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
836                 imagex_error("The image `%s' does not exist", 
837                                                 image_num_or_name);
838                 if (boot)
839                         imagex_error("If you would like to set the boot "
840                                      "index to 0, specify image \"0\" with "
841                                      "the --boot flag.");
842                 ret = WIMLIB_ERR_INVALID_IMAGE;
843                 goto done;
844         }
845
846         if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
847                 if (boot) {
848                         imagex_error("Cannot specify the --boot flag "
849                                      "without specifying a specific "
850                                      "image in a multi-image WIM");
851                         ret = WIMLIB_ERR_INVALID_IMAGE;
852                         goto done;
853                 }
854                 if (new_name) {
855                         imagex_error("Cannot specify the NEW_NAME "
856                                      "without specifying a specific "
857                                      "image in a multi-image WIM");
858                         ret = WIMLIB_ERR_INVALID_IMAGE;
859                         goto done;
860                 }
861         }
862
863         /* Operations that print information are separated from operations that
864          * recreate the WIM file. */
865         if (!new_name && !boot) {
866
867                 if (image == WIM_NO_IMAGE) {
868                         imagex_error("`%s' is not a valid image",
869                                      image_num_or_name);
870                         ret = WIMLIB_ERR_INVALID_IMAGE;
871                         goto done;
872                 }
873
874                 if (image == WIM_ALL_IMAGES && short_header)
875                         wimlib_print_wim_information(w);
876
877                 if (header)
878                         wimlib_print_header(w);
879
880                 if (lookup_table) {
881                         if (total_parts != 1) {
882                                 printf("Warning: Only showing the lookup table "
883                                        "for part %d of a %d-part WIM.\n",
884                                        part_number, total_parts);
885                         }
886                         wimlib_print_lookup_table(w);
887                 }
888
889                 if (xml) {
890                         ret = wimlib_extract_xml_data(w, stdout);
891                         if (ret != 0)
892                                 goto done;
893                 }
894
895                 if (xml_out_file) {
896                         fp = fopen(xml_out_file, "wb");
897                         if (!fp) {
898                                 imagex_error_with_errno("Failed to open the "
899                                                         "file `%s' for "
900                                                         "writing ",
901                                                         xml_out_file);
902                                 goto done;
903                         }
904                         ret = wimlib_extract_xml_data(w, fp);
905                         if (fclose(fp) != 0) {
906                                 imagex_error("Failed to close the file `%s'",
907                                              xml_out_file);
908                                 goto done;
909                         }
910
911                         if (ret != 0)
912                                 goto done;
913                 }
914
915                 if (short_header)
916                         wimlib_print_available_images(w, image);
917
918                 if (metadata) {
919                         if (total_parts != 1 && part_number != 1) {
920                                 imagex_error("Select part 1 of this %d-part WIM "
921                                              "to see the image metadata",
922                                              total_parts);
923                                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
924                         }
925                         ret = wimlib_print_metadata(w, image);
926                         if (ret != 0)
927                                 goto done;
928                 }
929         } else {
930                 if (total_parts != 1) {
931                         imagex_error("Modifying a split WIM is not supported.");
932                         return -1;
933                 }
934                 if (image == WIM_ALL_IMAGES)
935                         image = 1;
936
937                 if (image == WIM_NO_IMAGE && new_name) {
938                         imagex_error("Cannot specify new_name (`%s') when "
939                                      "using image 0", new_name);
940                         return -1;
941                 }
942
943                 if (boot) {
944                         if (image == wimlib_get_boot_idx(w)) {
945                                 printf("Image %d is already marked as "
946                                        "bootable.\n", image);
947                                 boot = false;
948                         } else {
949                                 printf("Marking image %d as bootable.\n",
950                                        image);
951                                 wimlib_set_boot_idx(w, image);
952                         }
953                 }
954                 if (new_name) {
955                         if (strcmp(wimlib_get_image_name(w, image), 
956                                                 new_name) == 0) {
957                                 printf("Image %d is already named \"%s\".\n",
958                                        image, new_name);
959                                 new_name = NULL;
960                         } else {
961                                 printf("Changing the name of image %d to "
962                                        "\"%s\".\n", image, new_name);
963                                 ret = wimlib_set_image_name(w, image, new_name);
964                                 if (ret != 0)
965                                         goto done;
966                         }
967                 }
968                 if (new_desc) {
969                         const char *old_desc;
970                         old_desc = wimlib_get_image_description(w, image);
971                         if (old_desc && strcmp(old_desc, new_desc) == 0) {
972                                 printf("The description of image %d is already "
973                                        "\"%s\".\n", image, new_desc);
974                                 new_desc = NULL;
975                         } else {
976                                 printf("Changing the description of image %d "
977                                        "to \"%s\".\n", image, new_desc);
978                                 ret = wimlib_set_image_descripton(w, image, 
979                                                                   new_desc);
980                                 if (ret != 0)
981                                         goto done;
982                         }
983                 }
984
985                 /* Only call wimlib_overwrite_xml_and_header() if something
986                  * actually needs to be changed. */
987                 if (boot || new_name || new_desc || 
988                                 check != wimlib_has_integrity_table(w)) {
989
990                         ret = wimlib_overwrite_xml_and_header(w, check ? 
991                                         WIMLIB_WRITE_FLAG_CHECK_INTEGRITY | 
992                                         WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
993                 } else {
994                         printf("The file `%s' was not modified because nothing "
995                                         "needed to be done.\n", wimfile);
996                         ret = 0;
997                 }
998         }
999
1000 done:
1001         wimlib_free(w);
1002         return ret;
1003 }
1004
1005 /* Join split WIMs into one part WIM */
1006 static int imagex_join(int argc, const char **argv)
1007 {
1008         int c;
1009         int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1010         const char *output_path;
1011
1012         for_opt(c, join_options) {
1013                 switch (c) {
1014                 case 'c':
1015                         flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1016                         break;
1017                 default:
1018                         goto err;
1019                 }
1020         }
1021         argc -= optind;
1022         argv += optind;
1023
1024         if (argc < 2) {
1025                 imagex_error("Must specify at least one split WIM (.swm) parts "
1026                              "to join");
1027                 goto err;
1028         }
1029         output_path = argv[0];
1030         return wimlib_join(++argv, --argc, output_path, flags);
1031 err:
1032         usage(JOIN);
1033         return -1;
1034 }
1035
1036 /* Mounts an image using a FUSE mount. */
1037 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1038 {
1039         int c;
1040         int mount_flags = 0;
1041         int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1042         const char *wimfile;
1043         const char *dir;
1044         WIMStruct *w;
1045         int image;
1046         int num_images;
1047         int ret;
1048
1049         if (strcmp(argv[0], "mountrw") == 0)
1050                 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1051         for_opt(c, mount_options) {
1052                 switch (c) {
1053                 case 'c':
1054                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1055                         break;
1056                 case 'd':
1057                         mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1058                         break;
1059                 case 's':
1060                         if (strcasecmp(optarg, "none") == 0)
1061                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1062                         else if (strcasecmp(optarg, "xattr") == 0)
1063                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1064                         else if (strcasecmp(optarg, "windows") == 0)
1065                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1066                         else {
1067                                 imagex_error("Unknown stream interface \"%s\"", optarg);
1068                                 goto mount_usage;
1069                         }
1070                         break;
1071                 default:
1072                         goto mount_usage;
1073                 }
1074         }
1075         argc -= optind;
1076         argv += optind;
1077         if (argc != 2 && argc != 3)
1078                 goto mount_usage;
1079
1080         wimfile = argv[0];
1081
1082         ret = wimlib_open_wim(wimfile, open_flags, &w);
1083         if (ret != 0)
1084                 return ret;
1085
1086         if (argc == 2) {
1087                 image = 1;
1088                 num_images = wimlib_get_num_images(w);
1089                 if (num_images != 1) {
1090                         imagex_error("The file `%s' contains %d images; Please "
1091                                      "select one", wimfile, num_images);
1092                         usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)  
1093                                         ? MOUNTRW : MOUNT);
1094                         ret = WIMLIB_ERR_INVALID_IMAGE;
1095                         goto done;
1096                 }
1097                 dir = argv[1];
1098         } else {
1099                 image = wimlib_resolve_image(w, argv[1]);
1100                 dir = argv[2];
1101         }
1102
1103         ret = verify_image_exists_and_is_single(image);
1104         if (ret != 0)
1105                 goto done;
1106
1107         ret = wimlib_mount(w, image, dir, mount_flags);
1108         if (ret != 0) {
1109                 imagex_error("Failed to mount image %d from `%s' on `%s'",
1110                              image, wimfile, dir);
1111
1112         }
1113 done:
1114         wimlib_free(w);
1115         return ret;
1116 mount_usage:
1117         usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)  
1118                         ? MOUNTRW : MOUNT);
1119         return -1;
1120 }
1121
1122 /* Split a WIM into a spanned set */
1123 static int imagex_split(int argc, const char **argv)
1124 {
1125         int c;
1126         int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1127         unsigned long part_size;
1128
1129         for_opt(c, split_options) {
1130                 switch (c) {
1131                 case 'c':
1132                         flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1133                         break;
1134                 default:
1135                         usage(SPLIT);
1136                         return -1;
1137                 }
1138         }
1139         argc -= optind;
1140         argv += optind;
1141
1142         if (argc != 3) {
1143                 usage(SPLIT);
1144                 return -1;
1145         }
1146         part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1147         return wimlib_split(argv[0], argv[1], part_size, flags);
1148 }
1149
1150 /* Unmounts an image. */
1151 static int imagex_unmount(int argc, const char **argv)
1152 {
1153         int c;
1154         int unmount_flags = 0;
1155         int ret;
1156
1157         for_opt(c, unmount_options) {
1158                 switch (c) {
1159                 case 'c':
1160                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1161                         break;
1162                 case 'C':
1163                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1164                         break;
1165                 default:
1166                         usage(UNMOUNT);
1167                         return -1;
1168                 }
1169         }
1170         argc -= optind;
1171         argv += optind;
1172         if (argc != 1) {
1173                 usage(UNMOUNT);
1174                 return -1;
1175         }
1176
1177         ret = wimlib_unmount(argv[0], unmount_flags);
1178         if (ret != 0)
1179                 imagex_error("Failed to unmount `%s'", argv[0]);
1180         return ret;
1181 }
1182
1183 struct imagex_command {
1184         const char *name;
1185         int (*func)(int , const char **);
1186         int cmd;
1187 };
1188
1189 static struct imagex_command imagex_commands[] = {
1190         {"append",  imagex_append,         APPEND},
1191         {"apply",   imagex_apply,          APPLY},
1192         {"capture", imagex_capture,        CAPTURE},
1193         {"delete",  imagex_delete,         DELETE},
1194         {"dir",     imagex_dir,            DIR},
1195         {"export",  imagex_export,         EXPORT},
1196         {"info",    imagex_info,           INFO},
1197         {"join",    imagex_join,           JOIN},
1198         {"mount",   imagex_mount_rw_or_ro, MOUNT},
1199         {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1200         {"split",   imagex_split,          SPLIT},
1201         {"unmount", imagex_unmount,        UNMOUNT},
1202 };
1203
1204 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1205                 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1206
1207 static void help_or_version(int argc, const char **argv)
1208 {
1209         int i;
1210         const char *p;
1211         struct imagex_command *cmd;
1212
1213         for (i = 1; i < argc; i++) {
1214                 p = argv[i];
1215                 if (*p == '-')
1216                         p++;
1217                 else
1218                         continue;
1219                 if (*p == '-')
1220                         p++;
1221                 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1222                         for_imagex_command(cmd) {
1223                                 if (strcmp(cmd->name, argv[1]) == 0) {
1224                                         usage(cmd->cmd);
1225                                         exit(0);
1226                                 }
1227                         }
1228                         usage_all();
1229                         exit(0);
1230                 }
1231                 if (strcmp(p, "version") == 0) {
1232                         version();
1233                         exit(0);
1234                 }
1235         }
1236 }
1237
1238
1239 int main(int argc, const char **argv)
1240 {
1241         struct imagex_command *cmd;
1242         int ret;
1243
1244         if (argc < 2) {
1245                 imagex_error("No command specified");
1246                 usage_all();
1247                 return 1;
1248         }
1249
1250         help_or_version(argc, argv);
1251         argc--;
1252         argv++;
1253
1254         wimlib_set_print_errors(true);
1255
1256         for_imagex_command(cmd) {
1257                 if (strcmp(cmd->name, *argv) == 0) {
1258                         ret = cmd->func(argc, argv);
1259                         if (ret > 0) {
1260                                 imagex_error("Exiting with error code %d:\n"
1261                                              "       %s.", ret,
1262                                              wimlib_get_error_string(ret));
1263                         }
1264                         return ret;
1265                 }
1266         }
1267
1268         imagex_error("Unrecognized command: `%s'", argv[0]);
1269         usage_all();
1270         return 1;
1271 }
1272 /*#ifndef WITH_NTFS_3G*/
1273                 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1274                 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1275                 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1276                 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1277                 /*return WIMLIB_ERR_UNSUPPORTED;*/
1278 /*#endif*/