From f09f5bc32b54e9d2e9f594fd2a2358af0a0f999a Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 8 Jun 2010 03:26:43 +0000 Subject: workaround IO#reopen bug in rbx when reopening logs IO#reopen in Rubinius seems to munge the O_APPEND flag when passed a path, however passing an actual IO object. However, at the system call level, everything is the same. ref: http://github.com/evanphx/rubinius/issues/347 --- lib/unicorn/util.rb | 2 +- test/unit/test_util.rb | 40 ++++++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/unicorn/util.rb b/lib/unicorn/util.rb index 5ceda5c..458b6d7 100644 --- a/lib/unicorn/util.rb +++ b/lib/unicorn/util.rb @@ -57,7 +57,7 @@ module Unicorn open_arg << ":#{enc.to_s}" enc = fp.internal_encoding and open_arg << ":#{enc.to_s}" end - fp.reopen(fp.path, open_arg) + File.open(fp.path, 'ab') { |tmpfp| fp.reopen(tmpfp) } fp.sync = true new_st = fp.stat if orig_st.uid != new_st.uid || orig_st.gid != new_st.gid diff --git a/test/unit/test_util.rb b/test/unit/test_util.rb index b267179..377f6b8 100644 --- a/test/unit/test_util.rb +++ b/test/unit/test_util.rb @@ -8,26 +8,29 @@ class TestUtil < Test::Unit::TestCase EXPECT_FLAGS = File::WRONLY | File::APPEND def test_reopen_logs_noop tmp = Tempfile.new('') - tmp.reopen(tmp.path, 'a') - tmp.sync = true - ext = tmp.external_encoding rescue nil - int = tmp.internal_encoding rescue nil - before = tmp.stat.inspect + fp = File.open(tmp.path, 'ab') + fp.sync = true + ext = fp.external_encoding rescue nil + int = fp.internal_encoding rescue nil + before = fp.stat.inspect Unicorn::Util.reopen_logs - assert_equal before, File.stat(tmp.path).inspect - assert_equal ext, (tmp.external_encoding rescue nil) - assert_equal int, (tmp.internal_encoding rescue nil) + assert_equal before, File.stat(fp.path).inspect + assert_equal ext, (fp.external_encoding rescue nil) + assert_equal int, (fp.internal_encoding rescue nil) + assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL)) assert_nothing_raised { tmp.close! } + assert_nothing_raised { fp.close } end def test_reopen_logs_renamed tmp = Tempfile.new('') tmp_path = tmp.path.freeze - tmp.reopen(tmp_path, 'a') - tmp.sync = true - ext = tmp.external_encoding rescue nil - int = tmp.internal_encoding rescue nil - before = tmp.stat.inspect + fp = File.open(tmp_path, 'ab') + fp.sync = true + + ext = fp.external_encoding rescue nil + int = fp.internal_encoding rescue nil + before = fp.stat.inspect to = Tempfile.new('') File.rename(tmp_path, to.path) assert ! File.exist?(tmp_path) @@ -35,13 +38,14 @@ class TestUtil < Test::Unit::TestCase assert_equal tmp_path, tmp.path assert File.exist?(tmp_path) assert before != File.stat(tmp_path).inspect - assert_equal tmp.stat.inspect, File.stat(tmp_path).inspect - assert_equal ext, (tmp.external_encoding rescue nil) - assert_equal int, (tmp.internal_encoding rescue nil) - assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & tmp.fcntl(Fcntl::F_GETFL)) - assert tmp.sync + assert_equal fp.stat.inspect, File.stat(tmp_path).inspect + assert_equal ext, (fp.external_encoding rescue nil) + assert_equal int, (fp.internal_encoding rescue nil) + assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL)) + assert fp.sync assert_nothing_raised { tmp.close! } assert_nothing_raised { to.close! } + assert_nothing_raised { fp.close } end def test_reopen_logs_renamed_with_encoding -- cgit v1.2.3-24-ge0c7