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/buffer_io.h"
57 #include "wimlib/capture.h"
58 #include "wimlib/dentry.h"
59 #include "wimlib/encoding.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, 8, buf) != 8) {
164 ERROR_WITH_ERRNO("Error reading reparse data");
165 ret = WIMLIB_ERR_NTFS_3G;
166 goto out_close_ntfs_attr;
168 *reparse_tag_ret = le32_to_cpu(*(u32*)buf);
169 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
178 /* Load the streams from a file or reparse point in the NTFS volume into the WIM
181 capture_ntfs_streams(struct wim_inode *inode,
185 struct wim_lookup_table *lookup_table,
189 ntfs_attr_search_ctx *actx;
190 struct ntfs_location *ntfs_loc;
192 struct wim_lookup_table_entry *lte;
194 DEBUG2("Capturing NTFS data streams from `%s'", path);
196 /* Get context to search the streams of the NTFS file. */
197 actx = ntfs_attr_get_search_ctx(ni, NULL);
199 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
201 return WIMLIB_ERR_NTFS_3G;
204 /* Capture each data stream or reparse data stream. */
205 while (!ntfs_attr_lookup(type, NULL, 0,
206 CASE_SENSITIVE, 0, NULL, 0, actx))
208 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
209 u64 name_length = actx->attr->name_length;
212 if (data_size == 0) {
213 /* Empty stream. No lookup table entry is needed. */
217 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
219 ret = WIMLIB_ERR_NOMEM;
222 ntfs_loc->ntfs_vol = vol;
223 ntfs_loc->path = memdup(path, path_len + 1);
224 if (!ntfs_loc->path) {
225 ret = WIMLIB_ERR_NOMEM;
226 goto out_free_ntfs_loc;
229 ntfs_loc->stream_name = memdup(attr_record_name(actx->attr),
231 if (!ntfs_loc->stream_name) {
232 ret = WIMLIB_ERR_NOMEM;
233 goto out_free_ntfs_loc;
235 ntfs_loc->stream_name_nchars = name_length;
238 lte = new_lookup_table_entry();
240 ret = WIMLIB_ERR_NOMEM;
241 goto out_free_ntfs_loc;
243 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
244 lte->ntfs_loc = ntfs_loc;
246 if (type == AT_REPARSE_POINT) {
248 ERROR("Invalid reparse data (only %u bytes)!",
249 (unsigned)data_size);
250 ret = WIMLIB_ERR_NTFS_3G;
253 lte->ntfs_loc->is_reparse_point = true;
254 lte->resource_entry.original_size = data_size - 8;
255 ret = read_reparse_tag(ni, lte->ntfs_loc,
256 &inode->i_reparse_tag);
260 lte->ntfs_loc->is_reparse_point = false;
261 lte->resource_entry.original_size = data_size;
264 if (name_length == 0) {
265 /* Unnamed data stream. Put the reference to it in the
268 ERROR("Found two un-named data streams for `%s'",
270 ret = WIMLIB_ERR_NTFS_3G;
276 /* Named data stream. Put the reference to it in the
277 * alternate data stream entries */
278 struct wim_ads_entry *new_ads_entry;
280 new_ads_entry = inode_add_ads_utf16le(inode,
281 attr_record_name(actx->attr),
283 if (!new_ads_entry) {
284 ret = WIMLIB_ERR_NOMEM;
287 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
288 stream_id = new_ads_entry->stream_id;
289 new_ads_entry->lte = lte;
292 lookup_table_insert_unhashed(lookup_table, lte,
296 if (errno == ENOENT) {
299 ERROR_WITH_ERRNO("Error listing NTFS attributes from `%s'",
301 ret = WIMLIB_ERR_NTFS_3G;
305 free_lookup_table_entry(lte);
308 FREE(ntfs_loc->path);
309 FREE(ntfs_loc->stream_name);
313 ntfs_attr_put_search_ctx(actx);
315 DEBUG2("Successfully captured NTFS streams from `%s'", path);
317 ERROR("Failed to capture NTFS streams from `%s'", path);
321 /* Red-black tree that maps NTFS inode numbers to DOS names */
322 struct dos_name_map {
323 struct rb_root rb_root;
326 struct dos_name_node {
327 struct rb_node rb_node;
333 /* Inserts a new DOS name into the map */
335 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
336 size_t name_nbytes, u64 ntfs_ino)
338 struct dos_name_node *new_node;
340 struct rb_root *root;
341 struct rb_node *rb_parent;
343 DEBUG("DOS name_len = %zu", name_nbytes);
344 new_node = MALLOC(sizeof(struct dos_name_node));
348 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
349 * assuming 2-byte ntfs characters) */
350 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
352 /* Initialize the DOS name, DOS name length, and NTFS inode number of
353 * the red-black tree node */
354 memcpy(new_node->dos_name, dos_name, name_nbytes);
355 new_node->name_nbytes = name_nbytes;
356 new_node->ntfs_ino = ntfs_ino;
358 /* Insert the red-black tree node */
359 root = &map->rb_root;
363 struct dos_name_node *this;
365 this = container_of(*p, struct dos_name_node, rb_node);
367 if (new_node->ntfs_ino < this->ntfs_ino)
368 p = &((*p)->rb_left);
369 else if (new_node->ntfs_ino > this->ntfs_ino)
370 p = &((*p)->rb_right);
372 /* This should be impossible since a NTFS inode cannot
373 * have multiple DOS names, and we only should get each
374 * DOS name entry once from the ntfs_readdir() calls. */
375 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
380 rb_link_node(&new_node->rb_node, rb_parent, p);
381 rb_insert_color(&new_node->rb_node, root);
382 DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
386 /* Returns a structure that contains the DOS name and its length for a NTFS
387 * inode, or NULL if the inode has no DOS name. */
388 static struct dos_name_node *
389 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
391 struct rb_node *node = map->rb_root.rb_node;
393 struct dos_name_node *this;
394 this = container_of(node, struct dos_name_node, rb_node);
395 if (ntfs_ino < this->ntfs_ino)
396 node = node->rb_left;
397 else if (ntfs_ino > this->ntfs_ino)
398 node = node->rb_right;
406 set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
408 const struct dos_name_map *map = arg;
409 const struct dos_name_node *node;
411 if (dentry->is_win32_name) {
412 node = lookup_dos_name(map, dentry->d_inode->i_ino);
414 dentry->short_name = MALLOC(node->name_nbytes + 2);
415 if (!dentry->short_name)
416 return WIMLIB_ERR_NOMEM;
417 memcpy(dentry->short_name, node->dos_name,
419 dentry->short_name[node->name_nbytes / 2] = 0;
420 dentry->short_name_nbytes = node->name_nbytes;
421 DEBUG("Assigned DOS name to ino %"PRIu64,
422 dentry->d_inode->i_ino);
424 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
425 "corresponding DOS name",
426 dentry->d_inode->i_ino);
433 free_dos_name_tree(struct rb_node *node) {
435 free_dos_name_tree(node->rb_left);
436 free_dos_name_tree(node->rb_right);
437 FREE(container_of(node, struct dos_name_node, rb_node));
442 destroy_dos_name_map(struct dos_name_map *map)
444 free_dos_name_tree(map->rb_root.rb_node);
448 struct wim_dentry *parent;
452 struct dos_name_map *dos_name_map;
454 struct add_image_params *params;
458 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
464 ntfs_volume *ntfs_vol,
465 struct add_image_params *params);
468 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
469 const int name_nchars, const int name_type,
470 const s64 pos, const MFT_REF mref,
471 const unsigned dt_type)
473 struct readdir_ctx *ctx;
474 size_t mbs_name_nbytes;
476 struct wim_dentry *child;
479 size_t name_nbytes = name_nchars * sizeof(ntfschar);
482 if (name_type & FILE_NAME_DOS) {
483 /* If this is the entry for a DOS name, store it for later. */
484 ret = insert_dos_name(ctx->dos_name_map, name,
485 name_nbytes, mref & MFT_REF_MASK_CPU);
487 /* Return now if an error occurred or if this is just a DOS name
488 * and not a Win32+DOS name. */
489 if (ret != 0 || name_type == FILE_NAME_DOS)
492 ret = utf16le_to_tstr(name, name_nbytes,
493 &mbs_name, &mbs_name_nbytes);
497 if (mbs_name[0] == '.' &&
498 (mbs_name[1] == '\0' ||
499 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
502 * note: name_type is POSIX for these, so DOS names will not
503 * have been inserted for them. */
505 goto out_free_mbs_name;
508 /* Open the inode for this directory entry and recursively capture the
509 * directory tree rooted at it */
510 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
512 ERROR_WITH_ERRNO("Failed to open NTFS inode");
514 goto out_free_mbs_name;
516 path_len = ctx->path_len;
518 ctx->path[path_len++] = '/';
519 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
520 path_len += mbs_name_nbytes;
522 ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
523 ni, ctx->path, path_len, name_type,
524 ctx->vol, ctx->params);
526 dentry_add_child(ctx->parent, child);
527 ntfs_inode_close(ni);
534 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
535 * At the same time, update the WIM lookup table with lookup table entries for
536 * the NTFS streams, and build an array of security descriptors.
539 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
546 struct add_image_params *params)
550 struct wim_dentry *root;
551 struct wim_inode *inode;
552 ATTR_TYPES stream_type;
554 if (exclude_path(path, path_len, params->config, false)) {
555 /* Exclude a file or directory tree based on the capture
556 * configuration file */
557 if ((params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE)
558 && params->progress_func)
560 union wimlib_progress_info info;
561 info.scan.cur_path = path;
562 info.scan.excluded = true;
563 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
570 /* Get file attributes */
571 struct SECURITY_CONTEXT ctx;
572 memset(&ctx, 0, sizeof(ctx));
574 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
575 ni, dir_ni, (char *)&attributes,
578 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
580 return WIMLIB_ERR_NTFS_3G;
583 if ((params->add_flags & WIMLIB_ADD_FLAG_VERBOSE)
584 && params->progress_func)
586 union wimlib_progress_info info;
587 info.scan.cur_path = path;
588 info.scan.excluded = false;
589 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
592 /* Create a WIM dentry with an associated inode, which may be shared */
593 ret = inode_table_new_dentry(¶ms->inode_table,
594 path_basename_with_len(path, path_len),
595 ni->mft_no, 0, false, &root);
599 inode = root->d_inode;
601 if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
604 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
605 root->is_win32_name = 1;
606 inode->i_creation_time = le64_to_cpu(ni->creation_time);
607 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
608 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
609 inode->i_attributes = le32_to_cpu(attributes);
610 inode->i_resolved = 1;
612 if (attributes & FILE_ATTR_REPARSE_POINT)
613 stream_type = AT_REPARSE_POINT;
615 stream_type = AT_DATA;
617 /* Capture the file's streams; more specifically, this is supposed to:
619 * - Regular files: capture unnamed data stream and any named data
621 * - Directories: capture any named data streams
622 * - Reparse points: capture reparse data only
624 ret = capture_ntfs_streams(inode, ni, path, path_len,
625 params->lookup_table, vol, stream_type);
629 if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
631 /* Recurse to directory children */
633 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
634 struct readdir_ctx ctx = {
638 .path_len = path_len,
639 .dos_name_map = &dos_name_map,
643 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
645 ERROR_WITH_ERRNO("ntfs_readdir()");
646 ret = WIMLIB_ERR_NTFS_3G;
648 ret = for_dentry_child(root, set_dentry_dos_name,
651 destroy_dos_name_map(&dos_name_map);
656 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
657 * only allow capturing an entire volume. */
658 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
659 inode_is_symlink(inode))
660 inode->i_not_rpfixed = 0;
662 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
663 /* Get security descriptor */
667 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
670 if (ret > sizeof(sd)) {
672 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
673 ni, dir_ni, sd, ret);
676 inode->i_security_id = sd_set_add_sd(¶ms->sd_set,
678 if (inode->i_security_id == -1) {
679 ERROR("Out of memory");
680 ret = WIMLIB_ERR_NOMEM;
683 DEBUG("Added security ID = %u for `%s'",
684 inode->i_security_id, path);
686 } else if (ret < 0) {
687 ERROR_WITH_ERRNO("Failed to get security information from "
689 ret = WIMLIB_ERR_NTFS_3G;
691 inode->i_security_id = -1;
692 DEBUG("No security ID for `%s'", path);
699 free_dentry_tree(root, params->lookup_table);
705 do_ntfs_umount(struct _ntfs_volume *vol)
707 DEBUG("Unmounting NTFS volume");
708 if (ntfs_umount(vol, FALSE))
709 return WIMLIB_ERR_NTFS_3G;
715 build_dentry_tree_ntfs(struct wim_dentry **root_p,
717 struct add_image_params *params)
723 DEBUG("Mounting NTFS volume `%s' read-only", device);
725 #if defined(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);
733 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
735 return WIMLIB_ERR_NTFS_3G;
737 ntfs_open_secure(vol);
739 /* We don't want to capture the special NTFS files such as $Bitmap. Not
740 * to be confused with "hidden" or "system" files which are real files
741 * that we do need to capture. */
742 NVolClearShowSysFiles(vol);
744 DEBUG("Opening root NTFS dentry");
745 root_ni = ntfs_inode_open(vol, FILE_root);
747 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
749 ret = WIMLIB_ERR_NTFS_3G;
753 /* Currently we assume that all the paths fit into this length and there
754 * is no check for overflow. */
755 char *path = MALLOC(32768);
757 ERROR("Could not allocate memory for NTFS pathname");
758 ret = WIMLIB_ERR_NOMEM;
764 ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
765 FILE_NAME_POSIX, vol, params);
768 ntfs_inode_close(root_ni);
770 ntfs_index_ctx_put(vol->secure_xsii);
771 ntfs_index_ctx_put(vol->secure_xsdh);
772 ntfs_inode_close(vol->secure_ni);
775 if (do_ntfs_umount(vol)) {
776 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
779 ret = WIMLIB_ERR_NTFS_3G;
782 /* We need to leave the NTFS volume mounted so that we can read
783 * the NTFS files again when we are actually writing the WIM */
784 *(ntfs_volume**)params->extra_arg = vol;
788 #endif /* WITH_NTFS_3G */