Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change hasQuarantineAttribute(_:) to hasAttribute(_:_:) #414

Merged
merged 6 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 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 @@ -169,9 +176,9 @@ 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.
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 @@ -306,7 +313,7 @@ public extension FileSystem {
throw FileSystemError(.unsupported, path)
}

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

func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool {
func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool {
#if canImport(Darwin)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like getxattr is available with import Glibc so I'm not adding a check for Linux in this condition for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay; that seems like a different bug to resolve separately.

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