Skip to content

Commit

Permalink
commands: mount - add 9P2000.L host support
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Sep 13, 2023
1 parent 8f5e41f commit bc78f07
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 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 Down
1 change: 1 addition & 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
65 changes: 65 additions & 0 deletions internal/commands/mountpoint_9p.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,82 @@ import (
"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 p9GuestSrvFlagName = "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 (o9 *plan9HostOptions) BindFlags(flagSet *flag.FlagSet) {
// TODO: - dedupe with guest
var (
flagPrefix = prefixIDFlag(p9fs.HostID)
srvUsage = "9P2000.L file system server `maddr`"
srvName = flagPrefix + p9GuestSrvFlagName
)
flagSetFunc(flagSet, srvName, srvUsage, o9,
func(value multiaddr.Multiaddr, settings *plan9HostSettings) error {
settings.Maddr = value
return nil
})
}

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,
Expand Down

0 comments on commit bc78f07

Please sign in to comment.