Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 95b5c62bd3ad
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e3f2bd4e28a4
Choose a head ref
  • 4 commits
  • 6 files changed
  • 1 contributor

Commits on Jun 1, 2015

  1. keep mkdir consistent when using uri-like paths

    * there was a problem with file:////path/to/dir where the extra slashes
      did confuse the logic
    * all pathes starting with uri: can not create any directories
    
    fixes #2972
    
    Sponsored by Lookout Inc.
    
    Conflicts:
    	core/src/main/java/org/jruby/RubyDir.java
    	core/src/main/java/org/jruby/util/URLResource.java
    	test/test_dir.rb
    	test/test_file.rb
    mkristian committed Jun 1, 2015
    Copy the full SHA
    63d8376 View commit details
  2. Copy the full SHA
    8203fe7 View commit details
  3. no more emma.properties

    mkristian committed Jun 1, 2015
    Copy the full SHA
    99c8bca View commit details
  4. Copy the full SHA
    e3f2bd4 View commit details
1 change: 0 additions & 1 deletion core/src/main/java/emma.properties

This file was deleted.

30 changes: 13 additions & 17 deletions core/src/main/java/org/jruby/RubyDir.java
Original file line number Diff line number Diff line change
@@ -43,15 +43,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;
@@ -310,16 +312,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;
@@ -457,8 +451,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('\\', '/');

@@ -666,10 +662,10 @@ public IRubyObject fileno(ThreadContext context) {
* @param path path for which to return the <code>File</code> object.
* @param mustExist is true the directory must exist. If false it must not.
*/
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);
@@ -722,7 +718,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
@@ -125,6 +125,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);
10 changes: 5 additions & 5 deletions core/src/main/java/org/jruby/util/URLResource.java
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ public class URLResource extends AbstractFileResource {

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

private final String uri;

@@ -150,15 +150,15 @@ public Channel openChannel( ModeFlags flags, int perm ) throws ResourceException
}

public static FileResource create(ClassLoader cl, String pathname, boolean isFile) {
try
try
{
pathname = new URI(pathname.replaceFirst("^/*", "/")).normalize().getPath().replaceAll("^/([.][.]/)*", "");
} catch (URISyntaxException e) {
pathname = pathname.replaceAll("^[.]?/*", "");
}
URL url = cl.getResource(pathname);
String[] files = isFile ? null : listClassLoaderFiles(cl, pathname);
return new URLResource(URI_CLASSLOADER + pathname,
return new URLResource(URI_CLASSLOADER + "/" + pathname,
cl,
url == null ? null : pathname,
files);
@@ -296,8 +296,8 @@ private static String[] listFiles(String pathname) {

public static URL getResourceURL(Ruby runtime, String location)
{
if (location.startsWith(URI + CLASSLOADER)){
return runtime.getJRubyClassLoader().getResource(location.substring(URI_CLASSLOADER.length()));
if (location.startsWith(URI_CLASSLOADER)){
return runtime.getJRubyClassLoader().getResource(location.substring(URI_CLASSLOADER.length() + 1));
}
try
{
14 changes: 14 additions & 0 deletions test/jruby/test_dir.rb
Original file line number Diff line number Diff line change
@@ -149,6 +149,7 @@ def test_chdir_and_pwd
end

def test_glob_inside_jar_file
# the respective files get found via the classloader
jar_file = jar_file_with_spaces.sub(/.*!/, 'uri:classloader:')

["#{jar_file}/abc", "#{jar_file}/inside_jar.rb", "#{jar_file}/second_jar.rb"].each do |f|
@@ -219,6 +220,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/jruby/test_file.rb
Original file line number Diff line number Diff line change
@@ -278,6 +278,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"))