Skip to content

Commit

Permalink
p9: fix Twstat encoding and decoding (#61)
Browse files Browse the repository at this point in the history
* proto: Better error messages for Receive().

* p9: Fix Twstat encoding and decoding.
  • Loading branch information
DeedleFake authored Oct 11, 2019
1 parent 589bc79 commit 05ab2d0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
38 changes: 38 additions & 0 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,43 @@ type Twstat struct {
Stat Stat
}

func (stat Twstat) P9Encode() ([]byte, error) {
var buf bytes.Buffer

err := proto.Write(&buf, stat.FID)
if err != nil {
return nil, err
}

err = proto.Write(&buf, stat.Stat.size()+2)
if err != nil {
return nil, err
}

err = proto.Write(&buf, stat.Stat)
return buf.Bytes(), err
}

func (stat *Twstat) P9Decode(r io.Reader) error {
err := proto.Read(r, &stat.FID)
if err != nil {
return err
}

var size uint16
err = proto.Read(r, &size)
if err != nil {
return err
}

r = &util.LimitedReader{
R: r,
N: uint32(size),
E: ErrLargeStat,
}

return proto.Read(r, &stat.Stat)
}

type Rwstat struct {
}
30 changes: 14 additions & 16 deletions proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,11 @@ func (p Proto) Receive(r io.Reader, msize uint32) (msg interface{}, tag uint16,
E: ErrLargeMessage,
}

read := func(v interface{}) {
if err != nil {
return
}

err = Read(lr, v)
if err != nil {
err = util.Errorf("receive: %w", err)
}
}

var msgType uint8
read(&msgType)
err = Read(lr, &msgType)
if err != nil {
return nil, NoTag, util.Errorf("receive: failed to read message type: %w", err)
}

t := p.TypeFromID(msgType)
if t == nil {
Expand All @@ -102,10 +94,16 @@ func (p Proto) Receive(r io.Reader, msize uint32) (msg interface{}, tag uint16,
}

tag = NoTag
read(&tag)
err = Read(lr, &tag)
if err != nil {
return nil, tag, util.Errorf("receive: failed to read tag: %w", err)
}

m := reflect.New(t)
read(m.Interface())
err = Read(lr, m.Interface())
if err != nil {
return nil, tag, util.Errorf("receive %v: %w", m.Type().Elem(), err)
}

return m.Elem().Interface(), tag, err
}
Expand All @@ -125,13 +123,13 @@ func (p Proto) Send(w io.Writer, tag uint16, msg interface{}) (err error) {

err = Write(w, v)
if err != nil {
err = util.Errorf("send: %w", err)
err = util.Errorf("send %T: %w", msg, err)
}
}

n, err := Size(msg)
if err != nil {
return util.Errorf("send: %w", err)
return util.Errorf("send %T: %w", msg, err)
}

write(4 + 1 + 2 + n)
Expand Down
2 changes: 1 addition & 1 deletion stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var (
// ErrLargeStat is returned during decoding when a stat is larger
// than its own declared size.
ErrLargeStat = errors.New("stat larger that declared size")
ErrLargeStat = errors.New("stat larger than declared size")
)

// FileMode stores permission and type information about a file or
Expand Down

0 comments on commit 05ab2d0

Please sign in to comment.