mirror of
https://github.com/apple/swift-system
synced 2026-11-22 04:30:46 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39774ef16a | ||
|
|
58a2725013 | ||
|
|
e520e61616 | ||
|
|
4f36710d0b | ||
|
|
591ff41944 |
No files matched your search
+22
-21
@@ -12,31 +12,32 @@
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let targets: [PackageDescription.Target] = [
|
||||
.target(
|
||||
name: "SystemPackage",
|
||||
dependencies: ["CSystem"],
|
||||
path: "Sources/System",
|
||||
swiftSettings: [
|
||||
.define("SYSTEM_PACKAGE"),
|
||||
.define("ENABLE_MOCKING", .when(configuration: .debug))
|
||||
]),
|
||||
.target(
|
||||
name: "CSystem",
|
||||
dependencies: []),
|
||||
.testTarget(
|
||||
name: "SystemTests",
|
||||
dependencies: ["SystemPackage"],
|
||||
swiftSettings: [
|
||||
.define("SYSTEM_PACKAGE")
|
||||
]),
|
||||
]
|
||||
|
||||
let package = Package(
|
||||
name: "swift-system",
|
||||
products: [
|
||||
.library(name: "SystemPackage", targets: ["SystemPackage"]),
|
||||
],
|
||||
dependencies: [],
|
||||
targets: targets
|
||||
targets: [
|
||||
.target(
|
||||
name: "CSystem",
|
||||
dependencies: []),
|
||||
.target(
|
||||
name: "SystemPackage",
|
||||
dependencies: ["CSystem"],
|
||||
path: "Sources/System",
|
||||
cSettings: [
|
||||
.define("_CRT_SECURE_NO_WARNINGS")
|
||||
],
|
||||
swiftSettings: [
|
||||
.define("SYSTEM_PACKAGE"),
|
||||
.define("ENABLE_MOCKING", .when(configuration: .debug))
|
||||
]),
|
||||
.testTarget(
|
||||
name: "SystemTests",
|
||||
dependencies: ["SystemPackage"],
|
||||
swiftSettings: [
|
||||
.define("SYSTEM_PACKAGE")
|
||||
]),
|
||||
]
|
||||
)
|
||||
@@ -234,6 +234,20 @@ extension FileDescriptor {
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "noFollow")
|
||||
public static var O_NOFOLLOW: OpenOptions { noFollow }
|
||||
|
||||
/// Indicates that opening the file only succeeds if the file is a directory.
|
||||
///
|
||||
/// If you specify this option and the file path you pass to
|
||||
/// <doc:System/FileDescriptor/open(_:_:options:permissions:)-10dcs>
|
||||
/// is a not a directory, then that open operation fails.
|
||||
///
|
||||
/// The corresponding C constant is `O_DIRECTORY`.
|
||||
@_alwaysEmitIntoClient
|
||||
public static var directory: OpenOptions { OpenOptions(_O_DIRECTORY) }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "directory")
|
||||
public static var O_DIRECTORY: OpenOptions { directory }
|
||||
#endif
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
|
||||
@@ -28,9 +28,6 @@
|
||||
/// let fd = try FileDescriptor.open(path, .writeOnly, options: .append)
|
||||
/// try fd.closeAfter { try fd.writeAll(message.utf8) }
|
||||
///
|
||||
/// TODO(docs): Section on all the new syntactic operations, lexical normalization, decomposition,
|
||||
/// components, etc.
|
||||
///
|
||||
/// File paths conform to the
|
||||
/// and <doc://com.apple.documentation/documentation/swift/equatable>
|
||||
/// and <doc://com.apple.documentation/documentation/swift/hashable> protocols
|
||||
@@ -40,6 +37,9 @@
|
||||
/// However, the rules for path equivalence
|
||||
/// are file-system–specific and have additional considerations
|
||||
/// like case insensitivity, Unicode normalization, and symbolic links.
|
||||
///
|
||||
// TODO(docs): Section on all the new syntactic operations, lexical normalization, decomposition,
|
||||
// components, etc.
|
||||
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
|
||||
public struct FilePath {
|
||||
internal var _storage: SystemString
|
||||
|
||||
@@ -64,9 +64,8 @@ extension FilePath {
|
||||
/// path.starts(with: "/usr/bin/ls///") // true
|
||||
/// path.starts(with: "/us") // false
|
||||
///
|
||||
/// TODO(Windows docs): examples with roots, such as whether `\foo\bar`
|
||||
/// starts with `C:\foo`
|
||||
///
|
||||
// TODO(Windows docs): examples with roots, such as whether `\foo\bar`
|
||||
// starts with `C:\foo`
|
||||
public func starts(with other: FilePath) -> Bool {
|
||||
guard !other.isEmpty else { return true }
|
||||
return self.root == other.root && components.starts(
|
||||
@@ -85,9 +84,8 @@ extension FilePath {
|
||||
/// path.ends(with: "/usr/bin/ls///") // true
|
||||
/// path.ends(with: "/ls") // false
|
||||
///
|
||||
/// TODO(Windows docs): examples with roots, such as whether `C:\foo\bar`
|
||||
/// ends with `C:bar`
|
||||
///
|
||||
// TODO(Windows docs): examples with roots, such as whether `C:\foo\bar`
|
||||
// ends with `C:bar`
|
||||
public func ends(with other: FilePath) -> Bool {
|
||||
if other.root != nil {
|
||||
// TODO: anything tricky here for Windows?
|
||||
@@ -453,8 +451,7 @@ extension FilePath {
|
||||
/// path.removePrefix("/us") // false
|
||||
/// path.removePrefix("/usr/local") // true, path is "bin"
|
||||
///
|
||||
/// TODO(Windows docs): example with roots
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
public mutating func removePrefix(_ prefix: FilePath) -> Bool {
|
||||
defer { _invariantCheck() }
|
||||
// FIXME: Should Windows have more nuanced semantics?
|
||||
@@ -476,8 +473,7 @@ extension FilePath {
|
||||
/// }
|
||||
/// // path is "/tmp/foo/bar/../baz"
|
||||
///
|
||||
/// TODO(Windows docs): example with roots
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
public mutating func append(_ component: __owned FilePath.Component) {
|
||||
defer { _invariantCheck() }
|
||||
_append(unchecked: component._slice)
|
||||
@@ -492,8 +488,7 @@ extension FilePath {
|
||||
/// let otherPath: FilePath = "/bin/ls"
|
||||
/// path.append(otherPath.components) // path is "/usr/local/bin/ls"
|
||||
///
|
||||
/// TODO(Windows docs): example with roots
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
public mutating func append<C: Collection>(
|
||||
_ components: __owned C
|
||||
) where C.Element == FilePath.Component {
|
||||
@@ -513,9 +508,8 @@ extension FilePath {
|
||||
/// path.append("static/assets") // "/var/www/website/static/assets"
|
||||
/// path.append("/main.css") // "/var/www/website/static/assets/main.css"
|
||||
///
|
||||
/// TODO(Windows docs): example with roots, should we rephrase this "spurious
|
||||
/// roots"?
|
||||
///
|
||||
// TODO(Windows docs): example with roots, should we rephrase this "spurious
|
||||
// roots"?
|
||||
public mutating func append(_ other: __owned String) {
|
||||
defer { _invariantCheck() }
|
||||
guard !other.utf8.isEmpty else { return }
|
||||
@@ -529,8 +523,7 @@ extension FilePath {
|
||||
|
||||
/// Non-mutating version of `append(_:Component)`.
|
||||
///
|
||||
/// TODO(Windows docs): example with roots
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
public __consuming func appending(_ other: __owned Component) -> FilePath {
|
||||
var copy = self
|
||||
copy.append(other)
|
||||
@@ -539,8 +532,7 @@ extension FilePath {
|
||||
|
||||
/// Non-mutating version of `append(_:C)`.
|
||||
///
|
||||
/// TODO(Windows docs): example with roots
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
public __consuming func appending<C: Collection>(
|
||||
_ components: __owned C
|
||||
) -> FilePath where C.Element == FilePath.Component {
|
||||
@@ -551,8 +543,7 @@ extension FilePath {
|
||||
|
||||
/// Non-mutating version of `append(_:String)`.
|
||||
///
|
||||
/// TODO(Windows docs): example with roots
|
||||
///
|
||||
// TODO(Windows docs): example with roots
|
||||
public __consuming func appending(_ other: __owned String) -> FilePath {
|
||||
var copy = self
|
||||
copy.append(other)
|
||||
@@ -573,23 +564,21 @@ extension FilePath {
|
||||
/// path.push("dir/file.txt") // path is "/tmp/dir/file.txt"
|
||||
/// path.push("/bin") // path is "/bin"
|
||||
///
|
||||
/// TODO(Windows docs): examples and docs with roots, update/generalize doc
|
||||
/// comment
|
||||
///
|
||||
// TODO(Windows docs): examples and docs with roots, update/generalize doc
|
||||
// comment
|
||||
public mutating func push(_ other: __owned FilePath) {
|
||||
defer { _invariantCheck() }
|
||||
guard other.root == nil else {
|
||||
self = other
|
||||
return
|
||||
}
|
||||
/// FIXME: Windows drive-relative roots, etc?
|
||||
// FIXME: Windows drive-relative roots, etc?
|
||||
_append(unchecked: other._storage[...])
|
||||
}
|
||||
|
||||
/// Non-mutating version of `push()`
|
||||
///
|
||||
/// TODO(Windows docs): examples and docs with roots
|
||||
///
|
||||
// TODO(Windows docs): examples and docs with roots
|
||||
public __consuming func pushing(_ other: __owned FilePath) -> FilePath {
|
||||
var copy = self
|
||||
copy.push(other)
|
||||
|
||||
@@ -156,7 +156,7 @@ internal struct WindowsRootInfo {
|
||||
/// * Device disk: `\\.\C:\`, `\\?\C:\`
|
||||
/// * UNC: `\\server\e:\`, `\\?\UNC\server\e:\`
|
||||
///
|
||||
/// TODO: NT paths? Admin paths using `$`?
|
||||
// TODO: NT paths? Admin paths using `$`?
|
||||
case drive(Character)
|
||||
|
||||
/// A volume with a GUID in a non-traditional path
|
||||
@@ -164,7 +164,7 @@ internal struct WindowsRootInfo {
|
||||
/// * UNC: `\\host\Volume{0000-...}\`, `\\.\UNC\host\Volume{0000-...}\`
|
||||
/// * Device roots: `\\.\Volume{0000-...}\`, `\\?\Volume{000-...}\`
|
||||
///
|
||||
/// TODO: GUID type?
|
||||
// TODO: GUID type?
|
||||
case guid(String)
|
||||
|
||||
// TODO: Legacy DOS devices, such as COM1?
|
||||
|
||||
@@ -498,7 +498,6 @@ internal var _O_EVTONLY: CInt { O_EVTONLY }
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _O_NOCTTY: CInt { O_NOCTTY }
|
||||
|
||||
// TODO: API?
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _O_DIRECTORY: CInt { O_DIRECTORY }
|
||||
#endif
|
||||
|
||||
@@ -146,7 +146,7 @@ private func originalSyscallName(_ function: String) -> String {
|
||||
|
||||
private func mockImpl(
|
||||
name: String,
|
||||
path: UnsafePointer<CChar>?,
|
||||
path: UnsafePointer<CInterop.PlatformChar>?,
|
||||
_ args: [AnyHashable]
|
||||
) -> CInt {
|
||||
precondition(mockingEnabled)
|
||||
@@ -177,18 +177,18 @@ private func mockImpl(
|
||||
}
|
||||
|
||||
internal func _mock(
|
||||
name: String = #function, path: UnsafePointer<CChar>? = nil, _ args: AnyHashable...
|
||||
name: String = #function, path: UnsafePointer<CInterop.PlatformChar>? = nil, _ args: AnyHashable...
|
||||
) -> CInt {
|
||||
return mockImpl(name: name, path: path, args)
|
||||
}
|
||||
internal func _mockInt(
|
||||
name: String = #function, path: UnsafePointer<CChar>? = nil, _ args: AnyHashable...
|
||||
name: String = #function, path: UnsafePointer<CInterop.PlatformChar>? = nil, _ args: AnyHashable...
|
||||
) -> Int {
|
||||
Int(mockImpl(name: name, path: path, args))
|
||||
}
|
||||
|
||||
internal func _mockOffT(
|
||||
name: String = #function, path: UnsafePointer<CChar>? = nil, _ args: AnyHashable...
|
||||
name: String = #function, path: UnsafePointer<CInterop.PlatformChar>? = nil, _ args: AnyHashable...
|
||||
) -> _COffT {
|
||||
_COffT(mockImpl(name: name, path: path, args))
|
||||
}
|
||||
|
||||
@@ -53,8 +53,8 @@ extension SystemChar {
|
||||
internal var isLetter: Bool {
|
||||
guard isASCII else { return false }
|
||||
let asciiRaw: UInt8 = numericCast(rawValue)
|
||||
return (UInt8(ascii: "a") ..< UInt8(ascii: "z")).contains(asciiRaw) ||
|
||||
(UInt8(ascii: "A") ..< UInt8(ascii: "Z")).contains(asciiRaw)
|
||||
return (UInt8(ascii: "a") ... UInt8(ascii: "z")).contains(asciiRaw) ||
|
||||
(UInt8(ascii: "A") ... UInt8(ascii: "Z")).contains(asciiRaw)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
|
||||
Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
@testable import SystemPackage
|
||||
#else
|
||||
@testable import System
|
||||
#endif
|
||||
|
||||
final class SystemCharTest: XCTestCase {
|
||||
func testIsLetter() {
|
||||
let valid = SystemString(
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
for char in valid {
|
||||
XCTAssertTrue(char.isLetter)
|
||||
}
|
||||
|
||||
// non printable
|
||||
for value in 0..<(UInt8(ascii: " ")) {
|
||||
XCTAssertFalse(SystemChar(codeUnit: value).isLetter)
|
||||
}
|
||||
XCTAssertFalse(SystemChar(codeUnit: 0x7F).isLetter) // DEL
|
||||
|
||||
// misc other
|
||||
let invalid = SystemString(
|
||||
##" !"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~"##)
|
||||
for char in invalid {
|
||||
XCTAssertFalse(char.isLetter)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,6 +94,15 @@ extension MockingTest {
|
||||
]
|
||||
}
|
||||
|
||||
extension SystemCharTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__SystemCharTest = [
|
||||
("testIsLetter", testIsLetter),
|
||||
]
|
||||
}
|
||||
|
||||
extension SystemStringTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
@@ -115,6 +124,7 @@ public func __allTests() -> [XCTestCaseEntry] {
|
||||
testCase(FilePathTest.__allTests__FilePathTest),
|
||||
testCase(FilePermissionsTest.__allTests__FilePermissionsTest),
|
||||
testCase(MockingTest.__allTests__MockingTest),
|
||||
testCase(SystemCharTest.__allTests__SystemCharTest),
|
||||
testCase(SystemStringTest.__allTests__SystemStringTest),
|
||||
]
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user