Skip to content

Commit 22c4a91

Browse files
authored
Add hasQuarantineAttribute to FileSystem (#411)
This new function returns `true` if a given path has a quarantine attribute applied if when file system supports this attribute, and returns `false` if such attribute is not applied or it isn't supported.
1 parent ae57c40 commit 22c4a91

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Sources/TSCBasic/FileSystem.swift

+16
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ public protocol FileSystem: Sendable {
168168
/// Check whether the given path is accessible and writable.
169169
func isWritable(_ path: AbsolutePath) -> Bool
170170

171+
/// Returns `true` if a given path has a quarantine attribute applied if when file system supports this attribute.
172+
/// Returns `false` if such attribute is not applied or it isn't supported.
173+
func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool
174+
171175
// FIXME: Actual file system interfaces will allow more efficient access to
172176
// more data than just the name here.
173177
//
@@ -293,6 +297,8 @@ public extension FileSystem {
293297
func withLock<T>(on path: AbsolutePath, type: FileLock.LockType, _ body: () throws -> T) throws -> T {
294298
throw FileSystemError(.unsupported, path)
295299
}
300+
301+
func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool { false }
296302
}
297303

298304
/// Concrete FileSystem implementation which communicates with the local file system.
@@ -341,6 +347,16 @@ private struct LocalFileSystem: FileSystem {
341347
return FileInfo(attrs)
342348
}
343349

350+
func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool {
351+
#if canImport(Darwin)
352+
let bufLength = getxattr(path.pathString, "com.apple.quarantine", nil, 0, 0, 0)
353+
354+
return bufLength > 0
355+
#else
356+
return false
357+
#endif
358+
}
359+
344360
var currentWorkingDirectory: AbsolutePath? {
345361
let cwdStr = FileManager.default.currentDirectoryPath
346362

Tests/TSCBasicTests/FileSystemTests.swift

+13
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,19 @@ class FileSystemTests: XCTestCase {
860860
try _testFileSystemFileLock(fileSystem: fs, fileA: fileA, fileB: fileB, lockFile: lockFile)
861861
}
862862

863+
#if canImport(Darwin)
864+
func testQuarantineAttribute() throws {
865+
try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
866+
let filePath = tempDir.appending(component: "quarantined")
867+
try localFileSystem.writeFileContents(filePath, bytes: "")
868+
try Process.checkNonZeroExit(args: "xattr", "-w", "com.apple.quarantine", "foo", filePath.pathString)
869+
XCTAssertTrue(localFileSystem.hasQuarantineAttribute(filePath))
870+
try Process.checkNonZeroExit(args: "xattr", "-d", "com.apple.quarantine", filePath.pathString)
871+
XCTAssertFalse(localFileSystem.hasQuarantineAttribute(filePath))
872+
}
873+
}
874+
#endif
875+
863876
private func _testFileSystemFileLock(fileSystem fs: FileSystem, fileA: AbsolutePath, fileB: AbsolutePath, lockFile: AbsolutePath) throws {
864877
// write initial value, since reader may start before writers and files would not exist
865878
try fs.writeFileContents(fileA, bytes: "0")

0 commit comments

Comments
 (0)