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/.
28 #include "wimlib_internal.h"
33 #include "lookup_table.h"
35 #include <ntfs-3g/layout.h>
36 #include <ntfs-3g/acls.h>
37 #include <ntfs-3g/attrib.h>
38 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
39 #include <ntfs-3g/xattrs.h>
40 #include <ntfs-3g/reparse.h>
44 struct ntfs_apply_args {
52 extern int ntfs_set_inode_security(ntfs_inode *ni, u32 selection,
54 extern int ntfs_set_inode_attributes(ntfs_inode *ni, u32 attrib);
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 * @dentry: The directory entry in the WIM file.
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;
113 DEBUG("Writing %u NTFS data stream%s for `%s'",
115 (dentry->num_ads == 0 ? "" : "s"),
116 dentry->full_path_utf8);
119 struct lookup_table_entry *lte;
122 lte = dentry_stream_lte(dentry, stream_idx, w->lookup_table);
124 if (stream_name_len) {
125 /* Create an empty named stream. */
126 ret = ntfs_attr_add(ni, AT_DATA, stream_name,
127 stream_name_len, NULL, 0);
129 ERROR_WITH_ERRNO("Failed to create name data "
130 "stream for extracted file "
132 dentry->full_path_utf8);
133 ret = WIMLIB_ERR_NTFS_3G;
138 /* If there's no lookup table entry, it's an empty stream.
139 * Otherwise, we must open the attribute and extract the data.
142 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
144 ERROR_WITH_ERRNO("Failed to open a data stream of "
145 "extracted file `%s'",
146 dentry->full_path_utf8);
147 ret = WIMLIB_ERR_NTFS_3G;
150 ret = extract_wim_resource_to_ntfs_attr(lte, na);
155 if (stream_idx == dentry->num_ads)
157 stream_name = (ntfschar*)dentry->ads_entries[stream_idx].stream_name;
158 stream_name_len = dentry->ads_entries[stream_idx].stream_name_len / 2;
165 * Makes a NTFS hard link
167 * It is named @from_dentry->file_name and is located under the directory
168 * specified by @dir_ni, and it is made to point to the previously extracted
169 * file located at @to_dentry->extracted_file.
171 * Return 0 on success, nonzero on failure.
173 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
174 const struct dentry *to_dentry,
176 ntfs_inode **to_ni_ret)
181 const char *dir_name;
186 wimlib_assert(dentry_is_regular_file(from_dentry)
187 && dentry_is_regular_file(to_dentry));
189 if (ntfs_inode_close(dir_ni) != 0) {
190 ERROR_WITH_ERRNO("Error closing directory");
191 return WIMLIB_ERR_NTFS_3G;
196 DEBUG("Extracting NTFS hard link `%s' => `%s'",
197 from_dentry->full_path_utf8, to_dentry->extracted_file);
199 to_ni = ntfs_pathname_to_inode(vol, NULL,
200 to_dentry->extracted_file);
202 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
203 to_dentry->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 to_dentry->extracted_file);
231 ret = WIMLIB_ERR_NTFS_3G;
237 /*#define HAVE_NTFS_INODE_FUNCTIONS*/
240 apply_file_attributes_and_security_data(ntfs_inode *ni,
242 const struct dentry *dentry,
245 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
246 dentry->full_path_utf8, dentry->attributes);
248 #ifdef HAVE_NTFS_INODE_FUNCTIONS
249 ret = ntfs_set_inode_attributes(ni, dentry->attributes);
251 struct SECURITY_CONTEXT ctx;
253 attributes_le32 = cpu_to_le32(dentry->attributes);
254 memset(&ctx, 0, sizeof(ctx));
256 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
258 (const char*)&attributes_le32,
262 ERROR("Failed to set NTFS file attributes on `%s'",
263 dentry->full_path_utf8);
264 return WIMLIB_ERR_NTFS_3G;
266 if (dentry->security_id != -1) {
267 const struct wim_security_data *sd;
268 const char *descriptor;
270 sd = wim_const_security_data(w);
271 wimlib_assert(dentry->security_id < sd->num_entries);
272 descriptor = sd->descriptors[dentry->security_id];
273 DEBUG("Applying security descriptor %d to `%s'",
274 dentry->security_id, dentry->full_path_utf8);
276 #ifdef HAVE_NTFS_INODE_FUNCTIONS
277 u32 selection = OWNER_SECURITY_INFORMATION |
278 GROUP_SECURITY_INFORMATION |
279 DACL_SECURITY_INFORMATION |
280 SACL_SECURITY_INFORMATION;
281 ret = ntfs_set_inode_security(ni, selection, descriptor);
283 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
284 ni, dir_ni, descriptor,
285 sd->sizes[dentry->security_id], 0);
289 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
290 dentry->full_path_utf8);
291 return WIMLIB_ERR_NTFS_3G;
297 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
300 struct lookup_table_entry *lte;
303 wimlib_assert(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
305 lte = dentry_unnamed_lte(dentry, w->lookup_table);
307 DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
310 ERROR("Could not find reparse data for `%s'",
311 dentry->full_path_utf8);
312 return WIMLIB_ERR_INVALID_DENTRY;
315 if (wim_resource_size(lte) >= 0xffff) {
316 ERROR("Reparse data of `%s' is too long (%lu bytes)",
317 dentry->full_path_utf8, wim_resource_size(lte));
318 return WIMLIB_ERR_INVALID_DENTRY;
321 u8 reparse_data_buf[8 + wim_resource_size(lte)];
322 u8 *p = reparse_data_buf;
323 p = put_u32(p, dentry->reparse_tag); /* ReparseTag */
324 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
325 p = put_u16(p, 0); /* Reserved */
327 ret = read_full_wim_resource(lte, p);
331 ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
332 wim_resource_size(lte) + 8, 0);
334 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
335 dentry->full_path_utf8);
336 return WIMLIB_ERR_NTFS_3G;
341 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
345 * If @dentry is part of a hard link group, search for hard-linked dentries in
346 * the same directory that have a nonempty DOS (short) filename. There should
347 * be exactly 0 or 1 such dentries. If there is 1, extract that dentry first,
348 * so that the DOS name is correctly associated with the corresponding long name
349 * in the Win32 namespace, and not any of the additional names in the POSIX
350 * namespace created from hard links.
352 static int preapply_dentry_with_dos_name(struct dentry *dentry,
353 ntfs_inode **dir_ni_p,
356 struct dentry *other;
357 struct dentry *dentry_with_dos_name;
359 dentry_with_dos_name = NULL;
360 list_for_each_entry(other, &dentry->link_group_list,
363 if (dentry->parent == other->parent && other->short_name_len) {
364 if (dentry_with_dos_name) {
365 ERROR("Found multiple DOS names for file `%s' "
366 "in the same directory",
367 dentry_with_dos_name->full_path_utf8);
368 return WIMLIB_ERR_INVALID_DENTRY;
370 dentry_with_dos_name = other;
373 /* If there's a dentry with a DOS name, extract it first */
374 if (dentry_with_dos_name
375 && !dentry_with_dos_name->extracted_file)
378 const char *dir_name;
381 ntfs_volume *vol = (*dir_ni_p)->vol;
383 DEBUG("pre-applying DOS name `%s'",
384 dentry_with_dos_name->full_path_utf8);
385 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
389 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
396 dir_name = dentry->full_path_utf8;
398 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
401 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
403 return WIMLIB_ERR_NTFS_3G;
410 * Applies a WIM dentry to a NTFS filesystem.
412 * @dentry: The WIM dentry to apply
413 * @dir_ni: The NTFS inode for the parent directory
414 * @w: The WIMStruct for the WIM containing the image we are applying.
416 * @return: 0 on success; nonzero on failure.
418 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
423 ntfs_inode *ni = NULL;
424 bool is_hardlink = false;
425 ntfs_volume *vol = dir_ni->vol;
427 if (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY) {
430 struct dentry *other;
432 /* Apply hard-linked directory in same directory with DOS name
433 * (if there is one) before this dentry */
434 if (dentry->short_name_len == 0) {
435 ret = preapply_dentry_with_dos_name(dentry,
442 /* See if we can make a hard link */
443 list_for_each_entry(other, &dentry->link_group_list,
445 if (other->extracted_file) {
446 /* Already extracted another dentry in the hard
447 * link group. We can make a hard link instead
448 * of extracting the file data. */
449 ret = wim_apply_hardlink_ntfs(dentry, other,
453 goto out_close_dir_ni;
455 dentry->extracted_file = dentry->full_path_utf8;
456 goto out_set_dos_name;
460 /* Can't make a hard link; extract the file itself */
461 dentry->extracted_file = dentry->full_path_utf8;
465 * Create a directory or file.
467 * Note: For symbolic links that are not directory junctions, pass
468 * S_IFREG here, since we manually set the reparse data later.
470 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
471 dentry->file_name_len / 2, type);
474 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
475 dentry->full_path_utf8);
476 ret = WIMLIB_ERR_NTFS_3G;
477 goto out_close_dir_ni;
480 /* Write the data streams, unless this is a directory or reparse point
482 if (!dentry_is_directory(dentry) &&
483 !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
484 ret = write_ntfs_data_streams(ni, dentry, w);
486 goto out_close_dir_ni;
490 ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry, w);
492 goto out_close_dir_ni;
494 if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
495 ret = apply_reparse_data(ni, dentry, w);
497 goto out_close_dir_ni;
501 /* Set DOS (short) name if given */
502 if (dentry->short_name_len != 0) {
504 char *short_name_utf8;
505 size_t short_name_utf8_len;
506 short_name_utf8 = utf16_to_utf8(dentry->short_name,
507 dentry->short_name_len,
508 &short_name_utf8_len);
509 if (!short_name_utf8) {
510 ERROR("Out of memory");
511 ret = WIMLIB_ERR_NOMEM;
512 goto out_close_dir_ni;
518 const char *dir_name;
520 /* ntfs_set_ntfs_dos_name() closes the inodes in the
521 * wrong order if we have applied a hard link. Close
522 * them ourselves, then re-open then. */
523 if (ntfs_inode_close(dir_ni) != 0) {
525 ret = WIMLIB_ERR_NTFS_3G;
526 ERROR_WITH_ERRNO("Failed to close directory inode");
528 if (ntfs_inode_close(ni) != 0) {
530 ret = WIMLIB_ERR_NTFS_3G;
531 ERROR_WITH_ERRNO("Failed to close hard link target inode");
533 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
540 dir_name = dentry->full_path_utf8;
542 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
545 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
547 return WIMLIB_ERR_NTFS_3G;
549 ni = ntfs_pathname_to_inode(vol, dir_ni,
550 dentry->file_name_utf8);
552 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
554 return WIMLIB_ERR_NTFS_3G;
558 DEBUG("Setting short (DOS) name of `%s' to %s",
559 dentry->full_path_utf8, short_name_utf8);
561 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
562 short_name_utf8_len, 0);
563 FREE(short_name_utf8);
565 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
566 dentry->full_path_utf8);
567 ret = WIMLIB_ERR_NTFS_3G;
569 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
574 if (ntfs_inode_close(dir_ni) != 0) {
576 ret = WIMLIB_ERR_NTFS_3G;
577 ERROR_WITH_ERRNO("Failed to close directory inode");
579 if (ni && ntfs_inode_close(ni) != 0) {
581 ret = WIMLIB_ERR_NTFS_3G;
582 ERROR_WITH_ERRNO("Failed to close inode");
587 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
594 wimlib_assert(dentry_is_directory(dentry));
595 ni = ntfs_pathname_to_inode(vol, NULL, "/");
597 ERROR_WITH_ERRNO("Could not find root NTFS inode");
598 return WIMLIB_ERR_NTFS_3G;
600 ret = apply_file_attributes_and_security_data(ni, ni, dentry, w);
601 if (ntfs_inode_close(ni) != 0) {
602 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
604 ret = WIMLIB_ERR_NTFS_3G;
609 /* Applies a WIM dentry to the NTFS volume */
610 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
612 struct ntfs_apply_args *args = arg;
613 ntfs_volume *vol = args->vol;
614 int extract_flags = args->extract_flags;
615 WIMStruct *w = args->w;
619 const char *dir_name;
621 if (dentry->extracted_file)
624 wimlib_assert(dentry->full_path_utf8);
626 DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
628 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
629 puts(dentry->full_path_utf8);
631 if (dentry_is_root(dentry))
632 return wim_apply_root_dentry_ntfs(dentry, vol, w);
634 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
641 dir_name = dentry->full_path_utf8;
643 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
646 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
648 return WIMLIB_ERR_NTFS_3G;
650 return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
653 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
655 struct ntfs_apply_args *args = arg;
656 ntfs_volume *vol = args->vol;
663 DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
665 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
667 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
668 dentry->full_path_utf8);
669 return WIMLIB_ERR_NTFS_3G;
673 p = put_u64(p, dentry->creation_time);
674 p = put_u64(p, dentry->last_write_time);
675 p = put_u64(p, dentry->last_access_time);
676 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
678 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
679 dentry->full_path_utf8);
680 ret = WIMLIB_ERR_NTFS_3G;
683 if (ntfs_inode_close(ni) != 0) {
685 ret = WIMLIB_ERR_NTFS_3G;
686 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
687 dentry->full_path_utf8);
692 static int dentry_clear_extracted_file(struct dentry *dentry, void *ignore)
694 if (dentry->extracted_file != dentry->full_path_utf8)
695 FREE(dentry->extracted_file);
696 dentry->extracted_file = NULL;
700 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
705 struct ntfs_apply_args args;
707 DEBUG("Mounting NTFS volume `%s'", device);
708 vol = ntfs_mount(device, 0);
710 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
711 return WIMLIB_ERR_NTFS_3G;
714 args.extract_flags = extract_flags;
716 root = wim_root_dentry(w);
718 for_dentry_in_tree(root, dentry_clear_extracted_file, NULL);
720 ret = for_dentry_in_tree(root, wim_apply_dentry_ntfs, &args);
724 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
725 printf("Setting timestamps of extracted files on NTFS "
726 "volume `%s'\n", device);
727 ret = for_dentry_in_tree_depth(root, wim_apply_dentry_timestamps,
730 if (ret == 0 && (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE))
731 printf("Finished applying image %d of %s to NTFS "
734 w->filename ? w->filename : "WIM",
737 DEBUG("Unmounting NTFS volume `%s'", device);
738 if (ntfs_umount(vol, FALSE) != 0) {
739 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
741 ret = WIMLIB_ERR_NTFS_3G;
748 * API entry point for applying a WIM image to a NTFS volume.
750 * Please note that this is a NTFS *volume* and not a directory. The intention
751 * is that the volume contain an empty filesystem, and the WIM image contain a
752 * full filesystem to be applied to the volume.
754 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
755 const char *device, int flags,
756 WIMStruct **additional_swms,
757 unsigned num_additional_swms)
759 struct lookup_table *joined_tab, *w_tab_save;
762 DEBUG("w->filename = %s, image = %d, device = %s, flags = 0x%x, "
763 "num_additional_swms = %u",
764 w->filename, image, device, flags, num_additional_swms);
767 return WIMLIB_ERR_INVALID_PARAM;
768 if (image == WIM_ALL_IMAGES) {
769 ERROR("Can only apply a single image when applying "
770 "directly to a NTFS volume");
771 return WIMLIB_ERR_INVALID_PARAM;
773 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
774 ERROR("Cannot specify symlink or hardlink flags when applying ");
775 ERROR("directly to a NTFS volume");
776 return WIMLIB_ERR_INVALID_PARAM;
779 ret = verify_swm_set(w, additional_swms, num_additional_swms);
783 if (num_additional_swms) {
784 ret = new_joined_lookup_table(w, additional_swms,
785 num_additional_swms, &joined_tab);
788 w_tab_save = w->lookup_table;
789 w->lookup_table = joined_tab;
792 ret = wimlib_select_image(w, image);
796 ret = do_wim_apply_image_ntfs(w, device, flags);
799 if (num_additional_swms) {
800 free_lookup_table(w->lookup_table);
801 w->lookup_table = w_tab_save;
806 #else /* WITH_NTFS_3G */
807 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
808 const char *device, int flags,
809 WIMStruct **additional_swms,
810 unsigned num_additional_swms)
812 ERROR("wimlib was compiled without support for NTFS-3g, so");
813 ERROR("we cannot apply a WIM image directly to a NTFS volume");
814 return WIMLIB_ERR_UNSUPPORTED;
816 #endif /* WITH_NTFS_3G */