From 1cff419a7df2f996336f04fa5fcb76250f9dab87 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 21 Apr 2023 15:44:57 +0100 Subject: [PATCH 1/6] Change `hasQuarantineAttribute(_:)` to `hasAttribute(name:_:)` --- Sources/TSCBasic/FileSystem.swift | 14 +++++++------- Tests/TSCBasicTests/FileSystemTests.swift | 11 ++++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift index 3ae448d3..59cdca65 100644 --- a/Sources/TSCBasic/FileSystem.swift +++ b/Sources/TSCBasic/FileSystem.swift @@ -169,9 +169,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: String, _ path: AbsolutePath) -> Bool // FIXME: Actual file system interfaces will allow more efficient access to // more data than just the name here. @@ -306,7 +306,7 @@ public extension FileSystem { throw FileSystemError(.unsupported, path) } - func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool { false } + func hasAttribute(name: String, _ path: AbsolutePath) -> Bool { false } } /// Concrete FileSystem implementation which communicates with the local file system. @@ -355,9 +355,9 @@ private struct LocalFileSystem: FileSystem { return FileInfo(attrs) } - func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool { -#if canImport(Darwin) - let bufLength = getxattr(path.pathString, "com.apple.quarantine", nil, 0, 0, 0) + func hasAttribute(name: String, _ path: AbsolutePath) -> Bool { +#if canImport(Darwin) || canImport(Glibc) + let bufLength = getxattr(path.pathString, name, nil, 0, 0, 0) return bufLength > 0 #else diff --git a/Tests/TSCBasicTests/FileSystemTests.swift b/Tests/TSCBasicTests/FileSystemTests.swift index 510bd430..0a53e693 100644 --- a/Tests/TSCBasicTests/FileSystemTests.swift +++ b/Tests/TSCBasicTests/FileSystemTests.swift @@ -860,15 +860,16 @@ class FileSystemTests: XCTestCase { try _testFileSystemFileLock(fileSystem: fs, fileA: fileA, fileB: fileB, lockFile: lockFile) } -#if canImport(Darwin) +#if canImport(Darwin) || canImport(Glibc) func testQuarantineAttribute() 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", "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", 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)) } } #endif From 330c53ee689e4d32fcc02765f2e57110f6c37cde Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 21 Apr 2023 15:58:19 +0100 Subject: [PATCH 2/6] Remove `canImport(Glibc)` check --- Sources/TSCBasic/FileSystem.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift index 59cdca65..cf3c4feb 100644 --- a/Sources/TSCBasic/FileSystem.swift +++ b/Sources/TSCBasic/FileSystem.swift @@ -356,7 +356,7 @@ private struct LocalFileSystem: FileSystem { } func hasAttribute(name: String, _ path: AbsolutePath) -> Bool { -#if canImport(Darwin) || canImport(Glibc) +#if canImport(Darwin) let bufLength = getxattr(path.pathString, name, nil, 0, 0, 0) return bufLength > 0 From 4dc25adcbc00332b05d7655212798e2216125246 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 21 Apr 2023 17:13:01 +0100 Subject: [PATCH 3/6] Fix Linux test --- Tests/TSCBasicTests/FileSystemTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/TSCBasicTests/FileSystemTests.swift b/Tests/TSCBasicTests/FileSystemTests.swift index 0a53e693..a4d45cc6 100644 --- a/Tests/TSCBasicTests/FileSystemTests.swift +++ b/Tests/TSCBasicTests/FileSystemTests.swift @@ -860,8 +860,8 @@ class FileSystemTests: XCTestCase { try _testFileSystemFileLock(fileSystem: fs, fileA: fileA, fileB: fileB, lockFile: lockFile) } -#if canImport(Darwin) || canImport(Glibc) - func testQuarantineAttribute() throws { +#if canImport(Darwin) + func testHasAttribute() throws { try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in let filePath = tempDir.appending(component: "quarantined") let attributeName = "com.apple.quarantine" From e93cb40103ec6f652b371f2219daba867cebe23c Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Wed, 10 May 2023 15:10:21 +0100 Subject: [PATCH 4/6] Add `public enum FileSystemAttribute` --- Sources/TSCBasic/FileSystem.swift | 15 +++++++++++---- Tests/TSCBasicTests/FileSystemTests.swift | 9 ++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift index cf3c4feb..be38fc19 100644 --- a/Sources/TSCBasic/FileSystem.swift +++ b/Sources/TSCBasic/FileSystem.swift @@ -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. @@ -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. @@ -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. @@ -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 diff --git a/Tests/TSCBasicTests/FileSystemTests.swift b/Tests/TSCBasicTests/FileSystemTests.swift index a4d45cc6..29430b91 100644 --- a/Tests/TSCBasicTests/FileSystemTests.swift +++ b/Tests/TSCBasicTests/FileSystemTests.swift @@ -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 From 50ce94465550511f2a544dd03460816816361abc Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Wed, 10 May 2023 15:47:42 +0100 Subject: [PATCH 5/6] Fix build on non-Darwin platforms due to raw value error --- Sources/TSCBasic/FileSystem.swift | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift index be38fc19..5eff5330 100644 --- a/Sources/TSCBasic/FileSystem.swift +++ b/Sources/TSCBasic/FileSystem.swift @@ -138,10 +138,30 @@ public enum FileMode: Sendable { } /// Extended file system attributes that can applied to a given file path. See also ``FileSystem/hasAttribute(_:_:)``. -public enum FileSystemAttribute: String { +public enum FileSystemAttribute: RawRepresentable { #if canImport(Darwin) - case quarantine = "com.apple.quarantine" + 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? From f78df765d2cb6bdcc4e231b92d17975ae97b9dad Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Wed, 10 May 2023 17:06:56 +0100 Subject: [PATCH 6/6] Bring back the old function to fix build issues --- Sources/TSCBasic/FileSystem.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift index 5eff5330..023e6e43 100644 --- a/Sources/TSCBasic/FileSystem.swift +++ b/Sources/TSCBasic/FileSystem.swift @@ -196,6 +196,9 @@ public protocol FileSystem: Sendable { /// Check whether the given path is accessible and writable. func isWritable(_ path: AbsolutePath) -> Bool + @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 @@ -333,6 +336,8 @@ public extension FileSystem { throw FileSystemError(.unsupported, path) } + func hasQuarantineAttribute(_ path: AbsolutePath) -> Bool { false } + func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool { false } }