From 73baf1113d5084ed3bb60d39599a5b7f6d639215 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 9 May 2024 14:31:11 -0700 Subject: [PATCH 01/38] Retarget pending changes to 1.4.0 We decided to spend the 1.3.0 version number on a small interim release; retarget the currently pending changes to 1.4.0. --- Sources/System/MachPort.swift | 32 +++++++++++++-------------- Tests/SystemTests/MachPortTests.swift | 2 +- Utilities/expand-availability.py | 1 + 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Sources/System/MachPort.swift b/Sources/System/MachPort.swift index b3eb0a9..b356789 100644 --- a/Sources/System/MachPort.swift +++ b/Sources/System/MachPort.swift @@ -11,10 +11,10 @@ import Darwin.Mach -@available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) +@available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public protocol MachPortRight {} -@available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) +@available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) @inlinable internal func _machPrecondition( file: StaticString = #file, @@ -26,10 +26,10 @@ internal func _machPrecondition( precondition(kr == expected, file: file, line: line) } -@available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) +@available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) @frozen public enum Mach { - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public struct Port: ~Copyable { @usableFromInline internal var _name: mach_port_name_t @@ -129,7 +129,7 @@ public enum Mach { public struct SendOnceRight: MachPortRight {} } -@available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) +@available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) extension Mach.Port where RightType == Mach.ReceiveRight { /// Transfer ownership of an existing, unmanaged, but already guarded, /// Mach port right into a Mach.Port by name. @@ -154,7 +154,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// This initializer will abort if the right could not be created. /// Callers may assert that a valid right is always returned. @inlinable - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public init() { var storage: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL) _machPrecondition( @@ -177,7 +177,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// After this function completes, the Mach.Port is destroyed and no longer /// usable. @inlinable - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public consuming func relinquish( ) -> (name: mach_port_name_t, context: mach_port_context_t) { let destructured = (name: _name, context: _context) @@ -200,7 +200,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// Mach.ReceiveRights. Use relinquish() to avoid the syscall and extract /// the context value along with the port name. @inlinable - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public consuming func unguardAndRelinquish() -> mach_port_name_t { let (name, context) = self.relinquish() _machPrecondition(mach_port_unguard(mach_task_self_, name, context)) @@ -217,7 +217,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// The body block may optionally return something, which will then be /// returned to the caller of withBorrowedName. @inlinable - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public func withBorrowedName( body: (mach_port_name_t, mach_port_context_t) -> ReturnType ) -> ReturnType { @@ -231,7 +231,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// This function will abort if the right could not be created. /// Callers may assert that a valid right is always returned. @inlinable - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public func makeSendOnceRight() -> Mach.Port { // send once rights do not coalesce var newRight: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL) @@ -260,7 +260,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// This function will abort if the right could not be created. /// Callers may assert that a valid right is always returned. @inlinable - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public func makeSendRight() -> Mach.Port { let how = MACH_MSG_TYPE_MAKE_SEND @@ -278,7 +278,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// /// Each get/set of this property makes a syscall. @inlinable - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public var makeSendCount: mach_port_mscount_t { get { var status: mach_port_status = mach_port_status() @@ -307,7 +307,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight { } } -@available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) +@available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) extension Mach.Port where RightType == Mach.SendRight { /// Transfer ownership of the underlying port right to the caller. /// @@ -333,7 +333,7 @@ extension Mach.Port where RightType == Mach.SendRight { /// receiving side has been deallocated, then copySendRight() will throw /// a Mach.PortRightError.deadName error. @inlinable - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public func copySendRight() throws -> Mach.Port { let how = MACH_MSG_TYPE_COPY_SEND @@ -350,7 +350,7 @@ extension Mach.Port where RightType == Mach.SendRight { } } -@available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) +@available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) extension Mach.Port where RightType == Mach.SendOnceRight { /// Transfer ownership of the underlying port right to the caller. /// @@ -362,7 +362,7 @@ extension Mach.Port where RightType == Mach.SendOnceRight { /// After this function completes, the Mach.Port is destroyed and no longer /// usable. @inlinable - @available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) + @available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) public consuming func relinquish() -> mach_port_name_t { let name = _name discard self diff --git a/Tests/SystemTests/MachPortTests.swift b/Tests/SystemTests/MachPortTests.swift index cba558b..073da9a 100644 --- a/Tests/SystemTests/MachPortTests.swift +++ b/Tests/SystemTests/MachPortTests.swift @@ -18,7 +18,7 @@ import SystemPackage import System #endif -@available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) +@available(/*System 1.4.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) final class MachPortTests: XCTestCase { func refCountForMachPortName(name:mach_port_name_t, kind:mach_port_right_t) -> mach_port_urefs_t { var refCount:mach_port_urefs_t = .max diff --git a/Utilities/expand-availability.py b/Utilities/expand-availability.py index 26ec2de..4cb1a1b 100755 --- a/Utilities/expand-availability.py +++ b/Utilities/expand-availability.py @@ -57,6 +57,7 @@ versions = { "System 1.1.0": "macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4", "System 1.2.0": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999", "System 1.3.0": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999", + "System 1.4.0": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999", } parser = argparse.ArgumentParser(description="Expand availability macros.") From 08800445eb169b9d6ed154d9b8b8e785070342fa Mon Sep 17 00:00:00 2001 From: Mishal Shah Date: Fri, 10 May 2024 16:27:47 -0700 Subject: [PATCH 02/38] Add support for Swift CI on release/1.4.0 --- .swiftci/5_10_ubuntu2204 | 5 +++++ .swiftci/5_7_ubuntu2204 | 5 +++++ .swiftci/5_8_ubuntu2204 | 5 +++++ .swiftci/5_9_ubuntu2204 | 5 +++++ .swiftci/nightly_6_0_macos | 5 +++++ .swiftci/nightly_6_0_ubuntu2204 | 5 +++++ .swiftci/nightly_main_macos | 5 +++++ .swiftci/nightly_main_ubuntu2204 | 5 +++++ .swiftci/nightly_main_windows | 7 +++++++ 9 files changed, 47 insertions(+) create mode 100644 .swiftci/5_10_ubuntu2204 create mode 100644 .swiftci/5_7_ubuntu2204 create mode 100644 .swiftci/5_8_ubuntu2204 create mode 100644 .swiftci/5_9_ubuntu2204 create mode 100644 .swiftci/nightly_6_0_macos create mode 100644 .swiftci/nightly_6_0_ubuntu2204 create mode 100644 .swiftci/nightly_main_macos create mode 100644 .swiftci/nightly_main_ubuntu2204 create mode 100644 .swiftci/nightly_main_windows diff --git a/.swiftci/5_10_ubuntu2204 b/.swiftci/5_10_ubuntu2204 new file mode 100644 index 0000000..deb8d0b --- /dev/null +++ b/.swiftci/5_10_ubuntu2204 @@ -0,0 +1,5 @@ +LinuxSwiftPackageJob { + swift_version_tag = "5.10-jammy" + repo = "swift-system" + branch = "release/1.4.0" +} diff --git a/.swiftci/5_7_ubuntu2204 b/.swiftci/5_7_ubuntu2204 new file mode 100644 index 0000000..f9b509b --- /dev/null +++ b/.swiftci/5_7_ubuntu2204 @@ -0,0 +1,5 @@ +LinuxSwiftPackageJob { + swift_version_tag = "5.7-jammy" + repo = "swift-system" + branch = "release/1.4.0" +} diff --git a/.swiftci/5_8_ubuntu2204 b/.swiftci/5_8_ubuntu2204 new file mode 100644 index 0000000..2c259fa --- /dev/null +++ b/.swiftci/5_8_ubuntu2204 @@ -0,0 +1,5 @@ +LinuxSwiftPackageJob { + swift_version_tag = "5.8-jammy" + repo = "swift-system" + branch = "release/1.4.0" +} diff --git a/.swiftci/5_9_ubuntu2204 b/.swiftci/5_9_ubuntu2204 new file mode 100644 index 0000000..d61e746 --- /dev/null +++ b/.swiftci/5_9_ubuntu2204 @@ -0,0 +1,5 @@ +LinuxSwiftPackageJob { + swift_version_tag = "5.9-jammy" + repo = "swift-system" + branch = "release/1.4.0" +} diff --git a/.swiftci/nightly_6_0_macos b/.swiftci/nightly_6_0_macos new file mode 100644 index 0000000..5532384 --- /dev/null +++ b/.swiftci/nightly_6_0_macos @@ -0,0 +1,5 @@ +macOSSwiftPackageJob { + swift_version = "6.0" + repo = "swift-system" + branch = "release/1.4.0" +} diff --git a/.swiftci/nightly_6_0_ubuntu2204 b/.swiftci/nightly_6_0_ubuntu2204 new file mode 100644 index 0000000..8499ebb --- /dev/null +++ b/.swiftci/nightly_6_0_ubuntu2204 @@ -0,0 +1,5 @@ +LinuxSwiftPackageJob { + nightly_docker_tag = "nightly-6.0-jammy" + repo = "swift-system" + branch = "release/1.4.0" +} diff --git a/.swiftci/nightly_main_macos b/.swiftci/nightly_main_macos new file mode 100644 index 0000000..c4db723 --- /dev/null +++ b/.swiftci/nightly_main_macos @@ -0,0 +1,5 @@ +macOSSwiftPackageJob { + swift_version = "main" + repo = "swift-system" + branch = "release/1.4.0" +} diff --git a/.swiftci/nightly_main_ubuntu2204 b/.swiftci/nightly_main_ubuntu2204 new file mode 100644 index 0000000..7e1594b --- /dev/null +++ b/.swiftci/nightly_main_ubuntu2204 @@ -0,0 +1,5 @@ +LinuxSwiftPackageJob { + nightly_docker_tag = "nightly-jammy" + repo = "swift-system" + branch = "release/1.4.0" +} diff --git a/.swiftci/nightly_main_windows b/.swiftci/nightly_main_windows new file mode 100644 index 0000000..be71344 --- /dev/null +++ b/.swiftci/nightly_main_windows @@ -0,0 +1,7 @@ +WindowsSwiftPackageWithDockerImageJob { + docker_image = "swiftlang/swift:nightly-windowsservercore-1809" + repo = "swift-system" + branch = "release/1.4.0" + sub_dir = "swift-system" + label = "windows-server-2019" +} From d18980c9600139046428cae45a7a96e33136a9d0 Mon Sep 17 00:00:00 2001 From: Mishal Shah Date: Fri, 10 May 2024 16:30:41 -0700 Subject: [PATCH 03/38] [CI] Remove support for Swift 5.7 --- .swiftci/5_7_ubuntu2204 | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .swiftci/5_7_ubuntu2204 diff --git a/.swiftci/5_7_ubuntu2204 b/.swiftci/5_7_ubuntu2204 deleted file mode 100644 index f9b509b..0000000 --- a/.swiftci/5_7_ubuntu2204 +++ /dev/null @@ -1,5 +0,0 @@ -LinuxSwiftPackageJob { - swift_version_tag = "5.7-jammy" - repo = "swift-system" - branch = "release/1.4.0" -} From 62e62b3a7c47c65e16816fbe6feabb56ae10ea3d Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 10 May 2024 17:05:37 -0700 Subject: [PATCH 04/38] Add CODEOWNERS --- .github/CODEOWNERS | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..70e4f6c --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,13 @@ +# Lines starting with '#' are comments. +# Each line is a case-sensitive file pattern followed by one or more owners. +# Order is important. The last matching pattern has the most precedence. +# More information: https://docs.github.com/en/articles/about-code-owners +# +# Please mirror the repository's file hierarchy in case-sensitive lexicographic +# order. + +# Default owners +* @glessard @lorentey @milseman + +# Swift CI configuration files +.swiftci/ @shahmishal From f9d6b48027fe819a72e50d972431259bd99431cd Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 3 Jun 2024 02:15:05 +0000 Subject: [PATCH 05/38] Suppress unhandled CMakeLists.txt warning by SwiftPM --- Package.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index e902aaa..7d02fca 100644 --- a/Package.swift +++ b/Package.swift @@ -27,11 +27,13 @@ let package = Package( targets: [ .target( name: "CSystem", - dependencies: []), + dependencies: [], + exclude: ["CMakeLists.txt"]), .target( name: "SystemPackage", dependencies: ["CSystem"], path: "Sources/System", + exclude: ["CMakeLists.txt"], cSettings: [ .define("_CRT_SECURE_NO_WARNINGS") ], From 1a8d472f35136451f61612f6e30b962cce2968d6 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 3 Jun 2024 02:15:05 +0000 Subject: [PATCH 06/38] Suppress unhandled CMakeLists.txt warning by SwiftPM --- Package.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 7511a8b..b135a35 100644 --- a/Package.swift +++ b/Package.swift @@ -21,11 +21,13 @@ let package = Package( targets: [ .target( name: "CSystem", - dependencies: []), + dependencies: [], + exclude: ["CMakeLists.txt"]), .target( name: "SystemPackage", dependencies: ["CSystem"], path: "Sources/System", + exclude: ["CMakeLists.txt"], cSettings: [ .define("_CRT_SECURE_NO_WARNINGS") ], From a1f77d0e1ab7c70871fb175091efc248b557a46b Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Wed, 17 Jul 2024 14:49:19 -0400 Subject: [PATCH 07/38] Cherry-pick: ensure invariants are enforced when decoding Ensure that FilePath and SystemString's invariants are enforced when decoding. Cherry-pick of merge-commit 9c42c54644f7bd78b8f2f3715060a01356e2a21d from PR #189 # Conflicts: # Sources/System/FilePath/FilePath.swift --- Sources/System/FilePath/FilePath.swift | 21 ++- Sources/System/FilePath/FilePathParsing.swift | 15 +- Sources/System/SystemString.swift | 34 ++++- .../FilePathTests/FilePathDecodable.swift | 129 ++++++++++++++++++ 4 files changed, 189 insertions(+), 10 deletions(-) create mode 100644 Tests/SystemTests/FilePathTests/FilePathDecodable.swift diff --git a/Sources/System/FilePath/FilePath.swift b/Sources/System/FilePath/FilePath.swift index 1aaa27e..54eed38 100644 --- a/Sources/System/FilePath/FilePath.swift +++ b/Sources/System/FilePath/FilePath.swift @@ -67,5 +67,22 @@ extension FilePath { } /*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/ -extension FilePath: Hashable, Codable {} - +extension FilePath: Hashable, Codable { + // Encoder is synthesized; it probably should have been explicit and used + // a single-value container, but making that change now is somewhat risky. + + // Decoder is written explicitly to ensure that we validate invariants on + // untrusted input. + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self._storage = try container.decode(SystemString.self, forKey: ._storage) + guard _invariantsSatisfied() else { + throw DecodingError.dataCorruptedError( + forKey: ._storage, + in: container, + debugDescription: + "Encoding does not satisfy the invariants of FilePath" + ) + } + } +} diff --git a/Sources/System/FilePath/FilePathParsing.swift b/Sources/System/FilePath/FilePathParsing.swift index 2675398..dff0409 100644 --- a/Sources/System/FilePath/FilePathParsing.swift +++ b/Sources/System/FilePath/FilePathParsing.swift @@ -359,13 +359,18 @@ extension FilePath { // MARK: - Invariants extension FilePath { - internal func _invariantCheck() { - #if DEBUG + internal func _invariantsSatisfied() -> Bool { var normal = self normal._normalizeSeparators() - precondition(self == normal) - precondition(!self._storage._hasTrailingSeparator()) - precondition(_hasRoot == (self.root != nil)) + guard self == normal else { return false } + guard !self._storage._hasTrailingSeparator() else { return false } + guard _hasRoot == (self.root != nil) else { return false } + return true + } + + internal func _invariantCheck() { + #if DEBUG + precondition(_invariantsSatisfied()) #endif // DEBUG } } diff --git a/Sources/System/SystemString.swift b/Sources/System/SystemString.swift index c26d904..54d1269 100644 --- a/Sources/System/SystemString.swift +++ b/Sources/System/SystemString.swift @@ -95,10 +95,18 @@ extension SystemString { } extension SystemString { + fileprivate func _invariantsSatisfied() -> Bool { + guard !nullTerminatedStorage.isEmpty else { return false } + guard nullTerminatedStorage.last! == .null else { return false } + guard nullTerminatedStorage.firstIndex(of: .null) == length else { + return false + } + return true + } + fileprivate func _invariantCheck() { #if DEBUG - precondition(nullTerminatedStorage.last! == .null) - precondition(nullTerminatedStorage.firstIndex(of: .null) == length) + precondition(_invariantsSatisfied()) #endif // DEBUG } } @@ -164,7 +172,27 @@ extension SystemString: RangeReplaceableCollection { } } -extension SystemString: Hashable, Codable {} +extension SystemString: Hashable, Codable { + // Encoder is synthesized; it probably should have been explicit and used + // a single-value container, but making that change now is somewhat risky. + + // Decoder is written explicitly to ensure that we validate invariants on + // untrusted input. + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.nullTerminatedStorage = try container.decode( + Storage.self, forKey: .nullTerminatedStorage + ) + guard _invariantsSatisfied() else { + throw DecodingError.dataCorruptedError( + forKey: .nullTerminatedStorage, + in: container, + debugDescription: + "Encoding does not satisfy the invariants of SystemString" + ) + } + } +} extension SystemString { diff --git a/Tests/SystemTests/FilePathTests/FilePathDecodable.swift b/Tests/SystemTests/FilePathTests/FilePathDecodable.swift new file mode 100644 index 0000000..290bef2 --- /dev/null +++ b/Tests/SystemTests/FilePathTests/FilePathDecodable.swift @@ -0,0 +1,129 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c)2024 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information + */ + +import XCTest + +#if SYSTEM_PACKAGE +@testable import SystemPackage +#else +@testable import System +#endif + +@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *) +final class FilePathDecodableTest: XCTestCase { + func testInvalidFilePath() { + // _storage is a valid SystemString, but the invariants of FilePath are + // violated (specifically, _storage is not normal). + let input: [UInt8] = [ + 123, 34, 95,115,116,111,114, 97,103,101, 34, 58,123, 34,110,117,108,108, + 84,101,114,109,105,110, 97,116,101,100, 83,116,111,114, 97,103,101, 34, + 58, 91, 49, 48, 57, 44, 45, 55, 54, 44, 53, 53, 44, 55, 49, 44, 49, 52, + 44, 53, 57, 44, 45, 49, 49, 50, 44, 45, 56, 52, 44, 52, 50, 44, 45, 55, + 48, 44, 45, 49, 48, 52, 44, 55, 51, 44, 45, 54, 44, 50, 44, 53, 55, 44, + 54, 50, 44, 45, 56, 55, 44, 45, 53, 44, 45, 54, 53, 44, 45, 51, 57, 44, + 45, 49, 48, 57, 44, 45, 55, 54, 44, 51, 48, 44, 53, 50, 44, 45, 56, 50, + 44, 45, 54, 48, 44, 45, 50, 44, 56, 53, 44, 49, 50, 51, 44, 45, 56, 52, + 44, 45, 53, 56, 44, 49, 49, 52, 44, 49, 44, 45, 49, 49, 54, 44, 56, 48, + 44, 49, 48, 52, 44, 45, 55, 56, 44, 45, 52, 53, 44, 49, 54, 44, 45, 52, + 54, 44, 55, 44, 49, 49, 56, 44, 45, 50, 52, 44, 54, 50, 44, 54, 52, 44, + 45, 52, 49, 44, 45, 49, 48, 51, 44, 53, 44, 45, 55, 53, 44, 50, 50, 44, + 45, 49, 48, 53, 44, 45, 49, 54, 44, 52, 55, 44, 52, 55, 44, 49, 50, 52, + 44, 45, 53, 55, 44, 53, 51, 44, 49, 49, 49, 44, 49, 53, 44, 45, 50, 55, + 44, 54, 54, 44, 45, 49, 54, 44, 49, 48, 50, 44, 49, 48, 54, 44, 49, 51, + 44, 49, 48, 53, 44, 45, 49, 49, 50, 44, 55, 56, 44, 45, 53, 48, 44, 50, + 48, 44, 56, 44, 45, 50, 55, 44, 52, 52, 44, 52, 44, 56, 44, 54, 53, 44, + 50, 51, 44, 57, 55, 44, 45, 50, 56, 44, 56, 56, 44, 52, 50, 44, 45, 51, + 54, 44, 45, 50, 51, 44, 49, 48, 51, 44, 57, 57, 44, 45, 53, 56, 44, 45, + 49, 49, 48, 44, 45, 53, 52, 44, 45, 49, 49, 55, 44, 45, 57, 52, 44, 45, + 55, 50, 44, 50, 57, 44, 45, 50, 52, 44, 45, 56, 52, 44, 53, 55, 44, 45, + 49, 50, 54, 44, 52, 52, 44, 55, 53, 44, 55, 54, 44, 52, 57, 44, 45, 52, + 49, 44, 45, 50, 53, 44, 50, 52, 44, 45, 49, 50, 54, 44, 55, 44, 50, 56, + 44, 45, 52, 56, 44, 56, 55, 44, 51, 49, 44, 45, 49, 49, 53, 44, 55, 44, + 45, 54, 48, 44, 53, 57, 44, 49, 51, 44, 55, 57, 44, 53, 48, 44, 45, 57, + 54, 44, 45, 50, 44, 45, 50, 52, 44, 45, 57, 49, 44, 55, 49, 44, 45, 49, + 50, 53, 44, 52, 50, 44, 45, 56, 52, 44, 52, 44, 53, 57, 44, 49, 50, 53, + 44, 49, 50, 49, 44, 45, 50, 54, 44, 45, 49, 50, 44, 45, 49, 48, 53, 44, + 53, 54, 44, 49, 49, 48, 44, 49, 52, 44, 45, 49, 48, 52, 44, 45, 53, 50, + 44, 45, 53, 56, 44, 45, 54, 44, 45, 50, 54, 44, 45, 52, 55, 44, 53, 57, + 44, 52, 50, 44, 49, 50, 51, 44, 52, 52, 44, 45, 57, 50, 44, 45, 50, 57, + 44, 45, 51, 54, 44, 45, 54, 50, 44, 50, 54, 44, 45, 49, 55, 44, 45, 49, + 48, 44, 45, 56, 49, 44, 54, 49, 44, 52, 55, 44, 45, 57, 52, 44, 45, 49, + 48, 54, 44, 49, 53, 44, 49, 48, 48, 44, 45, 49, 50, 49, 44, 45, 49, 49, + 49, 44, 51, 44, 45, 57, 44, 52, 54, 44, 45, 55, 48, 44, 45, 49, 57, 44, + 52, 56, 44, 45, 49, 50, 44, 45, 57, 49, 44, 45, 50, 48, 44, 49, 51, 44, + 54, 53, 44, 45, 55, 48, 44, 52, 49, 44, 45, 57, 53, 44, 49, 48, 52, 44, + 45, 55, 53, 44, 45, 49, 49, 53, 44, 49, 48, 49, 44, 45, 57, 52, 44, 45, + 49, 50, 51, 44, 45, 51, 53, 44, 45, 50, 49, 44, 45, 52, 50, 44, 45, 51, + 48, 44, 45, 55, 49, 44, 45, 49, 49, 57, 44, 52, 52, 44, 49, 49, 49, 44, + 49, 48, 53, 44, 54, 54, 44, 45, 49, 50, 54, 44, 55, 50, 44, 45, 52, 48, + 44, 49, 50, 49, 44, 45, 50, 49, 44, 52, 50, 44, 45, 55, 56, 44, 49, 50, + 54, 44, 56, 49, 44, 45, 57, 52, 44, 55, 52, 44, 49, 49, 50, 44, 45, 56, + 54, 44, 51, 50, 44, 55, 54, 44, 49, 49, 55, 44, 45, 56, 44, 56, 54, 44, + 49, 48, 51, 44, 54, 50, 44, 49, 49, 55, 44, 54, 55, 44, 45, 56, 54, 44, + 45, 49, 48, 48, 44, 45, 49, 48, 57, 44, 45, 53, 52, 44, 45, 51, 49, 44, + 45, 56, 57, 44, 48, 93,125,125, + ] + + XCTAssertThrowsError(try JSONDecoder().decode( + FilePath.self, + from: Data(input) + )) + } + + func testInvalidSystemString() { + // _storage is a SystemString whose invariants are violated; it contains + // a non-terminating null byte. + let input: [UInt8] = [ + 123, 34, 95,115,116,111,114, 97,103,101, 34, 58,123, 34,110,117,108,108, + 84,101,114,109,105,110, 97,116,101,100, 83,116,111,114, 97,103,101, 34, + 58, 91, 49, 49, 49, 44, 48, 44, 45, 49, 54, 44, 57, 49, 44, 52, 54, 44, + 45, 49, 48, 50, 44, 49, 49, 53, 44, 45, 50, 49, 44, 45, 49, 49, 56, 44, + 52, 57, 44, 57, 50, 44, 45, 49, 48, 44, 53, 56, 44, 45, 55, 48, 44, 57, + 55, 44, 56, 44, 57, 57, 44, 48, 93,125, 125 + ] + + XCTAssertThrowsError(try JSONDecoder().decode( + FilePath.self, + from: Data(input) + )) + } + + func testInvalidExample() { + // Another misformed example from Johannes that violates FilePath's + // invariants by virtue of not being normalized. + let input: [UInt8] = [ + 123, 34, 95,115,116,111,114, 97,103,101, 34, 58,123, 34,110,117,108,108, + 84,101,114,109,105,110, 97,116,101,100, 83,116,111,114, 97,103,101, 34, + 58, 91, 56, 55, 44, 50, 52, 44, 45, 49, 49, 53, 44, 45, 49, 57, 44, 49, + 50, 50, 44, 45, 54, 56, 44, 57, 49, 44, 45, 49, 48, 54, 44, 45, 49, 48, + 48, 44, 45, 49, 49, 52, 44, 53, 54, 44, 45, 54, 53, 44, 49, 49, 56, 44, + 45, 54, 48, 44, 54, 54, 44, 45, 52, 50, 44, 55, 55, 44, 45, 54, 44, 45, + 52, 50, 44, 45, 56, 56, 44, 52, 55, 44, 48, 93,125, 125 + ] + + XCTAssertThrowsError(try JSONDecoder().decode( + FilePath.self, + from: Data(input) + )) + } + + func testEmptyString() { + // FilePath with an empty (and hence not null-terminated) SystemString. + let input: [UInt8] = [ + 123, 34, 95,115,116,111,114, 97,103,101, 34, 58,123, 34,110,117,108,108, + 84,101,114,109,105,110, 97,116,101,100, 83,116,111,114, 97,103,101, 34, + 58, 91, 93,125,125 + ] + + XCTAssertThrowsError(try JSONDecoder().decode( + FilePath.self, + from: Data(input) + )) + } +} From 603c45181816ca05e702fb130ce239a580405e97 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 19 Sep 2024 12:49:08 -0700 Subject: [PATCH 08/38] Add code of conduct --- CODE_OF_CONDUCT.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 CODE_OF_CONDUCT.md diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..95c80e7 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,38 @@ +# Code of Conduct + +To be a truly great community, Swift.org needs to welcome developers from all walks of life, with different backgrounds, and with a wide range of experience. A diverse and friendly community will have more great ideas, more unique perspectives, and produce more great code. We will work diligently to make the Swift community welcoming to everyone. + +To give clarity of what is expected of our members, this code of conduct is based on [contributor-covenant.org](http://contributor-covenant.org). This document is used across many open source communities, and we think it articulates our values well. + +### Contributor Code of Conduct v1.4 + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language (e.g., prefer non-gendered words like “folks” to “guys”, non-ableist words like “soundness check” to “sanity check”, etc.) +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others’ private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +This Code of Conduct applies within all project spaces managed by Swift.org, including (but not limited to) source code repositories, bug trackers, web sites, documentation, and online forums. It also applies when an individual is representing the project or its community in public spaces. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting a member of the [Swift Core Team](https://swift.org/community/#community-structure) or by flagging the behavior for moderation (e.g., in the Forums), whether you are the target of that behavior or not. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. Maintainers are obligated to maintain confidentiality with regard to the reporter of an incident. The site of the disputed behavior is usually not an acceptable place to discuss moderation decisions, and moderators may move or remove any such discussion. + +Project maintainers are held to a higher standard, and project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project’s leadership. +If you disagree with a moderation action, you can appeal to the Core Team (or individual Core Team members) privately. + +This policy is adapted from the Contributor Code of Conduct [version 1.4](https://www.contributor-covenant.org/version/1/4/code-of-conduct/). From 4c612a1e29a0eb3d8aa1def29db51d7500c283fb Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 19 Sep 2024 12:57:57 -0700 Subject: [PATCH 09/38] Bump required toolchain version to 5.9. Sanitize Package manifest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Don’t define _CRT_SECURE_NO_WARNINGS outside Windows - Let all targets share the same settings --- Package.swift | 90 +++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/Package.swift b/Package.swift index b135a35..411a295 100644 --- a/Package.swift +++ b/Package.swift @@ -1,47 +1,51 @@ -// swift-tools-version:5.6 -// The swift-tools-version declares the minimum version of Swift required to build this package. - -/* - This source file is part of the Swift System open source project - - Copyright (c) 2020 Apple Inc. and the Swift System project authors - Licensed under Apache License v2.0 with Runtime Library Exception - - See https://swift.org/LICENSE.txt for license information -*/ +// swift-tools-version:5.9 +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift System open source project +// +// Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// import PackageDescription +let cSettings: [CSetting] = [ + .define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])), +] + +let swiftSettings: [SwiftSetting] = [ + .define( + "SYSTEM_PACKAGE_DARWIN", + .when(platforms: [.macOS, .iOS, .watchOS, .tvOS, .visionOS])), + .define("SYSTEM_PACKAGE"), + .define("ENABLE_MOCKING", .when(configuration: .debug)), +] + let package = Package( - name: "swift-system", - products: [ - .library(name: "SystemPackage", targets: ["SystemPackage"]), - ], - dependencies: [], - targets: [ - .target( - name: "CSystem", - dependencies: [], - exclude: ["CMakeLists.txt"]), - .target( - name: "SystemPackage", - dependencies: ["CSystem"], - path: "Sources/System", - exclude: ["CMakeLists.txt"], - cSettings: [ - .define("_CRT_SECURE_NO_WARNINGS") - ], - swiftSettings: [ - .define("SYSTEM_PACKAGE_DARWIN", .when(platforms: [.macOS, .iOS, .watchOS, .tvOS, .visionOS])), - .define("SYSTEM_PACKAGE"), - .define("ENABLE_MOCKING", .when(configuration: .debug)) - ]), - .testTarget( - name: "SystemTests", - dependencies: ["SystemPackage"], - swiftSettings: [ - .define("SYSTEM_PACKAGE_DARWIN", .when(platforms: [.macOS, .iOS, .watchOS, .tvOS, .visionOS])), - .define("SYSTEM_PACKAGE") - ]), - ] -) + name: "swift-system", + products: [ + .library(name: "SystemPackage", targets: ["SystemPackage"]), + ], + dependencies: [], + targets: [ + .target( + name: "CSystem", + dependencies: [], + exclude: ["CMakeLists.txt"], + cSettings: cSettings), + .target( + name: "SystemPackage", + dependencies: ["CSystem"], + path: "Sources/System", + exclude: ["CMakeLists.txt"], + cSettings: cSettings, + swiftSettings: swiftSettings), + .testTarget( + name: "SystemTests", + dependencies: ["SystemPackage"], + cSettings: cSettings, + swiftSettings: swiftSettings), + ]) From 88a60e08b3692d49a992429897c78f895b104804 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 19 Sep 2024 13:01:48 -0700 Subject: [PATCH 10/38] Update README MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Clarify swift-system’s stance on cross-platform abstractions. Stop using the term “multi-platform”. - Expand source stability section. - Add a section on our branching strategy. - Link to the Code of Conduct document. --- README.md | 66 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 14edea0..452ad86 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,13 @@ Swift System provides idiomatic interfaces to system calls and low-level currency types. Our vision is for System to act as the single home for low-level system interfaces for all supported Swift platforms. -## Multi-platform not Cross-platform +## No Cross-platform Abstractions -System is a multi-platform library, not a cross-platform one. It provides a separate set of APIs and behaviors on every supported platform, closely reflecting the underlying OS interfaces. A single import will pull in the native platform interfaces specific for the targeted OS. +Swift System is not a cross-platform library. It provides a separate set of APIs and behaviors on every supported platform, closely reflecting the underlying OS interfaces. A single import will pull in the native platform interfaces specific for the targeted OS. -Our immediate goal is to simplify building cross-platform libraries and applications such as SwiftNIO and SwiftPM. System does not eliminate the need for `#if os()` conditionals to implement cross-platform abstractions, but it does make it safer and more expressive to fill out the platform-specific parts. +Our immediate goal is to simplify building cross-platform libraries and applications such as SwiftNIO and SwiftPM. It is not a design goal for System to eliminate the need for `#if os()` conditionals to implement cross-platform abstractions; rather, our goal is to make it safer and more expressive to fill out the platform-specific parts. + +(That said, it is desirable to avoid unnecessary differences -- for example, when two operating systems share the same C name for a system call, ideally Swift System would expose them using the same Swift name. This is a particularly obvious expectation for system interfaces that implement an industry standard, such as POSIX.) ## Usage @@ -30,7 +32,7 @@ To use the `SystemPackage` library in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file: ```swift -.package(url: "https://github.com/apple/swift-system", from: "1.0.0"), +.package(url: "https://github.com/apple/swift-system", from: "1.3.0"), ``` Finally, include `"SystemPackage"` as a dependency for your executable target: @@ -39,7 +41,7 @@ Finally, include `"SystemPackage"` as a dependency for your executable target: let package = Package( // name, platforms, products, etc. dependencies: [ - .package(url: "https://github.com/apple/swift-system", from: "1.0.0"), + .package(url: "https://github.com/apple/swift-system", from: "1.3.0"), // other dependencies ], targets: [ @@ -57,10 +59,58 @@ The Swift System package is source stable. The version numbers follow [Semantic [semver]: https://semver.org -## Contributing +The public API of the swift-system package consists of non-underscored declarations that are marked `public` in the `SystemPackage` module. -Before contributing, please read [CONTRIBUTING.md](CONTRIBUTING.md). +By "underscored declarations" we mean declarations that have a leading underscore anywhere in their fully qualified name. For instance, here are some names that wouldn't be considered part of the public API, even if they were technically marked public: -## LICENSE +- `FooModule.Bar._someMember(value:)` (underscored member) +- `FooModule._Bar.someMember` (underscored type) +- `_FooModule.Bar` (underscored module) +- `FooModule.Bar.init(_value:)` (underscored initializer) + +Interfaces that aren't part of the public API may continue to change in any release, including patch releases. If you have a use case that requires using non-public APIs, please submit a Feature Request describing it! We'd like the public interface to be as useful as possible -- although preferably without compromising safety or limiting future evolution. + +Future minor versions of the package may update these rules as needed. + +## Toolchain Requirements + +The following table maps existing package releases to their minimum required Swift toolchain release: + +| Package version | Swift version | Xcode release | +| ----------------------- | --------------- | ------------- | +| swift-system 1.3.x | >= Swift 5.8 | >= Xcode 14.3 | +| swift-system 1.4.x (unreleased) | >= Swift 5.9 | >= Xcode 15.0 | + +We'd like this package to quickly embrace Swift language and toolchain improvements that are relevant to its mandate. Accordingly, from time to time, new versions of this package require clients to upgrade to a more recent Swift toolchain release. (This allows the package to make use of new language/stdlib features, build on compiler bug fixes, and adopt new package manager functionality as soon as they are available.) Patch (i.e., bugfix) releases will not increase the required toolchain version, but any minor (i.e., new feature) release may do so. + +(Note: the package has no minimum deployment target, so while it does require clients to use a recent Swift toolchain to build it, the code itself is able to run on any OS release that supports running Swift code.) + +## Licensing See [LICENSE](LICENSE.txt) for license information. + +## Contributing + +Before contributing, please read [CONTRIBUTING.md](CONTRIBUTING.md). + +### Branching Strategy + +We maintain separate branches for each active minor version of the package: + +| Package version | Branch | +| ----------------------- | ----------- | +| swift-system 1.3.x | release/1.3 | +| swift-system 1.4.x (unreleased) | release/1.4 | +| swift-system 1.5.x (unreleased) | main | + +Changes must land on the branch corresponding to the earliest release that they will need to ship on. They are periodically propagated to subsequent branches, in the following direction: + +`release/1.3` → `release/1.4` → `main` + +For example, anything landing on `release/1.3` will eventually appear on `release/1.4` and then `main` too; there is no need to file standalone PRs for each release line. (Change propagation currently requires manual work -- it is performed by project maintainers.) + +### Code of Conduct + +Like all Swift.org projects, we would like the Swift System project to foster a diverse and friendly community. We expect contributors to adhere to the [Swift.org Code of Conduct](https://swift.org/code-of-conduct/). A copy of this document is [available in this repository][coc]. + +[coc]: CODE_OF_CONDUCT.md From d4e2b09c37184249630bad72bf7f44dd5bde689a Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Wed, 20 Mar 2024 14:46:10 +0000 Subject: [PATCH 11/38] Fix swift-system to work on Windows. The swift-system tests were not passing, for various reasons, mostly to do with path related confusion. As part of this, add a function to create a temporary directory, as that was one of the problems. Also add Windows support for pipe() and ftruncate(), which was missing. rdar://125087707 --- Sources/System/ErrnoWindows.swift | 20 ++ Sources/System/FileOperations.swift | 8 +- Sources/System/FilePath/FilePathParsing.swift | 10 +- Sources/System/FilePath/FilePathTemp.swift | 256 ++++++++++++++++++ Sources/System/Internals/Mocking.swift | 16 +- Sources/System/Internals/Syscalls.swift | 82 +++++- .../Internals/WindowsSyscallAdapters.swift | 133 ++++++++- Tests/SystemTests/FileOperationsTest.swift | 120 ++++---- .../FilePathComponentsTest.swift | 20 +- .../FilePathTests/FilePathSyntaxTest.swift | 3 + .../FilePathTests/FilePathTempTest.swift | 49 ++++ .../FilePathTests/FilePathTest.swift | 10 + 12 files changed, 639 insertions(+), 88 deletions(-) create mode 100644 Sources/System/ErrnoWindows.swift create mode 100644 Sources/System/FilePath/FilePathTemp.swift create mode 100644 Tests/SystemTests/FilePathTests/FilePathTempTest.swift diff --git a/Sources/System/ErrnoWindows.swift b/Sources/System/ErrnoWindows.swift new file mode 100644 index 0000000..98fda43 --- /dev/null +++ b/Sources/System/ErrnoWindows.swift @@ -0,0 +1,20 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2024 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information +*/ + +#if os(Windows) + +import WinSDK + +extension Errno { + public init(windowsError: DWORD) { + self.init(rawValue: mapWindowsErrorToErrno(windowsError)) + } +} + +#endif diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index ada80cf..d2e8d65 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -369,6 +369,7 @@ extension FileDescriptor { } } +#if !os(WASI) @available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *) extension FileDescriptor { /// Duplicates this file descriptor and return the newly created copy. @@ -433,8 +434,9 @@ extension FileDescriptor { fatalError("Not implemented") } } +#endif -#if !os(Windows) +#if !os(WASI) @available(/*System 1.1.0: macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4*/iOS 8, *) extension FileDescriptor { /// Creates a unidirectional data channel, which can be used for interprocess communication. @@ -463,7 +465,6 @@ extension FileDescriptor { } #endif -#if !os(Windows) @available(/*System 1.2.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *) extension FileDescriptor { /// Truncates or extends the file referenced by this file descriptor. @@ -509,4 +510,3 @@ extension FileDescriptor { } } } -#endif diff --git a/Sources/System/FilePath/FilePathParsing.swift b/Sources/System/FilePath/FilePathParsing.swift index 79482bd..c31e6a5 100644 --- a/Sources/System/FilePath/FilePathParsing.swift +++ b/Sources/System/FilePath/FilePathParsing.swift @@ -89,6 +89,11 @@ extension SystemString { // `_prenormalizeWindowsRoots` and resume. readIdx = _prenormalizeWindowsRoots() writeIdx = readIdx + + // Skip redundant separators + while readIdx < endIndex && isSeparator(self[readIdx]) { + self.formIndex(after: &readIdx) + } } else { assert(genericSeparator == platformSeparator) } @@ -330,10 +335,13 @@ extension FilePath { // Whether we are providing Windows paths @inline(__always) internal var _windowsPaths: Bool { + if let forceWindowsPaths = forceWindowsPaths { + return forceWindowsPaths + } #if os(Windows) return true #else - return forceWindowsPaths + return false #endif } diff --git a/Sources/System/FilePath/FilePathTemp.swift b/Sources/System/FilePath/FilePathTemp.swift new file mode 100644 index 0000000..9aa65ff --- /dev/null +++ b/Sources/System/FilePath/FilePathTemp.swift @@ -0,0 +1,256 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2024 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information +*/ + +// MARK: - API + +public func withTemporaryPath(basename: FilePath.Component, + _ body: (FilePath) throws -> R) throws -> R { + let temporaryDir = try createUniqueTemporaryDirectory(basename: basename) + defer { + try? recursiveRemove(at: temporaryDir) + } + + return try body(temporaryDir) +} + +// MARK: - Internals + +#if os(Windows) +import WinSDK + +internal func getTemporaryDirectory() throws -> FilePath { + return try withUnsafeTemporaryAllocation(of: CInterop.PlatformChar.self, + capacity: Int(MAX_PATH) + 1) { + buffer in + + let count = GetTempPath2W(DWORD(buffer.count), buffer.baseAddress) + if count == 0 { + throw Errno(windowsError: GetLastError()) + } + + return FilePath(SystemString(platformString: buffer.baseAddress!)) + } +} + +internal func forEachFile( + at path: FilePath, + _ body: (WIN32_FIND_DATAW) throws -> () +) rethrows { + let searchPath = path.appending("\\*") + + try searchPath.withPlatformString { szPath in + var findData = WIN32_FIND_DATAW() + let hFind = FindFirstFileW(szPath, &findData) + if hFind == INVALID_HANDLE_VALUE { + throw Errno(windowsError: GetLastError()) + } + defer { + FindClose(hFind) + } + + repeat { + // Skip . and .. + if findData.cFileName.0 == 46 + && (findData.cFileName.1 == 0 + || (findData.cFileName.1 == 46 + && findData.cFileName.2 == 0)) { + continue + } + + try body(findData) + } while FindNextFileW(hFind, &findData) + } +} + +internal func recursiveRemove(at path: FilePath) throws { + // First, deal with subdirectories + try forEachFile(at: path) { findData in + if (findData.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY)) != 0 { + let name = withUnsafeBytes(of: findData.cFileName) { + return SystemString(platformString: $0.assumingMemoryBound( + to: CInterop.PlatformChar.self).baseAddress!) + } + let component = FilePath.Component(name)! + let subpath = path.appending(component) + + try recursiveRemove(at: subpath) + } + } + + // Now delete everything else + try forEachFile(at: path) { findData in + let name = withUnsafeBytes(of: findData.cFileName) { + return SystemString(platformString: $0.assumingMemoryBound( + to: CInterop.PlatformChar.self).baseAddress!) + } + let component = FilePath.Component(name)! + let subpath = path.appending(component) + + if (findData.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY)) == 0 { + try subpath.withPlatformString { + if !DeleteFileW($0) { + throw Errno(windowsError: GetLastError()) + } + } + } + } + + // Finally, delete the parent + try path.withPlatformString { + if !RemoveDirectoryW($0) { + throw Errno(windowsError: GetLastError()) + } + } +} + +#else +internal func getTemporaryDirectory() throws -> FilePath { + return "/tmp" +} + +internal func recursiveRemove(at path: FilePath) throws { + let dirfd = try FileDescriptor.open(path, .readOnly, options: .directory) + defer { + try? dirfd.close() + } + + let dot: (CInterop.PlatformChar, CInterop.PlatformChar) = (46, 0) + try withUnsafeBytes(of: dot) { + try recursiveRemove( + in: dirfd.rawValue, + path: $0.assumingMemoryBound(to: CInterop.PlatformChar.self).baseAddress! + ) + } + + try path.withPlatformString { + if system_rmdir($0) != 0 { + throw Errno.current + } + } +} + +fileprivate func impl_opendirat( + _ dirfd: CInt, + _ path: UnsafePointer +) -> UnsafeMutablePointer? { + let fd = system_openat(dirfd, path, + FileDescriptor.AccessMode.readOnly.rawValue + | FileDescriptor.OpenOptions.directory.rawValue) + if fd < 0 { + return nil + } + return system_fdopendir(fd) +} + +internal func forEachFile( + in dirfd: CInt, path: UnsafePointer, + _ body: (system_dirent) throws -> () +) throws { + guard let dir = impl_opendirat(dirfd, path) else { + throw Errno.current + } + defer { + _ = system_closedir(dir) + } + + while let dirent = system_readdir(dir) { + // Skip . and .. + if dirent.pointee.d_name.0 == 46 + && (dirent.pointee.d_name.1 == 0 + || (dirent.pointee.d_name.1 == 46 + && dirent.pointee.d_name.2 == 0)) { + continue + } + + try body(dirent.pointee) + } +} + +internal func recursiveRemove( + in dirfd: CInt, + path: UnsafePointer +) throws { + // First, deal with subdirectories + try forEachFile(in: dirfd, path: path) { dirent in + if dirent.d_type == SYSTEM_DT_DIR { + try withUnsafeBytes(of: dirent.d_name) { + try recursiveRemove( + in: dirfd, + path: $0.assumingMemoryBound(to: CInterop.PlatformChar.self) + .baseAddress! + ) + } + } + } + + // Now delete the contents of this directory + try forEachFile(in: dirfd, path: path) { dirent in + let flag: CInt + + if dirent.d_type == SYSTEM_DT_DIR { + flag = SYSTEM_AT_REMOVE_DIR + } else { + flag = 0 + } + + let result = withUnsafeBytes(of: dirent.d_name) { + system_unlinkat(dirfd, + $0.assumingMemoryBound(to: CInterop.PlatformChar.self) + .baseAddress!, + flag) + } + + if result != 0 { + throw Errno.current + } + } +} +#endif + +internal let base64 = Array( + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".utf8 +) + +internal func makeDirectory(at: FilePath) throws -> Bool { + return try at.withPlatformString { + if system_mkdir($0, 0o700) == 0 { + return true + } + let err = system_errno + if err == Errno.fileExists.rawValue { + return false + } else { + throw Errno(rawValue: err) + } + } +} + +internal func createRandomString(length: Int) -> String { + return String( + decoding: (0.. FilePath { + var tempDir = try getTemporaryDirectory() + tempDir.append(basename) + + while true { + tempDir.extension = createRandomString(length: 16) + + if try makeDirectory(at: tempDir) { + return tempDir + } + } +} diff --git a/Sources/System/Internals/Mocking.swift b/Sources/System/Internals/Mocking.swift index 405dc34..08c0f71 100644 --- a/Sources/System/Internals/Mocking.swift +++ b/Sources/System/Internals/Mocking.swift @@ -63,7 +63,7 @@ internal class MockingDriver { // Whether we should pretend to be Windows for syntactic operations // inside FilePath - fileprivate var forceWindowsSyntaxForPaths = false + fileprivate var forceWindowsSyntaxForPaths: Bool? = nil } private let driverKey: _PlatformTLSKey = { makeTLSKey() }() @@ -110,8 +110,8 @@ private var contextualMockingEnabled: Bool { extension MockingDriver { internal static var enabled: Bool { mockingEnabled } - internal static var forceWindowsPaths: Bool { - currentMockingDriver?.forceWindowsSyntaxForPaths ?? false + internal static var forceWindowsPaths: Optional { + currentMockingDriver?.forceWindowsSyntaxForPaths } } @@ -128,7 +128,7 @@ internal var mockingEnabled: Bool { } @inline(__always) -internal var forceWindowsPaths: Bool { +internal var forceWindowsPaths: Optional { #if !ENABLE_MOCKING return false #else @@ -196,15 +196,11 @@ internal func _mockOffT( #endif // ENABLE_MOCKING // Force paths to be treated as Windows syntactically if `enabled` is -// true. +// true, and as POSIX syntactically if not. internal func _withWindowsPaths(enabled: Bool, _ body: () -> ()) { #if ENABLE_MOCKING - guard enabled else { - body() - return - } MockingDriver.withMockingEnabled { driver in - driver.forceWindowsSyntaxForPaths = true + driver.forceWindowsSyntaxForPaths = enabled body() } #else diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index 793fdfd..4669141 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -13,6 +13,8 @@ import Darwin import Glibc #elseif canImport(Musl) import Musl +#elseif canImport(WASILibc) +import WASILibc #elseif os(Windows) import ucrt #else @@ -120,6 +122,7 @@ internal func system_pwrite( #endif } +#if !os(WASI) internal func system_dup(_ fd: Int32) -> Int32 { #if ENABLE_MOCKING if mockingEnabled { return _mock(fd) } @@ -133,8 +136,9 @@ internal func system_dup2(_ fd: Int32, _ fd2: Int32) -> Int32 { #endif return dup2(fd, fd2) } +#endif -#if !os(Windows) +#if !os(WASI) internal func system_pipe(_ fds: UnsafeMutablePointer) -> CInt { #if ENABLE_MOCKING if mockingEnabled { return _mock(fds) } @@ -143,11 +147,83 @@ internal func system_pipe(_ fds: UnsafeMutablePointer) -> CInt { } #endif -#if !os(Windows) internal func system_ftruncate(_ fd: Int32, _ length: off_t) -> Int32 { #if ENABLE_MOCKING if mockingEnabled { return _mock(fd, length) } #endif return ftruncate(fd, length) } + +internal func system_mkdir( + _ path: UnsafePointer, + _ mode: CInterop.Mode +) -> CInt { +#if ENABLE_MOCKING + if mockingEnabled { return _mock(path: path, mode) } +#endif + return mkdir(path, mode) +} + +internal func system_rmdir( + _ path: UnsafePointer +) -> CInt { +#if ENABLE_MOCKING + if mockingEnabled { return _mock(path: path) } +#endif + return rmdir(path) +} + +#if !os(Windows) +internal let SYSTEM_AT_REMOVE_DIR = AT_REMOVEDIR +internal let SYSTEM_DT_DIR = DT_DIR +internal typealias system_dirent = dirent +internal typealias system_DIR = DIR + +internal func system_unlinkat( + _ fd: CInt, + _ path: UnsafePointer, + _ flag: CInt +) -> CInt { +#if ENABLE_MOCKING + if mockingEnabled { return _mock(fd, path, flag) } +#endif +return unlinkat(fd, path, flag) +} + +internal func system_fdopendir( + _ fd: CInt +) -> UnsafeMutablePointer? { + return fdopendir(fd) +} + +internal func system_readdir( + _ dir: UnsafeMutablePointer +) -> UnsafeMutablePointer? { + return readdir(dir) +} + +internal func system_rewinddir( + _ dir: UnsafeMutablePointer +) { + return rewinddir(dir) +} + +internal func system_closedir( + _ dir: UnsafeMutablePointer +) -> CInt { + return closedir(dir) +} + +internal func system_openat( + _ fd: CInt, + _ path: UnsafePointer, + _ oflag: Int32 +) -> CInt { +#if ENABLE_MOCKING + if mockingEnabled { + return _mock(fd, path, oflag) + } +#endif + return openat(fd, path, oflag) +} #endif diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index 2f3851b..bc83782 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -80,7 +80,7 @@ internal func pread( _ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t ) -> Int { let handle: intptr_t = _get_osfhandle(fd) - if handle == /* INVALID_HANDLE_VALUE */ -1 { return Int(EBADF) } + if handle == /* INVALID_HANDLE_VALUE */ -1 { ucrt._set_errno(EBADF); return -1 } // NOTE: this is a non-owning handle, do *not* call CloseHandle on it let hFile: HANDLE = HANDLE(bitPattern: handle)! @@ -91,8 +91,7 @@ internal func pread( var nNumberOfBytesRead: DWORD = 0 if !ReadFile(hFile, buf, DWORD(nbyte), &nNumberOfBytesRead, &ovlOverlapped) { - let _ = GetLastError() - // TODO(compnerd) map windows error to errno + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) return Int(-1) } return Int(nNumberOfBytesRead) @@ -103,7 +102,7 @@ internal func pwrite( _ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t ) -> Int { let handle: intptr_t = _get_osfhandle(fd) - if handle == /* INVALID_HANDLE_VALUE */ -1 { return Int(EBADF) } + if handle == /* INVALID_HANDLE_VALUE */ -1 { ucrt._set_errno(EBADF); return -1 } // NOTE: this is a non-owning handle, do *not* call CloseHandle on it let hFile: HANDLE = HANDLE(bitPattern: handle)! @@ -115,11 +114,133 @@ internal func pwrite( var nNumberOfBytesWritten: DWORD = 0 if !WriteFile(hFile, buf, DWORD(nbyte), &nNumberOfBytesWritten, &ovlOverlapped) { - let _ = GetLastError() - // TODO(compnerd) map windows error to errno + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) return Int(-1) } return Int(nNumberOfBytesWritten) } +@inline(__always) +internal func pipe(_ fds: UnsafeMutablePointer) -> CInt { + return _pipe(fds, 4096, _O_BINARY | _O_NOINHERIT); +} + +@inline(__always) +internal func ftruncate(_ fd: Int32, _ length: off_t) -> Int32 { + let handle: intptr_t = _get_osfhandle(fd) + if handle == /* INVALID_HANDLE_VALUE */ -1 { ucrt._set_errno(EBADF); return -1 } + + // NOTE: this is a non-owning handle, do *not* call CloseHandle on it + let hFile: HANDLE = HANDLE(bitPattern: handle)! + let liDesiredLength = LARGE_INTEGER(QuadPart: LONGLONG(length)) + var liCurrentOffset = LARGE_INTEGER(QuadPart: 0) + + // Save the current position and restore it when we're done + if !SetFilePointerEx(hFile, liCurrentOffset, &liCurrentOffset, + DWORD(FILE_CURRENT)) { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + defer { + _ = SetFilePointerEx(hFile, liCurrentOffset, nil, DWORD(FILE_BEGIN)); + } + + // Truncate (or extend) the file + if !SetFilePointerEx(hFile, liDesiredLength, nil, DWORD(FILE_BEGIN)) + || !SetEndOfFile(hFile) { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + + return 0; +} + +@inline(__always) +internal func mkdir( + _ path: UnsafePointer, + _ mode: CInterop.Mode +) -> CInt { + return _wmkdir(path) +} + +@inline(__always) +internal func rmdir( + _ path: UnsafePointer +) -> CInt { + return _wrmdir(path) +} + +@usableFromInline +internal func mapWindowsErrorToErrno(_ errorCode: DWORD) -> CInt { + switch Int32(errorCode) { + case ERROR_SUCCESS: + return 0 + case ERROR_INVALID_FUNCTION, + ERROR_INVALID_ACCESS, + ERROR_INVALID_DATA, + ERROR_INVALID_PARAMETER, + ERROR_NEGATIVE_SEEK: + return EINVAL + case ERROR_FILE_NOT_FOUND, + ERROR_PATH_NOT_FOUND, + ERROR_INVALID_DRIVE, + ERROR_NO_MORE_FILES, + ERROR_BAD_NETPATH, + ERROR_BAD_NET_NAME, + ERROR_BAD_PATHNAME, + ERROR_FILENAME_EXCED_RANGE: + return ENOENT + case ERROR_TOO_MANY_OPEN_FILES: + return EMFILE + case ERROR_ACCESS_DENIED, + ERROR_CURRENT_DIRECTORY, + ERROR_LOCK_VIOLATION, + ERROR_NETWORK_ACCESS_DENIED, + ERROR_CANNOT_MAKE, + ERROR_FAIL_I24, + ERROR_DRIVE_LOCKED, + ERROR_SEEK_ON_DEVICE, + ERROR_NOT_LOCKED, + ERROR_LOCK_FAILED, + ERROR_WRITE_PROTECT...ERROR_SHARING_BUFFER_EXCEEDED: + return EACCES + case ERROR_INVALID_HANDLE, + ERROR_INVALID_TARGET_HANDLE, + ERROR_DIRECT_ACCESS_HANDLE: + return EBADF + case ERROR_ARENA_TRASHED, + ERROR_NOT_ENOUGH_MEMORY, + ERROR_INVALID_BLOCK, + ERROR_NOT_ENOUGH_QUOTA: + return ENOMEM + case ERROR_BAD_ENVIRONMENT: + return E2BIG + case ERROR_BAD_FORMAT, + ERROR_INVALID_STARTING_CODESEG...ERROR_INFLOOP_IN_RELOC_CHAIN: + return ENOEXEC + case ERROR_NOT_SAME_DEVICE: + return EXDEV + case ERROR_FILE_EXISTS, + ERROR_ALREADY_EXISTS: + return EEXIST + case ERROR_NO_PROC_SLOTS, + ERROR_MAX_THRDS_REACHED, + ERROR_NESTING_NOT_ALLOWED: + return EAGAIN + case ERROR_BROKEN_PIPE: + return EPIPE + case ERROR_DISK_FULL: + return ENOSPC + case ERROR_WAIT_NO_CHILDREN, + ERROR_CHILD_NOT_COMPLETE: + return ECHILD + case ERROR_DIR_NOT_EMPTY: + return ENOTEMPTY + case ERROR_NO_UNICODE_TRANSLATION: + return EILSEQ + default: + return EINVAL + } +} + #endif diff --git a/Tests/SystemTests/FileOperationsTest.swift b/Tests/SystemTests/FileOperationsTest.swift index ca178ba..ee82128 100644 --- a/Tests/SystemTests/FileOperationsTest.swift +++ b/Tests/SystemTests/FileOperationsTest.swift @@ -113,8 +113,7 @@ final class FileOperationsTest: XCTestCase { func testHelpers() { // TODO: Test writeAll, writeAll(toAbsoluteOffset), closeAfter } - -#if !os(Windows) + func testAdHocPipe() throws { // Ad-hoc test testing `Pipe` functionality. // We cannot test `Pipe` using `MockTestCase` because it calls `pipe` with a pointer to an array local to the `Pipe`, the address of which we do not know prior to invoking `Pipe`. @@ -133,33 +132,34 @@ final class FileOperationsTest: XCTestCase { } } } -#endif func testAdHocOpen() { // Ad-hoc test touching a file system. do { // TODO: Test this against a virtual in-memory file system - let fd = try FileDescriptor.open("/tmp/b.txt", .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite) - try fd.closeAfter { - try fd.writeAll("abc".utf8) - var def = "def" - try def.withUTF8 { - _ = try fd.write(UnsafeRawBufferPointer($0)) - } - try fd.seek(offset: 1, from: .start) + try withTemporaryPath(basename: "testAdhocOpen") { path in + let fd = try FileDescriptor.open(path.appending("b.txt"), .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite) + try fd.closeAfter { + try fd.writeAll("abc".utf8) + var def = "def" + try def.withUTF8 { + _ = try fd.write(UnsafeRawBufferPointer($0)) + } + try fd.seek(offset: 1, from: .start) - let readLen = 3 - let readBytes = try Array(unsafeUninitializedCapacity: readLen) { (buf, count) in - count = try fd.read(into: UnsafeMutableRawBufferPointer(buf)) - } - let preadBytes = try Array(unsafeUninitializedCapacity: readLen) { (buf, count) in - count = try fd.read(fromAbsoluteOffset: 1, into: UnsafeMutableRawBufferPointer(buf)) - } + let readLen = 3 + let readBytes = try Array(unsafeUninitializedCapacity: readLen) { (buf, count) in + count = try fd.read(into: UnsafeMutableRawBufferPointer(buf)) + } + let preadBytes = try Array(unsafeUninitializedCapacity: readLen) { (buf, count) in + count = try fd.read(fromAbsoluteOffset: 1, into: UnsafeMutableRawBufferPointer(buf)) + } - XCTAssertEqual(readBytes.first!, "b".utf8.first!) - XCTAssertEqual(readBytes, preadBytes) + XCTAssertEqual(readBytes.first!, "b".utf8.first!) + XCTAssertEqual(readBytes, preadBytes) - // TODO: seek + // TODO: seek + } } } catch let err as Errno { print("caught \(err))") @@ -185,48 +185,48 @@ final class FileOperationsTest: XCTestCase { } -#if !os(Windows) func testResizeFile() throws { - let fd = try FileDescriptor.open("/tmp/\(UUID().uuidString).txt", .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite) - try fd.closeAfter { - // File should be empty initially. - XCTAssertEqual(try fd.fileSize(), 0) - // Write 3 bytes. - try fd.writeAll("abc".utf8) - // File should now be 3 bytes. - XCTAssertEqual(try fd.fileSize(), 3) - // Resize to 6 bytes. - try fd.resize(to: 6) - // File should now be 6 bytes. - XCTAssertEqual(try fd.fileSize(), 6) - // Read in the 6 bytes. - let readBytes = try Array(unsafeUninitializedCapacity: 6) { (buf, count) in - try fd.seek(offset: 0, from: .start) - // Should have read all 6 bytes. - count = try fd.read(into: UnsafeMutableRawBufferPointer(buf)) - XCTAssertEqual(count, 6) + try withTemporaryPath(basename: "testResizeFile") { path in + let fd = try FileDescriptor.open(path.appending("\(UUID().uuidString).txt"), .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite) + try fd.closeAfter { + // File should be empty initially. + XCTAssertEqual(try fd.fileSize(), 0) + // Write 3 bytes. + try fd.writeAll("abc".utf8) + // File should now be 3 bytes. + XCTAssertEqual(try fd.fileSize(), 3) + // Resize to 6 bytes. + try fd.resize(to: 6) + // File should now be 6 bytes. + XCTAssertEqual(try fd.fileSize(), 6) + // Read in the 6 bytes. + let readBytes = try Array(unsafeUninitializedCapacity: 6) { (buf, count) in + try fd.seek(offset: 0, from: .start) + // Should have read all 6 bytes. + count = try fd.read(into: UnsafeMutableRawBufferPointer(buf)) + XCTAssertEqual(count, 6) + } + // First 3 bytes should be unaffected by resize. + XCTAssertEqual(Array(readBytes[..<3]), Array("abc".utf8)) + // Extension should be padded with zeros. + XCTAssertEqual(Array(readBytes[3...]), Array(repeating: 0, count: 3)) + // File should still be 6 bytes. + XCTAssertEqual(try fd.fileSize(), 6) + // Resize to 2 bytes. + try fd.resize(to: 2) + // File should now be 2 bytes. + XCTAssertEqual(try fd.fileSize(), 2) + // Read in file with a buffer big enough for 6 bytes. + let readBytesAfterTruncation = try Array(unsafeUninitializedCapacity: 6) { (buf, count) in + try fd.seek(offset: 0, from: .start) + count = try fd.read(into: UnsafeMutableRawBufferPointer(buf)) + // Should only have read 2 bytes. + XCTAssertEqual(count, 2) + } + // Written content was trunctated. + XCTAssertEqual(readBytesAfterTruncation, Array("ab".utf8)) } - // First 3 bytes should be unaffected by resize. - XCTAssertEqual(Array(readBytes[..<3]), Array("abc".utf8)) - // Extension should be padded with zeros. - XCTAssertEqual(Array(readBytes[3...]), Array(repeating: 0, count: 3)) - // File should still be 6 bytes. - XCTAssertEqual(try fd.fileSize(), 6) - // Resize to 2 bytes. - try fd.resize(to: 2) - // File should now be 2 bytes. - XCTAssertEqual(try fd.fileSize(), 2) - // Read in file with a buffer big enough for 6 bytes. - let readBytesAfterTruncation = try Array(unsafeUninitializedCapacity: 6) { (buf, count) in - try fd.seek(offset: 0, from: .start) - count = try fd.read(into: UnsafeMutableRawBufferPointer(buf)) - // Should only have read 2 bytes. - XCTAssertEqual(count, 2) - } - // Written content was trunctated. - XCTAssertEqual(readBytesAfterTruncation, Array("ab".utf8)) } } -#endif } diff --git a/Tests/SystemTests/FilePathTests/FilePathComponentsTest.swift b/Tests/SystemTests/FilePathTests/FilePathComponentsTest.swift index ad69cb4..e816bb5 100644 --- a/Tests/SystemTests/FilePathTests/FilePathComponentsTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathComponentsTest.swift @@ -231,7 +231,7 @@ final class FilePathComponentsTest: XCTestCase { } func testCases() { - let testPaths: Array = [ + var testPaths: Array = [ TestPathComponents("", root: nil, []), TestPathComponents("/", root: "/", []), TestPathComponents("foo", root: nil, ["foo"]), @@ -240,16 +240,28 @@ final class FilePathComponentsTest: XCTestCase { TestPathComponents("foo/bar", root: nil, ["foo", "bar"]), TestPathComponents("foo/bar/", root: nil, ["foo", "bar"]), TestPathComponents("/foo/bar", root: "/", ["foo", "bar"]), - TestPathComponents("///foo//", root: "/", ["foo"]), TestPathComponents("/foo///bar", root: "/", ["foo", "bar"]), TestPathComponents("foo/bar/", root: nil, ["foo", "bar"]), TestPathComponents("foo///bar/baz/", root: nil, ["foo", "bar", "baz"]), - TestPathComponents("//foo///bar/baz/", root: "/", ["foo", "bar", "baz"]), TestPathComponents("./", root: nil, ["."]), TestPathComponents("./..", root: nil, [".", ".."]), TestPathComponents("/./..//", root: "/", [".", ".."]), ] - testPaths.forEach { $0.runAllTests() } +#if !os(Windows) + testPaths.append(contentsOf:[ + TestPathComponents("///foo//", root: "/", ["foo"]), + TestPathComponents("//foo///bar/baz/", root: "/", ["foo", "bar", "baz"]) + ]) +#else + // On Windows, these are UNC paths + testPaths.append(contentsOf:[ + TestPathComponents("///foo//", root: "///foo//", []), + TestPathComponents("//foo///bar/baz/", root: "//foo//", ["bar", "baz"]) + ]) +#endif + testPaths.forEach { + $0.runAllTests() + } } func testSeparatorNormalization() { diff --git a/Tests/SystemTests/FilePathTests/FilePathSyntaxTest.swift b/Tests/SystemTests/FilePathTests/FilePathSyntaxTest.swift index ccaa782..d23999a 100644 --- a/Tests/SystemTests/FilePathTests/FilePathSyntaxTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathSyntaxTest.swift @@ -1064,7 +1064,10 @@ final class FilePathSyntaxTest: XCTestCase { XCTAssert(path.lexicallyContains("usr")) XCTAssert(path.lexicallyContains("/usr")) XCTAssert(path.lexicallyContains("local/bin")) +#if !os(Windows) + // On Windows, this is a relative path and is still contained XCTAssert(!path.lexicallyContains("/local/bin")) +#endif path.append("..") XCTAssert(!path.lexicallyContains("local/bin")) XCTAssert(path.lexicallyContains("local/bin/..")) diff --git a/Tests/SystemTests/FilePathTests/FilePathTempTest.swift b/Tests/SystemTests/FilePathTests/FilePathTempTest.swift new file mode 100644 index 0000000..0948f78 --- /dev/null +++ b/Tests/SystemTests/FilePathTests/FilePathTempTest.swift @@ -0,0 +1,49 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2024 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information +*/ + +import XCTest + +#if SYSTEM_PACKAGE +import SystemPackage +#else +import System +#endif + +final class TemporaryPathTest: XCTestCase { + func testUnique() throws { + try withTemporaryPath(basename: "test") { path in + let strPath = String(decoding: path) + XCTAssert(strPath.contains("test")) + try withTemporaryPath(basename: "test") { path2 in + let strPath2 = String(decoding: path2) + XCTAssertNotEqual(strPath, strPath2) + } + } + } + + func testCleanup() throws { + var thePath: FilePath? = nil + + try withTemporaryPath(basename: "test") { path in + thePath = path.appending("foo.txt") + let fd = try FileDescriptor.open(thePath!, .readWrite, + options: [.create, .truncate], + permissions: .ownerReadWrite) + _ = try fd.closeAfter { + try fd.writeAll("Hello World".utf8) + } + } + + XCTAssertThrowsError(try FileDescriptor.open(thePath!, .readOnly)) { + error in + + XCTAssertEqual(error as! Errno, Errno.noSuchFileOrDirectory) + } + } +} diff --git a/Tests/SystemTests/FilePathTests/FilePathTest.swift b/Tests/SystemTests/FilePathTests/FilePathTest.swift index 1686faa..b50cc17 100644 --- a/Tests/SystemTests/FilePathTests/FilePathTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathTest.swift @@ -34,6 +34,16 @@ final class FilePathTest: XCTestCase { let filePath: FilePath let string: String let validString: Bool + + init(filePath: FilePath, string: String, validString: Bool) { + self.filePath = filePath + #if os(Windows) + self.string = string.replacingOccurrences(of: "/", with: "\\") + #else + self.string = string + #endif + self.validString = validString + } } #if os(Windows) From 85de4e51e22070056e89dfbc396f23131bb711a5 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Wed, 20 Mar 2024 17:10:30 +0000 Subject: [PATCH 12/38] Use the proper temporary directory on Darwin. It seems we can get the temporary directory from confstr(). rdar://125087707 --- Sources/System/FilePath/FilePathTemp.swift | 26 +++++++++++++++++++ Sources/System/Internals/Syscalls.swift | 12 +++++++++ .../FilePathTests/FilePathTempTest.swift | 9 +++++++ 3 files changed, 47 insertions(+) diff --git a/Sources/System/FilePath/FilePathTemp.swift b/Sources/System/FilePath/FilePathTemp.swift index 9aa65ff..0028671 100644 --- a/Sources/System/FilePath/FilePathTemp.swift +++ b/Sources/System/FilePath/FilePathTemp.swift @@ -111,7 +111,33 @@ internal func recursiveRemove(at path: FilePath) throws { #else internal func getTemporaryDirectory() throws -> FilePath { + #if SYSTEM_PACKAGE_DARWIN + var capacity = 1024 + while true { + let path: FilePath? = withUnsafeTemporaryAllocation( + of: CInterop.PlatformChar.self, + capacity: 1024) { buffer in + let len = system_confstr(SYSTEM_CS_DARWIN_USER_TEMP_DIR, + buffer.baseAddress!, + buffer.count) + if len == 0 { + // Fall back to "/tmp" if we can't read the temp directory + return "/tmp" + } + // If it was truncated, increase capaciy and try again + if len > buffer.count { + capacity = len + return nil + } + return FilePath(SystemString(platformString: buffer.baseAddress!)) + } + if let path = path { + return path + } + } + #else return "/tmp" + #endif } internal func recursiveRemove(at path: FilePath) throws { diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index 4669141..cec8413 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -173,6 +173,18 @@ internal func system_rmdir( return rmdir(path) } +#if SYSTEM_PACKAGE_DARWIN +internal let SYSTEM_CS_DARWIN_USER_TEMP_DIR = _CS_DARWIN_USER_TEMP_DIR + +internal func system_confstr( + _ name: CInt, + _ buf: UnsafeMutablePointer, + _ len: Int +) -> Int { + return confstr(name, buf, len) +} +#endif + #if !os(Windows) internal let SYSTEM_AT_REMOVE_DIR = AT_REMOVEDIR internal let SYSTEM_DT_DIR = DT_DIR diff --git a/Tests/SystemTests/FilePathTests/FilePathTempTest.swift b/Tests/SystemTests/FilePathTests/FilePathTempTest.swift index 0948f78..b2b1fa2 100644 --- a/Tests/SystemTests/FilePathTests/FilePathTempTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathTempTest.swift @@ -16,6 +16,15 @@ import System #endif final class TemporaryPathTest: XCTestCase { + #if SYSTEM_PACKAGE_DARWIN + func testNotInSlashTmp() throws { + try withTemporaryPath(basename: "NotInSlashTmp") { path in + // We shouldn't be using "/tmp" on Darwin + XCTAssertNotEqual(path.components.first!, "tmp") + } + } + #endif + func testUnique() throws { try withTemporaryPath(basename: "test") { path in let strPath = String(decoding: path) From 4cafcc96f6aa635e1bdbe05a4c9b858b752f08cf Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Wed, 20 Mar 2024 17:27:38 +0000 Subject: [PATCH 13/38] Changes after comments from Saleem. Use idiomatic Swift notation, not `Optional`. Prefer Win32 APIs to UCRT APIs. rdar://125087707 --- Sources/System/FilePath/FilePathTemp.swift | 5 ++--- Sources/System/Internals/Mocking.swift | 4 ++-- .../System/Internals/WindowsSyscallAdapters.swift | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Sources/System/FilePath/FilePathTemp.swift b/Sources/System/FilePath/FilePathTemp.swift index 0028671..437d233 100644 --- a/Sources/System/FilePath/FilePathTemp.swift +++ b/Sources/System/FilePath/FilePathTemp.swift @@ -29,8 +29,7 @@ internal func getTemporaryDirectory() throws -> FilePath { capacity: Int(MAX_PATH) + 1) { buffer in - let count = GetTempPath2W(DWORD(buffer.count), buffer.baseAddress) - if count == 0 { + guard GetTempPath2W(DWORD(buffer.count), buffer.baseAddress) != 0 else { throw Errno(windowsError: GetLastError()) } @@ -116,7 +115,7 @@ internal func getTemporaryDirectory() throws -> FilePath { while true { let path: FilePath? = withUnsafeTemporaryAllocation( of: CInterop.PlatformChar.self, - capacity: 1024) { buffer in + capacity: capacity) { buffer in let len = system_confstr(SYSTEM_CS_DARWIN_USER_TEMP_DIR, buffer.baseAddress!, buffer.count) diff --git a/Sources/System/Internals/Mocking.swift b/Sources/System/Internals/Mocking.swift index 08c0f71..2945651 100644 --- a/Sources/System/Internals/Mocking.swift +++ b/Sources/System/Internals/Mocking.swift @@ -110,7 +110,7 @@ private var contextualMockingEnabled: Bool { extension MockingDriver { internal static var enabled: Bool { mockingEnabled } - internal static var forceWindowsPaths: Optional { + internal static var forceWindowsPaths: Bool? { currentMockingDriver?.forceWindowsSyntaxForPaths } } @@ -128,7 +128,7 @@ internal var mockingEnabled: Bool { } @inline(__always) -internal var forceWindowsPaths: Optional { +internal var forceWindowsPaths: Bool? { #if !ENABLE_MOCKING return false #else diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index bc83782..6d5c755 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -160,14 +160,25 @@ internal func mkdir( _ path: UnsafePointer, _ mode: CInterop.Mode ) -> CInt { - return _wmkdir(path) + // TODO: Read/write permissions (these need mapping to a SECURITY_DESCRIPTOR). + if !CreateDirectoryW(path, nil) { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + + return 0; } @inline(__always) internal func rmdir( _ path: UnsafePointer ) -> CInt { - return _wrmdir(path) + if !RemoveDirectoryW(path) { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + + return 0; } @usableFromInline From e293300430327554476a38ead2a022be3a2c2315 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Thu, 21 Mar 2024 16:15:19 +0000 Subject: [PATCH 14/38] Map mode bits to Windows ACLs. This adds support for mapping POSIX-style mode bits to Windows ACLs when calling `open()` or `mkdir()`. rdar://125087707 --- Sources/System/FilePath/FilePathTemp.swift | 23 +- .../Internals/WindowsSyscallAdapters.swift | 392 +++++++++++++++++- 2 files changed, 395 insertions(+), 20 deletions(-) diff --git a/Sources/System/FilePath/FilePathTemp.swift b/Sources/System/FilePath/FilePathTemp.swift index 437d233..23a0816 100644 --- a/Sources/System/FilePath/FilePathTemp.swift +++ b/Sources/System/FilePath/FilePathTemp.swift @@ -24,7 +24,7 @@ public func withTemporaryPath(basename: FilePath.Component, #if os(Windows) import WinSDK -internal func getTemporaryDirectory() throws -> FilePath { +fileprivate func getTemporaryDirectory() throws -> FilePath { return try withUnsafeTemporaryAllocation(of: CInterop.PlatformChar.self, capacity: Int(MAX_PATH) + 1) { buffer in @@ -37,7 +37,7 @@ internal func getTemporaryDirectory() throws -> FilePath { } } -internal func forEachFile( +fileprivate func forEachFile( at path: FilePath, _ body: (WIN32_FIND_DATAW) throws -> () ) rethrows { @@ -67,7 +67,7 @@ internal func forEachFile( } } -internal func recursiveRemove(at path: FilePath) throws { +fileprivate func recursiveRemove(at path: FilePath) throws { // First, deal with subdirectories try forEachFile(at: path) { findData in if (findData.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY)) != 0 { @@ -109,13 +109,14 @@ internal func recursiveRemove(at path: FilePath) throws { } #else -internal func getTemporaryDirectory() throws -> FilePath { +fileprivate func getTemporaryDirectory() throws -> FilePath { #if SYSTEM_PACKAGE_DARWIN var capacity = 1024 while true { let path: FilePath? = withUnsafeTemporaryAllocation( of: CInterop.PlatformChar.self, - capacity: capacity) { buffer in + capacity: capacity + ) { buffer in let len = system_confstr(SYSTEM_CS_DARWIN_USER_TEMP_DIR, buffer.baseAddress!, buffer.count) @@ -139,7 +140,7 @@ internal func getTemporaryDirectory() throws -> FilePath { #endif } -internal func recursiveRemove(at path: FilePath) throws { +fileprivate func recursiveRemove(at path: FilePath) throws { let dirfd = try FileDescriptor.open(path, .readOnly, options: .directory) defer { try? dirfd.close() @@ -173,7 +174,7 @@ fileprivate func impl_opendirat( return system_fdopendir(fd) } -internal func forEachFile( +fileprivate func forEachFile( in dirfd: CInt, path: UnsafePointer, _ body: (system_dirent) throws -> () ) throws { @@ -238,11 +239,11 @@ internal func recursiveRemove( } #endif -internal let base64 = Array( +fileprivate let base64 = Array( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".utf8 ) -internal func makeDirectory(at: FilePath) throws -> Bool { +fileprivate func makeTempDirectory(at: FilePath) throws -> Bool { return try at.withPlatformString { if system_mkdir($0, 0o700) == 0 { return true @@ -256,7 +257,7 @@ internal func makeDirectory(at: FilePath) throws -> Bool { } } -internal func createRandomString(length: Int) -> String { +fileprivate func createRandomString(length: Int) -> String { return String( decoding: (0.., _ oflag: Int32 ) -> CInt { - var fh: CInt = -1 - _ = _wsopen_s(&fh, path, oflag, _SH_DENYNO, _S_IREAD | _S_IWRITE) - return fh + if (oflag & _O_CREAT) != 0 { + ucrt._set_errno(EINVAL) + return -1 + } + + let decodedFlags = DecodedOpenFlags(oflag) + + var saAttrs = SECURITY_ATTRIBUTES( + nLength: DWORD(MemoryLayout.size), + lpSecurityDescriptor: nil, + bInheritHandle: decodedFlags.bInheritHandle + ) + + let hFile = CreateFileW(path, + decodedFlags.dwDesiredAccess, + DWORD(FILE_SHARE_DELETE + | FILE_SHARE_READ + | FILE_SHARE_WRITE), + &saAttrs, + decodedFlags.dwCreationDisposition, + decodedFlags.dwFlagsAndAttributes, + nil) + + if hFile == INVALID_HANDLE_VALUE { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + + return _open_osfhandle(intptr_t(bitPattern: hFile), oflag); } @inline(__always) @@ -26,10 +52,39 @@ internal func open( _ path: UnsafePointer, _ oflag: Int32, _ mode: CInterop.Mode ) -> CInt { - // TODO(compnerd): Apply read/write permissions - var fh: CInt = -1 - _ = _wsopen_s(&fh, path, oflag, _SH_DENYNO, _S_IREAD | _S_IWRITE) - return fh + guard let pSD = createSecurityDescriptor(from: mode) else { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + + defer { + pSD.deallocate() + } + + let decodedFlags = DecodedOpenFlags(oflag) + + var saAttrs = SECURITY_ATTRIBUTES( + nLength: DWORD(MemoryLayout.size), + lpSecurityDescriptor: pSD, + bInheritHandle: decodedFlags.bInheritHandle + ) + + let hFile = CreateFileW(path, + decodedFlags.dwDesiredAccess, + DWORD(FILE_SHARE_DELETE + | FILE_SHARE_READ + | FILE_SHARE_WRITE), + &saAttrs, + decodedFlags.dwCreationDisposition, + decodedFlags.dwFlagsAndAttributes, + nil) + + if hFile == INVALID_HANDLE_VALUE { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + + return _open_osfhandle(intptr_t(bitPattern: hFile), oflag); } @inline(__always) @@ -160,8 +215,21 @@ internal func mkdir( _ path: UnsafePointer, _ mode: CInterop.Mode ) -> CInt { - // TODO: Read/write permissions (these need mapping to a SECURITY_DESCRIPTOR). - if !CreateDirectoryW(path, nil) { + guard let pSD = createSecurityDescriptor(from: mode) else { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + defer { + pSD.deallocate() + } + + var saAttrs = SECURITY_ATTRIBUTES( + nLength: DWORD(MemoryLayout.size), + lpSecurityDescriptor: pSD, + bInheritHandle: false + ) + + if !CreateDirectoryW(path, &saAttrs) { ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) return -1 } @@ -254,4 +322,310 @@ internal func mapWindowsErrorToErrno(_ errorCode: DWORD) -> CInt { } } +fileprivate func rightsFromModeBits( + _ bits: Int, sticky: Bool = false +) -> DWORD { + var rights: DWORD = 0 + + if (bits & 0o4) != 0 { + rights |= DWORD(FILE_READ_ATTRIBUTES + | FILE_READ_DATA + | FILE_READ_EA + | STANDARD_RIGHTS_READ + | SYNCHRONIZE) + } + if (bits & 0o2) != 0 { + rights |= DWORD(FILE_APPEND_DATA + | FILE_WRITE_ATTRIBUTES + | FILE_WRITE_DATA + | FILE_WRITE_EA + | STANDARD_RIGHTS_WRITE + | SYNCHRONIZE) + if !sticky { + rights |= DWORD(FILE_DELETE_CHILD) + } + } + if (bits & 0o1) != 0 { + rights |= DWORD(FILE_EXECUTE + | FILE_READ_ATTRIBUTES + | STANDARD_RIGHTS_EXECUTE + | SYNCHRONIZE) + } + + return rights +} + +fileprivate func getTokenInformation( + of: T.Type, + hToken: HANDLE, + ticTokenClass: TOKEN_INFORMATION_CLASS +) -> UnsafePointer? { + var capacity = 1024 + for _ in 0..<2 { + let buffer = UnsafeMutableRawPointer.allocate( + byteCount: capacity, + alignment: MemoryLayout.alignment + ) + + var dwLength = DWORD(0) + + if GetTokenInformation(hToken, + ticTokenClass, + buffer, + DWORD(capacity), + &dwLength) { + return UnsafePointer(buffer.assumingMemoryBound(to: T.self)) + } + + buffer.deallocate() + + capacity = Int(dwLength) + } + return nil +} + +/// Build a SECURITY_DESCRIPTOR from UNIX-style "mode" bits. This only +/// takes account of the rwx and sticky bits; there's really nothing that +/// we can do about setuid/setgid. +@usableFromInline +internal func createSecurityDescriptor(from mode: CInterop.Mode) + -> PSECURITY_DESCRIPTOR? { + let ownerPerm = (Int(mode) >> 6) & 0o7 + let groupPerm = (Int(mode) >> 3) & 0o7 + let otherPerm = Int(mode) & 0o7 + + let ownerRights = rightsFromModeBits(ownerPerm) + let groupRights = rightsFromModeBits(groupPerm, sticky: (mode & 0o1000) != 0) + let otherRights = rightsFromModeBits(otherPerm, sticky: (mode & 0o1000) != 0) + + // If group or other permissions are *more* permissive, then we need + // some DENY ACEs as well to implement the expected semantics + let ownerDenyRights = ((ownerRights ^ groupRights) & groupRights) | + ((ownerRights ^ otherRights) & otherRights) + let groupDenyRights = (groupRights ^ otherRights) & otherRights + + var SIDAuthWorld = SID_IDENTIFIER_AUTHORITY(Value: (0, 0, 0, 0, 0, 1)) + var everyone: PSID? = nil + + guard AllocateAndInitializeSid(&SIDAuthWorld, 1, + DWORD(SECURITY_WORLD_RID), + 0, 0, 0, 0, 0, 0, 0, + &everyone) else { + return nil + } + guard let everyone = everyone else { + return nil + } + defer { + FreeSid(everyone) + } + + let hToken = GetCurrentThreadEffectiveToken()! + + guard let pTokenUser = getTokenInformation(of: TOKEN_USER.self, + hToken: hToken, + ticTokenClass: TokenUser) else { + return nil + } + defer { + pTokenUser.deallocate() + } + + guard let pTokenPrimaryGroup = getTokenInformation( + of: TOKEN_PRIMARY_GROUP.self, + hToken: hToken, + ticTokenClass: TokenPrimaryGroup + ) else { + return nil + } + defer { + pTokenPrimaryGroup.deallocate() + } + + let user = pTokenUser.pointee.User.Sid! + let group = pTokenPrimaryGroup.pointee.PrimaryGroup! + + var eas = [ + EXPLICIT_ACCESS_W( + grfAccessPermissions: ownerRights, + grfAccessMode: GRANT_ACCESS, + grfInheritance: DWORD(NO_INHERITANCE), + Trustee: TRUSTEE_W( + pMultipleTrustee: nil, + MultipleTrusteeOperation: NO_MULTIPLE_TRUSTEE, + TrusteeForm: TRUSTEE_IS_SID, + TrusteeType: TRUSTEE_IS_USER, + ptstrName: + user.assumingMemoryBound(to: CInterop.PlatformChar.self) + ) + ), + EXPLICIT_ACCESS_W( + grfAccessPermissions: groupRights, + grfAccessMode: GRANT_ACCESS, + grfInheritance: DWORD(NO_INHERITANCE), + Trustee: TRUSTEE_W( + pMultipleTrustee: nil, + MultipleTrusteeOperation: NO_MULTIPLE_TRUSTEE, + TrusteeForm: TRUSTEE_IS_SID, + TrusteeType: TRUSTEE_IS_GROUP, + ptstrName: + group.assumingMemoryBound(to: CInterop.PlatformChar.self) + ) + ), + EXPLICIT_ACCESS_W( + grfAccessPermissions: otherRights, + grfAccessMode: GRANT_ACCESS, + grfInheritance: DWORD(NO_INHERITANCE), + Trustee: TRUSTEE_W( + pMultipleTrustee: nil, + MultipleTrusteeOperation: NO_MULTIPLE_TRUSTEE, + TrusteeForm: TRUSTEE_IS_SID, + TrusteeType: TRUSTEE_IS_GROUP, + ptstrName: + everyone.assumingMemoryBound(to: CInterop.PlatformChar.self) + ) + ) + ] + + if ownerDenyRights != 0 { + eas.append( + EXPLICIT_ACCESS_W( + grfAccessPermissions: ownerDenyRights, + grfAccessMode: DENY_ACCESS, + grfInheritance: DWORD(NO_INHERITANCE), + Trustee: TRUSTEE_W( + pMultipleTrustee: nil, + MultipleTrusteeOperation: NO_MULTIPLE_TRUSTEE, + TrusteeForm: TRUSTEE_IS_SID, + TrusteeType: TRUSTEE_IS_USER, + ptstrName: + user.assumingMemoryBound(to: CInterop.PlatformChar.self) + ) + ) + ) + } + + if groupDenyRights != 0 { + eas.append( + EXPLICIT_ACCESS_W( + grfAccessPermissions: groupDenyRights, + grfAccessMode: DENY_ACCESS, + grfInheritance: DWORD(NO_INHERITANCE), + Trustee: TRUSTEE_W( + pMultipleTrustee: nil, + MultipleTrusteeOperation: NO_MULTIPLE_TRUSTEE, + TrusteeForm: TRUSTEE_IS_SID, + TrusteeType: TRUSTEE_IS_GROUP, + ptstrName: + group.assumingMemoryBound(to: CInterop.PlatformChar.self) + ) + ) + ) + } + + var pACL: PACL? = nil + guard SetEntriesInAclW(ULONG(eas.count), + &eas, + nil, + &pACL) == ERROR_SUCCESS else { + return nil + } + defer { + LocalFree(pACL) + } + + // Create the security descriptor, making sure that inherited ACEs don't + // take effect, since that wouldn't match the behaviour of mode bits. + var descriptor = SECURITY_DESCRIPTOR() + + guard InitializeSecurityDescriptor(&descriptor, + DWORD(SECURITY_DESCRIPTOR_REVISION)) else { + return nil + } + + guard SetSecurityDescriptorControl(&descriptor, + SECURITY_DESCRIPTOR_CONTROL(SE_DACL_PROTECTED), + SECURITY_DESCRIPTOR_CONTROL(SE_DACL_PROTECTED)) + && SetSecurityDescriptorOwner(&descriptor, user, false) + && SetSecurityDescriptorGroup(&descriptor, group, false) + && SetSecurityDescriptorDacl(&descriptor, + true, + pACL, + false) else { + return nil + } + + // Make it self-contained (up to this point it uses pointers) + var dwRelativeSize = DWORD(0) + + guard !MakeSelfRelativeSD(&descriptor, nil, &dwRelativeSize) + && GetLastError() == ERROR_INSUFFICIENT_BUFFER else { + return nil + } + + let pDescriptor = UnsafeMutableRawPointer.allocate( + byteCount: Int(dwRelativeSize), + alignment: MemoryLayout.alignment + ).assumingMemoryBound(to: SECURITY_DESCRIPTOR.self) + + guard MakeSelfRelativeSD(&descriptor, pDescriptor, &dwRelativeSize) else { + pDescriptor.deallocate() + return nil + } + + return UnsafeMutableRawPointer(pDescriptor) +} + +fileprivate struct DecodedOpenFlags { + var dwDesiredAccess: DWORD + var dwCreationDisposition: DWORD + var bInheritHandle: WindowsBool + var dwFlagsAndAttributes: DWORD + + init(_ oflag: Int32) { + switch oflag & (_O_CREAT | _O_EXCL | _O_TRUNC) { + case _O_CREAT | _O_EXCL, _O_CREAT | _O_EXCL | _O_TRUNC: + dwCreationDisposition = DWORD(CREATE_NEW) + case _O_CREAT: + dwCreationDisposition = DWORD(OPEN_ALWAYS) + case _O_CREAT | _O_TRUNC: + dwCreationDisposition = DWORD(CREATE_ALWAYS) + case _O_TRUNC: + dwCreationDisposition = DWORD(TRUNCATE_EXISTING) + default: + dwCreationDisposition = DWORD(OPEN_EXISTING) + } + + dwDesiredAccess = 0 + if (oflag & _O_RDONLY) != 0 { + dwDesiredAccess |= DWORD(GENERIC_READ) + } + if (oflag & _O_WRONLY) != 0 { + dwDesiredAccess |= DWORD(GENERIC_WRITE) + } + if (oflag & _O_RDWR) != 0 { + dwDesiredAccess |= DWORD(GENERIC_READ) | DWORD(GENERIC_WRITE) + } + + bInheritHandle = WindowsBool((oflag & _O_NOINHERIT) == 0) + + dwFlagsAndAttributes = 0 + if (oflag & _O_SEQUENTIAL) != 0 { + dwFlagsAndAttributes |= DWORD(FILE_FLAG_SEQUENTIAL_SCAN) + } + if (oflag & _O_RANDOM) != 0 { + dwFlagsAndAttributes |= DWORD(FILE_FLAG_RANDOM_ACCESS) + } + if (oflag & _O_TEMPORARY) != 0 { + dwFlagsAndAttributes |= DWORD(FILE_FLAG_DELETE_ON_CLOSE) + } + + if (oflag & _O_SHORT_LIVED) != 0 { + dwFlagsAndAttributes |= DWORD(FILE_ATTRIBUTE_TEMPORARY) + } else { + dwFlagsAndAttributes |= DWORD(FILE_ATTRIBUTE_NORMAL) + } + } +} + #endif From bcc7f6d5693d913acc42539ea5765f0fe89ccef8 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Thu, 21 Mar 2024 16:32:19 +0000 Subject: [PATCH 15/38] Update Sources/System/FilePath/FilePathTemp.swift Co-authored-by: Karoy Lorentey --- Sources/System/FilePath/FilePathTemp.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/System/FilePath/FilePathTemp.swift b/Sources/System/FilePath/FilePathTemp.swift index 23a0816..ea67428 100644 --- a/Sources/System/FilePath/FilePathTemp.swift +++ b/Sources/System/FilePath/FilePathTemp.swift @@ -9,8 +9,10 @@ // MARK: - API -public func withTemporaryPath(basename: FilePath.Component, - _ body: (FilePath) throws -> R) throws -> R { +public func withTemporaryPath( + basename: FilePath.Component, + _ body: (FilePath) throws -> R +) throws -> R { let temporaryDir = try createUniqueTemporaryDirectory(basename: basename) defer { try? recursiveRemove(at: temporaryDir) From 5ae5d5a3afd5428511f906fcb8bf341803befc9a Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Mon, 25 Mar 2024 18:28:18 +0000 Subject: [PATCH 16/38] Refactor FilePathTemp, add tests for permission code. Broke `FilePathTemp.swift` into separate files for POSIX and Windows. Added tests for permissions code on Windows. Added leading underscores to some function names. Made some other functions `fileprivate`. rdar://125087707 --- Sources/System/ErrnoWindows.swift | 2 +- Sources/System/FileOperations.swift | 46 ++- Sources/System/FilePath/FilePathTemp.swift | 263 +++--------------- .../System/FilePath/FilePathTempPosix.swift | 153 ++++++++++ .../System/FilePath/FilePathTempWindows.swift | 114 ++++++++ Sources/System/Internals/Syscalls.swift | 12 + .../Internals/WindowsSyscallAdapters.swift | 72 +++-- Tests/SystemTests/FileOperationsTest.swift | 4 +- .../FileOperationsTestWindows.swift | 241 ++++++++++++++++ .../FilePathTests/FilePathTempTest.swift | 8 +- 10 files changed, 657 insertions(+), 258 deletions(-) create mode 100644 Sources/System/FilePath/FilePathTempPosix.swift create mode 100644 Sources/System/FilePath/FilePathTempWindows.swift create mode 100644 Tests/SystemTests/FileOperationsTestWindows.swift diff --git a/Sources/System/ErrnoWindows.swift b/Sources/System/ErrnoWindows.swift index 98fda43..b94b577 100644 --- a/Sources/System/ErrnoWindows.swift +++ b/Sources/System/ErrnoWindows.swift @@ -13,7 +13,7 @@ import WinSDK extension Errno { public init(windowsError: DWORD) { - self.init(rawValue: mapWindowsErrorToErrno(windowsError)) + self.init(rawValue: _mapWindowsErrorToErrno(windowsError)) } } diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index d2e8d65..ba70e87 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -135,8 +135,6 @@ extension FileDescriptor { if let permissions = permissions { return system_open(path, oFlag, permissions.rawValue) } - precondition(!options.contains(.create), - "Create must be given permissions") return system_open(path, oFlag) } return descOrError.map { FileDescriptor(rawValue: $0) } @@ -510,3 +508,47 @@ extension FileDescriptor { } } } + +extension FilePermissions { + /// The file creation permission mask (aka "umask"). + /// + /// Permissions set in this mask will be cleared by functions that create + /// files or directories. Note that this mask is process-wide, and that + /// *getting* it is not thread safe. + @_alwaysEmitIntoClient + public static var creationMask: FilePermissions { + get { + let oldMask = _umask(0o22) + _ = _umask(oldMask) + return FilePermissions(rawValue: oldMask) + } + set { + _ = _umask(newValue.rawValue) + } + } + + /// Change the file creation permission mask, run some code, then + /// restore it to its original value. + /// + /// - Parameters: + /// - permissions: The new permission mask. + /// + /// This is more efficient than reading `creationMask` and restoring it + /// afterwards, because of the way reading the creation mask works. + @_alwaysEmitIntoClient + public static func withCreationMask( + _ permissions: FilePermissions, + body: () throws -> R + ) rethrows -> R { + let oldMask = _umask(permissions.rawValue) + defer { + _ = _umask(oldMask) + } + return try body() + } + + @usableFromInline + internal static func _umask(_ mode: CModeT) -> CModeT { + return system_umask(mode) + } +} diff --git a/Sources/System/FilePath/FilePathTemp.swift b/Sources/System/FilePath/FilePathTemp.swift index ea67428..7b41d0b 100644 --- a/Sources/System/FilePath/FilePathTemp.swift +++ b/Sources/System/FilePath/FilePathTemp.swift @@ -9,13 +9,22 @@ // MARK: - API -public func withTemporaryPath( +/// Create a temporary path for the duration of the closure. +/// +/// - Parameters: +/// - basename: The base name for the temporary path. +/// - body: The closure to execute. +/// +/// Creates a temporary directory with a name based on the given `basename`, +/// executes `body`, passing in the path of the created directory, then +/// deletes the directory and all of its contents before returning. +public func withTemporaryFilePath( basename: FilePath.Component, _ body: (FilePath) throws -> R ) throws -> R { let temporaryDir = try createUniqueTemporaryDirectory(basename: basename) defer { - try? recursiveRemove(at: temporaryDir) + try? _recursiveRemove(at: temporaryDir) } return try body(temporaryDir) @@ -23,230 +32,20 @@ public func withTemporaryPath( // MARK: - Internals -#if os(Windows) -import WinSDK - -fileprivate func getTemporaryDirectory() throws -> FilePath { - return try withUnsafeTemporaryAllocation(of: CInterop.PlatformChar.self, - capacity: Int(MAX_PATH) + 1) { - buffer in - - guard GetTempPath2W(DWORD(buffer.count), buffer.baseAddress) != 0 else { - throw Errno(windowsError: GetLastError()) - } - - return FilePath(SystemString(platformString: buffer.baseAddress!)) - } -} - -fileprivate func forEachFile( - at path: FilePath, - _ body: (WIN32_FIND_DATAW) throws -> () -) rethrows { - let searchPath = path.appending("\\*") - - try searchPath.withPlatformString { szPath in - var findData = WIN32_FIND_DATAW() - let hFind = FindFirstFileW(szPath, &findData) - if hFind == INVALID_HANDLE_VALUE { - throw Errno(windowsError: GetLastError()) - } - defer { - FindClose(hFind) - } - - repeat { - // Skip . and .. - if findData.cFileName.0 == 46 - && (findData.cFileName.1 == 0 - || (findData.cFileName.1 == 46 - && findData.cFileName.2 == 0)) { - continue - } - - try body(findData) - } while FindNextFileW(hFind, &findData) - } -} - -fileprivate func recursiveRemove(at path: FilePath) throws { - // First, deal with subdirectories - try forEachFile(at: path) { findData in - if (findData.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY)) != 0 { - let name = withUnsafeBytes(of: findData.cFileName) { - return SystemString(platformString: $0.assumingMemoryBound( - to: CInterop.PlatformChar.self).baseAddress!) - } - let component = FilePath.Component(name)! - let subpath = path.appending(component) - - try recursiveRemove(at: subpath) - } - } - - // Now delete everything else - try forEachFile(at: path) { findData in - let name = withUnsafeBytes(of: findData.cFileName) { - return SystemString(platformString: $0.assumingMemoryBound( - to: CInterop.PlatformChar.self).baseAddress!) - } - let component = FilePath.Component(name)! - let subpath = path.appending(component) - - if (findData.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY)) == 0 { - try subpath.withPlatformString { - if !DeleteFileW($0) { - throw Errno(windowsError: GetLastError()) - } - } - } - } - - // Finally, delete the parent - try path.withPlatformString { - if !RemoveDirectoryW($0) { - throw Errno(windowsError: GetLastError()) - } - } -} - -#else -fileprivate func getTemporaryDirectory() throws -> FilePath { - #if SYSTEM_PACKAGE_DARWIN - var capacity = 1024 - while true { - let path: FilePath? = withUnsafeTemporaryAllocation( - of: CInterop.PlatformChar.self, - capacity: capacity - ) { buffer in - let len = system_confstr(SYSTEM_CS_DARWIN_USER_TEMP_DIR, - buffer.baseAddress!, - buffer.count) - if len == 0 { - // Fall back to "/tmp" if we can't read the temp directory - return "/tmp" - } - // If it was truncated, increase capaciy and try again - if len > buffer.count { - capacity = len - return nil - } - return FilePath(SystemString(platformString: buffer.baseAddress!)) - } - if let path = path { - return path - } - } - #else - return "/tmp" - #endif -} - -fileprivate func recursiveRemove(at path: FilePath) throws { - let dirfd = try FileDescriptor.open(path, .readOnly, options: .directory) - defer { - try? dirfd.close() - } - - let dot: (CInterop.PlatformChar, CInterop.PlatformChar) = (46, 0) - try withUnsafeBytes(of: dot) { - try recursiveRemove( - in: dirfd.rawValue, - path: $0.assumingMemoryBound(to: CInterop.PlatformChar.self).baseAddress! - ) - } - - try path.withPlatformString { - if system_rmdir($0) != 0 { - throw Errno.current - } - } -} - -fileprivate func impl_opendirat( - _ dirfd: CInt, - _ path: UnsafePointer -) -> UnsafeMutablePointer? { - let fd = system_openat(dirfd, path, - FileDescriptor.AccessMode.readOnly.rawValue - | FileDescriptor.OpenOptions.directory.rawValue) - if fd < 0 { - return nil - } - return system_fdopendir(fd) -} - -fileprivate func forEachFile( - in dirfd: CInt, path: UnsafePointer, - _ body: (system_dirent) throws -> () -) throws { - guard let dir = impl_opendirat(dirfd, path) else { - throw Errno.current - } - defer { - _ = system_closedir(dir) - } - - while let dirent = system_readdir(dir) { - // Skip . and .. - if dirent.pointee.d_name.0 == 46 - && (dirent.pointee.d_name.1 == 0 - || (dirent.pointee.d_name.1 == 46 - && dirent.pointee.d_name.2 == 0)) { - continue - } - - try body(dirent.pointee) - } -} - -internal func recursiveRemove( - in dirfd: CInt, - path: UnsafePointer -) throws { - // First, deal with subdirectories - try forEachFile(in: dirfd, path: path) { dirent in - if dirent.d_type == SYSTEM_DT_DIR { - try withUnsafeBytes(of: dirent.d_name) { - try recursiveRemove( - in: dirfd, - path: $0.assumingMemoryBound(to: CInterop.PlatformChar.self) - .baseAddress! - ) - } - } - } - - // Now delete the contents of this directory - try forEachFile(in: dirfd, path: path) { dirent in - let flag: CInt - - if dirent.d_type == SYSTEM_DT_DIR { - flag = SYSTEM_AT_REMOVE_DIR - } else { - flag = 0 - } - - let result = withUnsafeBytes(of: dirent.d_name) { - system_unlinkat(dirfd, - $0.assumingMemoryBound(to: CInterop.PlatformChar.self) - .baseAddress!, - flag) - } - - if result != 0 { - throw Errno.current - } - } -} -#endif - fileprivate let base64 = Array( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".utf8 ) -fileprivate func makeTempDirectory(at: FilePath) throws -> Bool { - return try at.withPlatformString { +/// Create a directory that is only accessible to the current user. +/// +/// - Parameters: +/// - path: The path of the directory to create. +/// - Returns: `true` if a new directory was created. +/// +/// This function will throw if there is an error, except if the error +/// is that the directory exists, in which case it returns `false`. +fileprivate func makeLockedDownDirectory(at path: FilePath) throws -> Bool { + return try path.withPlatformString { if system_mkdir($0, 0o700) == 0 { return true } @@ -259,6 +58,11 @@ fileprivate func makeTempDirectory(at: FilePath) throws -> Bool { } } +/// Generate a random string of base64 filename safe characters. +/// +/// - Parameters: +/// - length: The number of characters in the returned string. +/// - Returns: A random string of length `length`. fileprivate func createRandomString(length: Int) -> String { return String( decoding: (0.. String { ) } -internal func createUniqueTemporaryDirectory( +/// Given a base name, create a uniquely named temporary directory. +/// +/// - Parameters: +/// - basename: The base name for the new directory. +/// - Returns: The path to the new directory. +/// +/// Creates a directory in the system temporary directory whose name +/// starts with `basename`, followed by a `.` and then a random +/// string of characters. +fileprivate func createUniqueTemporaryDirectory( basename: FilePath.Component ) throws -> FilePath { - var tempDir = try getTemporaryDirectory() + var tempDir = try _getTemporaryDirectory() tempDir.append(basename) while true { tempDir.extension = createRandomString(length: 16) - if try makeTempDirectory(at: tempDir) { + if try makeLockedDownDirectory(at: tempDir) { return tempDir } } diff --git a/Sources/System/FilePath/FilePathTempPosix.swift b/Sources/System/FilePath/FilePathTempPosix.swift new file mode 100644 index 0000000..623ccae --- /dev/null +++ b/Sources/System/FilePath/FilePathTempPosix.swift @@ -0,0 +1,153 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2024 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information + */ + +#if !os(Windows) + +/// Get the path to the system temporary directory. +internal func _getTemporaryDirectory() throws -> FilePath { + guard let tmp = system_getenv("TMPDIR") else { + return "/tmp" + } + + return FilePath(SystemString(platformString: tmp)) +} + +/// Delete the entire contents of a directory, including its subdirectories. +/// +/// - Parameters: +/// - path: The directory to be deleted. +/// +/// Removes a directory completely, including all of its contents. +internal func _recursiveRemove( + at path: FilePath +) throws { + let dirfd = try FileDescriptor.open(path, .readOnly, options: .directory) + defer { + try? dirfd.close() + } + + let dot: (CInterop.PlatformChar, CInterop.PlatformChar) = (46, 0) + try withUnsafeBytes(of: dot) { + try recursiveRemove( + in: dirfd.rawValue, + name: $0.assumingMemoryBound(to: CInterop.PlatformChar.self).baseAddress! + ) + } + + try path.withPlatformString { + if system_rmdir($0) != 0 { + throw Errno.current + } + } +} + +/// Open a directory by reference to its parent and name. +/// +/// - Parameters: +/// - dirfd: An open file descriptor for the parent directory. +/// - name: The name of the directory to open. +/// - Returns: A pointer to a `DIR` structure. +/// +/// This is like `opendir()`, but instead of taking a path, it uses a +/// file descriptor pointing at the parent, thus avoiding path length +/// limits. +fileprivate func impl_opendirat( + _ dirfd: CInt, + _ name: UnsafePointer +) -> UnsafeMutablePointer? { + let fd = system_openat(dirfd, name, + FileDescriptor.AccessMode.readOnly.rawValue + | FileDescriptor.OpenOptions.directory.rawValue) + if fd < 0 { + return nil + } + return system_fdopendir(fd) +} + +/// Invoke a closure for each file within a particular directory. +/// +/// - Parameters: +/// - dirfd: The parent of the directory to be enumerated. +/// - subdir: The subdirectory to be enumerated. +/// - body: The closure that will be invoked. +/// +/// We skip the `.` and `..` pseudo-entries. +fileprivate func forEachFile( + in dirfd: CInt, + subdir: UnsafePointer, + _ body: (system_dirent) throws -> () +) throws { + guard let dir = impl_opendirat(dirfd, subdir) else { + throw Errno.current + } + defer { + _ = system_closedir(dir) + } + + while let dirent = system_readdir(dir) { + // Skip . and .. + if dirent.pointee.d_name.0 == 46 + && (dirent.pointee.d_name.1 == 0 + || (dirent.pointee.d_name.1 == 46 + && dirent.pointee.d_name.2 == 0)) { + continue + } + + try body(dirent.pointee) + } +} + +/// Delete the entire contents of a directory, including its subdirectories. +/// +/// - Parameters: +/// - dirfd: The parent of the directory to be removed. +/// - name: The name of the directory to be removed. +/// +/// Removes a directory completely, including all of its contents. +fileprivate func recursiveRemove( + in dirfd: CInt, + name: UnsafePointer +) throws { + // First, deal with subdirectories + try forEachFile(in: dirfd, subdir: name) { dirent in + if dirent.d_type == SYSTEM_DT_DIR { + try withUnsafeBytes(of: dirent.d_name) { + try recursiveRemove( + in: dirfd, + name: $0.assumingMemoryBound(to: CInterop.PlatformChar.self) + .baseAddress! + ) + } + } + } + + // Now delete the contents of this directory + try forEachFile(in: dirfd, subdir: name) { dirent in + let flag: CInt + + if dirent.d_type == SYSTEM_DT_DIR { + flag = SYSTEM_AT_REMOVE_DIR + } else { + flag = 0 + } + + let result = withUnsafeBytes(of: dirent.d_name) { + system_unlinkat(dirfd, + $0.assumingMemoryBound(to: CInterop.PlatformChar.self) + .baseAddress!, + flag) + } + + if result != 0 { + throw Errno.current + } + } +} + +#endif // !os(Windows) diff --git a/Sources/System/FilePath/FilePathTempWindows.swift b/Sources/System/FilePath/FilePathTempWindows.swift new file mode 100644 index 0000000..0d97edc --- /dev/null +++ b/Sources/System/FilePath/FilePathTempWindows.swift @@ -0,0 +1,114 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2024 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information +*/ + +#if os(Windows) + +import WinSDK + +/// Get the path to the system temporary directory. +internal func _getTemporaryDirectory() throws -> FilePath { + return try withUnsafeTemporaryAllocation(of: CInterop.PlatformChar.self, + capacity: Int(MAX_PATH) + 1) { + buffer in + + guard GetTempPath2W(DWORD(buffer.count), buffer.baseAddress) != 0 else { + throw Errno(windowsError: GetLastError()) + } + + return FilePath(SystemString(platformString: buffer.baseAddress!)) + } +} + +/// Invoke a closure for each file within a particular directory. +/// +/// - Parameters: +/// - path: The path at which we should enumerate items. +/// - body: The closure that will be invoked. +/// +/// We skip the `.` and `..` pseudo-entries. +fileprivate func forEachFile( + at path: FilePath, + _ body: (WIN32_FIND_DATAW) throws -> () +) rethrows { + let searchPath = path.appending("\\*") + + try searchPath.withPlatformString { szPath in + var findData = WIN32_FIND_DATAW() + let hFind = FindFirstFileW(szPath, &findData) + if hFind == INVALID_HANDLE_VALUE { + throw Errno(windowsError: GetLastError()) + } + defer { + FindClose(hFind) + } + + repeat { + // Skip . and .. + if findData.cFileName.0 == 46 + && (findData.cFileName.1 == 0 + || (findData.cFileName.1 == 46 + && findData.cFileName.2 == 0)) { + continue + } + + try body(findData) + } while FindNextFileW(hFind, &findData) + } +} + +/// Delete the entire contents of a directory, including its subdirectories. +/// +/// - Parameters: +/// - path: The directory to be deleted. +/// +/// Removes a directory completely, including all of its contents. +internal func _recursiveRemove( + at path: FilePath +) throws { + // First, deal with subdirectories + try forEachFile(at: path) { findData in + if (findData.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY)) != 0 { + let name = withUnsafeBytes(of: findData.cFileName) { + return SystemString(platformString: $0.assumingMemoryBound( + to: CInterop.PlatformChar.self).baseAddress!) + } + let component = FilePath.Component(name)! + let subpath = path.appending(component) + + try _recursiveRemove(at: subpath) + } + } + + // Now delete everything else + try forEachFile(at: path) { findData in + let name = withUnsafeBytes(of: findData.cFileName) { + return SystemString(platformString: $0.assumingMemoryBound( + to: CInterop.PlatformChar.self).baseAddress!) + } + let component = FilePath.Component(name)! + let subpath = path.appending(component) + + if (findData.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY)) == 0 { + try subpath.withPlatformString { + if !DeleteFileW($0) { + throw Errno(windowsError: GetLastError()) + } + } + } + } + + // Finally, delete the parent + try path.withPlatformString { + if !RemoveDirectoryW($0) { + throw Errno(windowsError: GetLastError()) + } + } +} + +#endif // os(Windows) diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index cec8413..1c6ed72 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -239,3 +239,15 @@ internal func system_openat( return openat(fd, path, oflag) } #endif + +internal func system_umask( + _ mode: CInterop.Mode +) -> CInterop.Mode { + return umask(mode) +} + +internal func system_getenv( + _ name: UnsafePointer +) -> UnsafeMutablePointer? { + return getenv(name) +} diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index fa88cbe..3e3f9c6 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -12,15 +12,21 @@ import ucrt import WinSDK +fileprivate var _umask: CInterop.Mode = 0o22 + +@inline(__always) +func umask( + _ mode: CInterop.Mode +) -> CInterop.Mode { + let oldMask = _umask + _umask = mode + return oldMask +} + @inline(__always) internal func open( _ path: UnsafePointer, _ oflag: Int32 ) -> CInt { - if (oflag & _O_CREAT) != 0 { - ucrt._set_errno(EINVAL) - return -1 - } - let decodedFlags = DecodedOpenFlags(oflag) var saAttrs = SECURITY_ATTRIBUTES( @@ -40,7 +46,7 @@ internal func open( nil) if hFile == INVALID_HANDLE_VALUE { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return -1 } @@ -52,8 +58,10 @@ internal func open( _ path: UnsafePointer, _ oflag: Int32, _ mode: CInterop.Mode ) -> CInt { - guard let pSD = createSecurityDescriptor(from: mode) else { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + let actualMode = mode & ~_umask + + guard let pSD = _createSecurityDescriptor(from: actualMode, for: .file) else { + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return -1 } @@ -80,7 +88,7 @@ internal func open( nil) if hFile == INVALID_HANDLE_VALUE { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return -1 } @@ -146,7 +154,7 @@ internal func pread( var nNumberOfBytesRead: DWORD = 0 if !ReadFile(hFile, buf, DWORD(nbyte), &nNumberOfBytesRead, &ovlOverlapped) { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return Int(-1) } return Int(nNumberOfBytesRead) @@ -169,7 +177,7 @@ internal func pwrite( var nNumberOfBytesWritten: DWORD = 0 if !WriteFile(hFile, buf, DWORD(nbyte), &nNumberOfBytesWritten, &ovlOverlapped) { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return Int(-1) } return Int(nNumberOfBytesWritten) @@ -193,7 +201,7 @@ internal func ftruncate(_ fd: Int32, _ length: off_t) -> Int32 { // Save the current position and restore it when we're done if !SetFilePointerEx(hFile, liCurrentOffset, &liCurrentOffset, DWORD(FILE_CURRENT)) { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return -1 } defer { @@ -203,7 +211,7 @@ internal func ftruncate(_ fd: Int32, _ length: off_t) -> Int32 { // Truncate (or extend) the file if !SetFilePointerEx(hFile, liDesiredLength, nil, DWORD(FILE_BEGIN)) || !SetEndOfFile(hFile) { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return -1 } @@ -215,8 +223,11 @@ internal func mkdir( _ path: UnsafePointer, _ mode: CInterop.Mode ) -> CInt { - guard let pSD = createSecurityDescriptor(from: mode) else { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + let actualMode = mode & ~_umask + + guard let pSD = _createSecurityDescriptor(from: actualMode, + for: .directory) else { + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return -1 } defer { @@ -230,7 +241,7 @@ internal func mkdir( ) if !CreateDirectoryW(path, &saAttrs) { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return -1 } @@ -242,7 +253,7 @@ internal func rmdir( _ path: UnsafePointer ) -> CInt { if !RemoveDirectoryW(path) { - ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + ucrt._set_errno(_mapWindowsErrorToErrno(GetLastError())) return -1 } @@ -250,7 +261,7 @@ internal func rmdir( } @usableFromInline -internal func mapWindowsErrorToErrno(_ errorCode: DWORD) -> CInt { +internal func _mapWindowsErrorToErrno(_ errorCode: DWORD) -> CInt { switch Int32(errorCode) { case ERROR_SUCCESS: return 0 @@ -323,7 +334,9 @@ internal func mapWindowsErrorToErrno(_ errorCode: DWORD) -> CInt { } fileprivate func rightsFromModeBits( - _ bits: Int, sticky: Bool = false + _ bits: Int, + sticky: Bool = false, + for fileOrDirectory: _FileOrDirectory ) -> DWORD { var rights: DWORD = 0 @@ -341,7 +354,7 @@ fileprivate func rightsFromModeBits( | FILE_WRITE_EA | STANDARD_RIGHTS_WRITE | SYNCHRONIZE) - if !sticky { + if fileOrDirectory == .directory && !sticky { rights |= DWORD(FILE_DELETE_CHILD) } } @@ -384,19 +397,30 @@ fileprivate func getTokenInformation( return nil } +@usableFromInline +internal enum _FileOrDirectory { + case file + case directory +} + /// Build a SECURITY_DESCRIPTOR from UNIX-style "mode" bits. This only /// takes account of the rwx and sticky bits; there's really nothing that /// we can do about setuid/setgid. @usableFromInline -internal func createSecurityDescriptor(from mode: CInterop.Mode) +internal func _createSecurityDescriptor(from mode: CInterop.Mode, + for fileOrDirectory: _FileOrDirectory) -> PSECURITY_DESCRIPTOR? { let ownerPerm = (Int(mode) >> 6) & 0o7 let groupPerm = (Int(mode) >> 3) & 0o7 let otherPerm = Int(mode) & 0o7 - let ownerRights = rightsFromModeBits(ownerPerm) - let groupRights = rightsFromModeBits(groupPerm, sticky: (mode & 0o1000) != 0) - let otherRights = rightsFromModeBits(otherPerm, sticky: (mode & 0o1000) != 0) + let ownerRights = rightsFromModeBits(ownerPerm, for: fileOrDirectory) + let groupRights = rightsFromModeBits(groupPerm, + sticky: (mode & 0o1000) != 0, + for: fileOrDirectory) + let otherRights = rightsFromModeBits(otherPerm, + sticky: (mode & 0o1000) != 0, + for: fileOrDirectory) // If group or other permissions are *more* permissive, then we need // some DENY ACEs as well to implement the expected semantics diff --git a/Tests/SystemTests/FileOperationsTest.swift b/Tests/SystemTests/FileOperationsTest.swift index ee82128..be5a67d 100644 --- a/Tests/SystemTests/FileOperationsTest.swift +++ b/Tests/SystemTests/FileOperationsTest.swift @@ -137,7 +137,7 @@ final class FileOperationsTest: XCTestCase { // Ad-hoc test touching a file system. do { // TODO: Test this against a virtual in-memory file system - try withTemporaryPath(basename: "testAdhocOpen") { path in + try withTemporaryFilePath(basename: "testAdhocOpen") { path in let fd = try FileDescriptor.open(path.appending("b.txt"), .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite) try fd.closeAfter { try fd.writeAll("abc".utf8) @@ -186,7 +186,7 @@ final class FileOperationsTest: XCTestCase { } func testResizeFile() throws { - try withTemporaryPath(basename: "testResizeFile") { path in + try withTemporaryFilePath(basename: "testResizeFile") { path in let fd = try FileDescriptor.open(path.appending("\(UUID().uuidString).txt"), .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite) try fd.closeAfter { // File should be empty initially. diff --git a/Tests/SystemTests/FileOperationsTestWindows.swift b/Tests/SystemTests/FileOperationsTestWindows.swift new file mode 100644 index 0000000..35df731 --- /dev/null +++ b/Tests/SystemTests/FileOperationsTestWindows.swift @@ -0,0 +1,241 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2024 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information +*/ + +import XCTest + +#if os(Windows) + +#if SYSTEM_PACKAGE +import SystemPackage +#else +import System +#endif + +import WinSDK + +@available(iOS 8, *) +final class FileOperationsTestWindows: XCTestCase { + private let r = ACCESS_MASK( + FILE_READ_ATTRIBUTES + | FILE_READ_DATA + | FILE_READ_EA + | STANDARD_RIGHTS_READ + | SYNCHRONIZE + ) + private let w = ACCESS_MASK( + FILE_APPEND_DATA + | FILE_WRITE_ATTRIBUTES + | FILE_WRITE_DATA + | FILE_WRITE_EA + | STANDARD_RIGHTS_WRITE + | SYNCHRONIZE + ) + private let x = ACCESS_MASK( + FILE_EXECUTE + | FILE_READ_ATTRIBUTES + | STANDARD_RIGHTS_EXECUTE + | SYNCHRONIZE + ) + + private struct Test { + var permissions: CModeT + var ownerAccess: ACCESS_MASK + var groupAccess: ACCESS_MASK + var otherAccess: ACCESS_MASK + + init(_ permissions: CModeT, + _ ownerAccess: ACCESS_MASK, + _ groupAccess: ACCESS_MASK, + _ otherAccess: ACCESS_MASK) { + self.permissions = permissions + self.ownerAccess = ownerAccess + self.groupAccess = groupAccess + self.otherAccess = otherAccess + } + } + + /// Retrieve the owner, group and other access masks for a given file. + /// + /// - Parameters: + /// - path: The path to the file to inspect + /// - Returns: A tuple of ACCESS_MASK values. + func getAccessMasks( + path: FilePath + ) -> (ACCESS_MASK, ACCESS_MASK, ACCESS_MASK) { + var SIDAuthWorld = SID_IDENTIFIER_AUTHORITY(Value: (0, 0, 0, 0, 0, 1)) + var psidEveryone: PSID? = nil + + XCTAssert(AllocateAndInitializeSid(&SIDAuthWorld, 1, + DWORD(SECURITY_WORLD_RID), + 0, 0, 0, 0, 0, 0, 0, + &psidEveryone)) + defer { + FreeSid(psidEveryone) + } + + var everyone = TRUSTEE_W( + pMultipleTrustee: nil, + MultipleTrusteeOperation: NO_MULTIPLE_TRUSTEE, + TrusteeForm: TRUSTEE_IS_SID, + TrusteeType: TRUSTEE_IS_GROUP, + ptstrName: + psidEveryone!.assumingMemoryBound(to: CInterop.PlatformChar.self) + ) + + return path.withPlatformString { objectName in + var psidOwner: PSID? = nil + var psidGroup: PSID? = nil + var pDacl: PACL? = nil + var pSD: PSECURITY_DESCRIPTOR? = nil + + XCTAssertEqual(GetNamedSecurityInfoW( + objectName, + SE_FILE_OBJECT, + SECURITY_INFORMATION( + DACL_SECURITY_INFORMATION + | GROUP_SECURITY_INFORMATION + | OWNER_SECURITY_INFORMATION + ), + &psidOwner, + &psidGroup, + &pDacl, + nil, + &pSD), DWORD(ERROR_SUCCESS)) + defer { + LocalFree(pSD) + } + + var owner = TRUSTEE_W( + pMultipleTrustee: nil, + MultipleTrusteeOperation: NO_MULTIPLE_TRUSTEE, + TrusteeForm: TRUSTEE_IS_SID, + TrusteeType: TRUSTEE_IS_USER, + ptstrName: + psidOwner!.assumingMemoryBound(to: CInterop.PlatformChar.self) + ) + var group = TRUSTEE_W( + pMultipleTrustee: nil, + MultipleTrusteeOperation: NO_MULTIPLE_TRUSTEE, + TrusteeForm: TRUSTEE_IS_SID, + TrusteeType: TRUSTEE_IS_GROUP, + ptstrName: + psidGroup!.assumingMemoryBound(to: CInterop.PlatformChar.self) + ) + + var ownerAccess = ACCESS_MASK(0) + var groupAccess = ACCESS_MASK(0) + var otherAccess = ACCESS_MASK(0) + + XCTAssertEqual(GetEffectiveRightsFromAclW( + pDacl, + &owner, + &ownerAccess), DWORD(ERROR_SUCCESS)) + XCTAssertEqual(GetEffectiveRightsFromAclW( + pDacl, + &group, + &groupAccess), DWORD(ERROR_SUCCESS)) + XCTAssertEqual(GetEffectiveRightsFromAclW( + pDacl, + &everyone, + &otherAccess), DWORD(ERROR_SUCCESS)) + + return (ownerAccess, groupAccess, otherAccess) + } + } + + private func runTests(_ tests: [Test], at path: FilePath) throws { + for test in tests { + let octal = String(test.permissions, radix: 8) + let testPath = path.appending("test-\(octal).txt") + let fd = try FileDescriptor.open( + testPath, + .readWrite, + options: [.create, .truncate], + permissions: FilePermissions(rawValue: test.permissions) + ) + _ = try fd.closeAfter { + try fd.writeAll("Hello World".utf8) + } + + let (ownerAccess, groupAccess, otherAccess) + = getAccessMasks(path: testPath) + + XCTAssertEqual(ownerAccess, test.ownerAccess) + XCTAssertEqual(groupAccess, test.groupAccess) + XCTAssertEqual(otherAccess, test.otherAccess) + } + } + + /// Test that the umask works properly + func testUmask() throws { + // Default mask should be 0o022 + XCTAssertEqual(FilePermissions.creationMask, [.groupWrite, .otherWrite]) + + try withTemporaryFilePath(basename: "testUmask") { path in + let tests = [ + Test(0o000, 0, 0, 0), + Test(0o700, r|w|x, 0, 0), + Test(0o770, r|w|x, r|x, 0), + Test(0o777, r|w|x, r|x, r|x) + ] + + try runTests(tests, at: path) + } + + try FilePermissions.withCreationMask([.groupWrite, .groupExecute, + .otherWrite, .otherExecute]) { + try withTemporaryFilePath(basename: "testUmask") { path in + let tests = [ + Test(0o000, 0, 0, 0), + Test(0o700, r|w|x, 0, 0), + Test(0o770, r|w|x, r, 0), + Test(0o777, r|w|x, r, r) + ] + + try runTests(tests, at: path) + } + } + } + + /// Test that setting permissions on a file works as expected + func testPermissions() throws { + try FilePermissions.withCreationMask([]) { + try withTemporaryFilePath(basename: "testPermissions") { path in + let tests = [ + Test(0o000, 0, 0, 0), + + Test(0o400, r, 0, 0), + Test(0o200, w, 0, 0), + Test(0o100, x, 0, 0), + Test(0o040, 0, r, 0), + Test(0o020, 0, w, 0), + Test(0o010, 0, x, 0), + Test(0o004, 0, 0, r), + Test(0o002, 0, 0, w), + Test(0o001, 0, 0, x), + + Test(0o700, r|w|x, 0, 0), + Test(0o770, r|w|x, r|w|x, 0), + Test(0o777, r|w|x, r|w|x, r|w|x), + + Test(0o755, r|w|x, r|x, r|x), + Test(0o644, r|w, r, r), + + Test(0o007, 0, 0, r|w|x), + Test(0o070, 0, r|w|x, 0), + Test(0o077, 0, r|w|x, r|w|x), + ] + + try runTests(tests, at: path) + } + } + } +} + +#endif // os(Windows) diff --git a/Tests/SystemTests/FilePathTests/FilePathTempTest.swift b/Tests/SystemTests/FilePathTests/FilePathTempTest.swift index b2b1fa2..dc8597b 100644 --- a/Tests/SystemTests/FilePathTests/FilePathTempTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathTempTest.swift @@ -18,7 +18,7 @@ import System final class TemporaryPathTest: XCTestCase { #if SYSTEM_PACKAGE_DARWIN func testNotInSlashTmp() throws { - try withTemporaryPath(basename: "NotInSlashTmp") { path in + try withTemporaryFilePath(basename: "NotInSlashTmp") { path in // We shouldn't be using "/tmp" on Darwin XCTAssertNotEqual(path.components.first!, "tmp") } @@ -26,10 +26,10 @@ final class TemporaryPathTest: XCTestCase { #endif func testUnique() throws { - try withTemporaryPath(basename: "test") { path in + try withTemporaryFilePath(basename: "test") { path in let strPath = String(decoding: path) XCTAssert(strPath.contains("test")) - try withTemporaryPath(basename: "test") { path2 in + try withTemporaryFilePath(basename: "test") { path2 in let strPath2 = String(decoding: path2) XCTAssertNotEqual(strPath, strPath2) } @@ -39,7 +39,7 @@ final class TemporaryPathTest: XCTestCase { func testCleanup() throws { var thePath: FilePath? = nil - try withTemporaryPath(basename: "test") { path in + try withTemporaryFilePath(basename: "test") { path in thePath = path.appending("foo.txt") let fd = try FileDescriptor.open(thePath!, .readWrite, options: [.create, .truncate], From cf7530848ef64c9b9dae808d90415ad2f8bca77d Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Mon, 8 Apr 2024 17:01:42 +0100 Subject: [PATCH 17/38] [Linux] Glibc's DIR doesn't get imported into Swift. Swift ends up with OpaquePointer instead of a typed pointer. rdar://125087707 --- Sources/System/FilePath/FilePathTempPosix.swift | 2 +- Sources/System/Internals/Syscalls.swift | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Sources/System/FilePath/FilePathTempPosix.swift b/Sources/System/FilePath/FilePathTempPosix.swift index 623ccae..e8713c2 100644 --- a/Sources/System/FilePath/FilePathTempPosix.swift +++ b/Sources/System/FilePath/FilePathTempPosix.swift @@ -60,7 +60,7 @@ internal func _recursiveRemove( fileprivate func impl_opendirat( _ dirfd: CInt, _ name: UnsafePointer -) -> UnsafeMutablePointer? { +) -> system_DIRPtr? { let fd = system_openat(dirfd, name, FileDescriptor.AccessMode.readOnly.rawValue | FileDescriptor.OpenOptions.directory.rawValue) diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index 1c6ed72..3794d6e 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -189,7 +189,11 @@ internal func system_confstr( internal let SYSTEM_AT_REMOVE_DIR = AT_REMOVEDIR internal let SYSTEM_DT_DIR = DT_DIR internal typealias system_dirent = dirent -internal typealias system_DIR = DIR +#if os(Linux) +internal typealias system_DIRPtr = OpaquePointer +#else +internal typealias system_DIRPtr = UnsafeMutablePointer +#endif internal func system_unlinkat( _ fd: CInt, @@ -204,24 +208,24 @@ return unlinkat(fd, path, flag) internal func system_fdopendir( _ fd: CInt -) -> UnsafeMutablePointer? { +) -> system_DIRPtr? { return fdopendir(fd) } internal func system_readdir( - _ dir: UnsafeMutablePointer + _ dir: system_DIRPtr ) -> UnsafeMutablePointer? { return readdir(dir) } internal func system_rewinddir( - _ dir: UnsafeMutablePointer + _ dir: system_DIRPtr ) { return rewinddir(dir) } internal func system_closedir( - _ dir: UnsafeMutablePointer + _ dir: system_DIRPtr ) -> CInt { return closedir(dir) } From f1d7eb52c6b2073a7ee0264a65c49b32d406f5e3 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Mon, 13 May 2024 11:19:43 +0100 Subject: [PATCH 18/38] Remove accidentally public functions. These can't be new API as they haven't been reviewed. --- Sources/System/ErrnoWindows.swift | 2 +- Sources/System/FileOperations.swift | 4 ++-- Sources/System/FilePath/FilePathTemp.swift | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/System/ErrnoWindows.swift b/Sources/System/ErrnoWindows.swift index b94b577..cc481cb 100644 --- a/Sources/System/ErrnoWindows.swift +++ b/Sources/System/ErrnoWindows.swift @@ -12,7 +12,7 @@ import WinSDK extension Errno { - public init(windowsError: DWORD) { + internal init(windowsError: DWORD) { self.init(rawValue: _mapWindowsErrorToErrno(windowsError)) } } diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index ba70e87..3412834 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -516,7 +516,7 @@ extension FilePermissions { /// files or directories. Note that this mask is process-wide, and that /// *getting* it is not thread safe. @_alwaysEmitIntoClient - public static var creationMask: FilePermissions { + internal static var creationMask: FilePermissions { get { let oldMask = _umask(0o22) _ = _umask(oldMask) @@ -536,7 +536,7 @@ extension FilePermissions { /// This is more efficient than reading `creationMask` and restoring it /// afterwards, because of the way reading the creation mask works. @_alwaysEmitIntoClient - public static func withCreationMask( + internal static func withCreationMask( _ permissions: FilePermissions, body: () throws -> R ) rethrows -> R { diff --git a/Sources/System/FilePath/FilePathTemp.swift b/Sources/System/FilePath/FilePathTemp.swift index 7b41d0b..c0edf95 100644 --- a/Sources/System/FilePath/FilePathTemp.swift +++ b/Sources/System/FilePath/FilePathTemp.swift @@ -18,7 +18,7 @@ /// Creates a temporary directory with a name based on the given `basename`, /// executes `body`, passing in the path of the created directory, then /// deletes the directory and all of its contents before returning. -public func withTemporaryFilePath( +internal func withTemporaryFilePath( basename: FilePath.Component, _ body: (FilePath) throws -> R ) throws -> R { From 53471ce33bf15d7981092bdfb35bd75f03efaf03 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Mon, 13 May 2024 14:18:17 +0100 Subject: [PATCH 19/38] [Windows] Fix a problem with opening files read only. `_O_RDONLY` is zero, so we can't test for it by AND-ing. --- Sources/System/Internals/WindowsSyscallAdapters.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index 3e3f9c6..1e35ee9 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -621,14 +621,15 @@ fileprivate struct DecodedOpenFlags { } dwDesiredAccess = 0 - if (oflag & _O_RDONLY) != 0 { + switch (oflag & (_O_RDONLY|_O_WRONLY|_O_RDWR)) { + case _O_RDONLY: dwDesiredAccess |= DWORD(GENERIC_READ) - } - if (oflag & _O_WRONLY) != 0 { + case _O_WRONLY: dwDesiredAccess |= DWORD(GENERIC_WRITE) - } - if (oflag & _O_RDWR) != 0 { + case _O_RDWR: dwDesiredAccess |= DWORD(GENERIC_READ) | DWORD(GENERIC_WRITE) + default: + break } bInheritHandle = WindowsBool((oflag & _O_NOINHERIT) == 0) From 3a4e2f8071478c2542833da3ba692ca3717f96a8 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Mon, 13 May 2024 14:19:33 +0100 Subject: [PATCH 20/38] [Windows] Fix tests. We need to use `@testable` when importing `SystemPackage` in order to let us test the `internal` functions. Also, use `NUL` instead of `/dev/null`, and generate random bytes in a file instead of reading from `/dev/random`. --- Tests/SystemTests/FileOperationsTest.swift | 58 +++++++++++++++---- .../FileOperationsTestWindows.swift | 4 +- .../FilePathTests/FilePathTempTest.swift | 4 +- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/Tests/SystemTests/FileOperationsTest.swift b/Tests/SystemTests/FileOperationsTest.swift index be5a67d..ed05dcf 100644 --- a/Tests/SystemTests/FileOperationsTest.swift +++ b/Tests/SystemTests/FileOperationsTest.swift @@ -10,9 +10,9 @@ import XCTest #if SYSTEM_PACKAGE -import SystemPackage +@testable import SystemPackage #else -import System +@testable import System #endif @available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *) @@ -87,7 +87,11 @@ final class FileOperationsTest: XCTestCase { } func testWriteFromEmptyBuffer() throws { + #if os(Windows) + let fd = try FileDescriptor.open(FilePath("NUL"), .writeOnly) + #else let fd = try FileDescriptor.open(FilePath("/dev/null"), .writeOnly) + #endif let written1 = try fd.write(toAbsoluteOffset: 0, .init(start: nil, count: 0)) XCTAssertEqual(written1, 0) @@ -98,16 +102,48 @@ final class FileOperationsTest: XCTestCase { XCTAssertEqual(written2, 0) } - func testReadToEmptyBuffer() throws { - let fd = try FileDescriptor.open(FilePath("/dev/random"), .readOnly) - let read1 = try fd.read(fromAbsoluteOffset: 0, into: .init(start: nil, count: 0)) - XCTAssertEqual(read1, 0) + #if os(Windows) + // Generate a file containing random bytes; this should not be used + // for cryptography, it's just for testing. + func generateRandomData(at path: FilePath, count: Int) throws { + let fd = try FileDescriptor.open(path, .readWrite, + options: [.create, .truncate]) + defer { + try! fd.close() + } + let data = [UInt8]( + sequence(first: 0, + next: { + _ in UInt8.random(in: UInt8.min...UInt8.max) + }).dropFirst().prefix(count) + ) - let pointer = UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 8) - defer { pointer.deallocate() } - let empty = UnsafeMutableRawBufferPointer(start: pointer, count: 0) - let read2 = try fd.read(fromAbsoluteOffset: 0, into: empty) - XCTAssertEqual(read2, 0) + try data.withUnsafeBytes { + _ = try fd.write($0) + } + } + #endif + + func testReadToEmptyBuffer() throws { + try withTemporaryFilePath(basename: "testReadToEmptyBuffer") { path in + #if os(Windows) + // Windows doesn't have an equivalent to /dev/random, so generate + // some random bytes and write them to a file for the next step. + let randomPath = path.appending("random.txt") + try generateRandomData(at: randomPath, count: 16) + let fd = try FileDescriptor.open(randomPath, .readOnly) + #else // !os(Windows) + let fd = try FileDescriptor.open(FilePath("/dev/random"), .readOnly) + #endif + let read1 = try fd.read(fromAbsoluteOffset: 0, into: .init(start: nil, count: 0)) + XCTAssertEqual(read1, 0) + + let pointer = UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 8) + defer { pointer.deallocate() } + let empty = UnsafeMutableRawBufferPointer(start: pointer, count: 0) + let read2 = try fd.read(fromAbsoluteOffset: 0, into: empty) + XCTAssertEqual(read2, 0) + } } func testHelpers() { diff --git a/Tests/SystemTests/FileOperationsTestWindows.swift b/Tests/SystemTests/FileOperationsTestWindows.swift index 35df731..8203051 100644 --- a/Tests/SystemTests/FileOperationsTestWindows.swift +++ b/Tests/SystemTests/FileOperationsTestWindows.swift @@ -12,9 +12,9 @@ import XCTest #if os(Windows) #if SYSTEM_PACKAGE -import SystemPackage +@testable import SystemPackage #else -import System +@testable import System #endif import WinSDK diff --git a/Tests/SystemTests/FilePathTests/FilePathTempTest.swift b/Tests/SystemTests/FilePathTests/FilePathTempTest.swift index dc8597b..30d5826 100644 --- a/Tests/SystemTests/FilePathTests/FilePathTempTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathTempTest.swift @@ -10,9 +10,9 @@ import XCTest #if SYSTEM_PACKAGE -import SystemPackage +@testable import SystemPackage #else -import System +@testable import System #endif final class TemporaryPathTest: XCTestCase { From 6c64372b85f4b46af3e5b9e3519860cc0ab534dc Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Tue, 14 May 2024 14:42:57 +0100 Subject: [PATCH 21/38] [Windows] Add a comment to explain the O_RDONLY/O_WRONLY/O_RDWR logic. The values of these flags on Windows do not overlap, and more particularly, `O_RDONLY` is actually zero on Windows, which means we cannot test for it by AND-ing. --- Sources/System/Internals/WindowsSyscallAdapters.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index 1e35ee9..048377b 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -620,6 +620,9 @@ fileprivate struct DecodedOpenFlags { dwCreationDisposition = DWORD(OPEN_EXISTING) } + // The _O_RDONLY, _O_WRONLY and _O_RDWR flags are non-overlapping + // on Windows; in particular, _O_RDONLY is zero, which means we can't + // test for it by AND-ing. dwDesiredAccess = 0 switch (oflag & (_O_RDONLY|_O_WRONLY|_O_RDWR)) { case _O_RDONLY: From 9eb1f34cbfe6dc99c894f6cd3550fe6aa512666b Mon Sep 17 00:00:00 2001 From: George Barnett Date: Mon, 19 Aug 2024 10:18:30 +0100 Subject: [PATCH 22/38] Add support for macCatalyst Building swift-system for macCatalyst fails as there is no import of the Darwin module. # Conflicts: # Package.swift --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 411a295..b71ade6 100644 --- a/Package.swift +++ b/Package.swift @@ -19,7 +19,7 @@ let cSettings: [CSetting] = [ let swiftSettings: [SwiftSetting] = [ .define( "SYSTEM_PACKAGE_DARWIN", - .when(platforms: [.macOS, .iOS, .watchOS, .tvOS, .visionOS])), + .when(platforms: [.macOS, .macCatalyst, .iOS, .watchOS, .tvOS, .visionOS])), .define("SYSTEM_PACKAGE"), .define("ENABLE_MOCKING", .when(configuration: .debug)), ] From b9e4d2b2baca22f537da2004532b2586d207d1f6 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Wed, 16 Oct 2024 16:25:48 -0700 Subject: [PATCH 23/38] Remove more accidentally public functions --- Sources/System/FileOperations.swift | 3 --- Sources/System/Internals/WindowsSyscallAdapters.swift | 3 --- 2 files changed, 6 deletions(-) diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index 3412834..fc9fc93 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -515,7 +515,6 @@ extension FilePermissions { /// Permissions set in this mask will be cleared by functions that create /// files or directories. Note that this mask is process-wide, and that /// *getting* it is not thread safe. - @_alwaysEmitIntoClient internal static var creationMask: FilePermissions { get { let oldMask = _umask(0o22) @@ -535,7 +534,6 @@ extension FilePermissions { /// /// This is more efficient than reading `creationMask` and restoring it /// afterwards, because of the way reading the creation mask works. - @_alwaysEmitIntoClient internal static func withCreationMask( _ permissions: FilePermissions, body: () throws -> R @@ -547,7 +545,6 @@ extension FilePermissions { return try body() } - @usableFromInline internal static func _umask(_ mode: CModeT) -> CModeT { return system_umask(mode) } diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index 048377b..d470bd1 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -260,7 +260,6 @@ internal func rmdir( return 0; } -@usableFromInline internal func _mapWindowsErrorToErrno(_ errorCode: DWORD) -> CInt { switch Int32(errorCode) { case ERROR_SUCCESS: @@ -397,7 +396,6 @@ fileprivate func getTokenInformation( return nil } -@usableFromInline internal enum _FileOrDirectory { case file case directory @@ -406,7 +404,6 @@ internal enum _FileOrDirectory { /// Build a SECURITY_DESCRIPTOR from UNIX-style "mode" bits. This only /// takes account of the rwx and sticky bits; there's really nothing that /// we can do about setuid/setgid. -@usableFromInline internal func _createSecurityDescriptor(from mode: CInterop.Mode, for fileOrDirectory: _FileOrDirectory) -> PSECURITY_DESCRIPTOR? { From e7ffa45882f8bdc7e21b94448fa0f225e0de883d Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Wed, 31 Jul 2024 14:56:04 -0700 Subject: [PATCH 24/38] Remove private-and-@_aEIC initializers These were just passthroughs. --- Sources/System/Errno.swift | 221 +++++++++++++-------------- Sources/System/FileDescriptor.swift | 27 ++-- Sources/System/FilePermissions.swift | 51 +++---- 3 files changed, 145 insertions(+), 154 deletions(-) diff --git a/Sources/System/Errno.swift b/Sources/System/Errno.swift index 7f0aabe..498ccbc 100644 --- a/Sources/System/Errno.swift +++ b/Sources/System/Errno.swift @@ -20,13 +20,10 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient public init(rawValue: CInt) { self.rawValue = rawValue } - @_alwaysEmitIntoClient - private init(_ raw: CInt) { self.init(rawValue: raw) } - #if SYSTEM_PACKAGE_DARWIN /// Error. Not used. @_alwaysEmitIntoClient - public static var notUsed: Errno { Errno(_ERRNO_NOT_USED) } + public static var notUsed: Errno { .init(rawValue: _ERRNO_NOT_USED) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notUsed") @@ -41,7 +38,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPERM`. @_alwaysEmitIntoClient - public static var notPermitted: Errno { Errno(_EPERM) } + public static var notPermitted: Errno { .init(rawValue: _EPERM) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notPermitted") @@ -54,7 +51,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOENT`. @_alwaysEmitIntoClient - public static var noSuchFileOrDirectory: Errno { Errno(_ENOENT) } + public static var noSuchFileOrDirectory: Errno { .init(rawValue: _ENOENT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noSuchFileOrDirectory") @@ -66,7 +63,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ESRCH`. @_alwaysEmitIntoClient - public static var noSuchProcess: Errno { Errno(_ESRCH) } + public static var noSuchProcess: Errno { .init(rawValue: _ESRCH) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noSuchProcess") @@ -81,7 +78,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EINTR`. @_alwaysEmitIntoClient - public static var interrupted: Errno { Errno(_EINTR) } + public static var interrupted: Errno { .init(rawValue: _EINTR) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "interrupted") @@ -96,7 +93,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EIO`. @_alwaysEmitIntoClient - public static var ioError: Errno { Errno(_EIO) } + public static var ioError: Errno { .init(rawValue: _EIO) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "ioError") @@ -111,7 +108,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENXIO`. @_alwaysEmitIntoClient - public static var noSuchAddressOrDevice: Errno { Errno(_ENXIO) } + public static var noSuchAddressOrDevice: Errno { .init(rawValue: _ENXIO) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noSuchAddressOrDevice") @@ -125,7 +122,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `E2BIG`. @_alwaysEmitIntoClient - public static var argListTooLong: Errno { Errno(_E2BIG) } + public static var argListTooLong: Errno { .init(rawValue: _E2BIG) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "argListTooLong") @@ -139,7 +136,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOEXEC`. @_alwaysEmitIntoClient - public static var execFormatError: Errno { Errno(_ENOEXEC) } + public static var execFormatError: Errno { .init(rawValue: _ENOEXEC) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "execFormatError") @@ -154,7 +151,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EBADF`. @_alwaysEmitIntoClient - public static var badFileDescriptor: Errno { Errno(_EBADF) } + public static var badFileDescriptor: Errno { .init(rawValue: _EBADF) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "badFileDescriptor") @@ -168,7 +165,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ECHILD`. @_alwaysEmitIntoClient - public static var noChildProcess: Errno { Errno(_ECHILD) } + public static var noChildProcess: Errno { .init(rawValue: _ECHILD) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noChildProcess") @@ -181,7 +178,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EDEADLK`. @_alwaysEmitIntoClient - public static var deadlock: Errno { Errno(_EDEADLK) } + public static var deadlock: Errno { .init(rawValue: _EDEADLK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "deadlock") @@ -198,7 +195,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOMEM`. @_alwaysEmitIntoClient - public static var noMemory: Errno { Errno(_ENOMEM) } + public static var noMemory: Errno { .init(rawValue: _ENOMEM) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noMemory") @@ -211,7 +208,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EACCES`. @_alwaysEmitIntoClient - public static var permissionDenied: Errno { Errno(_EACCES) } + public static var permissionDenied: Errno { .init(rawValue: _EACCES) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "permissionDenied") @@ -223,7 +220,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EFAULT`. @_alwaysEmitIntoClient - public static var badAddress: Errno { Errno(_EFAULT) } + public static var badAddress: Errno { .init(rawValue: _EFAULT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "badAddress") @@ -236,7 +233,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOTBLK`. @_alwaysEmitIntoClient - public static var notBlockDevice: Errno { Errno(_ENOTBLK) } + public static var notBlockDevice: Errno { .init(rawValue: _ENOTBLK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notBlockDevice") @@ -250,7 +247,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EBUSY`. @_alwaysEmitIntoClient - public static var resourceBusy: Errno { Errno(_EBUSY) } + public static var resourceBusy: Errno { .init(rawValue: _EBUSY) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "resourceBusy") @@ -263,7 +260,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EEXIST`. @_alwaysEmitIntoClient - public static var fileExists: Errno { Errno(_EEXIST) } + public static var fileExists: Errno { .init(rawValue: _EEXIST) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "fileExists") @@ -275,7 +272,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EXDEV`. @_alwaysEmitIntoClient - public static var improperLink: Errno { Errno(_EXDEV) } + public static var improperLink: Errno { .init(rawValue: _EXDEV) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "improperLink") @@ -288,7 +285,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENODEV`. @_alwaysEmitIntoClient - public static var operationNotSupportedByDevice: Errno { Errno(_ENODEV) } + public static var operationNotSupportedByDevice: Errno { .init(rawValue: _ENODEV) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "operationNotSupportedByDevice") @@ -302,7 +299,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOTDIR`. @_alwaysEmitIntoClient - public static var notDirectory: Errno { Errno(_ENOTDIR) } + public static var notDirectory: Errno { .init(rawValue: _ENOTDIR) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notDirectory") @@ -315,7 +312,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EISDIR`. @_alwaysEmitIntoClient - public static var isDirectory: Errno { Errno(_EISDIR) } + public static var isDirectory: Errno { .init(rawValue: _EISDIR) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "isDirectory") @@ -328,7 +325,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EINVAL`. @_alwaysEmitIntoClient - public static var invalidArgument: Errno { Errno(_EINVAL) } + public static var invalidArgument: Errno { .init(rawValue: _EINVAL) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "invalidArgument") @@ -343,7 +340,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENFILE`. @_alwaysEmitIntoClient - public static var tooManyOpenFilesInSystem: Errno { Errno(_ENFILE) } + public static var tooManyOpenFilesInSystem: Errno { .init(rawValue: _ENFILE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManyOpenFilesInSystem") @@ -356,7 +353,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EMFILE`. @_alwaysEmitIntoClient - public static var tooManyOpenFiles: Errno { Errno(_EMFILE) } + public static var tooManyOpenFiles: Errno { .init(rawValue: _EMFILE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManyOpenFiles") @@ -371,7 +368,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOTTY`. @_alwaysEmitIntoClient - public static var inappropriateIOCTLForDevice: Errno { Errno(_ENOTTY) } + public static var inappropriateIOCTLForDevice: Errno { .init(rawValue: _ENOTTY) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "inappropriateIOCTLForDevice") @@ -386,7 +383,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ETXTBSY`. @_alwaysEmitIntoClient - public static var textFileBusy: Errno { Errno(_ETXTBSY) } + public static var textFileBusy: Errno { .init(rawValue: _ETXTBSY) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "textFileBusy") @@ -401,7 +398,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EFBIG`. @_alwaysEmitIntoClient - public static var fileTooLarge: Errno { Errno(_EFBIG) } + public static var fileTooLarge: Errno { .init(rawValue: _EFBIG) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "fileTooLarge") @@ -418,7 +415,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOSPC`. @_alwaysEmitIntoClient - public static var noSpace: Errno { Errno(_ENOSPC) } + public static var noSpace: Errno { .init(rawValue: _ENOSPC) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noSpace") @@ -430,7 +427,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ESPIPE`. @_alwaysEmitIntoClient - public static var illegalSeek: Errno { Errno(_ESPIPE) } + public static var illegalSeek: Errno { .init(rawValue: _ESPIPE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "illegalSeek") @@ -443,7 +440,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EROFS`. @_alwaysEmitIntoClient - public static var readOnlyFileSystem: Errno { Errno(_EROFS) } + public static var readOnlyFileSystem: Errno { .init(rawValue: _EROFS) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "readOnlyFileSystem") @@ -456,7 +453,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EMLINK`. @_alwaysEmitIntoClient - public static var tooManyLinks: Errno { Errno(_EMLINK) } + public static var tooManyLinks: Errno { .init(rawValue: _EMLINK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManyLinks") @@ -469,7 +466,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPIPE`. @_alwaysEmitIntoClient - public static var brokenPipe: Errno { Errno(_EPIPE) } + public static var brokenPipe: Errno { .init(rawValue: _EPIPE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "brokenPipe") @@ -482,7 +479,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EDOM`. @_alwaysEmitIntoClient - public static var outOfDomain: Errno { Errno(_EDOM) } + public static var outOfDomain: Errno { .init(rawValue: _EDOM) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "outOfDomain") @@ -497,7 +494,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ERANGE`. @_alwaysEmitIntoClient - public static var outOfRange: Errno { Errno(_ERANGE) } + public static var outOfRange: Errno { .init(rawValue: _ERANGE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "outOfRange") @@ -511,7 +508,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EAGAIN`. @_alwaysEmitIntoClient - public static var resourceTemporarilyUnavailable: Errno { Errno(_EAGAIN) } + public static var resourceTemporarilyUnavailable: Errno { .init(rawValue: _EAGAIN) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "resourceTemporarilyUnavailable") @@ -526,7 +523,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EINPROGRESS`. @_alwaysEmitIntoClient - public static var nowInProgress: Errno { Errno(_EINPROGRESS) } + public static var nowInProgress: Errno { .init(rawValue: _EINPROGRESS) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "nowInProgress") @@ -539,7 +536,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EALREADY`. @_alwaysEmitIntoClient - public static var alreadyInProcess: Errno { Errno(_EALREADY) } + public static var alreadyInProcess: Errno { .init(rawValue: _EALREADY) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "alreadyInProcess") @@ -549,7 +546,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOTSOCK`. @_alwaysEmitIntoClient - public static var notSocket: Errno { Errno(_ENOTSOCK) } + public static var notSocket: Errno { .init(rawValue: _ENOTSOCK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notSocket") @@ -561,7 +558,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EDESTADDRREQ`. @_alwaysEmitIntoClient - public static var addressRequired: Errno { Errno(_EDESTADDRREQ) } + public static var addressRequired: Errno { .init(rawValue: _EDESTADDRREQ) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "addressRequired") @@ -574,7 +571,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EMSGSIZE`. @_alwaysEmitIntoClient - public static var messageTooLong: Errno { Errno(_EMSGSIZE) } + public static var messageTooLong: Errno { .init(rawValue: _EMSGSIZE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "messageTooLong") @@ -589,7 +586,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPROTOTYPE`. @_alwaysEmitIntoClient - public static var protocolWrongTypeForSocket: Errno { Errno(_EPROTOTYPE) } + public static var protocolWrongTypeForSocket: Errno { .init(rawValue: _EPROTOTYPE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "protocolWrongTypeForSocket") @@ -602,7 +599,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOPROTOOPT`. @_alwaysEmitIntoClient - public static var protocolNotAvailable: Errno { Errno(_ENOPROTOOPT) } + public static var protocolNotAvailable: Errno { .init(rawValue: _ENOPROTOOPT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "protocolNotAvailable") @@ -615,7 +612,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPROTONOSUPPORT`. @_alwaysEmitIntoClient - public static var protocolNotSupported: Errno { Errno(_EPROTONOSUPPORT) } + public static var protocolNotSupported: Errno { .init(rawValue: _EPROTONOSUPPORT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "protocolNotSupported") @@ -628,7 +625,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ESOCKTNOSUPPORT`. @_alwaysEmitIntoClient - public static var socketTypeNotSupported: Errno { Errno(_ESOCKTNOSUPPORT) } + public static var socketTypeNotSupported: Errno { .init(rawValue: _ESOCKTNOSUPPORT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "socketTypeNotSupported") @@ -641,7 +638,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOTSUP`. @_alwaysEmitIntoClient - public static var notSupported: Errno { Errno(_ENOTSUP) } + public static var notSupported: Errno { .init(rawValue: _ENOTSUP) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notSupported") @@ -654,7 +651,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPFNOSUPPORT`. @_alwaysEmitIntoClient - public static var protocolFamilyNotSupported: Errno { Errno(_EPFNOSUPPORT) } + public static var protocolFamilyNotSupported: Errno { .init(rawValue: _EPFNOSUPPORT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "protocolFamilyNotSupported") @@ -668,7 +665,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EAFNOSUPPORT`. @_alwaysEmitIntoClient - public static var addressFamilyNotSupported: Errno { Errno(_EAFNOSUPPORT) } + public static var addressFamilyNotSupported: Errno { .init(rawValue: _EAFNOSUPPORT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "addressFamilyNotSupported") @@ -680,7 +677,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EADDRINUSE`. @_alwaysEmitIntoClient - public static var addressInUse: Errno { Errno(_EADDRINUSE) } + public static var addressInUse: Errno { .init(rawValue: _EADDRINUSE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "addressInUse") @@ -693,7 +690,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EADDRNOTAVAIL`. @_alwaysEmitIntoClient - public static var addressNotAvailable: Errno { Errno(_EADDRNOTAVAIL) } + public static var addressNotAvailable: Errno { .init(rawValue: _EADDRNOTAVAIL) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "addressNotAvailable") @@ -705,7 +702,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENETDOWN`. @_alwaysEmitIntoClient - public static var networkDown: Errno { Errno(_ENETDOWN) } + public static var networkDown: Errno { .init(rawValue: _ENETDOWN) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "networkDown") @@ -717,7 +714,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENETUNREACH`. @_alwaysEmitIntoClient - public static var networkUnreachable: Errno { Errno(_ENETUNREACH) } + public static var networkUnreachable: Errno { .init(rawValue: _ENETUNREACH) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "networkUnreachable") @@ -729,7 +726,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENETRESET`. @_alwaysEmitIntoClient - public static var networkReset: Errno { Errno(_ENETRESET) } + public static var networkReset: Errno { .init(rawValue: _ENETRESET) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "networkReset") @@ -741,7 +738,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ECONNABORTED`. @_alwaysEmitIntoClient - public static var connectionAbort: Errno { Errno(_ECONNABORTED) } + public static var connectionAbort: Errno { .init(rawValue: _ECONNABORTED) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "connectionAbort") @@ -755,7 +752,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ECONNRESET`. @_alwaysEmitIntoClient - public static var connectionReset: Errno { Errno(_ECONNRESET) } + public static var connectionReset: Errno { .init(rawValue: _ECONNRESET) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "connectionReset") @@ -769,7 +766,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOBUFS`. @_alwaysEmitIntoClient - public static var noBufferSpace: Errno { Errno(_ENOBUFS) } + public static var noBufferSpace: Errno { .init(rawValue: _ENOBUFS) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noBufferSpace") @@ -784,7 +781,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EISCONN`. @_alwaysEmitIntoClient - public static var socketIsConnected: Errno { Errno(_EISCONN) } + public static var socketIsConnected: Errno { .init(rawValue: _EISCONN) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "socketIsConnected") @@ -799,7 +796,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOTCONN`. @_alwaysEmitIntoClient - public static var socketNotConnected: Errno { Errno(_ENOTCONN) } + public static var socketNotConnected: Errno { .init(rawValue: _ENOTCONN) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "socketNotConnected") @@ -813,7 +810,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ESHUTDOWN`. @_alwaysEmitIntoClient - public static var socketShutdown: Errno { Errno(_ESHUTDOWN) } + public static var socketShutdown: Errno { .init(rawValue: _ESHUTDOWN) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "socketShutdown") @@ -828,7 +825,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ETIMEDOUT`. @_alwaysEmitIntoClient - public static var timedOut: Errno { Errno(_ETIMEDOUT) } + public static var timedOut: Errno { .init(rawValue: _ETIMEDOUT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "timedOut") @@ -843,7 +840,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ECONNREFUSED`. @_alwaysEmitIntoClient - public static var connectionRefused: Errno { Errno(_ECONNREFUSED) } + public static var connectionRefused: Errno { .init(rawValue: _ECONNREFUSED) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "connectionRefused") @@ -855,7 +852,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ELOOP`. @_alwaysEmitIntoClient - public static var tooManySymbolicLinkLevels: Errno { Errno(_ELOOP) } + public static var tooManySymbolicLinkLevels: Errno { .init(rawValue: _ELOOP) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManySymbolicLinkLevels") @@ -868,7 +865,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENAMETOOLONG`. @_alwaysEmitIntoClient - public static var fileNameTooLong: Errno { Errno(_ENAMETOOLONG) } + public static var fileNameTooLong: Errno { .init(rawValue: _ENAMETOOLONG) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "fileNameTooLong") @@ -880,7 +877,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EHOSTDOWN`. @_alwaysEmitIntoClient - public static var hostIsDown: Errno { Errno(_EHOSTDOWN) } + public static var hostIsDown: Errno { .init(rawValue: _EHOSTDOWN) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "hostIsDown") @@ -892,7 +889,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EHOSTUNREACH`. @_alwaysEmitIntoClient - public static var noRouteToHost: Errno { Errno(_EHOSTUNREACH) } + public static var noRouteToHost: Errno { .init(rawValue: _EHOSTUNREACH) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noRouteToHost") @@ -905,7 +902,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOTEMPTY`. @_alwaysEmitIntoClient - public static var directoryNotEmpty: Errno { Errno(_ENOTEMPTY) } + public static var directoryNotEmpty: Errno { .init(rawValue: _ENOTEMPTY) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "directoryNotEmpty") @@ -916,7 +913,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPROCLIM`. @_alwaysEmitIntoClient - public static var tooManyProcesses: Errno { Errno(_EPROCLIM) } + public static var tooManyProcesses: Errno { .init(rawValue: _EPROCLIM) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManyProcesses") @@ -929,7 +926,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EUSERS`. @_alwaysEmitIntoClient - public static var tooManyUsers: Errno { Errno(_EUSERS) } + public static var tooManyUsers: Errno { .init(rawValue: _EUSERS) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManyUsers") @@ -946,7 +943,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EDQUOT`. @_alwaysEmitIntoClient - public static var diskQuotaExceeded: Errno { Errno(_EDQUOT) } + public static var diskQuotaExceeded: Errno { .init(rawValue: _EDQUOT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "diskQuotaExceeded") @@ -961,7 +958,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ESTALE`. @_alwaysEmitIntoClient - public static var staleNFSFileHandle: Errno { Errno(_ESTALE) } + public static var staleNFSFileHandle: Errno { .init(rawValue: _ESTALE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "staleNFSFileHandle") @@ -976,7 +973,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EBADRPC`. @_alwaysEmitIntoClient - public static var rpcUnsuccessful: Errno { Errno(_EBADRPC) } + public static var rpcUnsuccessful: Errno { .init(rawValue: _EBADRPC) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "rpcUnsuccessful") @@ -989,7 +986,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ERPCMISMATCH`. @_alwaysEmitIntoClient - public static var rpcVersionMismatch: Errno { Errno(_ERPCMISMATCH) } + public static var rpcVersionMismatch: Errno { .init(rawValue: _ERPCMISMATCH) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "rpcVersionMismatch") @@ -1001,7 +998,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPROGUNAVAIL`. @_alwaysEmitIntoClient - public static var rpcProgramUnavailable: Errno { Errno(_EPROGUNAVAIL) } + public static var rpcProgramUnavailable: Errno { .init(rawValue: _EPROGUNAVAIL) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "rpcProgramUnavailable") @@ -1014,7 +1011,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPROGMISMATCH`. @_alwaysEmitIntoClient - public static var rpcProgramVersionMismatch: Errno { Errno(_EPROGMISMATCH) } + public static var rpcProgramVersionMismatch: Errno { .init(rawValue: _EPROGMISMATCH) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "rpcProgramVersionMismatch") @@ -1027,7 +1024,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPROCUNAVAIL`. @_alwaysEmitIntoClient - public static var rpcProcedureUnavailable: Errno { Errno(_EPROCUNAVAIL) } + public static var rpcProcedureUnavailable: Errno { .init(rawValue: _EPROCUNAVAIL) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "rpcProcedureUnavailable") @@ -1041,7 +1038,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOLCK`. @_alwaysEmitIntoClient - public static var noLocks: Errno { Errno(_ENOLCK) } + public static var noLocks: Errno { .init(rawValue: _ENOLCK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noLocks") @@ -1053,7 +1050,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOSYS`. @_alwaysEmitIntoClient - public static var noFunction: Errno { Errno(_ENOSYS) } + public static var noFunction: Errno { .init(rawValue: _ENOSYS) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noFunction") @@ -1068,7 +1065,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EFTYPE`. @_alwaysEmitIntoClient - public static var badFileTypeOrFormat: Errno { Errno(_EFTYPE) } + public static var badFileTypeOrFormat: Errno { .init(rawValue: _EFTYPE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "badFileTypeOrFormat") @@ -1082,7 +1079,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EAUTH`. @_alwaysEmitIntoClient - public static var authenticationError: Errno { Errno(_EAUTH) } + public static var authenticationError: Errno { .init(rawValue: _EAUTH) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "authenticationError") @@ -1095,7 +1092,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENEEDAUTH`. @_alwaysEmitIntoClient - public static var needAuthenticator: Errno { Errno(_ENEEDAUTH) } + public static var needAuthenticator: Errno { .init(rawValue: _ENEEDAUTH) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "needAuthenticator") @@ -1107,7 +1104,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPWROFF`. @_alwaysEmitIntoClient - public static var devicePowerIsOff: Errno { Errno(_EPWROFF) } + public static var devicePowerIsOff: Errno { .init(rawValue: _EPWROFF) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "devicePowerIsOff") @@ -1120,7 +1117,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EDEVERR`. @_alwaysEmitIntoClient - public static var deviceError: Errno { Errno(_EDEVERR) } + public static var deviceError: Errno { .init(rawValue: _EDEVERR) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "deviceError") @@ -1135,7 +1132,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EOVERFLOW`. @_alwaysEmitIntoClient - public static var overflow: Errno { Errno(_EOVERFLOW) } + public static var overflow: Errno { .init(rawValue: _EOVERFLOW) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "overflow") @@ -1149,7 +1146,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EBADEXEC`. @_alwaysEmitIntoClient - public static var badExecutable: Errno { Errno(_EBADEXEC) } + public static var badExecutable: Errno { .init(rawValue: _EBADEXEC) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "badExecutable") @@ -1161,7 +1158,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EBADARCH`. @_alwaysEmitIntoClient - public static var badCPUType: Errno { Errno(_EBADARCH) } + public static var badCPUType: Errno { .init(rawValue: _EBADARCH) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "badCPUType") @@ -1174,7 +1171,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ESHLIBVERS`. @_alwaysEmitIntoClient - public static var sharedLibraryVersionMismatch: Errno { Errno(_ESHLIBVERS) } + public static var sharedLibraryVersionMismatch: Errno { .init(rawValue: _ESHLIBVERS) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "sharedLibraryVersionMismatch") @@ -1186,7 +1183,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EBADMACHO`. @_alwaysEmitIntoClient - public static var malformedMachO: Errno { Errno(_EBADMACHO) } + public static var malformedMachO: Errno { .init(rawValue: _EBADMACHO) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "malformedMachO") @@ -1199,7 +1196,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ECANCELED`. @_alwaysEmitIntoClient - public static var canceled: Errno { Errno(_ECANCELED) } + public static var canceled: Errno { .init(rawValue: _ECANCELED) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "canceled") @@ -1212,7 +1209,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EIDRM`. @_alwaysEmitIntoClient - public static var identifierRemoved: Errno { Errno(_EIDRM) } + public static var identifierRemoved: Errno { .init(rawValue: _EIDRM) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "identifierRemoved") @@ -1225,7 +1222,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOMSG`. @_alwaysEmitIntoClient - public static var noMessage: Errno { Errno(_ENOMSG) } + public static var noMessage: Errno { .init(rawValue: _ENOMSG) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noMessage") @@ -1240,7 +1237,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EILSEQ`. @_alwaysEmitIntoClient - public static var illegalByteSequence: Errno { Errno(_EILSEQ) } + public static var illegalByteSequence: Errno { .init(rawValue: _EILSEQ) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "illegalByteSequence") @@ -1253,7 +1250,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOATTR`. @_alwaysEmitIntoClient - public static var attributeNotFound: Errno { Errno(_ENOATTR) } + public static var attributeNotFound: Errno { .init(rawValue: _ENOATTR) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "attributeNotFound") @@ -1268,7 +1265,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EBADMSG`. @_alwaysEmitIntoClient - public static var badMessage: Errno { Errno(_EBADMSG) } + public static var badMessage: Errno { .init(rawValue: _EBADMSG) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "badMessage") @@ -1281,7 +1278,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EMULTIHOP`. @_alwaysEmitIntoClient - public static var multiHop: Errno { Errno(_EMULTIHOP) } + public static var multiHop: Errno { .init(rawValue: _EMULTIHOP) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "multiHop") @@ -1293,7 +1290,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENODATA`. @_alwaysEmitIntoClient - public static var noData: Errno { Errno(_ENODATA) } + public static var noData: Errno { .init(rawValue: _ENODATA) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noData") @@ -1305,7 +1302,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOLINK`. @_alwaysEmitIntoClient - public static var noLink: Errno { Errno(_ENOLINK) } + public static var noLink: Errno { .init(rawValue: _ENOLINK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noLink") @@ -1317,7 +1314,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOSR`. @_alwaysEmitIntoClient - public static var noStreamResources: Errno { Errno(_ENOSR) } + public static var noStreamResources: Errno { .init(rawValue: _ENOSR) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noStreamResources") @@ -1329,7 +1326,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ENOSTR`. @_alwaysEmitIntoClient - public static var notStream: Errno { Errno(_ENOSTR) } + public static var notStream: Errno { .init(rawValue: _ENOSTR) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notStream") @@ -1344,7 +1341,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EPROTO`. @_alwaysEmitIntoClient - public static var protocolError: Errno { Errno(_EPROTO) } + public static var protocolError: Errno { .init(rawValue: _EPROTO) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "protocolError") @@ -1357,7 +1354,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `ETIME`. @_alwaysEmitIntoClient - public static var timeout: Errno { Errno(_ETIME) } + public static var timeout: Errno { .init(rawValue: _ETIME) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "timeout") @@ -1372,7 +1369,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { /// /// The corresponding C error is `EOPNOTSUPP`. @_alwaysEmitIntoClient - public static var notSupportedOnSocket: Errno { Errno(_EOPNOTSUPP) } + public static var notSupportedOnSocket: Errno { .init(rawValue: _EOPNOTSUPP) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notSupportedOnSocket") @@ -1387,7 +1384,7 @@ extension Errno { /// /// The corresponding C error is `EWOULDBLOCK`. @_alwaysEmitIntoClient - public static var wouldBlock: Errno { Errno(_EWOULDBLOCK) } + public static var wouldBlock: Errno { .init(rawValue: _EWOULDBLOCK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "wouldBlock") @@ -1397,7 +1394,7 @@ extension Errno { /// /// The corresponding C error is `ETOOMANYREFS`. @_alwaysEmitIntoClient - public static var tooManyReferences: Errno { Errno(_ETOOMANYREFS) } + public static var tooManyReferences: Errno { .init(rawValue: _ETOOMANYREFS) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManyReferences") @@ -1407,7 +1404,7 @@ extension Errno { /// /// The corresponding C error is `EREMOTE`. @_alwaysEmitIntoClient - public static var tooManyRemoteLevels: Errno { Errno(_EREMOTE) } + public static var tooManyRemoteLevels: Errno { .init(rawValue: _EREMOTE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManyRemoteLevels") @@ -1418,7 +1415,7 @@ extension Errno { /// /// The corresponding C error is `ENOPOLICY`. @_alwaysEmitIntoClient - public static var noSuchPolicy: Errno { Errno(_ENOPOLICY) } + public static var noSuchPolicy: Errno { .init(rawValue: _ENOPOLICY) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noSuchPolicy") @@ -1430,7 +1427,7 @@ extension Errno { /// /// The corresponding C error is `ENOTRECOVERABLE`. @_alwaysEmitIntoClient - public static var notRecoverable: Errno { Errno(_ENOTRECOVERABLE) } + public static var notRecoverable: Errno { .init(rawValue: _ENOTRECOVERABLE) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notRecoverable") @@ -1440,7 +1437,7 @@ extension Errno { /// /// The corresponding C error is `EOWNERDEAD`. @_alwaysEmitIntoClient - public static var previousOwnerDied: Errno { Errno(_EOWNERDEAD) } + public static var previousOwnerDied: Errno { .init(rawValue: _EOWNERDEAD) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "previousOwnerDied") @@ -1452,7 +1449,7 @@ extension Errno { /// /// The corresponding C error is `EQFULL`. @_alwaysEmitIntoClient - public static var outputQueueFull: Errno { Errno(_EQFULL) } + public static var outputQueueFull: Errno { .init(rawValue: _EQFULL) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "outputQueueFull") @@ -1466,7 +1463,7 @@ extension Errno { /// /// The corresponding C error is `ELAST`. @_alwaysEmitIntoClient - public static var lastErrnoValue: Errno { Errno(_ELAST) } + public static var lastErrnoValue: Errno { .init(rawValue: _ELAST) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "lastErrnoValue") diff --git a/Sources/System/FileDescriptor.swift b/Sources/System/FileDescriptor.swift index 0ac7f4f..d7b931e 100644 --- a/Sources/System/FileDescriptor.swift +++ b/Sources/System/FileDescriptor.swift @@ -98,9 +98,6 @@ extension FileDescriptor { @_alwaysEmitIntoClient public init(rawValue: CInt) { self.rawValue = rawValue } - @_alwaysEmitIntoClient - private init(_ raw: CInt) { self.init(rawValue: raw) } - #if !os(Windows) /// Indicates that opening the file doesn't /// wait for the file or device to become available. @@ -117,7 +114,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_NONBLOCK`. @_alwaysEmitIntoClient - public static var nonBlocking: OpenOptions { OpenOptions(_O_NONBLOCK) } + public static var nonBlocking: OpenOptions { .init(rawValue: _O_NONBLOCK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "nonBlocking") @@ -133,7 +130,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_APPEND`. @_alwaysEmitIntoClient - public static var append: OpenOptions { OpenOptions(_O_APPEND) } + public static var append: OpenOptions { .init(rawValue: _O_APPEND) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "append") @@ -143,7 +140,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_CREAT`. @_alwaysEmitIntoClient - public static var create: OpenOptions { OpenOptions(_O_CREAT) } + public static var create: OpenOptions { .init(rawValue: _O_CREAT) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "create") @@ -157,7 +154,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_TRUNC`. @_alwaysEmitIntoClient - public static var truncate: OpenOptions { OpenOptions(_O_TRUNC) } + public static var truncate: OpenOptions { .init(rawValue: _O_TRUNC) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "truncate") @@ -179,7 +176,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_EXCL`. @_alwaysEmitIntoClient - public static var exclusiveCreate: OpenOptions { OpenOptions(_O_EXCL) } + public static var exclusiveCreate: OpenOptions { .init(rawValue: _O_EXCL) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "exclusiveCreate") @@ -197,7 +194,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_SHLOCK`. @_alwaysEmitIntoClient - public static var sharedLock: OpenOptions { OpenOptions(_O_SHLOCK) } + public static var sharedLock: OpenOptions { .init(rawValue: _O_SHLOCK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "sharedLock") @@ -214,7 +211,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_EXLOCK`. @_alwaysEmitIntoClient - public static var exclusiveLock: OpenOptions { OpenOptions(_O_EXLOCK) } + public static var exclusiveLock: OpenOptions { .init(rawValue: _O_EXLOCK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "exclusiveLock") @@ -232,7 +229,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_NOFOLLOW`. @_alwaysEmitIntoClient - public static var noFollow: OpenOptions { OpenOptions(_O_NOFOLLOW) } + public static var noFollow: OpenOptions { .init(rawValue: _O_NOFOLLOW) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noFollow") @@ -246,7 +243,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_DIRECTORY`. @_alwaysEmitIntoClient - public static var directory: OpenOptions { OpenOptions(_O_DIRECTORY) } + public static var directory: OpenOptions { .init(rawValue: _O_DIRECTORY) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "directory") @@ -265,7 +262,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_SYMLINK`. @_alwaysEmitIntoClient - public static var symlink: OpenOptions { OpenOptions(_O_SYMLINK) } + public static var symlink: OpenOptions { .init(rawValue: _O_SYMLINK) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "symlink") @@ -281,7 +278,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_EVTONLY`. @_alwaysEmitIntoClient - public static var eventOnly: OpenOptions { OpenOptions(_O_EVTONLY) } + public static var eventOnly: OpenOptions { .init(rawValue: _O_EVTONLY) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "eventOnly") @@ -303,7 +300,7 @@ extension FileDescriptor { /// /// The corresponding C constant is `O_CLOEXEC`. @_alwaysEmitIntoClient - public static var closeOnExec: OpenOptions { OpenOptions(_O_CLOEXEC) } + public static var closeOnExec: OpenOptions { .init(rawValue: _O_CLOEXEC) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "closeOnExec") diff --git a/Sources/System/FilePermissions.swift b/Sources/System/FilePermissions.swift index 2cdb32f..918246e 100644 --- a/Sources/System/FilePermissions.swift +++ b/Sources/System/FilePermissions.swift @@ -27,104 +27,101 @@ public struct FilePermissions: OptionSet, Sendable, Hashable, Codable { @_alwaysEmitIntoClient public init(rawValue: CModeT) { self.rawValue = rawValue } - @_alwaysEmitIntoClient - private init(_ raw: CModeT) { self.init(rawValue: raw) } - /// Indicates that other users have read-only permission. @_alwaysEmitIntoClient - public static var otherRead: FilePermissions { FilePermissions(0o4) } + public static var otherRead: FilePermissions { .init(rawValue: 0o4) } /// Indicates that other users have write-only permission. @_alwaysEmitIntoClient - public static var otherWrite: FilePermissions { FilePermissions(0o2) } + public static var otherWrite: FilePermissions { .init(rawValue: 0o2) } /// Indicates that other users have execute-only permission. @_alwaysEmitIntoClient - public static var otherExecute: FilePermissions { FilePermissions(0o1) } + public static var otherExecute: FilePermissions { .init(rawValue: 0o1) } /// Indicates that other users have read-write permission. @_alwaysEmitIntoClient - public static var otherReadWrite: FilePermissions { FilePermissions(0o6) } + public static var otherReadWrite: FilePermissions { .init(rawValue: 0o6) } /// Indicates that other users have read-execute permission. @_alwaysEmitIntoClient - public static var otherReadExecute: FilePermissions { FilePermissions(0o5) } + public static var otherReadExecute: FilePermissions { .init(rawValue: 0o5) } /// Indicates that other users have write-execute permission. @_alwaysEmitIntoClient - public static var otherWriteExecute: FilePermissions { FilePermissions(0o3) } + public static var otherWriteExecute: FilePermissions { .init(rawValue: 0o3) } /// Indicates that other users have read, write, and execute permission. @_alwaysEmitIntoClient - public static var otherReadWriteExecute: FilePermissions { FilePermissions(0o7) } + public static var otherReadWriteExecute: FilePermissions { .init(rawValue: 0o7) } /// Indicates that the group has read-only permission. @_alwaysEmitIntoClient - public static var groupRead: FilePermissions { FilePermissions(0o40) } + public static var groupRead: FilePermissions { .init(rawValue: 0o40) } /// Indicates that the group has write-only permission. @_alwaysEmitIntoClient - public static var groupWrite: FilePermissions { FilePermissions(0o20) } + public static var groupWrite: FilePermissions { .init(rawValue: 0o20) } /// Indicates that the group has execute-only permission. @_alwaysEmitIntoClient - public static var groupExecute: FilePermissions { FilePermissions(0o10) } + public static var groupExecute: FilePermissions { .init(rawValue: 0o10) } /// Indicates that the group has read-write permission. @_alwaysEmitIntoClient - public static var groupReadWrite: FilePermissions { FilePermissions(0o60) } + public static var groupReadWrite: FilePermissions { .init(rawValue: 0o60) } /// Indicates that the group has read-execute permission. @_alwaysEmitIntoClient - public static var groupReadExecute: FilePermissions { FilePermissions(0o50) } + public static var groupReadExecute: FilePermissions { .init(rawValue: 0o50) } /// Indicates that the group has write-execute permission. @_alwaysEmitIntoClient - public static var groupWriteExecute: FilePermissions { FilePermissions(0o30) } + public static var groupWriteExecute: FilePermissions { .init(rawValue: 0o30) } /// Indicates that the group has read, write, and execute permission. @_alwaysEmitIntoClient - public static var groupReadWriteExecute: FilePermissions { FilePermissions(0o70) } + public static var groupReadWriteExecute: FilePermissions { .init(rawValue: 0o70) } /// Indicates that the owner has read-only permission. @_alwaysEmitIntoClient - public static var ownerRead: FilePermissions { FilePermissions(0o400) } + public static var ownerRead: FilePermissions { .init(rawValue: 0o400) } /// Indicates that the owner has write-only permission. @_alwaysEmitIntoClient - public static var ownerWrite: FilePermissions { FilePermissions(0o200) } + public static var ownerWrite: FilePermissions { .init(rawValue: 0o200) } /// Indicates that the owner has execute-only permission. @_alwaysEmitIntoClient - public static var ownerExecute: FilePermissions { FilePermissions(0o100) } + public static var ownerExecute: FilePermissions { .init(rawValue: 0o100) } /// Indicates that the owner has read-write permission. @_alwaysEmitIntoClient - public static var ownerReadWrite: FilePermissions { FilePermissions(0o600) } + public static var ownerReadWrite: FilePermissions { .init(rawValue: 0o600) } /// Indicates that the owner has read-execute permission. @_alwaysEmitIntoClient - public static var ownerReadExecute: FilePermissions { FilePermissions(0o500) } + public static var ownerReadExecute: FilePermissions { .init(rawValue: 0o500) } /// Indicates that the owner has write-execute permission. @_alwaysEmitIntoClient - public static var ownerWriteExecute: FilePermissions { FilePermissions(0o300) } + public static var ownerWriteExecute: FilePermissions { .init(rawValue: 0o300) } /// Indicates that the owner has read, write, and execute permission. @_alwaysEmitIntoClient - public static var ownerReadWriteExecute: FilePermissions { FilePermissions(0o700) } + public static var ownerReadWriteExecute: FilePermissions { .init(rawValue: 0o700) } /// Indicates that the file is executed as the owner. /// /// For more information, see the `setuid(2)` man page. @_alwaysEmitIntoClient - public static var setUserID: FilePermissions { FilePermissions(0o4000) } + public static var setUserID: FilePermissions { .init(rawValue: 0o4000) } /// Indicates that the file is executed as the group. /// /// For more information, see the `setgid(2)` man page. @_alwaysEmitIntoClient - public static var setGroupID: FilePermissions { FilePermissions(0o2000) } + public static var setGroupID: FilePermissions { .init(rawValue: 0o2000) } /// Indicates that executable's text segment /// should be kept in swap space even after it exits. @@ -132,7 +129,7 @@ public struct FilePermissions: OptionSet, Sendable, Hashable, Codable { /// For more information, see the `chmod(2)` man page's /// discussion of `S_ISVTX` (the sticky bit). @_alwaysEmitIntoClient - public static var saveText: FilePermissions { FilePermissions(0o1000) } + public static var saveText: FilePermissions { .init(rawValue: 0o1000) } } @available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *) From 8df901cb2495f4300f20749785af1a4440064dda Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Sun, 12 May 2024 14:07:11 -0700 Subject: [PATCH 25/38] fix a 32-bit overflow issue - ensures compatibility with watchOS --- Sources/System/MachPort.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sources/System/MachPort.swift b/Sources/System/MachPort.swift index b356789..8cc0509 100644 --- a/Sources/System/MachPort.swift +++ b/Sources/System/MachPort.swift @@ -56,8 +56,10 @@ public enum Mach { self._name = name if RightType.self == ReceiveRight.self { - precondition(name != 0xFFFFFFFF /* MACH_PORT_DEAD */, - "Receive rights cannot be dead names") + precondition( + _name != (0xFFFFFFFF as mach_port_name_t) /* MACH_PORT_DEAD */, + "Receive rights cannot be dead names" + ) let secret = mach_port_context_t(arc4random()) _machPrecondition(mach_port_guard(mach_task_self_, name, secret, 0)) @@ -87,8 +89,10 @@ public enum Mach { deinit { if RightType.self == ReceiveRight.self { - precondition(_name != 0xFFFFFFFF /* MACH_PORT_DEAD */, - "Receive rights cannot be dead names") + precondition( + _name != (0xFFFFFFFF as mach_port_name_t) /* MACH_PORT_DEAD */, + "Receive rights cannot be dead names" + ) _machPrecondition( mach_port_destruct(mach_task_self_, _name, 0, _context) ) From e11703d91539fdb3290d7cf1858f6fdcdddbc14c Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 30 Apr 2024 17:11:39 +0100 Subject: [PATCH 26/38] Add support for `WASILibc` module (#159) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Quoting the [wasi.dev landing page](https://wasi.dev): > The WebAssembly System Interface (WASI) is a group of standard API specifications for software compiled to the W3C WebAssembly (Wasm) standard. WASI is designed to provide a secure standard interface for applications that can be compiled to Wasm from any language, and that may run anywhere—from browsers to clouds to embedded devices. Currently support for WASI in Swift is based on [`wasi-libc`](https://github.com/WebAssembly/wasi-libc). Adding support for `wasi-libc` mostly amounted to excluding unsupported errnos, adding TLS dictionary storage shim for single-threaded environment, and adding constants in C headers for macros that Clang importer currently doesn't support. Co-authored-by: Guillaume Lessard # Conflicts: # Sources/System/FileOperations.swift # Sources/System/Internals/Syscalls.swift --- Sources/CSystem/include/CSystemWASI.h | 31 +++++++ Sources/CSystem/include/module.modulemap | 1 + Sources/System/Errno.swift | 23 +++++- Sources/System/Internals/CInterop.swift | 4 +- Sources/System/Internals/Constants.swift | 100 ++++++++++++++++++++--- Sources/System/Internals/Exports.swift | 29 ++++++- 6 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 Sources/CSystem/include/CSystemWASI.h diff --git a/Sources/CSystem/include/CSystemWASI.h b/Sources/CSystem/include/CSystemWASI.h new file mode 100644 index 0000000..9877853 --- /dev/null +++ b/Sources/CSystem/include/CSystemWASI.h @@ -0,0 +1,31 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2024 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information +*/ + +#pragma once + +#if __wasi__ + +#include +#include + +// wasi-libc defines the following constants in a way that Clang Importer can't +// understand, so we need to expose them manually. +static inline int32_t _getConst_O_ACCMODE(void) { return O_ACCMODE; } +static inline int32_t _getConst_O_APPEND(void) { return O_APPEND; } +static inline int32_t _getConst_O_CREAT(void) { return O_CREAT; } +static inline int32_t _getConst_O_DIRECTORY(void) { return O_DIRECTORY; } +static inline int32_t _getConst_O_EXCL(void) { return O_EXCL; } +static inline int32_t _getConst_O_NONBLOCK(void) { return O_NONBLOCK; } +static inline int32_t _getConst_O_TRUNC(void) { return O_TRUNC; } +static inline int32_t _getConst_O_WRONLY(void) { return O_WRONLY; } + +static inline int32_t _getConst_EWOULDBLOCK(void) { return EWOULDBLOCK; } +static inline int32_t _getConst_EOPNOTSUPP(void) { return EOPNOTSUPP; } + +#endif diff --git a/Sources/CSystem/include/module.modulemap b/Sources/CSystem/include/module.modulemap index 776f776..6e8b89e 100644 --- a/Sources/CSystem/include/module.modulemap +++ b/Sources/CSystem/include/module.modulemap @@ -1,5 +1,6 @@ module CSystem { header "CSystemLinux.h" + header "CSystemWASI.h" header "CSystemWindows.h" export * } diff --git a/Sources/System/Errno.swift b/Sources/System/Errno.swift index 498ccbc..093023d 100644 --- a/Sources/System/Errno.swift +++ b/Sources/System/Errno.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2021 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -226,7 +226,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "badAddress") public static var EFAULT: Errno { badAddress } -#if !os(Windows) +#if !os(Windows) && !os(WASI) /// Not a block device. /// /// You attempted a block device operation on a nonblock device or file. @@ -618,6 +618,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "protocolNotSupported") public static var EPROTONOSUPPORT: Errno { protocolNotSupported } +#if !os(WASI) /// Socket type not supported. /// /// Support for the socket type hasn't been configured into the system @@ -630,6 +631,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "socketTypeNotSupported") public static var ESOCKTNOSUPPORT: Errno { socketTypeNotSupported } +#endif /// Not supported. /// @@ -644,6 +646,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "notSupported") public static var ENOTSUP: Errno { notSupported } +#if !os(WASI) /// Protocol family not supported. /// /// The protocol family hasn't been configured into the system @@ -656,6 +659,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "protocolFamilyNotSupported") public static var EPFNOSUPPORT: Errno { protocolFamilyNotSupported } +#endif /// The address family isn't supported by the protocol family. /// @@ -802,6 +806,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "socketNotConnected") public static var ENOTCONN: Errno { socketNotConnected } +#if !os(WASI) /// Can't send after socket shutdown. /// /// A request to send data wasn't permitted @@ -815,6 +820,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "socketShutdown") public static var ESHUTDOWN: Errno { socketShutdown } +#endif /// Operation timed out. /// @@ -871,6 +877,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "fileNameTooLong") public static var ENAMETOOLONG: Errno { fileNameTooLong } +#if !os(WASI) /// The host is down. /// /// A socket operation failed because the destination host was down. @@ -882,6 +889,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "hostIsDown") public static var EHOSTDOWN: Errno { hostIsDown } +#endif /// No route to host. /// @@ -920,6 +928,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { public static var EPROCLIM: Errno { tooManyProcesses } #endif +#if !os(WASI) /// Too many users. /// /// The quota system ran out of table entries. @@ -931,6 +940,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManyUsers") public static var EUSERS: Errno { tooManyUsers } +#endif /// Disk quota exceeded. /// @@ -1284,6 +1294,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "multiHop") public static var EMULTIHOP: Errno { multiHop } +#if !os(WASI) /// No message available. /// /// No message was available to be received by the requested operation. @@ -1295,6 +1306,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noData") public static var ENODATA: Errno { noData } +#endif /// Reserved. /// @@ -1308,6 +1320,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "noLink") public static var ENOLINK: Errno { noLink } +#if !os(WASI) /// Reserved. /// /// This error is reserved for future use. @@ -1331,6 +1344,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notStream") public static var ENOSTR: Errno { notStream } +#endif #endif /// Protocol error. @@ -1347,7 +1361,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "protocolError") public static var EPROTO: Errno { protocolError } -#if !os(OpenBSD) +#if !os(OpenBSD) && !os(WASI) /// Reserved. /// /// This error is reserved for future use. @@ -1379,7 +1393,6 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { // Constants defined in header but not man page @available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *) extension Errno { - /// Operation would block. /// /// The corresponding C error is `EWOULDBLOCK`. @@ -1390,6 +1403,7 @@ extension Errno { @available(*, unavailable, renamed: "wouldBlock") public static var EWOULDBLOCK: Errno { wouldBlock } +#if !os(WASI) /// Too many references: can't splice. /// /// The corresponding C error is `ETOOMANYREFS`. @@ -1409,6 +1423,7 @@ extension Errno { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "tooManyRemoteLevels") public static var EREMOTE: Errno { tooManyRemoteLevels } +#endif #if SYSTEM_PACKAGE_DARWIN /// No such policy registered. diff --git a/Sources/System/Internals/CInterop.swift b/Sources/System/Internals/CInterop.swift index 3b4792e..19cf4d5 100644 --- a/Sources/System/Internals/CInterop.swift +++ b/Sources/System/Internals/CInterop.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -18,6 +18,8 @@ import Glibc #elseif canImport(Musl) @_implementationOnly import CSystem import Musl +#elseif canImport(WASILibc) +import WASILibc #else #error("Unsupported Platform") #endif diff --git a/Sources/System/Internals/Constants.swift b/Sources/System/Internals/Constants.swift index 8d4e4bb..27a145f 100644 --- a/Sources/System/Internals/Constants.swift +++ b/Sources/System/Internals/Constants.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -21,6 +21,9 @@ import Glibc #elseif canImport(Musl) import CSystem import Musl +#elseif canImport(WASILibc) +import CSystem +import WASILibc #else #error("Unsupported Platform") #endif @@ -73,7 +76,7 @@ internal var _EACCES: CInt { EACCES } @_alwaysEmitIntoClient internal var _EFAULT: CInt { EFAULT } -#if !os(Windows) +#if !os(Windows) && !os(WASI) @_alwaysEmitIntoClient internal var _ENOTBLK: CInt { ENOTBLK } #endif @@ -141,7 +144,13 @@ internal var _ERANGE: CInt { ERANGE } internal var _EAGAIN: CInt { EAGAIN } @_alwaysEmitIntoClient -internal var _EWOULDBLOCK: CInt { EWOULDBLOCK } +internal var _EWOULDBLOCK: CInt { +#if os(WASI) + _getConst_EWOULDBLOCK() +#else + EWOULDBLOCK +#endif +} @_alwaysEmitIntoClient internal var _EINPROGRESS: CInt { EINPROGRESS } @@ -167,6 +176,7 @@ internal var _ENOPROTOOPT: CInt { ENOPROTOOPT } @_alwaysEmitIntoClient internal var _EPROTONOSUPPORT: CInt { EPROTONOSUPPORT } +#if !os(WASI) @_alwaysEmitIntoClient internal var _ESOCKTNOSUPPORT: CInt { #if os(Windows) @@ -175,6 +185,7 @@ internal var _ESOCKTNOSUPPORT: CInt { return ESOCKTNOSUPPORT #endif } +#endif @_alwaysEmitIntoClient internal var _ENOTSUP: CInt { @@ -185,6 +196,7 @@ internal var _ENOTSUP: CInt { #endif } +#if !os(WASI) @_alwaysEmitIntoClient internal var _EPFNOSUPPORT: CInt { #if os(Windows) @@ -193,6 +205,7 @@ internal var _EPFNOSUPPORT: CInt { return EPFNOSUPPORT #endif } +#endif @_alwaysEmitIntoClient internal var _EAFNOSUPPORT: CInt { EAFNOSUPPORT } @@ -227,6 +240,7 @@ internal var _EISCONN: CInt { EISCONN } @_alwaysEmitIntoClient internal var _ENOTCONN: CInt { ENOTCONN } +#if !os(WASI) @_alwaysEmitIntoClient internal var _ESHUTDOWN: CInt { #if os(Windows) @@ -244,6 +258,7 @@ internal var _ETOOMANYREFS: CInt { return ETOOMANYREFS #endif } +#endif @_alwaysEmitIntoClient internal var _ETIMEDOUT: CInt { ETIMEDOUT } @@ -257,6 +272,7 @@ internal var _ELOOP: CInt { ELOOP } @_alwaysEmitIntoClient internal var _ENAMETOOLONG: CInt { ENAMETOOLONG } +#if !os(WASI) @_alwaysEmitIntoClient internal var _EHOSTDOWN: CInt { #if os(Windows) @@ -265,6 +281,7 @@ internal var _EHOSTDOWN: CInt { return EHOSTDOWN #endif } +#endif @_alwaysEmitIntoClient internal var _EHOSTUNREACH: CInt { EHOSTUNREACH } @@ -277,6 +294,7 @@ internal var _ENOTEMPTY: CInt { ENOTEMPTY } internal var _EPROCLIM: CInt { EPROCLIM } #endif +#if !os(WASI) @_alwaysEmitIntoClient internal var _EUSERS: CInt { #if os(Windows) @@ -285,6 +303,7 @@ internal var _EUSERS: CInt { return EUSERS #endif } +#endif @_alwaysEmitIntoClient internal var _EDQUOT: CInt { @@ -304,6 +323,7 @@ internal var _ESTALE: CInt { #endif } +#if !os(WASI) @_alwaysEmitIntoClient internal var _EREMOTE: CInt { #if os(Windows) @@ -312,6 +332,7 @@ internal var _EREMOTE: CInt { return EREMOTE #endif } +#endif #if SYSTEM_PACKAGE_DARWIN @_alwaysEmitIntoClient @@ -399,30 +420,41 @@ internal var _EBADMSG: CInt { EBADMSG } @_alwaysEmitIntoClient internal var _EMULTIHOP: CInt { EMULTIHOP } +#if !os(WASI) @_alwaysEmitIntoClient internal var _ENODATA: CInt { ENODATA } +#endif @_alwaysEmitIntoClient internal var _ENOLINK: CInt { ENOLINK } +#if !os(WASI) @_alwaysEmitIntoClient internal var _ENOSR: CInt { ENOSR } @_alwaysEmitIntoClient internal var _ENOSTR: CInt { ENOSTR } #endif +#endif @_alwaysEmitIntoClient internal var _EPROTO: CInt { EPROTO } -#if !os(OpenBSD) +#if !os(OpenBSD) && !os(WASI) @_alwaysEmitIntoClient internal var _ETIME: CInt { ETIME } #endif #endif + @_alwaysEmitIntoClient -internal var _EOPNOTSUPP: CInt { EOPNOTSUPP } +internal var _EOPNOTSUPP: CInt { +#if os(WASI) + _getConst_EOPNOTSUPP() +#else + EOPNOTSUPP +#endif +} #if SYSTEM_PACKAGE_DARWIN @_alwaysEmitIntoClient @@ -462,15 +494,33 @@ internal var _O_ACCMODE: CInt { 0x03|O_SEARCH } #else // TODO: API? @_alwaysEmitIntoClient -internal var _O_ACCMODE: CInt { O_ACCMODE } +internal var _O_ACCMODE: CInt { +#if os(WASI) + _getConst_O_ACCMODE() +#else + O_ACCMODE +#endif +} #endif @_alwaysEmitIntoClient -internal var _O_NONBLOCK: CInt { O_NONBLOCK } +internal var _O_NONBLOCK: CInt { +#if os(WASI) + _getConst_O_NONBLOCK() +#else + O_NONBLOCK +#endif +} #endif @_alwaysEmitIntoClient -internal var _O_APPEND: CInt { O_APPEND } +internal var _O_APPEND: CInt { +#if os(WASI) + _getConst_O_APPEND() +#else + O_APPEND +#endif +} #if SYSTEM_PACKAGE_DARWIN @_alwaysEmitIntoClient @@ -481,22 +531,42 @@ internal var _O_EXLOCK: CInt { O_EXLOCK } #endif #if !os(Windows) +#if !os(WASI) // TODO: API? @_alwaysEmitIntoClient internal var _O_ASYNC: CInt { O_ASYNC } +#endif @_alwaysEmitIntoClient internal var _O_NOFOLLOW: CInt { O_NOFOLLOW } #endif @_alwaysEmitIntoClient -internal var _O_CREAT: CInt { O_CREAT } +internal var _O_CREAT: CInt { +#if os(WASI) + _getConst_O_CREAT() +#else + O_CREAT +#endif +} @_alwaysEmitIntoClient -internal var _O_TRUNC: CInt { O_TRUNC } +internal var _O_TRUNC: CInt { +#if os(WASI) + _getConst_O_TRUNC() +#else + O_TRUNC +#endif +} @_alwaysEmitIntoClient -internal var _O_EXCL: CInt { O_EXCL } +internal var _O_EXCL: CInt { +#if os(WASI) + _getConst_O_EXCL() +#else + O_EXCL +#endif +} #if SYSTEM_PACKAGE_DARWIN @_alwaysEmitIntoClient @@ -509,7 +579,13 @@ internal var _O_EVTONLY: CInt { O_EVTONLY } internal var _O_NOCTTY: CInt { O_NOCTTY } @_alwaysEmitIntoClient -internal var _O_DIRECTORY: CInt { O_DIRECTORY } +internal var _O_DIRECTORY: CInt { +#if os(WASI) + _getConst_O_DIRECTORY() +#else + O_DIRECTORY +#endif +} #endif #if SYSTEM_PACKAGE_DARWIN diff --git a/Sources/System/Internals/Exports.swift b/Sources/System/Internals/Exports.swift index e20454e..f4358c5 100644 --- a/Sources/System/Internals/Exports.swift +++ b/Sources/System/Internals/Exports.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -23,6 +23,8 @@ import Glibc #elseif canImport(Musl) @_implementationOnly import CSystem import Musl +#elseif canImport(WASILibc) +import WASILibc #else #error("Unsupported Platform") #endif @@ -58,6 +60,11 @@ internal var system_errno: CInt { get { Musl.errno } set { Musl.errno = newValue } } +#elseif canImport(WASILibc) +internal var system_errno: CInt { + get { WASILibc.errno } + set { WASILibc.errno = newValue } +} #endif // MARK: C stdlib decls @@ -149,6 +156,24 @@ extension String { // TLS #if os(Windows) internal typealias _PlatformTLSKey = DWORD +#elseif os(WASI) && (swift(<6.1) || !_runtime(_multithreaded)) +// Mock TLS storage for single-threaded WASI +internal final class _PlatformTLSKey { + fileprivate init() {} +} +private final class TLSStorage: @unchecked Sendable { + var storage = [ObjectIdentifier: UnsafeMutableRawPointer]() +} +private let sharedTLSStorage = TLSStorage() + +func pthread_setspecific(_ key: _PlatformTLSKey, _ p: UnsafeMutableRawPointer?) -> Int { + sharedTLSStorage.storage[ObjectIdentifier(key)] = p + return 0 +} + +func pthread_getspecific(_ key: _PlatformTLSKey) -> UnsafeMutableRawPointer? { + sharedTLSStorage.storage[ObjectIdentifier(key)] +} #else internal typealias _PlatformTLSKey = pthread_key_t #endif @@ -160,6 +185,8 @@ internal func makeTLSKey() -> _PlatformTLSKey { fatalError("Unable to create key") } return raw + #elseif os(WASI) && (swift(<6.1) || !_runtime(_multithreaded)) + return _PlatformTLSKey() #else var raw = pthread_key_t() guard 0 == pthread_key_create(&raw, nil) else { From b2b60a6b9495f7e06b3aece461cc1b9ccaf917c5 Mon Sep 17 00:00:00 2001 From: Finagolfin Date: Thu, 9 May 2024 19:29:36 +0530 Subject: [PATCH 27/38] Android: DIR* is an OpaquePointer in Bionic too --- Sources/System/Internals/Syscalls.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index 3794d6e..f40b168 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -189,7 +189,7 @@ internal func system_confstr( internal let SYSTEM_AT_REMOVE_DIR = AT_REMOVEDIR internal let SYSTEM_DT_DIR = DT_DIR internal typealias system_dirent = dirent -#if os(Linux) +#if os(Linux) || os(Android) internal typealias system_DIRPtr = OpaquePointer #else internal typealias system_DIRPtr = UnsafeMutablePointer From 209dfeacb8e052a3425b40c9da83fa1bda8f73ee Mon Sep 17 00:00:00 2001 From: Finagolfin Date: Thu, 4 Jul 2024 23:37:25 +0530 Subject: [PATCH 28/38] Import new Android overlay Use Bionic module instead where possible --- Sources/System/Internals/CInterop.swift | 3 +++ Sources/System/Internals/Constants.swift | 2 ++ Sources/System/Internals/Exports.swift | 8 ++++++++ Sources/System/Internals/Syscalls.swift | 2 ++ 4 files changed, 15 insertions(+) diff --git a/Sources/System/Internals/CInterop.swift b/Sources/System/Internals/CInterop.swift index 19cf4d5..80d37d0 100644 --- a/Sources/System/Internals/CInterop.swift +++ b/Sources/System/Internals/CInterop.swift @@ -20,6 +20,9 @@ import Glibc import Musl #elseif canImport(WASILibc) import WASILibc +#elseif canImport(Bionic) +@_implementationOnly import CSystem +import Bionic #else #error("Unsupported Platform") #endif diff --git a/Sources/System/Internals/Constants.swift b/Sources/System/Internals/Constants.swift index 27a145f..904e5b2 100644 --- a/Sources/System/Internals/Constants.swift +++ b/Sources/System/Internals/Constants.swift @@ -24,6 +24,8 @@ import Musl #elseif canImport(WASILibc) import CSystem import WASILibc +#elseif canImport(Android) +import Android #else #error("Unsupported Platform") #endif diff --git a/Sources/System/Internals/Exports.swift b/Sources/System/Internals/Exports.swift index f4358c5..d18102a 100644 --- a/Sources/System/Internals/Exports.swift +++ b/Sources/System/Internals/Exports.swift @@ -25,6 +25,9 @@ import Glibc import Musl #elseif canImport(WASILibc) import WASILibc +#elseif canImport(Android) +@_implementationOnly import CSystem +import Android #else #error("Unsupported Platform") #endif @@ -65,6 +68,11 @@ internal var system_errno: CInt { get { WASILibc.errno } set { WASILibc.errno = newValue } } +#elseif canImport(Android) +internal var system_errno: CInt { + get { Android.errno } + set { Android.errno = newValue } +} #endif // MARK: C stdlib decls diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index f40b168..6f885be 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -17,6 +17,8 @@ import Musl import WASILibc #elseif os(Windows) import ucrt +#elseif canImport(Android) +import Android #else #error("Unsupported Platform") #endif From 69e737de3db0a8d5426e8ef9ee966aa0ac3abb9a Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 7 Mar 2024 16:51:12 -0800 Subject: [PATCH 29/38] Generate SystemString.string consistently - SystemString.string and Slice.string should be generated in the same manner. Now they are. --- Sources/System/SystemString.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sources/System/SystemString.swift b/Sources/System/SystemString.swift index 01de8e8..2c73411 100644 --- a/Sources/System/SystemString.swift +++ b/Sources/System/SystemString.swift @@ -230,7 +230,9 @@ extension Slice where Base == SystemString { } internal var string: String { - withCodeUnits { String(decoding: $0, as: CInterop.PlatformUnicodeEncoding.self) } + withCodeUnits { + String(decoding: $0, as: CInterop.PlatformUnicodeEncoding.self) + } } internal func withPlatformString( @@ -272,7 +274,11 @@ extension SystemString: ExpressibleByStringLiteral { } extension SystemString: CustomStringConvertible, CustomDebugStringConvertible { - internal var string: String { String(decoding: self) } + internal var string: String { + self.withCodeUnits { + String(decoding: $0, as: CInterop.PlatformUnicodeEncoding.self) + } + } internal var description: String { string } internal var debugDescription: String { description.debugDescription } From 448bef7c38cac332679d47902ccca839050714e7 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 7 Mar 2024 16:53:49 -0800 Subject: [PATCH 30/38] Test SystemString.string property consistency --- Tests/SystemTests/SystemStringTests.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Tests/SystemTests/SystemStringTests.swift b/Tests/SystemTests/SystemStringTests.swift index a189171..c929171 100644 --- a/Tests/SystemTests/SystemStringTests.swift +++ b/Tests/SystemTests/SystemStringTests.swift @@ -254,6 +254,11 @@ final class SystemStringTest: XCTestCase { XCTAssert(str == "abcd") } + func testStringProperty() { + let source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63] + let str = SystemString(platformString: source) + XCTAssertEqual(str.string, str[...].string) + } } extension SystemStringTest { From 95784d018f93d5994dbf40446bbe834ac761a25d Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 7 Mar 2024 16:31:54 -0800 Subject: [PATCH 31/38] Fix behaviour of `withCodeUnits()` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The original implementation includes a trailing zero with the full collection, but the implementation for a slice does not. As a result, given a `let s: SystemString`, `s.string` was not equal to `s[…].string` --- Sources/System/SystemString.swift | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Sources/System/SystemString.swift b/Sources/System/SystemString.swift index 2c73411..1229d16 100644 --- a/Sources/System/SystemString.swift +++ b/Sources/System/SystemString.swift @@ -204,17 +204,14 @@ extension SystemString { try nullTerminatedStorage.withContiguousStorageIfAvailable(f)! } + // withCodeUnits does not include the null terminator internal func withCodeUnits( _ f: (UnsafeBufferPointer) throws -> T ) rethrows -> T { - try withSystemChars { chars in - let length = chars.count * MemoryLayout.stride - let count = length / MemoryLayout.stride - return try chars.baseAddress!.withMemoryRebound( - to: CInterop.PlatformUnicodeEncoding.CodeUnit.self, - capacity: count - ) { pointer in - try f(UnsafeBufferPointer(start: pointer, count: count)) + try withSystemChars { + try $0.withMemoryRebound(to: CInterop.PlatformUnicodeEncoding.CodeUnit.self) { + assert($0.last == .zero) + return try f(.init(start: $0.baseAddress, count: $0.count&-1)) } } } From d96435c72b5ab39825dc56d6f300d5f0adfe4c6b Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 7 Mar 2024 17:00:47 -0800 Subject: [PATCH 32/38] Rename `withSystemChars` - `withNullTerminatedSystemChars` makes it clear that the null-termination is included. Operations where the null-termination is included should be obvious. --- Sources/System/FilePath/FilePathComponents.swift | 2 +- Sources/System/SystemString.swift | 9 ++++----- Tests/SystemTests/SystemStringTests.swift | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Sources/System/FilePath/FilePathComponents.swift b/Sources/System/FilePath/FilePathComponents.swift index 6b8a940..a19526f 100644 --- a/Sources/System/FilePath/FilePathComponents.swift +++ b/Sources/System/FilePath/FilePathComponents.swift @@ -146,7 +146,7 @@ extension _StrSlice { internal func _withSystemChars( _ f: (UnsafeBufferPointer) throws -> T ) rethrows -> T { - try _storage.withSystemChars { + try _storage.withNullTerminatedSystemChars { try f(UnsafeBufferPointer(rebasing: $0[_range])) } } diff --git a/Sources/System/SystemString.swift b/Sources/System/SystemString.swift index 1229d16..1605718 100644 --- a/Sources/System/SystemString.swift +++ b/Sources/System/SystemString.swift @@ -197,18 +197,17 @@ extension SystemString: Hashable, Codable { extension SystemString { - // withSystemChars includes the null terminator - internal func withSystemChars( + internal func withNullTerminatedSystemChars( _ f: (UnsafeBufferPointer) throws -> T ) rethrows -> T { - try nullTerminatedStorage.withContiguousStorageIfAvailable(f)! + try nullTerminatedStorage.withUnsafeBufferPointer(f) } // withCodeUnits does not include the null terminator internal func withCodeUnits( _ f: (UnsafeBufferPointer) throws -> T ) rethrows -> T { - try withSystemChars { + try withNullTerminatedSystemChars { try $0.withMemoryRebound(to: CInterop.PlatformUnicodeEncoding.CodeUnit.self) { assert($0.last == .zero) return try f(.init(start: $0.baseAddress, count: $0.count&-1)) @@ -314,7 +313,7 @@ extension SystemString { internal func withPlatformString( _ f: (UnsafePointer) throws -> T ) rethrows -> T { - try withSystemChars { chars in + try withNullTerminatedSystemChars { chars in let length = chars.count * MemoryLayout.stride return try chars.baseAddress!.withMemoryRebound( to: CInterop.PlatformChar.self, diff --git a/Tests/SystemTests/SystemStringTests.swift b/Tests/SystemTests/SystemStringTests.swift index c929171..1815392 100644 --- a/Tests/SystemTests/SystemStringTests.swift +++ b/Tests/SystemTests/SystemStringTests.swift @@ -101,7 +101,7 @@ struct StringTest: TestCase { // Test null insertion let rawChars = raw.lazy.map { SystemChar($0) } expectEqual(sysRaw, SystemString(rawChars.dropLast())) - sysRaw.withSystemChars { // TODO: assuming we want null in withSysChars + sysRaw.withNullTerminatedSystemChars { expectEqualSequence(rawChars, $0, "rawChars") } From b2d4917ebe1ef8a4fe28d39521878a752c1fb393 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 7 Mar 2024 17:05:54 -0800 Subject: [PATCH 33/38] Update copyright notices --- Sources/System/FilePath/FilePathComponents.swift | 2 +- Sources/System/SystemString.swift | 2 +- Tests/SystemTests/SystemStringTests.swift | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/System/FilePath/FilePathComponents.swift b/Sources/System/FilePath/FilePathComponents.swift index a19526f..b304a0a 100644 --- a/Sources/System/FilePath/FilePathComponents.swift +++ b/Sources/System/FilePath/FilePathComponents.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information diff --git a/Sources/System/SystemString.swift b/Sources/System/SystemString.swift index 1605718..57abc62 100644 --- a/Sources/System/SystemString.swift +++ b/Sources/System/SystemString.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information diff --git a/Tests/SystemTests/SystemStringTests.swift b/Tests/SystemTests/SystemStringTests.swift index 1815392..48ca83c 100644 --- a/Tests/SystemTests/SystemStringTests.swift +++ b/Tests/SystemTests/SystemStringTests.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information From 2e8f58ac417ac4a6ffb458c39b601c823cc295ff Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 17 Oct 2024 08:49:29 -0700 Subject: [PATCH 34/38] [1.4.0] Swift 5.8 is not supported on this branch --- .swiftci/5_8_ubuntu2204 | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .swiftci/5_8_ubuntu2204 diff --git a/.swiftci/5_8_ubuntu2204 b/.swiftci/5_8_ubuntu2204 deleted file mode 100644 index 2c259fa..0000000 --- a/.swiftci/5_8_ubuntu2204 +++ /dev/null @@ -1,5 +0,0 @@ -LinuxSwiftPackageJob { - swift_version_tag = "5.8-jammy" - repo = "swift-system" - branch = "release/1.4.0" -} From 11d481252db8972e3921a00fa03633effd3388bb Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 17 Oct 2024 13:17:08 -0700 Subject: [PATCH 35/38] [1.4.0] Updates to the README file --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 452ad86..30b60f7 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,15 @@ let package = Package( ## Source Stability -The Swift System package is source stable. The version numbers follow [Semantic Versioning][semver] -- source breaking changes to public API can only land in a new major version. +The Swift System package supports three types of operating systems: Darwin, POSIX, and Windows. The source-stability status of the package differs according to the platform: + +| Platform type | Stability | +| ----------------- | --------------- | +| Darwin | Source stable | +| POSIX | Source stable | +| Windows | Source unstable | + +The package version numbers follow [Semantic Versioning][semver] -- source breaking changes to public API can only land in a new major version. However, platforms for which support has not reached source stability can have source-breaking changes in a new minor version. [semver]: https://semver.org @@ -79,7 +87,7 @@ The following table maps existing package releases to their minimum required Swi | Package version | Swift version | Xcode release | | ----------------------- | --------------- | ------------- | | swift-system 1.3.x | >= Swift 5.8 | >= Xcode 14.3 | -| swift-system 1.4.x (unreleased) | >= Swift 5.9 | >= Xcode 15.0 | +| swift-system 1.4.x | >= Swift 5.9 | >= Xcode 15.0 | We'd like this package to quickly embrace Swift language and toolchain improvements that are relevant to its mandate. Accordingly, from time to time, new versions of this package require clients to upgrade to a more recent Swift toolchain release. (This allows the package to make use of new language/stdlib features, build on compiler bug fixes, and adopt new package manager functionality as soon as they are available.) Patch (i.e., bugfix) releases will not increase the required toolchain version, but any minor (i.e., new feature) release may do so. From 3f4e7cb45c350319cd1d26668f15f808bcd80736 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 17 Oct 2024 14:27:41 -0700 Subject: [PATCH 36/38] =?UTF-8?q?[windows]=20expose=20pipe=E2=80=99s=20buf?= =?UTF-8?q?fer=20size=20parameter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/System/Internals/WindowsSyscallAdapters.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index d470bd1..d56d33e 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -184,8 +184,10 @@ internal func pwrite( } @inline(__always) -internal func pipe(_ fds: UnsafeMutablePointer) -> CInt { - return _pipe(fds, 4096, _O_BINARY | _O_NOINHERIT); +internal func pipe( + _ fds: UnsafeMutablePointer, bytesReserved: UInt32 = 4096 +) -> CInt { +  return _pipe(fds, bytesReserved, _O_BINARY | _O_NOINHERIT); } @inline(__always) From eeeb81d0cda13798cefcd51348f08a32c233284e Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 17 Oct 2024 15:14:38 -0700 Subject: [PATCH 37/38] [readme] update usage example, reword stability --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 30b60f7..b54e735 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ To use the `SystemPackage` library in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file: ```swift -.package(url: "https://github.com/apple/swift-system", from: "1.3.0"), +.package(url: "https://github.com/apple/swift-system", from: "1.4.0"), ``` Finally, include `"SystemPackage"` as a dependency for your executable target: @@ -41,7 +41,7 @@ Finally, include `"SystemPackage"` as a dependency for your executable target: let package = Package( // name, platforms, products, etc. dependencies: [ - .package(url: "https://github.com/apple/swift-system", from: "1.3.0"), + .package(url: "https://github.com/apple/swift-system", from: "1.4.0"), // other dependencies ], targets: [ @@ -55,15 +55,15 @@ let package = Package( ## Source Stability -The Swift System package supports three types of operating systems: Darwin, POSIX, and Windows. The source-stability status of the package differs according to the platform: +The Swift System package supports three types of operating systems: Darwin-based, POSIX-like, and Windows. The source-stability status of the package differs according to the platform: -| Platform type | Stability | +| Platform type | Source Stability | | ----------------- | --------------- | -| Darwin | Source stable | -| POSIX | Source stable | -| Windows | Source unstable | +| Darwin (macOS, iOS, etc.) | Stable | +| POSIX (Linux, WASI, etc.) | Stable | +| Windows | Unstable | -The package version numbers follow [Semantic Versioning][semver] -- source breaking changes to public API can only land in a new major version. However, platforms for which support has not reached source stability can have source-breaking changes in a new minor version. +The package version numbers follow [Semantic Versioning][semver] -- source breaking changes to source-stable public API can only land in a new major version. However, platforms for which support has not reached source stability may see source-breaking changes in a new minor version. [semver]: https://semver.org From b80d743dabbf04af5a3c186f1397434356846977 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 17 Oct 2024 15:52:41 -0700 Subject: [PATCH 38/38] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b54e735..8fa266b 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ let package = Package( ## Source Stability -The Swift System package supports three types of operating systems: Darwin-based, POSIX-like, and Windows. The source-stability status of the package differs according to the platform: +At this time, the Swift System package supports three types of operating systems: Darwin-based, POSIX-like, and Windows. The source-stability status of the package differs according to the platform: | Platform type | Source Stability | | ----------------- | --------------- |