readonly_unix.go 4.18 KB
Newer Older
1 2
// A Go mirror of libfuse's hello.c

3
// +build linux darwin freebsd
4

5 6 7
package readonly

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8 9 10 11 12 13 14
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"runtime"
	"syscall"
	"time"
15 16 17 18 19 20

	"bazil.org/fuse"
	"bazil.org/fuse/fs"
	core "github.com/jbenet/go-ipfs/core"
	mdag "github.com/jbenet/go-ipfs/merkledag"
	u "github.com/jbenet/go-ipfs/util"
21 22
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
23
// FileSystem is the readonly Ipfs Fuse Filesystem.
24
type FileSystem struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25
	Ipfs *core.IpfsNode
26 27
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
28
// NewFileSystem constructs new fs using given core.IpfsNode instance.
29
func NewFileSystem(ipfs *core.IpfsNode) *FileSystem {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
	return &FileSystem{Ipfs: ipfs}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
33
// Root constructs the Root of the filesystem, a Root object.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34
func (f FileSystem) Root() (fs.Node, fuse.Error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
	return &Root{Ipfs: f.Ipfs}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
38
// Root is the root object of the filesystem tree.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39
type Root struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
	Ipfs *core.IpfsNode
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
43
// Attr returns file attributes.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45
func (*Root) Attr() fuse.Attr {
	return fuse.Attr{Mode: os.ModeDir | 0111} // -rw+x
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
48
// Lookup performs a lookup under this node.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
func (s *Root) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
50
	u.DOut("Root Lookup: '%s'\n", name)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51 52 53 54 55 56 57 58 59 60 61 62 63
	switch name {
	case "mach_kernel", ".hidden", "._.":
		// Just quiet some log noise on OS X.
		return nil, fuse.ENOENT
	}

	nd, err := s.Ipfs.Resolver.ResolvePath(name)
	if err != nil {
		// todo: make this error more versatile.
		return nil, fuse.ENOENT
	}

	return &Node{Ipfs: s.Ipfs, Nd: nd}, nil
64 65
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
66
// ReadDir reads a particular directory. Disallowed for root.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
func (*Root) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
68
	u.DOut("Read Root.\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69
	return nil, fuse.EPERM
70 71
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
72
// Node is the core object representing a filesystem tree node.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
type Node struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74 75
	Ipfs *core.IpfsNode
	Nd   *mdag.Node
76 77
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
78
// Attr returns the attributes of a given node.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
func (s *Node) Attr() fuse.Attr {
80
	u.DOut("Node attr.\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81 82 83
	if len(s.Nd.Links) > 0 {
		return fuse.Attr{Mode: os.ModeDir | 0555}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85 86
	size, _ := s.Nd.Size()
	return fuse.Attr{Mode: 0444, Size: uint64(size)}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
}
88

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
89
// Lookup performs a lookup under this node.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
func (s *Node) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
91
	u.DOut("Lookup '%s'\n", name)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93 94 95 96
	nd, err := s.Ipfs.Resolver.ResolveLinks(s.Nd, []string{name})
	if err != nil {
		// todo: make this error more versatile.
		return nil, fuse.ENOENT
	}
97

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98
	return &Node{Ipfs: s.Ipfs, Nd: nd}, nil
99 100
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
101
// ReadDir reads the link structure as directory entries
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102
func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
103
	u.DOut("Node ReadDir\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104 105 106 107 108 109 110 111 112 113 114 115 116
	entries := make([]fuse.Dirent, len(s.Nd.Links))
	for i, link := range s.Nd.Links {
		n := link.Name
		if len(n) == 0 {
			n = link.Hash.B58String()
		}
		entries[i] = fuse.Dirent{Name: n, Type: fuse.DT_File}
	}

	if len(entries) > 0 {
		return entries, nil
	}
	return nil, fuse.ENOENT
117 118
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
119
// ReadAll reads the object data as file data
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
120
func (s *Node) ReadAll(intr fs.Intr) ([]byte, fuse.Error) {
121
	u.DOut("Read node.\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122
	return []byte(s.Nd.Data), nil
123 124
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
125 126
// Mount mounts an IpfsNode instance at a particular path. It
// serves until the process receives exit signals (to Unmount).
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
func Mount(ipfs *core.IpfsNode, fpath string) error {

	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT,
		syscall.SIGTERM, syscall.SIGQUIT)

	go func() {
		<-sigc
		Unmount(fpath)
	}()

	c, err := fuse.Mount(fpath)
	if err != nil {
		return err
	}
	defer c.Close()

	err = fs.Serve(c, FileSystem{Ipfs: ipfs})
	if err != nil {
		return err
	}

	// check if the mount process has an error to report
	<-c.Ready
	if err := c.MountError; err != nil {
		return err
	}
	return nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
156

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
// Unmount attempts to unmount the provided FUSE mount point, forcibly
// if necessary.
func Unmount(point string) error {
	fmt.Printf("Unmounting %s...\n", point)

	var cmd *exec.Cmd
	switch runtime.GOOS {
	case "darwin":
		cmd = exec.Command("diskutil", "umount", "force", point)
	case "linux":
		cmd = exec.Command("fusermount", "-u", point)
	default:
		return fmt.Errorf("unmount: unimplemented")
	}

	errc := make(chan error, 1)
	go func() {
		if err := exec.Command("umount", point).Run(); err == nil {
			errc <- err
		}
		// retry to unmount with the fallback cmd
		errc <- cmd.Run()
	}()

	select {
	case <-time.After(1 * time.Second):
		return fmt.Errorf("umount timeout")
	case err := <-errc:
		return err
	}
187
}