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 /* NTFS-3g 2013 renamed MS_RDONLY to NTFS_MNT_RDONLY. We can't check for the
52 * existence of NTFS_MNT_RDONLY at compilation time because it's an enum. We
53 * also can't check for MS_RDONLY being missing because it's also a system
54 * constant. So check if the NTFS-3g specific MS_IGNORE_HIBERFILE is defined;
55 * if yes, then we need to use the old MS_RDONLY. */
56 #ifdef MS_IGNORE_HIBERFILE
57 # define NTFS_MNT_RDONLY MS_RDONLY
60 /* A reference-counted NTFS volume than is automatically unmounted when the
61 * reference count reaches 0 */
62 struct ntfs_volume_wrapper {
67 /* Description of where data is located in an NTFS volume */
68 struct ntfs_location {
69 struct ntfs_volume_wrapper *volume;
77 static struct ntfs_volume_wrapper *
78 get_ntfs_volume(struct ntfs_volume_wrapper *volume)
85 put_ntfs_volume(struct ntfs_volume_wrapper *volume)
87 if (--volume->refcnt == 0) {
88 ntfs_umount(volume->vol, FALSE);
93 static inline const ntfschar *
94 attr_record_name(const ATTR_RECORD *record)
96 return (const ntfschar *)
97 ((const u8 *)record + le16_to_cpu(record->name_offset));
101 open_ntfs_attr(ntfs_inode *ni, const struct ntfs_location *loc)
105 na = ntfs_attr_open(ni, loc->attr_type, loc->attr_name,
106 loc->attr_name_nchars);
108 ERROR_WITH_ERRNO("Failed to open attribute of NTFS inode %"PRIu64,
115 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
116 const struct read_blob_callbacks *cbs)
118 const struct ntfs_location *loc = blob->ntfs_loc;
119 ntfs_volume *vol = loc->volume->vol;
127 ni = ntfs_inode_open(vol, loc->mft_no);
129 ERROR_WITH_ERRNO("Failed to open NTFS inode %"PRIu64,
131 ret = WIMLIB_ERR_NTFS_3G;
135 na = open_ntfs_attr(ni, loc);
137 ret = WIMLIB_ERR_NTFS_3G;
138 goto out_close_ntfs_inode;
141 pos = (loc->attr_type == AT_REPARSE_POINT) ? REPARSE_DATA_OFFSET : 0;
142 bytes_remaining = size;
143 while (bytes_remaining) {
144 s64 to_read = min(bytes_remaining, sizeof(buf));
145 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
146 ERROR_WITH_ERRNO("Error reading data from NTFS inode "
147 "%"PRIu64, loc->mft_no);
148 ret = WIMLIB_ERR_NTFS_3G;
149 goto out_close_ntfs_attr;
152 bytes_remaining -= to_read;
153 ret = call_consume_chunk(buf, to_read, cbs);
155 goto out_close_ntfs_attr;
160 out_close_ntfs_inode:
161 ntfs_inode_close(ni);
167 free_ntfs_location(struct ntfs_location *loc)
169 put_ntfs_volume(loc->volume);
170 if (loc->attr_name != AT_UNNAMED)
171 FREE(loc->attr_name);
175 struct ntfs_location *
176 clone_ntfs_location(const struct ntfs_location *loc)
178 struct ntfs_location *new = memdup(loc, sizeof(*loc));
181 if (loc->attr_name != AT_UNNAMED) {
182 new->attr_name = utf16le_dup(loc->attr_name);
186 new->volume = get_ntfs_volume(loc->volume);
196 cmp_ntfs_locations(const struct ntfs_location *loc1,
197 const struct ntfs_location *loc2)
199 return cmp_u64(loc1->sort_key, loc2->sort_key);
202 /* Read rptag and rpreserved from the NTFS inode and save them in the WIM inode.
205 read_reparse_header(ntfs_inode *ni, struct wim_inode *inode)
215 na = ntfs_attr_open(ni, AT_REPARSE_POINT, AT_UNNAMED, 0);
217 return WIMLIB_ERR_NTFS_3G;
219 res = ntfs_attr_pread(na, 0, sizeof(hdr), &hdr);
223 if (res != sizeof(hdr))
224 return WIMLIB_ERR_NTFS_3G;
226 inode->i_reparse_tag = le32_to_cpu(hdr.rptag);
227 inode->i_rp_reserved = le16_to_cpu(hdr.rpreserved);
232 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
236 return STREAM_TYPE_DATA;
237 case AT_REPARSE_POINT:
238 return STREAM_TYPE_REPARSE_POINT;
241 return STREAM_TYPE_UNKNOWN;
245 /* When sorting blobs located in NTFS volumes for sequential reading, we sort
246 * first by starting LCN of the attribute if available, otherwise no sort order
247 * is defined. This usually results in better sequential access to the volume.
250 set_attr_sort_key(ntfs_inode *ni, struct ntfs_location *loc)
255 na = open_ntfs_attr(ni, loc);
257 return WIMLIB_ERR_NTFS_3G;
259 rl = ntfs_attr_find_vcn(na, 0);
260 if (rl && rl->lcn != LCN_HOLE)
261 loc->sort_key = rl->lcn;
270 * Add a new stream to the specified inode, with duplicate checking.
272 * This works around a problem where NTFS-3g can list multiple unnamed data
273 * streams for a single file. In this case we can only keep one. We'll prefer
274 * one that is nonempty.
277 add_stream(struct wim_inode *inode, const char *path, int stream_type,
278 const utf16lechar *stream_name, struct blob_descriptor **blob_p,
279 struct list_head *unhashed_blobs)
281 struct blob_descriptor *blob = *blob_p;
282 struct wim_inode_stream *strm;
284 strm = inode_get_stream(inode, stream_type, stream_name);
285 if (unlikely(strm)) {
286 /* Stream already existed. */
289 if (stream_blob_resolved(strm)) {
290 WARNING("\"%s\" has multiple nonempty streams "
291 "with the same type and name! Only the first "
292 "will be saved.", path);
295 inode_replace_stream_blob(inode, strm, blob, NULL);
297 strm = inode_add_stream(inode, stream_type, stream_name, blob);
299 return WIMLIB_ERR_NOMEM;
301 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
307 /* Save information about an NTFS attribute (stream) to a WIM inode. */
309 scan_ntfs_attr(struct wim_inode *inode,
312 struct list_head *unhashed_blobs,
313 struct ntfs_volume_wrapper *volume,
315 const ATTR_RECORD *record)
317 u64 data_size = ntfs_get_attribute_value_length(record);
318 const u32 name_nchars = record->name_length;
319 struct blob_descriptor *blob = NULL;
320 utf16lechar *stream_name = (utf16lechar *)NO_STREAM_NAME;
323 if (unlikely(name_nchars)) {
325 stream_name = utf16le_dupz(attr_record_name(record),
326 name_nchars * sizeof(ntfschar));
328 ret = WIMLIB_ERR_NOMEM;
333 if (unlikely(type == AT_REPARSE_POINT)) {
334 if (data_size < REPARSE_DATA_OFFSET) {
335 ERROR("Reparse point attribute of \"%s\" "
336 "is too short!", path);
337 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
340 data_size -= REPARSE_DATA_OFFSET;
342 ret = read_reparse_header(ni, inode);
344 ERROR_WITH_ERRNO("Error reading reparse point header "
350 /* If the stream is non-empty, set up a blob descriptor for it. */
351 if (data_size != 0) {
352 blob = new_blob_descriptor();
353 if (unlikely(!blob)) {
354 ret = WIMLIB_ERR_NOMEM;
358 blob->ntfs_loc = MALLOC(sizeof(struct ntfs_location));
359 if (unlikely(!blob->ntfs_loc)) {
360 ret = WIMLIB_ERR_NOMEM;
364 blob->blob_location = BLOB_IN_NTFS_VOLUME;
365 blob->size = data_size;
366 blob->ntfs_loc->volume = get_ntfs_volume(volume);
367 blob->ntfs_loc->mft_no = ni->mft_no;
368 blob->ntfs_loc->attr_type = type;
370 if (unlikely(name_nchars)) {
371 blob->ntfs_loc->attr_name_nchars = name_nchars;
372 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
373 if (!blob->ntfs_loc->attr_name) {
374 ret = WIMLIB_ERR_NOMEM;
378 blob->ntfs_loc->attr_name_nchars = 0;
379 blob->ntfs_loc->attr_name = AT_UNNAMED;
382 ret = set_attr_sort_key(ni, blob->ntfs_loc);
387 ret = add_stream(inode, path, attr_type_to_wimlib_stream_type(type),
388 stream_name, &blob, unhashed_blobs);
390 free_blob_descriptor(blob);
391 if (stream_name != NO_STREAM_NAME)
396 /* Scan attributes of the specified type from a file in the NTFS volume */
398 scan_ntfs_attrs_with_type(struct wim_inode *inode,
401 struct list_head *unhashed_blobs,
402 struct ntfs_volume_wrapper *volume,
405 ntfs_attr_search_ctx *actx;
408 actx = ntfs_attr_get_search_ctx(ni, NULL);
410 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
411 "context for \"%s\"", path);
412 return WIMLIB_ERR_NTFS_3G;
415 while (!ntfs_attr_lookup(type, NULL, 0,
416 CASE_SENSITIVE, 0, NULL, 0, actx))
418 ret = scan_ntfs_attr(inode,
428 if (errno != ENOENT) {
429 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
430 ret = WIMLIB_ERR_NTFS_3G;
435 ntfs_attr_put_search_ctx(actx);
439 /* Load the security descriptor of an NTFS inode into the corresponding WIM
440 * inode and the WIM image's security descriptor set. */
441 static noinline_for_stack int
442 get_security_descriptor(ntfs_inode *ni, struct wim_inode *inode,
443 ntfs_volume *vol, struct wim_sd_set *sd_set)
445 struct SECURITY_CONTEXT scx = {.vol = vol};
448 size_t avail_size = sizeof(_buf);
452 ret = ntfs_get_ntfs_acl(&scx, ni, buf, avail_size);
453 if (unlikely(ret < 0)) {
454 ret = WIMLIB_ERR_NTFS_3G;
458 if (unlikely(ret > avail_size)) {
459 if (unlikely(buf != _buf))
463 ret = WIMLIB_ERR_NOMEM;
470 if (likely(ret > 0)) {
471 inode->i_security_id = sd_set_add_sd(sd_set, buf, ret);
472 if (unlikely(inode->i_security_id < 0)) {
473 ret = WIMLIB_ERR_NOMEM;
480 if (unlikely(buf != _buf))
485 /* Binary tree that maps NTFS inode numbers to DOS names */
486 struct dos_name_map {
487 struct avl_tree_node *root;
490 struct dos_name_node {
491 struct avl_tree_node index_node;
497 #define DOS_NAME_NODE(avl_node) \
498 avl_tree_entry(avl_node, struct dos_name_node, index_node)
501 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
502 const struct avl_tree_node *n2)
504 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
505 DOS_NAME_NODE(n2)->ntfs_ino);
508 /* Inserts a new DOS name into the map */
510 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
511 size_t name_nbytes, u64 ntfs_ino)
513 struct dos_name_node *new_node;
515 new_node = MALLOC(sizeof(struct dos_name_node));
517 return WIMLIB_ERR_NOMEM;
519 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
520 * assuming 2-byte ntfs characters) */
521 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
523 /* Initialize the DOS name, DOS name length, and NTFS inode number of
524 * the search tree node */
525 memcpy(new_node->dos_name, dos_name, name_nbytes);
526 new_node->name_nbytes = name_nbytes;
527 new_node->ntfs_ino = ntfs_ino;
529 /* Insert the search tree node */
530 if (avl_tree_insert(&map->root, &new_node->index_node,
531 _avl_cmp_by_ntfs_ino))
533 /* This should be impossible since an NTFS inode cannot have
534 * multiple DOS names, and we only should get each DOS name
535 * entry once from the ntfs_readdir() calls. */
536 WARNING("NTFS inode %"PRIu64" has multiple DOS names", ntfs_ino);
542 /* Returns a structure that contains the DOS name and its length for an NTFS
543 * inode, or NULL if the inode has no DOS name. */
544 static struct dos_name_node *
545 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
547 struct dos_name_node dummy;
548 struct avl_tree_node *res;
550 dummy.ntfs_ino = ntfs_ino;
552 res = avl_tree_lookup_node(map->root, &dummy.index_node,
553 _avl_cmp_by_ntfs_ino);
556 return DOS_NAME_NODE(res);
560 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
562 const struct dos_name_node *node;
564 if (dentry->d_is_win32_name) {
565 node = lookup_dos_name(map, dentry->d_inode->i_ino);
567 dentry->d_short_name = utf16le_dupz(node->dos_name,
569 if (!dentry->d_short_name)
570 return WIMLIB_ERR_NOMEM;
571 dentry->d_short_name_nbytes = node->name_nbytes;
573 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
574 "corresponding DOS name",
575 dentry->d_inode->i_ino);
582 destroy_dos_name_map(struct dos_name_map *map)
584 struct dos_name_node *node;
585 avl_tree_for_each_in_postorder(node, map->root,
586 struct dos_name_node, index_node)
591 struct wim_dentry *parent;
594 struct dos_name_map dos_name_map;
595 struct ntfs_volume_wrapper *volume;
596 struct capture_params *params;
601 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_p,
606 struct ntfs_volume_wrapper *volume,
607 struct capture_params *params);
610 filldir(void *_ctx, const ntfschar *name, const int name_nchars,
611 const int name_type, const s64 pos, const MFT_REF mref,
612 const unsigned dt_type)
614 struct readdir_ctx *ctx = _ctx;
615 const size_t name_nbytes = name_nchars * sizeof(ntfschar);
617 size_t mbs_name_nbytes;
619 struct wim_dentry *child;
622 if (name_type & FILE_NAME_DOS) {
623 /* If this is the entry for a DOS name, store it for later. */
624 ret = insert_dos_name(&ctx->dos_name_map, name,
625 name_nbytes, MREF(mref));
627 /* Return now if an error occurred or if this is just a DOS name
628 * and not a Win32+DOS name. */
629 if (ret != 0 || name_type == FILE_NAME_DOS)
633 ret = utf16le_to_tstr(name, name_nbytes, &mbs_name, &mbs_name_nbytes);
637 if (should_ignore_filename(mbs_name, mbs_name_nbytes))
638 goto out_free_mbs_name;
640 path_len = ctx->path_len;
642 ctx->path[path_len++] = '/';
643 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
644 path_len += mbs_name_nbytes;
646 ret = ntfs_3g_build_dentry_tree_recursive(&child, mref, ctx->path,
648 ctx->volume, ctx->params);
649 attach_scanned_tree(ctx->parent, child, ctx->params->blob_table);
658 ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
659 struct wim_dentry *parent,
660 struct ntfs_volume_wrapper *volume,
661 struct capture_params *params)
665 struct readdir_ctx ctx = {
668 .path_len = path_len,
669 .dos_name_map = { .root = NULL },
674 ret = ntfs_readdir(ni, &pos, &ctx, filldir);
675 path[path_len] = '\0';
681 /* error from ntfs_readdir() itself */
682 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
683 ret = WIMLIB_ERR_NTFS_3G;
686 struct wim_dentry *child;
689 for_dentry_child(child, parent) {
690 ret = set_dentry_dos_name(child, &ctx.dos_name_map);
695 destroy_dos_name_map(&ctx.dos_name_map);
700 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
705 struct ntfs_volume_wrapper *volume,
706 struct capture_params *params)
710 struct wim_dentry *root = NULL;
711 struct wim_inode *inode = NULL;
712 ntfs_inode *ni = NULL;
714 ret = try_exclude(path, params);
715 if (unlikely(ret < 0)) /* Excluded? */
717 if (unlikely(ret > 0)) /* Error? */
720 ni = ntfs_inode_open(volume->vol, mref);
722 ERROR_WITH_ERRNO("Failed to open NTFS file \"%s\"", path);
723 ret = WIMLIB_ERR_NTFS_3G;
727 /* Get file attributes */
728 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
729 if (ret != sizeof(attributes)) {
730 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
731 ret = WIMLIB_ERR_NTFS_3G;
735 if (unlikely(attributes & FILE_ATTRIBUTE_ENCRYPTED)) {
736 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
738 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
739 "does not support encrypted files and directories", path);
740 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
743 params->progress.scan.cur_path = path;
744 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
748 /* Create a WIM dentry with an associated inode, which may be shared */
749 ret = inode_table_new_dentry(params->inode_table,
750 path_basename_with_len(path, path_len),
751 ni->mft_no, 0, false, &root);
755 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
756 root->d_is_win32_name = 1;
758 inode = root->d_inode;
760 if (inode->i_nlink > 1) {
761 /* Shared inode; nothing more to do */
765 inode->i_creation_time = le64_to_cpu(ni->creation_time);
766 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
767 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
768 inode->i_attributes = attributes;
770 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
771 /* Scan the reparse point stream. */
772 ret = scan_ntfs_attrs_with_type(inode, ni, path,
773 params->unhashed_blobs,
774 volume, AT_REPARSE_POINT);
779 /* Scan the data streams.
781 * Note: directories should not have an unnamed data stream, but they
782 * may have named data streams. Nondirectories (including reparse
783 * points) can have an unnamed data stream as well as named data
785 ret = scan_ntfs_attrs_with_type(inode, ni, path, params->unhashed_blobs,
790 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
791 * only allow capturing an entire volume. */
792 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
793 inode_is_symlink(inode))
794 inode->i_rp_flags &= ~WIM_RP_FLAG_NOT_FIXED;
796 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
797 ret = get_security_descriptor(ni, inode, volume->vol,
800 ERROR_WITH_ERRNO("Error reading security descriptor "
806 if (inode_is_directory(inode)) {
807 ret = ntfs_3g_recurse_directory(ni, path, path_len, root,
814 params->progress.scan.cur_path = path;
816 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
818 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
821 ntfs_inode_close(ni);
823 free_dentry_tree(root, params->blob_table);
825 ret = report_capture_error(params, ret, path);
832 ntfs_3g_build_dentry_tree(struct wim_dentry **root_ret,
834 struct capture_params *params)
836 struct ntfs_volume_wrapper *volume;
841 volume = MALLOC(sizeof(struct ntfs_volume_wrapper));
843 return WIMLIB_ERR_NOMEM;
845 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
847 ERROR_WITH_ERRNO("Failed to mount NTFS volume \"%s\" read-only",
850 return WIMLIB_ERR_NTFS_3G;
856 ntfs_open_secure(vol);
858 /* We don't want to capture the special NTFS files such as $Bitmap. Not
859 * to be confused with "hidden" or "system" files which are real files
860 * that we do need to capture. */
861 NVolClearShowSysFiles(vol);
863 /* Currently we assume that all the paths fit into this length and there
864 * is no check for overflow. */
865 path = MALLOC(32768);
867 ret = WIMLIB_ERR_NOMEM;
868 goto out_put_ntfs_volume;
873 ret = ntfs_3g_build_dentry_tree_recursive(root_ret, FILE_root, path, 1,
874 FILE_NAME_POSIX, volume,
878 ntfs_index_ctx_put(vol->secure_xsii);
879 ntfs_index_ctx_put(vol->secure_xsdh);
880 ntfs_inode_close(vol->secure_ni);
881 put_ntfs_volume(volume);
884 #endif /* WITH_NTFS_3G */