Protocolswift-nio 2.72.0_NIOFileSystem
FileSystemProtocol
The interface for interacting with a file system.
FileSystemProtocol.swift:20FileSystemProtocol.md- iOS
- 13.0+
- macOS
- 10.15+
- tvOS
- 13.0+
- watchOS
- 6.0+
protocol FileSystemProtocol : Sendable
Opening files with managed lifecycles
Files and directories can be opened and have their lifecycles managed by using the following methods:
func withFileHandle<Result>(forReadingAt: FilePath, options: OpenOptions.Read, execute: (_ read: ReadFileHandle) async throws -> Result
) async throws -> Result Opens the file at the given path and provides scoped read-only access to it.
func withFileHandle<Result>(forWritingAt: FilePath, options: OpenOptions.Write, execute: (_ write: WriteFileHandle) async throws -> Result
) async throws -> Result Opens the file at the given path and provides scoped write-only access to it.
func withFileHandle<Result>(forReadingAndWritingAt: FilePath, options: OpenOptions.Write, execute: (_ readWrite: ReadWriteFileHandle) async throws -> Result
) async throws -> Result Opens the file at the given path and provides scoped read-write access to it.
func withDirectoryHandle<Result>(atPath: FilePath, options: OpenOptions.Directory, execute: (_ directory: DirectoryFileHandle) async throws -> Result
) async throws -> Result Opens the directory at the given path and provides scoped access to it.
Opening files
Files and directories can be opened using the following methods. The caller is responsible for closing it to avoid leaking resources.
func openFile(forReadingAt: FilePath
) async throws -> ReadFileHandle Opens the file at
path
for reading and returns a handle to it.func openFile(forWritingAt: FilePath, options: OpenOptions.Write
) async throws -> WriteFileHandle Opens the file at
path
for writing and returns a handle to it.func openFile(forReadingAndWritingAt: FilePath, options: OpenOptions.Write
) async throws -> ReadWriteFileHandle Opens the file at
path
for reading and writing and returns a handle to it.func openDirectory(atPath: FilePath, options: OpenOptions.Directory
) async throws -> DirectoryFileHandle Opens the directory at
path
and returns a handle to it.
File information
func info(forFileAt: FilePath, infoAboutSymbolicLink: Bool
) async throws -> FileInfo? Returns information about the file at the given path, if it exists; nil otherwise.
Symbolic links
func createSymbolicLink(at: FilePath, withDestination: FilePath
) async throws Creates a symbolic link that points to the destination.
func destinationOfSymbolicLink(at: FilePath
) async throws -> FilePath Returns the path of the item pointed to by a symbolic link.
Managing files
func copyItem(at: FilePath, to: FilePath, shouldProceedAfterError: @escaping (_ entry: DirectoryEntry, _ error: Error) async throws -> Void, shouldCopyFile: @escaping (_ source: FilePath, _ destination: FilePath) async -> Bool
) async throws Copies the item at the specified path to a new location.
func removeItem(at: FilePath
) async throws -> Int Deletes the file or directory (and its contents) at
path
.func moveItem(at: FilePath, to: FilePath
) async throws Moves the file or directory at the specified path to a new location.
func replaceItem(at: FilePath, withItemAt: FilePath
) async throws Replaces the item at
destinationPath
with the item atexistingPath
.func createDirectory(at: FilePath, withIntermediateDirectories: Bool, permissions: FilePermissions?
) async throws Create a directory at the given path.
System directories
var currentWorkingDirectory: FilePath
Returns the current working directory.
var temporaryDirectory: FilePath
Returns the path of the temporary directory.
func withTemporaryDirectory<Result>(prefix: FilePath?, options: OpenOptions.Directory, execute: (_ directory: DirectoryFileHandle, _ path: FilePath) async throws -> Result
) async throws -> Result Create a temporary directory and removes it once the function returns.
Supertypes
protocol Sendable
A type whose values can safely be passed across concurrency domains by copying.
Requirements
Type members
associatedtype DirectoryFileHandle
The type of
DirectoryFileHandleProtocol
to return when opening directories.associatedtype ReadFileHandle
The type of
ReadableFileHandleProtocol
to return when opening files for reading.associatedtype ReadWriteFileHandle
The type of
ReadableAndWritableFileHandleProtocol
to return when opening files for reading and writing.associatedtype WriteFileHandle
The type of
WritableFileHandleProtocol
to return when opening files for writing.
Instance members
func copyItem(at: FilePath, to: FilePath, strategy: CopyStrategy, shouldProceedAfterError: @escaping (_ source: DirectoryEntry, _ error: Error) async throws -> Void, shouldCopyItem: @escaping (_ source: DirectoryEntry, _ destination: FilePath) async -> Bool
) async throws Copies the item at the specified path to a new location.
func createTemporaryDirectory(template: FilePath
) async throws -> FilePath Create a temporary directory at the given path, from a template.
func openFile(forReadingAt: FilePath, options: OpenOptions.Read
) async throws -> ReadFileHandle Opens the file at
path
for reading and returns a handle to it.func removeItem(at: FilePath, recursively: Bool
) async throws -> Int Deletes the file or directory (and its contents) at
path
.
See also
protocol FileHandleProtocol
A handle for a file system object.
protocol ReadableFileHandleProtocol
A handle for reading data from an open file.
protocol WritableFileHandleProtocol
A file handle suitable for writing.
typealias ReadableAndWritableFileHandleProtocol
A file handle which is suitable for reading and writing.
protocol DirectoryFileHandleProtocol
A handle suitable for directories.
Citizens in _NIOFileSystem
Instance members
func copyItem(at: FilePath, to: FilePath, shouldProceedAfterError: @escaping (_ source: DirectoryEntry, _ error: Error) async throws -> Void, shouldCopyItem: @escaping (_ source: DirectoryEntry, _ destination: FilePath) async -> Bool
) async throws Copies the item at the specified path to a new location.
func copyItem(at: FilePath, to: FilePath, strategy: CopyStrategy
) async throws Copies the item at the specified path to a new location.
func createDirectory(at: FilePath, withIntermediateDirectories: Bool
) async throws Create a directory at the given path.
func info(forFileAt: FilePath
) async throws -> FileInfo? Returns information about the file at the given path, if it exists; nil otherwise.
func openDirectory(atPath: FilePath
) async throws -> DirectoryFileHandle Opens the directory at
path
and returns a handle to it.
Citizens in _NIOFileSystem
where Self == FileSystem