From 83cf94ad2dea74e5f9a881a617d06c42cbb7ba49 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 19 Apr 2021 10:58:25 -0700 Subject: [PATCH] delay new file creation unless file is opened exclusively --- weed/filesys/dir.go | 28 ++++++++++++++++++++++++---- weed/filesys/file.go | 1 + 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/weed/filesys/dir.go b/weed/filesys/dir.go index 1dc2ab939..6ee20974b 100644 --- a/weed/filesys/dir.go +++ b/weed/filesys/dir.go @@ -28,6 +28,7 @@ type Dir struct { } var _ = fs.Node(&Dir{}) + //var _ = fs.NodeIdentifier(&Dir{}) var _ = fs.NodeCreater(&Dir{}) var _ = fs.NodeMknoder(&Dir{}) @@ -140,19 +141,38 @@ func (dir *Dir) newDirectory(fullpath util.FullPath) fs.Node { func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) { - request, err := dir.doCreateEntry(req.Name, req.Mode, req.Uid, req.Gid, req.Flags&fuse.OpenExclusive != 0) + exclusive := req.Flags&fuse.OpenExclusive != 0 + isDirectory := req.Mode&os.ModeDir > 0 - if err != nil { - return nil, nil, err + if exclusive || isDirectory { + _, err := dir.doCreateEntry(req.Name, req.Mode, req.Uid, req.Gid, exclusive) + if err != nil { + return nil, nil, err + } } var node fs.Node - if request.Entry.IsDirectory { + if isDirectory { node = dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name)) return node, nil, nil } node = dir.newFile(req.Name) file := node.(*File) + file.entry = &filer_pb.Entry{ + Name: req.Name, + IsDirectory: req.Mode&os.ModeDir > 0, + Attributes: &filer_pb.FuseAttributes{ + Mtime: time.Now().Unix(), + Crtime: time.Now().Unix(), + FileMode: uint32(req.Mode &^ dir.wfs.option.Umask), + Uid: req.Uid, + Gid: req.Gid, + Collection: dir.wfs.option.Collection, + Replication: dir.wfs.option.Replication, + TtlSec: dir.wfs.option.TtlSec, + }, + } + file.dirtyMetadata = true fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid) return file, fh, nil diff --git a/weed/filesys/file.go b/weed/filesys/file.go index 2e37c6e0e..5fb4d8d11 100644 --- a/weed/filesys/file.go +++ b/weed/filesys/file.go @@ -265,6 +265,7 @@ func (file *File) maybeLoadEntry(ctx context.Context) (entry *filer_pb.Entry, er file.wfs.handlesLock.Lock() handle, found := file.wfs.handles[file.Id()] file.wfs.handlesLock.Unlock() + entry = file.entry if found { glog.V(4).Infof("maybeLoadEntry found opened file %s/%s: %v %v", file.dir.FullPath(), file.Name, handle.f.entry, entry) entry = handle.f.entry