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);
172 ERROR_WITH_ERRNO("Failed to set DOS name of \"%s\" in NTFS "
173 "volume", dentry_full_path(dentry));
175 ERROR("This error may have been caused by a known "
176 "bug in libntfs-3g where it is unable to set "
177 "DOS names on files whose long names contain "
178 "unpaired surrogate characters. This bug "
179 "was fixed in the development version of "
180 "NTFS-3G in June 2016.");
184 dentry->d_name[dentry->d_name_nbytes / 2 - 1];
185 if (c == cpu_to_le16('.') || c == cpu_to_le16(' ')) {
186 ERROR("This error was probably caused by a "
187 "known bug in libntfs-3g where it is "
188 "unable to set DOS names on files whose "
189 "long names end with a dot or space "
191 "https://wimlib.net/forums/viewtopic.php?f=1&t=294 "
192 "for more information.");
195 ret = WIMLIB_ERR_SET_SHORT_NAME;
199 /* Unlike most other NTFS-3G functions, ntfs_set_ntfs_dos_name()
200 * changes the directory's last modification timestamp...
202 return ntfs_3g_restore_timestamps(vol, dentry->d_parent->d_inode);
205 /* ntfs_inode_close() can take a NULL argument, but it's probably best
206 * not to rely on this behavior. */
208 ntfs_inode_close(ni);
210 ntfs_inode_close(dir_ni);
215 ntfs_3g_restore_reparse_point(ntfs_inode *ni, const struct wim_inode *inode,
216 unsigned blob_size, struct ntfs_3g_apply_ctx *ctx)
218 complete_reparse_point(&ctx->rpbuf, inode, blob_size);
220 if (ntfs_set_ntfs_reparse_data(ni, (const char *)&ctx->rpbuf,
221 REPARSE_DATA_OFFSET + blob_size, 0))
224 ERROR_WITH_ERRNO("Failed to set reparse data on \"%s\"",
226 inode_first_extraction_dentry(inode)));
227 if (err == EINVAL && !(inode->i_reparse_tag & 0x80000000)) {
228 WARNING("This reparse point had a non-Microsoft reparse "
229 "tag. The preceding error may have been caused "
230 "by a known bug in libntfs-3g where it does not "
231 "correctly validate non-Microsoft reparse "
232 "points. This bug was fixed in NTFS-3G version "
235 return WIMLIB_ERR_SET_REPARSE_DATA;
242 ntfs_3g_has_empty_attributes(const struct wim_inode *inode)
244 for (unsigned i = 0; i < inode->i_num_streams; i++) {
245 const struct wim_inode_stream *strm = &inode->i_streams[i];
247 if (stream_blob_resolved(strm) == NULL &&
248 (strm->stream_type == STREAM_TYPE_REPARSE_POINT ||
249 stream_is_named_data_stream(strm)))
256 * Create empty attributes (named data streams and potentially a reparse point)
257 * for the specified file, if there are any.
259 * Since these won't have blob descriptors, they won't show up in the call to
260 * extract_blob_list(). Hence the need for the special case.
262 * Keep this in sync with ntfs_3g_has_empty_attributes()!
265 ntfs_3g_create_empty_attributes(ntfs_inode *ni,
266 const struct wim_inode *inode,
267 struct ntfs_3g_apply_ctx *ctx)
269 for (unsigned i = 0; i < inode->i_num_streams; i++) {
271 const struct wim_inode_stream *strm = &inode->i_streams[i];
274 if (stream_blob_resolved(strm) != NULL)
277 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT) {
278 ret = ntfs_3g_restore_reparse_point(ni, inode, 0, ctx);
281 } else if (stream_is_named_data_stream(strm)) {
282 if (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
283 utf16le_len_chars(strm->stream_name),
286 ERROR_WITH_ERRNO("Failed to create named data "
289 inode_first_extraction_dentry(inode)));
290 return WIMLIB_ERR_NTFS_3G;
297 /* Set attributes, security descriptor, and timestamps on the NTFS inode @ni.
300 ntfs_3g_set_metadata(ntfs_inode *ni, const struct wim_inode *inode,
301 const struct ntfs_3g_apply_ctx *ctx)
304 const struct wim_security_data *sd;
305 struct wim_dentry *one_dentry;
308 extract_flags = ctx->common.extract_flags;
309 sd = wim_get_current_security_data(ctx->common.wim);
310 one_dentry = inode_first_extraction_dentry(inode);
315 const void *object_id = inode_get_object_id(inode, &len);
316 if (unlikely(object_id != NULL) &&
317 ntfs_set_ntfs_object_id(ni, object_id, len, 0))
319 if (errno == EEXIST) {
320 WARNING("Duplicate object ID on file \"%s\"",
321 dentry_full_path(one_dentry));
323 ERROR_WITH_ERRNO("Failed to set object ID on "
324 "\"%s\" in NTFS volume",
325 dentry_full_path(one_dentry));
326 return WIMLIB_ERR_NTFS_3G;
332 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
333 u32 attrib = inode->i_attributes;
335 attrib &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
336 FILE_ATTRIBUTE_ENCRYPTED);
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 attr = 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;
735 ctx->open_attrs[ctx->num_open_attrs++] = attr;
736 ntfs_attr_truncate_solid(attr, blob->size);
741 ntfs_3g_cleanup_blob_extract(struct ntfs_3g_apply_ctx *ctx)
745 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
746 if (ntfs_attr_pclose(ctx->open_attrs[i]))
748 ntfs_attr_close(ctx->open_attrs[i]);
751 ctx->num_open_attrs = 0;
753 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
754 if (ntfs_inode_close(ctx->open_inodes[i]))
757 ctx->num_open_inodes = 0;
760 ctx->reparse_ptr = NULL;
761 ctx->num_reparse_inodes = 0;
766 ntfs_3g_open_inode(struct wim_inode *inode, struct ntfs_3g_apply_ctx *ctx)
770 /* If the same blob is being extracted to multiple streams of the same
771 * inode, then we must only open the inode once. */
772 if (unlikely(inode->i_num_streams > 1)) {
773 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
774 if (ctx->open_inodes[i]->mft_no == inode->i_mft_no) {
775 return ctx->open_inodes[i];
780 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
782 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
784 inode_first_extraction_dentry(inode)));
788 ctx->open_inodes[ctx->num_open_inodes++] = ni;
793 ntfs_3g_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
795 struct ntfs_3g_apply_ctx *ctx = _ctx;
796 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
800 for (u32 i = 0; i < blob->out_refcnt; i++) {
801 ret = WIMLIB_ERR_NTFS_3G;
802 ni = ntfs_3g_open_inode(targets[i].inode, ctx);
806 ret = ntfs_3g_begin_extract_blob_instance(blob, ni,
808 targets[i].stream, ctx);
816 ntfs_3g_cleanup_blob_extract(ctx);
822 * Note: prior to NTFS-3G version 2016.2.22, ntfs_attr_pwrite() could return a
823 * short count in non-error cases, contrary to its documentation. Specifically,
824 * a short count could be returned when writing to a compressed attribute and
825 * the requested count exceeded the size of an NTFS "compression block".
826 * Therefore, we must continue calling ntfs_attr_pwrite() until all bytes have
827 * been written or a real error has occurred.
830 ntfs_3g_full_pwrite(ntfs_attr *na, u64 offset, size_t size, const u8 *data)
833 s64 res = ntfs_attr_pwrite(na, offset, size, data);
834 if (unlikely(res <= 0))
836 wimlib_assert(res <= size);
845 ntfs_3g_extract_chunk(const void *chunk, size_t size, void *_ctx)
847 struct ntfs_3g_apply_ctx *ctx = _ctx;
849 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
850 if (!ntfs_3g_full_pwrite(ctx->open_attrs[i],
851 ctx->offset, size, chunk))
853 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
854 return WIMLIB_ERR_NTFS_3G;
857 if (ctx->reparse_ptr)
858 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
864 ntfs_3g_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
866 struct ntfs_3g_apply_ctx *ctx = _ctx;
874 for (u32 i = 0; i < ctx->num_reparse_inodes; i++) {
875 ret = ntfs_3g_restore_reparse_point(ctx->ntfs_reparse_inodes[i],
876 ctx->wim_reparse_inodes[i],
883 if (ntfs_3g_cleanup_blob_extract(ctx) && !ret) {
884 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
885 ret = WIMLIB_ERR_NTFS_3G;
891 ntfs_3g_count_dentries(const struct list_head *dentry_list)
893 const struct wim_dentry *dentry;
896 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
898 if ((dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) &&
899 dentry_has_short_name(dentry))
909 ntfs_3g_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
911 struct ntfs_3g_apply_ctx *ctx = (struct ntfs_3g_apply_ctx *)_ctx;
913 struct wim_dentry *root;
916 /* For NTFS-3G extraction mode we require that the dentries to extract
917 * form a single tree. */
918 root = list_first_entry(dentry_list, struct wim_dentry,
919 d_extraction_list_node);
921 /* Mount the NTFS volume. */
922 vol = ntfs_mount(ctx->common.target, 0);
924 ERROR_WITH_ERRNO("Failed to mount \"%s\" with NTFS-3G",
926 return WIMLIB_ERR_NTFS_3G;
930 /* Opening $Secure is required to set security descriptors in NTFS v3.0
931 * format, where security descriptors are stored in a per-volume index
932 * rather than being fully specified for each file. */
933 if (ntfs_open_secure(vol) && vol->major_ver >= 3) {
934 ERROR_WITH_ERRNO("Unable to open security descriptor index of "
935 "NTFS volume \"%s\"", ctx->common.target);
936 ret = WIMLIB_ERR_NTFS_3G;
940 /* Create all inodes and aliases, including short names, and set
941 * metadata (attributes, security descriptors, and timestamps). */
943 ret = start_file_structure_phase(&ctx->common,
944 ntfs_3g_count_dentries(dentry_list));
948 ret = ntfs_3g_create_directories(root, dentry_list, ctx);
952 ret = ntfs_3g_create_nondirectories(dentry_list, ctx);
956 ret = end_file_structure_phase(&ctx->common);
961 struct read_blob_callbacks cbs = {
962 .begin_blob = ntfs_3g_begin_extract_blob,
963 .consume_chunk = ntfs_3g_extract_chunk,
964 .end_blob = ntfs_3g_end_extract_blob,
967 ret = extract_blob_list(&ctx->common, &cbs);
969 /* We do not need a final pass to set timestamps because libntfs-3g does
970 * not update timestamps automatically (exception:
971 * ntfs_set_ntfs_dos_name() does, but we handle this elsewhere). */
974 if (vol->secure_ni) {
975 ntfs_index_ctx_put(vol->secure_xsii);
976 ntfs_index_ctx_put(vol->secure_xsdh);
977 if (ntfs_inode_close(vol->secure_ni) && !ret) {
978 ERROR_WITH_ERRNO("Failed to close security descriptor "
979 "index of NTFS volume \"%s\"",
981 ret = WIMLIB_ERR_NTFS_3G;
983 vol->secure_ni = NULL;
985 if (ntfs_umount(ctx->vol, FALSE) && !ret) {
986 ERROR_WITH_ERRNO("Failed to unmount \"%s\" with NTFS-3G",
988 ret = WIMLIB_ERR_NTFS_3G;
993 const struct apply_operations ntfs_3g_apply_ops = {
995 .get_supported_features = ntfs_3g_get_supported_features,
996 .extract = ntfs_3g_extract,
997 .context_size = sizeof(struct ntfs_3g_apply_ctx),
998 .single_tree_only = true,