[rubygems/rubygems] Move flock logic to its own method

https://github.com/rubygems/rubygems/commit/91274128a8
This commit is contained in:
David Rodríguez 2024-05-23 12:01:11 +02:00 committed by git
parent f41a2c96c3
commit 091a6ea8c1

View File

@ -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
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}" }