]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: edit: write override files as text files
authorнаб <nabijaczleweli@nabijaczleweli.xyz>
Tue, 8 Nov 2022 20:30:01 +0000 (21:30 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Tue, 22 Nov 2022 00:45:50 +0000 (01:45 +0100)
Instead of stripping the newline off the final would-be line;
continue to reduce an empty-line-only file to an empty file, though

Closes #25303

src/systemctl/systemctl-edit.c

index fe47f73d4a7231ccfbcafb8c74a2db318b2a0c5d..e8e6eeda29098d33da20920cced824630f10deb7 100644 (file)
@@ -483,6 +483,18 @@ static int trim_edit_markers(const char *path) {
                 strshorten(contents_start, contents_end - contents_start);
 
         contents_start = strstrip(contents_start);
+        if (*contents_start && !endswith(contents_start, "\n")) {
+                char *tmp = contents_start;
+                if (MALLOC_SIZEOF_SAFE(contents) - (contents_start - contents) - strlen(contents_start) < 2) {
+                        if ((tmp = realloc(contents, size + 1))) {
+                                contents_start = tmp + (contents_start - contents);
+                                contents = tmp;
+                        }
+                }
+
+                if (tmp)
+                        strcat(contents_start, "\n");
+        }
 
         /* Write new contents if the trimming actually changed anything */
         if (strlen(contents) != size) {