mount_unix.go 2.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// +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
17
// TODO is this non-deterministic?
18 19 20
const mountTimeout = time.Second

var mountCmd = &cmds.Command{
21 22 23 24
	Helptext: cmds.HelpText{
		Tagline: "Mounts IPFS to the filesystem (read-only)",
		ShortDescription: `
Mount ipfs at a read-only mountpoint on the OS. All ipfs objects
25 26 27
will be accessible under that directory. Note that the root will
not be listable, as it is virtual. Accessing known paths directly.
`,
28
	},
29

30
	Options: []cmds.Option{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
31
		// TODO longform
32
		cmds.StringOption("f", "The path where IPFS should be mounted"),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
33 34

		// TODO longform
35
		cmds.StringOption("n", "The path where IPNS should be mounted"),
36
	},
37
	Run: func(req cmds.Request) (interface{}, error) {
38 39 40 41 42 43 44 45 46
		cfg, err := req.Context().GetConfig()
		if err != nil {
			return nil, err
		}

		node, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
47 48

		// error if we aren't running node in online mode
49
		if node.Network == nil {
50
			return nil, errNotOnline
51 52 53
		}

		if err := platformFuseChecks(); err != nil {
54
			return nil, err
55 56
		}

57 58 59 60 61
		fsdir, found, err := req.Option("f").String()
		if err != nil {
			return nil, err
		}
		if !found {
62
			fsdir = cfg.Mounts.IPFS // use default value
63
		}
64
		fsdone := mountIpfs(node, fsdir)
65 66

		// get default mount points
67 68 69
		nsdir, found, err := req.Option("n").String()
		if err != nil {
			return nil, err
70
		}
71
		if !found {
72
			nsdir = cfg.Mounts.IPNS // NB: be sure to not redeclare!
73 74
		}

75
		nsdone := mountIpns(node, nsdir, fsdir)
76 77 78

		// wait until mounts return an error (or timeout if successful)
		select {
79 80 81 82
		case err := <-fsdone:
			return nil, err
		case err := <-nsdone:
			return nil, err
83 84 85

		// mounted successfully, we timed out with no errors
		case <-time.After(mountTimeout):
86
			output := cfg.Mounts
87
			return &output, nil
88 89 90
		}
	},
	Type: &config.Mounts{},
91
	Marshalers: cmds.MarshalerMap{
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 121 122 123 124 125 126 127 128 129 130 131 132
		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
}