+ struct update_primitive_list *l;
+
+ l = &j->cmd_prims[j->cur_cmd];
+
+ if (l->num_entries == l->num_alloc_entries) {
+ struct update_primitive *new_entries;
+ size_t new_num_alloc_entries;
+ size_t new_size;
+
+ new_num_alloc_entries = l->num_alloc_entries * 2;
+ new_size = new_num_alloc_entries * sizeof(new_entries[0]);
+ if (l->entries == l->inline_entries) {
+ new_entries = MALLOC(new_size);
+ if (!new_entries)
+ return WIMLIB_ERR_NOMEM;
+ memcpy(new_entries, l->inline_entries,
+ sizeof(l->inline_entries));
+ } else {
+ new_entries = REALLOC(l->entries, new_size);
+ if (!new_entries)
+ return WIMLIB_ERR_NOMEM;
+ }
+ 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->d_tmp_list, orphans);
+ subject->d_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->d_tmp_list);
+ subject->d_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_len_bytes(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->d_name,
+ &prim->name.subject->d_name_nbytes);
+ break;
+ case CHANGE_SHORT_NAME:
+ rollback_name_change(prim->name.old_name,
+ &prim->name.subject->d_short_name,
+ &prim->name.subject->d_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;