2018-05-06 13:47:16 +08:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
2018-05-06 14:39:29 +08:00
|
|
|
"bazil.org/fuse"
|
2018-05-08 16:59:43 +08:00
|
|
|
"bazil.org/fuse/fs"
|
2018-05-28 02:52:26 +08:00
|
|
|
"context"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
2018-05-08 16:59:43 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-05-10 14:18:02 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-05-14 17:02:17 +08:00
|
|
|
"os"
|
2018-05-28 02:52:26 +08:00
|
|
|
"path/filepath"
|
2018-05-14 17:02:17 +08:00
|
|
|
"time"
|
2018-05-06 13:47:16 +08:00
|
|
|
)
|
|
|
|
|
2018-05-08 16:59:43 +08:00
|
|
|
var _ = fs.Node(&File{})
|
2018-05-22 18:26:38 +08:00
|
|
|
var _ = fs.NodeOpener(&File{})
|
2018-05-16 15:54:27 +08:00
|
|
|
var _ = fs.NodeFsyncer(&File{})
|
2018-05-21 15:00:28 +08:00
|
|
|
var _ = fs.NodeSetattrer(&File{})
|
2018-05-08 16:59:43 +08:00
|
|
|
|
2018-05-06 13:47:16 +08:00
|
|
|
type File struct {
|
2018-05-22 18:26:38 +08:00
|
|
|
Chunks []*filer_pb.FileChunk
|
|
|
|
Name string
|
|
|
|
dir *Dir
|
|
|
|
wfs *WFS
|
|
|
|
attributes *filer_pb.FuseAttributes
|
2018-05-25 16:27:21 +08:00
|
|
|
isOpen bool
|
2018-05-06 13:47:16 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
func (file *File) fullpath() string {
|
|
|
|
return filepath.Join(file.dir.Path, file.Name)
|
|
|
|
}
|
2018-05-14 17:02:17 +08:00
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error {
|
2018-05-14 17:02:17 +08:00
|
|
|
|
2018-05-25 16:27:21 +08:00
|
|
|
if file.attributes == nil || !file.isOpen {
|
2018-06-06 17:09:57 +08:00
|
|
|
item := file.wfs.listDirectoryEntriesCache.Get(file.fullpath())
|
2018-06-07 13:48:51 +08:00
|
|
|
if item != nil && !item.Expired(){
|
2018-05-25 16:22:31 +08:00
|
|
|
entry := item.Value().(*filer_pb.Entry)
|
|
|
|
file.Chunks = entry.Chunks
|
|
|
|
file.attributes = entry.Attributes
|
2018-06-07 13:55:59 +08:00
|
|
|
// glog.V(1).Infof("file attr read cached %v attributes", file.Name)
|
2018-05-25 16:22:31 +08:00
|
|
|
} else {
|
|
|
|
err := file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
|
|
|
|
request := &filer_pb.GetEntryAttributesRequest{
|
|
|
|
Name: file.Name,
|
|
|
|
ParentDir: file.dir.Path,
|
|
|
|
}
|
2018-05-14 17:02:17 +08:00
|
|
|
|
2018-06-06 14:37:41 +08:00
|
|
|
resp, err := client.GetEntryAttributes(ctx, request)
|
2018-05-25 16:22:31 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("file attr read file %v: %v", request, err)
|
|
|
|
return err
|
|
|
|
}
|
2018-05-08 16:59:43 +08:00
|
|
|
|
2018-05-25 16:22:31 +08:00
|
|
|
file.attributes = resp.Attributes
|
|
|
|
file.Chunks = resp.Chunks
|
2018-05-22 18:26:38 +08:00
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
glog.V(1).Infof("file attr %v %+v: %d", file.fullpath(), file.attributes, filer2.TotalSize(file.Chunks))
|
2018-05-22 18:26:38 +08:00
|
|
|
|
2018-05-25 16:22:31 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-08 16:59:43 +08:00
|
|
|
}
|
2018-05-14 17:02:17 +08:00
|
|
|
}
|
2018-05-08 16:59:43 +08:00
|
|
|
|
2018-05-22 18:26:38 +08: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-20 04:51:44 +08:00
|
|
|
|
2018-05-14 17:02:17 +08:00
|
|
|
return nil
|
2018-05-08 16:59:43 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-22 18:26:38 +08:00
|
|
|
func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
glog.V(3).Infof("%v file open %+v", file.fullpath(), req)
|
2018-05-21 15:00:28 +08:00
|
|
|
|
2018-05-25 16:27:21 +08:00
|
|
|
file.isOpen = true
|
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid)
|
|
|
|
|
|
|
|
resp.Handle = fuse.HandleID(handle.handle)
|
|
|
|
|
|
|
|
glog.V(3).Infof("%v file open handle id = %d", file.fullpath(), handle.handle)
|
|
|
|
|
|
|
|
return handle, nil
|
2018-05-21 15:00:28 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
|
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
glog.V(3).Infof("%v file setattr %+v, fh=%d", file.fullpath(), req, req.Handle)
|
2018-05-22 18:26:38 +08:00
|
|
|
if req.Valid.Size() {
|
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
glog.V(3).Infof("%v file setattr set size=%v", file.fullpath(), req.Size)
|
2018-05-22 18:26:38 +08:00
|
|
|
if req.Size == 0 {
|
2018-05-25 14:20:12 +08:00
|
|
|
// fmt.Printf("truncate %v \n", fullPath)
|
2018-05-22 18:26:38 +08:00
|
|
|
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()
|
2018-05-21 15:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:54:27 +08:00
|
|
|
func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
|
|
|
|
// fsync works at OS level
|
2018-06-06 14:37:41 +08:00
|
|
|
// write the file chunks to the filerGrpcAddress
|
2018-05-24 11:55:24 +08:00
|
|
|
glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
|
2018-05-20 04:51:44 +08:00
|
|
|
|
2018-05-08 16:59:43 +08:00
|
|
|
return nil
|
2018-05-06 13:47:16 +08:00
|
|
|
}
|