Browse Source

ensure the same Node is returned

pull/655/head
Chris Lu 7 years ago
parent
commit
be58993f47
  1. 30
      weed/filesys/dir.go

30
weed/filesys/dir.go

@ -9,10 +9,13 @@ import (
"bazil.org/fuse/fs" "bazil.org/fuse/fs"
"bazil.org/fuse" "bazil.org/fuse"
"github.com/chrislusf/seaweedfs/weed/filer" "github.com/chrislusf/seaweedfs/weed/filer"
"sync"
) )
type Dir struct { type Dir struct {
Path string Path string
NodeMap map[string]fs.Node
NodeMapLock sync.Mutex
wfs *WFS wfs *WFS
} }
@ -21,16 +24,30 @@ func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error {
return nil return nil
} }
func (dir *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
func (dir *Dir) Lookup(ctx context.Context, name string) (node fs.Node, err error) {
dir.NodeMapLock.Lock()
defer dir.NodeMapLock.Unlock()
if dir.NodeMap == nil {
dir.NodeMap = make(map[string]fs.Node)
}
if node, ok := dir.NodeMap[name]; ok {
return node, nil
}
if entry, err := filer.LookupDirectoryEntry(dir.wfs.filer, dir.Path, name); err == nil { if entry, err := filer.LookupDirectoryEntry(dir.wfs.filer, dir.Path, name); err == nil {
if !entry.Found { if !entry.Found {
return nil, fuse.ENOENT return nil, fuse.ENOENT
} }
if entry.FileId != "" { if entry.FileId != "" {
return &File{FileId: filer.FileId(entry.FileId), Name: name, wfs: dir.wfs}, nil
node = &File{FileId: filer.FileId(entry.FileId), Name: name, wfs: dir.wfs}
} else { } else {
return &Dir{Path: path.Join(dir.Path, name), wfs: dir.wfs}, nil
node = &Dir{Path: path.Join(dir.Path, name), wfs: dir.wfs}
} }
dir.NodeMap[name] = node
return node, nil
} }
return nil, fuse.ENOENT return nil, fuse.ENOENT
@ -54,10 +71,17 @@ func (dir *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
} }
func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
dir.NodeMapLock.Lock()
defer dir.NodeMapLock.Unlock()
name := path.Join(dir.Path, req.Name) name := path.Join(dir.Path, req.Name)
err := filer.DeleteDirectoryOrFile(dir.wfs.filer, name, req.Dir) err := filer.DeleteDirectoryOrFile(dir.wfs.filer, name, req.Dir)
if err != nil { if err != nil {
fmt.Printf("Delete file %s [ERROR] %s\n", name, err) fmt.Printf("Delete file %s [ERROR] %s\n", name, err)
} else {
delete(dir.NodeMap, req.Name)
} }
return err return err
} }
Loading…
Cancel
Save