mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
dir.c: relative path Dir base
* dir.c (glob_helper): fix globbing based on a relative path Dir. [Feature #13056] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59479 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
6c676acfa2
commit
1ef613dd0b
2 changed files with 10 additions and 5 deletions
13
dir.c
13
dir.c
|
@ -1896,6 +1896,9 @@ glob_helper(
|
|||
int plain = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0;
|
||||
int escape = !(flags & FNM_NOESCAPE);
|
||||
size_t pathlen = baselen + namelen;
|
||||
const char *base = path;
|
||||
|
||||
if (fd != AT_FDCWD && *(base += baselen) == '/') base++;
|
||||
|
||||
for (cur = beg; cur < end; ++cur) {
|
||||
struct glob_pattern *p = *cur;
|
||||
|
@ -1928,9 +1931,9 @@ glob_helper(
|
|||
}
|
||||
}
|
||||
|
||||
if (*path) {
|
||||
if (*base) {
|
||||
if (match_all && pathtype == path_unknown) {
|
||||
if (do_lstat(fd, path, &st, flags, enc) == 0) {
|
||||
if (do_lstat(fd, base, &st, flags, enc) == 0) {
|
||||
pathtype = IFTODT(st.st_mode);
|
||||
}
|
||||
else {
|
||||
|
@ -1938,7 +1941,7 @@ glob_helper(
|
|||
}
|
||||
}
|
||||
if (match_dir && pathtype == path_unknown) {
|
||||
if (do_stat(fd, path, &st, flags, enc) == 0) {
|
||||
if (do_stat(fd, base, &st, flags, enc) == 0) {
|
||||
pathtype = IFTODT(st.st_mode);
|
||||
}
|
||||
else {
|
||||
|
@ -1980,7 +1983,7 @@ glob_helper(
|
|||
# else
|
||||
;
|
||||
# endif
|
||||
dirp = do_opendir(fd, *path ? path : ".", flags, enc, funcs->error, arg, &status);
|
||||
dirp = do_opendir(fd, *base ? base : ".", flags, enc, funcs->error, arg, &status);
|
||||
if (dirp == NULL) {
|
||||
# if FNM_SYSCASE || NORMALIZE_UTF8PATH
|
||||
if ((magical < 2) && !recursive && (errno == EACCES)) {
|
||||
|
@ -1990,7 +1993,7 @@ glob_helper(
|
|||
# endif
|
||||
return status;
|
||||
}
|
||||
IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *path ? path : "."));
|
||||
IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *base ? base : "."));
|
||||
|
||||
# if NORMALIZE_UTF8PATH
|
||||
if (!(norm_p || magical || recursive)) {
|
||||
|
|
|
@ -207,6 +207,7 @@ class TestDir < Test::Unit::TestCase
|
|||
files.each {|n| File.write(File.join(@root, n), "")}
|
||||
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})
|
||||
end
|
||||
|
@ -215,6 +216,7 @@ class TestDir < Test::Unit::TestCase
|
|||
files = %w[a/foo.c c/bar.c]
|
||||
files.each {|n| File.write(File.join(@root, n), "")}
|
||||
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)}})
|
||||
end
|
||||
|
||||
def assert_entries(entries, children = false)
|
||||
|
|
Loading…
Reference in a new issue