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 {
58 * Extracts a WIM resource to a NTFS attribute.
61 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
64 u64 bytes_remaining = wim_resource_size(lte);
65 u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
68 u8 hash[SHA1_HASH_SIZE];
73 while (bytes_remaining) {
74 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
75 ret = read_wim_resource(lte, buf, to_read, offset, false);
78 sha1_update(&ctx, buf, to_read);
79 if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
80 ERROR_WITH_ERRNO("Error extracting WIM resource");
81 return WIMLIB_ERR_WRITE;
83 bytes_remaining -= to_read;
86 sha1_final(hash, &ctx);
87 if (!hashes_equal(hash, lte->hash)) {
88 ERROR("Invalid checksum on a WIM resource "
89 "(detected when extracting to NTFS stream)");
90 ERROR("The following WIM resource is invalid:");
91 print_lookup_table_entry(lte);
92 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
97 /* Writes the data streams to a NTFS file
99 * @ni: The NTFS inode for the file.
100 * @inode: The WIM dentry that has an inode containing the streams.
101 * @w: The WIMStruct for the WIM containing the image we are applying.
103 * Returns 0 on success, nonzero on failure.
105 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
109 unsigned stream_idx = 0;
110 ntfschar *stream_name = AT_UNNAMED;
111 u32 stream_name_len = 0;
112 const struct inode *inode = dentry->d_inode;
114 DEBUG("Writing %u NTFS data stream%s for `%s'",
116 (inode->num_ads == 0 ? "" : "s"),
117 dentry->full_path_utf8);
120 struct lookup_table_entry *lte;
123 lte = inode_stream_lte(inode, stream_idx, w->lookup_table);
125 if (stream_name_len) {
126 /* Create an empty named stream. */
127 ret = ntfs_attr_add(ni, AT_DATA, stream_name,
128 stream_name_len, NULL, 0);
130 ERROR_WITH_ERRNO("Failed to create name data "
131 "stream for extracted file "
133 dentry->full_path_utf8);
134 ret = WIMLIB_ERR_NTFS_3G;
139 /* If there's no lookup table entry, it's an empty stream.
140 * Otherwise, we must open the attribute and extract the data.
143 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
145 ERROR_WITH_ERRNO("Failed to open a data stream of "
146 "extracted file `%s'",
147 dentry->full_path_utf8);
148 ret = WIMLIB_ERR_NTFS_3G;
151 ret = extract_wim_resource_to_ntfs_attr(lte, na);
156 if (stream_idx == inode->num_ads)
158 stream_name = (ntfschar*)inode->ads_entries[stream_idx].stream_name;
159 stream_name_len = inode->ads_entries[stream_idx].stream_name_len / 2;
166 * Makes a NTFS hard link
168 * It is named @from_dentry->file_name and is located under the directory
169 * specified by @dir_ni, and it is made to point to the previously extracted
170 * file located at @inode->extracted_file.
172 * Return 0 on success, nonzero on failure.
174 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
175 const struct inode *inode,
177 ntfs_inode **to_ni_ret)
182 const char *dir_name;
187 wimlib_assert(dentry_is_regular_file(from_dentry)
188 && inode_is_regular_file(inode));
190 if (ntfs_inode_close(dir_ni) != 0) {
191 ERROR_WITH_ERRNO("Error closing directory");
192 return WIMLIB_ERR_NTFS_3G;
197 DEBUG("Extracting NTFS hard link `%s' => `%s'",
198 from_dentry->full_path_utf8, inode->extracted_file);
200 to_ni = ntfs_pathname_to_inode(vol, NULL, inode->extracted_file);
202 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
203 inode->extracted_file);
204 return WIMLIB_ERR_NTFS_3G;
206 p = from_dentry->full_path_utf8 + from_dentry->full_path_utf8_len;
213 dir_name = from_dentry->full_path_utf8;
215 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
217 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
218 from_dentry->full_path_utf8);
220 return WIMLIB_ERR_NTFS_3G;
224 ret = ntfs_link(to_ni, dir_ni,
225 (ntfschar*)from_dentry->file_name,
226 from_dentry->file_name_len / 2);
228 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
229 from_dentry->full_path_utf8,
230 inode->extracted_file);
231 ret = WIMLIB_ERR_NTFS_3G;
238 apply_file_attributes_and_security_data(ntfs_inode *ni,
240 const struct dentry *dentry,
243 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
244 dentry->full_path_utf8, dentry->d_inode->attributes);
246 struct SECURITY_CONTEXT ctx;
248 attributes_le32 = cpu_to_le32(dentry->d_inode->attributes);
249 memset(&ctx, 0, sizeof(ctx));
251 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
253 (const char*)&attributes_le32,
256 ERROR("Failed to set NTFS file attributes on `%s'",
257 dentry->full_path_utf8);
258 return WIMLIB_ERR_NTFS_3G;
260 if (dentry->d_inode->security_id != -1) {
261 const struct wim_security_data *sd;
262 const char *descriptor;
264 sd = wim_const_security_data(w);
265 wimlib_assert(dentry->d_inode->security_id < sd->num_entries);
266 descriptor = sd->descriptors[dentry->d_inode->security_id];
267 DEBUG("Applying security descriptor %d to `%s'",
268 dentry->d_inode->security_id, dentry->full_path_utf8);
270 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
271 ni, dir_ni, descriptor,
272 sd->sizes[dentry->d_inode->security_id], 0);
275 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
276 dentry->full_path_utf8);
277 return WIMLIB_ERR_NTFS_3G;
283 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
286 struct lookup_table_entry *lte;
289 wimlib_assert(dentry->d_inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
291 lte = inode_unnamed_lte(dentry->d_inode, w->lookup_table);
293 DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
296 ERROR("Could not find reparse data for `%s'",
297 dentry->full_path_utf8);
298 return WIMLIB_ERR_INVALID_DENTRY;
301 if (wim_resource_size(lte) >= 0xffff) {
302 ERROR("Reparse data of `%s' is too long (%lu bytes)",
303 dentry->full_path_utf8, wim_resource_size(lte));
304 return WIMLIB_ERR_INVALID_DENTRY;
307 u8 reparse_data_buf[8 + wim_resource_size(lte)];
308 u8 *p = reparse_data_buf;
309 p = put_u32(p, dentry->d_inode->reparse_tag); /* ReparseTag */
310 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
311 p = put_u16(p, 0); /* Reserved */
313 ret = read_full_wim_resource(lte, p);
317 ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
318 wim_resource_size(lte) + 8, 0);
320 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
321 dentry->full_path_utf8);
322 return WIMLIB_ERR_NTFS_3G;
327 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
331 * If @dentry is part of a hard link group, search for hard-linked dentries in
332 * the same directory that have a nonempty DOS (short) filename. There should
333 * be exactly 0 or 1 such dentries. If there is 1, extract that dentry first,
334 * so that the DOS name is correctly associated with the corresponding long name
335 * in the Win32 namespace, and not any of the additional names in the POSIX
336 * namespace created from hard links.
338 static int preapply_dentry_with_dos_name(struct dentry *dentry,
339 ntfs_inode **dir_ni_p,
342 struct dentry *other;
343 struct dentry *dentry_with_dos_name;
345 dentry_with_dos_name = NULL;
346 inode_for_each_dentry(other, dentry->d_inode) {
347 if (other != dentry && (dentry->parent == other->parent)
348 && other->short_name_len)
350 if (dentry_with_dos_name) {
351 ERROR("Found multiple DOS names for file `%s' "
352 "in the same directory",
353 dentry_with_dos_name->full_path_utf8);
354 return WIMLIB_ERR_INVALID_DENTRY;
356 dentry_with_dos_name = other;
359 /* If there's a dentry with a DOS name, extract it first */
360 if (dentry_with_dos_name && !dentry_is_extracted(dentry)) {
362 const char *dir_name;
365 ntfs_volume *vol = (*dir_ni_p)->vol;
367 DEBUG("pre-applying DOS name `%s'",
368 dentry_with_dos_name->full_path_utf8);
369 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
373 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
380 dir_name = dentry->full_path_utf8;
382 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
385 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
387 return WIMLIB_ERR_NTFS_3G;
394 * Applies a WIM dentry to a NTFS filesystem.
396 * @dentry: The WIM dentry to apply
397 * @dir_ni: The NTFS inode for the parent directory
398 * @w: The WIMStruct for the WIM containing the image we are applying.
400 * @return: 0 on success; nonzero on failure.
402 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
407 ntfs_inode *ni = NULL;
408 bool is_hardlink = false;
409 ntfs_volume *vol = dir_ni->vol;
410 struct inode *inode = dentry->d_inode;
411 dentry->is_extracted = true;
413 if (inode->attributes & FILE_ATTRIBUTE_DIRECTORY) {
416 struct dentry *other;
418 /* Apply hard-linked directory in same directory with DOS name
419 * (if there is one) before this dentry */
420 if (dentry->short_name_len == 0) {
421 ret = preapply_dentry_with_dos_name(dentry,
429 if (inode->link_count > 1) {
430 /* Already extracted another dentry in the hard link
431 * group. We can make a hard link instead of extracting
433 if (inode->extracted_file) {
434 ret = wim_apply_hardlink_ntfs(dentry, inode,
438 goto out_close_dir_ni;
440 goto out_set_dos_name;
442 /* Can't make a hard link; extract the file itself */
443 FREE(inode->extracted_file);
444 inode->extracted_file = STRDUP(dentry->full_path_utf8);
445 if (!inode->extracted_file) {
446 ret = WIMLIB_ERR_NOMEM;
447 goto out_close_dir_ni;
453 * Create a directory or file.
455 * Note: For symbolic links that are not directory junctions, pass
456 * S_IFREG here, since we manually set the reparse data later.
458 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
459 dentry->file_name_len / 2, type);
462 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
463 dentry->full_path_utf8);
464 ret = WIMLIB_ERR_NTFS_3G;
465 goto out_close_dir_ni;
468 /* Write the data streams, unless this is a directory or reparse point
470 if (!(inode->attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
471 FILE_ATTRIBUTE_DIRECTORY))) {
472 ret = write_ntfs_data_streams(ni, dentry, w);
474 goto out_close_dir_ni;
478 ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry, w);
480 goto out_close_dir_ni;
482 if (inode->attributes & FILE_ATTR_REPARSE_POINT) {
483 ret = apply_reparse_data(ni, dentry, w);
485 goto out_close_dir_ni;
489 /* Set DOS (short) name if given */
490 if (dentry->short_name_len != 0) {
492 char *short_name_utf8;
493 size_t short_name_utf8_len;
494 short_name_utf8 = utf16_to_utf8(dentry->short_name,
495 dentry->short_name_len,
496 &short_name_utf8_len);
497 if (!short_name_utf8) {
498 ERROR("Out of memory");
499 ret = WIMLIB_ERR_NOMEM;
500 goto out_close_dir_ni;
506 const char *dir_name;
508 /* ntfs_set_ntfs_dos_name() closes the inodes in the
509 * wrong order if we have applied a hard link. Close
510 * them ourselves, then re-open then. */
511 if (ntfs_inode_close(dir_ni) != 0) {
513 ret = WIMLIB_ERR_NTFS_3G;
514 ERROR_WITH_ERRNO("Failed to close directory inode");
516 if (ntfs_inode_close(ni) != 0) {
518 ret = WIMLIB_ERR_NTFS_3G;
519 ERROR_WITH_ERRNO("Failed to close hard link target inode");
521 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
528 dir_name = dentry->full_path_utf8;
530 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
533 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
535 return WIMLIB_ERR_NTFS_3G;
537 ni = ntfs_pathname_to_inode(vol, dir_ni,
538 dentry->file_name_utf8);
540 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
542 return WIMLIB_ERR_NTFS_3G;
546 DEBUG("Setting short (DOS) name of `%s' to %s",
547 dentry->full_path_utf8, short_name_utf8);
549 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
550 short_name_utf8_len, 0);
551 FREE(short_name_utf8);
553 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
554 dentry->full_path_utf8);
555 ret = WIMLIB_ERR_NTFS_3G;
557 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
562 if (ntfs_inode_close(dir_ni) != 0) {
564 ret = WIMLIB_ERR_NTFS_3G;
565 ERROR_WITH_ERRNO("Failed to close directory inode");
567 if (ni && ntfs_inode_close(ni) != 0) {
569 ret = WIMLIB_ERR_NTFS_3G;
570 ERROR_WITH_ERRNO("Failed to close inode");
575 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
582 wimlib_assert(dentry_is_directory(dentry));
583 ni = ntfs_pathname_to_inode(vol, NULL, "/");
585 ERROR_WITH_ERRNO("Could not find root NTFS inode");
586 return WIMLIB_ERR_NTFS_3G;
588 ret = apply_file_attributes_and_security_data(ni, ni, dentry, w);
589 if (ntfs_inode_close(ni) != 0) {
590 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
592 ret = WIMLIB_ERR_NTFS_3G;
597 /* Applies a WIM dentry to the NTFS volume */
598 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
600 struct ntfs_apply_args *args = arg;
601 ntfs_volume *vol = args->vol;
602 int extract_flags = args->extract_flags;
603 WIMStruct *w = args->w;
607 const char *dir_name;
609 if (dentry_is_extracted(dentry))
612 wimlib_assert(dentry->full_path_utf8);
614 DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
616 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
617 puts(dentry->full_path_utf8);
619 if (dentry_is_root(dentry))
620 return wim_apply_root_dentry_ntfs(dentry, vol, w);
622 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
629 dir_name = dentry->full_path_utf8;
631 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
634 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
636 return WIMLIB_ERR_NTFS_3G;
638 return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
641 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
643 struct ntfs_apply_args *args = arg;
644 ntfs_volume *vol = args->vol;
651 DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
653 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
655 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
656 dentry->full_path_utf8);
657 return WIMLIB_ERR_NTFS_3G;
661 p = put_u64(p, dentry->d_inode->creation_time);
662 p = put_u64(p, dentry->d_inode->last_write_time);
663 p = put_u64(p, dentry->d_inode->last_access_time);
664 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
666 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
667 dentry->full_path_utf8);
668 ret = WIMLIB_ERR_NTFS_3G;
671 if (ntfs_inode_close(ni) != 0) {
673 ret = WIMLIB_ERR_NTFS_3G;
674 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
675 dentry->full_path_utf8);
680 static int dentry_set_unextracted(struct dentry *dentry, void *ignore)
682 dentry->is_extracted = false;
686 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
691 struct ntfs_apply_args args;
693 DEBUG("Mounting NTFS volume `%s'", device);
694 vol = ntfs_mount(device, 0);
696 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
697 return WIMLIB_ERR_NTFS_3G;
700 args.extract_flags = extract_flags;
702 root = wim_root_dentry(w);
704 for_dentry_in_tree(root, dentry_set_unextracted, NULL);
705 ret = for_dentry_in_tree(root, wim_apply_dentry_ntfs, &args);
709 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
710 printf("Setting timestamps of extracted files on NTFS "
711 "volume `%s'\n", device);
712 ret = for_dentry_in_tree_depth(root, wim_apply_dentry_timestamps,
715 if (ret == 0 && (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE))
716 printf("Finished applying image %d of %s to NTFS "
719 w->filename ? w->filename : "WIM",
722 DEBUG("Unmounting NTFS volume `%s'", device);
723 if (ntfs_umount(vol, FALSE) != 0) {
724 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
726 ret = WIMLIB_ERR_NTFS_3G;
733 * API entry point for applying a WIM image to a NTFS volume.
735 * Please note that this is a NTFS *volume* and not a directory. The intention
736 * is that the volume contain an empty filesystem, and the WIM image contain a
737 * full filesystem to be applied to the volume.
739 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
740 const char *device, int flags,
741 WIMStruct **additional_swms,
742 unsigned num_additional_swms)
744 struct lookup_table *joined_tab, *w_tab_save;
747 DEBUG("w->filename = %s, image = %d, device = %s, flags = 0x%x, "
748 "num_additional_swms = %u",
749 w->filename, image, device, flags, num_additional_swms);
752 return WIMLIB_ERR_INVALID_PARAM;
753 if (image == WIM_ALL_IMAGES) {
754 ERROR("Can only apply a single image when applying "
755 "directly to a NTFS volume");
756 return WIMLIB_ERR_INVALID_PARAM;
758 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
759 ERROR("Cannot specify symlink or hardlink flags when applying ");
760 ERROR("directly to a NTFS volume");
761 return WIMLIB_ERR_INVALID_PARAM;
764 ret = verify_swm_set(w, additional_swms, num_additional_swms);
768 if (num_additional_swms) {
769 ret = new_joined_lookup_table(w, additional_swms,
770 num_additional_swms, &joined_tab);
773 w_tab_save = w->lookup_table;
774 w->lookup_table = joined_tab;
777 ret = wimlib_select_image(w, image);
781 ret = do_wim_apply_image_ntfs(w, device, flags);
784 if (num_additional_swms) {
785 free_lookup_table(w->lookup_table);
786 w->lookup_table = w_tab_save;
791 #else /* WITH_NTFS_3G */
792 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
793 const char *device, int flags,
794 WIMStruct **additional_swms,
795 unsigned num_additional_swms)
797 ERROR("wimlib was compiled without support for NTFS-3g, so");
798 ERROR("we cannot apply a WIM image directly to a NTFS volume");
799 return WIMLIB_ERR_UNSUPPORTED;
801 #endif /* WITH_NTFS_3G */