Files
swift-nio/Sources/NIOFileSystem/DirectoryEntry.swift
T
aryan-25andGeorge Barnett 8997a24524 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>
2025-08-11 17:29:02 +01:00

65 lines
2.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
import SystemPackage
/// Information about an item within a directory.
public struct DirectoryEntry: Sendable, Hashable, Equatable {
/// The path of the directory entry.
///
/// - Precondition: The path must have at least one component.
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``.
///
/// If `path` is "/Users/tim/path-to-4T.key" then `name` will be "path-to-4T.key".
public var name: FilePath.Component {
self.path.lastComponent!
}
/// The type of entry.
public var type: FileType
/// 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.
public init?(path: NIOFilePath, type: FileType) {
if path.underlying.components.isEmpty {
return nil
}
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)
}
}