2016-09-24 13:52:43 +02:00
|
|
|
module Hbc
|
|
|
|
class Pkg
|
|
|
|
def self.all_matching(regexp, command)
|
2016-10-23 14:44:14 +02:00
|
|
|
command.run("/usr/sbin/pkgutil", args: ["--pkgs=#{regexp}"]).stdout.split("\n").map do |package_id|
|
2016-09-24 13:52:43 +02:00
|
|
|
new(package_id.chomp, command)
|
2016-10-23 14:44:14 +02:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
attr_reader :package_id
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def initialize(package_id, command = SystemCommand)
|
|
|
|
@package_id = package_id
|
|
|
|
@command = command
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def uninstall
|
|
|
|
odebug "Deleting pkg files"
|
|
|
|
pkgutil_bom_files.each_slice(500) do |file_slice|
|
|
|
|
@command.run("/bin/rm", args: file_slice.unshift("-f", "--"), sudo: true)
|
|
|
|
end
|
|
|
|
odebug "Deleting pkg symlinks and special files"
|
|
|
|
pkgutil_bom_specials.each_slice(500) do |file_slice|
|
|
|
|
@command.run("/bin/rm", args: file_slice.unshift("-f", "--"), sudo: true)
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
odebug "Deleting pkg directories"
|
|
|
|
_deepest_path_first(pkgutil_bom_dirs).each do |dir|
|
|
|
|
next unless dir.exist? && !MacOS.undeletable?(dir)
|
|
|
|
_with_full_permissions(dir) do
|
|
|
|
_clean_broken_symlinks(dir)
|
|
|
|
_clean_ds_store(dir)
|
|
|
|
_rmdir(dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
forget
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def forget
|
|
|
|
odebug "Unregistering pkg receipt (aka forgetting)"
|
|
|
|
@command.run!("/usr/sbin/pkgutil", args: ["--forget", package_id], sudo: true)
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def pkgutil_bom(*type)
|
|
|
|
@command.run!("/usr/sbin/pkgutil", args: [*type, "--files", package_id].compact)
|
|
|
|
.stdout
|
|
|
|
.split("\n")
|
|
|
|
.map { |path| root.join(path) }
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def pkgutil_bom_files
|
|
|
|
@pkgutil_bom_files ||= pkgutil_bom("--only-files")
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def pkgutil_bom_dirs
|
|
|
|
@pkgutil_bom_dirs ||= pkgutil_bom("--only-dirs")
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def pkgutil_bom_all
|
|
|
|
@pkgutil_bom_all ||= pkgutil_bom
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def pkgutil_bom_specials
|
|
|
|
pkgutil_bom_all - pkgutil_bom_files - pkgutil_bom_dirs
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def root
|
|
|
|
@root ||= Pathname(info.fetch("volume")).join(info.fetch("install-location"))
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def info
|
|
|
|
@command.run!("/usr/sbin/pkgutil", args: ["--pkg-info-plist", package_id])
|
|
|
|
.plist
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def _rmdir(path)
|
|
|
|
@command.run!("/bin/rmdir", args: ["--", path], sudo: true) if path.children.empty?
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def _with_full_permissions(path)
|
2016-10-14 20:01:35 +02:00
|
|
|
original_mode = (path.stat.mode % 01000).to_s(8)
|
2016-09-24 13:52:43 +02:00
|
|
|
# TODO: similarly read and restore macOS flags (cf man chflags)
|
|
|
|
@command.run!("/bin/chmod", args: ["--", "777", path], sudo: true)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
if path.exist? # block may have removed dir
|
|
|
|
@command.run!("/bin/chmod", args: ["--", original_mode, path], sudo: true)
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def _deepest_path_first(paths)
|
|
|
|
paths.sort do |path_a, path_b|
|
|
|
|
path_b.to_s.split("/").count <=> path_a.to_s.split("/").count
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
# Some pkgs leave broken symlinks hanging around; we clean them out before
|
|
|
|
# attempting to rmdir to prevent extra cruft from lying around after
|
|
|
|
# uninstall
|
|
|
|
def _clean_broken_symlinks(dir)
|
|
|
|
dir.children.each do |child|
|
|
|
|
if _broken_symlink?(child)
|
|
|
|
@command.run!("/bin/rm", args: ["--", child], sudo: true)
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def _clean_ds_store(dir)
|
|
|
|
ds_store = dir.join(".DS_Store")
|
|
|
|
@command.run!("/bin/rm", args: ["--", ds_store], sudo: true) if ds_store.exist?
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def _broken_symlink?(path)
|
|
|
|
path.symlink? && !path.exist?
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|