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->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 the development version of "
185 "NTFS-3G in June 2016.");
189 dentry->d_name[dentry->d_name_nbytes / 2 - 1];
190 if (c == cpu_to_le16('.') || c == cpu_to_le16(' ')) {
191 ERROR("This error was probably caused by a "
192 "known bug in libntfs-3g where it is "
193 "unable to set DOS names on files whose "
194 "long names end with a dot or space "
196 "https://wimlib.net/forums/viewtopic.php?f=1&t=294 "
197 "for more information.");
200 ret = WIMLIB_ERR_SET_SHORT_NAME;
204 /* Unlike most other NTFS-3G functions, ntfs_set_ntfs_dos_name()
205 * changes the directory's last modification timestamp...
207 return ntfs_3g_restore_timestamps(vol, dentry->d_parent->d_inode);
210 /* ntfs_inode_close() can take a NULL argument, but it's probably best
211 * not to rely on this behavior. */
213 ntfs_inode_close(ni);
215 ntfs_inode_close(dir_ni);
220 ntfs_3g_restore_reparse_point(ntfs_inode *ni, const struct wim_inode *inode,
221 unsigned blob_size, struct ntfs_3g_apply_ctx *ctx)
223 complete_reparse_point(&ctx->rpbuf, inode, blob_size);
225 if (ntfs_set_ntfs_reparse_data(ni, (const char *)&ctx->rpbuf,
226 REPARSE_DATA_OFFSET + blob_size, 0))
229 ERROR_WITH_ERRNO("Failed to set reparse data on \"%s\"",
231 inode_first_extraction_dentry(inode)));
232 if (err == EINVAL && !(inode->i_reparse_tag & 0x80000000)) {
233 WARNING("This reparse point had a non-Microsoft reparse "
234 "tag. The preceding error may have been caused "
235 "by a known bug in libntfs-3g where it does not "
236 "correctly validate non-Microsoft reparse "
237 "points. This bug was fixed in NTFS-3G version "
240 return WIMLIB_ERR_SET_REPARSE_DATA;
247 ntfs_3g_has_empty_attributes(const struct wim_inode *inode)
249 for (unsigned i = 0; i < inode->i_num_streams; i++) {
250 const struct wim_inode_stream *strm = &inode->i_streams[i];
252 if (stream_blob_resolved(strm) == NULL &&
253 (strm->stream_type == STREAM_TYPE_REPARSE_POINT ||
254 stream_is_named_data_stream(strm)))
261 * Create empty attributes (named data streams and potentially a reparse point)
262 * for the specified file, if there are any.
264 * Since these won't have blob descriptors, they won't show up in the call to
265 * extract_blob_list(). Hence the need for the special case.
267 * Keep this in sync with ntfs_3g_has_empty_attributes()!
270 ntfs_3g_create_empty_attributes(ntfs_inode *ni,
271 const struct wim_inode *inode,
272 struct ntfs_3g_apply_ctx *ctx)
274 for (unsigned i = 0; i < inode->i_num_streams; i++) {
276 const struct wim_inode_stream *strm = &inode->i_streams[i];
279 if (stream_blob_resolved(strm) != NULL)
282 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT) {
283 ret = ntfs_3g_restore_reparse_point(ni, inode, 0, ctx);
286 } else if (stream_is_named_data_stream(strm)) {
287 if (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
288 utf16le_len_chars(strm->stream_name),
291 ERROR_WITH_ERRNO("Failed to create named data "
294 inode_first_extraction_dentry(inode)));
295 return WIMLIB_ERR_NTFS_3G;
302 /* Set attributes, security descriptor, and timestamps on the NTFS inode @ni.
305 ntfs_3g_set_metadata(ntfs_inode *ni, const struct wim_inode *inode,
306 const struct ntfs_3g_apply_ctx *ctx)
309 const struct wim_security_data *sd;
310 struct wim_dentry *one_dentry;
313 extract_flags = ctx->common.extract_flags;
314 sd = wim_get_current_security_data(ctx->common.wim);
315 one_dentry = inode_first_extraction_dentry(inode);
320 const void *object_id = inode_get_object_id(inode, &len);
321 if (unlikely(object_id != NULL) &&
322 ntfs_set_ntfs_object_id(ni, object_id, len, 0))
324 if (errno == EEXIST) {
325 WARNING("Duplicate object ID on file \"%s\"",
326 dentry_full_path(one_dentry));
328 ERROR_WITH_ERRNO("Failed to set object ID on "
329 "\"%s\" in NTFS volume",
330 dentry_full_path(one_dentry));
331 return WIMLIB_ERR_NTFS_3G;
337 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
338 u32 attrib = inode->i_attributes;
340 if (ntfs_set_ntfs_attrib(ni, (const char *)&attrib,
343 ERROR_WITH_ERRNO("Failed to set attributes on \"%s\" "
345 dentry_full_path(one_dentry));
346 return WIMLIB_ERR_SET_ATTRIBUTES;
350 /* Security descriptor */
351 if (inode_has_security_descriptor(inode)
352 && !(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
354 struct SECURITY_CONTEXT sec_ctx = { ctx->vol };
358 desc = sd->descriptors[inode->i_security_id];
359 desc_size = sd->sizes[inode->i_security_id];
361 ret = ntfs_set_ntfs_acl(&sec_ctx, ni, desc, desc_size, 0);
365 ERROR_WITH_ERRNO("Failed to set security descriptor on "
366 "\"%s\" in NTFS volume",
367 dentry_full_path(one_dentry));
368 if (err == EINVAL && wimlib_print_errors) {
369 fprintf(wimlib_error_file,
370 "The security descriptor is: ");
371 print_byte_field(desc, desc_size, wimlib_error_file);
372 fprintf(wimlib_error_file,
373 "\n\nThis error occurred because libntfs-3g thinks "
374 "the security descriptor is invalid. There "
375 "are several known bugs with libntfs-3g's "
376 "security descriptor validation logic in older "
377 "versions. Please upgrade to NTFS-3G version "
378 "2016.2.22 or later if you haven't already.\n");
380 return WIMLIB_ERR_SET_SECURITY;
385 ret = ntfs_3g_set_timestamps(ni, inode);
387 ERROR_WITH_ERRNO("Failed to set timestamps on \"%s\" "
389 dentry_full_path(one_dentry));
395 /* Recursively creates all the subdirectories of @dir, which has been created as
396 * the NTFS inode @dir_ni. */
398 ntfs_3g_create_dirs_recursive(ntfs_inode *dir_ni, struct wim_dentry *dir,
399 struct ntfs_3g_apply_ctx *ctx)
401 struct wim_dentry *child;
403 for_dentry_child(child, dir) {
407 if (!(child->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
409 if (!will_extract_dentry(child))
412 ni = ntfs_create(dir_ni, 0, child->d_extraction_name,
413 child->d_extraction_name_nchars, S_IFDIR);
415 ERROR_WITH_ERRNO("Error creating \"%s\" in NTFS volume",
416 dentry_full_path(child));
417 return WIMLIB_ERR_NTFS_3G;
420 child->d_inode->i_mft_no = ni->mft_no;
422 ret = report_file_created(&ctx->common);
424 ret = ntfs_3g_set_metadata(ni, child->d_inode, ctx);
426 ret = ntfs_3g_create_dirs_recursive(ni, child, ctx);
428 if (ntfs_inode_close_in_dir(ni, dir_ni) && !ret) {
429 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
430 dentry_full_path(child));
431 ret = WIMLIB_ERR_NTFS_3G;
439 /* For each WIM dentry in the @root tree that represents a directory, create the
440 * corresponding directory in the NTFS volume @ctx->vol. */
442 ntfs_3g_create_directories(struct wim_dentry *root,
443 struct list_head *dentry_list,
444 struct ntfs_3g_apply_ctx *ctx)
448 struct wim_dentry *dentry;
450 /* Create the directories using POSIX names. */
452 root_ni = ntfs_inode_open(ctx->vol, FILE_root);
454 ERROR_WITH_ERRNO("Can't open root of NTFS volume");
455 return WIMLIB_ERR_NTFS_3G;
458 root->d_inode->i_mft_no = FILE_root;
460 ret = ntfs_3g_set_metadata(root_ni, root->d_inode, ctx);
462 ret = ntfs_3g_create_dirs_recursive(root_ni, root, ctx);
464 if (ntfs_inode_close(root_ni) && !ret) {
465 ERROR_WITH_ERRNO("Error closing root of NTFS volume");
466 ret = WIMLIB_ERR_NTFS_3G;
471 /* Set the DOS name of any directory that has one. In addition, create
472 * empty attributes for directories that have them. Note that creating
473 * an empty reparse point attribute must happen *after* setting the DOS
474 * name in order to work around a case where ntfs_set_ntfs_dos_name()
475 * fails with EOPNOTSUPP. This bug was fixed in NTFS-3G version
477 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
478 const struct wim_inode *inode = dentry->d_inode;
480 if (!(inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
482 if (dentry_has_short_name(dentry)) {
483 ret = ntfs_3g_restore_dos_name(NULL, NULL, dentry,
487 ret = report_file_created(&ctx->common);
491 if (ntfs_3g_has_empty_attributes(inode)) {
494 ret = WIMLIB_ERR_NTFS_3G;
495 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
497 ret = ntfs_3g_create_empty_attributes(ni, inode,
499 if (ntfs_inode_close(ni) && !ret)
500 ret = WIMLIB_ERR_NTFS_3G;
503 ERROR_WITH_ERRNO("Failed to create empty "
504 "attributes of directory "
505 "\"%s\" in NTFS volume",
506 dentry_full_path(dentry));
514 /* When creating an inode that will have a short (DOS) name, we create it using
515 * the long name associated with the short name. This ensures that the short
516 * name gets associated with the correct long name. */
517 static struct wim_dentry *
518 ntfs_3g_first_extraction_alias(struct wim_inode *inode)
520 struct wim_dentry *dentry;
522 inode_for_each_extraction_alias(dentry, inode)
523 if (dentry_has_short_name(dentry))
525 return inode_first_extraction_dentry(inode);
529 * Add a hard link for the NTFS inode @ni at the location corresponding to the
530 * WIM dentry @dentry.
532 * The parent directory must have already been created on the NTFS volume.
534 * Returns 0 on success; returns WIMLIB_ERR_NTFS_3G and sets errno on failure.
537 ntfs_3g_add_link(ntfs_inode *ni, struct wim_dentry *dentry)
542 /* Open the inode of the parent directory. */
543 dir_ni = ntfs_inode_open(ni->vol, dentry->d_parent->d_inode->i_mft_no);
547 /* Create the link. */
548 res = ntfs_link(ni, dir_ni, dentry->d_extraction_name,
549 dentry->d_extraction_name_nchars);
551 /* Close the parent directory. */
552 if (ntfs_inode_close(dir_ni) || res)
558 ERROR_WITH_ERRNO("Can't create link \"%s\" in NTFS volume",
559 dentry_full_path(dentry));
560 return WIMLIB_ERR_NTFS_3G;
564 ntfs_3g_create_nondirectory(struct wim_inode *inode,
565 struct ntfs_3g_apply_ctx *ctx)
567 struct wim_dentry *first_dentry;
570 struct wim_dentry *dentry;
573 first_dentry = ntfs_3g_first_extraction_alias(inode);
575 /* Create first link. */
577 dir_ni = ntfs_inode_open(ctx->vol, first_dentry->d_parent->d_inode->i_mft_no);
579 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
580 dentry_full_path(first_dentry->d_parent));
581 return WIMLIB_ERR_NTFS_3G;
584 ni = ntfs_create(dir_ni, 0, first_dentry->d_extraction_name,
585 first_dentry->d_extraction_name_nchars, S_IFREG);
588 ERROR_WITH_ERRNO("Can't create \"%s\" in NTFS volume",
589 dentry_full_path(first_dentry));
590 ntfs_inode_close(dir_ni);
591 return WIMLIB_ERR_NTFS_3G;
594 inode->i_mft_no = ni->mft_no;
596 /* Set short name if present. */
597 if (dentry_has_short_name(first_dentry)) {
599 ret = ntfs_3g_restore_dos_name(ni, dir_ni, first_dentry, ctx->vol);
601 /* ntfs_3g_restore_dos_name() closed both 'ni' and 'dir_ni'. */
606 /* Reopen the inode. */
607 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
609 ERROR_WITH_ERRNO("Failed to reopen \"%s\" "
611 dentry_full_path(first_dentry));
612 return WIMLIB_ERR_NTFS_3G;
615 /* Close the directory in which the first link was created. */
616 if (ntfs_inode_close(dir_ni)) {
617 ERROR_WITH_ERRNO("Failed to close \"%s\" in NTFS volume",
618 dentry_full_path(first_dentry->d_parent));
619 ret = WIMLIB_ERR_NTFS_3G;
624 /* Create additional links if present. */
625 inode_for_each_extraction_alias(dentry, inode) {
626 if (dentry != first_dentry) {
627 ret = ntfs_3g_add_link(ni, dentry);
634 ret = ntfs_3g_set_metadata(ni, inode, ctx);
638 ret = ntfs_3g_create_empty_attributes(ni, inode, ctx);
641 /* Close the inode. */
642 if (ntfs_inode_close(ni) && !ret) {
643 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
644 dentry_full_path(first_dentry));
645 ret = WIMLIB_ERR_NTFS_3G;
650 /* For each WIM dentry in the @dentry_list that represents a nondirectory file,
651 * create the corresponding nondirectory file in the NTFS volume.
653 * Directories must have already been created. */
655 ntfs_3g_create_nondirectories(struct list_head *dentry_list,
656 struct ntfs_3g_apply_ctx *ctx)
658 struct wim_dentry *dentry;
659 struct wim_inode *inode;
662 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
663 inode = dentry->d_inode;
664 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
666 if (dentry == inode_first_extraction_dentry(inode)) {
667 ret = ntfs_3g_create_nondirectory(inode, ctx);
671 ret = report_file_created(&ctx->common);
679 ntfs_3g_begin_extract_blob_instance(struct blob_descriptor *blob,
681 struct wim_inode *inode,
682 const struct wim_inode_stream *strm,
683 struct ntfs_3g_apply_ctx *ctx)
685 struct wim_dentry *one_dentry = inode_first_extraction_dentry(inode);
686 ntfschar *stream_name;
687 size_t stream_name_nchars;
690 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
692 if (blob->size > REPARSE_DATA_MAX_SIZE) {
693 ERROR("Reparse data of \"%s\" has size "
694 "%"PRIu64" bytes (exceeds %u bytes)",
695 dentry_full_path(one_dentry),
696 blob->size, REPARSE_DATA_MAX_SIZE);
697 return WIMLIB_ERR_INVALID_REPARSE_DATA;
699 ctx->reparse_ptr = ctx->rpbuf.rpdata;
700 ctx->ntfs_reparse_inodes[ctx->num_reparse_inodes] = ni;
701 ctx->wim_reparse_inodes[ctx->num_reparse_inodes] = inode;
702 ctx->num_reparse_inodes++;
706 /* It's a data stream (may be unnamed or named). */
707 wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
709 if (unlikely(stream_is_named(strm))) {
710 stream_name = strm->stream_name;
711 stream_name_nchars = utf16le_len_chars(stream_name);
713 if (ntfs_attr_add(ni, AT_DATA, stream_name,
714 stream_name_nchars, NULL, 0))
716 ERROR_WITH_ERRNO("Failed to create named data stream of \"%s\"",
717 dentry_full_path(one_dentry));
718 return WIMLIB_ERR_NTFS_3G;
721 /* Don't pass an empty string other than AT_UNNAMED to
722 * ntfs_attr_open() --- it violates assumptions made by
724 stream_name = AT_UNNAMED;
725 stream_name_nchars = 0;
728 /* This should be ensured by extract_blob_list() */
729 wimlib_assert(ctx->num_open_attrs < MAX_OPEN_FILES);
731 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_nchars);
733 ERROR_WITH_ERRNO("Failed to open data stream of \"%s\"",
734 dentry_full_path(one_dentry));
735 return WIMLIB_ERR_NTFS_3G;
739 * Note: there are problems with trying to combine compression with
740 * sparseness when extracting. For example, doing ntfs_attr_truncate()
741 * at the end to extend the attribute to its final size actually extends
742 * to a compression block size boundary rather than to the requested
743 * size. Until these problems are solved, we always write the full data
744 * to compressed attributes. We also don't attempt to preallocate space
745 * for compressed attributes, since we don't know how much space they
746 * are going to actually need.
748 ctx->is_sparse_attr[ctx->num_open_attrs] = false;
749 if (!(na->data_flags & ATTR_COMPRESSION_MASK)) {
750 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE) {
751 ctx->is_sparse_attr[ctx->num_open_attrs] = true;
752 ctx->any_sparse_attrs = true;
754 ntfs_attr_truncate_solid(na, blob->size);
757 ctx->open_attrs[ctx->num_open_attrs++] = na;
762 ntfs_3g_cleanup_blob_extract(struct ntfs_3g_apply_ctx *ctx)
766 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
767 if (ntfs_attr_pclose(ctx->open_attrs[i]))
769 ntfs_attr_close(ctx->open_attrs[i]);
772 ctx->num_open_attrs = 0;
774 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
775 if (ntfs_inode_close(ctx->open_inodes[i]))
778 ctx->num_open_inodes = 0;
780 ctx->any_sparse_attrs = false;
781 ctx->reparse_ptr = NULL;
782 ctx->num_reparse_inodes = 0;
787 ntfs_3g_open_inode(struct wim_inode *inode, struct ntfs_3g_apply_ctx *ctx)
791 /* If the same blob is being extracted to multiple streams of the same
792 * inode, then we must only open the inode once. */
793 if (unlikely(inode->i_num_streams > 1)) {
794 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
795 if (ctx->open_inodes[i]->mft_no == inode->i_mft_no) {
796 return ctx->open_inodes[i];
801 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
803 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
805 inode_first_extraction_dentry(inode)));
809 ctx->open_inodes[ctx->num_open_inodes++] = ni;
814 ntfs_3g_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
816 struct ntfs_3g_apply_ctx *ctx = _ctx;
817 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
821 for (u32 i = 0; i < blob->out_refcnt; i++) {
822 ret = WIMLIB_ERR_NTFS_3G;
823 ni = ntfs_3g_open_inode(targets[i].inode, ctx);
827 ret = ntfs_3g_begin_extract_blob_instance(blob, ni,
829 targets[i].stream, ctx);
837 ntfs_3g_cleanup_blob_extract(ctx);
843 * Note: prior to NTFS-3G version 2016.2.22, ntfs_attr_pwrite() could return a
844 * short count in non-error cases, contrary to its documentation. Specifically,
845 * a short count could be returned when writing to a compressed attribute and
846 * the requested count exceeded the size of an NTFS "compression block".
847 * Therefore, we must continue calling ntfs_attr_pwrite() until all bytes have
848 * been written or a real error has occurred.
851 ntfs_3g_full_pwrite(ntfs_attr *na, u64 offset, size_t size, const u8 *data)
854 s64 res = ntfs_attr_pwrite(na, offset, size, data);
855 if (unlikely(res <= 0))
857 wimlib_assert(res <= size);
866 ntfs_3g_extract_chunk(const struct blob_descriptor *blob, u64 offset,
867 const void *chunk, size_t size, void *_ctx)
869 struct ntfs_3g_apply_ctx *ctx = _ctx;
870 const void * const end = chunk + size;
877 * For sparse attributes, only write nonzero regions. This lets the
878 * filesystem use holes to represent zero regions.
880 for (p = chunk; p != end; p += len, offset += len) {
881 zeroes = maybe_detect_sparse_region(p, end - p, &len,
882 ctx->any_sparse_attrs);
883 for (i = 0; i < ctx->num_open_attrs; i++) {
884 if (!zeroes || !ctx->is_sparse_attr[i]) {
885 if (!ntfs_3g_full_pwrite(ctx->open_attrs[i],
892 if (ctx->reparse_ptr)
893 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
897 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
898 return WIMLIB_ERR_NTFS_3G;
902 ntfs_3g_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
904 struct ntfs_3g_apply_ctx *ctx = _ctx;
912 /* Extend sparse attributes to their final size. */
913 if (ctx->any_sparse_attrs) {
914 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
915 if (!ctx->is_sparse_attr[i])
917 if (ntfs_attr_truncate(ctx->open_attrs[i], blob->size))
919 ERROR_WITH_ERRNO("Error extending attribute to "
921 ret = WIMLIB_ERR_WRITE;
927 for (u32 i = 0; i < ctx->num_reparse_inodes; i++) {
928 ret = ntfs_3g_restore_reparse_point(ctx->ntfs_reparse_inodes[i],
929 ctx->wim_reparse_inodes[i],
936 if (ntfs_3g_cleanup_blob_extract(ctx) && !ret) {
937 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
938 ret = WIMLIB_ERR_NTFS_3G;
944 ntfs_3g_count_dentries(const struct list_head *dentry_list)
946 const struct wim_dentry *dentry;
949 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
951 if ((dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) &&
952 dentry_has_short_name(dentry))
962 ntfs_3g_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
964 struct ntfs_3g_apply_ctx *ctx = (struct ntfs_3g_apply_ctx *)_ctx;
966 struct wim_dentry *root;
969 /* For NTFS-3G extraction mode we require that the dentries to extract
970 * form a single tree. */
971 root = list_first_entry(dentry_list, struct wim_dentry,
972 d_extraction_list_node);
974 /* Mount the NTFS volume. */
975 vol = ntfs_mount(ctx->common.target, 0);
977 ERROR_WITH_ERRNO("Failed to mount \"%s\" with NTFS-3G",
979 return WIMLIB_ERR_NTFS_3G;
983 /* Opening $Secure is required to set security descriptors in NTFS v3.0
984 * format, where security descriptors are stored in a per-volume index
985 * rather than being fully specified for each file. */
986 if (ntfs_open_secure(vol) && vol->major_ver >= 3) {
987 ERROR_WITH_ERRNO("Unable to open security descriptor index of "
988 "NTFS volume \"%s\"", ctx->common.target);
989 ret = WIMLIB_ERR_NTFS_3G;
993 /* Create all inodes and aliases, including short names, and set
994 * metadata (attributes, security descriptors, and timestamps). */
996 ret = start_file_structure_phase(&ctx->common,
997 ntfs_3g_count_dentries(dentry_list));
1001 ret = ntfs_3g_create_directories(root, dentry_list, ctx);
1005 ret = ntfs_3g_create_nondirectories(dentry_list, ctx);
1009 ret = end_file_structure_phase(&ctx->common);
1013 /* Extract blobs. */
1014 struct read_blob_callbacks cbs = {
1015 .begin_blob = ntfs_3g_begin_extract_blob,
1016 .continue_blob = ntfs_3g_extract_chunk,
1017 .end_blob = ntfs_3g_end_extract_blob,
1020 ret = extract_blob_list(&ctx->common, &cbs);
1022 /* We do not need a final pass to set timestamps because libntfs-3g does
1023 * not update timestamps automatically (exception:
1024 * ntfs_set_ntfs_dos_name() does, but we handle this elsewhere). */
1027 if (vol->secure_ni) {
1028 ntfs_index_ctx_put(vol->secure_xsii);
1029 ntfs_index_ctx_put(vol->secure_xsdh);
1030 if (ntfs_inode_close(vol->secure_ni) && !ret) {
1031 ERROR_WITH_ERRNO("Failed to close security descriptor "
1032 "index of NTFS volume \"%s\"",
1033 ctx->common.target);
1034 ret = WIMLIB_ERR_NTFS_3G;
1036 vol->secure_ni = NULL;
1038 if (ntfs_umount(ctx->vol, FALSE) && !ret) {
1039 ERROR_WITH_ERRNO("Failed to unmount \"%s\" with NTFS-3G",
1040 ctx->common.target);
1041 ret = WIMLIB_ERR_NTFS_3G;
1046 const struct apply_operations ntfs_3g_apply_ops = {
1048 .get_supported_features = ntfs_3g_get_supported_features,
1049 .extract = ntfs_3g_extract,
1050 .context_size = sizeof(struct ntfs_3g_apply_ctx),
1051 .single_tree_only = true,