Replace num_fast_bytes => nice_match_length
[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, 2013 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 #ifdef HAVE_CONFIG_H
26 #  include "config.h" /* Need for PACKAGE_VERSION, etc. */
27 #endif
28
29 #include "wimlib.h"
30 #include "wimlib_tchar.h"
31
32 #include <ctype.h>
33 #include <errno.h>
34
35 #include <inttypes.h>
36 #include <libgen.h>
37 #include <limits.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <locale.h>
44
45 #ifdef HAVE_ALLOCA_H
46 #  include <alloca.h>
47 #endif
48
49 #ifdef __WIN32__
50 #  include "imagex-win32.h"
51 #  define tbasename     win32_wbasename
52 #  define OS_PREFERRED_PATH_SEPARATOR L'\\'
53 #  define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
54 #else /* __WIN32__ */
55 #  include <glob.h>
56 #  include <getopt.h>
57 #  include <langinfo.h>
58 #  define tbasename     basename
59 #  define OS_PREFERRED_PATH_SEPARATOR '/'
60 #  define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
61 static inline void set_fd_to_binary_mode(int fd)
62 {
63 }
64 #endif /* !__WIN32 */
65
66 /* Don't confuse the user by presenting the mounting commands on Windows when
67  * they will never work.  However on UNIX-like systems we always present them,
68  * even if WITH_FUSE is not defined at this point, as to not tie the build of
69  * wimlib-imagex to a specific build of wimlib.  */
70 #ifdef __WIN32__
71 #  define WIM_MOUNTING_SUPPORTED 0
72 #else
73 #  define WIM_MOUNTING_SUPPORTED 1
74 #endif
75
76 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
77
78 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
79                                 opts, NULL)) != -1)
80
81 enum {
82         CMD_NONE = -1,
83         CMD_APPEND = 0,
84         CMD_APPLY,
85         CMD_CAPTURE,
86         CMD_DELETE,
87         CMD_DIR,
88         CMD_EXPORT,
89         CMD_EXTRACT,
90         CMD_INFO,
91         CMD_JOIN,
92 #if WIM_MOUNTING_SUPPORTED
93         CMD_MOUNT,
94         CMD_MOUNTRW,
95 #endif
96         CMD_OPTIMIZE,
97         CMD_SPLIT,
98 #if WIM_MOUNTING_SUPPORTED
99         CMD_UNMOUNT,
100 #endif
101         CMD_UPDATE,
102         CMD_MAX,
103 };
104
105 static void usage(int cmd, FILE *fp);
106 static void usage_all(FILE *fp);
107 static void recommend_man_page(int cmd, FILE *fp);
108 static const tchar *get_cmd_string(int cmd, bool nospace);
109
110 static int imagex_progress_func(enum wimlib_progress_msg msg,
111                                 const union wimlib_progress_info *info);
112
113 static bool imagex_be_quiet = false;
114 static FILE *imagex_info_file;
115
116 #define imagex_printf(format, ...) \
117                 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
118
119 enum {
120         IMAGEX_ALLOW_OTHER_OPTION,
121         IMAGEX_BOOT_OPTION,
122         IMAGEX_CHECK_OPTION,
123         IMAGEX_CHUNK_SIZE_OPTION,
124         IMAGEX_COMMAND_OPTION,
125         IMAGEX_COMMIT_OPTION,
126         IMAGEX_COMPRESS_OPTION,
127         IMAGEX_COMPRESS_SLOW_OPTION,
128         IMAGEX_CONFIG_OPTION,
129         IMAGEX_DEBUG_OPTION,
130         IMAGEX_DELTA_FROM_OPTION,
131         IMAGEX_DEREFERENCE_OPTION,
132         IMAGEX_DEST_DIR_OPTION,
133         IMAGEX_EXTRACT_XML_OPTION,
134         IMAGEX_FLAGS_OPTION,
135         IMAGEX_FORCE_OPTION,
136         IMAGEX_HARDLINK_OPTION,
137         IMAGEX_HEADER_OPTION,
138         IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
139         IMAGEX_LAZY_OPTION,
140         IMAGEX_LOOKUP_TABLE_OPTION,
141         IMAGEX_METADATA_OPTION,
142         IMAGEX_NORPFIX_OPTION,
143         IMAGEX_NOCHECK_OPTION,
144         IMAGEX_NO_ACLS_OPTION,
145         IMAGEX_NO_WILDCARDS_OPTION,
146         IMAGEX_NOT_PIPABLE_OPTION,
147         IMAGEX_PACK_STREAMS_OPTION,
148         IMAGEX_PATH_OPTION,
149         IMAGEX_PIPABLE_OPTION,
150         IMAGEX_REBUILD_OPTION,
151         IMAGEX_RECOMPRESS_OPTION,
152         IMAGEX_RECURSIVE_OPTION,
153         IMAGEX_REF_OPTION,
154         IMAGEX_RESUME_OPTION,
155         IMAGEX_RPFIX_OPTION,
156         IMAGEX_SOFT_OPTION,
157         IMAGEX_SOURCE_LIST_OPTION,
158         IMAGEX_STAGING_DIR_OPTION,
159         IMAGEX_STREAMS_INTERFACE_OPTION,
160         IMAGEX_STRICT_ACLS_OPTION,
161         IMAGEX_STRICT_WILDCARDS_OPTION,
162         IMAGEX_SYMLINK_OPTION,
163         IMAGEX_THREADS_OPTION,
164         IMAGEX_TO_STDOUT_OPTION,
165         IMAGEX_UNIX_DATA_OPTION,
166         IMAGEX_UPDATE_OF_OPTION,
167         IMAGEX_VERBOSE_OPTION,
168         IMAGEX_XML_OPTION,
169 };
170
171 static const struct option apply_options[] = {
172         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
173         {T("hardlink"),    no_argument,       NULL, IMAGEX_HARDLINK_OPTION},
174         {T("symlink"),     no_argument,       NULL, IMAGEX_SYMLINK_OPTION},
175         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
176         {T("ref"),         required_argument, NULL, IMAGEX_REF_OPTION},
177         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
178         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
179         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
180         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
181         {T("rpfix"),       no_argument,       NULL, IMAGEX_RPFIX_OPTION},
182         {T("norpfix"),     no_argument,       NULL, IMAGEX_NORPFIX_OPTION},
183         {T("include-invalid-names"), no_argument,       NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
184
185         /* --resume is undocumented for now as it needs improvement.  */
186         {T("resume"),      no_argument,       NULL, IMAGEX_RESUME_OPTION},
187         {NULL, 0, NULL, 0},
188 };
189
190 static const struct option capture_or_append_options[] = {
191         {T("boot"),        no_argument,       NULL, IMAGEX_BOOT_OPTION},
192         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
193         {T("no-check"),    no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
194         {T("nocheck"),     no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
195         {T("compress"),    required_argument, NULL, IMAGEX_COMPRESS_OPTION},
196         {T("compress-slow"), no_argument,     NULL, IMAGEX_COMPRESS_SLOW_OPTION},
197         {T("chunk-size"),  required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
198         {T("pack-streams"), no_argument,      NULL, IMAGEX_PACK_STREAMS_OPTION},
199         {T("config"),      required_argument, NULL, IMAGEX_CONFIG_OPTION},
200         {T("dereference"), no_argument,       NULL, IMAGEX_DEREFERENCE_OPTION},
201         {T("flags"),       required_argument, NULL, IMAGEX_FLAGS_OPTION},
202         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
203         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
204         {T("rebuild"),     no_argument,       NULL, IMAGEX_REBUILD_OPTION},
205         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
206         {T("source-list"), no_argument,       NULL, IMAGEX_SOURCE_LIST_OPTION},
207         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
208         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
209         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
210         {T("rpfix"),       no_argument,       NULL, IMAGEX_RPFIX_OPTION},
211         {T("norpfix"),     no_argument,       NULL, IMAGEX_NORPFIX_OPTION},
212         {T("pipable"),     no_argument,       NULL, IMAGEX_PIPABLE_OPTION},
213         {T("not-pipable"), no_argument,       NULL, IMAGEX_NOT_PIPABLE_OPTION},
214         {T("update-of"),   required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
215         {T("delta-from"),  required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
216         {NULL, 0, NULL, 0},
217 };
218
219 static const struct option delete_options[] = {
220         {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
221         {T("soft"),  no_argument, NULL, IMAGEX_SOFT_OPTION},
222         {NULL, 0, NULL, 0},
223 };
224
225 static const struct option dir_options[] = {
226         {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
227         {NULL, 0, NULL, 0},
228 };
229
230 static const struct option export_options[] = {
231         {T("boot"),        no_argument,       NULL, IMAGEX_BOOT_OPTION},
232         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
233         {T("nocheck"),     no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
234         {T("no-check"),    no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
235         {T("compress"),    required_argument, NULL, IMAGEX_COMPRESS_OPTION},
236         {T("pack-streams"),no_argument,       NULL, IMAGEX_PACK_STREAMS_OPTION},
237         {T("chunk-size"),  required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
238         {T("ref"),         required_argument, NULL, IMAGEX_REF_OPTION},
239         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
240         {T("rebuild"),     no_argument,       NULL, IMAGEX_REBUILD_OPTION},
241         {T("pipable"),     no_argument,       NULL, IMAGEX_PIPABLE_OPTION},
242         {T("not-pipable"), no_argument,       NULL, IMAGEX_NOT_PIPABLE_OPTION},
243         {NULL, 0, NULL, 0},
244 };
245
246 static const struct option extract_options[] = {
247         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
248         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
249         {T("ref"),         required_argument, NULL, IMAGEX_REF_OPTION},
250         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
251         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
252         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
253         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
254         {T("dest-dir"),    required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
255         {T("to-stdout"),   no_argument,       NULL, IMAGEX_TO_STDOUT_OPTION},
256         {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
257         {T("strict-wildcards"), no_argument,  NULL, IMAGEX_STRICT_WILDCARDS_OPTION},
258         {T("no-wildcards"), no_argument,      NULL, IMAGEX_NO_WILDCARDS_OPTION},
259         {NULL, 0, NULL, 0},
260 };
261
262 static const struct option info_options[] = {
263         {T("boot"),         no_argument,       NULL, IMAGEX_BOOT_OPTION},
264         {T("check"),        no_argument,       NULL, IMAGEX_CHECK_OPTION},
265         {T("nocheck"),      no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
266         {T("no-check"),     no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
267         {T("extract-xml"),  required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
268         {T("header"),       no_argument,       NULL, IMAGEX_HEADER_OPTION},
269         {T("lookup-table"), no_argument,       NULL, IMAGEX_LOOKUP_TABLE_OPTION},
270         {T("metadata"),     no_argument,       NULL, IMAGEX_METADATA_OPTION},
271         {T("xml"),          no_argument,       NULL, IMAGEX_XML_OPTION},
272         {NULL, 0, NULL, 0},
273 };
274
275 static const struct option join_options[] = {
276         {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
277         {NULL, 0, NULL, 0},
278 };
279
280 static const struct option mount_options[] = {
281         {T("check"),             no_argument,       NULL, IMAGEX_CHECK_OPTION},
282         {T("debug"),             no_argument,       NULL, IMAGEX_DEBUG_OPTION},
283         {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
284         {T("ref"),               required_argument, NULL, IMAGEX_REF_OPTION},
285         {T("staging-dir"),       required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
286         {T("unix-data"),         no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
287         {T("allow-other"),       no_argument,       NULL, IMAGEX_ALLOW_OTHER_OPTION},
288         {NULL, 0, NULL, 0},
289 };
290
291 static const struct option optimize_options[] = {
292         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
293         {T("nocheck"),     no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
294         {T("no-check"),    no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
295         {T("compress"),    required_argument, NULL, IMAGEX_COMPRESS_OPTION},
296         {T("recompress"),  no_argument,       NULL, IMAGEX_RECOMPRESS_OPTION},
297         {T("compress-slow"), no_argument,     NULL, IMAGEX_COMPRESS_SLOW_OPTION},
298         {T("recompress-slow"), no_argument,     NULL, IMAGEX_COMPRESS_SLOW_OPTION},
299         {T("chunk-size"),  required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
300         {T("pack-streams"),no_argument,       NULL, IMAGEX_PACK_STREAMS_OPTION},
301         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
302         {T("pipable"),     no_argument,       NULL, IMAGEX_PIPABLE_OPTION},
303         {T("not-pipable"), no_argument,       NULL, IMAGEX_NOT_PIPABLE_OPTION},
304         {NULL, 0, NULL, 0},
305 };
306
307 static const struct option split_options[] = {
308         {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
309         {NULL, 0, NULL, 0},
310 };
311
312 static const struct option unmount_options[] = {
313         {T("commit"),  no_argument, NULL, IMAGEX_COMMIT_OPTION},
314         {T("check"),   no_argument, NULL, IMAGEX_CHECK_OPTION},
315         {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
316         {T("lazy"),    no_argument, NULL, IMAGEX_LAZY_OPTION},
317         {NULL, 0, NULL, 0},
318 };
319
320 static const struct option update_options[] = {
321         /* Careful: some of the options here set the defaults for update
322          * commands, but the flags given to an actual update command (and not to
323          * `imagex update' itself are also handled in
324          * update_command_add_option().  */
325         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
326         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
327         {T("rebuild"),     no_argument,       NULL, IMAGEX_REBUILD_OPTION},
328         {T("command"),     required_argument, NULL, IMAGEX_COMMAND_OPTION},
329
330         /* Default delete options */
331         {T("force"),       no_argument,       NULL, IMAGEX_FORCE_OPTION},
332         {T("recursive"),   no_argument,       NULL, IMAGEX_RECURSIVE_OPTION},
333
334         /* Global add option */
335         {T("config"),      required_argument, NULL, IMAGEX_CONFIG_OPTION},
336
337         /* Default add options */
338         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
339         {T("dereference"), no_argument,       NULL, IMAGEX_DEREFERENCE_OPTION},
340         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
341         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
342         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
343         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
344
345         {NULL, 0, NULL, 0},
346 };
347
348 #if 0
349 #       define _format_attribute(type, format_str, args_start) \
350                         __attribute__((format(type, format_str, args_start)))
351 #else
352 #       define _format_attribute(type, format_str, args_start)
353 #endif
354
355 /* Print formatted error message to stderr. */
356 static void _format_attribute(printf, 1, 2)
357 imagex_error(const tchar *format, ...)
358 {
359         va_list va;
360         va_start(va, format);
361         tfputs(T("ERROR: "), stderr);
362         tvfprintf(stderr, format, va);
363         tputc(T('\n'), stderr);
364         va_end(va);
365 }
366
367 /* Print formatted error message to stderr. */
368 static void _format_attribute(printf, 1, 2)
369 imagex_error_with_errno(const tchar *format, ...)
370 {
371         int errno_save = errno;
372         va_list va;
373         va_start(va, format);
374         tfputs(T("ERROR: "), stderr);
375         tvfprintf(stderr, format, va);
376         tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
377         va_end(va);
378 }
379
380 static int
381 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
382 {
383         if (image == WIMLIB_NO_IMAGE) {
384                 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
385                              "       Please specify a 1-based image index or "
386                              "image name.  To list the images\n"
387                              "       contained in the WIM archive, run\n"
388                              "\n"
389                              "           %"TS" \"%"TS"\"\n"),
390                              image_name, wim_name,
391                              get_cmd_string(CMD_INFO, false), wim_name);
392                 return WIMLIB_ERR_INVALID_IMAGE;
393         }
394         return 0;
395 }
396
397 static int
398 verify_image_is_single(int image)
399 {
400         if (image == WIMLIB_ALL_IMAGES) {
401                 imagex_error(T("Cannot specify all images for this action!"));
402                 return WIMLIB_ERR_INVALID_IMAGE;
403         }
404         return 0;
405 }
406
407 static int
408 verify_image_exists_and_is_single(int image, const tchar *image_name,
409                                   const tchar *wim_name)
410 {
411         int ret;
412         ret = verify_image_exists(image, image_name, wim_name);
413         if (ret == 0)
414                 ret = verify_image_is_single(image);
415         return ret;
416 }
417
418 /* Parse the argument to --compress */
419 static int
420 get_compression_type(const tchar *optarg)
421 {
422         if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
423                 return WIMLIB_COMPRESSION_TYPE_LZX;
424         else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
425                 return WIMLIB_COMPRESSION_TYPE_XPRESS;
426         else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
427                 return WIMLIB_COMPRESSION_TYPE_LZMS;
428         else if (!tstrcasecmp(optarg, T("none")))
429                 return WIMLIB_COMPRESSION_TYPE_NONE;
430         else {
431                 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
432                              "\"maximum\", \"fast\", or \"none\"."), optarg);
433                 return WIMLIB_COMPRESSION_TYPE_INVALID;
434         }
435 }
436
437 static int
438 set_compress_slow(void)
439 {
440         int ret;
441         static const struct wimlib_lzx_compressor_params slow_params = {
442                 .hdr = {
443                         .size = sizeof(struct wimlib_lzx_compressor_params),
444                 },
445                 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
446                 .alg_params = {
447                         .slow = {
448                                 .use_len2_matches = 1,
449                                 .nice_match_length = 96,
450                                 .num_optim_passes = 4,
451                                 .max_search_depth = 100,
452                                 .max_matches_per_pos = 10,
453                                 .main_nostat_cost = 15,
454                                 .len_nostat_cost = 15,
455                                 .aligned_nostat_cost = 7,
456                         },
457                 },
458         };
459         ret = wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
460                                                    &slow_params.hdr);
461         if (ret)
462                 imagex_error(T("Couldn't set slow compression parameters.!"));
463         return ret;
464 }
465
466 struct string_set {
467         const tchar **strings;
468         unsigned num_strings;
469         unsigned num_alloc_strings;
470 };
471
472 #define STRING_SET_INITIALIZER \
473         { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
474
475 #define STRING_SET(_strings) \
476         struct string_set _strings = STRING_SET_INITIALIZER
477
478 static int
479 string_set_append(struct string_set *set, const tchar *glob)
480 {
481         unsigned num_alloc_strings = set->num_alloc_strings;
482
483         if (set->num_strings == num_alloc_strings) {
484                 const tchar **new_strings;
485
486                 num_alloc_strings += 4;
487                 new_strings = realloc(set->strings,
488                                       sizeof(set->strings[0]) * num_alloc_strings);
489                 if (!new_strings) {
490                         imagex_error(T("Out of memory!"));
491                         return -1;
492                 }
493                 set->strings = new_strings;
494                 set->num_alloc_strings = num_alloc_strings;
495         }
496         set->strings[set->num_strings++] = glob;
497         return 0;
498 }
499
500 static void
501 string_set_destroy(struct string_set *set)
502 {
503         free(set->strings);
504 }
505
506 static int
507 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
508 {
509         return wimlib_reference_resource_files(wim, set->strings,
510                                                set->num_strings,
511                                                WIMLIB_REF_FLAG_GLOB_ENABLE,
512                                                open_flags,
513                                                imagex_progress_func);
514 }
515
516 static void
517 do_resource_not_found_warning(const tchar *wimfile,
518                               const struct wimlib_wim_info *info,
519                               const struct string_set *refglobs)
520 {
521         if (info->total_parts > 1) {
522                 if (refglobs->num_strings == 0) {
523                         imagex_error(T("\"%"TS"\" is part of a split WIM. "
524                                        "Use --ref to specify the other parts."),
525                                      wimfile);
526                 } else {
527                         imagex_error(T("Perhaps the '--ref' argument did not "
528                                        "specify all other parts of the split "
529                                        "WIM?"));
530                 }
531         } else {
532                 imagex_error(T("If this is a delta WIM, use the --ref argument "
533                                "to specify the WIM(s) on which it is based."));
534         }
535 }
536
537 /* Returns the size of a file given its name, or -1 if the file does not exist
538  * or its size cannot be determined.  */
539 static off_t
540 file_get_size(const tchar *filename)
541 {
542         struct stat st;
543         if (tstat(filename, &st) == 0)
544                 return st.st_size;
545         else
546                 return (off_t)-1;
547 }
548
549 enum {
550         PARSE_STRING_SUCCESS = 0,
551         PARSE_STRING_FAILURE = 1,
552         PARSE_STRING_NONE = 2,
553 };
554
555 /*
556  * Parses a string token from an array of characters.
557  *
558  * Tokens are either whitespace-delimited, or double or single-quoted.
559  *
560  * @line_p:  Pointer to the pointer to the line of data.  Will be updated
561  *           to point past the string token iff the return value is
562  *           PARSE_STRING_SUCCESS.  If *len_p > 0, (*line_p)[*len_p - 1] must
563  *           be '\0'.
564  *
565  * @len_p:   @len_p initially stores the length of the line of data, which may
566  *           be 0, and it will be updated to the number of bytes remaining in
567  *           the line iff the return value is PARSE_STRING_SUCCESS.
568  *
569  * @fn_ret:  Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
570  *           parsed string token will be returned here.
571  *
572  * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
573  *          PARSE_STRING_FAILURE if the data was invalid due to a missing
574  *          closing quote; or PARSE_STRING_NONE if the line ended before the
575  *          beginning of a string token was found.
576  */
577 static int
578 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
579 {
580         size_t len = *len_p;
581         tchar *line = *line_p;
582         tchar *fn;
583         tchar quote_char;
584
585         /* Skip leading whitespace */
586         for (;;) {
587                 if (len == 0)
588                         return PARSE_STRING_NONE;
589                 if (!istspace(*line) && *line != T('\0'))
590                         break;
591                 line++;
592                 len--;
593         }
594         quote_char = *line;
595         if (quote_char == T('"') || quote_char == T('\'')) {
596                 /* Quoted string */
597                 line++;
598                 len--;
599                 fn = line;
600                 line = tmemchr(line, quote_char, len);
601                 if (!line) {
602                         imagex_error(T("Missing closing quote: %"TS), fn - 1);
603                         return PARSE_STRING_FAILURE;
604                 }
605         } else {
606                 /* Unquoted string.  Go until whitespace.  Line is terminated
607                  * by '\0', so no need to check 'len'. */
608                 fn = line;
609                 do {
610                         line++;
611                 } while (!istspace(*line) && *line != T('\0'));
612         }
613         *line = T('\0');
614         len -= line - fn;
615         *len_p = len;
616         *line_p = line;
617         *fn_ret = fn;
618         return PARSE_STRING_SUCCESS;
619 }
620
621 /* Parses a line of data (not an empty line or comment) in the source list file
622  * format.  (See the man page for 'wimlib-imagex capture' for details on this
623  * format and the meaning.)
624  *
625  * @line:  Line of data to be parsed.  line[len - 1] must be '\0', unless
626  *         len == 0.  The data in @line will be modified by this function call.
627  *
628  * @len:   Length of the line of data.
629  *
630  * @source:  On success, the capture source and target described by the line is
631  *           written into this destination.  Note that it will contain pointers
632  *           to data in the @line array.
633  *
634  * Returns true if the line was valid; false otherwise.  */
635 static bool
636 parse_source_list_line(tchar *line, size_t len,
637                        struct wimlib_capture_source *source)
638 {
639         /* SOURCE [DEST] */
640         int ret;
641         ret = parse_string(&line, &len, &source->fs_source_path);
642         if (ret != PARSE_STRING_SUCCESS)
643                 return false;
644         ret = parse_string(&line, &len, &source->wim_target_path);
645         if (ret == PARSE_STRING_NONE)
646                 source->wim_target_path = source->fs_source_path;
647         return ret != PARSE_STRING_FAILURE;
648 }
649
650 /* Returns %true if the given line of length @len > 0 is a comment or empty line
651  * in the source list file format. */
652 static bool
653 is_comment_line(const tchar *line, size_t len)
654 {
655         for (;;) {
656                 if (*line == T('#'))
657                         return true;
658                 if (!istspace(*line) && *line != T('\0'))
659                         return false;
660                 ++line;
661                 --len;
662                 if (len == 0)
663                         return true;
664         }
665 }
666
667 static ssize_t
668 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
669 {
670         ssize_t nlines = 0;
671         tchar *contents = *contents_p;
672         size_t nchars = *nchars_p;
673         size_t i;
674
675         for (i = 0; i < nchars; i++)
676                 if (contents[i] == T('\n'))
677                         nlines++;
678
679         /* Handle last line not terminated by a newline */
680         if (nchars != 0 && contents[nchars - 1] != T('\n')) {
681                 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
682                 if (!contents) {
683                         imagex_error(T("Out of memory!"));
684                         return -1;
685                 }
686                 contents[nchars] = T('\n');
687                 *contents_p = contents;
688                 nchars++;
689                 nlines++;
690         }
691         *nchars_p = nchars;
692         return nlines;
693 }
694
695 /* Parses a file in the source list format.  (See the man page for
696  * 'wimlib-imagex capture' for details on this format and the meaning.)
697  *
698  * @source_list_contents:  Contents of the source list file.  Note that this
699  *                         buffer will be modified to save memory allocations,
700  *                         and cannot be freed until the returned array of
701  *                         wimlib_capture_source's has also been freed.
702  *
703  * @source_list_nbytes:    Number of bytes of data in the @source_list_contents
704  *                         buffer.
705  *
706  * @nsources_ret:          On success, the length of the returned array is
707  *                         returned here.
708  *
709  * Returns:   An array of `struct wimlib_capture_source's that can be passed to
710  * the wimlib_add_image_multisource() function to specify how a WIM image is to
711  * be created.  */
712 static struct wimlib_capture_source *
713 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
714                   size_t *nsources_ret)
715 {
716         ssize_t nlines;
717         tchar *p;
718         struct wimlib_capture_source *sources;
719         size_t i, j;
720
721         nlines = text_file_count_lines(source_list_contents_p,
722                                        &source_list_nchars);
723         if (nlines < 0)
724                 return NULL;
725
726         /* Always allocate at least 1 slot, just in case the implementation of
727          * calloc() returns NULL if 0 bytes are requested. */
728         sources = calloc(nlines ?: 1, sizeof(*sources));
729         if (!sources) {
730                 imagex_error(T("out of memory"));
731                 return NULL;
732         }
733         p = *source_list_contents_p;
734         j = 0;
735         for (i = 0; i < nlines; i++) {
736                 /* XXX: Could use rawmemchr() here instead, but it may not be
737                  * available on all platforms. */
738                 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
739                 size_t len = endp - p + 1;
740                 *endp = T('\0');
741                 if (!is_comment_line(p, len)) {
742                         if (!parse_source_list_line(p, len, &sources[j++])) {
743                                 free(sources);
744                                 return NULL;
745                         }
746                 }
747                 p = endp + 1;
748
749         }
750         *nsources_ret = j;
751         return sources;
752 }
753
754
755 enum capture_config_section {
756         CAPTURE_CONFIG_NO_SECTION,
757         CAPTURE_CONFIG_EXCLUSION_SECTION,
758         CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
759         CAPTURE_CONFIG_IGNORE_SECTION,
760 };
761
762 enum {
763         CAPTURE_CONFIG_INVALID_SECTION,
764         CAPTURE_CONFIG_CHANGED_SECTION,
765         CAPTURE_CONFIG_SAME_SECTION,
766 };
767
768 static int
769 check_config_section(tchar *line, size_t len,
770                      enum capture_config_section *cur_section)
771 {
772         while (istspace(*line))
773                 line++;
774
775         if (*line != T('['))
776                 return CAPTURE_CONFIG_SAME_SECTION;
777
778         line++;
779         tchar *endbrace = tstrrchr(line, T(']'));
780         if (!endbrace)
781                 return CAPTURE_CONFIG_SAME_SECTION;
782
783         if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
784                 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
785         } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
786                 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
787         } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
788                 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
789                 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
790                          "of capture config file\n"),
791                        stderr);
792         } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
793                 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
794                 tfputs(T("WARNING: Ignoring [AlignmentList] section "
795                          "of capture config file\n"),
796                        stderr);
797         } else {
798                 imagex_error(T("Invalid capture config file section \"%"TS"\""),
799                              line - 1);
800                 return CAPTURE_CONFIG_INVALID_SECTION;
801         }
802         return CAPTURE_CONFIG_CHANGED_SECTION;
803 }
804
805
806 static bool
807 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
808                          tchar *pat)
809 {
810         if (pat_list->num_pats == pat_list->num_allocated_pats) {
811                 tchar **pats;
812                 size_t num_allocated_pats = pat_list->num_pats + 8;
813
814                 pats = realloc(pat_list->pats,
815                                num_allocated_pats * sizeof(pat_list->pats[0]));
816                 if (!pats) {
817                         imagex_error(T("Out of memory!"));
818                         return false;
819                 }
820                 pat_list->pats = pats;
821                 pat_list->num_allocated_pats = num_allocated_pats;
822         }
823         pat_list->pats[pat_list->num_pats++] = pat;
824         return true;
825 }
826
827 static bool
828 parse_capture_config_line(tchar *line, size_t len,
829                           enum capture_config_section *cur_section,
830                           struct wimlib_capture_config *config)
831 {
832         tchar *filename;
833         int ret;
834
835         ret = check_config_section(line, len, cur_section);
836         if (ret == CAPTURE_CONFIG_INVALID_SECTION)
837                 return false;
838         if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
839                 return true;
840
841         switch (*cur_section) {
842         case CAPTURE_CONFIG_NO_SECTION:
843                 imagex_error(T("Line \"%"TS"\" is not in a section "
844                                "(such as [ExclusionList]"), line);
845                 return false;
846         case CAPTURE_CONFIG_EXCLUSION_SECTION:
847                 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
848                         return false;
849                 return pattern_list_add_pattern(&config->exclusion_pats,
850                                                 filename);
851         case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
852                 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
853                         return false;
854                 return pattern_list_add_pattern(&config->exclusion_exception_pats,
855                                                 filename);
856         case CAPTURE_CONFIG_IGNORE_SECTION:
857                 return true;
858         }
859         return false;
860 }
861
862 static int
863 parse_capture_config(tchar **contents_p, size_t nchars,
864                      struct wimlib_capture_config *config)
865 {
866         ssize_t nlines;
867         tchar *p;
868         size_t i;
869         enum capture_config_section cur_section;
870
871         memset(config, 0, sizeof(*config));
872
873         nlines = text_file_count_lines(contents_p, &nchars);
874         if (nlines < 0)
875                 return -1;
876
877         cur_section = CAPTURE_CONFIG_NO_SECTION;
878         p = *contents_p;
879         for (i = 0; i < nlines; i++) {
880                 tchar *endp = tmemchr(p, T('\n'), nchars);
881                 size_t len = endp - p + 1;
882                 *endp = T('\0');
883                 if (!is_comment_line(p, len))
884                         if (!parse_capture_config_line(p, len, &cur_section, config))
885                                 return -1;
886                 p = endp + 1;
887
888         }
889         return 0;
890 }
891
892 /* Reads the contents of a file into memory. */
893 static char *
894 file_get_contents(const tchar *filename, size_t *len_ret)
895 {
896         struct stat stbuf;
897         void *buf = NULL;
898         size_t len;
899         FILE *fp;
900
901         if (tstat(filename, &stbuf) != 0) {
902                 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
903                 goto out;
904         }
905         len = stbuf.st_size;
906
907         fp = tfopen(filename, T("rb"));
908         if (!fp) {
909                 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
910                 goto out;
911         }
912
913         buf = malloc(len ? len : 1);
914         if (!buf) {
915                 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
916                                "contents of file \"%"TS"\""), len, filename);
917                 goto out_fclose;
918         }
919         if (fread(buf, 1, len, fp) != len) {
920                 imagex_error_with_errno(T("Failed to read %zu bytes from the "
921                                           "file \"%"TS"\""), len, filename);
922                 goto out_free_buf;
923         }
924         *len_ret = len;
925         goto out_fclose;
926 out_free_buf:
927         free(buf);
928         buf = NULL;
929 out_fclose:
930         fclose(fp);
931 out:
932         return buf;
933 }
934
935 /* Read standard input until EOF and return the full contents in a malloc()ed
936  * buffer and the number of bytes of data in @len_ret.  Returns NULL on read
937  * error. */
938 static char *
939 stdin_get_contents(size_t *len_ret)
940 {
941         /* stdin can, of course, be a pipe or other non-seekable file, so the
942          * total length of the data cannot be pre-determined */
943         char *buf = NULL;
944         size_t newlen = 1024;
945         size_t pos = 0;
946         size_t inc = 1024;
947         for (;;) {
948                 char *p = realloc(buf, newlen);
949                 size_t bytes_read, bytes_to_read;
950                 if (!p) {
951                         imagex_error(T("out of memory while reading stdin"));
952                         break;
953                 }
954                 buf = p;
955                 bytes_to_read = newlen - pos;
956                 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
957                 pos += bytes_read;
958                 if (bytes_read != bytes_to_read) {
959                         if (feof(stdin)) {
960                                 *len_ret = pos;
961                                 return buf;
962                         } else {
963                                 imagex_error_with_errno(T("error reading stdin"));
964                                 break;
965                         }
966                 }
967                 newlen += inc;
968                 inc *= 3;
969                 inc /= 2;
970         }
971         free(buf);
972         return NULL;
973 }
974
975
976 static tchar *
977 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
978 {
979 #ifndef __WIN32__
980         /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
981          * */
982         *num_tchars_ret = num_bytes;
983         return text;
984 #else /* !__WIN32__ */
985         /* On Windows, translate the text to UTF-16LE */
986         wchar_t *text_wstr;
987         size_t num_wchars;
988
989         if (num_bytes >= 2 &&
990             (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
991              ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
992         {
993                 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
994                  * with something that looks like an ASCII character encoded as
995                  * a UTF-16LE code unit.  Assume the file is encoded as
996                  * UTF-16LE.  This is not a 100% reliable check. */
997                 num_wchars = num_bytes / 2;
998                 text_wstr = (wchar_t*)text;
999         } else {
1000                 /* File does not look like UTF-16LE.  Assume it is encoded in
1001                  * the current Windows code page.  I think these are always
1002                  * ASCII-compatible, so any so-called "plain-text" (ASCII) files
1003                  * should work as expected. */
1004                 text_wstr = win32_mbs_to_wcs(text,
1005                                              num_bytes,
1006                                              &num_wchars);
1007                 free(text);
1008         }
1009         *num_tchars_ret = num_wchars;
1010         return text_wstr;
1011 #endif /* __WIN32__ */
1012 }
1013
1014 static tchar *
1015 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1016 {
1017         char *contents;
1018         size_t num_bytes;
1019
1020         contents = file_get_contents(filename, &num_bytes);
1021         if (!contents)
1022                 return NULL;
1023         return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1024 }
1025
1026 static tchar *
1027 stdin_get_text_contents(size_t *num_tchars_ret)
1028 {
1029         char *contents;
1030         size_t num_bytes;
1031
1032         contents = stdin_get_contents(&num_bytes);
1033         if (!contents)
1034                 return NULL;
1035         return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1036 }
1037
1038 #define TO_PERCENT(numerator, denominator) \
1039         (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1040
1041 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1042 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1043 #define KIBIBYTE_MIN_NBYTES 10000ULL
1044
1045 static unsigned
1046 get_unit(uint64_t total_bytes, const tchar **name_ret)
1047 {
1048         if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1049                 *name_ret = T("GiB");
1050                 return 30;
1051         } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1052                 *name_ret = T("MiB");
1053                 return 20;
1054         } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1055                 *name_ret = T("KiB");
1056                 return 10;
1057         } else {
1058                 *name_ret = T("bytes");
1059                 return 0;
1060         }
1061 }
1062
1063 static struct wimlib_progress_info_scan last_scan_progress;
1064
1065 static void
1066 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1067 {
1068         uint64_t prev_count, cur_count;
1069
1070         prev_count = last_scan_progress.num_nondirs_scanned +
1071                      last_scan_progress.num_dirs_scanned;
1072         cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1073
1074         if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1075             cur_count % 128 == 0)
1076         {
1077                 unsigned unit_shift;
1078                 const tchar *unit_name;
1079
1080                 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1081                 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1082                                 "%"PRIu64" directories)    "),
1083                               scan->num_bytes_scanned >> unit_shift,
1084                               unit_name,
1085                               scan->num_nondirs_scanned,
1086                               scan->num_dirs_scanned);
1087                 last_scan_progress = *scan;
1088         }
1089 }
1090
1091 /* Progress callback function passed to various wimlib functions. */
1092 static int
1093 imagex_progress_func(enum wimlib_progress_msg msg,
1094                      const union wimlib_progress_info *info)
1095 {
1096         unsigned percent_done;
1097         unsigned unit_shift;
1098         const tchar *unit_name;
1099
1100         if (imagex_be_quiet)
1101                 return 0;
1102         switch (msg) {
1103         case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1104                 {
1105                         static bool first = true;
1106                         if (first) {
1107                                 imagex_printf(T("Writing %"TS"-compressed data "
1108                                                 "using %u thread%"TS"\n"),
1109                                               wimlib_get_compression_type_string(
1110                                                         info->write_streams.compression_type),
1111                                         info->write_streams.num_threads,
1112                                         (info->write_streams.num_threads == 1) ? T("") : T("s"));
1113                                 first = false;
1114                         }
1115                 }
1116                 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1117                 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1118                                           info->write_streams.total_bytes);
1119
1120                 if (info->write_streams.total_parts <= 1) {
1121                         imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1122                                 "written (%u%% done)"),
1123                                 info->write_streams.completed_bytes >> unit_shift,
1124                                 unit_name,
1125                                 info->write_streams.total_bytes >> unit_shift,
1126                                 unit_name,
1127                                 percent_done);
1128                 } else {
1129                         imagex_printf(T("\rWriting resources from part %u of %u: "
1130                                   "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1131                                 (info->write_streams.completed_parts ==
1132                                         info->write_streams.total_parts) ?
1133                                                 info->write_streams.completed_parts :
1134                                                 info->write_streams.completed_parts + 1,
1135                                 info->write_streams.total_parts,
1136                                 info->write_streams.completed_bytes >> unit_shift,
1137                                 unit_name,
1138                                 info->write_streams.total_bytes >> unit_shift,
1139                                 unit_name,
1140                                 percent_done);
1141                 }
1142                 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1143                         imagex_printf(T("\n"));
1144                 break;
1145         case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1146                 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1147                 if (*info->scan.wim_target_path) {
1148                         imagex_printf(T(" (loading as WIM path: "
1149                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1150                                info->scan.wim_target_path);
1151                 } else {
1152                         imagex_printf(T("\n"));
1153                 }
1154                 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1155                 break;
1156         case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1157                 switch (info->scan.status) {
1158                 case WIMLIB_SCAN_DENTRY_OK:
1159                         report_scan_progress(&info->scan, false);
1160                         break;
1161                 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1162                         imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1163                         break;
1164                 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1165                         imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1166                                         "         \"%"TS"\" from capture\n"), info->scan.cur_path);
1167                         break;
1168                 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1169                         imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1170                                         "with out-of-tree target:\n"
1171                                         "           \"%"TS"\" => \"%"TS"\"\n"
1172                                         "           (Use --norpfix to capture "
1173                                         "absolute symbolic links as-is)\n"),
1174                                         info->scan.cur_path, info->scan.symlink_target);
1175                         break;
1176                 }
1177                 break;
1178         case WIMLIB_PROGRESS_MSG_SCAN_END:
1179                 report_scan_progress(&info->scan, true);
1180                 imagex_printf(T("\n"));
1181                 break;
1182         case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1183                 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1184                 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1185                                           info->integrity.total_bytes);
1186                 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1187                         "of %"PRIu64" %"TS" (%u%%) done"),
1188                         info->integrity.filename,
1189                         info->integrity.completed_bytes >> unit_shift,
1190                         unit_name,
1191                         info->integrity.total_bytes >> unit_shift,
1192                         unit_name,
1193                         percent_done);
1194                 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1195                         imagex_printf(T("\n"));
1196                 break;
1197         case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1198                 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1199                 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1200                                           info->integrity.total_bytes);
1201                 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1202                           "of %"PRIu64" %"TS" (%u%%) done"),
1203                         info->integrity.completed_bytes >> unit_shift,
1204                         unit_name,
1205                         info->integrity.total_bytes >> unit_shift,
1206                         unit_name,
1207                         percent_done);
1208                 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1209                         imagex_printf(T("\n"));
1210                 break;
1211         case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1212                 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1213                           "to %"TS" \"%"TS"\"\n"),
1214                         info->extract.image,
1215                         info->extract.image_name,
1216                         info->extract.wimfile_name,
1217                         ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1218                          T("NTFS volume") : T("directory")),
1219                         info->extract.target);
1220                 break;
1221         case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1222                 if (info->extract.extract_root_wim_source_path[0] != T('\0')) {
1223                         imagex_printf(T("Extracting "
1224                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" "
1225                                   "from image %d (\"%"TS"\") "
1226                                   "in \"%"TS"\" to \"%"TS"\"\n"),
1227                                 info->extract.extract_root_wim_source_path,
1228                                 info->extract.image,
1229                                 info->extract.image_name,
1230                                 info->extract.wimfile_name,
1231                                 info->extract.target);
1232                 }
1233                 break;
1234         case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1235                 percent_done = TO_PERCENT(info->extract.completed_bytes,
1236                                           info->extract.total_bytes);
1237                 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1238                 imagex_printf(T("\rExtracting files: "
1239                           "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1240                         info->extract.completed_bytes >> unit_shift,
1241                         unit_name,
1242                         info->extract.total_bytes >> unit_shift,
1243                         unit_name,
1244                         percent_done);
1245                 if (info->extract.completed_bytes >= info->extract.total_bytes)
1246                         imagex_printf(T("\n"));
1247                 break;
1248         case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1249                 if (info->extract.total_parts != 1) {
1250                         imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1251                                       info->extract.part_number,
1252                                       info->extract.total_parts);
1253                 }
1254                 break;
1255         case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1256                 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1257                         imagex_printf(T("Setting timestamps on all extracted files...\n"));
1258                 break;
1259         case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1260                 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1261                         imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1262                                 info->extract.target);
1263                 }
1264                 break;
1265         case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1266                 percent_done = TO_PERCENT(info->split.completed_bytes,
1267                                           info->split.total_bytes);
1268                 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1269                 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1270                           "%"PRIu64" %"TS" (%u%%) written\n"),
1271                         info->split.part_name,
1272                         info->split.cur_part_number,
1273                         info->split.total_parts,
1274                         info->split.completed_bytes >> unit_shift,
1275                         unit_name,
1276                         info->split.total_bytes >> unit_shift,
1277                         unit_name,
1278                         percent_done);
1279                 break;
1280         case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1281                 if (info->split.completed_bytes == info->split.total_bytes) {
1282                         imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1283                                 info->split.cur_part_number,
1284                                 info->split.total_parts);
1285                 }
1286                 break;
1287         case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1288                 switch (info->update.command->op) {
1289                 case WIMLIB_UPDATE_OP_DELETE:
1290                         imagex_printf(T("Deleted WIM path "
1291                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1292                                 info->update.command->delete_.wim_path);
1293                         break;
1294                 case WIMLIB_UPDATE_OP_RENAME:
1295                         imagex_printf(T("Renamed WIM path "
1296                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1297                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1298                                 info->update.command->rename.wim_source_path,
1299                                 info->update.command->rename.wim_target_path);
1300                         break;
1301                 case WIMLIB_UPDATE_OP_ADD:
1302                 default:
1303                         break;
1304                 }
1305                 break;
1306         default:
1307                 break;
1308         }
1309         fflush(imagex_info_file);
1310         return 0;
1311 }
1312
1313 static unsigned
1314 parse_num_threads(const tchar *optarg)
1315 {
1316         tchar *tmp;
1317         unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1318         if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1319                 imagex_error(T("Number of threads must be a non-negative integer!"));
1320                 return UINT_MAX;
1321         } else {
1322                 return ul_nthreads;
1323         }
1324 }
1325
1326 static uint32_t parse_chunk_size(const tchar *optarg)
1327 {
1328        tchar *tmp;
1329        unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1330        if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1331                imagex_error(T("Chunk size must be a non-negative integer!"));
1332                return UINT32_MAX;
1333        } else {
1334                return chunk_size;
1335        }
1336 }
1337
1338
1339 /*
1340  * Parse an option passed to an update command.
1341  *
1342  * @op:         One of WIMLIB_UPDATE_OP_* that indicates the command being
1343  *              parsed.
1344  *
1345  * @option:     Text string for the option (beginning with --)
1346  *
1347  * @cmd:        `struct wimlib_update_command' that is being constructed for
1348  *              this command.
1349  *
1350  * Returns true if the option was recognized; false if not.
1351  */
1352 static bool
1353 update_command_add_option(int op, const tchar *option,
1354                           struct wimlib_update_command *cmd)
1355 {
1356         bool recognized = true;
1357         switch (op) {
1358         case WIMLIB_UPDATE_OP_ADD:
1359                 if (!tstrcmp(option, T("--verbose")))
1360                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1361                 else if (!tstrcmp(option, T("--unix-data")))
1362                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1363                 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1364                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1365                 else if (!tstrcmp(option, T("--strict-acls")))
1366                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1367                 else if (!tstrcmp(option, T("--dereference")))
1368                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1369                 else
1370                         recognized = false;
1371                 break;
1372         case WIMLIB_UPDATE_OP_DELETE:
1373                 if (!tstrcmp(option, T("--force")))
1374                         cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1375                 else if (!tstrcmp(option, T("--recursive")))
1376                         cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1377                 else
1378                         recognized = false;
1379                 break;
1380         default:
1381                 recognized = false;
1382                 break;
1383         }
1384         return recognized;
1385 }
1386
1387 /* How many nonoption arguments each `imagex update' command expects */
1388 static const unsigned update_command_num_nonoptions[] = {
1389         [WIMLIB_UPDATE_OP_ADD] = 2,
1390         [WIMLIB_UPDATE_OP_DELETE] = 1,
1391         [WIMLIB_UPDATE_OP_RENAME] = 2,
1392 };
1393
1394 static void
1395 update_command_add_nonoption(int op, const tchar *nonoption,
1396                              struct wimlib_update_command *cmd,
1397                              unsigned num_nonoptions)
1398 {
1399         switch (op) {
1400         case WIMLIB_UPDATE_OP_ADD:
1401                 if (num_nonoptions == 0)
1402                         cmd->add.fs_source_path = (tchar*)nonoption;
1403                 else
1404                         cmd->add.wim_target_path = (tchar*)nonoption;
1405                 break;
1406         case WIMLIB_UPDATE_OP_DELETE:
1407                 cmd->delete_.wim_path = (tchar*)nonoption;
1408                 break;
1409         case WIMLIB_UPDATE_OP_RENAME:
1410                 if (num_nonoptions == 0)
1411                         cmd->rename.wim_source_path = (tchar*)nonoption;
1412                 else
1413                         cmd->rename.wim_target_path = (tchar*)nonoption;
1414                 break;
1415         }
1416 }
1417
1418 /*
1419  * Parse a command passed on stdin to `imagex update'.
1420  *
1421  * @line:       Text of the command.
1422  * @len:        Length of the line, including a null terminator
1423  *              at line[len - 1].
1424  *
1425  * @command:    A `struct wimlib_update_command' to fill in from the parsed
1426  *              line.
1427  *
1428  * @line_number: Line number of the command, for diagnostics.
1429  *
1430  * Returns true on success; returns false on parse error.
1431  */
1432 static bool
1433 parse_update_command(tchar *line, size_t len,
1434                      struct wimlib_update_command *command,
1435                      size_t line_number)
1436 {
1437         int ret;
1438         tchar *command_name;
1439         int op;
1440         size_t num_nonoptions;
1441
1442         /* Get the command name ("add", "delete", "rename") */
1443         ret = parse_string(&line, &len, &command_name);
1444         if (ret != PARSE_STRING_SUCCESS)
1445                 return false;
1446
1447         if (!tstrcasecmp(command_name, T("add"))) {
1448                 op = WIMLIB_UPDATE_OP_ADD;
1449         } else if (!tstrcasecmp(command_name, T("delete"))) {
1450                 op = WIMLIB_UPDATE_OP_DELETE;
1451         } else if (!tstrcasecmp(command_name, T("rename"))) {
1452                 op = WIMLIB_UPDATE_OP_RENAME;
1453         } else {
1454                 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1455                              command_name, line_number);
1456                 return false;
1457         }
1458         command->op = op;
1459
1460         /* Parse additional options and non-options as needed */
1461         num_nonoptions = 0;
1462         for (;;) {
1463                 tchar *next_string;
1464
1465                 ret = parse_string(&line, &len, &next_string);
1466                 if (ret == PARSE_STRING_NONE) /* End of line */
1467                         break;
1468                 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1469                         return false;
1470                 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1471                         /* Option */
1472                         if (!update_command_add_option(op, next_string, command))
1473                         {
1474                                 imagex_error(T("Unrecognized option \"%"TS"\" to "
1475                                                "update command \"%"TS"\" on line %zu"),
1476                                              next_string, command_name, line_number);
1477
1478                                 return false;
1479                         }
1480                 } else {
1481                         /* Nonoption */
1482                         if (num_nonoptions == update_command_num_nonoptions[op])
1483                         {
1484                                 imagex_error(T("Unexpected argument \"%"TS"\" in "
1485                                                "update command on line %zu\n"
1486                                                "       (The \"%"TS"\" command only "
1487                                                "takes %zu nonoption arguments!)\n"),
1488                                              next_string, line_number,
1489                                              command_name, num_nonoptions);
1490                                 return false;
1491                         }
1492                         update_command_add_nonoption(op, next_string,
1493                                                      command, num_nonoptions);
1494                         num_nonoptions++;
1495                 }
1496         }
1497
1498         if (num_nonoptions != update_command_num_nonoptions[op]) {
1499                 imagex_error(T("Not enough arguments to update command "
1500                                "\"%"TS"\" on line %zu"), command_name, line_number);
1501                 return false;
1502         }
1503         return true;
1504 }
1505
1506 static struct wimlib_update_command *
1507 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1508                           size_t *num_cmds_ret)
1509 {
1510         ssize_t nlines;
1511         tchar *p;
1512         struct wimlib_update_command *cmds;
1513         size_t i, j;
1514
1515         nlines = text_file_count_lines(cmd_file_contents_p,
1516                                        &cmd_file_nchars);
1517         if (nlines < 0)
1518                 return NULL;
1519
1520         /* Always allocate at least 1 slot, just in case the implementation of
1521          * calloc() returns NULL if 0 bytes are requested. */
1522         cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1523         if (!cmds) {
1524                 imagex_error(T("out of memory"));
1525                 return NULL;
1526         }
1527         p = *cmd_file_contents_p;
1528         j = 0;
1529         for (i = 0; i < nlines; i++) {
1530                 /* XXX: Could use rawmemchr() here instead, but it may not be
1531                  * available on all platforms. */
1532                 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1533                 size_t len = endp - p + 1;
1534                 *endp = T('\0');
1535                 if (!is_comment_line(p, len)) {
1536                         if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1537                                 free(cmds);
1538                                 return NULL;
1539                         }
1540                 }
1541                 p = endp + 1;
1542         }
1543         *num_cmds_ret = j;
1544         return cmds;
1545 }
1546
1547 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1548  * one image from a WIM file to a NTFS volume.  */
1549 static int
1550 imagex_apply(int argc, tchar **argv, int cmd)
1551 {
1552         int c;
1553         int open_flags = 0;
1554         int image = WIMLIB_NO_IMAGE;
1555         WIMStruct *wim;
1556         struct wimlib_wim_info info;
1557         int ret;
1558         const tchar *wimfile;
1559         const tchar *target;
1560         const tchar *image_num_or_name = NULL;
1561         int extract_flags = 0;
1562
1563         STRING_SET(refglobs);
1564
1565         for_opt(c, apply_options) {
1566                 switch (c) {
1567                 case IMAGEX_CHECK_OPTION:
1568                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1569                         break;
1570                 case IMAGEX_HARDLINK_OPTION:
1571                         extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1572                         break;
1573                 case IMAGEX_SYMLINK_OPTION:
1574                         extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1575                         break;
1576                 case IMAGEX_VERBOSE_OPTION:
1577                         /* No longer does anything.  */
1578                         break;
1579                 case IMAGEX_REF_OPTION:
1580                         ret = string_set_append(&refglobs, optarg);
1581                         if (ret)
1582                                 goto out_free_refglobs;
1583                         break;
1584                 case IMAGEX_UNIX_DATA_OPTION:
1585                         extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1586                         break;
1587                 case IMAGEX_NO_ACLS_OPTION:
1588                         extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1589                         break;
1590                 case IMAGEX_STRICT_ACLS_OPTION:
1591                         extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1592                         break;
1593                 case IMAGEX_NORPFIX_OPTION:
1594                         extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1595                         break;
1596                 case IMAGEX_RPFIX_OPTION:
1597                         extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1598                         break;
1599                 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1600                         extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1601                         extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1602                         break;
1603                 case IMAGEX_RESUME_OPTION:
1604                         extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1605                         break;
1606                 default:
1607                         goto out_usage;
1608                 }
1609         }
1610         argc -= optind;
1611         argv += optind;
1612         if (argc != 2 && argc != 3)
1613                 goto out_usage;
1614
1615         wimfile = argv[0];
1616
1617         if (!tstrcmp(wimfile, T("-"))) {
1618                 /* Attempt to apply pipable WIM from standard input.  */
1619                 if (argc == 2) {
1620                         image_num_or_name = NULL;
1621                         target = argv[1];
1622                 } else {
1623                         image_num_or_name = argv[1];
1624                         target = argv[2];
1625                 }
1626                 wim = NULL;
1627         } else {
1628                 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1629                                       imagex_progress_func);
1630                 if (ret)
1631                         goto out_free_refglobs;
1632
1633                 wimlib_get_wim_info(wim, &info);
1634
1635                 if (argc >= 3) {
1636                         /* Image explicitly specified.  */
1637                         image_num_or_name = argv[1];
1638                         image = wimlib_resolve_image(wim, image_num_or_name);
1639                         ret = verify_image_exists(image, image_num_or_name, wimfile);
1640                         if (ret)
1641                                 goto out_wimlib_free;
1642                         target = argv[2];
1643                 } else {
1644                         /* No image specified; default to image 1, but only if the WIM
1645                          * contains exactly one image.  */
1646
1647                         if (info.image_count != 1) {
1648                                 imagex_error(T("\"%"TS"\" contains %d images; "
1649                                                "Please select one (or all)."),
1650                                              wimfile, info.image_count);
1651                                 wimlib_free(wim);
1652                                 goto out_usage;
1653                         }
1654                         image = 1;
1655                         target = argv[1];
1656                 }
1657         }
1658
1659         if (refglobs.num_strings) {
1660                 if (wim == NULL) {
1661                         imagex_error(T("Can't specify --ref when applying from stdin!"));
1662                         ret = -1;
1663                         goto out_wimlib_free;
1664                 }
1665                 ret = wim_reference_globs(wim, &refglobs, open_flags);
1666                 if (ret)
1667                         goto out_wimlib_free;
1668         }
1669
1670 #ifndef __WIN32__
1671         {
1672                 /* Interpret a regular file or block device target as a NTFS
1673                  * volume.  */
1674                 struct stat stbuf;
1675
1676                 if (tstat(target, &stbuf)) {
1677                         if (errno != ENOENT) {
1678                                 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1679                                                         target);
1680                                 ret = -1;
1681                                 goto out_wimlib_free;
1682                         }
1683                 } else {
1684                         if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1685                                 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1686                 }
1687         }
1688 #endif
1689
1690         if (wim) {
1691                 ret = wimlib_extract_image(wim, image, target, extract_flags,
1692                                            imagex_progress_func);
1693         } else {
1694                 set_fd_to_binary_mode(STDIN_FILENO);
1695                 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1696                                                      image_num_or_name,
1697                                                      target, extract_flags,
1698                                                      imagex_progress_func);
1699         }
1700         if (ret == 0) {
1701                 imagex_printf(T("Done applying WIM image.\n"));
1702         } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1703                 if (wim) {
1704                         do_resource_not_found_warning(wimfile, &info, &refglobs);
1705                 } else {
1706                         imagex_error(T(        "If you are applying an image "
1707                                                "from a split pipable WIM,\n"
1708                                        "       make sure you have "
1709                                        "concatenated together all parts."));
1710                 }
1711         }
1712 out_wimlib_free:
1713         wimlib_free(wim);
1714 out_free_refglobs:
1715         string_set_destroy(&refglobs);
1716         return ret;
1717
1718 out_usage:
1719         usage(CMD_APPLY, stderr);
1720         ret = -1;
1721         goto out_free_refglobs;
1722 }
1723
1724 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1725  * directory trees.  'wimlib-imagex capture': create a new WIM file containing
1726  * the desired image.  'wimlib-imagex append': add a new image to an existing
1727  * WIM file. */
1728 static int
1729 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1730 {
1731         int c;
1732         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1733         int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1734                               WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1735                               WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1736         int write_flags = 0;
1737         int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1738         uint32_t chunk_size = UINT32_MAX;
1739         const tchar *wimfile;
1740         int wim_fd;
1741         const tchar *name;
1742         const tchar *desc;
1743         const tchar *flags_element = NULL;
1744
1745         WIMStruct *wim;
1746         STRING_SET(base_wimfiles);
1747         WIMStruct **base_wims;
1748
1749         WIMStruct *template_wim;
1750         const tchar *template_wimfile = NULL;
1751         const tchar *template_image_name_or_num = NULL;
1752         int template_image = WIMLIB_NO_IMAGE;
1753
1754         int ret;
1755         unsigned num_threads = 0;
1756
1757         tchar *source;
1758         tchar *source_copy;
1759
1760         const tchar *config_file = NULL;
1761         tchar *config_str;
1762         struct wimlib_capture_config *config;
1763
1764         bool source_list = false;
1765         size_t source_list_nchars = 0;
1766         tchar *source_list_contents;
1767         bool capture_sources_malloced;
1768         struct wimlib_capture_source *capture_sources;
1769         size_t num_sources;
1770         bool name_defaulted;
1771
1772         for_opt(c, capture_or_append_options) {
1773                 switch (c) {
1774                 case IMAGEX_BOOT_OPTION:
1775                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1776                         break;
1777                 case IMAGEX_CHECK_OPTION:
1778                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1779                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1780                         break;
1781                 case IMAGEX_NOCHECK_OPTION:
1782                         write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1783                         break;
1784                 case IMAGEX_CONFIG_OPTION:
1785                         config_file = optarg;
1786                         add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1787                         break;
1788                 case IMAGEX_COMPRESS_OPTION:
1789                         compression_type = get_compression_type(optarg);
1790                         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1791                                 goto out_err;
1792                         break;
1793                 case IMAGEX_COMPRESS_SLOW_OPTION:
1794                         ret = set_compress_slow();
1795                         if (ret)
1796                                 goto out_err;
1797                         compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1798                         break;
1799                 case IMAGEX_CHUNK_SIZE_OPTION:
1800                         chunk_size = parse_chunk_size(optarg);
1801                         if (chunk_size == UINT32_MAX)
1802                                 goto out_err;
1803                         break;
1804                 case IMAGEX_PACK_STREAMS_OPTION:
1805                         write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1806                         break;
1807                 case IMAGEX_FLAGS_OPTION:
1808                         flags_element = optarg;
1809                         break;
1810                 case IMAGEX_DEREFERENCE_OPTION:
1811                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1812                         break;
1813                 case IMAGEX_VERBOSE_OPTION:
1814                         /* No longer does anything.  */
1815                         break;
1816                 case IMAGEX_THREADS_OPTION:
1817                         num_threads = parse_num_threads(optarg);
1818                         if (num_threads == UINT_MAX)
1819                                 goto out_err;
1820                         break;
1821                 case IMAGEX_REBUILD_OPTION:
1822                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1823                         break;
1824                 case IMAGEX_UNIX_DATA_OPTION:
1825                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1826                         break;
1827                 case IMAGEX_SOURCE_LIST_OPTION:
1828                         source_list = true;
1829                         break;
1830                 case IMAGEX_NO_ACLS_OPTION:
1831                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1832                         break;
1833                 case IMAGEX_STRICT_ACLS_OPTION:
1834                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1835                         break;
1836                 case IMAGEX_RPFIX_OPTION:
1837                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1838                         break;
1839                 case IMAGEX_NORPFIX_OPTION:
1840                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1841                         break;
1842                 case IMAGEX_PIPABLE_OPTION:
1843                         write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1844                         break;
1845                 case IMAGEX_NOT_PIPABLE_OPTION:
1846                         write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1847                         break;
1848                 case IMAGEX_UPDATE_OF_OPTION:
1849                         if (template_image_name_or_num) {
1850                                 imagex_error(T("'--update-of' can only be "
1851                                                "specified one time!"));
1852                                 goto out_err;
1853                         } else {
1854                                 tchar *colon;
1855                                 colon = tstrrchr(optarg, T(':'));
1856
1857                                 if (colon) {
1858                                         template_wimfile = optarg;
1859                                         *colon = T('\0');
1860                                         template_image_name_or_num = colon + 1;
1861                                 } else {
1862                                         template_wimfile = NULL;
1863                                         template_image_name_or_num = optarg;
1864                                 }
1865                         }
1866                         break;
1867                 case IMAGEX_DELTA_FROM_OPTION:
1868                         if (cmd != CMD_CAPTURE) {
1869                                 imagex_error(T("'--delta-from' is only "
1870                                                "valid for capture!"));
1871                                 goto out_usage;
1872                         }
1873                         ret = string_set_append(&base_wimfiles, optarg);
1874                         if (ret)
1875                                 goto out_free_base_wimfiles;
1876                         write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1877                         break;
1878                 default:
1879                         goto out_usage;
1880                 }
1881         }
1882         argc -= optind;
1883         argv += optind;
1884
1885         if (argc < 2 || argc > 4)
1886                 goto out_usage;
1887
1888         source = argv[0];
1889         wimfile = argv[1];
1890
1891         /* Set default compression type.  */
1892         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1893                 struct wimlib_lzx_compressor_params params;
1894                 memset(&params, 0, sizeof(params));
1895                 params.hdr.size = sizeof(params);
1896                 params.algorithm = WIMLIB_LZX_ALGORITHM_FAST;
1897                 params.use_defaults = 1;
1898
1899                 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1900                                                      &params.hdr);
1901                 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1902         }
1903
1904         if (!tstrcmp(wimfile, T("-"))) {
1905                 /* Writing captured WIM to standard output.  */
1906         #if 0
1907                 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1908                         imagex_error("Can't write a non-pipable WIM to "
1909                                      "standard output!  Specify --pipable\n"
1910                                      "       if you want to create a pipable WIM "
1911                                      "(but read the docs first).");
1912                         goto out_err;
1913                 }
1914         #else
1915                 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1916         #endif
1917                 if (cmd == CMD_APPEND) {
1918                         imagex_error(T("Using standard output for append does "
1919                                        "not make sense."));
1920                         goto out_err;
1921                 }
1922                 wim_fd = STDOUT_FILENO;
1923                 wimfile = NULL;
1924                 imagex_info_file = stderr;
1925                 set_fd_to_binary_mode(wim_fd);
1926         }
1927
1928         /* If template image was specified using --update-of=IMAGE rather
1929          * than --update-of=WIMFILE:IMAGE, set the default WIMFILE.  */
1930         if (template_image_name_or_num && !template_wimfile) {
1931                 if (base_wimfiles.num_strings == 1) {
1932                         /* Capturing delta WIM based on single WIM:  default to
1933                          * base WIM.  */
1934                         template_wimfile = base_wimfiles.strings[0];
1935                 } else if (cmd == CMD_APPEND) {
1936                         /* Appending to WIM:  default to WIM being appended to.
1937                          */
1938                         template_wimfile = wimfile;
1939                 } else {
1940                         /* Capturing a normal (non-delta) WIM, so the WIM file
1941                          * *must* be explicitly specified.  */
1942                         if (base_wimfiles.num_strings > 1) {
1943                                 imagex_error(T("For capture of delta WIM "
1944                                                "based on multiple existing "
1945                                                "WIMs,\n"
1946                                                "      '--update-of' must "
1947                                                "specify WIMFILE:IMAGE!"));
1948                         } else {
1949                                 imagex_error(T("For capture of non-delta WIM, "
1950                                                "'--update-of' must specify "
1951                                                "WIMFILE:IMAGE!"));
1952                         }
1953                         goto out_usage;
1954                 }
1955         }
1956
1957         if (argc >= 3) {
1958                 name = argv[2];
1959                 name_defaulted = false;
1960         } else {
1961                 /* Set default name to SOURCE argument, omitting any directory
1962                  * prefixes and trailing slashes.  This requires making a copy
1963                  * of @source.  Leave some free characters at the end in case we
1964                  * append a number to keep the name unique. */
1965                 size_t source_name_len;
1966
1967                 source_name_len = tstrlen(source);
1968                 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1969                 name = tbasename(tstrcpy(source_copy, source));
1970                 name_defaulted = true;
1971         }
1972         /* Image description defaults to NULL if not given. */
1973         if (argc >= 4)
1974                 desc = argv[3];
1975         else
1976                 desc = NULL;
1977
1978         if (source_list) {
1979                 /* Set up capture sources in source list mode */
1980                 if (source[0] == T('-') && source[1] == T('\0')) {
1981                         source_list_contents = stdin_get_text_contents(&source_list_nchars);
1982                 } else {
1983                         source_list_contents = file_get_text_contents(source,
1984                                                                       &source_list_nchars);
1985                 }
1986                 if (!source_list_contents)
1987                         goto out_err;
1988
1989                 capture_sources = parse_source_list(&source_list_contents,
1990                                                     source_list_nchars,
1991                                                     &num_sources);
1992                 if (!capture_sources) {
1993                         ret = -1;
1994                         goto out_free_source_list_contents;
1995                 }
1996                 capture_sources_malloced = true;
1997         } else {
1998                 /* Set up capture source in non-source-list mode.  */
1999                 capture_sources = alloca(sizeof(struct wimlib_capture_source));
2000                 capture_sources[0].fs_source_path = source;
2001                 capture_sources[0].wim_target_path = NULL;
2002                 capture_sources[0].reserved = 0;
2003                 num_sources = 1;
2004                 capture_sources_malloced = false;
2005                 source_list_contents = NULL;
2006         }
2007
2008         if (config_file) {
2009                 /* Read and parse capture configuration file.  */
2010                 size_t config_len;
2011
2012                 config_str = file_get_text_contents(config_file, &config_len);
2013                 if (!config_str) {
2014                         ret = -1;
2015                         goto out_free_capture_sources;
2016                 }
2017
2018                 config = alloca(sizeof(*config));
2019                 ret = parse_capture_config(&config_str, config_len, config);
2020                 if (ret)
2021                         goto out_free_config;
2022         } else {
2023                 /* No capture configuration file specified; use default
2024                  * configuration for capturing Windows operating systems.  */
2025                 config = NULL;
2026                 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
2027         }
2028
2029         /* Open the existing WIM, or create a new one.  */
2030         if (cmd == CMD_APPEND)
2031                 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2032                                       imagex_progress_func);
2033         else
2034                 ret = wimlib_create_new_wim(compression_type, &wim);
2035         if (ret)
2036                 goto out_free_config;
2037
2038         /* Set chunk size if non-default.  */
2039         if (chunk_size != UINT32_MAX) {
2040                 ret = wimlib_set_output_chunk_size(wim, chunk_size);
2041                 if (ret)
2042                         goto out_free_wim;
2043         }
2044
2045 #ifndef __WIN32__
2046         /* Detect if source is regular file or block device and set NTFS volume
2047          * capture mode.  */
2048         if (!source_list) {
2049                 struct stat stbuf;
2050
2051                 if (tstat(source, &stbuf) == 0) {
2052                         if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2053                                 imagex_printf(T("Capturing WIM image from NTFS "
2054                                           "filesystem on \"%"TS"\"\n"), source);
2055                                 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2056                         }
2057                 } else {
2058                         if (errno != ENOENT) {
2059                                 imagex_error_with_errno(T("Failed to stat "
2060                                                           "\"%"TS"\""), source);
2061                                 ret = -1;
2062                                 goto out_free_wim;
2063                         }
2064                 }
2065         }
2066 #endif
2067
2068         /* If the user did not specify an image name, and the basename of the
2069          * source already exists as an image name in the WIM file, append a
2070          * suffix to make it unique. */
2071         if (cmd == CMD_APPEND && name_defaulted) {
2072                 unsigned long conflict_idx;
2073                 tchar *name_end = tstrchr(name, T('\0'));
2074                 for (conflict_idx = 1;
2075                      wimlib_image_name_in_use(wim, name);
2076                      conflict_idx++)
2077                 {
2078                         tsprintf(name_end, T(" (%lu)"), conflict_idx);
2079                 }
2080         }
2081
2082         /* If capturing a delta WIM, reference resources from the base WIMs
2083          * before adding the new image.  */
2084         if (base_wimfiles.num_strings) {
2085                 base_wims = calloc(base_wimfiles.num_strings,
2086                                    sizeof(base_wims[0]));
2087                 if (base_wims == NULL) {
2088                         imagex_error(T("Out of memory!"));
2089                         ret = -1;
2090                         goto out_free_wim;
2091                 }
2092
2093                 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2094                         ret = wimlib_open_wim(base_wimfiles.strings[i],
2095                                               open_flags, &base_wims[i],
2096                                               imagex_progress_func);
2097                         if (ret)
2098                                 goto out_free_base_wims;
2099
2100                 }
2101
2102                 ret = wimlib_reference_resources(wim, base_wims,
2103                                                  base_wimfiles.num_strings, 0);
2104                 if (ret)
2105                         goto out_free_base_wims;
2106
2107                 if (base_wimfiles.num_strings == 1) {
2108                         imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2109                                       base_wimfiles.strings[0]);
2110                 } else {
2111                         imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2112                                       base_wimfiles.num_strings);
2113                 }
2114
2115         } else {
2116                 base_wims = NULL;
2117         }
2118
2119         /* If capturing or appending as an update of an existing (template) image,
2120          * open the WIM if needed and parse the image index.  */
2121         if (template_image_name_or_num) {
2122
2123
2124                 if (base_wimfiles.num_strings == 1 &&
2125                     template_wimfile == base_wimfiles.strings[0]) {
2126                         template_wim = base_wims[0];
2127                 } else if (template_wimfile == wimfile) {
2128                         template_wim = wim;
2129                 } else {
2130                         ret = wimlib_open_wim(template_wimfile, open_flags,
2131                                               &template_wim, imagex_progress_func);
2132                         if (ret)
2133                                 goto out_free_base_wims;
2134                 }
2135
2136                 template_image = wimlib_resolve_image(template_wim,
2137                                                       template_image_name_or_num);
2138
2139                 if (template_image_name_or_num[0] == T('-')) {
2140                         tchar *tmp;
2141                         unsigned long n;
2142                         struct wimlib_wim_info info;
2143
2144                         wimlib_get_wim_info(template_wim, &info);
2145                         n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2146                         if (n >= 1 && n <= info.image_count &&
2147                             *tmp == T('\0') &&
2148                             tmp != template_image_name_or_num + 1)
2149                         {
2150                                 template_image = info.image_count - (n - 1);
2151                         }
2152                 }
2153                 ret = verify_image_exists_and_is_single(template_image,
2154                                                         template_image_name_or_num,
2155                                                         template_wimfile);
2156                 if (ret)
2157                         goto out_free_template_wim;
2158         } else {
2159                 template_wim = NULL;
2160         }
2161
2162         ret = wimlib_add_image_multisource(wim,
2163                                            capture_sources,
2164                                            num_sources,
2165                                            name,
2166                                            config,
2167                                            add_image_flags,
2168                                            imagex_progress_func);
2169         if (ret)
2170                 goto out_free_template_wim;
2171
2172         if (desc || flags_element || template_image_name_or_num) {
2173                 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2174                  * on which the added one is to be based has been specified with
2175                  * --update-of.  Get the index of the image we just
2176                  *  added, then use it to call the appropriate functions.  */
2177                 struct wimlib_wim_info info;
2178
2179                 wimlib_get_wim_info(wim, &info);
2180
2181                 if (desc) {
2182                         ret = wimlib_set_image_descripton(wim,
2183                                                           info.image_count,
2184                                                           desc);
2185                         if (ret)
2186                                 goto out_free_template_wim;
2187                 }
2188
2189                 if (flags_element) {
2190                         ret = wimlib_set_image_flags(wim, info.image_count,
2191                                                      flags_element);
2192                         if (ret)
2193                                 goto out_free_template_wim;
2194                 }
2195
2196                 /* Reference template image if the user provided one.  */
2197                 if (template_image_name_or_num) {
2198                         imagex_printf(T("Using image %d "
2199                                         "from \"%"TS"\" as template\n"),
2200                                         template_image, template_wimfile);
2201                         ret = wimlib_reference_template_image(wim,
2202                                                               info.image_count,
2203                                                               template_wim,
2204                                                               template_image,
2205                                                               0, NULL);
2206                         if (ret)
2207                                 goto out_free_template_wim;
2208                 }
2209         }
2210
2211         /* Write the new WIM or overwrite the existing WIM with the new image
2212          * appended.  */
2213         if (cmd == CMD_APPEND) {
2214                 ret = wimlib_overwrite(wim, write_flags, num_threads,
2215                                        imagex_progress_func);
2216         } else if (wimfile) {
2217                 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2218                                    write_flags, num_threads,
2219                                    imagex_progress_func);
2220         } else {
2221                 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2222                                          write_flags, num_threads,
2223                                          imagex_progress_func);
2224         }
2225 out_free_template_wim:
2226         /* template_wim may alias base_wims[0] or wim.  */
2227         if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2228             template_wim != wim)
2229                 wimlib_free(template_wim);
2230 out_free_base_wims:
2231         for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2232                 wimlib_free(base_wims[i]);
2233         free(base_wims);
2234 out_free_wim:
2235         wimlib_free(wim);
2236 out_free_config:
2237         if (config) {
2238                 free(config->exclusion_pats.pats);
2239                 free(config->exclusion_exception_pats.pats);
2240                 free(config_str);
2241         }
2242 out_free_capture_sources:
2243         if (capture_sources_malloced)
2244                 free(capture_sources);
2245 out_free_source_list_contents:
2246         free(source_list_contents);
2247 out_free_base_wimfiles:
2248         string_set_destroy(&base_wimfiles);
2249         return ret;
2250
2251 out_usage:
2252         usage(cmd, stderr);
2253 out_err:
2254         ret = -1;
2255         goto out_free_base_wimfiles;
2256 }
2257
2258 /* Remove image(s) from a WIM. */
2259 static int
2260 imagex_delete(int argc, tchar **argv, int cmd)
2261 {
2262         int c;
2263         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2264         int write_flags = 0;
2265         const tchar *wimfile;
2266         const tchar *image_num_or_name;
2267         WIMStruct *wim;
2268         int image;
2269         int ret;
2270
2271         for_opt(c, delete_options) {
2272                 switch (c) {
2273                 case IMAGEX_CHECK_OPTION:
2274                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2275                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2276                         break;
2277                 case IMAGEX_SOFT_OPTION:
2278                         write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2279                         break;
2280                 default:
2281                         goto out_usage;
2282                 }
2283         }
2284         argc -= optind;
2285         argv += optind;
2286
2287         if (argc != 2) {
2288                 if (argc < 1)
2289                         imagex_error(T("Must specify a WIM file"));
2290                 if (argc < 2)
2291                         imagex_error(T("Must specify an image"));
2292                 goto out_usage;
2293         }
2294         wimfile = argv[0];
2295         image_num_or_name = argv[1];
2296
2297         ret = wimlib_open_wim(wimfile, open_flags, &wim,
2298                               imagex_progress_func);
2299         if (ret)
2300                 goto out;
2301
2302         image = wimlib_resolve_image(wim, image_num_or_name);
2303
2304         ret = verify_image_exists(image, image_num_or_name, wimfile);
2305         if (ret)
2306                 goto out_wimlib_free;
2307
2308         ret = wimlib_delete_image(wim, image);
2309         if (ret) {
2310                 imagex_error(T("Failed to delete image from \"%"TS"\""),
2311                              wimfile);
2312                 goto out_wimlib_free;
2313         }
2314
2315         ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2316         if (ret) {
2317                 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2318                                "deleted"), wimfile);
2319         }
2320 out_wimlib_free:
2321         wimlib_free(wim);
2322 out:
2323         return ret;
2324
2325 out_usage:
2326         usage(CMD_DELETE, stderr);
2327         ret = -1;
2328         goto out;
2329 }
2330
2331 static int
2332 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2333 {
2334         int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2335         return (ret >= 0) ? 0 : -1;
2336 }
2337
2338 /* Print the files contained in an image(s) in a WIM file. */
2339 static int
2340 imagex_dir(int argc, tchar **argv, int cmd)
2341 {
2342         const tchar *wimfile;
2343         WIMStruct *wim = NULL;
2344         int image;
2345         int ret;
2346         const tchar *path = T("");
2347         int c;
2348
2349         for_opt(c, dir_options) {
2350                 switch (c) {
2351                 case IMAGEX_PATH_OPTION:
2352                         path = optarg;
2353                         break;
2354                 default:
2355                         goto out_usage;
2356                 }
2357         }
2358         argc -= optind;
2359         argv += optind;
2360
2361         if (argc < 1) {
2362                 imagex_error(T("Must specify a WIM file"));
2363                 goto out_usage;
2364         }
2365         if (argc > 2) {
2366                 imagex_error(T("Too many arguments"));
2367                 goto out_usage;
2368         }
2369
2370         wimfile = argv[0];
2371         ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2372         if (ret)
2373                 goto out;
2374
2375         if (argc >= 2) {
2376                 image = wimlib_resolve_image(wim, argv[1]);
2377                 ret = verify_image_exists(image, argv[1], wimfile);
2378                 if (ret)
2379                         goto out_wimlib_free;
2380         } else {
2381                 /* No image specified; default to image 1, but only if the WIM
2382                  * contains exactly one image.  */
2383
2384                 struct wimlib_wim_info info;
2385
2386                 wimlib_get_wim_info(wim, &info);
2387                 if (info.image_count != 1) {
2388                         imagex_error(T("\"%"TS"\" contains %d images; Please "
2389                                        "select one (or all)."),
2390                                      wimfile, info.image_count);
2391                         wimlib_free(wim);
2392                         goto out_usage;
2393                 }
2394                 image = 1;
2395         }
2396
2397         ret = wimlib_iterate_dir_tree(wim, image, path,
2398                                       WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2399                                       print_full_path, NULL);
2400 out_wimlib_free:
2401         wimlib_free(wim);
2402 out:
2403         return ret;
2404
2405 out_usage:
2406         usage(CMD_DIR, stderr);
2407         ret = -1;
2408         goto out;
2409 }
2410
2411 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2412  * WIM file. */
2413 static int
2414 imagex_export(int argc, tchar **argv, int cmd)
2415 {
2416         int c;
2417         int open_flags = 0;
2418         int export_flags = 0;
2419         int write_flags = 0;
2420         int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2421         const tchar *src_wimfile;
2422         const tchar *src_image_num_or_name;
2423         const tchar *dest_wimfile;
2424         int dest_wim_fd;
2425         const tchar *dest_name;
2426         const tchar *dest_desc;
2427         WIMStruct *src_wim;
2428         struct wimlib_wim_info src_info;
2429         WIMStruct *dest_wim;
2430         int ret;
2431         int image;
2432         struct stat stbuf;
2433         bool wim_is_new;
2434         STRING_SET(refglobs);
2435         unsigned num_threads = 0;
2436         uint32_t chunk_size = UINT32_MAX;
2437
2438         for_opt(c, export_options) {
2439                 switch (c) {
2440                 case IMAGEX_BOOT_OPTION:
2441                         export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2442                         break;
2443                 case IMAGEX_CHECK_OPTION:
2444                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2445                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2446                         break;
2447                 case IMAGEX_NOCHECK_OPTION:
2448                         write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2449                         break;
2450                 case IMAGEX_COMPRESS_OPTION:
2451                         compression_type = get_compression_type(optarg);
2452                         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2453                                 goto out_err;
2454                         break;
2455                 case IMAGEX_PACK_STREAMS_OPTION:
2456                         write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2457                         break;
2458                 case IMAGEX_CHUNK_SIZE_OPTION:
2459                         chunk_size = parse_chunk_size(optarg);
2460                         if (chunk_size == UINT32_MAX)
2461                                 goto out_err;
2462                         break;
2463                 case IMAGEX_REF_OPTION:
2464                         ret = string_set_append(&refglobs, optarg);
2465                         if (ret)
2466                                 goto out_free_refglobs;
2467                         break;
2468                 case IMAGEX_THREADS_OPTION:
2469                         num_threads = parse_num_threads(optarg);
2470                         if (num_threads == UINT_MAX)
2471                                 goto out_err;
2472                         break;
2473                 case IMAGEX_REBUILD_OPTION:
2474                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2475                         break;
2476                 case IMAGEX_PIPABLE_OPTION:
2477                         write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2478                         break;
2479                 case IMAGEX_NOT_PIPABLE_OPTION:
2480                         write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2481                         break;
2482                 default:
2483                         goto out_usage;
2484                 }
2485         }
2486         argc -= optind;
2487         argv += optind;
2488         if (argc < 3 || argc > 5)
2489                 goto out_usage;
2490         src_wimfile           = argv[0];
2491         src_image_num_or_name = argv[1];
2492         dest_wimfile          = argv[2];
2493         dest_name             = (argc >= 4) ? argv[3] : NULL;
2494         dest_desc             = (argc >= 5) ? argv[4] : NULL;
2495         ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2496                               imagex_progress_func);
2497         if (ret)
2498                 goto out_free_refglobs;
2499
2500         wimlib_get_wim_info(src_wim, &src_info);
2501
2502         /* Determine if the destination is an existing file or not.  If so, we
2503          * try to append the exported image(s) to it; otherwise, we create a new
2504          * WIM containing the exported image(s).  Furthermore, determine if we
2505          * need to write a pipable WIM directly to standard output.  */
2506
2507         if (tstrcmp(dest_wimfile, T("-")) == 0) {
2508         #if 0
2509                 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2510                         imagex_error("Can't write a non-pipable WIM to "
2511                                      "standard output!  Specify --pipable\n"
2512                                      "       if you want to create a pipable WIM "
2513                                      "(but read the docs first).");
2514                         ret = -1;
2515                         goto out_free_src_wim;
2516                 }
2517         #else
2518                 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2519         #endif
2520                 dest_wimfile = NULL;
2521                 dest_wim_fd = STDOUT_FILENO;
2522                 imagex_info_file = stderr;
2523                 set_fd_to_binary_mode(dest_wim_fd);
2524         }
2525         errno = ENOENT;
2526         if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2527                 wim_is_new = false;
2528                 /* Destination file exists. */
2529
2530                 if (!S_ISREG(stbuf.st_mode)) {
2531                         imagex_error(T("\"%"TS"\" is not a regular file"),
2532                                      dest_wimfile);
2533                         ret = -1;
2534                         goto out_free_src_wim;
2535                 }
2536                 ret = wimlib_open_wim(dest_wimfile,
2537                                       open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2538                                       &dest_wim, imagex_progress_func);
2539                 if (ret)
2540                         goto out_free_src_wim;
2541
2542                 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2543                         /* The user specified a compression type, but we're
2544                          * exporting to an existing WIM.  Make sure the
2545                          * specified compression type is the same as the
2546                          * compression type of the existing destination WIM. */
2547                         struct wimlib_wim_info dest_info;
2548
2549                         wimlib_get_wim_info(dest_wim, &dest_info);
2550                         if (compression_type != dest_info.compression_type) {
2551                                 imagex_error(T("Cannot specify a compression type that is "
2552                                                "not the same as that used in the "
2553                                                "destination WIM"));
2554                                 ret = -1;
2555                                 goto out_free_dest_wim;
2556                         }
2557                 }
2558         } else {
2559                 wim_is_new = true;
2560
2561                 if (errno != ENOENT) {
2562                         imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2563                                                 dest_wimfile);
2564                         ret = -1;
2565                         goto out_free_src_wim;
2566                 }
2567
2568                 /* dest_wimfile is not an existing file, so create a new WIM. */
2569
2570                 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2571                         /* The user did not specify a compression type; default
2572                          * to that of the source WIM.  */
2573
2574                         compression_type = src_info.compression_type;
2575                 }
2576                 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2577                 if (ret)
2578                         goto out_free_src_wim;
2579
2580                 /* Use same chunk size if compression type is the same.  */
2581                 if (compression_type == src_info.compression_type &&
2582                     chunk_size == UINT32_MAX)
2583                         wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2584         }
2585
2586         if (chunk_size != UINT32_MAX) {
2587                 /* Set destination chunk size.  */
2588                 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2589                 if (ret)
2590                         goto out_free_dest_wim;
2591         }
2592
2593         image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2594         ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2595         if (ret)
2596                 goto out_free_dest_wim;
2597
2598         if (refglobs.num_strings) {
2599                 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2600                 if (ret)
2601                         goto out_free_dest_wim;
2602         }
2603
2604         if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2605             image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2606         {
2607                 imagex_error(T("--boot specified for all-images export, but source WIM "
2608                                "has no bootable image."));
2609                 ret = -1;
2610                 goto out_free_dest_wim;
2611         }
2612
2613         ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2614                                   dest_desc, export_flags, imagex_progress_func);
2615         if (ret) {
2616                 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2617                         do_resource_not_found_warning(src_wimfile,
2618                                                       &src_info, &refglobs);
2619                 }
2620                 goto out_free_dest_wim;
2621         }
2622
2623         if (!wim_is_new)
2624                 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2625                                        imagex_progress_func);
2626         else if (dest_wimfile)
2627                 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2628                                    write_flags, num_threads,
2629                                    imagex_progress_func);
2630         else
2631                 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2632                                          WIMLIB_ALL_IMAGES, write_flags,
2633                                          num_threads, imagex_progress_func);
2634 out_free_dest_wim:
2635         wimlib_free(dest_wim);
2636 out_free_src_wim:
2637         wimlib_free(src_wim);
2638 out_free_refglobs:
2639         string_set_destroy(&refglobs);
2640         return ret;
2641
2642 out_usage:
2643         usage(CMD_EXPORT, stderr);
2644 out_err:
2645         ret = -1;
2646         goto out_free_refglobs;
2647 }
2648
2649 static bool
2650 is_root_wim_path(const tchar *path)
2651 {
2652         const tchar *p;
2653         for (p = path; *p; p++)
2654                 if (*p != T('\\') && *p != T('/'))
2655                         return false;
2656         return true;
2657 }
2658
2659 static void
2660 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2661                       const tchar *dest_dir)
2662 {
2663         for (size_t i = 0; i < num_cmds; i++)
2664                 if (cmds[i].fs_dest_path != dest_dir)
2665                         free(cmds[i].fs_dest_path);
2666         free(cmds);
2667 }
2668
2669 static struct wimlib_extract_command *
2670 prepare_extract_commands(tchar **paths, unsigned num_paths,
2671                          int extract_flags, tchar *dest_dir,
2672                          size_t *num_cmds_ret)
2673 {
2674         struct wimlib_extract_command *cmds;
2675         size_t num_cmds;
2676         tchar *emptystr = T("");
2677
2678         if (num_paths == 0) {
2679                 num_paths = 1;
2680                 paths = &emptystr;
2681         }
2682         num_cmds = num_paths;
2683         cmds = calloc(num_cmds, sizeof(cmds[0]));
2684         if (!cmds) {
2685                 imagex_error(T("Out of memory!"));
2686                 return NULL;
2687         }
2688
2689         for (size_t i = 0; i < num_cmds; i++) {
2690                 cmds[i].extract_flags = extract_flags;
2691                 cmds[i].wim_source_path = paths[i];
2692                 if (is_root_wim_path(paths[i])) {
2693                         cmds[i].fs_dest_path = dest_dir;
2694                 } else {
2695                         size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2696                         cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2697                         if (!cmds[i].fs_dest_path) {
2698                                 free_extract_commands(cmds, num_cmds, dest_dir);
2699                                 return NULL;
2700                         }
2701                         tsprintf(cmds[i].fs_dest_path,
2702                                  T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2703                                  dest_dir, tbasename(paths[i]));
2704                 }
2705         }
2706         *num_cmds_ret = num_cmds;
2707         return cmds;
2708 }
2709
2710 /* Extract files or directories from a WIM image */
2711 static int
2712 imagex_extract(int argc, tchar **argv, int cmd)
2713 {
2714         int c;
2715         int open_flags = 0;
2716         int image;
2717         WIMStruct *wim;
2718         int ret;
2719         const tchar *wimfile;
2720         const tchar *image_num_or_name;
2721         const tchar *pathlist;
2722         tchar *dest_dir = T(".");
2723         int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX;
2724         int listfile_extract_flags = WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2725
2726         STRING_SET(refglobs);
2727
2728         struct wimlib_extract_command *cmds;
2729         size_t num_cmds;
2730
2731         for_opt(c, extract_options) {
2732                 switch (c) {
2733                 case IMAGEX_CHECK_OPTION:
2734                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2735                         break;
2736                 case IMAGEX_VERBOSE_OPTION:
2737                         /* No longer does anything.  */
2738                         break;
2739                 case IMAGEX_REF_OPTION:
2740                         ret = string_set_append(&refglobs, optarg);
2741                         if (ret)
2742                                 goto out_free_refglobs;
2743                         break;
2744                 case IMAGEX_UNIX_DATA_OPTION:
2745                         extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2746                         break;
2747                 case IMAGEX_NO_ACLS_OPTION:
2748                         extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2749                         break;
2750                 case IMAGEX_STRICT_ACLS_OPTION:
2751                         extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2752                         break;
2753                 case IMAGEX_DEST_DIR_OPTION:
2754                         dest_dir = optarg;
2755                         break;
2756                 case IMAGEX_TO_STDOUT_OPTION:
2757                         extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2758                         imagex_info_file = stderr;
2759                         imagex_be_quiet = true;
2760                         break;
2761                 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2762                         extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2763                         extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2764                         break;
2765                 case IMAGEX_NO_WILDCARDS_OPTION:
2766                         listfile_extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2767                         break;
2768                 case IMAGEX_STRICT_WILDCARDS_OPTION:
2769                         listfile_extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2770                         break;
2771                 default:
2772                         goto out_usage;
2773                 }
2774         }
2775         argc -= optind;
2776         argv += optind;
2777
2778         if (argc < 2)
2779                 goto out_usage;
2780
2781         wimfile = argv[0];
2782         image_num_or_name = argv[1];
2783
2784         argc -= 2;
2785         argv += 2;
2786
2787         if (argc == 1 && argv[0][0] == T('@')) {
2788                 pathlist = argv[0] + 1;
2789                 cmds = NULL;
2790                 num_cmds = 0;
2791         } else {
2792                 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2793                                                 &num_cmds);
2794                 if (cmds == NULL)
2795                         goto out_err;
2796                 pathlist = NULL;
2797         }
2798
2799         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2800         if (ret)
2801                 goto out_free_cmds;
2802
2803         image = wimlib_resolve_image(wim, image_num_or_name);
2804         ret = verify_image_exists_and_is_single(image,
2805                                                 image_num_or_name,
2806                                                 wimfile);
2807         if (ret)
2808                 goto out_wimlib_free;
2809
2810         if (refglobs.num_strings) {
2811                 ret = wim_reference_globs(wim, &refglobs, open_flags);
2812                 if (ret)
2813                         goto out_wimlib_free;
2814         }
2815
2816         ret = 0;
2817         if (ret == 0 && cmds != NULL) {
2818                 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2819                                            imagex_progress_func);
2820         }
2821         if (ret == 0 && pathlist != NULL) {
2822                 ret = wimlib_extract_pathlist(wim, image, dest_dir,
2823                                               pathlist,
2824                                               extract_flags | listfile_extract_flags,
2825                                               imagex_progress_func);
2826         }
2827         if (ret == 0) {
2828                 if (!imagex_be_quiet)
2829                         imagex_printf(T("Done extracting files.\n"));
2830         } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2831                 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2832                                    "files and directories\n"
2833                                    "      are in the WIM image.\n"),
2834                                 get_cmd_string(CMD_DIR, false));
2835         } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2836                 struct wimlib_wim_info info;
2837
2838                 wimlib_get_wim_info(wim, &info);
2839                 do_resource_not_found_warning(wimfile, &info, &refglobs);
2840         }
2841 out_wimlib_free:
2842         wimlib_free(wim);
2843 out_free_cmds:
2844         free_extract_commands(cmds, num_cmds, dest_dir);
2845 out_free_refglobs:
2846         string_set_destroy(&refglobs);
2847         return ret;
2848
2849 out_usage:
2850         usage(CMD_EXTRACT, stderr);
2851 out_err:
2852         ret = -1;
2853         goto out_free_refglobs;
2854 }
2855
2856 static void print_byte_field(const uint8_t field[], size_t len)
2857 {
2858         while (len--)
2859                 tprintf(T("%02hhx"), *field++);
2860 }
2861
2862 static void
2863 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2864 {
2865         tputs(T("WIM Information:"));
2866         tputs(T("----------------"));
2867         tprintf(T("Path:           %"TS"\n"), wimfile);
2868         tprintf(T("GUID:           0x"));
2869         print_byte_field(info->guid, sizeof(info->guid));
2870         tputchar(T('\n'));
2871         tprintf(T("Version:        %u\n"), info->wim_version);
2872         tprintf(T("Image Count:    %d\n"), info->image_count);
2873         tprintf(T("Compression:    %"TS"\n"),
2874                 wimlib_get_compression_type_string(info->compression_type));
2875         tprintf(T("Chunk Size:     %"PRIu32" bytes\n"),
2876                 info->chunk_size);
2877         tprintf(T("Part Number:    %d/%d\n"), info->part_number, info->total_parts);
2878         tprintf(T("Boot Index:     %d\n"), info->boot_index);
2879         tprintf(T("Size:           %"PRIu64" bytes\n"), info->total_bytes);
2880         tprintf(T("Integrity Info: %"TS"\n"),
2881                 info->has_integrity_table ? T("yes") : T("no"));
2882         tprintf(T("Relative path junction: %"TS"\n"),
2883                 info->has_rpfix ? T("yes") : T("no"));
2884         tprintf(T("Pipable:        %"TS"\n"),
2885                 info->pipable ? T("yes") : T("no"));
2886         tputchar(T('\n'));
2887 }
2888
2889 static int
2890 print_resource(const struct wimlib_resource_entry *resource,
2891                void *_ignore)
2892 {
2893         tprintf(T("Uncompressed size     = %"PRIu64" bytes\n"),
2894                 resource->uncompressed_size);
2895         if (resource->packed) {
2896                 tprintf(T("Raw compressed size   = %"PRIu64" bytes\n"),
2897                         resource->raw_resource_compressed_size);
2898
2899                 tprintf(T("Raw offset in WIM     = %"PRIu64" bytes\n"),
2900                         resource->raw_resource_offset_in_wim);
2901
2902                 tprintf(T("Offset in raw         = %"PRIu64" bytes\n"),
2903                         resource->offset);
2904         } else {
2905                 tprintf(T("Compressed size       = %"PRIu64" bytes\n"),
2906                         resource->compressed_size);
2907
2908                 tprintf(T("Offset in WIM         = %"PRIu64" bytes\n"),
2909                         resource->offset);
2910         }
2911
2912         tprintf(T("Part Number           = %u\n"), resource->part_number);
2913         tprintf(T("Reference Count       = %u\n"), resource->reference_count);
2914
2915         tprintf(T("Hash                  = 0x"));
2916         print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2917         tputchar(T('\n'));
2918
2919         tprintf(T("Flags                 = "));
2920         if (resource->is_compressed)
2921                 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED  "));
2922         if (resource->is_metadata)
2923                 tprintf(T("WIM_RESHDR_FLAG_METADATA  "));
2924         if (resource->is_free)
2925                 tprintf(T("WIM_RESHDR_FLAG_FREE  "));
2926         if (resource->is_spanned)
2927                 tprintf(T("WIM_RESHDR_FLAG_SPANNED  "));
2928         if (resource->packed)
2929                 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS  "));
2930         tputchar(T('\n'));
2931         tputchar(T('\n'));
2932         return 0;
2933 }
2934
2935 static void
2936 print_lookup_table(WIMStruct *wim)
2937 {
2938         wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2939 }
2940
2941 /* Prints information about a WIM file; also can mark an image as bootable,
2942  * change the name of an image, or change the description of an image. */
2943 static int
2944 imagex_info(int argc, tchar **argv, int cmd)
2945 {
2946         int c;
2947         bool boot         = false;
2948         bool check        = false;
2949         bool nocheck      = false;
2950         bool header       = false;
2951         bool lookup_table = false;
2952         bool xml          = false;
2953         bool metadata     = false;
2954         bool short_header = true;
2955         const tchar *xml_out_file = NULL;
2956         const tchar *wimfile;
2957         const tchar *image_num_or_name;
2958         const tchar *new_name;
2959         const tchar *new_desc;
2960         WIMStruct *wim;
2961         int image;
2962         int ret;
2963         int open_flags = 0;
2964         struct wimlib_wim_info info;
2965
2966         for_opt(c, info_options) {
2967                 switch (c) {
2968                 case IMAGEX_BOOT_OPTION:
2969                         boot = true;
2970                         break;
2971                 case IMAGEX_CHECK_OPTION:
2972                         check = true;
2973                         break;
2974                 case IMAGEX_NOCHECK_OPTION:
2975                         nocheck = true;
2976                         break;
2977                 case IMAGEX_HEADER_OPTION:
2978                         header = true;
2979                         short_header = false;
2980                         break;
2981                 case IMAGEX_LOOKUP_TABLE_OPTION:
2982                         lookup_table = true;
2983                         short_header = false;
2984                         break;
2985                 case IMAGEX_XML_OPTION:
2986                         xml = true;
2987                         short_header = false;
2988                         break;
2989                 case IMAGEX_EXTRACT_XML_OPTION:
2990                         xml_out_file = optarg;
2991                         short_header = false;
2992                         break;
2993                 case IMAGEX_METADATA_OPTION:
2994                         metadata = true;
2995                         short_header = false;
2996                         break;
2997                 default:
2998                         goto out_usage;
2999                 }
3000         }
3001
3002         argc -= optind;
3003         argv += optind;
3004         if (argc < 1 || argc > 4)
3005                 goto out_usage;
3006
3007         wimfile           = argv[0];
3008         image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3009         new_name          = (argc >= 3) ? argv[2] : NULL;
3010         new_desc          = (argc >= 4) ? argv[3] : NULL;
3011
3012         if (check && nocheck) {
3013                 imagex_error(T("Can't specify both --check and --nocheck"));
3014                 goto out_err;
3015         }
3016
3017         if (check)
3018                 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3019
3020         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3021         if (ret)
3022                 goto out;
3023
3024         wimlib_get_wim_info(wim, &info);
3025
3026         image = wimlib_resolve_image(wim, image_num_or_name);
3027         ret = WIMLIB_ERR_INVALID_IMAGE;
3028         if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3029                 verify_image_exists(image, image_num_or_name, wimfile);
3030                 if (boot) {
3031                         imagex_error(T("If you would like to set the boot "
3032                                        "index to 0, specify image \"0\" with "
3033                                        "the --boot flag."));
3034                 }
3035                 goto out_wimlib_free;
3036         }
3037
3038         if (boot && info.image_count == 0) {
3039                 imagex_error(T("--boot is meaningless on a WIM with no images"));
3040                 goto out_wimlib_free;
3041         }
3042
3043         if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3044                 if (boot) {
3045                         imagex_error(T("Cannot specify the --boot flag "
3046                                        "without specifying a specific "
3047                                        "image in a multi-image WIM"));
3048                         goto out_wimlib_free;
3049                 }
3050                 if (new_name) {
3051                         imagex_error(T("Cannot specify the NEW_NAME "
3052                                        "without specifying a specific "
3053                                        "image in a multi-image WIM"));
3054                         goto out_wimlib_free;
3055                 }
3056         }
3057
3058         /* Operations that print information are separated from operations that
3059          * recreate the WIM file. */
3060         if (!new_name && !boot) {
3061
3062                 /* Read-only operations */
3063
3064                 if (image == WIMLIB_NO_IMAGE) {
3065                         imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3066                                      image_num_or_name, wimfile);
3067                         goto out_wimlib_free;
3068                 }
3069
3070                 if (image == WIMLIB_ALL_IMAGES && short_header)
3071                         print_wim_information(wimfile, &info);
3072
3073                 if (header)
3074                         wimlib_print_header(wim);
3075
3076                 if (lookup_table) {
3077                         if (info.total_parts != 1) {
3078                                 tfprintf(stderr, T("Warning: Only showing the lookup table "
3079                                                    "for part %d of a %d-part WIM.\n"),
3080                                          info.part_number, info.total_parts);
3081                         }
3082                         print_lookup_table(wim);
3083                 }
3084
3085                 if (xml) {
3086                         ret = wimlib_extract_xml_data(wim, stdout);
3087                         if (ret)
3088                                 goto out_wimlib_free;
3089                 }
3090
3091                 if (xml_out_file) {
3092                         FILE *fp;
3093
3094                         fp = tfopen(xml_out_file, T("wb"));
3095                         if (!fp) {
3096                                 imagex_error_with_errno(T("Failed to open the "
3097                                                           "file \"%"TS"\" for "
3098                                                           "writing"),
3099                                                         xml_out_file);
3100                                 ret = -1;
3101                                 goto out_wimlib_free;
3102                         }
3103                         ret = wimlib_extract_xml_data(wim, fp);
3104                         if (fclose(fp)) {
3105                                 imagex_error(T("Failed to close the file "
3106                                                "\"%"TS"\""),
3107                                              xml_out_file);
3108                                 ret = -1;
3109                         }
3110                         if (ret)
3111                                 goto out_wimlib_free;
3112                 }
3113
3114                 if (short_header)
3115                         wimlib_print_available_images(wim, image);
3116
3117                 if (metadata) {
3118                         ret = wimlib_print_metadata(wim, image);
3119                         if (ret)
3120                                 goto out_wimlib_free;
3121                 }
3122                 ret = 0;
3123         } else {
3124
3125                 /* Modification operations */
3126
3127                 if (image == WIMLIB_ALL_IMAGES)
3128                         image = 1;
3129
3130                 if (image == WIMLIB_NO_IMAGE && new_name) {
3131                         imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3132                                        "when using image 0"), new_name);
3133                         ret = -1;
3134                         goto out_wimlib_free;
3135                 }
3136
3137                 if (boot) {
3138                         if (image == info.boot_index) {
3139                                 imagex_printf(T("Image %d is already marked as "
3140                                           "bootable.\n"), image);
3141                                 boot = false;
3142                         } else {
3143                                 imagex_printf(T("Marking image %d as bootable.\n"),
3144                                         image);
3145                                 info.boot_index = image;
3146                                 ret = wimlib_set_wim_info(wim, &info,
3147                                                           WIMLIB_CHANGE_BOOT_INDEX);
3148                                 if (ret)
3149                                         goto out_wimlib_free;
3150                         }
3151                 }
3152                 if (new_name) {
3153                         if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3154                         {
3155                                 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3156                                         image, new_name);
3157                                 new_name = NULL;
3158                         } else {
3159                                 imagex_printf(T("Changing the name of image %d to "
3160                                           "\"%"TS"\".\n"), image, new_name);
3161                                 ret = wimlib_set_image_name(wim, image, new_name);
3162                                 if (ret)
3163                                         goto out_wimlib_free;
3164                         }
3165                 }
3166                 if (new_desc) {
3167                         const tchar *old_desc;
3168                         old_desc = wimlib_get_image_description(wim, image);
3169                         if (old_desc && !tstrcmp(old_desc, new_desc)) {
3170                                 imagex_printf(T("The description of image %d is already "
3171                                           "\"%"TS"\".\n"), image, new_desc);
3172                                 new_desc = NULL;
3173                         } else {
3174                                 imagex_printf(T("Changing the description of image %d "
3175                                           "to \"%"TS"\".\n"), image, new_desc);
3176                                 ret = wimlib_set_image_descripton(wim, image,
3177                                                                   new_desc);
3178                                 if (ret)
3179                                         goto out_wimlib_free;
3180                         }
3181                 }
3182
3183                 /* Only call wimlib_overwrite() if something actually needs to
3184                  * be changed.  */
3185                 if (boot || new_name || new_desc ||
3186                     (check && !info.has_integrity_table) ||
3187                     (nocheck && info.has_integrity_table))
3188                 {
3189                         int write_flags = 0;
3190
3191                         if (check)
3192                                 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3193                         if (nocheck)
3194                                 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3195                         ret = wimlib_overwrite(wim, write_flags, 1,
3196                                                imagex_progress_func);
3197                 } else {
3198                         imagex_printf(T("The file \"%"TS"\" was not modified "
3199                                         "because nothing needed to be done.\n"),
3200                                       wimfile);
3201                         ret = 0;
3202                 }
3203         }
3204 out_wimlib_free:
3205         wimlib_free(wim);
3206 out:
3207         return ret;
3208
3209 out_usage:
3210         usage(CMD_INFO, stderr);
3211 out_err:
3212         ret = -1;
3213         goto out;
3214 }
3215