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 /* A reference-counted NTFS volume than is automatically unmounted when the
52 * reference count reaches 0 */
53 struct ntfs_volume_wrapper {
58 /* Description of where data is located in an NTFS volume */
59 struct ntfs_location {
60 struct ntfs_volume_wrapper *volume;
62 utf16lechar *attr_name;
63 unsigned attr_name_nchars;
68 static struct ntfs_volume_wrapper *
69 get_ntfs_volume(struct ntfs_volume_wrapper *volume)
76 put_ntfs_volume(struct ntfs_volume_wrapper *volume)
78 if (--volume->refcnt == 0) {
79 ntfs_umount(volume->vol, FALSE);
84 static inline const ntfschar *
85 attr_record_name(const ATTR_RECORD *record)
87 return (const ntfschar *)
88 ((const u8 *)record + le16_to_cpu(record->name_offset));
92 open_ntfs_attr(ntfs_inode *ni, const struct ntfs_location *loc)
96 na = ntfs_attr_open(ni,
97 (ATTR_TYPES)loc->attr_type,
99 loc->attr_name_nchars);
101 ERROR_WITH_ERRNO("Failed to open attribute of NTFS inode %"PRIu64,
108 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
109 const struct read_blob_callbacks *cbs)
111 const struct ntfs_location *loc = blob->ntfs_loc;
112 ntfs_volume *vol = loc->volume->vol;
120 ni = ntfs_inode_open(vol, loc->mft_no);
122 ERROR_WITH_ERRNO("Failed to open NTFS inode %"PRIu64,
124 ret = WIMLIB_ERR_NTFS_3G;
128 na = open_ntfs_attr(ni, loc);
130 ret = WIMLIB_ERR_NTFS_3G;
131 goto out_close_ntfs_inode;
134 pos = (loc->attr_type == AT_REPARSE_POINT) ? REPARSE_DATA_OFFSET : 0;
135 bytes_remaining = size;
136 while (bytes_remaining) {
137 s64 to_read = min(bytes_remaining, sizeof(buf));
138 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
139 ERROR_WITH_ERRNO("Error reading data from NTFS inode "
140 "%"PRIu64, loc->mft_no);
141 ret = WIMLIB_ERR_NTFS_3G;
142 goto out_close_ntfs_attr;
145 bytes_remaining -= to_read;
146 ret = call_consume_chunk(buf, to_read, cbs);
148 goto out_close_ntfs_attr;
153 out_close_ntfs_inode:
154 ntfs_inode_close(ni);
160 free_ntfs_location(struct ntfs_location *loc)
162 put_ntfs_volume(loc->volume);
163 FREE(loc->attr_name);
167 struct ntfs_location *
168 clone_ntfs_location(const struct ntfs_location *loc)
170 struct ntfs_location *new = memdup(loc, sizeof(*loc));
173 if (loc->attr_name) {
174 new->attr_name = utf16le_dup(loc->attr_name);
178 new->volume = get_ntfs_volume(loc->volume);
188 cmp_ntfs_locations(const struct ntfs_location *loc1,
189 const struct ntfs_location *loc2)
191 return cmp_u64(loc1->sort_key, loc2->sort_key);
195 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
196 u32 *reparse_tag_ret)
202 na = open_ntfs_attr(ni, loc);
204 ret = WIMLIB_ERR_NTFS_3G;
208 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
209 &reparse_tag) != sizeof(reparse_tag))
211 ERROR_WITH_ERRNO("Error reading reparse data");
212 ret = WIMLIB_ERR_NTFS_3G;
213 goto out_close_ntfs_attr;
215 *reparse_tag_ret = le32_to_cpu(reparse_tag);
216 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
226 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
230 return STREAM_TYPE_DATA;
231 case AT_REPARSE_POINT:
232 return STREAM_TYPE_REPARSE_POINT;
235 return STREAM_TYPE_UNKNOWN;
239 /* When sorting blobs located in NTFS volumes for sequential reading, we sort
240 * first by starting LCN of the attribute if available, otherwise no sort order
241 * is defined. This usually results in better sequential access to the volume.
244 set_attr_sort_key(ntfs_inode *ni, struct ntfs_location *loc)
249 na = open_ntfs_attr(ni, loc);
251 return WIMLIB_ERR_NTFS_3G;
253 rl = ntfs_attr_find_vcn(na, 0);
254 if (rl && rl->lcn != LCN_HOLE)
255 loc->sort_key = rl->lcn;
263 /* Save information about an NTFS attribute (stream) to a WIM inode. */
265 scan_ntfs_attr(struct wim_inode *inode,
269 struct list_head *unhashed_blobs,
270 struct ntfs_volume_wrapper *volume,
272 const ATTR_RECORD *record)
274 const u64 data_size = ntfs_get_attribute_value_length(record);
275 const size_t name_nchars = record->name_length;
276 struct blob_descriptor *blob = NULL;
277 utf16lechar *stream_name = NULL;
278 struct wim_inode_stream *strm;
281 if (unlikely(name_nchars)) {
283 stream_name = utf16le_dupz(attr_record_name(record),
284 name_nchars * sizeof(ntfschar));
286 ret = WIMLIB_ERR_NOMEM;
291 /* If the stream is non-empty, set up a blob descriptor for it. */
292 if (data_size != 0) {
293 blob = new_blob_descriptor();
294 if (unlikely(!blob)) {
295 ret = WIMLIB_ERR_NOMEM;
299 blob->ntfs_loc = CALLOC(1, sizeof(struct ntfs_location));
300 if (unlikely(!blob->ntfs_loc)) {
301 ret = WIMLIB_ERR_NOMEM;
305 blob->blob_location = BLOB_IN_NTFS_VOLUME;
306 blob->size = data_size;
307 blob->ntfs_loc->volume = get_ntfs_volume(volume);
308 blob->ntfs_loc->attr_type = type;
309 blob->ntfs_loc->mft_no = ni->mft_no;
311 if (unlikely(name_nchars)) {
312 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
313 if (!blob->ntfs_loc->attr_name) {
314 ret = WIMLIB_ERR_NOMEM;
317 blob->ntfs_loc->attr_name_nchars = name_nchars;
320 ret = set_attr_sort_key(ni, blob->ntfs_loc);
324 if (unlikely(type == AT_REPARSE_POINT)) {
325 if (blob->size < REPARSE_DATA_OFFSET) {
326 ERROR("Reparse data of \"%s\" "
327 "is invalid (only %"PRIu64" bytes)!",
329 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
332 blob->size -= REPARSE_DATA_OFFSET;
333 ret = read_reparse_tag(ni, blob->ntfs_loc,
334 &inode->i_reparse_tag);
340 strm = inode_add_stream(inode,
341 attr_type_to_wimlib_stream_type(type),
342 stream_name ? stream_name : NO_STREAM_NAME,
344 if (unlikely(!strm)) {
345 ret = WIMLIB_ERR_NOMEM;
348 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
352 free_blob_descriptor(blob);
357 /* Scan attributes of the specified type from a file in the NTFS volume */
359 scan_ntfs_attrs_with_type(struct wim_inode *inode,
363 struct list_head *unhashed_blobs,
364 struct ntfs_volume_wrapper *volume,
367 ntfs_attr_search_ctx *actx;
370 DEBUG("Scanning NTFS attributes from \"%s\"", path);
372 actx = ntfs_attr_get_search_ctx(ni, NULL);
374 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
375 "context for \"%s\"", path);
376 return WIMLIB_ERR_NTFS_3G;
379 while (!ntfs_attr_lookup(type, NULL, 0,
380 CASE_SENSITIVE, 0, NULL, 0, actx))
382 ret = scan_ntfs_attr(inode,
393 if (errno != ENOENT) {
394 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
395 ret = WIMLIB_ERR_NTFS_3G;
400 ntfs_attr_put_search_ctx(actx);
404 /* Binary tree that maps NTFS inode numbers to DOS names */
405 struct dos_name_map {
406 struct avl_tree_node *root;
409 struct dos_name_node {
410 struct avl_tree_node index_node;
416 #define DOS_NAME_NODE(avl_node) \
417 avl_tree_entry(avl_node, struct dos_name_node, index_node)
420 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
421 const struct avl_tree_node *n2)
423 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
424 DOS_NAME_NODE(n2)->ntfs_ino);
427 /* Inserts a new DOS name into the map */
429 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
430 size_t name_nbytes, le64 ntfs_ino)
432 struct dos_name_node *new_node;
434 DEBUG("DOS name_len = %zu", name_nbytes);
435 new_node = MALLOC(sizeof(struct dos_name_node));
437 return WIMLIB_ERR_NOMEM;
439 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
440 * assuming 2-byte ntfs characters) */
441 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
443 /* Initialize the DOS name, DOS name length, and NTFS inode number of
444 * the search tree node */
445 memcpy(new_node->dos_name, dos_name, name_nbytes);
446 new_node->name_nbytes = name_nbytes;
447 new_node->ntfs_ino = ntfs_ino;
449 /* Insert the search tree node */
450 if (avl_tree_insert(&map->root, &new_node->index_node,
451 _avl_cmp_by_ntfs_ino))
453 /* This should be impossible since an NTFS inode cannot
454 * have multiple DOS names, and we only should get each
455 * DOS name entry once from the ntfs_readdir() calls. */
456 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
457 le64_to_cpu(ntfs_ino));
459 return WIMLIB_ERR_NOMEM;
461 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
465 /* Returns a structure that contains the DOS name and its length for an NTFS
466 * inode, or NULL if the inode has no DOS name. */
467 static struct dos_name_node *
468 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
470 struct dos_name_node dummy;
471 struct avl_tree_node *res;
473 dummy.ntfs_ino = cpu_to_le64(ntfs_ino);
475 res = avl_tree_lookup_node(map->root, &dummy.index_node,
476 _avl_cmp_by_ntfs_ino);
479 return DOS_NAME_NODE(res);
483 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
485 const struct dos_name_node *node;
487 if (dentry->is_win32_name) {
488 node = lookup_dos_name(map, dentry->d_inode->i_ino);
490 dentry->short_name = utf16le_dupz(node->dos_name,
492 if (!dentry->short_name)
493 return WIMLIB_ERR_NOMEM;
494 dentry->short_name_nbytes = node->name_nbytes;
495 DEBUG("Assigned DOS name to ino %"PRIu64,
496 dentry->d_inode->i_ino);
498 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
499 "corresponding DOS name",
500 dentry->d_inode->i_ino);
507 free_dos_name_tree(struct avl_tree_node *node) {
509 free_dos_name_tree(node->left);
510 free_dos_name_tree(node->right);
511 FREE(DOS_NAME_NODE(node));
516 destroy_dos_name_map(struct dos_name_map *map)
518 free_dos_name_tree(map->root);
522 struct wim_dentry *parent;
525 struct dos_name_map *dos_name_map;
526 struct ntfs_volume_wrapper *volume;
527 struct capture_params *params;
532 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
537 struct ntfs_volume_wrapper *volume,
538 struct capture_params *params);
541 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
542 const int name_nchars, const int name_type,
543 const s64 pos, const MFT_REF mref,
544 const unsigned dt_type)
546 struct readdir_ctx *ctx;
547 size_t mbs_name_nbytes;
549 struct wim_dentry *child;
552 size_t name_nbytes = name_nchars * sizeof(ntfschar);
555 if (name_type & FILE_NAME_DOS) {
556 /* If this is the entry for a DOS name, store it for later. */
557 ret = insert_dos_name(ctx->dos_name_map, name,
558 name_nbytes, mref & MFT_REF_MASK_CPU);
560 /* Return now if an error occurred or if this is just a DOS name
561 * and not a Win32+DOS name. */
562 if (ret != 0 || name_type == FILE_NAME_DOS)
565 ret = utf16le_to_tstr(name, name_nbytes,
566 &mbs_name, &mbs_name_nbytes);
570 if (mbs_name[0] == '.' &&
571 (mbs_name[1] == '\0' ||
572 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
575 * note: name_type is POSIX for these, so DOS names will not
576 * have been inserted for them. */
578 goto out_free_mbs_name;
581 /* Open the inode for this directory entry and recursively capture the
582 * directory tree rooted at it */
583 ntfs_inode *ni = ntfs_inode_open(ctx->volume->vol, mref);
585 /* XXX This used to be treated as an error, but NTFS-3g seemed
586 * to be unable to read some inodes on a Windows 8 image for
588 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
589 ctx->path, mbs_name);
591 goto out_free_mbs_name;
593 path_len = ctx->path_len;
595 ctx->path[path_len++] = '/';
596 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
597 path_len += mbs_name_nbytes;
599 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
601 ctx->volume, ctx->params);
602 path_len -= mbs_name_nbytes + 1;
604 dentry_add_child(ctx->parent, child);
605 ntfs_inode_close(ni);
609 ctx->path[ctx->path_len] = '\0';
614 /* Recursive scan routine for NTFS volumes */
616 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
621 struct ntfs_volume_wrapper *volume,
622 struct capture_params *params)
626 struct wim_dentry *root = NULL;
627 struct wim_inode *inode = NULL;
629 ret = try_exclude(path, path_len, params);
630 if (ret < 0) /* Excluded? */
632 if (ret > 0) /* Error? */
635 /* Get file attributes */
636 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
637 if (ret != sizeof(attributes)) {
638 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
639 ret = WIMLIB_ERR_NTFS_3G;
643 if (attributes & FILE_ATTRIBUTE_ENCRYPTED) {
644 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
646 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
647 "does not support encrypted files and directories", path);
648 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
651 params->progress.scan.cur_path = path;
652 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
656 /* Create a WIM dentry with an associated inode, which may be shared */
657 ret = inode_table_new_dentry(params->inode_table,
658 path_basename_with_len(path, path_len),
659 ni->mft_no, 0, false, &root);
663 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
664 root->is_win32_name = 1;
666 inode = root->d_inode;
668 if (inode->i_nlink > 1) {
669 /* Shared inode; nothing more to do */
673 inode->i_creation_time = le64_to_cpu(ni->creation_time);
674 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
675 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
676 inode->i_attributes = attributes;
678 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
679 /* Scan the reparse point stream. */
680 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
681 params->unhashed_blobs,
682 volume, AT_REPARSE_POINT);
687 /* Scan the data streams.
689 * Note: directories should not have an unnamed data stream, but they
690 * may have named data streams. Nondirectories (including reparse
691 * points) can have an unnamed data stream as well as named data
693 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
694 params->unhashed_blobs,
699 if (inode_is_directory(inode)) {
701 /* Recurse to directory children */
703 struct dos_name_map dos_name_map = { .root = NULL };
704 struct readdir_ctx ctx = {
707 .path_len = path_len,
708 .dos_name_map = &dos_name_map,
713 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
719 /* error from ntfs_readdir() itself */
720 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
721 ret = WIMLIB_ERR_NTFS_3G;
724 struct wim_dentry *child;
727 for_dentry_child(child, root) {
728 ret = set_dentry_dos_name(child, &dos_name_map);
733 destroy_dos_name_map(&dos_name_map);
737 path[path_len] = '\0';
739 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
740 * only allow capturing an entire volume. */
741 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
742 inode_is_symlink(inode))
743 inode->i_not_rpfixed = 0;
745 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
746 struct SECURITY_CONTEXT sec_ctx;
750 /* Get security descriptor */
751 memset(&sec_ctx, 0, sizeof(sec_ctx));
752 sec_ctx.vol = volume->vol;
756 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
757 if (ret > sizeof(_sd)) {
759 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
762 inode->i_security_id = sd_set_add_sd(params->sd_set,
764 if (inode->i_security_id == -1) {
765 ERROR("Out of memory");
766 ret = WIMLIB_ERR_NOMEM;
769 DEBUG("Added security ID = %u for `%s'",
770 inode->i_security_id, path);
772 } else if (ret < 0) {
773 ERROR_WITH_ERRNO("Failed to get security information from "
775 ret = WIMLIB_ERR_NTFS_3G;
777 inode->i_security_id = -1;
778 DEBUG("No security ID for `%s'", path);
785 params->progress.scan.cur_path = path;
787 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
789 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
792 free_dentry_tree(root, params->blob_table);
794 ret = report_capture_error(params, ret, path);
801 build_dentry_tree_ntfs(struct wim_dentry **root_p,
803 struct capture_params *params)
805 struct ntfs_volume_wrapper *volume;
811 volume = MALLOC(sizeof(struct ntfs_volume_wrapper));
813 return WIMLIB_ERR_NOMEM;
815 DEBUG("Mounting NTFS volume `%s' read-only", device);
817 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
820 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
821 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers
822 * don't seem to contain any explicit version information. So we have
823 * to rely on a test done at configure time to detect whether
824 * NTFS_MNT_RDONLY should be used. */
825 #ifdef HAVE_NTFS_MNT_RDONLY
827 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
828 #elif defined(MS_RDONLY)
829 /* NTFS-3g 2011, 2012 */
830 vol = ntfs_mount(device, MS_RDONLY);
832 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
835 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
838 return WIMLIB_ERR_NTFS_3G;
844 ntfs_open_secure(vol);
846 /* We don't want to capture the special NTFS files such as $Bitmap. Not
847 * to be confused with "hidden" or "system" files which are real files
848 * that we do need to capture. */
849 NVolClearShowSysFiles(vol);
851 DEBUG("Opening root NTFS dentry");
852 root_ni = ntfs_inode_open(vol, FILE_root);
854 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
856 ret = WIMLIB_ERR_NTFS_3G;
857 goto out_put_ntfs_volume;
860 /* Currently we assume that all the paths fit into this length and there
861 * is no check for overflow. */
862 path = MALLOC(32768);
864 ret = WIMLIB_ERR_NOMEM;
865 goto out_close_root_ni;
870 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
871 FILE_NAME_POSIX, volume, params);
874 ntfs_inode_close(root_ni);
876 ntfs_index_ctx_put(vol->secure_xsii);
877 ntfs_index_ctx_put(vol->secure_xsdh);
878 ntfs_inode_close(vol->secure_ni);
879 put_ntfs_volume(volume);
882 #endif /* WITH_NTFS_3G */