tweaks: elide a duplicate opening of the existing file when prepending

master
Benno Schulenberg 2019-10-16 17:40:52 +02:00
parent e3807f00a2
commit f5693d4151
1 changed files with 11 additions and 17 deletions

View File

@ -1724,9 +1724,17 @@ bool write_file(const char *name, FILE *stream, bool tmp,
int fd_src;
FILE *source = NULL, *target = NULL;
if (fopen(realname, "rb") == NULL) {
fd_src = open(realname, O_RDONLY);
if (fd_src != -1) {
source = fdopen(fd_src, "rb");
if (source == NULL)
close(fd_src);
}
if (source == NULL) {
statusline(ALERT, _("Error reading %s: %s"), realname,
strerror(errno));
strerror(errno));
goto cleanup_and_exit;
}
@ -1738,21 +1746,7 @@ bool write_file(const char *name, FILE *stream, bool tmp,
goto cleanup_and_exit;
}
fd_src = open(realname, O_RDONLY);
if (fd_src != -1) {
source = fdopen(fd_src, "rb");
if (source == NULL) {
statusline(ALERT, _("Error reading %s: %s"), realname,
strerror(errno));
close(fd_src);
fclose(target);
unlink(tempname);
goto cleanup_and_exit;
}
}
if (source == NULL || copy_file(source, target, TRUE) != 0) {
if (copy_file(source, target, TRUE) != 0) {
statusline(ALERT, _("Error writing temp file: %s"),
strerror(errno));
unlink(tempname);