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, 2015 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/.
33 #include <ntfs-3g/attrib.h>
34 #include <ntfs-3g/reparse.h>
35 #include <ntfs-3g/security.h>
36 #include <ntfs-3g/volume.h>
38 #include "wimlib/alloca.h"
39 #include "wimlib/assert.h"
40 #include "wimlib/blob_table.h"
41 #include "wimlib/capture.h"
42 #include "wimlib/dentry.h"
43 #include "wimlib/encoding.h"
44 #include "wimlib/endianness.h"
45 #include "wimlib/error.h"
46 #include "wimlib/ntfs_3g.h"
47 #include "wimlib/paths.h"
48 #include "wimlib/reparse.h"
49 #include "wimlib/security.h"
51 static inline const ntfschar *
52 attr_record_name(const ATTR_RECORD *record)
54 return (const ntfschar *)
55 ((const u8 *)record + le16_to_cpu(record->name_offset));
59 open_ntfs_attr(ntfs_inode *ni, const struct ntfs_location *loc)
63 na = ntfs_attr_open(ni,
64 (ATTR_TYPES)loc->attr_type,
66 loc->attr_name_nchars);
68 ERROR_WITH_ERRNO("Failed to open attribute of NTFS inode %"PRIu64,
75 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
76 consume_data_callback_t cb, void *cb_ctx)
78 const struct ntfs_location *loc = blob->ntfs_loc;
79 ntfs_volume *vol = loc->ntfs_vol;
87 ni = ntfs_inode_open(vol, loc->mft_no);
89 ERROR_WITH_ERRNO("Failed to open NTFS inode %"PRIu64,
91 ret = WIMLIB_ERR_NTFS_3G;
95 na = open_ntfs_attr(ni, loc);
97 ret = WIMLIB_ERR_NTFS_3G;
98 goto out_close_ntfs_inode;
101 pos = (loc->attr_type == AT_REPARSE_POINT) ? REPARSE_DATA_OFFSET : 0;
102 bytes_remaining = size;
103 while (bytes_remaining) {
104 s64 to_read = min(bytes_remaining, sizeof(buf));
105 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
106 ERROR_WITH_ERRNO("Error reading data from NTFS inode "
107 "%"PRIu64, loc->mft_no);
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);
158 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
162 return STREAM_TYPE_DATA;
163 case AT_REPARSE_POINT:
164 return STREAM_TYPE_REPARSE_POINT;
167 return STREAM_TYPE_UNKNOWN;
171 /* Save information about an NTFS attribute (stream) to a WIM inode. */
173 scan_ntfs_attr(struct wim_inode *inode,
177 struct list_head *unhashed_blobs,
180 const ATTR_RECORD *record)
182 const u64 data_size = ntfs_get_attribute_value_length(record);
183 const size_t name_nchars = record->name_length;
184 struct blob_descriptor *blob = NULL;
185 utf16lechar *stream_name = NULL;
186 struct wim_inode_stream *strm;
189 if (unlikely(name_nchars)) {
191 stream_name = utf16le_dupz(attr_record_name(record),
192 name_nchars * sizeof(ntfschar));
194 ret = WIMLIB_ERR_NOMEM;
199 /* If the stream is non-empty, set up a blob descriptor for it. */
200 if (data_size != 0) {
201 blob = new_blob_descriptor();
202 if (unlikely(!blob)) {
203 ret = WIMLIB_ERR_NOMEM;
207 blob->ntfs_loc = CALLOC(1, sizeof(struct ntfs_location));
208 if (unlikely(!blob->ntfs_loc)) {
209 ret = WIMLIB_ERR_NOMEM;
213 blob->blob_location = BLOB_IN_NTFS_VOLUME;
214 blob->size = data_size;
215 blob->ntfs_loc->ntfs_vol = vol;
216 blob->ntfs_loc->attr_type = type;
217 blob->ntfs_loc->mft_no = ni->mft_no;
219 if (unlikely(name_nchars)) {
220 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
221 if (!blob->ntfs_loc->attr_name) {
222 ret = WIMLIB_ERR_NOMEM;
225 blob->ntfs_loc->attr_name_nchars = name_nchars;
228 if (unlikely(type == AT_REPARSE_POINT)) {
229 if (blob->size < REPARSE_DATA_OFFSET) {
230 ERROR("Reparse data of \"%s\" "
231 "is invalid (only %"PRIu64" bytes)!",
233 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
236 blob->size -= REPARSE_DATA_OFFSET;
237 ret = read_reparse_tag(ni, blob->ntfs_loc,
238 &inode->i_reparse_tag);
244 strm = inode_add_stream(inode,
245 attr_type_to_wimlib_stream_type(type),
246 stream_name ? stream_name : NO_STREAM_NAME,
248 if (unlikely(!strm)) {
249 ret = WIMLIB_ERR_NOMEM;
252 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
256 free_blob_descriptor(blob);
261 /* Scan attributes of the specified type from a file in the NTFS volume */
263 scan_ntfs_attrs_with_type(struct wim_inode *inode,
267 struct list_head *unhashed_blobs,
271 ntfs_attr_search_ctx *actx;
274 DEBUG("Scanning NTFS attributes from \"%s\"", path);
276 actx = ntfs_attr_get_search_ctx(ni, NULL);
278 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
279 "context for \"%s\"", path);
280 return WIMLIB_ERR_NTFS_3G;
283 while (!ntfs_attr_lookup(type, NULL, 0,
284 CASE_SENSITIVE, 0, NULL, 0, actx))
286 ret = scan_ntfs_attr(inode,
297 if (errno != ENOENT) {
298 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
299 ret = WIMLIB_ERR_NTFS_3G;
304 ntfs_attr_put_search_ctx(actx);
308 /* Binary tree that maps NTFS inode numbers to DOS names */
309 struct dos_name_map {
310 struct avl_tree_node *root;
313 struct dos_name_node {
314 struct avl_tree_node index_node;
320 #define DOS_NAME_NODE(avl_node) \
321 avl_tree_entry(avl_node, struct dos_name_node, index_node)
324 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
325 const struct avl_tree_node *n2)
327 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
328 DOS_NAME_NODE(n2)->ntfs_ino);
331 /* Inserts a new DOS name into the map */
333 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
334 size_t name_nbytes, le64 ntfs_ino)
336 struct dos_name_node *new_node;
338 DEBUG("DOS name_len = %zu", name_nbytes);
339 new_node = MALLOC(sizeof(struct dos_name_node));
341 return WIMLIB_ERR_NOMEM;
343 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
344 * assuming 2-byte ntfs characters) */
345 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
347 /* Initialize the DOS name, DOS name length, and NTFS inode number of
348 * the search tree node */
349 memcpy(new_node->dos_name, dos_name, name_nbytes);
350 new_node->name_nbytes = name_nbytes;
351 new_node->ntfs_ino = ntfs_ino;
353 /* Insert the search tree node */
354 if (avl_tree_insert(&map->root, &new_node->index_node,
355 _avl_cmp_by_ntfs_ino))
357 /* This should be impossible since an NTFS inode cannot
358 * have multiple DOS names, and we only should get each
359 * DOS name entry once from the ntfs_readdir() calls. */
360 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
361 le64_to_cpu(ntfs_ino));
363 return WIMLIB_ERR_NOMEM;
365 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
369 /* Returns a structure that contains the DOS name and its length for an NTFS
370 * inode, or NULL if the inode has no DOS name. */
371 static struct dos_name_node *
372 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
374 struct dos_name_node dummy;
375 struct avl_tree_node *res;
377 dummy.ntfs_ino = cpu_to_le64(ntfs_ino);
379 res = avl_tree_lookup_node(map->root, &dummy.index_node,
380 _avl_cmp_by_ntfs_ino);
383 return DOS_NAME_NODE(res);
387 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
389 const struct dos_name_node *node;
391 if (dentry->is_win32_name) {
392 node = lookup_dos_name(map, dentry->d_inode->i_ino);
394 dentry->short_name = utf16le_dupz(node->dos_name,
396 if (!dentry->short_name)
397 return WIMLIB_ERR_NOMEM;
398 dentry->short_name_nbytes = node->name_nbytes;
399 DEBUG("Assigned DOS name to ino %"PRIu64,
400 dentry->d_inode->i_ino);
402 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
403 "corresponding DOS name",
404 dentry->d_inode->i_ino);
411 free_dos_name_tree(struct avl_tree_node *node) {
413 free_dos_name_tree(node->left);
414 free_dos_name_tree(node->right);
415 FREE(DOS_NAME_NODE(node));
420 destroy_dos_name_map(struct dos_name_map *map)
422 free_dos_name_tree(map->root);
426 struct wim_dentry *parent;
429 struct dos_name_map *dos_name_map;
431 struct capture_params *params;
436 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
441 ntfs_volume *ntfs_vol,
442 struct capture_params *params);
445 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
446 const int name_nchars, const int name_type,
447 const s64 pos, const MFT_REF mref,
448 const unsigned dt_type)
450 struct readdir_ctx *ctx;
451 size_t mbs_name_nbytes;
453 struct wim_dentry *child;
456 size_t name_nbytes = name_nchars * sizeof(ntfschar);
459 if (name_type & FILE_NAME_DOS) {
460 /* If this is the entry for a DOS name, store it for later. */
461 ret = insert_dos_name(ctx->dos_name_map, name,
462 name_nbytes, mref & MFT_REF_MASK_CPU);
464 /* Return now if an error occurred or if this is just a DOS name
465 * and not a Win32+DOS name. */
466 if (ret != 0 || name_type == FILE_NAME_DOS)
469 ret = utf16le_to_tstr(name, name_nbytes,
470 &mbs_name, &mbs_name_nbytes);
474 if (mbs_name[0] == '.' &&
475 (mbs_name[1] == '\0' ||
476 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
479 * note: name_type is POSIX for these, so DOS names will not
480 * have been inserted for them. */
482 goto out_free_mbs_name;
485 /* Open the inode for this directory entry and recursively capture the
486 * directory tree rooted at it */
487 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
489 /* XXX This used to be treated as an error, but NTFS-3g seemed
490 * to be unable to read some inodes on a Windows 8 image for
492 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
493 ctx->path, mbs_name);
495 goto out_free_mbs_name;
497 path_len = ctx->path_len;
499 ctx->path[path_len++] = '/';
500 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
501 path_len += mbs_name_nbytes;
503 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
505 ctx->vol, ctx->params);
506 path_len -= mbs_name_nbytes + 1;
508 dentry_add_child(ctx->parent, child);
509 ntfs_inode_close(ni);
513 ctx->path[ctx->path_len] = '\0';
518 /* Recursive scan routine for NTFS volumes */
520 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
526 struct capture_params *params)
530 struct wim_dentry *root = NULL;
531 struct wim_inode *inode = NULL;
533 ret = try_exclude(path, path_len, params);
534 if (ret < 0) /* Excluded? */
536 if (ret > 0) /* Error? */
539 /* Get file attributes */
540 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
541 if (ret != sizeof(attributes)) {
542 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
543 ret = WIMLIB_ERR_NTFS_3G;
547 if (attributes & FILE_ATTRIBUTE_ENCRYPTED) {
548 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
550 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
551 "does not support encrypted files and directories", path);
552 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
555 params->progress.scan.cur_path = path;
556 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
560 /* Create a WIM dentry with an associated inode, which may be shared */
561 ret = inode_table_new_dentry(params->inode_table,
562 path_basename_with_len(path, path_len),
563 ni->mft_no, 0, false, &root);
567 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
568 root->is_win32_name = 1;
570 inode = root->d_inode;
572 if (inode->i_nlink > 1) {
573 /* Shared inode; nothing more to do */
577 inode->i_creation_time = le64_to_cpu(ni->creation_time);
578 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
579 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
580 inode->i_attributes = attributes;
582 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
583 /* Scan the reparse point stream. */
584 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
585 params->unhashed_blobs,
586 vol, AT_REPARSE_POINT);
591 /* Scan the data streams.
593 * Note: directories should not have an unnamed data stream, but they
594 * may have named data streams. Nondirectories (including reparse
595 * points) can have an unnamed data stream as well as named data
597 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
598 params->unhashed_blobs, vol, AT_DATA);
602 if (inode_is_directory(inode)) {
604 /* Recurse to directory children */
606 struct dos_name_map dos_name_map = { .root = NULL };
607 struct readdir_ctx ctx = {
610 .path_len = path_len,
611 .dos_name_map = &dos_name_map,
616 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
622 /* error from ntfs_readdir() itself */
623 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
624 ret = WIMLIB_ERR_NTFS_3G;
627 struct wim_dentry *child;
630 for_dentry_child(child, root) {
631 ret = set_dentry_dos_name(child, &dos_name_map);
636 destroy_dos_name_map(&dos_name_map);
640 path[path_len] = '\0';
642 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
643 * only allow capturing an entire volume. */
644 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
645 inode_is_symlink(inode))
646 inode->i_not_rpfixed = 0;
648 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
649 struct SECURITY_CONTEXT sec_ctx;
653 /* Get security descriptor */
654 memset(&sec_ctx, 0, sizeof(sec_ctx));
659 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
660 if (ret > sizeof(_sd)) {
662 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
665 inode->i_security_id = sd_set_add_sd(params->sd_set,
667 if (inode->i_security_id == -1) {
668 ERROR("Out of memory");
669 ret = WIMLIB_ERR_NOMEM;
672 DEBUG("Added security ID = %u for `%s'",
673 inode->i_security_id, path);
675 } else if (ret < 0) {
676 ERROR_WITH_ERRNO("Failed to get security information from "
678 ret = WIMLIB_ERR_NTFS_3G;
680 inode->i_security_id = -1;
681 DEBUG("No security ID for `%s'", path);
688 params->progress.scan.cur_path = path;
690 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
692 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
695 free_dentry_tree(root, params->blob_table);
697 ret = report_capture_error(params, ret, path);
705 do_ntfs_umount(struct _ntfs_volume *vol)
707 DEBUG("Unmounting NTFS volume");
708 if (ntfs_umount(vol, FALSE))
709 return WIMLIB_ERR_NTFS_3G;
715 build_dentry_tree_ntfs(struct wim_dentry **root_p,
717 struct capture_params *params)
723 DEBUG("Mounting NTFS volume `%s' read-only", device);
725 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
728 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
729 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
730 * seem to contain any explicit version information. So we have to rely on a
731 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
733 #ifdef HAVE_NTFS_MNT_RDONLY
735 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
736 #elif defined(MS_RDONLY)
737 /* NTFS-3g 2011, 2012 */
738 vol = ntfs_mount(device, MS_RDONLY);
740 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
743 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
745 return WIMLIB_ERR_NTFS_3G;
747 ntfs_open_secure(vol);
749 /* We don't want to capture the special NTFS files such as $Bitmap. Not
750 * to be confused with "hidden" or "system" files which are real files
751 * that we do need to capture. */
752 NVolClearShowSysFiles(vol);
754 DEBUG("Opening root NTFS dentry");
755 root_ni = ntfs_inode_open(vol, FILE_root);
757 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
759 ret = WIMLIB_ERR_NTFS_3G;
763 /* Currently we assume that all the paths fit into this length and there
764 * is no check for overflow. */
765 char *path = MALLOC(32768);
767 ERROR("Could not allocate memory for NTFS pathname");
768 ret = WIMLIB_ERR_NOMEM;
774 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
775 FILE_NAME_POSIX, vol, params);
778 ntfs_inode_close(root_ni);
780 ntfs_index_ctx_put(vol->secure_xsii);
781 ntfs_index_ctx_put(vol->secure_xsdh);
782 ntfs_inode_close(vol->secure_ni);
785 if (do_ntfs_umount(vol)) {
786 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
790 /* We need to leave the NTFS volume mounted so that we can read
791 * the NTFS files again when we are actually writing the WIM */
792 *(ntfs_volume**)params->extra_arg = vol;
796 #endif /* WITH_NTFS_3G */