+rollback:
+ transaction_abort(m);
+ return r;
+}
+
+static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) {
+ Job *j, *f;
+ int r;
+
+ assert(m);
+ assert(name);
+
+ /* Looks for an axisting prospective job and returns that. If
+ * it doesn't exist it is created and added to the prospective
+ * jobs list. */
+
+ f = hashmap_get(m->transaction_jobs, name);
+
+ for (j = f; j; j = j->transaction_next) {
+ assert(j->name == name);
+
+ if (j->type == type) {
+ if (is_new)
+ *is_new = false;
+ return j;
+ }
+ }
+
+ if (name->meta.job && name->meta.job->type == type)
+ j = name->meta.job;
+ else if (!(j = job_new(m, type, name)))
+ return NULL;
+
+ if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) {
+ job_free(j);
+ return NULL;
+ }
+
+ j->transaction_next = f;
+
+ if (f)
+ f->transaction_prev = j;
+
+ j->generation = 0;
+ j->marker = NULL;
+ j->matters_to_anchor = false;
+
+ if (is_new)
+ *is_new = true;
+
+ return j;
+}
+
+void manager_transaction_delete_job(Manager *m, Job *j) {
+ assert(m);
+ assert(j);
+
+ if (j->transaction_prev)
+ j->transaction_prev->transaction_next = j->transaction_next;
+ else if (j->transaction_next)
+ hashmap_replace(m->transaction_jobs, j->name, j->transaction_next);
+ else
+ hashmap_remove_value(m->transaction_jobs, j->name, j);
+
+ if (j->transaction_next)
+ j->transaction_next->transaction_prev = j->transaction_prev;
+
+ j->transaction_prev = j->transaction_next = NULL;
+
+ while (j->subject_list)
+ job_dependency_free(j->subject_list);
+ while (j->object_list)
+ job_dependency_free(j->object_list);
+}
+
+static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) {
+ Job *ret;
+ void *state;
+ Name *dep;
+ int r;
+ bool is_new;
+
+ assert(m);
+ assert(type < _JOB_TYPE_MAX);
+ assert(name);
+
+ /* First add the job. */
+ if (!(ret = transaction_add_one_job(m, type, name, &is_new)))
+ return -ENOMEM;
+
+ /* Then, add a link to the job. */
+ if (!job_dependency_new(by, ret, matters))
+ return -ENOMEM;
+
+ if (is_new) {
+ /* Finally, recursively add in all dependencies. */
+ if (type == JOB_START || type == JOB_RELOAD_OR_START) {
+ SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0)
+ goto fail;
+ SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0)
+ goto fail;
+ SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0)
+ goto fail;
+ SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0)
+ goto fail;
+ SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0)
+ goto fail;
+ SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0)
+ goto fail;
+
+ } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
+
+ SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
+ if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0)
+ goto fail;
+ }
+
+ /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
+ }