1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

dir.c: fix directory glob

* dir.c (glob_helper): fix directory glob which resulted in lacking
  the first byte.  adjust the length of basename to be appended as
  well as removing the heading path, not the length of the joined
  path.  [ruby-dev:50588] [Bug #14899]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63909 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-07-09 01:20:20 +00:00
parent 0528838b37
commit 58f2e6bc94
2 changed files with 27 additions and 4 deletions

7
dir.c
View file

@ -2068,10 +2068,11 @@ glob_helper(
if (status) return status;
}
if (match_dir && pathtype == path_directory) {
const char *subpath = path + baselen + (baselen && path[baselen] == '/');
char *tmp = join_path(subpath, namelen, dirsep, "", 0);
int seplen = (baselen && path[baselen] == '/');
const char *subpath = path + baselen + seplen;
char *tmp = join_path(subpath, namelen - seplen, dirsep, "", 0);
if (!tmp) return -1;
status = glob_call_func(funcs->match, tmp + (baselen ? dirsep : 0), arg, enc);
status = glob_call_func(funcs->match, tmp, arg, enc);
GLOB_FREE(tmp);
if (status) return status;
}

View file

@ -11,11 +11,13 @@ class TestDir < Test::Unit::TestCase
$VERBOSE = nil
@root = File.realpath(Dir.mktmpdir('__test_dir__'))
@nodir = File.join(@root, "dummy")
@dirs = []
for i in "a".."z"
if i.ord % 2 == 0
FileUtils.touch(File.join(@root, i))
else
FileUtils.mkdir(File.join(@root, i))
@dirs << File.join(i, "")
end
end
end
@ -210,18 +212,38 @@ class TestDir < Test::Unit::TestCase
def test_glob_base
files = %w[a/foo.c c/bar.c]
files.each {|n| File.write(File.join(@root, n), "")}
Dir.mkdir(File.join(@root, "a/dir"))
dirs = @dirs + %w[a/dir/]
dirs.sort!
assert_equal(files, Dir.glob("*/*.c", base: @root).sort)
assert_equal(files, Dir.chdir(@root) {Dir.glob("*/*.c", base: ".").sort})
assert_equal(%w[foo.c], Dir.chdir(@root) {Dir.glob("*.c", base: "a").sort})
assert_equal(files, Dir.chdir(@root) {Dir.glob("*/*.c", base: "").sort})
assert_equal(files, Dir.chdir(@root) {Dir.glob("*/*.c", base: nil).sort})
assert_equal(@dirs, Dir.glob("*/", base: @root).sort)
assert_equal(@dirs, Dir.chdir(@root) {Dir.glob("*/", base: ".").sort})
assert_equal(%w[dir/], Dir.chdir(@root) {Dir.glob("*/", base: "a").sort})
assert_equal(@dirs, Dir.chdir(@root) {Dir.glob("*/", base: "").sort})
assert_equal(@dirs, Dir.chdir(@root) {Dir.glob("*/", base: nil).sort})
assert_equal(dirs, Dir.glob("**/*/", base: @root).sort)
assert_equal(dirs, Dir.chdir(@root) {Dir.glob("**/*/", base: ".").sort})
assert_equal(%w[dir/], Dir.chdir(@root) {Dir.glob("**/*/", base: "a").sort})
assert_equal(dirs, Dir.chdir(@root) {Dir.glob("**/*/", base: "").sort})
assert_equal(dirs, Dir.chdir(@root) {Dir.glob("**/*/", base: nil).sort})
end
def test_glob_base_dir
files = %w[a/foo.c c/bar.c]
files.each {|n| File.write(File.join(@root, n), "")}
Dir.mkdir(File.join(@root, "a/dir"))
dirs = @dirs + %w[a/dir/]
dirs.sort!
assert_equal(files, Dir.open(@root) {|d| Dir.glob("*/*.c", base: d)}.sort)
assert_equal(%w[foo.c], Dir.chdir(@root) {Dir.open("a") {|d| Dir.glob("*", base: d)}})
assert_equal(%w[foo.c], Dir.chdir(@root) {Dir.open("a") {|d| Dir.glob("*.c", base: d)}})
assert_equal(@dirs, Dir.open(@root) {|d| Dir.glob("*/", base: d).sort})
assert_equal(%w[dir/], Dir.chdir(@root) {Dir.open("a") {|d| Dir.glob("*/", base: d).sort}})
assert_equal(dirs, Dir.open(@root) {|d| Dir.glob("**/*/", base: d).sort})
assert_equal(%w[dir/], Dir.chdir(@root) {Dir.open("a") {|d| Dir.glob("**/*/", base: d).sort}})
end
def assert_entries(entries, children_only = false)