From fecde35c6a5c336f1d40a4674a2d8afc7880f43a Mon Sep 17 00:00:00 2001 From: Michael Harp Date: Wed, 21 Oct 2026 10:36:51 -0400 Subject: [PATCH] Drive the macOS uninstaller from the package receipt The generated uninstaller removed every directory the project declared with `rm -Rf`. Directories shared between packages, such as /opt/puppetlabs/bin, which both openvox-agent and openbolt install into, were wiped along with the other package's files. Uninstalling OpenBolt on a Mac with openvox-agent installed removed the agent's `puppet` and `facter` links; the agent's uninstaller declares all of /opt/puppetlabs and would take OpenBolt with it. Remove exactly what the installer receipt records instead: every file and symlink is deleted, then each declared directory is removed recursively only when `pkgutil --file-info` shows no other installed package claiming it, then any receipt directories left empty are removed, and finally the receipt is forgotten. Operating-system directories such as /etc, /opt and /usr/local are never removed. Directories a package owns alone still go completely, so logs, caches and configuration written after installation are cleaned up as before. If no receipt exists the uninstaller now exits with an error instead of guessing. Fixes OpenVoxProject/openbolt#344 Co-Authored-By: Claude Fable 5.1 Signed-off-by: Michael Harp --- resources/macos/uninstaller.tool.erb | 87 +++++++++++++++++++++++-- spec/lib/vanagon/platform/macos_spec.rb | 52 ++++++++++++++- 2 files changed, 131 insertions(+), 8 deletions(-) diff --git a/resources/macos/uninstaller.tool.erb b/resources/macos/uninstaller.tool.erb index fc83017b..f3b4ab28 100644 --- a/resources/macos/uninstaller.tool.erb +++ b/resources/macos/uninstaller.tool.erb @@ -1,24 +1,97 @@ #!/bin/bash +# Uninstaller for <%= @name %>, generated by vanagon. +# +# Removal is driven by the installer receipt rather than by a fixed list of +# directories, so directories shared with other packages (for example +# /opt/puppetlabs/bin, which is used by both openvox-agent and openbolt) are +# only removed when nothing else owns them. + +export LC_ALL=C + +pkg_id="<%= @identifier %>.<%= @name %>" + +# Top-level directories that belong to the operating system. They are never +# removed, even when the receipt lists them and they end up empty. +protected_dirs=" Applications Library System Users bin etc opt private private/etc private/var sbin usr usr/local var " + +is_protected() { + case "${protected_dirs}" in + *" ${1} "*) return 0 ;; + *) return 1 ;; + esac +} + <%- if has_services? -%> <%- get_services.each do |service| -%> echo "Unloading service <%= service.name %>..." /bin/launchctl unload <%= service.service_file %> echo "OK" echo "Removing service file <%= service.service_file %>..." -/bin/rm <%= service.service_file %> +/bin/rm -f <%= service.service_file %> echo "OK" <%- end -%> <%- end -%> -echo "Removing identifier: <%= @identifier-%>.<%= @name -%>..." -if /usr/sbin/pkgutil --files <%= @identifier-%>.<%= @name -%> >/dev/null 2>&1 ; then - /usr/sbin/pkgutil --forget <%= @identifier-%>.<%= @name -%> - +pkg_info=$(/usr/sbin/pkgutil --pkg-info "${pkg_id}" 2>/dev/null) +if [ -z "${pkg_info}" ]; then + echo "No installer receipt found for ${pkg_id}; nothing to remove." >&2 + exit 1 fi + +volume=$(printf '%s\n' "${pkg_info}" | /usr/bin/sed -n 's/^volume: //p') +volume="${volume:-/}" +location=$(printf '%s\n' "${pkg_info}" | /usr/bin/sed -n 's/^location: //p') +root="${volume%/}/${location#/}" +root="${root%/}" + +workdir=$(/usr/bin/mktemp -d -t "${pkg_id}-uninstall") +trap '/bin/rm -rf "${workdir}"' EXIT + +/usr/sbin/pkgutil --volume "${volume}" --files "${pkg_id}" | /usr/bin/sort > "${workdir}/entries" +/usr/sbin/pkgutil --volume "${volume}" --only-dirs --files "${pkg_id}" | /usr/bin/sort > "${workdir}/directories" + +echo "Removing files installed by ${pkg_id}..." +/usr/bin/comm -23 "${workdir}/entries" "${workdir}/directories" | +while IFS= read -r entry; do + /bin/rm -f "${root}/${entry}" +done echo "OK" -echo "Removing Files" -/bin/rm -Rf <%= get_directories.map {|d| d.path}.join(" ").to_s -%> +# Directories declared by the project are removed along with anything written +# to them after installation (logs, caches, configuration), unless another +# installed package also claims them. +remove_unshared_directory() { + entry="${1#/}" + dir="${root}/${entry}" + [ -d "${dir}" ] || return 0 + if is_protected "${entry}"; then + echo "Keeping ${dir}: it belongs to the operating system" + return 0 + fi + owners=$(/usr/sbin/pkgutil --volume "${volume}" --file-info "${dir}" 2>/dev/null | + /usr/bin/sed -n 's/^pkgid: //p' | /usr/bin/grep -vx "${pkg_id}") + if [ -n "${owners}" ]; then + echo "Keeping ${dir}: it is also used by $(printf '%s' "${owners}" | /usr/bin/tr '\n' ' ')" + return 0 + fi + /bin/rm -Rf "${dir}" +} + +echo "Removing directories owned by ${pkg_id}..." +<%- get_directories.map(&:path).each do |dir| -%> +remove_unshared_directory '<%= dir %>' +<%- end -%> +echo "OK" + +echo "Removing empty directories..." +/usr/bin/sort -r "${workdir}/directories" | +while IFS= read -r entry; do + is_protected "${entry}" && continue + /bin/rmdir "${root}/${entry}" 2>/dev/null +done +echo "OK" +echo "Removing identifier: ${pkg_id}..." +/usr/sbin/pkgutil --volume "${volume}" --forget "${pkg_id}" echo "OK" diff --git a/spec/lib/vanagon/platform/macos_spec.rb b/spec/lib/vanagon/platform/macos_spec.rb index d78878ea..f6ccae3b 100644 --- a/spec/lib/vanagon/platform/macos_spec.rb +++ b/spec/lib/vanagon/platform/macos_spec.rb @@ -1,4 +1,7 @@ +require 'tempfile' require 'vanagon/platform' +require 'vanagon/project' +require 'vanagon/common' describe "Vanagon::Platform::MacOS" do let(:block) { @@ -17,5 +20,52 @@ expect(plat._platform.send(:mktemp)).to eq("mktemp -d -t 'tmp'") end end -end + describe "uninstaller" do + let(:configdir) { '/a/b/c' } + let(:template) { File.expand_path('../../../../resources/macos/uninstaller.tool.erb', __dir__) } + let(:project_block) { + %Q[ project 'test-fixture' do |proj| + proj.identifier 'org.example' + proj.directory '/opt/puppetlabs/test-fixture' + proj.directory '/opt/puppetlabs/bin' + end + ] + } + let(:uninstaller) do + proj = Vanagon::Project::DSL.new('test-fixture', configdir, plat._platform, []) + proj.instance_eval(project_block) + project = proj._project + project.erb_string(template, project.instance_eval { binding }) + end + + it "is valid bash" do + Tempfile.create('uninstaller') do |file| + file.write(uninstaller) + file.flush + expect(system('/bin/bash', '-n', file.path)).to be(true) + end + end + + it "identifies the package by its receipt" do + expect(uninstaller).to include('pkg_id="org.example.test-fixture"') + end + + it "removes the files recorded in the package receipt" do + expect(uninstaller).to include('--files "${pkg_id}"') + end + + it "forgets the package receipt" do + expect(uninstaller).to include('--forget "${pkg_id}"') + end + + it "removes declared directories only when no other package claims them" do + expect(uninstaller).to include("remove_unshared_directory '/opt/puppetlabs/test-fixture'") + expect(uninstaller).to include("remove_unshared_directory '/opt/puppetlabs/bin'") + end + + it "does not unconditionally remove declared directories" do + expect(uninstaller).not_to match(%r{^/bin/rm -Rf /opt/puppetlabs}) + end + end +end