]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
edit-util: introduce overwrite_with_origin switch
authorMike Yuan <me@yhndnzj.com>
Thu, 6 Apr 2023 17:41:24 +0000 (01:41 +0800)
committerMike Yuan <me@yhndnzj.com>
Fri, 7 Apr 2023 08:13:00 +0000 (16:13 +0800)
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
src/shared/edit-util.c
src/shared/edit-util.h
src/systemctl/systemctl-edit.c

index 6a29a32bcd22bf97a069bb04e561e2d1bfbdcdef..7eb22356670df75a8f7cb3b2a5d3503809ccaf60 100644 (file)
@@ -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())
index 097ef6a7919cb03a96e1da83e067fde3a9a9caaa..f482c2ac8b36c97dc205da01990b524430184dea 100644 (file)
@@ -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;
index e57ca80a4921dd198a1ecf1e1401c2d64334b8b6..83b3df86839f018af81e757d7212cc9f1364321c 100644 (file)
@@ -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);
index ff16b73229674a84b0d7043dcb39b357ecd3bdcd..561b01a67a46730cdeaa8d23f42be12ab05da7c6 100644 (file)
@@ -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;