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

9P host and guest support #39

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions internal/commands/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"unicode/utf8"
"unsafe"

"github.com/djdv/go-filesystem-utils/internal/filesystem"
"github.com/djdv/go-filesystem-utils/internal/generic"
"github.com/djdv/p9/p9"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -579,3 +580,13 @@ func parseMultiaddrList(parameter string) ([]multiaddr.Multiaddr, error) {
}
return maddrs, nil
}

func prefixIDFlag[
T interface {
~string
filesystem.Host | filesystem.ID
},
](ID T,
) string {
return strings.ToLower(string(ID)) + "-"
}
2 changes: 2 additions & 0 deletions internal/commands/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ func makeHostCommands() []command.Command {
var (
commandMakers = []makeCommand{
makeFUSECommand,
makePlan9HostCommand,
}
commands = make([]command.Command, 0, len(commandMakers))
)
Expand All @@ -292,6 +293,7 @@ func makeGuestCommands[
](host filesystem.Host,
) []command.Command {
guests := makeIPFSCommands[HC, HM](host)
guests = append(guests, makePlan9GuestCommand[HC, HM](host))
sortCommands(guests)
return guests
}
Expand Down
2 changes: 2 additions & 0 deletions internal/commands/mountpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func makeMountPointHosts(path ninePath, autoUnlink bool) mountPointHosts {
var (
hostMakers = []makeHostsFunc{
makeFUSEHost,
makePlan9Host,
}
hosts = make(mountPointHosts, len(hostMakers))
)
Expand Down Expand Up @@ -142,6 +143,7 @@ func makeMountPointGuests[
) mountPointGuests {
guests := make(mountPointGuests)
makeIPFSGuests[HC](guests, path)
guests[p9fs.GuestID] = newMountPointFunc[HC, p9fs.Guest](path)
return guests
}

Expand Down
121 changes: 121 additions & 0 deletions internal/commands/mountpoint_9p.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package commands

import (
"encoding/json"
"flag"
"fmt"

"github.com/djdv/go-filesystem-utils/internal/command"
"github.com/djdv/go-filesystem-utils/internal/filesystem"
p9fs "github.com/djdv/go-filesystem-utils/internal/filesystem/9p"
"github.com/djdv/go-filesystem-utils/internal/generic"
"github.com/multiformats/go-multiaddr"
)

type (
plan9GuestSettings p9fs.Guest
plan9GuestOption func(*plan9GuestSettings) error
plan9GuestOptions []plan9GuestOption

plan9HostSettings p9fs.Host
plan9HostOption func(*plan9HostSettings) error
plan9HostOptions []plan9HostOption
)

const p9ServerFlagName = "server"

func makePlan9HostCommand() command.Command {
return makeMountSubcommand(
p9fs.HostID,
makeGuestCommands[plan9HostOptions, plan9HostSettings](p9fs.HostID),
)
}

func makePlan9Host(path ninePath, autoUnlink bool) (filesystem.Host, p9fs.MakeGuestFunc) {
guests := makeMountPointGuests[p9fs.Host](path)
return p9fs.HostID, newMakeGuestFunc(guests, path, autoUnlink)
}

func unmarshalPlan9() (filesystem.Host, decodeFunc) {
return p9fs.HostID, func(b []byte) (string, error) {
var host p9fs.Host
err := json.Unmarshal(b, &host)
return host.Maddr.String(), err
}
}

func (*plan9HostOptions) usage(guest filesystem.ID) string {
return string(p9fs.HostID) + " hosts " +
string(guest) + " as a 9P file server"
}

func (*plan9HostOptions) BindFlags(*flag.FlagSet) { /* NOOP */ }

func (o9 plan9HostOptions) make() (plan9HostSettings, error) {
return makeWithOptions(o9...)
}

func (set plan9HostSettings) marshal(arg string) ([]byte, error) {
if arg == "" {
err := command.UsageError{
Err: generic.ConstError(
"expected server multiaddr",
),
}
return nil, err
}
maddr, err := multiaddr.NewMultiaddr(arg)
if err != nil {
return nil, err
}
set.Maddr = maddr
return json.Marshal(set)
}

func makePlan9GuestCommand[
HC mountCmdHost[HT, HM],
HM marshaller,
HT any,
](host filesystem.Host,
) command.Command {
return makeMountCommand[HC, HM, plan9GuestOptions, plan9GuestSettings](host, p9fs.GuestID)
}

func (*plan9GuestOptions) usage(filesystem.Host) string {
return string(p9fs.GuestID) + " attaches to a 9P file server"
}

func (o9 *plan9GuestOptions) BindFlags(flagSet *flag.FlagSet) {
var (
flagPrefix = prefixIDFlag(p9fs.GuestID)
srvUsage = "9P2000.L file system server `maddr`"
srvName = flagPrefix + p9ServerFlagName
)
flagSetFunc(flagSet, srvName, srvUsage, o9,
func(value multiaddr.Multiaddr, settings *plan9GuestSettings) error {
settings.Maddr = value
return nil
})
}

func (o9 plan9GuestOptions) make() (plan9GuestSettings, error) {
settings, err := makeWithOptions(o9...)
if err != nil {
return plan9GuestSettings{}, err
}
if settings.Maddr == nil {
var (
flagPrefix = prefixIDFlag(p9fs.GuestID)
srvName = flagPrefix + p9ServerFlagName
)
return plan9GuestSettings{}, fmt.Errorf(
"flag `-%s` must be provided for 9P guests",
srvName,
)
}
return settings, nil
}

func (s9 plan9GuestSettings) marshal(string) ([]byte, error) {
return json.Marshal(s9)
}
4 changes: 0 additions & 4 deletions internal/commands/mountpoint_ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ func guestOverlayText(overlay, overlaid filesystem.ID) string {
return string(overlay) + " is an " + string(overlaid) + " overlay"
}

func prefixIDFlag(system filesystem.ID) string {
return strings.ToLower(string(system)) + "-"
}

func (*ipfsOptions) usage(filesystem.Host) string {
return string(ipfs.IPFSID) + " provides an empty root directory." +
"\nChild paths are forwarded to the IPFS API."
Expand Down
1 change: 1 addition & 0 deletions internal/commands/unmount.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func newDecodeTargetFunc() p9fs.DecodeTargetFunc {
var (
decoderMakers = []makeDecoderFunc{
unmarshalFUSE,
unmarshalPlan9,
}
decoders = make(decoders, len(decoderMakers))
)
Expand Down
Loading