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
This commit is contained in:
L. E. Segovia 2019-10-01 11:12:23 +00:00
parent db18713c8a
commit f980ac82b9
No known key found for this signature in database
GPG Key ID: D5D1DC48B52B7AD5
2 changed files with 10 additions and 2 deletions

View File

@ -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.

View File

@ -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)
}
}