Skip to content

Commit

Permalink
merge revision(s) b555e65: [Backport #18388]
Browse files Browse the repository at this point in the history
    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(+)
  • Loading branch information
unak committed Mar 19, 2022
1 parent 5da2a3e commit 7eaec9a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions io.c
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions test/ruby/test_io.rb
Expand Up @@ -440,6 +440,18 @@ def test_copy_stream_append
}
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|

Expand Down
2 changes: 1 addition & 1 deletion version.h
Expand Up @@ -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
Expand Down

0 comments on commit 7eaec9a

Please sign in to comment.