Class: FileUtils::Mktemp

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
Library/Homebrew/extend/fileutils.rb

Overview

Performs mktemp's functionality, and tracks the results. Each instance is only intended to be used once.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileUtils

#make, mkdir, mktemp, #old_mkdir, #old_ruby, #ruby, #scons, #xcodebuild

Constructor Details

#initialize(prefix = name, opts = {}) ⇒ Mktemp

Returns a new instance of Mktemp



28
29
30
31
32
# File 'Library/Homebrew/extend/fileutils.rb', line 28

def initialize(prefix = name, opts = {})
  @prefix = prefix
  @retain = opts[:retain]
  @quiet = false
end

Instance Attribute Details

#tmpdirObject (readonly)

Path to the tmpdir used in this run, as a Pathname.



26
27
28
# File 'Library/Homebrew/extend/fileutils.rb', line 26

def tmpdir
  @tmpdir
end

Instance Method Details

#quiet!Object

Instructs this Mktemp to not emit messages when retention is triggered



45
46
47
# File 'Library/Homebrew/extend/fileutils.rb', line 45

def quiet!
  @quiet = true
end

#retain!Object

Instructs this Mktemp to retain the staged files



35
36
37
# File 'Library/Homebrew/extend/fileutils.rb', line 35

def retain!
  @retain = true
end

#retain?Boolean

True if the staged temporary files should be retained

Returns:

  • (Boolean)


40
41
42
# File 'Library/Homebrew/extend/fileutils.rb', line 40

def retain?
  @retain
end

#runObject



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
# File 'Library/Homebrew/extend/fileutils.rb', line 53

def run
  @tmpdir = Pathname.new(Dir.mktmpdir("#{@prefix}-", HOMEBREW_TEMP))

  # 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
    chown(nil, group_id, tmpdir)
  rescue Errno::EPERM
    opoo "Failed setting group \"#{Etc.getgrgid(group_id).name}\" on #{tmpdir}"
  end

  begin
    Dir.chdir(tmpdir) { yield self }
  ensure
    ignore_interrupts { rm_rf(tmpdir) } unless retain?
  end
ensure
  if retain? && !@tmpdir.nil? && !@quiet
    ohai "Kept temporary files"
    puts "Temporary files retained at #{@tmpdir}"
  end
end

#to_sObject



49
50
51
# File 'Library/Homebrew/extend/fileutils.rb', line 49

def to_s
  "[Mktemp: #{tmpdir} retain=#{@retain} quiet=#{@quiet}]"
end