Skip to content

Commit

Permalink
Don't remove cask directories when upgrading.
Browse files Browse the repository at this point in the history
  • Loading branch information
JBYoshi committed Apr 3, 2023
1 parent a883970 commit dbc76ac
Showing 1 changed file with 57 additions and 27 deletions.
84 changes: 57 additions & 27 deletions Library/Homebrew/cask/artifact/moved.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,45 @@ def move(adopt: false, force: false, verbose: false, command: nil, **options)
raise CaskError, "It seems the #{self.class.english_name} source '#{source}' is not there."
end

if Utils.path_occupied?(target)
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."
if Utils.path_occupied?(target) && (!target.directory? || !target.children.empty? || !source.directory?)
if upgrade && target.directory? && target.children.empty?
# If the target is an empty directory, it was probably an upgrade that only removed directory contents.
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

# Remove the source as we don't need to move it to the target location
source.rmtree
message = "It seems there is already #{self.class.english_article} " \
"#{self.class.english_name} at '#{target}'"
raise CaskError, "#{message}." unless force

return post_move(command)
opoo "#{message}; overwriting."
delete(target, force: force, command: command, **options)
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

ohai "Moving #{self.class.english_name} '#{source.basename}' to '#{target}'"
Expand All @@ -79,7 +90,16 @@ def move(adopt: false, force: false, verbose: false, command: nil, **options)
end
end

if target.dirname.writable?
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
# TODO: copy extended attributes
source.rmtree
elsif target.dirname.writable?
FileUtils.move(source, target)
else
# default sudo user isn't necessarily able to write to Homebrew's locations
Expand Down Expand Up @@ -125,13 +145,23 @@ def move_back(skip: false, force: false, command: nil, **options)
delete(target, force: force, command: command, **options)
end

def delete(target, force: false, command: nil, **_)
def delete(target, force: false, upgrade: false, command: nil, **_)
ohai "Removing #{self.class.english_name} '#{target}'"
raise CaskError, "Cannot remove undeletable #{self.class.english_name}." if MacOS.undeletable?(target)

return unless Utils.path_occupied?(target)

if target.parent.writable? && !force
if upgrade && 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
elif target.parent.writable? && !force
target.rmtree
else
Utils.gain_permissions_remove(target, command: command)
Expand Down

0 comments on commit dbc76ac

Please sign in to comment.