4 * Apply a WIM image to a NTFS volume. We restore everything we can, including
5 * security data and alternate data streams.
9 * Copyright (C) 2012 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
31 #include <ntfs-3g/endians.h>
32 #include <ntfs-3g/types.h>
35 #include "wimlib_internal.h"
40 #include "lookup_table.h"
42 #include <ntfs-3g/layout.h>
43 #include <ntfs-3g/acls.h>
44 #include <ntfs-3g/attrib.h>
45 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
46 #include <ntfs-3g/xattrs.h>
47 #include <ntfs-3g/reparse.h>
51 struct ntfs_apply_args {
59 extern int ntfs_set_inode_security(ntfs_inode *ni, u32 selection,
61 extern int ntfs_set_inode_attributes(ntfs_inode *ni, u32 attrib);
65 * Extracts a WIM resource to a NTFS attribute.
68 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
71 u64 bytes_remaining = wim_resource_size(lte);
72 u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
75 u8 hash[SHA1_HASH_SIZE];
80 while (bytes_remaining) {
81 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
82 ret = read_wim_resource(lte, buf, to_read, offset, false);
85 sha1_update(&ctx, buf, to_read);
86 if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
87 ERROR_WITH_ERRNO("Error extracting WIM resource");
88 return WIMLIB_ERR_WRITE;
90 bytes_remaining -= to_read;
93 sha1_final(hash, &ctx);
94 if (!hashes_equal(hash, lte->hash)) {
95 ERROR("Invalid checksum on a WIM resource "
96 "(detected when extracting to NTFS stream)");
97 ERROR("The following WIM resource is invalid:");
98 print_lookup_table_entry(lte);
99 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
104 /* Writes the data streams to a NTFS file
106 * @ni: The NTFS inode for the file.
107 * @inode: The WIM dentry that has an inode containing the streams.
108 * @w: The WIMStruct for the WIM containing the image we are applying.
110 * Returns 0 on success, nonzero on failure.
112 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
116 unsigned stream_idx = 0;
117 ntfschar *stream_name = AT_UNNAMED;
118 u32 stream_name_len = 0;
119 const struct inode *inode = dentry->d_inode;
121 DEBUG("Writing %u NTFS data stream%s for `%s'",
123 (inode->num_ads == 0 ? "" : "s"),
124 dentry->full_path_utf8);
127 struct lookup_table_entry *lte;
130 lte = inode_stream_lte(inode, stream_idx, w->lookup_table);
132 if (stream_name_len) {
133 /* Create an empty named stream. */
134 ret = ntfs_attr_add(ni, AT_DATA, stream_name,
135 stream_name_len, NULL, 0);
137 ERROR_WITH_ERRNO("Failed to create name data "
138 "stream for extracted file "
140 dentry->full_path_utf8);
141 ret = WIMLIB_ERR_NTFS_3G;
146 /* If there's no lookup table entry, it's an empty stream.
147 * Otherwise, we must open the attribute and extract the data.
150 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
152 ERROR_WITH_ERRNO("Failed to open a data stream of "
153 "extracted file `%s'",
154 dentry->full_path_utf8);
155 ret = WIMLIB_ERR_NTFS_3G;
158 ret = extract_wim_resource_to_ntfs_attr(lte, na);
163 if (stream_idx == inode->num_ads)
165 stream_name = (ntfschar*)inode->ads_entries[stream_idx].stream_name;
166 stream_name_len = inode->ads_entries[stream_idx].stream_name_len / 2;
173 * Makes a NTFS hard link
175 * It is named @from_dentry->file_name and is located under the directory
176 * specified by @dir_ni, and it is made to point to the previously extracted
177 * file located at @inode->extracted_file.
179 * Return 0 on success, nonzero on failure.
181 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
182 const struct inode *inode,
184 ntfs_inode **to_ni_ret)
189 const char *dir_name;
194 wimlib_assert(dentry_is_regular_file(from_dentry)
195 && inode_is_regular_file(inode));
197 if (ntfs_inode_close(dir_ni) != 0) {
198 ERROR_WITH_ERRNO("Error closing directory");
199 return WIMLIB_ERR_NTFS_3G;
204 DEBUG("Extracting NTFS hard link `%s' => `%s'",
205 from_dentry->full_path_utf8, inode->extracted_file);
207 to_ni = ntfs_pathname_to_inode(vol, NULL, inode->extracted_file);
209 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
210 inode->extracted_file);
211 return WIMLIB_ERR_NTFS_3G;
213 p = from_dentry->full_path_utf8 + from_dentry->full_path_utf8_len;
220 dir_name = from_dentry->full_path_utf8;
222 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
224 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
225 from_dentry->full_path_utf8);
227 return WIMLIB_ERR_NTFS_3G;
231 ret = ntfs_link(to_ni, dir_ni,
232 (ntfschar*)from_dentry->file_name,
233 from_dentry->file_name_len / 2);
235 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
236 from_dentry->full_path_utf8,
237 inode->extracted_file);
238 ret = WIMLIB_ERR_NTFS_3G;
245 apply_file_attributes_and_security_data(ntfs_inode *ni,
247 const struct dentry *dentry,
250 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
251 dentry->full_path_utf8, dentry->d_inode->attributes);
253 struct SECURITY_CONTEXT ctx;
255 attributes_le32 = cpu_to_le32(dentry->d_inode->attributes);
256 memset(&ctx, 0, sizeof(ctx));
258 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
260 (const char*)&attributes_le32,
263 ERROR("Failed to set NTFS file attributes on `%s'",
264 dentry->full_path_utf8);
265 return WIMLIB_ERR_NTFS_3G;
267 if (dentry->d_inode->security_id != -1) {
268 const struct wim_security_data *sd;
269 const char *descriptor;
271 sd = wim_const_security_data(w);
272 wimlib_assert(dentry->d_inode->security_id < sd->num_entries);
273 descriptor = sd->descriptors[dentry->d_inode->security_id];
274 DEBUG("Applying security descriptor %d to `%s'",
275 dentry->d_inode->security_id, dentry->full_path_utf8);
277 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
278 ni, dir_ni, descriptor,
279 sd->sizes[dentry->d_inode->security_id], 0);
282 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
283 dentry->full_path_utf8);
284 return WIMLIB_ERR_NTFS_3G;
290 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
293 struct lookup_table_entry *lte;
296 wimlib_assert(dentry->d_inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
298 lte = inode_unnamed_lte(dentry->d_inode, w->lookup_table);
300 DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
303 ERROR("Could not find reparse data for `%s'",
304 dentry->full_path_utf8);
305 return WIMLIB_ERR_INVALID_DENTRY;
308 if (wim_resource_size(lte) >= 0xffff) {
309 ERROR("Reparse data of `%s' is too long (%lu bytes)",
310 dentry->full_path_utf8, wim_resource_size(lte));
311 return WIMLIB_ERR_INVALID_DENTRY;
314 u8 reparse_data_buf[8 + wim_resource_size(lte)];
315 u8 *p = reparse_data_buf;
316 p = put_u32(p, dentry->d_inode->reparse_tag); /* ReparseTag */
317 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
318 p = put_u16(p, 0); /* Reserved */
320 ret = read_full_wim_resource(lte, p);
324 ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
325 wim_resource_size(lte) + 8, 0);
327 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
328 dentry->full_path_utf8);
329 return WIMLIB_ERR_NTFS_3G;
334 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
338 * If @dentry is part of a hard link group, search for hard-linked dentries in
339 * the same directory that have a nonempty DOS (short) filename. There should
340 * be exactly 0 or 1 such dentries. If there is 1, extract that dentry first,
341 * so that the DOS name is correctly associated with the corresponding long name
342 * in the Win32 namespace, and not any of the additional names in the POSIX
343 * namespace created from hard links.
345 static int preapply_dentry_with_dos_name(struct dentry *dentry,
346 ntfs_inode **dir_ni_p,
349 struct dentry *other;
350 struct dentry *dentry_with_dos_name;
352 dentry_with_dos_name = NULL;
353 inode_for_each_dentry(other, dentry->d_inode) {
354 if (other != dentry && (dentry->parent == other->parent)
355 && other->short_name_len)
357 if (dentry_with_dos_name) {
358 ERROR("Found multiple DOS names for file `%s' "
359 "in the same directory",
360 dentry_with_dos_name->full_path_utf8);
361 return WIMLIB_ERR_INVALID_DENTRY;
363 dentry_with_dos_name = other;
366 /* If there's a dentry with a DOS name, extract it first */
367 if (dentry_with_dos_name && !dentry_is_extracted(dentry)) {
369 const char *dir_name;
372 ntfs_volume *vol = (*dir_ni_p)->vol;
374 DEBUG("pre-applying DOS name `%s'",
375 dentry_with_dos_name->full_path_utf8);
376 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
380 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
387 dir_name = dentry->full_path_utf8;
389 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
392 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
394 return WIMLIB_ERR_NTFS_3G;
401 * Applies a WIM dentry to a NTFS filesystem.
403 * @dentry: The WIM dentry to apply
404 * @dir_ni: The NTFS inode for the parent directory
405 * @w: The WIMStruct for the WIM containing the image we are applying.
407 * @return: 0 on success; nonzero on failure.
409 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
414 ntfs_inode *ni = NULL;
415 bool is_hardlink = false;
416 ntfs_volume *vol = dir_ni->vol;
417 struct inode *inode = dentry->d_inode;
418 dentry->is_extracted = true;
420 if (inode->attributes & FILE_ATTRIBUTE_DIRECTORY) {
423 struct dentry *other;
425 /* Apply hard-linked directory in same directory with DOS name
426 * (if there is one) before this dentry */
427 if (dentry->short_name_len == 0) {
428 ret = preapply_dentry_with_dos_name(dentry,
436 if (inode->link_count > 1) {
437 /* Already extracted another dentry in the hard link
438 * group. We can make a hard link instead of extracting
440 if (inode->extracted_file) {
441 ret = wim_apply_hardlink_ntfs(dentry, inode,
445 goto out_close_dir_ni;
447 goto out_set_dos_name;
449 /* Can't make a hard link; extract the file itself */
450 FREE(inode->extracted_file);
451 inode->extracted_file = STRDUP(dentry->full_path_utf8);
452 if (!inode->extracted_file)
453 return WIMLIB_ERR_NOMEM;
458 * Create a directory or file.
460 * Note: For symbolic links that are not directory junctions, pass
461 * S_IFREG here, since we manually set the reparse data later.
463 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
464 dentry->file_name_len / 2, type);
467 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
468 dentry->full_path_utf8);
469 ret = WIMLIB_ERR_NTFS_3G;
470 goto out_close_dir_ni;
473 /* Write the data streams, unless this is a directory or reparse point
475 if (!(inode->attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
476 FILE_ATTRIBUTE_DIRECTORY))) {
477 ret = write_ntfs_data_streams(ni, dentry, w);
479 goto out_close_dir_ni;
483 ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry, w);
485 goto out_close_dir_ni;
487 if (inode->attributes & FILE_ATTR_REPARSE_POINT) {
488 ret = apply_reparse_data(ni, dentry, w);
490 goto out_close_dir_ni;
494 /* Set DOS (short) name if given */
495 if (dentry->short_name_len != 0) {
497 char *short_name_utf8;
498 size_t short_name_utf8_len;
499 short_name_utf8 = utf16_to_utf8(dentry->short_name,
500 dentry->short_name_len,
501 &short_name_utf8_len);
502 if (!short_name_utf8) {
503 ERROR("Out of memory");
504 ret = WIMLIB_ERR_NOMEM;
505 goto out_close_dir_ni;
511 const char *dir_name;
513 /* ntfs_set_ntfs_dos_name() closes the inodes in the
514 * wrong order if we have applied a hard link. Close
515 * them ourselves, then re-open then. */
516 if (ntfs_inode_close(dir_ni) != 0) {
518 ret = WIMLIB_ERR_NTFS_3G;
519 ERROR_WITH_ERRNO("Failed to close directory inode");
521 if (ntfs_inode_close(ni) != 0) {
523 ret = WIMLIB_ERR_NTFS_3G;
524 ERROR_WITH_ERRNO("Failed to close hard link target inode");
526 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
533 dir_name = dentry->full_path_utf8;
535 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
538 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
540 return WIMLIB_ERR_NTFS_3G;
542 ni = ntfs_pathname_to_inode(vol, dir_ni,
543 dentry->file_name_utf8);
545 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
547 return WIMLIB_ERR_NTFS_3G;
551 DEBUG("Setting short (DOS) name of `%s' to %s",
552 dentry->full_path_utf8, short_name_utf8);
554 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
555 short_name_utf8_len, 0);
556 FREE(short_name_utf8);
558 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
559 dentry->full_path_utf8);
560 ret = WIMLIB_ERR_NTFS_3G;
562 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
567 if (ntfs_inode_close(dir_ni) != 0) {
569 ret = WIMLIB_ERR_NTFS_3G;
570 ERROR_WITH_ERRNO("Failed to close directory inode");
572 if (ni && ntfs_inode_close(ni) != 0) {
574 ret = WIMLIB_ERR_NTFS_3G;
575 ERROR_WITH_ERRNO("Failed to close inode");
580 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
587 wimlib_assert(dentry_is_directory(dentry));
588 ni = ntfs_pathname_to_inode(vol, NULL, "/");
590 ERROR_WITH_ERRNO("Could not find root NTFS inode");
591 return WIMLIB_ERR_NTFS_3G;
593 ret = apply_file_attributes_and_security_data(ni, ni, dentry, w);
594 if (ntfs_inode_close(ni) != 0) {
595 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
597 ret = WIMLIB_ERR_NTFS_3G;
602 /* Applies a WIM dentry to the NTFS volume */
603 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
605 struct ntfs_apply_args *args = arg;
606 ntfs_volume *vol = args->vol;
607 int extract_flags = args->extract_flags;
608 WIMStruct *w = args->w;
612 const char *dir_name;
614 if (dentry_is_extracted(dentry))
617 wimlib_assert(dentry->full_path_utf8);
619 DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
621 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
622 puts(dentry->full_path_utf8);
624 if (dentry_is_root(dentry))
625 return wim_apply_root_dentry_ntfs(dentry, vol, w);
627 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
634 dir_name = dentry->full_path_utf8;
636 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
639 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
641 return WIMLIB_ERR_NTFS_3G;
643 return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
646 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
648 struct ntfs_apply_args *args = arg;
649 ntfs_volume *vol = args->vol;
656 DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
658 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
660 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
661 dentry->full_path_utf8);
662 return WIMLIB_ERR_NTFS_3G;
666 p = put_u64(p, dentry->d_inode->creation_time);
667 p = put_u64(p, dentry->d_inode->last_write_time);
668 p = put_u64(p, dentry->d_inode->last_access_time);
669 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
671 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
672 dentry->full_path_utf8);
673 ret = WIMLIB_ERR_NTFS_3G;
676 if (ntfs_inode_close(ni) != 0) {
678 ret = WIMLIB_ERR_NTFS_3G;
679 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
680 dentry->full_path_utf8);
685 static int dentry_set_unextracted(struct dentry *dentry, void *ignore)
687 dentry->is_extracted = false;
691 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
696 struct ntfs_apply_args args;
698 DEBUG("Mounting NTFS volume `%s'", device);
699 vol = ntfs_mount(device, 0);
701 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
702 return WIMLIB_ERR_NTFS_3G;
705 args.extract_flags = extract_flags;
707 root = wim_root_dentry(w);
709 for_dentry_in_tree(root, dentry_set_unextracted, NULL);
710 ret = for_dentry_in_tree(root, wim_apply_dentry_ntfs, &args);
714 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
715 printf("Setting timestamps of extracted files on NTFS "
716 "volume `%s'\n", device);
717 ret = for_dentry_in_tree_depth(root, wim_apply_dentry_timestamps,
720 if (ret == 0 && (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE))
721 printf("Finished applying image %d of %s to NTFS "
724 w->filename ? w->filename : "WIM",
727 DEBUG("Unmounting NTFS volume `%s'", device);
728 if (ntfs_umount(vol, FALSE) != 0) {
729 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
731 ret = WIMLIB_ERR_NTFS_3G;
738 * API entry point for applying a WIM image to a NTFS volume.
740 * Please note that this is a NTFS *volume* and not a directory. The intention
741 * is that the volume contain an empty filesystem, and the WIM image contain a
742 * full filesystem to be applied to the volume.
744 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
745 const char *device, int flags,
746 WIMStruct **additional_swms,
747 unsigned num_additional_swms)
749 struct lookup_table *joined_tab, *w_tab_save;
752 DEBUG("w->filename = %s, image = %d, device = %s, flags = 0x%x, "
753 "num_additional_swms = %u",
754 w->filename, image, device, flags, num_additional_swms);
757 return WIMLIB_ERR_INVALID_PARAM;
758 if (image == WIM_ALL_IMAGES) {
759 ERROR("Can only apply a single image when applying "
760 "directly to a NTFS volume");
761 return WIMLIB_ERR_INVALID_PARAM;
763 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
764 ERROR("Cannot specify symlink or hardlink flags when applying ");
765 ERROR("directly to a NTFS volume");
766 return WIMLIB_ERR_INVALID_PARAM;
769 ret = verify_swm_set(w, additional_swms, num_additional_swms);
773 if (num_additional_swms) {
774 ret = new_joined_lookup_table(w, additional_swms,
775 num_additional_swms, &joined_tab);
778 w_tab_save = w->lookup_table;
779 w->lookup_table = joined_tab;
782 ret = wimlib_select_image(w, image);
786 ret = do_wim_apply_image_ntfs(w, device, flags);
789 if (num_additional_swms) {
790 free_lookup_table(w->lookup_table);
791 w->lookup_table = w_tab_save;
796 #else /* WITH_NTFS_3G */
797 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
798 const char *device, int flags,
799 WIMStruct **additional_swms,
800 unsigned num_additional_swms)
802 ERROR("wimlib was compiled without support for NTFS-3g, so");
803 ERROR("we cannot apply a WIM image directly to a NTFS volume");
804 return WIMLIB_ERR_UNSUPPORTED;
806 #endif /* WITH_NTFS_3G */