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;
64 utf16lechar *attr_name;
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, loc->attr_type, loc->attr_name,
97 loc->attr_name_nchars);
99 ERROR_WITH_ERRNO("Failed to open attribute of NTFS inode %"PRIu64,
106 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
107 const struct read_blob_callbacks *cbs)
109 const struct ntfs_location *loc = blob->ntfs_loc;
110 ntfs_volume *vol = loc->volume->vol;
118 ni = ntfs_inode_open(vol, loc->mft_no);
120 ERROR_WITH_ERRNO("Failed to open NTFS inode %"PRIu64,
122 ret = WIMLIB_ERR_NTFS_3G;
126 na = open_ntfs_attr(ni, loc);
128 ret = WIMLIB_ERR_NTFS_3G;
129 goto out_close_ntfs_inode;
132 pos = (loc->attr_type == AT_REPARSE_POINT) ? REPARSE_DATA_OFFSET : 0;
133 bytes_remaining = size;
134 while (bytes_remaining) {
135 s64 to_read = min(bytes_remaining, sizeof(buf));
136 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
137 ERROR_WITH_ERRNO("Error reading data from NTFS inode "
138 "%"PRIu64, loc->mft_no);
139 ret = WIMLIB_ERR_NTFS_3G;
140 goto out_close_ntfs_attr;
143 bytes_remaining -= to_read;
144 ret = call_consume_chunk(buf, to_read, cbs);
146 goto out_close_ntfs_attr;
151 out_close_ntfs_inode:
152 ntfs_inode_close(ni);
158 free_ntfs_location(struct ntfs_location *loc)
160 put_ntfs_volume(loc->volume);
161 FREE(loc->attr_name);
165 struct ntfs_location *
166 clone_ntfs_location(const struct ntfs_location *loc)
168 struct ntfs_location *new = memdup(loc, sizeof(*loc));
171 if (loc->attr_name) {
172 new->attr_name = utf16le_dup(loc->attr_name);
176 new->volume = get_ntfs_volume(loc->volume);
186 cmp_ntfs_locations(const struct ntfs_location *loc1,
187 const struct ntfs_location *loc2)
189 return cmp_u64(loc1->sort_key, loc2->sort_key);
193 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
194 u32 *reparse_tag_ret)
200 na = open_ntfs_attr(ni, loc);
202 ret = WIMLIB_ERR_NTFS_3G;
206 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
207 &reparse_tag) != sizeof(reparse_tag))
209 ERROR_WITH_ERRNO("Error reading reparse data");
210 ret = WIMLIB_ERR_NTFS_3G;
211 goto out_close_ntfs_attr;
213 *reparse_tag_ret = le32_to_cpu(reparse_tag);
223 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
227 return STREAM_TYPE_DATA;
228 case AT_REPARSE_POINT:
229 return STREAM_TYPE_REPARSE_POINT;
232 return STREAM_TYPE_UNKNOWN;
236 /* When sorting blobs located in NTFS volumes for sequential reading, we sort
237 * first by starting LCN of the attribute if available, otherwise no sort order
238 * is defined. This usually results in better sequential access to the volume.
241 set_attr_sort_key(ntfs_inode *ni, struct ntfs_location *loc)
246 na = open_ntfs_attr(ni, loc);
248 return WIMLIB_ERR_NTFS_3G;
250 rl = ntfs_attr_find_vcn(na, 0);
251 if (rl && rl->lcn != LCN_HOLE)
252 loc->sort_key = rl->lcn;
260 /* Save information about an NTFS attribute (stream) to a WIM inode. */
262 scan_ntfs_attr(struct wim_inode *inode,
266 struct list_head *unhashed_blobs,
267 struct ntfs_volume_wrapper *volume,
269 const ATTR_RECORD *record)
271 const u64 data_size = ntfs_get_attribute_value_length(record);
272 const u32 name_nchars = record->name_length;
273 struct blob_descriptor *blob = NULL;
274 utf16lechar *stream_name = NULL;
275 struct wim_inode_stream *strm;
278 if (unlikely(name_nchars)) {
280 stream_name = utf16le_dupz(attr_record_name(record),
281 name_nchars * sizeof(ntfschar));
283 ret = WIMLIB_ERR_NOMEM;
288 /* If the stream is non-empty, set up a blob descriptor for it. */
289 if (data_size != 0) {
290 blob = new_blob_descriptor();
291 if (unlikely(!blob)) {
292 ret = WIMLIB_ERR_NOMEM;
296 blob->ntfs_loc = CALLOC(1, sizeof(struct ntfs_location));
297 if (unlikely(!blob->ntfs_loc)) {
298 ret = WIMLIB_ERR_NOMEM;
302 blob->blob_location = BLOB_IN_NTFS_VOLUME;
303 blob->size = data_size;
304 blob->ntfs_loc->volume = get_ntfs_volume(volume);
305 blob->ntfs_loc->attr_type = type;
306 blob->ntfs_loc->mft_no = ni->mft_no;
308 if (unlikely(name_nchars)) {
309 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
310 if (!blob->ntfs_loc->attr_name) {
311 ret = WIMLIB_ERR_NOMEM;
314 blob->ntfs_loc->attr_name_nchars = name_nchars;
317 ret = set_attr_sort_key(ni, blob->ntfs_loc);
321 if (unlikely(type == AT_REPARSE_POINT)) {
322 if (blob->size < REPARSE_DATA_OFFSET) {
323 ERROR("Reparse data of \"%s\" "
324 "is invalid (only %"PRIu64" bytes)!",
326 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
329 blob->size -= REPARSE_DATA_OFFSET;
330 ret = read_reparse_tag(ni, blob->ntfs_loc,
331 &inode->i_reparse_tag);
337 strm = inode_add_stream(inode,
338 attr_type_to_wimlib_stream_type(type),
339 stream_name ? stream_name : NO_STREAM_NAME,
341 if (unlikely(!strm)) {
342 ret = WIMLIB_ERR_NOMEM;
345 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
349 free_blob_descriptor(blob);
354 /* Scan attributes of the specified type from a file in the NTFS volume */
356 scan_ntfs_attrs_with_type(struct wim_inode *inode,
360 struct list_head *unhashed_blobs,
361 struct ntfs_volume_wrapper *volume,
364 ntfs_attr_search_ctx *actx;
367 actx = ntfs_attr_get_search_ctx(ni, NULL);
369 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
370 "context for \"%s\"", path);
371 return WIMLIB_ERR_NTFS_3G;
374 while (!ntfs_attr_lookup(type, NULL, 0,
375 CASE_SENSITIVE, 0, NULL, 0, actx))
377 ret = scan_ntfs_attr(inode,
388 if (errno != ENOENT) {
389 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
390 ret = WIMLIB_ERR_NTFS_3G;
395 ntfs_attr_put_search_ctx(actx);
399 /* Load the security descriptor of an NTFS inode into the corresponding WIM
400 * inode and the WIM image's security descriptor set. */
401 static noinline_for_stack int
402 get_security_descriptor(ntfs_inode *ni, struct wim_inode *inode,
403 ntfs_volume *vol, struct wim_sd_set *sd_set)
405 struct SECURITY_CONTEXT scx = {.vol = vol};
408 size_t avail_size = sizeof(_buf);
412 ret = ntfs_get_ntfs_acl(&scx, ni, buf, avail_size);
413 if (unlikely(ret < 0)) {
414 ret = WIMLIB_ERR_NTFS_3G;
418 if (unlikely(ret > avail_size)) {
419 if (unlikely(buf != _buf))
423 ret = WIMLIB_ERR_NOMEM;
430 if (likely(ret > 0)) {
431 inode->i_security_id = sd_set_add_sd(sd_set, buf, ret);
432 if (unlikely(inode->i_security_id < 0)) {
433 ret = WIMLIB_ERR_NOMEM;
440 if (unlikely(buf != _buf))
445 /* Binary tree that maps NTFS inode numbers to DOS names */
446 struct dos_name_map {
447 struct avl_tree_node *root;
450 struct dos_name_node {
451 struct avl_tree_node index_node;
457 #define DOS_NAME_NODE(avl_node) \
458 avl_tree_entry(avl_node, struct dos_name_node, index_node)
461 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
462 const struct avl_tree_node *n2)
464 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
465 DOS_NAME_NODE(n2)->ntfs_ino);
468 /* Inserts a new DOS name into the map */
470 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
471 size_t name_nbytes, u64 ntfs_ino)
473 struct dos_name_node *new_node;
475 new_node = MALLOC(sizeof(struct dos_name_node));
477 return WIMLIB_ERR_NOMEM;
479 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
480 * assuming 2-byte ntfs characters) */
481 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
483 /* Initialize the DOS name, DOS name length, and NTFS inode number of
484 * the search tree node */
485 memcpy(new_node->dos_name, dos_name, name_nbytes);
486 new_node->name_nbytes = name_nbytes;
487 new_node->ntfs_ino = ntfs_ino;
489 /* Insert the search tree node */
490 if (avl_tree_insert(&map->root, &new_node->index_node,
491 _avl_cmp_by_ntfs_ino))
493 /* This should be impossible since an NTFS inode cannot have
494 * multiple DOS names, and we only should get each DOS name
495 * entry once from the ntfs_readdir() calls. */
496 WARNING("NTFS inode %"PRIu64" has multiple DOS names", ntfs_ino);
502 /* Returns a structure that contains the DOS name and its length for an NTFS
503 * inode, or NULL if the inode has no DOS name. */
504 static struct dos_name_node *
505 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
507 struct dos_name_node dummy;
508 struct avl_tree_node *res;
510 dummy.ntfs_ino = ntfs_ino;
512 res = avl_tree_lookup_node(map->root, &dummy.index_node,
513 _avl_cmp_by_ntfs_ino);
516 return DOS_NAME_NODE(res);
520 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
522 const struct dos_name_node *node;
524 if (dentry->d_is_win32_name) {
525 node = lookup_dos_name(map, dentry->d_inode->i_ino);
527 dentry->d_short_name = utf16le_dupz(node->dos_name,
529 if (!dentry->d_short_name)
530 return WIMLIB_ERR_NOMEM;
531 dentry->d_short_name_nbytes = node->name_nbytes;
533 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
534 "corresponding DOS name",
535 dentry->d_inode->i_ino);
542 destroy_dos_name_map(struct dos_name_map *map)
544 struct dos_name_node *node;
545 avl_tree_for_each_in_postorder(node, map->root,
546 struct dos_name_node, index_node)
551 struct wim_dentry *parent;
554 struct dos_name_map dos_name_map;
555 struct ntfs_volume_wrapper *volume;
556 struct capture_params *params;
561 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_p,
566 struct ntfs_volume_wrapper *volume,
567 struct capture_params *params);
570 filldir(void *_ctx, const ntfschar *name, const int name_nchars,
571 const int name_type, const s64 pos, const MFT_REF mref,
572 const unsigned dt_type)
574 struct readdir_ctx *ctx = _ctx;
575 const size_t name_nbytes = name_nchars * sizeof(ntfschar);
577 size_t mbs_name_nbytes;
579 struct wim_dentry *child;
582 if (name_type & FILE_NAME_DOS) {
583 /* If this is the entry for a DOS name, store it for later. */
584 ret = insert_dos_name(&ctx->dos_name_map, name,
585 name_nbytes, MREF(mref));
587 /* Return now if an error occurred or if this is just a DOS name
588 * and not a Win32+DOS name. */
589 if (ret != 0 || name_type == FILE_NAME_DOS)
593 /* Ignore . and .. entries */
595 if ((name_nchars == 1 && name[0] == cpu_to_le16('.')) ||
596 (name_nchars == 2 && name[0] == cpu_to_le16('.') &&
597 name[1] == cpu_to_le16('.')))
600 ret = utf16le_to_tstr(name, name_nbytes, &mbs_name, &mbs_name_nbytes);
604 path_len = ctx->path_len;
606 ctx->path[path_len++] = '/';
607 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
608 path_len += mbs_name_nbytes;
610 ret = ntfs_3g_build_dentry_tree_recursive(&child, mref, ctx->path,
612 ctx->volume, ctx->params);
614 dentry_add_child(ctx->parent, child);
622 ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
623 struct wim_dentry *parent,
624 struct ntfs_volume_wrapper *volume,
625 struct capture_params *params)
629 struct readdir_ctx ctx = {
632 .path_len = path_len,
633 .dos_name_map = { .root = NULL },
638 ret = ntfs_readdir(ni, &pos, &ctx, filldir);
639 path[path_len] = '\0';
645 /* error from ntfs_readdir() itself */
646 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
647 ret = WIMLIB_ERR_NTFS_3G;
650 struct wim_dentry *child;
653 for_dentry_child(child, parent) {
654 ret = set_dentry_dos_name(child, &ctx.dos_name_map);
659 destroy_dos_name_map(&ctx.dos_name_map);
664 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
669 struct ntfs_volume_wrapper *volume,
670 struct capture_params *params)
674 struct wim_dentry *root = NULL;
675 struct wim_inode *inode = NULL;
676 ntfs_inode *ni = NULL;
678 ret = try_exclude(path, path_len, params);
679 if (ret < 0) /* Excluded? */
681 if (ret > 0) /* Error? */
684 ni = ntfs_inode_open(volume->vol, mref);
686 ERROR_WITH_ERRNO("Failed to open NTFS file \"%s\"", path);
687 ret = WIMLIB_ERR_NTFS_3G;
691 /* Get file attributes */
692 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
693 if (ret != sizeof(attributes)) {
694 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
695 ret = WIMLIB_ERR_NTFS_3G;
699 if (unlikely(attributes & FILE_ATTRIBUTE_ENCRYPTED)) {
700 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
702 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
703 "does not support encrypted files and directories", path);
704 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
707 params->progress.scan.cur_path = path;
708 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
712 /* Create a WIM dentry with an associated inode, which may be shared */
713 ret = inode_table_new_dentry(params->inode_table,
714 path_basename_with_len(path, path_len),
715 ni->mft_no, 0, false, &root);
719 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
720 root->d_is_win32_name = 1;
722 inode = root->d_inode;
724 if (inode->i_nlink > 1) {
725 /* Shared inode; nothing more to do */
729 inode->i_creation_time = le64_to_cpu(ni->creation_time);
730 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
731 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
732 inode->i_attributes = attributes;
734 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
735 /* Scan the reparse point stream. */
736 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
737 params->unhashed_blobs,
738 volume, AT_REPARSE_POINT);
743 /* Scan the data streams.
745 * Note: directories should not have an unnamed data stream, but they
746 * may have named data streams. Nondirectories (including reparse
747 * points) can have an unnamed data stream as well as named data
749 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
750 params->unhashed_blobs,
755 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
756 * only allow capturing an entire volume. */
757 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
758 inode_is_symlink(inode))
759 inode->i_rp_flags &= ~WIM_RP_FLAG_NOT_FIXED;
761 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
762 ret = get_security_descriptor(ni, inode, volume->vol,
765 ERROR_WITH_ERRNO("Error reading security descriptor "
771 if (inode_is_directory(inode)) {
772 ret = ntfs_3g_recurse_directory(ni, path, path_len, root,
779 params->progress.scan.cur_path = path;
781 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
783 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
786 ntfs_inode_close(ni);
788 free_dentry_tree(root, params->blob_table);
790 ret = report_capture_error(params, ret, path);
797 ntfs_3g_build_dentry_tree(struct wim_dentry **root_ret,
799 struct capture_params *params)
801 struct ntfs_volume_wrapper *volume;
806 volume = MALLOC(sizeof(struct ntfs_volume_wrapper));
808 return WIMLIB_ERR_NOMEM;
810 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
813 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
814 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers
815 * don't seem to contain any explicit version information. So we have
816 * to rely on a test done at configure time to detect whether
817 * NTFS_MNT_RDONLY should be used. */
818 #ifdef HAVE_NTFS_MNT_RDONLY
820 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
821 #elif defined(MS_RDONLY)
822 /* NTFS-3g 2011, 2012 */
823 vol = ntfs_mount(device, MS_RDONLY);
825 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
828 ERROR_WITH_ERRNO("Failed to mount NTFS volume \"%s\" read-only",
831 return WIMLIB_ERR_NTFS_3G;
837 ntfs_open_secure(vol);
839 /* We don't want to capture the special NTFS files such as $Bitmap. Not
840 * to be confused with "hidden" or "system" files which are real files
841 * that we do need to capture. */
842 NVolClearShowSysFiles(vol);
844 /* Currently we assume that all the paths fit into this length and there
845 * is no check for overflow. */
846 path = MALLOC(32768);
848 ret = WIMLIB_ERR_NOMEM;
849 goto out_put_ntfs_volume;
854 ret = ntfs_3g_build_dentry_tree_recursive(root_ret, FILE_root, path, 1,
855 FILE_NAME_POSIX, volume,
859 ntfs_index_ctx_put(vol->secure_xsii);
860 ntfs_index_ctx_put(vol->secure_xsdh);
861 ntfs_inode_close(vol->secure_ni);
862 put_ntfs_volume(volume);
865 #endif /* WITH_NTFS_3G */