seaweedfs/weed/filesys/dir.go

88 lines
1.9 KiB
Go
Raw Normal View History

2018-05-06 13:47:16 +08:00
package filesys
import (
"context"
"fmt"
2018-05-06 14:39:29 +08:00
"os"
2018-05-06 13:47:16 +08:00
"path"
2018-05-06 14:39:29 +08:00
"bazil.org/fuse/fs"
"bazil.org/fuse"
2018-05-06 13:47:16 +08:00
"github.com/chrislusf/seaweedfs/weed/filer"
2018-05-06 14:50:34 +08:00
"sync"
2018-05-06 13:47:16 +08:00
)
type Dir struct {
2018-05-06 14:50:34 +08:00
Path string
NodeMap map[string]fs.Node
NodeMapLock sync.Mutex
wfs *WFS
2018-05-06 13:47:16 +08:00
}
func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error {
attr.Mode = os.ModeDir | 0555
return nil
}
2018-05-06 14:50:34 +08:00
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
}
2018-05-06 13:47:16 +08:00
if entry, err := filer.LookupDirectoryEntry(dir.wfs.filer, dir.Path, name); err == nil {
if !entry.Found {
return nil, fuse.ENOENT
}
if entry.FileId != "" {
2018-05-06 14:50:34 +08:00
node = &File{FileId: filer.FileId(entry.FileId), Name: name, wfs: dir.wfs}
2018-05-06 13:47:16 +08:00
} else {
2018-05-06 14:50:34 +08:00
node = &Dir{Path: path.Join(dir.Path, name), wfs: dir.wfs}
2018-05-06 13:47:16 +08:00
}
2018-05-06 14:50:34 +08:00
dir.NodeMap[name] = node
return node, nil
2018-05-06 13:47:16 +08:00
}
return nil, fuse.ENOENT
}
func (dir *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
var ret []fuse.Dirent
if dirs, e := filer.ListDirectories(dir.wfs.filer, dir.Path); e == nil {
for _, d := range dirs.Directories {
dirent := fuse.Dirent{Name: string(d), Type: fuse.DT_Dir}
ret = append(ret, dirent)
}
}
if files, e := filer.ListFiles(dir.wfs.filer, dir.Path, ""); e == nil {
for _, f := range files.Files {
dirent := fuse.Dirent{Name: f.Name, Type: fuse.DT_File}
ret = append(ret, dirent)
}
}
return ret, nil
}
func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
2018-05-06 14:50:34 +08:00
dir.NodeMapLock.Lock()
defer dir.NodeMapLock.Unlock()
2018-05-06 13:47:16 +08:00
name := path.Join(dir.Path, req.Name)
err := filer.DeleteDirectoryOrFile(dir.wfs.filer, name, req.Dir)
if err != nil {
fmt.Printf("Delete file %s [ERROR] %s\n", name, err)
2018-05-06 14:50:34 +08:00
} else {
delete(dir.NodeMap, req.Name)
2018-05-06 13:47:16 +08:00
}
2018-05-06 14:50:34 +08:00
2018-05-06 13:47:16 +08:00
return err
}