github.com/charlievieth/fastwalk@v1.0.3/dirent_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  type portableDirent struct {
    12  	fs.DirEntry
    13  	path string
    14  	stat *fileInfo
    15  }
    16  
    17  // TODO: cache the result of Stat
    18  func (d *portableDirent) Stat() (fs.FileInfo, error) {
    19  	if d.DirEntry.Type()&os.ModeSymlink == 0 {
    20  		return d.DirEntry.Info()
    21  	}
    22  	stat := loadFileInfo(&d.stat)
    23  	stat.once.Do(func() {
    24  		stat.FileInfo, stat.err = os.Stat(d.path)
    25  	})
    26  	return stat.FileInfo, stat.err
    27  }
    28  
    29  func newDirEntry(dirName string, info fs.DirEntry) fs.DirEntry {
    30  	return &portableDirent{
    31  		DirEntry: info,
    32  		path:     dirName + string(os.PathSeparator) + info.Name(),
    33  	}
    34  }
    35  
    36  func fileInfoToDirEntry(dirname string, fi fs.FileInfo) fs.DirEntry {
    37  	return newDirEntry(dirname, fs.FileInfoToDirEntry(fi))
    38  }