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