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/misc.h>
39 #include <ntfs-3g/reparse.h>
40 #include <ntfs-3g/security.h>
41 #include <ntfs-3g/volume.h>
45 struct ntfs_apply_args {
51 extern int ntfs_inode_set_security(ntfs_inode *ni, u32 selection,
53 extern int ntfs_inode_set_attributes(ntfs_inode *ni, s32 attrib);
56 * Extracts a WIM resource to a NTFS attribute.
59 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
62 u64 bytes_remaining = wim_resource_size(lte);
63 u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
66 u8 hash[SHA1_HASH_SIZE];
71 while (bytes_remaining) {
72 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
73 ret = read_wim_resource(lte, buf, to_read, offset, false);
76 sha1_update(&ctx, buf, to_read);
77 if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
78 ERROR_WITH_ERRNO("Error extracting WIM resource");
79 return WIMLIB_ERR_WRITE;
81 bytes_remaining -= to_read;
84 sha1_final(hash, &ctx);
85 if (!hashes_equal(hash, lte->hash)) {
86 ERROR("Invalid checksum on a WIM resource "
87 "(detected when extracting to NTFS stream)");
88 ERROR("The following WIM resource is invalid:");
89 print_lookup_table_entry(lte);
90 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
95 /* Writes the data streams to a NTFS file
97 * @ni: The NTFS inode for the file.
98 * @dentry: The directory entry in the WIM file.
99 * @w: The WIMStruct for the WIM containing the image we are applying.
101 * Returns 0 on success, nonzero on failure.
103 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
107 unsigned stream_idx = 0;
108 ntfschar *stream_name = AT_UNNAMED;
109 u32 stream_name_len = 0;
111 DEBUG("Writing %u NTFS data stream%s for `%s'",
113 (dentry->num_ads == 0 ? "" : "s"),
114 dentry->full_path_utf8);
117 struct lookup_table_entry *lte;
120 lte = dentry_stream_lte(dentry, stream_idx, w->lookup_table);
122 if (stream_name_len) {
123 /* Create an empty named stream. */
124 ret = ntfs_attr_add(ni, AT_DATA, stream_name,
125 stream_name_len, NULL, 0);
127 ERROR_WITH_ERRNO("Failed to create name data "
128 "stream for extracted file "
130 dentry->full_path_utf8);
131 ret = WIMLIB_ERR_NTFS_3G;
136 /* If there's no lookup table entry, it's an empty stream.
137 * Otherwise, we must open the attribute and extract the data.
140 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
142 ERROR_WITH_ERRNO("Failed to open a data stream of "
143 "extracted file `%s'",
144 dentry->full_path_utf8);
145 ret = WIMLIB_ERR_NTFS_3G;
148 ret = extract_wim_resource_to_ntfs_attr(lte, na);
153 if (stream_idx == dentry->num_ads)
155 stream_name = (ntfschar*)dentry->ads_entries[stream_idx].stream_name;
156 stream_name_len = dentry->ads_entries[stream_idx].stream_name_len / 2;
163 * Makes a NTFS hard link
165 * It is named @from_dentry->file_name and is located under the directory
166 * specified by @dir_ni, and it is made to point to the previously extracted
167 * file located at @to_dentry->extracted_file.
169 * Return 0 on success, nonzero on failure.
171 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
172 const struct dentry *to_dentry,
174 ntfs_inode **to_ni_ret)
179 const char *dir_name;
184 wimlib_assert(dentry_is_regular_file(from_dentry)
185 && dentry_is_regular_file(to_dentry));
187 if (ntfs_inode_close(dir_ni) != 0) {
188 ERROR_WITH_ERRNO("Error closing directory");
189 return WIMLIB_ERR_NTFS_3G;
194 DEBUG("Extracting NTFS hard link `%s' => `%s'",
195 from_dentry->full_path_utf8, to_dentry->extracted_file);
197 to_ni = ntfs_pathname_to_inode(vol, NULL,
198 to_dentry->extracted_file);
200 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
201 to_dentry->extracted_file);
202 return WIMLIB_ERR_NTFS_3G;
204 p = from_dentry->full_path_utf8 + from_dentry->full_path_utf8_len;
211 dir_name = from_dentry->full_path_utf8;
213 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
215 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
216 from_dentry->full_path_utf8);
218 return WIMLIB_ERR_NTFS_3G;
222 ret = ntfs_link(to_ni, dir_ni,
223 (ntfschar*)from_dentry->file_name,
224 from_dentry->file_name_len / 2);
226 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
227 from_dentry->full_path_utf8,
228 to_dentry->extracted_file);
229 ret = WIMLIB_ERR_NTFS_3G;
236 apply_file_attributes_and_security_data(ntfs_inode *ni,
237 const struct dentry *dentry,
240 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
241 dentry->full_path_utf8, dentry->attributes);
242 if (!ntfs_inode_set_attributes(ni, dentry->attributes)) {
243 ERROR("Failed to set NTFS file attributes on `%s'",
244 dentry->full_path_utf8);
245 return WIMLIB_ERR_NTFS_3G;
248 if (dentry->security_id != -1) {
249 const struct wim_security_data *sd;
251 sd = wim_const_security_data(w);
252 wimlib_assert(dentry->security_id < sd->num_entries);
253 DEBUG("Applying security descriptor %d to `%s'",
254 dentry->security_id, dentry->full_path_utf8);
255 u32 selection = OWNER_SECURITY_INFORMATION |
256 GROUP_SECURITY_INFORMATION |
257 DACL_SECURITY_INFORMATION |
258 SACL_SECURITY_INFORMATION;
260 if (!ntfs_inode_set_security(ni, selection,
261 (const char*)sd->descriptors[dentry->security_id]))
263 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
264 dentry->full_path_utf8);
265 return WIMLIB_ERR_NTFS_3G;
271 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
274 struct lookup_table_entry *lte;
277 wimlib_assert(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
279 lte = dentry_unnamed_lte(dentry, w->lookup_table);
281 DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
284 ERROR("Could not find reparse data for `%s'",
285 dentry->full_path_utf8);
286 return WIMLIB_ERR_INVALID_DENTRY;
289 if (wim_resource_size(lte) >= 0xffff) {
290 ERROR("Reparse data of `%s' is too long (%lu bytes)",
291 dentry->full_path_utf8, wim_resource_size(lte));
292 return WIMLIB_ERR_INVALID_DENTRY;
295 u8 reparse_data_buf[8 + wim_resource_size(lte)];
296 u8 *p = reparse_data_buf;
297 p = put_u32(p, dentry->reparse_tag); /* ReparseTag */
298 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
299 p = put_u16(p, 0); /* Reserved */
301 ret = read_full_wim_resource(lte, p);
305 ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
306 wim_resource_size(lte) + 8, 0);
308 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
309 dentry->full_path_utf8);
310 return WIMLIB_ERR_NTFS_3G;
315 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
319 * If @dentry is part of a hard link group, search for hard-linked dentries in
320 * the same directory that have a nonempty DOS (short) filename. There should
321 * be exactly 0 or 1 such dentries. If there is 1, extract that dentry first,
322 * so that the DOS name is correctly associated with the corresponding long name
323 * in the Win32 namespace, and not any of the additional names in the POSIX
324 * namespace created from hard links.
326 static int preapply_dentry_with_dos_name(struct dentry *dentry,
327 ntfs_inode **dir_ni_p,
331 struct dentry *other;
332 struct dentry *dentry_with_dos_name;
334 if (dentry->link_group_list.next == &dentry->link_group_list)
337 dentry_with_dos_name = NULL;
338 list_for_each_entry(other, &dentry->link_group_list,
341 if (dentry->parent == other->parent && other->short_name_len) {
342 if (dentry_with_dos_name) {
343 ERROR("Found multiple DOS names for file `%s' "
344 "in the same directory",
345 dentry_with_dos_name->full_path_utf8);
346 return WIMLIB_ERR_INVALID_DENTRY;
348 dentry_with_dos_name = other;
351 /* If there's a dentry with a DOS name, extract it first */
352 if (dentry_with_dos_name && !dentry_with_dos_name->extracted_file) {
354 const char *dir_name;
356 ntfs_volume *vol = (*dir_ni_p)->vol;
358 DEBUG("pre-applying DOS name `%s'",
359 dentry_with_dos_name->full_path_utf8);
360 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
364 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
371 dir_name = dentry->full_path_utf8;
373 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
376 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
378 return WIMLIB_ERR_NTFS_3G;
385 * Applies a WIM dentry to a NTFS filesystem.
387 * @dentry: The WIM dentry to apply
388 * @dir_ni: The NTFS inode for the parent directory
389 * @w: The WIMStruct for the WIM containing the image we are applying.
391 * @return: 0 on success; nonzero on failure.
393 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
398 ntfs_inode *ni = NULL;
399 bool is_hardlink = false;
400 ntfs_volume *vol = dir_ni->vol;
402 if (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY) {
405 struct dentry *other;
407 /* Apply hard-linked directory in same directory with DOS name
408 * (if there is one) before this dentry */
409 ret = preapply_dentry_with_dos_name(dentry, &dir_ni, w);
414 /* See if we can make a hard link */
415 list_for_each_entry(other, &dentry->link_group_list,
417 if (other->extracted_file) {
418 /* Already extracted another dentry in the hard
419 * link group. We can make a hard link instead
420 * of extracting the file data. */
421 ret = wim_apply_hardlink_ntfs(dentry, other,
425 goto out_close_dir_ni;
427 goto out_set_dos_name;
430 /* Can't make a hard link; extract the file itself */
431 FREE(dentry->extracted_file);
432 dentry->extracted_file = STRDUP(dentry->full_path_utf8);
433 if (!dentry->extracted_file) {
434 ERROR("Failed to allocate memory for filename");
435 return WIMLIB_ERR_NOMEM;
440 * Create a directory or file.
442 * Note: For symbolic links that are not directory junctions, pass
443 * S_IFREG here, since we manually set the reparse data later.
445 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
446 dentry->file_name_len / 2, type);
449 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
450 dentry->full_path_utf8);
451 ret = WIMLIB_ERR_NTFS_3G;
452 goto out_close_dir_ni;
455 /* Write the data streams, unless this is a directory or reparse point
457 if (!dentry_is_directory(dentry) &&
458 !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
459 ret = write_ntfs_data_streams(ni, dentry, w);
461 goto out_close_dir_ni;
465 ret = apply_file_attributes_and_security_data(ni, dentry, w);
467 goto out_close_dir_ni;
469 if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
470 ret = apply_reparse_data(ni, dentry, w);
472 goto out_close_dir_ni;
476 /* Set DOS (short) name if given */
477 if (dentry->short_name_len != 0) {
479 char *short_name_utf8;
480 size_t short_name_utf8_len;
481 short_name_utf8 = utf16_to_utf8(dentry->short_name,
482 dentry->short_name_len,
483 &short_name_utf8_len);
484 if (!short_name_utf8) {
485 ERROR("Out of memory");
486 ret = WIMLIB_ERR_NOMEM;
487 goto out_close_dir_ni;
493 const char *dir_name;
495 /* ntfs_set_ntfs_dos_name() closes the inodes in the
496 * wrong order if we have applied a hard link. Close
497 * them ourselves, then re-open then. */
498 if (ntfs_inode_close(dir_ni) != 0) {
500 ret = WIMLIB_ERR_NTFS_3G;
501 ERROR_WITH_ERRNO("Failed to close directory inode");
503 if (ntfs_inode_close(ni) != 0) {
505 ret = WIMLIB_ERR_NTFS_3G;
506 ERROR_WITH_ERRNO("Failed to close hard link target inode");
508 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
515 dir_name = dentry->full_path_utf8;
517 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
520 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
522 return WIMLIB_ERR_NTFS_3G;
524 ni = ntfs_pathname_to_inode(vol, dir_ni,
525 dentry->file_name_utf8);
527 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
529 return WIMLIB_ERR_NTFS_3G;
533 DEBUG("Setting short (DOS) name of `%s' to %s",
534 dentry->full_path_utf8, short_name_utf8);
536 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
537 short_name_utf8_len, 0);
538 FREE(short_name_utf8);
540 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
541 dentry->full_path_utf8);
542 ret = WIMLIB_ERR_NTFS_3G;
544 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
549 if (ntfs_inode_close(dir_ni) != 0) {
551 ret = WIMLIB_ERR_NTFS_3G;
552 ERROR_WITH_ERRNO("Failed to close directory inode");
554 if (ni && ntfs_inode_close(ni) != 0) {
556 ret = WIMLIB_ERR_NTFS_3G;
557 ERROR_WITH_ERRNO("Failed to close inode");
562 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
569 wimlib_assert(dentry_is_directory(dentry));
570 ni = ntfs_pathname_to_inode(vol, NULL, "/");
572 ERROR_WITH_ERRNO("Could not find root NTFS inode");
573 return WIMLIB_ERR_NTFS_3G;
575 ret = apply_file_attributes_and_security_data(ni, dentry, w);
576 if (ntfs_inode_close(ni) != 0) {
577 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
579 ret = WIMLIB_ERR_NTFS_3G;
584 /* Applies a WIM dentry to the NTFS volume */
585 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
587 struct ntfs_apply_args *args = arg;
588 ntfs_volume *vol = args->vol;
589 int extract_flags = args->extract_flags;
590 WIMStruct *w = args->w;
594 const char *dir_name;
596 if (dentry->extracted_file)
599 wimlib_assert(dentry->full_path_utf8);
601 DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
603 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
604 puts(dentry->full_path_utf8);
606 if (dentry_is_root(dentry))
607 return wim_apply_root_dentry_ntfs(dentry, vol, w);
609 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
616 dir_name = dentry->full_path_utf8;
618 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
620 DEBUG("Found NTFS inode for `%s'", dir_name);
623 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
625 return WIMLIB_ERR_NTFS_3G;
627 return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
630 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
632 struct ntfs_apply_args *args = arg;
633 ntfs_volume *vol = args->vol;
640 DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
642 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
644 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
645 dentry->full_path_utf8);
646 return WIMLIB_ERR_NTFS_3G;
650 p = put_u64(p, dentry->creation_time);
651 p = put_u64(p, dentry->last_write_time);
652 p = put_u64(p, dentry->last_access_time);
653 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
655 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
656 dentry->full_path_utf8);
657 ret = WIMLIB_ERR_NTFS_3G;
660 if (ntfs_inode_close(ni) != 0) {
662 ret = WIMLIB_ERR_NTFS_3G;
663 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
664 dentry->full_path_utf8);
669 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
674 DEBUG("Mounting NTFS volume `%s'", device);
675 vol = ntfs_mount(device, 0);
677 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
678 return WIMLIB_ERR_NTFS_3G;
680 struct ntfs_apply_args args = {
682 .extract_flags = extract_flags,
685 ret = for_dentry_in_tree(wim_root_dentry(w), wim_apply_dentry_ntfs,
690 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
691 printf("Setting timestamps of extracted files on NTFS "
692 "volume `%s'\n", device);
693 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
694 wim_apply_dentry_timestamps,
696 if (ret == 0 && (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE))
697 printf("Finished applying image %d of %s to NTFS "
700 w->filename ? w->filename : "WIM",
703 DEBUG("Unmounting NTFS volume `%s'", device);
704 if (ntfs_umount(vol, FALSE) != 0) {
705 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
707 ret = WIMLIB_ERR_NTFS_3G;
714 * API entry point for applying a WIM image to a NTFS volume.
716 * Please note that this is a NTFS *volume* and not a directory. The intention
717 * is that the volume contain an empty filesystem, and the WIM image contain a
718 * full filesystem to be applied to the volume.
720 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
721 const char *device, int flags)
726 return WIMLIB_ERR_INVALID_PARAM;
727 if (image == WIM_ALL_IMAGES) {
728 ERROR("Can only apply a single image when applying "
729 "directly to a NTFS volume");
730 return WIMLIB_ERR_INVALID_PARAM;
732 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
733 ERROR("Cannot specify symlink or hardlink flags when applying ");
734 ERROR("directly to a NTFS volume");
735 return WIMLIB_ERR_INVALID_PARAM;
737 ret = wimlib_select_image(w, image);
741 return do_wim_apply_image_ntfs(w, device, flags);
744 #else /* WITH_NTFS_3G */
745 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
746 const char *device, int flags)
748 ERROR("wimlib was compiled without support for NTFS-3g, so");
749 ERROR("we cannot apply a WIM image directly to a NTFS volume");
750 return WIMLIB_ERR_UNSUPPORTED;
752 #endif /* WITH_NTFS_3G */