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