diff --git a/cmd/ipfs2/daemon.go b/cmd/ipfs2/daemon.go index 32eeb5f58..32d777786 100644 --- a/cmd/ipfs2/daemon.go +++ b/cmd/ipfs2/daemon.go @@ -18,6 +18,9 @@ import ( const ( initOptionKwd = "init" + mountKwd = "mount" + ipfsMountKwd = "mount-ipfs" + ipnsMountKwd = "mount-ipns" ) var daemonCmd = &cmds.Command{ @@ -34,12 +37,24 @@ the daemon. Options: []cmds.Option{ cmds.BoolOption(initOptionKwd, "Initialize IPFS with default settings if not already initialized"), + cmds.BoolOption(mountKwd, "Mounts IPFS to the filesystem"), + cmds.StringOption(ipfsMountKwd, "Path to the mountpoint for IPFS (if using --mount)"), + cmds.StringOption(ipnsMountKwd, "Path to the mountpoint for IPNS (if using --mount)"), }, Subcommands: map[string]*cmds.Command{}, Run: daemonFunc, } func daemonFunc(req cmds.Request) (interface{}, error) { + cfg, err := req.Context().GetConfig() + if err != nil { + return nil, err + } + + node, err := core.NewIpfsNode(cfg, true) + if err != nil { + return nil, err + } initialize, _, err := req.Option(initOptionKwd).Bool() if err != nil { @@ -65,19 +80,10 @@ func daemonFunc(req cmds.Request) (interface{}, error) { } defer lock.Close() - cfg, err := req.Context().GetConfig() - if err != nil { - return nil, err - } - // setup function that constructs the context. we have to do it this way // to play along with how the Context works and thus not expose its internals req.Context().ConstructNode = func() (*core.IpfsNode, error) { - return core.NewIpfsNode(cfg, true) - } - node, err := req.Context().GetNode() - if err != nil { - return nil, err + return node, nil } addr, err := ma.NewMultiaddr(cfg.Addresses.API) @@ -90,6 +96,34 @@ func daemonFunc(req cmds.Request) (interface{}, error) { return nil, err } + // mount if the user provided the --mount flag + mount, _, err := req.Option(mountKwd).Bool() + if err != nil { + return nil, err + } + if mount { + fsdir, found, err := req.Option(ipfsMountKwd).String() + if err != nil { + return nil, err + } + if !found { + fsdir = cfg.Mounts.IPFS + } + + nsdir, found, err := req.Option(ipnsMountKwd).String() + if err != nil { + return nil, err + } + if !found { + nsdir = cfg.Mounts.IPNS + } + + err = commands.Mount(node, fsdir, nsdir) + if err != nil { + return nil, err + } + } + mux := http.NewServeMux() cmdHandler := cmdsHttp.NewHandler(*req.Context(), commands.Root) mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler) diff --git a/core/commands2/mount_unix.go b/core/commands2/mount_unix.go index 6495958ce..42763768f 100644 --- a/core/commands2/mount_unix.go +++ b/core/commands2/mount_unix.go @@ -100,25 +100,11 @@ baz return nil, err } - // check if we already have live mounts. - // if the user said "Mount", then there must be something wrong. - // so, close them and try again. - if node.Mounts.Ipfs != nil { - node.Mounts.Ipfs.Unmount() - } - if node.Mounts.Ipns != nil { - node.Mounts.Ipns.Unmount() - } - // error if we aren't running node in online mode - if node.Network == nil { + if !node.OnlineMode() { return nil, errNotOnline } - if err := platformFuseChecks(); err != nil { - return nil, err - } - fsdir, found, err := req.Option("f").String() if err != nil { return nil, err @@ -136,7 +122,8 @@ baz nsdir = cfg.Mounts.IPNS // NB: be sure to not redeclare! } - if err := doMount(node, fsdir, nsdir); err != nil { + err = Mount(node, fsdir, nsdir) + if err != nil { return nil, err } @@ -207,3 +194,26 @@ func doMount(node *core.IpfsNode, fsdir, nsdir string) error { var platformFuseChecks = func() error { return nil } + +func Mount(node *core.IpfsNode, fsdir, nsdir string) error { + // check if we already have live mounts. + // if the user said "Mount", then there must be something wrong. + // so, close them and try again. + if node.Mounts.Ipfs != nil { + node.Mounts.Ipfs.Unmount() + } + if node.Mounts.Ipns != nil { + node.Mounts.Ipns.Unmount() + } + + if err := platformFuseChecks(); err != nil { + return err + } + + var err error + if err = doMount(node, fsdir, nsdir); err != nil { + return err + } + + return nil +}