Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make copy methods handle FIFOs and UNIX sockets #36

Merged
merged 2 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/fileutils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1369,18 +1369,21 @@ def copy(dest)
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
Expand Down
29 changes: 29 additions & 0 deletions test/fileutils/test_fileutils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,35 @@ def test_cp_r_symlink_preserve
}
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
skip "Skipping socket test on JRuby" if RUBY_ENGINE == 'jruby'
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'
Expand Down