1 4b33cdd0 2015-11-30 stephen.d package p9p
3 4b33cdd0 2015-11-30 stephen.d import (
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 5eb5d0b1 2016-05-19 stevvooe return Qid{}, err
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 c74282f8 2016-11-16 noreply n = copy(p, rread.Data)
153 c74282f8 2016-11-16 noreply switch {
154 c74282f8 2016-11-16 noreply case len(rread.Data) == 0:
155 c74282f8 2016-11-16 noreply err = io.EOF
156 c74282f8 2016-11-16 noreply case n < len(p):
157 c74282f8 2016-11-16 noreply // TODO(stevvooe): Technically, we should treat this as an io.EOF.
158 c74282f8 2016-11-16 noreply // However, we cannot tell if the short read was due to EOF or due to
159 c74282f8 2016-11-16 noreply // truncation.
162 c74282f8 2016-11-16 noreply return n, err
165 4b33cdd0 2015-11-30 stephen.d func (c *client) Write(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) {
166 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTwrite{
167 4b33cdd0 2015-11-30 stephen.d Fid: fid,
168 4b33cdd0 2015-11-30 stephen.d Offset: uint64(offset),
169 4b33cdd0 2015-11-30 stephen.d Data: p,
171 4b33cdd0 2015-11-30 stephen.d if err != nil {
172 4b33cdd0 2015-11-30 stephen.d return 0, err
175 4b33cdd0 2015-11-30 stephen.d rwrite, ok := resp.(MessageRwrite)
176 4b33cdd0 2015-11-30 stephen.d if !ok {
177 4b33cdd0 2015-11-30 stephen.d return 0, ErrUnexpectedMsg
180 c74282f8 2016-11-16 noreply if int(rwrite.Count) < len(p) {
181 c74282f8 2016-11-16 noreply err = io.ErrShortWrite
184 c74282f8 2016-11-16 noreply return int(rwrite.Count), err
187 4b33cdd0 2015-11-30 stephen.d func (c *client) Open(ctx context.Context, fid Fid, mode Flag) (Qid, uint32, error) {
188 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTopen{
189 4b33cdd0 2015-11-30 stephen.d Fid: fid,
190 4b33cdd0 2015-11-30 stephen.d Mode: mode,
192 4b33cdd0 2015-11-30 stephen.d if err != nil {
193 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
196 4b33cdd0 2015-11-30 stephen.d ropen, ok := resp.(MessageRopen)
197 4b33cdd0 2015-11-30 stephen.d if !ok {
198 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, ErrUnexpectedMsg
201 4b33cdd0 2015-11-30 stephen.d return ropen.Qid, ropen.IOUnit, nil
204 4b33cdd0 2015-11-30 stephen.d func (c *client) Create(ctx context.Context, parent Fid, name string, perm uint32, mode Flag) (Qid, uint32, error) {
205 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTcreate{
206 4b33cdd0 2015-11-30 stephen.d Fid: parent,
207 4b33cdd0 2015-11-30 stephen.d Name: name,
208 4b33cdd0 2015-11-30 stephen.d Perm: perm,
209 4b33cdd0 2015-11-30 stephen.d Mode: mode,
211 4b33cdd0 2015-11-30 stephen.d if err != nil {
212 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
215 4b33cdd0 2015-11-30 stephen.d rcreate, ok := resp.(MessageRcreate)
216 4b33cdd0 2015-11-30 stephen.d if !ok {
217 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, ErrUnexpectedMsg
220 4b33cdd0 2015-11-30 stephen.d return rcreate.Qid, rcreate.IOUnit, nil
223 4b33cdd0 2015-11-30 stephen.d func (c *client) Stat(ctx context.Context, fid Fid) (Dir, error) {
224 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTstat{Fid: fid})
225 4b33cdd0 2015-11-30 stephen.d if err != nil {
226 4b33cdd0 2015-11-30 stephen.d return Dir{}, err
229 4b33cdd0 2015-11-30 stephen.d rstat, ok := resp.(MessageRstat)
230 4b33cdd0 2015-11-30 stephen.d if !ok {
231 4b33cdd0 2015-11-30 stephen.d return Dir{}, ErrUnexpectedMsg
234 4b33cdd0 2015-11-30 stephen.d return rstat.Stat, nil
237 4b33cdd0 2015-11-30 stephen.d func (c *client) WStat(ctx context.Context, fid Fid, dir Dir) error {
238 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTwstat{
239 4b33cdd0 2015-11-30 stephen.d Fid: fid,
240 4b33cdd0 2015-11-30 stephen.d Stat: dir,
242 4b33cdd0 2015-11-30 stephen.d if err != nil {
243 4b33cdd0 2015-11-30 stephen.d return err
246 4b33cdd0 2015-11-30 stephen.d _, ok := resp.(MessageRwstat)
247 4b33cdd0 2015-11-30 stephen.d if !ok {
248 4b33cdd0 2015-11-30 stephen.d return ErrUnexpectedMsg
251 4b33cdd0 2015-11-30 stephen.d return nil