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, 2015 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/blob_table.h"
41 #include "wimlib/capture.h"
42 #include "wimlib/dentry.h"
43 #include "wimlib/encoding.h"
44 #include "wimlib/endianness.h"
45 #include "wimlib/error.h"
46 #include "wimlib/ntfs_3g.h"
47 #include "wimlib/paths.h"
48 #include "wimlib/reparse.h"
49 #include "wimlib/security.h"
51 static inline ntfschar *
52 attr_record_name(ATTR_RECORD *ar)
54 return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
58 open_ntfs_attr(ntfs_inode *ni, const struct ntfs_location *loc)
62 na = ntfs_attr_open(ni,
63 (ATTR_TYPES)loc->attr_type,
65 loc->attr_name_nchars);
67 ERROR_WITH_ERRNO("Failed to open attribute of \"%"TS"\" in "
68 "NTFS volume", loc->path);
74 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
75 consume_data_callback_t cb, void *cb_ctx)
77 const struct ntfs_location *loc = blob->ntfs_loc;
78 ntfs_volume *vol = loc->ntfs_vol;
86 ni = ntfs_pathname_to_inode(vol, NULL, loc->path);
88 ERROR_WITH_ERRNO("Can't find NTFS inode for \"%"TS"\"", loc->path);
89 ret = WIMLIB_ERR_NTFS_3G;
93 na = open_ntfs_attr(ni, loc);
95 ret = WIMLIB_ERR_NTFS_3G;
96 goto out_close_ntfs_inode;
99 pos = (loc->attr_type == AT_REPARSE_POINT) ? REPARSE_DATA_OFFSET : 0;
100 bytes_remaining = size;
101 while (bytes_remaining) {
102 s64 to_read = min(bytes_remaining, sizeof(buf));
103 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
104 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", loc->path);
105 ret = WIMLIB_ERR_NTFS_3G;
106 goto out_close_ntfs_attr;
109 bytes_remaining -= to_read;
110 ret = cb(buf, to_read, cb_ctx);
112 goto out_close_ntfs_attr;
117 out_close_ntfs_inode:
118 ntfs_inode_close(ni);
124 read_reparse_tag(ntfs_inode *ni, struct ntfs_location *loc,
125 u32 *reparse_tag_ret)
131 na = open_ntfs_attr(ni, loc);
133 ret = WIMLIB_ERR_NTFS_3G;
137 if (ntfs_attr_pread(na, 0, sizeof(reparse_tag),
138 &reparse_tag) != sizeof(reparse_tag))
140 ERROR_WITH_ERRNO("Error reading reparse data");
141 ret = WIMLIB_ERR_NTFS_3G;
142 goto out_close_ntfs_attr;
144 *reparse_tag_ret = le32_to_cpu(reparse_tag);
145 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
155 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
159 return STREAM_TYPE_DATA;
160 case AT_REPARSE_POINT:
161 return STREAM_TYPE_REPARSE_POINT;
164 return STREAM_TYPE_UNKNOWN;
168 /* Load attributes of the specified type from a file in the NTFS volume */
170 load_ntfs_attrs_with_type(struct wim_inode *inode,
174 struct list_head *unhashed_blobs,
178 ntfs_attr_search_ctx *actx;
179 struct ntfs_location *ntfs_loc;
181 struct blob_descriptor *blob;
182 utf16lechar *stream_name;
184 DEBUG("Loading NTFS attributes from \"%s\"", path);
186 /* Get context to search the attributes of the NTFS file. */
187 actx = ntfs_attr_get_search_ctx(ni, NULL);
189 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
190 "context for \"%s\"", path);
191 return WIMLIB_ERR_NTFS_3G;
194 /* Save each attribute */
195 while (!ntfs_attr_lookup(type, NULL, 0,
196 CASE_SENSITIVE, 0, NULL, 0, actx))
198 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
199 size_t name_nchars = actx->attr->name_length;
200 struct wim_inode_stream *strm;
203 stream_name = utf16le_dupz(attr_record_name(actx->attr),
204 name_nchars * sizeof(ntfschar));
206 ret = WIMLIB_ERR_NOMEM;
213 if (data_size == 0) {
214 /* Empty attribute. No blob is needed. */
218 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
220 ret = WIMLIB_ERR_NOMEM;
221 goto out_free_stream_name;
223 ntfs_loc->ntfs_vol = vol;
224 ntfs_loc->attr_type = type;
225 ntfs_loc->path = memdup(path, path_len + 1);
226 if (!ntfs_loc->path) {
227 ret = WIMLIB_ERR_NOMEM;
228 goto out_free_ntfs_loc;
231 ntfs_loc->attr_name = utf16le_dup(stream_name);
232 if (!ntfs_loc->attr_name) {
233 ret = WIMLIB_ERR_NOMEM;
234 goto out_free_ntfs_loc;
236 ntfs_loc->attr_name_nchars = name_nchars;
239 blob = new_blob_descriptor();
241 ret = WIMLIB_ERR_NOMEM;
242 goto out_free_ntfs_loc;
244 blob->blob_location = BLOB_IN_NTFS_VOLUME;
245 blob->ntfs_loc = ntfs_loc;
246 blob->size = data_size;
248 if (type == AT_REPARSE_POINT) {
249 if (data_size < REPARSE_DATA_OFFSET) {
250 ERROR("Reparse data of \"%s\" "
251 "is invalid (only %u bytes)!",
252 path, (unsigned)data_size);
253 ret = WIMLIB_ERR_NTFS_3G;
256 blob->size -= REPARSE_DATA_OFFSET;
257 ret = read_reparse_tag(ni, blob->ntfs_loc,
258 &inode->i_reparse_tag);
264 strm = inode_add_stream(inode,
265 attr_type_to_wimlib_stream_type(type),
266 stream_name ? stream_name : NO_STREAM_NAME,
269 ret = WIMLIB_ERR_NOMEM;
272 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
277 if (errno == ENOENT) {
280 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
281 ret = WIMLIB_ERR_NTFS_3G;
285 free_blob_descriptor(blob);
288 FREE(ntfs_loc->path);
289 FREE(ntfs_loc->attr_name);
292 out_free_stream_name:
295 ntfs_attr_put_search_ctx(actx);
297 DEBUG("Successfully loaded NTFS attributes from \"%s\"", path);
299 ERROR("Failed to load NTFS attributes from \"%s\"", path);
303 /* Binary tree that maps NTFS inode numbers to DOS names */
304 struct dos_name_map {
305 struct avl_tree_node *root;
308 struct dos_name_node {
309 struct avl_tree_node index_node;
315 #define DOS_NAME_NODE(avl_node) \
316 avl_tree_entry(avl_node, struct dos_name_node, index_node)
319 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
320 const struct avl_tree_node *n2)
322 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
323 DOS_NAME_NODE(n2)->ntfs_ino);
326 /* Inserts a new DOS name into the map */
328 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
329 size_t name_nbytes, le64 ntfs_ino)
331 struct dos_name_node *new_node;
333 DEBUG("DOS name_len = %zu", name_nbytes);
334 new_node = MALLOC(sizeof(struct dos_name_node));
336 return WIMLIB_ERR_NOMEM;
338 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
339 * assuming 2-byte ntfs characters) */
340 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
342 /* Initialize the DOS name, DOS name length, and NTFS inode number of
343 * the search tree node */
344 memcpy(new_node->dos_name, dos_name, name_nbytes);
345 new_node->name_nbytes = name_nbytes;
346 new_node->ntfs_ino = ntfs_ino;
348 /* Insert the search tree node */
349 if (avl_tree_insert(&map->root, &new_node->index_node,
350 _avl_cmp_by_ntfs_ino))
352 /* This should be impossible since an NTFS inode cannot
353 * have multiple DOS names, and we only should get each
354 * DOS name entry once from the ntfs_readdir() calls. */
355 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
356 le64_to_cpu(ntfs_ino));
358 return WIMLIB_ERR_NOMEM;
360 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
364 /* Returns a structure that contains the DOS name and its length for an NTFS
365 * inode, or NULL if the inode has no DOS name. */
366 static struct dos_name_node *
367 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
369 struct dos_name_node dummy;
370 struct avl_tree_node *res;
372 dummy.ntfs_ino = cpu_to_le64(ntfs_ino);
374 res = avl_tree_lookup_node(map->root, &dummy.index_node,
375 _avl_cmp_by_ntfs_ino);
378 return DOS_NAME_NODE(res);
382 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
384 const struct dos_name_node *node;
386 if (dentry->is_win32_name) {
387 node = lookup_dos_name(map, dentry->d_inode->i_ino);
389 dentry->short_name = utf16le_dupz(node->dos_name,
391 if (!dentry->short_name)
392 return WIMLIB_ERR_NOMEM;
393 dentry->short_name_nbytes = node->name_nbytes;
394 DEBUG("Assigned DOS name to ino %"PRIu64,
395 dentry->d_inode->i_ino);
397 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
398 "corresponding DOS name",
399 dentry->d_inode->i_ino);
406 free_dos_name_tree(struct avl_tree_node *node) {
408 free_dos_name_tree(node->left);
409 free_dos_name_tree(node->right);
410 FREE(DOS_NAME_NODE(node));
415 destroy_dos_name_map(struct dos_name_map *map)
417 free_dos_name_tree(map->root);
421 struct wim_dentry *parent;
424 struct dos_name_map *dos_name_map;
426 struct capture_params *params;
431 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
436 ntfs_volume *ntfs_vol,
437 struct capture_params *params);
440 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
441 const int name_nchars, const int name_type,
442 const s64 pos, const MFT_REF mref,
443 const unsigned dt_type)
445 struct readdir_ctx *ctx;
446 size_t mbs_name_nbytes;
448 struct wim_dentry *child;
451 size_t name_nbytes = name_nchars * sizeof(ntfschar);
454 if (name_type & FILE_NAME_DOS) {
455 /* If this is the entry for a DOS name, store it for later. */
456 ret = insert_dos_name(ctx->dos_name_map, name,
457 name_nbytes, mref & MFT_REF_MASK_CPU);
459 /* Return now if an error occurred or if this is just a DOS name
460 * and not a Win32+DOS name. */
461 if (ret != 0 || name_type == FILE_NAME_DOS)
464 ret = utf16le_to_tstr(name, name_nbytes,
465 &mbs_name, &mbs_name_nbytes);
469 if (mbs_name[0] == '.' &&
470 (mbs_name[1] == '\0' ||
471 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
474 * note: name_type is POSIX for these, so DOS names will not
475 * have been inserted for them. */
477 goto out_free_mbs_name;
480 /* Open the inode for this directory entry and recursively capture the
481 * directory tree rooted at it */
482 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
484 /* XXX This used to be treated as an error, but NTFS-3g seemed
485 * to be unable to read some inodes on a Windows 8 image for
487 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
488 ctx->path, mbs_name);
490 goto out_free_mbs_name;
492 path_len = ctx->path_len;
494 ctx->path[path_len++] = '/';
495 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
496 path_len += mbs_name_nbytes;
498 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
500 ctx->vol, ctx->params);
501 path_len -= mbs_name_nbytes + 1;
503 dentry_add_child(ctx->parent, child);
504 ntfs_inode_close(ni);
508 ctx->path[ctx->path_len] = '\0';
513 /* Recursive scan routine for NTFS volumes */
515 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
521 struct capture_params *params)
525 struct wim_dentry *root = NULL;
526 struct wim_inode *inode = NULL;
528 ret = try_exclude(path, path_len, params);
529 if (ret < 0) /* Excluded? */
531 if (ret > 0) /* Error? */
534 /* Get file attributes */
535 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
536 if (ret != sizeof(attributes)) {
537 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
538 ret = WIMLIB_ERR_NTFS_3G;
542 if (attributes & FILE_ATTRIBUTE_ENCRYPTED) {
543 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
545 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
546 "does not support encrypted files and directories", path);
547 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
550 params->progress.scan.cur_path = path;
551 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
555 /* Create a WIM dentry with an associated inode, which may be shared */
556 ret = inode_table_new_dentry(params->inode_table,
557 path_basename_with_len(path, path_len),
558 ni->mft_no, 0, false, &root);
562 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
563 root->is_win32_name = 1;
565 inode = root->d_inode;
567 if (inode->i_nlink > 1) {
568 /* Shared inode; nothing more to do */
572 inode->i_creation_time = le64_to_cpu(ni->creation_time);
573 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
574 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
575 inode->i_attributes = attributes;
577 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
578 /* Load the reparse point stream. */
579 ret = load_ntfs_attrs_with_type(inode, ni, path, path_len,
580 params->unhashed_blobs,
581 vol, AT_REPARSE_POINT);
586 /* Load the data streams.
588 * Note: directories should not have an unnamed data stream, but they
589 * may have named data streams. Nondirectories (including reparse
590 * points) can have an unnamed data stream as well as named data
592 ret = load_ntfs_attrs_with_type(inode, ni, path, path_len,
593 params->unhashed_blobs, vol, AT_DATA);
597 if (inode_is_directory(inode)) {
599 /* Recurse to directory children */
601 struct dos_name_map dos_name_map = { .root = NULL };
602 struct readdir_ctx ctx = {
605 .path_len = path_len,
606 .dos_name_map = &dos_name_map,
611 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
617 /* error from ntfs_readdir() itself */
618 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
619 ret = WIMLIB_ERR_NTFS_3G;
622 struct wim_dentry *child;
625 for_dentry_child(child, root) {
626 ret = set_dentry_dos_name(child, &dos_name_map);
631 destroy_dos_name_map(&dos_name_map);
635 path[path_len] = '\0';
637 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
638 * only allow capturing an entire volume. */
639 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
640 inode_is_symlink(inode))
641 inode->i_not_rpfixed = 0;
643 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
644 struct SECURITY_CONTEXT sec_ctx;
648 /* Get security descriptor */
649 memset(&sec_ctx, 0, sizeof(sec_ctx));
654 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
655 if (ret > sizeof(_sd)) {
657 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
660 inode->i_security_id = sd_set_add_sd(params->sd_set,
662 if (inode->i_security_id == -1) {
663 ERROR("Out of memory");
664 ret = WIMLIB_ERR_NOMEM;
667 DEBUG("Added security ID = %u for `%s'",
668 inode->i_security_id, path);
670 } else if (ret < 0) {
671 ERROR_WITH_ERRNO("Failed to get security information from "
673 ret = WIMLIB_ERR_NTFS_3G;
675 inode->i_security_id = -1;
676 DEBUG("No security ID for `%s'", path);
683 params->progress.scan.cur_path = path;
685 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
687 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
690 free_dentry_tree(root, params->blob_table);
692 ret = report_capture_error(params, ret, path);
700 do_ntfs_umount(struct _ntfs_volume *vol)
702 DEBUG("Unmounting NTFS volume");
703 if (ntfs_umount(vol, FALSE))
704 return WIMLIB_ERR_NTFS_3G;
710 build_dentry_tree_ntfs(struct wim_dentry **root_p,
712 struct capture_params *params)
718 DEBUG("Mounting NTFS volume `%s' read-only", device);
720 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
723 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
724 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
725 * seem to contain any explicit version information. So we have to rely on a
726 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
728 #ifdef HAVE_NTFS_MNT_RDONLY
730 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
731 #elif defined(MS_RDONLY)
732 /* NTFS-3g 2011, 2012 */
733 vol = ntfs_mount(device, MS_RDONLY);
735 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
738 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
740 return WIMLIB_ERR_NTFS_3G;
742 ntfs_open_secure(vol);
744 /* We don't want to capture the special NTFS files such as $Bitmap. Not
745 * to be confused with "hidden" or "system" files which are real files
746 * that we do need to capture. */
747 NVolClearShowSysFiles(vol);
749 DEBUG("Opening root NTFS dentry");
750 root_ni = ntfs_inode_open(vol, FILE_root);
752 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
754 ret = WIMLIB_ERR_NTFS_3G;
758 /* Currently we assume that all the paths fit into this length and there
759 * is no check for overflow. */
760 char *path = MALLOC(32768);
762 ERROR("Could not allocate memory for NTFS pathname");
763 ret = WIMLIB_ERR_NOMEM;
769 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
770 FILE_NAME_POSIX, vol, params);
773 ntfs_inode_close(root_ni);
775 ntfs_index_ctx_put(vol->secure_xsii);
776 ntfs_index_ctx_put(vol->secure_xsdh);
777 ntfs_inode_close(vol->secure_ni);
780 if (do_ntfs_umount(vol)) {
781 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
785 /* We need to leave the NTFS volume mounted so that we can read
786 * the NTFS files again when we are actually writing the WIM */
787 *(ntfs_volume**)params->extra_arg = vol;
791 #endif /* WITH_NTFS_3G */