[rubygems/rubygems] Protect binstub access during creation with a flock

https://github.com/rubygems/rubygems/commit/88e3f1d23c
This commit is contained in:
David Rodríguez 2024-06-26 16:47:06 +02:00 коммит произвёл git
Родитель 091a6ea8c1
Коммит 5c826ebea5
2 изменённых файлов: 20 добавлений и 17 удалений

Просмотреть файл

@ -802,6 +802,25 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
end
end
##
# Open a file with given flags, and protect access with flock
def self.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
##
# The path to the running Ruby interpreter.
@ -1298,22 +1317,6 @@ 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

Просмотреть файл

@ -222,7 +222,7 @@ class Gem::Installer
ruby_executable = false
existing = nil
File.open generated_bin, "rb" do |io|
Gem.open_file_with_flock generated_bin, "rb+" do |io|
line = io.gets
shebang = /^#!.*ruby/o