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...
  • Loading branch information
enebo committed Sep 6, 2017
1 parent c715c6f commit a7f6943
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
@@ -1664,11 +1664,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);
}

@@ -1784,7 +1793,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 a7f6943

Please sign in to comment.