Skip to content

Commit

Permalink
Change hasQuarantineAttribute(_:) to hasAttribute(_:_:) (#414)
Browse files Browse the repository at this point in the history
Makes the attribute reading API more generic.
  • Loading branch information
MaxDesiatov authored May 10, 2023
1 parent 1296d2f commit b3d8257
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
40 changes: 36 additions & 4 deletions Sources/TSCBasic/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ public enum FileMode: Sendable {
}
}

/// Extended file system attributes that can applied to a given file path. See also ``FileSystem/hasAttribute(_:_:)``.
public enum FileSystemAttribute: RawRepresentable {
#if canImport(Darwin)
case quarantine
#endif

public init?(rawValue: String) {
switch rawValue {
#if canImport(Darwin)
case "com.apple.quarantine":
self = .quarantine
#endif
default:
return nil
}
}

public var rawValue: String {
switch self {
#if canImport(Darwin)
case .quarantine:
return "com.apple.quarantine"
#endif
}
}
}

// FIXME: Design an asynchronous story?
//
/// Abstracted access to file system operations.
Expand Down Expand Up @@ -169,10 +196,13 @@ public protocol FileSystem: Sendable {
/// Check whether the given path is accessible and writable.
func isWritable(_ path: AbsolutePath) -> Bool

/// Returns `true` if a given path has a quarantine attribute applied if when file system supports this attribute.
/// Returns `false` if such attribute is not applied or it isn't supported.
@available(*, deprecated, message: "use `hasAttribute(_:_:)` instead")
func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool

/// Returns `true` if a given path has an attribute with a given name applied when file system supports this
/// attribute. Returns `false` if such attribute is not applied or it isn't supported.
func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool

// FIXME: Actual file system interfaces will allow more efficient access to
// more data than just the name here.
//
Expand Down Expand Up @@ -307,6 +337,8 @@ public extension FileSystem {
}

func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool { false }

func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool { false }
}

/// Concrete FileSystem implementation which communicates with the local file system.
Expand Down Expand Up @@ -355,9 +387,9 @@ private struct LocalFileSystem: FileSystem {
return FileInfo(attrs)
}

func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool {
func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool {
#if canImport(Darwin)
let bufLength = getxattr(path.pathString, "com.apple.quarantine", nil, 0, 0, 0)
let bufLength = getxattr(path.pathString, name.rawValue, nil, 0, 0, 0)

return bufLength > 0
#else
Expand Down
10 changes: 5 additions & 5 deletions Tests/TSCBasicTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -861,14 +861,14 @@ class FileSystemTests: XCTestCase {
}

#if canImport(Darwin)
func testQuarantineAttribute() throws {
func testHasAttribute() throws {
try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
let filePath = tempDir.appending(component: "quarantined")
try localFileSystem.writeFileContents(filePath, bytes: "")
try Process.checkNonZeroExit(args: "xattr", "-w", "com.apple.quarantine", "foo", filePath.pathString)
XCTAssertTrue(localFileSystem.hasQuarantineAttribute(filePath))
try Process.checkNonZeroExit(args: "xattr", "-d", "com.apple.quarantine", filePath.pathString)
XCTAssertFalse(localFileSystem.hasQuarantineAttribute(filePath))
try Process.checkNonZeroExit(args: "xattr", "-w", FileSystemAttribute.quarantine.rawValue, "foo", filePath.pathString)
XCTAssertTrue(localFileSystem.hasAttribute(.quarantine, filePath))
try Process.checkNonZeroExit(args: "xattr", "-d", FileSystemAttribute.quarantine.rawValue, filePath.pathString)
XCTAssertFalse(localFileSystem.hasAttribute(.quarantine, filePath))
}
}
#endif
Expand Down

0 comments on commit b3d8257

Please sign in to comment.