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;
168 struct wim_lookup_table_entry **back_ptr;
169 if (data_size == 0) {
171 ERROR_WITH_ERRNO("Failed to get size of attribute of "
173 ret = WIMLIB_ERR_NTFS_3G;
176 /* Empty stream. No lookup table entry is needed. */
179 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
182 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
183 ntfs_loc->path = MALLOC(path_len + 1);
185 goto out_free_ntfs_loc;
186 memcpy(ntfs_loc->path, path, path_len + 1);
188 ntfs_loc->stream_name = MALLOC(name_length * 2);
189 if (!ntfs_loc->stream_name)
190 goto out_free_ntfs_loc;
191 memcpy(ntfs_loc->stream_name,
192 attr_record_name(actx->attr),
193 actx->attr->name_length * 2);
194 ntfs_loc->stream_name_nchars = name_length;
197 lte = new_lookup_table_entry();
199 goto out_free_ntfs_loc;
200 lte->ntfs_loc = ntfs_loc;
201 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
203 if (type == AT_REPARSE_POINT) {
204 ntfs_loc->is_reparse_point = true;
205 lte->resource_entry.original_size = data_size - 8;
206 lte->resource_entry.size = data_size - 8;
208 ntfs_loc->is_reparse_point = false;
209 lte->resource_entry.original_size = data_size;
210 lte->resource_entry.size = data_size;
213 ntfs_loc->is_reparse_point = (type == AT_REPARSE_POINT);
214 lte->resource_entry.original_size = data_size;
218 if (name_length == 0) {
219 /* Unnamed data stream. Put the reference to it in the
222 ERROR("Found two un-named data streams for `%s'",
224 ret = WIMLIB_ERR_NTFS_3G;
227 back_ptr = &inode->i_lte;
229 /* Named data stream. Put the reference to it in the
230 * alternate data stream entries */
231 struct wim_ads_entry *new_ads_entry;
233 new_ads_entry = inode_add_ads_utf16le(inode,
234 attr_record_name(actx->attr),
238 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
239 back_ptr = &new_ads_entry->lte;
241 lookup_table_insert_unhashed(lookup_table, lte, back_ptr);
246 free_lookup_table_entry(lte);
249 FREE(ntfs_loc->path);
250 FREE(ntfs_loc->stream_name);
254 ntfs_attr_put_search_ctx(actx);
256 DEBUG2("Successfully captured NTFS streams from `%s'", path);
258 ERROR("Failed to capture NTFS streams from `%s", path);
262 /* Red-black tree that maps NTFS inode numbers to DOS names */
263 struct dos_name_map {
264 struct rb_root rb_root;
267 struct dos_name_node {
268 struct rb_node rb_node;
274 /* Inserts a new DOS name into the map */
276 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
277 size_t name_nbytes, u64 ntfs_ino)
279 struct dos_name_node *new_node;
281 struct rb_root *root;
282 struct rb_node *rb_parent;
284 DEBUG("DOS name_len = %zu", name_nbytes);
285 new_node = MALLOC(sizeof(struct dos_name_node));
289 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
290 * assuming 2-byte ntfs characters) */
291 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
293 /* Initialize the DOS name, DOS name length, and NTFS inode number of
294 * the red-black tree node */
295 memcpy(new_node->dos_name, dos_name, name_nbytes);
296 new_node->name_nbytes = name_nbytes;
297 new_node->ntfs_ino = ntfs_ino;
299 /* Insert the red-black tree node */
300 root = &map->rb_root;
304 struct dos_name_node *this;
306 this = container_of(*p, struct dos_name_node, rb_node);
308 if (new_node->ntfs_ino < this->ntfs_ino)
309 p = &((*p)->rb_left);
310 else if (new_node->ntfs_ino > this->ntfs_ino)
311 p = &((*p)->rb_right);
313 /* This should be impossible since a NTFS inode cannot
314 * have multiple DOS names, and we only should get each
315 * DOS name entry once from the ntfs_readdir() calls. */
316 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
321 rb_link_node(&new_node->rb_node, rb_parent, p);
322 rb_insert_color(&new_node->rb_node, root);
323 DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
327 /* Returns a structure that contains the DOS name and its length for a NTFS
328 * inode, or NULL if the inode has no DOS name. */
329 static struct dos_name_node *
330 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
332 struct rb_node *node = map->rb_root.rb_node;
334 struct dos_name_node *this;
335 this = container_of(node, struct dos_name_node, rb_node);
336 if (ntfs_ino < this->ntfs_ino)
337 node = node->rb_left;
338 else if (ntfs_ino > this->ntfs_ino)
339 node = node->rb_right;
347 set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
349 const struct dos_name_map *map = arg;
350 const struct dos_name_node *node;
352 if (dentry->is_win32_name) {
353 node = lookup_dos_name(map, dentry->d_inode->i_ino);
355 dentry->short_name = MALLOC(node->name_nbytes + 2);
356 if (!dentry->short_name)
357 return WIMLIB_ERR_NOMEM;
358 memcpy(dentry->short_name, node->dos_name,
360 dentry->short_name[node->name_nbytes / 2] = 0;
361 dentry->short_name_nbytes = node->name_nbytes;
362 DEBUG("Assigned DOS name to ino %"PRIu64,
363 dentry->d_inode->i_ino);
365 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
366 "corresponding DOS name",
367 dentry->d_inode->i_ino);
374 free_dos_name_tree(struct rb_node *node) {
376 free_dos_name_tree(node->rb_left);
377 free_dos_name_tree(node->rb_right);
378 FREE(container_of(node, struct dos_name_node, rb_node));
383 destroy_dos_name_map(struct dos_name_map *map)
385 free_dos_name_tree(map->rb_root.rb_node);
389 struct wim_dentry *parent;
393 struct wim_lookup_table *lookup_table;
394 struct wim_inode_table *inode_table;
395 struct sd_set *sd_set;
396 struct dos_name_map *dos_name_map;
397 const struct wimlib_capture_config *config;
398 ntfs_volume **ntfs_vol_p;
400 wimlib_progress_func_t progress_func;
404 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
410 struct wim_lookup_table *lookup_table,
411 struct wim_inode_table *inode_table,
412 struct sd_set *sd_set,
413 const struct wimlib_capture_config *config,
414 ntfs_volume **ntfs_vol_p,
416 wimlib_progress_func_t progress_func);
419 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
420 const int name_nchars, const int name_type,
421 const s64 pos, const MFT_REF mref,
422 const unsigned dt_type)
424 struct readdir_ctx *ctx;
425 size_t mbs_name_nbytes;
427 struct wim_dentry *child;
430 size_t name_nbytes = name_nchars * sizeof(ntfschar);
433 if (name_type & FILE_NAME_DOS) {
434 /* If this is the entry for a DOS name, store it for later. */
435 ret = insert_dos_name(ctx->dos_name_map, name,
436 name_nbytes, mref & MFT_REF_MASK_CPU);
438 /* Return now if an error occurred or if this is just a DOS name
439 * and not a Win32+DOS name. */
440 if (ret != 0 || name_type == FILE_NAME_DOS)
443 ret = utf16le_to_tstr(name, name_nbytes,
444 &mbs_name, &mbs_name_nbytes);
448 if (mbs_name[0] == '.' &&
449 (mbs_name[1] == '\0' ||
450 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
453 * note: name_type is POSIX for these, so DOS names will not
454 * have been inserted for them. */
456 goto out_free_mbs_name;
459 /* Open the inode for this directory entry and recursively capture the
460 * directory tree rooted at it */
461 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
463 ERROR_WITH_ERRNO("Failed to open NTFS inode");
465 goto out_free_mbs_name;
467 path_len = ctx->path_len;
469 ctx->path[path_len++] = '/';
470 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
471 path_len += mbs_name_nbytes;
473 ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
474 ni, ctx->path, path_len, name_type,
478 ctx->config, ctx->ntfs_vol_p,
479 ctx->add_image_flags,
482 dentry_add_child(ctx->parent, child);
483 ntfs_inode_close(ni);
490 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
491 * At the same time, update the WIM lookup table with lookup table entries for
492 * the NTFS streams, and build an array of security descriptors.
495 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
501 struct wim_lookup_table *lookup_table,
502 struct wim_inode_table *inode_table,
503 struct sd_set *sd_set,
504 const struct wimlib_capture_config *config,
505 ntfs_volume **ntfs_vol_p,
507 wimlib_progress_func_t progress_func)
511 struct wim_dentry *root;
512 struct wim_inode *inode;
514 if (exclude_path(path, path_len, config, false)) {
515 /* Exclude a file or directory tree based on the capture
516 * configuration file */
517 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
520 union wimlib_progress_info info;
521 info.scan.cur_path = path;
522 info.scan.excluded = true;
523 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
530 /* Get file attributes */
531 struct SECURITY_CONTEXT ctx;
532 memset(&ctx, 0, sizeof(ctx));
534 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
535 ni, dir_ni, (char *)&attributes,
538 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
540 return WIMLIB_ERR_NTFS_3G;
543 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
546 union wimlib_progress_info info;
547 info.scan.cur_path = path;
548 info.scan.excluded = false;
549 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
552 /* Create a WIM dentry with an associated inode, which may be shared */
553 ret = inode_table_new_dentry(inode_table,
554 path_basename_with_len(path, path_len),
561 inode = root->d_inode;
563 if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
566 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name */
567 root->is_win32_name = 1;
568 inode->i_creation_time = le64_to_cpu(ni->creation_time);
569 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
570 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
571 inode->i_attributes = le32_to_cpu(attributes);
572 inode->i_resolved = 1;
574 if (attributes & FILE_ATTR_REPARSE_POINT) {
575 /* Junction point, symbolic link, or other reparse point */
576 ret = capture_ntfs_streams(inode, ni, path,
577 path_len, lookup_table,
578 ntfs_vol_p, AT_REPARSE_POINT);
579 } else if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
581 /* Normal directory */
583 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
584 struct readdir_ctx ctx = {
588 .path_len = path_len,
589 .lookup_table = lookup_table,
590 .inode_table = inode_table,
592 .dos_name_map = &dos_name_map,
594 .ntfs_vol_p = ntfs_vol_p,
595 .add_image_flags = add_image_flags,
596 .progress_func = progress_func,
598 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
600 ERROR_WITH_ERRNO("ntfs_readdir()");
601 ret = WIMLIB_ERR_NTFS_3G;
603 ret = for_dentry_child(root, set_dentry_dos_name,
606 destroy_dos_name_map(&dos_name_map);
609 ret = capture_ntfs_streams(inode, ni, path,
610 path_len, lookup_table,
611 ntfs_vol_p, AT_DATA);
616 if (!(add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NO_ACLS)) {
617 /* Get security descriptor */
621 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
624 if (ret > sizeof(sd)) {
626 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
627 ni, dir_ni, sd, ret);
630 inode->i_security_id = sd_set_add_sd(sd_set, sd, ret);
631 if (inode->i_security_id == -1) {
632 ERROR("Out of memory");
633 ret = WIMLIB_ERR_NOMEM;
636 DEBUG("Added security ID = %u for `%s'",
637 inode->i_security_id, path);
639 } else if (ret < 0) {
640 ERROR_WITH_ERRNO("Failed to get security information from "
642 ret = WIMLIB_ERR_NTFS_3G;
644 inode->i_security_id = -1;
645 DEBUG("No security ID for `%s'", path);
652 free_dentry_tree(root, lookup_table);
657 build_dentry_tree_ntfs(struct wim_dentry **root_p,
659 struct wim_lookup_table *lookup_table,
660 struct wim_inode_table *inode_table,
661 struct sd_set *sd_set,
662 const struct wimlib_capture_config *config,
664 wimlib_progress_func_t progress_func,
670 ntfs_volume **ntfs_vol_p = extra_arg;
672 DEBUG("Mounting NTFS volume `%s' read-only", device);
674 #ifdef HAVE_NTFS_MNT_RDONLY
676 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
678 /* NTFS-3g 2011, 2012 */
679 vol = ntfs_mount(device, MS_RDONLY);
682 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
684 return WIMLIB_ERR_NTFS_3G;
686 ntfs_open_secure(vol);
688 /* We don't want to capture the special NTFS files such as $Bitmap. Not
689 * to be confused with "hidden" or "system" files which are real files
690 * that we do need to capture. */
691 NVolClearShowSysFiles(vol);
693 DEBUG("Opening root NTFS dentry");
694 root_ni = ntfs_inode_open(vol, FILE_root);
696 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
698 ret = WIMLIB_ERR_NTFS_3G;
702 /* Currently we assume that all the paths fit into this length and there
703 * is no check for overflow. */
704 char *path = MALLOC(32768);
706 ERROR("Could not allocate memory for NTFS pathname");
707 ret = WIMLIB_ERR_NOMEM;
713 ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
714 FILE_NAME_POSIX, lookup_table,
722 ntfs_inode_close(root_ni);
724 ntfs_index_ctx_put(vol->secure_xsii);
725 ntfs_index_ctx_put(vol->secure_xsdh);
726 ntfs_inode_close(vol->secure_ni);
729 if (ntfs_umount(vol, FALSE) != 0) {
730 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
733 ret = WIMLIB_ERR_NTFS_3G;
736 /* We need to leave the NTFS volume mounted so that we can read
737 * the NTFS files again when we are actually writing the WIM */