+ /* DOS names are supposed to be 12 characters max (that's 24 bytes,
+ * assuming 2-byte ntfs characters) */
+ wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
+
+ /* Initialize the DOS name, DOS name length, and NTFS inode number of
+ * the red-black tree node */
+ memcpy(new_node->dos_name, dos_name, name_nbytes);
+ new_node->name_nbytes = name_nbytes;
+ new_node->ntfs_ino = ntfs_ino;
+
+ /* Insert the red-black tree node */
+ root = &map->rb_root;
+ p = &root->rb_node;
+ rb_parent = NULL;
+ while (*p) {
+ struct dos_name_node *this;
+
+ this = container_of(*p, struct dos_name_node, rb_node);
+ rb_parent = *p;
+ if (new_node->ntfs_ino < this->ntfs_ino)
+ p = &((*p)->rb_left);
+ else if (new_node->ntfs_ino > this->ntfs_ino)
+ p = &((*p)->rb_right);
+ else {
+ /* This should be impossible since a NTFS inode cannot
+ * have multiple DOS names, and we only should get each
+ * DOS name entry once from the ntfs_readdir() calls. */
+ ERROR("NTFS inode %"PRIu64" has multiple DOS names",
+ ntfs_ino);
+ return -1;
+ }
+ }
+ rb_link_node(&new_node->rb_node, rb_parent, p);
+ rb_insert_color(&new_node->rb_node, root);
+ DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
+ return 0;
+}
+
+/* Returns a structure that contains the DOS name and its length for a NTFS
+ * inode, or NULL if the inode has no DOS name. */
+static struct dos_name_node *
+lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
+{
+ struct rb_node *node = map->rb_root.rb_node;
+ while (node) {
+ struct dos_name_node *this;
+ this = container_of(node, struct dos_name_node, rb_node);
+ if (ntfs_ino < this->ntfs_ino)
+ node = node->rb_left;
+ else if (ntfs_ino > this->ntfs_ino)
+ node = node->rb_right;
+ else
+ return this;
+ }
+ return NULL;
+}
+
+static int
+set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
+{
+ const struct dos_name_map *map = arg;
+ const struct dos_name_node *node;
+
+ if (dentry->is_win32_name) {
+ node = lookup_dos_name(map, dentry->d_inode->i_ino);
+ if (node) {
+ dentry->short_name = MALLOC(node->name_nbytes + 2);
+ if (!dentry->short_name)
+ return WIMLIB_ERR_NOMEM;
+ memcpy(dentry->short_name, node->dos_name,
+ node->name_nbytes);
+ dentry->short_name[node->name_nbytes / 2] = 0;
+ dentry->short_name_nbytes = node->name_nbytes;
+ DEBUG("Assigned DOS name to ino %"PRIu64,
+ dentry->d_inode->i_ino);
+ } else {
+ WARNING("NTFS inode %"PRIu64" has Win32 name with no "
+ "corresponding DOS name",
+ dentry->d_inode->i_ino);
+ }
+ }
+ return 0;
+}
+
+static void
+free_dos_name_tree(struct rb_node *node) {
+ if (node) {
+ free_dos_name_tree(node->rb_left);
+ free_dos_name_tree(node->rb_right);
+ FREE(container_of(node, struct dos_name_node, rb_node));
+ }
+}
+
+static void
+destroy_dos_name_map(struct dos_name_map *map)
+{
+ free_dos_name_tree(map->rb_root.rb_node);
+}
+
+struct readdir_ctx {
+ struct wim_dentry *parent;
+ ntfs_inode *dir_ni;
+ char *path;
+ size_t path_len;
+ struct wim_lookup_table *lookup_table;
+ struct sd_set *sd_set;
+ struct dos_name_map *dos_name_map;
+ const struct capture_config *config;
+ ntfs_volume **ntfs_vol_p;
+ int add_image_flags;
+ wimlib_progress_func_t progress_func;
+};
+
+static int
+build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
+ ntfs_inode *dir_ni,
+ ntfs_inode *ni,
+ char *path,
+ size_t path_len,
+ int name_type,
+ struct wim_lookup_table *lookup_table,
+ struct sd_set *sd_set,
+ const struct capture_config *config,
+ ntfs_volume **ntfs_vol_p,
+ int add_image_flags,
+ wimlib_progress_func_t progress_func);
+
+static int
+wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
+ const int name_nchars, const int name_type,
+ const s64 pos, const MFT_REF mref,
+ const unsigned dt_type)
+{
+ struct readdir_ctx *ctx;
+ size_t mbs_name_nbytes;
+ char *mbs_name;
+ struct wim_dentry *child;
+ int ret;
+ size_t path_len;
+ size_t name_nbytes = name_nchars * sizeof(ntfschar);
+
+ ctx = dirent;
+ if (name_type & FILE_NAME_DOS) {
+ /* If this is the entry for a DOS name, store it for later. */
+ ret = insert_dos_name(ctx->dos_name_map, name,
+ name_nbytes, mref & MFT_REF_MASK_CPU);
+
+ /* Return now if an error occurred or if this is just a DOS name
+ * and not a Win32+DOS name. */
+ if (ret != 0 || name_type == FILE_NAME_DOS)
+ goto out;
+ }
+ ret = utf16le_to_tstr(name, name_nbytes,
+ &mbs_name, &mbs_name_nbytes);
+ if (ret)
+ goto out;
+
+ if (mbs_name[0] == '.' &&
+ (mbs_name[1] == '\0' ||
+ (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
+ /* . or .. entries
+ *
+ * note: name_type is POSIX for these, so DOS names will not
+ * have been inserted for them. */
+ ret = 0;
+ goto out_free_mbs_name;
+ }
+
+ /* Open the inode for this directory entry and recursively capture the
+ * directory tree rooted at it */
+ ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
+ if (!ni) {
+ ERROR_WITH_ERRNO("Failed to open NTFS inode");
+ ret = -1;
+ goto out_free_mbs_name;
+ }
+ path_len = ctx->path_len;
+ if (path_len != 1)
+ ctx->path[path_len++] = '/';
+ memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
+ path_len += mbs_name_nbytes;
+ child = NULL;
+ ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
+ ni, ctx->path, path_len, name_type,
+ ctx->lookup_table, ctx->sd_set,
+ ctx->config, ctx->ntfs_vol_p,
+ ctx->add_image_flags,
+ ctx->progress_func);
+ if (child)
+ dentry_add_child(ctx->parent, child);
+ ntfs_inode_close(ni);
+out_free_mbs_name:
+ FREE(mbs_name);
+out:
+ return ret;
+}
+
+/* Recursively build a WIM dentry tree corresponding to a NTFS volume.
+ * At the same time, update the WIM lookup table with lookup table entries for
+ * the NTFS streams, and build an array of security descriptors.
+ */
+static int
+build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
+ ntfs_inode *dir_ni,
+ ntfs_inode *ni,
+ char *path,
+ size_t path_len,
+ int name_type,
+ struct wim_lookup_table *lookup_table,
+ struct sd_set *sd_set,
+ const struct capture_config *config,
+ ntfs_volume **ntfs_vol_p,
+ int add_image_flags,
+ wimlib_progress_func_t progress_func)
+{
+ u32 attributes;
+ int ret;
+ struct wim_dentry *root;
+
+ if (exclude_path(path, config, false)) {
+ /* Exclude a file or directory tree based on the capture
+ * configuration file */
+ if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
+ && progress_func)
+ {
+ union wimlib_progress_info info;
+ info.scan.cur_path = path;
+ info.scan.excluded = true;
+ progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
+ }
+ *root_p = NULL;
+ return 0;
+ }
+
+ /* Get file attributes */
+ struct SECURITY_CONTEXT ctx;
+ memset(&ctx, 0, sizeof(ctx));
+ ctx.vol = ni->vol;
+ ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
+ ni, dir_ni, (char *)&attributes,
+ sizeof(u32));
+ if (ret != 4) {
+ ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
+ path);
+ return WIMLIB_ERR_NTFS_3G;
+ }
+
+ if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
+ && progress_func)
+ {
+ union wimlib_progress_info info;
+ info.scan.cur_path = path;
+ info.scan.excluded = false;
+ progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
+ }
+
+ /* Create the new WIM dentry */
+ ret = new_dentry_with_timeless_inode(path_basename(path), &root);
+ if (ret)
+ return ret;
+
+ *root_p = root;
+
+ if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name */
+ root->is_win32_name = 1;
+ root->d_inode->i_creation_time = le64_to_cpu(ni->creation_time);
+ root->d_inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
+ root->d_inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
+ root->d_inode->i_attributes = le32_to_cpu(attributes);
+ root->d_inode->i_ino = ni->mft_no;
+ root->d_inode->i_resolved = 1;