Merge pull request #20232 from Homebrew/pathname_instance_variables

Improve some `Pathname` instance variable handling
This commit is contained in:
Mike McQuaid 2025-07-11 08:21:24 +00:00 committed by GitHub
commit 3e0b0c1b32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 48 additions and 16 deletions

View File

@ -449,7 +449,7 @@ class Pathname
# This is why monkeypatching is non-ideal (but right solution to get # This is why monkeypatching is non-ideal (but right solution to get
# Ruby 3.3 over the line). # Ruby 3.3 over the line).
odisabled "rmtree", "FileUtils#rm_r" odisabled "rmtree", "FileUtils#rm_r"
FileUtils.rm_r(@path, noop:, verbose:, secure:) FileUtils.rm_r(T.must(@path), noop:, verbose:, secure:)
nil nil
end end
end end

View File

@ -44,6 +44,19 @@ module ELFShim
requires_ancestor { Pathname } requires_ancestor { Pathname }
def initialize(*args)
@elf = T.let(nil, T.nilable(T::Boolean))
@arch = T.let(nil, T.nilable(Symbol))
@elf_type = T.let(nil, T.nilable(Symbol))
@rpath = T.let(nil, T.nilable(String))
@interpreter = T.let(nil, T.nilable(String))
@dynamic_elf = T.let(nil, T.nilable(T::Boolean))
@metadata = T.let(nil, T.nilable(Metadata))
@patchelf_patcher = nil
super
end
def read_uint8(offset) def read_uint8(offset)
read(1, offset).unpack1("C") read(1, offset).unpack1("C")
end end
@ -52,8 +65,10 @@ module ELFShim
read(2, offset).unpack1("v") read(2, offset).unpack1("v")
end end
sig { returns(T::Boolean) }
def elf? def elf?
return @elf if defined? @elf return @elf unless @elf.nil?
return @elf = false if read(MAGIC_NUMBER_ASCII.size, MAGIC_NUMBER_OFFSET) != MAGIC_NUMBER_ASCII return @elf = false if read(MAGIC_NUMBER_ASCII.size, MAGIC_NUMBER_OFFSET) != MAGIC_NUMBER_ASCII
# Check that this ELF file is for Linux or System V. # Check that this ELF file is for Linux or System V.
@ -61,6 +76,7 @@ module ELFShim
@elf = [OS_ABI_LINUX, OS_ABI_SYSTEM_V].include? read_uint8(OS_ABI_OFFSET) @elf = [OS_ABI_LINUX, OS_ABI_SYSTEM_V].include? read_uint8(OS_ABI_OFFSET)
end end
sig { returns(Symbol) }
def arch def arch
return :dunno unless elf? return :dunno unless elf?
@ -84,6 +100,7 @@ module ELFShim
wanted_arch == arch wanted_arch == arch
end end
sig { returns(Symbol) }
def elf_type def elf_type
return :dunno unless elf? return :dunno unless elf?
@ -104,10 +121,9 @@ module ELFShim
# The runtime search path, such as: # The runtime search path, such as:
# "/lib:/usr/lib:/usr/local/lib" # "/lib:/usr/lib:/usr/local/lib"
sig { returns(T.nilable(String)) }
def rpath def rpath
return @rpath if defined? @rpath @rpath ||= rpath_using_patchelf_rb
@rpath = rpath_using_patchelf_rb
end end
# An array of runtime search path entries, such as: # An array of runtime search path entries, such as:
@ -116,10 +132,9 @@ module ELFShim
Array(rpath&.split(":")) Array(rpath&.split(":"))
end end
sig { returns(T.nilable(String)) }
def interpreter def interpreter
return @interpreter if defined? @interpreter @interpreter ||= patchelf_patcher.interpreter
@interpreter = patchelf_patcher.interpreter
end end
def patch!(interpreter: nil, rpath: nil) def patch!(interpreter: nil, rpath: nil)
@ -128,23 +143,31 @@ module ELFShim
save_using_patchelf_rb interpreter, rpath save_using_patchelf_rb interpreter, rpath
end end
sig { returns(T::Boolean) }
def dynamic_elf? def dynamic_elf?
return @dynamic_elf if defined? @dynamic_elf @dynamic_elf ||= patchelf_patcher.elf.segment_by_type(:DYNAMIC).present?
@dynamic_elf = patchelf_patcher.elf.segment_by_type(:DYNAMIC).present?
end end
# Helper class for reading metadata from an ELF file. # Helper class for reading metadata from an ELF file.
class Metadata class Metadata
attr_reader :path, :dylib_id, :dylibs sig { returns(ELFShim) }
attr_reader :path
sig { returns(T.nilable(String)) }
attr_reader :dylib_id
sig { returns(T::Array[String]) }
attr_reader :dylibs
sig { params(path: ELFShim).void }
def initialize(path) def initialize(path)
@path = path @path = T.let(path, ELFShim)
@dylibs = [] @dylibs = T.let([], T::Array[String])
@dylib_id = T.let(nil, T.nilable(String))
@dylib_id, needed = needed_libraries path @dylib_id, needed = needed_libraries path
return if needed.empty? @dylibs = needed.map { |lib| find_full_lib_path(lib).to_s } if needed.present?
@dylibs = needed.map { |lib| find_full_lib_path(lib).to_s } @metadata = T.let(nil, T.nilable(T::Hash[String, T.untyped]))
end end
private private
@ -225,6 +248,7 @@ module ELFShim
@patchelf_patcher ||= ::PatchELF::Patcher.new to_s, on_error: :silent @patchelf_patcher ||= ::PatchELF::Patcher.new to_s, on_error: :silent
end end
sig { returns(Metadata) }
def metadata def metadata
@metadata ||= Metadata.new(self) @metadata ||= Metadata.new(self)
end end

View File

@ -12,11 +12,19 @@ module MachOShim
delegate [:dylib_id] => :macho delegate [:dylib_id] => :macho
def initialize(*args)
@macho = T.let(nil, T.nilable(MachO::MachOFile))
@mach_data = T.let(nil, T.nilable(T::Array[T::Hash[Symbol, T.untyped]]))
super
end
def macho def macho
@macho ||= MachO.open(to_s) @macho ||= MachO.open(to_s)
end end
private :macho private :macho
sig { returns(T::Array[T::Hash[Symbol, T.untyped]]) }
def mach_data def mach_data
@mach_data ||= begin @mach_data ||= begin
machos = [] machos = []