From 8ef0192103f9edc4a7193f635a27206b56944637 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 27 Aug 2013 08:03:00 +0000 Subject: [PATCH] io.c: copy in binary mode * io.c (copy_stream_body): should write in binary mode. based on a patch by godfat (Lin Jen-Shin) at [ruby-core:56556]. [ruby-core:56518] [Bug #8767] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42709 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++++++++ io.c | 4 ++-- test/ruby/test_io.rb | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 58e9f285cd..da8beb82b2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Tue Aug 27 17:02:58 2013 Nobuyoshi Nakada + + * io.c (copy_stream_body): should write in binary mode. based on a + patch by godfat (Lin Jen-Shin) at [ruby-core:56556]. + [ruby-core:56518] [Bug #8767] + +Tue Aug 27 17:02:33 2013 Nobuyoshi Nakada + + * io.c (copy_stream_body): move common open flags. + Tue Aug 27 16:56:50 2013 Nobuyoshi Nakada * enumerator.c (enumerator_size): use rb_check_funcall() instead of diff --git a/io.c b/io.c index 5cffc6eeec..8d2ed8d06c 100644 --- a/io.c +++ b/io.c @@ -10229,9 +10229,9 @@ copy_stream_body(VALUE arg) #ifdef O_BINARY if (src_fptr) SET_BINARY_MODE_WITH_SEEK_CUR(src_fptr); - if (dst_fptr) - setmode(dst_fd, O_BINARY); #endif + if (dst_fptr) + rb_io_ascii8bit_binmode(dst_io); if (stp->src_offset == (off_t)-1 && src_fptr && src_fptr->rbuf.len) { size_t len = src_fptr->rbuf.len; diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 4f002e8b3d..e424747734 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -857,6 +857,47 @@ class TestIO < Test::Unit::TestCase } end + def test_copy_stream_write_in_binmode + bug8767 = '[ruby-core:56518] [Bug #8767]' + mkcdtmpdir { + EnvUtil.with_default_internal(Encoding::UTF_8) do + # StringIO to object with to_path + bytes = "\xDE\xAD\xBE\xEF".force_encoding(Encoding::ASCII_8BIT) + src = StringIO.new(bytes) + dst = Object.new + def dst.to_path + "qux" + end + assert_nothing_raised(bug8767) { + IO.copy_stream(src, dst) + } + assert_equal(bytes, File.binread("qux"), bug8767) + assert_equal(4, src.pos, bug8767) + end + } + end + + def test_copy_stream_read_in_binmode + bug8767 = '[ruby-core:56518] [Bug #8767]' + mkcdtmpdir { + EnvUtil.with_default_internal(Encoding::UTF_8) do + # StringIO to object with to_path + bytes = "\xDE\xAD\xBE\xEF".force_encoding(Encoding::ASCII_8BIT) + File.binwrite("qux", bytes) + dst = StringIO.new + src = Object.new + def src.to_path + "qux" + end + assert_nothing_raised(bug8767) { + IO.copy_stream(src, dst) + } + assert_equal(bytes, dst.string.b, bug8767) + assert_equal(4, dst.pos, bug8767) + end + } + end + class Rot13IO def initialize(io) @io = io