aboutsummaryrefslogtreecommitdiff
path: root/weed/mount/directory_read.go
blob: 51c51ae16c9b828ebbf967913ace4837515e4194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package mount

import (
	"context"
	"github.com/chrislusf/seaweedfs/weed/filer"
	"github.com/chrislusf/seaweedfs/weed/filesys/meta_cache"
	"github.com/chrislusf/seaweedfs/weed/glog"
	"github.com/chrislusf/seaweedfs/weed/util"
	"github.com/hanwen/go-fuse/v2/fs"
	"github.com/hanwen/go-fuse/v2/fuse"
	"math"
	"os"
	"syscall"
)

var _ = fs.NodeReaddirer(&Directory{})
var _ = fs.NodeGetattrer(&Directory{})

func (dir *Directory) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
	out.Mode = 0755
	return 0
}

func (dir *Directory) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {

	dirPath := util.FullPath(dir.FullPath())
	glog.V(4).Infof("Readdir %s", dirPath)

	sourceChan := make(chan fuse.DirEntry, 64)

	stream := newDirectoryListStream(sourceChan)

	processEachEntryFn := func(entry *filer.Entry, isLast bool) {
		sourceChan <- fuse.DirEntry{
			Mode: uint32(entry.Mode),
			Name: entry.Name(),
			Ino:  dirPath.Child(entry.Name()).AsInode(os.ModeDir),
		}
	}

	if err := meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, dirPath); err != nil {
		glog.Errorf("dir ReadDirAll %s: %v", dirPath, err)
		return nil, fs.ToErrno(os.ErrInvalid)
	}
	go func() {
		dir.wfs.metaCache.ListDirectoryEntries(context.Background(), dirPath, "", false, int64(math.MaxInt32), func(entry *filer.Entry) bool {
			processEachEntryFn(entry, false)
			return true
		})
		close(sourceChan)
	}()

	return stream, fs.OK
}

var _ = fs.DirStream(&DirectoryListStream{})

type DirectoryListStream struct {
	next       fuse.DirEntry
	sourceChan chan fuse.DirEntry
	isStarted  bool
	hasNext    bool
}

func newDirectoryListStream(ch chan fuse.DirEntry) *DirectoryListStream {
	return &DirectoryListStream{
		sourceChan: ch,
	}
}

func (i *DirectoryListStream) HasNext() bool {
	if !i.isStarted {
		i.next, i.hasNext = <-i.sourceChan
		i.isStarted = true
	}
	return i.hasNext
}
func (i *DirectoryListStream) Next() (fuse.DirEntry, syscall.Errno) {
	t := i.next
	i.next, i.hasNext = <-i.sourceChan
	return t, fs.OK
}
func (i *DirectoryListStream) Close() {
}