Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix widget install and uninstall using hardlinks. #2312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/cask/artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 18 additions & 0 deletions lib/cask/artifact/hardlinked.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 24 additions & 6 deletions lib/cask/artifact/symlinked.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/cask/artifact/widget.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
class Cask::Artifact::Widget < Cask::Artifact::Symlinked
class Cask::Artifact::Widget < Cask::Artifact::Hardlinked
end