summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authorNAKAMURA Usaku <usa@ruby-lang.org>2022-03-19 22:41:16 +0900
committerNAKAMURA Usaku <usa@ruby-lang.org>2022-03-19 22:41:16 +0900
commit7eaec9a6b355a685a3a9503a005b91f4b3cb4f39 (patch)
tree696cb2af22077cfbe40519b86ed732d949a4ba01 /io.c
parent5da2a3e31699570ae1a951c8bbc489040051d19e (diff)
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(+)
Diffstat (limited to 'io.c')
-rw-r--r--io.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/io.c b/io.c
index 4d32e2ee3b..be6c6b637f 100644
--- a/io.c
+++ b/io.c
@@ -11022,6 +11022,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;