Blame


1 8a7ec697 2015-10-26 adrien package p9pnew
2 8a7ec697 2015-10-26 adrien
3 8a7ec697 2015-10-26 adrien import (
4 8a7ec697 2015-10-26 adrien "golang.org/x/net/context"
5 8a7ec697 2015-10-26 adrien
6 8a7ec697 2015-10-26 adrien "net"
7 8a7ec697 2015-10-26 adrien )
8 8a7ec697 2015-10-26 adrien
9 8a7ec697 2015-10-26 adrien type client struct {
10 fb37ce2a 2015-10-30 stephen.d version string
11 dce371f6 2015-11-05 stephen.d msize int
12 e6bcde66 2015-10-29 stephen.d ctx context.Context
13 e6bcde66 2015-10-29 stephen.d transport roundTripper
14 8a7ec697 2015-10-26 adrien }
15 8a7ec697 2015-10-26 adrien
16 5f1e8105 2015-10-28 stephen.d // NewSession returns a session using the connection. The Context ctx provides
17 5f1e8105 2015-10-28 stephen.d // a context for out of bad messages, such as flushes, that may be sent by the
18 5f1e8105 2015-10-28 stephen.d // session. The session can effectively shutdown with this context.
19 5f1e8105 2015-10-28 stephen.d func NewSession(ctx context.Context, conn net.Conn) (Session, error) {
20 dce371f6 2015-11-05 stephen.d ch := newChannel(conn, codec9p{}, DefaultMSize) // sets msize, effectively.
21 fb37ce2a 2015-10-30 stephen.d
22 fb37ce2a 2015-10-30 stephen.d // negotiate the protocol version
23 dce371f6 2015-11-05 stephen.d version, err := clientnegotiate(ctx, ch, DefaultVersion)
24 fb37ce2a 2015-10-30 stephen.d if err != nil {
25 fb37ce2a 2015-10-30 stephen.d return nil, err
26 fb37ce2a 2015-10-30 stephen.d }
27 fb37ce2a 2015-10-30 stephen.d
28 8a7ec697 2015-10-26 adrien return &client{
29 dce371f6 2015-11-05 stephen.d version: version,
30 dce371f6 2015-11-05 stephen.d msize: ch.MSize(),
31 e6bcde66 2015-10-29 stephen.d ctx: ctx,
32 fb37ce2a 2015-10-30 stephen.d transport: newTransport(ctx, ch),
33 499f8c59 2015-10-27 stephen.d }, nil
34 8a7ec697 2015-10-26 adrien }
35 8a7ec697 2015-10-26 adrien
36 8a7ec697 2015-10-26 adrien var _ Session = &client{}
37 8a7ec697 2015-10-26 adrien
38 dce371f6 2015-11-05 stephen.d func (c *client) Version() (int, string) {
39 fb37ce2a 2015-10-30 stephen.d return c.msize, c.version
40 fb37ce2a 2015-10-30 stephen.d }
41 fb37ce2a 2015-10-30 stephen.d
42 8a7ec697 2015-10-26 adrien func (c *client) Auth(ctx context.Context, afid Fid, uname, aname string) (Qid, error) {
43 deb98ab4 2015-11-05 stephen.d m := MessageTauth{
44 deb98ab4 2015-11-05 stephen.d Afid: afid,
45 deb98ab4 2015-11-05 stephen.d Uname: uname,
46 deb98ab4 2015-11-05 stephen.d Aname: aname,
47 deb98ab4 2015-11-05 stephen.d }
48 deb98ab4 2015-11-05 stephen.d
49 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, m)
50 deb98ab4 2015-11-05 stephen.d if err != nil {
51 deb98ab4 2015-11-05 stephen.d return Qid{}, nil
52 deb98ab4 2015-11-05 stephen.d }
53 deb98ab4 2015-11-05 stephen.d
54 deb98ab4 2015-11-05 stephen.d rauth, ok := resp.(MessageRauth)
55 deb98ab4 2015-11-05 stephen.d if !ok {
56 deb98ab4 2015-11-05 stephen.d return Qid{}, ErrUnexpectedMsg
57 deb98ab4 2015-11-05 stephen.d }
58 deb98ab4 2015-11-05 stephen.d
59 deb98ab4 2015-11-05 stephen.d return rauth.Qid, nil
60 8a7ec697 2015-10-26 adrien }
61 8a7ec697 2015-10-26 adrien
62 8a7ec697 2015-10-26 adrien func (c *client) Attach(ctx context.Context, fid, afid Fid, uname, aname string) (Qid, error) {
63 deb98ab4 2015-11-05 stephen.d m := MessageTattach{
64 deb98ab4 2015-11-05 stephen.d Fid: fid,
65 deb98ab4 2015-11-05 stephen.d Afid: afid,
66 deb98ab4 2015-11-05 stephen.d Uname: uname,
67 deb98ab4 2015-11-05 stephen.d Aname: aname,
68 5f1e8105 2015-10-28 stephen.d }
69 5f1e8105 2015-10-28 stephen.d
70 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, m)
71 5f1e8105 2015-10-28 stephen.d if err != nil {
72 5f1e8105 2015-10-28 stephen.d return Qid{}, err
73 5f1e8105 2015-10-28 stephen.d }
74 5f1e8105 2015-10-28 stephen.d
75 deb98ab4 2015-11-05 stephen.d rattach, ok := resp.(MessageRattach)
76 5f1e8105 2015-10-28 stephen.d if !ok {
77 deb98ab4 2015-11-05 stephen.d return Qid{}, ErrUnexpectedMsg
78 5f1e8105 2015-10-28 stephen.d }
79 5f1e8105 2015-10-28 stephen.d
80 deb98ab4 2015-11-05 stephen.d return rattach.Qid, nil
81 8a7ec697 2015-10-26 adrien }
82 8a7ec697 2015-10-26 adrien
83 8a7ec697 2015-10-26 adrien func (c *client) Clunk(ctx context.Context, fid Fid) error {
84 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, MessageTclunk{
85 e6bcde66 2015-10-29 stephen.d Fid: fid,
86 e6bcde66 2015-10-29 stephen.d })
87 e6bcde66 2015-10-29 stephen.d if err != nil {
88 e6bcde66 2015-10-29 stephen.d return err
89 e6bcde66 2015-10-29 stephen.d }
90 e6bcde66 2015-10-29 stephen.d
91 deb98ab4 2015-11-05 stephen.d _, ok := resp.(MessageRclunk)
92 deb98ab4 2015-11-05 stephen.d if !ok {
93 deb98ab4 2015-11-05 stephen.d return ErrUnexpectedMsg
94 e6bcde66 2015-10-29 stephen.d }
95 e6bcde66 2015-10-29 stephen.d
96 e6bcde66 2015-10-29 stephen.d return nil
97 8a7ec697 2015-10-26 adrien }
98 8a7ec697 2015-10-26 adrien
99 8a7ec697 2015-10-26 adrien func (c *client) Remove(ctx context.Context, fid Fid) error {
100 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, MessageTremove{
101 deb98ab4 2015-11-05 stephen.d Fid: fid,
102 deb98ab4 2015-11-05 stephen.d })
103 deb98ab4 2015-11-05 stephen.d if err != nil {
104 deb98ab4 2015-11-05 stephen.d return err
105 deb98ab4 2015-11-05 stephen.d }
106 deb98ab4 2015-11-05 stephen.d
107 deb98ab4 2015-11-05 stephen.d _, ok := resp.(MessageRremove)
108 deb98ab4 2015-11-05 stephen.d if !ok {
109 deb98ab4 2015-11-05 stephen.d return ErrUnexpectedMsg
110 deb98ab4 2015-11-05 stephen.d }
111 deb98ab4 2015-11-05 stephen.d
112 deb98ab4 2015-11-05 stephen.d return nil
113 8a7ec697 2015-10-26 adrien }
114 8a7ec697 2015-10-26 adrien
115 8a7ec697 2015-10-26 adrien func (c *client) Walk(ctx context.Context, fid Fid, newfid Fid, names ...string) ([]Qid, error) {
116 5f1e8105 2015-10-28 stephen.d if len(names) > 16 {
117 deb98ab4 2015-11-05 stephen.d return nil, ErrWalkLimit
118 5f1e8105 2015-10-28 stephen.d }
119 5f1e8105 2015-10-28 stephen.d
120 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, MessageTwalk{
121 deb98ab4 2015-11-05 stephen.d Fid: fid,
122 deb98ab4 2015-11-05 stephen.d Newfid: newfid,
123 deb98ab4 2015-11-05 stephen.d Wnames: names,
124 deb98ab4 2015-11-05 stephen.d })
125 5f1e8105 2015-10-28 stephen.d if err != nil {
126 5f1e8105 2015-10-28 stephen.d return nil, err
127 5f1e8105 2015-10-28 stephen.d }
128 5f1e8105 2015-10-28 stephen.d
129 deb98ab4 2015-11-05 stephen.d rwalk, ok := resp.(MessageRwalk)
130 5f1e8105 2015-10-28 stephen.d if !ok {
131 deb98ab4 2015-11-05 stephen.d return nil, ErrUnexpectedMsg
132 5f1e8105 2015-10-28 stephen.d }
133 5f1e8105 2015-10-28 stephen.d
134 deb98ab4 2015-11-05 stephen.d return rwalk.Qids, nil
135 8a7ec697 2015-10-26 adrien }
136 8a7ec697 2015-10-26 adrien
137 8a7ec697 2015-10-26 adrien func (c *client) Read(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) {
138 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, MessageTread{
139 deb98ab4 2015-11-05 stephen.d Fid: fid,
140 deb98ab4 2015-11-05 stephen.d Offset: uint64(offset),
141 deb98ab4 2015-11-05 stephen.d Count: uint32(len(p)),
142 deb98ab4 2015-11-05 stephen.d })
143 5f1e8105 2015-10-28 stephen.d if err != nil {
144 5f1e8105 2015-10-28 stephen.d return 0, err
145 5f1e8105 2015-10-28 stephen.d }
146 5f1e8105 2015-10-28 stephen.d
147 deb98ab4 2015-11-05 stephen.d rread, ok := resp.(MessageRread)
148 5f1e8105 2015-10-28 stephen.d if !ok {
149 deb98ab4 2015-11-05 stephen.d return 0, ErrUnexpectedMsg
150 5f1e8105 2015-10-28 stephen.d }
151 5f1e8105 2015-10-28 stephen.d
152 deb98ab4 2015-11-05 stephen.d return copy(p, rread.Data), nil
153 8a7ec697 2015-10-26 adrien }
154 8a7ec697 2015-10-26 adrien
155 8a7ec697 2015-10-26 adrien func (c *client) Write(ctx context.Context, fid Fid, p []byte, offset int64) (n int, err error) {
156 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, MessageTwrite{
157 deb98ab4 2015-11-05 stephen.d Fid: fid,
158 deb98ab4 2015-11-05 stephen.d Offset: uint64(offset),
159 deb98ab4 2015-11-05 stephen.d Data: p,
160 deb98ab4 2015-11-05 stephen.d })
161 5f1e8105 2015-10-28 stephen.d if err != nil {
162 5f1e8105 2015-10-28 stephen.d return 0, err
163 5f1e8105 2015-10-28 stephen.d }
164 5f1e8105 2015-10-28 stephen.d
165 deb98ab4 2015-11-05 stephen.d rwrite, ok := resp.(MessageRwrite)
166 5f1e8105 2015-10-28 stephen.d if !ok {
167 deb98ab4 2015-11-05 stephen.d return 0, ErrUnexpectedMsg
168 5f1e8105 2015-10-28 stephen.d }
169 5f1e8105 2015-10-28 stephen.d
170 deb98ab4 2015-11-05 stephen.d return int(rwrite.Count), nil
171 8a7ec697 2015-10-26 adrien }
172 8a7ec697 2015-10-26 adrien
173 deb98ab4 2015-11-05 stephen.d func (c *client) Open(ctx context.Context, fid Fid, mode Flag) (Qid, uint32, error) {
174 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, MessageTopen{
175 e6bcde66 2015-10-29 stephen.d Fid: fid,
176 e6bcde66 2015-10-29 stephen.d Mode: mode,
177 e6bcde66 2015-10-29 stephen.d })
178 e6bcde66 2015-10-29 stephen.d if err != nil {
179 e6bcde66 2015-10-29 stephen.d return Qid{}, 0, err
180 e6bcde66 2015-10-29 stephen.d }
181 e6bcde66 2015-10-29 stephen.d
182 deb98ab4 2015-11-05 stephen.d ropen, ok := resp.(MessageRopen)
183 e6bcde66 2015-10-29 stephen.d if !ok {
184 deb98ab4 2015-11-05 stephen.d return Qid{}, 0, ErrUnexpectedMsg
185 e6bcde66 2015-10-29 stephen.d }
186 e6bcde66 2015-10-29 stephen.d
187 deb98ab4 2015-11-05 stephen.d return ropen.Qid, ropen.IOUnit, nil
188 8a7ec697 2015-10-26 adrien }
189 8a7ec697 2015-10-26 adrien
190 deb98ab4 2015-11-05 stephen.d func (c *client) Create(ctx context.Context, parent Fid, name string, perm uint32, mode Flag) (Qid, uint32, error) {
191 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, MessageTcreate{
192 deb98ab4 2015-11-05 stephen.d Fid: parent,
193 deb98ab4 2015-11-05 stephen.d Name: name,
194 deb98ab4 2015-11-05 stephen.d Perm: perm,
195 deb98ab4 2015-11-05 stephen.d Mode: mode,
196 deb98ab4 2015-11-05 stephen.d })
197 deb98ab4 2015-11-05 stephen.d if err != nil {
198 deb98ab4 2015-11-05 stephen.d return Qid{}, 0, err
199 deb98ab4 2015-11-05 stephen.d }
200 deb98ab4 2015-11-05 stephen.d
201 deb98ab4 2015-11-05 stephen.d rcreate, ok := resp.(MessageRcreate)
202 deb98ab4 2015-11-05 stephen.d if !ok {
203 deb98ab4 2015-11-05 stephen.d return Qid{}, 0, ErrUnexpectedMsg
204 deb98ab4 2015-11-05 stephen.d }
205 deb98ab4 2015-11-05 stephen.d
206 deb98ab4 2015-11-05 stephen.d return rcreate.Qid, rcreate.IOUnit, nil
207 8a7ec697 2015-10-26 adrien }
208 8a7ec697 2015-10-26 adrien
209 74ec7ac9 2015-10-30 stephen.d func (c *client) Stat(ctx context.Context, fid Fid) (Dir, error) {
210 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, MessageTstat{Fid: fid})
211 74ec7ac9 2015-10-30 stephen.d if err != nil {
212 74ec7ac9 2015-10-30 stephen.d return Dir{}, err
213 74ec7ac9 2015-10-30 stephen.d }
214 74ec7ac9 2015-10-30 stephen.d
215 deb98ab4 2015-11-05 stephen.d rstat, ok := resp.(MessageRstat)
216 74ec7ac9 2015-10-30 stephen.d if !ok {
217 deb98ab4 2015-11-05 stephen.d return Dir{}, ErrUnexpectedMsg
218 74ec7ac9 2015-10-30 stephen.d }
219 74ec7ac9 2015-10-30 stephen.d
220 deb98ab4 2015-11-05 stephen.d return rstat.Stat, nil
221 8a7ec697 2015-10-26 adrien }
222 8a7ec697 2015-10-26 adrien
223 deb98ab4 2015-11-05 stephen.d func (c *client) WStat(ctx context.Context, fid Fid, dir Dir) error {
224 deb98ab4 2015-11-05 stephen.d resp, err := c.transport.send(ctx, MessageTwstat{
225 deb98ab4 2015-11-05 stephen.d Fid: fid,
226 deb98ab4 2015-11-05 stephen.d Stat: dir,
227 deb98ab4 2015-11-05 stephen.d })
228 deb98ab4 2015-11-05 stephen.d if err != nil {
229 deb98ab4 2015-11-05 stephen.d return err
230 deb98ab4 2015-11-05 stephen.d }
231 8a7ec697 2015-10-26 adrien
232 deb98ab4 2015-11-05 stephen.d _, ok := resp.(MessageRwstat)
233 deb98ab4 2015-11-05 stephen.d if !ok {
234 deb98ab4 2015-11-05 stephen.d return ErrUnexpectedMsg
235 deb98ab4 2015-11-05 stephen.d }
236 fb37ce2a 2015-10-30 stephen.d
237 deb98ab4 2015-11-05 stephen.d return nil
238 fb37ce2a 2015-10-30 stephen.d }