| 
									
										
										
										
											2021-09-11 01:00:23 +01:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  | require "utils/user" | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | require "open3" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | BUG_REPORTS_URL = "https://github.com/Homebrew/homebrew-cask#reporting-bugs" | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 08:29:14 +02:00
										 |  |  | module Cask | 
					
						
							| 
									
										
										
										
											2020-08-24 23:54:41 +02:00
										 |  |  |   # Helper functions for various cask operations. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api private | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |   module Utils | 
					
						
							| 
									
										
										
										
											2023-05-13 02:05:02 +02:00
										 |  |  |     def self.gain_permissions_mkpath(path, command: SystemCommand) | 
					
						
							|  |  |  |       dir = path.ascend.find(&:directory?) | 
					
						
							|  |  |  |       return if path == dir | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if dir.writable? | 
					
						
							|  |  |  |         path.mkpath | 
					
						
							|  |  |  |       else | 
					
						
							| 
									
										
										
										
											2023-05-13 06:44:28 +02:00
										 |  |  |         command.run!("/bin/mkdir", args: ["-p", "--", path], sudo: true, print_stderr: false) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.gain_permissions_rmdir(path, command: SystemCommand) | 
					
						
							|  |  |  |       gain_permissions(path, [], command) do |p| | 
					
						
							|  |  |  |         if p.parent.writable? | 
					
						
							|  |  |  |           FileUtils.rmdir p | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           command.run!("/bin/rmdir", args: ["--", p], sudo: true, print_stderr: false) | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2023-05-13 02:05:02 +02:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.gain_permissions_remove(path, command: SystemCommand) | 
					
						
							| 
									
										
										
										
											2023-05-13 03:41:20 +02:00
										 |  |  |       directory = false | 
					
						
							|  |  |  |       permission_flags = if path.symlink? | 
					
						
							|  |  |  |         ["-h"] | 
					
						
							|  |  |  |       elsif path.directory? | 
					
						
							|  |  |  |         directory = true | 
					
						
							|  |  |  |         ["-R"] | 
					
						
							|  |  |  |       elsif path.exist? | 
					
						
							|  |  |  |         [] | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         # Nothing to remove. | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       gain_permissions(path, permission_flags, command) do |p| | 
					
						
							|  |  |  |         if p.parent.writable? | 
					
						
							|  |  |  |           if directory | 
					
						
							| 
									
										
										
										
											2017-04-01 01:53:29 +02:00
										 |  |  |             p.rmtree | 
					
						
							|  |  |  |           else | 
					
						
							| 
									
										
										
										
											2023-05-13 03:41:20 +02:00
										 |  |  |             FileUtils.rm_f p | 
					
						
							| 
									
										
										
										
											2017-04-01 01:53:29 +02:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2023-05-13 03:41:20 +02:00
										 |  |  |         else | 
					
						
							|  |  |  |           recursive_flag = directory ? ["-R"] : [] | 
					
						
							| 
									
										
										
										
											2023-05-13 06:44:28 +02:00
										 |  |  |           command.run!("/bin/rm", args: recursive_flag + ["-f", "--", p], sudo: true, print_stderr: false) | 
					
						
							| 
									
										
										
										
											2017-04-01 01:53:29 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2024-04-01 16:22:42 +01:00
										 |  |  |           print_stderr = Context.current.debug? || Context.current.verbose? | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |           # 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", | 
					
						
							| 
									
										
										
										
											2024-04-01 16:22:42 +01:00
										 |  |  |                       print_stderr:, | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |                       args:         command_args + ["--", "000", path]) | 
					
						
							|  |  |  |           command.run("/bin/chmod", | 
					
						
							| 
									
										
										
										
											2024-04-01 16:22:42 +01:00
										 |  |  |                       print_stderr:, | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |                       args:         command_args + ["--", "u+rwx", path]) | 
					
						
							|  |  |  |           command.run("/bin/chmod", | 
					
						
							| 
									
										
										
										
											2024-04-01 16:22:42 +01:00
										 |  |  |                       print_stderr:, | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |                       args:         command_args + ["-N", path]) | 
					
						
							|  |  |  |           tried_permissions = true | 
					
						
							|  |  |  |           retry # rmtree | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2019-10-23 05:21:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         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", | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  |                       args: command_args + ["--", User.current, path], | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |                       sudo: true) | 
					
						
							|  |  |  |           tried_ownership = true | 
					
						
							|  |  |  |           # retry chflags/chmod after chown | 
					
						
							|  |  |  |           tried_permissions = false | 
					
						
							|  |  |  |           retry # rmtree | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2019-10-23 05:21:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         raise | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |     sig { params(path: Pathname).returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.path_occupied?(path) | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       path.exist? || path.symlink? | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-12 16:06:38 -04:00
										 |  |  |     sig { params(name: String).returns(String) } | 
					
						
							|  |  |  |     def self.token_from(name) | 
					
						
							|  |  |  |       name.downcase | 
					
						
							|  |  |  |           .gsub("+", "-plus-") | 
					
						
							|  |  |  |           .gsub("@", "-at-") | 
					
						
							|  |  |  |           .gsub(/[ _·•]/, "-") | 
					
						
							|  |  |  |           .gsub(/[^\w-]/, "") | 
					
						
							|  |  |  |           .gsub(/--+/, "-") | 
					
						
							|  |  |  |           .delete_prefix("-") | 
					
						
							|  |  |  |           .delete_suffix("-") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |     sig { returns(String) } | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2020-08-19 23:00:46 +02:00
										 |  |  |       message = +"Unexpected method '#{method}' called " | 
					
						
							|  |  |  |       message << "during #{section} " if section | 
					
						
							|  |  |  |       message << "on Cask #{token}." | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 17:00:54 +01:00
										 |  |  |       ofail "#{message}\n#{error_message_with_suggestions}" | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end |