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;
73 utf16lechar *attr_name;
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 FREE(loc->attr_name);
174 struct ntfs_location *
175 clone_ntfs_location(const struct ntfs_location *loc)
177 struct ntfs_location *new = memdup(loc, sizeof(*loc));
180 if (loc->attr_name) {
181 new->attr_name = utf16le_dup(loc->attr_name);
185 new->volume = get_ntfs_volume(loc->volume);
195 cmp_ntfs_locations(const struct ntfs_location *loc1,
196 const struct ntfs_location *loc2)
198 return cmp_u64(loc1->sort_key, loc2->sort_key);
201 /* Read rptag and rpreserved from the NTFS inode and save them in the WIM inode.
204 read_reparse_header(ntfs_inode *ni, struct wim_inode *inode)
214 na = ntfs_attr_open(ni, AT_REPARSE_POINT, AT_UNNAMED, 0);
216 return WIMLIB_ERR_NTFS_3G;
218 res = ntfs_attr_pread(na, 0, sizeof(hdr), &hdr);
222 if (res != sizeof(hdr))
223 return WIMLIB_ERR_NTFS_3G;
225 inode->i_reparse_tag = le32_to_cpu(hdr.rptag);
226 inode->i_rp_reserved = le16_to_cpu(hdr.rpreserved);
231 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
235 return STREAM_TYPE_DATA;
236 case AT_REPARSE_POINT:
237 return STREAM_TYPE_REPARSE_POINT;
240 return STREAM_TYPE_UNKNOWN;
244 /* When sorting blobs located in NTFS volumes for sequential reading, we sort
245 * first by starting LCN of the attribute if available, otherwise no sort order
246 * is defined. This usually results in better sequential access to the volume.
249 set_attr_sort_key(ntfs_inode *ni, struct ntfs_location *loc)
254 na = open_ntfs_attr(ni, loc);
256 return WIMLIB_ERR_NTFS_3G;
258 rl = ntfs_attr_find_vcn(na, 0);
259 if (rl && rl->lcn != LCN_HOLE)
260 loc->sort_key = rl->lcn;
269 * Add a new stream to the specified inode, with duplicate checking.
271 * This works around a problem where NTFS-3g can list multiple unnamed data
272 * streams for a single file. In this case we can only keep one. We'll prefer
273 * one that is nonempty.
276 add_stream(struct wim_inode *inode, const char *path, int stream_type,
277 const utf16lechar *stream_name, struct blob_descriptor **blob_p,
278 struct list_head *unhashed_blobs)
280 struct blob_descriptor *blob = *blob_p;
281 struct wim_inode_stream *strm;
283 strm = inode_get_stream(inode, stream_type, stream_name);
284 if (unlikely(strm)) {
285 /* Stream already existed. */
288 if (stream_blob_resolved(strm)) {
289 WARNING("\"%s\" has multiple nonempty streams "
290 "with the same type and name! Only the first "
291 "will be saved.", path);
294 inode_replace_stream_blob(inode, strm, blob, NULL);
296 strm = inode_add_stream(inode, stream_type, stream_name, blob);
298 return WIMLIB_ERR_NOMEM;
300 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
306 /* Save information about an NTFS attribute (stream) to a WIM inode. */
308 scan_ntfs_attr(struct wim_inode *inode,
311 struct list_head *unhashed_blobs,
312 struct ntfs_volume_wrapper *volume,
314 const ATTR_RECORD *record)
316 u64 data_size = ntfs_get_attribute_value_length(record);
317 const u32 name_nchars = record->name_length;
318 struct blob_descriptor *blob = NULL;
319 utf16lechar *stream_name = NULL;
322 if (unlikely(name_nchars)) {
324 stream_name = utf16le_dupz(attr_record_name(record),
325 name_nchars * sizeof(ntfschar));
327 ret = WIMLIB_ERR_NOMEM;
332 if (unlikely(type == AT_REPARSE_POINT)) {
333 if (data_size < REPARSE_DATA_OFFSET) {
334 ERROR("Reparse point attribute of \"%s\" "
335 "is too short!", path);
336 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
339 data_size -= REPARSE_DATA_OFFSET;
341 ret = read_reparse_header(ni, inode);
343 ERROR_WITH_ERRNO("Error reading reparse point header "
349 /* If the stream is non-empty, set up a blob descriptor for it. */
350 if (data_size != 0) {
351 blob = new_blob_descriptor();
352 if (unlikely(!blob)) {
353 ret = WIMLIB_ERR_NOMEM;
357 blob->ntfs_loc = CALLOC(1, sizeof(struct ntfs_location));
358 if (unlikely(!blob->ntfs_loc)) {
359 ret = WIMLIB_ERR_NOMEM;
363 blob->blob_location = BLOB_IN_NTFS_VOLUME;
364 blob->size = data_size;
365 blob->ntfs_loc->volume = get_ntfs_volume(volume);
366 blob->ntfs_loc->attr_type = type;
367 blob->ntfs_loc->mft_no = ni->mft_no;
369 if (unlikely(name_nchars)) {
370 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
371 if (!blob->ntfs_loc->attr_name) {
372 ret = WIMLIB_ERR_NOMEM;
375 blob->ntfs_loc->attr_name_nchars = name_nchars;
378 ret = set_attr_sort_key(ni, blob->ntfs_loc);
383 ret = add_stream(inode, path, attr_type_to_wimlib_stream_type(type),
384 stream_name ? stream_name : NO_STREAM_NAME,
385 &blob, unhashed_blobs);
387 free_blob_descriptor(blob);
392 /* Scan attributes of the specified type from a file in the NTFS volume */
394 scan_ntfs_attrs_with_type(struct wim_inode *inode,
397 struct list_head *unhashed_blobs,
398 struct ntfs_volume_wrapper *volume,
401 ntfs_attr_search_ctx *actx;
404 actx = ntfs_attr_get_search_ctx(ni, NULL);
406 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
407 "context for \"%s\"", path);
408 return WIMLIB_ERR_NTFS_3G;
411 while (!ntfs_attr_lookup(type, NULL, 0,
412 CASE_SENSITIVE, 0, NULL, 0, actx))
414 ret = scan_ntfs_attr(inode,
424 if (errno != ENOENT) {
425 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
426 ret = WIMLIB_ERR_NTFS_3G;
431 ntfs_attr_put_search_ctx(actx);
435 /* Load the security descriptor of an NTFS inode into the corresponding WIM
436 * inode and the WIM image's security descriptor set. */
437 static noinline_for_stack int
438 get_security_descriptor(ntfs_inode *ni, struct wim_inode *inode,
439 ntfs_volume *vol, struct wim_sd_set *sd_set)
441 struct SECURITY_CONTEXT scx = {.vol = vol};
444 size_t avail_size = sizeof(_buf);
448 ret = ntfs_get_ntfs_acl(&scx, ni, buf, avail_size);
449 if (unlikely(ret < 0)) {
450 ret = WIMLIB_ERR_NTFS_3G;
454 if (unlikely(ret > avail_size)) {
455 if (unlikely(buf != _buf))
459 ret = WIMLIB_ERR_NOMEM;
466 if (likely(ret > 0)) {
467 inode->i_security_id = sd_set_add_sd(sd_set, buf, ret);
468 if (unlikely(inode->i_security_id < 0)) {
469 ret = WIMLIB_ERR_NOMEM;
476 if (unlikely(buf != _buf))
481 /* Binary tree that maps NTFS inode numbers to DOS names */
482 struct dos_name_map {
483 struct avl_tree_node *root;
486 struct dos_name_node {
487 struct avl_tree_node index_node;
493 #define DOS_NAME_NODE(avl_node) \
494 avl_tree_entry(avl_node, struct dos_name_node, index_node)
497 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
498 const struct avl_tree_node *n2)
500 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
501 DOS_NAME_NODE(n2)->ntfs_ino);
504 /* Inserts a new DOS name into the map */
506 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
507 size_t name_nbytes, u64 ntfs_ino)
509 struct dos_name_node *new_node;
511 new_node = MALLOC(sizeof(struct dos_name_node));
513 return WIMLIB_ERR_NOMEM;
515 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
516 * assuming 2-byte ntfs characters) */
517 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
519 /* Initialize the DOS name, DOS name length, and NTFS inode number of
520 * the search tree node */
521 memcpy(new_node->dos_name, dos_name, name_nbytes);
522 new_node->name_nbytes = name_nbytes;
523 new_node->ntfs_ino = ntfs_ino;
525 /* Insert the search tree node */
526 if (avl_tree_insert(&map->root, &new_node->index_node,
527 _avl_cmp_by_ntfs_ino))
529 /* This should be impossible since an NTFS inode cannot have
530 * multiple DOS names, and we only should get each DOS name
531 * entry once from the ntfs_readdir() calls. */
532 WARNING("NTFS inode %"PRIu64" has multiple DOS names", ntfs_ino);
538 /* Returns a structure that contains the DOS name and its length for an NTFS
539 * inode, or NULL if the inode has no DOS name. */
540 static struct dos_name_node *
541 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
543 struct dos_name_node dummy;
544 struct avl_tree_node *res;
546 dummy.ntfs_ino = ntfs_ino;
548 res = avl_tree_lookup_node(map->root, &dummy.index_node,
549 _avl_cmp_by_ntfs_ino);
552 return DOS_NAME_NODE(res);
556 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
558 const struct dos_name_node *node;
560 if (dentry->d_is_win32_name) {
561 node = lookup_dos_name(map, dentry->d_inode->i_ino);
563 dentry->d_short_name = utf16le_dupz(node->dos_name,
565 if (!dentry->d_short_name)
566 return WIMLIB_ERR_NOMEM;
567 dentry->d_short_name_nbytes = node->name_nbytes;
569 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
570 "corresponding DOS name",
571 dentry->d_inode->i_ino);
578 destroy_dos_name_map(struct dos_name_map *map)
580 struct dos_name_node *node;
581 avl_tree_for_each_in_postorder(node, map->root,
582 struct dos_name_node, index_node)
587 struct wim_dentry *parent;
590 struct dos_name_map dos_name_map;
591 struct ntfs_volume_wrapper *volume;
592 struct capture_params *params;
597 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_p,
602 struct ntfs_volume_wrapper *volume,
603 struct capture_params *params);
606 filldir(void *_ctx, const ntfschar *name, const int name_nchars,
607 const int name_type, const s64 pos, const MFT_REF mref,
608 const unsigned dt_type)
610 struct readdir_ctx *ctx = _ctx;
611 const size_t name_nbytes = name_nchars * sizeof(ntfschar);
613 size_t mbs_name_nbytes;
615 struct wim_dentry *child;
618 if (name_type & FILE_NAME_DOS) {
619 /* If this is the entry for a DOS name, store it for later. */
620 ret = insert_dos_name(&ctx->dos_name_map, name,
621 name_nbytes, MREF(mref));
623 /* Return now if an error occurred or if this is just a DOS name
624 * and not a Win32+DOS name. */
625 if (ret != 0 || name_type == FILE_NAME_DOS)
629 ret = utf16le_to_tstr(name, name_nbytes, &mbs_name, &mbs_name_nbytes);
633 if (should_ignore_filename(mbs_name, mbs_name_nbytes))
634 goto out_free_mbs_name;
636 path_len = ctx->path_len;
638 ctx->path[path_len++] = '/';
639 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
640 path_len += mbs_name_nbytes;
642 ret = ntfs_3g_build_dentry_tree_recursive(&child, mref, ctx->path,
644 ctx->volume, ctx->params);
645 attach_scanned_tree(ctx->parent, child, ctx->params->blob_table);
654 ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
655 struct wim_dentry *parent,
656 struct ntfs_volume_wrapper *volume,
657 struct capture_params *params)
661 struct readdir_ctx ctx = {
664 .path_len = path_len,
665 .dos_name_map = { .root = NULL },
670 ret = ntfs_readdir(ni, &pos, &ctx, filldir);
671 path[path_len] = '\0';
677 /* error from ntfs_readdir() itself */
678 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
679 ret = WIMLIB_ERR_NTFS_3G;
682 struct wim_dentry *child;
685 for_dentry_child(child, parent) {
686 ret = set_dentry_dos_name(child, &ctx.dos_name_map);
691 destroy_dos_name_map(&ctx.dos_name_map);
696 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
701 struct ntfs_volume_wrapper *volume,
702 struct capture_params *params)
706 struct wim_dentry *root = NULL;
707 struct wim_inode *inode = NULL;
708 ntfs_inode *ni = NULL;
710 ret = try_exclude(path, params);
711 if (unlikely(ret < 0)) /* Excluded? */
713 if (unlikely(ret > 0)) /* Error? */
716 ni = ntfs_inode_open(volume->vol, mref);
718 ERROR_WITH_ERRNO("Failed to open NTFS file \"%s\"", path);
719 ret = WIMLIB_ERR_NTFS_3G;
723 /* Get file attributes */
724 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
725 if (ret != sizeof(attributes)) {
726 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
727 ret = WIMLIB_ERR_NTFS_3G;
731 if (unlikely(attributes & FILE_ATTRIBUTE_ENCRYPTED)) {
732 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
734 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
735 "does not support encrypted files and directories", path);
736 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
739 params->progress.scan.cur_path = path;
740 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
744 /* Create a WIM dentry with an associated inode, which may be shared */
745 ret = inode_table_new_dentry(params->inode_table,
746 path_basename_with_len(path, path_len),
747 ni->mft_no, 0, false, &root);
751 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
752 root->d_is_win32_name = 1;
754 inode = root->d_inode;
756 if (inode->i_nlink > 1) {
757 /* Shared inode; nothing more to do */
761 inode->i_creation_time = le64_to_cpu(ni->creation_time);
762 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
763 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
764 inode->i_attributes = attributes;
766 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
767 /* Scan the reparse point stream. */
768 ret = scan_ntfs_attrs_with_type(inode, ni, path,
769 params->unhashed_blobs,
770 volume, AT_REPARSE_POINT);
775 /* Scan the data streams.
777 * Note: directories should not have an unnamed data stream, but they
778 * may have named data streams. Nondirectories (including reparse
779 * points) can have an unnamed data stream as well as named data
781 ret = scan_ntfs_attrs_with_type(inode, ni, path, params->unhashed_blobs,
786 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
787 * only allow capturing an entire volume. */
788 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
789 inode_is_symlink(inode))
790 inode->i_rp_flags &= ~WIM_RP_FLAG_NOT_FIXED;
792 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
793 ret = get_security_descriptor(ni, inode, volume->vol,
796 ERROR_WITH_ERRNO("Error reading security descriptor "
802 if (inode_is_directory(inode)) {
803 ret = ntfs_3g_recurse_directory(ni, path, path_len, root,
810 params->progress.scan.cur_path = path;
812 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
814 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
817 ntfs_inode_close(ni);
819 free_dentry_tree(root, params->blob_table);
821 ret = report_capture_error(params, ret, path);
828 ntfs_3g_build_dentry_tree(struct wim_dentry **root_ret,
830 struct capture_params *params)
832 struct ntfs_volume_wrapper *volume;
837 volume = MALLOC(sizeof(struct ntfs_volume_wrapper));
839 return WIMLIB_ERR_NOMEM;
841 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
843 ERROR_WITH_ERRNO("Failed to mount NTFS volume \"%s\" read-only",
846 return WIMLIB_ERR_NTFS_3G;
852 ntfs_open_secure(vol);
854 /* We don't want to capture the special NTFS files such as $Bitmap. Not
855 * to be confused with "hidden" or "system" files which are real files
856 * that we do need to capture. */
857 NVolClearShowSysFiles(vol);
859 /* Currently we assume that all the paths fit into this length and there
860 * is no check for overflow. */
861 path = MALLOC(32768);
863 ret = WIMLIB_ERR_NOMEM;
864 goto out_put_ntfs_volume;
869 ret = ntfs_3g_build_dentry_tree_recursive(root_ret, FILE_root, path, 1,
870 FILE_NAME_POSIX, volume,
874 ntfs_index_ctx_put(vol->secure_xsii);
875 ntfs_index_ctx_put(vol->secure_xsdh);
876 ntfs_inode_close(vol->secure_ni);
877 put_ntfs_volume(volume);
880 #endif /* WITH_NTFS_3G */