2014-05-14 02:25:48 +08:00
|
|
|
// +build linux darwin
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-10-27 02:34:55 +08:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
|
2014-05-14 02:25:48 +08:00
|
|
|
"bazil.org/fuse"
|
|
|
|
"bazil.org/fuse/fs"
|
2015-04-17 03:18:06 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/go/filer"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/storage"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/util"
|
2015-02-06 15:11:27 +08:00
|
|
|
"golang.org/x/net/context"
|
2014-05-14 02:25:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func runMount(cmd *Command, args []string) bool {
|
2014-09-21 12:18:26 +08:00
|
|
|
fmt.Printf("This is Seaweed File System version %s %s %s\n", util.VERSION, runtime.GOOS, runtime.GOARCH)
|
2014-05-14 02:25:48 +08:00
|
|
|
if *mountOptions.dir == "" {
|
|
|
|
fmt.Printf("Please specify the mount directory via \"-dir\"")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := fuse.Mount(*mountOptions.dir)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatal(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-05-14 06:04:04 +08:00
|
|
|
OnInterrupt(func() {
|
|
|
|
fuse.Unmount(*mountOptions.dir)
|
|
|
|
c.Close()
|
|
|
|
})
|
2014-05-14 02:25:48 +08: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
|
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
FileId filer.FileId
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:09:29 +08:00
|
|
|
func (File) Attr(attr *fuse.Attr) {
|
2014-05-14 02:25:48 +08:00
|
|
|
}
|
2015-02-06 15:11:27 +08:00
|
|
|
func (File) ReadAll(ctx context.Context) ([]byte, error) {
|
2014-05-14 02:25:48 +08:00
|
|
|
return []byte("hello, world\n"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Dir struct {
|
2014-05-15 14:44:19 +08:00
|
|
|
Path string
|
|
|
|
Id uint64
|
2014-05-14 02:25:48 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 02:09:29 +08:00
|
|
|
func (dir Dir) Attr(attr *fuse.Attr) {
|
2014-05-14 02:25:48 +08:00
|
|
|
}
|
|
|
|
|
2015-02-06 15:11:27 +08:00
|
|
|
func (dir Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
2014-05-15 14:44:19 +08:00
|
|
|
files_result, e := filer.ListFiles(*mountOptions.filer, dir.Path, name)
|
2014-05-14 02:25:48 +08:00
|
|
|
if e != nil {
|
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
|
|
|
if len(files_result.Files) > 0 {
|
|
|
|
return File{files_result.Files[0].Id, files_result.Files[0].Name}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("File Not Found for %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
type WFS struct{}
|
|
|
|
|
2015-02-06 15:11:27 +08:00
|
|
|
func (WFS) Root() (fs.Node, error) {
|
2014-05-14 02:25:48 +08:00
|
|
|
return Dir{}, nil
|
|
|
|
}
|
|
|
|
|
2015-02-06 15:11:27 +08:00
|
|
|
func (dir *Dir) ReadDir(ctx context.Context) ([]fuse.Dirent, error) {
|
2015-03-10 15:20:31 +08:00
|
|
|
var ret []fuse.Dirent
|
2014-05-15 14:44:19 +08:00
|
|
|
if dirs, e := filer.ListDirectories(*mountOptions.filer, dir.Path); e == nil {
|
2014-05-14 02:25:48 +08:00
|
|
|
for _, d := range dirs.Directories {
|
|
|
|
dirId := uint64(d.Id)
|
|
|
|
ret = append(ret, fuse.Dirent{Inode: dirId, Name: d.Name, Type: fuse.DT_Dir})
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 14:44:19 +08:00
|
|
|
if files, e := filer.ListFiles(*mountOptions.filer, dir.Path, ""); e == nil {
|
2014-05-14 02:25:48 +08:00
|
|
|
for _, f := range files.Files {
|
|
|
|
if fileId, e := storage.ParseFileId(string(f.Id)); e == nil {
|
|
|
|
fileInode := uint64(fileId.VolumeId)<<48 + fileId.Key
|
|
|
|
ret = append(ret, fuse.Dirent{Inode: fileInode, Name: f.Name, Type: fuse.DT_File})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|