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"
382 "[CompressionExclusionList]\n"
386 "\\WINDOWS\\inf\\*.pnf\n"
390 PARSE_FILENAME_SUCCESS = 0,
391 PARSE_FILENAME_FAILURE = 1,
392 PARSE_FILENAME_NONE = 2,
396 * Parses a filename in the source list file format. (See the man page for
397 * 'wimlib-imagex capture' for details on this format and the meaning.)
398 * Accepted formats for filenames are an unquoted string (whitespace-delimited),
399 * or a double or single-quoted string.
401 * @line_p: Pointer to the pointer to the line of data. Will be updated
402 * to point past the filename iff the return value is
403 * PARSE_FILENAME_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
406 * @len_p: @len_p initially stores the length of the line of data, which may
407 * be 0, and it will be updated to the number of bytes remaining in
408 * the line iff the return value is PARSE_FILENAME_SUCCESS.
410 * @fn_ret: Iff the return value is PARSE_FILENAME_SUCCESS, a pointer to the
411 * parsed filename will be returned here.
413 * Returns: PARSE_FILENAME_SUCCESS if a filename was successfully parsed; or
414 * PARSE_FILENAME_FAILURE if the data was invalid due to a missing
415 * closing quote; or PARSE_FILENAME_NONE if the line ended before the
416 * beginning of a filename was found.
419 parse_filename(tchar **line_p, size_t *len_p, tchar **fn_ret)
422 tchar *line = *line_p;
426 /* Skip leading whitespace */
429 return PARSE_FILENAME_NONE;
430 if (!istspace(*line) && *line != T('\0'))
436 if (quote_char == T('"') || quote_char == T('\'')) {
437 /* Quoted filename */
441 line = tmemchr(line, quote_char, len);
443 imagex_error(T("Missing closing quote: %"TS), fn - 1);
444 return PARSE_FILENAME_FAILURE;
447 /* Unquoted filename. Go until whitespace. Line is terminated
448 * by '\0', so no need to check 'len'. */
452 } while (!istspace(*line) && *line != T('\0'));
459 return PARSE_FILENAME_SUCCESS;
462 /* Parses a line of data (not an empty line or comment) in the source list file
463 * format. (See the man page for 'wimlib-imagex capture' for details on this
464 * format and the meaning.)
466 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
467 * len == 0. The data in @line will be modified by this function call.
469 * @len: Length of the line of data.
471 * @source: On success, the capture source and target described by the line is
472 * written into this destination. Note that it will contain pointers
473 * to data in the @line array.
475 * Returns true if the line was valid; false otherwise. */
477 parse_source_list_line(tchar *line, size_t len,
478 struct wimlib_capture_source *source)
482 ret = parse_filename(&line, &len, &source->fs_source_path);
483 if (ret != PARSE_FILENAME_SUCCESS)
485 ret = parse_filename(&line, &len, &source->wim_target_path);
486 if (ret == PARSE_FILENAME_NONE)
487 source->wim_target_path = source->fs_source_path;
488 return ret != PARSE_FILENAME_FAILURE;
491 /* Returns %true if the given line of length @len > 0 is a comment or empty line
492 * in the source list file format. */
494 is_comment_line(const tchar *line, size_t len)
499 if (!istspace(*line) && *line != T('\0'))
508 /* Parses a file in the source list format. (See the man page for
509 * 'wimlib-imagex capture' for details on this format and the meaning.)
511 * @source_list_contents: Contents of the source list file. Note that this
512 * buffer will be modified to save memory allocations,
513 * and cannot be freed until the returned array of
514 * wimlib_capture_source's has also been freed.
516 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
519 * @nsources_ret: On success, the length of the returned array is
522 * Returns: An array of `struct wimlib_capture_source's that can be passed to
523 * the wimlib_add_image_multisource() function to specify how a WIM image is to
525 static struct wimlib_capture_source *
526 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
527 size_t *nsources_ret)
531 struct wimlib_capture_source *sources;
533 tchar *source_list_contents = *source_list_contents_p;
536 for (i = 0; i < source_list_nchars; i++)
537 if (source_list_contents[i] == T('\n'))
540 /* Handle last line not terminated by a newline */
541 if (source_list_nchars != 0 &&
542 source_list_contents[source_list_nchars - 1] != T('\n'))
544 source_list_contents = realloc(source_list_contents,
545 (source_list_nchars + 1) * sizeof(tchar));
546 if (!source_list_contents)
548 source_list_contents[source_list_nchars] = T('\n');
549 *source_list_contents_p = source_list_contents;
550 source_list_nchars++;
554 sources = calloc(nlines, sizeof(*sources));
557 p = source_list_contents;
559 for (i = 0; i < nlines; i++) {
560 /* XXX: Could use rawmemchr() here instead, but it may not be
561 * available on all platforms. */
562 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
563 size_t len = endp - p + 1;
565 if (!is_comment_line(p, len)) {
566 if (!parse_source_list_line(p, len, &sources[j++])) {
577 imagex_error(T("out of memory"));
581 /* Reads the contents of a file into memory. */
583 file_get_contents(const tchar *filename, size_t *len_ret)
590 if (tstat(filename, &stbuf) != 0) {
591 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
596 fp = tfopen(filename, T("rb"));
598 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
604 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
605 "contents of file \"%"TS"\""), len, filename);
608 if (fread(buf, 1, len, fp) != len) {
609 imagex_error_with_errno(T("Failed to read %zu bytes from the "
610 "file \"%"TS"\""), len, filename);
624 /* Read standard input until EOF and return the full contents in a malloc()ed
625 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
628 stdin_get_contents(size_t *len_ret)
630 /* stdin can, of course, be a pipe or other non-seekable file, so the
631 * total length of the data cannot be pre-determined */
633 size_t newlen = 1024;
637 char *p = realloc(buf, newlen);
638 size_t bytes_read, bytes_to_read;
640 imagex_error(T("out of memory while reading stdin"));
644 bytes_to_read = newlen - pos;
645 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
647 if (bytes_read != bytes_to_read) {
652 imagex_error_with_errno(T("error reading stdin"));
666 translate_text_to_tstr(char *text, size_t num_bytes,
667 size_t *num_tchars_ret)
670 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
672 *num_tchars_ret = num_bytes;
674 #else /* !__WIN32__ */
675 /* On Windows, translate the text to UTF-16LE */
679 if (num_bytes >= 2 &&
680 ((text[0] == 0xff && text[1] == 0xfe) ||
681 (text[0] <= 0x7f && text[1] == 0x00)))
683 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
684 * with something that looks like an ASCII character encoded as
685 * a UTF-16LE code unit. Assume the file is encoded as
686 * UTF-16LE. This is not a 100% reliable check. */
687 num_wchars = num_bytes / 2;
688 text_wstr = (wchar_t*)text;
690 /* File does not look like UTF-16LE. Assume it is encoded in
691 * the current Windows code page. I think these are always
692 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
693 * should work as expected. */
694 text_wstr = win32_mbs_to_wcs(text,
699 *num_tchars_ret = num_wchars;
701 #endif /* __WIN32__ */
705 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
710 contents = file_get_contents(filename, &num_bytes);
713 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
717 stdin_get_text_contents(size_t *num_tchars_ret)
722 contents = stdin_get_contents(&num_bytes);
725 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
728 /* Return 0 if a path names a file to which the current user has write access;
729 * -1 otherwise (and print an error message). */
731 file_writable(const tchar *path)
734 ret = taccess(path, W_OK);
736 imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
740 #define TO_PERCENT(numerator, denominator) \
741 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
743 /* Given an enumerated value for WIM compression type, return a descriptive
746 get_data_type(int ctype)
749 case WIMLIB_COMPRESSION_TYPE_NONE:
750 return T("uncompressed");
751 case WIMLIB_COMPRESSION_TYPE_LZX:
752 return T("LZX-compressed");
753 case WIMLIB_COMPRESSION_TYPE_XPRESS:
754 return T("XPRESS-compressed");
759 /* Progress callback function passed to various wimlib functions. */
761 imagex_progress_func(enum wimlib_progress_msg msg,
762 const union wimlib_progress_info *info)
764 unsigned percent_done;
766 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
767 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
768 info->write_streams.total_bytes);
769 if (info->write_streams.completed_streams == 0) {
770 const tchar *data_type;
772 data_type = get_data_type(info->write_streams.compression_type);
773 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
774 data_type, info->write_streams.num_threads,
775 (info->write_streams.num_threads == 1) ? T("") : T("s"));
777 tprintf(T("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
778 "written (%u%% done)"),
779 info->write_streams.completed_bytes >> 20,
780 info->write_streams.total_bytes >> 20,
782 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
785 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
786 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
787 if (*info->scan.wim_target_path) {
788 tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
789 info->scan.wim_target_path);
791 tprintf(T(" (loading as root of WIM image)...\n"));
794 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
795 if (info->scan.excluded)
796 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
798 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
800 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
802 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
803 percent_done = TO_PERCENT(info->integrity.completed_bytes,
804 info->integrity.total_bytes);
805 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" MiB "
806 "of %"PRIu64" MiB (%u%%) done"),
807 info->integrity.filename,
808 info->integrity.completed_bytes >> 20,
809 info->integrity.total_bytes >> 20,
811 if (info->integrity.completed_bytes == info->integrity.total_bytes)
814 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
815 percent_done = TO_PERCENT(info->integrity.completed_bytes,
816 info->integrity.total_bytes);
817 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" MiB "
818 "of %"PRIu64" MiB (%u%%) done"),
819 info->integrity.completed_bytes >> 20,
820 info->integrity.total_bytes >> 20,
822 if (info->integrity.completed_bytes == info->integrity.total_bytes)
825 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
826 tprintf(T("Applying image %d (%"TS") from \"%"TS"\" "
827 "to %"TS" \"%"TS"\"\n"),
829 info->extract.image_name,
830 info->extract.wimfile_name,
831 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
832 T("NTFS volume") : T("directory")),
833 info->extract.target);
835 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
836 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
837 /*info->extract.target);*/
839 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
840 percent_done = TO_PERCENT(info->extract.completed_bytes,
841 info->extract.total_bytes);
842 tprintf(T("\rExtracting files: "
843 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done"),
844 info->extract.completed_bytes >> 20,
845 info->extract.total_bytes >> 20,
847 if (info->extract.completed_bytes >= info->extract.total_bytes)
850 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
851 tprintf(T("%"TS"\n"), info->extract.cur_path);
853 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
854 tprintf(T("Setting timestamps on all extracted files...\n"));
856 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
857 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
858 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
859 info->extract.target);
862 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
863 percent_done = TO_PERCENT(info->join.completed_bytes,
864 info->join.total_bytes);
865 tprintf(T("Writing resources from part %u of %u: "
866 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n"),
867 (info->join.completed_parts == info->join.total_parts) ?
868 info->join.completed_parts : info->join.completed_parts + 1,
869 info->join.total_parts,
870 info->join.completed_bytes >> 20,
871 info->join.total_bytes >> 20,
874 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
875 percent_done = TO_PERCENT(info->split.completed_bytes,
876 info->split.total_bytes);
877 tprintf(T("Writing \"%"TS"\": %"PRIu64" MiB of "
878 "%"PRIu64" MiB (%u%%) written\n"),
879 info->split.part_name,
880 info->split.completed_bytes >> 20,
881 info->split.total_bytes >> 20,
884 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
885 if (info->split.completed_bytes == info->split.total_bytes) {
886 tprintf(T("Finished writing %u split WIM parts\n"),
887 info->split.cur_part_number);
897 /* Open all the split WIM parts that correspond to a file glob.
899 * @first_part specifies the first part of the split WIM and it may be either
900 * included or omitted from the glob. */
902 open_swms_from_glob(const tchar *swm_glob,
903 const tchar *first_part,
905 WIMStruct ***additional_swms_ret,
906 unsigned *num_additional_swms_ret)
908 unsigned num_additional_swms = 0;
909 WIMStruct **additional_swms = NULL;
913 /* Warning: glob() is replaced in Windows native builds */
914 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
916 if (ret == GLOB_NOMATCH) {
917 imagex_error(T("Found no files for glob \"%"TS"\""),
920 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
926 num_additional_swms = globbuf.gl_pathc;
927 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
928 if (!additional_swms) {
929 imagex_error(T("Out of memory"));
934 for (unsigned i = 0; i < num_additional_swms; i++) {
935 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
939 ret = wimlib_open_wim(globbuf.gl_pathv[i],
940 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
941 &additional_swms[i - offset],
942 imagex_progress_func);
946 *additional_swms_ret = additional_swms;
947 *num_additional_swms_ret = num_additional_swms - offset;
951 for (unsigned i = 0; i < num_additional_swms; i++)
952 wimlib_free(additional_swms[i]);
953 free(additional_swms);
962 parse_num_threads(const tchar *optarg)
965 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
966 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
967 imagex_error(T("Number of threads must be a non-negative integer!"));
975 /* Apply one image, or all images, from a WIM file into a directory, OR apply
976 * one image from a WIM file to a NTFS volume. */
978 imagex_apply(int argc, tchar **argv)
981 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
986 const tchar *wimfile;
988 const tchar *image_num_or_name;
989 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
991 const tchar *swm_glob = NULL;
992 WIMStruct **additional_swms = NULL;
993 unsigned num_additional_swms = 0;
995 for_opt(c, apply_options) {
997 case IMAGEX_CHECK_OPTION:
998 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1000 case IMAGEX_HARDLINK_OPTION:
1001 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1003 case IMAGEX_SYMLINK_OPTION:
1004 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1006 case IMAGEX_VERBOSE_OPTION:
1007 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1009 case IMAGEX_REF_OPTION:
1012 case IMAGEX_UNIX_DATA_OPTION:
1013 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1015 case IMAGEX_NO_ACLS_OPTION:
1016 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1018 case IMAGEX_STRICT_ACLS_OPTION:
1019 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1028 if (argc != 2 && argc != 3) {
1035 image_num_or_name = T("1");
1038 image_num_or_name = argv[1];
1042 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1046 image = wimlib_resolve_image(w, image_num_or_name);
1047 ret = verify_image_exists(image, image_num_or_name, wimfile);
1051 num_images = wimlib_get_num_images(w);
1052 if (argc == 2 && num_images != 1) {
1053 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1054 "(or all)"), wimfile, num_images);
1061 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1063 &num_additional_swms);
1070 ret = tstat(target, &stbuf);
1072 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1073 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1075 if (errno != ENOENT) {
1076 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1084 win32_acquire_restore_privileges();
1086 ret = wimlib_extract_image(w, image, target, extract_flags,
1087 additional_swms, num_additional_swms,
1088 imagex_progress_func);
1090 tprintf(T("Done applying WIM image.\n"));
1092 win32_release_restore_privileges();
1096 if (additional_swms) {
1097 for (unsigned i = 0; i < num_additional_swms; i++)
1098 wimlib_free(additional_swms[i]);
1099 free(additional_swms);
1104 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1105 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1106 * the desired image. 'wimlib-imagex append': add a new image to an existing
1109 imagex_capture_or_append(int argc, tchar **argv)
1113 int add_image_flags = 0;
1114 int write_flags = 0;
1115 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1116 const tchar *wimfile;
1119 const tchar *flags_element = NULL;
1120 WIMStruct *w = NULL;
1123 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1124 unsigned num_threads = 0;
1127 size_t source_name_len;
1130 const tchar *config_file = NULL;
1131 tchar *config_str = NULL;
1134 bool source_list = false;
1135 size_t source_list_nchars;
1136 tchar *source_list_contents = NULL;
1137 bool capture_sources_malloced = false;
1138 struct wimlib_capture_source *capture_sources;
1141 for_opt(c, capture_or_append_options) {
1143 case IMAGEX_BOOT_OPTION:
1144 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1146 case IMAGEX_CHECK_OPTION:
1147 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1148 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1150 case IMAGEX_CONFIG_OPTION:
1151 config_file = optarg;
1153 case IMAGEX_COMPRESS_OPTION:
1154 compression_type = get_compression_type(optarg);
1155 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1158 case IMAGEX_FLAGS_OPTION:
1159 flags_element = optarg;
1161 case IMAGEX_DEREFERENCE_OPTION:
1162 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1164 case IMAGEX_VERBOSE_OPTION:
1165 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1167 case IMAGEX_THREADS_OPTION:
1168 num_threads = parse_num_threads(optarg);
1169 if (num_threads == UINT_MAX)
1172 case IMAGEX_REBULID_OPTION:
1173 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1175 case IMAGEX_UNIX_DATA_OPTION:
1176 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1178 case IMAGEX_SOURCE_LIST_OPTION:
1181 case IMAGEX_NO_ACLS_OPTION:
1182 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1184 case IMAGEX_STRICT_ACLS_OPTION:
1185 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1195 if (argc < 2 || argc > 4) {
1206 /* Set default name to SOURCE argument, omitting any directory
1207 * prefixes and trailing slashes. This requires making a copy
1209 source_name_len = tstrlen(source);
1210 source_copy = alloca((source_name_len + 1) * sizeof(tchar));
1211 name = tbasename(tstrcpy(source_copy, source));
1213 /* Image description defaults to NULL if not given. */
1214 desc = (argc >= 4) ? argv[3] : NULL;
1217 /* Set up capture sources in source list mode */
1218 if (source[0] == T('-') && source[1] == T('\0')) {
1219 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1221 source_list_contents = file_get_text_contents(source,
1222 &source_list_nchars);
1224 if (!source_list_contents)
1227 capture_sources = parse_source_list(&source_list_contents,
1230 if (!capture_sources) {
1234 capture_sources_malloced = true;
1236 /* Set up capture source in non-source-list mode (could be
1237 * either "normal" mode or "NTFS mode"--- see the man page). */
1238 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1239 capture_sources[0].fs_source_path = source;
1240 capture_sources[0].wim_target_path = NULL;
1241 capture_sources[0].reserved = 0;
1246 config_str = file_get_text_contents(config_file, &config_len);
1254 ret = wimlib_open_wim(wimfile, open_flags, &w,
1255 imagex_progress_func);
1257 ret = wimlib_create_new_wim(compression_type, &w);
1263 ret = tstat(source, &stbuf);
1265 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1266 tprintf(T("Capturing WIM image from NTFS "
1267 "filesystem on \"%"TS"\"\n"), source);
1268 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1271 if (errno != ENOENT) {
1272 imagex_error_with_errno(T("Failed to stat "
1273 "\"%"TS"\""), source);
1280 win32_acquire_capture_privileges();
1283 ret = wimlib_add_image_multisource(w, capture_sources,
1285 (config_str ? config_str :
1286 default_capture_config),
1287 (config_str ? config_len :
1288 tstrlen(default_capture_config)),
1290 imagex_progress_func);
1292 goto out_release_privs;
1293 cur_image = wimlib_get_num_images(w);
1295 ret = wimlib_set_image_descripton(w, cur_image, desc);
1297 goto out_release_privs;
1299 if (flags_element) {
1300 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1302 goto out_release_privs;
1304 if (cmd == APPEND) {
1305 ret = wimlib_overwrite(w, write_flags, num_threads,
1306 imagex_progress_func);
1308 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1309 num_threads, imagex_progress_func);
1311 if (ret == WIMLIB_ERR_REOPEN)
1314 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1318 win32_release_capture_privileges();
1323 free(source_list_contents);
1324 if (capture_sources_malloced)
1325 free(capture_sources);
1329 /* Remove image(s) from a WIM. */
1331 imagex_delete(int argc, tchar **argv)
1335 int write_flags = 0;
1336 const tchar *wimfile;
1337 const tchar *image_num_or_name;
1342 for_opt(c, delete_options) {
1344 case IMAGEX_CHECK_OPTION:
1345 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1346 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1348 case IMAGEX_SOFT_OPTION:
1349 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1361 imagex_error(T("Must specify a WIM file"));
1363 imagex_error(T("Must specify an image"));
1368 image_num_or_name = argv[1];
1370 ret = file_writable(wimfile);
1374 ret = wimlib_open_wim(wimfile, open_flags, &w,
1375 imagex_progress_func);
1379 image = wimlib_resolve_image(w, image_num_or_name);
1381 ret = verify_image_exists(image, image_num_or_name, wimfile);
1385 ret = wimlib_delete_image(w, image);
1387 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1391 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1392 if (ret == WIMLIB_ERR_REOPEN)
1395 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1396 "deleted"), wimfile);
1403 /* Print the files contained in an image(s) in a WIM file. */
1405 imagex_dir(int argc, tchar **argv)
1407 const tchar *wimfile;
1414 imagex_error(T("Must specify a WIM file"));
1419 imagex_error(T("Too many arguments"));
1425 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1426 imagex_progress_func);
1431 image = wimlib_resolve_image(w, argv[2]);
1432 ret = verify_image_exists(image, argv[2], wimfile);
1436 /* Image was not specified. If the WIM only contains one image,
1437 * choose that one; otherwise, print an error. */
1438 num_images = wimlib_get_num_images(w);
1439 if (num_images != 1) {
1440 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
1441 "select one."), wimfile, num_images);
1449 ret = wimlib_print_files(w, image);
1455 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1458 imagex_export(int argc, tchar **argv)
1462 int export_flags = 0;
1463 int write_flags = 0;
1464 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1465 bool compression_type_specified = false;
1466 const tchar *src_wimfile;
1467 const tchar *src_image_num_or_name;
1468 const tchar *dest_wimfile;
1469 const tchar *dest_name;
1470 const tchar *dest_desc;
1471 WIMStruct *src_w = NULL;
1472 WIMStruct *dest_w = NULL;
1477 const tchar *swm_glob = NULL;
1478 WIMStruct **additional_swms = NULL;
1479 unsigned num_additional_swms = 0;
1480 unsigned num_threads = 0;
1482 for_opt(c, export_options) {
1484 case IMAGEX_BOOT_OPTION:
1485 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1487 case IMAGEX_CHECK_OPTION:
1488 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1489 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1491 case IMAGEX_COMPRESS_OPTION:
1492 compression_type = get_compression_type(optarg);
1493 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1495 compression_type_specified = true;
1497 case IMAGEX_REF_OPTION:
1500 case IMAGEX_THREADS_OPTION:
1501 num_threads = parse_num_threads(optarg);
1502 if (num_threads == UINT_MAX)
1505 case IMAGEX_REBULID_OPTION:
1506 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1515 if (argc < 3 || argc > 5) {
1519 src_wimfile = argv[0];
1520 src_image_num_or_name = argv[1];
1521 dest_wimfile = argv[2];
1522 dest_name = (argc >= 4) ? argv[3] : NULL;
1523 dest_desc = (argc >= 5) ? argv[4] : NULL;
1524 ret = wimlib_open_wim(src_wimfile,
1525 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1526 imagex_progress_func);
1530 /* Determine if the destination is an existing file or not.
1531 * If so, we try to append the exported image(s) to it; otherwise, we
1532 * create a new WIM containing the exported image(s). */
1533 if (tstat(dest_wimfile, &stbuf) == 0) {
1537 /* Destination file exists. */
1539 if (!S_ISREG(stbuf.st_mode)) {
1540 imagex_error(T("\"%"TS"\" is not a regular file"),
1545 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1546 imagex_progress_func);
1550 ret = file_writable(dest_wimfile);
1554 dest_ctype = wimlib_get_compression_type(dest_w);
1555 if (compression_type_specified
1556 && compression_type != dest_ctype)
1558 imagex_error(T("Cannot specify a compression type that is "
1559 "not the same as that used in the "
1560 "destination WIM"));
1566 /* dest_wimfile is not an existing file, so create a new WIM. */
1567 if (!compression_type_specified)
1568 compression_type = wimlib_get_compression_type(src_w);
1569 if (errno == ENOENT) {
1570 ret = wimlib_create_new_wim(compression_type, &dest_w);
1574 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
1581 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1582 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1587 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1589 &num_additional_swms);
1594 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1595 export_flags, additional_swms,
1596 num_additional_swms, imagex_progress_func);
1602 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1603 write_flags, num_threads,
1604 imagex_progress_func);
1606 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1607 imagex_progress_func);
1609 if (ret == WIMLIB_ERR_REOPEN)
1612 wimlib_free(dest_w);
1613 if (additional_swms) {
1614 for (unsigned i = 0; i < num_additional_swms; i++)
1615 wimlib_free(additional_swms[i]);
1616 free(additional_swms);
1621 /* Prints information about a WIM file; also can mark an image as bootable,
1622 * change the name of an image, or change the description of an image. */
1624 imagex_info(int argc, tchar **argv)
1629 bool header = false;
1630 bool lookup_table = false;
1632 bool metadata = false;
1633 bool short_header = true;
1634 const tchar *xml_out_file = NULL;
1635 const tchar *wimfile;
1636 const tchar *image_num_or_name = T("all");
1637 const tchar *new_name = NULL;
1638 const tchar *new_desc = NULL;
1643 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1648 for_opt(c, info_options) {
1650 case IMAGEX_BOOT_OPTION:
1653 case IMAGEX_CHECK_OPTION:
1656 case IMAGEX_HEADER_OPTION:
1658 short_header = false;
1660 case IMAGEX_LOOKUP_TABLE_OPTION:
1661 lookup_table = true;
1662 short_header = false;
1664 case IMAGEX_XML_OPTION:
1666 short_header = false;
1668 case IMAGEX_EXTRACT_XML_OPTION:
1669 xml_out_file = optarg;
1670 short_header = false;
1672 case IMAGEX_METADATA_OPTION:
1674 short_header = false;
1684 if (argc == 0 || argc > 4) {
1690 image_num_or_name = argv[1];
1700 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1702 ret = wimlib_open_wim(wimfile, open_flags, &w,
1703 imagex_progress_func);
1707 part_number = wimlib_get_part_number(w, &total_parts);
1709 image = wimlib_resolve_image(w, image_num_or_name);
1710 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
1711 imagex_error(T("The image \"%"TS"\" does not exist"),
1714 imagex_error(T("If you would like to set the boot "
1715 "index to 0, specify image \"0\" with "
1716 "the --boot flag."));
1718 ret = WIMLIB_ERR_INVALID_IMAGE;
1722 num_images = wimlib_get_num_images(w);
1724 if (num_images == 0) {
1726 imagex_error(T("--boot is meaningless on a WIM with no "
1728 ret = WIMLIB_ERR_INVALID_IMAGE;
1733 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1735 imagex_error(T("Cannot specify the --boot flag "
1736 "without specifying a specific "
1737 "image in a multi-image WIM"));
1738 ret = WIMLIB_ERR_INVALID_IMAGE;
1742 imagex_error(T("Cannot specify the NEW_NAME "
1743 "without specifying a specific "
1744 "image in a multi-image WIM"));
1745 ret = WIMLIB_ERR_INVALID_IMAGE;
1750 /* Operations that print information are separated from operations that
1751 * recreate the WIM file. */
1752 if (!new_name && !boot) {
1754 /* Read-only operations */
1756 if (image == WIMLIB_NO_IMAGE) {
1757 imagex_error(T("\"%"TS"\" is not a valid image"),
1759 ret = WIMLIB_ERR_INVALID_IMAGE;
1763 if (image == WIMLIB_ALL_IMAGES && short_header)
1764 wimlib_print_wim_information(w);
1767 wimlib_print_header(w);
1770 if (total_parts != 1) {
1771 tprintf(T("Warning: Only showing the lookup table "
1772 "for part %d of a %d-part WIM.\n"),
1773 part_number, total_parts);
1775 wimlib_print_lookup_table(w);
1779 ret = wimlib_extract_xml_data(w, stdout);
1785 fp = tfopen(xml_out_file, T("wb"));
1787 imagex_error_with_errno(T("Failed to open the "
1788 "file \"%"TS"\" for "
1794 ret = wimlib_extract_xml_data(w, fp);
1795 if (fclose(fp) != 0) {
1796 imagex_error(T("Failed to close the file "
1807 wimlib_print_available_images(w, image);
1810 ret = wimlib_print_metadata(w, image);
1816 /* Modification operations */
1817 if (total_parts != 1) {
1818 imagex_error(T("Modifying a split WIM is not supported."));
1822 if (image == WIMLIB_ALL_IMAGES)
1825 if (image == WIMLIB_NO_IMAGE && new_name) {
1826 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
1827 "when using image 0"), new_name);
1833 if (image == wimlib_get_boot_idx(w)) {
1834 tprintf(T("Image %d is already marked as "
1835 "bootable.\n"), image);
1838 tprintf(T("Marking image %d as bootable.\n"),
1840 wimlib_set_boot_idx(w, image);
1844 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
1846 tprintf(T("Image %d is already named \"%"TS"\".\n"),
1850 tprintf(T("Changing the name of image %d to "
1851 "\"%"TS"\".\n"), image, new_name);
1852 ret = wimlib_set_image_name(w, image, new_name);
1858 const tchar *old_desc;
1859 old_desc = wimlib_get_image_description(w, image);
1860 if (old_desc && !tstrcmp(old_desc, new_desc)) {
1861 tprintf(T("The description of image %d is already "
1862 "\"%"TS"\".\n"), image, new_desc);
1865 tprintf(T("Changing the description of image %d "
1866 "to \"%"TS"\".\n"), image, new_desc);
1867 ret = wimlib_set_image_descripton(w, image,
1874 /* Only call wimlib_overwrite() if something actually needs to
1876 if (boot || new_name || new_desc ||
1877 (check && !wimlib_has_integrity_table(w)))
1881 ret = file_writable(wimfile);
1886 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1890 ret = wimlib_overwrite(w, write_flags, 1,
1891 imagex_progress_func);
1892 if (ret == WIMLIB_ERR_REOPEN)
1895 tprintf(T("The file \"%"TS"\" was not modified because nothing "
1896 "needed to be done.\n"), wimfile);
1905 /* Join split WIMs into one part WIM */
1907 imagex_join(int argc, tchar **argv)
1910 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1911 int wim_write_flags = 0;
1912 const tchar *output_path;
1914 for_opt(c, join_options) {
1916 case IMAGEX_CHECK_OPTION:
1917 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1918 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1928 imagex_error(T("Must specify one or more split WIM (.swm) "
1932 output_path = argv[0];
1933 return wimlib_join((const tchar * const *)++argv,
1938 imagex_progress_func);
1944 /* Mounts an image using a FUSE mount. */
1946 imagex_mount_rw_or_ro(int argc, tchar **argv)
1949 int mount_flags = 0;
1950 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1951 const tchar *wimfile;
1957 const tchar *swm_glob = NULL;
1958 WIMStruct **additional_swms = NULL;
1959 unsigned num_additional_swms = 0;
1960 const tchar *staging_dir = NULL;
1962 if (!tstrcmp(argv[0], T("mountrw")))
1963 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1965 for_opt(c, mount_options) {
1967 case IMAGEX_ALLOW_OTHER_OPTION:
1968 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1970 case IMAGEX_CHECK_OPTION:
1971 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1973 case IMAGEX_DEBUG_OPTION:
1974 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1976 case IMAGEX_STREAMS_INTERFACE_OPTION:
1977 if (!tstrcasecmp(optarg, T("none")))
1978 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1979 else if (!tstrcasecmp(optarg, T("xattr")))
1980 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1981 else if (!tstrcasecmp(optarg, T("windows")))
1982 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1984 imagex_error(T("Unknown stream interface \"%"TS"\""),
1989 case IMAGEX_REF_OPTION:
1992 case IMAGEX_STAGING_DIR_OPTION:
1993 staging_dir = optarg;
1995 case IMAGEX_UNIX_DATA_OPTION:
1996 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2004 if (argc != 2 && argc != 3)
2009 ret = wimlib_open_wim(wimfile, open_flags, &w,
2010 imagex_progress_func);
2015 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2017 &num_additional_swms);
2024 num_images = wimlib_get_num_images(w);
2025 if (num_images != 1) {
2026 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
2027 "select one."), wimfile, num_images);
2028 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2035 image = wimlib_resolve_image(w, argv[1]);
2037 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2042 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2043 ret = file_writable(wimfile);
2048 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2049 num_additional_swms, staging_dir);
2051 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2053 image, wimfile, dir);
2058 if (additional_swms) {
2059 for (unsigned i = 0; i < num_additional_swms; i++)
2060 wimlib_free(additional_swms[i]);
2061 free(additional_swms);
2065 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2070 /* Rebuild a WIM file */
2072 imagex_optimize(int argc, tchar **argv)
2076 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2079 const tchar *wimfile;
2083 for_opt(c, optimize_options) {
2085 case IMAGEX_CHECK_OPTION:
2086 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2087 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2089 case IMAGEX_RECOMPRESS_OPTION:
2090 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2107 ret = wimlib_open_wim(wimfile, open_flags, &w,
2108 imagex_progress_func);
2112 old_size = file_get_size(argv[0]);
2113 tprintf(T("\"%"TS"\" original size: "), wimfile);
2115 tfputs(T("Unknown\n"), stdout);
2117 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2119 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
2122 new_size = file_get_size(argv[0]);
2123 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2125 tfputs(T("Unknown\n"), stdout);
2127 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2129 tfputs(T("Space saved: "), stdout);
2130 if (new_size != -1 && old_size != -1) {
2131 tprintf(T("%lld KiB\n"),
2132 ((long long)old_size - (long long)new_size) >> 10);
2134 tfputs(T("Unknown\n"), stdout);
2142 /* Split a WIM into a spanned set */
2144 imagex_split(int argc, tchar **argv)
2147 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2148 int write_flags = 0;
2149 unsigned long part_size;
2154 for_opt(c, split_options) {
2156 case IMAGEX_CHECK_OPTION:
2157 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2158 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2172 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2173 if (tmp == argv[2] || *tmp) {
2174 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2175 imagex_error(T("The part size must be an integer or "
2176 "floating-point number of megabytes."));
2179 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2182 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2187 /* Unmounts a mounted WIM image. */
2189 imagex_unmount(int argc, tchar **argv)
2192 int unmount_flags = 0;
2195 for_opt(c, unmount_options) {
2197 case IMAGEX_COMMIT_OPTION:
2198 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2200 case IMAGEX_CHECK_OPTION:
2201 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2203 case IMAGEX_REBULID_OPTION:
2204 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2218 ret = wimlib_unmount_image(argv[0], unmount_flags,
2219 imagex_progress_func);
2221 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2225 struct imagex_command {
2227 int (*func)(int , tchar **);
2232 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
2233 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
2235 static const struct imagex_command imagex_commands[] = {
2236 {T("append"), imagex_capture_or_append, APPEND},
2237 {T("apply"), imagex_apply, APPLY},
2238 {T("capture"), imagex_capture_or_append, CAPTURE},
2239 {T("delete"), imagex_delete, DELETE},
2240 {T("dir"), imagex_dir, DIR},
2241 {T("export"), imagex_export, EXPORT},
2242 {T("info"), imagex_info, INFO},
2243 {T("join"), imagex_join, JOIN},
2244 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
2245 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
2246 {T("optimize"),imagex_optimize, OPTIMIZE},
2247 {T("split"), imagex_split, SPLIT},
2248 {T("unmount"), imagex_unmount, UNMOUNT},
2254 static const tchar *s =
2256 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
2257 "Copyright (C) 2012, 2013 Eric Biggers\n"
2258 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
2259 "This is free software: you are free to change and redistribute it.\n"
2260 "There is NO WARRANTY, to the extent permitted by law.\n"
2262 "Report bugs to "PACKAGE_BUGREPORT".\n"
2269 help_or_version(int argc, tchar **argv)
2273 const struct imagex_command *cmd;
2275 for (i = 1; i < argc; i++) {
2283 if (!tstrcmp(p, T("help"))) {
2284 for_imagex_command(cmd) {
2285 if (!tstrcmp(cmd->name, argv[1])) {
2293 if (!tstrcmp(p, T("version"))) {
2304 const struct imagex_command *cmd;
2305 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
2306 for_imagex_command(cmd) {
2307 if (cmd->cmd == cmd_type) {
2308 tprintf(T("\nTry `man "IMAGEX_PROGNAME"-%"TS"' "
2309 "for more details.\n"), cmd->name);
2317 tfputs(T("Usage:\n"), stdout);
2318 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
2319 tprintf(T(" %"TS), usage_strings[i]);
2320 static const tchar *extra =
2322 " "IMAGEX_PROGNAME" --help\n"
2323 " "IMAGEX_PROGNAME" --version\n"
2325 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
2327 " Try `man "IMAGEX_PROGNAME"' for more information.\n"
2329 tfputs(extra, stdout);
2332 /* Entry point for wimlib's ImageX implementation. On UNIX the command
2333 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
2334 * something else), while an Windows the command arguments will be UTF-16LE
2335 * encoded 'wchar_t' strings. */
2338 wmain(int argc, wchar_t **argv, wchar_t **envp)
2340 main(int argc, char **argv)
2343 const struct imagex_command *cmd;
2347 setlocale(LC_ALL, "");
2349 char *codeset = nl_langinfo(CODESET);
2350 if (!strstr(codeset, "UTF-8") &&
2351 !strstr(codeset, "UTF8") &&
2352 !strstr(codeset, "utf-8") &&
2353 !strstr(codeset, "utf8"))
2356 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
2357 " (Maybe try: `export LANG=en_US.UTF-8'?\n", stderr);
2361 #endif /* !__WIN32__ */
2364 imagex_error(T("No command specified"));
2370 /* Handle --help and --version for all commands. Note that this will
2371 * not return if either of these arguments are present. */
2372 help_or_version(argc, argv);
2376 /* The user may like to see more informative error messages. */
2377 wimlib_set_print_errors(true);
2379 /* Do any initializations that the library needs */
2380 ret = wimlib_global_init();
2382 goto out_check_status;
2384 /* Search for the function to handle the ImageX subcommand. */
2385 for_imagex_command(cmd) {
2386 if (!tstrcmp(cmd->name, *argv)) {
2387 ret = cmd->func(argc, argv);
2388 goto out_check_write_error;
2392 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
2396 out_check_write_error:
2397 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
2398 * standard output is part of the program's actual behavior and not just
2399 * for informational purposes, so we should set a failure exit status if
2400 * there was a write error. */
2401 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
2402 if (ferror(stdout) || fclose(stdout)) {
2403 imagex_error_with_errno(T("error writing to standard output"));
2409 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
2410 * outside of the wimlib library code. 0 indicates success. > 0
2411 * indicates a wimlib error code from which an error message can be
2414 imagex_error(T("Exiting with error code %d:\n"
2416 wimlib_get_error_string(ret));
2417 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
2418 imagex_error_with_errno(T("errno"));
2421 /* Make the library free any resources it's holding (not strictly
2422 * necessary because the process is ending anyway). */
2423 wimlib_global_cleanup();