4 * Capture a WIM image directly from an NTFS volume using libntfs-3g. We capture
5 * everything we can, including security data and alternate data streams.
9 * Copyright (C) 2012, 2013, 2014 Eric Biggers
11 * This file is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
38 #include <ntfs-3g/attrib.h>
39 #include <ntfs-3g/reparse.h>
40 #include <ntfs-3g/security.h>
41 #include <ntfs-3g/volume.h>
43 #include "wimlib/assert.h"
44 #include "wimlib/capture.h"
45 #include "wimlib/dentry.h"
46 #include "wimlib/encoding.h"
47 #include "wimlib/endianness.h"
48 #include "wimlib/error.h"
49 #include "wimlib/lookup_table.h"
50 #include "wimlib/ntfs_3g.h"
51 #include "wimlib/paths.h"
52 #include "wimlib/security.h"
54 static inline ntfschar *
55 attr_record_name(ATTR_RECORD *ar)
57 return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
61 open_ntfs_attr(ntfs_inode *ni, struct ntfs_location *loc)
65 na = ntfs_attr_open(ni,
66 loc->is_reparse_point ? AT_REPARSE_POINT : AT_DATA,
68 loc->stream_name_nchars);
70 ERROR_WITH_ERRNO("Failed to open attribute of \"%"TS"\" in "
71 "NTFS volume", loc->path);
77 read_ntfs_file_prefix(const struct wim_lookup_table_entry *lte, u64 size,
78 consume_data_callback_t cb, void *cb_ctx)
80 struct ntfs_location *loc = lte->ntfs_loc;
81 ntfs_volume *vol = loc->ntfs_vol;
89 ni = ntfs_pathname_to_inode(vol, NULL, loc->path);
91 ERROR_WITH_ERRNO("Can't find NTFS inode for \"%"TS"\"", loc->path);
92 ret = WIMLIB_ERR_NTFS_3G;
96 na = open_ntfs_attr(ni, loc);
98 ret = WIMLIB_ERR_NTFS_3G;
99 goto out_close_ntfs_inode;
102 pos = (loc->is_reparse_point) ? 8 : 0;
103 bytes_remaining = size;
104 while (bytes_remaining) {
105 s64 to_read = min(bytes_remaining, sizeof(buf));
106 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
107 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
108 ret = WIMLIB_ERR_NTFS_3G;
109 goto out_close_ntfs_attr;
112 bytes_remaining -= to_read;
113 ret = cb(buf, to_read, cb_ctx);
115 goto out_close_ntfs_attr;
120 out_close_ntfs_inode:
121 ntfs_inode_close(ni);
127 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
128 u32 *reparse_tag_ret)
134 na = open_ntfs_attr(ni, loc);
136 ret = WIMLIB_ERR_NTFS_3G;
140 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
141 &reparse_tag) != sizeof(reparse_tag))
143 ERROR_WITH_ERRNO("Error reading reparse data");
144 ret = WIMLIB_ERR_NTFS_3G;
145 goto out_close_ntfs_attr;
147 *reparse_tag_ret = le32_to_cpu(reparse_tag);
148 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
157 /* Load the streams from a file or reparse point in the NTFS volume */
159 capture_ntfs_streams(struct wim_inode *inode,
163 struct list_head *unhashed_streams,
167 ntfs_attr_search_ctx *actx;
168 struct ntfs_location *ntfs_loc;
170 struct wim_lookup_table_entry *lte;
172 DEBUG("Capturing NTFS data streams from `%s'", path);
174 /* Get context to search the streams of the NTFS file. */
175 actx = ntfs_attr_get_search_ctx(ni, NULL);
177 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
178 "context for \"%s\"", path);
179 return WIMLIB_ERR_NTFS_3G;
182 /* Capture each data stream or reparse data stream. */
183 while (!ntfs_attr_lookup(type, NULL, 0,
184 CASE_SENSITIVE, 0, NULL, 0, actx))
186 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
187 u64 name_length = actx->attr->name_length;
190 if (data_size == 0) {
191 /* Empty stream. No lookup table entry is needed. */
195 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
197 ret = WIMLIB_ERR_NOMEM;
200 ntfs_loc->ntfs_vol = vol;
201 ntfs_loc->path = memdup(path, path_len + 1);
202 if (!ntfs_loc->path) {
203 ret = WIMLIB_ERR_NOMEM;
204 goto out_free_ntfs_loc;
207 ntfs_loc->stream_name = memdup(attr_record_name(actx->attr),
209 if (!ntfs_loc->stream_name) {
210 ret = WIMLIB_ERR_NOMEM;
211 goto out_free_ntfs_loc;
213 ntfs_loc->stream_name_nchars = name_length;
216 lte = new_lookup_table_entry();
218 ret = WIMLIB_ERR_NOMEM;
219 goto out_free_ntfs_loc;
221 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
222 lte->ntfs_loc = ntfs_loc;
224 if (type == AT_REPARSE_POINT) {
226 ERROR("Invalid reparse data on \"%s\" "
227 "(only %u bytes)!", path, (unsigned)data_size);
228 ret = WIMLIB_ERR_NTFS_3G;
231 lte->ntfs_loc->is_reparse_point = true;
232 lte->size = data_size - 8;
233 ret = read_reparse_tag(ni, lte->ntfs_loc,
234 &inode->i_reparse_tag);
238 lte->ntfs_loc->is_reparse_point = false;
239 lte->size = data_size;
242 if (name_length == 0) {
243 /* Unnamed data stream. Put the reference to it in the
247 if (!(inode->i_attributes &
248 FILE_ATTRIBUTE_REPARSE_POINT))
250 WARNING("Found two un-named "
251 "data streams for \"%s\" "
252 "(sizes = %"PRIu64", "
258 free_lookup_table_entry(lte);
266 /* Named data stream. Put the reference to it in the
267 * alternate data stream entries */
268 struct wim_ads_entry *new_ads_entry;
270 new_ads_entry = inode_add_ads_utf16le(inode,
271 attr_record_name(actx->attr),
273 if (!new_ads_entry) {
274 ret = WIMLIB_ERR_NOMEM;
277 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
278 stream_id = new_ads_entry->stream_id;
279 new_ads_entry->lte = lte;
282 add_unhashed_stream(lte, inode,
283 stream_id, unhashed_streams);
286 if (errno == ENOENT) {
289 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
290 ret = WIMLIB_ERR_NTFS_3G;
294 free_lookup_table_entry(lte);
297 FREE(ntfs_loc->path);
298 FREE(ntfs_loc->stream_name);
302 ntfs_attr_put_search_ctx(actx);
304 DEBUG("Successfully captured NTFS streams from \"%s\"", path);
306 ERROR("Failed to capture NTFS streams from \"%s\"", path);
310 /* Binary tree that maps NTFS inode numbers to DOS names */
311 struct dos_name_map {
312 struct avl_tree_node *root;
315 struct dos_name_node {
316 struct avl_tree_node index_node;
322 #define DOS_NAME_NODE(avl_node) \
323 avl_tree_entry(avl_node, struct dos_name_node, index_node)
326 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
327 const struct avl_tree_node *n2)
329 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
330 DOS_NAME_NODE(n2)->ntfs_ino);
333 /* Inserts a new DOS name into the map */
335 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
336 size_t name_nbytes, le64 ntfs_ino)
338 struct dos_name_node *new_node;
340 DEBUG("DOS name_len = %zu", name_nbytes);
341 new_node = MALLOC(sizeof(struct dos_name_node));
343 return WIMLIB_ERR_NOMEM;
345 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
346 * assuming 2-byte ntfs characters) */
347 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
349 /* Initialize the DOS name, DOS name length, and NTFS inode number of
350 * the search tree node */
351 memcpy(new_node->dos_name, dos_name, name_nbytes);
352 new_node->name_nbytes = name_nbytes;
353 new_node->ntfs_ino = ntfs_ino;
355 /* Insert the search tree node */
356 if (avl_tree_insert(&map->root, &new_node->index_node,
357 _avl_cmp_by_ntfs_ino))
359 /* This should be impossible since an NTFS inode cannot
360 * have multiple DOS names, and we only should get each
361 * DOS name entry once from the ntfs_readdir() calls. */
362 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
363 le64_to_cpu(ntfs_ino));
365 return WIMLIB_ERR_NOMEM;
367 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
371 /* Returns a structure that contains the DOS name and its length for an NTFS
372 * inode, or NULL if the inode has no DOS name. */
373 static struct dos_name_node *
374 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
376 struct dos_name_node dummy;
377 struct avl_tree_node *res;
379 dummy.ntfs_ino = cpu_to_le64(ntfs_ino);
381 res = avl_tree_lookup_node(map->root, &dummy.index_node,
382 _avl_cmp_by_ntfs_ino);
385 return DOS_NAME_NODE(res);
389 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
391 const struct dos_name_node *node;
393 if (dentry->is_win32_name) {
394 node = lookup_dos_name(map, dentry->d_inode->i_ino);
396 dentry->short_name = utf16le_dupz((const utf16lechar *)node->dos_name,
398 if (!dentry->short_name)
399 return WIMLIB_ERR_NOMEM;
400 dentry->short_name_nbytes = node->name_nbytes;
401 DEBUG("Assigned DOS name to ino %"PRIu64,
402 dentry->d_inode->i_ino);
404 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
405 "corresponding DOS name",
406 dentry->d_inode->i_ino);
413 free_dos_name_tree(struct avl_tree_node *node) {
415 free_dos_name_tree(node->left);
416 free_dos_name_tree(node->right);
417 FREE(DOS_NAME_NODE(node));
422 destroy_dos_name_map(struct dos_name_map *map)
424 free_dos_name_tree(map->root);
428 struct wim_dentry *parent;
431 struct dos_name_map *dos_name_map;
433 struct capture_params *params;
438 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
443 ntfs_volume *ntfs_vol,
444 struct capture_params *params);
447 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
448 const int name_nchars, const int name_type,
449 const s64 pos, const MFT_REF mref,
450 const unsigned dt_type)
452 struct readdir_ctx *ctx;
453 size_t mbs_name_nbytes;
455 struct wim_dentry *child;
458 size_t name_nbytes = name_nchars * sizeof(ntfschar);
461 if (name_type & FILE_NAME_DOS) {
462 /* If this is the entry for a DOS name, store it for later. */
463 ret = insert_dos_name(ctx->dos_name_map, name,
464 name_nbytes, mref & MFT_REF_MASK_CPU);
466 /* Return now if an error occurred or if this is just a DOS name
467 * and not a Win32+DOS name. */
468 if (ret != 0 || name_type == FILE_NAME_DOS)
471 ret = utf16le_to_tstr(name, name_nbytes,
472 &mbs_name, &mbs_name_nbytes);
476 if (mbs_name[0] == '.' &&
477 (mbs_name[1] == '\0' ||
478 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
481 * note: name_type is POSIX for these, so DOS names will not
482 * have been inserted for them. */
484 goto out_free_mbs_name;
487 /* Open the inode for this directory entry and recursively capture the
488 * directory tree rooted at it */
489 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
491 /* XXX This used to be treated as an error, but NTFS-3g seemed
492 * to be unable to read some inodes on a Windows 8 image for
494 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
495 ctx->path, mbs_name);
497 goto out_free_mbs_name;
499 path_len = ctx->path_len;
501 ctx->path[path_len++] = '/';
502 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
503 path_len += mbs_name_nbytes;
505 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
507 ctx->vol, ctx->params);
508 path_len -= mbs_name_nbytes + 1;
510 dentry_add_child(ctx->parent, child);
511 ntfs_inode_close(ni);
515 ctx->path[ctx->path_len] = '\0';
520 /* Recursively build a WIM dentry tree corresponding to an NTFS volume.
521 * At the same time, update the WIM lookup table with lookup table entries for
522 * the NTFS streams, and build an array of security descriptors.
525 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
531 struct capture_params *params)
535 struct wim_dentry *root = NULL;
536 struct wim_inode *inode = NULL;
538 ret = try_exclude(path, path_len, params);
539 if (ret < 0) /* Excluded? */
541 if (ret > 0) /* Error? */
544 /* Get file attributes */
545 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
546 if (ret != sizeof(attributes)) {
547 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
548 ret = WIMLIB_ERR_NTFS_3G;
552 if ((attributes & (FILE_ATTRIBUTE_DIRECTORY |
553 FILE_ATTRIBUTE_ENCRYPTED)) == FILE_ATTRIBUTE_ENCRYPTED)
555 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
557 ERROR("Can't archive unsupported encrypted file \"%s\"", path);
558 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
561 params->progress.scan.cur_path = path;
562 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
566 /* Create a WIM dentry with an associated inode, which may be shared */
567 ret = inode_table_new_dentry(params->inode_table,
568 path_basename_with_len(path, path_len),
569 ni->mft_no, 0, false, &root);
573 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
574 root->is_win32_name = 1;
576 inode = root->d_inode;
578 if (inode->i_nlink > 1) {
579 /* Shared inode; nothing more to do */
583 inode->i_creation_time = le64_to_cpu(ni->creation_time);
584 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
585 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
586 inode->i_attributes = attributes;
587 inode->i_resolved = 1;
589 /* Capture streams. */
591 if (attributes & FILE_ATTR_REPARSE_POINT) {
592 /* Capture reparse data stream. */
593 ret = capture_ntfs_streams(inode, ni, path, path_len,
594 params->unhashed_streams,
595 vol, AT_REPARSE_POINT);
600 /* Capture data streams.
602 * Directories should not have an unnamed data stream, but they may have
603 * named data streams.
605 * Reparse points may have an unnamed data stream (which will be ignored
606 * in favor of the reparse data stream), and they also may have named
609 * Regular files can have an unnamed data stream as well as named data
611 ret = capture_ntfs_streams(inode, ni, path, path_len,
612 params->unhashed_streams, vol, AT_DATA);
616 if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
618 /* Recurse to directory children */
620 struct dos_name_map dos_name_map = { .root = NULL };
621 struct readdir_ctx ctx = {
624 .path_len = path_len,
625 .dos_name_map = &dos_name_map,
630 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
636 /* error from ntfs_readdir() itself */
637 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
638 ret = WIMLIB_ERR_NTFS_3G;
641 struct wim_dentry *child;
644 for_dentry_child(child, root) {
645 ret = set_dentry_dos_name(child, &dos_name_map);
650 destroy_dos_name_map(&dos_name_map);
654 path[path_len] = '\0';
656 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
657 * only allow capturing an entire volume. */
658 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
659 inode_is_symlink(inode))
660 inode->i_not_rpfixed = 0;
662 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
663 struct SECURITY_CONTEXT sec_ctx;
667 /* Get security descriptor */
668 memset(&sec_ctx, 0, sizeof(sec_ctx));
673 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
674 if (ret > sizeof(_sd)) {
676 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
679 inode->i_security_id = sd_set_add_sd(params->sd_set,
681 if (inode->i_security_id == -1) {
682 ERROR("Out of memory");
683 ret = WIMLIB_ERR_NOMEM;
686 DEBUG("Added security ID = %u for `%s'",
687 inode->i_security_id, path);
689 } else if (ret < 0) {
690 ERROR_WITH_ERRNO("Failed to get security information from "
692 ret = WIMLIB_ERR_NTFS_3G;
694 inode->i_security_id = -1;
695 DEBUG("No security ID for `%s'", path);
702 params->progress.scan.cur_path = path;
704 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
706 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
709 free_dentry_tree(root, params->lookup_table);
711 ret = report_capture_error(params, ret, path);
719 do_ntfs_umount(struct _ntfs_volume *vol)
721 DEBUG("Unmounting NTFS volume");
722 if (ntfs_umount(vol, FALSE))
723 return WIMLIB_ERR_NTFS_3G;
729 build_dentry_tree_ntfs(struct wim_dentry **root_p,
731 struct capture_params *params)
737 DEBUG("Mounting NTFS volume `%s' read-only", device);
739 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
742 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
743 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
744 * seem to contain any explicit version information. So we have to rely on a
745 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
747 #ifdef HAVE_NTFS_MNT_RDONLY
749 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
750 #elif defined(MS_RDONLY)
751 /* NTFS-3g 2011, 2012 */
752 vol = ntfs_mount(device, MS_RDONLY);
754 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
757 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
759 return WIMLIB_ERR_NTFS_3G;
761 ntfs_open_secure(vol);
763 /* We don't want to capture the special NTFS files such as $Bitmap. Not
764 * to be confused with "hidden" or "system" files which are real files
765 * that we do need to capture. */
766 NVolClearShowSysFiles(vol);
768 DEBUG("Opening root NTFS dentry");
769 root_ni = ntfs_inode_open(vol, FILE_root);
771 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
773 ret = WIMLIB_ERR_NTFS_3G;
777 /* Currently we assume that all the paths fit into this length and there
778 * is no check for overflow. */
779 char *path = MALLOC(32768);
781 ERROR("Could not allocate memory for NTFS pathname");
782 ret = WIMLIB_ERR_NOMEM;
788 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
789 FILE_NAME_POSIX, vol, params);
792 ntfs_inode_close(root_ni);
794 ntfs_index_ctx_put(vol->secure_xsii);
795 ntfs_index_ctx_put(vol->secure_xsdh);
796 ntfs_inode_close(vol->secure_ni);
799 if (do_ntfs_umount(vol)) {
800 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
804 /* We need to leave the NTFS volume mounted so that we can read
805 * the NTFS files again when we are actually writing the WIM */
806 *(ntfs_volume**)params->extra_arg = vol;
810 #endif /* WITH_NTFS_3G */