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, 2013 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>
47 extract_wim_chunk_to_ntfs_attr(const void *buf, size_t len,
48 u64 offset, void *arg)
51 if (ntfs_attr_pwrite(na, offset, len, buf) == len) {
54 ERROR_WITH_ERRNO("Error extracting WIM resource to NTFS attribute");
55 return WIMLIB_ERR_WRITE;
60 * Extracts a WIM resource to a NTFS attribute.
63 extract_wim_resource_to_ntfs_attr(const struct wim_lookup_table_entry *lte,
66 return extract_wim_resource(lte, wim_resource_size(lte),
67 extract_wim_chunk_to_ntfs_attr, na);
70 /* Writes the data streams of a WIM inode to the data attributes of a NTFS
73 * @ni: The NTFS inode to which the streams are to be extracted.
75 * @dentry: The WIM dentry being extracted. The @d_inode member points to the
76 * corresponding WIM inode that contains the streams being extracted.
77 * The WIM dentry itself is only needed to provide a file path for
78 * better error messages.
80 * @progress_info: Progress information for the image application. The number
81 * of extracted bytes will be incremented by the uncompressed
82 * size of each stream extracted.
84 * Returns 0 on success, nonzero on failure.
87 write_ntfs_data_streams(ntfs_inode *ni, const struct wim_dentry *dentry,
88 union wimlib_progress_info *progress_info)
91 unsigned stream_idx = 0;
92 ntfschar *stream_name = AT_UNNAMED;
93 u32 stream_name_nbytes = 0;
94 const struct wim_inode *inode = dentry->d_inode;
95 struct wim_lookup_table_entry *lte;
97 DEBUG("Writing %u NTFS data stream%s for `%s'",
99 (inode->i_num_ads == 0 ? "" : "s"),
104 if (stream_name_nbytes) {
105 /* Skip special UNIX data entries (see documentation for
106 * WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) */
107 if (stream_name_nbytes == WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES
108 && !memcmp(stream_name,
109 WIMLIB_UNIX_DATA_TAG_UTF16LE,
110 WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES))
113 /* Create an empty named stream. */
114 ret = ntfs_attr_add(ni, AT_DATA, stream_name,
115 stream_name_nbytes / 2, NULL, 0);
117 ERROR_WITH_ERRNO("Failed to create named data "
118 "stream for extracted file "
121 ret = WIMLIB_ERR_NTFS_3G;
127 /* If there's no lookup table entry, it's an empty stream.
128 * Otherwise, open the attribute and extract the data. */
132 na = ntfs_attr_open(ni, AT_DATA, stream_name,
133 stream_name_nbytes / 2);
135 ERROR_WITH_ERRNO("Failed to open a data stream of "
136 "extracted file `%s'",
138 ret = WIMLIB_ERR_NTFS_3G;
142 /* The WIM lookup table entry provides the stream
143 * length, so the NTFS attribute should be resized to
144 * this length before starting to extract the data. */
145 ret = ntfs_attr_truncate_solid(na, wim_resource_size(lte));
151 /* Actually extract the stream */
152 ret = extract_wim_resource_to_ntfs_attr(lte, na);
154 /* Close the attribute */
159 /* Record the number of bytes of uncompressed data that
160 * have been extracted. */
161 progress_info->extract.completed_bytes += wim_resource_size(lte);
164 if (stream_idx == inode->i_num_ads) /* Has the last stream been extracted? */
167 /* Get the name and lookup table entry for the next stream. */
168 stream_name = inode->i_ads_entries[stream_idx].stream_name;
169 stream_name_nbytes = inode->i_ads_entries[stream_idx].stream_name_nbytes;
170 lte = inode->i_ads_entries[stream_idx].lte;
176 /* Open the NTFS inode that corresponds to the parent of a WIM dentry. Returns
177 * the opened inode, or NULL on failure. */
179 dentry_open_parent_ni(const struct wim_dentry *dentry, ntfs_volume *vol)
182 const char *dir_name;
186 p = dentry->full_path + dentry->full_path_nbytes;
193 dir_name = dentry->full_path;
194 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
196 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
204 * Makes a NTFS hard link.
206 * The hard link is named @from_dentry->file_name and is located under the
207 * directory specified by @dir_ni, and it is made to point to the previously
208 * extracted file located at @inode->i_extracted_file.
210 * Or, in other words, this adds a new name @from_dentry->full_path to an
211 * existing NTFS inode which already has a name @inode->i_extracted_file.
213 * The new name is made in the POSIX namespace (this is the behavior of
216 * Return 0 on success, nonzero on failure. dir_ni is closed either way.
219 apply_ntfs_hardlink(const struct wim_dentry *from_dentry,
220 const struct wim_inode *inode,
228 ret = ntfs_inode_close(dir_ni);
230 ERROR_WITH_ERRNO("Error closing directory");
231 return WIMLIB_ERR_NTFS_3G;
234 DEBUG("Extracting NTFS hard link `%s' => `%s'",
235 from_dentry->full_path, inode->i_extracted_file);
237 to_ni = ntfs_pathname_to_inode(vol, NULL, inode->i_extracted_file);
239 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
240 inode->i_extracted_file);
241 return WIMLIB_ERR_NTFS_3G;
244 dir_ni = dentry_open_parent_ni(from_dentry, vol);
246 ntfs_inode_close(to_ni);
247 return WIMLIB_ERR_NTFS_3G;
250 ret = ntfs_link(to_ni, dir_ni,
251 from_dentry->file_name,
252 from_dentry->file_name_nbytes / 2);
253 ret |= ntfs_inode_close(dir_ni);
254 ret |= ntfs_inode_close(to_ni);
256 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
257 from_dentry->full_path,
258 inode->i_extracted_file);
259 ret = WIMLIB_ERR_NTFS_3G;
264 /* Transfers file attributes and possibly a security descriptor from a WIM inode
267 * @ni: The NTFS inode to apply the metadata to.
268 * @dir_ni: The NTFS inode for a directory containing @ni.
269 * @dentry: The WIM dentry whose inode contains the metadata to apply.
270 * @w: The WIMStruct for the WIM, through which the table of security
271 * descriptors can be accessed.
273 * Returns 0 on success, nonzero on failure.
276 apply_file_attributes_and_security_data(ntfs_inode *ni,
278 const struct wim_dentry *dentry,
283 struct SECURITY_CONTEXT ctx;
285 const struct wim_inode *inode;
287 inode = dentry->d_inode;
289 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
290 dentry->full_path, inode->i_attributes);
292 attributes_le32 = cpu_to_le32(inode->i_attributes);
293 memset(&ctx, 0, sizeof(ctx));
295 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
297 (const char*)&attributes_le32,
300 ERROR("Failed to set NTFS file attributes on `%s'",
302 return WIMLIB_ERR_NTFS_3G;
304 if (inode->i_security_id != -1 &&
305 !(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
308 const struct wim_security_data *sd;
310 sd = wim_const_security_data(w);
311 wimlib_assert(inode->i_security_id < sd->num_entries);
312 desc = (const char *)sd->descriptors[inode->i_security_id];
313 DEBUG("Applying security descriptor %d to `%s'",
314 inode->i_security_id, dentry->full_path);
316 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
318 sd->sizes[inode->i_security_id], 0);
321 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
323 return WIMLIB_ERR_NTFS_3G;
330 * Transfers the reparse data from a WIM inode (which must represent a reparse
331 * point) to a NTFS inode.
334 apply_reparse_data(ntfs_inode *ni, const struct wim_dentry *dentry,
335 union wimlib_progress_info *progress_info)
337 struct wim_lookup_table_entry *lte;
340 lte = inode_unnamed_lte_resolved(dentry->d_inode);
342 DEBUG("Applying reparse data to `%s'", dentry->full_path);
345 ERROR("Could not find reparse data for `%s'",
347 return WIMLIB_ERR_INVALID_DENTRY;
350 if (wim_resource_size(lte) >= 0xffff) {
351 ERROR("Reparse data of `%s' is too long (%"PRIu64" bytes)",
352 dentry->full_path, wim_resource_size(lte));
353 return WIMLIB_ERR_INVALID_DENTRY;
356 u8 reparse_data_buf[8 + wim_resource_size(lte)];
357 u8 *p = reparse_data_buf;
358 p = put_u32(p, dentry->d_inode->i_reparse_tag); /* ReparseTag */
359 DEBUG("ReparseTag = %#x", dentry->d_inode->i_reparse_tag);
360 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
361 p = put_u16(p, 0); /* Reserved */
363 ret = read_full_wim_resource(lte, p, 0);
367 ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
368 wim_resource_size(lte) + 8, 0);
370 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
372 return WIMLIB_ERR_NTFS_3G;
374 progress_info->extract.completed_bytes += wim_resource_size(lte);
379 * Applies a WIM dentry to a NTFS filesystem.
381 * @dentry: The WIM dentry to apply
382 * @dir_ni: The NTFS inode for the parent directory
384 * @return: 0 on success; nonzero on failure.
387 do_apply_dentry_ntfs(struct wim_dentry *dentry, ntfs_inode *dir_ni,
388 struct apply_args *args)
392 ntfs_inode *ni = NULL;
393 struct wim_inode *inode = dentry->d_inode;
394 dentry->is_extracted = 1;
396 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
400 if (inode->i_nlink > 1) {
401 /* Inode has multiple dentries referencing it. */
402 if (inode->i_extracted_file) {
403 /* Already extracted another dentry in the hard
404 * link group. Make a hard link instead of
405 * extracting the file data. */
406 ret = apply_ntfs_hardlink(dentry, inode, dir_ni);
407 /* dir_ni was closed */
410 /* None of the dentries of this inode have been
411 * extracted yet, so go ahead and extract the
413 FREE(inode->i_extracted_file);
414 inode->i_extracted_file = STRDUP(dentry->full_path);
415 if (!inode->i_extracted_file) {
416 ret = WIMLIB_ERR_NOMEM;
417 goto out_close_dir_ni;
423 /* Create a NTFS directory or file.
425 * Note: For symbolic links that are not directory junctions, S_IFREG is
426 * passed here, since the reparse data and file attributes are set
428 ni = ntfs_create(dir_ni, 0, dentry->file_name,
429 dentry->file_name_nbytes / 2, type);
432 ERROR_WITH_ERRNO("Could not create NTFS inode for `%s'",
434 ret = WIMLIB_ERR_NTFS_3G;
435 goto out_close_dir_ni;
438 /* Write the data streams, unless this is a directory or reparse point
440 if (!(inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
441 FILE_ATTRIBUTE_DIRECTORY))) {
442 ret = write_ntfs_data_streams(ni, dentry, &args->progress);
444 goto out_close_dir_ni;
447 ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry,
449 args->extract_flags);
451 goto out_close_dir_ni;
453 if (inode->i_attributes & FILE_ATTR_REPARSE_POINT) {
454 ret = apply_reparse_data(ni, dentry, &args->progress);
456 goto out_close_dir_ni;
459 /* Set DOS (short) name if given */
460 if (dentry_has_short_name(dentry)) {
461 char *short_name_mbs;
462 size_t short_name_mbs_nbytes;
463 ret = utf16le_to_tstr(dentry->short_name,
464 dentry->short_name_nbytes,
466 &short_name_mbs_nbytes);
468 goto out_close_dir_ni;
470 DEBUG("Setting short (DOS) name of `%s' to %s",
471 dentry->full_path, short_name_mbs);
473 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_mbs,
474 short_name_mbs_nbytes, 0);
475 FREE(short_name_mbs);
477 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
479 ret = WIMLIB_ERR_NTFS_3G;
481 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
487 if (ntfs_inode_close_in_dir(ni, dir_ni)) {
489 ret = WIMLIB_ERR_NTFS_3G;
490 ERROR_WITH_ERRNO("Failed to close inode for `%s'",
494 if (ntfs_inode_close(dir_ni)) {
496 ret = WIMLIB_ERR_NTFS_3G;
497 ERROR_WITH_ERRNO("Failed to close inode of directory "
498 "containing `%s'", dentry->full_path);
506 apply_root_dentry_ntfs(const struct wim_dentry *dentry,
507 ntfs_volume *vol, const WIMStruct *w,
513 ni = ntfs_pathname_to_inode(vol, NULL, "/");
515 ERROR_WITH_ERRNO("Could not find root NTFS inode");
516 return WIMLIB_ERR_NTFS_3G;
518 ret = apply_file_attributes_and_security_data(ni, ni, dentry, w,
520 if (ntfs_inode_close(ni) != 0) {
521 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
523 ret = WIMLIB_ERR_NTFS_3G;
528 /* Applies a WIM dentry to the NTFS volume */
530 apply_dentry_ntfs(struct wim_dentry *dentry, void *arg)
532 struct apply_args *args = arg;
533 ntfs_volume *vol = args->vol;
534 WIMStruct *w = args->w;
535 struct wim_dentry *orig_dentry;
536 struct wim_dentry *other;
539 /* Treat the root dentry specially. */
540 if (dentry_is_root(dentry))
541 return apply_root_dentry_ntfs(dentry, vol, w,
542 args->extract_flags);
544 /* NTFS filename namespaces need careful consideration. A name for a
545 * NTFS file may be in either the POSIX, Win32, DOS, or Win32+DOS
546 * namespaces. A NTFS file (a.k.a. inode) may have multiple names in
547 * multiple directories (i.e. hard links); however, a NTFS file can have
548 * at most 1 DOS name total. Furthermore, a Win32 name is always
549 * associated with a DOS name (either as a Win32+DOS name, or a Win32
550 * name and a DOS name separately), which implies that a NTFS file can
551 * have at most 1 Win32 name.
553 * A WIM dentry just contains a "long name", which wimlib makes sure is
554 * non-empty, and a "short name", which may be empty. So, wimlib must
555 * map these to the correct NTFS names. wimlib collects all WIM
556 * dentries that map to the same NTFS inode and factors out the common
557 * information into a 'struct wim_inode', so this should make the
558 * mapping a little more obvious. As a NTFS file can have at most 1 DOS
559 * name, a WIM inode cannot have more than 1 dentry with a non-empty
560 * short name, and this is checked in the verify_inode() function in
561 * verify.c. Furthermore, a WIM dentry, if any, that has a DOS name
562 * must have a long name that corresponds to a Win32 name or Win32+DOS
565 * WIM dentries that have a long name but no associated short name are
566 * assumed to be in the POSIX namespace.
568 * So, given a WIM inode that is to map to a NTFS inode, we must apply
569 * the Win32 and DOS or Win32+DOS names, if they exist, then any
570 * additional (POSIX) names. A caveat when actually doing this: as
571 * confirmed by the libntfs-3g authors, ntfs_set_ntfs_dos_name() is only
572 * guaranteed to associate a DOS name with the appropriate long name if
573 * it's called when that long name is the only one in existence for that
574 * file. So, this implies that the correct ordering of function calls
575 * to extract a NTFS file are:
577 * if (file has a DOS name) {
578 * - Call ntfs_create() to create long name associated with
579 * the DOS name (this initially creates a POSIX name)
580 * - Call ntfs_set_ntfs_dos_name() to associate a DOS name
581 * with the long name just created. This either changes
582 * the POSIX name to Win32+DOS, or changes the POSIX name
583 * to Win32 and creates a separate DOS name.
585 * - Call ntfs_create() to create the first link to the
586 * file in the POSIX namespace
588 * - Call ntfs_link() to create the other names of the file, in the
593 if (!dentry->d_inode->i_dos_name_extracted &&
594 !dentry_has_short_name(dentry))
596 inode_for_each_dentry(other, dentry->d_inode) {
597 if (dentry_has_short_name(other)) {
598 orig_dentry = dentry;
604 dentry->d_inode->i_dos_name_extracted = 1;
605 ntfs_inode *dir_ni = dentry_open_parent_ni(dentry, vol);
607 ret = do_apply_dentry_ntfs(dentry, dir_ni, arg);
608 if (ret == 0 && orig_dentry != NULL) {
609 dentry = orig_dentry;
613 ret = WIMLIB_ERR_NTFS_3G;
618 /* Transfers the 100-nanosecond precision timestamps from a WIM dentry to a NTFS
621 apply_dentry_timestamps_ntfs(struct wim_dentry *dentry, void *arg)
623 struct apply_args *args = arg;
624 ntfs_volume *vol = args->vol;
630 DEBUG("Setting timestamps on `%s'", dentry->full_path);
632 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path);
634 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
636 return WIMLIB_ERR_NTFS_3G;
640 p = put_u64(p, dentry->d_inode->i_creation_time);
641 p = put_u64(p, dentry->d_inode->i_last_write_time);
642 p = put_u64(p, dentry->d_inode->i_last_access_time);
643 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
645 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
647 ret = WIMLIB_ERR_NTFS_3G;
650 if (ntfs_inode_close(ni) != 0) {
652 ret = WIMLIB_ERR_NTFS_3G;
653 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
660 libntfs3g_global_init()
662 ntfs_set_char_encoding(setlocale(LC_ALL, ""));