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: 08f348c930cd
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: bbd0440f8039
Choose a head ref
  • 5 commits
  • 2 files changed
  • 2 contributors

Commits on Jan 10, 2017

  1. Copy the full SHA
    c81c89a View commit details
  2. [#4353] Renamed few variables in glob_helper

    Sumit M committed Jan 10, 2017
    Copy the full SHA
    53593e8 View commit details
  3. [#4353] added test for Dir.glob consecutive double stars should retur…

    …n uniq results
    Sumit M committed Jan 10, 2017
    Copy the full SHA
    b8edd91 View commit details

Commits on Jan 11, 2017

  1. [#4353] Cleaning up dirs created during test

    Sumit M committed Jan 11, 2017
    Copy the full SHA
    c59de88 View commit details
  2. Merge pull request #4428 from sumitmah/4353-fix-dir-glob

    [#4353] Ignore consecutive DOUBLE_STAR  in glob_helper.
    enebo authored Jan 11, 2017
    Copy the full SHA
    bbd0440 View commit details
Showing with 44 additions and 48 deletions.
  1. +35 −48 core/src/main/java/org/jruby/util/Dir.java
  2. +9 −0 test/jruby/test_dir.rb
83 changes: 35 additions & 48 deletions core/src/main/java/org/jruby/util/Dir.java
Original file line number Diff line number Diff line change
@@ -662,37 +662,8 @@ private static int addToResultIfExists(Ruby runtime, String cwd, byte[] bytes, i
if (cwd == null) cwd = "C:";
cwd = cwd + "/";
}

FileResource file = JRubyFile.createResource(runtime, cwd, fileName);

if (file.exists()) {
boolean trailingSlash = bytes[end - 1] == '/';

// On case-insenstive file systems any case string will 'exists',
// but what does it display as if you ls/dir it?
/* No idea what this is doing =/
if ((flags & FNM_CASEFOLD) != 0 && !isSpecialFile(fileName)) {
try {
String realName = file.getCanonicalFile().getName();
// TODO: This is only being done to the name of the file,
// but it should do for all parent directories too...
// TODO: OMGZ is this ugly
int fileNameLength = fileName.length();
int newEnd = fileNameLength <= 1 ? -1 : fileName.lastIndexOf('/', fileNameLength - 2);
if (newEnd != -1) {
realName = fileName.substring(0, newEnd + 1) + realName;
}
// It came in with a trailing slash preserve that in new name.
if (trailingSlash) realName = realName + "/";
bytes = realName.getBytes();
begin = 0;
end = bytes.length;
} catch (Exception e) {} // Failure will just use what we pass in
}*/

return func.call(bytes, begin, end - begin, arg);
}

@@ -710,9 +681,9 @@ private static int glob_helper(Ruby runtime, String cwd,
final int flags, GlobFunc<GlobArgs> func, GlobArgs arg) {
int status = 0;

int p = sub != -1 ? sub : begin;
int ptr = sub != -1 ? sub : begin;

if ( ! has_magic(path, p, end, flags) ) {
if ( ! has_magic(path, ptr, end, flags) ) {
if ( DOSISH || (flags & FNM_NOESCAPE) == 0 ) {
if ( sub != -1 ) { // can modify path (our internal buf[])
end = remove_backslashes(path, sub, end);
@@ -726,7 +697,7 @@ private static int glob_helper(Ruby runtime, String cwd,
}
}

if ( (end - begin) > 0 ) {
if (end > begin) {
if ( isAbsolutePath(path, begin, end) ) {
status = addToResultIfExists(runtime, null, path, begin, end, flags, func, arg);
} else {
@@ -740,27 +711,42 @@ private static int glob_helper(Ruby runtime, String cwd,
final ArrayList<DirGlobber> links = new ArrayList<DirGlobber>();

ByteList buf = new ByteList(20); FileResource resource;
mainLoop: while(p != -1 && status == 0) {
if ( path[p] == '/' ) p++;

final int s = indexOf(path, p, end, (byte) '/');
if ( has_magic(path, p, s == -1 ? end : s, flags) ) {
mainLoop: while(ptr != -1 && status == 0) {
if ( path[ptr] == '/' ) ptr++;

final int SLASH_INDEX = indexOf(path, ptr, end, (byte) '/');
if ( has_magic(path, ptr, SLASH_INDEX == -1 ? end : SLASH_INDEX, flags) ) {
finalize: do {
byte[] base = extract_path(path, begin, p);
byte[] dir = begin == p ? new byte[] { '.' } : base;
byte[] magic = extract_elem(path, p, end);
byte[] base = extract_path(path, begin, ptr);
byte[] dir = begin == ptr ? new byte[] { '.' } : base;
byte[] magic = extract_elem(path, ptr, end);
boolean recursive = false;

resource = JRubyFile.createResource(runtime, cwd, newStringFromUTF8(dir, 0, dir.length));

if ( resource.isDirectory() ) {
if ( s != -1 && Arrays.equals(magic, DOUBLE_STAR) ) {
final int n = base.length;
if ( SLASH_INDEX != -1 && Arrays.equals(magic, DOUBLE_STAR) ) {
final int lengthOfBase = base.length;
recursive = true;
buf.length(0);
buf.append(base);
buf.append(path, (n > 0 ? s : s + 1), end - (n > 0 ? s : s + 1));
status = glob_helper(runtime, cwd, buf, n, flags, func, arg);
int nextStartIndex;
int indexOfSlash = SLASH_INDEX;
do {
nextStartIndex = indexOfSlash + 1;
indexOfSlash = indexOf(path, nextStartIndex, end, (byte) '/');
magic = extract_elem(path, nextStartIndex, end);
} while(Arrays.equals(magic, DOUBLE_STAR) && indexOfSlash != -1);

int remainingPathStartIndex;
if(Arrays.equals(magic, DOUBLE_STAR)) {
remainingPathStartIndex = nextStartIndex;
} else {
remainingPathStartIndex = nextStartIndex - 1;
}
remainingPathStartIndex = lengthOfBase > 0 ? remainingPathStartIndex : remainingPathStartIndex + 1;
buf.append(path, remainingPathStartIndex, end - remainingPathStartIndex);
status = glob_helper(runtime, cwd, buf, lengthOfBase, flags, func, arg);
if ( status != 0 ) break finalize;
}
} else {
@@ -785,7 +771,7 @@ private static int glob_helper(Ruby runtime, String cwd,
final int len = buf.getRealSize();
buf.append(SLASH);
buf.append(DOUBLE_STAR);
buf.append(path, s, end - s);
buf.append(path, SLASH_INDEX, end - SLASH_INDEX);
status = glob_helper(runtime, cwd, buf, buf.getBegin() + len, flags, func, arg);
if ( status != 0 ) break;
}
@@ -796,7 +782,7 @@ private static int glob_helper(Ruby runtime, String cwd,
buf.append(base);
buf.append( isRoot(base) ? EMPTY : SLASH );
buf.append( getBytesInUTF8(file) );
if ( s == -1 ) {
if ( SLASH_INDEX == -1 ) {
status = func.call(buf.getUnsafeBytes(), 0, buf.getRealSize(), arg);
if ( status != 0 ) break;
continue;
@@ -816,16 +802,17 @@ private static int glob_helper(Ruby runtime, String cwd,
final int len = link.getRealSize();
buf.length(0);
buf.append(link);
buf.append(path, s, end - s);
buf.append(path, SLASH_INDEX, end - SLASH_INDEX);
status = glob_helper(runtime, cwd, buf, buf.getBegin() + len, flags, func, arg);
}
}
}
break mainLoop;
}
}
p = s;
ptr = SLASH_INDEX;
}

return status;
}

9 changes: 9 additions & 0 deletions test/jruby/test_dir.rb
Original file line number Diff line number Diff line change
@@ -101,6 +101,15 @@ def test_glob_double_star
Dir.glob('./testDir_2/**/testDir_tmp1').each {|f| assert File.exist?(f) }
end

def test_glob_consecutive_double_star_returns_uniq_results
Dir.mkdir("testDir_bug4353")
Dir.mkdir("testDir_bug4353/level2")
open("testDir_bug4353/level2/testDir_tmp1", "w").close
assert_equal(Dir.glob('./testDir_bug4353/**/**/testDir_tmp1'), ['./testDir_bug4353/level2/testDir_tmp1'])

FileUtils.rm_r 'testDir_bug4353'
end

def test_glob_with_blocks
Dir.mkdir("testDir_3")
open("testDir_3/testDir_tmp1", "w").close