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