From 7411caa103ccba8b9c16b9bd7aff96fc2b42f1fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Wed, 11 Sep 2024 15:46:56 +0200 Subject: [PATCH] [rubygems/rubygems] Make sure implementations of `Gem.open_file_with_flock` match https://github.com/rubygems/rubygems/commit/174a8e5284 --- lib/bundler/rubygems_ext.rb | 13 +++++-------- 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