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