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