4 * Apply a WIM image to a NTFS volume. Restore as much information as possible,
5 * including security data, file attributes, DOS names, and alternate data
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 3 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * 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"
35 #include "buffer_io.h"
37 #include "lookup_table.h"
39 #include <ntfs-3g/attrib.h>
40 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
41 #include <ntfs-3g/reparse.h>
42 #include <ntfs-3g/xattrs.h>
45 static int extract_wim_chunk_to_ntfs_attr(const u8 *buf, size_t len,
46 u64 offset, void *arg)
49 if (ntfs_attr_pwrite(na, offset, len, buf) == len) {
52 ERROR_WITH_ERRNO("Error extracting WIM resource to NTFS attribute");
53 return WIMLIB_ERR_WRITE;
58 * Extracts a WIM resource to a NTFS attribute.
61 extract_wim_resource_to_ntfs_attr(const struct wim_lookup_table_entry *lte,
64 return extract_wim_resource(lte, wim_resource_size(lte),
65 extract_wim_chunk_to_ntfs_attr, na);
68 /* Writes the data streams of a WIM inode to the data attributes of a NTFS
71 * @ni: The NTFS inode to which the streams are to be extracted.
73 * @dentry: The WIM dentry being extracted. The @d_inode member points to the
74 * corresponding WIM inode that contains the streams being extracted.
75 * The WIM dentry itself is only needed to provide a file path for
76 * better error messages.
78 * @progress_info: Progress information for the image application. The number
79 * of extracted bytes will be incremented by the uncompressed
80 * size of each stream extracted.
82 * Returns 0 on success, nonzero on failure.
84 static int write_ntfs_data_streams(ntfs_inode *ni, const struct wim_dentry *dentry,
85 union wimlib_progress_info *progress_info)
88 unsigned stream_idx = 0;
89 ntfschar *stream_name = AT_UNNAMED;
90 u32 stream_name_len = 0;
91 const struct wim_inode *inode = dentry->d_inode;
92 struct wim_lookup_table_entry *lte;
94 DEBUG("Writing %u NTFS data stream%s for `%s'",
96 (inode->i_num_ads == 0 ? "" : "s"),
97 dentry->full_path_utf8);
101 if (stream_name_len) {
102 /* Create an empty named stream. */
103 ret = ntfs_attr_add(ni, AT_DATA, stream_name,
104 stream_name_len, NULL, 0);
106 ERROR_WITH_ERRNO("Failed to create name data "
107 "stream for extracted file "
109 dentry->full_path_utf8);
110 ret = WIMLIB_ERR_NTFS_3G;
116 /* If there's no lookup table entry, it's an empty stream.
117 * Otherwise, open the attribute and extract the data. */
121 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
123 ERROR_WITH_ERRNO("Failed to open a data stream of "
124 "extracted file `%s'",
125 dentry->full_path_utf8);
126 ret = WIMLIB_ERR_NTFS_3G;
130 /* The WIM lookup table entry provides the stream
131 * length, so the NTFS attribute should be resized to
132 * this length before starting to extract the data. */
133 ret = ntfs_attr_truncate_solid(na, wim_resource_size(lte));
139 /* Actually extract the stream */
140 ret = extract_wim_resource_to_ntfs_attr(lte, na);
142 /* Close the attribute */
147 /* Record the number of bytes of uncompressed data that
148 * have been extracted. */
149 progress_info->extract.completed_bytes += wim_resource_size(lte);
151 if (stream_idx == inode->i_num_ads) /* Has the last stream been extracted? */
154 /* Get the name and lookup table entry for the next stream. */
155 stream_name = (ntfschar*)inode->i_ads_entries[stream_idx].stream_name;
156 stream_name_len = inode->i_ads_entries[stream_idx].stream_name_len / 2;
157 lte = inode->i_ads_entries[stream_idx].lte;
163 /* Open the NTFS inode that corresponds to the parent of a WIM dentry. Returns
164 * the opened inode, or NULL on failure. */
165 static ntfs_inode *dentry_open_parent_ni(const struct wim_dentry *dentry,
169 const char *dir_name;
173 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
180 dir_name = dentry->full_path_utf8;
181 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
183 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
191 * Makes a NTFS hard link.
193 * The hard link is named @from_dentry->file_name and is located under the
194 * directory specified by @dir_ni, and it is made to point to the previously
195 * extracted file located at @inode->i_extracted_file.
197 * Or, in other words, this adds a new name @from_dentry->full_path_utf8 to an
198 * existing NTFS inode which already has a name @inode->i_extracted_file.
200 * Return 0 on success, nonzero on failure.
202 static int apply_ntfs_hardlink(const struct wim_dentry *from_dentry,
203 const struct wim_inode *inode,
204 ntfs_inode **dir_ni_p)
213 ret = ntfs_inode_close(dir_ni);
216 ERROR_WITH_ERRNO("Error closing directory");
217 return WIMLIB_ERR_NTFS_3G;
220 DEBUG("Extracting NTFS hard link `%s' => `%s'",
221 from_dentry->full_path_utf8, inode->i_extracted_file);
223 to_ni = ntfs_pathname_to_inode(vol, NULL, inode->i_extracted_file);
225 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
226 inode->i_extracted_file);
227 return WIMLIB_ERR_NTFS_3G;
230 dir_ni = dentry_open_parent_ni(from_dentry, vol);
232 ntfs_inode_close(to_ni);
233 return WIMLIB_ERR_NTFS_3G;
238 ret = ntfs_link(to_ni, dir_ni,
239 (ntfschar*)from_dentry->file_name,
240 from_dentry->file_name_len / 2);
241 if (ntfs_inode_close_in_dir(to_ni, dir_ni) || ret != 0) {
242 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
243 from_dentry->full_path_utf8,
244 inode->i_extracted_file);
245 ret = WIMLIB_ERR_NTFS_3G;
250 /* Transfers file attributes and possibly a security descriptor from a WIM inode
253 * @ni: The NTFS inode to apply the metadata to.
254 * @dir_ni: The NTFS inode for a directory containing @ni.
255 * @dentry: The WIM dentry whose inode contains the metadata to apply.
256 * @w: The WIMStruct for the WIM, through which the table of security
257 * descriptors can be accessed.
259 * Returns 0 on success, nonzero on failure.
262 apply_file_attributes_and_security_data(ntfs_inode *ni,
264 const struct wim_dentry *dentry,
268 struct SECURITY_CONTEXT ctx;
270 const struct wim_inode *inode;
272 inode = dentry->d_inode;
274 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
275 dentry->full_path_utf8, inode->i_attributes);
277 attributes_le32 = cpu_to_le32(inode->i_attributes);
278 memset(&ctx, 0, sizeof(ctx));
280 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
282 (const char*)&attributes_le32,
285 ERROR("Failed to set NTFS file attributes on `%s'",
286 dentry->full_path_utf8);
287 return WIMLIB_ERR_NTFS_3G;
289 if (inode->i_security_id != -1) {
291 const struct wim_security_data *sd;
293 sd = wim_const_security_data(w);
294 wimlib_assert(inode->i_security_id < sd->num_entries);
295 desc = (const char *)sd->descriptors[inode->i_security_id];
296 DEBUG("Applying security descriptor %d to `%s'",
297 inode->i_security_id, dentry->full_path_utf8);
299 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
301 sd->sizes[inode->i_security_id], 0);
304 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
305 dentry->full_path_utf8);
306 return WIMLIB_ERR_NTFS_3G;
313 * Transfers the reparse data from a WIM inode (which must represent a reparse
314 * point) to a NTFS inode.
316 static int apply_reparse_data(ntfs_inode *ni, const struct wim_dentry *dentry,
317 union wimlib_progress_info *progress_info)
319 struct wim_lookup_table_entry *lte;
322 lte = inode_unnamed_lte_resolved(dentry->d_inode);
324 DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
327 ERROR("Could not find reparse data for `%s'",
328 dentry->full_path_utf8);
329 return WIMLIB_ERR_INVALID_DENTRY;
332 if (wim_resource_size(lte) >= 0xffff) {
333 ERROR("Reparse data of `%s' is too long (%"PRIu64" bytes)",
334 dentry->full_path_utf8, wim_resource_size(lte));
335 return WIMLIB_ERR_INVALID_DENTRY;
338 u8 reparse_data_buf[8 + wim_resource_size(lte)];
339 u8 *p = reparse_data_buf;
340 p = put_u32(p, dentry->d_inode->i_reparse_tag); /* ReparseTag */
341 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
342 p = put_u16(p, 0); /* Reserved */
344 ret = read_full_wim_resource(lte, p, 0);
348 ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
349 wim_resource_size(lte) + 8, 0);
351 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
352 dentry->full_path_utf8);
353 return WIMLIB_ERR_NTFS_3G;
355 progress_info->extract.completed_bytes += wim_resource_size(lte);
359 static int do_apply_dentry_ntfs(struct wim_dentry *dentry, ntfs_inode *dir_ni,
360 struct apply_args *args);
363 * If @dentry is part of a hard link group, search for hard-linked dentries in
364 * the same directory that have a nonempty DOS (short) filename. There should
365 * be exactly 0 or 1 such dentries. If there is 1, extract that dentry first,
366 * so that the DOS name is correctly associated with the corresponding long name
367 * in the Win32 namespace, and not any of the additional names in the POSIX
368 * namespace created from hard links.
370 static int preapply_dentry_with_dos_name(struct wim_dentry *dentry,
371 ntfs_inode **dir_ni_p,
372 struct apply_args *args)
374 struct wim_dentry *other;
375 struct wim_dentry *dentry_with_dos_name;
377 dentry_with_dos_name = NULL;
378 inode_for_each_dentry(other, dentry->d_inode) {
379 if (other != dentry && (dentry->parent == other->parent)
380 && other->short_name_len)
382 if (dentry_with_dos_name) {
383 ERROR("Found multiple DOS names for file `%s' "
384 "in the same directory",
385 dentry_with_dos_name->full_path_utf8);
386 return WIMLIB_ERR_INVALID_DENTRY;
388 dentry_with_dos_name = other;
391 /* If there's a dentry with a DOS name, extract it first */
392 if (dentry_with_dos_name && !dentry_with_dos_name->is_extracted) {
394 ntfs_volume *vol = (*dir_ni_p)->vol;
396 DEBUG("pre-applying DOS name `%s'",
397 dentry_with_dos_name->full_path_utf8);
398 ret = do_apply_dentry_ntfs(dentry_with_dos_name,
403 *dir_ni_p = dentry_open_parent_ni(dentry, vol);
405 return WIMLIB_ERR_NTFS_3G;
411 * Applies a WIM dentry to a NTFS filesystem.
413 * @dentry: The WIM dentry to apply
414 * @dir_ni: The NTFS inode for the parent directory
416 * @return: 0 on success; nonzero on failure.
418 static int do_apply_dentry_ntfs(struct wim_dentry *dentry, ntfs_inode *dir_ni,
419 struct apply_args *args)
423 ntfs_inode *ni = NULL;
424 ntfs_volume *vol = dir_ni->vol;
425 struct wim_inode *inode = dentry->d_inode;
426 dentry->is_extracted = 1;
428 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
431 /* If this dentry is hard-linked to any other dentries in the
432 * same directory, make sure to apply the one (if any) with a
433 * DOS name first. Otherwise, NTFS-3g might not assign the file
434 * names correctly. */
435 if (dentry->short_name_len == 0) {
436 ret = preapply_dentry_with_dos_name(dentry,
442 if (inode->i_nlink > 1) {
443 /* Inode has multiple dentries referencing it. */
445 if (inode->i_extracted_file) {
446 /* Already extracted another dentry in the hard
447 * link group. Make a hard link instead of
448 * extracting the file data. */
449 ret = apply_ntfs_hardlink(dentry, inode,
452 goto out_set_dos_name;
454 goto out_close_dir_ni;
456 /* None of the dentries of this inode have been
457 * extracted yet, so go ahead and extract the
459 FREE(inode->i_extracted_file);
460 inode->i_extracted_file = STRDUP(dentry->full_path_utf8);
461 if (!inode->i_extracted_file) {
462 ret = WIMLIB_ERR_NOMEM;
463 goto out_close_dir_ni;
469 /* Create a NTFS directory or file.
471 * Note: For symbolic links that are not directory junctions, S_IFREG is
472 * passed here, since the reparse data and file attributes are set
474 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
475 dentry->file_name_len / 2, type);
478 ERROR_WITH_ERRNO("Could not create NTFS inode for `%s'",
479 dentry->full_path_utf8);
480 ret = WIMLIB_ERR_NTFS_3G;
481 goto out_close_dir_ni;
484 /* Write the data streams, unless this is a directory or reparse point
486 if (!(inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
487 FILE_ATTRIBUTE_DIRECTORY))) {
488 ret = write_ntfs_data_streams(ni, dentry, &args->progress);
490 goto out_close_dir_ni;
494 ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry,
497 goto out_close_dir_ni;
499 if (inode->i_attributes & FILE_ATTR_REPARSE_POINT) {
500 ret = apply_reparse_data(ni, dentry, &args->progress);
502 goto out_close_dir_ni;
506 /* Set DOS (short) name if given */
507 if (dentry->short_name_len != 0) {
509 char *short_name_utf8;
510 size_t short_name_utf8_len;
511 ret = utf16_to_utf8(dentry->short_name,
512 dentry->short_name_len,
514 &short_name_utf8_len);
516 goto out_close_dir_ni;
519 /* Hardlink was made; linked inode needs to be looked up
521 ni = ntfs_pathname_to_inode(vol, dir_ni,
522 dentry->file_name_utf8);
524 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
525 dentry->full_path_utf8);
526 FREE(short_name_utf8);
527 ret = WIMLIB_ERR_NTFS_3G;
528 goto out_close_dir_ni;
532 DEBUG("Setting short (DOS) name of `%s' to %s",
533 dentry->full_path_utf8, short_name_utf8);
535 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
536 short_name_utf8_len, 0);
537 FREE(short_name_utf8);
539 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
540 dentry->full_path_utf8);
541 ret = WIMLIB_ERR_NTFS_3G;
543 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
550 if (ntfs_inode_close_in_dir(ni, dir_ni)) {
552 ret = WIMLIB_ERR_NTFS_3G;
553 ERROR_WITH_ERRNO("Failed to close inode for `%s'",
554 dentry->full_path_utf8);
557 if (ntfs_inode_close(dir_ni)) {
559 ret = WIMLIB_ERR_NTFS_3G;
560 ERROR_WITH_ERRNO("Failed to close inode of directory "
561 "containing `%s'", dentry->full_path_utf8);
564 wimlib_assert(ni == NULL);
569 static int apply_root_dentry_ntfs(const struct wim_dentry *dentry,
570 ntfs_volume *vol, const WIMStruct *w)
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, 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 int apply_dentry_ntfs(struct wim_dentry *dentry, void *arg)
592 struct apply_args *args = arg;
593 ntfs_volume *vol = args->vol;
594 WIMStruct *w = args->w;
597 if (dentry_is_root(dentry))
598 return apply_root_dentry_ntfs(dentry, vol, w);
600 dir_ni = dentry_open_parent_ni(dentry, vol);
602 return do_apply_dentry_ntfs(dentry, dir_ni, arg);
604 return WIMLIB_ERR_NTFS_3G;
607 /* Transfers the 100-nanosecond precision timestamps from a WIM dentry to a NTFS
609 int apply_dentry_timestamps_ntfs(struct wim_dentry *dentry, void *arg)
611 struct apply_args *args = arg;
612 ntfs_volume *vol = args->vol;
618 DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
620 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
622 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
623 dentry->full_path_utf8);
624 return WIMLIB_ERR_NTFS_3G;
628 p = put_u64(p, dentry->d_inode->i_creation_time);
629 p = put_u64(p, dentry->d_inode->i_last_write_time);
630 p = put_u64(p, dentry->d_inode->i_last_access_time);
631 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
633 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
634 dentry->full_path_utf8);
635 ret = WIMLIB_ERR_NTFS_3G;
638 if (ntfs_inode_close(ni) != 0) {
640 ret = WIMLIB_ERR_NTFS_3G;
641 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
642 dentry->full_path_utf8);