From c1fecc5eabba5bb22a3ebd5616fa50ad612ef4d9 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 12 Aug 2024 20:38:09 +0900 Subject: [PATCH] [rubygems/rubygems] Simplify `Gem.read_binary` and `Gem.write_binary` Since `Gem.open_file` no longer locks the target file and is same as `File.open` now, simply `Gem.read_binary` should read in binary mode. Also the body of `Gem.write_binary` is same as `File.binwrite`. https://github.com/rubygems/rubygems/commit/44df9045df --- lib/rubygems.rb | 8 ++------ test/rubygems/test_gem_installer.rb | 14 +++++++------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/rubygems.rb b/lib/rubygems.rb index c51ba69203..bd9f240e20 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -773,18 +773,14 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # Safely read a file in binary mode on all platforms. def self.read_binary(path) - open_file(path, "rb+", &:read) - rescue Errno::EACCES, Errno::EROFS - open_file(path, "rb", &:read) + File.binread(path) end ## # Safely write a file in binary mode on all platforms. def self.write_binary(path, data) - open_file(path, "wb") do |io| - io.write data - end + File.binwrite(path, data) rescue Errno::ENOSPC # If we ran out of space but the file exists, it's *guaranteed* to be corrupted. File.delete(path) if File.exist?(path) diff --git a/test/rubygems/test_gem_installer.rb b/test/rubygems/test_gem_installer.rb index 52498cdd6d..a61d1b6fff 100644 --- a/test/rubygems/test_gem_installer.rb +++ b/test/rubygems/test_gem_installer.rb @@ -2386,10 +2386,10 @@ end installer = Gem::Installer.for_spec @spec installer.gem_home = @gemhome - File.class_eval do - alias_method :original_write, :write + File.singleton_class.class_eval do + alias_method :original_binwrite, :binwrite - def write(data) + def binwrite(path, data) raise Errno::ENOSPC end end @@ -2400,10 +2400,10 @@ end assert_path_not_exist @spec.spec_file ensure - File.class_eval do - remove_method :write - alias_method :write, :original_write - remove_method :original_write + File.singleton_class.class_eval do + remove_method :binwrite + alias_method :binwrite, :original_binwrite + remove_method :original_binwrite end end