From f980ac82b98517d0aa62cd0a31ee1fa910f7f388 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" <13498015+amyspark@users.noreply.github.com> Date: Tue, 1 Oct 2019 11:12:23 +0000 Subject: [PATCH 1/5] Cask: refuse to trash root-owned files These files cannot be trashed from CLI without sudo, which trashes them instead to root's Trash. AppleScript isn't of use here because it will break Travis (as it will fail when asking for admin permissions). Fixes homebrew/homebrew-cask#69897 --- Library/Homebrew/cask/artifact/abstract_uninstall.rb | 2 ++ Library/Homebrew/cask/utils/trash.swift | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) 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) } } From aa85bec73ed6a2a729e6cea3cc37a24a43299d54 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" <13498015+amyspark@users.noreply.github.com> Date: Wed, 2 Oct 2019 00:25:38 +0000 Subject: [PATCH 2/5] Fix style issues --- Library/Homebrew/cask/artifact/abstract_uninstall.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/artifact/abstract_uninstall.rb b/Library/Homebrew/cask/artifact/abstract_uninstall.rb index 1a03781641..826777c378 100644 --- a/Library/Homebrew/cask/artifact/abstract_uninstall.rb +++ b/Library/Homebrew/cask/artifact/abstract_uninstall.rb @@ -362,7 +362,7 @@ 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?) + raise CaskError, "Some files cannot be moved to the user's Trash." unless paths.all?(&:writable?) result = command.run!("/usr/bin/swift", args: [TRASH_SCRIPT, *paths]) From b9a3884ebfb468988dcecdd8997fe8ab8cb97587 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" <13498015+amyspark@users.noreply.github.com> Date: Wed, 2 Oct 2019 13:36:05 +0000 Subject: [PATCH 3/5] Cask: output files requiring sudo to console --- Library/Homebrew/cask/artifact/abstract_uninstall.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cask/artifact/abstract_uninstall.rb b/Library/Homebrew/cask/artifact/abstract_uninstall.rb index 826777c378..a43358d029 100644 --- a/Library/Homebrew/cask/artifact/abstract_uninstall.rb +++ b/Library/Homebrew/cask/artifact/abstract_uninstall.rb @@ -362,9 +362,13 @@ module Cask def trash_paths(*paths, command: nil, **_) return if paths.empty? - raise CaskError, "Some files cannot be moved to the user's Trash." unless paths.all?(&:writable?) + trashable, untrashable = paths.split(&:writable?) + unless untrashable.empty? + opoo "These files cannot be moved to the user's Trash: " + puts untrashable + end - result = command.run!("/usr/bin/swift", args: [TRASH_SCRIPT, *paths]) + result = command.run!("/usr/bin/swift", args: [TRASH_SCRIPT, *trashable]) # Remove AppleScript's automatic newline. result.tap { |r| r.stdout.sub!(/\n$/, "") } From 70c49809fed548a2e0686ed11ee40ebcd7a5c389 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" <13498015+amyspark@users.noreply.github.com> Date: Wed, 2 Oct 2019 14:09:26 +0000 Subject: [PATCH 4/5] Cask: typo in trash_paths --- Library/Homebrew/cask/artifact/abstract_uninstall.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/artifact/abstract_uninstall.rb b/Library/Homebrew/cask/artifact/abstract_uninstall.rb index a43358d029..de5044667a 100644 --- a/Library/Homebrew/cask/artifact/abstract_uninstall.rb +++ b/Library/Homebrew/cask/artifact/abstract_uninstall.rb @@ -362,7 +362,7 @@ module Cask def trash_paths(*paths, command: nil, **_) return if paths.empty? - trashable, untrashable = paths.split(&:writable?) + trashable, untrashable = paths.partition(&:writable?) unless untrashable.empty? opoo "These files cannot be moved to the user's Trash: " puts untrashable From 1ddafee6145e53ba038fbb465f1f0ef921553dd3 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" <13498015+amyspark@users.noreply.github.com> Date: Thu, 3 Oct 2019 20:16:01 +0000 Subject: [PATCH 5/5] Cask: address @reitermarkus's comments --- Library/Homebrew/cask/artifact/abstract_uninstall.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cask/artifact/abstract_uninstall.rb b/Library/Homebrew/cask/artifact/abstract_uninstall.rb index de5044667a..19d8476b7e 100644 --- a/Library/Homebrew/cask/artifact/abstract_uninstall.rb +++ b/Library/Homebrew/cask/artifact/abstract_uninstall.rb @@ -364,8 +364,8 @@ module Cask trashable, untrashable = paths.partition(&:writable?) unless untrashable.empty? - opoo "These files cannot be moved to the user's Trash: " - puts untrashable + opoo "These files cannot be moved to the user's Trash:" + $stderr.puts untrashable end result = command.run!("/usr/bin/swift", args: [TRASH_SCRIPT, *trashable])