commit f5693d4151ece1a3b59c53e21c0eb1d6d940fdd1
parent e3807f00a2c2ecb6e882bdb0838a8cbb275fda69
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Wed, 16 Oct 2019 17:40:52 +0200
tweaks: elide a duplicate opening of the existing file when prepending
Diffstat:
1 file changed, 11 insertions(+), 17 deletions(-)
diff --git 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);