Maxim Belkin de0b93f912
pathname: improvements, cleanups, and new methods
- atomic_write: close file before renaming to prevent error:
  'Device or resource busy'
- ensure_writable: preserve executable bit
- new elf? and dynamic? methods
2017-11-07 14:18:25 -06:00

20 lines
546 B
Ruby

class Pathname
# @private
def elf?
# See: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header
read(4) == "\x7fELF"
end
# @private
def dynamic_elf?
if which "readelf"
popen_read("readelf", "-l", to_path).include?(" DYNAMIC ")
elsif which "file"
!popen_read("file", "-L", "-b", to_path)[/dynamic|shared/].nil?
else
raise StandardError, "Neither `readelf` nor `file` is available "\
"to determine whether '#{self}' is dynamically or statically linked."
end
end
end