Skip to content
This repository
Browse code

JRUBY-1214: The behaviour of File.flock is not same with C Ruby.

(Patch by Kaoru Shirai, with modifications and regression test).

Signed-off-by: Vladimir Sizikov <vsizikov@gmail.com>


git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@5930 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information...
commit 0607cc9eda1aa4efd7b3e7cb57444459331a472b 1 parent 0cdac18
Vladimir Sizikov vvs authored

Showing 2 changed files with 26 additions and 1 deletion. Show diff stats Hide diff stats

  1. +10 0 src/org/jruby/RubyFile.java
  2. +16 1 test/test_file.rb
10 src/org/jruby/RubyFile.java
@@ -305,6 +305,16 @@ public IRubyObject flock(IRubyObject lockingConstant) {
305 305 FileChannel fileChannel = (FileChannel)openFile.getMainStream().getDescriptor().getChannel();
306 306 int lockMode = RubyNumeric.num2int(lockingConstant);
307 307
  308 + // Exclusive locks in Java require the channel to be writable, otherwise
  309 + // an exception is thrown (terminating JRuby execution).
  310 + // But flock behavior of MRI is that it allows
  311 + // exclusive locks even on non-writable file. So we convert exclusive
  312 + // lock to shared lock if the channel is not writable, to better match
  313 + // the MRI behavior.
  314 + if (!openFile.isWritable() && (lockMode & LOCK_EX) > 0) {
  315 + lockMode = (lockMode ^ LOCK_EX) | LOCK_SH;
  316 + }
  317 +
308 318 try {
309 319 switch (lockMode) {
310 320 case LOCK_UN:
17 test/test_file.rb
@@ -407,7 +407,22 @@ def test_flock
407 407 file.close
408 408 File.delete(filename)
409 409 end
410   -
  410 +
  411 + # JRUBY-1214
  412 + def test_flock_exclusive_on_readonly
  413 + filename = '__lock_test_2_'
  414 + File.open(filename, "w+") { }
  415 + File.open(filename, "r") do |file|
  416 + begin
  417 + assert_nothing_raised {
  418 + file.flock(File::LOCK_EX)
  419 + }
  420 + ensure
  421 + file.flock(File::LOCK_UN)
  422 + end
  423 + end
  424 + end
  425 +
411 426 def test_truncate_doesnt_create_file
412 427 name = "___foo_bar___"
413 428 assert(!File.exists?(name))

0 comments on commit 0607cc9

Please sign in to comment.
Something went wrong with that request. Please try again.