commit 287a65746ba4a3ad22b9c48ee3293e9ddde96fbc
parent c2a30f351932ac96a20217c786e699fe1d89e070
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 8 Sep 2023 10:42:06 +0900
implement Child, Parent
Diffstat:
1 file changed, 29 insertions(+), 12 deletions(-)
diff --git a/diskfs/file.go b/diskfs/file.go
@@ -16,6 +16,7 @@ type File struct {
fs *FS // file system to which this file belongs
file *os.File // underlying file
path string // absolute path from the root of the fs.
+ children []lib9p.File
}
func openFile(fsys *FS, fpath string) (*File, error) {
@@ -88,20 +89,13 @@ func (f *File) Parent() (lib9p.File, error) {
}
func (f *File) Child() ([]lib9p.File, error) {
- de, err := f.ReadDir(-1)
- if err != nil {
- return nil, fmt.Errorf("read dir: %v", err)
- }
- children := make([]lib9p.File, len(de))
- for i := 0; i < len(de); i++ {
- childPath := path.Join(f.PathName(), de[i].Name())
- file, err := f.fs.Open(childPath)
+ if f.children == nil {
+ _, err := f.ReadDir(-1)
if err != nil {
- return children, fmt.Errorf("open child: %v", err)
+ return f.children, fmt.Errorf("read dir: %v")
}
- children[i] = file
}
- return children, nil
+ return f.children, nil
}
func (f *File) ReadAt(p []byte, off int64) (int, error) {
@@ -113,14 +107,37 @@ func (f *File) ReadDir(n int) ([]*lib9p.DirEntry, error) {
if err != nil {
return nil, err
}
+ if len(fsde) == 0 {
+ if f.children == nil {
+ f.children = make([]lib9p.File, 0)
+ }
+ de := make([]*lib9p.DirEntry, len(f.children))
+ for i := 0; i < len(f.children); i++ {
+ fi, err := f.children[i].Stat()
+ if err != nil {
+ return de, fmt.Errorf("stat: %v", err)
+ }
+ de[i] = &lib9p.FileInfo{*fi.Sys().(*lib9p.Stat)}
+ }
+ return de, nil
+ }
de := make([]*lib9p.DirEntry, len(fsde))
+ children := make([]lib9p.File, len(fsde))
for i := 0; i < len(de); i++ {
info, err := fsde[i].Info()
if err != nil {
- return nil, err
+ return de, err
}
id := idFromInfo(info)
de[i] = &lib9p.FileInfo{*fiStat(f.fs.qidPool, id, info)}
+
+ childPath := path.Join(f.PathName(), info.Name())
+ child, err := f.fs.Open(childPath)
+ if err != nil {
+ return de, fmt.Errorf("open: %v", err)
+ }
+ children[i] = child
}
+ f.children = append(f.children, children...)
return de, nil
}