Permalink
Browse files

Fix File.readlink when chdir and avoid NPE

Existence test is moved in the catch block since readlink should succeed even if target does not exist.

Signed-off-by: Charles Oliver Nutter <headius@headius.com>
  • Loading branch information...
eregon authored and headius committed Apr 4, 2012
1 parent df125be commit 7bb636efaeade8a8ca247e1a517bbbe3ecb6715f
Showing with 8 additions and 6 deletions.
  1. +8 −6 src/org/jruby/RubyFile.java
@@ -922,24 +922,26 @@ public static IRubyObject symlink(ThreadContext context, IRubyObject recv, IRuby
@JRubyMethod(required = 1, meta = true)
public static IRubyObject readlink(ThreadContext context, IRubyObject recv, IRubyObject path) {
Ruby runtime = context.getRuntime();
+ JRubyFile link = file(path);
try {
- String realPath = runtime.getPosix().readlink(path.convertToString().getUnicodeValue());
-
- if (!RubyFileTest.exist_p(recv, path).isTrue()) {
- throw runtime.newErrnoENOENTError(path.toString());
- }
-
+ String realPath = runtime.getPosix().readlink(link.toString());
+
if (!RubyFileTest.symlink_p(recv, path).isTrue()) {
throw runtime.newErrnoEINVALError(path.toString());
}
if (realPath == null) {
//FIXME: When we get JNA3 we need to properly write this to errno.
+ throw runtime.newErrnoFromLastPOSIXErrno();
}
return runtime.newString(realPath);
} catch (IOException e) {
+ if (!RubyFileTest.exist_p(recv, path).isTrue()) {
+ throw runtime.newErrnoENOENTError(link.toString());
+ }
+
throw runtime.newIOError(e.getMessage());
}
}

0 comments on commit 7bb636e

Please sign in to comment.