Skip to content

Commit cf3bae6

Browse files
committed
[Truffle] Correct exception when reading as a dir something that is not a dir.
1 parent 314295c commit cf3bae6

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

truffle/src/main/java/org/jruby/truffle/nodes/rubinius/DirPrimitiveNodes.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.oracle.truffle.api.nodes.UnexpectedResultException;
1515
import com.oracle.truffle.api.object.HiddenKey;
1616
import com.oracle.truffle.api.source.SourceSection;
17+
import jnr.constants.platform.Errno;
1718
import org.jruby.truffle.nodes.objectstorage.ReadHeadObjectFieldNode;
1819
import org.jruby.truffle.nodes.objectstorage.WriteHeadObjectFieldNode;
1920
import org.jruby.truffle.runtime.RubyCallStack;
@@ -69,10 +70,18 @@ public DirOpenPrimitiveNode(DirOpenPrimitiveNode prev) {
6970
@CompilerDirectives.TruffleBoundary
7071
@Specialization
7172
public RubyNilClass open(RubyBasicObject dir, RubyString path, RubyNilClass encoding) {
72-
final String[] contents = new File(path.toString()).list();
73+
// TODO CS 22-Apr-15 race conditions here
74+
75+
final File file = new File(path.toString());
76+
77+
if (!file.isDirectory()) {
78+
throw new RaiseException(getContext().getCoreLibrary().errnoError(Errno.ENOTDIR.intValue(), this));
79+
}
80+
81+
final String[] contents = file.list();
7382

7483
if (contents == null) {
75-
throw new RaiseException(getContext().getCoreLibrary().fileNotFoundError(path.toString(), this));
84+
throw new UnsupportedOperationException();
7685
}
7786

7887
writeContentsNode.execute(dir, contents);

truffle/src/main/java/org/jruby/truffle/runtime/core/CoreLibrary.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,7 @@ public RubyException errnoError(int errno, Node currentNode) {
702702
return systemCallError(String.format("Unknown Error (%s)", errno), currentNode);
703703
}
704704

705-
// TODO (nirvdrum 03-Apr-15): This should return the correct errno exception class.
706-
return systemCallError(errnoObj.description(), currentNode);
705+
return new RubyException(getErrnoClass(errnoObj), context.makeString(errnoObj.description()), RubyCallStack.getBacktrace(currentNode));
707706
}
708707

709708
public RubyException indexError(String message, Node currentNode) {

0 commit comments

Comments
 (0)