| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2017-03-08 03:03:49 +01:00
										 |  |  |       unless pkgutil_bom_files.empty? | 
					
						
							|  |  |  |         odebug "Deleting pkg files" | 
					
						
							| 
									
										
										
										
											2017-08-06 12:30:40 +02:00
										 |  |  |         @command.run!("/usr/bin/xargs", args: ["-0", "--", "/bin/rm", "--"], input: pkgutil_bom_files.join("\0"), sudo: true) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-03-08 03:03:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       unless pkgutil_bom_specials.empty? | 
					
						
							|  |  |  |         odebug "Deleting pkg symlinks and special files" | 
					
						
							| 
									
										
										
										
											2017-08-06 12:30:40 +02:00
										 |  |  |         @command.run!("/usr/bin/xargs", args: ["-0", "--", "/bin/rm", "--"], input: pkgutil_bom_specials.join("\0"), sudo: true) | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-03-08 03:03:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       unless pkgutil_bom_dirs.empty? | 
					
						
							|  |  |  |         odebug "Deleting pkg directories" | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |         deepest_path_first(pkgutil_bom_dirs).each do |dir| | 
					
						
							|  |  |  |           with_full_permissions(dir) do | 
					
						
							|  |  |  |             clean_broken_symlinks(dir) | 
					
						
							|  |  |  |             clean_ds_store(dir) | 
					
						
							|  |  |  |             rmdir(dir) | 
					
						
							| 
									
										
										
										
											2017-03-08 03:03:49 +01:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-03-08 03:03:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |       if root.directory? && !MacOS.undeletable?(root) | 
					
						
							|  |  |  |         clean_ds_store(root) | 
					
						
							|  |  |  |         rmdir(root) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def pkgutil_bom_files | 
					
						
							|  |  |  |       @pkgutil_bom_files ||= pkgutil_bom_all.select(&:file?) - pkgutil_bom_specials | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def pkgutil_bom_specials | 
					
						
							|  |  |  |       @pkgutil_bom_specials ||= pkgutil_bom_all.select(&method(:special?)) | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     def pkgutil_bom_dirs | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |       @pkgutil_bom_dirs ||= pkgutil_bom_all.select(&:directory?) - pkgutil_bom_specials | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     def pkgutil_bom_all | 
					
						
							| 
									
										
										
										
											2017-04-21 15:50:39 +02:00
										 |  |  |       @pkgutil_bom_all ||= @command.run!("/usr/sbin/pkgutil", args: ["--files", package_id]) | 
					
						
							|  |  |  |                                    .stdout | 
					
						
							|  |  |  |                                    .split("\n") | 
					
						
							|  |  |  |                                    .map { |path| root.join(path) } | 
					
						
							| 
									
										
										
										
											2017-08-06 12:30:40 +02:00
										 |  |  |                                    .reject(&MacOS.public_method(:undeletable?)) | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     def root | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |       @root ||= Pathname.new(info.fetch("volume")).join(info.fetch("install-location")) | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     def info | 
					
						
							| 
									
										
										
										
											2017-04-21 15:50:39 +02:00
										 |  |  |       @info ||= @command.run!("/usr/sbin/pkgutil", args: ["--pkg-info-plist", package_id]) | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |                         .plist | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def special?(path) | 
					
						
							|  |  |  |       path.symlink? || path.chardev? || path.blockdev? | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def rmdir(path) | 
					
						
							| 
									
										
										
										
											2016-11-22 03:08:10 +09:00
										 |  |  |       return unless path.children.empty? | 
					
						
							|  |  |  |       if path.symlink? | 
					
						
							|  |  |  |         @command.run!("/bin/rm", args: ["-f", "--", path], sudo: true) | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         @command.run!("/bin/rmdir", args: ["--", path], sudo: true) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def with_full_permissions(path) | 
					
						
							| 
									
										
										
										
											2016-10-14 20:01:35 +02:00
										 |  |  |       original_mode = (path.stat.mode % 01000).to_s(8) | 
					
						
							| 
									
										
										
										
											2017-03-10 08:24:40 +01:00
										 |  |  |       original_flags = @command.run!("/usr/bin/stat", args: ["-f", "%Of", "--", path]).stdout.chomp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       @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) | 
					
						
							| 
									
										
										
										
											2017-03-10 08:24:40 +01:00
										 |  |  |         @command.run!("/usr/bin/chflags", args: ["--", original_flags, path], sudo: true) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def deepest_path_first(paths) | 
					
						
							|  |  |  |       paths.sort_by { |path| -path.to_s.split(File::SEPARATOR).count } | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def clean_ds_store(dir) | 
					
						
							|  |  |  |       return unless (ds_store = dir.join(".DS_Store")).exist? | 
					
						
							|  |  |  |       @command.run!("/bin/rm", args: ["--", ds_store], sudo: true) | 
					
						
							| 
									
										
										
										
											2016-12-28 10:56:03 -07:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     # Some packages leave broken symlinks around; we clean them out before | 
					
						
							|  |  |  |     # attempting to `rmdir` to prevent extra cruft from lying around. | 
					
						
							|  |  |  |     def clean_broken_symlinks(dir) | 
					
						
							|  |  |  |       dir.children.select(&method(:broken_symlink?)).each do |path| | 
					
						
							|  |  |  |         @command.run!("/bin/rm", args: ["--", path], sudo: true) | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def broken_symlink?(path) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       path.symlink? && !path.exist? | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end |