Skip to content

Commit 1239035

Browse files
committed
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.
1 parent 400574d commit 1239035

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

lib/fileutils.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,18 +1369,21 @@ def copy(dest)
13691369
end
13701370
when symlink?
13711371
File.symlink File.readlink(path()), dest
1372-
when chardev?
1373-
raise "cannot handle device file" unless File.respond_to?(:mknod)
1374-
mknod dest, ?c, 0666, lstat().rdev
1375-
when blockdev?
1376-
raise "cannot handle device file" unless File.respond_to?(:mknod)
1377-
mknod dest, ?b, 0666, lstat().rdev
1372+
when chardev?, blockdev?
1373+
raise "cannot handle device file"
13781374
when socket?
1379-
raise "cannot handle socket" unless File.respond_to?(:mknod)
1380-
mknod dest, nil, lstat().mode, 0
1375+
begin
1376+
require 'socket'
1377+
rescue LoadError
1378+
raise "cannot handle socket"
1379+
else
1380+
raise "cannot handle socket" unless defined?(UNIXServer)
1381+
end
1382+
UNIXServer.new(dest).close
1383+
File.chmod lstat().mode, dest
13811384
when pipe?
13821385
raise "cannot handle FIFO" unless File.respond_to?(:mkfifo)
1383-
mkfifo dest, 0666
1386+
File.mkfifo dest, lstat().mode
13841387
when door?
13851388
raise "cannot handle door: #{path()}"
13861389
else

test/fileutils/test_fileutils.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,34 @@ def test_cp_r_symlink_preserve
430430
}
431431
end if have_symlink? and !no_broken_symlink?
432432

433+
def test_cp_r_fifo
434+
Dir.mkdir('tmp/cpr_src')
435+
File.mkfifo 'tmp/cpr_src/fifo', 0600
436+
cp_r 'tmp/cpr_src', 'tmp/cpr_dest'
437+
assert_equal(true, File.pipe?('tmp/cpr_dest/fifo'))
438+
end if File.respond_to?(:mkfifo)
439+
440+
def test_cp_r_dev
441+
devs = Dir['/dev/*']
442+
chardev = Dir['/dev/*'].find{|f| File.chardev?(f)}
443+
blockdev = Dir['/dev/*'].find{|f| File.blockdev?(f)}
444+
Dir.mkdir('tmp/cpr_dest')
445+
assert_raise(RuntimeError) { cp_r chardev, 'tmp/cpr_dest/cd' }
446+
assert_raise(RuntimeError) { cp_r blockdev, 'tmp/cpr_dest/bd' }
447+
end
448+
449+
begin
450+
require 'socket'
451+
rescue LoadError
452+
else
453+
def test_cp_r_socket
454+
Dir.mkdir('tmp/cpr_src')
455+
UNIXServer.new('tmp/cpr_src/socket').close
456+
cp_r 'tmp/cpr_src', 'tmp/cpr_dest'
457+
assert_equal(true, File.socket?('tmp/cpr_dest/socket'))
458+
end if defined?(UNIXServer)
459+
end
460+
433461
def test_cp_r_pathname
434462
# pathname
435463
touch 'tmp/cprtmp'

0 commit comments

Comments
 (0)