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 static inline const ntfschar *
52 attr_record_name(const ATTR_RECORD *record)
54 return (const ntfschar *)
55 ((const u8 *)record + le16_to_cpu(record->name_offset));
59 open_ntfs_attr(ntfs_inode *ni, const struct ntfs_location *loc)
63 na = ntfs_attr_open(ni,
64 (ATTR_TYPES)loc->attr_type,
66 loc->attr_name_nchars);
68 ERROR_WITH_ERRNO("Failed to open attribute of \"%"TS"\" in "
69 "NTFS volume", loc->path);
75 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
76 consume_data_callback_t cb, void *cb_ctx)
78 const struct ntfs_location *loc = blob->ntfs_loc;
79 ntfs_volume *vol = loc->ntfs_vol;
87 ni = ntfs_pathname_to_inode(vol, NULL, loc->path);
89 ERROR_WITH_ERRNO("Can't find NTFS inode for \"%"TS"\"", loc->path);
90 ret = WIMLIB_ERR_NTFS_3G;
94 na = open_ntfs_attr(ni, loc);
96 ret = WIMLIB_ERR_NTFS_3G;
97 goto out_close_ntfs_inode;
100 pos = (loc->attr_type == AT_REPARSE_POINT) ? REPARSE_DATA_OFFSET : 0;
101 bytes_remaining = size;
102 while (bytes_remaining) {
103 s64 to_read = min(bytes_remaining, sizeof(buf));
104 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
105 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
106 ret = WIMLIB_ERR_NTFS_3G;
107 goto out_close_ntfs_attr;
110 bytes_remaining -= to_read;
111 ret = cb(buf, to_read, cb_ctx);
113 goto out_close_ntfs_attr;
118 out_close_ntfs_inode:
119 ntfs_inode_close(ni);
125 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
126 u32 *reparse_tag_ret)
132 na = open_ntfs_attr(ni, loc);
134 ret = WIMLIB_ERR_NTFS_3G;
138 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
139 &reparse_tag) != sizeof(reparse_tag))
141 ERROR_WITH_ERRNO("Error reading reparse data");
142 ret = WIMLIB_ERR_NTFS_3G;
143 goto out_close_ntfs_attr;
145 *reparse_tag_ret = le32_to_cpu(reparse_tag);
146 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
156 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
160 return STREAM_TYPE_DATA;
161 case AT_REPARSE_POINT:
162 return STREAM_TYPE_REPARSE_POINT;
165 return STREAM_TYPE_UNKNOWN;
169 /* Save information about an NTFS attribute (stream) to a WIM inode. */
171 scan_ntfs_attr(struct wim_inode *inode,
175 struct list_head *unhashed_blobs,
178 const ATTR_RECORD *record)
180 const u64 data_size = ntfs_get_attribute_value_length(record);
181 const size_t name_nchars = record->name_length;
182 struct blob_descriptor *blob = NULL;
183 utf16lechar *stream_name = NULL;
184 struct wim_inode_stream *strm;
187 if (unlikely(name_nchars)) {
189 stream_name = utf16le_dupz(attr_record_name(record),
190 name_nchars * sizeof(ntfschar));
192 ret = WIMLIB_ERR_NOMEM;
197 /* If the stream is non-empty, set up a blob descriptor for it. */
198 if (data_size != 0) {
199 blob = new_blob_descriptor();
200 if (unlikely(!blob)) {
201 ret = WIMLIB_ERR_NOMEM;
205 blob->ntfs_loc = CALLOC(1, sizeof(struct ntfs_location));
206 if (unlikely(!blob->ntfs_loc)) {
207 ret = WIMLIB_ERR_NOMEM;
211 blob->blob_location = BLOB_IN_NTFS_VOLUME;
212 blob->size = data_size;
213 blob->ntfs_loc->ntfs_vol = vol;
214 blob->ntfs_loc->attr_type = type;
215 blob->ntfs_loc->path = memdup(path, path_len + 1);
216 if (unlikely(!blob->ntfs_loc->path)) {
217 ret = WIMLIB_ERR_NOMEM;
221 if (unlikely(name_nchars)) {
222 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
223 if (!blob->ntfs_loc->attr_name) {
224 ret = WIMLIB_ERR_NOMEM;
227 blob->ntfs_loc->attr_name_nchars = name_nchars;
230 if (unlikely(type == AT_REPARSE_POINT)) {
231 if (blob->size < REPARSE_DATA_OFFSET) {
232 ERROR("Reparse data of \"%s\" "
233 "is invalid (only %"PRIu64" bytes)!",
235 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
238 blob->size -= REPARSE_DATA_OFFSET;
239 ret = read_reparse_tag(ni, blob->ntfs_loc,
240 &inode->i_reparse_tag);
246 strm = inode_add_stream(inode,
247 attr_type_to_wimlib_stream_type(type),
248 stream_name ? stream_name : NO_STREAM_NAME,
250 if (unlikely(!strm)) {
251 ret = WIMLIB_ERR_NOMEM;
254 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
258 free_blob_descriptor(blob);
263 /* Scan attributes of the specified type from a file in the NTFS volume */
265 scan_ntfs_attrs_with_type(struct wim_inode *inode,
269 struct list_head *unhashed_blobs,
273 ntfs_attr_search_ctx *actx;
276 DEBUG("Scanning NTFS attributes from \"%s\"", path);
278 actx = ntfs_attr_get_search_ctx(ni, NULL);
280 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
281 "context for \"%s\"", path);
282 return WIMLIB_ERR_NTFS_3G;
285 while (!ntfs_attr_lookup(type, NULL, 0,
286 CASE_SENSITIVE, 0, NULL, 0, actx))
288 ret = scan_ntfs_attr(inode,
299 if (errno != ENOENT) {
300 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
301 ret = WIMLIB_ERR_NTFS_3G;
306 ntfs_attr_put_search_ctx(actx);
310 /* Binary tree that maps NTFS inode numbers to DOS names */
311 struct dos_name_map {
312 struct avl_tree_node *root;
315 struct dos_name_node {
316 struct avl_tree_node index_node;
322 #define DOS_NAME_NODE(avl_node) \
323 avl_tree_entry(avl_node, struct dos_name_node, index_node)
326 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
327 const struct avl_tree_node *n2)
329 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
330 DOS_NAME_NODE(n2)->ntfs_ino);
333 /* Inserts a new DOS name into the map */
335 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
336 size_t name_nbytes, le64 ntfs_ino)
338 struct dos_name_node *new_node;
340 DEBUG("DOS name_len = %zu", name_nbytes);
341 new_node = MALLOC(sizeof(struct dos_name_node));
343 return WIMLIB_ERR_NOMEM;
345 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
346 * assuming 2-byte ntfs characters) */
347 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
349 /* Initialize the DOS name, DOS name length, and NTFS inode number of
350 * the search tree node */
351 memcpy(new_node->dos_name, dos_name, name_nbytes);
352 new_node->name_nbytes = name_nbytes;
353 new_node->ntfs_ino = ntfs_ino;
355 /* Insert the search tree node */
356 if (avl_tree_insert(&map->root, &new_node->index_node,
357 _avl_cmp_by_ntfs_ino))
359 /* This should be impossible since an NTFS inode cannot
360 * have multiple DOS names, and we only should get each
361 * DOS name entry once from the ntfs_readdir() calls. */
362 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
363 le64_to_cpu(ntfs_ino));
365 return WIMLIB_ERR_NOMEM;
367 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
371 /* Returns a structure that contains the DOS name and its length for an NTFS
372 * inode, or NULL if the inode has no DOS name. */
373 static struct dos_name_node *
374 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
376 struct dos_name_node dummy;
377 struct avl_tree_node *res;
379 dummy.ntfs_ino = cpu_to_le64(ntfs_ino);
381 res = avl_tree_lookup_node(map->root, &dummy.index_node,
382 _avl_cmp_by_ntfs_ino);
385 return DOS_NAME_NODE(res);
389 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
391 const struct dos_name_node *node;
393 if (dentry->is_win32_name) {
394 node = lookup_dos_name(map, dentry->d_inode->i_ino);
396 dentry->short_name = utf16le_dupz(node->dos_name,
398 if (!dentry->short_name)
399 return WIMLIB_ERR_NOMEM;
400 dentry->short_name_nbytes = node->name_nbytes;
401 DEBUG("Assigned DOS name to ino %"PRIu64,
402 dentry->d_inode->i_ino);
404 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
405 "corresponding DOS name",
406 dentry->d_inode->i_ino);
413 free_dos_name_tree(struct avl_tree_node *node) {
415 free_dos_name_tree(node->left);
416 free_dos_name_tree(node->right);
417 FREE(DOS_NAME_NODE(node));
422 destroy_dos_name_map(struct dos_name_map *map)
424 free_dos_name_tree(map->root);
428 struct wim_dentry *parent;
431 struct dos_name_map *dos_name_map;
433 struct capture_params *params;
438 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
443 ntfs_volume *ntfs_vol,
444 struct capture_params *params);
447 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
448 const int name_nchars, const int name_type,
449 const s64 pos, const MFT_REF mref,
450 const unsigned dt_type)
452 struct readdir_ctx *ctx;
453 size_t mbs_name_nbytes;
455 struct wim_dentry *child;
458 size_t name_nbytes = name_nchars * sizeof(ntfschar);
461 if (name_type & FILE_NAME_DOS) {
462 /* If this is the entry for a DOS name, store it for later. */
463 ret = insert_dos_name(ctx->dos_name_map, name,
464 name_nbytes, mref & MFT_REF_MASK_CPU);
466 /* Return now if an error occurred or if this is just a DOS name
467 * and not a Win32+DOS name. */
468 if (ret != 0 || name_type == FILE_NAME_DOS)
471 ret = utf16le_to_tstr(name, name_nbytes,
472 &mbs_name, &mbs_name_nbytes);
476 if (mbs_name[0] == '.' &&
477 (mbs_name[1] == '\0' ||
478 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
481 * note: name_type is POSIX for these, so DOS names will not
482 * have been inserted for them. */
484 goto out_free_mbs_name;
487 /* Open the inode for this directory entry and recursively capture the
488 * directory tree rooted at it */
489 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
491 /* XXX This used to be treated as an error, but NTFS-3g seemed
492 * to be unable to read some inodes on a Windows 8 image for
494 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
495 ctx->path, mbs_name);
497 goto out_free_mbs_name;
499 path_len = ctx->path_len;
501 ctx->path[path_len++] = '/';
502 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
503 path_len += mbs_name_nbytes;
505 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
507 ctx->vol, ctx->params);
508 path_len -= mbs_name_nbytes + 1;
510 dentry_add_child(ctx->parent, child);
511 ntfs_inode_close(ni);
515 ctx->path[ctx->path_len] = '\0';
520 /* Recursive scan routine for NTFS volumes */
522 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
528 struct capture_params *params)
532 struct wim_dentry *root = NULL;
533 struct wim_inode *inode = NULL;
535 ret = try_exclude(path, path_len, params);
536 if (ret < 0) /* Excluded? */
538 if (ret > 0) /* Error? */
541 /* Get file attributes */
542 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
543 if (ret != sizeof(attributes)) {
544 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
545 ret = WIMLIB_ERR_NTFS_3G;
549 if (attributes & FILE_ATTRIBUTE_ENCRYPTED) {
550 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
552 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
553 "does not support encrypted files and directories", path);
554 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
557 params->progress.scan.cur_path = path;
558 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
562 /* Create a WIM dentry with an associated inode, which may be shared */
563 ret = inode_table_new_dentry(params->inode_table,
564 path_basename_with_len(path, path_len),
565 ni->mft_no, 0, false, &root);
569 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
570 root->is_win32_name = 1;
572 inode = root->d_inode;
574 if (inode->i_nlink > 1) {
575 /* Shared inode; nothing more to do */
579 inode->i_creation_time = le64_to_cpu(ni->creation_time);
580 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
581 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
582 inode->i_attributes = attributes;
584 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
585 /* Scan the reparse point stream. */
586 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
587 params->unhashed_blobs,
588 vol, AT_REPARSE_POINT);
593 /* Scan the data streams.
595 * Note: directories should not have an unnamed data stream, but they
596 * may have named data streams. Nondirectories (including reparse
597 * points) can have an unnamed data stream as well as named data
599 ret = scan_ntfs_attrs_with_type(inode, ni, path, path_len,
600 params->unhashed_blobs, vol, AT_DATA);
604 if (inode_is_directory(inode)) {
606 /* Recurse to directory children */
608 struct dos_name_map dos_name_map = { .root = NULL };
609 struct readdir_ctx ctx = {
612 .path_len = path_len,
613 .dos_name_map = &dos_name_map,
618 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
624 /* error from ntfs_readdir() itself */
625 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
626 ret = WIMLIB_ERR_NTFS_3G;
629 struct wim_dentry *child;
632 for_dentry_child(child, root) {
633 ret = set_dentry_dos_name(child, &dos_name_map);
638 destroy_dos_name_map(&dos_name_map);
642 path[path_len] = '\0';
644 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
645 * only allow capturing an entire volume. */
646 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
647 inode_is_symlink(inode))
648 inode->i_not_rpfixed = 0;
650 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
651 struct SECURITY_CONTEXT sec_ctx;
655 /* Get security descriptor */
656 memset(&sec_ctx, 0, sizeof(sec_ctx));
661 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
662 if (ret > sizeof(_sd)) {
664 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
667 inode->i_security_id = sd_set_add_sd(params->sd_set,
669 if (inode->i_security_id == -1) {
670 ERROR("Out of memory");
671 ret = WIMLIB_ERR_NOMEM;
674 DEBUG("Added security ID = %u for `%s'",
675 inode->i_security_id, path);
677 } else if (ret < 0) {
678 ERROR_WITH_ERRNO("Failed to get security information from "
680 ret = WIMLIB_ERR_NTFS_3G;
682 inode->i_security_id = -1;
683 DEBUG("No security ID for `%s'", path);
690 params->progress.scan.cur_path = path;
692 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
694 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
697 free_dentry_tree(root, params->blob_table);
699 ret = report_capture_error(params, ret, path);
707 do_ntfs_umount(struct _ntfs_volume *vol)
709 DEBUG("Unmounting NTFS volume");
710 if (ntfs_umount(vol, FALSE))
711 return WIMLIB_ERR_NTFS_3G;
717 build_dentry_tree_ntfs(struct wim_dentry **root_p,
719 struct capture_params *params)
725 DEBUG("Mounting NTFS volume `%s' read-only", device);
727 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
730 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
731 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
732 * seem to contain any explicit version information. So we have to rely on a
733 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
735 #ifdef HAVE_NTFS_MNT_RDONLY
737 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
738 #elif defined(MS_RDONLY)
739 /* NTFS-3g 2011, 2012 */
740 vol = ntfs_mount(device, MS_RDONLY);
742 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
745 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
747 return WIMLIB_ERR_NTFS_3G;
749 ntfs_open_secure(vol);
751 /* We don't want to capture the special NTFS files such as $Bitmap. Not
752 * to be confused with "hidden" or "system" files which are real files
753 * that we do need to capture. */
754 NVolClearShowSysFiles(vol);
756 DEBUG("Opening root NTFS dentry");
757 root_ni = ntfs_inode_open(vol, FILE_root);
759 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
761 ret = WIMLIB_ERR_NTFS_3G;
765 /* Currently we assume that all the paths fit into this length and there
766 * is no check for overflow. */
767 char *path = MALLOC(32768);
769 ERROR("Could not allocate memory for NTFS pathname");
770 ret = WIMLIB_ERR_NOMEM;
776 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
777 FILE_NAME_POSIX, vol, params);
780 ntfs_inode_close(root_ni);
782 ntfs_index_ctx_put(vol->secure_xsii);
783 ntfs_index_ctx_put(vol->secure_xsdh);
784 ntfs_inode_close(vol->secure_ni);
787 if (do_ntfs_umount(vol)) {
788 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
792 /* We need to leave the NTFS volume mounted so that we can read
793 * the NTFS files again when we are actually writing the WIM */
794 *(ntfs_volume**)params->extra_arg = vol;
798 #endif /* WITH_NTFS_3G */