Update FilePath references to NIOFilePath (#3333)

Motivation:

Currently, file paths are represented by the `SystemPackage.FilePath`
type in `NIOFileSystem`. Following from #3322, we want to use
`NIOFilePath` instead.

Modifications:

This change provides `NIOFilePath` accepting/returning variants to all
existing public methods that accept/return `SystemPackage.FilePath`.

The bulk of the changes are in `FileSystemProtocol` and
`FileHandleProtocol`, and their conforming types. The protocols have
been modified to require `NIOFilePath` accepting/returning methods.
Default implementations are provided for `SystemPackage.FilePath`
accepting/returning methods---these call through to the `NIOFilePath`
variants.

Result:

`NIOFilePath` path representations can now be used with the methods for
interacting with the file system.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
This commit is contained in:
aryan-25andGeorge Barnett authored and GitHub committed 2025-08-11 17:29:02 +01:00
1 parent b881f6ec60
commit 8997a24524
20 files changed
+1387 -191

No files matched your search

+31 -3
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -24,7 +24,7 @@ extension Array where Element == UInt8 {
/// - maximumSizeAllowed: The maximum size of file which can be read, in bytes, as a ``ByteCount``.
/// - fileSystem: The ``FileSystemProtocol`` instance to use to read the file.
public init(
contentsOf path: FilePath,
contentsOf path: NIOFilePath,
maximumSizeAllowed: ByteCount,
fileSystem: some FileSystemProtocol
) async throws {
@@ -35,13 +35,28 @@ extension Array where Element == UInt8 {
self = Self(buffer: byteBuffer)
}
/// Reads the contents of the file at the path.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, in bytes, as a ``ByteCount``.
/// - fileSystem: The ``FileSystemProtocol`` instance to use to read the file.
@_disfavoredOverload
public init(
contentsOf path: FilePath,
maximumSizeAllowed: ByteCount,
fileSystem: some FileSystemProtocol
) async throws {
try await self.init(contentsOf: .init(path), maximumSizeAllowed: maximumSizeAllowed, fileSystem: fileSystem)
}
/// Reads the contents of the file at the path using ``FileSystem``.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, as a ``ByteCount``.
public init(
contentsOf path: FilePath,
contentsOf path: NIOFilePath,
maximumSizeAllowed: ByteCount
) async throws {
self = try await Self(
@@ -50,5 +65,18 @@ extension Array where Element == UInt8 {
fileSystem: .shared
)
}
/// Reads the contents of the file at the path using ``FileSystem``.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, as a ``ByteCount``.
@_disfavoredOverload
public init(
contentsOf path: FilePath,
maximumSizeAllowed: ByteCount
) async throws {
try await self.init(contentsOf: .init(path), maximumSizeAllowed: maximumSizeAllowed)
}
}
#endif
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -24,7 +24,7 @@ extension ArraySlice where Element == UInt8 {
/// - maximumSizeAllowed: The maximum size of file which can be read, in bytes, as a ``ByteCount``.
/// - fileSystem: The ``FileSystemProtocol`` instance to use to read the file.
public init(
contentsOf path: FilePath,
contentsOf path: NIOFilePath,
maximumSizeAllowed: ByteCount,
fileSystem: some FileSystemProtocol
) async throws {
@@ -37,13 +37,28 @@ extension ArraySlice where Element == UInt8 {
self = Self(bytes)
}
/// Reads the contents of the file at the path.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, in bytes, as a ``ByteCount``.
/// - fileSystem: The ``FileSystemProtocol`` instance to use to read the file.
@_disfavoredOverload
public init(
contentsOf path: FilePath,
maximumSizeAllowed: ByteCount,
fileSystem: some FileSystemProtocol
) async throws {
try await self.init(contentsOf: .init(path), maximumSizeAllowed: maximumSizeAllowed, fileSystem: fileSystem)
}
/// Reads the contents of the file at the path using ``FileSystem``.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, as a ``ByteCount``.
public init(
contentsOf path: FilePath,
contentsOf path: NIOFilePath,
maximumSizeAllowed: ByteCount
) async throws {
self = try await Self(
@@ -52,5 +67,18 @@ extension ArraySlice where Element == UInt8 {
fileSystem: .shared
)
}
/// Reads the contents of the file at the path using ``FileSystem``.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, as a ``ByteCount``.
@_disfavoredOverload
public init(
contentsOf path: FilePath,
maximumSizeAllowed: ByteCount
) async throws {
try await self.init(contentsOf: .init(path), maximumSizeAllowed: maximumSizeAllowed)
}
}
#endif
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -23,7 +23,7 @@ extension ByteBuffer {
/// - maximumSizeAllowed: The maximum size of file which can be read, in bytes, as a ``ByteCount``.
/// - fileSystem: The ``FileSystemProtocol`` instance to use to read the file.
public init(
contentsOf path: FilePath,
contentsOf path: NIOFilePath,
maximumSizeAllowed: ByteCount,
fileSystem: some FileSystemProtocol
) async throws {
@@ -35,13 +35,28 @@ extension ByteBuffer {
}
}
/// Reads the contents of the file at the path.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, in bytes, as a ``ByteCount``.
/// - fileSystem: The ``FileSystemProtocol`` instance to use to read the file.
@_disfavoredOverload
public init(
contentsOf path: FilePath,
maximumSizeAllowed: ByteCount,
fileSystem: some FileSystemProtocol
) async throws {
try await self.init(contentsOf: .init(path), maximumSizeAllowed: maximumSizeAllowed, fileSystem: fileSystem)
}
/// Reads the contents of the file at the path using ``FileSystem``.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, as a ``ByteCount``.
public init(
contentsOf path: FilePath,
contentsOf path: NIOFilePath,
maximumSizeAllowed: ByteCount
) async throws {
self = try await Self(
@@ -51,6 +66,19 @@ extension ByteBuffer {
)
}
/// Reads the contents of the file at the path using ``FileSystem``.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, as a ``ByteCount``.
@_disfavoredOverload
public init(
contentsOf path: FilePath,
maximumSizeAllowed: ByteCount
) async throws {
try await self.init(contentsOf: .init(path), maximumSizeAllowed: maximumSizeAllowed)
}
/// Writes the readable bytes of the ``ByteBuffer`` to a file.
///
/// - Parameters:
@@ -61,7 +89,7 @@ extension ByteBuffer {
/// - Returns: The number of bytes written to the file.
@discardableResult
public func write(
toFileAt path: FilePath,
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
@@ -80,11 +108,30 @@ extension ByteBuffer {
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - fileSystem: The ``FileSystemProtocol`` instance to use.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
) async throws -> Int64 {
try await self.write(toFileAt: .init(path), absoluteOffset: offset, options: options, fileSystem: fileSystem)
}
/// Writes the readable bytes of the ``ByteBuffer`` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@discardableResult
public func write(
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(
@@ -94,4 +141,21 @@ extension ByteBuffer {
fileSystem: .shared
)
}
/// Writes the readable bytes of the ``ByteBuffer`` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(toFileAt: .init(path), absoluteOffset: offset, options: options)
}
}
+173 -5
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -26,7 +26,7 @@ extension String {
@discardableResult
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func write(
toFileAt path: FilePath,
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
@@ -45,12 +45,37 @@ extension String {
/// - path: The path of the file to write the `String` to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - fileSystem: The ``FileSystemProtocol`` instance to use.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
) async throws -> Int64 {
try await self.write(
toFileAt: .init(path),
absoluteOffset: offset,
options: options,
fileSystem: fileSystem
)
}
/// Writes the UTF8 encoded `String` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the `String` to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@discardableResult
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func write(
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(
@@ -60,6 +85,24 @@ extension String {
fileSystem: .shared
)
}
/// Writes the UTF8 encoded `String` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the `String` to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(toFileAt: .init(path), absoluteOffset: offset, options: options)
}
}
extension Sequence<UInt8> where Self: Sendable {
@@ -74,7 +117,7 @@ extension Sequence<UInt8> where Self: Sendable {
@discardableResult
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func write(
toFileAt path: FilePath,
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
@@ -90,12 +133,37 @@ extension Sequence<UInt8> where Self: Sendable {
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - fileSystem: The ``FileSystemProtocol`` instance to use.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
) async throws -> Int64 {
try await self.write(
toFileAt: .init(path),
absoluteOffset: offset,
options: options,
fileSystem: fileSystem
)
}
/// Writes the contents of the `Sequence` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@discardableResult
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func write(
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(
@@ -105,6 +173,24 @@ extension Sequence<UInt8> where Self: Sendable {
fileSystem: .shared
)
}
/// Writes the contents of the `Sequence` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(toFileAt: .init(path), absoluteOffset: offset, options: options)
}
}
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
@@ -119,7 +205,7 @@ extension AsyncSequence where Self.Element: Sequence<UInt8>, Self: Sendable {
/// - Returns: The number of bytes written to the file.
@discardableResult
public func write(
toFileAt path: FilePath,
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
@@ -138,11 +224,35 @@ extension AsyncSequence where Self.Element: Sequence<UInt8>, Self: Sendable {
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - fileSystem: The ``FileSystemProtocol`` instance to use.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
) async throws -> Int64 {
try await self.write(
toFileAt: .init(path),
absoluteOffset: offset,
options: options,
fileSystem: fileSystem
)
}
/// Writes the contents of the `AsyncSequence` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@discardableResult
public func write(
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(
@@ -152,6 +262,23 @@ extension AsyncSequence where Self.Element: Sequence<UInt8>, Self: Sendable {
fileSystem: .shared
)
}
/// Writes the contents of the `AsyncSequence` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(toFileAt: .init(path), absoluteOffset: offset, options: options)
}
}
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
@@ -166,7 +293,7 @@ extension AsyncSequence where Self.Element == UInt8, Self: Sendable {
/// - Returns: The number of bytes written to the file.
@discardableResult
public func write(
toFileAt path: FilePath,
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
@@ -185,11 +312,35 @@ extension AsyncSequence where Self.Element == UInt8, Self: Sendable {
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - fileSystem: The ``FileSystemProtocol`` instance to use.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false),
fileSystem: some FileSystemProtocol
) async throws -> Int64 {
try await self.write(
toFileAt: .init(path),
absoluteOffset: offset,
options: options,
fileSystem: fileSystem
)
}
/// Writes the contents of the `AsyncSequence` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@discardableResult
public func write(
toFileAt path: NIOFilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(
@@ -199,4 +350,21 @@ extension AsyncSequence where Self.Element == UInt8, Self: Sendable {
fileSystem: .shared
)
}
/// Writes the contents of the `AsyncSequence` to a file.
///
/// - Parameters:
/// - path: The path of the file to write the contents of the sequence to.
/// - offset: The offset into the file to write to, defaults to zero.
/// - options: Options for opening the file, defaults to creating a new file.
/// - Returns: The number of bytes written to the file.
@_disfavoredOverload
@discardableResult
public func write(
toFileAt path: FilePath,
absoluteOffset offset: Int64 = 0,
options: OpenOptions.Write = .newFile(replaceExisting: false)
) async throws -> Int64 {
try await self.write(toFileAt: .init(path), absoluteOffset: offset, options: options)
}
}
+22 -5
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -19,7 +19,14 @@ public struct DirectoryEntry: Sendable, Hashable, Equatable {
/// The path of the directory entry.
///
/// - Precondition: The path must have at least one component.
public let path: FilePath
public let filePath: NIOFilePath
/// The path of the directory entry.
///
/// - Note: This property returns the underlying `SystemPackage.FilePath` instance of ``filePath``.
public var path: FilePath {
self.filePath.underlying
}
/// The name of the entry; the final component of the ``path``.
///
@@ -36,12 +43,22 @@ public struct DirectoryEntry: Sendable, Hashable, Equatable {
/// - Parameters:
/// - path: The path of the directory entry which must contain at least one component.
/// - type: The type of entry.
public init?(path: FilePath, type: FileType) {
if path.components.isEmpty {
public init?(path: NIOFilePath, type: FileType) {
if path.underlying.components.isEmpty {
return nil
}
self.path = path
self.filePath = path
self.type = type
}
/// Creates a directory entry; returns `nil` if `path` has no components.
///
/// - Parameters:
/// - path: The path of the directory entry which must contain at least one component.
/// - type: The type of entry.
@_disfavoredOverload
public init?(path: FilePath, type: FileType) {
self.init(path: .init(path), type: type)
}
}
@@ -11,10 +11,10 @@
Open files and directories at paths relative to the directory handle. These methods manage
the lifecycle of the handles by closing them when the `execute` closure returns.
- ``withFileHandle(forReadingAt:options:execute:)``
- ``withFileHandle(forWritingAt:options:execute:)``
- ``withFileHandle(forReadingAndWritingAt:options:execute:)``
- ``withDirectoryHandle(atPath:options:execute:)``
- ``withFileHandle(forReadingAt:options:execute:)-52xsn``
- ``withFileHandle(forWritingAt:options:execute:)-36f0k``
- ``withFileHandle(forReadingAndWritingAt:options:execute:)-5j0f3``
- ``withDirectoryHandle(atPath:options:execute:)-2jszb``
### Opening files
@@ -7,17 +7,17 @@
Files and directories can be opened and have their lifecycles managed by using the
following methods:
- ``withFileHandle(forReadingAt:options:execute:)``
- ``withFileHandle(forWritingAt:options:execute:)``
- ``withFileHandle(forReadingAndWritingAt:options:execute:)``
- ``withDirectoryHandle(atPath:options:execute:)``
- ``withFileHandle(forReadingAt:options:execute:)-nsue``
- ``withFileHandle(forWritingAt:options:execute:)-1p6ka``
- ``withFileHandle(forReadingAndWritingAt:options:execute:)-9nqu3``
- ``withDirectoryHandle(atPath:options:execute:)-4wzzz``
### Opening files
Files and directories can be opened using the following methods. The caller is responsible for
closing it to avoid leaking resources.
- ``openFile(forReadingAt:)``
- ``openFile(forReadingAt:)-6v2b8``
- ``openFile(forWritingAt:options:)``
- ``openFile(forReadingAndWritingAt:options:)``
- ``openDirectory(atPath:options:)``
@@ -33,8 +33,8 @@ closing it to avoid leaking resources.
### Managing files
- ``copyItem(at:to:shouldProceedAfterError:shouldCopyFile:)``
- ``removeItem(at:)``
- ``copyItem(at:to:strategy:shouldProceedAfterError:shouldCopyItem:)``
- ``removeItem(at:)-1vii4``
- ``moveItem(at:to:)``
- ``replaceItem(at:withItemAt:)``
- ``createDirectory(at:withIntermediateDirectories:permissions:)``
@@ -43,4 +43,4 @@ closing it to avoid leaking resources.
- ``currentWorkingDirectory``
- ``temporaryDirectory``
- ``withTemporaryDirectory(prefix:options:execute:)``
- ``withTemporaryDirectory(prefix:options:execute:)-rbkk``
+5 -5
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -312,7 +312,7 @@ public struct DirectoryFileHandle: DirectoryFileHandleProtocol, _HasFileHandle,
}
public func openFile(
forReadingAt path: FilePath,
forReadingAt path: NIOFilePath,
options: OpenOptions.Read
) async throws -> ReadFileHandle {
let systemFileHandle = try await self.fileHandle.systemFileHandle.openFile(
@@ -323,7 +323,7 @@ public struct DirectoryFileHandle: DirectoryFileHandleProtocol, _HasFileHandle,
}
public func openFile(
forWritingAt path: FilePath,
forWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> WriteFileHandle {
let systemFileHandle = try await self.fileHandle.systemFileHandle.openFile(
@@ -334,7 +334,7 @@ public struct DirectoryFileHandle: DirectoryFileHandleProtocol, _HasFileHandle,
}
public func openFile(
forReadingAndWritingAt path: FilePath,
forReadingAndWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> ReadWriteFileHandle {
let systemFileHandle = try await self.fileHandle.systemFileHandle.openFile(
@@ -345,7 +345,7 @@ public struct DirectoryFileHandle: DirectoryFileHandleProtocol, _HasFileHandle,
}
public func openDirectory(
atPath path: FilePath,
atPath path: NIOFilePath,
options: OpenOptions.Directory
) async throws -> DirectoryFileHandle {
let systemFileHandle = try await self.fileHandle.systemFileHandle.openDirectory(
+180 -15
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -584,7 +584,7 @@ public protocol DirectoryFileHandleProtocol: FileHandleProtocol {
/// - options: How the file should be opened.
/// - Returns: A read-only handle to the opened file.
func openFile(
forReadingAt path: FilePath,
forReadingAt path: NIOFilePath,
options: OpenOptions.Read
) async throws -> ReadFileHandle
@@ -597,7 +597,7 @@ public protocol DirectoryFileHandleProtocol: FileHandleProtocol {
/// - options: How the file should be opened.
/// - Returns: A write-only handle to the opened file.
func openFile(
forWritingAt path: FilePath,
forWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> WriteFileHandle
@@ -609,7 +609,7 @@ public protocol DirectoryFileHandleProtocol: FileHandleProtocol {
/// - path: The path of the file to open relative to the open file.
/// - options: How the file should be opened.
func openFile(
forReadingAndWritingAt path: FilePath,
forReadingAndWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> ReadWriteFileHandle
@@ -623,11 +623,80 @@ public protocol DirectoryFileHandleProtocol: FileHandleProtocol {
/// - options: How the file should be opened.
/// - Returns: A handle to the opened directory.
func openDirectory(
atPath path: FilePath,
atPath path: NIOFilePath,
options: OpenOptions.Directory
) async throws -> Self
}
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension DirectoryFileHandleProtocol {
/// Opens the file at `path` for reading and returns a handle to it.
///
/// If `path` is a relative path then it is opened relative to the handle. The file being
/// opened must already exist otherwise this function will throw a ``FileSystemError`` with
/// code ``FileSystemError/Code-swift.struct/notFound``.
///
/// - Parameters:
/// - path: The path of the directory to open relative to the open file.
/// - options: How the file should be opened.
/// - Returns: A read-only handle to the opened file.
@_disfavoredOverload
public func openFile(
forReadingAt path: FilePath,
options: OpenOptions.Read
) async throws -> ReadFileHandle {
try await self.openFile(forReadingAt: .init(path), options: options)
}
/// Opens the file at `path` for writing and returns a handle to it.
///
/// If `path` is a relative path then it is opened relative to the handle.
///
/// - Parameters:
/// - path: The path of the file to open relative to the open file.
/// - options: How the file should be opened.
/// - Returns: A write-only handle to the opened file.
@_disfavoredOverload
public func openFile(
forWritingAt path: FilePath,
options: OpenOptions.Write
) async throws -> WriteFileHandle {
try await self.openFile(forWritingAt: .init(path), options: options)
}
/// Opens the file at `path` for reading and writing and returns a handle to it.
///
/// If `path` is a relative path then it is opened relative to the handle.
///
/// - Parameters:
/// - path: The path of the file to open relative to the open file.
/// - options: How the file should be opened.
@_disfavoredOverload
public func openFile(
forReadingAndWritingAt path: FilePath,
options: OpenOptions.Write
) async throws -> ReadWriteFileHandle {
try await self.openFile(forReadingAndWritingAt: .init(path), options: options)
}
/// Opens the directory at `path` and returns a handle to it.
///
/// The directory being opened must already exist otherwise this function will throw an error.
/// If `path` is a relative path then it is opened relative to the handle.
///
/// - Parameters:
/// - path: The path of the directory to open.
/// - options: How the file should be opened.
/// - Returns: A handle to the opened directory.
@_disfavoredOverload
public func openDirectory(
atPath path: FilePath,
options: OpenOptions.Directory
) async throws -> Self {
try await self.openDirectory(atPath: .init(path), options: options)
}
}
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension DirectoryFileHandleProtocol {
/// Returns an `AsyncSequence` of entries in the open directory.
@@ -645,8 +714,8 @@ extension DirectoryFileHandleProtocol {
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in write-only and read-write
/// mode by calling ``withFileHandle(forWritingAt:options:execute:)``
/// and ``withFileHandle(forReadingAndWritingAt:options:execute:)``,
/// mode by calling ``DirectoryFileHandleProtocol/withFileHandle(forWritingAt:options:execute:)-36f0k``
/// and ``DirectoryFileHandleProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)-5j0f3``,
/// respectively.
///
/// If `path` is a relative path then it is opened relative to the handle. The file being
@@ -661,7 +730,7 @@ extension DirectoryFileHandleProtocol {
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withFileHandle<Result>(
forReadingAt path: FilePath,
forReadingAt path: NIOFilePath,
options: OpenOptions.Read = OpenOptions.Read(),
execute body: (_ read: ReadFileHandle) async throws -> Result
) async throws -> Result {
@@ -674,12 +743,40 @@ extension DirectoryFileHandleProtocol {
}
}
/// Opens the file at the given path and provides scoped read access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in write-only and read-write
/// mode by calling ``DirectoryFileHandleProtocol/withFileHandle(forWritingAt:options:execute:)-8xwbq``
/// and ``DirectoryFileHandleProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)-5zcub``,
/// respectively.
///
/// If `path` is a relative path then it is opened relative to the handle. The file being
/// opened must already exist otherwise this function will throw a ``FileSystemError`` with
/// code ``FileSystemError/Code-swift.struct/notFound``.
///
/// - Parameters:
/// - path: The path of the file to open for reading.
/// - options: How the file should be opened.
/// - body: A closure which provides read-only access to the open file. The file is closed
/// automatically after the closure exits.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
@_disfavoredOverload
public func withFileHandle<Result>(
forReadingAt path: FilePath,
options: OpenOptions.Read = OpenOptions.Read(),
execute body: (_ read: ReadFileHandle) async throws -> Result
) async throws -> Result {
try await self.withFileHandle(forReadingAt: .init(path), options: options, execute: body)
}
/// Opens the file at the given path and provides scoped write access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in read-only or read-write
/// mode by calling ``withFileHandle(forReadingAt:options:execute:)`` and
/// ``withFileHandle(forReadingAndWritingAt:options:execute:)``,
/// mode by calling ``DirectoryFileHandleProtocol/withFileHandle(forReadingAt:options:execute:)-52xsn`` and
/// ``DirectoryFileHandleProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)-5j0f3``,
/// respectively.
///
/// If `path` is a relative path then it is opened relative to the handle.
@@ -692,7 +789,7 @@ extension DirectoryFileHandleProtocol {
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withFileHandle<Result>(
forWritingAt path: FilePath,
forWritingAt path: NIOFilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute body: (_ write: WriteFileHandle) async throws -> Result
) async throws -> Result {
@@ -710,12 +807,38 @@ extension DirectoryFileHandleProtocol {
}
}
/// Opens the file at the given path and provides scoped write access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in read-only or read-write
/// mode by calling ``DirectoryFileHandleProtocol/withFileHandle(forReadingAt:options:execute:)-6cqam`` and
/// ``DirectoryFileHandleProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)-5zcub``,
/// respectively.
///
/// If `path` is a relative path then it is opened relative to the handle.
///
/// - Parameters:
/// - path: The path of the file to open for reading.
/// - options: How the file should be opened.
/// - body: A closure which provides write-only access to the open file. The file is closed
/// automatically after the closure exits.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
@_disfavoredOverload
public func withFileHandle<Result>(
forWritingAt path: FilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute body: (_ write: WriteFileHandle) async throws -> Result
) async throws -> Result {
try await self.withFileHandle(forWritingAt: .init(path), options: options, execute: body)
}
/// Opens the file at the given path and provides scoped read-write access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in read-only or write-only
/// mode by calling ``withFileHandle(forReadingAt:options:execute:)`` and
/// ``withFileHandle(forWritingAt:options:execute:)``, respectively.
/// mode by calling ``DirectoryFileHandleProtocol/withFileHandle(forReadingAt:options:execute:)-52xsn`` and
/// ``DirectoryFileHandleProtocol/withFileHandle(forWritingAt:options:execute:)-36f0k``, respectively.
///
/// If `path` is a relative path then it is opened relative to the handle.
///
@@ -727,7 +850,7 @@ extension DirectoryFileHandleProtocol {
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withFileHandle<Result>(
forReadingAndWritingAt path: FilePath,
forReadingAndWritingAt path: NIOFilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute body: (_ readWrite: ReadWriteFileHandle) async throws -> Result
) async throws -> Result {
@@ -745,6 +868,31 @@ extension DirectoryFileHandleProtocol {
}
}
/// Opens the file at the given path and provides scoped read-write access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in read-only or write-only
/// mode by calling ``DirectoryFileHandleProtocol/withFileHandle(forReadingAt:options:execute:)-6cqam`` and
/// ``DirectoryFileHandleProtocol/withFileHandle(forWritingAt:options:execute:)-8xwbq``, respectively.
///
/// If `path` is a relative path then it is opened relative to the handle.
///
/// - Parameters:
/// - path: The path of the file to open for reading and writing.
/// - options: How the file should be opened.
/// - body: A closure which provides access to the open file. The file is closed
/// automatically after the closure exits.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
@_disfavoredOverload
public func withFileHandle<Result>(
forReadingAndWritingAt path: FilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute body: (_ readWrite: ReadWriteFileHandle) async throws -> Result
) async throws -> Result {
try await self.withFileHandle(forReadingAndWritingAt: .init(path), options: options, execute: body)
}
/// Opens the directory at the given path and provides scoped access to it.
///
/// - Parameters:
@@ -754,7 +902,7 @@ extension DirectoryFileHandleProtocol {
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withDirectoryHandle<Result>(
atPath path: FilePath,
atPath path: NIOFilePath,
options: OpenOptions.Directory = OpenOptions.Directory(),
execute body: (_ directory: Self) async throws -> Result
) async throws -> Result {
@@ -766,4 +914,21 @@ extension DirectoryFileHandleProtocol {
try await handle.close()
}
}
/// Opens the directory at the given path and provides scoped access to it.
///
/// - Parameters:
/// - path: The path of the directory to open.
/// - options: How the directory should be opened.
/// - body: A closure which provides access to the directory.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
@_disfavoredOverload
public func withDirectoryHandle<Result>(
atPath path: FilePath,
options: OpenOptions.Directory = OpenOptions.Directory(),
execute body: (_ directory: Self) async throws -> Result
) async throws -> Result {
try await self.withDirectoryHandle(atPath: .init(path), options: options, execute: body)
}
}
+52 -50
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -104,11 +104,11 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - options: How the file should be opened.
/// - Returns: A readable handle to the opened file.
public func openFile(
forReadingAt path: FilePath,
forReadingAt path: NIOFilePath,
options: OpenOptions.Read
) async throws -> ReadFileHandle {
let handle = try await self.threadPool.runIfActive {
let handle = try self._openFile(forReadingAt: path, options: options).get()
let handle = try self._openFile(forReadingAt: path.underlying, options: options).get()
// Okay to transfer: we just created it and are now moving back to the callers task.
return UnsafeTransfer(handle)
}
@@ -134,11 +134,11 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - options: How the file should be opened.
/// - Returns: A writable handle to the opened file.
public func openFile(
forWritingAt path: FilePath,
forWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> WriteFileHandle {
let handle = try await self.threadPool.runIfActive {
let handle = try self._openFile(forWritingAt: path, options: options).get()
let handle = try self._openFile(forWritingAt: path.underlying, options: options).get()
// Okay to transfer: we just created it and are now moving back to the callers task.
return UnsafeTransfer(handle)
}
@@ -164,11 +164,11 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - options: How the file should be opened.
/// - Returns: A readable and writable handle to the opened file.
public func openFile(
forReadingAndWritingAt path: FilePath,
forReadingAndWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> ReadWriteFileHandle {
let handle = try await self.threadPool.runIfActive {
let handle = try self._openFile(forReadingAndWritingAt: path, options: options).get()
let handle = try self._openFile(forReadingAndWritingAt: path.underlying, options: options).get()
// Okay to transfer: we just created it and are now moving back to the callers task.
return UnsafeTransfer(handle)
}
@@ -191,11 +191,11 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - options: How the directory should be opened.
/// - Returns: A handle to the opened directory.
public func openDirectory(
atPath path: FilePath,
atPath path: NIOFilePath,
options: OpenOptions.Directory
) async throws -> DirectoryFileHandle {
let handle = try await self.threadPool.runIfActive {
let handle = try self._openDirectory(at: path, options: options).get()
let handle = try self._openDirectory(at: path.underlying, options: options).get()
// Okay to transfer: we just created it and are now moving back to the callers task.
return UnsafeTransfer(handle)
}
@@ -229,13 +229,13 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - permissions: The permissions to set on the new directory; default permissions will be
/// used if not specified.
public func createDirectory(
at path: FilePath,
at path: NIOFilePath,
withIntermediateDirectories createIntermediateDirectories: Bool,
permissions: FilePermissions?
) async throws {
try await self.threadPool.runIfActive {
try self._createDirectory(
at: path,
at: path.underlying,
withIntermediateDirectories: createIntermediateDirectories,
permissions: permissions ?? .defaultsForDirectory
).get()
@@ -261,10 +261,10 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - Returns:
/// - The path to the new temporary directory.
public func createTemporaryDirectory(
template: FilePath
) async throws -> FilePath {
template: NIOFilePath
) async throws -> NIOFilePath {
try await self.threadPool.runIfActive {
try self._createTemporaryDirectory(template: template).get()
NIOFilePath(try self._createTemporaryDirectory(template: template.underlying).get())
}
}
@@ -281,11 +281,11 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// destination of the symbolic link is returned.
/// - Returns: Information about the file at the given path or `nil` if no file exists.
public func info(
forFileAt path: FilePath,
forFileAt path: NIOFilePath,
infoAboutSymbolicLink: Bool
) async throws -> FileInfo? {
try await self.threadPool.runIfActive {
try self._info(forFileAt: path, infoAboutSymbolicLink: infoAboutSymbolicLink).get()
try self._info(forFileAt: path.underlying, infoAboutSymbolicLink: infoAboutSymbolicLink).get()
}
}
@@ -311,8 +311,8 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// This function is platform dependent. On Darwin the `copyfile(2)` system call is used and
/// items are cloned where possible. On Linux the `sendfile(2)` system call is used.
public func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
at sourcePath: NIOFilePath,
to destinationPath: NIOFilePath,
strategy copyStrategy: CopyStrategy,
shouldProceedAfterError: @escaping @Sendable (
_ source: DirectoryEntry,
@@ -320,7 +320,7 @@ public struct FileSystem: Sendable, FileSystemProtocol {
) async throws -> Void,
shouldCopyItem: @escaping @Sendable (
_ source: DirectoryEntry,
_ destination: FilePath
_ destination: NIOFilePath
) async -> Bool
) async throws {
guard let info = try await self.info(forFileAt: sourcePath, infoAboutSymbolicLink: true)
@@ -338,19 +338,20 @@ public struct FileSystem: Sendable, FileSystemProtocol {
if await shouldCopyItem(.init(path: sourcePath, type: info.type)!, destinationPath) {
switch info.type {
case .regular:
try await self.copyRegularFile(from: sourcePath, to: destinationPath)
try await self.copyRegularFile(from: sourcePath.underlying, to: destinationPath.underlying)
case .symlink:
try await self.copySymbolicLink(from: sourcePath, to: destinationPath)
try await self.copySymbolicLink(from: sourcePath.underlying, to: destinationPath.underlying)
case .directory:
try await self.copyDirectory(
from: sourcePath,
to: destinationPath,
from: sourcePath.underlying,
to: destinationPath.underlying,
strategy: copyStrategy,
shouldProceedAfterError: shouldProceedAfterError,
shouldCopyItem: shouldCopyItem
)
shouldProceedAfterError: shouldProceedAfterError
) { source, destination in
await shouldCopyItem(source, .init(destination))
}
default:
throw FileSystemError(
@@ -395,12 +396,12 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
public func removeItem(
at path: FilePath,
at path: NIOFilePath,
strategy removalStrategy: RemovalStrategy,
recursively removeItemRecursively: Bool
) async throws -> Int {
// Try to remove the item: we might just get lucky.
let result = try await self.threadPool.runIfActive { Libc.remove(path) }
let result = try await self.threadPool.runIfActive { Libc.remove(path.underlying) }
switch result {
case .success:
@@ -428,13 +429,13 @@ public struct FileSystem: Sendable, FileSystemProtocol {
switch removalStrategy.wrapped {
case .sequential:
return try await self.removeItemSequentially(at: path)
return try await self.removeItemSequentially(at: path.underlying)
case let .parallel(maxDescriptors):
return try await self.removeConcurrently(at: path, maxDescriptors)
return try await self.removeConcurrently(at: path.underlying, maxDescriptors)
}
case let .failure(errno):
throw FileSystemError.remove(errno: errno, path: path, location: .here())
throw FileSystemError.remove(errno: errno, path: path.underlying, location: .here())
}
}
@@ -505,9 +506,9 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - Parameters:
/// - sourcePath: The path to the item to move.
/// - destinationPath: The path at which to place the item.
public func moveItem(at sourcePath: FilePath, to destinationPath: FilePath) async throws {
public func moveItem(at sourcePath: NIOFilePath, to destinationPath: NIOFilePath) async throws {
let result = try await self.threadPool.runIfActive {
try self._moveItem(at: sourcePath, to: destinationPath).get()
try self._moveItem(at: sourcePath.underlying, to: destinationPath.underlying).get()
}
switch result {
@@ -540,8 +541,8 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - destinationPath: The path of the file or directory to replace.
/// - existingPath: The path of the existing file or directory.
public func replaceItem(
at destinationPath: FilePath,
withItemAt existingPath: FilePath
at destinationPath: NIOFilePath,
withItemAt existingPath: NIOFilePath
) async throws {
do {
try await self.removeItem(at: destinationPath, strategy: .platformDefault)
@@ -576,11 +577,11 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - linkPath: The path at which to create the symbolic link.
/// - destinationPath: The path that contains the item that the symbolic link points to.`
public func createSymbolicLink(
at linkPath: FilePath,
withDestination destinationPath: FilePath
at linkPath: NIOFilePath,
withDestination destinationPath: NIOFilePath
) async throws {
try await self.threadPool.runIfActive {
try self._createSymbolicLink(at: linkPath, withDestination: destinationPath).get()
try self._createSymbolicLink(at: linkPath.underlying, withDestination: destinationPath.underlying).get()
}
}
@@ -589,7 +590,7 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// The destination of the symbolic link is not guaranteed to be a valid path, nor is it
/// guaranteed to be an absolute path. If you need to open a file which is the destination of a
/// symbolic link then the appropriate `open` function:
/// - ``openFile(forReadingAt:)``
/// - ``openFile(forReadingAt:)-55z6f``
/// - ``openFile(forWritingAt:options:)``
/// - ``openFile(forReadingAndWritingAt:options:)``
/// - ``openDirectory(atPath:options:)``
@@ -608,10 +609,10 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - Parameter path: The path of a file or directory.
/// - Returns: The path of the file or directory to which the symbolic link points to.
public func destinationOfSymbolicLink(
at path: FilePath
) async throws -> FilePath {
at path: NIOFilePath
) async throws -> NIOFilePath {
try await self.threadPool.runIfActive {
try self._destinationOfSymbolicLink(at: path).get()
NIOFilePath(try self._destinationOfSymbolicLink(at: path.underlying).get())
}
}
@@ -622,13 +623,14 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// Uses the `getcwd(2)` system call.
///
/// - Returns: The path to the current working directory.
public var currentWorkingDirectory: FilePath {
public var currentWorkingDirectory: NIOFilePath {
get async throws {
try await self.threadPool.runIfActive {
let result = try await self.threadPool.runIfActive {
try Libc.getcwd().mapError { errno in
FileSystemError.getcwd(errno: errno, location: .here())
}.get()
}
return .init(result)
}
}
@@ -645,10 +647,10 @@ public struct FileSystem: Sendable, FileSystemProtocol {
/// - On other platforms this returns "/tmp".
///
/// - Returns: The path to a temporary directory.
public var temporaryDirectory: FilePath {
public var temporaryDirectory: NIOFilePath {
get async throws {
if let tmpdir = getenv("TMPDIR") {
return FilePath(String(cString: tmpdir))
return NIOFilePath(String(cString: tmpdir))
}
#if canImport(Darwin)
@@ -656,15 +658,15 @@ public struct FileSystem: Sendable, FileSystemProtocol {
let result = Libc.constr(_CS_DARWIN_USER_TEMP_DIR)
switch result {
case .success(let path):
return FilePath(path)
return NIOFilePath(path)
case .failure(_):
return FilePath("/tmp")
return NIOFilePath("/tmp")
}
}
#elseif os(Android)
return FilePath("/data/local/tmp")
return NIOFilePath("/data/local/tmp")
#else
return FilePath("/tmp")
return NIOFilePath("/tmp")
#endif
}
}
@@ -0,0 +1,336 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// This extension provides implementations of ``FileSystemProtocol`` methods that accept/return ``FilePath`` instead of
/// ``NIOFilePath``.
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension FileSystemProtocol {
// MARK: - File access
/// Opens the file at `path` for reading and returns a handle to it.
///
/// The file being opened must exist otherwise this function will throw a ``FileSystemError``
/// with code ``FileSystemError/Code-swift.struct/notFound``.
///
/// - Parameters:
/// - path: The path of the file to open.
/// - options: How the file should be opened.
/// - Returns: A readable handle to the opened file.
@_disfavoredOverload
public func openFile(
forReadingAt path: FilePath,
options: OpenOptions.Read
) async throws -> ReadFileHandle {
try await self.openFile(forReadingAt: .init(path), options: options)
}
/// Opens the file at `path` for writing and returns a handle to it.
///
/// - Parameters:
/// - path: The path of the file to open relative to the open file.
/// - options: How the file should be opened.
/// - Returns: A writable handle to the opened file.
@_disfavoredOverload
public func openFile(
forWritingAt path: FilePath,
options: OpenOptions.Write
) async throws -> WriteFileHandle {
try await self.openFile(forWritingAt: .init(path), options: options)
}
/// Opens the file at `path` for reading and writing and returns a handle to it.
///
/// - Parameters:
/// - path: The path of the file to open relative to the open file.
/// - options: How the file should be opened.
@_disfavoredOverload
public func openFile(
forReadingAndWritingAt path: FilePath,
options: OpenOptions.Write
) async throws -> ReadWriteFileHandle {
try await self.openFile(forReadingAndWritingAt: .init(path), options: options)
}
/// Opens the directory at `path` and returns a handle to it.
///
/// The directory being opened must already exist otherwise this function will throw an error.
/// Use ``createDirectory(at:withIntermediateDirectories:permissions:)`` to create directories.
///
/// - Parameters:
/// - path: The path of the directory to open.
/// - options: How the directory should be opened.
/// - Returns: A handle to the opened directory.
@_disfavoredOverload
public func openDirectory(
atPath path: FilePath,
options: OpenOptions.Directory
) async throws -> DirectoryFileHandle {
try await self.openDirectory(atPath: .init(path), options: options)
}
/// Create a directory at the given path.
///
/// If a directory (or file) already exists at `path` then an error will be thrown. If
/// `createIntermediateDirectories` is `false` then the full prefix of `path` must already
/// exist. If set to `true` then all intermediate directories will be created.
///
/// Related system calls: `mkdir(2)`.
///
/// - Parameters:
/// - path: The directory to create.
/// - createIntermediateDirectories: Whether intermediate directories should be created.
/// - permissions: The permissions to set on the new directory; default permissions will be
/// used if not specified.
@_disfavoredOverload
public func createDirectory(
at path: FilePath,
withIntermediateDirectories createIntermediateDirectories: Bool,
permissions: FilePermissions?
) async throws {
try await self.createDirectory(
at: .init(path),
withIntermediateDirectories: createIntermediateDirectories,
permissions: permissions
)
}
// MARK: - Common directories
/// Create a temporary directory at the given path, from a template.
///
/// The template for the path of the temporary directory must end in at least
/// three 'X's, which will be replaced with a unique alphanumeric combination.
/// The template can contain intermediary directories which will be created
/// if they do not exist already.
///
/// Related system calls: `mkdir(2)`.
///
/// - Parameters:
/// - template: The template for the path of the temporary directory.
/// - Returns:
/// - The path to the new temporary directory.
@_disfavoredOverload
public func createTemporaryDirectory(
template: FilePath
) async throws -> FilePath {
let result = try await self.createTemporaryDirectory(template: .init(template))
return result.underlying
}
// MARK: - File information
/// Returns information about the file at the given path, if it exists; nil otherwise.
///
/// - Parameters:
/// - path: The path to get information about.
/// - infoAboutSymbolicLink: If the file is a symbolic link and this parameter is `true` then
/// information about the link will be returned, otherwise information about the
/// destination of the symbolic link is returned.
/// - Returns: Information about the file at the given path or `nil` if no file exists.
@_disfavoredOverload
public func info(
forFileAt path: FilePath,
infoAboutSymbolicLink: Bool
) async throws -> FileInfo? {
try await self.info(forFileAt: .init(path), infoAboutSymbolicLink: infoAboutSymbolicLink)
}
// MARK: - Symbolic links
/// Creates a symbolic link that points to the destination.
///
/// If a file or directory exists at `path` then an error is thrown.
///
/// - Parameters:
/// - path: The path at which to create the symbolic link.
/// - destinationPath: The path that contains the item that the symbolic link points to.`
@_disfavoredOverload
public func createSymbolicLink(
at path: FilePath,
withDestination destinationPath: FilePath
) async throws {
try await self.createSymbolicLink(at: .init(path), withDestination: .init(destinationPath))
}
/// Returns the path of the item pointed to by a symbolic link.
///
/// - Parameter path: The path of a file or directory.
/// - Returns: The path of the file or directory to which the symbolic link points to.
@_disfavoredOverload
public func destinationOfSymbolicLink(
at path: FilePath
) async throws -> FilePath {
let result = try await self.destinationOfSymbolicLink(at: .init(path))
return result.underlying
}
// MARK: - File copying, removal, and moving
/// Copies the item at the specified path to a new location.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `sourcePath` does not exist,
/// - ``FileSystemError/Code-swift.struct/invalidArgument`` if an item at `destinationPath`
/// exists prior to the copy or its parent directory does not exist.
///
/// Note that other errors may also be thrown.
///
/// If `sourcePath` is a symbolic link then only the link is copied. The copied file will
/// preserve permissions and any extended attributes (if supported by the file system).
///
/// - Parameters:
/// - sourcePath: The path to the item to copy.
/// - destinationPath: The path at which to place the copy.
/// - copyStrategy: How to deal with concurrent aspects of the copy, only relevant to directories.
/// - shouldProceedAfterError: A closure which is executed to determine whether to continue
/// copying files if an error is encountered during the operation. See Errors section for full details.
/// - shouldCopyItem: A closure which is executed before each copy to determine whether each
/// item should be copied. See Filtering section for full details
///
/// #### Errors
///
/// No errors should be throw by implementors without first calling `shouldProceedAfterError`,
/// if that returns without throwing this is taken as permission to continue and the error is swallowed.
/// If instead the closure throws then ``copyItem(at:to:strategy:shouldProceedAfterError:shouldCopyItem:)``
/// will throw and copying will stop, though the precise semantics of this can depend on the `strategy`.
///
/// if using ``CopyStrategy/parallel(maxDescriptors:)``
/// Already started work may continue for an indefinite period of time. In particular, after throwing an error
/// it is possible that invocations of `shouldCopyItem` may continue to occur!
///
/// If using ``CopyStrategy/sequential`` only one invocation of any of the `should*` closures will occur at a time,
/// and an error will immediately stop further activity.
///
/// The specific error thrown from copyItem is undefined, it does not have to be the same error thrown from
/// `shouldProceedAfterError`.
/// In the event of any errors (ignored or otherwise) implementations are under no obbligation to
/// attempt to 'tidy up' after themselves. The state of the file system within `destinationPath`
/// after an aborted copy should is undefined.
///
/// When calling `shouldProceedAfterError` implementations of this method
/// MUST:
/// - Do so once and only once per item.
/// - Not hold any locks when doing so.
/// MAY:
/// - invoke the function multiple times concurrently (except when using ``CopyStrategy/sequential``)
///
/// #### Filtering
///
/// When invoking `shouldCopyItem` implementations of this method
/// MUST:
/// - Do so once and only once per item.
/// - Do so before attempting any operations related to the copy (including determining if they can do so).
/// - Not hold any locks when doing so.
/// - Check parent directories *before* items within them,
/// * if a parent is ignored no items within it should be considered or checked
/// - Skip all contents of a directory which is filtered out.
/// - Invoke it for the `sourcePath` itself.
/// MAY:
/// - invoke the function multiple times concurrently (except when using ``CopyStrategy/sequential``)
/// - invoke the function an arbitrary point before actually trying to copy the file
@_disfavoredOverload
public func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
strategy copyStrategy: CopyStrategy,
shouldProceedAfterError:
@escaping @Sendable (
_ source: DirectoryEntry,
_ error: Error
) async throws -> Void,
shouldCopyItem:
@escaping @Sendable (
_ source: DirectoryEntry,
_ destination: FilePath
) async -> Bool
) async throws {
try await self.copyItem(
at: .init(sourcePath),
to: .init(destinationPath),
strategy: copyStrategy,
shouldProceedAfterError: shouldProceedAfterError
) { (source: DirectoryEntry, destination: NIOFilePath) in
await shouldCopyItem(source, destination.underlying)
}
}
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
/// at the given path then this function returns zero.
///
/// If the item at the `path` is a directory and `removeItemRecursively` is `true` then the
/// contents of all of its subdirectories will be removed recursively before the directory at
/// `path`. Symbolic links are removed (but their targets are not deleted).
///
/// - Parameters:
/// - path: The path to delete.
/// - removalStrategy: Whether to delete files sequentially (one-by-one), or perform a
/// concurrent scan of the tree at `path` and delete files when they are found. Ignored if
/// the item being removed isn't a directory.
/// - removeItemRecursively: If the item being removed is a directory, remove it by
/// recursively removing its children. Setting this to `true` is synonymous with calling
/// `rm -r`, setting this false is synonymous to calling `rmdir`. Ignored if the item
/// being removed isn't a directory.
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
@_disfavoredOverload
public func removeItem(
at path: FilePath,
strategy removalStrategy: RemovalStrategy,
recursively removeItemRecursively: Bool
) async throws -> Int {
try await self.removeItem(at: .init(path), strategy: removalStrategy, recursively: removeItemRecursively)
}
/// Moves the file or directory at the specified path to a new location.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `sourcePath` does not exist,
/// - ``FileSystemError/Code-swift.struct/invalidArgument`` if an item at `destinationPath`
/// exists prior to the copy or its parent directory does not exist.
///
/// Note that other errors may also be thrown.
///
/// If the file at `sourcePath` is a symbolic link then only the link is moved to the new path.
///
/// - Parameters:
/// - sourcePath: The path to the item to move.
/// - destinationPath: The path at which to place the item.
@_disfavoredOverload
public func moveItem(at sourcePath: FilePath, to destinationPath: FilePath) async throws {
try await self.moveItem(at: .init(sourcePath), to: .init(destinationPath))
}
/// Replaces the item at `destinationPath` with the item at `existingPath`.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `existingPath` does
/// not exist,
/// - ``FileSystemError/Code-swift.struct/io`` if the file at `existingPath` was successfully
/// copied to `destinationPath` but an error occurred while removing it from `existingPath.`
///
/// Note that other errors may also be thrown.
///
/// The item at `destinationPath` is not required to exist. Note that it is possible to replace
/// a file with a directory and vice versa. After the file or directory at `destinationPath`
/// has been replaced, the item at `existingPath` will be removed.
///
/// - Parameters:
/// - destinationPath: The path of the file or directory to replace.
/// - existingPath: The path of the existing file or directory.
@_disfavoredOverload
public func replaceItem(at destinationPath: FilePath, withItemAt existingPath: FilePath) async throws {
try await self.replaceItem(at: .init(destinationPath), withItemAt: .init(existingPath))
}
}
+375 -50
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -45,7 +45,7 @@ public protocol FileSystemProtocol: Sendable {
/// - options: How the file should be opened.
/// - Returns: A readable handle to the opened file.
func openFile(
forReadingAt path: FilePath,
forReadingAt path: NIOFilePath,
options: OpenOptions.Read
) async throws -> ReadFileHandle
@@ -56,7 +56,7 @@ public protocol FileSystemProtocol: Sendable {
/// - options: How the file should be opened.
/// - Returns: A writable handle to the opened file.
func openFile(
forWritingAt path: FilePath,
forWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> WriteFileHandle
@@ -66,7 +66,7 @@ public protocol FileSystemProtocol: Sendable {
/// - path: The path of the file to open relative to the open file.
/// - options: How the file should be opened.
func openFile(
forReadingAndWritingAt path: FilePath,
forReadingAndWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> ReadWriteFileHandle
@@ -80,7 +80,7 @@ public protocol FileSystemProtocol: Sendable {
/// - options: How the directory should be opened.
/// - Returns: A handle to the opened directory.
func openDirectory(
atPath path: FilePath,
atPath path: NIOFilePath,
options: OpenOptions.Directory
) async throws -> DirectoryFileHandle
@@ -98,7 +98,7 @@ public protocol FileSystemProtocol: Sendable {
/// - permissions: The permissions to set on the new directory; default permissions will be
/// used if not specified.
func createDirectory(
at path: FilePath,
at path: NIOFilePath,
withIntermediateDirectories createIntermediateDirectories: Bool,
permissions: FilePermissions?
) async throws
@@ -106,10 +106,10 @@ public protocol FileSystemProtocol: Sendable {
// MARK: - Common directories
/// Returns the current working directory.
var currentWorkingDirectory: FilePath { get async throws }
var currentWorkingDirectory: NIOFilePath { get async throws }
/// Returns the path of the temporary directory.
var temporaryDirectory: FilePath { get async throws }
var temporaryDirectory: NIOFilePath { get async throws }
/// Create a temporary directory at the given path, from a template.
///
@@ -125,8 +125,8 @@ public protocol FileSystemProtocol: Sendable {
/// - Returns:
/// - The path to the new temporary directory.
func createTemporaryDirectory(
template: FilePath
) async throws -> FilePath
template: NIOFilePath
) async throws -> NIOFilePath
// MARK: - File information
@@ -139,7 +139,7 @@ public protocol FileSystemProtocol: Sendable {
/// destination of the symbolic link is returned.
/// - Returns: Information about the file at the given path or `nil` if no file exists.
func info(
forFileAt path: FilePath,
forFileAt path: NIOFilePath,
infoAboutSymbolicLink: Bool
) async throws -> FileInfo?
@@ -153,8 +153,8 @@ public protocol FileSystemProtocol: Sendable {
/// - path: The path at which to create the symbolic link.
/// - destinationPath: The path that contains the item that the symbolic link points to.`
func createSymbolicLink(
at path: FilePath,
withDestination destinationPath: FilePath
at path: NIOFilePath,
withDestination destinationPath: NIOFilePath
) async throws
/// Returns the path of the item pointed to by a symbolic link.
@@ -162,8 +162,8 @@ public protocol FileSystemProtocol: Sendable {
/// - Parameter path: The path of a file or directory.
/// - Returns: The path of the file or directory to which the symbolic link points to.
func destinationOfSymbolicLink(
at path: FilePath
) async throws -> FilePath
at path: NIOFilePath
) async throws -> NIOFilePath
// MARK: - File copying, removal, and moving
@@ -230,8 +230,8 @@ public protocol FileSystemProtocol: Sendable {
/// - invoke the function multiple times concurrently (except when using ``CopyStrategy/sequential``)
/// - invoke the function an arbitrary point before actually trying to copy the file
func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
at sourcePath: NIOFilePath,
to destinationPath: NIOFilePath,
strategy copyStrategy: CopyStrategy,
shouldProceedAfterError: @escaping @Sendable (
_ source: DirectoryEntry,
@@ -239,7 +239,7 @@ public protocol FileSystemProtocol: Sendable {
) async throws -> Void,
shouldCopyItem: @escaping @Sendable (
_ source: DirectoryEntry,
_ destination: FilePath
_ destination: NIOFilePath
) async -> Bool
) async throws
@@ -264,7 +264,7 @@ public protocol FileSystemProtocol: Sendable {
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
func removeItem(
at path: FilePath,
at path: NIOFilePath,
strategy removalStrategy: RemovalStrategy,
recursively removeItemRecursively: Bool
) async throws -> Int
@@ -283,7 +283,7 @@ public protocol FileSystemProtocol: Sendable {
/// - Parameters:
/// - sourcePath: The path to the item to move.
/// - destinationPath: The path at which to place the item.
func moveItem(at sourcePath: FilePath, to destinationPath: FilePath) async throws
func moveItem(at sourcePath: NIOFilePath, to destinationPath: NIOFilePath) async throws
/// Replaces the item at `destinationPath` with the item at `existingPath`.
///
@@ -302,7 +302,7 @@ public protocol FileSystemProtocol: Sendable {
/// - Parameters:
/// - destinationPath: The path of the file or directory to replace.
/// - existingPath: The path of the existing file or directory.
func replaceItem(at destinationPath: FilePath, withItemAt existingPath: FilePath) async throws
func replaceItem(at destinationPath: NIOFilePath, withItemAt existingPath: NIOFilePath) async throws
}
// MARK: - Open existing files/directories
@@ -313,8 +313,8 @@ extension FileSystemProtocol {
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in read-write or write-only
/// mode by calling ``withFileHandle(forReadingAndWritingAt:options:execute:)`` and
/// ``withFileHandle(forWritingAt:options:execute:)``.
/// mode by calling ``FileSystemProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)-9nqu3`` and
/// ``FileSystemProtocol/withFileHandle(forWritingAt:options:execute:)-1p6ka``.
///
/// - Parameters:
/// - path: The path of the file to open for reading.
@@ -324,7 +324,7 @@ extension FileSystemProtocol {
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withFileHandle<Result>(
forReadingAt path: FilePath,
forReadingAt path: NIOFilePath,
options: OpenOptions.Read = OpenOptions.Read(),
execute: (_ read: ReadFileHandle) async throws -> Result
) async throws -> Result {
@@ -336,12 +336,35 @@ extension FileSystemProtocol {
}
}
/// Opens the file at the given path and provides scoped read-only access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in read-write or write-only
/// mode by calling ``FileSystemProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)-3822z`` and
/// ``FileSystemProtocol/withFileHandle(forWritingAt:options:execute:)-41x6r``.
///
/// - Parameters:
/// - path: The path of the file to open for reading.
/// - options: How the file should be opened.
/// - execute: A closure which provides read-only access to the open file. The file is closed
/// automatically after the closure exits.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
@_disfavoredOverload
public func withFileHandle<Result>(
forReadingAt path: FilePath,
options: OpenOptions.Read = OpenOptions.Read(),
execute: (_ read: ReadFileHandle) async throws -> Result
) async throws -> Result {
try await self.withFileHandle(forReadingAt: .init(path), options: options, execute: execute)
}
/// Opens the file at the given path and provides scoped write-only access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in read-write or read-only
/// mode by calling ``withFileHandle(forReadingAndWritingAt:options:execute:)`` and
/// ``withFileHandle(forReadingAt:options:execute:)``.
/// mode by calling ``FileSystemProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)-9nqu3`` and
/// ``FileSystemProtocol/withFileHandle(forReadingAt:options:execute:)-nsue``.
///
/// - Parameters:
/// - path: The path of the file to open for reading.
@@ -351,7 +374,7 @@ extension FileSystemProtocol {
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withFileHandle<Result>(
forWritingAt path: FilePath,
forWritingAt path: NIOFilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute: (_ write: WriteFileHandle) async throws -> Result
) async throws -> Result {
@@ -368,12 +391,35 @@ extension FileSystemProtocol {
}
}
/// Opens the file at the given path and provides scoped write-only access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns. Files may also be opened in read-write or read-only
/// mode by calling ``FileSystemProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)-3822z`` and
/// ``FileSystemProtocol/withFileHandle(forReadingAt:options:execute:)-20x3l``.
///
/// - Parameters:
/// - path: The path of the file to open for reading.
/// - options: How the file should be opened.
/// - execute: A closure which provides write-only access to the open file. The file is closed
/// automatically after the closure exits.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
@_disfavoredOverload
public func withFileHandle<Result>(
forWritingAt path: FilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute: (_ write: WriteFileHandle) async throws -> Result
) async throws -> Result {
try await self.withFileHandle(forWritingAt: .init(path), options: options, execute: execute)
}
/// Opens the file at the given path and provides scoped read-write access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the function returns. Files may also be opened in read-only or
/// write-only mode by with ``withFileHandle(forReadingAt:options:execute:)`` and
/// ``withFileHandle(forWritingAt:options:execute:)``.
/// write-only mode by with ``FileSystemProtocol/withFileHandle(forReadingAt:options:execute:)-nsue`` and
/// ``FileSystemProtocol/withFileHandle(forWritingAt:options:execute:)-1p6ka``.
///
/// - Parameters:
/// - path: The path of the file to open for reading and writing.
@@ -383,7 +429,7 @@ extension FileSystemProtocol {
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withFileHandle<Result>(
forReadingAndWritingAt path: FilePath,
forReadingAndWritingAt path: NIOFilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute: (_ readWrite: ReadWriteFileHandle) async throws -> Result
) async throws -> Result {
@@ -395,6 +441,50 @@ extension FileSystemProtocol {
}
}
/// Opens the file at the given path and provides scoped read-write access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the function returns. Files may also be opened in read-only or
/// write-only mode by with ``FileSystemProtocol/withFileHandle(forReadingAt:options:execute:)-20x3l`` and
/// ``FileSystemProtocol/withFileHandle(forWritingAt:options:execute:)-41x6r``.
///
/// - Parameters:
/// - path: The path of the file to open for reading and writing.
/// - options: How the file should be opened.
/// - execute: A closure which provides access to the open file. The file is closed
/// automatically after the closure exits.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
@_disfavoredOverload
public func withFileHandle<Result>(
forReadingAndWritingAt path: FilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute: (_ readWrite: ReadWriteFileHandle) async throws -> Result
) async throws -> Result {
try await self.withFileHandle(forReadingAndWritingAt: .init(path), options: options, execute: execute)
}
/// Opens the directory at the given path and provides scoped access to it.
///
/// - Parameters:
/// - path: The path of the directory to open.
/// - options: How the file should be opened.
/// - execute: A closure which provides access to the directory.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withDirectoryHandle<Result>(
atPath path: NIOFilePath,
options: OpenOptions.Directory = OpenOptions.Directory(),
execute: (_ directory: DirectoryFileHandle) async throws -> Result
) async throws -> Result {
let handle = try await self.openDirectory(atPath: path, options: options)
return try await withUncancellableTearDown {
try await execute(handle)
} tearDown: { _ in
try await handle.close()
}
}
/// Opens the directory at the given path and provides scoped access to it.
///
/// - Parameters:
@@ -403,17 +493,13 @@ extension FileSystemProtocol {
/// - execute: A closure which provides access to the directory.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
@_disfavoredOverload
public func withDirectoryHandle<Result>(
atPath path: FilePath,
options: OpenOptions.Directory = OpenOptions.Directory(),
execute: (_ directory: DirectoryFileHandle) async throws -> Result
) async throws -> Result {
let handle = try await self.openDirectory(atPath: path, options: options)
return try await withUncancellableTearDown {
try await execute(handle)
} tearDown: { _ in
try await handle.close()
}
try await self.withDirectoryHandle(atPath: .init(path), options: options, execute: execute)
}
}
@@ -428,11 +514,26 @@ extension FileSystemProtocol {
/// - path: The path of the file to open.
/// - Returns: A readable handle to the opened file.
public func openFile(
forReadingAt path: FilePath
forReadingAt path: NIOFilePath
) async throws -> ReadFileHandle {
try await self.openFile(forReadingAt: path, options: OpenOptions.Read())
}
/// Opens the file at `path` for reading and returns a handle to it.
///
/// The file being opened must exist otherwise this function will throw a ``FileSystemError``
/// with code ``FileSystemError/Code-swift.struct/notFound``.
///
/// - Parameters:
/// - path: The path of the file to open.
/// - Returns: A readable handle to the opened file.
@_disfavoredOverload
public func openFile(
forReadingAt path: FilePath
) async throws -> ReadFileHandle {
try await self.openFile(forReadingAt: .init(path))
}
/// Opens the directory at `path` and returns a handle to it.
///
/// The directory being opened must already exist otherwise this function will throw an error.
@@ -442,11 +543,26 @@ extension FileSystemProtocol {
/// - path: The path of the directory to open.
/// - Returns: A handle to the opened directory.
public func openDirectory(
atPath path: FilePath
atPath path: NIOFilePath
) async throws -> DirectoryFileHandle {
try await self.openDirectory(atPath: path, options: OpenOptions.Directory())
}
/// Opens the directory at `path` and returns a handle to it.
///
/// The directory being opened must already exist otherwise this function will throw an error.
/// Use ``createDirectory(at:withIntermediateDirectories:permissions:)`` to create directories.
///
/// - Parameters:
/// - path: The path of the directory to open.
/// - Returns: A handle to the opened directory.
@_disfavoredOverload
public func openDirectory(
atPath path: FilePath
) async throws -> DirectoryFileHandle {
try await self.openDirectory(atPath: .init(path))
}
/// Returns information about the file at the given path, if it exists; nil otherwise.
///
/// Calls ``info(forFileAt:infoAboutSymbolicLink:)`` setting `infoAboutSymbolicLink` to `false`.
@@ -454,10 +570,22 @@ extension FileSystemProtocol {
/// - Parameters:
/// - path: The path to get information about.
/// - Returns: Information about the file at the given path or `nil` if no file exists.
public func info(forFileAt path: FilePath) async throws -> FileInfo? {
public func info(forFileAt path: NIOFilePath) async throws -> FileInfo? {
try await self.info(forFileAt: path, infoAboutSymbolicLink: false)
}
/// Returns information about the file at the given path, if it exists; nil otherwise.
///
/// Calls ``info(forFileAt:infoAboutSymbolicLink:)`` setting `infoAboutSymbolicLink` to `false`.
///
/// - Parameters:
/// - path: The path to get information about.
/// - Returns: Information about the file at the given path or `nil` if no file exists.
@_disfavoredOverload
public func info(forFileAt path: FilePath) async throws -> FileInfo? {
try await self.info(forFileAt: .init(path))
}
/// Copies the item at the specified path to a new location.
///
/// The following error codes may be thrown:
@@ -476,8 +604,8 @@ extension FileSystemProtocol {
/// - destinationPath: The path at which to place the copy.
/// - copyStrategy: This controls the concurrency used if the file at `sourcePath` is a directory.
public func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
at sourcePath: NIOFilePath,
to destinationPath: NIOFilePath,
strategy copyStrategy: CopyStrategy = .platformDefault
) async throws {
try await self.copyItem(at: sourcePath, to: destinationPath, strategy: copyStrategy) { path, error in
@@ -487,6 +615,32 @@ extension FileSystemProtocol {
}
}
/// Copies the item at the specified path to a new location.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `sourcePath` does not exist,
/// - ``FileSystemError/Code-swift.struct/invalidArgument`` if an item at `destinationPath`
/// exists prior to the copy or its parent directory does not exist.
///
/// Note that other errors may also be thrown. If any error is encountered during the copy
/// then the copy is aborted. You can modify the behaviour with the `shouldProceedAfterError`
/// parameter of ``FileSystemProtocol/copyItem(at:to:strategy:shouldProceedAfterError:shouldCopyItem:)``.
///
/// If the file at `sourcePath` is a symbolic link then only the link is copied to the new path.
///
/// - Parameters:
/// - sourcePath: The path to the item to copy.
/// - destinationPath: The path at which to place the copy.
/// - copyStrategy: This controls the concurrency used if the file at `sourcePath` is a directory.
@_disfavoredOverload
public func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
strategy copyStrategy: CopyStrategy = .platformDefault
) async throws {
try await self.copyItem(at: .init(sourcePath), to: .init(destinationPath), strategy: copyStrategy)
}
/// Copies the item at the specified path to a new location.
///
/// The item to be copied must be a:
@@ -566,6 +720,54 @@ extension FileSystemProtocol {
/// in the event of copying a directory.
/// See the detailed description on ``copyItem(at:to:strategy:shouldProceedAfterError:shouldCopyItem:)``
/// for the implications of this with respect to the `shouldProceedAfterError` and `shouldCopyItem` callbacks
public func copyItem(
at sourcePath: NIOFilePath,
to destinationPath: NIOFilePath,
shouldProceedAfterError: @escaping @Sendable (
_ source: DirectoryEntry,
_ error: Error
) async throws -> Void,
shouldCopyItem: @escaping @Sendable (
_ source: DirectoryEntry,
_ destination: NIOFilePath
) async -> Bool
) async throws {
try await self.copyItem(
at: sourcePath,
to: destinationPath,
strategy: .platformDefault,
shouldProceedAfterError: shouldProceedAfterError,
shouldCopyItem: shouldCopyItem
)
}
/// Copies the item at the specified path to a new location.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `sourcePath` does not exist,
/// - ``FileSystemError/Code-swift.struct/invalidArgument`` if an item at `destinationPath`
/// exists prior to the copy or its parent directory does not exist.
///
/// Note that other errors may also be thrown.
///
/// If `sourcePath` is a symbolic link then only the link is copied. The copied file will
/// preserve permissions and any extended attributes (if supported by the file system).
///
/// - Parameters:
/// - sourcePath: The path to the item to copy.
/// - destinationPath: The path at which to place the copy.
/// - shouldProceedAfterError: A closure which is executed to determine whether to continue
/// copying files if an error is encountered during the operation. See Errors section for full details.
/// - shouldCopyItem: A closure which is executed before each copy to determine whether each
/// item should be copied. See Filtering section for full details
///
/// #### Parallelism
///
/// This overload uses ``CopyStrategy/platformDefault`` which is likely to result in multiple concurrency domains being used
/// in the event of copying a directory.
/// See the detailed description on ``copyItem(at:to:strategy:shouldProceedAfterError:shouldCopyItem:)``
/// for the implications of this with respect to the `shouldProceedAfterError` and `shouldCopyItem` callbacks
@_disfavoredOverload
public func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
@@ -604,11 +806,34 @@ extension FileSystemProtocol {
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
public func removeItem(
at path: FilePath
at path: NIOFilePath
) async throws -> Int {
try await self.removeItem(at: path, strategy: .platformDefault, recursively: true)
}
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
/// at the given path then this function returns zero.
///
/// If the item at the `path` is a directory then the contents of all of its subdirectories will
/// be removed recursively before the directory at `path`. Symbolic links are removed (but their
/// targets are not deleted).
///
/// The strategy for deletion will be determined automatically depending on the discovered
/// platform.
///
/// - Parameters:
/// - path: The path to delete.
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@_disfavoredOverload
@discardableResult
public func removeItem(
at path: FilePath
) async throws -> Int {
try await self.removeItem(at: .init(path))
}
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
@@ -630,12 +855,40 @@ extension FileSystemProtocol {
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
public func removeItem(
at path: FilePath,
at path: NIOFilePath,
recursively removeItemRecursively: Bool
) async throws -> Int {
try await self.removeItem(at: path, strategy: .platformDefault, recursively: removeItemRecursively)
}
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
/// at the given path then this function returns zero.
///
/// If the item at the `path` is a directory then the contents of all of its subdirectories will
/// be removed recursively before the directory at `path`. Symbolic links are removed (but their
/// targets are not deleted).
///
/// The strategy for deletion will be determined automatically depending on the discovered
/// platform.
///
/// - Parameters:
/// - path: The path to delete.
/// - removeItemRecursively: If the item being removed is a directory, remove it by
/// recursively removing its children. Setting this to `true` is synonymous with calling
/// `rm -r`, setting this false is synonymous to calling `rmdir`. Ignored if the item
/// being removed isn't a directory.
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@_disfavoredOverload
@discardableResult
public func removeItem(
at path: FilePath,
recursively removeItemRecursively: Bool
) async throws -> Int {
try await self.removeItem(at: .init(path), recursively: removeItemRecursively)
}
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
@@ -652,12 +905,35 @@ extension FileSystemProtocol {
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
public func removeItem(
at path: FilePath,
at path: NIOFilePath,
strategy removalStrategy: RemovalStrategy
) async throws -> Int {
try await self.removeItem(at: path, strategy: removalStrategy, recursively: true)
}
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
/// at the given path then this function returns zero.
///
/// If the item at the `path` is a directory then the contents of all of its subdirectories will
/// be removed recursively before the directory at `path`. Symbolic links are removed (but their
/// targets are not deleted).
///
/// - Parameters:
/// - path: The path to delete.
/// - removalStrategy: Whether to delete files sequentially (one-by-one), or perform a
/// concurrent scan of the tree at `path` and delete files when they are found.
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@_disfavoredOverload
@discardableResult
public func removeItem(
at path: FilePath,
strategy removalStrategy: RemovalStrategy
) async throws -> Int {
try await self.removeItem(at: .init(path), strategy: removalStrategy)
}
/// Create a directory at the given path.
///
/// If a directory (or file) already exists at `path` then an error will be thrown. If
@@ -673,7 +949,7 @@ extension FileSystemProtocol {
/// - path: The directory to create.
/// - createIntermediateDirectories: Whether intermediate directories should be created.
public func createDirectory(
at path: FilePath,
at path: NIOFilePath,
withIntermediateDirectories createIntermediateDirectories: Bool
) async throws {
try await self.createDirectory(
@@ -683,6 +959,28 @@ extension FileSystemProtocol {
)
}
/// Create a directory at the given path.
///
/// If a directory (or file) already exists at `path` then an error will be thrown. If
/// `createIntermediateDirectories` is `false` then the full prefix of `path` must already
/// exist. If set to `true` then all intermediate directories will be created.
///
/// New directories will be given read-write-execute owner permissions and read-execute group
/// and other permissions.
///
/// Related system calls: `mkdir(2)`.
///
/// - Parameters:
/// - path: The directory to create.
/// - createIntermediateDirectories: Whether intermediate directories should be created.
@_disfavoredOverload
public func createDirectory(
at path: FilePath,
withIntermediateDirectories createIntermediateDirectories: Bool
) async throws {
try await self.createDirectory(at: .init(path), withIntermediateDirectories: createIntermediateDirectories)
}
/// Create a temporary directory and removes it once the function returns.
///
/// You can use `prefix` to specify the directory in which the temporary directory should
@@ -697,19 +995,19 @@ extension FileSystemProtocol {
/// - execute: A closure which provides access to the directory and its path.
/// - Returns: The result of `execute`.
public func withTemporaryDirectory<Result>(
prefix: FilePath? = nil,
prefix: NIOFilePath? = nil,
options: OpenOptions.Directory = OpenOptions.Directory(),
execute: (_ directory: DirectoryFileHandle, _ path: FilePath) async throws -> Result
execute: (_ directory: DirectoryFileHandle, _ path: NIOFilePath) async throws -> Result
) async throws -> Result {
let template: FilePath
if let prefix = prefix {
template = prefix.appending("XXXXXXXX")
template = prefix.underlying.appending("XXXXXXXX")
} else {
template = try await self.temporaryDirectory.appending("XXXXXXXX")
template = try await self.temporaryDirectory.underlying.appending("XXXXXXXX")
}
let directory = try await self.createTemporaryDirectory(template: template)
let directory = NIOFilePath(try await self.createTemporaryDirectory(template: template))
return try await withUncancellableTearDown {
try await withDirectoryHandle(atPath: directory, options: options) { handle in
try await execute(handle, directory)
@@ -718,4 +1016,31 @@ extension FileSystemProtocol {
try await self.removeItem(at: directory, strategy: .platformDefault, recursively: true)
}
}
/// Create a temporary directory and removes it once the function returns.
///
/// You can use `prefix` to specify the directory in which the temporary directory should
/// be created. If `prefix` is `nil` then the value of ``temporaryDirectory`` is used as
/// the prefix.
///
/// The temporary directory, and all of its contents, is removed once `execute` returns.
///
/// - Parameters:
/// - prefix: The prefix to use for the path of the temporary directory.
/// - options: Options used to create the directory.
/// - execute: A closure which provides access to the directory and its path.
/// - Returns: The result of `execute`.
@_disfavoredOverload
public func withTemporaryDirectory<Result>(
prefix: FilePath? = nil,
options: OpenOptions.Directory = OpenOptions.Directory(),
execute: (_ directory: DirectoryFileHandle, _ path: FilePath) async throws -> Result
) async throws -> Result {
try await self.withTemporaryDirectory(
prefix: prefix.map { NIOFilePath($0) },
options: options
) { directory, path in
try await execute(directory, .init(path))
}
}
}
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -1178,13 +1178,13 @@ extension SystemFileHandle: DirectoryFileHandleProtocol {
}
public func openFile(
forReadingAt path: FilePath,
forReadingAt path: NIOFilePath,
options: OpenOptions.Read
) async throws -> SystemFileHandle {
let opts = options.descriptorOptions.union(.nonBlocking)
let handle = try await self.threadPool.runIfActive { [sendableView] in
let handle = try sendableView._open(
atPath: path,
atPath: path.underlying,
mode: .readOnly,
options: opts,
transactionalIfPossible: false
@@ -1196,14 +1196,14 @@ extension SystemFileHandle: DirectoryFileHandleProtocol {
}
public func openFile(
forReadingAndWritingAt path: FilePath,
forReadingAndWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> SystemFileHandle {
let perms = options.permissionsForRegularFile
let opts = options.descriptorOptions.union(.nonBlocking)
let handle = try await self.threadPool.runIfActive { [sendableView] in
let handle = try sendableView._open(
atPath: path,
atPath: path.underlying,
mode: .readWrite,
options: opts,
permissions: perms,
@@ -1216,14 +1216,14 @@ extension SystemFileHandle: DirectoryFileHandleProtocol {
}
public func openFile(
forWritingAt path: FilePath,
forWritingAt path: NIOFilePath,
options: OpenOptions.Write
) async throws -> SystemFileHandle {
let perms = options.permissionsForRegularFile
let opts = options.descriptorOptions.union(.nonBlocking)
let handle = try await self.threadPool.runIfActive { [sendableView] in
let handle = try sendableView._open(
atPath: path,
atPath: path.underlying,
mode: .writeOnly,
options: opts,
permissions: perms,
@@ -1236,13 +1236,13 @@ extension SystemFileHandle: DirectoryFileHandleProtocol {
}
public func openDirectory(
atPath path: FilePath,
atPath path: NIOFilePath,
options: OpenOptions.Directory
) async throws -> SystemFileHandle {
let opts = options.descriptorOptions.union(.nonBlocking)
let handle = try await self.threadPool.runIfActive { [sendableView] in
let handle = try sendableView._open(
atPath: path,
atPath: path.underlying,
mode: .readOnly,
options: opts,
transactionalIfPossible: false
+3 -3
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -177,8 +177,8 @@ extension OpenOptions {
///
/// When this option is set and applied the newly created file will only materialize
/// on the file system when the file is closed. When used in conjunction with
/// ``FileSystemProtocol/withFileHandle(forWritingAt:options:execute:)`` and
/// ``FileSystemProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)`` the
/// ``FileSystemProtocol/withFileHandle(forWritingAt:options:execute:)-1p6ka`` and
/// ``FileSystemProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)-9nqu3`` the
/// file will only materialize when the file is closed and no errors have been thrown.
///
/// - Important: This flag is only applied if ``OpenOptions/Write/existingFile`` is
+34 -2
View File
@@ -25,7 +25,7 @@ extension String {
///
/// - Throws: If the file is larger than `maximumSizeAllowed`, an ``FileSystemError/Code-swift.struct/resourceExhausted`` error will be thrown.
public init(
contentsOf path: FilePath,
contentsOf path: NIOFilePath,
maximumSizeAllowed: ByteCount,
fileSystem: some FileSystemProtocol
) async throws {
@@ -36,6 +36,23 @@ extension String {
self = Self(buffer: byteBuffer)
}
/// Reads the contents of the file at the path into a String.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, in bytes, as a ``ByteCount``. If the file is larger than this, an error is thrown.
/// - fileSystem: The ``FileSystemProtocol`` instance to use to read the file.
///
/// - Throws: If the file is larger than `maximumSizeAllowed`, an ``FileSystemError/Code-swift.struct/resourceExhausted`` error will be thrown.
@_disfavoredOverload
public init(
contentsOf path: FilePath,
maximumSizeAllowed: ByteCount,
fileSystem: some FileSystemProtocol
) async throws {
try await self.init(contentsOf: .init(path), maximumSizeAllowed: maximumSizeAllowed, fileSystem: fileSystem)
}
/// Reads the contents of the file at the path using ``FileSystem``.
///
/// - Parameters:
@@ -44,7 +61,7 @@ extension String {
///
/// - Throws: If the file is larger than `maximumSizeAllowed`, an ``FileSystemError/Code-swift.struct/resourceExhausted`` error will be thrown.
public init(
contentsOf path: FilePath,
contentsOf path: NIOFilePath,
maximumSizeAllowed: ByteCount
) async throws {
self = try await Self(
@@ -53,4 +70,19 @@ extension String {
fileSystem: .shared
)
}
/// Reads the contents of the file at the path using ``FileSystem``.
///
/// - Parameters:
/// - path: The path of the file to read.
/// - maximumSizeAllowed: The maximum size of file which can be read, in bytes, as a ``ByteCount``. If the file is larger than this, an error is thrown.
///
/// - Throws: If the file is larger than `maximumSizeAllowed`, an ``FileSystemError/Code-swift.struct/resourceExhausted`` error will be thrown.
@_disfavoredOverload
public init(
contentsOf path: FilePath,
maximumSizeAllowed: ByteCount
) async throws {
try await self.init(contentsOf: .init(path), maximumSizeAllowed: maximumSizeAllowed)
}
}