Skip to content

Commit

Permalink
Add public enum FileSystemAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxDesiatov committed May 10, 2023
1 parent 4dc25ad commit e93cb40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
15 changes: 11 additions & 4 deletions Sources/TSCBasic/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ public enum FileMode: Sendable {
}
}

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

// FIXME: Design an asynchronous story?
//
/// Abstracted access to file system operations.
Expand Down Expand Up @@ -171,7 +178,7 @@ public protocol FileSystem: Sendable {

/// 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: String, _ path: AbsolutePath) -> Bool
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 @@ -306,7 +313,7 @@ public extension FileSystem {
throw FileSystemError(.unsupported, path)
}

func hasAttribute(name: String, _ 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 +362,9 @@ private struct LocalFileSystem: FileSystem {
return FileInfo(attrs)
}

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

return bufLength > 0
#else
Expand Down
9 changes: 4 additions & 5 deletions Tests/TSCBasicTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -864,12 +864,11 @@ class FileSystemTests: XCTestCase {
func testHasAttribute() throws {
try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
let filePath = tempDir.appending(component: "quarantined")
let attributeName = "com.apple.quarantine"
try localFileSystem.writeFileContents(filePath, bytes: "")
try Process.checkNonZeroExit(args: "xattr", "-w", attributeName, "foo", filePath.pathString)
XCTAssertTrue(localFileSystem.hasAttribute(name: attributeName, filePath))
try Process.checkNonZeroExit(args: "xattr", "-d", attributeName, filePath.pathString)
XCTAssertFalse(localFileSystem.hasAttribute(name: attributeName, 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 e93cb40

Please sign in to comment.