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 {
52 #ifndef WITH_NEW_NTFS_3G
53 extern int ntfs_set_inode_security(ntfs_inode *ni, u32 selection,
55 extern int ntfs_set_inode_attributes(ntfs_inode *ni, u32 attrib);
59 * Extracts a WIM resource to a NTFS attribute.
62 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
65 u64 bytes_remaining = wim_resource_size(lte);
66 u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
69 u8 hash[SHA1_HASH_SIZE];
74 while (bytes_remaining) {
75 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
76 ret = read_wim_resource(lte, buf, to_read, offset, false);
79 sha1_update(&ctx, buf, to_read);
80 if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
81 ERROR_WITH_ERRNO("Error extracting WIM resource");
82 return WIMLIB_ERR_WRITE;
84 bytes_remaining -= to_read;
87 sha1_final(hash, &ctx);
88 if (!hashes_equal(hash, lte->hash)) {
89 ERROR("Invalid checksum on a WIM resource "
90 "(detected when extracting to NTFS stream)");
91 ERROR("The following WIM resource is invalid:");
92 print_lookup_table_entry(lte);
93 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
98 /* Writes the data streams to a NTFS file
100 * @ni: The NTFS inode for the file.
101 * @dentry: The directory entry in the WIM file.
102 * @w: The WIMStruct for the WIM containing the image we are applying.
104 * Returns 0 on success, nonzero on failure.
106 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
110 unsigned stream_idx = 0;
111 ntfschar *stream_name = AT_UNNAMED;
112 u32 stream_name_len = 0;
114 DEBUG("Writing %u NTFS data stream%s for `%s'",
116 (dentry->num_ads == 0 ? "" : "s"),
117 dentry->full_path_utf8);
120 struct lookup_table_entry *lte;
123 lte = dentry_stream_lte(dentry, 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 == dentry->num_ads)
158 stream_name = (ntfschar*)dentry->ads_entries[stream_idx].stream_name;
159 stream_name_len = dentry->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 @to_dentry->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 dentry *to_dentry,
177 ntfs_inode **to_ni_ret)
182 const char *dir_name;
187 wimlib_assert(dentry_is_regular_file(from_dentry)
188 && dentry_is_regular_file(to_dentry));
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, to_dentry->extracted_file);
200 to_ni = ntfs_pathname_to_inode(vol, NULL,
201 to_dentry->extracted_file);
203 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
204 to_dentry->extracted_file);
205 return WIMLIB_ERR_NTFS_3G;
207 p = from_dentry->full_path_utf8 + from_dentry->full_path_utf8_len;
214 dir_name = from_dentry->full_path_utf8;
216 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
218 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
219 from_dentry->full_path_utf8);
221 return WIMLIB_ERR_NTFS_3G;
225 ret = ntfs_link(to_ni, dir_ni,
226 (ntfschar*)from_dentry->file_name,
227 from_dentry->file_name_len / 2);
229 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
230 from_dentry->full_path_utf8,
231 to_dentry->extracted_file);
232 ret = WIMLIB_ERR_NTFS_3G;
239 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->attributes);
245 if (ntfs_set_inode_attributes(ni, dentry->attributes)) {
246 ERROR("Failed to set NTFS file attributes on `%s'",
247 dentry->full_path_utf8);
248 return WIMLIB_ERR_NTFS_3G;
251 if (dentry->security_id != -1) {
252 const struct wim_security_data *sd;
254 sd = wim_const_security_data(w);
255 wimlib_assert(dentry->security_id < sd->num_entries);
256 DEBUG("Applying security descriptor %d to `%s'",
257 dentry->security_id, dentry->full_path_utf8);
258 u32 selection = OWNER_SECURITY_INFORMATION |
259 GROUP_SECURITY_INFORMATION |
260 DACL_SECURITY_INFORMATION |
261 SACL_SECURITY_INFORMATION;
263 if (ntfs_set_inode_security(ni, selection,
264 (const char*)sd->descriptors[dentry->security_id]))
266 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
267 dentry->full_path_utf8);
268 return WIMLIB_ERR_NTFS_3G;
274 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
277 struct lookup_table_entry *lte;
280 wimlib_assert(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
282 lte = dentry_unnamed_lte(dentry, w->lookup_table);
284 DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
287 ERROR("Could not find reparse data for `%s'",
288 dentry->full_path_utf8);
289 return WIMLIB_ERR_INVALID_DENTRY;
292 if (wim_resource_size(lte) >= 0xffff) {
293 ERROR("Reparse data of `%s' is too long (%lu bytes)",
294 dentry->full_path_utf8, wim_resource_size(lte));
295 return WIMLIB_ERR_INVALID_DENTRY;
298 u8 reparse_data_buf[8 + wim_resource_size(lte)];
299 u8 *p = reparse_data_buf;
300 p = put_u32(p, dentry->reparse_tag); /* ReparseTag */
301 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
302 p = put_u16(p, 0); /* Reserved */
304 ret = read_full_wim_resource(lte, p);
308 ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
309 wim_resource_size(lte) + 8, 0);
311 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
312 dentry->full_path_utf8);
313 return WIMLIB_ERR_NTFS_3G;
318 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
322 * If @dentry is part of a hard link group, search for hard-linked dentries in
323 * the same directory that have a nonempty DOS (short) filename. There should
324 * be exactly 0 or 1 such dentries. If there is 1, extract that dentry first,
325 * so that the DOS name is correctly associated with the corresponding long name
326 * in the Win32 namespace, and not any of the additional names in the POSIX
327 * namespace created from hard links.
329 static int preapply_dentry_with_dos_name(struct dentry *dentry,
330 ntfs_inode **dir_ni_p,
334 struct dentry *other;
335 struct dentry *dentry_with_dos_name;
337 if (dentry->link_group_list.next == &dentry->link_group_list)
340 dentry_with_dos_name = NULL;
341 list_for_each_entry(other, &dentry->link_group_list,
344 if (dentry->parent == other->parent && other->short_name_len) {
345 if (dentry_with_dos_name) {
346 ERROR("Found multiple DOS names for file `%s' "
347 "in the same directory",
348 dentry_with_dos_name->full_path_utf8);
349 return WIMLIB_ERR_INVALID_DENTRY;
351 dentry_with_dos_name = other;
354 /* If there's a dentry with a DOS name, extract it first */
355 if (dentry_with_dos_name
356 && !dentry_with_dos_name->extracted_file)
359 const char *dir_name;
361 ntfs_volume *vol = (*dir_ni_p)->vol;
363 DEBUG("pre-applying DOS name `%s'",
364 dentry_with_dos_name->full_path_utf8);
365 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
369 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
376 dir_name = dentry->full_path_utf8;
378 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
381 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
383 return WIMLIB_ERR_NTFS_3G;
390 * Applies a WIM dentry to a NTFS filesystem.
392 * @dentry: The WIM dentry to apply
393 * @dir_ni: The NTFS inode for the parent directory
394 * @w: The WIMStruct for the WIM containing the image we are applying.
396 * @return: 0 on success; nonzero on failure.
398 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
403 ntfs_inode *ni = NULL;
404 bool is_hardlink = false;
405 ntfs_volume *vol = dir_ni->vol;
407 if (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY) {
410 struct dentry *other;
412 /* Apply hard-linked directory in same directory with DOS name
413 * (if there is one) before this dentry */
414 ret = preapply_dentry_with_dos_name(dentry, &dir_ni, w);
419 /* See if we can make a hard link */
420 list_for_each_entry(other, &dentry->link_group_list,
422 if (other->extracted_file) {
423 /* Already extracted another dentry in the hard
424 * link group. We can make a hard link instead
425 * of extracting the file data. */
426 ret = wim_apply_hardlink_ntfs(dentry, other,
430 goto out_close_dir_ni;
432 goto out_set_dos_name;
435 /* Can't make a hard link; extract the file itself */
436 FREE(dentry->extracted_file);
437 dentry->extracted_file = STRDUP(dentry->full_path_utf8);
438 if (!dentry->extracted_file) {
439 ERROR("Failed to allocate memory for filename");
440 return WIMLIB_ERR_NOMEM;
445 * Create a directory or file.
447 * Note: For symbolic links that are not directory junctions, pass
448 * S_IFREG here, since we manually set the reparse data later.
450 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
451 dentry->file_name_len / 2, type);
454 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
455 dentry->full_path_utf8);
456 ret = WIMLIB_ERR_NTFS_3G;
457 goto out_close_dir_ni;
460 /* Write the data streams, unless this is a directory or reparse point
462 if (!dentry_is_directory(dentry) &&
463 !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
464 ret = write_ntfs_data_streams(ni, dentry, w);
466 goto out_close_dir_ni;
470 ret = apply_file_attributes_and_security_data(ni, dentry, w);
472 goto out_close_dir_ni;
474 if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
475 ret = apply_reparse_data(ni, dentry, w);
477 goto out_close_dir_ni;
481 /* Set DOS (short) name if given */
482 if (dentry->short_name_len != 0) {
484 char *short_name_utf8;
485 size_t short_name_utf8_len;
486 short_name_utf8 = utf16_to_utf8(dentry->short_name,
487 dentry->short_name_len,
488 &short_name_utf8_len);
489 if (!short_name_utf8) {
490 ERROR("Out of memory");
491 ret = WIMLIB_ERR_NOMEM;
492 goto out_close_dir_ni;
498 const char *dir_name;
500 /* ntfs_set_ntfs_dos_name() closes the inodes in the
501 * wrong order if we have applied a hard link. Close
502 * them ourselves, then re-open then. */
503 if (ntfs_inode_close(dir_ni) != 0) {
505 ret = WIMLIB_ERR_NTFS_3G;
506 ERROR_WITH_ERRNO("Failed to close directory inode");
508 if (ntfs_inode_close(ni) != 0) {
510 ret = WIMLIB_ERR_NTFS_3G;
511 ERROR_WITH_ERRNO("Failed to close hard link target inode");
513 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
520 dir_name = dentry->full_path_utf8;
522 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
525 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
527 return WIMLIB_ERR_NTFS_3G;
529 ni = ntfs_pathname_to_inode(vol, dir_ni,
530 dentry->file_name_utf8);
532 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
534 return WIMLIB_ERR_NTFS_3G;
538 DEBUG("Setting short (DOS) name of `%s' to %s",
539 dentry->full_path_utf8, short_name_utf8);
541 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
542 short_name_utf8_len, 0);
543 FREE(short_name_utf8);
545 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
546 dentry->full_path_utf8);
547 ret = WIMLIB_ERR_NTFS_3G;
549 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
554 if (ntfs_inode_close(dir_ni) != 0) {
556 ret = WIMLIB_ERR_NTFS_3G;
557 ERROR_WITH_ERRNO("Failed to close directory inode");
559 if (ni && ntfs_inode_close(ni) != 0) {
561 ret = WIMLIB_ERR_NTFS_3G;
562 ERROR_WITH_ERRNO("Failed to close inode");
567 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
574 wimlib_assert(dentry_is_directory(dentry));
575 ni = ntfs_pathname_to_inode(vol, NULL, "/");
577 ERROR_WITH_ERRNO("Could not find root NTFS inode");
578 return WIMLIB_ERR_NTFS_3G;
580 ret = apply_file_attributes_and_security_data(ni, dentry, w);
581 if (ntfs_inode_close(ni) != 0) {
582 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
584 ret = WIMLIB_ERR_NTFS_3G;
589 /* Applies a WIM dentry to the NTFS volume */
590 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
592 struct ntfs_apply_args *args = arg;
593 ntfs_volume *vol = args->vol;
594 int extract_flags = args->extract_flags;
595 WIMStruct *w = args->w;
599 const char *dir_name;
601 if (dentry->extracted_file)
604 wimlib_assert(dentry->full_path_utf8);
606 DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
608 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
609 puts(dentry->full_path_utf8);
611 if (dentry_is_root(dentry))
612 return wim_apply_root_dentry_ntfs(dentry, vol, w);
614 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
621 dir_name = dentry->full_path_utf8;
623 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
625 DEBUG("Found NTFS inode for `%s'", dir_name);
628 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
630 return WIMLIB_ERR_NTFS_3G;
632 return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
635 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
637 struct ntfs_apply_args *args = arg;
638 ntfs_volume *vol = args->vol;
645 DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
647 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
649 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
650 dentry->full_path_utf8);
651 return WIMLIB_ERR_NTFS_3G;
655 p = put_u64(p, dentry->creation_time);
656 p = put_u64(p, dentry->last_write_time);
657 p = put_u64(p, dentry->last_access_time);
658 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
660 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
661 dentry->full_path_utf8);
662 ret = WIMLIB_ERR_NTFS_3G;
665 if (ntfs_inode_close(ni) != 0) {
667 ret = WIMLIB_ERR_NTFS_3G;
668 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
669 dentry->full_path_utf8);
674 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
679 DEBUG("Mounting NTFS volume `%s'", device);
680 vol = ntfs_mount(device, 0);
682 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
683 return WIMLIB_ERR_NTFS_3G;
685 struct ntfs_apply_args args = {
687 .extract_flags = extract_flags,
690 ret = for_dentry_in_tree(wim_root_dentry(w), wim_apply_dentry_ntfs,
695 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
696 printf("Setting timestamps of extracted files on NTFS "
697 "volume `%s'\n", device);
698 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
699 wim_apply_dentry_timestamps,
701 if (ret == 0 && (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE))
702 printf("Finished applying image %d of %s to NTFS "
705 w->filename ? w->filename : "WIM",
708 DEBUG("Unmounting NTFS volume `%s'", device);
709 if (ntfs_umount(vol, FALSE) != 0) {
710 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
712 ret = WIMLIB_ERR_NTFS_3G;
719 * API entry point for applying a WIM image to a NTFS volume.
721 * Please note that this is a NTFS *volume* and not a directory. The intention
722 * is that the volume contain an empty filesystem, and the WIM image contain a
723 * full filesystem to be applied to the volume.
725 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
726 const char *device, int flags)
731 return WIMLIB_ERR_INVALID_PARAM;
732 if (image == WIM_ALL_IMAGES) {
733 ERROR("Can only apply a single image when applying "
734 "directly to a NTFS volume");
735 return WIMLIB_ERR_INVALID_PARAM;
737 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
738 ERROR("Cannot specify symlink or hardlink flags when applying ");
739 ERROR("directly to a NTFS volume");
740 return WIMLIB_ERR_INVALID_PARAM;
742 ret = wimlib_select_image(w, image);
746 return do_wim_apply_image_ntfs(w, device, flags);
749 #else /* WITH_NTFS_3G */
750 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
751 const char *device, int flags)
753 ERROR("wimlib was compiled without support for NTFS-3g, so");
754 ERROR("we cannot apply a WIM image directly to a NTFS volume");
755 return WIMLIB_ERR_UNSUPPORTED;
757 #endif /* WITH_NTFS_3G */