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 /* When sorting blobs located in NTFS volumes for sequential reading, we sort
172 * first by starting LCN of the attribute if available, otherwise no sort order
173 * is defined. This usually results in better sequential access to the volume.
176 set_attr_sort_key(ntfs_inode *ni, struct ntfs_location *loc)
181 na = open_ntfs_attr(ni, loc);
183 return WIMLIB_ERR_NTFS_3G;
185 rl = ntfs_attr_find_vcn(na, 0);
186 if (rl && rl->lcn != LCN_HOLE)
187 loc->sort_key = rl->lcn;
195 /* Save information about an NTFS attribute (stream) to a WIM inode. */
197 scan_ntfs_attr(struct wim_inode *inode,
201 struct list_head *unhashed_blobs,
204 const ATTR_RECORD *record)
206 const u64 data_size = ntfs_get_attribute_value_length(record);
207 const size_t name_nchars = record->name_length;
208 struct blob_descriptor *blob = NULL;
209 utf16lechar *stream_name = NULL;
210 struct wim_inode_stream *strm;
213 if (unlikely(name_nchars)) {
215 stream_name = utf16le_dupz(attr_record_name(record),
216 name_nchars * sizeof(ntfschar));
218 ret = WIMLIB_ERR_NOMEM;
223 /* If the stream is non-empty, set up a blob descriptor for it. */
224 if (data_size != 0) {
225 blob = new_blob_descriptor();
226 if (unlikely(!blob)) {
227 ret = WIMLIB_ERR_NOMEM;
231 blob->ntfs_loc = CALLOC(1, sizeof(struct ntfs_location));
232 if (unlikely(!blob->ntfs_loc)) {
233 ret = WIMLIB_ERR_NOMEM;
237 blob->blob_location = BLOB_IN_NTFS_VOLUME;
238 blob->size = data_size;
239 blob->ntfs_loc->ntfs_vol = vol;
240 blob->ntfs_loc->attr_type = type;
241 blob->ntfs_loc->mft_no = ni->mft_no;
243 if (unlikely(name_nchars)) {
244 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
245 if (!blob->ntfs_loc->attr_name) {
246 ret = WIMLIB_ERR_NOMEM;
249 blob->ntfs_loc->attr_name_nchars = name_nchars;
252 ret = set_attr_sort_key(ni, blob->ntfs_loc);
256 if (unlikely(type == AT_REPARSE_POINT)) {
257 if (blob->size < REPARSE_DATA_OFFSET) {
258 ERROR("Reparse data of \"%s\" "
259 "is invalid (only %"PRIu64" bytes)!",
261 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
264 blob->size -= REPARSE_DATA_OFFSET;
265 ret = read_reparse_tag(ni, blob->ntfs_loc,
266 &inode->i_reparse_tag);
272 strm = inode_add_stream(inode,
273 attr_type_to_wimlib_stream_type(type),
274 stream_name ? stream_name : NO_STREAM_NAME,
276 if (unlikely(!strm)) {
277 ret = WIMLIB_ERR_NOMEM;
280 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
284 free_blob_descriptor(blob);
289 /* Scan attributes of the specified type from a file in the NTFS volume */
291 scan_ntfs_attrs_with_type(struct wim_inode *inode,
295 struct list_head *unhashed_blobs,
299 ntfs_attr_search_ctx *actx;
302 DEBUG("Scanning NTFS attributes from \"%s\"", path);
304 actx = ntfs_attr_get_search_ctx(ni, NULL);
306 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
307 "context for \"%s\"", path);
308 return WIMLIB_ERR_NTFS_3G;
311 while (!ntfs_attr_lookup(type, NULL, 0,
312 CASE_SENSITIVE, 0, NULL, 0, actx))
314 ret = scan_ntfs_attr(inode,
325 if (errno != ENOENT) {
326 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
327 ret = WIMLIB_ERR_NTFS_3G;
332 ntfs_attr_put_search_ctx(actx);
336 /* Binary tree that maps NTFS inode numbers to DOS names */
337 struct dos_name_map {
338 struct avl_tree_node *root;
341 struct dos_name_node {
342 struct avl_tree_node index_node;
348 #define DOS_NAME_NODE(avl_node) \
349 avl_tree_entry(avl_node, struct dos_name_node, index_node)
352 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
353 const struct avl_tree_node *n2)
355 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
356 DOS_NAME_NODE(n2)->ntfs_ino);
359 /* Inserts a new DOS name into the map */
361 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
362 size_t name_nbytes, le64 ntfs_ino)
364 struct dos_name_node *new_node;
366 DEBUG("DOS name_len = %zu", name_nbytes);
367 new_node = MALLOC(sizeof(struct dos_name_node));
369 return WIMLIB_ERR_NOMEM;
371 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
372 * assuming 2-byte ntfs characters) */
373 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
375 /* Initialize the DOS name, DOS name length, and NTFS inode number of
376 * the search tree node */
377 memcpy(new_node->dos_name, dos_name, name_nbytes);
378 new_node->name_nbytes = name_nbytes;
379 new_node->ntfs_ino = ntfs_ino;
381 /* Insert the search tree node */
382 if (avl_tree_insert(&map->root, &new_node->index_node,
383 _avl_cmp_by_ntfs_ino))
385 /* This should be impossible since an NTFS inode cannot
386 * have multiple DOS names, and we only should get each
387 * DOS name entry once from the ntfs_readdir() calls. */
388 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
389 le64_to_cpu(ntfs_ino));
391 return WIMLIB_ERR_NOMEM;
393 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
397 /* Returns a structure that contains the DOS name and its length for an NTFS
398 * inode, or NULL if the inode has no DOS name. */
399 static struct dos_name_node *
400 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
402 struct dos_name_node dummy;
403 struct avl_tree_node *res;
405 dummy.ntfs_ino = cpu_to_le64(ntfs_ino);
407 res = avl_tree_lookup_node(map->root, &dummy.index_node,
408 _avl_cmp_by_ntfs_ino);
411 return DOS_NAME_NODE(res);
415 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
417 const struct dos_name_node *node;
419 if (dentry->is_win32_name) {
420 node = lookup_dos_name(map, dentry->d_inode->i_ino);
422 dentry->short_name = utf16le_dupz(node->dos_name,
424 if (!dentry->short_name)
425 return WIMLIB_ERR_NOMEM;
426 dentry->short_name_nbytes = node->name_nbytes;
427 DEBUG("Assigned DOS name to ino %"PRIu64,
428 dentry->d_inode->i_ino);
430 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
431 "corresponding DOS name",
432 dentry->d_inode->i_ino);
439 free_dos_name_tree(struct avl_tree_node *node) {
441 free_dos_name_tree(node->left);
442 free_dos_name_tree(node->right);
443 FREE(DOS_NAME_NODE(node));
448 destroy_dos_name_map(struct dos_name_map *map)
450 free_dos_name_tree(map->root);
454 struct wim_dentry *parent;
457 struct dos_name_map *dos_name_map;
459 struct capture_params *params;
464 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
469 ntfs_volume *ntfs_vol,
470 struct capture_params *params);
473 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
474 const int name_nchars, const int name_type,
475 const s64 pos, const MFT_REF mref,
476 const unsigned dt_type)
478 struct readdir_ctx *ctx;
479 size_t mbs_name_nbytes;
481 struct wim_dentry *child;
484 size_t name_nbytes = name_nchars * sizeof(ntfschar);
487 if (name_type & FILE_NAME_DOS) {
488 /* If this is the entry for a DOS name, store it for later. */
489 ret = insert_dos_name(ctx->dos_name_map, name,
490 name_nbytes, mref & MFT_REF_MASK_CPU);
492 /* Return now if an error occurred or if this is just a DOS name
493 * and not a Win32+DOS name. */
494 if (ret != 0 || name_type == FILE_NAME_DOS)
497 ret = utf16le_to_tstr(name, name_nbytes,
498 &mbs_name, &mbs_name_nbytes);
502 if (mbs_name[0] == '.' &&
503 (mbs_name[1] == '\0' ||
504 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
507 * note: name_type is POSIX for these, so DOS names will not
508 * have been inserted for them. */
510 goto out_free_mbs_name;
513 /* Open the inode for this directory entry and recursively capture the
514 * directory tree rooted at it */
515 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
517 /* XXX This used to be treated as an error, but NTFS-3g seemed
518 * to be unable to read some inodes on a Windows 8 image for
520 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
521 ctx->path, mbs_name);
523 goto out_free_mbs_name;
525 path_len = ctx->path_len;
527 ctx->path[path_len++] = '/';
528 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
529 path_len += mbs_name_nbytes;
531 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
533 ctx->vol, ctx->params);
534 path_len -= mbs_name_nbytes + 1;
536 dentry_add_child(ctx->parent, child);
537 ntfs_inode_close(ni);
541 ctx->path[ctx->path_len] = '\0';
546 /* Recursive scan routine for NTFS volumes */
548 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
554 struct capture_params *params)
558 struct wim_dentry *root = NULL;
559 struct wim_inode *inode = NULL;
561 ret = try_exclude(path, path_len, params);
562 if (ret < 0) /* Excluded? */
564 if (ret > 0) /* Error? */
567 /* Get file attributes */
568 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
569 if (ret != sizeof(attributes)) {
570 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
571 ret = WIMLIB_ERR_NTFS_3G;
575 if (attributes & FILE_ATTRIBUTE_ENCRYPTED) {
576 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
578 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
579 "does not support encrypted files and directories", path);
580 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
583 params->progress.scan.cur_path = path;
584 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
588 /* Create a WIM dentry with an associated inode, which may be shared */
589 ret = inode_table_new_dentry(params->inode_table,
590 path_basename_with_len(path, path_len),
591 ni->mft_no, 0, false, &root);
595 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
596 root->is_win32_name = 1;
598 inode = root->d_inode;
600 if (inode->i_nlink > 1) {
601 /* Shared inode; nothing more to do */
605 inode->i_creation_time = le64_to_cpu(ni->creation_time);
606 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
607 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
608 inode->i_attributes = attributes;
610 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
611 /* Scan the reparse point stream. */
612 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
613 params->unhashed_blobs,
614 vol, AT_REPARSE_POINT);
619 /* Scan the data streams.
621 * Note: directories should not have an unnamed data stream, but they
622 * may have named data streams. Nondirectories (including reparse
623 * points) can have an unnamed data stream as well as named data
625 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
626 params->unhashed_blobs, vol, AT_DATA);
630 if (inode_is_directory(inode)) {
632 /* Recurse to directory children */
634 struct dos_name_map dos_name_map = { .root = NULL };
635 struct readdir_ctx ctx = {
638 .path_len = path_len,
639 .dos_name_map = &dos_name_map,
644 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
650 /* error from ntfs_readdir() itself */
651 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
652 ret = WIMLIB_ERR_NTFS_3G;
655 struct wim_dentry *child;
658 for_dentry_child(child, root) {
659 ret = set_dentry_dos_name(child, &dos_name_map);
664 destroy_dos_name_map(&dos_name_map);
668 path[path_len] = '\0';
670 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
671 * only allow capturing an entire volume. */
672 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
673 inode_is_symlink(inode))
674 inode->i_not_rpfixed = 0;
676 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
677 struct SECURITY_CONTEXT sec_ctx;
681 /* Get security descriptor */
682 memset(&sec_ctx, 0, sizeof(sec_ctx));
687 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
688 if (ret > sizeof(_sd)) {
690 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
693 inode->i_security_id = sd_set_add_sd(params->sd_set,
695 if (inode->i_security_id == -1) {
696 ERROR("Out of memory");
697 ret = WIMLIB_ERR_NOMEM;
700 DEBUG("Added security ID = %u for `%s'",
701 inode->i_security_id, path);
703 } else if (ret < 0) {
704 ERROR_WITH_ERRNO("Failed to get security information from "
706 ret = WIMLIB_ERR_NTFS_3G;
708 inode->i_security_id = -1;
709 DEBUG("No security ID for `%s'", path);
716 params->progress.scan.cur_path = path;
718 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
720 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
723 free_dentry_tree(root, params->blob_table);
725 ret = report_capture_error(params, ret, path);
733 do_ntfs_umount(struct _ntfs_volume *vol)
735 DEBUG("Unmounting NTFS volume");
736 if (ntfs_umount(vol, FALSE))
737 return WIMLIB_ERR_NTFS_3G;
743 build_dentry_tree_ntfs(struct wim_dentry **root_p,
745 struct capture_params *params)
751 DEBUG("Mounting NTFS volume `%s' read-only", device);
753 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
756 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
757 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
758 * seem to contain any explicit version information. So we have to rely on a
759 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
761 #ifdef HAVE_NTFS_MNT_RDONLY
763 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
764 #elif defined(MS_RDONLY)
765 /* NTFS-3g 2011, 2012 */
766 vol = ntfs_mount(device, MS_RDONLY);
768 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
771 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
773 return WIMLIB_ERR_NTFS_3G;
775 ntfs_open_secure(vol);
777 /* We don't want to capture the special NTFS files such as $Bitmap. Not
778 * to be confused with "hidden" or "system" files which are real files
779 * that we do need to capture. */
780 NVolClearShowSysFiles(vol);
782 DEBUG("Opening root NTFS dentry");
783 root_ni = ntfs_inode_open(vol, FILE_root);
785 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
787 ret = WIMLIB_ERR_NTFS_3G;
791 /* Currently we assume that all the paths fit into this length and there
792 * is no check for overflow. */
793 char *path = MALLOC(32768);
795 ERROR("Could not allocate memory for NTFS pathname");
796 ret = WIMLIB_ERR_NOMEM;
802 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
803 FILE_NAME_POSIX, vol, params);
806 ntfs_inode_close(root_ni);
808 ntfs_index_ctx_put(vol->secure_xsii);
809 ntfs_index_ctx_put(vol->secure_xsdh);
810 ntfs_inode_close(vol->secure_ni);
813 if (do_ntfs_umount(vol)) {
814 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
818 /* We need to leave the NTFS volume mounted so that we can read
819 * the NTFS files again when we are actually writing the WIM */
820 *(ntfs_volume**)params->extra_arg = vol;
824 #endif /* WITH_NTFS_3G */