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 <time.h> /* NTFS-3g headers are missing <time.h> include */
43 #include <ntfs-3g/attrib.h>
44 #include <ntfs-3g/endians.h>
45 #include <ntfs-3g/layout.h>
46 #include <ntfs-3g/misc.h>
47 #include <ntfs-3g/reparse.h>
48 #include <ntfs-3g/security.h> /* ntfs-3g/security.h before ntfs-3g/xattrs.h */
49 #include <ntfs-3g/types.h>
50 #include <ntfs-3g/volume.h>
51 #include <ntfs-3g/xattrs.h>
53 #include <ntfs-3g/acls.h> /* This should be included last as it requires
54 definitions from above not included by itself */
56 #include "wimlib/capture.h"
57 #include "wimlib/dentry.h"
58 #include "wimlib/encoding.h"
59 #include "wimlib/endianness.h"
60 #include "wimlib/error.h"
61 #include "wimlib/lookup_table.h"
62 #include "wimlib/ntfs_3g.h"
63 #include "wimlib/paths.h"
64 #include "wimlib/security.h"
66 static inline ntfschar *
67 attr_record_name(ATTR_RECORD *ar)
69 return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
73 open_ntfs_attr(ntfs_inode *ni, struct ntfs_location *loc)
77 na = ntfs_attr_open(ni,
78 loc->is_reparse_point ? AT_REPARSE_POINT : AT_DATA,
80 loc->stream_name_nchars);
82 ERROR_WITH_ERRNO("Failed to open attribute of \"%"TS"\" in "
83 "NTFS volume", loc->path);
89 read_ntfs_file_prefix(const struct wim_lookup_table_entry *lte,
91 consume_data_callback_t cb,
95 struct ntfs_location *loc = lte->ntfs_loc;
96 ntfs_volume *vol = loc->ntfs_vol;
104 ni = ntfs_pathname_to_inode(vol, NULL, loc->path);
106 ERROR_WITH_ERRNO("Can't find NTFS inode for \"%"TS"\"", loc->path);
107 ret = WIMLIB_ERR_NTFS_3G;
111 na = open_ntfs_attr(ni, loc);
113 ret = WIMLIB_ERR_NTFS_3G;
114 goto out_close_ntfs_inode;
118 out_buf = alloca(WIM_CHUNK_SIZE);
120 out_buf = ctx_or_buf;
121 pos = (loc->is_reparse_point) ? 8 : 0;
122 bytes_remaining = size;
123 while (bytes_remaining) {
124 s64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
125 if (ntfs_attr_pread(na, pos, to_read, out_buf) != to_read) {
126 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
127 ret = WIMLIB_ERR_NTFS_3G;
128 goto out_close_ntfs_attr;
131 bytes_remaining -= to_read;
133 ret = cb(out_buf, to_read, ctx_or_buf);
135 goto out_close_ntfs_attr;
143 out_close_ntfs_inode:
144 ntfs_inode_close(ni);
150 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
151 u32 *reparse_tag_ret)
157 na = open_ntfs_attr(ni, loc);
159 ret = WIMLIB_ERR_NTFS_3G;
163 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
164 &reparse_tag) != sizeof(reparse_tag))
166 ERROR_WITH_ERRNO("Error reading reparse data");
167 ret = WIMLIB_ERR_NTFS_3G;
168 goto out_close_ntfs_attr;
170 *reparse_tag_ret = le32_to_cpu(reparse_tag);
171 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
180 /* Load the streams from a file or reparse point in the NTFS volume into the WIM
183 capture_ntfs_streams(struct wim_inode *inode,
187 struct wim_lookup_table *lookup_table,
191 ntfs_attr_search_ctx *actx;
192 struct ntfs_location *ntfs_loc;
194 struct wim_lookup_table_entry *lte;
196 DEBUG2("Capturing NTFS data streams from `%s'", path);
198 /* Get context to search the streams of the NTFS file. */
199 actx = ntfs_attr_get_search_ctx(ni, NULL);
201 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
203 return WIMLIB_ERR_NTFS_3G;
206 /* Capture each data stream or reparse data stream. */
207 while (!ntfs_attr_lookup(type, NULL, 0,
208 CASE_SENSITIVE, 0, NULL, 0, actx))
210 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
211 u64 name_length = actx->attr->name_length;
214 if (data_size == 0) {
215 /* Empty stream. No lookup table entry is needed. */
219 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
221 ret = WIMLIB_ERR_NOMEM;
224 ntfs_loc->ntfs_vol = vol;
225 ntfs_loc->path = memdup(path, path_len + 1);
226 if (!ntfs_loc->path) {
227 ret = WIMLIB_ERR_NOMEM;
228 goto out_free_ntfs_loc;
231 ntfs_loc->stream_name = memdup(attr_record_name(actx->attr),
233 if (!ntfs_loc->stream_name) {
234 ret = WIMLIB_ERR_NOMEM;
235 goto out_free_ntfs_loc;
237 ntfs_loc->stream_name_nchars = name_length;
240 lte = new_lookup_table_entry();
242 ret = WIMLIB_ERR_NOMEM;
243 goto out_free_ntfs_loc;
245 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
246 lte->ntfs_loc = ntfs_loc;
248 if (type == AT_REPARSE_POINT) {
250 ERROR("Invalid reparse data (only %u bytes)!",
251 (unsigned)data_size);
252 ret = WIMLIB_ERR_NTFS_3G;
255 lte->ntfs_loc->is_reparse_point = true;
256 lte->resource_entry.original_size = data_size - 8;
257 ret = read_reparse_tag(ni, lte->ntfs_loc,
258 &inode->i_reparse_tag);
262 lte->ntfs_loc->is_reparse_point = false;
263 lte->resource_entry.original_size = data_size;
266 if (name_length == 0) {
267 /* Unnamed data stream. Put the reference to it in the
270 ERROR("Found two un-named data streams for `%s'",
272 ret = WIMLIB_ERR_NTFS_3G;
278 /* Named data stream. Put the reference to it in the
279 * alternate data stream entries */
280 struct wim_ads_entry *new_ads_entry;
282 new_ads_entry = inode_add_ads_utf16le(inode,
283 attr_record_name(actx->attr),
285 if (!new_ads_entry) {
286 ret = WIMLIB_ERR_NOMEM;
289 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
290 stream_id = new_ads_entry->stream_id;
291 new_ads_entry->lte = lte;
294 lookup_table_insert_unhashed(lookup_table, lte,
298 if (errno == ENOENT) {
301 ERROR_WITH_ERRNO("Error listing NTFS attributes from `%s'",
303 ret = WIMLIB_ERR_NTFS_3G;
307 free_lookup_table_entry(lte);
310 FREE(ntfs_loc->path);
311 FREE(ntfs_loc->stream_name);
315 ntfs_attr_put_search_ctx(actx);
317 DEBUG2("Successfully captured NTFS streams from `%s'", path);
319 ERROR("Failed to capture NTFS streams from `%s'", path);
323 /* Red-black tree that maps NTFS inode numbers to DOS names */
324 struct dos_name_map {
325 struct rb_root rb_root;
328 struct dos_name_node {
329 struct rb_node rb_node;
335 /* Inserts a new DOS name into the map */
337 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
338 size_t name_nbytes, u64 ntfs_ino)
340 struct dos_name_node *new_node;
342 struct rb_root *root;
343 struct rb_node *rb_parent;
345 DEBUG("DOS name_len = %zu", name_nbytes);
346 new_node = MALLOC(sizeof(struct dos_name_node));
350 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
351 * assuming 2-byte ntfs characters) */
352 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
354 /* Initialize the DOS name, DOS name length, and NTFS inode number of
355 * the red-black tree node */
356 memcpy(new_node->dos_name, dos_name, name_nbytes);
357 new_node->name_nbytes = name_nbytes;
358 new_node->ntfs_ino = ntfs_ino;
360 /* Insert the red-black tree node */
361 root = &map->rb_root;
365 struct dos_name_node *this;
367 this = container_of(*p, struct dos_name_node, rb_node);
369 if (new_node->ntfs_ino < this->ntfs_ino)
370 p = &((*p)->rb_left);
371 else if (new_node->ntfs_ino > this->ntfs_ino)
372 p = &((*p)->rb_right);
374 /* This should be impossible since a NTFS inode cannot
375 * have multiple DOS names, and we only should get each
376 * DOS name entry once from the ntfs_readdir() calls. */
377 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
382 rb_link_node(&new_node->rb_node, rb_parent, p);
383 rb_insert_color(&new_node->rb_node, root);
384 DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
388 /* Returns a structure that contains the DOS name and its length for a NTFS
389 * inode, or NULL if the inode has no DOS name. */
390 static struct dos_name_node *
391 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
393 struct rb_node *node = map->rb_root.rb_node;
395 struct dos_name_node *this;
396 this = container_of(node, struct dos_name_node, rb_node);
397 if (ntfs_ino < this->ntfs_ino)
398 node = node->rb_left;
399 else if (ntfs_ino > this->ntfs_ino)
400 node = node->rb_right;
408 set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
410 const struct dos_name_map *map = arg;
411 const struct dos_name_node *node;
413 if (dentry->is_win32_name) {
414 node = lookup_dos_name(map, dentry->d_inode->i_ino);
416 dentry->short_name = MALLOC(node->name_nbytes + 2);
417 if (!dentry->short_name)
418 return WIMLIB_ERR_NOMEM;
419 memcpy(dentry->short_name, node->dos_name,
421 dentry->short_name[node->name_nbytes / 2] = 0;
422 dentry->short_name_nbytes = node->name_nbytes;
423 DEBUG("Assigned DOS name to ino %"PRIu64,
424 dentry->d_inode->i_ino);
426 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
427 "corresponding DOS name",
428 dentry->d_inode->i_ino);
435 free_dos_name_tree(struct rb_node *node) {
437 free_dos_name_tree(node->rb_left);
438 free_dos_name_tree(node->rb_right);
439 FREE(container_of(node, struct dos_name_node, rb_node));
444 destroy_dos_name_map(struct dos_name_map *map)
446 free_dos_name_tree(map->rb_root.rb_node);
450 struct wim_dentry *parent;
454 struct dos_name_map *dos_name_map;
456 struct add_image_params *params;
460 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
466 ntfs_volume *ntfs_vol,
467 struct add_image_params *params);
470 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
471 const int name_nchars, const int name_type,
472 const s64 pos, const MFT_REF mref,
473 const unsigned dt_type)
475 struct readdir_ctx *ctx;
476 size_t mbs_name_nbytes;
478 struct wim_dentry *child;
481 size_t name_nbytes = name_nchars * sizeof(ntfschar);
484 if (name_type & FILE_NAME_DOS) {
485 /* If this is the entry for a DOS name, store it for later. */
486 ret = insert_dos_name(ctx->dos_name_map, name,
487 name_nbytes, mref & MFT_REF_MASK_CPU);
489 /* Return now if an error occurred or if this is just a DOS name
490 * and not a Win32+DOS name. */
491 if (ret != 0 || name_type == FILE_NAME_DOS)
494 ret = utf16le_to_tstr(name, name_nbytes,
495 &mbs_name, &mbs_name_nbytes);
499 if (mbs_name[0] == '.' &&
500 (mbs_name[1] == '\0' ||
501 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
504 * note: name_type is POSIX for these, so DOS names will not
505 * have been inserted for them. */
507 goto out_free_mbs_name;
510 /* Open the inode for this directory entry and recursively capture the
511 * directory tree rooted at it */
512 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
514 ERROR_WITH_ERRNO("Failed to open NTFS inode");
516 goto out_free_mbs_name;
518 path_len = ctx->path_len;
520 ctx->path[path_len++] = '/';
521 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
522 path_len += mbs_name_nbytes;
524 ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
525 ni, ctx->path, path_len, name_type,
526 ctx->vol, ctx->params);
528 dentry_add_child(ctx->parent, child);
529 ntfs_inode_close(ni);
536 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
537 * At the same time, update the WIM lookup table with lookup table entries for
538 * the NTFS streams, and build an array of security descriptors.
541 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
548 struct add_image_params *params)
552 struct wim_dentry *root;
553 struct wim_inode *inode;
554 ATTR_TYPES stream_type;
556 if (exclude_path(path, path_len, params->config, false)) {
557 /* Exclude a file or directory tree based on the capture
558 * configuration file */
559 if ((params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE)
560 && params->progress_func)
562 union wimlib_progress_info info;
563 info.scan.cur_path = path;
564 info.scan.excluded = true;
565 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
572 /* Get file attributes */
573 struct SECURITY_CONTEXT ctx;
574 memset(&ctx, 0, sizeof(ctx));
576 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
577 ni, dir_ni, (char *)&attributes,
579 if (ret != sizeof(attributes)) {
580 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
582 return WIMLIB_ERR_NTFS_3G;
585 if ((params->add_flags & WIMLIB_ADD_FLAG_VERBOSE)
586 && params->progress_func)
588 union wimlib_progress_info info;
589 info.scan.cur_path = path;
590 info.scan.excluded = false;
591 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
594 /* Create a WIM dentry with an associated inode, which may be shared */
595 ret = inode_table_new_dentry(¶ms->inode_table,
596 path_basename_with_len(path, path_len),
597 ni->mft_no, 0, false, &root);
601 inode = root->d_inode;
603 if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
606 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
607 root->is_win32_name = 1;
608 inode->i_creation_time = le64_to_cpu(ni->creation_time);
609 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
610 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
611 inode->i_attributes = le32_to_cpu(attributes);
612 inode->i_resolved = 1;
614 if (attributes & FILE_ATTR_REPARSE_POINT)
615 stream_type = AT_REPARSE_POINT;
617 stream_type = AT_DATA;
619 /* Capture the file's streams; more specifically, this is supposed to:
621 * - Regular files: capture unnamed data stream and any named data
623 * - Directories: capture any named data streams
624 * - Reparse points: capture reparse data only
626 ret = capture_ntfs_streams(inode, ni, path, path_len,
627 params->lookup_table, vol, stream_type);
631 if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
633 /* Recurse to directory children */
635 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
636 struct readdir_ctx ctx = {
640 .path_len = path_len,
641 .dos_name_map = &dos_name_map,
645 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
647 ERROR_WITH_ERRNO("ntfs_readdir()");
648 ret = WIMLIB_ERR_NTFS_3G;
650 ret = for_dentry_child(root, set_dentry_dos_name,
653 destroy_dos_name_map(&dos_name_map);
658 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
659 * only allow capturing an entire volume. */
660 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
661 inode_is_symlink(inode))
662 inode->i_not_rpfixed = 0;
664 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
665 /* Get security descriptor */
669 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
672 if (ret > sizeof(sd)) {
674 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
675 ni, dir_ni, sd, ret);
678 inode->i_security_id = sd_set_add_sd(¶ms->sd_set,
680 if (inode->i_security_id == -1) {
681 ERROR("Out of memory");
682 ret = WIMLIB_ERR_NOMEM;
685 DEBUG("Added security ID = %u for `%s'",
686 inode->i_security_id, path);
688 } else if (ret < 0) {
689 ERROR_WITH_ERRNO("Failed to get security information from "
691 ret = WIMLIB_ERR_NTFS_3G;
693 inode->i_security_id = -1;
694 DEBUG("No security ID for `%s'", path);
701 free_dentry_tree(root, params->lookup_table);
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 add_image_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, NULL, 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'",
791 ret = WIMLIB_ERR_NTFS_3G;
794 /* We need to leave the NTFS volume mounted so that we can read
795 * the NTFS files again when we are actually writing the WIM */
796 *(ntfs_volume**)params->extra_arg = vol;
800 #endif /* WITH_NTFS_3G */