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, and alternate data streams.
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, 2013, 2014, 2015 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/reparse.h>
39 #include <ntfs-3g/security.h>
41 #include "wimlib/assert.h"
42 #include "wimlib/apply.h"
43 #include "wimlib/blob_table.h"
44 #include "wimlib/dentry.h"
45 #include "wimlib/encoding.h"
46 #include "wimlib/error.h"
47 #include "wimlib/metadata.h"
48 #include "wimlib/ntfs_3g.h"
49 #include "wimlib/reparse.h"
50 #include "wimlib/security.h"
51 #include "wimlib/security_descriptor.h"
54 ntfs_3g_get_supported_features(const char *target,
55 struct wim_features *supported_features)
57 supported_features->archive_files = 1;
58 supported_features->hidden_files = 1;
59 supported_features->system_files = 1;
60 supported_features->compressed_files = 1;
61 supported_features->not_context_indexed_files = 1;
62 supported_features->named_data_streams = 1;
63 supported_features->hard_links = 1;
64 supported_features->reparse_points = 1;
65 supported_features->security_descriptors = 1;
66 supported_features->short_names = 1;
67 supported_features->timestamps = 1;
68 supported_features->case_sensitive_filenames = 1;
72 struct ntfs_3g_apply_ctx {
73 /* Extract flags, the pointer to the WIMStruct, etc. */
74 struct apply_ctx common;
76 /* Pointer to the open NTFS volume */
79 ntfs_attr *open_attrs[MAX_OPEN_FILES];
80 unsigned num_open_attrs;
81 ntfs_inode *open_inodes[MAX_OPEN_FILES];
82 unsigned num_open_inodes;
84 struct reparse_buffer_disk rpbuf;
87 /* Offset in the blob currently being read */
90 unsigned num_reparse_inodes;
91 ntfs_inode *ntfs_reparse_inodes[MAX_OPEN_FILES];
92 struct wim_inode *wim_reparse_inodes[MAX_OPEN_FILES];
96 sid_size(const wimlib_SID *sid)
98 return offsetof(wimlib_SID, sub_authority) +
99 sizeof(le32) * sid->sub_authority_count;
103 * sd_fixup - Fix up a Windows NT security descriptor for libntfs-3g.
105 * libntfs-3g validates security descriptors before setting them, but old
106 * versions contain bugs causing it to reject unusual but valid security
109 * - Versions before 2013.1.13 reject security descriptors ending with an empty
110 * SACL (System Access Control List). This bug can be worked around either by
111 * moving the empty SACL earlier in the security descriptor or by removing the
112 * SACL entirely. The latter work-around is valid because an empty SACL is
113 * equivalent to a "null", or non-existent, SACL.
114 * - Versions before 2014.2.15 reject security descriptors ending with an empty
115 * DACL (Discretionary Access Control List). This is very similar to the SACL
116 * bug. However, removing the DACL is not a valid workaround because this
117 * changes the meaning of the security descriptor--- an empty DACL allows no
118 * access, whereas a "null" DACL allows all access.
120 * If the security descriptor was fixed, this function returns an allocated
121 * buffer containing the fixed security descriptor, and its size is updated.
122 * Otherwise (or if no memory is available) NULL is returned.
125 sd_fixup(const void *_desc, size_t *size_p)
127 u32 owner_offset, group_offset, dacl_offset, sacl_offset;
128 bool owner_valid, group_valid;
129 size_t size = *size_p;
130 const wimlib_SECURITY_DESCRIPTOR_RELATIVE *desc = _desc;
131 wimlib_SECURITY_DESCRIPTOR_RELATIVE *desc_new;
132 const wimlib_SID *owner, *group, *sid;
134 /* Don't attempt to fix clearly invalid security descriptors. */
135 if (size < sizeof(wimlib_SECURITY_DESCRIPTOR_RELATIVE))
138 if (le16_to_cpu(desc->control) & wimlib_SE_DACL_PRESENT)
139 dacl_offset = le32_to_cpu(desc->dacl_offset);
143 if (le16_to_cpu(desc->control) & wimlib_SE_SACL_PRESENT)
144 sacl_offset = le32_to_cpu(desc->sacl_offset);
148 /* Check if the security descriptor will be affected by one of the bugs.
149 * If not, do nothing and return. */
150 if (!((sacl_offset != 0 && sacl_offset == size - sizeof(wimlib_ACL)) ||
151 (dacl_offset != 0 && dacl_offset == size - sizeof(wimlib_ACL))))
154 owner_offset = le32_to_cpu(desc->owner_offset);
155 group_offset = le32_to_cpu(desc->group_offset);
156 owner = (const wimlib_SID*)((const u8*)desc + owner_offset);
157 group = (const wimlib_SID*)((const u8*)desc + group_offset);
159 /* We'll try to move the owner or group SID to the end of the security
160 * descriptor to avoid the bug. This is only possible if at least one
162 owner_valid = (owner_offset != 0) &&
163 (owner_offset % 4 == 0) &&
164 (owner_offset <= size - sizeof(SID)) &&
165 (owner_offset + sid_size(owner) <= size) &&
166 (owner_offset >= sizeof(wimlib_SECURITY_DESCRIPTOR_RELATIVE));
167 group_valid = (group_offset != 0) &&
168 (group_offset % 4 == 0) &&
169 (group_offset <= size - sizeof(SID)) &&
170 (group_offset + sid_size(group) <= size) &&
171 (group_offset >= sizeof(wimlib_SECURITY_DESCRIPTOR_RELATIVE));
174 } else if (group_valid) {
180 desc_new = MALLOC(size + sid_size(sid));
184 memcpy(desc_new, desc, size);
186 desc_new->owner_offset = cpu_to_le32(size);
187 else if (group_valid)
188 desc_new->group_offset = cpu_to_le32(size);
189 memcpy((u8*)desc_new + size, sid, sid_size(sid));
190 *size_p = size + sid_size(sid);
194 /* Set the security descriptor @desc of size @desc_size on the NTFS inode @ni.
197 ntfs_3g_set_security_descriptor(ntfs_inode *ni, const void *desc, size_t desc_size)
199 struct SECURITY_CONTEXT sec_ctx;
200 void *desc_fixed = NULL;
203 memset(&sec_ctx, 0, sizeof(sec_ctx));
204 sec_ctx.vol = ni->vol;
207 if (ntfs_set_ntfs_acl(&sec_ctx, ni, desc, desc_size, 0)) {
208 if (desc_fixed == NULL) {
209 desc_fixed = sd_fixup(desc, &desc_size);
210 if (desc_fixed != NULL) {
215 ret = WIMLIB_ERR_SET_SECURITY;
223 ntfs_3g_set_timestamps(ntfs_inode *ni, const struct wim_inode *inode)
226 inode->i_creation_time,
227 inode->i_last_write_time,
228 inode->i_last_access_time,
231 if (ntfs_inode_set_times(ni, (const char *)times, sizeof(times), 0))
232 return WIMLIB_ERR_SET_TIMESTAMPS;
236 /* Restore the timestamps on the NTFS inode corresponding to @inode. */
238 ntfs_3g_restore_timestamps(ntfs_volume *vol, const struct wim_inode *inode)
243 ni = ntfs_inode_open(vol, inode->i_mft_no);
247 res = ntfs_3g_set_timestamps(ni, inode);
249 if (ntfs_inode_close(ni) || res)
255 ERROR_WITH_ERRNO("Failed to update timestamps of \"%s\" in NTFS volume",
256 dentry_full_path(inode_first_extraction_dentry(inode)));
257 return WIMLIB_ERR_SET_TIMESTAMPS;
260 /* Restore the DOS name of the @dentry.
261 * This closes both @ni and @dir_ni.
262 * If either is NULL, then they are opened temporarily. */
264 ntfs_3g_restore_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni,
265 struct wim_dentry *dentry, ntfs_volume *vol)
268 const char *dos_name;
269 size_t dos_name_nbytes;
271 /* Note: ntfs_set_ntfs_dos_name() closes both inodes (even if it fails).
272 * And it takes in a multibyte string, even though it translates it to
273 * UTF-16LE internally... which is annoying because we currently have
274 * the UTF-16LE string but not the multibyte string. */
276 ret = utf16le_get_tstr(dentry->d_short_name, dentry->d_short_name_nbytes,
277 &dos_name, &dos_name_nbytes);
282 dir_ni = ntfs_inode_open(vol, dentry->d_parent->d_inode->i_mft_no);
284 ni = ntfs_inode_open(vol, dentry->d_inode->i_mft_no);
286 ret = ntfs_set_ntfs_dos_name(ni, dir_ni,
287 dos_name, dos_name_nbytes, 0);
293 utf16le_put_tstr(dos_name);
295 ERROR_WITH_ERRNO("Failed to set DOS name of \"%s\" in NTFS "
296 "volume", dentry_full_path(dentry));
297 ret = WIMLIB_ERR_SET_SHORT_NAME;
301 /* Unlike most other NTFS-3g functions, ntfs_set_ntfs_dos_name()
302 * changes the directory's last modification timestamp...
304 return ntfs_3g_restore_timestamps(vol, dentry->d_parent->d_inode);
307 /* ntfs_inode_close() can take a NULL argument, but it's probably best
308 * not to rely on this behavior. */
310 ntfs_inode_close(ni);
312 ntfs_inode_close(dir_ni);
317 ntfs_3g_restore_reparse_point(ntfs_inode *ni, const struct wim_inode *inode,
318 unsigned blob_size, struct ntfs_3g_apply_ctx *ctx)
320 complete_reparse_point(&ctx->rpbuf, inode, blob_size);
322 if (ntfs_set_ntfs_reparse_data(ni, (const char *)&ctx->rpbuf,
323 REPARSE_DATA_OFFSET + blob_size, 0))
325 ERROR_WITH_ERRNO("Failed to set reparse data on \"%s\"",
327 inode_first_extraction_dentry(inode)));
328 return WIMLIB_ERR_SET_REPARSE_DATA;
336 * Create empty attributes (named data streams and potentially a reparse point)
337 * for the specified file, if there are any.
339 * Since these won't have blob descriptors, they won't show up in the call to
340 * extract_blob_list(). Hence the need for the special case.
343 ntfs_3g_create_empty_attributes(ntfs_inode *ni,
344 const struct wim_inode *inode,
345 struct ntfs_3g_apply_ctx *ctx)
348 for (unsigned i = 0; i < inode->i_num_streams; i++) {
350 const struct wim_inode_stream *strm = &inode->i_streams[i];
353 if (stream_blob_resolved(strm) != NULL)
356 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT) {
357 ret = ntfs_3g_restore_reparse_point(ni, inode, 0, ctx);
360 } else if (stream_is_named_data_stream(strm)) {
361 if (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
362 utf16le_len_chars(strm->stream_name),
365 ERROR_WITH_ERRNO("Failed to create named data "
368 inode_first_extraction_dentry(inode)));
369 return WIMLIB_ERR_NTFS_3G;
376 /* Set attributes, security descriptor, and timestamps on the NTFS inode @ni.
379 ntfs_3g_set_metadata(ntfs_inode *ni, const struct wim_inode *inode,
380 const struct ntfs_3g_apply_ctx *ctx)
383 const struct wim_security_data *sd;
384 struct wim_dentry *one_dentry;
387 extract_flags = ctx->common.extract_flags;
388 sd = wim_get_current_security_data(ctx->common.wim);
389 one_dentry = inode_first_extraction_dentry(inode);
392 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
393 u32 attrib = inode->i_attributes;
395 attrib &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
396 FILE_ATTRIBUTE_ENCRYPTED);
398 if (ntfs_set_ntfs_attrib(ni, (const char *)&attrib,
401 ERROR_WITH_ERRNO("Failed to set attributes on \"%s\" "
403 dentry_full_path(one_dentry));
404 return WIMLIB_ERR_SET_ATTRIBUTES;
408 /* Security descriptor */
409 if (inode_has_security_descriptor(inode)
410 && !(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
415 desc = sd->descriptors[inode->i_security_id];
416 desc_size = sd->sizes[inode->i_security_id];
418 ret = ntfs_3g_set_security_descriptor(ni, desc, desc_size);
422 ERROR_WITH_ERRNO("Failed to set security descriptor on "
423 "\"%s\" in NTFS volume",
424 dentry_full_path(one_dentry));
425 if (err == EINVAL && wimlib_print_errors) {
426 fprintf(wimlib_error_file,
427 "The security descriptor is: ");
428 print_byte_field(desc, desc_size, wimlib_error_file);
429 fprintf(wimlib_error_file, "\n");
430 fprintf(wimlib_error_file,
431 "\nThis error occurred because libntfs-3g thinks "
432 "the security descriptor is invalid. If you "
433 "are extracting a Windows 10 image, this may be "
434 "caused by a known bug in libntfs-3g. See: "
435 "http://wimlib.net/forums/viewtopic.php?f=1&t=4 "
436 "for more information.\n\n");
443 ret = ntfs_3g_set_timestamps(ni, inode);
445 ERROR_WITH_ERRNO("Failed to set timestamps on \"%s\" "
447 dentry_full_path(one_dentry));
453 /* Recursively creates all the subdirectories of @dir, which has been created as
454 * the NTFS inode @dir_ni. */
456 ntfs_3g_create_dirs_recursive(ntfs_inode *dir_ni, struct wim_dentry *dir,
457 struct ntfs_3g_apply_ctx *ctx)
459 struct wim_dentry *child;
461 for_dentry_child(child, dir) {
465 if (!(child->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
467 if (!will_extract_dentry(child))
470 ni = ntfs_create(dir_ni, 0, child->d_extraction_name,
471 child->d_extraction_name_nchars, S_IFDIR);
473 ERROR_WITH_ERRNO("Error creating \"%s\" in NTFS volume",
474 dentry_full_path(child));
475 return WIMLIB_ERR_NTFS_3G;
478 child->d_inode->i_mft_no = ni->mft_no;
480 ret = report_file_created(&ctx->common);
482 ret = ntfs_3g_set_metadata(ni, child->d_inode, ctx);
484 ret = ntfs_3g_create_empty_attributes(ni, child->d_inode, ctx);
486 ret = ntfs_3g_create_dirs_recursive(ni, child, ctx);
488 if (ntfs_inode_close_in_dir(ni, dir_ni) && !ret) {
489 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
490 dentry_full_path(child));
491 ret = WIMLIB_ERR_NTFS_3G;
499 /* For each WIM dentry in the @root tree that represents a directory, create the
500 * corresponding directory in the NTFS volume @ctx->vol. */
502 ntfs_3g_create_directories(struct wim_dentry *root,
503 struct list_head *dentry_list,
504 struct ntfs_3g_apply_ctx *ctx)
508 struct wim_dentry *dentry;
510 /* Create the directories using POSIX names. */
512 root_ni = ntfs_inode_open(ctx->vol, FILE_root);
514 ERROR_WITH_ERRNO("Can't open root of NTFS volume");
515 return WIMLIB_ERR_NTFS_3G;
518 root->d_inode->i_mft_no = FILE_root;
520 ret = ntfs_3g_create_dirs_recursive(root_ni, root, ctx);
522 if (ntfs_inode_close(root_ni) && !ret) {
523 ERROR_WITH_ERRNO("Error closing root of NTFS volume");
524 ret = WIMLIB_ERR_NTFS_3G;
529 /* Set the DOS name of any directory that has one. */
530 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
531 if (!(dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
533 if (!dentry_has_short_name(dentry))
535 ret = ntfs_3g_restore_dos_name(NULL, NULL, dentry, ctx->vol);
538 ret = report_file_created(&ctx->common);
545 /* When creating an inode that will have a short (DOS) name, we create it using
546 * the long name associated with the short name. This ensures that the short
547 * name gets associated with the correct long name. */
548 static struct wim_dentry *
549 ntfs_3g_first_extraction_alias(struct wim_inode *inode)
551 struct wim_dentry *dentry;
553 inode_for_each_extraction_alias(dentry, inode)
554 if (dentry_has_short_name(dentry))
556 return inode_first_extraction_dentry(inode);
560 * Add a hard link for the NTFS inode @ni at the location corresponding to the
561 * WIM dentry @dentry.
563 * The parent directory must have already been created on the NTFS volume.
565 * Returns 0 on success; returns WIMLIB_ERR_NTFS_3G and sets errno on failure.
568 ntfs_3g_add_link(ntfs_inode *ni, struct wim_dentry *dentry)
573 /* Open the inode of the parent directory. */
574 dir_ni = ntfs_inode_open(ni->vol, dentry->d_parent->d_inode->i_mft_no);
578 /* Create the link. */
579 res = ntfs_link(ni, dir_ni, dentry->d_extraction_name,
580 dentry->d_extraction_name_nchars);
582 /* Close the parent directory. */
583 if (ntfs_inode_close(dir_ni) || res)
589 ERROR_WITH_ERRNO("Can't create link \"%s\" in NTFS volume",
590 dentry_full_path(dentry));
591 return WIMLIB_ERR_NTFS_3G;
595 ntfs_3g_create_nondirectory(struct wim_inode *inode,
596 struct ntfs_3g_apply_ctx *ctx)
598 struct wim_dentry *first_dentry;
601 struct wim_dentry *dentry;
604 first_dentry = ntfs_3g_first_extraction_alias(inode);
606 /* Create first link. */
608 dir_ni = ntfs_inode_open(ctx->vol, first_dentry->d_parent->d_inode->i_mft_no);
610 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
611 dentry_full_path(first_dentry->d_parent));
612 return WIMLIB_ERR_NTFS_3G;
615 ni = ntfs_create(dir_ni, 0, first_dentry->d_extraction_name,
616 first_dentry->d_extraction_name_nchars, S_IFREG);
619 ERROR_WITH_ERRNO("Can't create \"%s\" in NTFS volume",
620 dentry_full_path(first_dentry));
621 ntfs_inode_close(dir_ni);
622 return WIMLIB_ERR_NTFS_3G;
625 inode->i_mft_no = ni->mft_no;
627 /* Set short name if present. */
628 if (dentry_has_short_name(first_dentry)) {
630 ret = ntfs_3g_restore_dos_name(ni, dir_ni, first_dentry, ctx->vol);
632 /* ntfs_3g_restore_dos_name() closed both 'ni' and 'dir_ni'. */
637 /* Reopen the inode. */
638 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
640 ERROR_WITH_ERRNO("Failed to reopen \"%s\" "
642 dentry_full_path(first_dentry));
643 return WIMLIB_ERR_NTFS_3G;
646 /* Close the directory in which the first link was created. */
647 if (ntfs_inode_close(dir_ni)) {
648 ERROR_WITH_ERRNO("Failed to close \"%s\" in NTFS volume",
649 dentry_full_path(first_dentry->d_parent));
650 ret = WIMLIB_ERR_NTFS_3G;
655 /* Create additional links if present. */
656 inode_for_each_extraction_alias(dentry, inode) {
657 if (dentry != first_dentry) {
658 ret = ntfs_3g_add_link(ni, dentry);
665 ret = ntfs_3g_set_metadata(ni, inode, ctx);
669 ret = ntfs_3g_create_empty_attributes(ni, inode, ctx);
672 /* Close the inode. */
673 if (ntfs_inode_close(ni) && !ret) {
674 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
675 dentry_full_path(first_dentry));
676 ret = WIMLIB_ERR_NTFS_3G;
681 /* For each WIM dentry in the @dentry_list that represents a nondirectory file,
682 * create the corresponding nondirectory file in the NTFS volume.
684 * Directories must have already been created. */
686 ntfs_3g_create_nondirectories(struct list_head *dentry_list,
687 struct ntfs_3g_apply_ctx *ctx)
689 struct wim_dentry *dentry;
690 struct wim_inode *inode;
693 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
694 inode = dentry->d_inode;
695 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
697 if (dentry == inode_first_extraction_dentry(inode)) {
698 ret = ntfs_3g_create_nondirectory(inode, ctx);
702 ret = report_file_created(&ctx->common);
710 ntfs_3g_begin_extract_blob_instance(struct blob_descriptor *blob,
712 struct wim_inode *inode,
713 const struct wim_inode_stream *strm,
714 struct ntfs_3g_apply_ctx *ctx)
716 struct wim_dentry *one_dentry = inode_first_extraction_dentry(inode);
717 size_t stream_name_nchars;
720 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
722 if (blob->size > REPARSE_DATA_MAX_SIZE) {
723 ERROR("Reparse data of \"%s\" has size "
724 "%"PRIu64" bytes (exceeds %u bytes)",
725 dentry_full_path(one_dentry),
726 blob->size, REPARSE_DATA_MAX_SIZE);
727 return WIMLIB_ERR_INVALID_REPARSE_DATA;
729 ctx->reparse_ptr = ctx->rpbuf.rpdata;
730 ctx->ntfs_reparse_inodes[ctx->num_reparse_inodes] = ni;
731 ctx->wim_reparse_inodes[ctx->num_reparse_inodes] = inode;
732 ctx->num_reparse_inodes++;
736 /* It's a data stream (may be unnamed or named). */
737 wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
739 stream_name_nchars = utf16le_len_chars(strm->stream_name);
741 if (stream_name_nchars &&
742 (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
743 stream_name_nchars, NULL, 0)))
745 ERROR_WITH_ERRNO("Failed to create named data stream of \"%s\"",
746 dentry_full_path(one_dentry));
747 return WIMLIB_ERR_NTFS_3G;
750 /* This should be ensured by extract_blob_list() */
751 wimlib_assert(ctx->num_open_attrs < MAX_OPEN_FILES);
753 attr = ntfs_attr_open(ni, AT_DATA, strm->stream_name,
756 ERROR_WITH_ERRNO("Failed to open data stream of \"%s\"",
757 dentry_full_path(one_dentry));
758 return WIMLIB_ERR_NTFS_3G;
760 ctx->open_attrs[ctx->num_open_attrs++] = attr;
761 ntfs_attr_truncate_solid(attr, blob->size);
766 ntfs_3g_cleanup_blob_extract(struct ntfs_3g_apply_ctx *ctx)
770 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
771 if (ntfs_attr_pclose(ctx->open_attrs[i]))
773 ntfs_attr_close(ctx->open_attrs[i]);
776 ctx->num_open_attrs = 0;
778 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
779 if (ntfs_inode_close(ctx->open_inodes[i]))
782 ctx->num_open_inodes = 0;
785 ctx->reparse_ptr = NULL;
786 ctx->num_reparse_inodes = 0;
791 ntfs_3g_open_inode(struct wim_inode *inode, struct ntfs_3g_apply_ctx *ctx)
795 /* If the same blob is being extracted to multiple streams of the same
796 * inode, then we must only open the inode once. */
797 if (unlikely(inode->i_num_streams > 1)) {
798 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
799 if (ctx->open_inodes[i]->mft_no == inode->i_mft_no) {
800 return ctx->open_inodes[i];
805 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
807 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
809 inode_first_extraction_dentry(inode)));
813 ctx->open_inodes[ctx->num_open_inodes++] = ni;
818 ntfs_3g_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
820 struct ntfs_3g_apply_ctx *ctx = _ctx;
821 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
825 for (u32 i = 0; i < blob->out_refcnt; i++) {
826 ret = WIMLIB_ERR_NTFS_3G;
827 ni = ntfs_3g_open_inode(targets[i].inode, ctx);
831 ret = ntfs_3g_begin_extract_blob_instance(blob, ni,
833 targets[i].stream, ctx);
841 ntfs_3g_cleanup_blob_extract(ctx);
847 ntfs_3g_extract_chunk(const void *chunk, size_t size, void *_ctx)
849 struct ntfs_3g_apply_ctx *ctx = _ctx;
852 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
853 res = ntfs_attr_pwrite(ctx->open_attrs[i],
854 ctx->offset, size, chunk);
856 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
857 return WIMLIB_ERR_NTFS_3G;
860 if (ctx->reparse_ptr)
861 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
867 ntfs_3g_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
869 struct ntfs_3g_apply_ctx *ctx = _ctx;
877 for (u32 i = 0; i < ctx->num_reparse_inodes; i++) {
878 ret = ntfs_3g_restore_reparse_point(ctx->ntfs_reparse_inodes[i],
879 ctx->wim_reparse_inodes[i],
886 if (ntfs_3g_cleanup_blob_extract(ctx) && !ret) {
887 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
888 ret = WIMLIB_ERR_NTFS_3G;
894 ntfs_3g_count_dentries(const struct list_head *dentry_list)
896 const struct wim_dentry *dentry;
899 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
901 if ((dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) &&
902 dentry_has_short_name(dentry))
912 ntfs_3g_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
914 struct ntfs_3g_apply_ctx *ctx = (struct ntfs_3g_apply_ctx *)_ctx;
916 struct wim_dentry *root;
919 /* For NTFS-3g extraction mode we require that the dentries to extract
920 * form a single tree. */
921 root = list_first_entry(dentry_list, struct wim_dentry,
922 d_extraction_list_node);
924 /* Mount the NTFS volume. */
925 vol = ntfs_mount(ctx->common.target, 0);
927 ERROR_WITH_ERRNO("Failed to mount \"%s\" with NTFS-3g",
929 return WIMLIB_ERR_NTFS_3G;
933 /* Create all inodes and aliases, including short names, and set
934 * metadata (attributes, security descriptors, and timestamps). */
936 ret = start_file_structure_phase(&ctx->common,
937 ntfs_3g_count_dentries(dentry_list));
941 ret = ntfs_3g_create_directories(root, dentry_list, ctx);
945 ret = ntfs_3g_create_nondirectories(dentry_list, ctx);
949 ret = end_file_structure_phase(&ctx->common);
954 struct read_blob_callbacks cbs = {
955 .begin_blob = ntfs_3g_begin_extract_blob,
956 .consume_chunk = ntfs_3g_extract_chunk,
957 .end_blob = ntfs_3g_end_extract_blob,
960 ret = extract_blob_list(&ctx->common, &cbs);
962 /* We do not need a final pass to set timestamps because libntfs-3g does
963 * not update timestamps automatically (exception:
964 * ntfs_set_ntfs_dos_name() does, but we handle this elsewhere). */
967 if (ntfs_umount(ctx->vol, FALSE) && !ret) {
968 ERROR_WITH_ERRNO("Failed to unmount \"%s\" with NTFS-3g",
970 ret = WIMLIB_ERR_NTFS_3G;
975 const struct apply_operations ntfs_3g_apply_ops = {
977 .get_supported_features = ntfs_3g_get_supported_features,
978 .extract = ntfs_3g_extract,
979 .context_size = sizeof(struct ntfs_3g_apply_ctx),
980 .single_tree_only = true,
984 libntfs3g_global_init(void)
986 ntfs_set_char_encoding(setlocale(LC_ALL, ""));