github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/cmd/ipfs/mount_unix.go (about)

     1  // +build linux darwin freebsd
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
     9  	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
    10  
    11  	core "github.com/jbenet/go-ipfs/core"
    12  	ipns "github.com/jbenet/go-ipfs/fuse/ipns"
    13  	rofs "github.com/jbenet/go-ipfs/fuse/readonly"
    14  )
    15  
    16  var cmdIpfsMount = &commander.Command{
    17  	UsageLine: "mount",
    18  	Short:     "Mount an ipfs read-only mountpoint.",
    19  	Long: `ipfs mount <os-path> - Mount an ipfs read-only mountpoint.
    20  
    21      Mount ipfs at a read-only mountpoint on the OS. All ipfs objects
    22      will be accessible under that directory. Note that the root will
    23      not be listable, as it is virtual. Accessing known paths directly.
    24  
    25  `,
    26  	Run:  mountCmd,
    27  	Flag: *flag.NewFlagSet("ipfs-mount", flag.ExitOnError),
    28  }
    29  
    30  func init() {
    31  	cmdIpfsMount.Flag.String("f", "", "specify a mountpoint for ipfs")
    32  	cmdIpfsMount.Flag.String("n", "", "specify a mountpoint for ipns")
    33  }
    34  
    35  func mountCmd(c *commander.Command, inp []string) error {
    36  	if err := platformFuseChecks(); err != nil {
    37  		return err
    38  	}
    39  
    40  	cc, err := setupCmdContext(c, true)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	defer cc.daemon.Close()
    45  
    46  	// update fsdir with flag.
    47  	fsdir := cc.node.Config.Mounts.IPFS
    48  	if val, ok := c.Flag.Lookup("f").Value.Get().(string); ok && val != "" {
    49  		fsdir = val
    50  	}
    51  	fsdone := mountIpfs(cc.node, fsdir)
    52  
    53  	// get default mount points
    54  	nsdir := cc.node.Config.Mounts.IPNS
    55  	if val, ok := c.Flag.Lookup("n").Value.Get().(string); ok && val != "" {
    56  		nsdir = val
    57  	}
    58  	nsdone := mountIpns(cc.node, nsdir, fsdir)
    59  
    60  	// wait till mounts are done.
    61  	err1 := <-fsdone
    62  	err2 := <-nsdone
    63  
    64  	if err1 != nil {
    65  		return err1
    66  	}
    67  	return err2
    68  }
    69  
    70  func mountIpfs(node *core.IpfsNode, fsdir string) <-chan error {
    71  	done := make(chan error)
    72  	fmt.Printf("mounting ipfs at %s\n", fsdir)
    73  
    74  	go func() {
    75  		err := rofs.Mount(node, fsdir)
    76  		done <- err
    77  		close(done)
    78  	}()
    79  
    80  	return done
    81  }
    82  
    83  func mountIpns(node *core.IpfsNode, nsdir, fsdir string) <-chan error {
    84  	if nsdir == "" {
    85  		return nil
    86  	}
    87  	done := make(chan error)
    88  	fmt.Printf("mounting ipns at %s\n", nsdir)
    89  
    90  	go func() {
    91  		err := ipns.Mount(node, nsdir, fsdir)
    92  		done <- err
    93  		close(done)
    94  	}()
    95  
    96  	return done
    97  }
    98  
    99  var platformFuseChecks = func() error {
   100  	return nil
   101  }