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->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;
244 /*#define HAVE_NTFS_INODE_FUNCTIONS*/
247 apply_file_attributes_and_security_data(ntfs_inode *ni,
249 const struct dentry *dentry,
252 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
253 dentry->full_path_utf8, dentry->inode->attributes);
255 #ifdef HAVE_NTFS_INODE_FUNCTIONS
256 ret = ntfs_set_inode_attributes(ni, dentry->inode->attributes);
258 struct SECURITY_CONTEXT ctx;
260 attributes_le32 = cpu_to_le32(dentry->inode->attributes);
261 memset(&ctx, 0, sizeof(ctx));
263 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
265 (const char*)&attributes_le32,
269 ERROR("Failed to set NTFS file attributes on `%s'",
270 dentry->full_path_utf8);
271 return WIMLIB_ERR_NTFS_3G;
273 if (dentry->inode->security_id != -1) {
274 const struct wim_security_data *sd;
275 const char *descriptor;
277 sd = wim_const_security_data(w);
278 wimlib_assert(dentry->inode->security_id < sd->num_entries);
279 descriptor = sd->descriptors[dentry->inode->security_id];
280 DEBUG("Applying security descriptor %d to `%s'",
281 dentry->inode->security_id, dentry->full_path_utf8);
283 #ifdef HAVE_NTFS_INODE_FUNCTIONS
284 u32 selection = OWNER_SECURITY_INFORMATION |
285 GROUP_SECURITY_INFORMATION |
286 DACL_SECURITY_INFORMATION |
287 SACL_SECURITY_INFORMATION;
288 ret = ntfs_set_inode_security(ni, selection, descriptor);
290 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
291 ni, dir_ni, descriptor,
292 sd->sizes[dentry->inode->security_id], 0);
296 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
297 dentry->full_path_utf8);
298 return WIMLIB_ERR_NTFS_3G;
304 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
307 struct lookup_table_entry *lte;
310 wimlib_assert(dentry->inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
312 lte = inode_unnamed_lte(dentry->inode, w->lookup_table);
314 DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
317 ERROR("Could not find reparse data for `%s'",
318 dentry->full_path_utf8);
319 return WIMLIB_ERR_INVALID_DENTRY;
322 if (wim_resource_size(lte) >= 0xffff) {
323 ERROR("Reparse data of `%s' is too long (%lu bytes)",
324 dentry->full_path_utf8, wim_resource_size(lte));
325 return WIMLIB_ERR_INVALID_DENTRY;
328 u8 reparse_data_buf[8 + wim_resource_size(lte)];
329 u8 *p = reparse_data_buf;
330 p = put_u32(p, dentry->inode->reparse_tag); /* ReparseTag */
331 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
332 p = put_u16(p, 0); /* Reserved */
334 ret = read_full_wim_resource(lte, p);
338 ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
339 wim_resource_size(lte) + 8, 0);
341 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
342 dentry->full_path_utf8);
343 return WIMLIB_ERR_NTFS_3G;
348 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
352 * If @dentry is part of a hard link group, search for hard-linked dentries in
353 * the same directory that have a nonempty DOS (short) filename. There should
354 * be exactly 0 or 1 such dentries. If there is 1, extract that dentry first,
355 * so that the DOS name is correctly associated with the corresponding long name
356 * in the Win32 namespace, and not any of the additional names in the POSIX
357 * namespace created from hard links.
359 static int preapply_dentry_with_dos_name(struct dentry *dentry,
360 ntfs_inode **dir_ni_p,
363 struct dentry *other;
364 struct dentry *dentry_with_dos_name;
366 dentry_with_dos_name = NULL;
367 inode_for_each_dentry(other, dentry->inode) {
368 if (other != dentry && (dentry->parent == other->parent)
369 && other->short_name_len)
371 if (dentry_with_dos_name) {
372 ERROR("Found multiple DOS names for file `%s' "
373 "in the same directory",
374 dentry_with_dos_name->full_path_utf8);
375 return WIMLIB_ERR_INVALID_DENTRY;
377 dentry_with_dos_name = other;
380 /* If there's a dentry with a DOS name, extract it first */
381 if (dentry_with_dos_name && !dentry_is_extracted(dentry)) {
383 const char *dir_name;
386 ntfs_volume *vol = (*dir_ni_p)->vol;
388 DEBUG("pre-applying DOS name `%s'",
389 dentry_with_dos_name->full_path_utf8);
390 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
394 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
401 dir_name = dentry->full_path_utf8;
403 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
406 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
408 return WIMLIB_ERR_NTFS_3G;
415 * Applies a WIM dentry to a NTFS filesystem.
417 * @dentry: The WIM dentry to apply
418 * @dir_ni: The NTFS inode for the parent directory
419 * @w: The WIMStruct for the WIM containing the image we are applying.
421 * @return: 0 on success; nonzero on failure.
423 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
428 ntfs_inode *ni = NULL;
429 bool is_hardlink = false;
430 ntfs_volume *vol = dir_ni->vol;
431 struct inode *inode = dentry->inode;
432 dentry->is_extracted = true;
434 if (inode->attributes & FILE_ATTRIBUTE_DIRECTORY) {
437 struct dentry *other;
439 /* Apply hard-linked directory in same directory with DOS name
440 * (if there is one) before this dentry */
441 if (dentry->short_name_len == 0) {
442 ret = preapply_dentry_with_dos_name(dentry,
450 if (inode->link_count > 1) {
451 /* Already extracted another dentry in the hard link
452 * group. We can make a hard link instead of extracting
454 if (inode->extracted_file) {
455 ret = wim_apply_hardlink_ntfs(dentry, inode,
459 goto out_close_dir_ni;
461 goto out_set_dos_name;
463 /* Can't make a hard link; extract the file itself */
464 FREE(inode->extracted_file);
465 inode->extracted_file = STRDUP(dentry->full_path_utf8);
466 if (!inode->extracted_file)
467 return WIMLIB_ERR_NOMEM;
472 * Create a directory or file.
474 * Note: For symbolic links that are not directory junctions, pass
475 * S_IFREG here, since we manually set the reparse data later.
477 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
478 dentry->file_name_len / 2, type);
481 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
482 dentry->full_path_utf8);
483 ret = WIMLIB_ERR_NTFS_3G;
484 goto out_close_dir_ni;
487 /* Write the data streams, unless this is a directory or reparse point
489 if (!(inode->attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
490 FILE_ATTRIBUTE_DIRECTORY))) {
491 ret = write_ntfs_data_streams(ni, dentry, w);
493 goto out_close_dir_ni;
497 ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry, w);
499 goto out_close_dir_ni;
501 if (inode->attributes & FILE_ATTR_REPARSE_POINT) {
502 ret = apply_reparse_data(ni, dentry, w);
504 goto out_close_dir_ni;
508 /* Set DOS (short) name if given */
509 if (dentry->short_name_len != 0) {
511 char *short_name_utf8;
512 size_t short_name_utf8_len;
513 short_name_utf8 = utf16_to_utf8(dentry->short_name,
514 dentry->short_name_len,
515 &short_name_utf8_len);
516 if (!short_name_utf8) {
517 ERROR("Out of memory");
518 ret = WIMLIB_ERR_NOMEM;
519 goto out_close_dir_ni;
525 const char *dir_name;
527 /* ntfs_set_ntfs_dos_name() closes the inodes in the
528 * wrong order if we have applied a hard link. Close
529 * them ourselves, then re-open then. */
530 if (ntfs_inode_close(dir_ni) != 0) {
532 ret = WIMLIB_ERR_NTFS_3G;
533 ERROR_WITH_ERRNO("Failed to close directory inode");
535 if (ntfs_inode_close(ni) != 0) {
537 ret = WIMLIB_ERR_NTFS_3G;
538 ERROR_WITH_ERRNO("Failed to close hard link target inode");
540 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
547 dir_name = dentry->full_path_utf8;
549 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
552 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
554 return WIMLIB_ERR_NTFS_3G;
556 ni = ntfs_pathname_to_inode(vol, dir_ni,
557 dentry->file_name_utf8);
559 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
561 return WIMLIB_ERR_NTFS_3G;
565 DEBUG("Setting short (DOS) name of `%s' to %s",
566 dentry->full_path_utf8, short_name_utf8);
568 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
569 short_name_utf8_len, 0);
570 FREE(short_name_utf8);
572 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
573 dentry->full_path_utf8);
574 ret = WIMLIB_ERR_NTFS_3G;
576 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
581 if (ntfs_inode_close(dir_ni) != 0) {
583 ret = WIMLIB_ERR_NTFS_3G;
584 ERROR_WITH_ERRNO("Failed to close directory inode");
586 if (ni && ntfs_inode_close(ni) != 0) {
588 ret = WIMLIB_ERR_NTFS_3G;
589 ERROR_WITH_ERRNO("Failed to close inode");
594 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
601 wimlib_assert(dentry_is_directory(dentry));
602 ni = ntfs_pathname_to_inode(vol, NULL, "/");
604 ERROR_WITH_ERRNO("Could not find root NTFS inode");
605 return WIMLIB_ERR_NTFS_3G;
607 ret = apply_file_attributes_and_security_data(ni, ni, dentry, w);
608 if (ntfs_inode_close(ni) != 0) {
609 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
611 ret = WIMLIB_ERR_NTFS_3G;
616 /* Applies a WIM dentry to the NTFS volume */
617 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
619 struct ntfs_apply_args *args = arg;
620 ntfs_volume *vol = args->vol;
621 int extract_flags = args->extract_flags;
622 WIMStruct *w = args->w;
626 const char *dir_name;
628 if (dentry_is_extracted(dentry))
631 wimlib_assert(dentry->full_path_utf8);
633 DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
635 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
636 puts(dentry->full_path_utf8);
638 if (dentry_is_root(dentry))
639 return wim_apply_root_dentry_ntfs(dentry, vol, w);
641 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
648 dir_name = dentry->full_path_utf8;
650 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
653 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
655 return WIMLIB_ERR_NTFS_3G;
657 return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
660 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
662 struct ntfs_apply_args *args = arg;
663 ntfs_volume *vol = args->vol;
670 DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
672 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
674 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
675 dentry->full_path_utf8);
676 return WIMLIB_ERR_NTFS_3G;
680 p = put_u64(p, dentry->inode->creation_time);
681 p = put_u64(p, dentry->inode->last_write_time);
682 p = put_u64(p, dentry->inode->last_access_time);
683 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
685 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
686 dentry->full_path_utf8);
687 ret = WIMLIB_ERR_NTFS_3G;
690 if (ntfs_inode_close(ni) != 0) {
692 ret = WIMLIB_ERR_NTFS_3G;
693 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
694 dentry->full_path_utf8);
699 static int dentry_set_unextracted(struct dentry *dentry, void *ignore)
701 dentry->is_extracted = false;
705 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
710 struct ntfs_apply_args args;
712 DEBUG("Mounting NTFS volume `%s'", device);
713 vol = ntfs_mount(device, 0);
715 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
716 return WIMLIB_ERR_NTFS_3G;
719 args.extract_flags = extract_flags;
721 root = wim_root_dentry(w);
723 for_dentry_in_tree(root, dentry_set_unextracted, NULL);
724 ret = for_dentry_in_tree(root, wim_apply_dentry_ntfs, &args);
728 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
729 printf("Setting timestamps of extracted files on NTFS "
730 "volume `%s'\n", device);
731 ret = for_dentry_in_tree_depth(root, wim_apply_dentry_timestamps,
734 if (ret == 0 && (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE))
735 printf("Finished applying image %d of %s to NTFS "
738 w->filename ? w->filename : "WIM",
741 DEBUG("Unmounting NTFS volume `%s'", device);
742 if (ntfs_umount(vol, FALSE) != 0) {
743 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
745 ret = WIMLIB_ERR_NTFS_3G;
752 * API entry point for applying a WIM image to a NTFS volume.
754 * Please note that this is a NTFS *volume* and not a directory. The intention
755 * is that the volume contain an empty filesystem, and the WIM image contain a
756 * full filesystem to be applied to the volume.
758 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
759 const char *device, int flags,
760 WIMStruct **additional_swms,
761 unsigned num_additional_swms)
763 struct lookup_table *joined_tab, *w_tab_save;
766 DEBUG("w->filename = %s, image = %d, device = %s, flags = 0x%x, "
767 "num_additional_swms = %u",
768 w->filename, image, device, flags, num_additional_swms);
771 return WIMLIB_ERR_INVALID_PARAM;
772 if (image == WIM_ALL_IMAGES) {
773 ERROR("Can only apply a single image when applying "
774 "directly to a NTFS volume");
775 return WIMLIB_ERR_INVALID_PARAM;
777 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
778 ERROR("Cannot specify symlink or hardlink flags when applying ");
779 ERROR("directly to a NTFS volume");
780 return WIMLIB_ERR_INVALID_PARAM;
783 ret = verify_swm_set(w, additional_swms, num_additional_swms);
787 if (num_additional_swms) {
788 ret = new_joined_lookup_table(w, additional_swms,
789 num_additional_swms, &joined_tab);
792 w_tab_save = w->lookup_table;
793 w->lookup_table = joined_tab;
796 ret = wimlib_select_image(w, image);
800 ret = do_wim_apply_image_ntfs(w, device, flags);
803 if (num_additional_swms) {
804 free_lookup_table(w->lookup_table);
805 w->lookup_table = w_tab_save;
810 #else /* WITH_NTFS_3G */
811 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
812 const char *device, int flags,
813 WIMStruct **additional_swms,
814 unsigned num_additional_swms)
816 ERROR("wimlib was compiled without support for NTFS-3g, so");
817 ERROR("we cannot apply a WIM image directly to a NTFS volume");
818 return WIMLIB_ERR_UNSUPPORTED;
820 #endif /* WITH_NTFS_3G */