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

ruby-changes:57974

From: Jeremy <ko1@a...>
Date: Fri, 27 Sep 2019 13:05:55 +0900 (JST)
Subject: [ruby-changes:57974] 1d99163aa5 (master): [ruby/fileutils] Make copy methods handle FIFOs and UNIX sockets

https://git.ruby-lang.org/ruby.git/commit/?id=1d99163aa5

From 1d99163aa59b637f1c14287135f26480df447e49 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Tue, 9 Jul 2019 20:41:51 -0700
Subject: [ruby/fileutils] Make copy methods handle FIFOs and UNIX sockets

Previously, this was broken.  Trying to copy a FIFO would raise a
NoMethodError if File.mkfifo was defined.  Trying to copy a UNIX
socket would raise a RuntimeError as File.mknod is not something
Ruby defines.

Handle the FIFO issue using File.mkfifo instead of mkfifo.

Handle the UNIX Socket issue by creating a unix socket.

Continue to not support character or block devices, raising a
RuntimeError for both.

Add tests for FIFO, UNIX Socket, and character/block devices.

https://github.com/ruby/fileutils/commit/123903532d

diff --git a/lib/fileutils.rb b/lib/fileutils.rb
index ee903e2..bc4fd70 100644
--- a/lib/fileutils.rb
+++ b/lib/fileutils.rb
@@ -1383,18 +1383,21 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L1383
         end
       when symlink?
         File.symlink File.readlink(path()), dest
-      when chardev?
-        raise "cannot handle device file" unless File.respond_to?(:mknod)
-        mknod dest, ?c, 0666, lstat().rdev
-      when blockdev?
-        raise "cannot handle device file" unless File.respond_to?(:mknod)
-        mknod dest, ?b, 0666, lstat().rdev
+      when chardev?, blockdev?
+        raise "cannot handle device file"
       when socket?
-        raise "cannot handle socket" unless File.respond_to?(:mknod)
-        mknod dest, nil, lstat().mode, 0
+        begin
+          require 'socket'
+        rescue LoadError
+          raise "cannot handle socket"
+        else
+          raise "cannot handle socket" unless defined?(UNIXServer)
+        end
+        UNIXServer.new(dest).close
+        File.chmod lstat().mode, dest
       when pipe?
         raise "cannot handle FIFO" unless File.respond_to?(:mkfifo)
-        mkfifo dest, 0666
+        File.mkfifo dest, lstat().mode
       when door?
         raise "cannot handle door: #{path()}"
       else
diff --git a/test/fileutils/test_fileutils.rb b/test/fileutils/test_fileutils.rb
index 18a4bed..663c81e 100644
--- a/test/fileutils/test_fileutils.rb
+++ b/test/fileutils/test_fileutils.rb
@@ -440,6 +440,34 @@ class TestFileUtils < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L440
     }
   end if have_symlink? and !no_broken_symlink?
 
+  def test_cp_r_fifo
+    Dir.mkdir('tmp/cpr_src')
+    File.mkfifo 'tmp/cpr_src/fifo', 0600
+    cp_r 'tmp/cpr_src', 'tmp/cpr_dest'
+    assert_equal(true, File.pipe?('tmp/cpr_dest/fifo'))
+  end if File.respond_to?(:mkfifo)
+
+  def test_cp_r_dev
+    devs = Dir['/dev/*']
+    chardev = Dir['/dev/*'].find{|f| File.chardev?(f)}
+    blockdev = Dir['/dev/*'].find{|f| File.blockdev?(f)}
+    Dir.mkdir('tmp/cpr_dest')
+    assert_raise(RuntimeError) { cp_r chardev, 'tmp/cpr_dest/cd' }
+    assert_raise(RuntimeError) { cp_r blockdev, 'tmp/cpr_dest/bd' }
+  end
+
+  begin
+    require 'socket'
+  rescue LoadError
+  else
+    def test_cp_r_socket
+      Dir.mkdir('tmp/cpr_src')
+      UNIXServer.new('tmp/cpr_src/socket').close
+      cp_r 'tmp/cpr_src', 'tmp/cpr_dest'
+      assert_equal(true, File.socket?('tmp/cpr_dest/socket'))
+    end if defined?(UNIXServer)
+  end
+
   def test_cp_r_pathname
     # pathname
     touch 'tmp/cprtmp'
-- 
cgit v0.10.2


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

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