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;
183 DEBUG("Loading NTFS attributes from \"%s\"", path);
185 /* Get context to search the attributes of the NTFS file. */
186 actx = ntfs_attr_get_search_ctx(ni, NULL);
188 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
189 "context for \"%s\"", path);
190 return WIMLIB_ERR_NTFS_3G;
193 /* Save each attribute */
194 while (!ntfs_attr_lookup(type, NULL, 0,
195 CASE_SENSITIVE, 0, NULL, 0, actx))
197 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
198 size_t name_nchars = actx->attr->name_length;
199 struct wim_inode_stream *strm;
200 const utf16lechar *stream_name = NO_STREAM_NAME;
202 if (data_size == 0) {
203 /* Empty attribute. No blob is needed. */
207 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
209 ret = WIMLIB_ERR_NOMEM;
212 ntfs_loc->ntfs_vol = vol;
213 ntfs_loc->attr_type = type;
214 ntfs_loc->path = memdup(path, path_len + 1);
215 if (!ntfs_loc->path) {
216 ret = WIMLIB_ERR_NOMEM;
217 goto out_free_ntfs_loc;
220 ntfs_loc->attr_name =
221 utf16le_dupz(attr_record_name(actx->attr),
222 name_nchars * sizeof(ntfschar));
223 if (!ntfs_loc->attr_name) {
224 ret = WIMLIB_ERR_NOMEM;
225 goto out_free_ntfs_loc;
227 ntfs_loc->attr_name_nchars = name_nchars;
228 stream_name = ntfs_loc->attr_name;
231 blob = new_blob_descriptor();
233 ret = WIMLIB_ERR_NOMEM;
234 goto out_free_ntfs_loc;
236 blob->blob_location = BLOB_IN_NTFS_VOLUME;
237 blob->ntfs_loc = ntfs_loc;
238 blob->size = data_size;
240 if (type == AT_REPARSE_POINT) {
241 if (data_size < REPARSE_DATA_OFFSET) {
242 ERROR("Reparse data of \"%s\" "
243 "is invalid (only %u bytes)!",
244 path, (unsigned)data_size);
245 ret = WIMLIB_ERR_NTFS_3G;
248 blob->size -= REPARSE_DATA_OFFSET;
249 ret = read_reparse_tag(ni, blob->ntfs_loc,
250 &inode->i_reparse_tag);
256 strm = inode_add_stream(inode,
257 attr_type_to_wimlib_stream_type(type),
261 ret = WIMLIB_ERR_NOMEM;
264 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
266 if (errno == ENOENT) {
269 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
270 ret = WIMLIB_ERR_NTFS_3G;
274 free_blob_descriptor(blob);
277 FREE(ntfs_loc->path);
278 FREE(ntfs_loc->attr_name);
282 ntfs_attr_put_search_ctx(actx);
284 DEBUG("Successfully loaded NTFS attributes from \"%s\"", path);
286 ERROR("Failed to load NTFS attributes from \"%s\"", path);
290 /* Binary tree that maps NTFS inode numbers to DOS names */
291 struct dos_name_map {
292 struct avl_tree_node *root;
295 struct dos_name_node {
296 struct avl_tree_node index_node;
302 #define DOS_NAME_NODE(avl_node) \
303 avl_tree_entry(avl_node, struct dos_name_node, index_node)
306 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
307 const struct avl_tree_node *n2)
309 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
310 DOS_NAME_NODE(n2)->ntfs_ino);
313 /* Inserts a new DOS name into the map */
315 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
316 size_t name_nbytes, le64 ntfs_ino)
318 struct dos_name_node *new_node;
320 DEBUG("DOS name_len = %zu", name_nbytes);
321 new_node = MALLOC(sizeof(struct dos_name_node));
323 return WIMLIB_ERR_NOMEM;
325 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
326 * assuming 2-byte ntfs characters) */
327 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
329 /* Initialize the DOS name, DOS name length, and NTFS inode number of
330 * the search tree node */
331 memcpy(new_node->dos_name, dos_name, name_nbytes);
332 new_node->name_nbytes = name_nbytes;
333 new_node->ntfs_ino = ntfs_ino;
335 /* Insert the search tree node */
336 if (avl_tree_insert(&map->root, &new_node->index_node,
337 _avl_cmp_by_ntfs_ino))
339 /* This should be impossible since an NTFS inode cannot
340 * have multiple DOS names, and we only should get each
341 * DOS name entry once from the ntfs_readdir() calls. */
342 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
343 le64_to_cpu(ntfs_ino));
345 return WIMLIB_ERR_NOMEM;
347 DEBUG("Inserted DOS name for inode %"PRIu64, le64_to_cpu(ntfs_ino));
351 /* Returns a structure that contains the DOS name and its length for an NTFS
352 * inode, or NULL if the inode has no DOS name. */
353 static struct dos_name_node *
354 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
356 struct dos_name_node dummy;
357 struct avl_tree_node *res;
359 dummy.ntfs_ino = cpu_to_le64(ntfs_ino);
361 res = avl_tree_lookup_node(map->root, &dummy.index_node,
362 _avl_cmp_by_ntfs_ino);
365 return DOS_NAME_NODE(res);
369 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
371 const struct dos_name_node *node;
373 if (dentry->is_win32_name) {
374 node = lookup_dos_name(map, dentry->d_inode->i_ino);
376 dentry->short_name = utf16le_dupz(node->dos_name,
378 if (!dentry->short_name)
379 return WIMLIB_ERR_NOMEM;
380 dentry->short_name_nbytes = node->name_nbytes;
381 DEBUG("Assigned DOS name to ino %"PRIu64,
382 dentry->d_inode->i_ino);
384 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
385 "corresponding DOS name",
386 dentry->d_inode->i_ino);
393 free_dos_name_tree(struct avl_tree_node *node) {
395 free_dos_name_tree(node->left);
396 free_dos_name_tree(node->right);
397 FREE(DOS_NAME_NODE(node));
402 destroy_dos_name_map(struct dos_name_map *map)
404 free_dos_name_tree(map->root);
408 struct wim_dentry *parent;
411 struct dos_name_map *dos_name_map;
413 struct capture_params *params;
418 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
423 ntfs_volume *ntfs_vol,
424 struct capture_params *params);
427 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
428 const int name_nchars, const int name_type,
429 const s64 pos, const MFT_REF mref,
430 const unsigned dt_type)
432 struct readdir_ctx *ctx;
433 size_t mbs_name_nbytes;
435 struct wim_dentry *child;
438 size_t name_nbytes = name_nchars * sizeof(ntfschar);
441 if (name_type & FILE_NAME_DOS) {
442 /* If this is the entry for a DOS name, store it for later. */
443 ret = insert_dos_name(ctx->dos_name_map, name,
444 name_nbytes, mref & MFT_REF_MASK_CPU);
446 /* Return now if an error occurred or if this is just a DOS name
447 * and not a Win32+DOS name. */
448 if (ret != 0 || name_type == FILE_NAME_DOS)
451 ret = utf16le_to_tstr(name, name_nbytes,
452 &mbs_name, &mbs_name_nbytes);
456 if (mbs_name[0] == '.' &&
457 (mbs_name[1] == '\0' ||
458 (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
461 * note: name_type is POSIX for these, so DOS names will not
462 * have been inserted for them. */
464 goto out_free_mbs_name;
467 /* Open the inode for this directory entry and recursively capture the
468 * directory tree rooted at it */
469 ntfs_inode *ni = ntfs_inode_open(ctx->vol, mref);
471 /* XXX This used to be treated as an error, but NTFS-3g seemed
472 * to be unable to read some inodes on a Windows 8 image for
474 WARNING_WITH_ERRNO("Failed to open NTFS file \"%s/%s\"",
475 ctx->path, mbs_name);
477 goto out_free_mbs_name;
479 path_len = ctx->path_len;
481 ctx->path[path_len++] = '/';
482 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
483 path_len += mbs_name_nbytes;
485 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path,
487 ctx->vol, ctx->params);
488 path_len -= mbs_name_nbytes + 1;
490 dentry_add_child(ctx->parent, child);
491 ntfs_inode_close(ni);
495 ctx->path[ctx->path_len] = '\0';
500 /* Recursive scan routine for NTFS volumes */
502 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
508 struct capture_params *params)
512 struct wim_dentry *root = NULL;
513 struct wim_inode *inode = NULL;
515 ret = try_exclude(path, path_len, params);
516 if (ret < 0) /* Excluded? */
518 if (ret > 0) /* Error? */
521 /* Get file attributes */
522 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
523 if (ret != sizeof(attributes)) {
524 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
525 ret = WIMLIB_ERR_NTFS_3G;
529 if (attributes & FILE_ATTRIBUTE_ENCRYPTED) {
530 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
532 ERROR("Can't archive \"%s\" because NTFS-3g capture mode "
533 "does not support encrypted files and directories", path);
534 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
537 params->progress.scan.cur_path = path;
538 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
542 /* Create a WIM dentry with an associated inode, which may be shared */
543 ret = inode_table_new_dentry(params->inode_table,
544 path_basename_with_len(path, path_len),
545 ni->mft_no, 0, false, &root);
549 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
550 root->is_win32_name = 1;
552 inode = root->d_inode;
554 if (inode->i_nlink > 1) {
555 /* Shared inode; nothing more to do */
559 inode->i_creation_time = le64_to_cpu(ni->creation_time);
560 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
561 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
562 inode->i_attributes = attributes;
564 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
565 /* Load the reparse point stream. */
566 ret = load_ntfs_attrs_with_type(inode, ni, path, path_len,
567 params->unhashed_blobs,
568 vol, AT_REPARSE_POINT);
573 /* Load the data streams.
575 * Note: directories should not have an unnamed data stream, but they
576 * may have named data streams. Nondirectories (including reparse
577 * points) can have an unnamed data stream as well as named data
579 ret = load_ntfs_attrs_with_type(inode, ni, path, path_len,
580 params->unhashed_blobs, vol, AT_DATA);
584 if (inode_is_directory(inode)) {
586 /* Recurse to directory children */
588 struct dos_name_map dos_name_map = { .root = NULL };
589 struct readdir_ctx ctx = {
592 .path_len = path_len,
593 .dos_name_map = &dos_name_map,
598 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
604 /* error from ntfs_readdir() itself */
605 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
606 ret = WIMLIB_ERR_NTFS_3G;
609 struct wim_dentry *child;
612 for_dentry_child(child, root) {
613 ret = set_dentry_dos_name(child, &dos_name_map);
618 destroy_dos_name_map(&dos_name_map);
622 path[path_len] = '\0';
624 /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
625 * only allow capturing an entire volume. */
626 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
627 inode_is_symlink(inode))
628 inode->i_not_rpfixed = 0;
630 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
631 struct SECURITY_CONTEXT sec_ctx;
635 /* Get security descriptor */
636 memset(&sec_ctx, 0, sizeof(sec_ctx));
641 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, sizeof(_sd));
642 if (ret > sizeof(_sd)) {
644 ret = ntfs_get_ntfs_acl(&sec_ctx, ni, sd, ret);
647 inode->i_security_id = sd_set_add_sd(params->sd_set,
649 if (inode->i_security_id == -1) {
650 ERROR("Out of memory");
651 ret = WIMLIB_ERR_NOMEM;
654 DEBUG("Added security ID = %u for `%s'",
655 inode->i_security_id, path);
657 } else if (ret < 0) {
658 ERROR_WITH_ERRNO("Failed to get security information from "
660 ret = WIMLIB_ERR_NTFS_3G;
662 inode->i_security_id = -1;
663 DEBUG("No security ID for `%s'", path);
670 params->progress.scan.cur_path = path;
672 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
674 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
677 free_dentry_tree(root, params->blob_table);
679 ret = report_capture_error(params, ret, path);
687 do_ntfs_umount(struct _ntfs_volume *vol)
689 DEBUG("Unmounting NTFS volume");
690 if (ntfs_umount(vol, FALSE))
691 return WIMLIB_ERR_NTFS_3G;
697 build_dentry_tree_ntfs(struct wim_dentry **root_p,
699 struct capture_params *params)
705 DEBUG("Mounting NTFS volume `%s' read-only", device);
707 /* NTFS-3g 2013 renamed the "read-only" mount flag from MS_RDONLY to
710 * Unfortunately we can't check for defined(NTFS_MNT_RDONLY) because
711 * NTFS_MNT_RDONLY is an enumerated constant. Also, the NTFS-3g headers don't
712 * seem to contain any explicit version information. So we have to rely on a
713 * test done at configure time to detect whether NTFS_MNT_RDONLY should be used.
715 #ifdef HAVE_NTFS_MNT_RDONLY
717 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
718 #elif defined(MS_RDONLY)
719 /* NTFS-3g 2011, 2012 */
720 vol = ntfs_mount(device, MS_RDONLY);
722 #error "Can't find NTFS_MNT_RDONLY or MS_RDONLY flags"
725 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
727 return WIMLIB_ERR_NTFS_3G;
729 ntfs_open_secure(vol);
731 /* We don't want to capture the special NTFS files such as $Bitmap. Not
732 * to be confused with "hidden" or "system" files which are real files
733 * that we do need to capture. */
734 NVolClearShowSysFiles(vol);
736 DEBUG("Opening root NTFS dentry");
737 root_ni = ntfs_inode_open(vol, FILE_root);
739 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
741 ret = WIMLIB_ERR_NTFS_3G;
745 /* Currently we assume that all the paths fit into this length and there
746 * is no check for overflow. */
747 char *path = MALLOC(32768);
749 ERROR("Could not allocate memory for NTFS pathname");
750 ret = WIMLIB_ERR_NOMEM;
756 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
757 FILE_NAME_POSIX, vol, params);
760 ntfs_inode_close(root_ni);
762 ntfs_index_ctx_put(vol->secure_xsii);
763 ntfs_index_ctx_put(vol->secure_xsdh);
764 ntfs_inode_close(vol->secure_ni);
767 if (do_ntfs_umount(vol)) {
768 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
772 /* We need to leave the NTFS volume mounted so that we can read
773 * the NTFS files again when we are actually writing the WIM */
774 *(ntfs_volume**)params->extra_arg = vol;
778 #endif /* WITH_NTFS_3G */