mirror of
https://github.com/apple/swift-system
synced 2026-11-23 04:34:26 +00:00
Compare commits
81
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ff423fe26 | ||
|
|
7661e95acf | ||
|
|
3b62b88701 | ||
|
|
3536697660 | ||
|
|
ffc90f58de | ||
|
|
55d9da0dcd | ||
|
|
539134c24b | ||
|
|
a74751c99c | ||
|
|
1cbb2cbcc6 | ||
|
|
7bcc01f0bf | ||
|
|
6bbed6b199 | ||
|
|
2d6c6a82b3 | ||
|
|
5b1b4cab56 | ||
|
|
7c6609eaca | ||
|
|
aabb8d0019 | ||
|
|
f059b4bfb3 | ||
|
|
64486e7d26 | ||
|
|
9674a9fee8 | ||
|
|
bb6ab43e44 | ||
|
|
e86702b34a | ||
|
|
cee9335aeb | ||
|
|
f572e4becb | ||
|
|
e708e4adda | ||
|
|
cc96d40bee | ||
|
|
9cdaaa356e | ||
|
|
16c7885387 | ||
|
|
08e9abe4e6 | ||
|
|
0fb71dd6ad | ||
|
|
16a2775645 | ||
|
|
7c11f19259 | ||
|
|
13bf719c29 | ||
|
|
3750b96b2e | ||
|
|
12baba9706 | ||
|
|
2c2a831f66 | ||
|
|
deb56e1ad2 | ||
|
|
a2bf0d5708 | ||
|
|
b5e330163a | ||
|
|
3f66dfbaf8 | ||
|
|
ccea4eed1a | ||
|
|
700b8f867e | ||
|
|
d129f62ce9 | ||
|
|
ae6b1f24a9 | ||
|
|
47feb93a38 | ||
|
|
6a9ebce259 | ||
|
|
7f4560f022 | ||
|
|
6dd57fe3a9 | ||
|
|
551d7b897f | ||
|
|
296374ecef | ||
|
|
213165ab72 | ||
|
|
bd036acda6 | ||
|
|
4536c47763 | ||
|
|
bdc776bd3e | ||
|
|
c2d940ecf3 | ||
|
|
304ccc3abb | ||
|
|
77d9ca18c1 | ||
|
|
18cceda484 | ||
|
|
252c0f0905 | ||
|
|
e1f6974ba6 | ||
|
|
015e9baf40 | ||
|
|
036f567f23 | ||
|
|
348b4de820 | ||
|
|
e378fb2f0d | ||
|
|
e9886bf251 | ||
|
|
63cdebeff6 | ||
|
|
97a03f97c8 | ||
|
|
0a7e8e121b | ||
|
|
a102156af3 | ||
|
|
dc5a3107c0 | ||
|
|
bcba321273 | ||
|
|
a5c43b17a9 | ||
|
|
429f64259f | ||
|
|
18c5f7a54b | ||
|
|
f9a514897e | ||
|
|
8e247d927e | ||
|
|
59ddf00c8e | ||
|
|
6d8abddbc8 | ||
|
|
ca700460b1 | ||
|
|
81d78be499 | ||
|
|
915070e006 | ||
|
|
6469433909 | ||
|
|
c0e8ef346b |
No files matched your search
+3
-1
@@ -1,6 +1,8 @@
|
||||
.DS_Store
|
||||
/.swiftpm
|
||||
/.build
|
||||
/Packages
|
||||
/*.xcodeproj
|
||||
swift-system.xcodeproj
|
||||
xcuserdata/
|
||||
.*.sw?
|
||||
.docc-build
|
||||
@@ -13,9 +13,11 @@ add_library(SystemPackage
|
||||
FileHelpers.swift
|
||||
FileOperations.swift
|
||||
FilePermissions.swift
|
||||
MachPort.swift
|
||||
PlatformString.swift
|
||||
SystemString.swift
|
||||
Util.swift
|
||||
Util+StringArray.swift
|
||||
UtilConsumers.swift)
|
||||
set_target_properties(SystemPackage PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
@@ -28,10 +30,12 @@ target_sources(SystemPackage PRIVATE
|
||||
FilePath/FilePathSyntax.swift
|
||||
FilePath/FilePathWindows.swift)
|
||||
target_sources(SystemPackage PRIVATE
|
||||
Internals/Backcompat.swift
|
||||
Internals/CInterop.swift
|
||||
Internals/Constants.swift
|
||||
Internals/Exports.swift
|
||||
Internals/Mocking.swift
|
||||
Internals/RawBuffer.swift
|
||||
Internals/Syscalls.swift
|
||||
Internals/WindowsSyscallAdapters.swift)
|
||||
target_link_libraries(SystemPackage PUBLIC
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
/// An error number used by system calls to communicate what kind of error
|
||||
/// occurred.
|
||||
@frozen
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
public struct Errno: RawRepresentable, Error, Hashable, Codable {
|
||||
/// The raw C error number.
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -1274,6 +1274,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
|
||||
@available(*, unavailable, renamed: "badMessage")
|
||||
public static var EBADMSG: Errno { badMessage }
|
||||
|
||||
#if !os(OpenBSD)
|
||||
/// Reserved.
|
||||
///
|
||||
/// This error is reserved for future use.
|
||||
@@ -1333,6 +1334,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "notStream")
|
||||
public static var ENOSTR: Errno { notStream }
|
||||
#endif
|
||||
|
||||
/// Protocol error.
|
||||
///
|
||||
@@ -1348,6 +1350,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
|
||||
@available(*, unavailable, renamed: "protocolError")
|
||||
public static var EPROTO: Errno { protocolError }
|
||||
|
||||
#if !os(OpenBSD)
|
||||
/// Reserved.
|
||||
///
|
||||
/// This error is reserved for future use.
|
||||
@@ -1359,6 +1362,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "timeout")
|
||||
public static var ETIME: Errno { timeout }
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/// Operation not supported on socket.
|
||||
@@ -1376,7 +1380,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
|
||||
}
|
||||
|
||||
// Constants defined in header but not man page
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension Errno {
|
||||
|
||||
/// Operation would block.
|
||||
@@ -1470,7 +1474,7 @@ extension Errno {
|
||||
#endif
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension Errno {
|
||||
// TODO: We want to provide safe access to `errno`, but we need a
|
||||
// release-barrier to do so.
|
||||
@@ -1485,14 +1489,14 @@ extension Errno {
|
||||
}
|
||||
|
||||
// Use "hidden" entry points for `NSError` bridging
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension Errno {
|
||||
public var _code: Int { Int(rawValue) }
|
||||
|
||||
public var _domain: String { "NSPOSIXErrorDomain" }
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension Errno: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
/// A textual representation of the most recent error
|
||||
/// returned by a system call.
|
||||
@@ -1512,7 +1516,7 @@ extension Errno: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
public var debugDescription: String { self.description }
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension Errno {
|
||||
@_alwaysEmitIntoClient
|
||||
public static func ~=(_ lhs: Errno, _ rhs: Error) -> Bool {
|
||||
|
||||
+18
-6
@@ -14,7 +14,7 @@
|
||||
/// of `FileDescriptor` values,
|
||||
/// in the same way as you manage a raw C file handle.
|
||||
@frozen
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
public struct FileDescriptor: RawRepresentable, Hashable, Codable {
|
||||
/// The raw C file handle.
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -25,7 +25,8 @@ public struct FileDescriptor: RawRepresentable, Hashable, Codable {
|
||||
public init(rawValue: CInt) { self.rawValue = rawValue }
|
||||
}
|
||||
|
||||
// Standard file descriptors
|
||||
// Standard file descriptors.
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FileDescriptor {
|
||||
/// The standard input file descriptor, with a numeric value of 0.
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -40,7 +41,7 @@ extension FileDescriptor {
|
||||
public static var standardError: FileDescriptor { .init(rawValue: 2) }
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FileDescriptor {
|
||||
/// The desired read and write access for a newly opened file.
|
||||
@frozen
|
||||
@@ -385,7 +386,7 @@ extension FileDescriptor {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FileDescriptor.AccessMode
|
||||
: CustomStringConvertible, CustomDebugStringConvertible
|
||||
{
|
||||
@@ -404,7 +405,7 @@ extension FileDescriptor.AccessMode
|
||||
public var debugDescription: String { self.description }
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FileDescriptor.SeekOrigin
|
||||
: CustomStringConvertible, CustomDebugStringConvertible
|
||||
{
|
||||
@@ -427,7 +428,7 @@ extension FileDescriptor.SeekOrigin
|
||||
public var debugDescription: String { self.description }
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FileDescriptor.OpenOptions
|
||||
: CustomStringConvertible, CustomDebugStringConvertible
|
||||
{
|
||||
@@ -473,3 +474,14 @@ extension FileDescriptor.OpenOptions
|
||||
/// A textual representation of the open options, suitable for debugging.
|
||||
public var debugDescription: String { self.description }
|
||||
}
|
||||
|
||||
#if compiler(>=5.5) && canImport(_Concurrency)
|
||||
// The decision on whether to make FileDescriptor Sendable or not
|
||||
// is currently being discussed in https://github.com/apple/swift-system/pull/112
|
||||
//@available(*, unavailable, message: "File descriptors are not completely thread-safe.")
|
||||
//extension FileDescriptor: Sendable {}
|
||||
|
||||
extension FileDescriptor.AccessMode: Sendable {}
|
||||
extension FileDescriptor.OpenOptions: Sendable {}
|
||||
extension FileDescriptor.SeekOrigin: Sendable {}
|
||||
#endif
|
||||
@@ -7,7 +7,7 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FileDescriptor {
|
||||
/// Runs a closure and then closes the file descriptor, even if an error occurs.
|
||||
///
|
||||
+75
-13
@@ -7,7 +7,7 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FileDescriptor {
|
||||
/// Opens or creates a file for reading or writing.
|
||||
///
|
||||
@@ -31,12 +31,70 @@ extension FileDescriptor {
|
||||
permissions: FilePermissions? = nil,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> FileDescriptor {
|
||||
try path.withPlatformString {
|
||||
#if !os(Windows)
|
||||
return try path.withCString {
|
||||
try FileDescriptor.open(
|
||||
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
|
||||
}
|
||||
#else
|
||||
return try path.withPlatformString {
|
||||
try FileDescriptor.open(
|
||||
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !os(Windows)
|
||||
// On Darwin, `CInterop.PlatformChar` is less available than
|
||||
// `FileDescriptor.open`, so we need to use `CChar` instead.
|
||||
|
||||
/// Opens or creates a file for reading or writing.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - path: The location of the file to open.
|
||||
/// - mode: The read and write access to use.
|
||||
/// - options: The behavior for opening the file.
|
||||
/// - permissions: The file permissions to use for created files.
|
||||
/// - retryOnInterrupt: Whether to retry the open operation
|
||||
/// if it throws ``Errno/interrupted``.
|
||||
/// The default is `true`.
|
||||
/// Pass `false` to try only once and throw an error upon interruption.
|
||||
/// - Returns: A file descriptor for the open file
|
||||
///
|
||||
/// The corresponding C function is `open`.
|
||||
@_alwaysEmitIntoClient
|
||||
public static func open(
|
||||
_ path: UnsafePointer<CChar>,
|
||||
_ mode: FileDescriptor.AccessMode,
|
||||
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
|
||||
permissions: FilePermissions? = nil,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> FileDescriptor {
|
||||
try FileDescriptor._open(
|
||||
path, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal static func _open(
|
||||
_ path: UnsafePointer<CChar>,
|
||||
_ mode: FileDescriptor.AccessMode,
|
||||
options: FileDescriptor.OpenOptions,
|
||||
permissions: FilePermissions?,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<FileDescriptor, Errno> {
|
||||
let oFlag = mode.rawValue | options.rawValue
|
||||
let descOrError: Result<CInt, Errno> = valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
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) }
|
||||
}
|
||||
#else
|
||||
/// Opens or creates a file for reading or writing.
|
||||
///
|
||||
/// - Parameters:
|
||||
@@ -83,6 +141,7 @@ extension FileDescriptor {
|
||||
}
|
||||
return descOrError.map { FileDescriptor(rawValue: $0) }
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Deletes a file descriptor.
|
||||
///
|
||||
@@ -144,7 +203,7 @@ extension FileDescriptor {
|
||||
/// Pass `false` to try only once and throw an error upon interruption.
|
||||
/// - Returns: The number of bytes that were read.
|
||||
///
|
||||
/// The <doc://com.apple.documentation/documentation/swift/unsafemutablerawbufferpointer/3019191-count> property of `buffer`
|
||||
/// The <doc://com.apple.documentation/documentation/swift/unsafemutablerawbufferpointer/count-95usp> property of `buffer`
|
||||
/// determines the maximum number of bytes that are read into that buffer.
|
||||
///
|
||||
/// After reading,
|
||||
@@ -182,7 +241,7 @@ extension FileDescriptor {
|
||||
/// Pass `false` to try only once and throw an error upon interruption.
|
||||
/// - Returns: The number of bytes that were read.
|
||||
///
|
||||
/// The <doc://com.apple.documentation/documentation/swift/unsafemutablerawbufferpointer/3019191-count> property of `buffer`
|
||||
/// The <doc://com.apple.documentation/documentation/swift/unsafemutablerawbufferpointer/count-95usp> property of `buffer`
|
||||
/// determines the maximum number of bytes that are read into that buffer.
|
||||
///
|
||||
/// Unlike <doc:FileDescriptor/read(into:retryOnInterrupt:)>,
|
||||
@@ -308,7 +367,10 @@ extension FileDescriptor {
|
||||
buffer,
|
||||
retryOnInterrupt: retryOnInterrupt)
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FileDescriptor {
|
||||
/// Duplicate this file descriptor and return the newly created copy.
|
||||
///
|
||||
/// - Parameters:
|
||||
@@ -337,7 +399,7 @@ extension FileDescriptor {
|
||||
///
|
||||
/// The corresponding C functions are `dup` and `dup2`.
|
||||
@_alwaysEmitIntoClient
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
public func duplicate(
|
||||
as target: FileDescriptor? = nil,
|
||||
retryOnInterrupt: Bool = true
|
||||
@@ -345,7 +407,7 @@ extension FileDescriptor {
|
||||
try _duplicate(as: target, retryOnInterrupt: retryOnInterrupt).get()
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
@usableFromInline
|
||||
internal func _duplicate(
|
||||
as target: FileDescriptor?,
|
||||
@@ -373,7 +435,7 @@ extension FileDescriptor {
|
||||
}
|
||||
|
||||
#if !os(Windows)
|
||||
/*System 1.1.0, @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)*/
|
||||
@available(/*System 1.1.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *)
|
||||
extension FileDescriptor {
|
||||
/// Create a pipe, a unidirectional data channel which can be used for interprocess communication.
|
||||
///
|
||||
@@ -381,12 +443,12 @@ extension FileDescriptor {
|
||||
///
|
||||
/// The corresponding C function is `pipe`.
|
||||
@_alwaysEmitIntoClient
|
||||
/*System 1.1.0, @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)*/
|
||||
@available(/*System 1.1.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *)
|
||||
public static func pipe() throws -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) {
|
||||
try _pipe().get()
|
||||
}
|
||||
|
||||
/*System 1.1.0, @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)*/
|
||||
|
||||
@available(/*System 1.1.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *)
|
||||
@usableFromInline
|
||||
internal static func _pipe() -> Result<(readEnd: FileDescriptor, writeEnd: FileDescriptor), Errno> {
|
||||
var fds: (Int32, Int32) = (-1, -1)
|
||||
@@ -402,7 +464,7 @@ extension FileDescriptor {
|
||||
#endif
|
||||
|
||||
#if !os(Windows)
|
||||
/*System 1.2.0, @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)*/
|
||||
@available(/*System 1.2.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *)
|
||||
extension FileDescriptor {
|
||||
/// Truncate or extend the file referenced by this file descriptor.
|
||||
///
|
||||
@@ -424,7 +486,7 @@ extension FileDescriptor {
|
||||
/// associated with the file.
|
||||
///
|
||||
/// The corresponding C function is `ftruncate`.
|
||||
/*System 1.2.0, @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)*/
|
||||
@available(/*System 1.2.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public func resize(
|
||||
to newSize: Int64,
|
||||
@@ -436,7 +498,7 @@ extension FileDescriptor {
|
||||
).get()
|
||||
}
|
||||
|
||||
/*System 1.2.0, @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)*/
|
||||
@available(/*System 1.2.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *)
|
||||
@usableFromInline
|
||||
internal func _resize(
|
||||
to newSize: Int64,
|
||||
@@ -37,11 +37,10 @@
|
||||
/// However, the rules for path equivalence
|
||||
/// are file-system–specific and have additional considerations
|
||||
/// like case insensitivity, Unicode normalization, and symbolic links.
|
||||
///
|
||||
// TODO(docs): Section on all the new syntactic operations, lexical normalization, decomposition,
|
||||
// components, etc.
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
public struct FilePath {
|
||||
// TODO(docs): Section on all the new syntactic operations, lexical normalization, decomposition,
|
||||
// components, etc.
|
||||
internal var _storage: SystemString
|
||||
|
||||
/// Creates an empty, null-terminated path.
|
||||
@@ -60,12 +59,15 @@ public struct FilePath {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// The length of the file path, excluding the null terminator.
|
||||
public var length: Int { _storage.length }
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath: Hashable, Codable {}
|
||||
|
||||
#if compiler(>=5.5) && canImport(_Concurrency)
|
||||
extension FilePath: Sendable {}
|
||||
#endif
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// MARK: - API
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// A bidirectional, range replaceable collection of the non-root components
|
||||
/// that make up a file path.
|
||||
@@ -28,7 +28,6 @@ extension FilePath {
|
||||
///
|
||||
/// path.components.removeAll { $0.kind == .currentDirectory }
|
||||
/// // path is "/home/username/bin/scripts/tree"
|
||||
///
|
||||
public struct ComponentView {
|
||||
internal var _path: FilePath
|
||||
internal var _start: SystemString.Index
|
||||
@@ -40,7 +39,7 @@ extension FilePath {
|
||||
}
|
||||
}
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
#if SYSTEM_PACKAGE // Workaround for a __consuming getter bug in Swift 5.3.3
|
||||
/// View the non-root components that make up this path.
|
||||
public var components: ComponentView {
|
||||
get { ComponentView(self) }
|
||||
@@ -89,7 +88,7 @@ extension FilePath {
|
||||
#endif
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.ComponentView: BidirectionalCollection {
|
||||
public typealias Element = FilePath.Component
|
||||
public struct Index: Comparable, Hashable {
|
||||
@@ -123,7 +122,7 @@ extension FilePath.ComponentView: BidirectionalCollection {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.ComponentView: RangeReplaceableCollection {
|
||||
public init() {
|
||||
self.init(FilePath())
|
||||
@@ -173,7 +172,7 @@ extension FilePath.ComponentView: RangeReplaceableCollection {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// Create a file path from a root and a collection of components.
|
||||
public init<C: Collection>(
|
||||
@@ -202,6 +201,7 @@ extension FilePath {
|
||||
|
||||
// MARK: - Internals
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.ComponentView: _PathSlice {
|
||||
internal var _range: Range<SystemString.Index> {
|
||||
_start ..< _path._storage.endIndex
|
||||
@@ -214,6 +214,7 @@ extension FilePath.ComponentView: _PathSlice {
|
||||
|
||||
// MARK: - Invariants
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.ComponentView {
|
||||
internal func _invariantCheck() {
|
||||
#if DEBUG
|
||||
@@ -238,3 +239,8 @@ extension FilePath.ComponentView {
|
||||
#endif // DEBUG
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=5.5) && canImport(_Concurrency)
|
||||
extension FilePath.ComponentView: Sendable {}
|
||||
extension FilePath.ComponentView.Index: Sendable {}
|
||||
#endif
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// MARK: - API
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// Represents a root of a file path.
|
||||
///
|
||||
@@ -54,7 +54,6 @@ extension FilePath {
|
||||
/// file.kind == .regular // true
|
||||
/// file.extension // "txt"
|
||||
/// path.append(file) // path is "/tmp/foo.txt"
|
||||
///
|
||||
public struct Component {
|
||||
internal var _path: FilePath
|
||||
internal var _range: Range<SystemString.Index>
|
||||
@@ -73,7 +72,7 @@ extension FilePath {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component {
|
||||
|
||||
/// Whether a component is a regular file or directory name, or a special
|
||||
@@ -98,6 +97,7 @@ extension FilePath.Component {
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Root {
|
||||
// TODO: Windows analysis APIs
|
||||
}
|
||||
@@ -183,15 +183,18 @@ extension _PathSlice {
|
||||
internal var _storage: SystemString { _path._storage }
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component: _PathSlice {
|
||||
}
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Root: _PathSlice {
|
||||
internal var _range: Range<SystemString.Index> {
|
||||
(..<_rootEnd).relative(to: _path._storage)
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath: _PlatformStringable {
|
||||
@usableFromInline
|
||||
func _withPlatformString<Result>(_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result) rethrows -> Result {
|
||||
try _storage.withPlatformString(body)
|
||||
}
|
||||
@@ -202,6 +205,7 @@ extension FilePath: _PlatformStringable {
|
||||
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component {
|
||||
// The index of the `.` denoting an extension
|
||||
internal func _extensionIndex() -> SystemString.Index? {
|
||||
@@ -230,6 +234,7 @@ internal func _makeExtension(_ ext: String) -> SystemString {
|
||||
return result
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component {
|
||||
internal init?(_ str: SystemString) {
|
||||
// FIXME: explicit null root? Or something else?
|
||||
@@ -242,6 +247,7 @@ extension FilePath.Component {
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Root {
|
||||
internal init?(_ str: SystemString) {
|
||||
// FIXME: explicit null root? Or something else?
|
||||
@@ -256,6 +262,7 @@ extension FilePath.Root {
|
||||
|
||||
// MARK: - Invariants
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component {
|
||||
// TODO: ensure this all gets easily optimized away in release...
|
||||
internal func _invariantCheck() {
|
||||
@@ -267,6 +274,8 @@ extension FilePath.Component {
|
||||
#endif // DEBUG
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Root {
|
||||
internal func _invariantCheck() {
|
||||
#if DEBUG
|
||||
@@ -276,3 +285,9 @@ extension FilePath.Root {
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=5.5) && canImport(_Concurrency)
|
||||
extension FilePath.Root: Sendable {}
|
||||
extension FilePath.Component: Sendable {}
|
||||
extension FilePath.Component.Kind: Sendable {}
|
||||
#endif
|
||||
@@ -111,6 +111,7 @@ extension SystemString {
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
internal mutating func _removeTrailingSeparator() {
|
||||
_storage._removeTrailingSeparator()
|
||||
@@ -191,6 +192,7 @@ extension SystemString {
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
internal var _relativeStart: SystemString.Index {
|
||||
_storage._relativePathStart
|
||||
@@ -202,6 +204,7 @@ extension FilePath {
|
||||
|
||||
// Parse separators
|
||||
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
internal typealias _Index = SystemString.Index
|
||||
|
||||
@@ -265,6 +268,7 @@ extension FilePath {
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.ComponentView {
|
||||
// TODO: Store this...
|
||||
internal var _relativeStart: SystemString.Index {
|
||||
@@ -289,6 +293,7 @@ extension SystemString {
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Root {
|
||||
// Asserting self is a root, returns whether this is an
|
||||
// absolute root.
|
||||
@@ -313,6 +318,7 @@ extension FilePath.Root {
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
internal var _portableDescription: String {
|
||||
guard _windowsPaths else { return description }
|
||||
@@ -331,6 +337,7 @@ internal var _windowsPaths: Bool {
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
// Whether we should add a separator when doing an append
|
||||
internal var _needsSeparatorForAppend: Bool {
|
||||
@@ -358,6 +365,7 @@ extension FilePath {
|
||||
}
|
||||
|
||||
// MARK: - Invariants
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
internal func _invariantCheck() {
|
||||
#if DEBUG
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// MARK: - Platform string
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// Creates a file path by copying bytes from a null-terminated platform
|
||||
/// string.
|
||||
@@ -63,6 +63,13 @@ extension FilePath {
|
||||
}
|
||||
}
|
||||
|
||||
#if !os(Windows)
|
||||
// Note: This function should have been opaque, but it shipped as
|
||||
// `@_alwaysEmitIntoClient` in macOS 12/iOS 15, and now it is stuck
|
||||
// this way forever. (Or until the language provides a way for us
|
||||
// to declare separate availability for a function's exported symbol
|
||||
// and its inlinable body.)
|
||||
|
||||
/// Calls the given closure with a pointer to the contents of the file path,
|
||||
/// represented as a null-terminated platform string.
|
||||
///
|
||||
@@ -79,16 +86,30 @@ extension FilePath {
|
||||
public func withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
) rethrows -> Result {
|
||||
// For backwards deployment, call withCString if available.
|
||||
#if !os(Windows)
|
||||
return try withCString(body)
|
||||
#else
|
||||
return try _withPlatformString(body)
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
/// Calls the given closure with a pointer to the contents of the file path,
|
||||
/// represented as a null-terminated platform string.
|
||||
///
|
||||
/// - Parameter body: A closure with a pointer parameter
|
||||
/// that points to a null-terminated platform string.
|
||||
/// If `body` has a return value,
|
||||
/// that value is also used as the return value for this method.
|
||||
/// - Returns: The return value, if any, of the `body` closure parameter.
|
||||
///
|
||||
/// The pointer passed as an argument to `body` is valid
|
||||
/// only during the execution of this method.
|
||||
/// Don't try to store the pointer for later use.
|
||||
public func withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
) rethrows -> Result {
|
||||
return try _withPlatformString(body)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component {
|
||||
/// Creates a file path component by copying bytes from a null-terminated
|
||||
/// platform string.
|
||||
@@ -98,7 +119,6 @@ extension FilePath.Component {
|
||||
///
|
||||
/// - Parameter platformString: A pointer to a null-terminated platform
|
||||
/// string.
|
||||
///
|
||||
public init?(platformString: UnsafePointer<CInterop.PlatformChar>) {
|
||||
self.init(_platformString: platformString)
|
||||
}
|
||||
@@ -178,7 +198,7 @@ extension FilePath.Component {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Root {
|
||||
/// Creates a file path root by copying bytes from a null-terminated platform
|
||||
/// string.
|
||||
@@ -187,7 +207,6 @@ extension FilePath.Root {
|
||||
///
|
||||
/// - Parameter platformString: A pointer to a null-terminated platform
|
||||
/// string.
|
||||
///
|
||||
public init?(platformString: UnsafePointer<CInterop.PlatformChar>) {
|
||||
self.init(_platformString: platformString)
|
||||
}
|
||||
@@ -268,7 +287,7 @@ extension FilePath.Root {
|
||||
|
||||
// MARK: - String literals
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath: ExpressibleByStringLiteral {
|
||||
/// Creates a file path from a string literal.
|
||||
///
|
||||
@@ -287,7 +306,7 @@ extension FilePath: ExpressibleByStringLiteral {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component: ExpressibleByStringLiteral {
|
||||
/// Create a file path component from a string literal.
|
||||
///
|
||||
@@ -312,7 +331,7 @@ extension FilePath.Component: ExpressibleByStringLiteral {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Root: ExpressibleByStringLiteral {
|
||||
/// Create a file path root from a string literal.
|
||||
///
|
||||
@@ -337,7 +356,7 @@ extension FilePath.Root: ExpressibleByStringLiteral {
|
||||
|
||||
// MARK: - Printing and dumping
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
/// A textual representation of the file path.
|
||||
///
|
||||
@@ -353,7 +372,7 @@ extension FilePath: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
public var debugDescription: String { description.debugDescription }
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
|
||||
/// A textual representation of the path component.
|
||||
@@ -370,7 +389,7 @@ extension FilePath.Component: CustomStringConvertible, CustomDebugStringConverti
|
||||
public var debugDescription: String { description.debugDescription }
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Root: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
|
||||
/// A textual representation of the path root.
|
||||
@@ -390,7 +409,7 @@ extension FilePath.Root: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
// MARK: - Convenience helpers
|
||||
|
||||
// Convenience helpers
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// Creates a string by interpreting the path’s content as UTF-8 on Unix
|
||||
/// and UTF-16 on Windows.
|
||||
@@ -401,7 +420,7 @@ extension FilePath {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component {
|
||||
/// Creates a string by interpreting the component’s content as UTF-8 on Unix
|
||||
/// and UTF-16 on Windows.
|
||||
@@ -412,7 +431,7 @@ extension FilePath.Component {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Root {
|
||||
/// On Unix, this returns `"/"`.
|
||||
///
|
||||
@@ -426,7 +445,7 @@ extension FilePath.Root {
|
||||
|
||||
// MARK: - Decoding and validating
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension String {
|
||||
/// Creates a string by interpreting the file path's content as UTF-8 on Unix
|
||||
/// and UTF-16 on Windows.
|
||||
@@ -456,7 +475,7 @@ extension String {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension String {
|
||||
/// Creates a string by interpreting the path component's content as UTF-8 on
|
||||
/// Unix and UTF-16 on Windows.
|
||||
@@ -486,7 +505,7 @@ extension String {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension String {
|
||||
/// On Unix, creates the string `"/"`
|
||||
///
|
||||
@@ -539,7 +558,7 @@ extension String {
|
||||
|
||||
// MARK: - Deprecations
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension String {
|
||||
@available(*, deprecated, renamed: "init(decoding:)")
|
||||
public init(_ path: FilePath) { self.init(decoding: path) }
|
||||
@@ -549,7 +568,7 @@ extension String {
|
||||
}
|
||||
|
||||
#if !os(Windows)
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// For backwards compatibility only. This initializer is equivalent to
|
||||
/// the preferred `FilePath(platformString:)`.
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// MARK: - Query API
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// Returns true if this path uniquely identifies the location of
|
||||
/// a file without reference to an additional starting location.
|
||||
@@ -52,6 +52,8 @@ extension FilePath {
|
||||
/// * `\Users`
|
||||
public var isRelative: Bool { !isAbsolute }
|
||||
|
||||
// TODO(Windows docs): examples with roots, such as whether `\foo\bar`
|
||||
// starts with `C:\foo`
|
||||
/// Returns whether `other` is a prefix of `self`, only considering
|
||||
/// whole path components.
|
||||
///
|
||||
@@ -63,15 +65,14 @@ extension FilePath {
|
||||
/// path.starts(with: "/usr/bin/ls") // true
|
||||
/// path.starts(with: "/usr/bin/ls///") // true
|
||||
/// path.starts(with: "/us") // false
|
||||
///
|
||||
// TODO(Windows docs): examples with roots, such as whether `\foo\bar`
|
||||
// starts with `C:\foo`
|
||||
public func starts(with other: FilePath) -> Bool {
|
||||
guard !other.isEmpty else { return true }
|
||||
return self.root == other.root && components.starts(
|
||||
with: other.components)
|
||||
}
|
||||
|
||||
// TODO(Windows docs): examples with roots, such as whether `C:\foo\bar`
|
||||
// ends with `C:bar`
|
||||
/// Returns whether `other` is a suffix of `self`, only considering
|
||||
/// whole path components.
|
||||
///
|
||||
@@ -83,9 +84,6 @@ extension FilePath {
|
||||
/// path.ends(with: "usr/bin/ls") // true
|
||||
/// path.ends(with: "/usr/bin/ls///") // true
|
||||
/// path.ends(with: "/ls") // false
|
||||
///
|
||||
// TODO(Windows docs): examples with roots, such as whether `C:\foo\bar`
|
||||
// ends with `C:bar`
|
||||
public func ends(with other: FilePath) -> Bool {
|
||||
if other.root != nil {
|
||||
// TODO: anything tricky here for Windows?
|
||||
@@ -101,7 +99,7 @@ extension FilePath {
|
||||
}
|
||||
|
||||
// MARK: - Decompose a path
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// Returns the root of a path if there is one, otherwise `nil`.
|
||||
///
|
||||
@@ -145,7 +143,6 @@ extension FilePath {
|
||||
/// path.root = nil // path is #"foo\bar"#
|
||||
/// path.root = "C:" // path is #"C:foo\bar"#
|
||||
/// path.root = #"C:\"# // path is #"C:\foo\bar"#
|
||||
///
|
||||
public var root: FilePath.Root? {
|
||||
get {
|
||||
guard _hasRoot else { return nil }
|
||||
@@ -178,7 +175,6 @@ extension FilePath {
|
||||
/// * `\\?\device\folder\file.exe => folder\file.exe`
|
||||
/// * `\\server\share\file => file`
|
||||
/// * `\ => ""`
|
||||
///
|
||||
public __consuming func removingRoot() -> FilePath {
|
||||
var copy = self
|
||||
copy.root = nil
|
||||
@@ -186,7 +182,7 @@ extension FilePath {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// Returns the final component of the path.
|
||||
/// Returns `nil` if the path is empty or only contains a root.
|
||||
@@ -209,7 +205,6 @@ extension FilePath {
|
||||
/// * `\\?\UNC\server\share\bar.exe => bar.exe`
|
||||
/// * `\\server\share => nil`
|
||||
/// * `\\?\UNC\server\share\ => nil`
|
||||
///
|
||||
public var lastComponent: Component? { components.last }
|
||||
|
||||
/// Creates a new path with everything up to but not including
|
||||
@@ -247,7 +242,6 @@ extension FilePath {
|
||||
/// path.removeLastComponent() == true // path is "/usr"
|
||||
/// path.removeLastComponent() == true // path is "/"
|
||||
/// path.removeLastComponent() == false // path is "/"
|
||||
///
|
||||
@discardableResult
|
||||
public mutating func removeLastComponent() -> Bool {
|
||||
defer { _invariantCheck() }
|
||||
@@ -258,7 +252,7 @@ extension FilePath {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath.Component {
|
||||
/// The extension of this file or directory component.
|
||||
///
|
||||
@@ -271,7 +265,6 @@ extension FilePath.Component {
|
||||
/// * `Foo.app => app`
|
||||
/// * `.hidden => nil`
|
||||
/// * `.. => nil`
|
||||
///
|
||||
public var `extension`: String? {
|
||||
guard let range = _extensionRange() else { return nil }
|
||||
return _slice[range].string
|
||||
@@ -285,13 +278,12 @@ extension FilePath.Component {
|
||||
/// * `Foo.app => Foo`
|
||||
/// * `.hidden => .hidden`
|
||||
/// * `.. => ..`
|
||||
///
|
||||
public var stem: String {
|
||||
_slice[_stemRange()].string
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
|
||||
/// The extension of the file or directory last component.
|
||||
@@ -322,7 +314,6 @@ extension FilePath {
|
||||
/// path.extension = "o" // path is "/tmp/file.o"
|
||||
/// path.extension = nil // path is "/tmp/file"
|
||||
/// path.extension = "" // path is "/tmp/file."
|
||||
///
|
||||
public var `extension`: String? {
|
||||
get { lastComponent?.extension }
|
||||
set {
|
||||
@@ -349,8 +340,8 @@ extension FilePath {
|
||||
/// Returns `nil` if `lastComponent` is `nil`
|
||||
///
|
||||
/// * `/tmp/foo.txt => foo`
|
||||
/// * `/Appliations/Foo.app/ => Foo`
|
||||
/// * `/Appliations/Foo.app/bar.txt => bar`
|
||||
/// * `/Applications/Foo.app/ => Foo`
|
||||
/// * `/Applications/Foo.app/bar.txt => bar`
|
||||
/// * `/tmp/.hidden => .hidden`
|
||||
/// * `/tmp/.. => ..`
|
||||
/// * `/ => nil`
|
||||
@@ -358,7 +349,7 @@ extension FilePath {
|
||||
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// Whether the path is in lexical-normal form, that is `.` and `..`
|
||||
/// components have been collapsed lexically (i.e. without following
|
||||
@@ -439,8 +430,9 @@ extension FilePath {
|
||||
}
|
||||
|
||||
// Modification and concatenation API
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
// TODO(Windows docs): example with roots
|
||||
/// If `prefix` is a prefix of `self`, removes it and returns `true`.
|
||||
/// Otherwise returns `false`.
|
||||
///
|
||||
@@ -450,8 +442,6 @@ extension FilePath {
|
||||
/// path.removePrefix("/usr/bin") // false
|
||||
/// path.removePrefix("/us") // false
|
||||
/// path.removePrefix("/usr/local") // true, path is "bin"
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
public mutating func removePrefix(_ prefix: FilePath) -> Bool {
|
||||
defer { _invariantCheck() }
|
||||
// FIXME: Should Windows have more nuanced semantics?
|
||||
@@ -462,6 +452,7 @@ extension FilePath {
|
||||
return true
|
||||
}
|
||||
|
||||
// TODO(Windows docs): example with roots
|
||||
/// Append a `component` on to the end of this path.
|
||||
///
|
||||
/// Example:
|
||||
@@ -472,13 +463,12 @@ extension FilePath {
|
||||
/// path.append(comp)
|
||||
/// }
|
||||
/// // path is "/tmp/foo/bar/../baz"
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
public mutating func append(_ component: __owned FilePath.Component) {
|
||||
defer { _invariantCheck() }
|
||||
_append(unchecked: component._slice)
|
||||
}
|
||||
|
||||
// TODO(Windows docs): example with roots
|
||||
/// Append `components` on to the end of this path.
|
||||
///
|
||||
/// Example:
|
||||
@@ -487,8 +477,6 @@ extension FilePath {
|
||||
/// path.append(["usr", "local"]) // path is "/usr/local"
|
||||
/// let otherPath: FilePath = "/bin/ls"
|
||||
/// path.append(otherPath.components) // path is "/usr/local/bin/ls"
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
public mutating func append<C: Collection>(
|
||||
_ components: __owned C
|
||||
) where C.Element == FilePath.Component {
|
||||
@@ -498,6 +486,8 @@ extension FilePath {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(Windows docs): example with roots, should we rephrase this "spurious
|
||||
// roots"?
|
||||
/// Append the contents of `other`, ignoring any spurious leading separators.
|
||||
///
|
||||
/// A leading separator is spurious if `self` is non-empty.
|
||||
@@ -508,9 +498,6 @@ extension FilePath {
|
||||
/// path.append("/var/www/website") // "/var/www/website"
|
||||
/// path.append("static/assets") // "/var/www/website/static/assets"
|
||||
/// path.append("/main.css") // "/var/www/website/static/assets/main.css"
|
||||
///
|
||||
// TODO(Windows docs): example with roots, should we rephrase this "spurious
|
||||
// roots"?
|
||||
public mutating func append(_ other: __owned String) {
|
||||
defer { _invariantCheck() }
|
||||
guard !other.utf8.isEmpty else { return }
|
||||
@@ -522,18 +509,16 @@ extension FilePath {
|
||||
_append(unchecked: otherPath._storage[otherPath._relativeStart...])
|
||||
}
|
||||
|
||||
/// Non-mutating version of `append(_:Component)`.
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
/// Non-mutating version of `append(_:Component)`.
|
||||
public __consuming func appending(_ other: __owned Component) -> FilePath {
|
||||
var copy = self
|
||||
copy.append(other)
|
||||
return copy
|
||||
}
|
||||
|
||||
/// Non-mutating version of `append(_:C)`.
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
/// Non-mutating version of `append(_:C)`.
|
||||
public __consuming func appending<C: Collection>(
|
||||
_ components: __owned C
|
||||
) -> FilePath where C.Element == FilePath.Component {
|
||||
@@ -542,15 +527,16 @@ extension FilePath {
|
||||
return copy
|
||||
}
|
||||
|
||||
/// Non-mutating version of `append(_:String)`.
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
/// Non-mutating version of `append(_:String)`.
|
||||
public __consuming func appending(_ other: __owned String) -> FilePath {
|
||||
var copy = self
|
||||
copy.append(other)
|
||||
return copy
|
||||
}
|
||||
|
||||
// TODO(Windows docs): examples and docs with roots, update/generalize doc
|
||||
// comment
|
||||
/// If `other` does not have a root, append each component of `other`. If
|
||||
/// `other` has a root, replaces `self` with other.
|
||||
///
|
||||
@@ -564,9 +550,6 @@ extension FilePath {
|
||||
/// var path: FilePath = "/tmp"
|
||||
/// path.push("dir/file.txt") // path is "/tmp/dir/file.txt"
|
||||
/// path.push("/bin") // path is "/bin"
|
||||
///
|
||||
// TODO(Windows docs): examples and docs with roots, update/generalize doc
|
||||
// comment
|
||||
public mutating func push(_ other: __owned FilePath) {
|
||||
defer { _invariantCheck() }
|
||||
guard other.root == nil else {
|
||||
@@ -577,9 +560,8 @@ extension FilePath {
|
||||
_append(unchecked: other._storage[...])
|
||||
}
|
||||
|
||||
/// Non-mutating version of `push()`.
|
||||
///
|
||||
// TODO(Windows docs): examples and docs with roots
|
||||
/// Non-mutating version of `push()`.
|
||||
public __consuming func pushing(_ other: __owned FilePath) -> FilePath {
|
||||
var copy = self
|
||||
copy.push(other)
|
||||
@@ -601,7 +583,7 @@ extension FilePath {
|
||||
}
|
||||
|
||||
// MARK - Renamed
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
@available(*, unavailable, renamed: "removingLastComponent()")
|
||||
public var dirname: FilePath { removingLastComponent() }
|
||||
|
||||
@@ -150,21 +150,19 @@ internal struct WindowsRootInfo {
|
||||
/// * Omitted volume from other forms: `\\.\`, `\\.\UNC\server\\`, `\\server\\`
|
||||
case empty
|
||||
|
||||
// TODO: NT paths? Admin paths using `$`?
|
||||
/// A specified drive.
|
||||
///
|
||||
/// * Traditional disk: `C:\`, `C:`
|
||||
/// * Device disk: `\\.\C:\`, `\\?\C:\`
|
||||
/// * UNC: `\\server\e:\`, `\\?\UNC\server\e:\`
|
||||
///
|
||||
// TODO: NT paths? Admin paths using `$`?
|
||||
case drive(Character)
|
||||
|
||||
// TODO: GUID type?
|
||||
/// A volume with a GUID in a non-traditional path
|
||||
///
|
||||
/// * UNC: `\\host\Volume{0000-...}\`, `\\.\UNC\host\Volume{0000-...}\`
|
||||
/// * Device roots: `\\.\Volume{0000-...}\`, `\\?\Volume{000-...}\`
|
||||
///
|
||||
// TODO: GUID type?
|
||||
case guid(String)
|
||||
|
||||
// TODO: Legacy DOS devices, such as COM1?
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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
|
||||
*/
|
||||
|
||||
extension String {
|
||||
internal init(
|
||||
_unsafeUninitializedCapacity capacity: Int,
|
||||
initializingUTF8With body: (UnsafeMutableBufferPointer<UInt8>) throws -> Int
|
||||
) rethrows {
|
||||
if #available(macOS 11, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
|
||||
self = try String(
|
||||
unsafeUninitializedCapacity: capacity,
|
||||
initializingUTF8With: body)
|
||||
return
|
||||
}
|
||||
|
||||
let array = try Array<UInt8>(
|
||||
unsafeUninitializedCapacity: capacity
|
||||
) { buffer, count in
|
||||
count = try body(buffer)
|
||||
}
|
||||
self = String(decoding: array, as: UTF8.self)
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,15 @@
|
||||
/*
|
||||
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 - 2021 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: - Public typealiases
|
||||
|
||||
/// The C `mode_t` type.
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(*, deprecated, renamed: "CInterop.Mode")
|
||||
public typealias CModeT = CInterop.Mode
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#elseif os(Linux) || os(FreeBSD) || os(Android)
|
||||
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
|
||||
@_implementationOnly import CSystem
|
||||
import Glibc
|
||||
#elseif os(Windows)
|
||||
@@ -26,15 +19,23 @@ import ucrt
|
||||
#error("Unsupported Platform")
|
||||
#endif
|
||||
|
||||
/// A namespace for C and platform types
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
public enum CInterop {
|
||||
// MARK: - Public typealiases
|
||||
|
||||
// FIXME: `CModeT` ought to be deprecated and replaced with `CInterop.Mode`
|
||||
// if/when the compiler becomes less strict about availability checking
|
||||
// of "namespaced" typealiases. (rdar://81722893)
|
||||
#if os(Windows)
|
||||
public typealias Mode = CInt
|
||||
/// The C `mode_t` type.
|
||||
public typealias CModeT = CInt
|
||||
#else
|
||||
public typealias Mode = mode_t
|
||||
/// The C `mode_t` type.
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
public typealias CModeT = mode_t
|
||||
#endif
|
||||
|
||||
/// A namespace for C and platform types
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
public enum CInterop {
|
||||
/// The C `char` type
|
||||
public typealias Char = CChar
|
||||
|
||||
|
||||
@@ -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 - 2021 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,7 +13,7 @@
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#elseif os(Linux) || os(FreeBSD) || os(Android)
|
||||
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
|
||||
import Glibc
|
||||
#elseif os(Windows)
|
||||
import CSystem
|
||||
@@ -22,7 +22,7 @@ import ucrt
|
||||
#error("Unsupported Platform")
|
||||
#endif
|
||||
|
||||
// MARK: errno
|
||||
// MARK: - errno
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _ERRNO_NOT_USED: CInt { 0 }
|
||||
@@ -392,6 +392,7 @@ internal var _ENOATTR: CInt { ENOATTR }
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _EBADMSG: CInt { EBADMSG }
|
||||
|
||||
#if !os(OpenBSD)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _EMULTIHOP: CInt { EMULTIHOP }
|
||||
|
||||
@@ -406,13 +407,16 @@ internal var _ENOSR: CInt { ENOSR }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _ENOSTR: CInt { ENOSTR }
|
||||
#endif
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _EPROTO: CInt { EPROTO }
|
||||
|
||||
#if !os(OpenBSD)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _ETIME: CInt { ETIME }
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _EOPNOTSUPP: CInt { EOPNOTSUPP }
|
||||
@@ -438,7 +442,7 @@ internal var _EQFULL: CInt { EQFULL }
|
||||
internal var _ELAST: CInt { ELAST }
|
||||
#endif
|
||||
|
||||
// MARK: File Operations
|
||||
// MARK: - File Operations
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _O_RDONLY: CInt { O_RDONLY }
|
||||
|
||||
@@ -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 - 2021 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 @@
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#elseif os(Linux) || os(FreeBSD) || os(Android)
|
||||
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
|
||||
@_implementationOnly import CSystem
|
||||
import Glibc
|
||||
#elseif os(Windows)
|
||||
@@ -60,7 +60,10 @@ internal func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer<Int8>!
|
||||
strerror(__errnum)
|
||||
}
|
||||
|
||||
internal func system_strlen(_ s: UnsafePointer<Int8>) -> Int {
|
||||
internal func system_strlen(_ s: UnsafePointer<CChar>) -> Int {
|
||||
strlen(s)
|
||||
}
|
||||
internal func system_strlen(_ s: UnsafeMutablePointer<CChar>) -> Int {
|
||||
strlen(s)
|
||||
}
|
||||
|
||||
@@ -78,6 +81,16 @@ internal func system_platform_strlen(_ s: UnsafePointer<CInterop.PlatformChar>)
|
||||
#endif
|
||||
}
|
||||
|
||||
// memset for raw buffers
|
||||
// FIXME: Do we really not have something like this in the stdlib already?
|
||||
internal func system_memset(
|
||||
_ buffer: UnsafeMutableRawBufferPointer,
|
||||
to byte: UInt8
|
||||
) {
|
||||
guard buffer.count > 0 else { return }
|
||||
memset(buffer.baseAddress!, CInt(byte), buffer.count)
|
||||
}
|
||||
|
||||
// Interop between String and platfrom string
|
||||
extension String {
|
||||
internal func _withPlatformString<Result>(
|
||||
|
||||
@@ -19,9 +19,10 @@
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
internal struct Trace {
|
||||
internal struct Entry: Hashable {
|
||||
private var name: String
|
||||
private var arguments: [AnyHashable]
|
||||
internal struct Entry {
|
||||
|
||||
internal var name: String
|
||||
internal var arguments: [AnyHashable]
|
||||
|
||||
internal init(name: String, _ arguments: [AnyHashable]) {
|
||||
self.name = name
|
||||
@@ -192,6 +193,13 @@ internal func _mockOffT(
|
||||
) -> _COffT {
|
||||
_COffT(mockImpl(name: name, path: path, args))
|
||||
}
|
||||
|
||||
internal func _mockModeT(
|
||||
name: String = #function, _ args: AnyHashable...
|
||||
) -> CInterop.Mode {
|
||||
CInterop.Mode(mockImpl(name: name, path: nil, args))
|
||||
}
|
||||
|
||||
#endif // ENABLE_MOCKING
|
||||
|
||||
// Force paths to be treated as Windows syntactically if `enabled` is
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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
|
||||
*/
|
||||
|
||||
// A copy-on-write fixed-size buffer of raw memory.
|
||||
internal struct _RawBuffer {
|
||||
internal var _storage: Storage?
|
||||
|
||||
internal init() {
|
||||
self._storage = nil
|
||||
}
|
||||
|
||||
internal init(minimumCapacity: Int) {
|
||||
if minimumCapacity > 0 {
|
||||
self._storage = Storage.create(minimumCapacity: minimumCapacity)
|
||||
} else {
|
||||
self._storage = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension _RawBuffer {
|
||||
internal var capacity: Int {
|
||||
_storage?.header ?? 0 // Note: not capacity!
|
||||
}
|
||||
|
||||
internal mutating func ensureUnique() {
|
||||
guard _storage != nil else { return }
|
||||
let unique = isKnownUniquelyReferenced(&_storage)
|
||||
if !unique {
|
||||
_storage = _copy(capacity: capacity)
|
||||
}
|
||||
}
|
||||
|
||||
internal func _grow(desired: Int) -> Int {
|
||||
let next = Int(1.75 * Double(self.capacity))
|
||||
return Swift.max(next, desired)
|
||||
}
|
||||
|
||||
internal mutating func ensureUnique(capacity: Int) {
|
||||
let unique = isKnownUniquelyReferenced(&_storage)
|
||||
if !unique || self.capacity < capacity {
|
||||
_storage = _copy(capacity: _grow(desired: capacity))
|
||||
}
|
||||
}
|
||||
|
||||
internal func withUnsafeBytes<R>(
|
||||
_ body: (UnsafeRawBufferPointer) throws -> R
|
||||
) rethrows -> R {
|
||||
guard let storage = _storage else {
|
||||
return try body(UnsafeRawBufferPointer(start: nil, count: 0))
|
||||
}
|
||||
return try storage.withUnsafeMutablePointers { count, bytes in
|
||||
let buffer = UnsafeRawBufferPointer(start: bytes, count: count.pointee)
|
||||
return try body(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
internal mutating func withUnsafeMutableBytes<R>(
|
||||
_ body: (UnsafeMutableRawBufferPointer) throws -> R
|
||||
) rethrows -> R {
|
||||
guard _storage != nil else {
|
||||
return try body(UnsafeMutableRawBufferPointer(start: nil, count: 0))
|
||||
}
|
||||
ensureUnique()
|
||||
return try _storage!.withUnsafeMutablePointers { count, bytes in
|
||||
let buffer = UnsafeMutableRawBufferPointer(start: bytes, count: count.pointee)
|
||||
return try body(buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension _RawBuffer {
|
||||
internal class Storage: ManagedBuffer<Int, UInt8> {
|
||||
internal static func create(minimumCapacity: Int) -> Storage {
|
||||
Storage.create(
|
||||
minimumCapacity: minimumCapacity,
|
||||
makingHeaderWith: { $0.capacity }
|
||||
) as! Storage
|
||||
}
|
||||
}
|
||||
|
||||
internal func _copy(capacity: Int) -> Storage {
|
||||
let copy = Storage.create(minimumCapacity: capacity)
|
||||
copy.withUnsafeMutablePointers { dstlen, dst in
|
||||
self.withUnsafeBytes { src in
|
||||
guard src.count > 0 else { return }
|
||||
assert(src.count <= dstlen.pointee)
|
||||
UnsafeMutableRawPointer(dst)
|
||||
.copyMemory(
|
||||
from: src.baseAddress!,
|
||||
byteCount: Swift.min(src.count, dstlen.pointee))
|
||||
}
|
||||
}
|
||||
return copy
|
||||
}
|
||||
}
|
||||
@@ -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 - 2021 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
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#elseif os(Linux) || os(FreeBSD) || os(Android)
|
||||
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
|
||||
import Glibc
|
||||
#elseif os(Windows)
|
||||
import ucrt
|
||||
@@ -54,7 +54,7 @@ internal func system_close(_ fd: Int32) -> Int32 {
|
||||
|
||||
// read
|
||||
internal func system_read(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer?, _ nbyte: Int
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
|
||||
@@ -64,7 +64,7 @@ internal func system_read(
|
||||
|
||||
// pread
|
||||
internal func system_pread(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer?, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
|
||||
@@ -84,7 +84,7 @@ internal func system_lseek(
|
||||
|
||||
// write
|
||||
internal func system_write(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer?, _ nbyte: Int
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
|
||||
@@ -94,7 +94,7 @@ internal func system_write(
|
||||
|
||||
// pwrite
|
||||
internal func system_pwrite(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer?, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
|
||||
|
||||
@@ -58,6 +58,23 @@ internal func write(
|
||||
Int(_write(fd, buf, numericCast(nbyte)))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal func lseek(
|
||||
_ fd: Int32, _ off: off_t, _ whence: Int32
|
||||
) -> off_t {
|
||||
_lseek(fd, off, whence)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal func dup(_ fd: Int32) -> Int32 {
|
||||
_dup(fd)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal func dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
|
||||
_dup2(fd, fd2)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal func pread(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
|
||||
@@ -0,0 +1,334 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2022 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 false && swift(>=5.8) && $MoveOnly && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS))
|
||||
|
||||
import Darwin.Mach
|
||||
|
||||
public protocol MachPortRight {}
|
||||
|
||||
@inlinable
|
||||
internal func _machPrecondition(
|
||||
file: StaticString = #file,
|
||||
line: UInt = #line,
|
||||
_ body: @autoclosure () -> kern_return_t
|
||||
) {
|
||||
let kr = body()
|
||||
let expected = KERN_SUCCESS
|
||||
precondition(kr == expected, file: file, line: line)
|
||||
}
|
||||
|
||||
@available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *)
|
||||
@frozen
|
||||
public enum Mach {
|
||||
public struct Port<RightType: MachPortRight>: ~Copyable {
|
||||
@usableFromInline
|
||||
internal var _name: mach_port_name_t
|
||||
|
||||
@usableFromInline
|
||||
internal var _context: mach_port_context_t
|
||||
|
||||
/// Transfer ownership of an existing unmanaged Mach port right into a
|
||||
/// `Mach.Port` by name.
|
||||
///
|
||||
/// This initializer traps if `name` is `MACH_PORT_NULL`, or if `name` is
|
||||
/// `MACH_PORT_DEAD` and the `RightType` is `Mach.ReceiveRight`.
|
||||
///
|
||||
/// If the type of the right does not match the `RightType` of the
|
||||
/// `Mach.Port` being constructed, behavior is undefined.
|
||||
///
|
||||
/// The underlying port right will be automatically deallocated at the
|
||||
/// end of the `Mach.Port` instance's lifetime.
|
||||
///
|
||||
/// This initializer makes a syscall to guard the right.
|
||||
public init(name: mach_port_name_t) {
|
||||
precondition(name != mach_port_name_t(MACH_PORT_NULL), "Mach.Port cannot be initialized with MACH_PORT_NULL")
|
||||
self._name = name
|
||||
|
||||
if RightType.self == ReceiveRight.self {
|
||||
precondition(name != 0xFFFFFFFF /* 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))
|
||||
self._context = secret
|
||||
}
|
||||
else {
|
||||
self._context = 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Borrow access to the port name in a block that can perform
|
||||
/// non-consuming operations.
|
||||
///
|
||||
/// Take care when using this function; many operations consume rights,
|
||||
/// and send-once rights are easily consumed.
|
||||
///
|
||||
/// If the right is consumed, behavior is undefined.
|
||||
///
|
||||
/// The body block may optionally return something, which will then be
|
||||
/// returned to the caller of withBorrowedName.
|
||||
@inlinable
|
||||
public func withBorrowedName<ReturnType>(
|
||||
body: (mach_port_name_t) -> ReturnType
|
||||
) -> ReturnType {
|
||||
return body(_name)
|
||||
}
|
||||
|
||||
deinit {
|
||||
if _name == 0xFFFFFFFF /* MACH_PORT_DEAD */ {
|
||||
precondition(RightType.self != ReceiveRight.self, "Receive rights cannot be dead names")
|
||||
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_DEAD_NAME, -1))
|
||||
} else {
|
||||
if RightType.self == ReceiveRight.self {
|
||||
_machPrecondition(mach_port_destruct(mach_task_self_, _name, -1, _context))
|
||||
} else if RightType.self == SendRight.self {
|
||||
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND, -1))
|
||||
} else if RightType.self == SendOnceRight.self {
|
||||
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND_ONCE, -1))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Possible errors that can be thrown by Mach.Port operations.
|
||||
public enum PortRightError : Error {
|
||||
/// Returned when an operation cannot be completed, because the Mach
|
||||
/// port right has become a dead name. This is caused by deallocation of the
|
||||
/// receive right on the other end.
|
||||
case deadName
|
||||
}
|
||||
|
||||
/// The MachPortRight type used to manage a receive right.
|
||||
@frozen
|
||||
public struct ReceiveRight: MachPortRight {}
|
||||
|
||||
/// The MachPortRight type used to manage a send right.
|
||||
@frozen
|
||||
public struct SendRight: MachPortRight {}
|
||||
|
||||
/// The MachPortRight type used to manage a send-once right.
|
||||
///
|
||||
/// Send-once rights are the most restrictive type of Mach port rights.
|
||||
/// They cannot create other rights, and are consumed upon use.
|
||||
///
|
||||
/// Upon destruction a send-once notification will be sent to the
|
||||
/// receiving end.
|
||||
@frozen
|
||||
public struct SendOnceRight: MachPortRight {}
|
||||
}
|
||||
|
||||
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.
|
||||
///
|
||||
/// This initializer aborts if name is MACH_PORT_NULL.
|
||||
///
|
||||
/// If the type of the right does not match the type T of Mach.Port<T>
|
||||
/// being constructed, the behavior is undefined.
|
||||
///
|
||||
/// The underlying port right will be automatically deallocated when
|
||||
/// the Mach.Port object is destroyed.
|
||||
public init(name: mach_port_name_t, context: mach_port_context_t) {
|
||||
self._name = name
|
||||
self._context = context
|
||||
}
|
||||
|
||||
/// Allocate a new Mach port with a receive right, creating a
|
||||
/// Mach.Port<Mach.ReceiveRight> to manage it.
|
||||
///
|
||||
/// This initializer will abort if the right could not be created.
|
||||
/// Callers may assert that a valid right is always returned.
|
||||
@inlinable
|
||||
public init() {
|
||||
var storage: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL)
|
||||
withUnsafeMutablePointer(to: &storage) { storage in
|
||||
_machPrecondition(mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, storage))
|
||||
}
|
||||
|
||||
// name-only init will guard ReceiveRights
|
||||
self.init(name: storage)
|
||||
}
|
||||
|
||||
|
||||
/// Transfer ownership of the underlying port right to the caller.
|
||||
///
|
||||
/// Returns a tuple containing the Mach port name representing the right,
|
||||
/// and the context value used to guard the right.
|
||||
///
|
||||
/// This operation liberates the right from management by the Mach.Port,
|
||||
/// and the underlying right will no longer be automatically deallocated.
|
||||
///
|
||||
/// After this function completes, the Mach.Port is destroyed and no longer
|
||||
/// usable.
|
||||
@inlinable
|
||||
public __consuming func relinquish(
|
||||
) -> (name: mach_port_name_t, context: mach_port_context_t) {
|
||||
return (name: _name, context: _context)
|
||||
}
|
||||
|
||||
/// Remove guard and transfer ownership of the underlying port right to
|
||||
/// the caller.
|
||||
///
|
||||
/// Returns the Mach port name representing the right.
|
||||
///
|
||||
/// This operation liberates the right from management by the Mach.Port,
|
||||
/// and the underlying right will no longer be automatically deallocated.
|
||||
///
|
||||
/// After this function completes, the Mach.Port is destroyed and no longer
|
||||
/// usable.
|
||||
///
|
||||
/// This function makes a syscall to remove the guard from
|
||||
/// Mach.ReceiveRights. Use relinquish() to avoid the syscall and extract
|
||||
/// the context value along with the port name.
|
||||
@inlinable
|
||||
public __consuming func unguardAndRelinquish() -> mach_port_name_t {
|
||||
_machPrecondition(mach_port_unguard(mach_task_self_, _name, _context))
|
||||
return _name
|
||||
}
|
||||
|
||||
/// Borrow access to the port name in a block that can perform
|
||||
/// non-consuming operations.
|
||||
///
|
||||
/// Take care when using this function; many operations consume rights.
|
||||
///
|
||||
/// If the right is consumed, behavior is undefined.
|
||||
///
|
||||
/// The body block may optionally return something, which will then be
|
||||
/// returned to the caller of withBorrowedName.
|
||||
@inlinable
|
||||
public func withBorrowedName<ReturnType>(
|
||||
body: (mach_port_name_t, mach_port_context_t) -> ReturnType
|
||||
) -> ReturnType {
|
||||
return body(_name, _context)
|
||||
}
|
||||
|
||||
/// Create a send-once right for a given receive right.
|
||||
///
|
||||
/// This does not affect the makeSendCount of the receive right.
|
||||
///
|
||||
/// This function will abort if the right could not be created.
|
||||
/// Callers may assert that a valid right is always returned.
|
||||
@inlinable
|
||||
public func makeSendOnceRight() -> Mach.Port<Mach.SendOnceRight> {
|
||||
// send once rights do not coalesce
|
||||
var newRight: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL)
|
||||
var newRightType: mach_port_type_t = MACH_PORT_TYPE_NONE
|
||||
|
||||
withUnsafeMutablePointer(to: &newRight) { newRight in
|
||||
withUnsafeMutablePointer(to: &newRightType) { newRightType in
|
||||
_machPrecondition(
|
||||
mach_port_extract_right(mach_task_self_,
|
||||
_name,
|
||||
mach_msg_type_name_t(MACH_MSG_TYPE_MAKE_SEND_ONCE),
|
||||
newRight,
|
||||
newRightType)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// The value of newRight is validated by the Mach.Port initializer
|
||||
precondition(newRightType == MACH_MSG_TYPE_MOVE_SEND_ONCE)
|
||||
|
||||
return Mach.Port(name: newRight)
|
||||
}
|
||||
|
||||
/// Create a send right for a given receive right.
|
||||
///
|
||||
/// This increments the makeSendCount of the receive right.
|
||||
///
|
||||
/// This function will abort if the right could not be created.
|
||||
/// Callers may assert that a valid right is always returned.
|
||||
@inlinable
|
||||
public func makeSendRight() -> Mach.Port<Mach.SendRight> {
|
||||
let how = MACH_MSG_TYPE_MAKE_SEND
|
||||
|
||||
// name is the same because send and recv rights are coalesced
|
||||
_machPrecondition(mach_port_insert_right(mach_task_self_, _name, _name, mach_msg_type_name_t(how)))
|
||||
|
||||
return Mach.Port(name: _name)
|
||||
}
|
||||
|
||||
/// Access the make-send count.
|
||||
///
|
||||
/// Each get/set of this property makes a syscall.
|
||||
@inlinable
|
||||
public var makeSendCount: mach_port_mscount_t {
|
||||
get {
|
||||
var status: mach_port_status = mach_port_status()
|
||||
var size: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout<mach_port_status>.size / MemoryLayout<natural_t>.size)
|
||||
withUnsafeMutablePointer(to: &size) { size in
|
||||
withUnsafeMutablePointer(to: &status) { status in
|
||||
let info = UnsafeMutableRawPointer(status).bindMemory(to: integer_t.self, capacity: 1)
|
||||
_machPrecondition(mach_port_get_attributes(mach_task_self_, _name, MACH_PORT_RECEIVE_STATUS, info, size))
|
||||
}
|
||||
}
|
||||
return status.mps_mscount
|
||||
}
|
||||
|
||||
set {
|
||||
_machPrecondition(mach_port_set_mscount(mach_task_self_, _name, newValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Mach.Port where RightType == Mach.SendRight {
|
||||
/// Transfer ownership of the underlying port right to the caller.
|
||||
///
|
||||
/// Returns the Mach port name representing the right.
|
||||
///
|
||||
/// This operation liberates the right from management by the Mach.Port,
|
||||
/// and the underlying right will no longer be automatically deallocated.
|
||||
///
|
||||
/// After this function completes, the Mach.Port is destroyed and no longer
|
||||
/// usable.
|
||||
@inlinable
|
||||
public __consuming func relinquish() -> mach_port_name_t {
|
||||
return _name
|
||||
}
|
||||
|
||||
/// Create another send right from a given send right.
|
||||
///
|
||||
/// This does not affect the makeSendCount of the receive right.
|
||||
///
|
||||
/// If the send right being copied has become a dead name, meaning the
|
||||
/// receiving side has been deallocated, then copySendRight() will throw
|
||||
/// a Mach.PortRightError.deadName error.
|
||||
@inlinable
|
||||
public func copySendRight() throws -> Mach.Port<Mach.SendRight> {
|
||||
let how = MACH_MSG_TYPE_COPY_SEND
|
||||
|
||||
// name is the same because send rights are coalesced
|
||||
let kr = mach_port_insert_right(mach_task_self_, _name, _name, mach_msg_type_name_t(how))
|
||||
if kr == KERN_INVALID_CAPABILITY {
|
||||
throw Mach.PortRightError.deadName
|
||||
}
|
||||
_machPrecondition(kr)
|
||||
|
||||
return Mach.Port(name: _name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension Mach.Port where RightType == Mach.SendOnceRight {
|
||||
/// Transfer ownership of the underlying port right to the caller.
|
||||
///
|
||||
/// Returns the Mach port name representing the right.
|
||||
///
|
||||
/// This operation liberates the right from management by the Mach.Port,
|
||||
/// and the underlying right will no longer be automatically deallocated.
|
||||
///
|
||||
/// After this function completes, the Mach.Port is destroyed and no longer
|
||||
/// usable.
|
||||
@inlinable
|
||||
public __consuming func relinquish() -> mach_port_name_t {
|
||||
return _name
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -7,7 +7,7 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension String {
|
||||
/// Creates a string by interpreting the null-terminated platform string as
|
||||
/// UTF-8 on Unix and UTF-16 on Windows.
|
||||
@@ -164,6 +164,7 @@ extension String {
|
||||
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension CInterop.PlatformChar {
|
||||
internal var _platformCodeUnit: CInterop.PlatformUnicodeEncoding.CodeUnit {
|
||||
#if os(Windows)
|
||||
@@ -173,6 +174,8 @@ extension CInterop.PlatformChar {
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension CInterop.PlatformUnicodeEncoding.CodeUnit {
|
||||
internal var _platformChar: CInterop.PlatformChar {
|
||||
#if os(Windows)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
extension CInterop {
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
/// The C `int` type
|
||||
public typealias Signal = CInt
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
/// hangup
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGHUP: CInterop.Signal { SIGHUP }
|
||||
/// interrupt
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGINT: CInterop.Signal { SIGINT }
|
||||
/// quit
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGQUIT: CInterop.Signal { SIGQUIT }
|
||||
/// illegal instruction (not reset when caught)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGILL: CInterop.Signal { SIGILL }
|
||||
/// trace trap (not reset when caught)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGTRAP: CInterop.Signal { SIGTRAP }
|
||||
/// abort()
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGABRT: CInterop.Signal { SIGABRT }
|
||||
#if false
|
||||
/// pollable event
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGPOLL: CInterop.Signal { SIGPOLL }
|
||||
#endif
|
||||
/// compatibility
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGIOT: CInterop.Signal { SIGIOT }
|
||||
/// EMT instruction
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGEMT: CInterop.Signal { SIGEMT }
|
||||
/// floating point exception
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGFPE: CInterop.Signal { SIGFPE }
|
||||
/// kill (cannot be caught or ignored)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGKILL: CInterop.Signal { SIGKILL }
|
||||
/// bus error
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGBUS: CInterop.Signal { SIGBUS }
|
||||
/// segmentation violation
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGSEGV: CInterop.Signal { SIGSEGV }
|
||||
/// bad argument to system call
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGSYS: CInterop.Signal { SIGSYS }
|
||||
/// write on a pipe with no one to read it
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGPIPE: CInterop.Signal { SIGPIPE }
|
||||
/// alarm clock
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGALRM: CInterop.Signal { SIGALRM }
|
||||
/// software termination signal from kill
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGTERM: CInterop.Signal { SIGTERM }
|
||||
/// urgent condition on IO channel
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGURG: CInterop.Signal { SIGURG }
|
||||
/// sendable stop signal not from tty
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGSTOP: CInterop.Signal { SIGSTOP }
|
||||
/// stop signal from tty
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGTSTP: CInterop.Signal { SIGTSTP }
|
||||
/// continue a stopped process
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGCONT: CInterop.Signal { SIGCONT }
|
||||
/// to parent on child stop or exit
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGCHLD: CInterop.Signal { SIGCHLD }
|
||||
/// to readers pgrp upon background tty read
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGTTIN: CInterop.Signal { SIGTTIN }
|
||||
/// like TTIN for output if (tp->t_local<OSTOP)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGTTOU: CInterop.Signal { SIGTTOU }
|
||||
/// input/output possible signal
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGIO: CInterop.Signal { SIGIO }
|
||||
/// exceeded CPU time limit
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGXCPU: CInterop.Signal { SIGXCPU }
|
||||
/// exceeded file size limit
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGXFSZ: CInterop.Signal { SIGXFSZ }
|
||||
/// virtual time alarm
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGVTALRM: CInterop.Signal { SIGVTALRM }
|
||||
/// profiling time alarm
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGPROF: CInterop.Signal { SIGPROF }
|
||||
/// window size changes
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGWINCH: CInterop.Signal { SIGWINCH }
|
||||
/// information request
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGINFO: CInterop.Signal { SIGINFO }
|
||||
/// user defined signal 1
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGUSR1: CInterop.Signal { SIGUSR1 }
|
||||
/// user defined signal 2
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SIGUSR2: CInterop.Signal { SIGUSR2 }
|
||||
#endif
|
||||
@@ -0,0 +1,296 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
public struct Signal: RawRepresentable {
|
||||
/// The raw C signal value.
|
||||
@_alwaysEmitIntoClient
|
||||
public let rawValue: CInterop.Signal
|
||||
|
||||
/// Creates a strongly typed signal from a raw C signal value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: CInterop.Signal) { self.rawValue = rawValue }
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
extension Signal {
|
||||
/// The corresponding C signal is `SIGHUP`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var hangup: Self { Self(rawValue: _SIGHUP) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "hangup")
|
||||
internal static var SIGHUP: Self { hangup }
|
||||
|
||||
/// The corresponding C signal is `SIGINT`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var interrupt: Self { Self(rawValue: _SIGINT) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "interrupt")
|
||||
internal static var SIGINT: Self { interrupt }
|
||||
|
||||
/// The corresponding C signal is `SIGQUIT`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var quit: Self { Self(rawValue: _SIGQUIT) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "quit")
|
||||
internal static var SIGQUIT: Self { quit }
|
||||
|
||||
/// The corresponding C signal is `SIGILL`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var illegalInstruction: Self { Self(rawValue: _SIGILL) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "illegalInstruction")
|
||||
internal static var SIGILL: Self { illegalInstruction }
|
||||
|
||||
/// The corresponding C signal is `SIGTRAP`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var trap: Self { Self(rawValue: _SIGTRAP) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "trap")
|
||||
internal static var SIGTRAP: Self { trap }
|
||||
|
||||
/// The corresponding C signal is `SIGABRT`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var abort: Self { Self(rawValue: _SIGABRT) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "abort")
|
||||
internal static var SIGABRT: Self { abort }
|
||||
|
||||
#if false
|
||||
/// The corresponding C signal is `SIGPOLL`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var pollableEvent: Self { Self(rawValue: _SIGPOLL) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "pollableEvent")
|
||||
internal static var SIGPOLL: Self { pollableEvent }
|
||||
#endif
|
||||
|
||||
/// The corresponding C signal is `SIGIOT`.
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, deprecated, renamed: "abort")
|
||||
internal static var inputOutputTrap: Self { Self(rawValue: _SIGIOT) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, deprecated)
|
||||
@available(*, unavailable, renamed: "inputOutputTrap")
|
||||
internal static var SIGIOT: Self { inputOutputTrap }
|
||||
|
||||
/// The corresponding C signal is `SIGEMT`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var emulatorTrap: Self { Self(rawValue: _SIGEMT) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "emulatorTrap")
|
||||
internal static var SIGEMT: Self { emulatorTrap }
|
||||
|
||||
/// The corresponding C signal is `SIGFPE`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var floatingPointException: Self { Self(rawValue: _SIGFPE) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "floatingPointException")
|
||||
internal static var SIGFPE: Self { floatingPointException }
|
||||
|
||||
/// The corresponding C signal is `SIGKILL`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var kill: Self { Self(rawValue: _SIGKILL) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "kill")
|
||||
internal static var SIGKILL: Self { kill }
|
||||
|
||||
/// The corresponding C signal is `SIGBUS`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var busError: Self { Self(rawValue: _SIGBUS) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "busError")
|
||||
internal static var SIGBUS: Self { busError }
|
||||
|
||||
/// The corresponding C signal is `SIGSEGV`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var segmentationViolation: Self { Self(rawValue: _SIGSEGV) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "segmentationViolation")
|
||||
internal static var SIGSEGV: Self { segmentationViolation }
|
||||
|
||||
/// The corresponding C signal is `SIGSYS`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var badSystemCall: Self { Self(rawValue: _SIGSYS) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "badSystemCall")
|
||||
internal static var SIGSYS: Self { badSystemCall }
|
||||
|
||||
/// The corresponding C signal is `SIGPIPE`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var brokenPipe: Self { Self(rawValue: _SIGPIPE) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "brokenPipe")
|
||||
internal static var SIGPIPE: Self { brokenPipe }
|
||||
|
||||
/// The corresponding C signal is `SIGALRM`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var alarm: Self { Self(rawValue: _SIGALRM) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "alarm")
|
||||
internal static var SIGALRM: Self { alarm }
|
||||
|
||||
/// The corresponding C signal is `SIGTERM`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var terminationRequest: Self { Self(rawValue: _SIGTERM) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "terminationRequest")
|
||||
internal static var SIGTERM: Self { terminationRequest }
|
||||
|
||||
/// The corresponding C signal is `SIGURG`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var urgentInputOutput: Self { Self(rawValue: _SIGURG) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "urgentInputOutput")
|
||||
internal static var SIGURG: Self { urgentInputOutput }
|
||||
|
||||
/// The corresponding C signal is `SIGSTOP`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var stop: Self { Self(rawValue: _SIGSTOP) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "stop")
|
||||
internal static var SIGSTOP: Self { stop }
|
||||
|
||||
/// The corresponding C signal is `SIGTSTP`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var interactiveStop: Self { Self(rawValue: _SIGTSTP) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "interactiveStop")
|
||||
internal static var SIGTSTP: Self { interactiveStop }
|
||||
|
||||
/// The corresponding C signal is `SIGCONT`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var `continue`: Self { Self(rawValue: _SIGCONT) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "continue")
|
||||
internal static var SIGCONT: Self { `continue` }
|
||||
|
||||
/// The corresponding C signal is `SIGCHLD`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var childStopOrExit: Self { Self(rawValue: _SIGCHLD) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "childStopOrExit")
|
||||
internal static var SIGCHLD: Self { childStopOrExit }
|
||||
|
||||
/// The corresponding C signal is `SIGTTIN`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var backgroundRead: Self { Self(rawValue: _SIGTTIN) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "backgroundRead")
|
||||
internal static var SIGTTIN: Self { backgroundRead }
|
||||
|
||||
/// The corresponding C signal is `SIGTTOU`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var backgroundWrite: Self { Self(rawValue: _SIGTTOU) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "backgroundWrite")
|
||||
internal static var SIGTTOU: Self { backgroundWrite }
|
||||
|
||||
/// The corresponding C signal is `SIGIO`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var possibleInputOutput: Self { Self(rawValue: _SIGIO) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "possibleInputOutput")
|
||||
internal static var SIGIO: Self { possibleInputOutput }
|
||||
|
||||
/// The corresponding C signal is `SIGXCPU`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var exceededCPUTimeLimit: Self { Self(rawValue: _SIGXCPU) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "exceededCPUTimeLimit")
|
||||
internal static var SIGXCPU: Self { exceededCPUTimeLimit }
|
||||
|
||||
/// The corresponding C signal is `SIGXFSZ`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var exceededFileSizeLimit: Self { Self(rawValue: _SIGXFSZ) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "exceededFileSizeLimit")
|
||||
internal static var SIGXFSZ: Self { exceededFileSizeLimit }
|
||||
|
||||
/// The corresponding C signal is `SIGVTALRM`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var virtualAlarm: Self { Self(rawValue: _SIGVTALRM) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "virtualAlarm")
|
||||
internal static var SIGVTALRM: Self { virtualAlarm }
|
||||
|
||||
/// The corresponding C signal is `SIGPROF`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var profilingAlarm: Self { Self(rawValue: _SIGPROF) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "profilingAlarm")
|
||||
internal static var SIGPROF: Self { profilingAlarm }
|
||||
|
||||
/// The corresponding C signal is `SIGWINCH`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var windowSizeChange: Self { Self(rawValue: _SIGWINCH) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "windowSizeChange")
|
||||
internal static var SIGWINCH: Self { windowSizeChange }
|
||||
|
||||
/// The corresponding C signal is `SIGINFO`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var informationRequest: Self { Self(rawValue: _SIGINFO) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "informationRequest")
|
||||
internal static var SIGINFO: Self { informationRequest }
|
||||
|
||||
/// The corresponding C signal is `SIGUSR1`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var userDefined1: Self { Self(rawValue: _SIGUSR1) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "userDefined1")
|
||||
internal static var SIGUSR1: Self { userDefined1 }
|
||||
|
||||
/// The corresponding C signal is `SIGUSR2`.
|
||||
@_alwaysEmitIntoClient
|
||||
internal static var userDefined2: Self { Self(rawValue: _SIGUSR2) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "userDefined2")
|
||||
internal static var SIGUSR2: Self { userDefined2 }
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
public struct SignalSet {
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Rauhul Varma on 6/16/23.
|
||||
//
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// FIXME: this is wrong and should be done with wrapping fcntrl
|
||||
// FIXME: go through and figure out the right way to express `at` methods
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FileDescriptor {
|
||||
public struct ControlFlags {
|
||||
let rawValue: Int32
|
||||
// Test stub
|
||||
public static var none: ControlFlags = ControlFlags(rawValue: 0)
|
||||
// Out of scope of this sketch
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
|
||||
// FIXME: Document
|
||||
@frozen
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
public struct FileFlags: OptionSet, Hashable, Codable {
|
||||
/// The raw C file flags.
|
||||
@_alwaysEmitIntoClient
|
||||
public let rawValue: CInterop.FileFlags
|
||||
|
||||
/// Create a strongly-typed file flags from a raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: CInterop.FileFlags) { self.rawValue = rawValue }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
private init(_ raw: CInterop.FileFlags) { self.init(rawValue: raw) }
|
||||
|
||||
/// Do not dump the file. Modifiable by file owner or super-user.
|
||||
///
|
||||
/// The corresponding C constant is `UF_NODUMP`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var noDump: FileFlags { FileFlags(_UF_NODUMP) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "noDump")
|
||||
public static var UF_NODUMP: FileFlags { noDump }
|
||||
|
||||
/// The file may not be changed. Modifiable by file owner or super-user.
|
||||
///
|
||||
/// The corresponding C constant is `UF_IMMUTABLE`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var immutable: FileFlags { FileFlags(_UF_IMMUTABLE) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "immutable")
|
||||
public static var UF_IMMUTABLE: FileFlags { immutable }
|
||||
|
||||
/// The file may only be appended to. Modifiable by file owner or super-user.
|
||||
///
|
||||
/// The corresponding C constant is `UF_APPEND`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var appendOnly: FileFlags { FileFlags(_UF_APPEND) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "appendOnly")
|
||||
public static var UF_APPEND: FileFlags { appendOnly }
|
||||
|
||||
/// The directory is opaque when viewed through a union stack. Modifiable by file owner or super-user.
|
||||
///
|
||||
/// The corresponding C constant is `UF_OPAQUE`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var opaque: FileFlags { FileFlags(_UF_OPAQUE) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "opaque")
|
||||
public static var UF_OPAQUE: FileFlags { opaque }
|
||||
|
||||
// #if os(FreeBSD)
|
||||
// /// The file may not be removed or renamed. Modifiable by file owner or super-user.
|
||||
// ///
|
||||
// /// The corresponding C constant is `UF_NOUNLINK`
|
||||
// @_alwaysEmitIntoClient
|
||||
// public static var noUnlink: FileFlags { FileFlags(_UF_NOUNLINK) }
|
||||
//
|
||||
// @_alwaysEmitIntoClient
|
||||
// @available(*, unavailable, renamed: "noUnlink")
|
||||
// public static var UF_NOUNLINK: FileFlags { noUnlink }
|
||||
// #endif
|
||||
|
||||
/// The file is compressed (some file-systems). Modifiable by file owner or super-user.
|
||||
///
|
||||
/// The corresponding C constant is `UF_COMPRESSED`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var compressed: FileFlags { FileFlags(_UF_COMPRESSED) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "compressed")
|
||||
public static var UF_COMPRESSED: FileFlags { compressed }
|
||||
|
||||
/// No notifications will be issued for deletes or renames. Modifiable by file owner or super-user.
|
||||
///
|
||||
/// The corresponding C constant is `UF_TRACKED`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var tracked: FileFlags { FileFlags(_UF_TRACKED) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "tracked")
|
||||
public static var UF_TRACKED: FileFlags { tracked }
|
||||
|
||||
/// The file requires entitlement required for reading and writing. Modifiable by file owner or super-user.
|
||||
///
|
||||
/// The corresponding C constant is `UF_DATAVAULT`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var dataVault: FileFlags { FileFlags(_UF_DATAVAULT) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "dataVault")
|
||||
public static var UF_DATAVAULT: FileFlags { dataVault }
|
||||
|
||||
/// The file or directory is not intended to be displayed to the user. Modifiable by file owner or super-user.
|
||||
///
|
||||
/// The corresponding C constant is `UF_HIDDEN`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var hidden: FileFlags { FileFlags(_UF_HIDDEN) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "hidden")
|
||||
public static var UF_HIDDEN: FileFlags { hidden }
|
||||
|
||||
/// The file has been archived. Only modifiable by the super-user.
|
||||
///
|
||||
/// The corresponding C constant is `SF_ARCHIVED`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var superUserArchived: FileFlags { FileFlags(_SF_ARCHIVED) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "superUserArchived")
|
||||
public static var SF_ARCHIVED: FileFlags { superUserArchived }
|
||||
|
||||
/// The file may not be changed. Only modifiable by the super-user.
|
||||
///
|
||||
/// The corresponding C constant is `SF_IMMUTABLE`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var superUserImmutable: FileFlags { FileFlags(_SF_IMMUTABLE) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "superUserImmutable")
|
||||
public static var SF_IMMUTABLE: FileFlags { superUserImmutable }
|
||||
|
||||
/// The file may only be appended to. Only modifiable by the super-user.
|
||||
///
|
||||
/// The corresponding C constant is `SF_APPEND`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var superUserAppend: FileFlags { FileFlags(_SF_APPEND) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "superUserAppend")
|
||||
public static var SF_APPEND: FileFlags { superUserAppend }
|
||||
|
||||
/// The file requires entitlement required for reading and writing. Only modifiable by the super-user.
|
||||
///
|
||||
/// The corresponding C constant is `SF_RESTRICTED`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var superUserRestricted: FileFlags { FileFlags(_SF_RESTRICTED) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "superUserRestricted")
|
||||
public static var SF_RESTRICTED: FileFlags { superUserRestricted }
|
||||
|
||||
/// The file may not be removed, renamed or mounted on. Only modifiable by the super-user.
|
||||
///
|
||||
/// The corresponding C constant is `SF_NOUNLINK`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var superUserNoUnlink: FileFlags { FileFlags(_SF_NOUNLINK) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "superUserNoUnlink")
|
||||
public static var SF_NOUNLINK: FileFlags { superUserNoUnlink }
|
||||
|
||||
// #if os(FreeBSD)
|
||||
// /// The file is a snapshot file. Only modifiable by the super-user.
|
||||
// ///
|
||||
// /// The corresponding C constant is `SF_SNAPSHOT`
|
||||
// @_alwaysEmitIntoClient
|
||||
// public static var superUserSnapshot: FileFlags { FileFlags(_SF_SNAPSHOT) }
|
||||
//
|
||||
// @_alwaysEmitIntoClient
|
||||
// @available(*, unavailable, renamed: "superUserSnapshot")
|
||||
// public static var SF_SNAPSHOT: FileFlags { superUserSnapshot }
|
||||
// #endif
|
||||
|
||||
/// The file is a firmlink. Only modifiable by the super-user.
|
||||
///
|
||||
/// The corresponding C constant is `SF_FIRMLINK`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var superUserFirmlink: FileFlags { FileFlags(_SF_FIRMLINK) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "superUserFirmlink")
|
||||
public static var SF_FIRMLINK: FileFlags { superUserFirmlink }
|
||||
|
||||
/// The file is a dataless placeholder. The system will attempt to materialize it when accessed according to the dataless file materialization policy of the accessing thread or process. Cannot be modified in user-space.
|
||||
///
|
||||
/// The corresponding C constant is `SF_DATALESS`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var kernelDataless: FileFlags { FileFlags(_SF_DATALESS) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "kernelDataless")
|
||||
public static var SF_DATALESS: FileFlags { kernelDataless }
|
||||
}
|
||||
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FileFlags
|
||||
: CustomStringConvertible, CustomDebugStringConvertible
|
||||
{
|
||||
/// A textual representation of the file permissions.
|
||||
@inline(never)
|
||||
public var description: String {
|
||||
let descriptions: [(Element, StaticString)] = [
|
||||
(.noDump, ".noDump"),
|
||||
(.immutable, ".immutable"),
|
||||
(.appendOnly, ".appendOnly"),
|
||||
(.opaque, ".opaque"),
|
||||
(.compressed, ".compressed"),
|
||||
(.tracked, ".tracked"),
|
||||
(.dataVault, ".dataVault"),
|
||||
(.hidden, ".hidden"),
|
||||
(.superUserArchived, ".superUserArchived"),
|
||||
(.superUserImmutable, ".superUserImmutable"),
|
||||
(.superUserAppend, ".superUserAppend"),
|
||||
(.superUserRestricted, ".superUserRestricted"),
|
||||
(.superUserNoUnlink, ".superUserNoUnlink"),
|
||||
(.superUserFirmlink, ".superUserFirmlink"),
|
||||
(.kernelDataless, ".kernelDataless"),
|
||||
]
|
||||
|
||||
return _buildDescription(descriptions)
|
||||
}
|
||||
|
||||
/// A textual representation of the file permissions, suitable for debugging.
|
||||
public var debugDescription: String { self.description }
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
|
||||
// FIXME: should the subtypes of Mode mask their rawValues to only allow their bits to be changed?
|
||||
|
||||
/// The superset of access permissions and type for a file.
|
||||
///
|
||||
/// The following example creates an instance of the `FileMode` structure
|
||||
/// from a raw octal literal and reads the file type from it:
|
||||
///
|
||||
/// let mode = FileMode(rawValue: 0o140000)
|
||||
/// mode.type == .socket // true
|
||||
@frozen
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
public struct FileMode: RawRepresentable {
|
||||
/// The raw C file mode.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: CInterop.Mode
|
||||
|
||||
/// Create a strongly-typed file mode from a raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: CInterop.Mode) { self.rawValue = rawValue }
|
||||
|
||||
/// Subset of mode for accessing and modifying file permissions.
|
||||
@_alwaysEmitIntoClient
|
||||
public var permissions: FilePermissions {
|
||||
get { FilePermissions(rawValue: rawValue & _MODE_PERMISSIONS) }
|
||||
set { rawValue = (rawValue & ~_MODE_PERMISSIONS) | (newValue.rawValue & _MODE_PERMISSIONS ) }
|
||||
}
|
||||
|
||||
/// Subset of mode for accessing and modifying file type.
|
||||
@_alwaysEmitIntoClient
|
||||
public var type: FileType {
|
||||
get { FileType(rawValue: rawValue & _MODE_TYPE) }
|
||||
set { rawValue = (rawValue & ~_MODE_TYPE) | (newValue.rawValue & _MODE_TYPE ) }
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -17,18 +17,18 @@
|
||||
/// let perms = FilePermissions(rawValue: 0o644)
|
||||
/// perms == [.ownerReadWrite, .groupRead, .otherRead] // true
|
||||
@frozen
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
public struct FilePermissions: OptionSet, Hashable, Codable {
|
||||
/// The raw C file permissions.
|
||||
@_alwaysEmitIntoClient
|
||||
public let rawValue: CInterop.Mode
|
||||
public let rawValue: CModeT
|
||||
|
||||
/// Create a strongly-typed file permission from a raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: CInterop.Mode) { self.rawValue = rawValue }
|
||||
public init(rawValue: CModeT) { self.rawValue = rawValue }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
private init(_ raw: CInterop.Mode) { self.init(rawValue: raw) }
|
||||
private init(_ raw: CModeT) { self.init(rawValue: raw) }
|
||||
|
||||
/// Indicates that other users have read-only permission.
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -135,7 +135,7 @@ public struct FilePermissions: OptionSet, Hashable, Codable {
|
||||
public static var saveText: FilePermissions { FilePermissions(0o1000) }
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
extension FilePermissions
|
||||
: CustomStringConvertible, CustomDebugStringConvertible
|
||||
{
|
||||
@@ -175,3 +175,7 @@ extension FilePermissions
|
||||
/// A textual representation of the file permissions, suitable for debugging.
|
||||
public var debugDescription: String { self.description }
|
||||
}
|
||||
|
||||
#if compiler(>=5.5) && canImport(_Concurrency)
|
||||
extension FilePermissions: Sendable {}
|
||||
#endif
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
|
||||
// FIXME: Document
|
||||
@frozen
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
public struct FileStatus: RawRepresentable {
|
||||
/// The raw C file status.
|
||||
@_alwaysEmitIntoClient
|
||||
public let rawValue: CInterop.Stat
|
||||
|
||||
/// Create a strongly-typed file status from a raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: CInterop.Stat) { self.rawValue = rawValue }
|
||||
|
||||
// FIXME: replace with swift type DeviceID that splits out major/minor
|
||||
/// ID of device containing file.
|
||||
@_alwaysEmitIntoClient
|
||||
public var deviceID: CInterop.DeviceID { rawValue.st_dev }
|
||||
|
||||
/// Mode of file.
|
||||
@_alwaysEmitIntoClient
|
||||
public var mode: FileMode { FileMode(rawValue: rawValue.st_mode) }
|
||||
|
||||
/// Number of hard links.
|
||||
@_alwaysEmitIntoClient
|
||||
public var hardLinkCount: CInterop.NumberOfLinks { rawValue.st_nlink }
|
||||
|
||||
/// File serial number.
|
||||
@_alwaysEmitIntoClient
|
||||
public var inodeNumber: CInterop.INodeNumber { rawValue.st_ino }
|
||||
|
||||
/// User ID of the file.
|
||||
@_alwaysEmitIntoClient
|
||||
public var userID: CInterop.UserID { rawValue.st_uid }
|
||||
|
||||
/// Group ID of the file.
|
||||
@_alwaysEmitIntoClient
|
||||
public var groupID: CInterop.GroupID { rawValue.st_gid }
|
||||
|
||||
/// Device ID.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rDeviceID: CInterop.DeviceID { rawValue.st_rdev }
|
||||
|
||||
/// Time of last access.
|
||||
@_alwaysEmitIntoClient
|
||||
public var accessTime: TimeSpecification { TimeSpecification(rawValue: rawValue.st_atimespec) }
|
||||
|
||||
/// Time of last data modification.
|
||||
@_alwaysEmitIntoClient
|
||||
public var modifyTime: TimeSpecification { TimeSpecification(rawValue: rawValue.st_mtimespec) }
|
||||
|
||||
/// Time of last status change.
|
||||
@_alwaysEmitIntoClient
|
||||
public var statusChangeTime: TimeSpecification { TimeSpecification(rawValue: rawValue.st_ctimespec) }
|
||||
|
||||
/// Time of file creation.
|
||||
@_alwaysEmitIntoClient
|
||||
public var creationTime: TimeSpecification { TimeSpecification(rawValue: rawValue.st_birthtimespec) }
|
||||
|
||||
/// File size, in bytes.
|
||||
@_alwaysEmitIntoClient
|
||||
public var fileSize: CInterop.Offset { rawValue.st_size }
|
||||
|
||||
/// Blocks allocated for file.
|
||||
@_alwaysEmitIntoClient
|
||||
public var blockCount: CInterop.BlockCount { rawValue.st_blocks }
|
||||
|
||||
/// Optimal block size for I/O.
|
||||
@_alwaysEmitIntoClient
|
||||
public var blockSize: CInterop.BlockSize { rawValue.st_blksize }
|
||||
|
||||
/// User defined flags for file.
|
||||
@_alwaysEmitIntoClient
|
||||
public var fileFlags: FileFlags { FileFlags(rawValue: rawValue.st_flags) }
|
||||
|
||||
/// File generation number.
|
||||
@_alwaysEmitIntoClient
|
||||
public var generationID: CInterop.GenerationID { rawValue.st_gen }
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
|
||||
// FIXME: Document
|
||||
@frozen
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
public struct FileType: RawRepresentable {
|
||||
/// The raw C file type.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: CInterop.Mode
|
||||
|
||||
/// Create a strongly-typed file type from a raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: CInterop.Mode) { self.rawValue = rawValue }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
private init(_ raw: CInterop.Mode) { self.init(rawValue: raw) }
|
||||
|
||||
/// Named pipe (fifo)
|
||||
///
|
||||
/// The corresponding C constant is `S_IFIFO`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var fifo: FileType { FileType(_S_IFIFO) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "fifo")
|
||||
public static var S_IFIFO: FileType { fifo }
|
||||
|
||||
/// Character special
|
||||
///
|
||||
/// The corresponding C constant is `S_IFCHR`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var characterDevice: FileType { FileType(_S_IFCHR) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "characterDevice")
|
||||
public static var S_IFCHR: FileType { characterDevice }
|
||||
|
||||
/// Directory
|
||||
///
|
||||
/// The corresponding C constant is `S_IFDIR`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var directory: FileType { FileType(_S_IFDIR) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "directory")
|
||||
public static var S_IFDIR: FileType { directory }
|
||||
|
||||
/// Block special
|
||||
///
|
||||
/// The corresponding C constant is `S_IFBLK`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var blockDevice: FileType { FileType(_S_IFBLK) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "blockDevice")
|
||||
public static var S_IFBLK: FileType { blockDevice }
|
||||
|
||||
/// Regular
|
||||
///
|
||||
/// The corresponding C constant is `S_IFREG`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var regular: FileType { FileType(_S_IFREG) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "regular")
|
||||
public static var S_IFREG: FileType { regular }
|
||||
|
||||
/// Symbolic link
|
||||
///
|
||||
/// The corresponding C constant is `S_IFLNK`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var symbolicLink: FileType { FileType(_S_IFLNK) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "symbolicLink")
|
||||
public static var S_IFLNK: FileType { symbolicLink }
|
||||
|
||||
/// Socket
|
||||
///
|
||||
/// The corresponding C constant is `S_IFSOCK`
|
||||
@_alwaysEmitIntoClient
|
||||
public static var socket: FileType { FileType(_S_IFSOCK) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "socket")
|
||||
public static var S_IFSOCK: FileType { socket }
|
||||
|
||||
/// Whiteout
|
||||
///
|
||||
/// The corresponding C constant is `S_IFWHT`
|
||||
@_alwaysEmitIntoClient
|
||||
// FIXME: rename with inclusive language
|
||||
public static var whiteout: FileType { FileType(_S_IFWHT) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "whiteout")
|
||||
public static var S_IFWHT: FileType { whiteout }
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - stat
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FileDescriptor {
|
||||
/// Obtain information about the file pointed to by the FileDescriptor.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - retryOnInterrupt: Whether to retry the read operation
|
||||
/// if it throws ``Errno/interrupted``.
|
||||
/// The default is `true`.
|
||||
/// Pass `false` to try only once and throw an error upon interruption.
|
||||
/// - Returns: A `FileStatus` for the file pointed to by `self`.
|
||||
///
|
||||
/// The corresponding C function is `fstat`.
|
||||
@_alwaysEmitIntoClient
|
||||
public func getFileStatus(
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> FileStatus {
|
||||
try _getFileStatus(
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _getFileStatus(
|
||||
retryOnInterrupt: Bool = true
|
||||
) -> Result<FileStatus, Errno> {
|
||||
var result = CInterop.Stat()
|
||||
return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_fstat(self.rawValue, &result)
|
||||
}.map { FileStatus(rawValue: result) }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - chmod
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FileDescriptor {
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeFileMode(
|
||||
to mode: FileMode,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileMode(
|
||||
to: mode,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileMode(
|
||||
to mode: FileMode,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_fchmod(self.rawValue, mode.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - chown
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FileDescriptor {
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeFileOwner(
|
||||
to owner: (userID: CInterop.UserID, groupID: CInterop.GroupID),
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileOwner(
|
||||
to: owner,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileOwner(
|
||||
to owner: (userID: CInterop.UserID, groupID: CInterop.GroupID),
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_fchown(self.rawValue, owner.userID, owner.groupID)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - chflags
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FileDescriptor {
|
||||
@_alwaysEmitIntoClient
|
||||
internal func changeFileFlags(
|
||||
to flags: FileFlags,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileFlags(
|
||||
to: flags,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileFlags(
|
||||
to flags: FileFlags,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_fchflags(self.rawValue, flags.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - utimens
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FileDescriptor {
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeFileTimes(
|
||||
to times: (access: TimeSpecification, modification: TimeSpecification)?,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileTimes(
|
||||
to: times,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileTimes(
|
||||
to times: (access: TimeSpecification, modification: TimeSpecification)?,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
if let times = times {
|
||||
return withUnsafePointer(to: times) { tuplePtr in
|
||||
tuplePtr.withMemoryRebound(to: CInterop.TimeSpec.self, capacity: 2) {
|
||||
timespecPtr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_futimens(self.rawValue, timespecPtr)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_futimens(self.rawValue, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - umask
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FileMode {
|
||||
@_alwaysEmitIntoClient
|
||||
func updateProcessMask() -> Self {
|
||||
_updateProcessMask()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
func _updateProcessMask() -> Self {
|
||||
Self(rawValue: system_umask(self.rawValue))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,544 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - stat
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FilePath {
|
||||
/// Obtain information about the file pointed to by the FilePath.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - followSymlinks: Whether to follow symlinks.
|
||||
/// The default is `true`.
|
||||
/// - retryOnInterrupt: Whether to retry the read operation
|
||||
/// if it throws ``Errno/interrupted``.
|
||||
/// The default is `true`.
|
||||
/// Pass `false` to try only once and throw an error upon interruption.
|
||||
/// - Returns: A `FileStatus` for the file pointed to by `self`.
|
||||
///
|
||||
/// Read, write or execute permission of the pointed to file is not required,
|
||||
/// but all intermediate directories must be searchable.
|
||||
///
|
||||
/// The corresponding C functions are `stat` and `lstat`.
|
||||
@_alwaysEmitIntoClient
|
||||
public func getFileStatus(
|
||||
followSymlinks: Bool = true,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> FileStatus {
|
||||
try _getFileStatus(
|
||||
followSymlinks: followSymlinks,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _getFileStatus(
|
||||
followSymlinks: Bool,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<FileStatus, Errno> {
|
||||
var result = CInterop.Stat()
|
||||
let fn = followSymlinks ? system_lstat : system_stat
|
||||
return withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
fn(ptr, &result)
|
||||
}.map { FileStatus(rawValue: result) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Obtain information about the file pointed to by the FilePath relative to
|
||||
/// the provided FileDescriptor.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - relativeTo: if `self` is relative, treat it as relative to this file descriptor
|
||||
/// rather than relative to the current working directory.
|
||||
/// - followSymlinks: Whether to follow symlinks.
|
||||
/// The default is `true`.
|
||||
/// - retryOnInterrupt: Whether to retry the read operation
|
||||
/// if it throws ``Errno/interrupted``.
|
||||
/// The default is `true`.
|
||||
/// Pass `false` to try only once and throw an error upon interruption.
|
||||
/// - Returns: A `FileStatus` for the file pointed to by `self`.
|
||||
///
|
||||
/// Read, write or execute permission of the pointed to file is not required,
|
||||
/// but all intermediate directories must be searchable.
|
||||
///
|
||||
/// The corresponding C function is `fstatat`.
|
||||
@_alwaysEmitIntoClient
|
||||
public func getFileStatus(
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> FileStatus {
|
||||
try _getFileStatus(
|
||||
relativeTo: fd,
|
||||
fcntrl: fcntrl,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _getFileStatus(
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<FileStatus, Errno> {
|
||||
var result = CInterop.Stat()
|
||||
return withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_fstatat(fd.rawValue, ptr, &result, fcntrl.rawValue)
|
||||
}.map { FileStatus(rawValue: result) }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - chmod
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FilePath {
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeFileMode(
|
||||
to mode: FileMode,
|
||||
followSymlinks: Bool = true,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileMode(
|
||||
to: mode,
|
||||
followSymlinks: followSymlinks,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileMode(
|
||||
to mode: FileMode,
|
||||
followSymlinks: Bool,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
let fn = followSymlinks ? system_lchmod : system_chmod
|
||||
return withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
fn(ptr, mode.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// valid flags: AT_SYMLINK_NOFOLLOW | AT_REALDEV | AT_FDONLY
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeFileMode(
|
||||
to mode: FileMode,
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileMode(
|
||||
to: mode,
|
||||
relativeTo: fd,
|
||||
fcntrl: fcntrl,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileMode(
|
||||
to mode: FileMode,
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
self.withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_fchmodat(fd.rawValue, ptr, mode.rawValue, fcntrl.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - chown
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FilePath {
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeOwner(
|
||||
to owner: (userID: CInterop.UserID, groupID: CInterop.GroupID),
|
||||
followSymlinks: Bool = true,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeOwner(
|
||||
to: owner,
|
||||
followSymlinks: followSymlinks,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeOwner(
|
||||
to owner: (userID: CInterop.UserID, groupID: CInterop.GroupID),
|
||||
followSymlinks: Bool,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
let fn = followSymlinks ? system_lchown : system_chown
|
||||
return withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
fn(ptr, owner.userID, owner.groupID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeOwner(
|
||||
to owner: (userID: CInterop.UserID, groupID: CInterop.GroupID),
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeOwner(
|
||||
to: owner,
|
||||
relativeTo: fd,
|
||||
fcntrl: fcntrl,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeOwner(
|
||||
to owner: (userID: CInterop.UserID, groupID: CInterop.GroupID),
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
self.withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_fchownat(
|
||||
fd.rawValue, ptr, owner.userID, owner.groupID, fcntrl.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - chflags
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FilePath {
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeFileFlags(
|
||||
to flags: FileFlags,
|
||||
followSymlinks: Bool = true,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileFlags(
|
||||
to: flags,
|
||||
followSymlinks: followSymlinks,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileFlags(
|
||||
to flags: FileFlags,
|
||||
followSymlinks: Bool,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
let fn = followSymlinks ? system_lchflags : system_chflags
|
||||
return withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
fn(ptr, flags.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if os(FreeBSD)
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeFileFlags(
|
||||
to flags: FileFlags,
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileFlags(
|
||||
to: flags,
|
||||
relativeTo: fd,
|
||||
fcntrl: fcntrl,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileFlags(
|
||||
to flags: FileFlags,
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
return withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_chflagsat(fd.rawValue, ptr, flags.rawValue, fcntrl.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - mkfifo
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FilePath {
|
||||
@_alwaysEmitIntoClient
|
||||
public func makeFIFO(
|
||||
withMode mode: FileMode,
|
||||
retryOnInterrupt: Bool = true) throws {
|
||||
try _makeFIFO(
|
||||
withMode: mode,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _makeFIFO(
|
||||
withMode mode: FileMode,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_mkfifo(ptr, mode.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 13.0, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public func makeFIFO(
|
||||
withMode mode: FileMode,
|
||||
relativeTo fd: FileDescriptor,
|
||||
retryOnInterrupt: Bool = true) throws {
|
||||
try _makeFIFO(
|
||||
withMode: mode,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@available(macOS 13.0, *)
|
||||
@usableFromInline
|
||||
internal func _makeFIFO(
|
||||
withMode mode: FileMode,
|
||||
relativeTo fd: FileDescriptor,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_mkfifoat(fd.rawValue, ptr, mode.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - mknod
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FilePath {
|
||||
@_alwaysEmitIntoClient
|
||||
public func makeNode(
|
||||
withMode mode: FileMode,
|
||||
andDeviceID deviceID: CInterop.DeviceID,
|
||||
retryOnInterrupt: Bool = true) throws {
|
||||
try _makeNode(
|
||||
withMode: mode,
|
||||
andDeviceID: deviceID,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _makeNode(
|
||||
withMode mode: FileMode,
|
||||
andDeviceID deviceID: CInterop.DeviceID,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_mknod(ptr, mode.rawValue, deviceID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 13.0, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public func makeNode(
|
||||
withMode mode: FileMode,
|
||||
andDeviceID deviceID: CInterop.DeviceID,
|
||||
relativeTo fd: FileDescriptor,
|
||||
retryOnInterrupt: Bool = true) throws {
|
||||
try _makeNode(
|
||||
withMode: mode,
|
||||
andDeviceID: deviceID,
|
||||
relativeTo: fd,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@available(macOS 13.0, *)
|
||||
@usableFromInline
|
||||
internal func _makeNode(
|
||||
withMode mode: FileMode,
|
||||
andDeviceID deviceID: CInterop.DeviceID,
|
||||
relativeTo fd: FileDescriptor,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_mknodat(fd.rawValue, ptr, mode.rawValue, deviceID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - mkdir
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FilePath {
|
||||
@_alwaysEmitIntoClient
|
||||
public func makeDirectory(
|
||||
withMode mode: FileMode,
|
||||
retryOnInterrupt: Bool = true) throws {
|
||||
try _makeDirectory(
|
||||
withMode: mode,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _makeDirectory(
|
||||
withMode mode: FileMode,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_mkdir(ptr, mode.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 13.0, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public func makeDirectory(
|
||||
withMode mode: FileMode,
|
||||
relativeTo fd: FileDescriptor,
|
||||
retryOnInterrupt: Bool = true) throws {
|
||||
try _makeDirectory(
|
||||
withMode: mode,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@available(macOS 13.0, *)
|
||||
@usableFromInline
|
||||
internal func _makeDirectory(
|
||||
withMode mode: FileMode,
|
||||
relativeTo fd: FileDescriptor,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
withPlatformString { ptr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_mkdirat(fd.rawValue, ptr, mode.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// MARK: - mkdir
|
||||
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
||||
extension FilePath {
|
||||
#if os(FreeBSD)
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeFileTimes(
|
||||
to times: (access: TimeSpecification, modification: TimeSpecification)?,
|
||||
followSymlinks: Bool = true,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileTimes(
|
||||
to: times,
|
||||
followSymlinks: followSymlinks,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileTimes(
|
||||
to times: (access: TimeSpecification, modification: TimeSpecification)?,
|
||||
followSymlinks: Bool,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
let fn = followSymlinks ? system_utimens : system_lutimens
|
||||
return withPlatformString { ptr in
|
||||
if let times = times {
|
||||
return withUnsafePointer(to: times) { tuplePtr in
|
||||
tuplePtr.withMemoryRebound(to: CInterop.TimeSpec.self, capacity: 2) {
|
||||
timespecPtr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
fn(ptr, timespecPtr)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
fn(ptr, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func changeFileTimes(
|
||||
to times: (access: TimeSpecification, modification: TimeSpecification)?,
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
try _changeFileTimes(
|
||||
to: times,
|
||||
relativeTo: fd,
|
||||
fcntrl: fcntrl,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _changeFileTimes(
|
||||
to times: (access: TimeSpecification, modification: TimeSpecification)?,
|
||||
relativeTo fd: FileDescriptor,
|
||||
fcntrl: FileDescriptor.ControlFlags,
|
||||
retryOnInterrupt: Bool
|
||||
) -> Result<Void, Errno> {
|
||||
self.withPlatformString { ptr in
|
||||
if let times = times {
|
||||
return withUnsafePointer(to: times) { tuplePtr in
|
||||
tuplePtr.withMemoryRebound(to: CInterop.TimeSpec.self, capacity: 2) {
|
||||
timespecPtr in
|
||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_utimensat(fd.rawValue, ptr, timespecPtr, fcntrl.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_utimensat(fd.rawValue, ptr, nil, fcntrl.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(Windows)
|
||||
import CSystem
|
||||
import ucrt
|
||||
#endif
|
||||
|
||||
extension CInterop {
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
/// The C `stat` type.
|
||||
public typealias Stat = stat
|
||||
/// The C `mode_t` type.
|
||||
public typealias Mode = mode_t
|
||||
/// The C `uid_t` type.
|
||||
public typealias UserID = uid_t
|
||||
/// The C `gid_t` type.
|
||||
public typealias GroupID = gid_t
|
||||
/// The C `dev_t` type.
|
||||
public typealias DeviceID = dev_t
|
||||
/// The C `nlink_t` type.
|
||||
public typealias NumberOfLinks = nlink_t
|
||||
/// The C `ino_t` type.
|
||||
public typealias INodeNumber = ino_t
|
||||
/// The C `timespec` type.
|
||||
public typealias TimeSpec = timespec
|
||||
/// The C `off_t` type.
|
||||
public typealias Offset = off_t
|
||||
/// The C `blkcnt_t` type.
|
||||
public typealias BlockCount = blkcnt_t
|
||||
/// The C `blksize_t` type.
|
||||
public typealias BlockSize = blksize_t
|
||||
/// The C `UInt32` type.
|
||||
public typealias GenerationID = UInt32
|
||||
/// The C `UInt32` type.
|
||||
public typealias FileFlags = UInt32
|
||||
#endif
|
||||
|
||||
#if os(Windows)
|
||||
public typealias Mode = CInt
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
// MARK: - Mode Masks
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// mode_t: type ugs rwx rwx rwx
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _MODE_PERMISSIONS: CInterop.Mode { 0b0000_111_111_111_111 }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _MODE_TYPE: CInterop.Mode { 0b1111_000_000_000_000 }
|
||||
#endif
|
||||
|
||||
// MARK: - File Type
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _S_IFIFO: CInterop.Mode { S_IFIFO }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _S_IFCHR: CInterop.Mode { S_IFCHR }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _S_IFDIR: CInterop.Mode { S_IFDIR }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _S_IFBLK: CInterop.Mode { S_IFBLK }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _S_IFREG: CInterop.Mode { S_IFREG }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _S_IFLNK: CInterop.Mode { S_IFLNK }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _S_IFSOCK: CInterop.Mode { S_IFSOCK }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _S_IFWHT: CInterop.Mode { S_IFWHT }
|
||||
#endif
|
||||
|
||||
// MARK: - File Flags
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UF_NODUMP: CInterop.FileFlags { UInt32(bitPattern: UF_NODUMP) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UF_IMMUTABLE: CInterop.FileFlags { UInt32(bitPattern: UF_IMMUTABLE) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UF_APPEND: CInterop.FileFlags { UInt32(bitPattern: UF_APPEND) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UF_OPAQUE: CInterop.FileFlags { UInt32(bitPattern: UF_OPAQUE) }
|
||||
|
||||
#if os(FreeBSD)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UF_NOUNLINK: CInterop.FileFlags { UInt32(bitPattern: UF_NOUNLINK) }
|
||||
#endif
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UF_COMPRESSED: CInterop.FileFlags { UInt32(bitPattern: UF_COMPRESSED) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UF_TRACKED: CInterop.FileFlags { UInt32(bitPattern: UF_TRACKED) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UF_DATAVAULT: CInterop.FileFlags { UInt32(bitPattern: UF_DATAVAULT) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UF_HIDDEN: CInterop.FileFlags { UInt32(bitPattern: UF_HIDDEN) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SF_ARCHIVED: CInterop.FileFlags { UInt32(bitPattern: SF_ARCHIVED) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SF_IMMUTABLE: CInterop.FileFlags { UInt32(bitPattern: SF_IMMUTABLE) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SF_APPEND: CInterop.FileFlags { UInt32(bitPattern: SF_APPEND) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SF_RESTRICTED: CInterop.FileFlags { UInt32(bitPattern: SF_RESTRICTED) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SF_NOUNLINK: CInterop.FileFlags { UInt32(bitPattern: SF_NOUNLINK) }
|
||||
|
||||
#if os(FreeBSD)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SF_SNAPSHOT: CInterop.FileFlags { UInt32(bitPattern: SF_SNAPSHOT) }
|
||||
#endif
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SF_FIRMLINK: CInterop.FileFlags { UInt32(bitPattern: SF_FIRMLINK) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SF_DATALESS: CInterop.FileFlags { UInt32(bitPattern: SF_DATALESS) }
|
||||
#endif
|
||||
|
||||
// MARK: - Time Specification
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UTIME_NOW: Int { Int(UTIME_NOW) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _UTIME_OMIT: Int { Int(UTIME_OMIT) }
|
||||
#endif
|
||||
@@ -0,0 +1,296 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
internal func system_stat(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ buf: UnsafeMutablePointer<stat>
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, buf) }
|
||||
#endif
|
||||
return stat(path, buf)
|
||||
}
|
||||
|
||||
internal func system_lstat(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ buf: UnsafeMutablePointer<stat>
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, buf) }
|
||||
#endif
|
||||
return lstat(path, buf)
|
||||
}
|
||||
|
||||
internal func system_fstat(
|
||||
_ fd: Int32,
|
||||
_ buf: UnsafeMutablePointer<stat>
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, buf) }
|
||||
#endif
|
||||
return fstat(fd, buf)
|
||||
}
|
||||
|
||||
internal func system_fstatat(
|
||||
_ fd: Int32,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ buf: UnsafeMutablePointer<stat>,
|
||||
_ flag: Int32
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, path, buf, flag) }
|
||||
#endif
|
||||
return fstatat(fd, path, buf, flag)
|
||||
}
|
||||
|
||||
internal func system_chmod(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, mode) }
|
||||
#endif
|
||||
return chmod(path, mode)
|
||||
}
|
||||
|
||||
internal func system_lchmod(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, mode) }
|
||||
#endif
|
||||
return lchmod(path, mode)
|
||||
}
|
||||
|
||||
internal func system_fchmod(
|
||||
_ fd: Int32,
|
||||
_ mode: CInterop.Mode
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, mode) }
|
||||
#endif
|
||||
return fchmod(fd, mode)
|
||||
}
|
||||
|
||||
internal func system_fchmodat(
|
||||
_ fd: Int32,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode,
|
||||
_ flag: Int32
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, path, mode, flag) }
|
||||
#endif
|
||||
return fchmodat(fd, path, mode, flag)
|
||||
}
|
||||
|
||||
internal func system_chown(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ userID: CInterop.UserID,
|
||||
_ groupID: CInterop.GroupID
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, userID, groupID) }
|
||||
#endif
|
||||
return chown(path, userID, groupID)
|
||||
}
|
||||
|
||||
internal func system_lchown(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ userID: CInterop.UserID,
|
||||
_ groupID: CInterop.GroupID
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, userID, groupID) }
|
||||
#endif
|
||||
return lchown(path, userID, groupID)
|
||||
}
|
||||
|
||||
internal func system_fchown(
|
||||
_ fd: Int32,
|
||||
_ userID: CInterop.UserID,
|
||||
_ groupID: CInterop.GroupID
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, userID, groupID) }
|
||||
#endif
|
||||
return fchown(fd, userID, groupID)
|
||||
}
|
||||
|
||||
internal func system_fchownat(
|
||||
_ fd: Int32,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ userID: CInterop.UserID,
|
||||
_ groupID: CInterop.GroupID,
|
||||
_ flag: Int32
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, path, userID, groupID, flag) }
|
||||
#endif
|
||||
return fchownat(fd, path, userID, groupID, flag)
|
||||
}
|
||||
|
||||
internal func system_chflags(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ flag: UInt32
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, flag) }
|
||||
#endif
|
||||
return chflags(path, flag)
|
||||
}
|
||||
|
||||
internal func system_lchflags(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ flag: UInt32
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, flag) }
|
||||
#endif
|
||||
return lchflags(path, flag)
|
||||
}
|
||||
|
||||
internal func system_fchflags(
|
||||
_ fd: Int32,
|
||||
_ flag: UInt32
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, flag) }
|
||||
#endif
|
||||
return fchflags(fd, flag)
|
||||
}
|
||||
|
||||
internal func system_umask(
|
||||
_ cmask: CInterop.Mode
|
||||
) -> CInterop.Mode {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mockModeT(cmask) }
|
||||
#endif
|
||||
return umask(cmask)
|
||||
}
|
||||
|
||||
internal func system_mkfifo(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, mode) }
|
||||
#endif
|
||||
return mkfifo(path, mode)
|
||||
}
|
||||
|
||||
@available(macOS 13.0, *)
|
||||
internal func system_mkfifoat(
|
||||
_ fd: Int32,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, mode) }
|
||||
#endif
|
||||
return mkfifoat(fd, path, mode)
|
||||
}
|
||||
|
||||
internal func system_mknod(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode,
|
||||
_ deviceID: CInterop.DeviceID
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, mode, deviceID) }
|
||||
#endif
|
||||
return mknod(path, mode, deviceID)
|
||||
}
|
||||
|
||||
@available(macOS 13.0, *)
|
||||
internal func system_mknodat(
|
||||
_ fd: Int32,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode,
|
||||
_ deviceID: CInterop.DeviceID
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, mode, deviceID) }
|
||||
#endif
|
||||
return mknodat(fd, path, mode, deviceID)
|
||||
}
|
||||
|
||||
internal func system_mkdir(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, mode) }
|
||||
#endif
|
||||
return mkdir(path, mode)
|
||||
}
|
||||
|
||||
internal func system_mkdirat(
|
||||
_ fd: Int32,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, path, mode) }
|
||||
#endif
|
||||
return mkdirat(fd, path, mode)
|
||||
}
|
||||
|
||||
#if os(FreeBSD)
|
||||
internal func system_utimens(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ times: UnsafePointer<timespec>?
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, times) }
|
||||
#endif
|
||||
return utimens(path, times)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(FreeBSD)
|
||||
internal func system_lutimens(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ times: UnsafePointer<timespec>?
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(path, times) }
|
||||
#endif
|
||||
return lutimens(path, times)
|
||||
}
|
||||
#endif
|
||||
|
||||
internal func system_utimensat(
|
||||
_ fd: Int32,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ times: UnsafePointer<timespec>?,
|
||||
_ flag: Int32
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, path, times, flag) }
|
||||
#endif
|
||||
return utimensat(fd, path, times, flag)
|
||||
}
|
||||
|
||||
internal func system_futimens(
|
||||
_ fd: Int32,
|
||||
_ times: UnsafePointer<timespec>?
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(fd, times) }
|
||||
#endif
|
||||
return futimens(fd, times)
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
// MARK: - stat
|
||||
// int stat(const char *, struct stat *)
|
||||
// int lstat(const char *, struct stat *)
|
||||
// FilePath.getFileStatus(followSymlinks:) throws Errno -> FileStatus
|
||||
|
||||
// int fstatat(int, const char *, struct stat *, int)
|
||||
// FilePath.getFileStatus(relativeTo:controlFlags:) throws Errno -> FileStatus
|
||||
|
||||
// int fstat(int, struct stat *)
|
||||
// FileDescriptor.getFileStatus() throws Errno -> FileStatus
|
||||
|
||||
|
||||
// MARK: - chmod
|
||||
// int chmod(const char *, mode_t)
|
||||
// int lchmod(const char *, mode_t)
|
||||
// FilePath.changeFileMode(to:followSymlinks) throws Errno -> Void
|
||||
|
||||
// int fchmodat(int, const char *, mode_t, int)
|
||||
// FilePath.changeFileMode(to:relativeTo:controlFlags:) throws Errno -> Void
|
||||
|
||||
// int fchmod(int, mode_t)
|
||||
// FileDescriptor.changeFileMode(to:) throws Errno -> Void
|
||||
|
||||
|
||||
// MARK: - chown
|
||||
// int chown(const char *, uid_t, gid_t)
|
||||
// int lchown(const char *, uid_t, gid_t)
|
||||
// FilePath.changeFileOwner(to:followSymlinks:) throws Errno -> Void
|
||||
|
||||
// int fchownat(int, const char *, uid_t, gid_t, int)
|
||||
// FilePath.changeFileOwner(to:relativeTo:controlFlags:) throws Errno -> Void
|
||||
|
||||
// int fchown(int, uid_t, gid_t)
|
||||
// FileDescriptor.changeFileOwner(to:) throws Errno -> Void
|
||||
|
||||
|
||||
// MARK: - chflags
|
||||
// int chflags(const char *, uint32_t)
|
||||
// int lchflags(const char *, uint32_t)
|
||||
// FilePath.changeFileFlags(to:followSymlinks:) throws Errno -> Void
|
||||
|
||||
// int chflagsat(int, const char *, uint32_t, int) (FreeBSD)
|
||||
// FilePath.changeFileFlags(to:relativeTo:controlFlags:) throws Errno -> Void
|
||||
|
||||
// int fchflags(int, uint32_t)
|
||||
// FileDescriptor.changeFileFlags(to:) throws Errno -> Void
|
||||
|
||||
|
||||
// MARK: - umask
|
||||
// mode_t umask(mode_t)
|
||||
// FileMode.updateProcessMask() -> FileMode
|
||||
|
||||
|
||||
// MARK: - mkfifo
|
||||
// int mkfifo(const char *, mode_t)
|
||||
// FilePath.makeFIFO(withMode:) throws Errno -> Void
|
||||
|
||||
// int mkfifoat(int, const char *, mode_t)
|
||||
// FilePath.makeFIFO(withMode:relativeTo:) throws Errno -> Void
|
||||
|
||||
|
||||
// MARK: - mknod
|
||||
// int mknod(const char *, mode_t, dev_t)
|
||||
// FilePath.makeNode(withMode:andDeviceID:) throws Errno -> Void
|
||||
|
||||
// int mknodat(int, const char *, mode_t, dev_t)
|
||||
// FilePath.makeNode(withMode:andDeviceID:relativeTo:) throws Errno -> Void
|
||||
|
||||
|
||||
// MARK: - mkdir
|
||||
// int mkdir(const char *, mode_t)
|
||||
// FilePath.makeDirectory(withMode:) throws Errno -> Void
|
||||
|
||||
// int mkdirat(int, const char *, mode_t)
|
||||
// FilePath.makeDirectory(withMode:relativeTo:) throws Errno -> Void
|
||||
|
||||
|
||||
// MARK: - utimens
|
||||
// int utimens(const char *, const struct timespec[2])
|
||||
// int lutimens(const char *, const struct timespec[2])
|
||||
// FilePath.changeFileTimes(to:followSymlinks:) throws Errno -> Void
|
||||
|
||||
// int utimensat(int, const char *, const struct timespec[2], int)
|
||||
// FilePath.changeFileTimes(to:relativeTo:controlFlags:) throws Errno -> Void
|
||||
|
||||
// int futimens(int, const struct timespec[2])
|
||||
// FileDescriptor.changeFileTimes(to:) throws Errno -> Void
|
||||
@@ -288,6 +288,11 @@ extension SystemString {
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=5.5) && canImport(_Concurrency)
|
||||
extension SystemChar: Sendable {}
|
||||
extension SystemString: Sendable {}
|
||||
#endif
|
||||
|
||||
// TODO: SystemString should use a COW-interchangable storage form rather
|
||||
// than array, so you could "borrow" the storage from a non-bridged String
|
||||
// or Data or whatever
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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
|
||||
*/
|
||||
|
||||
// Specification:
|
||||
// - POSIX.1-2017
|
||||
// - IEEE Std 1003.1™-2017
|
||||
// - The Open Group Technical Standard Base Specifications, Issue 7
|
||||
// Reference: https://pubs.opengroup.org/onlinepubs/9699919799
|
||||
// sys/time.h
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
/// A system provided timer which can be configured to run after an initial
|
||||
/// delay and subsequent interval.
|
||||
///
|
||||
/// The corresponding C type is `int`.
|
||||
@frozen
|
||||
public struct IntervalTimer: RawRepresentable {
|
||||
public typealias RawValue = CInterop.IntervalTimer
|
||||
|
||||
/// The raw C ITIMER value.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: RawValue
|
||||
|
||||
/// Create a strongly-typed interval timer from a raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: RawValue) { self.rawValue = rawValue }
|
||||
}
|
||||
|
||||
extension IntervalTimer {
|
||||
/// A timer which decrements in real time.
|
||||
///
|
||||
/// A `SIGALRM` signal is delivered when this timer expires.
|
||||
///
|
||||
/// The corresponding C timer is `ITIMER_REAL`.
|
||||
@_alwaysEmitIntoClient
|
||||
public static var real: Self { Self(rawValue: _ITIMER_REAL) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "real")
|
||||
public static var ITIMER_REAL: Self { real }
|
||||
|
||||
/// A timer which decrements in process virtual time.
|
||||
///
|
||||
/// The timer runs only while the process is executing. A `SIGVTALRM` signal
|
||||
/// is delivered when this timer expires.
|
||||
///
|
||||
/// The corresponding C timer is `ITIMER_VIRTUAL`.
|
||||
@_alwaysEmitIntoClient
|
||||
public static var virtual: Self { Self(rawValue: _ITIMER_VIRTUAL) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "virtual")
|
||||
public static var ITIMER_VIRTUAL: Self { virtual }
|
||||
|
||||
/// A timer which decrements both in process virtual time and when the system
|
||||
/// is running on behalf of the process.
|
||||
///
|
||||
/// This timer is designed to be used by interpreters when statistically
|
||||
/// profiling the execution of interpreted programs. A `SIGPROF` signal
|
||||
/// is delivered when this timer expires. Because this signal may interrupt
|
||||
/// in-progress system calls, programs using this timer must be prepared to
|
||||
/// restart interrupted system calls.
|
||||
///
|
||||
/// The corresponding C timer is `ITIMER_PROF`.
|
||||
@_alwaysEmitIntoClient
|
||||
public static var profile: Self { Self(rawValue: _ITIMER_PROF) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "profile")
|
||||
public static var ITIMER_PROF: Self { profile }
|
||||
}
|
||||
|
||||
extension IntervalTimer {
|
||||
/// Returns the current value for the timer.
|
||||
///
|
||||
/// - Returns: The current value of the timer.
|
||||
///
|
||||
/// The corresponding C function is `getitimer`.
|
||||
@_alwaysEmitIntoClient
|
||||
public func get() throws -> IntervalTimerValue {
|
||||
try _get().get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _get() -> Result<IntervalTimerValue, Errno> {
|
||||
var currentValue = itimerval()
|
||||
return nothingOrErrno(retryOnInterrupt: false) {
|
||||
system_getitimer(self.rawValue, ¤tValue)
|
||||
}
|
||||
.map {
|
||||
IntervalTimerValue(rawValue: currentValue)
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a timer to run on specified value.
|
||||
///
|
||||
/// If the initial delay is non-zero, it indicates the time to the next timer
|
||||
/// expiration. If the interval is non-zero, it specifies a value to be used
|
||||
/// in reloading initial delay when the timer expires.
|
||||
///
|
||||
/// Setting the initial delay to `0` disables the timer.
|
||||
/// Setting the interval to `0` causes a timer to be disabled after its next
|
||||
/// expiration, assuming initial delay is non-zero.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newValue: The new interval to run the timer.
|
||||
/// - Returns: The previous value of the timer.
|
||||
///
|
||||
/// The corresponding C function is `setitimer`.
|
||||
@_alwaysEmitIntoClient
|
||||
@discardableResult
|
||||
public func set(newValue: IntervalTimerValue) throws -> IntervalTimerValue {
|
||||
try _set(newValue: newValue).get()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _set(
|
||||
newValue: IntervalTimerValue
|
||||
) -> Result<IntervalTimerValue, Errno> {
|
||||
var newValue = newValue.rawValue
|
||||
var oldValue = itimerval()
|
||||
return nothingOrErrno(retryOnInterrupt: false) {
|
||||
system_setitimer(self.rawValue, &newValue, &oldValue)
|
||||
}
|
||||
.map {
|
||||
IntervalTimerValue(rawValue: oldValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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
|
||||
*/
|
||||
|
||||
// Specification:
|
||||
// - POSIX.1-2017
|
||||
// - IEEE Std 1003.1™-2017
|
||||
// - The Open Group Technical Standard Base Specifications, Issue 7
|
||||
// Reference: https://pubs.opengroup.org/onlinepubs/9699919799
|
||||
// sys/time.h
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
/// The value an interval timer is configured to run on.
|
||||
///
|
||||
/// The corresponding C type is `itimerval`.
|
||||
@frozen
|
||||
public struct IntervalTimerValue: RawRepresentable {
|
||||
public typealias RawValue = CInterop.IntervalTimerValue
|
||||
|
||||
/// The raw C itimerval type.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: RawValue
|
||||
|
||||
/// Create a strongly-typed internal timer value from a raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: RawValue) { self.rawValue = rawValue }
|
||||
}
|
||||
|
||||
extension IntervalTimerValue {
|
||||
/// The initial delay before running the timer.
|
||||
///
|
||||
/// Setting the initial delay to `0` disables the timer.
|
||||
///
|
||||
/// The corresponding C property is `it_value`.
|
||||
@_alwaysEmitIntoClient
|
||||
public var initialDelay: TimeValue {
|
||||
get { TimeValue(rawValue: rawValue.it_value) }
|
||||
set { rawValue.it_value = newValue.rawValue }
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "initialDelay")
|
||||
public var it_value: TimeValue { initialDelay }
|
||||
|
||||
/// The subsequent delay after the initial delay before running the timer.
|
||||
///
|
||||
/// Setting the interval to `0` causes a timer to be disabled after its next
|
||||
/// expiration, assuming initial delay is non-zero.
|
||||
///
|
||||
/// The corresponding C property is `it_interval`.
|
||||
@_alwaysEmitIntoClient
|
||||
public var interval: TimeValue {
|
||||
get { TimeValue(rawValue: rawValue.it_interval) }
|
||||
set { rawValue.it_interval = newValue.rawValue }
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "interval")
|
||||
public var it_interval: TimeValue { interval }
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
extension CInterop {
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
/// The C `time_t` type
|
||||
public typealias Time = time_t
|
||||
/// The C `suseconds_t` type
|
||||
public typealias Microseconds = suseconds_t
|
||||
/// The C `long` type
|
||||
public typealias Nanoseconds = CLong
|
||||
/// The C `timeval` type
|
||||
public typealias TimeValue = timeval
|
||||
/// The C `timespec` type
|
||||
public typealias TimeSpecification = timespec
|
||||
/// The C `itimerval` type
|
||||
public typealias IntervalTimerValue = itimerval
|
||||
/// The C type of `ITIMER` values
|
||||
public typealias IntervalTimer = CInt
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _ITIMER_REAL: CInterop.IntervalTimer { ITIMER_REAL }
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _ITIMER_VIRTUAL: CInterop.IntervalTimer { ITIMER_VIRTUAL }
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _ITIMER_PROF: CInterop.IntervalTimer { ITIMER_PROF }
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
// getitimer
|
||||
internal func system_getitimer(
|
||||
_ timer: CInt,
|
||||
_ currentValue: UnsafeMutablePointer<itimerval>
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(timer, currentValue) }
|
||||
#endif
|
||||
return getitimer(timer, currentValue)
|
||||
}
|
||||
|
||||
// setitimer
|
||||
internal func system_setitimer(
|
||||
_ timer: CInt,
|
||||
_ newValue: UnsafePointer<itimerval>,
|
||||
_ oldValue: UnsafeMutablePointer<itimerval>?
|
||||
) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return _mock(timer, newValue, oldValue) }
|
||||
#endif
|
||||
return setitimer(timer, newValue, oldValue)
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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
|
||||
*/
|
||||
|
||||
// Specification:
|
||||
// - POSIX.1-2017
|
||||
// - IEEE Std 1003.1™-2017
|
||||
// - The Open Group Technical Standard Base Specifications, Issue 7
|
||||
// Reference: https://pubs.opengroup.org/onlinepubs/9699919799
|
||||
// time.h
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
/// Represents an elapsed time since a reference point.
|
||||
///
|
||||
/// The corresponding C type is `timeval`.
|
||||
@frozen
|
||||
public struct TimeSpecification: RawRepresentable {
|
||||
public typealias RawValue = CInterop.TimeSpecification
|
||||
|
||||
/// The raw C timeval type.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: RawValue
|
||||
|
||||
/// Create a strongly-typed time value from a raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: RawValue) { self.rawValue = rawValue }
|
||||
}
|
||||
|
||||
extension TimeSpecification {
|
||||
/// The elapsed time in seconds.
|
||||
///
|
||||
/// The corresponding C property is `tv_sec`.
|
||||
@_alwaysEmitIntoClient
|
||||
public var seconds: CInterop.Time {
|
||||
get { rawValue.tv_sec }
|
||||
set { rawValue.tv_sec = newValue }
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "seconds")
|
||||
public var tv_sec: CInterop.Time { seconds }
|
||||
|
||||
/// The remaining elapsed time in nanoseconds.
|
||||
///
|
||||
/// This value must always be in the range [0, 999999999].
|
||||
///
|
||||
/// The corresponding C property is `tv_nsec`.
|
||||
@_alwaysEmitIntoClient
|
||||
public var nanoseconds: CInterop.Nanoseconds {
|
||||
get { rawValue.tv_nsec }
|
||||
set { rawValue.tv_nsec = newValue }
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "nanoseconds")
|
||||
public var tv_nsec: CInterop.Nanoseconds { nanoseconds }
|
||||
}
|
||||
|
||||
extension TimeSpecification {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
private static let attosecondsPerNanoseconds: Int64 = 1_000_000_000
|
||||
|
||||
/// Creates a new value, rounded to the closest possible representation.
|
||||
@_alwaysEmitIntoClient
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public init(_ duration: Duration) {
|
||||
let (seconds, attoseconds) = duration.components
|
||||
let nanoseconds = attoseconds / Self.attosecondsPerNanoseconds
|
||||
self.rawValue = .init(
|
||||
tv_sec: CInterop.Time(truncatingIfNeeded: seconds),
|
||||
tv_nsec: CInterop.Nanoseconds(nanoseconds))
|
||||
}
|
||||
|
||||
/// Creates a new value, if the given duration can be represented exactly.
|
||||
@_alwaysEmitIntoClient
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public init?(exactly duration: Duration) {
|
||||
let (seconds, attoseconds) = duration.components
|
||||
guard
|
||||
let seconds = CInterop.Time(exactly: seconds),
|
||||
attoseconds.isMultiple(of: Self.attosecondsPerNanoseconds)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
let nanoseconds = attoseconds / Self.attosecondsPerNanoseconds
|
||||
self.rawValue = .init(
|
||||
tv_sec: CInterop.Time(truncatingIfNeeded: seconds),
|
||||
tv_nsec: CInterop.Nanoseconds(nanoseconds))
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public var duration: Duration {
|
||||
let seconds = Int64(self.seconds)
|
||||
let attoseconds = Int64(self.nanoseconds) *
|
||||
Self.attosecondsPerNanoseconds
|
||||
return .init(
|
||||
secondsComponent: seconds,
|
||||
attosecondsComponent: attoseconds)
|
||||
}
|
||||
}
|
||||
|
||||
extension TimeSpecification: AdditiveArithmetic {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
private static let nanosecondsPerSecond: CInterop.Nanoseconds = 1_000_000_000
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public static var zero: Self {
|
||||
Self(rawValue: .init(tv_sec: 0, tv_nsec: 0))
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public static func + (lhs: Self, rhs: Self) -> Self {
|
||||
var newValue = TimeSpecification.zero
|
||||
newValue.seconds = lhs.seconds + rhs.seconds
|
||||
newValue.nanoseconds = lhs.nanoseconds + rhs.nanoseconds
|
||||
if (newValue.nanoseconds >= Self.nanosecondsPerSecond) {
|
||||
newValue.seconds += 1
|
||||
newValue.nanoseconds -= Self.nanosecondsPerSecond
|
||||
}
|
||||
return newValue
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public static func - (lhs: Self, rhs: Self) -> Self {
|
||||
var newValue = TimeSpecification.zero
|
||||
newValue.seconds = lhs.seconds - rhs.seconds;
|
||||
newValue.nanoseconds = lhs.nanoseconds - rhs.nanoseconds
|
||||
if (newValue.nanoseconds < 0) {
|
||||
newValue.seconds -= 1
|
||||
newValue.nanoseconds += Self.nanosecondsPerSecond
|
||||
}
|
||||
return newValue
|
||||
}
|
||||
}
|
||||
|
||||
extension TimeSpecification: Comparable {
|
||||
@_alwaysEmitIntoClient
|
||||
public static func < (lhs: Self, rhs: Self) -> Bool {
|
||||
guard lhs.seconds == rhs.seconds else {
|
||||
return lhs.seconds < rhs.seconds
|
||||
}
|
||||
return lhs.nanoseconds < rhs.nanoseconds
|
||||
}
|
||||
}
|
||||
|
||||
extension TimeSpecification: Equatable {
|
||||
@_alwaysEmitIntoClient
|
||||
public static func == (lhs: Self, rhs: Self) -> Bool {
|
||||
lhs.seconds == rhs.seconds && lhs.nanoseconds == rhs.nanoseconds
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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
|
||||
*/
|
||||
|
||||
// Specification:
|
||||
// - POSIX.1-2017
|
||||
// - IEEE Std 1003.1™-2017
|
||||
// - The Open Group Technical Standard Base Specifications, Issue 7
|
||||
// Reference: https://pubs.opengroup.org/onlinepubs/9699919799
|
||||
// sys/time.h
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
/// Represents an elapsed time since a reference point.
|
||||
///
|
||||
/// The corresponding C type is `timeval`.
|
||||
@frozen
|
||||
public struct TimeValue: RawRepresentable {
|
||||
public typealias RawValue = CInterop.TimeValue
|
||||
|
||||
/// The raw C timeval type.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: RawValue
|
||||
|
||||
/// Create a strongly-typed time value from a raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: RawValue) { self.rawValue = rawValue }
|
||||
}
|
||||
|
||||
extension TimeValue {
|
||||
/// The elapsed time in seconds.
|
||||
///
|
||||
/// The corresponding C property is `tv_sec`.
|
||||
@_alwaysEmitIntoClient
|
||||
public var seconds: CInterop.Time {
|
||||
get { rawValue.tv_sec }
|
||||
set { rawValue.tv_sec = newValue }
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "seconds")
|
||||
public var tv_sec: CInterop.Time { seconds }
|
||||
|
||||
/// The remaining elapsed time in microseconds.
|
||||
///
|
||||
/// This value must always be in the range [0, 999999].
|
||||
///
|
||||
/// The corresponding C property is `tv_usec`.
|
||||
@_alwaysEmitIntoClient
|
||||
public var microseconds: CInterop.Microseconds {
|
||||
get { rawValue.tv_usec }
|
||||
set { rawValue.tv_usec = newValue }
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "microseconds")
|
||||
public var tv_usec: CInterop.Microseconds { microseconds }
|
||||
}
|
||||
|
||||
extension TimeValue {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
private static let attosecondsPerMicrosecond: Int64 = 1_000_000_000_000
|
||||
|
||||
/// Creates a new value, rounded to the closest possible representation.
|
||||
@_alwaysEmitIntoClient
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public init(_ duration: Duration) {
|
||||
let (seconds, attoseconds) = duration.components
|
||||
let microseconds = attoseconds / Self.attosecondsPerMicrosecond
|
||||
self.rawValue = .init(
|
||||
tv_sec: CInterop.Time(truncatingIfNeeded: seconds),
|
||||
tv_usec: CInterop.Microseconds(microseconds))
|
||||
}
|
||||
|
||||
/// Creates a new value, if the given duration can be represented exactly.
|
||||
@_alwaysEmitIntoClient
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public init?(exactly duration: Duration) {
|
||||
let (seconds, attoseconds) = duration.components
|
||||
guard
|
||||
let seconds = CInterop.Time(exactly: seconds),
|
||||
attoseconds.isMultiple(of: Self.attosecondsPerMicrosecond)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
let microseconds = attoseconds / Self.attosecondsPerMicrosecond
|
||||
self.rawValue = .init(
|
||||
tv_sec: CInterop.Time(truncatingIfNeeded: seconds),
|
||||
tv_usec: CInterop.Microseconds(microseconds))
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public var duration: Duration {
|
||||
let seconds = Int64(self.seconds)
|
||||
let attoseconds = Int64(self.microseconds) *
|
||||
Self.attosecondsPerMicrosecond
|
||||
return .init(
|
||||
secondsComponent: seconds,
|
||||
attosecondsComponent: attoseconds)
|
||||
}
|
||||
}
|
||||
|
||||
extension TimeValue: AdditiveArithmetic {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
private static let microsecondsPerSecond: CInterop.Microseconds = 1_000_000
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public static var zero: TimeValue {
|
||||
Self(rawValue: .init(tv_sec: 0, tv_usec: 0))
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public static func + (lhs: Self, rhs: Self) -> Self {
|
||||
var newValue = TimeValue.zero
|
||||
newValue.seconds = lhs.seconds + rhs.seconds
|
||||
newValue.microseconds = lhs.microseconds + rhs.microseconds
|
||||
if (newValue.microseconds >= Self.microsecondsPerSecond) {
|
||||
newValue.seconds += 1
|
||||
newValue.microseconds -= Self.microsecondsPerSecond
|
||||
}
|
||||
return newValue
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public static func - (lhs: Self, rhs: Self) -> Self {
|
||||
var newValue = TimeValue.zero
|
||||
newValue.seconds = lhs.seconds - rhs.seconds;
|
||||
newValue.microseconds = lhs.microseconds - rhs.microseconds
|
||||
if (newValue.microseconds < 0) {
|
||||
newValue.seconds -= 1
|
||||
newValue.microseconds += Self.microsecondsPerSecond
|
||||
}
|
||||
return newValue
|
||||
}
|
||||
}
|
||||
|
||||
extension TimeValue: Comparable {
|
||||
@_alwaysEmitIntoClient
|
||||
public static func < (lhs: Self, rhs: Self) -> Bool {
|
||||
guard lhs.seconds == rhs.seconds else {
|
||||
return lhs.seconds < rhs.seconds
|
||||
}
|
||||
return lhs.microseconds < rhs.microseconds
|
||||
}
|
||||
}
|
||||
|
||||
extension TimeValue: Equatable {
|
||||
@_alwaysEmitIntoClient
|
||||
public static func == (lhs: Self, rhs: Self) -> Bool {
|
||||
lhs.seconds == rhs.seconds && lhs.microseconds == rhs.microseconds
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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
|
||||
*/
|
||||
|
||||
extension Array where Element == String {
|
||||
internal typealias CStr = UnsafePointer<CChar>?
|
||||
|
||||
/// Call `body` with a buffer of `UnsafePointer<CChar>?` values,
|
||||
/// suitable for passing to a C function that expects a C string array.
|
||||
/// The buffer is guaranteed to be followed by an extra storage slot
|
||||
/// containing a null pointer. (For C functions that expect an array
|
||||
/// terminated with a null pointer.)
|
||||
///
|
||||
/// This function is careful not to heap allocate memory unless there are
|
||||
/// too many strings, or if it needs to copy too much character data.
|
||||
internal func _withCStringArray<R>(
|
||||
_ body: (UnsafeBufferPointer<UnsafePointer<CChar>?>) throws -> R
|
||||
) rethrows -> R {
|
||||
if self.count == 0 {
|
||||
// Fast path: empty array.
|
||||
let p: CStr = nil
|
||||
return try Swift.withUnsafePointer(to: p) { array in
|
||||
try body(UnsafeBufferPointer(start: array, count: 0))
|
||||
}
|
||||
}
|
||||
#if SYSTEM_OS_BUILD // String._guts isn't accessible from SwiftPM or CMake
|
||||
if self.count == 1, self[0]._guts._isLargeZeroTerminatedContiguousUTF8 {
|
||||
// Fast path: Single fast string.
|
||||
let start = self[0]._guts._largeContiguousUTF8CodeUnits.baseAddress!
|
||||
var p: (CStr, CStr) = (
|
||||
UnsafeRawPointer(start).assumingMemoryBound(to: CChar.self),
|
||||
nil
|
||||
)
|
||||
return try Swift.withUnsafeBytes(of: &p) { buffer in
|
||||
let start = buffer.baseAddress!.assumingMemoryBound(to: CStr.self)
|
||||
return try body(UnsafeBufferPointer(start: start, count: 1))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// We need to create a buffer for the C array.
|
||||
return try _withStackBuffer(
|
||||
capacity: (self.count + 1) * MemoryLayout<CStr>.stride
|
||||
) { array in
|
||||
let array = array.bindMemory(to: CStr.self)
|
||||
// Calculate number of bytes we need for character storage
|
||||
let bytes = self.reduce(into: 0) { count, string in
|
||||
#if SYSTEM_OS_BUILD
|
||||
if string._guts._isLargeZeroTerminatedContiguousUTF8 { return }
|
||||
#endif
|
||||
count += string.utf8.count + 1 // Plus one for terminating NUL
|
||||
}
|
||||
#if SYSTEM_OS_BUILD
|
||||
if bytes == 0 {
|
||||
// Fast path: we only contain strings with stable null-terminated storage
|
||||
for i in self.indices {
|
||||
let string = self[i]
|
||||
precondition(string._guts._isLargeZeroTerminatedContiguousUTF8)
|
||||
let address = string._guts._largeContiguousUTF8CodeUnits.baseAddress!
|
||||
array[i] = UnsafeRawPointer(address).assumingMemoryBound(to: CChar.self)
|
||||
}
|
||||
array[self.count] = nil
|
||||
return try body(UnsafeBufferPointer(rebasing: array.dropLast()))
|
||||
}
|
||||
#endif
|
||||
return try _withStackBuffer(capacity: bytes) { chars in
|
||||
var chars = chars
|
||||
for i in self.indices {
|
||||
let (cstr, scratchUsed) = self[i]._getCStr(with: chars)
|
||||
array[i] = cstr.assumingMemoryBound(to: CChar.self)
|
||||
chars = .init(rebasing: chars[scratchUsed...])
|
||||
}
|
||||
array[self.count] = nil
|
||||
return try body(UnsafeBufferPointer(rebasing: array.dropLast()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
fileprivate func _getCStr(
|
||||
with scratch: UnsafeMutableRawBufferPointer
|
||||
) -> (cstr: UnsafeRawPointer, scratchUsed: Int) {
|
||||
#if SYSTEM_OS_BUILD
|
||||
if _guts._isLargeZeroTerminatedContiguousUTF8 {
|
||||
// This is a wonderful string, we can just use its storage address.
|
||||
let address = _guts._largeContiguousUTF8CodeUnits.baseAddress!
|
||||
return (UnsafeRawPointer(address), 0)
|
||||
}
|
||||
#endif
|
||||
let r: (UnsafeRawPointer, Int)? = self.utf8.withContiguousStorageIfAvailable { source in
|
||||
// This is a somewhat okay string -- we need to use memcpy.
|
||||
precondition(source.count <= scratch.count)
|
||||
let start = scratch.baseAddress!
|
||||
start.copyMemory(from: source.baseAddress!, byteCount: source.count)
|
||||
start.storeBytes(of: 0, toByteOffset: source.count, as: UInt8.self)
|
||||
return (UnsafeRawPointer(start), source.count + 1)
|
||||
}
|
||||
if let r = r { return r }
|
||||
|
||||
// What a horrible string; we need to copy individual bytes.
|
||||
precondition(self.utf8.count <= scratch.count)
|
||||
var c = 0
|
||||
for byte in self.utf8 {
|
||||
scratch[c] = byte
|
||||
c += 1
|
||||
}
|
||||
scratch[c] = 0
|
||||
c += 1
|
||||
return (UnsafeRawPointer(scratch.baseAddress!), c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
/*
|
||||
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 - 2021 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
|
||||
*/
|
||||
|
||||
// Results in errno if i == -1
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
private func valueOrErrno<I: FixedWidthInteger>(
|
||||
_ i: I
|
||||
) -> Result<I, Errno> {
|
||||
i == -1 ? .failure(Errno.current) : .success(i)
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
private func nothingOrErrno<I: FixedWidthInteger>(
|
||||
_ i: I
|
||||
) -> Result<(), Errno> {
|
||||
valueOrErrno(i).map { _ in () }
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
internal func valueOrErrno<I: FixedWidthInteger>(
|
||||
retryOnInterrupt: Bool, _ f: () -> I
|
||||
) -> Result<I, Errno> {
|
||||
@@ -36,7 +36,7 @@ internal func valueOrErrno<I: FixedWidthInteger>(
|
||||
} while true
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
internal func nothingOrErrno<I: FixedWidthInteger>(
|
||||
retryOnInterrupt: Bool, _ f: () -> I
|
||||
) -> Result<(), Errno> {
|
||||
@@ -127,3 +127,38 @@ extension MutableCollection where Element: Equatable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal func _withOptionalUnsafePointerOrNull<T, R>(
|
||||
to value: T?,
|
||||
_ body: (UnsafePointer<T>?) throws -> R
|
||||
) rethrows -> R {
|
||||
guard let value = value else {
|
||||
return try body(nil)
|
||||
}
|
||||
return try withUnsafePointer(to: value, body)
|
||||
}
|
||||
|
||||
/// Calls `body` with a temporary buffer of the indicated size,
|
||||
/// possibly stack-allocated.
|
||||
internal func _withStackBuffer<R>(
|
||||
capacity: Int,
|
||||
_ body: (UnsafeMutableRawBufferPointer) throws -> R
|
||||
) rethrows -> R {
|
||||
typealias StackStorage = (
|
||||
UInt64, UInt64, UInt64, UInt64,
|
||||
UInt64, UInt64, UInt64, UInt64,
|
||||
UInt64, UInt64, UInt64, UInt64,
|
||||
UInt64, UInt64, UInt64, UInt64
|
||||
)
|
||||
if capacity > MemoryLayout<StackStorage>.size {
|
||||
var buffer = _RawBuffer(minimumCapacity: capacity)
|
||||
return try buffer.withUnsafeMutableBytes { buffer in
|
||||
try body(.init(rebasing: buffer[..<capacity]))
|
||||
}
|
||||
} else {
|
||||
var storage: StackStorage = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
return try withUnsafeMutableBytes(of: &storage) { buffer in
|
||||
try body(.init(rebasing: buffer[..<capacity]))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,11 @@ import SystemPackage
|
||||
import System
|
||||
#endif
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
#if os(Windows)
|
||||
import WinSDK
|
||||
#endif
|
||||
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
final class ErrnoTest: XCTestCase {
|
||||
func testConstants() {
|
||||
XCTAssert(EPERM == Errno.notPermitted.rawValue)
|
||||
@@ -32,7 +36,9 @@ final class ErrnoTest: XCTestCase {
|
||||
XCTAssert(ENOMEM == Errno.noMemory.rawValue)
|
||||
XCTAssert(EACCES == Errno.permissionDenied.rawValue)
|
||||
XCTAssert(EFAULT == Errno.badAddress.rawValue)
|
||||
#if !os(Windows)
|
||||
XCTAssert(ENOTBLK == Errno.notBlockDevice.rawValue)
|
||||
#endif
|
||||
XCTAssert(EBUSY == Errno.resourceBusy.rawValue)
|
||||
XCTAssert(EEXIST == Errno.fileExists.rawValue)
|
||||
XCTAssert(EXDEV == Errno.improperLink.rawValue)
|
||||
@@ -42,8 +48,10 @@ final class ErrnoTest: XCTestCase {
|
||||
XCTAssert(EINVAL == Errno.invalidArgument.rawValue)
|
||||
XCTAssert(ENFILE == Errno.tooManyOpenFilesInSystem.rawValue)
|
||||
XCTAssert(EMFILE == Errno.tooManyOpenFiles.rawValue)
|
||||
#if !os(Windows)
|
||||
XCTAssert(ENOTTY == Errno.inappropriateIOCTLForDevice.rawValue)
|
||||
XCTAssert(ETXTBSY == Errno.textFileBusy.rawValue)
|
||||
#endif
|
||||
XCTAssert(EFBIG == Errno.fileTooLarge.rawValue)
|
||||
XCTAssert(ENOSPC == Errno.noSpace.rawValue)
|
||||
XCTAssert(ESPIPE == Errno.illegalSeek.rawValue)
|
||||
@@ -61,9 +69,15 @@ final class ErrnoTest: XCTestCase {
|
||||
XCTAssert(EPROTOTYPE == Errno.protocolWrongTypeForSocket.rawValue)
|
||||
XCTAssert(ENOPROTOOPT == Errno.protocolNotAvailable.rawValue)
|
||||
XCTAssert(EPROTONOSUPPORT == Errno.protocolNotSupported.rawValue)
|
||||
#if os(Windows)
|
||||
XCTAssert(WSAESOCKTNOSUPPORT == Errno.socketTypeNotSupported.rawValue)
|
||||
XCTAssert(WSAEOPNOTSUPP == Errno.notSupported.rawValue)
|
||||
XCTAssert(WSAEPFNOSUPPORT == Errno.protocolFamilyNotSupported.rawValue)
|
||||
#else
|
||||
XCTAssert(ESOCKTNOSUPPORT == Errno.socketTypeNotSupported.rawValue)
|
||||
XCTAssert(ENOTSUP == Errno.notSupported.rawValue)
|
||||
XCTAssert(EPFNOSUPPORT == Errno.protocolFamilyNotSupported.rawValue)
|
||||
#endif
|
||||
XCTAssert(EAFNOSUPPORT == Errno.addressFamilyNotSupported.rawValue)
|
||||
XCTAssert(EADDRINUSE == Errno.addressInUse.rawValue)
|
||||
XCTAssert(EADDRNOTAVAIL == Errno.addressNotAvailable.rawValue)
|
||||
@@ -75,12 +89,20 @@ final class ErrnoTest: XCTestCase {
|
||||
XCTAssert(ENOBUFS == Errno.noBufferSpace.rawValue)
|
||||
XCTAssert(EISCONN == Errno.socketIsConnected.rawValue)
|
||||
XCTAssert(ENOTCONN == Errno.socketNotConnected.rawValue)
|
||||
#if os(Windows)
|
||||
XCTAssert(WSAESHUTDOWN == Errno.socketShutdown.rawValue)
|
||||
#else
|
||||
XCTAssert(ESHUTDOWN == Errno.socketShutdown.rawValue)
|
||||
#endif
|
||||
XCTAssert(ETIMEDOUT == Errno.timedOut.rawValue)
|
||||
XCTAssert(ECONNREFUSED == Errno.connectionRefused.rawValue)
|
||||
XCTAssert(ELOOP == Errno.tooManySymbolicLinkLevels.rawValue)
|
||||
XCTAssert(ENAMETOOLONG == Errno.fileNameTooLong.rawValue)
|
||||
#if os(Windows)
|
||||
XCTAssert(WSAEHOSTDOWN == Errno.hostIsDown.rawValue)
|
||||
#else
|
||||
XCTAssert(EHOSTDOWN == Errno.hostIsDown.rawValue)
|
||||
#endif
|
||||
XCTAssert(EHOSTUNREACH == Errno.noRouteToHost.rawValue)
|
||||
XCTAssert(ENOTEMPTY == Errno.directoryNotEmpty.rawValue)
|
||||
|
||||
@@ -88,9 +110,15 @@ final class ErrnoTest: XCTestCase {
|
||||
XCTAssert(EPROCLIM == Errno.tooManyProcesses.rawValue)
|
||||
#endif
|
||||
|
||||
#if os(Windows)
|
||||
XCTAssert(WSAEUSERS == Errno.tooManyUsers.rawValue)
|
||||
XCTAssert(WSAEDQUOT == Errno.diskQuotaExceeded.rawValue)
|
||||
XCTAssert(WSAESTALE == Errno.staleNFSFileHandle.rawValue)
|
||||
#else
|
||||
XCTAssert(EUSERS == Errno.tooManyUsers.rawValue)
|
||||
XCTAssert(EDQUOT == Errno.diskQuotaExceeded.rawValue)
|
||||
XCTAssert(ESTALE == Errno.staleNFSFileHandle.rawValue)
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
XCTAssert(EBADRPC == Errno.rpcUnsuccessful.rawValue)
|
||||
@@ -111,7 +139,9 @@ final class ErrnoTest: XCTestCase {
|
||||
XCTAssert(EDEVERR == Errno.deviceError.rawValue)
|
||||
#endif
|
||||
|
||||
#if !os(Windows)
|
||||
XCTAssert(EOVERFLOW == Errno.overflow.rawValue)
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
XCTAssert(EBADEXEC == Errno.badExecutable.rawValue)
|
||||
@@ -121,14 +151,17 @@ final class ErrnoTest: XCTestCase {
|
||||
#endif
|
||||
|
||||
XCTAssert(ECANCELED == Errno.canceled.rawValue)
|
||||
#if !os(Windows)
|
||||
XCTAssert(EIDRM == Errno.identifierRemoved.rawValue)
|
||||
XCTAssert(ENOMSG == Errno.noMessage.rawValue)
|
||||
#endif
|
||||
XCTAssert(EILSEQ == Errno.illegalByteSequence.rawValue)
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
XCTAssert(ENOATTR == Errno.attributeNotFound.rawValue)
|
||||
#endif
|
||||
|
||||
#if !os(Windows)
|
||||
XCTAssert(EBADMSG == Errno.badMessage.rawValue)
|
||||
XCTAssert(EMULTIHOP == Errno.multiHop.rawValue)
|
||||
XCTAssert(ENODATA == Errno.noData.rawValue)
|
||||
@@ -137,19 +170,27 @@ final class ErrnoTest: XCTestCase {
|
||||
XCTAssert(ENOSTR == Errno.notStream.rawValue)
|
||||
XCTAssert(EPROTO == Errno.protocolError.rawValue)
|
||||
XCTAssert(ETIME == Errno.timeout.rawValue)
|
||||
#endif
|
||||
XCTAssert(EOPNOTSUPP == Errno.notSupportedOnSocket.rawValue)
|
||||
|
||||
// From headers but not man page
|
||||
XCTAssert(EWOULDBLOCK == Errno.wouldBlock.rawValue)
|
||||
#if os(Windows)
|
||||
XCTAssert(WSAETOOMANYREFS == Errno.tooManyReferences.rawValue)
|
||||
XCTAssert(WSAEREMOTE == Errno.tooManyRemoteLevels.rawValue)
|
||||
#else
|
||||
XCTAssert(ETOOMANYREFS == Errno.tooManyReferences.rawValue)
|
||||
XCTAssert(EREMOTE == Errno.tooManyRemoteLevels.rawValue)
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
XCTAssert(ENOPOLICY == Errno.noSuchPolicy.rawValue)
|
||||
#endif
|
||||
|
||||
#if !os(Windows)
|
||||
XCTAssert(ENOTRECOVERABLE == Errno.notRecoverable.rawValue)
|
||||
XCTAssert(EOWNERDEAD == Errno.previousOwnerDied.rawValue)
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
XCTAssert(EQFULL == Errno.outputQueueFull.rawValue)
|
||||
|
||||
@@ -15,7 +15,7 @@ import SystemPackage
|
||||
import System
|
||||
#endif
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
final class FileOperationsTest: XCTestCase {
|
||||
func testSyscalls() {
|
||||
let fd = FileDescriptor(rawValue: 1)
|
||||
@@ -28,7 +28,8 @@ final class FileOperationsTest: XCTestCase {
|
||||
let writeBuf = UnsafeRawBufferPointer(rawBuf)
|
||||
let writeBufAddr = writeBuf.baseAddress
|
||||
|
||||
let syscallTestCases: Array<MockTestCase> = [
|
||||
var syscallTestCases: Array<MockTestCase> = []
|
||||
syscallTestCases += [
|
||||
MockTestCase(name: "open", .interruptable, "a path", O_RDWR | O_APPEND) {
|
||||
retryOnInterrupt in
|
||||
_ = try FileDescriptor.open(
|
||||
@@ -83,6 +84,49 @@ final class FileOperationsTest: XCTestCase {
|
||||
},
|
||||
]
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
let fp = FilePath("/")
|
||||
let rawFP = fp.withPlatformString { strdup($0) }
|
||||
defer { free(rawFP) }
|
||||
|
||||
let stat = UnsafeMutablePointer<stat>.allocate(capacity: 1)
|
||||
defer { stat.deallocate() }
|
||||
|
||||
syscallTestCases += [
|
||||
// MockTestCase(name: "stat", .interruptable, rawFP) { retryOnInterrupt in
|
||||
// _ = try fp.stat(followSymlinks: false, retryOnInterrupt: retryOnInterrupt)
|
||||
// },
|
||||
// MockTestCase(name: "lstat", .interruptable, rawFP) { retryOnInterrupt in
|
||||
// _ = try fp.stat(followSymlinks: true, retryOnInterrupt: retryOnInterrupt)
|
||||
// },
|
||||
// MockTestCase(name: "fstat", .interruptable, rawFP) { retryOnInterrupt in
|
||||
// _ = try fd.stat(retryOnInterrupt: retryOnInterrupt)
|
||||
// },
|
||||
// MockTestCase(name: "fstatat", .interruptable, rawFP) { retryOnInterrupt in
|
||||
// _ = try fp.stat(relativeTo: fd, fcntrl: .none, retryOnInterrupt: retryOnInterrupt)
|
||||
// },
|
||||
// fstatat
|
||||
// chmod
|
||||
// lchmod
|
||||
// fchmod
|
||||
// fchmodat
|
||||
// chown
|
||||
// lchown
|
||||
// fchown
|
||||
// fchownat
|
||||
// chflags
|
||||
// lchflags
|
||||
// fchflags
|
||||
// umask
|
||||
// mkfifo
|
||||
// mknod
|
||||
// mkdir
|
||||
// mkdirat
|
||||
// futimens
|
||||
// utimensat
|
||||
]
|
||||
#endif
|
||||
|
||||
for test in syscallTestCases { test.runAllTests() }
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import XCTest
|
||||
@testable import System
|
||||
#endif
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
struct TestPathComponents: TestCase {
|
||||
var path: FilePath
|
||||
var expectedRoot: FilePath.Root?
|
||||
@@ -100,7 +100,7 @@ struct TestPathComponents: TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
final class FilePathComponentsTest: XCTestCase {
|
||||
func testAdHocRRC() {
|
||||
var path: FilePath = "/usr/local/bin"
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#endif
|
||||
|
||||
// Why can't I write this extension on `FilePath.ComponentView.SubSequence`?
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension Slice where Base == FilePath.ComponentView {
|
||||
internal var _storageSlice: SystemString.SubSequence {
|
||||
base._path._storage[self.startIndex._storage ..< self.endIndex._storage]
|
||||
@@ -15,7 +15,7 @@ extension Slice where Base == FilePath.ComponentView {
|
||||
|
||||
|
||||
// Proposed API that didn't make the cut, but we stil want to keep our testing for
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension FilePath {
|
||||
/// Returns `self` relative to `base`.
|
||||
/// This does not cosult the file system or resolve symlinks.
|
||||
@@ -51,7 +51,6 @@ extension FilePath {
|
||||
|
||||
/// Whether a lexically-normalized `self` contains a lexically-normalized
|
||||
/// `other`.
|
||||
///
|
||||
public func lexicallyContains(_ other: FilePath) -> Bool {
|
||||
guard !other.isEmpty else { return true }
|
||||
guard !isEmpty else { return false }
|
||||
|
||||
@@ -68,7 +68,7 @@ extension ParsingTestCase {
|
||||
@testable import System
|
||||
#endif
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
final class FilePathParsingTest: XCTestCase {
|
||||
func testNormalization() {
|
||||
let unixPaths: Array<ParsingTestCase> = [
|
||||
|
||||
@@ -146,7 +146,7 @@ extension SyntaxTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension SyntaxTestCase {
|
||||
func testComponents(_ path: FilePath, expected: [String]) {
|
||||
let expectedComponents = expected.map { FilePath.Component($0)! }
|
||||
@@ -342,7 +342,7 @@ private struct WindowsRootTestCase: TestCase {
|
||||
var line: UInt
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
extension WindowsRootTestCase {
|
||||
func runAllTests() {
|
||||
withWindowsPaths(enabled: true) {
|
||||
@@ -357,7 +357,7 @@ extension WindowsRootTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
final class FilePathSyntaxTest: XCTestCase {
|
||||
func testPathSyntax() {
|
||||
let unixPaths: Array<SyntaxTestCase> = [
|
||||
|
||||
@@ -15,7 +15,7 @@ import SystemPackage
|
||||
import System
|
||||
#endif
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
func filePathFromUnterminatedBytes<S: Sequence>(_ bytes: S) -> FilePath where S.Element == UInt8 {
|
||||
var array = Array(bytes)
|
||||
assert(array.last != 0, "already null terminated")
|
||||
@@ -29,7 +29,7 @@ func filePathFromUnterminatedBytes<S: Sequence>(_ bytes: S) -> FilePath where S.
|
||||
}
|
||||
let invalidBytes: [UInt8] = [0x2F, 0x61, 0x2F, 0x62, 0x2F, 0x83]
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
final class FilePathTest: XCTestCase {
|
||||
struct TestPath {
|
||||
let filePath: FilePath
|
||||
@@ -69,7 +69,11 @@ final class FilePathTest: XCTestCase {
|
||||
}
|
||||
|
||||
testPath.filePath.withPlatformString {
|
||||
#if os(Windows)
|
||||
XCTAssertEqual(testPath.string, String(decodingCString: $0, as: UTF16.self))
|
||||
#else
|
||||
XCTAssertEqual(testPath.string, String(cString: $0))
|
||||
#endif
|
||||
XCTAssertEqual(testPath.filePath, FilePath(platformString: $0))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import SystemPackage
|
||||
import System
|
||||
#endif
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
final class FileDescriptorTest: XCTestCase {
|
||||
func testStandardDescriptors() {
|
||||
XCTAssertEqual(FileDescriptor.standardInput.rawValue, 0)
|
||||
@@ -29,13 +29,17 @@ final class FileDescriptorTest: XCTestCase {
|
||||
XCTAssertEqual(O_WRONLY, FileDescriptor.AccessMode.writeOnly.rawValue)
|
||||
XCTAssertEqual(O_RDWR, FileDescriptor.AccessMode.readWrite.rawValue)
|
||||
|
||||
#if !os(Windows)
|
||||
XCTAssertEqual(O_NONBLOCK, FileDescriptor.OpenOptions.nonBlocking.rawValue)
|
||||
#endif
|
||||
XCTAssertEqual(O_APPEND, FileDescriptor.OpenOptions.append.rawValue)
|
||||
XCTAssertEqual(O_CREAT, FileDescriptor.OpenOptions.create.rawValue)
|
||||
XCTAssertEqual(O_TRUNC, FileDescriptor.OpenOptions.truncate.rawValue)
|
||||
XCTAssertEqual(O_EXCL, FileDescriptor.OpenOptions.exclusiveCreate.rawValue)
|
||||
#if !os(Windows)
|
||||
XCTAssertEqual(O_NOFOLLOW, FileDescriptor.OpenOptions.noFollow.rawValue)
|
||||
XCTAssertEqual(O_CLOEXEC, FileDescriptor.OpenOptions.closeOnExec.rawValue)
|
||||
#endif
|
||||
|
||||
// BSD only
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
@@ -60,7 +64,7 @@ final class FileDescriptorTest: XCTestCase {
|
||||
|
||||
}
|
||||
|
||||
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
|
||||
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
|
||||
final class FilePermissionsTest: XCTestCase {
|
||||
|
||||
func testPermissions() {
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2022 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 false && swift(>=5.8) && $MoveOnly && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS))
|
||||
|
||||
import XCTest
|
||||
import Darwin.Mach
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
import SystemPackage
|
||||
#else
|
||||
import System
|
||||
#endif
|
||||
|
||||
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 = 0
|
||||
withUnsafeMutablePointer(to: &refCount) { refCount in
|
||||
let kr = mach_port_get_refs(mach_task_self_, name, kind, refCount)
|
||||
assert(kr == KERN_SUCCESS)
|
||||
}
|
||||
return refCount
|
||||
}
|
||||
|
||||
func scopedReceiveRight(name:mach_port_name_t) -> mach_port_urefs_t {
|
||||
_ = Mach.Port<Mach.ReceiveRight>(name:name) // this should automatically deallocate when going out of scope
|
||||
return refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_RECEIVE)
|
||||
}
|
||||
|
||||
func testRecieveRightDeallocation() throws {
|
||||
var name:mach_port_name_t = 0 // Never read
|
||||
withUnsafeMutablePointer(to:&name) { name in
|
||||
let kr = mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, name)
|
||||
assert(kr == KERN_SUCCESS)
|
||||
}
|
||||
|
||||
XCTAssert(name != 0xFFFFFFFF)
|
||||
|
||||
let one = scopedReceiveRight(name:name)
|
||||
let zero = refCountForMachPortName(name:name, kind: MACH_PORT_RIGHT_RECEIVE)
|
||||
|
||||
XCTAssert(one == 1);
|
||||
XCTAssert(zero == 0);
|
||||
}
|
||||
|
||||
func consumeSendRightAutomatically(name:mach_port_name_t) -> mach_port_urefs_t {
|
||||
let send = Mach.Port<Mach.SendRight>(name:name) // this should automatically deallocate when going out of scope
|
||||
return send.withBorrowedName { name in
|
||||
// Get the ref count before automatic deallocation happens
|
||||
return refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_SEND)
|
||||
}
|
||||
}
|
||||
|
||||
func testSendRightDeallocation() throws {
|
||||
let recv = Mach.Port<Mach.ReceiveRight>()
|
||||
recv.withBorrowedName { name in
|
||||
let kr = mach_port_insert_right(mach_task_self_, name, name, mach_msg_type_name_t(MACH_MSG_TYPE_MAKE_SEND))
|
||||
XCTAssert(kr == KERN_SUCCESS)
|
||||
let one = consumeSendRightAutomatically(name:name)
|
||||
XCTAssert(one == 1);
|
||||
let zero = refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_SEND)
|
||||
XCTAssert(zero == 0);
|
||||
}
|
||||
}
|
||||
|
||||
func testSendRightRelinquishment() throws {
|
||||
let recv = Mach.Port<Mach.ReceiveRight>()
|
||||
|
||||
let name = ({
|
||||
let send = recv.makeSendRight()
|
||||
let one = send.withBorrowedName { name in
|
||||
return self.refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_SEND)
|
||||
}
|
||||
XCTAssert(one == 1)
|
||||
|
||||
return send.relinquish()
|
||||
})()
|
||||
|
||||
let stillOne = refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_SEND)
|
||||
XCTAssert(stillOne == 1)
|
||||
}
|
||||
|
||||
func testMakeSendCountSettable() throws {
|
||||
var recv = Mach.Port<Mach.ReceiveRight>()
|
||||
XCTAssert(recv.makeSendCount == 0)
|
||||
recv.makeSendCount = 7
|
||||
XCTAssert(recv.makeSendCount == 7)
|
||||
}
|
||||
|
||||
func makeSendRight() throws -> Mach.Port<Mach.SendRight> {
|
||||
let recv = Mach.Port<Mach.ReceiveRight>()
|
||||
let zero = recv.makeSendCount
|
||||
XCTAssert(zero == 0)
|
||||
let send = recv.makeSendRight()
|
||||
let one = recv.makeSendCount
|
||||
XCTAssert(one == 1)
|
||||
return send
|
||||
}
|
||||
|
||||
func testMakeSendCountIncrement() throws {
|
||||
_ = try makeSendRight()
|
||||
}
|
||||
|
||||
func testMakeSendOnceDoesntIncrementMakeSendCount() throws {
|
||||
let recv = Mach.Port<Mach.ReceiveRight>()
|
||||
let zero = recv.makeSendCount
|
||||
XCTAssert(zero == 0)
|
||||
_ = recv.makeSendOnceRight()
|
||||
let same = recv.makeSendCount
|
||||
XCTAssert(same == zero)
|
||||
}
|
||||
|
||||
func testMakeSendOnceIsUnique() throws {
|
||||
let recv = Mach.Port<Mach.ReceiveRight>()
|
||||
let once = recv.makeSendOnceRight()
|
||||
recv.withBorrowedName { rname in
|
||||
once.withBorrowedName { oname in
|
||||
print(oname, rname)
|
||||
XCTAssert(oname != rname)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testCopySend() throws {
|
||||
let recv = Mach.Port<Mach.ReceiveRight>()
|
||||
let zero = recv.makeSendCount
|
||||
XCTAssert(zero == 0)
|
||||
let send = recv.makeSendRight()
|
||||
let one = recv.makeSendCount
|
||||
XCTAssert(one == 1)
|
||||
_ = try send.copySendRight()
|
||||
let same = recv.makeSendCount
|
||||
XCTAssert(same == one)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -15,7 +15,7 @@ import XCTest
|
||||
@testable import System
|
||||
#endif
|
||||
|
||||
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
final class MockingTest: XCTestCase {
|
||||
func testMocking() {
|
||||
XCTAssertFalse(mockingEnabled)
|
||||
|
||||
@@ -25,7 +25,7 @@ final class SystemCharTest: XCTestCase {
|
||||
|
||||
// non printable
|
||||
for value in 0..<(UInt8(ascii: " ")) {
|
||||
XCTAssertFalse(SystemChar(codeUnit: value).isLetter)
|
||||
XCTAssertFalse(SystemChar(codeUnit: CInterop.PlatformUnicodeEncoding.CodeUnit(value)).isLetter)
|
||||
}
|
||||
XCTAssertFalse(SystemChar(codeUnit: 0x7F).isLetter) // DEL
|
||||
|
||||
|
||||
@@ -23,7 +23,11 @@ private func makeRaw(
|
||||
_ str: String
|
||||
) -> [CInterop.PlatformChar] {
|
||||
var str = str
|
||||
#if os(Windows)
|
||||
var array = Array(str.utf16)
|
||||
#else
|
||||
var array = str.withUTF8 { $0.withMemoryRebound(to: CChar.self, Array.init) }
|
||||
#endif
|
||||
array.append(0)
|
||||
return array
|
||||
}
|
||||
@@ -288,7 +292,7 @@ extension SystemStringTest {
|
||||
source.withUnsafeBufferPointer {
|
||||
XCTAssertEqual(str, String(validatingPlatformString: $0.baseAddress!))
|
||||
}
|
||||
source[1] = CInterop.PlatformChar(truncatingIfNeeded: 0xffff)
|
||||
source[1] = CInterop.PlatformChar(truncatingIfNeeded: 0xdfff)
|
||||
str = String(validatingPlatformString: source)
|
||||
XCTAssertNil(str)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,25 @@ import XCTest
|
||||
@testable import System
|
||||
#endif
|
||||
|
||||
internal struct Wildcard: Hashable {}
|
||||
|
||||
extension Trace.Entry {
|
||||
/// This implements `==` with wildcard matching.
|
||||
/// (`Entry` cannot conform to `Equatable`/`Hashable` this way because
|
||||
/// the wildcard matching `==` relation isn't transitive.)
|
||||
internal func matches(_ other: Self) -> Bool {
|
||||
guard self.name == other.name else { return false }
|
||||
guard self.arguments.count == other.arguments.count else { return false }
|
||||
for i in self.arguments.indices {
|
||||
if self.arguments[i] is Wildcard || other.arguments[i] is Wildcard {
|
||||
continue
|
||||
}
|
||||
guard self.arguments[i] == other.arguments[i] else { return false }
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// To aid debugging, force failures to fatal error
|
||||
internal var forceFatalFailures = false
|
||||
|
||||
@@ -40,7 +59,7 @@ extension TestCase {
|
||||
_ message: String? = nil
|
||||
) where S1.Element: Equatable, S1.Element == S2.Element {
|
||||
if !expected.elementsEqual(actual) {
|
||||
defer { print("expected: \(expected), actual: \(actual)") }
|
||||
defer { print("expected: \(expected)\n actual: \(actual)") }
|
||||
fail(message)
|
||||
}
|
||||
}
|
||||
@@ -49,7 +68,7 @@ extension TestCase {
|
||||
_ message: String? = nil
|
||||
) {
|
||||
if actual != expected {
|
||||
defer { print("expected: \(expected), actual: \(actual)") }
|
||||
defer { print("expected: \(expected)\n actual: \(actual)") }
|
||||
fail(message)
|
||||
}
|
||||
}
|
||||
@@ -62,6 +81,27 @@ extension TestCase {
|
||||
fail(message)
|
||||
}
|
||||
}
|
||||
func expectMatch(
|
||||
_ expected: Trace.Entry?, _ actual: Trace.Entry?,
|
||||
_ message: String? = nil
|
||||
) {
|
||||
func check() -> Bool {
|
||||
switch (expected, actual) {
|
||||
case let (expected?, actual?):
|
||||
return expected.matches(actual)
|
||||
case (nil, nil):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
if !check() {
|
||||
let e = expected.map { "\($0)" } ?? "nil"
|
||||
let a = actual.map { "\($0)" } ?? "nil"
|
||||
defer { print("expected: \(e)\n actual: \(a)") }
|
||||
fail(message)
|
||||
}
|
||||
}
|
||||
func expectNil<T>(
|
||||
_ actual: T?,
|
||||
_ message: String? = nil
|
||||
@@ -149,7 +189,7 @@ internal struct MockTestCase: TestCase {
|
||||
// Test our API mappings to the lower-level syscall invocation
|
||||
do {
|
||||
try body(true)
|
||||
self.expectEqual(self.expected, mocking.trace.dequeue())
|
||||
self.expectMatch(self.expected, mocking.trace.dequeue())
|
||||
} catch {
|
||||
self.fail()
|
||||
}
|
||||
@@ -158,9 +198,9 @@ internal struct MockTestCase: TestCase {
|
||||
guard interruptBehavior != .noError else {
|
||||
do {
|
||||
try body(interruptable)
|
||||
self.expectEqual(self.expected, mocking.trace.dequeue())
|
||||
self.expectMatch(self.expected, mocking.trace.dequeue())
|
||||
try body(!interruptable)
|
||||
self.expectEqual(self.expected, mocking.trace.dequeue())
|
||||
self.expectMatch(self.expected, mocking.trace.dequeue())
|
||||
} catch {
|
||||
self.fail()
|
||||
}
|
||||
@@ -177,7 +217,7 @@ internal struct MockTestCase: TestCase {
|
||||
self.fail()
|
||||
} catch Errno.interrupted {
|
||||
// Success!
|
||||
self.expectEqual(self.expected, mocking.trace.dequeue())
|
||||
self.expectMatch(self.expected, mocking.trace.dequeue())
|
||||
} catch {
|
||||
self.fail()
|
||||
}
|
||||
@@ -188,13 +228,13 @@ internal struct MockTestCase: TestCase {
|
||||
mocking.forceErrno = .counted(errno: EINTR, count: 3)
|
||||
|
||||
try body(interruptable)
|
||||
self.expectEqual(self.expected, mocking.trace.dequeue()) // EINTR
|
||||
self.expectEqual(self.expected, mocking.trace.dequeue()) // EINTR
|
||||
self.expectEqual(self.expected, mocking.trace.dequeue()) // EINTR
|
||||
self.expectEqual(self.expected, mocking.trace.dequeue()) // Success
|
||||
self.expectMatch(self.expected, mocking.trace.dequeue()) // EINTR
|
||||
self.expectMatch(self.expected, mocking.trace.dequeue()) // EINTR
|
||||
self.expectMatch(self.expected, mocking.trace.dequeue()) // EINTR
|
||||
self.expectMatch(self.expected, mocking.trace.dequeue()) // Success
|
||||
} catch Errno.interrupted {
|
||||
self.expectFalse(interruptable)
|
||||
self.expectEqual(self.expected, mocking.trace.dequeue()) // EINTR
|
||||
self.expectMatch(self.expected, mocking.trace.dequeue()) // EINTR
|
||||
} catch {
|
||||
self.fail()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2021 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
|
||||
|
||||
class UtilTests: XCTestCase {
|
||||
func testStackBuffer() {
|
||||
// Exercises _withStackBuffer a bit, in hopes any bugs will
|
||||
// show up as ASan failures.
|
||||
for size in stride(from: 0, to: 1000, by: 5) {
|
||||
var called = false
|
||||
_withStackBuffer(capacity: size) { buffer in
|
||||
XCTAssertFalse(called)
|
||||
called = true
|
||||
|
||||
buffer.initializeMemory(as: UInt8.self, repeating: 42)
|
||||
}
|
||||
XCTAssertTrue(called)
|
||||
}
|
||||
}
|
||||
|
||||
func testCStringArray() {
|
||||
func check(
|
||||
_ array: [String],
|
||||
file: StaticString = #file,
|
||||
line: UInt = #line
|
||||
) {
|
||||
array._withCStringArray { carray in
|
||||
let actual = carray.map { $0.map { String(cString: $0) } ?? "<NULL>" }
|
||||
XCTAssertEqual(actual, array, file: file, line: line)
|
||||
// Verify that there is a null pointer following the last item in
|
||||
// carray. (Note: this is intentionally addressing beyond the
|
||||
// end of the buffer, as the function promises that is going to be okay.)
|
||||
XCTAssertNil((carray.baseAddress! + carray.count).pointee)
|
||||
}
|
||||
}
|
||||
|
||||
check([])
|
||||
check([""])
|
||||
check(["", ""])
|
||||
check(["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""])
|
||||
// String literals of various sizes and counts
|
||||
check(["hello"])
|
||||
check(["hello", "world"])
|
||||
check(["This is a rather large string literal"])
|
||||
check([
|
||||
"This is a rather large string literal",
|
||||
"This is small",
|
||||
"This one is not that small -- it's even longer than the first",
|
||||
])
|
||||
check([
|
||||
"This is a rather large string literal",
|
||||
"This one is not that small -- it's even longer than the first",
|
||||
"And this is the largest of them all. I wonder if it even fits on a line"
|
||||
])
|
||||
check(Array(repeating: "", count: 100))
|
||||
check(Array(repeating: "Hiii", count: 100))
|
||||
check(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"])
|
||||
check(["", "b", "", "d", "", "f", "", "h", "", "j", "", "👨👨👧👦👩❤️💋👨", "m"])
|
||||
|
||||
var girls = ["Dörothy", "Róse", "Blánche", "Sőphia"]
|
||||
check(girls) // Small strings
|
||||
for i in girls.indices {
|
||||
// Convert to native
|
||||
girls[i] = "\(girls[i]) \(girls[i]) \(girls[i])"
|
||||
}
|
||||
check(girls) // Native large strings
|
||||
for i in girls.indices {
|
||||
let data = girls[i].data(using: .utf16)!
|
||||
girls[i] = NSString(
|
||||
data: data,
|
||||
encoding: String.Encoding.utf16.rawValue
|
||||
)! as String
|
||||
}
|
||||
check(girls) // UTF-16 Cocoa strings
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#if !canImport(ObjectiveC)
|
||||
#if !canImport(ObjectiveC) && swift(<5.5)
|
||||
import XCTest
|
||||
|
||||
extension ErrnoTest {
|
||||
|
||||
@@ -6,11 +6,19 @@
|
||||
# In order for this to work, ABI-impacting declarations need to be annotated
|
||||
# with special comments in the following format:
|
||||
#
|
||||
# /*System 0.0.2*/
|
||||
# @available(/*System 0.0.2*/iOS 8, *)
|
||||
# public func greeting() -> String {
|
||||
# "Hello"
|
||||
# }
|
||||
#
|
||||
# (The iOS 8 availability is a dummy no-op declaration -- it only has to be
|
||||
# there because `@available(*)` isn't valid syntax, and commenting out the
|
||||
# entire `@available` attribute would interfere with parser tools for doc
|
||||
# comments. `iOS 8` is the shortest version string that matches the minimum
|
||||
# possible deployment target for Swift code, so we use that as our dummy
|
||||
# availability version. `@available(iOS 8, *)` is functionally equivalent to not
|
||||
# having an `@available` attribute at all.)
|
||||
#
|
||||
# The script adds full availability incantations to these comments. It can run
|
||||
# in one of two modes:
|
||||
#
|
||||
@@ -19,16 +27,16 @@
|
||||
# availability across `SystemPackage` and the ABI-stable `System` module that
|
||||
# ships in Apple's OS releases:
|
||||
#
|
||||
# /*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
# @available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
# public func greeting() -> String {
|
||||
# "Hello"
|
||||
# }
|
||||
#
|
||||
# `expand-availability.py --attributes` adds actual availability attributes.
|
||||
# `expand-availability.py --attributes` adds actual availability declarations.
|
||||
# This is used by maintainers to build ABI stable releases of System on Apple's
|
||||
# platforms:
|
||||
#
|
||||
# /*System 0.0.2*/@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
|
||||
# @available(/*System 0.0.2: */macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
|
||||
# public func greeting() -> String {
|
||||
# "Hello"
|
||||
# }
|
||||
@@ -48,6 +56,7 @@ versions = {
|
||||
"System 0.0.2": "macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0",
|
||||
"System 1.1.0": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
|
||||
"System 1.2.0": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
|
||||
"System 1.3.0": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
|
||||
}
|
||||
|
||||
parser = argparse.ArgumentParser(description="Expand availability macros.")
|
||||
@@ -64,22 +73,44 @@ def swift_sources_in(path):
|
||||
result.append(os.path.join(dir, file))
|
||||
return result
|
||||
|
||||
macro_pattern = re.compile(
|
||||
# Old-style syntax:
|
||||
# /*System 0.0.2*/
|
||||
# /*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
|
||||
# /*System 0.0.2*/@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
|
||||
old_macro_pattern = re.compile(
|
||||
r"/\*(System [^ *]+)(, @available\([^)]*\))?\*/(@available\([^)]*\))?")
|
||||
|
||||
# New-style comments:
|
||||
# @available(/*SwiftSystem 0.0.2*/macOS 10, *)
|
||||
# @available(/*SwiftSystem 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
|
||||
# @available(/*SwiftSystem 0.0.2*/macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
|
||||
#
|
||||
# These do not interfere with our tools' ability to find doc comments.
|
||||
macro_pattern = re.compile(
|
||||
r"@available\(/\*(System [^ *:]+)[^*/)]*\*/([^)]*)\*\)")
|
||||
|
||||
def available_attribute(filename, lineno, symbolic_version):
|
||||
expansion = versions[symbolic_version]
|
||||
if expansion is None:
|
||||
raise ValueError("{0}:{1}: error: Unknown System version '{0}'"
|
||||
.format(fileinput.filename(), fileinput.lineno(), symbolic_version))
|
||||
if args.attributes:
|
||||
attribute = "@available(/*{0}*/{1}, *)".format(symbolic_version, expansion)
|
||||
else:
|
||||
# Sadly `@available(*)` is not valid syntax, so we have to mention at
|
||||
# least one actual platform here.
|
||||
attribute = "@available(/*{0}: {1}*/iOS 8, *)".format(symbolic_version, expansion)
|
||||
return attribute
|
||||
|
||||
|
||||
sources = swift_sources_in("Sources") + swift_sources_in("Tests")
|
||||
for line in fileinput.input(files=sources, inplace=True):
|
||||
match = re.search(macro_pattern, line)
|
||||
if match is None:
|
||||
match = re.search(old_macro_pattern, line)
|
||||
if match:
|
||||
system_version = match.group(1)
|
||||
expansion = versions[system_version]
|
||||
if expansion is None:
|
||||
raise ValueError("{0}:{1}: error: Unknown System version '{0}'"
|
||||
.format(fileinput.filename(), fileinput.lineno(),
|
||||
system_version))
|
||||
if args.attributes:
|
||||
replacement = "/*{0}*/@available({1}, *)".format(system_version, expansion)
|
||||
else:
|
||||
replacement = "/*{0}, @available({1}, *)*/".format(system_version, expansion)
|
||||
symbolic_version = match.group(1)
|
||||
replacement = available_attribute(
|
||||
fileinput.filename(), fileinput.lineno(), symbolic_version)
|
||||
line = line[:match.start()] + replacement + line[match.end():]
|
||||
print(line, end="")
|
||||
@@ -17,7 +17,7 @@ See https://swift.org/LICENSE.txt for license information
|
||||
function(get_swift_host_arch result_var_name)
|
||||
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
|
||||
set("${result_var_name}" "x86_64" PARENT_SCOPE)
|
||||
elseif ("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "AArch64|aarch64|arm64")
|
||||
elseif ("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "AArch64|aarch64|arm64|ARM64")
|
||||
if(CMAKE_SYSTEM_NAME MATCHES Darwin)
|
||||
set("${result_var_name}" "arm64" PARENT_SCOPE)
|
||||
else()
|
||||
|
||||
@@ -8,5 +8,5 @@ See https://swift.org/LICENSE.txt for license information
|
||||
#]]
|
||||
|
||||
if(NOT TARGET SystemPackage)
|
||||
include(@SWIFT_SYSTEM_EXPORTS_FILE@)
|
||||
include("@SWIFT_SYSTEM_EXPORTS_FILE@")
|
||||
endif()
|
||||
Reference in new issue
Block a user