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-2016 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 http://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->named_data_streams = 1;
65 supported_features->hard_links = 1;
66 supported_features->reparse_points = 1;
67 supported_features->security_descriptors = 1;
68 supported_features->short_names = 1;
69 supported_features->object_ids = 1;
70 supported_features->timestamps = 1;
71 supported_features->case_sensitive_filenames = 1;
75 struct ntfs_3g_apply_ctx {
76 /* Extract flags, the pointer to the WIMStruct, etc. */
77 struct apply_ctx common;
79 /* Pointer to the open NTFS volume */
82 ntfs_attr *open_attrs[MAX_OPEN_FILES];
83 unsigned num_open_attrs;
84 ntfs_inode *open_inodes[MAX_OPEN_FILES];
85 unsigned num_open_inodes;
87 struct reparse_buffer_disk rpbuf;
90 /* Offset in the blob currently being read */
93 unsigned num_reparse_inodes;
94 ntfs_inode *ntfs_reparse_inodes[MAX_OPEN_FILES];
95 struct wim_inode *wim_reparse_inodes[MAX_OPEN_FILES];
99 ntfs_3g_set_timestamps(ntfs_inode *ni, const struct wim_inode *inode)
102 inode->i_creation_time,
103 inode->i_last_write_time,
104 inode->i_last_access_time,
107 if (ntfs_inode_set_times(ni, (const char *)times, sizeof(times), 0))
108 return WIMLIB_ERR_SET_TIMESTAMPS;
112 /* Restore the timestamps on the NTFS inode corresponding to @inode. */
114 ntfs_3g_restore_timestamps(ntfs_volume *vol, const struct wim_inode *inode)
119 ni = ntfs_inode_open(vol, inode->i_mft_no);
123 res = ntfs_3g_set_timestamps(ni, inode);
125 if (ntfs_inode_close(ni) || res)
131 ERROR_WITH_ERRNO("Failed to update timestamps of \"%s\" in NTFS volume",
132 dentry_full_path(inode_first_extraction_dentry(inode)));
133 return WIMLIB_ERR_SET_TIMESTAMPS;
136 /* Restore the DOS name of the @dentry.
137 * This closes both @ni and @dir_ni.
138 * If either is NULL, then they are opened temporarily. */
140 ntfs_3g_restore_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni,
141 struct wim_dentry *dentry, ntfs_volume *vol)
144 const char *dos_name;
145 size_t dos_name_nbytes;
147 /* Note: ntfs_set_ntfs_dos_name() closes both inodes (even if it fails).
148 * And it takes in a multibyte string, even though it translates it to
149 * UTF-16LE internally... which is annoying because we currently have
150 * the UTF-16LE string but not the multibyte string. */
152 ret = utf16le_get_tstr(dentry->d_short_name, dentry->d_short_name_nbytes,
153 &dos_name, &dos_name_nbytes);
158 dir_ni = ntfs_inode_open(vol, dentry->d_parent->d_inode->i_mft_no);
160 ni = ntfs_inode_open(vol, dentry->d_inode->i_mft_no);
162 ret = ntfs_set_ntfs_dos_name(ni, dir_ni,
163 dos_name, dos_name_nbytes, 0);
169 utf16le_put_tstr(dos_name);
171 ERROR_WITH_ERRNO("Failed to set DOS name of \"%s\" in NTFS "
172 "volume", dentry_full_path(dentry));
173 ret = WIMLIB_ERR_SET_SHORT_NAME;
177 /* Unlike most other NTFS-3G functions, ntfs_set_ntfs_dos_name()
178 * changes the directory's last modification timestamp...
180 return ntfs_3g_restore_timestamps(vol, dentry->d_parent->d_inode);
183 /* ntfs_inode_close() can take a NULL argument, but it's probably best
184 * not to rely on this behavior. */
186 ntfs_inode_close(ni);
188 ntfs_inode_close(dir_ni);
193 ntfs_3g_restore_reparse_point(ntfs_inode *ni, const struct wim_inode *inode,
194 unsigned blob_size, struct ntfs_3g_apply_ctx *ctx)
196 complete_reparse_point(&ctx->rpbuf, inode, blob_size);
198 if (ntfs_set_ntfs_reparse_data(ni, (const char *)&ctx->rpbuf,
199 REPARSE_DATA_OFFSET + blob_size, 0))
202 ERROR_WITH_ERRNO("Failed to set reparse data on \"%s\"",
204 inode_first_extraction_dentry(inode)));
205 if (err == EINVAL && !(inode->i_reparse_tag & 0x80000000)) {
206 WARNING("This reparse point had a non-Microsoft reparse "
207 "tag. The preceding error may have been caused "
208 "by a known bug in libntfs-3g where it does not "
209 "correctly validate non-Microsoft reparse "
210 "points. This bug was fixed in NTFS-3G version "
213 return WIMLIB_ERR_SET_REPARSE_DATA;
220 ntfs_3g_has_empty_attributes(const struct wim_inode *inode)
222 for (unsigned i = 0; i < inode->i_num_streams; i++) {
223 const struct wim_inode_stream *strm = &inode->i_streams[i];
225 if (stream_blob_resolved(strm) == NULL &&
226 (strm->stream_type == STREAM_TYPE_REPARSE_POINT ||
227 stream_is_named_data_stream(strm)))
234 * Create empty attributes (named data streams and potentially a reparse point)
235 * for the specified file, if there are any.
237 * Since these won't have blob descriptors, they won't show up in the call to
238 * extract_blob_list(). Hence the need for the special case.
240 * Keep this in sync with ntfs_3g_has_empty_attributes()!
243 ntfs_3g_create_empty_attributes(ntfs_inode *ni,
244 const struct wim_inode *inode,
245 struct ntfs_3g_apply_ctx *ctx)
247 for (unsigned i = 0; i < inode->i_num_streams; i++) {
249 const struct wim_inode_stream *strm = &inode->i_streams[i];
252 if (stream_blob_resolved(strm) != NULL)
255 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT) {
256 ret = ntfs_3g_restore_reparse_point(ni, inode, 0, ctx);
259 } else if (stream_is_named_data_stream(strm)) {
260 if (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
261 utf16le_len_chars(strm->stream_name),
264 ERROR_WITH_ERRNO("Failed to create named data "
267 inode_first_extraction_dentry(inode)));
268 return WIMLIB_ERR_NTFS_3G;
275 /* Set attributes, security descriptor, and timestamps on the NTFS inode @ni.
278 ntfs_3g_set_metadata(ntfs_inode *ni, const struct wim_inode *inode,
279 const struct ntfs_3g_apply_ctx *ctx)
282 const struct wim_security_data *sd;
283 struct wim_dentry *one_dentry;
286 extract_flags = ctx->common.extract_flags;
287 sd = wim_get_current_security_data(ctx->common.wim);
288 one_dentry = inode_first_extraction_dentry(inode);
293 const void *object_id = inode_get_object_id(inode, &len);
294 if (unlikely(object_id != NULL) &&
295 ntfs_set_ntfs_object_id(ni, object_id, len, 0))
297 if (errno == EEXIST) {
298 WARNING("Duplicate object ID on file \"%s\"",
299 dentry_full_path(one_dentry));
301 ERROR_WITH_ERRNO("Failed to set object ID on "
302 "\"%s\" in NTFS volume",
303 dentry_full_path(one_dentry));
304 return WIMLIB_ERR_NTFS_3G;
310 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
311 u32 attrib = inode->i_attributes;
313 attrib &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
314 FILE_ATTRIBUTE_ENCRYPTED);
316 if (ntfs_set_ntfs_attrib(ni, (const char *)&attrib,
319 ERROR_WITH_ERRNO("Failed to set attributes on \"%s\" "
321 dentry_full_path(one_dentry));
322 return WIMLIB_ERR_SET_ATTRIBUTES;
326 /* Security descriptor */
327 if (inode_has_security_descriptor(inode)
328 && !(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
330 struct SECURITY_CONTEXT sec_ctx = { ctx->vol };
334 desc = sd->descriptors[inode->i_security_id];
335 desc_size = sd->sizes[inode->i_security_id];
337 ret = ntfs_set_ntfs_acl(&sec_ctx, ni, desc, desc_size, 0);
341 ERROR_WITH_ERRNO("Failed to set security descriptor on "
342 "\"%s\" in NTFS volume",
343 dentry_full_path(one_dentry));
344 if (err == EINVAL && wimlib_print_errors) {
345 fprintf(wimlib_error_file,
346 "The security descriptor is: ");
347 print_byte_field(desc, desc_size, wimlib_error_file);
348 fprintf(wimlib_error_file,
349 "\n\nThis error occurred because libntfs-3g thinks "
350 "the security descriptor is invalid. There "
351 "are several known bugs with libntfs-3g's "
352 "security descriptor validation logic in older "
353 "versions. Please upgrade to NTFS-3G version "
354 "2016.2.22 or later if you haven't already.\n");
356 return WIMLIB_ERR_SET_SECURITY;
361 ret = ntfs_3g_set_timestamps(ni, inode);
363 ERROR_WITH_ERRNO("Failed to set timestamps on \"%s\" "
365 dentry_full_path(one_dentry));
371 /* Recursively creates all the subdirectories of @dir, which has been created as
372 * the NTFS inode @dir_ni. */
374 ntfs_3g_create_dirs_recursive(ntfs_inode *dir_ni, struct wim_dentry *dir,
375 struct ntfs_3g_apply_ctx *ctx)
377 struct wim_dentry *child;
379 for_dentry_child(child, dir) {
383 if (!(child->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
385 if (!will_extract_dentry(child))
388 ni = ntfs_create(dir_ni, 0, child->d_extraction_name,
389 child->d_extraction_name_nchars, S_IFDIR);
391 ERROR_WITH_ERRNO("Error creating \"%s\" in NTFS volume",
392 dentry_full_path(child));
393 return WIMLIB_ERR_NTFS_3G;
396 child->d_inode->i_mft_no = ni->mft_no;
398 ret = report_file_created(&ctx->common);
400 ret = ntfs_3g_set_metadata(ni, child->d_inode, ctx);
402 ret = ntfs_3g_create_dirs_recursive(ni, child, ctx);
404 if (ntfs_inode_close_in_dir(ni, dir_ni) && !ret) {
405 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
406 dentry_full_path(child));
407 ret = WIMLIB_ERR_NTFS_3G;
415 /* For each WIM dentry in the @root tree that represents a directory, create the
416 * corresponding directory in the NTFS volume @ctx->vol. */
418 ntfs_3g_create_directories(struct wim_dentry *root,
419 struct list_head *dentry_list,
420 struct ntfs_3g_apply_ctx *ctx)
424 struct wim_dentry *dentry;
426 /* Create the directories using POSIX names. */
428 root_ni = ntfs_inode_open(ctx->vol, FILE_root);
430 ERROR_WITH_ERRNO("Can't open root of NTFS volume");
431 return WIMLIB_ERR_NTFS_3G;
434 root->d_inode->i_mft_no = FILE_root;
436 ret = ntfs_3g_set_metadata(root_ni, root->d_inode, ctx);
438 ret = ntfs_3g_create_dirs_recursive(root_ni, root, ctx);
440 if (ntfs_inode_close(root_ni) && !ret) {
441 ERROR_WITH_ERRNO("Error closing root of NTFS volume");
442 ret = WIMLIB_ERR_NTFS_3G;
447 /* Set the DOS name of any directory that has one. In addition, create
448 * empty attributes for directories that have them. Note that creating
449 * an empty reparse point attribute must happen *after* setting the DOS
450 * name in order to work around a case where ntfs_set_ntfs_dos_name()
451 * fails with EOPNOTSUPP. This bug was fixed in NTFS-3G version
453 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
454 const struct wim_inode *inode = dentry->d_inode;
456 if (!(inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
458 if (dentry_has_short_name(dentry)) {
459 ret = ntfs_3g_restore_dos_name(NULL, NULL, dentry,
463 ret = report_file_created(&ctx->common);
467 if (ntfs_3g_has_empty_attributes(inode)) {
470 ret = WIMLIB_ERR_NTFS_3G;
471 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
473 ret = ntfs_3g_create_empty_attributes(ni, inode,
475 if (ntfs_inode_close(ni) && !ret)
476 ret = WIMLIB_ERR_NTFS_3G;
479 ERROR_WITH_ERRNO("Failed to create empty "
480 "attributes of directory "
481 "\"%s\" in NTFS volume",
482 dentry_full_path(dentry));
490 /* When creating an inode that will have a short (DOS) name, we create it using
491 * the long name associated with the short name. This ensures that the short
492 * name gets associated with the correct long name. */
493 static struct wim_dentry *
494 ntfs_3g_first_extraction_alias(struct wim_inode *inode)
496 struct wim_dentry *dentry;
498 inode_for_each_extraction_alias(dentry, inode)
499 if (dentry_has_short_name(dentry))
501 return inode_first_extraction_dentry(inode);
505 * Add a hard link for the NTFS inode @ni at the location corresponding to the
506 * WIM dentry @dentry.
508 * The parent directory must have already been created on the NTFS volume.
510 * Returns 0 on success; returns WIMLIB_ERR_NTFS_3G and sets errno on failure.
513 ntfs_3g_add_link(ntfs_inode *ni, struct wim_dentry *dentry)
518 /* Open the inode of the parent directory. */
519 dir_ni = ntfs_inode_open(ni->vol, dentry->d_parent->d_inode->i_mft_no);
523 /* Create the link. */
524 res = ntfs_link(ni, dir_ni, dentry->d_extraction_name,
525 dentry->d_extraction_name_nchars);
527 /* Close the parent directory. */
528 if (ntfs_inode_close(dir_ni) || res)
534 ERROR_WITH_ERRNO("Can't create link \"%s\" in NTFS volume",
535 dentry_full_path(dentry));
536 return WIMLIB_ERR_NTFS_3G;
540 ntfs_3g_create_nondirectory(struct wim_inode *inode,
541 struct ntfs_3g_apply_ctx *ctx)
543 struct wim_dentry *first_dentry;
546 struct wim_dentry *dentry;
549 first_dentry = ntfs_3g_first_extraction_alias(inode);
551 /* Create first link. */
553 dir_ni = ntfs_inode_open(ctx->vol, first_dentry->d_parent->d_inode->i_mft_no);
555 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
556 dentry_full_path(first_dentry->d_parent));
557 return WIMLIB_ERR_NTFS_3G;
560 ni = ntfs_create(dir_ni, 0, first_dentry->d_extraction_name,
561 first_dentry->d_extraction_name_nchars, S_IFREG);
564 ERROR_WITH_ERRNO("Can't create \"%s\" in NTFS volume",
565 dentry_full_path(first_dentry));
566 ntfs_inode_close(dir_ni);
567 return WIMLIB_ERR_NTFS_3G;
570 inode->i_mft_no = ni->mft_no;
572 /* Set short name if present. */
573 if (dentry_has_short_name(first_dentry)) {
575 ret = ntfs_3g_restore_dos_name(ni, dir_ni, first_dentry, ctx->vol);
577 /* ntfs_3g_restore_dos_name() closed both 'ni' and 'dir_ni'. */
582 /* Reopen the inode. */
583 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
585 ERROR_WITH_ERRNO("Failed to reopen \"%s\" "
587 dentry_full_path(first_dentry));
588 return WIMLIB_ERR_NTFS_3G;
591 /* Close the directory in which the first link was created. */
592 if (ntfs_inode_close(dir_ni)) {
593 ERROR_WITH_ERRNO("Failed to close \"%s\" in NTFS volume",
594 dentry_full_path(first_dentry->d_parent));
595 ret = WIMLIB_ERR_NTFS_3G;
600 /* Create additional links if present. */
601 inode_for_each_extraction_alias(dentry, inode) {
602 if (dentry != first_dentry) {
603 ret = ntfs_3g_add_link(ni, dentry);
610 ret = ntfs_3g_set_metadata(ni, inode, ctx);
614 ret = ntfs_3g_create_empty_attributes(ni, inode, ctx);
617 /* Close the inode. */
618 if (ntfs_inode_close(ni) && !ret) {
619 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
620 dentry_full_path(first_dentry));
621 ret = WIMLIB_ERR_NTFS_3G;
626 /* For each WIM dentry in the @dentry_list that represents a nondirectory file,
627 * create the corresponding nondirectory file in the NTFS volume.
629 * Directories must have already been created. */
631 ntfs_3g_create_nondirectories(struct list_head *dentry_list,
632 struct ntfs_3g_apply_ctx *ctx)
634 struct wim_dentry *dentry;
635 struct wim_inode *inode;
638 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
639 inode = dentry->d_inode;
640 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
642 if (dentry == inode_first_extraction_dentry(inode)) {
643 ret = ntfs_3g_create_nondirectory(inode, ctx);
647 ret = report_file_created(&ctx->common);
655 ntfs_3g_begin_extract_blob_instance(struct blob_descriptor *blob,
657 struct wim_inode *inode,
658 const struct wim_inode_stream *strm,
659 struct ntfs_3g_apply_ctx *ctx)
661 struct wim_dentry *one_dentry = inode_first_extraction_dentry(inode);
662 ntfschar *stream_name;
663 size_t stream_name_nchars;
666 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
668 if (blob->size > REPARSE_DATA_MAX_SIZE) {
669 ERROR("Reparse data of \"%s\" has size "
670 "%"PRIu64" bytes (exceeds %u bytes)",
671 dentry_full_path(one_dentry),
672 blob->size, REPARSE_DATA_MAX_SIZE);
673 return WIMLIB_ERR_INVALID_REPARSE_DATA;
675 ctx->reparse_ptr = ctx->rpbuf.rpdata;
676 ctx->ntfs_reparse_inodes[ctx->num_reparse_inodes] = ni;
677 ctx->wim_reparse_inodes[ctx->num_reparse_inodes] = inode;
678 ctx->num_reparse_inodes++;
682 /* It's a data stream (may be unnamed or named). */
683 wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
685 if (unlikely(stream_is_named(strm))) {
686 stream_name = strm->stream_name;
687 stream_name_nchars = utf16le_len_chars(stream_name);
689 if (ntfs_attr_add(ni, AT_DATA, stream_name,
690 stream_name_nchars, NULL, 0))
692 ERROR_WITH_ERRNO("Failed to create named data stream of \"%s\"",
693 dentry_full_path(one_dentry));
694 return WIMLIB_ERR_NTFS_3G;
697 /* Don't pass an empty string other than AT_UNNAMED to
698 * ntfs_attr_open() --- it violates assumptions made by
700 stream_name = AT_UNNAMED;
701 stream_name_nchars = 0;
704 /* This should be ensured by extract_blob_list() */
705 wimlib_assert(ctx->num_open_attrs < MAX_OPEN_FILES);
707 attr = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_nchars);
709 ERROR_WITH_ERRNO("Failed to open data stream of \"%s\"",
710 dentry_full_path(one_dentry));
711 return WIMLIB_ERR_NTFS_3G;
713 ctx->open_attrs[ctx->num_open_attrs++] = attr;
714 ntfs_attr_truncate_solid(attr, blob->size);
719 ntfs_3g_cleanup_blob_extract(struct ntfs_3g_apply_ctx *ctx)
723 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
724 if (ntfs_attr_pclose(ctx->open_attrs[i]))
726 ntfs_attr_close(ctx->open_attrs[i]);
729 ctx->num_open_attrs = 0;
731 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
732 if (ntfs_inode_close(ctx->open_inodes[i]))
735 ctx->num_open_inodes = 0;
738 ctx->reparse_ptr = NULL;
739 ctx->num_reparse_inodes = 0;
744 ntfs_3g_open_inode(struct wim_inode *inode, struct ntfs_3g_apply_ctx *ctx)
748 /* If the same blob is being extracted to multiple streams of the same
749 * inode, then we must only open the inode once. */
750 if (unlikely(inode->i_num_streams > 1)) {
751 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
752 if (ctx->open_inodes[i]->mft_no == inode->i_mft_no) {
753 return ctx->open_inodes[i];
758 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
760 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
762 inode_first_extraction_dentry(inode)));
766 ctx->open_inodes[ctx->num_open_inodes++] = ni;
771 ntfs_3g_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
773 struct ntfs_3g_apply_ctx *ctx = _ctx;
774 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
778 for (u32 i = 0; i < blob->out_refcnt; i++) {
779 ret = WIMLIB_ERR_NTFS_3G;
780 ni = ntfs_3g_open_inode(targets[i].inode, ctx);
784 ret = ntfs_3g_begin_extract_blob_instance(blob, ni,
786 targets[i].stream, ctx);
794 ntfs_3g_cleanup_blob_extract(ctx);
799 /* Note: contrary to its documentation, ntfs_attr_pwrite() can return a short
800 * count in non-error cases --- specifically, when writing to a compressed
801 * attribute and the requested count exceeds the size of an NTFS "compression
802 * block". Therefore, we must continue calling ntfs_attr_pwrite() until all
803 * bytes have been written or a real error has occurred. */
805 ntfs_3g_full_pwrite(ntfs_attr *na, u64 offset, size_t size, const u8 *data)
808 s64 res = ntfs_attr_pwrite(na, offset, size, data);
809 if (unlikely(res <= 0))
811 wimlib_assert(res <= size);
820 ntfs_3g_extract_chunk(const void *chunk, size_t size, void *_ctx)
822 struct ntfs_3g_apply_ctx *ctx = _ctx;
824 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
825 if (!ntfs_3g_full_pwrite(ctx->open_attrs[i],
826 ctx->offset, size, chunk))
828 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
829 return WIMLIB_ERR_NTFS_3G;
832 if (ctx->reparse_ptr)
833 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
839 ntfs_3g_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
841 struct ntfs_3g_apply_ctx *ctx = _ctx;
849 for (u32 i = 0; i < ctx->num_reparse_inodes; i++) {
850 ret = ntfs_3g_restore_reparse_point(ctx->ntfs_reparse_inodes[i],
851 ctx->wim_reparse_inodes[i],
858 if (ntfs_3g_cleanup_blob_extract(ctx) && !ret) {
859 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
860 ret = WIMLIB_ERR_NTFS_3G;
866 ntfs_3g_count_dentries(const struct list_head *dentry_list)
868 const struct wim_dentry *dentry;
871 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
873 if ((dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) &&
874 dentry_has_short_name(dentry))
884 ntfs_3g_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
886 struct ntfs_3g_apply_ctx *ctx = (struct ntfs_3g_apply_ctx *)_ctx;
888 struct wim_dentry *root;
891 /* For NTFS-3G extraction mode we require that the dentries to extract
892 * form a single tree. */
893 root = list_first_entry(dentry_list, struct wim_dentry,
894 d_extraction_list_node);
896 /* Mount the NTFS volume. */
897 vol = ntfs_mount(ctx->common.target, 0);
899 ERROR_WITH_ERRNO("Failed to mount \"%s\" with NTFS-3G",
901 return WIMLIB_ERR_NTFS_3G;
905 /* Create all inodes and aliases, including short names, and set
906 * metadata (attributes, security descriptors, and timestamps). */
908 ret = start_file_structure_phase(&ctx->common,
909 ntfs_3g_count_dentries(dentry_list));
913 ret = ntfs_3g_create_directories(root, dentry_list, ctx);
917 ret = ntfs_3g_create_nondirectories(dentry_list, ctx);
921 ret = end_file_structure_phase(&ctx->common);
926 struct read_blob_callbacks cbs = {
927 .begin_blob = ntfs_3g_begin_extract_blob,
928 .consume_chunk = ntfs_3g_extract_chunk,
929 .end_blob = ntfs_3g_end_extract_blob,
932 ret = extract_blob_list(&ctx->common, &cbs);
934 /* We do not need a final pass to set timestamps because libntfs-3g does
935 * not update timestamps automatically (exception:
936 * ntfs_set_ntfs_dos_name() does, but we handle this elsewhere). */
939 if (ntfs_umount(ctx->vol, FALSE) && !ret) {
940 ERROR_WITH_ERRNO("Failed to unmount \"%s\" with NTFS-3G",
942 ret = WIMLIB_ERR_NTFS_3G;
947 const struct apply_operations ntfs_3g_apply_ops = {
949 .get_supported_features = ntfs_3g_get_supported_features,
950 .extract = ntfs_3g_extract,
951 .context_size = sizeof(struct ntfs_3g_apply_ctx),
952 .single_tree_only = true,