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;
268 /* Save information about an NTFS attribute (stream) to a WIM inode. */
270 scan_ntfs_attr(struct wim_inode *inode,
273 struct list_head *unhashed_blobs,
274 struct ntfs_volume_wrapper *volume,
276 const ATTR_RECORD *record)
278 u64 data_size = ntfs_get_attribute_value_length(record);
279 const u32 name_nchars = record->name_length;
280 struct blob_descriptor *blob = NULL;
281 utf16lechar *stream_name = NULL;
282 struct wim_inode_stream *strm;
285 if (unlikely(name_nchars)) {
287 stream_name = utf16le_dupz(attr_record_name(record),
288 name_nchars * sizeof(ntfschar));
290 ret = WIMLIB_ERR_NOMEM;
295 if (unlikely(type == AT_REPARSE_POINT)) {
296 if (data_size < REPARSE_DATA_OFFSET) {
297 ERROR("Reparse point attribute of \"%s\" "
298 "is too short!", path);
299 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
302 data_size -= REPARSE_DATA_OFFSET;
304 ret = read_reparse_header(ni, inode);
306 ERROR_WITH_ERRNO("Error reading reparse point header "
312 /* If the stream is non-empty, set up a blob descriptor for it. */
313 if (data_size != 0) {
314 blob = new_blob_descriptor();
315 if (unlikely(!blob)) {
316 ret = WIMLIB_ERR_NOMEM;
320 blob->ntfs_loc = CALLOC(1, sizeof(struct ntfs_location));
321 if (unlikely(!blob->ntfs_loc)) {
322 ret = WIMLIB_ERR_NOMEM;
326 blob->blob_location = BLOB_IN_NTFS_VOLUME;
327 blob->size = data_size;
328 blob->ntfs_loc->volume = get_ntfs_volume(volume);
329 blob->ntfs_loc->attr_type = type;
330 blob->ntfs_loc->mft_no = ni->mft_no;
332 if (unlikely(name_nchars)) {
333 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
334 if (!blob->ntfs_loc->attr_name) {
335 ret = WIMLIB_ERR_NOMEM;
338 blob->ntfs_loc->attr_name_nchars = name_nchars;
341 ret = set_attr_sort_key(ni, blob->ntfs_loc);
346 strm = inode_add_stream(inode,
347 attr_type_to_wimlib_stream_type(type),
348 stream_name ? stream_name : NO_STREAM_NAME,
350 if (unlikely(!strm)) {
351 ret = WIMLIB_ERR_NOMEM;
354 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
358 free_blob_descriptor(blob);
363 /* Scan attributes of the specified type from a file in the NTFS volume */
365 scan_ntfs_attrs_with_type(struct wim_inode *inode,
368 struct list_head *unhashed_blobs,
369 struct ntfs_volume_wrapper *volume,
372 ntfs_attr_search_ctx *actx;
375 actx = ntfs_attr_get_search_ctx(ni, NULL);
377 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
378 "context for \"%s\"", path);
379 return WIMLIB_ERR_NTFS_3G;
382 while (!ntfs_attr_lookup(type, NULL, 0,
383 CASE_SENSITIVE, 0, NULL, 0, actx))
385 ret = scan_ntfs_attr(inode,
395 if (errno != ENOENT) {
396 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
397 ret = WIMLIB_ERR_NTFS_3G;
402 ntfs_attr_put_search_ctx(actx);
406 /* Load the security descriptor of an NTFS inode into the corresponding WIM
407 * inode and the WIM image's security descriptor set. */
408 static noinline_for_stack int
409 get_security_descriptor(ntfs_inode *ni, struct wim_inode *inode,
410 ntfs_volume *vol, struct wim_sd_set *sd_set)
412 struct SECURITY_CONTEXT scx = {.vol = vol};
415 size_t avail_size = sizeof(_buf);
419 ret = ntfs_get_ntfs_acl(&scx, ni, buf, avail_size);
420 if (unlikely(ret < 0)) {
421 ret = WIMLIB_ERR_NTFS_3G;
425 if (unlikely(ret > avail_size)) {
426 if (unlikely(buf != _buf))
430 ret = WIMLIB_ERR_NOMEM;
437 if (likely(ret > 0)) {
438 inode->i_security_id = sd_set_add_sd(sd_set, buf, ret);
439 if (unlikely(inode->i_security_id < 0)) {
440 ret = WIMLIB_ERR_NOMEM;
447 if (unlikely(buf != _buf))
452 /* Binary tree that maps NTFS inode numbers to DOS names */
453 struct dos_name_map {
454 struct avl_tree_node *root;
457 struct dos_name_node {
458 struct avl_tree_node index_node;
464 #define DOS_NAME_NODE(avl_node) \
465 avl_tree_entry(avl_node, struct dos_name_node, index_node)
468 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
469 const struct avl_tree_node *n2)
471 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
472 DOS_NAME_NODE(n2)->ntfs_ino);
475 /* Inserts a new DOS name into the map */
477 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
478 size_t name_nbytes, u64 ntfs_ino)
480 struct dos_name_node *new_node;
482 new_node = MALLOC(sizeof(struct dos_name_node));
484 return WIMLIB_ERR_NOMEM;
486 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
487 * assuming 2-byte ntfs characters) */
488 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
490 /* Initialize the DOS name, DOS name length, and NTFS inode number of
491 * the search tree node */
492 memcpy(new_node->dos_name, dos_name, name_nbytes);
493 new_node->name_nbytes = name_nbytes;
494 new_node->ntfs_ino = ntfs_ino;
496 /* Insert the search tree node */
497 if (avl_tree_insert(&map->root, &new_node->index_node,
498 _avl_cmp_by_ntfs_ino))
500 /* This should be impossible since an NTFS inode cannot have
501 * multiple DOS names, and we only should get each DOS name
502 * entry once from the ntfs_readdir() calls. */
503 WARNING("NTFS inode %"PRIu64" has multiple DOS names", ntfs_ino);
509 /* Returns a structure that contains the DOS name and its length for an NTFS
510 * inode, or NULL if the inode has no DOS name. */
511 static struct dos_name_node *
512 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
514 struct dos_name_node dummy;
515 struct avl_tree_node *res;
517 dummy.ntfs_ino = ntfs_ino;
519 res = avl_tree_lookup_node(map->root, &dummy.index_node,
520 _avl_cmp_by_ntfs_ino);
523 return DOS_NAME_NODE(res);
527 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
529 const struct dos_name_node *node;
531 if (dentry->d_is_win32_name) {
532 node = lookup_dos_name(map, dentry->d_inode->i_ino);
534 dentry->d_short_name = utf16le_dupz(node->dos_name,
536 if (!dentry->d_short_name)
537 return WIMLIB_ERR_NOMEM;
538 dentry->d_short_name_nbytes = node->name_nbytes;
540 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
541 "corresponding DOS name",
542 dentry->d_inode->i_ino);
549 destroy_dos_name_map(struct dos_name_map *map)
551 struct dos_name_node *node;
552 avl_tree_for_each_in_postorder(node, map->root,
553 struct dos_name_node, index_node)
558 struct wim_dentry *parent;
561 struct dos_name_map dos_name_map;
562 struct ntfs_volume_wrapper *volume;
563 struct capture_params *params;
568 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_p,
573 struct ntfs_volume_wrapper *volume,
574 struct capture_params *params);
577 filldir(void *_ctx, const ntfschar *name, const int name_nchars,
578 const int name_type, const s64 pos, const MFT_REF mref,
579 const unsigned dt_type)
581 struct readdir_ctx *ctx = _ctx;
582 const size_t name_nbytes = name_nchars * sizeof(ntfschar);
584 size_t mbs_name_nbytes;
586 struct wim_dentry *child;
589 if (name_type & FILE_NAME_DOS) {
590 /* If this is the entry for a DOS name, store it for later. */
591 ret = insert_dos_name(&ctx->dos_name_map, name,
592 name_nbytes, MREF(mref));
594 /* Return now if an error occurred or if this is just a DOS name
595 * and not a Win32+DOS name. */
596 if (ret != 0 || name_type == FILE_NAME_DOS)
600 ret = utf16le_to_tstr(name, name_nbytes, &mbs_name, &mbs_name_nbytes);
604 if (should_ignore_filename(mbs_name, mbs_name_nbytes))
605 goto out_free_mbs_name;
607 path_len = ctx->path_len;
609 ctx->path[path_len++] = '/';
610 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
611 path_len += mbs_name_nbytes;
613 ret = ntfs_3g_build_dentry_tree_recursive(&child, mref, ctx->path,
615 ctx->volume, ctx->params);
616 attach_scanned_tree(ctx->parent, child, ctx->params->blob_table);
625 ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
626 struct wim_dentry *parent,
627 struct ntfs_volume_wrapper *volume,
628 struct capture_params *params)
632 struct readdir_ctx ctx = {
635 .path_len = path_len,
636 .dos_name_map = { .root = NULL },
641 ret = ntfs_readdir(ni, &pos, &ctx, filldir);
642 path[path_len] = '\0';
648 /* error from ntfs_readdir() itself */
649 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
650 ret = WIMLIB_ERR_NTFS_3G;
653 struct wim_dentry *child;
656 for_dentry_child(child, parent) {
657 ret = set_dentry_dos_name(child, &ctx.dos_name_map);
662 destroy_dos_name_map(&ctx.dos_name_map);
667 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
672 struct ntfs_volume_wrapper *volume,
673 struct capture_params *params)
677 struct wim_dentry *root = NULL;
678 struct wim_inode *inode = NULL;
679 ntfs_inode *ni = NULL;
681 ret = try_exclude(path, params);
682 if (unlikely(ret < 0)) /* Excluded? */
684 if (unlikely(ret > 0)) /* Error? */
687 ni = ntfs_inode_open(volume->vol, mref);
689 ERROR_WITH_ERRNO("Failed to open NTFS file \"%s\"", path);
690 ret = WIMLIB_ERR_NTFS_3G;
694 /* Get file attributes */
695 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
696 if (ret != sizeof(attributes)) {
697 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
698 ret = WIMLIB_ERR_NTFS_3G;
702 if (unlikely(attributes & FILE_ATTRIBUTE_ENCRYPTED)) {
703 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
705 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
706 "does not support encrypted files and directories", path);
707 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
710 params->progress.scan.cur_path = path;
711 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
715 /* Create a WIM dentry with an associated inode, which may be shared */
716 ret = inode_table_new_dentry(params->inode_table,
717 path_basename_with_len(path, path_len),
718 ni->mft_no, 0, false, &root);
722 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
723 root->d_is_win32_name = 1;
725 inode = root->d_inode;
727 if (inode->i_nlink > 1) {
728 /* Shared inode; nothing more to do */
732 inode->i_creation_time = le64_to_cpu(ni->creation_time);
733 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
734 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
735 inode->i_attributes = attributes;
737 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
738 /* Scan the reparse point stream. */
739 ret = scan_ntfs_attrs_with_type(inode, ni, path,
740 params->unhashed_blobs,
741 volume, AT_REPARSE_POINT);
746 /* Scan the data streams.
748 * Note: directories should not have an unnamed data stream, but they
749 * may have named data streams. Nondirectories (including reparse
750 * points) can have an unnamed data stream as well as named data
752 ret = scan_ntfs_attrs_with_type(inode, ni, path, params->unhashed_blobs,
757 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
758 * only allow capturing an entire volume. */
759 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
760 inode_is_symlink(inode))
761 inode->i_rp_flags &= ~WIM_RP_FLAG_NOT_FIXED;
763 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
764 ret = get_security_descriptor(ni, inode, volume->vol,
767 ERROR_WITH_ERRNO("Error reading security descriptor "
773 if (inode_is_directory(inode)) {
774 ret = ntfs_3g_recurse_directory(ni, path, path_len, root,
781 params->progress.scan.cur_path = path;
783 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
785 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
788 ntfs_inode_close(ni);
790 free_dentry_tree(root, params->blob_table);
792 ret = report_capture_error(params, ret, path);
799 ntfs_3g_build_dentry_tree(struct wim_dentry **root_ret,
801 struct capture_params *params)
803 struct ntfs_volume_wrapper *volume;
808 volume = MALLOC(sizeof(struct ntfs_volume_wrapper));
810 return WIMLIB_ERR_NOMEM;
812 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
814 ERROR_WITH_ERRNO("Failed to mount NTFS volume \"%s\" read-only",
817 return WIMLIB_ERR_NTFS_3G;
823 ntfs_open_secure(vol);
825 /* We don't want to capture the special NTFS files such as $Bitmap. Not
826 * to be confused with "hidden" or "system" files which are real files
827 * that we do need to capture. */
828 NVolClearShowSysFiles(vol);
830 /* Currently we assume that all the paths fit into this length and there
831 * is no check for overflow. */
832 path = MALLOC(32768);
834 ret = WIMLIB_ERR_NOMEM;
835 goto out_put_ntfs_volume;
840 ret = ntfs_3g_build_dentry_tree_recursive(root_ret, FILE_root, path, 1,
841 FILE_NAME_POSIX, volume,
845 ntfs_index_ctx_put(vol->secure_xsii);
846 ntfs_index_ctx_put(vol->secure_xsdh);
847 ntfs_inode_close(vol->secure_ni);
848 put_ntfs_volume(volume);
851 #endif /* WITH_NTFS_3G */