github.com/charlievieth/fastwalk@v1.0.3/entry_filter_portable.go (about)

     1  //go:build appengine || (!linux && !darwin && !freebsd && !openbsd && !netbsd && !windows && !solaris)
     2  // +build appengine !linux,!darwin,!freebsd,!openbsd,!netbsd,!windows,!solaris
     3  
     4  package fastwalk
     5  
     6  import (
     7  	"io/fs"
     8  	"path/filepath"
     9  	"sync"
    10  )
    11  
    12  type EntryFilter struct {
    13  	// we assume most files have not been seen so
    14  	// no need for a RWMutex
    15  	mu   sync.Mutex
    16  	seen map[string]struct{}
    17  }
    18  
    19  func (e *EntryFilter) Entry(path string, _ fs.DirEntry) bool {
    20  	name, err := filepath.EvalSymlinks(path)
    21  	if err != nil {
    22  		return false
    23  	}
    24  	e.mu.Lock()
    25  	if e.seen == nil {
    26  		e.seen = make(map[string]struct{}, 128)
    27  	}
    28  	_, ok := e.seen[name]
    29  	if !ok {
    30  		e.seen[name] = struct{}{}
    31  	}
    32  	e.mu.Unlock()
    33  	return ok
    34  }
    35  
    36  func NewEntryFilter() *EntryFilter {
    37  	return &EntryFilter{seen: make(map[string]struct{}, 128)}
    38  }