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

ruby-changes:51697

From: nobu <ko1@a...>
Date: Mon, 9 Jul 2018 10:20:27 +0900 (JST)
Subject: [ruby-changes:51697] nobu:r63909 (trunk): dir.c: fix directory glob

nobu	2018-07-09 10:20:20 +0900 (Mon, 09 Jul 2018)

  New Revision: 63909

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

  Log:
    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]

  Modified files:
    trunk/dir.c
    trunk/test/ruby/test_dir.rb
Index: test/ruby/test_dir.rb
===================================================================
--- test/ruby/test_dir.rb	(revision 63908)
+++ test/ruby/test_dir.rb	(revision 63909)
@@ -11,11 +11,13 @@ class TestDir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L11
     $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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L212
   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)
Index: dir.c
===================================================================
--- dir.c	(revision 63908)
+++ dir.c	(revision 63909)
@@ -2068,10 +2068,11 @@ glob_helper( https://github.com/ruby/ruby/blob/trunk/dir.c#L2068
 	    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;
 	}

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

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