File: entry_filter_portable.go

package info (click to toggle)
golang-github-charlievieth-fastwalk 1.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 372 kB
  • sloc: makefile: 80; sh: 35; asm: 13
file content (37 lines) | stat: -rw-r--r-- 741 bytes parent folder | download | duplicates (2)
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
//go:build !darwin && !windows && !(aix || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris)

package fastwalk

import (
	"io/fs"
	"path/filepath"
	"sync"
)

type EntryFilter struct {
	// we assume most files have not been seen so
	// no need for a RWMutex
	mu   sync.Mutex
	seen map[string]struct{}
}

func (e *EntryFilter) Entry(path string, _ fs.DirEntry) bool {
	name, err := filepath.EvalSymlinks(path)
	if err != nil {
		return false
	}
	e.mu.Lock()
	if e.seen == nil {
		e.seen = make(map[string]struct{}, 128)
	}
	_, ok := e.seen[name]
	if !ok {
		e.seen[name] = struct{}{}
	}
	e.mu.Unlock()
	return ok
}

func NewEntryFilter() *EntryFilter {
	return &EntryFilter{seen: make(map[string]struct{}, 128)}
}