diff options
| author | David RodrÃguez <deivid.rodriguez@riseup.net> | 2024-09-11 15:46:56 +0200 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-09-16 11:37:58 +0000 |
| commit | 7411caa103ccba8b9c16b9bd7aff96fc2b42f1fc (patch) | |
| tree | ebf6a72ced0f79b0f77255cbcd387e66f65c316d | |
| parent | 532af89e3b5b78dd3a6fe29c6cc64ad1b073afe2 (diff) | |
[rubygems/rubygems] Make sure implementations of `Gem.open_file_with_flock` match
https://github.com/rubygems/rubygems/commit/174a8e5284
| -rw-r--r-- | lib/bundler/rubygems_ext.rb | 13 | ||||
| -rw-r--r-- | lib/rubygems.rb | 9 |
2 files changed, 9 insertions, 13 deletions
diff --git a/lib/bundler/rubygems_ext.rb b/lib/bundler/rubygems_ext.rb index 481587df86..650395c53e 100644 --- a/lib/bundler/rubygems_ext.rb +++ b/lib/bundler/rubygems_ext.rb @@ -33,20 +33,17 @@ module Gem # Can be removed once RubyGems 3.5.14 support is dropped unless Gem.respond_to?(:open_file_with_flock) 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 io.flock(File::LOCK_EX) rescue Errno::ENOSYS, Errno::ENOTSUP + rescue Errno::ENOLCK # NFS + raise unless Thread.main == Thread.current end yield io - rescue Errno::ENOLCK # NFS - if Thread.main != Thread.current - raise - else - File.open(path, flags, &block) - end end end end diff --git a/lib/rubygems.rb b/lib/rubygems.rb index 9d40fcc2f7..ded0aa3b43 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -794,15 +794,14 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} File.open(path, flags, &block) 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 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 io.flock(File::LOCK_EX) rescue Errno::ENOSYS, Errno::ENOTSUP |
