| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | // 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 12:11:01 +02:00
										 |  |  | // +build linux darwin freebsd | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | package fuse | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2017-05-30 11:21:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"bazil.org/fuse" | 
					
						
							|  |  |  | 	"bazil.org/fuse/fs" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/log" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/swarm/api" | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	errEmptyMountPoint = errors.New("need non-empty mount point") | 
					
						
							|  |  |  | 	errMaxMountCount   = errors.New("max FUSE mount count reached") | 
					
						
							|  |  |  | 	errMountTimeout    = errors.New("mount timeout") | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	errAlreadyMounted  = errors.New("mount point is already serving") | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02: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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | // information about every active mount | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | type MountInfo struct { | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	MountPoint     string | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	StartManifest  string | 
					
						
							|  |  |  | 	LatestManifest string | 
					
						
							|  |  |  | 	rootDir        *SwarmDir | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	fuseConnection *fuse.Conn | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	swarmApi       *api.Api | 
					
						
							|  |  |  | 	lock           *sync.RWMutex | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | func NewMountInfo(mhash, mpoint string, sapi *api.Api) *MountInfo { | 
					
						
							|  |  |  | 	newMountInfo := &MountInfo{ | 
					
						
							|  |  |  | 		MountPoint:     mpoint, | 
					
						
							|  |  |  | 		StartManifest:  mhash, | 
					
						
							|  |  |  | 		LatestManifest: mhash, | 
					
						
							|  |  |  | 		rootDir:        nil, | 
					
						
							|  |  |  | 		fuseConnection: nil, | 
					
						
							|  |  |  | 		swarmApi:       sapi, | 
					
						
							|  |  |  | 		lock:           &sync.RWMutex{}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return newMountInfo | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | func (self *SwarmFS) Mount(mhash, mountpoint string) (*MountInfo, error) { | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	if mountpoint == "" { | 
					
						
							|  |  |  | 		return nil, errEmptyMountPoint | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	cleanedMountPoint, err := filepath.Abs(filepath.Clean(mountpoint)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	self.swarmFsLock.Lock() | 
					
						
							|  |  |  | 	defer self.swarmFsLock.Unlock() | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	noOfActiveMounts := len(self.activeMounts) | 
					
						
							|  |  |  | 	if noOfActiveMounts >= maxFuseMounts { | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 		return nil, errMaxMountCount | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if _, ok := self.activeMounts[cleanedMountPoint]; ok { | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 		return nil, errAlreadyMounted | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	log.Info(fmt.Sprintf("Attempting to mount %s ", cleanedMountPoint)) | 
					
						
							| 
									
										
										
										
											2017-08-08 20:34:35 +03:00
										 |  |  | 	_, manifestEntryMap, err := self.swarmApi.BuildDirectoryTree(mhash, true) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	mi := NewMountInfo(mhash, cleanedMountPoint, self.swarmApi) | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	dirTree := map[string]*SwarmDir{} | 
					
						
							|  |  |  | 	rootDir := NewSwarmDir("/", mi) | 
					
						
							|  |  |  | 	dirTree["/"] = rootDir | 
					
						
							|  |  |  | 	mi.rootDir = rootDir | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	for suffix, entry := range manifestEntryMap { | 
					
						
							| 
									
										
										
										
											2017-08-08 20:34:35 +03:00
										 |  |  | 		key := common.Hex2Bytes(entry.Hash) | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 		fullpath := "/" + suffix | 
					
						
							|  |  |  | 		basepath := filepath.Dir(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 { | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 					dirTree[dirUntilNow] = NewSwarmDir(dirUntilNow, mi) | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 					parentDir.directories = append(parentDir.directories, dirTree[dirUntilNow]) | 
					
						
							|  |  |  | 					parentDir = dirTree[dirUntilNow] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					parentDir = dirTree[dirUntilNow] | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 		thisFile := NewSwarmFile(basepath, filepath.Base(fullpath), mi) | 
					
						
							|  |  |  | 		thisFile.key = key | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 		parentDir.files = append(parentDir.files, thisFile) | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	fconn, err := fuse.Mount(cleanedMountPoint, fuse.FSName("swarmfs"), fuse.VolumeName(mhash)) | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	if isFUSEUnsupportedError(err) { | 
					
						
							|  |  |  | 		log.Warn("Fuse not installed", "mountpoint", cleanedMountPoint, "err", err) | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} else if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 		fuse.Unmount(cleanedMountPoint) | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 		log.Warn("Error mounting swarm manifest", "mountpoint", cleanedMountPoint, "err", err) | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	mi.fuseConnection = fconn | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	serverr := make(chan error, 1) | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 		log.Info(fmt.Sprintf("Serving %s at %s", mhash, cleanedMountPoint)) | 
					
						
							|  |  |  | 		filesys := &SwarmRoot{root: rootDir} | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 		if err := fs.Serve(fconn, filesys); err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 			log.Warn(fmt.Sprintf("Could not Serve SwarmFileSystem error: %v", err)) | 
					
						
							|  |  |  | 			serverr <- err | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check if the mount process has an error to report. | 
					
						
							|  |  |  | 	select { | 
					
						
							|  |  |  | 	case <-time.After(mountTimeout): | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 		fuse.Unmount(cleanedMountPoint) | 
					
						
							|  |  |  | 		return nil, errMountTimeout | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	case err := <-serverr: | 
					
						
							|  |  |  | 		fuse.Unmount(cleanedMountPoint) | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 		log.Warn("Error serving swarm FUSE FS", "mountpoint", cleanedMountPoint, "err", err) | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	case <-fconn.Ready: | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 		log.Info("Now serving swarm FUSE FS", "manifest", mhash, "mountpoint", cleanedMountPoint) | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	self.activeMounts[cleanedMountPoint] = mi | 
					
						
							|  |  |  | 	return mi, nil | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | func (self *SwarmFS) Unmount(mountpoint string) (*MountInfo, error) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	self.swarmFsLock.Lock() | 
					
						
							|  |  |  | 	defer self.swarmFsLock.Unlock() | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	cleanedMountPoint, err := filepath.Abs(filepath.Clean(mountpoint)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mountInfo := self.activeMounts[cleanedMountPoint] | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	if mountInfo == nil || mountInfo.MountPoint != cleanedMountPoint { | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 		return nil, fmt.Errorf("%s is not mounted", cleanedMountPoint) | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 	err = fuse.Unmount(cleanedMountPoint) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-05-30 11:21:59 +02:00
										 |  |  | 		err1 := externalUnmount(cleanedMountPoint) | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 		if err1 != nil { | 
					
						
							|  |  |  | 			errStr := fmt.Sprintf("UnMount error: %v", err) | 
					
						
							|  |  |  | 			log.Warn(errStr) | 
					
						
							|  |  |  | 			return nil, err1 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mountInfo.fuseConnection.Close() | 
					
						
							|  |  |  | 	delete(self.activeMounts, cleanedMountPoint) | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	succString := fmt.Sprintf("UnMounting %v succeeded", cleanedMountPoint) | 
					
						
							|  |  |  | 	log.Info(succString) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return mountInfo, nil | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | func (self *SwarmFS) Listmounts() []*MountInfo { | 
					
						
							| 
									
										
										
										
											2017-04-12 05:36:02 +05:30
										 |  |  | 	self.swarmFsLock.RLock() | 
					
						
							|  |  |  | 	defer self.swarmFsLock.RUnlock() | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	rows := make([]*MountInfo, 0, len(self.activeMounts)) | 
					
						
							|  |  |  | 	for _, mi := range self.activeMounts { | 
					
						
							|  |  |  | 		rows = append(rows, mi) | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 	return rows | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (self *SwarmFS) Stop() bool { | 
					
						
							|  |  |  | 	for mp := range self.activeMounts { | 
					
						
							|  |  |  | 		mountInfo := self.activeMounts[mp] | 
					
						
							| 
									
										
										
										
											2017-03-31 12:11:01 +02:00
										 |  |  | 		self.Unmount(mountInfo.MountPoint) | 
					
						
							| 
									
										
										
										
											2017-03-23 19:26:06 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } |