]> wimlib.net Git - wimlib/blob - programs/imagex.c
d86bca3a6c0986f50a412656f1dc512df5ed1814
[wimlib] / programs / imagex.c
1 /*
2  * imagex.c
3  *
4  * Use wimlib to create, modify, extract, mount, unmount, or display information
5  * about a WIM file
6  */
7
8 /*
9  * Copyright (C) 2012, 2013 Eric Biggers
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h" /* Need for PACKAGE_VERSION, etc. */
27 #endif
28
29 #include "wimlib.h"
30 #include "wimlib_tchar.h"
31
32 #include <ctype.h>
33 #include <errno.h>
34
35 #include <inttypes.h>
36 #include <libgen.h>
37 #include <limits.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <locale.h>
44
45 #ifdef HAVE_ALLOCA_H
46 #  include <alloca.h>
47 #endif
48
49 #ifdef __WIN32__
50 #  include "imagex-win32.h"
51 #  define tbasename     win32_wbasename
52 #  define tglob         win32_wglob
53 #  define OS_PREFERRED_PATH_SEPARATOR L'\\'
54 #  define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
55 #else /* __WIN32__ */
56 #  include <glob.h>
57 #  include <getopt.h>
58 #  include <langinfo.h>
59 #  define tbasename     basename
60 #  define tglob         glob
61 #  define OS_PREFERRED_PATH_SEPARATOR '/'
62 #  define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
63 #endif /* !__WIN32 */
64
65
66 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
67
68 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
69                                 opts, NULL)) != -1)
70
71 enum imagex_op_type {
72         APPEND = 0,
73         APPLY,
74         CAPTURE,
75         DELETE,
76         DIR,
77         EXPORT,
78         EXTRACT,
79         INFO,
80         JOIN,
81         MOUNT,
82         MOUNTRW,
83         OPTIMIZE,
84         SPLIT,
85         UNMOUNT,
86         UPDATE,
87 };
88
89 static void usage(int cmd_type);
90 static void usage_all(void);
91
92 static bool imagex_be_quiet = false;
93
94
95 static const tchar *usage_strings[] = {
96 [APPEND] =
97 T(
98 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
99 "                     [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
100 "                     [--verbose] [--dereference] [--config=FILE]\n"
101 "                     [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
102 "                     [--source-list] [--no-acls] [--strict-acls]\n"
103 "                     [--rpfix] [--norpfix]\n"
104 ),
105 [APPLY] =
106 T(
107 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
108 "                    (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
109 "                    [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
110 "                    [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
111 "                    [--include-invalid-names]\n"
112 ),
113 [CAPTURE] =
114 T(
115 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
116 "                      [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
117 "                      [--flags EDITION_ID] [--verbose] [--dereference]\n"
118 "                      [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
119 "                      [--source-list] [--no-acls] [--strict-acls]\n"
120 "                      [--rpfix] [--norpfix]\n"
121 ),
122 [DELETE] =
123 T(
124 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
125 ),
126 [DIR] =
127 T(
128 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
129 ),
130 [EXPORT] =
131 T(
132 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
133 "              DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
134 "              [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
135 "              [--threads=NUM_THREADS] [--rebuild]\n"
136 ),
137 [EXTRACT] =
138 T(
139 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
140 "              [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
141 "              [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
142 "              [--include-invalid-names]\n"
143 ),
144 [INFO] =
145 T(
146 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
147 "                   [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
148 "                   [--xml] [--extract-xml FILE] [--metadata]\n"
149 ),
150 [JOIN] =
151 T(
152 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
153 ),
154 [MOUNT] =
155 T(
156 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
157 "                    [--check] [--debug] [--streams-interface=INTERFACE]\n"
158 "                    [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
159 ),
160 [MOUNTRW] =
161 T(
162 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
163 "                      [--check] [--debug] [--streams-interface=INTERFACE]\n"
164 "                      [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
165 ),
166 [OPTIMIZE] =
167 T(
168 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
169 "                      [--threads=NUM_THREADS]\n"
170 ),
171 [SPLIT] =
172 T(
173 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
174 ),
175 [UNMOUNT] =
176 T(
177 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
178 ),
179 [UPDATE] =
180 T(
181 IMAGEX_PROGNAME" update WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
182 "                       [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
183 "                       [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
184 ),
185 };
186
187
188 static void
189 recommend_man_page(const tchar *cmd_name)
190 {
191         const tchar *format_str;
192 #ifdef __WIN32__
193         format_str = T("See "IMAGEX_PROGNAME"%"TS"%"TS".pdf in the "
194                        "doc directory for more details.\n");
195 #else
196         format_str = T("Try `man "IMAGEX_PROGNAME"%"TS"%"TS"' "
197                        "for more details.\n");
198 #endif
199         tprintf(format_str, *cmd_name ? T("-") : T(""), cmd_name);
200 }
201
202 enum {
203         IMAGEX_ALLOW_OTHER_OPTION,
204         IMAGEX_BOOT_OPTION,
205         IMAGEX_CHECK_OPTION,
206         IMAGEX_COMMAND_OPTION,
207         IMAGEX_COMMIT_OPTION,
208         IMAGEX_COMPRESS_OPTION,
209         IMAGEX_CONFIG_OPTION,
210         IMAGEX_DEBUG_OPTION,
211         IMAGEX_DEREFERENCE_OPTION,
212         IMAGEX_DEST_DIR_OPTION,
213         IMAGEX_EXTRACT_XML_OPTION,
214         IMAGEX_FLAGS_OPTION,
215         IMAGEX_FORCE_OPTION,
216         IMAGEX_HARDLINK_OPTION,
217         IMAGEX_HEADER_OPTION,
218         IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
219         IMAGEX_LAZY_OPTION,
220         IMAGEX_LOOKUP_TABLE_OPTION,
221         IMAGEX_METADATA_OPTION,
222         IMAGEX_NORPFIX_OPTION,
223         IMAGEX_NO_ACLS_OPTION,
224         IMAGEX_PATH_OPTION,
225         IMAGEX_REBUILD_OPTION,
226         IMAGEX_RECOMPRESS_OPTION,
227         IMAGEX_RECURSIVE_OPTION,
228         IMAGEX_REF_OPTION,
229         IMAGEX_RPFIX_OPTION,
230         IMAGEX_SOFT_OPTION,
231         IMAGEX_SOURCE_LIST_OPTION,
232         IMAGEX_STAGING_DIR_OPTION,
233         IMAGEX_STREAMS_INTERFACE_OPTION,
234         IMAGEX_STRICT_ACLS_OPTION,
235         IMAGEX_SYMLINK_OPTION,
236         IMAGEX_THREADS_OPTION,
237         IMAGEX_TO_STDOUT_OPTION,
238         IMAGEX_UNIX_DATA_OPTION,
239         IMAGEX_VERBOSE_OPTION,
240         IMAGEX_XML_OPTION,
241 };
242
243 static const struct option apply_options[] = {
244         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
245         {T("hardlink"),    no_argument,       NULL, IMAGEX_HARDLINK_OPTION},
246         {T("symlink"),     no_argument,       NULL, IMAGEX_SYMLINK_OPTION},
247         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
248         {T("ref"),         required_argument, NULL, IMAGEX_REF_OPTION},
249         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
250         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
251         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
252         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
253         {T("rpfix"),       no_argument,       NULL, IMAGEX_RPFIX_OPTION},
254         {T("norpfix"),     no_argument,       NULL, IMAGEX_NORPFIX_OPTION},
255         {T("include-invalid-names"), no_argument,       NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
256         {NULL, 0, NULL, 0},
257 };
258 static const struct option capture_or_append_options[] = {
259         {T("boot"),        no_argument,       NULL, IMAGEX_BOOT_OPTION},
260         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
261         {T("compress"),    required_argument, NULL, IMAGEX_COMPRESS_OPTION},
262         {T("config"),      required_argument, NULL, IMAGEX_CONFIG_OPTION},
263         {T("dereference"), no_argument,       NULL, IMAGEX_DEREFERENCE_OPTION},
264         {T("flags"),       required_argument, NULL, IMAGEX_FLAGS_OPTION},
265         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
266         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
267         {T("rebuild"),     no_argument,       NULL, IMAGEX_REBUILD_OPTION},
268         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
269         {T("source-list"), no_argument,       NULL, IMAGEX_SOURCE_LIST_OPTION},
270         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
271         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
272         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
273         {T("rpfix"),       no_argument,       NULL, IMAGEX_RPFIX_OPTION},
274         {T("norpfix"),     no_argument,       NULL, IMAGEX_NORPFIX_OPTION},
275         {NULL, 0, NULL, 0},
276 };
277 static const struct option delete_options[] = {
278         {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
279         {T("soft"),  no_argument, NULL, IMAGEX_SOFT_OPTION},
280         {NULL, 0, NULL, 0},
281 };
282
283 static const struct option dir_options[] = {
284         {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
285         {NULL, 0, NULL, 0},
286 };
287
288 static const struct option export_options[] = {
289         {T("boot"),       no_argument,       NULL, IMAGEX_BOOT_OPTION},
290         {T("check"),      no_argument,       NULL, IMAGEX_CHECK_OPTION},
291         {T("compress"),   required_argument, NULL, IMAGEX_COMPRESS_OPTION},
292         {T("ref"),        required_argument, NULL, IMAGEX_REF_OPTION},
293         {T("threads"),    required_argument, NULL, IMAGEX_THREADS_OPTION},
294         {T("rebuild"),    no_argument,       NULL, IMAGEX_REBUILD_OPTION},
295         {NULL, 0, NULL, 0},
296 };
297
298 static const struct option extract_options[] = {
299         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
300         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
301         {T("ref"),         required_argument, NULL, IMAGEX_REF_OPTION},
302         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
303         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
304         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
305         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
306         {T("dest-dir"),    required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
307         {T("to-stdout"),   no_argument,       NULL, IMAGEX_TO_STDOUT_OPTION},
308         {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
309         {NULL, 0, NULL, 0},
310 };
311
312 static const struct option info_options[] = {
313         {T("boot"),         no_argument,       NULL, IMAGEX_BOOT_OPTION},
314         {T("check"),        no_argument,       NULL, IMAGEX_CHECK_OPTION},
315         {T("extract-xml"),  required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
316         {T("header"),       no_argument,       NULL, IMAGEX_HEADER_OPTION},
317         {T("lookup-table"), no_argument,       NULL, IMAGEX_LOOKUP_TABLE_OPTION},
318         {T("metadata"),     no_argument,       NULL, IMAGEX_METADATA_OPTION},
319         {T("xml"),          no_argument,       NULL, IMAGEX_XML_OPTION},
320         {NULL, 0, NULL, 0},
321 };
322
323 static const struct option join_options[] = {
324         {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
325         {NULL, 0, NULL, 0},
326 };
327
328 static const struct option mount_options[] = {
329         {T("check"),             no_argument,       NULL, IMAGEX_CHECK_OPTION},
330         {T("debug"),             no_argument,       NULL, IMAGEX_DEBUG_OPTION},
331         {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
332         {T("ref"),               required_argument, NULL, IMAGEX_REF_OPTION},
333         {T("staging-dir"),       required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
334         {T("unix-data"),         no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
335         {T("allow-other"),       no_argument,       NULL, IMAGEX_ALLOW_OTHER_OPTION},
336         {NULL, 0, NULL, 0},
337 };
338
339 static const struct option optimize_options[] = {
340         {T("check"),      no_argument, NULL, IMAGEX_CHECK_OPTION},
341         {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
342         {T("threads"),    required_argument, NULL, IMAGEX_THREADS_OPTION},
343         {NULL, 0, NULL, 0},
344 };
345
346 static const struct option split_options[] = {
347         {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
348         {NULL, 0, NULL, 0},
349 };
350
351 static const struct option unmount_options[] = {
352         {T("commit"),  no_argument, NULL, IMAGEX_COMMIT_OPTION},
353         {T("check"),   no_argument, NULL, IMAGEX_CHECK_OPTION},
354         {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
355         {T("lazy"),    no_argument, NULL, IMAGEX_LAZY_OPTION},
356         {NULL, 0, NULL, 0},
357 };
358
359 static const struct option update_options[] = {
360         /* Careful: some of the options here set the defaults for update
361          * commands, but the flags given to an actual update command (and not to
362          * `imagex update' itself are also handled in
363          * update_command_add_option().  */
364         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
365         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
366         {T("rebuild"),     no_argument,       NULL, IMAGEX_REBUILD_OPTION},
367         {T("command"),     required_argument, NULL, IMAGEX_COMMAND_OPTION},
368
369         /* Default delete options */
370         {T("force"),       no_argument,       NULL, IMAGEX_FORCE_OPTION},
371         {T("recursive"),   no_argument,       NULL, IMAGEX_RECURSIVE_OPTION},
372
373         /* Global add option */
374         {T("config"),      required_argument, NULL, IMAGEX_CONFIG_OPTION},
375
376         /* Default add options */
377         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
378         {T("dereference"), no_argument,       NULL, IMAGEX_DEREFERENCE_OPTION},
379         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
380         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
381         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
382         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
383
384         {NULL, 0, NULL, 0},
385 };
386
387 #ifdef __GNUC__
388 #       define _format_attribute(type, format_str, args_start) \
389                         __attribute__((format(type, format_str, args_start)))
390 #else
391 #       define _format_attribute(type, format_str, args_start)
392 #endif
393
394 /* Print formatted error message to stderr. */
395 static void _format_attribute(printf, 1, 2)
396 imagex_error(const tchar *format, ...)
397 {
398         va_list va;
399         va_start(va, format);
400         tfputs(T("ERROR: "), stderr);
401         tvfprintf(stderr, format, va);
402         tputc(T('\n'), stderr);
403         va_end(va);
404 }
405
406 /* Print formatted error message to stderr. */
407 static void _format_attribute(printf, 1, 2)
408 imagex_error_with_errno(const tchar *format, ...)
409 {
410         int errno_save = errno;
411         va_list va;
412         va_start(va, format);
413         tfputs(T("ERROR: "), stderr);
414         tvfprintf(stderr, format, va);
415         tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
416         va_end(va);
417 }
418
419 static int
420 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
421 {
422         if (image == WIMLIB_NO_IMAGE) {
423                 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
424                              "       Please specify a 1-based image index or "
425                              "image name.\n"
426                              "       You may use `"IMAGEX_PROGNAME" info' to list the images "
427                              "contained in a WIM."),
428                              image_name, wim_name);
429                 return -1;
430         }
431         return 0;
432 }
433
434 static int
435 verify_image_is_single(int image)
436 {
437         if (image == WIMLIB_ALL_IMAGES) {
438                 imagex_error(T("Cannot specify all images for this action!"));
439                 return -1;
440         }
441         return 0;
442 }
443
444 static int
445 verify_image_exists_and_is_single(int image, const tchar *image_name,
446                                   const tchar *wim_name)
447 {
448         int ret;
449         ret = verify_image_exists(image, image_name, wim_name);
450         if (ret == 0)
451                 ret = verify_image_is_single(image);
452         return ret;
453 }
454
455 /* Parse the argument to --compress */
456 static int
457 get_compression_type(const tchar *optarg)
458 {
459         if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
460                 return WIMLIB_COMPRESSION_TYPE_LZX;
461         else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
462                 return WIMLIB_COMPRESSION_TYPE_XPRESS;
463         else if (!tstrcasecmp(optarg, T("none")))
464                 return WIMLIB_COMPRESSION_TYPE_NONE;
465         else {
466                 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
467                              "\"maximum\", \"fast\", or \"none\"."), optarg);
468                 return WIMLIB_COMPRESSION_TYPE_INVALID;
469         }
470 }
471
472 /* Returns the size of a file given its name, or -1 if the file does not exist
473  * or its size cannot be determined.  */
474 static off_t
475 file_get_size(const tchar *filename)
476 {
477         struct stat st;
478         if (tstat(filename, &st) == 0)
479                 return st.st_size;
480         else
481                 return (off_t)-1;
482 }
483
484 tchar pat_ntfs_log[]                  = T("/$ntfs.log");
485 tchar pat_hiberfil_sys[]              = T("/hiberfil.sys");
486 tchar pat_pagefile_sys[]              = T("/pagefile.sys");
487 tchar pat_system_volume_information[] = T("/System Volume Information");
488 tchar pat_recycler[]                  = T("/RECYCLER");
489 tchar pat_windows_csc[]               = T("/Windows/CSC");
490
491 tchar *default_pats[] = {
492         pat_ntfs_log,
493         pat_hiberfil_sys,
494         pat_pagefile_sys,
495         pat_system_volume_information,
496         pat_recycler,
497         pat_windows_csc,
498 };
499
500 static struct wimlib_capture_config default_capture_config = {
501         .exclusion_pats = {
502                 .num_pats = sizeof(default_pats) / sizeof(default_pats[0]),
503                 .pats = default_pats,
504         },
505 };
506
507 enum {
508         PARSE_STRING_SUCCESS = 0,
509         PARSE_STRING_FAILURE = 1,
510         PARSE_STRING_NONE = 2,
511 };
512
513 /*
514  * Parses a string token from an array of characters.
515  *
516  * Tokens are either whitespace-delimited, or double or single-quoted.
517  *
518  * @line_p:  Pointer to the pointer to the line of data.  Will be updated
519  *           to point past the string token iff the return value is
520  *           PARSE_STRING_SUCCESS.  If *len_p > 0, (*line_p)[*len_p - 1] must
521  *           be '\0'.
522  *
523  * @len_p:   @len_p initially stores the length of the line of data, which may
524  *           be 0, and it will be updated to the number of bytes remaining in
525  *           the line iff the return value is PARSE_STRING_SUCCESS.
526  *
527  * @fn_ret:  Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
528  *           parsed string token will be returned here.
529  *
530  * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
531  *          PARSE_STRING_FAILURE if the data was invalid due to a missing
532  *          closing quote; or PARSE_STRING_NONE if the line ended before the
533  *          beginning of a string token was found.
534  */
535 static int
536 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
537 {
538         size_t len = *len_p;
539         tchar *line = *line_p;
540         tchar *fn;
541         tchar quote_char;
542
543         /* Skip leading whitespace */
544         for (;;) {
545                 if (len == 0)
546                         return PARSE_STRING_NONE;
547                 if (!istspace(*line) && *line != T('\0'))
548                         break;
549                 line++;
550                 len--;
551         }
552         quote_char = *line;
553         if (quote_char == T('"') || quote_char == T('\'')) {
554                 /* Quoted string */
555                 line++;
556                 len--;
557                 fn = line;
558                 line = tmemchr(line, quote_char, len);
559                 if (!line) {
560                         imagex_error(T("Missing closing quote: %"TS), fn - 1);
561                         return PARSE_STRING_FAILURE;
562                 }
563         } else {
564                 /* Unquoted string.  Go until whitespace.  Line is terminated
565                  * by '\0', so no need to check 'len'. */
566                 fn = line;
567                 do {
568                         line++;
569                 } while (!istspace(*line) && *line != T('\0'));
570         }
571         *line = T('\0');
572         len -= line - fn;
573         *len_p = len;
574         *line_p = line;
575         *fn_ret = fn;
576         return PARSE_STRING_SUCCESS;
577 }
578
579 /* Parses a line of data (not an empty line or comment) in the source list file
580  * format.  (See the man page for 'wimlib-imagex capture' for details on this
581  * format and the meaning.)
582  *
583  * @line:  Line of data to be parsed.  line[len - 1] must be '\0', unless
584  *         len == 0.  The data in @line will be modified by this function call.
585  *
586  * @len:   Length of the line of data.
587  *
588  * @source:  On success, the capture source and target described by the line is
589  *           written into this destination.  Note that it will contain pointers
590  *           to data in the @line array.
591  *
592  * Returns true if the line was valid; false otherwise.  */
593 static bool
594 parse_source_list_line(tchar *line, size_t len,
595                        struct wimlib_capture_source *source)
596 {
597         /* SOURCE [DEST] */
598         int ret;
599         ret = parse_string(&line, &len, &source->fs_source_path);
600         if (ret != PARSE_STRING_SUCCESS)
601                 return false;
602         ret = parse_string(&line, &len, &source->wim_target_path);
603         if (ret == PARSE_STRING_NONE)
604                 source->wim_target_path = source->fs_source_path;
605         return ret != PARSE_STRING_FAILURE;
606 }
607
608 /* Returns %true if the given line of length @len > 0 is a comment or empty line
609  * in the source list file format. */
610 static bool
611 is_comment_line(const tchar *line, size_t len)
612 {
613         for (;;) {
614                 if (*line == T('#'))
615                         return true;
616                 if (!istspace(*line) && *line != T('\0'))
617                         return false;
618                 ++line;
619                 --len;
620                 if (len == 0)
621                         return true;
622         }
623 }
624
625 static ssize_t
626 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
627 {
628         ssize_t nlines = 0;
629         tchar *contents = *contents_p;
630         size_t nchars = *nchars_p;
631         size_t i;
632
633         for (i = 0; i < nchars; i++)
634                 if (contents[i] == T('\n'))
635                         nlines++;
636
637         /* Handle last line not terminated by a newline */
638         if (nchars != 0 && contents[nchars - 1] != T('\n')) {
639                 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
640                 if (!contents) {
641                         imagex_error(T("Out of memory!"));
642                         return -1;
643                 }
644                 contents[nchars] = T('\n');
645                 *contents_p = contents;
646                 nchars++;
647                 nlines++;
648         }
649         *nchars_p = nchars;
650         return nlines;
651 }
652
653 /* Parses a file in the source list format.  (See the man page for
654  * 'wimlib-imagex capture' for details on this format and the meaning.)
655  *
656  * @source_list_contents:  Contents of the source list file.  Note that this
657  *                         buffer will be modified to save memory allocations,
658  *                         and cannot be freed until the returned array of
659  *                         wimlib_capture_source's has also been freed.
660  *
661  * @source_list_nbytes:    Number of bytes of data in the @source_list_contents
662  *                         buffer.
663  *
664  * @nsources_ret:          On success, the length of the returned array is
665  *                         returned here.
666  *
667  * Returns:   An array of `struct wimlib_capture_source's that can be passed to
668  * the wimlib_add_image_multisource() function to specify how a WIM image is to
669  * be created.  */
670 static struct wimlib_capture_source *
671 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
672                   size_t *nsources_ret)
673 {
674         ssize_t nlines;
675         tchar *p;
676         struct wimlib_capture_source *sources;
677         size_t i, j;
678
679         nlines = text_file_count_lines(source_list_contents_p,
680                                        &source_list_nchars);
681         if (nlines < 0)
682                 return NULL;
683
684         /* Always allocate at least 1 slot, just in case the implementation of
685          * calloc() returns NULL if 0 bytes are requested. */
686         sources = calloc(nlines ?: 1, sizeof(*sources));
687         if (!sources) {
688                 imagex_error(T("out of memory"));
689                 return NULL;
690         }
691         p = *source_list_contents_p;
692         j = 0;
693         for (i = 0; i < nlines; i++) {
694                 /* XXX: Could use rawmemchr() here instead, but it may not be
695                  * available on all platforms. */
696                 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
697                 size_t len = endp - p + 1;
698                 *endp = T('\0');
699                 if (!is_comment_line(p, len)) {
700                         if (!parse_source_list_line(p, len, &sources[j++])) {
701                                 free(sources);
702                                 return NULL;
703                         }
704                 }
705                 p = endp + 1;
706
707         }
708         *nsources_ret = j;
709         return sources;
710 }
711
712
713 enum capture_config_section {
714         CAPTURE_CONFIG_NO_SECTION,
715         CAPTURE_CONFIG_EXCLUSION_SECTION,
716         CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
717         CAPTURE_CONFIG_IGNORE_SECTION,
718 };
719
720 enum {
721         CAPTURE_CONFIG_INVALID_SECTION,
722         CAPTURE_CONFIG_CHANGED_SECTION,
723         CAPTURE_CONFIG_SAME_SECTION,
724 };
725
726 static int
727 check_config_section(tchar *line, size_t len,
728                      enum capture_config_section *cur_section)
729 {
730         while (istspace(*line))
731                 line++;
732
733         if (*line != T('['))
734                 return CAPTURE_CONFIG_SAME_SECTION;
735
736         line++;
737         tchar *endbrace = tstrrchr(line, T(']'));
738         if (!endbrace)
739                 return CAPTURE_CONFIG_SAME_SECTION;
740
741         if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
742                 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
743         } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
744                 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
745         } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
746                 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
747                 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
748                          "of capture config file\n"),
749                        stderr);
750         } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
751                 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
752                 tfputs(T("WARNING: Ignoring [AlignmentList] section "
753                          "of capture config file\n"),
754                        stderr);
755         } else {
756                 imagex_error(T("Invalid capture config file section \"%"TS"\""),
757                              line - 1);
758                 return CAPTURE_CONFIG_INVALID_SECTION;
759         }
760         return CAPTURE_CONFIG_CHANGED_SECTION;
761 }
762
763
764 static bool
765 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
766                          tchar *pat)
767 {
768         if (pat_list->num_pats == pat_list->num_allocated_pats) {
769                 tchar **pats;
770                 size_t num_allocated_pats = pat_list->num_pats + 8;
771
772                 pats = realloc(pat_list->pats,
773                                num_allocated_pats * sizeof(pat_list->pats[0]));
774                 if (!pats) {
775                         imagex_error(T("Out of memory!"));
776                         return false;
777                 }
778                 pat_list->pats = pats;
779                 pat_list->num_allocated_pats = num_allocated_pats;
780         }
781         pat_list->pats[pat_list->num_pats++] = pat;
782         return true;
783 }
784
785 static bool
786 parse_capture_config_line(tchar *line, size_t len,
787                           enum capture_config_section *cur_section,
788                           struct wimlib_capture_config *config)
789 {
790         tchar *filename;
791         int ret;
792
793         ret = check_config_section(line, len, cur_section);
794         if (ret == CAPTURE_CONFIG_INVALID_SECTION)
795                 return false;
796         if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
797                 return true;
798
799         switch (*cur_section) {
800         case CAPTURE_CONFIG_NO_SECTION:
801                 imagex_error(T("Line \"%"TS"\" is not in a section "
802                                "(such as [ExclusionList]"), line);
803                 return false;
804         case CAPTURE_CONFIG_EXCLUSION_SECTION:
805                 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
806                         return false;
807                 return pattern_list_add_pattern(&config->exclusion_pats,
808                                                 filename);
809         case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
810                 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
811                         return false;
812                 return pattern_list_add_pattern(&config->exclusion_exception_pats,
813                                                 filename);
814         case CAPTURE_CONFIG_IGNORE_SECTION:
815                 return true;
816         }
817         return false;
818 }
819
820 static int
821 parse_capture_config(tchar **contents_p, size_t nchars,
822                      struct wimlib_capture_config *config)
823 {
824         ssize_t nlines;
825         tchar *p;
826         size_t i;
827         enum capture_config_section cur_section;
828
829         memset(config, 0, sizeof(*config));
830
831         nlines = text_file_count_lines(contents_p, &nchars);
832         if (nlines < 0)
833                 return -1;
834
835         cur_section = CAPTURE_CONFIG_NO_SECTION;
836         p = *contents_p;
837         for (i = 0; i < nlines; i++) {
838                 tchar *endp = tmemchr(p, T('\n'), nchars);
839                 size_t len = endp - p + 1;
840                 *endp = T('\0');
841                 if (!is_comment_line(p, len))
842                         if (!parse_capture_config_line(p, len, &cur_section, config))
843                                 return -1;
844                 p = endp + 1;
845
846         }
847         return 0;
848 }
849
850 /* Reads the contents of a file into memory. */
851 static char *
852 file_get_contents(const tchar *filename, size_t *len_ret)
853 {
854         struct stat stbuf;
855         void *buf = NULL;
856         size_t len;
857         FILE *fp;
858
859         if (tstat(filename, &stbuf) != 0) {
860                 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
861                 goto out;
862         }
863         len = stbuf.st_size;
864
865         fp = tfopen(filename, T("rb"));
866         if (!fp) {
867                 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
868                 goto out;
869         }
870
871         buf = malloc(len ? len : 1);
872         if (!buf) {
873                 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
874                                "contents of file \"%"TS"\""), len, filename);
875                 goto out_fclose;
876         }
877         if (fread(buf, 1, len, fp) != len) {
878                 imagex_error_with_errno(T("Failed to read %zu bytes from the "
879                                           "file \"%"TS"\""), len, filename);
880                 goto out_free_buf;
881         }
882         *len_ret = len;
883         goto out_fclose;
884 out_free_buf:
885         free(buf);
886         buf = NULL;
887 out_fclose:
888         fclose(fp);
889 out:
890         return buf;
891 }
892
893 /* Read standard input until EOF and return the full contents in a malloc()ed
894  * buffer and the number of bytes of data in @len_ret.  Returns NULL on read
895  * error. */
896 static char *
897 stdin_get_contents(size_t *len_ret)
898 {
899         /* stdin can, of course, be a pipe or other non-seekable file, so the
900          * total length of the data cannot be pre-determined */
901         char *buf = NULL;
902         size_t newlen = 1024;
903         size_t pos = 0;
904         size_t inc = 1024;
905         for (;;) {
906                 char *p = realloc(buf, newlen);
907                 size_t bytes_read, bytes_to_read;
908                 if (!p) {
909                         imagex_error(T("out of memory while reading stdin"));
910                         break;
911                 }
912                 buf = p;
913                 bytes_to_read = newlen - pos;
914                 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
915                 pos += bytes_read;
916                 if (bytes_read != bytes_to_read) {
917                         if (feof(stdin)) {
918                                 *len_ret = pos;
919                                 return buf;
920                         } else {
921                                 imagex_error_with_errno(T("error reading stdin"));
922                                 break;
923                         }
924                 }
925                 newlen += inc;
926                 inc *= 3;
927                 inc /= 2;
928         }
929         free(buf);
930         return NULL;
931 }
932
933
934 static tchar *
935 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
936 {
937 #ifndef __WIN32__
938         /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
939          * */
940         *num_tchars_ret = num_bytes;
941         return text;
942 #else /* !__WIN32__ */
943         /* On Windows, translate the text to UTF-16LE */
944         wchar_t *text_wstr;
945         size_t num_wchars;
946
947         if (num_bytes >= 2 &&
948             ((text[0] == 0xff && text[1] == 0xfe) ||
949              (text[0] <= 0x7f && text[1] == 0x00)))
950         {
951                 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
952                  * with something that looks like an ASCII character encoded as
953                  * a UTF-16LE code unit.  Assume the file is encoded as
954                  * UTF-16LE.  This is not a 100% reliable check. */
955                 num_wchars = num_bytes / 2;
956                 text_wstr = (wchar_t*)text;
957         } else {
958                 /* File does not look like UTF-16LE.  Assume it is encoded in
959                  * the current Windows code page.  I think these are always
960                  * ASCII-compatible, so any so-called "plain-text" (ASCII) files
961                  * should work as expected. */
962                 text_wstr = win32_mbs_to_wcs(text,
963                                              num_bytes,
964                                              &num_wchars);
965                 free(text);
966         }
967         *num_tchars_ret = num_wchars;
968         return text_wstr;
969 #endif /* __WIN32__ */
970 }
971
972 static tchar *
973 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
974 {
975         char *contents;
976         size_t num_bytes;
977
978         contents = file_get_contents(filename, &num_bytes);
979         if (!contents)
980                 return NULL;
981         return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
982 }
983
984 static tchar *
985 stdin_get_text_contents(size_t *num_tchars_ret)
986 {
987         char *contents;
988         size_t num_bytes;
989
990         contents = stdin_get_contents(&num_bytes);
991         if (!contents)
992                 return NULL;
993         return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
994 }
995
996 #define TO_PERCENT(numerator, denominator) \
997         (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
998
999 /* Given an enumerated value for WIM compression type, return a descriptive
1000  * string. */
1001 static const tchar *
1002 get_data_type(int ctype)
1003 {
1004         switch (ctype) {
1005         case WIMLIB_COMPRESSION_TYPE_NONE:
1006                 return T("uncompressed");
1007         case WIMLIB_COMPRESSION_TYPE_LZX:
1008                 return T("LZX-compressed");
1009         case WIMLIB_COMPRESSION_TYPE_XPRESS:
1010                 return T("XPRESS-compressed");
1011         }
1012         return NULL;
1013 }
1014
1015 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1016 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1017 #define KIBIBYTE_MIN_NBYTES 10000ULL
1018
1019 static unsigned
1020 get_unit(uint64_t total_bytes, const tchar **name_ret)
1021 {
1022         if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1023                 *name_ret = T("GiB");
1024                 return 30;
1025         } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1026                 *name_ret = T("MiB");
1027                 return 20;
1028         } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1029                 *name_ret = T("KiB");
1030                 return 10;
1031         } else {
1032                 *name_ret = T("bytes");
1033                 return 0;
1034         }
1035 }
1036
1037 /* Progress callback function passed to various wimlib functions. */
1038 static int
1039 imagex_progress_func(enum wimlib_progress_msg msg,
1040                      const union wimlib_progress_info *info)
1041 {
1042         unsigned percent_done;
1043         unsigned unit_shift;
1044         const tchar *unit_name;
1045         if (imagex_be_quiet)
1046                 return 0;
1047         switch (msg) {
1048         case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1049                 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1050                 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1051                                           info->write_streams.total_bytes);
1052                 if (info->write_streams.completed_streams == 0) {
1053                         const tchar *data_type;
1054
1055                         data_type = get_data_type(info->write_streams.compression_type);
1056                         tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
1057                                 data_type, info->write_streams.num_threads,
1058                                 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1059                 }
1060                 tprintf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1061                         "written (%u%% done)"),
1062                         info->write_streams.completed_bytes >> unit_shift,
1063                         unit_name,
1064                         info->write_streams.total_bytes >> unit_shift,
1065                         unit_name,
1066                         percent_done);
1067                 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1068                         tputchar(T('\n'));
1069                 break;
1070         case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1071                 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
1072                 if (*info->scan.wim_target_path) {
1073                         tprintf(T(" (loading as WIM path: "
1074                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1075                                info->scan.wim_target_path);
1076                 } else {
1077                         tprintf(T(" (loading as root of WIM image)...\n"));
1078                 }
1079                 break;
1080         case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1081                 if (info->scan.excluded)
1082                         tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1083                 else
1084                         tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1085                 break;
1086         /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1087                 /*break;*/
1088         case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1089                 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1090                 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1091                                           info->integrity.total_bytes);
1092                 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1093                         "of %"PRIu64" %"TS" (%u%%) done"),
1094                         info->integrity.filename,
1095                         info->integrity.completed_bytes >> unit_shift,
1096                         unit_name,
1097                         info->integrity.total_bytes >> unit_shift,
1098                         unit_name,
1099                         percent_done);
1100                 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1101                         tputchar(T('\n'));
1102                 break;
1103         case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1104                 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1105                 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1106                                           info->integrity.total_bytes);
1107                 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1108                           "of %"PRIu64" %"TS" (%u%%) done"),
1109                         info->integrity.completed_bytes >> unit_shift,
1110                         unit_name,
1111                         info->integrity.total_bytes >> unit_shift,
1112                         unit_name,
1113                         percent_done);
1114                 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1115                         tputchar(T('\n'));
1116                 break;
1117         case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1118                 tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1119                           "to %"TS" \"%"TS"\"\n"),
1120                         info->extract.image,
1121                         info->extract.image_name,
1122                         info->extract.wimfile_name,
1123                         ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1124                          T("NTFS volume") : T("directory")),
1125                         info->extract.target);
1126                 break;
1127         case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1128                 tprintf(T("Extracting "
1129                           "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1130                           "in \"%"TS"\" to \"%"TS"\"\n"),
1131                         info->extract.extract_root_wim_source_path,
1132                         info->extract.image,
1133                         info->extract.image_name,
1134                         info->extract.wimfile_name,
1135                         info->extract.target);
1136                 break;
1137         /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1138                 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
1139                         /*info->extract.target);*/
1140                 /*break;*/
1141         case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1142                 percent_done = TO_PERCENT(info->extract.completed_bytes,
1143                                           info->extract.total_bytes);
1144                 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1145                 tprintf(T("\rExtracting files: "
1146                           "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1147                         info->extract.completed_bytes >> unit_shift,
1148                         unit_name,
1149                         info->extract.total_bytes >> unit_shift,
1150                         unit_name,
1151                         percent_done);
1152                 if (info->extract.completed_bytes >= info->extract.total_bytes)
1153                         tputchar(T('\n'));
1154                 break;
1155         case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
1156                 tprintf(T("%"TS"\n"), info->extract.cur_path);
1157                 break;
1158         case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1159                 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1160                         tprintf(T("Setting timestamps on all extracted files...\n"));
1161                 break;
1162         case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1163                 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1164                         tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1165                                 info->extract.target);
1166                 }
1167                 break;
1168         case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1169                 percent_done = TO_PERCENT(info->join.completed_bytes,
1170                                           info->join.total_bytes);
1171                 unit_shift = get_unit(info->join.total_bytes, &unit_name);
1172                 tprintf(T("Writing resources from part %u of %u: "
1173                           "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
1174                         (info->join.completed_parts == info->join.total_parts) ?
1175                         info->join.completed_parts : info->join.completed_parts + 1,
1176                         info->join.total_parts,
1177                         info->join.completed_bytes >> unit_shift,
1178                         unit_name,
1179                         info->join.total_bytes >> unit_shift,
1180                         unit_name,
1181                         percent_done);
1182                 break;
1183         case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1184                 percent_done = TO_PERCENT(info->split.completed_bytes,
1185                                           info->split.total_bytes);
1186                 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1187                 tprintf(T("Writing \"%"TS"\": %"PRIu64" %"TS" of "
1188                           "%"PRIu64" %"TS" (%u%%) written\n"),
1189                         info->split.part_name,
1190                         info->split.completed_bytes >> unit_shift,
1191                         unit_name,
1192                         info->split.total_bytes >> unit_shift,
1193                         unit_name,
1194                         percent_done);
1195                 break;
1196         case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1197                 if (info->split.completed_bytes == info->split.total_bytes) {
1198                         tprintf(T("Finished writing %u split WIM parts\n"),
1199                                 info->split.cur_part_number);
1200                 }
1201                 break;
1202         case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1203                 switch (info->update.command->op) {
1204                 case WIMLIB_UPDATE_OP_DELETE:
1205                         tprintf(T("Deleted WIM path "
1206                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1207                                 info->update.command->delete.wim_path);
1208                         break;
1209                 case WIMLIB_UPDATE_OP_RENAME:
1210                         tprintf(T("Renamed WIM path "
1211                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1212                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1213                                 info->update.command->rename.wim_source_path,
1214                                 info->update.command->rename.wim_target_path);
1215                         break;
1216                 case WIMLIB_UPDATE_OP_ADD:
1217                 default:
1218                         break;
1219                 }
1220                 break;
1221         default:
1222                 break;
1223         }
1224         fflush(stdout);
1225         return 0;
1226 }
1227
1228 /* Open all the split WIM parts that correspond to a file glob.
1229  *
1230  * @first_part specifies the first part of the split WIM and it may be either
1231  * included or omitted from the glob. */
1232 static int
1233 open_swms_from_glob(const tchar *swm_glob,
1234                     const tchar *first_part,
1235                     int open_flags,
1236                     WIMStruct ***additional_swms_ret,
1237                     unsigned *num_additional_swms_ret)
1238 {
1239         unsigned num_additional_swms = 0;
1240         WIMStruct **additional_swms = NULL;
1241         glob_t globbuf;
1242         int ret;
1243
1244         /* Warning: glob() is replaced in Windows native builds */
1245         ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1246         if (ret) {
1247                 if (ret == GLOB_NOMATCH) {
1248                         imagex_error(T("Found no files for glob \"%"TS"\""),
1249                                      swm_glob);
1250                 } else {
1251                         imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1252                                                 swm_glob);
1253                 }
1254                 ret = -1;
1255                 goto out;
1256         }
1257         num_additional_swms = globbuf.gl_pathc;
1258         additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1259         if (!additional_swms) {
1260                 imagex_error(T("Out of memory"));
1261                 ret = -1;
1262                 goto out_globfree;
1263         }
1264         unsigned offset = 0;
1265         for (unsigned i = 0; i < num_additional_swms; i++) {
1266                 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1267                         offset++;
1268                         continue;
1269                 }
1270                 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1271                                       open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1272                                       &additional_swms[i - offset],
1273                                       imagex_progress_func);
1274                 if (ret)
1275                         goto out_close_swms;
1276         }
1277         *additional_swms_ret = additional_swms;
1278         *num_additional_swms_ret = num_additional_swms - offset;
1279         ret = 0;
1280         goto out_globfree;
1281 out_close_swms:
1282         for (unsigned i = 0; i < num_additional_swms; i++)
1283                 wimlib_free(additional_swms[i]);
1284         free(additional_swms);
1285 out_globfree:
1286         globfree(&globbuf);
1287 out:
1288         return ret;
1289 }
1290
1291
1292 static unsigned
1293 parse_num_threads(const tchar *optarg)
1294 {
1295         tchar *tmp;
1296         unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1297         if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1298                 imagex_error(T("Number of threads must be a non-negative integer!"));
1299                 return UINT_MAX;
1300         } else {
1301                 return ul_nthreads;
1302         }
1303 }
1304
1305 /*
1306  * Parse an option passed to an update command.
1307  *
1308  * @op:         One of WIMLIB_UPDATE_OP_* that indicates the command being
1309  *              parsed.
1310  *
1311  * @option:     Text string for the option (beginning with --)
1312  *
1313  * @cmd:        `struct wimlib_update_command' that is being constructed for
1314  *              this command.
1315  *
1316  * Returns true if the option was recognized; false if not.
1317  */
1318 static bool
1319 update_command_add_option(int op, const tchar *option,
1320                           struct wimlib_update_command *cmd)
1321 {
1322         bool recognized = true;
1323         switch (op) {
1324         case WIMLIB_UPDATE_OP_ADD:
1325                 if (!tstrcmp(option, T("--verbose")))
1326                         cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1327                 else if (!tstrcmp(option, T("--unix-data")))
1328                         cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1329                 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1330                         cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1331                 else if (!tstrcmp(option, T("--strict-acls")))
1332                         cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1333                 else if (!tstrcmp(option, T("--dereference")))
1334                         cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1335                 else
1336                         recognized = false;
1337                 break;
1338         case WIMLIB_UPDATE_OP_DELETE:
1339                 if (!tstrcmp(option, T("--force")))
1340                         cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1341                 else if (!tstrcmp(option, T("--recursive")))
1342                         cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1343                 else
1344                         recognized = false;
1345                 break;
1346         default:
1347                 recognized = false;
1348                 break;
1349         }
1350         return recognized;
1351 }
1352
1353 /* How many nonoption arguments each `imagex update' command expects */
1354 static const unsigned update_command_num_nonoptions[] = {
1355         [WIMLIB_UPDATE_OP_ADD] = 2,
1356         [WIMLIB_UPDATE_OP_DELETE] = 1,
1357         [WIMLIB_UPDATE_OP_RENAME] = 2,
1358 };
1359
1360 static void
1361 update_command_add_nonoption(int op, const tchar *nonoption,
1362                              struct wimlib_update_command *cmd,
1363                              unsigned num_nonoptions)
1364 {
1365         switch (op) {
1366         case WIMLIB_UPDATE_OP_ADD:
1367                 if (num_nonoptions == 0)
1368                         cmd->add.fs_source_path = (tchar*)nonoption;
1369                 else
1370                         cmd->add.wim_target_path = (tchar*)nonoption;
1371                 break;
1372         case WIMLIB_UPDATE_OP_DELETE:
1373                 cmd->delete.wim_path = (tchar*)nonoption;
1374                 break;
1375         case WIMLIB_UPDATE_OP_RENAME:
1376                 if (num_nonoptions == 0)
1377                         cmd->rename.wim_source_path = (tchar*)nonoption;
1378                 else
1379                         cmd->rename.wim_target_path = (tchar*)nonoption;
1380                 break;
1381         }
1382 }
1383
1384 /*
1385  * Parse a command passed on stdin to `imagex update'.
1386  *
1387  * @line:       Text of the command.
1388  * @len:        Length of the line, including a null terminator
1389  *              at line[len - 1].
1390  *
1391  * @command:    A `struct wimlib_update_command' to fill in from the parsed
1392  *              line.
1393  *
1394  * @line_number: Line number of the command, for diagnostics.
1395  *
1396  * Returns true on success; returns false on parse error.
1397  */
1398 static bool
1399 parse_update_command(tchar *line, size_t len,
1400                      struct wimlib_update_command *command,
1401                      size_t line_number)
1402 {
1403         int ret;
1404         tchar *command_name;
1405         int op;
1406         size_t num_nonoptions;
1407
1408         /* Get the command name ("add", "delete", "rename") */
1409         ret = parse_string(&line, &len, &command_name);
1410         if (ret != PARSE_STRING_SUCCESS)
1411                 return false;
1412
1413         if (!tstrcasecmp(command_name, T("add"))) {
1414                 op = WIMLIB_UPDATE_OP_ADD;
1415         } else if (!tstrcasecmp(command_name, T("delete"))) {
1416                 op = WIMLIB_UPDATE_OP_DELETE;
1417         } else if (!tstrcasecmp(command_name, T("rename"))) {
1418                 op = WIMLIB_UPDATE_OP_RENAME;
1419         } else {
1420                 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1421                              command_name, line_number);
1422                 return false;
1423         }
1424         command->op = op;
1425
1426         /* Parse additional options and non-options as needed */
1427         num_nonoptions = 0;
1428         for (;;) {
1429                 tchar *next_string;
1430
1431                 ret = parse_string(&line, &len, &next_string);
1432                 if (ret == PARSE_STRING_NONE) /* End of line */
1433                         break;
1434                 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1435                         return false;
1436                 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1437                         /* Option */
1438                         if (!update_command_add_option(op, next_string, command))
1439                         {
1440                                 imagex_error(T("Unrecognized option \"%"TS"\" to "
1441                                                "update command \"%"TS"\" on line %zu"),
1442                                              next_string, command_name, line_number);
1443
1444                                 return false;
1445                         }
1446                 } else {
1447                         /* Nonoption */
1448                         if (num_nonoptions == update_command_num_nonoptions[op])
1449                         {
1450                                 imagex_error(T("Unexpected argument \"%"TS"\" in "
1451                                                "update command on line %zu\n"
1452                                                "       (The \"%"TS"\" command only "
1453                                                "takes %zu nonoption arguments!)\n"),
1454                                              next_string, line_number,
1455                                              command_name, num_nonoptions);
1456                                 return false;
1457                         }
1458                         update_command_add_nonoption(op, next_string,
1459                                                      command, num_nonoptions);
1460                         num_nonoptions++;
1461                 }
1462         }
1463
1464         if (num_nonoptions != update_command_num_nonoptions[op]) {
1465                 imagex_error(T("Not enough arguments to update command "
1466                                "\"%"TS"\" on line %zu"), command_name, line_number);
1467                 return false;
1468         }
1469         return true;
1470 }
1471
1472 static struct wimlib_update_command *
1473 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1474                           size_t *num_cmds_ret)
1475 {
1476         ssize_t nlines;
1477         tchar *p;
1478         struct wimlib_update_command *cmds;
1479         size_t i, j;
1480
1481         nlines = text_file_count_lines(cmd_file_contents_p,
1482                                        &cmd_file_nchars);
1483         if (nlines < 0)
1484                 return NULL;
1485
1486         /* Always allocate at least 1 slot, just in case the implementation of
1487          * calloc() returns NULL if 0 bytes are requested. */
1488         cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1489         if (!cmds) {
1490                 imagex_error(T("out of memory"));
1491                 return NULL;
1492         }
1493         p = *cmd_file_contents_p;
1494         j = 0;
1495         for (i = 0; i < nlines; i++) {
1496                 /* XXX: Could use rawmemchr() here instead, but it may not be
1497                  * available on all platforms. */
1498                 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1499                 size_t len = endp - p + 1;
1500                 *endp = T('\0');
1501                 if (!is_comment_line(p, len)) {
1502                         if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1503                                 free(cmds);
1504                                 return NULL;
1505                         }
1506                 }
1507                 p = endp + 1;
1508         }
1509         *num_cmds_ret = j;
1510         return cmds;
1511 }
1512
1513 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1514  * one image from a WIM file to a NTFS volume. */
1515 static int
1516 imagex_apply(int argc, tchar **argv)
1517 {
1518         int c;
1519         int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1520         int image;
1521         WIMStruct *wim;
1522         int ret;
1523         const tchar *wimfile;
1524         const tchar *target;
1525         int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1526
1527         const tchar *swm_glob = NULL;
1528         WIMStruct **additional_swms;
1529         unsigned num_additional_swms;
1530
1531         for_opt(c, apply_options) {
1532                 switch (c) {
1533                 case IMAGEX_CHECK_OPTION:
1534                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1535                         break;
1536                 case IMAGEX_HARDLINK_OPTION:
1537                         extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1538                         break;
1539                 case IMAGEX_SYMLINK_OPTION:
1540                         extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1541                         break;
1542                 case IMAGEX_VERBOSE_OPTION:
1543                         extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1544                         break;
1545                 case IMAGEX_REF_OPTION:
1546                         swm_glob = optarg;
1547                         break;
1548                 case IMAGEX_UNIX_DATA_OPTION:
1549                         extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1550                         break;
1551                 case IMAGEX_NO_ACLS_OPTION:
1552                         extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1553                         break;
1554                 case IMAGEX_STRICT_ACLS_OPTION:
1555                         extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1556                         break;
1557                 case IMAGEX_NORPFIX_OPTION:
1558                         extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1559                         break;
1560                 case IMAGEX_RPFIX_OPTION:
1561                         extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1562                         break;
1563                 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1564                         extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1565                         extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1566                         break;
1567                 default:
1568                         goto out_usage;
1569                 }
1570         }
1571         argc -= optind;
1572         argv += optind;
1573         if (argc != 2 && argc != 3)
1574                 goto out_usage;
1575
1576         wimfile = argv[0];
1577
1578         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
1579         if (ret)
1580                 goto out;
1581
1582         if (argc >= 3) {
1583                 /* Image explicitly specified.  */
1584                 image = wimlib_resolve_image(wim, argv[1]);
1585                 ret = verify_image_exists(image, argv[1], wimfile);
1586                 if (ret)
1587                         goto out_wimlib_free;
1588                 target = argv[2];
1589         } else {
1590                 /* No image specified; default to image 1, but only if the WIM
1591                  * contains exactly one image.  */
1592                 struct wimlib_wim_info info;
1593
1594                 wimlib_get_wim_info(wim, &info);
1595                 if (info.image_count != 1) {
1596                         imagex_error(T("\"%"TS"\" contains %d images; "
1597                                        "Please select one (or all)."),
1598                                      wimfile, info.image_count);
1599                         wimlib_free(wim);
1600                         goto out_usage;
1601                 }
1602                 image = 1;
1603                 target = argv[1];
1604         }
1605
1606         if (swm_glob) {
1607                 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1608                                           &additional_swms,
1609                                           &num_additional_swms);
1610                 if (ret)
1611                         goto out_wimlib_free;
1612         } else {
1613                 additional_swms = NULL;
1614                 num_additional_swms = 0;
1615         }
1616
1617
1618 #ifndef __WIN32__
1619         {
1620                 /* Interpret a regular file or block device target as a NTFS
1621                  * volume.  */
1622                 struct stat stbuf;
1623
1624                 if (tstat(target, &stbuf)) {
1625                         if (errno != ENOENT) {
1626                                 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1627                                                         target);
1628                                 ret = -1;
1629                                 goto out_free_swms;
1630                         }
1631                 } else {
1632                         if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1633                                 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1634                 }
1635         }
1636 #endif
1637
1638 #ifdef __WIN32__
1639         win32_acquire_restore_privileges();
1640 #endif
1641         ret = wimlib_extract_image(wim, image, target, extract_flags,
1642                                    additional_swms, num_additional_swms,
1643                                    imagex_progress_func);
1644         if (ret == 0)
1645                 tprintf(T("Done applying WIM image.\n"));
1646 #ifdef __WIN32__
1647         win32_release_restore_privileges();
1648 #endif
1649 out_free_swms:
1650         if (swm_glob) {
1651                 for (unsigned i = 0; i < num_additional_swms; i++)
1652                         wimlib_free(additional_swms[i]);
1653                 free(additional_swms);
1654         }
1655 out_wimlib_free:
1656         wimlib_free(wim);
1657 out:
1658         return ret;
1659
1660 out_usage:
1661         usage(APPLY);
1662         ret = -1;
1663         goto out;
1664 }
1665
1666 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1667  * directory trees.  'wimlib-imagex capture': create a new WIM file containing
1668  * the desired image.  'wimlib-imagex append': add a new image to an existing
1669  * WIM file. */
1670 static int
1671 imagex_capture_or_append(int argc, tchar **argv)
1672 {
1673         int c;
1674         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1675         int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1676         int write_flags = 0;
1677         int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1678         const tchar *wimfile;
1679         const tchar *name;
1680         const tchar *desc;
1681         const tchar *flags_element = NULL;
1682         WIMStruct *wim;
1683         int ret;
1684         int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1685         unsigned num_threads = 0;
1686
1687         tchar *source;
1688         tchar *source_copy;
1689
1690         const tchar *config_file = NULL;
1691         tchar *config_str;
1692         struct wimlib_capture_config *config;
1693
1694         bool source_list = false;
1695         size_t source_list_nchars;
1696         tchar *source_list_contents;
1697         bool capture_sources_malloced;
1698         struct wimlib_capture_source *capture_sources;
1699         size_t num_sources;
1700         bool name_defaulted;
1701
1702         for_opt(c, capture_or_append_options) {
1703                 switch (c) {
1704                 case IMAGEX_BOOT_OPTION:
1705                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1706                         break;
1707                 case IMAGEX_CHECK_OPTION:
1708                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1709                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1710                         break;
1711                 case IMAGEX_CONFIG_OPTION:
1712                         config_file = optarg;
1713                         break;
1714                 case IMAGEX_COMPRESS_OPTION:
1715                         compression_type = get_compression_type(optarg);
1716                         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1717                                 goto out_err;
1718                         break;
1719                 case IMAGEX_FLAGS_OPTION:
1720                         flags_element = optarg;
1721                         break;
1722                 case IMAGEX_DEREFERENCE_OPTION:
1723                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1724                         break;
1725                 case IMAGEX_VERBOSE_OPTION:
1726                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1727                         break;
1728                 case IMAGEX_THREADS_OPTION:
1729                         num_threads = parse_num_threads(optarg);
1730                         if (num_threads == UINT_MAX)
1731                                 goto out_err;
1732                         break;
1733                 case IMAGEX_REBUILD_OPTION:
1734                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1735                         break;
1736                 case IMAGEX_UNIX_DATA_OPTION:
1737                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1738                         break;
1739                 case IMAGEX_SOURCE_LIST_OPTION:
1740                         source_list = true;
1741                         break;
1742                 case IMAGEX_NO_ACLS_OPTION:
1743                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1744                         break;
1745                 case IMAGEX_STRICT_ACLS_OPTION:
1746                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1747                         break;
1748                 case IMAGEX_RPFIX_OPTION:
1749                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1750                         break;
1751                 case IMAGEX_NORPFIX_OPTION:
1752                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1753                         break;
1754                 default:
1755                         goto out_usage;
1756                 }
1757         }
1758         argc -= optind;
1759         argv += optind;
1760
1761         if (argc < 2 || argc > 4)
1762                 goto out_usage;
1763
1764         source = argv[0];
1765         wimfile = argv[1];
1766
1767         if (argc >= 3) {
1768                 name = argv[2];
1769                 name_defaulted = false;
1770         } else {
1771                 /* Set default name to SOURCE argument, omitting any directory
1772                  * prefixes and trailing slashes.  This requires making a copy
1773                  * of @source.  Leave some free characters at the end in case we
1774                  * append a number to keep the name unique. */
1775                 size_t source_name_len;
1776
1777                 source_name_len = tstrlen(source);
1778                 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1779                 name = tbasename(tstrcpy(source_copy, source));
1780                 name_defaulted = true;
1781         }
1782         /* Image description defaults to NULL if not given. */
1783         if (argc >= 4)
1784                 desc = argv[3];
1785         else
1786                 desc = NULL;
1787
1788         if (source_list) {
1789                 /* Set up capture sources in source list mode */
1790                 if (source[0] == T('-') && source[1] == T('\0')) {
1791                         source_list_contents = stdin_get_text_contents(&source_list_nchars);
1792                 } else {
1793                         source_list_contents = file_get_text_contents(source,
1794                                                                       &source_list_nchars);
1795                 }
1796                 if (!source_list_contents)
1797                         goto out_err;
1798
1799                 capture_sources = parse_source_list(&source_list_contents,
1800                                                     source_list_nchars,
1801                                                     &num_sources);
1802                 if (!capture_sources) {
1803                         ret = -1;
1804                         goto out_free_source_list_contents;
1805                 }
1806                 capture_sources_malloced = true;
1807         } else {
1808                 /* Set up capture source in non-source-list mode (could be
1809                  * either "normal" mode or "NTFS mode"--- see the man page). */
1810                 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1811                 capture_sources[0].fs_source_path = source;
1812                 capture_sources[0].wim_target_path = NULL;
1813                 capture_sources[0].reserved = 0;
1814                 num_sources = 1;
1815                 capture_sources_malloced = false;
1816                 source_list_contents = NULL;
1817         }
1818
1819         if (config_file) {
1820                 size_t config_len;
1821
1822                 config_str = file_get_text_contents(config_file, &config_len);
1823                 if (!config_str) {
1824                         ret = -1;
1825                         goto out_free_capture_sources;
1826                 }
1827
1828                 config = alloca(sizeof(*config));
1829                 ret = parse_capture_config(&config_str, config_len, config);
1830                 if (ret)
1831                         goto out_free_config;
1832         } else {
1833                 config = &default_capture_config;
1834         }
1835
1836         if (cmd == APPEND)
1837                 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1838                                       imagex_progress_func);
1839         else
1840                 ret = wimlib_create_new_wim(compression_type, &wim);
1841         if (ret)
1842                 goto out_free_config;
1843
1844 #ifndef __WIN32__
1845         if (!source_list) {
1846                 struct stat stbuf;
1847
1848                 if (tstat(source, &stbuf) == 0) {
1849                         if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1850                                 tprintf(T("Capturing WIM image from NTFS "
1851                                           "filesystem on \"%"TS"\"\n"), source);
1852                                 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1853                         }
1854                 } else {
1855                         if (errno != ENOENT) {
1856                                 imagex_error_with_errno(T("Failed to stat "
1857                                                           "\"%"TS"\""), source);
1858                                 ret = -1;
1859                                 goto out_wimlib_free;
1860                         }
1861                 }
1862         }
1863 #endif
1864
1865         if (cmd == APPEND && name_defaulted) {
1866                 /* If the user did not specify an image name, and the basename
1867                  * of the source already exists as an image name in the WIM
1868                  * file, append a suffix to make it unique. */
1869                 unsigned long conflict_idx;
1870                 tchar *name_end = tstrchr(name, T('\0'));
1871                 for (conflict_idx = 1;
1872                      wimlib_image_name_in_use(wim, name);
1873                      conflict_idx++)
1874                 {
1875                         tsprintf(name_end, T(" (%lu)"), conflict_idx);
1876                 }
1877         }
1878 #ifdef __WIN32__
1879         win32_acquire_capture_privileges();
1880 #endif
1881
1882         ret = wimlib_add_image_multisource(wim,
1883                                            capture_sources,
1884                                            num_sources,
1885                                            name,
1886                                            config,
1887                                            add_image_flags,
1888                                            imagex_progress_func);
1889         if (ret)
1890                 goto out_release_privs;
1891
1892         if (desc || flags_element) {
1893                 /* User provided <DESCRIPTION> or <FLAGS> element.  Get the
1894                  * index of the image we just added, then use it to call the
1895                  * appropriate functions.  */
1896                 struct wimlib_wim_info info;
1897
1898                 wimlib_get_wim_info(wim, &info);
1899
1900                 if (desc) {
1901                         ret = wimlib_set_image_descripton(wim,
1902                                                           info.image_count,
1903                                                           desc);
1904                         if (ret)
1905                                 goto out_release_privs;
1906                 }
1907
1908                 if (flags_element) {
1909                         ret = wimlib_set_image_flags(wim, info.image_count,
1910                                                      flags_element);
1911                         if (ret)
1912                                 goto out_release_privs;
1913                 }
1914         }
1915
1916         if (cmd == APPEND) {
1917                 ret = wimlib_overwrite(wim, write_flags, num_threads,
1918                                        imagex_progress_func);
1919         } else {
1920                 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1921                                    num_threads, imagex_progress_func);
1922         }
1923         if (ret)
1924                 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1925                              wimfile);
1926 out_release_privs:
1927 #ifdef __WIN32__
1928         win32_release_capture_privileges();
1929 #endif
1930 out_wimlib_free:
1931         wimlib_free(wim);
1932 out_free_config:
1933         if (config != &default_capture_config) {
1934                 free(config->exclusion_pats.pats);
1935                 free(config->exclusion_exception_pats.pats);
1936                 free(config_str);
1937         }
1938 out_free_capture_sources:
1939         if (capture_sources_malloced)
1940                 free(capture_sources);
1941 out_free_source_list_contents:
1942         free(source_list_contents);
1943 out:
1944         return ret;
1945
1946 out_usage:
1947         usage(cmd);
1948 out_err:
1949         ret = -1;
1950         goto out;
1951 }
1952
1953 /* Remove image(s) from a WIM. */
1954 static int
1955 imagex_delete(int argc, tchar **argv)
1956 {
1957         int c;
1958         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1959         int write_flags = 0;
1960         const tchar *wimfile;
1961         const tchar *image_num_or_name;
1962         WIMStruct *wim;
1963         int image;
1964         int ret;
1965
1966         for_opt(c, delete_options) {
1967                 switch (c) {
1968                 case IMAGEX_CHECK_OPTION:
1969                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1970                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1971                         break;
1972                 case IMAGEX_SOFT_OPTION:
1973                         write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1974                         break;
1975                 default:
1976                         goto out_usage;
1977                 }
1978         }
1979         argc -= optind;
1980         argv += optind;
1981
1982         if (argc != 2) {
1983                 if (argc < 1)
1984                         imagex_error(T("Must specify a WIM file"));
1985                 if (argc < 2)
1986                         imagex_error(T("Must specify an image"));
1987                 goto out_usage;
1988         }
1989         wimfile = argv[0];
1990         image_num_or_name = argv[1];
1991
1992         ret = wimlib_open_wim(wimfile, open_flags, &wim,
1993                               imagex_progress_func);
1994         if (ret)
1995                 goto out;
1996
1997         image = wimlib_resolve_image(wim, image_num_or_name);
1998
1999         ret = verify_image_exists(image, image_num_or_name, wimfile);
2000         if (ret)
2001                 goto out_wimlib_free;
2002
2003         ret = wimlib_delete_image(wim, image);
2004         if (ret) {
2005                 imagex_error(T("Failed to delete image from \"%"TS"\""),
2006                              wimfile);
2007                 goto out_wimlib_free;
2008         }
2009
2010         ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2011         if (ret) {
2012                 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2013                                "deleted"), wimfile);
2014         }
2015 out_wimlib_free:
2016         wimlib_free(wim);
2017 out:
2018         return ret;
2019
2020 out_usage:
2021         usage(DELETE);
2022         ret = -1;
2023         goto out;
2024 }
2025
2026 static int
2027 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2028 {
2029         int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2030         return (ret >= 0) ? 0 : -1;
2031 }
2032
2033 /* Print the files contained in an image(s) in a WIM file. */
2034 static int
2035 imagex_dir(int argc, tchar **argv)
2036 {
2037         const tchar *wimfile;
2038         WIMStruct *wim = NULL;
2039         int image;
2040         int ret;
2041         const tchar *path = T("");
2042         int c;
2043
2044         for_opt(c, dir_options) {
2045                 switch (c) {
2046                 case IMAGEX_PATH_OPTION:
2047                         path = optarg;
2048                         break;
2049                 default:
2050                         goto out_usage;
2051                 }
2052         }
2053         argc -= optind;
2054         argv += optind;
2055
2056         if (argc < 1) {
2057                 imagex_error(T("Must specify a WIM file"));
2058                 goto out_usage;
2059         }
2060         if (argc > 2) {
2061                 imagex_error(T("Too many arguments"));
2062                 goto out_usage;
2063         }
2064
2065         wimfile = argv[0];
2066         ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &wim,
2067                               imagex_progress_func);
2068         if (ret)
2069                 goto out;
2070
2071         if (argc >= 2) {
2072                 image = wimlib_resolve_image(wim, argv[1]);
2073                 ret = verify_image_exists(image, argv[1], wimfile);
2074                 if (ret)
2075                         goto out_wimlib_free;
2076         } else {
2077                 /* No image specified; default to image 1, but only if the WIM
2078                  * contains exactly one image.  */
2079
2080                 struct wimlib_wim_info info;
2081
2082                 wimlib_get_wim_info(wim, &info);
2083                 if (info.image_count != 1) {
2084                         imagex_error(T("\"%"TS"\" contains %d images; Please "
2085                                        "select one (or all)."),
2086                                      wimfile, info.image_count);
2087                         wimlib_free(wim);
2088                         goto out_usage;
2089                 }
2090                 image = 1;
2091         }
2092
2093         ret = wimlib_iterate_dir_tree(wim, image, path,
2094                                       WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2095                                       print_full_path, NULL);
2096 out_wimlib_free:
2097         wimlib_free(wim);
2098 out:
2099         return ret;
2100
2101 out_usage:
2102         usage(DIR);
2103         ret = -1;
2104         goto out;
2105 }
2106
2107 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2108  * WIM file. */
2109 static int
2110 imagex_export(int argc, tchar **argv)
2111 {
2112         int c;
2113         int open_flags = 0;
2114         int export_flags = 0;
2115         int write_flags = 0;
2116         int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2117         const tchar *src_wimfile;
2118         const tchar *src_image_num_or_name;
2119         const tchar *dest_wimfile;
2120         const tchar *dest_name;
2121         const tchar *dest_desc;
2122         WIMStruct *src_wim;
2123         WIMStruct *dest_wim;
2124         int ret;
2125         int image;
2126         struct stat stbuf;
2127         bool wim_is_new;
2128         const tchar *swm_glob = NULL;
2129         WIMStruct **additional_swms;
2130         unsigned num_additional_swms;
2131         unsigned num_threads = 0;
2132
2133         for_opt(c, export_options) {
2134                 switch (c) {
2135                 case IMAGEX_BOOT_OPTION:
2136                         export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2137                         break;
2138                 case IMAGEX_CHECK_OPTION:
2139                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2140                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2141                         break;
2142                 case IMAGEX_COMPRESS_OPTION:
2143                         compression_type = get_compression_type(optarg);
2144                         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2145                                 goto out_err;
2146                         break;
2147                 case IMAGEX_REF_OPTION:
2148                         swm_glob = optarg;
2149                         break;
2150                 case IMAGEX_THREADS_OPTION:
2151                         num_threads = parse_num_threads(optarg);
2152                         if (num_threads == UINT_MAX)
2153                                 goto out_err;
2154                         break;
2155                 case IMAGEX_REBUILD_OPTION:
2156                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2157                         break;
2158                 default:
2159                         goto out_usage;
2160                 }
2161         }
2162         argc -= optind;
2163         argv += optind;
2164         if (argc < 3 || argc > 5)
2165                 goto out_usage;
2166         src_wimfile           = argv[0];
2167         src_image_num_or_name = argv[1];
2168         dest_wimfile          = argv[2];
2169         dest_name             = (argc >= 4) ? argv[3] : NULL;
2170         dest_desc             = (argc >= 5) ? argv[4] : NULL;
2171         ret = wimlib_open_wim(src_wimfile,
2172                               open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_wim,
2173                               imagex_progress_func);
2174         if (ret)
2175                 goto out;
2176
2177         /* Determine if the destination is an existing file or not.
2178          * If so, we try to append the exported image(s) to it; otherwise, we
2179          * create a new WIM containing the exported image(s). */
2180         if (tstat(dest_wimfile, &stbuf) == 0) {
2181                 wim_is_new = false;
2182                 /* Destination file exists. */
2183
2184                 if (!S_ISREG(stbuf.st_mode)) {
2185                         imagex_error(T("\"%"TS"\" is not a regular file"),
2186                                      dest_wimfile);
2187                         ret = -1;
2188                         goto out_free_src_wim;
2189                 }
2190                 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2191                                       &dest_wim, imagex_progress_func);
2192                 if (ret)
2193                         goto out_free_src_wim;
2194
2195                 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2196                         /* The user specified a compression type, but we're
2197                          * exporting to an existing WIM.  Make sure the
2198                          * specified compression type is the same as the
2199                          * compression type of the existing destination WIM. */
2200                         struct wimlib_wim_info dest_info;
2201
2202                         wimlib_get_wim_info(dest_wim, &dest_info);
2203                         if (compression_type != dest_info.compression_type) {
2204                                 imagex_error(T("Cannot specify a compression type that is "
2205                                                "not the same as that used in the "
2206                                                "destination WIM"));
2207                                 ret = -1;
2208                                 goto out_free_dest_wim;
2209                         }
2210                 }
2211         } else {
2212                 wim_is_new = true;
2213
2214                 if (errno != ENOENT) {
2215                         imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2216                                                 dest_wimfile);
2217                         ret = -1;
2218                         goto out_free_src_wim;
2219                 }
2220
2221                 /* dest_wimfile is not an existing file, so create a new WIM. */
2222
2223                 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2224                         /* The user did not specify a compression type; default
2225                          * to that of the source WIM.  */
2226
2227                         struct wimlib_wim_info src_info;
2228
2229                         wimlib_get_wim_info(src_wim, &src_info);
2230                         compression_type = src_info.compression_type;
2231                 }
2232                 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2233                 if (ret)
2234                         goto out_free_src_wim;
2235         }
2236
2237         image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2238         ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2239         if (ret)
2240                 goto out_free_dest_wim;
2241
2242         if (swm_glob) {
2243                 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2244                                           &additional_swms,
2245                                           &num_additional_swms);
2246                 if (ret)
2247                         goto out_free_dest_wim;
2248         } else {
2249                 additional_swms = NULL;
2250                 num_additional_swms = 0;
2251         }
2252
2253         ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2254                                   dest_desc, export_flags, additional_swms,
2255                                   num_additional_swms, imagex_progress_func);
2256         if (ret)
2257                 goto out_free_swms;
2258
2259         if (wim_is_new)
2260                 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2261                                    write_flags, num_threads,
2262                                    imagex_progress_func);
2263         else
2264                 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2265                                        imagex_progress_func);
2266 out_free_swms:
2267         if (swm_glob) {
2268                 for (unsigned i = 0; i < num_additional_swms; i++)
2269                         wimlib_free(additional_swms[i]);
2270                 free(additional_swms);
2271         }
2272 out_free_dest_wim:
2273         wimlib_free(dest_wim);
2274 out_free_src_wim:
2275         wimlib_free(src_wim);
2276 out:
2277         return ret;
2278
2279 out_usage:
2280         usage(EXPORT);
2281 out_err:
2282         ret = -1;
2283         goto out;
2284 }
2285
2286 static bool
2287 is_root_wim_path(const tchar *path)
2288 {
2289         const tchar *p;
2290         for (p = path; *p; p++)
2291                 if (*p != T('\\') && *p != T('/'))
2292                         return false;
2293         return true;
2294 }
2295
2296 static void
2297 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2298                       const tchar *dest_dir)
2299 {
2300         for (size_t i = 0; i < num_cmds; i++)
2301                 if (cmds[i].fs_dest_path != dest_dir)
2302                         free(cmds[i].fs_dest_path);
2303         free(cmds);
2304 }
2305
2306 static struct wimlib_extract_command *
2307 prepare_extract_commands(tchar **paths, unsigned num_paths,
2308                          int extract_flags, tchar *dest_dir,
2309                          size_t *num_cmds_ret)
2310 {
2311         struct wimlib_extract_command *cmds;
2312         size_t num_cmds;
2313         tchar *emptystr = T("");
2314
2315         if (num_paths == 0) {
2316                 num_paths = 1;
2317                 paths = &emptystr;
2318         }
2319         num_cmds = num_paths;
2320         cmds = calloc(num_cmds, sizeof(cmds[0]));
2321         if (!cmds) {
2322                 imagex_error(T("Out of memory!"));
2323                 return NULL;
2324         }
2325
2326         for (size_t i = 0; i < num_cmds; i++) {
2327                 cmds[i].extract_flags = extract_flags;
2328                 cmds[i].wim_source_path = paths[i];
2329                 if (is_root_wim_path(paths[i])) {
2330                         cmds[i].fs_dest_path = dest_dir;
2331                 } else {
2332                         size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2333                         cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2334                         if (!cmds[i].fs_dest_path) {
2335                                 free_extract_commands(cmds, num_cmds, dest_dir);
2336                                 return NULL;
2337                         }
2338                         tsprintf(cmds[i].fs_dest_path,
2339                                  T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2340                                  dest_dir, tbasename(paths[i]));
2341                 }
2342         }
2343         *num_cmds_ret = num_cmds;
2344         return cmds;
2345 }
2346
2347 /* Extract files or directories from a WIM image */
2348 static int
2349 imagex_extract(int argc, tchar **argv)
2350 {
2351         int c;
2352         int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2353         int image;
2354         WIMStruct *wim;
2355         int ret;
2356         const tchar *wimfile;
2357         const tchar *image_num_or_name;
2358         tchar *dest_dir = T(".");
2359         int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2360
2361         const tchar *swm_glob = NULL;
2362         WIMStruct **additional_swms;
2363         unsigned num_additional_swms;
2364
2365         struct wimlib_extract_command *cmds;
2366         size_t num_cmds;
2367
2368         for_opt(c, extract_options) {
2369                 switch (c) {
2370                 case IMAGEX_CHECK_OPTION:
2371                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2372                         break;
2373                 case IMAGEX_VERBOSE_OPTION:
2374                         extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2375                         break;
2376                 case IMAGEX_REF_OPTION:
2377                         swm_glob = optarg;
2378                         break;
2379                 case IMAGEX_UNIX_DATA_OPTION:
2380                         extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2381                         break;
2382                 case IMAGEX_NO_ACLS_OPTION:
2383                         extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2384                         break;
2385                 case IMAGEX_STRICT_ACLS_OPTION:
2386                         extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2387                         break;
2388                 case IMAGEX_DEST_DIR_OPTION:
2389                         dest_dir = optarg;
2390                         break;
2391                 case IMAGEX_TO_STDOUT_OPTION:
2392                         extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2393                         imagex_be_quiet = true;
2394                         break;
2395                 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2396                         extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2397                         extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2398                         break;
2399                 default:
2400                         goto out_usage;
2401                 }
2402         }
2403         argc -= optind;
2404         argv += optind;
2405
2406         if (argc < 2)
2407                 goto out_usage;
2408
2409         wimfile = argv[0];
2410         image_num_or_name = argv[1];
2411
2412         argc -= 2;
2413         argv += 2;
2414
2415         cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2416                                         &num_cmds);
2417         if (!cmds)
2418                 goto out_err;
2419
2420         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2421         if (ret)
2422                 goto out_free_cmds;
2423
2424         image = wimlib_resolve_image(wim, image_num_or_name);
2425         ret = verify_image_exists_and_is_single(image,
2426                                                 image_num_or_name,
2427                                                 wimfile);
2428         if (ret)
2429                 goto out_wimlib_free;
2430
2431         if (swm_glob) {
2432                 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2433                                           &additional_swms,
2434                                           &num_additional_swms);
2435                 if (ret)
2436                         goto out_wimlib_free;
2437         } else {
2438                 additional_swms = NULL;
2439                 num_additional_swms = 0;
2440         }
2441
2442 #ifdef __WIN32__
2443         win32_acquire_restore_privileges();
2444 #endif
2445
2446         ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2447                                    additional_swms, num_additional_swms,
2448                                    imagex_progress_func);
2449         if (ret == 0) {
2450                 if (!imagex_be_quiet)
2451                         tprintf(T("Done extracting files.\n"));
2452         } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2453                 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2454                                    "files and directories\n"
2455                                    "      are in the WIM image.\n"));
2456         }
2457 #ifdef __WIN32__
2458         win32_release_restore_privileges();
2459 #endif
2460         if (swm_glob) {
2461                 for (unsigned i = 0; i < num_additional_swms; i++)
2462                         wimlib_free(additional_swms[i]);
2463                 free(additional_swms);
2464         }
2465 out_wimlib_free:
2466         wimlib_free(wim);
2467 out_free_cmds:
2468         free_extract_commands(cmds, num_cmds, dest_dir);
2469 out:
2470         return ret;
2471
2472 out_usage:
2473         usage(EXTRACT);
2474 out_err:
2475         ret = -1;
2476         goto out;
2477 }
2478
2479 static void print_byte_field(const uint8_t field[], size_t len)
2480 {
2481         while (len--)
2482                 tprintf(T("%02hhx"), *field++);
2483 }
2484
2485 static void
2486 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2487 {
2488         tputs(T("WIM Information:"));
2489         tputs(T("----------------"));
2490         tprintf(T("Path:           %"TS"\n"), wimfile);
2491         tprintf(T("GUID:           0x"));
2492         print_byte_field(info->guid, sizeof(info->guid));
2493         tputchar(T('\n'));
2494         tprintf(T("Image Count:    %d\n"), info->image_count);
2495         tprintf(T("Compression:    %"TS"\n"),
2496                 wimlib_get_compression_type_string(info->compression_type));
2497         tprintf(T("Part Number:    %d/%d\n"), info->part_number, info->total_parts);
2498         tprintf(T("Boot Index:     %d\n"), info->boot_index);
2499         tprintf(T("Size:           %"PRIu64" bytes\n"), info->total_bytes);
2500         tprintf(T("Integrity Info: %"TS"\n"),
2501                 info->has_integrity_table ? T("yes") : T("no"));
2502         tprintf(T("Relative path junction: %"TS"\n"),
2503                 info->has_rpfix ? T("yes") : T("no"));
2504         tputchar(T('\n'));
2505 }
2506
2507 static int
2508 print_resource(const struct wimlib_resource_entry *resource,
2509                void *_ignore)
2510 {
2511
2512         tprintf(T("Uncompressed size   = %"PRIu64" bytes\n"),
2513                 resource->uncompressed_size);
2514
2515         tprintf(T("Compressed size     = %"PRIu64" bytes\n"),
2516                 resource->compressed_size);
2517
2518         tprintf(T("Offset              = %"PRIu64" bytes\n"),
2519                 resource->offset);
2520
2521
2522         tprintf(T("Part Number         = %u\n"), resource->part_number);
2523         tprintf(T("Reference Count     = %u\n"), resource->reference_count);
2524
2525         tprintf(T("Hash                = 0x"));
2526         print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2527         tputchar(T('\n'));
2528
2529         tprintf(T("Flags               = "));
2530         if (resource->is_compressed)
2531                 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED  "));
2532         if (resource->is_metadata)
2533                 tprintf(T("WIM_RESHDR_FLAG_METADATA  "));
2534         if (resource->is_free)
2535                 tprintf(T("WIM_RESHDR_FLAG_FREE  "));
2536         if (resource->is_spanned)
2537                 tprintf(T("WIM_RESHDR_FLAG_SPANNED  "));
2538         tputchar(T('\n'));
2539         tputchar(T('\n'));
2540         return 0;
2541 }
2542
2543 static void
2544 print_lookup_table(WIMStruct *wim)
2545 {
2546         wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2547 }
2548
2549 /* Prints information about a WIM file; also can mark an image as bootable,
2550  * change the name of an image, or change the description of an image. */
2551 static int
2552 imagex_info(int argc, tchar **argv)
2553 {
2554         int c;
2555         bool boot         = false;
2556         bool check        = false;
2557         bool header       = false;
2558         bool lookup_table = false;
2559         bool xml          = false;
2560         bool metadata     = false;
2561         bool short_header = true;
2562         const tchar *xml_out_file = NULL;
2563         const tchar *wimfile;
2564         const tchar *image_num_or_name;
2565         const tchar *new_name;
2566         const tchar *new_desc;
2567         WIMStruct *wim;
2568         int image;
2569         int ret;
2570         int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2571         struct wimlib_wim_info info;
2572
2573         for_opt(c, info_options) {
2574                 switch (c) {
2575                 case IMAGEX_BOOT_OPTION:
2576                         boot = true;
2577                         break;
2578                 case IMAGEX_CHECK_OPTION:
2579                         check = true;
2580                         break;
2581                 case IMAGEX_HEADER_OPTION:
2582                         header = true;
2583                         short_header = false;
2584                         break;
2585                 case IMAGEX_LOOKUP_TABLE_OPTION:
2586                         lookup_table = true;
2587                         short_header = false;
2588                         break;
2589                 case IMAGEX_XML_OPTION:
2590                         xml = true;
2591                         short_header = false;
2592                         break;
2593                 case IMAGEX_EXTRACT_XML_OPTION:
2594                         xml_out_file = optarg;
2595                         short_header = false;
2596                         break;
2597                 case IMAGEX_METADATA_OPTION:
2598                         metadata = true;
2599                         short_header = false;
2600                         break;
2601                 default:
2602                         goto out_usage;
2603                 }
2604         }
2605
2606         argc -= optind;
2607         argv += optind;
2608         if (argc < 1 || argc > 4)
2609                 goto out_usage;
2610
2611         wimfile           = argv[0];
2612         image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2613         new_name          = (argc >= 3) ? argv[2] : NULL;
2614         new_desc          = (argc >= 4) ? argv[3] : NULL;
2615
2616         if (check)
2617                 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2618
2619         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2620         if (ret)
2621                 goto out;
2622
2623         wimlib_get_wim_info(wim, &info);
2624
2625         image = wimlib_resolve_image(wim, image_num_or_name);
2626         ret = WIMLIB_ERR_INVALID_IMAGE;
2627         if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2628                 imagex_error(T("The image \"%"TS"\" does not exist in \"%"TS"\""),
2629                              image_num_or_name, wimfile);
2630                 if (boot) {
2631                         imagex_error(T("If you would like to set the boot "
2632                                        "index to 0, specify image \"0\" with "
2633                                        "the --boot flag."));
2634                 }
2635                 goto out_wimlib_free;
2636         }
2637
2638         if (boot && info.image_count == 0) {
2639                 imagex_error(T("--boot is meaningless on a WIM with no images"));
2640                 goto out_wimlib_free;
2641         }
2642
2643         if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2644                 if (boot) {
2645                         imagex_error(T("Cannot specify the --boot flag "
2646                                        "without specifying a specific "
2647                                        "image in a multi-image WIM"));
2648                         goto out_wimlib_free;
2649                 }
2650                 if (new_name) {
2651                         imagex_error(T("Cannot specify the NEW_NAME "
2652                                        "without specifying a specific "
2653                                        "image in a multi-image WIM"));
2654                         goto out_wimlib_free;
2655                 }
2656         }
2657
2658         /* Operations that print information are separated from operations that
2659          * recreate the WIM file. */
2660         if (!new_name && !boot) {
2661
2662                 /* Read-only operations */
2663
2664                 if (image == WIMLIB_NO_IMAGE) {
2665                         imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2666                                      image_num_or_name, wimfile);
2667                         goto out_wimlib_free;
2668                 }
2669
2670                 if (image == WIMLIB_ALL_IMAGES && short_header)
2671                         print_wim_information(wimfile, &info);
2672
2673                 if (header)
2674                         wimlib_print_header(wim);
2675
2676                 if (lookup_table) {
2677                         if (info.total_parts != 1) {
2678                                 tfprintf(stderr, T("Warning: Only showing the lookup table "
2679                                                    "for part %d of a %d-part WIM.\n"),
2680                                          info.part_number, info.total_parts);
2681                         }
2682                         print_lookup_table(wim);
2683                 }
2684
2685                 if (xml) {
2686                         ret = wimlib_extract_xml_data(wim, stdout);
2687                         if (ret)
2688                                 goto out_wimlib_free;
2689                 }
2690
2691                 if (xml_out_file) {
2692                         FILE *fp;
2693
2694                         fp = tfopen(xml_out_file, T("wb"));
2695                         if (!fp) {
2696                                 imagex_error_with_errno(T("Failed to open the "
2697                                                           "file \"%"TS"\" for "
2698                                                           "writing"),
2699                                                         xml_out_file);
2700                                 ret = -1;
2701                                 goto out_wimlib_free;
2702                         }
2703                         ret = wimlib_extract_xml_data(wim, fp);
2704                         if (fclose(fp)) {
2705                                 imagex_error(T("Failed to close the file "
2706                                                "\"%"TS"\""),
2707                                              xml_out_file);
2708                                 ret = -1;
2709                         }
2710                         if (ret)
2711                                 goto out_wimlib_free;
2712                 }
2713
2714                 if (short_header)
2715                         wimlib_print_available_images(wim, image);
2716
2717                 if (metadata) {
2718                         ret = wimlib_print_metadata(wim, image);
2719                         if (ret)
2720                                 goto out_wimlib_free;
2721                 }
2722                 ret = 0;
2723         } else {
2724
2725                 /* Modification operations */
2726
2727                 if (image == WIMLIB_ALL_IMAGES)
2728                         image = 1;
2729
2730                 if (image == WIMLIB_NO_IMAGE && new_name) {
2731                         imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2732                                        "when using image 0"), new_name);
2733                         ret = -1;
2734                         goto out_wimlib_free;
2735                 }
2736
2737                 if (boot) {
2738                         if (image == info.boot_index) {
2739                                 tprintf(T("Image %d is already marked as "
2740                                           "bootable.\n"), image);
2741                                 boot = false;
2742                         } else {
2743                                 tprintf(T("Marking image %d as bootable.\n"),
2744                                         image);
2745                                 info.boot_index = image;
2746                                 ret = wimlib_set_wim_info(wim, &info,
2747                                                           WIMLIB_CHANGE_BOOT_INDEX);
2748                                 if (ret)
2749                                         goto out_wimlib_free;
2750                         }
2751                 }
2752                 if (new_name) {
2753                         if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
2754                         {
2755                                 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2756                                         image, new_name);
2757                                 new_name = NULL;
2758                         } else {
2759                                 tprintf(T("Changing the name of image %d to "
2760                                           "\"%"TS"\".\n"), image, new_name);
2761                                 ret = wimlib_set_image_name(wim, image, new_name);
2762                                 if (ret)
2763                                         goto out_wimlib_free;
2764                         }
2765                 }
2766                 if (new_desc) {
2767                         const tchar *old_desc;
2768                         old_desc = wimlib_get_image_description(wim, image);
2769                         if (old_desc && !tstrcmp(old_desc, new_desc)) {
2770                                 tprintf(T("The description of image %d is already "
2771                                           "\"%"TS"\".\n"), image, new_desc);
2772                                 new_desc = NULL;
2773                         } else {
2774                                 tprintf(T("Changing the description of image %d "
2775                                           "to \"%"TS"\".\n"), image, new_desc);
2776                                 ret = wimlib_set_image_descripton(wim, image,
2777                                                                   new_desc);
2778                                 if (ret)
2779                                         goto out_wimlib_free;
2780                         }
2781                 }
2782
2783                 /* Only call wimlib_overwrite() if something actually needs to
2784                  * be changed. */
2785                 if (boot || new_name || new_desc ||
2786                     (check && !info.has_integrity_table))
2787                 {
2788                         int write_flags = 0;
2789
2790                         if (check)
2791                                 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2792                         ret = wimlib_overwrite(wim, write_flags, 1,
2793                                                imagex_progress_func);
2794                 } else {
2795                         tprintf(T("The file \"%"TS"\" was not modified because nothing "
2796                                   "needed to be done.\n"), wimfile);
2797                         ret = 0;
2798                 }
2799         }
2800 out_wimlib_free:
2801         wimlib_free(wim);
2802 out:
2803         return ret;
2804
2805 out_usage:
2806         usage(INFO);
2807         ret = -1;
2808         goto out;
2809 }
2810
2811 /* Join split WIMs into one part WIM */
2812 static int
2813 imagex_join(int argc, tchar **argv)
2814 {
2815         int c;
2816         int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2817         int wim_write_flags = 0;
2818         const tchar *output_path;
2819         int ret;
2820
2821         for_opt(c, join_options) {
2822                 switch (c) {
2823                 case IMAGEX_CHECK_OPTION:
2824                         swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2825                         wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2826                         break;
2827                 default:
2828                         goto out_usage;
2829                 }
2830         }
2831         argc -= optind;
2832         argv += optind;
2833
2834         if (argc < 2) {
2835                 imagex_error(T("Must specify one or more split WIM (.swm) "
2836                                "parts to join"));
2837                 goto out_usage;
2838         }
2839         output_path = argv[0];
2840         ret = wimlib_join((const tchar * const *)++argv,
2841                           --argc,
2842                           output_path,
2843                           swm_open_flags,
2844                           wim_write_flags,
2845                           imagex_progress_func);
2846 out:
2847         return ret;
2848
2849 out_usage:
2850         usage(JOIN);
2851         ret = -1;
2852         goto out;
2853 }
2854
2855 /* Mounts an image using a FUSE mount. */
2856 static int
2857 imagex_mount_rw_or_ro(int argc, tchar **argv)
2858 {
2859         int c;
2860         int mount_flags = 0;
2861         int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2862         const tchar *swm_glob = NULL;
2863         const tchar *staging_dir = NULL;
2864         const tchar *wimfile;
2865         const tchar *dir;
2866         WIMStruct *wim;
2867         int image;
2868         int ret;
2869         WIMStruct **additional_swms;
2870         unsigned num_additional_swms;
2871
2872         if (!tstrcmp(argv[0], T("mountrw"))) {
2873                 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2874                 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2875         }
2876
2877         for_opt(c, mount_options) {
2878                 switch (c) {
2879                 case IMAGEX_ALLOW_OTHER_OPTION:
2880                         mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2881                         break;
2882                 case IMAGEX_CHECK_OPTION:
2883                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2884                         break;
2885                 case IMAGEX_DEBUG_OPTION:
2886                         mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2887                         break;
2888                 case IMAGEX_STREAMS_INTERFACE_OPTION:
2889                         if (!tstrcasecmp(optarg, T("none")))
2890                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2891                         else if (!tstrcasecmp(optarg, T("xattr")))
2892                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2893                         else if (!tstrcasecmp(optarg, T("windows")))
2894                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2895                         else {
2896                                 imagex_error(T("Unknown stream interface \"%"TS"\""),
2897                                              optarg);
2898                                 goto out_usage;
2899                         }
2900                         break;
2901                 case IMAGEX_REF_OPTION:
2902                         swm_glob = optarg;
2903                         break;
2904                 case IMAGEX_STAGING_DIR_OPTION:
2905                         staging_dir = optarg;
2906                         break;
2907                 case IMAGEX_UNIX_DATA_OPTION:
2908                         mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2909                         break;
2910                 default:
2911                         goto out_usage;
2912                 }
2913         }
2914         argc -= optind;
2915         argv += optind;
2916         if (argc != 2 && argc != 3)
2917                 goto out_usage;
2918
2919         wimfile = argv[0];
2920
2921         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2922         if (ret)
2923                 goto out;
2924
2925         if (argc >= 3) {
2926                 /* Image explicitly specified.  */
2927                 image = wimlib_resolve_image(wim, argv[1]);
2928                 dir = argv[2];
2929                 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2930                 if (ret)
2931                         goto out_free_wim;
2932         } else {
2933                 /* No image specified; default to image 1, but only if the WIM
2934                  * contains exactly one image.  */
2935                 struct wimlib_wim_info info;
2936
2937                 wimlib_get_wim_info(wim, &info);
2938                 if (info.image_count != 1) {
2939                         imagex_error(T("\"%"TS"\" contains %d images; Please "
2940                                        "select one."), wimfile, info.image_count);
2941                         wimlib_free(wim);
2942                         goto out_usage;
2943                 }
2944                 image = 1;
2945                 dir = argv[1];
2946         }
2947
2948         if (swm_glob) {
2949                 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2950                                           &additional_swms,
2951                                           &num_additional_swms);
2952                 if (ret)
2953                         goto out_free_wim;
2954         } else {
2955                 additional_swms = NULL;
2956                 num_additional_swms = 0;
2957         }
2958
2959         ret = wimlib_mount_image(wim, image, dir, mount_flags, additional_swms,
2960                                  num_additional_swms, staging_dir);
2961         if (ret) {
2962                 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2963                                "on \"%"TS"\""),
2964                              image, wimfile, dir);
2965         }
2966         if (swm_glob) {
2967                 for (unsigned i = 0; i < num_additional_swms; i++)
2968                         wimlib_free(additional_swms[i]);
2969                 free(additional_swms);
2970         }
2971 out_free_wim:
2972         wimlib_free(wim);
2973 out:
2974         return ret;
2975
2976 out_usage:
2977         usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2978                         ? MOUNTRW : MOUNT);
2979         ret = -1;
2980         goto out;
2981 }
2982
2983 /* Rebuild a WIM file */
2984 static int
2985 imagex_optimize(int argc, tchar **argv)
2986 {
2987         int c;
2988         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2989         int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2990         int ret;
2991         WIMStruct *wim;
2992         const tchar *wimfile;
2993         off_t old_size;
2994         off_t new_size;
2995         unsigned num_threads = 0;
2996
2997         for_opt(c, optimize_options) {
2998                 switch (c) {
2999                 case IMAGEX_CHECK_OPTION:
3000                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3001                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3002                         break;
3003                 case IMAGEX_RECOMPRESS_OPTION:
3004                         write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3005                         break;
3006                 case IMAGEX_THREADS_OPTION:
3007                         num_threads = parse_num_threads(optarg);
3008                         if (num_threads == UINT_MAX)
3009                                 goto out_err;
3010                         break;
3011                 default:
3012                         goto out_usage;
3013                 }
3014         }
3015         argc -= optind;
3016         argv += optind;
3017
3018         if (argc != 1)
3019                 goto out_usage;
3020
3021         wimfile = argv[0];
3022
3023         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3024         if (ret)
3025                 goto out;
3026
3027         old_size = file_get_size(wimfile);
3028         tprintf(T("\"%"TS"\" original size: "), wimfile);
3029         if (old_size == -1)
3030                 tputs(T("Unknown"));
3031         else
3032                 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3033
3034         ret = wimlib_overwrite(wim, write_flags, num_threads,
3035                                imagex_progress_func);
3036         if (ret)
3037                 goto out_wimlib_free;
3038
3039         new_size = file_get_size(wimfile);
3040         tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3041         if (new_size == -1)
3042                 tputs(T("Unknown"));
3043         else
3044                 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3045
3046         tfputs(T("Space saved: "), stdout);
3047         if (new_size != -1 && old_size != -1) {
3048                 tprintf(T("%lld KiB\n"),
3049                        ((long long)old_size - (long long)new_size) >> 10);
3050         } else {
3051                 tputs(T("Unknown"));
3052         }
3053         ret = 0;
3054 out_wimlib_free:
3055         wimlib_free(wim);
3056 out:
3057         return ret;
3058
3059 out_usage:
3060         usage(OPTIMIZE);
3061 out_err:
3062         ret = -1;
3063         goto out;
3064 }
3065
3066 /* Split a WIM into a spanned set */
3067 static int
3068 imagex_split(int argc, tchar **argv)
3069 {
3070         int c;
3071         int open_flags = 0;
3072         int write_flags = 0;
3073         unsigned long part_size;
3074         tchar *tmp;
3075         int ret;
3076         WIMStruct *wim;
3077
3078         for_opt(c, split_options) {
3079                 switch (c) {
3080                 case IMAGEX_CHECK_OPTION:
3081                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3082                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3083                         break;
3084                 default:
3085                         goto out_usage;
3086                 }
3087         }
3088         argc -= optind;
3089         argv += optind;
3090
3091         if (argc != 3)
3092                 goto out_usage;
3093
3094         part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3095         if (tmp == argv[2] || *tmp) {
3096                 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3097                 imagex_error(T("The part size must be an integer or "
3098                                "floating-point number of megabytes."));
3099                 goto out_err;
3100         }
3101         ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3102         if (ret)
3103                 goto out;
3104
3105         ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3106         wimlib_free(wim);
3107 out:
3108         return ret;
3109
3110 out_usage:
3111         usage(SPLIT);
3112 out_err:
3113         ret = -1;
3114         goto out;
3115 }
3116
3117 /* Unmounts a mounted WIM image. */
3118 static int
3119 imagex_unmount(int argc, tchar **argv)
3120 {
3121         int c;
3122         int unmount_flags = 0;
3123         int ret;
3124
3125         for_opt(c, unmount_options) {
3126                 switch (c) {
3127                 case IMAGEX_COMMIT_OPTION:
3128                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3129                         break;
3130                 case IMAGEX_CHECK_OPTION:
3131                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3132                         break;
3133                 case IMAGEX_REBUILD_OPTION:
3134                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3135                         break;
3136                 case IMAGEX_LAZY_OPTION:
3137                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3138                         break;
3139                 default:
3140                         goto out_usage;
3141                 }
3142         }
3143         argc -= optind;
3144         argv += optind;
3145         if (argc != 1)
3146                 goto out_usage;
3147
3148         ret = wimlib_unmount_image(argv[0], unmount_flags,
3149                                    imagex_progress_func);
3150         if (ret)
3151                 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3152 out:
3153         return ret;
3154
3155 out_usage:
3156         usage(UNMOUNT);
3157         ret = -1;
3158         goto out;
3159 }
3160
3161 /*
3162  * Add, delete, or rename files in a WIM image.
3163  */
3164 static int
3165 imagex_update(int argc, tchar **argv)
3166 {
3167         const tchar *wimfile;
3168         int image;
3169         WIMStruct *wim;
3170         int ret;
3171         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3172         int write_flags = 0;
3173         int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3174         int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
3175         int default_delete_flags = 0;
3176         unsigned num_threads = 0;
3177         int c;
3178         tchar *cmd_file_contents;
3179         size_t cmd_file_nchars;
3180         struct wimlib_update_command *cmds;
3181         size_t num_cmds;
3182         tchar *command_str = NULL;
3183
3184         const tchar *config_file = NULL;
3185         tchar *config_str;
3186         struct wimlib_capture_config *config;
3187
3188         for_opt(c, update_options) {
3189                 switch (c) {
3190                 /* Generic or write options */
3191                 case IMAGEX_THREADS_OPTION:
3192                         num_threads = parse_num_threads(optarg);
3193                         if (num_threads == UINT_MAX)
3194                                 goto out_err;
3195                         break;
3196                 case IMAGEX_CHECK_OPTION:
3197                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3198                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3199                         break;
3200                 case IMAGEX_REBUILD_OPTION:
3201                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3202                         break;
3203                 case IMAGEX_COMMAND_OPTION:
3204                         if (command_str) {
3205                                 imagex_error(T("--command may only be specified "
3206                                                "one time.  Please provide\n"
3207                                                "       the update commands "
3208                                                "on standard input instead."));
3209                                 goto out_err;
3210                         }
3211                         command_str = tstrdup(optarg);
3212                         if (!command_str) {
3213                                 imagex_error(T("Out of memory!"));
3214                                 goto out_err;
3215                         }
3216                         break;
3217                 /* Default delete options */
3218                 case IMAGEX_FORCE_OPTION:
3219                         default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3220                         break;
3221                 case IMAGEX_RECURSIVE_OPTION:
3222                         default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3223                         break;
3224
3225                 /* Global add option */
3226                 case IMAGEX_CONFIG_OPTION:
3227                         config_file = optarg;
3228                         break;
3229
3230                 /* Default add options */
3231                 case IMAGEX_VERBOSE_OPTION:
3232                         default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3233                         break;
3234                 case IMAGEX_DEREFERENCE_OPTION:
3235                         default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3236                         break;
3237                 case IMAGEX_UNIX_DATA_OPTION:
3238                         default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3239                         break;
3240                 case IMAGEX_NO_ACLS_OPTION:
3241                         default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3242                         break;
3243                 case IMAGEX_STRICT_ACLS_OPTION:
3244                         default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3245                         break;
3246                 default:
3247                         goto out_usage;
3248                 }
3249         }
3250         argv += optind;
3251         argc -= optind;
3252
3253         if (argc != 1 && argc != 2)
3254                 goto out_usage;
3255         wimfile = argv[0];
3256
3257         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3258         if (ret)
3259                 goto out_free_command_str;
3260
3261         if (argc >= 2) {
3262                 /* Image explicitly specified.  */
3263                 image = wimlib_resolve_image(wim, argv[1]);
3264                 ret = verify_image_exists_and_is_single(image, argv[1],
3265                                                         wimfile);
3266                 if (ret)
3267                         goto out_wimlib_free;
3268         } else {
3269                 /* No image specified; default to image 1, but only if the WIM
3270                  * contains exactly one image.  */
3271                 struct wimlib_wim_info info;
3272
3273                 wimlib_get_wim_info(wim, &info);
3274                 if (info.image_count != 1) {
3275                         imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3276                                      wimfile, info.image_count);
3277                         wimlib_free(wim);
3278                         goto out_usage;
3279                 }
3280                 image = 1;
3281         }
3282
3283         /* Parse capture configuration file if specified */
3284         if (config_file) {
3285                 size_t config_len;
3286
3287                 config_str = file_get_text_contents(config_file, &config_len);
3288                 if (!config_str) {
3289                         ret = -1;
3290                         goto out_wimlib_free;
3291                 }
3292
3293                 config = alloca(sizeof(*config));
3294                 ret = parse_capture_config(&config_str, config_len, config);
3295                 if (ret)
3296                         goto out_free_config;
3297         } else {
3298                 config = &default_capture_config;
3299         }
3300
3301         /* Read update commands from standard input, or the command string if
3302          * specified.  */
3303         if (command_str) {
3304                 cmd_file_contents = NULL;
3305                 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3306                                                  &num_cmds);
3307         } else {
3308                 if (isatty(STDIN_FILENO)) {
3309                         tputs(T("Reading update commands from standard input..."));
3310                         recommend_man_page(T("update"));
3311                 }
3312                 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3313                 if (!cmd_file_contents) {
3314                         ret = -1;
3315                         goto out_free_config;
3316                 }
3317
3318                 /* Parse the update commands */
3319                 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3320                                                  &num_cmds);
3321         }
3322         if (!cmds) {
3323                 ret = -1;
3324                 goto out_free_cmd_file_contents;
3325         }
3326
3327         /* Set default flags and capture config on the update commands */
3328         bool have_add_command = false;
3329         for (size_t i = 0; i < num_cmds; i++) {
3330                 switch (cmds[i].op) {
3331                 case WIMLIB_UPDATE_OP_ADD:
3332                         cmds[i].add.add_flags |= default_add_flags;
3333                         cmds[i].add.config = config;
3334                         have_add_command = true;
3335                         break;
3336                 case WIMLIB_UPDATE_OP_DELETE:
3337                         cmds[i].delete.delete_flags |= default_delete_flags;
3338                         break;
3339                 default:
3340                         break;
3341                 }
3342         }
3343
3344 #ifdef __WIN32__
3345         if (have_add_command)
3346                 win32_acquire_capture_privileges();
3347 #endif
3348
3349         /* Execute the update commands */
3350         ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3351                                   imagex_progress_func);
3352         if (ret)
3353                 goto out_release_privs;
3354
3355         /* Overwrite the updated WIM */
3356         ret = wimlib_overwrite(wim, write_flags, num_threads,
3357                                imagex_progress_func);
3358 out_release_privs:
3359 #ifdef __WIN32__
3360         if (have_add_command)
3361                 win32_release_capture_privileges();
3362 #endif
3363         free(cmds);
3364 out_free_cmd_file_contents:
3365         free(cmd_file_contents);
3366 out_free_config:
3367         if (config != &default_capture_config) {
3368                 free(config->exclusion_pats.pats);
3369                 free(config->exclusion_exception_pats.pats);
3370                 free(config_str);
3371         }
3372 out_wimlib_free:
3373         wimlib_free(wim);
3374 out_free_command_str:
3375         free(command_str);
3376         return ret;
3377
3378 out_usage:
3379         usage(UPDATE);
3380 out_err:
3381         ret = -1;
3382         goto out_free_command_str;
3383 }
3384
3385 struct imagex_command {
3386         const tchar *name;
3387         int (*func)(int , tchar **);
3388         int cmd;
3389 };
3390
3391
3392 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3393                 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3394
3395 static const struct imagex_command imagex_commands[] = {
3396         {T("append"),  imagex_capture_or_append, APPEND},
3397         {T("apply"),   imagex_apply,             APPLY},
3398         {T("capture"), imagex_capture_or_append, CAPTURE},
3399         {T("delete"),  imagex_delete,            DELETE},
3400         {T("dir"),     imagex_dir,               DIR},
3401         {T("export"),  imagex_export,            EXPORT},
3402         {T("extract"), imagex_extract,           EXTRACT},
3403         {T("info"),    imagex_info,              INFO},
3404         {T("join"),    imagex_join,              JOIN},
3405         {T("mount"),   imagex_mount_rw_or_ro,    MOUNT},
3406         {T("mountrw"), imagex_mount_rw_or_ro,    MOUNTRW},
3407         {T("optimize"),imagex_optimize,          OPTIMIZE},
3408         {T("split"),   imagex_split,             SPLIT},
3409         {T("unmount"), imagex_unmount,           UNMOUNT},
3410         {T("update"),  imagex_update,            UPDATE},
3411 };
3412
3413 static void
3414 version(void)
3415 {
3416         static const tchar *s =
3417         T(
3418 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3419 "Copyright (C) 2012, 2013 Eric Biggers\n"
3420 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3421 "This is free software: you are free to change and redistribute it.\n"
3422 "There is NO WARRANTY, to the extent permitted by law.\n"
3423 "\n"
3424 "Report bugs to "PACKAGE_BUGREPORT".\n"
3425         );
3426         tfputs(s, stdout);
3427 }
3428
3429
3430 static void
3431 help_or_version(int argc, tchar **argv)
3432 {
3433         int i;
3434         const tchar *p;
3435         const struct imagex_command *cmd;
3436
3437         for (i = 1; i < argc; i++) {
3438                 p = argv[i];
3439                 if (*p == T('-'))
3440                         p++;
3441                 else
3442                         continue;
3443                 if (*p == T('-'))
3444                         p++;
3445                 if (!tstrcmp(p, T("help"))) {
3446                         for_imagex_command(cmd) {
3447                                 if (!tstrcmp(cmd->name, argv[1])) {
3448                                         usage(cmd->cmd);
3449                                         exit(0);
3450                                 }
3451                         }
3452                         usage_all();
3453                         exit(0);
3454                 }
3455                 if (!tstrcmp(p, T("version"))) {
3456                         version();
3457                         exit(0);
3458                 }
3459         }
3460 }
3461
3462
3463 static void
3464 usage(int cmd_type)
3465 {
3466         const struct imagex_command *cmd;
3467         tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3468         for_imagex_command(cmd) {
3469                 if (cmd->cmd == cmd_type) {
3470                         tputc(T('\n'), stdout);
3471                         recommend_man_page(cmd->name);
3472                 }
3473         }
3474 }
3475
3476 static void
3477 usage_all(void)
3478 {
3479         tfputs(T("Usage:\n"), stdout);
3480         for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3481                 tprintf(T("    %"TS), usage_strings[i]);
3482         static const tchar *extra =
3483         T(
3484 "    "IMAGEX_PROGNAME" --help\n"
3485 "    "IMAGEX_PROGNAME" --version\n"
3486 "\n"
3487 "    The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3488 "\n"
3489         );
3490         tfputs(extra, stdout);
3491         recommend_man_page(T(""));
3492 }
3493
3494 /* Entry point for wimlib's ImageX implementation.  On UNIX the command
3495  * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3496  * something else), while an Windows the command arguments will be UTF-16LE
3497  * encoded 'wchar_t' strings. */
3498 int
3499 #ifdef __WIN32__
3500 wmain(int argc, wchar_t **argv, wchar_t **envp)
3501 #else
3502 main(int argc, char **argv)
3503 #endif
3504 {
3505         const struct imagex_command *cmd;
3506         int ret;
3507         int init_flags = 0;
3508
3509 #ifndef __WIN32__
3510         if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3511                 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3512         } else {
3513                 char *codeset;
3514
3515                 setlocale(LC_ALL, "");
3516                 codeset = nl_langinfo(CODESET);
3517                 if (!strstr(codeset, "UTF-8") &&
3518                     !strstr(codeset, "UTF8") &&
3519                     !strstr(codeset, "utf-8") &&
3520                     !strstr(codeset, "utf8"))
3521                 {
3522                         fputs(
3523 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3524 "         Maybe try: `export LANG=en_US.UTF-8'?\n"
3525 "         Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3526 "         to any value to force wimlib to use UTF-8.\n",
3527                         stderr);
3528
3529                 }
3530         }
3531 #endif /* !__WIN32__ */
3532
3533         if (argc < 2) {
3534                 imagex_error(T("No command specified"));
3535                 usage_all();
3536                 ret = 2;
3537                 goto out;
3538         }
3539
3540         /* Handle --help and --version for all commands.  Note that this will
3541          * not return if either of these arguments are present. */
3542         help_or_version(argc, argv);
3543         argc--;
3544         argv++;
3545
3546         /* The user may like to see more informative error messages. */
3547         wimlib_set_print_errors(true);
3548
3549         /* Do any initializations that the library needs */
3550         ret = wimlib_global_init(init_flags);
3551         if (ret)
3552                 goto out_check_status;
3553
3554         /* Search for the function to handle the ImageX subcommand. */
3555         for_imagex_command(cmd) {
3556                 if (!tstrcmp(cmd->name, *argv)) {
3557                         ret = cmd->func(argc, argv);
3558                         goto out_check_write_error;
3559                 }
3560         }
3561
3562         imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3563         usage_all();
3564         ret = 2;
3565         goto out_cleanup;
3566 out_check_write_error:
3567         /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3568          * standard output is part of the program's actual behavior and not just
3569          * for informational purposes, so we should set a failure exit status if
3570          * there was a write error. */
3571         if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3572                 if (ferror(stdout) || fclose(stdout)) {
3573                         imagex_error_with_errno(T("error writing to standard output"));
3574                         if (ret == 0)
3575                                 ret = -1;
3576                 }
3577         }
3578 out_check_status:
3579         /* Exit status (ret):  -1 indicates an error found by 'wimlib-imagex'
3580          * outside of the wimlib library code.  0 indicates success.  > 0
3581          * indicates a wimlib error code from which an error message can be
3582          * printed. */
3583         if (ret > 0) {
3584                 imagex_error(T("Exiting with error code %d:\n"
3585                                "       %"TS"."), ret,
3586                              wimlib_get_error_string(ret));
3587                 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3588                         imagex_error_with_errno(T("errno"));
3589         }
3590 out_cleanup:
3591         /* Make the library free any resources it's holding (not strictly
3592          * necessary because the process is ending anyway). */
3593         wimlib_global_cleanup();
3594 out:
3595         return ret;
3596 }