1 4b33cdd0 2015-11-30 stephen.d package p9p
3 4b33cdd0 2015-11-30 stephen.d import (
6 4b33cdd0 2015-11-30 stephen.d "golang.org/x/net/context"
9 4b33cdd0 2015-11-30 stephen.d type client struct {
10 4b33cdd0 2015-11-30 stephen.d version string
11 4b33cdd0 2015-11-30 stephen.d msize int
12 4b33cdd0 2015-11-30 stephen.d ctx context.Context
13 4b33cdd0 2015-11-30 stephen.d transport roundTripper
16 4b33cdd0 2015-11-30 stephen.d // NewSession returns a session using the connection. The Context ctx provides
17 4b33cdd0 2015-11-30 stephen.d // a context for out of bad messages, such as flushes, that may be sent by the
18 4b33cdd0 2015-11-30 stephen.d // session. The session can effectively shutdown with this context.
19 4b33cdd0 2015-11-30 stephen.d func NewSession(ctx context.Context, conn net.Conn) (Session, error) {
20 4b33cdd0 2015-11-30 stephen.d ch := newChannel(conn, codec9p{}, DefaultMSize) // sets msize, effectively.
22 4b33cdd0 2015-11-30 stephen.d // negotiate the protocol version
23 4b33cdd0 2015-11-30 stephen.d version, err := clientnegotiate(ctx, ch, DefaultVersion)
24 4b33cdd0 2015-11-30 stephen.d if err != nil {
25 4b33cdd0 2015-11-30 stephen.d return nil, err
28 4b33cdd0 2015-11-30 stephen.d return &client{
29 4b33cdd0 2015-11-30 stephen.d version: version,
30 4b33cdd0 2015-11-30 stephen.d msize: ch.MSize(),
31 4b33cdd0 2015-11-30 stephen.d ctx: ctx,
32 4b33cdd0 2015-11-30 stephen.d transport: newTransport(ctx, ch),
36 4b33cdd0 2015-11-30 stephen.d var _ Session = &client{}
38 4b33cdd0 2015-11-30 stephen.d func (c *client) Version() (int, string) {
39 4b33cdd0 2015-11-30 stephen.d return c.msize, c.version
42 4b33cdd0 2015-11-30 stephen.d func (c *client) Auth(ctx context.Context, afid Fid, uname, aname string) (Qid, error) {
43 4b33cdd0 2015-11-30 stephen.d m := MessageTauth{
44 4b33cdd0 2015-11-30 stephen.d Afid: afid,
45 4b33cdd0 2015-11-30 stephen.d Uname: uname,
46 4b33cdd0 2015-11-30 stephen.d Aname: aname,
49 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, m)
50 4b33cdd0 2015-11-30 stephen.d if err != nil {
51 4b33cdd0 2015-11-30 stephen.d return Qid{}, nil
54 4b33cdd0 2015-11-30 stephen.d rauth, ok := resp.(MessageRauth)
55 4b33cdd0 2015-11-30 stephen.d if !ok {
56 4b33cdd0 2015-11-30 stephen.d return Qid{}, ErrUnexpectedMsg
59 4b33cdd0 2015-11-30 stephen.d return rauth.Qid, nil
62 4b33cdd0 2015-11-30 stephen.d func (c *client) Attach(ctx context.Context, fid, afid Fid, uname, aname string) (Qid, error) {
63 4b33cdd0 2015-11-30 stephen.d m := MessageTattach{
64 4b33cdd0 2015-11-30 stephen.d Fid: fid,
65 4b33cdd0 2015-11-30 stephen.d Afid: afid,
66 4b33cdd0 2015-11-30 stephen.d Uname: uname,
67 4b33cdd0 2015-11-30 stephen.d Aname: aname,
70 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, m)
71 4b33cdd0 2015-11-30 stephen.d if err != nil {
72 4b33cdd0 2015-11-30 stephen.d return Qid{}, err
75 4b33cdd0 2015-11-30 stephen.d rattach, ok := resp.(MessageRattach)
76 4b33cdd0 2015-11-30 stephen.d if !ok {
77 4b33cdd0 2015-11-30 stephen.d return Qid{}, ErrUnexpectedMsg
80 4b33cdd0 2015-11-30 stephen.d return rattach.Qid, nil
83 4b33cdd0 2015-11-30 stephen.d func (c *client) Clunk(ctx context.Context, fid Fid) error {
84 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTclunk{
85 4b33cdd0 2015-11-30 stephen.d Fid: fid,
87 4b33cdd0 2015-11-30 stephen.d if err != nil {
88 4b33cdd0 2015-11-30 stephen.d return err
91 4b33cdd0 2015-11-30 stephen.d _, ok := resp.(MessageRclunk)
92 4b33cdd0 2015-11-30 stephen.d if !ok {
93 4b33cdd0 2015-11-30 stephen.d return ErrUnexpectedMsg
96 4b33cdd0 2015-11-30 stephen.d return nil
99 4b33cdd0 2015-11-30 stephen.d func (c *client) Remove(ctx context.Context, fid Fid) error {
100 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTremove{
101 4b33cdd0 2015-11-30 stephen.d Fid: fid,
103 4b33cdd0 2015-11-30 stephen.d if err != nil {
104 4b33cdd0 2015-11-30 stephen.d return err
107 4b33cdd0 2015-11-30 stephen.d _, ok := resp.(MessageRremove)
108 4b33cdd0 2015-11-30 stephen.d if !ok {
109 4b33cdd0 2015-11-30 stephen.d return ErrUnexpectedMsg
112 4b33cdd0 2015-11-30 stephen.d return nil
115 4b33cdd0 2015-11-30 stephen.d func (c *client) Walk(ctx context.Context, fid Fid, newfid Fid, names ...string) ([]Qid, error) {
116 4b33cdd0 2015-11-30 stephen.d if len(names) > 16 {
117 4b33cdd0 2015-11-30 stephen.d return nil, ErrWalkLimit
120 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTwalk{
121 4b33cdd0 2015-11-30 stephen.d Fid: fid,
122 4b33cdd0 2015-11-30 stephen.d Newfid: newfid,
123 4b33cdd0 2015-11-30 stephen.d Wnames: names,
125 4b33cdd0 2015-11-30 stephen.d if err != nil {
126 4b33cdd0 2015-11-30 stephen.d return nil, err
129 4b33cdd0 2015-11-30 stephen.d rwalk, ok := resp.(MessageRwalk)
130 4b33cdd0 2015-11-30 stephen.d if !ok {
131 4b33cdd0 2015-11-30 stephen.d return nil, ErrUnexpectedMsg
134 4b33cdd0 2015-11-30 stephen.d return rwalk.Qids, nil
137 4b33cdd0 2015-11-30 stephen.d func (c *client) Read(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) {
138 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTread{
139 4b33cdd0 2015-11-30 stephen.d Fid: fid,
140 4b33cdd0 2015-11-30 stephen.d Offset: uint64(offset),
141 4b33cdd0 2015-11-30 stephen.d Count: uint32(len(p)),
143 4b33cdd0 2015-11-30 stephen.d if err != nil {
144 4b33cdd0 2015-11-30 stephen.d return 0, err
147 4b33cdd0 2015-11-30 stephen.d rread, ok := resp.(MessageRread)
148 4b33cdd0 2015-11-30 stephen.d if !ok {
149 4b33cdd0 2015-11-30 stephen.d return 0, ErrUnexpectedMsg
152 4b33cdd0 2015-11-30 stephen.d return copy(p, rread.Data), nil
155 4b33cdd0 2015-11-30 stephen.d func (c *client) Write(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) {
156 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTwrite{
157 4b33cdd0 2015-11-30 stephen.d Fid: fid,
158 4b33cdd0 2015-11-30 stephen.d Offset: uint64(offset),
159 4b33cdd0 2015-11-30 stephen.d Data: p,
161 4b33cdd0 2015-11-30 stephen.d if err != nil {
162 4b33cdd0 2015-11-30 stephen.d return 0, err
165 4b33cdd0 2015-11-30 stephen.d rwrite, ok := resp.(MessageRwrite)
166 4b33cdd0 2015-11-30 stephen.d if !ok {
167 4b33cdd0 2015-11-30 stephen.d return 0, ErrUnexpectedMsg
170 4b33cdd0 2015-11-30 stephen.d return int(rwrite.Count), nil
173 4b33cdd0 2015-11-30 stephen.d func (c *client) Open(ctx context.Context, fid Fid, mode Flag) (Qid, uint32, error) {
174 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTopen{
175 4b33cdd0 2015-11-30 stephen.d Fid: fid,
176 4b33cdd0 2015-11-30 stephen.d Mode: mode,
178 4b33cdd0 2015-11-30 stephen.d if err != nil {
179 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
182 4b33cdd0 2015-11-30 stephen.d ropen, ok := resp.(MessageRopen)
183 4b33cdd0 2015-11-30 stephen.d if !ok {
184 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, ErrUnexpectedMsg
187 4b33cdd0 2015-11-30 stephen.d return ropen.Qid, ropen.IOUnit, nil
190 4b33cdd0 2015-11-30 stephen.d func (c *client) Create(ctx context.Context, parent Fid, name string, perm uint32, mode Flag) (Qid, uint32, error) {
191 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTcreate{
192 4b33cdd0 2015-11-30 stephen.d Fid: parent,
193 4b33cdd0 2015-11-30 stephen.d Name: name,
194 4b33cdd0 2015-11-30 stephen.d Perm: perm,
195 4b33cdd0 2015-11-30 stephen.d Mode: mode,
197 4b33cdd0 2015-11-30 stephen.d if err != nil {
198 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
201 4b33cdd0 2015-11-30 stephen.d rcreate, ok := resp.(MessageRcreate)
202 4b33cdd0 2015-11-30 stephen.d if !ok {
203 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, ErrUnexpectedMsg
206 4b33cdd0 2015-11-30 stephen.d return rcreate.Qid, rcreate.IOUnit, nil
209 4b33cdd0 2015-11-30 stephen.d func (c *client) Stat(ctx context.Context, fid Fid) (Dir, error) {
210 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTstat{Fid: fid})
211 4b33cdd0 2015-11-30 stephen.d if err != nil {
212 4b33cdd0 2015-11-30 stephen.d return Dir{}, err
215 4b33cdd0 2015-11-30 stephen.d rstat, ok := resp.(MessageRstat)
216 4b33cdd0 2015-11-30 stephen.d if !ok {
217 4b33cdd0 2015-11-30 stephen.d return Dir{}, ErrUnexpectedMsg
220 4b33cdd0 2015-11-30 stephen.d return rstat.Stat, nil
223 4b33cdd0 2015-11-30 stephen.d func (c *client) WStat(ctx context.Context, fid Fid, dir Dir) error {
224 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTwstat{
225 4b33cdd0 2015-11-30 stephen.d Fid: fid,
226 4b33cdd0 2015-11-30 stephen.d Stat: dir,
228 4b33cdd0 2015-11-30 stephen.d if err != nil {
229 4b33cdd0 2015-11-30 stephen.d return err
232 4b33cdd0 2015-11-30 stephen.d _, ok := resp.(MessageRwstat)
233 4b33cdd0 2015-11-30 stephen.d if !ok {
234 4b33cdd0 2015-11-30 stephen.d return ErrUnexpectedMsg
237 4b33cdd0 2015-11-30 stephen.d return nil