Blame


1 4b33cdd0 2015-11-30 stephen.d package p9p
2 4b33cdd0 2015-11-30 stephen.d
3 4b33cdd0 2015-11-30 stephen.d import (
4 c74282f8 2016-11-16 noreply "io"
5 4b33cdd0 2015-11-30 stephen.d "net"
6 4b33cdd0 2015-11-30 stephen.d
7 529e2b2e 2016-11-14 noreply "context"
8 4b33cdd0 2015-11-30 stephen.d )
9 4b33cdd0 2015-11-30 stephen.d
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
15 4b33cdd0 2015-11-30 stephen.d }
16 4b33cdd0 2015-11-30 stephen.d
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.
22 4b33cdd0 2015-11-30 stephen.d
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
27 4b33cdd0 2015-11-30 stephen.d }
28 4b33cdd0 2015-11-30 stephen.d
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),
34 4b33cdd0 2015-11-30 stephen.d }, nil
35 4b33cdd0 2015-11-30 stephen.d }
36 4b33cdd0 2015-11-30 stephen.d
37 4b33cdd0 2015-11-30 stephen.d var _ Session = &client{}
38 4b33cdd0 2015-11-30 stephen.d
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
41 4b33cdd0 2015-11-30 stephen.d }
42 4b33cdd0 2015-11-30 stephen.d
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,
48 4b33cdd0 2015-11-30 stephen.d }
49 4b33cdd0 2015-11-30 stephen.d
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
53 4b33cdd0 2015-11-30 stephen.d }
54 4b33cdd0 2015-11-30 stephen.d
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
58 4b33cdd0 2015-11-30 stephen.d }
59 4b33cdd0 2015-11-30 stephen.d
60 4b33cdd0 2015-11-30 stephen.d return rauth.Qid, nil
61 4b33cdd0 2015-11-30 stephen.d }
62 4b33cdd0 2015-11-30 stephen.d
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,
69 4b33cdd0 2015-11-30 stephen.d }
70 4b33cdd0 2015-11-30 stephen.d
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
74 4b33cdd0 2015-11-30 stephen.d }
75 4b33cdd0 2015-11-30 stephen.d
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
79 4b33cdd0 2015-11-30 stephen.d }
80 4b33cdd0 2015-11-30 stephen.d
81 4b33cdd0 2015-11-30 stephen.d return rattach.Qid, nil
82 4b33cdd0 2015-11-30 stephen.d }
83 4b33cdd0 2015-11-30 stephen.d
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,
87 4b33cdd0 2015-11-30 stephen.d })
88 4b33cdd0 2015-11-30 stephen.d if err != nil {
89 4b33cdd0 2015-11-30 stephen.d return err
90 4b33cdd0 2015-11-30 stephen.d }
91 4b33cdd0 2015-11-30 stephen.d
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
95 4b33cdd0 2015-11-30 stephen.d }
96 4b33cdd0 2015-11-30 stephen.d
97 4b33cdd0 2015-11-30 stephen.d return nil
98 4b33cdd0 2015-11-30 stephen.d }
99 4b33cdd0 2015-11-30 stephen.d
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,
103 4b33cdd0 2015-11-30 stephen.d })
104 4b33cdd0 2015-11-30 stephen.d if err != nil {
105 4b33cdd0 2015-11-30 stephen.d return err
106 4b33cdd0 2015-11-30 stephen.d }
107 4b33cdd0 2015-11-30 stephen.d
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
111 4b33cdd0 2015-11-30 stephen.d }
112 4b33cdd0 2015-11-30 stephen.d
113 4b33cdd0 2015-11-30 stephen.d return nil
114 4b33cdd0 2015-11-30 stephen.d }
115 4b33cdd0 2015-11-30 stephen.d
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
119 4b33cdd0 2015-11-30 stephen.d }
120 4b33cdd0 2015-11-30 stephen.d
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,
125 4b33cdd0 2015-11-30 stephen.d })
126 4b33cdd0 2015-11-30 stephen.d if err != nil {
127 4b33cdd0 2015-11-30 stephen.d return nil, err
128 4b33cdd0 2015-11-30 stephen.d }
129 4b33cdd0 2015-11-30 stephen.d
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
133 4b33cdd0 2015-11-30 stephen.d }
134 4b33cdd0 2015-11-30 stephen.d
135 4b33cdd0 2015-11-30 stephen.d return rwalk.Qids, nil
136 4b33cdd0 2015-11-30 stephen.d }
137 4b33cdd0 2015-11-30 stephen.d
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)),
143 4b33cdd0 2015-11-30 stephen.d })
144 4b33cdd0 2015-11-30 stephen.d if err != nil {
145 4b33cdd0 2015-11-30 stephen.d return 0, err
146 4b33cdd0 2015-11-30 stephen.d }
147 4b33cdd0 2015-11-30 stephen.d
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
151 4b33cdd0 2015-11-30 stephen.d }
152 4b33cdd0 2015-11-30 stephen.d
153 c74282f8 2016-11-16 noreply n = copy(p, rread.Data)
154 c74282f8 2016-11-16 noreply switch {
155 c74282f8 2016-11-16 noreply case len(rread.Data) == 0:
156 c74282f8 2016-11-16 noreply err = io.EOF
157 c74282f8 2016-11-16 noreply case n < len(p):
158 c74282f8 2016-11-16 noreply // TODO(stevvooe): Technically, we should treat this as an io.EOF.
159 c74282f8 2016-11-16 noreply // However, we cannot tell if the short read was due to EOF or due to
160 c74282f8 2016-11-16 noreply // truncation.
161 c74282f8 2016-11-16 noreply }
162 c74282f8 2016-11-16 noreply
163 c74282f8 2016-11-16 noreply return n, err
164 4b33cdd0 2015-11-30 stephen.d }
165 4b33cdd0 2015-11-30 stephen.d
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,
171 4b33cdd0 2015-11-30 stephen.d })
172 4b33cdd0 2015-11-30 stephen.d if err != nil {
173 4b33cdd0 2015-11-30 stephen.d return 0, err
174 4b33cdd0 2015-11-30 stephen.d }
175 4b33cdd0 2015-11-30 stephen.d
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
179 4b33cdd0 2015-11-30 stephen.d }
180 4b33cdd0 2015-11-30 stephen.d
181 c74282f8 2016-11-16 noreply if int(rwrite.Count) < len(p) {
182 c74282f8 2016-11-16 noreply err = io.ErrShortWrite
183 c74282f8 2016-11-16 noreply }
184 c74282f8 2016-11-16 noreply
185 c74282f8 2016-11-16 noreply return int(rwrite.Count), err
186 4b33cdd0 2015-11-30 stephen.d }
187 4b33cdd0 2015-11-30 stephen.d
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,
192 4b33cdd0 2015-11-30 stephen.d })
193 4b33cdd0 2015-11-30 stephen.d if err != nil {
194 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
195 4b33cdd0 2015-11-30 stephen.d }
196 4b33cdd0 2015-11-30 stephen.d
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
200 4b33cdd0 2015-11-30 stephen.d }
201 4b33cdd0 2015-11-30 stephen.d
202 4b33cdd0 2015-11-30 stephen.d return ropen.Qid, ropen.IOUnit, nil
203 4b33cdd0 2015-11-30 stephen.d }
204 4b33cdd0 2015-11-30 stephen.d
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,
211 4b33cdd0 2015-11-30 stephen.d })
212 4b33cdd0 2015-11-30 stephen.d if err != nil {
213 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
214 4b33cdd0 2015-11-30 stephen.d }
215 4b33cdd0 2015-11-30 stephen.d
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
219 4b33cdd0 2015-11-30 stephen.d }
220 4b33cdd0 2015-11-30 stephen.d
221 4b33cdd0 2015-11-30 stephen.d return rcreate.Qid, rcreate.IOUnit, nil
222 4b33cdd0 2015-11-30 stephen.d }
223 4b33cdd0 2015-11-30 stephen.d
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
228 4b33cdd0 2015-11-30 stephen.d }
229 4b33cdd0 2015-11-30 stephen.d
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
233 4b33cdd0 2015-11-30 stephen.d }
234 4b33cdd0 2015-11-30 stephen.d
235 4b33cdd0 2015-11-30 stephen.d return rstat.Stat, nil
236 4b33cdd0 2015-11-30 stephen.d }
237 4b33cdd0 2015-11-30 stephen.d
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,
242 4b33cdd0 2015-11-30 stephen.d })
243 4b33cdd0 2015-11-30 stephen.d if err != nil {
244 4b33cdd0 2015-11-30 stephen.d return err
245 4b33cdd0 2015-11-30 stephen.d }
246 4b33cdd0 2015-11-30 stephen.d
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
250 4b33cdd0 2015-11-30 stephen.d }
251 4b33cdd0 2015-11-30 stephen.d
252 4b33cdd0 2015-11-30 stephen.d return nil
253 4b33cdd0 2015-11-30 stephen.d }