Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the NFS3 protocol #155

Merged
merged 10 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,20 @@ var targets: [PackageDescription.Target] = [
"NIOSOCKS",
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOEmbedded", package: "swift-nio"),
])
]),
.target(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we intentionally not defining a product here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lukasa for now, yes. In a follow-up I might either add a product (either NIONFS3 or _NIONFS3Preview) but we can discuss then. I want to first land the bulk of the code

name: "NIONFS3",
dependencies: [
.product(name: "NIOCore", package: "swift-nio"),
]),
.testTarget(
name: "NIONFS3Tests",
dependencies: [
"NIONFS3",
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOEmbedded", package: "swift-nio"),
.product(name: "NIOTestUtils", package: "swift-nio"),
]),
]

let package = Package(
Expand Down
14 changes: 13 additions & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,19 @@ var targets: [PackageDescription.Target] = [
"NIOSOCKS",
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOEmbedded", package: "swift-nio"),
])
]),
.target(
name: "NIONFS3",
dependencies: [
.product(name: "NIOCore", package: "swift-nio"),
]),
.testTarget(
name: "NIONFS3Tests",
dependencies: [
"NIONFS3",
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOTestUtils", package: "swift-nio"),
]),
]

let package = Package(
Expand Down
85 changes: 85 additions & 0 deletions Sources/NIONFS3/MountTypes+Mount.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021-2023 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 NIOCore

// MARK: - Mount
public struct MountCallMount: Hashable & Sendable {
public init(dirPath: String) {
self.dirPath = dirPath
}

public var dirPath: String
}

public struct MountReplyMount: Hashable & Sendable {
public init(result: NFS3Result<MountReplyMount.Okay, NFS3Nothing>) {
self.result = result
}

public struct Okay: Hashable & Sendable {
public init(fileHandle: NFS3FileHandle, authFlavors: [RPCAuthFlavor] = [.unix]) {
self.fileHandle = fileHandle
self.authFlavors = authFlavors
}

public var fileHandle: NFS3FileHandle
public var authFlavors: [RPCAuthFlavor] = [.unix]
}

public var result: NFS3Result<Okay, NFS3Nothing>
}

extension ByteBuffer {
public mutating func readNFS3CallMount() throws -> MountCallMount {
let dirPath = try self.readNFS3String()
return MountCallMount(dirPath: dirPath)
}

@discardableResult public mutating func writeNFS3CallMount(_ call: MountCallMount) -> Int {
self.writeNFS3String(call.dirPath)
}

@discardableResult public mutating func writeNFS3ReplyMount(_ reply: MountReplyMount) -> Int {
var bytesWritten = self.writeNFS3ResultStatus(reply.result)

switch reply.result {
case .okay(let reply):
bytesWritten += self.writeNFS3FileHandle(reply.fileHandle)
precondition(reply.authFlavors == [.unix] || reply.authFlavors == [.noAuth],
"Sorry, anything but [.unix] / [.system] / [.noAuth] unimplemented.")
glbrntt marked this conversation as resolved.
Show resolved Hide resolved
bytesWritten += self.writeInteger(UInt32(reply.authFlavors.count), as: UInt32.self)
for flavor in reply.authFlavors {
bytesWritten += self.writeInteger(flavor.rawValue, as: UInt32.self)
}
case .fail(_, _):
()
}

return bytesWritten
}

public mutating func readNFS3ReplyMount() throws -> MountReplyMount {
let result = try self.readNFS3Result(readOkay: { buffer -> MountReplyMount.Okay in
let fileHandle = try buffer.readNFS3FileHandle()
let authFlavors = try buffer.readNFS3List(readEntry: { buffer in
try buffer.readRPCAuthFlavor()
})
return MountReplyMount.Okay(fileHandle: fileHandle, authFlavors: authFlavors)

},
readFail: { _ in NFS3Nothing() })
return MountReplyMount(result: result)
}
}
30 changes: 30 additions & 0 deletions Sources/NIONFS3/MountTypes+Null.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021-2023 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 NIOCore

// MARK: - Null
public struct MountCallNull: Hashable & Sendable {
public init() {}
}

extension ByteBuffer {
public mutating func readMountCallNull() throws -> MountCallNull {
glbrntt marked this conversation as resolved.
Show resolved Hide resolved
weissi marked this conversation as resolved.
Show resolved Hide resolved
return MountCallNull()
}

@discardableResult public mutating func writeMountCallNull(_ call: MountCallNull) -> Int {
return 0
}
}
47 changes: 47 additions & 0 deletions Sources/NIONFS3/MountTypes+Unmount.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021-2023 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 NIOCore

// MARK: - Unmount
public struct MountCallUnmount: Hashable & Sendable {
public init(dirPath: String) {
self.dirPath = dirPath
}

public var dirPath: String
}

public struct MountReplyUnmount: Hashable & Sendable {
public init() {}
}

extension ByteBuffer {
public mutating func readNFS3CallUnmount() throws -> MountCallUnmount {
let dirPath = try self.readNFS3String()
return MountCallUnmount(dirPath: dirPath)
}

@discardableResult public mutating func writeNFS3CallUnmount(_ call: MountCallUnmount) -> Int {
self.writeNFS3String(call.dirPath)
}

@discardableResult public mutating func writeNFS3ReplyUnmount(_ reply: MountReplyUnmount) -> Int {
return 0
}

public mutating func readNFS3ReplyUnmount() throws -> MountReplyUnmount {
return MountReplyUnmount()
}
}
37 changes: 37 additions & 0 deletions Sources/NIONFS3/NFSCallDecoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021-2023 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 NIOCore

public struct NFS3CallDecoder: NIOSingleStepByteToMessageDecoder {
public typealias InboundOut = RPCNFS3Call

public init() {}

public mutating func decode(buffer: inout ByteBuffer) throws -> RPCNFS3Call? {
guard let message = try buffer.readRPCMessage() else {
return nil
}

guard case (.call(let call), var body) = message else {
throw NFS3Error.wrongMessageType(message.0)
}

return try body.readNFS3Call(rpc: call)
}

public mutating func decodeLast(buffer: inout ByteBuffer, seenEOF: Bool) throws -> RPCNFS3Call? {
return try self.decode(buffer: &buffer)
}
}
25 changes: 25 additions & 0 deletions Sources/NIONFS3/NFSCallEncoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021-2023 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 NIOCore

public struct NFS3CallEncoder: MessageToByteEncoder {
public typealias OutboundIn = RPCNFS3Call

public init() {}

public func encode(data: RPCNFS3Call, out: inout ByteBuffer) throws {
out.writeRPCNFS3Call(data)
}
}
Loading