lib9p

Go 9P library.
Log | Files | Refs

commit f18218b8afb78e449d2a06526d2ce4badec11dc6
parent ca64448d5dd94ec9ea96ae39e48cfa5ce58f34cc
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 14 Oct 2023 08:05:54 +0900

add child to clientFile

Diffstat:
Mclient_test.go | 10++++++++++
Mfile.go | 54+++++++++++++++++++++++++++++++++++++++++++-----------
Mserver.go | 1-
3 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/client_test.go b/client_test.go @@ -132,4 +132,14 @@ func TestClient2(t *testing.T) { t.Errorf("mount: %v", err) } t.Log(fs) + + fs.walk(".") + f := fs.Root() + t.Log(f) + + c, err := f.Child() + if err != nil { + t.Errorf("child: %v", err) + } + t.Log(c) } \ No newline at end of file diff --git a/file.go b/file.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "path" ) type File interface { @@ -69,12 +70,11 @@ func walkfile(f File, name string) (File, error) { } type ClientFile struct { - name string - path string + name string + path string parent *ClientFile - children []*ClientFile - fid *clientFid - qid Qid + fid *clientFid + qid Qid iounit uint32 client *Client } @@ -84,11 +84,41 @@ func (cf *ClientFile) Parent() (File, error) { } func (cf *ClientFile) Child() ([]File, error) { - children := make([]File, len(cf.children)) - for i := 0; i < len(children); i++ { - children[i] = cf.children[i] + if cf.fid == nil || (cf.fid.omode != OREAD && cf.fid.omode != ORDWR) { + if err := cf.Open(OREAD); err != nil { + return nil, fmt.Errorf("open: %v", err) + } + } + if cf.qid.Type&QTDIR == 0 { + return nil, fmt.Errorf("not a directory") + } + var data []byte + buf := make([]byte, 0, cf.iounit) + for { + n, err := cf.Read(buf) + if err == io.EOF { + if n > 0 { + data = append(data, buf[:n]...) + } + break + } else if err != nil { + return nil, fmt.Errorf("read dir: %v", err) + } + data = append(data, buf[:n]...) } - return children, nil + + var files []File + for len(data) > 0 { + stat := newStat(data) + files = append(files, &ClientFile{ + name: stat.Name, + path: path.Join(cf.name, stat.Name), + parent: cf, + client: cf.client, + }) + data = data[stat.size():] + } + return files, nil } func (cf *ClientFile) Stat() (*FileInfo, error) { @@ -104,6 +134,9 @@ func (cf *ClientFile) Qid() Qid { } func (cf *ClientFile) Open(mode OpenMode) error { + if cf.fid == nil { + panic("TODO: Walk to the file") + } qid, iounit, err := cf.client.Open(context.TODO(), cf.fid.fid, mode) if err != nil { return err @@ -122,4 +155,4 @@ func (cf *ClientFile) Close() error { func (cf *ClientFile) Read(b []byte) (int, error) { return 0, io.EOF -} -\ No newline at end of file +} diff --git a/server.go b/server.go @@ -513,7 +513,6 @@ func sRead(s *Server, r *Req) { log.Printf("get children: %v", err) } - // TODO: check if TRead offset matches that of last TRead message. if ifcall.Offset() != 0 && ifcall.Offset() != r.fid.dirOffset { respond(r, fmt.Errorf("invalid dir offset")) return