mount_unix.go 2.14 KB
Newer Older
1
// +build linux darwin freebsd
2

3 4 5
package main

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	"errors"
7
	"fmt"
8

9 10
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
11 12
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"

13
	"github.com/jbenet/go-ipfs/daemon"
14
	ipns "github.com/jbenet/go-ipfs/fuse/ipns"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	rofs "github.com/jbenet/go-ipfs/fuse/readonly"
)

var cmdIpfsMount = &commander.Command{
	UsageLine: "mount",
	Short:     "Mount an ipfs read-only mountpoint.",
	Long: `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:  mountCmd,
	Flag: *flag.NewFlagSet("ipfs-mount", flag.ExitOnError),
}

32 33
func init() {
	cmdIpfsMount.Flag.String("n", "", "specify a mountpoint for ipns")
Jeromy's avatar
Jeromy committed
34
	cmdIpfsMount.Flag.String("f", "/ipfs", "specify a mountpoint for ipfs")
35 36
}

37
func mountCmd(c *commander.Command, inp []string) error {
38 39
	conf, err := getConfigDir(c.Parent)
	if err != nil {
40
		fmt.Println("Couldnt get config dir")
41 42
		return err
	}
Jeromy's avatar
Jeromy committed
43
	n, err := localNode(conf, true)
44
	if err != nil {
45
		fmt.Println("Local node creation failed.")
46 47 48
		return err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50
	// launch the API RPC endpoint.
	if n.Config.Addresses.API == "" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51 52 53
		return errors.New("no config.RPCAddress endpoint supplied")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
	maddr, err := ma.NewMultiaddr(n.Config.Addresses.API)
55 56 57 58
	if err != nil {
		return err
	}

59
	dl, err := daemon.NewDaemonListener(n, maddr, conf)
60
	if err != nil {
61
		fmt.Println("Failed to create daemon listener.")
62 63 64 65 66
		return err
	}
	go dl.Listen()
	defer dl.Close()

Jeromy's avatar
Jeromy committed
67
	mp := c.Flag.Lookup("f").Value.Get().(string)
68
	fmt.Printf("Mounting at %s\n", mp)
69

70
	var ipnsDone chan struct{}
71
	ns, ok := c.Flag.Lookup("n").Value.Get().(string)
Jeromy's avatar
Jeromy committed
72
	if ok && ns != "" {
73
		ipnsDone = make(chan struct{})
74 75 76 77 78
		go func() {
			err = ipns.Mount(n, ns, mp)
			if err != nil {
				fmt.Printf("Error mounting ipns: %s\n", err)
			}
79
			ipnsDone <- struct{}{}
80 81 82
		}()
	}

83 84 85 86 87
	err = rofs.Mount(n, mp)
	if ipnsDone != nil {
		<-ipnsDone
	}
	return err
88
}