You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nfs/finder/elem.go

49 lines
948 B
Go

1 year ago
package finder
import (
"git.noahlan.cn/noahlan/ntool/nstr"
"io/fs"
)
// Elem of find file/dir path result
type Elem interface {
fs.DirEntry
// Path get file/dir path. eg: "/path/to/file.go"
Path() string
// Info get file info. like fs.DirEntry.Info(), but will cache result.
Info() (fs.FileInfo, error)
}
type elem struct {
fs.DirEntry
path string
stat fs.FileInfo
sErr error
}
// NewElem create a new Elem instance
func NewElem(fPath string, ent fs.DirEntry) Elem {
return &elem{
path: fPath,
DirEntry: ent,
}
}
// Path get full file/dir path. eg: "/path/to/file.go"
func (e *elem) Path() string {
return e.path
}
// Info get file info, will cache result
func (e *elem) Info() (fs.FileInfo, error) {
if e.stat == nil {
e.stat, e.sErr = e.DirEntry.Info()
}
return e.stat, e.sErr
}
// String get string representation
func (e *elem) String() string {
return nstr.OrCond(e.IsDir(), "dir: ", "file: ") + e.Path()
}