Class: Mktemp Private

Inherits:
Object show all
Includes:
FileUtils
Defined in:
mktemp.rb

Overview

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

Performs Formula#mktemp's functionality and tracks the results. Each instance is only intended to be used once. Can also be used to create a temporary directory with the brew instance's group.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, retain: false, retain_in_cache: false) ⇒ 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.

Parameters:

  • prefix (String)
  • retain (Boolean) (defaults to: false)
  • retain_in_cache (Boolean) (defaults to: false)


15
16
17
18
19
20
21
# File 'mktemp.rb', line 15

def initialize(prefix, retain: false, retain_in_cache: false)
  @prefix = prefix
  @retain_in_cache = T.let(retain_in_cache, T::Boolean)
  @retain = T.let(retain || @retain_in_cache, T::Boolean)
  @quiet = T.let(false, T::Boolean)
  @tmpdir = T.let(nil, T.nilable(Pathname))
end

Instance Attribute Details

#tmpdirPathname? (readonly)

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.

Path to the tmpdir used in this run

Returns:



12
13
14
# File 'mktemp.rb', line 12

def tmpdir
  @tmpdir
end

Instance Method Details

#quiet!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.

Instructs this Mktemp to not emit messages when retention is triggered.



43
44
45
# File 'mktemp.rb', line 43

def quiet!
  @quiet = true
end

#retain!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.

Instructs this Mktemp to retain the staged files.



25
26
27
# File 'mktemp.rb', line 25

def retain!
  @retain = true
end

#retain?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.

True if the staged temporary files should be retained.

Returns:

  • (Boolean)


31
32
33
# File 'mktemp.rb', line 31

def retain?
  @retain
end

#retain_in_cache?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.

True if the source files should be retained.

Returns:

  • (Boolean)


37
38
39
# File 'mktemp.rb', line 37

def retain_in_cache?
  @retain_in_cache
end

#run(chdir: true, &_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:

  • chdir (Boolean) (defaults to: true)
  • _block (T.proc.params(arg0: Mktemp).void)


53
54
55
56
57
58
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'mktemp.rb', line 53

def run(chdir: true, &_block)
  prefix_name = @prefix.tr "@", "AT"
  @tmpdir = if retain_in_cache?
    tmp_dir = HOMEBREW_CACHE/"Sources/#{prefix_name}"
    chmod_rm_rf(tmp_dir) # clear out previous staging directory
    tmp_dir.mkpath
    tmp_dir
  else
    Pathname.new(Dir.mktmpdir("#{prefix_name}-", HOMEBREW_TEMP))
  end

  # Make sure files inside the temporary directory have the same group as the
  # brew instance.
  #
  # Reference from `man 2 open`
  # > When a new file is created, it is given the group of the directory which
  # contains it.
  group_id = if HOMEBREW_BREW_FILE.grpowned?
    HOMEBREW_BREW_FILE.stat.gid
  else
    Process.gid
  end
  begin
    @tmpdir.chown(nil, group_id)
  rescue Errno::EPERM
    require "etc"
    group_name = begin
      Etc.getgrgid(group_id)&.name
    rescue ArgumentError
      # Cover for misconfigured NSS setups
      nil
    end
    opoo "Failed setting group \"#{group_name || group_id}\" on #{@tmpdir}"
  end

  begin
    if chdir
      Dir.chdir(@tmpdir) { yield self }
    else
      yield self
    end
  ensure
    ignore_interrupts { chmod_rm_rf(@tmpdir) } unless retain?
  end
ensure
  if retain? && @tmpdir.present? && !@quiet
    message = retain_in_cache? ? "Source files for debugging available at:" : "Temporary files retained at:"
    ohai message, @tmpdir.to_s
  end
end