From 9e84f6e0f32161f7259ae468621f2b360261c04a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Thu, 19 Oct 2023 11:37:23 +0200 Subject: [PATCH] Fix `FileUtils.ln_sf` to override special file types (#13896) --- spec/std/file_utils_spec.cr | 35 +++++++++++++++++++++++++++++++++++ src/file_utils.cr | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/spec/std/file_utils_spec.cr b/spec/std/file_utils_spec.cr index 448ff620208f..b90203b8b1a4 100644 --- a/spec/std/file_utils_spec.cr +++ b/spec/std/file_utils_spec.cr @@ -715,6 +715,41 @@ describe "FileUtils" do end end + {% if flag?(:unix) %} + it "overwrites a destination named pipe" do + with_tempfile("ln_sf_src", "ln_sf_dst_pipe_exists") do |path1, path2| + test_with_string_and_path(path1, path2) do |arg1, arg2| + FileUtils.touch([path1]) + `mkfifo #{path2}` + File.symlink?(path1).should be_false + File.symlink?(path2).should be_false + + FileUtils.ln_sf(arg1, arg2) + File.symlink?(path1).should be_false + File.symlink?(path2).should be_true + FileUtils.rm_rf([path1, path2]) + end + end + end + {% end %} + + it "overwrites a destination dir in dir" do + with_tempfile("ln_sf_src", "ln_sf_dst_dir_exists") do |path1, path2| + test_with_string_and_path(path1, path2) do |arg1, arg2| + FileUtils.touch([path1]) + dir_dest = File.join(path2, File.basename(path1)) + Dir.mkdir_p(dir_dest) + File.symlink?(path1).should be_false + File.symlink?(path2).should be_false + + FileUtils.ln_sf(arg1, arg2) + File.symlink?(path1).should be_false + File.symlink?(dir_dest).should be_true + FileUtils.rm_rf([path1, path2]) + end + end + end + it "overwrites a destination file inside a dir" do with_tempfile("ln_sf_dst_dir", "ln_sf_dst") do |dir, path2| dir += File::SEPARATOR diff --git a/src/file_utils.cr b/src/file_utils.cr index ad189ecd0b25..c5b1ced3c7c5 100644 --- a/src/file_utils.cr +++ b/src/file_utils.cr @@ -223,7 +223,7 @@ module FileUtils dest_path = File.join(dest_path, File.basename(src_path)) end - File.delete(dest_path) if File.file?(dest_path) + rm_rf(dest_path) if File.exists?(dest_path) File.symlink(src_path, dest_path) end