Skip to content

Commit

Permalink
Showing 5 changed files with 44 additions and 20 deletions.
32 changes: 15 additions & 17 deletions core/src/main/java/org/jruby/RubyDir.java
Original file line number Diff line number Diff line change
@@ -42,15 +42,17 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import jnr.posix.FileStat;

import jnr.posix.FileStat;
import jnr.posix.POSIX;

import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyClass;

import jnr.posix.util.Platform;

import org.jcodings.Encoding;
import org.jcodings.specific.UTF8Encoding;

import org.jruby.exceptions.RaiseException;
import org.jruby.javasupport.JavaUtil;
import org.jruby.runtime.Block;
@@ -63,7 +65,9 @@
import org.jruby.util.FileResource;
import org.jruby.util.JRubyFile;
import org.jruby.util.ByteList;

import static org.jruby.CompatVersion.*;

import org.jruby.util.StringSupport;

/**
@@ -323,16 +327,8 @@ public static IRubyObject chdir(ThreadContext context, IRubyObject recv, IRubyOb
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();
}
FileResource dir = getDir(runtime, adjustedPath, true);
realPath = dir.canonicalPath();
}

IRubyObject result = null;
@@ -461,8 +457,10 @@ public static IRubyObject mkdir19(ThreadContext context, IRubyObject recv, IRuby
}

private static IRubyObject mkdirCommon(Ruby runtime, String path, IRubyObject[] args) {
File newDir = getDir(runtime, path, false);

if (path.startsWith("uri:")) {
throw runtime.newErrnoEACCESError(path);
}
File newDir = getDir(runtime, path, false).hackyGetJRubyFile();

String name = path.replace('\\', '/');

@@ -642,10 +640,10 @@ public static IRubyObject exist(ThreadContext context, IRubyObject recv, IRubyOb
* @param mustExist is true the directory must exist. If false it must not.
* @throws java.io.IOError if <code>path</code> is not a directory.
*/
protected static JRubyFile getDir(final Ruby runtime, final String path, final boolean mustExist) {
protected static FileResource getDir(final Ruby runtime, final String path, final boolean mustExist) {
String dir = dirFromPath(path, runtime);

JRubyFile result = JRubyFile.create(runtime.getCurrentDirectory(), dir);
FileResource result = JRubyFile.createResource(runtime, dir);

if (mustExist && !result.exists()) {
throw runtime.newErrnoENOENTError(dir);
@@ -698,7 +696,7 @@ private static String dirFromPath(final String path, final Ruby runtime) throws
String dir = path;
String[] pathParts = RubyFile.splitURI(path);
if (pathParts != null) {
if (pathParts[0].equals("file:") && pathParts[1].length() > 0 && pathParts[1].indexOf("!/") == -1) {
if (pathParts[0].startsWith("file:") && pathParts[1].length() > 0 && pathParts[1].indexOf("!/") == -1) {
dir = pathParts[1];
} else {
throw runtime.newErrnoENOTDIRError(dir);
6 changes: 6 additions & 0 deletions core/src/main/java/org/jruby/util/JRubyFile.java
Original file line number Diff line number Diff line change
@@ -114,6 +114,12 @@ private static JRubyFile createNoUnicodeConversion(String cwd, String pathname)
if (pathname == null || pathname.equals("") || Ruby.isSecurityRestricted()) {
return JRubyNonExistentFile.NOT_EXIST;
}
if(pathname.startsWith("file:")) {
pathname = pathname.substring(5);
}
if(pathname.startsWith("uri:")) {
return new JRubyFile(pathname);
}
File internal = new JavaSecuredFile(pathname);
if(cwd != null && cwd.startsWith("uri:") && !pathname.startsWith("uri:") && !pathname.contains("!/") && !internal.isAbsolute()) {
return new JRubyFile(cwd + "/" + pathname);
4 changes: 1 addition & 3 deletions core/src/main/java/org/jruby/util/URLResource.java
Original file line number Diff line number Diff line change
@@ -18,14 +18,12 @@

import org.jruby.Ruby;
import org.jruby.util.io.ChannelDescriptor;
import org.jruby.Ruby;
import org.jruby.exceptions.RaiseException;
import org.jruby.util.io.ModeFlags;

public class URLResource extends AbstractFileResource {

public static String URI = "uri:";
public static String CLASSLOADER = "classloader:/";
public static String CLASSLOADER = "classloader:";
public static String URI_CLASSLOADER = URI + CLASSLOADER;

private final String uri;
13 changes: 13 additions & 0 deletions test/test_dir.rb
Original file line number Diff line number Diff line change
@@ -218,6 +218,19 @@ def xxx_test_mktmpdir
end
end

# GH-2972
def test_mkdir_within_classloader
assert_raise(Errno::EACCES) do
Dir.mkdir 'uri:classloader://new_dir'
end
assert_raise(Errno::EACCES) do
FileUtils.mkdir 'uri:classloader://new_dir'
end
assert_raise(Errno::EACCES) do
FileUtils.mkdir_p 'uri:classloader://new_dir'
end
end

# JRUBY-4983
def test_entries_unicode
utf8_dir = "testDir_1/glk\u00a9"
9 changes: 9 additions & 0 deletions test/test_file.rb
Original file line number Diff line number Diff line change
@@ -279,6 +279,15 @@ def test_mkdir_with_file_uri_works_as_expected
FileUtils.rm_rf("test_mkdir_with_file_uri_works_as_expected")
end

# GH-2972
def test_mkdir_with_file_uri_and_absolute_path_works_as_expected
# the extra slashes were the problem
FileUtils.mkdir("file://#{Dir.pwd}/test_mkdir_with_file_uri_works_as_expected")
assert File.directory?("#{Dir.pwd}/test_mkdir_with_file_uri_works_as_expected")
ensure
FileUtils.rm_rf("#{Dir.pwd}/test_mkdir_with_file_uri_works_as_expected")
end

def test_expand_path_corner_case
# this would fail on MRI 1.8.6 (MRI returns "/foo").
assert_equal("//foo", File.expand_path("../foo", "//bar"))

0 comments on commit 0137667

Please sign in to comment.