Backport #2560
closedIO.read not always closes the file
Description
=begin
Consider the following:
File.open('test_file', 'wb') { |f| f.write 'abc' }
IO.read('test_file', 0, -1) rescue nil
File.unlink('test_file')
On Windows the unlink call fails with:
`unlink': Permission denied - test_file (Errno::EACCES)
This is due to fact that IO.read, invoked with invalid offset parameter does not close the opened file. But the documentation clearly states that:
"read ensures the file is closed before returning."
This causes two RubySpec failures for IO.read:
mspec -t r core\io\read_spec.rb
ruby 1.9.2dev (2009-11-12 trunk 25723) [i386-mingw32]
..........E..E..................E.
An exception occurred during: after :each
IO.read raises an Errno::EINVAL when not passed a valid offset ERROR
Errno::EACCES: Permission denied - D:/work/jruby-dev/rubyspec.git/rubyspec_temp/io_read.txt
D:/work/jruby-dev/rubyspec.git/core/io/read_spec.rb:13:in block (2 levels) in <top (required)>' D:/work/jruby-dev/rubyspec.git/core/io/read_spec.rb:5:in
<top (required)>'
D:/work/jruby-dev/mspec.git/bin/mspec-run:8:in `'
An exception occurred during: after :each
IO#read can be read from consecutively ERROR
Errno::EACCES: Permission denied - D:/work/jruby-dev/rubyspec.git/rubyspec_temp/io_read.txt
D:/work/jruby-dev/rubyspec.git/core/io/read_spec.rb:100:in block (2 levels) in <top (required)>' D:/work/jruby-dev/rubyspec.git/core/io/read_spec.rb:88:in
<top (required)>'
D:/work/jruby-dev/mspec.git/bin/mspec-run:8:in `'
=end