From 7eaec9a6b355a685a3a9503a005b91f4b3cb4f39 Mon Sep 17 00:00:00 2001 From: NAKAMURA Usaku Date: Sat, 19 Mar 2022 22:41:16 +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 +++++++ test/ruby/test_io.rb | 12 ++++++++++++ version.h | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) 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; diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 1e4d88ef1e..37f1477483 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -440,6 +440,18 @@ class TestIO < Test::Unit::TestCase } end + def test_copy_stream_append_to_nonempty + with_srccontent("foobar") {|src, content| + preface = 'preface' + File.write('dst', preface) + File.open('dst', 'ab') do |dst| + ret = IO.copy_stream(src, dst) + assert_equal(content.bytesize, ret) + assert_equal(preface + content, File.read("dst")) + end + } + end + def test_copy_stream_smaller with_srccontent {|src, content| diff --git a/version.h b/version.h index 8afa4b53af..6fbb66953c 100644 --- a/version.h +++ b/version.h @@ -2,7 +2,7 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 6 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 215 +#define RUBY_PATCHLEVEL 216 #define RUBY_RELEASE_YEAR 2022 #define RUBY_RELEASE_MONTH 3 -- cgit v1.2.3