4 * Capture a WIM image from a NTFS volume. We capture everything we can,
5 * 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/.
30 #include <ntfs-3g/endians.h>
31 #include <ntfs-3g/types.h>
33 #include "buffer_io.h"
35 #include "lookup_table.h"
37 #include "wimlib_internal.h"
39 #include <ntfs-3g/layout.h>
40 #include <ntfs-3g/acls.h>
41 #include <ntfs-3g/attrib.h>
42 #include <ntfs-3g/misc.h>
43 #include <ntfs-3g/reparse.h>
44 #include <ntfs-3g/security.h> /* ntfs-3g/security.h before ntfs-3g/xattrs.h */
45 #include <ntfs-3g/xattrs.h>
46 #include <ntfs-3g/volume.h>
55 static inline ntfschar *attr_record_name(ATTR_RECORD *ar)
57 return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
60 /* Calculates the SHA1 message digest of a NTFS attribute.
62 * @ni: The NTFS inode containing the attribute.
63 * @ar: The ATTR_RECORD describing the attribute.
64 * @md: If successful, the returned SHA1 message digest.
65 * @reparse_tag_ret: Optional pointer into which the first 4 bytes of the
66 * attribute will be written (to get the reparse
69 * Return 0 on success or nonzero on error.
71 static int ntfs_attr_sha1sum(ntfs_inode *ni, ATTR_RECORD *ar,
72 u8 md[SHA1_HASH_SIZE],
73 bool is_reparse_point,
78 char buf[BUFFER_SIZE];
82 na = ntfs_attr_open(ni, ar->type, attr_record_name(ar),
85 ERROR_WITH_ERRNO("Failed to open NTFS attribute");
86 return WIMLIB_ERR_NTFS_3G;
89 bytes_remaining = na->data_size;
91 if (is_reparse_point) {
92 if (ntfs_attr_pread(na, 0, 8, buf) != 8)
94 *reparse_tag_ret = le32_to_cpu(*(u32*)buf);
95 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
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)
105 sha1_update(&ctx, buf, to_read);
107 bytes_remaining -= to_read;
109 sha1_final(md, &ctx);
113 ERROR_WITH_ERRNO("Error reading NTFS attribute");
114 return WIMLIB_ERR_NTFS_3G;
117 /* Load the streams from a file or reparse point in the NTFS volume into the WIM
119 static int capture_ntfs_streams(struct wim_dentry *dentry, ntfs_inode *ni,
120 char path[], size_t path_len,
121 struct wim_lookup_table *lookup_table,
122 ntfs_volume **ntfs_vol_p,
125 ntfs_attr_search_ctx *actx;
126 u8 attr_hash[SHA1_HASH_SIZE];
127 struct ntfs_location *ntfs_loc = NULL;
129 struct wim_lookup_table_entry *lte;
131 DEBUG2("Capturing NTFS data streams from `%s'", path);
133 /* Get context to search the streams of the NTFS file. */
134 actx = ntfs_attr_get_search_ctx(ni, NULL);
136 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
138 return WIMLIB_ERR_NTFS_3G;
141 /* Capture each data stream or reparse data stream. */
142 while (!ntfs_attr_lookup(type, NULL, 0,
143 CASE_SENSITIVE, 0, NULL, 0, actx))
145 char *stream_name_utf8;
147 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
148 u64 name_length = actx->attr->name_length;
150 if (data_size == 0) {
152 ERROR_WITH_ERRNO("Failed to get size of attribute of "
154 ret = WIMLIB_ERR_NTFS_3G;
157 /* Empty stream. No lookup table entry is needed. */
160 if (type == AT_REPARSE_POINT && data_size < 8) {
161 ERROR("`%s': reparse point buffer too small",
163 ret = WIMLIB_ERR_NTFS_3G;
166 /* Checksum the stream. */
167 ret = ntfs_attr_sha1sum(ni, actx->attr, attr_hash,
168 type == AT_REPARSE_POINT, &reparse_tag);
172 if (type == AT_REPARSE_POINT)
173 dentry->d_inode->i_reparse_tag = reparse_tag;
175 /* Make a lookup table entry for the stream, or use an existing
176 * one if there's already an identical stream. */
177 lte = __lookup_resource(lookup_table, attr_hash);
178 ret = WIMLIB_ERR_NOMEM;
182 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
185 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
186 ntfs_loc->path_utf8 = MALLOC(path_len + 1);
187 if (!ntfs_loc->path_utf8)
188 goto out_free_ntfs_loc;
189 memcpy(ntfs_loc->path_utf8, path, path_len + 1);
191 ntfs_loc->stream_name_utf16 = MALLOC(name_length * 2);
192 if (!ntfs_loc->stream_name_utf16)
193 goto out_free_ntfs_loc;
194 memcpy(ntfs_loc->stream_name_utf16,
195 attr_record_name(actx->attr),
196 actx->attr->name_length * 2);
197 ntfs_loc->stream_name_utf16_num_chars = name_length;
200 lte = new_lookup_table_entry();
202 goto out_free_ntfs_loc;
203 lte->ntfs_loc = ntfs_loc;
204 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
205 if (type == AT_REPARSE_POINT) {
206 ntfs_loc->is_reparse_point = true;
207 lte->resource_entry.original_size = data_size - 8;
208 lte->resource_entry.size = data_size - 8;
210 ntfs_loc->is_reparse_point = false;
211 lte->resource_entry.original_size = data_size;
212 lte->resource_entry.size = data_size;
215 DEBUG("Add resource for `%s' (size = %"PRIu64")",
216 dentry->file_name_utf8,
217 lte->resource_entry.original_size);
218 copy_hash(lte->hash, attr_hash);
219 lookup_table_insert(lookup_table, lte);
222 if (name_length == 0) {
223 /* Unnamed data stream. Put the reference to it in the
226 if (dentry->d_inode->i_lte) {
227 ERROR("Found two un-named data streams for "
229 ret = WIMLIB_ERR_NTFS_3G;
232 dentry->d_inode->i_lte = lte;
234 if (dentry->d_inode->i_lte) {
235 WARNING("Found two un-named data streams for "
237 free_lookup_table_entry(lte);
239 dentry->d_inode->i_lte = lte;
243 /* Named data stream. Put the reference to it in the
244 * alternate data stream entries */
245 struct wim_ads_entry *new_ads_entry;
246 size_t stream_name_utf8_len;
248 ret = utf16_to_utf8((const char*)attr_record_name(actx->attr),
251 &stream_name_utf8_len);
254 new_ads_entry = inode_add_ads(dentry->d_inode, stream_name_utf8);
255 FREE(stream_name_utf8);
259 wimlib_assert(new_ads_entry->stream_name_len == name_length * 2);
261 new_ads_entry->lte = lte;
267 free_lookup_table_entry(lte);
270 FREE(ntfs_loc->path_utf8);
271 FREE(ntfs_loc->stream_name_utf16);
275 ntfs_attr_put_search_ctx(actx);
277 DEBUG2("Successfully captured NTFS streams from `%s'", path);
279 ERROR("Failed to capture NTFS streams from `%s", path);
283 /* Red-black tree that maps NTFS inode numbers to DOS names */
284 struct dos_name_map {
285 struct rb_root rb_root;
288 struct dos_name_node {
289 struct rb_node rb_node;
295 /* Inserts a new DOS name into the map */
296 static int insert_dos_name(struct dos_name_map *map,
297 const ntfschar *dos_name, int name_len,
300 struct dos_name_node *new_node;
302 struct rb_root *root;
303 struct rb_node *rb_parent;
305 DEBUG("DOS name_len = %d", name_len);
306 new_node = MALLOC(sizeof(struct dos_name_node));
310 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
311 * assuming 2-byte ntfs characters) */
312 wimlib_assert(name_len * sizeof(ntfschar) <= sizeof(new_node->dos_name));
314 /* Initialize the DOS name, DOS name length, and NTFS inode number of
315 * the red-black tree node */
316 memcpy(new_node->dos_name, dos_name, name_len * sizeof(ntfschar));
317 new_node->name_len_bytes = name_len * sizeof(ntfschar);
318 new_node->ntfs_ino = ntfs_ino;
320 /* Insert the red-black tree node */
321 root = &map->rb_root;
325 struct dos_name_node *this;
327 this = container_of(*p, struct dos_name_node, rb_node);
329 if (new_node->ntfs_ino < this->ntfs_ino)
330 p = &((*p)->rb_left);
331 else if (new_node->ntfs_ino > this->ntfs_ino)
332 p = &((*p)->rb_right);
334 /* This should be impossible since a NTFS inode cannot
335 * have multiple DOS names, and we only should get each
336 * DOS name entry once from the ntfs_readdir() calls. */
337 ERROR("NTFS inode %"PRIu64" has multiple DOS names",
342 rb_link_node(&new_node->rb_node, rb_parent, p);
343 rb_insert_color(&new_node->rb_node, root);
344 DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
348 /* Returns a structure that contains the DOS name and its length for a NTFS
349 * inode, or NULL if the inode has no DOS name. */
350 static struct dos_name_node *
351 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
353 struct rb_node *node = map->rb_root.rb_node;
355 struct dos_name_node *this;
356 this = container_of(node, struct dos_name_node, rb_node);
357 if (ntfs_ino < this->ntfs_ino)
358 node = node->rb_left;
359 else if (ntfs_ino > this->ntfs_ino)
360 node = node->rb_right;
367 static int set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
369 const struct dos_name_map *map = arg;
370 const struct dos_name_node *node;
372 if (dentry->is_win32_name) {
373 node = lookup_dos_name(map, dentry->d_inode->i_ino);
375 dentry->short_name = MALLOC(node->name_len_bytes);
376 if (!dentry->short_name)
377 return WIMLIB_ERR_NOMEM;
378 memcpy(dentry->short_name, node->dos_name,
379 node->name_len_bytes);
380 dentry->short_name_len = node->name_len_bytes;
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);
392 static void free_dos_name_tree(struct rb_node *node) {
394 free_dos_name_tree(node->rb_left);
395 free_dos_name_tree(node->rb_right);
396 FREE(container_of(node, struct dos_name_node, rb_node));
400 static void destroy_dos_name_map(struct dos_name_map *map)
402 free_dos_name_tree(map->rb_root.rb_node);
406 struct wim_dentry *parent;
410 struct wim_lookup_table *lookup_table;
411 struct sd_set *sd_set;
412 struct dos_name_map *dos_name_map;
413 const struct capture_config *config;
414 ntfs_volume **ntfs_vol_p;
416 wimlib_progress_func_t progress_func;
420 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p, ntfs_inode *dir_ni,
421 ntfs_inode *ni, char path[], size_t path_len,
423 struct wim_lookup_table *lookup_table,
424 struct sd_set *sd_set,
425 const struct capture_config *config,
426 ntfs_volume **ntfs_vol_p,
428 wimlib_progress_func_t progress_func);
430 static int wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
431 const int name_len, const int name_type,
432 const s64 pos, const MFT_REF mref,
433 const unsigned dt_type)
435 struct readdir_ctx *ctx;
436 size_t utf8_name_len;
438 struct wim_dentry *child;
443 if (name_type & FILE_NAME_DOS) {
444 /* If this is the entry for a DOS name, store it for later. */
445 ret = insert_dos_name(ctx->dos_name_map, name,
446 name_len, mref & MFT_REF_MASK_CPU);
448 /* Return now if an error occurred or if this is just a DOS name
449 * and not a Win32+DOS name. */
450 if (ret != 0 || name_type == FILE_NAME_DOS)
453 ret = utf16_to_utf8((const char*)name, name_len * 2,
454 &utf8_name, &utf8_name_len);
458 if (utf8_name[0] == '.' &&
459 (utf8_name[1] == '\0' ||
460 (utf8_name[1] == '.' && utf8_name[2] == '\0'))) {
463 * note: name_type is POSIX for these, so DOS names will not
464 * have been inserted for them. */
466 goto out_free_utf8_name;
469 /* Open the inode for this directory entry and recursively capture the
470 * directory tree rooted at it */
471 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
473 ERROR_WITH_ERRNO("Failed to open NTFS inode");
474 goto out_free_utf8_name;
476 path_len = ctx->path_len;
478 ctx->path[path_len++] = '/';
479 memcpy(ctx->path + path_len, utf8_name, utf8_name_len + 1);
480 path_len += utf8_name_len;
482 ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
483 ni, ctx->path, path_len, name_type,
484 ctx->lookup_table, ctx->sd_set,
485 ctx->config, ctx->ntfs_vol_p,
486 ctx->add_image_flags,
489 dentry_add_child(ctx->parent, child);
490 ntfs_inode_close(ni);
496 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
497 * At the same time, update the WIM lookup table with lookup table entries for
498 * the NTFS streams, and build an array of security descriptors.
500 static int build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
506 struct wim_lookup_table *lookup_table,
507 struct sd_set *sd_set,
508 const struct capture_config *config,
509 ntfs_volume **ntfs_vol_p,
511 wimlib_progress_func_t progress_func)
515 struct wim_dentry *root;
517 if (exclude_path(path, config, false)) {
518 /* Exclude a file or directory tree based on the capture
519 * configuration file */
520 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
523 union wimlib_progress_info info;
524 info.scan.cur_path = path;
525 info.scan.excluded = true;
526 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
532 /* Get file attributes */
533 struct SECURITY_CONTEXT ctx;
534 memset(&ctx, 0, sizeof(ctx));
536 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
537 ni, dir_ni, (char *)&attributes,
540 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
542 return WIMLIB_ERR_NTFS_3G;
545 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
548 union wimlib_progress_info info;
549 info.scan.cur_path = path;
550 info.scan.excluded = false;
551 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
554 /* Create the new WIM dentry */
555 root = new_dentry_with_timeless_inode(path_basename(path));
558 return WIMLIB_ERR_INVALID_UTF8_STRING;
559 else if (errno == ENOMEM)
560 return WIMLIB_ERR_NOMEM;
562 return WIMLIB_ERR_ICONV_NOT_AVAILABLE;
566 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name */
567 root->is_win32_name = 1;
568 root->d_inode->i_creation_time = le64_to_cpu(ni->creation_time);
569 root->d_inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
570 root->d_inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
571 root->d_inode->i_attributes = le32_to_cpu(attributes);
572 root->d_inode->i_ino = ni->mft_no;
573 root->d_inode->i_resolved = 1;
575 if (attributes & FILE_ATTR_REPARSE_POINT) {
576 /* Junction point, symbolic link, or other reparse point */
577 ret = capture_ntfs_streams(root, ni, path, path_len,
578 lookup_table, ntfs_vol_p,
580 } else if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
582 /* Normal directory */
584 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
585 struct readdir_ctx ctx = {
589 .path_len = path_len,
590 .lookup_table = lookup_table,
592 .dos_name_map = &dos_name_map,
594 .ntfs_vol_p = ntfs_vol_p,
595 .add_image_flags = add_image_flags,
596 .progress_func = progress_func,
598 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
600 ERROR_WITH_ERRNO("ntfs_readdir()");
601 ret = WIMLIB_ERR_NTFS_3G;
603 ret = for_dentry_child(root, set_dentry_dos_name,
606 destroy_dos_name_map(&dos_name_map);
609 ret = capture_ntfs_streams(root, ni, path, path_len,
610 lookup_table, ntfs_vol_p,
616 /* Get security descriptor */
620 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
623 if (ret > sizeof(sd)) {
625 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
626 ni, dir_ni, sd, ret);
629 root->d_inode->i_security_id = sd_set_add_sd(sd_set, sd, ret);
630 if (root->d_inode->i_security_id == -1) {
631 ERROR("Out of memory");
632 return WIMLIB_ERR_NOMEM;
634 DEBUG("Added security ID = %u for `%s'",
635 root->d_inode->i_security_id, path);
637 } else if (ret < 0) {
638 ERROR_WITH_ERRNO("Failed to get security information from "
640 ret = WIMLIB_ERR_NTFS_3G;
642 root->d_inode->i_security_id = -1;
643 DEBUG("No security ID for `%s'", path);
648 int build_dentry_tree_ntfs(struct wim_dentry **root_p,
650 struct wim_lookup_table *lookup_table,
651 struct wim_security_data *sd,
652 const struct capture_config *config,
654 wimlib_progress_func_t progress_func,
660 struct sd_set sd_set = {
664 ntfs_volume **ntfs_vol_p = extra_arg;
666 DEBUG("Mounting NTFS volume `%s' read-only", device);
668 #ifdef HAVE_NTFS_MNT_RDONLY
670 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
672 /* NTFS-3g 2011, 2012 */
673 vol = ntfs_mount(device, MS_RDONLY);
676 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
678 return WIMLIB_ERR_NTFS_3G;
680 ntfs_open_secure(vol);
682 /* We don't want to capture the special NTFS files such as $Bitmap. Not
683 * to be confused with "hidden" or "system" files which are real files
684 * that we do need to capture. */
685 NVolClearShowSysFiles(vol);
687 DEBUG("Opening root NTFS dentry");
688 root_ni = ntfs_inode_open(vol, FILE_root);
690 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
692 ret = WIMLIB_ERR_NTFS_3G;
696 /* Currently we assume that all the UTF-8 paths fit into this length and
697 * there is no check for overflow. */
698 char *path = MALLOC(32768);
700 ERROR("Could not allocate memory for NTFS pathname");
701 ret = WIMLIB_ERR_NOMEM;
707 ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
708 FILE_NAME_POSIX, lookup_table,
715 ntfs_inode_close(root_ni);
716 destroy_sd_set(&sd_set);
718 ntfs_index_ctx_put(vol->secure_xsii);
719 ntfs_index_ctx_put(vol->secure_xsdh);
720 ntfs_inode_close(vol->secure_ni);
723 if (ntfs_umount(vol, FALSE) != 0) {
724 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
727 ret = WIMLIB_ERR_NTFS_3G;
730 /* We need to leave the NTFS volume mounted so that we can read
731 * the NTFS files again when we are actually writing the WIM */