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 read_ntfs_file_prefix(const struct wim_lookup_table_entry *lte,
64 consume_data_callback_t cb,
68 struct ntfs_location *loc = lte->ntfs_loc;
69 ntfs_volume *vol = *loc->ntfs_vol_p;
77 ni = ntfs_pathname_to_inode(vol, NULL, loc->path);
79 ERROR_WITH_ERRNO("Can't find NTFS inode for \"%"TS"\"", loc->path);
80 ret = WIMLIB_ERR_NTFS_3G;
84 na = ntfs_attr_open(ni,
85 loc->is_reparse_point ? AT_REPARSE_POINT : AT_DATA,
87 loc->stream_name_nchars);
89 ERROR_WITH_ERRNO("Failed to open attribute of \"%"TS"\" in "
90 "NTFS volume", loc->path);
91 ret = WIMLIB_ERR_NTFS_3G;
92 goto out_close_ntfs_inode;
95 /*if (is_reparse_point) {*/
96 /*if (ntfs_attr_pread(na, 0, 8, buf) != 8)*/
98 /**reparse_tag_ret = le32_to_cpu(*(u32*)buf);*/
99 /*DEBUG("ReparseTag = %#x", *reparse_tag_ret);*/
101 /*bytes_remaining -= 8;*/
105 out_buf = alloca(WIM_CHUNK_SIZE);
107 out_buf = ctx_or_buf;
109 bytes_remaining = na->data_size;
110 while (bytes_remaining) {
111 s64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
112 if (ntfs_attr_pread(na, pos, to_read, out_buf) != to_read) {
113 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
114 ret = WIMLIB_ERR_NTFS_3G;
115 goto out_close_ntfs_attr;
118 bytes_remaining -= to_read;
120 ret = cb(out_buf, to_read, ctx_or_buf);
122 goto out_close_ntfs_attr;
130 out_close_ntfs_inode:
131 ntfs_inode_close(ni);
136 /* Load the streams from a file or reparse point in the NTFS volume into the WIM
139 capture_ntfs_streams(struct wim_inode *inode,
143 struct wim_lookup_table *lookup_table,
144 ntfs_volume **ntfs_vol_p,
147 ntfs_attr_search_ctx *actx;
148 struct ntfs_location *ntfs_loc = NULL;
150 struct wim_lookup_table_entry *lte;
152 DEBUG2("Capturing NTFS data streams from `%s'", path);
154 /* Get context to search the streams of the NTFS file. */
155 actx = ntfs_attr_get_search_ctx(ni, NULL);
157 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
159 return WIMLIB_ERR_NTFS_3G;
162 /* Capture each data stream or reparse data stream. */
163 while (!ntfs_attr_lookup(type, NULL, 0,
164 CASE_SENSITIVE, 0, NULL, 0, actx))
166 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
167 u64 name_length = actx->attr->name_length;
170 if (data_size == 0) {
172 ERROR_WITH_ERRNO("Failed to get size of attribute of "
174 ret = WIMLIB_ERR_NTFS_3G;
177 /* Empty stream. No lookup table entry is needed. */
180 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
183 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
184 ntfs_loc->path = MALLOC(path_len + 1);
186 goto out_free_ntfs_loc;
187 memcpy(ntfs_loc->path, path, path_len + 1);
189 ntfs_loc->stream_name = MALLOC(name_length * 2);
190 if (!ntfs_loc->stream_name)
191 goto out_free_ntfs_loc;
192 memcpy(ntfs_loc->stream_name,
193 attr_record_name(actx->attr),
194 actx->attr->name_length * 2);
195 ntfs_loc->stream_name_nchars = name_length;
198 lte = new_lookup_table_entry();
200 goto out_free_ntfs_loc;
201 lte->ntfs_loc = ntfs_loc;
202 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
204 if (type == AT_REPARSE_POINT) {
205 ntfs_loc->is_reparse_point = true;
206 lte->resource_entry.original_size = data_size - 8;
207 lte->resource_entry.size = data_size - 8;
209 ntfs_loc->is_reparse_point = false;
210 lte->resource_entry.original_size = data_size;
211 lte->resource_entry.size = data_size;
214 ntfs_loc->is_reparse_point = (type == AT_REPARSE_POINT);
215 lte->resource_entry.original_size = data_size;
219 if (name_length == 0) {
220 /* Unnamed data stream. Put the reference to it in the
223 ERROR("Found two un-named data streams for `%s'",
225 ret = WIMLIB_ERR_NTFS_3G;
231 /* Named data stream. Put the reference to it in the
232 * alternate data stream entries */
233 struct wim_ads_entry *new_ads_entry;
235 new_ads_entry = inode_add_ads_utf16le(inode,
236 attr_record_name(actx->attr),
240 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
241 stream_id = new_ads_entry->stream_id;
242 new_ads_entry->lte = lte;
245 lookup_table_insert_unhashed(lookup_table, lte,
252 free_lookup_table_entry(lte);
255 FREE(ntfs_loc->path);
256 FREE(ntfs_loc->stream_name);
260 ntfs_attr_put_search_ctx(actx);
262 DEBUG2("Successfully captured NTFS streams from `%s'", path);
264 ERROR("Failed to capture NTFS streams from `%s", path);
268 /* Red-black tree that maps NTFS inode numbers to DOS names */
269 struct dos_name_map {
270 struct rb_root rb_root;
273 struct dos_name_node {
274 struct rb_node rb_node;
280 /* Inserts a new DOS name into the map */
282 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
283 size_t name_nbytes, u64 ntfs_ino)
285 struct dos_name_node *new_node;
287 struct rb_root *root;
288 struct rb_node *rb_parent;
290 DEBUG("DOS name_len = %zu", name_nbytes);
291 new_node = MALLOC(sizeof(struct dos_name_node));
295 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
296 * assuming 2-byte ntfs characters) */
297 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
299 /* Initialize the DOS name, DOS name length, and NTFS inode number of
300 * the red-black tree node */
301 memcpy(new_node->dos_name, dos_name, name_nbytes);
302 new_node->name_nbytes = name_nbytes;
303 new_node->ntfs_ino = ntfs_ino;
305 /* Insert the red-black tree node */
306 root = &map->rb_root;
310 struct dos_name_node *this;
312 this = container_of(*p, struct dos_name_node, rb_node);
314 if (new_node->ntfs_ino < this->ntfs_ino)
315 p = &((*p)->rb_left);
316 else if (new_node->ntfs_ino > this->ntfs_ino)
317 p = &((*p)->rb_right);
319 /* This should be impossible since a NTFS inode cannot
320 * have multiple DOS names, and we only should get each
321 * DOS name entry once from the ntfs_readdir() calls. */
322 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
327 rb_link_node(&new_node->rb_node, rb_parent, p);
328 rb_insert_color(&new_node->rb_node, root);
329 DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
333 /* Returns a structure that contains the DOS name and its length for a NTFS
334 * inode, or NULL if the inode has no DOS name. */
335 static struct dos_name_node *
336 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
338 struct rb_node *node = map->rb_root.rb_node;
340 struct dos_name_node *this;
341 this = container_of(node, struct dos_name_node, rb_node);
342 if (ntfs_ino < this->ntfs_ino)
343 node = node->rb_left;
344 else if (ntfs_ino > this->ntfs_ino)
345 node = node->rb_right;
353 set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
355 const struct dos_name_map *map = arg;
356 const struct dos_name_node *node;
358 if (dentry->is_win32_name) {
359 node = lookup_dos_name(map, dentry->d_inode->i_ino);
361 dentry->short_name = MALLOC(node->name_nbytes + 2);
362 if (!dentry->short_name)
363 return WIMLIB_ERR_NOMEM;
364 memcpy(dentry->short_name, node->dos_name,
366 dentry->short_name[node->name_nbytes / 2] = 0;
367 dentry->short_name_nbytes = node->name_nbytes;
368 DEBUG("Assigned DOS name to ino %"PRIu64,
369 dentry->d_inode->i_ino);
371 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
372 "corresponding DOS name",
373 dentry->d_inode->i_ino);
380 free_dos_name_tree(struct rb_node *node) {
382 free_dos_name_tree(node->rb_left);
383 free_dos_name_tree(node->rb_right);
384 FREE(container_of(node, struct dos_name_node, rb_node));
389 destroy_dos_name_map(struct dos_name_map *map)
391 free_dos_name_tree(map->rb_root.rb_node);
395 struct wim_dentry *parent;
399 struct wim_lookup_table *lookup_table;
400 struct wim_inode_table *inode_table;
401 struct sd_set *sd_set;
402 struct dos_name_map *dos_name_map;
403 const struct wimlib_capture_config *config;
404 ntfs_volume **ntfs_vol_p;
406 wimlib_progress_func_t progress_func;
410 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
416 struct wim_lookup_table *lookup_table,
417 struct wim_inode_table *inode_table,
418 struct sd_set *sd_set,
419 const struct wimlib_capture_config *config,
420 ntfs_volume **ntfs_vol_p,
422 wimlib_progress_func_t progress_func);
425 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
426 const int name_nchars, const int name_type,
427 const s64 pos, const MFT_REF mref,
428 const unsigned dt_type)
430 struct readdir_ctx *ctx;
431 size_t mbs_name_nbytes;
433 struct wim_dentry *child;
436 size_t name_nbytes = name_nchars * sizeof(ntfschar);
439 if (name_type & FILE_NAME_DOS) {
440 /* If this is the entry for a DOS name, store it for later. */
441 ret = insert_dos_name(ctx->dos_name_map, name,
442 name_nbytes, mref & MFT_REF_MASK_CPU);
444 /* Return now if an error occurred or if this is just a DOS name
445 * and not a Win32+DOS name. */
446 if (ret != 0 || name_type == FILE_NAME_DOS)
449 ret = utf16le_to_tstr(name, name_nbytes,
450 &mbs_name, &mbs_name_nbytes);
454 if (mbs_name[0] == '.' &&
455 (mbs_name[1] == '\0' ||
456 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
459 * note: name_type is POSIX for these, so DOS names will not
460 * have been inserted for them. */
462 goto out_free_mbs_name;
465 /* Open the inode for this directory entry and recursively capture the
466 * directory tree rooted at it */
467 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
469 ERROR_WITH_ERRNO("Failed to open NTFS inode");
471 goto out_free_mbs_name;
473 path_len = ctx->path_len;
475 ctx->path[path_len++] = '/';
476 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
477 path_len += mbs_name_nbytes;
479 ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
480 ni, ctx->path, path_len, name_type,
484 ctx->config, ctx->ntfs_vol_p,
485 ctx->add_image_flags,
488 dentry_add_child(ctx->parent, child);
489 ntfs_inode_close(ni);
496 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
497 * At the same time, update the WIM lookup table with lookup table entries for
498 * the NTFS streams, and build an array of security descriptors.
501 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
507 struct wim_lookup_table *lookup_table,
508 struct wim_inode_table *inode_table,
509 struct sd_set *sd_set,
510 const struct wimlib_capture_config *config,
511 ntfs_volume **ntfs_vol_p,
513 wimlib_progress_func_t progress_func)
517 struct wim_dentry *root;
518 struct wim_inode *inode;
520 if (exclude_path(path, path_len, config, false)) {
521 /* Exclude a file or directory tree based on the capture
522 * configuration file */
523 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
526 union wimlib_progress_info info;
527 info.scan.cur_path = path;
528 info.scan.excluded = true;
529 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
536 /* Get file attributes */
537 struct SECURITY_CONTEXT ctx;
538 memset(&ctx, 0, sizeof(ctx));
540 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
541 ni, dir_ni, (char *)&attributes,
544 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
546 return WIMLIB_ERR_NTFS_3G;
549 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
552 union wimlib_progress_info info;
553 info.scan.cur_path = path;
554 info.scan.excluded = false;
555 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
558 /* Create a WIM dentry with an associated inode, which may be shared */
559 ret = inode_table_new_dentry(inode_table,
560 path_basename_with_len(path, path_len),
567 inode = root->d_inode;
569 if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
572 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name */
573 root->is_win32_name = 1;
574 inode->i_creation_time = le64_to_cpu(ni->creation_time);
575 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
576 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
577 inode->i_attributes = le32_to_cpu(attributes);
578 inode->i_resolved = 1;
580 if (attributes & FILE_ATTR_REPARSE_POINT) {
581 /* Junction point, symbolic link, or other reparse point */
582 ret = capture_ntfs_streams(inode, ni, path,
583 path_len, lookup_table,
584 ntfs_vol_p, AT_REPARSE_POINT);
585 } else if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
587 /* Normal directory */
589 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
590 struct readdir_ctx ctx = {
594 .path_len = path_len,
595 .lookup_table = lookup_table,
596 .inode_table = inode_table,
598 .dos_name_map = &dos_name_map,
600 .ntfs_vol_p = ntfs_vol_p,
601 .add_image_flags = add_image_flags,
602 .progress_func = progress_func,
604 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
606 ERROR_WITH_ERRNO("ntfs_readdir()");
607 ret = WIMLIB_ERR_NTFS_3G;
609 ret = for_dentry_child(root, set_dentry_dos_name,
612 destroy_dos_name_map(&dos_name_map);
615 ret = capture_ntfs_streams(inode, ni, path,
616 path_len, lookup_table,
617 ntfs_vol_p, AT_DATA);
622 if (!(add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NO_ACLS)) {
623 /* Get security descriptor */
627 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
630 if (ret > sizeof(sd)) {
632 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
633 ni, dir_ni, sd, ret);
636 inode->i_security_id = sd_set_add_sd(sd_set, sd, ret);
637 if (inode->i_security_id == -1) {
638 ERROR("Out of memory");
639 ret = WIMLIB_ERR_NOMEM;
642 DEBUG("Added security ID = %u for `%s'",
643 inode->i_security_id, path);
645 } else if (ret < 0) {
646 ERROR_WITH_ERRNO("Failed to get security information from "
648 ret = WIMLIB_ERR_NTFS_3G;
650 inode->i_security_id = -1;
651 DEBUG("No security ID for `%s'", path);
658 free_dentry_tree(root, lookup_table);
663 build_dentry_tree_ntfs(struct wim_dentry **root_p,
665 struct wim_lookup_table *lookup_table,
666 struct wim_inode_table *inode_table,
667 struct sd_set *sd_set,
668 const struct wimlib_capture_config *config,
670 wimlib_progress_func_t progress_func,
676 ntfs_volume **ntfs_vol_p = extra_arg;
678 DEBUG("Mounting NTFS volume `%s' read-only", device);
680 #ifdef HAVE_NTFS_MNT_RDONLY
682 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
684 /* NTFS-3g 2011, 2012 */
685 vol = ntfs_mount(device, MS_RDONLY);
688 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
690 return WIMLIB_ERR_NTFS_3G;
692 ntfs_open_secure(vol);
694 /* We don't want to capture the special NTFS files such as $Bitmap. Not
695 * to be confused with "hidden" or "system" files which are real files
696 * that we do need to capture. */
697 NVolClearShowSysFiles(vol);
699 DEBUG("Opening root NTFS dentry");
700 root_ni = ntfs_inode_open(vol, FILE_root);
702 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
704 ret = WIMLIB_ERR_NTFS_3G;
708 /* Currently we assume that all the paths fit into this length and there
709 * is no check for overflow. */
710 char *path = MALLOC(32768);
712 ERROR("Could not allocate memory for NTFS pathname");
713 ret = WIMLIB_ERR_NOMEM;
719 ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
720 FILE_NAME_POSIX, lookup_table,
728 ntfs_inode_close(root_ni);
730 ntfs_index_ctx_put(vol->secure_xsii);
731 ntfs_index_ctx_put(vol->secure_xsdh);
732 ntfs_inode_close(vol->secure_ni);
735 if (ntfs_umount(vol, FALSE) != 0) {
736 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
739 ret = WIMLIB_ERR_NTFS_3G;
742 /* We need to leave the NTFS volume mounted so that we can read
743 * the NTFS files again when we are actually writing the WIM */