Blame


1 8a7ec697 2015-10-26 adrien package p9pnew
2 8a7ec697 2015-10-26 adrien
3 499f8c59 2015-10-27 stephen.d import "fmt"
4 8a7ec697 2015-10-26 adrien
5 8a7ec697 2015-10-26 adrien type FcallType uint8
6 8a7ec697 2015-10-26 adrien
7 8a7ec697 2015-10-26 adrien const (
8 8a7ec697 2015-10-26 adrien Tversion FcallType = iota + 100
9 8a7ec697 2015-10-26 adrien Rversion
10 8a7ec697 2015-10-26 adrien Tauth
11 8a7ec697 2015-10-26 adrien Rauth
12 8a7ec697 2015-10-26 adrien Tattach
13 8a7ec697 2015-10-26 adrien Rattach
14 8a7ec697 2015-10-26 adrien Terror
15 8a7ec697 2015-10-26 adrien Rerror
16 8a7ec697 2015-10-26 adrien Tflush
17 8a7ec697 2015-10-26 adrien Rflush
18 8a7ec697 2015-10-26 adrien Twalk
19 8a7ec697 2015-10-26 adrien Rwalk
20 8a7ec697 2015-10-26 adrien Topen
21 8a7ec697 2015-10-26 adrien Ropen
22 8a7ec697 2015-10-26 adrien Tcreate
23 8a7ec697 2015-10-26 adrien Rcreate
24 8a7ec697 2015-10-26 adrien Tread
25 8a7ec697 2015-10-26 adrien Rread
26 8a7ec697 2015-10-26 adrien Twrite
27 8a7ec697 2015-10-26 adrien Rwrite
28 8a7ec697 2015-10-26 adrien Tclunk
29 8a7ec697 2015-10-26 adrien Rclunk
30 8a7ec697 2015-10-26 adrien Tremove
31 8a7ec697 2015-10-26 adrien Rremove
32 8a7ec697 2015-10-26 adrien Tstat
33 8a7ec697 2015-10-26 adrien Rstat
34 8a7ec697 2015-10-26 adrien Twstat
35 8a7ec697 2015-10-26 adrien Rwstat
36 8a7ec697 2015-10-26 adrien Tmax
37 8a7ec697 2015-10-26 adrien )
38 8a7ec697 2015-10-26 adrien
39 499f8c59 2015-10-27 stephen.d func (fct FcallType) String() string {
40 499f8c59 2015-10-27 stephen.d switch fct {
41 499f8c59 2015-10-27 stephen.d case Tversion:
42 499f8c59 2015-10-27 stephen.d return "Tversion"
43 499f8c59 2015-10-27 stephen.d case Rversion:
44 499f8c59 2015-10-27 stephen.d return "Rversion"
45 499f8c59 2015-10-27 stephen.d case Tauth:
46 499f8c59 2015-10-27 stephen.d return "Tauth"
47 499f8c59 2015-10-27 stephen.d case Rauth:
48 499f8c59 2015-10-27 stephen.d return "Rauth"
49 499f8c59 2015-10-27 stephen.d case Tattach:
50 499f8c59 2015-10-27 stephen.d return "Tattach"
51 499f8c59 2015-10-27 stephen.d case Rattach:
52 499f8c59 2015-10-27 stephen.d return "Rattach"
53 499f8c59 2015-10-27 stephen.d case Terror:
54 499f8c59 2015-10-27 stephen.d // invalid.
55 499f8c59 2015-10-27 stephen.d return "Terror"
56 499f8c59 2015-10-27 stephen.d case Rerror:
57 499f8c59 2015-10-27 stephen.d return "Rerror"
58 499f8c59 2015-10-27 stephen.d case Tflush:
59 499f8c59 2015-10-27 stephen.d return "Tflush"
60 499f8c59 2015-10-27 stephen.d case Rflush:
61 499f8c59 2015-10-27 stephen.d return "Rflush"
62 499f8c59 2015-10-27 stephen.d case Twalk:
63 499f8c59 2015-10-27 stephen.d return "Twalk"
64 499f8c59 2015-10-27 stephen.d case Rwalk:
65 499f8c59 2015-10-27 stephen.d return "Rwalk"
66 499f8c59 2015-10-27 stephen.d case Topen:
67 499f8c59 2015-10-27 stephen.d return "Topen"
68 499f8c59 2015-10-27 stephen.d case Ropen:
69 499f8c59 2015-10-27 stephen.d return "Ropen"
70 499f8c59 2015-10-27 stephen.d case Tcreate:
71 499f8c59 2015-10-27 stephen.d return "Tcreate"
72 499f8c59 2015-10-27 stephen.d case Rcreate:
73 499f8c59 2015-10-27 stephen.d return "Rcreate"
74 499f8c59 2015-10-27 stephen.d case Tread:
75 499f8c59 2015-10-27 stephen.d return "Tread"
76 499f8c59 2015-10-27 stephen.d case Rread:
77 499f8c59 2015-10-27 stephen.d return "Rread"
78 499f8c59 2015-10-27 stephen.d case Twrite:
79 499f8c59 2015-10-27 stephen.d return "Twrite"
80 499f8c59 2015-10-27 stephen.d case Rwrite:
81 499f8c59 2015-10-27 stephen.d return "Rwrite"
82 499f8c59 2015-10-27 stephen.d case Tclunk:
83 499f8c59 2015-10-27 stephen.d return "Tclunk"
84 499f8c59 2015-10-27 stephen.d case Rclunk:
85 499f8c59 2015-10-27 stephen.d return "Rclunk"
86 499f8c59 2015-10-27 stephen.d case Tremove:
87 499f8c59 2015-10-27 stephen.d return "Tremote"
88 499f8c59 2015-10-27 stephen.d case Rremove:
89 499f8c59 2015-10-27 stephen.d return "Rremove"
90 499f8c59 2015-10-27 stephen.d case Tstat:
91 499f8c59 2015-10-27 stephen.d return "Tstat"
92 499f8c59 2015-10-27 stephen.d case Rstat:
93 499f8c59 2015-10-27 stephen.d return "Rstat"
94 499f8c59 2015-10-27 stephen.d case Twstat:
95 499f8c59 2015-10-27 stephen.d return "Twstat"
96 499f8c59 2015-10-27 stephen.d case Rwstat:
97 499f8c59 2015-10-27 stephen.d return "Rwstat"
98 499f8c59 2015-10-27 stephen.d default:
99 499f8c59 2015-10-27 stephen.d return "Tunknown"
100 499f8c59 2015-10-27 stephen.d }
101 499f8c59 2015-10-27 stephen.d }
102 499f8c59 2015-10-27 stephen.d
103 8a7ec697 2015-10-26 adrien type Fcall struct {
104 499f8c59 2015-10-27 stephen.d Type FcallType
105 8a7ec697 2015-10-26 adrien Tag Tag
106 8a7ec697 2015-10-26 adrien Message Message
107 8a7ec697 2015-10-26 adrien }
108 8a7ec697 2015-10-26 adrien
109 e6bcde66 2015-10-29 stephen.d func newFcall(msg Message) *Fcall {
110 74ec7ac9 2015-10-30 stephen.d var tag Tag
111 74ec7ac9 2015-10-30 stephen.d
112 74ec7ac9 2015-10-30 stephen.d switch msg.Type() {
113 74ec7ac9 2015-10-30 stephen.d case Tversion, Rversion:
114 74ec7ac9 2015-10-30 stephen.d tag = NOTAG
115 74ec7ac9 2015-10-30 stephen.d }
116 74ec7ac9 2015-10-30 stephen.d
117 e6bcde66 2015-10-29 stephen.d return &Fcall{
118 e6bcde66 2015-10-29 stephen.d Type: msg.Type(),
119 74ec7ac9 2015-10-30 stephen.d Tag: tag,
120 e6bcde66 2015-10-29 stephen.d Message: msg,
121 e6bcde66 2015-10-29 stephen.d }
122 e6bcde66 2015-10-29 stephen.d }
123 e6bcde66 2015-10-29 stephen.d
124 e6bcde66 2015-10-29 stephen.d func (fc *Fcall) String() string {
125 74ec7ac9 2015-10-30 stephen.d return fmt.Sprintf("%v(%v) %v", fc.Type, fc.Tag, string9p(fc.Message))
126 8a7ec697 2015-10-26 adrien }
127 8a7ec697 2015-10-26 adrien
128 499f8c59 2015-10-27 stephen.d type Message interface {
129 e6bcde66 2015-10-29 stephen.d // Type indicates the Fcall type of the message. This must match
130 e6bcde66 2015-10-29 stephen.d // Fcall.Type.
131 e6bcde66 2015-10-29 stephen.d Type() FcallType
132 8a7ec697 2015-10-26 adrien }
133 8a7ec697 2015-10-26 adrien
134 499f8c59 2015-10-27 stephen.d // newMessage returns a new instance of the message based on the Fcall type.
135 499f8c59 2015-10-27 stephen.d func newMessage(typ FcallType) (Message, error) {
136 499f8c59 2015-10-27 stephen.d // NOTE(stevvooe): This is a nasty bit of code but makes the transport
137 499f8c59 2015-10-27 stephen.d // fairly simple to implement.
138 499f8c59 2015-10-27 stephen.d switch typ {
139 e6bcde66 2015-10-29 stephen.d case Tversion:
140 e6bcde66 2015-10-29 stephen.d return &MessageTversion{}, nil
141 e6bcde66 2015-10-29 stephen.d case Rversion:
142 e6bcde66 2015-10-29 stephen.d return &MessageRversion{}, nil
143 8a7ec697 2015-10-26 adrien case Tauth:
144 8a7ec697 2015-10-26 adrien
145 8a7ec697 2015-10-26 adrien case Rauth:
146 97423e8b 2015-10-29 stephen.d
147 8a7ec697 2015-10-26 adrien case Tattach:
148 e6bcde66 2015-10-29 stephen.d return &MessageTattach{}, nil
149 8a7ec697 2015-10-26 adrien case Rattach:
150 e6bcde66 2015-10-29 stephen.d return &MessageRattach{}, nil
151 8a7ec697 2015-10-26 adrien case Rerror:
152 e6bcde66 2015-10-29 stephen.d return &MessageRerror{}, nil
153 8a7ec697 2015-10-26 adrien case Tflush:
154 d6198009 2015-10-28 stephen.d return &MessageTflush{}, nil
155 8a7ec697 2015-10-26 adrien case Rflush:
156 97423e8b 2015-10-29 stephen.d return &MessageRflush{}, nil // No message body for this response.
157 8a7ec697 2015-10-26 adrien case Twalk:
158 e9f5e414 2015-10-27 stephen.d return &MessageTwalk{}, nil
159 8a7ec697 2015-10-26 adrien case Rwalk:
160 e9f5e414 2015-10-27 stephen.d return &MessageRwalk{}, nil
161 8a7ec697 2015-10-26 adrien case Topen:
162 e6bcde66 2015-10-29 stephen.d return &MessageTopen{}, nil
163 8a7ec697 2015-10-26 adrien case Ropen:
164 e6bcde66 2015-10-29 stephen.d return &MessageRopen{}, nil
165 8a7ec697 2015-10-26 adrien case Tcreate:
166 8a7ec697 2015-10-26 adrien
167 8a7ec697 2015-10-26 adrien case Rcreate:
168 8a7ec697 2015-10-26 adrien
169 8a7ec697 2015-10-26 adrien case Tread:
170 5f1e8105 2015-10-28 stephen.d return &MessageTread{}, nil
171 8a7ec697 2015-10-26 adrien case Rread:
172 d6198009 2015-10-28 stephen.d return &MessageRread{}, nil
173 8a7ec697 2015-10-26 adrien case Twrite:
174 5f1e8105 2015-10-28 stephen.d return &MessageTwrite{}, nil
175 8a7ec697 2015-10-26 adrien case Rwrite:
176 5f1e8105 2015-10-28 stephen.d return &MessageRwrite{}, nil
177 8a7ec697 2015-10-26 adrien case Tclunk:
178 e6bcde66 2015-10-29 stephen.d return &MessageTclunk{}, nil
179 8a7ec697 2015-10-26 adrien case Rclunk:
180 97423e8b 2015-10-29 stephen.d return &MessageRclunk{}, nil // no response body
181 8a7ec697 2015-10-26 adrien case Tremove:
182 8a7ec697 2015-10-26 adrien
183 8a7ec697 2015-10-26 adrien case Rremove:
184 8a7ec697 2015-10-26 adrien
185 8a7ec697 2015-10-26 adrien case Tstat:
186 8a7ec697 2015-10-26 adrien
187 8a7ec697 2015-10-26 adrien case Rstat:
188 d6198009 2015-10-28 stephen.d return &MessageRstat{}, nil
189 8a7ec697 2015-10-26 adrien case Twstat:
190 8a7ec697 2015-10-26 adrien
191 8a7ec697 2015-10-26 adrien case Rwstat:
192 8a7ec697 2015-10-26 adrien
193 8a7ec697 2015-10-26 adrien }
194 8a7ec697 2015-10-26 adrien
195 e6bcde66 2015-10-29 stephen.d return nil, fmt.Errorf("unknown message type")
196 8a7ec697 2015-10-26 adrien }
197 8a7ec697 2015-10-26 adrien
198 8a7ec697 2015-10-26 adrien // MessageVersion encodes the message body for Tversion and Rversion RPC
199 8a7ec697 2015-10-26 adrien // calls. The body is identical in both directions.
200 e6bcde66 2015-10-29 stephen.d type MessageTversion struct {
201 8a7ec697 2015-10-26 adrien MSize uint32
202 8a7ec697 2015-10-26 adrien Version string
203 8a7ec697 2015-10-26 adrien }
204 8a7ec697 2015-10-26 adrien
205 e6bcde66 2015-10-29 stephen.d type MessageRversion struct {
206 e6bcde66 2015-10-29 stephen.d MSize uint32
207 e6bcde66 2015-10-29 stephen.d Version string
208 8a7ec697 2015-10-26 adrien }
209 8a7ec697 2015-10-26 adrien
210 e6bcde66 2015-10-29 stephen.d type MessageTauth struct {
211 e9f5e414 2015-10-27 stephen.d Afid Fid
212 e9f5e414 2015-10-27 stephen.d Uname string
213 e9f5e414 2015-10-27 stephen.d Aname string
214 e9f5e414 2015-10-27 stephen.d }
215 e9f5e414 2015-10-27 stephen.d
216 e6bcde66 2015-10-29 stephen.d type MessageRauth struct {
217 e9f5e414 2015-10-27 stephen.d Qid Qid
218 e9f5e414 2015-10-27 stephen.d }
219 e9f5e414 2015-10-27 stephen.d
220 d6198009 2015-10-28 stephen.d type MessageRerror struct {
221 e9f5e414 2015-10-27 stephen.d Ename string
222 e9f5e414 2015-10-27 stephen.d }
223 e9f5e414 2015-10-27 stephen.d
224 d6198009 2015-10-28 stephen.d type MessageTflush struct {
225 d6198009 2015-10-28 stephen.d Oldtag Tag
226 d6198009 2015-10-28 stephen.d }
227 d6198009 2015-10-28 stephen.d
228 97423e8b 2015-10-29 stephen.d type MessageRflush struct{}
229 97423e8b 2015-10-29 stephen.d
230 e9f5e414 2015-10-27 stephen.d type MessageTattach struct {
231 e9f5e414 2015-10-27 stephen.d Fid Fid
232 e9f5e414 2015-10-27 stephen.d Afid Fid
233 e9f5e414 2015-10-27 stephen.d Uname string
234 e9f5e414 2015-10-27 stephen.d Aname string
235 e9f5e414 2015-10-27 stephen.d }
236 e9f5e414 2015-10-27 stephen.d
237 e9f5e414 2015-10-27 stephen.d type MessageRattach struct {
238 e9f5e414 2015-10-27 stephen.d Qid Qid
239 e9f5e414 2015-10-27 stephen.d }
240 e9f5e414 2015-10-27 stephen.d
241 e9f5e414 2015-10-27 stephen.d type MessageTwalk struct {
242 e9f5e414 2015-10-27 stephen.d Fid Fid
243 e9f5e414 2015-10-27 stephen.d Newfid Fid
244 e6bcde66 2015-10-29 stephen.d Wnames []string
245 e9f5e414 2015-10-27 stephen.d }
246 e9f5e414 2015-10-27 stephen.d
247 e9f5e414 2015-10-27 stephen.d type MessageRwalk struct {
248 d6198009 2015-10-28 stephen.d Qids []Qid
249 e9f5e414 2015-10-27 stephen.d }
250 e9f5e414 2015-10-27 stephen.d
251 e9f5e414 2015-10-27 stephen.d type MessageTopen struct {
252 e9f5e414 2015-10-27 stephen.d Fid Fid
253 e9f5e414 2015-10-27 stephen.d Mode uint8
254 e9f5e414 2015-10-27 stephen.d }
255 e9f5e414 2015-10-27 stephen.d
256 e9f5e414 2015-10-27 stephen.d type MessageRopen struct {
257 fb37ce2a 2015-10-30 stephen.d Qid Qid
258 fb37ce2a 2015-10-30 stephen.d IOUnit uint32
259 e9f5e414 2015-10-27 stephen.d }
260 e9f5e414 2015-10-27 stephen.d
261 e9f5e414 2015-10-27 stephen.d type MessageTcreate struct {
262 e9f5e414 2015-10-27 stephen.d Fid Fid
263 e9f5e414 2015-10-27 stephen.d Name string
264 e9f5e414 2015-10-27 stephen.d Perm uint32
265 e9f5e414 2015-10-27 stephen.d Mode uint8
266 e9f5e414 2015-10-27 stephen.d }
267 e9f5e414 2015-10-27 stephen.d
268 d6198009 2015-10-28 stephen.d type MessageRcreate struct {
269 d6198009 2015-10-28 stephen.d Qid Qid
270 d6198009 2015-10-28 stephen.d IOUnit uint32
271 d6198009 2015-10-28 stephen.d }
272 d6198009 2015-10-28 stephen.d
273 e9f5e414 2015-10-27 stephen.d type MessageTread struct {
274 e9f5e414 2015-10-27 stephen.d Fid Fid
275 e9f5e414 2015-10-27 stephen.d Offset uint64
276 e9f5e414 2015-10-27 stephen.d Count uint32
277 e9f5e414 2015-10-27 stephen.d }
278 e9f5e414 2015-10-27 stephen.d
279 e9f5e414 2015-10-27 stephen.d type MessageRread struct {
280 e9f5e414 2015-10-27 stephen.d Data []byte
281 e9f5e414 2015-10-27 stephen.d }
282 e9f5e414 2015-10-27 stephen.d
283 d6198009 2015-10-28 stephen.d type MessageTwrite struct {
284 d6198009 2015-10-28 stephen.d Fid Fid
285 d6198009 2015-10-28 stephen.d Offset uint64
286 d6198009 2015-10-28 stephen.d Data []byte
287 8a7ec697 2015-10-26 adrien }
288 8a7ec697 2015-10-26 adrien
289 d6198009 2015-10-28 stephen.d type MessageRwrite struct {
290 d6198009 2015-10-28 stephen.d Count uint32
291 d6198009 2015-10-28 stephen.d }
292 d6198009 2015-10-28 stephen.d
293 d6198009 2015-10-28 stephen.d type MessageTclunk struct {
294 d6198009 2015-10-28 stephen.d Fid Fid
295 d6198009 2015-10-28 stephen.d }
296 d6198009 2015-10-28 stephen.d
297 97423e8b 2015-10-29 stephen.d type MessageRclunk struct{}
298 97423e8b 2015-10-29 stephen.d
299 d6198009 2015-10-28 stephen.d type MessageTremove struct {
300 d6198009 2015-10-28 stephen.d Fid Fid
301 d6198009 2015-10-28 stephen.d }
302 d6198009 2015-10-28 stephen.d
303 d6198009 2015-10-28 stephen.d type MessageTstat struct {
304 d6198009 2015-10-28 stephen.d Fid Fid
305 d6198009 2015-10-28 stephen.d }
306 d6198009 2015-10-28 stephen.d
307 d6198009 2015-10-28 stephen.d type MessageRstat struct {
308 d6198009 2015-10-28 stephen.d Stat Dir
309 d6198009 2015-10-28 stephen.d }
310 d6198009 2015-10-28 stephen.d
311 d6198009 2015-10-28 stephen.d type MessageTwstat struct {
312 d6198009 2015-10-28 stephen.d Fid Fid
313 d6198009 2015-10-28 stephen.d Stat Dir
314 d6198009 2015-10-28 stephen.d }
315 e6bcde66 2015-10-29 stephen.d
316 e6bcde66 2015-10-29 stephen.d func (MessageTversion) Type() FcallType { return Tversion }
317 e6bcde66 2015-10-29 stephen.d func (MessageRversion) Type() FcallType { return Rversion }
318 e6bcde66 2015-10-29 stephen.d func (MessageTauth) Type() FcallType { return Tauth }
319 e6bcde66 2015-10-29 stephen.d func (MessageRauth) Type() FcallType { return Rauth }
320 e6bcde66 2015-10-29 stephen.d func (MessageRerror) Type() FcallType { return Rerror }
321 e6bcde66 2015-10-29 stephen.d func (MessageTflush) Type() FcallType { return Tflush }
322 97423e8b 2015-10-29 stephen.d func (MessageRflush) Type() FcallType { return Rflush }
323 e6bcde66 2015-10-29 stephen.d func (MessageTattach) Type() FcallType { return Tattach }
324 e6bcde66 2015-10-29 stephen.d func (MessageRattach) Type() FcallType { return Rattach }
325 e6bcde66 2015-10-29 stephen.d func (MessageTwalk) Type() FcallType { return Twalk }
326 e6bcde66 2015-10-29 stephen.d func (MessageRwalk) Type() FcallType { return Rwalk }
327 e6bcde66 2015-10-29 stephen.d func (MessageTopen) Type() FcallType { return Topen }
328 e6bcde66 2015-10-29 stephen.d func (MessageRopen) Type() FcallType { return Ropen }
329 e6bcde66 2015-10-29 stephen.d func (MessageTcreate) Type() FcallType { return Tcreate }
330 e6bcde66 2015-10-29 stephen.d func (MessageRcreate) Type() FcallType { return Rcreate }
331 e6bcde66 2015-10-29 stephen.d func (MessageTread) Type() FcallType { return Tread }
332 e6bcde66 2015-10-29 stephen.d func (MessageRread) Type() FcallType { return Rread }
333 e6bcde66 2015-10-29 stephen.d func (MessageTwrite) Type() FcallType { return Twrite }
334 e6bcde66 2015-10-29 stephen.d func (MessageRwrite) Type() FcallType { return Rwrite }
335 e6bcde66 2015-10-29 stephen.d func (MessageTclunk) Type() FcallType { return Tclunk }
336 97423e8b 2015-10-29 stephen.d func (MessageRclunk) Type() FcallType { return Rclunk }
337 e6bcde66 2015-10-29 stephen.d func (MessageTremove) Type() FcallType { return Tremove }
338 e6bcde66 2015-10-29 stephen.d func (MessageTstat) Type() FcallType { return Tstat }
339 e6bcde66 2015-10-29 stephen.d func (MessageRstat) Type() FcallType { return Rstat }
340 e6bcde66 2015-10-29 stephen.d func (MessageTwstat) Type() FcallType { return Twstat }