diff --git a/Library/Homebrew/cask/artifact/abstract_uninstall.rb b/Library/Homebrew/cask/artifact/abstract_uninstall.rb index 789306bb99..1a03781641 100644 --- a/Library/Homebrew/cask/artifact/abstract_uninstall.rb +++ b/Library/Homebrew/cask/artifact/abstract_uninstall.rb @@ -362,6 +362,8 @@ module Cask def trash_paths(*paths, command: nil, **_) return if paths.empty? + raise CaskError, "Some files are owned by `root` and cannot be moved to the user's Trash." unless paths.all?(&:writable?) + result = command.run!("/usr/bin/swift", args: [TRASH_SCRIPT, *paths]) # Remove AppleScript's automatic newline. diff --git a/Library/Homebrew/cask/utils/trash.swift b/Library/Homebrew/cask/utils/trash.swift index cddbd686cc..91d948fa42 100644 --- a/Library/Homebrew/cask/utils/trash.swift +++ b/Library/Homebrew/cask/utils/trash.swift @@ -2,6 +2,11 @@ import Foundation +struct swifterr: TextOutputStream { + public static var stream = swifterr() + mutating func write(_ string: String) { fputs(string, stderr) } +} + if (CommandLine.arguments.count < 2) { exit(2) } @@ -12,10 +17,11 @@ for item in CommandLine.arguments[1...] { do { let path: URL = URL(fileURLWithPath: item) try manager.trashItem(at: path, resultingItemURL: nil) - print(path) + print(path, terminator: "\0") } catch { - print("\0") + print(error.localizedDescription, to: &swifterr.stream) + exit(1) } }