From f6f225127fd864e4fc2034c675613fc24a8ac797 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Fri, 26 Dec 2014 09:59:24 -0500 Subject: [PATCH] rm homebrew-fork extend Pathname#rmdir_if_possible integrate a modified version into our codebase as a utility method (no longer a monkeypatch) --- lib/cask/installer.rb | 10 +++------ lib/cask/utils.rb | 21 +++++++++++++++++++ .../Library/Homebrew/extend/pathname.rb | 16 -------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/cask/installer.rb b/lib/cask/installer.rb index 06757971c8..340913e94b 100644 --- a/lib/cask/installer.rb +++ b/lib/cask/installer.rb @@ -316,15 +316,11 @@ class Cask::Installer permissions_rmtree subdir unless PERSISTENT_METADATA_SUBDIRS.include?(subdir.basename) end end - if @cask.metadata_versioned_container_path.respond_to?(:rmdir_if_possible) - @cask.metadata_versioned_container_path.rmdir_if_possible - end - if @cask.metadata_master_container_path.respond_to?(:rmdir_if_possible) - @cask.metadata_master_container_path.rmdir_if_possible - end + Cask::Utils.rmdir_if_possible(@cask.metadata_versioned_container_path) + Cask::Utils.rmdir_if_possible(@cask.metadata_master_container_path) # toplevel staged distribution - @cask.caskroom_path.rmdir_if_possible + Cask::Utils.rmdir_if_possible(@cask.caskroom_path) end def purge_caskroom_path diff --git a/lib/cask/utils.rb b/lib/cask/utils.rb index 9dfa929d69..5c1c92030f 100644 --- a/lib/cask/utils.rb +++ b/lib/cask/utils.rb @@ -134,6 +134,27 @@ module Cask::Utils end end + # from Homebrew + # children.length == 0 is slow to enumerate the whole directory just + # to see if it is empty + def self.rmdir_if_possible(dir) + dirpath = Pathname(dir) + begin + dirpath.rmdir + true + rescue Errno::ENOTEMPTY + if (ds_store = dirpath.join('.DS_Store')).exist? and + dirpath.children.length == 1 + ds_store.unlink + retry + else + false + end + rescue Errno::EACCES, Errno::ENOENT + false + end + end + # paths that "look" descendant (textually) will still # return false unless both the given paths exist def self.file_is_descendant(file, dir) diff --git a/lib/homebrew-fork/Library/Homebrew/extend/pathname.rb b/lib/homebrew-fork/Library/Homebrew/extend/pathname.rb index 9bf5163d0c..0de3f3039f 100644 --- a/lib/homebrew-fork/Library/Homebrew/extend/pathname.rb +++ b/lib/homebrew-fork/Library/Homebrew/extend/pathname.rb @@ -1,22 +1,6 @@ require 'pathname' class Pathname - # I don't trust the children.length == 0 check particularly, not to mention - # it is slow to enumerate the whole directory just to see if it is empty, - # instead rely on good ol' libc and the filesystem - def rmdir_if_possible - rmdir - true - rescue Errno::ENOTEMPTY - if (ds_store = self+'.DS_Store').exist? && children.length == 1 - ds_store.unlink - retry - else - false - end - rescue Errno::EACCES, Errno::ENOENT - false - end def version require 'version'