|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021-2023 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIOCore |
| 16 | + |
| 17 | +// MARK: - Mount |
| 18 | +public struct MountCallMount: Hashable & Sendable { |
| 19 | + public init(dirPath: String) { |
| 20 | + self.dirPath = dirPath |
| 21 | + } |
| 22 | + |
| 23 | + public var dirPath: String |
| 24 | +} |
| 25 | + |
| 26 | +public struct MountReplyMount: Hashable & Sendable { |
| 27 | + public init(result: NFS3Result<MountReplyMount.Okay, NFS3Nothing>) { |
| 28 | + self.result = result |
| 29 | + } |
| 30 | + |
| 31 | + public struct Okay: Hashable & Sendable { |
| 32 | + public init(fileHandle: NFS3FileHandle, authFlavors: [RPCAuthFlavor] = [.unix]) { |
| 33 | + self.fileHandle = fileHandle |
| 34 | + self.authFlavors = authFlavors |
| 35 | + } |
| 36 | + |
| 37 | + public var fileHandle: NFS3FileHandle |
| 38 | + public var authFlavors: [RPCAuthFlavor] = [.unix] |
| 39 | + } |
| 40 | + |
| 41 | + public var result: NFS3Result<Okay, NFS3Nothing> |
| 42 | +} |
| 43 | + |
| 44 | +extension ByteBuffer { |
| 45 | + public mutating func readNFS3CallMount() throws -> MountCallMount { |
| 46 | + let dirPath = try self.readNFS3String() |
| 47 | + return MountCallMount(dirPath: dirPath) |
| 48 | + } |
| 49 | + |
| 50 | + @discardableResult public mutating func writeNFS3CallMount(_ call: MountCallMount) -> Int { |
| 51 | + self.writeNFS3String(call.dirPath) |
| 52 | + } |
| 53 | + |
| 54 | + @discardableResult public mutating func writeNFS3ReplyMount(_ reply: MountReplyMount) -> Int { |
| 55 | + var bytesWritten = self.writeNFS3ResultStatus(reply.result) |
| 56 | + |
| 57 | + switch reply.result { |
| 58 | + case .okay(let reply): |
| 59 | + bytesWritten += self.writeNFS3FileHandle(reply.fileHandle) |
| 60 | + precondition(reply.authFlavors == [.unix] || reply.authFlavors == [.noAuth], |
| 61 | + "Sorry, anything but [.unix] / [.system] / [.noAuth] unimplemented.") |
| 62 | + bytesWritten += self.writeInteger(UInt32(reply.authFlavors.count), as: UInt32.self) |
| 63 | + for flavor in reply.authFlavors { |
| 64 | + bytesWritten += self.writeInteger(flavor.rawValue, as: UInt32.self) |
| 65 | + } |
| 66 | + case .fail(_, _): |
| 67 | + () |
| 68 | + } |
| 69 | + |
| 70 | + return bytesWritten |
| 71 | + } |
| 72 | + |
| 73 | + public mutating func readNFS3ReplyMount() throws -> MountReplyMount { |
| 74 | + let result = try self.readNFS3Result(readOkay: { buffer -> MountReplyMount.Okay in |
| 75 | + let fileHandle = try buffer.readNFS3FileHandle() |
| 76 | + let authFlavors = try buffer.readNFS3List(readEntry: { buffer in |
| 77 | + try buffer.readRPCAuthFlavor() |
| 78 | + }) |
| 79 | + return MountReplyMount.Okay(fileHandle: fileHandle, authFlavors: authFlavors) |
| 80 | + |
| 81 | + }, |
| 82 | + readFail: { _ in NFS3Nothing() }) |
| 83 | + return MountReplyMount(result: result) |
| 84 | + } |
| 85 | +} |
0 commit comments