From 091a6ea8c1005f5bfc24839da3da0e37251b15d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 23 May 2024 12:01:11 +0200 Subject: [PATCH] [rubygems/rubygems] Move flock logic to its own method https://github.com/rubygems/rubygems/commit/91274128a8 --- lib/rubygems.rb | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/rubygems.rb b/lib/rubygems.rb index 0d089ddafb..c3cb1d5f12 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -791,23 +791,14 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} end ## - # Open a file with given flags, and on Windows protect access with flock + # Open a file with given flags. It requires special logic on Windows, like + # protecting access with flock def self.open_file(path, flags, &block) - File.open(path, flags) do |io| - if !java_platform? && win_platform? - begin - io.flock(File::LOCK_EX) - rescue Errno::ENOSYS, Errno::ENOTSUP - end - end - yield io - end - rescue Errno::ENOLCK # NFS - if Thread.main != Thread.current - raise + if !java_platform? && win_platform? + open_file_with_flock(path, flags, &block) else - File.open(path, flags, &block) + open_file_without_flock(path, flags, &block) end end @@ -1307,6 +1298,26 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} private + def open_file_with_flock(path, flags, &block) + File.open(path, flags) do |io| + begin + io.flock(File::LOCK_EX) + rescue Errno::ENOSYS, Errno::ENOTSUP + end + yield io + end + rescue Errno::ENOLCK # NFS + if Thread.main != Thread.current + raise + else + open_file_without_flock(path, flags, &block) + end + end + + def open_file_without_flock(path, flags, &block) + File.open(path, flags, &block) + end + def already_loaded?(file) $LOADED_FEATURES.any? do |feature_path| feature_path.end_with?(file) && default_gem_load_paths.any? {|load_path_entry| feature_path == "#{load_path_entry}/#{file}" }