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