[前][次][番号順一覧][スレッド一覧]

ruby-changes:47363

From: nobu <ko1@a...>
Date: Thu, 3 Aug 2017 12:39:40 +0900 (JST)
Subject: [ruby-changes:47363] nobu:r59479 (trunk): dir.c: relative path Dir base

nobu	2017-08-03 12:39:33 +0900 (Thu, 03 Aug 2017)

  New Revision: 59479

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59479

  Log:
    dir.c: relative path Dir base
    
    * dir.c (glob_helper): fix globbing based on a relative path Dir.
      [Feature #13056]

  Modified files:
    trunk/dir.c
    trunk/test/ruby/test_dir.rb
Index: dir.c
===================================================================
--- dir.c	(revision 59478)
+++ dir.c	(revision 59479)
@@ -1896,6 +1896,9 @@ glob_helper( https://github.com/ruby/ruby/blob/trunk/dir.c#L1896
     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( https://github.com/ruby/ruby/blob/trunk/dir.c#L1931
 	}
     }
 
-    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( https://github.com/ruby/ruby/blob/trunk/dir.c#L1941
 	    }
 	}
 	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( https://github.com/ruby/ruby/blob/trunk/dir.c#L1983
 # 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( https://github.com/ruby/ruby/blob/trunk/dir.c#L1993
 # 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)) {
Index: test/ruby/test_dir.rb
===================================================================
--- test/ruby/test_dir.rb	(revision 59478)
+++ test/ruby/test_dir.rb	(revision 59479)
@@ -207,6 +207,7 @@ class TestDir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L207
     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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L216
     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)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]