Reset extended attributes of the base directories.

This commit is contained in:
JBYoshi 2023-04-03 20:21:37 -05:00
parent 8c4f29d983
commit 3f8998a4fc
No known key found for this signature in database
GPG Key ID: AE4430116622D05D
3 changed files with 98 additions and 1 deletions

View File

@ -2,6 +2,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cask/artifact/relocated" require "cask/artifact/relocated"
require "cask/quarantine"
module Cask module Cask
module Artifact module Artifact
@ -97,7 +98,9 @@ module Cask
command.run!("/bin/cp", args: ["-pR", "#{source}/*", "#{source}/.*", "#{target}/"], command.run!("/bin/cp", args: ["-pR", "#{source}/*", "#{source}/.*", "#{target}/"],
sudo: true) sudo: true)
end end
# TODO: copy extended attributes unless Quarantine.copy_xattrs(source, target)
opoo "Unable to transfer extended attributes on the root directory"
end
source.rmtree source.rmtree
elsif target.dirname.writable? elsif target.dirname.writable?
FileUtils.move(source, target) FileUtils.move(source, target)

View File

@ -14,6 +14,7 @@ module Cask
QUARANTINE_ATTRIBUTE = "com.apple.quarantine" QUARANTINE_ATTRIBUTE = "com.apple.quarantine"
QUARANTINE_SCRIPT = (HOMEBREW_LIBRARY_PATH/"cask/utils/quarantine.swift").freeze QUARANTINE_SCRIPT = (HOMEBREW_LIBRARY_PATH/"cask/utils/quarantine.swift").freeze
COPY_XATTRS_SCRIPT = (HOMEBREW_LIBRARY_PATH/"cask/utils/copy-xattrs.swift").freeze
def self.swift def self.swift
@swift ||= DevelopmentTools.locate("swift") @swift ||= DevelopmentTools.locate("swift")
@ -174,5 +175,18 @@ module Cask
raise CaskQuarantinePropagationError.new(to, quarantiner.stderr) raise CaskQuarantinePropagationError.new(to, quarantiner.stderr)
end end
def self.copy_xattrs(from, to)
odebug "Copying xattrs from #{from} to #{to}"
copier = system_command!(swift,
args: [
*swift_target_args,
COPY_XATTRS_SCRIPT,
from,
to,
])
copier.success?
end
end end
end end

View File

@ -0,0 +1,80 @@
#!/usr/bin/swift
import Foundation
struct SwiftErr: TextOutputStream {
public static var stream = SwiftErr()
mutating func write(_ string: String) {
fputs(string, stderr)
}
}
guard CommandLine.arguments.count >= 3 else {
print("Usage: swift copy-xattrs.swift <source> <dest>")
exit(2)
}
CommandLine.arguments[2].withCString { destPath in
let destNamesLen = listxattr(destPath, nil, 0, 0)
if destNamesLen == -1 {
print("listxattr for destination failed: \(errno)", to: &SwiftErr.stream)
exit(1)
}
let destNamesBuf = UnsafeMutablePointer<Int8>.allocate(capacity: destNamesLen)
if listxattr(destPath, destNamesBuf, destNamesLen, 0) != destNamesLen {
print("Attributes changed during system call", to: &SwiftErr.stream)
exit(1)
}
var destNamesIdx = 0
while destNamesIdx < destNamesLen {
let attribute = destNamesBuf + destNamesIdx
if removexattr(destPath, attribute, 0) != 0 {
print("removexattr for \(String(cString: attribute)) failed: \(errno)", to: &SwiftErr.stream)
exit(1)
}
destNamesIdx += strlen(attribute) + 1
}
destNamesBuf.deallocate()
CommandLine.arguments[1].withCString { sourcePath in
let sourceNamesLen = listxattr(sourcePath, nil, 0, 0)
if sourceNamesLen == -1 {
print("listxattr for source failed: \(errno)", to: &SwiftErr.stream)
exit(1)
}
let sourceNamesBuf = UnsafeMutablePointer<Int8>.allocate(capacity: sourceNamesLen)
if listxattr(sourcePath, sourceNamesBuf, sourceNamesLen, 0) != sourceNamesLen {
print("Attributes changed during system call", to: &SwiftErr.stream)
exit(1)
}
var sourceNamesIdx = 0
while sourceNamesIdx < sourceNamesLen {
let attribute = sourceNamesBuf + sourceNamesIdx
let valueLen = getxattr(sourcePath, attribute, nil, 0, 0, 0)
if valueLen == -1 {
print("getxattr for \(String(cString: attribute)) failed: \(errno)", to: &SwiftErr.stream)
exit(1)
}
let valueBuf = UnsafeMutablePointer<Int8>.allocate(capacity: valueLen)
if getxattr(sourcePath, attribute, valueBuf, valueLen, 0, 0) != valueLen {
print("Attributes changed during system call", to: &SwiftErr.stream)
exit(1)
}
if setxattr(destPath, attribute, valueBuf, valueLen, 0, 0) != 0 {
print("setxattr for \(String(cString: attribute)) failed: \(errno)", to: &SwiftErr.stream)
exit(1)
}
valueBuf.deallocate()
sourceNamesIdx += strlen(attribute) + 1
}
sourceNamesBuf.deallocate()
}
}