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,
84 struct ntfs_location *loc = lte->ntfs_loc;
85 ntfs_volume *vol = loc->ntfs_vol;
93 ni = ntfs_pathname_to_inode(vol, NULL, loc->path);
95 ERROR_WITH_ERRNO("Can't find NTFS inode for \"%"TS"\"", loc->path);
96 ret = WIMLIB_ERR_NTFS_3G;
100 na = open_ntfs_attr(ni, loc);
102 ret = WIMLIB_ERR_NTFS_3G;
103 goto out_close_ntfs_inode;
106 pos = (loc->is_reparse_point) ? 8 : 0;
107 bytes_remaining = size;
108 while (bytes_remaining) {
109 s64 to_read = min(bytes_remaining, sizeof(buf));
110 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
111 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
112 ret = WIMLIB_ERR_NTFS_3G;
113 goto out_close_ntfs_attr;
116 bytes_remaining -= to_read;
117 ret = cb(buf, to_read, cb_ctx);
119 goto out_close_ntfs_attr;
124 out_close_ntfs_inode:
125 ntfs_inode_close(ni);
131 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
132 u32 *reparse_tag_ret)
138 na = open_ntfs_attr(ni, loc);
140 ret = WIMLIB_ERR_NTFS_3G;
144 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
145 &reparse_tag) != sizeof(reparse_tag))
147 ERROR_WITH_ERRNO("Error reading reparse data");
148 ret = WIMLIB_ERR_NTFS_3G;
149 goto out_close_ntfs_attr;
151 *reparse_tag_ret = le32_to_cpu(reparse_tag);
152 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
161 /* Load the streams from a file or reparse point in the NTFS volume into the WIM
164 capture_ntfs_streams(struct wim_inode *inode,
168 struct wim_lookup_table *lookup_table,
172 ntfs_attr_search_ctx *actx;
173 struct ntfs_location *ntfs_loc;
175 struct wim_lookup_table_entry *lte;
177 DEBUG2("Capturing NTFS data streams from `%s'", path);
179 /* Get context to search the streams of the NTFS file. */
180 actx = ntfs_attr_get_search_ctx(ni, NULL);
182 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
183 "context for \"%s\"", path);
184 return WIMLIB_ERR_NTFS_3G;
187 /* Capture each data stream or reparse data stream. */
188 while (!ntfs_attr_lookup(type, NULL, 0,
189 CASE_SENSITIVE, 0, NULL, 0, actx))
191 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
192 u64 name_length = actx->attr->name_length;
195 if (data_size == 0) {
196 /* Empty stream. No lookup table entry is needed. */
200 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
202 ret = WIMLIB_ERR_NOMEM;
205 ntfs_loc->ntfs_vol = vol;
206 ntfs_loc->path = memdup(path, path_len + 1);
207 if (!ntfs_loc->path) {
208 ret = WIMLIB_ERR_NOMEM;
209 goto out_free_ntfs_loc;
212 ntfs_loc->stream_name = memdup(attr_record_name(actx->attr),
214 if (!ntfs_loc->stream_name) {
215 ret = WIMLIB_ERR_NOMEM;
216 goto out_free_ntfs_loc;
218 ntfs_loc->stream_name_nchars = name_length;
221 lte = new_lookup_table_entry();
223 ret = WIMLIB_ERR_NOMEM;
224 goto out_free_ntfs_loc;
226 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
227 lte->ntfs_loc = ntfs_loc;
229 if (type == AT_REPARSE_POINT) {
231 ERROR("Invalid reparse data on \"%s\" "
232 "(only %u bytes)!", path, (unsigned)data_size);
233 ret = WIMLIB_ERR_NTFS_3G;
236 lte->ntfs_loc->is_reparse_point = true;
237 lte->size = data_size - 8;
238 ret = read_reparse_tag(ni, lte->ntfs_loc,
239 &inode->i_reparse_tag);
243 lte->ntfs_loc->is_reparse_point = false;
244 lte->size = data_size;
247 if (name_length == 0) {
248 /* Unnamed data stream. Put the reference to it in the
252 ERROR("Found two un-named data streams for \"%s\" "
253 "(sizes = %"PRIu64", %"PRIu64")",
254 path, inode->i_lte->size,
256 ret = WIMLIB_ERR_NTFS_3G;
264 /* Named data stream. Put the reference to it in the
265 * alternate data stream entries */
266 struct wim_ads_entry *new_ads_entry;
268 new_ads_entry = inode_add_ads_utf16le(inode,
269 attr_record_name(actx->attr),
271 if (!new_ads_entry) {
272 ret = WIMLIB_ERR_NOMEM;
275 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
276 stream_id = new_ads_entry->stream_id;
277 new_ads_entry->lte = lte;
280 lookup_table_insert_unhashed(lookup_table, lte,
284 if (errno == ENOENT) {
287 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
288 ret = WIMLIB_ERR_NTFS_3G;
292 free_lookup_table_entry(lte);
295 FREE(ntfs_loc->path);
296 FREE(ntfs_loc->stream_name);
300 ntfs_attr_put_search_ctx(actx);
302 DEBUG2("Successfully captured NTFS streams from \"%s\"", path);
304 ERROR("Failed to capture NTFS streams from \"%s\"", path);
308 /* Red-black tree that maps NTFS inode numbers to DOS names */
309 struct dos_name_map {
310 struct rb_root rb_root;
313 struct dos_name_node {
314 struct rb_node rb_node;
320 /* Inserts a new DOS name into the map */
322 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
323 size_t name_nbytes, le64 ntfs_ino)
325 struct dos_name_node *new_node;
327 struct rb_root *root;
328 struct rb_node *rb_parent;
330 DEBUG("DOS name_len = %zu", name_nbytes);
331 new_node = MALLOC(sizeof(struct dos_name_node));
335 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
336 * assuming 2-byte ntfs characters) */
337 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
339 /* Initialize the DOS name, DOS name length, and NTFS inode number of
340 * the red-black tree node */
341 memcpy(new_node->dos_name, dos_name, name_nbytes);
342 new_node->name_nbytes = name_nbytes;
343 new_node->ntfs_ino = ntfs_ino;
345 /* Insert the red-black tree node */
346 root = &map->rb_root;
350 struct dos_name_node *this;
352 this = container_of(*p, struct dos_name_node, rb_node);
354 if (new_node->ntfs_ino < this->ntfs_ino)
355 p = &((*p)->rb_left);
356 else if (new_node->ntfs_ino > this->ntfs_ino)
357 p = &((*p)->rb_right);
359 /* This should be impossible since a 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));
367 rb_link_node(&new_node->rb_node, rb_parent, p);
368 rb_insert_color(&new_node->rb_node, root);
369 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
373 /* Returns a structure that contains the DOS name and its length for a NTFS
374 * inode, or NULL if the inode has no DOS name. */
375 static struct dos_name_node *
376 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
378 struct rb_node *node = map->rb_root.rb_node;
380 struct dos_name_node *this;
381 this = container_of(node, struct dos_name_node, rb_node);
382 if (ntfs_ino < this->ntfs_ino)
383 node = node->rb_left;
384 else if (ntfs_ino > this->ntfs_ino)
385 node = node->rb_right;
393 set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
395 const struct dos_name_map *map = arg;
396 const struct dos_name_node *node;
398 if (dentry->is_win32_name) {
399 node = lookup_dos_name(map, dentry->d_inode->i_ino);
401 dentry->short_name = MALLOC(node->name_nbytes + 2);
402 if (!dentry->short_name)
403 return WIMLIB_ERR_NOMEM;
404 memcpy(dentry->short_name, node->dos_name,
406 dentry->short_name[node->name_nbytes / 2] = 0;
407 dentry->short_name_nbytes = node->name_nbytes;
408 DEBUG("Assigned DOS name to ino %"PRIu64,
409 dentry->d_inode->i_ino);
411 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
412 "corresponding DOS name",
413 dentry->d_inode->i_ino);
420 free_dos_name_tree(struct rb_node *node) {
422 free_dos_name_tree(node->rb_left);
423 free_dos_name_tree(node->rb_right);
424 FREE(container_of(node, struct dos_name_node, rb_node));
429 destroy_dos_name_map(struct dos_name_map *map)
431 free_dos_name_tree(map->rb_root.rb_node);
435 struct wim_dentry *parent;
438 struct dos_name_map *dos_name_map;
440 struct add_image_params *params;
444 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
449 ntfs_volume *ntfs_vol,
450 struct add_image_params *params);
453 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
454 const int name_nchars, const int name_type,
455 const s64 pos, const MFT_REF mref,
456 const unsigned dt_type)
458 struct readdir_ctx *ctx;
459 size_t mbs_name_nbytes;
461 struct wim_dentry *child;
464 size_t name_nbytes = name_nchars * sizeof(ntfschar);
467 if (name_type & FILE_NAME_DOS) {
468 /* If this is the entry for a DOS name, store it for later. */
469 ret = insert_dos_name(ctx->dos_name_map, name,
470 name_nbytes, mref & MFT_REF_MASK_CPU);
472 /* Return now if an error occurred or if this is just a DOS name
473 * and not a Win32+DOS name. */
474 if (ret != 0 || name_type == FILE_NAME_DOS)
477 ret = utf16le_to_tstr(name, name_nbytes,
478 &mbs_name, &mbs_name_nbytes);
482 if (mbs_name[0] == '.' &&
483 (mbs_name[1] == '\0' ||
484 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
487 * note: name_type is POSIX for these, so DOS names will not
488 * have been inserted for them. */
490 goto out_free_mbs_name;
493 /* Open the inode for this directory entry and recursively capture the
494 * directory tree rooted at it */
495 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
497 /* XXX This used to be treated as an error, but NTFS-3g seemed
498 * to be unable to read some inodes on a Windows 8 image for
500 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
501 ctx->path, mbs_name);
503 goto out_free_mbs_name;
505 path_len = ctx->path_len;
507 ctx->path[path_len++] = '/';
508 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
509 path_len += mbs_name_nbytes;
511 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
513 ctx->vol, ctx->params);
514 path_len -= mbs_name_nbytes + 1;
516 dentry_add_child(ctx->parent, child);
517 ntfs_inode_close(ni);
521 ctx->path[ctx->path_len] = '\0';
525 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
526 * At the same time, update the WIM lookup table with lookup table entries for
527 * the NTFS streams, and build an array of security descriptors.
530 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
536 struct add_image_params *params)
540 struct wim_dentry *root = NULL;
541 struct wim_inode *inode;
542 ATTR_TYPES stream_type;
544 params->progress.scan.cur_path = path;
546 if (exclude_path(path, path_len, params->config, false)) {
547 /* Exclude a file or directory tree based on the capture
548 * configuration file */
549 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED);
554 /* Get file attributes */
555 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
556 if (ret != sizeof(attributes)) {
557 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
558 ret = WIMLIB_ERR_NTFS_3G;
562 if ((attributes & (FILE_ATTRIBUTE_DIRECTORY |
563 FILE_ATTRIBUTE_ENCRYPTED)) == FILE_ATTRIBUTE_ENCRYPTED)
565 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
567 ERROR("Can't archive unsupported encrypted file \"%s\"", path);
568 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
571 do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED);
576 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK);
578 /* Create a WIM dentry with an associated inode, which may be shared */
579 ret = inode_table_new_dentry(¶ms->inode_table,
580 path_basename_with_len(path, path_len),
581 ni->mft_no, 0, false, &root);
585 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
586 root->is_win32_name = 1;
588 inode = root->d_inode;
590 if (inode->i_nlink > 1) {
591 /* Shared inode; nothing more to do */
596 inode->i_creation_time = le64_to_cpu(ni->creation_time);
597 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
598 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
599 inode->i_attributes = le32_to_cpu(attributes);
600 inode->i_resolved = 1;
602 if (attributes & FILE_ATTR_REPARSE_POINT)
603 stream_type = AT_REPARSE_POINT;
605 stream_type = AT_DATA;
607 /* Capture the file's streams; more specifically, this is supposed to:
609 * - Regular files: capture unnamed data stream and any named data
611 * - Directories: capture any named data streams
612 * - Reparse points: capture reparse data only
614 ret = capture_ntfs_streams(inode, ni, path, path_len,
615 params->lookup_table, vol, stream_type);
619 if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
621 /* Recurse to directory children */
623 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
624 struct readdir_ctx ctx = {
627 .path_len = path_len,
628 .dos_name_map = &dos_name_map,
632 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
634 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
635 ret = WIMLIB_ERR_NTFS_3G;
637 ret = for_dentry_child(root, set_dentry_dos_name,
640 destroy_dos_name_map(&dos_name_map);
645 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
646 * only allow capturing an entire volume. */
647 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
648 inode_is_symlink(inode))
649 inode->i_not_rpfixed = 0;
651 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
652 struct SECURITY_CONTEXT sec_ctx;
656 /* Get security descriptor */
657 memset(&sec_ctx, 0, sizeof(sec_ctx));
662 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
663 if (ret > sizeof(_sd)) {
665 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
668 inode->i_security_id = sd_set_add_sd(¶ms->sd_set,
670 if (inode->i_security_id == -1) {
671 ERROR("Out of memory");
672 ret = WIMLIB_ERR_NOMEM;
675 DEBUG("Added security ID = %u for `%s'",
676 inode->i_security_id, path);
678 } else if (ret < 0) {
679 ERROR_WITH_ERRNO("Failed to get security information from "
681 ret = WIMLIB_ERR_NTFS_3G;
683 inode->i_security_id = -1;
684 DEBUG("No security ID for `%s'", path);
691 free_dentry_tree(root, params->lookup_table);
697 do_ntfs_umount(struct _ntfs_volume *vol)
699 DEBUG("Unmounting NTFS volume");
700 if (ntfs_umount(vol, FALSE))
701 return WIMLIB_ERR_NTFS_3G;
707 build_dentry_tree_ntfs(struct wim_dentry **root_p,
709 struct add_image_params *params)
715 DEBUG("Mounting NTFS volume `%s' read-only", device);
717 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
720 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
721 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
722 * seem to contain any explicit version information. So we have to rely on a
723 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
725 #ifdef HAVE_NTFS_MNT_RDONLY
727 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
728 #elif defined(MS_RDONLY)
729 /* NTFS-3g 2011, 2012 */
730 vol = ntfs_mount(device, MS_RDONLY);
732 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
735 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
737 return WIMLIB_ERR_NTFS_3G;
739 ntfs_open_secure(vol);
741 /* We don't want to capture the special NTFS files such as $Bitmap. Not
742 * to be confused with "hidden" or "system" files which are real files
743 * that we do need to capture. */
744 NVolClearShowSysFiles(vol);
746 DEBUG("Opening root NTFS dentry");
747 root_ni = ntfs_inode_open(vol, FILE_root);
749 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
751 ret = WIMLIB_ERR_NTFS_3G;
755 /* Currently we assume that all the paths fit into this length and there
756 * is no check for overflow. */
757 char *path = MALLOC(32768);
759 ERROR("Could not allocate memory for NTFS pathname");
760 ret = WIMLIB_ERR_NOMEM;
766 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
767 FILE_NAME_POSIX, vol, params);
770 ntfs_inode_close(root_ni);
772 ntfs_index_ctx_put(vol->secure_xsii);
773 ntfs_index_ctx_put(vol->secure_xsdh);
774 ntfs_inode_close(vol->secure_ni);
777 if (do_ntfs_umount(vol)) {
778 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
781 ret = WIMLIB_ERR_NTFS_3G;
784 /* We need to leave the NTFS volume mounted so that we can read
785 * the NTFS files again when we are actually writing the WIM */
786 *(ntfs_volume**)params->extra_arg = vol;
790 #endif /* WITH_NTFS_3G */