public final class FileHandle : FileDescriptor
FileHandle is a handle to an open file.
When creating a
FileHandle it takes ownership of the underlying file descriptor. When a
FileHandle is no longer
needed you must
close it or take back ownership of the file descriptor using
One underlying file descriptor should usually be managed by one
Failing to manage the lifetime of a
FileHandle correctly will result in undefined behaviour.
FileHandle objects are not thread-safe and are mutable. They also cannot be fully thread-safe as they refer to a global underlying file descriptor.
public private(set) var isOpen: Bool
FileHandletaking ownership of
descriptor. You must call
FileHandle.takeDescriptorOwnershipbefore this object can be safely released.
public init(descriptor: CInt)
FileHandle. This means that a new
FileHandleobject with a new underlying file descriptor is returned. The caller takes ownership of the returned
FileHandleand is responsible for closing it.
FileHandleis not fully independent, the seek pointer is shared as documented by
public func duplicate() throws -> FileHandle
FileHandlewith a fresh underlying file descriptor but shared seek pointer.
Take the ownership of the underlying file descriptor. This is similar to
close()but the underlying file descriptor remains open. The caller is responsible for closing the file descriptor by some other means.
After calling this, the
FileHandlecannot be used for anything else and all the operations will throw.
public func takeDescriptorOwnership() throws -> CInt
The underlying file descriptor, now owned by the caller.
public func close() throws
public func withUnsafeFileDescriptor<T>(_ body: (CInt) throws -> T) throws -> T
Open a new
public convenience init(path: String) throws
the path of the file to open. The ownership of the file descriptor is transferred to this
FileHandleand so it will be closed once