4 * Apply a WIM image directly to an NTFS volume using libntfs-3g. Restore as
5 * much information as possible, including security data, file attributes, DOS
6 * names, alternate data streams, and object IDs.
8 * Note: because NTFS-3G offers inode-based interfaces, we actually don't need
9 * to deal with paths at all! (Other than for error messages.)
13 * Copyright (C) 2012-2017 Eric Biggers
15 * This file is free software; you can redistribute it and/or modify it under
16 * the terms of the GNU Lesser General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option) any
20 * This file is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this file; if not, see https://www.gnu.org/licenses/.
37 #include <ntfs-3g/attrib.h>
38 #include <ntfs-3g/object_id.h>
39 #include <ntfs-3g/reparse.h>
40 #include <ntfs-3g/security.h>
42 #include "wimlib/assert.h"
43 #include "wimlib/apply.h"
44 #include "wimlib/blob_table.h"
45 #include "wimlib/dentry.h"
46 #include "wimlib/encoding.h"
47 #include "wimlib/error.h"
48 #include "wimlib/metadata.h"
49 #include "wimlib/ntfs_3g.h"
50 #include "wimlib/object_id.h"
51 #include "wimlib/reparse.h"
52 #include "wimlib/security.h"
55 ntfs_3g_get_supported_features(const char *target,
56 struct wim_features *supported_features)
58 supported_features->readonly_files = 1;
59 supported_features->hidden_files = 1;
60 supported_features->system_files = 1;
61 supported_features->archive_files = 1;
62 supported_features->compressed_files = 1;
63 supported_features->not_context_indexed_files = 1;
64 supported_features->sparse_files = 1;
65 supported_features->named_data_streams = 1;
66 supported_features->hard_links = 1;
67 supported_features->reparse_points = 1;
68 supported_features->security_descriptors = 1;
69 supported_features->short_names = 1;
70 supported_features->object_ids = 1;
71 supported_features->timestamps = 1;
72 supported_features->case_sensitive_filenames = 1;
76 struct ntfs_3g_apply_ctx {
77 /* Extract flags, the pointer to the WIMStruct, etc. */
78 struct apply_ctx common;
80 /* Pointer to the open NTFS volume */
83 ntfs_attr *open_attrs[MAX_OPEN_FILES];
84 unsigned num_open_attrs;
85 ntfs_inode *open_inodes[MAX_OPEN_FILES];
86 unsigned num_open_inodes;
88 /* For each currently open attribute, whether we're writing to it in
89 * "sparse" mode or not. */
90 bool is_sparse_attr[MAX_OPEN_FILES];
92 /* Whether is_sparse_attr[] is true for any currently open attribute */
93 bool any_sparse_attrs;
95 struct reparse_buffer_disk rpbuf;
98 unsigned num_reparse_inodes;
99 ntfs_inode *ntfs_reparse_inodes[MAX_OPEN_FILES];
100 struct wim_inode *wim_reparse_inodes[MAX_OPEN_FILES];
104 ntfs_3g_set_timestamps(ntfs_inode *ni, const struct wim_inode *inode)
107 inode->i_creation_time,
108 inode->i_last_write_time,
109 inode->i_last_access_time,
112 if (ntfs_inode_set_times(ni, (const char *)times, sizeof(times), 0))
113 return WIMLIB_ERR_SET_TIMESTAMPS;
117 /* Restore the timestamps on the NTFS inode corresponding to @inode. */
119 ntfs_3g_restore_timestamps(ntfs_volume *vol, const struct wim_inode *inode)
124 ni = ntfs_inode_open(vol, inode->i_mft_no);
128 res = ntfs_3g_set_timestamps(ni, inode);
130 if (ntfs_inode_close(ni) || res)
136 ERROR_WITH_ERRNO("Failed to update timestamps of \"%s\" in NTFS volume",
137 dentry_full_path(inode_first_extraction_dentry(inode)));
138 return WIMLIB_ERR_SET_TIMESTAMPS;
141 /* Restore the DOS name of the @dentry.
142 * This closes both @ni and @dir_ni.
143 * If either is NULL, then they are opened temporarily. */
145 ntfs_3g_restore_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni,
146 struct wim_dentry *dentry, ntfs_volume *vol)
149 const char *dos_name;
150 size_t dos_name_nbytes;
152 /* Note: ntfs_set_ntfs_dos_name() closes both inodes (even if it fails).
153 * And it takes in a multibyte string, even though it translates it to
154 * UTF-16LE internally... which is annoying because we currently have
155 * the UTF-16LE string but not the multibyte string. */
157 ret = utf16le_get_tstr(dentry->d_short_name, dentry->d_short_name_nbytes,
158 &dos_name, &dos_name_nbytes);
163 dir_ni = ntfs_inode_open(vol, dentry->d_parent->d_inode->i_mft_no);
165 ni = ntfs_inode_open(vol, dentry->d_inode->i_mft_no);
167 ret = ntfs_set_ntfs_dos_name(ni, dir_ni,
168 dos_name, dos_name_nbytes, 0);
174 utf16le_put_tstr(dos_name);
177 ERROR_WITH_ERRNO("Failed to set DOS name of \"%s\" in NTFS "
178 "volume", dentry_full_path(dentry));
180 ERROR("This error may have been caused by a known "
181 "bug in libntfs-3g where it is unable to set "
182 "DOS names on files whose long names contain "
183 "unpaired surrogate characters. This bug "
184 "was fixed in NTFS-3G version 2017.3.23.");
188 dentry->d_name[dentry->d_name_nbytes / 2 - 1];
189 if (c == cpu_to_le16('.') || c == cpu_to_le16(' ')) {
190 ERROR("This error was probably caused by a "
191 "known bug in libntfs-3g where it is "
192 "unable to set DOS names on files whose "
193 "long names end with a dot or space "
194 "character. This bug was fixed in "
195 "NTFS-3G version 2017.3.23.");
198 ret = WIMLIB_ERR_SET_SHORT_NAME;
202 /* Unlike most other NTFS-3G functions, ntfs_set_ntfs_dos_name()
203 * changes the directory's last modification timestamp...
205 return ntfs_3g_restore_timestamps(vol, dentry->d_parent->d_inode);
208 /* ntfs_inode_close() can take a NULL argument, but it's probably best
209 * not to rely on this behavior. */
211 ntfs_inode_close(ni);
213 ntfs_inode_close(dir_ni);
218 ntfs_3g_restore_reparse_point(ntfs_inode *ni, const struct wim_inode *inode,
219 unsigned blob_size, struct ntfs_3g_apply_ctx *ctx)
221 complete_reparse_point(&ctx->rpbuf, inode, blob_size);
223 if (ntfs_set_ntfs_reparse_data(ni, (const char *)&ctx->rpbuf,
224 REPARSE_DATA_OFFSET + blob_size, 0))
227 ERROR_WITH_ERRNO("Failed to set reparse data on \"%s\"",
229 inode_first_extraction_dentry(inode)));
230 if (err == EINVAL && !(inode->i_reparse_tag & 0x80000000)) {
231 WARNING("This reparse point had a non-Microsoft reparse "
232 "tag. The preceding error may have been caused "
233 "by a known bug in libntfs-3g where it does not "
234 "correctly validate non-Microsoft reparse "
235 "points. This bug was fixed in NTFS-3G version "
238 return WIMLIB_ERR_SET_REPARSE_DATA;
245 ntfs_3g_has_empty_attributes(const struct wim_inode *inode)
247 for (unsigned i = 0; i < inode->i_num_streams; i++) {
248 const struct wim_inode_stream *strm = &inode->i_streams[i];
250 if (stream_blob_resolved(strm) == NULL &&
251 (strm->stream_type == STREAM_TYPE_REPARSE_POINT ||
252 stream_is_named_data_stream(strm)))
259 * Create empty attributes (named data streams and potentially a reparse point)
260 * for the specified file, if there are any.
262 * Since these won't have blob descriptors, they won't show up in the call to
263 * extract_blob_list(). Hence the need for the special case.
265 * Keep this in sync with ntfs_3g_has_empty_attributes()!
268 ntfs_3g_create_empty_attributes(ntfs_inode *ni,
269 const struct wim_inode *inode,
270 struct ntfs_3g_apply_ctx *ctx)
272 for (unsigned i = 0; i < inode->i_num_streams; i++) {
274 const struct wim_inode_stream *strm = &inode->i_streams[i];
277 if (stream_blob_resolved(strm) != NULL)
280 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT) {
281 ret = ntfs_3g_restore_reparse_point(ni, inode, 0, ctx);
284 } else if (stream_is_named_data_stream(strm)) {
285 if (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
286 utf16le_len_chars(strm->stream_name),
289 ERROR_WITH_ERRNO("Failed to create named data "
292 inode_first_extraction_dentry(inode)));
293 return WIMLIB_ERR_NTFS_3G;
300 /* Set attributes, security descriptor, and timestamps on the NTFS inode @ni.
303 ntfs_3g_set_metadata(ntfs_inode *ni, const struct wim_inode *inode,
304 const struct ntfs_3g_apply_ctx *ctx)
307 const struct wim_security_data *sd;
308 struct wim_dentry *one_dentry;
311 extract_flags = ctx->common.extract_flags;
312 sd = wim_get_current_security_data(ctx->common.wim);
313 one_dentry = inode_first_extraction_dentry(inode);
318 const void *object_id = inode_get_object_id(inode, &len);
319 if (unlikely(object_id != NULL) &&
320 ntfs_set_ntfs_object_id(ni, object_id, len, 0))
322 if (errno == EEXIST) {
323 WARNING("Duplicate object ID on file \"%s\"",
324 dentry_full_path(one_dentry));
326 ERROR_WITH_ERRNO("Failed to set object ID on "
327 "\"%s\" in NTFS volume",
328 dentry_full_path(one_dentry));
329 return WIMLIB_ERR_NTFS_3G;
335 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
336 u32 attrib = inode->i_attributes;
338 if (ntfs_set_ntfs_attrib(ni, (const char *)&attrib,
341 ERROR_WITH_ERRNO("Failed to set attributes on \"%s\" "
343 dentry_full_path(one_dentry));
344 return WIMLIB_ERR_SET_ATTRIBUTES;
348 /* Security descriptor */
349 if (inode_has_security_descriptor(inode)
350 && !(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
352 struct SECURITY_CONTEXT sec_ctx = { ctx->vol };
356 desc = sd->descriptors[inode->i_security_id];
357 desc_size = sd->sizes[inode->i_security_id];
359 ret = ntfs_set_ntfs_acl(&sec_ctx, ni, desc, desc_size, 0);
363 ERROR_WITH_ERRNO("Failed to set security descriptor on "
364 "\"%s\" in NTFS volume",
365 dentry_full_path(one_dentry));
366 if (err == EINVAL && wimlib_print_errors) {
367 fprintf(wimlib_error_file,
368 "The security descriptor is: ");
369 print_byte_field(desc, desc_size, wimlib_error_file);
370 fprintf(wimlib_error_file,
371 "\n\nThis error occurred because libntfs-3g thinks "
372 "the security descriptor is invalid. There "
373 "are several known bugs with libntfs-3g's "
374 "security descriptor validation logic in older "
375 "versions. Please upgrade to NTFS-3G version "
376 "2016.2.22 or later if you haven't already.\n");
378 return WIMLIB_ERR_SET_SECURITY;
383 ret = ntfs_3g_set_timestamps(ni, inode);
385 ERROR_WITH_ERRNO("Failed to set timestamps on \"%s\" "
387 dentry_full_path(one_dentry));
393 /* Recursively creates all the subdirectories of @dir, which has been created as
394 * the NTFS inode @dir_ni. */
396 ntfs_3g_create_dirs_recursive(ntfs_inode *dir_ni, struct wim_dentry *dir,
397 struct ntfs_3g_apply_ctx *ctx)
399 struct wim_dentry *child;
401 for_dentry_child(child, dir) {
405 if (!(child->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
407 if (!will_extract_dentry(child))
410 ni = ntfs_create(dir_ni, 0, child->d_extraction_name,
411 child->d_extraction_name_nchars, S_IFDIR);
413 ERROR_WITH_ERRNO("Error creating \"%s\" in NTFS volume",
414 dentry_full_path(child));
415 return WIMLIB_ERR_NTFS_3G;
418 child->d_inode->i_mft_no = ni->mft_no;
420 ret = report_file_created(&ctx->common);
422 ret = ntfs_3g_set_metadata(ni, child->d_inode, ctx);
424 ret = ntfs_3g_create_dirs_recursive(ni, child, ctx);
426 if (ntfs_inode_close_in_dir(ni, dir_ni) && !ret) {
427 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
428 dentry_full_path(child));
429 ret = WIMLIB_ERR_NTFS_3G;
437 /* For each WIM dentry in the @root tree that represents a directory, create the
438 * corresponding directory in the NTFS volume @ctx->vol. */
440 ntfs_3g_create_directories(struct wim_dentry *root,
441 struct list_head *dentry_list,
442 struct ntfs_3g_apply_ctx *ctx)
446 struct wim_dentry *dentry;
448 /* Create the directories using POSIX names. */
450 root_ni = ntfs_inode_open(ctx->vol, FILE_root);
452 ERROR_WITH_ERRNO("Can't open root of NTFS volume");
453 return WIMLIB_ERR_NTFS_3G;
456 root->d_inode->i_mft_no = FILE_root;
458 ret = ntfs_3g_set_metadata(root_ni, root->d_inode, ctx);
460 ret = ntfs_3g_create_dirs_recursive(root_ni, root, ctx);
462 if (ntfs_inode_close(root_ni) && !ret) {
463 ERROR_WITH_ERRNO("Error closing root of NTFS volume");
464 ret = WIMLIB_ERR_NTFS_3G;
469 /* Set the DOS name of any directory that has one. In addition, create
470 * empty attributes for directories that have them. Note that creating
471 * an empty reparse point attribute must happen *after* setting the DOS
472 * name in order to work around a case where ntfs_set_ntfs_dos_name()
473 * fails with EOPNOTSUPP. This bug was fixed in NTFS-3G version
475 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
476 const struct wim_inode *inode = dentry->d_inode;
478 if (!(inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
480 if (dentry_has_short_name(dentry)) {
481 ret = ntfs_3g_restore_dos_name(NULL, NULL, dentry,
485 ret = report_file_created(&ctx->common);
489 if (ntfs_3g_has_empty_attributes(inode)) {
492 ret = WIMLIB_ERR_NTFS_3G;
493 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
495 ret = ntfs_3g_create_empty_attributes(ni, inode,
497 if (ntfs_inode_close(ni) && !ret)
498 ret = WIMLIB_ERR_NTFS_3G;
501 ERROR_WITH_ERRNO("Failed to create empty "
502 "attributes of directory "
503 "\"%s\" in NTFS volume",
504 dentry_full_path(dentry));
512 /* When creating an inode that will have a short (DOS) name, we create it using
513 * the long name associated with the short name. This ensures that the short
514 * name gets associated with the correct long name. */
515 static struct wim_dentry *
516 ntfs_3g_first_extraction_alias(struct wim_inode *inode)
518 struct wim_dentry *dentry;
520 inode_for_each_extraction_alias(dentry, inode)
521 if (dentry_has_short_name(dentry))
523 return inode_first_extraction_dentry(inode);
527 * Add a hard link for the NTFS inode @ni at the location corresponding to the
528 * WIM dentry @dentry.
530 * The parent directory must have already been created on the NTFS volume.
532 * Returns 0 on success; returns WIMLIB_ERR_NTFS_3G and sets errno on failure.
535 ntfs_3g_add_link(ntfs_inode *ni, struct wim_dentry *dentry)
540 /* Open the inode of the parent directory. */
541 dir_ni = ntfs_inode_open(ni->vol, dentry->d_parent->d_inode->i_mft_no);
545 /* Create the link. */
546 res = ntfs_link(ni, dir_ni, dentry->d_extraction_name,
547 dentry->d_extraction_name_nchars);
549 /* Close the parent directory. */
550 if (ntfs_inode_close(dir_ni) || res)
556 ERROR_WITH_ERRNO("Can't create link \"%s\" in NTFS volume",
557 dentry_full_path(dentry));
558 return WIMLIB_ERR_NTFS_3G;
562 ntfs_3g_create_nondirectory(struct wim_inode *inode,
563 struct ntfs_3g_apply_ctx *ctx)
565 struct wim_dentry *first_dentry;
568 struct wim_dentry *dentry;
571 first_dentry = ntfs_3g_first_extraction_alias(inode);
573 /* Create first link. */
575 dir_ni = ntfs_inode_open(ctx->vol, first_dentry->d_parent->d_inode->i_mft_no);
577 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
578 dentry_full_path(first_dentry->d_parent));
579 return WIMLIB_ERR_NTFS_3G;
582 ni = ntfs_create(dir_ni, 0, first_dentry->d_extraction_name,
583 first_dentry->d_extraction_name_nchars, S_IFREG);
586 ERROR_WITH_ERRNO("Can't create \"%s\" in NTFS volume",
587 dentry_full_path(first_dentry));
588 ntfs_inode_close(dir_ni);
589 return WIMLIB_ERR_NTFS_3G;
592 inode->i_mft_no = ni->mft_no;
594 /* Set short name if present. */
595 if (dentry_has_short_name(first_dentry)) {
597 ret = ntfs_3g_restore_dos_name(ni, dir_ni, first_dentry, ctx->vol);
599 /* ntfs_3g_restore_dos_name() closed both 'ni' and 'dir_ni'. */
604 /* Reopen the inode. */
605 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
607 ERROR_WITH_ERRNO("Failed to reopen \"%s\" "
609 dentry_full_path(first_dentry));
610 return WIMLIB_ERR_NTFS_3G;
613 /* Close the directory in which the first link was created. */
614 if (ntfs_inode_close(dir_ni)) {
615 ERROR_WITH_ERRNO("Failed to close \"%s\" in NTFS volume",
616 dentry_full_path(first_dentry->d_parent));
617 ret = WIMLIB_ERR_NTFS_3G;
622 /* Create additional links if present. */
623 inode_for_each_extraction_alias(dentry, inode) {
624 if (dentry != first_dentry) {
625 ret = ntfs_3g_add_link(ni, dentry);
632 ret = ntfs_3g_set_metadata(ni, inode, ctx);
636 ret = ntfs_3g_create_empty_attributes(ni, inode, ctx);
639 /* Close the inode. */
640 if (ntfs_inode_close(ni) && !ret) {
641 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
642 dentry_full_path(first_dentry));
643 ret = WIMLIB_ERR_NTFS_3G;
648 /* For each WIM dentry in the @dentry_list that represents a nondirectory file,
649 * create the corresponding nondirectory file in the NTFS volume.
651 * Directories must have already been created. */
653 ntfs_3g_create_nondirectories(struct list_head *dentry_list,
654 struct ntfs_3g_apply_ctx *ctx)
656 struct wim_dentry *dentry;
657 struct wim_inode *inode;
660 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
661 inode = dentry->d_inode;
662 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
664 if (dentry == inode_first_extraction_dentry(inode)) {
665 ret = ntfs_3g_create_nondirectory(inode, ctx);
669 ret = report_file_created(&ctx->common);
677 ntfs_3g_begin_extract_blob_instance(struct blob_descriptor *blob,
679 struct wim_inode *inode,
680 const struct wim_inode_stream *strm,
681 struct ntfs_3g_apply_ctx *ctx)
683 struct wim_dentry *one_dentry = inode_first_extraction_dentry(inode);
684 ntfschar *stream_name;
685 size_t stream_name_nchars;
688 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
690 if (blob->size > REPARSE_DATA_MAX_SIZE) {
691 ERROR("Reparse data of \"%s\" has size "
692 "%"PRIu64" bytes (exceeds %u bytes)",
693 dentry_full_path(one_dentry),
694 blob->size, REPARSE_DATA_MAX_SIZE);
695 return WIMLIB_ERR_INVALID_REPARSE_DATA;
697 ctx->reparse_ptr = ctx->rpbuf.rpdata;
698 ctx->ntfs_reparse_inodes[ctx->num_reparse_inodes] = ni;
699 ctx->wim_reparse_inodes[ctx->num_reparse_inodes] = inode;
700 ctx->num_reparse_inodes++;
704 /* It's a data stream (may be unnamed or named). */
705 wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
707 if (unlikely(stream_is_named(strm))) {
708 stream_name = strm->stream_name;
709 stream_name_nchars = utf16le_len_chars(stream_name);
711 if (ntfs_attr_add(ni, AT_DATA, stream_name,
712 stream_name_nchars, NULL, 0))
714 ERROR_WITH_ERRNO("Failed to create named data stream of \"%s\"",
715 dentry_full_path(one_dentry));
716 return WIMLIB_ERR_NTFS_3G;
719 /* Don't pass an empty string other than AT_UNNAMED to
720 * ntfs_attr_open() --- it violates assumptions made by
722 stream_name = AT_UNNAMED;
723 stream_name_nchars = 0;
726 /* This should be ensured by extract_blob_list() */
727 wimlib_assert(ctx->num_open_attrs < MAX_OPEN_FILES);
729 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_nchars);
731 ERROR_WITH_ERRNO("Failed to open data stream of \"%s\"",
732 dentry_full_path(one_dentry));
733 return WIMLIB_ERR_NTFS_3G;
737 * Note: there are problems with trying to combine compression with
738 * sparseness when extracting. For example, doing ntfs_attr_truncate()
739 * at the end to extend the attribute to its final size actually extends
740 * to a compression block size boundary rather than to the requested
741 * size. Until these problems are solved, we always write the full data
742 * to compressed attributes. We also don't attempt to preallocate space
743 * for compressed attributes, since we don't know how much space they
744 * are going to actually need.
746 ctx->is_sparse_attr[ctx->num_open_attrs] = false;
747 if (!(na->data_flags & ATTR_COMPRESSION_MASK)) {
748 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE) {
749 ctx->is_sparse_attr[ctx->num_open_attrs] = true;
750 ctx->any_sparse_attrs = true;
752 ntfs_attr_truncate_solid(na, blob->size);
755 ctx->open_attrs[ctx->num_open_attrs++] = na;
760 ntfs_3g_cleanup_blob_extract(struct ntfs_3g_apply_ctx *ctx)
764 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
765 if (ntfs_attr_pclose(ctx->open_attrs[i]))
767 ntfs_attr_close(ctx->open_attrs[i]);
770 ctx->num_open_attrs = 0;
772 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
773 if (ntfs_inode_close(ctx->open_inodes[i]))
776 ctx->num_open_inodes = 0;
778 ctx->any_sparse_attrs = false;
779 ctx->reparse_ptr = NULL;
780 ctx->num_reparse_inodes = 0;
785 ntfs_3g_open_inode(struct wim_inode *inode, struct ntfs_3g_apply_ctx *ctx)
789 /* If the same blob is being extracted to multiple streams of the same
790 * inode, then we must only open the inode once. */
791 if (unlikely(inode->i_num_streams > 1)) {
792 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
793 if (ctx->open_inodes[i]->mft_no == inode->i_mft_no) {
794 return ctx->open_inodes[i];
799 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
801 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
803 inode_first_extraction_dentry(inode)));
807 ctx->open_inodes[ctx->num_open_inodes++] = ni;
812 ntfs_3g_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
814 struct ntfs_3g_apply_ctx *ctx = _ctx;
815 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
819 for (u32 i = 0; i < blob->out_refcnt; i++) {
820 ret = WIMLIB_ERR_NTFS_3G;
821 ni = ntfs_3g_open_inode(targets[i].inode, ctx);
825 ret = ntfs_3g_begin_extract_blob_instance(blob, ni,
827 targets[i].stream, ctx);
835 ntfs_3g_cleanup_blob_extract(ctx);
841 * Note: prior to NTFS-3G version 2016.2.22, ntfs_attr_pwrite() could return a
842 * short count in non-error cases, contrary to its documentation. Specifically,
843 * a short count could be returned when writing to a compressed attribute and
844 * the requested count exceeded the size of an NTFS "compression block".
845 * Therefore, we must continue calling ntfs_attr_pwrite() until all bytes have
846 * been written or a real error has occurred.
849 ntfs_3g_full_pwrite(ntfs_attr *na, u64 offset, size_t size, const u8 *data)
852 s64 res = ntfs_attr_pwrite(na, offset, size, data);
853 if (unlikely(res <= 0))
855 wimlib_assert(res <= size);
864 ntfs_3g_extract_chunk(const struct blob_descriptor *blob, u64 offset,
865 const void *chunk, size_t size, void *_ctx)
867 struct ntfs_3g_apply_ctx *ctx = _ctx;
868 const void * const end = chunk + size;
875 * For sparse attributes, only write nonzero regions. This lets the
876 * filesystem use holes to represent zero regions.
878 for (p = chunk; p != end; p += len, offset += len) {
879 zeroes = maybe_detect_sparse_region(p, end - p, &len,
880 ctx->any_sparse_attrs);
881 for (i = 0; i < ctx->num_open_attrs; i++) {
882 if (!zeroes || !ctx->is_sparse_attr[i]) {
883 if (!ntfs_3g_full_pwrite(ctx->open_attrs[i],
890 if (ctx->reparse_ptr)
891 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
895 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
896 return WIMLIB_ERR_NTFS_3G;
900 ntfs_3g_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
902 struct ntfs_3g_apply_ctx *ctx = _ctx;
910 /* Extend sparse attributes to their final size. */
911 if (ctx->any_sparse_attrs) {
912 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
913 if (!ctx->is_sparse_attr[i])
915 if (ntfs_attr_truncate(ctx->open_attrs[i], blob->size))
917 ERROR_WITH_ERRNO("Error extending attribute to "
919 ret = WIMLIB_ERR_WRITE;
925 for (u32 i = 0; i < ctx->num_reparse_inodes; i++) {
926 ret = ntfs_3g_restore_reparse_point(ctx->ntfs_reparse_inodes[i],
927 ctx->wim_reparse_inodes[i],
934 if (ntfs_3g_cleanup_blob_extract(ctx) && !ret) {
935 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
936 ret = WIMLIB_ERR_NTFS_3G;
942 ntfs_3g_count_dentries(const struct list_head *dentry_list)
944 const struct wim_dentry *dentry;
947 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
949 if ((dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) &&
950 dentry_has_short_name(dentry))
960 ntfs_3g_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
962 struct ntfs_3g_apply_ctx *ctx = (struct ntfs_3g_apply_ctx *)_ctx;
964 struct wim_dentry *root;
967 /* For NTFS-3G extraction mode we require that the dentries to extract
968 * form a single tree. */
969 root = list_first_entry(dentry_list, struct wim_dentry,
970 d_extraction_list_node);
972 /* Mount the NTFS volume. */
973 vol = ntfs_mount(ctx->common.target, 0);
975 ERROR_WITH_ERRNO("Failed to mount \"%s\" with NTFS-3G",
977 return WIMLIB_ERR_NTFS_3G;
981 /* Opening $Secure is required to set security descriptors in NTFS v3.0
982 * format, where security descriptors are stored in a per-volume index
983 * rather than being fully specified for each file. */
984 if (ntfs_open_secure(vol) && vol->major_ver >= 3) {
985 ERROR_WITH_ERRNO("Unable to open security descriptor index of "
986 "NTFS volume \"%s\"", ctx->common.target);
987 ret = WIMLIB_ERR_NTFS_3G;
991 /* Create all inodes and aliases, including short names, and set
992 * metadata (attributes, security descriptors, and timestamps). */
994 ret = start_file_structure_phase(&ctx->common,
995 ntfs_3g_count_dentries(dentry_list));
999 ret = ntfs_3g_create_directories(root, dentry_list, ctx);
1003 ret = ntfs_3g_create_nondirectories(dentry_list, ctx);
1007 ret = end_file_structure_phase(&ctx->common);
1011 /* Extract blobs. */
1012 struct read_blob_callbacks cbs = {
1013 .begin_blob = ntfs_3g_begin_extract_blob,
1014 .continue_blob = ntfs_3g_extract_chunk,
1015 .end_blob = ntfs_3g_end_extract_blob,
1018 ret = extract_blob_list(&ctx->common, &cbs);
1020 /* We do not need a final pass to set timestamps because libntfs-3g does
1021 * not update timestamps automatically (exception:
1022 * ntfs_set_ntfs_dos_name() does, but we handle this elsewhere). */
1025 if (vol->secure_ni) {
1026 ntfs_index_ctx_put(vol->secure_xsii);
1027 ntfs_index_ctx_put(vol->secure_xsdh);
1028 if (ntfs_inode_close(vol->secure_ni) && !ret) {
1029 ERROR_WITH_ERRNO("Failed to close security descriptor "
1030 "index of NTFS volume \"%s\"",
1031 ctx->common.target);
1032 ret = WIMLIB_ERR_NTFS_3G;
1034 vol->secure_ni = NULL;
1036 if (ntfs_umount(ctx->vol, FALSE) && !ret) {
1037 ERROR_WITH_ERRNO("Failed to unmount \"%s\" with NTFS-3G",
1038 ctx->common.target);
1039 ret = WIMLIB_ERR_NTFS_3G;
1044 const struct apply_operations ntfs_3g_apply_ops = {
1046 .get_supported_features = ntfs_3g_get_supported_features,
1047 .extract = ntfs_3g_extract,
1048 .context_size = sizeof(struct ntfs_3g_apply_ctx),
1049 .single_tree_only = true,