refactoring fuse

This commit is contained in:
Chris Lu
2018-05-05 22:47:16 -07:00
parent 2cada598c6
commit 456738ba64
4 changed files with 117 additions and 100 deletions

62
weed/filesys/dir.go Normal file
View File

@@ -0,0 +1,62 @@
package filesys
import (
"bazil.org/fuse/fs"
"bazil.org/fuse"
"context"
"os"
"fmt"
"path"
"github.com/chrislusf/seaweedfs/weed/filer"
)
type Dir struct {
Path string
wfs *WFS
}
func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error {
attr.Mode = os.ModeDir | 0555
return nil
}
func (dir *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
if entry, err := filer.LookupDirectoryEntry(dir.wfs.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, wfs: dir.wfs}, nil
} else {
return &Dir{Path: path.Join(dir.Path, name), wfs: dir.wfs}, nil
}
}
return nil, fuse.ENOENT
}
func (dir *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
var ret []fuse.Dirent
if dirs, e := filer.ListDirectories(dir.wfs.filer, dir.Path); e == nil {
for _, d := range dirs.Directories {
dirent := fuse.Dirent{Name: string(d), Type: fuse.DT_Dir}
ret = append(ret, dirent)
}
}
if files, e := filer.ListFiles(dir.wfs.filer, dir.Path, ""); e == nil {
for _, f := range files.Files {
dirent := fuse.Dirent{Name: f.Name, Type: fuse.DT_File}
ret = append(ret, dirent)
}
}
return ret, nil
}
func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
name := path.Join(dir.Path, req.Name)
err := filer.DeleteDirectoryOrFile(dir.wfs.filer, name, req.Dir)
if err != nil {
fmt.Printf("Delete file %s [ERROR] %s\n", name, err)
}
return err
}

34
weed/filesys/file.go Normal file
View File

@@ -0,0 +1,34 @@
package filesys
import (
"bazil.org/fuse"
"fmt"
"github.com/chrislusf/seaweedfs/weed/filer"
"context"
)
type File struct {
FileId filer.FileId
Name string
wfs *WFS
}
func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
attr.Mode = 0444
ret, err := filer.GetFileSize(file.wfs.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(file.wfs.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
}

17
weed/filesys/wfs.go Normal file
View File

@@ -0,0 +1,17 @@
package filesys
import "bazil.org/fuse/fs"
type WFS struct {
filer string
}
func NewSeaweedFileSystem(filer string) *WFS {
return &WFS{
filer: filer,
}
}
func (wfs *WFS) Root() (fs.Node, error) {
return &Dir{Path: "/", wfs: wfs}, nil
}