4 * Capture a WIM image directly from a NTFS volume using libntfs-3g. We capture
5 * everything we can, including security data and alternate data streams.
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
40 #include <ntfs-3g/attrib.h>
41 #include <ntfs-3g/reparse.h>
42 #include <ntfs-3g/security.h>
43 #include <ntfs-3g/volume.h>
45 #include "wimlib/capture.h"
46 #include "wimlib/dentry.h"
47 #include "wimlib/encoding.h"
48 #include "wimlib/endianness.h"
49 #include "wimlib/error.h"
50 #include "wimlib/lookup_table.h"
51 #include "wimlib/ntfs_3g.h"
52 #include "wimlib/paths.h"
53 #include "wimlib/security.h"
55 static inline ntfschar *
56 attr_record_name(ATTR_RECORD *ar)
58 return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
62 open_ntfs_attr(ntfs_inode *ni, struct ntfs_location *loc)
66 na = ntfs_attr_open(ni,
67 loc->is_reparse_point ? AT_REPARSE_POINT : AT_DATA,
69 loc->stream_name_nchars);
71 ERROR_WITH_ERRNO("Failed to open attribute of \"%"TS"\" in "
72 "NTFS volume", loc->path);
78 read_ntfs_file_prefix(const struct wim_lookup_table_entry *lte,
80 consume_data_callback_t cb,
85 struct ntfs_location *loc = lte->ntfs_loc;
86 ntfs_volume *vol = loc->ntfs_vol;
92 bool out_buf_malloced;
95 ni = ntfs_pathname_to_inode(vol, NULL, loc->path);
97 ERROR_WITH_ERRNO("Can't find NTFS inode for \"%"TS"\"", loc->path);
98 ret = WIMLIB_ERR_NTFS_3G;
102 na = open_ntfs_attr(ni, loc);
104 ret = WIMLIB_ERR_NTFS_3G;
105 goto out_close_ntfs_inode;
108 out_buf_malloced = false;
110 if (in_chunk_size <= STACK_MAX) {
111 out_buf = alloca(in_chunk_size);
113 out_buf = MALLOC(in_chunk_size);
114 if (out_buf == NULL) {
115 ret = WIMLIB_ERR_NOMEM;
116 goto out_close_ntfs_attr;
118 out_buf_malloced = true;
121 out_buf = ctx_or_buf;
123 pos = (loc->is_reparse_point) ? 8 : 0;
124 bytes_remaining = size;
125 while (bytes_remaining) {
126 s64 to_read = min(bytes_remaining, in_chunk_size);
127 if (ntfs_attr_pread(na, pos, to_read, out_buf) != to_read) {
128 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
129 ret = WIMLIB_ERR_NTFS_3G;
130 goto out_free_memory;
133 bytes_remaining -= to_read;
135 ret = cb(out_buf, to_read, ctx_or_buf);
137 goto out_free_memory;
144 if (out_buf_malloced)
148 out_close_ntfs_inode:
149 ntfs_inode_close(ni);
155 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
156 u32 *reparse_tag_ret)
162 na = open_ntfs_attr(ni, loc);
164 ret = WIMLIB_ERR_NTFS_3G;
168 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
169 &reparse_tag) != sizeof(reparse_tag))
171 ERROR_WITH_ERRNO("Error reading reparse data");
172 ret = WIMLIB_ERR_NTFS_3G;
173 goto out_close_ntfs_attr;
175 *reparse_tag_ret = le32_to_cpu(reparse_tag);
176 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
185 /* Load the streams from a file or reparse point in the NTFS volume into the WIM
188 capture_ntfs_streams(struct wim_inode *inode,
192 struct wim_lookup_table *lookup_table,
196 ntfs_attr_search_ctx *actx;
197 struct ntfs_location *ntfs_loc;
199 struct wim_lookup_table_entry *lte;
201 DEBUG2("Capturing NTFS data streams from `%s'", path);
203 /* Get context to search the streams of the NTFS file. */
204 actx = ntfs_attr_get_search_ctx(ni, NULL);
206 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
207 "context for \"%s\"", path);
208 return WIMLIB_ERR_NTFS_3G;
211 /* Capture each data stream or reparse data stream. */
212 while (!ntfs_attr_lookup(type, NULL, 0,
213 CASE_SENSITIVE, 0, NULL, 0, actx))
215 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
216 u64 name_length = actx->attr->name_length;
219 if (data_size == 0) {
220 /* Empty stream. No lookup table entry is needed. */
224 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
226 ret = WIMLIB_ERR_NOMEM;
229 ntfs_loc->ntfs_vol = vol;
230 ntfs_loc->path = memdup(path, path_len + 1);
231 if (!ntfs_loc->path) {
232 ret = WIMLIB_ERR_NOMEM;
233 goto out_free_ntfs_loc;
236 ntfs_loc->stream_name = memdup(attr_record_name(actx->attr),
238 if (!ntfs_loc->stream_name) {
239 ret = WIMLIB_ERR_NOMEM;
240 goto out_free_ntfs_loc;
242 ntfs_loc->stream_name_nchars = name_length;
245 lte = new_lookup_table_entry();
247 ret = WIMLIB_ERR_NOMEM;
248 goto out_free_ntfs_loc;
250 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
251 lte->ntfs_loc = ntfs_loc;
253 if (type == AT_REPARSE_POINT) {
255 ERROR("Invalid reparse data on \"%s\" "
256 "(only %u bytes)!", path, (unsigned)data_size);
257 ret = WIMLIB_ERR_NTFS_3G;
260 lte->ntfs_loc->is_reparse_point = true;
261 lte->size = data_size - 8;
262 ret = read_reparse_tag(ni, lte->ntfs_loc,
263 &inode->i_reparse_tag);
267 lte->ntfs_loc->is_reparse_point = false;
268 lte->size = data_size;
271 if (name_length == 0) {
272 /* Unnamed data stream. Put the reference to it in the
276 ERROR("Found two un-named data streams for \"%s\" "
277 "(sizes = %"PRIu64", %"PRIu64")",
278 path, inode->i_lte->size,
280 ret = WIMLIB_ERR_NTFS_3G;
288 /* Named data stream. Put the reference to it in the
289 * alternate data stream entries */
290 struct wim_ads_entry *new_ads_entry;
292 new_ads_entry = inode_add_ads_utf16le(inode,
293 attr_record_name(actx->attr),
295 if (!new_ads_entry) {
296 ret = WIMLIB_ERR_NOMEM;
299 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
300 stream_id = new_ads_entry->stream_id;
301 new_ads_entry->lte = lte;
304 lookup_table_insert_unhashed(lookup_table, lte,
308 if (errno == ENOENT) {
311 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
312 ret = WIMLIB_ERR_NTFS_3G;
316 free_lookup_table_entry(lte);
319 FREE(ntfs_loc->path);
320 FREE(ntfs_loc->stream_name);
324 ntfs_attr_put_search_ctx(actx);
326 DEBUG2("Successfully captured NTFS streams from \"%s\"", path);
328 ERROR("Failed to capture NTFS streams from \"%s\"", path);
332 /* Red-black tree that maps NTFS inode numbers to DOS names */
333 struct dos_name_map {
334 struct rb_root rb_root;
337 struct dos_name_node {
338 struct rb_node rb_node;
344 /* Inserts a new DOS name into the map */
346 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
347 size_t name_nbytes, le64 ntfs_ino)
349 struct dos_name_node *new_node;
351 struct rb_root *root;
352 struct rb_node *rb_parent;
354 DEBUG("DOS name_len = %zu", name_nbytes);
355 new_node = MALLOC(sizeof(struct dos_name_node));
359 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
360 * assuming 2-byte ntfs characters) */
361 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
363 /* Initialize the DOS name, DOS name length, and NTFS inode number of
364 * the red-black tree node */
365 memcpy(new_node->dos_name, dos_name, name_nbytes);
366 new_node->name_nbytes = name_nbytes;
367 new_node->ntfs_ino = ntfs_ino;
369 /* Insert the red-black tree node */
370 root = &map->rb_root;
374 struct dos_name_node *this;
376 this = container_of(*p, struct dos_name_node, rb_node);
378 if (new_node->ntfs_ino < this->ntfs_ino)
379 p = &((*p)->rb_left);
380 else if (new_node->ntfs_ino > this->ntfs_ino)
381 p = &((*p)->rb_right);
383 /* This should be impossible since a NTFS inode cannot
384 * have multiple DOS names, and we only should get each
385 * DOS name entry once from the ntfs_readdir() calls. */
386 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
387 le64_to_cpu(ntfs_ino));
391 rb_link_node(&new_node->rb_node, rb_parent, p);
392 rb_insert_color(&new_node->rb_node, root);
393 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
397 /* Returns a structure that contains the DOS name and its length for a NTFS
398 * inode, or NULL if the inode has no DOS name. */
399 static struct dos_name_node *
400 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
402 struct rb_node *node = map->rb_root.rb_node;
404 struct dos_name_node *this;
405 this = container_of(node, struct dos_name_node, rb_node);
406 if (ntfs_ino < this->ntfs_ino)
407 node = node->rb_left;
408 else if (ntfs_ino > this->ntfs_ino)
409 node = node->rb_right;
417 set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
419 const struct dos_name_map *map = arg;
420 const struct dos_name_node *node;
422 if (dentry->is_win32_name) {
423 node = lookup_dos_name(map, dentry->d_inode->i_ino);
425 dentry->short_name = MALLOC(node->name_nbytes + 2);
426 if (!dentry->short_name)
427 return WIMLIB_ERR_NOMEM;
428 memcpy(dentry->short_name, node->dos_name,
430 dentry->short_name[node->name_nbytes / 2] = 0;
431 dentry->short_name_nbytes = node->name_nbytes;
432 DEBUG("Assigned DOS name to ino %"PRIu64,
433 dentry->d_inode->i_ino);
435 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
436 "corresponding DOS name",
437 dentry->d_inode->i_ino);
444 free_dos_name_tree(struct rb_node *node) {
446 free_dos_name_tree(node->rb_left);
447 free_dos_name_tree(node->rb_right);
448 FREE(container_of(node, struct dos_name_node, rb_node));
453 destroy_dos_name_map(struct dos_name_map *map)
455 free_dos_name_tree(map->rb_root.rb_node);
459 struct wim_dentry *parent;
462 struct dos_name_map *dos_name_map;
464 struct add_image_params *params;
468 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
473 ntfs_volume *ntfs_vol,
474 struct add_image_params *params);
477 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
478 const int name_nchars, const int name_type,
479 const s64 pos, const MFT_REF mref,
480 const unsigned dt_type)
482 struct readdir_ctx *ctx;
483 size_t mbs_name_nbytes;
485 struct wim_dentry *child;
488 size_t name_nbytes = name_nchars * sizeof(ntfschar);
491 if (name_type & FILE_NAME_DOS) {
492 /* If this is the entry for a DOS name, store it for later. */
493 ret = insert_dos_name(ctx->dos_name_map, name,
494 name_nbytes, mref & MFT_REF_MASK_CPU);
496 /* Return now if an error occurred or if this is just a DOS name
497 * and not a Win32+DOS name. */
498 if (ret != 0 || name_type == FILE_NAME_DOS)
501 ret = utf16le_to_tstr(name, name_nbytes,
502 &mbs_name, &mbs_name_nbytes);
506 if (mbs_name[0] == '.' &&
507 (mbs_name[1] == '\0' ||
508 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
511 * note: name_type is POSIX for these, so DOS names will not
512 * have been inserted for them. */
514 goto out_free_mbs_name;
517 /* Open the inode for this directory entry and recursively capture the
518 * directory tree rooted at it */
519 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
521 /* XXX This used to be treated as an error, but NTFS-3g seemed
522 * to be unable to read some inodes on a Windows 8 image for
524 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
525 ctx->path, mbs_name);
527 goto out_free_mbs_name;
529 path_len = ctx->path_len;
531 ctx->path[path_len++] = '/';
532 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
533 path_len += mbs_name_nbytes;
535 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
537 ctx->vol, ctx->params);
538 path_len -= mbs_name_nbytes + 1;
540 dentry_add_child(ctx->parent, child);
541 ntfs_inode_close(ni);
545 ctx->path[ctx->path_len] = '\0';
549 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
550 * At the same time, update the WIM lookup table with lookup table entries for
551 * the NTFS streams, and build an array of security descriptors.
554 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
560 struct add_image_params *params)
564 struct wim_dentry *root = NULL;
565 struct wim_inode *inode;
566 ATTR_TYPES stream_type;
568 params->progress.scan.cur_path = path;
570 if (exclude_path(path, path_len, params->config, false)) {
571 /* Exclude a file or directory tree based on the capture
572 * configuration file */
573 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED);
578 /* Get file attributes */
579 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
580 if (ret != sizeof(attributes)) {
581 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
582 ret = WIMLIB_ERR_NTFS_3G;
586 if ((attributes & (FILE_ATTRIBUTE_DIRECTORY |
587 FILE_ATTRIBUTE_ENCRYPTED)) == FILE_ATTRIBUTE_ENCRYPTED)
589 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
591 ERROR("Can't archive unsupported encrypted file \"%s\"", path);
592 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
595 do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED);
600 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK);
602 /* Create a WIM dentry with an associated inode, which may be shared */
603 ret = inode_table_new_dentry(¶ms->inode_table,
604 path_basename_with_len(path, path_len),
605 ni->mft_no, 0, false, &root);
609 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
610 root->is_win32_name = 1;
612 inode = root->d_inode;
614 if (inode->i_nlink > 1) {
615 /* Shared inode; nothing more to do */
620 inode->i_creation_time = le64_to_cpu(ni->creation_time);
621 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
622 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
623 inode->i_attributes = le32_to_cpu(attributes);
624 inode->i_resolved = 1;
626 if (attributes & FILE_ATTR_REPARSE_POINT)
627 stream_type = AT_REPARSE_POINT;
629 stream_type = AT_DATA;
631 /* Capture the file's streams; more specifically, this is supposed to:
633 * - Regular files: capture unnamed data stream and any named data
635 * - Directories: capture any named data streams
636 * - Reparse points: capture reparse data only
638 ret = capture_ntfs_streams(inode, ni, path, path_len,
639 params->lookup_table, vol, stream_type);
643 if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
645 /* Recurse to directory children */
647 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
648 struct readdir_ctx ctx = {
651 .path_len = path_len,
652 .dos_name_map = &dos_name_map,
656 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
658 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
659 ret = WIMLIB_ERR_NTFS_3G;
661 ret = for_dentry_child(root, set_dentry_dos_name,
664 destroy_dos_name_map(&dos_name_map);
669 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
670 * only allow capturing an entire volume. */
671 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
672 inode_is_symlink(inode))
673 inode->i_not_rpfixed = 0;
675 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
676 struct SECURITY_CONTEXT sec_ctx;
680 /* Get security descriptor */
681 memset(&sec_ctx, 0, sizeof(sec_ctx));
686 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
687 if (ret > sizeof(_sd)) {
689 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
692 inode->i_security_id = sd_set_add_sd(¶ms->sd_set,
694 if (inode->i_security_id == -1) {
695 ERROR("Out of memory");
696 ret = WIMLIB_ERR_NOMEM;
699 DEBUG("Added security ID = %u for `%s'",
700 inode->i_security_id, path);
702 } else if (ret < 0) {
703 ERROR_WITH_ERRNO("Failed to get security information from "
705 ret = WIMLIB_ERR_NTFS_3G;
707 inode->i_security_id = -1;
708 DEBUG("No security ID for `%s'", path);
715 free_dentry_tree(root, params->lookup_table);
721 do_ntfs_umount(struct _ntfs_volume *vol)
723 DEBUG("Unmounting NTFS volume");
724 if (ntfs_umount(vol, FALSE))
725 return WIMLIB_ERR_NTFS_3G;
731 build_dentry_tree_ntfs(struct wim_dentry **root_p,
733 struct add_image_params *params)
739 DEBUG("Mounting NTFS volume `%s' read-only", device);
741 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
744 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
745 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
746 * seem to contain any explicit version information. So we have to rely on a
747 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
749 #ifdef HAVE_NTFS_MNT_RDONLY
751 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
752 #elif defined(MS_RDONLY)
753 /* NTFS-3g 2011, 2012 */
754 vol = ntfs_mount(device, MS_RDONLY);
756 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
759 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
761 return WIMLIB_ERR_NTFS_3G;
763 ntfs_open_secure(vol);
765 /* We don't want to capture the special NTFS files such as $Bitmap. Not
766 * to be confused with "hidden" or "system" files which are real files
767 * that we do need to capture. */
768 NVolClearShowSysFiles(vol);
770 DEBUG("Opening root NTFS dentry");
771 root_ni = ntfs_inode_open(vol, FILE_root);
773 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
775 ret = WIMLIB_ERR_NTFS_3G;
779 /* Currently we assume that all the paths fit into this length and there
780 * is no check for overflow. */
781 char *path = MALLOC(32768);
783 ERROR("Could not allocate memory for NTFS pathname");
784 ret = WIMLIB_ERR_NOMEM;
790 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
791 FILE_NAME_POSIX, vol, params);
794 ntfs_inode_close(root_ni);
796 ntfs_index_ctx_put(vol->secure_xsii);
797 ntfs_index_ctx_put(vol->secure_xsdh);
798 ntfs_inode_close(vol->secure_ni);
801 if (do_ntfs_umount(vol)) {
802 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
805 ret = WIMLIB_ERR_NTFS_3G;
808 /* We need to leave the NTFS volume mounted so that we can read
809 * the NTFS files again when we are actually writing the WIM */
810 *(ntfs_volume**)params->extra_arg = vol;
814 #endif /* WITH_NTFS_3G */