From 04493e2dde87024efa94aaba6c201c62d9e0d2cf Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 23 Oct 2019 05:21:21 +0200 Subject: [PATCH] Fix `trash.swift` without arguments. --- .../cask/artifact/abstract_uninstall.rb | 31 +++++++++++++------ Library/Homebrew/cask/utils.rb | 3 ++ Library/Homebrew/cask/utils/quarantine.swift | 0 Library/Homebrew/cask/utils/trash.swift | 29 +++++++++-------- .../artifact/shared_examples/uninstall_zap.rb | 4 +-- 5 files changed, 43 insertions(+), 24 deletions(-) mode change 100644 => 100755 Library/Homebrew/cask/utils/quarantine.swift mode change 100644 => 100755 Library/Homebrew/cask/utils/trash.swift diff --git a/Library/Homebrew/cask/artifact/abstract_uninstall.rb b/Library/Homebrew/cask/artifact/abstract_uninstall.rb index 9278bff97da2e..a58552f7866fd 100644 --- a/Library/Homebrew/cask/artifact/abstract_uninstall.rb +++ b/Library/Homebrew/cask/artifact/abstract_uninstall.rb @@ -24,8 +24,6 @@ class AbstractUninstall < AbstractArtifact :rmdir, ].freeze - TRASH_SCRIPT = (HOMEBREW_LIBRARY_PATH/"cask/utils/trash.swift").freeze - def self.from_args(cask, **directives) new(cask, directives) end @@ -360,16 +358,31 @@ def uninstall_trash(*paths, **options) def trash_paths(*paths, command: nil, **_) return if paths.empty? - trashable, untrashable = paths.partition(&:writable?) - unless untrashable.empty? - opoo "These files cannot be moved to the user's Trash:" - $stderr.puts untrashable + stdout, stderr, = system_command HOMEBREW_LIBRARY_PATH/"cask/utils/trash.swift", + args: paths, + print_stderr: false + + trashed = stdout.split(":").sort + untrashable = stderr.split(":").sort + + return trashed, untrashable if untrashable.empty? + + untrashable.delete_if do |path| + Utils.gain_permissions(path, ["-R"], SystemCommand) do + system_command! HOMEBREW_LIBRARY_PATH/"cask/utils/trash.swift", + args: [path], + print_stderr: false + end + + true + rescue + false end - result = command.run!("/usr/bin/swift", args: [TRASH_SCRIPT, *trashable]) + opoo "The following files could not trashed, please do so manually:" + $stderr.puts untrashable - # Remove AppleScript's automatic newline. - result.tap { |r| r.stdout.sub!(/\n$/, "") } + [trashed, untrashable] end def uninstall_rmdir(*directories, command: nil, **_) diff --git a/Library/Homebrew/cask/utils.rb b/Library/Homebrew/cask/utils.rb index 69bdd965260c9..a071c9c69374d 100644 --- a/Library/Homebrew/cask/utils.rb +++ b/Library/Homebrew/cask/utils.rb @@ -49,6 +49,7 @@ def self.gain_permissions(path, command_args, command) tried_permissions = true retry # rmtree end + unless tried_ownership # in case of ownership problems # TODO: Further examine files to see if ownership is the problem @@ -62,6 +63,8 @@ def self.gain_permissions(path, command_args, command) tried_permissions = false retry # rmtree end + + raise end end diff --git a/Library/Homebrew/cask/utils/quarantine.swift b/Library/Homebrew/cask/utils/quarantine.swift old mode 100644 new mode 100755 diff --git a/Library/Homebrew/cask/utils/trash.swift b/Library/Homebrew/cask/utils/trash.swift old mode 100644 new mode 100755 index 91d948fa42a2f..a55c0dacd93f5 --- a/Library/Homebrew/cask/utils/trash.swift +++ b/Library/Homebrew/cask/utils/trash.swift @@ -2,27 +2,30 @@ import Foundation -struct swifterr: TextOutputStream { - public static var stream = swifterr() - mutating func write(_ string: String) { fputs(string, stderr) } +extension FileHandle : TextOutputStream { + public func write(_ string: String) { + self.write(string.data(using: .utf8)!) + } } -if (CommandLine.arguments.count < 2) { - exit(2) -} +var stderr = FileHandle.standardError let manager: FileManager = FileManager() +var success = true + for item in CommandLine.arguments[1...] { do { let path: URL = URL(fileURLWithPath: item) - try manager.trashItem(at: path, resultingItemURL: nil) - print(path, terminator: "\0") - } - catch { - print(error.localizedDescription, to: &swifterr.stream) - exit(1) + var trashedPath: NSURL! + try manager.trashItem(at: path, resultingItemURL: &trashedPath) + print((trashedPath as URL).path, terminator: ":") + } catch { + print(item, terminator: ":", to: &stderr) + success = false } } -exit(0) +guard success else { + exit(1) +} diff --git a/Library/Homebrew/test/cask/artifact/shared_examples/uninstall_zap.rb b/Library/Homebrew/test/cask/artifact/shared_examples/uninstall_zap.rb index 209be079fab5d..49d86dae22e70 100644 --- a/Library/Homebrew/test/cask/artifact/shared_examples/uninstall_zap.rb +++ b/Library/Homebrew/test/cask/artifact/shared_examples/uninstall_zap.rb @@ -199,8 +199,8 @@ before do allow_any_instance_of(Cask::Artifact::AbstractUninstall).to receive(:trash_paths) .and_wrap_original do |method, *args| - method.call(*args).tap do |result| - FileUtils.rm_rf result.stdout.split("\0") + method.call(*args).tap do |trashed, _| + FileUtils.rm_r trashed end end end