Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2146,11 +2146,13 @@ private static CharSequence canonicalize(CharSequence canonicalPath, String rema
}
} else if (child.equals("..")) {
if (canonicalPath == null) throw new IllegalArgumentException("Cannot have .. at the start of an absolute path");
canonicalPath = canonicalPath.toString();
int lastDir = ((String) canonicalPath).lastIndexOf('/');
String canonicalPathString = canonicalPath.toString();
int lastDir = canonicalPathString.lastIndexOf('/');
if (lastDir == -1) {
if (startsWithDriveLetterOnWindows(canonicalPath)) {
// do nothing, we should not delete the drive letter
} else if (isLocalURI(canonicalPathString)) {
// do nothing, leave the URI bits alone
} else {
path = "";
}
Expand All @@ -2160,13 +2162,29 @@ private static CharSequence canonicalize(CharSequence canonicalPath, String rema
} else if (canonicalPath == null) {
path = child;
} else {
path = new StringBuilder(canonicalPath.length() + 1 + child.length()).
append(canonicalPath).append('/').append(child);
// add a slash (if not already there) plus child and recurse) (jruby/jruby#6045)
int length = canonicalPath.length();
String canonPathString = canonicalPath.toString();
if (canonPathString.length() > 0 &&
canonicalPath.charAt(length - 1) == '/' &&
canonPathString.startsWith("uri:classloader:")) {
path = canonicalPath + child;
} else {
path = canonicalPath + "/" + child;
}
}

return canonicalize(path, remaining);
}

private static boolean isLocalURI(String canonicalPathString) {
return startsWith("classpath:", canonicalPathString) ||
startsWith("classloader:", canonicalPathString) ||
startsWith("uri:classloader:", canonicalPathString) ||
startsWith("file:", canonicalPathString) ||
startsWith("jar:file", canonicalPathString);
}

private static StringBuilder appendSlash(final CharSequence canonicalPath) {
return new StringBuilder(canonicalPath.length() + 1).append(canonicalPath).append('/');
}
Expand Down
4 changes: 2 additions & 2 deletions spec/java_integration/paths/uri_classloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

describe "uri:classloader path strings" do
let(:many_slashes_path) { "uri:classloader:////foo/b.gemspec" }
let(:unc_like_path) { "uri:classloader://foo/b.gemspec" }
let(:unc_like_path) { "uri:classloader:/foo/b.gemspec" }
let(:normal_path) { "uri:classloader:/foo/b.gemspec" }

let(:sub_path) { "../../../vendor/jface/*.jar" }
let(:base_path) { "uri:classloader:/gems/swt-4.6.1/lib/swt/full.rb" }
let(:resolved_path) { "uri:classloader://gems/swt-4.6.1/vendor/jface/*.jar" }
let(:resolved_path) { "uri:classloader:/gems/swt-4.6.1/vendor/jface/*.jar" }
let(:resolved_path_win) { "uri:classloader:/gems/swt-4.6.1/vendor/jface/*.jar" }
let(:windows) { RbConfig::CONFIG['host_os'] =~ /Windows|mswin/ }

Expand Down
12 changes: 4 additions & 8 deletions test/jruby/test_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,9 @@ def test_expand_path_with_uri_file_prefix_slash_slash_slash
def test_expand_path_with_uri_classloader_prefix
assert_equal "uri:classloader:/foo/bar", File.expand_path("uri:classloader:/foo/bar")
assert_equal "uri:classloader:/bar", File.expand_path("uri:classloader:/foo/../bar")
# uri:classloader:// and uri:classloader:/ are treated as equivalent as
# there is an internal normalization going on path which replace all // with /
assert_equal "uri:classloader://foo/bar/baz", File.expand_path("baz", "uri:classloader:/foo/bar")
assert_equal "uri:classloader:/foo/bar/baz", File.expand_path("baz", "uri:classloader:/foo/bar")
assert_equal "uri:classloader:/foo/bar", File.expand_path("uri:classloader:/foo/bar", "uri:classloader:/baz/quux")
# uri:classloader:// and uri:classloader:/ are treated as equivalent as
# there is an internal normalization going on path which replace all // with /
assert_equal "uri:classloader://foo/bar", File.expand_path("../../foo/bar", "uri:classloader:/baz/quux")
assert_equal "uri:classloader:/foo/bar", File.expand_path("../../foo/bar", "uri:classloader:/baz/quux")
assert_equal "uri:classloader:/foo/bar", File.expand_path("../../../foo/bar", "uri:classloader:/baz/quux")
end if IS_JRUBY

Expand All @@ -367,8 +363,8 @@ def test_expand_path_with_file_url_relative_path
# GH-3176
def test_expand_path_with_relative_reference_and_inside_uri_classloader
Dir.chdir( 'uri:classloader:/') do
assert_equal 'uri:classloader://something/foo', File.expand_path('foo', 'something')
assert_equal 'uri:classloader://foo', File.expand_path('foo', '.')
assert_equal 'uri:classloader:/something/foo', File.expand_path('foo', 'something')
assert_equal 'uri:classloader:/foo', File.expand_path('foo', '.')
end
end if IS_JRUBY

Expand Down
2 changes: 1 addition & 1 deletion test/jruby/test_pathname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_pathname_windows

# GH-3150
def test_expand_path_with_pathname_and_uri_path
assert_equal 'uri:classloader://foo', File.expand_path('foo', Pathname.new('uri:classloader:/'))
assert_equal 'uri:classloader:/foo', File.expand_path('foo', Pathname.new('uri:classloader:/'))
end if defined?(JRUBY_VERSION)

end
8 changes: 8 additions & 0 deletions test/jruby/test_uri_classloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ def test_subprocess_cwd_from_uri_classloader_cwd
assert_equal cwd, `pwd`.chomp
end
end

# GH-6045: don't double up slashes when canonicalizing a URI path
def test_uri_expand_path_does_not_double_slash
assert_equal "uri:classloader:/foo/bar", File.expand_path("uri:classloader://foo/bar")
assert_equal "uri:classloader:/foo/bar", File.expand_path("uri:classloader:/foo/bar");
assert_equal "uri:classloader:/foo/bar", File.expand_path("foo/bar", "uri:classloader://")
assert_equal "uri:classloader:/foo/bar", File.expand_path("foo/bar", "uri:classloader:/")
end
end