mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
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:
1 parent
b881f6ec60
commit
8997a24524
20 files changed
+1387
-191
No files matched your search
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user