2016-08-18 22:11:42 +03:00
|
|
|
require "yaml"
|
|
|
|
require "open3"
|
|
|
|
require "stringio"
|
|
|
|
|
2018-05-25 18:03:16 +02:00
|
|
|
BUG_REPORTS_URL = "https://github.com/Homebrew/homebrew-cask#reporting-bugs".freeze
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
module Hbc
|
|
|
|
module Utils
|
|
|
|
def self.gain_permissions_remove(path, command: SystemCommand)
|
|
|
|
if path.respond_to?(:rmtree) && path.exist?
|
2017-04-01 01:53:29 +02:00
|
|
|
gain_permissions(path, ["-R"], command) do |p|
|
|
|
|
if p.parent.writable?
|
|
|
|
p.rmtree
|
|
|
|
else
|
|
|
|
command.run("/bin/rm",
|
2017-05-04 00:17:56 -07:00
|
|
|
args: ["-r", "-f", "--", p],
|
2017-04-01 01:53:29 +02:00
|
|
|
sudo: true)
|
|
|
|
end
|
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
elsif File.symlink?(path)
|
|
|
|
gain_permissions(path, ["-h"], command, &FileUtils.method(:rm_f))
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.gain_permissions(path, command_args, command)
|
|
|
|
tried_permissions = false
|
|
|
|
tried_ownership = false
|
|
|
|
begin
|
|
|
|
yield path
|
2018-09-02 20:14:54 +01:00
|
|
|
rescue
|
2016-09-24 13:52:43 +02:00
|
|
|
# in case of permissions problems
|
|
|
|
unless tried_permissions
|
|
|
|
# TODO: Better handling for the case where path is a symlink.
|
|
|
|
# The -h and -R flags cannot be combined, and behavior is
|
|
|
|
# dependent on whether the file argument has a trailing
|
|
|
|
# slash. This should do the right thing, but is fragile.
|
|
|
|
command.run("/usr/bin/chflags",
|
|
|
|
must_succeed: false,
|
|
|
|
args: command_args + ["--", "000", path])
|
|
|
|
command.run("/bin/chmod",
|
|
|
|
must_succeed: false,
|
|
|
|
args: command_args + ["--", "u+rwx", path])
|
|
|
|
command.run("/bin/chmod",
|
|
|
|
must_succeed: false,
|
|
|
|
args: command_args + ["-N", path])
|
|
|
|
tried_permissions = true
|
|
|
|
retry # rmtree
|
|
|
|
end
|
|
|
|
unless tried_ownership
|
|
|
|
# in case of ownership problems
|
|
|
|
# TODO: Further examine files to see if ownership is the problem
|
|
|
|
# before using sudo+chown
|
|
|
|
ohai "Using sudo to gain ownership of path '#{path}'"
|
|
|
|
command.run("/usr/sbin/chown",
|
|
|
|
args: command_args + ["--", current_user, path],
|
|
|
|
sudo: true)
|
|
|
|
tried_ownership = true
|
|
|
|
# retry chflags/chmod after chown
|
|
|
|
tried_permissions = false
|
|
|
|
retry # rmtree
|
|
|
|
end
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.current_user
|
|
|
|
Etc.getpwuid(Process.euid).name
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.path_occupied?(path)
|
|
|
|
File.exist?(path) || File.symlink?(path)
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.error_message_with_suggestions
|
2017-10-15 02:28:32 +02:00
|
|
|
<<~EOS
|
2016-11-03 02:51:22 +00:00
|
|
|
Follow the instructions here:
|
2017-03-31 20:19:27 +01:00
|
|
|
#{Formatter.url(BUG_REPORTS_URL)}
|
2016-09-24 13:52:43 +02:00
|
|
|
EOS
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.method_missing_message(method, token, section = nil)
|
|
|
|
poo = []
|
|
|
|
poo << "Unexpected method '#{method}' called"
|
|
|
|
poo << "during #{section}" if section
|
|
|
|
poo << "on Cask #{token}."
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
opoo(poo.join(" ") + "\n" + error_message_with_suggestions)
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|