From 4123b05efac890b8263093a6d64cac3472c681f0 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Sat, 4 Jan 2014 12:39:37 -0500 Subject: [PATCH] use hard links for fonts instead of symlinks per an issue reported by @jgarber623. Recast for compatibility with #2300. --- lib/cask/artifact.rb | 1 + lib/cask/artifact/font.rb | 2 +- lib/cask/artifact/hardlinked.rb | 14 ++++++++++++++ lib/cask/artifact/symlinked.rb | 24 ++++++++++++++++++------ test/cask/artifact/app_test.rb | 2 +- 5 files changed, 35 insertions(+), 8 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/font.rb b/lib/cask/artifact/font.rb index 43019285b794..3410dcfa5b99 100644 --- a/lib/cask/artifact/font.rb +++ b/lib/cask/artifact/font.rb @@ -1,2 +1,2 @@ -class Cask::Artifact::Font < Cask::Artifact::Symlinked +class Cask::Artifact::Font < Cask::Artifact::Hardlinked end diff --git a/lib/cask/artifact/hardlinked.rb b/lib/cask/artifact/hardlinked.rb new file mode 100644 index 000000000000..ed5cdaf10bb3 --- /dev/null +++ b/lib/cask/artifact/hardlinked.rb @@ -0,0 +1,14 @@ +class Cask::Artifact::Hardlinked < Cask::Artifact::Symlinked + def self.islink?(path) + return false unless path.respond_to?(:stat) + path.stat.nlink > 1 + end + + def self.link_type_english_name + 'Hardlink' + end + + def create_filesystem_link(source, target) + @command.run!('/bin/ln', :args => ['-hf', source, target]) + end +end diff --git a/lib/cask/artifact/symlinked.rb b/lib/cask/artifact/symlinked.rb index 42b3bc0eb69b..27951d6004b9 100644 --- a/lib/cask/artifact/symlinked.rb +++ b/lib/cask/artifact/symlinked.rb @@ -1,16 +1,28 @@ class Cask::Artifact::Symlinked < Cask::Artifact::Base + def self.islink?(path) + path.symlink? + end + + def self.link_type_english_name + 'Symlink' + end + + def create_filesystem_link(source, target) + @command.run!('/bin/ln', :args => ['-hfs', source, target]) + end + def link(artifact_relative_path) source = @cask.destination_path.join(artifact_relative_path) target = Cask.send(self.class.artifact_dirmethod).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]) + ohai "#{self.class.link_type_english_name}ing #{self.class.artifact_english_name} '#{source.basename}' to '#{target}'" + create_filesystem_link(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? - ohai "Removing #{self.class.artifact_english_name} link: '#{linked_path}'" + if linked_path.exist? && self.class.islink?(linked_path) + ohai "Removing #{self.class.artifact_english_name} #{self.class.link_type_english_name.downcase}: '#{linked_path}'" linked_path.delete end end @@ -24,12 +36,12 @@ def uninstall end def preflight_checks(source, target) - if target.exist? && !target.symlink? + if target.exist? && !self.class.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.class.link_type_english_name.downcase} source is not there: '#{source}'" end true end diff --git a/test/cask/artifact/app_test.rb b/test/cask/artifact/app_test.rb index 1fb1da28172d..2fcae9027d45 100644 --- a/test/cask/artifact/app_test.rb +++ b/test/cask/artifact/app_test.rb @@ -83,7 +83,7 @@ TestHelper.must_output(self, lambda { Cask::Artifact::App.new(cask).install - }, "==> Linking App 'Caffeine.app' to '#{Cask.appdir.join('Caffeine.app')}'") + }, "==> Symlinking App 'Caffeine.app' to '#{Cask.appdir.join('Caffeine.app')}'") File.readlink(Cask.appdir/'Caffeine.app').wont_equal '/tmp' end