seaweedfs/weed/filesys/file.go

98 lines
2.1 KiB
Go
Raw Normal View History

2018-05-06 05:47:16 +00:00
package filesys
import (
2018-05-06 06:39:29 +00:00
"context"
2018-05-06 05:47:16 +00:00
"fmt"
2018-05-06 06:39:29 +00:00
"bazil.org/fuse"
2018-05-06 05:47:16 +00:00
"github.com/chrislusf/seaweedfs/weed/filer"
2018-05-08 08:59:43 +00:00
"bazil.org/fuse/fs"
"github.com/chrislusf/seaweedfs/weed/glog"
2018-05-10 06:18:02 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"path/filepath"
"os"
"time"
2018-05-06 05:47:16 +00:00
)
2018-05-08 08:59:43 +00: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 05:47:16 +00:00
type File struct {
FileId filer.FileId
Name string
dir *Dir
2018-05-06 05:47:16 +00:00
wfs *WFS
}
func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
fullPath := filepath.Join(file.dir.Path, file.Name)
item := file.wfs.listDirectoryEntriesCache.Get(fullPath)
var attributes *filer_pb.FuseAttributes
if item != nil {
attributes = item.Value().(*filer_pb.FuseAttributes)
glog.V(1).Infof("read cached file %v attributes", file.Name)
} else {
err := file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
request := &filer_pb.GetFileAttributesRequest{
Name: file.Name,
ParentDir: file.dir.Path,
}
glog.V(1).Infof("read file size: %v", request)
resp, err := client.GetFileAttributes(context, request)
if err != nil {
return err
}
attributes = resp.Attributes
return nil
})
2018-05-08 08:59:43 +00:00
if err != nil {
return err
}
}
2018-05-08 08:59:43 +00:00
attr.Mode = os.FileMode(attributes.FileMode)
attr.Size = attributes.FileSize
attr.Mtime = time.Unix(attributes.Mtime, 0)
attr.Gid = attributes.Gid
attr.Uid = attributes.Uid
return nil
2018-05-08 08:59:43 +00:00
}
func (file *File) ReadAll(ctx context.Context) (content []byte, err error) {
2018-05-10 06:18:02 +00:00
err = file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
2018-05-08 08:59:43 +00:00
2018-05-10 06:18:02 +00:00
request := &filer_pb.GetFileContentRequest{
2018-05-08 08:59:43 +00: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 05:47:16 +00:00
}
2018-05-08 08:59:43 +00: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 05:47:16 +00:00
}