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
382 && !dentry_with_dos_name->inode->extracted_file)
385 const char *dir_name;
388 ntfs_volume *vol = (*dir_ni_p)->vol;
390 DEBUG("pre-applying DOS name `%s'",
391 dentry_with_dos_name->full_path_utf8);
392 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
396 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
403 dir_name = dentry->full_path_utf8;
405 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
408 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
410 return WIMLIB_ERR_NTFS_3G;
417 * Applies a WIM dentry to a NTFS filesystem.
419 * @dentry: The WIM dentry to apply
420 * @dir_ni: The NTFS inode for the parent directory
421 * @w: The WIMStruct for the WIM containing the image we are applying.
423 * @return: 0 on success; nonzero on failure.
425 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
430 ntfs_inode *ni = NULL;
431 bool is_hardlink = false;
432 ntfs_volume *vol = dir_ni->vol;
433 struct inode *inode = dentry->inode;
435 if (inode->attributes & FILE_ATTRIBUTE_DIRECTORY) {
438 struct dentry *other;
440 /* Apply hard-linked directory in same directory with DOS name
441 * (if there is one) before this dentry */
442 if (dentry->short_name_len == 0) {
443 ret = preapply_dentry_with_dos_name(dentry,
451 if (inode->link_count > 1) {
452 /* Already extracted another dentry in the hard link
453 * group. We can make a hard link instead of extracting
455 if (inode->extracted_file) {
456 ret = wim_apply_hardlink_ntfs(dentry, inode,
460 goto out_close_dir_ni;
462 goto out_set_dos_name;
464 /* Can't make a hard link; extract the file itself */
465 FREE(inode->extracted_file);
466 inode->extracted_file = STRDUP(dentry->full_path_utf8);
467 if (!inode->extracted_file)
468 return WIMLIB_ERR_NOMEM;
473 * Create a directory or file.
475 * Note: For symbolic links that are not directory junctions, pass
476 * S_IFREG here, since we manually set the reparse data later.
478 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
479 dentry->file_name_len / 2, type);
482 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
483 dentry->full_path_utf8);
484 ret = WIMLIB_ERR_NTFS_3G;
485 goto out_close_dir_ni;
488 /* Write the data streams, unless this is a directory or reparse point
490 if (!(inode->attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
491 FILE_ATTRIBUTE_DIRECTORY))) {
492 ret = write_ntfs_data_streams(ni, dentry, w);
494 goto out_close_dir_ni;
498 ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry, w);
500 goto out_close_dir_ni;
502 if (inode->attributes & FILE_ATTR_REPARSE_POINT) {
503 ret = apply_reparse_data(ni, dentry, w);
505 goto out_close_dir_ni;
509 /* Set DOS (short) name if given */
510 if (dentry->short_name_len != 0) {
512 char *short_name_utf8;
513 size_t short_name_utf8_len;
514 short_name_utf8 = utf16_to_utf8(dentry->short_name,
515 dentry->short_name_len,
516 &short_name_utf8_len);
517 if (!short_name_utf8) {
518 ERROR("Out of memory");
519 ret = WIMLIB_ERR_NOMEM;
520 goto out_close_dir_ni;
526 const char *dir_name;
528 /* ntfs_set_ntfs_dos_name() closes the inodes in the
529 * wrong order if we have applied a hard link. Close
530 * them ourselves, then re-open then. */
531 if (ntfs_inode_close(dir_ni) != 0) {
533 ret = WIMLIB_ERR_NTFS_3G;
534 ERROR_WITH_ERRNO("Failed to close directory inode");
536 if (ntfs_inode_close(ni) != 0) {
538 ret = WIMLIB_ERR_NTFS_3G;
539 ERROR_WITH_ERRNO("Failed to close hard link target inode");
541 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
548 dir_name = dentry->full_path_utf8;
550 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
553 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
555 return WIMLIB_ERR_NTFS_3G;
557 ni = ntfs_pathname_to_inode(vol, dir_ni,
558 dentry->file_name_utf8);
560 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
562 return WIMLIB_ERR_NTFS_3G;
566 DEBUG("Setting short (DOS) name of `%s' to %s",
567 dentry->full_path_utf8, short_name_utf8);
569 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
570 short_name_utf8_len, 0);
571 FREE(short_name_utf8);
573 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
574 dentry->full_path_utf8);
575 ret = WIMLIB_ERR_NTFS_3G;
577 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
582 if (ntfs_inode_close(dir_ni) != 0) {
584 ret = WIMLIB_ERR_NTFS_3G;
585 ERROR_WITH_ERRNO("Failed to close directory inode");
587 if (ni && ntfs_inode_close(ni) != 0) {
589 ret = WIMLIB_ERR_NTFS_3G;
590 ERROR_WITH_ERRNO("Failed to close inode");
595 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
602 wimlib_assert(dentry_is_directory(dentry));
603 ni = ntfs_pathname_to_inode(vol, NULL, "/");
605 ERROR_WITH_ERRNO("Could not find root NTFS inode");
606 return WIMLIB_ERR_NTFS_3G;
608 ret = apply_file_attributes_and_security_data(ni, ni, dentry, w);
609 if (ntfs_inode_close(ni) != 0) {
610 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
612 ret = WIMLIB_ERR_NTFS_3G;
617 /* Applies a WIM dentry to the NTFS volume */
618 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
620 struct ntfs_apply_args *args = arg;
621 ntfs_volume *vol = args->vol;
622 int extract_flags = args->extract_flags;
623 WIMStruct *w = args->w;
627 const char *dir_name;
629 if (dentry_is_extracted(dentry))
632 wimlib_assert(dentry->full_path_utf8);
634 DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
636 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
637 puts(dentry->full_path_utf8);
639 if (dentry_is_root(dentry))
640 return wim_apply_root_dentry_ntfs(dentry, vol, w);
642 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
649 dir_name = dentry->full_path_utf8;
651 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
654 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
656 return WIMLIB_ERR_NTFS_3G;
658 return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
661 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
663 struct ntfs_apply_args *args = arg;
664 ntfs_volume *vol = args->vol;
671 DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
673 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
675 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
676 dentry->full_path_utf8);
677 return WIMLIB_ERR_NTFS_3G;
681 p = put_u64(p, dentry->inode->creation_time);
682 p = put_u64(p, dentry->inode->last_write_time);
683 p = put_u64(p, dentry->inode->last_access_time);
684 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
686 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
687 dentry->full_path_utf8);
688 ret = WIMLIB_ERR_NTFS_3G;
691 if (ntfs_inode_close(ni) != 0) {
693 ret = WIMLIB_ERR_NTFS_3G;
694 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
695 dentry->full_path_utf8);
700 static int dentry_set_unextracted(struct dentry *dentry, void *ignore)
702 dentry->is_extracted = false;
706 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
711 struct ntfs_apply_args args;
713 DEBUG("Mounting NTFS volume `%s'", device);
714 vol = ntfs_mount(device, 0);
716 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
717 return WIMLIB_ERR_NTFS_3G;
720 args.extract_flags = extract_flags;
722 root = wim_root_dentry(w);
724 for_dentry_in_tree(root, dentry_set_unextracted, NULL);
725 ret = for_dentry_in_tree(root, wim_apply_dentry_ntfs, &args);
729 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
730 printf("Setting timestamps of extracted files on NTFS "
731 "volume `%s'\n", device);
732 ret = for_dentry_in_tree_depth(root, wim_apply_dentry_timestamps,
735 if (ret == 0 && (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE))
736 printf("Finished applying image %d of %s to NTFS "
739 w->filename ? w->filename : "WIM",
742 DEBUG("Unmounting NTFS volume `%s'", device);
743 if (ntfs_umount(vol, FALSE) != 0) {
744 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
746 ret = WIMLIB_ERR_NTFS_3G;
753 * API entry point for applying a WIM image to a NTFS volume.
755 * Please note that this is a NTFS *volume* and not a directory. The intention
756 * is that the volume contain an empty filesystem, and the WIM image contain a
757 * full filesystem to be applied to the volume.
759 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
760 const char *device, int flags,
761 WIMStruct **additional_swms,
762 unsigned num_additional_swms)
764 struct lookup_table *joined_tab, *w_tab_save;
767 DEBUG("w->filename = %s, image = %d, device = %s, flags = 0x%x, "
768 "num_additional_swms = %u",
769 w->filename, image, device, flags, num_additional_swms);
772 return WIMLIB_ERR_INVALID_PARAM;
773 if (image == WIM_ALL_IMAGES) {
774 ERROR("Can only apply a single image when applying "
775 "directly to a NTFS volume");
776 return WIMLIB_ERR_INVALID_PARAM;
778 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
779 ERROR("Cannot specify symlink or hardlink flags when applying ");
780 ERROR("directly to a NTFS volume");
781 return WIMLIB_ERR_INVALID_PARAM;
784 ret = verify_swm_set(w, additional_swms, num_additional_swms);
788 if (num_additional_swms) {
789 ret = new_joined_lookup_table(w, additional_swms,
790 num_additional_swms, &joined_tab);
793 w_tab_save = w->lookup_table;
794 w->lookup_table = joined_tab;
797 ret = wimlib_select_image(w, image);
801 ret = do_wim_apply_image_ntfs(w, device, flags);
804 if (num_additional_swms) {
805 free_lookup_table(w->lookup_table);
806 w->lookup_table = w_tab_save;
811 #else /* WITH_NTFS_3G */
812 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
813 const char *device, int flags,
814 WIMStruct **additional_swms,
815 unsigned num_additional_swms)
817 ERROR("wimlib was compiled without support for NTFS-3g, so");
818 ERROR("we cannot apply a WIM image directly to a NTFS volume");
819 return WIMLIB_ERR_UNSUPPORTED;
821 #endif /* WITH_NTFS_3G */