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 f3174901 2022-11-26 op "context"
5 c74282f8 2016-11-16 noreply "io"
6 4b33cdd0 2015-11-30 stephen.d "net"
7 4b33cdd0 2015-11-30 stephen.d )
8 4b33cdd0 2015-11-30 stephen.d
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
14 4b33cdd0 2015-11-30 stephen.d }
15 4b33cdd0 2015-11-30 stephen.d
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.
21 4b33cdd0 2015-11-30 stephen.d
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
26 4b33cdd0 2015-11-30 stephen.d }
27 4b33cdd0 2015-11-30 stephen.d
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),
33 4b33cdd0 2015-11-30 stephen.d }, nil
34 4b33cdd0 2015-11-30 stephen.d }
35 4b33cdd0 2015-11-30 stephen.d
36 4b33cdd0 2015-11-30 stephen.d var _ Session = &client{}
37 4b33cdd0 2015-11-30 stephen.d
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
40 4b33cdd0 2015-11-30 stephen.d }
41 4b33cdd0 2015-11-30 stephen.d
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,
47 4b33cdd0 2015-11-30 stephen.d }
48 4b33cdd0 2015-11-30 stephen.d
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
52 4b33cdd0 2015-11-30 stephen.d }
53 4b33cdd0 2015-11-30 stephen.d
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
57 4b33cdd0 2015-11-30 stephen.d }
58 4b33cdd0 2015-11-30 stephen.d
59 4b33cdd0 2015-11-30 stephen.d return rauth.Qid, nil
60 4b33cdd0 2015-11-30 stephen.d }
61 4b33cdd0 2015-11-30 stephen.d
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,
68 4b33cdd0 2015-11-30 stephen.d }
69 4b33cdd0 2015-11-30 stephen.d
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
73 4b33cdd0 2015-11-30 stephen.d }
74 4b33cdd0 2015-11-30 stephen.d
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
78 4b33cdd0 2015-11-30 stephen.d }
79 4b33cdd0 2015-11-30 stephen.d
80 4b33cdd0 2015-11-30 stephen.d return rattach.Qid, nil
81 4b33cdd0 2015-11-30 stephen.d }
82 4b33cdd0 2015-11-30 stephen.d
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,
86 4b33cdd0 2015-11-30 stephen.d })
87 4b33cdd0 2015-11-30 stephen.d if err != nil {
88 4b33cdd0 2015-11-30 stephen.d return err
89 4b33cdd0 2015-11-30 stephen.d }
90 4b33cdd0 2015-11-30 stephen.d
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
94 4b33cdd0 2015-11-30 stephen.d }
95 4b33cdd0 2015-11-30 stephen.d
96 4b33cdd0 2015-11-30 stephen.d return nil
97 4b33cdd0 2015-11-30 stephen.d }
98 4b33cdd0 2015-11-30 stephen.d
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,
102 4b33cdd0 2015-11-30 stephen.d })
103 4b33cdd0 2015-11-30 stephen.d if err != nil {
104 4b33cdd0 2015-11-30 stephen.d return err
105 4b33cdd0 2015-11-30 stephen.d }
106 4b33cdd0 2015-11-30 stephen.d
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
110 4b33cdd0 2015-11-30 stephen.d }
111 4b33cdd0 2015-11-30 stephen.d
112 4b33cdd0 2015-11-30 stephen.d return nil
113 4b33cdd0 2015-11-30 stephen.d }
114 4b33cdd0 2015-11-30 stephen.d
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
118 4b33cdd0 2015-11-30 stephen.d }
119 4b33cdd0 2015-11-30 stephen.d
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,
124 4b33cdd0 2015-11-30 stephen.d })
125 4b33cdd0 2015-11-30 stephen.d if err != nil {
126 4b33cdd0 2015-11-30 stephen.d return nil, err
127 4b33cdd0 2015-11-30 stephen.d }
128 4b33cdd0 2015-11-30 stephen.d
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
132 4b33cdd0 2015-11-30 stephen.d }
133 4b33cdd0 2015-11-30 stephen.d
134 4b33cdd0 2015-11-30 stephen.d return rwalk.Qids, nil
135 4b33cdd0 2015-11-30 stephen.d }
136 4b33cdd0 2015-11-30 stephen.d
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)),
142 4b33cdd0 2015-11-30 stephen.d })
143 4b33cdd0 2015-11-30 stephen.d if err != nil {
144 4b33cdd0 2015-11-30 stephen.d return 0, err
145 4b33cdd0 2015-11-30 stephen.d }
146 4b33cdd0 2015-11-30 stephen.d
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
150 4b33cdd0 2015-11-30 stephen.d }
151 4b33cdd0 2015-11-30 stephen.d
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.
160 c74282f8 2016-11-16 noreply }
161 c74282f8 2016-11-16 noreply
162 c74282f8 2016-11-16 noreply return n, err
163 4b33cdd0 2015-11-30 stephen.d }
164 4b33cdd0 2015-11-30 stephen.d
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,
170 4b33cdd0 2015-11-30 stephen.d })
171 4b33cdd0 2015-11-30 stephen.d if err != nil {
172 4b33cdd0 2015-11-30 stephen.d return 0, err
173 4b33cdd0 2015-11-30 stephen.d }
174 4b33cdd0 2015-11-30 stephen.d
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
178 4b33cdd0 2015-11-30 stephen.d }
179 4b33cdd0 2015-11-30 stephen.d
180 c74282f8 2016-11-16 noreply if int(rwrite.Count) < len(p) {
181 c74282f8 2016-11-16 noreply err = io.ErrShortWrite
182 c74282f8 2016-11-16 noreply }
183 c74282f8 2016-11-16 noreply
184 c74282f8 2016-11-16 noreply return int(rwrite.Count), err
185 4b33cdd0 2015-11-30 stephen.d }
186 4b33cdd0 2015-11-30 stephen.d
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,
191 4b33cdd0 2015-11-30 stephen.d })
192 4b33cdd0 2015-11-30 stephen.d if err != nil {
193 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
194 4b33cdd0 2015-11-30 stephen.d }
195 4b33cdd0 2015-11-30 stephen.d
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
199 4b33cdd0 2015-11-30 stephen.d }
200 4b33cdd0 2015-11-30 stephen.d
201 4b33cdd0 2015-11-30 stephen.d return ropen.Qid, ropen.IOUnit, nil
202 4b33cdd0 2015-11-30 stephen.d }
203 4b33cdd0 2015-11-30 stephen.d
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,
210 4b33cdd0 2015-11-30 stephen.d })
211 4b33cdd0 2015-11-30 stephen.d if err != nil {
212 4b33cdd0 2015-11-30 stephen.d return Qid{}, 0, err
213 4b33cdd0 2015-11-30 stephen.d }
214 4b33cdd0 2015-11-30 stephen.d
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
218 4b33cdd0 2015-11-30 stephen.d }
219 4b33cdd0 2015-11-30 stephen.d
220 4b33cdd0 2015-11-30 stephen.d return rcreate.Qid, rcreate.IOUnit, nil
221 4b33cdd0 2015-11-30 stephen.d }
222 4b33cdd0 2015-11-30 stephen.d
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
227 4b33cdd0 2015-11-30 stephen.d }
228 4b33cdd0 2015-11-30 stephen.d
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
232 4b33cdd0 2015-11-30 stephen.d }
233 4b33cdd0 2015-11-30 stephen.d
234 4b33cdd0 2015-11-30 stephen.d return rstat.Stat, nil
235 4b33cdd0 2015-11-30 stephen.d }
236 4b33cdd0 2015-11-30 stephen.d
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,
241 4b33cdd0 2015-11-30 stephen.d })
242 4b33cdd0 2015-11-30 stephen.d if err != nil {
243 4b33cdd0 2015-11-30 stephen.d return err
244 4b33cdd0 2015-11-30 stephen.d }
245 4b33cdd0 2015-11-30 stephen.d
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
249 4b33cdd0 2015-11-30 stephen.d }
250 4b33cdd0 2015-11-30 stephen.d
251 4b33cdd0 2015-11-30 stephen.d return nil
252 4b33cdd0 2015-11-30 stephen.d }