Skip to content

Commit

Permalink
allow to change directory into the uri:classloader:// space
Browse files Browse the repository at this point in the history
  • Loading branch information
mkristian committed Dec 27, 2014
1 parent 3d56328 commit ef39dd5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
25 changes: 16 additions & 9 deletions core/src/main/java/org/jruby/RubyDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ private static IRubyObject asRubyStringList(Ruby runtime, List<ByteList> dirs) {
}

private static String getCWD(Ruby runtime) {
if (runtime.getCurrentDirectory().startsWith("uri:")) {
return runtime.getCurrentDirectory();
}
try {
return new org.jruby.util.NormalizedFile(runtime.getCurrentDirectory()).getCanonicalPath();
} catch (Exception e) {
Expand Down Expand Up @@ -310,17 +313,22 @@ public static IRubyObject chdir(ThreadContext context, IRubyObject recv, IRubyOb
RubyFile.get_path(context, args[0]) : getHomeDirectoryPath(context);
String adjustedPath = RubyFile.adjustRootPathOnWindows(runtime, path.asJavaString(), null);
checkDirIsTwoSlashesOnWindows(runtime, adjustedPath);
JRubyFile dir = getDir(runtime, adjustedPath, true);
String realPath = null;
String oldCwd = runtime.getCurrentDirectory();
if (adjustedPath.startsWith("uri:")){
realPath = adjustedPath;
}
else {
JRubyFile dir = getDir(runtime, adjustedPath, true);

// We get canonical path to try and flatten the path out.
// a dir '/subdir/..' should return as '/'
// cnutter: Do we want to flatten path out?
try {
realPath = dir.getCanonicalPath();
} catch (IOException e) {
realPath = dir.getAbsolutePath();
// We get canonical path to try and flatten the path out.
// a dir '/subdir/..' should return as '/'
// cnutter: Do we want to flatten path out?
try {
realPath = dir.getCanonicalPath();
} catch (IOException e) {
realPath = dir.getAbsolutePath();
}
}

IRubyObject result = null;
Expand All @@ -330,7 +338,6 @@ public static IRubyObject chdir(ThreadContext context, IRubyObject recv, IRubyOb
try {
result = block.yield(context, path);
} finally {
dir = getDir(runtime, oldCwd, true);
runtime.setCurrentDirectory(oldCwd);
}
} else {
Expand Down
11 changes: 8 additions & 3 deletions core/src/main/java/org/jruby/util/JRubyFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ public static FileResource createResource(POSIX posix, Ruby runtime, String cwd,
}
}

JRubyFile f = create(cwd, pathname);
if (cwd != null && cwd.startsWith("uri:")){
return createResource(posix, runtime, null, f.getPath());
}

// If any other special resource types fail, count it as a filesystem backed resource.
return new RegularFileResource(posix, create(cwd, pathname));
return new RegularFileResource(posix, f);
}

public static String normalizeSeps(String path) {
Expand All @@ -109,10 +114,10 @@ private static JRubyFile createNoUnicodeConversion(String cwd, String pathname)
if (pathname == null || pathname.equals("") || Ruby.isSecurityRestricted()) {
return JRubyNonExistentFile.NOT_EXIST;
}
if(cwd != null && cwd.startsWith("uri:") && !pathname.startsWith("uri:")) {
File internal = new JavaSecuredFile(pathname);
if(cwd != null && cwd.startsWith("uri:") && !pathname.startsWith("uri:") && !pathname.contains("!/") && !internal.isAbsolute()) {
return new JRubyFile(cwd + "/" + pathname);
}
File internal = new JavaSecuredFile(pathname);
if(!internal.isAbsolute()) {
internal = new JavaSecuredFile(cwd, pathname);
if(!internal.isAbsolute()) {
Expand Down
50 changes: 49 additions & 1 deletion test/test_uri_classloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,64 @@ def setup
$CLASSPATH << File.expand_path( '../test_uri_classloader.jar', __FILE__ )
end

def ensure_cwd
pwd = Dir.pwd
begin
yield
ensure
Dir.chdir pwd
end
end

def test_dir_glob_on_uri_classloader_path
assert_equal ['uri:classloader://Rakefile'], Dir[ 'uri:classloader://*' ]

ensure_cwd do
Dir.chdir( 'uri:classloader://' )
assert_equal ['Rakefile'], Dir[ '*' ]
end

ensure_cwd do
JRuby.runtime.current_directory = 'uri:classloader://'
assert_equal ['Rakefile'], Dir[ '*' ]
end
end

def test_dir_glob_on_uri_classloader_path_with_dot
assert_equal ['uri:classloader://./Rakefile'], Dir[ 'uri:classloader://./*' ]

ensure_cwd do
Dir.chdir( 'uri:classloader://.' )
assert_equal ['Rakefile'], Dir[ '*' ]
end

ensure_cwd do
JRuby.runtime.current_directory = 'uri:classloader://.'
assert_equal ['Rakefile'], Dir[ '*' ]
end
end

def test_dir_glob_on_uri_classloader_path_with_dot_dot
assert_equal ['uri:classloader://lib/../Rakefile'], Dir[ 'uri:classloader://lib/../*' ]
assert_equal ['uri:classloader://lib/./../Rakefile'], Dir[ 'uri:classloader://lib/./../*' ]

ensure_cwd do
JRuby.runtime.current_directory = 'uri:classloader://lib/..'
assert_equal ['Rakefile'], Dir[ '*' ]
end

ensure_cwd do
Dir.chdir 'uri:classloader://lib/..'
assert_equal ['Rakefile'], Dir[ '*' ]
end

ensure_cwd do
JRuby.runtime.current_directory = 'uri:classloader://'
assert_equal ['lib/../Rakefile'], Dir[ 'lib/../*' ]
end

ensure_cwd do
Dir.chdir 'uri:classloader://'
assert_equal ['lib/../Rakefile'], Dir[ 'lib/../*' ]
end
end
end

0 comments on commit ef39dd5

Please sign in to comment.