From f4f0c793f6eb427b0a85445bff49fdc6b73447ae Mon Sep 17 00:00:00 2001 From: nagachika Date: Sun, 13 Mar 2022 15:10:34 +0900 Subject: merge revision(s) b555e659c4974acc423083b71b1bd5ec6a926046: [Backport #18388] Do not use `fcopyfile` if appending to non-empty file [Bug #18388] `fcopyfile` appends `src` to `to` and then truncates `to` to it's original size. --- io.c | 7 +++++++ test/ruby/test_io.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+) --- io.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'io.c') diff --git a/io.c b/io.c index 9720aed06e..caa4dd28f7 100644 --- a/io.c +++ b/io.c @@ -11250,6 +11250,13 @@ nogvl_fcopyfile(struct copy_stream_struct *stp) return 0; if (lseek(stp->dst_fd, 0, SEEK_CUR) > (off_t)0) /* if dst IO was already written */ return 0; + if (fcntl(stp->dst_fd, F_GETFL) & O_APPEND) { + /* fcopyfile(3) appends src IO to dst IO and then truncates + * dst IO to src IO's original size. */ + off_t end = lseek(stp->dst_fd, 0, SEEK_END); + lseek(stp->dst_fd, 0, SEEK_SET); + if (end > (off_t)0) return 0; + } if (src_offset > (off_t)0) { off_t r; -- cgit v1.2.3