-static bool unit_is_editable(const char *unit_name, const char *fragment_path, const char *user_home) {
- bool editable = true;
- const char *invalid_path;
-
- assert(unit_name);
-
- if (!arg_runtime)
- return true;
-
- switch (arg_scope) {
- case UNIT_FILE_SYSTEM:
- if (path_startswith(fragment_path, "/etc/systemd/system")) {
- editable = false;
- invalid_path = "/etc/systemd/system";
- } else if (path_startswith(fragment_path, SYSTEM_CONFIG_UNIT_PATH)) {
- editable = false;
- invalid_path = SYSTEM_CONFIG_UNIT_PATH;
- }
- break;
- case UNIT_FILE_GLOBAL:
- if (path_startswith(fragment_path, "/etc/systemd/user")) {
- editable = false;
- invalid_path = "/etc/systemd/user";
- } else if (path_startswith(fragment_path, USER_CONFIG_UNIT_PATH)) {
- editable = false;
- invalid_path = USER_CONFIG_UNIT_PATH;
- }
- break;
- case UNIT_FILE_USER:
- assert(user_home);
-
- if (path_startswith(fragment_path, "/etc/systemd/user")) {
- editable = false;
- invalid_path = "/etc/systemd/user";
- } else if (path_startswith(fragment_path, USER_CONFIG_UNIT_PATH)) {
- editable = false;
- invalid_path = USER_CONFIG_UNIT_PATH;
- } else if (path_startswith(fragment_path, user_home)) {
- editable = false;
- invalid_path = user_home;
- }
- break;
- default:
- assert_not_reached("Invalid scope");
- }
-
- if (!editable)
- log_error("%s ignored: cannot temporarily edit units from %s", unit_name, invalid_path);
-
- return editable;
-}
-
-static int get_copy_to_edit(const char *unit_name, const char *fragment_path, const char *user_home, const char *user_runtime, char **ret_path) {
- char *tmp_new_path;
-
- assert(unit_name);
- assert(ret_path);
-
- if (!unit_is_editable(unit_name, fragment_path, user_home))
- return -EINVAL;
-
- switch (arg_scope) {
- case UNIT_FILE_SYSTEM:
- tmp_new_path = path_join(arg_root, arg_runtime ? "/run/systemd/system/" : SYSTEM_CONFIG_UNIT_PATH, unit_name);
- break;
- case UNIT_FILE_GLOBAL:
- tmp_new_path = path_join(arg_root, arg_runtime ? "/run/systemd/user/" : USER_CONFIG_UNIT_PATH, unit_name);
- break;
- case UNIT_FILE_USER:
- assert(user_home);
- assert(user_runtime);
-
- tmp_new_path = path_join(arg_root, arg_runtime ? user_runtime : user_home, unit_name);
- break;
- default:
- assert_not_reached("Invalid scope");
- }
- if (!tmp_new_path)
- return log_oom();
-
- *ret_path = tmp_new_path;
-
- return 0;
-}
-