Skip to content

Commit bffb9c9

Browse files
znzhsbt
authored andcommitted
Fix remove_entry error when path encoding is not compatible UTF-8
1 parent c41b01a commit bffb9c9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/fileutils.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,11 @@ def door?
12891289

12901290
def entries
12911291
opts = {}
1292-
opts[:encoding] = fu_windows? ? ::Encoding::UTF_8 : path.encoding
1292+
if fu_windows? && ::Encoding.compatible?(::Encoding::UTF_8, path.encoding)
1293+
opts[:encoding] = ::Encoding::UTF_8
1294+
else
1295+
opts[:encoding] = path.encoding
1296+
end
12931297

12941298
files = if Dir.respond_to?(:children)
12951299
Dir.children(path, **opts)

test/fileutils/test_fileutils.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,23 @@ def test_remove_entry_cjk_path
756756
assert_file_not_exist dir
757757
end
758758

759+
def test_remove_entry_multibyte_path
760+
c = "\u00a7"
761+
begin
762+
c = c.encode('filesystem')
763+
rescue EncodingError
764+
c = c.b
765+
end
766+
dir = "tmpdir#{c}"
767+
my_rm_rf dir
768+
769+
Dir.mkdir dir
770+
File.write("#{dir}/#{c}.txt", "test_remove_entry_multibyte_path")
771+
772+
remove_entry dir
773+
assert_file_not_exist dir
774+
end
775+
759776
def test_remove_entry_secure
760777
check_singleton :remove_entry_secure
761778

0 commit comments

Comments
 (0)