From 97575e718549b960d3eeffc6f1a9f05803a01dc2 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 16 Sep 2018 12:37:06 -0700 Subject: [PATCH] do not set attributes if the file is still open --- weed/filesys/file.go | 83 ++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/weed/filesys/file.go b/weed/filesys/file.go index 3f22f0571..1bf544494 100644 --- a/weed/filesys/file.go +++ b/weed/filesys/file.go @@ -32,39 +32,8 @@ func (file *File) fullpath() string { func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error { - if file.attributes == nil || !file.isOpen { - item := file.wfs.listDirectoryEntriesCache.Get(file.fullpath()) - if item != nil && !item.Expired() { - 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, - } - - resp, err := client.GetEntryAttributes(ctx, request) - if err != nil { - glog.V(0).Infof("file attr read file %v: %v", request, err) - return err - } - - file.attributes = resp.Attributes - file.Chunks = resp.Chunks - - glog.V(1).Infof("file attr %v %+v: %d", file.fullpath(), file.attributes, filer2.TotalSize(file.Chunks)) - - return nil - }) - - if err != nil { - return err - } - } + if err := file.maybeLoadAttributes(ctx); err != nil { + return err } attr.Mode = os.FileMode(file.attributes.FileMode) @@ -95,7 +64,15 @@ func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.Op func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error { - glog.V(3).Infof("%v file setattr %+v, fh=%d", file.fullpath(), req, req.Handle) + if err := file.maybeLoadAttributes(ctx); err != nil { + return err + } + + if file.isOpen { + return nil + } + + glog.V(3).Infof("%v file setattr %+v, old:%+v", file.fullpath(), req, file.attributes) if req.Valid.Size() { glog.V(3).Infof("%v file setattr set size=%v", file.fullpath(), req.Size) @@ -151,3 +128,41 @@ func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error { return nil } + +func (file *File) maybeLoadAttributes(ctx context.Context) error { + if file.attributes == nil || !file.isOpen { + item := file.wfs.listDirectoryEntriesCache.Get(file.fullpath()) + if item != nil && !item.Expired() { + 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, + } + + resp, err := client.GetEntryAttributes(ctx, request) + if err != nil { + glog.V(0).Infof("file attr read file %v: %v", request, err) + return err + } + + file.attributes = resp.Attributes + file.Chunks = resp.Chunks + + glog.V(1).Infof("file attr %v %+v: %d", file.fullpath(), file.attributes, filer2.TotalSize(file.Chunks)) + + return nil + }) + + if err != nil { + return err + } + } + } + return nil +}