+ l->entries = new_entries;
+ l->num_alloc_entries = new_num_alloc_entries;
+ }
+ l->entries[l->num_entries++] = prim;
+ return 0;
+}
+
+static void
+do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
+ struct wim_dentry **root_p)
+{
+ if (parent) {
+ /* Unlink @subject from its @parent. */
+ wimlib_assert(subject->d_parent == parent);
+ unlink_dentry(subject);
+ } else {
+ /* Unset @subject as the root of the image. */
+ *root_p = NULL;
+ }
+ subject->d_parent = subject;
+}
+
+static void
+do_link(struct wim_dentry *subject, struct wim_dentry *parent,
+ struct wim_dentry **root_p)
+{
+ if (parent) {
+ /* Link @subject to its @parent */
+ struct wim_dentry *existing;
+
+ existing = dentry_add_child(parent, subject);
+ wimlib_assert(!existing);
+ } else {
+ /* Set @subject as root of the image */
+ *root_p = subject;
+ }
+}
+
+/* Undo a link operation. */
+static void
+rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
+ struct wim_dentry **root_p, struct list_head *orphans)
+{
+ /* Unlink is the opposite of link */
+ do_unlink(subject, parent, root_p);
+
+ /* @subject is now unlinked. Add it to orphans. */
+ list_add(&subject->tmp_list, orphans);
+ subject->is_orphan = 1;
+}
+
+/* Undo an unlink operation. */
+static void
+rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
+ struct wim_dentry **root_p)
+{
+ /* Link is the opposite of unlink */
+ do_link(subject, parent, root_p);
+
+ /* @subject is no longer unlinked. Delete it from orphans. */
+ list_del(&subject->tmp_list);
+ subject->is_orphan = 0;
+}
+
+/* Rollback a name change operation. */
+static void
+rollback_name_change(utf16lechar *old_name,
+ utf16lechar **name_ptr, u16 *name_nbytes_ptr)
+{
+ /* Free the new name, then replace it with the old name. */
+ FREE(*name_ptr);
+ if (old_name) {
+ *name_ptr = old_name;
+ *name_nbytes_ptr = utf16le_strlen(old_name);
+ } else {
+ *name_ptr = NULL;
+ *name_nbytes_ptr = 0;
+ }
+}
+
+/* Rollback a primitive update operation. */
+static void
+rollback_update_primitive(const struct update_primitive *prim,
+ struct wim_dentry **root_p,
+ struct list_head *orphans)
+{
+ switch (prim->type) {
+ case LINK_DENTRY:
+ rollback_link(prim->link.subject, prim->link.parent, root_p,
+ orphans);
+ break;
+ case UNLINK_DENTRY:
+ rollback_unlink(prim->link.subject, prim->link.parent, root_p);
+ break;
+ case CHANGE_FILE_NAME:
+ rollback_name_change(prim->name.old_name,
+ &prim->name.subject->file_name,
+ &prim->name.subject->file_name_nbytes);
+ break;
+ case CHANGE_SHORT_NAME:
+ rollback_name_change(prim->name.old_name,
+ &prim->name.subject->short_name,
+ &prim->name.subject->short_name_nbytes);
+ break;
+ }
+}
+
+/* Rollback a logical update command */
+static void
+rollback_update_command(const struct update_primitive_list *l,
+ struct wim_dentry **root_p,
+ struct list_head *orphans)
+{
+ size_t i = l->num_entries;
+
+ /* Rollback each primitive operation, in reverse order. */
+ while (i--)
+ rollback_update_primitive(&l->entries[i], root_p, orphans);
+}
+
+/****************************************************************************/
+
+/* Link @subject into the directory @parent; or, if @parent is NULL, set
+ * @subject as the root of the WIM image.
+ *
+ * This is the journaled version, so it can be rolled back. */
+static int
+journaled_link(struct update_command_journal *j,
+ struct wim_dentry *subject, struct wim_dentry *parent)
+{
+ struct update_primitive prim;
+ int ret;
+
+ prim.type = LINK_DENTRY;
+ prim.link.subject = subject;
+ prim.link.parent = parent;
+
+ ret = record_update_primitive(j, prim);
+ if (ret)
+ return ret;
+
+ do_link(subject, parent, j->root_p);
+
+ if (subject->is_orphan) {
+ list_del(&subject->tmp_list);
+ subject->is_orphan = 0;