2018-05-06 13:47:16 +08:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
2018-05-06 14:39:29 +08:00
|
|
|
"context"
|
2018-05-06 13:47:16 +08:00
|
|
|
"fmt"
|
2018-05-06 14:39:29 +08:00
|
|
|
|
|
|
|
"bazil.org/fuse"
|
2018-05-06 13:47:16 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2018-05-08 16:59:43 +08:00
|
|
|
"bazil.org/fuse/fs"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-05-10 14:18:02 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-05-06 13:47:16 +08:00
|
|
|
)
|
|
|
|
|
2018-05-08 16:59:43 +08:00
|
|
|
var _ = fs.Node(&File{})
|
|
|
|
// var _ = fs.NodeOpener(&File{})
|
|
|
|
// var _ = fs.NodeFsyncer(&File{})
|
|
|
|
var _ = fs.Handle(&File{})
|
|
|
|
var _ = fs.HandleReadAller(&File{})
|
|
|
|
// var _ = fs.HandleReader(&File{})
|
|
|
|
var _ = fs.HandleWriter(&File{})
|
|
|
|
|
2018-05-06 13:47:16 +08:00
|
|
|
type File struct {
|
|
|
|
FileId filer.FileId
|
|
|
|
Name string
|
|
|
|
wfs *WFS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
|
|
|
|
attr.Mode = 0444
|
2018-05-10 14:18:02 +08:00
|
|
|
return file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-05-08 16:59:43 +08:00
|
|
|
|
2018-05-10 14:18:02 +08:00
|
|
|
request := &filer_pb.GetFileAttributesRequest{
|
2018-05-08 16:59:43 +08:00
|
|
|
Name: file.Name,
|
|
|
|
ParentDir: "", //TODO add parent folder
|
|
|
|
FileId: string(file.FileId),
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("read file size: %v", request)
|
|
|
|
resp, err := client.GetFileAttributes(context, request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
attr.Size = resp.Attributes.FileSize
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *File) ReadAll(ctx context.Context) (content []byte, err error) {
|
|
|
|
|
2018-05-10 14:18:02 +08:00
|
|
|
err = file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-05-08 16:59:43 +08:00
|
|
|
|
2018-05-10 14:18:02 +08:00
|
|
|
request := &filer_pb.GetFileContentRequest{
|
2018-05-08 16:59:43 +08:00
|
|
|
FileId: string(file.FileId),
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("read file content: %v", request)
|
|
|
|
resp, err := client.GetFileContent(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = resp.Content
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return content, err
|
2018-05-06 13:47:16 +08:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:59:43 +08:00
|
|
|
func (file *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
|
|
|
|
fmt.Printf("write file %+v\n", req)
|
|
|
|
return nil
|
2018-05-06 13:47:16 +08:00
|
|
|
}
|