mirror of
https://github.com/apple/swift-system
synced 2026-11-22 04:30:46 +00:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ff423fe26 | ||
|
|
7661e95acf | ||
|
|
3b62b88701 | ||
|
|
3536697660 | ||
|
|
ffc90f58de | ||
|
|
55d9da0dcd | ||
|
|
539134c24b |
No files matched your search
+1
-1
@@ -5,4 +5,4 @@
|
||||
swift-system.xcodeproj
|
||||
xcuserdata/
|
||||
.*.sw?
|
||||
.docc-build
|
||||
.docc-build
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -36,12 +36,6 @@ public typealias CModeT = mode_t
|
||||
/// 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 {
|
||||
#if os(Windows)
|
||||
public typealias Mode = CInt
|
||||
#else
|
||||
public typealias Mode = mode_t
|
||||
#endif
|
||||
|
||||
/// The C `char` type
|
||||
public typealias Char = CChar
|
||||
|
||||
|
||||
@@ -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 }
|
||||
@@ -442,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 }
|
||||
@@ -531,3 +531,4 @@ internal var _SEEK_HOLE: CInt { SEEK_HOLE }
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _SEEK_DATA: CInt { SEEK_DATA }
|
||||
#endif
|
||||
|
||||
@@ -193,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,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
|
||||
File renamed without changes.
@@ -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
|
||||
@@ -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
|
||||
@@ -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() }
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user