Skip to content

Commit

Permalink
Fixes #4692. File path canonicalization still broken inside jars on W…
Browse files Browse the repository at this point in the history
…indows

This fixed two things in expandPathInternal which displayed differences in
File.expand_path, and File.realpath.  The fixes themselves are pretty gorey
but since they are limited to classpath uris and only on windows I see no
risk.  We really really need to rewrite expandPathInternal...
enebo committed Sep 6, 2017
1 parent 4c22238 commit c588a18
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
@@ -1662,11 +1662,20 @@ private static IRubyObject expandPathInternal(ThreadContext context, IRubyObject
relativePath = canonicalizePath(relativePath);
}

if (Platform.IS_WINDOWS && !preFix.contains("file:") && startsWithDriveLetterOnWindows(relativePath)) {
// this is basically for classpath:/ and uri:classloader:/
relativePath = relativePath.substring(2).replace('\\', '/');
if (Platform.IS_WINDOWS) {
// FIXME: If this is only for classLoader uri's then we probably don't need file: check here.
// Also can we ever get a drive letter in relative path now?
if (!preFix.contains("file:") && startsWithDriveLetterOnWindows(relativePath)) {
// this is basically for classpath:/ and uri:classloader:/
relativePath = relativePath.substring(2);
}
if (classloaderURI) {
relativePath = relativePath.replace('\\', '/');
}
}



return concatStrings(runtime, preFix, extra, relativePath, enc);
}

@@ -1782,7 +1791,24 @@ private static IRubyObject expandPathInternal(ThreadContext context, IRubyObject
path = JRubyFile.create(cwd, relativePath);
}

String realPath = padSlashes + canonicalize(path.getAbsolutePath());
String canonicalPath = null;
if (Platform.IS_WINDOWS && uriParts != null && "classpath:".equals(uriParts[0])) {
// FIXME: This is all total madness. we extract off classpath: earlier in processing
// and then build a absolute path which on windows will stick a drive letter onto it
// but this is bogus in a classpath file path. I think the proper fix for expand path
// is to split out non-file: scheme format paths into a totally different method. Weaving
// uri and non-uri paths into one super long method is so rife with hurt that I am literally
// crying on my keyboard.
String absolutePath = path.getAbsolutePath();
if (absolutePath.length() >= 2 && absolutePath.charAt(1) == ':') {
canonicalPath = canonicalize(absolutePath.substring(2));
}
}

if (canonicalPath == null) canonicalPath = canonicalize(path.getAbsolutePath());

String realPath = padSlashes + canonicalPath;

if (realPath.startsWith("file:") && preFix.length() > 0) realPath = realPath.substring(5);

if (canonicalize) {

0 comments on commit c588a18

Please sign in to comment.