Skip to content

Commit 11e7a71

Browse files
jmozahfjl
authored andcommitted
swarm/api: support mounting manifests via FUSE (bnb-chain#3690)
1 parent 61d2150 commit 11e7a71

44 files changed

Lines changed: 7005 additions & 3 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.travis.yml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,34 @@ matrix:
55
include:
66
- os: linux
77
dist: trusty
8+
sudo: required
89
go: 1.7.5
10+
script:
11+
- sudo -E apt-get -yq --no-install-suggests --no-install-recommends --force-yes install fuse
12+
- sudo modprobe fuse
13+
- sudo chmod 666 /dev/fuse
14+
- sudo chown root:$USER /etc/fuse.conf
915

1016
# These are the latest Go versions, only run go vet and misspell on these
1117
- os: linux
1218
dist: trusty
19+
sudo: required
1320
go: 1.8
1421
script:
22+
- sudo -E apt-get -yq --no-install-suggests --no-install-recommends --force-yes install fuse
23+
- sudo modprobe fuse
24+
- sudo chmod 666 /dev/fuse
25+
- sudo chown root:$USER /etc/fuse.conf
1526
- go run build/ci.go install
1627
- go run build/ci.go test -coverage -vet -misspell
1728

1829
- os: osx
1930
go: 1.8
31+
sudo: required
2032
script:
33+
- brew update
34+
- brew install caskroom/cask/brew-cask
35+
- brew cask install osxfuse
2136
- go run build/ci.go install
2237
- go run build/ci.go test -coverage -vet -misspell
2338

‎build/ci.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ func doInstall(cmdline []string) {
173173
if flag.NArg() > 0 {
174174
packages = flag.Args()
175175
}
176+
177+
// Resolve ./... manually and remove vendor/bazil/fuse (fuse is not in windows)
178+
out, err := goTool("list", "./...").CombinedOutput()
179+
if err != nil {
180+
log.Fatalf("package listing failed: %v\n%s", err, string(out))
181+
}
182+
packages = []string{}
183+
for _, line := range strings.Split(string(out), "\n") {
184+
if !strings.Contains(line, "vendor") {
185+
packages = append(packages, strings.TrimSpace(line))
186+
}
187+
}
188+
189+
176190
if *arch == "" || *arch == runtime.GOARCH {
177191
goinstall := goTool("install", buildFlags(env)...)
178192
goinstall.Args = append(goinstall.Args, "-v")

‎cmd/swarm/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func init() {
122122
// Override flag defaults so bzzd can run alongside geth.
123123
utils.ListenPortFlag.Value = 30399
124124
utils.IPCPathFlag.Value = utils.DirectoryString{Value: "bzzd.ipc"}
125-
utils.IPCApiFlag.Value = "admin, bzz, chequebook, debug, rpc, web3"
125+
utils.IPCApiFlag.Value = "admin, bzz, chequebook, debug, rpc, swarmfs, web3"
126126

127127
// Set up the cli app.
128128
app.Action = bzzd

‎internal/web3ext/web3ext.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ var Modules = map[string]string{
2727
"personal": Personal_JS,
2828
"rpc": RPC_JS,
2929
"shh": Shh_JS,
30+
"swarmfs": SWARMFS_JS,
3031
"txpool": TxPool_JS,
32+
3133
}
3234

35+
3336
const Chequebook_JS = `
3437
web3._extend({
3538
property: 'chequebook',
@@ -486,6 +489,32 @@ web3._extend({
486489
]
487490
});
488491
`
492+
const SWARMFS_JS = `
493+
web3._extend({
494+
property: 'swarmfs',
495+
methods:
496+
[
497+
new web3._extend.Method({
498+
name: 'mount',
499+
call: 'swarmfs_mount',
500+
params: 2,
501+
inputFormatter: [null,null]
502+
}),
503+
new web3._extend.Method({
504+
name: 'unmount',
505+
call: 'swarmfs_unmount',
506+
params: 1,
507+
inputFormatter: [null]
508+
}),
509+
new web3._extend.Method({
510+
name: 'listmounts',
511+
call: 'swarmfs_listmounts',
512+
params: 0,
513+
inputFormatter: []
514+
})
515+
]
516+
});
517+
`
489518

490519
const TxPool_JS = `
491520
web3._extend({

‎swarm/api/fuse.go‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build !windows
18+
19+
package api
20+
21+
import (
22+
"io"
23+
"os"
24+
25+
"bazil.org/fuse"
26+
"bazil.org/fuse/fs"
27+
"github.com/ethereum/go-ethereum/log"
28+
"github.com/ethereum/go-ethereum/swarm/storage"
29+
"golang.org/x/net/context"
30+
)
31+
32+
33+
34+
35+
// Data structures used for Fuse filesystem, serving directories and serving files to Fuse driver
36+
type FS struct {
37+
root *Dir
38+
}
39+
40+
type Dir struct {
41+
inode uint64
42+
name string
43+
path string
44+
directories []*Dir
45+
files []*File
46+
}
47+
48+
type File struct {
49+
inode uint64
50+
name string
51+
path string
52+
key storage.Key
53+
swarmApi *Api
54+
fileSize uint64
55+
reader storage.LazySectionReader
56+
}
57+
58+
59+
// Functions which satisfy the Fuse File System requests
60+
func (filesystem *FS) Root() (fs.Node, error) {
61+
return filesystem.root, nil
62+
}
63+
64+
func (directory *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
65+
a.Inode = directory.inode
66+
//TODO: need to get permission as argument
67+
a.Mode = os.ModeDir | 0500
68+
a.Uid = uint32(os.Getuid())
69+
a.Gid = uint32(os.Getegid())
70+
return nil
71+
}
72+
73+
func (directory *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
74+
if directory.files != nil {
75+
for _, n := range directory.files {
76+
if n.name == name {
77+
return n, nil
78+
}
79+
}
80+
}
81+
if directory.directories != nil {
82+
for _, n := range directory.directories {
83+
if n.name == name {
84+
return n, nil
85+
}
86+
}
87+
}
88+
return nil, fuse.ENOENT
89+
}
90+
91+
func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
92+
var children []fuse.Dirent
93+
if d.files != nil {
94+
for _, file := range d.files {
95+
children = append(children, fuse.Dirent{Inode: file.inode, Type: fuse.DT_File, Name: file.name})
96+
}
97+
}
98+
if d.directories != nil {
99+
for _, dir := range d.directories {
100+
children = append(children, fuse.Dirent{Inode: dir.inode, Type: fuse.DT_Dir, Name: dir.name})
101+
}
102+
}
103+
return children, nil
104+
}
105+
106+
func (file *File) Attr(ctx context.Context, a *fuse.Attr) error {
107+
108+
a.Inode = file.inode
109+
//TODO: need to get permission as argument
110+
a.Mode = 0500
111+
a.Uid = uint32(os.Getuid())
112+
a.Gid = uint32(os.Getegid())
113+
114+
115+
reader := file.swarmApi.Retrieve(file.key)
116+
quitC := make(chan bool)
117+
size, err := reader.Size(quitC)
118+
if err != nil {
119+
log.Warn("Couldnt file size of file %s : %v", file.path, err)
120+
a.Size = uint64(0)
121+
}
122+
a.Size = uint64(size)
123+
file.fileSize = a.Size
124+
return nil
125+
}
126+
127+
var _ = fs.HandleReader(&File{})
128+
129+
func (file *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
130+
buf := make([]byte, req.Size)
131+
reader := file.swarmApi.Retrieve(file.key)
132+
n, err := reader.ReadAt(buf, req.Offset)
133+
if err == io.ErrUnexpectedEOF || err == io.EOF {
134+
err = nil
135+
}
136+
resp.Data = buf[:n]
137+
return err
138+
139+
}

‎swarm/api/swarmfs.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package api
18+
19+
import (
20+
"time"
21+
"sync"
22+
)
23+
24+
const (
25+
Swarmfs_Version = "0.1"
26+
mountTimeout = time.Second * 5
27+
maxFuseMounts = 5
28+
)
29+
30+
31+
type SwarmFS struct {
32+
swarmApi *Api
33+
activeMounts map[string]*MountInfo
34+
activeLock *sync.RWMutex
35+
}
36+
37+
38+
39+
func NewSwarmFS(api *Api) *SwarmFS {
40+
swarmfs := &SwarmFS{
41+
swarmApi: api,
42+
activeLock: &sync.RWMutex{},
43+
activeMounts: map[string]*MountInfo{},
44+
}
45+
return swarmfs
46+
}
47+
48+

0 commit comments

Comments
 (0)