Repository: apple/swift-nio
File: Sources/NIOFileSystem/Internal/SystemFileHandle.swift (Swift)

Code before:
    }

    public func withUnsafeDescriptor<R: Sendable>(
        _ execute: @Sendable @escaping (FileDescriptor) throws -> R
    ) async throws -> R {
        try await self.executor.execute { [sendableView] in
            try sendableView._withUnsafeDescriptor {
                return try execute($0)
            } onUnavailable: {
                FileSystemError(
                    code: .closed,
                    message: "File is closed ('\(sendableView.path)').",
                    cause: nil,
                    location: .here()
                )
            }
        }
    }

    public func detachUnsafeFileDescriptor() throws -> FileDescriptor {
        return try self.sendableView.lifecycle.withLockedValue { lifecycle in
            switch lifecycle {
            case let .open(descriptor):
                lifecycle = .detached

                // We need to be careful handling files which have delayed materialiszation to avoid
                // leftover temporary files.
                //
                // Where we use the 'link' mode we simply call materialize and return the
                // descriptor as it will be materialized when closed.
                //
                // For the 'rename' mode we create a hard link to the desired file and unlink the
                // created file.
                guard let materialization = self.sendableView.materialization else {
                    // File opened 'normally', just detach and return.
                    return descriptor
                }

                switch materialization.mode {
                #if canImport(Glibc) || canImport(Musl)
                case .link:
                    let result = self.sendableView._materializeLink(
                        descriptor: descriptor,
                        from: materialization.created,
                        to: materialization.desired,
                        exclusive: materialization.exclusive
                    )
                    return try result.map { descriptor }.get()
                #endif

                case .rename:

Review comment (suggestion):
```suggestion
                // We need to be careful handling files which have delayed materialization to avoid
```

Code after:
    }

    public func withUnsafeDescriptor<R: Sendable>(
        _ execute: @Sendable @escaping (FileDescriptor) throws -> R
    ) async throws -> R {
        try await self.executor.execute { [sendableView] in
            try sendableView._withUnsafeDescriptor {
                return try execute($0)
            } onUnavailable: {
                FileSystemError(
                    code: .closed,
                    message: "File is closed ('\(sendableView.path)').",
                    cause: nil,
                    location: .here()
                )
            }
        }
    }

    public func detachUnsafeFileDescriptor() throws -> FileDescriptor {
        return try self.sendableView.lifecycle.withLockedValue { lifecycle in
            switch lifecycle {
            case let .open(descriptor):
                lifecycle = .detached

                // We need to be careful handling files which have delayed materialization to avoid
                // leftover temporary files.
                //
                // Where we use the 'link' mode we simply call materialize and return the
                // descriptor as it will be materialized when closed.
                //
                // For the 'rename' mode we create a hard link to the desired file and unlink the
                // created file.
                guard let materialization = self.sendableView.materialization else {
                    // File opened 'normally', just detach and return.
                    return descriptor
                }

                switch materialization.mode {
                #if canImport(Glibc) || canImport(Musl)
                case .link:
                    let result = self.sendableView._materializeLink(
                        descriptor: descriptor,
                        from: materialization.created,
                        to: materialization.desired,
                        exclusive: materialization.exclusive
                    )
                    return try result.map { descriptor }.get()
                #endif

                case .rename: