aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cleaner.rb
blob: 437b5662835bd4be9f18ae7773fb985452c6353c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
# Cleans a newly installed keg.
# By default:
# * removes info files
# * removes .la files
# * removes empty directories
# * sets permissions on executables
class Cleaner

  # Create a cleaner for the given formula and clean its keg
  def initialize f
    ObserverPathnameExtension.reset_counts!

    @f = f
    [f.bin, f.sbin, f.lib].select{ |d| d.exist? }.each{ |d| clean_dir d }

    if ENV['HOMEBREW_KEEP_INFO']
      # Get rid of the directory file, so it no longer bother us at link stage.
      info_dir_file = f.info + 'dir'
      if info_dir_file.file? and not f.skip_clean? info_dir_file
        puts "rm #{info_dir_file}" if ARGV.verbose?
        info_dir_file.unlink
      end
    else
      f.info.rmtree if f.info.directory? and not f.skip_clean? f.info
    end

    prune
  end

  private

  def prune
    dirs = []
    symlinks = []

    @f.prefix.find do |path|
      if @f.skip_clean? path
        Find.prune
      elsif path.symlink?
        symlinks << path
      elsif path.directory?
        dirs << path
      end
    end

    dirs.reverse_each do |d|
      if d.children.empty?
        puts "rmdir: #{d} (empty)" if ARGV.verbose?
        d.rmdir
      end
    end

    symlinks.reverse_each do |s|
      s.unlink unless s.resolved_path_exists?
    end
  end

  # Set permissions for executables and non-executables
  def clean_file_permissions path
    perms = if path.mach_o_executable? || path.text_executable?
      0555
    else
      0444
    end
    if ARGV.debug?
      old_perms = path.stat.mode & 0777
      if perms != old_perms
        puts "Fixing #{path} permissions from #{old_perms.to_s(8)} to #{perms.to_s(8)}"
      end
    end
    path.chmod perms
  end

  # Clean a single folder (non-recursively)
  def clean_dir d
    d.find do |path|
      path.extend(ObserverPathnameExtension)

      Find.prune if @f.skip_clean? path

      if path.symlink? or path.directory?
        next
      elsif path.extname == '.la'
        path.unlink
      elsif path == @f.lib+'charset.alias'
        # Many formulae symlink this file, but it is not strictly needed
        path.unlink
      else
        clean_file_permissions(path)
      end
    end
  end

end