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-2016 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/compat.h> /* for ENODATA, if needed */
35 #include <ntfs-3g/object_id.h>
36 #include <ntfs-3g/reparse.h>
37 #include <ntfs-3g/security.h>
38 #include <ntfs-3g/volume.h>
40 #include "wimlib/alloca.h"
41 #include "wimlib/assert.h"
42 #include "wimlib/blob_table.h"
43 #include "wimlib/dentry.h"
44 #include "wimlib/encoding.h"
45 #include "wimlib/endianness.h"
46 #include "wimlib/error.h"
47 #include "wimlib/ntfs_3g.h"
48 #include "wimlib/object_id.h"
49 #include "wimlib/paths.h"
50 #include "wimlib/reparse.h"
51 #include "wimlib/scan.h"
52 #include "wimlib/security.h"
54 /* NTFS-3G 2013 renamed MS_RDONLY to NTFS_MNT_RDONLY. We can't check for the
55 * existence of NTFS_MNT_RDONLY at compilation time because it's an enum. We
56 * also can't check for MS_RDONLY being missing because it's also a system
57 * constant. So check if the NTFS-3G specific MS_IGNORE_HIBERFILE is defined;
58 * if yes, then we need to use the old MS_RDONLY. */
59 #ifdef MS_IGNORE_HIBERFILE
60 # define NTFS_MNT_RDONLY MS_RDONLY
63 /* A reference-counted NTFS volume than is automatically unmounted when the
64 * reference count reaches 0 */
65 struct ntfs_volume_wrapper {
70 /* Description of where data is located in an NTFS volume */
71 struct ntfs_location {
72 struct ntfs_volume_wrapper *volume;
80 static struct ntfs_volume_wrapper *
81 get_ntfs_volume(struct ntfs_volume_wrapper *volume)
88 put_ntfs_volume(struct ntfs_volume_wrapper *volume)
90 if (--volume->refcnt == 0) {
91 ntfs_umount(volume->vol, FALSE);
96 static inline const ntfschar *
97 attr_record_name(const ATTR_RECORD *record)
99 return (const ntfschar *)
100 ((const u8 *)record + le16_to_cpu(record->name_offset));
104 open_ntfs_attr(ntfs_inode *ni, const struct ntfs_location *loc)
108 na = ntfs_attr_open(ni, loc->attr_type, loc->attr_name,
109 loc->attr_name_nchars);
111 ERROR_WITH_ERRNO("Failed to open attribute of NTFS inode %"PRIu64,
118 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
119 const struct read_blob_callbacks *cbs)
121 const struct ntfs_location *loc = blob->ntfs_loc;
122 ntfs_volume *vol = loc->volume->vol;
130 ni = ntfs_inode_open(vol, loc->mft_no);
132 ERROR_WITH_ERRNO("Failed to open NTFS inode %"PRIu64,
134 ret = WIMLIB_ERR_NTFS_3G;
138 na = open_ntfs_attr(ni, loc);
140 ret = WIMLIB_ERR_NTFS_3G;
141 goto out_close_ntfs_inode;
144 pos = (loc->attr_type == AT_REPARSE_POINT) ? REPARSE_DATA_OFFSET : 0;
145 bytes_remaining = size;
146 while (bytes_remaining) {
147 s64 to_read = min(bytes_remaining, sizeof(buf));
148 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
149 ERROR_WITH_ERRNO("Error reading data from NTFS inode "
150 "%"PRIu64, loc->mft_no);
151 ret = WIMLIB_ERR_NTFS_3G;
152 goto out_close_ntfs_attr;
155 bytes_remaining -= to_read;
156 ret = call_consume_chunk(buf, to_read, cbs);
158 goto out_close_ntfs_attr;
163 out_close_ntfs_inode:
164 ntfs_inode_close(ni);
170 free_ntfs_location(struct ntfs_location *loc)
172 put_ntfs_volume(loc->volume);
173 if (loc->attr_name != AT_UNNAMED)
174 FREE(loc->attr_name);
178 struct ntfs_location *
179 clone_ntfs_location(const struct ntfs_location *loc)
181 struct ntfs_location *new = memdup(loc, sizeof(*loc));
184 if (loc->attr_name != AT_UNNAMED) {
185 new->attr_name = utf16le_dup(loc->attr_name);
189 new->volume = get_ntfs_volume(loc->volume);
199 cmp_ntfs_locations(const struct ntfs_location *loc1,
200 const struct ntfs_location *loc2)
202 return cmp_u64(loc1->sort_key, loc2->sort_key);
205 /* Read rptag and rpreserved from the NTFS inode and save them in the WIM inode.
208 read_reparse_header(ntfs_inode *ni, struct wim_inode *inode)
218 na = ntfs_attr_open(ni, AT_REPARSE_POINT, AT_UNNAMED, 0);
220 return WIMLIB_ERR_NTFS_3G;
222 res = ntfs_attr_pread(na, 0, sizeof(hdr), &hdr);
226 if (res != sizeof(hdr))
227 return WIMLIB_ERR_NTFS_3G;
229 inode->i_reparse_tag = le32_to_cpu(hdr.rptag);
230 inode->i_rp_reserved = le16_to_cpu(hdr.rpreserved);
235 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
239 return STREAM_TYPE_DATA;
240 case AT_REPARSE_POINT:
241 return STREAM_TYPE_REPARSE_POINT;
244 return STREAM_TYPE_UNKNOWN;
248 /* When sorting blobs located in NTFS volumes for sequential reading, we sort
249 * first by starting LCN of the attribute if available, otherwise no sort order
250 * is defined. This usually results in better sequential access to the volume.
253 set_attr_sort_key(ntfs_inode *ni, struct ntfs_location *loc)
258 na = open_ntfs_attr(ni, loc);
260 return WIMLIB_ERR_NTFS_3G;
262 rl = ntfs_attr_find_vcn(na, 0);
263 if (rl && rl->lcn != LCN_HOLE)
264 loc->sort_key = rl->lcn;
273 * Add a new stream to the specified inode, with duplicate checking.
275 * This works around a problem where NTFS-3G can list multiple unnamed data
276 * streams for a single file. In this case we can only keep one. We'll prefer
277 * one that is nonempty.
280 add_stream(struct wim_inode *inode, const char *path, int stream_type,
281 const utf16lechar *stream_name, struct blob_descriptor **blob_p,
282 struct list_head *unhashed_blobs)
284 struct blob_descriptor *blob = *blob_p;
285 struct wim_inode_stream *strm;
287 strm = inode_get_stream(inode, stream_type, stream_name);
288 if (unlikely(strm)) {
289 /* Stream already existed. */
292 if (stream_blob_resolved(strm)) {
293 WARNING("\"%s\" has multiple nonempty streams "
294 "with the same type and name! Only the first "
295 "will be saved.", path);
298 inode_replace_stream_blob(inode, strm, blob, NULL);
300 strm = inode_add_stream(inode, stream_type, stream_name, blob);
302 return WIMLIB_ERR_NOMEM;
304 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
310 /* Save information about an NTFS attribute (stream) to a WIM inode. */
312 scan_ntfs_attr(struct wim_inode *inode,
315 struct list_head *unhashed_blobs,
316 struct ntfs_volume_wrapper *volume,
318 const ATTR_RECORD *record)
320 u64 data_size = ntfs_get_attribute_value_length(record);
321 const u32 name_nchars = record->name_length;
322 struct blob_descriptor *blob = NULL;
323 utf16lechar *stream_name = (utf16lechar *)NO_STREAM_NAME;
326 if (unlikely(name_nchars)) {
328 stream_name = utf16le_dupz(attr_record_name(record),
329 name_nchars * sizeof(ntfschar));
331 ret = WIMLIB_ERR_NOMEM;
336 if (unlikely(type == AT_REPARSE_POINT)) {
337 if (data_size < REPARSE_DATA_OFFSET) {
338 ERROR("Reparse point attribute of \"%s\" "
339 "is too short!", path);
340 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
343 data_size -= REPARSE_DATA_OFFSET;
345 ret = read_reparse_header(ni, inode);
347 ERROR_WITH_ERRNO("Error reading reparse point header "
353 /* If the stream is non-empty, set up a blob descriptor for it. */
354 if (data_size != 0) {
355 blob = new_blob_descriptor();
356 if (unlikely(!blob)) {
357 ret = WIMLIB_ERR_NOMEM;
361 blob->ntfs_loc = MALLOC(sizeof(struct ntfs_location));
362 if (unlikely(!blob->ntfs_loc)) {
363 ret = WIMLIB_ERR_NOMEM;
367 blob->blob_location = BLOB_IN_NTFS_VOLUME;
368 blob->size = data_size;
369 blob->ntfs_loc->volume = get_ntfs_volume(volume);
370 blob->ntfs_loc->mft_no = ni->mft_no;
371 blob->ntfs_loc->attr_type = type;
373 if (unlikely(name_nchars)) {
374 blob->ntfs_loc->attr_name_nchars = name_nchars;
375 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
376 if (!blob->ntfs_loc->attr_name) {
377 ret = WIMLIB_ERR_NOMEM;
381 blob->ntfs_loc->attr_name_nchars = 0;
382 blob->ntfs_loc->attr_name = AT_UNNAMED;
385 ret = set_attr_sort_key(ni, blob->ntfs_loc);
390 ret = add_stream(inode, path, attr_type_to_wimlib_stream_type(type),
391 stream_name, &blob, unhashed_blobs);
393 free_blob_descriptor(blob);
394 if (stream_name != NO_STREAM_NAME)
399 /* Scan attributes of the specified type from a file in the NTFS volume */
401 scan_ntfs_attrs_with_type(struct wim_inode *inode,
404 struct list_head *unhashed_blobs,
405 struct ntfs_volume_wrapper *volume,
408 ntfs_attr_search_ctx *actx;
411 actx = ntfs_attr_get_search_ctx(ni, NULL);
413 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
414 "context for \"%s\"", path);
415 return WIMLIB_ERR_NTFS_3G;
418 while (!ntfs_attr_lookup(type, NULL, 0,
419 CASE_SENSITIVE, 0, NULL, 0, actx))
421 ret = scan_ntfs_attr(inode,
431 if (errno != ENOENT) {
432 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
433 ret = WIMLIB_ERR_NTFS_3G;
438 ntfs_attr_put_search_ctx(actx);
442 static noinline_for_stack int
443 load_object_id(ntfs_inode *ni, struct wim_inode *inode)
448 len = ntfs_get_ntfs_object_id(ni, (char *)&attr, sizeof(attr));
449 if (likely(len == -ENODATA || len == 0))
452 return WIMLIB_ERR_NTFS_3G;
453 if (!inode_set_object_id(inode, &attr, len))
454 return WIMLIB_ERR_NOMEM;
458 /* Load the security descriptor of an NTFS inode into the corresponding WIM
459 * inode and the WIM image's security descriptor set. */
460 static noinline_for_stack int
461 get_security_descriptor(ntfs_inode *ni, struct wim_inode *inode,
462 ntfs_volume *vol, struct wim_sd_set *sd_set)
464 struct SECURITY_CONTEXT scx = {.vol = vol};
467 size_t avail_size = sizeof(_buf);
471 ret = ntfs_get_ntfs_acl(&scx, ni, buf, avail_size);
472 if (unlikely(ret < 0)) {
473 ret = WIMLIB_ERR_NTFS_3G;
477 if (unlikely(ret > avail_size)) {
478 if (unlikely(buf != _buf))
482 ret = WIMLIB_ERR_NOMEM;
489 if (likely(ret > 0)) {
490 inode->i_security_id = sd_set_add_sd(sd_set, buf, ret);
491 if (unlikely(inode->i_security_id < 0)) {
492 ret = WIMLIB_ERR_NOMEM;
499 if (unlikely(buf != _buf))
504 /* Binary tree that maps NTFS inode numbers to DOS names */
505 struct dos_name_map {
506 struct avl_tree_node *root;
509 struct dos_name_node {
510 struct avl_tree_node index_node;
516 #define DOS_NAME_NODE(avl_node) \
517 avl_tree_entry(avl_node, struct dos_name_node, index_node)
520 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
521 const struct avl_tree_node *n2)
523 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
524 DOS_NAME_NODE(n2)->ntfs_ino);
527 /* Inserts a new DOS name into the map */
529 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
530 size_t name_nbytes, u64 ntfs_ino)
532 struct dos_name_node *new_node;
534 new_node = MALLOC(sizeof(struct dos_name_node));
536 return WIMLIB_ERR_NOMEM;
538 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
539 * assuming 2-byte ntfs characters) */
540 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
542 /* Initialize the DOS name, DOS name length, and NTFS inode number of
543 * the search tree node */
544 memcpy(new_node->dos_name, dos_name, name_nbytes);
545 new_node->name_nbytes = name_nbytes;
546 new_node->ntfs_ino = ntfs_ino;
548 /* Insert the search tree node */
549 if (avl_tree_insert(&map->root, &new_node->index_node,
550 _avl_cmp_by_ntfs_ino))
552 /* This should be impossible since an NTFS inode cannot have
553 * multiple DOS names, and we only should get each DOS name
554 * entry once from the ntfs_readdir() calls. */
555 WARNING("NTFS inode %"PRIu64" has multiple DOS names", ntfs_ino);
561 /* Returns a structure that contains the DOS name and its length for an NTFS
562 * inode, or NULL if the inode has no DOS name. */
563 static struct dos_name_node *
564 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
566 struct dos_name_node dummy;
567 struct avl_tree_node *res;
569 dummy.ntfs_ino = ntfs_ino;
571 res = avl_tree_lookup_node(map->root, &dummy.index_node,
572 _avl_cmp_by_ntfs_ino);
575 return DOS_NAME_NODE(res);
579 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
581 const struct dos_name_node *node;
583 if (dentry->d_is_win32_name) {
584 node = lookup_dos_name(map, dentry->d_inode->i_ino);
586 dentry->d_short_name = utf16le_dupz(node->dos_name,
588 if (!dentry->d_short_name)
589 return WIMLIB_ERR_NOMEM;
590 dentry->d_short_name_nbytes = node->name_nbytes;
592 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
593 "corresponding DOS name",
594 dentry->d_inode->i_ino);
601 destroy_dos_name_map(struct dos_name_map *map)
603 struct dos_name_node *node;
604 avl_tree_for_each_in_postorder(node, map->root,
605 struct dos_name_node, index_node)
610 struct wim_dentry *parent;
613 struct dos_name_map dos_name_map;
614 struct ntfs_volume_wrapper *volume;
615 struct scan_params *params;
620 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_p,
625 struct ntfs_volume_wrapper *volume,
626 struct scan_params *params);
629 filldir(void *_ctx, const ntfschar *name, const int name_nchars,
630 const int name_type, const s64 pos, const MFT_REF mref,
631 const unsigned dt_type)
633 struct readdir_ctx *ctx = _ctx;
634 const size_t name_nbytes = name_nchars * sizeof(ntfschar);
636 size_t mbs_name_nbytes;
638 struct wim_dentry *child;
641 if (name_type & FILE_NAME_DOS) {
642 /* If this is the entry for a DOS name, store it for later. */
643 ret = insert_dos_name(&ctx->dos_name_map, name,
644 name_nbytes, MREF(mref));
646 /* Return now if an error occurred or if this is just a DOS name
647 * and not a Win32+DOS name. */
648 if (ret != 0 || name_type == FILE_NAME_DOS)
652 ret = utf16le_to_tstr(name, name_nbytes, &mbs_name, &mbs_name_nbytes);
656 if (should_ignore_filename(mbs_name, mbs_name_nbytes))
657 goto out_free_mbs_name;
659 path_len = ctx->path_len;
661 ctx->path[path_len++] = '/';
662 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
663 path_len += mbs_name_nbytes;
665 ret = ntfs_3g_build_dentry_tree_recursive(&child, mref, ctx->path,
667 ctx->volume, ctx->params);
668 attach_scanned_tree(ctx->parent, child, ctx->params->blob_table);
677 ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
678 struct wim_dentry *parent,
679 struct ntfs_volume_wrapper *volume,
680 struct scan_params *params)
684 struct readdir_ctx ctx = {
687 .path_len = path_len,
688 .dos_name_map = { .root = NULL },
693 ret = ntfs_readdir(ni, &pos, &ctx, filldir);
694 path[path_len] = '\0';
700 /* error from ntfs_readdir() itself */
701 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
702 ret = WIMLIB_ERR_NTFS_3G;
705 struct wim_dentry *child;
708 for_dentry_child(child, parent) {
709 ret = set_dentry_dos_name(child, &ctx.dos_name_map);
714 destroy_dos_name_map(&ctx.dos_name_map);
719 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
724 struct ntfs_volume_wrapper *volume,
725 struct scan_params *params)
729 struct wim_dentry *root = NULL;
730 struct wim_inode *inode = NULL;
731 ntfs_inode *ni = NULL;
733 ret = try_exclude(path, params);
734 if (unlikely(ret < 0)) /* Excluded? */
736 if (unlikely(ret > 0)) /* Error? */
739 ni = ntfs_inode_open(volume->vol, mref);
741 ERROR_WITH_ERRNO("Failed to open NTFS file \"%s\"", path);
742 ret = WIMLIB_ERR_NTFS_3G;
746 /* Get file attributes */
747 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
748 if (ret != sizeof(attributes)) {
749 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
750 ret = WIMLIB_ERR_NTFS_3G;
754 if (unlikely(attributes & FILE_ATTRIBUTE_ENCRYPTED)) {
755 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
757 ERROR("Can't archive \"%s\" because NTFS-3G capture mode "
758 "does not support encrypted files and directories", path);
759 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
762 params->progress.scan.cur_path = path;
763 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
767 /* Create a WIM dentry with an associated inode, which may be shared */
768 ret = inode_table_new_dentry(params->inode_table,
769 path_basename_with_len(path, path_len),
770 ni->mft_no, 0, false, &root);
774 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
775 root->d_is_win32_name = 1;
777 inode = root->d_inode;
779 if (inode->i_nlink > 1) {
780 /* Shared inode; nothing more to do */
784 inode->i_creation_time = le64_to_cpu(ni->creation_time);
785 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
786 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
787 inode->i_attributes = attributes;
789 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
790 /* Scan the reparse point stream. */
791 ret = scan_ntfs_attrs_with_type(inode, ni, path,
792 params->unhashed_blobs,
793 volume, AT_REPARSE_POINT);
798 /* Load the object ID. */
799 ret = load_object_id(ni, inode);
801 ERROR_WITH_ERRNO("Error reading object ID of \"%s\"", path);
805 /* Scan the data streams.
807 * Note: directories should not have an unnamed data stream, but they
808 * may have named data streams. Nondirectories (including reparse
809 * points) can have an unnamed data stream as well as named data
811 ret = scan_ntfs_attrs_with_type(inode, ni, path, params->unhashed_blobs,
816 /* Reparse-point fixups are a no-op because in NTFS-3G capture mode we
817 * only allow capturing an entire volume. */
818 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
819 inode_is_symlink(inode))
820 inode->i_rp_flags &= ~WIM_RP_FLAG_NOT_FIXED;
822 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
823 ret = get_security_descriptor(ni, inode, volume->vol,
826 ERROR_WITH_ERRNO("Error reading security descriptor "
832 if (inode_is_directory(inode)) {
833 ret = ntfs_3g_recurse_directory(ni, path, path_len, root,
840 params->progress.scan.cur_path = path;
842 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
844 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
847 ntfs_inode_close(ni);
849 free_dentry_tree(root, params->blob_table);
851 ret = report_scan_error(params, ret, path);
858 ntfs_3g_build_dentry_tree(struct wim_dentry **root_ret,
859 const char *device, struct scan_params *params)
861 struct ntfs_volume_wrapper *volume;
866 volume = MALLOC(sizeof(struct ntfs_volume_wrapper));
868 return WIMLIB_ERR_NOMEM;
870 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
872 ERROR_WITH_ERRNO("Failed to mount NTFS volume \"%s\" read-only",
875 return WIMLIB_ERR_NTFS_3G;
881 ntfs_open_secure(vol);
883 /* We don't want to capture the special NTFS files such as $Bitmap. Not
884 * to be confused with "hidden" or "system" files which are real files
885 * that we do need to capture. */
886 NVolClearShowSysFiles(vol);
888 /* Currently we assume that all the paths fit into this length and there
889 * is no check for overflow. */
890 path = MALLOC(32768);
892 ret = WIMLIB_ERR_NOMEM;
893 goto out_put_ntfs_volume;
898 ret = ntfs_3g_build_dentry_tree_recursive(root_ret, FILE_root, path, 1,
899 FILE_NAME_POSIX, volume,
903 ntfs_index_ctx_put(vol->secure_xsii);
904 ntfs_index_ctx_put(vol->secure_xsdh);
905 ntfs_inode_close(vol->secure_ni);
906 put_ntfs_volume(volume);
909 #endif /* WITH_NTFS_3G */