diff --git a/Package.swift b/Package.swift index 7f774e6..2c75bb0 100644 --- a/Package.swift +++ b/Package.swift @@ -59,13 +59,9 @@ let availability: [Available] = [ Available("1.3.2", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4, visionOS 1.0"), Available("1.4.0", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4, visionOS 1.0"), - Available("1.4.1", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"), - Available("1.4.2", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"), - Available("1.5.0", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"), - Available("1.6.0", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"), - Available("1.6.1", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"), + Available("1.7.0", "macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0"), - Available("99", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"), + Available("199", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"), ] let swiftSettingsAvailability = availability.map(\.swiftSetting) diff --git a/Proposals/0006-system-stat.md b/Proposals/0006-system-stat.md index 6214b76..4b1c2ec 100644 --- a/Proposals/0006-system-stat.md +++ b/Proposals/0006-system-stat.md @@ -79,7 +79,7 @@ These initializers use a typed `throws(Errno)` and require Swift 6.0 or later. See the **Appendix** section at the end of this proposal for a table view of Swift API to C mappings. -All API are marked `@_alwaysEmitIntoClient` for performance and back-dating of availability. +All API are marked `@_alwaysEmitIntoClient` for performance and back-dating of availability, except `Stat`'s `==` and `hash(into:)`, which are ordinary `public` API. ### FileType @@ -693,24 +693,27 @@ public struct Stat: RawRepresentable, Sendable { /// File generation number /// - /// The file generation number is used to distinguish between different files - /// that have used the same inode over time. + /// The file generation number may be used to distinguish between different + /// files that have used the same inode over time. /// /// The corresponding C property is `st_gen`. - /// - Note: Only available on Darwin, FreeBSD, and OpenBSD. - public var generationNumber: Int { get set } + /// - Note: Only available on Darwin, FreeBSD, and OpenBSD. The underlying C + /// field is 32-bit on Darwin and OpenBSD, and 64-bit on FreeBSD. + public var generationNumber: UInt64 { get set } #endif } // MARK: - Equatable and Hashable extension Stat: Equatable { - /// Compares the raw bytes of two `Stat` structs for equality. + /// Compares the meaningful file-metadata fields of two `Stat` values. + /// + /// Alignment padding and platform reserved/"spare" fields are not compared. public static func == (lhs: Self, rhs: Self) -> Bool } extension Stat: Hashable { - /// Hashes the raw bytes of this `Stat` struct. + /// Hashes the meaningful file-metadata fields of a `Stat` struct. public func hash(into hasher: inout Hasher) } ``` diff --git a/Sources/System/CMakeLists.txt b/Sources/System/CMakeLists.txt index 7816f1a..a904eb1 100644 --- a/Sources/System/CMakeLists.txt +++ b/Sources/System/CMakeLists.txt @@ -24,7 +24,8 @@ add_library(SystemPackage set_target_properties(SystemPackage PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) target_compile_options(SystemPackage PRIVATE - -enable-experimental-feature Lifetimes) + -enable-experimental-feature Lifetimes + "SHELL:-package-name swift-system") target_sources(SystemPackage PRIVATE FilePath/FilePath.swift FilePath/FilePathComponents.swift diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index bed123f..2c8deb9 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -230,7 +230,7 @@ extension FileDescriptor { internal func _read( into buffer: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool - ) throws -> Result { + ) -> Result { valueOrErrno(retryOnInterrupt: retryOnInterrupt) { system_read(self.rawValue, buffer.baseAddress, buffer.count) } diff --git a/Sources/System/FileSystem/FileFlags.swift b/Sources/System/FileSystem/FileFlags.swift index 5a905ed..15084b1 100644 --- a/Sources/System/FileSystem/FileFlags.swift +++ b/Sources/System/FileSystem/FileFlags.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift System open source project // -// Copyright (c) 2025 Apple Inc. and the Swift System project authors +// Copyright (c) 2025 - 2026 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 @@ -45,7 +45,7 @@ /// /// - Note: Only available on Darwin, FreeBSD, and OpenBSD. @frozen -@available(System 99, *) +@available(System 1.7.0, *) public struct FileFlags: OptionSet, Sendable, Hashable, Codable { /// The raw C flags. diff --git a/Sources/System/FileSystem/FileMode.swift b/Sources/System/FileSystem/FileMode.swift index 91329f1..0bbeb56 100644 --- a/Sources/System/FileSystem/FileMode.swift +++ b/Sources/System/FileSystem/FileMode.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift System open source project // -// Copyright (c) 2025 Apple Inc. and the Swift System project authors +// Copyright (c) 2025 - 2026 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 @@ -14,7 +14,7 @@ /// /// - Note: Only available on Unix-like platforms. @frozen -@available(System 99, *) +@available(System 1.7.0, *) public struct FileMode: RawRepresentable, Sendable, Hashable, Codable { /// The raw C mode. diff --git a/Sources/System/FileSystem/FileType.swift b/Sources/System/FileSystem/FileType.swift index 4213452..9a365a4 100644 --- a/Sources/System/FileSystem/FileType.swift +++ b/Sources/System/FileSystem/FileType.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift System open source project // -// Copyright (c) 2025 Apple Inc. and the Swift System project authors +// Copyright (c) 2025 - 2026 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 @@ -34,7 +34,7 @@ /// /// - Note: Only available on Unix-like platforms. @frozen -@available(System 99, *) +@available(System 1.7.0, *) public struct FileType: RawRepresentable, Sendable, Hashable, Codable { /// The raw file-type bits from the C mode. diff --git a/Sources/System/FileSystem/Identifiers.swift b/Sources/System/FileSystem/Identifiers.swift index b8f9014..dda606e 100644 --- a/Sources/System/FileSystem/Identifiers.swift +++ b/Sources/System/FileSystem/Identifiers.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift System open source project // -// Copyright (c) 2025 Apple Inc. and the Swift System project authors +// Copyright (c) 2025 - 2026 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 @@ -12,7 +12,7 @@ #if !os(Windows) /// A Swift wrapper of the C `uid_t` type. @frozen -@available(System 99, *) +@available(System 1.7.0, *) public struct UserID: RawRepresentable, Sendable, Hashable, Codable { /// The raw C `uid_t`. @@ -30,7 +30,7 @@ public struct UserID: RawRepresentable, Sendable, Hashable, Codable { /// A Swift wrapper of the C `gid_t` type. @frozen -@available(System 99, *) +@available(System 1.7.0, *) public struct GroupID: RawRepresentable, Sendable, Hashable, Codable { /// The raw C `gid_t`. @@ -48,7 +48,7 @@ public struct GroupID: RawRepresentable, Sendable, Hashable, Codable { /// A Swift wrapper of the C `dev_t` type. @frozen -@available(System 99, *) +@available(System 1.7.0, *) public struct DeviceID: RawRepresentable, Sendable, Hashable, Codable { /// The raw C `dev_t`. @@ -66,7 +66,7 @@ public struct DeviceID: RawRepresentable, Sendable, Hashable, Codable { /// A Swift wrapper of the C `ino_t` type. @frozen -@available(System 99, *) +@available(System 1.7.0, *) public struct Inode: RawRepresentable, Sendable, Hashable, Codable { /// The raw C `ino_t`. diff --git a/Sources/System/FileSystem/Stat.swift b/Sources/System/FileSystem/Stat.swift index e30e2b1..ce4b6b5 100644 --- a/Sources/System/FileSystem/Stat.swift +++ b/Sources/System/FileSystem/Stat.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift System open source project // -// Copyright (c) 2025 Apple Inc. and the Swift System project authors +// Copyright (c) 2025 - 2026 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 @@ -35,7 +35,7 @@ import Android /// /// - Note: Only available on Unix-like platforms. @frozen -@available(System 99, *) +@available(System 1.7.0, *) public struct Stat: RawRepresentable, Sendable { /// The raw C `stat` struct. @@ -521,42 +521,93 @@ public struct Stat: RawRepresentable, Sendable { /// File generation number /// - /// The file generation number is used to distinguish between different files - /// that have used the same inode over time. + /// The file generation number may be used to distinguish between different + /// files that have used the same inode over time. /// /// The corresponding C property is `st_gen`. - /// - Note: Only available on Darwin, FreeBSD, and OpenBSD. + /// - Note: Only available on Darwin, FreeBSD, and OpenBSD. The underlying C + /// field is 32-bit on Darwin and OpenBSD, and 64-bit on FreeBSD. @_alwaysEmitIntoClient - public var generationNumber: Int { - get { Int(rawValue.st_gen) } - set { rawValue.st_gen = numericCast(newValue)} + public var generationNumber: UInt64 { + get { UInt64(rawValue.st_gen) } + set { rawValue.st_gen = numericCast(newValue) } } #endif } // MARK: - Equatable and Hashable -@available(System 99, *) +@available(System 1.7.0, *) extension Stat: Equatable { - @_alwaysEmitIntoClient - /// Compares the raw bytes of two `Stat` structs for equality. + /// Compares the meaningful file-metadata fields of two `Stat` values. + /// + /// Alignment padding and platform reserved/"spare" fields are not compared. public static func == (lhs: Self, rhs: Self) -> Bool { - return withUnsafeBytes(of: lhs.rawValue) { lhsBytes in - withUnsafeBytes(of: rhs.rawValue) { rhsBytes in - lhsBytes.elementsEqual(rhsBytes) - } + guard lhs.rawValue.st_dev == rhs.rawValue.st_dev, + lhs.rawValue.st_ino == rhs.rawValue.st_ino, + lhs.rawValue.st_mode == rhs.rawValue.st_mode, + lhs.rawValue.st_nlink == rhs.rawValue.st_nlink, + lhs.rawValue.st_uid == rhs.rawValue.st_uid, + lhs.rawValue.st_gid == rhs.rawValue.st_gid, + lhs.rawValue.st_rdev == rhs.rawValue.st_rdev, + lhs.rawValue.st_size == rhs.rawValue.st_size, + lhs.rawValue.st_blksize == rhs.rawValue.st_blksize, + lhs.rawValue.st_blocks == rhs.rawValue.st_blocks, + lhs.st_atim.tv_sec == rhs.st_atim.tv_sec, + lhs.st_atim.tv_nsec == rhs.st_atim.tv_nsec, + lhs.st_mtim.tv_sec == rhs.st_mtim.tv_sec, + lhs.st_mtim.tv_nsec == rhs.st_mtim.tv_nsec, + lhs.st_ctim.tv_sec == rhs.st_ctim.tv_sec, + lhs.st_ctim.tv_nsec == rhs.st_ctim.tv_nsec else { + return false } + #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) + guard lhs.st_birthtim.tv_sec == rhs.st_birthtim.tv_sec, + lhs.st_birthtim.tv_nsec == rhs.st_birthtim.tv_nsec else { + return false + } + #endif + #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD) + guard lhs.rawValue.st_flags == rhs.rawValue.st_flags, + lhs.rawValue.st_gen == rhs.rawValue.st_gen else { + return false + } + #endif + return true } } -@available(System 99, *) +@available(System 1.7.0, *) extension Stat: Hashable { - @_alwaysEmitIntoClient - /// Hashes the raw bytes of this `Stat` struct. + /// Hashes the meaningful file-metadata fields of a `Stat` struct. + /// + /// These are the same fields compared by `==`, fed in the same order. + /// Alignment padding and platform reserved/"spare" fields are not hashed. public func hash(into hasher: inout Hasher) { - withUnsafeBytes(of: rawValue) { bytes in - hasher.combine(bytes: bytes) - } + hasher.combine(rawValue.st_dev) + hasher.combine(rawValue.st_ino) + hasher.combine(rawValue.st_mode) + hasher.combine(rawValue.st_nlink) + hasher.combine(rawValue.st_uid) + hasher.combine(rawValue.st_gid) + hasher.combine(rawValue.st_rdev) + hasher.combine(rawValue.st_size) + hasher.combine(rawValue.st_blksize) + hasher.combine(rawValue.st_blocks) + hasher.combine(st_atim.tv_sec) + hasher.combine(st_atim.tv_nsec) + hasher.combine(st_mtim.tv_sec) + hasher.combine(st_mtim.tv_nsec) + hasher.combine(st_ctim.tv_sec) + hasher.combine(st_ctim.tv_nsec) + #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) + hasher.combine(st_birthtim.tv_sec) + hasher.combine(st_birthtim.tv_nsec) + #endif + #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD) + hasher.combine(rawValue.st_flags) + hasher.combine(rawValue.st_gen) + #endif } } @@ -564,7 +615,7 @@ extension Stat: Hashable { // MARK: - FileDescriptor Extensions -@available(System 99, *) +@available(System 1.7.0, *) extension FileDescriptor { /// Creates a `Stat` struct for the file referenced by this `FileDescriptor`. @@ -580,7 +631,7 @@ extension FileDescriptor { // MARK: - FilePath Extensions -@available(System 99, *) +@available(System 1.7.0, *) extension FilePath { /// Creates a `Stat` struct for the file referenced by this `FilePath`. diff --git a/Sources/System/Internals/CInterop.swift b/Sources/System/Internals/CInterop.swift index 3a31660..e6af1b2 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 - 2024 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2026 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 @@ -106,7 +106,7 @@ extension CInterop { } } -@available(System 99, *) +@available(System 1.7.0, *) extension CInterop { public typealias DeviceID = dev_t public typealias Inode = ino_t diff --git a/Sources/System/Internals/Exports.swift b/Sources/System/Internals/Exports.swift index 57e5c13..aa3506e 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 - 2024 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2026 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 @@ -96,15 +96,15 @@ internal func system_strlen(_ s: UnsafeMutablePointer) -> Int { internal func system_stat(_ p: UnsafePointer, _ s: inout CInterop.Stat) -> Int32 { stat(p, &s) } -@available(System 99, *) +@available(System 1.7.0, *) internal func system_lstat(_ p: UnsafePointer, _ s: inout CInterop.Stat) -> Int32 { lstat(p, &s) } -@available(System 99, *) +@available(System 1.7.0, *) internal func system_fstat(_ fd: CInt, _ s: inout CInterop.Stat) -> Int32 { fstat(fd, &s) } -@available(System 99, *) +@available(System 1.7.0, *) internal func system_fstatat(_ fd: CInt, _ p: UnsafePointer, _ s: inout CInterop.Stat, _ flags: CInt) -> Int32 { fstatat(fd, p, &s, flags) } diff --git a/Tests/SystemTests/FileModeTests.swift b/Tests/SystemTests/FileModeTests.swift index 46c3a5d..d24fd09 100644 --- a/Tests/SystemTests/FileModeTests.swift +++ b/Tests/SystemTests/FileModeTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift System open source project // -// Copyright (c) 2025 Apple Inc. and the Swift System project authors +// Copyright (c) 2025 - 2026 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 @@ -36,7 +36,7 @@ import Android @Suite("FileMode") private struct FileModeTests { - @available(System 99, *) + @available(System 1.7.0, *) @Test func basics() async throws { var mode = FileMode(rawValue: S_IFREG | 0o644) // Regular file, rw-r--r-- #expect(mode.type == .regular) @@ -67,7 +67,7 @@ private struct FileModeTests { #expect(mode.type == mode2.type) } - @available(System 99, *) + @available(System 1.7.0, *) @Test func invalidInput() async throws { // No permissions, all other bits set var invalidMode = FileMode(rawValue: ~0o7777) diff --git a/Tests/SystemTests/StatTests.swift b/Tests/SystemTests/StatTests.swift index e80ede1..80062e2 100644 --- a/Tests/SystemTests/StatTests.swift +++ b/Tests/SystemTests/StatTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift System open source project // -// Copyright (c) 2025 Apple Inc. and the Swift System project authors +// Copyright (c) 2025 - 2026 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 @@ -39,7 +39,7 @@ import Android @Suite("Stat") private struct StatTests { - @available(System 99, *) + @available(System 1.7.0, *) @Test func basics() async throws { try withTemporaryFilePath(basename: "Stat_basics") { tempDir in let dirStatFromFilePath = try tempDir.stat() @@ -81,7 +81,7 @@ private struct StatTests { } } - @available(System 99, *) + @available(System 1.7.0, *) @Test func followSymlinkInits() async throws { try withTemporaryFilePath(basename: "Stat_followSymlinkInits") { tempDir in let targetFilePath = tempDir.appending("target.txt") @@ -190,7 +190,7 @@ private struct StatTests { } } - @available(System 99, *) + @available(System 1.7.0, *) @Test func permissions() async throws { try withTemporaryFilePath(basename: "Stat_permissions") { tempDir in let testFile = tempDir.appending("test.txt") @@ -220,7 +220,7 @@ private struct StatTests { } } - @available(System 99, *) + @available(System 1.7.0, *) @Test func times() async throws { var start = timespec() try #require(clock_gettime(CLOCK_REALTIME, &start) == 0, "\(Errno.current)") @@ -349,7 +349,7 @@ private struct StatTests { } #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD) - @available(System 99, *) + @available(System 1.7.0, *) @Test func flags() async throws { try withTemporaryFilePath(basename: "Stat_flags") { tempDir in let filePath = tempDir.appending("test.txt") @@ -397,6 +397,38 @@ private struct StatTests { } #endif + @available(System 1.7.0, *) + @Test func equalityAndHashing() async throws { + try withTemporaryFilePath(basename: "Stat_equality") { tempDir in + let file = tempDir.appending("test.txt") + let fd = try FileDescriptor.open( + file, .writeOnly, options: .create, permissions: .ownerReadWrite) + defer { try? fd.close() } + + // Two stats of the same, unchanged file compare equal and hash equal, + // so they coalesce in a Set. + let stat1 = try file.stat() + var stat2 = try file.stat() + stat2.st_atim = stat1.st_atim // stat() can bump access time + #expect(stat1 == stat2) + #expect(stat1.hashValue == stat2.hashValue) + #expect(Set([stat1, stat2]).count == 1) + + // Reserved/"spare" and padding bytes must not affect equality. + // Poke a reserved field and confirm equality/hashing are stable. + #if SYSTEM_PACKAGE_DARWIN + var stat3 = stat1 + stat3.rawValue.st_lspare = stat1.rawValue.st_lspare &+ 1 + #expect(stat1 == stat3) + #expect(stat1.hashValue == stat3.hashValue) + #endif + + // A stat of a different file (the containing directory) is not equal. + let dirStat = try tempDir.stat() + #expect(stat1 != dirStat) + } + } + } // Comparison operators for timespec until UTCClock.Instant properties are available