4 * Capture a WIM image from a NTFS volume. We capture everything we can,
5 * 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/.
30 #include <ntfs-3g/endians.h>
31 #include <ntfs-3g/types.h>
33 #include "buffer_io.h"
35 #include "lookup_table.h"
37 #include "wimlib_internal.h"
39 #include <ntfs-3g/layout.h>
40 #include <ntfs-3g/acls.h>
41 #include <ntfs-3g/attrib.h>
42 #include <ntfs-3g/misc.h>
43 #include <ntfs-3g/reparse.h>
44 #include <ntfs-3g/security.h> /* ntfs-3g/security.h before ntfs-3g/xattrs.h */
45 #include <ntfs-3g/xattrs.h>
46 #include <ntfs-3g/volume.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;
107 out_buf = alloca(WIM_CHUNK_SIZE);
109 out_buf = ctx_or_buf;
110 pos = (loc->is_reparse_point) ? 8 : 0;
111 bytes_remaining = size;
112 while (bytes_remaining) {
113 s64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
114 if (ntfs_attr_pread(na, pos, to_read, out_buf) != to_read) {
115 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
116 ret = WIMLIB_ERR_NTFS_3G;
117 goto out_close_ntfs_attr;
120 bytes_remaining -= to_read;
122 ret = cb(out_buf, to_read, ctx_or_buf);
124 goto out_close_ntfs_attr;
132 out_close_ntfs_inode:
133 ntfs_inode_close(ni);
139 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
140 u32 *reparse_tag_ret)
146 na = open_ntfs_attr(ni, loc);
148 ret = WIMLIB_ERR_NTFS_3G;
152 if (ntfs_attr_pread(na, 0, 8, buf) != 8) {
153 ERROR_WITH_ERRNO("Error reading reparse data");
154 ret = WIMLIB_ERR_NTFS_3G;
155 goto out_close_ntfs_attr;
157 *reparse_tag_ret = le32_to_cpu(*(u32*)buf);
158 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
167 /* Load the streams from a file or reparse point in the NTFS volume into the WIM
170 capture_ntfs_streams(struct wim_inode *inode,
174 struct wim_lookup_table *lookup_table,
178 ntfs_attr_search_ctx *actx;
179 struct ntfs_location *ntfs_loc;
181 struct wim_lookup_table_entry *lte;
183 DEBUG2("Capturing NTFS data streams from `%s'", path);
185 /* Get context to search the streams of the NTFS file. */
186 actx = ntfs_attr_get_search_ctx(ni, NULL);
188 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
190 return WIMLIB_ERR_NTFS_3G;
193 /* Capture each data stream or reparse data stream. */
194 while (!ntfs_attr_lookup(type, NULL, 0,
195 CASE_SENSITIVE, 0, NULL, 0, actx))
197 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
198 u64 name_length = actx->attr->name_length;
201 if (data_size == 0) {
202 /* Empty stream. No lookup table entry is needed. */
206 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
208 ret = WIMLIB_ERR_NOMEM;
211 ntfs_loc->ntfs_vol = vol;
212 ntfs_loc->path = MALLOC(path_len + 1);
213 if (!ntfs_loc->path) {
214 ret = WIMLIB_ERR_NOMEM;
215 goto out_free_ntfs_loc;
217 memcpy(ntfs_loc->path, path, path_len + 1);
219 ntfs_loc->stream_name = MALLOC(name_length * 2);
220 if (!ntfs_loc->stream_name) {
221 ret = WIMLIB_ERR_NOMEM;
222 goto out_free_ntfs_loc;
224 memcpy(ntfs_loc->stream_name,
225 attr_record_name(actx->attr),
226 actx->attr->name_length * 2);
227 ntfs_loc->stream_name_nchars = name_length;
230 lte = new_lookup_table_entry();
232 ret = WIMLIB_ERR_NOMEM;
233 goto out_free_ntfs_loc;
235 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
236 lte->ntfs_loc = ntfs_loc;
238 if (type == AT_REPARSE_POINT) {
240 ERROR("Invalid reparse data (only %u bytes)!",
241 (unsigned)data_size);
242 ret = WIMLIB_ERR_NTFS_3G;
245 lte->ntfs_loc->is_reparse_point = true;
246 lte->resource_entry.original_size = data_size - 8;
247 ret = read_reparse_tag(ni, lte->ntfs_loc,
248 &inode->i_reparse_tag);
252 lte->ntfs_loc->is_reparse_point = false;
253 lte->resource_entry.original_size = data_size;
256 if (name_length == 0) {
257 /* Unnamed data stream. Put the reference to it in the
260 ERROR("Found two un-named data streams for `%s'",
262 ret = WIMLIB_ERR_NTFS_3G;
268 /* Named data stream. Put the reference to it in the
269 * alternate data stream entries */
270 struct wim_ads_entry *new_ads_entry;
272 new_ads_entry = inode_add_ads_utf16le(inode,
273 attr_record_name(actx->attr),
275 if (!new_ads_entry) {
276 ret = WIMLIB_ERR_NOMEM;
279 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
280 stream_id = new_ads_entry->stream_id;
281 new_ads_entry->lte = lte;
284 lookup_table_insert_unhashed(lookup_table, lte,
288 if (errno == ENOENT) {
291 ERROR_WITH_ERRNO("Error listing NTFS attributes from `%s'",
293 ret = WIMLIB_ERR_NTFS_3G;
297 free_lookup_table_entry(lte);
300 FREE(ntfs_loc->path);
301 FREE(ntfs_loc->stream_name);
305 ntfs_attr_put_search_ctx(actx);
307 DEBUG2("Successfully captured NTFS streams from `%s'", path);
309 ERROR("Failed to capture NTFS streams from `%s'", path);
313 /* Red-black tree that maps NTFS inode numbers to DOS names */
314 struct dos_name_map {
315 struct rb_root rb_root;
318 struct dos_name_node {
319 struct rb_node rb_node;
325 /* Inserts a new DOS name into the map */
327 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
328 size_t name_nbytes, u64 ntfs_ino)
330 struct dos_name_node *new_node;
332 struct rb_root *root;
333 struct rb_node *rb_parent;
335 DEBUG("DOS name_len = %zu", name_nbytes);
336 new_node = MALLOC(sizeof(struct dos_name_node));
340 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
341 * assuming 2-byte ntfs characters) */
342 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
344 /* Initialize the DOS name, DOS name length, and NTFS inode number of
345 * the red-black tree node */
346 memcpy(new_node->dos_name, dos_name, name_nbytes);
347 new_node->name_nbytes = name_nbytes;
348 new_node->ntfs_ino = ntfs_ino;
350 /* Insert the red-black tree node */
351 root = &map->rb_root;
355 struct dos_name_node *this;
357 this = container_of(*p, struct dos_name_node, rb_node);
359 if (new_node->ntfs_ino < this->ntfs_ino)
360 p = &((*p)->rb_left);
361 else if (new_node->ntfs_ino > this->ntfs_ino)
362 p = &((*p)->rb_right);
364 /* This should be impossible since a NTFS inode cannot
365 * have multiple DOS names, and we only should get each
366 * DOS name entry once from the ntfs_readdir() calls. */
367 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
372 rb_link_node(&new_node->rb_node, rb_parent, p);
373 rb_insert_color(&new_node->rb_node, root);
374 DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
378 /* Returns a structure that contains the DOS name and its length for a NTFS
379 * inode, or NULL if the inode has no DOS name. */
380 static struct dos_name_node *
381 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
383 struct rb_node *node = map->rb_root.rb_node;
385 struct dos_name_node *this;
386 this = container_of(node, struct dos_name_node, rb_node);
387 if (ntfs_ino < this->ntfs_ino)
388 node = node->rb_left;
389 else if (ntfs_ino > this->ntfs_ino)
390 node = node->rb_right;
398 set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
400 const struct dos_name_map *map = arg;
401 const struct dos_name_node *node;
403 if (dentry->is_win32_name) {
404 node = lookup_dos_name(map, dentry->d_inode->i_ino);
406 dentry->short_name = MALLOC(node->name_nbytes + 2);
407 if (!dentry->short_name)
408 return WIMLIB_ERR_NOMEM;
409 memcpy(dentry->short_name, node->dos_name,
411 dentry->short_name[node->name_nbytes / 2] = 0;
412 dentry->short_name_nbytes = node->name_nbytes;
413 DEBUG("Assigned DOS name to ino %"PRIu64,
414 dentry->d_inode->i_ino);
416 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
417 "corresponding DOS name",
418 dentry->d_inode->i_ino);
425 free_dos_name_tree(struct rb_node *node) {
427 free_dos_name_tree(node->rb_left);
428 free_dos_name_tree(node->rb_right);
429 FREE(container_of(node, struct dos_name_node, rb_node));
434 destroy_dos_name_map(struct dos_name_map *map)
436 free_dos_name_tree(map->rb_root.rb_node);
440 struct wim_dentry *parent;
444 struct dos_name_map *dos_name_map;
446 struct add_image_params *params;
450 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
456 ntfs_volume *ntfs_vol,
457 struct add_image_params *params);
460 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
461 const int name_nchars, const int name_type,
462 const s64 pos, const MFT_REF mref,
463 const unsigned dt_type)
465 struct readdir_ctx *ctx;
466 size_t mbs_name_nbytes;
468 struct wim_dentry *child;
471 size_t name_nbytes = name_nchars * sizeof(ntfschar);
474 if (name_type & FILE_NAME_DOS) {
475 /* If this is the entry for a DOS name, store it for later. */
476 ret = insert_dos_name(ctx->dos_name_map, name,
477 name_nbytes, mref & MFT_REF_MASK_CPU);
479 /* Return now if an error occurred or if this is just a DOS name
480 * and not a Win32+DOS name. */
481 if (ret != 0 || name_type == FILE_NAME_DOS)
484 ret = utf16le_to_tstr(name, name_nbytes,
485 &mbs_name, &mbs_name_nbytes);
489 if (mbs_name[0] == '.' &&
490 (mbs_name[1] == '\0' ||
491 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
494 * note: name_type is POSIX for these, so DOS names will not
495 * have been inserted for them. */
497 goto out_free_mbs_name;
500 /* Open the inode for this directory entry and recursively capture the
501 * directory tree rooted at it */
502 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
504 ERROR_WITH_ERRNO("Failed to open NTFS inode");
506 goto out_free_mbs_name;
508 path_len = ctx->path_len;
510 ctx->path[path_len++] = '/';
511 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
512 path_len += mbs_name_nbytes;
514 ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
515 ni, ctx->path, path_len, name_type,
516 ctx->vol, ctx->params);
518 dentry_add_child(ctx->parent, child);
519 ntfs_inode_close(ni);
526 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
527 * At the same time, update the WIM lookup table with lookup table entries for
528 * the NTFS streams, and build an array of security descriptors.
531 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
538 struct add_image_params *params)
542 struct wim_dentry *root;
543 struct wim_inode *inode;
544 ATTR_TYPES stream_type;
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 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
550 && params->progress_func)
552 union wimlib_progress_info info;
553 info.scan.cur_path = path;
554 info.scan.excluded = true;
555 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
562 /* Get file attributes */
563 struct SECURITY_CONTEXT ctx;
564 memset(&ctx, 0, sizeof(ctx));
566 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
567 ni, dir_ni, (char *)&attributes,
570 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
572 return WIMLIB_ERR_NTFS_3G;
575 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
576 && params->progress_func)
578 union wimlib_progress_info info;
579 info.scan.cur_path = path;
580 info.scan.excluded = false;
581 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
584 /* Create a WIM dentry with an associated inode, which may be shared */
585 ret = inode_table_new_dentry(params->inode_table,
586 path_basename_with_len(path, path_len),
587 ni->mft_no, 0, false, &root);
591 inode = root->d_inode;
593 if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
596 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
597 root->is_win32_name = 1;
598 inode->i_creation_time = le64_to_cpu(ni->creation_time);
599 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
600 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
601 inode->i_attributes = le32_to_cpu(attributes);
602 inode->i_resolved = 1;
604 if (attributes & FILE_ATTR_REPARSE_POINT)
605 stream_type = AT_REPARSE_POINT;
607 stream_type = AT_DATA;
609 /* Capture the file's streams; more specifically, this is supposed to:
611 * - Regular files: capture unnamed data stream and any named data
613 * - Directories: capture any named data streams
614 * - Reparse points: capture reparse data only
616 ret = capture_ntfs_streams(inode, ni, path, path_len,
617 params->lookup_table, vol, stream_type);
621 if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
623 /* Recurse to directory children */
625 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
626 struct readdir_ctx ctx = {
630 .path_len = path_len,
631 .dos_name_map = &dos_name_map,
635 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
637 ERROR_WITH_ERRNO("ntfs_readdir()");
638 ret = WIMLIB_ERR_NTFS_3G;
640 ret = for_dentry_child(root, set_dentry_dos_name,
643 destroy_dos_name_map(&dos_name_map);
648 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
649 * only allow capturing an entire volume. */
650 if (params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_RPFIX &&
651 inode_is_symlink(inode))
652 inode->i_not_rpfixed = 0;
654 if (!(params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NO_ACLS)) {
655 /* Get security descriptor */
659 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
662 if (ret > sizeof(sd)) {
664 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
665 ni, dir_ni, sd, ret);
668 inode->i_security_id = sd_set_add_sd(params->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 #ifdef HAVE_NTFS_MNT_RDONLY
719 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
721 /* NTFS-3g 2011, 2012 */
722 vol = ntfs_mount(device, MS_RDONLY);
725 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
727 return WIMLIB_ERR_NTFS_3G;
729 ntfs_open_secure(vol);
731 /* We don't want to capture the special NTFS files such as $Bitmap. Not
732 * to be confused with "hidden" or "system" files which are real files
733 * that we do need to capture. */
734 NVolClearShowSysFiles(vol);
736 DEBUG("Opening root NTFS dentry");
737 root_ni = ntfs_inode_open(vol, FILE_root);
739 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
741 ret = WIMLIB_ERR_NTFS_3G;
745 /* Currently we assume that all the paths fit into this length and there
746 * is no check for overflow. */
747 char *path = MALLOC(32768);
749 ERROR("Could not allocate memory for NTFS pathname");
750 ret = WIMLIB_ERR_NOMEM;
756 ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
757 FILE_NAME_POSIX, vol, params);
760 ntfs_inode_close(root_ni);
762 ntfs_index_ctx_put(vol->secure_xsii);
763 ntfs_index_ctx_put(vol->secure_xsdh);
764 ntfs_inode_close(vol->secure_ni);
767 if (do_ntfs_umount(vol)) {
768 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
771 ret = WIMLIB_ERR_NTFS_3G;
774 /* We need to leave the NTFS volume mounted so that we can read
775 * the NTFS files again when we are actually writing the WIM */
776 *(ntfs_volume**)params->extra_arg = vol;