4 * Capture a WIM image directly from an 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 free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
33 #include <ntfs-3g/attrib.h>
34 #include <ntfs-3g/reparse.h>
35 #include <ntfs-3g/security.h>
36 #include <ntfs-3g/volume.h>
38 #include "wimlib/alloca.h"
39 #include "wimlib/assert.h"
40 #include "wimlib/capture.h"
41 #include "wimlib/dentry.h"
42 #include "wimlib/encoding.h"
43 #include "wimlib/endianness.h"
44 #include "wimlib/error.h"
45 #include "wimlib/lookup_table.h"
46 #include "wimlib/ntfs_3g.h"
47 #include "wimlib/paths.h"
48 #include "wimlib/security.h"
50 static inline ntfschar *
51 attr_record_name(ATTR_RECORD *ar)
53 return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
57 open_ntfs_attr(ntfs_inode *ni, struct ntfs_location *loc)
61 na = ntfs_attr_open(ni,
62 loc->is_reparse_point ? AT_REPARSE_POINT : AT_DATA,
64 loc->stream_name_nchars);
66 ERROR_WITH_ERRNO("Failed to open attribute of \"%"TS"\" in "
67 "NTFS volume", loc->path);
73 read_ntfs_file_prefix(const struct wim_lookup_table_entry *lte, u64 size,
74 consume_data_callback_t cb, void *cb_ctx)
76 struct ntfs_location *loc = lte->ntfs_loc;
77 ntfs_volume *vol = loc->ntfs_vol;
85 ni = ntfs_pathname_to_inode(vol, NULL, loc->path);
87 ERROR_WITH_ERRNO("Can't find NTFS inode for \"%"TS"\"", loc->path);
88 ret = WIMLIB_ERR_NTFS_3G;
92 na = open_ntfs_attr(ni, loc);
94 ret = WIMLIB_ERR_NTFS_3G;
95 goto out_close_ntfs_inode;
98 pos = (loc->is_reparse_point) ? 8 : 0;
99 bytes_remaining = size;
100 while (bytes_remaining) {
101 s64 to_read = min(bytes_remaining, sizeof(buf));
102 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
103 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
104 ret = WIMLIB_ERR_NTFS_3G;
105 goto out_close_ntfs_attr;
108 bytes_remaining -= to_read;
109 ret = cb(buf, to_read, cb_ctx);
111 goto out_close_ntfs_attr;
116 out_close_ntfs_inode:
117 ntfs_inode_close(ni);
123 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
124 u32 *reparse_tag_ret)
130 na = open_ntfs_attr(ni, loc);
132 ret = WIMLIB_ERR_NTFS_3G;
136 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
137 &reparse_tag) != sizeof(reparse_tag))
139 ERROR_WITH_ERRNO("Error reading reparse data");
140 ret = WIMLIB_ERR_NTFS_3G;
141 goto out_close_ntfs_attr;
143 *reparse_tag_ret = le32_to_cpu(reparse_tag);
144 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
153 /* Load the streams from a file or reparse point in the NTFS volume */
155 capture_ntfs_streams(struct wim_inode *inode,
159 struct list_head *unhashed_streams,
163 ntfs_attr_search_ctx *actx;
164 struct ntfs_location *ntfs_loc;
166 struct wim_lookup_table_entry *lte;
168 DEBUG("Capturing NTFS data streams from `%s'", path);
170 /* Get context to search the streams of the NTFS file. */
171 actx = ntfs_attr_get_search_ctx(ni, NULL);
173 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
174 "context for \"%s\"", path);
175 return WIMLIB_ERR_NTFS_3G;
178 /* Capture each data stream or reparse data stream. */
179 while (!ntfs_attr_lookup(type, NULL, 0,
180 CASE_SENSITIVE, 0, NULL, 0, actx))
182 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
183 u64 name_length = actx->attr->name_length;
186 if (data_size == 0) {
187 /* Empty stream. No lookup table entry is needed. */
191 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
193 ret = WIMLIB_ERR_NOMEM;
196 ntfs_loc->ntfs_vol = vol;
197 ntfs_loc->path = memdup(path, path_len + 1);
198 if (!ntfs_loc->path) {
199 ret = WIMLIB_ERR_NOMEM;
200 goto out_free_ntfs_loc;
203 ntfs_loc->stream_name = memdup(attr_record_name(actx->attr),
205 if (!ntfs_loc->stream_name) {
206 ret = WIMLIB_ERR_NOMEM;
207 goto out_free_ntfs_loc;
209 ntfs_loc->stream_name_nchars = name_length;
212 lte = new_lookup_table_entry();
214 ret = WIMLIB_ERR_NOMEM;
215 goto out_free_ntfs_loc;
217 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
218 lte->ntfs_loc = ntfs_loc;
220 if (type == AT_REPARSE_POINT) {
222 ERROR("Invalid reparse data on \"%s\" "
223 "(only %u bytes)!", path, (unsigned)data_size);
224 ret = WIMLIB_ERR_NTFS_3G;
227 lte->ntfs_loc->is_reparse_point = true;
228 lte->size = data_size - 8;
229 ret = read_reparse_tag(ni, lte->ntfs_loc,
230 &inode->i_reparse_tag);
234 lte->ntfs_loc->is_reparse_point = false;
235 lte->size = data_size;
238 if (name_length == 0) {
239 /* Unnamed data stream. Put the reference to it in the
243 if (!(inode->i_attributes &
244 FILE_ATTRIBUTE_REPARSE_POINT))
246 WARNING("Found two un-named "
247 "data streams for \"%s\" "
248 "(sizes = %"PRIu64", "
254 free_lookup_table_entry(lte);
262 /* Named data stream. Put the reference to it in the
263 * alternate data stream entries */
264 struct wim_ads_entry *new_ads_entry;
266 new_ads_entry = inode_add_ads_utf16le(inode,
267 attr_record_name(actx->attr),
269 if (!new_ads_entry) {
270 ret = WIMLIB_ERR_NOMEM;
273 wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
274 stream_id = new_ads_entry->stream_id;
275 new_ads_entry->lte = lte;
278 add_unhashed_stream(lte, inode,
279 stream_id, unhashed_streams);
282 if (errno == ENOENT) {
285 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
286 ret = WIMLIB_ERR_NTFS_3G;
290 free_lookup_table_entry(lte);
293 FREE(ntfs_loc->path);
294 FREE(ntfs_loc->stream_name);
298 ntfs_attr_put_search_ctx(actx);
300 DEBUG("Successfully captured NTFS streams from \"%s\"", path);
302 ERROR("Failed to capture NTFS streams from \"%s\"", path);
306 /* Binary tree that maps NTFS inode numbers to DOS names */
307 struct dos_name_map {
308 struct avl_tree_node *root;
311 struct dos_name_node {
312 struct avl_tree_node index_node;
318 #define DOS_NAME_NODE(avl_node) \
319 avl_tree_entry(avl_node, struct dos_name_node, index_node)
322 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
323 const struct avl_tree_node *n2)
325 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
326 DOS_NAME_NODE(n2)->ntfs_ino);
329 /* Inserts a new DOS name into the map */
331 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
332 size_t name_nbytes, le64 ntfs_ino)
334 struct dos_name_node *new_node;
336 DEBUG("DOS name_len = %zu", name_nbytes);
337 new_node = MALLOC(sizeof(struct dos_name_node));
339 return WIMLIB_ERR_NOMEM;
341 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
342 * assuming 2-byte ntfs characters) */
343 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
345 /* Initialize the DOS name, DOS name length, and NTFS inode number of
346 * the search tree node */
347 memcpy(new_node->dos_name, dos_name, name_nbytes);
348 new_node->name_nbytes = name_nbytes;
349 new_node->ntfs_ino = ntfs_ino;
351 /* Insert the search tree node */
352 if (avl_tree_insert(&map->root, &new_node->index_node,
353 _avl_cmp_by_ntfs_ino))
355 /* This should be impossible since an NTFS inode cannot
356 * have multiple DOS names, and we only should get each
357 * DOS name entry once from the ntfs_readdir() calls. */
358 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
359 le64_to_cpu(ntfs_ino));
361 return WIMLIB_ERR_NOMEM;
363 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
367 /* Returns a structure that contains the DOS name and its length for an NTFS
368 * inode, or NULL if the inode has no DOS name. */
369 static struct dos_name_node *
370 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
372 struct dos_name_node dummy;
373 struct avl_tree_node *res;
375 dummy.ntfs_ino = cpu_to_le64(ntfs_ino);
377 res = avl_tree_lookup_node(map->root, &dummy.index_node,
378 _avl_cmp_by_ntfs_ino);
381 return DOS_NAME_NODE(res);
385 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
387 const struct dos_name_node *node;
389 if (dentry->is_win32_name) {
390 node = lookup_dos_name(map, dentry->d_inode->i_ino);
392 dentry->short_name = utf16le_dupz(node->dos_name,
394 if (!dentry->short_name)
395 return WIMLIB_ERR_NOMEM;
396 dentry->short_name_nbytes = node->name_nbytes;
397 DEBUG("Assigned DOS name to ino %"PRIu64,
398 dentry->d_inode->i_ino);
400 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
401 "corresponding DOS name",
402 dentry->d_inode->i_ino);
409 free_dos_name_tree(struct avl_tree_node *node) {
411 free_dos_name_tree(node->left);
412 free_dos_name_tree(node->right);
413 FREE(DOS_NAME_NODE(node));
418 destroy_dos_name_map(struct dos_name_map *map)
420 free_dos_name_tree(map->root);
424 struct wim_dentry *parent;
427 struct dos_name_map *dos_name_map;
429 struct capture_params *params;
434 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
439 ntfs_volume *ntfs_vol,
440 struct capture_params *params);
443 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
444 const int name_nchars, const int name_type,
445 const s64 pos, const MFT_REF mref,
446 const unsigned dt_type)
448 struct readdir_ctx *ctx;
449 size_t mbs_name_nbytes;
451 struct wim_dentry *child;
454 size_t name_nbytes = name_nchars * sizeof(ntfschar);
457 if (name_type & FILE_NAME_DOS) {
458 /* If this is the entry for a DOS name, store it for later. */
459 ret = insert_dos_name(ctx->dos_name_map, name,
460 name_nbytes, mref & MFT_REF_MASK_CPU);
462 /* Return now if an error occurred or if this is just a DOS name
463 * and not a Win32+DOS name. */
464 if (ret != 0 || name_type == FILE_NAME_DOS)
467 ret = utf16le_to_tstr(name, name_nbytes,
468 &mbs_name, &mbs_name_nbytes);
472 if (mbs_name[0] == '.' &&
473 (mbs_name[1] == '\0' ||
474 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
477 * note: name_type is POSIX for these, so DOS names will not
478 * have been inserted for them. */
480 goto out_free_mbs_name;
483 /* Open the inode for this directory entry and recursively capture the
484 * directory tree rooted at it */
485 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
487 /* XXX This used to be treated as an error, but NTFS-3g seemed
488 * to be unable to read some inodes on a Windows 8 image for
490 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
491 ctx->path, mbs_name);
493 goto out_free_mbs_name;
495 path_len = ctx->path_len;
497 ctx->path[path_len++] = '/';
498 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
499 path_len += mbs_name_nbytes;
501 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
503 ctx->vol, ctx->params);
504 path_len -= mbs_name_nbytes + 1;
506 dentry_add_child(ctx->parent, child);
507 ntfs_inode_close(ni);
511 ctx->path[ctx->path_len] = '\0';
516 /* Recursively build a WIM dentry tree corresponding to an NTFS volume.
517 * At the same time, update the WIM lookup table with lookup table entries for
518 * the NTFS streams, and build an array of security descriptors.
521 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
527 struct capture_params *params)
531 struct wim_dentry *root = NULL;
532 struct wim_inode *inode = NULL;
534 ret = try_exclude(path, path_len, params);
535 if (ret < 0) /* Excluded? */
537 if (ret > 0) /* Error? */
540 /* Get file attributes */
541 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
542 if (ret != sizeof(attributes)) {
543 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
544 ret = WIMLIB_ERR_NTFS_3G;
548 if ((attributes & (FILE_ATTRIBUTE_DIRECTORY |
549 FILE_ATTRIBUTE_ENCRYPTED)) == FILE_ATTRIBUTE_ENCRYPTED)
551 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
553 ERROR("Can't archive unsupported encrypted file \"%s\"", path);
554 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
557 params->progress.scan.cur_path = path;
558 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
562 /* Create a WIM dentry with an associated inode, which may be shared */
563 ret = inode_table_new_dentry(params->inode_table,
564 path_basename_with_len(path, path_len),
565 ni->mft_no, 0, false, &root);
569 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
570 root->is_win32_name = 1;
572 inode = root->d_inode;
574 if (inode->i_nlink > 1) {
575 /* Shared inode; nothing more to do */
579 inode->i_creation_time = le64_to_cpu(ni->creation_time);
580 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
581 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
582 inode->i_attributes = attributes;
583 inode->i_resolved = 1;
585 /* Capture streams. */
587 if (attributes & FILE_ATTR_REPARSE_POINT) {
588 /* Capture reparse data stream. */
589 ret = capture_ntfs_streams(inode, ni, path, path_len,
590 params->unhashed_streams,
591 vol, AT_REPARSE_POINT);
596 /* Capture data streams.
598 * Directories should not have an unnamed data stream, but they may have
599 * named data streams.
601 * Reparse points may have an unnamed data stream (which will be ignored
602 * in favor of the reparse data stream), and they also may have named
605 * Regular files can have an unnamed data stream as well as named data
607 ret = capture_ntfs_streams(inode, ni, path, path_len,
608 params->unhashed_streams, vol, AT_DATA);
612 if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
614 /* Recurse to directory children */
616 struct dos_name_map dos_name_map = { .root = NULL };
617 struct readdir_ctx ctx = {
620 .path_len = path_len,
621 .dos_name_map = &dos_name_map,
626 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
632 /* error from ntfs_readdir() itself */
633 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
634 ret = WIMLIB_ERR_NTFS_3G;
637 struct wim_dentry *child;
640 for_dentry_child(child, root) {
641 ret = set_dentry_dos_name(child, &dos_name_map);
646 destroy_dos_name_map(&dos_name_map);
650 path[path_len] = '\0';
652 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
653 * only allow capturing an entire volume. */
654 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
655 inode_is_symlink(inode))
656 inode->i_not_rpfixed = 0;
658 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
659 struct SECURITY_CONTEXT sec_ctx;
663 /* Get security descriptor */
664 memset(&sec_ctx, 0, sizeof(sec_ctx));
669 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
670 if (ret > sizeof(_sd)) {
672 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
675 inode->i_security_id = sd_set_add_sd(params->sd_set,
677 if (inode->i_security_id == -1) {
678 ERROR("Out of memory");
679 ret = WIMLIB_ERR_NOMEM;
682 DEBUG("Added security ID = %u for `%s'",
683 inode->i_security_id, path);
685 } else if (ret < 0) {
686 ERROR_WITH_ERRNO("Failed to get security information from "
688 ret = WIMLIB_ERR_NTFS_3G;
690 inode->i_security_id = -1;
691 DEBUG("No security ID for `%s'", path);
698 params->progress.scan.cur_path = path;
700 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
702 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
705 free_dentry_tree(root, params->lookup_table);
707 ret = report_capture_error(params, ret, path);
715 do_ntfs_umount(struct _ntfs_volume *vol)
717 DEBUG("Unmounting NTFS volume");
718 if (ntfs_umount(vol, FALSE))
719 return WIMLIB_ERR_NTFS_3G;
725 build_dentry_tree_ntfs(struct wim_dentry **root_p,
727 struct capture_params *params)
733 DEBUG("Mounting NTFS volume `%s' read-only", device);
735 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
738 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
739 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
740 * seem to contain any explicit version information. So we have to rely on a
741 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
743 #ifdef HAVE_NTFS_MNT_RDONLY
745 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
746 #elif defined(MS_RDONLY)
747 /* NTFS-3g 2011, 2012 */
748 vol = ntfs_mount(device, MS_RDONLY);
750 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
753 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
755 return WIMLIB_ERR_NTFS_3G;
757 ntfs_open_secure(vol);
759 /* We don't want to capture the special NTFS files such as $Bitmap. Not
760 * to be confused with "hidden" or "system" files which are real files
761 * that we do need to capture. */
762 NVolClearShowSysFiles(vol);
764 DEBUG("Opening root NTFS dentry");
765 root_ni = ntfs_inode_open(vol, FILE_root);
767 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
769 ret = WIMLIB_ERR_NTFS_3G;
773 /* Currently we assume that all the paths fit into this length and there
774 * is no check for overflow. */
775 char *path = MALLOC(32768);
777 ERROR("Could not allocate memory for NTFS pathname");
778 ret = WIMLIB_ERR_NOMEM;
784 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
785 FILE_NAME_POSIX, vol, params);
788 ntfs_inode_close(root_ni);
790 ntfs_index_ctx_put(vol->secure_xsii);
791 ntfs_index_ctx_put(vol->secure_xsdh);
792 ntfs_inode_close(vol->secure_ni);
795 if (do_ntfs_umount(vol)) {
796 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
800 /* We need to leave the NTFS volume mounted so that we can read
801 * the NTFS files again when we are actually writing the WIM */
802 *(ntfs_volume**)params->extra_arg = vol;
806 #endif /* WITH_NTFS_3G */