Permalink
Browse files

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...
1 parent 0cdac18 commit 0607cc9eda1aa4efd7b3e7cb57444459331a472b @vvs vvs committed Feb 14, 2008
Showing with 26 additions and 1 deletion.
  1. +10 −0 src/org/jruby/RubyFile.java
  2. +16 −1 test/test_file.rb
@@ -305,6 +305,16 @@ public IRubyObject flock(IRubyObject lockingConstant) {
FileChannel fileChannel = (FileChannel)openFile.getMainStream().getDescriptor().getChannel();
int lockMode = RubyNumeric.num2int(lockingConstant);
+ // Exclusive locks in Java require the channel to be writable, otherwise
+ // an exception is thrown (terminating JRuby execution).
+ // But flock behavior of MRI is that it allows
+ // exclusive locks even on non-writable file. So we convert exclusive
+ // lock to shared lock if the channel is not writable, to better match
+ // the MRI behavior.
+ if (!openFile.isWritable() && (lockMode & LOCK_EX) > 0) {
+ lockMode = (lockMode ^ LOCK_EX) | LOCK_SH;
+ }
+
try {
switch (lockMode) {
case LOCK_UN:
View
@@ -407,7 +407,22 @@ def test_flock
file.close
File.delete(filename)
end
-
+
+ # JRUBY-1214
+ def test_flock_exclusive_on_readonly
+ filename = '__lock_test_2_'
+ File.open(filename, "w+") { }
+ File.open(filename, "r") do |file|
+ begin
+ assert_nothing_raised {
+ file.flock(File::LOCK_EX)
+ }
+ ensure
+ file.flock(File::LOCK_UN)
+ end
+ end
+ end
+
def test_truncate_doesnt_create_file
name = "___foo_bar___"
assert(!File.exists?(name))

0 comments on commit 0607cc9

Please sign in to comment.