+ /* Deletes one job from the transaction */
+
+ manager_transaction_unlink_job(m, j);
+
+ if (!j->linked)
+ job_free(j);
+}
+
+static void transaction_delete_name(Manager *m, Name *n) {
+ Job *j;
+
+ /* Deletes all jobs associated with a certain name from the
+ * transaction */
+
+ while ((j = hashmap_get(m->transaction_jobs, n)))
+ transaction_delete_job(m, j);
+}
+
+static void transaction_abort(Manager *m) {
+ Job *j;
+
+ assert(m);
+
+ while ((j = hashmap_first(m->transaction_jobs)))
+ if (j->linked)
+ transaction_delete_job(m, j);
+ else
+ job_free(j);
+
+ assert(hashmap_isempty(m->transaction_jobs));
+ assert(!m->transaction_anchor);
+}
+
+static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
+ JobDependency *l;
+
+ assert(m);
+
+ /* A recursive sweep through the graph that marks all names
+ * that matter to the anchor job, i.e. are directly or
+ * indirectly a dependency of the anchor job via paths that
+ * are fully marked as mattering. */
+
+ for (l = j ? j->subject_list : m->transaction_anchor; l; l = l->subject_next) {
+
+ /* This link does not matter */
+ if (!l->matters)
+ continue;
+
+ /* This name has already been marked */
+ if (l->object->generation == generation)
+ continue;
+
+ l->object->matters_to_anchor = true;
+ l->object->generation = generation;
+
+ transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
+ }
+}
+
+static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
+ JobDependency *l, *last;
+
+ assert(j);
+ assert(other);
+ assert(j->name == other->name);
+ assert(!j->linked);
+
+ /* Merges 'other' into 'j' and then deletes j. */
+
+ j->type = t;
+ j->state = JOB_WAITING;
+ j->forced = j->forced || other->forced;
+
+ j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
+
+ /* Patch us in as new owner of the JobDependency objects */
+ last = NULL;
+ for (l = other->subject_list; l; l = l->subject_next) {
+ assert(l->subject == other);
+ l->subject = j;
+ last = l;
+ }
+
+ /* Merge both lists */
+ if (last) {
+ last->subject_next = j->subject_list;
+ if (j->subject_list)
+ j->subject_list->subject_prev = last;
+ j->subject_list = other->subject_list;
+ }
+
+ /* Patch us in as new owner of the JobDependency objects */
+ last = NULL;
+ for (l = other->object_list; l; l = l->object_next) {
+ assert(l->object == other);
+ l->object = j;
+ last = l;
+ }
+
+ /* Merge both lists */
+ if (last) {
+ last->object_next = j->object_list;
+ if (j->object_list)
+ j->object_list->object_prev = last;
+ j->object_list = other->object_list;
+ }
+
+ /* Kill the other job */
+ other->subject_list = NULL;
+ other->object_list = NULL;
+ transaction_delete_job(m, other);
+}
+
+static int delete_one_unmergeable_job(Manager *m, Job *j) {
+ Job *k;
+
+ assert(j);
+
+ /* Tries to delete one item in the linked list
+ * j->transaction_next->transaction_next->... that conflicts
+ * whith another one, in an attempt to make an inconsistent
+ * transaction work. */
+
+ /* We rely here on the fact that if a merged with b does not
+ * merge with c, either a or b merge with c neither */
+ for (; j; j = j->transaction_next)
+ for (k = j->transaction_next; k; k = k->transaction_next) {
+ Job *d;
+
+ /* Is this one mergeable? Then skip it */
+ if (job_type_is_mergeable(j->type, k->type))
+ continue;
+
+ /* Ok, we found two that conflict, let's see if we can
+ * drop one of them */
+ if (!j->matters_to_anchor)
+ d = j;
+ else if (!k->matters_to_anchor)
+ d = k;
+ else
+ return -ENOEXEC;
+
+ /* Ok, we can drop one, so let's do so. */
+ log_debug("Try to fix job merging by deleting job %s/%s", name_id(d->name), job_type_to_string(d->type));
+ transaction_delete_job(m, d);
+ return 0;
+ }
+
+ return -EINVAL;