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