Skip to content

Commit

Permalink
fix creating FileResource with CWD a uri-like path and path a regular…
Browse files Browse the repository at this point in the history
… file

if the path is an absolute path to regular file and the CWD is using a
uri:classloader:/ then the absolute path on the filesystem "wins"

fixes #3071

Sponsored by Lookout Inc.
mkristian committed Jun 22, 2015
1 parent e4be16f commit edac27b
Showing 2 changed files with 28 additions and 6 deletions.
14 changes: 8 additions & 6 deletions core/src/main/java/org/jruby/util/JRubyFile.java
Original file line number Diff line number Diff line change
@@ -97,7 +97,8 @@ public static FileResource createResource(POSIX posix, Ruby runtime, String cwd,
}
}

if (cwd != null && (cwd.startsWith("uri:") || cwd.startsWith("file:"))) {
File internal = new JavaSecuredFile(pathname);
if (cwd != null && !internal.isAbsolute() && (cwd.startsWith("uri:") || cwd.startsWith("file:"))) {
return createResource(posix, runtime, null, cwd + "/" + pathname);
}

@@ -122,14 +123,15 @@ private static JRubyFile createNoUnicodeConversion(String cwd, String pathname)
pathname = pathname.substring(5);
}
File internal = new JavaSecuredFile(pathname);
if(cwd != null && cwd.startsWith("uri:") && !pathname.startsWith("uri:") && !pathname.contains("!/") && !internal.isAbsolute()) {
if (internal.isAbsolute()) {
return new JRubyFile(internal);
}
if(cwd != null && cwd.startsWith("uri:") && !pathname.startsWith("uri:") && !pathname.contains("!/")) {
return new JRubyFile(cwd + "/" + pathname);
}
internal = new JavaSecuredFile(cwd, pathname);
if(!internal.isAbsolute()) {
internal = new JavaSecuredFile(cwd, pathname);
if(!internal.isAbsolute()) {
throw new IllegalArgumentException("Neither current working directory ("+cwd+") nor pathname ("+pathname+") led to an absolute path");
}
throw new IllegalArgumentException("Neither current working directory ("+cwd+") nor pathname ("+pathname+") led to an absolute path");
}
return new JRubyFile(internal);
}
20 changes: 20 additions & 0 deletions test/test_file.rb
Original file line number Diff line number Diff line change
@@ -970,6 +970,26 @@ def test_file_utf8
end
end

def test_file_with_absolute_path_and_uri_path_as_cwd
filename = File.expand_path( 'test_absolute_path_and_uri_path_as_cwd' )

Dir.chdir('uri:classloader:/') do
begin
f = File.new(filename, File::CREAT)
assert_equal(nil, f.read(1))

assert File.file?(filename)
assert File.exist?(filename)
ensure
f.close
File.delete(filename)

assert !File.file?(filename)
assert !File.exist?(filename)
end
end
end

def test_file_create
filename = '2nnever'
f = File.new(filename, File::CREAT)

0 comments on commit edac27b

Please sign in to comment.