]> wimlib.net Git - wimlib/blob - programs/imagex.c
Update LZMS compressor
[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 void
438 set_compress_slow(void)
439 {
440         int ret;
441         static const struct wimlib_lzx_compressor_params lzx_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
460         static const struct wimlib_lzms_compressor_params lzms_slow_params = {
461                 .hdr = {
462                         .size = sizeof(struct wimlib_lzms_compressor_params),
463                 },
464                 .min_match_length = 2,
465                 .max_match_length = UINT32_MAX,
466                 .nice_match_length = 96,
467                 .max_search_depth = 100,
468                 .max_matches_per_pos = 10,
469                 .optim_array_length = 1024,
470         };
471
472         wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
473                                              &lzx_slow_params.hdr);
474
475         wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
476                                              &lzms_slow_params.hdr);
477 }
478
479 struct string_set {
480         const tchar **strings;
481         unsigned num_strings;
482         unsigned num_alloc_strings;
483 };
484
485 #define STRING_SET_INITIALIZER \
486         { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
487
488 #define STRING_SET(_strings) \
489         struct string_set _strings = STRING_SET_INITIALIZER
490
491 static int
492 string_set_append(struct string_set *set, const tchar *glob)
493 {
494         unsigned num_alloc_strings = set->num_alloc_strings;
495
496         if (set->num_strings == num_alloc_strings) {
497                 const tchar **new_strings;
498
499                 num_alloc_strings += 4;
500                 new_strings = realloc(set->strings,
501                                       sizeof(set->strings[0]) * num_alloc_strings);
502                 if (!new_strings) {
503                         imagex_error(T("Out of memory!"));
504                         return -1;
505                 }
506                 set->strings = new_strings;
507                 set->num_alloc_strings = num_alloc_strings;
508         }
509         set->strings[set->num_strings++] = glob;
510         return 0;
511 }
512
513 static void
514 string_set_destroy(struct string_set *set)
515 {
516         free(set->strings);
517 }
518
519 static int
520 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
521 {
522         return wimlib_reference_resource_files(wim, set->strings,
523                                                set->num_strings,
524                                                WIMLIB_REF_FLAG_GLOB_ENABLE,
525                                                open_flags,
526                                                imagex_progress_func);
527 }
528
529 static void
530 do_resource_not_found_warning(const tchar *wimfile,
531                               const struct wimlib_wim_info *info,
532                               const struct string_set *refglobs)
533 {
534         if (info->total_parts > 1) {
535                 if (refglobs->num_strings == 0) {
536                         imagex_error(T("\"%"TS"\" is part of a split WIM. "
537                                        "Use --ref to specify the other parts."),
538                                      wimfile);
539                 } else {
540                         imagex_error(T("Perhaps the '--ref' argument did not "
541                                        "specify all other parts of the split "
542                                        "WIM?"));
543                 }
544         } else {
545                 imagex_error(T("If this is a delta WIM, use the --ref argument "
546                                "to specify the WIM(s) on which it is based."));
547         }
548 }
549
550 /* Returns the size of a file given its name, or -1 if the file does not exist
551  * or its size cannot be determined.  */
552 static off_t
553 file_get_size(const tchar *filename)
554 {
555         struct stat st;
556         if (tstat(filename, &st) == 0)
557                 return st.st_size;
558         else
559                 return (off_t)-1;
560 }
561
562 enum {
563         PARSE_STRING_SUCCESS = 0,
564         PARSE_STRING_FAILURE = 1,
565         PARSE_STRING_NONE = 2,
566 };
567
568 /*
569  * Parses a string token from an array of characters.
570  *
571  * Tokens are either whitespace-delimited, or double or single-quoted.
572  *
573  * @line_p:  Pointer to the pointer to the line of data.  Will be updated
574  *           to point past the string token iff the return value is
575  *           PARSE_STRING_SUCCESS.  If *len_p > 0, (*line_p)[*len_p - 1] must
576  *           be '\0'.
577  *
578  * @len_p:   @len_p initially stores the length of the line of data, which may
579  *           be 0, and it will be updated to the number of bytes remaining in
580  *           the line iff the return value is PARSE_STRING_SUCCESS.
581  *
582  * @fn_ret:  Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
583  *           parsed string token will be returned here.
584  *
585  * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
586  *          PARSE_STRING_FAILURE if the data was invalid due to a missing
587  *          closing quote; or PARSE_STRING_NONE if the line ended before the
588  *          beginning of a string token was found.
589  */
590 static int
591 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
592 {
593         size_t len = *len_p;
594         tchar *line = *line_p;
595         tchar *fn;
596         tchar quote_char;
597
598         /* Skip leading whitespace */
599         for (;;) {
600                 if (len == 0)
601                         return PARSE_STRING_NONE;
602                 if (!istspace(*line) && *line != T('\0'))
603                         break;
604                 line++;
605                 len--;
606         }
607         quote_char = *line;
608         if (quote_char == T('"') || quote_char == T('\'')) {
609                 /* Quoted string */
610                 line++;
611                 len--;
612                 fn = line;
613                 line = tmemchr(line, quote_char, len);
614                 if (!line) {
615                         imagex_error(T("Missing closing quote: %"TS), fn - 1);
616                         return PARSE_STRING_FAILURE;
617                 }
618         } else {
619                 /* Unquoted string.  Go until whitespace.  Line is terminated
620                  * by '\0', so no need to check 'len'. */
621                 fn = line;
622                 do {
623                         line++;
624                 } while (!istspace(*line) && *line != T('\0'));
625         }
626         *line = T('\0');
627         len -= line - fn;
628         *len_p = len;
629         *line_p = line;
630         *fn_ret = fn;
631         return PARSE_STRING_SUCCESS;
632 }
633
634 /* Parses a line of data (not an empty line or comment) in the source list file
635  * format.  (See the man page for 'wimlib-imagex capture' for details on this
636  * format and the meaning.)
637  *
638  * @line:  Line of data to be parsed.  line[len - 1] must be '\0', unless
639  *         len == 0.  The data in @line will be modified by this function call.
640  *
641  * @len:   Length of the line of data.
642  *
643  * @source:  On success, the capture source and target described by the line is
644  *           written into this destination.  Note that it will contain pointers
645  *           to data in the @line array.
646  *
647  * Returns true if the line was valid; false otherwise.  */
648 static bool
649 parse_source_list_line(tchar *line, size_t len,
650                        struct wimlib_capture_source *source)
651 {
652         /* SOURCE [DEST] */
653         int ret;
654         ret = parse_string(&line, &len, &source->fs_source_path);
655         if (ret != PARSE_STRING_SUCCESS)
656                 return false;
657         ret = parse_string(&line, &len, &source->wim_target_path);
658         if (ret == PARSE_STRING_NONE)
659                 source->wim_target_path = source->fs_source_path;
660         return ret != PARSE_STRING_FAILURE;
661 }
662
663 /* Returns %true if the given line of length @len > 0 is a comment or empty line
664  * in the source list file format. */
665 static bool
666 is_comment_line(const tchar *line, size_t len)
667 {
668         for (;;) {
669                 if (*line == T('#'))
670                         return true;
671                 if (!istspace(*line) && *line != T('\0'))
672                         return false;
673                 ++line;
674                 --len;
675                 if (len == 0)
676                         return true;
677         }
678 }
679
680 static ssize_t
681 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
682 {
683         ssize_t nlines = 0;
684         tchar *contents = *contents_p;
685         size_t nchars = *nchars_p;
686         size_t i;
687
688         for (i = 0; i < nchars; i++)
689                 if (contents[i] == T('\n'))
690                         nlines++;
691
692         /* Handle last line not terminated by a newline */
693         if (nchars != 0 && contents[nchars - 1] != T('\n')) {
694                 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
695                 if (!contents) {
696                         imagex_error(T("Out of memory!"));
697                         return -1;
698                 }
699                 contents[nchars] = T('\n');
700                 *contents_p = contents;
701                 nchars++;
702                 nlines++;
703         }
704         *nchars_p = nchars;
705         return nlines;
706 }
707
708 /* Parses a file in the source list format.  (See the man page for
709  * 'wimlib-imagex capture' for details on this format and the meaning.)
710  *
711  * @source_list_contents:  Contents of the source list file.  Note that this
712  *                         buffer will be modified to save memory allocations,
713  *                         and cannot be freed until the returned array of
714  *                         wimlib_capture_source's has also been freed.
715  *
716  * @source_list_nbytes:    Number of bytes of data in the @source_list_contents
717  *                         buffer.
718  *
719  * @nsources_ret:          On success, the length of the returned array is
720  *                         returned here.
721  *
722  * Returns:   An array of `struct wimlib_capture_source's that can be passed to
723  * the wimlib_add_image_multisource() function to specify how a WIM image is to
724  * be created.  */
725 static struct wimlib_capture_source *
726 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
727                   size_t *nsources_ret)
728 {
729         ssize_t nlines;
730         tchar *p;
731         struct wimlib_capture_source *sources;
732         size_t i, j;
733
734         nlines = text_file_count_lines(source_list_contents_p,
735                                        &source_list_nchars);
736         if (nlines < 0)
737                 return NULL;
738
739         /* Always allocate at least 1 slot, just in case the implementation of
740          * calloc() returns NULL if 0 bytes are requested. */
741         sources = calloc(nlines ?: 1, sizeof(*sources));
742         if (!sources) {
743                 imagex_error(T("out of memory"));
744                 return NULL;
745         }
746         p = *source_list_contents_p;
747         j = 0;
748         for (i = 0; i < nlines; i++) {
749                 /* XXX: Could use rawmemchr() here instead, but it may not be
750                  * available on all platforms. */
751                 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
752                 size_t len = endp - p + 1;
753                 *endp = T('\0');
754                 if (!is_comment_line(p, len)) {
755                         if (!parse_source_list_line(p, len, &sources[j++])) {
756                                 free(sources);
757                                 return NULL;
758                         }
759                 }
760                 p = endp + 1;
761
762         }
763         *nsources_ret = j;
764         return sources;
765 }
766
767
768 enum capture_config_section {
769         CAPTURE_CONFIG_NO_SECTION,
770         CAPTURE_CONFIG_EXCLUSION_SECTION,
771         CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
772         CAPTURE_CONFIG_IGNORE_SECTION,
773 };
774
775 enum {
776         CAPTURE_CONFIG_INVALID_SECTION,
777         CAPTURE_CONFIG_CHANGED_SECTION,
778         CAPTURE_CONFIG_SAME_SECTION,
779 };
780
781 static int
782 check_config_section(tchar *line, size_t len,
783                      enum capture_config_section *cur_section)
784 {
785         while (istspace(*line))
786                 line++;
787
788         if (*line != T('['))
789                 return CAPTURE_CONFIG_SAME_SECTION;
790
791         line++;
792         tchar *endbrace = tstrrchr(line, T(']'));
793         if (!endbrace)
794                 return CAPTURE_CONFIG_SAME_SECTION;
795
796         if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
797                 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
798         } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
799                 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
800         } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
801                 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
802                 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
803                          "of capture config file\n"),
804                        stderr);
805         } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
806                 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
807                 tfputs(T("WARNING: Ignoring [AlignmentList] section "
808                          "of capture config file\n"),
809                        stderr);
810         } else {
811                 imagex_error(T("Invalid capture config file section \"%"TS"\""),
812                              line - 1);
813                 return CAPTURE_CONFIG_INVALID_SECTION;
814         }
815         return CAPTURE_CONFIG_CHANGED_SECTION;
816 }
817
818
819 static bool
820 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
821                          tchar *pat)
822 {
823         if (pat_list->num_pats == pat_list->num_allocated_pats) {
824                 tchar **pats;
825                 size_t num_allocated_pats = pat_list->num_pats + 8;
826
827                 pats = realloc(pat_list->pats,
828                                num_allocated_pats * sizeof(pat_list->pats[0]));
829                 if (!pats) {
830                         imagex_error(T("Out of memory!"));
831                         return false;
832                 }
833                 pat_list->pats = pats;
834                 pat_list->num_allocated_pats = num_allocated_pats;
835         }
836         pat_list->pats[pat_list->num_pats++] = pat;
837         return true;
838 }
839
840 static bool
841 parse_capture_config_line(tchar *line, size_t len,
842                           enum capture_config_section *cur_section,
843                           struct wimlib_capture_config *config)
844 {
845         tchar *filename;
846         int ret;
847
848         ret = check_config_section(line, len, cur_section);
849         if (ret == CAPTURE_CONFIG_INVALID_SECTION)
850                 return false;
851         if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
852                 return true;
853
854         switch (*cur_section) {
855         case CAPTURE_CONFIG_NO_SECTION:
856                 imagex_error(T("Line \"%"TS"\" is not in a section "
857                                "(such as [ExclusionList]"), line);
858                 return false;
859         case CAPTURE_CONFIG_EXCLUSION_SECTION:
860                 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
861                         return false;
862                 return pattern_list_add_pattern(&config->exclusion_pats,
863                                                 filename);
864         case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
865                 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
866                         return false;
867                 return pattern_list_add_pattern(&config->exclusion_exception_pats,
868                                                 filename);
869         case CAPTURE_CONFIG_IGNORE_SECTION:
870                 return true;
871         }
872         return false;
873 }
874
875 static int
876 parse_capture_config(tchar **contents_p, size_t nchars,
877                      struct wimlib_capture_config *config)
878 {
879         ssize_t nlines;
880         tchar *p;
881         size_t i;
882         enum capture_config_section cur_section;
883
884         memset(config, 0, sizeof(*config));
885
886         nlines = text_file_count_lines(contents_p, &nchars);
887         if (nlines < 0)
888                 return -1;
889
890         cur_section = CAPTURE_CONFIG_NO_SECTION;
891         p = *contents_p;
892         for (i = 0; i < nlines; i++) {
893                 tchar *endp = tmemchr(p, T('\n'), nchars);
894                 size_t len = endp - p + 1;
895                 *endp = T('\0');
896                 if (!is_comment_line(p, len))
897                         if (!parse_capture_config_line(p, len, &cur_section, config))
898                                 return -1;
899                 p = endp + 1;
900
901         }
902         return 0;
903 }
904
905 /* Reads the contents of a file into memory. */
906 static char *
907 file_get_contents(const tchar *filename, size_t *len_ret)
908 {
909         struct stat stbuf;
910         void *buf = NULL;
911         size_t len;
912         FILE *fp;
913
914         if (tstat(filename, &stbuf) != 0) {
915                 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
916                 goto out;
917         }
918         len = stbuf.st_size;
919
920         fp = tfopen(filename, T("rb"));
921         if (!fp) {
922                 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
923                 goto out;
924         }
925
926         buf = malloc(len ? len : 1);
927         if (!buf) {
928                 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
929                                "contents of file \"%"TS"\""), len, filename);
930                 goto out_fclose;
931         }
932         if (fread(buf, 1, len, fp) != len) {
933                 imagex_error_with_errno(T("Failed to read %zu bytes from the "
934                                           "file \"%"TS"\""), len, filename);
935                 goto out_free_buf;
936         }
937         *len_ret = len;
938         goto out_fclose;
939 out_free_buf:
940         free(buf);
941         buf = NULL;
942 out_fclose:
943         fclose(fp);
944 out:
945         return buf;
946 }
947
948 /* Read standard input until EOF and return the full contents in a malloc()ed
949  * buffer and the number of bytes of data in @len_ret.  Returns NULL on read
950  * error. */
951 static char *
952 stdin_get_contents(size_t *len_ret)
953 {
954         /* stdin can, of course, be a pipe or other non-seekable file, so the
955          * total length of the data cannot be pre-determined */
956         char *buf = NULL;
957         size_t newlen = 1024;
958         size_t pos = 0;
959         size_t inc = 1024;
960         for (;;) {
961                 char *p = realloc(buf, newlen);
962                 size_t bytes_read, bytes_to_read;
963                 if (!p) {
964                         imagex_error(T("out of memory while reading stdin"));
965                         break;
966                 }
967                 buf = p;
968                 bytes_to_read = newlen - pos;
969                 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
970                 pos += bytes_read;
971                 if (bytes_read != bytes_to_read) {
972                         if (feof(stdin)) {
973                                 *len_ret = pos;
974                                 return buf;
975                         } else {
976                                 imagex_error_with_errno(T("error reading stdin"));
977                                 break;
978                         }
979                 }
980                 newlen += inc;
981                 inc *= 3;
982                 inc /= 2;
983         }
984         free(buf);
985         return NULL;
986 }
987
988
989 static tchar *
990 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
991 {
992 #ifndef __WIN32__
993         /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
994          * */
995         *num_tchars_ret = num_bytes;
996         return text;
997 #else /* !__WIN32__ */
998         /* On Windows, translate the text to UTF-16LE */
999         wchar_t *text_wstr;
1000         size_t num_wchars;
1001
1002         if (num_bytes >= 2 &&
1003             (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
1004              ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
1005         {
1006                 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
1007                  * with something that looks like an ASCII character encoded as
1008                  * a UTF-16LE code unit.  Assume the file is encoded as
1009                  * UTF-16LE.  This is not a 100% reliable check. */
1010                 num_wchars = num_bytes / 2;
1011                 text_wstr = (wchar_t*)text;
1012         } else {
1013                 /* File does not look like UTF-16LE.  Assume it is encoded in
1014                  * the current Windows code page.  I think these are always
1015                  * ASCII-compatible, so any so-called "plain-text" (ASCII) files
1016                  * should work as expected. */
1017                 text_wstr = win32_mbs_to_wcs(text,
1018                                              num_bytes,
1019                                              &num_wchars);
1020                 free(text);
1021         }
1022         *num_tchars_ret = num_wchars;
1023         return text_wstr;
1024 #endif /* __WIN32__ */
1025 }
1026
1027 static tchar *
1028 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1029 {
1030         char *contents;
1031         size_t num_bytes;
1032
1033         contents = file_get_contents(filename, &num_bytes);
1034         if (!contents)
1035                 return NULL;
1036         return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1037 }
1038
1039 static tchar *
1040 stdin_get_text_contents(size_t *num_tchars_ret)
1041 {
1042         char *contents;
1043         size_t num_bytes;
1044
1045         contents = stdin_get_contents(&num_bytes);
1046         if (!contents)
1047                 return NULL;
1048         return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1049 }
1050
1051 #define TO_PERCENT(numerator, denominator) \
1052         (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1053
1054 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1055 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1056 #define KIBIBYTE_MIN_NBYTES 10000ULL
1057
1058 static unsigned
1059 get_unit(uint64_t total_bytes, const tchar **name_ret)
1060 {
1061         if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1062                 *name_ret = T("GiB");
1063                 return 30;
1064         } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1065                 *name_ret = T("MiB");
1066                 return 20;
1067         } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1068                 *name_ret = T("KiB");
1069                 return 10;
1070         } else {
1071                 *name_ret = T("bytes");
1072                 return 0;
1073         }
1074 }
1075
1076 static struct wimlib_progress_info_scan last_scan_progress;
1077
1078 static void
1079 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1080 {
1081         uint64_t prev_count, cur_count;
1082
1083         prev_count = last_scan_progress.num_nondirs_scanned +
1084                      last_scan_progress.num_dirs_scanned;
1085         cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1086
1087         if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1088             cur_count % 128 == 0)
1089         {
1090                 unsigned unit_shift;
1091                 const tchar *unit_name;
1092
1093                 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1094                 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1095                                 "%"PRIu64" directories)    "),
1096                               scan->num_bytes_scanned >> unit_shift,
1097                               unit_name,
1098                               scan->num_nondirs_scanned,
1099                               scan->num_dirs_scanned);
1100                 last_scan_progress = *scan;
1101         }
1102 }
1103
1104 /* Progress callback function passed to various wimlib functions. */
1105 static int
1106 imagex_progress_func(enum wimlib_progress_msg msg,
1107                      const union wimlib_progress_info *info)
1108 {
1109         unsigned percent_done;
1110         unsigned unit_shift;
1111         const tchar *unit_name;
1112
1113         if (imagex_be_quiet)
1114                 return 0;
1115         switch (msg) {
1116         case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1117                 {
1118                         static bool first = true;
1119                         if (first) {
1120                                 imagex_printf(T("Writing %"TS"-compressed data "
1121                                                 "using %u thread%"TS"\n"),
1122                                               wimlib_get_compression_type_string(
1123                                                         info->write_streams.compression_type),
1124                                         info->write_streams.num_threads,
1125                                         (info->write_streams.num_threads == 1) ? T("") : T("s"));
1126                                 first = false;
1127                         }
1128                 }
1129                 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1130                 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1131                                           info->write_streams.total_bytes);
1132
1133                 if (info->write_streams.total_parts <= 1) {
1134                         imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1135                                 "written (%u%% done)"),
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                 } else {
1142                         imagex_printf(T("\rWriting resources from part %u of %u: "
1143                                   "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1144                                 (info->write_streams.completed_parts ==
1145                                         info->write_streams.total_parts) ?
1146                                                 info->write_streams.completed_parts :
1147                                                 info->write_streams.completed_parts + 1,
1148                                 info->write_streams.total_parts,
1149                                 info->write_streams.completed_bytes >> unit_shift,
1150                                 unit_name,
1151                                 info->write_streams.total_bytes >> unit_shift,
1152                                 unit_name,
1153                                 percent_done);
1154                 }
1155                 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1156                         imagex_printf(T("\n"));
1157                 break;
1158         case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1159                 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1160                 if (*info->scan.wim_target_path) {
1161                         imagex_printf(T(" (loading as WIM path: "
1162                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1163                                info->scan.wim_target_path);
1164                 } else {
1165                         imagex_printf(T("\n"));
1166                 }
1167                 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1168                 break;
1169         case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1170                 switch (info->scan.status) {
1171                 case WIMLIB_SCAN_DENTRY_OK:
1172                         report_scan_progress(&info->scan, false);
1173                         break;
1174                 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1175                         imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1176                         break;
1177                 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1178                         imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1179                                         "         \"%"TS"\" from capture\n"), info->scan.cur_path);
1180                         break;
1181                 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1182                         imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1183                                         "with out-of-tree target:\n"
1184                                         "           \"%"TS"\" => \"%"TS"\"\n"
1185                                         "           (Use --norpfix to capture "
1186                                         "absolute symbolic links as-is)\n"),
1187                                         info->scan.cur_path, info->scan.symlink_target);
1188                         break;
1189                 }
1190                 break;
1191         case WIMLIB_PROGRESS_MSG_SCAN_END:
1192                 report_scan_progress(&info->scan, true);
1193                 imagex_printf(T("\n"));
1194                 break;
1195         case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1196                 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1197                 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1198                                           info->integrity.total_bytes);
1199                 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1200                         "of %"PRIu64" %"TS" (%u%%) done"),
1201                         info->integrity.filename,
1202                         info->integrity.completed_bytes >> unit_shift,
1203                         unit_name,
1204                         info->integrity.total_bytes >> unit_shift,
1205                         unit_name,
1206                         percent_done);
1207                 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1208                         imagex_printf(T("\n"));
1209                 break;
1210         case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1211                 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1212                 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1213                                           info->integrity.total_bytes);
1214                 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1215                           "of %"PRIu64" %"TS" (%u%%) done"),
1216                         info->integrity.completed_bytes >> unit_shift,
1217                         unit_name,
1218                         info->integrity.total_bytes >> unit_shift,
1219                         unit_name,
1220                         percent_done);
1221                 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1222                         imagex_printf(T("\n"));
1223                 break;
1224         case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1225                 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1226                           "to %"TS" \"%"TS"\"\n"),
1227                         info->extract.image,
1228                         info->extract.image_name,
1229                         info->extract.wimfile_name,
1230                         ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1231                          T("NTFS volume") : T("directory")),
1232                         info->extract.target);
1233                 break;
1234         case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1235                 if (info->extract.extract_root_wim_source_path[0] != T('\0')) {
1236                         imagex_printf(T("Extracting "
1237                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" "
1238                                   "from image %d (\"%"TS"\") "
1239                                   "in \"%"TS"\" to \"%"TS"\"\n"),
1240                                 info->extract.extract_root_wim_source_path,
1241                                 info->extract.image,
1242                                 info->extract.image_name,
1243                                 info->extract.wimfile_name,
1244                                 info->extract.target);
1245                 }
1246                 break;
1247         case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1248                 percent_done = TO_PERCENT(info->extract.completed_bytes,
1249                                           info->extract.total_bytes);
1250                 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1251                 imagex_printf(T("\rExtracting files: "
1252                           "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1253                         info->extract.completed_bytes >> unit_shift,
1254                         unit_name,
1255                         info->extract.total_bytes >> unit_shift,
1256                         unit_name,
1257                         percent_done);
1258                 if (info->extract.completed_bytes >= info->extract.total_bytes)
1259                         imagex_printf(T("\n"));
1260                 break;
1261         case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1262                 if (info->extract.total_parts != 1) {
1263                         imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1264                                       info->extract.part_number,
1265                                       info->extract.total_parts);
1266                 }
1267                 break;
1268         case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1269                 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1270                         imagex_printf(T("Setting timestamps on all extracted files...\n"));
1271                 break;
1272         case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1273                 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1274                         imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1275                                 info->extract.target);
1276                 }
1277                 break;
1278         case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1279                 percent_done = TO_PERCENT(info->split.completed_bytes,
1280                                           info->split.total_bytes);
1281                 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1282                 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1283                           "%"PRIu64" %"TS" (%u%%) written\n"),
1284                         info->split.part_name,
1285                         info->split.cur_part_number,
1286                         info->split.total_parts,
1287                         info->split.completed_bytes >> unit_shift,
1288                         unit_name,
1289                         info->split.total_bytes >> unit_shift,
1290                         unit_name,
1291                         percent_done);
1292                 break;
1293         case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1294                 if (info->split.completed_bytes == info->split.total_bytes) {
1295                         imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1296                                 info->split.cur_part_number,
1297                                 info->split.total_parts);
1298                 }
1299                 break;
1300         case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1301                 switch (info->update.command->op) {
1302                 case WIMLIB_UPDATE_OP_DELETE:
1303                         imagex_printf(T("Deleted WIM path "
1304                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1305                                 info->update.command->delete_.wim_path);
1306                         break;
1307                 case WIMLIB_UPDATE_OP_RENAME:
1308                         imagex_printf(T("Renamed WIM path "
1309                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1310                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1311                                 info->update.command->rename.wim_source_path,
1312                                 info->update.command->rename.wim_target_path);
1313                         break;
1314                 case WIMLIB_UPDATE_OP_ADD:
1315                 default:
1316                         break;
1317                 }
1318                 break;
1319         default:
1320                 break;
1321         }
1322         fflush(imagex_info_file);
1323         return 0;
1324 }
1325
1326 static unsigned
1327 parse_num_threads(const tchar *optarg)
1328 {
1329         tchar *tmp;
1330         unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1331         if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1332                 imagex_error(T("Number of threads must be a non-negative integer!"));
1333                 return UINT_MAX;
1334         } else {
1335                 return ul_nthreads;
1336         }
1337 }
1338
1339 static uint32_t parse_chunk_size(const tchar *optarg)
1340 {
1341        tchar *tmp;
1342        unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1343        if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1344                imagex_error(T("Chunk size must be a non-negative integer!"));
1345                return UINT32_MAX;
1346        } else {
1347                return chunk_size;
1348        }
1349 }
1350
1351
1352 /*
1353  * Parse an option passed to an update command.
1354  *
1355  * @op:         One of WIMLIB_UPDATE_OP_* that indicates the command being
1356  *              parsed.
1357  *
1358  * @option:     Text string for the option (beginning with --)
1359  *
1360  * @cmd:        `struct wimlib_update_command' that is being constructed for
1361  *              this command.
1362  *
1363  * Returns true if the option was recognized; false if not.
1364  */
1365 static bool
1366 update_command_add_option(int op, const tchar *option,
1367                           struct wimlib_update_command *cmd)
1368 {
1369         bool recognized = true;
1370         switch (op) {
1371         case WIMLIB_UPDATE_OP_ADD:
1372                 if (!tstrcmp(option, T("--verbose")))
1373                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1374                 else if (!tstrcmp(option, T("--unix-data")))
1375                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1376                 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1377                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1378                 else if (!tstrcmp(option, T("--strict-acls")))
1379                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1380                 else if (!tstrcmp(option, T("--dereference")))
1381                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1382                 else
1383                         recognized = false;
1384                 break;
1385         case WIMLIB_UPDATE_OP_DELETE:
1386                 if (!tstrcmp(option, T("--force")))
1387                         cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1388                 else if (!tstrcmp(option, T("--recursive")))
1389                         cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1390                 else
1391                         recognized = false;
1392                 break;
1393         default:
1394                 recognized = false;
1395                 break;
1396         }
1397         return recognized;
1398 }
1399
1400 /* How many nonoption arguments each `imagex update' command expects */
1401 static const unsigned update_command_num_nonoptions[] = {
1402         [WIMLIB_UPDATE_OP_ADD] = 2,
1403         [WIMLIB_UPDATE_OP_DELETE] = 1,
1404         [WIMLIB_UPDATE_OP_RENAME] = 2,
1405 };
1406
1407 static void
1408 update_command_add_nonoption(int op, const tchar *nonoption,
1409                              struct wimlib_update_command *cmd,
1410                              unsigned num_nonoptions)
1411 {
1412         switch (op) {
1413         case WIMLIB_UPDATE_OP_ADD:
1414                 if (num_nonoptions == 0)
1415                         cmd->add.fs_source_path = (tchar*)nonoption;
1416                 else
1417                         cmd->add.wim_target_path = (tchar*)nonoption;
1418                 break;
1419         case WIMLIB_UPDATE_OP_DELETE:
1420                 cmd->delete_.wim_path = (tchar*)nonoption;
1421                 break;
1422         case WIMLIB_UPDATE_OP_RENAME:
1423                 if (num_nonoptions == 0)
1424                         cmd->rename.wim_source_path = (tchar*)nonoption;
1425                 else
1426                         cmd->rename.wim_target_path = (tchar*)nonoption;
1427                 break;
1428         }
1429 }
1430
1431 /*
1432  * Parse a command passed on stdin to `imagex update'.
1433  *
1434  * @line:       Text of the command.
1435  * @len:        Length of the line, including a null terminator
1436  *              at line[len - 1].
1437  *
1438  * @command:    A `struct wimlib_update_command' to fill in from the parsed
1439  *              line.
1440  *
1441  * @line_number: Line number of the command, for diagnostics.
1442  *
1443  * Returns true on success; returns false on parse error.
1444  */
1445 static bool
1446 parse_update_command(tchar *line, size_t len,
1447                      struct wimlib_update_command *command,
1448                      size_t line_number)
1449 {
1450         int ret;
1451         tchar *command_name;
1452         int op;
1453         size_t num_nonoptions;
1454
1455         /* Get the command name ("add", "delete", "rename") */
1456         ret = parse_string(&line, &len, &command_name);
1457         if (ret != PARSE_STRING_SUCCESS)
1458                 return false;
1459
1460         if (!tstrcasecmp(command_name, T("add"))) {
1461                 op = WIMLIB_UPDATE_OP_ADD;
1462         } else if (!tstrcasecmp(command_name, T("delete"))) {
1463                 op = WIMLIB_UPDATE_OP_DELETE;
1464         } else if (!tstrcasecmp(command_name, T("rename"))) {
1465                 op = WIMLIB_UPDATE_OP_RENAME;
1466         } else {
1467                 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1468                              command_name, line_number);
1469                 return false;
1470         }
1471         command->op = op;
1472
1473         /* Parse additional options and non-options as needed */
1474         num_nonoptions = 0;
1475         for (;;) {
1476                 tchar *next_string;
1477
1478                 ret = parse_string(&line, &len, &next_string);
1479                 if (ret == PARSE_STRING_NONE) /* End of line */
1480                         break;
1481                 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1482                         return false;
1483                 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1484                         /* Option */
1485                         if (!update_command_add_option(op, next_string, command))
1486                         {
1487                                 imagex_error(T("Unrecognized option \"%"TS"\" to "
1488                                                "update command \"%"TS"\" on line %zu"),
1489                                              next_string, command_name, line_number);
1490
1491                                 return false;
1492                         }
1493                 } else {
1494                         /* Nonoption */
1495                         if (num_nonoptions == update_command_num_nonoptions[op])
1496                         {
1497                                 imagex_error(T("Unexpected argument \"%"TS"\" in "
1498                                                "update command on line %zu\n"
1499                                                "       (The \"%"TS"\" command only "
1500                                                "takes %zu nonoption arguments!)\n"),
1501                                              next_string, line_number,
1502                                              command_name, num_nonoptions);
1503                                 return false;
1504                         }
1505                         update_command_add_nonoption(op, next_string,
1506                                                      command, num_nonoptions);
1507                         num_nonoptions++;
1508                 }
1509         }
1510
1511         if (num_nonoptions != update_command_num_nonoptions[op]) {
1512                 imagex_error(T("Not enough arguments to update command "
1513                                "\"%"TS"\" on line %zu"), command_name, line_number);
1514                 return false;
1515         }
1516         return true;
1517 }
1518
1519 static struct wimlib_update_command *
1520 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1521                           size_t *num_cmds_ret)
1522 {
1523         ssize_t nlines;
1524         tchar *p;
1525         struct wimlib_update_command *cmds;
1526         size_t i, j;
1527
1528         nlines = text_file_count_lines(cmd_file_contents_p,
1529                                        &cmd_file_nchars);
1530         if (nlines < 0)
1531                 return NULL;
1532
1533         /* Always allocate at least 1 slot, just in case the implementation of
1534          * calloc() returns NULL if 0 bytes are requested. */
1535         cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1536         if (!cmds) {
1537                 imagex_error(T("out of memory"));
1538                 return NULL;
1539         }
1540         p = *cmd_file_contents_p;
1541         j = 0;
1542         for (i = 0; i < nlines; i++) {
1543                 /* XXX: Could use rawmemchr() here instead, but it may not be
1544                  * available on all platforms. */
1545                 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1546                 size_t len = endp - p + 1;
1547                 *endp = T('\0');
1548                 if (!is_comment_line(p, len)) {
1549                         if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1550                                 free(cmds);
1551                                 return NULL;
1552                         }
1553                 }
1554                 p = endp + 1;
1555         }
1556         *num_cmds_ret = j;
1557         return cmds;
1558 }
1559
1560 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1561  * one image from a WIM file to a NTFS volume.  */
1562 static int
1563 imagex_apply(int argc, tchar **argv, int cmd)
1564 {
1565         int c;
1566         int open_flags = 0;
1567         int image = WIMLIB_NO_IMAGE;
1568         WIMStruct *wim;
1569         struct wimlib_wim_info info;
1570         int ret;
1571         const tchar *wimfile;
1572         const tchar *target;
1573         const tchar *image_num_or_name = NULL;
1574         int extract_flags = 0;
1575
1576         STRING_SET(refglobs);
1577
1578         for_opt(c, apply_options) {
1579                 switch (c) {
1580                 case IMAGEX_CHECK_OPTION:
1581                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1582                         break;
1583                 case IMAGEX_HARDLINK_OPTION:
1584                         extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1585                         break;
1586                 case IMAGEX_SYMLINK_OPTION:
1587                         extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1588                         break;
1589                 case IMAGEX_VERBOSE_OPTION:
1590                         /* No longer does anything.  */
1591                         break;
1592                 case IMAGEX_REF_OPTION:
1593                         ret = string_set_append(&refglobs, optarg);
1594                         if (ret)
1595                                 goto out_free_refglobs;
1596                         break;
1597                 case IMAGEX_UNIX_DATA_OPTION:
1598                         extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1599                         break;
1600                 case IMAGEX_NO_ACLS_OPTION:
1601                         extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1602                         break;
1603                 case IMAGEX_STRICT_ACLS_OPTION:
1604                         extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1605                         break;
1606                 case IMAGEX_NORPFIX_OPTION:
1607                         extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1608                         break;
1609                 case IMAGEX_RPFIX_OPTION:
1610                         extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1611                         break;
1612                 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1613                         extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1614                         extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1615                         break;
1616                 case IMAGEX_RESUME_OPTION:
1617                         extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1618                         break;
1619                 default:
1620                         goto out_usage;
1621                 }
1622         }
1623         argc -= optind;
1624         argv += optind;
1625         if (argc != 2 && argc != 3)
1626                 goto out_usage;
1627
1628         wimfile = argv[0];
1629
1630         if (!tstrcmp(wimfile, T("-"))) {
1631                 /* Attempt to apply pipable WIM from standard input.  */
1632                 if (argc == 2) {
1633                         image_num_or_name = NULL;
1634                         target = argv[1];
1635                 } else {
1636                         image_num_or_name = argv[1];
1637                         target = argv[2];
1638                 }
1639                 wim = NULL;
1640         } else {
1641                 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1642                                       imagex_progress_func);
1643                 if (ret)
1644                         goto out_free_refglobs;
1645
1646                 wimlib_get_wim_info(wim, &info);
1647
1648                 if (argc >= 3) {
1649                         /* Image explicitly specified.  */
1650                         image_num_or_name = argv[1];
1651                         image = wimlib_resolve_image(wim, image_num_or_name);
1652                         ret = verify_image_exists(image, image_num_or_name, wimfile);
1653                         if (ret)
1654                                 goto out_wimlib_free;
1655                         target = argv[2];
1656                 } else {
1657                         /* No image specified; default to image 1, but only if the WIM
1658                          * contains exactly one image.  */
1659
1660                         if (info.image_count != 1) {
1661                                 imagex_error(T("\"%"TS"\" contains %d images; "
1662                                                "Please select one (or all)."),
1663                                              wimfile, info.image_count);
1664                                 wimlib_free(wim);
1665                                 goto out_usage;
1666                         }
1667                         image = 1;
1668                         target = argv[1];
1669                 }
1670         }
1671
1672         if (refglobs.num_strings) {
1673                 if (wim == NULL) {
1674                         imagex_error(T("Can't specify --ref when applying from stdin!"));
1675                         ret = -1;
1676                         goto out_wimlib_free;
1677                 }
1678                 ret = wim_reference_globs(wim, &refglobs, open_flags);
1679                 if (ret)
1680                         goto out_wimlib_free;
1681         }
1682
1683 #ifndef __WIN32__
1684         {
1685                 /* Interpret a regular file or block device target as a NTFS
1686                  * volume.  */
1687                 struct stat stbuf;
1688
1689                 if (tstat(target, &stbuf)) {
1690                         if (errno != ENOENT) {
1691                                 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1692                                                         target);
1693                                 ret = -1;
1694                                 goto out_wimlib_free;
1695                         }
1696                 } else {
1697                         if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1698                                 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1699                 }
1700         }
1701 #endif
1702
1703         if (wim) {
1704                 ret = wimlib_extract_image(wim, image, target, extract_flags,
1705                                            imagex_progress_func);
1706         } else {
1707                 set_fd_to_binary_mode(STDIN_FILENO);
1708                 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1709                                                      image_num_or_name,
1710                                                      target, extract_flags,
1711                                                      imagex_progress_func);
1712         }
1713         if (ret == 0) {
1714                 imagex_printf(T("Done applying WIM image.\n"));
1715         } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1716                 if (wim) {
1717                         do_resource_not_found_warning(wimfile, &info, &refglobs);
1718                 } else {
1719                         imagex_error(T(        "If you are applying an image "
1720                                                "from a split pipable WIM,\n"
1721                                        "       make sure you have "
1722                                        "concatenated together all parts."));
1723                 }
1724         }
1725 out_wimlib_free:
1726         wimlib_free(wim);
1727 out_free_refglobs:
1728         string_set_destroy(&refglobs);
1729         return ret;
1730
1731 out_usage:
1732         usage(CMD_APPLY, stderr);
1733         ret = -1;
1734         goto out_free_refglobs;
1735 }
1736
1737 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1738  * directory trees.  'wimlib-imagex capture': create a new WIM file containing
1739  * the desired image.  'wimlib-imagex append': add a new image to an existing
1740  * WIM file. */
1741 static int
1742 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1743 {
1744         int c;
1745         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1746         int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1747                               WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1748                               WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1749         int write_flags = 0;
1750         int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1751         uint32_t chunk_size = UINT32_MAX;
1752         const tchar *wimfile;
1753         int wim_fd;
1754         const tchar *name;
1755         const tchar *desc;
1756         const tchar *flags_element = NULL;
1757
1758         WIMStruct *wim;
1759         STRING_SET(base_wimfiles);
1760         WIMStruct **base_wims;
1761
1762         WIMStruct *template_wim;
1763         const tchar *template_wimfile = NULL;
1764         const tchar *template_image_name_or_num = NULL;
1765         int template_image = WIMLIB_NO_IMAGE;
1766
1767         int ret;
1768         unsigned num_threads = 0;
1769
1770         tchar *source;
1771         tchar *source_copy;
1772
1773         const tchar *config_file = NULL;
1774         tchar *config_str;
1775         struct wimlib_capture_config *config;
1776
1777         bool source_list = false;
1778         size_t source_list_nchars = 0;
1779         tchar *source_list_contents;
1780         bool capture_sources_malloced;
1781         struct wimlib_capture_source *capture_sources;
1782         size_t num_sources;
1783         bool name_defaulted;
1784         bool compress_slow = false;
1785
1786         for_opt(c, capture_or_append_options) {
1787                 switch (c) {
1788                 case IMAGEX_BOOT_OPTION:
1789                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1790                         break;
1791                 case IMAGEX_CHECK_OPTION:
1792                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1793                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1794                         break;
1795                 case IMAGEX_NOCHECK_OPTION:
1796                         write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1797                         break;
1798                 case IMAGEX_CONFIG_OPTION:
1799                         config_file = optarg;
1800                         add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1801                         break;
1802                 case IMAGEX_COMPRESS_OPTION:
1803                         compression_type = get_compression_type(optarg);
1804                         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1805                                 goto out_err;
1806                         break;
1807                 case IMAGEX_COMPRESS_SLOW_OPTION:
1808                         compress_slow = true;
1809                         break;
1810                 case IMAGEX_CHUNK_SIZE_OPTION:
1811                         chunk_size = parse_chunk_size(optarg);
1812                         if (chunk_size == UINT32_MAX)
1813                                 goto out_err;
1814                         break;
1815                 case IMAGEX_PACK_STREAMS_OPTION:
1816                         write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1817                         break;
1818                 case IMAGEX_FLAGS_OPTION:
1819                         flags_element = optarg;
1820                         break;
1821                 case IMAGEX_DEREFERENCE_OPTION:
1822                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1823                         break;
1824                 case IMAGEX_VERBOSE_OPTION:
1825                         /* No longer does anything.  */
1826                         break;
1827                 case IMAGEX_THREADS_OPTION:
1828                         num_threads = parse_num_threads(optarg);
1829                         if (num_threads == UINT_MAX)
1830                                 goto out_err;
1831                         break;
1832                 case IMAGEX_REBUILD_OPTION:
1833                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1834                         break;
1835                 case IMAGEX_UNIX_DATA_OPTION:
1836                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1837                         break;
1838                 case IMAGEX_SOURCE_LIST_OPTION:
1839                         source_list = true;
1840                         break;
1841                 case IMAGEX_NO_ACLS_OPTION:
1842                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1843                         break;
1844                 case IMAGEX_STRICT_ACLS_OPTION:
1845                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1846                         break;
1847                 case IMAGEX_RPFIX_OPTION:
1848                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1849                         break;
1850                 case IMAGEX_NORPFIX_OPTION:
1851                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1852                         break;
1853                 case IMAGEX_PIPABLE_OPTION:
1854                         write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1855                         break;
1856                 case IMAGEX_NOT_PIPABLE_OPTION:
1857                         write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1858                         break;
1859                 case IMAGEX_UPDATE_OF_OPTION:
1860                         if (template_image_name_or_num) {
1861                                 imagex_error(T("'--update-of' can only be "
1862                                                "specified one time!"));
1863                                 goto out_err;
1864                         } else {
1865                                 tchar *colon;
1866                                 colon = tstrrchr(optarg, T(':'));
1867
1868                                 if (colon) {
1869                                         template_wimfile = optarg;
1870                                         *colon = T('\0');
1871                                         template_image_name_or_num = colon + 1;
1872                                 } else {
1873                                         template_wimfile = NULL;
1874                                         template_image_name_or_num = optarg;
1875                                 }
1876                         }
1877                         break;
1878                 case IMAGEX_DELTA_FROM_OPTION:
1879                         if (cmd != CMD_CAPTURE) {
1880                                 imagex_error(T("'--delta-from' is only "
1881                                                "valid for capture!"));
1882                                 goto out_usage;
1883                         }
1884                         ret = string_set_append(&base_wimfiles, optarg);
1885                         if (ret)
1886                                 goto out_free_base_wimfiles;
1887                         write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1888                         break;
1889                 default:
1890                         goto out_usage;
1891                 }
1892         }
1893         argc -= optind;
1894         argv += optind;
1895
1896         if (argc < 2 || argc > 4)
1897                 goto out_usage;
1898
1899         source = argv[0];
1900         wimfile = argv[1];
1901
1902         /* Set default compression type and parameters.  */
1903
1904
1905         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1906                 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1907
1908                 if (!compress_slow) {
1909                         struct wimlib_lzx_compressor_params params = {
1910                                 .hdr.size = sizeof(params),
1911                                 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1912                                 .use_defaults = 1,
1913                         };
1914                         wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1915                                                              &params.hdr);
1916                 }
1917         }
1918
1919         if (compress_slow)
1920                 set_compress_slow();
1921
1922         if (!tstrcmp(wimfile, T("-"))) {
1923                 /* Writing captured WIM to standard output.  */
1924         #if 0
1925                 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1926                         imagex_error("Can't write a non-pipable WIM to "
1927                                      "standard output!  Specify --pipable\n"
1928                                      "       if you want to create a pipable WIM "
1929                                      "(but read the docs first).");
1930                         goto out_err;
1931                 }
1932         #else
1933                 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1934         #endif
1935                 if (cmd == CMD_APPEND) {
1936                         imagex_error(T("Using standard output for append does "
1937                                        "not make sense."));
1938                         goto out_err;
1939                 }
1940                 wim_fd = STDOUT_FILENO;
1941                 wimfile = NULL;
1942                 imagex_info_file = stderr;
1943                 set_fd_to_binary_mode(wim_fd);
1944         }
1945
1946         /* If template image was specified using --update-of=IMAGE rather
1947          * than --update-of=WIMFILE:IMAGE, set the default WIMFILE.  */
1948         if (template_image_name_or_num && !template_wimfile) {
1949                 if (base_wimfiles.num_strings == 1) {
1950                         /* Capturing delta WIM based on single WIM:  default to
1951                          * base WIM.  */
1952                         template_wimfile = base_wimfiles.strings[0];
1953                 } else if (cmd == CMD_APPEND) {
1954                         /* Appending to WIM:  default to WIM being appended to.
1955                          */
1956                         template_wimfile = wimfile;
1957                 } else {
1958                         /* Capturing a normal (non-delta) WIM, so the WIM file
1959                          * *must* be explicitly specified.  */
1960                         if (base_wimfiles.num_strings > 1) {
1961                                 imagex_error(T("For capture of delta WIM "
1962                                                "based on multiple existing "
1963                                                "WIMs,\n"
1964                                                "      '--update-of' must "
1965                                                "specify WIMFILE:IMAGE!"));
1966                         } else {
1967                                 imagex_error(T("For capture of non-delta WIM, "
1968                                                "'--update-of' must specify "
1969                                                "WIMFILE:IMAGE!"));
1970                         }
1971                         goto out_usage;
1972                 }
1973         }
1974
1975         if (argc >= 3) {
1976                 name = argv[2];
1977                 name_defaulted = false;
1978         } else {
1979                 /* Set default name to SOURCE argument, omitting any directory
1980                  * prefixes and trailing slashes.  This requires making a copy
1981                  * of @source.  Leave some free characters at the end in case we
1982                  * append a number to keep the name unique. */
1983                 size_t source_name_len;
1984
1985                 source_name_len = tstrlen(source);
1986                 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1987                 name = tbasename(tstrcpy(source_copy, source));
1988                 name_defaulted = true;
1989         }
1990         /* Image description defaults to NULL if not given. */
1991         if (argc >= 4)
1992                 desc = argv[3];
1993         else
1994                 desc = NULL;
1995
1996         if (source_list) {
1997                 /* Set up capture sources in source list mode */
1998                 if (source[0] == T('-') && source[1] == T('\0')) {
1999                         source_list_contents = stdin_get_text_contents(&source_list_nchars);
2000                 } else {
2001                         source_list_contents = file_get_text_contents(source,
2002                                                                       &source_list_nchars);
2003                 }
2004                 if (!source_list_contents)
2005                         goto out_err;
2006
2007                 capture_sources = parse_source_list(&source_list_contents,
2008                                                     source_list_nchars,
2009                                                     &num_sources);
2010                 if (!capture_sources) {
2011                         ret = -1;
2012                         goto out_free_source_list_contents;
2013                 }
2014                 capture_sources_malloced = true;
2015         } else {
2016                 /* Set up capture source in non-source-list mode.  */
2017                 capture_sources = alloca(sizeof(struct wimlib_capture_source));
2018                 capture_sources[0].fs_source_path = source;
2019                 capture_sources[0].wim_target_path = NULL;
2020                 capture_sources[0].reserved = 0;
2021                 num_sources = 1;
2022                 capture_sources_malloced = false;
2023                 source_list_contents = NULL;
2024         }
2025
2026         if (config_file) {
2027                 /* Read and parse capture configuration file.  */
2028                 size_t config_len;
2029
2030                 config_str = file_get_text_contents(config_file, &config_len);
2031                 if (!config_str) {
2032                         ret = -1;
2033                         goto out_free_capture_sources;
2034                 }
2035
2036                 config = alloca(sizeof(*config));
2037                 ret = parse_capture_config(&config_str, config_len, config);
2038                 if (ret)
2039                         goto out_free_config;
2040         } else {
2041                 /* No capture configuration file specified; use default
2042                  * configuration for capturing Windows operating systems.  */
2043                 config = NULL;
2044                 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
2045         }
2046
2047         /* Open the existing WIM, or create a new one.  */
2048         if (cmd == CMD_APPEND)
2049                 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2050                                       imagex_progress_func);
2051         else
2052                 ret = wimlib_create_new_wim(compression_type, &wim);
2053         if (ret)
2054                 goto out_free_config;
2055
2056         /* Set chunk size if non-default.  */
2057         if (chunk_size != UINT32_MAX) {
2058                 ret = wimlib_set_output_chunk_size(wim, chunk_size);
2059                 if (ret)
2060                         goto out_free_wim;
2061         }
2062
2063 #ifndef __WIN32__
2064         /* Detect if source is regular file or block device and set NTFS volume
2065          * capture mode.  */
2066         if (!source_list) {
2067                 struct stat stbuf;
2068
2069                 if (tstat(source, &stbuf) == 0) {
2070                         if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2071                                 imagex_printf(T("Capturing WIM image from NTFS "
2072                                           "filesystem on \"%"TS"\"\n"), source);
2073                                 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2074                         }
2075                 } else {
2076                         if (errno != ENOENT) {
2077                                 imagex_error_with_errno(T("Failed to stat "
2078                                                           "\"%"TS"\""), source);
2079                                 ret = -1;
2080                                 goto out_free_wim;
2081                         }
2082                 }
2083         }
2084 #endif
2085
2086         /* If the user did not specify an image name, and the basename of the
2087          * source already exists as an image name in the WIM file, append a
2088          * suffix to make it unique. */
2089         if (cmd == CMD_APPEND && name_defaulted) {
2090                 unsigned long conflict_idx;
2091                 tchar *name_end = tstrchr(name, T('\0'));
2092                 for (conflict_idx = 1;
2093                      wimlib_image_name_in_use(wim, name);
2094                      conflict_idx++)
2095                 {
2096                         tsprintf(name_end, T(" (%lu)"), conflict_idx);
2097                 }
2098         }
2099
2100         /* If capturing a delta WIM, reference resources from the base WIMs
2101          * before adding the new image.  */
2102         if (base_wimfiles.num_strings) {
2103                 base_wims = calloc(base_wimfiles.num_strings,
2104                                    sizeof(base_wims[0]));
2105                 if (base_wims == NULL) {
2106                         imagex_error(T("Out of memory!"));
2107                         ret = -1;
2108                         goto out_free_wim;
2109                 }
2110
2111                 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2112                         ret = wimlib_open_wim(base_wimfiles.strings[i],
2113                                               open_flags, &base_wims[i],
2114                                               imagex_progress_func);
2115                         if (ret)
2116                                 goto out_free_base_wims;
2117
2118                 }
2119
2120                 ret = wimlib_reference_resources(wim, base_wims,
2121                                                  base_wimfiles.num_strings, 0);
2122                 if (ret)
2123                         goto out_free_base_wims;
2124
2125                 if (base_wimfiles.num_strings == 1) {
2126                         imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2127                                       base_wimfiles.strings[0]);
2128                 } else {
2129                         imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2130                                       base_wimfiles.num_strings);
2131                 }
2132
2133         } else {
2134                 base_wims = NULL;
2135         }
2136
2137         /* If capturing or appending as an update of an existing (template) image,
2138          * open the WIM if needed and parse the image index.  */
2139         if (template_image_name_or_num) {
2140
2141
2142                 if (base_wimfiles.num_strings == 1 &&
2143                     template_wimfile == base_wimfiles.strings[0]) {
2144                         template_wim = base_wims[0];
2145                 } else if (template_wimfile == wimfile) {
2146                         template_wim = wim;
2147                 } else {
2148                         ret = wimlib_open_wim(template_wimfile, open_flags,
2149                                               &template_wim, imagex_progress_func);
2150                         if (ret)
2151                                 goto out_free_base_wims;
2152                 }
2153
2154                 template_image = wimlib_resolve_image(template_wim,
2155                                                       template_image_name_or_num);
2156
2157                 if (template_image_name_or_num[0] == T('-')) {
2158                         tchar *tmp;
2159                         unsigned long n;
2160                         struct wimlib_wim_info info;
2161
2162                         wimlib_get_wim_info(template_wim, &info);
2163                         n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2164                         if (n >= 1 && n <= info.image_count &&
2165                             *tmp == T('\0') &&
2166                             tmp != template_image_name_or_num + 1)
2167                         {
2168                                 template_image = info.image_count - (n - 1);
2169                         }
2170                 }
2171                 ret = verify_image_exists_and_is_single(template_image,
2172                                                         template_image_name_or_num,
2173                                                         template_wimfile);
2174                 if (ret)
2175                         goto out_free_template_wim;
2176         } else {
2177                 template_wim = NULL;
2178         }
2179
2180         ret = wimlib_add_image_multisource(wim,
2181                                            capture_sources,
2182                                            num_sources,
2183                                            name,
2184                                            config,
2185                                            add_image_flags,
2186                                            imagex_progress_func);
2187         if (ret)
2188                 goto out_free_template_wim;
2189
2190         if (desc || flags_element || template_image_name_or_num) {
2191                 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2192                  * on which the added one is to be based has been specified with
2193                  * --update-of.  Get the index of the image we just
2194                  *  added, then use it to call the appropriate functions.  */
2195                 struct wimlib_wim_info info;
2196
2197                 wimlib_get_wim_info(wim, &info);
2198
2199                 if (desc) {
2200                         ret = wimlib_set_image_descripton(wim,
2201                                                           info.image_count,
2202                                                           desc);
2203                         if (ret)
2204                                 goto out_free_template_wim;
2205                 }
2206
2207                 if (flags_element) {
2208                         ret = wimlib_set_image_flags(wim, info.image_count,
2209                                                      flags_element);
2210                         if (ret)
2211                                 goto out_free_template_wim;
2212                 }
2213
2214                 /* Reference template image if the user provided one.  */
2215                 if (template_image_name_or_num) {
2216                         imagex_printf(T("Using image %d "
2217                                         "from \"%"TS"\" as template\n"),
2218                                         template_image, template_wimfile);
2219                         ret = wimlib_reference_template_image(wim,
2220                                                               info.image_count,
2221                                                               template_wim,
2222                                                               template_image,
2223                                                               0, NULL);
2224                         if (ret)
2225                                 goto out_free_template_wim;
2226                 }
2227         }
2228
2229         /* Write the new WIM or overwrite the existing WIM with the new image
2230          * appended.  */
2231         if (cmd == CMD_APPEND) {
2232                 ret = wimlib_overwrite(wim, write_flags, num_threads,
2233                                        imagex_progress_func);
2234         } else if (wimfile) {
2235                 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2236                                    write_flags, num_threads,
2237                                    imagex_progress_func);
2238         } else {
2239                 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2240                                          write_flags, num_threads,
2241                                          imagex_progress_func);
2242         }
2243 out_free_template_wim:
2244         /* template_wim may alias base_wims[0] or wim.  */
2245         if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2246             template_wim != wim)
2247                 wimlib_free(template_wim);
2248 out_free_base_wims:
2249         for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2250                 wimlib_free(base_wims[i]);
2251         free(base_wims);
2252 out_free_wim:
2253         wimlib_free(wim);
2254 out_free_config:
2255         if (config) {
2256                 free(config->exclusion_pats.pats);
2257                 free(config->exclusion_exception_pats.pats);
2258                 free(config_str);
2259         }
2260 out_free_capture_sources:
2261         if (capture_sources_malloced)
2262                 free(capture_sources);
2263 out_free_source_list_contents:
2264         free(source_list_contents);
2265 out_free_base_wimfiles:
2266         string_set_destroy(&base_wimfiles);
2267         return ret;
2268
2269 out_usage:
2270         usage(cmd, stderr);
2271 out_err:
2272         ret = -1;
2273         goto out_free_base_wimfiles;
2274 }
2275
2276 /* Remove image(s) from a WIM. */
2277 static int
2278 imagex_delete(int argc, tchar **argv, int cmd)
2279 {
2280         int c;
2281         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2282         int write_flags = 0;
2283         const tchar *wimfile;
2284         const tchar *image_num_or_name;
2285         WIMStruct *wim;
2286         int image;
2287         int ret;
2288
2289         for_opt(c, delete_options) {
2290                 switch (c) {
2291                 case IMAGEX_CHECK_OPTION:
2292                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2293                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2294                         break;
2295                 case IMAGEX_SOFT_OPTION:
2296                         write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2297                         break;
2298                 default:
2299                         goto out_usage;
2300                 }
2301         }
2302         argc -= optind;
2303         argv += optind;
2304
2305         if (argc != 2) {
2306                 if (argc < 1)
2307                         imagex_error(T("Must specify a WIM file"));
2308                 if (argc < 2)
2309                         imagex_error(T("Must specify an image"));
2310                 goto out_usage;
2311         }
2312         wimfile = argv[0];
2313         image_num_or_name = argv[1];
2314
2315         ret = wimlib_open_wim(wimfile, open_flags, &wim,
2316                               imagex_progress_func);
2317         if (ret)
2318                 goto out;
2319
2320         image = wimlib_resolve_image(wim, image_num_or_name);
2321
2322         ret = verify_image_exists(image, image_num_or_name, wimfile);
2323         if (ret)
2324                 goto out_wimlib_free;
2325
2326         ret = wimlib_delete_image(wim, image);
2327         if (ret) {
2328                 imagex_error(T("Failed to delete image from \"%"TS"\""),
2329                              wimfile);
2330                 goto out_wimlib_free;
2331         }
2332
2333         ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2334         if (ret) {
2335                 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2336                                "deleted"), wimfile);
2337         }
2338 out_wimlib_free:
2339         wimlib_free(wim);
2340 out:
2341         return ret;
2342
2343 out_usage:
2344         usage(CMD_DELETE, stderr);
2345         ret = -1;
2346         goto out;
2347 }
2348
2349 static int
2350 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2351 {
2352         int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2353         return (ret >= 0) ? 0 : -1;
2354 }
2355
2356 /* Print the files contained in an image(s) in a WIM file. */
2357 static int
2358 imagex_dir(int argc, tchar **argv, int cmd)
2359 {
2360         const tchar *wimfile;
2361         WIMStruct *wim = NULL;
2362         int image;
2363         int ret;
2364         const tchar *path = T("");
2365         int c;
2366
2367         for_opt(c, dir_options) {
2368                 switch (c) {
2369                 case IMAGEX_PATH_OPTION:
2370                         path = optarg;
2371                         break;
2372                 default:
2373                         goto out_usage;
2374                 }
2375         }
2376         argc -= optind;
2377         argv += optind;
2378
2379         if (argc < 1) {
2380                 imagex_error(T("Must specify a WIM file"));
2381                 goto out_usage;
2382         }
2383         if (argc > 2) {
2384                 imagex_error(T("Too many arguments"));
2385                 goto out_usage;
2386         }
2387
2388         wimfile = argv[0];
2389         ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2390         if (ret)
2391                 goto out;
2392
2393         if (argc >= 2) {
2394                 image = wimlib_resolve_image(wim, argv[1]);
2395                 ret = verify_image_exists(image, argv[1], wimfile);
2396                 if (ret)
2397                         goto out_wimlib_free;
2398         } else {
2399                 /* No image specified; default to image 1, but only if the WIM
2400                  * contains exactly one image.  */
2401
2402                 struct wimlib_wim_info info;
2403
2404                 wimlib_get_wim_info(wim, &info);
2405                 if (info.image_count != 1) {
2406                         imagex_error(T("\"%"TS"\" contains %d images; Please "
2407                                        "select one (or all)."),
2408                                      wimfile, info.image_count);
2409                         wimlib_free(wim);
2410                         goto out_usage;
2411                 }
2412                 image = 1;
2413         }
2414
2415         ret = wimlib_iterate_dir_tree(wim, image, path,
2416                                       WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2417                                       print_full_path, NULL);
2418 out_wimlib_free:
2419         wimlib_free(wim);
2420 out:
2421         return ret;
2422
2423 out_usage:
2424         usage(CMD_DIR, stderr);
2425         ret = -1;
2426         goto out;
2427 }
2428
2429 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2430  * WIM file. */
2431 static int
2432 imagex_export(int argc, tchar **argv, int cmd)
2433 {
2434         int c;
2435         int open_flags = 0;
2436         int export_flags = 0;
2437         int write_flags = 0;
2438         int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2439         const tchar *src_wimfile;
2440         const tchar *src_image_num_or_name;
2441         const tchar *dest_wimfile;
2442         int dest_wim_fd;
2443         const tchar *dest_name;
2444         const tchar *dest_desc;
2445         WIMStruct *src_wim;
2446         struct wimlib_wim_info src_info;
2447         WIMStruct *dest_wim;
2448         int ret;
2449         int image;
2450         struct stat stbuf;
2451         bool wim_is_new;
2452         STRING_SET(refglobs);
2453         unsigned num_threads = 0;
2454         uint32_t chunk_size = UINT32_MAX;
2455
2456         for_opt(c, export_options) {
2457                 switch (c) {
2458                 case IMAGEX_BOOT_OPTION:
2459                         export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2460                         break;
2461                 case IMAGEX_CHECK_OPTION:
2462                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2463                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2464                         break;
2465                 case IMAGEX_NOCHECK_OPTION:
2466                         write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2467                         break;
2468                 case IMAGEX_COMPRESS_OPTION:
2469                         compression_type = get_compression_type(optarg);
2470                         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2471                                 goto out_err;
2472                         break;
2473                 case IMAGEX_PACK_STREAMS_OPTION:
2474                         write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2475                         break;
2476                 case IMAGEX_CHUNK_SIZE_OPTION:
2477                         chunk_size = parse_chunk_size(optarg);
2478                         if (chunk_size == UINT32_MAX)
2479                                 goto out_err;
2480                         break;
2481                 case IMAGEX_REF_OPTION:
2482                         ret = string_set_append(&refglobs, optarg);
2483                         if (ret)
2484                                 goto out_free_refglobs;
2485                         break;
2486                 case IMAGEX_THREADS_OPTION:
2487                         num_threads = parse_num_threads(optarg);
2488                         if (num_threads == UINT_MAX)
2489                                 goto out_err;
2490                         break;
2491                 case IMAGEX_REBUILD_OPTION:
2492                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2493                         break;
2494                 case IMAGEX_PIPABLE_OPTION:
2495                         write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2496                         break;
2497                 case IMAGEX_NOT_PIPABLE_OPTION:
2498                         write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2499                         break;
2500                 default:
2501                         goto out_usage;
2502                 }
2503         }
2504         argc -= optind;
2505         argv += optind;
2506         if (argc < 3 || argc > 5)
2507                 goto out_usage;
2508         src_wimfile           = argv[0];
2509         src_image_num_or_name = argv[1];
2510         dest_wimfile          = argv[2];
2511         dest_name             = (argc >= 4) ? argv[3] : NULL;
2512         dest_desc             = (argc >= 5) ? argv[4] : NULL;
2513         ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2514                               imagex_progress_func);
2515         if (ret)
2516                 goto out_free_refglobs;
2517
2518         wimlib_get_wim_info(src_wim, &src_info);
2519
2520         /* Determine if the destination is an existing file or not.  If so, we
2521          * try to append the exported image(s) to it; otherwise, we create a new
2522          * WIM containing the exported image(s).  Furthermore, determine if we
2523          * need to write a pipable WIM directly to standard output.  */
2524
2525         if (tstrcmp(dest_wimfile, T("-")) == 0) {
2526         #if 0
2527                 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2528                         imagex_error("Can't write a non-pipable WIM to "
2529                                      "standard output!  Specify --pipable\n"
2530                                      "       if you want to create a pipable WIM "
2531                                      "(but read the docs first).");
2532                         ret = -1;
2533                         goto out_free_src_wim;
2534                 }
2535         #else
2536                 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2537         #endif
2538                 dest_wimfile = NULL;
2539                 dest_wim_fd = STDOUT_FILENO;
2540                 imagex_info_file = stderr;
2541                 set_fd_to_binary_mode(dest_wim_fd);
2542         }
2543         errno = ENOENT;
2544         if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2545                 wim_is_new = false;
2546                 /* Destination file exists. */
2547
2548                 if (!S_ISREG(stbuf.st_mode)) {
2549                         imagex_error(T("\"%"TS"\" is not a regular file"),
2550                                      dest_wimfile);
2551                         ret = -1;
2552                         goto out_free_src_wim;
2553                 }
2554                 ret = wimlib_open_wim(dest_wimfile,
2555                                       open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2556                                       &dest_wim, imagex_progress_func);
2557                 if (ret)
2558                         goto out_free_src_wim;
2559
2560                 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2561                         /* The user specified a compression type, but we're
2562                          * exporting to an existing WIM.  Make sure the
2563                          * specified compression type is the same as the
2564                          * compression type of the existing destination WIM. */
2565                         struct wimlib_wim_info dest_info;
2566
2567                         wimlib_get_wim_info(dest_wim, &dest_info);
2568                         if (compression_type != dest_info.compression_type) {
2569                                 imagex_error(T("Cannot specify a compression type that is "
2570                                                "not the same as that used in the "
2571                                                "destination WIM"));
2572                                 ret = -1;
2573                                 goto out_free_dest_wim;
2574                         }
2575                 }
2576         } else {
2577                 wim_is_new = true;
2578
2579                 if (errno != ENOENT) {
2580                         imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2581                                                 dest_wimfile);
2582                         ret = -1;
2583                         goto out_free_src_wim;
2584                 }
2585
2586                 /* dest_wimfile is not an existing file, so create a new WIM. */
2587
2588                 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2589                         /* The user did not specify a compression type; default
2590                          * to that of the source WIM.  */
2591
2592                         compression_type = src_info.compression_type;
2593                 }
2594                 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2595                 if (ret)
2596                         goto out_free_src_wim;
2597
2598                 /* Use same chunk size if compression type is the same.  */
2599                 if (compression_type == src_info.compression_type &&
2600                     chunk_size == UINT32_MAX)
2601                         wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2602         }
2603
2604         if (chunk_size != UINT32_MAX) {
2605                 /* Set destination chunk size.  */
2606                 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2607                 if (ret)
2608                         goto out_free_dest_wim;
2609         }
2610
2611         image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2612         ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2613         if (ret)
2614                 goto out_free_dest_wim;
2615
2616         if (refglobs.num_strings) {
2617                 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2618                 if (ret)
2619                         goto out_free_dest_wim;
2620         }
2621
2622         if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2623             image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2624         {
2625                 imagex_error(T("--boot specified for all-images export, but source WIM "
2626                                "has no bootable image."));
2627                 ret = -1;
2628                 goto out_free_dest_wim;
2629         }
2630
2631         ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2632                                   dest_desc, export_flags, imagex_progress_func);
2633         if (ret) {
2634                 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2635                         do_resource_not_found_warning(src_wimfile,
2636                                                       &src_info, &refglobs);
2637                 }
2638                 goto out_free_dest_wim;
2639         }
2640
2641         if (!wim_is_new)
2642                 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2643                                        imagex_progress_func);
2644         else if (dest_wimfile)
2645                 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2646                                    write_flags, num_threads,
2647                                    imagex_progress_func);
2648         else
2649                 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2650                                          WIMLIB_ALL_IMAGES, write_flags,
2651                                          num_threads, imagex_progress_func);
2652 out_free_dest_wim:
2653         wimlib_free(dest_wim);
2654 out_free_src_wim:
2655         wimlib_free(src_wim);
2656 out_free_refglobs:
2657         string_set_destroy(&refglobs);
2658         return ret;
2659
2660 out_usage:
2661         usage(CMD_EXPORT, stderr);
2662 out_err:
2663         ret = -1;
2664         goto out_free_refglobs;
2665 }
2666
2667 static bool
2668 is_root_wim_path(const tchar *path)
2669 {
2670         const tchar *p;
2671         for (p = path; *p; p++)
2672                 if (*p != T('\\') && *p != T('/'))
2673                         return false;
2674         return true;
2675 }
2676
2677 static void
2678 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2679                       const tchar *dest_dir)
2680 {
2681         for (size_t i = 0; i < num_cmds; i++)
2682                 if (cmds[i].fs_dest_path != dest_dir)
2683                         free(cmds[i].fs_dest_path);
2684         free(cmds);
2685 }
2686
2687 static struct wimlib_extract_command *
2688 prepare_extract_commands(tchar **paths, unsigned num_paths,
2689                          int extract_flags, tchar *dest_dir,
2690                          size_t *num_cmds_ret)
2691 {
2692         struct wimlib_extract_command *cmds;
2693         size_t num_cmds;
2694         tchar *emptystr = T("");
2695
2696         if (num_paths == 0) {
2697                 num_paths = 1;
2698                 paths = &emptystr;
2699         }
2700         num_cmds = num_paths;
2701         cmds = calloc(num_cmds, sizeof(cmds[0]));
2702         if (!cmds) {
2703                 imagex_error(T("Out of memory!"));
2704                 return NULL;
2705         }
2706
2707         for (size_t i = 0; i < num_cmds; i++) {
2708                 cmds[i].extract_flags = extract_flags;
2709                 cmds[i].wim_source_path = paths[i];
2710                 if (is_root_wim_path(paths[i])) {
2711                         cmds[i].fs_dest_path = dest_dir;
2712                 } else {
2713                         size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2714                         cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2715                         if (!cmds[i].fs_dest_path) {
2716                                 free_extract_commands(cmds, num_cmds, dest_dir);
2717                                 return NULL;
2718                         }
2719                         tsprintf(cmds[i].fs_dest_path,
2720                                  T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2721                                  dest_dir, tbasename(paths[i]));
2722                 }
2723         }
2724         *num_cmds_ret = num_cmds;
2725         return cmds;
2726 }
2727
2728 /* Extract files or directories from a WIM image */
2729 static int
2730 imagex_extract(int argc, tchar **argv, int cmd)
2731 {
2732         int c;
2733         int open_flags = 0;
2734         int image;
2735         WIMStruct *wim;
2736         int ret;
2737         const tchar *wimfile;
2738         const tchar *image_num_or_name;
2739         const tchar *pathlist;
2740         tchar *dest_dir = T(".");
2741         int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX;
2742         int listfile_extract_flags = WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2743
2744         STRING_SET(refglobs);
2745
2746         struct wimlib_extract_command *cmds;
2747         size_t num_cmds;
2748
2749         for_opt(c, extract_options) {
2750                 switch (c) {
2751                 case IMAGEX_CHECK_OPTION:
2752                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2753                         break;
2754                 case IMAGEX_VERBOSE_OPTION:
2755                         /* No longer does anything.  */
2756                         break;
2757                 case IMAGEX_REF_OPTION:
2758                         ret = string_set_append(&refglobs, optarg);
2759                         if (ret)
2760                                 goto out_free_refglobs;
2761                         break;
2762                 case IMAGEX_UNIX_DATA_OPTION:
2763                         extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2764                         break;
2765                 case IMAGEX_NO_ACLS_OPTION:
2766                         extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2767                         break;
2768                 case IMAGEX_STRICT_ACLS_OPTION:
2769                         extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2770                         break;
2771                 case IMAGEX_DEST_DIR_OPTION:
2772                         dest_dir = optarg;
2773                         break;
2774                 case IMAGEX_TO_STDOUT_OPTION:
2775                         extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2776                         imagex_info_file = stderr;
2777                         imagex_be_quiet = true;
2778                         break;
2779                 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2780                         extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2781                         extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2782                         break;
2783                 case IMAGEX_NO_WILDCARDS_OPTION:
2784                         listfile_extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2785                         break;
2786                 case IMAGEX_STRICT_WILDCARDS_OPTION:
2787                         listfile_extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2788                         break;
2789                 default:
2790                         goto out_usage;
2791                 }
2792         }
2793         argc -= optind;
2794         argv += optind;
2795
2796         if (argc < 2)
2797                 goto out_usage;
2798
2799         wimfile = argv[0];
2800         image_num_or_name = argv[1];
2801
2802         argc -= 2;
2803         argv += 2;
2804
2805         if (argc == 1 && argv[0][0] == T('@')) {
2806                 pathlist = argv[0] + 1;
2807                 cmds = NULL;
2808                 num_cmds = 0;
2809         } else {
2810                 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2811                                                 &num_cmds);
2812                 if (cmds == NULL)
2813                         goto out_err;
2814                 pathlist = NULL;
2815         }
2816
2817         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2818         if (ret)
2819                 goto out_free_cmds;
2820
2821         image = wimlib_resolve_image(wim, image_num_or_name);
2822         ret = verify_image_exists_and_is_single(image,
2823                                                 image_num_or_name,
2824                                                 wimfile);
2825         if (ret)
2826                 goto out_wimlib_free;
2827
2828         if (refglobs.num_strings) {
2829                 ret = wim_reference_globs(wim, &refglobs, open_flags);
2830                 if (ret)
2831                         goto out_wimlib_free;
2832         }
2833
2834         ret = 0;
2835         if (ret == 0 && cmds != NULL) {
2836                 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2837                                            imagex_progress_func);
2838         }
2839         if (ret == 0 && pathlist != NULL) {
2840                 ret = wimlib_extract_pathlist(wim, image, dest_dir,
2841                                               pathlist,
2842                                               extract_flags | listfile_extract_flags,
2843                                               imagex_progress_func);
2844         }
2845         if (ret == 0) {
2846                 if (!imagex_be_quiet)
2847                         imagex_printf(T("Done extracting files.\n"));
2848         } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2849                 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2850                                    "files and directories\n"
2851                                    "      are in the WIM image.\n"),
2852                                 get_cmd_string(CMD_DIR, false));
2853         } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2854                 struct wimlib_wim_info info;
2855
2856                 wimlib_get_wim_info(wim, &info);
2857                 do_resource_not_found_warning(wimfile, &info, &refglobs);
2858         }
2859 out_wimlib_free:
2860         wimlib_free(wim);
2861 out_free_cmds:
2862         free_extract_commands(cmds, num_cmds, dest_dir);
2863 out_free_refglobs:
2864         string_set_destroy(&refglobs);
2865         return ret;
2866
2867 out_usage:
2868         usage(CMD_EXTRACT, stderr);
2869 out_err:
2870         ret = -1;
2871         goto out_free_refglobs;
2872 }
2873
2874 static void print_byte_field(const uint8_t field[], size_t len)
2875 {
2876         while (len--)
2877                 tprintf(T("%02hhx"), *field++);
2878 }
2879
2880 static void
2881 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2882 {
2883         tputs(T("WIM Information:"));
2884         tputs(T("----------------"));
2885         tprintf(T("Path:           %"TS"\n"), wimfile);
2886         tprintf(T("GUID:           0x"));
2887         print_byte_field(info->guid, sizeof(info->guid));
2888         tputchar(T('\n'));
2889         tprintf(T("Version:        %u\n"), info->wim_version);
2890         tprintf(T("Image Count:    %d\n"), info->image_count);
2891         tprintf(T("Compression:    %"TS"\n"),
2892                 wimlib_get_compression_type_string(info->compression_type));
2893         tprintf(T("Chunk Size:     %"PRIu32" bytes\n"),
2894                 info->chunk_size);
2895         tprintf(T("Part Number:    %d/%d\n"), info->part_number, info->total_parts);
2896         tprintf(T("Boot Index:     %d\n"), info->boot_index);
2897         tprintf(T("Size:           %"PRIu64" bytes\n"), info->total_bytes);
2898         tprintf(T("Integrity Info: %"TS"\n"),
2899                 info->has_integrity_table ? T("yes") : T("no"));
2900         tprintf(T("Relative path junction: %"TS"\n"),
2901                 info->has_rpfix ? T("yes") : T("no"));
2902         tprintf(T("Pipable:        %"TS"\n"),
2903                 info->pipable ? T("yes") : T("no"));
2904         tputchar(T('\n'));
2905 }
2906
2907 static int
2908 print_resource(const struct wimlib_resource_entry *resource,
2909                void *_ignore)
2910 {
2911         tprintf(T("Uncompressed size     = %"PRIu64" bytes\n"),
2912                 resource->uncompressed_size);
2913         if (resource->packed) {
2914                 tprintf(T("Raw compressed size   = %"PRIu64" bytes\n"),
2915                         resource->raw_resource_compressed_size);
2916
2917                 tprintf(T("Raw offset in WIM     = %"PRIu64" bytes\n"),
2918                         resource->raw_resource_offset_in_wim);
2919
2920                 tprintf(T("Offset in raw         = %"PRIu64" bytes\n"),
2921                         resource->offset);
2922         } else {
2923                 tprintf(T("Compressed size       = %"PRIu64" bytes\n"),
2924                         resource->compressed_size);
2925
2926                 tprintf(T("Offset in WIM         = %"PRIu64" bytes\n"),
2927                         resource->offset);
2928         }
2929
2930         tprintf(T("Part Number           = %u\n"), resource->part_number);
2931         tprintf(T("Reference Count       = %u\n"), resource->reference_count);
2932
2933         tprintf(T("Hash                  = 0x"));
2934         print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2935         tputchar(T('\n'));
2936
2937         tprintf(T("Flags                 = "));
2938         if (resource->is_compressed)
2939                 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED  "));
2940         if (resource->is_metadata)
2941                 tprintf(T("WIM_RESHDR_FLAG_METADATA  "));
2942         if (resource->is_free)
2943                 tprintf(T("WIM_RESHDR_FLAG_FREE  "));
2944         if (resource->is_spanned)
2945                 tprintf(T("WIM_RESHDR_FLAG_SPANNED  "));
2946         if (resource->packed)
2947                 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS  "));
2948         tputchar(T('\n'));
2949         tputchar(T('\n'));
2950         return 0;
2951 }
2952
2953 static void
2954 print_lookup_table(WIMStruct *wim)
2955 {
2956         wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2957 }
2958
2959 /* Prints information about a WIM file; also can mark an image as bootable,
2960  * change the name of an image, or change the description of an image. */
2961 static int
2962 imagex_info(int argc, tchar **argv, int cmd)
2963 {
2964         int c;
2965         bool boot         = false;
2966         bool check        = false;
2967         bool nocheck      = false;
2968         bool header       = false;
2969         bool lookup_table = false;
2970         bool xml          = false;
2971         bool metadata     = false;
2972         bool short_header = true;
2973         const tchar *xml_out_file = NULL;
2974         const tchar *wimfile;
2975         const tchar *image_num_or_name;
2976         const tchar *new_name;
2977         const tchar *new_desc;
2978         WIMStruct *wim;
2979         int image;
2980         int ret;
2981         int open_flags = 0;
2982         struct wimlib_wim_info info;
2983
2984         for_opt(c, info_options) {
2985                 switch (c) {
2986                 case IMAGEX_BOOT_OPTION:
2987                         boot = true;
2988                         break;
2989                 case IMAGEX_CHECK_OPTION:
2990                         check = true;
2991                         break;
2992                 case IMAGEX_NOCHECK_OPTION:
2993                         nocheck = true;
2994                         break;
2995                 case IMAGEX_HEADER_OPTION:
2996                         header = true;
2997                         short_header = false;
2998                         break;
2999                 case IMAGEX_LOOKUP_TABLE_OPTION:
3000                         lookup_table = true;
3001                         short_header = false;
3002                         break;
3003                 case IMAGEX_XML_OPTION:
3004                         xml = true;
3005                         short_header = false;
3006                         break;
3007                 case IMAGEX_EXTRACT_XML_OPTION:
3008                         xml_out_file = optarg;
3009                         short_header = false;
3010                         break;
3011                 case IMAGEX_METADATA_OPTION:
3012                         metadata = true;
3013                         short_header = false;
3014                         break;
3015                 default:
3016                         goto out_usage;
3017                 }
3018         }
3019
3020         argc -= optind;
3021         argv += optind;
3022         if (argc < 1 || argc > 4)
3023                 goto out_usage;
3024
3025         wimfile           = argv[0];
3026         image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3027         new_name          = (argc >= 3) ? argv[2] : NULL;
3028         new_desc          = (argc >= 4) ? argv[3] : NULL;
3029
3030         if (check && nocheck) {
3031                 imagex_error(T("Can't specify both --check and --nocheck"));
3032                 goto out_err;
3033         }
3034
3035         if (check)
3036                 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3037
3038         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3039         if (ret)
3040                 goto out;
3041
3042         wimlib_get_wim_info(wim, &info);
3043
3044         image = wimlib_resolve_image(wim, image_num_or_name);
3045         ret = WIMLIB_ERR_INVALID_IMAGE;
3046         if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3047                 verify_image_exists(image, image_num_or_name, wimfile);
3048                 if (boot) {
3049                         imagex_error(T("If you would like to set the boot "
3050                                        "index to 0, specify image \"0\" with "
3051                                        "the --boot flag."));
3052                 }
3053                 goto out_wimlib_free;
3054         }
3055
3056         if (boot && info.image_count == 0) {
3057                 imagex_error(T("--boot is meaningless on a WIM with no images"));
3058                 goto out_wimlib_free;
3059         }
3060
3061         if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3062                 if (boot) {
3063                         imagex_error(T("Cannot specify the --boot flag "
3064                                        "without specifying a specific "
3065                                        "image in a multi-image WIM"));
3066                         goto out_wimlib_free;
3067                 }
3068                 if (new_name) {
3069                         imagex_error(T("Cannot specify the NEW_NAME "
3070                                        "without specifying a specific "
3071                                        "image in a multi-image WIM"));
3072                         goto out_wimlib_free;
3073                 }
3074         }
3075
3076         /* Operations that print information are separated from operations that
3077          * recreate the WIM file. */
3078         if (!new_name && !boot) {
3079
3080                 /* Read-only operations */
3081
3082                 if (image == WIMLIB_NO_IMAGE) {
3083                         imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3084                                      image_num_or_name, wimfile);
3085                         goto out_wimlib_free;
3086                 }
3087
3088                 if (image == WIMLIB_ALL_IMAGES && short_header)
3089                         print_wim_information(wimfile, &info);
3090
3091                 if (header)
3092                         wimlib_print_header(wim);
3093
3094                 if (lookup_table) {
3095                         if (info.total_parts != 1) {
3096                                 tfprintf(stderr, T("Warning: Only showing the lookup table "
3097                                                    "for part %d of a %d-part WIM.\n"),
3098                                          info.part_number, info.total_parts);
3099                         }
3100                         print_lookup_table(wim);
3101                 }
3102
3103                 if (xml) {
3104                         ret = wimlib_extract_xml_data(wim, stdout);
3105                         if (ret)
3106                                 goto out_wimlib_free;
3107                 }
3108
3109                 if (xml_out_file) {
3110                         FILE *fp;
3111
3112                         fp = tfopen(xml_out_file, T("wb"));
3113                         if (!fp) {
3114                                 imagex_error_with_errno(T("Failed to open the "
3115                                                           "file \"%"TS"\" for "
3116                                                           "writing"),
3117                                                         xml_out_file);
3118                                 ret = -1;
3119                                 goto out_wimlib_free;
3120                         }
3121                         ret = wimlib_extract_xml_data(wim, fp);
3122                         if (fclose(fp)) {
3123                                 imagex_error(T("Failed to close the file "
3124                                                "\"%"TS"\""),
3125                                              xml_out_file);
3126                                 ret = -1;
3127                         }
3128                         if (ret)
3129                                 goto out_wimlib_free;
3130                 }
3131
3132                 if (short_header)
3133                         wimlib_print_available_images(wim, image);
3134
3135                 if (metadata) {
3136                         ret = wimlib_print_metadata(wim, image);
3137                         if (ret)
3138                                 goto out_wimlib_free;
3139                 }
3140                 ret = 0;
3141         } else {
3142
3143                 /* Modification operations */
3144
3145                 if (image == WIMLIB_ALL_IMAGES)
3146                         image = 1;
3147
3148                 if (image == WIMLIB_NO_IMAGE && new_name) {
3149                         imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3150                                        "when using image 0"), new_name);
3151                         ret = -1;
3152                         goto out_wimlib_free;
3153                 }
3154
3155                 if (boot) {
3156                         if (image == info.boot_index) {
3157                                 imagex_printf(T("Image %d is already marked as "
3158                                           "bootable.\n"), image);
3159                                 boot = false;
3160                         } else {
3161                                 imagex_printf(T("Marking image %d as bootable.\n"),
3162                                         image);
3163                                 info.boot_index = image;
3164                                 ret = wimlib_set_wim_info(wim, &info,
3165                                                           WIMLIB_CHANGE_BOOT_INDEX);
3166                                 if (ret)
3167                                         goto out_wimlib_free;
3168                         }
3169                 }
3170                 if (new_name) {
3171                         if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3172                         {
3173                                 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3174                                         image, new_name);
3175                                 new_name = NULL;
3176                         } else {
3177                                 imagex_printf(T("Changing the name of image %d to "
3178                                           "\"%"TS"\".\n"), image, new_name);
3179                                 ret = wimlib_set_image_name(wim, image, new_name);
3180                                 if (ret)
3181                                         goto out_wimlib_free;
3182                         }
3183                 }
3184                 if (new_desc) {
3185                         const tchar *old_desc;
3186                         old_desc = wimlib_get_image_description(wim, image);
3187                         if (old_desc && !tstrcmp(old_desc, new_desc)) {
3188                                 imagex_printf(T("The description of image %d is already "
3189                                           "\"%"TS"\".\n"), image, new_desc);
3190                                 new_desc = NULL;
3191                         } else {
3192                                 imagex_printf(T("Changing the description of image %d "
3193                                           "to \"%"TS"\".\n"), image, new_desc);
3194                                 ret = wimlib_set_image_descripton(wim, image,
3195                                                                   new_desc);
3196                                 if (ret)
3197                                         goto out_wimlib_free;
3198                         }
3199                 }
3200
3201                 /* Only call wimlib_overwrite() if something actually needs to
3202                  * be changed.  */
3203                 if (boot || new_name || new_desc ||
3204                     (check && !info.has_integrity_table) ||
3205                     (nocheck && info.has_integrity_table))
3206                 {
3207                         int write_flags = 0;
3208
3209                         if (check)
3210                                 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3211                         if (nocheck)
3212                                 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3213                         ret = wimlib_overwrite(wim, write_flags, 1,
3214                                                imagex_progress_func);
3215                 } else {
3216                         imagex_printf(T("The file \"%"TS"\" was not modified "
3217                                         "because nothing needed to be done.\n"),
3218                                       wimfile);
3219                         ret = 0;
3220                 }
3221         }
3222 out_wimlib_free:
3223         wimlib_free(wim);
3224 out:
3225         return ret;
3226
3227 out_usage:
3228         usage(CMD_INFO, stderr);
3229 out_err:
3230         ret = -1;
3231         goto out;
3232 }
3233
3234 /* Join split WIMs into one part WIM */
3235 static int
3236 imagex_join(int argc, tchar **argv, int cmd)
3237 {
3238         int c;
3239         int swm_open_flags = 0;
3240         int wim_write_flags = 0;
3241         const tchar *output_path;
3242         int ret;
3243
3244         for_opt(c, join_options) {
3245                 switch (c) {
3246                 case IMAGEX_CHECK_OPTION:
3247                         swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3248                         wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3249                         break;
3250                 default:
3251                         goto out_usage;
3252                 }
3253         }
3254         argc -= optind;
3255         argv += optind;
3256
3257         if (argc < 2) {
3258                 imagex_error(T("Must specify one or more split WIM (.swm) "
3259                                "parts to join"));
3260                 goto out_usage;
3261         }
3262         output_path = argv[0];
3263         ret = wimlib_join((const tchar * const *)++argv,
3264                           --argc,
3265                           output_path,
3266                           swm_open_flags,
3267                           wim_write_flags,
3268                           imagex_progress_func);
3269 out:
3270         return ret;
3271
3272 out_usage:
3273         usage(CMD_JOIN, stderr);
3274         ret = -1;
3275         goto out;
3276 }
3277
3278 #if WIM_MOUNTING_SUPPORTED
3279
3280 /* Mounts a WIM image.  */
3281 static int
3282 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3283 {
3284         int c;
3285         int mount_flags = 0;
3286         int open_flags = 0;
3287         const tchar *staging_dir = NULL;
3288         const tchar *wimfile;
3289         const tchar *dir;
3290         WIMStruct *wim;
3291         struct wimlib_wim_info info;
3292         int image;
3293         int ret;
3294
3295         STRING_SET(refglobs);
3296
3297         if (cmd == CMD_MOUNTRW) {
3298                 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3299                 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3300         }
3301
3302         for_opt(c, mount_options) {
3303                 switch (c) {
3304                 case IMAGEX_ALLOW_OTHER_OPTION:
3305                         mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3306                         break;
3307                 case IMAGEX_CHECK_OPTION:
3308                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3309                         break;
3310                 case IMAGEX_DEBUG_OPTION:
3311                         mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3312                         break;
3313                 case IMAGEX_STREAMS_INTERFACE_OPTION:
3314                         if (!tstrcasecmp(optarg, T("none")))
3315                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3316                         else if (!tstrcasecmp(optarg, T("xattr")))
3317                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3318                         else if (!tstrcasecmp(optarg, T("windows")))
3319                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3320                         else {
3321                                 imagex_error(T("Unknown stream interface \"%"TS"\""),
3322                                              optarg);
3323                                 goto out_usage;
3324                         }
3325                         break;
3326                 case IMAGEX_REF_OPTION:
3327                         ret = string_set_append(&refglobs, optarg);
3328                         if (ret)
3329                                 goto out_free_refglobs;
3330                         break;
3331                 case IMAGEX_STAGING_DIR_OPTION:
3332                         staging_dir = optarg;
3333                         break;
3334                 case IMAGEX_UNIX_DATA_OPTION:
3335                         mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3336                         break;
3337                 default:
3338                         goto out_usage;
3339                 }
3340         }
3341         argc -= optind;
3342         argv += optind;
3343         if (argc != 2 && argc != 3)
3344                 goto out_usage;
3345
3346         wimfile = argv[0];
3347
3348         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3349         if (ret)
3350                 goto out_free_refglobs;
3351
3352         wimlib_get_wim_info(wim, &info);
3353
3354         if (argc >= 3) {
3355                 /* Image explicitly specified.  */
3356                 image = wimlib_resolve_image(wim, argv[1]);
3357                 dir = argv[2];
3358                 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3359                 if (ret)
3360                         goto out_free_wim;
3361         } else {
3362                 /* No image specified; default to image 1, but only if the WIM
3363                  * contains exactly one image.  */
3364
3365                 if (info.image_count != 1) {
3366                         imagex_error(T("\"%"TS"\" contains %d images; Please "
3367                                        "select one."), wimfile, info.image_count);
3368                         wimlib_free(wim);
3369                         goto out_usage;
3370                 }
3371                 image = 1;
3372                 dir = argv[1];
3373         }
3374
3375         if (refglobs.num_strings) {
3376                 ret = wim_reference_globs(wim, &refglobs, open_flags);
3377                 if (ret)
3378                         goto out_free_wim;
3379         }
3380
3381         ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3382         if (ret) {
3383                 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3384                                "on \"%"TS"\""),
3385                              image, wimfile, dir);
3386         }
3387 out_free_wim:
3388         wimlib_free(wim);
3389 out_free_refglobs:
3390         string_set_destroy(&refglobs);
3391         return ret;
3392
3393 out_usage:
3394         usage(cmd, stderr);
3395         ret = -1;
3396         goto out_free_refglobs;
3397 }
3398 #endif /* WIM_MOUNTING_SUPPORTED */
3399
3400 /* Rebuild a WIM file */
3401 static int
3402 imagex_optimize(int argc, tchar **argv, int cmd)
3403 {
3404         int c;
3405         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3406         int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3407         int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3408         uint32_t chunk_size = UINT32_MAX;
3409         int ret;
3410         WIMStruct *wim;
3411         const tchar *wimfile;
3412         off_t old_size;
3413         off_t new_size;
3414         unsigned num_threads = 0;
3415
3416         for_opt(c, optimize_options) {
3417                 switch (c) {
3418                 case IMAGEX_CHECK_OPTION:
3419                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3420                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3421                         break;
3422                 case IMAGEX_NOCHECK_OPTION:
3423                         write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3424                         break;
3425                 case IMAGEX_COMPRESS_OPTION:
3426                         write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3427                         compression_type = get_compression_type(optarg);
3428                         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3429                                 goto out_err;
3430                         break;
3431                 case IMAGEX_RECOMPRESS_OPTION:
3432                         write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3433                         break;
3434                 case IMAGEX_COMPRESS_SLOW_OPTION:
3435                         write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3436                         set_compress_slow();
3437                         break;
3438                 case IMAGEX_CHUNK_SIZE_OPTION:
3439                         chunk_size = parse_chunk_size(optarg);
3440                         if (chunk_size == UINT32_MAX)
3441                                 goto out_err;
3442                         break;
3443                 case IMAGEX_PACK_STREAMS_OPTION:
3444                         write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3445                         write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3446                         break;
3447                 case IMAGEX_THREADS_OPTION:
3448                         num_threads = parse_num_threads(optarg);
3449                         if (num_threads == UINT_MAX)
3450                                 goto out_err;
3451                         break;
3452                 case IMAGEX_PIPABLE_OPTION:
3453                         write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3454                         break;
3455                 case IMAGEX_NOT_PIPABLE_OPTION:
3456                         write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3457                         break;
3458                 default:
3459                         goto out_usage;
3460                 }
3461         }
3462         argc -= optind;
3463         argv += optind;
3464
3465         if (argc != 1)
3466                 goto out_usage;
3467
3468         wimfile = argv[0];
3469
3470         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3471         if (ret)
3472                 goto out;
3473
3474         if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3475                 /* Change compression type.  */
3476                 ret = wimlib_set_output_compression_type(wim, compression_type);
3477                 if (ret)
3478                         goto out_wimlib_free;
3479         }
3480
3481         if (chunk_size != UINT32_MAX) {
3482                 /* Change chunk size.  */
3483                 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3484                 if (ret)
3485                         goto out_wimlib_free;
3486         }
3487
3488         old_size = file_get_size(wimfile);
3489         tprintf(T("\"%"TS"\" original size: "), wimfile);
3490         if (old_size == -1)
3491                 tputs(T("Unknown"));
3492         else
3493                 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3494
3495         ret = wimlib_overwrite(wim, write_flags, num_threads,
3496                                imagex_progress_func);
3497         if (ret) {
3498                 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3499                 goto out_wimlib_free;
3500         }
3501
3502         new_size = file_get_size(wimfile);
3503         tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3504         if (new_size == -1)
3505                 tputs(T("Unknown"));
3506         else
3507                 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3508
3509         tfputs(T("Space saved: "), stdout);
3510         if (new_size != -1 && old_size != -1) {
3511                 tprintf(T("%lld KiB\n"),
3512                        ((long long)old_size - (long long)new_size) >> 10);
3513         } else {
3514                 tputs(T("Unknown"));
3515         }
3516         ret = 0;
3517 out_wimlib_free:
3518         wimlib_free(wim);
3519 out:
3520         return ret;
3521
3522 out_usage:
3523         usage(CMD_OPTIMIZE, stderr);
3524 out_err:
3525         ret = -1;
3526         goto out;
3527 }
3528
3529 /* Split a WIM into a spanned set */
3530 static int
3531 imagex_split(int argc, tchar **argv, int cmd)
3532 {
3533         int c;
3534         int open_flags = 0;
3535         int write_flags = 0;
3536         unsigned long part_size;
3537         tchar *tmp;
3538         int ret;
3539         WIMStruct *wim;
3540
3541         for_opt(c, split_options) {
3542                 switch (c) {
3543                 case IMAGEX_CHECK_OPTION:
3544                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3545                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3546                         break;
3547                 default:
3548                         goto out_usage;
3549                 }
3550         }
3551         argc -= optind;
3552         argv += optind;
3553
3554         if (argc != 3)
3555                 goto out_usage;
3556
3557         part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3558         if (tmp == argv[2] || *tmp) {
3559                 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3560                 imagex_error(T("The part size must be an integer or "
3561                                "floating-point number of megabytes."));
3562                 goto out_err;
3563         }
3564         ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3565         if (ret)
3566                 goto out;
3567
3568         ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3569         wimlib_free(wim);
3570 out:
3571         return ret;
3572
3573 out_usage:
3574         usage(CMD_SPLIT, stderr);
3575 out_err:
3576         ret = -1;
3577         goto out;
3578 }
3579
3580 #if WIM_MOUNTING_SUPPORTED
3581 /* Unmounts a mounted WIM image. */
3582 static int
3583 imagex_unmount(int argc, tchar **argv, int cmd)
3584 {
3585         int c;
3586         int unmount_flags = 0;
3587         int ret;
3588
3589         for_opt(c, unmount_options) {
3590                 switch (c) {
3591                 case IMAGEX_COMMIT_OPTION:
3592                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3593                         break;
3594                 case IMAGEX_CHECK_OPTION:
3595                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3596                         break;
3597                 case IMAGEX_REBUILD_OPTION:
3598                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3599                         break;
3600                 case IMAGEX_LAZY_OPTION:
3601                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3602                         break;
3603                 default:
3604                         goto out_usage;
3605                 }
3606         }
3607         argc -= optind;
3608         argv += optind;
3609         if (argc != 1)
3610                 goto out_usage;
3611
3612         ret = wimlib_unmount_image(argv[0], unmount_flags,
3613                                    imagex_progress_func);
3614         if (ret)
3615                 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3616 out:
3617         return ret;
3618
3619 out_usage:
3620         usage(CMD_UNMOUNT, stderr);
3621         ret = -1;
3622         goto out;
3623 }
3624 #endif /* WIM_MOUNTING_SUPPORTED */
3625
3626 /*
3627  * Add, delete, or rename files in a WIM image.
3628  */
3629 static int
3630 imagex_update(int argc, tchar **argv, int cmd)
3631 {
3632         const tchar *wimfile;
3633         int image;
3634         WIMStruct *wim;
3635         int ret;
3636         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3637         int write_flags = 0;
3638         int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3639         int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3640                                 WIMLIB_ADD_FLAG_VERBOSE;
3641         int default_delete_flags = 0;
3642         unsigned num_threads = 0;
3643         int c;
3644         tchar *cmd_file_contents;
3645         size_t cmd_file_nchars;
3646         struct wimlib_update_command *cmds;
3647         size_t num_cmds;
3648         tchar *command_str = NULL;
3649
3650         const tchar *config_file = NULL;
3651         tchar *config_str;
3652         struct wimlib_capture_config *config;
3653
3654         for_opt(c, update_options) {
3655                 switch (c) {
3656                 /* Generic or write options */
3657                 case IMAGEX_THREADS_OPTION:
3658                         num_threads = parse_num_threads(optarg);
3659                         if (num_threads == UINT_MAX)
3660                                 goto out_err;
3661                         break;
3662                 case IMAGEX_CHECK_OPTION:
3663                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3664                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3665                         break;
3666                 case IMAGEX_REBUILD_OPTION:
3667                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3668                         break;
3669                 case IMAGEX_COMMAND_OPTION:
3670                         if (command_str) {
3671                                 imagex_error(T("--command may only be specified "
3672                                                "one time.  Please provide\n"
3673                                                "       the update commands "
3674                                                "on standard input instead."));
3675                                 goto out_err;
3676                         }
3677                         command_str = tstrdup(optarg);
3678                         if (!command_str) {
3679                                 imagex_error(T("Out of memory!"));
3680                                 goto out_err;
3681                         }
3682                         break;
3683                 /* Default delete options */
3684                 case IMAGEX_FORCE_OPTION:
3685                         default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3686                         break;
3687                 case IMAGEX_RECURSIVE_OPTION:
3688                         default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3689                         break;
3690
3691                 /* Global add option */
3692                 case IMAGEX_CONFIG_OPTION:
3693                         default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3694                         config_file = optarg;
3695                         break;
3696
3697                 /* Default add options */
3698                 case IMAGEX_VERBOSE_OPTION:
3699                         /* No longer does anything.  */
3700                         break;
3701                 case IMAGEX_DEREFERENCE_OPTION:
3702                         default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3703                         break;
3704                 case IMAGEX_UNIX_DATA_OPTION:
3705                         default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3706                         break;
3707                 case IMAGEX_NO_ACLS_OPTION:
3708                         default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3709                         break;
3710                 case IMAGEX_STRICT_ACLS_OPTION:
3711                         default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3712                         break;
3713                 default:
3714                         goto out_usage;
3715                 }
3716         }
3717         argv += optind;
3718         argc -= optind;
3719
3720         if (argc != 1 && argc != 2)
3721                 goto out_usage;
3722         wimfile = argv[0];
3723
3724         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3725         if (ret)
3726                 goto out_free_command_str;
3727
3728         if (argc >= 2) {
3729                 /* Image explicitly specified.  */
3730                 image = wimlib_resolve_image(wim, argv[1]);
3731                 ret = verify_image_exists_and_is_single(image, argv[1],
3732                                                         wimfile);
3733                 if (ret)
3734                         goto out_wimlib_free;
3735         } else {
3736                 /* No image specified; default to image 1, but only if the WIM
3737                  * contains exactly one image.  */
3738                 struct wimlib_wim_info info;
3739
3740                 wimlib_get_wim_info(wim, &info);
3741                 if (info.image_count != 1) {
3742                         imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3743                                      wimfile, info.image_count);
3744                         wimlib_free(wim);
3745                         goto out_usage;
3746                 }
3747                 image = 1;
3748         }
3749
3750         /* Parse capture configuration file if specified */
3751         if (config_file) {
3752                 size_t config_len;
3753
3754                 config_str = file_get_text_contents(config_file, &config_len);
3755                 if (!config_str) {
3756                         ret = -1;
3757                         goto out_wimlib_free;
3758                 }
3759
3760                 config = alloca(sizeof(*config));
3761                 ret = parse_capture_config(&config_str, config_len, config);
3762                 if (ret)
3763                         goto out_free_config;
3764         } else {
3765                 config = NULL;
3766                 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3767         }
3768
3769         /* Read update commands from standard input, or the command string if
3770          * specified.  */
3771         if (command_str) {
3772                 cmd_file_contents = NULL;
3773                 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3774                                                  &num_cmds);
3775         } else {
3776                 if (isatty(STDIN_FILENO)) {
3777                         tputs(T("Reading update commands from standard input..."));
3778                         recommend_man_page(CMD_UPDATE, stdout);
3779                 }
3780                 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3781                 if (!cmd_file_contents) {
3782                         ret = -1;
3783                         goto out_free_config;
3784                 }
3785
3786                 /* Parse the update commands */
3787                 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3788                                                  &num_cmds);
3789         }
3790         if (!cmds) {
3791                 ret = -1;
3792                 goto out_free_cmd_file_contents;
3793         }
3794
3795         /* Set default flags and capture config on the update commands */
3796         for (size_t i = 0; i < num_cmds; i++) {
3797                 switch (cmds[i].op) {
3798                 case WIMLIB_UPDATE_OP_ADD:
3799                         cmds[i].add.add_flags |= default_add_flags;
3800                         cmds[i].add.config = config;
3801                         break;
3802                 case WIMLIB_UPDATE_OP_DELETE:
3803                         cmds[i].delete_.delete_flags |= default_delete_flags;
3804                         break;
3805                 default:
3806                         break;
3807                 }
3808         }
3809
3810         /* Execute the update commands */
3811         ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3812                                   imagex_progress_func);
3813         if (ret)
3814                 goto out_free_cmds;
3815
3816         /* Overwrite the updated WIM */
3817         ret = wimlib_overwrite(wim, write_flags, num_threads,
3818                                imagex_progress_func);
3819 out_free_cmds:
3820         free(cmds);
3821 out_free_cmd_file_contents:
3822         free(cmd_file_contents);
3823 out_free_config:
3824         if (config) {
3825                 free(config->exclusion_pats.pats);
3826                 free(config->exclusion_exception_pats.pats);
3827                 free(config_str);
3828         }
3829 out_wimlib_free:
3830         wimlib_free(wim);
3831 out_free_command_str:
3832         free(command_str);
3833         return ret;
3834
3835 out_usage:
3836         usage(CMD_UPDATE, stderr);
3837 out_err:
3838         ret = -1;
3839         goto out_free_command_str;
3840 }
3841
3842
3843
3844 struct imagex_command {
3845         const tchar *name;
3846         int (*func)(int argc, tchar **argv, int cmd);
3847 };
3848
3849 static const struct imagex_command imagex_commands[] = {
3850         [CMD_APPEND]   = {T("append"),   imagex_capture_or_append},
3851         [CMD_APPLY]    = {T("apply"),    imagex_apply},
3852         [CMD_CAPTURE]  = {T("capture"),  imagex_capture_or_append},
3853         [CMD_DELETE]   = {T("delete"),   imagex_delete},
3854         [CMD_DIR ]     = {T("dir"),      imagex_dir},
3855         [CMD_EXPORT]   = {T("export"),   imagex_export},
3856         [CMD_EXTRACT]  = {T("extract"),  imagex_extract},
3857         [CMD_INFO]     = {T("info"),     imagex_info},
3858         [CMD_JOIN]     = {T("join"),     imagex_join},
3859 #if WIM_MOUNTING_SUPPORTED
3860         [CMD_MOUNT]    = {T("mount"),    imagex_mount_rw_or_ro},
3861         [CMD_MOUNTRW]  = {T("mountrw"),  imagex_mount_rw_or_ro},
3862 #endif
3863         [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3864         [CMD_SPLIT]    = {T("split"),    imagex_split},
3865 #if WIM_MOUNTING_SUPPORTED
3866         [CMD_UNMOUNT]  = {T("unmount"),  imagex_unmount},
3867 #endif
3868         [CMD_UPDATE]   = {T("update"),   imagex_update},
3869 };
3870
3871 static const tchar *usage_strings[] = {
3872 [CMD_APPEND] =
3873 T(
3874 "    %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3875 "                    [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3876 "                    [--nocheck] [--flags EDITION_ID] [--dereference]\n"
3877 "                    [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3878 "                    [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3879 "                    [--update-of=[WIMFILE:]IMAGE]\n"
3880 ),
3881 [CMD_APPLY] =
3882 T(
3883 "    %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3884 "                    (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
3885 "                    [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3886 "                    [--hardlink] [--symlink] [--include-invalid-names]\n"
3887 ),
3888 [CMD_CAPTURE] =
3889 T(
3890 "    %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3891 "                    [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3892 "                    [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3893 "                    [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3894 "                    [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3895 "                    [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
3896 "                    [--delta-from=WIMFILE]\n"
3897 ),
3898 [CMD_DELETE] =
3899 T(
3900 "    %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
3901 "                    [--check] [--soft]\n"
3902 ),
3903 [CMD_DIR] =
3904 T(
3905 "    %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3906 ),
3907 [CMD_EXPORT] =
3908 T(
3909 "    %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3910 "                    DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3911 "                    [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3912 "                    [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3913 ),
3914 [CMD_EXTRACT] =
3915 T(
3916 "    %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) ([PATH...] | @LISTFILE)\n"
3917 "                    [--check] [--ref=\"GLOB\"] [--no-acls] [--strict-acls]\n"
3918 "                    [--to-stdout] [--dest-dir=CMD_DIR]\n"
3919 "                    [--include-invalid-names]\n"
3920 ),
3921 [CMD_INFO] =
3922 T(
3923 "    %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3924 "                    [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
3925 "                    [--extract-xml FILE] [--header] [--lookup-table]\n"
3926 ),
3927 [CMD_JOIN] =
3928 T(
3929 "    %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3930 ),
3931 #if WIM_MOUNTING_SUPPORTED
3932 [CMD_MOUNT] =
3933 T(
3934 "    %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3935 "                    [--check] [--streams-interface=INTERFACE]\n"
3936 "                    [--ref=\"GLOB\"] [--allow-other]\n"
3937 ),
3938 [CMD_MOUNTRW] =
3939 T(
3940 "    %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3941 "                    [--check] [--streams-interface=INTERFACE]\n"
3942 "                    [--staging-dir=CMD_DIR] [--allow-other]\n"
3943 ),
3944 #endif
3945 [CMD_OPTIMIZE] =
3946 T(
3947 "    %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3948 "                    [--recompress-slow] [--compress=TYPE]\n"
3949 "                    [--threads=NUM_THREADS]\n"
3950 ),
3951 [CMD_SPLIT] =
3952 T(
3953 "    %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3954 ),
3955 #if WIM_MOUNTING_SUPPORTED
3956 [CMD_UNMOUNT] =
3957 T(
3958 "    %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3959 ),
3960 #endif
3961 [CMD_UPDATE] =
3962 T(
3963 "    %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3964 "                    [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3965 "                    [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3966 ),
3967 };
3968
3969 static const tchar *invocation_name;
3970 static int invocation_cmd = CMD_NONE;
3971
3972 static const tchar *get_cmd_string(int cmd, bool nospace)
3973 {
3974         static tchar buf[50];
3975         if (cmd == CMD_NONE) {
3976                 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3977         } else if (invocation_cmd != CMD_NONE) {
3978                 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3979         } else {
3980                 const tchar *format;
3981
3982                 if (nospace)
3983                         format = T("%"TS"-%"TS"");
3984                 else
3985                         format = T("%"TS" %"TS"");
3986                 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3987         }
3988         return buf;
3989 }
3990
3991 static void
3992 version(void)
3993 {
3994         static const tchar *s =
3995         T(
3996 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3997 "Copyright (C) 2012, 2013 Eric Biggers\n"
3998 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3999 "This is free software: you are free to change and redistribute it.\n"
4000 "There is NO WARRANTY, to the extent permitted by law.\n"
4001 "\n"
4002 "Report bugs to "PACKAGE_BUGREPORT".\n"
4003         );
4004         tfputs(s, stdout);
4005 }
4006
4007
4008 static void
4009 help_or_version(int argc, tchar **argv, int cmd)
4010 {
4011         int i;
4012         const tchar *p;
4013
4014         for (i = 1; i < argc; i++) {
4015                 p = argv[i];
4016                 if (p[0] == T('-') && p[1] == T('-')) {
4017                         p += 2;
4018                         if (!tstrcmp(p, T("help"))) {
4019                                 if (cmd == CMD_NONE)
4020                                         usage_all(stdout);
4021                                 else
4022                                         usage(cmd, stdout);
4023                                 exit(0);
4024                         } else if (!tstrcmp(p, T("version"))) {
4025                                 version();
4026                                 exit(0);
4027                         }
4028                 }
4029         }
4030 }
4031
4032 static void
4033 print_usage_string(int cmd, FILE *fp)
4034 {
4035         tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4036 }
4037
4038 static void
4039 recommend_man_page(int cmd, FILE *fp)
4040 {
4041         const tchar *format_str;
4042 #ifdef __WIN32__
4043         format_str = T("Uncommon options are not listed;\n"
4044                        "See %"TS".pdf in the doc directory for more details.\n");
4045 #else
4046         format_str = T("Uncommon options are not listed;\n"
4047                        "Try `man %"TS"' for more details.\n");
4048 #endif
4049         tfprintf(fp, format_str, get_cmd_string(cmd, true));
4050 }
4051
4052 static void
4053 usage(int cmd, FILE *fp)
4054 {
4055         tfprintf(fp, T("Usage:\n"));
4056         print_usage_string(cmd, fp);
4057         tfprintf(fp, T("\n"));
4058         recommend_man_page(cmd, fp);
4059 }
4060
4061 static void
4062 usage_all(FILE *fp)
4063 {
4064         tfprintf(fp, T("Usage:\n"));
4065         for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4066                 print_usage_string(cmd, fp);
4067                 tfprintf(fp, T("\n"));
4068         }
4069         static const tchar *extra =
4070         T(
4071 "    %"TS" --help\n"
4072 "    %"TS" --version\n"
4073 "\n"
4074 "    The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4075 "\n"
4076         );
4077         tfprintf(fp, extra, invocation_name, invocation_name);
4078         recommend_man_page(CMD_NONE, fp);
4079 }
4080
4081 /* Entry point for wimlib's ImageX implementation.  On UNIX the command
4082  * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4083  * something else), while an Windows the command arguments will be UTF-16LE
4084  * encoded 'wchar_t' strings. */
4085 int
4086 #ifdef __WIN32__
4087 wmain(int argc, wchar_t **argv, wchar_t **envp)
4088 #else
4089 main(int argc, char **argv)
4090 #endif
4091 {
4092         int ret;
4093         int init_flags = 0;
4094         int cmd;
4095
4096         imagex_info_file = stdout;
4097         invocation_name = tbasename(argv[0]);
4098
4099 #ifndef __WIN32__
4100         if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4101                 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4102         } else {
4103                 char *codeset;
4104
4105                 setlocale(LC_ALL, "");
4106                 codeset = nl_langinfo(CODESET);
4107                 if (!strstr(codeset, "UTF-8") &&
4108                     !strstr(codeset, "UTF8") &&
4109                     !strstr(codeset, "utf-8") &&
4110                     !strstr(codeset, "utf8"))
4111                 {
4112                         fprintf(stderr,
4113 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4114 "         Maybe try: `export LANG=en_US.UTF-8'?\n"
4115 "         Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4116 "         to any value to force wimlib to use UTF-8.\n",
4117                         invocation_name);
4118
4119                 }
4120         }
4121
4122 #endif /* !__WIN32__ */
4123
4124         {
4125                 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4126                 if (igcase != NULL) {
4127                         if (!tstrcmp(igcase, T("no")) ||
4128                             !tstrcmp(igcase, T("0")))
4129                                 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4130                         else if (!tstrcmp(igcase, T("yes")) ||
4131                                  !tstrcmp(igcase, T("1")))
4132                                 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4133                         else {
4134                                 fprintf(stderr,
4135                                         "WARNING: Ignoring unknown setting of "
4136                                         "WIMLIB_IMAGEX_IGNORE_CASE\n");
4137                         }
4138                 }
4139         }
4140
4141         /* Allow being invoked as wimCOMMAND (e.g. wimapply).  */
4142         cmd = CMD_NONE;
4143         if (!tstrncmp(invocation_name, T("wim"), 3) &&
4144             tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4145                 for (int i = 0; i < CMD_MAX; i++) {
4146                         if (!tstrcmp(invocation_name + 3,
4147                                      imagex_commands[i].name))
4148                         {
4149                                 invocation_cmd = i;
4150                                 cmd = i;
4151                                 break;
4152                         }
4153                 }
4154         }
4155
4156         /* Unless already known from the invocation name, determine which
4157          * command was specified.  */
4158         if (cmd == CMD_NONE) {
4159                 if (argc < 2) {
4160                         imagex_error(T("No command specified!\n"));
4161                         usage_all(stderr);
4162                         exit(2);
4163                 }
4164                 for (int i = 0; i < CMD_MAX; i++) {
4165                         if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4166                                 cmd = i;
4167                                 break;
4168                         }
4169                 }
4170                 if (cmd != CMD_NONE) {
4171                         argc--;
4172                         argv++;
4173                 }
4174         }
4175
4176         /* Handle --help and --version.  --help can be either for the program as
4177          * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4178          * CMD_NONE).  Note: help_or_version() will not return if a --help or
4179          * --version argument was found.  */
4180         help_or_version(argc, argv, cmd);
4181
4182         /* Bail if a valid command was not specified.  */
4183         if (cmd == CMD_NONE) {
4184                 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4185                 usage_all(stderr);
4186                 exit(2);
4187         }
4188
4189         /* Enable warning and error messages in wimlib be more user-friendly.
4190          * */
4191         wimlib_set_print_errors(true);
4192
4193         /* Initialize wimlib.  */
4194         ret = wimlib_global_init(init_flags);
4195         if (ret)
4196                 goto out_check_status;
4197
4198         /* Call the command handler function.  */
4199         ret = imagex_commands[cmd].func(argc, argv, cmd);
4200
4201         /* Check for error writing to standard output, especially since for some
4202          * commands, writing to standard output is part of the program's actual
4203          * behavior and not just for informational purposes.  */
4204         if (ferror(stdout) || fclose(stdout)) {
4205                 imagex_error_with_errno(T("error writing to standard output"));
4206                 if (ret == 0)
4207                         ret = -1;
4208         }
4209 out_check_status:
4210         /* Exit status (ret):  -1 indicates an error found by 'wimlib-imagex'
4211          * itself (not by wimlib).  0 indicates success.  > 0 indicates a wimlib
4212          * error code from which an error message can be printed.  */
4213         if (ret > 0) {
4214                 imagex_error(T("Exiting with error code %d:\n"
4215                                "       %"TS"."), ret,
4216                              wimlib_get_error_string(ret));
4217                 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4218                         imagex_error_with_errno(T("errno"));
4219         }
4220         /* Make wimlib free any resources it's holding (although this is not
4221          * strictly necessary because the process is ending anyway).  */
4222         wimlib_global_cleanup();
4223         return ret;
4224 }