[rubygems/rubygems] Make sure implementations of Gem.open_file_with_flock match

https://github.com/rubygems/rubygems/commit/174a8e5284
This commit is contained in:
David Rodríguez 2024-09-11 15:46:56 +02:00 committed by git
parent 532af89e3b
commit 7411caa103
2 changed files with 9 additions and 13 deletions

View File

@ -33,20 +33,17 @@ module Gem
# Can be removed once RubyGems 3.5.14 support is dropped # Can be removed once RubyGems 3.5.14 support is dropped
unless Gem.respond_to?(:open_file_with_flock) unless Gem.respond_to?(:open_file_with_flock)
def self.open_file_with_flock(path, &block) def self.open_file_with_flock(path, &block)
flags = File.exist?(path) ? "r+" : "a+" mode = IO::RDONLY | IO::APPEND | IO::CREAT | IO::BINARY
mode |= IO::SHARE_DELETE if IO.const_defined?(:SHARE_DELETE)
File.open(path, flags) do |io| File.open(path, mode) do |io|
begin begin
io.flock(File::LOCK_EX) io.flock(File::LOCK_EX)
rescue Errno::ENOSYS, Errno::ENOTSUP rescue Errno::ENOSYS, Errno::ENOTSUP
rescue Errno::ENOLCK # NFS
raise unless Thread.main == Thread.current
end end
yield io yield io
rescue Errno::ENOLCK # NFS
if Thread.main != Thread.current
raise
else
File.open(path, flags, &block)
end
end end
end end
end end

View File

@ -794,15 +794,14 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
File.open(path, flags, &block) File.open(path, flags, &block)
end end
mode = IO::RDONLY | IO::APPEND | IO::CREAT | IO::BINARY
mode |= IO::SHARE_DELETE if IO.const_defined?(:SHARE_DELETE)
MODE_TO_FLOCK = mode # :nodoc:
## ##
# Open a file with given flags, and protect access with flock # Open a file with given flags, and protect access with flock
def self.open_file_with_flock(path, &block) def self.open_file_with_flock(path, &block)
File.open(path, MODE_TO_FLOCK) do |io| mode = IO::RDONLY | IO::APPEND | IO::CREAT | IO::BINARY
mode |= IO::SHARE_DELETE if IO.const_defined?(:SHARE_DELETE)
File.open(path, mode) do |io|
begin begin
io.flock(File::LOCK_EX) io.flock(File::LOCK_EX)
rescue Errno::ENOSYS, Errno::ENOTSUP rescue Errno::ENOSYS, Errno::ENOTSUP