ruby-changes:70063
From: Nobuyoshi <ko1@a...>
Date: Sun, 5 Dec 2021 19:04:44 +0900 (JST)
Subject: [ruby-changes:70063] b555e659c4 (master): Do not use `fcopyfile` if appending to non-empty file [Bug #18388]
https://git.ruby-lang.org/ruby.git/commit/?id=b555e659c4 From b555e659c4974acc423083b71b1bd5ec6a926046 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sun, 5 Dec 2021 18:08:55 +0900 Subject: 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(+) diff --git a/io.c b/io.c index b2ca5a69e37..bc64938648b 100644 --- a/io.c +++ b/io.c @@ -11483,6 +11483,13 @@ nogvl_fcopyfile(struct copy_stream_struct *stp) https://github.com/ruby/ruby/blob/trunk/io.c#L11483 return 0; if (lseek(stp->dst_fptr->fd, 0, SEEK_CUR) > (off_t)0) /* if dst IO was already written */ return 0; + if (fcntl(stp->dst_fptr->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_fptr->fd, 0, SEEK_END); + lseek(stp->dst_fptr->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 96e572b98d7..71a8911fbd0 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -476,6 +476,18 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L476 } 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| -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/