Skip to content

Commit

Permalink
Raise Errno::ENOTDIR if the parent of an opened file is not a directory.
Browse files Browse the repository at this point in the history
Note that Errno::ENOENT is raised on Windows. This is consistent with
the MRI implementation.
philr authored and kares committed Oct 18, 2017
1 parent 471b9c9 commit 870011e
Showing 4 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/org/jruby/util/RegularFileResource.java
Original file line number Diff line number Diff line change
@@ -173,6 +173,8 @@ public Channel openChannel(ModeFlags flags, int perm) throws ResourceException {
throw new ResourceException.TooManySymlinks(absolutePath());
case EISDIR:
throw new ResourceException.FileIsDirectory(absolutePath());
case ENOTDIR:
throw new ResourceException.FileIsNotDirectory(absolutePath());
default:
throw new ResourceException.IOError(new IOException("unhandled errno: " + errno));

4 changes: 4 additions & 0 deletions core/src/main/java/org/jruby/util/ResourceException.java
Original file line number Diff line number Diff line change
@@ -32,6 +32,10 @@ public static class FileIsDirectory extends ErrnoException {
public FileIsDirectory(String path) { super("EISDIR", path); }
}

public static class FileIsNotDirectory extends ErrnoException {
public FileIsNotDirectory(String path) { super ("ENOTDIR", path); }
}

public static class FileExists extends ErrnoException {
public FileExists(String path) { super("EEXIST", path); }
}
2 changes: 2 additions & 0 deletions core/src/main/java/org/jruby/util/io/PosixShim.java
Original file line number Diff line number Diff line change
@@ -416,6 +416,8 @@ public Channel open(String cwd, String path, ModeFlags flags, int perm) {
errno = Errno.EEXIST;
} catch (ResourceException.FileIsDirectory e) {
errno = Errno.EISDIR;
} catch (ResourceException.FileIsNotDirectory e) {
errno = Errno.ENOTDIR;
} catch (ResourceException.NotFound e) {
errno = Errno.ENOENT;
} catch (ResourceException.PermissionDenied e) {
5 changes: 5 additions & 0 deletions test/jruby/test_io.rb
Original file line number Diff line number Diff line change
@@ -205,6 +205,11 @@ def test_open_with_block
assert_raises(Errno::EBADF) { f.close }
end

def test_open_child_of_file
ensure_files @file
assert_raises(WINDOWS ? Errno::ENOENT : Errno::ENOTDIR) { File.open(File.join(@file, 'child')) }
end

unless WINDOWS # Windows doesn't take kindly to perm mode tests
def test_sysopen
ensure_files @file

0 comments on commit 870011e

Please sign in to comment.