Module: Cask::Utils Private

Extended by:
Utils::Output::Mixin
Defined in:
cask/utils.rb,
cask/utils/trash.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Helper functions for various cask operations.

Defined Under Namespace

Modules: Trash

Constant Summary collapse

BUG_REPORTS_URL =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"https://github.com/Homebrew/homebrew-cask#reporting-bugs"
FULL_DISK_ACCESS_TCC_PATH =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"~/Library/Application Support/com.apple.TCC"

Class Method Summary collapse

Methods included from Utils::Output::Mixin

issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_once, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning

Class Method Details

.full_disk_access_enabled? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


29
30
31
# File 'cask/utils.rb', line 29

def self.full_disk_access_enabled?
  File.readable?(File.expand_path(FULL_DISK_ACCESS_TCC_PATH))
end

.gain_permissions(path, command_args, command, &_block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'cask/utils.rb', line 95

def self.gain_permissions(path, command_args, command, &_block)
  tried_permissions = T.let(false, T::Boolean)
  tried_ownership = T.let(false, T::Boolean)
  begin
    yield path
  rescue
    target = path.cleanpath
    flags = target.symlink? ? ["-h"] : command_args

    # in case of permissions problems
    unless tried_permissions
      print_stderr = Context.current.debug? || Context.current.verbose?
      command.run("/usr/bin/chflags",
                  print_stderr:,
                  args:         flags + ["--", "000", target])
      command.run("chmod",
                  print_stderr:,
                  args:         flags + ["--", "u+rwx", target])
      command.run("chmod",
                  print_stderr:,
                  args:         flags + ["-N", target])
      tried_permissions = true
      retry # rmtree
    end

    # in case of ownership problems
    recursive = flags.include?("-R")
    if !tried_ownership && ownership_problem?(target, recursive:)
      ohai "Using sudo to gain ownership of path '#{path}'"
      command.run("chown",
                  args: flags + ["--", User.current.to_s, target],
                  sudo: true)
      tried_ownership = true
      # retry chflags/chmod after chown
      tried_permissions = false
      retry # rmtree
    end

    raise
  end
end

.gain_permissions_mkpath(path, command: SystemCommand) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



34
35
36
37
38
39
40
41
42
43
# File 'cask/utils.rb', line 34

def self.gain_permissions_mkpath(path, command: SystemCommand)
  dir = path.ascend.find(&:directory?)
  return if path == dir

  if dir&.writable?
    path.mkpath
  else
    command.run!("mkdir", args: ["-p", "--", path], sudo: nil, print_stderr: false)
  end
end

.gain_permissions_remove(path, command: SystemCommand) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'cask/utils.rb', line 59

def self.gain_permissions_remove(path, command: SystemCommand)
  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
        FileUtils.rm_r p
      else
        FileUtils.rm_f p
      end
    else
      recursive_flag = directory ? ["-R"] : []
      command.run!("/bin/rm", args: recursive_flag + ["-f", "--", p], sudo: nil, print_stderr: false)
    end
  end
end

.gain_permissions_rmdir(path, command: SystemCommand) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



46
47
48
49
50
51
52
53
54
55
56
# File 'cask/utils.rb', line 46

def self.gain_permissions_rmdir(path, command: SystemCommand)
  # `-h` unconditionally: it is a no-op on a real directory and avoids
  # deciding from a path that could be replaced before the recovery runs.
  gain_permissions(path, ["-h"], command) do |p|
    if p.parent.writable?
      FileUtils.rmdir p
    else
      command.run!("rmdir", args: ["--", p], sudo: nil, print_stderr: false)
    end
  end
end

.ownership_problem?(path, recursive:) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Whether sudo chown could plausibly fix the failure we just rescued: the chflags/chmod above run without sudo, so they only fail on paths we do not own. lstat rather than owned?, which would follow a symlink.

Parameters:

Returns:

  • (Boolean)


141
142
143
144
145
146
147
148
149
150
151
152
# File 'cask/utils.rb', line 141

def self.ownership_problem?(path, recursive:)
  return false if Process.euid.zero?

  paths = recursive ? path.find : [path]
  paths.any? do |candidate|
    candidate.lstat.uid != Process.euid
  rescue SystemCallError
    false
  end
rescue SystemCallError
  false
end

.path_occupied?(path) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


155
156
157
# File 'cask/utils.rb', line 155

def self.path_occupied?(path)
  path.exist? || path.symlink?
end

.privacy_security_preference_pane(access) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



18
19
20
21
22
23
24
25
26
# File 'cask/utils.rb', line 18

def self.privacy_security_preference_pane(access)
  navigation_path = if OnSystem.os_condition_met?(:monterey, :or_older)
    "System Preferences → Security & Privacy → Privacy"
  else
    "System Settings → Privacy & Security"
  end

  "#{navigation_path} → #{access}"
end

.token_from(name) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



160
161
162
163
164
165
166
167
168
# File 'cask/utils.rb', line 160

def self.token_from(name)
  name.downcase
      .gsub("+", "-plus-")
      .gsub(/[ _·•]/, "-")
      .gsub(/[^\w@-]/, "")
      .gsub(/--+/, "-")
      .delete_prefix("-")
      .delete_suffix("-")
end