Compare commits

...
6 Commits
Author SHA1 Message Date
Michael Ilseman ce8b491073 Update README for 1.0 2021-09-01 09:26:18 -06:00
YR Chen 39774ef16a Simplify Package.swift 2021-08-05 10:05:06 -06:00
YR Chen 58a2725013 Fix building on Windows 2021-08-05 10:05:06 -06:00
Rauhul Varma e520e61616 Fix SystemChar isLetter logic
- Fixes range expression in isLetter logic and adds test coverage.
2021-08-05 10:03:53 -06:00
Rauhul Varma 4f36710d0b Expose O_DIRECTORY as api
- Adds a `directory` OpenOptions as a type-safe version of O_DIRECTORY.
  Allows clients to only open a FileDescriptor for FilePaths that refer
  to directories.
2021-07-14 18:24:52 -06:00
Alex Martini 591ff41944 Move TODO and FIXME markers to normal comments.
When they appear in triple-slash comments, that makes them part of the
actual documentation that gets published.

Fixes <rdar://problem/79071171>.
2021-06-10 16:34:32 -06:00
11 changed files with 120 additions and 71 deletions

No files matched your search

+22 -21
View File
@@ -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")
]),
]
)
+8 -11
View File
@@ -28,16 +28,7 @@ To use the `SystemPackage` library in a SwiftPM project,
add the following line to the dependencies in your `Package.swift` file:
```swift
.package(url: "https://github.com/apple/swift-system", from: "0.0.1"),
```
Because `SystemPackage` is under active development,
source-stability is only guaranteed within minor versions (e.g. between `0.0.3` and `0.0.4`).
If you don't want potentially source-breaking package updates,
use this dependency specification instead:
```swift
.package(url: "https://github.com/apple/swift-system", .upToNextMinor(from: "0.0.1")),
.package(url: "https://github.com/apple/swift-system", from: "1.0.0"),
```
Finally, include `"SystemPackage"` as a dependency for your executable target:
@@ -46,7 +37,7 @@ Finally, include `"SystemPackage"` as a dependency for your executable target:
let package = Package(
// name, platforms, products, etc.
dependencies: [
.package(url: "https://github.com/apple/swift-system", from: "0.0.1"),
.package(url: "https://github.com/apple/swift-system", from: "1.0.0"),
// other dependencies
],
targets: [
@@ -58,6 +49,12 @@ let package = Package(
)
```
## Source Stability
The Swift System package is source stable. The version numbers follow [Semantic Versioning][semver] -- source breaking changes to public API can only land in a new major version.
[semver]: https://semver.org
## Contributing
Before contributing, please read [CONTRIBUTING.md](CONTRIBUTING.md).
+14
View File
@@ -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)
+3 -3
View File
@@ -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
+16 -27
View File
@@ -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?
-1
View File
@@ -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
+4 -4
View File
@@ -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))
}
+2 -2
View File
@@ -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)
}
}
+39
View File
@@ -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)
}
}
}
+10
View File
@@ -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),
]
}