Skip to content

Raise Errno::EISDIR when opening a directory for reading on Windows#4818

Merged
enebo merged 2 commits intojruby:masterfrom
philr:file_open_read_raise_eisdir_on_windows
Jan 24, 2018
Merged

Raise Errno::EISDIR when opening a directory for reading on Windows#4818
enebo merged 2 commits intojruby:masterfrom
philr:file_open_read_raise_eisdir_on_windows

Conversation

@philr
Copy link
Contributor

@philr philr commented Oct 15, 2017

On Windows, MRI 2.0.0 and later raises Errno::EISDIR when attempting to open a directory for reading:

> File.open('C:\\Windows', 'r')
Errno::EISDIR: Is a directory @ rb_sysopen - C:\Windows
        from (irb):1:in `initialize'
        from (irb):1:in `open'
        from (irb):1

However, JRuby 9.1.13.0 raises Errno::EACCES instead:

> File.open('C:\\Windows', 'r')
Errno::EACCES: Permission denied - C:\Windows
        from org/jruby/RubyFile.java:366:in `initialize'
        from org/jruby/RubyIO.java:1154:in `open'
        from (irb):1:in `<eval>'
        from org/jruby/RubyKernel.java:994:in `eval'
        from org/jruby/RubyKernel.java:1292:in `loop'
        from org/jruby/RubyKernel.java:1114:in `catch'
        from org/jruby/RubyKernel.java:1114:in `catch'

This pull request modifies RegularFileResource.openChannel() to throw ResourceException.FileIsDirectory in all cases when opening a directory instead of just for writes.

}

if (file.isDirectory() && flags.isWritable()) {
if (file.isDirectory()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philr Should this happen on all platforms and not just windows? Sorry this PR got lost in the cracks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enebo On platforms other than Windows (with both MRI and JRuby), File.open(directory) succeeds and an Errno::EISDIR exception is raised when calling File#read.

On Windows, File.open(directory) raises an exception. MRI raises Errno::EISDIR, but JRuby raises Errno::EACCES.

To replicate the behaviour of MRI, it is just this code that handles Windows in the openChannel() method that needs to be changed.

@enebo enebo added this to the JRuby 9.1.16.0 milestone Jan 16, 2018
@enebo enebo merged commit 73846e9 into jruby:master Jan 24, 2018
@philr philr deleted the file_open_read_raise_eisdir_on_windows branch March 23, 2025 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants