177 lines
6.0 KiB
Ruby
Raw Normal View History

# typed: true
# frozen_string_literal: true
require "cask/artifact/relocated"
require "cask/quarantine"
2016-08-18 22:11:42 +03:00
2018-09-06 08:29:14 +02:00
module Cask
2016-09-24 13:52:43 +02:00
module Artifact
# Superclass for all artifacts that are installed by moving them to the target location.
2020-08-19 10:23:41 +02:00
#
# @api private
2016-09-24 13:52:43 +02:00
class Moved < Relocated
2020-10-20 12:03:48 +02:00
extend T::Sig
sig { returns(String) }
2016-09-24 13:52:43 +02:00
def self.english_description
"#{english_name}s"
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
def install_phase(**options)
move(**options)
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
def uninstall_phase(**options)
move_back(**options)
end
def summarize_installed
if target.exist?
"#{printable_target} (#{target.abv})"
else
Formatter.error(printable_target, label: "Missing #{self.class.english_name}")
end
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
private
2016-08-18 22:11:42 +03:00
def move(adopt: false, force: false, verbose: false, upgrade_or_reinstall: false, reinstall: false,
command: nil, **options)
unless source.exist?
raise CaskError, "It seems the #{self.class.english_name} source '#{source}' is not there."
end
2016-09-24 13:52:43 +02:00
if Utils.path_occupied?(target)
if upgrade_or_reinstall && target.directory? && target.children.empty?
# An upgrade removed the directory contents but left the directory itself (see below).
unless source.directory?
if target.parent.writable? && !force
target.rmdir
else
Utils.gain_permissions_remove(target, command: command)
end
end
else
if adopt
ohai "Adopting existing #{self.class.english_name} at '#{target}'"
same = command.run(
"/usr/bin/diff",
args: ["--recursive", "--brief", source, target],
verbose: verbose,
print_stdout: verbose,
).success?
unless same
raise CaskError,
"It seems the existing #{self.class.english_name} is different from " \
"the one being installed."
end
# Remove the source as we don't need to move it to the target location
source.rmtree
return post_move(command)
end
message = "It seems there is already #{self.class.english_article} " \
"#{self.class.english_name} at '#{target}'"
raise CaskError, "#{message}." unless force
opoo "#{message}; overwriting."
delete(target, force: force, command: command, **options)
end
2016-09-24 13:52:43 +02:00
end
2017-03-10 09:33:48 +01:00
2021-01-26 15:21:24 -05:00
ohai "Moving #{self.class.english_name} '#{source.basename}' to '#{target}'"
unless target.dirname.exist?
if target.dirname.ascend.find(&:directory?).writable?
target.dirname.mkpath
else
command.run!("/bin/mkdir", args: ["-p", target.dirname], sudo: true)
end
end
if target.directory?
if target.writable?
source.children.each { |child| FileUtils.move(child, target + child.basename) }
else
command.run!("/bin/cp", args: ["-pR", "#{source}/*", "#{source}/.*", "#{target}/"],
sudo: true)
end
unless Quarantine.copy_xattrs(source, target)
opoo "Unable to transfer extended attributes on the root directory"
end
source.rmtree
elsif target.dirname.writable?
FileUtils.move(source, target)
else
# default sudo user isn't necessarily able to write to Homebrew's locations
# e.g. with runas_default set in the sudoers (5) file.
command.run!("/bin/cp", args: ["-pR", source, target], sudo: true)
source.rmtree
end
post_move(command)
end
# Performs any actions necessary after the source has been moved to the target location.
def post_move(command)
FileUtils.ln_sf target, source
add_altname_metadata(target, source.basename, command: command)
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
def move_back(skip: false, force: false, command: nil, **options)
FileUtils.rm source if source.symlink? && source.dirname.join(source.readlink) == target
if Utils.path_occupied?(source)
message = "It seems there is already #{self.class.english_article} " \
"#{self.class.english_name} at '#{source}'"
raise CaskError, "#{message}." unless force
2018-09-17 02:45:00 +02:00
opoo "#{message}; overwriting."
delete(source, force: force, command: command, **options)
end
unless target.exist?
return if skip || force
2018-09-17 02:45:00 +02:00
raise CaskError, "It seems the #{self.class.english_name} source '#{target}' is not there."
end
2021-01-26 15:21:24 -05:00
ohai "Backing #{self.class.english_name} '#{target.basename}' up to '#{source}'"
source.dirname.mkpath
# We need to preserve extended attributes between copies.
command.run!("/bin/cp", args: ["-pR", target, source], sudo: !source.parent.writable?)
2018-01-21 19:10:30 +10:00
delete(target, force: force, command: command, **options)
end
def delete(target, force: false, upgrade_or_reinstall: false, command: nil, **_)
2021-01-26 15:21:24 -05:00
ohai "Removing #{self.class.english_name} '#{target}'"
raise CaskError, "Cannot remove undeletable #{self.class.english_name}." if MacOS.undeletable?(target)
2016-09-20 15:11:33 +02:00
return unless Utils.path_occupied?(target)
if upgrade_or_reinstall && target.directory?
# If an app folder is deleted, macOS considers the app uninstalled and removes some data.
# Remove only the contents to handle this case.
target.children.each do |child|
if target.writable? && !force
child.rmtree
else
Utils.gain_permissions_remove(child, command: command)
end
end
elsif target.parent.writable? && !force
2016-09-24 13:52:43 +02:00
target.rmtree
else
Utils.gain_permissions_remove(target, command: command)
2016-09-24 13:52:43 +02:00
end
end
end
2016-08-18 22:11:42 +03:00
end
end