Skip to content

Commit e93cb40

Browse files
committed
Add public enum FileSystemAttribute
1 parent 4dc25ad commit e93cb40

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

Sources/TSCBasic/FileSystem.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ 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: String {
142+
#if canImport(Darwin)
143+
case quarantine = "com.apple.quarantine"
144+
#endif
145+
}
146+
140147
// FIXME: Design an asynchronous story?
141148
//
142149
/// Abstracted access to file system operations.
@@ -171,7 +178,7 @@ public protocol FileSystem: Sendable {
171178

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

176183
// FIXME: Actual file system interfaces will allow more efficient access to
177184
// more data than just the name here.
@@ -306,7 +313,7 @@ public extension FileSystem {
306313
throw FileSystemError(.unsupported, path)
307314
}
308315

309-
func hasAttribute(name: String, _ path: AbsolutePath) -> Bool { false }
316+
func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool { false }
310317
}
311318

312319
/// Concrete FileSystem implementation which communicates with the local file system.
@@ -355,9 +362,9 @@ private struct LocalFileSystem: FileSystem {
355362
return FileInfo(attrs)
356363
}
357364

358-
func hasAttribute(name: String, _ path: AbsolutePath) -> Bool {
365+
func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool {
359366
#if canImport(Darwin)
360-
let bufLength = getxattr(path.pathString, name, nil, 0, 0, 0)
367+
let bufLength = getxattr(path.pathString, name.rawValue, nil, 0, 0, 0)
361368

362369
return bufLength > 0
363370
#else

Tests/TSCBasicTests/FileSystemTests.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -864,12 +864,11 @@ class FileSystemTests: XCTestCase {
864864
func testHasAttribute() throws {
865865
try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
866866
let filePath = tempDir.appending(component: "quarantined")
867-
let attributeName = "com.apple.quarantine"
868867
try localFileSystem.writeFileContents(filePath, bytes: "")
869-
try Process.checkNonZeroExit(args: "xattr", "-w", attributeName, "foo", filePath.pathString)
870-
XCTAssertTrue(localFileSystem.hasAttribute(name: attributeName, filePath))
871-
try Process.checkNonZeroExit(args: "xattr", "-d", attributeName, filePath.pathString)
872-
XCTAssertFalse(localFileSystem.hasAttribute(name: attributeName, 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))
873872
}
874873
}
875874
#endif

0 commit comments

Comments
 (0)