seaweedfs/weed/filesys/file.go

130 lines
3 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"
"bazil.org/fuse"
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"
"github.com/chrislusf/seaweedfs/weed/filer2"
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.NodeSetattrer(&File{})
2018-05-08 08:59:43 +00:00
2018-05-06 05:47:16 +00:00
type File struct {
Chunks []*filer_pb.FileChunk
Name string
dir *Dir
wfs *WFS
attributes *filer_pb.FuseAttributes
2018-05-06 05:47:16 +00:00
}
func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
2018-05-23 10:08:46 +00:00
fullPath := filepath.Join(file.dir.Path, file.Name)
item := file.wfs.listDirectoryEntriesCache.Get(fullPath)
if item != nil {
entry := item.Value().(*filer_pb.Entry)
file.Chunks = entry.Chunks
file.attributes = entry.Attributes
glog.V(1).Infof("file attr read cached %v attributes", file.Name)
} else {
err := file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
request := &filer_pb.GetEntryAttributesRequest{
Name: file.Name,
ParentDir: file.dir.Path,
}
2018-05-23 10:08:46 +00:00
resp, err := client.GetEntryAttributes(context, request)
if err != nil {
glog.V(0).Infof("file attr read file %v: %v", request, err)
return err
}
2018-05-23 10:08:46 +00:00
file.attributes = resp.Attributes
file.Chunks = resp.Chunks
2018-05-08 08:59:43 +00:00
2018-05-23 10:08:46 +00:00
glog.V(1).Infof("file attr %v %+v: %d", fullPath, file.attributes, filer2.TotalSize(file.Chunks))
2018-05-23 10:08:46 +00:00
return nil
})
2018-05-23 10:08:46 +00:00
if err != nil {
return err
2018-05-08 08:59:43 +00:00
}
}
2018-05-08 08:59:43 +00:00
attr.Mode = os.FileMode(file.attributes.FileMode)
attr.Size = filer2.TotalSize(file.Chunks)
attr.Mtime = time.Unix(file.attributes.Mtime, 0)
attr.Gid = file.attributes.Gid
attr.Uid = file.attributes.Uid
2018-05-19 20:51:44 +00:00
return nil
2018-05-08 08:59:43 +00:00
}
func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
2018-05-23 10:08:46 +00:00
fullPath := filepath.Join(file.dir.Path, file.Name)
glog.V(3).Infof("%v file open %+v", fullPath, req)
2018-05-23 10:08:46 +00:00
return &FileHandle{
2018-05-25 07:57:25 +00:00
f: file,
RequestId: req.Header.ID,
NodeId: req.Header.Node,
Uid: req.Uid,
Gid: req.Gid,
2018-05-23 10:08:46 +00:00
}, nil
}
func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
fullPath := filepath.Join(file.dir.Path, file.Name)
glog.V(3).Infof("%v file setattr %+v", fullPath, req)
if req.Valid.Size() {
2018-05-25 06:20:12 +00:00
glog.V(3).Infof("%v file setattr set size=%v", fullPath, req.Size)
if req.Size == 0 {
2018-05-25 06:20:12 +00:00
// fmt.Printf("truncate %v \n", fullPath)
file.Chunks = nil
}
file.attributes.FileSize = req.Size
}
if req.Valid.Mode() {
file.attributes.FileMode = uint32(req.Mode)
}
if req.Valid.Uid() {
file.attributes.Uid = req.Uid
}
if req.Valid.Gid() {
file.attributes.Gid = req.Gid
}
if req.Valid.Mtime() {
file.attributes.Mtime = req.Mtime.Unix()
}
return nil
}
func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
// fsync works at OS level
// write the file chunks to the filer
glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
2018-05-19 20:51:44 +00:00
2018-05-08 08:59:43 +00:00
return nil
2018-05-06 05:47:16 +00:00
}