From f5693d4151ece1a3b59c53e21c0eb1d6d940fdd1 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 16 Oct 2019 17:40:52 +0200 Subject: [PATCH] tweaks: elide a duplicate opening of the existing file when prepending --- src/files.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/files.c b/src/files.c index 95747ea7..fd35096a 100644 --- a/src/files.c +++ b/src/files.c @@ -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);