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>
46 static int extract_wim_chunk_to_ntfs_attr(const void *buf, size_t len,
47 u64 offset, void *arg)
50 if (ntfs_attr_pwrite(na, offset, len, buf) == len) {
53 ERROR_WITH_ERRNO("Error extracting WIM resource to NTFS attribute");
54 return WIMLIB_ERR_WRITE;
59 * Extracts a WIM resource to a NTFS attribute.
62 extract_wim_resource_to_ntfs_attr(const struct wim_lookup_table_entry *lte,
65 return extract_wim_resource(lte, wim_resource_size(lte),
66 extract_wim_chunk_to_ntfs_attr, na);
69 /* Writes the data streams of a WIM inode to the data attributes of a NTFS
72 * @ni: The NTFS inode to which the streams are to be extracted.
74 * @dentry: The WIM dentry being extracted. The @d_inode member points to the
75 * corresponding WIM inode that contains the streams being extracted.
76 * The WIM dentry itself is only needed to provide a file path for
77 * better error messages.
79 * @progress_info: Progress information for the image application. The number
80 * of extracted bytes will be incremented by the uncompressed
81 * size of each stream extracted.
83 * Returns 0 on success, nonzero on failure.
85 static int write_ntfs_data_streams(ntfs_inode *ni, const struct wim_dentry *dentry,
86 union wimlib_progress_info *progress_info)
89 unsigned stream_idx = 0;
90 ntfschar *stream_name = AT_UNNAMED;
91 u32 stream_name_nbytes = 0;
92 const struct wim_inode *inode = dentry->d_inode;
93 struct wim_lookup_table_entry *lte;
95 DEBUG("Writing %u NTFS data stream%s for `%s'",
97 (inode->i_num_ads == 0 ? "" : "s"),
102 if (stream_name_nbytes) {
103 /* Skip special UNIX data entries (see documentation for
104 * WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) */
105 if (stream_name_nbytes == WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES
106 && !memcmp(stream_name,
107 WIMLIB_UNIX_DATA_TAG_UTF16LE,
108 WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES))
111 /* Create an empty named stream. */
112 ret = ntfs_attr_add(ni, AT_DATA, stream_name,
113 stream_name_nbytes / 2, NULL, 0);
115 ERROR_WITH_ERRNO("Failed to create named data "
116 "stream for extracted file "
119 ret = WIMLIB_ERR_NTFS_3G;
125 /* If there's no lookup table entry, it's an empty stream.
126 * Otherwise, open the attribute and extract the data. */
130 na = ntfs_attr_open(ni, AT_DATA, stream_name,
131 stream_name_nbytes / 2);
133 ERROR_WITH_ERRNO("Failed to open a data stream of "
134 "extracted file `%s'",
136 ret = WIMLIB_ERR_NTFS_3G;
140 /* The WIM lookup table entry provides the stream
141 * length, so the NTFS attribute should be resized to
142 * this length before starting to extract the data. */
143 ret = ntfs_attr_truncate_solid(na, wim_resource_size(lte));
149 /* Actually extract the stream */
150 ret = extract_wim_resource_to_ntfs_attr(lte, na);
152 /* Close the attribute */
157 /* Record the number of bytes of uncompressed data that
158 * have been extracted. */
159 progress_info->extract.completed_bytes += wim_resource_size(lte);
162 if (stream_idx == inode->i_num_ads) /* Has the last stream been extracted? */
165 /* Get the name and lookup table entry for the next stream. */
166 stream_name = inode->i_ads_entries[stream_idx].stream_name;
167 stream_name_nbytes = inode->i_ads_entries[stream_idx].stream_name_nbytes;
168 lte = inode->i_ads_entries[stream_idx].lte;
174 /* Open the NTFS inode that corresponds to the parent of a WIM dentry. Returns
175 * the opened inode, or NULL on failure. */
177 dentry_open_parent_ni(const struct wim_dentry *dentry, ntfs_volume *vol)
180 const mbchar *dir_name;
184 p = dentry->full_path + dentry->full_path_nbytes;
191 dir_name = dentry->full_path;
192 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
194 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
202 * Makes a NTFS hard link.
204 * The hard link is named @from_dentry->file_name and is located under the
205 * directory specified by @dir_ni, and it is made to point to the previously
206 * extracted file located at @inode->i_extracted_file.
208 * Or, in other words, this adds a new name @from_dentry->full_path to an
209 * existing NTFS inode which already has a name @inode->i_extracted_file.
211 * The new name is made in the POSIX namespace (this is the behavior of
214 * Return 0 on success, nonzero on failure. dir_ni is closed either way.
217 apply_ntfs_hardlink(const struct wim_dentry *from_dentry,
218 const struct wim_inode *inode,
226 ret = ntfs_inode_close(dir_ni);
228 ERROR_WITH_ERRNO("Error closing directory");
229 return WIMLIB_ERR_NTFS_3G;
232 DEBUG("Extracting NTFS hard link `%s' => `%s'",
233 from_dentry->full_path, inode->i_extracted_file);
235 to_ni = ntfs_pathname_to_inode(vol, NULL, inode->i_extracted_file);
237 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
238 inode->i_extracted_file);
239 return WIMLIB_ERR_NTFS_3G;
242 dir_ni = dentry_open_parent_ni(from_dentry, vol);
244 ntfs_inode_close(to_ni);
245 return WIMLIB_ERR_NTFS_3G;
248 ret = ntfs_link(to_ni, dir_ni,
249 from_dentry->file_name,
250 from_dentry->file_name_nbytes / 2);
251 ret |= ntfs_inode_close(dir_ni);
252 ret |= ntfs_inode_close(to_ni);
254 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
255 from_dentry->full_path,
256 inode->i_extracted_file);
257 ret = WIMLIB_ERR_NTFS_3G;
262 /* Transfers file attributes and possibly a security descriptor from a WIM inode
265 * @ni: The NTFS inode to apply the metadata to.
266 * @dir_ni: The NTFS inode for a directory containing @ni.
267 * @dentry: The WIM dentry whose inode contains the metadata to apply.
268 * @w: The WIMStruct for the WIM, through which the table of security
269 * descriptors can be accessed.
271 * Returns 0 on success, nonzero on failure.
274 apply_file_attributes_and_security_data(ntfs_inode *ni,
276 const struct wim_dentry *dentry,
281 struct SECURITY_CONTEXT ctx;
283 const struct wim_inode *inode;
285 inode = dentry->d_inode;
287 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
288 dentry->full_path, inode->i_attributes);
290 attributes_le32 = cpu_to_le32(inode->i_attributes);
291 memset(&ctx, 0, sizeof(ctx));
293 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
295 (const char*)&attributes_le32,
298 ERROR("Failed to set NTFS file attributes on `%s'",
300 return WIMLIB_ERR_NTFS_3G;
302 if (inode->i_security_id != -1 &&
303 !(extract_flags & WIMLIB_EXTRACT_FLAG_NOACLS))
306 const struct wim_security_data *sd;
308 sd = wim_const_security_data(w);
309 wimlib_assert(inode->i_security_id < sd->num_entries);
310 desc = (const char *)sd->descriptors[inode->i_security_id];
311 DEBUG("Applying security descriptor %d to `%s'",
312 inode->i_security_id, dentry->full_path);
314 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
316 sd->sizes[inode->i_security_id], 0);
319 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
321 return WIMLIB_ERR_NTFS_3G;
328 * Transfers the reparse data from a WIM inode (which must represent a reparse
329 * point) to a NTFS inode.
331 static int apply_reparse_data(ntfs_inode *ni, const struct wim_dentry *dentry,
332 union wimlib_progress_info *progress_info)
334 struct wim_lookup_table_entry *lte;
337 lte = inode_unnamed_lte_resolved(dentry->d_inode);
339 DEBUG("Applying reparse data to `%s'", dentry->full_path);
342 ERROR("Could not find reparse data for `%s'",
344 return WIMLIB_ERR_INVALID_DENTRY;
347 if (wim_resource_size(lte) >= 0xffff) {
348 ERROR("Reparse data of `%s' is too long (%"PRIu64" bytes)",
349 dentry->full_path, wim_resource_size(lte));
350 return WIMLIB_ERR_INVALID_DENTRY;
353 u8 reparse_data_buf[8 + wim_resource_size(lte)];
354 u8 *p = reparse_data_buf;
355 p = put_u32(p, dentry->d_inode->i_reparse_tag); /* ReparseTag */
356 DEBUG("ReparseTag = %#x", dentry->d_inode->i_reparse_tag);
357 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
358 p = put_u16(p, 0); /* Reserved */
360 ret = read_full_wim_resource(lte, p, 0);
364 ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
365 wim_resource_size(lte) + 8, 0);
367 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
369 return WIMLIB_ERR_NTFS_3G;
371 progress_info->extract.completed_bytes += wim_resource_size(lte);
376 * Applies a WIM dentry to a NTFS filesystem.
378 * @dentry: The WIM dentry to apply
379 * @dir_ni: The NTFS inode for the parent directory
381 * @return: 0 on success; nonzero on failure.
384 do_apply_dentry_ntfs(struct wim_dentry *dentry, ntfs_inode *dir_ni,
385 struct apply_args *args)
389 ntfs_inode *ni = NULL;
390 struct wim_inode *inode = dentry->d_inode;
391 dentry->is_extracted = 1;
393 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
397 if (inode->i_nlink > 1) {
398 /* Inode has multiple dentries referencing it. */
399 if (inode->i_extracted_file) {
400 /* Already extracted another dentry in the hard
401 * link group. Make a hard link instead of
402 * extracting the file data. */
403 ret = apply_ntfs_hardlink(dentry, inode, dir_ni);
404 /* dir_ni was closed */
407 /* None of the dentries of this inode have been
408 * extracted yet, so go ahead and extract the
410 FREE(inode->i_extracted_file);
411 inode->i_extracted_file = STRDUP(dentry->full_path);
412 if (!inode->i_extracted_file) {
413 ret = WIMLIB_ERR_NOMEM;
414 goto out_close_dir_ni;
420 /* Create a NTFS directory or file.
422 * Note: For symbolic links that are not directory junctions, S_IFREG is
423 * passed here, since the reparse data and file attributes are set
425 ni = ntfs_create(dir_ni, 0, dentry->file_name,
426 dentry->file_name_nbytes / 2, type);
429 ERROR_WITH_ERRNO("Could not create NTFS inode for `%s'",
431 ret = WIMLIB_ERR_NTFS_3G;
432 goto out_close_dir_ni;
435 /* Write the data streams, unless this is a directory or reparse point
437 if (!(inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
438 FILE_ATTRIBUTE_DIRECTORY))) {
439 ret = write_ntfs_data_streams(ni, dentry, &args->progress);
441 goto out_close_dir_ni;
444 ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry,
446 args->extract_flags);
448 goto out_close_dir_ni;
450 if (inode->i_attributes & FILE_ATTR_REPARSE_POINT) {
451 ret = apply_reparse_data(ni, dentry, &args->progress);
453 goto out_close_dir_ni;
456 /* Set DOS (short) name if given */
457 if (dentry_has_short_name(dentry)) {
458 mbchar *short_name_mbs;
459 size_t short_name_mbs_nbytes;
460 ret = utf16le_to_mbs(dentry->short_name,
461 dentry->short_name_nbytes,
463 &short_name_mbs_nbytes);
465 goto out_close_dir_ni;
467 DEBUG("Setting short (DOS) name of `%s' to %s",
468 dentry->full_path, short_name_mbs);
470 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_mbs,
471 short_name_mbs_nbytes, 0);
472 FREE(short_name_mbs);
474 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
476 ret = WIMLIB_ERR_NTFS_3G;
478 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
484 if (ntfs_inode_close_in_dir(ni, dir_ni)) {
486 ret = WIMLIB_ERR_NTFS_3G;
487 ERROR_WITH_ERRNO("Failed to close inode for `%s'",
491 if (ntfs_inode_close(dir_ni)) {
493 ret = WIMLIB_ERR_NTFS_3G;
494 ERROR_WITH_ERRNO("Failed to close inode of directory "
495 "containing `%s'", dentry->full_path);
503 apply_root_dentry_ntfs(const struct wim_dentry *dentry,
504 ntfs_volume *vol, const WIMStruct *w,
510 ni = ntfs_pathname_to_inode(vol, NULL, "/");
512 ERROR_WITH_ERRNO("Could not find root NTFS inode");
513 return WIMLIB_ERR_NTFS_3G;
515 ret = apply_file_attributes_and_security_data(ni, ni, dentry, w,
517 if (ntfs_inode_close(ni) != 0) {
518 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
520 ret = WIMLIB_ERR_NTFS_3G;
525 /* Applies a WIM dentry to the NTFS volume */
527 apply_dentry_ntfs(struct wim_dentry *dentry, void *arg)
529 struct apply_args *args = arg;
530 ntfs_volume *vol = args->vol;
531 WIMStruct *w = args->w;
532 struct wim_dentry *orig_dentry;
533 struct wim_dentry *other;
536 /* Treat the root dentry specially. */
537 if (dentry_is_root(dentry))
538 return apply_root_dentry_ntfs(dentry, vol, w,
539 args->extract_flags);
541 /* NTFS filename namespaces need careful consideration. A name for a
542 * NTFS file may be in either the POSIX, Win32, DOS, or Win32+DOS
543 * namespaces. A NTFS file (a.k.a. inode) may have multiple names in
544 * multiple directories (i.e. hard links); however, a NTFS file can have
545 * at most 1 DOS name total. Furthermore, a Win32 name is always
546 * associated with a DOS name (either as a Win32+DOS name, or a Win32
547 * name and a DOS name separately), which implies that a NTFS file can
548 * have at most 1 Win32 name.
550 * A WIM dentry just contains a "long name", which wimlib makes sure is
551 * non-empty, and a "short name", which may be empty. So, wimlib must
552 * map these to the correct NTFS names. wimlib collects all WIM
553 * dentries that map to the same NTFS inode and factors out the common
554 * information into a 'struct wim_inode', so this should make the
555 * mapping a little more obvious. As a NTFS file can have at most 1 DOS
556 * name, a WIM inode cannot have more than 1 dentry with a non-empty
557 * short name, and this is checked in the verify_inode() function in
558 * verify.c. Furthermore, a WIM dentry, if any, that has a DOS name
559 * must have a long name that corresponds to a Win32 name or Win32+DOS
562 * WIM dentries that have a long name but no associated short name are
563 * assumed to be in the POSIX namespace.
565 * So, given a WIM inode that is to map to a NTFS inode, we must apply
566 * the Win32 and DOS or Win32+DOS names, if they exist, then any
567 * additional (POSIX) names. A caveat when actually doing this: as
568 * confirmed by the libntfs-3g authors, ntfs_set_ntfs_dos_name() is only
569 * guaranteed to associate a DOS name with the appropriate long name if
570 * it's called when that long name is the only one in existence for that
571 * file. So, this implies that the correct ordering of function calls
572 * to extract a NTFS file are:
574 if (file has a DOS name) {
575 * - Call ntfs_create() to create long name associated with
576 * the DOS name (this initially creates a POSIX name)
577 * - Call ntfs_set_ntfs_dos_name() to associate a DOS name
578 * with the long name just created. This either changes
579 * the POSIX name to Win32+DOS, or changes the POSIX name
580 * to Win32 and creates a separate DOS name.
582 * - Call ntfs_create() to create the first link to the
583 * file in the POSIX namespace
585 * - Call ntfs_link() to create the other names of the file, in the
590 if (!dentry->d_inode->i_dos_name_extracted &&
591 !dentry_has_short_name(dentry))
593 inode_for_each_dentry(other, dentry->d_inode) {
594 if (dentry_has_short_name(other)) {
595 orig_dentry = dentry;
601 dentry->d_inode->i_dos_name_extracted = 1;
602 ntfs_inode *dir_ni = dentry_open_parent_ni(dentry, vol);
604 ret = do_apply_dentry_ntfs(dentry, dir_ni, arg);
605 if (ret == 0 && orig_dentry != NULL) {
606 dentry = orig_dentry;
610 ret = WIMLIB_ERR_NTFS_3G;
615 /* Transfers the 100-nanosecond precision timestamps from a WIM dentry to a NTFS
618 apply_dentry_timestamps_ntfs(struct wim_dentry *dentry, void *arg)
620 struct apply_args *args = arg;
621 ntfs_volume *vol = args->vol;
627 DEBUG("Setting timestamps on `%s'", dentry->full_path);
629 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path);
631 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
633 return WIMLIB_ERR_NTFS_3G;
637 p = put_u64(p, dentry->d_inode->i_creation_time);
638 p = put_u64(p, dentry->d_inode->i_last_write_time);
639 p = put_u64(p, dentry->d_inode->i_last_access_time);
640 ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
642 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
644 ret = WIMLIB_ERR_NTFS_3G;
647 if (ntfs_inode_close(ni) != 0) {
649 ret = WIMLIB_ERR_NTFS_3G;
650 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
657 libntfs3g_global_init()
659 ntfs_set_char_encoding(setlocale(LC_ALL, ""));