mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-02-09 09:57:26 +03:00
systemctl: simplify trim_edit_markers()
This is not performance sensitive, don#t try to be smart with realloc() Follow-up for: #25305 Fixes: #25303
This commit is contained in:
parent
0b92fbbaee
commit
8eda5560b0
@ -459,51 +459,42 @@ static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int trim_edit_markers(const char *path) {
|
static int trim_edit_markers(const char *path) {
|
||||||
_cleanup_free_ char *contents = NULL;
|
_cleanup_free_ char *old_contents = NULL, *new_contents = NULL;
|
||||||
char *contents_start = NULL;
|
char *contents_start, *contents_end;
|
||||||
const char *contents_end = NULL;
|
const char *c = NULL;
|
||||||
size_t size;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
/* Trim out the lines between the two markers */
|
/* Trim out the lines between the two markers */
|
||||||
r = read_full_file(path, &contents, NULL);
|
r = read_full_file(path, &old_contents, NULL);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_error_errno(r, "Failed to read temporary file \"%s\": %m", path);
|
return log_error_errno(r, "Failed to read temporary file \"%s\": %m", path);
|
||||||
|
|
||||||
size = strlen(contents);
|
contents_start = strstr(old_contents, EDIT_MARKER_START);
|
||||||
|
|
||||||
contents_start = strstr(contents, EDIT_MARKER_START);
|
|
||||||
if (contents_start)
|
if (contents_start)
|
||||||
contents_start += strlen(EDIT_MARKER_START);
|
contents_start += strlen(EDIT_MARKER_START);
|
||||||
else
|
else
|
||||||
contents_start = contents;
|
contents_start = old_contents;
|
||||||
|
|
||||||
contents_end = strstr(contents_start, EDIT_MARKER_END);
|
contents_end = strstr(contents_start, EDIT_MARKER_END);
|
||||||
if (contents_end)
|
if (contents_end)
|
||||||
strshorten(contents_start, contents_end - contents_start);
|
contents_end[0] = 0;
|
||||||
|
|
||||||
contents_start = strstrip(contents_start);
|
c = strstrip(contents_start);
|
||||||
if (*contents_start && !endswith(contents_start, "\n")) {
|
if (isempty(c))
|
||||||
char *tmp = contents_start;
|
return 0; /* All gone now */
|
||||||
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)
|
new_contents = strjoin(c, "\n"); /* Trim prefix and suffix, but ensure suffixed by single newline */
|
||||||
strcat(contents_start, "\n");
|
if (!new_contents)
|
||||||
}
|
return log_oom();
|
||||||
|
|
||||||
/* Write new contents if the trimming actually changed anything */
|
if (streq(old_contents, new_contents)) /* Don't touch the file if the above didn't change a thing */
|
||||||
if (strlen(contents) != size) {
|
return 1; /* Unchanged, but good */
|
||||||
r = write_string_file(path, contents_start, WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_TRUNCATE | WRITE_STRING_FILE_AVOID_NEWLINE);
|
|
||||||
if (r < 0)
|
|
||||||
return log_error_errno(r, "Failed to modify temporary file \"%s\": %m", path);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
r = write_string_file(path, new_contents, WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_TRUNCATE | WRITE_STRING_FILE_AVOID_NEWLINE);
|
||||||
|
if (r < 0)
|
||||||
|
return log_error_errno(r, "Failed to modify temporary file \"%s\": %m", path);
|
||||||
|
|
||||||
|
return 1; /* Changed, but good */
|
||||||
}
|
}
|
||||||
|
|
||||||
int verb_edit(int argc, char *argv[], void *userdata) {
|
int verb_edit(int argc, char *argv[], void *userdata) {
|
||||||
@ -561,16 +552,13 @@ int verb_edit(int argc, char *argv[], void *userdata) {
|
|||||||
* modification. */
|
* modification. */
|
||||||
r = trim_edit_markers(*tmp);
|
r = trim_edit_markers(*tmp);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
|
goto end;
|
||||||
|
if (r == 0) /* has no actual contents? then ignore it */
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (null_or_empty_path(*tmp)) {
|
r = RET_NERRNO(rename(*tmp, *original));
|
||||||
log_warning("Editing \"%s\" canceled: temporary file is empty.", *original);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = rename(*tmp, *original);
|
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original);
|
log_error_errno(r, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user