seaweedfs/weed/command/mount_std.go

145 lines
3.4 KiB
Go
Raw Normal View History

// +build linux darwin
package command
import (
"fmt"
2017-03-06 05:04:46 +00:00
"os"
"runtime"
2017-03-10 02:28:07 +00:00
"path"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/chrislusf/seaweedfs/weed/filer"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/storage"
"github.com/chrislusf/seaweedfs/weed/util"
"golang.org/x/net/context"
)
func runMount(cmd *Command, args []string) bool {
fmt.Printf("This is SeaweedFS version %s %s %s\n", util.VERSION, runtime.GOOS, runtime.GOARCH)
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())
if err != nil {
glog.Fatal(err)
return false
}
2017-06-22 08:33:58 +00:00
util.OnInterrupt(func() {
2014-05-13 22:04:04 +00:00
fuse.Unmount(*mountOptions.dir)
c.Close()
})
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{}
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
}
var fileIdMap = make(map[uint64]filer.FileId)
type Dir struct {
2017-03-10 02:28:07 +00:00
Path string
DirentMap map[string]*fuse.Dirent
}
2017-03-10 02:28:07 +00:00
func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error {
2017-03-06 05:04:46 +00:00
attr.Mode = os.ModeDir | 0555
return nil
}
2017-03-10 02:28:07 +00:00
func (dir *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
2018-05-05 09:01:50 +00:00
if entry, err := filer.LookupDirectoryEntry(*mountOptions.filer, dir.Path, name); err == nil {
if !entry.Found {
return nil, fuse.ENOENT
}
if entry.FileId != "" {
return &File{FileId: filer.FileId(entry.FileId), Name: name}, nil
} else {
return &Dir{Path: path.Join(dir.Path, name)}, nil
2017-03-10 02:28:07 +00:00
}
}
2018-05-05 09:01:50 +00:00
2017-03-10 02:28:07 +00:00
return nil, fuse.ENOENT
}
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)
}
if dirs, e := filer.ListDirectories(*mountOptions.filer, dir.Path); e == nil {
for _, d := range dirs.Directories {
2018-05-05 09:01:50 +00:00
dirent := fuse.Dirent{Name: string(d), Type: fuse.DT_Dir}
2017-03-10 02:28:07 +00:00
ret = append(ret, dirent)
2018-05-05 09:01:50 +00:00
dir.DirentMap[string(d)] = &dirent
}
}
if files, e := filer.ListFiles(*mountOptions.filer, dir.Path, ""); e == nil {
for _, f := range files.Files {
if fileId, e := storage.ParseFileId(string(f.Id)); e == nil {
fileInode := uint64(fileId.VolumeId)<<48 + fileId.Key
2018-05-05 09:01:50 +00:00
dirent := fuse.Dirent{Name: f.Name, Type: fuse.DT_File}
2017-03-10 02:28:07 +00:00
ret = append(ret, dirent)
dir.DirentMap[f.Name] = &dirent
fileIdMap[fileInode] = f.Id
}
}
}
return ret, nil
}
2017-03-06 05:04:46 +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 {
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.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
}