1 8a7ec697 2015-10-26 adrien package p9pnew
3 499f8c59 2015-10-27 stephen.d import "fmt"
5 8a7ec697 2015-10-26 adrien type FcallType uint8
8 8a7ec697 2015-10-26 adrien Tversion FcallType = iota + 100
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"
103 8a7ec697 2015-10-26 adrien type Fcall struct {
104 499f8c59 2015-10-27 stephen.d Type FcallType
106 8a7ec697 2015-10-26 adrien Message Message
109 e6bcde66 2015-10-29 stephen.d func newFcall(msg Message) *Fcall {
110 74ec7ac9 2015-10-30 stephen.d var tag Tag
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
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,
124 40d4a02d 2015-11-03 stephen.d func newErrorFcall(tag Tag, err error) *Fcall {
125 40d4a02d 2015-11-03 stephen.d var msg Message
127 40d4a02d 2015-11-03 stephen.d switch v := err.(type) {
128 40d4a02d 2015-11-03 stephen.d case MessageRerror:
129 40d4a02d 2015-11-03 stephen.d msg = v
130 40d4a02d 2015-11-03 stephen.d case *MessageRerror:
131 40d4a02d 2015-11-03 stephen.d msg = v
132 40d4a02d 2015-11-03 stephen.d default:
133 40d4a02d 2015-11-03 stephen.d msg = MessageRerror{Ename: v.Error()}
136 40d4a02d 2015-11-03 stephen.d return &Fcall{
137 40d4a02d 2015-11-03 stephen.d Type: Rerror,
138 40d4a02d 2015-11-03 stephen.d Tag: tag,
139 40d4a02d 2015-11-03 stephen.d Message: msg,
143 e6bcde66 2015-10-29 stephen.d func (fc *Fcall) String() string {
144 74ec7ac9 2015-10-30 stephen.d return fmt.Sprintf("%v(%v) %v", fc.Type, fc.Tag, string9p(fc.Message))
147 499f8c59 2015-10-27 stephen.d type Message interface {
148 e6bcde66 2015-10-29 stephen.d // Type indicates the Fcall type of the message. This must match
149 e6bcde66 2015-10-29 stephen.d // Fcall.Type.
150 e6bcde66 2015-10-29 stephen.d Type() FcallType
153 499f8c59 2015-10-27 stephen.d // newMessage returns a new instance of the message based on the Fcall type.
154 499f8c59 2015-10-27 stephen.d func newMessage(typ FcallType) (Message, error) {
155 499f8c59 2015-10-27 stephen.d // NOTE(stevvooe): This is a nasty bit of code but makes the transport
156 499f8c59 2015-10-27 stephen.d // fairly simple to implement.
157 499f8c59 2015-10-27 stephen.d switch typ {
158 e6bcde66 2015-10-29 stephen.d case Tversion:
159 e6bcde66 2015-10-29 stephen.d return &MessageTversion{}, nil
160 e6bcde66 2015-10-29 stephen.d case Rversion:
161 e6bcde66 2015-10-29 stephen.d return &MessageRversion{}, nil
162 8a7ec697 2015-10-26 adrien case Tauth:
163 40d4a02d 2015-11-03 stephen.d return &MessageTauth{}, nil
164 8a7ec697 2015-10-26 adrien case Rauth:
165 40d4a02d 2015-11-03 stephen.d return &MessageRauth{}, nil
166 8a7ec697 2015-10-26 adrien case Tattach:
167 e6bcde66 2015-10-29 stephen.d return &MessageTattach{}, nil
168 8a7ec697 2015-10-26 adrien case Rattach:
169 e6bcde66 2015-10-29 stephen.d return &MessageRattach{}, nil
170 8a7ec697 2015-10-26 adrien case Rerror:
171 e6bcde66 2015-10-29 stephen.d return &MessageRerror{}, nil
172 8a7ec697 2015-10-26 adrien case Tflush:
173 d6198009 2015-10-28 stephen.d return &MessageTflush{}, nil
174 8a7ec697 2015-10-26 adrien case Rflush:
175 97423e8b 2015-10-29 stephen.d return &MessageRflush{}, nil // No message body for this response.
176 8a7ec697 2015-10-26 adrien case Twalk:
177 e9f5e414 2015-10-27 stephen.d return &MessageTwalk{}, nil
178 8a7ec697 2015-10-26 adrien case Rwalk:
179 e9f5e414 2015-10-27 stephen.d return &MessageRwalk{}, nil
180 8a7ec697 2015-10-26 adrien case Topen:
181 e6bcde66 2015-10-29 stephen.d return &MessageTopen{}, nil
182 8a7ec697 2015-10-26 adrien case Ropen:
183 e6bcde66 2015-10-29 stephen.d return &MessageRopen{}, nil
184 8a7ec697 2015-10-26 adrien case Tcreate:
185 40d4a02d 2015-11-03 stephen.d return &MessageTcreate{}, nil
186 8a7ec697 2015-10-26 adrien case Rcreate:
187 40d4a02d 2015-11-03 stephen.d return &MessageRcreate{}, nil
188 8a7ec697 2015-10-26 adrien case Tread:
189 5f1e8105 2015-10-28 stephen.d return &MessageTread{}, nil
190 8a7ec697 2015-10-26 adrien case Rread:
191 d6198009 2015-10-28 stephen.d return &MessageRread{}, nil
192 8a7ec697 2015-10-26 adrien case Twrite:
193 5f1e8105 2015-10-28 stephen.d return &MessageTwrite{}, nil
194 8a7ec697 2015-10-26 adrien case Rwrite:
195 5f1e8105 2015-10-28 stephen.d return &MessageRwrite{}, nil
196 8a7ec697 2015-10-26 adrien case Tclunk:
197 e6bcde66 2015-10-29 stephen.d return &MessageTclunk{}, nil
198 8a7ec697 2015-10-26 adrien case Rclunk:
199 97423e8b 2015-10-29 stephen.d return &MessageRclunk{}, nil // no response body
200 8a7ec697 2015-10-26 adrien case Tremove:
202 8a7ec697 2015-10-26 adrien case Rremove:
204 8a7ec697 2015-10-26 adrien case Tstat:
206 8a7ec697 2015-10-26 adrien case Rstat:
207 d6198009 2015-10-28 stephen.d return &MessageRstat{}, nil
208 8a7ec697 2015-10-26 adrien case Twstat:
210 8a7ec697 2015-10-26 adrien case Rwstat:
214 e6bcde66 2015-10-29 stephen.d return nil, fmt.Errorf("unknown message type")
217 8a7ec697 2015-10-26 adrien // MessageVersion encodes the message body for Tversion and Rversion RPC
218 8a7ec697 2015-10-26 adrien // calls. The body is identical in both directions.
219 e6bcde66 2015-10-29 stephen.d type MessageTversion struct {
220 8a7ec697 2015-10-26 adrien MSize uint32
221 8a7ec697 2015-10-26 adrien Version string
224 e6bcde66 2015-10-29 stephen.d type MessageRversion struct {
225 e6bcde66 2015-10-29 stephen.d MSize uint32
226 e6bcde66 2015-10-29 stephen.d Version string
229 e6bcde66 2015-10-29 stephen.d type MessageTauth struct {
230 e9f5e414 2015-10-27 stephen.d Afid Fid
231 e9f5e414 2015-10-27 stephen.d Uname string
232 e9f5e414 2015-10-27 stephen.d Aname string
235 e6bcde66 2015-10-29 stephen.d type MessageRauth struct {
236 e9f5e414 2015-10-27 stephen.d Qid Qid
239 d6198009 2015-10-28 stephen.d type MessageRerror struct {
240 e9f5e414 2015-10-27 stephen.d Ename string
243 40d4a02d 2015-11-03 stephen.d func (e MessageRerror) Error() string {
244 40d4a02d 2015-11-03 stephen.d return fmt.Sprintf("9p: %v", e.Ename)
247 d6198009 2015-10-28 stephen.d type MessageTflush struct {
248 d6198009 2015-10-28 stephen.d Oldtag Tag
251 97423e8b 2015-10-29 stephen.d type MessageRflush struct{}
253 e9f5e414 2015-10-27 stephen.d type MessageTattach struct {
254 e9f5e414 2015-10-27 stephen.d Fid Fid
255 e9f5e414 2015-10-27 stephen.d Afid Fid
256 e9f5e414 2015-10-27 stephen.d Uname string
257 e9f5e414 2015-10-27 stephen.d Aname string
260 e9f5e414 2015-10-27 stephen.d type MessageRattach struct {
261 e9f5e414 2015-10-27 stephen.d Qid Qid
264 e9f5e414 2015-10-27 stephen.d type MessageTwalk struct {
265 e9f5e414 2015-10-27 stephen.d Fid Fid
266 e9f5e414 2015-10-27 stephen.d Newfid Fid
267 e6bcde66 2015-10-29 stephen.d Wnames []string
270 e9f5e414 2015-10-27 stephen.d type MessageRwalk struct {
271 d6198009 2015-10-28 stephen.d Qids []Qid
274 e9f5e414 2015-10-27 stephen.d type MessageTopen struct {
275 e9f5e414 2015-10-27 stephen.d Fid Fid
276 e9f5e414 2015-10-27 stephen.d Mode uint8
279 e9f5e414 2015-10-27 stephen.d type MessageRopen struct {
280 fb37ce2a 2015-10-30 stephen.d Qid Qid
281 fb37ce2a 2015-10-30 stephen.d IOUnit uint32
284 e9f5e414 2015-10-27 stephen.d type MessageTcreate struct {
285 e9f5e414 2015-10-27 stephen.d Fid Fid
286 e9f5e414 2015-10-27 stephen.d Name string
287 e9f5e414 2015-10-27 stephen.d Perm uint32
288 e9f5e414 2015-10-27 stephen.d Mode uint8
291 d6198009 2015-10-28 stephen.d type MessageRcreate struct {
292 d6198009 2015-10-28 stephen.d Qid Qid
293 d6198009 2015-10-28 stephen.d IOUnit uint32
296 e9f5e414 2015-10-27 stephen.d type MessageTread struct {
297 e9f5e414 2015-10-27 stephen.d Fid Fid
298 e9f5e414 2015-10-27 stephen.d Offset uint64
299 e9f5e414 2015-10-27 stephen.d Count uint32
302 e9f5e414 2015-10-27 stephen.d type MessageRread struct {
303 e9f5e414 2015-10-27 stephen.d Data []byte
306 d6198009 2015-10-28 stephen.d type MessageTwrite struct {
307 d6198009 2015-10-28 stephen.d Fid Fid
308 d6198009 2015-10-28 stephen.d Offset uint64
309 d6198009 2015-10-28 stephen.d Data []byte
312 d6198009 2015-10-28 stephen.d type MessageRwrite struct {
313 d6198009 2015-10-28 stephen.d Count uint32
316 d6198009 2015-10-28 stephen.d type MessageTclunk struct {
317 d6198009 2015-10-28 stephen.d Fid Fid
320 97423e8b 2015-10-29 stephen.d type MessageRclunk struct{}
322 d6198009 2015-10-28 stephen.d type MessageTremove struct {
323 d6198009 2015-10-28 stephen.d Fid Fid
326 40d4a02d 2015-11-03 stephen.d type MessageRremove struct{}
328 d6198009 2015-10-28 stephen.d type MessageTstat struct {
329 d6198009 2015-10-28 stephen.d Fid Fid
332 d6198009 2015-10-28 stephen.d type MessageRstat struct {
333 d6198009 2015-10-28 stephen.d Stat Dir
336 d6198009 2015-10-28 stephen.d type MessageTwstat struct {
337 d6198009 2015-10-28 stephen.d Fid Fid
338 d6198009 2015-10-28 stephen.d Stat Dir
341 e6bcde66 2015-10-29 stephen.d func (MessageTversion) Type() FcallType { return Tversion }
342 e6bcde66 2015-10-29 stephen.d func (MessageRversion) Type() FcallType { return Rversion }
343 e6bcde66 2015-10-29 stephen.d func (MessageTauth) Type() FcallType { return Tauth }
344 e6bcde66 2015-10-29 stephen.d func (MessageRauth) Type() FcallType { return Rauth }
345 e6bcde66 2015-10-29 stephen.d func (MessageTflush) Type() FcallType { return Tflush }
346 97423e8b 2015-10-29 stephen.d func (MessageRflush) Type() FcallType { return Rflush }
347 40d4a02d 2015-11-03 stephen.d func (MessageRerror) Type() FcallType { return Rerror }
348 e6bcde66 2015-10-29 stephen.d func (MessageTattach) Type() FcallType { return Tattach }
349 e6bcde66 2015-10-29 stephen.d func (MessageRattach) Type() FcallType { return Rattach }
350 e6bcde66 2015-10-29 stephen.d func (MessageTwalk) Type() FcallType { return Twalk }
351 e6bcde66 2015-10-29 stephen.d func (MessageRwalk) Type() FcallType { return Rwalk }
352 e6bcde66 2015-10-29 stephen.d func (MessageTopen) Type() FcallType { return Topen }
353 e6bcde66 2015-10-29 stephen.d func (MessageRopen) Type() FcallType { return Ropen }
354 e6bcde66 2015-10-29 stephen.d func (MessageTcreate) Type() FcallType { return Tcreate }
355 e6bcde66 2015-10-29 stephen.d func (MessageRcreate) Type() FcallType { return Rcreate }
356 e6bcde66 2015-10-29 stephen.d func (MessageTread) Type() FcallType { return Tread }
357 e6bcde66 2015-10-29 stephen.d func (MessageRread) Type() FcallType { return Rread }
358 e6bcde66 2015-10-29 stephen.d func (MessageTwrite) Type() FcallType { return Twrite }
359 e6bcde66 2015-10-29 stephen.d func (MessageRwrite) Type() FcallType { return Rwrite }
360 e6bcde66 2015-10-29 stephen.d func (MessageTclunk) Type() FcallType { return Tclunk }
361 97423e8b 2015-10-29 stephen.d func (MessageRclunk) Type() FcallType { return Rclunk }
362 e6bcde66 2015-10-29 stephen.d func (MessageTremove) Type() FcallType { return Tremove }
363 40d4a02d 2015-11-03 stephen.d func (MessageRremove) Type() FcallType { return Rremove }
364 e6bcde66 2015-10-29 stephen.d func (MessageTstat) Type() FcallType { return Tstat }
365 e6bcde66 2015-10-29 stephen.d func (MessageRstat) Type() FcallType { return Rstat }
366 e6bcde66 2015-10-29 stephen.d func (MessageTwstat) Type() FcallType { return Twstat }