From bc6c7a58c25f9e974996d43473fbfb1273d746c0 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 7 Apr 2023 01:41:24 +0800 Subject: [PATCH] edit-util: introduce overwrite_with_origin switch Before this commit, if `original_path` is given, it will always be used to overwrite `path`. After this commit, it's controlled by the newly-added switch `overwrite_with_origin`. --- src/machine/machinectl.c | 4 +--- src/shared/edit-util.c | 4 +++- src/shared/edit-util.h | 1 + src/systemctl/systemctl-edit.c | 1 + 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 6a29a32bcd2..7eb22356670 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -1454,9 +1454,7 @@ static int get_settings_path(const char *name, char **ret_path) { } static int edit_settings(int argc, char *argv[], void *userdata) { - _cleanup_(edit_file_context_done) EditFileContext context = { - .remove_parent = false, - }; + _cleanup_(edit_file_context_done) EditFileContext context = {}; int r; if (!on_tty()) diff --git a/src/shared/edit-util.c b/src/shared/edit-util.c index 097ef6a7919..f482c2ac8b3 100644 --- a/src/shared/edit-util.c +++ b/src/shared/edit-util.c @@ -129,9 +129,11 @@ static int create_edit_temp_file(EditFile *e) { has_original = e->original_path && access(e->original_path, F_OK) >= 0; has_target = access(e->path, F_OK) >= 0; - if (has_original) + if (has_original && (!has_target || e->context->overwrite_with_origin)) + /* We are asked to overwrite target with original_path or target doesn't exist. */ source = e->original_path; else if (has_target) + /* Target exists and shouldn't be overwritten. */ source = e->path; else source = NULL; diff --git a/src/shared/edit-util.h b/src/shared/edit-util.h index e57ca80a492..83b3df86839 100644 --- a/src/shared/edit-util.h +++ b/src/shared/edit-util.h @@ -24,6 +24,7 @@ struct EditFileContext { const char *marker_start; const char *marker_end; bool remove_parent; + bool overwrite_with_origin; /* whether to always overwrite target with original file */ }; void edit_file_context_done(EditFileContext *context); diff --git a/src/systemctl/systemctl-edit.c b/src/systemctl/systemctl-edit.c index ff16b732296..561b01a67a4 100644 --- a/src/systemctl/systemctl-edit.c +++ b/src/systemctl/systemctl-edit.c @@ -316,6 +316,7 @@ int verb_edit(int argc, char *argv[], void *userdata) { .marker_start = DROPIN_MARKER_START, .marker_end = DROPIN_MARKER_END, .remove_parent = !arg_full, + .overwrite_with_origin = true, }; _cleanup_(lookup_paths_free) LookupPaths lp = {}; _cleanup_strv_free_ char **names = NULL; -- 2.47.3