commit 8404fb355041ce5292210491b039d5c28b568098 from: Adrien Duermael date: Wed Nov 11 03:01:54 2015 UTC implementing p9p fs with v1/pkg/p9p (wip) commit - e342de7d52c80e5babe159afd1452c790e709fc0 commit + 8404fb355041ce5292210491b039d5c28b568098 blob - 5405996d8c9189d9946e0229e5f1e8c0dd7cf716 blob + 54e1d785e47221b2a1766be11f5ffa9db492362a --- client.go +++ client.go @@ -2,6 +2,7 @@ package p9p import ( "golang.org/x/net/context" + "io" "net" ) @@ -150,6 +151,55 @@ func (c *client) Read(ctx context.Context, fid Fid, p } return copy(p, rread.Data), nil +} + +// Readdir implements Plan9 wire protocol specifics for reading Dir entries. +func (c *client) Readdir(ctx context.Context, fid Fid) ([]*Dir, error) { + + errChan := make(chan error) + doneChan := make(chan []*Dir) + + reader, writer := io.Pipe() + buf := make([]byte, 256) + offset := int64(0) + + go func() { + for { + n, err := c.Read(ctx, fid, buf, offset) + if err != nil { + errChan <- err + break + } + if n == 0 { + break + } + writer.Write(buf[:n]) + } + writer.Close() + }() + + go func() { + dirs := []*Dir{} + dir := &Dir{} + for { + err := DecodeDir(NewCodec(), reader, dir) + if err != nil && err != io.EOF { + errChan <- err + break + } + dirs = append(dirs, dir) + if err != nil { + doneChan <- dirs + } + } + }() + + select { + case err := <-errChan: + return nil, err + case dirs := <-doneChan: + return dirs, nil + } } func (c *client) Write(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) { blob - ff2fd20cbe5bddfe06b2157dc145d0fc537287d0 blob + 3b37420d69f4a52eb888486100d1177f174bb7c5 --- session.go +++ session.go @@ -24,6 +24,7 @@ type Session interface { Remove(ctx context.Context, fid Fid) error Walk(ctx context.Context, fid Fid, newfid Fid, names ...string) ([]Qid, error) Read(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) + Readdir(ctx context.Context, fid Fid) ([]*Dir, error) Write(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) Open(ctx context.Context, fid Fid, mode Flag) (Qid, uint32, error) Create(ctx context.Context, parent Fid, name string, perm uint32, mode Flag) (Qid, uint32, error)