File: readdir.go

package info (click to toggle)
golang-github-hugelgupf-p9 0.3.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 608 kB
  • sloc: makefile: 9
file content (50 lines) | stat: -rw-r--r-- 863 bytes parent folder | download
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
package localfs

import (
	"io"
	"path"

	"github.com/hugelgupf/p9/p9"
)

// Readdir implements p9.File.Readdir.
func (l *Local) Readdir(offset uint64, count uint32) (p9.Dirents, error) {
	var (
		p9Ents = make([]p9.Dirent, 0)
		cursor = uint64(0)
	)

	for len(p9Ents) < int(count) {
		singleEnt, err := l.file.Readdirnames(1)

		if err == io.EOF {
			return p9Ents, nil
		} else if err != nil {
			return nil, err
		}

		// we consumed an entry
		cursor++

		// cursor \in (offset, offset+count)
		if cursor < offset || cursor > offset+uint64(count) {
			continue
		}

		name := singleEnt[0]

		localEnt := Local{path: path.Join(l.path, name)}
		qid, _, err := localEnt.info()
		if err != nil {
			return p9Ents, err
		}
		p9Ents = append(p9Ents, p9.Dirent{
			QID:    qid,
			Type:   qid.Type,
			Name:   name,
			Offset: cursor,
		})
	}

	return p9Ents, nil
}