4 * Apply a WIM image directly to a NTFS volume using libntfs-3g. Restore as
5 * much information as possible, including security data, file attributes, DOS
6 * names, and alternate data streams.
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/.
36 #include <time.h> /* NTFS-3g headers are missing <time.h> include */
38 #include <ntfs-3g/attrib.h>
39 #include <ntfs-3g/endians.h>
40 #include <ntfs-3g/reparse.h>
41 #include <ntfs-3g/security.h>
42 #include <ntfs-3g/types.h>
43 #include <ntfs-3g/xattrs.h>
45 #include "wimlib/apply.h"
46 #include "wimlib/compiler.h"
47 #include "wimlib/dentry.h"
48 #include "wimlib/encoding.h"
49 #include "wimlib/error.h"
50 #include "wimlib/lookup_table.h"
51 #include "wimlib/metadata.h"
52 #include "wimlib/ntfs_3g.h"
53 #include "wimlib/reparse.h"
54 #include "wimlib/security.h"
56 struct ntfs_attr_extract_ctx {
62 extract_wim_chunk_to_ntfs_attr(const void *buf, size_t len, void *_ctx)
64 struct ntfs_attr_extract_ctx *ctx = _ctx;
65 if (ntfs_attr_pwrite(ctx->na, ctx->offset, len, buf) == len) {
69 ERROR_WITH_ERRNO("Error extracting WIM resource to NTFS attribute");
70 return WIMLIB_ERR_WRITE;
75 * Extracts a WIM resource to a NTFS attribute.
78 extract_wim_resource_to_ntfs_attr(const struct wim_lookup_table_entry *lte,
81 struct ntfs_attr_extract_ctx ctx;
84 return extract_wim_resource(lte, wim_resource_size(lte),
85 extract_wim_chunk_to_ntfs_attr, &ctx);
88 /* Writes the data streams of a WIM inode to the data attributes of a NTFS
91 * @ni: The NTFS inode to which the streams are to be extracted.
93 * @dentry: The WIM dentry being extracted. The @d_inode member points to the
94 * corresponding WIM inode that contains the streams being extracted.
95 * The WIM dentry itself is only needed to provide a file path for
96 * better error messages.
98 * @progress_info: Progress information for the image application. The number
99 * of extracted bytes will be incremented by the uncompressed
100 * size of each stream extracted.
102 * Returns 0 on success, nonzero on failure.
105 write_ntfs_data_streams(ntfs_inode *ni, struct wim_dentry *dentry,
106 union wimlib_progress_info *progress_info)
109 unsigned stream_idx = 0;
110 ntfschar *stream_name = AT_UNNAMED;
111 u32 stream_name_nbytes = 0;
112 const struct wim_inode *inode = dentry->d_inode;
113 struct wim_lookup_table_entry *lte;
117 /* For directories, skip unnamed streams; just extract alternate data
119 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
122 DEBUG("Writing %u NTFS data stream%s for `%s'",
123 inode->i_num_ads + 1,
124 (inode->i_num_ads == 0 ? "" : "s"),
128 if (stream_name_nbytes) {
129 /* Skip special UNIX data entries (see documentation for
130 * WIMLIB_ADD_FLAG_UNIX_DATA) */
131 if (stream_name_nbytes == WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES
132 && !memcmp(stream_name,
133 WIMLIB_UNIX_DATA_TAG_UTF16LE,
134 WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES))
137 /* Create an empty named stream. */
138 ret = ntfs_attr_add(ni, AT_DATA, stream_name,
139 stream_name_nbytes / 2, NULL, 0);
141 ERROR_WITH_ERRNO("Failed to create named data "
142 "stream for extracted file "
145 ret = WIMLIB_ERR_NTFS_3G;
151 /* If there's no lookup table entry, it's an empty stream.
152 * Otherwise, open the attribute and extract the data. */
156 na = ntfs_attr_open(ni, AT_DATA, stream_name,
157 stream_name_nbytes / 2);
159 ERROR_WITH_ERRNO("Failed to open a data stream of "
160 "extracted file `%s'",
162 ret = WIMLIB_ERR_NTFS_3G;
166 /* The WIM lookup table entry provides the stream
167 * length, so the NTFS attribute should be resized to
168 * this length before starting to extract the data. */
169 ret = ntfs_attr_truncate_solid(na, wim_resource_size(lte));
175 /* Actually extract the stream */
176 ret = extract_wim_resource_to_ntfs_attr(lte, na);
178 /* Close the attribute */
183 /* Record the number of bytes of uncompressed data that
184 * have been extracted. */
185 progress_info->extract.completed_bytes += wim_resource_size(lte);
188 if (stream_idx == inode->i_num_ads) /* Has the last stream been extracted? */
191 /* Get the name and lookup table entry for the next stream. */
192 stream_name = inode->i_ads_entries[stream_idx].stream_name;
193 stream_name_nbytes = inode->i_ads_entries[stream_idx].stream_name_nbytes;
194 lte = inode->i_ads_entries[stream_idx].lte;
200 /* Open the NTFS inode that corresponds to the parent of a WIM dentry. Returns
201 * the opened inode, or NULL on failure. */
203 dentry_open_parent_ni(struct wim_dentry *dentry, ntfs_volume *vol)
206 const char *dir_name;
210 p = dentry->_full_path + dentry->full_path_nbytes;
217 dir_name = dentry->_full_path;
218 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
220 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
228 * Makes a NTFS hard link.
230 * The hard link is named @from_dentry->file_name and is located under the
231 * directory specified by @dir_ni, and it is made to point to the previously
232 * extracted file located at @inode->i_extracted_file.
234 * Or, in other words, this adds a new name @from_dentry->full_path to an
235 * existing NTFS inode which already has a name @inode->i_extracted_file.
237 * The new name is made in the POSIX namespace (this is the behavior of
240 * Return 0 on success, nonzero on failure. dir_ni is closed either way.
243 apply_ntfs_hardlink(struct wim_dentry *from_dentry,
244 const struct wim_inode *inode,
252 ret = ntfs_inode_close(dir_ni);
254 ERROR_WITH_ERRNO("Error closing directory");
255 return WIMLIB_ERR_NTFS_3G;
258 DEBUG("Extracting NTFS hard link `%s' => `%s'",
259 from_dentry->_full_path, inode->i_extracted_file);
261 to_ni = ntfs_pathname_to_inode(vol, NULL, inode->i_extracted_file);
263 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
264 inode->i_extracted_file);
265 return WIMLIB_ERR_NTFS_3G;
268 dir_ni = dentry_open_parent_ni(from_dentry, vol);
270 ntfs_inode_close(to_ni);
271 return WIMLIB_ERR_NTFS_3G;
274 ret = ntfs_link(to_ni, dir_ni,
275 from_dentry->file_name,
276 from_dentry->file_name_nbytes / 2);
277 ret |= ntfs_inode_close(dir_ni);
278 ret |= ntfs_inode_close(to_ni);
280 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
281 from_dentry->_full_path,
282 inode->i_extracted_file);
283 ret = WIMLIB_ERR_NTFS_3G;
288 /* Transfers file attributes and possibly a security descriptor from a WIM inode
291 * @ni: The NTFS inode to apply the metadata to.
292 * @dir_ni: The NTFS inode for a directory containing @ni.
293 * @dentry: The WIM dentry whose inode contains the metadata to apply.
294 * @wim: The WIMStruct for the WIM, through which the table of security
295 * descriptors can be accessed.
297 * Returns 0 on success, nonzero on failure.
300 apply_file_attributes_and_security_data(ntfs_inode *ni,
302 struct wim_dentry *dentry,
303 const WIMStruct *wim,
307 struct SECURITY_CONTEXT ctx;
309 const struct wim_inode *inode;
311 inode = dentry->d_inode;
313 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
314 dentry->_full_path, inode->i_attributes);
316 attributes = cpu_to_le32(inode->i_attributes);
317 memset(&ctx, 0, sizeof(ctx));
319 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
322 sizeof(attributes), 0);
324 ERROR("Failed to set NTFS file attributes on `%s'",
326 ret = WIMLIB_ERR_NTFS_3G;
327 } else if (inode->i_security_id != -1 &&
328 !(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
331 const struct wim_security_data *sd;
333 sd = wim_const_security_data(wim);
334 wimlib_assert(inode->i_security_id < sd->num_entries);
335 desc = (const char *)sd->descriptors[inode->i_security_id];
336 DEBUG("Applying security descriptor %d to `%s'",
337 inode->i_security_id, dentry->_full_path);
339 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
341 sd->sizes[inode->i_security_id], 0);
344 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
346 ret = WIMLIB_ERR_NTFS_3G;
353 * Transfers the reparse data from a WIM inode (which must represent a reparse
354 * point) to a NTFS inode.
357 apply_reparse_data(ntfs_inode *ni, struct wim_dentry *dentry,
358 union wimlib_progress_info *progress_info)
361 u8 rpbuf[REPARSE_POINT_MAX_SIZE] _aligned_attribute(8);
364 DEBUG("Applying reparse data to `%s'", dentry->_full_path);
366 ret = wim_inode_get_reparse_data(dentry->d_inode, rpbuf, &rpbuflen);
370 ret = ntfs_set_ntfs_reparse_data(ni, rpbuf, rpbuflen, 0);
372 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
374 return WIMLIB_ERR_NTFS_3G;
377 progress_info->extract.completed_bytes += rpbuflen - 8;
382 * Applies a WIM dentry to a NTFS filesystem.
384 * @dentry: The WIM dentry to apply
385 * @dir_ni: The NTFS inode for the parent directory
387 * @return: 0 on success; nonzero on failure.
390 do_apply_dentry_ntfs(struct wim_dentry *dentry, ntfs_inode *dir_ni,
391 struct apply_args *args)
395 ntfs_inode *ni = NULL;
396 struct wim_inode *inode = dentry->d_inode;
397 dentry->needs_extraction = 0;
399 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
403 if (inode->i_nlink > 1) {
404 /* Inode has multiple dentries referencing it. */
405 if (inode->i_extracted_file) {
406 /* Already extracted another dentry in the hard
407 * link group. Make a hard link instead of
408 * extracting the file data. */
409 ret = apply_ntfs_hardlink(dentry, inode, dir_ni);
410 /* dir_ni was closed */
413 /* None of the dentries of this inode have been
414 * extracted yet, so go ahead and extract the
416 FREE(inode->i_extracted_file);
417 if (!(inode->i_extracted_file = STRDUP(dentry->_full_path)))
419 ret = WIMLIB_ERR_NOMEM;
420 goto out_close_dir_ni;
426 /* Create a NTFS directory or file.
428 * Note: For symbolic links that are not directory junctions, S_IFREG is
429 * passed here, since the reparse data and file attributes are set
431 ni = ntfs_create(dir_ni, 0, dentry->file_name,
432 dentry->file_name_nbytes / 2, type);
435 ERROR_WITH_ERRNO("Could not create NTFS inode for `%s'",
437 ret = WIMLIB_ERR_NTFS_3G;
438 goto out_close_dir_ni;
441 /* Write the data streams, unless this is reparse point. */
442 if (!(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
443 ret = write_ntfs_data_streams(ni, dentry, &args->progress);
445 goto out_close_dir_ni;
448 ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry,
450 args->extract_flags);
452 goto out_close_dir_ni;
454 if (inode->i_attributes & FILE_ATTR_REPARSE_POINT) {
455 ret = apply_reparse_data(ni, dentry, &args->progress);
457 goto out_close_dir_ni;
460 /* Set DOS (short) name if given */
461 if (dentry_has_short_name(dentry) && !dentry->dos_name_invalid)
463 char *short_name_mbs;
464 size_t short_name_mbs_nbytes;
465 ret = utf16le_to_tstr(dentry->short_name,
466 dentry->short_name_nbytes,
468 &short_name_mbs_nbytes);
470 goto out_close_dir_ni;
472 DEBUG("Setting short (DOS) name of `%s' to %s",
473 dentry->_full_path, short_name_mbs);
475 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_mbs,
476 short_name_mbs_nbytes, 0);
477 FREE(short_name_mbs);
479 WARNING_WITH_ERRNO("Could not set DOS (short) name for `%s'",
483 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
489 if (ntfs_inode_close_in_dir(ni, dir_ni)) {
491 ret = WIMLIB_ERR_NTFS_3G;
492 ERROR_WITH_ERRNO("Failed to close inode for `%s'",
496 if (ntfs_inode_close(dir_ni)) {
498 ret = WIMLIB_ERR_NTFS_3G;
499 ERROR_WITH_ERRNO("Failed to close inode of directory "
509 apply_root_dentry_ntfs(struct wim_dentry *dentry,
510 ntfs_volume *vol, const WIMStruct *wim,
516 ret = calculate_dentry_full_path(dentry);
520 ni = ntfs_pathname_to_inode(vol, NULL, "/");
522 ERROR_WITH_ERRNO("Could not find root NTFS inode");
523 return WIMLIB_ERR_NTFS_3G;
525 ret = apply_file_attributes_and_security_data(ni, ni, dentry, wim,
527 if (ntfs_inode_close(ni) != 0) {
528 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
530 ret = WIMLIB_ERR_NTFS_3G;
535 /* Applies a WIM dentry to the NTFS volume */
537 apply_dentry_ntfs(struct wim_dentry *dentry, void *arg)
539 struct apply_args *args = arg;
540 ntfs_volume *vol = args->vol;
541 WIMStruct *wim = args->wim;
542 struct wim_dentry *orig_dentry;
543 struct wim_dentry *other;
546 /* Treat the root dentry specially. */
547 if (dentry_is_root(dentry))
548 return apply_root_dentry_ntfs(dentry, vol, wim,
549 args->extract_flags);
551 /* NTFS filename namespaces need careful consideration. A name for a
552 * NTFS file may be in either the POSIX, Win32, DOS, or Win32+DOS
553 * namespaces. A NTFS file (a.k.a. inode) may have multiple names in
554 * multiple directories (i.e. hard links); however, a NTFS file can have
555 * at most 1 DOS name total. Furthermore, a Win32 name is always
556 * associated with a DOS name (either as a Win32+DOS name, or a Win32
557 * name and a DOS name separately), which implies that a NTFS file can
558 * have at most 1 Win32 name.
560 * A WIM dentry just contains a "long name", which wimlib makes sure is
561 * non-empty, and a "short name", which may be empty. So, wimlib must
562 * map these to the correct NTFS names. wimlib collects all WIM
563 * dentries that map to the same NTFS inode and factors out the common
564 * information into a 'struct wim_inode', so this should make the
565 * mapping a little more obvious. As a NTFS file can have at most 1 DOS
566 * name, a WIM inode cannot have more than 1 dentry with a non-empty
567 * short name, and this is checked in the verify_inode() function in
568 * verify.c. Furthermore, a WIM dentry, if any, that has a DOS name
569 * must have a long name that corresponds to a Win32 name or Win32+DOS
572 * WIM dentries that have a long name but no associated short name are
573 * assumed to be in the POSIX namespace.
575 * So, given a WIM inode that is to map to a NTFS inode, we must apply
576 * the Win32 and DOS or Win32+DOS names, if they exist, then any
577 * additional (POSIX) names. A caveat when actually doing this: as
578 * confirmed by the libntfs-3g authors, ntfs_set_ntfs_dos_name() is only
579 * guaranteed to associate a DOS name with the appropriate long name if
580 * it's called when that long name is the only one in existence for that
581 * file. So, this implies that the correct ordering of function calls
582 * to extract a NTFS file are:
584 * if (file has a DOS name) {
585 * - Call ntfs_create() to create long name associated with
586 * the DOS name (this initially creates a POSIX name)
587 * - Call ntfs_set_ntfs_dos_name() to associate a DOS name
588 * with the long name just created. This either changes
589 * the POSIX name to Win32+DOS, or changes the POSIX name
590 * to Win32 and creates a separate DOS name.
592 * - Call ntfs_create() to create the first link to the
593 * file in the POSIX namespace
595 * - Call ntfs_link() to create the other names of the file, in the
600 if (!dentry->d_inode->i_dos_name_extracted &&
601 (!dentry_has_short_name(dentry) || dentry->dos_name_invalid))
603 inode_for_each_dentry(other, dentry->d_inode) {
604 if (dentry_has_short_name(other) && !other->dos_name_invalid) {
605 orig_dentry = dentry;
611 dentry->d_inode->i_dos_name_extracted = 1;
613 ret = calculate_dentry_full_path(dentry);
617 ntfs_inode *dir_ni = dentry_open_parent_ni(dentry, vol);
619 ret = do_apply_dentry_ntfs(dentry, dir_ni, arg);
620 if (ret == 0 && orig_dentry != NULL) {
621 dentry = orig_dentry;
625 ret = WIMLIB_ERR_NTFS_3G;
630 /* Transfers the 100-nanosecond precision timestamps from a WIM dentry to a NTFS
633 apply_dentry_timestamps_ntfs(struct wim_dentry *dentry, void *arg)
635 struct apply_args *args = arg;
636 ntfs_volume *vol = args->vol;
637 u64 ntfs_timestamps[3];
641 DEBUG("Setting timestamps on `%s'", dentry->_full_path);
643 ni = ntfs_pathname_to_inode(vol, NULL, dentry->_full_path);
645 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
647 return WIMLIB_ERR_NTFS_3G;
650 /* Note: ntfs_inode_set_times() expects the times in native byte order,
651 * not little endian. */
652 ntfs_timestamps[0] = dentry->d_inode->i_creation_time;
653 ntfs_timestamps[1] = dentry->d_inode->i_last_write_time;
654 ntfs_timestamps[2] = dentry->d_inode->i_last_access_time;
655 ret = ntfs_inode_set_times(ni, (const char*)ntfs_timestamps,
656 sizeof(ntfs_timestamps), 0);
658 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
660 ret = WIMLIB_ERR_NTFS_3G;
663 if (ntfs_inode_close(ni) != 0) {
665 ret = WIMLIB_ERR_NTFS_3G;
666 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
673 libntfs3g_global_init(void)
675 ntfs_set_char_encoding(setlocale(LC_ALL, ""));
678 #endif /* WITH_NTFS_3G */