mount_unix.go 2.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// +build linux darwin freebsd

package commands

import (
	"fmt"
	"time"

	cmds "github.com/jbenet/go-ipfs/commands"
	"github.com/jbenet/go-ipfs/config"
	core "github.com/jbenet/go-ipfs/core"
	ipns "github.com/jbenet/go-ipfs/fuse/ipns"
	rofs "github.com/jbenet/go-ipfs/fuse/readonly"
)

// amount of time to wait for mount errors
const mountTimeout = time.Second

var mountCmd = &cmds.Command{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
20 21 22
	Arguments: []cmds.Argument{
		cmds.Argument{Name: "os-path", Type: cmds.ArgString, Required: false, Variadic: false},
	},
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	Options: []cmds.Option{
		cmds.Option{[]string{"f"}, cmds.String},
		cmds.Option{[]string{"n"}, cmds.String},
	},
	Help: `ipfs mount <os-path> - Mount an ipfs read-only mountpoint.

    Mount ipfs at a read-only mountpoint on the OS. All ipfs objects
    will be accessible under that directory. Note that the root will
    not be listable, as it is virtual. Accessing known paths directly.
`,
	Run: func(res cmds.Response, req cmds.Request) {
		ctx := req.Context()

		// error if we aren't running node in online mode
		if ctx.Node.Network == nil {
			res.SetError(errNotOnline, cmds.ErrNormal)
			return
		}

		if err := platformFuseChecks(); err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		// update fsdir with flag.
		fsdir := ctx.Config.Mounts.IPFS
		opt, _ := req.Option("f")
		if val, ok := opt.(string); ok && val != "" {
			fsdir = val
		}
		fsdone := mountIpfs(ctx.Node, fsdir)

		// get default mount points
		nsdir := ctx.Config.Mounts.IPNS
		opt, _ = req.Option("f")
		if val, ok := opt.(string); ok && val != "" {
			nsdir = val
		}
		nsdone := mountIpns(ctx.Node, nsdir, fsdir)

		// wait until mounts return an error (or timeout if successful)
		var err error
		select {
		case err = <-fsdone:
		case err = <-nsdone:

		// mounted successfully, we timed out with no errors
		case <-time.After(mountTimeout):
			output := ctx.Config.Mounts
			res.SetOutput(&output)
			return
		}

		res.SetError(err, cmds.ErrNormal)
	},
	Type: &config.Mounts{},
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			v := res.Output().(*config.Mounts)
			s := fmt.Sprintf("IPFS mounted at: %s\n", v.IPFS)
			s += fmt.Sprintf("IPNS mounted at: %s\n", v.IPNS)
			return []byte(s), nil
		},
	},
}

func mountIpfs(node *core.IpfsNode, fsdir string) <-chan error {
	done := make(chan error)
	log.Info("Mounting IPFS at ", fsdir)

	go func() {
		err := rofs.Mount(node, fsdir)
		done <- err
		close(done)
	}()

	return done
}

func mountIpns(node *core.IpfsNode, nsdir, fsdir string) <-chan error {
	if nsdir == "" {
		return nil
	}
	done := make(chan error)
	log.Info("Mounting IPNS at ", nsdir)

	go func() {
		err := ipns.Mount(node, nsdir, fsdir)
		done <- err
		close(done)
	}()

	return done
}

var platformFuseChecks = func() error {
	return nil
}