2017-03-23 16:56:06 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
// +build linux darwin freebsd
|
2017-03-23 16:56:06 +03:00
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-03-31 13:11:01 +03:00
|
|
|
"errors"
|
2017-03-23 16:56:06 +03:00
|
|
|
"fmt"
|
2017-03-31 13:11:01 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-23 16:56:06 +03:00
|
|
|
"strings"
|
2017-03-31 13:11:01 +03:00
|
|
|
"sync"
|
2017-03-23 16:56:06 +03:00
|
|
|
"time"
|
2017-03-31 13:11:01 +03:00
|
|
|
|
2017-03-23 16:56:06 +03:00
|
|
|
"bazil.org/fuse"
|
|
|
|
"bazil.org/fuse/fs"
|
2017-03-31 13:11:01 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
2017-03-23 16:56:06 +03:00
|
|
|
)
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
var (
|
|
|
|
inode uint64 = 1
|
|
|
|
inodeLock sync.RWMutex
|
|
|
|
)
|
2017-03-23 16:56:06 +03:00
|
|
|
|
|
|
|
var (
|
2017-03-31 13:11:01 +03:00
|
|
|
errEmptyMountPoint = errors.New("need non-empty mount point")
|
|
|
|
errMaxMountCount = errors.New("max FUSE mount count reached")
|
|
|
|
errMountTimeout = errors.New("mount timeout")
|
2017-03-23 16:56:06 +03:00
|
|
|
)
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
func isFUSEUnsupportedError(err error) bool {
|
|
|
|
if perr, ok := err.(*os.PathError); ok {
|
|
|
|
return perr.Op == "open" && perr.Path == "/dev/fuse"
|
|
|
|
}
|
|
|
|
return err == fuse.ErrOSXFUSENotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountInfo contains information about every active mount
|
2017-03-23 16:56:06 +03:00
|
|
|
type MountInfo struct {
|
2017-03-31 13:11:01 +03:00
|
|
|
MountPoint string
|
|
|
|
ManifestHash string
|
2017-03-23 16:56:06 +03:00
|
|
|
resolvedKey storage.Key
|
|
|
|
rootDir *Dir
|
|
|
|
fuseConnection *fuse.Conn
|
|
|
|
}
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
// newInode creates a new inode number.
|
2017-03-23 16:56:06 +03:00
|
|
|
// Inode numbers need to be unique, they are used for caching inside fuse
|
2017-03-31 13:11:01 +03:00
|
|
|
func newInode() uint64 {
|
2017-03-23 16:56:06 +03:00
|
|
|
inodeLock.Lock()
|
2017-03-31 13:11:01 +03:00
|
|
|
defer inodeLock.Unlock()
|
2017-03-23 16:56:06 +03:00
|
|
|
inode += 1
|
|
|
|
return inode
|
|
|
|
}
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
func (self *SwarmFS) Mount(mhash, mountpoint string) (*MountInfo, error) {
|
|
|
|
if mountpoint == "" {
|
|
|
|
return nil, errEmptyMountPoint
|
|
|
|
}
|
|
|
|
cleanedMountPoint, err := filepath.Abs(filepath.Clean(mountpoint))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-23 16:56:06 +03:00
|
|
|
|
|
|
|
self.activeLock.Lock()
|
|
|
|
defer self.activeLock.Unlock()
|
|
|
|
|
|
|
|
noOfActiveMounts := len(self.activeMounts)
|
|
|
|
if noOfActiveMounts >= maxFuseMounts {
|
2017-03-31 13:11:01 +03:00
|
|
|
return nil, errMaxMountCount
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := self.activeMounts[cleanedMountPoint]; ok {
|
2017-03-31 13:11:01 +03:00
|
|
|
return nil, fmt.Errorf("%s is already mounted", cleanedMountPoint)
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
2017-04-07 01:22:22 +03:00
|
|
|
uri, err := Parse("bzz:/" + mhash)
|
2017-03-23 16:56:06 +03:00
|
|
|
if err != nil {
|
2017-04-07 01:22:22 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
key, err := self.swarmApi.Resolve(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
2017-04-07 01:22:22 +03:00
|
|
|
path := uri.Path
|
2017-03-23 16:56:06 +03:00
|
|
|
if len(path) > 0 {
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
quitC := make(chan bool)
|
|
|
|
trie, err := loadManifest(self.swarmApi.dpa, key, quitC)
|
|
|
|
if err != nil {
|
2017-03-31 13:11:01 +03:00
|
|
|
return nil, fmt.Errorf("can't load manifest %v: %v", key.String(), err)
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dirTree := map[string]*Dir{}
|
|
|
|
|
|
|
|
rootDir := &Dir{
|
2017-03-31 13:11:01 +03:00
|
|
|
inode: newInode(),
|
2017-03-23 16:56:06 +03:00
|
|
|
name: "root",
|
|
|
|
directories: nil,
|
|
|
|
files: nil,
|
|
|
|
}
|
|
|
|
dirTree["root"] = rootDir
|
|
|
|
|
|
|
|
err = trie.listWithPrefix(path, quitC, func(entry *manifestTrieEntry, suffix string) {
|
|
|
|
key = common.Hex2Bytes(entry.Hash)
|
|
|
|
fullpath := "/" + suffix
|
|
|
|
basepath := filepath.Dir(fullpath)
|
|
|
|
filename := filepath.Base(fullpath)
|
|
|
|
|
|
|
|
parentDir := rootDir
|
|
|
|
dirUntilNow := ""
|
|
|
|
paths := strings.Split(basepath, "/")
|
|
|
|
for i := range paths {
|
|
|
|
if paths[i] != "" {
|
|
|
|
thisDir := paths[i]
|
|
|
|
dirUntilNow = dirUntilNow + "/" + thisDir
|
|
|
|
|
|
|
|
if _, ok := dirTree[dirUntilNow]; !ok {
|
|
|
|
dirTree[dirUntilNow] = &Dir{
|
2017-03-31 13:11:01 +03:00
|
|
|
inode: newInode(),
|
2017-03-23 16:56:06 +03:00
|
|
|
name: thisDir,
|
|
|
|
path: dirUntilNow,
|
|
|
|
directories: nil,
|
|
|
|
files: nil,
|
|
|
|
}
|
|
|
|
parentDir.directories = append(parentDir.directories, dirTree[dirUntilNow])
|
|
|
|
parentDir = dirTree[dirUntilNow]
|
|
|
|
|
|
|
|
} else {
|
|
|
|
parentDir = dirTree[dirUntilNow]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thisFile := &File{
|
2017-03-31 13:11:01 +03:00
|
|
|
inode: newInode(),
|
2017-03-23 16:56:06 +03:00
|
|
|
name: filename,
|
|
|
|
path: fullpath,
|
|
|
|
key: key,
|
|
|
|
swarmApi: self.swarmApi,
|
|
|
|
}
|
|
|
|
parentDir.files = append(parentDir.files, thisFile)
|
|
|
|
})
|
|
|
|
|
|
|
|
fconn, err := fuse.Mount(cleanedMountPoint, fuse.FSName("swarmfs"), fuse.VolumeName(mhash))
|
|
|
|
if err != nil {
|
|
|
|
fuse.Unmount(cleanedMountPoint)
|
2017-03-31 13:11:01 +03:00
|
|
|
log.Warn("Error mounting swarm manifest", "mountpoint", cleanedMountPoint, "err", err)
|
|
|
|
return nil, err
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mounterr := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
filesys := &FS{root: rootDir}
|
|
|
|
if err := fs.Serve(fconn, filesys); err != nil {
|
2017-03-31 13:11:01 +03:00
|
|
|
mounterr <- err
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Check if the mount process has an error to report.
|
|
|
|
select {
|
|
|
|
case <-time.After(mountTimeout):
|
2017-03-31 13:11:01 +03:00
|
|
|
fuse.Unmount(cleanedMountPoint)
|
|
|
|
return nil, errMountTimeout
|
2017-03-23 16:56:06 +03:00
|
|
|
|
|
|
|
case err := <-mounterr:
|
2017-03-31 13:11:01 +03:00
|
|
|
log.Warn("Error serving swarm FUSE FS", "mountpoint", cleanedMountPoint, "err", err)
|
|
|
|
return nil, err
|
2017-03-23 16:56:06 +03:00
|
|
|
|
|
|
|
case <-fconn.Ready:
|
2017-03-31 13:11:01 +03:00
|
|
|
log.Info("Now serving swarm FUSE FS", "manifest", mhash, "mountpoint", cleanedMountPoint)
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
// Assemble and Store the mount information for future use
|
|
|
|
mi := &MountInfo{
|
|
|
|
MountPoint: cleanedMountPoint,
|
|
|
|
ManifestHash: mhash,
|
2017-03-23 16:56:06 +03:00
|
|
|
resolvedKey: key,
|
|
|
|
rootDir: rootDir,
|
|
|
|
fuseConnection: fconn,
|
|
|
|
}
|
2017-03-31 13:11:01 +03:00
|
|
|
self.activeMounts[cleanedMountPoint] = mi
|
|
|
|
return mi, nil
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
func (self *SwarmFS) Unmount(mountpoint string) (bool, error) {
|
2017-03-23 16:56:06 +03:00
|
|
|
self.activeLock.Lock()
|
|
|
|
defer self.activeLock.Unlock()
|
|
|
|
|
|
|
|
cleanedMountPoint, err := filepath.Abs(filepath.Clean(mountpoint))
|
|
|
|
if err != nil {
|
2017-03-31 13:11:01 +03:00
|
|
|
return false, err
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mountInfo := self.activeMounts[cleanedMountPoint]
|
2017-03-31 13:11:01 +03:00
|
|
|
if mountInfo == nil || mountInfo.MountPoint != cleanedMountPoint {
|
|
|
|
return false, fmt.Errorf("%s is not mounted", cleanedMountPoint)
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
err = fuse.Unmount(cleanedMountPoint)
|
|
|
|
if err != nil {
|
2017-03-31 13:11:01 +03:00
|
|
|
// TODO(jmozah): try forceful unmount if normal unmount fails
|
|
|
|
return false, err
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
// remove the mount information from the active map
|
2017-03-23 16:56:06 +03:00
|
|
|
mountInfo.fuseConnection.Close()
|
|
|
|
delete(self.activeMounts, cleanedMountPoint)
|
2017-03-31 13:11:01 +03:00
|
|
|
return true, nil
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
func (self *SwarmFS) Listmounts() []*MountInfo {
|
2017-03-23 16:56:06 +03:00
|
|
|
self.activeLock.RLock()
|
|
|
|
defer self.activeLock.RUnlock()
|
|
|
|
|
2017-03-31 13:11:01 +03:00
|
|
|
rows := make([]*MountInfo, 0, len(self.activeMounts))
|
|
|
|
for _, mi := range self.activeMounts {
|
|
|
|
rows = append(rows, mi)
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
2017-03-31 13:11:01 +03:00
|
|
|
return rows
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SwarmFS) Stop() bool {
|
|
|
|
for mp := range self.activeMounts {
|
|
|
|
mountInfo := self.activeMounts[mp]
|
2017-03-31 13:11:01 +03:00
|
|
|
self.Unmount(mountInfo.MountPoint)
|
2017-03-23 16:56:06 +03:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|