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>
34 #include "wimlib_internal.h"
36 #include "lookup_table.h"
38 #include <ntfs-3g/layout.h>
39 #include <ntfs-3g/acls.h>
40 #include <ntfs-3g/attrib.h>
41 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
42 #include <ntfs-3g/xattrs.h>
43 #include <ntfs-3g/reparse.h>
48 * Extracts a WIM resource to a NTFS attribute.
51 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
54 u64 bytes_remaining = wim_resource_size(lte);
55 u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
58 u8 hash[SHA1_HASH_SIZE];
63 while (bytes_remaining) {
64 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
65 ret = read_wim_resource(lte, buf, to_read, offset, 0);
68 sha1_update(&ctx, buf, to_read);
69 if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
70 ERROR_WITH_ERRNO("Error extracting WIM resource");
71 return WIMLIB_ERR_WRITE;
73 bytes_remaining -= to_read;
76 sha1_final(hash, &ctx);
77 if (!hashes_equal(hash, lte->hash)) {
78 ERROR("Invalid checksum on a WIM resource "
79 "(detected when extracting to NTFS stream)");
80 ERROR("The following WIM resource is invalid:");
81 print_lookup_table_entry(lte);
82 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
87 /* Writes the data streams to a NTFS file
89 * @ni: The NTFS inode for the file.
90 * @inode: The WIM dentry that has an inode containing the streams.
92 * Returns 0 on success, nonzero on failure.
94 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
95 struct apply_args *args)
98 unsigned stream_idx = 0;
99 ntfschar *stream_name = AT_UNNAMED;
100 u32 stream_name_len = 0;
101 const struct inode *inode = dentry->d_inode;
103 DEBUG("Writing %u NTFS data stream%s for `%s'",
105 (inode->num_ads == 0 ? "" : "s"),
106 dentry->full_path_utf8);
109 struct lookup_table_entry *lte;
112 lte = inode_stream_lte_resolved(inode, stream_idx);
114 if (stream_name_len) {
115 /* Create an empty named stream. */
116 ret = ntfs_attr_add(ni, AT_DATA, stream_name,
117 stream_name_len, NULL, 0);
119 ERROR_WITH_ERRNO("Failed to create name data "
120 "stream for extracted file "
122 dentry->full_path_utf8);
123 ret = WIMLIB_ERR_NTFS_3G;
128 /* If there's no lookup table entry, it's an empty stream.
129 * Otherwise, we must open the attribute and extract the data.
132 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
134 ERROR_WITH_ERRNO("Failed to open a data stream of "
135 "extracted file `%s'",
136 dentry->full_path_utf8);
137 ret = WIMLIB_ERR_NTFS_3G;
140 ret = extract_wim_resource_to_ntfs_attr(lte, na);
143 args->progress.extract.completed_bytes += wim_resource_size(lte);
146 if (stream_idx == inode->num_ads)
148 stream_name = (ntfschar*)inode->ads_entries[stream_idx].stream_name;
149 stream_name_len = inode->ads_entries[stream_idx].stream_name_len / 2;
156 * Makes a NTFS hard link
158 * It is named @from_dentry->file_name and is located under the directory
159 * specified by @dir_ni, and it is made to point to the previously extracted
160 * file located at @inode->extracted_file.
162 * Return 0 on success, nonzero on failure.
164 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
165 const struct inode *inode,
167 ntfs_inode **to_ni_ret)
172 const char *dir_name;
177 wimlib_assert(dentry_is_regular_file(from_dentry)
178 && inode_is_regular_file(inode));
180 if (ntfs_inode_close(dir_ni) != 0) {
181 ERROR_WITH_ERRNO("Error closing directory");
182 return WIMLIB_ERR_NTFS_3G;
187 DEBUG("Extracting NTFS hard link `%s' => `%s'",
188 from_dentry->full_path_utf8, inode->extracted_file);
190 to_ni = ntfs_pathname_to_inode(vol, NULL, inode->extracted_file);
192 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
193 inode->extracted_file);
194 return WIMLIB_ERR_NTFS_3G;
196 p = from_dentry->full_path_utf8 + from_dentry->full_path_utf8_len;
203 dir_name = from_dentry->full_path_utf8;
205 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
207 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
208 from_dentry->full_path_utf8);
210 return WIMLIB_ERR_NTFS_3G;
214 ret = ntfs_link(to_ni, dir_ni,
215 (ntfschar*)from_dentry->file_name,
216 from_dentry->file_name_len / 2);
218 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
219 from_dentry->full_path_utf8,
220 inode->extracted_file);
221 ret = WIMLIB_ERR_NTFS_3G;
228 apply_file_attributes_and_security_data(ntfs_inode *ni,
230 const struct dentry *dentry,
233 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
234 dentry->full_path_utf8, dentry->d_inode->attributes);
236 struct SECURITY_CONTEXT ctx;
238 attributes_le32 = cpu_to_le32(dentry->d_inode->attributes);
239 memset(&ctx, 0, sizeof(ctx));
241 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
243 (const char*)&attributes_le32,
246 ERROR("Failed to set NTFS file attributes on `%s'",
247 dentry->full_path_utf8);
248 return WIMLIB_ERR_NTFS_3G;
250 if (dentry->d_inode->security_id != -1) {
251 const struct wim_security_data *sd;
252 const char *descriptor;
254 sd = wim_const_security_data(w);
255 wimlib_assert(dentry->d_inode->security_id < sd->num_entries);
256 descriptor = (const char *)sd->descriptors[dentry->d_inode->security_id];
257 DEBUG("Applying security descriptor %d to `%s'",
258 dentry->d_inode->security_id, dentry->full_path_utf8);
260 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
261 ni, dir_ni, descriptor,
262 sd->sizes[dentry->d_inode->security_id], 0);
265 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
266 dentry->full_path_utf8);
267 return WIMLIB_ERR_NTFS_3G;
273 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
274 struct apply_args *args)
276 struct lookup_table_entry *lte;
279 lte = inode_unnamed_lte_resolved(dentry->d_inode);
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 (%"PRIu64" 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->d_inode->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, 0);
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;
312 args->progress.extract.completed_bytes += wim_resource_size(lte);
316 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
317 struct apply_args *args);
320 * If @dentry is part of a hard link group, search for hard-linked dentries in
321 * the same directory that have a nonempty DOS (short) filename. There should
322 * be exactly 0 or 1 such dentries. If there is 1, extract that dentry first,
323 * so that the DOS name is correctly associated with the corresponding long name
324 * in the Win32 namespace, and not any of the additional names in the POSIX
325 * namespace created from hard links.
327 static int preapply_dentry_with_dos_name(struct dentry *dentry,
328 ntfs_inode **dir_ni_p,
329 struct apply_args *args)
331 struct dentry *other;
332 struct dentry *dentry_with_dos_name;
334 dentry_with_dos_name = NULL;
335 inode_for_each_dentry(other, dentry->d_inode) {
336 if (other != dentry && (dentry->parent == other->parent)
337 && other->short_name_len)
339 if (dentry_with_dos_name) {
340 ERROR("Found multiple DOS names for file `%s' "
341 "in the same directory",
342 dentry_with_dos_name->full_path_utf8);
343 return WIMLIB_ERR_INVALID_DENTRY;
345 dentry_with_dos_name = other;
348 /* If there's a dentry with a DOS name, extract it first */
349 if (dentry_with_dos_name && !dentry_with_dos_name->is_extracted) {
351 const char *dir_name;
354 ntfs_volume *vol = (*dir_ni_p)->vol;
356 DEBUG("pre-applying DOS name `%s'",
357 dentry_with_dos_name->full_path_utf8);
358 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
362 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
369 dir_name = dentry->full_path_utf8;
371 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
374 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
376 return WIMLIB_ERR_NTFS_3G;
383 * Applies a WIM dentry to a NTFS filesystem.
385 * @dentry: The WIM dentry to apply
386 * @dir_ni: The NTFS inode for the parent directory
388 * @return: 0 on success; nonzero on failure.
390 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
391 struct apply_args *args)
395 ntfs_inode *ni = NULL;
396 bool is_hardlink = false;
397 ntfs_volume *vol = dir_ni->vol;
398 struct inode *inode = dentry->d_inode;
399 dentry->is_extracted = true;
401 if (inode->attributes & FILE_ATTRIBUTE_DIRECTORY) {
404 /* Apply hard-linked directory in same directory with DOS name
405 * (if there is one) before this dentry */
406 if (dentry->short_name_len == 0) {
407 ret = preapply_dentry_with_dos_name(dentry,
415 if (inode->link_count > 1) {
416 /* Already extracted another dentry in the hard link
417 * group. We can make a hard link instead of extracting
419 if (inode->extracted_file) {
420 ret = wim_apply_hardlink_ntfs(dentry, inode,
424 goto out_close_dir_ni;
426 goto out_set_dos_name;
428 /* Can't make a hard link; extract the file itself */
429 FREE(inode->extracted_file);
430 inode->extracted_file = STRDUP(dentry->full_path_utf8);
431 if (!inode->extracted_file) {
432 ret = WIMLIB_ERR_NOMEM;
433 goto out_close_dir_ni;
439 * Create a directory or file.
441 * Note: For symbolic links that are not directory junctions, pass
442 * S_IFREG here, since we manually set the reparse data later.
444 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
445 dentry->file_name_len / 2, type);
448 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
449 dentry->full_path_utf8);
450 ret = WIMLIB_ERR_NTFS_3G;
451 goto out_close_dir_ni;
454 /* Write the data streams, unless this is a directory or reparse point
456 if (!(inode->attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
457 FILE_ATTRIBUTE_DIRECTORY))) {
458 ret = write_ntfs_data_streams(ni, dentry, args);
460 goto out_close_dir_ni;
464 ret = apply_file_attributes_and_security_data(ni, dir_ni,
467 goto out_close_dir_ni;
469 if (inode->attributes & FILE_ATTR_REPARSE_POINT) {
470 ret = apply_reparse_data(ni, dentry, args);
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, 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 int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
587 struct 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->is_extracted)
599 if (extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
600 if (inode_unnamed_lte_resolved(dentry->d_inode))
604 DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
606 if ((extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
609 args->progress.extract.cur_path = dentry->full_path_utf8;
610 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
614 if (dentry_is_root(dentry))
615 return wim_apply_root_dentry_ntfs(dentry, vol, w);
617 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
624 dir_name = dentry->full_path_utf8;
626 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
629 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
631 return WIMLIB_ERR_NTFS_3G;
633 return do_wim_apply_dentry_ntfs(dentry, dir_ni, arg);
636 int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
638 struct apply_args *args = arg;
639 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->d_inode->creation_time);
656 p = put_u64(p, dentry->d_inode->last_write_time);
657 p = put_u64(p, dentry->d_inode->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);