Skip to content

Commit b3d8257

Browse files
authored
Change hasQuarantineAttribute(_:) to hasAttribute(_:_:) (#414)
Makes the attribute reading API more generic.
1 parent 1296d2f commit b3d8257

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

Sources/TSCBasic/FileSystem.swift

+36-4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,33 @@ public enum FileMode: Sendable {
137137
}
138138
}
139139

140+
/// Extended file system attributes that can applied to a given file path. See also ``FileSystem/hasAttribute(_:_:)``.
141+
public enum FileSystemAttribute: RawRepresentable {
142+
#if canImport(Darwin)
143+
case quarantine
144+
#endif
145+
146+
public init?(rawValue: String) {
147+
switch rawValue {
148+
#if canImport(Darwin)
149+
case "com.apple.quarantine":
150+
self = .quarantine
151+
#endif
152+
default:
153+
return nil
154+
}
155+
}
156+
157+
public var rawValue: String {
158+
switch self {
159+
#if canImport(Darwin)
160+
case .quarantine:
161+
return "com.apple.quarantine"
162+
#endif
163+
}
164+
}
165+
}
166+
140167
// FIXME: Design an asynchronous story?
141168
//
142169
/// Abstracted access to file system operations.
@@ -169,10 +196,13 @@ public protocol FileSystem: Sendable {
169196
/// Check whether the given path is accessible and writable.
170197
func isWritable(_ path: AbsolutePath) -> Bool
171198

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

202+
/// Returns `true` if a given path has an attribute with a given name applied when file system supports this
203+
/// attribute. Returns `false` if such attribute is not applied or it isn't supported.
204+
func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool
205+
176206
// FIXME: Actual file system interfaces will allow more efficient access to
177207
// more data than just the name here.
178208
//
@@ -307,6 +337,8 @@ public extension FileSystem {
307337
}
308338

309339
func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool { false }
340+
341+
func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool { false }
310342
}
311343

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

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

362394
return bufLength > 0
363395
#else

Tests/TSCBasicTests/FileSystemTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -861,14 +861,14 @@ class FileSystemTests: XCTestCase {
861861
}
862862

863863
#if canImport(Darwin)
864-
func testQuarantineAttribute() throws {
864+
func testHasAttribute() throws {
865865
try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
866866
let filePath = tempDir.appending(component: "quarantined")
867867
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))
868+
try Process.checkNonZeroExit(args: "xattr", "-w", FileSystemAttribute.quarantine.rawValue, "foo", filePath.pathString)
869+
XCTAssertTrue(localFileSystem.hasAttribute(.quarantine, filePath))
870+
try Process.checkNonZeroExit(args: "xattr", "-d", FileSystemAttribute.quarantine.rawValue, filePath.pathString)
871+
XCTAssertFalse(localFileSystem.hasAttribute(.quarantine, filePath))
872872
}
873873
}
874874
#endif

0 commit comments

Comments
 (0)