2014-05-13 18:25:48 +00:00
|
|
|
// +build linux darwin
|
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
package command
|
2014-05-13 18:25:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-10-26 18:34:55 +00:00
|
|
|
"fmt"
|
2017-03-06 05:04:46 +00:00
|
|
|
"os"
|
2014-10-26 18:34:55 +00:00
|
|
|
"runtime"
|
|
|
|
|
2017-03-10 02:28:07 +00:00
|
|
|
"path"
|
|
|
|
|
2014-05-13 18:25:48 +00:00
|
|
|
"bazil.org/fuse"
|
|
|
|
"bazil.org/fuse/fs"
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2015-02-06 07:11:27 +00:00
|
|
|
"golang.org/x/net/context"
|
2014-05-13 18:25:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func runMount(cmd *Command, args []string) bool {
|
2015-04-16 21:11:25 +00:00
|
|
|
fmt.Printf("This is SeaweedFS version %s %s %s\n", util.VERSION, runtime.GOOS, runtime.GOARCH)
|
2014-05-13 18:25:48 +00:00
|
|
|
if *mountOptions.dir == "" {
|
|
|
|
fmt.Printf("Please specify the mount directory via \"-dir\"")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:28:07 +00:00
|
|
|
c, err := fuse.Mount(*mountOptions.dir, fuse.LocalVolume())
|
2014-05-13 18:25:48 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatal(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-05-13 22:04:04 +00:00
|
|
|
OnInterrupt(func() {
|
|
|
|
fuse.Unmount(*mountOptions.dir)
|
|
|
|
c.Close()
|
|
|
|
})
|
2014-05-13 18:25:48 +00:00
|
|
|
|
|
|
|
err = fs.Serve(c, WFS{})
|
|
|
|
if err != nil {
|
|
|
|
fuse.Unmount(*mountOptions.dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the mount process has an error to report
|
|
|
|
<-c.Ready
|
|
|
|
if err := c.MountError; err != nil {
|
|
|
|
glog.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-03-06 05:04:46 +00:00
|
|
|
type WFS struct{}
|
2014-05-13 18:25:48 +00:00
|
|
|
|
2017-03-06 05:04:46 +00:00
|
|
|
func (WFS) Root() (fs.Node, error) {
|
2017-03-10 02:28:07 +00:00
|
|
|
return &Dir{Path: "/"}, nil
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 07:02:27 +00:00
|
|
|
var fileIdMap = make(map[uint64]filer.FileId)
|
|
|
|
|
2014-05-13 18:25:48 +00:00
|
|
|
type Dir struct {
|
2017-03-10 02:28:07 +00:00
|
|
|
Id uint64
|
|
|
|
Path string
|
|
|
|
DirentMap map[string]*fuse.Dirent
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 02:28:07 +00:00
|
|
|
func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error {
|
|
|
|
attr.Inode = dir.Id
|
2017-03-06 05:04:46 +00:00
|
|
|
attr.Mode = os.ModeDir | 0555
|
2015-05-30 20:28:39 +00:00
|
|
|
return nil
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 02:28:07 +00:00
|
|
|
func (dir *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
|
|
|
if dirent, ok := dir.DirentMap[name]; ok {
|
|
|
|
if dirent.Type == fuse.DT_File {
|
2017-03-17 07:02:27 +00:00
|
|
|
return &File{Id: dirent.Inode, FileId: fileIdMap[dirent.Inode], Name: dirent.Name}, nil
|
2017-03-10 02:28:07 +00:00
|
|
|
}
|
|
|
|
return &Dir{
|
|
|
|
Id: dirent.Inode,
|
|
|
|
Path: path.Join(dir.Path, dirent.Name),
|
|
|
|
}, nil
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
2017-03-10 02:28:07 +00:00
|
|
|
return nil, fuse.ENOENT
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 02:28:07 +00:00
|
|
|
func (dir *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
|
2015-03-10 07:20:31 +00:00
|
|
|
var ret []fuse.Dirent
|
2017-03-10 02:28:07 +00:00
|
|
|
if dir.DirentMap == nil {
|
|
|
|
dir.DirentMap = make(map[string]*fuse.Dirent)
|
|
|
|
}
|
2014-05-15 06:44:19 +00:00
|
|
|
if dirs, e := filer.ListDirectories(*mountOptions.filer, dir.Path); e == nil {
|
2014-05-13 18:25:48 +00:00
|
|
|
for _, d := range dirs.Directories {
|
|
|
|
dirId := uint64(d.Id)
|
2017-03-10 02:28:07 +00:00
|
|
|
dirent := fuse.Dirent{Inode: dirId, Name: d.Name, Type: fuse.DT_Dir}
|
|
|
|
ret = append(ret, dirent)
|
|
|
|
dir.DirentMap[d.Name] = &dirent
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-15 06:44:19 +00:00
|
|
|
if files, e := filer.ListFiles(*mountOptions.filer, dir.Path, ""); e == nil {
|
2014-05-13 18:25:48 +00:00
|
|
|
for _, f := range files.Files {
|
|
|
|
if fileId, e := storage.ParseFileId(string(f.Id)); e == nil {
|
|
|
|
fileInode := uint64(fileId.VolumeId)<<48 + fileId.Key
|
2017-03-10 02:28:07 +00:00
|
|
|
dirent := fuse.Dirent{Inode: fileInode, Name: f.Name, Type: fuse.DT_File}
|
|
|
|
ret = append(ret, dirent)
|
|
|
|
dir.DirentMap[f.Name] = &dirent
|
2017-03-17 07:02:27 +00:00
|
|
|
fileIdMap[fileInode] = f.Id
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
2017-03-06 05:04:46 +00:00
|
|
|
|
2017-03-17 07:02:27 +00:00
|
|
|
func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
|
|
|
|
name := path.Join(dir.Path, req.Name)
|
|
|
|
err := filer.DeleteDirectoryOrFile(*mountOptions.filer, name, req.Dir)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Delete file %s [ERROR] %s\n", name, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-06 05:04:46 +00:00
|
|
|
type File struct {
|
2017-03-17 07:02:27 +00:00
|
|
|
Id uint64
|
|
|
|
FileId filer.FileId
|
|
|
|
Name string
|
2017-03-06 05:04:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 02:28:07 +00:00
|
|
|
func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
|
|
|
|
attr.Inode = file.Id
|
2017-03-17 07:02:27 +00:00
|
|
|
attr.Mode = 0444
|
|
|
|
ret, err := filer.GetFileSize(*mountOptions.filer, string(file.FileId))
|
|
|
|
if err == nil {
|
|
|
|
attr.Size = ret.Size
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Get file %s attr [ERROR] %s\n", file.Name, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *File) ReadAll(ctx context.Context) ([]byte, error) {
|
|
|
|
ret, err := filer.GetFileContent(*mountOptions.filer, string(file.FileId))
|
|
|
|
if err == nil {
|
|
|
|
return ret.Content, nil
|
|
|
|
}
|
|
|
|
fmt.Printf("Get file %s content [ERROR] %s\n", file.Name, err)
|
|
|
|
return nil, err
|
2017-03-06 05:04:46 +00:00
|
|
|
}
|