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/.
36 #include <ntfs-3g/attrib.h>
37 #include <ntfs-3g/reparse.h>
38 #include <ntfs-3g/security.h>
40 #include "wimlib/assert.h"
41 #include "wimlib/apply.h"
42 #include "wimlib/blob_table.h"
43 #include "wimlib/dentry.h"
44 #include "wimlib/encoding.h"
45 #include "wimlib/error.h"
46 #include "wimlib/metadata.h"
47 #include "wimlib/ntfs_3g.h"
48 #include "wimlib/reparse.h"
49 #include "wimlib/security.h"
50 #include "wimlib/security_descriptor.h"
53 ntfs_3g_get_supported_features(const char *target,
54 struct wim_features *supported_features)
56 supported_features->archive_files = 1;
57 supported_features->hidden_files = 1;
58 supported_features->system_files = 1;
59 supported_features->compressed_files = 1;
60 supported_features->not_context_indexed_files = 1;
61 supported_features->named_data_streams = 1;
62 supported_features->hard_links = 1;
63 supported_features->reparse_points = 1;
64 supported_features->security_descriptors = 1;
65 supported_features->short_names = 1;
66 supported_features->timestamps = 1;
67 supported_features->case_sensitive_filenames = 1;
71 struct ntfs_3g_apply_ctx {
72 /* Extract flags, the pointer to the WIMStruct, etc. */
73 struct apply_ctx common;
75 /* Pointer to the open NTFS volume */
78 ntfs_attr *open_attrs[MAX_OPEN_FILES];
79 unsigned num_open_attrs;
80 ntfs_inode *open_inodes[MAX_OPEN_FILES];
81 unsigned num_open_inodes;
83 struct reparse_buffer_disk rpbuf;
86 /* Offset in the blob currently being read */
89 unsigned num_reparse_inodes;
90 ntfs_inode *ntfs_reparse_inodes[MAX_OPEN_FILES];
91 struct wim_inode *wim_reparse_inodes[MAX_OPEN_FILES];
95 sid_size(const wimlib_SID *sid)
97 return offsetof(wimlib_SID, sub_authority) +
98 sizeof(le32) * sid->sub_authority_count;
102 * sd_fixup - Fix up a Windows NT security descriptor for libntfs-3g.
104 * libntfs-3g validates security descriptors before setting them, but old
105 * versions contain bugs causing it to reject unusual but valid security
108 * - Versions before 2013.1.13 reject security descriptors ending with an empty
109 * SACL (System Access Control List). This bug can be worked around either by
110 * moving the empty SACL earlier in the security descriptor or by removing the
111 * SACL entirely. The latter work-around is valid because an empty SACL is
112 * equivalent to a "null", or non-existent, SACL.
113 * - Versions up to and including 2013.1.13 reject security descriptors ending
114 * with an empty DACL (Discretionary Access Control List). This is very
115 * similar to the SACL bug and should be fixed in the next release after
116 * 2013.1.13. However, removing the DACL is not a valid workaround because
117 * this changes the meaning of the security descriptor--- an empty DACL allows
118 * no 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) the original descriptor is returned.
125 sd_fixup(const u8 *_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 =
131 (const wimlib_SECURITY_DESCRIPTOR_RELATIVE*)_desc;
132 wimlib_SECURITY_DESCRIPTOR_RELATIVE *desc_new;
133 const wimlib_SID *owner, *group, *sid;
135 /* Don't attempt to fix clearly invalid security descriptors. */
136 if (size < sizeof(wimlib_SECURITY_DESCRIPTOR_RELATIVE))
139 if (le16_to_cpu(desc->control) & wimlib_SE_DACL_PRESENT)
140 dacl_offset = le32_to_cpu(desc->dacl_offset);
144 if (le16_to_cpu(desc->control) & wimlib_SE_SACL_PRESENT)
145 sacl_offset = le32_to_cpu(desc->sacl_offset);
149 /* Check if the security descriptor will be affected by one of the bugs.
150 * If not, do nothing and return.
152 * Note: HAVE_NTFS_MNT_RDONLY is defined if libntfs-3g is
153 * version 2013.1.13 or later. */
155 #if !defined(HAVE_NTFS_MNT_RDONLY)
156 (sacl_offset != 0 && sacl_offset == size - sizeof(wimlib_ACL)) ||
158 (dacl_offset != 0 && dacl_offset == size - sizeof(wimlib_ACL))))
161 owner_offset = le32_to_cpu(desc->owner_offset);
162 group_offset = le32_to_cpu(desc->group_offset);
163 owner = (const wimlib_SID*)((const u8*)desc + owner_offset);
164 group = (const wimlib_SID*)((const u8*)desc + group_offset);
166 /* We'll try to move the owner or group SID to the end of the security
167 * descriptor to avoid the bug. This is only possible if at least one
169 owner_valid = (owner_offset != 0) &&
170 (owner_offset % 4 == 0) &&
171 (owner_offset <= size - sizeof(SID)) &&
172 (owner_offset + sid_size(owner) <= size) &&
173 (owner_offset >= sizeof(wimlib_SECURITY_DESCRIPTOR_RELATIVE));
174 group_valid = (group_offset != 0) &&
175 (group_offset % 4 == 0) &&
176 (group_offset <= size - sizeof(SID)) &&
177 (group_offset + sid_size(group) <= size) &&
178 (group_offset >= sizeof(wimlib_SECURITY_DESCRIPTOR_RELATIVE));
181 } else if (group_valid) {
187 desc_new = MALLOC(size + sid_size(sid));
191 memcpy(desc_new, desc, size);
193 desc_new->owner_offset = cpu_to_le32(size);
194 else if (group_valid)
195 desc_new->group_offset = cpu_to_le32(size);
196 memcpy((u8*)desc_new + size, sid, sid_size(sid));
197 *size_p = size + sid_size(sid);
198 return (u8*)desc_new;
201 /* Set the security descriptor @desc of size @desc_size on the NTFS inode @ni.
204 ntfs_3g_set_security_descriptor(ntfs_inode *ni, const void *desc, size_t desc_size)
206 struct SECURITY_CONTEXT sec_ctx;
210 memset(&sec_ctx, 0, sizeof(sec_ctx));
211 sec_ctx.vol = ni->vol;
213 desc_fixed = sd_fixup(desc, &desc_size);
215 if (ntfs_set_ntfs_acl(&sec_ctx, ni, desc_fixed, desc_size, 0))
216 ret = WIMLIB_ERR_SET_SECURITY;
218 if (desc_fixed != desc)
225 ntfs_3g_set_timestamps(ntfs_inode *ni, const struct wim_inode *inode)
228 inode->i_creation_time,
229 inode->i_last_write_time,
230 inode->i_last_access_time,
233 if (ntfs_inode_set_times(ni, (const char *)times, sizeof(times), 0))
234 return WIMLIB_ERR_SET_TIMESTAMPS;
238 /* Restore the timestamps on the NTFS inode corresponding to @inode. */
240 ntfs_3g_restore_timestamps(ntfs_volume *vol, const struct wim_inode *inode)
245 ni = ntfs_inode_open(vol, inode->i_mft_no);
249 res = ntfs_3g_set_timestamps(ni, inode);
251 if (ntfs_inode_close(ni) || res)
257 ERROR_WITH_ERRNO("Failed to update timestamps of \"%s\" in NTFS volume",
258 dentry_full_path(inode_first_extraction_dentry(inode)));
259 return WIMLIB_ERR_SET_TIMESTAMPS;
262 /* Restore the DOS name of the @dentry.
263 * This closes both @ni and @dir_ni.
264 * If either is NULL, then they are opened temporarily. */
266 ntfs_3g_restore_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni,
267 struct wim_dentry *dentry, ntfs_volume *vol)
270 const char *dos_name;
271 size_t dos_name_nbytes;
273 /* Note: ntfs_set_ntfs_dos_name() closes both inodes (even if it fails).
274 * And it takes in a multibyte string, even though it translates it to
275 * UTF-16LE internally... which is annoying because we currently have
276 * the UTF-16LE string but not the multibyte string. */
278 ret = utf16le_get_tstr(dentry->short_name, dentry->short_name_nbytes,
279 &dos_name, &dos_name_nbytes);
284 dir_ni = ntfs_inode_open(vol, dentry->d_parent->d_inode->i_mft_no);
286 ni = ntfs_inode_open(vol, dentry->d_inode->i_mft_no);
288 ret = ntfs_set_ntfs_dos_name(ni, dir_ni,
289 dos_name, dos_name_nbytes, 0);
295 utf16le_put_tstr(dos_name);
297 ERROR_WITH_ERRNO("Failed to set DOS name of \"%s\" in NTFS "
298 "volume", dentry_full_path(dentry));
299 ret = WIMLIB_ERR_SET_SHORT_NAME;
303 /* Unlike most other NTFS-3g functions, ntfs_set_ntfs_dos_name()
304 * changes the directory's last modification timestamp...
306 return ntfs_3g_restore_timestamps(vol, dentry->d_parent->d_inode);
309 /* ntfs_inode_close() can take a NULL argument, but it's probably best
310 * not to rely on this behavior. */
312 ntfs_inode_close(ni);
314 ntfs_inode_close(dir_ni);
319 * Create empty named data streams for the specified file, if there are any.
321 * Since these won't have blob descriptors, they won't show up in the call to
322 * extract_blob_list(). Hence the need for the special case.
325 ntfs_3g_create_empty_named_data_streams(ntfs_inode *ni,
326 const struct wim_inode *inode,
327 const struct ntfs_3g_apply_ctx *ctx)
329 for (unsigned i = 0; i < inode->i_num_streams; i++) {
331 const struct wim_inode_stream *strm = &inode->i_streams[i];
333 if (!stream_is_named_data_stream(strm) ||
334 stream_blob_resolved(strm) != NULL)
337 if (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
338 utf16le_len_chars(strm->stream_name),
341 ERROR_WITH_ERRNO("Failed to create named data stream "
342 "of \"%s\"", dentry_full_path(
343 inode_first_extraction_dentry(inode)));
344 return WIMLIB_ERR_NTFS_3G;
350 /* Set attributes, security descriptor, and timestamps on the NTFS inode @ni.
353 ntfs_3g_set_metadata(ntfs_inode *ni, const struct wim_inode *inode,
354 const struct ntfs_3g_apply_ctx *ctx)
357 const struct wim_security_data *sd;
358 struct wim_dentry *one_dentry;
361 extract_flags = ctx->common.extract_flags;
362 sd = wim_get_current_security_data(ctx->common.wim);
363 one_dentry = inode_first_extraction_dentry(inode);
366 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
367 u32 attrib = inode->i_attributes;
369 attrib &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
370 FILE_ATTRIBUTE_ENCRYPTED);
372 if (ntfs_set_ntfs_attrib(ni, (const char *)&attrib,
375 ERROR_WITH_ERRNO("Failed to set attributes on \"%s\" "
377 dentry_full_path(one_dentry));
378 return WIMLIB_ERR_SET_ATTRIBUTES;
382 /* Security descriptor */
383 if ((inode->i_security_id >= 0)
384 && !(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
389 desc = sd->descriptors[inode->i_security_id];
390 desc_size = sd->sizes[inode->i_security_id];
392 ret = ntfs_3g_set_security_descriptor(ni, desc, desc_size);
394 if (wimlib_print_errors) {
395 ERROR_WITH_ERRNO("Failed to set security descriptor "
396 "on \"%s\" in NTFS volume",
397 dentry_full_path(one_dentry));
398 fprintf(wimlib_error_file,
399 "The security descriptor is: ");
400 print_byte_field(desc, desc_size, wimlib_error_file);
401 fprintf(wimlib_error_file, "\n");
408 ret = ntfs_3g_set_timestamps(ni, inode);
410 ERROR_WITH_ERRNO("Failed to set timestamps on \"%s\" "
412 dentry_full_path(one_dentry));
418 /* Recursively creates all the subdirectories of @dir, which has been created as
419 * the NTFS inode @dir_ni. */
421 ntfs_3g_create_dirs_recursive(ntfs_inode *dir_ni, struct wim_dentry *dir,
422 struct ntfs_3g_apply_ctx *ctx)
424 struct wim_dentry *child;
426 for_dentry_child(child, dir) {
430 if (!(child->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
432 if (!will_extract_dentry(child))
435 ni = ntfs_create(dir_ni, 0, child->d_extraction_name,
436 child->d_extraction_name_nchars, S_IFDIR);
438 ERROR_WITH_ERRNO("Error creating \"%s\" in NTFS volume",
439 dentry_full_path(child));
440 return WIMLIB_ERR_NTFS_3G;
443 child->d_inode->i_mft_no = ni->mft_no;
445 ret = report_file_created(&ctx->common);
447 ret = ntfs_3g_set_metadata(ni, child->d_inode, ctx);
449 ret = ntfs_3g_create_empty_named_data_streams(ni, child->d_inode, ctx);
451 ret = ntfs_3g_create_dirs_recursive(ni, child, ctx);
453 if (ntfs_inode_close_in_dir(ni, dir_ni) && !ret) {
454 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
455 dentry_full_path(child));
456 ret = WIMLIB_ERR_NTFS_3G;
464 /* For each WIM dentry in the @root tree that represents a directory, create the
465 * corresponding directory in the NTFS volume @ctx->vol. */
467 ntfs_3g_create_directories(struct wim_dentry *root,
468 struct list_head *dentry_list,
469 struct ntfs_3g_apply_ctx *ctx)
473 struct wim_dentry *dentry;
475 /* Create the directories using POSIX names. */
477 root_ni = ntfs_inode_open(ctx->vol, FILE_root);
479 ERROR_WITH_ERRNO("Can't open root of NTFS volume");
480 return WIMLIB_ERR_NTFS_3G;
483 root->d_inode->i_mft_no = FILE_root;
485 ret = ntfs_3g_create_dirs_recursive(root_ni, root, ctx);
487 if (ntfs_inode_close(root_ni) && !ret) {
488 ERROR_WITH_ERRNO("Error closing root of NTFS volume");
489 ret = WIMLIB_ERR_NTFS_3G;
494 /* Set the DOS name of any directory that has one. */
495 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
496 if (!(dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
498 if (!dentry_has_short_name(dentry))
500 ret = ntfs_3g_restore_dos_name(NULL, NULL, dentry, ctx->vol);
503 ret = report_file_created(&ctx->common);
510 /* When creating an inode that will have a short (DOS) name, we create it using
511 * the long name associated with the short name. This ensures that the short
512 * name gets associated with the correct long name. */
513 static struct wim_dentry *
514 ntfs_3g_first_extraction_alias(struct wim_inode *inode)
516 struct list_head *next = inode->i_extraction_aliases.next;
517 struct wim_dentry *dentry;
520 dentry = list_entry(next, struct wim_dentry,
521 d_extraction_alias_node);
522 if (dentry_has_short_name(dentry))
525 } while (next != &inode->i_extraction_aliases);
530 * Add a hard link for the NTFS inode @ni at the location corresponding to the
531 * WIM dentry @dentry.
533 * The parent directory must have already been created on the NTFS volume.
535 * Returns 0 on success; returns WIMLIB_ERR_NTFS_3G and sets errno on failure.
538 ntfs_3g_add_link(ntfs_inode *ni, struct wim_dentry *dentry)
543 /* Open the inode of the parent directory. */
544 dir_ni = ntfs_inode_open(ni->vol, dentry->d_parent->d_inode->i_mft_no);
548 /* Create the link. */
549 res = ntfs_link(ni, dir_ni, dentry->d_extraction_name,
550 dentry->d_extraction_name_nchars);
552 /* Close the parent directory. */
553 if (ntfs_inode_close(dir_ni) || res)
559 ERROR_WITH_ERRNO("Can't create link \"%s\" in NTFS volume",
560 dentry_full_path(dentry));
561 return WIMLIB_ERR_NTFS_3G;
565 ntfs_3g_create_nondirectory(struct wim_inode *inode,
566 const struct ntfs_3g_apply_ctx *ctx)
568 struct wim_dentry *first_dentry;
571 struct list_head *next;
572 struct wim_dentry *dentry;
575 first_dentry = ntfs_3g_first_extraction_alias(inode);
577 /* Create first link. */
579 dir_ni = ntfs_inode_open(ctx->vol, first_dentry->d_parent->d_inode->i_mft_no);
581 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
582 dentry_full_path(first_dentry->d_parent));
583 return WIMLIB_ERR_NTFS_3G;
586 ni = ntfs_create(dir_ni, 0, first_dentry->d_extraction_name,
587 first_dentry->d_extraction_name_nchars, S_IFREG);
590 ERROR_WITH_ERRNO("Can't create \"%s\" in NTFS volume",
591 dentry_full_path(first_dentry));
592 ntfs_inode_close(dir_ni);
593 return WIMLIB_ERR_NTFS_3G;
596 inode->i_mft_no = ni->mft_no;
598 /* Set short name if present. */
599 if (dentry_has_short_name(first_dentry)) {
601 ret = ntfs_3g_restore_dos_name(ni, dir_ni, first_dentry, ctx->vol);
603 /* ntfs_3g_restore_dos_name() closed both 'ni' and 'dir_ni'. */
608 /* Reopen the inode. */
609 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
611 ERROR_WITH_ERRNO("Failed to reopen \"%s\" "
613 dentry_full_path(first_dentry));
614 return WIMLIB_ERR_NTFS_3G;
617 /* Close the directory in which the first link was created. */
618 if (ntfs_inode_close(dir_ni)) {
619 ERROR_WITH_ERRNO("Failed to close \"%s\" in NTFS volume",
620 dentry_full_path(first_dentry->d_parent));
621 ret = WIMLIB_ERR_NTFS_3G;
626 /* Create additional links if present. */
627 next = inode->i_extraction_aliases.next;
629 dentry = list_entry(next, struct wim_dentry,
630 d_extraction_alias_node);
631 if (dentry != first_dentry) {
632 ret = ntfs_3g_add_link(ni, dentry);
637 } while (next != &inode->i_extraction_aliases);
640 ret = ntfs_3g_set_metadata(ni, inode, ctx);
644 ret = ntfs_3g_create_empty_named_data_streams(ni, inode, ctx);
647 /* Close the inode. */
648 if (ntfs_inode_close(ni) && !ret) {
649 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
650 dentry_full_path(first_dentry));
651 ret = WIMLIB_ERR_NTFS_3G;
656 /* For each WIM dentry in the @dentry_list that represents a nondirectory file,
657 * create the corresponding nondirectory file in the NTFS volume.
659 * Directories must have already been created. */
661 ntfs_3g_create_nondirectories(struct list_head *dentry_list,
662 struct ntfs_3g_apply_ctx *ctx)
664 struct wim_dentry *dentry;
665 struct wim_inode *inode;
668 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
669 inode = dentry->d_inode;
670 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
672 if (dentry == inode_first_extraction_dentry(inode)) {
673 ret = ntfs_3g_create_nondirectory(inode, ctx);
677 ret = report_file_created(&ctx->common);
685 ntfs_3g_begin_extract_blob_instance(struct blob_descriptor *blob,
687 struct wim_inode *inode,
688 const struct wim_inode_stream *strm,
689 struct ntfs_3g_apply_ctx *ctx)
691 struct wim_dentry *one_dentry = inode_first_extraction_dentry(inode);
692 size_t stream_name_nchars;
695 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
697 if (blob->size > REPARSE_DATA_MAX_SIZE) {
698 ERROR("Reparse data of \"%s\" has size "
699 "%"PRIu64" bytes (exceeds %u bytes)",
700 dentry_full_path(one_dentry),
701 blob->size, REPARSE_DATA_MAX_SIZE);
702 return WIMLIB_ERR_INVALID_REPARSE_DATA;
704 ctx->reparse_ptr = ctx->rpbuf.rpdata;
705 ctx->rpbuf.rpdatalen = cpu_to_le16(blob->size);
706 ctx->rpbuf.rpreserved = cpu_to_le16(0);
707 ctx->ntfs_reparse_inodes[ctx->num_reparse_inodes] = ni;
708 ctx->wim_reparse_inodes[ctx->num_reparse_inodes] = inode;
709 ctx->num_reparse_inodes++;
713 /* It's a data stream (may be unnamed or named). */
714 wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
716 stream_name_nchars = utf16le_len_chars(strm->stream_name);
718 if (stream_name_nchars &&
719 (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
720 stream_name_nchars, NULL, 0)))
722 ERROR_WITH_ERRNO("Failed to create named data stream of \"%s\"",
723 dentry_full_path(one_dentry));
724 return WIMLIB_ERR_NTFS_3G;
727 /* This should be ensured by extract_blob_list() */
728 wimlib_assert(ctx->num_open_attrs < MAX_OPEN_FILES);
730 attr = ntfs_attr_open(ni, AT_DATA, strm->stream_name,
733 ERROR_WITH_ERRNO("Failed to open data stream of \"%s\"",
734 dentry_full_path(one_dentry));
735 return WIMLIB_ERR_NTFS_3G;
737 ctx->open_attrs[ctx->num_open_attrs++] = attr;
738 ntfs_attr_truncate_solid(attr, blob->size);
743 ntfs_3g_cleanup_blob_extract(struct ntfs_3g_apply_ctx *ctx)
747 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
748 if (ntfs_attr_pclose(ctx->open_attrs[i]))
750 ntfs_attr_close(ctx->open_attrs[i]);
753 ctx->num_open_attrs = 0;
755 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
756 if (ntfs_inode_close(ctx->open_inodes[i]))
759 ctx->num_open_inodes = 0;
762 ctx->reparse_ptr = NULL;
763 ctx->num_reparse_inodes = 0;
768 ntfs_3g_open_inode(struct wim_inode *inode, struct ntfs_3g_apply_ctx *ctx)
770 ntfs_inode *ni = NULL;
772 if (inode->i_visited) {
773 for (u32 i = 0; i < ctx->num_open_inodes; i++) {
774 if (ctx->open_inodes[i]->mft_no == inode->i_mft_no) {
775 ni = ctx->open_inodes[i];
781 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
782 ctx->open_inodes[ctx->num_open_inodes++] = ni;
783 inode->i_visited = 1;
787 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
789 inode_first_extraction_dentry(inode)));
796 ntfs_3g_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
798 struct ntfs_3g_apply_ctx *ctx = _ctx;
799 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
803 for (u32 i = 0; i < blob->out_refcnt; i++) {
804 ret = WIMLIB_ERR_NTFS_3G;
805 ni = ntfs_3g_open_inode(targets[i].inode, ctx);
809 ret = ntfs_3g_begin_extract_blob_instance(blob, ni,
811 targets[i].stream, ctx);
819 ntfs_3g_cleanup_blob_extract(ctx);
821 for (u32 i = 0; i < blob->out_refcnt; i++)
822 targets[i].inode->i_visited = 0;
827 ntfs_3g_extract_chunk(const void *chunk, size_t size, void *_ctx)
829 struct ntfs_3g_apply_ctx *ctx = _ctx;
832 for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
833 res = ntfs_attr_pwrite(ctx->open_attrs[i],
834 ctx->offset, size, chunk);
836 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
837 return WIMLIB_ERR_NTFS_3G;
840 if (ctx->reparse_ptr)
841 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
847 ntfs_3g_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
849 struct ntfs_3g_apply_ctx *ctx = _ctx;
857 for (u32 i = 0; i < ctx->num_reparse_inodes; i++) {
858 struct wim_inode *inode = ctx->wim_reparse_inodes[i];
860 ctx->rpbuf.rptag = cpu_to_le32(inode->i_reparse_tag);
862 if (ntfs_set_ntfs_reparse_data(ctx->ntfs_reparse_inodes[i],
863 (const char *)&ctx->rpbuf,
864 blob->size + REPARSE_DATA_OFFSET,
867 ERROR_WITH_ERRNO("Failed to set reparse "
870 inode_first_extraction_dentry(inode)));
871 ret = WIMLIB_ERR_NTFS_3G;
877 if (ntfs_3g_cleanup_blob_extract(ctx) && !ret) {
878 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
879 ret = WIMLIB_ERR_NTFS_3G;
885 ntfs_3g_count_dentries(const struct list_head *dentry_list)
887 const struct wim_dentry *dentry;
890 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
892 if ((dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) &&
893 dentry_has_short_name(dentry))
903 ntfs_3g_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
905 struct ntfs_3g_apply_ctx *ctx = (struct ntfs_3g_apply_ctx *)_ctx;
907 struct wim_dentry *root;
910 /* For NTFS-3g extraction mode we require that the dentries to extract
911 * form a single tree. */
912 root = list_first_entry(dentry_list, struct wim_dentry,
913 d_extraction_list_node);
915 /* Mount the NTFS volume. */
916 vol = ntfs_mount(ctx->common.target, 0);
918 ERROR_WITH_ERRNO("Failed to mount \"%s\" with NTFS-3g",
920 return WIMLIB_ERR_NTFS_3G;
924 /* Create all inodes and aliases, including short names, and set
925 * metadata (attributes, security descriptors, and timestamps). */
927 ret = start_file_structure_phase(&ctx->common,
928 ntfs_3g_count_dentries(dentry_list));
932 ret = ntfs_3g_create_directories(root, dentry_list, ctx);
936 ret = ntfs_3g_create_nondirectories(dentry_list, ctx);
940 ret = end_file_structure_phase(&ctx->common);
945 struct read_blob_list_callbacks cbs = {
946 .begin_blob = ntfs_3g_begin_extract_blob,
947 .begin_blob_ctx = ctx,
948 .consume_chunk = ntfs_3g_extract_chunk,
949 .consume_chunk_ctx = ctx,
950 .end_blob = ntfs_3g_end_extract_blob,
953 ret = extract_blob_list(&ctx->common, &cbs);
955 /* We do not need a final pass to set timestamps because libntfs-3g does
956 * not update timestamps automatically (exception:
957 * ntfs_set_ntfs_dos_name() does, but we handle this elsewhere). */
960 if (ntfs_umount(ctx->vol, FALSE) && !ret) {
961 ERROR_WITH_ERRNO("Failed to unmount \"%s\" with NTFS-3g",
963 ret = WIMLIB_ERR_NTFS_3G;
968 const struct apply_operations ntfs_3g_apply_ops = {
970 .get_supported_features = ntfs_3g_get_supported_features,
971 .extract = ntfs_3g_extract,
972 .context_size = sizeof(struct ntfs_3g_apply_ctx),
973 .single_tree_only = true,
977 libntfs3g_global_init(void)
979 ntfs_set_char_encoding(setlocale(LC_ALL, ""));