4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013 Eric Biggers
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.
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.
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/>.
26 #include "wimlib_tchar.h"
47 # include "imagex-win32.h"
48 # define tbasename win32_wbasename
49 # define tglob win32_wglob
53 # include <langinfo.h>
54 # define tbasename basename
59 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
61 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
80 static void usage(int cmd_type);
81 static void usage_all();
84 static const tchar *usage_strings[] = {
87 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
88 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
89 " [--verbose] [--dereference] [--config=FILE]\n"
90 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
91 " [--source-list] [--no-acls] [--strict-acls]\n"
95 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
96 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
97 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
98 " [--no-acls] [--strict-acls]\n"
102 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
103 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
104 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
105 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
106 " [--source-list] [--no-acls] [--strict-acls]\n"
110 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
114 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
118 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
119 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
120 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
121 " [--threads=NUM_THREADS] [--rebuild]\n"
125 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
126 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
127 " [--xml] [--extract-xml FILE] [--metadata]\n"
131 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
135 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
136 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
137 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
141 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
142 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
143 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
147 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
151 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
155 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild]\n"
160 IMAGEX_ALLOW_OTHER_OPTION,
163 IMAGEX_COMMIT_OPTION,
164 IMAGEX_COMPRESS_OPTION,
165 IMAGEX_CONFIG_OPTION,
167 IMAGEX_DEREFERENCE_OPTION,
168 IMAGEX_EXTRACT_XML_OPTION,
170 IMAGEX_HARDLINK_OPTION,
171 IMAGEX_HEADER_OPTION,
172 IMAGEX_LOOKUP_TABLE_OPTION,
173 IMAGEX_METADATA_OPTION,
174 IMAGEX_NO_ACLS_OPTION,
175 IMAGEX_REBULID_OPTION,
176 IMAGEX_RECOMPRESS_OPTION,
179 IMAGEX_SOURCE_LIST_OPTION,
180 IMAGEX_STAGING_DIR_OPTION,
181 IMAGEX_STREAMS_INTERFACE_OPTION,
182 IMAGEX_STRICT_ACLS_OPTION,
183 IMAGEX_SYMLINK_OPTION,
184 IMAGEX_THREADS_OPTION,
185 IMAGEX_UNIX_DATA_OPTION,
186 IMAGEX_VERBOSE_OPTION,
190 static const struct option apply_options[] = {
191 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
192 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
193 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
194 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
195 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
196 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
197 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
198 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
199 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
202 static const struct option capture_or_append_options[] = {
203 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
204 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
205 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
206 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
207 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
208 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
209 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
210 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
211 {T("rebuild"), no_argument, NULL, IMAGEX_REBULID_OPTION},
212 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
213 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
214 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
215 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
216 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
219 static const struct option delete_options[] = {
220 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
221 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
225 static const struct option export_options[] = {
226 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
227 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
228 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
229 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
230 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
231 {T("rebuild"), no_argument, NULL, IMAGEX_REBULID_OPTION},
235 static const struct option info_options[] = {
236 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
237 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
238 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
239 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
240 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
241 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
242 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
246 static const struct option join_options[] = {
247 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
251 static const struct option mount_options[] = {
252 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
253 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
254 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
255 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
256 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
257 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
258 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
262 static const struct option optimize_options[] = {
263 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
264 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
268 static const struct option split_options[] = {
269 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
273 static const struct option unmount_options[] = {
274 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
275 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
276 {T("rebuild"), no_argument, NULL, IMAGEX_REBULID_OPTION},
282 /* Print formatted error message to stderr. */
284 imagex_error(const tchar *format, ...)
287 va_start(va, format);
288 tfputs(T("ERROR: "), stderr);
289 tvfprintf(stderr, format, va);
290 tputc(T('\n'), stderr);
294 /* Print formatted error message to stderr. */
296 imagex_error_with_errno(const tchar *format, ...)
298 int errno_save = errno;
300 va_start(va, format);
301 tfputs(T("ERROR: "), stderr);
302 tvfprintf(stderr, format, va);
303 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
308 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
310 if (image == WIMLIB_NO_IMAGE) {
311 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
312 " Please specify a 1-based image index or "
314 " You may use `"IMAGEX_PROGNAME" info' to list the images "
315 "contained in a WIM."),
316 image_name, wim_name);
323 verify_image_is_single(int image)
325 if (image == WIMLIB_ALL_IMAGES) {
326 imagex_error(T("Cannot specify all images for this action!"));
333 verify_image_exists_and_is_single(int image, const tchar *image_name,
334 const tchar *wim_name)
337 ret = verify_image_exists(image, image_name, wim_name);
339 ret = verify_image_is_single(image);
343 /* Parse the argument to --compress */
345 get_compression_type(const tchar *optarg)
347 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
348 return WIMLIB_COMPRESSION_TYPE_LZX;
349 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
350 return WIMLIB_COMPRESSION_TYPE_XPRESS;
351 else if (!tstrcasecmp(optarg, T("none")))
352 return WIMLIB_COMPRESSION_TYPE_NONE;
354 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
355 "\"maximum\", \"fast\", or \"none\"."), optarg);
356 return WIMLIB_COMPRESSION_TYPE_INVALID;
360 /* Returns the size of a file given its name, or -1 if the file does not exist
361 * or its size cannot be determined. */
363 file_get_size(const tchar *filename)
366 if (tstat(filename, &st) == 0)
372 static const tchar *default_capture_config =
378 "\\System Volume Information\n"
384 PARSE_FILENAME_SUCCESS = 0,
385 PARSE_FILENAME_FAILURE = 1,
386 PARSE_FILENAME_NONE = 2,
390 * Parses a filename in the source list file format. (See the man page for
391 * 'wimlib-imagex capture' for details on this format and the meaning.)
392 * Accepted formats for filenames are an unquoted string (whitespace-delimited),
393 * or a double or single-quoted string.
395 * @line_p: Pointer to the pointer to the line of data. Will be updated
396 * to point past the filename iff the return value is
397 * PARSE_FILENAME_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
400 * @len_p: @len_p initially stores the length of the line of data, which may
401 * be 0, and it will be updated to the number of bytes remaining in
402 * the line iff the return value is PARSE_FILENAME_SUCCESS.
404 * @fn_ret: Iff the return value is PARSE_FILENAME_SUCCESS, a pointer to the
405 * parsed filename will be returned here.
407 * Returns: PARSE_FILENAME_SUCCESS if a filename was successfully parsed; or
408 * PARSE_FILENAME_FAILURE if the data was invalid due to a missing
409 * closing quote; or PARSE_FILENAME_NONE if the line ended before the
410 * beginning of a filename was found.
413 parse_filename(tchar **line_p, size_t *len_p, tchar **fn_ret)
416 tchar *line = *line_p;
420 /* Skip leading whitespace */
423 return PARSE_FILENAME_NONE;
424 if (!istspace(*line) && *line != T('\0'))
430 if (quote_char == T('"') || quote_char == T('\'')) {
431 /* Quoted filename */
435 line = tmemchr(line, quote_char, len);
437 imagex_error(T("Missing closing quote: %"TS), fn - 1);
438 return PARSE_FILENAME_FAILURE;
441 /* Unquoted filename. Go until whitespace. Line is terminated
442 * by '\0', so no need to check 'len'. */
446 } while (!istspace(*line) && *line != T('\0'));
453 return PARSE_FILENAME_SUCCESS;
456 /* Parses a line of data (not an empty line or comment) in the source list file
457 * format. (See the man page for 'wimlib-imagex capture' for details on this
458 * format and the meaning.)
460 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
461 * len == 0. The data in @line will be modified by this function call.
463 * @len: Length of the line of data.
465 * @source: On success, the capture source and target described by the line is
466 * written into this destination. Note that it will contain pointers
467 * to data in the @line array.
469 * Returns true if the line was valid; false otherwise. */
471 parse_source_list_line(tchar *line, size_t len,
472 struct wimlib_capture_source *source)
476 ret = parse_filename(&line, &len, &source->fs_source_path);
477 if (ret != PARSE_FILENAME_SUCCESS)
479 ret = parse_filename(&line, &len, &source->wim_target_path);
480 if (ret == PARSE_FILENAME_NONE)
481 source->wim_target_path = source->fs_source_path;
482 return ret != PARSE_FILENAME_FAILURE;
485 /* Returns %true if the given line of length @len > 0 is a comment or empty line
486 * in the source list file format. */
488 is_comment_line(const tchar *line, size_t len)
493 if (!istspace(*line) && *line != T('\0'))
502 /* Parses a file in the source list format. (See the man page for
503 * 'wimlib-imagex capture' for details on this format and the meaning.)
505 * @source_list_contents: Contents of the source list file. Note that this
506 * buffer will be modified to save memory allocations,
507 * and cannot be freed until the returned array of
508 * wimlib_capture_source's has also been freed.
510 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
513 * @nsources_ret: On success, the length of the returned array is
516 * Returns: An array of `struct wimlib_capture_source's that can be passed to
517 * the wimlib_add_image_multisource() function to specify how a WIM image is to
519 static struct wimlib_capture_source *
520 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
521 size_t *nsources_ret)
525 struct wimlib_capture_source *sources;
527 tchar *source_list_contents = *source_list_contents_p;
530 for (i = 0; i < source_list_nchars; i++)
531 if (source_list_contents[i] == T('\n'))
534 /* Handle last line not terminated by a newline */
535 if (source_list_nchars != 0 &&
536 source_list_contents[source_list_nchars - 1] != T('\n'))
538 source_list_contents = realloc(source_list_contents,
539 (source_list_nchars + 1) * sizeof(tchar));
540 if (!source_list_contents)
542 source_list_contents[source_list_nchars] = T('\n');
543 *source_list_contents_p = source_list_contents;
544 source_list_nchars++;
548 /* Always allocate at least 1 slot, just in case the implementation of
549 * calloc() returns NULL if 0 bytes are requested. */
550 sources = calloc(nlines ?: 1, sizeof(*sources));
553 p = source_list_contents;
555 for (i = 0; i < nlines; i++) {
556 /* XXX: Could use rawmemchr() here instead, but it may not be
557 * available on all platforms. */
558 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
559 size_t len = endp - p + 1;
561 if (!is_comment_line(p, len)) {
562 if (!parse_source_list_line(p, len, &sources[j++])) {
573 imagex_error(T("out of memory"));
577 /* Reads the contents of a file into memory. */
579 file_get_contents(const tchar *filename, size_t *len_ret)
586 if (tstat(filename, &stbuf) != 0) {
587 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
592 fp = tfopen(filename, T("rb"));
594 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
600 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
601 "contents of file \"%"TS"\""), len, filename);
604 if (fread(buf, 1, len, fp) != len) {
605 imagex_error_with_errno(T("Failed to read %zu bytes from the "
606 "file \"%"TS"\""), len, filename);
620 /* Read standard input until EOF and return the full contents in a malloc()ed
621 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
624 stdin_get_contents(size_t *len_ret)
626 /* stdin can, of course, be a pipe or other non-seekable file, so the
627 * total length of the data cannot be pre-determined */
629 size_t newlen = 1024;
633 char *p = realloc(buf, newlen);
634 size_t bytes_read, bytes_to_read;
636 imagex_error(T("out of memory while reading stdin"));
640 bytes_to_read = newlen - pos;
641 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
643 if (bytes_read != bytes_to_read) {
648 imagex_error_with_errno(T("error reading stdin"));
662 translate_text_to_tstr(char *text, size_t num_bytes,
663 size_t *num_tchars_ret)
666 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
668 *num_tchars_ret = num_bytes;
670 #else /* !__WIN32__ */
671 /* On Windows, translate the text to UTF-16LE */
675 if (num_bytes >= 2 &&
676 ((text[0] == 0xff && text[1] == 0xfe) ||
677 (text[0] <= 0x7f && text[1] == 0x00)))
679 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
680 * with something that looks like an ASCII character encoded as
681 * a UTF-16LE code unit. Assume the file is encoded as
682 * UTF-16LE. This is not a 100% reliable check. */
683 num_wchars = num_bytes / 2;
684 text_wstr = (wchar_t*)text;
686 /* File does not look like UTF-16LE. Assume it is encoded in
687 * the current Windows code page. I think these are always
688 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
689 * should work as expected. */
690 text_wstr = win32_mbs_to_wcs(text,
695 *num_tchars_ret = num_wchars;
697 #endif /* __WIN32__ */
701 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
706 contents = file_get_contents(filename, &num_bytes);
709 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
713 stdin_get_text_contents(size_t *num_tchars_ret)
718 contents = stdin_get_contents(&num_bytes);
721 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
724 /* Return 0 if a path names a file to which the current user has write access;
725 * -1 otherwise (and print an error message). */
727 file_writable(const tchar *path)
730 ret = taccess(path, W_OK);
732 imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
736 #define TO_PERCENT(numerator, denominator) \
737 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
739 /* Given an enumerated value for WIM compression type, return a descriptive
742 get_data_type(int ctype)
745 case WIMLIB_COMPRESSION_TYPE_NONE:
746 return T("uncompressed");
747 case WIMLIB_COMPRESSION_TYPE_LZX:
748 return T("LZX-compressed");
749 case WIMLIB_COMPRESSION_TYPE_XPRESS:
750 return T("XPRESS-compressed");
755 /* Progress callback function passed to various wimlib functions. */
757 imagex_progress_func(enum wimlib_progress_msg msg,
758 const union wimlib_progress_info *info)
760 unsigned percent_done;
762 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
763 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
764 info->write_streams.total_bytes);
765 if (info->write_streams.completed_streams == 0) {
766 const tchar *data_type;
768 data_type = get_data_type(info->write_streams.compression_type);
769 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
770 data_type, info->write_streams.num_threads,
771 (info->write_streams.num_threads == 1) ? T("") : T("s"));
773 tprintf(T("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
774 "written (%u%% done)"),
775 info->write_streams.completed_bytes >> 20,
776 info->write_streams.total_bytes >> 20,
778 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
781 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
782 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
783 if (*info->scan.wim_target_path) {
784 tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
785 info->scan.wim_target_path);
787 tprintf(T(" (loading as root of WIM image)...\n"));
790 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
791 if (info->scan.excluded)
792 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
794 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
796 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
798 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
799 percent_done = TO_PERCENT(info->integrity.completed_bytes,
800 info->integrity.total_bytes);
801 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" MiB "
802 "of %"PRIu64" MiB (%u%%) done"),
803 info->integrity.filename,
804 info->integrity.completed_bytes >> 20,
805 info->integrity.total_bytes >> 20,
807 if (info->integrity.completed_bytes == info->integrity.total_bytes)
810 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
811 percent_done = TO_PERCENT(info->integrity.completed_bytes,
812 info->integrity.total_bytes);
813 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" MiB "
814 "of %"PRIu64" MiB (%u%%) done"),
815 info->integrity.completed_bytes >> 20,
816 info->integrity.total_bytes >> 20,
818 if (info->integrity.completed_bytes == info->integrity.total_bytes)
821 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
822 tprintf(T("Applying image %d (%"TS") from \"%"TS"\" "
823 "to %"TS" \"%"TS"\"\n"),
825 info->extract.image_name,
826 info->extract.wimfile_name,
827 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
828 T("NTFS volume") : T("directory")),
829 info->extract.target);
831 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
832 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
833 /*info->extract.target);*/
835 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
836 percent_done = TO_PERCENT(info->extract.completed_bytes,
837 info->extract.total_bytes);
838 tprintf(T("\rExtracting files: "
839 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done"),
840 info->extract.completed_bytes >> 20,
841 info->extract.total_bytes >> 20,
843 if (info->extract.completed_bytes >= info->extract.total_bytes)
846 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
847 tprintf(T("%"TS"\n"), info->extract.cur_path);
849 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
850 tprintf(T("Setting timestamps on all extracted files...\n"));
852 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
853 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
854 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
855 info->extract.target);
858 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
859 percent_done = TO_PERCENT(info->join.completed_bytes,
860 info->join.total_bytes);
861 tprintf(T("Writing resources from part %u of %u: "
862 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n"),
863 (info->join.completed_parts == info->join.total_parts) ?
864 info->join.completed_parts : info->join.completed_parts + 1,
865 info->join.total_parts,
866 info->join.completed_bytes >> 20,
867 info->join.total_bytes >> 20,
870 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
871 percent_done = TO_PERCENT(info->split.completed_bytes,
872 info->split.total_bytes);
873 tprintf(T("Writing \"%"TS"\": %"PRIu64" MiB of "
874 "%"PRIu64" MiB (%u%%) written\n"),
875 info->split.part_name,
876 info->split.completed_bytes >> 20,
877 info->split.total_bytes >> 20,
880 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
881 if (info->split.completed_bytes == info->split.total_bytes) {
882 tprintf(T("Finished writing %u split WIM parts\n"),
883 info->split.cur_part_number);
893 /* Open all the split WIM parts that correspond to a file glob.
895 * @first_part specifies the first part of the split WIM and it may be either
896 * included or omitted from the glob. */
898 open_swms_from_glob(const tchar *swm_glob,
899 const tchar *first_part,
901 WIMStruct ***additional_swms_ret,
902 unsigned *num_additional_swms_ret)
904 unsigned num_additional_swms = 0;
905 WIMStruct **additional_swms = NULL;
909 /* Warning: glob() is replaced in Windows native builds */
910 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
912 if (ret == GLOB_NOMATCH) {
913 imagex_error(T("Found no files for glob \"%"TS"\""),
916 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
922 num_additional_swms = globbuf.gl_pathc;
923 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
924 if (!additional_swms) {
925 imagex_error(T("Out of memory"));
930 for (unsigned i = 0; i < num_additional_swms; i++) {
931 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
935 ret = wimlib_open_wim(globbuf.gl_pathv[i],
936 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
937 &additional_swms[i - offset],
938 imagex_progress_func);
942 *additional_swms_ret = additional_swms;
943 *num_additional_swms_ret = num_additional_swms - offset;
947 for (unsigned i = 0; i < num_additional_swms; i++)
948 wimlib_free(additional_swms[i]);
949 free(additional_swms);
958 parse_num_threads(const tchar *optarg)
961 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
962 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
963 imagex_error(T("Number of threads must be a non-negative integer!"));
971 /* Apply one image, or all images, from a WIM file into a directory, OR apply
972 * one image from a WIM file to a NTFS volume. */
974 imagex_apply(int argc, tchar **argv)
977 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
982 const tchar *wimfile;
984 const tchar *image_num_or_name;
985 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
987 const tchar *swm_glob = NULL;
988 WIMStruct **additional_swms = NULL;
989 unsigned num_additional_swms = 0;
991 for_opt(c, apply_options) {
993 case IMAGEX_CHECK_OPTION:
994 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
996 case IMAGEX_HARDLINK_OPTION:
997 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
999 case IMAGEX_SYMLINK_OPTION:
1000 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1002 case IMAGEX_VERBOSE_OPTION:
1003 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1005 case IMAGEX_REF_OPTION:
1008 case IMAGEX_UNIX_DATA_OPTION:
1009 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1011 case IMAGEX_NO_ACLS_OPTION:
1012 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1014 case IMAGEX_STRICT_ACLS_OPTION:
1015 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1024 if (argc != 2 && argc != 3) {
1031 image_num_or_name = T("1");
1034 image_num_or_name = argv[1];
1038 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1042 image = wimlib_resolve_image(w, image_num_or_name);
1043 ret = verify_image_exists(image, image_num_or_name, wimfile);
1047 num_images = wimlib_get_num_images(w);
1048 if (argc == 2 && num_images != 1) {
1049 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1050 "(or all)"), wimfile, num_images);
1057 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1059 &num_additional_swms);
1066 ret = tstat(target, &stbuf);
1068 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1069 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1071 if (errno != ENOENT) {
1072 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1080 win32_acquire_restore_privileges();
1082 ret = wimlib_extract_image(w, image, target, extract_flags,
1083 additional_swms, num_additional_swms,
1084 imagex_progress_func);
1086 tprintf(T("Done applying WIM image.\n"));
1088 win32_release_restore_privileges();
1092 if (additional_swms) {
1093 for (unsigned i = 0; i < num_additional_swms; i++)
1094 wimlib_free(additional_swms[i]);
1095 free(additional_swms);
1100 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1101 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1102 * the desired image. 'wimlib-imagex append': add a new image to an existing
1105 imagex_capture_or_append(int argc, tchar **argv)
1109 int add_image_flags = 0;
1110 int write_flags = 0;
1111 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1112 const tchar *wimfile;
1115 const tchar *flags_element = NULL;
1116 WIMStruct *w = NULL;
1119 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1120 unsigned num_threads = 0;
1123 size_t source_name_len;
1126 const tchar *config_file = NULL;
1127 tchar *config_str = NULL;
1130 bool source_list = false;
1131 size_t source_list_nchars;
1132 tchar *source_list_contents = NULL;
1133 bool capture_sources_malloced = false;
1134 struct wimlib_capture_source *capture_sources;
1137 for_opt(c, capture_or_append_options) {
1139 case IMAGEX_BOOT_OPTION:
1140 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1142 case IMAGEX_CHECK_OPTION:
1143 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1144 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1146 case IMAGEX_CONFIG_OPTION:
1147 config_file = optarg;
1149 case IMAGEX_COMPRESS_OPTION:
1150 compression_type = get_compression_type(optarg);
1151 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1154 case IMAGEX_FLAGS_OPTION:
1155 flags_element = optarg;
1157 case IMAGEX_DEREFERENCE_OPTION:
1158 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1160 case IMAGEX_VERBOSE_OPTION:
1161 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1163 case IMAGEX_THREADS_OPTION:
1164 num_threads = parse_num_threads(optarg);
1165 if (num_threads == UINT_MAX)
1168 case IMAGEX_REBULID_OPTION:
1169 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1171 case IMAGEX_UNIX_DATA_OPTION:
1172 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1174 case IMAGEX_SOURCE_LIST_OPTION:
1177 case IMAGEX_NO_ACLS_OPTION:
1178 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1180 case IMAGEX_STRICT_ACLS_OPTION:
1181 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1191 if (argc < 2 || argc > 4) {
1202 /* Set default name to SOURCE argument, omitting any directory
1203 * prefixes and trailing slashes. This requires making a copy
1205 source_name_len = tstrlen(source);
1206 source_copy = alloca((source_name_len + 1) * sizeof(tchar));
1207 name = tbasename(tstrcpy(source_copy, source));
1209 /* Image description defaults to NULL if not given. */
1210 desc = (argc >= 4) ? argv[3] : NULL;
1213 /* Set up capture sources in source list mode */
1214 if (source[0] == T('-') && source[1] == T('\0')) {
1215 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1217 source_list_contents = file_get_text_contents(source,
1218 &source_list_nchars);
1220 if (!source_list_contents)
1223 capture_sources = parse_source_list(&source_list_contents,
1226 if (!capture_sources) {
1230 capture_sources_malloced = true;
1232 /* Set up capture source in non-source-list mode (could be
1233 * either "normal" mode or "NTFS mode"--- see the man page). */
1234 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1235 capture_sources[0].fs_source_path = source;
1236 capture_sources[0].wim_target_path = NULL;
1237 capture_sources[0].reserved = 0;
1242 config_str = file_get_text_contents(config_file, &config_len);
1250 ret = wimlib_open_wim(wimfile, open_flags, &w,
1251 imagex_progress_func);
1253 ret = wimlib_create_new_wim(compression_type, &w);
1259 ret = tstat(source, &stbuf);
1261 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1262 tprintf(T("Capturing WIM image from NTFS "
1263 "filesystem on \"%"TS"\"\n"), source);
1264 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1267 if (errno != ENOENT) {
1268 imagex_error_with_errno(T("Failed to stat "
1269 "\"%"TS"\""), source);
1276 win32_acquire_capture_privileges();
1279 ret = wimlib_add_image_multisource(w, capture_sources,
1281 (config_str ? config_str :
1282 default_capture_config),
1283 (config_str ? config_len :
1284 tstrlen(default_capture_config)),
1286 imagex_progress_func);
1288 goto out_release_privs;
1289 cur_image = wimlib_get_num_images(w);
1291 ret = wimlib_set_image_descripton(w, cur_image, desc);
1293 goto out_release_privs;
1295 if (flags_element) {
1296 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1298 goto out_release_privs;
1300 if (cmd == APPEND) {
1301 ret = wimlib_overwrite(w, write_flags, num_threads,
1302 imagex_progress_func);
1304 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1305 num_threads, imagex_progress_func);
1307 if (ret == WIMLIB_ERR_REOPEN)
1310 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1314 win32_release_capture_privileges();
1319 free(source_list_contents);
1320 if (capture_sources_malloced)
1321 free(capture_sources);
1325 /* Remove image(s) from a WIM. */
1327 imagex_delete(int argc, tchar **argv)
1331 int write_flags = 0;
1332 const tchar *wimfile;
1333 const tchar *image_num_or_name;
1338 for_opt(c, delete_options) {
1340 case IMAGEX_CHECK_OPTION:
1341 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1342 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1344 case IMAGEX_SOFT_OPTION:
1345 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1357 imagex_error(T("Must specify a WIM file"));
1359 imagex_error(T("Must specify an image"));
1364 image_num_or_name = argv[1];
1366 ret = file_writable(wimfile);
1370 ret = wimlib_open_wim(wimfile, open_flags, &w,
1371 imagex_progress_func);
1375 image = wimlib_resolve_image(w, image_num_or_name);
1377 ret = verify_image_exists(image, image_num_or_name, wimfile);
1381 ret = wimlib_delete_image(w, image);
1383 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1387 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1388 if (ret == WIMLIB_ERR_REOPEN)
1391 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1392 "deleted"), wimfile);
1399 /* Print the files contained in an image(s) in a WIM file. */
1401 imagex_dir(int argc, tchar **argv)
1403 const tchar *wimfile;
1410 imagex_error(T("Must specify a WIM file"));
1415 imagex_error(T("Too many arguments"));
1421 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1422 imagex_progress_func);
1427 image = wimlib_resolve_image(w, argv[2]);
1428 ret = verify_image_exists(image, argv[2], wimfile);
1432 /* Image was not specified. If the WIM only contains one image,
1433 * choose that one; otherwise, print an error. */
1434 num_images = wimlib_get_num_images(w);
1435 if (num_images != 1) {
1436 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
1437 "select one."), wimfile, num_images);
1445 ret = wimlib_print_files(w, image);
1451 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1454 imagex_export(int argc, tchar **argv)
1458 int export_flags = 0;
1459 int write_flags = 0;
1460 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1461 bool compression_type_specified = false;
1462 const tchar *src_wimfile;
1463 const tchar *src_image_num_or_name;
1464 const tchar *dest_wimfile;
1465 const tchar *dest_name;
1466 const tchar *dest_desc;
1467 WIMStruct *src_w = NULL;
1468 WIMStruct *dest_w = NULL;
1473 const tchar *swm_glob = NULL;
1474 WIMStruct **additional_swms = NULL;
1475 unsigned num_additional_swms = 0;
1476 unsigned num_threads = 0;
1478 for_opt(c, export_options) {
1480 case IMAGEX_BOOT_OPTION:
1481 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1483 case IMAGEX_CHECK_OPTION:
1484 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1485 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1487 case IMAGEX_COMPRESS_OPTION:
1488 compression_type = get_compression_type(optarg);
1489 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1491 compression_type_specified = true;
1493 case IMAGEX_REF_OPTION:
1496 case IMAGEX_THREADS_OPTION:
1497 num_threads = parse_num_threads(optarg);
1498 if (num_threads == UINT_MAX)
1501 case IMAGEX_REBULID_OPTION:
1502 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1511 if (argc < 3 || argc > 5) {
1515 src_wimfile = argv[0];
1516 src_image_num_or_name = argv[1];
1517 dest_wimfile = argv[2];
1518 dest_name = (argc >= 4) ? argv[3] : NULL;
1519 dest_desc = (argc >= 5) ? argv[4] : NULL;
1520 ret = wimlib_open_wim(src_wimfile,
1521 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1522 imagex_progress_func);
1526 /* Determine if the destination is an existing file or not.
1527 * If so, we try to append the exported image(s) to it; otherwise, we
1528 * create a new WIM containing the exported image(s). */
1529 if (tstat(dest_wimfile, &stbuf) == 0) {
1533 /* Destination file exists. */
1535 if (!S_ISREG(stbuf.st_mode)) {
1536 imagex_error(T("\"%"TS"\" is not a regular file"),
1541 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1542 imagex_progress_func);
1546 ret = file_writable(dest_wimfile);
1550 dest_ctype = wimlib_get_compression_type(dest_w);
1551 if (compression_type_specified
1552 && compression_type != dest_ctype)
1554 imagex_error(T("Cannot specify a compression type that is "
1555 "not the same as that used in the "
1556 "destination WIM"));
1562 /* dest_wimfile is not an existing file, so create a new WIM. */
1563 if (!compression_type_specified)
1564 compression_type = wimlib_get_compression_type(src_w);
1565 if (errno == ENOENT) {
1566 ret = wimlib_create_new_wim(compression_type, &dest_w);
1570 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
1577 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1578 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1583 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1585 &num_additional_swms);
1590 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1591 export_flags, additional_swms,
1592 num_additional_swms, imagex_progress_func);
1598 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1599 write_flags, num_threads,
1600 imagex_progress_func);
1602 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1603 imagex_progress_func);
1605 if (ret == WIMLIB_ERR_REOPEN)
1608 wimlib_free(dest_w);
1609 if (additional_swms) {
1610 for (unsigned i = 0; i < num_additional_swms; i++)
1611 wimlib_free(additional_swms[i]);
1612 free(additional_swms);
1617 /* Prints information about a WIM file; also can mark an image as bootable,
1618 * change the name of an image, or change the description of an image. */
1620 imagex_info(int argc, tchar **argv)
1625 bool header = false;
1626 bool lookup_table = false;
1628 bool metadata = false;
1629 bool short_header = true;
1630 const tchar *xml_out_file = NULL;
1631 const tchar *wimfile;
1632 const tchar *image_num_or_name = T("all");
1633 const tchar *new_name = NULL;
1634 const tchar *new_desc = NULL;
1639 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1644 for_opt(c, info_options) {
1646 case IMAGEX_BOOT_OPTION:
1649 case IMAGEX_CHECK_OPTION:
1652 case IMAGEX_HEADER_OPTION:
1654 short_header = false;
1656 case IMAGEX_LOOKUP_TABLE_OPTION:
1657 lookup_table = true;
1658 short_header = false;
1660 case IMAGEX_XML_OPTION:
1662 short_header = false;
1664 case IMAGEX_EXTRACT_XML_OPTION:
1665 xml_out_file = optarg;
1666 short_header = false;
1668 case IMAGEX_METADATA_OPTION:
1670 short_header = false;
1680 if (argc == 0 || argc > 4) {
1686 image_num_or_name = argv[1];
1696 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1698 ret = wimlib_open_wim(wimfile, open_flags, &w,
1699 imagex_progress_func);
1703 part_number = wimlib_get_part_number(w, &total_parts);
1705 image = wimlib_resolve_image(w, image_num_or_name);
1706 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
1707 imagex_error(T("The image \"%"TS"\" does not exist"),
1710 imagex_error(T("If you would like to set the boot "
1711 "index to 0, specify image \"0\" with "
1712 "the --boot flag."));
1714 ret = WIMLIB_ERR_INVALID_IMAGE;
1718 num_images = wimlib_get_num_images(w);
1720 if (num_images == 0) {
1722 imagex_error(T("--boot is meaningless on a WIM with no "
1724 ret = WIMLIB_ERR_INVALID_IMAGE;
1729 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1731 imagex_error(T("Cannot specify the --boot flag "
1732 "without specifying a specific "
1733 "image in a multi-image WIM"));
1734 ret = WIMLIB_ERR_INVALID_IMAGE;
1738 imagex_error(T("Cannot specify the NEW_NAME "
1739 "without specifying a specific "
1740 "image in a multi-image WIM"));
1741 ret = WIMLIB_ERR_INVALID_IMAGE;
1746 /* Operations that print information are separated from operations that
1747 * recreate the WIM file. */
1748 if (!new_name && !boot) {
1750 /* Read-only operations */
1752 if (image == WIMLIB_NO_IMAGE) {
1753 imagex_error(T("\"%"TS"\" is not a valid image"),
1755 ret = WIMLIB_ERR_INVALID_IMAGE;
1759 if (image == WIMLIB_ALL_IMAGES && short_header)
1760 wimlib_print_wim_information(w);
1763 wimlib_print_header(w);
1766 if (total_parts != 1) {
1767 tprintf(T("Warning: Only showing the lookup table "
1768 "for part %d of a %d-part WIM.\n"),
1769 part_number, total_parts);
1771 wimlib_print_lookup_table(w);
1775 ret = wimlib_extract_xml_data(w, stdout);
1781 fp = tfopen(xml_out_file, T("wb"));
1783 imagex_error_with_errno(T("Failed to open the "
1784 "file \"%"TS"\" for "
1790 ret = wimlib_extract_xml_data(w, fp);
1791 if (fclose(fp) != 0) {
1792 imagex_error(T("Failed to close the file "
1803 wimlib_print_available_images(w, image);
1806 ret = wimlib_print_metadata(w, image);
1812 /* Modification operations */
1813 if (total_parts != 1) {
1814 imagex_error(T("Modifying a split WIM is not supported."));
1818 if (image == WIMLIB_ALL_IMAGES)
1821 if (image == WIMLIB_NO_IMAGE && new_name) {
1822 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
1823 "when using image 0"), new_name);
1829 if (image == wimlib_get_boot_idx(w)) {
1830 tprintf(T("Image %d is already marked as "
1831 "bootable.\n"), image);
1834 tprintf(T("Marking image %d as bootable.\n"),
1836 wimlib_set_boot_idx(w, image);
1840 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
1842 tprintf(T("Image %d is already named \"%"TS"\".\n"),
1846 tprintf(T("Changing the name of image %d to "
1847 "\"%"TS"\".\n"), image, new_name);
1848 ret = wimlib_set_image_name(w, image, new_name);
1854 const tchar *old_desc;
1855 old_desc = wimlib_get_image_description(w, image);
1856 if (old_desc && !tstrcmp(old_desc, new_desc)) {
1857 tprintf(T("The description of image %d is already "
1858 "\"%"TS"\".\n"), image, new_desc);
1861 tprintf(T("Changing the description of image %d "
1862 "to \"%"TS"\".\n"), image, new_desc);
1863 ret = wimlib_set_image_descripton(w, image,
1870 /* Only call wimlib_overwrite() if something actually needs to
1872 if (boot || new_name || new_desc ||
1873 (check && !wimlib_has_integrity_table(w)))
1877 ret = file_writable(wimfile);
1882 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1886 ret = wimlib_overwrite(w, write_flags, 1,
1887 imagex_progress_func);
1888 if (ret == WIMLIB_ERR_REOPEN)
1891 tprintf(T("The file \"%"TS"\" was not modified because nothing "
1892 "needed to be done.\n"), wimfile);
1901 /* Join split WIMs into one part WIM */
1903 imagex_join(int argc, tchar **argv)
1906 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1907 int wim_write_flags = 0;
1908 const tchar *output_path;
1910 for_opt(c, join_options) {
1912 case IMAGEX_CHECK_OPTION:
1913 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1914 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1924 imagex_error(T("Must specify one or more split WIM (.swm) "
1928 output_path = argv[0];
1929 return wimlib_join((const tchar * const *)++argv,
1934 imagex_progress_func);
1940 /* Mounts an image using a FUSE mount. */
1942 imagex_mount_rw_or_ro(int argc, tchar **argv)
1945 int mount_flags = 0;
1946 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1947 const tchar *wimfile;
1953 const tchar *swm_glob = NULL;
1954 WIMStruct **additional_swms = NULL;
1955 unsigned num_additional_swms = 0;
1956 const tchar *staging_dir = NULL;
1958 if (!tstrcmp(argv[0], T("mountrw")))
1959 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1961 for_opt(c, mount_options) {
1963 case IMAGEX_ALLOW_OTHER_OPTION:
1964 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1966 case IMAGEX_CHECK_OPTION:
1967 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1969 case IMAGEX_DEBUG_OPTION:
1970 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1972 case IMAGEX_STREAMS_INTERFACE_OPTION:
1973 if (!tstrcasecmp(optarg, T("none")))
1974 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1975 else if (!tstrcasecmp(optarg, T("xattr")))
1976 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1977 else if (!tstrcasecmp(optarg, T("windows")))
1978 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1980 imagex_error(T("Unknown stream interface \"%"TS"\""),
1985 case IMAGEX_REF_OPTION:
1988 case IMAGEX_STAGING_DIR_OPTION:
1989 staging_dir = optarg;
1991 case IMAGEX_UNIX_DATA_OPTION:
1992 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2000 if (argc != 2 && argc != 3)
2005 ret = wimlib_open_wim(wimfile, open_flags, &w,
2006 imagex_progress_func);
2011 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2013 &num_additional_swms);
2020 num_images = wimlib_get_num_images(w);
2021 if (num_images != 1) {
2022 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
2023 "select one."), wimfile, num_images);
2024 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2031 image = wimlib_resolve_image(w, argv[1]);
2033 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2038 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2039 ret = file_writable(wimfile);
2044 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2045 num_additional_swms, staging_dir);
2047 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2049 image, wimfile, dir);
2054 if (additional_swms) {
2055 for (unsigned i = 0; i < num_additional_swms; i++)
2056 wimlib_free(additional_swms[i]);
2057 free(additional_swms);
2061 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2066 /* Rebuild a WIM file */
2068 imagex_optimize(int argc, tchar **argv)
2072 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2075 const tchar *wimfile;
2079 for_opt(c, optimize_options) {
2081 case IMAGEX_CHECK_OPTION:
2082 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2083 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2085 case IMAGEX_RECOMPRESS_OPTION:
2086 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2103 ret = wimlib_open_wim(wimfile, open_flags, &w,
2104 imagex_progress_func);
2108 old_size = file_get_size(argv[0]);
2109 tprintf(T("\"%"TS"\" original size: "), wimfile);
2111 tfputs(T("Unknown\n"), stdout);
2113 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2115 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
2118 new_size = file_get_size(argv[0]);
2119 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2121 tfputs(T("Unknown\n"), stdout);
2123 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2125 tfputs(T("Space saved: "), stdout);
2126 if (new_size != -1 && old_size != -1) {
2127 tprintf(T("%lld KiB\n"),
2128 ((long long)old_size - (long long)new_size) >> 10);
2130 tfputs(T("Unknown\n"), stdout);
2138 /* Split a WIM into a spanned set */
2140 imagex_split(int argc, tchar **argv)
2143 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2144 int write_flags = 0;
2145 unsigned long part_size;
2150 for_opt(c, split_options) {
2152 case IMAGEX_CHECK_OPTION:
2153 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2154 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2168 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2169 if (tmp == argv[2] || *tmp) {
2170 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2171 imagex_error(T("The part size must be an integer or "
2172 "floating-point number of megabytes."));
2175 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2178 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2183 /* Unmounts a mounted WIM image. */
2185 imagex_unmount(int argc, tchar **argv)
2188 int unmount_flags = 0;
2191 for_opt(c, unmount_options) {
2193 case IMAGEX_COMMIT_OPTION:
2194 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2196 case IMAGEX_CHECK_OPTION:
2197 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2199 case IMAGEX_REBULID_OPTION:
2200 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2214 ret = wimlib_unmount_image(argv[0], unmount_flags,
2215 imagex_progress_func);
2217 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2221 struct imagex_command {
2223 int (*func)(int , tchar **);
2228 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
2229 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
2231 static const struct imagex_command imagex_commands[] = {
2232 {T("append"), imagex_capture_or_append, APPEND},
2233 {T("apply"), imagex_apply, APPLY},
2234 {T("capture"), imagex_capture_or_append, CAPTURE},
2235 {T("delete"), imagex_delete, DELETE},
2236 {T("dir"), imagex_dir, DIR},
2237 {T("export"), imagex_export, EXPORT},
2238 {T("info"), imagex_info, INFO},
2239 {T("join"), imagex_join, JOIN},
2240 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
2241 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
2242 {T("optimize"),imagex_optimize, OPTIMIZE},
2243 {T("split"), imagex_split, SPLIT},
2244 {T("unmount"), imagex_unmount, UNMOUNT},
2250 static const tchar *s =
2252 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
2253 "Copyright (C) 2012, 2013 Eric Biggers\n"
2254 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
2255 "This is free software: you are free to change and redistribute it.\n"
2256 "There is NO WARRANTY, to the extent permitted by law.\n"
2258 "Report bugs to "PACKAGE_BUGREPORT".\n"
2265 help_or_version(int argc, tchar **argv)
2269 const struct imagex_command *cmd;
2271 for (i = 1; i < argc; i++) {
2279 if (!tstrcmp(p, T("help"))) {
2280 for_imagex_command(cmd) {
2281 if (!tstrcmp(cmd->name, argv[1])) {
2289 if (!tstrcmp(p, T("version"))) {
2300 const struct imagex_command *cmd;
2301 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
2302 for_imagex_command(cmd) {
2303 if (cmd->cmd == cmd_type) {
2304 tprintf(T("\nTry `man "IMAGEX_PROGNAME"-%"TS"' "
2305 "for more details.\n"), cmd->name);
2313 tfputs(T("Usage:\n"), stdout);
2314 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
2315 tprintf(T(" %"TS), usage_strings[i]);
2316 static const tchar *extra =
2318 " "IMAGEX_PROGNAME" --help\n"
2319 " "IMAGEX_PROGNAME" --version\n"
2321 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
2323 " Try `man "IMAGEX_PROGNAME"' for more information.\n"
2325 tfputs(extra, stdout);
2328 /* Entry point for wimlib's ImageX implementation. On UNIX the command
2329 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
2330 * something else), while an Windows the command arguments will be UTF-16LE
2331 * encoded 'wchar_t' strings. */
2334 wmain(int argc, wchar_t **argv, wchar_t **envp)
2336 main(int argc, char **argv)
2339 const struct imagex_command *cmd;
2343 setlocale(LC_ALL, "");
2345 char *codeset = nl_langinfo(CODESET);
2346 if (!strstr(codeset, "UTF-8") &&
2347 !strstr(codeset, "UTF8") &&
2348 !strstr(codeset, "utf-8") &&
2349 !strstr(codeset, "utf8"))
2352 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
2353 " (Maybe try: `export LANG=en_US.UTF-8'?\n", stderr);
2357 #endif /* !__WIN32__ */
2360 imagex_error(T("No command specified"));
2366 /* Handle --help and --version for all commands. Note that this will
2367 * not return if either of these arguments are present. */
2368 help_or_version(argc, argv);
2372 /* The user may like to see more informative error messages. */
2373 wimlib_set_print_errors(true);
2375 /* Do any initializations that the library needs */
2376 ret = wimlib_global_init();
2378 goto out_check_status;
2380 /* Search for the function to handle the ImageX subcommand. */
2381 for_imagex_command(cmd) {
2382 if (!tstrcmp(cmd->name, *argv)) {
2383 ret = cmd->func(argc, argv);
2384 goto out_check_write_error;
2388 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
2392 out_check_write_error:
2393 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
2394 * standard output is part of the program's actual behavior and not just
2395 * for informational purposes, so we should set a failure exit status if
2396 * there was a write error. */
2397 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
2398 if (ferror(stdout) || fclose(stdout)) {
2399 imagex_error_with_errno(T("error writing to standard output"));
2405 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
2406 * outside of the wimlib library code. 0 indicates success. > 0
2407 * indicates a wimlib error code from which an error message can be
2410 imagex_error(T("Exiting with error code %d:\n"
2412 wimlib_get_error_string(ret));
2413 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
2414 imagex_error_with_errno(T("errno"));
2417 /* Make the library free any resources it's holding (not strictly
2418 * necessary because the process is ending anyway). */
2419 wimlib_global_cleanup();