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