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, 2014 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 <ntfs-3g/attrib.h>
41 #include <ntfs-3g/reparse.h>
42 #include <ntfs-3g/security.h>
43 #include <ntfs-3g/volume.h>
45 #include "wimlib/capture.h"
46 #include "wimlib/dentry.h"
47 #include "wimlib/encoding.h"
48 #include "wimlib/endianness.h"
49 #include "wimlib/error.h"
50 #include "wimlib/lookup_table.h"
51 #include "wimlib/ntfs_3g.h"
52 #include "wimlib/paths.h"
53 #include "wimlib/security.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, u64 size,
79 consume_data_callback_t cb, void *cb_ctx)
81 struct ntfs_location *loc = lte->ntfs_loc;
82 ntfs_volume *vol = loc->ntfs_vol;
90 ni = ntfs_pathname_to_inode(vol, NULL, loc->path);
92 ERROR_WITH_ERRNO("Can't find NTFS inode for \"%"TS"\"", loc->path);
93 ret = WIMLIB_ERR_NTFS_3G;
97 na = open_ntfs_attr(ni, loc);
99 ret = WIMLIB_ERR_NTFS_3G;
100 goto out_close_ntfs_inode;
103 pos = (loc->is_reparse_point) ? 8 : 0;
104 bytes_remaining = size;
105 while (bytes_remaining) {
106 s64 to_read = min(bytes_remaining, sizeof(buf));
107 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
108 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
109 ret = WIMLIB_ERR_NTFS_3G;
110 goto out_close_ntfs_attr;
113 bytes_remaining -= to_read;
114 ret = cb(buf, to_read, cb_ctx);
116 goto out_close_ntfs_attr;
121 out_close_ntfs_inode:
122 ntfs_inode_close(ni);
128 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
129 u32 *reparse_tag_ret)
135 na = open_ntfs_attr(ni, loc);
137 ret = WIMLIB_ERR_NTFS_3G;
141 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
142 &reparse_tag) != sizeof(reparse_tag))
144 ERROR_WITH_ERRNO("Error reading reparse data");
145 ret = WIMLIB_ERR_NTFS_3G;
146 goto out_close_ntfs_attr;
148 *reparse_tag_ret = le32_to_cpu(reparse_tag);
149 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
158 /* Load the streams from a file or reparse point in the NTFS volume */
160 capture_ntfs_streams(struct wim_inode *inode,
164 struct list_head *unhashed_streams,
168 ntfs_attr_search_ctx *actx;
169 struct ntfs_location *ntfs_loc;
171 struct wim_lookup_table_entry *lte;
173 DEBUG("Capturing NTFS data streams from `%s'", path);
175 /* Get context to search the streams of the NTFS file. */
176 actx = ntfs_attr_get_search_ctx(ni, NULL);
178 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
179 "context for \"%s\"", path);
180 return WIMLIB_ERR_NTFS_3G;
183 /* Capture each data stream or reparse data stream. */
184 while (!ntfs_attr_lookup(type, NULL, 0,
185 CASE_SENSITIVE, 0, NULL, 0, actx))
187 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
188 u64 name_length = actx->attr->name_length;
191 if (data_size == 0) {
192 /* Empty stream. No lookup table entry is needed. */
196 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
198 ret = WIMLIB_ERR_NOMEM;
201 ntfs_loc->ntfs_vol = vol;
202 ntfs_loc->path = memdup(path, path_len + 1);
203 if (!ntfs_loc->path) {
204 ret = WIMLIB_ERR_NOMEM;
205 goto out_free_ntfs_loc;
208 ntfs_loc->stream_name = memdup(attr_record_name(actx->attr),
210 if (!ntfs_loc->stream_name) {
211 ret = WIMLIB_ERR_NOMEM;
212 goto out_free_ntfs_loc;
214 ntfs_loc->stream_name_nchars = name_length;
217 lte = new_lookup_table_entry();
219 ret = WIMLIB_ERR_NOMEM;
220 goto out_free_ntfs_loc;
222 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
223 lte->ntfs_loc = ntfs_loc;
225 if (type == AT_REPARSE_POINT) {
227 ERROR("Invalid reparse data on \"%s\" "
228 "(only %u bytes)!", path, (unsigned)data_size);
229 ret = WIMLIB_ERR_NTFS_3G;
232 lte->ntfs_loc->is_reparse_point = true;
233 lte->size = data_size - 8;
234 ret = read_reparse_tag(ni, lte->ntfs_loc,
235 &inode->i_reparse_tag);
239 lte->ntfs_loc->is_reparse_point = false;
240 lte->size = data_size;
243 if (name_length == 0) {
244 /* Unnamed data stream. Put the reference to it in the
248 if (!(inode->i_attributes &
249 FILE_ATTRIBUTE_REPARSE_POINT))
251 WARNING("Found two un-named "
252 "data streams for \"%s\" "
253 "(sizes = %"PRIu64", "
259 free_lookup_table_entry(lte);
267 /* Named data stream. Put the reference to it in the
268 * alternate data stream entries */
269 struct wim_ads_entry *new_ads_entry;
271 new_ads_entry = inode_add_ads_utf16le(inode,
272 attr_record_name(actx->attr),
274 if (!new_ads_entry) {
275 ret = WIMLIB_ERR_NOMEM;
278 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
279 stream_id = new_ads_entry->stream_id;
280 new_ads_entry->lte = lte;
283 add_unhashed_stream(lte, inode,
284 stream_id, unhashed_streams);
287 if (errno == ENOENT) {
290 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
291 ret = WIMLIB_ERR_NTFS_3G;
295 free_lookup_table_entry(lte);
298 FREE(ntfs_loc->path);
299 FREE(ntfs_loc->stream_name);
303 ntfs_attr_put_search_ctx(actx);
305 DEBUG("Successfully captured NTFS streams from \"%s\"", path);
307 ERROR("Failed to capture NTFS streams from \"%s\"", path);
311 /* Binary tree that maps NTFS inode numbers to DOS names */
312 struct dos_name_map {
313 struct avl_tree_node *root;
316 struct dos_name_node {
317 struct avl_tree_node index_node;
323 #define DOS_NAME_NODE(avl_node) \
324 avl_tree_entry(avl_node, struct dos_name_node, index_node)
327 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
328 const struct avl_tree_node *n2)
330 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
331 DOS_NAME_NODE(n2)->ntfs_ino);
334 /* Inserts a new DOS name into the map */
336 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
337 size_t name_nbytes, le64 ntfs_ino)
339 struct dos_name_node *new_node;
341 DEBUG("DOS name_len = %zu", name_nbytes);
342 new_node = MALLOC(sizeof(struct dos_name_node));
346 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
347 * assuming 2-byte ntfs characters) */
348 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
350 /* Initialize the DOS name, DOS name length, and NTFS inode number of
351 * the search tree node */
352 memcpy(new_node->dos_name, dos_name, name_nbytes);
353 new_node->name_nbytes = name_nbytes;
354 new_node->ntfs_ino = ntfs_ino;
356 /* Insert the search tree node */
357 if (avl_tree_insert(&map->root, &new_node->index_node,
358 _avl_cmp_by_ntfs_ino))
360 /* This should be impossible since a NTFS inode cannot
361 * have multiple DOS names, and we only should get each
362 * DOS name entry once from the ntfs_readdir() calls. */
363 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
364 le64_to_cpu(ntfs_ino));
368 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
372 /* Returns a structure that contains the DOS name and its length for a NTFS
373 * inode, or NULL if the inode has no DOS name. */
374 static struct dos_name_node *
375 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
377 struct dos_name_node dummy;
378 struct avl_tree_node *res;
380 dummy.ntfs_ino = cpu_to_le64(ntfs_ino);
382 res = avl_tree_lookup_node(map->root, &dummy.index_node,
383 _avl_cmp_by_ntfs_ino);
386 return DOS_NAME_NODE(res);
390 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
392 const struct dos_name_node *node;
394 if (dentry->is_win32_name) {
395 node = lookup_dos_name(map, dentry->d_inode->i_ino);
397 dentry->short_name = MALLOC(node->name_nbytes + 2);
398 if (!dentry->short_name)
399 return WIMLIB_ERR_NOMEM;
400 memcpy(dentry->short_name, node->dos_name,
402 dentry->short_name[node->name_nbytes / 2] = 0;
403 dentry->short_name_nbytes = node->name_nbytes;
404 DEBUG("Assigned DOS name to ino %"PRIu64,
405 dentry->d_inode->i_ino);
407 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
408 "corresponding DOS name",
409 dentry->d_inode->i_ino);
416 free_dos_name_tree(struct avl_tree_node *node) {
418 free_dos_name_tree(node->left);
419 free_dos_name_tree(node->right);
420 FREE(DOS_NAME_NODE(node));
425 destroy_dos_name_map(struct dos_name_map *map)
427 free_dos_name_tree(map->root);
431 struct wim_dentry *parent;
434 struct dos_name_map *dos_name_map;
436 struct add_image_params *params;
440 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
445 ntfs_volume *ntfs_vol,
446 struct add_image_params *params);
449 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
450 const int name_nchars, const int name_type,
451 const s64 pos, const MFT_REF mref,
452 const unsigned dt_type)
454 struct readdir_ctx *ctx;
455 size_t mbs_name_nbytes;
457 struct wim_dentry *child;
460 size_t name_nbytes = name_nchars * sizeof(ntfschar);
463 if (name_type & FILE_NAME_DOS) {
464 /* If this is the entry for a DOS name, store it for later. */
465 ret = insert_dos_name(ctx->dos_name_map, name,
466 name_nbytes, mref & MFT_REF_MASK_CPU);
468 /* Return now if an error occurred or if this is just a DOS name
469 * and not a Win32+DOS name. */
470 if (ret != 0 || name_type == FILE_NAME_DOS)
473 ret = utf16le_to_tstr(name, name_nbytes,
474 &mbs_name, &mbs_name_nbytes);
478 if (mbs_name[0] == '.' &&
479 (mbs_name[1] == '\0' ||
480 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
483 * note: name_type is POSIX for these, so DOS names will not
484 * have been inserted for them. */
486 goto out_free_mbs_name;
489 /* Open the inode for this directory entry and recursively capture the
490 * directory tree rooted at it */
491 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
493 /* XXX This used to be treated as an error, but NTFS-3g seemed
494 * to be unable to read some inodes on a Windows 8 image for
496 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
497 ctx->path, mbs_name);
499 goto out_free_mbs_name;
501 path_len = ctx->path_len;
503 ctx->path[path_len++] = '/';
504 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
505 path_len += mbs_name_nbytes;
507 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
509 ctx->vol, ctx->params);
510 path_len -= mbs_name_nbytes + 1;
512 dentry_add_child(ctx->parent, child);
513 ntfs_inode_close(ni);
517 ctx->path[ctx->path_len] = '\0';
521 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
522 * At the same time, update the WIM lookup table with lookup table entries for
523 * the NTFS streams, and build an array of security descriptors.
526 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
532 struct add_image_params *params)
536 struct wim_dentry *root = NULL;
537 struct wim_inode *inode = NULL;
539 if (exclude_path(path, path_len, params->config, false)) {
540 /* Exclude a file or directory tree based on the capture
541 * configuration file. */
546 /* Get file attributes */
547 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
548 if (ret != sizeof(attributes)) {
549 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
550 ret = WIMLIB_ERR_NTFS_3G;
554 if ((attributes & (FILE_ATTRIBUTE_DIRECTORY |
555 FILE_ATTRIBUTE_ENCRYPTED)) == FILE_ATTRIBUTE_ENCRYPTED)
557 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
559 ERROR("Can't archive unsupported encrypted file \"%s\"", path);
560 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
563 params->progress.scan.cur_path = path;
564 do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
569 /* Create a WIM dentry with an associated inode, which may be shared */
570 ret = inode_table_new_dentry(¶ms->inode_table,
571 path_basename_with_len(path, path_len),
572 ni->mft_no, 0, false, &root);
576 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
577 root->is_win32_name = 1;
579 inode = root->d_inode;
581 if (inode->i_nlink > 1) {
582 /* Shared inode; nothing more to do */
587 inode->i_creation_time = le64_to_cpu(ni->creation_time);
588 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
589 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
590 inode->i_attributes = le32_to_cpu(attributes);
591 inode->i_resolved = 1;
593 /* Capture streams. */
595 if (attributes & FILE_ATTR_REPARSE_POINT) {
596 /* Capture reparse data stream. */
597 ret = capture_ntfs_streams(inode, ni, path, path_len,
598 params->unhashed_streams,
599 vol, AT_REPARSE_POINT);
604 /* Capture data streams.
606 * Directories should not have an unnamed data stream, but they may have
607 * named data streams.
609 * Reparse points may have an unnamed data stream (which will be ignored
610 * in favor of the reparse data stream), and they also may have named
613 * Regular files can have an unnamed data stream as well as named data
615 ret = capture_ntfs_streams(inode, ni, path, path_len,
616 params->unhashed_streams, vol, AT_DATA);
620 if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
622 /* Recurse to directory children */
624 struct dos_name_map dos_name_map = { .root = NULL };
625 struct readdir_ctx ctx = {
628 .path_len = path_len,
629 .dos_name_map = &dos_name_map,
633 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
635 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
636 ret = WIMLIB_ERR_NTFS_3G;
638 struct wim_dentry *child;
641 for_dentry_child(child, root) {
642 ret = set_dentry_dos_name(child, &dos_name_map);
647 destroy_dos_name_map(&dos_name_map);
651 path[path_len] = '\0';
653 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
654 * only allow capturing an entire volume. */
655 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
656 inode_is_symlink(inode))
657 inode->i_not_rpfixed = 0;
659 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
660 struct SECURITY_CONTEXT sec_ctx;
664 /* Get security descriptor */
665 memset(&sec_ctx, 0, sizeof(sec_ctx));
670 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
671 if (ret > sizeof(_sd)) {
673 ret = ntfs_get_ntfs_acl(&sec_ctx, 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 params->progress.scan.cur_path = path;
701 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
703 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
708 free_dentry_tree(root, params->lookup_table);
714 do_ntfs_umount(struct _ntfs_volume *vol)
716 DEBUG("Unmounting NTFS volume");
717 if (ntfs_umount(vol, FALSE))
718 return WIMLIB_ERR_NTFS_3G;
724 build_dentry_tree_ntfs(struct wim_dentry **root_p,
726 struct add_image_params *params)
732 DEBUG("Mounting NTFS volume `%s' read-only", device);
734 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
737 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
738 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
739 * seem to contain any explicit version information. So we have to rely on a
740 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
742 #ifdef HAVE_NTFS_MNT_RDONLY
744 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
745 #elif defined(MS_RDONLY)
746 /* NTFS-3g 2011, 2012 */
747 vol = ntfs_mount(device, MS_RDONLY);
749 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
752 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
754 return WIMLIB_ERR_NTFS_3G;
756 ntfs_open_secure(vol);
758 /* We don't want to capture the special NTFS files such as $Bitmap. Not
759 * to be confused with "hidden" or "system" files which are real files
760 * that we do need to capture. */
761 NVolClearShowSysFiles(vol);
763 DEBUG("Opening root NTFS dentry");
764 root_ni = ntfs_inode_open(vol, FILE_root);
766 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
768 ret = WIMLIB_ERR_NTFS_3G;
772 /* Currently we assume that all the paths fit into this length and there
773 * is no check for overflow. */
774 char *path = MALLOC(32768);
776 ERROR("Could not allocate memory for NTFS pathname");
777 ret = WIMLIB_ERR_NOMEM;
783 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
784 FILE_NAME_POSIX, vol, params);
787 ntfs_inode_close(root_ni);
789 ntfs_index_ctx_put(vol->secure_xsii);
790 ntfs_index_ctx_put(vol->secure_xsdh);
791 ntfs_inode_close(vol->secure_ni);
794 if (do_ntfs_umount(vol)) {
795 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
798 ret = WIMLIB_ERR_NTFS_3G;
801 /* We need to leave the NTFS volume mounted so that we can read
802 * the NTFS files again when we are actually writing the WIM */
803 *(ntfs_volume**)params->extra_arg = vol;
807 #endif /* WITH_NTFS_3G */