Delete pkgutil directories that are really files.

Sometimes, pkgutil will return actual files (usually .nib files) as if they were part of the directory.  Microsoft Office is an example of this: in a recent update the file `/Library/Application Support/Microsoft/MAU2.0/Microsoft AutoUpdate.app/Contents/SharedSupport/Microsoft Error Reporting.app/Contents/Resources/en.lproj/MainWindowAlt.nib` was returning from `/usr/sbin/pkgutil --only-dirs --files com.microsoft.package.component` even though it should have been a file instead of a directory.  This caused the `rmdir` command to fail.

This patch will check if we are trying to delete a “directory” that is really a “file” - and if we are, we just delete the file instead.  This will allow packages that get in this state to be uninstalled.  A unit test which can be run using `brew cask-tests` is also included.
This commit is contained in:
Nathan Toone 2016-12-28 10:56:03 -07:00
parent 6648ff1307
commit 6d318761d2
2 changed files with 25 additions and 0 deletions

View File

@ -26,6 +26,7 @@ module Hbc
_deepest_path_first(pkgutil_bom_dirs).each do |dir|
next unless dir.exist? && !MacOS.undeletable?(dir)
_with_full_permissions(dir) do
_delete_broken_file_dir(dir) && next
_clean_broken_symlinks(dir)
_clean_ds_store(dir)
_rmdir(dir)
@ -97,6 +98,13 @@ module Hbc
end
end
# Some pkgs (microsoft office for one) leave files (generally nibs) but
# report them as directories. We remove these as files instead.
def _delete_broken_file_dir(path)
return unless path.file? && !path.symlink?
@command.run!("/bin/rm", args: ["-f", "--", path], sudo: true)
end
# Some pkgs leave broken symlinks hanging around; we clean them out before
# attempting to rmdir to prevent extra cruft from lying around after
# uninstall

View File

@ -68,6 +68,23 @@ describe Hbc::Pkg do
fake_dir.must_be :exist?
end
it "chokes on directories that are really files" do
pkg = Hbc::Pkg.new("my.fake.pkg", Hbc::NeverSudoSystemCommand)
fake_dir = Pathname(Dir.mktmpdir)
fake_file = fake_dir.join("ima_file").tap { |path| FileUtils.touch(path) }
pkg.stubs(:pkgutil_bom_specials).returns([])
pkg.stubs(:pkgutil_bom_files).returns([])
pkg.stubs(:pkgutil_bom_dirs).returns([fake_file, fake_dir])
pkg.stubs(:forget)
pkg.uninstall
fake_file.wont_be :exist?
fake_dir.wont_be :exist?
end
it "snags permissions on ornery dirs, but returns them afterwords" do
pkg = Hbc::Pkg.new("my.fake.pkg", Hbc::NeverSudoSystemCommand)