github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/internal/fastwalk/fastwalk_portable.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build appengine || (!linux && !darwin && !freebsd && !openbsd && !netbsd)
     6  // +build appengine !linux,!darwin,!freebsd,!openbsd,!netbsd
     7  
     8  package fastwalk
     9  
    10  import (
    11  	"io/ioutil"
    12  	"os"
    13  )
    14  
    15  // readDir calls fn for each directory entry in dirName.
    16  // It does not descend into directories or follow symlinks.
    17  // If fn returns a non-nil error, readDir returns with that error
    18  // immediately.
    19  func readDir(dirName string, fn func(dirName, entName string, typ os.FileMode) error) error {
    20  	fis, err := ioutil.ReadDir(dirName)
    21  	if err != nil {
    22  		return err
    23  	}
    24  	skipFiles := false
    25  	for _, fi := range fis {
    26  		if fi.Mode().IsRegular() && skipFiles {
    27  			continue
    28  		}
    29  		if err := fn(dirName, fi.Name(), fi.Mode()&os.ModeType); err != nil {
    30  			if err == ErrSkipFiles {
    31  				skipFiles = true
    32  				continue
    33  			}
    34  			return err
    35  		}
    36  	}
    37  	return nil
    38  }