1 4b33cdd0 2015-11-30 stephen.d package p9p
3 4b33cdd0 2015-11-30 stephen.d import (
10 4b33cdd0 2015-11-30 stephen.d type client struct {
11 4b33cdd0 2015-11-30 stephen.d version string
12 4b33cdd0 2015-11-30 stephen.d msize int
13 4b33cdd0 2015-11-30 stephen.d ctx context.Context
14 4b33cdd0 2015-11-30 stephen.d transport roundTripper
17 4b33cdd0 2015-11-30 stephen.d // NewSession returns a session using the connection. The Context ctx provides
18 4b33cdd0 2015-11-30 stephen.d // a context for out of bad messages, such as flushes, that may be sent by the
19 4b33cdd0 2015-11-30 stephen.d // session. The session can effectively shutdown with this context.
20 4b33cdd0 2015-11-30 stephen.d func NewSession(ctx context.Context, conn net.Conn) (Session, error) {
21 4b33cdd0 2015-11-30 stephen.d ch := newChannel(conn, codec9p{}, DefaultMSize) // sets msize, effectively.
23 4b33cdd0 2015-11-30 stephen.d // negotiate the protocol version
24 4b33cdd0 2015-11-30 stephen.d version, err := clientnegotiate(ctx, ch, DefaultVersion)
25 4b33cdd0 2015-11-30 stephen.d if err != nil {
26 4b33cdd0 2015-11-30 stephen.d return nil, err
29 4b33cdd0 2015-11-30 stephen.d return &client{
30 4b33cdd0 2015-11-30 stephen.d version: version,
31 4b33cdd0 2015-11-30 stephen.d msize: ch.MSize(),
32 4b33cdd0 2015-11-30 stephen.d ctx: ctx,
33 4b33cdd0 2015-11-30 stephen.d transport: newTransport(ctx, ch),
37 4b33cdd0 2015-11-30 stephen.d var _ Session = &client{}
39 4b33cdd0 2015-11-30 stephen.d func (c *client) Version() (int, string) {
40 4b33cdd0 2015-11-30 stephen.d return c.msize, c.version
43 4b33cdd0 2015-11-30 stephen.d func (c *client) Auth(ctx context.Context, afid Fid, uname, aname string) (Qid, error) {
44 4b33cdd0 2015-11-30 stephen.d m := MessageTauth{
45 4b33cdd0 2015-11-30 stephen.d Afid: afid,
46 4b33cdd0 2015-11-30 stephen.d Uname: uname,
47 4b33cdd0 2015-11-30 stephen.d Aname: aname,
50 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, m)
51 4b33cdd0 2015-11-30 stephen.d if err != nil {
52 5eb5d0b1 2016-05-19 stevvooe return Qid{}, err
55 4b33cdd0 2015-11-30 stephen.d rauth, ok := resp.(MessageRauth)
56 4b33cdd0 2015-11-30 stephen.d if !ok {
57 4b33cdd0 2015-11-30 stephen.d return Qid{}, ErrUnexpectedMsg
60 4b33cdd0 2015-11-30 stephen.d return rauth.Qid, nil
63 4b33cdd0 2015-11-30 stephen.d func (c *client) Attach(ctx context.Context, fid, afid Fid, uname, aname string) (Qid, error) {
64 4b33cdd0 2015-11-30 stephen.d m := MessageTattach{
65 4b33cdd0 2015-11-30 stephen.d Fid: fid,
66 4b33cdd0 2015-11-30 stephen.d Afid: afid,
67 4b33cdd0 2015-11-30 stephen.d Uname: uname,
68 4b33cdd0 2015-11-30 stephen.d Aname: aname,
71 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, m)
72 4b33cdd0 2015-11-30 stephen.d if err != nil {
73 4b33cdd0 2015-11-30 stephen.d return Qid{}, err
76 4b33cdd0 2015-11-30 stephen.d rattach, ok := resp.(MessageRattach)
77 4b33cdd0 2015-11-30 stephen.d if !ok {
78 4b33cdd0 2015-11-30 stephen.d return Qid{}, ErrUnexpectedMsg
81 4b33cdd0 2015-11-30 stephen.d return rattach.Qid, nil
84 4b33cdd0 2015-11-30 stephen.d func (c *client) Clunk(ctx context.Context, fid Fid) error {
85 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTclunk{
86 4b33cdd0 2015-11-30 stephen.d Fid: fid,
88 4b33cdd0 2015-11-30 stephen.d if err != nil {
89 4b33cdd0 2015-11-30 stephen.d return err
92 4b33cdd0 2015-11-30 stephen.d _, ok := resp.(MessageRclunk)
93 4b33cdd0 2015-11-30 stephen.d if !ok {
94 4b33cdd0 2015-11-30 stephen.d return ErrUnexpectedMsg
97 4b33cdd0 2015-11-30 stephen.d return nil
100 4b33cdd0 2015-11-30 stephen.d func (c *client) Remove(ctx context.Context, fid Fid) error {
101 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTremove{
102 4b33cdd0 2015-11-30 stephen.d Fid: fid,
104 4b33cdd0 2015-11-30 stephen.d if err != nil {
105 4b33cdd0 2015-11-30 stephen.d return err
108 4b33cdd0 2015-11-30 stephen.d _, ok := resp.(MessageRremove)
109 4b33cdd0 2015-11-30 stephen.d if !ok {
110 4b33cdd0 2015-11-30 stephen.d return ErrUnexpectedMsg
113 4b33cdd0 2015-11-30 stephen.d return nil
116 4b33cdd0 2015-11-30 stephen.d func (c *client) Walk(ctx context.Context, fid Fid, newfid Fid, names ...string) ([]Qid, error) {
117 4b33cdd0 2015-11-30 stephen.d if len(names) > 16 {
118 4b33cdd0 2015-11-30 stephen.d return nil, ErrWalkLimit
121 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTwalk{
122 4b33cdd0 2015-11-30 stephen.d Fid: fid,
123 4b33cdd0 2015-11-30 stephen.d Newfid: newfid,
124 4b33cdd0 2015-11-30 stephen.d Wnames: names,
126 4b33cdd0 2015-11-30 stephen.d if err != nil {
127 4b33cdd0 2015-11-30 stephen.d return nil, err
130 4b33cdd0 2015-11-30 stephen.d rwalk, ok := resp.(MessageRwalk)
131 4b33cdd0 2015-11-30 stephen.d if !ok {
132 4b33cdd0 2015-11-30 stephen.d return nil, ErrUnexpectedMsg
135 4b33cdd0 2015-11-30 stephen.d return rwalk.Qids, nil
138 4b33cdd0 2015-11-30 stephen.d func (c *client) Read(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) {
139 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTread{
140 4b33cdd0 2015-11-30 stephen.d Fid: fid,
141 4b33cdd0 2015-11-30 stephen.d Offset: uint64(offset),
142 4b33cdd0 2015-11-30 stephen.d Count: uint32(len(p)),
144 4b33cdd0 2015-11-30 stephen.d if err != nil {
145 4b33cdd0 2015-11-30 stephen.d return 0, err
148 4b33cdd0 2015-11-30 stephen.d rread, ok := resp.(MessageRread)
149 4b33cdd0 2015-11-30 stephen.d if !ok {
150 4b33cdd0 2015-11-30 stephen.d return 0, ErrUnexpectedMsg
153 0f5f58bb 2016-11-16 stephen.d n = copy(p, rread.Data)
154 0f5f58bb 2016-11-16 stephen.d switch {
155 0f5f58bb 2016-11-16 stephen.d case len(rread.Data) == 0:
156 0f5f58bb 2016-11-16 stephen.d err = io.EOF
157 0f5f58bb 2016-11-16 stephen.d case n < len(p):
158 0f5f58bb 2016-11-16 stephen.d // TODO(stevvooe): Technically, we should treat this as an io.EOF.
159 0f5f58bb 2016-11-16 stephen.d // However, we cannot tell if the short read was due to EOF or due to
160 0f5f58bb 2016-11-16 stephen.d // truncation.
163 0f5f58bb 2016-11-16 stephen.d return n, err
166 4b33cdd0 2015-11-30 stephen.d func (c *client) Write(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) {
167 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTwrite{
168 4b33cdd0 2015-11-30 stephen.d Fid: fid,
169 4b33cdd0 2015-11-30 stephen.d Offset: uint64(offset),
170 4b33cdd0 2015-11-30 stephen.d Data: p,
172 4b33cdd0 2015-11-30 stephen.d if err != nil {
173 4b33cdd0 2015-11-30 stephen.d return 0, err
176 4b33cdd0 2015-11-30 stephen.d rwrite, ok := resp.(MessageRwrite)
177 4b33cdd0 2015-11-30 stephen.d if !ok {
178 4b33cdd0 2015-11-30 stephen.d return 0, ErrUnexpectedMsg
181 0f5f58bb 2016-11-16 stephen.d if int(rwrite.Count) < len(p) {
182 0f5f58bb 2016-11-16 stephen.d err = io.ErrShortWrite
185 0f5f58bb 2016-11-16 stephen.d return int(rwrite.Count), err
188 4b33cdd0 2015-11-30 stephen.d func (c *client) Open(ctx context.Context, fid Fid, mode Flag) (Qid, uint32, error) {
189 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTopen{
190 4b33cdd0 2015-11-30 stephen.d Fid: fid,
191 4b33cdd0 2015-11-30 stephen.d Mode: mode,
193 4b33cdd0 2015-11-30 stephen.d if err != nil {
194 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
197 4b33cdd0 2015-11-30 stephen.d ropen, ok := resp.(MessageRopen)
198 4b33cdd0 2015-11-30 stephen.d if !ok {
199 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, ErrUnexpectedMsg
202 4b33cdd0 2015-11-30 stephen.d return ropen.Qid, ropen.IOUnit, nil
205 4b33cdd0 2015-11-30 stephen.d func (c *client) Create(ctx context.Context, parent Fid, name string, perm uint32, mode Flag) (Qid, uint32, error) {
206 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTcreate{
207 4b33cdd0 2015-11-30 stephen.d Fid: parent,
208 4b33cdd0 2015-11-30 stephen.d Name: name,
209 4b33cdd0 2015-11-30 stephen.d Perm: perm,
210 4b33cdd0 2015-11-30 stephen.d Mode: mode,
212 4b33cdd0 2015-11-30 stephen.d if err != nil {
213 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
216 4b33cdd0 2015-11-30 stephen.d rcreate, ok := resp.(MessageRcreate)
217 4b33cdd0 2015-11-30 stephen.d if !ok {
218 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, ErrUnexpectedMsg
221 4b33cdd0 2015-11-30 stephen.d return rcreate.Qid, rcreate.IOUnit, nil
224 4b33cdd0 2015-11-30 stephen.d func (c *client) Stat(ctx context.Context, fid Fid) (Dir, error) {
225 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTstat{Fid: fid})
226 4b33cdd0 2015-11-30 stephen.d if err != nil {
227 4b33cdd0 2015-11-30 stephen.d return Dir{}, err
230 4b33cdd0 2015-11-30 stephen.d rstat, ok := resp.(MessageRstat)
231 4b33cdd0 2015-11-30 stephen.d if !ok {
232 4b33cdd0 2015-11-30 stephen.d return Dir{}, ErrUnexpectedMsg
235 4b33cdd0 2015-11-30 stephen.d return rstat.Stat, nil
238 4b33cdd0 2015-11-30 stephen.d func (c *client) WStat(ctx context.Context, fid Fid, dir Dir) error {
239 4b33cdd0 2015-11-30 stephen.d resp, err := c.transport.send(ctx, MessageTwstat{
240 4b33cdd0 2015-11-30 stephen.d Fid: fid,
241 4b33cdd0 2015-11-30 stephen.d Stat: dir,
243 4b33cdd0 2015-11-30 stephen.d if err != nil {
244 4b33cdd0 2015-11-30 stephen.d return err
247 4b33cdd0 2015-11-30 stephen.d _, ok := resp.(MessageRwstat)
248 4b33cdd0 2015-11-30 stephen.d if !ok {
249 4b33cdd0 2015-11-30 stephen.d return ErrUnexpectedMsg
252 4b33cdd0 2015-11-30 stephen.d return nil