[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
This commit is contained in:
Nobuyoshi Nakada 2024-08-12 20:38:09 +09:00 committed by git
parent 1f00f6a09e
commit c1fecc5eab
2 changed files with 9 additions and 13 deletions

View File

@ -773,18 +773,14 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
# Safely read a file in binary mode on all platforms. # Safely read a file in binary mode on all platforms.
def self.read_binary(path) def self.read_binary(path)
open_file(path, "rb+", &:read) File.binread(path)
rescue Errno::EACCES, Errno::EROFS
open_file(path, "rb", &:read)
end end
## ##
# Safely write a file in binary mode on all platforms. # Safely write a file in binary mode on all platforms.
def self.write_binary(path, data) def self.write_binary(path, data)
open_file(path, "wb") do |io| File.binwrite(path, data)
io.write data
end
rescue Errno::ENOSPC rescue Errno::ENOSPC
# If we ran out of space but the file exists, it's *guaranteed* to be corrupted. # If we ran out of space but the file exists, it's *guaranteed* to be corrupted.
File.delete(path) if File.exist?(path) File.delete(path) if File.exist?(path)

View File

@ -2386,10 +2386,10 @@ end
installer = Gem::Installer.for_spec @spec installer = Gem::Installer.for_spec @spec
installer.gem_home = @gemhome installer.gem_home = @gemhome
File.class_eval do File.singleton_class.class_eval do
alias_method :original_write, :write alias_method :original_binwrite, :binwrite
def write(data) def binwrite(path, data)
raise Errno::ENOSPC raise Errno::ENOSPC
end end
end end
@ -2400,10 +2400,10 @@ end
assert_path_not_exist @spec.spec_file assert_path_not_exist @spec.spec_file
ensure ensure
File.class_eval do File.singleton_class.class_eval do
remove_method :write remove_method :binwrite
alias_method :write, :original_write alias_method :binwrite, :original_binwrite
remove_method :original_write remove_method :original_binwrite
end end
end end