Module: CoreEx::FileUtils::RemoveDir

Included in:
FileUtils
Defined in:
lib/core_ex/file_utils.rb

Instance Method Summary collapse

Instance Method Details

#remove_dir(dir, force = false) ⇒ Object

:nodoc:



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
# File 'lib/core_ex/file_utils.rb', line 17

def remove_dir (dir, force = false) #:nodoc:
  dir = dir.sub(%r</\z>, '')
  first_time_p = true
  begin
    Dir.foreach(dir) do |file|
      next if /\A\.\.?\z/ =~ file
      path = "#{dir}/#{file.untaint}"
      if File.symlink?(path)
        remove_file path, force
      elsif File.directory?(path)
        remove_dir path, force
      else
        remove_file path, force
      end
    end
    begin
      Dir.rmdir dir
    rescue Errno::ENOENT
      raise unless force
    end
  rescue
    if first_time_p
      first_time_p = false
      File.chmod 0777, dir
      retry
    end
    raise
  end
end

#setupObject



13
14
15
# File 'lib/core_ex/file_utils.rb', line 13

def setup
  alias_method :remove_dir_without_chmod, :remove_dir
end