Another bug in fileutils.rb

From: Johan Holmberg <holmberg@...>
Date: 2004-05-04 16:36:40 UTC
List: ruby-core #2843
Hi !

Many methods in FileUtils take either an array of arguments or a
single scalar argument. To handle this, the internal method
'fu_list' naively uses the Kernel.Array method. For a string this
means (as far as I can understand):

  Kernel.Array --> Enumerable.to_a --> String.each

Considering that:

  - String.each returns the *records* (normally lines) of the
    string

  - On UNIX filenames are allowed to contain newlines

it is obvious that many FileUtils methods don't work as expected
on such weird filenames. For example: the call to

  FileUtils.rmdir "abc\ndef"

removed the two directories "abc\n" and "def", *not* the directory
"abc\ndef" as it is supposed to do.

Below is a patch that I think fixeshe problem, and an added
test to the test-suite that demonstrates the problem
(the diffs are compared with the latest in CVS).

I have made the test so it is not executed on Windows, since
newlines are not allowed in filenames there.

Is this a reasonable change ?

/Johan Holmberg

------------------------------------------------------------------------------
Index: lib/fileutils.rb
===================================================================
RCS file: /src/ruby/lib/fileutils.rb,v
retrieving revision 1.41
diff -u -r1.41 fileutils.rb
--- lib/fileutils.rb	7 Apr 2004 02:51:05 -0000	1.41
+++ lib/fileutils.rb	4 May 2004 15:48:19 -0000
@@ -717,7 +717,7 @@
   end

   def fu_list(arg)
-    Array(arg).map {|path| File.path(path) }
+    (arg.is_a?(Array) ? arg : [arg]).map {|path| File.path(path) }
   end

   def fu_each_src_dest(src, dest)
Index: test/fileutils/test_fileutils.rb
===================================================================
RCS file: /src/ruby/test/fileutils/test_fileutils.rb,v
retrieving revision 1.21
diff -u -r1.21 test_fileutils.rb
--- test/fileutils/test_fileutils.rb	2 May 2004 12:57:31 -0000	1.21
+++ test/fileutils/test_fileutils.rb	4 May 2004 15:48:20 -0000
@@ -537,6 +537,13 @@
       mkdir Pathname.new('tmp/tmpdirtmp')
       mkdir [Pathname.new('tmp/tmpdirtmp2'), Pathname.new('tmp/tmpdirtmp3')]
     }
+
+    if /mswin/ !~ RUBY_PLATFORM
+      # newlines not allowed in filenames on Windows, but it's ok on UNIX
+      mkdir "tmp-first-line\ntmp-second-line"
+      assert_directory "tmp-first-line\ntmp-second-line"
+      Dir.rmdir "tmp-first-line\ntmp-second-line"
+    end
   end

   def test_mkdir_p
------------------------------------------------------------------------------



In This Thread

Prev Next