+/*
+ * Links a dentry into the directory tree.
+ *
+ * @parent: The dentry that will be the parent of @dentry.
+ * @dentry: The dentry to link.
+ */
+struct wim_dentry *
+dentry_add_child(struct wim_dentry * restrict parent,
+ struct wim_dentry * restrict child)
+{
+ wimlib_assert(dentry_is_directory(parent));
+
+ struct rb_root *root = &parent->d_inode->i_children;
+ struct rb_node **new = &(root->rb_node);
+ struct rb_node *rb_parent = NULL;
+
+ while (*new) {
+ struct wim_dentry *this = rbnode_dentry(*new);
+ int result = dentry_compare_names(child, this);
+
+ rb_parent = *new;
+
+ if (result < 0)
+ new = &((*new)->rb_left);
+ else if (result > 0)
+ new = &((*new)->rb_right);
+ else
+ return this;
+ }
+ child->parent = parent;
+ rb_link_node(&child->rb_node, rb_parent, new);
+ rb_insert_color(&child->rb_node, root);
+ return NULL;
+}
+
+/* Unlink a WIM dentry from the directory entry tree. */
+void
+unlink_dentry(struct wim_dentry *dentry)
+{
+ struct wim_dentry *parent = dentry->parent;
+ if (parent == dentry)
+ return;
+ rb_erase(&dentry->rb_node, &parent->d_inode->i_children);
+}
+
+/*
+ * Returns the alternate data stream entry belonging to @inode that has the
+ * stream name @stream_name.
+ */
+struct wim_ads_entry *
+inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
+ u16 *idx_ret)
+{
+ if (inode->i_num_ads == 0) {
+ return NULL;
+ } else {
+ size_t stream_name_utf16le_nbytes;
+ u16 i;
+ struct wim_ads_entry *result;
+
+ #if TCHAR_IS_UTF16LE
+ const utf16lechar *stream_name_utf16le;
+
+ stream_name_utf16le = stream_name;
+ stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
+ #else
+ utf16lechar *stream_name_utf16le;
+
+ {
+ int ret = tstr_to_utf16le(stream_name,
+ tstrlen(stream_name) *
+ sizeof(tchar),
+ &stream_name_utf16le,
+ &stream_name_utf16le_nbytes);
+ if (ret)
+ return NULL;
+ }
+ #endif
+ i = 0;
+ result = NULL;
+ do {
+ if (ads_entry_has_name(&inode->i_ads_entries[i],
+ stream_name_utf16le,
+ stream_name_utf16le_nbytes))
+ {
+ if (idx_ret)
+ *idx_ret = i;
+ result = &inode->i_ads_entries[i];
+ break;
+ }
+ } while (++i != inode->i_num_ads);
+ #if !TCHAR_IS_UTF16LE
+ FREE(stream_name_utf16le);
+ #endif
+ return result;
+ }
+}
+
+static struct wim_ads_entry *
+do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
+ size_t stream_name_nbytes, bool is_utf16le)
+{
+ u16 num_ads;
+ struct wim_ads_entry *ads_entries;
+ struct wim_ads_entry *new_entry;
+
+ if (inode->i_num_ads >= 0xfffe) {
+ ERROR("Too many alternate data streams in one inode!");
+ return NULL;
+ }
+ num_ads = inode->i_num_ads + 1;
+ ads_entries = REALLOC(inode->i_ads_entries,
+ num_ads * sizeof(inode->i_ads_entries[0]));
+ if (!ads_entries) {
+ ERROR("Failed to allocate memory for new alternate data stream");
+ return NULL;
+ }
+ inode->i_ads_entries = ads_entries;
+
+ new_entry = &inode->i_ads_entries[num_ads - 1];
+ if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
+ return NULL;
+ new_entry->stream_id = inode->i_next_stream_id++;
+ inode->i_num_ads = num_ads;
+ return new_entry;
+}
+
+struct wim_ads_entry *
+inode_add_ads_utf16le(struct wim_inode *inode,
+ const utf16lechar *stream_name,
+ size_t stream_name_nbytes)
+{
+ DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
+ return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
+}
+
+/*
+ * Add an alternate stream entry to a WIM inode and return a pointer to it, or
+ * NULL if memory could not be allocated.
+ */
+struct wim_ads_entry *
+inode_add_ads(struct wim_inode *inode, const tchar *stream_name)