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

     1  //go:build appengine || solaris || (!linux && !darwin && !freebsd && !openbsd && !netbsd)
     2  // +build appengine solaris !linux,!darwin,!freebsd,!openbsd,!netbsd
     3  
     4  package fastwalk
     5  
     6  import (
     7  	"io/fs"
     8  	"os"
     9  )
    10  
    11  // readDir calls fn for each directory entry in dirName.
    12  // It does not descend into directories or follow symlinks.
    13  // If fn returns a non-nil error, readDir returns with that error
    14  // immediately.
    15  func readDir(dirName string, fn func(dirName, entName string, de fs.DirEntry) error) error {
    16  	f, err := os.Open(dirName)
    17  	if err != nil {
    18  		return err
    19  	}
    20  	des, readErr := f.ReadDir(-1)
    21  	f.Close()
    22  	if readErr != nil && len(des) == 0 {
    23  		return readErr
    24  	}
    25  
    26  	var skipFiles bool
    27  	for _, d := range des {
    28  		if skipFiles && d.Type().IsRegular() {
    29  			continue
    30  		}
    31  		// Need to use FileMode.Type().Type() for fs.DirEntry
    32  		e := newDirEntry(dirName, d)
    33  		if err := fn(dirName, d.Name(), e); err != nil {
    34  			if err != ErrSkipFiles {
    35  				return err
    36  			}
    37  			skipFiles = true
    38  		}
    39  	}
    40  
    41  	return readErr
    42  }