File.open with open mode a+ do not work #4348

Closed
limmet opened this Issue Dec 1, 2016 · 3 comments

Projects

None yet

3 participants

@limmet
limmet commented Dec 1, 2016

Environment

jruby 9.1.5.0 (2.3.1) 2016-09-07 036ce39 Java HotSpot(TM) 64-Bit Server VM 25.40-b25 on 1.8.0_40-b26 +jit [mswin32-x86_64]

Behavior

In version 9.1.5.0 of Jruby when i use the a+ mode for opening and writing to a file the pointer is at the begining of the file.

The expected behaviour is to position at the end of the file for appending.
The script below works in MRI ruby 2.3.1 but overwrites at the top of the file for Jruby 9.1.5.0 and 9.1.6.0

date_now = Time.now
path_to_logfile = 'C:/temp/jruby_file_log'
Dir.mkdir path_to_logfile unless File.directory? path_to_logfile
path_to_logfile += "/test_#{date_now.strftime('%Y_%m_%d')}.txt"
File.open(path_to_logfile, 'a+') { |f| f.write("First: #{date_now}\n") }
File.open(path_to_logfile, 'a+') { |f| f.write("Second:#{date_now}\n") }

@qzio
qzio commented Dec 1, 2016

Is this a windows-only bug?

I just tested this on debian/jessie:

$ ruby -v && ruby test.rb && cat test_2016_12_01.txt 
jruby 9.1.5.0 (2.3.1) 2016-09-07 036ce39 OpenJDK 64-Bit Server VM 25.111-b14 on 1.8.0_111-8u111-b14-2~bpo8+1-b14 +jit [linux-x86_64]
First: 2016-12-01 13:25:09 +0000
Second:2016-12-01 13:25:09 +0000
@enebo enebo added this to the JRuby 9.1.7.0 milestone Dec 1, 2016
@enebo
Member
enebo commented Dec 1, 2016

@qzio yeah it works fine on MacOs as well. Windows does not have all the native methods we would like implemented yet so in places we go down a totally different codepath. I also cannot test whether this works with native support disabled on windows: #4349

@enebo enebo closed this in d26784d Dec 9, 2016
@enebo
Member
enebo commented Dec 9, 2016

@limmet can you tell me if you just happened to be playing with a+ and noticed this or you had a real use case which needs a+? I have been trying to figure out what people use a+ for and would love a real world use case for it. I tried searching online and I got either: a) how does a+ work b) your impl does not support a+ properly c) I am using a+ and it does this weird thing.

So quick note on this one. We did not support a+ when native was disabled OR on windows where we still use pure-Java code for this. The behavior is new to me and I used to do C a gazillion years ago. In a+ mode, you can seek and read all you want but if you write then because O_APPEND is set it will write to the end of the file. Even if you seeked away from the end at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment