From 099ff29fc1d11676f51fe24fc1d7b42f74e03650 Mon Sep 17 00:00:00 2001 From: Miguel Flores Ruiz de Eguino Date: Sat, 4 Jan 2014 20:43:43 -0600 Subject: [PATCH] Fix widget install and uninstall using hardlinks. --- lib/cask/artifact.rb | 1 + lib/cask/artifact/hardlinked.rb | 18 ++++++++++++++++++ lib/cask/artifact/symlinked.rb | 30 ++++++++++++++++++++++++------ lib/cask/artifact/widget.rb | 2 +- 4 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 lib/cask/artifact/hardlinked.rb diff --git a/lib/cask/artifact.rb b/lib/cask/artifact.rb index 714168debc62..89cfd4a44606 100644 --- a/lib/cask/artifact.rb +++ b/lib/cask/artifact.rb @@ -2,6 +2,7 @@ module Cask::Artifact; end require 'cask/artifact/base' require 'cask/artifact/symlinked' +require 'cask/artifact/hardlinked' require 'cask/artifact/app' require 'cask/artifact/block' diff --git a/lib/cask/artifact/hardlinked.rb b/lib/cask/artifact/hardlinked.rb new file mode 100644 index 000000000000..cb877892b72c --- /dev/null +++ b/lib/cask/artifact/hardlinked.rb @@ -0,0 +1,18 @@ +class Cask::Artifact::Hardlinked < Cask::Artifact::Symlinked + def islink?(path) + return false unless path.respond_to?(:stat) + path.stat.nlink > 1 + end + + def link_action(source, target) + File.link(source, target) + end + + def unlink_action(target) + FileUtils.rm_rf target + end + + def link_name + 'hardlink' + end +end diff --git a/lib/cask/artifact/symlinked.rb b/lib/cask/artifact/symlinked.rb index 42b3bc0eb69b..4c28875e6936 100644 --- a/lib/cask/artifact/symlinked.rb +++ b/lib/cask/artifact/symlinked.rb @@ -1,17 +1,35 @@ class Cask::Artifact::Symlinked < Cask::Artifact::Base + def islink?(path) + path.symlink? + end + + def link_action(source, target) + @command.run!('/bin/ln', :args => ['-hfs', source, target]) + end + + def unlink_action(target) + target.delete + end + + def link_name + 'symlink' + end + def link(artifact_relative_path) source = @cask.destination_path.join(artifact_relative_path) - target = Cask.send(self.class.artifact_dirmethod).join(source.basename) + target_dir = Cask.send(self.class.artifact_dirmethod) + Dir.mkdir(target_dir) unless target_dir.exist? + target = target_dir.join(source.basename) return unless preflight_checks(source, target) ohai "Linking #{self.class.artifact_english_name} '#{source.basename}' to '#{target}'" - @command.run!('/bin/ln', :args => ['-hfs', source, target]) + self.link_action(source, target) end def unlink(artifact_relative_path) linked_path = Cask.send(self.class.artifact_dirmethod).join(Pathname(artifact_relative_path).basename) - if linked_path.exist? && linked_path.symlink? + if linked_path.exist? && self.islink?(linked_path) ohai "Removing #{self.class.artifact_english_name} link: '#{linked_path}'" - linked_path.delete + self.unlink_action(linked_path) end end @@ -24,12 +42,12 @@ def uninstall end def preflight_checks(source, target) - if target.exist? && !target.symlink? + if target.exist? && !self.islink?(target) ohai "It seems there is already #{self.class.artifact_english_article} #{self.class.artifact_english_name} at '#{target}'; not linking." return false end unless source.exist? - raise "it seems the symlink source is not there: '#{source}'" + raise "it seems the #{self.link_name} source is not there: '#{source}'" end true end diff --git a/lib/cask/artifact/widget.rb b/lib/cask/artifact/widget.rb index 9f49edbbf348..3dfb1a49ddce 100644 --- a/lib/cask/artifact/widget.rb +++ b/lib/cask/artifact/widget.rb @@ -1,2 +1,2 @@ -class Cask::Artifact::Widget < Cask::Artifact::Symlinked +class Cask::Artifact::Widget < Cask::Artifact::Hardlinked end