Skip to content

Commit e2d08d6

Browse files
committed
pr reviews
1 parent 4bad8de commit e2d08d6

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

lib/rubygems/package.rb

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,13 @@ def extract_tar_gz(io, destination_dir, pattern = "*") # :nodoc:
441441
full_name = entry.full_name
442442
next unless File.fnmatch pattern, full_name, File::FNM_DOTMATCH
443443

444-
destination = install_location full_name, destination_dir
445-
446-
if invalid_windows_filename?(full_name)
444+
if Gem.win_platform? && invalid_windows_filename?(full_name)
447445
gem_name = @spec ? @spec.full_name : "unknown"
448446
raise Gem::Package::InvalidWindowsFileNameError.new(full_name, gem_name)
449447
end
450448

449+
destination = install_location full_name, destination_dir
450+
451451
if entry.symlink?
452452
link_target = entry.header.linkname
453453
real_destination = link_target.start_with?("/") ? link_target : File.expand_path(link_target, File.dirname(destination))
@@ -471,18 +471,13 @@ def extract_tar_gz(io, destination_dir, pattern = "*") # :nodoc:
471471
end
472472

473473
if entry.file?
474-
begin
475-
File.open(destination, "wb") do |out|
476-
copy_stream(tar.io, out, entry.size)
477-
# Flush needs to happen before chmod because there could be data
478-
# in the IO buffer that needs to be written, and that could be
479-
# written after the chmod (on close) which would mess up the perms
480-
out.flush
481-
out.chmod file_mode(entry.header.mode) & ~File.umask
482-
end
483-
rescue Errno::EINVAL
484-
gem_name = @spec ? @spec.full_name : "unknown"
485-
raise Gem::Package::InvalidWindowsFileNameError.new(full_name, gem_name)
474+
File.open(destination, "wb") do |out|
475+
copy_stream(tar.io, out, entry.size)
476+
# Flush needs to happen before chmod because there could be data
477+
# in the IO buffer that needs to be written, and that could be
478+
# written after the chmod (on close) which would mess up the perms
479+
out.flush
480+
out.chmod file_mode(entry.header.mode) & ~File.umask
486481
end
487482
end
488483

@@ -562,10 +557,7 @@ def normalize_path(pathname) # :nodoc:
562557
# Note: Colons are only valid as drive letter separators (e.g., C:), not in filenames.
563558

564559
def invalid_windows_filename?(filename) # :nodoc:
565-
return false unless Gem.win_platform?
566-
567-
basename = File.basename(filename)
568-
basename.match?(/[:<>"|?*\\\x00-\x1f]/)
560+
filename.to_s.split("/").any? { |part| part.match?(/[:<>"|?*\\\x00-\x1f]/) }
569561
end
570562

571563
##

test/rubygems/test_gem_package.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,4 +1343,35 @@ def test_extract_tar_gz_invalid_filename
13431343
assert_match(/characters in its name that are not allowed on Windows/, e.message)
13441344
assert_match(/This is a problem with the 'a-2' gem, not Rubygems/, e.message)
13451345
end
1346+
1347+
def test_build_warns_on_invalid_windows_filename
1348+
pend "Windows filename validation only applies on non-Windows" if Gem.win_platform?
1349+
1350+
spec = Gem::Specification.new "test_gem", "1.0"
1351+
spec.summary = "test"
1352+
spec.authors = "test"
1353+
spec.files = ["lib/code.rb", "lib/file:name.rb"]
1354+
1355+
FileUtils.mkdir "lib"
1356+
1357+
File.open "lib/code.rb", "w" do |io|
1358+
io.write "# lib/code.rb"
1359+
end
1360+
1361+
File.open "lib/file:name.rb", "w" do |io|
1362+
io.write "# lib/file:name.rb"
1363+
end
1364+
1365+
package = Gem::Package.new spec.file_name
1366+
package.spec = spec
1367+
1368+
ui = Gem::MockGemUi.new
1369+
use_ui ui do
1370+
package.build
1371+
end
1372+
1373+
assert_match(%r{filename 'lib/file:name\.rb' contains characters that are invalid on Windows}, ui.error)
1374+
assert_match(/This gem may fail to install on Windows/, ui.error)
1375+
assert_path_exist spec.file_name
1376+
end
13461377
end

0 commit comments

Comments
 (0)