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

ruby-changes:28995

From: ayumin <ko1@a...>
Date: Mon, 3 Jun 2013 23:20:25 +0900 (JST)
Subject: [ruby-changes:28995] ayumin:r41047 (trunk): * lib/fileutils.rb: fix behavior when mkdir/mkdir_p accepted "/".

ayumin	2013-06-03 23:20:15 +0900 (Mon, 03 Jun 2013)

  New Revision: 41047

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41047

  Log:
    * lib/fileutils.rb: fix behavior when mkdir/mkdir_p accepted "/".
    * test/fileutils/test_fileutils.rb: add test for above change.

  Modified files:
    trunk/ChangeLog
    trunk/lib/fileutils.rb
    trunk/test/fileutils/test_fileutils.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41046)
+++ ChangeLog	(revision 41047)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jun  3 23:11:19 2013  Ayumu AIZAWA  <ayumu.aizawa@g...>
+
+	* lib/fileutils.rb: fix behavior when mkdir/mkdir_p accepted "/".
+	* test/fileutils/test_fileutils.rb: add test for above change.
+	  Patched by Mitsunori Komatsu. [GH-319]
+
 Mon Jun  3 19:02:20 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* dir.c (is_hfs): use the file descriptor instead of a path.
Index: lib/fileutils.rb
===================================================================
--- lib/fileutils.rb	(revision 41046)
+++ lib/fileutils.rb	(revision 41047)
@@ -154,6 +154,11 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L154
   end
   module_function :uptodate?
 
+  def remove_tailing_slash(dir)
+    dir == '/' ? dir : dir.chomp(?/)
+  end
+  private_module_function :remove_tailing_slash
+
   #
   # Options: mode noop verbose
   #
@@ -200,7 +205,7 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L205
     fu_output_message "mkdir -p #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
     return *list if options[:noop]
 
-    list.map {|path| path.chomp(?/) }.each do |path|
+    list.map {|path| remove_tailing_slash(path)}.each do |path|
       # optimize for the most common case
       begin
         fu_mkdir path, options[:mode]
@@ -237,7 +242,7 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L242
   OPT_TABLE['makedirs'] = [:mode, :noop, :verbose]
 
   def fu_mkdir(path, mode)   #:nodoc:
-    path = path.chomp(?/)
+    path = remove_tailing_slash(path)
     if mode
       Dir.mkdir path, mode
       File.chmod mode, path
Index: test/fileutils/test_fileutils.rb
===================================================================
--- test/fileutils/test_fileutils.rb	(revision 41046)
+++ test/fileutils/test_fileutils.rb	(revision 41047)
@@ -758,6 +758,10 @@ class TestFileUtils https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L758
     assert_directory 'tmp/tmp'
     assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
     Dir.rmdir 'tmp/tmp'
+
+    assert_raise(Errno::EISDIR) {
+      mkdir '/'
+    }
   end
 
   def test_mkdir_file_perm
@@ -831,6 +835,8 @@ class TestFileUtils https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L835
     # (rm(1) try to chdir to parent directory, it fails to remove directory.)
     Dir.rmdir 'tmp/tmp'
     Dir.rmdir 'tmp'
+
+    mkdir_p '/'
   end
 
   def test_mkdir_p_file_perm

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

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